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

openmc-dev / openmc / 22596289416

02 Mar 2026 09:22PM UTC coverage: 80.951% (-0.6%) from 81.508%
22596289416

Pull #3757

github

web-flow
Merge d60b97b21 into 823b4c96c
Pull Request #3757: Testing point detectors

17543 of 25514 branches covered (68.76%)

Branch coverage included in aggregate %.

117 of 510 new or added lines in 26 files covered. (22.94%)

22 existing lines in 3 files now uncovered.

57759 of 67508 relevant lines covered (85.56%)

44598697.25 hits per line

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

81.1
/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
namespace {
42

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

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

53
} // namespace
54

55
//==============================================================================
56
// Global variables
57
//==============================================================================
58

59
namespace model {
60

61
vector<unique_ptr<Source>> external_sources;
62

63
DiscreteIndex external_sources_probability;
64

65
} // namespace model
66

67
//==============================================================================
68
// Source implementation
69
//==============================================================================
70

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

81
  // Check for additional defined constraints
82
  read_constraints(node);
45,834✔
83
}
45,834✔
84

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

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

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

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

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

157
  if (check_for_node(node, "fissionable")) {
45,834✔
158
    only_fissionable_ = get_node_value_bool(node, "fissionable");
1,408✔
159
  }
160

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

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

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

192
SourceSite Source::sample_with_constraints(uint64_t* seed) const
34,380,257✔
193
{
194
  bool accepted = false;
34,380,257✔
195
  static int64_t n_reject = 0;
34,380,257✔
196
  static int64_t n_accept = 0;
34,380,257✔
197
  SourceSite site;
34,380,257✔
198

199
  while (!accepted) {
36,398,632✔
200
    // Sample a source site without considering constraints yet
201
    site = this->sample(seed);
35,727,272✔
202

203
    if (constraints_applied()) {
35,727,268✔
204
      accepted = true;
205
    } else {
206
      // Check whether sampled site satisfies constraints
207
      accepted = satisfies_spatial_constraints(site.r) &&
2,722,761✔
208
                 satisfies_energy_constraints(site.E) &&
2,700,697✔
209
                 satisfies_time_constraints(site.time);
682,322✔
210
      if (!accepted) {
1,347,015✔
211
        // Increment number of rejections and check against minimum fraction
212
        ++n_reject;
1,347,015✔
213
        check_rejection_fraction(n_reject, n_accept);
1,347,015✔
214

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

225
  // Increment number of accepted samples
226
  ++n_accept;
34,380,253✔
227

228
  return site;
34,380,253✔
229
}
230

231
bool Source::satisfies_energy_constraints(double E) const
34,413,279✔
232
{
233
  return E > energy_bounds_.first && E < energy_bounds_.second;
34,413,279!
234
}
235

236
bool Source::satisfies_time_constraints(double time) const
682,322✔
237
{
238
  return time > time_bounds_.first && time < time_bounds_.second;
682,322✔
239
}
240

241
bool Source::satisfies_spatial_constraints(Position r) const
39,170,409✔
242
{
243
  GeometryState geom_state;
39,170,409✔
244
  geom_state.r() = r;
39,170,409✔
245
  geom_state.u() = {0.0, 0.0, 1.0};
39,170,409✔
246

247
  // Reject particle if it's not in the geometry at all
248
  bool found = exhaustive_find_cell(geom_state);
39,170,409✔
249
  if (!found)
39,170,409✔
250
    return false;
251

252
  // Check the geometry state against specified domains
253
  bool accepted = true;
38,680,069✔
254
  if (!domain_ids_.empty()) {
38,680,069✔
255
    if (domain_type_ == DomainType::MATERIAL) {
2,003,194!
256
      auto mat_index = geom_state.material();
×
257
      if (mat_index == MATERIAL_VOID) {
×
258
        accepted = false;
259
      } else {
260
        accepted = contains(domain_ids_, model::materials[mat_index]->id());
×
261
      }
262
    } else {
263
      for (int i = 0; i < geom_state.n_coord(); i++) {
3,855,828✔
264
        auto id =
2,003,194✔
265
          (domain_type_ == DomainType::CELL)
266
            ? model::cells[geom_state.coord(i).cell()].get()->id_
2,003,194!
267
            : model::universes[geom_state.coord(i).universe()].get()->id_;
×
268
        if ((accepted = contains(domain_ids_, id)))
4,006,388✔
269
          break;
270
      }
271
    }
272
  }
273

274
  // Check if spatial site is in fissionable material
275
  if (accepted && only_fissionable_) {
38,680,069✔
276
    // Determine material
277
    auto mat_index = geom_state.material();
1,076,199✔
278
    if (mat_index == MATERIAL_VOID) {
1,076,199!
279
      accepted = false;
280
    } else {
281
      accepted = model::materials[mat_index]->fissionable();
1,076,199✔
282
    }
283
  }
284

285
  return accepted;
286
}
39,170,409✔
287

288
//==============================================================================
289
// IndependentSource implementation
290
//==============================================================================
291

292
IndependentSource::IndependentSource(
2,037✔
293
  UPtrSpace space, UPtrAngle angle, UPtrDist energy, UPtrDist time)
2,037✔
294
  : space_ {std::move(space)}, angle_ {std::move(angle)},
2,037✔
295
    energy_ {std::move(energy)}, time_ {std::move(time)}
2,037✔
296
{}
2,037✔
297

298
IndependentSource::IndependentSource(pugi::xml_node node) : Source(node)
45,542✔
299
{
300
  // Check for particle type
301
  if (check_for_node(node, "particle")) {
45,542✔
302
    auto temp_str = get_node_value(node, "particle", false, true);
44,717✔
303
    particle_ = ParticleType(temp_str);
44,717✔
304
    if (particle_ == ParticleType::photon() ||
44,717✔
305
        particle_ == ParticleType::electron() ||
44,717✔
306
        particle_ == ParticleType::positron()) {
44,558!
307
      settings::photon_transport = true;
159✔
308
    }
309
  }
44,717✔
310
  validate_particle_type(particle_, "IndependentSource");
45,542✔
311

312
  // Check for external source file
313
  if (check_for_node(node, "file")) {
45,542!
314

315
  } else {
316

317
    // Spatial distribution for external source
318
    if (check_for_node(node, "space")) {
45,542✔
319
      space_ = SpatialDistribution::create(node.child("space"));
7,439✔
320
    } else {
321
      // If no spatial distribution specified, make it a point source
322
      space_ = UPtrSpace {new SpatialPoint()};
38,103✔
323
    }
324

325
    // For backwards compatibility, check for only fissionable setting on box
326
    // source
327
    auto space_box = dynamic_cast<SpatialBox*>(space_.get());
45,541!
328
    if (space_box) {
45,541✔
329
      if (!only_fissionable_) {
4,011✔
330
        only_fissionable_ = space_box->only_fissionable();
2,603✔
331
      }
332
    }
333

334
    // Determine external source angular distribution
335
    if (check_for_node(node, "angle")) {
45,541✔
336
      angle_ = UnitSphereDistribution::create(node.child("angle"));
3,358✔
337
    } else {
338
      angle_ = UPtrAngle {new Isotropic()};
42,183✔
339
    }
340

341
    // Determine external source energy distribution
342
    if (check_for_node(node, "energy")) {
45,541✔
343
      pugi::xml_node node_dist = node.child("energy");
4,748✔
344
      energy_ = distribution_from_xml(node_dist);
4,748✔
345
    } else {
346
      // Default to a Watt spectrum with parameters 0.988 MeV and 2.249 MeV^-1
347
      energy_ = UPtrDist {new Watt(0.988e6, 2.249e-6)};
40,793✔
348
    }
349

350
    // Determine external source time distribution
351
    if (check_for_node(node, "time")) {
45,541✔
352
      pugi::xml_node node_dist = node.child("time");
41✔
353
      time_ = distribution_from_xml(node_dist);
41✔
354
    } else {
355
      // Default to a Constant time T=0
356
      double T[] {0.0};
45,500✔
357
      double p[] {1.0};
45,500✔
358
      time_ = UPtrDist {new Discrete {T, p, 1}};
45,500✔
359
    }
360
  }
361
}
45,541✔
362

363
SourceSite IndependentSource::sample(uint64_t* seed) const
35,817,217✔
364
{
365
  SourceSite site;
35,817,217✔
366
  site.particle = particle_;
35,817,217✔
367
  double r_wgt = 1.0;
35,817,217✔
368
  double E_wgt = 1.0;
35,817,217✔
369

370
  // Repeat sampling source location until a good site has been accepted
371
  bool accepted = false;
35,817,217✔
372
  static int64_t n_reject = 0;
35,817,217✔
373
  static int64_t n_accept = 0;
35,817,217✔
374

375
  while (!accepted) {
72,969,247✔
376

377
    // Sample spatial distribution
378
    auto [r, r_wgt_temp] = space_->sample(seed);
37,152,034✔
379
    site.r = r;
37,152,034✔
380
    r_wgt = r_wgt_temp;
37,152,034✔
381

382
    // Check if sampled position satisfies spatial constraints
383
    accepted = satisfies_spatial_constraints(site.r);
37,152,034✔
384

385
    // Check for rejection
386
    if (!accepted) {
37,152,034✔
387
      ++n_reject;
1,334,821✔
388
      check_rejection_fraction(n_reject, n_accept);
1,334,821✔
389
    }
390
  }
391

392
  // Sample angle
393
  auto [u, u_wgt] = angle_->sample(seed);
35,817,213✔
394
  site.u = u;
35,817,213✔
395

396
  site.wgt = r_wgt * u_wgt;
35,817,213✔
397

398
  // Sample energy and time for neutron and photon sources
399
  if (settings::solver_type != SolverType::RANDOM_RAY) {
35,817,213✔
400
    // Check for monoenergetic source above maximum particle energy
401
    auto p = particle_.transport_index();
33,708,893✔
402
    auto energy_ptr = dynamic_cast<Discrete*>(energy_.get());
33,708,893!
403
    if (energy_ptr) {
33,708,893✔
404
      auto energies =
18,134,771✔
405
        tensor::Tensor<double>(energy_ptr->x().data(), energy_ptr->x().size());
18,134,771✔
406
      if ((energies > data::energy_max[p]).any()) {
54,404,313!
407
        fatal_error("Source energy above range of energies of at least "
×
408
                    "one cross section table");
409
      }
410
    }
18,134,771✔
411

412
    while (true) {
33,708,893✔
413
      // Sample energy spectrum
414
      auto [E, E_wgt_temp] = energy_->sample(seed);
33,708,893✔
415
      site.E = E;
33,708,893✔
416
      E_wgt = E_wgt_temp;
33,708,893✔
417

418
      // Resample if energy falls above maximum particle energy
419
      if (site.E < data::energy_max[p] &&
67,417,786!
420
          (satisfies_energy_constraints(site.E)))
33,708,893✔
421
        break;
422

423
      n_reject++;
×
424
      check_rejection_fraction(n_reject, n_accept);
×
425
    }
×
426

427
    // Sample particle creation time
428
    auto [time, time_wgt] = time_->sample(seed);
33,708,893✔
429
    site.time = time;
33,708,893✔
430

431
    site.wgt *= (E_wgt * time_wgt);
33,708,893✔
432
  }
433

434
  // Increment number of accepted samples
435
  ++n_accept;
35,817,213✔
436

437
  return site;
35,817,213✔
438
}
439

440
//==============================================================================
441
// FileSource implementation
442
//==============================================================================
443

444
FileSource::FileSource(pugi::xml_node node) : Source(node)
61✔
445
{
446
  auto path = get_node_value(node, "file", false, true);
61✔
447
  load_sites_from_file(path);
61✔
448
}
52✔
449

450
FileSource::FileSource(const std::string& path)
30✔
451
{
452
  load_sites_from_file(path);
30✔
453
}
30✔
454

455
void FileSource::load_sites_from_file(const std::string& path)
91✔
456
{
457
  // If MCPL file, use the dedicated file reader
458
  if (ends_with(path, ".mcpl") || ends_with(path, ".mcpl.gz")) {
152!
459
    sites_ = mcpl_source_sites(path);
30✔
460
  } else {
461
    // Check if source file exists
462
    if (!file_exists(path)) {
61!
463
      fatal_error(fmt::format("Source file '{}' does not exist.", path));
×
464
    }
465

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

468
    // Open the binary file
469
    hid_t file_id = file_open(path, 'r', true);
61✔
470

471
    // Check to make sure this is a source file
472
    std::string filetype;
61✔
473
    read_attribute(file_id, "filetype", filetype);
61✔
474
    if (filetype != "source" && filetype != "statepoint") {
61!
475
      fatal_error("Specified starting source file not a source file type.");
×
476
    }
477

478
    // Read in the source particles
479
    read_source_bank(file_id, sites_, false);
61✔
480

481
    // Close file
482
    file_close(file_id);
52✔
483
  }
52✔
484

485
  // Make sure particles in source file have valid types
486
  for (const auto& site : this->sites_) {
170,093✔
487
    validate_particle_type(site.particle, "FileSource");
340,022✔
488
  }
489
}
82✔
490

491
SourceSite FileSource::sample(uint64_t* seed) const
286,356✔
492
{
493
  // Sample a particle randomly from list
494
  size_t i_site = sites_.size() * prn(seed);
286,356✔
495
  return sites_[i_site];
286,356✔
496
}
497

498
//==============================================================================
499
// CompiledSourceWrapper implementation
500
//==============================================================================
501

502
CompiledSourceWrapper::CompiledSourceWrapper(pugi::xml_node node) : Source(node)
30✔
503
{
504
  // Get shared library path and parameters
505
  auto path = get_node_value(node, "library", false, true);
30✔
506
  std::string parameters;
30✔
507
  if (check_for_node(node, "parameters")) {
30✔
508
    parameters = get_node_value(node, "parameters", false, true);
15✔
509
  }
510
  setup(path, parameters);
30✔
511
}
30✔
512

513
void CompiledSourceWrapper::setup(
30✔
514
  const std::string& path, const std::string& parameters)
515
{
516
#ifdef HAS_DYNAMIC_LINKING
517
  // Open the library
518
  shared_library_ = dlopen(path.c_str(), RTLD_LAZY);
30✔
519
  if (!shared_library_) {
30!
520
    fatal_error("Couldn't open source library " + path);
×
521
  }
522

523
  // reset errors
524
  dlerror();
30✔
525

526
  // get the function to create the custom source from the library
527
  auto create_compiled_source = reinterpret_cast<create_compiled_source_t*>(
30✔
528
    dlsym(shared_library_, "openmc_create_source"));
30✔
529

530
  // check for any dlsym errors
531
  auto dlsym_error = dlerror();
30✔
532
  if (dlsym_error) {
30!
533
    std::string error_msg = fmt::format(
×
534
      "Couldn't open the openmc_create_source symbol: {}", dlsym_error);
×
535
    dlclose(shared_library_);
×
536
    fatal_error(error_msg);
×
537
  }
×
538

539
  // create a pointer to an instance of the custom source
540
  compiled_source_ = create_compiled_source(parameters);
30✔
541

542
#else
543
  fatal_error("Custom source libraries have not yet been implemented for "
544
              "non-POSIX systems");
545
#endif
546
}
30✔
547

548
CompiledSourceWrapper::~CompiledSourceWrapper()
60✔
549
{
550
  // Make sure custom source is cleared before closing shared library
551
  if (compiled_source_.get())
30!
552
    compiled_source_.reset();
30✔
553

554
#ifdef HAS_DYNAMIC_LINKING
555
  dlclose(shared_library_);
30✔
556
#else
557
  fatal_error("Custom source libraries have not yet been implemented for "
558
              "non-POSIX systems");
559
#endif
560
}
60✔
561

562
//==============================================================================
563
// MeshElementSpatial implementation
564
//==============================================================================
565

566
std::pair<Position, double> MeshElementSpatial::sample(uint64_t* seed) const
1,525,111✔
567
{
568
  return {model::meshes[mesh_index_]->sample_element(elem_index_, seed), 1.0};
1,525,111✔
569
}
570

571
//==============================================================================
572
// MeshSource implementation
573
//==============================================================================
574

575
MeshSource::MeshSource(pugi::xml_node node) : Source(node)
201✔
576
{
577
  int32_t mesh_id = stoi(get_node_value(node, "mesh"));
402✔
578
  int32_t mesh_idx = model::mesh_map.at(mesh_id);
201✔
579
  const auto& mesh = model::meshes[mesh_idx];
201✔
580

581
  std::vector<double> strengths;
201✔
582
  // read all source distributions and populate strengths vector for MeshSpatial
583
  // object
584
  for (auto source_node : node.children("source")) {
37,653✔
585
    auto src = Source::create(source_node);
37,452✔
586
    if (auto ptr = dynamic_cast<IndependentSource*>(src.get())) {
37,452!
587
      src.release();
37,452✔
588
      sources_.emplace_back(ptr);
37,452✔
589
    } else {
590
      fatal_error(
×
591
        "The source assigned to each element must be an IndependentSource.");
592
    }
593
    strengths.push_back(sources_.back()->strength());
37,452✔
594
  }
37,452✔
595

596
  // Set spatial distributions for each mesh element
597
  for (int elem_index = 0; elem_index < sources_.size(); ++elem_index) {
37,653✔
598
    sources_[elem_index]->set_space(
37,452✔
599
      std::make_unique<MeshElementSpatial>(mesh_idx, elem_index));
74,904✔
600
  }
601

602
  // Make sure sources use valid particle types
603
  for (const auto& src : sources_) {
37,653✔
604
    validate_particle_type(src->particle_type(), "MeshSource");
74,904✔
605
  }
606

607
  // the number of source distributions should either be one or equal to the
608
  // number of mesh elements
609
  if (sources_.size() > 1 && sources_.size() != mesh->n_bins()) {
201!
610
    fatal_error(fmt::format("Incorrect number of source distributions ({}) for "
×
611
                            "mesh source with {} elements.",
612
      sources_.size(), mesh->n_bins()));
×
613
  }
614

615
  space_ = std::make_unique<MeshSpatial>(mesh_idx, strengths);
201✔
616
}
201✔
617

618
SourceSite MeshSource::sample(uint64_t* seed) const
1,512,019✔
619
{
620
  // Sample a mesh element based on the relative strengths
621
  int32_t element = space_->sample_element_index(seed);
1,512,019✔
622

623
  // Sample the distribution for the specific mesh element; note that the
624
  // spatial distribution has been set for each element using MeshElementSpatial
625
  return source(element)->sample_with_constraints(seed);
3,024,038!
626
}
627

628
//==============================================================================
629
// Non-member functions
630
//==============================================================================
631

632
void initialize_source()
3,840✔
633
{
634
  write_message("Initializing source particles...", 5);
3,840✔
635

636
// Generation source sites from specified distribution in user input
637
#pragma omp parallel for
2,210✔
638
  for (int64_t i = 0; i < simulation::work_per_rank; ++i) {
1,205,295✔
639
    // initialize random number seed
640
    int64_t id = simulation::total_gen * settings::n_particles +
1,203,665✔
641
                 simulation::work_index[mpi::rank] + i + 1;
1,203,665✔
642
    uint64_t seed = init_seed(id, STREAM_SOURCE);
1,203,665✔
643

644
    // sample external source distribution
645
    simulation::source_bank[i] = sample_external_source(&seed);
1,203,665✔
646
  }
647

648
  // Write out initial source
649
  if (settings::write_initial_source) {
3,840!
650
    write_message("Writing out initial source...", 5);
×
651
    std::string filename = settings::path_output + "initial_source.h5";
×
652
    hid_t file_id = file_open(filename, 'w', true);
×
653
    write_source_bank(file_id, simulation::source_bank, simulation::work_index);
×
654
    file_close(file_id);
×
655
  }
×
656
}
3,840✔
657

658
SourceSite sample_external_source(uint64_t* seed)
32,868,238✔
659
{
660
  // Sample from among multiple source distributions
661
  int i = 0;
32,868,238✔
662
  int n_sources = model::external_sources.size();
32,868,238✔
663
  if (n_sources > 1) {
32,868,238✔
664
    if (settings::uniform_source_sampling) {
3,558,300✔
665
      i = prn(seed) * n_sources;
2,200✔
666
    } else {
667
      i = model::external_sources_probability.sample(seed);
3,556,100✔
668
    }
669
  }
670

671
  // Sample source site from i-th source distribution
672
  SourceSite site {model::external_sources[i]->sample_with_constraints(seed)};
32,868,238✔
673

674
  // For uniform source sampling, multiply the weight by the ratio of the actual
675
  // probability of sampling source i to the biased probability of sampling
676
  // source i, which is (strength_i / total_strength) / (1 / n)
677
  if (n_sources > 1 && settings::uniform_source_sampling) {
32,868,234✔
678
    double total_strength = model::external_sources_probability.integral();
2,200✔
679
    site.wgt *=
4,400✔
680
      model::external_sources[i]->strength() * n_sources / total_strength;
2,200✔
681
  }
682

683
  // If running in MG, convert site.E to group
684
  if (!settings::run_CE) {
32,868,234✔
685
    site.E = lower_bound_index(data::mg.rev_energy_bins_.begin(),
1,742,400✔
686
      data::mg.rev_energy_bins_.end(), site.E);
687
    site.E = data::mg.num_energy_groups_ - site.E - 1.;
1,742,400✔
688
  }
689

690
  if (!model::active_point_tallies.empty()) {
32,868,234!
NEW
691
    score_point_tally(site, i);
×
692
  }
693

694
  return site;
32,868,234✔
695
}
696

697
void free_memory_source()
8,206✔
698
{
699
  model::external_sources.clear();
8,206✔
700
}
8,206✔
701

702
//==============================================================================
703
// C API
704
//==============================================================================
705

706
extern "C" int openmc_sample_external_source(
955✔
707
  size_t n, uint64_t* seed, void* sites)
708
{
709
  if (!sites || !seed) {
955!
710
    set_errmsg("Received null pointer.");
×
711
    return OPENMC_E_INVALID_ARGUMENT;
×
712
  }
713

714
  if (model::external_sources.empty()) {
955!
715
    set_errmsg("No external sources have been defined.");
×
716
    return OPENMC_E_OUT_OF_BOUNDS;
×
717
  }
718

719
  auto sites_array = static_cast<SourceSite*>(sites);
720
  for (size_t i = 0; i < n; ++i) {
2,957,779✔
721
    sites_array[i] = sample_external_source(seed);
2,956,824✔
722
  }
723
  return 0;
724
}
725

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