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

openmc-dev / openmc / 20472454196

23 Dec 2025 09:56PM UTC coverage: 82.155% (+0.02%) from 82.139%
20472454196

Pull #3692

github

web-flow
Merge 064b0c3b2 into 3f06a42ab
Pull Request #3692: Fix a bug in rotational periodic boundary conditions

17067 of 23647 branches covered (72.17%)

Branch coverage included in aggregate %.

12 of 16 new or added lines in 3 files covered. (75.0%)

185 existing lines in 9 files now uncovered.

55183 of 64296 relevant lines covered (85.83%)

43492701.44 hits per line

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

77.04
/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
57,035,658✔
19
{
20
  // Random ray and Monte Carlo need different treatments at vacuum BCs
21
  if (settings::solver_type == SolverType::RANDOM_RAY) {
57,035,658✔
22
    // Reflect ray off of the surface
23
    ReflectiveBC().handle_particle(p, surf);
23,673,129✔
24

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

29
  } else {
30
    p.cross_vacuum_bc(surf);
33,362,529✔
31
  }
32
}
57,035,658✔
33

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

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

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

46
  p.cross_reflective_bc(surf, u);
638,431,292✔
47
}
638,431,292✔
48

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

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

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

61
  p.cross_reflective_bc(surf, u);
1,007,149✔
62
}
1,007,149✔
63

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

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

74
  // Make sure the first surface has an appropriate type.
75
  if (const auto* ptr = dynamic_cast<const SurfaceXPlane*>(&surf1)) {
234!
76
  } else if (const auto* ptr = dynamic_cast<const SurfaceYPlane*>(&surf1)) {
180!
77
  } else if (const auto* ptr = dynamic_cast<const SurfaceZPlane*>(&surf1)) {
158!
78
  } else if (const auto* ptr = dynamic_cast<const SurfacePlane*>(&surf1)) {
64!
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)) {
234!
88
  } else if (const auto* ptr = dynamic_cast<const SurfaceYPlane*>(&surf2)) {
180!
89
  } else if (const auto* ptr = dynamic_cast<const SurfaceZPlane*>(&surf2)) {
158!
90
  } else if (const auto* ptr = dynamic_cast<const SurfacePlane*>(&surf2)) {
96!
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};
234✔
102
  Direction u = surf1.normal(origin);
234✔
103
  double d1;
104
  double e1 = surf1.evaluate(origin);
234✔
105
  if (e1 > FP_COINCIDENT) {
234✔
106
    d1 = -surf1.distance(origin, -u, false);
138✔
107
  } else if (e1 < -FP_COINCIDENT) {
96!
108
    d1 = surf1.distance(origin, u, false);
96✔
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);
234✔
116
  if (e2 > FP_COINCIDENT) {
234✔
117
    d2 = -surf2.distance(origin, -u, false);
96✔
118
  } else if (e2 < -FP_COINCIDENT) {
138!
119
    d2 = surf2.distance(origin, u, false);
138✔
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);
234✔
127
}
234✔
128

129
void TranslationalPeriodicBC::handle_particle(
296,797✔
130
  Particle& p, const Surface& surf) const
131
{
132
  int i_particle_surf = p.surface_index();
296,797✔
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;
296,797✔
137
  int new_surface;
138
  if (i_particle_surf == i_surf_) {
296,797✔
139
    new_r = p.r() + translation_;
147,047✔
140
    new_surface = p.surface() > 0 ? j_surf_ + 1 : -(j_surf_ + 1);
147,047✔
141
  } else if (i_particle_surf == j_surf_) {
149,750!
142
    new_r = p.r() - translation_;
149,750✔
143
    new_surface = p.surface() > 0 ? i_surf_ + 1 : -(i_surf_ + 1);
149,750✔
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);
296,797✔
152

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

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

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

168
  // below convention for right handed coordinate system
169
  switch (axis) {
224!
170
  case x:
32✔
171
    zero_axis_idx_ = 0; // x component of plane must be zero
32✔
172
    axis_1_idx_ = 1;    // y component independent
32✔
173
    axis_2_idx_ = 2;    // z component dependent
32✔
174
    break;
32✔
175
  case y:
32✔
176
    zero_axis_idx_ = 1; // y component of plane must be zero
32✔
177
    axis_1_idx_ = 2;    // z component independent
32✔
178
    axis_2_idx_ = 0;    // x component dependent
32✔
179
    break;
32✔
180
  case z:
160✔
181
    zero_axis_idx_ = 2; // z component of plane must be zero
160✔
182
    axis_1_idx_ = 0;    // x component independent
160✔
183
    axis_2_idx_ = 1;    // y component dependent
160✔
184
    break;
160✔
185
  default:
×
186
    throw std::invalid_argument(
×
187
      fmt::format("You've specified an axis that is not x, y, or z."));
×
188
  }
189

190
  Direction ax = {0.0, 0.0, 0.0};
224✔
191
  ax[zero_axis_idx_] = 1.0;
224✔
192

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

213
  // Compute the signed rotation angle about the periodic axis. Note that
214
  // (n1×n2)·a = |n1||n2|sin(θ) and n1·n2 = |n1||n2|cos(θ), where a is the axis
215
  // of rotation.
216
  auto c = norm1.cross(norm2);
224✔
217
  angle_ = std::atan2(c.dot(ax), norm1.dot(norm2));
224✔
218

219
  // If the normals point in the same general direction, the surface sense
220
  // should change when crossing the boundary
221
  flip_sense_ = (norm1.dot(norm2) > 0.0);
224✔
222
  if (!flip_sense_)
224✔
223
    angle_ += PI;
160✔
224

225
  // Normalize range of angle to [-PI,PI].
226
  angle_ = std::remainder(angle_, 2 * PI);
224✔
227

228
  // Warn the user if the angle does not evenly divide a circle
229
  double rem = std::abs(std::remainder((2 * PI / angle_), 1.0));
224✔
230
  if (rem > FP_REL_PRECISION && rem < 1 - FP_REL_PRECISION) {
224!
UNCOV
231
    warning(fmt::format(
×
232
      "Rotational periodic BC specified with a rotation "
233
      "angle of {} degrees which does not evenly divide 360 degrees.",
UNCOV
234
      angle_ * 180 / PI));
×
235
  }
236
}
224✔
237

238
void RotationalPeriodicBC::handle_particle(
1,256,530✔
239
  Particle& p, const Surface& surf) const
240
{
241
  int new_surface = p.surface() > 0 ? -(j_surf_ + 1) : j_surf_ + 1;
1,256,530✔
242
  if (flip_sense_)
1,256,530✔
243
    new_surface = -new_surface;
497,596✔
244

245
  // Rotate the particle's position and direction.
246
  Position r = p.r();
1,256,530✔
247
  Direction u = p.u();
1,256,530✔
248
  double cos_theta = std::cos(angle_);
1,256,530✔
249
  double sin_theta = std::sin(angle_);
1,256,530✔
250

251
  Position new_r;
1,256,530✔
252
  new_r[zero_axis_idx_] = r[zero_axis_idx_];
1,256,530✔
253
  new_r[axis_1_idx_] = cos_theta * r[axis_1_idx_] - sin_theta * r[axis_2_idx_];
1,256,530✔
254
  new_r[axis_2_idx_] = sin_theta * r[axis_1_idx_] + cos_theta * r[axis_2_idx_];
1,256,530✔
255

256
  Direction new_u;
1,256,530✔
257
  new_u[zero_axis_idx_] = u[zero_axis_idx_];
1,256,530✔
258
  new_u[axis_1_idx_] = cos_theta * u[axis_1_idx_] - sin_theta * u[axis_2_idx_];
1,256,530✔
259
  new_u[axis_2_idx_] = sin_theta * u[axis_1_idx_] + cos_theta * u[axis_2_idx_];
1,256,530✔
260

261
  // Handle the effects of the surface albedo on the particle's weight.
262
  BoundaryCondition::handle_albedo(p, surf);
1,256,530✔
263

264
  // Pass the new location, direction, and surface to the particle.
265
  p.cross_periodic_bc(surf, new_r, new_u, new_surface);
1,256,530✔
266
}
1,256,530✔
267

268
} // 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