• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openmc-dev / openmc / 13461947318

21 Feb 2025 05:24PM UTC coverage: 85.019% (+0.05%) from 84.969%
13461947318

Pull #3140

github

web-flow
Merge ece247ada into 2b788ea6e
Pull Request #3140: Add Versioning Support from `version.txt`

8 of 8 new or added lines in 2 files covered. (100.0%)

1293 existing lines in 43 files now uncovered.

50627 of 59548 relevant lines covered (85.02%)

35584918.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

55.17
/include/openmc/position.h
1
#ifndef OPENMC_POSITION_H
2
#define OPENMC_POSITION_H
3

4
#include <cmath> // for sqrt
5
#include <iostream>
6
#include <stdexcept> // for out_of_range
7

8
#include "fmt/format.h"
9
#include "openmc/array.h"
10
#include "openmc/vector.h"
11

12
namespace openmc {
13

14
//==============================================================================
15
//! Type representing a position in Cartesian coordinates
16
//==============================================================================
17

18
struct Position {
19
  // Constructors
20
  Position() = default;
×
21
  Position(double x_, double y_, double z_) : x {x_}, y {y_}, z {z_} {};
×
22
  Position(const double xyz[]) : x {xyz[0]}, y {xyz[1]}, z {xyz[2]} {};
23
  Position(const vector<double>& xyz) : x {xyz[0]}, y {xyz[1]}, z {xyz[2]} {};
×
24
  Position(const array<double, 3>& xyz) : x {xyz[0]}, y {xyz[1]}, z {xyz[2]} {};
25

26
  // Unary operators
27
  Position& operator+=(Position);
28
  Position& operator+=(double);
29
  Position& operator-=(Position);
30
  Position& operator-=(double);
31
  Position& operator*=(Position);
32
  Position& operator*=(double);
33
  Position& operator/=(Position);
34
  Position& operator/=(double);
35
  Position operator-() const;
36

37
  const double& operator[](int i) const
2,147,483,647✔
38
  {
39
    switch (i) {
2,147,483,647✔
40
    case 0:
2,147,483,647✔
41
      return x;
2,147,483,647✔
42
    case 1:
2,147,483,647✔
43
      return y;
2,147,483,647✔
44
    case 2:
2,147,483,647✔
45
      return z;
2,147,483,647✔
46
    default:
×
47
      throw std::out_of_range {"Index in Position must be between 0 and 2."};
×
48
    }
49
  }
50
  double& operator[](int i)
2,147,483,647✔
51
  {
52
    switch (i) {
2,147,483,647✔
53
    case 0:
2,147,483,647✔
54
      return x;
2,147,483,647✔
55
    case 1:
2,147,483,647✔
56
      return y;
2,147,483,647✔
57
    case 2:
2,147,483,647✔
58
      return z;
2,147,483,647✔
59
    default:
×
60
      throw std::out_of_range {"Index in Position must be between 0 and 2."};
×
61
    }
62
  }
63

64
  // Access to x, y, or z by compile time known index (specializations below)
65
  template<int i>
66
  const double& get() const
67
  {
68
    throw std::out_of_range {"Index in Position must be between 0 and 2."};
69
  }
70
  template<int i>
71
  double& get()
72
  {
73
    throw std::out_of_range {"Index in Position must be between 0 and 2."};
74
  }
75

76
  // Other member functions
77

78
  //! Dot product of two vectors
79
  //! \param[in] other Vector to take dot product with
80
  //! \result Resulting dot product
81
  inline double dot(Position other) const
82
  {
83
    return x * other.x + y * other.y + z * other.z;
×
84
  }
85
  inline double norm() const { return std::sqrt(x * x + y * y + z * z); }
86
  inline Position cross(Position other) const
87
  {
88
    return {y * other.z - z * other.y, z * other.x - x * other.z,
89
      x * other.y - y * other.x};
90
  }
91

92
  //! Reflect a direction across a normal vector
93
  //! \param[in] other Vector to reflect across
94
  //! \result Reflected vector
95
  Position reflect(Position n) const;
96

97
  //! Rotate the position by applying a rotation matrix
98
  template<typename T>
99
  Position rotate(const T& rotation) const
100
  {
101
    return {x * rotation[0] + y * rotation[1] + z * rotation[2],
102
      x * rotation[3] + y * rotation[4] + z * rotation[5],
103
      x * rotation[6] + y * rotation[7] + z * rotation[8]};
104
  }
105

106
  //! Rotate the position by applying the inverse of a rotation matrix
107
  //! using the fact that rotation matrices are orthonormal.
108
  template<typename T>
109
  Position inverse_rotate(const T& rotation) const
110
  {
111
    return {x * rotation[0] + y * rotation[3] + z * rotation[6],
112
      x * rotation[1] + y * rotation[4] + z * rotation[7],
113
      x * rotation[2] + y * rotation[5] + z * rotation[8]};
114
  }
115

116
  // Data members
117
  double x = 0.;
118
  double y = 0.;
119
  double z = 0.;
120
};
121

122
// Compile-time known member index access functions
123
template<>
124
inline const double& Position::get<0>() const
125
{
126
  return x;
127
}
128
template<>
129
inline const double& Position::get<1>() const
130
{
131
  return y;
132
}
133
template<>
134
inline const double& Position::get<2>() const
135
{
136
  return z;
137
}
138
template<>
139
inline double& Position::get<0>()
140
{
141
  return x;
142
}
143
template<>
144
inline double& Position::get<1>()
145
{
146
  return y;
147
}
148
template<>
149
inline double& Position::get<2>()
150
{
151
  return z;
152
}
153

154
// Binary operators
155
inline Position operator+(Position a, Position b)
156
{
UNCOV
157
  return a += b;
×
158
}
159
inline Position operator+(Position a, double b)
160
{
161
  return a += b;
162
}
163
inline Position operator+(double a, Position b)
164
{
165
  return b += a;
166
}
167

168
inline Position operator-(Position a, Position b)
169
{
UNCOV
170
  return a -= b;
×
171
}
172
inline Position operator-(Position a, double b)
173
{
174
  return a -= b;
175
}
176
inline Position operator-(double a, Position b)
177
{
178
  return b -= a;
179
}
180

181
inline Position operator*(Position a, Position b)
182
{
UNCOV
183
  return a *= b;
×
184
}
185
inline Position operator*(Position a, double b)
186
{
UNCOV
187
  return a *= b;
×
188
}
189
inline Position operator*(double a, Position b)
190
{
UNCOV
191
  return b *= a;
×
192
}
193

194
inline Position operator/(Position a, Position b)
195
{
196
  return a /= b;
197
}
198
inline Position operator/(Position a, double b)
199
{
200
  return a /= b;
201
}
202
inline Position operator/(double a, Position b)
203
{
204
  return b /= a;
205
}
206

207
inline Position Position::reflect(Position n) const
208
{
209
  const double projection = n.dot(*this);
210
  const double magnitude = n.dot(n);
211
  n *= (2.0 * projection / magnitude);
212
  return *this - n;
213
}
214

215
inline bool operator==(Position a, Position b)
216
{
217
  return a.x == b.x && a.y == b.y && a.z == b.z;
218
}
219

220
inline bool operator!=(Position a, Position b)
221
{
222
  return a.x != b.x || a.y != b.y || a.z != b.z;
223
}
224

225
std::ostream& operator<<(std::ostream& os, Position a);
226

227
//==============================================================================
228
//! Type representing a vector direction in Cartesian coordinates
229
//==============================================================================
230

231
using Direction = Position;
232

233
} // namespace openmc
234

235
namespace fmt {
236

237
template<>
238
struct formatter<openmc::Position> : formatter<std::string> {
239
  template<typename FormatContext>
240
#if FMT_VERSION >= 110000 // Version 11.0.0 and above
241
  auto format(const openmc::Position& pos, FormatContext& ctx) const {
242
#else // For versions below 11.0.0
243
  auto format(const openmc::Position& pos, FormatContext& ctx)
244
  {
245
#endif
246
    return formatter<std::string>::format(
247
      fmt::format("({}, {}, {})", pos.x, pos.y, pos.z), ctx);
248
}
249
}; // namespace fmt
250

251
} // namespace fmt
252

253
#endif // OPENMC_POSITION_H
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc