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

openmc-dev / openmc / 25631610076

10 May 2026 02:44PM UTC coverage: 81.026% (-0.4%) from 81.388%
25631610076

Pull #3757

github

web-flow
Merge debc5a921 into d56cda254
Pull Request #3757: Testing point detectors

17769 of 25812 branches covered (68.84%)

Branch coverage included in aggregate %.

51 of 373 new or added lines in 25 files covered. (13.67%)

3 existing lines in 2 files now uncovered.

58805 of 68694 relevant lines covered (85.6%)

40257430.53 hits per line

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

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

40
namespace openmc {
41

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

45
namespace {
46

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

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

57
} // namespace
58

59
//==============================================================================
60
// Global variables
61
//==============================================================================
62

63
namespace model {
64

65
vector<unique_ptr<Source>> external_sources;
66

67
vector<unique_ptr<Source>> adjoint_sources;
68

69
DiscreteIndex external_sources_probability;
70

71
} // namespace model
72

73
//==============================================================================
74
// Source implementation
75
//==============================================================================
76

77
Source::Source(pugi::xml_node node)
43,358✔
78
{
79
  // Check for source strength
80
  if (check_for_node(node, "strength")) {
43,358✔
81
    strength_ = std::stod(get_node_value(node, "strength"));
85,770✔
82
    if (strength_ < 0.0) {
42,885!
83
      fatal_error("Source strength is negative.");
×
84
    }
85
  }
86

87
  // Check for additional defined constraints
88
  read_constraints(node);
43,358✔
89
}
43,358✔
90

91
unique_ptr<Source> Source::create(pugi::xml_node node)
43,358✔
92
{
93
  // if the source type is present, use it to determine the type
94
  // of object to create
95
  if (check_for_node(node, "type")) {
43,358✔
96
    std::string source_type = get_node_value(node, "type");
42,732✔
97
    if (source_type == "independent") {
42,732✔
98
      return make_unique<IndependentSource>(node);
42,541✔
99
    } else if (source_type == "file") {
191✔
100
      return make_unique<FileSource>(node);
22✔
101
    } else if (source_type == "compiled") {
169✔
102
      return make_unique<CompiledSourceWrapper>(node);
22✔
103
    } else if (source_type == "mesh") {
147!
104
      return make_unique<MeshSource>(node);
147✔
105
    } else {
106
      fatal_error(fmt::format("Invalid source type '{}' found.", source_type));
×
107
    }
108
  } else {
42,725✔
109
    // support legacy source format
110
    if (check_for_node(node, "file")) {
626✔
111
      return make_unique<FileSource>(node);
22✔
112
    } else if (check_for_node(node, "library")) {
604!
113
      return make_unique<CompiledSourceWrapper>(node);
×
114
    } else {
115
      return make_unique<IndependentSource>(node);
604✔
116
    }
117
  }
118
}
119

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

130
  // Check for domains to reject from
131
  if (check_for_node(node, "domain_type")) {
43,358✔
132
    std::string domain_type = get_node_value(node, "domain_type");
390✔
133
    if (domain_type == "cell") {
390✔
134
      domain_type_ = DomainType::CELL;
69✔
135
    } else if (domain_type == "material") {
321✔
136
      domain_type_ = DomainType::MATERIAL;
46✔
137
    } else if (domain_type == "universe") {
275!
138
      domain_type_ = DomainType::UNIVERSE;
275✔
139
    } else {
140
      fatal_error(
×
141
        std::string("Unrecognized domain type for constraint: " + domain_type));
×
142
    }
143

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

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

163
  if (check_for_node(node, "fissionable")) {
43,358✔
164
    only_fissionable_ = get_node_value_bool(node, "fissionable");
1,041✔
165
  }
166

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

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

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

198
SourceSite Source::sample_with_constraints(uint64_t* seed) const
25,668,875✔
199
{
200
  bool accepted = false;
25,668,875✔
201
  int64_t n_local_reject = 0;
25,668,875✔
202
  SourceSite site {};
25,668,875✔
203

204
  while (!accepted) {
77,968,050✔
205
    // Sample a source site without considering constraints yet
206
    site = this->sample(seed);
26,630,300✔
207

208
    if (constraints_applied()) {
26,630,300✔
209
      accepted = true;
210
    } else {
211
      // Check whether sampled site satisfies constraints
212
      accepted = satisfies_spatial_constraints(site.r) &&
29,080,376✔
213
                 satisfies_energy_constraints(site.E) &&
1,945,888✔
214
                 satisfies_time_constraints(site.time);
496,193✔
215
      if (!accepted) {
961,425✔
216
        ++n_local_reject;
961,425✔
217

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

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

234
  // Flush local rejection count, update accept counter, and check overall
235
  // rejection fraction
236
  if (n_local_reject > 0) {
25,668,875✔
237
    source_n_reject += n_local_reject;
13,910✔
238
  }
239
  ++source_n_accept;
25,668,875✔
240
  check_rejection_fraction(source_n_reject, source_n_accept);
25,668,875✔
241

242
  return site;
25,668,873✔
243
}
244

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

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

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

261
  // Reject particle if it's not in the geometry at all
262
  bool found = exhaustive_find_cell(geom_state);
29,240,836✔
263
  if (!found)
29,240,836✔
264
    return false;
265

266
  // Check the geometry state against specified domains
267
  bool accepted = true;
28,884,137✔
268
  if (!domain_ids_.empty()) {
28,884,137✔
269
    if (domain_type_ == DomainType::MATERIAL) {
1,584,912✔
270
      auto mat_index = geom_state.material();
160,000✔
271
      if (mat_index == MATERIAL_VOID) {
160,000!
272
        accepted = false;
273
      } else {
274
        accepted = contains(domain_ids_, model::materials[mat_index]->id());
320,000✔
275
      }
276
    } else {
277
      for (int i = 0; i < geom_state.n_coord(); i++) {
2,740,542✔
278
        auto id =
1,424,912✔
279
          (domain_type_ == DomainType::CELL)
280
            ? model::cells[geom_state.coord(i).cell()].get()->id_
1,424,912!
281
            : model::universes[geom_state.coord(i).universe()].get()->id_;
×
282
        if ((accepted = contains(domain_ids_, id)))
2,849,824✔
283
          break;
284
      }
285
    }
286
  }
287

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

299
  return accepted;
300
}
29,240,836✔
301

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

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

312
IndependentSource::IndependentSource(pugi::xml_node node) : Source(node)
43,145✔
313
{
314
  // Check for particle type
315
  if (check_for_node(node, "particle")) {
43,145✔
316
    auto temp_str = get_node_value(node, "particle", false, true);
42,541✔
317
    particle_ = ParticleType(temp_str);
42,541✔
318
    if (particle_ == ParticleType::photon() ||
42,541✔
319
        particle_ == ParticleType::electron() ||
42,541✔
320
        particle_ == ParticleType::positron()) {
42,390!
321
      settings::photon_transport = true;
151✔
322
    }
323
  }
42,541✔
324
  validate_particle_type(particle_, "IndependentSource");
43,145✔
325

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

329
  } else {
330

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

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

348
    // Determine external source angular distribution
349
    if (check_for_node(node, "angle")) {
43,144✔
350
      angle_ = UnitSphereDistribution::create(node.child("angle"));
2,476✔
351
    } else {
352
      angle_ = UPtrAngle {new Isotropic()};
40,668✔
353
    }
354

355
    // Determine external source energy distribution
356
    if (check_for_node(node, "energy")) {
43,144✔
357
      pugi::xml_node node_dist = node.child("energy");
3,554✔
358
      energy_ = distribution_from_xml(node_dist);
3,554✔
359

360
      // For decay photon sources, use the absolute photon emission rate in
361
      // [photons/s] as the source strength
362
      if (dynamic_cast<DecaySpectrum*>(energy_.get())) {
3,554!
363
        if (strength_ != 1.0) {
40!
364
          warning(fmt::format(
×
365
            "Source strength of {} is ignored because the source uses a "
366
            "DecaySpectrum energy distribution. The source strength will be "
367
            "set from the DecaySpectrum emission rate.",
368
            strength_));
×
369
        }
370
        strength_ = energy_->integral();
40✔
371
      }
372
    } else {
373
      // Default to a Watt spectrum with parameters 0.988 MeV and 2.249 MeV^-1
374
      energy_ = UPtrDist {new Watt(0.988e6, 2.249e-6)};
39,590✔
375
    }
376

377
    // Determine external source time distribution
378
    if (check_for_node(node, "time")) {
43,144✔
379
      pugi::xml_node node_dist = node.child("time");
30✔
380
      time_ = distribution_from_xml(node_dist);
30✔
381
    } else {
382
      // Default to a Constant time T=0
383
      double T[] {0.0};
43,114✔
384
      double p[] {1.0};
43,114✔
385
      time_ = UPtrDist {new Discrete {T, p, 1}};
43,114✔
386
    }
387
  }
388
}
43,144✔
389

390
SourceSite IndependentSource::sample(uint64_t* seed) const
26,834,125✔
391
{
392
  SourceSite site {};
26,834,125✔
393
  site.particle = particle_;
26,834,125✔
394
  double r_wgt = 1.0;
26,834,125✔
395
  double E_wgt = 1.0;
26,834,125✔
396

397
  // Repeat sampling source location until a good site has been accepted
398
  bool accepted = false;
26,834,125✔
399
  int64_t n_local_reject = 0;
26,834,125✔
400

401
  while (!accepted) {
54,625,266✔
402

403
    // Sample spatial distribution
404
    auto [r, r_wgt_temp] = space_->sample(seed);
27,791,141✔
405
    site.r = r;
27,791,141✔
406
    r_wgt = r_wgt_temp;
27,791,141✔
407

408
    // Check if sampled position satisfies spatial constraints
409
    accepted = satisfies_spatial_constraints(site.r);
27,791,141✔
410

411
    // Check for rejection
412
    if (!accepted) {
27,791,141✔
413
      ++n_local_reject;
957,016✔
414
      if (n_local_reject >= MAX_SOURCE_REJECTIONS_PER_SAMPLE) {
957,016!
415
        fatal_error("Exceeded maximum number of source rejections per "
×
416
                    "sample. Please check your source definition.");
417
      }
418
    }
419
  }
420

421
  // Sample angle
422
  auto [u, u_wgt] = angle_->sample(seed);
26,834,125✔
423
  site.u = u;
26,834,125✔
424

425
  site.wgt = r_wgt * u_wgt;
26,834,125✔
426

427
  // Sample energy and time for neutron and photon sources
428
  if (settings::solver_type != SolverType::RANDOM_RAY) {
26,834,125✔
429
    // Check for monoenergetic source above maximum particle energy
430
    auto p = particle_.transport_index();
25,180,605✔
431
    auto energy_ptr = dynamic_cast<Discrete*>(energy_.get());
25,180,605!
432
    auto decay_spectrum = dynamic_cast<DecaySpectrum*>(energy_.get());
25,180,605!
433
    if (energy_ptr) {
25,180,605✔
434
      auto energies =
13,483,608✔
435
        tensor::Tensor<double>(energy_ptr->x().data(), energy_ptr->x().size());
13,483,608✔
436
      if ((energies > data::energy_max[p]).any()) {
40,450,824!
437
        fatal_error("Source energy above range of energies of at least "
×
438
                    "one cross section table");
439
      }
440
    }
13,483,608✔
441

442
    while (true) {
25,180,605✔
443
      // Sample energy spectrum. For decay photon sources, also get the parent
444
      // nuclide index to store in the source site for tallying purposes.
445
      if (decay_spectrum) {
25,180,605✔
446
        auto sample = decay_spectrum->sample_with_parent(seed);
280,000✔
447
        site.E = sample.energy;
280,000✔
448
        E_wgt = sample.weight;
280,000✔
449
        site.parent_nuclide = sample.parent_nuclide;
280,000✔
450
      } else {
451
        auto [E, E_wgt_temp] = energy_->sample(seed);
24,900,605✔
452
        site.E = E;
24,900,605✔
453
        E_wgt = E_wgt_temp;
24,900,605✔
454
      }
455

456
      // Resample if energy falls above maximum particle energy
457
      if (site.E < data::energy_max[p] &&
50,361,210!
458
          (satisfies_energy_constraints(site.E)))
25,180,605✔
459
        break;
460

461
      ++n_local_reject;
×
462
      if (n_local_reject >= MAX_SOURCE_REJECTIONS_PER_SAMPLE) {
×
463
        fatal_error("Exceeded maximum number of source rejections per "
×
464
                    "sample. Please check your source definition.");
465
      }
466
    }
467

468
    // Sample particle creation time
469
    auto [time, time_wgt] = time_->sample(seed);
25,180,605✔
470
    site.time = time;
25,180,605✔
471

472
    site.wgt *= (E_wgt * time_wgt);
25,180,605✔
473
  }
474

475
  // Flush local rejection count into global counter
476
  if (n_local_reject > 0) {
26,834,125✔
477
    source_n_reject += n_local_reject;
254,231✔
478
  }
479

480
  return site;
26,834,125✔
481
}
482

483
//==============================================================================
484
// FileSource implementation
485
//==============================================================================
486

487
FileSource::FileSource(pugi::xml_node node) : Source(node)
44✔
488
{
489
  auto path = get_node_value(node, "file", false, true);
44✔
490
  load_sites_from_file(path);
44✔
491
}
38✔
492

493
FileSource::FileSource(const std::string& path)
22✔
494
{
495
  load_sites_from_file(path);
22✔
496
}
22✔
497

498
void FileSource::load_sites_from_file(const std::string& path)
66✔
499
{
500
  // If MCPL file, use the dedicated file reader
501
  if (ends_with(path, ".mcpl") || ends_with(path, ".mcpl.gz")) {
110!
502
    sites_ = mcpl_source_sites(path);
22✔
503
  } else {
504
    // Check if source file exists
505
    if (!file_exists(path)) {
44!
506
      fatal_error(fmt::format("Source file '{}' does not exist.", path));
×
507
    }
508

509
    write_message(6, "Reading source file from {}...", path);
44✔
510

511
    // Open the binary file
512
    hid_t file_id = file_open(path, 'r', true);
44✔
513

514
    // Check to make sure this is a source file
515
    std::string filetype;
44✔
516
    read_attribute(file_id, "filetype", filetype);
44✔
517
    if (filetype != "source" && filetype != "statepoint") {
44!
518
      fatal_error("Specified starting source file not a source file type.");
×
519
    }
520

521
    // Read in the source particles
522
    read_source_bank(file_id, sites_, false);
44✔
523

524
    // Close file
525
    file_close(file_id);
38✔
526
  }
38✔
527

528
  // Make sure particles in source file have valid types
529
  for (const auto& site : this->sites_) {
124,068✔
530
    validate_particle_type(site.particle, "FileSource");
248,016✔
531
  }
532
}
60✔
533

534
SourceSite FileSource::sample(uint64_t* seed) const
208,081✔
535
{
536
  // Sample a particle randomly from list
537
  size_t i_site = sites_.size() * prn(seed);
208,081✔
538
  return sites_[i_site];
208,081✔
539
}
540

541
//==============================================================================
542
// CompiledSourceWrapper implementation
543
//==============================================================================
544

545
CompiledSourceWrapper::CompiledSourceWrapper(pugi::xml_node node) : Source(node)
22✔
546
{
547
  // Get shared library path and parameters
548
  auto path = get_node_value(node, "library", false, true);
22✔
549
  std::string parameters;
22✔
550
  if (check_for_node(node, "parameters")) {
22✔
551
    parameters = get_node_value(node, "parameters", false, true);
11✔
552
  }
553
  setup(path, parameters);
22✔
554
}
22✔
555

556
void CompiledSourceWrapper::setup(
22✔
557
  const std::string& path, const std::string& parameters)
558
{
559
#ifdef HAS_DYNAMIC_LINKING
560
  // Open the library
561
  shared_library_ = dlopen(path.c_str(), RTLD_LAZY);
22✔
562
  if (!shared_library_) {
22!
563
    fatal_error("Couldn't open source library " + path);
×
564
  }
565

566
  // reset errors
567
  dlerror();
22✔
568

569
  // get the function to create the custom source from the library
570
  auto create_compiled_source = reinterpret_cast<create_compiled_source_t*>(
22✔
571
    dlsym(shared_library_, "openmc_create_source"));
22✔
572

573
  // check for any dlsym errors
574
  auto dlsym_error = dlerror();
22✔
575
  if (dlsym_error) {
22!
576
    std::string error_msg = fmt::format(
×
577
      "Couldn't open the openmc_create_source symbol: {}", dlsym_error);
×
578
    dlclose(shared_library_);
×
579
    fatal_error(error_msg);
×
580
  }
×
581

582
  // create a pointer to an instance of the custom source
583
  compiled_source_ = create_compiled_source(parameters);
22✔
584

585
#else
586
  fatal_error("Custom source libraries have not yet been implemented for "
587
              "non-POSIX systems");
588
#endif
589
}
22✔
590

591
CompiledSourceWrapper::~CompiledSourceWrapper()
44✔
592
{
593
  // Make sure custom source is cleared before closing shared library
594
  if (compiled_source_.get())
22!
595
    compiled_source_.reset();
22✔
596

597
#ifdef HAS_DYNAMIC_LINKING
598
  dlclose(shared_library_);
22✔
599
#else
600
  fatal_error("Custom source libraries have not yet been implemented for "
601
              "non-POSIX systems");
602
#endif
603
}
44✔
604

605
//==============================================================================
606
// MeshElementSpatial implementation
607
//==============================================================================
608

609
std::pair<Position, double> MeshElementSpatial::sample(uint64_t* seed) const
1,092,273✔
610
{
611
  return {model::meshes[mesh_index_]->sample_element(elem_index_, seed), 1.0};
1,092,273✔
612
}
613

614
//==============================================================================
615
// MeshSource implementation
616
//==============================================================================
617

618
MeshSource::MeshSource(pugi::xml_node node) : Source(node)
147✔
619
{
620
  int32_t mesh_id = stoi(get_node_value(node, "mesh"));
294✔
621
  int32_t mesh_idx = model::mesh_map.at(mesh_id);
147✔
622
  const auto& mesh = model::meshes[mesh_idx];
147✔
623

624
  std::vector<double> strengths;
147✔
625
  // read all source distributions and populate strengths vector for MeshSpatial
626
  // object
627
  for (auto source_node : node.children("source")) {
37,203✔
628
    auto src = Source::create(source_node);
37,056✔
629
    if (auto ptr = dynamic_cast<IndependentSource*>(src.get())) {
37,056!
630
      src.release();
37,056✔
631
      sources_.emplace_back(ptr);
37,056✔
632
    } else {
633
      fatal_error(
×
634
        "The source assigned to each element must be an IndependentSource.");
635
    }
636
    strengths.push_back(sources_.back()->strength());
37,056✔
637
  }
37,056✔
638

639
  // Set spatial distributions for each mesh element
640
  for (int elem_index = 0; elem_index < sources_.size(); ++elem_index) {
37,203✔
641
    sources_[elem_index]->set_space(
37,056✔
642
      std::make_unique<MeshElementSpatial>(mesh_idx, elem_index));
74,112✔
643
  }
644

645
  // Make sure sources use valid particle types
646
  for (const auto& src : sources_) {
37,203✔
647
    validate_particle_type(src->particle_type(), "MeshSource");
74,112✔
648
  }
649

650
  // the number of source distributions should either be one or equal to the
651
  // number of mesh elements
652
  if (sources_.size() > 1 && sources_.size() != mesh->n_bins()) {
147!
653
    fatal_error(fmt::format("Incorrect number of source distributions ({}) for "
×
654
                            "mesh source with {} elements.",
655
      sources_.size(), mesh->n_bins()));
×
656
  }
657

658
  space_ = std::make_unique<MeshSpatial>(mesh_idx, strengths);
147✔
659
}
147✔
660

661
SourceSite MeshSource::sample(uint64_t* seed) const
1,081,614✔
662
{
663
  // Sample a mesh element based on the relative strengths
664
  int32_t element = space_->sample_element_index(seed);
1,081,614✔
665

666
  // Sample the distribution for the specific mesh element; note that the
667
  // spatial distribution has been set for each element using MeshElementSpatial
668
  return source(element)->sample_with_constraints(seed);
2,163,228!
669
}
670

671
//==============================================================================
672
// Non-member functions
673
//==============================================================================
674

675
void initialize_source()
2,943✔
676
{
677
  write_message("Initializing source particles...", 5);
2,943✔
678

679
// Generation source sites from specified distribution in user input
680
#pragma omp parallel for
2,313✔
681
  for (int64_t i = 0; i < simulation::work_per_rank; ++i) {
516,256✔
682
    // initialize random number seed
683
    int64_t id = simulation::total_gen * settings::n_particles +
515,626✔
684
                 simulation::work_index[mpi::rank] + i + 1;
515,626✔
685
    uint64_t seed = init_seed(id, STREAM_SOURCE);
515,626✔
686

687
    // sample external source distribution
688
    simulation::source_bank[i] = sample_external_source(&seed);
515,626✔
689
  }
690

691
  // Write out initial source
692
  if (settings::write_initial_source) {
2,943!
693
    write_message("Writing out initial source...", 5);
×
694
    std::string filename = settings::path_output + "initial_source.h5";
×
695
    hid_t file_id = file_open(filename, 'w', true);
×
696
    write_source_bank(file_id, simulation::source_bank, simulation::work_index);
×
697
    file_close(file_id);
×
698
  }
×
699
}
2,943✔
700

701
SourceSite sample_external_source(uint64_t* seed)
24,587,261✔
702
{
703
  // Sample from among multiple source distributions
704
  int i = 0;
24,587,261✔
705
  int n_sources = model::external_sources.size();
24,587,261✔
706
  if (n_sources > 1) {
24,587,261✔
707
    if (settings::uniform_source_sampling) {
2,588,400✔
708
      i = prn(seed) * n_sources;
1,600✔
709
    } else {
710
      i = model::external_sources_probability.sample(seed);
2,586,800✔
711
    }
712
  }
713

714
  // Sample source site from i-th source distribution
715
  SourceSite site {model::external_sources[i]->sample_with_constraints(seed)};
24,587,261✔
716

717
  // For uniform source sampling, multiply the weight by the ratio of the actual
718
  // probability of sampling source i to the biased probability of sampling
719
  // source i, which is (strength_i / total_strength) / (1 / n)
720
  if (n_sources > 1 && settings::uniform_source_sampling) {
24,587,259✔
721
    double total_strength = model::external_sources_probability.integral();
1,600✔
722
    site.wgt *=
3,200✔
723
      model::external_sources[i]->strength() * n_sources / total_strength;
1,600✔
724
  }
725

726
  // If running in MG, convert site.E to group
727
  if (!settings::run_CE) {
24,587,259✔
728
    site.E = lower_bound_index(data::mg.rev_energy_bins_.begin(),
1,267,440✔
729
      data::mg.rev_energy_bins_.end(), site.E);
730
    site.E = data::mg.num_energy_groups_ - site.E - 1.;
1,267,440✔
731
  }
732

733
  if (!model::active_point_tallies.empty()) {
24,587,259!
NEW
734
    score_point_tally_source(site, i);
×
735
  }
736

737
  return site;
24,587,259✔
738
}
739

740
void free_memory_source()
6,204✔
741
{
742
  model::external_sources.clear();
6,204✔
743
  model::adjoint_sources.clear();
6,204✔
744
  reset_source_rejection_counters();
6,204✔
745
}
6,204✔
746

747
void reset_source_rejection_counters()
11,674✔
748
{
749
  source_n_accept = 0;
11,674✔
750
  source_n_reject = 0;
11,674✔
751
}
11,674✔
752

753
//==============================================================================
754
// C API
755
//==============================================================================
756

757
extern "C" int openmc_sample_external_source(
867✔
758
  size_t n, uint64_t* seed, void* sites)
759
{
760
  if (!sites || !seed) {
867!
761
    set_errmsg("Received null pointer.");
×
762
    return OPENMC_E_INVALID_ARGUMENT;
×
763
  }
764

765
  if (model::external_sources.empty()) {
867!
766
    set_errmsg("No external sources have been defined.");
×
767
    return OPENMC_E_OUT_OF_BOUNDS;
×
768
  }
769

770
  auto sites_array = static_cast<SourceSite*>(sites);
867✔
771

772
  // Derive independent per-particle seeds from the base seed so that
773
  // each iteration has its own RNG state for thread-safe parallel sampling.
774
  uint64_t base_seed = *seed;
867✔
775

776
#pragma omp parallel for schedule(static)
801✔
777
  for (size_t i = 0; i < n; ++i) {
428,594✔
778
    uint64_t particle_seed = init_seed(base_seed + i, STREAM_SOURCE);
428,528✔
779
    sites_array[i] = sample_external_source(&particle_seed);
428,528✔
780
  }
781
  return 0;
801✔
782
}
783

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