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

openmc-dev / openmc / 17637711185

11 Sep 2025 07:44AM UTC coverage: 85.193% (-0.02%) from 85.209%
17637711185

Pull #3566

github

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

268 of 326 new or added lines in 9 files covered. (82.21%)

175 existing lines in 3 files now uncovered.

53205 of 62452 relevant lines covered (85.19%)

37768560.15 hits per line

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

70.93
/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(
41,363✔
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");
41,363✔
42
  if (coeffs_file.size() != coeffs.size()) {
41,363✔
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;
41,363✔
50
  for (auto c : coeffs) {
127,763✔
51
    *c = coeffs_file[i++];
86,400✔
52
  }
53
}
41,363✔
54

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

59
Surface::Surface() {} // empty constructor
1,956✔
60

61
Surface::Surface(pugi::xml_node surf_node)
41,363✔
62
{
63
  if (check_for_node(surf_node, "id")) {
41,363✔
64
    id_ = std::stoi(get_node_value(surf_node, "id"));
41,363✔
65
    if (contains(settings::source_write_surf_id, id_) ||
81,938✔
66
        settings::source_write_surf_id.empty()) {
40,575✔
67
      surf_source_ = true;
40,321✔
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")) {
41,363✔
74
    name_ = get_node_value(surf_node, "name", false);
8,894✔
75
  }
76

77
  if (check_for_node(surf_node, "boundary")) {
41,363✔
78
    std::string surf_bc = get_node_value(surf_node, "boundary", true, true);
22,864✔
79

80
    if (surf_bc == "transmission" || surf_bc == "transmit" || surf_bc.empty()) {
22,864✔
81
      // Leave the bc_ a nullptr
82
    } else if (surf_bc == "vacuum") {
22,864✔
83
      bc_ = make_unique<VacuumBC>();
11,531✔
84
    } else if (surf_bc == "reflective" || surf_bc == "reflect" ||
11,727✔
85
               surf_bc == "reflecting") {
394✔
86
      bc_ = make_unique<ReflectiveBC>();
10,939✔
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_) {
22,864✔
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
  }
22,864✔
114
}
41,363✔
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,414,649✔
128
  }
129
  return f > 0.0;
2,147,483,647✔
130
}
131

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

138
  // Reflect direction according to normal.
139
  return u.reflect(n);
1,203,968,472✔
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
34,181✔
164
{
165
  hid_t surf_group = create_group(group_id, fmt::format("surface {}", id_));
68,362✔
166

167
  if (geom_type() == GeometryType::DAG) {
34,181✔
168
    write_string(surf_group, "geom_type", "dagmc", false);
1,694✔
169
  } else if (geom_type() == GeometryType::CSG) {
32,487✔
170
    write_string(surf_group, "geom_type", "csg", false);
32,487✔
171

172
    if (bc_) {
32,487✔
173
      write_string(surf_group, "boundary_type", bc_->type(), false);
18,138✔
174
      bc_->to_hdf5(surf_group);
18,138✔
175

176
      // write periodic surface ID
177
      if (bc_->type() == "periodic") {
18,138✔
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);
14,349✔
190
    }
191
  }
192

193
  if (!name_.empty()) {
34,181✔
194
    write_string(surf_group, "name", name_, false);
6,623✔
195
  }
196

197
  to_hdf5_inner(surf_group);
34,181✔
198

199
  close_group(surf_group);
34,181✔
200
}
34,181✔
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;
606,057,700✔
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)
54,770,166✔
225
{
2,147,483,647✔
226
  read_coeffs(surf_node, id_, {&x0_});
2,147,483,647✔
227
}
1,137,939,012✔
228

1,214,390,299✔
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
{
285,725,822✔
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);
265,561,712✔
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
  }
9,906✔
258
}
259

9,906✔
260
bool SurfaceXPlane::triso_in_mesh(
9,906✔
261
  vector<double> mesh_center, vector<double> lattice_pitch) const
262
{
1,578,697,115✔
263
  return false;
264
}
1,578,697,115✔
265

266
//==============================================================================
267
// SurfaceYPlane implementation
2,147,483,647✔
268
//==============================================================================
269
bool SurfaceYPlane::triso_in_mesh(
2,147,483,647✔
270
  vector<double> mesh_center, vector<double> lattice_pitch) const
271
{
272
  return false;
265,624,290✔
273
}
274
SurfaceYPlane::SurfaceYPlane(pugi::xml_node surf_node) : Surface(surf_node)
265,624,290✔
275
{
276
  read_coeffs(surf_node, id_, {&y0_});
277
}
7,866✔
278

279
double SurfaceYPlane::evaluate(Position r) const
7,866✔
280
{
7,866✔
281
  return r.y - y0_;
7,866✔
282
}
7,866✔
283

284
double SurfaceYPlane::distance(Position r, Direction u, bool coincident) const
644✔
285
{
286
  return axis_aligned_plane_distance<1>(r, u, coincident, y0_);
644✔
287
}
322✔
288

289
Direction SurfaceYPlane::normal(Position r) const
322✔
290
{
291
  return {0., 1., 0.};
292
}
UNCOV
293

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

301
BoundingBox SurfaceYPlane::bounding_box(bool pos_side) const
UNCOV
302
{
×
303
  if (pos_side) {
304
    return {-INFTY, INFTY, y0_, INFTY, -INFTY, INFTY};
UNCOV
305
  } else {
×
306
    return {-INFTY, INFTY, -INFTY, y0_, -INFTY, INFTY};
307
  }
8,891✔
308
}
309

8,891✔
310
//==============================================================================
8,891✔
311
// SurfaceZPlane implementation
312
//==============================================================================
1,570,607,497✔
313
bool SurfaceZPlane::triso_in_mesh(
314
  vector<double> mesh_center, vector<double> lattice_pitch) const
1,570,607,497✔
315
{
316
  return false;
317
}
2,147,483,647✔
318
SurfaceZPlane::SurfaceZPlane(pugi::xml_node surf_node) : Surface(surf_node)
319
{
2,147,483,647✔
320
  read_coeffs(surf_node, id_, {&z0_});
321
}
322

285,981,899✔
323
double SurfaceZPlane::evaluate(Position r) const
324
{
285,981,899✔
325
  return r.z - z0_;
326
}
327

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

7,203✔
333
Direction SurfaceZPlane::normal(Position r) const
334
{
644✔
335
  return {0., 0., 1.};
336
}
644✔
337

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

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

6,189✔
354
//==============================================================================
6,189✔
355
// SurfacePlane implementation
356
//==============================================================================
530,539,727✔
357
bool SurfacePlane::triso_in_mesh(
358
  vector<double> mesh_center, vector<double> lattice_pitch) const
530,539,727✔
359
{
360
  return false;
361
}
2,147,483,647✔
362

363
SurfacePlane::SurfacePlane(pugi::xml_node surf_node) : Surface(surf_node)
2,147,483,647✔
364
{
365
  read_coeffs(surf_node, id_, {&A_, &B_, &C_, &D_});
366
}
54,945,732✔
367

368
double SurfacePlane::evaluate(Position r) const
54,945,732✔
369
{
370
  return A_ * r.x + B_ * r.y + C_ * r.z - D_;
371
}
5,243✔
372

373
double SurfacePlane::distance(Position r, Direction u, bool coincident) const
5,243✔
374
{
5,243✔
375
  const double f = A_ * r.x + B_ * r.y + C_ * r.z - D_;
5,243✔
376
  const double projection = A_ * u.x + B_ * u.y + C_ * u.z;
5,243✔
377
  if (coincident || std::abs(f) < FP_COINCIDENT || projection == 0.0) {
UNCOV
378
    return INFTY;
×
379
  } else {
UNCOV
380
    const double d = -f / projection;
×
UNCOV
381
    if (d < 0.0)
×
382
      return INFTY;
383
    return d;
×
384
  }
385
}
386

387
Direction SurfacePlane::normal(Position r) const
388
{
389
  return {A_, B_, C_};
UNCOV
390
}
×
391

392
void SurfacePlane::to_hdf5_inner(hid_t group_id) const
UNCOV
393
{
×
394
  write_string(group_id, "type", "plane", false);
395
  array<double, 4> coeffs {{A_, B_, C_, D_}};
396
  write_dataset(group_id, "coefficients", coeffs);
1,588✔
397
}
398

1,588✔
399
//==============================================================================
1,588✔
400
// Generic functions for x-, y-, and z-, cylinders
401
//==============================================================================
202,909,986✔
402

403
// The template parameters indicate the axes perpendicular to the axis of the
202,909,986✔
404
// cylinder.  offset1 and offset2 should correspond with i1 and i2,
405
// respectively.
406
template<int i1, int i2>
583,908,110✔
407
double axis_aligned_cylinder_evaluate(
408
  Position r, double offset1, double offset2, double radius)
583,908,110✔
409
{
583,908,110✔
410
  const double r1 = r.get<i1>() - offset1;
583,908,110✔
411
  const double r2 = r.get<i2>() - offset2;
35,742,154✔
412
  return r1 * r1 + r2 * r2 - radius * radius;
413
}
548,165,956✔
414

548,165,956✔
415
// The first template parameter indicates which axis the cylinder is aligned to.
256,137,494✔
416
// The other two parameters indicate the other two axes.  offset1 and offset2
292,028,462✔
417
// should correspond with i2 and i3, respectively.
418
template<int i1, int i2, int i3>
419
double axis_aligned_cylinder_distance(Position r, Direction u, bool coincident,
420
  double offset1, double offset2, double radius)
5,912,867✔
421
{
422
  const double a = 1.0 - u.get<i1>() * u.get<i1>(); // u^2 + v^2
5,912,867✔
423
  if (a == 0.0)
424
    return INFTY;
425

1,114✔
426
  const double r2 = r.get<i2>() - offset1;
427
  const double r3 = r.get<i3>() - offset2;
1,114✔
428
  const double k = r2 * u.get<i2>() + r3 * u.get<i3>();
1,114✔
429
  const double c = r2 * r2 + r3 * r3 - radius * radius;
1,114✔
430
  const double quad = k * k - a * c;
1,114✔
431

432
  if (quad < 0.0) {
433
    // No intersection with cylinder.
434
    return INFTY;
435

436
  } else if (coincident || std::abs(c) < FP_COINCIDENT) {
437
    // Particle is on the cylinder, thus one distance is positive/negative
438
    // and the other is zero. The sign of k determines if we are facing in or
439
    // out.
440
    if (k >= 0.0) {
1,388,251,872✔
441
      return INFTY;
442
    } else {
443
      return (-k + sqrt(quad)) / a;
1,388,251,872✔
444
    }
1,388,251,872✔
445

1,388,251,872✔
446
  } else if (c < 0.0) {
447
    // Particle is inside the cylinder, thus one distance must be negative
1,386,891,385✔
448
    // and one must be positive. The positive distance will be the one with
449
    // negative sign on sqrt(quad).
450
    return (-k + sqrt(quad)) / a;
1,386,891,385✔
451

1,386,891,385✔
452
  } else {
1,386,891,385✔
453
    // Particle is outside the cylinder, thus both distances are either
454
    // positive or negative. If positive, the smaller distance is the one
×
455
    // with positive sign on sqrt(quad).
456
    const double d = (-k - sqrt(quad)) / a;
457
    if (d < 0.0)
×
458
      return INFTY;
×
459
    return d;
×
460
  }
461
}
1,360,487✔
462

463
// The first template parameter indicates which axis the cylinder is aligned to.
464
// The other two parameters indicate the other two axes.  offset1 and offset2
1,360,487✔
465
// should correspond with i2 and i3, respectively.
1,360,487✔
466
template<int i1, int i2, int i3>
1,360,487✔
467
Direction axis_aligned_cylinder_normal(
468
  Position r, double offset1, double offset2)
469
{
470
  Direction u;
471
  u.get<i2>() = 2.0 * (r.get<i2>() - offset1);
472
  u.get<i3>() = 2.0 * (r.get<i3>() - offset2);
473
  u.get<i1>() = 0.0;
1,623,974,035✔
474
  return u;
475
}
476

1,623,974,035✔
477
//==============================================================================
1,623,974,035✔
478
// SurfaceXCylinder implementation
828,641✔
479
//==============================================================================
480

1,623,145,394✔
481
bool SurfaceXCylinder::triso_in_mesh(
1,623,145,394✔
482
  vector<double> mesh_center, vector<double> lattice_pitch) const
1,623,145,394✔
483
{
1,623,145,394✔
484
  return false;
1,623,145,394✔
485
}
486

1,623,145,394✔
487
SurfaceXCylinder::SurfaceXCylinder(pugi::xml_node surf_node)
488
  : Surface(surf_node)
268,288,214✔
489
{
490
  read_coeffs(surf_node, id_, {&y0_, &z0_, &radius_});
1,354,857,180✔
491
}
492

493
double SurfaceXCylinder::evaluate(Position r) const
494
{
618,443,609✔
495
  return axis_aligned_cylinder_evaluate<1, 2>(r, y0_, z0_, radius_);
310,286,975✔
496
}
497

308,156,634✔
498
double SurfaceXCylinder::distance(
499
  Position r, Direction u, bool coincident) const
500
{
736,413,571✔
501
  return axis_aligned_cylinder_distance<0, 1, 2>(
502
    r, u, coincident, y0_, z0_, radius_);
503
}
504

296,282,451✔
505
Direction SurfaceXCylinder::normal(Position r) const
506
{
507
  return axis_aligned_cylinder_normal<0, 1, 2>(r, y0_, z0_);
508
}
509

510
void SurfaceXCylinder::to_hdf5_inner(hid_t group_id) const
440,131,120✔
511
{
440,131,120✔
512
  write_string(group_id, "type", "x-cylinder", false);
62,955,835✔
513
  array<double, 3> coeffs {{y0_, z0_, radius_}};
377,175,285✔
514
  write_dataset(group_id, "coefficients", coeffs);
515
}
516

1,622,890,799✔
517
BoundingBox SurfaceXCylinder::bounding_box(bool pos_side) const
518
{
519
  if (!pos_side) {
1,622,890,799✔
520
    return {-INFTY, INFTY, y0_ - radius_, y0_ + radius_, z0_ - radius_,
1,622,890,799✔
521
      z0_ + radius_};
388,641✔
522
  } else {
523
    return {};
1,622,502,158✔
524
  }
1,622,502,158✔
525
}
1,622,502,158✔
526
//==============================================================================
1,622,502,158✔
527
// SurfaceYCylinder implementation
1,622,502,158✔
528
//==============================================================================
529

1,622,502,158✔
530
bool SurfaceYCylinder::triso_in_mesh(
531
  vector<double> mesh_center, vector<double> lattice_pitch) const
268,288,214✔
532
{
533
  return false;
1,354,213,944✔
534
}
535

536
SurfaceYCylinder::SurfaceYCylinder(pugi::xml_node surf_node)
537
  : Surface(surf_node)
618,443,609✔
538
{
310,286,975✔
539
  read_coeffs(surf_node, id_, {&x0_, &z0_, &radius_});
540
}
308,156,634✔
541

542
double SurfaceYCylinder::evaluate(Position r) const
543
{
735,770,335✔
544
  return axis_aligned_cylinder_evaluate<0, 2>(r, x0_, z0_, radius_);
545
}
546

547
double SurfaceYCylinder::distance(
295,639,215✔
548
  Position r, Direction u, bool coincident) const
549
{
550
  return axis_aligned_cylinder_distance<1, 0, 2>(
551
    r, u, coincident, x0_, z0_, radius_);
552
}
553

440,131,120✔
554
Direction SurfaceYCylinder::normal(Position r) const
440,131,120✔
555
{
62,955,835✔
556
  return axis_aligned_cylinder_normal<1, 0, 2>(r, x0_, z0_);
377,175,285✔
557
}
558

UNCOV
559
void SurfaceYCylinder::to_hdf5_inner(hid_t group_id) const
×
560
{
561
  write_string(group_id, "type", "y-cylinder", false);
UNCOV
562
  array<double, 3> coeffs {{x0_, z0_, radius_}};
×
UNCOV
563
  write_dataset(group_id, "coefficients", coeffs);
×
UNCOV
564
}
×
565

UNCOV
566
BoundingBox SurfaceYCylinder::bounding_box(bool pos_side) const
×
UNCOV
567
{
×
UNCOV
568
  if (!pos_side) {
×
UNCOV
569
    return {x0_ - radius_, x0_ + radius_, -INFTY, INFTY, z0_ - radius_,
×
UNCOV
570
      z0_ + radius_};
×
571
  } else {
UNCOV
572
    return {};
×
573
  }
574
}
×
575

576
//==============================================================================
×
577
// SurfaceZCylinder implementation
578
//==============================================================================
579

NEW
580
bool SurfaceZCylinder::triso_in_mesh(
×
NEW
581
  vector<double> mesh_center, vector<double> lattice_pitch) const
×
582
{
NEW
583
  return false;
×
584
}
585

586
SurfaceZCylinder::SurfaceZCylinder(pugi::xml_node surf_node)
×
587
  : Surface(surf_node)
588
{
589
  read_coeffs(surf_node, id_, {&x0_, &y0_, &radius_});
590
}
×
591

592
double SurfaceZCylinder::evaluate(Position r) const
593
{
594
  return axis_aligned_cylinder_evaluate<0, 1>(r, x0_, y0_, radius_);
595
}
UNCOV
596

×
UNCOV
597
double SurfaceZCylinder::distance(
×
598
  Position r, Direction u, bool coincident) const
×
599
{
×
600
  return axis_aligned_cylinder_distance<2, 0, 1>(
601
    r, u, coincident, x0_, y0_, radius_);
602
}
1,083,236✔
603

604
Direction SurfaceZCylinder::normal(Position r) const
605
{
1,083,236✔
606
  return axis_aligned_cylinder_normal<2, 0, 1>(r, x0_, y0_);
1,083,236✔
607
}
440,000✔
608

609
void SurfaceZCylinder::to_hdf5_inner(hid_t group_id) const
643,236✔
610
{
643,236✔
611
  write_string(group_id, "type", "z-cylinder", false);
643,236✔
612
  array<double, 3> coeffs {{x0_, y0_, radius_}};
643,236✔
613
  write_dataset(group_id, "coefficients", coeffs);
643,236✔
614
}
615

643,236✔
616
BoundingBox SurfaceZCylinder::bounding_box(bool pos_side) const
617
{
×
618
  if (!pos_side) {
619
    return {x0_ - radius_, x0_ + radius_, y0_ - radius_, y0_ + radius_, -INFTY,
643,236✔
620
      INFTY};
621
  } else {
622
    return {};
UNCOV
623
  }
×
UNCOV
624
}
×
625

UNCOV
626
//==============================================================================
×
627
// SurfaceSphere implementation
628
//==============================================================================
629

643,236✔
630
SurfaceSphere::SurfaceSphere(pugi::xml_node surf_node) : Surface(surf_node)
631
{
632
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &radius_});
633
}
643,236✔
634

635
double SurfaceSphere::evaluate(Position r) const
636
{
637
  const double x = r.x - x0_;
638
  const double y = r.y - y0_;
UNCOV
639
  const double z = r.z - z0_;
×
UNCOV
640
  return x * x + y * y + z * z - radius_ * radius_;
×
641
}
×
642

×
643
double SurfaceSphere::distance(Position r, Direction u, bool coincident) const
644
{
645
  const double x = r.x - x0_;
646
  const double y = r.y - y0_;
647
  const double z = r.z - z0_;
648
  const double k = x * u.x + y * u.y + z * u.z;
649
  const double c = x * x + y * y + z * z - radius_ * radius_;
650
  const double quad = k * k - c;
1,387,667✔
651

652
  if (quad < 0.0) {
653
    // No intersection with sphere.
1,387,667✔
654
    return INFTY;
1,387,667✔
655

1,387,667✔
656
  } else if (coincident || std::abs(c) < FP_COINCIDENT) {
1,387,667✔
657
    // Particle is on the sphere, thus one distance is positive/negative and
1,387,667✔
658
    // the other is zero. The sign of k determines if we are facing in or out.
659
    if (k >= 0.0) {
1,387,667✔
660
      return INFTY;
661
    } else {
662
      return -k + sqrt(quad);
1,387,667✔
663
    }
1,387,667✔
664

1,387,667✔
665
  } else if (c < 0.0) {
1,387,667✔
666
    // Particle is inside the sphere, thus one distance must be negative and
1,387,667✔
667
    // one must be positive. The positive distance will be the one with
UNCOV
668
    // negative sign on sqrt(quad)
×
669
    return -k + sqrt(quad);
670

UNCOV
671
  } else {
×
UNCOV
672
    // Particle is outside the sphere, thus both distances are either positive
×
UNCOV
673
    // or negative. If positive, the smaller distance is the one with positive
×
UNCOV
674
    // sign on sqrt(quad).
×
UNCOV
675
    const double d = -k - sqrt(quad);
×
676
    if (d < 0.0)
UNCOV
677
      return INFTY;
×
678
    return d;
679
  }
UNCOV
680
}
×
UNCOV
681

×
UNCOV
682
Direction SurfaceSphere::normal(Position r) const
×
UNCOV
683
{
×
UNCOV
684
  return {2.0 * (r.x - x0_), 2.0 * (r.y - y0_), 2.0 * (r.z - z0_)};
×
685
}
686

687
void SurfaceSphere::to_hdf5_inner(hid_t group_id) const
688
{
689
  write_string(group_id, "type", "sphere", false);
690
  array<double, 4> coeffs {{x0_, y0_, z0_, radius_}};
691
  write_dataset(group_id, "coefficients", coeffs);
×
692
}
693

UNCOV
694
BoundingBox SurfaceSphere::bounding_box(bool pos_side) const
×
695
{
696
  if (!pos_side) {
697
    return {x0_ - radius_, x0_ + radius_, y0_ - radius_, y0_ + radius_,
32✔
698
      z0_ - radius_, z0_ + radius_};
32✔
699
  } else {
700
    return {};
32✔
701
  }
32✔
702
}
703

1,360,487✔
704
void SurfaceSphere::connect_to_triso_base(int triso_index, std::string key)
705
{
1,360,487✔
706
  if (key == "base") {
707
    triso_base_index_ = triso_index;
708
    is_triso_surface_ = true;
1,083,236✔
709
  } else if (key == "particle") {
710
    triso_particle_index_ = triso_index;
711
  }
1,083,236✔
712
}
1,083,236✔
713

714
vector<double> SurfaceSphere::get_center() const
NEW
715
{
×
716
  return {x0_, y0_, z0_};
NEW
717
}
×
718

719
double SurfaceSphere::get_radius() const
720
{
22✔
721
  return radius_;
722
}
22✔
723

22✔
724
bool SurfaceSphere::triso_in_mesh(
22✔
725
  vector<double> mesh_center, vector<double> lattice_pitch) const
22✔
726
{
NEW
727
  double dis_x;
×
728
  double dis_y;
NEW
729
  double dis_z;
×
NEW
730
  double x_min = mesh_center[0] - lattice_pitch[0] / 2;
×
NEW
731
  double x_max = mesh_center[0] + lattice_pitch[0] / 2;
×
732
  double y_min = mesh_center[1] - lattice_pitch[1] / 2;
NEW
733
  double y_max = mesh_center[1] + lattice_pitch[1] / 2;
×
734
  double z_min = mesh_center[2] - lattice_pitch[2] / 2;
735
  double z_max = mesh_center[2] + lattice_pitch[2] / 2;
736
  if (x0_ >= x_min && x0_ <= x_max) {
737
    dis_x = 0;
738
  } else if (x0_ < x_min) {
739
    dis_x = pow(x_min - x0_, 2);
NEW
740
  } else {
×
741
    dis_x = pow(x_max - x0_, 2);
742
  }
NEW
743

×
744
  if (y0_ >= y_min && y0_ <= y_max) {
745
    dis_y = 0;
NEW
746
  } else if (y0_ < y_min) {
×
NEW
747
    dis_y = pow(y_min - y0_, 2);
×
748
  } else {
NEW
749
    dis_y = pow(y_max - y0_, 2);
×
750
  }
751

NEW
752
  if (z0_ >= z_min && z0_ <= z_max) {
×
753
    dis_z = 0;
NEW
754
  } else if (z0_ < z_min) {
×
755
    dis_z = pow(z_min - z0_, 2);
756
  } else {
NEW
757
    dis_z = pow(z_max - z0_, 2);
×
758
  }
759

NEW
760
  if (sqrt(dis_x + dis_y + dis_z) < radius_) {
×
NEW
761
    return true;
×
762
  } else {
763
    return false;
NEW
764
  }
×
765
}
NEW
766

×
767
//==============================================================================
768
// Generic functions for x-, y-, and z-, cones
UNCOV
769
//==============================================================================
×
770

UNCOV
771
// The first template parameter indicates which axis the cone is aligned to.
×
UNCOV
772
// The other two parameters indicate the other two axes.  offset1, offset2,
×
UNCOV
773
// and offset3 should correspond with i1, i2, and i3, respectively.
×
774
template<int i1, int i2, int i3>
775
double axis_aligned_cone_evaluate(
UNCOV
776
  Position r, double offset1, double offset2, double offset3, double radius_sq)
×
777
{
UNCOV
778
  const double r1 = r.get<i1>() - offset1;
×
UNCOV
779
  const double r2 = r.get<i2>() - offset2;
×
UNCOV
780
  const double r3 = r.get<i3>() - offset3;
×
781
  return r2 * r2 + r3 * r3 - radius_sq * r1 * r1;
UNCOV
782
}
×
783

784
// The first template parameter indicates which axis the cone is aligned to.
785
// The other two parameters indicate the other two axes.  offset1, offset2,
786
// and offset3 should correspond with i1, i2, and i3, respectively.
787
template<int i1, int i2, int i3>
788
double axis_aligned_cone_distance(Position r, Direction u, bool coincident,
789
  double offset1, double offset2, double offset3, double radius_sq)
790
{
×
791
  const double r1 = r.get<i1>() - offset1;
792
  const double r2 = r.get<i2>() - offset2;
UNCOV
793
  const double r3 = r.get<i3>() - offset3;
×
794
  const double a = u.get<i2>() * u.get<i2>() + u.get<i3>() * u.get<i3>() -
795
                   radius_sq * u.get<i1>() * u.get<i1>();
796
  const double k =
4,614✔
797
    r2 * u.get<i2>() + r3 * u.get<i3>() - radius_sq * r1 * u.get<i1>();
4,614✔
798
  const double c = r2 * r2 + r3 * r3 - radius_sq * r1 * r1;
799
  double quad = k * k - a * c;
4,614✔
800

4,614✔
801
  double d;
802

1,386,891,385✔
803
  if (quad < 0.0) {
804
    // No intersection with cone.
1,386,891,385✔
805
    return INFTY;
806

807
  } else if (coincident || std::abs(c) < FP_COINCIDENT) {
1,622,890,799✔
808
    // Particle is on the cone, thus one distance is positive/negative
809
    // and the other is zero. The sign of k determines if we are facing in or
810
    // out.
1,622,890,799✔
811
    if (k >= 0.0) {
1,622,890,799✔
812
      d = (-k - sqrt(quad)) / a;
813
    } else {
814
      d = (-k + sqrt(quad)) / a;
1,387,667✔
815
    }
816

1,387,667✔
817
  } else {
818
    // Calculate both solutions to the quadratic.
819
    quad = sqrt(quad);
3,356✔
820
    d = (-k - quad) / a;
821
    const double b = (-k + quad) / a;
3,356✔
822

3,356✔
823
    // Determine the smallest positive solution.
3,356✔
824
    if (d < 0.0) {
3,356✔
825
      if (b > 0.0)
826
        d = b;
48✔
827
    } else {
828
      if (b > 0.0) {
48✔
829
        if (b < d)
24✔
830
          d = b;
24✔
831
      }
832
    }
24✔
833
  }
834

835
  // If the distance was negative, set boundary distance to infinity.
836
  if (d <= 0.0)
837
    return INFTY;
838
  return d;
839
}
840

9,883✔
841
// The first template parameter indicates which axis the cone is aligned to.
842
// The other two parameters indicate the other two axes.  offset1, offset2,
9,883✔
843
// and offset3 should correspond with i1, i2, and i3, respectively.
9,883✔
844
template<int i1, int i2, int i3>
845
Direction axis_aligned_cone_normal(
565,584,276✔
846
  Position r, double offset1, double offset2, double offset3, double radius_sq)
847
{
565,584,276✔
848
  Direction u;
565,584,276✔
849
  u.get<i1>() = -2.0 * radius_sq * (r.get<i1>() - offset1);
565,584,276✔
850
  u.get<i2>() = 2.0 * (r.get<i2>() - offset2);
565,584,276✔
851
  u.get<i3>() = 2.0 * (r.get<i3>() - offset3);
852
  return u;
853
}
705,524,622✔
854

855
//==============================================================================
705,524,622✔
856
// SurfaceXCone implementation
705,524,622✔
857
//==============================================================================
705,524,622✔
858
bool SurfaceXCone::triso_in_mesh(
705,524,622✔
859
  vector<double> mesh_center, vector<double> lattice_pitch) const
705,524,622✔
860
{
705,524,622✔
861
  return false;
862
}
705,524,622✔
863
SurfaceXCone::SurfaceXCone(pugi::xml_node surf_node) : Surface(surf_node)
864
{
184,641,805✔
865
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &radius_sq_});
866
}
520,882,817✔
867

868
double SurfaceXCone::evaluate(Position r) const
869
{
76,772,685✔
870
  return axis_aligned_cone_evaluate<0, 1, 2>(r, x0_, y0_, z0_, radius_sq_);
38,026,279✔
871
}
872

38,746,406✔
873
double SurfaceXCone::distance(Position r, Direction u, bool coincident) const
874
{
875
  return axis_aligned_cone_distance<0, 1, 2>(
444,110,132✔
876
    r, u, coincident, x0_, y0_, z0_, radius_sq_);
877
}
878

879
Direction SurfaceXCone::normal(Position r) const
394,833,463✔
880
{
881
  return axis_aligned_cone_normal<0, 1, 2>(r, x0_, y0_, z0_, radius_sq_);
882
}
883

884
void SurfaceXCone::to_hdf5_inner(hid_t group_id) const
885
{
49,276,669✔
886
  write_string(group_id, "type", "x-cone", false);
49,276,669✔
887
  array<double, 4> coeffs {{x0_, y0_, z0_, radius_sq_}};
10,484,035✔
888
  write_dataset(group_id, "coefficients", coeffs);
38,792,634✔
889
}
890

891
//==============================================================================
892
// SurfaceYCone implementation
7,097,156✔
893
//==============================================================================
894
bool SurfaceYCone::triso_in_mesh(
7,097,156✔
895
  vector<double> mesh_center, vector<double> lattice_pitch) const
896
{
897
  return false;
7,463✔
898
}
899
SurfaceYCone::SurfaceYCone(pugi::xml_node surf_node) : Surface(surf_node)
7,463✔
900
{
7,463✔
901
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &radius_sq_});
7,463✔
902
}
7,463✔
903

UNCOV
904
double SurfaceYCone::evaluate(Position r) const
×
905
{
UNCOV
906
  return axis_aligned_cone_evaluate<1, 0, 2>(r, y0_, x0_, z0_, radius_sq_);
×
UNCOV
907
}
×
UNCOV
908

×
909
double SurfaceYCone::distance(Position r, Direction u, bool coincident) const
UNCOV
910
{
×
911
  return axis_aligned_cone_distance<1, 0, 2>(
912
    r, u, coincident, y0_, x0_, z0_, radius_sq_);
913
}
914

4,176✔
915
Direction SurfaceYCone::normal(Position r) const
916
{
4,176✔
917
  return axis_aligned_cone_normal<1, 0, 2>(r, y0_, x0_, z0_, radius_sq_);
2,576✔
918
}
2,576✔
919

1,600✔
920
void SurfaceYCone::to_hdf5_inner(hid_t group_id) const
1,600✔
921
{
922
  write_string(group_id, "type", "y-cone", false);
4,176✔
923
  array<double, 4> coeffs {{x0_, y0_, z0_, radius_sq_}};
924
  write_dataset(group_id, "coefficients", coeffs);
18,070,101✔
925
}
926

18,070,101✔
927
//==============================================================================
928
// SurfaceZCone implementation
929
//==============================================================================
18,068,501✔
930
bool SurfaceZCone::triso_in_mesh(
931
  vector<double> mesh_center, vector<double> lattice_pitch) const
18,068,501✔
932
{
933
  return false;
934
}
20,416✔
935

936
SurfaceZCone::SurfaceZCone(pugi::xml_node surf_node) : Surface(surf_node)
937
{
938
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &radius_sq_});
939
}
940

20,416✔
941
double SurfaceZCone::evaluate(Position r) const
20,416✔
942
{
20,416✔
943
  return axis_aligned_cone_evaluate<2, 0, 1>(r, z0_, x0_, y0_, radius_sq_);
20,416✔
944
}
20,416✔
945

20,416✔
946
double SurfaceZCone::distance(Position r, Direction u, bool coincident) const
20,416✔
947
{
8,672✔
948
  return axis_aligned_cone_distance<2, 0, 1>(
11,744✔
949
    r, u, coincident, z0_, x0_, y0_, radius_sq_);
6,208✔
950
}
951

5,536✔
952
Direction SurfaceZCone::normal(Position r) const
953
{
954
  return axis_aligned_cone_normal<2, 0, 1>(r, z0_, x0_, y0_, radius_sq_);
20,416✔
955
}
8,800✔
956

11,616✔
957
void SurfaceZCone::to_hdf5_inner(hid_t group_id) const
5,632✔
958
{
959
  write_string(group_id, "type", "z-cone", false);
5,984✔
960
  array<double, 4> coeffs {{x0_, y0_, z0_, radius_sq_}};
961
  write_dataset(group_id, "coefficients", coeffs);
962
}
20,416✔
963

8,736✔
964
//==============================================================================
11,680✔
965
// SurfaceQuadric implementation
6,352✔
966
//==============================================================================
967
bool SurfaceQuadric::triso_in_mesh(
5,328✔
968
  vector<double> mesh_center, vector<double> lattice_pitch) const
969
{
970
  return false;
20,416✔
971
}
2,576✔
972
SurfaceQuadric::SurfaceQuadric(pugi::xml_node surf_node) : Surface(surf_node)
973
{
17,840✔
974
  read_coeffs(
975
    surf_node, id_, {&A_, &B_, &C_, &D_, &E_, &F_, &G_, &H_, &J_, &K_});
976
}
977

978
double SurfaceQuadric::evaluate(Position r) const
979
{
980
  const double x = r.x;
981
  const double y = r.y;
982
  const double z = r.z;
983
  return x * (A_ * x + D_ * y + G_) + y * (B_ * y + E_ * z + H_) +
984
         z * (C_ * z + F_ * x + J_) + K_;
985
}
322,586✔
986

987
double SurfaceQuadric::distance(
988
  Position r, Direction ang, bool coincident) const
322,586✔
989
{
322,586✔
990
  const double& x = r.x;
322,586✔
991
  const double& y = r.y;
322,586✔
992
  const double& z = r.z;
993
  const double& u = ang.x;
322,586✔
994
  const double& v = ang.y;
995
  const double& w = ang.z;
996

322,586✔
997
  const double a =
322,586✔
998
    A_ * u * u + B_ * v * v + C_ * w * w + D_ * u * v + E_ * v * w + F_ * u * w;
322,586✔
999
  const double k = A_ * u * x + B_ * v * y + C_ * w * z +
322,586✔
1000
                   0.5 * (D_ * (u * y + v * x) + E_ * (v * z + w * y) +
UNCOV
1001
                           F_ * (w * x + u * z) + G_ * u + H_ * v + J_ * w);
×
1002
  const double c = A_ * x * x + B_ * y * y + C_ * z * z + D_ * x * y +
1003
                   E_ * y * z + F_ * x * z + G_ * x + H_ * y + J_ * z + K_;
UNCOV
1004
  double quad = k * k - a * c;
×
UNCOV
1005

×
UNCOV
1006
  double d;
×
UNCOV
1007

×
1008
  if (quad < 0.0) {
UNCOV
1009
    // No intersection with surface.
×
1010
    return INFTY;
1011

UNCOV
1012
  } else if (coincident || std::abs(c) < FP_COINCIDENT) {
×
UNCOV
1013
    // Particle is on the surface, thus one distance is positive/negative and
×
UNCOV
1014
    // the other is zero. The sign of k determines which distance is zero and
×
UNCOV
1015
    // which is not. Additionally, if a is zero, it means the particle is on
×
1016
    // a plane-like surface.
1017
    if (a == 0.0) {
1018
      d = INFTY; // see the below explanation
1019
    } else if (k >= 0.0) {
1020
      d = (-k - sqrt(quad)) / a;
1021
    } else {
1022
      d = (-k + sqrt(quad)) / a;
963,358✔
1023
    }
1024

1025
  } else if (a == 0.0) {
963,358✔
1026
    // Given the orientation of the particle, the quadric looks like a plane in
963,358✔
1027
    // this case, and thus we have only one solution despite potentially having
963,358✔
1028
    // quad > 0.0. While the term under the square root may be real, in one
963,358✔
1029
    // case of the +/- of the quadratic formula, 0/0 results, and in another, a
963,358✔
1030
    // finite value over 0 results. Applying L'Hopital's to the 0/0 case gives
963,358✔
1031
    // the below. Alternatively this can be found by simply putting a=0 in the
963,358✔
1032
    // equation ax^2 + bx + c = 0.
963,358✔
1033
    d = -0.5 * c / k;
963,358✔
1034
  } else {
1035
    // Calculate both solutions to the quadratic.
1036
    quad = sqrt(quad);
1037
    d = (-k - quad) / a;
963,358✔
1038
    double b = (-k + quad) / a;
UNCOV
1039

×
1040
    // Determine the smallest positive solution.
1041
    if (d < 0.0) {
963,358✔
1042
      if (b > 0.0)
1043
        d = b;
1044
    } else {
1045
      if (b > 0.0) {
60,566✔
UNCOV
1046
        if (b < d)
×
1047
          d = b;
1048
      }
60,566✔
1049
    }
1050
  }
1051

1052
  // If the distance was negative, set boundary distance to infinity.
1053
  if (d <= 0.0)
902,792✔
1054
    return INFTY;
902,792✔
1055
  return d;
902,792✔
1056
}
1057

1058
Direction SurfaceQuadric::normal(Position r) const
902,792✔
1059
{
766,524✔
1060
  const double& x = r.x;
650,507✔
1061
  const double& y = r.y;
1062
  const double& z = r.z;
136,268✔
1063
  return {2.0 * A_ * x + D_ * y + F_ * z + G_,
136,268✔
1064
    2.0 * B_ * y + D_ * x + E_ * z + H_, 2.0 * C_ * z + E_ * y + F_ * x + J_};
136,268✔
1065
}
1066

1067
void SurfaceQuadric::to_hdf5_inner(hid_t group_id) const
1068
{
1069
  write_string(group_id, "type", "quadric", false);
1070
  array<double, 10> coeffs {{A_, B_, C_, D_, E_, F_, G_, H_, J_, K_}};
963,358✔
1071
  write_dataset(group_id, "coefficients", coeffs);
137,467✔
1072
}
825,891✔
1073

1074
//==============================================================================
963,358✔
1075
// Torus helper functions
1076
//==============================================================================
1077

963,358✔
1078
double torus_distance(double x1, double x2, double x3, double u1, double u2,
963,358✔
1079
  double u3, double A, double B, double C, bool coincident)
963,358✔
1080
{
963,358✔
1081
  // Coefficients for equation: (c2 t^2 + c1 t + c0)^2 = c2' t^2 + c1' t + c0'
963,358✔
1082
  double D = (C * C) / (B * B);
963,358✔
1083
  double c2 = u1 * u1 + u2 * u2 + D * u3 * u3;
963,358✔
1084
  double c1 = 2 * (u1 * x1 + u2 * x2 + D * u3 * x3);
963,358✔
1085
  double c0 = x1 * x1 + x2 * x2 + D * x3 * x3 + A * A - C * C;
963,358✔
1086
  double four_A2 = 4 * A * A;
1087
  double c2p = four_A2 * (u1 * u1 + u2 * u2);
1088
  double c1p = 2 * four_A2 * (u1 * x1 + u2 * x2);
1089
  double c0p = four_A2 * (x1 * x1 + x2 * x2);
963,358✔
1090

UNCOV
1091
  // Coefficient for equation: a t^4 + b t^3 + c t^2 + d t + e = 0. If the point
×
1092
  // is coincident, the 'e' coefficient should be zero. Explicitly setting it to
1093
  // zero helps avoid numerical issues below with root finding.
963,358✔
1094
  double coeff[5];
1095
  coeff[0] = coincident ? 0.0 : c0 * c0 - c0p;
1096
  coeff[1] = 2 * c0 * c1 - c1p;
1097
  coeff[2] = c1 * c1 + 2 * c0 * c2 - c2p;
60,566✔
UNCOV
1098
  coeff[3] = 2 * c1 * c2;
×
1099
  coeff[4] = c2 * c2;
1100

60,566✔
1101
  std::complex<double> roots[4];
1102
  oqs::quartic_solver(coeff, roots);
1103

1104
  // Find smallest positive, real root. In the case where the particle is
1105
  // coincident with the surface, we are sure to have one root very close to
902,792✔
1106
  // zero but possibly small and positive. A tolerance is set to discard that
902,792✔
1107
  // zero.
902,792✔
1108
  double distance = INFTY;
1109
  double cutoff = coincident ? TORUS_TOL : 0.0;
1110
  for (int i = 0; i < 4; ++i) {
902,792✔
1111
    if (roots[i].imag() == 0) {
766,524✔
1112
      double root = roots[i].real();
650,507✔
1113
      if (root > cutoff && root < distance) {
1114
        // Avoid roots corresponding to internal surfaces
136,268✔
1115
        double s1 = x1 + u1 * root;
136,268✔
1116
        double s2 = x2 + u2 * root;
136,268✔
1117
        double s3 = x3 + u3 * root;
1118
        double check = D * s3 * s3 + s1 * s1 + s2 * s2 + A * A - C * C;
1119
        if (check >= 0) {
1120
          distance = root;
1121
        }
1122
      }
963,358✔
1123
    }
137,467✔
1124
  }
825,891✔
1125
  return distance;
UNCOV
1126
}
×
1127

1128
//==============================================================================
UNCOV
1129
// SurfaceXTorus implementation
×
UNCOV
1130
//==============================================================================
×
NEW
1131
bool SurfaceXTorus::triso_in_mesh(
×
NEW
1132
  vector<double> mesh_center, vector<double> lattice_pitch) const
×
NEW
1133
{
×
NEW
1134
  return false;
×
NEW
1135
}
×
UNCOV
1136
SurfaceXTorus::SurfaceXTorus(pugi::xml_node surf_node) : Surface(surf_node)
×
UNCOV
1137
{
×
1138
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &A_, &B_, &C_});
1139
}
1140

UNCOV
1141
void SurfaceXTorus::to_hdf5_inner(hid_t group_id) const
×
1142
{
UNCOV
1143
  write_string(group_id, "type", "x-torus", false);
×
1144
  std::array<double, 6> coeffs {{x0_, y0_, z0_, A_, B_, C_}};
UNCOV
1145
  write_dataset(group_id, "coefficients", coeffs);
×
1146
}
1147

1148
double SurfaceXTorus::evaluate(Position r) const
UNCOV
1149
{
×
1150
  double x = r.x - x0_;
×
1151
  double y = r.y - y0_;
1152
  double z = r.z - z0_;
×
1153
  return (x * x) / (B_ * B_) +
1154
         std::pow(std::sqrt(y * y + z * z) - A_, 2) / (C_ * C_) - 1.;
1155
}
1156

1157
double SurfaceXTorus::distance(Position r, Direction u, bool coincident) const
×
1158
{
×
UNCOV
1159
  double x = r.x - x0_;
×
1160
  double y = r.y - y0_;
1161
  double z = r.z - z0_;
1162
  return torus_distance(y, z, x, u.y, u.z, u.x, A_, B_, C_, coincident);
×
UNCOV
1163
}
×
1164

×
1165
Direction SurfaceXTorus::normal(Position r) const
1166
{
×
UNCOV
1167
  // reduce the expansion of the full form for torus
×
UNCOV
1168
  double x = r.x - x0_;
×
1169
  double y = r.y - y0_;
1170
  double z = r.z - z0_;
1171

1172
  // f(x,y,z) = x^2/B^2 + (sqrt(y^2 + z^2) - A)^2/C^2 - 1
1173
  // ∂f/∂x = 2x/B^2
UNCOV
1174
  // ∂f/∂y = 2y(g - A)/(g*C^2) where g = sqrt(y^2 + z^2)
×
UNCOV
1175
  // ∂f/∂z = 2z(g - A)/(g*C^2)
×
UNCOV
1176
  // Multiplying by g*C^2*B^2 / 2 gives:
×
1177
  double g = std::sqrt(y * y + z * z);
1178
  double nx = C_ * C_ * g * x;
×
1179
  double ny = y * (g - A_) * B_ * B_;
1180
  double nz = z * (g - A_) * B_ * B_;
UNCOV
1181
  Direction n(nx, ny, nz);
×
UNCOV
1182
  return n / n.norm();
×
1183
}
×
1184

×
1185
//==============================================================================
×
UNCOV
1186
// SurfaceYTorus implementation
×
1187
//==============================================================================
×
NEW
1188
bool SurfaceYTorus::triso_in_mesh(
×
NEW
1189
  vector<double> mesh_center, vector<double> lattice_pitch) const
×
1190
{
1191
  return false;
1192
}
UNCOV
1193
SurfaceYTorus::SurfaceYTorus(pugi::xml_node surf_node) : Surface(surf_node)
×
1194
{
UNCOV
1195
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &A_, &B_, &C_});
×
1196
}
UNCOV
1197

×
1198
void SurfaceYTorus::to_hdf5_inner(hid_t group_id) const
1199
{
1200
  write_string(group_id, "type", "y-torus", false);
1201
  std::array<double, 6> coeffs {{x0_, y0_, z0_, A_, B_, C_}};
×
UNCOV
1202
  write_dataset(group_id, "coefficients", coeffs);
×
1203
}
UNCOV
1204

×
1205
double SurfaceYTorus::evaluate(Position r) const
1206
{
1207
  double x = r.x - x0_;
1208
  double y = r.y - y0_;
1209
  double z = r.z - z0_;
×
1210
  return (y * y) / (B_ * B_) +
×
1211
         std::pow(std::sqrt(x * x + z * z) - A_, 2) / (C_ * C_) - 1.;
×
1212
}
1213

1214
double SurfaceYTorus::distance(Position r, Direction u, bool coincident) const
×
UNCOV
1215
{
×
UNCOV
1216
  double x = r.x - x0_;
×
1217
  double y = r.y - y0_;
1218
  double z = r.z - z0_;
×
UNCOV
1219
  return torus_distance(x, z, y, u.x, u.z, u.y, A_, B_, C_, coincident);
×
1220
}
×
1221

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

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

60,566✔
1242
//==============================================================================
60,566✔
1243
// SurfaceZTorus implementation
1244
//==============================================================================
60,566✔
1245
bool SurfaceZTorus::triso_in_mesh(
1246
  vector<double> mesh_center, vector<double> lattice_pitch) const
1247
{
60,566✔
1248
  return false;
60,566✔
1249
}
60,566✔
1250

60,566✔
1251
SurfaceZTorus::SurfaceZTorus(pugi::xml_node surf_node) : Surface(surf_node)
60,566✔
1252
{
UNCOV
1253
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &A_, &B_, &C_});
×
1254
}
1255

1256
void SurfaceZTorus::to_hdf5_inner(hid_t group_id) const
×
1257
{
×
1258
  write_string(group_id, "type", "z-torus", false);
×
UNCOV
1259
  std::array<double, 6> coeffs {{x0_, y0_, z0_, A_, B_, C_}};
×
UNCOV
1260
  write_dataset(group_id, "coefficients", coeffs);
×
1261
}
UNCOV
1262

×
1263
double SurfaceZTorus::evaluate(Position r) const
1264
{
UNCOV
1265
  double x = r.x - x0_;
×
UNCOV
1266
  double y = r.y - y0_;
×
UNCOV
1267
  double z = r.z - z0_;
×
UNCOV
1268
  return (z * z) / (B_ * B_) +
×
UNCOV
1269
         std::pow(std::sqrt(x * x + y * y) - A_, 2) / (C_ * C_) - 1.;
×
1270
}
1271

1272
double SurfaceZTorus::distance(Position r, Direction u, bool coincident) const
1273
{
1274
  double x = r.x - x0_;
UNCOV
1275
  double y = r.y - y0_;
×
1276
  double z = r.z - z0_;
1277
  return torus_distance(x, y, z, u.x, u.y, u.z, A_, B_, C_, coincident);
UNCOV
1278
}
×
1279

UNCOV
1280
Direction SurfaceZTorus::normal(Position r) const
×
1281
{
UNCOV
1282
  // reduce the expansion of the full form for torus
×
1283
  double x = r.x - x0_;
1284
  double y = r.y - y0_;
UNCOV
1285
  double z = r.z - z0_;
×
1286

1287
  // f(x,y,z) = z^2/B^2 + (sqrt(x^2 + y^2) - A)^2/C^2 - 1
×
1288
  // ∂f/∂x = 2x(g - A)/(g*C^2) where g = sqrt(x^2 + y^2)
1289
  // ∂f/∂y = 2y(g - A)/(g*C^2)
1290
  // ∂f/∂z = 2z/B^2
×
1291
  // Multiplying by g*C^2*B^2 / 2 gives:
1292
  double g = std::sqrt(x * x + y * y);
×
UNCOV
1293
  double nx = x * (g - A_) * B_ * B_;
×
1294
  double ny = y * (g - A_) * B_ * B_;
1295
  double nz = C_ * C_ * g * z;
1296
  Position n(nx, ny, nz);
×
1297
  return n / n.norm();
1298
}
×
1299

1300
//==============================================================================
UNCOV
1301

×
1302
void read_surfaces(pugi::xml_node node)
UNCOV
1303
{
×
UNCOV
1304
  // Count the number of surfaces
×
UNCOV
1305
  int n_surfaces = 0;
×
1306
  for (pugi::xml_node surf_node : node.children("surface")) {
1307
    n_surfaces++;
1308
  }
1309

1310
  // Loop over XML surface elements and populate the array.  Keep track of
1311
  // periodic surfaces and their albedos.
×
1312
  model::surfaces.reserve(n_surfaces);
1313
  std::set<std::pair<int, int>> periodic_pairs;
UNCOV
1314
  std::unordered_map<int, double> albedo_map;
×
1315
  {
1316
    pugi::xml_node surf_node;
×
1317
    int i_surf;
1318
    for (surf_node = node.child("surface"), i_surf = 0; surf_node;
×
1319
         surf_node = surf_node.next_sibling("surface"), i_surf++) {
1320
      std::string surf_type = get_node_value(surf_node, "type", true, true);
UNCOV
1321

×
1322
      // Allocate and initialize the new surface
UNCOV
1323

×
1324
      if (surf_type == "x-plane") {
1325
        model::surfaces.push_back(make_unique<SurfaceXPlane>(surf_node));
UNCOV
1326

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

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

×
1333
      } else if (surf_type == "plane") {
UNCOV
1334
        model::surfaces.push_back(make_unique<SurfacePlane>(surf_node));
×
1335

1336
      } else if (surf_type == "x-cylinder") {
UNCOV
1337
        model::surfaces.push_back(make_unique<SurfaceXCylinder>(surf_node));
×
1338

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

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

1345
      } else if (surf_type == "sphere") {
1346
        model::surfaces.push_back(make_unique<SurfaceSphere>(surf_node));
UNCOV
1347

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

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

16✔
1354
      } else if (surf_type == "z-cone") {
1355
        model::surfaces.push_back(make_unique<SurfaceZCone>(surf_node));
16✔
1356

16✔
1357
      } else if (surf_type == "quadric") {
1358
        model::surfaces.push_back(make_unique<SurfaceQuadric>(surf_node));
322,586✔
1359

1360
      } else if (surf_type == "x-torus") {
322,586✔
1361
        model::surfaces.push_back(std::make_unique<SurfaceXTorus>(surf_node));
1362

1363
      } else if (surf_type == "y-torus") {
963,358✔
1364
        model::surfaces.push_back(std::make_unique<SurfaceYTorus>(surf_node));
1365

963,358✔
1366
      } else if (surf_type == "z-torus") {
963,358✔
1367
        model::surfaces.push_back(std::make_unique<SurfaceZTorus>(surf_node));
1368

1369
      } else {
60,566✔
1370
        fatal_error(fmt::format("Invalid surface type, \"{}\"", surf_type));
1371
      }
60,566✔
1372

1373
      // Check for a periodic surface
1374
      if (check_for_node(surf_node, "boundary")) {
11✔
1375
        std::string surf_bc = get_node_value(surf_node, "boundary", true, true);
1376
        if (surf_bc == "periodic") {
11✔
1377
          // Check for surface albedo. Skip sanity check as it is already done
11✔
1378
          // in the Surface class's constructor.
11✔
1379
          if (check_for_node(surf_node, "albedo")) {
11✔
1380
            albedo_map[model::surfaces.back()->id_] =
1381
              std::stod(get_node_value(surf_node, "albedo"));
1382
          }
1383
          if (check_for_node(surf_node, "periodic_surface_id")) {
UNCOV
1384
            int i_periodic =
×
1385
              std::stoi(get_node_value(surf_node, "periodic_surface_id"));
1386
            int lo_id = std::min(model::surfaces.back()->id_, i_periodic);
UNCOV
1387
            int hi_id = std::max(model::surfaces.back()->id_, i_periodic);
×
1388
            periodic_pairs.insert({lo_id, hi_id});
1389
          } else {
16✔
1390
            periodic_pairs.insert({model::surfaces.back()->id_, -1});
UNCOV
1391
          }
×
1392
        }
16✔
1393
      }
16✔
1394
    }
1395
  }
177,572✔
1396

1397
  // Fill the surface map
177,572✔
1398
  for (int i_surf = 0; i_surf < model::surfaces.size(); i_surf++) {
177,572✔
1399
    int id = model::surfaces[i_surf]->id_;
177,572✔
1400
    auto in_map = model::surface_map.find(id);
177,572✔
1401
    if (in_map == model::surface_map.end()) {
177,572✔
1402
      model::surface_map[id] = i_surf;
1403
    } else {
1404
      fatal_error(
312,510✔
1405
        fmt::format("Two or more surfaces use the same unique ID: {}", id));
1406
    }
1407
  }
312,510✔
1408

312,510✔
1409
  // Resolve unpaired periodic surfaces.  A lambda function is used with
312,510✔
1410
  // std::find_if to identify the unpaired surfaces.
312,510✔
1411
  auto is_unresolved_pair = [](const std::pair<int, int> p) {
312,510✔
1412
    return p.second == -1;
312,510✔
1413
  };
1414
  auto first_unresolved = std::find_if(
312,510✔
1415
    periodic_pairs.begin(), periodic_pairs.end(), is_unresolved_pair);
312,510✔
1416
  if (first_unresolved != periodic_pairs.end()) {
312,510✔
1417
    // Found one unpaired surface; search for a second one
312,510✔
1418
    auto next_elem = first_unresolved;
312,510✔
1419
    next_elem++;
312,510✔
1420
    auto second_unresolved =
312,510✔
1421
      std::find_if(next_elem, periodic_pairs.end(), is_unresolved_pair);
312,510✔
1422
    if (second_unresolved == periodic_pairs.end()) {
1423
      fatal_error("Found only one periodic surface without a specified partner."
1424
                  " Please specify the partner for each periodic surface.");
1425
    }
312,510✔
1426

UNCOV
1427
    // Make sure there isn't a third unpaired surface
×
1428
    next_elem = second_unresolved;
1429
    next_elem++;
312,510✔
1430
    auto third_unresolved =
1431
      std::find_if(next_elem, periodic_pairs.end(), is_unresolved_pair);
1432
    if (third_unresolved != periodic_pairs.end()) {
1433
      fatal_error(
1434
        "Found at least three periodic surfaces without a specified "
34,540✔
UNCOV
1435
        "partner. Please specify the partner for each periodic surface.");
×
1436
    }
34,540✔
UNCOV
1437

×
1438
    // Add the completed pair and remove the old, unpaired entries
1439
    int lo_id = std::min(first_unresolved->first, second_unresolved->first);
34,540✔
1440
    int hi_id = std::max(first_unresolved->first, second_unresolved->first);
1441
    periodic_pairs.insert({lo_id, hi_id});
1442
    periodic_pairs.erase(first_unresolved);
277,970✔
1443
    periodic_pairs.erase(second_unresolved);
1444
  }
1445

1446
  // Assign the periodic boundary conditions with albedos
1447
  for (auto periodic_pair : periodic_pairs) {
1448
    int i_surf = model::surface_map[periodic_pair.first];
1449
    int j_surf = model::surface_map[periodic_pair.second];
1450
    Surface& surf1 {*model::surfaces[i_surf]};
×
1451
    Surface& surf2 {*model::surfaces[j_surf]};
1452

1453
    // Compute the dot product of the surface normals
277,970✔
1454
    Direction norm1 = surf1.normal({0, 0, 0});
277,970✔
1455
    Direction norm2 = surf2.normal({0, 0, 0});
277,970✔
1456
    norm1 /= norm1.norm();
1457
    norm2 /= norm2.norm();
1458
    double dot_prod = norm1.dot(norm2);
277,970✔
1459

277,970✔
1460
    // If the dot product is 1 (to within floating point precision) then the
277,970✔
1461
    // planes are parallel which indicates a translational periodic boundary
UNCOV
1462
    // condition.  Otherwise, it is a rotational periodic BC.
×
1463
    if (std::abs(1.0 - dot_prod) < FP_PRECISION) {
×
UNCOV
1464
      surf1.bc_ = make_unique<TranslationalPeriodicBC>(i_surf, j_surf);
×
1465
      surf2.bc_ = make_unique<TranslationalPeriodicBC>(i_surf, j_surf);
1466
    } else {
1467
      surf1.bc_ = make_unique<RotationalPeriodicBC>(i_surf, j_surf);
1468
      surf2.bc_ = make_unique<RotationalPeriodicBC>(i_surf, j_surf);
1469
    }
1470

312,510✔
UNCOV
1471
    // If albedo data is present in albedo map, set the boundary albedo.
×
1472
    if (albedo_map.count(surf1.id_)) {
312,510✔
1473
      surf1.bc_->set_albedo(albedo_map[surf1.id_]);
1474
    }
1475
    if (albedo_map.count(surf2.id_)) {
34,540✔
1476
      surf2.bc_->set_albedo(albedo_map[surf2.id_]);
1477
    }
34,540✔
1478
  }
34,540✔
1479
}
34,540✔
1480

34,540✔
1481
void free_memory_surfaces()
34,540✔
1482
{
1483
  model::surfaces.clear();
1484
  model::surface_map.clear();
11✔
1485
}
1486

11✔
1487
} // namespace openmc
11✔
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