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

openmc-dev / openmc / 20278515727

16 Dec 2025 06:26PM UTC coverage: 81.822% (-0.1%) from 81.92%
20278515727

Pull #3493

github

web-flow
Merge 7f36869a3 into bbfa18d72
Pull Request #3493: Implement vector fitting to replace external `vectfit` package

17013 of 23575 branches covered (72.17%)

Branch coverage included in aggregate %.

188 of 207 new or added lines in 3 files covered. (90.82%)

3101 existing lines in 56 files now uncovered.

54973 of 64404 relevant lines covered (85.36%)

41385524.25 hits per line

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

79.47
/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,562✔
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,562✔
42
  if (coeffs_file.size() != coeffs.size()) {
36,562!
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,562✔
50
  for (auto c : coeffs) {
109,216✔
51
    *c = coeffs_file[i++];
72,654✔
52
  }
53
}
36,562✔
54

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

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

61
Surface::Surface(pugi::xml_node surf_node)
36,562✔
62
{
63
  if (check_for_node(surf_node, "id")) {
36,562!
64
    id_ = std::stoi(get_node_value(surf_node, "id"));
36,562✔
65
    if (contains(settings::source_write_surf_id, id_) ||
72,412✔
66
        settings::source_write_surf_id.empty()) {
35,850✔
67
      surf_source_ = true;
35,621✔
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,562✔
74
    name_ = get_node_value(surf_node, "name", false);
8,546✔
75
  }
76

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

80
    if (surf_bc == "transmission" || surf_bc == "transmit" || surf_bc.empty()) {
21,567!
81
      // Leave the bc_ a nullptr
82
    } else if (surf_bc == "vacuum") {
21,567✔
83
      bc_ = make_unique<VacuumBC>();
10,865✔
84
    } else if (surf_bc == "reflective" || surf_bc == "reflect" ||
11,106!
85
               surf_bc == "reflecting") {
404✔
86
      bc_ = make_unique<ReflectiveBC>();
10,298✔
87
    } else if (surf_bc == "white") {
404✔
88
      bc_ = make_unique<WhiteBC>();
84✔
89
    } else if (surf_bc == "periodic") {
320!
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,567!
98
      double surf_alb = std::stod(get_node_value(surf_node, "albedo"));
84✔
99

100
      if (surf_alb < 0.0)
84!
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)
84!
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);
84✔
112
    }
113
  }
21,567✔
114
}
36,562✔
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;
4,542,864✔
128
  }
129
  return f > 0.0;
2,147,483,647✔
130
}
131

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

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

142
Direction Surface::diffuse_reflect(
915,590✔
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);
915,590✔
149
  n /= n.norm();
915,590✔
150
  const double projection = n.dot(u);
915,590✔
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));
915,590✔
155

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

159
  // normalize the direction
160
  return u / u.norm();
1,831,180✔
161
}
162

163
void Surface::to_hdf5(hid_t group_id) const
29,680✔
164
{
165
  hid_t surf_group = create_group(group_id, fmt::format("surface {}", id_));
59,360✔
166

167
  if (geom_type() == GeometryType::DAG) {
29,680!
168
    write_string(surf_group, "geom_type", "dagmc", false);
602!
169
  } else if (geom_type() == GeometryType::CSG) {
29,078!
170
    write_string(surf_group, "geom_type", "csg", false);
29,078✔
171

172
    if (bc_) {
29,078✔
173
      write_string(surf_group, "boundary_type", bc_->type(), false);
17,356✔
174
      bc_->to_hdf5(surf_group);
17,356✔
175

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

182
        if (id_ == surf1.id_) {
256✔
183
          write_dataset(surf_group, "periodic_surface_id", surf2.id_);
128✔
184
        } else {
185
          write_dataset(surf_group, "periodic_surface_id", surf1.id_);
128✔
186
        }
187
      }
188
    } else {
189
      write_string(surf_group, "boundary_type", "transmission", false);
11,722✔
190
    }
191
  }
192

193
  if (!name_.empty()) {
29,680✔
194
    write_string(surf_group, "name", name_, false);
6,514✔
195
  }
196

197
  to_hdf5_inner(surf_group);
29,680✔
198

199
  close_group(surf_group);
29,680✔
200
}
29,680✔
201

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

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

220
//==============================================================================
221
// SurfaceXPlane implementation
222
//==============================================================================
223

224
SurfaceXPlane::SurfaceXPlane(pugi::xml_node surf_node) : Surface(surf_node)
8,976✔
225
{
226
  read_coeffs(surf_node, id_, {&x0_});
8,976✔
227
}
8,976✔
228

229
double SurfaceXPlane::evaluate(Position r) const
1,452,288,710✔
230
{
231
  return r.x - x0_;
1,452,288,710✔
232
}
233

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

239
Direction SurfaceXPlane::normal(Position r) const
251,966,515✔
240
{
241
  return {1., 0., 0.};
251,966,515✔
242
}
243

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

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

260
//==============================================================================
261
// SurfaceYPlane implementation
262
//==============================================================================
263

264
SurfaceYPlane::SurfaceYPlane(pugi::xml_node surf_node) : Surface(surf_node)
8,056✔
265
{
266
  read_coeffs(surf_node, id_, {&y0_});
8,056✔
267
}
8,056✔
268

269
double SurfaceYPlane::evaluate(Position r) const
1,442,043,499✔
270
{
271
  return r.y - y0_;
1,442,043,499✔
272
}
273

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

279
Direction SurfaceYPlane::normal(Position r) const
270,853,179✔
280
{
281
  return {0., 1., 0.};
270,853,179✔
282
}
283

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

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

300
//==============================================================================
301
// SurfaceZPlane implementation
302
//==============================================================================
303

304
SurfaceZPlane::SurfaceZPlane(pugi::xml_node surf_node) : Surface(surf_node)
6,214✔
305
{
306
  read_coeffs(surf_node, id_, {&z0_});
6,214✔
307
}
6,214✔
308

309
double SurfaceZPlane::evaluate(Position r) const
449,562,789✔
310
{
311
  return r.z - z0_;
449,562,789✔
312
}
313

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

319
Direction SurfaceZPlane::normal(Position r) const
49,608,874✔
320
{
321
  return {0., 0., 1.};
49,608,874✔
322
}
323

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

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

340
//==============================================================================
341
// SurfacePlane implementation
342
//==============================================================================
343

344
SurfacePlane::SurfacePlane(pugi::xml_node surf_node) : Surface(surf_node)
1,336✔
345
{
346
  read_coeffs(surf_node, id_, {&A_, &B_, &C_, &D_});
1,336✔
347
}
1,336✔
348

349
double SurfacePlane::evaluate(Position r) const
180,426,538✔
350
{
351
  return A_ * r.x + B_ * r.y + C_ * r.z - D_;
180,426,538✔
352
}
353

354
double SurfacePlane::distance(Position r, Direction u, bool coincident) const
523,888,980✔
355
{
356
  const double f = A_ * r.x + B_ * r.y + C_ * r.z - D_;
523,888,980✔
357
  const double projection = A_ * u.x + B_ * u.y + C_ * u.z;
523,888,980✔
358
  if (coincident || std::abs(f) < FP_COINCIDENT || projection == 0.0) {
523,888,980!
359
    return INFTY;
32,040,158✔
360
  } else {
361
    const double d = -f / projection;
491,848,822✔
362
    if (d < 0.0)
491,848,822✔
363
      return INFTY;
229,886,910✔
364
    return d;
261,961,912✔
365
  }
366
}
367

368
Direction SurfacePlane::normal(Position r) const
5,187,976✔
369
{
370
  return {A_, B_, C_};
5,187,976✔
371
}
372

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

380
//==============================================================================
381
// Generic functions for x-, y-, and z-, cylinders
382
//==============================================================================
383

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

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

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

413
  if (quad < 0.0) {
1,564,386,972✔
414
    // No intersection with cylinder.
415
    return INFTY;
261,586,205✔
416

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

427
  } else if (c < 0.0) {
697,295,940✔
428
    // Particle is inside the cylinder, thus one distance must be negative
429
    // and one must be positive. The positive distance will be the one with
430
    // negative sign on sqrt(quad).
431
    return (-k + sqrt(quad)) / a;
274,228,753✔
432

433
  } else {
434
    // Particle is outside the cylinder, thus both distances are either
435
    // positive or negative. If positive, the smaller distance is the one
436
    // with positive sign on sqrt(quad).
437
    const double d = (-k - sqrt(quad)) / a;
423,067,187✔
438
    if (d < 0.0)
423,067,187✔
439
      return INFTY;
57,857,194✔
440
    return d;
365,209,993✔
441
  }
442
}
443

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

458
//==============================================================================
459
// SurfaceXCylinder implementation
460
//==============================================================================
461

462
SurfaceXCylinder::SurfaceXCylinder(pugi::xml_node surf_node)
42✔
463
  : Surface(surf_node)
42✔
464
{
465
  read_coeffs(surf_node, id_, {&y0_, &z0_, &radius_});
42✔
466
}
42✔
467

468
double SurfaceXCylinder::evaluate(Position r) const
1,387,398✔
469
{
470
  return axis_aligned_cylinder_evaluate<1, 2>(r, y0_, z0_, radius_);
1,387,398✔
471
}
472

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

480
Direction SurfaceXCylinder::normal(Position r) const
×
481
{
482
  return axis_aligned_cylinder_normal<0, 1, 2>(r, y0_, z0_);
×
483
}
484

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

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

505
SurfaceYCylinder::SurfaceYCylinder(pugi::xml_node surf_node)
14✔
506
  : Surface(surf_node)
14✔
507
{
508
  read_coeffs(surf_node, id_, {&x0_, &z0_, &radius_});
14✔
509
}
14✔
510

511
double SurfaceYCylinder::evaluate(Position r) const
155,440✔
512
{
513
  return axis_aligned_cylinder_evaluate<0, 2>(r, x0_, z0_, radius_);
155,440✔
514
}
515

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

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

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

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

545
//==============================================================================
546
// SurfaceZCylinder implementation
547
//==============================================================================
548

549
SurfaceZCylinder::SurfaceZCylinder(pugi::xml_node surf_node)
4,292✔
550
  : Surface(surf_node)
4,292✔
551
{
552
  read_coeffs(surf_node, id_, {&x0_, &y0_, &radius_});
4,292✔
553
}
4,292✔
554

555
double SurfaceZCylinder::evaluate(Position r) const
1,320,540,908✔
556
{
557
  return axis_aligned_cylinder_evaluate<0, 1>(r, x0_, y0_, radius_);
1,320,540,908✔
558
}
559

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

567
Direction SurfaceZCylinder::normal(Position r) const
1,278,141✔
568
{
569
  return axis_aligned_cylinder_normal<2, 0, 1>(r, x0_, y0_);
1,278,141✔
570
}
571

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

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

589
//==============================================================================
590
// SurfaceSphere implementation
591
//==============================================================================
592

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

598
double SurfaceSphere::evaluate(Position r) const
522,105,773✔
599
{
600
  const double x = r.x - x0_;
522,105,773✔
601
  const double y = r.y - y0_;
522,105,773✔
602
  const double z = r.z - z0_;
522,105,773✔
603
  return x * x + y * y + z * z - radius_ * radius_;
522,105,773✔
604
}
605

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

615
  if (quad < 0.0) {
632,556,919✔
616
    // No intersection with sphere.
617
    return INFTY;
109,984,301✔
618

619
  } else if (coincident || std::abs(c) < FP_COINCIDENT) {
522,572,618!
620
    // Particle is on the sphere, thus one distance is positive/negative and
621
    // the other is zero. The sign of k determines if we are facing in or out.
622
    if (k >= 0.0) {
93,991,807✔
623
      return INFTY;
40,075,133✔
624
    } else {
625
      return -k + sqrt(quad);
53,916,674✔
626
    }
627

628
  } else if (c < 0.0) {
428,580,811✔
629
    // Particle is inside the sphere, thus one distance must be negative and
630
    // one must be positive. The positive distance will be the one with
631
    // negative sign on sqrt(quad)
632
    return -k + sqrt(quad);
377,763,162✔
633

634
  } else {
635
    // Particle is outside the sphere, thus both distances are either positive
636
    // or negative. If positive, the smaller distance is the one with positive
637
    // sign on sqrt(quad).
638
    const double d = -k - sqrt(quad);
50,817,649✔
639
    if (d < 0.0)
50,817,649✔
640
      return INFTY;
9,798,744✔
641
    return d;
41,018,905✔
642
  }
643
}
644

645
Direction SurfaceSphere::normal(Position r) const
19,516,760✔
646
{
647
  return {2.0 * (r.x - x0_), 2.0 * (r.y - y0_), 2.0 * (r.z - z0_)};
19,516,760✔
648
}
649

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

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

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

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

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

701
  double d;
702

703
  if (quad < 0.0) {
889,110!
704
    // No intersection with cone.
705
    return INFTY;
×
706

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

717
  } else {
718
    // Calculate both solutions to the quadratic.
719
    quad = sqrt(quad);
835,010✔
720
    d = (-k - quad) / a;
835,010✔
721
    const double b = (-k + quad) / a;
835,010✔
722

723
    // Determine the smallest positive solution.
724
    if (d < 0.0) {
835,010✔
725
      if (b > 0.0)
709,130✔
726
        d = b;
604,640✔
727
    } else {
728
      if (b > 0.0) {
125,880!
729
        if (b < d)
125,880!
730
          d = b;
125,880✔
731
      }
732
    }
733
  }
734

735
  // If the distance was negative, set boundary distance to infinity.
736
  if (d <= 0.0)
889,110✔
737
    return INFTY;
124,020✔
738
  return d;
765,090✔
739
}
740

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

755
//==============================================================================
756
// SurfaceXCone implementation
757
//==============================================================================
758

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

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

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

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

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

787
//==============================================================================
788
// SurfaceYCone implementation
789
//==============================================================================
790

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

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

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

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

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

819
//==============================================================================
820
// SurfaceZCone implementation
821
//==============================================================================
822

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

828
double SurfaceZCone::evaluate(Position r) const
296,230✔
829
{
830
  return axis_aligned_cone_evaluate<2, 0, 1>(r, z0_, x0_, y0_, radius_sq_);
296,230✔
831
}
832

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

839
Direction SurfaceZCone::normal(Position r) const
54,100✔
840
{
841
  return axis_aligned_cone_normal<2, 0, 1>(r, z0_, x0_, y0_, radius_sq_);
54,100✔
842
}
843

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

851
//==============================================================================
852
// SurfaceQuadric implementation
853
//==============================================================================
854

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

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

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

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

889
  double d;
890

891
  if (quad < 0.0) {
287,890!
892
    // No intersection with surface.
893
    return INFTY;
×
894

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

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

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

935
  // If the distance was negative, set boundary distance to infinity.
936
  if (d <= 0.0)
287,890!
937
    return INFTY;
×
938
  return d;
287,890✔
939
}
940

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

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

957
//==============================================================================
958
// Torus helper functions
959
//==============================================================================
960

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

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

984
  std::complex<double> roots[4];
24,122,330✔
985
  oqs::quartic_solver(coeff, roots);
24,122,330✔
986

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

1011
//==============================================================================
1012
// SurfaceXTorus implementation
1013
//==============================================================================
1014

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

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

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

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

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

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

1064
//==============================================================================
1065
// SurfaceYTorus implementation
1066
//==============================================================================
1067

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

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

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

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

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

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

1117
//==============================================================================
1118
// SurfaceZTorus implementation
1119
//==============================================================================
1120

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

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

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

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

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

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

1170
//==============================================================================
1171

1172
void read_surfaces(pugi::xml_node node)
6,790✔
1173
{
1174
  // Count the number of surfaces
1175
  int n_surfaces = 0;
6,790✔
1176
  for (pugi::xml_node surf_node : node.children("surface")) {
43,352✔
1177
    n_surfaces++;
36,562✔
1178
  }
1179

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

1192
      // Allocate and initialize the new surface
1193

1194
      if (surf_type == "x-plane") {
36,562✔
1195
        model::surfaces.push_back(make_unique<SurfaceXPlane>(surf_node));
8,976✔
1196

1197
      } else if (surf_type == "y-plane") {
27,586✔
1198
        model::surfaces.push_back(make_unique<SurfaceYPlane>(surf_node));
8,056✔
1199

1200
      } else if (surf_type == "z-plane") {
19,530✔
1201
        model::surfaces.push_back(make_unique<SurfaceZPlane>(surf_node));
6,214✔
1202

1203
      } else if (surf_type == "plane") {
13,316✔
1204
        model::surfaces.push_back(make_unique<SurfacePlane>(surf_node));
1,336✔
1205

1206
      } else if (surf_type == "x-cylinder") {
11,980✔
1207
        model::surfaces.push_back(make_unique<SurfaceXCylinder>(surf_node));
42✔
1208

1209
      } else if (surf_type == "y-cylinder") {
11,938✔
1210
        model::surfaces.push_back(make_unique<SurfaceYCylinder>(surf_node));
14✔
1211

1212
      } else if (surf_type == "z-cylinder") {
11,924✔
1213
        model::surfaces.push_back(make_unique<SurfaceZCylinder>(surf_node));
4,292✔
1214

1215
      } else if (surf_type == "sphere") {
7,632✔
1216
        model::surfaces.push_back(make_unique<SurfaceSphere>(surf_node));
7,400✔
1217

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

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

1224
      } else if (surf_type == "z-cone") {
232✔
1225
        model::surfaces.push_back(make_unique<SurfaceZCone>(surf_node));
14✔
1226

1227
      } else if (surf_type == "quadric") {
218✔
1228
        model::surfaces.push_back(make_unique<SurfaceQuadric>(surf_node));
14✔
1229

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

1233
      } else if (surf_type == "y-torus") {
150✔
1234
        model::surfaces.push_back(std::make_unique<SurfaceYTorus>(surf_node));
54✔
1235

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

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

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

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

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

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

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

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

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

1330
    // If the dot product is 1 (to within floating point precision) then the
1331
    // planes are parallel which indicates a translational periodic boundary
1332
    // condition.  Otherwise, it is a rotational periodic BC.
1333
    if (std::abs(1.0 - dot_prod) < FP_PRECISION) {
160✔
1334
      surf1.bc_ = make_unique<TranslationalPeriodicBC>(i_surf, j_surf);
104✔
1335
      surf2.bc_ = make_unique<TranslationalPeriodicBC>(i_surf, j_surf);
104✔
1336
    } else {
1337
      // check that both normals have at least one 0 component
1338
      if (std::abs(norm1.x) > FP_PRECISION &&
98✔
1339
          std::abs(norm1.y) > FP_PRECISION &&
98!
1340
          std::abs(norm1.z) > FP_PRECISION) {
14✔
UNCOV
1341
        fatal_error(fmt::format(
×
1342
          "The normal ({}) of the periodic surface ({}) does not contain any "
1343
          "component with a zero value. A RotationalPeriodicBC requires one "
1344
          "component which is zero for both plane normals.",
1345
          norm1, i_surf));
1346
      }
1347
      if (std::abs(norm2.x) > FP_PRECISION &&
84✔
1348
          std::abs(norm2.y) > FP_PRECISION &&
84!
1349
          std::abs(norm2.z) > FP_PRECISION) {
14✔
UNCOV
1350
        fatal_error(fmt::format(
×
1351
          "The normal ({}) of the periodic surface ({}) does not contain any "
1352
          "component with a zero value. A RotationalPeriodicBC requires one "
1353
          "component which is zero for both plane normals.",
1354
          norm2, j_surf));
1355
      }
1356
      // find common zero component, which indicates the periodic axis
1357
      RotationalPeriodicBC::PeriodicAxis axis;
1358
      if (std::abs(norm1.x) <= FP_PRECISION &&
70!
1359
          std::abs(norm2.x) <= FP_PRECISION) {
14✔
1360
        axis = RotationalPeriodicBC::PeriodicAxis::x;
14✔
1361
      } else if (std::abs(norm1.y) <= FP_PRECISION &&
70✔
1362
                 std::abs(norm2.y) <= FP_PRECISION) {
28✔
1363
        axis = RotationalPeriodicBC::PeriodicAxis::y;
14✔
1364
      } else if (std::abs(norm1.z) <= FP_PRECISION &&
56!
1365
                 std::abs(norm2.z) <= FP_PRECISION) {
28✔
1366
        axis = RotationalPeriodicBC::PeriodicAxis::z;
28✔
1367
      } else {
UNCOV
1368
        fatal_error(fmt::format(
×
1369
          "There is no component which is 0.0 in both normal vectors. This "
1370
          "indicates that the two planes are not periodic about the X, Y, or Z "
1371
          "axis, which is not supported."));
1372
      }
1373
      surf1.bc_ = make_unique<RotationalPeriodicBC>(i_surf, j_surf, axis);
56✔
1374
      surf2.bc_ = make_unique<RotationalPeriodicBC>(i_surf, j_surf, axis);
56✔
1375
    }
1376

1377
    // If albedo data is present in albedo map, set the boundary albedo.
1378
    if (albedo_map.count(surf1.id_)) {
160!
UNCOV
1379
      surf1.bc_->set_albedo(albedo_map[surf1.id_]);
×
1380
    }
1381
    if (albedo_map.count(surf2.id_)) {
160!
UNCOV
1382
      surf2.bc_->set_albedo(albedo_map[surf2.id_]);
×
1383
    }
1384
  }
1385
}
6,790✔
1386

1387
void free_memory_surfaces()
6,899✔
1388
{
1389
  model::surfaces.clear();
6,899✔
1390
  model::surface_map.clear();
6,899✔
1391
}
6,899✔
1392

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

© 2025 Coveralls, Inc