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

openmc-dev / openmc / 14515233533

17 Apr 2025 12:06PM UTC coverage: 83.16% (-2.3%) from 85.414%
14515233533

Pull #3087

github

web-flow
Merge 6ed397e9d into 47ca2916a
Pull Request #3087: wheel building with scikit build core

140769 of 169274 relevant lines covered (83.16%)

11830168.1 hits per line

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

70.29
/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(
30,572✔
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");
30,572✔
42
  if (coeffs_file.size() != coeffs.size()) {
30,572✔
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;
30,572✔
50
  for (auto c : coeffs) {
93,825✔
51
    *c = coeffs_file[i++];
63,253✔
52
  }
53
}
30,572✔
54

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

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

61
Surface::Surface(pugi::xml_node surf_node)
30,572✔
62
{
63
  if (check_for_node(surf_node, "id")) {
30,572✔
64
    id_ = std::stoi(get_node_value(surf_node, "id"));
30,572✔
65
    if (contains(settings::source_write_surf_id, id_) ||
60,367✔
66
        settings::source_write_surf_id.empty()) {
29,795✔
67
      surf_source_ = true;
29,563✔
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")) {
30,572✔
74
    name_ = get_node_value(surf_node, "name", false);
7,214✔
75
  }
76

77
  if (check_for_node(surf_node, "boundary")) {
30,572✔
78
    std::string surf_bc = get_node_value(surf_node, "boundary", true, true);
17,871✔
79

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

97
    if (check_for_node(surf_node, "albedo") && bc_) {
17,871✔
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
  }
17,871✔
114
}
30,572✔
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;
889,541✔
128
  }
129
  return f > 0.0;
2,147,483,647✔
130
}
131

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

138
  // Reflect direction according to normal.
139
  return u.reflect(n);
831,103,732✔
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
22,889✔
164
{
165
  hid_t surf_group = create_group(group_id, fmt::format("surface {}", id_));
45,778✔
166

167
  if (geom_type() == GeometryType::DAG) {
22,889✔
168
    write_string(surf_group, "geom_type", "dagmc", false);
321✔
169
  } else if (geom_type() == GeometryType::CSG) {
22,568✔
170
    write_string(surf_group, "geom_type", "csg", false);
22,568✔
171

172
    if (bc_) {
22,568✔
173
      write_string(surf_group, "boundary_type", bc_->type(), false);
13,411✔
174
      bc_->to_hdf5(surf_group);
13,411✔
175
    } else {
176
      write_string(surf_group, "boundary_type", "transmission", false);
9,157✔
177
    }
178
  }
179

180
  if (!name_.empty()) {
22,889✔
181
    write_string(surf_group, "name", name_, false);
5,043✔
182
  }
183

184
  to_hdf5_inner(surf_group);
22,889✔
185

186
  close_group(surf_group);
22,889✔
187
}
22,889✔
188

189
//==============================================================================
190
// Generic functions for x-, y-, and z-, planes.
191
//==============================================================================
192

193
// The template parameter indicates the axis normal to the plane.
194
template<int i>
195
double axis_aligned_plane_distance(
2,147,483,647✔
196
  Position r, Direction u, bool coincident, double offset)
197
{
198
  const double f = offset - r[i];
2,147,483,647✔
199
  if (coincident || std::abs(f) < FP_COINCIDENT || u[i] == 0.0)
2,147,483,647✔
200
    return INFTY;
418,364,888✔
201
  const double d = f / u[i];
2,147,483,647✔
202
  if (d < 0.0)
2,147,483,647✔
203
    return INFTY;
2,147,483,647✔
204
  return d;
2,147,483,647✔
205
}
206

2,024,752,221✔
207
//==============================================================================
208
// SurfaceXPlane implementation
209
//==============================================================================
2,024,752,221✔
210

2,024,752,221✔
211
SurfaceXPlane::SurfaceXPlane(pugi::xml_node surf_node) : Surface(surf_node)
31,025,140✔
212
{
1,993,727,081✔
213
  read_coeffs(surf_node, id_, {&x0_});
1,993,727,081✔
214
}
970,253,385✔
215

1,023,473,696✔
216
double SurfaceXPlane::evaluate(Position r) const
217
{
2,147,483,647✔
218
  return r.x - x0_;
219
}
220

2,147,483,647✔
221
double SurfaceXPlane::distance(Position r, Direction u, bool coincident) const
2,147,483,647✔
222
{
195,194,499✔
223
  return axis_aligned_plane_distance<0>(r, u, coincident, x0_);
2,147,483,647✔
224
}
2,147,483,647✔
225

2,147,483,647✔
226
Direction SurfaceXPlane::normal(Position r) const
2,147,483,647✔
227
{
228
  return {1., 0., 0.};
2,147,483,647✔
229
}
230

231
void SurfaceXPlane::to_hdf5_inner(hid_t group_id) const
2,147,483,647✔
232
{
2,147,483,647✔
233
  write_string(group_id, "type", "x-plane", false);
192,145,249✔
234
  array<double, 1> coeffs {{x0_}};
2,147,483,647✔
235
  write_dataset(group_id, "coefficients", coeffs);
2,147,483,647✔
236
}
2,147,483,647✔
237

2,147,483,647✔
238
BoundingBox SurfaceXPlane::bounding_box(bool pos_side) const
239
{
240
  if (pos_side) {
241
    return {x0_, INFTY, -INFTY, INFTY, -INFTY, INFTY};
242
  } else {
243
    return {-INFTY, x0_, -INFTY, INFTY, -INFTY, INFTY};
244
  }
7,060✔
245
}
246

7,060✔
247
//==============================================================================
7,060✔
248
// SurfaceYPlane implementation
249
//==============================================================================
1,042,012,610✔
250

251
SurfaceYPlane::SurfaceYPlane(pugi::xml_node surf_node) : Surface(surf_node)
1,042,012,610✔
252
{
253
  read_coeffs(surf_node, id_, {&y0_});
254
}
2,147,483,647✔
255

256
double SurfaceYPlane::evaluate(Position r) const
2,147,483,647✔
257
{
258
  return r.y - y0_;
259
}
194,015,806✔
260

261
double SurfaceYPlane::distance(Position r, Direction u, bool coincident) const
194,015,806✔
262
{
263
  return axis_aligned_plane_distance<1>(r, u, coincident, y0_);
264
}
5,084✔
265

266
Direction SurfaceYPlane::normal(Position r) const
5,084✔
267
{
5,084✔
268
  return {0., 1., 0.};
5,084✔
269
}
5,084✔
270

271
void SurfaceYPlane::to_hdf5_inner(hid_t group_id) const
×
272
{
273
  write_string(group_id, "type", "y-plane", false);
×
274
  array<double, 1> coeffs {{y0_}};
×
275
  write_dataset(group_id, "coefficients", coeffs);
276
}
×
277

278
BoundingBox SurfaceYPlane::bounding_box(bool pos_side) const
279
{
280
  if (pos_side) {
281
    return {-INFTY, INFTY, y0_, INFTY, -INFTY, INFTY};
282
  } else {
283
    return {-INFTY, INFTY, -INFTY, y0_, -INFTY, INFTY};
284
  }
6,336✔
285
}
286

6,336✔
287
//==============================================================================
6,336✔
288
// SurfaceZPlane implementation
289
//==============================================================================
1,062,346,042✔
290

291
SurfaceZPlane::SurfaceZPlane(pugi::xml_node surf_node) : Surface(surf_node)
1,062,346,042✔
292
{
293
  read_coeffs(surf_node, id_, {&z0_});
294
}
2,147,483,647✔
295

296
double SurfaceZPlane::evaluate(Position r) const
2,147,483,647✔
297
{
298
  return r.z - z0_;
299
}
197,356,326✔
300

301
double SurfaceZPlane::distance(Position r, Direction u, bool coincident) const
197,356,326✔
302
{
303
  return axis_aligned_plane_distance<2>(r, u, coincident, z0_);
304
}
4,712✔
305

306
Direction SurfaceZPlane::normal(Position r) const
4,712✔
307
{
4,712✔
308
  return {0., 0., 1.};
4,712✔
309
}
4,712✔
310

311
void SurfaceZPlane::to_hdf5_inner(hid_t group_id) const
×
312
{
313
  write_string(group_id, "type", "z-plane", false);
×
314
  array<double, 1> coeffs {{z0_}};
×
315
  write_dataset(group_id, "coefficients", coeffs);
316
}
×
317

318
BoundingBox SurfaceZPlane::bounding_box(bool pos_side) const
319
{
320
  if (pos_side) {
321
    return {-INFTY, INFTY, -INFTY, INFTY, z0_, INFTY};
322
  } else {
323
    return {-INFTY, INFTY, -INFTY, INFTY, -INFTY, z0_};
324
  }
5,111✔
325
}
326

5,111✔
327
//==============================================================================
5,111✔
328
// SurfacePlane implementation
329
//==============================================================================
384,063,270✔
330

331
SurfacePlane::SurfacePlane(pugi::xml_node surf_node) : Surface(surf_node)
384,063,270✔
332
{
333
  read_coeffs(surf_node, id_, {&A_, &B_, &C_, &D_});
334
}
2,024,752,221✔
335

336
double SurfacePlane::evaluate(Position r) const
2,024,752,221✔
337
{
338
  return A_ * r.x + B_ * r.y + C_ * r.z - D_;
339
}
31,890,107✔
340

341
double SurfacePlane::distance(Position r, Direction u, bool coincident) const
31,890,107✔
342
{
343
  const double f = A_ * r.x + B_ * r.y + C_ * r.z - D_;
344
  const double projection = A_ * u.x + B_ * u.y + C_ * u.z;
4,179✔
345
  if (coincident || std::abs(f) < FP_COINCIDENT || projection == 0.0) {
346
    return INFTY;
4,179✔
347
  } else {
4,179✔
348
    const double d = -f / projection;
4,179✔
349
    if (d < 0.0)
4,179✔
350
      return INFTY;
351
    return d;
×
352
  }
353
}
×
354

×
355
Direction SurfacePlane::normal(Position r) const
356
{
×
357
  return {A_, B_, C_};
358
}
359

360
void SurfacePlane::to_hdf5_inner(hid_t group_id) const
361
{
362
  write_string(group_id, "type", "plane", false);
363
  array<double, 4> coeffs {{A_, B_, C_, D_}};
364
  write_dataset(group_id, "coefficients", coeffs);
1,376✔
365
}
366

1,376✔
367
//==============================================================================
1,376✔
368
// Generic functions for x-, y-, and z-, cylinders
369
//==============================================================================
186,910,875✔
370

371
// The template parameters indicate the axes perpendicular to the axis of the
186,910,875✔
372
// cylinder.  offset1 and offset2 should correspond with i1 and i2,
373
// respectively.
374
template<int i1, int i2>
563,050,362✔
375
double axis_aligned_cylinder_evaluate(
376
  Position r, double offset1, double offset2, double radius)
563,050,362✔
377
{
563,050,362✔
378
  const double r1 = r.get<i1>() - offset1;
563,050,362✔
379
  const double r2 = r.get<i2>() - offset2;
33,573,089✔
380
  return r1 * r1 + r2 * r2 - radius * radius;
381
}
529,477,273✔
382

529,477,273✔
383
// The first template parameter indicates which axis the cylinder is aligned to.
247,874,627✔
384
// The other two parameters indicate the other two axes.  offset1 and offset2
281,602,646✔
385
// should correspond with i2 and i3, respectively.
386
template<int i1, int i2, int i3>
387
double axis_aligned_cylinder_distance(Position r, Direction u, bool coincident,
388
  double offset1, double offset2, double radius)
5,467,715✔
389
{
390
  const double a = 1.0 - u.get<i1>() * u.get<i1>(); // u^2 + v^2
5,467,715✔
391
  if (a == 0.0)
392
    return INFTY;
393

902✔
394
  const double r2 = r.get<i2>() - offset1;
395
  const double r3 = r.get<i3>() - offset2;
902✔
396
  const double k = r2 * u.get<i2>() + r3 * u.get<i3>();
902✔
397
  const double c = r2 * r2 + r3 * r3 - radius * radius;
902✔
398
  const double quad = k * k - a * c;
902✔
399

400
  if (quad < 0.0) {
401
    // No intersection with cylinder.
402
    return INFTY;
403

404
  } else if (coincident || std::abs(c) < FP_COINCIDENT) {
405
    // Particle is on the cylinder, thus one distance is positive/negative
406
    // and the other is zero. The sign of k determines if we are facing in or
407
    // out.
408
    if (k >= 0.0) {
892,592,561✔
409
      return INFTY;
410
    } else {
411
      return (-k + sqrt(quad)) / a;
892,592,561✔
412
    }
892,592,561✔
413

892,592,561✔
414
  } else if (c < 0.0) {
415
    // Particle is inside the cylinder, thus one distance must be negative
891,221,514✔
416
    // and one must be positive. The positive distance will be the one with
417
    // negative sign on sqrt(quad).
418
    return (-k + sqrt(quad)) / a;
891,221,514✔
419

891,221,514✔
420
  } else {
891,221,514✔
421
    // Particle is outside the cylinder, thus both distances are either
422
    // positive or negative. If positive, the smaller distance is the one
×
423
    // with positive sign on sqrt(quad).
424
    const double d = (-k - sqrt(quad)) / a;
425
    if (d < 0.0)
×
426
      return INFTY;
×
427
    return d;
×
428
  }
429
}
1,371,047✔
430

431
// The first template parameter indicates which axis the cylinder is aligned to.
432
// The other two parameters indicate the other two axes.  offset1 and offset2
1,371,047✔
433
// should correspond with i2 and i3, respectively.
1,371,047✔
434
template<int i1, int i2, int i3>
1,371,047✔
435
Direction axis_aligned_cylinder_normal(
436
  Position r, double offset1, double offset2)
437
{
438
  Direction u;
439
  u.get<i2>() = 2.0 * (r.get<i2>() - offset1);
440
  u.get<i3>() = 2.0 * (r.get<i3>() - offset2);
441
  u.get<i1>() = 0.0;
819,852,534✔
442
  return u;
443
}
444

819,852,534✔
445
//==============================================================================
819,852,534✔
446
// SurfaceXCylinder implementation
604,901✔
447
//==============================================================================
448

819,247,633✔
449
SurfaceXCylinder::SurfaceXCylinder(pugi::xml_node surf_node)
819,247,633✔
450
  : Surface(surf_node)
819,247,633✔
451
{
819,247,633✔
452
  read_coeffs(surf_node, id_, {&y0_, &z0_, &radius_});
819,247,633✔
453
}
454

819,247,633✔
455
double SurfaceXCylinder::evaluate(Position r) const
456
{
152,869,829✔
457
  return axis_aligned_cylinder_evaluate<1, 2>(r, y0_, z0_, radius_);
458
}
666,377,804✔
459

460
double SurfaceXCylinder::distance(
461
  Position r, Direction u, bool coincident) const
462
{
289,835,043✔
463
  return axis_aligned_cylinder_distance<0, 1, 2>(
145,296,157✔
464
    r, u, coincident, y0_, z0_, radius_);
465
}
144,538,886✔
466

467
Direction SurfaceXCylinder::normal(Position r) const
468
{
376,542,761✔
469
  return axis_aligned_cylinder_normal<0, 1, 2>(r, y0_, z0_);
470
}
471

472
void SurfaceXCylinder::to_hdf5_inner(hid_t group_id) const
157,669,918✔
473
{
474
  write_string(group_id, "type", "x-cylinder", false);
475
  array<double, 3> coeffs {{y0_, z0_, radius_}};
476
  write_dataset(group_id, "coefficients", coeffs);
477
}
478

218,872,843✔
479
BoundingBox SurfaceXCylinder::bounding_box(bool pos_side) const
218,872,843✔
480
{
34,422,675✔
481
  if (!pos_side) {
184,450,168✔
482
    return {-INFTY, INFTY, y0_ - radius_, y0_ + radius_, z0_ - radius_,
483
      z0_ + radius_};
484
  } else {
818,764,238✔
485
    return {};
486
  }
487
}
818,764,238✔
488
//==============================================================================
818,764,238✔
489
// SurfaceYCylinder implementation
164,901✔
490
//==============================================================================
491

818,599,337✔
492
SurfaceYCylinder::SurfaceYCylinder(pugi::xml_node surf_node)
818,599,337✔
493
  : Surface(surf_node)
818,599,337✔
494
{
818,599,337✔
495
  read_coeffs(surf_node, id_, {&x0_, &z0_, &radius_});
818,599,337✔
496
}
497

818,599,337✔
498
double SurfaceYCylinder::evaluate(Position r) const
499
{
152,869,829✔
500
  return axis_aligned_cylinder_evaluate<0, 2>(r, x0_, z0_, radius_);
501
}
665,729,508✔
502

503
double SurfaceYCylinder::distance(
504
  Position r, Direction u, bool coincident) const
505
{
289,835,043✔
506
  return axis_aligned_cylinder_distance<1, 0, 2>(
145,296,157✔
507
    r, u, coincident, x0_, z0_, radius_);
508
}
144,538,886✔
509

510
Direction SurfaceYCylinder::normal(Position r) const
511
{
375,894,465✔
512
  return axis_aligned_cylinder_normal<1, 0, 2>(r, x0_, z0_);
513
}
514

515
void SurfaceYCylinder::to_hdf5_inner(hid_t group_id) const
157,021,622✔
516
{
517
  write_string(group_id, "type", "y-cylinder", false);
518
  array<double, 3> coeffs {{x0_, z0_, radius_}};
519
  write_dataset(group_id, "coefficients", coeffs);
520
}
521

218,872,843✔
522
BoundingBox SurfaceYCylinder::bounding_box(bool pos_side) const
218,872,843✔
523
{
34,422,675✔
524
  if (!pos_side) {
184,450,168✔
525
    return {x0_ - radius_, x0_ + radius_, -INFTY, INFTY, z0_ - radius_,
526
      z0_ + radius_};
527
  } else {
×
528
    return {};
529
  }
530
}
×
531

×
532
//==============================================================================
×
533
// SurfaceZCylinder implementation
534
//==============================================================================
×
535

×
536
SurfaceZCylinder::SurfaceZCylinder(pugi::xml_node surf_node)
×
537
  : Surface(surf_node)
×
538
{
×
539
  read_coeffs(surf_node, id_, {&x0_, &y0_, &radius_});
540
}
×
541

542
double SurfaceZCylinder::evaluate(Position r) const
×
543
{
544
  return axis_aligned_cylinder_evaluate<0, 1>(r, x0_, y0_, radius_);
×
545
}
546

547
double SurfaceZCylinder::distance(
548
  Position r, Direction u, bool coincident) const
×
549
{
×
550
  return axis_aligned_cylinder_distance<2, 0, 1>(
551
    r, u, coincident, x0_, y0_, radius_);
×
552
}
553

554
Direction SurfaceZCylinder::normal(Position r) const
×
555
{
556
  return axis_aligned_cylinder_normal<2, 0, 1>(r, x0_, y0_);
557
}
558

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

×
566
BoundingBox SurfaceZCylinder::bounding_box(bool pos_side) const
×
567
{
×
568
  if (!pos_side) {
569
    return {x0_ - radius_, x0_ + radius_, y0_ - radius_, y0_ + radius_, -INFTY,
570
      INFTY};
1,088,296✔
571
  } else {
572
    return {};
573
  }
1,088,296✔
574
}
1,088,296✔
575

440,000✔
576
//==============================================================================
577
// SurfaceSphere implementation
648,296✔
578
//==============================================================================
648,296✔
579

648,296✔
580
SurfaceSphere::SurfaceSphere(pugi::xml_node surf_node) : Surface(surf_node)
648,296✔
581
{
648,296✔
582
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &radius_});
583
}
648,296✔
584

585
double SurfaceSphere::evaluate(Position r) const
×
586
{
587
  const double x = r.x - x0_;
648,296✔
588
  const double y = r.y - y0_;
589
  const double z = r.z - z0_;
590
  return x * x + y * y + z * z - radius_ * radius_;
591
}
×
592

×
593
double SurfaceSphere::distance(Position r, Direction u, bool coincident) const
594
{
×
595
  const double x = r.x - x0_;
596
  const double y = r.y - y0_;
597
  const double z = r.z - z0_;
648,296✔
598
  const double k = x * u.x + y * u.y + z * u.z;
599
  const double c = x * x + y * y + z * z - radius_ * radius_;
600
  const double quad = k * k - c;
601

648,296✔
602
  if (quad < 0.0) {
603
    // No intersection with sphere.
604
    return INFTY;
605

606
  } else if (coincident || std::abs(c) < FP_COINCIDENT) {
607
    // Particle is on the sphere, thus one distance is positive/negative and
×
608
    // the other is zero. The sign of k determines if we are facing in or out.
×
609
    if (k >= 0.0) {
×
610
      return INFTY;
×
611
    } else {
612
      return -k + sqrt(quad);
613
    }
614

615
  } else if (c < 0.0) {
616
    // Particle is inside the sphere, thus one distance must be negative and
617
    // one must be positive. The positive distance will be the one with
618
    // negative sign on sqrt(quad)
1,387,667✔
619
    return -k + sqrt(quad);
620

621
  } else {
1,387,667✔
622
    // Particle is outside the sphere, thus both distances are either positive
1,387,667✔
623
    // or negative. If positive, the smaller distance is the one with positive
1,387,667✔
624
    // sign on sqrt(quad).
1,387,667✔
625
    const double d = -k - sqrt(quad);
1,387,667✔
626
    if (d < 0.0)
627
      return INFTY;
1,387,667✔
628
    return d;
629
  }
630
}
1,387,667✔
631

1,387,667✔
632
Direction SurfaceSphere::normal(Position r) const
1,387,667✔
633
{
1,387,667✔
634
  return {2.0 * (r.x - x0_), 2.0 * (r.y - y0_), 2.0 * (r.z - z0_)};
1,387,667✔
635
}
636

×
637
void SurfaceSphere::to_hdf5_inner(hid_t group_id) const
638
{
639
  write_string(group_id, "type", "sphere", false);
×
640
  array<double, 4> coeffs {{x0_, y0_, z0_, radius_}};
×
641
  write_dataset(group_id, "coefficients", coeffs);
×
642
}
×
643

×
644
BoundingBox SurfaceSphere::bounding_box(bool pos_side) const
645
{
×
646
  if (!pos_side) {
647
    return {x0_ - radius_, x0_ + radius_, y0_ - radius_, y0_ + radius_,
648
      z0_ - radius_, z0_ + radius_};
×
649
  } else {
×
650
    return {};
×
651
  }
×
652
}
×
653

654
//==============================================================================
655
// Generic functions for x-, y-, and z-, cones
656
//==============================================================================
657

658
// The first template parameter indicates which axis the cone is aligned to.
659
// The other two parameters indicate the other two axes.  offset1, offset2,
32✔
660
// and offset3 should correspond with i1, i2, and i3, respectively.
32✔
661
template<int i1, int i2, int i3>
662
double axis_aligned_cone_evaluate(
32✔
663
  Position r, double offset1, double offset2, double offset3, double radius_sq)
32✔
664
{
665
  const double r1 = r.get<i1>() - offset1;
1,371,047✔
666
  const double r2 = r.get<i2>() - offset2;
667
  const double r3 = r.get<i3>() - offset3;
1,371,047✔
668
  return r2 * r2 + r3 * r3 - radius_sq * r1 * r1;
669
}
670

1,088,296✔
671
// The first template parameter indicates which axis the cone is aligned to.
672
// The other two parameters indicate the other two axes.  offset1, offset2,
673
// and offset3 should correspond with i1, i2, and i3, respectively.
1,088,296✔
674
template<int i1, int i2, int i3>
1,088,296✔
675
double axis_aligned_cone_distance(Position r, Direction u, bool coincident,
676
  double offset1, double offset2, double offset3, double radius_sq)
677
{
×
678
  const double r1 = r.get<i1>() - offset1;
679
  const double r2 = r.get<i2>() - offset2;
×
680
  const double r3 = r.get<i3>() - offset3;
681
  const double a = u.get<i2>() * u.get<i2>() + u.get<i3>() * u.get<i3>() -
682
                   radius_sq * u.get<i1>() * u.get<i1>();
22✔
683
  const double k =
684
    r2 * u.get<i2>() + r3 * u.get<i3>() - radius_sq * r1 * u.get<i1>();
22✔
685
  const double c = r2 * r2 + r3 * r3 - radius_sq * r1 * r1;
22✔
686
  double quad = k * k - a * c;
22✔
687

22✔
688
  double d;
689

×
690
  if (quad < 0.0) {
691
    // No intersection with cone.
×
692
    return INFTY;
×
693

×
694
  } else if (coincident || std::abs(c) < FP_COINCIDENT) {
695
    // Particle is on the cone, thus one distance is positive/negative
×
696
    // and the other is zero. The sign of k determines if we are facing in or
697
    // out.
698
    if (k >= 0.0) {
699
      d = (-k - sqrt(quad)) / a;
700
    } else {
701
      d = (-k + sqrt(quad)) / a;
702
    }
×
703

×
704
  } else {
705
    // Calculate both solutions to the quadratic.
×
706
    quad = sqrt(quad);
707
    d = (-k - quad) / a;
708
    const double b = (-k + quad) / a;
×
709

710
    // Determine the smallest positive solution.
×
711
    if (d < 0.0) {
712
      if (b > 0.0)
713
        d = b;
×
714
    } else {
715
      if (b > 0.0) {
716
        if (b < d)
×
717
          d = b;
×
718
      }
719
    }
720
  }
×
721

722
  // If the distance was negative, set boundary distance to infinity.
×
723
  if (d <= 0.0)
724
    return INFTY;
725
  return d;
×
726
}
727

×
728
// The first template parameter indicates which axis the cone is aligned to.
×
729
// The other two parameters indicate the other two axes.  offset1, offset2,
×
730
// and offset3 should correspond with i1, i2, and i3, respectively.
731
template<int i1, int i2, int i3>
732
Direction axis_aligned_cone_normal(
×
733
  Position r, double offset1, double offset2, double offset3, double radius_sq)
734
{
×
735
  Direction u;
×
736
  u.get<i1>() = -2.0 * radius_sq * (r.get<i1>() - offset1);
×
737
  u.get<i2>() = 2.0 * (r.get<i2>() - offset2);
738
  u.get<i3>() = 2.0 * (r.get<i3>() - offset3);
×
739
  return u;
740
}
741

742
//==============================================================================
743
// SurfaceXCone implementation
744
//==============================================================================
745

746
SurfaceXCone::SurfaceXCone(pugi::xml_node surf_node) : Surface(surf_node)
4,034✔
747
{
4,034✔
748
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &radius_sq_});
749
}
4,034✔
750

4,034✔
751
double SurfaceXCone::evaluate(Position r) const
752
{
891,221,514✔
753
  return axis_aligned_cone_evaluate<0, 1, 2>(r, x0_, y0_, z0_, radius_sq_);
754
}
891,221,514✔
755

756
double SurfaceXCone::distance(Position r, Direction u, bool coincident) const
757
{
818,764,238✔
758
  return axis_aligned_cone_distance<0, 1, 2>(
759
    r, u, coincident, x0_, y0_, z0_, radius_sq_);
760
}
818,764,238✔
761

818,764,238✔
762
Direction SurfaceXCone::normal(Position r) const
763
{
764
  return axis_aligned_cone_normal<0, 1, 2>(r, x0_, y0_, z0_, radius_sq_);
1,387,667✔
765
}
766

1,387,667✔
767
void SurfaceXCone::to_hdf5_inner(hid_t group_id) const
768
{
769
  write_string(group_id, "type", "x-cone", false);
2,786✔
770
  array<double, 4> coeffs {{x0_, y0_, z0_, radius_sq_}};
771
  write_dataset(group_id, "coefficients", coeffs);
2,786✔
772
}
2,786✔
773

2,786✔
774
//==============================================================================
2,786✔
775
// SurfaceYCone implementation
776
//==============================================================================
×
777

778
SurfaceYCone::SurfaceYCone(pugi::xml_node surf_node) : Surface(surf_node)
×
779
{
×
780
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &radius_sq_});
×
781
}
782

×
783
double SurfaceYCone::evaluate(Position r) const
784
{
785
  return axis_aligned_cone_evaluate<1, 0, 2>(r, y0_, x0_, z0_, radius_sq_);
786
}
787

788
double SurfaceYCone::distance(Position r, Direction u, bool coincident) const
789
{
790
  return axis_aligned_cone_distance<1, 0, 2>(
6,363✔
791
    r, u, coincident, y0_, x0_, z0_, radius_sq_);
792
}
6,363✔
793

6,363✔
794
Direction SurfaceYCone::normal(Position r) const
795
{
514,560,983✔
796
  return axis_aligned_cone_normal<1, 0, 2>(r, y0_, x0_, z0_, radius_sq_);
797
}
514,560,983✔
798

514,560,983✔
799
void SurfaceYCone::to_hdf5_inner(hid_t group_id) const
514,560,983✔
800
{
514,560,983✔
801
  write_string(group_id, "type", "y-cone", false);
802
  array<double, 4> coeffs {{x0_, y0_, z0_, radius_sq_}};
803
  write_dataset(group_id, "coefficients", coeffs);
499,039,901✔
804
}
805

499,039,901✔
806
//==============================================================================
499,039,901✔
807
// SurfaceZCone implementation
499,039,901✔
808
//==============================================================================
499,039,901✔
809

499,039,901✔
810
SurfaceZCone::SurfaceZCone(pugi::xml_node surf_node) : Surface(surf_node)
499,039,901✔
811
{
812
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &radius_sq_});
499,039,901✔
813
}
814

95,347,791✔
815
double SurfaceZCone::evaluate(Position r) const
816
{
403,692,110✔
817
  return axis_aligned_cone_evaluate<2, 0, 1>(r, z0_, x0_, y0_, radius_sq_);
818
}
819

48,184,675✔
820
double SurfaceZCone::distance(Position r, Direction u, bool coincident) const
26,290,990✔
821
{
822
  return axis_aligned_cone_distance<2, 0, 1>(
21,893,685✔
823
    r, u, coincident, z0_, x0_, y0_, radius_sq_);
824
}
825

355,507,435✔
826
Direction SurfaceZCone::normal(Position r) const
827
{
828
  return axis_aligned_cone_normal<2, 0, 1>(r, z0_, x0_, y0_, radius_sq_);
829
}
328,121,571✔
830

831
void SurfaceZCone::to_hdf5_inner(hid_t group_id) const
832
{
833
  write_string(group_id, "type", "z-cone", false);
834
  array<double, 4> coeffs {{x0_, y0_, z0_, radius_sq_}};
835
  write_dataset(group_id, "coefficients", coeffs);
27,385,864✔
836
}
27,385,864✔
837

3,838,362✔
838
//==============================================================================
23,547,502✔
839
// SurfaceQuadric implementation
840
//==============================================================================
841

842
SurfaceQuadric::SurfaceQuadric(pugi::xml_node surf_node) : Surface(surf_node)
859,518✔
843
{
844
  read_coeffs(
859,518✔
845
    surf_node, id_, {&A_, &B_, &C_, &D_, &E_, &F_, &G_, &H_, &J_, &K_});
846
}
847

4,663✔
848
double SurfaceQuadric::evaluate(Position r) const
849
{
4,663✔
850
  const double x = r.x;
4,663✔
851
  const double y = r.y;
4,663✔
852
  const double z = r.z;
4,663✔
853
  return x * (A_ * x + D_ * y + G_) + y * (B_ * y + E_ * z + H_) +
854
         z * (C_ * z + F_ * x + J_) + K_;
×
855
}
856

×
857
double SurfaceQuadric::distance(
×
858
  Position r, Direction ang, bool coincident) const
×
859
{
860
  const double& x = r.x;
×
861
  const double& y = r.y;
862
  const double& z = r.z;
863
  const double& u = ang.x;
864
  const double& v = ang.y;
865
  const double& w = ang.z;
866

867
  const double a =
868
    A_ * u * u + B_ * v * v + C_ * w * w + D_ * u * v + E_ * v * w + F_ * u * w;
869
  const double k = A_ * u * x + B_ * v * y + C_ * w * z +
870
                   0.5 * (D_ * (u * y + v * x) + E_ * (v * z + w * y) +
871
                           F_ * (w * x + u * z) + G_ * u + H_ * v + J_ * w);
872
  const double c = A_ * x * x + B_ * y * y + C_ * z * z + D_ * x * y +
322,586✔
873
                   E_ * y * z + F_ * x * z + G_ * x + H_ * y + J_ * z + K_;
874
  double quad = k * k - a * c;
875

322,586✔
876
  double d;
322,586✔
877

322,586✔
878
  if (quad < 0.0) {
322,586✔
879
    // No intersection with surface.
880
    return INFTY;
322,586✔
881

882
  } else if (coincident || std::abs(c) < FP_COINCIDENT) {
883
    // Particle is on the surface, thus one distance is positive/negative and
322,586✔
884
    // the other is zero. The sign of k determines which distance is zero and
322,586✔
885
    // which is not. Additionally, if a is zero, it means the particle is on
322,586✔
886
    // a plane-like surface.
322,586✔
887
    if (a == 0.0) {
888
      d = INFTY; // see the below explanation
×
889
    } else if (k >= 0.0) {
890
      d = (-k - sqrt(quad)) / a;
891
    } else {
×
892
      d = (-k + sqrt(quad)) / a;
×
893
    }
×
894

×
895
  } else if (a == 0.0) {
896
    // Given the orientation of the particle, the quadric looks like a plane in
×
897
    // this case, and thus we have only one solution despite potentially having
898
    // quad > 0.0. While the term under the square root may be real, in one
899
    // case of the +/- of the quadratic formula, 0/0 results, and in another, a
×
900
    // finite value over 0 results. Applying L'Hopital's to the 0/0 case gives
×
901
    // the below. Alternatively this can be found by simply putting a=0 in the
×
902
    // equation ax^2 + bx + c = 0.
×
903
    d = -0.5 * c / k;
904
  } else {
905
    // Calculate both solutions to the quadratic.
906
    quad = sqrt(quad);
907
    d = (-k - quad) / a;
908
    double b = (-k + quad) / a;
909

963,358✔
910
    // Determine the smallest positive solution.
911
    if (d < 0.0) {
912
      if (b > 0.0)
963,358✔
913
        d = b;
963,358✔
914
    } else {
963,358✔
915
      if (b > 0.0) {
963,358✔
916
        if (b < d)
963,358✔
917
          d = b;
963,358✔
918
      }
963,358✔
919
    }
963,358✔
920
  }
963,358✔
921

922
  // If the distance was negative, set boundary distance to infinity.
923
  if (d <= 0.0)
924
    return INFTY;
963,358✔
925
  return d;
926
}
×
927

928
Direction SurfaceQuadric::normal(Position r) const
963,358✔
929
{
930
  const double& x = r.x;
931
  const double& y = r.y;
932
  const double& z = r.z;
60,566✔
933
  return {2.0 * A_ * x + D_ * y + F_ * z + G_,
×
934
    2.0 * B_ * y + D_ * x + E_ * z + H_, 2.0 * C_ * z + E_ * y + F_ * x + J_};
935
}
60,566✔
936

937
void SurfaceQuadric::to_hdf5_inner(hid_t group_id) const
938
{
939
  write_string(group_id, "type", "quadric", false);
940
  array<double, 10> coeffs {{A_, B_, C_, D_, E_, F_, G_, H_, J_, K_}};
902,792✔
941
  write_dataset(group_id, "coefficients", coeffs);
902,792✔
942
}
902,792✔
943

944
//==============================================================================
945
// Torus helper functions
902,792✔
946
//==============================================================================
766,524✔
947

650,507✔
948
double torus_distance(double x1, double x2, double x3, double u1, double u2,
949
  double u3, double A, double B, double C, bool coincident)
136,268✔
950
{
136,268✔
951
  // Coefficients for equation: (c2 t^2 + c1 t + c0)^2 = c2' t^2 + c1' t + c0'
136,268✔
952
  double D = (C * C) / (B * B);
953
  double c2 = u1 * u1 + u2 * u2 + D * u3 * u3;
954
  double c1 = 2 * (u1 * x1 + u2 * x2 + D * u3 * x3);
955
  double c0 = x1 * x1 + x2 * x2 + D * x3 * x3 + A * A - C * C;
956
  double four_A2 = 4 * A * A;
957
  double c2p = four_A2 * (u1 * u1 + u2 * u2);
963,358✔
958
  double c1p = 2 * four_A2 * (u1 * x1 + u2 * x2);
137,467✔
959
  double c0p = four_A2 * (x1 * x1 + x2 * x2);
825,891✔
960

961
  // Coefficient for equation: a t^4 + b t^3 + c t^2 + d t + e = 0. If the point
963,358✔
962
  // is coincident, the 'e' coefficient should be zero. Explicitly setting it to
963
  // zero helps avoid numerical issues below with root finding.
964
  double coeff[5];
963,358✔
965
  coeff[0] = coincident ? 0.0 : c0 * c0 - c0p;
963,358✔
966
  coeff[1] = 2 * c0 * c1 - c1p;
963,358✔
967
  coeff[2] = c1 * c1 + 2 * c0 * c2 - c2p;
963,358✔
968
  coeff[3] = 2 * c1 * c2;
963,358✔
969
  coeff[4] = c2 * c2;
963,358✔
970

963,358✔
971
  std::complex<double> roots[4];
963,358✔
972
  oqs::quartic_solver(coeff, roots);
963,358✔
973

974
  // Find smallest positive, real root. In the case where the particle is
975
  // coincident with the surface, we are sure to have one root very close to
976
  // zero but possibly small and positive. A tolerance is set to discard that
963,358✔
977
  // zero.
978
  double distance = INFTY;
×
979
  double cutoff = coincident ? TORUS_TOL : 0.0;
980
  for (int i = 0; i < 4; ++i) {
963,358✔
981
    if (roots[i].imag() == 0) {
982
      double root = roots[i].real();
983
      if (root > cutoff && root < distance) {
984
        // Avoid roots corresponding to internal surfaces
60,566✔
985
        double s1 = x1 + u1 * root;
×
986
        double s2 = x2 + u2 * root;
987
        double s3 = x3 + u3 * root;
60,566✔
988
        double check = D * s3 * s3 + s1 * s1 + s2 * s2 + A * A - C * C;
989
        if (check >= 0) {
990
          distance = root;
991
        }
992
      }
902,792✔
993
    }
902,792✔
994
  }
902,792✔
995
  return distance;
996
}
997

902,792✔
998
//==============================================================================
766,524✔
999
// SurfaceXTorus implementation
650,507✔
1000
//==============================================================================
1001

136,268✔
1002
SurfaceXTorus::SurfaceXTorus(pugi::xml_node surf_node) : Surface(surf_node)
136,268✔
1003
{
136,268✔
1004
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &A_, &B_, &C_});
1005
}
1006

1007
void SurfaceXTorus::to_hdf5_inner(hid_t group_id) const
1008
{
1009
  write_string(group_id, "type", "x-torus", false);
963,358✔
1010
  std::array<double, 6> coeffs {{x0_, y0_, z0_, A_, B_, C_}};
137,467✔
1011
  write_dataset(group_id, "coefficients", coeffs);
825,891✔
1012
}
1013

×
1014
double SurfaceXTorus::evaluate(Position r) const
1015
{
1016
  double x = r.x - x0_;
×
1017
  double y = r.y - y0_;
×
1018
  double z = r.z - z0_;
×
1019
  return (x * x) / (B_ * B_) +
×
1020
         std::pow(std::sqrt(y * y + z * z) - A_, 2) / (C_ * C_) - 1.;
×
1021
}
×
1022

×
1023
double SurfaceXTorus::distance(Position r, Direction u, bool coincident) const
×
1024
{
×
1025
  double x = r.x - x0_;
1026
  double y = r.y - y0_;
1027
  double z = r.z - z0_;
1028
  return torus_distance(y, z, x, u.y, u.z, u.x, A_, B_, C_, coincident);
×
1029
}
1030

×
1031
Direction SurfaceXTorus::normal(Position r) const
1032
{
×
1033
  // reduce the expansion of the full form for torus
1034
  double x = r.x - x0_;
1035
  double y = r.y - y0_;
1036
  double z = r.z - z0_;
×
1037

×
1038
  // f(x,y,z) = x^2/B^2 + (sqrt(y^2 + z^2) - A)^2/C^2 - 1
1039
  // ∂f/∂x = 2x/B^2
×
1040
  // ∂f/∂y = 2y(g - A)/(g*C^2) where g = sqrt(y^2 + z^2)
1041
  // ∂f/∂z = 2z(g - A)/(g*C^2)
1042
  // Multiplying by g*C^2*B^2 / 2 gives:
1043
  double g = std::sqrt(y * y + z * z);
1044
  double nx = C_ * C_ * g * x;
×
1045
  double ny = y * (g - A_) * B_ * B_;
×
1046
  double nz = z * (g - A_) * B_ * B_;
×
1047
  Direction n(nx, ny, nz);
1048
  return n / n.norm();
1049
}
×
1050

×
1051
//==============================================================================
×
1052
// SurfaceYTorus implementation
1053
//==============================================================================
×
1054

×
1055
SurfaceYTorus::SurfaceYTorus(pugi::xml_node surf_node) : Surface(surf_node)
×
1056
{
1057
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &A_, &B_, &C_});
1058
}
1059

1060
void SurfaceYTorus::to_hdf5_inner(hid_t group_id) const
1061
{
×
1062
  write_string(group_id, "type", "y-torus", false);
×
1063
  std::array<double, 6> coeffs {{x0_, y0_, z0_, A_, B_, C_}};
×
1064
  write_dataset(group_id, "coefficients", coeffs);
1065
}
×
1066

1067
double SurfaceYTorus::evaluate(Position r) const
1068
{
×
1069
  double x = r.x - x0_;
×
1070
  double y = r.y - y0_;
×
1071
  double z = r.z - z0_;
×
1072
  return (y * y) / (B_ * B_) +
×
1073
         std::pow(std::sqrt(x * x + z * z) - A_, 2) / (C_ * C_) - 1.;
×
1074
}
×
1075

×
1076
double SurfaceYTorus::distance(Position r, Direction u, bool coincident) const
×
1077
{
1078
  double x = r.x - x0_;
1079
  double y = r.y - y0_;
1080
  double z = r.z - z0_;
×
1081
  return torus_distance(x, z, y, u.x, u.z, u.y, A_, B_, C_, coincident);
1082
}
×
1083

1084
Direction SurfaceYTorus::normal(Position r) const
×
1085
{
1086
  // reduce the expansion of the full form for torus
1087
  double x = r.x - x0_;
1088
  double y = r.y - y0_;
×
1089
  double z = r.z - z0_;
×
1090

1091
  // f(x,y,z) = y^2/B^2 + (sqrt(x^2 + z^2) - A)^2/C^2 - 1
×
1092
  // ∂f/∂x = 2x(g - A)/(g*C^2) where g = sqrt(x^2 + z^2)
1093
  // ∂f/∂y = 2y/B^2
1094
  // ∂f/∂z = 2z(g - A)/(g*C^2)
1095
  // Multiplying by g*C^2*B^2 / 2 gives:
1096
  double g = std::sqrt(x * x + z * z);
×
1097
  double nx = x * (g - A_) * B_ * B_;
×
1098
  double ny = C_ * C_ * g * y;
×
1099
  double nz = z * (g - A_) * B_ * B_;
1100
  Direction n(nx, ny, nz);
1101
  return n / n.norm();
×
1102
}
×
1103

×
1104
//==============================================================================
1105
// SurfaceZTorus implementation
×
1106
//==============================================================================
×
1107

×
1108
SurfaceZTorus::SurfaceZTorus(pugi::xml_node surf_node) : Surface(surf_node)
1109
{
1110
  read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &A_, &B_, &C_});
1111
}
1112

1113
void SurfaceZTorus::to_hdf5_inner(hid_t group_id) const
×
1114
{
×
1115
  write_string(group_id, "type", "z-torus", false);
×
1116
  std::array<double, 6> coeffs {{x0_, y0_, z0_, A_, B_, C_}};
1117
  write_dataset(group_id, "coefficients", coeffs);
1118
}
1119

1120
double SurfaceZTorus::evaluate(Position r) const
1121
{
1122
  double x = r.x - x0_;
60,566✔
1123
  double y = r.y - y0_;
1124
  double z = r.z - z0_;
1125
  return (z * z) / (B_ * B_) +
60,566✔
1126
         std::pow(std::sqrt(x * x + y * y) - A_, 2) / (C_ * C_) - 1.;
60,566✔
1127
}
60,566✔
1128

60,566✔
1129
double SurfaceZTorus::distance(Position r, Direction u, bool coincident) const
60,566✔
1130
{
1131
  double x = r.x - x0_;
60,566✔
1132
  double y = r.y - y0_;
1133
  double z = r.z - z0_;
1134
  return torus_distance(x, y, z, u.x, u.y, u.z, A_, B_, C_, coincident);
60,566✔
1135
}
60,566✔
1136

60,566✔
1137
Direction SurfaceZTorus::normal(Position r) const
60,566✔
1138
{
60,566✔
1139
  // reduce the expansion of the full form for torus
1140
  double x = r.x - x0_;
×
1141
  double y = r.y - y0_;
1142
  double z = r.z - z0_;
1143

×
1144
  // f(x,y,z) = z^2/B^2 + (sqrt(x^2 + y^2) - A)^2/C^2 - 1
×
1145
  // ∂f/∂x = 2x(g - A)/(g*C^2) where g = sqrt(x^2 + y^2)
×
1146
  // ∂f/∂y = 2y(g - A)/(g*C^2)
×
1147
  // ∂f/∂z = 2z/B^2
×
1148
  // Multiplying by g*C^2*B^2 / 2 gives:
1149
  double g = std::sqrt(x * x + y * y);
×
1150
  double nx = x * (g - A_) * B_ * B_;
1151
  double ny = y * (g - A_) * B_ * B_;
1152
  double nz = C_ * C_ * g * z;
×
1153
  Position n(nx, ny, nz);
×
1154
  return n / n.norm();
×
1155
}
×
1156

×
1157
//==============================================================================
1158

1159
void read_surfaces(pugi::xml_node node)
1160
{
1161
  // Count the number of surfaces
1162
  int n_surfaces = 0;
1163
  for (pugi::xml_node surf_node : node.children("surface")) {
×
1164
    n_surfaces++;
1165
  }
×
1166

1167
  // Loop over XML surface elements and populate the array.  Keep track of
1168
  // periodic surfaces and their albedos.
×
1169
  model::surfaces.reserve(n_surfaces);
1170
  std::set<std::pair<int, int>> periodic_pairs;
×
1171
  std::unordered_map<int, double> albedo_map;
1172
  {
1173
    pugi::xml_node surf_node;
×
1174
    int i_surf;
1175
    for (surf_node = node.child("surface"), i_surf = 0; surf_node;
×
1176
         surf_node = surf_node.next_sibling("surface"), i_surf++) {
×
1177
      std::string surf_type = get_node_value(surf_node, "type", true, true);
1178

1179
      // Allocate and initialize the new surface
×
1180

1181
      if (surf_type == "x-plane") {
×
1182
        model::surfaces.push_back(make_unique<SurfaceXPlane>(surf_node));
1183

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

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

1190
      } else if (surf_type == "plane") {
1191
        model::surfaces.push_back(make_unique<SurfacePlane>(surf_node));
1192

1193
      } else if (surf_type == "x-cylinder") {
1194
        model::surfaces.push_back(make_unique<SurfaceXCylinder>(surf_node));
1195

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

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

1202
      } else if (surf_type == "sphere") {
×
1203
        model::surfaces.push_back(make_unique<SurfaceSphere>(surf_node));
1204

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

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

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

×
1214
      } else if (surf_type == "quadric") {
1215
        model::surfaces.push_back(make_unique<SurfaceQuadric>(surf_node));
1216

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

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

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

1226
      } else {
1227
        fatal_error(fmt::format("Invalid surface type, \"{}\"", surf_type));
16✔
1228
      }
1229

16✔
1230
      // Check for a periodic surface
16✔
1231
      if (check_for_node(surf_node, "boundary")) {
1232
        std::string surf_bc = get_node_value(surf_node, "boundary", true, true);
322,586✔
1233
        if (surf_bc == "periodic") {
1234
          // Check for surface albedo. Skip sanity check as it is already done
322,586✔
1235
          // in the Surface class's constructor.
1236
          if (check_for_node(surf_node, "albedo")) {
1237
            albedo_map[model::surfaces.back()->id_] =
963,358✔
1238
              std::stod(get_node_value(surf_node, "albedo"));
1239
          }
963,358✔
1240
          if (check_for_node(surf_node, "periodic_surface_id")) {
963,358✔
1241
            int i_periodic =
1242
              std::stoi(get_node_value(surf_node, "periodic_surface_id"));
1243
            int lo_id = std::min(model::surfaces.back()->id_, i_periodic);
60,566✔
1244
            int hi_id = std::max(model::surfaces.back()->id_, i_periodic);
1245
            periodic_pairs.insert({lo_id, hi_id});
60,566✔
1246
          } else {
1247
            periodic_pairs.insert({model::surfaces.back()->id_, -1});
1248
          }
11✔
1249
        }
1250
      }
11✔
1251
    }
11✔
1252
  }
11✔
1253

11✔
1254
  // Fill the surface map
1255
  for (int i_surf = 0; i_surf < model::surfaces.size(); i_surf++) {
1256
    int id = model::surfaces[i_surf]->id_;
1257
    auto in_map = model::surface_map.find(id);
1258
    if (in_map == model::surface_map.end()) {
1259
      model::surface_map[id] = i_surf;
16✔
1260
    } else {
1261
      fatal_error(
×
1262
        fmt::format("Two or more surfaces use the same unique ID: {}", id));
16✔
1263
    }
16✔
1264
  }
1265

177,572✔
1266
  // Resolve unpaired periodic surfaces.  A lambda function is used with
1267
  // std::find_if to identify the unpaired surfaces.
177,572✔
1268
  auto is_unresolved_pair = [](const std::pair<int, int> p) {
177,572✔
1269
    return p.second == -1;
177,572✔
1270
  };
177,572✔
1271
  auto first_unresolved = std::find_if(
177,572✔
1272
    periodic_pairs.begin(), periodic_pairs.end(), is_unresolved_pair);
1273
  if (first_unresolved != periodic_pairs.end()) {
1274
    // Found one unpaired surface; search for a second one
312,510✔
1275
    auto next_elem = first_unresolved;
1276
    next_elem++;
1277
    auto second_unresolved =
312,510✔
1278
      std::find_if(next_elem, periodic_pairs.end(), is_unresolved_pair);
312,510✔
1279
    if (second_unresolved == periodic_pairs.end()) {
312,510✔
1280
      fatal_error("Found only one periodic surface without a specified partner."
312,510✔
1281
                  " Please specify the partner for each periodic surface.");
312,510✔
1282
    }
312,510✔
1283

1284
    // Make sure there isn't a third unpaired surface
312,510✔
1285
    next_elem = second_unresolved;
312,510✔
1286
    next_elem++;
312,510✔
1287
    auto third_unresolved =
312,510✔
1288
      std::find_if(next_elem, periodic_pairs.end(), is_unresolved_pair);
312,510✔
1289
    if (third_unresolved != periodic_pairs.end()) {
312,510✔
1290
      fatal_error(
312,510✔
1291
        "Found at least three periodic surfaces without a specified "
312,510✔
1292
        "partner. Please specify the partner for each periodic surface.");
1293
    }
1294

1295
    // Add the completed pair and remove the old, unpaired entries
312,510✔
1296
    int lo_id = std::min(first_unresolved->first, second_unresolved->first);
1297
    int hi_id = std::max(first_unresolved->first, second_unresolved->first);
×
1298
    periodic_pairs.insert({lo_id, hi_id});
1299
    periodic_pairs.erase(first_unresolved);
312,510✔
1300
    periodic_pairs.erase(second_unresolved);
1301
  }
1302

1303
  // Assign the periodic boundary conditions with albedos
1304
  for (auto periodic_pair : periodic_pairs) {
34,540✔
1305
    int i_surf = model::surface_map[periodic_pair.first];
×
1306
    int j_surf = model::surface_map[periodic_pair.second];
34,540✔
1307
    Surface& surf1 {*model::surfaces[i_surf]};
×
1308
    Surface& surf2 {*model::surfaces[j_surf]};
1309

34,540✔
1310
    // Compute the dot product of the surface normals
1311
    Direction norm1 = surf1.normal({0, 0, 0});
1312
    Direction norm2 = surf2.normal({0, 0, 0});
277,970✔
1313
    norm1 /= norm1.norm();
1314
    norm2 /= norm2.norm();
1315
    double dot_prod = norm1.dot(norm2);
1316

1317
    // If the dot product is 1 (to within floating point precision) then the
1318
    // planes are parallel which indicates a translational periodic boundary
1319
    // condition.  Otherwise, it is a rotational periodic BC.
1320
    if (std::abs(1.0 - dot_prod) < FP_PRECISION) {
×
1321
      surf1.bc_ = make_unique<TranslationalPeriodicBC>(i_surf, j_surf);
1322
      surf2.bc_ = make_unique<TranslationalPeriodicBC>(i_surf, j_surf);
1323
    } else {
277,970✔
1324
      surf1.bc_ = make_unique<RotationalPeriodicBC>(i_surf, j_surf);
277,970✔
1325
      surf2.bc_ = make_unique<RotationalPeriodicBC>(i_surf, j_surf);
277,970✔
1326
    }
1327

1328
    // If albedo data is present in albedo map, set the boundary albedo.
277,970✔
1329
    if (albedo_map.count(surf1.id_)) {
277,970✔
1330
      surf1.bc_->set_albedo(albedo_map[surf1.id_]);
277,970✔
1331
    }
1332
    if (albedo_map.count(surf2.id_)) {
×
1333
      surf2.bc_->set_albedo(albedo_map[surf2.id_]);
×
1334
    }
×
1335
  }
1336
}
1337

1338
void free_memory_surfaces()
1339
{
1340
  model::surfaces.clear();
312,510✔
1341
  model::surface_map.clear();
×
1342
}
312,510✔
1343

1344
} // namespace openmc
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc