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

openmc-dev / openmc / 12954288577

24 Jan 2025 05:13PM UTC coverage: 84.833% (-0.1%) from 84.928%
12954288577

Pull #2671

github

web-flow
Merge d2ca87df5 into 560bd22bc
Pull Request #2671: Adding methods to automatically apply results to existing Tally objects.

53 of 65 new or added lines in 7 files covered. (81.54%)

59 existing lines in 20 files now uncovered.

50089 of 59044 relevant lines covered (84.83%)

34992387.74 hits per line

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

57.14
/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✔
UNCOV
46
    default:
×
UNCOV
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✔
UNCOV
59
    default:
×
UNCOV
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 based on a rotation matrix
98
  Position rotate(const vector<double>& rotation) const;
99

100
  // Data members
101
  double x = 0.;
102
  double y = 0.;
103
  double z = 0.;
104
};
105

106
// Compile-time known member index access functions
107
template<>
108
inline const double& Position::get<0>() const
109
{
110
  return x;
111
}
112
template<>
113
inline const double& Position::get<1>() const
114
{
115
  return y;
116
}
117
template<>
118
inline const double& Position::get<2>() const
119
{
120
  return z;
121
}
122
template<>
123
inline double& Position::get<0>()
124
{
125
  return x;
126
}
127
template<>
128
inline double& Position::get<1>()
129
{
130
  return y;
131
}
132
template<>
133
inline double& Position::get<2>()
134
{
135
  return z;
136
}
137

138
// Binary operators
139
inline Position operator+(Position a, Position b)
140
{
141
  return a += b;
×
142
}
143
inline Position operator+(Position a, double b)
144
{
145
  return a += b;
146
}
147
inline Position operator+(double a, Position b)
148
{
149
  return b += a;
150
}
151

152
inline Position operator-(Position a, Position b)
153
{
154
  return a -= b;
×
155
}
156
inline Position operator-(Position a, double b)
157
{
158
  return a -= b;
159
}
160
inline Position operator-(double a, Position b)
161
{
162
  return b -= a;
163
}
164

165
inline Position operator*(Position a, Position b)
166
{
167
  return a *= b;
168
}
169
inline Position operator*(Position a, double b)
170
{
171
  return a *= b;
×
172
}
173
inline Position operator*(double a, Position b)
174
{
175
  return b *= a;
×
176
}
177

178
inline Position operator/(Position a, Position b)
179
{
180
  return a /= b;
181
}
182
inline Position operator/(Position a, double b)
183
{
184
  return a /= b;
185
}
186
inline Position operator/(double a, Position b)
187
{
188
  return b /= a;
189
}
190

191
inline Position Position::reflect(Position n) const
192
{
193
  const double projection = n.dot(*this);
194
  const double magnitude = n.dot(n);
195
  n *= (2.0 * projection / magnitude);
196
  return *this - n;
197
}
198

199
inline bool operator==(Position a, Position b)
200
{
201
  return a.x == b.x && a.y == b.y && a.z == b.z;
202
}
203

204
inline bool operator!=(Position a, Position b)
205
{
206
  return a.x != b.x || a.y != b.y || a.z != b.z;
207
}
208

209
std::ostream& operator<<(std::ostream& os, Position a);
210

211
//==============================================================================
212
//! Type representing a vector direction in Cartesian coordinates
213
//==============================================================================
214

215
using Direction = Position;
216

217
} // namespace openmc
218

219
namespace fmt {
220

221
template<>
222
struct formatter<openmc::Position> : formatter<std::string> {
223
  template<typename FormatContext>
224
#if FMT_VERSION >= 110000 // Version 11.0.0 and above
225
  auto format(const openmc::Position& pos, FormatContext& ctx) const {
226
#else // For versions below 11.0.0
227
  auto format(const openmc::Position& pos, FormatContext& ctx)
228
  {
229
#endif
230
    return formatter<std::string>::format(
231
      fmt::format("({}, {}, {})", pos.x, pos.y, pos.z), ctx);
232
}
233
}; // namespace fmt
234

235
} // namespace fmt
236

237
#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