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

openmc-dev / openmc / 21240277645

22 Jan 2026 07:46AM UTC coverage: 80.906% (-1.1%) from 81.998%
21240277645

Pull #3745

github

web-flow
Merge b47b53e16 into c5df2bf62
Pull Request #3745: Add n_elements to the MeshBase protocol and deprecate num_mesh_cells

16262 of 22513 branches covered (72.23%)

Branch coverage included in aggregate %.

15 of 21 new or added lines in 2 files covered. (71.43%)

1061 existing lines in 55 files now uncovered.

53902 of 64210 relevant lines covered (83.95%)

7933045.48 hits per line

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

77.96
/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)
988✔
57
{
58
  // Check for source strength
59
  if (check_for_node(node, "strength")) {
988✔
60
    strength_ = std::stod(get_node_value(node, "strength"));
902✔
61
    if (strength_ < 0.0) {
902!
62
      fatal_error("Source strength is negative.");
×
63
    }
64
  }
65

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

70
unique_ptr<Source> Source::create(pugi::xml_node node)
988✔
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")) {
988✔
75
    std::string source_type = get_node_value(node, "type");
881✔
76
    if (source_type == "independent") {
881✔
77
      return make_unique<IndependentSource>(node);
854✔
78
    } else if (source_type == "file") {
27✔
79
      return make_unique<FileSource>(node);
3✔
80
    } else if (source_type == "compiled") {
24✔
81
      return make_unique<CompiledSourceWrapper>(node);
4✔
82
    } else if (source_type == "mesh") {
20!
83
      return make_unique<MeshSource>(node);
20✔
84
    } else {
85
      fatal_error(fmt::format("Invalid source type '{}' found.", source_type));
×
86
    }
87
  } else {
880✔
88
    // support legacy source format
89
    if (check_for_node(node, "file")) {
107✔
90
      return make_unique<FileSource>(node);
4✔
91
    } else if (check_for_node(node, "library")) {
103!
92
      return make_unique<CompiledSourceWrapper>(node);
×
93
    } else {
94
      return make_unique<IndependentSource>(node);
103✔
95
    }
96
  }
97
}
98

99
void Source::read_constraints(pugi::xml_node node)
988✔
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");
988✔
105
  if (constraints_node) {
988✔
106
    node = constraints_node;
201✔
107
  }
108

109
  // Check for domains to reject from
110
  if (check_for_node(node, "domain_type")) {
988✔
111
    std::string domain_type = get_node_value(node, "domain_type");
56✔
112
    if (domain_type == "cell") {
56✔
113
      domain_type_ = DomainType::CELL;
8✔
114
    } else if (domain_type == "material") {
48✔
115
      domain_type_ = DomainType::MATERIAL;
2✔
116
    } else if (domain_type == "universe") {
46!
117
      domain_type_ = DomainType::UNIVERSE;
46✔
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");
56✔
124
    domain_ids_.insert(ids.begin(), ids.end());
56✔
125
  }
56✔
126

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

142
  if (check_for_node(node, "fissionable")) {
988✔
143
    only_fissionable_ = get_node_value_bool(node, "fissionable");
144✔
144
  }
145

146
  // Check for how to handle rejected particles
147
  if (check_for_node(node, "rejection_strategy")) {
988!
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
}
988✔
159

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

166
  // Compute fraction of accepted sites and compare against minimum
167
  double fraction = static_cast<double>(n_accept) / n_reject;
129,346✔
168
  if (fraction <= settings::source_rejection_fraction) {
129,346!
UNCOV
169
    fatal_error(fmt::format(
×
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
2,916,677✔
178
{
179
  bool accepted = false;
2,916,677✔
180
  static int64_t n_reject = 0;
181
  static int64_t n_accept = 0;
182
  SourceSite site;
2,916,677✔
183

184
  while (!accepted) {
5,961,646✔
185
    // Sample a source site without considering constraints yet
186
    site = this->sample(seed);
3,044,969✔
187

188
    if (constraints_applied()) {
3,044,969✔
189
      accepted = true;
2,845,647✔
190
    } else {
191
      // Check whether sampled site satisfies constraints
192
      accepted = satisfies_spatial_constraints(site.r) &&
199,322✔
193
                 satisfies_energy_constraints(site.E) &&
271,305✔
194
                 satisfies_time_constraints(site.time);
71,983✔
195
      if (!accepted) {
199,322✔
196
        // Increment number of rejections and check against minimum fraction
197
        ++n_reject;
128,292✔
198
        check_rejection_fraction(n_reject, n_accept);
128,292✔
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) {
128,292!
203
          accepted = true;
×
204
          site.wgt = 0.0;
×
205
        }
206
      }
207
    }
208
  }
209

210
  // Increment number of accepted samples
211
  ++n_accept;
2,916,677✔
212

213
  return site;
2,916,677✔
214
}
215

216
bool Source::satisfies_energy_constraints(double E) const
2,919,617✔
217
{
218
  return E > energy_bounds_.first && E < energy_bounds_.second;
2,919,617!
219
}
220

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

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

232
  // Reject particle if it's not in the geometry at all
233
  bool found = exhaustive_find_cell(geom_state);
3,295,885✔
234
  if (!found)
3,295,885✔
235
    return false;
44,547✔
236

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

259
  // Check if spatial site is in fissionable material
260
  if (accepted && only_fissionable_) {
3,251,338✔
261
    // Determine material
262
    auto mat_index = geom_state.material();
96,974✔
263
    if (mat_index == MATERIAL_VOID) {
96,974!
264
      accepted = false;
×
265
    } else {
266
      accepted = model::materials[mat_index]->fissionable();
96,974✔
267
    }
268
  }
269

270
  return accepted;
3,251,338✔
271
}
3,295,885✔
272

273
//==============================================================================
274
// IndependentSource implementation
275
//==============================================================================
276

277
IndependentSource::IndependentSource(
198✔
278
  UPtrSpace space, UPtrAngle angle, UPtrDist energy, UPtrDist time)
198✔
279
  : space_ {std::move(space)}, angle_ {std::move(angle)},
198✔
280
    energy_ {std::move(energy)}, time_ {std::move(time)}
396✔
281
{}
198✔
282

283
IndependentSource::IndependentSource(pugi::xml_node node) : Source(node)
957✔
284
{
285
  // Check for particle type
286
  if (check_for_node(node, "particle")) {
957✔
287
    auto temp_str = get_node_value(node, "particle", true, true);
854✔
288
    if (temp_str == "neutron") {
854✔
289
      particle_ = ParticleType::neutron;
835✔
290
    } else if (temp_str == "photon") {
19✔
291
      particle_ = ParticleType::photon;
17✔
292
      settings::photon_transport = true;
17✔
293
    } else if (temp_str == "electron") {
2!
294
      particle_ = ParticleType::electron;
2✔
295
      settings::photon_transport = true;
2✔
296
    } else if (temp_str == "positron") {
×
297
      particle_ = ParticleType::positron;
×
298
      settings::photon_transport = true;
×
299
    } else {
300
      fatal_error(std::string("Unknown source particle type: ") + temp_str);
×
301
    }
302
  }
854✔
303

304
  // Check for external source file
305
  if (check_for_node(node, "file")) {
957!
306

307
  } else {
308

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

317
    // For backwards compatibility, check for only fissionable setting on box
318
    // source
319
    auto space_box = dynamic_cast<SpatialBox*>(space_.get());
957!
320
    if (space_box) {
957✔
321
      if (!only_fissionable_) {
437✔
322
        only_fissionable_ = space_box->only_fissionable();
293✔
323
      }
324
    }
325

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

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

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

355
SourceSite IndependentSource::sample(uint64_t* seed) const
3,020,247✔
356
{
357
  SourceSite site;
3,020,247✔
358
  site.particle = particle_;
3,020,247✔
359
  double r_wgt = 1.0;
3,020,247✔
360
  double E_wgt = 1.0;
3,020,247✔
361

362
  // Repeat sampling source location until a good site has been accepted
363
  bool accepted = false;
3,020,247✔
364
  static int64_t n_reject = 0;
365
  static int64_t n_accept = 0;
366

367
  while (!accepted) {
6,116,810✔
368

369
    // Sample spatial distribution
370
    auto [r, r_wgt_temp] = space_->sample(seed);
3,096,563✔
371
    site.r = r;
3,096,563✔
372
    r_wgt = r_wgt_temp;
3,096,563✔
373

374
    // Check if sampled position satisfies spatial constraints
375
    accepted = satisfies_spatial_constraints(site.r);
3,096,563✔
376

377
    // Check for rejection
378
    if (!accepted) {
3,096,563✔
379
      ++n_reject;
76,316✔
380
      check_rejection_fraction(n_reject, n_accept);
76,316✔
381
    }
382
  }
383

384
  // Sample angle
385
  auto [u, u_wgt] = angle_->sample(seed);
3,020,247✔
386
  site.u = u;
3,020,247✔
387

388
  site.wgt = r_wgt * u_wgt;
3,020,247✔
389

390
  // Sample energy and time for neutron and photon sources
391
  if (settings::solver_type != SolverType::RANDOM_RAY) {
3,020,247✔
392
    // Check for monoenergetic source above maximum particle energy
393
    auto p = static_cast<int>(particle_);
2,845,647✔
394
    auto energy_ptr = dynamic_cast<Discrete*>(energy_.get());
2,845,647!
395
    if (energy_ptr) {
2,845,647✔
396
      auto energies = xt::adapt(energy_ptr->x());
1,574,953✔
397
      if (xt::any(energies > data::energy_max[p])) {
1,574,953!
398
        fatal_error("Source energy above range of energies of at least "
×
399
                    "one cross section table");
400
      }
401
    }
1,574,953✔
402

403
    while (true) {
404
      // Sample energy spectrum
405
      auto [E, E_wgt_temp] = energy_->sample(seed);
2,845,647✔
406
      site.E = E;
2,845,647✔
407
      E_wgt = E_wgt_temp;
2,845,647✔
408

409
      // Resample if energy falls above maximum particle energy
410
      if (site.E < data::energy_max[p] &&
5,691,294!
411
          (satisfies_energy_constraints(site.E)))
2,845,647!
412
        break;
2,845,647✔
413

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

418
    // Sample particle creation time
419
    auto [time, time_wgt] = time_->sample(seed);
2,845,647✔
420
    site.time = time;
2,845,647✔
421

422
    site.wgt *= (E_wgt * time_wgt);
2,845,647✔
423
  }
424

425
  // Increment number of accepted samples
426
  ++n_accept;
3,020,247✔
427

428
  return site;
6,040,494✔
429
}
430

431
//==============================================================================
432
// FileSource implementation
433
//==============================================================================
434

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

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

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

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

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

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

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

472
    // Close file
473
    file_close(file_id);
6✔
474
  }
6✔
475
}
10✔
476

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

484
//==============================================================================
485
// CompiledSourceWrapper implementation
486
//==============================================================================
487

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

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

509
  // reset errors
510
  dlerror();
4✔
511

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

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

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

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

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

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

548
//==============================================================================
549
// MeshElementSpatial implementation
550
//==============================================================================
551

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

557
//==============================================================================
558
// MeshSource implementation
559
//==============================================================================
560

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

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

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

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

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

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

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

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

613
void initialize_source()
433✔
614
{
615
  write_message("Initializing source particles...", 5);
433✔
616

617
// Generation source sites from specified distribution in user input
618
#pragma omp parallel for
619
  for (int64_t i = 0; i < simulation::work_per_rank; ++i) {
239,546✔
620
    // initialize random number seed
621
    int64_t id = simulation::total_gen * settings::n_particles +
478,226✔
622
                 simulation::work_index[mpi::rank] + i + 1;
239,113✔
623
    uint64_t seed = init_seed(id, STREAM_SOURCE);
239,113✔
624

625
    // sample external source distribution
626
    simulation::source_bank[i] = sample_external_source(&seed);
239,113✔
627
  }
628

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

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

652
  // Sample source site from i-th source distribution
653
  SourceSite site {model::external_sources[i]->sample_with_constraints(seed)};
2,763,325✔
654

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

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

671
  return site;
2,763,325✔
672
}
673

674
void free_memory_source()
843✔
675
{
676
  model::external_sources.clear();
843✔
677
}
843✔
678

679
//==============================================================================
680
// C API
681
//==============================================================================
682

683
extern "C" int openmc_sample_external_source(
32✔
684
  size_t n, uint64_t* seed, void* sites)
685
{
686
  if (!sites || !seed) {
32!
687
    set_errmsg("Received null pointer.");
×
688
    return OPENMC_E_INVALID_ARGUMENT;
×
689
  }
690

691
  if (model::external_sources.empty()) {
32!
692
    set_errmsg("No external sources have been defined.");
×
693
    return OPENMC_E_OUT_OF_BOUNDS;
×
694
  }
695

696
  auto sites_array = static_cast<SourceSite*>(sites);
32✔
697
  for (size_t i = 0; i < n; ++i) {
214,286✔
698
    sites_array[i] = sample_external_source(seed);
214,254✔
699
  }
700
  return 0;
32✔
701
}
702

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