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

openmc-dev / openmc / 20278909785

16 Dec 2025 06:41PM UTC coverage: 81.834% (-0.09%) from 81.92%
20278909785

Pull #3493

github

web-flow
Merge 9dc7c7a58 into bbfa18d72
Pull Request #3493: Implement vector fitting to replace external `vectfit` package

17020 of 23572 branches covered (72.2%)

Branch coverage included in aggregate %.

188 of 207 new or added lines in 3 files covered. (90.82%)

3101 existing lines in 56 files now uncovered.

54979 of 64410 relevant lines covered (85.36%)

41388074.54 hits per line

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

78.21
/src/boundary_condition.cpp
1
#include "openmc/boundary_condition.h"
2

3
#include <exception>
4

5
#include <fmt/core.h>
6

7
#include "openmc/constants.h"
8
#include "openmc/error.h"
9
#include "openmc/random_ray/random_ray.h"
10
#include "openmc/surface.h"
11

12
namespace openmc {
13

14
//==============================================================================
15
// VacuumBC implementation
16
//==============================================================================
17

18
void VacuumBC::handle_particle(Particle& p, const Surface& surf) const
51,842,648✔
19
{
20
  // Random ray and Monte Carlo need different treatments at vacuum BCs
21
  if (settings::solver_type == SolverType::RANDOM_RAY) {
51,842,648✔
22
    // Reflect ray off of the surface
23
    ReflectiveBC().handle_particle(p, surf);
21,522,492✔
24

25
    // Set ray's angular flux spectrum to vacuum conditions (zero)
26
    RandomRay* r = static_cast<RandomRay*>(&p);
21,522,492✔
27
    std::fill(r->angular_flux_.begin(), r->angular_flux_.end(), 0.0);
21,522,492✔
28

29
  } else {
30
    p.cross_vacuum_bc(surf);
30,320,156✔
31
  }
32
}
51,842,648✔
33

34
//==============================================================================
35
// ReflectiveBC implementation
36
//==============================================================================
37

38
void ReflectiveBC::handle_particle(Particle& p, const Surface& surf) const
580,037,018✔
39
{
40
  Direction u = surf.reflect(p.r(), p.u(), &p);
580,037,018✔
41
  u /= u.norm();
580,037,018✔
42

43
  // Handle the effects of the surface albedo on the particle's weight.
44
  BoundaryCondition::handle_albedo(p, surf);
580,037,018✔
45

46
  p.cross_reflective_bc(surf, u);
580,037,018✔
47
}
580,037,018✔
48

49
//==============================================================================
50
// WhiteBC implementation
51
//==============================================================================
52

53
void WhiteBC::handle_particle(Particle& p, const Surface& surf) const
915,590✔
54
{
55
  Direction u = surf.diffuse_reflect(p.r(), p.u(), p.current_seed());
915,590✔
56
  u /= u.norm();
915,590✔
57

58
  // Handle the effects of the surface albedo on the particle's weight.
59
  BoundaryCondition::handle_albedo(p, surf);
915,590✔
60

61
  p.cross_reflective_bc(surf, u);
915,590✔
62
}
915,590✔
63

64
//==============================================================================
65
// TranslationalPeriodicBC implementation
66
//==============================================================================
67

68
TranslationalPeriodicBC::TranslationalPeriodicBC(int i_surf, int j_surf)
208✔
69
  : PeriodicBC(i_surf, j_surf)
208✔
70
{
71
  Surface& surf1 {*model::surfaces[i_surf_]};
208✔
72
  Surface& surf2 {*model::surfaces[j_surf_]};
208✔
73

74
  // Make sure the first surface has an appropriate type.
75
  if (const auto* ptr = dynamic_cast<const SurfaceXPlane*>(&surf1)) {
208!
76
  } else if (const auto* ptr = dynamic_cast<const SurfaceYPlane*>(&surf1)) {
160!
77
  } else if (const auto* ptr = dynamic_cast<const SurfaceZPlane*>(&surf1)) {
140!
78
  } else if (const auto* ptr = dynamic_cast<const SurfacePlane*>(&surf1)) {
56!
79
  } else {
80
    throw std::invalid_argument(fmt::format(
×
81
      "Surface {} is an invalid type for "
82
      "translational periodic BCs. Only planes are supported for these BCs.",
83
      surf1.id_));
×
84
  }
85

86
  // Make sure the second surface has an appropriate type.
87
  if (const auto* ptr = dynamic_cast<const SurfaceXPlane*>(&surf2)) {
208!
88
  } else if (const auto* ptr = dynamic_cast<const SurfaceYPlane*>(&surf2)) {
160!
89
  } else if (const auto* ptr = dynamic_cast<const SurfaceZPlane*>(&surf2)) {
140!
90
  } else if (const auto* ptr = dynamic_cast<const SurfacePlane*>(&surf2)) {
84!
91
  } else {
92
    throw std::invalid_argument(fmt::format(
×
93
      "Surface {} is an invalid type for "
94
      "translational periodic BCs. Only planes are supported for these BCs.",
95
      surf2.id_));
×
96
  }
97

98
  // Compute the distance from the first surface to the origin.  Check the
99
  // surface evaluate function to decide if the distance is positive, negative,
100
  // or zero.
101
  Position origin {0, 0, 0};
208✔
102
  Direction u = surf1.normal(origin);
208✔
103
  double d1;
104
  double e1 = surf1.evaluate(origin);
208✔
105
  if (e1 > FP_COINCIDENT) {
208✔
106
    d1 = -surf1.distance(origin, -u, false);
124✔
107
  } else if (e1 < -FP_COINCIDENT) {
84!
108
    d1 = surf1.distance(origin, u, false);
84✔
109
  } else {
110
    d1 = 0.0;
×
111
  }
112

113
  // Compute the distance from the second surface to the origin.
114
  double d2;
115
  double e2 = surf2.evaluate(origin);
208✔
116
  if (e2 > FP_COINCIDENT) {
208✔
117
    d2 = -surf2.distance(origin, -u, false);
84✔
118
  } else if (e2 < -FP_COINCIDENT) {
124!
119
    d2 = surf2.distance(origin, u, false);
124✔
120
  } else {
121
    d2 = 0.0;
×
122
  }
123

124
  // Set the translation vector; it's length is the difference in the two
125
  // distances.
126
  translation_ = u * (d2 - d1);
208✔
127
}
208✔
128

129
void TranslationalPeriodicBC::handle_particle(
269,528✔
130
  Particle& p, const Surface& surf) const
131
{
132
  int i_particle_surf = p.surface_index();
269,528✔
133

134
  // Figure out which of the two BC surfaces were struck then find the
135
  // particle's new location and surface.
136
  Position new_r;
269,528✔
137
  int new_surface;
138
  if (i_particle_surf == i_surf_) {
269,528✔
139
    new_r = p.r() + translation_;
133,540✔
140
    new_surface = p.surface() > 0 ? j_surf_ + 1 : -(j_surf_ + 1);
133,540✔
141
  } else if (i_particle_surf == j_surf_) {
135,988!
142
    new_r = p.r() - translation_;
135,988✔
143
    new_surface = p.surface() > 0 ? i_surf_ + 1 : -(i_surf_ + 1);
135,988✔
144
  } else {
145
    throw std::runtime_error(
×
146
      "Called BoundaryCondition::handle_particle after "
147
      "hitting a surface, but that surface is not recognized by the BC.");
×
148
  }
149

150
  // Handle the effects of the surface albedo on the particle's weight.
151
  BoundaryCondition::handle_albedo(p, surf);
269,528✔
152

153
  // Pass the new location and surface to the particle.
154
  p.cross_periodic_bc(surf, new_r, p.u(), new_surface);
269,528✔
155
}
269,528✔
156

157
//==============================================================================
158
// RotationalPeriodicBC implementation
159
//==============================================================================
160

161
RotationalPeriodicBC::RotationalPeriodicBC(
112✔
162
  int i_surf, int j_surf, PeriodicAxis axis)
112✔
163
  : PeriodicBC(i_surf, j_surf)
112✔
164
{
165
  Surface& surf1 {*model::surfaces[i_surf_]};
112✔
166
  Surface& surf2 {*model::surfaces[j_surf_]};
112✔
167

168
  // below convention for right handed coordinate system
169
  switch (axis) {
112!
170
  case x:
28✔
171
    zero_axis_idx_ = 0; // x component of plane must be zero
28✔
172
    axis_1_idx_ = 1;    // y component independent
28✔
173
    axis_2_idx_ = 2;    // z component dependent
28✔
174
    break;
28✔
175
  case y:
28✔
176
    // for a right handed coordinate system, z should be the independent axis
177
    // but this would cause the y-rotation case to be different than the other
178
    // two. using a left handed coordinate system and a negative rotation the
179
    // compute angle and rotation matrix behavior mimics that of the x and z
180
    // cases
181
    zero_axis_idx_ = 1; // y component of plane must be zero
28✔
182
    axis_1_idx_ = 0;    // x component independent
28✔
183
    axis_2_idx_ = 2;    // z component dependent
28✔
184
    break;
28✔
185
  case z:
56✔
186
    zero_axis_idx_ = 2; // z component of plane must be zero
56✔
187
    axis_1_idx_ = 0;    // x component independent
56✔
188
    axis_2_idx_ = 1;    // y component dependent
56✔
189
    break;
56✔
UNCOV
190
  default:
×
UNCOV
191
    throw std::invalid_argument(
×
192
      fmt::format("You've specified an axis that is not x, y, or z."));
×
193
  }
194

195
  // Compute the surface normal vectors and make sure they are perpendicular
196
  // to the correct axis
197
  Direction norm1 = surf1.normal({0, 0, 0});
112✔
198
  Direction norm2 = surf2.normal({0, 0, 0});
112✔
199
  // Make sure both surfaces intersect the origin
200
  if (std::abs(surf1.evaluate({0, 0, 0})) > FP_COINCIDENT) {
112!
UNCOV
201
    throw std::invalid_argument(fmt::format(
×
202
      "Rotational periodic BCs are only "
203
      "supported for rotations about the origin, but surface {} does not "
204
      "intersect the origin.",
UNCOV
205
      surf1.id_));
×
206
  }
207
  if (std::abs(surf2.evaluate({0, 0, 0})) > FP_COINCIDENT) {
112!
208
    throw std::invalid_argument(fmt::format(
×
209
      "Rotational periodic BCs are only "
210
      "supported for rotations about the origin, but surface {} does not "
211
      "intersect the origin.",
UNCOV
212
      surf2.id_));
×
213
  }
214

215
  angle_ = compute_periodic_rotation(norm1[axis_2_idx_], norm1[axis_1_idx_],
112✔
216
    norm2[axis_2_idx_], norm2[axis_1_idx_]);
112✔
217

218
  // Warn the user if the angle does not evenly divide a circle
219
  double rem = std::abs(std::remainder((2 * PI / angle_), 1.0));
112✔
220
  if (rem > FP_REL_PRECISION && rem < 1 - FP_REL_PRECISION) {
112!
221
    warning(fmt::format(
84✔
222
      "Rotational periodic BC specified with a rotation "
223
      "angle of {} degrees which does not evenly divide 360 degrees.",
224
      angle_ * 180 / PI));
168✔
225
  }
226
}
112✔
227

228
double RotationalPeriodicBC::compute_periodic_rotation(
112✔
229
  double rise_1, double run_1, double rise_2, double run_2) const
230
{
231
  // Compute the BC rotation angle.  Here it is assumed that both surface
232
  // normal vectors point inwards---towards the valid geometry region.
233
  // Consequently, the rotation angle is not the difference between the two
234
  // normals, but is instead the difference between one normal and one
235
  // anti-normal.  (An incident ray on one surface must be an outgoing ray on
236
  // the other surface after rotation hence the anti-normal.)
237
  double theta1 = std::atan2(rise_1, run_1);
112✔
238
  double theta2 = std::atan2(rise_2, run_2) + PI;
112✔
239
  return theta2 - theta1;
112✔
240
}
241

242
void RotationalPeriodicBC::handle_particle(
463,760✔
243
  Particle& p, const Surface& surf) const
244
{
245
  int i_particle_surf = p.surface_index();
463,760✔
246

247
  // Figure out which of the two BC surfaces were struck to figure out if a
248
  // forward or backward rotation is required.  Specify the other surface as
249
  // the particle's new surface.
250
  double theta;
251
  int new_surface;
252
  if (i_particle_surf == i_surf_) {
463,760✔
253
    theta = angle_;
232,600✔
254
    new_surface = p.surface() > 0 ? -(j_surf_ + 1) : j_surf_ + 1;
232,600!
255
  } else if (i_particle_surf == j_surf_) {
231,160!
256
    theta = -angle_;
231,160✔
257
    new_surface = p.surface() > 0 ? -(i_surf_ + 1) : i_surf_ + 1;
231,160!
258
  } else {
UNCOV
259
    throw std::runtime_error(
×
260
      "Called BoundaryCondition::handle_particle after "
UNCOV
261
      "hitting a surface, but that surface is not recognized by the BC.");
×
262
  }
263

264
  // Rotate the particle's position and direction about the z-axis.
265
  Position r = p.r();
463,760✔
266
  Direction u = p.u();
463,760✔
267
  double cos_theta = std::cos(theta);
463,760✔
268
  double sin_theta = std::sin(theta);
463,760✔
269

270
  Position new_r;
463,760✔
271
  new_r[zero_axis_idx_] = r[zero_axis_idx_];
463,760✔
272
  new_r[axis_1_idx_] = cos_theta * r[axis_1_idx_] - sin_theta * r[axis_2_idx_];
463,760✔
273
  new_r[axis_2_idx_] = sin_theta * r[axis_1_idx_] + cos_theta * r[axis_2_idx_];
463,760✔
274

275
  Direction new_u;
463,760✔
276
  new_u[zero_axis_idx_] = u[zero_axis_idx_];
463,760✔
277
  new_u[axis_1_idx_] = cos_theta * u[axis_1_idx_] - sin_theta * u[axis_2_idx_];
463,760✔
278
  new_u[axis_2_idx_] = sin_theta * u[axis_1_idx_] + cos_theta * u[axis_2_idx_];
463,760✔
279

280
  // Handle the effects of the surface albedo on the particle's weight.
281
  BoundaryCondition::handle_albedo(p, surf);
463,760✔
282

283
  // Pass the new location, direction, and surface to the particle.
284
  p.cross_periodic_bc(surf, new_r, new_u, new_surface);
463,760✔
285
}
463,760✔
286

287
} // namespace openmc
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

© 2025 Coveralls, Inc