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

openmc-dev / openmc / 21489137916

29 Jan 2026 05:58PM UTC coverage: 81.248% (-0.7%) from 81.953%
21489137916

Pull #3757

github

web-flow
Merge 9d16542dd into f7a734189
Pull Request #3757: Testing point detectors

17267 of 24417 branches covered (70.72%)

Branch coverage included in aggregate %.

94 of 512 new or added lines in 26 files covered. (18.36%)

3 existing lines in 2 files now uncovered.

55822 of 65541 relevant lines covered (85.17%)

43735001.71 hits per line

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

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

39
namespace openmc {
40

41
//==============================================================================
42
// Global variables
43
//==============================================================================
44

45
namespace model {
46

47
vector<unique_ptr<Source>> external_sources;
48

49
DiscreteIndex external_sources_probability;
50

51
} // namespace model
52

53
//==============================================================================
54
// Source implementation
55
//==============================================================================
56

57
Source::Source(pugi::xml_node node)
45,706✔
58
{
59
  // Check for source strength
60
  if (check_for_node(node, "strength")) {
45,706✔
61
    strength_ = std::stod(get_node_value(node, "strength"));
45,018✔
62
    if (strength_ < 0.0) {
45,018!
63
      fatal_error("Source strength is negative.");
×
64
    }
65
  }
66

67
  // Check for additional defined constraints
68
  read_constraints(node);
45,706✔
69
}
45,706✔
70

71
unique_ptr<Source> Source::create(pugi::xml_node node)
45,706✔
72
{
73
  // if the source type is present, use it to determine the type
74
  // of object to create
75
  if (check_for_node(node, "type")) {
45,706✔
76
    std::string source_type = get_node_value(node, "type");
44,805✔
77
    if (source_type == "independent") {
44,805✔
78
      return make_unique<IndependentSource>(node);
44,541✔
79
    } else if (source_type == "file") {
264✔
80
      return make_unique<FileSource>(node);
31✔
81
    } else if (source_type == "compiled") {
233✔
82
      return make_unique<CompiledSourceWrapper>(node);
32✔
83
    } else if (source_type == "mesh") {
201!
84
      return make_unique<MeshSource>(node);
201✔
85
    } else {
86
      fatal_error(fmt::format("Invalid source type '{}' found.", source_type));
×
87
    }
88
  } else {
44,795✔
89
    // support legacy source format
90
    if (check_for_node(node, "file")) {
901✔
91
      return make_unique<FileSource>(node);
32✔
92
    } else if (check_for_node(node, "library")) {
869!
93
      return make_unique<CompiledSourceWrapper>(node);
×
94
    } else {
95
      return make_unique<IndependentSource>(node);
869✔
96
    }
97
  }
98
}
99

100
void Source::read_constraints(pugi::xml_node node)
45,706✔
101
{
102
  // Check for constraints node. For backwards compatibility, if no constraints
103
  // node is given, still try searching for domain constraints from top-level
104
  // node.
105
  pugi::xml_node constraints_node = node.child("constraints");
45,706✔
106
  if (constraints_node) {
45,706✔
107
    node = constraints_node;
1,871✔
108
  }
109

110
  // Check for domains to reject from
111
  if (check_for_node(node, "domain_type")) {
45,706✔
112
    std::string domain_type = get_node_value(node, "domain_type");
475✔
113
    if (domain_type == "cell") {
475✔
114
      domain_type_ = DomainType::CELL;
91✔
115
    } else if (domain_type == "material") {
384✔
116
      domain_type_ = DomainType::MATERIAL;
16✔
117
    } else if (domain_type == "universe") {
368!
118
      domain_type_ = DomainType::UNIVERSE;
368✔
119
    } else {
120
      fatal_error(
×
121
        std::string("Unrecognized domain type for constraint: " + domain_type));
×
122
    }
123

124
    auto ids = get_node_array<int>(node, "domain_ids");
475✔
125
    domain_ids_.insert(ids.begin(), ids.end());
475✔
126
  }
475✔
127

128
  if (check_for_node(node, "time_bounds")) {
45,706✔
129
    auto ids = get_node_array<double>(node, "time_bounds");
11✔
130
    if (ids.size() != 2) {
11!
131
      fatal_error("Time bounds must be represented by two numbers.");
×
132
    }
133
    time_bounds_ = std::make_pair(ids[0], ids[1]);
11✔
134
  }
11✔
135
  if (check_for_node(node, "energy_bounds")) {
45,706✔
136
    auto ids = get_node_array<double>(node, "energy_bounds");
11✔
137
    if (ids.size() != 2) {
11!
138
      fatal_error("Energy bounds must be represented by two numbers.");
×
139
    }
140
    energy_bounds_ = std::make_pair(ids[0], ids[1]);
11✔
141
  }
11✔
142

143
  if (check_for_node(node, "fissionable")) {
45,706✔
144
    only_fissionable_ = get_node_value_bool(node, "fissionable");
1,385✔
145
  }
146

147
  // Check for how to handle rejected particles
148
  if (check_for_node(node, "rejection_strategy")) {
45,706!
149
    std::string rejection_strategy = get_node_value(node, "rejection_strategy");
×
150
    if (rejection_strategy == "kill") {
×
151
      rejection_strategy_ = RejectionStrategy::KILL;
×
152
    } else if (rejection_strategy == "resample") {
×
153
      rejection_strategy_ = RejectionStrategy::RESAMPLE;
×
154
    } else {
155
      fatal_error(std::string(
×
156
        "Unrecognized strategy source rejection: " + rejection_strategy));
157
    }
158
  }
×
159
}
45,706✔
160

161
void check_rejection_fraction(int64_t n_reject, int64_t n_accept)
2,602,811✔
162
{
163
  // Don't check unless we've hit a minimum number of total sites rejected
164
  if (n_reject < EXTSRC_REJECT_THRESHOLD)
2,602,811✔
165
    return;
894,111✔
166

167
  // Compute fraction of accepted sites and compare against minimum
168
  double fraction = static_cast<double>(n_accept) / n_reject;
1,708,700✔
169
  if (fraction <= settings::source_rejection_fraction) {
1,708,700✔
170
    fatal_error(fmt::format(
3!
171
      "Too few source sites satisfied the constraints (minimum source "
172
      "rejection fraction = {}). Please check your source definition or "
173
      "set a lower value of Settings.source_rejection_fraction.",
174
      settings::source_rejection_fraction));
175
  }
176
}
177

178
SourceSite Source::sample_with_constraints(uint64_t* seed) const
33,252,851✔
179
{
180
  bool accepted = false;
33,252,851✔
181
  static int64_t n_reject = 0;
182
  static int64_t n_accept = 0;
183
  SourceSite site;
33,252,851✔
184

185
  while (!accepted) {
67,851,192✔
186
    // Sample a source site without considering constraints yet
187
    site = this->sample(seed);
34,598,344✔
188

189
    if (constraints_applied()) {
34,598,341✔
190
      accepted = true;
32,581,488✔
191
    } else {
192
      // Check whether sampled site satisfies constraints
193
      accepted = satisfies_spatial_constraints(site.r) &&
2,016,853✔
194
                 satisfies_energy_constraints(site.E) &&
2,699,121✔
195
                 satisfies_time_constraints(site.time);
682,268✔
196
      if (!accepted) {
2,016,853✔
197
        // Increment number of rejections and check against minimum fraction
198
        ++n_reject;
1,345,493✔
199
        check_rejection_fraction(n_reject, n_accept);
1,345,493✔
200

201
        // For the "kill" strategy, accept particle but set weight to 0 so that
202
        // it is terminated immediately
203
        if (rejection_strategy_ == RejectionStrategy::KILL) {
1,345,493!
204
          accepted = true;
×
205
          site.wgt = 0.0;
×
206
        }
207
      }
208
    }
209
  }
210

211
  // Increment number of accepted samples
212
  ++n_accept;
33,252,848✔
213

214
  return site;
33,252,848✔
215
}
216

217
bool Source::satisfies_energy_constraints(double E) const
33,285,672✔
218
{
219
  return E > energy_bounds_.first && E < energy_bounds_.second;
33,285,672!
220
}
221

222
bool Source::satisfies_time_constraints(double time) const
682,268✔
223
{
224
  return time > time_bounds_.first && time < time_bounds_.second;
682,268✔
225
}
226

227
bool Source::satisfies_spatial_constraints(Position r) const
37,809,979✔
228
{
229
  GeometryState geom_state;
37,809,979✔
230
  geom_state.r() = r;
37,809,979✔
231
  geom_state.u() = {0.0, 0.0, 1.0};
37,809,979✔
232

233
  // Reject particle if it's not in the geometry at all
234
  bool found = exhaustive_find_cell(geom_state);
37,809,979✔
235
  if (!found)
37,809,979✔
236
    return false;
490,340✔
237

238
  // Check the geometry state against specified domains
239
  bool accepted = true;
37,319,639✔
240
  if (!domain_ids_.empty()) {
37,319,639✔
241
    if (domain_type_ == DomainType::MATERIAL) {
1,928,467!
242
      auto mat_index = geom_state.material();
×
243
      if (mat_index == MATERIAL_VOID) {
×
244
        accepted = false;
×
245
      } else {
246
        accepted = contains(domain_ids_, model::materials[mat_index]->id());
×
247
      }
248
    } else {
249
      for (int i = 0; i < geom_state.n_coord(); i++) {
3,707,514✔
250
        auto id =
251
          (domain_type_ == DomainType::CELL)
1,928,467✔
252
            ? model::cells[geom_state.coord(i).cell()].get()->id_
1,928,467!
253
            : model::universes[geom_state.coord(i).universe()].get()->id_;
×
254
        if ((accepted = contains(domain_ids_, id)))
1,928,467✔
255
          break;
149,420✔
256
      }
257
    }
258
  }
259

260
  // Check if spatial site is in fissionable material
261
  if (accepted && only_fissionable_) {
37,319,639✔
262
    // Determine material
263
    auto mat_index = geom_state.material();
1,068,763✔
264
    if (mat_index == MATERIAL_VOID) {
1,068,763!
265
      accepted = false;
×
266
    } else {
267
      accepted = model::materials[mat_index]->fissionable();
1,068,763✔
268
    }
269
  }
270

271
  return accepted;
37,319,639✔
272
}
37,809,979✔
273

274
//==============================================================================
275
// IndependentSource implementation
276
//==============================================================================
277

278
IndependentSource::IndependentSource(
2,028✔
279
  UPtrSpace space, UPtrAngle angle, UPtrDist energy, UPtrDist time)
2,028✔
280
  : space_ {std::move(space)}, angle_ {std::move(angle)},
2,028✔
281
    energy_ {std::move(energy)}, time_ {std::move(time)}
4,056✔
282
{}
2,028✔
283

284
IndependentSource::IndependentSource(pugi::xml_node node) : Source(node)
45,410✔
285
{
286
  // Check for particle type
287
  if (check_for_node(node, "particle")) {
45,410✔
288
    auto temp_str = get_node_value(node, "particle", true, true);
44,541✔
289
    if (temp_str == "neutron") {
44,541✔
290
      particle_ = ParticleType::neutron;
44,378✔
291
    } else if (temp_str == "photon") {
163✔
292
      particle_ = ParticleType::photon;
147✔
293
      settings::photon_transport = true;
147✔
294
    } else if (temp_str == "electron") {
16!
295
      particle_ = ParticleType::electron;
16✔
296
      settings::photon_transport = true;
16✔
297
    } else if (temp_str == "positron") {
×
298
      particle_ = ParticleType::positron;
×
299
      settings::photon_transport = true;
×
300
    } else {
301
      fatal_error(std::string("Unknown source particle type: ") + temp_str);
×
302
    }
303
  }
44,541✔
304

305
  // Check for external source file
306
  if (check_for_node(node, "file")) {
45,410!
307

308
  } else {
309

310
    // Spatial distribution for external source
311
    if (check_for_node(node, "space")) {
45,410✔
312
      space_ = SpatialDistribution::create(node.child("space"));
7,279✔
313
    } else {
314
      // If no spatial distribution specified, make it a point source
315
      space_ = UPtrSpace {new SpatialPoint()};
38,131✔
316
    }
317

318
    // For backwards compatibility, check for only fissionable setting on box
319
    // source
320
    auto space_box = dynamic_cast<SpatialBox*>(space_.get());
45,409!
321
    if (space_box) {
45,409✔
322
      if (!only_fissionable_) {
3,967✔
323
        only_fissionable_ = space_box->only_fissionable();
2,582✔
324
      }
325
    }
326

327
    // Determine external source angular distribution
328
    if (check_for_node(node, "angle")) {
45,409✔
329
      angle_ = UnitSphereDistribution::create(node.child("angle"));
3,356✔
330
    } else {
331
      angle_ = UPtrAngle {new Isotropic()};
42,053✔
332
    }
333

334
    // Determine external source energy distribution
335
    if (check_for_node(node, "energy")) {
45,409✔
336
      pugi::xml_node node_dist = node.child("energy");
4,674✔
337
      energy_ = distribution_from_xml(node_dist);
4,674✔
338
    } else {
339
      // Default to a Watt spectrum with parameters 0.988 MeV and 2.249 MeV^-1
340
      energy_ = UPtrDist {new Watt(0.988e6, 2.249e-6)};
40,735✔
341
    }
342

343
    // Determine external source time distribution
344
    if (check_for_node(node, "time")) {
45,409✔
345
      pugi::xml_node node_dist = node.child("time");
43✔
346
      time_ = distribution_from_xml(node_dist);
43✔
347
    } else {
348
      // Default to a Constant time T=0
349
      double T[] {0.0};
45,366✔
350
      double p[] {1.0};
45,366✔
351
      time_ = UPtrDist {new Discrete {T, p, 1}};
45,366✔
352
    }
353
  }
354
}
45,409✔
355

356
SourceSite IndependentSource::sample(uint64_t* seed) const
34,535,811✔
357
{
358
  SourceSite site;
34,535,811✔
359
  site.particle = particle_;
34,535,811✔
360
  double r_wgt = 1.0;
34,535,811✔
361
  double E_wgt = 1.0;
34,535,811✔
362

363
  // Repeat sampling source location until a good site has been accepted
364
  bool accepted = false;
34,535,811✔
365
  static int64_t n_reject = 0;
366
  static int64_t n_accept = 0;
367

368
  while (!accepted) {
70,328,934✔
369

370
    // Sample spatial distribution
371
    auto [r, r_wgt_temp] = space_->sample(seed);
35,793,126✔
372
    site.r = r;
35,793,126✔
373
    r_wgt = r_wgt_temp;
35,793,126✔
374

375
    // Check if sampled position satisfies spatial constraints
376
    accepted = satisfies_spatial_constraints(site.r);
35,793,126✔
377

378
    // Check for rejection
379
    if (!accepted) {
35,793,126✔
380
      ++n_reject;
1,257,318✔
381
      check_rejection_fraction(n_reject, n_accept);
1,257,318✔
382
    }
383
  }
384

385
  // Sample angle
386
  auto [u, u_wgt] = angle_->sample(seed);
34,535,808✔
387
  site.u = u;
34,535,808✔
388

389
  site.wgt = r_wgt * u_wgt;
34,535,808✔
390

391
  // Sample energy and time for neutron and photon sources
392
  if (settings::solver_type != SolverType::RANDOM_RAY) {
34,535,808✔
393
    // Check for monoenergetic source above maximum particle energy
394
    auto p = static_cast<int>(particle_);
32,581,488✔
395
    auto energy_ptr = dynamic_cast<Discrete*>(energy_.get());
32,581,488!
396
    if (energy_ptr) {
32,581,488✔
397
      auto energies = xt::adapt(energy_ptr->x());
18,114,049✔
398
      if (xt::any(energies > data::energy_max[p])) {
18,114,049!
399
        fatal_error("Source energy above range of energies of at least "
×
400
                    "one cross section table");
401
      }
402
    }
18,114,049✔
403

404
    while (true) {
405
      // Sample energy spectrum
406
      auto [E, E_wgt_temp] = energy_->sample(seed);
32,581,488✔
407
      site.E = E;
32,581,488✔
408
      E_wgt = E_wgt_temp;
32,581,488✔
409

410
      // Resample if energy falls above maximum particle energy
411
      if (site.E < data::energy_max[p] &&
65,162,976!
412
          (satisfies_energy_constraints(site.E)))
32,581,488!
413
        break;
32,581,488✔
414

415
      n_reject++;
×
416
      check_rejection_fraction(n_reject, n_accept);
×
417
    }
×
418

419
    // Sample particle creation time
420
    auto [time, time_wgt] = time_->sample(seed);
32,581,488✔
421
    site.time = time;
32,581,488✔
422

423
    site.wgt *= (E_wgt * time_wgt);
32,581,488✔
424
  }
425

426
  // Increment number of accepted samples
427
  ++n_accept;
34,535,808✔
428

429
  return site;
69,071,616✔
430
}
431

432
//==============================================================================
433
// FileSource implementation
434
//==============================================================================
435

436
FileSource::FileSource(pugi::xml_node node) : Source(node)
63✔
437
{
438
  auto path = get_node_value(node, "file", false, true);
63✔
439
  load_sites_from_file(path);
63✔
440
}
54✔
441

442
FileSource::FileSource(const std::string& path)
32✔
443
{
444
  load_sites_from_file(path);
32✔
445
}
32✔
446

447
void FileSource::load_sites_from_file(const std::string& path)
95✔
448
{
449
  // If MCPL file, use the dedicated file reader
450
  if (ends_with(path, ".mcpl") || ends_with(path, ".mcpl.gz")) {
95!
451
    sites_ = mcpl_source_sites(path);
32✔
452
  } else {
453
    // Check if source file exists
454
    if (!file_exists(path)) {
63!
455
      fatal_error(fmt::format("Source file '{}' does not exist.", path));
×
456
    }
457

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

460
    // Open the binary file
461
    hid_t file_id = file_open(path, 'r', true);
63✔
462

463
    // Check to make sure this is a source file
464
    std::string filetype;
63✔
465
    read_attribute(file_id, "filetype", filetype);
63✔
466
    if (filetype != "source" && filetype != "statepoint") {
63!
467
      fatal_error("Specified starting source file not a source file type.");
×
468
    }
469

470
    // Read in the source particles
471
    read_source_bank(file_id, sites_, false);
63✔
472

473
    // Close file
474
    file_close(file_id);
54✔
475
  }
54✔
476
}
86✔
477

478
SourceSite FileSource::sample(uint64_t* seed) const
286,154✔
479
{
480
  // Sample a particle randomly from list
481
  size_t i_site = sites_.size() * prn(seed);
286,154✔
482
  return sites_[i_site];
286,154✔
483
}
484

485
//==============================================================================
486
// CompiledSourceWrapper implementation
487
//==============================================================================
488

489
CompiledSourceWrapper::CompiledSourceWrapper(pugi::xml_node node) : Source(node)
32✔
490
{
491
  // Get shared library path and parameters
492
  auto path = get_node_value(node, "library", false, true);
32✔
493
  std::string parameters;
32✔
494
  if (check_for_node(node, "parameters")) {
32✔
495
    parameters = get_node_value(node, "parameters", false, true);
16✔
496
  }
497
  setup(path, parameters);
32✔
498
}
32✔
499

500
void CompiledSourceWrapper::setup(
32✔
501
  const std::string& path, const std::string& parameters)
502
{
503
#ifdef HAS_DYNAMIC_LINKING
504
  // Open the library
505
  shared_library_ = dlopen(path.c_str(), RTLD_LAZY);
32✔
506
  if (!shared_library_) {
32!
507
    fatal_error("Couldn't open source library " + path);
×
508
  }
509

510
  // reset errors
511
  dlerror();
32✔
512

513
  // get the function to create the custom source from the library
514
  auto create_compiled_source = reinterpret_cast<create_compiled_source_t*>(
515
    dlsym(shared_library_, "openmc_create_source"));
32✔
516

517
  // check for any dlsym errors
518
  auto dlsym_error = dlerror();
32✔
519
  if (dlsym_error) {
32!
520
    std::string error_msg = fmt::format(
521
      "Couldn't open the openmc_create_source symbol: {}", dlsym_error);
×
522
    dlclose(shared_library_);
×
523
    fatal_error(error_msg);
×
524
  }
×
525

526
  // create a pointer to an instance of the custom source
527
  compiled_source_ = create_compiled_source(parameters);
32✔
528

529
#else
530
  fatal_error("Custom source libraries have not yet been implemented for "
531
              "non-POSIX systems");
532
#endif
533
}
32✔
534

535
CompiledSourceWrapper::~CompiledSourceWrapper()
64✔
536
{
537
  // Make sure custom source is cleared before closing shared library
538
  if (compiled_source_.get())
32!
539
    compiled_source_.reset();
32✔
540

541
#ifdef HAS_DYNAMIC_LINKING
542
  dlclose(shared_library_);
32✔
543
#else
544
  fatal_error("Custom source libraries have not yet been implemented for "
545
              "non-POSIX systems");
546
#endif
547
}
64✔
548

549
//==============================================================================
550
// MeshElementSpatial implementation
551
//==============================================================================
552

553
std::pair<Position, double> MeshElementSpatial::sample(uint64_t* seed) const
1,525,788✔
554
{
555
  return {model::meshes[mesh_index_]->sample_element(elem_index_, seed), 1.0};
1,525,788✔
556
}
557

558
//==============================================================================
559
// MeshSource implementation
560
//==============================================================================
561

562
MeshSource::MeshSource(pugi::xml_node node) : Source(node)
201✔
563
{
564
  int32_t mesh_id = stoi(get_node_value(node, "mesh"));
201✔
565
  int32_t mesh_idx = model::mesh_map.at(mesh_id);
201✔
566
  const auto& mesh = model::meshes[mesh_idx];
201✔
567

568
  std::vector<double> strengths;
201✔
569
  // read all source distributions and populate strengths vector for MeshSpatial
570
  // object
571
  for (auto source_node : node.children("source")) {
37,653✔
572
    auto src = Source::create(source_node);
37,452✔
573
    if (auto ptr = dynamic_cast<IndependentSource*>(src.get())) {
37,452!
574
      src.release();
37,452✔
575
      sources_.emplace_back(ptr);
37,452✔
576
    } else {
577
      fatal_error(
×
578
        "The source assigned to each element must be an IndependentSource.");
579
    }
580
    strengths.push_back(sources_.back()->strength());
37,452✔
581
  }
37,452✔
582

583
  // Set spatial distributions for each mesh element
584
  for (int elem_index = 0; elem_index < sources_.size(); ++elem_index) {
37,653✔
585
    sources_[elem_index]->set_space(
74,904✔
586
      std::make_unique<MeshElementSpatial>(mesh_idx, elem_index));
74,904✔
587
  }
588

589
  // the number of source distributions should either be one or equal to the
590
  // number of mesh elements
591
  if (sources_.size() > 1 && sources_.size() != mesh->n_bins()) {
201!
592
    fatal_error(fmt::format("Incorrect number of source distributions ({}) for "
×
593
                            "mesh source with {} elements.",
594
      sources_.size(), mesh->n_bins()));
×
595
  }
596

597
  space_ = std::make_unique<MeshSpatial>(mesh_idx, strengths);
201✔
598
}
201✔
599

600
SourceSite MeshSource::sample(uint64_t* seed) const
1,510,699✔
601
{
602
  // Sample a mesh element based on the relative strengths
603
  int32_t element = space_->sample_element_index(seed);
1,510,699✔
604

605
  // Sample the distribution for the specific mesh element; note that the
606
  // spatial distribution has been set for each element using MeshElementSpatial
607
  return source(element)->sample_with_constraints(seed);
1,510,699✔
608
}
609

610
//==============================================================================
611
// Non-member functions
612
//==============================================================================
613

614
void initialize_source()
3,942✔
615
{
616
  write_message("Initializing source particles...", 5);
3,942✔
617

618
// Generation source sites from specified distribution in user input
619
#pragma omp parallel for
620
  for (int64_t i = 0; i < simulation::work_per_rank; ++i) {
1,199,018✔
621
    // initialize random number seed
622
    int64_t id = simulation::total_gen * settings::n_particles +
2,394,530✔
623
                 simulation::work_index[mpi::rank] + i + 1;
1,197,265✔
624
    uint64_t seed = init_seed(id, STREAM_SOURCE);
1,197,265✔
625

626
    // sample external source distribution
627
    simulation::source_bank[i] = sample_external_source(&seed);
1,197,265✔
628
  }
629

630
  // Write out initial source
631
  if (settings::write_initial_source) {
3,942!
632
    write_message("Writing out initial source...", 5);
×
633
    std::string filename = settings::path_output + "initial_source.h5";
×
634
    hid_t file_id = file_open(filename, 'w', true);
×
635
    write_source_bank(file_id, simulation::source_bank, simulation::work_index);
×
636
    file_close(file_id);
×
637
  }
×
638
}
3,942✔
639

640
SourceSite sample_external_source(uint64_t* seed)
31,742,152✔
641
{
642
  // Sample from among multiple source distributions
643
  int i = 0;
31,742,152✔
644
  int n_sources = model::external_sources.size();
31,742,152✔
645
  if (n_sources > 1) {
31,742,152✔
646
    if (settings::uniform_source_sampling) {
2,458,300✔
647
      i = prn(seed) * n_sources;
2,200✔
648
    } else {
649
      i = model::external_sources_probability.sample(seed);
2,456,100✔
650
    }
651
  }
652

653
  // Sample source site from i-th source distribution
654
  SourceSite site {model::external_sources[i]->sample_with_constraints(seed)};
31,742,152✔
655

656
  // For uniform source sampling, multiply the weight by the ratio of the actual
657
  // probability of sampling source i to the biased probability of sampling
658
  // source i, which is (strength_i / total_strength) / (1 / n)
659
  if (n_sources > 1 && settings::uniform_source_sampling) {
31,742,149✔
660
    double total_strength = model::external_sources_probability.integral();
2,200✔
661
    site.wgt *=
2,200✔
662
      model::external_sources[i]->strength() * n_sources / total_strength;
2,200✔
663
  }
664

665
  // If running in MG, convert site.E to group
666
  if (!settings::run_CE) {
31,742,149✔
667
    site.E = lower_bound_index(data::mg.rev_energy_bins_.begin(),
1,742,400✔
668
      data::mg.rev_energy_bins_.end(), site.E);
669
    site.E = data::mg.num_energy_groups_ - site.E - 1.;
1,742,400✔
670
  }
671

672
  if (!model::active_point_tallies.empty()) {
31,742,149!
NEW
673
    score_point_tally(site, i);
×
674
  }
675

676
  return site;
31,742,149✔
677
}
678

679
void free_memory_source()
8,164✔
680
{
681
  model::external_sources.clear();
8,164✔
682
}
8,164✔
683

684
//==============================================================================
685
// C API
686
//==============================================================================
687

688
extern "C" int openmc_sample_external_source(
955✔
689
  size_t n, uint64_t* seed, void* sites)
690
{
691
  if (!sites || !seed) {
955!
692
    set_errmsg("Received null pointer.");
×
693
    return OPENMC_E_INVALID_ARGUMENT;
×
694
  }
695

696
  if (model::external_sources.empty()) {
955!
697
    set_errmsg("No external sources have been defined.");
×
698
    return OPENMC_E_OUT_OF_BOUNDS;
×
699
  }
700

701
  auto sites_array = static_cast<SourceSite*>(sites);
955✔
702
  for (size_t i = 0; i < n; ++i) {
2,957,779✔
703
    sites_array[i] = sample_external_source(seed);
2,956,824✔
704
  }
705
  return 0;
955✔
706
}
707

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