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

openmc-dev / openmc / 35496191655

20 Sep 2026 07:11AM UTC coverage: 81.484% (+0.007%) from 81.477%
35496191655

Pull #4139

github

web-flow
Merge 1690933f6 into afa7a14ac
Pull Request #4139: Fix lost particles after virtual surface crossings in complex regions

19973 of 29002 branches covered (68.87%)

Branch coverage included in aggregate %.

5 of 5 new or added lines in 1 file covered. (100.0%)

592 existing lines in 10 files now uncovered.

62444 of 72143 relevant lines covered (86.56%)

49753632.27 hits per line

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

82.22
/src/surface.cpp
1
#include "openmc/surface.h"
2

3
#include <cmath>
4
#include <complex>
5
#include <initializer_list>
6
#include <iterator>
7
#include <set>
8
#include <utility>
9

10
#include <fmt/core.h>
11

12
#include "openmc/array.h"
13
#include "openmc/cell.h"
14
#include "openmc/container_util.h"
15
#include "openmc/error.h"
16
#include "openmc/external/quartic_solver.h"
17
#include "openmc/hdf5_interface.h"
18
#include "openmc/math_functions.h"
19
#include "openmc/random_lcg.h"
20
#include "openmc/settings.h"
21
#include "openmc/string_utils.h"
22
#include "openmc/xml_interface.h"
23

24
namespace openmc {
25

26
//==============================================================================
27
// Global variables
28
//==============================================================================
29

30
namespace model {
31
std::unordered_map<int, int> surface_map;
32
vector<unique_ptr<Surface>> surfaces;
33
} // namespace model
34

35
//==============================================================================
36
// Helper functions for reading the "coeffs" node of an XML surface element
37
//==============================================================================
38

39
void read_coeffs(
51,328 ✔
40
  pugi::xml_node surf_node, int surf_id, std::initializer_list<double*> coeffs)
41
{
42
  // Check the given number of coefficients.
43
  auto coeffs_file = get_node_array<double>(surf_node, "coeffs");
51,328 ✔
44
  if (coeffs_file.size() != coeffs.size()) {
51,328 !
45
    fatal_error(
×
46
      fmt::format("Surface {} expects {} coefficient but was given {}", surf_id,
×
UNCOV
47
        coeffs.size(), coeffs_file.size()));
×
48
  }
49

50
  // Copy the coefficients
51
  int i = 0;
51,328 ✔
52
  for (auto c : coeffs) {
150,788 ✔
53
    *c = coeffs_file[i++];
99,460 ✔
54
  }
55
}
51,328 ✔
56

57
//==============================================================================
58
// Surface implementation
59
//==============================================================================
60

61
Surface::Surface() {} // empty constructor
851 ✔
62

63
Surface::Surface(pugi::xml_node surf_node)
51,328 ✔
64
{
65
  if (check_for_node(surf_node, "id")) {
51,328 !
66
    id_ = std::stoi(get_node_value(surf_node, "id"));
102,656 ✔
67
    if (contains(settings::source_write_surf_id, id_) ||
102,656 ✔
68
        settings::source_write_surf_id.empty()) {
50,540 ✔
69
      surf_source_ = true;
50,286 ✔
70
    }
71
  } else {
UNCOV
72
    fatal_error("Must specify id of surface in geometry XML file.");
×
73
  }
74

75
  if (check_for_node(surf_node, "name")) {
51,328 ✔
76
    name_ = get_node_value(surf_node, "name", false);
12,319 ✔
77
  }
78

79
  if (check_for_node(surf_node, "boundary")) {
51,328 ✔
80
    std::string surf_bc = get_node_value(surf_node, "boundary", true, true);
30,046 ✔
81

82
    if (surf_bc == "transmission" || surf_bc == "transmit" || surf_bc.empty()) {
60,092 !
83
      // Leave the bc_ a nullptr
84
    } else if (surf_bc == "vacuum") {
30,046 ✔
85
      bc_ = make_unique<VacuumBC>();
14,646 ✔
86
    } else if (surf_bc == "reflective" || surf_bc == "reflect" ||
16,036 !
87
               surf_bc == "reflecting") {
636 !
88
      bc_ = make_unique<ReflectiveBC>();
14,764 ✔
89
    } else if (surf_bc == "white") {
636 ✔
90
      bc_ = make_unique<WhiteBC>();
90 ✔
91
    } else if (surf_bc == "periodic") {
546 !
92
      // Periodic BCs are handled separately
93
    } else {
UNCOV
94
      fatal_error(fmt::format("Unknown boundary condition \"{}\" specified "
×
95
                              "on surface {}",
UNCOV
96
        surf_bc, id_));
×
97
    }
98

99
    if (check_for_node(surf_node, "albedo") && bc_) {
30,046 !
100
      double surf_alb = std::stod(get_node_value(surf_node, "albedo"));
180 ✔
101

102
      if (surf_alb < 0.0)
90 !
UNCOV
103
        fatal_error(fmt::format("Surface {} has an albedo of {}. "
×
104
                                "Albedo values must be positive.",
UNCOV
105
          id_, surf_alb));
×
106

107
      if (surf_alb > 1.0)
90 !
UNCOV
108
        warning(fmt::format("Surface {} has an albedo of {}. "
×
109
                            "Albedos greater than 1 may cause "
110
                            "unphysical behaviour.",
UNCOV
111
          id_, surf_alb));
×
112

113
      bc_->set_albedo(surf_alb);
90 ✔
114
    }
115
  }
30,046 ✔
116
}
51,328 ✔
117

118
bool Surface::sense(Position r, Direction u) const
2,147,483,647 ✔
119
{
120
  // Evaluate the surface equation at the particle's coordinates to determine
121
  // which side the particle is on.
122
  const double f = evaluate(r);
2,147,483,647 ✔
123

124
  // Check which side of surface the point is on.
125
  if (std::abs(f) < FP_COINCIDENT) {
2,147,483,647 ✔
126
    // Particle may be coincident with this surface. To determine the sense, we
127
    // look at the direction of the particle relative to the surface normal (by
128
    // default in the positive direction) via their dot product.
129
    return u.dot(normal(r)) > 0.0;
10,005,691 ✔
130
  }
131
  return f > 0.0;
2,147,483,647 ✔
132
}
133

134
Direction Surface::reflect(Position r, Direction u, GeometryState* p) const
980,320,958 ✔
135
{
136
  // Determine projection of direction onto normal and squared magnitude of
137
  // normal.
138
  Direction n = normal(r);
980,320,958 ✔
139

140
  // Reflect direction according to normal.
141
  return u.reflect(n);
980,320,958 ✔
142
}
143

144
Direction Surface::diffuse_reflect(
1,007,149 ✔
145
  Position r, Direction u, uint64_t* seed) const
146
{
147
  // Diffuse reflect direction according to the normal.
148
  // cosine distribution
149

150
  Direction n = this->normal(r);
1,007,149 ✔
151
  n /= n.norm();
1,007,149 ✔
152
  const double projection = n.dot(u);
1,007,149 ✔
153

154
  // sample from inverse function, u=sqrt(rand) since p(u)=2u, so F(u)=u^2
155
  const double mu =
1,007,149 ✔
156
    (projection >= 0.0) ? -std::sqrt(prn(seed)) : std::sqrt(prn(seed));
1,007,149 ✔
157

158
  // sample azimuthal distribution uniformly
159
  u = rotate_angle(n, mu, nullptr, seed);
1,007,149 ✔
160

161
  // normalize the direction
162
  return u / u.norm();
1,007,149 ✔
163
}
164

165
void Surface::to_hdf5(hid_t group_id) const
42,055 ✔
166
{
167
  hid_t surf_group = create_group(group_id, fmt::format("surface {}", id_));
42,055 ✔
168

169
  if (geom_type() == GeometryType::DAG) {
42,055 ✔
170
    write_string(surf_group, "geom_type", "dagmc", false);
1,268 !
171
  } else if (geom_type() == GeometryType::CSG) {
41,421 !
172
    write_string(surf_group, "geom_type", "csg", false);
41,421 ✔
173

174
    if (bc_) {
41,421 ✔
175
      write_string(surf_group, "boundary_type", bc_->type(), false);
24,999 ✔
176
      bc_->to_hdf5(surf_group);
24,999 ✔
177

178
      // write periodic surface ID
179
      if (bc_->type() == "periodic") {
24,999 ✔
180
        auto pbc = dynamic_cast<PeriodicBC*>(bc_.get());
458 !
181
        Surface& surf1 {*model::surfaces[pbc->i_surf()]};
458 !
182
        Surface& surf2 {*model::surfaces[pbc->j_surf()]};
458 !
183

184
        if (id_ == surf1.id_) {
458 !
185
          write_dataset(surf_group, "periodic_surface_id", surf2.id_);
458 ✔
186
        } else {
UNCOV
187
          write_dataset(surf_group, "periodic_surface_id", surf1.id_);
×
188
        }
189
      }
190
    } else {
191
      write_string(surf_group, "boundary_type", "transmission", false);
32,844 ✔
192
    }
193
  }
194

195
  if (!name_.empty()) {
42,055 ✔
196
    write_string(surf_group, "name", name_, false);
10,037 ✔
197
  }
198

199
  to_hdf5_inner(surf_group);
42,055 ✔
200

201
  close_group(surf_group);
42,055 ✔
202
}
42,055 ✔
203

204
//==============================================================================
205
// Generic functions for x-, y-, and z-, planes.
206
//==============================================================================
207

208
// The template parameter indicates the axis normal to the plane.
209
template<int i>
210
double axis_aligned_plane_distance(
2,147,483,647 ✔
211
  Position r, Direction u, bool coincident, double offset)
212
{
213
  const double f = offset - r[i];
2,147,483,647 ✔
214
  if (coincident || std::abs(f) < FP_COINCIDENT || u[i] == 0.0)
2,147,483,647 ✔
215
    return INFTY;
216
  const double d = f / u[i];
2,147,483,647 ✔
217
  if (d < 0.0)
2,147,483,647 ✔
218
    return INFTY;
2,147,483,647 ✔
219
  return d;
220
}
221

222
//==============================================================================
223
// SurfaceXPlane implementation
224
//==============================================================================
225

226
SurfaceXPlane::SurfaceXPlane(pugi::xml_node surf_node) : Surface(surf_node)
13,649 ✔
227
{
228
  read_coeffs(surf_node, id_, {&x0_});
13,649 ✔
229
}
13,649 ✔
230

231
double SurfaceXPlane::evaluate(Position r) const
2,147,483,647 ✔
232
{
233
  return r.x - x0_;
2,147,483,647 ✔
234
}
235

236
double SurfaceXPlane::distance(Position r, Direction u, bool coincident) const
2,147,483,647 ✔
237
{
238
  return axis_aligned_plane_distance<0>(r, u, coincident, x0_);
2,147,483,647 ✔
239
}
240

241
Direction SurfaceXPlane::normal(Position r) const
374,845,629 ✔
242
{
243
  return {1., 0., 0.};
374,845,629 ✔
244
}
245

246
void SurfaceXPlane::to_hdf5_inner(hid_t group_id) const
10,393 ✔
247
{
248
  write_string(group_id, "type", "x-plane", false);
10,393 ✔
249
  array<double, 1> coeffs {{x0_}};
10,393 ✔
250
  write_dataset(group_id, "coefficients", coeffs);
10,393 ✔
251
}
10,393 ✔
252

253
BoundingBox SurfaceXPlane::bounding_box(bool pos_side) const
242 ✔
254
{
255
  if (pos_side) {
242 ✔
256
    return {{x0_, -INFTY, -INFTY}, {INFTY, INFTY, INFTY}};
121 ✔
257
  } else {
258
    return {{-INFTY, -INFTY, -INFTY}, {x0_, INFTY, INFTY}};
121 ✔
259
  }
260
}
261

262
//==============================================================================
263
// SurfaceYPlane implementation
264
//==============================================================================
265

266
SurfaceYPlane::SurfaceYPlane(pugi::xml_node surf_node) : Surface(surf_node)
11,434 ✔
267
{
268
  read_coeffs(surf_node, id_, {&y0_});
11,434 ✔
269
}
11,434 ✔
270

271
double SurfaceYPlane::evaluate(Position r) const
2,147,483,647 ✔
272
{
273
  return r.y - y0_;
2,147,483,647 ✔
274
}
275

276
double SurfaceYPlane::distance(Position r, Direction u, bool coincident) const
2,147,483,647 ✔
277
{
278
  return axis_aligned_plane_distance<1>(r, u, coincident, y0_);
2,147,483,647 ✔
279
}
280

281
Direction SurfaceYPlane::normal(Position r) const
524,796,120 ✔
282
{
283
  return {0., 1., 0.};
524,796,120 ✔
284
}
285

286
void SurfaceYPlane::to_hdf5_inner(hid_t group_id) const
9,570 ✔
287
{
288
  write_string(group_id, "type", "y-plane", false);
9,570 ✔
289
  array<double, 1> coeffs {{y0_}};
9,570 ✔
290
  write_dataset(group_id, "coefficients", coeffs);
9,570 ✔
291
}
9,570 ✔
292

293
BoundingBox SurfaceYPlane::bounding_box(bool pos_side) const
253 ✔
294
{
295
  if (pos_side) {
253 ✔
296
    return {{-INFTY, y0_, -INFTY}, {INFTY, INFTY, INFTY}};
121 ✔
297
  } else {
298
    return {{-INFTY, -INFTY, -INFTY}, {INFTY, y0_, INFTY}};
132 ✔
299
  }
300
}
301

302
//==============================================================================
303
// SurfaceZPlane implementation
304
//==============================================================================
305

306
SurfaceZPlane::SurfaceZPlane(pugi::xml_node surf_node) : Surface(surf_node)
8,460 ✔
307
{
308
  read_coeffs(surf_node, id_, {&z0_});
8,460 ✔
309
}
8,460 ✔
310

311
double SurfaceZPlane::evaluate(Position r) const
2,147,483,647 ✔
312
{
313
  return r.z - z0_;
2,147,483,647 ✔
314
}
315

316
double SurfaceZPlane::distance(Position r, Direction u, bool coincident) const
2,147,483,647 ✔
317
{
318
  return axis_aligned_plane_distance<2>(r, u, coincident, z0_);
2,147,483,647 ✔
319
}
320

321
Direction SurfaceZPlane::normal(Position r) const
261,005,339 ✔
322
{
323
  return {0., 0., 1.};
261,005,339 ✔
324
}
325

326
void SurfaceZPlane::to_hdf5_inner(hid_t group_id) const
7,310 ✔
327
{
328
  write_string(group_id, "type", "z-plane", false);
7,310 ✔
329
  array<double, 1> coeffs {{z0_}};
7,310 ✔
330
  write_dataset(group_id, "coefficients", coeffs);
7,310 ✔
331
}
7,310 ✔
332

UNCOV
333
BoundingBox SurfaceZPlane::bounding_box(bool pos_side) const
×
334
{
335
  if (pos_side) {
×
UNCOV
336
    return {{-INFTY, -INFTY, z0_}, {INFTY, INFTY, INFTY}};
×
337
  } else {
UNCOV
338
    return {{-INFTY, -INFTY, -INFTY}, {INFTY, INFTY, z0_}};
×
339
  }
340
}
341

342
//==============================================================================
343
// SurfacePlane implementation
344
//==============================================================================
345

346
SurfacePlane::SurfacePlane(pugi::xml_node surf_node) : Surface(surf_node)
2,183 ✔
347
{
348
  read_coeffs(surf_node, id_, {&A_, &B_, &C_, &D_});
2,183 ✔
349
}
2,183 ✔
350

351
double SurfacePlane::evaluate(Position r) const
362,391,846 ✔
352
{
353
  return A_ * r.x + B_ * r.y + C_ * r.z - D_;
362,391,846 ✔
354
}
355

356
BoundingBox SurfacePlane::bounding_box(bool pos_side) const
154 ✔
357
{
358
  // A general plane bounds a half-space in one direction only when its normal
359
  // is parallel to a coordinate axis; otherwise both half-spaces are unbounded
360
  // along every axis. This mirrors PlaneMixin.bounding_box on the Python side,
361
  // so that a plane whose off-axis coefficients are rotation-matrix roundoff
362
  // (e.g. B = 1 with A = C = 6.1e-17) yields the same box through both APIs.
363
  const Direction n = normal({});
154 ✔
364
  const double norm = n.norm();
154 ✔
365
  if (norm == 0.0)
154 ✔
366
    return {};
11 ✔
367

368
  int axis = -1;
396 ✔
369
  for (int i = 0; i < 3; ++i) {
396 ✔
370
    if (std::abs(std::abs(n[i] / norm) - 1.0) > PLANE_ALIGNMENT_TOL)
341 ✔
371
      continue;
231 ✔
372

373
    bool aligned = true;
396 ✔
374
    for (int j = 0; j < 3; ++j) {
396 ✔
375
      if (j != i && std::abs(n[j] / norm) > PLANE_ALIGNMENT_TOL) {
308 ✔
376
        aligned = false;
377
        break;
378
      }
379
    }
380
    if (aligned) {
110 ✔
381
      axis = i;
382
      break;
383
    }
384
  }
385
  if (axis == -1)
143 ✔
386
    return {};
55 ✔
387

388
  // The half-space is bounded below when the outward normal points along the
389
  // positive axis direction and we are on the positive side, or vice versa.
390
  BoundingBox bbox;
88 ✔
391
  const double intercept = D_ / n[axis];
88 ✔
392
  if (pos_side == (n[axis] > 0.0)) {
88 ✔
393
    bbox.min[axis] = intercept;
55 ✔
394
  } else {
395
    bbox.max[axis] = intercept;
33 ✔
396
  }
397
  return bbox;
88 ✔
398
}
399

400
double SurfacePlane::distance(Position r, Direction u, bool coincident) const
820,914,742 ✔
401
{
402
  const double f = A_ * r.x + B_ * r.y + C_ * r.z - D_;
820,914,742 ✔
403
  const double projection = A_ * u.x + B_ * u.y + C_ * u.z;
820,914,742 ✔
404
  if (coincident || std::abs(f) < FP_COINCIDENT || projection == 0.0) {
820,914,742 !
405
    return INFTY;
406
  } else {
407
    const double d = -f / projection;
753,121,473 ✔
408
    if (d < 0.0)
753,121,473 ✔
409
      return INFTY;
410
    return d;
410,461,477 ✔
411
  }
412
}
413

414
Direction SurfacePlane::normal(Position r) const
5,748,774 ✔
415
{
416
  return {A_, B_, C_};
5,748,774 ✔
417
}
418

419
void SurfacePlane::to_hdf5_inner(hid_t group_id) const
1,579 ✔
420
{
421
  write_string(group_id, "type", "plane", false);
1,579 ✔
422
  array<double, 4> coeffs {{A_, B_, C_, D_}};
1,579 ✔
423
  write_dataset(group_id, "coefficients", coeffs);
1,579 ✔
424
}
1,579 ✔
425

426
//==============================================================================
427
// Generic functions for x-, y-, and z-, cylinders
428
//==============================================================================
429

430
// The template parameters indicate the axes perpendicular to the axis of the
431
// cylinder.  offset1 and offset2 should correspond with i1 and i2,
432
// respectively.
433
template<int i1, int i2>
434
double axis_aligned_cylinder_evaluate(
2,147,483,647 ✔
435
  Position r, double offset1, double offset2, double radius)
436
{
437
  const double r1 = r.get<i1>() - offset1;
2,147,483,647 ✔
438
  const double r2 = r.get<i2>() - offset2;
2,147,483,647 ✔
439
  return r1 * r1 + r2 * r2 - radius * radius;
2,147,483,647 ✔
440
}
441

442
// The first template parameter indicates which axis the cylinder is aligned to.
443
// The other two parameters indicate the other two axes.  offset1 and offset2
444
// should correspond with i2 and i3, respectively.
445
template<int i1, int i2, int i3>
446
double axis_aligned_cylinder_distance(Position r, Direction u, bool coincident,
2,147,483,647 ✔
447
  double offset1, double offset2, double radius)
448
{
449
  const double a = 1.0 - u.get<i1>() * u.get<i1>(); // u^2 + v^2
2,147,483,647 ✔
450
  if (a == 0.0)
2,147,483,647 ✔
451
    return INFTY;
452

453
  const double r2 = r.get<i2>() - offset1;
2,147,483,647 ✔
454
  const double r3 = r.get<i3>() - offset2;
2,147,483,647 ✔
455
  const double k = r2 * u.get<i2>() + r3 * u.get<i3>();
2,147,483,647 ✔
456
  const double c = r2 * r2 + r3 * r3 - radius * radius;
2,147,483,647 ✔
457
  const double quad = k * k - a * c;
2,147,483,647 ✔
458

459
  if (quad < 0.0) {
2,147,483,647 ✔
460
    // No intersection with cylinder.
461
    return INFTY;
462

463
  } else if (coincident || std::abs(c) < FP_COINCIDENT) {
2,147,483,647 ✔
464
    // Particle is on the cylinder, thus one distance is positive/negative
465
    // and the other is zero. The sign of k determines if we are facing in or
466
    // out.
467
    if (k >= 0.0) {
1,908,184,013 ✔
468
      return INFTY;
469
    } else {
470
      return (-k + sqrt(quad)) / a;
1,090,398,489 ✔
471
    }
472

473
  } else if (c < 0.0) {
2,147,483,647 ✔
474
    // Particle is inside the cylinder, thus one distance must be negative
475
    // and one must be positive. The positive distance will be the one with
476
    // negative sign on sqrt(quad).
477
    return (-k + sqrt(quad)) / a;
1,077,424,081 ✔
478

479
  } else {
480
    // Particle is outside the cylinder, thus both distances are either
481
    // positive or negative. If positive, the smaller distance is the one
482
    // with positive sign on sqrt(quad).
483
    const double d = (-k - sqrt(quad)) / a;
1,466,822,445 ✔
484
    if (d < 0.0)
1,466,822,445 ✔
485
      return INFTY;
486
    return d;
1,143,010,610 ✔
487
  }
488
}
489

490
// The first template parameter indicates which axis the cylinder is aligned to.
491
// The other two parameters indicate the other two axes.  offset1 and offset2
492
// should correspond with i2 and i3, respectively.
493
template<int i1, int i2, int i3>
494
Direction axis_aligned_cylinder_normal(
281,139,633 ✔
495
  Position r, double offset1, double offset2)
496
{
497
  Direction u;
281,139,633 ✔
498
  u.get<i2>() = 2.0 * (r.get<i2>() - offset1);
281,139,633 ✔
499
  u.get<i3>() = 2.0 * (r.get<i3>() - offset2);
281,139,633 ✔
500
  u.get<i1>() = 0.0;
501
  return u;
502
}
503

504
//==============================================================================
505
// SurfaceXCylinder implementation
506
//==============================================================================
507

508
SurfaceXCylinder::SurfaceXCylinder(pugi::xml_node surf_node)
45 ✔
509
  : Surface(surf_node)
45 ✔
510
{
511
  read_coeffs(surf_node, id_, {&y0_, &z0_, &radius_});
45 ✔
512
}
45 ✔
513

514
double SurfaceXCylinder::evaluate(Position r) const
1,277,480 ✔
515
{
516
  return axis_aligned_cylinder_evaluate<1, 2>(r, y0_, z0_, radius_);
1,277,480 ✔
517
}
518

519
double SurfaceXCylinder::distance(
1,622,038 ✔
520
  Position r, Direction u, bool coincident) const
521
{
522
  return axis_aligned_cylinder_distance<0, 1, 2>(
1,622,038 ✔
523
    r, u, coincident, y0_, z0_, radius_);
1,622,038 ✔
524
}
525

526
Direction SurfaceXCylinder::normal(Position r) const
397,936 ✔
527
{
528
  return axis_aligned_cylinder_normal<0, 1, 2>(r, y0_, z0_);
397,936 ✔
529
}
530

531
void SurfaceXCylinder::to_hdf5_inner(hid_t group_id) const
33 ✔
532
{
533
  write_string(group_id, "type", "x-cylinder", false);
33 ✔
534
  array<double, 3> coeffs {{y0_, z0_, radius_}};
33 ✔
535
  write_dataset(group_id, "coefficients", coeffs);
33 ✔
536
}
33 ✔
537

UNCOV
538
BoundingBox SurfaceXCylinder::bounding_box(bool pos_side) const
×
539
{
540
  if (!pos_side) {
×
541
    return {{-INFTY, y0_ - radius_, z0_ - radius_},
×
UNCOV
542
      {INFTY, y0_ + radius_, z0_ + radius_}};
×
543
  } else {
UNCOV
544
    return {};
×
545
  }
546
}
547
//==============================================================================
548
// SurfaceYCylinder implementation
549
//==============================================================================
550

551
SurfaceYCylinder::SurfaceYCylinder(pugi::xml_node surf_node)
26 ✔
552
  : Surface(surf_node)
26 ✔
553
{
554
  read_coeffs(surf_node, id_, {&x0_, &z0_, &radius_});
26 ✔
555
}
26 ✔
556

557
double SurfaceYCylinder::evaluate(Position r) const
170,984 ✔
558
{
559
  return axis_aligned_cylinder_evaluate<0, 2>(r, x0_, z0_, radius_);
170,984 ✔
560
}
561

562
double SurfaceYCylinder::distance(
659,769 ✔
563
  Position r, Direction u, bool coincident) const
564
{
565
  return axis_aligned_cylinder_distance<1, 0, 2>(
659,769 ✔
566
    r, u, coincident, x0_, z0_, radius_);
659,769 ✔
567
}
568

UNCOV
569
Direction SurfaceYCylinder::normal(Position r) const
×
570
{
UNCOV
571
  return axis_aligned_cylinder_normal<1, 0, 2>(r, x0_, z0_);
×
572
}
573

574
void SurfaceYCylinder::to_hdf5_inner(hid_t group_id) const
22 ✔
575
{
576
  write_string(group_id, "type", "y-cylinder", false);
22 ✔
577
  array<double, 3> coeffs {{x0_, z0_, radius_}};
22 ✔
578
  write_dataset(group_id, "coefficients", coeffs);
22 ✔
579
}
22 ✔
580

581
BoundingBox SurfaceYCylinder::bounding_box(bool pos_side) const
11 ✔
582
{
583
  if (!pos_side) {
11 !
584
    return {{x0_ - radius_, -INFTY, z0_ - radius_},
11 ✔
585
      {x0_ + radius_, INFTY, z0_ + radius_}};
11 ✔
586
  } else {
UNCOV
587
    return {};
×
588
  }
589
}
590

591
//==============================================================================
592
// SurfaceZCylinder implementation
593
//==============================================================================
594

595
SurfaceZCylinder::SurfaceZCylinder(pugi::xml_node surf_node)
5,818 ✔
596
  : Surface(surf_node)
5,818 ✔
597
{
598
  read_coeffs(surf_node, id_, {&x0_, &y0_, &radius_});
5,818 ✔
599
}
5,818 ✔
600

601
double SurfaceZCylinder::evaluate(Position r) const
2,147,483,647 ✔
602
{
603
  return axis_aligned_cylinder_evaluate<0, 1>(r, x0_, y0_, radius_);
2,147,483,647 ✔
604
}
605

606
double SurfaceZCylinder::distance(
2,147,483,647 ✔
607
  Position r, Direction u, bool coincident) const
608
{
609
  return axis_aligned_cylinder_distance<2, 0, 1>(
2,147,483,647 ✔
610
    r, u, coincident, x0_, y0_, radius_);
2,147,483,647 ✔
611
}
612

613
Direction SurfaceZCylinder::normal(Position r) const
280,741,697 ✔
614
{
615
  return axis_aligned_cylinder_normal<2, 0, 1>(r, x0_, y0_);
280,741,697 ✔
616
}
617

618
void SurfaceZCylinder::to_hdf5_inner(hid_t group_id) const
4,596 ✔
619
{
620
  write_string(group_id, "type", "z-cylinder", false);
4,596 ✔
621
  array<double, 3> coeffs {{x0_, y0_, radius_}};
4,596 ✔
622
  write_dataset(group_id, "coefficients", coeffs);
4,596 ✔
623
}
4,596 ✔
624

625
BoundingBox SurfaceZCylinder::bounding_box(bool pos_side) const
44 ✔
626
{
627
  if (!pos_side) {
44 ✔
628
    return {{x0_ - radius_, y0_ - radius_, -INFTY},
22 ✔
629
      {x0_ + radius_, y0_ + radius_, INFTY}};
22 ✔
630
  } else {
631
    return {};
22 ✔
632
  }
633
}
634

635
//==============================================================================
636
// SurfaceSphere implementation
637
//==============================================================================
638

639
SurfaceSphere::SurfaceSphere(pugi::xml_node surf_node) : Surface(surf_node)
9,417 ✔
640
{
641
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &radius_});
9,417 ✔
642
}
9,417 ✔
643

644
double SurfaceSphere::evaluate(Position r) const
1,256,372,558 ✔
645
{
646
  const double x = r.x - x0_;
1,256,372,558 ✔
647
  const double y = r.y - y0_;
1,256,372,558 ✔
648
  const double z = r.z - z0_;
1,256,372,558 ✔
649
  return x * x + y * y + z * z - radius_ * radius_;
1,256,372,558 ✔
650
}
651

652
double SurfaceSphere::distance(Position r, Direction u, bool coincident) const
2,136,764,819 ✔
653
{
654
  const double x = r.x - x0_;
2,136,764,819 ✔
655
  const double y = r.y - y0_;
2,136,764,819 ✔
656
  const double z = r.z - z0_;
2,136,764,819 ✔
657
  const double k = x * u.x + y * u.y + z * u.z;
2,136,764,819 ✔
658
  const double c = x * x + y * y + z * z - radius_ * radius_;
2,136,764,819 ✔
659
  const double quad = k * k - c;
2,136,764,819 ✔
660

661
  if (quad < 0.0) {
2,136,764,819 ✔
662
    // No intersection with sphere.
663
    return INFTY;
664

665
  } else if (coincident || std::abs(c) < FP_COINCIDENT) {
1,948,141,133 !
666
    // Particle is on the sphere, thus one distance is positive/negative and
667
    // the other is zero. The sign of k determines if we are facing in or out.
668
    if (k >= 0.0) {
117,728,647 ✔
669
      return INFTY;
670
    } else {
671
      return -k + sqrt(quad);
67,118,232 ✔
672
    }
673

674
  } else if (c < 0.0) {
1,830,412,486 ✔
675
    // Particle is inside the sphere, thus one distance must be negative and
676
    // one must be positive. The positive distance will be the one with
677
    // negative sign on sqrt(quad)
678
    return -k + sqrt(quad);
1,762,703,835 ✔
679

680
  } else {
681
    // Particle is outside the sphere, thus both distances are either positive
682
    // or negative. If positive, the smaller distance is the one with positive
683
    // sign on sqrt(quad).
684
    const double d = -k - sqrt(quad);
67,708,651 ✔
685
    if (d < 0.0)
67,708,651 ✔
686
      return INFTY;
687
    return d;
52,899,319 ✔
688
  }
689
}
690

691
Direction SurfaceSphere::normal(Position r) const
389,726,810 ✔
692
{
693
  return {2.0 * (r.x - x0_), 2.0 * (r.y - y0_), 2.0 * (r.z - z0_)};
389,726,810 ✔
694
}
695

696
void SurfaceSphere::to_hdf5_inner(hid_t group_id) const
7,698 ✔
697
{
698
  write_string(group_id, "type", "sphere", false);
7,698 ✔
699
  array<double, 4> coeffs {{x0_, y0_, z0_, radius_}};
7,698 ✔
700
  write_dataset(group_id, "coefficients", coeffs);
7,698 ✔
701
}
7,698 ✔
702

UNCOV
703
BoundingBox SurfaceSphere::bounding_box(bool pos_side) const
×
704
{
705
  if (!pos_side) {
×
706
    return {{x0_ - radius_, y0_ - radius_, z0_ - radius_},
×
UNCOV
707
      {x0_ + radius_, y0_ + radius_, z0_ + radius_}};
×
708
  } else {
UNCOV
709
    return {};
×
710
  }
711
}
712

713
//==============================================================================
714
// Generic functions for x-, y-, and z-, cones
715
//==============================================================================
716

717
// The first template parameter indicates which axis the cone is aligned to.
718
// The other two parameters indicate the other two axes.  offset1, offset2,
719
// and offset3 should correspond with i1, i2, and i3, respectively.
720
template<int i1, int i2, int i3>
721
double axis_aligned_cone_evaluate(
325,853 ✔
722
  Position r, double offset1, double offset2, double offset3, double radius_sq)
723
{
724
  const double r1 = r.get<i1>() - offset1;
325,853 ✔
725
  const double r2 = r.get<i2>() - offset2;
325,853 ✔
726
  const double r3 = r.get<i3>() - offset3;
325,853 ✔
727
  return r2 * r2 + r3 * r3 - radius_sq * r1 * r1;
325,853 ✔
728
}
729

730
// The first template parameter indicates which axis the cone is aligned to.
731
// The other two parameters indicate the other two axes.  offset1, offset2,
732
// and offset3 should correspond with i1, i2, and i3, respectively.
733
template<int i1, int i2, int i3>
734
double axis_aligned_cone_distance(Position r, Direction u, bool coincident,
978,021 ✔
735
  double offset1, double offset2, double offset3, double radius_sq)
736
{
737
  const double r1 = r.get<i1>() - offset1;
978,021 ✔
738
  const double r2 = r.get<i2>() - offset2;
978,021 ✔
739
  const double r3 = r.get<i3>() - offset3;
978,021 ✔
740
  const double a = u.get<i2>() * u.get<i2>() + u.get<i3>() * u.get<i3>() -
978,021 ✔
741
                   radius_sq * u.get<i1>() * u.get<i1>();
978,021 ✔
742
  const double k =
978,021 ✔
743
    r2 * u.get<i2>() + r3 * u.get<i3>() - radius_sq * r1 * u.get<i1>();
978,021 ✔
744
  const double c = r2 * r2 + r3 * r3 - radius_sq * r1 * r1;
978,021 ✔
745
  double quad = k * k - a * c;
978,021 ✔
746

747
  double d;
748

749
  if (quad < 0.0) {
978,021 !
750
    // No intersection with cone.
751
    return INFTY;
752

753
  } else if (coincident || std::abs(c) < FP_COINCIDENT) {
978,021 !
754
    // Particle is on the cone, thus one distance is positive/negative
755
    // and the other is zero. The sign of k determines if we are facing in or
756
    // out.
757
    if (k >= 0.0) {
59,510 !
UNCOV
758
      d = (-k - sqrt(quad)) / a;
×
759
    } else {
760
      d = (-k + sqrt(quad)) / a;
59,510 ✔
761
    }
762

763
  } else {
764
    // Calculate both solutions to the quadratic.
765
    quad = sqrt(quad);
918,511 ✔
766
    d = (-k - quad) / a;
918,511 ✔
767
    const double b = (-k + quad) / a;
918,511 ✔
768

769
    // Determine the smallest positive solution.
770
    if (d < 0.0) {
918,511 ✔
771
      if (b > 0.0)
780,043 ✔
772
        d = b;
665,104 ✔
773
    } else {
774
      if (b > 0.0) {
138,468 !
775
        if (b < d)
138,468 !
776
          d = b;
138,468 ✔
777
      }
778
    }
779
  }
780

781
  // If the distance was negative, set boundary distance to infinity.
782
  if (d <= 0.0)
978,021 ✔
783
    return INFTY;
136,422 ✔
784
  return d;
785
}
786

787
// The first template parameter indicates which axis the cone is aligned to.
788
// The other two parameters indicate the other two axes.  offset1, offset2,
789
// and offset3 should correspond with i1, i2, and i3, respectively.
790
template<int i1, int i2, int i3>
791
Direction axis_aligned_cone_normal(
59,510 ✔
792
  Position r, double offset1, double offset2, double offset3, double radius_sq)
793
{
794
  Direction u;
795
  u.get<i1>() = -2.0 * radius_sq * (r.get<i1>() - offset1);
59,510 ✔
796
  u.get<i2>() = 2.0 * (r.get<i2>() - offset2);
59,510 ✔
797
  u.get<i3>() = 2.0 * (r.get<i3>() - offset3);
59,510 ✔
798
  return u;
799
}
800

801
//==============================================================================
802
// SurfaceXCone implementation
803
//==============================================================================
804

UNCOV
805
SurfaceXCone::SurfaceXCone(pugi::xml_node surf_node) : Surface(surf_node)
×
806
{
807
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &radius_sq_});
×
UNCOV
808
}
×
809

UNCOV
810
double SurfaceXCone::evaluate(Position r) const
×
811
{
UNCOV
812
  return axis_aligned_cone_evaluate<0, 1, 2>(r, x0_, y0_, z0_, radius_sq_);
×
813
}
814

UNCOV
815
double SurfaceXCone::distance(Position r, Direction u, bool coincident) const
×
816
{
817
  return axis_aligned_cone_distance<0, 1, 2>(
×
UNCOV
818
    r, u, coincident, x0_, y0_, z0_, radius_sq_);
×
819
}
820

UNCOV
821
Direction SurfaceXCone::normal(Position r) const
×
822
{
UNCOV
823
  return axis_aligned_cone_normal<0, 1, 2>(r, x0_, y0_, z0_, radius_sq_);
×
824
}
825

UNCOV
826
void SurfaceXCone::to_hdf5_inner(hid_t group_id) const
×
827
{
828
  write_string(group_id, "type", "x-cone", false);
×
829
  array<double, 4> coeffs {{x0_, y0_, z0_, radius_sq_}};
×
830
  write_dataset(group_id, "coefficients", coeffs);
×
UNCOV
831
}
×
832

833
//==============================================================================
834
// SurfaceYCone implementation
835
//==============================================================================
836

UNCOV
837
SurfaceYCone::SurfaceYCone(pugi::xml_node surf_node) : Surface(surf_node)
×
838
{
839
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &radius_sq_});
×
UNCOV
840
}
×
841

UNCOV
842
double SurfaceYCone::evaluate(Position r) const
×
843
{
UNCOV
844
  return axis_aligned_cone_evaluate<1, 0, 2>(r, y0_, x0_, z0_, radius_sq_);
×
845
}
846

UNCOV
847
double SurfaceYCone::distance(Position r, Direction u, bool coincident) const
×
848
{
849
  return axis_aligned_cone_distance<1, 0, 2>(
×
UNCOV
850
    r, u, coincident, y0_, x0_, z0_, radius_sq_);
×
851
}
852

UNCOV
853
Direction SurfaceYCone::normal(Position r) const
×
854
{
UNCOV
855
  return axis_aligned_cone_normal<1, 0, 2>(r, y0_, x0_, z0_, radius_sq_);
×
856
}
857

UNCOV
858
void SurfaceYCone::to_hdf5_inner(hid_t group_id) const
×
859
{
860
  write_string(group_id, "type", "y-cone", false);
×
861
  array<double, 4> coeffs {{x0_, y0_, z0_, radius_sq_}};
×
862
  write_dataset(group_id, "coefficients", coeffs);
×
UNCOV
863
}
×
864

865
//==============================================================================
866
// SurfaceZCone implementation
867
//==============================================================================
868

869
SurfaceZCone::SurfaceZCone(pugi::xml_node surf_node) : Surface(surf_node)
15 ✔
870
{
871
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &radius_sq_});
15 ✔
872
}
15 ✔
873

874
double SurfaceZCone::evaluate(Position r) const
325,853 ✔
875
{
876
  return axis_aligned_cone_evaluate<2, 0, 1>(r, z0_, x0_, y0_, radius_sq_);
325,853 ✔
877
}
878

879
double SurfaceZCone::distance(Position r, Direction u, bool coincident) const
978,021 ✔
880
{
881
  return axis_aligned_cone_distance<2, 0, 1>(
978,021 ✔
882
    r, u, coincident, z0_, x0_, y0_, radius_sq_);
978,021 ✔
883
}
884

885
Direction SurfaceZCone::normal(Position r) const
59,510 ✔
886
{
887
  return axis_aligned_cone_normal<2, 0, 1>(r, z0_, x0_, y0_, radius_sq_);
59,510 ✔
888
}
889

890
void SurfaceZCone::to_hdf5_inner(hid_t group_id) const
11 ✔
891
{
892
  write_string(group_id, "type", "z-cone", false);
11 ✔
893
  array<double, 4> coeffs {{x0_, y0_, z0_, radius_sq_}};
11 ✔
894
  write_dataset(group_id, "coefficients", coeffs);
11 ✔
895
}
11 ✔
896

897
//==============================================================================
898
// SurfaceQuadric implementation
899
//==============================================================================
900

901
SurfaceQuadric::SurfaceQuadric(pugi::xml_node surf_node) : Surface(surf_node)
26 ✔
902
{
903
  read_coeffs(
52 ✔
904
    surf_node, id_, {&A_, &B_, &C_, &D_, &E_, &F_, &G_, &H_, &J_, &K_});
26 ✔
905
}
26 ✔
906

907
double SurfaceQuadric::evaluate(Position r) const
172,565 ✔
908
{
909
  const double x = r.x;
172,565 ✔
910
  const double y = r.y;
172,565 ✔
911
  const double z = r.z;
172,565 ✔
912
  return x * (A_ * x + D_ * y + G_) + y * (B_ * y + E_ * z + H_) +
172,565 ✔
913
         z * (C_ * z + F_ * x + J_) + K_;
172,565 ✔
914
}
915

916
double SurfaceQuadric::distance(
316,690 ✔
917
  Position r, Direction ang, bool coincident) const
918
{
919
  const double& x = r.x;
316,690 ✔
920
  const double& y = r.y;
316,690 ✔
921
  const double& z = r.z;
316,690 ✔
922
  const double& u = ang.x;
316,690 ✔
923
  const double& v = ang.y;
316,690 ✔
924
  const double& w = ang.z;
316,690 ✔
925

926
  const double a =
316,690 ✔
927
    A_ * u * u + B_ * v * v + C_ * w * w + D_ * u * v + E_ * v * w + F_ * u * w;
316,690 ✔
928
  const double k = A_ * u * x + B_ * v * y + C_ * w * z +
316,690 ✔
929
                   0.5 * (D_ * (u * y + v * x) + E_ * (v * z + w * y) +
316,690 ✔
930
                           F_ * (w * x + u * z) + G_ * u + H_ * v + J_ * w);
316,690 ✔
931
  const double c = A_ * x * x + B_ * y * y + C_ * z * z + D_ * x * y +
316,690 ✔
932
                   E_ * y * z + F_ * x * z + G_ * x + H_ * y + J_ * z + K_;
316,690 ✔
933
  double quad = k * k - a * c;
316,690 ✔
934

935
  double d;
316,690 ✔
936

937
  if (quad < 0.0) {
316,690 !
938
    // No intersection with surface.
939
    return INFTY;
940

941
  } else if (coincident || std::abs(c) < FP_COINCIDENT) {
316,690 !
942
    // Particle is on the surface, thus one distance is positive/negative and
943
    // the other is zero. The sign of k determines which distance is zero and
944
    // which is not. Additionally, if a is zero, it means the particle is on
945
    // a plane-like surface.
946
    if (a == 0.0) {
36,058 !
947
      d = INFTY; // see the below explanation
948
    } else if (k >= 0.0) {
36,058 !
UNCOV
949
      d = (-k - sqrt(quad)) / a;
×
950
    } else {
951
      d = (-k + sqrt(quad)) / a;
36,058 ✔
952
    }
953

954
  } else if (a == 0.0) {
280,632 !
955
    // Given the orientation of the particle, the quadric looks like a plane in
956
    // this case, and thus we have only one solution despite potentially having
957
    // quad > 0.0. While the term under the square root may be real, in one
958
    // case of the +/- of the quadratic formula, 0/0 results, and in another, a
959
    // finite value over 0 results. Applying L'Hopital's to the 0/0 case gives
960
    // the below. Alternatively this can be found by simply putting a=0 in the
961
    // equation ax^2 + bx + c = 0.
UNCOV
962
    d = -0.5 * c / k;
×
963
  } else {
964
    // Calculate both solutions to the quadratic.
965
    quad = sqrt(quad);
280,632 ✔
966
    d = (-k - quad) / a;
280,632 ✔
967
    double b = (-k + quad) / a;
280,632 ✔
968

969
    // Determine the smallest positive solution.
970
    if (d < 0.0) {
280,632 ✔
971
      if (b > 0.0)
280,621 !
972
        d = b;
280,621 ✔
973
    } else {
974
      if (b > 0.0) {
11 !
975
        if (b < d)
11 !
UNCOV
976
          d = b;
×
977
      }
978
    }
979
  }
980

981
  // If the distance was negative, set boundary distance to infinity.
982
  if (d <= 0.0)
316,690 !
UNCOV
983
    return INFTY;
×
984
  return d;
985
}
986

987
Direction SurfaceQuadric::normal(Position r) const
36,058 ✔
988
{
989
  const double& x = r.x;
36,058 ✔
990
  const double& y = r.y;
36,058 ✔
991
  const double& z = r.z;
36,058 ✔
992
  return {2.0 * A_ * x + D_ * y + F_ * z + G_,
36,058 ✔
993
    2.0 * B_ * y + D_ * x + E_ * z + H_, 2.0 * C_ * z + E_ * y + F_ * x + J_};
36,058 ✔
994
}
995

996
void SurfaceQuadric::to_hdf5_inner(hid_t group_id) const
11 ✔
997
{
998
  write_string(group_id, "type", "quadric", false);
11 ✔
999
  array<double, 10> coeffs {{A_, B_, C_, D_, E_, F_, G_, H_, J_, K_}};
11 ✔
1000
  write_dataset(group_id, "coefficients", coeffs);
11 ✔
1001
}
11 ✔
1002

1003
//==============================================================================
1004
// Torus helper functions
1005
//==============================================================================
1006

1007
double torus_distance(double x1, double x2, double x3, double u1, double u2,
26,534,563 ✔
1008
  double u3, double A, double B, double C, bool coincident)
1009
{
1010
  // Coefficients for equation: (c2 t^2 + c1 t + c0)^2 = c2' t^2 + c1' t + c0'
1011
  double D = (C * C) / (B * B);
26,534,563 ✔
1012
  double c2 = u1 * u1 + u2 * u2 + D * u3 * u3;
26,534,563 ✔
1013
  double c1 = 2 * (u1 * x1 + u2 * x2 + D * u3 * x3);
26,534,563 ✔
1014
  double c0 = x1 * x1 + x2 * x2 + D * x3 * x3 + A * A - C * C;
26,534,563 ✔
1015
  double four_A2 = 4 * A * A;
26,534,563 ✔
1016
  double c2p = four_A2 * (u1 * u1 + u2 * u2);
26,534,563 ✔
1017
  double c1p = 2 * four_A2 * (u1 * x1 + u2 * x2);
26,534,563 ✔
1018
  double c0p = four_A2 * (x1 * x1 + x2 * x2);
26,534,563 ✔
1019

1020
  // Coefficient for equation: a t^4 + b t^3 + c t^2 + d t + e = 0. If the point
1021
  // is coincident, the 'e' coefficient should be zero. Explicitly setting it to
1022
  // zero helps avoid numerical issues below with root finding.
1023
  double coeff[5];
26,534,563 ✔
1024
  coeff[0] = coincident ? 0.0 : c0 * c0 - c0p;
26,534,563 ✔
1025
  coeff[1] = 2 * c0 * c1 - c1p;
26,534,563 ✔
1026
  coeff[2] = c1 * c1 + 2 * c0 * c2 - c2p;
26,534,563 ✔
1027
  coeff[3] = 2 * c1 * c2;
26,534,563 ✔
1028
  coeff[4] = c2 * c2;
26,534,563 ✔
1029

1030
  std::complex<double> roots[4];
26,534,563 ✔
1031
  oqs::quartic_solver(coeff, roots);
26,534,563 ✔
1032

1033
  // Find smallest positive, real root. In the case where the particle is
1034
  // coincident with the surface, we are sure to have one root very close to
1035
  // zero but possibly small and positive. A tolerance is set to discard that
1036
  // zero.
1037
  double distance = INFTY;
26,534,563 ✔
1038
  double cutoff = coincident ? TORUS_TOL : 0.0;
26,534,563 ✔
1039
  for (int i = 0; i < 4; ++i) {
132,672,815 ✔
1040
    if (roots[i].imag() == 0) {
106,138,252 ✔
1041
      double root = roots[i].real();
25,944,028 ✔
1042
      if (root > cutoff && root < distance) {
25,944,028 ✔
1043
        // Avoid roots corresponding to internal surfaces
1044
        double s1 = x1 + u1 * root;
10,411,995 ✔
1045
        double s2 = x2 + u2 * root;
10,411,995 ✔
1046
        double s3 = x3 + u3 * root;
10,411,995 ✔
1047
        double check = D * s3 * s3 + s1 * s1 + s2 * s2 + A * A - C * C;
10,411,995 ✔
1048
        if (check >= 0) {
10,411,995 !
1049
          distance = root;
10,411,995 ✔
1050
        }
1051
      }
1052
    }
1053
  }
1054
  return distance;
26,534,563 ✔
1055
}
1056

1057
//==============================================================================
1058
// SurfaceXTorus implementation
1059
//==============================================================================
1060

1061
SurfaceXTorus::SurfaceXTorus(pugi::xml_node surf_node) : Surface(surf_node)
70 ✔
1062
{
1063
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &A_, &B_, &C_});
70 ✔
1064
}
70 ✔
1065

1066
void SurfaceXTorus::to_hdf5_inner(hid_t group_id) const
55 ✔
1067
{
1068
  write_string(group_id, "type", "x-torus", false);
55 ✔
1069
  std::array<double, 6> coeffs {{x0_, y0_, z0_, A_, B_, C_}};
55 ✔
1070
  write_dataset(group_id, "coefficients", coeffs);
55 ✔
1071
}
55 ✔
1072

1073
double SurfaceXTorus::evaluate(Position r) const
811,266 ✔
1074
{
1075
  double x = r.x - x0_;
811,266 ✔
1076
  double y = r.y - y0_;
811,266 ✔
1077
  double z = r.z - z0_;
811,266 ✔
1078
  return (x * x) / (B_ * B_) +
1,622,532 ✔
1079
         std::pow(std::sqrt(y * y + z * z) - A_, 2) / (C_ * C_) - 1.;
811,266 ✔
1080
}
1081

1082
BoundingBox SurfaceXTorus::bounding_box(bool pos_side) const
22 ✔
1083
{
1084
  // The torus interior is compact: it extends +/-B_ along the axis of
1085
  // revolution and +/-(A_ + C_) in the two perpendicular directions. Mirrors
1086
  // XTorus.bounding_box on the Python side.
1087
  if (pos_side)
22 ✔
1088
    return {};
11 ✔
1089
  return {{x0_ - B_, y0_ - A_ - C_, z0_ - A_ - C_},
11 ✔
1090
    {x0_ + B_, y0_ + A_ + C_, z0_ + A_ + C_}};
11 ✔
1091
}
1092

1093
double SurfaceXTorus::distance(Position r, Direction u, bool coincident) const
8,348,615 ✔
1094
{
1095
  double x = r.x - x0_;
8,348,615 ✔
1096
  double y = r.y - y0_;
8,348,615 ✔
1097
  double z = r.z - z0_;
8,348,615 ✔
1098
  return torus_distance(y, z, x, u.y, u.z, u.x, A_, B_, C_, coincident);
8,348,615 ✔
1099
}
1100

UNCOV
1101
Direction SurfaceXTorus::normal(Position r) const
×
1102
{
1103
  // reduce the expansion of the full form for torus
1104
  double x = r.x - x0_;
×
1105
  double y = r.y - y0_;
×
UNCOV
1106
  double z = r.z - z0_;
×
1107

1108
  // f(x,y,z) = x^2/B^2 + (sqrt(y^2 + z^2) - A)^2/C^2 - 1
1109
  // ∂f/∂x = 2x/B^2
1110
  // ∂f/∂y = 2y(g - A)/(g*C^2) where g = sqrt(y^2 + z^2)
1111
  // ∂f/∂z = 2z(g - A)/(g*C^2)
1112
  // Multiplying by g*C^2*B^2 / 2 gives:
1113
  double g = std::sqrt(y * y + z * z);
×
1114
  double nx = C_ * C_ * g * x;
×
1115
  double ny = y * (g - A_) * B_ * B_;
×
1116
  double nz = z * (g - A_) * B_ * B_;
×
1117
  Direction n(nx, ny, nz);
×
UNCOV
1118
  return n / n.norm();
×
1119
}
1120

1121
//==============================================================================
1122
// SurfaceYTorus implementation
1123
//==============================================================================
1124

1125
SurfaceYTorus::SurfaceYTorus(pugi::xml_node surf_node) : Surface(surf_node)
70 ✔
1126
{
1127
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &A_, &B_, &C_});
70 ✔
1128
}
70 ✔
1129

1130
void SurfaceYTorus::to_hdf5_inner(hid_t group_id) const
55 ✔
1131
{
1132
  write_string(group_id, "type", "y-torus", false);
55 ✔
1133
  std::array<double, 6> coeffs {{x0_, y0_, z0_, A_, B_, C_}};
55 ✔
1134
  write_dataset(group_id, "coefficients", coeffs);
55 ✔
1135
}
55 ✔
1136

1137
double SurfaceYTorus::evaluate(Position r) const
780,718 ✔
1138
{
1139
  double x = r.x - x0_;
780,718 ✔
1140
  double y = r.y - y0_;
780,718 ✔
1141
  double z = r.z - z0_;
780,718 ✔
1142
  return (y * y) / (B_ * B_) +
1,561,436 ✔
1143
         std::pow(std::sqrt(x * x + z * z) - A_, 2) / (C_ * C_) - 1.;
780,718 ✔
1144
}
1145

1146
BoundingBox SurfaceYTorus::bounding_box(bool pos_side) const
22 ✔
1147
{
1148
  if (pos_side)
22 ✔
1149
    return {};
11 ✔
1150
  return {{x0_ - A_ - C_, y0_ - B_, z0_ - A_ - C_},
11 ✔
1151
    {x0_ + A_ + C_, y0_ + B_, z0_ + A_ + C_}};
11 ✔
1152
}
1153

1154
double SurfaceYTorus::distance(Position r, Direction u, bool coincident) const
8,413,900 ✔
1155
{
1156
  double x = r.x - x0_;
8,413,900 ✔
1157
  double y = r.y - y0_;
8,413,900 ✔
1158
  double z = r.z - z0_;
8,413,900 ✔
1159
  return torus_distance(x, z, y, u.x, u.z, u.y, A_, B_, C_, coincident);
8,413,900 ✔
1160
}
1161

UNCOV
1162
Direction SurfaceYTorus::normal(Position r) const
×
1163
{
1164
  // reduce the expansion of the full form for torus
1165
  double x = r.x - x0_;
×
1166
  double y = r.y - y0_;
×
UNCOV
1167
  double z = r.z - z0_;
×
1168

1169
  // f(x,y,z) = y^2/B^2 + (sqrt(x^2 + z^2) - A)^2/C^2 - 1
1170
  // ∂f/∂x = 2x(g - A)/(g*C^2) where g = sqrt(x^2 + z^2)
1171
  // ∂f/∂y = 2y/B^2
1172
  // ∂f/∂z = 2z(g - A)/(g*C^2)
1173
  // Multiplying by g*C^2*B^2 / 2 gives:
1174
  double g = std::sqrt(x * x + z * z);
×
1175
  double nx = x * (g - A_) * B_ * B_;
×
1176
  double ny = C_ * C_ * g * y;
×
1177
  double nz = z * (g - A_) * B_ * B_;
×
1178
  Direction n(nx, ny, nz);
×
UNCOV
1179
  return n / n.norm();
×
1180
}
1181

1182
//==============================================================================
1183
// SurfaceZTorus implementation
1184
//==============================================================================
1185

1186
SurfaceZTorus::SurfaceZTorus(pugi::xml_node surf_node) : Surface(surf_node)
115 ✔
1187
{
1188
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &A_, &B_, &C_});
115 ✔
1189
}
115 ✔
1190

1191
void SurfaceZTorus::to_hdf5_inner(hid_t group_id) const
88 ✔
1192
{
1193
  write_string(group_id, "type", "z-torus", false);
88 ✔
1194
  std::array<double, 6> coeffs {{x0_, y0_, z0_, A_, B_, C_}};
88 ✔
1195
  write_dataset(group_id, "coefficients", coeffs);
88 ✔
1196
}
88 ✔
1197

1198
double SurfaceZTorus::evaluate(Position r) const
1,307,178 ✔
1199
{
1200
  double x = r.x - x0_;
1,307,178 ✔
1201
  double y = r.y - y0_;
1,307,178 ✔
1202
  double z = r.z - z0_;
1,307,178 ✔
1203
  return (z * z) / (B_ * B_) +
2,614,356 ✔
1204
         std::pow(std::sqrt(x * x + y * y) - A_, 2) / (C_ * C_) - 1.;
1,307,178 ✔
1205
}
1206

1207
BoundingBox SurfaceZTorus::bounding_box(bool pos_side) const
22 ✔
1208
{
1209
  if (pos_side)
22 ✔
1210
    return {};
11 ✔
1211
  return {{x0_ - A_ - C_, y0_ - A_ - C_, z0_ - B_},
11 ✔
1212
    {x0_ + A_ + C_, y0_ + A_ + C_, z0_ + B_}};
11 ✔
1213
}
1214

1215
double SurfaceZTorus::distance(Position r, Direction u, bool coincident) const
9,772,048 ✔
1216
{
1217
  double x = r.x - x0_;
9,772,048 ✔
1218
  double y = r.y - y0_;
9,772,048 ✔
1219
  double z = r.z - z0_;
9,772,048 ✔
1220
  return torus_distance(x, y, z, u.x, u.y, u.z, A_, B_, C_, coincident);
9,772,048 ✔
1221
}
1222

UNCOV
1223
Direction SurfaceZTorus::normal(Position r) const
×
1224
{
1225
  // reduce the expansion of the full form for torus
1226
  double x = r.x - x0_;
×
1227
  double y = r.y - y0_;
×
UNCOV
1228
  double z = r.z - z0_;
×
1229

1230
  // f(x,y,z) = z^2/B^2 + (sqrt(x^2 + y^2) - A)^2/C^2 - 1
1231
  // ∂f/∂x = 2x(g - A)/(g*C^2) where g = sqrt(x^2 + y^2)
1232
  // ∂f/∂y = 2y(g - A)/(g*C^2)
1233
  // ∂f/∂z = 2z/B^2
1234
  // Multiplying by g*C^2*B^2 / 2 gives:
1235
  double g = std::sqrt(x * x + y * y);
×
1236
  double nx = x * (g - A_) * B_ * B_;
×
1237
  double ny = y * (g - A_) * B_ * B_;
×
1238
  double nz = C_ * C_ * g * z;
×
1239
  Position n(nx, ny, nz);
×
UNCOV
1240
  return n / n.norm();
×
1241
}
1242

1243
//==============================================================================
1244

1245
void read_surfaces(pugi::xml_node node,
9,592 ✔
1246
  std::set<std::pair<int, int>>& periodic_pairs,
1247
  std::unordered_map<int, double>& albedo_map,
1248
  std::unordered_map<int, int>& periodic_sense_map)
1249
{
1250
  // Count the number of surfaces
1251
  auto surf_nodes = node.children("surface");
9,592 ✔
1252
  int n_surfaces = std::distance(surf_nodes.begin(), surf_nodes.end());
9,592 ✔
1253

1254
  // Loop over XML surface elements and populate the array.  Keep track of
1255
  // periodic surfaces and their albedos.
1256
  model::surfaces.reserve(n_surfaces);
9,592 ✔
1257
  {
9,592 ✔
1258
    pugi::xml_node surf_node;
9,592 ✔
1259
    int i_surf;
9,592 ✔
1260
    for (surf_node = node.child("surface"), i_surf = 0; surf_node;
59,699 ✔
1261
         surf_node = surf_node.next_sibling("surface"), i_surf++) {
50,107 ✔
1262
      std::string surf_type = get_node_value(surf_node, "type", true, true);
50,107 ✔
1263

1264
      // Allocate and initialize the new surface
1265

1266
      if (surf_type == "x-plane") {
50,107 ✔
1267
        model::surfaces.push_back(make_unique<SurfaceXPlane>(surf_node));
12,615 ✔
1268

1269
      } else if (surf_type == "y-plane") {
37,492 ✔
1270
        model::surfaces.push_back(make_unique<SurfaceYPlane>(surf_node));
11,434 ✔
1271

1272
      } else if (surf_type == "z-plane") {
26,058 ✔
1273
        model::surfaces.push_back(make_unique<SurfaceZPlane>(surf_node));
8,460 ✔
1274

1275
      } else if (surf_type == "plane") {
17,598 ✔
1276
        model::surfaces.push_back(make_unique<SurfacePlane>(surf_node));
2,095 ✔
1277

1278
      } else if (surf_type == "x-cylinder") {
15,503 ✔
1279
        model::surfaces.push_back(make_unique<SurfaceXCylinder>(surf_node));
45 ✔
1280

1281
      } else if (surf_type == "y-cylinder") {
15,458 ✔
1282
        model::surfaces.push_back(make_unique<SurfaceYCylinder>(surf_node));
26 ✔
1283

1284
      } else if (surf_type == "z-cylinder") {
15,432 ✔
1285
        model::surfaces.push_back(make_unique<SurfaceZCylinder>(surf_node));
5,807 ✔
1286

1287
      } else if (surf_type == "sphere") {
9,625 ✔
1288
        model::surfaces.push_back(make_unique<SurfaceSphere>(surf_node));
9,373 ✔
1289

1290
      } else if (surf_type == "x-cone") {
252 !
UNCOV
1291
        model::surfaces.push_back(make_unique<SurfaceXCone>(surf_node));
×
1292

1293
      } else if (surf_type == "y-cone") {
252 !
UNCOV
1294
        model::surfaces.push_back(make_unique<SurfaceYCone>(surf_node));
×
1295

1296
      } else if (surf_type == "z-cone") {
252 ✔
1297
        model::surfaces.push_back(make_unique<SurfaceZCone>(surf_node));
15 ✔
1298

1299
      } else if (surf_type == "quadric") {
237 ✔
1300
        model::surfaces.push_back(make_unique<SurfaceQuadric>(surf_node));
15 ✔
1301

1302
      } else if (surf_type == "x-torus") {
222 ✔
1303
        model::surfaces.push_back(std::make_unique<SurfaceXTorus>(surf_node));
59 ✔
1304

1305
      } else if (surf_type == "y-torus") {
163 ✔
1306
        model::surfaces.push_back(std::make_unique<SurfaceYTorus>(surf_node));
59 ✔
1307

1308
      } else if (surf_type == "z-torus") {
104 !
1309
        model::surfaces.push_back(std::make_unique<SurfaceZTorus>(surf_node));
104 ✔
1310

1311
      } else {
UNCOV
1312
        fatal_error(fmt::format("Invalid surface type, \"{}\"", surf_type));
×
1313
      }
1314

1315
      // Check for a periodic surface
1316
      if (check_for_node(surf_node, "boundary")) {
50,107 ✔
1317
        std::string surf_bc = get_node_value(surf_node, "boundary", true, true);
30,046 ✔
1318
        if (surf_bc == "periodic") {
30,046 ✔
1319
          periodic_sense_map[model::surfaces.back()->id_] = 0;
546 ✔
1320
          // Check for surface albedo. Skip sanity check as it is already done
1321
          // in the Surface class's constructor.
1322
          if (check_for_node(surf_node, "albedo")) {
546 !
UNCOV
1323
            albedo_map[model::surfaces.back()->id_] =
×
1324
              std::stod(get_node_value(surf_node, "albedo"));
×
1325
          }
1326
          if (check_for_node(surf_node, "periodic_surface_id")) {
546 ✔
1327
            int i_periodic =
356 ✔
1328
              std::stoi(get_node_value(surf_node, "periodic_surface_id"));
712 ✔
1329
            int lo_id = std::min(model::surfaces.back()->id_, i_periodic);
356 ✔
1330
            int hi_id = std::max(model::surfaces.back()->id_, i_periodic);
356 ✔
1331
            periodic_pairs.insert({lo_id, hi_id});
356 ✔
1332
          } else {
1333
            periodic_pairs.insert({model::surfaces.back()->id_, -1});
190 ✔
1334
          }
1335
        }
1336
      }
30,046 ✔
1337
    }
50,107 ✔
1338
  }
1339

1340
  // Fill the surface map
1341
  for (int i_surf = 0; i_surf < model::surfaces.size(); i_surf++) {
59,699 ✔
1342
    int id = model::surfaces[i_surf]->id_;
50,107 !
1343
    auto in_map = model::surface_map.find(id);
50,107 !
1344
    if (in_map == model::surface_map.end()) {
50,107 !
1345
      model::surface_map[id] = i_surf;
50,107 ✔
1346
    } else {
UNCOV
1347
      fatal_error(
×
1348
        fmt::format("Two or more surfaces use the same unique ID: {}", id));
×
1349
    }
1350
  }
1351
}
9,592 ✔
1352

1353
void prepare_boundary_conditions(std::set<std::pair<int, int>>& periodic_pairs,
9,590 ✔
1354
  std::unordered_map<int, double>& albedo_map,
1355
  std::unordered_map<int, int>& periodic_sense_map)
1356
{
1357
  // Fill the senses map for periodic surfaces
1358
  auto n_periodic = periodic_sense_map.size();
9,590 ✔
1359
  for (const auto& cell : model::cells) {
9,847 ✔
1360
    if (n_periodic == 0)
9,716 ✔
1361
      break; // Early exit once all periodic surfaces found
1362

1363
    for (auto s : cell->surfaces()) {
1,335 ✔
1364
      auto surf_idx = std::abs(s) - 1;
1,078 ✔
1365
      auto id = model::surfaces[surf_idx]->id_;
1,078 ✔
1366

1367
      if (periodic_sense_map.count(id)) {
1,078 ✔
1368
        periodic_sense_map[id] = std::copysign(1, s);
546 ✔
1369
        --n_periodic;
546 ✔
1370
      }
1371
    }
257 ✔
1372
  }
1373

1374
  // Resolve unpaired periodic surfaces.  A lambda function is used with
1375
  // std::find_if to identify the unpaired surfaces.
1376
  auto is_unresolved_pair = [](const std::pair<int, int> p) {
9,958 ✔
1377
    return p.second == -1;
368 ✔
1378
  };
1379
  auto first_unresolved = std::find_if(
9,590 ✔
1380
    periodic_pairs.begin(), periodic_pairs.end(), is_unresolved_pair);
1381
  if (first_unresolved != periodic_pairs.end()) {
9,590 ✔
1382
    // Found one unpaired surface; search for a second one
1383
    auto next_elem = first_unresolved;
95 ✔
1384
    next_elem++;
95 !
1385
    auto second_unresolved =
95 !
1386
      std::find_if(next_elem, periodic_pairs.end(), is_unresolved_pair);
95 !
1387
    if (second_unresolved == periodic_pairs.end()) {
95 !
UNCOV
1388
      fatal_error("Found only one periodic surface without a specified partner."
×
1389
                  " Please specify the partner for each periodic surface.");
1390
    }
1391

1392
    // Make sure there isn't a third unpaired surface
1393
    next_elem = second_unresolved;
95 ✔
1394
    next_elem++;
95 !
1395
    auto third_unresolved =
95 !
1396
      std::find_if(next_elem, periodic_pairs.end(), is_unresolved_pair);
95 !
1397
    if (third_unresolved != periodic_pairs.end()) {
95 !
UNCOV
1398
      fatal_error(
×
1399
        "Found at least three periodic surfaces without a specified "
1400
        "partner. Please specify the partner for each periodic surface.");
1401
    }
1402

1403
    // Add the completed pair and remove the old, unpaired entries
1404
    int lo_id = std::min(first_unresolved->first, second_unresolved->first);
95 !
1405
    int hi_id = std::max(first_unresolved->first, second_unresolved->first);
95 !
1406
    periodic_pairs.insert({lo_id, hi_id});
95 ✔
1407
    periodic_pairs.erase(first_unresolved);
95 ✔
1408
    periodic_pairs.erase(second_unresolved);
95 ✔
1409
  }
1410

1411
  // Assign the periodic boundary conditions with albedos
1412
  for (auto periodic_pair : periodic_pairs) {
9,863 ✔
1413
    int i_surf = model::surface_map[periodic_pair.first];
273 ✔
1414
    int j_surf = model::surface_map[periodic_pair.second];
273 ✔
1415
    Surface& surf1 {*model::surfaces[i_surf]};
273 ✔
1416
    Surface& surf2 {*model::surfaces[j_surf]};
273 ✔
1417

1418
    // Compute the dot product of the surface normals
1419
    Direction norm1 = surf1.normal({0, 0, 0});
273 ✔
1420
    Direction norm2 = surf2.normal({0, 0, 0});
273 ✔
1421
    norm1 /= norm1.norm();
273 ✔
1422
    norm2 /= norm2.norm();
273 ✔
1423
    double dot_prod = norm1.dot(norm2);
273 ✔
1424

1425
    // If the dot product is 1 (to within floating point precision) then the
1426
    // planes are parallel which indicates a translational periodic boundary
1427
    // condition.  Otherwise, it is a rotational periodic BC.
1428
    if (std::abs(1.0 - dot_prod) < FP_PRECISION) {
273 ✔
1429
      surf1.bc_ = make_unique<TranslationalPeriodicBC>(i_surf, j_surf);
113 ✔
1430
      surf2.bc_ = make_unique<TranslationalPeriodicBC>(j_surf, i_surf);
113 ✔
1431
    } else {
1432
      // check that both normals have at least one 0 component
1433
      if (std::abs(norm1.x) > FP_PRECISION &&
160 ✔
1434
          std::abs(norm1.y) > FP_PRECISION &&
160 !
1435
          std::abs(norm1.z) > FP_PRECISION) {
115 !
UNCOV
1436
        fatal_error(fmt::format(
×
1437
          "The normal ({}) of the periodic surface ({}) does not contain any "
1438
          "component with a zero value. A RotationalPeriodicBC requires one "
1439
          "component which is zero for both plane normals.",
1440
          norm1, i_surf));
1441
      }
1442
      if (std::abs(norm2.x) > FP_PRECISION &&
160 ✔
1443
          std::abs(norm2.y) > FP_PRECISION &&
160 !
1444
          std::abs(norm2.z) > FP_PRECISION) {
115 !
UNCOV
1445
        fatal_error(fmt::format(
×
1446
          "The normal ({}) of the periodic surface ({}) does not contain any "
1447
          "component with a zero value. A RotationalPeriodicBC requires one "
1448
          "component which is zero for both plane normals.",
1449
          norm2, j_surf));
1450
      }
1451
      // find common zero component, which indicates the periodic axis
1452
      RotationalPeriodicBC::PeriodicAxis axis;
160 ✔
1453
      if (std::abs(norm1.x) <= FP_PRECISION &&
160 ✔
1454
          std::abs(norm2.x) <= FP_PRECISION) {
15 !
1455
        axis = RotationalPeriodicBC::PeriodicAxis::x;
15 ✔
1456
      } else if (std::abs(norm1.y) <= FP_PRECISION &&
145 ✔
1457
                 std::abs(norm2.y) <= FP_PRECISION) {
30 ✔
1458
        axis = RotationalPeriodicBC::PeriodicAxis::y;
15 ✔
1459
      } else if (std::abs(norm1.z) <= FP_PRECISION &&
130 !
1460
                 std::abs(norm2.z) <= FP_PRECISION) {
130 !
1461
        axis = RotationalPeriodicBC::PeriodicAxis::z;
130 ✔
1462
      } else {
UNCOV
1463
        fatal_error(fmt::format(
×
1464
          "There is no component which is 0.0 in both normal vectors. This "
1465
          "indicates that the two planes are not periodic about the X, Y, or Z "
1466
          "axis, which is not supported."));
1467
      }
1468
      auto i_sign = periodic_sense_map[periodic_pair.first];
160 ✔
1469
      auto j_sign = periodic_sense_map[periodic_pair.second];
160 ✔
1470
      surf1.bc_ = make_unique<RotationalPeriodicBC>(
160 ✔
1471
        i_sign * (i_surf + 1), j_sign * (j_surf + 1), axis);
160 ✔
1472
      surf2.bc_ = make_unique<RotationalPeriodicBC>(
160 ✔
1473
        j_sign * (j_surf + 1), i_sign * (i_surf + 1), axis);
320 ✔
1474
    }
1475

1476
    // If albedo data is present in albedo map, set the boundary albedo.
1477
    if (albedo_map.count(surf1.id_)) {
273 !
UNCOV
1478
      surf1.bc_->set_albedo(albedo_map[surf1.id_]);
×
1479
    }
1480
    if (albedo_map.count(surf2.id_)) {
273 !
1481
      surf2.bc_->set_albedo(albedo_map[surf2.id_]);
273 ✔
1482
    }
1483
  }
1484
}
9,590 ✔
1485

1486
void free_memory_surfaces()
9,721 ✔
1487
{
1488
  model::surfaces.clear();
9,721 ✔
1489
  model::surface_map.clear();
9,721 ✔
1490
}
9,721 ✔
1491

1492
} // namespace openmc
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc