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

openmc-dev / openmc / 21621898565

03 Feb 2026 07:57AM UTC coverage: 81.278% (-0.5%) from 81.763%
21621898565

Pull #3474

github

web-flow
Merge 7486f3ff0 into b41e22f68
Pull Request #3474: Cylindrical IndependentSource enhancements

17285 of 24269 branches covered (71.22%)

Branch coverage included in aggregate %.

56 of 64 new or added lines in 2 files covered. (87.5%)

402 existing lines in 9 files now uncovered.

55547 of 65340 relevant lines covered (85.01%)

34579169.11 hits per line

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

63.4
/src/distribution_spatial.cpp
1
#include "openmc/distribution_spatial.h"
2

3
#include "openmc/error.h"
4
#include "openmc/mesh.h"
5
#include "openmc/random_lcg.h"
6
#include "openmc/search.h"
7
#include "openmc/xml_interface.h"
8

9
namespace openmc {
10

11
//==============================================================================
12
// SpatialDistribution implementation
13
//==============================================================================
14

15
unique_ptr<SpatialDistribution> SpatialDistribution::create(pugi::xml_node node)
4,636✔
16
{
17
  // Check for type of spatial distribution and read
18
  std::string type;
4,636✔
19
  if (check_for_node(node, "type"))
4,636!
20
    type = get_node_value(node, "type", true, true);
4,636✔
21
  if (type == "cartesian") {
4,636✔
22
    return UPtrSpace {new CartesianIndependent(node)};
10✔
23
  } else if (type == "cylindrical") {
4,626✔
24
    return UPtrSpace {new CylindricalIndependent(node)};
30✔
25
  } else if (type == "spherical") {
4,596✔
26
    return UPtrSpace {new SphericalIndependent(node)};
51✔
27
  } else if (type == "mesh") {
4,545✔
28
    return UPtrSpace {new MeshSpatial(node)};
28✔
29
  } else if (type == "cloud") {
4,517✔
30
    return UPtrSpace {new PointCloud(node)};
7✔
31
  } else if (type == "box") {
4,510✔
32
    return UPtrSpace {new SpatialBox(node)};
2,502✔
33
  } else if (type == "fission") {
2,008✔
34
    return UPtrSpace {new SpatialBox(node, true)};
14✔
35
  } else if (type == "point") {
1,994!
36
    return UPtrSpace {new SpatialPoint(node)};
1,994✔
37
  } else {
38
    fatal_error(fmt::format(
×
39
      "Invalid spatial distribution for external source: {}", type));
40
  }
41
}
4,635✔
42

43
//==============================================================================
44
// CartesianIndependent implementation
45
//==============================================================================
46

47
CartesianIndependent::CartesianIndependent(pugi::xml_node node)
10✔
48
{
49
  // Read distribution for x coordinate
50
  if (check_for_node(node, "x")) {
10!
51
    pugi::xml_node node_dist = node.child("x");
10✔
52
    x_ = distribution_from_xml(node_dist);
10✔
53
  } else {
54
    // If no distribution was specified, default to a single point at x=0
55
    double x[] {0.0};
×
56
    double p[] {1.0};
×
57
    x_ = UPtrDist {new Discrete {x, p, 1}};
×
58
  }
59

60
  // Read distribution for y coordinate
61
  if (check_for_node(node, "y")) {
10!
62
    pugi::xml_node node_dist = node.child("y");
10✔
63
    y_ = distribution_from_xml(node_dist);
10✔
64
  } else {
65
    // If no distribution was specified, default to a single point at y=0
66
    double x[] {0.0};
×
67
    double p[] {1.0};
×
68
    y_ = UPtrDist {new Discrete {x, p, 1}};
×
69
  }
70

71
  // Read distribution for z coordinate
72
  if (check_for_node(node, "z")) {
10!
73
    pugi::xml_node node_dist = node.child("z");
10✔
74
    z_ = distribution_from_xml(node_dist);
10✔
75
  } else {
76
    // If no distribution was specified, default to a single point at z=0
77
    double x[] {0.0};
×
78
    double p[] {1.0};
×
79
    z_ = UPtrDist {new Discrete {x, p, 1}};
×
80
  }
81
}
10✔
82

83
std::pair<Position, double> CartesianIndependent::sample(uint64_t* seed) const
2,030✔
84
{
85
  auto [x_val, x_wgt] = x_->sample(seed);
2,030✔
86
  auto [y_val, y_wgt] = y_->sample(seed);
2,030✔
87
  auto [z_val, z_wgt] = z_->sample(seed);
2,030✔
88
  Position xi {x_val, y_val, z_val};
2,030✔
89
  return {xi, x_wgt * y_wgt * z_wgt};
2,030✔
90
}
91

92
//==============================================================================
93
// CylindricalIndependent implementation
94
//==============================================================================
95

96
CylindricalIndependent::CylindricalIndependent(pugi::xml_node node)
30✔
97
{
98
  // Read distribution for r-coordinate
99
  if (check_for_node(node, "r")) {
30!
100
    pugi::xml_node node_dist = node.child("r");
30✔
101
    r_ = distribution_from_xml(node_dist);
30✔
102
  } else {
103
    // If no distribution was specified, default to a single point at r=0
104
    double x[] {0.0};
×
105
    double p[] {1.0};
×
106
    r_ = make_unique<Discrete>(x, p, 1);
×
107
  }
108

109
  // Read distribution for phi-coordinate
110
  if (check_for_node(node, "phi")) {
30!
111
    pugi::xml_node node_dist = node.child("phi");
30✔
112
    phi_ = distribution_from_xml(node_dist);
30✔
113
  } else {
114
    // If no distribution was specified, default to a single point at phi=0
115
    double x[] {0.0};
×
116
    double p[] {1.0};
×
117
    phi_ = make_unique<Discrete>(x, p, 1);
×
118
  }
119

120
  // Read distribution for z-coordinate
121
  if (check_for_node(node, "z")) {
30!
122
    pugi::xml_node node_dist = node.child("z");
30✔
123
    z_ = distribution_from_xml(node_dist);
30✔
124
  } else {
125
    // If no distribution was specified, default to a single point at z=0
126
    double x[] {0.0};
×
127
    double p[] {1.0};
×
128
    z_ = make_unique<Discrete>(x, p, 1);
×
129
  }
130

131
  // Read cylinder center coordinates
132
  if (check_for_node(node, "origin")) {
30!
133
    auto origin = get_node_array<double>(node, "origin");
30✔
134
    if (origin.size() == 3) {
30!
135
      origin_ = origin;
30✔
136
    } else {
137
      fatal_error(
×
138
        "Origin for cylindrical source distribution must be length 3");
139
    }
140
  } else {
30✔
141
    // If no coordinates were specified, default to (0, 0, 0)
142
    origin_ = {0.0, 0.0, 0.0};
×
143
  }
144

145
  // Read cylinder z_dir
146
  if (check_for_node(node, "z_dir")) {
30!
147
    auto z_dir = get_node_array<double>(node, "z_dir");
30✔
148
    if (z_dir.size() == 3) {
30!
149
      z_dir_ = z_dir;
30✔
150
      z_dir_ /= z_dir_.norm();
30✔
151
    } else {
NEW
152
      fatal_error("z_dir for cylindrical source distribution must be length 3");
×
153
    }
154
  } else {
30✔
155
    // If no z_dir was specified, default to (0, 0, 1)
NEW
156
    z_dir_ = {0.0, 0.0, 1.0};
×
157
  }
158

159
  // Read cylinder r_dir
160
  if (check_for_node(node, "r_dir")) {
30!
161
    auto r_dir = get_node_array<double>(node, "r_dir");
30✔
162
    if (r_dir.size() == 3) {
30!
163
      r_dir_ = r_dir;
30✔
164
      r_dir_ /= r_dir_.norm();
30✔
165
    } else {
NEW
166
      fatal_error("r_dir for cylindrical source distribution must be length 3");
×
167
    }
168
  } else {
30✔
169
    // If no r_dir was specified, default to (1, 0, 0)
NEW
170
    r_dir_ = {1.0, 0.0, 0.0};
×
171
  }
172

173
  if (r_dir_.dot(z_dir_) > 1e-12)
30!
NEW
174
    fatal_error("r_dir must be perpendicular to z_dir");
×
175

176
  auto phi_dir = z_dir_.cross(r_dir_);
30✔
177
  phi_dir /= phi_dir.norm();
30✔
178
  phi_dir_ = phi_dir;
30✔
179
}
30✔
180

181
std::pair<Position, double> CylindricalIndependent::sample(uint64_t* seed) const
2,254✔
182
{
183
  auto [r, r_wgt] = r_->sample(seed);
2,254✔
184
  auto [phi, phi_wgt] = phi_->sample(seed);
2,254✔
185
  auto [z, z_wgt] = z_->sample(seed);
2,254✔
186
  Position xi =
187
    r * (cos(phi) * r_dir_ + sin(phi) * phi_dir_) + z * z_dir_ + origin_;
2,254✔
188
  return {xi, r_wgt * phi_wgt * z_wgt};
2,254✔
189
}
190

191
//==============================================================================
192
// SphericalIndependent implementation
193
//==============================================================================
194

195
SphericalIndependent::SphericalIndependent(pugi::xml_node node)
51✔
196
{
197
  // Read distribution for r-coordinate
198
  if (check_for_node(node, "r")) {
51!
199
    pugi::xml_node node_dist = node.child("r");
51✔
200
    r_ = distribution_from_xml(node_dist);
51✔
201
  } else {
202
    // If no distribution was specified, default to a single point at r=0
203
    double x[] {0.0};
×
204
    double p[] {1.0};
×
205
    r_ = make_unique<Discrete>(x, p, 1);
×
206
  }
207

208
  // Read distribution for cos_theta-coordinate
209
  if (check_for_node(node, "cos_theta")) {
51!
210
    pugi::xml_node node_dist = node.child("cos_theta");
51✔
211
    cos_theta_ = distribution_from_xml(node_dist);
51✔
212
  } else {
213
    // If no distribution was specified, default to a single point at
214
    // cos_theta=0
215
    double x[] {0.0};
×
216
    double p[] {1.0};
×
217
    cos_theta_ = make_unique<Discrete>(x, p, 1);
×
218
  }
219

220
  // Read distribution for phi-coordinate
221
  if (check_for_node(node, "phi")) {
51!
222
    pugi::xml_node node_dist = node.child("phi");
51✔
223
    phi_ = distribution_from_xml(node_dist);
51✔
224
  } else {
225
    // If no distribution was specified, default to a single point at phi=0
226
    double x[] {0.0};
×
227
    double p[] {1.0};
×
228
    phi_ = make_unique<Discrete>(x, p, 1);
×
229
  }
230

231
  // Read sphere center coordinates
232
  if (check_for_node(node, "origin")) {
51!
233
    auto origin = get_node_array<double>(node, "origin");
51✔
234
    if (origin.size() == 3) {
51!
235
      origin_ = origin;
51✔
236
    } else {
237
      fatal_error("Origin for spherical source distribution must be length 3");
×
238
    }
239
  } else {
51✔
240
    // If no coordinates were specified, default to (0, 0, 0)
241
    origin_ = {0.0, 0.0, 0.0};
×
242
  }
243
}
51✔
244

245
std::pair<Position, double> SphericalIndependent::sample(uint64_t* seed) const
181,456✔
246
{
247
  auto [r, r_wgt] = r_->sample(seed);
181,456✔
248
  auto [cos_theta, cos_theta_wgt] = cos_theta_->sample(seed);
181,456✔
249
  auto [phi, phi_wgt] = phi_->sample(seed);
181,456✔
250
  // sin(theta) by sin**2 + cos**2 = 1
251
  double x = r * std::sqrt(1 - cos_theta * cos_theta) * cos(phi) + origin_.x;
181,456✔
252
  double y = r * std::sqrt(1 - cos_theta * cos_theta) * sin(phi) + origin_.y;
181,456✔
253
  double z = r * cos_theta + origin_.z;
181,456✔
254
  Position xi {x, y, z};
181,456✔
255
  return {xi, r_wgt * cos_theta_wgt * phi_wgt};
181,456✔
256
}
257

258
//==============================================================================
259
// MeshSpatial implementation
260
//==============================================================================
261

262
MeshSpatial::MeshSpatial(pugi::xml_node node)
28✔
263
{
264

265
  if (get_node_value(node, "type", true, true) != "mesh") {
28!
266
    fatal_error(fmt::format(
×
267
      "Incorrect spatial type '{}' for a MeshSpatial distribution"));
268
  }
269

270
  // No in-tet distributions implemented, could include distributions for the
271
  // barycentric coords Read in unstructured mesh from mesh_id value
272
  int32_t mesh_id = std::stoi(get_node_value(node, "mesh_id"));
28✔
273
  // Get pointer to spatial distribution
274
  mesh_idx_ = model::mesh_map.at(mesh_id);
28✔
275

276
  const auto mesh_ptr = model::meshes.at(mesh_idx_).get();
28✔
277

278
  check_element_types();
28✔
279

280
  size_t n_bins = this->n_sources();
28✔
281
  std::vector<double> strengths(n_bins, 1.0);
28✔
282

283
  // Create cdfs for sampling for an element over a mesh
284
  // Volume scheme is weighted by the volume of each tet
285
  // File scheme is weighted by an array given in the xml file
286
  if (check_for_node(node, "strengths")) {
28✔
287
    strengths = get_node_array<double>(node, "strengths");
25✔
288
    if (strengths.size() != n_bins) {
25✔
289
      fatal_error(
1✔
290
        fmt::format("Number of entries in the source strengths array {} does "
1!
291
                    "not match the number of entities in mesh {} ({}).",
292
          strengths.size(), mesh_id, n_bins));
1✔
293
    }
294
  }
295

296
  if (get_node_value_bool(node, "volume_normalized")) {
27✔
297
    for (int i = 0; i < n_bins; i++) {
36,864✔
298
      strengths[i] *= this->mesh()->volume(i);
36,840✔
299
    }
300
  }
301

302
  elem_idx_dist_.assign(strengths);
27✔
303

304
  if (check_for_node(node, "bias")) {
27!
305
    pugi::xml_node bias_node = node.child("bias");
×
306

307
    if (check_for_node(bias_node, "strengths")) {
×
308
      std::vector<double> bias_strengths(n_bins, 1.0);
×
309
      bias_strengths = get_node_array<double>(node, "strengths");
×
310

311
      if (bias_strengths.size() != n_bins) {
×
312
        fatal_error(
×
313
          fmt::format("Number of entries in the bias strengths array {} does "
×
314
                      "not match the number of entities in mesh {} ({}).",
315
            bias_strengths.size(), mesh_id, n_bins));
×
316
      }
317

318
      if (get_node_value_bool(node, "volume_normalized")) {
×
319
        for (int i = 0; i < n_bins; i++) {
×
320
          bias_strengths[i] *= this->mesh()->volume(i);
×
321
        }
322
      }
323

324
      // Compute importance weights
325
      weight_ = compute_importance_weights(strengths, bias_strengths);
×
326

327
      // Re-initialize DiscreteIndex with bias strengths for sampling
328
      elem_idx_dist_.assign(bias_strengths);
×
329
    } else {
×
330
      fatal_error(fmt::format(
×
331
        "Bias node for mesh {} found without strengths array.", mesh_id));
332
    }
333
  }
334
}
27✔
335

336
MeshSpatial::MeshSpatial(int32_t mesh_idx, span<const double> strengths)
129✔
337
  : mesh_idx_(mesh_idx)
129✔
338
{
339
  check_element_types();
129✔
340
  elem_idx_dist_.assign(strengths);
129✔
341
}
129✔
342

343
void MeshSpatial::check_element_types() const
157✔
344
{
345
  const auto umesh_ptr = dynamic_cast<const UnstructuredMesh*>(this->mesh());
157!
346
  if (umesh_ptr) {
157✔
347
    // ensure that the unstructured mesh contains only linear tets
348
    for (int bin = 0; bin < umesh_ptr->n_bins(); bin++) {
120,010✔
349
      if (umesh_ptr->element_type(bin) != ElementType::LINEAR_TET) {
120,000!
350
        fatal_error(
×
351
          "Mesh specified for source must contain only linear tetrahedra.");
352
      }
353
    }
354
  }
355
}
157✔
356

357
int32_t MeshSpatial::sample_element_index(uint64_t* seed) const
1,566,270✔
358
{
359
  return elem_idx_dist_.sample(seed);
1,566,270✔
360
}
361

362
std::pair<int32_t, Position> MeshSpatial::sample_mesh(uint64_t* seed) const
601,410✔
363
{
364
  // Sample the CDF defined in initialization above
365
  int32_t elem_idx = this->sample_element_index(seed);
601,410✔
366
  return {elem_idx, mesh()->sample_element(elem_idx, seed)};
601,410✔
367
}
368

369
std::pair<Position, double> MeshSpatial::sample(uint64_t* seed) const
601,410✔
370
{
371
  auto [elem_idx, u] = this->sample_mesh(seed);
601,410✔
372
  double wgt = weight_.empty() ? 1.0 : weight_[elem_idx];
601,410!
373
  return {u, wgt};
1,202,820✔
374
}
375

376
//==============================================================================
377
// PointCloud implementation
378
//==============================================================================
379

380
PointCloud::PointCloud(pugi::xml_node node)
7✔
381
{
382
  if (check_for_node(node, "coords")) {
7!
383
    point_cloud_ = get_node_position_array(node, "coords");
7✔
384
  } else {
385
    fatal_error("No coordinates were provided for the PointCloud "
×
386
                "spatial distribution");
387
  }
388

389
  std::vector<double> strengths;
7✔
390

391
  if (check_for_node(node, "strengths"))
7!
392
    strengths = get_node_array<double>(node, "strengths");
7✔
393
  else
394
    strengths.resize(point_cloud_.size(), 1.0);
×
395

396
  if (strengths.size() != point_cloud_.size()) {
7!
397
    fatal_error(
×
398
      fmt::format("Number of entries for the strengths array {} does "
×
399
                  "not match the number of spatial points provided {}.",
400
        strengths.size(), point_cloud_.size()));
×
401
  }
402

403
  point_idx_dist_.assign(strengths);
7✔
404

405
  if (check_for_node(node, "bias")) {
7!
406
    pugi::xml_node bias_node = node.child("bias");
×
407

408
    if (check_for_node(bias_node, "strengths")) {
×
409
      std::vector<double> bias_strengths(point_cloud_.size(), 1.0);
×
410
      bias_strengths = get_node_array<double>(node, "strengths");
×
411

412
      if (bias_strengths.size() != point_cloud_.size()) {
×
413
        fatal_error(
×
414
          fmt::format("Number of entries in the bias strengths array {} does "
×
415
                      "not match the number of spatial points provided {}.",
416
            bias_strengths.size(), point_cloud_.size()));
×
417
      }
418

419
      // Compute importance weights
420
      weight_ = compute_importance_weights(strengths, bias_strengths);
×
421

422
      // Re-initialize DiscreteIndex with bias strengths for sampling
423
      point_idx_dist_.assign(bias_strengths);
×
424
    } else {
×
425
      fatal_error(
×
426
        fmt::format("Bias node for PointCloud found without strengths array."));
×
427
    }
428
  }
429
}
7✔
430

431
PointCloud::PointCloud(
×
432
  std::vector<Position> point_cloud, span<const double> strengths)
×
433
{
434
  point_cloud_.assign(point_cloud.begin(), point_cloud.end());
×
435
  point_idx_dist_.assign(strengths);
×
436
}
×
437

438
std::pair<Position, double> PointCloud::sample(uint64_t* seed) const
350,000✔
439
{
440
  int32_t index = point_idx_dist_.sample(seed);
350,000✔
441
  double wgt = weight_.empty() ? 1.0 : weight_[index];
350,000!
442
  return {point_cloud_[index], wgt};
700,000✔
443
}
444

445
//==============================================================================
446
// SpatialBox implementation
447
//==============================================================================
448

449
SpatialBox::SpatialBox(pugi::xml_node node, bool fission)
2,516✔
450
  : only_fissionable_ {fission}
2,516✔
451
{
452
  // Read lower-right/upper-left coordinates
453
  auto params = get_node_array<double>(node, "parameters");
2,516✔
454
  if (params.size() != 6)
2,516!
455
    openmc::fatal_error("Box/fission spatial source must have six "
×
456
                        "parameters specified.");
457

458
  lower_left_ = Position {params[0], params[1], params[2]};
2,516✔
459
  upper_right_ = Position {params[3], params[4], params[5]};
2,516✔
460
}
2,516✔
461

462
SpatialBox::SpatialBox(Position lower_left, Position upper_right, bool fission)
7✔
463
  : lower_left_(lower_left), upper_right_(upper_right),
7✔
464
    only_fissionable_(fission)
7✔
465
{}
7✔
466

467
std::pair<Position, double> SpatialBox::sample(uint64_t* seed) const
4,107,332✔
468
{
469
  Position xi {prn(seed), prn(seed), prn(seed)};
4,107,332✔
470
  return {lower_left_ + xi * (upper_right_ - lower_left_), 1.0};
4,107,332✔
471
}
472

473
//==============================================================================
474
// SpatialPoint implementation
475
//==============================================================================
476

477
SpatialPoint::SpatialPoint(pugi::xml_node node)
1,994✔
478
{
479
  // Read location of point source
480
  auto params = get_node_array<double>(node, "parameters");
1,994✔
481
  if (params.size() != 3)
1,994!
482
    openmc::fatal_error("Point spatial source must have three "
×
483
                        "parameters specified.");
484

485
  // Set position
486
  r_ = Position {params.data()};
1,994✔
487
}
1,994✔
488

489
std::pair<Position, double> SpatialPoint::sample(uint64_t* seed) const
16,893,404✔
490
{
491
  return {r_, 1.0};
16,893,404✔
492
}
493

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

© 2026 Coveralls, Inc