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

openmc-dev / openmc / 21597104861

02 Feb 2026 03:53PM UTC coverage: 81.674% (-0.3%) from 81.968%
21597104861

Pull #3751

github

web-flow
Merge 4a6501226 into 6041ee6ae
Pull Request #3751: Resolve conflict with weight windows and global russian roulette

16697 of 23049 branches covered (72.44%)

Branch coverage included in aggregate %.

76 of 84 new or added lines in 5 files covered. (90.48%)

471 existing lines in 31 files now uncovered.

54833 of 64531 relevant lines covered (84.97%)

37154420.72 hits per line

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

61.32
/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)
5,342✔
16
{
17
  // Check for type of spatial distribution and read
18
  std::string type;
5,342✔
19
  if (check_for_node(node, "type"))
5,342!
20
    type = get_node_value(node, "type", true, true);
5,342✔
21
  if (type == "cartesian") {
5,342✔
22
    return UPtrSpace {new CartesianIndependent(node)};
12✔
23
  } else if (type == "cylindrical") {
5,330✔
24
    return UPtrSpace {new CylindricalIndependent(node)};
36✔
25
  } else if (type == "spherical") {
5,294✔
26
    return UPtrSpace {new SphericalIndependent(node)};
34✔
27
  } else if (type == "mesh") {
5,260✔
28
    return UPtrSpace {new MeshSpatial(node)};
26✔
29
  } else if (type == "cloud") {
5,234✔
30
    return UPtrSpace {new PointCloud(node)};
8✔
31
  } else if (type == "box") {
5,226✔
32
    return UPtrSpace {new SpatialBox(node)};
2,926✔
33
  } else if (type == "fission") {
2,300✔
34
    return UPtrSpace {new SpatialBox(node, true)};
16✔
35
  } else if (type == "point") {
2,284!
36
    return UPtrSpace {new SpatialPoint(node)};
2,284✔
37
  } else {
38
    fatal_error(fmt::format(
×
39
      "Invalid spatial distribution for external source: {}", type));
40
  }
41
}
5,342✔
42

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

47
CartesianIndependent::CartesianIndependent(pugi::xml_node node)
12✔
48
{
49
  // Read distribution for x coordinate
50
  if (check_for_node(node, "x")) {
12!
51
    pugi::xml_node node_dist = node.child("x");
12✔
52
    x_ = distribution_from_xml(node_dist);
12✔
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")) {
12!
62
    pugi::xml_node node_dist = node.child("y");
12✔
63
    y_ = distribution_from_xml(node_dist);
12✔
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")) {
12!
73
    pugi::xml_node node_dist = node.child("z");
12✔
74
    z_ = distribution_from_xml(node_dist);
12✔
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
}
12✔
82

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

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

96
CylindricalIndependent::CylindricalIndependent(pugi::xml_node node)
36✔
97
{
98
  // Read distribution for r-coordinate
99
  if (check_for_node(node, "r")) {
36!
100
    pugi::xml_node node_dist = node.child("r");
36✔
101
    r_ = distribution_from_xml(node_dist);
36✔
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")) {
36!
111
    pugi::xml_node node_dist = node.child("phi");
36✔
112
    phi_ = distribution_from_xml(node_dist);
36✔
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")) {
36!
122
    pugi::xml_node node_dist = node.child("z");
36✔
123
    z_ = distribution_from_xml(node_dist);
36✔
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")) {
36!
133
    auto origin = get_node_array<double>(node, "origin");
36✔
134
    if (origin.size() == 3) {
36!
135
      origin_ = origin;
36✔
136
    } else {
137
      fatal_error(
×
138
        "Origin for cylindrical source distribution must be length 3");
139
    }
140
  } else {
36✔
141
    // If no coordinates were specified, default to (0, 0, 0)
142
    origin_ = {0.0, 0.0, 0.0};
×
143
  }
144
}
36✔
145

146
std::pair<Position, double> CylindricalIndependent::sample(uint64_t* seed) const
2,576✔
147
{
148
  auto [r, r_wgt] = r_->sample(seed);
2,576✔
149
  auto [phi, phi_wgt] = phi_->sample(seed);
2,576✔
150
  auto [z, z_wgt] = z_->sample(seed);
2,576✔
151
  double x = r * cos(phi) + origin_.x;
2,576✔
152
  double y = r * sin(phi) + origin_.y;
2,576✔
153
  z += origin_.z;
2,576✔
154
  Position xi {x, y, z};
2,576✔
155
  return {xi, r_wgt * phi_wgt * z_wgt};
2,576✔
156
}
157

158
//==============================================================================
159
// SphericalIndependent implementation
160
//==============================================================================
161

162
SphericalIndependent::SphericalIndependent(pugi::xml_node node)
34✔
163
{
164
  // Read distribution for r-coordinate
165
  if (check_for_node(node, "r")) {
34!
166
    pugi::xml_node node_dist = node.child("r");
34✔
167
    r_ = distribution_from_xml(node_dist);
34✔
168
  } else {
169
    // If no distribution was specified, default to a single point at r=0
170
    double x[] {0.0};
×
171
    double p[] {1.0};
×
172
    r_ = make_unique<Discrete>(x, p, 1);
×
173
  }
174

175
  // Read distribution for cos_theta-coordinate
176
  if (check_for_node(node, "cos_theta")) {
34!
177
    pugi::xml_node node_dist = node.child("cos_theta");
34✔
178
    cos_theta_ = distribution_from_xml(node_dist);
34✔
179
  } else {
180
    // If no distribution was specified, default to a single point at
181
    // cos_theta=0
182
    double x[] {0.0};
×
183
    double p[] {1.0};
×
184
    cos_theta_ = make_unique<Discrete>(x, p, 1);
×
185
  }
186

187
  // Read distribution for phi-coordinate
188
  if (check_for_node(node, "phi")) {
34!
189
    pugi::xml_node node_dist = node.child("phi");
34✔
190
    phi_ = distribution_from_xml(node_dist);
34✔
191
  } else {
192
    // If no distribution was specified, default to a single point at phi=0
193
    double x[] {0.0};
×
194
    double p[] {1.0};
×
195
    phi_ = make_unique<Discrete>(x, p, 1);
×
196
  }
197

198
  // Read sphere center coordinates
199
  if (check_for_node(node, "origin")) {
34!
200
    auto origin = get_node_array<double>(node, "origin");
34✔
201
    if (origin.size() == 3) {
34!
202
      origin_ = origin;
34✔
203
    } else {
204
      fatal_error("Origin for spherical source distribution must be length 3");
×
205
    }
206
  } else {
34✔
207
    // If no coordinates were specified, default to (0, 0, 0)
208
    origin_ = {0.0, 0.0, 0.0};
×
209
  }
210
}
34✔
211

212
std::pair<Position, double> SphericalIndependent::sample(uint64_t* seed) const
51,664✔
213
{
214
  auto [r, r_wgt] = r_->sample(seed);
51,664✔
215
  auto [cos_theta, cos_theta_wgt] = cos_theta_->sample(seed);
51,664✔
216
  auto [phi, phi_wgt] = phi_->sample(seed);
51,664✔
217
  // sin(theta) by sin**2 + cos**2 = 1
218
  double x = r * std::sqrt(1 - cos_theta * cos_theta) * cos(phi) + origin_.x;
51,664✔
219
  double y = r * std::sqrt(1 - cos_theta * cos_theta) * sin(phi) + origin_.y;
51,664✔
220
  double z = r * cos_theta + origin_.z;
51,664✔
221
  Position xi {x, y, z};
51,664✔
222
  return {xi, r_wgt * cos_theta_wgt * phi_wgt};
51,664✔
223
}
224

225
//==============================================================================
226
// MeshSpatial implementation
227
//==============================================================================
228

229
MeshSpatial::MeshSpatial(pugi::xml_node node)
26✔
230
{
231

232
  if (get_node_value(node, "type", true, true) != "mesh") {
26!
233
    fatal_error(fmt::format(
×
234
      "Incorrect spatial type '{}' for a MeshSpatial distribution"));
235
  }
236

237
  // No in-tet distributions implemented, could include distributions for the
238
  // barycentric coords Read in unstructured mesh from mesh_id value
239
  int32_t mesh_id = std::stoi(get_node_value(node, "mesh_id"));
26✔
240
  // Get pointer to spatial distribution
241
  mesh_idx_ = model::mesh_map.at(mesh_id);
26✔
242

243
  const auto mesh_ptr = model::meshes.at(mesh_idx_).get();
26✔
244

245
  check_element_types();
26✔
246

247
  size_t n_bins = this->n_sources();
26✔
248
  std::vector<double> strengths(n_bins, 1.0);
26✔
249

250
  // Create cdfs for sampling for an element over a mesh
251
  // Volume scheme is weighted by the volume of each tet
252
  // File scheme is weighted by an array given in the xml file
253
  if (check_for_node(node, "strengths")) {
26✔
254
    strengths = get_node_array<double>(node, "strengths");
25✔
255
    if (strengths.size() != n_bins) {
25!
UNCOV
256
      fatal_error(
×
UNCOV
257
        fmt::format("Number of entries in the source strengths array {} does "
×
258
                    "not match the number of entities in mesh {} ({}).",
UNCOV
259
          strengths.size(), mesh_id, n_bins));
×
260
    }
261
  }
262

263
  if (get_node_value_bool(node, "volume_normalized")) {
26✔
264
    for (int i = 0; i < n_bins; i++) {
12,985✔
265
      strengths[i] *= this->mesh()->volume(i);
12,960✔
266
    }
267
  }
268

269
  elem_idx_dist_.assign(strengths);
26✔
270

271
  if (check_for_node(node, "bias")) {
26!
272
    pugi::xml_node bias_node = node.child("bias");
×
273

274
    if (check_for_node(bias_node, "strengths")) {
×
275
      std::vector<double> bias_strengths(n_bins, 1.0);
×
276
      bias_strengths = get_node_array<double>(node, "strengths");
×
277

278
      if (bias_strengths.size() != n_bins) {
×
279
        fatal_error(
×
280
          fmt::format("Number of entries in the bias strengths array {} does "
×
281
                      "not match the number of entities in mesh {} ({}).",
282
            bias_strengths.size(), mesh_id, n_bins));
×
283
      }
284

285
      if (get_node_value_bool(node, "volume_normalized")) {
×
286
        for (int i = 0; i < n_bins; i++) {
×
287
          bias_strengths[i] *= this->mesh()->volume(i);
×
288
        }
289
      }
290

291
      // Compute importance weights
292
      weight_ = compute_importance_weights(strengths, bias_strengths);
×
293

294
      // Re-initialize DiscreteIndex with bias strengths for sampling
295
      elem_idx_dist_.assign(bias_strengths);
×
296
    } else {
×
297
      fatal_error(fmt::format(
×
298
        "Bias node for mesh {} found without strengths array.", mesh_id));
299
    }
300
  }
301
}
26✔
302

303
MeshSpatial::MeshSpatial(int32_t mesh_idx, span<const double> strengths)
145✔
304
  : mesh_idx_(mesh_idx)
145✔
305
{
306
  check_element_types();
145✔
307
  elem_idx_dist_.assign(strengths);
145✔
308
}
145✔
309

310
void MeshSpatial::check_element_types() const
171✔
311
{
312
  const auto umesh_ptr = dynamic_cast<const UnstructuredMesh*>(this->mesh());
171!
313
  if (umesh_ptr) {
171✔
314
    // ensure that the unstructured mesh contains only linear tets
315
    for (int bin = 0; bin < umesh_ptr->n_bins(); bin++) {
36,003✔
316
      if (umesh_ptr->element_type(bin) != ElementType::LINEAR_TET) {
36,000!
317
        fatal_error(
×
318
          "Mesh specified for source must contain only linear tetrahedra.");
319
      }
320
    }
321
  }
322
}
171✔
323

324
int32_t MeshSpatial::sample_element_index(uint64_t* seed) const
1,311,542✔
325
{
326
  return elem_idx_dist_.sample(seed);
1,311,542✔
327
}
328

329
std::pair<int32_t, Position> MeshSpatial::sample_mesh(uint64_t* seed) const
200,640✔
330
{
331
  // Sample the CDF defined in initialization above
332
  int32_t elem_idx = this->sample_element_index(seed);
200,640✔
333
  return {elem_idx, mesh()->sample_element(elem_idx, seed)};
200,640✔
334
}
335

336
std::pair<Position, double> MeshSpatial::sample(uint64_t* seed) const
200,640✔
337
{
338
  auto [elem_idx, u] = this->sample_mesh(seed);
200,640✔
339
  double wgt = weight_.empty() ? 1.0 : weight_[elem_idx];
200,640!
340
  return {u, wgt};
401,280✔
341
}
342

343
//==============================================================================
344
// PointCloud implementation
345
//==============================================================================
346

347
PointCloud::PointCloud(pugi::xml_node node)
8✔
348
{
349
  if (check_for_node(node, "coords")) {
8!
350
    point_cloud_ = get_node_position_array(node, "coords");
8✔
351
  } else {
352
    fatal_error("No coordinates were provided for the PointCloud "
×
353
                "spatial distribution");
354
  }
355

356
  std::vector<double> strengths;
8✔
357

358
  if (check_for_node(node, "strengths"))
8!
359
    strengths = get_node_array<double>(node, "strengths");
8✔
360
  else
361
    strengths.resize(point_cloud_.size(), 1.0);
×
362

363
  if (strengths.size() != point_cloud_.size()) {
8!
364
    fatal_error(
×
365
      fmt::format("Number of entries for the strengths array {} does "
×
366
                  "not match the number of spatial points provided {}.",
367
        strengths.size(), point_cloud_.size()));
×
368
  }
369

370
  point_idx_dist_.assign(strengths);
8✔
371

372
  if (check_for_node(node, "bias")) {
8!
373
    pugi::xml_node bias_node = node.child("bias");
×
374

375
    if (check_for_node(bias_node, "strengths")) {
×
376
      std::vector<double> bias_strengths(point_cloud_.size(), 1.0);
×
377
      bias_strengths = get_node_array<double>(node, "strengths");
×
378

379
      if (bias_strengths.size() != point_cloud_.size()) {
×
380
        fatal_error(
×
381
          fmt::format("Number of entries in the bias strengths array {} does "
×
382
                      "not match the number of spatial points provided {}.",
383
            bias_strengths.size(), point_cloud_.size()));
×
384
      }
385

386
      // Compute importance weights
387
      weight_ = compute_importance_weights(strengths, bias_strengths);
×
388

389
      // Re-initialize DiscreteIndex with bias strengths for sampling
390
      point_idx_dist_.assign(bias_strengths);
×
391
    } else {
×
392
      fatal_error(
×
393
        fmt::format("Bias node for PointCloud found without strengths array."));
×
394
    }
395
  }
396
}
8✔
397

398
PointCloud::PointCloud(
×
399
  std::vector<Position> point_cloud, span<const double> strengths)
×
400
{
401
  point_cloud_.assign(point_cloud.begin(), point_cloud.end());
×
402
  point_idx_dist_.assign(strengths);
×
403
}
×
404

405
std::pair<Position, double> PointCloud::sample(uint64_t* seed) const
400,000✔
406
{
407
  int32_t index = point_idx_dist_.sample(seed);
400,000✔
408
  double wgt = weight_.empty() ? 1.0 : weight_[index];
400,000!
409
  return {point_cloud_[index], wgt};
800,000✔
410
}
411

412
//==============================================================================
413
// SpatialBox implementation
414
//==============================================================================
415

416
SpatialBox::SpatialBox(pugi::xml_node node, bool fission)
2,942✔
417
  : only_fissionable_ {fission}
2,942✔
418
{
419
  // Read lower-right/upper-left coordinates
420
  auto params = get_node_array<double>(node, "parameters");
2,942✔
421
  if (params.size() != 6)
2,942!
422
    openmc::fatal_error("Box/fission spatial source must have six "
×
423
                        "parameters specified.");
424

425
  lower_left_ = Position {params[0], params[1], params[2]};
2,942✔
426
  upper_right_ = Position {params[3], params[4], params[5]};
2,942✔
427
}
2,942✔
428

429
SpatialBox::SpatialBox(Position lower_left, Position upper_right, bool fission)
8✔
430
  : lower_left_(lower_left), upper_right_(upper_right),
8✔
431
    only_fissionable_(fission)
8✔
432
{}
8✔
433

434
std::pair<Position, double> SpatialBox::sample(uint64_t* seed) const
4,657,484✔
435
{
436
  Position xi {prn(seed), prn(seed), prn(seed)};
4,657,484✔
437
  return {lower_left_ + xi * (upper_right_ - lower_left_), 1.0};
4,657,484✔
438
}
439

440
//==============================================================================
441
// SpatialPoint implementation
442
//==============================================================================
443

444
SpatialPoint::SpatialPoint(pugi::xml_node node)
2,284✔
445
{
446
  // Read location of point source
447
  auto params = get_node_array<double>(node, "parameters");
2,284✔
448
  if (params.size() != 3)
2,284!
449
    openmc::fatal_error("Point spatial source must have three "
×
450
                        "parameters specified.");
451

452
  // Set position
453
  r_ = Position {params.data()};
2,284✔
454
}
2,284✔
455

456
std::pair<Position, double> SpatialPoint::sample(uint64_t* seed) const
19,269,837✔
457
{
458
  return {r_, 1.0};
19,269,837✔
459
}
460

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