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

openmc-dev / openmc / 25459358369

06 May 2026 08:29PM UTC coverage: 81.015% (-0.4%) from 81.374%
25459358369

Pull #3757

github

web-flow
Merge b077c860e into e542b2f03
Pull Request #3757: Testing point detectors

17720 of 25751 branches covered (68.81%)

Branch coverage included in aggregate %.

44 of 340 new or added lines in 25 files covered. (12.94%)

81 existing lines in 3 files now uncovered.

58569 of 68415 relevant lines covered (85.61%)

47185324.75 hits per line

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

80.7
/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)
78,870✔
47
{
48
  if (type.is_transportable())
78,870!
49
    return;
78,870✔
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
vector<unique_ptr<Source>> adjoint_sources;
67

68
DiscreteIndex external_sources_probability;
69

70
} // namespace model
71

72
//==============================================================================
73
// Source implementation
74
//==============================================================================
75

76
Source::Source(pugi::xml_node node)
4,334✔
77
{
78
  // Check for source strength
79
  if (check_for_node(node, "strength")) {
4,334✔
80
    strength_ = std::stod(get_node_value(node, "strength"));
8,152✔
81
    if (strength_ < 0.0) {
4,076!
82
      fatal_error("Source strength is negative.");
×
83
    }
84
  }
85

86
  // Check for additional defined constraints
87
  read_constraints(node);
4,334✔
88
}
4,334✔
89

90
unique_ptr<Source> Source::create(pugi::xml_node node)
4,334✔
91
{
92
  // if the source type is present, use it to determine the type
93
  // of object to create
94
  if (check_for_node(node, "type")) {
4,334✔
95
    std::string source_type = get_node_value(node, "type");
3,983✔
96
    if (source_type == "independent") {
3,983✔
97
      return make_unique<IndependentSource>(node);
3,866✔
98
    } else if (source_type == "file") {
117✔
99
      return make_unique<FileSource>(node);
15✔
100
    } else if (source_type == "compiled") {
102✔
101
      return make_unique<CompiledSourceWrapper>(node);
12✔
102
    } else if (source_type == "mesh") {
90!
103
      return make_unique<MeshSource>(node);
90✔
104
    } else {
105
      fatal_error(fmt::format("Invalid source type '{}' found.", source_type));
×
106
    }
107
  } else {
3,978✔
108
    // support legacy source format
109
    if (check_for_node(node, "file")) {
351✔
110
      return make_unique<FileSource>(node);
12✔
111
    } else if (check_for_node(node, "library")) {
339!
112
      return make_unique<CompiledSourceWrapper>(node);
×
113
    } else {
114
      return make_unique<IndependentSource>(node);
339✔
115
    }
116
  }
117
}
118

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

129
  // Check for domains to reject from
130
  if (check_for_node(node, "domain_type")) {
4,334✔
131
    std::string domain_type = get_node_value(node, "domain_type");
212✔
132
    if (domain_type == "cell") {
212✔
133
      domain_type_ = DomainType::CELL;
50✔
134
    } else if (domain_type == "material") {
162✔
135
      domain_type_ = DomainType::MATERIAL;
12✔
136
    } else if (domain_type == "universe") {
150!
137
      domain_type_ = DomainType::UNIVERSE;
150✔
138
    } else {
139
      fatal_error(
×
140
        std::string("Unrecognized domain type for constraint: " + domain_type));
×
141
    }
142

143
    auto ids = get_node_array<int>(node, "domain_ids");
212✔
144
    domain_ids_.insert(ids.begin(), ids.end());
212✔
145
  }
212✔
146

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

162
  if (check_for_node(node, "fissionable")) {
4,334✔
163
    only_fissionable_ = get_node_value_bool(node, "fissionable");
617✔
164
  }
165

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

180
void check_rejection_fraction(int64_t n_reject, int64_t n_accept)
15,519,319✔
181
{
182
  // Don't check unless we've hit a minimum number of total sites rejected
183
  if (n_reject < EXTSRC_REJECT_THRESHOLD)
15,519,319✔
184
    return;
185

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

197
SourceSite Source::sample_with_constraints(uint64_t* seed) const
15,519,319✔
198
{
199
  bool accepted = false;
15,519,319✔
200
  int64_t n_local_reject = 0;
15,519,319✔
201
  SourceSite site {};
15,519,319✔
202

203
  while (!accepted) {
47,151,661✔
204
    // Sample a source site without considering constraints yet
205
    site = this->sample(seed);
16,113,023✔
206

207
    if (constraints_applied()) {
16,113,023✔
208
      accepted = true;
209
    } else {
210
      // Check whether sampled site satisfies constraints
211
      accepted = satisfies_spatial_constraints(site.r) &&
17,637,281✔
212
                 satisfies_energy_constraints(site.E) &&
1,209,059✔
213
                 satisfies_time_constraints(site.time);
310,205✔
214
      if (!accepted) {
593,704✔
215
        ++n_local_reject;
593,704✔
216

217
        // Check per-particle rejection limit
218
        if (n_local_reject >= MAX_SOURCE_REJECTIONS_PER_SAMPLE) {
593,704!
219
          fatal_error("Exceeded maximum number of source rejections per "
×
220
                      "sample. Please check your source definition.");
221
        }
222

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

233
  // Flush local rejection count, update accept counter, and check overall
234
  // rejection fraction
235
  if (n_local_reject > 0) {
15,519,319✔
236
    source_n_reject += n_local_reject;
8,746✔
237
  }
238
  ++source_n_accept;
15,519,319✔
239
  check_rejection_fraction(source_n_reject, source_n_accept);
15,519,319✔
240

241
  return site;
15,519,315✔
242
}
243

244
bool Source::satisfies_energy_constraints(double E) const
15,534,423✔
245
{
246
  return E > energy_bounds_.first && E < energy_bounds_.second;
15,534,423!
247
}
248

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

254
bool Source::satisfies_spatial_constraints(Position r) const
17,828,920✔
255
{
256
  GeometryState geom_state;
17,828,920✔
257
  geom_state.r() = r;
17,828,920✔
258
  geom_state.u() = {0.0, 0.0, 1.0};
17,828,920✔
259

260
  // Reject particle if it's not in the geometry at all
261
  bool found = exhaustive_find_cell(geom_state);
17,828,920✔
262
  if (!found)
17,828,920✔
263
    return false;
264

265
  // Check the geometry state against specified domains
266
  bool accepted = true;
17,606,185✔
267
  if (!domain_ids_.empty()) {
17,606,185✔
268
    if (domain_type_ == DomainType::MATERIAL) {
969,051!
269
      auto mat_index = geom_state.material();
×
270
      if (mat_index == MATERIAL_VOID) {
×
271
        accepted = false;
272
      } else {
273
        accepted = contains(domain_ids_, model::materials[mat_index]->id());
×
274
      }
275
    } else {
276
      for (int i = 0; i < geom_state.n_coord(); i++) {
1,868,538✔
277
        auto id =
969,051✔
278
          (domain_type_ == DomainType::CELL)
279
            ? model::cells[geom_state.coord(i).cell()].get()->id_
969,051!
280
            : model::universes[geom_state.coord(i).universe()].get()->id_;
×
281
        if ((accepted = contains(domain_ids_, id)))
1,938,102✔
282
          break;
283
      }
284
    }
285
  }
286

287
  // Check if spatial site is in fissionable material
288
  if (accepted && only_fissionable_) {
17,606,185✔
289
    // Determine material
290
    auto mat_index = geom_state.material();
489,990✔
291
    if (mat_index == MATERIAL_VOID) {
489,990!
292
      accepted = false;
293
    } else {
294
      accepted = model::materials[mat_index]->fissionable();
489,990✔
295
    }
296
  }
297

298
  return accepted;
299
}
17,828,920✔
300

301
//==============================================================================
302
// IndependentSource implementation
303
//==============================================================================
304

305
IndependentSource::IndependentSource(
949✔
306
  UPtrSpace space, UPtrAngle angle, UPtrDist energy, UPtrDist time)
949✔
307
  : space_ {std::move(space)}, angle_ {std::move(angle)},
949✔
308
    energy_ {std::move(energy)}, time_ {std::move(time)}
949✔
309
{}
949✔
310

311
IndependentSource::IndependentSource(pugi::xml_node node) : Source(node)
4,205✔
312
{
313
  // Check for particle type
314
  if (check_for_node(node, "particle")) {
4,205✔
315
    auto temp_str = get_node_value(node, "particle", false, true);
3,866✔
316
    particle_ = ParticleType(temp_str);
3,866✔
317
    if (particle_ == ParticleType::photon() ||
3,866✔
318
        particle_ == ParticleType::electron() ||
3,866✔
319
        particle_ == ParticleType::positron()) {
3,791!
320
      settings::photon_transport = true;
75✔
321
    }
322
  }
3,866✔
323
  validate_particle_type(particle_, "IndependentSource");
4,205✔
324

325
  // Check for external source file
326
  if (check_for_node(node, "file")) {
4,205!
327

328
  } else {
329

330
    // Spatial distribution for external source
331
    if (check_for_node(node, "space")) {
4,205✔
332
      space_ = SpatialDistribution::create(node.child("space"));
3,233✔
333
    } else {
334
      // If no spatial distribution specified, make it a point source
335
      space_ = UPtrSpace {new SpatialPoint()};
972✔
336
    }
337

338
    // For backwards compatibility, check for only fissionable setting on box
339
    // source
340
    auto space_box = dynamic_cast<SpatialBox*>(space_.get());
4,205!
341
    if (space_box) {
4,205✔
342
      if (!only_fissionable_) {
1,699✔
343
        only_fissionable_ = space_box->only_fissionable();
1,082✔
344
      }
345
    }
346

347
    // Determine external source angular distribution
348
    if (check_for_node(node, "angle")) {
4,205✔
349
      angle_ = UnitSphereDistribution::create(node.child("angle"));
1,534✔
350
    } else {
351
      angle_ = UPtrAngle {new Isotropic()};
2,671✔
352
    }
353

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

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

376
SourceSite IndependentSource::sample(uint64_t* seed) const
16,247,169✔
377
{
378
  SourceSite site {};
16,247,169✔
379
  site.particle = particle_;
16,247,169✔
380
  double r_wgt = 1.0;
16,247,169✔
381
  double E_wgt = 1.0;
16,247,169✔
382

383
  // Repeat sampling source location until a good site has been accepted
384
  bool accepted = false;
16,247,169✔
385
  int64_t n_local_reject = 0;
16,247,169✔
386

387
  while (!accepted) {
33,177,235✔
388

389
    // Sample spatial distribution
390
    auto [r, r_wgt_temp] = space_->sample(seed);
16,930,066✔
391
    site.r = r;
16,930,066✔
392
    r_wgt = r_wgt_temp;
16,930,066✔
393

394
    // Check if sampled position satisfies spatial constraints
395
    accepted = satisfies_spatial_constraints(site.r);
16,930,066✔
396

397
    // Check for rejection
398
    if (!accepted) {
16,930,066✔
399
      ++n_local_reject;
682,897✔
400
      if (n_local_reject >= MAX_SOURCE_REJECTIONS_PER_SAMPLE) {
682,897!
401
        fatal_error("Exceeded maximum number of source rejections per "
×
402
                    "sample. Please check your source definition.");
403
      }
404
    }
405
  }
406

407
  // Sample angle
408
  auto [u, u_wgt] = angle_->sample(seed);
16,247,169✔
409
  site.u = u;
16,247,169✔
410

411
  site.wgt = r_wgt * u_wgt;
16,247,169✔
412

413
  // Sample energy and time for neutron and photon sources
414
  if (settings::solver_type != SolverType::RANDOM_RAY) {
16,247,169✔
415
    // Check for monoenergetic source above maximum particle energy
416
    auto p = particle_.transport_index();
15,214,169✔
417
    auto energy_ptr = dynamic_cast<Discrete*>(energy_.get());
15,214,169!
418
    if (energy_ptr) {
15,214,169✔
419
      auto energies =
7,935,455✔
420
        tensor::Tensor<double>(energy_ptr->x().data(), energy_ptr->x().size());
7,935,455✔
421
      if ((energies > data::energy_max[p]).any()) {
23,806,365!
422
        fatal_error("Source energy above range of energies of at least "
×
423
                    "one cross section table");
424
      }
425
    }
7,935,455✔
426

427
    while (true) {
15,214,169✔
428
      // Sample energy spectrum
429
      auto [E, E_wgt_temp] = energy_->sample(seed);
15,214,169✔
430
      site.E = E;
15,214,169✔
431
      E_wgt = E_wgt_temp;
15,214,169✔
432

433
      // Resample if energy falls above maximum particle energy
434
      if (site.E < data::energy_max[p] &&
30,428,338!
435
          (satisfies_energy_constraints(site.E)))
15,214,169✔
436
        break;
437

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

445
    // Sample particle creation time
446
    auto [time, time_wgt] = time_->sample(seed);
15,214,169✔
447
    site.time = time;
15,214,169✔
448

449
    site.wgt *= (E_wgt * time_wgt);
15,214,169✔
450
  }
451

452
  // Flush local rejection count into global counter
453
  if (n_local_reject > 0) {
16,247,169✔
454
    source_n_reject += n_local_reject;
160,467✔
455
  }
456

457
  return site;
16,247,169✔
458
}
459

460
//==============================================================================
461
// FileSource implementation
462
//==============================================================================
463

464
FileSource::FileSource(pugi::xml_node node) : Source(node)
27✔
465
{
466
  auto path = get_node_value(node, "file", false, true);
27✔
467
  load_sites_from_file(path);
27✔
468
}
22✔
469

470
FileSource::FileSource(const std::string& path)
12✔
471
{
472
  load_sites_from_file(path);
12✔
473
}
12✔
474

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

486
    write_message(6, "Reading source file from {}...", path);
27✔
487

488
    // Open the binary file
489
    hid_t file_id = file_open(path, 'r', true);
27✔
490

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

498
    // Read in the source particles
499
    read_source_bank(file_id, sites_, false);
27✔
500

501
    // Close file
502
    file_close(file_id);
22✔
503
  }
22✔
504

505
  // Make sure particles in source file have valid types
506
  for (const auto& site : this->sites_) {
74,039✔
507
    validate_particle_type(site.particle, "FileSource");
148,010✔
508
  }
509
}
34✔
510

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

518
//==============================================================================
519
// CompiledSourceWrapper implementation
520
//==============================================================================
521

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

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

543
  // reset errors
544
  dlerror();
12✔
545

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

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

559
  // create a pointer to an instance of the custom source
560
  compiled_source_ = create_compiled_source(parameters);
12✔
561

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

568
CompiledSourceWrapper::~CompiledSourceWrapper()
24✔
569
{
570
  // Make sure custom source is cleared before closing shared library
571
  if (compiled_source_.get())
12!
572
    compiled_source_.reset();
12✔
573

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

582
//==============================================================================
583
// MeshElementSpatial implementation
584
//==============================================================================
585

586
std::pair<Position, double> MeshElementSpatial::sample(uint64_t* seed) const
675,212✔
587
{
588
  return {model::meshes[mesh_index_]->sample_element(elem_index_, seed), 1.0};
675,212✔
589
}
590

591
//==============================================================================
592
// MeshSource implementation
593
//==============================================================================
594

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

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

616
  // Set spatial distributions for each mesh element
617
  for (int elem_index = 0; elem_index < sources_.size(); ++elem_index) {
750✔
618
    sources_[elem_index]->set_space(
660✔
619
      std::make_unique<MeshElementSpatial>(mesh_idx, elem_index));
1,320✔
620
  }
621

622
  // Make sure sources use valid particle types
623
  for (const auto& src : sources_) {
750✔
624
    validate_particle_type(src->particle_type(), "MeshSource");
1,320✔
625
  }
626

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

635
  space_ = std::make_unique<MeshSpatial>(mesh_idx, strengths);
90✔
636
}
90✔
637

638
SourceSite MeshSource::sample(uint64_t* seed) const
668,600✔
639
{
640
  // Sample a mesh element based on the relative strengths
641
  int32_t element = space_->sample_element_index(seed);
668,600✔
642

643
  // Sample the distribution for the specific mesh element; note that the
644
  // spatial distribution has been set for each element using MeshElementSpatial
645
  return source(element)->sample_with_constraints(seed);
1,337,200!
646
}
647

648
//==============================================================================
649
// Non-member functions
650
//==============================================================================
651

652
void initialize_source()
1,715✔
653
{
654
  write_message("Initializing source particles...", 5);
1,715✔
655

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

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

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

678
SourceSite sample_external_source(uint64_t* seed)
14,850,719✔
679
{
680
  // Sample from among multiple source distributions
681
  int i = 0;
14,850,719✔
682
  int n_sources = model::external_sources.size();
14,850,719✔
683
  if (n_sources > 1) {
14,850,719✔
684
    if (settings::uniform_source_sampling) {
1,616,500✔
685
      i = prn(seed) * n_sources;
1,000✔
686
    } else {
687
      i = model::external_sources_probability.sample(seed);
1,615,500✔
688
    }
689
  }
690

691
  // Sample source site from i-th source distribution
692
  SourceSite site {model::external_sources[i]->sample_with_constraints(seed)};
14,850,719✔
693

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

703
  // If running in MG, convert site.E to group
704
  if (!settings::run_CE) {
14,850,715✔
705
    site.E = lower_bound_index(data::mg.rev_energy_bins_.begin(),
792,150✔
706
      data::mg.rev_energy_bins_.end(), site.E);
707
    site.E = data::mg.num_energy_groups_ - site.E - 1.;
792,150✔
708
  }
709

710
  if (!model::active_point_tallies.empty()) {
14,850,715!
NEW
711
    score_point_tally(site, i);
×
712
  }
713

714
  return site;
14,850,715✔
715
}
716

717
void free_memory_source()
3,620✔
718
{
719
  model::external_sources.clear();
3,620✔
720
  model::adjoint_sources.clear();
3,620✔
721
  reset_source_rejection_counters();
3,620✔
722
}
3,620✔
723

724
void reset_source_rejection_counters()
6,803✔
725
{
726
  source_n_accept = 0;
6,803✔
727
  source_n_reject = 0;
6,803✔
728
}
6,803✔
729

730
//==============================================================================
731
// C API
732
//==============================================================================
733

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

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

747
  auto sites_array = static_cast<SourceSite*>(sites);
165✔
748

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

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

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