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

openmc-dev / openmc / 42411379612

07 Nov 2026 08:30PM UTC coverage: 81.831% (-0.2%) from 82.009%
42411379612

Pull #4215

github

web-flow
Merge 36e674ca2 into 93b147fa4
Pull Request #4215: XDG Mesh Geometry

19729 of 27802 branches covered (70.96%)

Branch coverage included in aggregate %.

117 of 222 new or added lines in 18 files covered. (52.7%)

514 existing lines in 31 files now uncovered.

62406 of 72570 relevant lines covered (85.99%)

39997221.18 hits per line

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

81.64
/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(
31,683 ✔
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");
31,683 ✔
44
  if (coeffs_file.size() != coeffs.size()) {
31,683 !
45
    fatal_error(
×
46
      fmt::format("Surface {} expects {} coefficient but was given {}", surf_id,
×
47
        coeffs.size(), coeffs_file.size()));
×
48
  }
49

50
  // Copy the coefficients
51
  int i = 0;
31,683 ✔
52
  for (auto c : coeffs) {
93,881 ✔
53
    *c = coeffs_file[i++];
62,198 ✔
54
  }
55
}
31,683 ✔
56

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

UNCOV
61
Surface::Surface() {} // empty constructor
×
62

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

75
  if (check_for_node(surf_node, "name")) {
31,683 ✔
76
    name_ = get_node_value(surf_node, "name", false);
7,481 ✔
77
  }
78

79
  if (check_for_node(surf_node, "boundary")) {
31,683 ✔
80
    std::string surf_bc = get_node_value(surf_node, "boundary", true, true);
18,680 ✔
81

82
    if (surf_bc == "transmission" || surf_bc == "transmit" || surf_bc.empty()) {
37,360 !
83
      // Leave the bc_ a nullptr
84
    } else if (surf_bc == "vacuum") {
18,680 ✔
85
      bc_ = make_unique<VacuumBC>();
9,010 ✔
86
    } else if (surf_bc == "reflective" || surf_bc == "reflect" ||
10,058 !
87
               surf_bc == "reflecting") {
388 !
88
      bc_ = make_unique<ReflectiveBC>();
9,282 ✔
89
    } else if (surf_bc == "white") {
388 ✔
90
      bc_ = make_unique<WhiteBC>();
54 ✔
91
    } else if (surf_bc == "periodic") {
334 !
92
      // Periodic BCs are handled separately
93
    } else {
94
      fatal_error(fmt::format("Unknown boundary condition \"{}\" specified "
×
95
                              "on surface {}",
96
        surf_bc, id_));
×
97
    }
98

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

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

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

113
      bc_->set_albedo(surf_alb);
54 ✔
114
    }
115
  }
18,680 ✔
116
}
31,683 ✔
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;
6,372,311 ✔
130
  }
131
  return f > 0.0;
2,147,483,647 ✔
132
}
133

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

140
  // Reflect direction according to normal.
141
  return u.reflect(n);
627,960,562 ✔
142
}
143

144
Direction Surface::diffuse_reflect(
640,913 ✔
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);
640,913 ✔
151
  n /= n.norm();
640,913 ✔
152
  const double projection = n.dot(u);
640,913 ✔
153

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

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

161
  // normalize the direction
162
  return u / u.norm();
640,913 ✔
163
}
164

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

169
  if (geom_type() == GeometryType::DAG) {
26,437 !
UNCOV
170
    write_string(surf_group, "geom_type", "dagmc", false);
×
171
  } else if (geom_type() == GeometryType::XDG_SURFACE_MESH) {
26,437 !
NEW
172
    write_string(surf_group, "geom_type", "xdg", false);
×
173
  } else if (geom_type() == GeometryType::CSG) {
26,437 !
174
    write_string(surf_group, "geom_type", "csg", false);
26,437 ✔
175

176
    if (bc_) {
26,437 ✔
177
      write_string(surf_group, "boundary_type", bc_->type(), false);
15,980 ✔
178
      bc_->to_hdf5(surf_group);
15,980 ✔
179

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

186
        if (id_ == surf1.id_) {
290 !
187
          write_dataset(surf_group, "periodic_surface_id", surf2.id_);
290 ✔
188
        } else {
189
          write_dataset(surf_group, "periodic_surface_id", surf1.id_);
×
190
        }
191
      }
192
    } else {
193
      write_string(surf_group, "boundary_type", "transmission", false);
20,914 ✔
194
    }
195
  }
196

197
  if (!name_.empty()) {
26,437 ✔
198
    write_string(surf_group, "name", name_, false);
6,391 ✔
199
  }
200

201
  to_hdf5_inner(surf_group);
26,437 ✔
202

203
  close_group(surf_group);
26,437 ✔
204
}
26,437 ✔
205

206
//==============================================================================
207
// Generic functions for x-, y-, and z-, planes.
208
//==============================================================================
209

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

224
//==============================================================================
225
// SurfaceXPlane implementation
226
//==============================================================================
227

228
SurfaceXPlane::SurfaceXPlane(pugi::xml_node surf_node) : Surface(surf_node)
8,321 ✔
229
{
230
  read_coeffs(surf_node, id_, {&x0_});
8,321 ✔
231
}
8,321 ✔
232

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

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

243
Direction SurfaceXPlane::normal(Position r) const
237,363,966 ✔
244
{
245
  return {1., 0., 0.};
237,363,966 ✔
246
}
247

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

255
BoundingBox SurfaceXPlane::bounding_box(bool pos_side) const
154 ✔
256
{
257
  if (pos_side) {
154 ✔
258
    return {{x0_, -INFTY, -INFTY}, {INFTY, INFTY, INFTY}};
77 ✔
259
  } else {
260
    return {{-INFTY, -INFTY, -INFTY}, {x0_, INFTY, INFTY}};
77 ✔
261
  }
262
}
263

264
//==============================================================================
265
// SurfaceYPlane implementation
266
//==============================================================================
267

268
SurfaceYPlane::SurfaceYPlane(pugi::xml_node surf_node) : Surface(surf_node)
7,002 ✔
269
{
270
  read_coeffs(surf_node, id_, {&y0_});
7,002 ✔
271
}
7,002 ✔
272

273
double SurfaceYPlane::evaluate(Position r) const
1,665,791,982 ✔
274
{
275
  return r.y - y0_;
1,665,791,982 ✔
276
}
277

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

283
Direction SurfaceYPlane::normal(Position r) const
332,812,974 ✔
284
{
285
  return {0., 1., 0.};
332,812,974 ✔
286
}
287

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

295
BoundingBox SurfaceYPlane::bounding_box(bool pos_side) const
161 ✔
296
{
297
  if (pos_side) {
161 ✔
298
    return {{-INFTY, y0_, -INFTY}, {INFTY, INFTY, INFTY}};
77 ✔
299
  } else {
300
    return {{-INFTY, -INFTY, -INFTY}, {INFTY, y0_, INFTY}};
84 ✔
301
  }
302
}
303

304
//==============================================================================
305
// SurfaceZPlane implementation
306
//==============================================================================
307

308
SurfaceZPlane::SurfaceZPlane(pugi::xml_node surf_node) : Surface(surf_node)
5,082 ✔
309
{
310
  read_coeffs(surf_node, id_, {&z0_});
5,082 ✔
311
}
5,082 ✔
312

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

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

323
Direction SurfaceZPlane::normal(Position r) const
162,958,487 ✔
324
{
325
  return {0., 0., 1.};
162,958,487 ✔
326
}
327

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

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

344
//==============================================================================
345
// SurfacePlane implementation
346
//==============================================================================
347

348
SurfacePlane::SurfacePlane(pugi::xml_node surf_node) : Surface(surf_node)
1,471 ✔
349
{
350
  read_coeffs(surf_node, id_, {&A_, &B_, &C_, &D_});
1,471 ✔
351
}
1,471 ✔
352

353
double SurfacePlane::evaluate(Position r) const
248,612,446 ✔
354
{
355
  return A_ * r.x + B_ * r.y + C_ * r.z - D_;
248,612,446 ✔
356
}
357

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

370
  int axis = -1;
252 ✔
371
  for (int i = 0; i < 3; ++i) {
252 ✔
372
    if (std::abs(std::abs(n[i] / norm) - 1.0) > PLANE_ALIGNMENT_TOL)
217 ✔
373
      continue;
147 ✔
374

375
    bool aligned = true;
252 ✔
376
    for (int j = 0; j < 3; ++j) {
252 ✔
377
      if (j != i && std::abs(n[j] / norm) > PLANE_ALIGNMENT_TOL) {
196 ✔
378
        aligned = false;
379
        break;
380
      }
381
    }
382
    if (aligned) {
70 ✔
383
      axis = i;
384
      break;
385
    }
386
  }
387
  if (axis == -1)
91 ✔
388
    return {};
35 ✔
389

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

402
double SurfacePlane::distance(Position r, Direction u, bool coincident) const
549,506,902 ✔
403
{
404
  const double f = A_ * r.x + B_ * r.y + C_ * r.z - D_;
549,506,902 ✔
405
  const double projection = A_ * u.x + B_ * u.y + C_ * u.z;
549,506,902 ✔
406
  if (coincident || std::abs(f) < FP_COINCIDENT || projection == 0.0) {
549,506,902 !
407
    return INFTY;
408
  } else {
409
    const double d = -f / projection;
502,729,905 ✔
410
    if (d < 0.0)
502,729,905 ✔
411
      return INFTY;
412
    return d;
274,761,515 ✔
413
  }
414
}
415

416
Direction SurfacePlane::normal(Position r) const
3,665,472 ✔
417
{
418
  return {A_, B_, C_};
3,665,472 ✔
419
}
420

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

428
//==============================================================================
429
// Generic functions for x-, y-, and z-, cylinders
430
//==============================================================================
431

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

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

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

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

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

475
  } else if (c < 0.0) {
1,637,508,582 ✔
476
    // Particle is inside the cylinder, thus one distance must be negative
477
    // and one must be positive. The positive distance will be the one with
478
    // negative sign on sqrt(quad).
479
    return (-k + sqrt(quad)) / a;
694,609,083 ✔
480

481
  } else {
482
    // Particle is outside the cylinder, thus both distances are either
483
    // positive or negative. If positive, the smaller distance is the one
484
    // with positive sign on sqrt(quad).
485
    const double d = (-k - sqrt(quad)) / a;
942,899,499 ✔
486
    if (d < 0.0)
942,899,499 ✔
487
      return INFTY;
488
    return d;
735,773,655 ✔
489
  }
490
}
491

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

506
//==============================================================================
507
// SurfaceXCylinder implementation
508
//==============================================================================
509

510
SurfaceXCylinder::SurfaceXCylinder(pugi::xml_node surf_node)
27 ✔
511
  : Surface(surf_node)
27 ✔
512
{
513
  read_coeffs(surf_node, id_, {&y0_, &z0_, &radius_});
27 ✔
514
}
27 ✔
515

516
double SurfaceXCylinder::evaluate(Position r) const
813,050 ✔
517
{
518
  return axis_aligned_cylinder_evaluate<1, 2>(r, y0_, z0_, radius_);
813,050 ✔
519
}
520

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

528
Direction SurfaceXCylinder::normal(Position r) const
253,288 ✔
529
{
530
  return axis_aligned_cylinder_normal<0, 1, 2>(r, y0_, z0_);
253,288 ✔
531
}
532

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

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

553
SurfaceYCylinder::SurfaceYCylinder(pugi::xml_node surf_node)
16 ✔
554
  : Surface(surf_node)
16 ✔
555
{
556
  read_coeffs(surf_node, id_, {&x0_, &z0_, &radius_});
16 ✔
557
}
16 ✔
558

559
double SurfaceYCylinder::evaluate(Position r) const
108,808 ✔
560
{
561
  return axis_aligned_cylinder_evaluate<0, 2>(r, x0_, z0_, radius_);
108,808 ✔
562
}
563

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

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

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

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

593
//==============================================================================
594
// SurfaceZCylinder implementation
595
//==============================================================================
596

597
SurfaceZCylinder::SurfaceZCylinder(pugi::xml_node surf_node)
3,690 ✔
598
  : Surface(surf_node)
3,690 ✔
599
{
600
  read_coeffs(surf_node, id_, {&x0_, &y0_, &radius_});
3,690 ✔
601
}
3,690 ✔
602

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

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

615
Direction SurfaceZCylinder::normal(Position r) const
178,583,881 ✔
616
{
617
  return axis_aligned_cylinder_normal<2, 0, 1>(r, x0_, y0_);
178,583,881 ✔
618
}
619

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

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

637
//==============================================================================
638
// SurfaceSphere implementation
639
//==============================================================================
640

641
SurfaceSphere::SurfaceSphere(pugi::xml_node surf_node) : Surface(surf_node)
5,890 ✔
642
{
643
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &radius_});
5,890 ✔
644
}
5,890 ✔
645

646
double SurfaceSphere::evaluate(Position r) const
802,121,962 ✔
647
{
648
  const double x = r.x - x0_;
802,121,962 ✔
649
  const double y = r.y - y0_;
802,121,962 ✔
650
  const double z = r.z - z0_;
802,121,962 ✔
651
  return x * x + y * y + z * z - radius_ * radius_;
802,121,962 ✔
652
}
653

654
double SurfaceSphere::distance(Position r, Direction u, bool coincident) const
1,366,950,959 ✔
655
{
656
  const double x = r.x - x0_;
1,366,950,959 ✔
657
  const double y = r.y - y0_;
1,366,950,959 ✔
658
  const double z = r.z - z0_;
1,366,950,959 ✔
659
  const double k = x * u.x + y * u.y + z * u.z;
1,366,950,959 ✔
660
  const double c = x * x + y * y + z * z - radius_ * radius_;
1,366,950,959 ✔
661
  const double quad = k * k - c;
1,366,950,959 ✔
662

663
  if (quad < 0.0) {
1,366,950,959 ✔
664
    // No intersection with sphere.
665
    return INFTY;
666

667
  } else if (coincident || std::abs(c) < FP_COINCIDENT) {
1,246,881,378 !
668
    // Particle is on the sphere, thus one distance is positive/negative and
669
    // the other is zero. The sign of k determines if we are facing in or out.
670
    if (k >= 0.0) {
74,940,911 ✔
671
      return INFTY;
672
    } else {
673
      return -k + sqrt(quad);
42,728,245 ✔
674
    }
675

676
  } else if (c < 0.0) {
1,171,940,467 ✔
677
    // Particle is inside the sphere, thus one distance must be negative and
678
    // one must be positive. The positive distance will be the one with
679
    // negative sign on sqrt(quad)
680
    return -k + sqrt(quad);
1,128,847,032 ✔
681

682
  } else {
683
    // Particle is outside the sphere, thus both distances are either positive
684
    // or negative. If positive, the smaller distance is the one with positive
685
    // sign on sqrt(quad).
686
    const double d = -k - sqrt(quad);
43,093,435 ✔
687
    if (d < 0.0)
43,093,435 ✔
688
      return INFTY;
689
    return d;
33,670,265 ✔
690
  }
691
}
692

693
Direction SurfaceSphere::normal(Position r) const
248,015,880 ✔
694
{
695
  return {2.0 * (r.x - x0_), 2.0 * (r.y - y0_), 2.0 * (r.z - z0_)};
248,015,880 ✔
696
}
697

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

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

715
//==============================================================================
716
// Generic functions for x-, y-, and z-, cones
717
//==============================================================================
718

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

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

749
  double d;
750

751
  if (quad < 0.0) {
622,377 !
752
    // No intersection with cone.
753
    return INFTY;
754

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

765
  } else {
766
    // Calculate both solutions to the quadratic.
767
    quad = sqrt(quad);
584,507 ✔
768
    d = (-k - quad) / a;
584,507 ✔
769
    const double b = (-k + quad) / a;
584,507 ✔
770

771
    // Determine the smallest positive solution.
772
    if (d < 0.0) {
584,507 ✔
773
      if (b > 0.0)
496,391 ✔
774
        d = b;
423,248 ✔
775
    } else {
776
      if (b > 0.0) {
88,116 !
777
        if (b < d)
88,116 !
778
          d = b;
88,116 ✔
779
      }
780
    }
781
  }
782

783
  // If the distance was negative, set boundary distance to infinity.
784
  if (d <= 0.0)
622,377 ✔
785
    return INFTY;
86,814 ✔
786
  return d;
787
}
788

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

803
//==============================================================================
804
// SurfaceXCone implementation
805
//==============================================================================
806

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

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

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

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

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

835
//==============================================================================
836
// SurfaceYCone implementation
837
//==============================================================================
838

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

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

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

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

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

867
//==============================================================================
868
// SurfaceZCone implementation
869
//==============================================================================
870

871
SurfaceZCone::SurfaceZCone(pugi::xml_node surf_node) : Surface(surf_node)
9 ✔
872
{
873
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &radius_sq_});
9 ✔
874
}
9 ✔
875

876
double SurfaceZCone::evaluate(Position r) const
207,361 ✔
877
{
878
  return axis_aligned_cone_evaluate<2, 0, 1>(r, z0_, x0_, y0_, radius_sq_);
207,361 ✔
879
}
880

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

887
Direction SurfaceZCone::normal(Position r) const
37,870 ✔
888
{
889
  return axis_aligned_cone_normal<2, 0, 1>(r, z0_, x0_, y0_, radius_sq_);
37,870 ✔
890
}
891

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

899
//==============================================================================
900
// SurfaceQuadric implementation
901
//==============================================================================
902

903
SurfaceQuadric::SurfaceQuadric(pugi::xml_node surf_node) : Surface(surf_node)
16 ✔
904
{
905
  read_coeffs(
32 ✔
906
    surf_node, id_, {&A_, &B_, &C_, &D_, &E_, &F_, &G_, &H_, &J_, &K_});
16 ✔
907
}
16 ✔
908

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

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

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

937
  double d;
201,530 ✔
938

939
  if (quad < 0.0) {
201,530 !
940
    // No intersection with surface.
941
    return INFTY;
942

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

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

971
    // Determine the smallest positive solution.
972
    if (d < 0.0) {
178,584 ✔
973
      if (b > 0.0)
178,577 !
974
        d = b;
178,577 ✔
975
    } else {
976
      if (b > 0.0) {
7 !
977
        if (b < d)
7 !
978
          d = b;
×
979
      }
980
    }
981
  }
982

983
  // If the distance was negative, set boundary distance to infinity.
984
  if (d <= 0.0)
201,530 !
985
    return INFTY;
×
986
  return d;
987
}
988

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

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

1005
//==============================================================================
1006
// Torus helper functions
1007
//==============================================================================
1008

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

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

1032
  std::complex<double> roots[4];
16,885,631 ✔
1033
  oqs::quartic_solver(coeff, roots);
16,885,631 ✔
1034

1035
  // Find smallest positive, real root. In the case where the particle is
1036
  // coincident with the surface, we are sure to have one root very close to
1037
  // zero but possibly small and positive. A tolerance is set to discard that
1038
  // zero.
1039
  double distance = INFTY;
16,885,631 ✔
1040
  double cutoff = coincident ? TORUS_TOL : 0.0;
16,885,631 ✔
1041
  for (int i = 0; i < 4; ++i) {
84,428,155 ✔
1042
    if (roots[i].imag() == 0) {
67,542,524 ✔
1043
      double root = roots[i].real();
16,509,836 ✔
1044
      if (root > cutoff && root < distance) {
16,509,836 ✔
1045
        // Avoid roots corresponding to internal surfaces
1046
        double s1 = x1 + u1 * root;
6,625,815 ✔
1047
        double s2 = x2 + u2 * root;
6,625,815 ✔
1048
        double s3 = x3 + u3 * root;
6,625,815 ✔
1049
        double check = D * s3 * s3 + s1 * s1 + s2 * s2 + A * A - C * C;
6,625,815 ✔
1050
        if (check >= 0) {
6,625,815 !
1051
          distance = root;
6,625,815 ✔
1052
        }
1053
      }
1054
    }
1055
  }
1056
  return distance;
16,885,631 ✔
1057
}
1058

1059
//==============================================================================
1060
// SurfaceXTorus implementation
1061
//==============================================================================
1062

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

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

1075
double SurfaceXTorus::evaluate(Position r) const
516,256 ✔
1076
{
1077
  double x = r.x - x0_;
516,256 ✔
1078
  double y = r.y - y0_;
516,256 ✔
1079
  double z = r.z - z0_;
516,256 ✔
1080
  return (x * x) / (B_ * B_) +
1,032,512 ✔
1081
         std::pow(std::sqrt(y * y + z * z) - A_, 2) / (C_ * C_) - 1.;
516,256 ✔
1082
}
1083

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

1095
double SurfaceXTorus::distance(Position r, Direction u, bool coincident) const
5,312,755 ✔
1096
{
1097
  double x = r.x - x0_;
5,312,755 ✔
1098
  double y = r.y - y0_;
5,312,755 ✔
1099
  double z = r.z - z0_;
5,312,755 ✔
1100
  return torus_distance(y, z, x, u.y, u.z, u.x, A_, B_, C_, coincident);
5,312,755 ✔
1101
}
1102

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

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

1123
//==============================================================================
1124
// SurfaceYTorus implementation
1125
//==============================================================================
1126

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

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

1139
double SurfaceYTorus::evaluate(Position r) const
491,898 ✔
1140
{
1141
  double x = r.x - x0_;
491,898 ✔
1142
  double y = r.y - y0_;
491,898 ✔
1143
  double z = r.z - z0_;
491,898 ✔
1144
  return (y * y) / (B_ * B_) +
983,796 ✔
1145
         std::pow(std::sqrt(x * x + z * z) - A_, 2) / (C_ * C_) - 1.;
491,898 ✔
1146
}
1147

1148
BoundingBox SurfaceYTorus::bounding_box(bool pos_side) const
14 ✔
1149
{
1150
  if (pos_side)
14 ✔
1151
    return {};
7 ✔
1152
  return {{x0_ - A_ - C_, y0_ - B_, z0_ - A_ - C_},
7 ✔
1153
    {x0_ + A_ + C_, y0_ + B_, z0_ + A_ + C_}};
7 ✔
1154
}
1155

1156
double SurfaceYTorus::distance(Position r, Direction u, bool coincident) const
5,354,300 ✔
1157
{
1158
  double x = r.x - x0_;
5,354,300 ✔
1159
  double y = r.y - y0_;
5,354,300 ✔
1160
  double z = r.z - z0_;
5,354,300 ✔
1161
  return torus_distance(x, z, y, u.x, u.z, u.y, A_, B_, C_, coincident);
5,354,300 ✔
1162
}
1163

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

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

1184
//==============================================================================
1185
// SurfaceZTorus implementation
1186
//==============================================================================
1187

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

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

1200
double SurfaceZTorus::evaluate(Position r) const
834,639 ✔
1201
{
1202
  double x = r.x - x0_;
834,639 ✔
1203
  double y = r.y - y0_;
834,639 ✔
1204
  double z = r.z - z0_;
834,639 ✔
1205
  return (z * z) / (B_ * B_) +
1,669,278 ✔
1206
         std::pow(std::sqrt(x * x + y * y) - A_, 2) / (C_ * C_) - 1.;
834,639 ✔
1207
}
1208

1209
BoundingBox SurfaceZTorus::bounding_box(bool pos_side) const
14 ✔
1210
{
1211
  if (pos_side)
14 ✔
1212
    return {};
7 ✔
1213
  return {{x0_ - A_ - C_, y0_ - A_ - C_, z0_ - B_},
7 ✔
1214
    {x0_ + A_ + C_, y0_ + A_ + C_, z0_ + B_}};
7 ✔
1215
}
1216

1217
double SurfaceZTorus::distance(Position r, Direction u, bool coincident) const
6,218,576 ✔
1218
{
1219
  double x = r.x - x0_;
6,218,576 ✔
1220
  double y = r.y - y0_;
6,218,576 ✔
1221
  double z = r.z - z0_;
6,218,576 ✔
1222
  return torus_distance(x, y, z, u.x, u.y, u.z, A_, B_, C_, coincident);
6,218,576 ✔
1223
}
1224

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

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

1245
//==============================================================================
1246

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

1256
  // Loop over XML surface elements and populate the array.  Keep track of
1257
  // periodic surfaces and their albedos.
1258
  model::surfaces.reserve(n_surfaces);
6,032 ✔
1259
  {
6,032 ✔
1260
    pugi::xml_node surf_node;
6,032 ✔
1261
    int i_surf;
6,032 ✔
1262
    for (surf_node = node.child("surface"), i_surf = 0; surf_node;
37,008 ✔
1263
         surf_node = surf_node.next_sibling("surface"), i_surf++) {
30,976 ✔
1264
      std::string surf_type = get_node_value(surf_node, "type", true, true);
30,976 ✔
1265

1266
      // Allocate and initialize the new surface
1267

1268
      if (surf_type == "x-plane") {
30,976 ✔
1269
        model::surfaces.push_back(make_unique<SurfaceXPlane>(surf_node));
7,733 ✔
1270

1271
      } else if (surf_type == "y-plane") {
23,243 ✔
1272
        model::surfaces.push_back(make_unique<SurfaceYPlane>(surf_node));
7,002 ✔
1273

1274
      } else if (surf_type == "z-plane") {
16,241 ✔
1275
        model::surfaces.push_back(make_unique<SurfaceZPlane>(surf_node));
5,082 ✔
1276

1277
      } else if (surf_type == "plane") {
11,159 ✔
1278
        model::surfaces.push_back(make_unique<SurfacePlane>(surf_node));
1,415 ✔
1279

1280
      } else if (surf_type == "x-cylinder") {
9,744 ✔
1281
        model::surfaces.push_back(make_unique<SurfaceXCylinder>(surf_node));
27 ✔
1282

1283
      } else if (surf_type == "y-cylinder") {
9,717 ✔
1284
        model::surfaces.push_back(make_unique<SurfaceYCylinder>(surf_node));
16 ✔
1285

1286
      } else if (surf_type == "z-cylinder") {
9,701 ✔
1287
        model::surfaces.push_back(make_unique<SurfaceZCylinder>(surf_node));
3,683 ✔
1288

1289
      } else if (surf_type == "sphere") {
6,018 ✔
1290
        model::surfaces.push_back(make_unique<SurfaceSphere>(surf_node));
5,862 ✔
1291

1292
      } else if (surf_type == "x-cone") {
156 !
1293
        model::surfaces.push_back(make_unique<SurfaceXCone>(surf_node));
×
1294

1295
      } else if (surf_type == "y-cone") {
156 !
1296
        model::surfaces.push_back(make_unique<SurfaceYCone>(surf_node));
×
1297

1298
      } else if (surf_type == "z-cone") {
156 ✔
1299
        model::surfaces.push_back(make_unique<SurfaceZCone>(surf_node));
9 ✔
1300

1301
      } else if (surf_type == "quadric") {
147 ✔
1302
        model::surfaces.push_back(make_unique<SurfaceQuadric>(surf_node));
9 ✔
1303

1304
      } else if (surf_type == "x-torus") {
138 ✔
1305
        model::surfaces.push_back(std::make_unique<SurfaceXTorus>(surf_node));
37 ✔
1306

1307
      } else if (surf_type == "y-torus") {
101 ✔
1308
        model::surfaces.push_back(std::make_unique<SurfaceYTorus>(surf_node));
37 ✔
1309

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

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

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

1342
  // Fill the surface map
1343
  for (int i_surf = 0; i_surf < model::surfaces.size(); i_surf++) {
37,008 ✔
1344
    int id = model::surfaces[i_surf]->id_;
30,976 !
1345
    auto in_map = model::surface_map.find(id);
30,976 !
1346
    if (in_map == model::surface_map.end()) {
30,976 !
1347
      model::surface_map[id] = i_surf;
30,976 ✔
1348
    } else {
1349
      fatal_error(
×
1350
        fmt::format("Two or more surfaces use the same unique ID: {}", id));
×
1351
    }
1352
  }
1353
}
6,032 ✔
1354

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

1365
    for (auto s : cell->surfaces()) {
813 ✔
1366
      auto surf_idx = std::abs(s) - 1;
656 ✔
1367
      auto id = model::surfaces[surf_idx]->id_;
656 ✔
1368

1369
      if (periodic_sense_map.count(id)) {
656 ✔
1370
        periodic_sense_map[id] = std::copysign(1, s);
334 ✔
1371
        --n_periodic;
334 ✔
1372
      }
1373
    }
157 ✔
1374
  }
1375

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

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

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

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

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

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

1478
    // If albedo data is present in albedo map, set the boundary albedo.
1479
    if (albedo_map.count(surf1.id_)) {
167 !
1480
      surf1.bc_->set_albedo(albedo_map[surf1.id_]);
×
1481
    }
1482
    if (albedo_map.count(surf2.id_)) {
167 !
1483
      surf2.bc_->set_albedo(albedo_map[surf2.id_]);
167 ✔
1484
    }
1485
  }
1486
}
6,032 ✔
1487

1488
void free_memory_surfaces()
6,096 ✔
1489
{
1490
  model::surfaces.clear();
6,096 ✔
1491
  model::surface_map.clear();
6,096 ✔
1492
}
6,096 ✔
1493

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