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

openmc-dev / openmc / 21887062786

10 Feb 2026 11:55PM UTC coverage: 81.899% (-0.1%) from 82.009%
21887062786

Pull #3493

github

web-flow
Merge 320b68711 into 3f20a5e22
Pull Request #3493: Implement vector fitting to replace external `vectfit` package

17361 of 24292 branches covered (71.47%)

Branch coverage included in aggregate %.

185 of 218 new or added lines in 3 files covered. (84.86%)

2770 existing lines in 69 files now uncovered.

56369 of 65733 relevant lines covered (85.75%)

51144917.16 hits per line

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

79.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/xml_interface.h"
37

38
namespace openmc {
39

40
namespace {
41

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

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

52
} // namespace
53

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

58
namespace model {
59

60
vector<unique_ptr<Source>> external_sources;
61

62
DiscreteIndex external_sources_probability;
63

64
} // namespace model
65

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

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

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

84
unique_ptr<Source> Source::create(pugi::xml_node node)
44,989✔
85
{
86
  // if the source type is present, use it to determine the type
87
  // of object to create
88
  if (check_for_node(node, "type")) {
44,989✔
89
    std::string source_type = get_node_value(node, "type");
44,195✔
90
    if (source_type == "independent") {
44,195✔
91
      return make_unique<IndependentSource>(node);
43,956✔
92
    } else if (source_type == "file") {
239✔
93
      return make_unique<FileSource>(node);
28✔
94
    } else if (source_type == "compiled") {
211✔
95
      return make_unique<CompiledSourceWrapper>(node);
28✔
96
    } else if (source_type == "mesh") {
183!
97
      return make_unique<MeshSource>(node);
183✔
98
    } else {
UNCOV
99
      fatal_error(fmt::format("Invalid source type '{}' found.", source_type));
×
100
    }
101
  } else {
44,186✔
102
    // support legacy source format
103
    if (check_for_node(node, "file")) {
794✔
104
      return make_unique<FileSource>(node);
28✔
105
    } else if (check_for_node(node, "library")) {
766!
UNCOV
106
      return make_unique<CompiledSourceWrapper>(node);
×
107
    } else {
108
      return make_unique<IndependentSource>(node);
766✔
109
    }
110
  }
111
}
112

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

123
  // Check for domains to reject from
124
  if (check_for_node(node, "domain_type")) {
44,989✔
125
    std::string domain_type = get_node_value(node, "domain_type");
419✔
126
    if (domain_type == "cell") {
419✔
127
      domain_type_ = DomainType::CELL;
83✔
128
    } else if (domain_type == "material") {
336✔
129
      domain_type_ = DomainType::MATERIAL;
14✔
130
    } else if (domain_type == "universe") {
322!
131
      domain_type_ = DomainType::UNIVERSE;
322✔
132
    } else {
UNCOV
133
      fatal_error(
×
UNCOV
134
        std::string("Unrecognized domain type for constraint: " + domain_type));
×
135
    }
136

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

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

156
  if (check_for_node(node, "fissionable")) {
44,989✔
157
    only_fissionable_ = get_node_value_bool(node, "fissionable");
1,296✔
158
  }
159

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

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

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

191
SourceSite Source::sample_with_constraints(uint64_t* seed) const
31,286,277✔
192
{
193
  bool accepted = false;
31,286,277✔
194
  static int64_t n_reject = 0;
195
  static int64_t n_accept = 0;
196
  SourceSite site;
31,286,277✔
197

198
  while (!accepted) {
63,777,358✔
199
    // Sample a source site without considering constraints yet
200
    site = this->sample(seed);
32,491,084✔
201

202
    if (constraints_applied()) {
32,491,081✔
203
      accepted = true;
30,675,944✔
204
    } else {
205
      // Check whether sampled site satisfies constraints
206
      accepted = satisfies_spatial_constraints(site.r) &&
1,815,137✔
207
                 satisfies_energy_constraints(site.E) &&
2,435,127✔
208
                 satisfies_time_constraints(site.time);
619,990✔
209
      if (!accepted) {
1,815,137✔
210
        // Increment number of rejections and check against minimum fraction
211
        ++n_reject;
1,204,807✔
212
        check_rejection_fraction(n_reject, n_accept);
1,204,807✔
213

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

224
  // Increment number of accepted samples
225
  ++n_accept;
31,286,274✔
226

227
  return site;
31,286,274✔
228
}
229

230
bool Source::satisfies_energy_constraints(double E) const
31,315,561✔
231
{
232
  return E > energy_bounds_.first && E < energy_bounds_.second;
31,315,561!
233
}
234

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

240
bool Source::satisfies_spatial_constraints(Position r) const
35,593,881✔
241
{
242
  GeometryState geom_state;
35,593,881✔
243
  geom_state.r() = r;
35,593,881✔
244
  geom_state.u() = {0.0, 0.0, 1.0};
35,593,881✔
245

246
  // Reject particle if it's not in the geometry at all
247
  bool found = exhaustive_find_cell(geom_state);
35,593,881✔
248
  if (!found)
35,593,881✔
249
    return false;
445,793✔
250

251
  // Check the geometry state against specified domains
252
  bool accepted = true;
35,148,088✔
253
  if (!domain_ids_.empty()) {
35,148,088✔
254
    if (domain_type_ == DomainType::MATERIAL) {
1,774,246!
UNCOV
255
      auto mat_index = geom_state.material();
×
UNCOV
256
      if (mat_index == MATERIAL_VOID) {
×
UNCOV
257
        accepted = false;
×
258
      } else {
UNCOV
259
        accepted = contains(domain_ids_, model::materials[mat_index]->id());
×
260
      }
261
    } else {
262
      for (int i = 0; i < geom_state.n_coord(); i++) {
3,412,072✔
263
        auto id =
264
          (domain_type_ == DomainType::CELL)
1,774,246✔
265
            ? model::cells[geom_state.coord(i).cell()].get()->id_
1,774,246!
UNCOV
266
            : model::universes[geom_state.coord(i).universe()].get()->id_;
×
267
        if ((accepted = contains(domain_ids_, id)))
1,774,246✔
268
          break;
136,420✔
269
      }
270
    }
271
  }
272

273
  // Check if spatial site is in fissionable material
274
  if (accepted && only_fissionable_) {
35,148,088✔
275
    // Determine material
276
    auto mat_index = geom_state.material();
978,211✔
277
    if (mat_index == MATERIAL_VOID) {
978,211!
UNCOV
278
      accepted = false;
×
279
    } else {
280
      accepted = model::materials[mat_index]->fissionable();
978,211✔
281
    }
282
  }
283

284
  return accepted;
35,148,088✔
285
}
35,593,881✔
286

287
//==============================================================================
288
// IndependentSource implementation
289
//==============================================================================
290

291
IndependentSource::IndependentSource(
1,848✔
292
  UPtrSpace space, UPtrAngle angle, UPtrDist energy, UPtrDist time)
1,848✔
293
  : space_ {std::move(space)}, angle_ {std::move(angle)},
1,848✔
294
    energy_ {std::move(energy)}, time_ {std::move(time)}
3,696✔
295
{}
1,848✔
296

297
IndependentSource::IndependentSource(pugi::xml_node node) : Source(node)
44,722✔
298
{
299
  // Check for particle type
300
  if (check_for_node(node, "particle")) {
44,722✔
301
    auto temp_str = get_node_value(node, "particle", false, true);
43,956✔
302
    particle_ = ParticleType(temp_str);
43,956✔
303
    if (particle_ == ParticleType::photon() ||
87,780✔
304
        particle_ == ParticleType::electron() ||
87,780!
305
        particle_ == ParticleType::positron()) {
43,810✔
306
      settings::photon_transport = true;
146✔
307
    }
308
  }
43,956✔
309
  validate_particle_type(particle_, "IndependentSource");
44,722✔
310

311
  // Check for external source file
312
  if (check_for_node(node, "file")) {
44,722!
313

314
  } else {
315

316
    // Spatial distribution for external source
317
    if (check_for_node(node, "space")) {
44,722✔
318
      space_ = SpatialDistribution::create(node.child("space"));
6,800✔
319
    } else {
320
      // If no spatial distribution specified, make it a point source
321
      space_ = UPtrSpace {new SpatialPoint()};
37,922✔
322
    }
323

324
    // For backwards compatibility, check for only fissionable setting on box
325
    // source
326
    auto space_box = dynamic_cast<SpatialBox*>(space_.get());
44,721!
327
    if (space_box) {
44,721✔
328
      if (!only_fissionable_) {
3,683✔
329
        only_fissionable_ = space_box->only_fissionable();
2,387✔
330
      }
331
    }
332

333
    // Determine external source angular distribution
334
    if (check_for_node(node, "angle")) {
44,721✔
335
      angle_ = UnitSphereDistribution::create(node.child("angle"));
3,044✔
336
    } else {
337
      angle_ = UPtrAngle {new Isotropic()};
41,677✔
338
    }
339

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

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

362
SourceSite IndependentSource::sample(uint64_t* seed) const
32,592,667✔
363
{
364
  SourceSite site;
32,592,667✔
365
  site.particle = particle_;
32,592,667✔
366
  double r_wgt = 1.0;
32,592,667✔
367
  double E_wgt = 1.0;
32,592,667✔
368

369
  // Repeat sampling source location until a good site has been accepted
370
  bool accepted = false;
32,592,667✔
371
  static int64_t n_reject = 0;
372
  static int64_t n_accept = 0;
373

374
  while (!accepted) {
66,371,408✔
375

376
    // Sample spatial distribution
377
    auto [r, r_wgt_temp] = space_->sample(seed);
33,778,744✔
378
    site.r = r;
33,778,744✔
379
    r_wgt = r_wgt_temp;
33,778,744✔
380

381
    // Check if sampled position satisfies spatial constraints
382
    accepted = satisfies_spatial_constraints(site.r);
33,778,744✔
383

384
    // Check for rejection
385
    if (!accepted) {
33,778,744✔
386
      ++n_reject;
1,186,080✔
387
      check_rejection_fraction(n_reject, n_accept);
1,186,080✔
388
    }
389
  }
390

391
  // Sample angle
392
  auto [u, u_wgt] = angle_->sample(seed);
32,592,664✔
393
  site.u = u;
32,592,664✔
394

395
  site.wgt = r_wgt * u_wgt;
32,592,664✔
396

397
  // Sample energy and time for neutron and photon sources
398
  if (settings::solver_type != SolverType::RANDOM_RAY) {
32,592,664✔
399
    // Check for monoenergetic source above maximum particle energy
400
    auto p = particle_.transport_index();
30,675,944✔
401
    auto energy_ptr = dynamic_cast<Discrete*>(energy_.get());
30,675,944!
402
    if (energy_ptr) {
30,675,944✔
403
      auto energies = xt::adapt(energy_ptr->x());
16,547,190✔
404
      if (xt::any(energies > data::energy_max[p])) {
16,547,190!
UNCOV
405
        fatal_error("Source energy above range of energies of at least "
×
406
                    "one cross section table");
407
      }
408
    }
16,547,190✔
409

410
    while (true) {
411
      // Sample energy spectrum
412
      auto [E, E_wgt_temp] = energy_->sample(seed);
30,675,944✔
413
      site.E = E;
30,675,944✔
414
      E_wgt = E_wgt_temp;
30,675,944✔
415

416
      // Resample if energy falls above maximum particle energy
417
      if (site.E < data::energy_max[p] &&
61,351,888!
418
          (satisfies_energy_constraints(site.E)))
30,675,944!
419
        break;
30,675,944✔
420

UNCOV
421
      n_reject++;
×
UNCOV
422
      check_rejection_fraction(n_reject, n_accept);
×
UNCOV
423
    }
×
424

425
    // Sample particle creation time
426
    auto [time, time_wgt] = time_->sample(seed);
30,675,944✔
427
    site.time = time;
30,675,944✔
428

429
    site.wgt *= (E_wgt * time_wgt);
30,675,944✔
430
  }
431

432
  // Increment number of accepted samples
433
  ++n_accept;
32,592,664✔
434

435
  return site;
65,185,328✔
436
}
437

438
//==============================================================================
439
// FileSource implementation
440
//==============================================================================
441

442
FileSource::FileSource(pugi::xml_node node) : Source(node)
56✔
443
{
444
  auto path = get_node_value(node, "file", false, true);
56✔
445
  load_sites_from_file(path);
56✔
446
}
48✔
447

448
FileSource::FileSource(const std::string& path)
28✔
449
{
450
  load_sites_from_file(path);
28✔
451
}
28✔
452

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

464
    write_message(6, "Reading source file from {}...", path);
56✔
465

466
    // Open the binary file
467
    hid_t file_id = file_open(path, 'r', true);
56✔
468

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

476
    // Read in the source particles
477
    read_source_bank(file_id, sites_, false);
56✔
478

479
    // Close file
480
    file_close(file_id);
48✔
481
  }
48✔
482

483
  // Make sure particles in source file have valid types
484
  for (const auto& site : this->sites_) {
156,086✔
485
    validate_particle_type(site.particle, "FileSource");
156,010✔
486
  }
487
}
76✔
488

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

496
//==============================================================================
497
// CompiledSourceWrapper implementation
498
//==============================================================================
499

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

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

521
  // reset errors
522
  dlerror();
28✔
523

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

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

537
  // create a pointer to an instance of the custom source
538
  compiled_source_ = create_compiled_source(parameters);
28✔
539

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

546
CompiledSourceWrapper::~CompiledSourceWrapper()
56✔
547
{
548
  // Make sure custom source is cleared before closing shared library
549
  if (compiled_source_.get())
28!
550
    compiled_source_.reset();
28✔
551

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

560
//==============================================================================
561
// MeshElementSpatial implementation
562
//==============================================================================
563

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

569
//==============================================================================
570
// MeshSource implementation
571
//==============================================================================
572

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

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

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

600
  // Make sure sources use valid particle types
601
  for (const auto& src : sources_) {
37,503✔
602
    validate_particle_type(src->particle_type(), "MeshSource");
37,320✔
603
  }
604

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

613
  space_ = std::make_unique<MeshSpatial>(mesh_idx, strengths);
183✔
614
}
183✔
615

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

621
  // Sample the distribution for the specific mesh element; note that the
622
  // spatial distribution has been set for each element using MeshElementSpatial
623
  return source(element)->sample_with_constraints(seed);
1,355,550✔
624
}
625

626
//==============================================================================
627
// Non-member functions
628
//==============================================================================
629

630
void initialize_source()
3,528✔
631
{
632
  write_message("Initializing source particles...", 5);
3,528✔
633

634
// Generation source sites from specified distribution in user input
635
#pragma omp parallel for
636
  for (int64_t i = 0; i < simulation::work_per_rank; ++i) {
960,179✔
637
    // initialize random number seed
638
    int64_t id = simulation::total_gen * settings::n_particles +
1,917,704✔
639
                 simulation::work_index[mpi::rank] + i + 1;
958,852✔
640
    uint64_t seed = init_seed(id, STREAM_SOURCE);
958,852✔
641

642
    // sample external source distribution
643
    simulation::source_bank[i] = sample_external_source(&seed);
958,852✔
644
  }
645

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

656
SourceSite sample_external_source(uint64_t* seed)
29,930,727✔
657
{
658
  // Sample from among multiple source distributions
659
  int i = 0;
29,930,727✔
660
  int n_sources = model::external_sources.size();
29,930,727✔
661
  if (n_sources > 1) {
29,930,727✔
662
    if (settings::uniform_source_sampling) {
3,235,000✔
663
      i = prn(seed) * n_sources;
2,000✔
664
    } else {
665
      i = model::external_sources_probability.sample(seed);
3,233,000✔
666
    }
667
  }
668

669
  // Sample source site from i-th source distribution
670
  SourceSite site {model::external_sources[i]->sample_with_constraints(seed)};
29,930,727✔
671

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

681
  // If running in MG, convert site.E to group
682
  if (!settings::run_CE) {
29,930,724✔
683
    site.E = lower_bound_index(data::mg.rev_energy_bins_.begin(),
1,584,000✔
684
      data::mg.rev_energy_bins_.end(), site.E);
685
    site.E = data::mg.num_energy_groups_ - site.E - 1.;
1,584,000✔
686
  }
687

688
  return site;
29,930,724✔
689
}
690

691
void free_memory_source()
7,505✔
692
{
693
  model::external_sources.clear();
7,505✔
694
}
7,505✔
695

696
//==============================================================================
697
// C API
698
//==============================================================================
699

700
extern "C" int openmc_sample_external_source(
923✔
701
  size_t n, uint64_t* seed, void* sites)
702
{
703
  if (!sites || !seed) {
923!
UNCOV
704
    set_errmsg("Received null pointer.");
×
UNCOV
705
    return OPENMC_E_INVALID_ARGUMENT;
×
706
  }
707

708
  if (model::external_sources.empty()) {
923!
UNCOV
709
    set_errmsg("No external sources have been defined.");
×
UNCOV
710
    return OPENMC_E_OUT_OF_BOUNDS;
×
711
  }
712

713
  auto sites_array = static_cast<SourceSite*>(sites);
923✔
714
  for (size_t i = 0; i < n; ++i) {
2,743,493✔
715
    sites_array[i] = sample_external_source(seed);
2,742,570✔
716
  }
717
  return 0;
923✔
718
}
719

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