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

openmc-dev / openmc / 23148760986

16 Mar 2026 02:27PM UTC coverage: 81.202% (-0.2%) from 81.435%
23148760986

Pull #3757

github

web-flow
Merge 6206e3966 into 157869812
Pull Request #3757: Testing point detectors

17624 of 25493 branches covered (69.13%)

Branch coverage included in aggregate %.

44 of 227 new or added lines in 14 files covered. (19.38%)

1 existing line in 1 file now uncovered.

58081 of 67737 relevant lines covered (85.74%)

44465291.14 hits per line

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

80.76
/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/tallies/tally_scoring.h"
37
#include "openmc/xml_interface.h"
38

39
namespace openmc {
40

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

44
namespace {
45

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

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

56
} // namespace
57

58
//==============================================================================
59
// Global variables
60
//==============================================================================
61

62
namespace model {
63

64
vector<unique_ptr<Source>> external_sources;
65

66
DiscreteIndex external_sources_probability;
67

68
} // namespace model
69

70
//==============================================================================
71
// Source implementation
72
//==============================================================================
73

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

84
  // Check for additional defined constraints
85
  read_constraints(node);
45,915✔
86
}
45,915✔
87

88
unique_ptr<Source> Source::create(pugi::xml_node node)
45,915✔
89
{
90
  // if the source type is present, use it to determine the type
91
  // of object to create
92
  if (check_for_node(node, "type")) {
45,915✔
93
    std::string source_type = get_node_value(node, "type");
45,060✔
94
    if (source_type == "independent") {
45,060✔
95
      return make_unique<IndependentSource>(node);
44,798✔
96
    } else if (source_type == "file") {
262✔
97
      return make_unique<FileSource>(node);
31✔
98
    } else if (source_type == "compiled") {
231✔
99
      return make_unique<CompiledSourceWrapper>(node);
30✔
100
    } else if (source_type == "mesh") {
201!
101
      return make_unique<MeshSource>(node);
201✔
102
    } else {
103
      fatal_error(fmt::format("Invalid source type '{}' found.", source_type));
×
104
    }
105
  } else {
45,050✔
106
    // support legacy source format
107
    if (check_for_node(node, "file")) {
855✔
108
      return make_unique<FileSource>(node);
30✔
109
    } else if (check_for_node(node, "library")) {
825!
110
      return make_unique<CompiledSourceWrapper>(node);
×
111
    } else {
112
      return make_unique<IndependentSource>(node);
825✔
113
    }
114
  }
115
}
116

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

127
  // Check for domains to reject from
128
  if (check_for_node(node, "domain_type")) {
45,915✔
129
    std::string domain_type = get_node_value(node, "domain_type");
467✔
130
    if (domain_type == "cell") {
467✔
131
      domain_type_ = DomainType::CELL;
92✔
132
    } else if (domain_type == "material") {
375✔
133
      domain_type_ = DomainType::MATERIAL;
30✔
134
    } else if (domain_type == "universe") {
345!
135
      domain_type_ = DomainType::UNIVERSE;
345✔
136
    } else {
137
      fatal_error(
×
138
        std::string("Unrecognized domain type for constraint: " + domain_type));
×
139
    }
140

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

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

160
  if (check_for_node(node, "fissionable")) {
45,915✔
161
    only_fissionable_ = get_node_value_bool(node, "fissionable");
1,430✔
162
  }
163

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

178
void check_rejection_fraction(int64_t n_reject, int64_t n_accept)
34,479,334✔
179
{
180
  // Don't check unless we've hit a minimum number of total sites rejected
181
  if (n_reject < EXTSRC_REJECT_THRESHOLD)
34,479,334✔
182
    return;
183

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

195
SourceSite Source::sample_with_constraints(uint64_t* seed) const
34,479,334✔
196
{
197
  bool accepted = false;
34,479,334✔
198
  int64_t n_local_reject = 0;
34,479,334✔
199
  SourceSite site {};
34,479,334✔
200

201
  while (!accepted) {
104,758,306✔
202
    // Sample a source site without considering constraints yet
203
    site = this->sample(seed);
35,799,638✔
204

205
    if (constraints_applied()) {
35,799,638✔
206
      accepted = true;
207
    } else {
208
      // Check whether sampled site satisfies constraints
209
      accepted = satisfies_spatial_constraints(site.r) &&
39,166,770✔
210
                 satisfies_energy_constraints(site.E) &&
2,673,887✔
211
                 satisfies_time_constraints(site.time);
682,223✔
212
      if (!accepted) {
1,320,304✔
213
        ++n_local_reject;
1,320,304✔
214

215
        // Check per-particle rejection limit
216
        if (n_local_reject >= MAX_SOURCE_REJECTIONS_PER_SAMPLE) {
1,320,304!
217
          fatal_error("Exceeded maximum number of source rejections per "
×
218
                      "sample. Please check your source definition.");
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,320,304!
224
          accepted = true;
×
225
          site.wgt = 0.0;
×
226
        }
227
      }
228
    }
229
  }
230

231
  // Flush local rejection count, update accept counter, and check overall
232
  // rejection fraction
233
  if (n_local_reject > 0) {
34,479,334✔
234
    source_n_reject += n_local_reject;
19,137✔
235
  }
236
  ++source_n_accept;
34,479,334✔
237
  check_rejection_fraction(source_n_reject, source_n_accept);
34,479,334✔
238

239
  return site;
34,479,330✔
240
}
241

242
bool Source::satisfies_energy_constraints(double E) const
34,512,082✔
243
{
244
  return E > energy_bounds_.first && E < energy_bounds_.second;
34,512,082!
245
}
246

247
bool Source::satisfies_time_constraints(double time) const
682,223✔
248
{
249
  return time > time_bounds_.first && time < time_bounds_.second;
682,223✔
250
}
251

252
bool Source::satisfies_spatial_constraints(Position r) const
39,245,751✔
253
{
254
  GeometryState geom_state;
39,245,751✔
255
  geom_state.r() = r;
39,245,751✔
256
  geom_state.u() = {0.0, 0.0, 1.0};
39,245,751✔
257

258
  // Reject particle if it's not in the geometry at all
259
  bool found = exhaustive_find_cell(geom_state);
39,245,751✔
260
  if (!found)
39,245,751✔
261
    return false;
262

263
  // Check the geometry state against specified domains
264
  bool accepted = true;
38,755,411✔
265
  if (!domain_ids_.empty()) {
38,755,411✔
266
    if (domain_type_ == DomainType::MATERIAL) {
1,979,737!
267
      auto mat_index = geom_state.material();
×
268
      if (mat_index == MATERIAL_VOID) {
×
269
        accepted = false;
270
      } else {
271
        accepted = contains(domain_ids_, model::materials[mat_index]->id());
×
272
      }
273
    } else {
274
      for (int i = 0; i < geom_state.n_coord(); i++) {
3,808,910✔
275
        auto id =
1,979,737✔
276
          (domain_type_ == DomainType::CELL)
277
            ? model::cells[geom_state.coord(i).cell()].get()->id_
1,979,737!
278
            : model::universes[geom_state.coord(i).universe()].get()->id_;
×
279
        if ((accepted = contains(domain_ids_, id)))
3,959,474✔
280
          break;
281
      }
282
    }
283
  }
284

285
  // Check if spatial site is in fissionable material
286
  if (accepted && only_fissionable_) {
38,755,411✔
287
    // Determine material
288
    auto mat_index = geom_state.material();
1,076,309✔
289
    if (mat_index == MATERIAL_VOID) {
1,076,309!
290
      accepted = false;
291
    } else {
292
      accepted = model::materials[mat_index]->fissionable();
1,076,309✔
293
    }
294
  }
295

296
  return accepted;
297
}
39,245,751✔
298

299
//==============================================================================
300
// IndependentSource implementation
301
//==============================================================================
302

303
IndependentSource::IndependentSource(
2,059✔
304
  UPtrSpace space, UPtrAngle angle, UPtrDist energy, UPtrDist time)
2,059✔
305
  : space_ {std::move(space)}, angle_ {std::move(angle)},
2,059✔
306
    energy_ {std::move(energy)}, time_ {std::move(time)}
2,059✔
307
{}
2,059✔
308

309
IndependentSource::IndependentSource(pugi::xml_node node) : Source(node)
45,623✔
310
{
311
  // Check for particle type
312
  if (check_for_node(node, "particle")) {
45,623✔
313
    auto temp_str = get_node_value(node, "particle", false, true);
44,798✔
314
    particle_ = ParticleType(temp_str);
44,798✔
315
    if (particle_ == ParticleType::photon() ||
44,798✔
316
        particle_ == ParticleType::electron() ||
44,798✔
317
        particle_ == ParticleType::positron()) {
44,624!
318
      settings::photon_transport = true;
174✔
319
    }
320
  }
44,798✔
321
  validate_particle_type(particle_, "IndependentSource");
45,623✔
322

323
  // Check for external source file
324
  if (check_for_node(node, "file")) {
45,623!
325

326
  } else {
327

328
    // Spatial distribution for external source
329
    if (check_for_node(node, "space")) {
45,623✔
330
      space_ = SpatialDistribution::create(node.child("space"));
7,505✔
331
    } else {
332
      // If no spatial distribution specified, make it a point source
333
      space_ = UPtrSpace {new SpatialPoint()};
38,118✔
334
    }
335

336
    // For backwards compatibility, check for only fissionable setting on box
337
    // source
338
    auto space_box = dynamic_cast<SpatialBox*>(space_.get());
45,622!
339
    if (space_box) {
45,622✔
340
      if (!only_fissionable_) {
4,033✔
341
        only_fissionable_ = space_box->only_fissionable();
2,603✔
342
      }
343
    }
344

345
    // Determine external source angular distribution
346
    if (check_for_node(node, "angle")) {
45,622✔
347
      angle_ = UnitSphereDistribution::create(node.child("angle"));
3,402✔
348
    } else {
349
      angle_ = UPtrAngle {new Isotropic()};
42,220✔
350
    }
351

352
    // Determine external source energy distribution
353
    if (check_for_node(node, "energy")) {
45,622✔
354
      pugi::xml_node node_dist = node.child("energy");
4,763✔
355
      energy_ = distribution_from_xml(node_dist);
4,763✔
356
    } else {
357
      // Default to a Watt spectrum with parameters 0.988 MeV and 2.249 MeV^-1
358
      energy_ = UPtrDist {new Watt(0.988e6, 2.249e-6)};
40,859✔
359
    }
360

361
    // Determine external source time distribution
362
    if (check_for_node(node, "time")) {
45,622✔
363
      pugi::xml_node node_dist = node.child("time");
41✔
364
      time_ = distribution_from_xml(node_dist);
41✔
365
    } else {
366
      // Default to a Constant time T=0
367
      double T[] {0.0};
45,581✔
368
      double p[] {1.0};
45,581✔
369
      time_ = UPtrDist {new Discrete {T, p, 1}};
45,581✔
370
    }
371
  }
372
}
45,622✔
373

374
SourceSite IndependentSource::sample(uint64_t* seed) const
35,916,294✔
375
{
376
  SourceSite site {};
35,916,294✔
377
  site.particle = particle_;
35,916,294✔
378
  double r_wgt = 1.0;
35,916,294✔
379
  double E_wgt = 1.0;
35,916,294✔
380

381
  // Repeat sampling source location until a good site has been accepted
382
  bool accepted = false;
35,916,294✔
383
  int64_t n_local_reject = 0;
35,916,294✔
384

385
  while (!accepted) {
73,170,381✔
386

387
    // Sample spatial distribution
388
    auto [r, r_wgt_temp] = space_->sample(seed);
37,254,087✔
389
    site.r = r;
37,254,087✔
390
    r_wgt = r_wgt_temp;
37,254,087✔
391

392
    // Check if sampled position satisfies spatial constraints
393
    accepted = satisfies_spatial_constraints(site.r);
37,254,087✔
394

395
    // Check for rejection
396
    if (!accepted) {
37,254,087✔
397
      ++n_local_reject;
1,337,793✔
398
      if (n_local_reject >= MAX_SOURCE_REJECTIONS_PER_SAMPLE) {
1,337,793!
399
        fatal_error("Exceeded maximum number of source rejections per "
×
400
                    "sample. Please check your source definition.");
401
      }
402
    }
403
  }
404

405
  // Sample angle
406
  auto [u, u_wgt] = angle_->sample(seed);
35,916,294✔
407
  site.u = u;
35,916,294✔
408

409
  site.wgt = r_wgt * u_wgt;
35,916,294✔
410

411
  // Sample energy and time for neutron and photon sources
412
  if (settings::solver_type != SolverType::RANDOM_RAY) {
35,916,294✔
413
    // Check for monoenergetic source above maximum particle energy
414
    auto p = particle_.transport_index();
33,807,974✔
415
    auto energy_ptr = dynamic_cast<Discrete*>(energy_.get());
33,807,974!
416
    if (energy_ptr) {
33,807,974✔
417
      auto energies =
18,244,881✔
418
        tensor::Tensor<double>(energy_ptr->x().data(), energy_ptr->x().size());
18,244,881✔
419
      if ((energies > data::energy_max[p]).any()) {
54,734,643!
420
        fatal_error("Source energy above range of energies of at least "
×
421
                    "one cross section table");
422
      }
423
    }
18,244,881✔
424

425
    while (true) {
33,807,974✔
426
      // Sample energy spectrum
427
      auto [E, E_wgt_temp] = energy_->sample(seed);
33,807,974✔
428
      site.E = E;
33,807,974✔
429
      E_wgt = E_wgt_temp;
33,807,974✔
430

431
      // Resample if energy falls above maximum particle energy
432
      if (site.E < data::energy_max[p] &&
67,615,948!
433
          (satisfies_energy_constraints(site.E)))
33,807,974✔
434
        break;
435

436
      ++n_local_reject;
×
437
      if (n_local_reject >= MAX_SOURCE_REJECTIONS_PER_SAMPLE) {
×
438
        fatal_error("Exceeded maximum number of source rejections per "
×
439
                    "sample. Please check your source definition.");
440
      }
441
    }
×
442

443
    // Sample particle creation time
444
    auto [time, time_wgt] = time_->sample(seed);
33,807,974✔
445
    site.time = time;
33,807,974✔
446

447
    site.wgt *= (E_wgt * time_wgt);
33,807,974✔
448
  }
449

450
  // Flush local rejection count into global counter
451
  if (n_local_reject > 0) {
35,916,294✔
452
    source_n_reject += n_local_reject;
350,248✔
453
  }
454

455
  return site;
35,916,294✔
456
}
457

458
//==============================================================================
459
// FileSource implementation
460
//==============================================================================
461

462
FileSource::FileSource(pugi::xml_node node) : Source(node)
61✔
463
{
464
  auto path = get_node_value(node, "file", false, true);
61✔
465
  load_sites_from_file(path);
61✔
466
}
52✔
467

468
FileSource::FileSource(const std::string& path)
30✔
469
{
470
  load_sites_from_file(path);
30✔
471
}
30✔
472

473
void FileSource::load_sites_from_file(const std::string& path)
91✔
474
{
475
  // If MCPL file, use the dedicated file reader
476
  if (ends_with(path, ".mcpl") || ends_with(path, ".mcpl.gz")) {
152!
477
    sites_ = mcpl_source_sites(path);
30✔
478
  } else {
479
    // Check if source file exists
480
    if (!file_exists(path)) {
61!
481
      fatal_error(fmt::format("Source file '{}' does not exist.", path));
×
482
    }
483

484
    write_message(6, "Reading source file from {}...", path);
61✔
485

486
    // Open the binary file
487
    hid_t file_id = file_open(path, 'r', true);
61✔
488

489
    // Check to make sure this is a source file
490
    std::string filetype;
61✔
491
    read_attribute(file_id, "filetype", filetype);
61✔
492
    if (filetype != "source" && filetype != "statepoint") {
61!
493
      fatal_error("Specified starting source file not a source file type.");
×
494
    }
495

496
    // Read in the source particles
497
    read_source_bank(file_id, sites_, false);
61✔
498

499
    // Close file
500
    file_close(file_id);
52✔
501
  }
52✔
502

503
  // Make sure particles in source file have valid types
504
  for (const auto& site : this->sites_) {
170,093✔
505
    validate_particle_type(site.particle, "FileSource");
340,022✔
506
  }
507
}
82✔
508

509
SourceSite FileSource::sample(uint64_t* seed) const
286,078✔
510
{
511
  // Sample a particle randomly from list
512
  size_t i_site = sites_.size() * prn(seed);
286,078✔
513
  return sites_[i_site];
286,078✔
514
}
515

516
//==============================================================================
517
// CompiledSourceWrapper implementation
518
//==============================================================================
519

520
CompiledSourceWrapper::CompiledSourceWrapper(pugi::xml_node node) : Source(node)
30✔
521
{
522
  // Get shared library path and parameters
523
  auto path = get_node_value(node, "library", false, true);
30✔
524
  std::string parameters;
30✔
525
  if (check_for_node(node, "parameters")) {
30✔
526
    parameters = get_node_value(node, "parameters", false, true);
15✔
527
  }
528
  setup(path, parameters);
30✔
529
}
30✔
530

531
void CompiledSourceWrapper::setup(
30✔
532
  const std::string& path, const std::string& parameters)
533
{
534
#ifdef HAS_DYNAMIC_LINKING
535
  // Open the library
536
  shared_library_ = dlopen(path.c_str(), RTLD_LAZY);
30✔
537
  if (!shared_library_) {
30!
538
    fatal_error("Couldn't open source library " + path);
×
539
  }
540

541
  // reset errors
542
  dlerror();
30✔
543

544
  // get the function to create the custom source from the library
545
  auto create_compiled_source = reinterpret_cast<create_compiled_source_t*>(
30✔
546
    dlsym(shared_library_, "openmc_create_source"));
30✔
547

548
  // check for any dlsym errors
549
  auto dlsym_error = dlerror();
30✔
550
  if (dlsym_error) {
30!
551
    std::string error_msg = fmt::format(
×
552
      "Couldn't open the openmc_create_source symbol: {}", dlsym_error);
×
553
    dlclose(shared_library_);
×
554
    fatal_error(error_msg);
×
555
  }
×
556

557
  // create a pointer to an instance of the custom source
558
  compiled_source_ = create_compiled_source(parameters);
30✔
559

560
#else
561
  fatal_error("Custom source libraries have not yet been implemented for "
562
              "non-POSIX systems");
563
#endif
564
}
30✔
565

566
CompiledSourceWrapper::~CompiledSourceWrapper()
60✔
567
{
568
  // Make sure custom source is cleared before closing shared library
569
  if (compiled_source_.get())
30!
570
    compiled_source_.reset();
30✔
571

572
#ifdef HAS_DYNAMIC_LINKING
573
  dlclose(shared_library_);
30✔
574
#else
575
  fatal_error("Custom source libraries have not yet been implemented for "
576
              "non-POSIX systems");
577
#endif
578
}
60✔
579

580
//==============================================================================
581
// MeshElementSpatial implementation
582
//==============================================================================
583

584
std::pair<Position, double> MeshElementSpatial::sample(uint64_t* seed) const
1,500,983✔
585
{
586
  return {model::meshes[mesh_index_]->sample_element(elem_index_, seed), 1.0};
1,500,983✔
587
}
588

589
//==============================================================================
590
// MeshSource implementation
591
//==============================================================================
592

593
MeshSource::MeshSource(pugi::xml_node node) : Source(node)
201✔
594
{
595
  int32_t mesh_id = stoi(get_node_value(node, "mesh"));
402✔
596
  int32_t mesh_idx = model::mesh_map.at(mesh_id);
201✔
597
  const auto& mesh = model::meshes[mesh_idx];
201✔
598

599
  std::vector<double> strengths;
201✔
600
  // read all source distributions and populate strengths vector for MeshSpatial
601
  // object
602
  for (auto source_node : node.children("source")) {
37,653✔
603
    auto src = Source::create(source_node);
37,452✔
604
    if (auto ptr = dynamic_cast<IndependentSource*>(src.get())) {
37,452!
605
      src.release();
37,452✔
606
      sources_.emplace_back(ptr);
37,452✔
607
    } else {
608
      fatal_error(
×
609
        "The source assigned to each element must be an IndependentSource.");
610
    }
611
    strengths.push_back(sources_.back()->strength());
37,452✔
612
  }
37,452✔
613

614
  // Set spatial distributions for each mesh element
615
  for (int elem_index = 0; elem_index < sources_.size(); ++elem_index) {
37,653✔
616
    sources_[elem_index]->set_space(
37,452✔
617
      std::make_unique<MeshElementSpatial>(mesh_idx, elem_index));
74,904✔
618
  }
619

620
  // Make sure sources use valid particle types
621
  for (const auto& src : sources_) {
37,653✔
622
    validate_particle_type(src->particle_type(), "MeshSource");
74,904✔
623
  }
624

625
  // the number of source distributions should either be one or equal to the
626
  // number of mesh elements
627
  if (sources_.size() > 1 && sources_.size() != mesh->n_bins()) {
201!
628
    fatal_error(fmt::format("Incorrect number of source distributions ({}) for "
×
629
                            "mesh source with {} elements.",
630
      sources_.size(), mesh->n_bins()));
×
631
  }
632

633
  space_ = std::make_unique<MeshSpatial>(mesh_idx, strengths);
201✔
634
}
201✔
635

636
SourceSite MeshSource::sample(uint64_t* seed) const
1,485,586✔
637
{
638
  // Sample a mesh element based on the relative strengths
639
  int32_t element = space_->sample_element_index(seed);
1,485,586✔
640

641
  // Sample the distribution for the specific mesh element; note that the
642
  // spatial distribution has been set for each element using MeshElementSpatial
643
  return source(element)->sample_with_constraints(seed);
2,971,172!
644
}
645

646
//==============================================================================
647
// Non-member functions
648
//==============================================================================
649

650
void initialize_source()
3,851✔
651
{
652
  write_message("Initializing source particles...", 5);
3,851✔
653

654
// Generation source sites from specified distribution in user input
655
#pragma omp parallel for
2,216✔
656
  for (int64_t i = 0; i < simulation::work_per_rank; ++i) {
1,210,300✔
657
    // initialize random number seed
658
    int64_t id = simulation::total_gen * settings::n_particles +
1,208,665✔
659
                 simulation::work_index[mpi::rank] + i + 1;
1,208,665✔
660
    uint64_t seed = init_seed(id, STREAM_SOURCE);
1,208,665✔
661

662
    // sample external source distribution
663
    simulation::source_bank[i] = sample_external_source(&seed);
1,208,665✔
664
  }
665

666
  // Write out initial source
667
  if (settings::write_initial_source) {
3,851!
668
    write_message("Writing out initial source...", 5);
×
669
    std::string filename = settings::path_output + "initial_source.h5";
×
670
    hid_t file_id = file_open(filename, 'w', true);
×
671
    write_source_bank(file_id, simulation::source_bank, simulation::work_index);
×
672
    file_close(file_id);
×
673
  }
×
674
}
3,851✔
675

676
SourceSite sample_external_source(uint64_t* seed)
32,993,748✔
677
{
678
  // Sample from among multiple source distributions
679
  int i = 0;
32,993,748✔
680
  int n_sources = model::external_sources.size();
32,993,748✔
681
  if (n_sources > 1) {
32,993,748✔
682
    if (settings::uniform_source_sampling) {
3,558,300✔
683
      i = prn(seed) * n_sources;
2,200✔
684
    } else {
685
      i = model::external_sources_probability.sample(seed);
3,556,100✔
686
    }
687
  }
688

689
  // Sample source site from i-th source distribution
690
  SourceSite site {model::external_sources[i]->sample_with_constraints(seed)};
32,993,748✔
691

692
  // For uniform source sampling, multiply the weight by the ratio of the actual
693
  // probability of sampling source i to the biased probability of sampling
694
  // source i, which is (strength_i / total_strength) / (1 / n)
695
  if (n_sources > 1 && settings::uniform_source_sampling) {
32,993,744✔
696
    double total_strength = model::external_sources_probability.integral();
2,200✔
697
    site.wgt *=
4,400✔
698
      model::external_sources[i]->strength() * n_sources / total_strength;
2,200✔
699
  }
700

701
  // If running in MG, convert site.E to group
702
  if (!settings::run_CE) {
32,993,744✔
703
    site.E = lower_bound_index(data::mg.rev_energy_bins_.begin(),
1,742,400✔
704
      data::mg.rev_energy_bins_.end(), site.E);
705
    site.E = data::mg.num_energy_groups_ - site.E - 1.;
1,742,400✔
706
  }
707

708
  if (!model::active_point_tallies.empty()) {
32,993,744!
NEW
709
    score_point_tally(site, i);
×
710
  }
711

712
  return site;
32,993,744✔
713
}
714

715
void free_memory_source()
8,309✔
716
{
717
  model::external_sources.clear();
8,309✔
718
  reset_source_rejection_counters();
8,309✔
719
}
8,309✔
720

721
void reset_source_rejection_counters()
15,495✔
722
{
723
  source_n_accept = 0;
15,495✔
724
  source_n_reject = 0;
15,495✔
725
}
15,495✔
726

727
//==============================================================================
728
// C API
729
//==============================================================================
730

731
extern "C" int openmc_sample_external_source(
966✔
732
  size_t n, uint64_t* seed, void* sites)
733
{
734
  if (!sites || !seed) {
966!
735
    set_errmsg("Received null pointer.");
×
736
    return OPENMC_E_INVALID_ARGUMENT;
×
737
  }
738

739
  if (model::external_sources.empty()) {
966!
740
    set_errmsg("No external sources have been defined.");
×
741
    return OPENMC_E_OUT_OF_BOUNDS;
×
742
  }
743

744
  auto sites_array = static_cast<SourceSite*>(sites);
966✔
745

746
  // Derive independent per-particle seeds from the base seed so that
747
  // each iteration has its own RNG state for thread-safe parallel sampling.
748
  uint64_t base_seed = *seed;
966✔
749

750
#pragma omp parallel for schedule(static)
801✔
751
  for (size_t i = 0; i < n; ++i) {
1,071,485✔
752
    uint64_t particle_seed = init_seed(base_seed + i, STREAM_SOURCE);
1,071,320✔
753
    sites_array[i] = sample_external_source(&particle_seed);
1,071,320✔
754
  }
755
  return 0;
801✔
756
}
757

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