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

openmc-dev / openmc / 22242348990

20 Feb 2026 09:43PM UTC coverage: 81.696% (-0.1%) from 81.826%
22242348990

Pull #3830

github

web-flow
Merge 319bf9b1f into 53ce1910f
Pull Request #3830: Parallelize sampling external sources and threadsafe rejection counters

16751 of 23332 branches covered (71.79%)

Branch coverage included in aggregate %.

65 of 74 new or added lines in 5 files covered. (87.84%)

319 existing lines in 27 files now uncovered.

56726 of 66608 relevant lines covered (85.16%)

43263289.88 hits per line

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

79.25
/src/source.cpp
1
#include "openmc/source.h"
2

3
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
4
#define HAS_DYNAMIC_LINKING
5
#endif
6

7
#include <utility> // for move
8

9
#ifdef HAS_DYNAMIC_LINKING
10
#include <dlfcn.h> // for dlopen, dlsym, dlclose, dlerror
11
#endif
12

13
#include "openmc/tensor.h"
14
#include <fmt/core.h>
15

16
#include "openmc/bank.h"
17
#include "openmc/capi.h"
18
#include "openmc/cell.h"
19
#include "openmc/container_util.h"
20
#include "openmc/error.h"
21
#include "openmc/file_utils.h"
22
#include "openmc/geometry.h"
23
#include "openmc/hdf5_interface.h"
24
#include "openmc/material.h"
25
#include "openmc/mcpl_interface.h"
26
#include "openmc/memory.h"
27
#include "openmc/message_passing.h"
28
#include "openmc/mgxs_interface.h"
29
#include "openmc/nuclide.h"
30
#include "openmc/random_lcg.h"
31
#include "openmc/search.h"
32
#include "openmc/settings.h"
33
#include "openmc/simulation.h"
34
#include "openmc/state_point.h"
35
#include "openmc/string_utils.h"
36
#include "openmc/xml_interface.h"
37

38
namespace openmc {
39

40
std::atomic<int64_t> source_n_accept {0};
41
std::atomic<int64_t> source_n_reject {0};
42

43
namespace {
44

45
void validate_particle_type(ParticleType type, const std::string& context)
194,880✔
46
{
47
  if (type.is_transportable())
194,880!
48
    return;
194,880✔
49

50
  fatal_error(
×
51
    fmt::format("Unsupported source particle type '{}' (PDG {}) in {}.",
×
52
      type.str(), type.pdg_number(), context));
×
53
}
54

55
} // namespace
56

57
//==============================================================================
58
// Global variables
59
//==============================================================================
60

61
namespace model {
62

63
vector<unique_ptr<Source>> external_sources;
64

65
DiscreteIndex external_sources_probability;
66

67
} // namespace model
68

69
//==============================================================================
70
// Source implementation
71
//==============================================================================
72

73
Source::Source(pugi::xml_node node)
31,920✔
74
{
75
  // Check for source strength
76
  if (check_for_node(node, "strength")) {
31,920✔
77
    strength_ = std::stod(get_node_value(node, "strength"));
31,404✔
78
    if (strength_ < 0.0) {
31,404!
79
      fatal_error("Source strength is negative.");
×
80
    }
81
  }
82

83
  // Check for additional defined constraints
84
  read_constraints(node);
31,920✔
85
}
31,920✔
86

87
unique_ptr<Source> Source::create(pugi::xml_node node)
31,920✔
88
{
89
  // if the source type is present, use it to determine the type
90
  // of object to create
91
  if (check_for_node(node, "type")) {
31,920✔
92
    std::string source_type = get_node_value(node, "type");
31,233✔
93
    if (source_type == "independent") {
31,233✔
94
      return make_unique<IndependentSource>(node);
31,020✔
95
    } else if (source_type == "file") {
213✔
96
      return make_unique<FileSource>(node);
25✔
97
    } else if (source_type == "compiled") {
188✔
98
      return make_unique<CompiledSourceWrapper>(node);
24✔
99
    } else if (source_type == "mesh") {
164!
100
      return make_unique<MeshSource>(node);
164✔
101
    } else {
102
      fatal_error(fmt::format("Invalid source type '{}' found.", source_type));
×
103
    }
104
  } else {
31,226✔
105
    // support legacy source format
106
    if (check_for_node(node, "file")) {
687✔
107
      return make_unique<FileSource>(node);
24✔
108
    } else if (check_for_node(node, "library")) {
663!
109
      return make_unique<CompiledSourceWrapper>(node);
×
110
    } else {
111
      return make_unique<IndependentSource>(node);
663✔
112
    }
113
  }
114
}
115

116
void Source::read_constraints(pugi::xml_node node)
31,920✔
117
{
118
  // Check for constraints node. For backwards compatibility, if no constraints
119
  // node is given, still try searching for domain constraints from top-level
120
  // node.
121
  pugi::xml_node constraints_node = node.child("constraints");
31,920✔
122
  if (constraints_node) {
31,920✔
123
    node = constraints_node;
1,511✔
124
  }
125

126
  // Check for domains to reject from
127
  if (check_for_node(node, "domain_type")) {
31,920✔
128
    std::string domain_type = get_node_value(node, "domain_type");
375✔
129
    if (domain_type == "cell") {
375✔
130
      domain_type_ = DomainType::CELL;
75✔
131
    } else if (domain_type == "material") {
300✔
132
      domain_type_ = DomainType::MATERIAL;
24✔
133
    } else if (domain_type == "universe") {
276!
134
      domain_type_ = DomainType::UNIVERSE;
276✔
135
    } else {
136
      fatal_error(
×
137
        std::string("Unrecognized domain type for constraint: " + domain_type));
×
138
    }
139

140
    auto ids = get_node_array<int>(node, "domain_ids");
375✔
141
    domain_ids_.insert(ids.begin(), ids.end());
375✔
142
  }
375✔
143

144
  if (check_for_node(node, "time_bounds")) {
31,920✔
145
    auto ids = get_node_array<double>(node, "time_bounds");
9✔
146
    if (ids.size() != 2) {
9!
147
      fatal_error("Time bounds must be represented by two numbers.");
×
148
    }
149
    time_bounds_ = std::make_pair(ids[0], ids[1]);
9✔
150
  }
9✔
151
  if (check_for_node(node, "energy_bounds")) {
31,920✔
152
    auto ids = get_node_array<double>(node, "energy_bounds");
9✔
153
    if (ids.size() != 2) {
9!
154
      fatal_error("Energy bounds must be represented by two numbers.");
×
155
    }
156
    energy_bounds_ = std::make_pair(ids[0], ids[1]);
9✔
157
  }
9✔
158

159
  if (check_for_node(node, "fissionable")) {
31,920✔
160
    only_fissionable_ = get_node_value_bool(node, "fissionable");
1,127✔
161
  }
162

163
  // Check for how to handle rejected particles
164
  if (check_for_node(node, "rejection_strategy")) {
31,920!
165
    std::string rejection_strategy = get_node_value(node, "rejection_strategy");
×
166
    if (rejection_strategy == "kill") {
×
167
      rejection_strategy_ = RejectionStrategy::KILL;
×
168
    } else if (rejection_strategy == "resample") {
×
169
      rejection_strategy_ = RejectionStrategy::RESAMPLE;
×
170
    } else {
171
      fatal_error(std::string(
×
172
        "Unrecognized strategy source rejection: " + rejection_strategy));
173
    }
174
  }
×
175
}
31,920✔
176

177
void check_rejection_fraction(int64_t n_reject, int64_t n_accept)
29,684,558✔
178
{
179
  // Don't check unless we've hit a minimum number of total sites rejected
180
  if (n_reject < EXTSRC_REJECT_THRESHOLD)
29,684,558✔
181
    return;
28,619,906✔
182

183
  // Compute fraction of accepted sites and compare against minimum
184
  double fraction = static_cast<double>(n_accept) / n_reject;
1,064,652✔
185
  if (fraction <= settings::source_rejection_fraction) {
1,064,652✔
186
    fatal_error(fmt::format(
3!
187
      "Too few source sites satisfied the constraints (minimum source "
188
      "rejection fraction = {}). Please check your source definition or "
189
      "set a lower value of Settings.source_rejection_fraction.",
190
      settings::source_rejection_fraction));
191
  }
192
}
193

194
SourceSite Source::sample_with_constraints(uint64_t* seed) const
27,960,158✔
195
{
196
  bool accepted = false;
27,960,158✔
197
  int64_t n_local_reject = 0;
27,960,158✔
198
  SourceSite site {};
27,960,158✔
199

200
  while (!accepted) {
57,008,758✔
201
    // Sample a source site without considering constraints yet
202
    site = this->sample(seed);
29,048,603✔
203

204
    if (constraints_applied()) {
29,048,600✔
205
      accepted = true;
27,410,865✔
206
    } else {
207
      // Check whether sampled site satisfies constraints
208
      accepted = satisfies_spatial_constraints(site.r) &&
1,637,735✔
209
                 satisfies_energy_constraints(site.E) &&
2,195,899✔
210
                 satisfies_time_constraints(site.time);
558,164✔
211
      if (!accepted) {
1,637,735✔
212
        ++n_local_reject;
1,088,445✔
213

214
        // Check per-particle rejection limit
215
        if (n_local_reject >= settings::max_source_rejections_per_sample) {
1,088,445!
NEW
216
          fatal_error("Exceeded maximum number of source rejections per "
×
217
                      "sample. Please check your source definition or increase "
218
                      "Settings.max_source_rejections_per_sample.");
219
        }
220

221
        // For the "kill" strategy, accept particle but set weight to 0 so that
222
        // it is terminated immediately
223
        if (rejection_strategy_ == RejectionStrategy::KILL) {
1,088,445!
224
          accepted = true;
×
225
          site.wgt = 0.0;
×
226
        }
227
      }
228
    }
229
  }
230

231
  // Update global rejection counters only when constraints were actually
232
  // checked.  When constraints_applied() is true, no rejection is possible
233
  // so the reject counter and fraction check can be skipped entirely.
234
  if (!constraints_applied()) {
27,960,155✔
235
    if (n_local_reject > 0) {
549,290✔
236
      source_n_reject += n_local_reject;
15,636✔
237
    }
238
    check_rejection_fraction(source_n_reject, source_n_accept);
549,290✔
239
  }
240
  ++source_n_accept;
27,960,155✔
241

242
  return site;
27,960,155✔
243
}
244

245
bool Source::satisfies_energy_constraints(double E) const
27,986,637✔
246
{
247
  return E > energy_bounds_.first && E < energy_bounds_.second;
27,986,637!
248
}
249

250
bool Source::satisfies_time_constraints(double time) const
558,164✔
251
{
252
  return time > time_bounds_.first && time < time_bounds_.second;
558,164✔
253
}
254

255
bool Source::satisfies_spatial_constraints(Position r) const
31,880,985✔
256
{
257
  GeometryState geom_state;
31,880,985✔
258
  geom_state.r() = r;
31,880,985✔
259
  geom_state.u() = {0.0, 0.0, 1.0};
31,880,985✔
260

261
  // Reject particle if it's not in the geometry at all
262
  bool found = exhaustive_find_cell(geom_state);
31,880,985✔
263
  if (!found)
31,880,985✔
264
    return false;
400,923✔
265

266
  // Check the geometry state against specified domains
267
  bool accepted = true;
31,480,062✔
268
  if (!domain_ids_.empty()) {
31,480,062✔
269
    if (domain_type_ == DomainType::MATERIAL) {
1,642,473!
270
      auto mat_index = geom_state.material();
×
271
      if (mat_index == MATERIAL_VOID) {
×
272
        accepted = false;
×
273
      } else {
274
        accepted = contains(domain_ids_, model::materials[mat_index]->id());
×
275
      }
276
    } else {
277
      for (int i = 0; i < geom_state.n_coord(); i++) {
3,161,523✔
278
        auto id =
279
          (domain_type_ == DomainType::CELL)
1,642,473✔
280
            ? model::cells[geom_state.coord(i).cell()].get()->id_
1,642,473!
281
            : model::universes[geom_state.coord(i).universe()].get()->id_;
×
282
        if ((accepted = contains(domain_ids_, id)))
1,642,473✔
283
          break;
123,423✔
284
      }
285
    }
286
  }
287

288
  // Check if spatial site is in fissionable material
289
  if (accepted && only_fissionable_) {
31,480,062✔
290
    // Determine material
291
    auto mat_index = geom_state.material();
879,352✔
292
    if (mat_index == MATERIAL_VOID) {
879,352!
293
      accepted = false;
×
294
    } else {
295
      accepted = model::materials[mat_index]->fissionable();
879,352✔
296
    }
297
  }
298

299
  return accepted;
31,480,062✔
300
}
31,880,985✔
301

302
//==============================================================================
303
// IndependentSource implementation
304
//==============================================================================
305

306
IndependentSource::IndependentSource(
1,630✔
307
  UPtrSpace space, UPtrAngle angle, UPtrDist energy, UPtrDist time)
1,630✔
308
  : space_ {std::move(space)}, angle_ {std::move(angle)},
1,630✔
309
    energy_ {std::move(energy)}, time_ {std::move(time)}
3,260✔
310
{}
1,630✔
311

312
IndependentSource::IndependentSource(pugi::xml_node node) : Source(node)
31,683✔
313
{
314
  // Check for particle type
315
  if (check_for_node(node, "particle")) {
31,683✔
316
    auto temp_str = get_node_value(node, "particle", false, true);
31,020✔
317
    particle_ = ParticleType(temp_str);
31,020✔
318
    if (particle_ == ParticleType::photon() ||
61,923✔
319
        particle_ == ParticleType::electron() ||
61,923!
320
        particle_ == ParticleType::positron()) {
30,891✔
321
      settings::photon_transport = true;
129✔
322
    }
323
  }
31,020✔
324
  validate_particle_type(particle_, "IndependentSource");
31,683✔
325

326
  // Check for external source file
327
  if (check_for_node(node, "file")) {
31,683!
328

329
  } else {
330

331
    // Spatial distribution for external source
332
    if (check_for_node(node, "space")) {
31,683✔
333
      space_ = SpatialDistribution::create(node.child("space"));
5,970✔
334
    } else {
335
      // If no spatial distribution specified, make it a point source
336
      space_ = UPtrSpace {new SpatialPoint()};
25,713✔
337
    }
338

339
    // For backwards compatibility, check for only fissionable setting on box
340
    // source
341
    auto space_box = dynamic_cast<SpatialBox*>(space_.get());
31,683!
342
    if (space_box) {
31,683✔
343
      if (!only_fissionable_) {
3,206✔
344
        only_fissionable_ = space_box->only_fissionable();
2,079✔
345
      }
346
    }
347

348
    // Determine external source angular distribution
349
    if (check_for_node(node, "angle")) {
31,683✔
350
      angle_ = UnitSphereDistribution::create(node.child("angle"));
2,730✔
351
    } else {
352
      angle_ = UPtrAngle {new Isotropic()};
28,953✔
353
    }
354

355
    // Determine external source energy distribution
356
    if (check_for_node(node, "energy")) {
31,683✔
357
      pugi::xml_node node_dist = node.child("energy");
3,841✔
358
      energy_ = distribution_from_xml(node_dist);
3,841✔
359
    } else {
360
      // Default to a Watt spectrum with parameters 0.988 MeV and 2.249 MeV^-1
361
      energy_ = UPtrDist {new Watt(0.988e6, 2.249e-6)};
27,842✔
362
    }
363

364
    // Determine external source time distribution
365
    if (check_for_node(node, "time")) {
31,683✔
366
      pugi::xml_node node_dist = node.child("time");
33✔
367
      time_ = distribution_from_xml(node_dist);
33✔
368
    } else {
369
      // Default to a Constant time T=0
370
      double T[] {0.0};
31,650✔
371
      double p[] {1.0};
31,650✔
372
      time_ = UPtrDist {new Discrete {T, p, 1}};
31,650✔
373
    }
374
  }
375
}
31,683✔
376

377
SourceSite IndependentSource::sample(uint64_t* seed) const
29,135,268✔
378
{
379
  SourceSite site {};
29,135,268✔
380
  site.particle = particle_;
29,135,268✔
381
  double r_wgt = 1.0;
29,135,268✔
382
  double E_wgt = 1.0;
29,135,268✔
383

384
  // Repeat sampling source location until a good site has been accepted
385
  bool accepted = false;
29,135,268✔
386
  int64_t n_local_reject = 0;
29,135,268✔
387

388
  while (!accepted) {
59,378,518✔
389

390
    // Sample spatial distribution
391
    auto [r, r_wgt_temp] = space_->sample(seed);
30,243,250✔
392
    site.r = r;
30,243,250✔
393
    r_wgt = r_wgt_temp;
30,243,250✔
394

395
    // Check if sampled position satisfies spatial constraints
396
    accepted = satisfies_spatial_constraints(site.r);
30,243,250✔
397

398
    // Check for rejection
399
    if (!accepted) {
30,243,250✔
400
      ++n_local_reject;
1,107,982✔
401
      if (n_local_reject >= settings::max_source_rejections_per_sample) {
1,107,982!
NEW
402
        fatal_error("Exceeded maximum number of source rejections per "
×
403
                    "sample. Please check your source definition or increase "
404
                    "Settings.max_source_rejections_per_sample.");
405
      }
406
    }
407
  }
408

409
  // Sample angle
410
  auto [u, u_wgt] = angle_->sample(seed);
29,135,268✔
411
  site.u = u;
29,135,268✔
412

413
  site.wgt = r_wgt * u_wgt;
29,135,268✔
414

415
  // Sample energy and time for neutron and photon sources
416
  if (settings::solver_type != SolverType::RANDOM_RAY) {
29,135,268✔
417
    // Check for monoenergetic source above maximum particle energy
418
    auto p = particle_.transport_index();
27,410,868✔
419
    auto energy_ptr = dynamic_cast<Discrete*>(energy_.get());
27,410,868!
420
    if (energy_ptr) {
27,410,868✔
421
      auto energies =
422
        tensor::Tensor<double>(energy_ptr->x().data(), energy_ptr->x().size());
14,692,459✔
423
      if ((energies > data::energy_max[p]).any()) {
14,692,459!
424
        fatal_error("Source energy above range of energies of at least "
×
425
                    "one cross section table");
426
      }
427
    }
14,692,459✔
428

429
    while (true) {
430
      // Sample energy spectrum
431
      auto [E, E_wgt_temp] = energy_->sample(seed);
27,410,868✔
432
      site.E = E;
27,410,868✔
433
      E_wgt = E_wgt_temp;
27,410,868✔
434

435
      // Resample if energy falls above maximum particle energy
436
      if (site.E < data::energy_max[p] &&
54,821,736!
437
          (satisfies_energy_constraints(site.E)))
27,410,868!
438
        break;
27,410,868✔
439

NEW
440
      ++n_local_reject;
×
NEW
441
      if (n_local_reject >= settings::max_source_rejections_per_sample) {
×
NEW
442
        fatal_error("Exceeded maximum number of source rejections per "
×
443
                    "sample. Please check your source definition or increase "
444
                    "Settings.max_source_rejections_per_sample.");
445
      }
UNCOV
446
    }
×
447

448
    // Sample particle creation time
449
    auto [time, time_wgt] = time_->sample(seed);
27,410,868✔
450
    site.time = time;
27,410,868✔
451

452
    site.wgt *= (E_wgt * time_wgt);
27,410,868✔
453
  }
454

455
  // Flush local rejection count and increment accept count atomically
456
  if (n_local_reject > 0) {
29,135,268✔
457
    source_n_reject += n_local_reject;
286,411✔
458
  }
459
  ++source_n_accept;
29,135,268✔
460

461
  // Periodically check overall rejection fraction
462
  check_rejection_fraction(source_n_reject, source_n_accept);
29,135,268✔
463

464
  return site;
58,270,530✔
465
}
466

467
//==============================================================================
468
// FileSource implementation
469
//==============================================================================
470

471
FileSource::FileSource(pugi::xml_node node) : Source(node)
49✔
472
{
473
  auto path = get_node_value(node, "file", false, true);
49✔
474
  load_sites_from_file(path);
49✔
475
}
42✔
476

477
FileSource::FileSource(const std::string& path)
24✔
478
{
479
  load_sites_from_file(path);
24✔
480
}
24✔
481

482
void FileSource::load_sites_from_file(const std::string& path)
73✔
483
{
484
  // If MCPL file, use the dedicated file reader
485
  if (ends_with(path, ".mcpl") || ends_with(path, ".mcpl.gz")) {
73!
486
    sites_ = mcpl_source_sites(path);
24✔
487
  } else {
488
    // Check if source file exists
489
    if (!file_exists(path)) {
49!
490
      fatal_error(fmt::format("Source file '{}' does not exist.", path));
×
491
    }
492

493
    write_message(6, "Reading source file from {}...", path);
49✔
494

495
    // Open the binary file
496
    hid_t file_id = file_open(path, 'r', true);
49✔
497

498
    // Check to make sure this is a source file
499
    std::string filetype;
49✔
500
    read_attribute(file_id, "filetype", filetype);
49✔
501
    if (filetype != "source" && filetype != "statepoint") {
49!
502
      fatal_error("Specified starting source file not a source file type.");
×
503
    }
504

505
    // Read in the source particles
506
    read_source_bank(file_id, sites_, false);
49✔
507

508
    // Close file
509
    file_close(file_id);
42✔
510
  }
42✔
511

512
  // Make sure particles in source file have valid types
513
  for (const auto& site : this->sites_) {
138,075✔
514
    validate_particle_type(site.particle, "FileSource");
138,009✔
515
  }
516
}
66✔
517

518
SourceSite FileSource::sample(uint64_t* seed) const
233,749✔
519
{
520
  // Sample a particle randomly from list
521
  size_t i_site = sites_.size() * prn(seed);
233,749✔
522
  return sites_[i_site];
233,749✔
523
}
524

525
//==============================================================================
526
// CompiledSourceWrapper implementation
527
//==============================================================================
528

529
CompiledSourceWrapper::CompiledSourceWrapper(pugi::xml_node node) : Source(node)
24✔
530
{
531
  // Get shared library path and parameters
532
  auto path = get_node_value(node, "library", false, true);
24✔
533
  std::string parameters;
24✔
534
  if (check_for_node(node, "parameters")) {
24✔
535
    parameters = get_node_value(node, "parameters", false, true);
12✔
536
  }
537
  setup(path, parameters);
24✔
538
}
24✔
539

540
void CompiledSourceWrapper::setup(
24✔
541
  const std::string& path, const std::string& parameters)
542
{
543
#ifdef HAS_DYNAMIC_LINKING
544
  // Open the library
545
  shared_library_ = dlopen(path.c_str(), RTLD_LAZY);
24✔
546
  if (!shared_library_) {
24!
547
    fatal_error("Couldn't open source library " + path);
×
548
  }
549

550
  // reset errors
551
  dlerror();
24✔
552

553
  // get the function to create the custom source from the library
554
  auto create_compiled_source = reinterpret_cast<create_compiled_source_t*>(
555
    dlsym(shared_library_, "openmc_create_source"));
24✔
556

557
  // check for any dlsym errors
558
  auto dlsym_error = dlerror();
24✔
559
  if (dlsym_error) {
24!
560
    std::string error_msg = fmt::format(
561
      "Couldn't open the openmc_create_source symbol: {}", dlsym_error);
×
562
    dlclose(shared_library_);
×
563
    fatal_error(error_msg);
×
564
  }
×
565

566
  // create a pointer to an instance of the custom source
567
  compiled_source_ = create_compiled_source(parameters);
24✔
568

569
#else
570
  fatal_error("Custom source libraries have not yet been implemented for "
571
              "non-POSIX systems");
572
#endif
573
}
24✔
574

575
CompiledSourceWrapper::~CompiledSourceWrapper()
48✔
576
{
577
  // Make sure custom source is cleared before closing shared library
578
  if (compiled_source_.get())
24!
579
    compiled_source_.reset();
24✔
580

581
#ifdef HAS_DYNAMIC_LINKING
582
  dlclose(shared_library_);
24✔
583
#else
584
  fatal_error("Custom source libraries have not yet been implemented for "
585
              "non-POSIX systems");
586
#endif
587
}
48✔
588

589
//==============================================================================
590
// MeshElementSpatial implementation
591
//==============================================================================
592

593
std::pair<Position, double> MeshElementSpatial::sample(uint64_t* seed) const
1,235,403✔
594
{
595
  return {model::meshes[mesh_index_]->sample_element(elem_index_, seed), 1.0};
1,235,403✔
596
}
597

598
//==============================================================================
599
// MeshSource implementation
600
//==============================================================================
601

602
MeshSource::MeshSource(pugi::xml_node node) : Source(node)
164✔
603
{
604
  int32_t mesh_id = stoi(get_node_value(node, "mesh"));
164✔
605
  int32_t mesh_idx = model::mesh_map.at(mesh_id);
164✔
606
  const auto& mesh = model::meshes[mesh_idx];
164✔
607

608
  std::vector<double> strengths;
164✔
609
  // read all source distributions and populate strengths vector for MeshSpatial
610
  // object
611
  for (auto source_node : node.children("source")) {
25,352✔
612
    auto src = Source::create(source_node);
25,188✔
613
    if (auto ptr = dynamic_cast<IndependentSource*>(src.get())) {
25,188!
614
      src.release();
25,188✔
615
      sources_.emplace_back(ptr);
25,188✔
616
    } else {
617
      fatal_error(
×
618
        "The source assigned to each element must be an IndependentSource.");
619
    }
620
    strengths.push_back(sources_.back()->strength());
25,188✔
621
  }
25,188✔
622

623
  // Set spatial distributions for each mesh element
624
  for (int elem_index = 0; elem_index < sources_.size(); ++elem_index) {
25,352✔
625
    sources_[elem_index]->set_space(
50,376✔
626
      std::make_unique<MeshElementSpatial>(mesh_idx, elem_index));
50,376✔
627
  }
628

629
  // Make sure sources use valid particle types
630
  for (const auto& src : sources_) {
25,352✔
631
    validate_particle_type(src->particle_type(), "MeshSource");
25,188✔
632
  }
633

634
  // the number of source distributions should either be one or equal to the
635
  // number of mesh elements
636
  if (sources_.size() > 1 && sources_.size() != mesh->n_bins()) {
164!
637
    fatal_error(fmt::format("Incorrect number of source distributions ({}) for "
×
638
                            "mesh source with {} elements.",
639
      sources_.size(), mesh->n_bins()));
×
640
  }
641

642
  space_ = std::make_unique<MeshSpatial>(mesh_idx, strengths);
164✔
643
}
164✔
644

645
SourceSite MeshSource::sample(uint64_t* seed) const
1,223,986✔
646
{
647
  // Sample a mesh element based on the relative strengths
648
  int32_t element = space_->sample_element_index(seed);
1,223,986✔
649

650
  // Sample the distribution for the specific mesh element; note that the
651
  // spatial distribution has been set for each element using MeshElementSpatial
652
  return source(element)->sample_with_constraints(seed);
1,223,986✔
653
}
654

655
//==============================================================================
656
// Non-member functions
657
//==============================================================================
658

659
void initialize_source()
3,070✔
660
{
661
  write_message("Initializing source particles...", 5);
3,070✔
662

663
// Generation source sites from specified distribution in user input
664
#pragma omp parallel for
665
  for (int64_t i = 0; i < simulation::work_per_rank; ++i) {
960,179✔
666
    // initialize random number seed
667
    int64_t id = simulation::total_gen * settings::n_particles +
1,917,704✔
668
                 simulation::work_index[mpi::rank] + i + 1;
958,852✔
669
    uint64_t seed = init_seed(id, STREAM_SOURCE);
958,852✔
670

671
    // sample external source distribution
672
    simulation::source_bank[i] = sample_external_source(&seed);
958,852✔
673
  }
674

675
  // Write out initial source
676
  if (settings::write_initial_source) {
3,070!
677
    write_message("Writing out initial source...", 5);
×
678
    std::string filename = settings::path_output + "initial_source.h5";
×
679
    hid_t file_id = file_open(filename, 'w', true);
×
680
    write_source_bank(file_id, simulation::source_bank, simulation::work_index);
×
681
    file_close(file_id);
×
682
  }
×
683
}
3,070✔
684

685
SourceSite sample_external_source(uint64_t* seed)
26,736,172✔
686
{
687
  // Sample from among multiple source distributions
688
  int i = 0;
26,736,172✔
689
  int n_sources = model::external_sources.size();
26,736,172✔
690
  if (n_sources > 1) {
26,736,172✔
691
    if (settings::uniform_source_sampling) {
2,909,700✔
692
      i = prn(seed) * n_sources;
1,800✔
693
    } else {
694
      i = model::external_sources_probability.sample(seed);
2,907,900✔
695
    }
696
  }
697

698
  // Sample source site from i-th source distribution
699
  SourceSite site {model::external_sources[i]->sample_with_constraints(seed)};
26,736,172✔
700

701
  // For uniform source sampling, multiply the weight by the ratio of the actual
702
  // probability of sampling source i to the biased probability of sampling
703
  // source i, which is (strength_i / total_strength) / (1 / n)
704
  if (n_sources > 1 && settings::uniform_source_sampling) {
26,736,169✔
705
    double total_strength = model::external_sources_probability.integral();
1,800✔
706
    site.wgt *=
1,800✔
707
      model::external_sources[i]->strength() * n_sources / total_strength;
1,800✔
708
  }
709

710
  // If running in MG, convert site.E to group
711
  if (!settings::run_CE) {
26,736,169✔
712
    site.E = lower_bound_index(data::mg.rev_energy_bins_.begin(),
1,425,600✔
713
      data::mg.rev_energy_bins_.end(), site.E);
714
    site.E = data::mg.num_energy_groups_ - site.E - 1.;
1,425,600✔
715
  }
716

717
  return site;
26,736,169✔
718
}
719

720
void free_memory_source()
6,564✔
721
{
722
  model::external_sources.clear();
6,564✔
723
  reset_source_rejection_counters();
6,564✔
724
}
6,564✔
725

726
void reset_source_rejection_counters()
12,259✔
727
{
728
  source_n_accept = 0;
12,259✔
729
  source_n_reject = 0;
12,259✔
730
}
12,259✔
731

732
//==============================================================================
733
// C API
734
//==============================================================================
735

736
extern "C" int openmc_sample_external_source(
708✔
737
  size_t n, uint64_t* seed, void* sites, int threads)
738
{
739
  if (!sites || !seed) {
708!
740
    set_errmsg("Received null pointer.");
×
741
    return OPENMC_E_INVALID_ARGUMENT;
×
742
  }
743

744
  if (model::external_sources.empty()) {
708!
745
    set_errmsg("No external sources have been defined.");
×
746
    return OPENMC_E_OUT_OF_BOUNDS;
×
747
  }
748

749
  auto sites_array = static_cast<SourceSite*>(sites);
708✔
750

751
  // Derive independent per-particle seeds from the base seed so that
752
  // each iteration has its own RNG state for thread-safe parallel sampling.
753
  uint64_t base_seed = *seed;
708✔
754

755
#pragma omp parallel for schedule(static) num_threads(threads)
572✔
756
  for (size_t i = 0; i < n; ++i) {
857,232✔
757
    uint64_t particle_seed = init_seed(base_seed + i, STREAM_SOURCE);
857,096✔
758
    sites_array[i] = sample_external_source(&particle_seed);
857,096✔
759
  }
760
  return 0;
708✔
761
}
762

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