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

openmc-dev / openmc / 35234388252

17 Sep 2026 02:33PM UTC coverage: 81.531% (+0.05%) from 81.477%
35234388252

Pull #3566

github

web-flow
Merge a71b610c1 into c66d748ca
Pull Request #3566: Resolve merge conflicts for PR #3516 - Implement Virtual Lattice Method for Efficient Simulation of Dispersed Fuels

20282 of 29358 branches covered (69.09%)

Branch coverage included in aggregate %.

388 of 443 new or added lines in 10 files covered. (87.58%)

2 existing lines in 1 file now uncovered.

62836 of 72589 relevant lines covered (86.56%)

49587992.13 hits per line

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

81.24
/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/string_utils.h"
21
#include "openmc/xml_interface.h"
22

23
namespace openmc {
24

25
//==============================================================================
26
// Global variables
27
//==============================================================================
28

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

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

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

49
  // Copy the coefficients
50
  int i = 0;
52,868 ✔
51
  for (auto c : coeffs) {
158,548 ✔
52
    *c = coeffs_file[i++];
105,680 ✔
53
  }
54
}
52,868 ✔
55

56
//==============================================================================
57
// Surface implementation
58
//==============================================================================
59

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

62
Surface::Surface(pugi::xml_node surf_node)
52,868 ✔
63
{
64
  if (check_for_node(surf_node, "id")) {
52,868 !
65
    id_ = std::stoi(get_node_value(surf_node, "id"));
105,736 ✔
66
    if (contains(settings::source_write_surf_id, id_) ||
105,736 ✔
67
        settings::source_write_surf_id.empty()) {
52,080 ✔
68
      surf_source_ = true;
51,826 ✔
69
    }
70
  } else {
71
    fatal_error("Must specify id of surface in geometry XML file.");
×
72
  }
73

74
  if (check_for_node(surf_node, "name")) {
52,868 ✔
75
    name_ = get_node_value(surf_node, "name", false);
12,319 ✔
76
  }
77

78
  if (check_for_node(surf_node, "boundary")) {
52,868 ✔
79
    std::string surf_bc = get_node_value(surf_node, "boundary", true, true);
30,136 ✔
80

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

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

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

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

112
      bc_->set_albedo(surf_alb);
90 ✔
113
    }
114
  }
30,136 ✔
115
}
52,868 ✔
116

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

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

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

139
  // Reflect direction according to normal.
140
  return u.reflect(n);
983,466,602 ✔
141
}
142

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

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

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

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

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

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

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

173
    if (bc_) {
41,531 ✔
174
      write_string(surf_group, "boundary_type", bc_->type(), false);
25,065 ✔
175
      bc_->to_hdf5(surf_group);
25,065 ✔
176

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

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

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

198
  to_hdf5_inner(surf_group);
42,165 ✔
199

200
  close_group(surf_group);
42,165 ✔
201
}
42,165 ✔
202

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

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

221
//==============================================================================
222
// SurfaceXPlane implementation
223
//==============================================================================
224

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

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

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

240
Direction SurfaceXPlane::normal(Position r) const
375,912,080 ✔
241
{
242
  return {1., 0., 0.};
375,912,080 ✔
243
}
244

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

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

NEW
261
bool SurfaceXPlane::triso_in_mesh(
×
262
  vector<double> mesh_center, vector<double> lattice_pitch) const
263
{
NEW
264
  return false;
×
265
}
266

267
//==============================================================================
268
// SurfaceYPlane implementation
269
//==============================================================================
NEW
270
bool SurfaceYPlane::triso_in_mesh(
×
271
  vector<double> mesh_center, vector<double> lattice_pitch) const
272
{
NEW
273
  return false;
×
274
}
275
SurfaceYPlane::SurfaceYPlane(pugi::xml_node surf_node) : Surface(surf_node)
11,464 ✔
276
{
277
  read_coeffs(surf_node, id_, {&y0_});
11,464 ✔
278
}
11,464 ✔
279

280
double SurfaceYPlane::evaluate(Position r) const
2,147,483,647 ✔
281
{
282
  return r.y - y0_;
2,147,483,647 ✔
283
}
284

285
double SurfaceYPlane::distance(Position r, Direction u, bool coincident) const
2,147,483,647 ✔
286
{
287
  return axis_aligned_plane_distance<1>(r, u, coincident, y0_);
2,147,483,647 ✔
288
}
289

290
Direction SurfaceYPlane::normal(Position r) const
525,851,261 ✔
291
{
292
  return {0., 1., 0.};
525,851,261 ✔
293
}
294

295
void SurfaceYPlane::to_hdf5_inner(hid_t group_id) const
9,592 ✔
296
{
297
  write_string(group_id, "type", "y-plane", false);
9,592 ✔
298
  array<double, 1> coeffs {{y0_}};
9,592 ✔
299
  write_dataset(group_id, "coefficients", coeffs);
9,592 ✔
300
}
9,592 ✔
301

302
BoundingBox SurfaceYPlane::bounding_box(bool pos_side) const
253 ✔
303
{
304
  if (pos_side) {
253 ✔
305
    return {{-INFTY, y0_, -INFTY}, {INFTY, INFTY, INFTY}};
121 ✔
306
  } else {
307
    return {{-INFTY, -INFTY, -INFTY}, {INFTY, y0_, INFTY}};
132 ✔
308
  }
309
}
310

311
//==============================================================================
312
// SurfaceZPlane implementation
313
//==============================================================================
NEW
314
bool SurfaceZPlane::triso_in_mesh(
×
315
  vector<double> mesh_center, vector<double> lattice_pitch) const
316
{
NEW
317
  return false;
×
318
}
319
SurfaceZPlane::SurfaceZPlane(pugi::xml_node surf_node) : Surface(surf_node)
8,490 ✔
320
{
321
  read_coeffs(surf_node, id_, {&z0_});
8,490 ✔
322
}
8,490 ✔
323

324
double SurfaceZPlane::evaluate(Position r) const
2,147,483,647 ✔
325
{
326
  return r.z - z0_;
2,147,483,647 ✔
327
}
328

329
double SurfaceZPlane::distance(Position r, Direction u, bool coincident) const
2,147,483,647 ✔
330
{
331
  return axis_aligned_plane_distance<2>(r, u, coincident, z0_);
2,147,483,647 ✔
332
}
333

334
Direction SurfaceZPlane::normal(Position r) const
262,075,739 ✔
335
{
336
  return {0., 0., 1.};
262,075,739 ✔
337
}
338

339
void SurfaceZPlane::to_hdf5_inner(hid_t group_id) const
7,332 ✔
340
{
341
  write_string(group_id, "type", "z-plane", false);
7,332 ✔
342
  array<double, 1> coeffs {{z0_}};
7,332 ✔
343
  write_dataset(group_id, "coefficients", coeffs);
7,332 ✔
344
}
7,332 ✔
345

346
BoundingBox SurfaceZPlane::bounding_box(bool pos_side) const
×
347
{
348
  if (pos_side) {
×
349
    return {{-INFTY, -INFTY, z0_}, {INFTY, INFTY, INFTY}};
×
350
  } else {
351
    return {{-INFTY, -INFTY, -INFTY}, {INFTY, INFTY, z0_}};
×
352
  }
353
}
354

355
//==============================================================================
356
// SurfacePlane implementation
357
//==============================================================================
NEW
358
bool SurfacePlane::triso_in_mesh(
×
359
  vector<double> mesh_center, vector<double> lattice_pitch) const
360
{
NEW
361
  return false;
×
362
}
363

364
SurfacePlane::SurfacePlane(pugi::xml_node surf_node) : Surface(surf_node)
2,183 ✔
365
{
366
  read_coeffs(surf_node, id_, {&A_, &B_, &C_, &D_});
2,183 ✔
367
}
2,183 ✔
368

369
double SurfacePlane::evaluate(Position r) const
362,218,824 ✔
370
{
371
  return A_ * r.x + B_ * r.y + C_ * r.z - D_;
362,218,824 ✔
372
}
373

374
BoundingBox SurfacePlane::bounding_box(bool pos_side) const
154 ✔
375
{
376
  // A general plane bounds a half-space in one direction only when its normal
377
  // is parallel to a coordinate axis; otherwise both half-spaces are unbounded
378
  // along every axis. This mirrors PlaneMixin.bounding_box on the Python side,
379
  // so that a plane whose off-axis coefficients are rotation-matrix roundoff
380
  // (e.g. B = 1 with A = C = 6.1e-17) yields the same box through both APIs.
381
  const Direction n = normal({});
154 ✔
382
  const double norm = n.norm();
154 ✔
383
  if (norm == 0.0)
154 ✔
384
    return {};
11 ✔
385

386
  int axis = -1;
396 ✔
387
  for (int i = 0; i < 3; ++i) {
396 ✔
388
    if (std::abs(std::abs(n[i] / norm) - 1.0) > PLANE_ALIGNMENT_TOL)
341 ✔
389
      continue;
231 ✔
390

391
    bool aligned = true;
396 ✔
392
    for (int j = 0; j < 3; ++j) {
396 ✔
393
      if (j != i && std::abs(n[j] / norm) > PLANE_ALIGNMENT_TOL) {
308 ✔
394
        aligned = false;
395
        break;
396
      }
397
    }
398
    if (aligned) {
110 ✔
399
      axis = i;
400
      break;
401
    }
402
  }
403
  if (axis == -1)
143 ✔
404
    return {};
55 ✔
405

406
  // The half-space is bounded below when the outward normal points along the
407
  // positive axis direction and we are on the positive side, or vice versa.
408
  BoundingBox bbox;
88 ✔
409
  const double intercept = D_ / n[axis];
88 ✔
410
  if (pos_side == (n[axis] > 0.0)) {
88 ✔
411
    bbox.min[axis] = intercept;
55 ✔
412
  } else {
413
    bbox.max[axis] = intercept;
33 ✔
414
  }
415
  return bbox;
88 ✔
416
}
417

418
double SurfacePlane::distance(Position r, Direction u, bool coincident) const
820,808,654 ✔
419
{
420
  const double f = A_ * r.x + B_ * r.y + C_ * r.z - D_;
820,808,654 ✔
421
  const double projection = A_ * u.x + B_ * u.y + C_ * u.z;
820,808,654 ✔
422
  if (coincident || std::abs(f) < FP_COINCIDENT || projection == 0.0) {
820,808,654 !
423
    return INFTY;
424
  } else {
425
    const double d = -f / projection;
753,018,759 ✔
426
    if (d < 0.0)
753,018,759 ✔
427
      return INFTY;
428
    return d;
410,394,498 ✔
429
  }
430
}
431

432
Direction SurfacePlane::normal(Position r) const
5,748,768 ✔
433
{
434
  return {A_, B_, C_};
5,748,768 ✔
435
}
436

437
void SurfacePlane::to_hdf5_inner(hid_t group_id) const
1,579 ✔
438
{
439
  write_string(group_id, "type", "plane", false);
1,579 ✔
440
  array<double, 4> coeffs {{A_, B_, C_, D_}};
1,579 ✔
441
  write_dataset(group_id, "coefficients", coeffs);
1,579 ✔
442
}
1,579 ✔
443

444
//==============================================================================
445
// Generic functions for x-, y-, and z-, cylinders
446
//==============================================================================
447

448
// The template parameters indicate the axes perpendicular to the axis of the
449
// cylinder.  offset1 and offset2 should correspond with i1 and i2,
450
// respectively.
451
template<int i1, int i2>
452
double axis_aligned_cylinder_evaluate(
2,147,483,647 ✔
453
  Position r, double offset1, double offset2, double radius)
454
{
455
  const double r1 = r.get<i1>() - offset1;
2,147,483,647 ✔
456
  const double r2 = r.get<i2>() - offset2;
2,147,483,647 ✔
457
  return r1 * r1 + r2 * r2 - radius * radius;
2,147,483,647 ✔
458
}
459

460
// The first template parameter indicates which axis the cylinder is aligned to.
461
// The other two parameters indicate the other two axes.  offset1 and offset2
462
// should correspond with i2 and i3, respectively.
463
template<int i1, int i2, int i3>
464
double axis_aligned_cylinder_distance(Position r, Direction u, bool coincident,
2,147,483,647 ✔
465
  double offset1, double offset2, double radius)
466
{
467
  const double a = 1.0 - u.get<i1>() * u.get<i1>(); // u^2 + v^2
2,147,483,647 ✔
468
  if (a == 0.0)
2,147,483,647 ✔
469
    return INFTY;
470

471
  const double r2 = r.get<i2>() - offset1;
2,147,483,647 ✔
472
  const double r3 = r.get<i3>() - offset2;
2,147,483,647 ✔
473
  const double k = r2 * u.get<i2>() + r3 * u.get<i3>();
2,147,483,647 ✔
474
  const double c = r2 * r2 + r3 * r3 - radius * radius;
2,147,483,647 ✔
475
  const double quad = k * k - a * c;
2,147,483,647 ✔
476

477
  if (quad < 0.0) {
2,147,483,647 ✔
478
    // No intersection with cylinder.
479
    return INFTY;
480

481
  } else if (coincident || std::abs(c) < FP_COINCIDENT) {
2,147,483,647 ✔
482
    // Particle is on the cylinder, thus one distance is positive/negative
483
    // and the other is zero. The sign of k determines if we are facing in or
484
    // out.
485
    if (k >= 0.0) {
1,908,142,852 ✔
486
      return INFTY;
487
    } else {
488
      return (-k + sqrt(quad)) / a;
1,090,364,539 ✔
489
    }
490

491
  } else if (c < 0.0) {
2,147,483,647 ✔
492
    // Particle is inside the cylinder, thus one distance must be negative
493
    // and one must be positive. The positive distance will be the one with
494
    // negative sign on sqrt(quad).
495
    return (-k + sqrt(quad)) / a;
1,077,367,071 ✔
496

497
  } else {
498
    // Particle is outside the cylinder, thus both distances are either
499
    // positive or negative. If positive, the smaller distance is the one
500
    // with positive sign on sqrt(quad).
501
    const double d = (-k - sqrt(quad)) / a;
1,466,754,116 ✔
502
    if (d < 0.0)
1,466,754,116 ✔
503
      return INFTY;
504
    return d;
1,142,974,748 ✔
505
  }
506
}
507

508
// The first template parameter indicates which axis the cylinder is aligned to.
509
// The other two parameters indicate the other two axes.  offset1 and offset2
510
// should correspond with i2 and i3, respectively.
511
template<int i1, int i2, int i3>
512
Direction axis_aligned_cylinder_normal(
281,110,479 ✔
513
  Position r, double offset1, double offset2)
514
{
515
  Direction u;
281,110,479 ✔
516
  u.get<i2>() = 2.0 * (r.get<i2>() - offset1);
281,110,479 ✔
517
  u.get<i3>() = 2.0 * (r.get<i3>() - offset2);
281,110,479 ✔
518
  u.get<i1>() = 0.0;
519
  return u;
520
}
521

522
//==============================================================================
523
// SurfaceXCylinder implementation
524
//==============================================================================
525

NEW
526
bool SurfaceXCylinder::triso_in_mesh(
×
527
  vector<double> mesh_center, vector<double> lattice_pitch) const
528
{
NEW
529
  return false;
×
530
}
531

532
SurfaceXCylinder::SurfaceXCylinder(pugi::xml_node surf_node)
45 ✔
533
  : Surface(surf_node)
45 ✔
534
{
535
  read_coeffs(surf_node, id_, {&y0_, &z0_, &radius_});
45 ✔
536
}
45 ✔
537

538
double SurfaceXCylinder::evaluate(Position r) const
1,277,479 ✔
539
{
540
  return axis_aligned_cylinder_evaluate<1, 2>(r, y0_, z0_, radius_);
1,277,479 ✔
541
}
542

543
double SurfaceXCylinder::distance(
1,622,038 ✔
544
  Position r, Direction u, bool coincident) const
545
{
546
  return axis_aligned_cylinder_distance<0, 1, 2>(
1,622,038 ✔
547
    r, u, coincident, y0_, z0_, radius_);
1,622,038 ✔
548
}
549

550
Direction SurfaceXCylinder::normal(Position r) const
397,936 ✔
551
{
552
  return axis_aligned_cylinder_normal<0, 1, 2>(r, y0_, z0_);
397,936 ✔
553
}
554

555
void SurfaceXCylinder::to_hdf5_inner(hid_t group_id) const
33 ✔
556
{
557
  write_string(group_id, "type", "x-cylinder", false);
33 ✔
558
  array<double, 3> coeffs {{y0_, z0_, radius_}};
33 ✔
559
  write_dataset(group_id, "coefficients", coeffs);
33 ✔
560
}
33 ✔
561

562
BoundingBox SurfaceXCylinder::bounding_box(bool pos_side) const
×
563
{
564
  if (!pos_side) {
×
565
    return {{-INFTY, y0_ - radius_, z0_ - radius_},
×
566
      {INFTY, y0_ + radius_, z0_ + radius_}};
×
567
  } else {
568
    return {};
×
569
  }
570
}
571
//==============================================================================
572
// SurfaceYCylinder implementation
573
//==============================================================================
574

NEW
575
bool SurfaceYCylinder::triso_in_mesh(
×
576
  vector<double> mesh_center, vector<double> lattice_pitch) const
577
{
NEW
578
  return false;
×
579
}
580

581
SurfaceYCylinder::SurfaceYCylinder(pugi::xml_node surf_node)
26 ✔
582
  : Surface(surf_node)
26 ✔
583
{
584
  read_coeffs(surf_node, id_, {&x0_, &z0_, &radius_});
26 ✔
585
}
26 ✔
586

587
double SurfaceYCylinder::evaluate(Position r) const
170,984 ✔
588
{
589
  return axis_aligned_cylinder_evaluate<0, 2>(r, x0_, z0_, radius_);
170,984 ✔
590
}
591

592
double SurfaceYCylinder::distance(
659,769 ✔
593
  Position r, Direction u, bool coincident) const
594
{
595
  return axis_aligned_cylinder_distance<1, 0, 2>(
659,769 ✔
596
    r, u, coincident, x0_, z0_, radius_);
659,769 ✔
597
}
598

599
Direction SurfaceYCylinder::normal(Position r) const
×
600
{
601
  return axis_aligned_cylinder_normal<1, 0, 2>(r, x0_, z0_);
×
602
}
603

604
void SurfaceYCylinder::to_hdf5_inner(hid_t group_id) const
22 ✔
605
{
606
  write_string(group_id, "type", "y-cylinder", false);
22 ✔
607
  array<double, 3> coeffs {{x0_, z0_, radius_}};
22 ✔
608
  write_dataset(group_id, "coefficients", coeffs);
22 ✔
609
}
22 ✔
610

611
BoundingBox SurfaceYCylinder::bounding_box(bool pos_side) const
11 ✔
612
{
613
  if (!pos_side) {
11 !
614
    return {{x0_ - radius_, -INFTY, z0_ - radius_},
11 ✔
615
      {x0_ + radius_, INFTY, z0_ + radius_}};
11 ✔
616
  } else {
617
    return {};
×
618
  }
619
}
620

621
//==============================================================================
622
// SurfaceZCylinder implementation
623
//==============================================================================
624

NEW
625
bool SurfaceZCylinder::triso_in_mesh(
×
626
  vector<double> mesh_center, vector<double> lattice_pitch) const
627
{
NEW
628
  return false;
×
629
}
630

631
SurfaceZCylinder::SurfaceZCylinder(pugi::xml_node surf_node)
5,818 ✔
632
  : Surface(surf_node)
5,818 ✔
633
{
634
  read_coeffs(surf_node, id_, {&x0_, &y0_, &radius_});
5,818 ✔
635
}
5,818 ✔
636

637
double SurfaceZCylinder::evaluate(Position r) const
2,147,483,647 ✔
638
{
639
  return axis_aligned_cylinder_evaluate<0, 1>(r, x0_, y0_, radius_);
2,147,483,647 ✔
640
}
641

642
double SurfaceZCylinder::distance(
2,147,483,647 ✔
643
  Position r, Direction u, bool coincident) const
644
{
645
  return axis_aligned_cylinder_distance<2, 0, 1>(
2,147,483,647 ✔
646
    r, u, coincident, x0_, y0_, radius_);
2,147,483,647 ✔
647
}
648

649
Direction SurfaceZCylinder::normal(Position r) const
280,712,543 ✔
650
{
651
  return axis_aligned_cylinder_normal<2, 0, 1>(r, x0_, y0_);
280,712,543 ✔
652
}
653

654
void SurfaceZCylinder::to_hdf5_inner(hid_t group_id) const
4,596 ✔
655
{
656
  write_string(group_id, "type", "z-cylinder", false);
4,596 ✔
657
  array<double, 3> coeffs {{x0_, y0_, radius_}};
4,596 ✔
658
  write_dataset(group_id, "coefficients", coeffs);
4,596 ✔
659
}
4,596 ✔
660

661
BoundingBox SurfaceZCylinder::bounding_box(bool pos_side) const
44 ✔
662
{
663
  if (!pos_side) {
44 ✔
664
    return {{x0_ - radius_, y0_ - radius_, -INFTY},
22 ✔
665
      {x0_ + radius_, y0_ + radius_, INFTY}};
22 ✔
666
  } else {
667
    return {};
22 ✔
668
  }
669
}
670

671
//==============================================================================
672
// SurfaceSphere implementation
673
//==============================================================================
674

675
SurfaceSphere::SurfaceSphere(pugi::xml_node surf_node) : Surface(surf_node)
10,977 ✔
676
{
677
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &radius_});
10,977 ✔
678
}
10,977 ✔
679

680
double SurfaceSphere::evaluate(Position r) const
1,272,638,737 ✔
681
{
682
  const double x = r.x - x0_;
1,272,638,737 ✔
683
  const double y = r.y - y0_;
1,272,638,737 ✔
684
  const double z = r.z - z0_;
1,272,638,737 ✔
685
  return x * x + y * y + z * z - radius_ * radius_;
1,272,638,737 ✔
686
}
687

688
double SurfaceSphere::distance(Position r, Direction u, bool coincident) const
2,147,483,647 ✔
689
{
690
  const double x = r.x - x0_;
2,147,483,647 ✔
691
  const double y = r.y - y0_;
2,147,483,647 ✔
692
  const double z = r.z - z0_;
2,147,483,647 ✔
693
  const double k = x * u.x + y * u.y + z * u.z;
2,147,483,647 ✔
694
  const double c = x * x + y * y + z * z - radius_ * radius_;
2,147,483,647 ✔
695
  const double quad = k * k - c;
2,147,483,647 ✔
696

697
  if (quad < 0.0) {
2,147,483,647 ✔
698
    // No intersection with sphere.
699
    return INFTY;
700

701
  } else if (coincident || std::abs(c) < FP_COINCIDENT) {
1,967,218,010 !
702
    // Particle is on the sphere, thus one distance is positive/negative and
703
    // the other is zero. The sign of k determines if we are facing in or out.
704
    if (k >= 0.0) {
125,574,372 ✔
705
      return INFTY;
706
    } else {
707
      return -k + sqrt(quad);
70,972,211 ✔
708
    }
709

710
  } else if (c < 0.0) {
1,841,643,638 ✔
711
    // Particle is inside the sphere, thus one distance must be negative and
712
    // one must be positive. The positive distance will be the one with
713
    // negative sign on sqrt(quad)
714
    return -k + sqrt(quad);
1,769,685,827 ✔
715

716
  } else {
717
    // Particle is outside the sphere, thus both distances are either positive
718
    // or negative. If positive, the smaller distance is the one with positive
719
    // sign on sqrt(quad).
720
    const double d = -k - sqrt(quad);
71,957,811 ✔
721
    if (d < 0.0)
71,957,811 ✔
722
      return INFTY;
723
    return d;
57,023,010 ✔
724
  }
725
}
726

727
Direction SurfaceSphere::normal(Position r) const
389,726,810 ✔
728
{
729
  return {2.0 * (r.x - x0_), 2.0 * (r.y - y0_), 2.0 * (r.z - z0_)};
389,726,810 ✔
730
}
731

732
void SurfaceSphere::to_hdf5_inner(hid_t group_id) const
7,742 ✔
733
{
734
  write_string(group_id, "type", "sphere", false);
7,742 ✔
735
  array<double, 4> coeffs {{x0_, y0_, z0_, radius_}};
7,742 ✔
736
  write_dataset(group_id, "coefficients", coeffs);
7,742 ✔
737
}
7,742 ✔
738

739
BoundingBox SurfaceSphere::bounding_box(bool pos_side) const
×
740
{
741
  if (!pos_side) {
×
742
    return {{x0_ - radius_, y0_ - radius_, z0_ - radius_},
×
743
      {x0_ + radius_, y0_ + radius_, z0_ + radius_}};
×
744
  } else {
745
    return {};
×
746
  }
747
}
748

749
void SurfaceSphere::connect_to_triso_base(int triso_index, std::string key)
3,915 ✔
750
{
751
  if (key == "base") {
3,915 ✔
752
    triso_base_index_ = triso_index;
2,415 ✔
753
    is_triso_surface_ = true;
2,415 ✔
754
  } else if (key == "particle") {
1,500 !
755
    triso_particle_index_ = triso_index;
1,500 ✔
756
  }
757
}
3,915 ✔
758

759
vector<double> SurfaceSphere::get_center() const
18,015,551 ✔
760
{
761
  return {x0_, y0_, z0_};
18,015,551 ✔
762
}
763

764
double SurfaceSphere::get_radius() const
18,014,051 ✔
765
{
766
  return radius_;
18,014,051 ✔
767
}
768

769
bool SurfaceSphere::triso_in_mesh(
19,140 ✔
770
  vector<double> mesh_center, vector<double> lattice_pitch) const
771
{
772
  double dis_x;
19,140 ✔
773
  double dis_y;
19,140 ✔
774
  double dis_z;
19,140 ✔
775
  double x_min = mesh_center[0] - lattice_pitch[0] / 2;
19,140 ✔
776
  double x_max = mesh_center[0] + lattice_pitch[0] / 2;
19,140 ✔
777
  double y_min = mesh_center[1] - lattice_pitch[1] / 2;
19,140 ✔
778
  double y_max = mesh_center[1] + lattice_pitch[1] / 2;
19,140 ✔
779
  double z_min = mesh_center[2] - lattice_pitch[2] / 2;
19,140 ✔
780
  double z_max = mesh_center[2] + lattice_pitch[2] / 2;
19,140 ✔
781
  if (x0_ >= x_min && x0_ <= x_max) {
19,140 ✔
782
    dis_x = 0;
783
  } else if (x0_ < x_min) {
11,010 ✔
784
    dis_x = pow(x_min - x0_, 2);
5,820 ✔
785
  } else {
786
    dis_x = pow(x_max - x0_, 2);
5,190 ✔
787
  }
788

789
  if (y0_ >= y_min && y0_ <= y_max) {
19,140 ✔
790
    dis_y = 0;
791
  } else if (y0_ < y_min) {
10,890 ✔
792
    dis_y = pow(y_min - y0_, 2);
5,280 ✔
793
  } else {
794
    dis_y = pow(y_max - y0_, 2);
5,610 ✔
795
  }
796

797
  if (z0_ >= z_min && z0_ <= z_max) {
19,140 ✔
798
    dis_z = 0;
799
  } else if (z0_ < z_min) {
10,950 ✔
800
    dis_z = pow(z_min - z0_, 2);
5,955 ✔
801
  } else {
802
    dis_z = pow(z_max - z0_, 2);
4,995 ✔
803
  }
804

805
  if (sqrt(dis_x + dis_y + dis_z) < radius_) {
19,140 ✔
806
    return true;
807
  } else {
808
    return false;
16,725 ✔
809
  }
810
}
811

812
//==============================================================================
813
// Generic functions for x-, y-, and z-, cones
814
//==============================================================================
815

816
// The first template parameter indicates which axis the cone is aligned to.
817
// The other two parameters indicate the other two axes.  offset1, offset2,
818
// and offset3 should correspond with i1, i2, and i3, respectively.
819
template<int i1, int i2, int i3>
820
double axis_aligned_cone_evaluate(
325,853 ✔
821
  Position r, double offset1, double offset2, double offset3, double radius_sq)
822
{
823
  const double r1 = r.get<i1>() - offset1;
325,853 ✔
824
  const double r2 = r.get<i2>() - offset2;
325,853 ✔
825
  const double r3 = r.get<i3>() - offset3;
325,853 ✔
826
  return r2 * r2 + r3 * r3 - radius_sq * r1 * r1;
325,853 ✔
827
}
828

829
// The first template parameter indicates which axis the cone is aligned to.
830
// The other two parameters indicate the other two axes.  offset1, offset2,
831
// and offset3 should correspond with i1, i2, and i3, respectively.
832
template<int i1, int i2, int i3>
833
double axis_aligned_cone_distance(Position r, Direction u, bool coincident,
978,021 ✔
834
  double offset1, double offset2, double offset3, double radius_sq)
835
{
836
  const double r1 = r.get<i1>() - offset1;
978,021 ✔
837
  const double r2 = r.get<i2>() - offset2;
978,021 ✔
838
  const double r3 = r.get<i3>() - offset3;
978,021 ✔
839
  const double a = u.get<i2>() * u.get<i2>() + u.get<i3>() * u.get<i3>() -
978,021 ✔
840
                   radius_sq * u.get<i1>() * u.get<i1>();
978,021 ✔
841
  const double k =
978,021 ✔
842
    r2 * u.get<i2>() + r3 * u.get<i3>() - radius_sq * r1 * u.get<i1>();
978,021 ✔
843
  const double c = r2 * r2 + r3 * r3 - radius_sq * r1 * r1;
978,021 ✔
844
  double quad = k * k - a * c;
978,021 ✔
845

846
  double d;
847

848
  if (quad < 0.0) {
978,021 !
849
    // No intersection with cone.
850
    return INFTY;
851

852
  } else if (coincident || std::abs(c) < FP_COINCIDENT) {
978,021 !
853
    // Particle is on the cone, thus one distance is positive/negative
854
    // and the other is zero. The sign of k determines if we are facing in or
855
    // out.
856
    if (k >= 0.0) {
59,510 !
857
      d = (-k - sqrt(quad)) / a;
×
858
    } else {
859
      d = (-k + sqrt(quad)) / a;
59,510 ✔
860
    }
861

862
  } else {
863
    // Calculate both solutions to the quadratic.
864
    quad = sqrt(quad);
918,511 ✔
865
    d = (-k - quad) / a;
918,511 ✔
866
    const double b = (-k + quad) / a;
918,511 ✔
867

868
    // Determine the smallest positive solution.
869
    if (d < 0.0) {
918,511 ✔
870
      if (b > 0.0)
780,043 ✔
871
        d = b;
665,104 ✔
872
    } else {
873
      if (b > 0.0) {
138,468 !
874
        if (b < d)
138,468 !
875
          d = b;
138,468 ✔
876
      }
877
    }
878
  }
879

880
  // If the distance was negative, set boundary distance to infinity.
881
  if (d <= 0.0)
978,021 ✔
882
    return INFTY;
136,422 ✔
883
  return d;
884
}
885

886
// The first template parameter indicates which axis the cone is aligned to.
887
// The other two parameters indicate the other two axes.  offset1, offset2,
888
// and offset3 should correspond with i1, i2, and i3, respectively.
889
template<int i1, int i2, int i3>
890
Direction axis_aligned_cone_normal(
59,510 ✔
891
  Position r, double offset1, double offset2, double offset3, double radius_sq)
892
{
893
  Direction u;
894
  u.get<i1>() = -2.0 * radius_sq * (r.get<i1>() - offset1);
59,510 ✔
895
  u.get<i2>() = 2.0 * (r.get<i2>() - offset2);
59,510 ✔
896
  u.get<i3>() = 2.0 * (r.get<i3>() - offset3);
59,510 ✔
897
  return u;
898
}
899

900
//==============================================================================
901
// SurfaceXCone implementation
902
//==============================================================================
NEW
903
bool SurfaceXCone::triso_in_mesh(
×
904
  vector<double> mesh_center, vector<double> lattice_pitch) const
905
{
NEW
906
  return false;
×
907
}
UNCOV
908
SurfaceXCone::SurfaceXCone(pugi::xml_node surf_node) : Surface(surf_node)
×
909
{
910
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &radius_sq_});
×
911
}
×
912

913
double SurfaceXCone::evaluate(Position r) const
×
914
{
915
  return axis_aligned_cone_evaluate<0, 1, 2>(r, x0_, y0_, z0_, radius_sq_);
×
916
}
917

918
double SurfaceXCone::distance(Position r, Direction u, bool coincident) const
×
919
{
920
  return axis_aligned_cone_distance<0, 1, 2>(
×
921
    r, u, coincident, x0_, y0_, z0_, radius_sq_);
×
922
}
923

924
Direction SurfaceXCone::normal(Position r) const
×
925
{
926
  return axis_aligned_cone_normal<0, 1, 2>(r, x0_, y0_, z0_, radius_sq_);
×
927
}
928

929
void SurfaceXCone::to_hdf5_inner(hid_t group_id) const
×
930
{
931
  write_string(group_id, "type", "x-cone", false);
×
932
  array<double, 4> coeffs {{x0_, y0_, z0_, radius_sq_}};
×
933
  write_dataset(group_id, "coefficients", coeffs);
×
934
}
×
935

936
//==============================================================================
937
// SurfaceYCone implementation
938
//==============================================================================
NEW
939
bool SurfaceYCone::triso_in_mesh(
×
940
  vector<double> mesh_center, vector<double> lattice_pitch) const
941
{
NEW
942
  return false;
×
943
}
UNCOV
944
SurfaceYCone::SurfaceYCone(pugi::xml_node surf_node) : Surface(surf_node)
×
945
{
946
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &radius_sq_});
×
947
}
×
948

949
double SurfaceYCone::evaluate(Position r) const
×
950
{
951
  return axis_aligned_cone_evaluate<1, 0, 2>(r, y0_, x0_, z0_, radius_sq_);
×
952
}
953

954
double SurfaceYCone::distance(Position r, Direction u, bool coincident) const
×
955
{
956
  return axis_aligned_cone_distance<1, 0, 2>(
×
957
    r, u, coincident, y0_, x0_, z0_, radius_sq_);
×
958
}
959

960
Direction SurfaceYCone::normal(Position r) const
×
961
{
962
  return axis_aligned_cone_normal<1, 0, 2>(r, y0_, x0_, z0_, radius_sq_);
×
963
}
964

965
void SurfaceYCone::to_hdf5_inner(hid_t group_id) const
×
966
{
967
  write_string(group_id, "type", "y-cone", false);
×
968
  array<double, 4> coeffs {{x0_, y0_, z0_, radius_sq_}};
×
969
  write_dataset(group_id, "coefficients", coeffs);
×
970
}
×
971

972
//==============================================================================
973
// SurfaceZCone implementation
974
//==============================================================================
NEW
975
bool SurfaceZCone::triso_in_mesh(
×
976
  vector<double> mesh_center, vector<double> lattice_pitch) const
977
{
NEW
978
  return false;
×
979
}
980

981
SurfaceZCone::SurfaceZCone(pugi::xml_node surf_node) : Surface(surf_node)
15 ✔
982
{
983
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &radius_sq_});
15 ✔
984
}
15 ✔
985

986
double SurfaceZCone::evaluate(Position r) const
325,853 ✔
987
{
988
  return axis_aligned_cone_evaluate<2, 0, 1>(r, z0_, x0_, y0_, radius_sq_);
325,853 ✔
989
}
990

991
double SurfaceZCone::distance(Position r, Direction u, bool coincident) const
978,021 ✔
992
{
993
  return axis_aligned_cone_distance<2, 0, 1>(
978,021 ✔
994
    r, u, coincident, z0_, x0_, y0_, radius_sq_);
978,021 ✔
995
}
996

997
Direction SurfaceZCone::normal(Position r) const
59,510 ✔
998
{
999
  return axis_aligned_cone_normal<2, 0, 1>(r, z0_, x0_, y0_, radius_sq_);
59,510 ✔
1000
}
1001

1002
void SurfaceZCone::to_hdf5_inner(hid_t group_id) const
11 ✔
1003
{
1004
  write_string(group_id, "type", "z-cone", false);
11 ✔
1005
  array<double, 4> coeffs {{x0_, y0_, z0_, radius_sq_}};
11 ✔
1006
  write_dataset(group_id, "coefficients", coeffs);
11 ✔
1007
}
11 ✔
1008

1009
//==============================================================================
1010
// SurfaceQuadric implementation
1011
//==============================================================================
NEW
1012
bool SurfaceQuadric::triso_in_mesh(
×
1013
  vector<double> mesh_center, vector<double> lattice_pitch) const
1014
{
NEW
1015
  return false;
×
1016
}
1017
SurfaceQuadric::SurfaceQuadric(pugi::xml_node surf_node) : Surface(surf_node)
26 ✔
1018
{
1019
  read_coeffs(
52 ✔
1020
    surf_node, id_, {&A_, &B_, &C_, &D_, &E_, &F_, &G_, &H_, &J_, &K_});
26 ✔
1021
}
26 ✔
1022

1023
double SurfaceQuadric::evaluate(Position r) const
177,411 ✔
1024
{
1025
  const double x = r.x;
177,411 ✔
1026
  const double y = r.y;
177,411 ✔
1027
  const double z = r.z;
177,411 ✔
1028
  return x * (A_ * x + D_ * y + G_) + y * (B_ * y + E_ * z + H_) +
177,411 ✔
1029
         z * (C_ * z + F_ * x + J_) + K_;
177,411 ✔
1030
}
1031

1032
double SurfaceQuadric::distance(
316,690 ✔
1033
  Position r, Direction ang, bool coincident) const
1034
{
1035
  const double& x = r.x;
316,690 ✔
1036
  const double& y = r.y;
316,690 ✔
1037
  const double& z = r.z;
316,690 ✔
1038
  const double& u = ang.x;
316,690 ✔
1039
  const double& v = ang.y;
316,690 ✔
1040
  const double& w = ang.z;
316,690 ✔
1041

1042
  const double a =
316,690 ✔
1043
    A_ * u * u + B_ * v * v + C_ * w * w + D_ * u * v + E_ * v * w + F_ * u * w;
316,690 ✔
1044
  const double k = A_ * u * x + B_ * v * y + C_ * w * z +
316,690 ✔
1045
                   0.5 * (D_ * (u * y + v * x) + E_ * (v * z + w * y) +
316,690 ✔
1046
                           F_ * (w * x + u * z) + G_ * u + H_ * v + J_ * w);
316,690 ✔
1047
  const double c = A_ * x * x + B_ * y * y + C_ * z * z + D_ * x * y +
316,690 ✔
1048
                   E_ * y * z + F_ * x * z + G_ * x + H_ * y + J_ * z + K_;
316,690 ✔
1049
  double quad = k * k - a * c;
316,690 ✔
1050

1051
  double d;
316,690 ✔
1052

1053
  if (quad < 0.0) {
316,690 !
1054
    // No intersection with surface.
1055
    return INFTY;
1056

1057
  } else if (coincident || std::abs(c) < FP_COINCIDENT) {
316,690 !
1058
    // Particle is on the surface, thus one distance is positive/negative and
1059
    // the other is zero. The sign of k determines which distance is zero and
1060
    // which is not. Additionally, if a is zero, it means the particle is on
1061
    // a plane-like surface.
1062
    if (a == 0.0) {
36,058 !
1063
      d = INFTY; // see the below explanation
1064
    } else if (k >= 0.0) {
36,058 !
1065
      d = (-k - sqrt(quad)) / a;
×
1066
    } else {
1067
      d = (-k + sqrt(quad)) / a;
36,058 ✔
1068
    }
1069

1070
  } else if (a == 0.0) {
280,632 !
1071
    // Given the orientation of the particle, the quadric looks like a plane in
1072
    // this case, and thus we have only one solution despite potentially having
1073
    // quad > 0.0. While the term under the square root may be real, in one
1074
    // case of the +/- of the quadratic formula, 0/0 results, and in another, a
1075
    // finite value over 0 results. Applying L'Hopital's to the 0/0 case gives
1076
    // the below. Alternatively this can be found by simply putting a=0 in the
1077
    // equation ax^2 + bx + c = 0.
1078
    d = -0.5 * c / k;
×
1079
  } else {
1080
    // Calculate both solutions to the quadratic.
1081
    quad = sqrt(quad);
280,632 ✔
1082
    d = (-k - quad) / a;
280,632 ✔
1083
    double b = (-k + quad) / a;
280,632 ✔
1084

1085
    // Determine the smallest positive solution.
1086
    if (d < 0.0) {
280,632 ✔
1087
      if (b > 0.0)
280,621 !
1088
        d = b;
280,621 ✔
1089
    } else {
1090
      if (b > 0.0) {
11 !
1091
        if (b < d)
11 !
1092
          d = b;
×
1093
      }
1094
    }
1095
  }
1096

1097
  // If the distance was negative, set boundary distance to infinity.
1098
  if (d <= 0.0)
316,690 !
1099
    return INFTY;
×
1100
  return d;
1101
}
1102

1103
Direction SurfaceQuadric::normal(Position r) const
36,058 ✔
1104
{
1105
  const double& x = r.x;
36,058 ✔
1106
  const double& y = r.y;
36,058 ✔
1107
  const double& z = r.z;
36,058 ✔
1108
  return {2.0 * A_ * x + D_ * y + F_ * z + G_,
36,058 ✔
1109
    2.0 * B_ * y + D_ * x + E_ * z + H_, 2.0 * C_ * z + E_ * y + F_ * x + J_};
36,058 ✔
1110
}
1111

1112
void SurfaceQuadric::to_hdf5_inner(hid_t group_id) const
11 ✔
1113
{
1114
  write_string(group_id, "type", "quadric", false);
11 ✔
1115
  array<double, 10> coeffs {{A_, B_, C_, D_, E_, F_, G_, H_, J_, K_}};
11 ✔
1116
  write_dataset(group_id, "coefficients", coeffs);
11 ✔
1117
}
11 ✔
1118

1119
//==============================================================================
1120
// Torus helper functions
1121
//==============================================================================
1122

1123
double torus_distance(double x1, double x2, double x3, double u1, double u2,
26,534,563 ✔
1124
  double u3, double A, double B, double C, bool coincident)
1125
{
1126
  // Coefficients for equation: (c2 t^2 + c1 t + c0)^2 = c2' t^2 + c1' t + c0'
1127
  double D = (C * C) / (B * B);
26,534,563 ✔
1128
  double c2 = u1 * u1 + u2 * u2 + D * u3 * u3;
26,534,563 ✔
1129
  double c1 = 2 * (u1 * x1 + u2 * x2 + D * u3 * x3);
26,534,563 ✔
1130
  double c0 = x1 * x1 + x2 * x2 + D * x3 * x3 + A * A - C * C;
26,534,563 ✔
1131
  double four_A2 = 4 * A * A;
26,534,563 ✔
1132
  double c2p = four_A2 * (u1 * u1 + u2 * u2);
26,534,563 ✔
1133
  double c1p = 2 * four_A2 * (u1 * x1 + u2 * x2);
26,534,563 ✔
1134
  double c0p = four_A2 * (x1 * x1 + x2 * x2);
26,534,563 ✔
1135

1136
  // Coefficient for equation: a t^4 + b t^3 + c t^2 + d t + e = 0. If the point
1137
  // is coincident, the 'e' coefficient should be zero. Explicitly setting it to
1138
  // zero helps avoid numerical issues below with root finding.
1139
  double coeff[5];
26,534,563 ✔
1140
  coeff[0] = coincident ? 0.0 : c0 * c0 - c0p;
26,534,563 ✔
1141
  coeff[1] = 2 * c0 * c1 - c1p;
26,534,563 ✔
1142
  coeff[2] = c1 * c1 + 2 * c0 * c2 - c2p;
26,534,563 ✔
1143
  coeff[3] = 2 * c1 * c2;
26,534,563 ✔
1144
  coeff[4] = c2 * c2;
26,534,563 ✔
1145

1146
  std::complex<double> roots[4];
26,534,563 ✔
1147
  oqs::quartic_solver(coeff, roots);
26,534,563 ✔
1148

1149
  // Find smallest positive, real root. In the case where the particle is
1150
  // coincident with the surface, we are sure to have one root very close to
1151
  // zero but possibly small and positive. A tolerance is set to discard that
1152
  // zero.
1153
  double distance = INFTY;
26,534,563 ✔
1154
  double cutoff = coincident ? TORUS_TOL : 0.0;
26,534,563 ✔
1155
  for (int i = 0; i < 4; ++i) {
132,672,815 ✔
1156
    if (roots[i].imag() == 0) {
106,138,252 ✔
1157
      double root = roots[i].real();
25,944,028 ✔
1158
      if (root > cutoff && root < distance) {
25,944,028 ✔
1159
        // Avoid roots corresponding to internal surfaces
1160
        double s1 = x1 + u1 * root;
10,411,995 ✔
1161
        double s2 = x2 + u2 * root;
10,411,995 ✔
1162
        double s3 = x3 + u3 * root;
10,411,995 ✔
1163
        double check = D * s3 * s3 + s1 * s1 + s2 * s2 + A * A - C * C;
10,411,995 ✔
1164
        if (check >= 0) {
10,411,995 !
1165
          distance = root;
10,411,995 ✔
1166
        }
1167
      }
1168
    }
1169
  }
1170
  return distance;
26,534,563 ✔
1171
}
1172

1173
//==============================================================================
1174
// SurfaceXTorus implementation
1175
//==============================================================================
NEW
1176
bool SurfaceXTorus::triso_in_mesh(
×
1177
  vector<double> mesh_center, vector<double> lattice_pitch) const
1178
{
NEW
1179
  return false;
×
1180
}
1181
SurfaceXTorus::SurfaceXTorus(pugi::xml_node surf_node) : Surface(surf_node)
70 ✔
1182
{
1183
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &A_, &B_, &C_});
70 ✔
1184
}
70 ✔
1185

1186
void SurfaceXTorus::to_hdf5_inner(hid_t group_id) const
55 ✔
1187
{
1188
  write_string(group_id, "type", "x-torus", false);
55 ✔
1189
  std::array<double, 6> coeffs {{x0_, y0_, z0_, A_, B_, C_}};
55 ✔
1190
  write_dataset(group_id, "coefficients", coeffs);
55 ✔
1191
}
55 ✔
1192

1193
double SurfaceXTorus::evaluate(Position r) const
811,264 ✔
1194
{
1195
  double x = r.x - x0_;
811,264 ✔
1196
  double y = r.y - y0_;
811,264 ✔
1197
  double z = r.z - z0_;
811,264 ✔
1198
  return (x * x) / (B_ * B_) +
1,622,528 ✔
1199
         std::pow(std::sqrt(y * y + z * z) - A_, 2) / (C_ * C_) - 1.;
811,264 ✔
1200
}
1201

1202
BoundingBox SurfaceXTorus::bounding_box(bool pos_side) const
22 ✔
1203
{
1204
  // The torus interior is compact: it extends +/-B_ along the axis of
1205
  // revolution and +/-(A_ + C_) in the two perpendicular directions. Mirrors
1206
  // XTorus.bounding_box on the Python side.
1207
  if (pos_side)
22 ✔
1208
    return {};
11 ✔
1209
  return {{x0_ - B_, y0_ - A_ - C_, z0_ - A_ - C_},
11 ✔
1210
    {x0_ + B_, y0_ + A_ + C_, z0_ + A_ + C_}};
11 ✔
1211
}
1212

1213
double SurfaceXTorus::distance(Position r, Direction u, bool coincident) const
8,348,615 ✔
1214
{
1215
  double x = r.x - x0_;
8,348,615 ✔
1216
  double y = r.y - y0_;
8,348,615 ✔
1217
  double z = r.z - z0_;
8,348,615 ✔
1218
  return torus_distance(y, z, x, u.y, u.z, u.x, A_, B_, C_, coincident);
8,348,615 ✔
1219
}
1220

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

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

1241
//==============================================================================
1242
// SurfaceYTorus implementation
1243
//==============================================================================
NEW
1244
bool SurfaceYTorus::triso_in_mesh(
×
1245
  vector<double> mesh_center, vector<double> lattice_pitch) const
1246
{
NEW
1247
  return false;
×
1248
}
1249
SurfaceYTorus::SurfaceYTorus(pugi::xml_node surf_node) : Surface(surf_node)
70 ✔
1250
{
1251
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &A_, &B_, &C_});
70 ✔
1252
}
70 ✔
1253

1254
void SurfaceYTorus::to_hdf5_inner(hid_t group_id) const
55 ✔
1255
{
1256
  write_string(group_id, "type", "y-torus", false);
55 ✔
1257
  std::array<double, 6> coeffs {{x0_, y0_, z0_, A_, B_, C_}};
55 ✔
1258
  write_dataset(group_id, "coefficients", coeffs);
55 ✔
1259
}
55 ✔
1260

1261
double SurfaceYTorus::evaluate(Position r) const
780,718 ✔
1262
{
1263
  double x = r.x - x0_;
780,718 ✔
1264
  double y = r.y - y0_;
780,718 ✔
1265
  double z = r.z - z0_;
780,718 ✔
1266
  return (y * y) / (B_ * B_) +
1,561,436 ✔
1267
         std::pow(std::sqrt(x * x + z * z) - A_, 2) / (C_ * C_) - 1.;
780,718 ✔
1268
}
1269

1270
BoundingBox SurfaceYTorus::bounding_box(bool pos_side) const
22 ✔
1271
{
1272
  if (pos_side)
22 ✔
1273
    return {};
11 ✔
1274
  return {{x0_ - A_ - C_, y0_ - B_, z0_ - A_ - C_},
11 ✔
1275
    {x0_ + A_ + C_, y0_ + B_, z0_ + A_ + C_}};
11 ✔
1276
}
1277

1278
double SurfaceYTorus::distance(Position r, Direction u, bool coincident) const
8,413,900 ✔
1279
{
1280
  double x = r.x - x0_;
8,413,900 ✔
1281
  double y = r.y - y0_;
8,413,900 ✔
1282
  double z = r.z - z0_;
8,413,900 ✔
1283
  return torus_distance(x, z, y, u.x, u.z, u.y, A_, B_, C_, coincident);
8,413,900 ✔
1284
}
1285

1286
Direction SurfaceYTorus::normal(Position r) const
×
1287
{
1288
  // reduce the expansion of the full form for torus
1289
  double x = r.x - x0_;
×
1290
  double y = r.y - y0_;
×
1291
  double z = r.z - z0_;
×
1292

1293
  // f(x,y,z) = y^2/B^2 + (sqrt(x^2 + z^2) - A)^2/C^2 - 1
1294
  // ∂f/∂x = 2x(g - A)/(g*C^2) where g = sqrt(x^2 + z^2)
1295
  // ∂f/∂y = 2y/B^2
1296
  // ∂f/∂z = 2z(g - A)/(g*C^2)
1297
  // Multiplying by g*C^2*B^2 / 2 gives:
1298
  double g = std::sqrt(x * x + z * z);
×
1299
  double nx = x * (g - A_) * B_ * B_;
×
1300
  double ny = C_ * C_ * g * y;
×
1301
  double nz = z * (g - A_) * B_ * B_;
×
1302
  Direction n(nx, ny, nz);
×
1303
  return n / n.norm();
×
1304
}
1305

1306
//==============================================================================
1307
// SurfaceZTorus implementation
1308
//==============================================================================
NEW
1309
bool SurfaceZTorus::triso_in_mesh(
×
1310
  vector<double> mesh_center, vector<double> lattice_pitch) const
1311
{
NEW
1312
  return false;
×
1313
}
1314

1315
SurfaceZTorus::SurfaceZTorus(pugi::xml_node surf_node) : Surface(surf_node)
115 ✔
1316
{
1317
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &A_, &B_, &C_});
115 ✔
1318
}
115 ✔
1319

1320
void SurfaceZTorus::to_hdf5_inner(hid_t group_id) const
88 ✔
1321
{
1322
  write_string(group_id, "type", "z-torus", false);
88 ✔
1323
  std::array<double, 6> coeffs {{x0_, y0_, z0_, A_, B_, C_}};
88 ✔
1324
  write_dataset(group_id, "coefficients", coeffs);
88 ✔
1325
}
88 ✔
1326

1327
double SurfaceZTorus::evaluate(Position r) const
1,307,175 ✔
1328
{
1329
  double x = r.x - x0_;
1,307,175 ✔
1330
  double y = r.y - y0_;
1,307,175 ✔
1331
  double z = r.z - z0_;
1,307,175 ✔
1332
  return (z * z) / (B_ * B_) +
2,614,350 ✔
1333
         std::pow(std::sqrt(x * x + y * y) - A_, 2) / (C_ * C_) - 1.;
1,307,175 ✔
1334
}
1335

1336
BoundingBox SurfaceZTorus::bounding_box(bool pos_side) const
22 ✔
1337
{
1338
  if (pos_side)
22 ✔
1339
    return {};
11 ✔
1340
  return {{x0_ - A_ - C_, y0_ - A_ - C_, z0_ - B_},
11 ✔
1341
    {x0_ + A_ + C_, y0_ + A_ + C_, z0_ + B_}};
11 ✔
1342
}
1343

1344
double SurfaceZTorus::distance(Position r, Direction u, bool coincident) const
9,772,048 ✔
1345
{
1346
  double x = r.x - x0_;
9,772,048 ✔
1347
  double y = r.y - y0_;
9,772,048 ✔
1348
  double z = r.z - z0_;
9,772,048 ✔
1349
  return torus_distance(x, y, z, u.x, u.y, u.z, A_, B_, C_, coincident);
9,772,048 ✔
1350
}
1351

1352
Direction SurfaceZTorus::normal(Position r) const
×
1353
{
1354
  // reduce the expansion of the full form for torus
1355
  double x = r.x - x0_;
×
1356
  double y = r.y - y0_;
×
1357
  double z = r.z - z0_;
×
1358

1359
  // f(x,y,z) = z^2/B^2 + (sqrt(x^2 + y^2) - A)^2/C^2 - 1
1360
  // ∂f/∂x = 2x(g - A)/(g*C^2) where g = sqrt(x^2 + y^2)
1361
  // ∂f/∂y = 2y(g - A)/(g*C^2)
1362
  // ∂f/∂z = 2z/B^2
1363
  // Multiplying by g*C^2*B^2 / 2 gives:
1364
  double g = std::sqrt(x * x + y * y);
×
1365
  double nx = x * (g - A_) * B_ * B_;
×
1366
  double ny = y * (g - A_) * B_ * B_;
×
1367
  double nz = C_ * C_ * g * z;
×
1368
  Position n(nx, ny, nz);
×
1369
  return n / n.norm();
×
1370
}
1371

1372
//==============================================================================
1373

1374
void read_surfaces(pugi::xml_node node,
9,607 ✔
1375
  std::set<std::pair<int, int>>& periodic_pairs,
1376
  std::unordered_map<int, double>& albedo_map,
1377
  std::unordered_map<int, int>& periodic_sense_map)
1378
{
1379
  // Count the number of surfaces
1380
  int n_surfaces = 0;
9,607 ✔
1381
  for (pugi::xml_node surf_node : node.children("surface")) {
61,364 ✔
1382
    n_surfaces++;
51,757 ✔
1383
  }
1384

1385
  // Loop over XML surface elements and populate the array.  Keep track of
1386
  // periodic surfaces and their albedos.
1387
  model::surfaces.reserve(n_surfaces);
9,607 ✔
1388
  {
9,607 ✔
1389
    pugi::xml_node surf_node;
9,607 ✔
1390
    int i_surf;
9,607 ✔
1391
    for (surf_node = node.child("surface"), i_surf = 0; surf_node;
61,364 ✔
1392
         surf_node = surf_node.next_sibling("surface"), i_surf++) {
51,757 ✔
1393
      std::string surf_type = get_node_value(surf_node, "type", true, true);
51,757 ✔
1394

1395
      // Allocate and initialize the new surface
1396

1397
      if (surf_type == "x-plane") {
51,757 ✔
1398
        model::surfaces.push_back(make_unique<SurfaceXPlane>(surf_node));
12,645 ✔
1399

1400
      } else if (surf_type == "y-plane") {
39,112 ✔
1401
        model::surfaces.push_back(make_unique<SurfaceYPlane>(surf_node));
11,464 ✔
1402

1403
      } else if (surf_type == "z-plane") {
27,648 ✔
1404
        model::surfaces.push_back(make_unique<SurfaceZPlane>(surf_node));
8,490 ✔
1405

1406
      } else if (surf_type == "plane") {
19,158 ✔
1407
        model::surfaces.push_back(make_unique<SurfacePlane>(surf_node));
2,095 ✔
1408

1409
      } else if (surf_type == "x-cylinder") {
17,063 ✔
1410
        model::surfaces.push_back(make_unique<SurfaceXCylinder>(surf_node));
45 ✔
1411

1412
      } else if (surf_type == "y-cylinder") {
17,018 ✔
1413
        model::surfaces.push_back(make_unique<SurfaceYCylinder>(surf_node));
26 ✔
1414

1415
      } else if (surf_type == "z-cylinder") {
16,992 ✔
1416
        model::surfaces.push_back(make_unique<SurfaceZCylinder>(surf_node));
5,807 ✔
1417

1418
      } else if (surf_type == "sphere") {
11,185 ✔
1419
        model::surfaces.push_back(make_unique<SurfaceSphere>(surf_node));
10,933 ✔
1420

1421
      } else if (surf_type == "x-cone") {
252 !
1422
        model::surfaces.push_back(make_unique<SurfaceXCone>(surf_node));
×
1423

1424
      } else if (surf_type == "y-cone") {
252 !
1425
        model::surfaces.push_back(make_unique<SurfaceYCone>(surf_node));
×
1426

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

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

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

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

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

1442
      } else {
1443
        fatal_error(fmt::format("Invalid surface type, \"{}\"", surf_type));
×
1444
      }
1445

1446
      // Check for a periodic surface
1447
      if (check_for_node(surf_node, "boundary")) {
51,757 ✔
1448
        std::string surf_bc = get_node_value(surf_node, "boundary", true, true);
30,136 ✔
1449
        if (surf_bc == "periodic") {
30,136 ✔
1450
          periodic_sense_map[model::surfaces.back()->id_] = 0;
546 ✔
1451
          // Check for surface albedo. Skip sanity check as it is already done
1452
          // in the Surface class's constructor.
1453
          if (check_for_node(surf_node, "albedo")) {
546 !
1454
            albedo_map[model::surfaces.back()->id_] =
×
1455
              std::stod(get_node_value(surf_node, "albedo"));
×
1456
          }
1457
          if (check_for_node(surf_node, "periodic_surface_id")) {
546 ✔
1458
            int i_periodic =
356 ✔
1459
              std::stoi(get_node_value(surf_node, "periodic_surface_id"));
712 ✔
1460
            int lo_id = std::min(model::surfaces.back()->id_, i_periodic);
356 ✔
1461
            int hi_id = std::max(model::surfaces.back()->id_, i_periodic);
356 ✔
1462
            periodic_pairs.insert({lo_id, hi_id});
356 ✔
1463
          } else {
1464
            periodic_pairs.insert({model::surfaces.back()->id_, -1});
190 ✔
1465
          }
1466
        }
1467
      }
30,136 ✔
1468
    }
51,757 ✔
1469
  }
1470

1471
  // Fill the surface map
1472
  for (int i_surf = 0; i_surf < model::surfaces.size(); i_surf++) {
61,364 ✔
1473
    int id = model::surfaces[i_surf]->id_;
51,757 !
1474
    auto in_map = model::surface_map.find(id);
51,757 !
1475
    if (in_map == model::surface_map.end()) {
51,757 !
1476
      model::surface_map[id] = i_surf;
51,757 ✔
1477
    } else {
1478
      fatal_error(
×
1479
        fmt::format("Two or more surfaces use the same unique ID: {}", id));
×
1480
    }
1481
  }
1482
}
9,607 ✔
1483

1484
void prepare_boundary_conditions(std::set<std::pair<int, int>>& periodic_pairs,
9,605 ✔
1485
  std::unordered_map<int, double>& albedo_map,
1486
  std::unordered_map<int, int>& periodic_sense_map)
1487
{
1488
  // Fill the senses map for periodic surfaces
1489
  auto n_periodic = periodic_sense_map.size();
9,605 ✔
1490
  for (const auto& cell : model::cells) {
9,862 ✔
1491
    if (n_periodic == 0)
9,731 ✔
1492
      break; // Early exit once all periodic surfaces found
1493

1494
    for (auto s : cell->surfaces()) {
1,335 ✔
1495
      auto surf_idx = std::abs(s) - 1;
1,078 ✔
1496
      auto id = model::surfaces[surf_idx]->id_;
1,078 ✔
1497

1498
      if (periodic_sense_map.count(id)) {
1,078 ✔
1499
        periodic_sense_map[id] = std::copysign(1, s);
546 ✔
1500
        --n_periodic;
546 ✔
1501
      }
1502
    }
257 ✔
1503
  }
1504

1505
  // Resolve unpaired periodic surfaces.  A lambda function is used with
1506
  // std::find_if to identify the unpaired surfaces.
1507
  auto is_unresolved_pair = [](const std::pair<int, int> p) {
9,973 ✔
1508
    return p.second == -1;
368 ✔
1509
  };
1510
  auto first_unresolved = std::find_if(
9,605 ✔
1511
    periodic_pairs.begin(), periodic_pairs.end(), is_unresolved_pair);
1512
  if (first_unresolved != periodic_pairs.end()) {
9,605 ✔
1513
    // Found one unpaired surface; search for a second one
1514
    auto next_elem = first_unresolved;
95 ✔
1515
    next_elem++;
95 !
1516
    auto second_unresolved =
95 !
1517
      std::find_if(next_elem, periodic_pairs.end(), is_unresolved_pair);
95 !
1518
    if (second_unresolved == periodic_pairs.end()) {
95 !
1519
      fatal_error("Found only one periodic surface without a specified partner."
×
1520
                  " Please specify the partner for each periodic surface.");
1521
    }
1522

1523
    // Make sure there isn't a third unpaired surface
1524
    next_elem = second_unresolved;
95 ✔
1525
    next_elem++;
95 !
1526
    auto third_unresolved =
95 !
1527
      std::find_if(next_elem, periodic_pairs.end(), is_unresolved_pair);
95 !
1528
    if (third_unresolved != periodic_pairs.end()) {
95 !
1529
      fatal_error(
×
1530
        "Found at least three periodic surfaces without a specified "
1531
        "partner. Please specify the partner for each periodic surface.");
1532
    }
1533

1534
    // Add the completed pair and remove the old, unpaired entries
1535
    int lo_id = std::min(first_unresolved->first, second_unresolved->first);
95 !
1536
    int hi_id = std::max(first_unresolved->first, second_unresolved->first);
95 !
1537
    periodic_pairs.insert({lo_id, hi_id});
95 ✔
1538
    periodic_pairs.erase(first_unresolved);
95 ✔
1539
    periodic_pairs.erase(second_unresolved);
95 ✔
1540
  }
1541

1542
  // Assign the periodic boundary conditions with albedos
1543
  for (auto periodic_pair : periodic_pairs) {
9,878 ✔
1544
    int i_surf = model::surface_map[periodic_pair.first];
273 ✔
1545
    int j_surf = model::surface_map[periodic_pair.second];
273 ✔
1546
    Surface& surf1 {*model::surfaces[i_surf]};
273 ✔
1547
    Surface& surf2 {*model::surfaces[j_surf]};
273 ✔
1548

1549
    // Compute the dot product of the surface normals
1550
    Direction norm1 = surf1.normal({0, 0, 0});
273 ✔
1551
    Direction norm2 = surf2.normal({0, 0, 0});
273 ✔
1552
    norm1 /= norm1.norm();
273 ✔
1553
    norm2 /= norm2.norm();
273 ✔
1554
    double dot_prod = norm1.dot(norm2);
273 ✔
1555

1556
    // If the dot product is 1 (to within floating point precision) then the
1557
    // planes are parallel which indicates a translational periodic boundary
1558
    // condition.  Otherwise, it is a rotational periodic BC.
1559
    if (std::abs(1.0 - dot_prod) < FP_PRECISION) {
273 ✔
1560
      surf1.bc_ = make_unique<TranslationalPeriodicBC>(i_surf, j_surf);
113 ✔
1561
      surf2.bc_ = make_unique<TranslationalPeriodicBC>(j_surf, i_surf);
113 ✔
1562
    } else {
1563
      // check that both normals have at least one 0 component
1564
      if (std::abs(norm1.x) > FP_PRECISION &&
160 ✔
1565
          std::abs(norm1.y) > FP_PRECISION &&
160 !
1566
          std::abs(norm1.z) > FP_PRECISION) {
115 !
1567
        fatal_error(fmt::format(
×
1568
          "The normal ({}) of the periodic surface ({}) does not contain any "
1569
          "component with a zero value. A RotationalPeriodicBC requires one "
1570
          "component which is zero for both plane normals.",
1571
          norm1, i_surf));
1572
      }
1573
      if (std::abs(norm2.x) > FP_PRECISION &&
160 ✔
1574
          std::abs(norm2.y) > FP_PRECISION &&
160 !
1575
          std::abs(norm2.z) > FP_PRECISION) {
115 !
1576
        fatal_error(fmt::format(
×
1577
          "The normal ({}) of the periodic surface ({}) does not contain any "
1578
          "component with a zero value. A RotationalPeriodicBC requires one "
1579
          "component which is zero for both plane normals.",
1580
          norm2, j_surf));
1581
      }
1582
      // find common zero component, which indicates the periodic axis
1583
      RotationalPeriodicBC::PeriodicAxis axis;
160 ✔
1584
      if (std::abs(norm1.x) <= FP_PRECISION &&
160 ✔
1585
          std::abs(norm2.x) <= FP_PRECISION) {
15 !
1586
        axis = RotationalPeriodicBC::PeriodicAxis::x;
15 ✔
1587
      } else if (std::abs(norm1.y) <= FP_PRECISION &&
145 ✔
1588
                 std::abs(norm2.y) <= FP_PRECISION) {
30 ✔
1589
        axis = RotationalPeriodicBC::PeriodicAxis::y;
15 ✔
1590
      } else if (std::abs(norm1.z) <= FP_PRECISION &&
130 !
1591
                 std::abs(norm2.z) <= FP_PRECISION) {
130 !
1592
        axis = RotationalPeriodicBC::PeriodicAxis::z;
130 ✔
1593
      } else {
1594
        fatal_error(fmt::format(
×
1595
          "There is no component which is 0.0 in both normal vectors. This "
1596
          "indicates that the two planes are not periodic about the X, Y, or Z "
1597
          "axis, which is not supported."));
1598
      }
1599
      auto i_sign = periodic_sense_map[periodic_pair.first];
160 ✔
1600
      auto j_sign = periodic_sense_map[periodic_pair.second];
160 ✔
1601
      surf1.bc_ = make_unique<RotationalPeriodicBC>(
160 ✔
1602
        i_sign * (i_surf + 1), j_sign * (j_surf + 1), axis);
160 ✔
1603
      surf2.bc_ = make_unique<RotationalPeriodicBC>(
160 ✔
1604
        j_sign * (j_surf + 1), i_sign * (i_surf + 1), axis);
320 ✔
1605
    }
1606

1607
    // If albedo data is present in albedo map, set the boundary albedo.
1608
    if (albedo_map.count(surf1.id_)) {
273 !
1609
      surf1.bc_->set_albedo(albedo_map[surf1.id_]);
×
1610
    }
1611
    if (albedo_map.count(surf2.id_)) {
273 !
1612
      surf2.bc_->set_albedo(albedo_map[surf2.id_]);
273 ✔
1613
    }
1614
  }
1615
}
9,605 ✔
1616

1617
void free_memory_surfaces()
9,736 ✔
1618
{
1619
  model::surfaces.clear();
9,736 ✔
1620
  model::surface_map.clear();
9,736 ✔
1621
}
9,736 ✔
1622

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