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

openmc-dev / openmc / 15314656955

29 May 2025 02:09AM UTC coverage: 85.33% (+0.1%) from 85.218%
15314656955

Pull #3421

github

web-flow
Merge 5e8cb3214 into cb95c784b
Pull Request #3421: Fix no serialization of periodic_surface_id bug

18 of 19 new or added lines in 3 files covered. (94.74%)

128 existing lines in 2 files now uncovered.

52435 of 61450 relevant lines covered (85.33%)

36965683.74 hits per line

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

71.94
/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/container_util.h"
13
#include "openmc/error.h"
14
#include "openmc/external/quartic_solver.h"
15
#include "openmc/hdf5_interface.h"
16
#include "openmc/math_functions.h"
17
#include "openmc/random_lcg.h"
18
#include "openmc/settings.h"
19
#include "openmc/string_utils.h"
20
#include "openmc/xml_interface.h"
21

22
namespace openmc {
23

24
//==============================================================================
25
// Global variables
26
//==============================================================================
27

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

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

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

48
  // Copy the coefficients
49
  int i = 0;
36,015✔
50
  for (auto c : coeffs) {
110,150✔
51
    *c = coeffs_file[i++];
74,135✔
52
  }
53
}
36,015✔
54

55
//==============================================================================
56
// Surface implementation
57
//==============================================================================
58

59
Surface::Surface() {} // empty constructor
828✔
60

61
Surface::Surface(pugi::xml_node surf_node)
36,015✔
62
{
63
  if (check_for_node(surf_node, "id")) {
36,015✔
64
    id_ = std::stoi(get_node_value(surf_node, "id"));
36,015✔
65
    if (contains(settings::source_write_surf_id, id_) ||
71,253✔
66
        settings::source_write_surf_id.empty()) {
35,238✔
67
      surf_source_ = true;
35,006✔
68
    }
69
  } else {
70
    fatal_error("Must specify id of surface in geometry XML file.");
×
71
  }
72

73
  if (check_for_node(surf_node, "name")) {
36,015✔
74
    name_ = get_node_value(surf_node, "name", false);
8,380✔
75
  }
76

77
  if (check_for_node(surf_node, "boundary")) {
36,015✔
78
    std::string surf_bc = get_node_value(surf_node, "boundary", true, true);
21,156✔
79

80
    if (surf_bc == "transmission" || surf_bc == "transmit" || surf_bc.empty()) {
21,156✔
81
      // Leave the bc_ a nullptr
82
    } else if (surf_bc == "vacuum") {
21,156✔
83
      bc_ = make_unique<VacuumBC>();
10,309✔
84
    } else if (surf_bc == "reflective" || surf_bc == "reflect" ||
11,175✔
85
               surf_bc == "reflecting") {
328✔
86
      bc_ = make_unique<ReflectiveBC>();
10,519✔
87
    } else if (surf_bc == "white") {
328✔
88
      bc_ = make_unique<WhiteBC>();
96✔
89
    } else if (surf_bc == "periodic") {
232✔
90
      // Periodic BCs are handled separately
91
    } else {
92
      fatal_error(fmt::format("Unknown boundary condition \"{}\" specified "
×
93
                              "on surface {}",
94
        surf_bc, id_));
×
95
    }
96

97
    if (check_for_node(surf_node, "albedo") && bc_) {
21,156✔
98
      double surf_alb = std::stod(get_node_value(surf_node, "albedo"));
96✔
99

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

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

111
      bc_->set_albedo(surf_alb);
96✔
112
    }
113
  }
21,156✔
114
}
36,015✔
115

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

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

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

138
  // Reflect direction according to normal.
139
  return u.reflect(n);
1,206,139,954✔
140
}
141

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

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

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

156
  // sample azimuthal distribution uniformly
157
  u = rotate_angle(n, mu, nullptr, seed);
1,016,983✔
158

159
  // normalize the direction
160
  return u / u.norm();
2,033,966✔
161
}
162

163
void Surface::to_hdf5(hid_t group_id) const
28,527✔
164
{
165
  hid_t surf_group = create_group(group_id, fmt::format("surface {}", id_));
57,054✔
166

167
  if (geom_type() == GeometryType::DAG) {
28,527✔
168
    write_string(surf_group, "geom_type", "dagmc", false);
594✔
169
  } else if (geom_type() == GeometryType::CSG) {
27,933✔
170
    write_string(surf_group, "geom_type", "csg", false);
27,933✔
171

172
    if (bc_) {
27,933✔
173
      write_string(surf_group, "boundary_type", bc_->type(), false);
16,618✔
174
      bc_->to_hdf5(surf_group);
16,618✔
175

176
      // write periodic surface id
177
      if (bc_->type() == "periodic") {
16,618✔
178
        auto pbc = dynamic_cast<PeriodicBC*>(bc_.get());
172✔
179
        if (id_ == pbc->i_surf() + 1) {
172✔
180
          write_string(surf_group, "periodic_surface_id",
86✔
181
            fmt::format("{}", pbc->j_surf() + 1), false);
242✔
182
        } else if (id_ == pbc->j_surf() + 1) {
86✔
183
          write_string(surf_group, "periodic_surface_id",
86✔
184
            fmt::format("{}", pbc->i_surf() + 1), false);
242✔
185
        } else {
NEW
186
          UNREACHABLE();
×
187
        }
188
      }
189
    } else {
190
      write_string(surf_group, "boundary_type", "transmission", false);
11,315✔
191
    }
192
  }
193

194
  if (!name_.empty()) {
28,527✔
195
    write_string(surf_group, "name", name_, false);
6,209✔
196
  }
197

198
  to_hdf5_inner(surf_group);
28,527✔
199

200
  close_group(surf_group);
28,527✔
201
}
28,527✔
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;
601,730,675✔
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;
2,147,483,647✔
219
}
220

2,147,483,647✔
221
//==============================================================================
222
// SurfaceXPlane implementation
223
//==============================================================================
2,147,483,647✔
224

2,147,483,647✔
225
SurfaceXPlane::SurfaceXPlane(pugi::xml_node surf_node) : Surface(surf_node)
55,109,547✔
226
{
2,147,483,647✔
227
  read_coeffs(surf_node, id_, {&x0_});
2,147,483,647✔
228
}
1,136,158,210✔
229

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

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

2,147,483,647✔
240
Direction SurfaceXPlane::normal(Position r) const
2,147,483,647✔
241
{
242
  return {1., 0., 0.};
2,147,483,647✔
243
}
244

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

2,147,483,647✔
252
BoundingBox SurfaceXPlane::bounding_box(bool pos_side) const
253
{
254
  if (pos_side) {
255
    return {x0_, INFTY, -INFTY, INFTY, -INFTY, INFTY};
256
  } else {
257
    return {-INFTY, x0_, -INFTY, INFTY, -INFTY, INFTY};
258
  }
8,579✔
259
}
260

8,579✔
261
//==============================================================================
8,579✔
262
// SurfaceYPlane implementation
263
//==============================================================================
1,512,330,985✔
264

265
SurfaceYPlane::SurfaceYPlane(pugi::xml_node surf_node) : Surface(surf_node)
1,512,330,985✔
266
{
267
  read_coeffs(surf_node, id_, {&y0_});
268
}
2,147,483,647✔
269

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

275
double SurfaceYPlane::distance(Position r, Direction u, bool coincident) const
263,072,176✔
276
{
277
  return axis_aligned_plane_distance<1>(r, u, coincident, y0_);
278
}
6,599✔
279

280
Direction SurfaceYPlane::normal(Position r) const
6,599✔
281
{
6,599✔
282
  return {0., 1., 0.};
6,599✔
283
}
6,599✔
284

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

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

7,635✔
301
//==============================================================================
7,635✔
302
// SurfaceZPlane implementation
303
//==============================================================================
1,511,614,772✔
304

305
SurfaceZPlane::SurfaceZPlane(pugi::xml_node surf_node) : Surface(surf_node)
1,511,614,772✔
306
{
307
  read_coeffs(surf_node, id_, {&z0_});
308
}
2,147,483,647✔
309

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

315
double SurfaceZPlane::distance(Position r, Direction u, bool coincident) const
285,708,553✔
316
{
317
  return axis_aligned_plane_distance<2>(r, u, coincident, z0_);
318
}
6,007✔
319

320
Direction SurfaceZPlane::normal(Position r) const
6,007✔
321
{
6,007✔
322
  return {0., 0., 1.};
6,007✔
323
}
6,007✔
324

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

332
BoundingBox SurfaceZPlane::bounding_box(bool pos_side) const
333
{
334
  if (pos_side) {
335
    return {-INFTY, INFTY, -INFTY, INFTY, z0_, INFTY};
336
  } else {
337
    return {-INFTY, INFTY, -INFTY, INFTY, -INFTY, z0_};
338
  }
5,759✔
339
}
340

5,759✔
341
//==============================================================================
5,759✔
342
// SurfacePlane implementation
343
//==============================================================================
486,971,268✔
344

345
SurfacePlane::SurfacePlane(pugi::xml_node surf_node) : Surface(surf_node)
486,971,268✔
346
{
347
  read_coeffs(surf_node, id_, {&A_, &B_, &C_, &D_});
348
}
2,147,483,647✔
349

350
double SurfacePlane::evaluate(Position r) const
2,147,483,647✔
351
{
352
  return A_ * r.x + B_ * r.y + C_ * r.z - D_;
353
}
55,779,836✔
354

355
double SurfacePlane::distance(Position r, Direction u, bool coincident) const
55,779,836✔
356
{
357
  const double f = A_ * r.x + B_ * r.y + C_ * r.z - D_;
358
  const double projection = A_ * u.x + B_ * u.y + C_ * u.z;
4,823✔
359
  if (coincident || std::abs(f) < FP_COINCIDENT || projection == 0.0) {
360
    return INFTY;
4,823✔
361
  } else {
4,823✔
362
    const double d = -f / projection;
4,823✔
363
    if (d < 0.0)
4,823✔
364
      return INFTY;
UNCOV
365
    return d;
×
366
  }
367
}
×
UNCOV
368

×
369
Direction SurfacePlane::normal(Position r) const
UNCOV
370
{
×
371
  return {A_, B_, C_};
372
}
373

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

1,508✔
381
//==============================================================================
1,508✔
382
// Generic functions for x-, y-, and z-, cylinders
383
//==============================================================================
201,585,251✔
384

385
// The template parameters indicate the axes perpendicular to the axis of the
201,585,251✔
386
// cylinder.  offset1 and offset2 should correspond with i1 and i2,
387
// respectively.
388
template<int i1, int i2>
578,660,990✔
389
double axis_aligned_cylinder_evaluate(
390
  Position r, double offset1, double offset2, double radius)
578,660,990✔
391
{
578,660,990✔
392
  const double r1 = r.get<i1>() - offset1;
578,660,990✔
393
  const double r2 = r.get<i2>() - offset2;
35,616,614✔
394
  return r1 * r1 + r2 * r2 - radius * radius;
395
}
543,044,376✔
396

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

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

414
  if (quad < 0.0) {
415
    // No intersection with cylinder.
416
    return INFTY;
417

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

1,408,423,458✔
428
  } else if (c < 0.0) {
429
    // Particle is inside the cylinder, thus one distance must be negative
1,407,062,975✔
430
    // and one must be positive. The positive distance will be the one with
431
    // negative sign on sqrt(quad).
432
    return (-k + sqrt(quad)) / a;
1,407,062,975✔
433

1,407,062,975✔
434
  } else {
1,407,062,975✔
435
    // Particle is outside the cylinder, thus both distances are either
UNCOV
436
    // positive or negative. If positive, the smaller distance is the one
×
437
    // with positive sign on sqrt(quad).
438
    const double d = (-k - sqrt(quad)) / a;
439
    if (d < 0.0)
×
440
      return INFTY;
×
UNCOV
441
    return d;
×
442
  }
443
}
1,360,483✔
444

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

1,634,729,675✔
459
//==============================================================================
1,634,729,675✔
460
// SurfaceXCylinder implementation
809,996✔
461
//==============================================================================
462

1,633,919,679✔
463
SurfaceXCylinder::SurfaceXCylinder(pugi::xml_node surf_node)
1,633,919,679✔
464
  : Surface(surf_node)
1,633,919,679✔
465
{
1,633,919,679✔
466
  read_coeffs(surf_node, id_, {&y0_, &z0_, &radius_});
1,633,919,679✔
467
}
468

1,633,919,679✔
469
double SurfaceXCylinder::evaluate(Position r) const
470
{
269,307,807✔
471
  return axis_aligned_cylinder_evaluate<1, 2>(r, y0_, z0_, radius_);
472
}
1,364,611,872✔
473

474
double SurfaceXCylinder::distance(
475
  Position r, Direction u, bool coincident) const
476
{
621,102,857✔
477
  return axis_aligned_cylinder_distance<0, 1, 2>(
311,631,141✔
478
    r, u, coincident, y0_, z0_, radius_);
479
}
309,471,716✔
480

481
Direction SurfaceXCylinder::normal(Position r) const
482
{
743,509,015✔
483
  return axis_aligned_cylinder_normal<0, 1, 2>(r, y0_, z0_);
484
}
485

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

443,028,718✔
493
BoundingBox SurfaceXCylinder::bounding_box(bool pos_side) const
443,028,718✔
494
{
63,740,278✔
495
  if (!pos_side) {
379,288,440✔
496
    return {-INFTY, INFTY, y0_ - radius_, y0_ + radius_, z0_ - radius_,
497
      z0_ + radius_};
498
  } else {
1,633,646,439✔
499
    return {};
500
  }
501
}
1,633,646,439✔
502
//==============================================================================
1,633,646,439✔
503
// SurfaceYCylinder implementation
369,996✔
504
//==============================================================================
505

1,633,276,443✔
506
SurfaceYCylinder::SurfaceYCylinder(pugi::xml_node surf_node)
1,633,276,443✔
507
  : Surface(surf_node)
1,633,276,443✔
508
{
1,633,276,443✔
509
  read_coeffs(surf_node, id_, {&x0_, &z0_, &radius_});
1,633,276,443✔
510
}
511

1,633,276,443✔
512
double SurfaceYCylinder::evaluate(Position r) const
513
{
269,307,807✔
514
  return axis_aligned_cylinder_evaluate<0, 2>(r, x0_, z0_, radius_);
515
}
1,363,968,636✔
516

517
double SurfaceYCylinder::distance(
518
  Position r, Direction u, bool coincident) const
519
{
621,102,857✔
520
  return axis_aligned_cylinder_distance<1, 0, 2>(
311,631,141✔
521
    r, u, coincident, x0_, z0_, radius_);
522
}
309,471,716✔
523

524
Direction SurfaceYCylinder::normal(Position r) const
525
{
742,865,779✔
526
  return axis_aligned_cylinder_normal<1, 0, 2>(r, x0_, z0_);
527
}
528

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

443,028,718✔
536
BoundingBox SurfaceYCylinder::bounding_box(bool pos_side) const
443,028,718✔
537
{
63,740,278✔
538
  if (!pos_side) {
379,288,440✔
539
    return {x0_ - radius_, x0_ + radius_, -INFTY, INFTY, z0_ - radius_,
540
      z0_ + radius_};
UNCOV
541
  } else {
×
542
    return {};
543
  }
544
}
×
545

×
UNCOV
546
//==============================================================================
×
547
// SurfaceZCylinder implementation
548
//==============================================================================
×
549

×
550
SurfaceZCylinder::SurfaceZCylinder(pugi::xml_node surf_node)
×
551
  : Surface(surf_node)
×
UNCOV
552
{
×
553
  read_coeffs(surf_node, id_, {&x0_, &y0_, &radius_});
UNCOV
554
}
×
555

UNCOV
556
double SurfaceZCylinder::evaluate(Position r) const
×
557
{
UNCOV
558
  return axis_aligned_cylinder_evaluate<0, 1>(r, x0_, y0_, radius_);
×
559
}
560

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

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

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

×
580
BoundingBox SurfaceZCylinder::bounding_box(bool pos_side) const
×
UNCOV
581
{
×
582
  if (!pos_side) {
583
    return {x0_ - radius_, x0_ + radius_, y0_ - radius_, y0_ + radius_, -INFTY,
584
      INFTY};
1,083,236✔
585
  } else {
586
    return {};
587
  }
1,083,236✔
588
}
1,083,236✔
589

440,000✔
590
//==============================================================================
591
// SurfaceSphere implementation
643,236✔
592
//==============================================================================
643,236✔
593

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

UNCOV
599
double SurfaceSphere::evaluate(Position r) const
×
600
{
601
  const double x = r.x - x0_;
643,236✔
602
  const double y = r.y - y0_;
603
  const double z = r.z - z0_;
604
  return x * x + y * y + z * z - radius_ * radius_;
605
}
×
UNCOV
606

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

643,236✔
616
  if (quad < 0.0) {
617
    // No intersection with sphere.
618
    return INFTY;
619

620
  } else if (coincident || std::abs(c) < FP_COINCIDENT) {
621
    // Particle is on the sphere, thus one distance is positive/negative and
×
622
    // the other is zero. The sign of k determines if we are facing in or out.
×
623
    if (k >= 0.0) {
×
UNCOV
624
      return INFTY;
×
625
    } else {
626
      return -k + sqrt(quad);
627
    }
628

629
  } else if (c < 0.0) {
630
    // Particle is inside the sphere, thus one distance must be negative and
631
    // one must be positive. The positive distance will be the one with
632
    // negative sign on sqrt(quad)
1,387,667✔
633
    return -k + sqrt(quad);
634

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

1,387,667✔
646
Direction SurfaceSphere::normal(Position r) const
1,387,667✔
647
{
1,387,667✔
648
  return {2.0 * (r.x - x0_), 2.0 * (r.y - y0_), 2.0 * (r.z - z0_)};
1,387,667✔
649
}
UNCOV
650

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

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

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

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

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

22✔
702
  double d;
UNCOV
703

×
704
  if (quad < 0.0) {
705
    // No intersection with cone.
×
706
    return INFTY;
×
UNCOV
707

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

×
718
  } else {
UNCOV
719
    // Calculate both solutions to the quadratic.
×
720
    quad = sqrt(quad);
721
    d = (-k - quad) / a;
UNCOV
722
    const double b = (-k + quad) / a;
×
723

UNCOV
724
    // Determine the smallest positive solution.
×
725
    if (d < 0.0) {
726
      if (b > 0.0)
UNCOV
727
        d = b;
×
728
    } else {
729
      if (b > 0.0) {
730
        if (b < d)
×
UNCOV
731
          d = b;
×
732
      }
733
    }
UNCOV
734
  }
×
735

UNCOV
736
  // If the distance was negative, set boundary distance to infinity.
×
737
  if (d <= 0.0)
738
    return INFTY;
UNCOV
739
  return d;
×
740
}
741

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

756
//==============================================================================
757
// SurfaceXCone implementation
758
//==============================================================================
759

760
SurfaceXCone::SurfaceXCone(pugi::xml_node surf_node) : Surface(surf_node)
4,526✔
761
{
4,526✔
762
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &radius_sq_});
763
}
4,526✔
764

4,526✔
765
double SurfaceXCone::evaluate(Position r) const
766
{
1,407,062,975✔
767
  return axis_aligned_cone_evaluate<0, 1, 2>(r, x0_, y0_, z0_, radius_sq_);
768
}
1,407,062,975✔
769

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

1,633,646,439✔
776
Direction SurfaceXCone::normal(Position r) const
777
{
778
  return axis_aligned_cone_normal<0, 1, 2>(r, x0_, y0_, z0_, radius_sq_);
1,387,667✔
779
}
780

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

3,278✔
788
//==============================================================================
3,278✔
789
// SurfaceYCone implementation
790
//==============================================================================
44✔
791

792
SurfaceYCone::SurfaceYCone(pugi::xml_node surf_node) : Surface(surf_node)
44✔
793
{
22✔
794
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &radius_sq_});
22✔
795
}
796

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

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

7,716✔
808
Direction SurfaceYCone::normal(Position r) const
809
{
542,015,966✔
810
  return axis_aligned_cone_normal<1, 0, 2>(r, y0_, x0_, z0_, radius_sq_);
811
}
542,015,966✔
812

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

599,355,820✔
820
//==============================================================================
599,355,820✔
821
// SurfaceZCone implementation
599,355,820✔
822
//==============================================================================
599,355,820✔
823

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

116,819,827✔
829
double SurfaceZCone::evaluate(Position r) const
830
{
482,535,993✔
831
  return axis_aligned_cone_evaluate<2, 0, 1>(r, z0_, x0_, y0_, radius_sq_);
832
}
833

67,687,142✔
834
double SurfaceZCone::distance(Position r, Direction u, bool coincident) const
33,153,405✔
835
{
836
  return axis_aligned_cone_distance<2, 0, 1>(
34,533,737✔
837
    r, u, coincident, z0_, x0_, y0_, radius_sq_);
838
}
839

414,848,851✔
840
Direction SurfaceZCone::normal(Position r) const
841
{
842
  return axis_aligned_cone_normal<2, 0, 1>(r, z0_, x0_, y0_, radius_sq_);
843
}
371,618,112✔
844

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

9,370,940✔
852
//==============================================================================
33,859,799✔
853
// SurfaceQuadric implementation
854
//==============================================================================
855

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

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

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

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

322,586✔
890
  double d;
322,586✔
891

322,586✔
892
  if (quad < 0.0) {
322,586✔
893
    // No intersection with surface.
894
    return INFTY;
322,586✔
895

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

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

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

936
  // If the distance was negative, set boundary distance to infinity.
937
  if (d <= 0.0)
938
    return INFTY;
963,358✔
939
  return d;
UNCOV
940
}
×
941

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

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

958
//==============================================================================
959
// Torus helper functions
902,792✔
960
//==============================================================================
766,524✔
961

650,507✔
962
double torus_distance(double x1, double x2, double x3, double u1, double u2,
963
  double u3, double A, double B, double C, bool coincident)
136,268✔
964
{
136,268✔
965
  // Coefficients for equation: (c2 t^2 + c1 t + c0)^2 = c2' t^2 + c1' t + c0'
136,268✔
966
  double D = (C * C) / (B * B);
967
  double c2 = u1 * u1 + u2 * u2 + D * u3 * u3;
968
  double c1 = 2 * (u1 * x1 + u2 * x2 + D * u3 * x3);
969
  double c0 = x1 * x1 + x2 * x2 + D * x3 * x3 + A * A - C * C;
970
  double four_A2 = 4 * A * A;
971
  double c2p = four_A2 * (u1 * u1 + u2 * u2);
963,358✔
972
  double c1p = 2 * four_A2 * (u1 * x1 + u2 * x2);
137,467✔
973
  double c0p = four_A2 * (x1 * x1 + x2 * x2);
825,891✔
974

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

963,358✔
985
  std::complex<double> roots[4];
963,358✔
986
  oqs::quartic_solver(coeff, roots);
963,358✔
987

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

902,792✔
1012
//==============================================================================
766,524✔
1013
// SurfaceXTorus implementation
650,507✔
1014
//==============================================================================
1015

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

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

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

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

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

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

×
UNCOV
1065
//==============================================================================
×
1066
// SurfaceYTorus implementation
1067
//==============================================================================
×
1068

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

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

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

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

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

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

×
1118
//==============================================================================
1119
// SurfaceZTorus implementation
×
1120
//==============================================================================
×
UNCOV
1121

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

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

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

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

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

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

×
1171
//==============================================================================
1172

1173
void read_surfaces(pugi::xml_node node)
1174
{
1175
  // Count the number of surfaces
1176
  int n_surfaces = 0;
UNCOV
1177
  for (pugi::xml_node surf_node : node.children("surface")) {
×
1178
    n_surfaces++;
UNCOV
1179
  }
×
1180

1181
  // Loop over XML surface elements and populate the array.  Keep track of
UNCOV
1182
  // periodic surfaces and their albedos.
×
1183
  model::surfaces.reserve(n_surfaces);
UNCOV
1184
  std::set<std::pair<int, int>> periodic_pairs;
×
1185
  std::unordered_map<int, double> albedo_map;
1186
  {
UNCOV
1187
    pugi::xml_node surf_node;
×
1188
    int i_surf;
1189
    for (surf_node = node.child("surface"), i_surf = 0; surf_node;
×
UNCOV
1190
         surf_node = surf_node.next_sibling("surface"), i_surf++) {
×
1191
      std::string surf_type = get_node_value(surf_node, "type", true, true);
1192

UNCOV
1193
      // Allocate and initialize the new surface
×
1194

UNCOV
1195
      if (surf_type == "x-plane") {
×
1196
        model::surfaces.push_back(make_unique<SurfaceXPlane>(surf_node));
1197

UNCOV
1198
      } else if (surf_type == "y-plane") {
×
1199
        model::surfaces.push_back(make_unique<SurfaceYPlane>(surf_node));
1200

×
1201
      } else if (surf_type == "z-plane") {
×
UNCOV
1202
        model::surfaces.push_back(make_unique<SurfaceZPlane>(surf_node));
×
1203

1204
      } else if (surf_type == "plane") {
1205
        model::surfaces.push_back(make_unique<SurfacePlane>(surf_node));
1206

1207
      } else if (surf_type == "x-cylinder") {
1208
        model::surfaces.push_back(make_unique<SurfaceXCylinder>(surf_node));
UNCOV
1209

×
1210
      } else if (surf_type == "y-cylinder") {
UNCOV
1211
        model::surfaces.push_back(make_unique<SurfaceYCylinder>(surf_node));
×
1212

1213
      } else if (surf_type == "z-cylinder") {
UNCOV
1214
        model::surfaces.push_back(make_unique<SurfaceZCylinder>(surf_node));
×
1215

UNCOV
1216
      } else if (surf_type == "sphere") {
×
1217
        model::surfaces.push_back(make_unique<SurfaceSphere>(surf_node));
1218

UNCOV
1219
      } else if (surf_type == "x-cone") {
×
1220
        model::surfaces.push_back(make_unique<SurfaceXCone>(surf_node));
1221

×
UNCOV
1222
      } else if (surf_type == "y-cone") {
×
1223
        model::surfaces.push_back(make_unique<SurfaceYCone>(surf_node));
1224

UNCOV
1225
      } else if (surf_type == "z-cone") {
×
1226
        model::surfaces.push_back(make_unique<SurfaceZCone>(surf_node));
UNCOV
1227

×
1228
      } else if (surf_type == "quadric") {
1229
        model::surfaces.push_back(make_unique<SurfaceQuadric>(surf_node));
UNCOV
1230

×
1231
      } else if (surf_type == "x-torus") {
1232
        model::surfaces.push_back(std::make_unique<SurfaceXTorus>(surf_node));
×
1233

×
UNCOV
1234
      } else if (surf_type == "y-torus") {
×
1235
        model::surfaces.push_back(std::make_unique<SurfaceYTorus>(surf_node));
1236

1237
      } else if (surf_type == "z-torus") {
1238
        model::surfaces.push_back(std::make_unique<SurfaceZTorus>(surf_node));
1239

1240
      } else {
1241
        fatal_error(fmt::format("Invalid surface type, \"{}\"", surf_type));
16✔
1242
      }
1243

16✔
1244
      // Check for a periodic surface
16✔
1245
      if (check_for_node(surf_node, "boundary")) {
1246
        std::string surf_bc = get_node_value(surf_node, "boundary", true, true);
322,586✔
1247
        if (surf_bc == "periodic") {
1248
          // Check for surface albedo. Skip sanity check as it is already done
322,586✔
1249
          // in the Surface class's constructor.
1250
          if (check_for_node(surf_node, "albedo")) {
1251
            albedo_map[model::surfaces.back()->id_] =
963,358✔
1252
              std::stod(get_node_value(surf_node, "albedo"));
1253
          }
963,358✔
1254
          if (check_for_node(surf_node, "periodic_surface_id")) {
963,358✔
1255
            int i_periodic =
1256
              std::stoi(get_node_value(surf_node, "periodic_surface_id"));
1257
            int lo_id = std::min(model::surfaces.back()->id_, i_periodic);
60,566✔
1258
            int hi_id = std::max(model::surfaces.back()->id_, i_periodic);
1259
            periodic_pairs.insert({lo_id, hi_id});
60,566✔
1260
          } else {
1261
            periodic_pairs.insert({model::surfaces.back()->id_, -1});
1262
          }
11✔
1263
        }
1264
      }
11✔
1265
    }
11✔
1266
  }
11✔
1267

11✔
1268
  // Fill the surface map
1269
  for (int i_surf = 0; i_surf < model::surfaces.size(); i_surf++) {
1270
    int id = model::surfaces[i_surf]->id_;
1271
    auto in_map = model::surface_map.find(id);
1272
    if (in_map == model::surface_map.end()) {
1273
      model::surface_map[id] = i_surf;
16✔
1274
    } else {
UNCOV
1275
      fatal_error(
×
1276
        fmt::format("Two or more surfaces use the same unique ID: {}", id));
16✔
1277
    }
16✔
1278
  }
1279

177,572✔
1280
  // Resolve unpaired periodic surfaces.  A lambda function is used with
1281
  // std::find_if to identify the unpaired surfaces.
177,572✔
1282
  auto is_unresolved_pair = [](const std::pair<int, int> p) {
177,572✔
1283
    return p.second == -1;
177,572✔
1284
  };
177,572✔
1285
  auto first_unresolved = std::find_if(
177,572✔
1286
    periodic_pairs.begin(), periodic_pairs.end(), is_unresolved_pair);
1287
  if (first_unresolved != periodic_pairs.end()) {
1288
    // Found one unpaired surface; search for a second one
312,510✔
1289
    auto next_elem = first_unresolved;
1290
    next_elem++;
1291
    auto second_unresolved =
312,510✔
1292
      std::find_if(next_elem, periodic_pairs.end(), is_unresolved_pair);
312,510✔
1293
    if (second_unresolved == periodic_pairs.end()) {
312,510✔
1294
      fatal_error("Found only one periodic surface without a specified partner."
312,510✔
1295
                  " Please specify the partner for each periodic surface.");
312,510✔
1296
    }
312,510✔
1297

1298
    // Make sure there isn't a third unpaired surface
312,510✔
1299
    next_elem = second_unresolved;
312,510✔
1300
    next_elem++;
312,510✔
1301
    auto third_unresolved =
312,510✔
1302
      std::find_if(next_elem, periodic_pairs.end(), is_unresolved_pair);
312,510✔
1303
    if (third_unresolved != periodic_pairs.end()) {
312,510✔
1304
      fatal_error(
312,510✔
1305
        "Found at least three periodic surfaces without a specified "
312,510✔
1306
        "partner. Please specify the partner for each periodic surface.");
1307
    }
1308

1309
    // Add the completed pair and remove the old, unpaired entries
312,510✔
1310
    int lo_id = std::min(first_unresolved->first, second_unresolved->first);
UNCOV
1311
    int hi_id = std::max(first_unresolved->first, second_unresolved->first);
×
1312
    periodic_pairs.insert({lo_id, hi_id});
1313
    periodic_pairs.erase(first_unresolved);
312,510✔
1314
    periodic_pairs.erase(second_unresolved);
1315
  }
1316

1317
  // Assign the periodic boundary conditions with albedos
1318
  for (auto periodic_pair : periodic_pairs) {
34,540✔
UNCOV
1319
    int i_surf = model::surface_map[periodic_pair.first];
×
1320
    int j_surf = model::surface_map[periodic_pair.second];
34,540✔
UNCOV
1321
    Surface& surf1 {*model::surfaces[i_surf]};
×
1322
    Surface& surf2 {*model::surfaces[j_surf]};
1323

34,540✔
1324
    // Compute the dot product of the surface normals
1325
    Direction norm1 = surf1.normal({0, 0, 0});
1326
    Direction norm2 = surf2.normal({0, 0, 0});
277,970✔
1327
    norm1 /= norm1.norm();
1328
    norm2 /= norm2.norm();
1329
    double dot_prod = norm1.dot(norm2);
1330

1331
    // If the dot product is 1 (to within floating point precision) then the
1332
    // planes are parallel which indicates a translational periodic boundary
1333
    // condition.  Otherwise, it is a rotational periodic BC.
UNCOV
1334
    if (std::abs(1.0 - dot_prod) < FP_PRECISION) {
×
1335
      surf1.bc_ = make_unique<TranslationalPeriodicBC>(i_surf, j_surf);
1336
      surf2.bc_ = make_unique<TranslationalPeriodicBC>(i_surf, j_surf);
1337
    } else {
277,970✔
1338
      surf1.bc_ = make_unique<RotationalPeriodicBC>(i_surf, j_surf);
277,970✔
1339
      surf2.bc_ = make_unique<RotationalPeriodicBC>(i_surf, j_surf);
277,970✔
1340
    }
1341

1342
    // If albedo data is present in albedo map, set the boundary albedo.
277,970✔
1343
    if (albedo_map.count(surf1.id_)) {
277,970✔
1344
      surf1.bc_->set_albedo(albedo_map[surf1.id_]);
277,970✔
1345
    }
1346
    if (albedo_map.count(surf2.id_)) {
×
1347
      surf2.bc_->set_albedo(albedo_map[surf2.id_]);
×
UNCOV
1348
    }
×
1349
  }
1350
}
1351

1352
void free_memory_surfaces()
1353
{
1354
  model::surfaces.clear();
312,510✔
UNCOV
1355
  model::surface_map.clear();
×
1356
}
312,510✔
1357

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