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

openmc-dev / openmc / 20344300244

18 Dec 2025 04:45PM UTC coverage: 82.144% (+0.005%) from 82.139%
20344300244

Pull #3692

github

web-flow
Merge 4c6dd2c78 into a230b8612
Pull Request #3692: Simplify rotational periodic boundary conditions

17003 of 23570 branches covered (72.14%)

Branch coverage included in aggregate %.

5 of 9 new or added lines in 2 files covered. (55.56%)

3 existing lines in 1 file now uncovered.

55106 of 64214 relevant lines covered (85.82%)

43475984.96 hits per line

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

79.17
/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,002,292✔
19
{
20
  // Random ray and Monte Carlo need different treatments at vacuum BCs
21
  if (settings::solver_type == SolverType::RANDOM_RAY) {
57,002,292✔
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,329,163✔
31
  }
32
}
57,002,292✔
33

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

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

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

46
  p.cross_reflective_bc(surf, u);
637,974,143✔
47
}
637,974,143✔
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(
128✔
162
  int i_surf, int j_surf, PeriodicAxis axis)
128✔
163
  : PeriodicBC(i_surf, j_surf)
128✔
164
{
165
  Surface& surf1 {*model::surfaces[i_surf_]};
128✔
166
  Surface& surf2 {*model::surfaces[j_surf_]};
128✔
167

168
  // below convention for right handed coordinate system
169
  switch (axis) {
128!
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
    // 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
32✔
182
    axis_1_idx_ = 0;    // x component independent
32✔
183
    axis_2_idx_ = 2;    // z component dependent
32✔
184
    break;
32✔
185
  case z:
64✔
186
    zero_axis_idx_ = 2; // z component of plane must be zero
64✔
187
    axis_1_idx_ = 0;    // x component independent
64✔
188
    axis_2_idx_ = 1;    // y component dependent
64✔
189
    break;
64✔
NEW
190
  default:
×
NEW
191
    throw std::invalid_argument(
×
NEW
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});
128✔
198
  Direction norm2 = surf2.normal({0, 0, 0});
128✔
199
  // Make sure both surfaces intersect the origin
200
  if (std::abs(surf1.evaluate({0, 0, 0})) > FP_COINCIDENT) {
128!
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) {
128!
UNCOV
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.",
NEW
212
      surf2.id_));
×
213
  }
214

215
  angle_ = compute_periodic_rotation(norm1[axis_2_idx_], norm1[axis_1_idx_],
128✔
216
    norm2[axis_2_idx_], norm2[axis_1_idx_]);
128✔
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));
128✔
220
  if (rem > FP_REL_PRECISION && rem < 1 - FP_REL_PRECISION) {
128!
221
    warning(fmt::format(
64✔
222
      "Rotational periodic BC specified with a rotation "
223
      "angle of {} degrees which does not evenly divide 360 degrees.",
224
      angle_ * 180 / PI));
128✔
225
  }
226
}
128✔
227

228
double RotationalPeriodicBC::compute_periodic_rotation(
128✔
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);
128✔
238
  double theta2 = std::atan2(rise_2, run_2) + PI;
128✔
239
  return theta2 - theta1;
128✔
240
}
241

242
void RotationalPeriodicBC::handle_particle(
510,136✔
243
  Particle& p, const Surface& surf) const
244
{
245
  int new_surface = p.surface() > 0 ? -(j_surf_ + 1) : j_surf_ + 1;
510,136!
246

247
  // Rotate the particle's position and direction.
248
  Position r = p.r();
510,136✔
249
  Direction u = p.u();
510,136✔
250
  double cos_theta = std::cos(angle_);
510,136✔
251
  double sin_theta = std::sin(angle_);
510,136✔
252

253
  Position new_r;
510,136✔
254
  new_r[zero_axis_idx_] = r[zero_axis_idx_];
510,136✔
255
  new_r[axis_1_idx_] = cos_theta * r[axis_1_idx_] - sin_theta * r[axis_2_idx_];
510,136✔
256
  new_r[axis_2_idx_] = sin_theta * r[axis_1_idx_] + cos_theta * r[axis_2_idx_];
510,136✔
257

258
  Direction new_u;
510,136✔
259
  new_u[zero_axis_idx_] = u[zero_axis_idx_];
510,136✔
260
  new_u[axis_1_idx_] = cos_theta * u[axis_1_idx_] - sin_theta * u[axis_2_idx_];
510,136✔
261
  new_u[axis_2_idx_] = sin_theta * u[axis_1_idx_] + cos_theta * u[axis_2_idx_];
510,136✔
262

263
  // Handle the effects of the surface albedo on the particle's weight.
264
  BoundaryCondition::handle_albedo(p, surf);
510,136✔
265

266
  // Pass the new location, direction, and surface to the particle.
267
  p.cross_periodic_bc(surf, new_r, new_u, new_surface);
510,136✔
268
}
510,136✔
269

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