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

openmc-dev / openmc / 22920521397

10 Mar 2026 07:28PM UTC coverage: 81.221% (-0.5%) from 81.721%
22920521397

Pull #3810

github

web-flow
Merge c81063b52 into 1dc4aa988
Pull Request #3810: Implementation of migration area score

17031 of 24387 branches covered (69.84%)

Branch coverage included in aggregate %.

37 of 52 new or added lines in 5 files covered. (71.15%)

2537 existing lines in 86 files now uncovered.

57019 of 66784 relevant lines covered (85.38%)

35780397.68 hits per line

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

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

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

9
#include <fmt/core.h>
10

11
#include "openmc/array.h"
12
#include "openmc/cell.h"
13
#include "openmc/container_util.h"
14
#include "openmc/error.h"
15
#include "openmc/external/quartic_solver.h"
16
#include "openmc/hdf5_interface.h"
17
#include "openmc/math_functions.h"
18
#include "openmc/random_lcg.h"
19
#include "openmc/settings.h"
20
#include "openmc/simulation.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(
28,717✔
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");
28,717✔
44
  if (coeffs_file.size() != coeffs.size()) {
28,717!
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;
28,717✔
52
  for (auto c : coeffs) {
85,493✔
53
    *c = coeffs_file[i++];
56,776✔
54
  }
55
}
28,717✔
56

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

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

63
Surface::Surface(pugi::xml_node surf_node)
28,717✔
64
{
65
  if (check_for_node(surf_node, "id")) {
28,717!
66
    id_ = std::stoi(get_node_value(surf_node, "id"));
57,434✔
67
    if (contains(settings::source_write_surf_id, id_) ||
57,434✔
68
        settings::source_write_surf_id.empty()) {
28,185✔
69
      surf_source_ = true;
28,010✔
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")) {
28,717✔
76
    name_ = get_node_value(surf_node, "name", false);
7,045✔
77
  }
78

79
  if (check_for_node(surf_node, "boundary")) {
28,717✔
80
    std::string surf_bc = get_node_value(surf_node, "boundary", true, true);
16,862✔
81

82
    if (surf_bc == "transmission" || surf_bc == "transmit" || surf_bc.empty()) {
33,724!
83
      // Leave the bc_ a nullptr
84
    } else if (surf_bc == "vacuum") {
16,862✔
85
      bc_ = make_unique<VacuumBC>();
8,125✔
86
    } else if (surf_bc == "reflective" || surf_bc == "reflect" ||
9,157!
87
               surf_bc == "reflecting") {
420!
88
      bc_ = make_unique<ReflectiveBC>();
8,317✔
89
    } else if (surf_bc == "white") {
420✔
90
      bc_ = make_unique<WhiteBC>();
60✔
91
    } else if (surf_bc == "periodic") {
360!
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_) {
16,862!
100
      double surf_alb = std::stod(get_node_value(surf_node, "albedo"));
120✔
101

102
      if (surf_alb < 0.0)
60!
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)
60!
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);
60✔
114
    }
115
  }
16,862✔
116
}
28,717✔
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,720,299✔
130
  }
131
  return f > 0.0;
2,147,483,647✔
132
}
133

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

140
  // Reflect direction according to normal.
141
  return u.reflect(n);
434,581,775✔
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
22,590✔
166
{
167
  hid_t surf_group = create_group(group_id, fmt::format("surface {}", id_));
22,590✔
168

169
  if (geom_type() == GeometryType::DAG) {
22,590!
UNCOV
170
    write_string(surf_group, "geom_type", "dagmc", false);
×
171
  } else if (geom_type() == GeometryType::CSG) {
22,590!
172
    write_string(surf_group, "geom_type", "csg", false);
22,590✔
173

174
    if (bc_) {
22,590✔
175
      write_string(surf_group, "boundary_type", bc_->type(), false);
13,753✔
176
      bc_->to_hdf5(surf_group);
13,753✔
177

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

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

195
  if (!name_.empty()) {
22,590✔
196
    write_string(surf_group, "name", name_, false);
5,570✔
197
  }
198

199
  to_hdf5_inner(surf_group);
22,590✔
200

201
  close_group(surf_group);
22,590✔
202
}
22,590✔
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)
7,510✔
227
{
228
  read_coeffs(surf_node, id_, {&x0_});
7,510✔
229
}
7,510✔
230

231
double SurfaceXPlane::evaluate(Position r) const
1,086,508,308✔
232
{
233
  return r.x - x0_;
1,086,508,308✔
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
192,450,112✔
242
{
243
  return {1., 0., 0.};
192,450,112✔
244
}
245

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

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

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

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

271
double SurfaceYPlane::evaluate(Position r) const
1,079,526,895✔
272
{
273
  return r.y - y0_;
1,079,526,895✔
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
204,750,490✔
282
{
283
  return {0., 1., 0.};
204,750,490✔
284
}
285

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

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

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

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

311
double SurfaceZPlane::evaluate(Position r) const
316,167,521✔
312
{
313
  return r.z - z0_;
316,167,521✔
314
}
315

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

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

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

333
BoundingBox SurfaceZPlane::bounding_box(bool pos_side) const
×
334
{
335
  if (pos_side) {
×
336
    return {{-INFTY, -INFTY, z0_}, {INFTY, INFTY, INFTY}};
×
337
  } else {
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)
1,298✔
347
{
348
  read_coeffs(surf_node, id_, {&A_, &B_, &C_, &D_});
1,298✔
349
}
1,298✔
350

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

356
double SurfacePlane::distance(Position r, Direction u, bool coincident) const
511,399,282✔
357
{
358
  const double f = A_ * r.x + B_ * r.y + C_ * r.z - D_;
511,399,282✔
359
  const double projection = A_ * u.x + B_ * u.y + C_ * u.z;
511,399,282✔
360
  if (coincident || std::abs(f) < FP_COINCIDENT || projection == 0.0) {
511,399,282!
361
    return INFTY;
362
  } else {
363
    const double d = -f / projection;
469,632,878✔
364
    if (d < 0.0)
469,632,878✔
365
      return INFTY;
366
    return d;
255,563,502✔
367
  }
368
}
369

370
Direction SurfacePlane::normal(Position r) const
3,637,096✔
371
{
372
  return {A_, B_, C_};
3,637,096✔
373
}
374

375
void SurfacePlane::to_hdf5_inner(hid_t group_id) const
940✔
376
{
377
  write_string(group_id, "type", "plane", false);
940✔
378
  array<double, 4> coeffs {{A_, B_, C_, D_}};
940✔
379
  write_dataset(group_id, "coefficients", coeffs);
940✔
380
}
940✔
381

382
//==============================================================================
383
// Generic functions for x-, y-, and z-, cylinders
384
//==============================================================================
385

386
// The template parameters indicate the axes perpendicular to the axis of the
387
// cylinder.  offset1 and offset2 should correspond with i1 and i2,
388
// respectively.
389
template<int i1, int i2>
390
double axis_aligned_cylinder_evaluate(
1,186,376,556✔
391
  Position r, double offset1, double offset2, double radius)
392
{
393
  const double r1 = r.get<i1>() - offset1;
1,186,376,556✔
394
  const double r2 = r.get<i2>() - offset2;
1,186,376,556✔
395
  return r1 * r1 + r2 * r2 - radius * radius;
1,186,376,556✔
396
}
397

398
// The first template parameter indicates which axis the cylinder is aligned to.
399
// The other two parameters indicate the other two axes.  offset1 and offset2
400
// should correspond with i2 and i3, respectively.
401
template<int i1, int i2, int i3>
402
double axis_aligned_cylinder_distance(Position r, Direction u, bool coincident,
1,315,373,087✔
403
  double offset1, double offset2, double radius)
404
{
405
  const double a = 1.0 - u.get<i1>() * u.get<i1>(); // u^2 + v^2
1,315,373,087✔
406
  if (a == 0.0)
1,315,373,087✔
407
    return INFTY;
408

409
  const double r2 = r.get<i2>() - offset1;
1,314,857,635✔
410
  const double r3 = r.get<i3>() - offset2;
1,314,857,635✔
411
  const double k = r2 * u.get<i2>() + r3 * u.get<i3>();
1,314,857,635✔
412
  const double c = r2 * r2 + r3 * r3 - radius * radius;
1,314,857,635✔
413
  const double quad = k * k - a * c;
1,314,857,635✔
414

415
  if (quad < 0.0) {
1,314,857,635✔
416
    // No intersection with cylinder.
417
    return INFTY;
418

419
  } else if (coincident || std::abs(c) < FP_COINCIDENT) {
1,107,380,127✔
420
    // Particle is on the cylinder, thus one distance is positive/negative
421
    // and the other is zero. The sign of k determines if we are facing in or
422
    // out.
423
    if (k >= 0.0) {
510,508,649✔
424
      return INFTY;
425
    } else {
426
      return (-k + sqrt(quad)) / a;
254,497,642✔
427
    }
428

429
  } else if (c < 0.0) {
596,871,478✔
430
    // Particle is inside the cylinder, thus one distance must be negative
431
    // and one must be positive. The positive distance will be the one with
432
    // negative sign on sqrt(quad).
433
    return (-k + sqrt(quad)) / a;
244,294,089✔
434

435
  } else {
436
    // Particle is outside the cylinder, thus both distances are either
437
    // positive or negative. If positive, the smaller distance is the one
438
    // with positive sign on sqrt(quad).
439
    const double d = (-k - sqrt(quad)) / a;
352,577,389✔
440
    if (d < 0.0)
352,577,389✔
441
      return INFTY;
442
    return d;
305,536,762✔
443
  }
444
}
445

446
// The first template parameter indicates which axis the cylinder is aligned to.
447
// The other two parameters indicate the other two axes.  offset1 and offset2
448
// should correspond with i2 and i3, respectively.
449
template<int i1, int i2, int i3>
450
Direction axis_aligned_cylinder_normal(
1,522,521✔
451
  Position r, double offset1, double offset2)
452
{
453
  Direction u;
1,522,521✔
454
  u.get<i2>() = 2.0 * (r.get<i2>() - offset1);
1,522,521✔
455
  u.get<i3>() = 2.0 * (r.get<i3>() - offset2);
1,522,521✔
456
  u.get<i1>() = 0.0;
457
  return u;
458
}
459

460
//==============================================================================
461
// SurfaceXCylinder implementation
462
//==============================================================================
463

464
SurfaceXCylinder::SurfaceXCylinder(pugi::xml_node surf_node)
30✔
465
  : Surface(surf_node)
30✔
466
{
467
  read_coeffs(surf_node, id_, {&y0_, &z0_, &radius_});
30✔
468
}
30✔
469

470
double SurfaceXCylinder::evaluate(Position r) const
971,167✔
471
{
472
  return axis_aligned_cylinder_evaluate<1, 2>(r, y0_, z0_, radius_);
971,167✔
473
}
474

475
double SurfaceXCylinder::distance(
1,111,684✔
476
  Position r, Direction u, bool coincident) const
477
{
478
  return axis_aligned_cylinder_distance<0, 1, 2>(
1,111,684✔
479
    r, u, coincident, y0_, z0_, radius_);
1,111,684✔
480
}
481

482
Direction SurfaceXCylinder::normal(Position r) const
253,414✔
483
{
484
  return axis_aligned_cylinder_normal<0, 1, 2>(r, y0_, z0_);
253,414✔
485
}
486

487
void SurfaceXCylinder::to_hdf5_inner(hid_t group_id) const
21✔
488
{
489
  write_string(group_id, "type", "x-cylinder", false);
21✔
490
  array<double, 3> coeffs {{y0_, z0_, radius_}};
21✔
491
  write_dataset(group_id, "coefficients", coeffs);
21✔
492
}
21✔
493

494
BoundingBox SurfaceXCylinder::bounding_box(bool pos_side) const
×
495
{
496
  if (!pos_side) {
×
497
    return {{-INFTY, y0_ - radius_, z0_ - radius_},
×
498
      {INFTY, y0_ + radius_, z0_ + radius_}};
×
499
  } else {
500
    return {};
×
501
  }
502
}
503
//==============================================================================
504
// SurfaceYCylinder implementation
505
//==============================================================================
506

507
SurfaceYCylinder::SurfaceYCylinder(pugi::xml_node surf_node)
10✔
508
  : Surface(surf_node)
10✔
509
{
510
  read_coeffs(surf_node, id_, {&x0_, &z0_, &radius_});
10✔
511
}
10✔
512

513
double SurfaceYCylinder::evaluate(Position r) const
108,808✔
514
{
515
  return axis_aligned_cylinder_evaluate<0, 2>(r, x0_, z0_, radius_);
108,808✔
516
}
517

518
double SurfaceYCylinder::distance(
419,853✔
519
  Position r, Direction u, bool coincident) const
520
{
521
  return axis_aligned_cylinder_distance<1, 0, 2>(
419,853✔
522
    r, u, coincident, x0_, z0_, radius_);
419,853✔
523
}
524

525
Direction SurfaceYCylinder::normal(Position r) const
×
526
{
527
  return axis_aligned_cylinder_normal<1, 0, 2>(r, x0_, z0_);
×
528
}
529

530
void SurfaceYCylinder::to_hdf5_inner(hid_t group_id) const
7✔
531
{
532
  write_string(group_id, "type", "y-cylinder", false);
7✔
533
  array<double, 3> coeffs {{x0_, z0_, radius_}};
7✔
534
  write_dataset(group_id, "coefficients", coeffs);
7✔
535
}
7✔
536

537
BoundingBox SurfaceYCylinder::bounding_box(bool pos_side) const
×
538
{
539
  if (!pos_side) {
×
540
    return {{x0_ - radius_, -INFTY, z0_ - radius_},
×
541
      {x0_ + radius_, INFTY, z0_ + radius_}};
×
542
  } else {
543
    return {};
×
544
  }
545
}
546

547
//==============================================================================
548
// SurfaceZCylinder implementation
549
//==============================================================================
550

551
SurfaceZCylinder::SurfaceZCylinder(pugi::xml_node surf_node)
3,428✔
552
  : Surface(surf_node)
3,428✔
553
{
554
  read_coeffs(surf_node, id_, {&x0_, &y0_, &radius_});
3,428✔
555
}
3,428✔
556

557
double SurfaceZCylinder::evaluate(Position r) const
1,185,296,581✔
558
{
559
  return axis_aligned_cylinder_evaluate<0, 1>(r, x0_, y0_, radius_);
1,185,296,581✔
560
}
561

562
double SurfaceZCylinder::distance(
1,313,841,550✔
563
  Position r, Direction u, bool coincident) const
564
{
565
  return axis_aligned_cylinder_distance<2, 0, 1>(
1,313,841,550✔
566
    r, u, coincident, x0_, y0_, radius_);
1,313,841,550✔
567
}
568

569
Direction SurfaceZCylinder::normal(Position r) const
1,269,107✔
570
{
571
  return axis_aligned_cylinder_normal<2, 0, 1>(r, x0_, y0_);
1,269,107✔
572
}
573

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

581
BoundingBox SurfaceZCylinder::bounding_box(bool pos_side) const
28✔
582
{
583
  if (!pos_side) {
28✔
584
    return {{x0_ - radius_, y0_ - radius_, -INFTY},
14✔
585
      {x0_ + radius_, y0_ + radius_, INFTY}};
14✔
586
  } else {
587
    return {};
14✔
588
  }
589
}
590

591
//==============================================================================
592
// SurfaceSphere implementation
593
//==============================================================================
594

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

600
double SurfaceSphere::evaluate(Position r) const
426,257,326✔
601
{
602
  const double x = r.x - x0_;
426,257,326✔
603
  const double y = r.y - y0_;
426,257,326✔
604
  const double z = r.z - z0_;
426,257,326✔
605
  return x * x + y * y + z * z - radius_ * radius_;
426,257,326✔
606
}
607

608
double SurfaceSphere::distance(Position r, Direction u, bool coincident) const
718,221,351✔
609
{
610
  const double x = r.x - x0_;
718,221,351✔
611
  const double y = r.y - y0_;
718,221,351✔
612
  const double z = r.z - z0_;
718,221,351✔
613
  const double k = x * u.x + y * u.y + z * u.z;
718,221,351✔
614
  const double c = x * x + y * y + z * z - radius_ * radius_;
718,221,351✔
615
  const double quad = k * k - c;
718,221,351✔
616

617
  if (quad < 0.0) {
718,221,351✔
618
    // No intersection with sphere.
619
    return INFTY;
620

621
  } else if (coincident || std::abs(c) < FP_COINCIDENT) {
613,378,357!
622
    // Particle is on the sphere, thus one distance is positive/negative and
623
    // the other is zero. The sign of k determines if we are facing in or out.
624
    if (k >= 0.0) {
66,164,766✔
625
      return INFTY;
626
    } else {
627
      return -k + sqrt(quad);
37,927,800✔
628
    }
629

630
  } else if (c < 0.0) {
547,213,591✔
631
    // Particle is inside the sphere, thus one distance must be negative and
632
    // one must be positive. The positive distance will be the one with
633
    // negative sign on sqrt(quad)
634
    return -k + sqrt(quad);
511,331,379✔
635

636
  } else {
637
    // Particle is outside the sphere, thus both distances are either positive
638
    // or negative. If positive, the smaller distance is the one with positive
639
    // sign on sqrt(quad).
640
    const double d = -k - sqrt(quad);
35,882,212✔
641
    if (d < 0.0)
35,882,212✔
642
      return INFTY;
643
    return d;
28,960,529✔
644
  }
645
}
646

647
Direction SurfaceSphere::normal(Position r) const
34,691,181✔
648
{
649
  return {2.0 * (r.x - x0_), 2.0 * (r.y - y0_), 2.0 * (r.z - z0_)};
34,691,181✔
650
}
651

652
void SurfaceSphere::to_hdf5_inner(hid_t group_id) const
4,388✔
653
{
654
  write_string(group_id, "type", "sphere", false);
4,388✔
655
  array<double, 4> coeffs {{x0_, y0_, z0_, radius_}};
4,388✔
656
  write_dataset(group_id, "coefficients", coeffs);
4,388✔
657
}
4,388✔
658

659
BoundingBox SurfaceSphere::bounding_box(bool pos_side) const
×
660
{
661
  if (!pos_side) {
×
662
    return {{x0_ - radius_, y0_ - radius_, z0_ - radius_},
×
663
      {x0_ + radius_, y0_ + radius_, z0_ + radius_}};
×
664
  } else {
665
    return {};
×
666
  }
667
}
668

669
//==============================================================================
670
// Generic functions for x-, y-, and z-, cones
671
//==============================================================================
672

673
// The first template parameter indicates which axis the cone is aligned to.
674
// The other two parameters indicate the other two axes.  offset1, offset2,
675
// and offset3 should correspond with i1, i2, and i3, respectively.
676
template<int i1, int i2, int i3>
677
double axis_aligned_cone_evaluate(
207,361✔
678
  Position r, double offset1, double offset2, double offset3, double radius_sq)
679
{
680
  const double r1 = r.get<i1>() - offset1;
207,361✔
681
  const double r2 = r.get<i2>() - offset2;
207,361✔
682
  const double r3 = r.get<i3>() - offset3;
207,361✔
683
  return r2 * r2 + r3 * r3 - radius_sq * r1 * r1;
207,361✔
684
}
685

686
// The first template parameter indicates which axis the cone is aligned to.
687
// The other two parameters indicate the other two axes.  offset1, offset2,
688
// and offset3 should correspond with i1, i2, and i3, respectively.
689
template<int i1, int i2, int i3>
690
double axis_aligned_cone_distance(Position r, Direction u, bool coincident,
622,377✔
691
  double offset1, double offset2, double offset3, double radius_sq)
692
{
693
  const double r1 = r.get<i1>() - offset1;
622,377✔
694
  const double r2 = r.get<i2>() - offset2;
622,377✔
695
  const double r3 = r.get<i3>() - offset3;
622,377✔
696
  const double a = u.get<i2>() * u.get<i2>() + u.get<i3>() * u.get<i3>() -
622,377✔
697
                   radius_sq * u.get<i1>() * u.get<i1>();
622,377✔
698
  const double k =
622,377✔
699
    r2 * u.get<i2>() + r3 * u.get<i3>() - radius_sq * r1 * u.get<i1>();
622,377✔
700
  const double c = r2 * r2 + r3 * r3 - radius_sq * r1 * r1;
622,377✔
701
  double quad = k * k - a * c;
622,377✔
702

703
  double d;
704

705
  if (quad < 0.0) {
622,377!
706
    // No intersection with cone.
707
    return INFTY;
708

709
  } else if (coincident || std::abs(c) < FP_COINCIDENT) {
622,377!
710
    // Particle is on the cone, thus one distance is positive/negative
711
    // and the other is zero. The sign of k determines if we are facing in or
712
    // out.
713
    if (k >= 0.0) {
37,870!
714
      d = (-k - sqrt(quad)) / a;
×
715
    } else {
716
      d = (-k + sqrt(quad)) / a;
37,870✔
717
    }
718

719
  } else {
720
    // Calculate both solutions to the quadratic.
721
    quad = sqrt(quad);
584,507✔
722
    d = (-k - quad) / a;
584,507✔
723
    const double b = (-k + quad) / a;
584,507✔
724

725
    // Determine the smallest positive solution.
726
    if (d < 0.0) {
584,507✔
727
      if (b > 0.0)
496,391✔
728
        d = b;
423,248✔
729
    } else {
730
      if (b > 0.0) {
88,116!
731
        if (b < d)
88,116!
732
          d = b;
88,116✔
733
      }
734
    }
735
  }
736

737
  // If the distance was negative, set boundary distance to infinity.
738
  if (d <= 0.0)
622,377✔
739
    return INFTY;
86,814✔
740
  return d;
741
}
742

743
// The first template parameter indicates which axis the cone is aligned to.
744
// The other two parameters indicate the other two axes.  offset1, offset2,
745
// and offset3 should correspond with i1, i2, and i3, respectively.
746
template<int i1, int i2, int i3>
747
Direction axis_aligned_cone_normal(
37,870✔
748
  Position r, double offset1, double offset2, double offset3, double radius_sq)
749
{
750
  Direction u;
751
  u.get<i1>() = -2.0 * radius_sq * (r.get<i1>() - offset1);
37,870✔
752
  u.get<i2>() = 2.0 * (r.get<i2>() - offset2);
37,870✔
753
  u.get<i3>() = 2.0 * (r.get<i3>() - offset3);
37,870✔
754
  return u;
755
}
756

757
//==============================================================================
758
// SurfaceXCone implementation
759
//==============================================================================
760

761
SurfaceXCone::SurfaceXCone(pugi::xml_node surf_node) : Surface(surf_node)
×
762
{
763
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &radius_sq_});
×
764
}
×
765

766
double SurfaceXCone::evaluate(Position r) const
×
767
{
768
  return axis_aligned_cone_evaluate<0, 1, 2>(r, x0_, y0_, z0_, radius_sq_);
×
769
}
770

771
double SurfaceXCone::distance(Position r, Direction u, bool coincident) const
×
772
{
773
  return axis_aligned_cone_distance<0, 1, 2>(
×
774
    r, u, coincident, x0_, y0_, z0_, radius_sq_);
×
775
}
776

777
Direction SurfaceXCone::normal(Position r) const
×
778
{
779
  return axis_aligned_cone_normal<0, 1, 2>(r, x0_, y0_, z0_, radius_sq_);
×
780
}
781

782
void SurfaceXCone::to_hdf5_inner(hid_t group_id) const
×
783
{
784
  write_string(group_id, "type", "x-cone", false);
×
785
  array<double, 4> coeffs {{x0_, y0_, z0_, radius_sq_}};
×
786
  write_dataset(group_id, "coefficients", coeffs);
×
787
}
×
788

789
//==============================================================================
790
// SurfaceYCone implementation
791
//==============================================================================
792

793
SurfaceYCone::SurfaceYCone(pugi::xml_node surf_node) : Surface(surf_node)
×
794
{
795
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &radius_sq_});
×
796
}
×
797

798
double SurfaceYCone::evaluate(Position r) const
×
799
{
800
  return axis_aligned_cone_evaluate<1, 0, 2>(r, y0_, x0_, z0_, radius_sq_);
×
801
}
802

803
double SurfaceYCone::distance(Position r, Direction u, bool coincident) const
×
804
{
805
  return axis_aligned_cone_distance<1, 0, 2>(
×
806
    r, u, coincident, y0_, x0_, z0_, radius_sq_);
×
807
}
808

809
Direction SurfaceYCone::normal(Position r) const
×
810
{
811
  return axis_aligned_cone_normal<1, 0, 2>(r, y0_, x0_, z0_, radius_sq_);
×
812
}
813

814
void SurfaceYCone::to_hdf5_inner(hid_t group_id) const
×
815
{
816
  write_string(group_id, "type", "y-cone", false);
×
817
  array<double, 4> coeffs {{x0_, y0_, z0_, radius_sq_}};
×
818
  write_dataset(group_id, "coefficients", coeffs);
×
819
}
×
820

821
//==============================================================================
822
// SurfaceZCone implementation
823
//==============================================================================
824

825
SurfaceZCone::SurfaceZCone(pugi::xml_node surf_node) : Surface(surf_node)
10✔
826
{
827
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &radius_sq_});
10✔
828
}
10✔
829

830
double SurfaceZCone::evaluate(Position r) const
207,361✔
831
{
832
  return axis_aligned_cone_evaluate<2, 0, 1>(r, z0_, x0_, y0_, radius_sq_);
207,361✔
833
}
834

835
double SurfaceZCone::distance(Position r, Direction u, bool coincident) const
622,377✔
836
{
837
  return axis_aligned_cone_distance<2, 0, 1>(
622,377✔
838
    r, u, coincident, z0_, x0_, y0_, radius_sq_);
622,377✔
839
}
840

841
Direction SurfaceZCone::normal(Position r) const
37,870✔
842
{
843
  return axis_aligned_cone_normal<2, 0, 1>(r, z0_, x0_, y0_, radius_sq_);
37,870✔
844
}
845

846
void SurfaceZCone::to_hdf5_inner(hid_t group_id) const
7✔
847
{
848
  write_string(group_id, "type", "z-cone", false);
7✔
849
  array<double, 4> coeffs {{x0_, y0_, z0_, radius_sq_}};
7✔
850
  write_dataset(group_id, "coefficients", coeffs);
7✔
851
}
7✔
852

853
//==============================================================================
854
// SurfaceQuadric implementation
855
//==============================================================================
856

857
SurfaceQuadric::SurfaceQuadric(pugi::xml_node surf_node) : Surface(surf_node)
10✔
858
{
859
  read_coeffs(
20✔
860
    surf_node, id_, {&A_, &B_, &C_, &D_, &E_, &F_, &G_, &H_, &J_, &K_});
10✔
861
}
10✔
862

863
double SurfaceQuadric::evaluate(Position r) const
104,724✔
864
{
865
  const double x = r.x;
104,724✔
866
  const double y = r.y;
104,724✔
867
  const double z = r.z;
104,724✔
868
  return x * (A_ * x + D_ * y + G_) + y * (B_ * y + E_ * z + H_) +
104,724✔
869
         z * (C_ * z + F_ * x + J_) + K_;
104,724✔
870
}
871

872
double SurfaceQuadric::distance(
201,523✔
873
  Position r, Direction ang, bool coincident) const
874
{
875
  const double& x = r.x;
201,523✔
876
  const double& y = r.y;
201,523✔
877
  const double& z = r.z;
201,523✔
878
  const double& u = ang.x;
201,523✔
879
  const double& v = ang.y;
201,523✔
880
  const double& w = ang.z;
201,523✔
881

882
  const double a =
201,523✔
883
    A_ * u * u + B_ * v * v + C_ * w * w + D_ * u * v + E_ * v * w + F_ * u * w;
201,523✔
884
  const double k = A_ * u * x + B_ * v * y + C_ * w * z +
201,523✔
885
                   0.5 * (D_ * (u * y + v * x) + E_ * (v * z + w * y) +
201,523✔
886
                           F_ * (w * x + u * z) + G_ * u + H_ * v + J_ * w);
201,523✔
887
  const double c = A_ * x * x + B_ * y * y + C_ * z * z + D_ * x * y +
201,523✔
888
                   E_ * y * z + F_ * x * z + G_ * x + H_ * y + J_ * z + K_;
201,523✔
889
  double quad = k * k - a * c;
201,523✔
890

891
  double d;
201,523✔
892

893
  if (quad < 0.0) {
201,523!
894
    // No intersection with surface.
895
    return INFTY;
896

897
  } else if (coincident || std::abs(c) < FP_COINCIDENT) {
201,523!
898
    // Particle is on the surface, thus one distance is positive/negative and
899
    // the other is zero. The sign of k determines which distance is zero and
900
    // which is not. Additionally, if a is zero, it means the particle is on
901
    // a plane-like surface.
902
    if (a == 0.0) {
22,946!
903
      d = INFTY; // see the below explanation
904
    } else if (k >= 0.0) {
22,946!
905
      d = (-k - sqrt(quad)) / a;
×
906
    } else {
907
      d = (-k + sqrt(quad)) / a;
22,946✔
908
    }
909

910
  } else if (a == 0.0) {
178,577!
911
    // Given the orientation of the particle, the quadric looks like a plane in
912
    // this case, and thus we have only one solution despite potentially having
913
    // quad > 0.0. While the term under the square root may be real, in one
914
    // case of the +/- of the quadratic formula, 0/0 results, and in another, a
915
    // finite value over 0 results. Applying L'Hopital's to the 0/0 case gives
916
    // the below. Alternatively this can be found by simply putting a=0 in the
917
    // equation ax^2 + bx + c = 0.
918
    d = -0.5 * c / k;
×
919
  } else {
920
    // Calculate both solutions to the quadratic.
921
    quad = sqrt(quad);
178,577✔
922
    d = (-k - quad) / a;
178,577✔
923
    double b = (-k + quad) / a;
178,577✔
924

925
    // Determine the smallest positive solution.
926
    if (d < 0.0) {
178,577!
927
      if (b > 0.0)
178,577!
928
        d = b;
178,577✔
929
    } else {
930
      if (b > 0.0) {
×
931
        if (b < d)
×
932
          d = b;
×
933
      }
934
    }
935
  }
936

937
  // If the distance was negative, set boundary distance to infinity.
938
  if (d <= 0.0)
201,523!
939
    return INFTY;
×
940
  return d;
941
}
942

943
Direction SurfaceQuadric::normal(Position r) const
22,946✔
944
{
945
  const double& x = r.x;
22,946✔
946
  const double& y = r.y;
22,946✔
947
  const double& z = r.z;
22,946✔
948
  return {2.0 * A_ * x + D_ * y + F_ * z + G_,
22,946✔
949
    2.0 * B_ * y + D_ * x + E_ * z + H_, 2.0 * C_ * z + E_ * y + F_ * x + J_};
22,946✔
950
}
951

952
void SurfaceQuadric::to_hdf5_inner(hid_t group_id) const
7✔
953
{
954
  write_string(group_id, "type", "quadric", false);
7✔
955
  array<double, 10> coeffs {{A_, B_, C_, D_, E_, F_, G_, H_, J_, K_}};
7✔
956
  write_dataset(group_id, "coefficients", coeffs);
7✔
957
}
7✔
958

959
//==============================================================================
960
// Torus helper functions
961
//==============================================================================
962

963
double torus_distance(double x1, double x2, double x3, double u1, double u2,
16,885,631✔
964
  double u3, double A, double B, double C, bool coincident)
965
{
966
  // Coefficients for equation: (c2 t^2 + c1 t + c0)^2 = c2' t^2 + c1' t + c0'
967
  double D = (C * C) / (B * B);
16,885,631✔
968
  double c2 = u1 * u1 + u2 * u2 + D * u3 * u3;
16,885,631✔
969
  double c1 = 2 * (u1 * x1 + u2 * x2 + D * u3 * x3);
16,885,631✔
970
  double c0 = x1 * x1 + x2 * x2 + D * x3 * x3 + A * A - C * C;
16,885,631✔
971
  double four_A2 = 4 * A * A;
16,885,631✔
972
  double c2p = four_A2 * (u1 * u1 + u2 * u2);
16,885,631✔
973
  double c1p = 2 * four_A2 * (u1 * x1 + u2 * x2);
16,885,631✔
974
  double c0p = four_A2 * (x1 * x1 + x2 * x2);
16,885,631✔
975

976
  // Coefficient for equation: a t^4 + b t^3 + c t^2 + d t + e = 0. If the point
977
  // is coincident, the 'e' coefficient should be zero. Explicitly setting it to
978
  // zero helps avoid numerical issues below with root finding.
979
  double coeff[5];
16,885,631✔
980
  coeff[0] = coincident ? 0.0 : c0 * c0 - c0p;
16,885,631✔
981
  coeff[1] = 2 * c0 * c1 - c1p;
16,885,631✔
982
  coeff[2] = c1 * c1 + 2 * c0 * c2 - c2p;
16,885,631✔
983
  coeff[3] = 2 * c1 * c2;
16,885,631✔
984
  coeff[4] = c2 * c2;
16,885,631✔
985

986
  std::complex<double> roots[4];
16,885,631✔
987
  oqs::quartic_solver(coeff, roots);
16,885,631✔
988

989
  // Find smallest positive, real root. In the case where the particle is
990
  // coincident with the surface, we are sure to have one root very close to
991
  // zero but possibly small and positive. A tolerance is set to discard that
992
  // zero.
993
  double distance = INFTY;
16,885,631✔
994
  double cutoff = coincident ? TORUS_TOL : 0.0;
16,885,631✔
995
  for (int i = 0; i < 4; ++i) {
84,428,155✔
996
    if (roots[i].imag() == 0) {
67,542,524✔
997
      double root = roots[i].real();
16,509,836✔
998
      if (root > cutoff && root < distance) {
16,509,836✔
999
        // Avoid roots corresponding to internal surfaces
1000
        double s1 = x1 + u1 * root;
6,625,815✔
1001
        double s2 = x2 + u2 * root;
6,625,815✔
1002
        double s3 = x3 + u3 * root;
6,625,815✔
1003
        double check = D * s3 * s3 + s1 * s1 + s2 * s2 + A * A - C * C;
6,625,815✔
1004
        if (check >= 0) {
6,625,815!
1005
          distance = root;
6,625,815✔
1006
        }
1007
      }
1008
    }
1009
  }
1010
  return distance;
16,885,631✔
1011
}
1012

1013
//==============================================================================
1014
// SurfaceXTorus implementation
1015
//==============================================================================
1016

1017
SurfaceXTorus::SurfaceXTorus(pugi::xml_node surf_node) : Surface(surf_node)
38✔
1018
{
1019
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &A_, &B_, &C_});
38✔
1020
}
38✔
1021

1022
void SurfaceXTorus::to_hdf5_inner(hid_t group_id) const
35✔
1023
{
1024
  write_string(group_id, "type", "x-torus", false);
35✔
1025
  std::array<double, 6> coeffs {{x0_, y0_, z0_, A_, B_, C_}};
35✔
1026
  write_dataset(group_id, "coefficients", coeffs);
35✔
1027
}
35✔
1028

1029
double SurfaceXTorus::evaluate(Position r) const
516,259✔
1030
{
1031
  double x = r.x - x0_;
516,259✔
1032
  double y = r.y - y0_;
516,259✔
1033
  double z = r.z - z0_;
516,259✔
1034
  return (x * x) / (B_ * B_) +
1,032,518✔
1035
         std::pow(std::sqrt(y * y + z * z) - A_, 2) / (C_ * C_) - 1.;
516,259✔
1036
}
1037

1038
double SurfaceXTorus::distance(Position r, Direction u, bool coincident) const
5,312,755✔
1039
{
1040
  double x = r.x - x0_;
5,312,755✔
1041
  double y = r.y - y0_;
5,312,755✔
1042
  double z = r.z - z0_;
5,312,755✔
1043
  return torus_distance(y, z, x, u.y, u.z, u.x, A_, B_, C_, coincident);
5,312,755✔
1044
}
1045

1046
Direction SurfaceXTorus::normal(Position r) const
×
1047
{
1048
  // reduce the expansion of the full form for torus
1049
  double x = r.x - x0_;
×
1050
  double y = r.y - y0_;
×
1051
  double z = r.z - z0_;
×
1052

1053
  // f(x,y,z) = x^2/B^2 + (sqrt(y^2 + z^2) - A)^2/C^2 - 1
1054
  // ∂f/∂x = 2x/B^2
1055
  // ∂f/∂y = 2y(g - A)/(g*C^2) where g = sqrt(y^2 + z^2)
1056
  // ∂f/∂z = 2z(g - A)/(g*C^2)
1057
  // Multiplying by g*C^2*B^2 / 2 gives:
1058
  double g = std::sqrt(y * y + z * z);
×
1059
  double nx = C_ * C_ * g * x;
×
1060
  double ny = y * (g - A_) * B_ * B_;
×
1061
  double nz = z * (g - A_) * B_ * B_;
×
1062
  Direction n(nx, ny, nz);
×
1063
  return n / n.norm();
×
1064
}
1065

1066
//==============================================================================
1067
// SurfaceYTorus implementation
1068
//==============================================================================
1069

1070
SurfaceYTorus::SurfaceYTorus(pugi::xml_node surf_node) : Surface(surf_node)
38✔
1071
{
1072
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &A_, &B_, &C_});
38✔
1073
}
38✔
1074

1075
void SurfaceYTorus::to_hdf5_inner(hid_t group_id) const
35✔
1076
{
1077
  write_string(group_id, "type", "y-torus", false);
35✔
1078
  std::array<double, 6> coeffs {{x0_, y0_, z0_, A_, B_, C_}};
35✔
1079
  write_dataset(group_id, "coefficients", coeffs);
35✔
1080
}
35✔
1081

1082
double SurfaceYTorus::evaluate(Position r) const
495,084✔
1083
{
1084
  double x = r.x - x0_;
495,084✔
1085
  double y = r.y - y0_;
495,084✔
1086
  double z = r.z - z0_;
495,084✔
1087
  return (y * y) / (B_ * B_) +
990,168✔
1088
         std::pow(std::sqrt(x * x + z * z) - A_, 2) / (C_ * C_) - 1.;
495,084✔
1089
}
1090

1091
double SurfaceYTorus::distance(Position r, Direction u, bool coincident) const
5,354,300✔
1092
{
1093
  double x = r.x - x0_;
5,354,300✔
1094
  double y = r.y - y0_;
5,354,300✔
1095
  double z = r.z - z0_;
5,354,300✔
1096
  return torus_distance(x, z, y, u.x, u.z, u.y, A_, B_, C_, coincident);
5,354,300✔
1097
}
1098

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

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

1119
//==============================================================================
1120
// SurfaceZTorus implementation
1121
//==============================================================================
1122

1123
SurfaceZTorus::SurfaceZTorus(pugi::xml_node surf_node) : Surface(surf_node)
68✔
1124
{
1125
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &A_, &B_, &C_});
68✔
1126
}
68✔
1127

1128
void SurfaceZTorus::to_hdf5_inner(hid_t group_id) const
56✔
1129
{
1130
  write_string(group_id, "type", "z-torus", false);
56✔
1131
  std::array<double, 6> coeffs {{x0_, y0_, z0_, A_, B_, C_}};
56✔
1132
  write_dataset(group_id, "coefficients", coeffs);
56✔
1133
}
56✔
1134

1135
double SurfaceZTorus::evaluate(Position r) const
832,830✔
1136
{
1137
  double x = r.x - x0_;
832,830✔
1138
  double y = r.y - y0_;
832,830✔
1139
  double z = r.z - z0_;
832,830✔
1140
  return (z * z) / (B_ * B_) +
1,665,660✔
1141
         std::pow(std::sqrt(x * x + y * y) - A_, 2) / (C_ * C_) - 1.;
832,830✔
1142
}
1143

1144
double SurfaceZTorus::distance(Position r, Direction u, bool coincident) const
6,218,576✔
1145
{
1146
  double x = r.x - x0_;
6,218,576✔
1147
  double y = r.y - y0_;
6,218,576✔
1148
  double z = r.z - z0_;
6,218,576✔
1149
  return torus_distance(x, y, z, u.x, u.y, u.z, A_, B_, C_, coincident);
6,218,576✔
1150
}
1151

1152
Direction SurfaceZTorus::normal(Position r) const
×
1153
{
1154
  // reduce the expansion of the full form for torus
1155
  double x = r.x - x0_;
×
1156
  double y = r.y - y0_;
×
1157
  double z = r.z - z0_;
×
1158

1159
  // f(x,y,z) = z^2/B^2 + (sqrt(x^2 + y^2) - A)^2/C^2 - 1
1160
  // ∂f/∂x = 2x(g - A)/(g*C^2) where g = sqrt(x^2 + y^2)
1161
  // ∂f/∂y = 2y(g - A)/(g*C^2)
1162
  // ∂f/∂z = 2z/B^2
1163
  // Multiplying by g*C^2*B^2 / 2 gives:
1164
  double g = std::sqrt(x * x + y * y);
×
1165
  double nx = x * (g - A_) * B_ * B_;
×
1166
  double ny = y * (g - A_) * B_ * B_;
×
1167
  double nz = C_ * C_ * g * z;
×
1168
  Position n(nx, ny, nz);
×
1169
  return n / n.norm();
×
1170
}
1171

1172
//==============================================================================
1173

1174
void read_surfaces(pugi::xml_node node,
5,277✔
1175
  std::set<std::pair<int, int>>& periodic_pairs,
1176
  std::unordered_map<int, double>& albedo_map,
1177
  std::unordered_map<int, int>& periodic_sense_map)
1178
{
1179
  simulation::nonvacuum_boundary_present = false;
5,277✔
1180

1181
  // Count the number of surfaces
1182
  int n_surfaces = 0;
5,277✔
1183
  for (pugi::xml_node surf_node : node.children("surface")) {
33,427✔
1184
    n_surfaces++;
28,150✔
1185
  }
1186

1187
  // Loop over XML surface elements and populate the array.  Keep track of
1188
  // periodic surfaces and their albedos.
1189
  model::surfaces.reserve(n_surfaces);
5,277✔
1190
  {
5,277✔
1191
    pugi::xml_node surf_node;
5,277✔
1192
    int i_surf;
5,277✔
1193
    for (surf_node = node.child("surface"), i_surf = 0; surf_node;
33,427✔
1194
         surf_node = surf_node.next_sibling("surface"), i_surf++) {
28,150✔
1195
      std::string surf_type = get_node_value(surf_node, "type", true, true);
28,150✔
1196

1197
      // Allocate and initialize the new surface
1198

1199
      if (surf_type == "x-plane") {
28,150✔
1200
        model::surfaces.push_back(make_unique<SurfaceXPlane>(surf_node));
6,943✔
1201

1202
      } else if (surf_type == "y-plane") {
21,207✔
1203
        model::surfaces.push_back(make_unique<SurfaceYPlane>(surf_node));
6,235✔
1204

1205
      } else if (surf_type == "z-plane") {
14,972✔
1206
        model::surfaces.push_back(make_unique<SurfaceZPlane>(surf_node));
4,579✔
1207

1208
      } else if (surf_type == "plane") {
10,393✔
1209
        model::surfaces.push_back(make_unique<SurfacePlane>(surf_node));
1,298✔
1210

1211
      } else if (surf_type == "x-cylinder") {
9,095✔
1212
        model::surfaces.push_back(make_unique<SurfaceXCylinder>(surf_node));
30✔
1213

1214
      } else if (surf_type == "y-cylinder") {
9,065✔
1215
        model::surfaces.push_back(make_unique<SurfaceYCylinder>(surf_node));
10✔
1216

1217
      } else if (surf_type == "z-cylinder") {
9,055✔
1218
        model::surfaces.push_back(make_unique<SurfaceZCylinder>(surf_node));
3,428✔
1219

1220
      } else if (surf_type == "sphere") {
5,627✔
1221
        model::surfaces.push_back(make_unique<SurfaceSphere>(surf_node));
5,463✔
1222

1223
      } else if (surf_type == "x-cone") {
164!
1224
        model::surfaces.push_back(make_unique<SurfaceXCone>(surf_node));
×
1225

1226
      } else if (surf_type == "y-cone") {
164!
1227
        model::surfaces.push_back(make_unique<SurfaceYCone>(surf_node));
×
1228

1229
      } else if (surf_type == "z-cone") {
164✔
1230
        model::surfaces.push_back(make_unique<SurfaceZCone>(surf_node));
10✔
1231

1232
      } else if (surf_type == "quadric") {
154✔
1233
        model::surfaces.push_back(make_unique<SurfaceQuadric>(surf_node));
10✔
1234

1235
      } else if (surf_type == "x-torus") {
144✔
1236
        model::surfaces.push_back(std::make_unique<SurfaceXTorus>(surf_node));
38✔
1237

1238
      } else if (surf_type == "y-torus") {
106✔
1239
        model::surfaces.push_back(std::make_unique<SurfaceYTorus>(surf_node));
38✔
1240

1241
      } else if (surf_type == "z-torus") {
68!
1242
        model::surfaces.push_back(std::make_unique<SurfaceZTorus>(surf_node));
68✔
1243

1244
      } else {
1245
        fatal_error(fmt::format("Invalid surface type, \"{}\"", surf_type));
×
1246
      }
1247

1248
      // Check for a periodic surface
1249
      if (check_for_node(surf_node, "boundary")) {
28,150✔
1250
        std::string surf_bc = get_node_value(surf_node, "boundary", true, true);
16,862✔
1251
        if (surf_bc != "vacuum")
16,862✔
1252
          simulation::nonvacuum_boundary_present = true;
8,737✔
1253
        if (surf_bc == "periodic") {
16,862✔
1254
          periodic_sense_map[model::surfaces.back()->id_] = 0;
360✔
1255
          // Check for surface albedo. Skip sanity check as it is already done
1256
          // in the Surface class's constructor.
1257
          if (check_for_node(surf_node, "albedo")) {
360!
1258
            albedo_map[model::surfaces.back()->id_] =
×
1259
              std::stod(get_node_value(surf_node, "albedo"));
×
1260
          }
1261
          if (check_for_node(surf_node, "periodic_surface_id")) {
360✔
1262
            int i_periodic =
232✔
1263
              std::stoi(get_node_value(surf_node, "periodic_surface_id"));
464✔
1264
            int lo_id = std::min(model::surfaces.back()->id_, i_periodic);
232✔
1265
            int hi_id = std::max(model::surfaces.back()->id_, i_periodic);
232✔
1266
            periodic_pairs.insert({lo_id, hi_id});
232✔
1267
          } else {
1268
            periodic_pairs.insert({model::surfaces.back()->id_, -1});
128✔
1269
          }
1270
        }
1271
      }
16,862✔
1272
    }
28,150✔
1273
  }
1274

1275
  // Fill the surface map
1276
  for (int i_surf = 0; i_surf < model::surfaces.size(); i_surf++) {
33,427✔
1277
    int id = model::surfaces[i_surf]->id_;
28,150!
1278
    auto in_map = model::surface_map.find(id);
28,150!
1279
    if (in_map == model::surface_map.end()) {
28,150!
1280
      model::surface_map[id] = i_surf;
28,150✔
1281
    } else {
1282
      fatal_error(
×
1283
        fmt::format("Two or more surfaces use the same unique ID: {}", id));
×
1284
    }
1285
  }
1286
}
5,277✔
1287

1288
void prepare_boundary_conditions(std::set<std::pair<int, int>>& periodic_pairs,
5,277✔
1289
  std::unordered_map<int, double>& albedo_map,
1290
  std::unordered_map<int, int>& periodic_sense_map)
1291
{
1292
  // Fill the senses map for periodic surfaces
1293
  auto n_periodic = periodic_sense_map.size();
5,277✔
1294
  for (const auto& cell : model::cells) {
5,448✔
1295
    if (n_periodic == 0)
5,362✔
1296
      break; // Early exit once all periodic surfaces found
1297

1298
    for (auto s : cell->surfaces()) {
890✔
1299
      auto surf_idx = std::abs(s) - 1;
719✔
1300
      auto id = model::surfaces[surf_idx]->id_;
719✔
1301

1302
      if (periodic_sense_map.count(id)) {
719✔
1303
        periodic_sense_map[id] = std::copysign(1, s);
360✔
1304
        --n_periodic;
360✔
1305
      }
1306
    }
171✔
1307
  }
1308

1309
  // Resolve unpaired periodic surfaces.  A lambda function is used with
1310
  // std::find_if to identify the unpaired surfaces.
1311
  auto is_unresolved_pair = [](const std::pair<int, int> p) {
5,521✔
1312
    return p.second == -1;
244✔
1313
  };
1314
  auto first_unresolved = std::find_if(
5,277✔
1315
    periodic_pairs.begin(), periodic_pairs.end(), is_unresolved_pair);
1316
  if (first_unresolved != periodic_pairs.end()) {
5,277✔
1317
    // Found one unpaired surface; search for a second one
1318
    auto next_elem = first_unresolved;
64✔
1319
    next_elem++;
64!
1320
    auto second_unresolved =
64!
1321
      std::find_if(next_elem, periodic_pairs.end(), is_unresolved_pair);
64!
1322
    if (second_unresolved == periodic_pairs.end()) {
64!
1323
      fatal_error("Found only one periodic surface without a specified partner."
×
1324
                  " Please specify the partner for each periodic surface.");
1325
    }
1326

1327
    // Make sure there isn't a third unpaired surface
1328
    next_elem = second_unresolved;
64✔
1329
    next_elem++;
64!
1330
    auto third_unresolved =
64!
1331
      std::find_if(next_elem, periodic_pairs.end(), is_unresolved_pair);
64!
1332
    if (third_unresolved != periodic_pairs.end()) {
64!
1333
      fatal_error(
×
1334
        "Found at least three periodic surfaces without a specified "
1335
        "partner. Please specify the partner for each periodic surface.");
1336
    }
1337

1338
    // Add the completed pair and remove the old, unpaired entries
1339
    int lo_id = std::min(first_unresolved->first, second_unresolved->first);
64!
1340
    int hi_id = std::max(first_unresolved->first, second_unresolved->first);
64!
1341
    periodic_pairs.insert({lo_id, hi_id});
64✔
1342
    periodic_pairs.erase(first_unresolved);
64✔
1343
    periodic_pairs.erase(second_unresolved);
64✔
1344
  }
1345

1346
  // Assign the periodic boundary conditions with albedos
1347
  for (auto periodic_pair : periodic_pairs) {
5,457✔
1348
    int i_surf = model::surface_map[periodic_pair.first];
180✔
1349
    int j_surf = model::surface_map[periodic_pair.second];
180✔
1350
    Surface& surf1 {*model::surfaces[i_surf]};
180✔
1351
    Surface& surf2 {*model::surfaces[j_surf]};
180✔
1352

1353
    // Compute the dot product of the surface normals
1354
    Direction norm1 = surf1.normal({0, 0, 0});
180✔
1355
    Direction norm2 = surf2.normal({0, 0, 0});
180✔
1356
    norm1 /= norm1.norm();
180✔
1357
    norm2 /= norm2.norm();
180✔
1358
    double dot_prod = norm1.dot(norm2);
180✔
1359

1360
    // If the dot product is 1 (to within floating point precision) then the
1361
    // planes are parallel which indicates a translational periodic boundary
1362
    // condition.  Otherwise, it is a rotational periodic BC.
1363
    if (std::abs(1.0 - dot_prod) < FP_PRECISION) {
180✔
1364
      surf1.bc_ = make_unique<TranslationalPeriodicBC>(i_surf, j_surf);
75✔
1365
      surf2.bc_ = make_unique<TranslationalPeriodicBC>(j_surf, i_surf);
75✔
1366
    } else {
1367
      // check that both normals have at least one 0 component
1368
      if (std::abs(norm1.x) > FP_PRECISION &&
105✔
1369
          std::abs(norm1.y) > FP_PRECISION &&
105!
1370
          std::abs(norm1.z) > FP_PRECISION) {
75!
1371
        fatal_error(fmt::format(
×
1372
          "The normal ({}) of the periodic surface ({}) does not contain any "
1373
          "component with a zero value. A RotationalPeriodicBC requires one "
1374
          "component which is zero for both plane normals.",
1375
          norm1, i_surf));
1376
      }
1377
      if (std::abs(norm2.x) > FP_PRECISION &&
105✔
1378
          std::abs(norm2.y) > FP_PRECISION &&
105!
1379
          std::abs(norm2.z) > FP_PRECISION) {
75!
1380
        fatal_error(fmt::format(
×
1381
          "The normal ({}) of the periodic surface ({}) does not contain any "
1382
          "component with a zero value. A RotationalPeriodicBC requires one "
1383
          "component which is zero for both plane normals.",
1384
          norm2, j_surf));
1385
      }
1386
      // find common zero component, which indicates the periodic axis
1387
      RotationalPeriodicBC::PeriodicAxis axis;
105✔
1388
      if (std::abs(norm1.x) <= FP_PRECISION &&
105✔
1389
          std::abs(norm2.x) <= FP_PRECISION) {
10!
1390
        axis = RotationalPeriodicBC::PeriodicAxis::x;
10✔
1391
      } else if (std::abs(norm1.y) <= FP_PRECISION &&
95✔
1392
                 std::abs(norm2.y) <= FP_PRECISION) {
20✔
1393
        axis = RotationalPeriodicBC::PeriodicAxis::y;
10✔
1394
      } else if (std::abs(norm1.z) <= FP_PRECISION &&
85!
1395
                 std::abs(norm2.z) <= FP_PRECISION) {
85!
1396
        axis = RotationalPeriodicBC::PeriodicAxis::z;
85✔
1397
      } else {
1398
        fatal_error(fmt::format(
×
1399
          "There is no component which is 0.0 in both normal vectors. This "
1400
          "indicates that the two planes are not periodic about the X, Y, or Z "
1401
          "axis, which is not supported."));
1402
      }
1403
      auto i_sign = periodic_sense_map[periodic_pair.first];
105✔
1404
      auto j_sign = periodic_sense_map[periodic_pair.second];
105✔
1405
      surf1.bc_ = make_unique<RotationalPeriodicBC>(
105✔
1406
        i_sign * (i_surf + 1), j_sign * (j_surf + 1), axis);
105✔
1407
      surf2.bc_ = make_unique<RotationalPeriodicBC>(
105✔
1408
        j_sign * (j_surf + 1), i_sign * (i_surf + 1), axis);
210✔
1409
    }
1410

1411
    // If albedo data is present in albedo map, set the boundary albedo.
1412
    if (albedo_map.count(surf1.id_)) {
180!
1413
      surf1.bc_->set_albedo(albedo_map[surf1.id_]);
×
1414
    }
1415
    if (albedo_map.count(surf2.id_)) {
180!
1416
      surf2.bc_->set_albedo(albedo_map[surf2.id_]);
180✔
1417
    }
1418
  }
1419
}
5,277✔
1420

1421
void free_memory_surfaces()
5,358✔
1422
{
1423
  model::surfaces.clear();
5,358✔
1424
  model::surface_map.clear();
5,358✔
1425
}
5,358✔
1426

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

© 2026 Coveralls, Inc