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

openmc-dev / openmc / 15702517337

17 Jun 2025 08:44AM UTC coverage: 85.156% (-0.006%) from 85.162%
15702517337

Pull #3404

github

web-flow
Merge 637e34930 into 23eab2c89
Pull Request #3404: New Feature: electron/positron independent source.

18 of 24 new or added lines in 5 files covered. (75.0%)

47 existing lines in 2 files now uncovered.

52383 of 61514 relevant lines covered (85.16%)

36532686.89 hits per line

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

84.95
/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
//==============================================================================
41
// Global variables
42
//==============================================================================
43

44
namespace model {
45

46
vector<unique_ptr<Source>> external_sources;
47

48
DiscreteIndex external_sources_probability;
49

50
} // namespace model
51

52
//==============================================================================
53
// Source implementation
54
//==============================================================================
55

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

66
  // Check for additional defined constraints
67
  read_constraints(node);
44,444 ✔
68
}
44,444 ✔
69

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

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

109
  // Check for domains to reject from
110
  if (check_for_node(node, "domain_type")) {
44,444 ✔
111
    std::string domain_type = get_node_value(node, "domain_type");
399 ✔
112
    if (domain_type == "cell") {
399 ✔
113
      domain_type_ = DomainType::CELL;
47 ✔
114
    } else if (domain_type == "material") {
352 ✔
115
      domain_type_ = DomainType::MATERIAL;
16 ✔
116
    } else if (domain_type == "universe") {
336 ✔
117
      domain_type_ = DomainType::UNIVERSE;
336 ✔
118
    } else {
119
      fatal_error(
×
120
        std::string("Unrecognized domain type for constraint: " + domain_type));
×
121
    }
122

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

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

142
  if (check_for_node(node, "fissionable")) {
44,444 ✔
143
    only_fissionable_ = get_node_value_bool(node, "fissionable");
1,121 ✔
144
  }
145

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

160
void check_rejection_fraction(int64_t n_reject, int64_t n_accept)
1,127,527 ✔
161
{
162
  // Don't check unless we've hit a minimum number of total sites rejected
163
  if (n_reject < EXTSRC_REJECT_THRESHOLD)
1,127,527 ✔
164
    return;
669,540 ✔
165

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

177
SourceSite Source::sample_with_constraints(uint64_t* seed) const
26,699,386 ✔
178
{
179
  bool accepted = false;
26,699,386 ✔
180
  static int64_t n_reject = 0;
181
  static int64_t n_accept = 0;
182
  SourceSite site;
26,699,386 ✔
183

184
  while (!accepted) {
53,432,200 ✔
185
    // Sample a source site without considering constraints yet
186
    site = this->sample(seed);
26,732,817 ✔
187

188
    if (constraints_applied()) {
26,732,814 ✔
189
      accepted = true;
26,335,943 ✔
190
    } else {
191
      // Check whether sampled site satisfies constraints
192
      accepted = satisfies_spatial_constraints(site.r) &&
396,871 ✔
193
                 satisfies_energy_constraints(site.E) &&
771,382 ✔
194
                 satisfies_time_constraints(site.time);
374,511 ✔
195
      if (!accepted) {
396,871 ✔
196
        // Increment number of rejections and check against minimum fraction
197
        ++n_reject;
33,431 ✔
198
        check_rejection_fraction(n_reject, n_accept);
33,431 ✔
199

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

210
  // Increment number of accepted samples
211
  ++n_accept;
26,699,383 ✔
212

213
  return site;
26,699,383 ✔
214
}
215

216
bool Source::satisfies_energy_constraints(double E) const
26,732,814 ✔
217
{
218
  return E > energy_bounds_.first && E < energy_bounds_.second;
26,732,814 ✔
219
}
220

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

226
bool Source::satisfies_spatial_constraints(Position r) const
30,987,441 ✔
227
{
228
  GeometryState geom_state;
30,987,441 ✔
229
  geom_state.r() = r;
30,987,441 ✔
230
  geom_state.u() = {0.0, 0.0, 1.0};
30,987,441 ✔
231

232
  // Reject particle if it's not in the geometry at all
233
  bool found = exhaustive_find_cell(geom_state);
30,987,441 ✔
234
  if (!found)
30,987,441 ✔
235
    return false;
375,441 ✔
236

237
  // Check the geometry state against specified domains
238
  bool accepted = true;
30,612,000 ✔
239
  if (!domain_ids_.empty()) {
30,612,000 ✔
240
    if (domain_type_ == DomainType::MATERIAL) {
1,794,310 ✔
241
      auto mat_index = geom_state.material();
×
242
      if (mat_index != MATERIAL_VOID) {
×
243
        accepted = contains(domain_ids_, model::materials[mat_index]->id());
×
244
      }
245
    } else {
246
      for (int i = 0; i < geom_state.n_coord(); i++) {
3,560,200 ✔
247
        auto id = (domain_type_ == DomainType::CELL)
1,794,310 ✔
248
                    ? model::cells[geom_state.coord(i).cell]->id_
1,794,310 ✔
249
                    : model::universes[geom_state.coord(i).universe]->id_;
×
250
        if ((accepted = contains(domain_ids_, id)))
1,794,310 ✔
251
          break;
28,420 ✔
252
      }
253
    }
254
  }
255

256
  // Check if spatial site is in fissionable material
257
  if (accepted && only_fissionable_) {
30,612,000 ✔
258
    // Determine material
259
    auto mat_index = geom_state.material();
1,003,176 ✔
260
    if (mat_index == MATERIAL_VOID) {
1,003,176 ✔
261
      accepted = false;
×
262
    } else {
263
      accepted = model::materials[mat_index]->fissionable();
1,003,176 ✔
264
    }
265
  }
266

267
  return accepted;
30,612,000 ✔
268
}
30,987,441 ✔
269

270
//==============================================================================
271
// IndependentSource implementation
272
//==============================================================================
273

274
IndependentSource::IndependentSource(
1,620 ✔
275
  UPtrSpace space, UPtrAngle angle, UPtrDist energy, UPtrDist time)
1,620 ✔
276
  : space_ {std::move(space)}, angle_ {std::move(angle)},
1,620 ✔
277
    energy_ {std::move(energy)}, time_ {std::move(time)}
3,240 ✔
278
{}
1,620 ✔
279

280
IndependentSource::IndependentSource(pugi::xml_node node) : Source(node)
44,137 ✔
281
{
282
  // Check for particle type
283
  if (check_for_node(node, "particle")) {
44,137 ✔
284
    auto temp_str = get_node_value(node, "particle", true, true);
43,252 ✔
285
    if (temp_str == "neutron") {
43,252 ✔
286
      particle_ = ParticleType::neutron;
43,111 ✔
287
    } else if (temp_str == "photon") {
141 ✔
288
      particle_ = ParticleType::photon;
125 ✔
289
      settings::photon_transport = true;
125 ✔
290
    } else if (temp_str == "electron") {
16 ✔
291
      particle_ = ParticleType::electron;
16 ✔
292
      settings::photon_transport = true;
16 ✔
293
      if (settings::electron_treatment == ElectronTreatment::LED)
16 ✔
NEW
294
        settings::electron_treatment = ElectronTreatment::TTB;
×
NEW
295
    } else if (temp_str == "positron") {
×
NEW
296
      particle_ = ParticleType::positron;
×
NEW
297
      settings::photon_transport = true;
×
NEW
298
      if (settings::electron_treatment == ElectronTreatment::LED)
×
NEW
UNCOV
299
        settings::electron_treatment = ElectronTreatment::TTB;
×
300
    } else {
UNCOV
301
      fatal_error(std::string("Unknown source particle type: ") + temp_str);
×
302
    }
303
  }
43,252 ✔
304

305
  // Check for external source file
306
  if (check_for_node(node, "file")) {
44,137 ✔
307

308
  } else {
309

310
    // Spatial distribution for external source
311
    if (check_for_node(node, "space")) {
44,137 ✔
312
      space_ = SpatialDistribution::create(node.child("space"));
6,141 ✔
313
    } else {
314
      // If no spatial distribution specified, make it a point source
315
      space_ = UPtrSpace {new SpatialPoint()};
37,996 ✔
316
    }
317

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

327
    // Determine external source angular distribution
328
    if (check_for_node(node, "angle")) {
44,136 ✔
329
      angle_ = UnitSphereDistribution::create(node.child("angle"));
3,179 ✔
330
    } else {
331
      angle_ = UPtrAngle {new Isotropic()};
40,957 ✔
332
    }
333

334
    // Determine external source energy distribution
335
    if (check_for_node(node, "energy")) {
44,136 ✔
336
      pugi::xml_node node_dist = node.child("energy");
4,208 ✔
337
      energy_ = distribution_from_xml(node_dist);
4,208 ✔
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)};
39,928 ✔
341
    }
342

343
    // Determine external source time distribution
344
    if (check_for_node(node, "time")) {
44,136 ✔
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};
44,093 ✔
350
      double p[] {1.0};
44,093 ✔
351
      time_ = UPtrDist {new Discrete {T, p, 1}};
44,093 ✔
352
    }
353
  }
354
}
44,136 ✔
355

356
SourceSite IndependentSource::sample(uint64_t* seed) const
27,994,606 ✔
357
{
358
  SourceSite site;
27,994,606 ✔
359
  site.particle = particle_;
27,994,606 ✔
360

361
  // Repeat sampling source location until a good site has been accepted
362
  bool accepted = false;
27,994,606 ✔
363
  static int64_t n_reject = 0;
364
  static int64_t n_accept = 0;
365

366
  while (!accepted) {
57,083,302 ✔
367

368
    // Sample spatial distribution
369
    site.r = space_->sample(seed);
29,088,699 ✔
370

371
    // Check if sampled position satisfies spatial constraints
372
    accepted = satisfies_spatial_constraints(site.r);
29,088,699 ✔
373

374
    // Check for rejection
375
    if (!accepted) {
29,088,699 ✔
376
      ++n_reject;
1,094,096 ✔
377
      check_rejection_fraction(n_reject, n_accept);
1,094,096 ✔
378
    }
379
  }
380

381
  // Sample angle
382
  site.u = angle_->sample(seed);
27,994,603 ✔
383

384
  // Sample energy and time for neutron and photon sources
385
  if (settings::solver_type != SolverType::RANDOM_RAY) {
27,994,603 ✔
386
    // Check for monoenergetic source above maximum particle energy
387
    auto p = static_cast<int>(particle_);
26,148,803 ✔
388
    auto energy_ptr = dynamic_cast<Discrete*>(energy_.get());
26,148,803 ✔
389
    if (energy_ptr) {
26,148,803 ✔
390
      auto energies = xt::adapt(energy_ptr->x());
16,880,827 ✔
391
      if (xt::any(energies > data::energy_max[p])) {
16,880,827 ✔
UNCOV
392
        fatal_error("Source energy above range of energies of at least "
×
393
                    "one cross section table");
394
      }
395
    }
16,880,827 ✔
396

397
    while (true) {
398
      // Sample energy spectrum
399
      site.E = energy_->sample(seed);
26,148,803 ✔
400

401
      // Resample if energy falls above maximum particle energy
402
      if (site.E < data::energy_max[p] &&
52,297,606 ✔
403
          (satisfies_energy_constraints(site.E)))
26,148,803 ✔
404
        break;
26,148,803 ✔
405

UNCOV
406
      n_reject++;
×
UNCOV
407
      check_rejection_fraction(n_reject, n_accept);
×
408
    }
409

410
    // Sample particle creation time
411
    site.time = time_->sample(seed);
26,148,803 ✔
412
  }
413

414
  // Increment number of accepted samples
415
  ++n_accept;
27,994,603 ✔
416

417
  return site;
27,994,603 ✔
418
}
419

420
//==============================================================================
421
// FileSource implementation
422
//==============================================================================
423

424
FileSource::FileSource(pugi::xml_node node) : Source(node)
74 ✔
425
{
426
  auto path = get_node_value(node, "file", false, true);
74 ✔
427
  if (ends_with(path, ".mcpl") || ends_with(path, ".mcpl.gz")) {
74 ✔
428
    sites_ = mcpl_source_sites(path);
16 ✔
429
  } else {
430
    this->load_sites_from_file(path);
58 ✔
431
  }
432
}
65 ✔
433

434
FileSource::FileSource(const std::string& path)
16 ✔
435
{
436
  load_sites_from_file(path);
16 ✔
437
}
16 ✔
438

439
void FileSource::load_sites_from_file(const std::string& path)
74 ✔
440
{
441
  // Check if source file exists
442
  if (!file_exists(path)) {
74 ✔
UNCOV
443
    fatal_error(fmt::format("Source file '{}' does not exist.", path));
×
444
  }
445

446
  // Read the source from a binary file instead of sampling from some
447
  // assumed source distribution
448
  write_message(6, "Reading source file from {}...", path);
74 ✔
449

450
  // Open the binary file
451
  hid_t file_id = file_open(path, 'r', true);
74 ✔
452

453
  // Check to make sure this is a source file
454
  std::string filetype;
74 ✔
455
  read_attribute(file_id, "filetype", filetype);
74 ✔
456
  if (filetype != "source" && filetype != "statepoint") {
74 ✔
UNCOV
457
    fatal_error("Specified starting source file not a source file type.");
×
458
  }
459

460
  // Read in the source particles
461
  read_source_bank(file_id, sites_, false);
74 ✔
462

463
  // Close file
464
  file_close(file_id);
65 ✔
465
}
65 ✔
466

467
SourceSite FileSource::sample(uint64_t* seed) const
176,871 ✔
468
{
469
  // Sample a particle randomly from list
470
  size_t i_site = sites_.size() * prn(seed);
176,871 ✔
471
  return sites_[i_site];
176,871 ✔
472
}
473

474
//==============================================================================
475
// CompiledSourceWrapper implementation
476
//==============================================================================
477

478
CompiledSourceWrapper::CompiledSourceWrapper(pugi::xml_node node) : Source(node)
32 ✔
479
{
480
  // Get shared library path and parameters
481
  auto path = get_node_value(node, "library", false, true);
32 ✔
482
  std::string parameters;
32 ✔
483
  if (check_for_node(node, "parameters")) {
32 ✔
484
    parameters = get_node_value(node, "parameters", false, true);
16 ✔
485
  }
486
  setup(path, parameters);
32 ✔
487
}
32 ✔
488

489
void CompiledSourceWrapper::setup(
32 ✔
490
  const std::string& path, const std::string& parameters)
491
{
492
#ifdef HAS_DYNAMIC_LINKING
493
  // Open the library
494
  shared_library_ = dlopen(path.c_str(), RTLD_LAZY);
32 ✔
495
  if (!shared_library_) {
32 ✔
UNCOV
496
    fatal_error("Couldn't open source library " + path);
×
497
  }
498

499
  // reset errors
500
  dlerror();
32 ✔
501

502
  // get the function to create the custom source from the library
503
  auto create_compiled_source = reinterpret_cast<create_compiled_source_t*>(
504
    dlsym(shared_library_, "openmc_create_source"));
32 ✔
505

506
  // check for any dlsym errors
507
  auto dlsym_error = dlerror();
32 ✔
508
  if (dlsym_error) {
32 ✔
509
    std::string error_msg = fmt::format(
UNCOV
510
      "Couldn't open the openmc_create_source symbol: {}", dlsym_error);
×
UNCOV
511
    dlclose(shared_library_);
×
UNCOV
512
    fatal_error(error_msg);
×
UNCOV
513
  }
×
514

515
  // create a pointer to an instance of the custom source
516
  compiled_source_ = create_compiled_source(parameters);
32 ✔
517

518
#else
519
  fatal_error("Custom source libraries have not yet been implemented for "
520
              "non-POSIX systems");
521
#endif
522
}
32 ✔
523

524
CompiledSourceWrapper::~CompiledSourceWrapper()
64 ✔
525
{
526
  // Make sure custom source is cleared before closing shared library
527
  if (compiled_source_.get())
32 ✔
528
    compiled_source_.reset();
32 ✔
529

530
#ifdef HAS_DYNAMIC_LINKING
531
  dlclose(shared_library_);
32 ✔
532
#else
533
  fatal_error("Custom source libraries have not yet been implemented for "
534
              "non-POSIX systems");
535
#endif
536
}
64 ✔
537

32 ✔
538
//==============================================================================
539
// MeshSource implementation
540
//==============================================================================
541

542
MeshSource::MeshSource(pugi::xml_node node) : Source(node)
543
{
544
  int32_t mesh_id = stoi(get_node_value(node, "mesh"));
545
  int32_t mesh_idx = model::mesh_map.at(mesh_id);
546
  const auto& mesh = model::meshes[mesh_idx];
547

548
  std::vector<double> strengths;
549
  // read all source distributions and populate strengths vector for MeshSpatial
32 ✔
550
  // object
32 ✔
551
  for (auto source_node : node.children("source")) {
552
    sources_.emplace_back(Source::create(source_node));
553
    strengths.push_back(sources_.back()->strength());
32 ✔
554
  }
32 ✔
555

556
  // the number of source distributions should either be one or equal to the
557
  // number of mesh elements
32 ✔
558
  if (sources_.size() > 1 && sources_.size() != mesh->n_bins()) {
559
    fatal_error(fmt::format("Incorrect number of source distributions ({}) for "
560
                            "mesh source with {} elements.",
561
      sources_.size(), mesh->n_bins()));
562
  }
32 ✔
563

564
  space_ = std::make_unique<MeshSpatial>(mesh_idx, strengths);
565
}
566

567
SourceSite MeshSource::sample(uint64_t* seed) const
568
{
201 ✔
569
  // Sample the CDF defined in initialization above
570
  int32_t element = space_->sample_element_index(seed);
201 ✔
571

201 ✔
572
  // Sample position and apply rejection on spatial domains
201 ✔
573
  Position r;
574
  do {
201 ✔
575
    r = space_->mesh()->sample_element(element, seed);
576
  } while (!this->satisfies_spatial_constraints(r));
577

37,642 ✔
578
  SourceSite site;
37,441 ✔
579
  while (true) {
37,441 ✔
580
    // Sample source for the chosen element and replace the position
581
    site = source(element)->sample_with_constraints(seed);
582
    site.r = r;
583

584
    // Apply other rejections
201 ✔
UNCOV
585
    if (satisfies_energy_constraints(site.E) &&
×
586
        satisfies_time_constraints(site.time)) {
UNCOV
587
      break;
×
588
    }
589
  }
590

201 ✔
591
  return site;
201 ✔
592
}
593

187,140 ✔
594
//==============================================================================
595
// Non-member functions
596
//==============================================================================
187,140 ✔
597

598
void initialize_source()
599
{
187,140 ✔
600
  write_message("Initializing source particles...", 5);
601

1,501,871 ✔
602
// Generation source sites from specified distribution in user input
1,501,871 ✔
603
#pragma omp parallel for
604
  for (int64_t i = 0; i < simulation::work_per_rank; ++i) {
187,140 ✔
605
    // initialize random number seed
606
    int64_t id = simulation::total_gen * settings::n_particles +
607
                 simulation::work_index[mpi::rank] + i + 1;
187,140 ✔
608
    uint64_t seed = init_seed(id, STREAM_SOURCE);
187,140 ✔
609

610
    // sample external source distribution
611
    simulation::source_bank[i] = sample_external_source(&seed);
374,280 ✔
612
  }
187,140 ✔
613

187,140 ✔
614
  // Write out initial source
615
  if (settings::write_initial_source) {
616
    write_message("Writing out initial source...", 5);
617
    std::string filename = settings::path_output + "initial_source.h5";
374,280 ✔
618
    hid_t file_id = file_open(filename, 'w', true);
619
    write_source_bank(file_id, simulation::source_bank, simulation::work_index);
620
    file_close(file_id);
621
  }
622
}
623

624
SourceSite sample_external_source(uint64_t* seed)
3,410 ✔
625
{
626
  // Sample from among multiple source distributions
3,410 ✔
627
  int i = 0;
628
  int n_sources = model::external_sources.size();
629
  if (n_sources > 1) {
630
    if (settings::uniform_source_sampling) {
1,047,964 ✔
631
      i = prn(seed) * n_sources;
632
    } else {
2,092,900 ✔
633
      i = model::external_sources_probability.sample(seed);
1,046,450 ✔
634
    }
1,046,450 ✔
635
  }
636

637
  // Sample source site from i-th source distribution
1,046,450 ✔
638
  SourceSite site {model::external_sources[i]->sample_with_constraints(seed)};
639

640
  // For uniform source sampling, multiply the weight by the ratio of the actual
641
  // probability of sampling source i to the biased probability of sampling
3,410 ✔
642
  // source i, which is (strength_i / total_strength) / (1 / n)
×
UNCOV
643
  if (n_sources > 1 && settings::uniform_source_sampling) {
×
UNCOV
644
    double total_strength = model::external_sources_probability.integral();
×
UNCOV
645
    site.wgt *=
×
UNCOV
646
      model::external_sources[i]->strength() * n_sources / total_strength;
×
647
  }
648

3,410 ✔
649
  // If running in MG, convert site.E to group
650
  if (!settings::run_CE) {
26,512,246 ✔
651
    site.E = lower_bound_index(data::mg.rev_energy_bins_.begin(),
652
      data::mg.rev_energy_bins_.end(), site.E);
653
    site.E = data::mg.num_energy_groups_ - site.E - 1.;
26,512,246 ✔
654
  }
26,512,246 ✔
655

26,512,246 ✔
656
  return site;
146,300 ✔
657
}
2,200 ✔
658

659
void free_memory_source()
144,100 ✔
660
{
661
  model::external_sources.clear();
662
}
663

664
//==============================================================================
26,512,246 ✔
665
// C API
666
//==============================================================================
667

668
extern "C" int openmc_sample_external_source(
669
  size_t n, uint64_t* seed, void* sites)
26,512,243 ✔
670
{
2,200 ✔
671
  if (!sites || !seed) {
2,200 ✔
672
    set_errmsg("Received null pointer.");
2,200 ✔
673
    return OPENMC_E_INVALID_ARGUMENT;
674
  }
675

676
  if (model::external_sources.empty()) {
26,512,243 ✔
677
    set_errmsg("No external sources have been defined.");
1,742,400 ✔
678
    return OPENMC_E_OUT_OF_BOUNDS;
679
  }
1,742,400 ✔
680

681
  auto sites_array = static_cast<SourceSite*>(sites);
682
  for (size_t i = 0; i < n; ++i) {
26,512,243 ✔
683
    sites_array[i] = sample_external_source(seed);
684
  }
685
  return 0;
6,801 ✔
686
}
687

6,801 ✔
688
} // namespace openmc
6,801 ✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc