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

openmc-dev / openmc / 23285088002

19 Mar 2026 07:51AM UTC coverage: 80.71% (-0.7%) from 81.447%
23285088002

Pull #3886

github

web-flow
Merge 9cdb2d588 into 3ce6cbfdd
Pull Request #3886: Implement python tally types

16322 of 23494 branches covered (69.47%)

Branch coverage included in aggregate %.

215 of 264 new or added lines in 11 files covered. (81.44%)

713 existing lines in 49 files now uncovered.

56449 of 66670 relevant lines covered (84.67%)

14035992.85 hits per line

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

80.86
/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)
29,832✔
46
{
47
  if (type.is_transportable())
29,832!
48
    return;
29,832✔
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)
1,616✔
74
{
75
  // Check for source strength
76
  if (check_for_node(node, "strength")) {
1,616✔
77
    strength_ = std::stod(get_node_value(node, "strength"));
3,060✔
78
    if (strength_ < 0.0) {
1,530!
79
      fatal_error("Source strength is negative.");
×
80
    }
81
  }
82

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

87
unique_ptr<Source> Source::create(pugi::xml_node node)
1,616✔
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")) {
1,616✔
92
    std::string source_type = get_node_value(node, "type");
1,494✔
93
    if (source_type == "independent") {
1,494✔
94
      return make_unique<IndependentSource>(node);
1,448✔
95
    } else if (source_type == "file") {
46✔
96
      return make_unique<FileSource>(node);
6✔
97
    } else if (source_type == "compiled") {
40✔
98
      return make_unique<CompiledSourceWrapper>(node);
4✔
99
    } else if (source_type == "mesh") {
36!
100
      return make_unique<MeshSource>(node);
36✔
101
    } else {
102
      fatal_error(fmt::format("Invalid source type '{}' found.", source_type));
×
103
    }
104
  } else {
1,492✔
105
    // support legacy source format
106
    if (check_for_node(node, "file")) {
122✔
107
      return make_unique<FileSource>(node);
4✔
108
    } else if (check_for_node(node, "library")) {
118!
109
      return make_unique<CompiledSourceWrapper>(node);
×
110
    } else {
111
      return make_unique<IndependentSource>(node);
118✔
112
    }
113
  }
114
}
115

116
void Source::read_constraints(pugi::xml_node node)
1,616✔
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");
1,616✔
122
  if (constraints_node) {
1,616✔
123
    node = constraints_node;
298✔
124
  }
125

126
  // Check for domains to reject from
127
  if (check_for_node(node, "domain_type")) {
1,616✔
128
    std::string domain_type = get_node_value(node, "domain_type");
68✔
129
    if (domain_type == "cell") {
68✔
130
      domain_type_ = DomainType::CELL;
18✔
131
    } else if (domain_type == "material") {
50✔
132
      domain_type_ = DomainType::MATERIAL;
4✔
133
    } else if (domain_type == "universe") {
46!
134
      domain_type_ = DomainType::UNIVERSE;
46✔
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");
68✔
141
    domain_ids_.insert(ids.begin(), ids.end());
68✔
142
  }
68✔
143

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

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

163
  // Check for how to handle rejected particles
164
  if (check_for_node(node, "rejection_strategy")) {
1,616!
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
    }
UNCOV
174
  }
×
175
}
1,616✔
176

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

183
  // Compute fraction of accepted sites and compare against minimum
184
  double fraction = static_cast<double>(n_accept) / n_reject;
246,382✔
185
  if (fraction <= settings::source_rejection_fraction) {
246,382✔
186
    fatal_error(fmt::format(
2✔
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
6,135,009✔
195
{
196
  bool accepted = false;
6,135,009✔
197
  int64_t n_local_reject = 0;
6,135,009✔
198
  SourceSite site {};
6,135,009✔
199

200
  while (!accepted) {
18,653,392✔
201
    // Sample a source site without considering constraints yet
202
    site = this->sample(seed);
6,383,374✔
203

204
    if (constraints_applied()) {
6,383,374✔
205
      accepted = true;
206
    } else {
207
      // Check whether sampled site satisfies constraints
208
      accepted = satisfies_spatial_constraints(site.r) &&
7,004,117✔
209
                 satisfies_energy_constraints(site.E) &&
494,556✔
210
                 satisfies_time_constraints(site.time);
124,131✔
211
      if (!accepted) {
248,365✔
212
        ++n_local_reject;
248,365✔
213

214
        // Check per-particle rejection limit
215
        if (n_local_reject >= MAX_SOURCE_REJECTIONS_PER_SAMPLE) {
248,365!
216
          fatal_error("Exceeded maximum number of source rejections per "
×
217
                      "sample. Please check your source definition.");
218
        }
219

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

230
  // Flush local rejection count, update accept counter, and check overall
231
  // rejection fraction
232
  if (n_local_reject > 0) {
6,135,009✔
233
    source_n_reject += n_local_reject;
3,490✔
234
  }
235
  ++source_n_accept;
6,135,009✔
236
  check_rejection_fraction(source_n_reject, source_n_accept);
6,135,009✔
237

238
  return site;
6,135,007✔
239
}
240

241
bool Source::satisfies_energy_constraints(double E) const
6,141,207✔
242
{
243
  return E > energy_bounds_.first && E < energy_bounds_.second;
6,141,207!
244
}
245

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

251
bool Source::satisfies_spatial_constraints(Position r) const
7,068,466✔
252
{
253
  GeometryState geom_state;
7,068,466✔
254
  geom_state.r() = r;
7,068,466✔
255
  geom_state.u() = {0.0, 0.0, 1.0};
7,068,466✔
256

257
  // Reject particle if it's not in the geometry at all
258
  bool found = exhaustive_find_cell(geom_state);
7,068,466✔
259
  if (!found)
7,068,466✔
260
    return false;
261

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

284
  // Check if spatial site is in fissionable material
285
  if (accepted && only_fissionable_) {
6,979,372✔
286
    // Determine material
287
    auto mat_index = geom_state.material();
195,996✔
288
    if (mat_index == MATERIAL_VOID) {
195,996!
289
      accepted = false;
290
    } else {
291
      accepted = model::materials[mat_index]->fissionable();
195,996✔
292
    }
293
  }
294

295
  return accepted;
296
}
7,068,466✔
297

298
//==============================================================================
299
// IndependentSource implementation
300
//==============================================================================
301

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

308
IndependentSource::IndependentSource(pugi::xml_node node) : Source(node)
1,566✔
309
{
310
  // Check for particle type
311
  if (check_for_node(node, "particle")) {
1,566✔
312
    auto temp_str = get_node_value(node, "particle", false, true);
1,448✔
313
    particle_ = ParticleType(temp_str);
1,448✔
314
    if (particle_ == ParticleType::photon() ||
1,448✔
315
        particle_ == ParticleType::electron() ||
1,448✔
316
        particle_ == ParticleType::positron()) {
1,420!
317
      settings::photon_transport = true;
28✔
318
    }
319
  }
1,448✔
320
  validate_particle_type(particle_, "IndependentSource");
1,566✔
321

322
  // Check for external source file
323
  if (check_for_node(node, "file")) {
1,566!
324

325
  } else {
326

327
    // Spatial distribution for external source
328
    if (check_for_node(node, "space")) {
1,566✔
329
      space_ = SpatialDistribution::create(node.child("space"));
1,202✔
330
    } else {
331
      // If no spatial distribution specified, make it a point source
332
      space_ = UPtrSpace {new SpatialPoint()};
364✔
333
    }
334

335
    // For backwards compatibility, check for only fissionable setting on box
336
    // source
337
    auto space_box = dynamic_cast<SpatialBox*>(space_.get());
1,566!
338
    if (space_box) {
1,566✔
339
      if (!only_fissionable_) {
604✔
340
        only_fissionable_ = space_box->only_fissionable();
376✔
341
      }
342
    }
343

344
    // Determine external source angular distribution
345
    if (check_for_node(node, "angle")) {
1,566✔
346
      angle_ = UnitSphereDistribution::create(node.child("angle"));
608✔
347
    } else {
348
      angle_ = UPtrAngle {new Isotropic()};
958✔
349
    }
350

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

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

373
SourceSite IndependentSource::sample(uint64_t* seed) const
6,396,149✔
374
{
375
  SourceSite site {};
6,396,149✔
376
  site.particle = particle_;
6,396,149✔
377
  double r_wgt = 1.0;
6,396,149✔
378
  double E_wgt = 1.0;
6,396,149✔
379

380
  // Repeat sampling source location until a good site has been accepted
381
  bool accepted = false;
6,396,149✔
382
  int64_t n_local_reject = 0;
6,396,149✔
383

384
  while (!accepted) {
13,094,190✔
385

386
    // Sample spatial distribution
387
    auto [r, r_wgt_temp] = space_->sample(seed);
6,698,041✔
388
    site.r = r;
6,698,041✔
389
    r_wgt = r_wgt_temp;
6,698,041✔
390

391
    // Check if sampled position satisfies spatial constraints
392
    accepted = satisfies_spatial_constraints(site.r);
6,698,041✔
393

394
    // Check for rejection
395
    if (!accepted) {
6,698,041✔
396
      ++n_local_reject;
301,892✔
397
      if (n_local_reject >= MAX_SOURCE_REJECTIONS_PER_SAMPLE) {
301,892!
398
        fatal_error("Exceeded maximum number of source rejections per "
×
399
                    "sample. Please check your source definition.");
400
      }
401
    }
402
  }
403

404
  // Sample angle
405
  auto [u, u_wgt] = angle_->sample(seed);
6,396,149✔
406
  site.u = u;
6,396,149✔
407

408
  site.wgt = r_wgt * u_wgt;
6,396,149✔
409

410
  // Sample energy and time for neutron and photon sources
411
  if (settings::solver_type != SolverType::RANDOM_RAY) {
6,396,149✔
412
    // Check for monoenergetic source above maximum particle energy
413
    auto p = particle_.transport_index();
6,012,949✔
414
    auto energy_ptr = dynamic_cast<Discrete*>(energy_.get());
6,012,949!
415
    if (energy_ptr) {
6,012,949✔
416
      auto energies =
3,174,182✔
417
        tensor::Tensor<double>(energy_ptr->x().data(), energy_ptr->x().size());
3,174,182✔
418
      if ((energies > data::energy_max[p]).any()) {
9,522,546!
419
        fatal_error("Source energy above range of energies of at least "
×
420
                    "one cross section table");
421
      }
422
    }
3,174,182✔
423

424
    while (true) {
6,012,949✔
425
      // Sample energy spectrum
426
      auto [E, E_wgt_temp] = energy_->sample(seed);
6,012,949✔
427
      site.E = E;
6,012,949✔
428
      E_wgt = E_wgt_temp;
6,012,949✔
429

430
      // Resample if energy falls above maximum particle energy
431
      if (site.E < data::energy_max[p] &&
12,025,898!
432
          (satisfies_energy_constraints(site.E)))
6,012,949✔
433
        break;
434

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

442
    // Sample particle creation time
443
    auto [time, time_wgt] = time_->sample(seed);
6,012,949✔
444
    site.time = time;
6,012,949✔
445

446
    site.wgt *= (E_wgt * time_wgt);
6,012,949✔
447
  }
448

449
  // Flush local rejection count into global counter
450
  if (n_local_reject > 0) {
6,396,149✔
451
    source_n_reject += n_local_reject;
64,359✔
452
  }
453

454
  return site;
6,396,149✔
455
}
456

457
//==============================================================================
458
// FileSource implementation
459
//==============================================================================
460

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

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

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

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

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

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

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

498
    // Close file
499
    file_close(file_id);
8✔
500
  }
8✔
501

502
  // Make sure particles in source file have valid types
503
  for (const auto& site : this->sites_) {
28,014✔
504
    validate_particle_type(site.particle, "FileSource");
56,004✔
505
  }
506
}
12✔
507

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

515
//==============================================================================
516
// CompiledSourceWrapper implementation
517
//==============================================================================
518

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

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

540
  // reset errors
541
  dlerror();
4✔
542

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

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

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

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

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

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

579
//==============================================================================
580
// MeshElementSpatial implementation
581
//==============================================================================
582

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

588
//==============================================================================
589
// MeshSource implementation
590
//==============================================================================
591

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

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

613
  // Set spatial distributions for each mesh element
614
  for (int elem_index = 0; elem_index < sources_.size(); ++elem_index) {
300✔
615
    sources_[elem_index]->set_space(
264✔
616
      std::make_unique<MeshElementSpatial>(mesh_idx, elem_index));
528✔
617
  }
618

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

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

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

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

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

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

649
void initialize_source()
598✔
650
{
651
  write_message("Initializing source particles...", 5);
598✔
652

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

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

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

675
SourceSite sample_external_source(uint64_t* seed)
5,856,842✔
676
{
677
  // Sample from among multiple source distributions
678
  int i = 0;
5,856,842✔
679
  int n_sources = model::external_sources.size();
5,856,842✔
680
  if (n_sources > 1) {
5,856,842✔
681
    if (settings::uniform_source_sampling) {
646,600✔
682
      i = prn(seed) * n_sources;
400✔
683
    } else {
684
      i = model::external_sources_probability.sample(seed);
646,200✔
685
    }
686
  }
687

688
  // Sample source site from i-th source distribution
689
  SourceSite site {model::external_sources[i]->sample_with_constraints(seed)};
5,856,842✔
690

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

700
  // If running in MG, convert site.E to group
701
  if (!settings::run_CE) {
5,856,840✔
702
    site.E = lower_bound_index(data::mg.rev_energy_bins_.begin(),
316,800✔
703
      data::mg.rev_energy_bins_.end(), site.E);
704
    site.E = data::mg.num_energy_groups_ - site.E - 1.;
316,800✔
705
  }
706

707
  return site;
5,856,840✔
708
}
709

710
void free_memory_source()
1,330✔
711
{
712
  model::external_sources.clear();
1,330✔
713
  reset_source_rejection_counters();
1,330✔
714
}
1,330✔
715

716
void reset_source_rejection_counters()
2,464✔
717
{
718
  source_n_accept = 0;
2,464✔
719
  source_n_reject = 0;
2,464✔
720
}
2,464✔
721

722
//==============================================================================
723
// C API
724
//==============================================================================
725

726
extern "C" int openmc_sample_external_source(
66✔
727
  size_t n, uint64_t* seed, void* sites)
728
{
729
  if (!sites || !seed) {
66!
730
    set_errmsg("Received null pointer.");
×
731
    return OPENMC_E_INVALID_ARGUMENT;
×
732
  }
733

734
  if (model::external_sources.empty()) {
66!
735
    set_errmsg("No external sources have been defined.");
×
736
    return OPENMC_E_OUT_OF_BOUNDS;
×
737
  }
738

739
  auto sites_array = static_cast<SourceSite*>(sites);
66✔
740

741
  // Derive independent per-particle seeds from the base seed so that
742
  // each iteration has its own RNG state for thread-safe parallel sampling.
743
  uint64_t base_seed = *seed;
66✔
744

745
#pragma omp parallel for schedule(static)
746
  for (size_t i = 0; i < n; ++i) {
428,594✔
747
    uint64_t particle_seed = init_seed(base_seed + i, STREAM_SOURCE);
428,528✔
748
    sites_array[i] = sample_external_source(&particle_seed);
428,528✔
749
  }
750
  return 0;
751
}
752

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