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

openmc-dev / openmc / 15648508499

14 Jun 2025 04:41AM UTC coverage: 85.162% (+0.04%) from 85.126%
15648508499

Pull #3346

github

web-flow
Merge 95507c5a3 into b11eb0265
Pull Request #3346: Allow specifying number of equiprobable angles for thermal scattering data generation

52373 of 61498 relevant lines covered (85.16%)

36636961.06 hits per line

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

71.99
/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,511✔
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,511✔
42
  if (coeffs_file.size() != coeffs.size()) {
36,511✔
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,511✔
50
  for (auto c : coeffs) {
111,245✔
51
    *c = coeffs_file[i++];
74,734✔
52
  }
53
}
36,511✔
54

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

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

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

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

80
    if (surf_bc == "transmission" || surf_bc == "transmit" || surf_bc.empty()) {
21,527✔
81
      // Leave the bc_ a nullptr
82
    } else if (surf_bc == "vacuum") {
21,527✔
83
      bc_ = make_unique<VacuumBC>();
10,616✔
84
    } else if (surf_bc == "reflective" || surf_bc == "reflect" ||
11,305✔
85
               surf_bc == "reflecting") {
394✔
86
      bc_ = make_unique<ReflectiveBC>();
10,517✔
87
    } else if (surf_bc == "white") {
394✔
88
      bc_ = make_unique<WhiteBC>();
96✔
89
    } else if (surf_bc == "periodic") {
298✔
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,527✔
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,527✔
114
}
36,511✔
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,509,309✔
128
  }
129
  return f > 0.0;
2,147,483,647✔
130
}
131

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

138
  // Reflect direction according to normal.
139
  return u.reflect(n);
1,206,414,522✔
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
29,029✔
164
{
165
  hid_t surf_group = create_group(group_id, fmt::format("surface {}", id_));
58,058✔
166

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

172
    if (bc_) {
28,435✔
173
      write_string(surf_group, "boundary_type", bc_->type(), false);
16,995✔
174
      bc_->to_hdf5(surf_group);
16,995✔
175

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

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

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

197
  to_hdf5_inner(surf_group);
29,029✔
198

199
  close_group(surf_group);
29,029✔
200
}
29,029✔
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;
602,304,033✔
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

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

2,147,483,647✔
224
SurfaceXPlane::SurfaceXPlane(pugi::xml_node surf_node) : Surface(surf_node)
55,672,948✔
225
{
2,147,483,647✔
226
  read_coeffs(surf_node, id_, {&x0_});
2,147,483,647✔
227
}
1,141,260,259✔
228

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

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

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

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

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

8,695✔
260
//==============================================================================
8,695✔
261
// SurfaceYPlane implementation
262
//==============================================================================
1,514,605,647✔
263

264
SurfaceYPlane::SurfaceYPlane(pugi::xml_node surf_node) : Surface(surf_node)
1,514,605,647✔
265
{
266
  read_coeffs(surf_node, id_, {&y0_});
267
}
2,147,483,647✔
268

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

274
double SurfaceYPlane::distance(Position r, Direction u, bool coincident) const
263,077,221✔
275
{
276
  return axis_aligned_plane_distance<1>(r, u, coincident, y0_);
277
}
6,717✔
278

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

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

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

7,751✔
300
//==============================================================================
7,751✔
301
// SurfaceZPlane implementation
302
//==============================================================================
1,512,842,161✔
303

304
SurfaceZPlane::SurfaceZPlane(pugi::xml_node surf_node) : Surface(surf_node)
1,512,842,161✔
305
{
306
  read_coeffs(surf_node, id_, {&z0_});
307
}
2,147,483,647✔
308

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

314
double SurfaceZPlane::distance(Position r, Direction u, bool coincident) const
285,713,559✔
315
{
316
  return axis_aligned_plane_distance<2>(r, u, coincident, z0_);
317
}
6,125✔
318

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

324
void SurfaceZPlane::to_hdf5_inner(hid_t group_id) const
242✔
325
{
326
  write_string(group_id, "type", "z-plane", false);
242✔
327
  array<double, 1> coeffs {{z0_}};
121✔
328
  write_dataset(group_id, "coefficients", coeffs);
329
}
121✔
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
  }
5,985✔
338
}
339

5,985✔
340
//==============================================================================
5,985✔
341
// SurfacePlane implementation
342
//==============================================================================
489,482,671✔
343

344
SurfacePlane::SurfacePlane(pugi::xml_node surf_node) : Surface(surf_node)
489,482,671✔
345
{
346
  read_coeffs(surf_node, id_, {&A_, &B_, &C_, &D_});
347
}
2,147,483,647✔
348

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

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

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

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

1,508✔
380
//==============================================================================
1,508✔
381
// Generic functions for x-, y-, and z-, cylinders
382
//==============================================================================
201,595,679✔
383

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

543,044,376✔
396
// The first template parameter indicates which axis the cylinder is aligned to.
253,639,474✔
397
// The other two parameters indicate the other two axes.  offset1 and offset2
289,404,902✔
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,
401
  double offset1, double offset2, double radius)
5,787,326✔
402
{
403
  const double a = 1.0 - u.get<i1>() * u.get<i1>(); // u^2 + v^2
5,787,326✔
404
  if (a == 0.0)
405
    return INFTY;
406

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

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

417
  } else if (coincident || std::abs(c) < FP_COINCIDENT) {
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) {
1,408,640,025✔
422
      return INFTY;
423
    } else {
424
      return (-k + sqrt(quad)) / a;
1,408,640,025✔
425
    }
1,408,640,025✔
426

1,408,640,025✔
427
  } else if (c < 0.0) {
428
    // Particle is inside the cylinder, thus one distance must be negative
1,407,279,538✔
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;
1,407,279,538✔
432

1,407,279,538✔
433
  } else {
1,407,279,538✔
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;
438
    if (d < 0.0)
×
439
      return INFTY;
×
440
    return d;
×
441
  }
442
}
1,360,487✔
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
1,360,487✔
446
// should correspond with i2 and i3, respectively.
1,360,487✔
447
template<int i1, int i2, int i3>
1,360,487✔
448
Direction axis_aligned_cylinder_normal(
449
  Position r, double offset1, double offset2)
450
{
451
  Direction u;
452
  u.get<i2>() = 2.0 * (r.get<i2>() - offset1);
453
  u.get<i3>() = 2.0 * (r.get<i3>() - offset2);
454
  u.get<i1>() = 0.0;
1,634,846,539✔
455
  return u;
456
}
457

1,634,846,539✔
458
//==============================================================================
1,634,846,539✔
459
// SurfaceXCylinder implementation
809,996✔
460
//==============================================================================
461

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

1,634,036,543✔
468
double SurfaceXCylinder::evaluate(Position r) const
469
{
269,323,328✔
470
  return axis_aligned_cylinder_evaluate<1, 2>(r, y0_, z0_, radius_);
471
}
1,364,713,215✔
472

473
double SurfaceXCylinder::distance(
474
  Position r, Direction u, bool coincident) const
475
{
621,105,068✔
476
  return axis_aligned_cylinder_distance<0, 1, 2>(
311,632,428✔
477
    r, u, coincident, y0_, z0_, radius_);
478
}
309,472,640✔
479

480
Direction SurfaceXCylinder::normal(Position r) const
481
{
743,608,147✔
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
300,561,719✔
486
{
487
  write_string(group_id, "type", "x-cylinder", false);
488
  array<double, 3> coeffs {{y0_, z0_, radius_}};
489
  write_dataset(group_id, "coefficients", coeffs);
490
}
491

443,046,428✔
492
BoundingBox SurfaceXCylinder::bounding_box(bool pos_side) const
443,046,428✔
493
{
63,749,397✔
494
  if (!pos_side) {
379,297,031✔
495
    return {-INFTY, INFTY, y0_ - radius_, y0_ + radius_, z0_ - radius_,
496
      z0_ + radius_};
497
  } else {
1,633,763,303✔
498
    return {};
499
  }
500
}
1,633,763,303✔
501
//==============================================================================
1,633,763,303✔
502
// SurfaceYCylinder implementation
369,996✔
503
//==============================================================================
504

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

1,633,393,307✔
511
double SurfaceYCylinder::evaluate(Position r) const
512
{
269,323,328✔
513
  return axis_aligned_cylinder_evaluate<0, 2>(r, x0_, z0_, radius_);
514
}
1,364,069,979✔
515

516
double SurfaceYCylinder::distance(
517
  Position r, Direction u, bool coincident) const
518
{
621,105,068✔
519
  return axis_aligned_cylinder_distance<1, 0, 2>(
311,632,428✔
520
    r, u, coincident, x0_, z0_, radius_);
521
}
309,472,640✔
522

523
Direction SurfaceYCylinder::normal(Position r) const
524
{
742,964,911✔
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
299,918,483✔
529
{
530
  write_string(group_id, "type", "y-cylinder", false);
531
  array<double, 3> coeffs {{x0_, z0_, radius_}};
532
  write_dataset(group_id, "coefficients", coeffs);
533
}
534

443,046,428✔
535
BoundingBox SurfaceYCylinder::bounding_box(bool pos_side) const
443,046,428✔
536
{
63,749,397✔
537
  if (!pos_side) {
379,297,031✔
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)
×
550
  : Surface(surf_node)
×
551
{
×
552
  read_coeffs(surf_node, id_, {&x0_, &y0_, &radius_});
553
}
×
554

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

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

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

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

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

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

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

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

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

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

619
  } else if (coincident || std::abs(c) < FP_COINCIDENT) {
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) {
×
623
      return INFTY;
×
624
    } else {
625
      return -k + sqrt(quad);
626
    }
627

628
  } else if (c < 0.0) {
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)
1,387,667✔
632
    return -k + sqrt(quad);
633

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

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

×
650
void SurfaceSphere::to_hdf5_inner(hid_t group_id) const
651
{
652
  write_string(group_id, "type", "sphere", false);
×
653
  array<double, 4> coeffs {{x0_, y0_, z0_, radius_}};
×
654
  write_dataset(group_id, "coefficients", coeffs);
×
655
}
×
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,
32✔
673
// and offset3 should correspond with i1, i2, and i3, respectively.
32✔
674
template<int i1, int i2, int i3>
675
double axis_aligned_cone_evaluate(
32✔
676
  Position r, double offset1, double offset2, double offset3, double radius_sq)
32✔
677
{
678
  const double r1 = r.get<i1>() - offset1;
1,360,487✔
679
  const double r2 = r.get<i2>() - offset2;
680
  const double r3 = r.get<i3>() - offset3;
1,360,487✔
681
  return r2 * r2 + r3 * r3 - radius_sq * r1 * r1;
682
}
683

1,083,236✔
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.
1,083,236✔
687
template<int i1, int i2, int i3>
1,083,236✔
688
double axis_aligned_cone_distance(Position r, Direction u, bool coincident,
689
  double offset1, double offset2, double offset3, double radius_sq)
690
{
×
691
  const double r1 = r.get<i1>() - offset1;
692
  const double r2 = r.get<i2>() - offset2;
×
693
  const double r3 = r.get<i3>() - offset3;
694
  const double a = u.get<i2>() * u.get<i2>() + u.get<i3>() * u.get<i3>() -
695
                   radius_sq * u.get<i1>() * u.get<i1>();
22✔
696
  const double k =
697
    r2 * u.get<i2>() + r3 * u.get<i3>() - radius_sq * r1 * u.get<i1>();
22✔
698
  const double c = r2 * r2 + r3 * r3 - radius_sq * r1 * r1;
22✔
699
  double quad = k * k - a * c;
22✔
700

22✔
701
  double d;
702

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

×
707
  } else if (coincident || std::abs(c) < FP_COINCIDENT) {
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) {
712
      d = (-k - sqrt(quad)) / a;
713
    } else {
714
      d = (-k + sqrt(quad)) / a;
715
    }
×
716

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

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

735
  // If the distance was negative, set boundary distance to infinity.
×
736
  if (d <= 0.0)
737
    return INFTY;
738
  return d;
×
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(
×
746
  Position r, double offset1, double offset2, double offset3, double radius_sq)
747
{
×
748
  Direction u;
×
749
  u.get<i1>() = -2.0 * radius_sq * (r.get<i1>() - offset1);
×
750
  u.get<i2>() = 2.0 * (r.get<i2>() - offset2);
751
  u.get<i3>() = 2.0 * (r.get<i3>() - offset3);
×
752
  return u;
753
}
754

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

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

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

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

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

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

3,289✔
787
//==============================================================================
3,289✔
788
// SurfaceYCone implementation
789
//==============================================================================
44✔
790

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

22✔
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>(
7,743✔
804
    r, u, coincident, y0_, x0_, z0_, radius_sq_);
805
}
7,743✔
806

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

542,550,209✔
812
void SurfaceYCone::to_hdf5_inner(hid_t group_id) const
542,550,209✔
813
{
542,550,209✔
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);
600,506,637✔
817
}
818

600,506,637✔
819
//==============================================================================
600,506,637✔
820
// SurfaceZCone implementation
600,506,637✔
821
//==============================================================================
600,506,637✔
822

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

116,820,193✔
828
double SurfaceZCone::evaluate(Position r) const
829
{
483,686,444✔
830
  return axis_aligned_cone_evaluate<2, 0, 1>(r, z0_, x0_, y0_, radius_sq_);
831
}
832

67,824,461✔
833
double SurfaceZCone::distance(Position r, Direction u, bool coincident) const
33,153,415✔
834
{
835
  return axis_aligned_cone_distance<2, 0, 1>(
34,671,046✔
836
    r, u, coincident, z0_, x0_, y0_, radius_sq_);
837
}
838

415,861,983✔
839
Direction SurfaceZCone::normal(Position r) const
840
{
841
  return axis_aligned_cone_normal<2, 0, 1>(r, z0_, x0_, y0_, radius_sq_);
842
}
372,631,088✔
843

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

9,371,024✔
851
//==============================================================================
33,859,871✔
852
// SurfaceQuadric implementation
853
//==============================================================================
854

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

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

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

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

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

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

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

×
908
  } else if (a == 0.0) {
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);
920
    d = (-k - quad) / a;
921
    double b = (-k + quad) / a;
922

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

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

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

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

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

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

974
  // Coefficient for equation: a t^4 + b t^3 + c t^2 + d t + e = 0. If the point
963,358✔
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];
963,358✔
978
  coeff[0] = coincident ? 0.0 : c0 * c0 - c0p;
963,358✔
979
  coeff[1] = 2 * c0 * c1 - c1p;
963,358✔
980
  coeff[2] = c1 * c1 + 2 * c0 * c2 - c2p;
963,358✔
981
  coeff[3] = 2 * c1 * c2;
963,358✔
982
  coeff[4] = c2 * c2;
963,358✔
983

963,358✔
984
  std::complex<double> roots[4];
963,358✔
985
  oqs::quartic_solver(coeff, roots);
963,358✔
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
963,358✔
990
  // zero.
991
  double distance = INFTY;
×
992
  double cutoff = coincident ? TORUS_TOL : 0.0;
993
  for (int i = 0; i < 4; ++i) {
963,358✔
994
    if (roots[i].imag() == 0) {
995
      double root = roots[i].real();
996
      if (root > cutoff && root < distance) {
997
        // Avoid roots corresponding to internal surfaces
60,566✔
998
        double s1 = x1 + u1 * root;
×
999
        double s2 = x2 + u2 * root;
1000
        double s3 = x3 + u3 * root;
60,566✔
1001
        double check = D * s3 * s3 + s1 * s1 + s2 * s2 + A * A - C * C;
1002
        if (check >= 0) {
1003
          distance = root;
1004
        }
1005
      }
902,792✔
1006
    }
902,792✔
1007
  }
902,792✔
1008
  return distance;
1009
}
1010

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

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

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

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

×
1036
double SurfaceXTorus::distance(Position r, Direction u, bool coincident) const
×
1037
{
×
1038
  double x = r.x - x0_;
1039
  double y = r.y - y0_;
1040
  double z = r.z - z0_;
1041
  return torus_distance(y, z, x, u.y, u.z, u.x, A_, B_, C_, coincident);
×
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)
×
1069
{
1070
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &A_, &B_, &C_});
1071
}
1072

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

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

×
1089
double SurfaceYTorus::distance(Position r, Direction u, bool coincident) const
×
1090
{
1091
  double x = r.x - x0_;
1092
  double y = r.y - y0_;
1093
  double z = r.z - z0_;
×
1094
  return torus_distance(x, z, y, u.x, u.z, u.y, A_, B_, C_, coincident);
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)
1122
{
1123
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &A_, &B_, &C_});
1124
}
1125

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

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

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

60,566✔
1150
Direction SurfaceZTorus::normal(Position r) const
60,566✔
1151
{
60,566✔
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)
1173
{
1174
  // Count the number of surfaces
1175
  int n_surfaces = 0;
1176
  for (pugi::xml_node surf_node : node.children("surface")) {
×
1177
    n_surfaces++;
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);
1183
  std::set<std::pair<int, int>> periodic_pairs;
×
1184
  std::unordered_map<int, double> albedo_map;
1185
  {
1186
    pugi::xml_node surf_node;
×
1187
    int i_surf;
1188
    for (surf_node = node.child("surface"), i_surf = 0; surf_node;
×
1189
         surf_node = surf_node.next_sibling("surface"), i_surf++) {
×
1190
      std::string surf_type = get_node_value(surf_node, "type", true, true);
1191

1192
      // Allocate and initialize the new surface
×
1193

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

34,540✔
1323
    // Compute the dot product of the surface normals
1324
    Direction norm1 = surf1.normal({0, 0, 0});
1325
    Direction norm2 = surf2.normal({0, 0, 0});
277,970✔
1326
    norm1 /= norm1.norm();
1327
    norm2 /= norm2.norm();
1328
    double dot_prod = norm1.dot(norm2);
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) {
×
1334
      surf1.bc_ = make_unique<TranslationalPeriodicBC>(i_surf, j_surf);
1335
      surf2.bc_ = make_unique<TranslationalPeriodicBC>(i_surf, j_surf);
1336
    } else {
277,970✔
1337
      surf1.bc_ = make_unique<RotationalPeriodicBC>(i_surf, j_surf);
277,970✔
1338
      surf2.bc_ = make_unique<RotationalPeriodicBC>(i_surf, j_surf);
277,970✔
1339
    }
1340

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

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

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