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

openmc-dev / openmc / 21879360132

10 Feb 2026 07:28PM UTC coverage: 81.74% (-0.08%) from 81.817%
21879360132

Pull #3785

github

web-flow
Merge 8f0c98b1a into 3f20a5e22
Pull Request #3785: Coincident source

17383 of 24440 branches covered (71.13%)

Branch coverage included in aggregate %.

128 of 206 new or added lines in 5 files covered. (62.14%)

505 existing lines in 13 files now uncovered.

56297 of 65700 relevant lines covered (85.69%)

46772278.72 hits per line

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

62.47
/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 "xtensor/xadapt.hpp"
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
namespace {
41

42
void validate_particle_type(ParticleType type, const std::string& context)
257,194✔
43
{
44
  if (type.is_transportable())
257,194!
45
    return;
257,194✔
46

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

52
} // namespace
53

54
//==============================================================================
55
// Global variables
56
//==============================================================================
57

58
namespace model {
59

60
vector<unique_ptr<Source>> external_sources;
61

62
DiscreteIndex external_sources_probability;
63

64
} // namespace model
65

66
//==============================================================================
67
// Source implementation
68
//==============================================================================
69

70
Source::Source(pugi::xml_node node)
46,027✔
71
{
72
  // Check for source strength
73
  if (check_for_node(node, "strength")) {
46,027✔
74
    strength_ = std::stod(get_node_value(node, "strength"));
45,339✔
75
    if (strength_ < 0.0) {
45,339!
76
      fatal_error("Source strength is negative.");
×
77
    }
78
  }
79

80
  // Check for additional defined constraints
81
  read_constraints(node);
46,027✔
82
}
46,027✔
83

84
unique_ptr<Source> Source::create(pugi::xml_node node)
46,027✔
85
{
86
  // if the source type is present, use it to determine the type
87
  // of object to create
88
  if (check_for_node(node, "type")) {
46,027✔
89
    std::string source_type = get_node_value(node, "type");
45,126✔
90
    if (source_type == "independent") {
45,126✔
91
      return make_unique<IndependentSource>(node);
44,862✔
92
    } else if (source_type == "file") {
264✔
93
      return make_unique<FileSource>(node);
31✔
94
    } else if (source_type == "compiled") {
233✔
95
      return make_unique<CompiledSourceWrapper>(node);
32✔
96
    } else if (source_type == "mesh") {
201!
97
      return make_unique<MeshSource>(node);
201✔
NEW
98
    } else if (source_type == "coincident") {
×
NEW
99
      return make_unique<CoincidentSource>(node);
×
100
    } else {
101
      fatal_error(fmt::format("Invalid source type '{}' found.", source_type));
×
102
    }
103
  } else {
45,116✔
104
    // support legacy source format
105
    if (check_for_node(node, "file")) {
901✔
106
      return make_unique<FileSource>(node);
32✔
107
    } else if (check_for_node(node, "library")) {
869!
108
      return make_unique<CompiledSourceWrapper>(node);
×
109
    } else {
110
      return make_unique<IndependentSource>(node);
869✔
111
    }
112
  }
113
}
114

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

125
  // Check for domains to reject from
126
  if (check_for_node(node, "domain_type")) {
46,027✔
127
    std::string domain_type = get_node_value(node, "domain_type");
475✔
128
    if (domain_type == "cell") {
475✔
129
      domain_type_ = DomainType::CELL;
91✔
130
    } else if (domain_type == "material") {
384✔
131
      domain_type_ = DomainType::MATERIAL;
16✔
132
    } else if (domain_type == "universe") {
368!
133
      domain_type_ = DomainType::UNIVERSE;
368✔
134
    } else {
135
      fatal_error(
×
136
        std::string("Unrecognized domain type for constraint: " + domain_type));
×
137
    }
138

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

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

158
  if (check_for_node(node, "fissionable")) {
46,027✔
159
    only_fissionable_ = get_node_value_bool(node, "fissionable");
1,455✔
160
  }
161

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

176
void check_rejection_fraction(int64_t n_reject, int64_t n_accept)
2,592,908✔
177
{
178
  // Don't check unless we've hit a minimum number of total sites rejected
179
  if (n_reject < EXTSRC_REJECT_THRESHOLD)
2,592,908✔
180
    return;
899,352✔
181

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

193
SourceSite Source::sample_with_constraints(uint64_t* seed) const
1,496,540✔
194
{
195
  bool accepted = false;
1,496,540✔
196
  static int64_t n_reject = 0;
197
  static int64_t n_accept = 0;
198
  SourceSite site;
1,496,540✔
199

200
  while (!accepted) {
2,993,080✔
201
    // Sample a source site without considering constraints yet
202
    site = this->sample(seed);
1,496,540✔
203

204
    if (constraints_applied()) {
1,496,540!
205
      accepted = true;
1,496,540✔
206
    } else {
207
      // Check whether sampled site satisfies constraints
UNCOV
208
      accepted = satisfies_spatial_constraints(site.r) &&
×
UNCOV
209
                 satisfies_energy_constraints(site.E) &&
×
UNCOV
210
                 satisfies_time_constraints(site.time);
×
UNCOV
211
      if (!accepted) {
×
212
        // Increment number of rejections and check against minimum fraction
UNCOV
213
        ++n_reject;
×
UNCOV
214
        check_rejection_fraction(n_reject, n_accept);
×
215

216
        // For the "kill" strategy, accept particle but set weight to 0 so that
217
        // it is terminated immediately
UNCOV
218
        if (rejection_strategy_ == RejectionStrategy::KILL) {
×
219
          accepted = true;
×
220
          site.wgt = 0.0;
×
221
        }
222
      }
223
    }
224
  }
225

226
  // Increment number of accepted samples
227
  ++n_accept;
1,496,540✔
228

229
  return site;
1,496,540✔
230
}
231

232
vector<SourceSite> Source::sample_sites_with_constraints(uint64_t* seed) const
32,844,352✔
233
{
234
  bool accepted = false;
32,844,352✔
235
  static int64_t n_reject = 0;
236
  static int64_t n_accept = 0;
237
  vector<SourceSite> sites;
32,844,352✔
238

239
  while (!accepted) {
67,019,630✔
240
    // Sample all sites from the source
241
    sites = this->sample_sites(seed);
34,175,281✔
242

243
    accepted = true;
34,175,278✔
244
    if (!constraints_applied()) {
34,175,278✔
245
      // Check whether all sampled sites satisfy constraints
246
      for (const auto& site : sites) {
2,673,649✔
247
        if (!satisfies_spatial_constraints(site.r) ||
2,002,289✔
248
            !satisfies_energy_constraints(site.E) ||
2,684,557✔
249
            !satisfies_time_constraints(site.time)) {
682,268✔
250
          accepted = false;
1,330,929✔
251
          break;
1,330,929✔
252
        }
253
      }
254

255
      if (!accepted) {
2,002,289✔
256
        ++n_reject;
1,330,929✔
257
        check_rejection_fraction(n_reject, n_accept);
1,330,929✔
258

259
        if (rejection_strategy_ == RejectionStrategy::KILL) {
1,330,929!
NEW
260
          accepted = true;
×
NEW
261
          for (auto& site : sites) {
×
NEW
262
            site.wgt = 0.0;
×
263
          }
264
        }
265
      }
266
    }
267
  }
268

269
  ++n_accept;
32,844,349✔
270
  return sites;
32,844,349✔
NEW
271
}
×
272

273
bool Source::satisfies_energy_constraints(double E) const
34,373,308✔
274
{
275
  return E > energy_bounds_.first && E < energy_bounds_.second;
34,373,308!
276
}
277

278
bool Source::satisfies_time_constraints(double time) const
682,268✔
279
{
280
  return time > time_bounds_.first && time < time_bounds_.second;
682,268✔
281
}
282

283
bool Source::satisfies_spatial_constraints(Position r) const
39,042,117✔
284
{
285
  GeometryState geom_state;
39,042,117✔
286
  geom_state.r() = r;
39,042,117✔
287
  geom_state.u() = {0.0, 0.0, 1.0};
39,042,117✔
288

289
  // Reject particle if it's not in the geometry at all
290
  bool found = exhaustive_find_cell(geom_state);
39,042,117✔
291
  if (!found)
39,042,117✔
292
    return false;
490,340✔
293

294
  // Check the geometry state against specified domains
295
  bool accepted = true;
38,551,777✔
296
  if (!domain_ids_.empty()) {
38,551,777✔
297
    if (domain_type_ == DomainType::MATERIAL) {
1,913,733!
298
      auto mat_index = geom_state.material();
×
299
      if (mat_index == MATERIAL_VOID) {
×
300
        accepted = false;
×
301
      } else {
302
        accepted = contains(domain_ids_, model::materials[mat_index]->id());
×
303
      }
304
    } else {
305
      for (int i = 0; i < geom_state.n_coord(); i++) {
3,678,046✔
306
        auto id =
307
          (domain_type_ == DomainType::CELL)
1,913,733✔
308
            ? model::cells[geom_state.coord(i).cell()].get()->id_
1,913,733!
309
            : model::universes[geom_state.coord(i).universe()].get()->id_;
×
310
        if ((accepted = contains(domain_ids_, id)))
1,913,733✔
311
          break;
149,420✔
312
      }
313
    }
314
  }
315

316
  // Check if spatial site is in fissionable material
317
  if (accepted && only_fissionable_) {
38,551,777✔
318
    // Determine material
319
    auto mat_index = geom_state.material();
1,076,199✔
320
    if (mat_index == MATERIAL_VOID) {
1,076,199!
321
      accepted = false;
×
322
    } else {
323
      accepted = model::materials[mat_index]->fissionable();
1,076,199✔
324
    }
325
  }
326

327
  return accepted;
38,551,777✔
328
}
39,042,117✔
329

330
//==============================================================================
331
// IndependentSource implementation
332
//==============================================================================
333

334
IndependentSource::IndependentSource(
2,050✔
335
  UPtrSpace space, UPtrAngle angle, UPtrDist energy, UPtrDist time)
2,050✔
336
  : space_ {std::move(space)}, angle_ {std::move(angle)},
2,050✔
337
    energy_ {std::move(energy)}, time_ {std::move(time)}
4,100✔
338
{}
2,050✔
339

340
IndependentSource::IndependentSource(pugi::xml_node node) : Source(node)
45,731✔
341
{
342
  // Check for particle type
343
  if (check_for_node(node, "particle")) {
45,731✔
344
    auto temp_str = get_node_value(node, "particle", false, true);
44,862✔
345
    particle_ = ParticleType(temp_str);
44,862✔
346
    if (particle_ == ParticleType::photon() ||
89,577✔
347
        particle_ == ParticleType::electron() ||
89,577!
348
        particle_ == ParticleType::positron()) {
44,699✔
349
      settings::photon_transport = true;
163✔
350
    }
351
  }
44,862✔
352
  validate_particle_type(particle_, "IndependentSource");
45,731✔
353

354
  // Check for external source file
355
  if (check_for_node(node, "file")) {
45,731!
356

357
  } else {
358

359
    // Spatial distribution for external source
360
    if (check_for_node(node, "space")) {
45,731✔
361
      space_ = SpatialDistribution::create(node.child("space"));
7,600✔
362
    } else {
363
      // If no spatial distribution specified, make it a point source
364
      space_ = UPtrSpace {new SpatialPoint()};
38,131✔
365
    }
366

367
    // For backwards compatibility, check for only fissionable setting on box
368
    // source
369
    auto space_box = dynamic_cast<SpatialBox*>(space_.get());
45,730!
370
    if (space_box) {
45,730✔
371
      if (!only_fissionable_) {
4,156✔
372
        only_fissionable_ = space_box->only_fissionable();
2,701✔
373
      }
374
    }
375

376
    // Determine external source angular distribution
377
    if (check_for_node(node, "angle")) {
45,730✔
378
      angle_ = UnitSphereDistribution::create(node.child("angle"));
3,356✔
379
    } else {
380
      angle_ = UPtrAngle {new Isotropic()};
42,374✔
381
    }
382

383
    // Determine external source energy distribution
384
    if (check_for_node(node, "energy")) {
45,730✔
385
      pugi::xml_node node_dist = node.child("energy");
4,762✔
386
      energy_ = distribution_from_xml(node_dist);
4,762✔
387
    } else {
388
      // Default to a Watt spectrum with parameters 0.988 MeV and 2.249 MeV^-1
389
      energy_ = UPtrDist {new Watt(0.988e6, 2.249e-6)};
40,968✔
390
    }
391

392
    // Determine external source time distribution
393
    if (check_for_node(node, "time")) {
45,730✔
394
      pugi::xml_node node_dist = node.child("time");
43✔
395
      time_ = distribution_from_xml(node_dist);
43✔
396
    } else {
397
      // Default to a Constant time T=0
398
      double T[] {0.0};
45,687✔
399
      double p[] {1.0};
45,687✔
400
      time_ = UPtrDist {new Discrete {T, p, 1}};
45,687✔
401
    }
402
  }
403
}
45,730✔
404

405
SourceSite IndependentSource::sample(uint64_t* seed) const
35,777,852✔
406
{
407
  SourceSite site;
35,777,852✔
408
  site.particle = particle_;
35,777,852✔
409
  double r_wgt = 1.0;
35,777,852✔
410
  double E_wgt = 1.0;
35,777,852✔
411

412
  // Repeat sampling source location until a good site has been accepted
413
  bool accepted = false;
35,777,852✔
414
  static int64_t n_reject = 0;
415
  static int64_t n_accept = 0;
416

417
  while (!accepted) {
72,817,677✔
418

419
    // Sample spatial distribution
420
    auto [r, r_wgt_temp] = space_->sample(seed);
37,039,828✔
421
    site.r = r;
37,039,828✔
422
    r_wgt = r_wgt_temp;
37,039,828✔
423

424
    // Check if sampled position satisfies spatial constraints
425
    accepted = satisfies_spatial_constraints(site.r);
37,039,828✔
426

427
    // Check for rejection
428
    if (!accepted) {
37,039,828✔
429
      ++n_reject;
1,261,979✔
430
      check_rejection_fraction(n_reject, n_accept);
1,261,979✔
431
    }
432
  }
433

434
  // Sample angle
435
  auto [u, u_wgt] = angle_->sample(seed);
35,777,849✔
436
  site.u = u;
35,777,849✔
437

438
  site.wgt = r_wgt * u_wgt;
35,777,849✔
439

440
  // Sample energy and time for neutron and photon sources
441
  if (settings::solver_type != SolverType::RANDOM_RAY) {
35,777,849✔
442
    // Check for monoenergetic source above maximum particle energy
443
    auto p = particle_.transport_index();
33,669,529✔
444
    auto energy_ptr = dynamic_cast<Discrete*>(energy_.get());
33,669,529!
445
    if (energy_ptr) {
33,669,529✔
446
      auto energies = xt::adapt(energy_ptr->x());
18,123,421✔
447
      if (xt::any(energies > data::energy_max[p])) {
18,123,421!
448
        fatal_error("Source energy above range of energies of at least "
×
449
                    "one cross section table");
450
      }
451
    }
18,123,421✔
452

453
    while (true) {
454
      // Sample energy spectrum
455
      auto [E, E_wgt_temp] = energy_->sample(seed);
33,669,529✔
456
      site.E = E;
33,669,529✔
457
      E_wgt = E_wgt_temp;
33,669,529✔
458

459
      // Resample if energy falls above maximum particle energy
460
      if (site.E < data::energy_max[p] &&
67,339,058!
461
          (satisfies_energy_constraints(site.E)))
33,669,529!
462
        break;
33,669,529✔
463

464
      n_reject++;
×
465
      check_rejection_fraction(n_reject, n_accept);
×
466
    }
×
467

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

472
    site.wgt *= (E_wgt * time_wgt);
33,669,529✔
473
  }
474

475
  // Increment number of accepted samples
476
  ++n_accept;
35,777,849✔
477

478
  return site;
71,555,698✔
479
}
480

481
//==============================================================================
482
// FileSource implementation
483
//==============================================================================
484

485
FileSource::FileSource(pugi::xml_node node) : Source(node)
63✔
486
{
487
  auto path = get_node_value(node, "file", false, true);
63✔
488
  load_sites_from_file(path);
63✔
489
}
54✔
490

491
FileSource::FileSource(const std::string& path)
32✔
492
{
493
  load_sites_from_file(path);
32✔
494
}
32✔
495

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

507
    write_message(6, "Reading source file from {}...", path);
63✔
508

509
    // Open the binary file
510
    hid_t file_id = file_open(path, 'r', true);
63✔
511

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

519
    // Read in the source particles
520
    read_source_bank(file_id, sites_, false);
63✔
521

522
    // Close file
523
    file_close(file_id);
54✔
524
  }
54✔
525

526
  // Make sure particles in source file have valid types
527
  for (const auto& site : this->sites_) {
174,097✔
528
    validate_particle_type(site.particle, "FileSource");
174,011✔
529
  }
530
}
86✔
531

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

539
//==============================================================================
540
// CompiledSourceWrapper implementation
541
//==============================================================================
542

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

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

564
  // reset errors
565
  dlerror();
32✔
566

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

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

580
  // create a pointer to an instance of the custom source
581
  compiled_source_ = create_compiled_source(parameters);
32✔
582

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

589
CompiledSourceWrapper::~CompiledSourceWrapper()
64✔
590
{
591
  // Make sure custom source is cleared before closing shared library
592
  if (compiled_source_.get())
32!
593
    compiled_source_.reset();
32✔
594

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

603
//==============================================================================
604
// MeshElementSpatial implementation
605
//==============================================================================
606

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

612
//==============================================================================
613
// MeshSource implementation
614
//==============================================================================
615

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

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

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

643
  // Make sure sources use valid particle types
644
  for (const auto& src : sources_) {
37,653✔
645
    validate_particle_type(src->particle_type(), "MeshSource");
37,452✔
646
  }
647

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

656
  space_ = std::make_unique<MeshSpatial>(mesh_idx, strengths);
201✔
657
}
201✔
658

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

664
  // Sample the distribution for the specific mesh element; note that the
665
  // spatial distribution has been set for each element using MeshElementSpatial
666
  return source(element)->sample_with_constraints(seed);
1,496,540✔
667
}
668

669
//==============================================================================
670
// CoincidentSource implementation
671
//==============================================================================
672

NEW
673
CoincidentSource::CoincidentSource(pugi::xml_node node) : Source(node)
×
674
{
675
  // Coincident sources are only valid for fixed-source simulations
NEW
676
  if (settings::run_mode == RunMode::EIGENVALUE) {
×
NEW
677
    fatal_error("Coincident sources cannot be used in eigenvalue mode.");
×
678
  }
679

680
  // Read shared spatial distribution
NEW
681
  if (check_for_node(node, "space")) {
×
NEW
682
    space_ = SpatialDistribution::create(node.child("space"));
×
683
  } else {
NEW
684
    space_ = UPtrSpace {new SpatialPoint()};
×
685
  }
686

687
  // Read shared time distribution
NEW
688
  if (check_for_node(node, "time")) {
×
NEW
689
    pugi::xml_node node_dist = node.child("time");
×
NEW
690
    time_ = distribution_from_xml(node_dist);
×
691
  } else {
NEW
692
    double T[] {0.0};
×
NEW
693
    double p[] {1.0};
×
NEW
694
    time_ = UPtrDist {new Discrete {T, p, 1}};
×
695
  }
696

697
  // Read child <source> elements as sub-sources
NEW
698
  for (auto source_node : node.children("source")) {
×
699
    // Read emission probability (default 1.0)
NEW
700
    double prob = 1.0;
×
NEW
701
    if (source_node.attribute("probability")) {
×
NEW
702
      prob = std::stod(source_node.attribute("probability").value());
×
NEW
703
      if (prob <= 0.0 || prob > 1.0) {
×
NEW
704
        fatal_error("Sub-source probability must be in (0, 1].");
×
705
      }
706
    }
707

NEW
708
    auto src = Source::create(source_node);
×
NEW
709
    if (auto ptr = dynamic_cast<IndependentSource*>(src.get())) {
×
NEW
710
      sources_.emplace_back(std::move(ptr));
×
711
    } else {
NEW
712
      fatal_error(
×
713
        "Sub-sources of a coincident source must be IndependentSource.");
714
    }
NEW
715
    probabilities_.push_back(prob);
×
NEW
716
  }
×
717

718
  // Validate at least 2 sub-sources
NEW
719
  if (sources_.size() < 2) {
×
NEW
720
    fatal_error("A coincident source must have at least 2 sub-sources.");
×
721
  }
722

723
  // Precompute P(>=1 emission) and the "first success" CDF for direct
724
  // conditional sampling when all probabilities are < 1.
NEW
725
  double prod_complement = 1.0;
×
NEW
726
  for (double p : probabilities_)
×
NEW
727
    prod_complement *= (1.0 - p);
×
NEW
728
  prob_at_least_one_ = 1.0 - prod_complement;
×
729

730
  // Build CDF: q_i = p_i * prod_{j<i}(1 - p_j)
NEW
731
  first_success_cdf_.resize(sources_.size());
×
NEW
732
  double cumulative = 0.0;
×
NEW
733
  double running_complement = 1.0;
×
NEW
734
  for (size_t i = 0; i < sources_.size(); ++i) {
×
NEW
735
    cumulative += probabilities_[i] * running_complement;
×
NEW
736
    first_success_cdf_[i] = cumulative / prob_at_least_one_;
×
NEW
737
    running_complement *= (1.0 - probabilities_[i]);
×
738
  }
739

740
  // Scaling the source by P(>=1 emission) to sample only interesting histories
NEW
741
  strength_ *= prob_at_least_one_;
×
NEW
742
}
×
743

NEW
744
SourceSite CoincidentSource::sample(uint64_t* seed) const
×
745
{
NEW
746
  auto sites = sample_sites(seed);
×
NEW
747
  return sites[0];
×
NEW
748
}
×
749

NEW
750
vector<SourceSite> CoincidentSource::sample_sites(uint64_t* seed) const
×
751
{
752
  // Sample shared position once
NEW
753
  auto [r, r_wgt] = space_->sample(seed);
×
754

755
  // Sample shared time once
NEW
756
  auto [time, time_wgt] = time_->sample(seed);
×
757

758
  // Build a site for each sub-source, applying emission probabilities.
759
  // To guarantee at least one particle, we use direct conditional sampling:
760
  // pick a guaranteed "first success" source, then roll the rest normally.
NEW
761
  vector<SourceSite> sites;
×
NEW
762
  sites.reserve(sources_.size());
×
763

764
  // Determine the guaranteed source via the first-success CDF
NEW
765
  size_t guaranteed = 0;
×
NEW
766
  if (prob_at_least_one_ < 1.0) {
×
NEW
767
    double xi = prn(seed);
×
NEW
768
    for (size_t i = 0; i < sources_.size(); ++i) {
×
NEW
769
      if (xi < first_success_cdf_[i]) {
×
NEW
770
        guaranteed = i;
×
NEW
771
        break;
×
772
      }
773
    }
774
  }
775

NEW
776
  for (size_t i = guaranteed; i < sources_.size(); ++i) {
×
NEW
777
    if (prob_at_least_one_ < 1.0) {
×
778
      // Sources after the guaranteed one roll normally
NEW
779
      if (i > guaranteed && prn(seed) >= probabilities_[i])
×
NEW
780
        continue;
×
781
    } else {
782
      // At least one source has probability 1, so roll all normally
NEW
783
      if (prn(seed) >= probabilities_[i])
×
NEW
784
        continue;
×
785
    }
786

787
    // Sample from the sub-source to get particle type, energy, angle
NEW
788
    SourceSite site = sources_[i]->sample(seed);
×
789

790
    // Override position and time with shared values
NEW
791
    site.r = r;
×
NEW
792
    site.time = time;
×
793

794
    // Apply shared spatial and time weights
NEW
795
    site.wgt *= (r_wgt * time_wgt);
×
796

NEW
797
    sites.push_back(site);
×
798
  }
799

NEW
800
  return sites;
×
NEW
801
}
×
802

803
//==============================================================================
804
// Non-member functions
805
//==============================================================================
806

807
void initialize_source()
3,964✔
808
{
809
  write_message("Initializing source particles...", 5);
3,964✔
810

811
// Generation source sites from specified distribution in user input
812
#pragma omp parallel for
813
  for (int64_t i = 0; i < simulation::work_per_rank; ++i) {
1,200,028✔
814
    // initialize random number seed
815
    int64_t id = simulation::total_gen * settings::n_particles +
2,396,530✔
816
                 simulation::work_index[mpi::rank] + i + 1;
1,198,265✔
817
    uint64_t seed = init_seed(id, STREAM_SOURCE);
1,198,265✔
818

819
    // sample external source distribution (store only primary site)
820
    auto sites = sample_external_source(&seed);
1,198,265✔
821
    simulation::source_bank[i] = sites[0];
1,198,265✔
822
  }
1,198,265✔
823

824
  // Write out initial source
825
  if (settings::write_initial_source) {
3,964!
826
    write_message("Writing out initial source...", 5);
×
827
    std::string filename = settings::path_output + "initial_source.h5";
×
828
    hid_t file_id = file_open(filename, 'w', true);
×
829
    write_source_bank(file_id, simulation::source_bank, simulation::work_index);
×
830
    file_close(file_id);
×
831
  }
×
832
}
3,964✔
833

834
vector<SourceSite> sample_external_source(uint64_t* seed)
32,844,352✔
835
{
836
  // Sample from among multiple source distributions
837
  int i = 0;
32,844,352✔
838
  int n_sources = model::external_sources.size();
32,844,352✔
839
  if (n_sources > 1) {
32,844,352✔
840
    if (settings::uniform_source_sampling) {
3,558,300✔
841
      i = prn(seed) * n_sources;
2,200✔
842
    } else {
843
      i = model::external_sources_probability.sample(seed);
3,556,100✔
844
    }
845
  }
846

847
  // Sample source sites from i-th source distribution
848
  vector<SourceSite> sites {
849
    model::external_sources[i]->sample_sites_with_constraints(seed)};
32,844,352✔
850

851
  // For uniform source sampling, multiply the weight by the ratio of the actual
852
  // probability of sampling source i to the biased probability of sampling
853
  // source i, which is (strength_i / total_strength) / (1 / n)
854
  if (n_sources > 1 && settings::uniform_source_sampling) {
32,844,349✔
855
    double total_strength = model::external_sources_probability.integral();
2,200✔
856
    double wgt_factor =
857
      model::external_sources[i]->strength() * n_sources / total_strength;
2,200✔
858
    for (auto& site : sites) {
4,400✔
859
      site.wgt *= wgt_factor;
2,200✔
860
    }
861
  }
862

863
  // If running in MG, convert site.E to group
864
  if (!settings::run_CE) {
32,844,349✔
865
    for (auto& site : sites) {
3,484,800✔
866
      site.E = lower_bound_index(data::mg.rev_energy_bins_.begin(),
1,742,400✔
867
        data::mg.rev_energy_bins_.end(), site.E);
1,742,400✔
868
      site.E = data::mg.num_energy_groups_ - site.E - 1.;
1,742,400✔
869
    }
870
  }
871

872
  return sites;
32,844,349✔
UNCOV
873
}
×
874

875
void free_memory_source()
8,382✔
876
{
877
  model::external_sources.clear();
8,382✔
878
}
8,382✔
879

880
//==============================================================================
881
// C API
882
//==============================================================================
883

884
extern "C" int openmc_sample_external_source(
955✔
885
  size_t n, uint64_t* seed, void* sites)
886
{
887
  if (!sites || !seed) {
955!
888
    set_errmsg("Received null pointer.");
×
889
    return OPENMC_E_INVALID_ARGUMENT;
×
890
  }
891

892
  if (model::external_sources.empty()) {
955!
893
    set_errmsg("No external sources have been defined.");
×
894
    return OPENMC_E_OUT_OF_BOUNDS;
×
895
  }
896

897
  auto sites_array = static_cast<SourceSite*>(sites);
955✔
898
  for (size_t i = 0; i < n; ++i) {
2,957,779✔
899
    auto sampled = sample_external_source(seed);
2,956,824✔
900
    sites_array[i] = sampled[0];
2,956,824✔
901
  }
2,956,824✔
902
  return 0;
955✔
903
}
904

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