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

openmc-dev / openmc / 15525148003

09 Jun 2025 02:02AM UTC coverage: 85.129% (-0.003%) from 85.132%
15525148003

Pull #3431

github

web-flow
Merge 248aa020e into 7fda3eb84
Pull Request #3431: Allow spatial constraints on element sources within MeshSource

11 of 13 new or added lines in 2 files covered. (84.62%)

7 existing lines in 2 files now uncovered.

52320 of 61460 relevant lines covered (85.13%)

36532781.55 hits per line

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

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

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

70
unique_ptr<Source> Source::create(pugi::xml_node node)
44,389✔
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,389✔
75
    std::string source_type = get_node_value(node, "type");
43,472✔
76
    if (source_type == "independent") {
43,472✔
77
      return make_unique<IndependentSource>(node);
43,208✔
78
    } else if (source_type == "file") {
264✔
79
      return make_unique<FileSource>(node);
31✔
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,462✔
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,389✔
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,389✔
105
  if (constraints_node) {
44,389✔
106
    node = constraints_node;
1,547✔
107
  }
108

109
  // Check for domains to reject from
110
  if (check_for_node(node, "domain_type")) {
44,389✔
111
    std::string domain_type = get_node_value(node, "domain_type");
415✔
112
    if (domain_type == "cell") {
415✔
113
      domain_type_ = DomainType::CELL;
63✔
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");
415✔
124
    domain_ids_.insert(ids.begin(), ids.end());
415✔
125
  }
415✔
126

127
  if (check_for_node(node, "time_bounds")) {
44,389✔
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,389✔
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,389✔
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,389✔
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,389✔
159

160
SourceSite Source::sample_with_constraints(uint64_t* seed) const
27,920,006✔
161
{
162
  bool accepted = false;
27,920,006✔
163
  static int n_reject = 0;
164
  static int n_accept = 0;
165
  SourceSite site;
27,920,006✔
166

167
  while (!accepted) {
57,188,275✔
168
    // Sample a source site without considering constraints yet
169
    site = this->sample(seed);
29,268,272✔
170

171
    if (constraints_applied()) {
29,268,269✔
172
      accepted = true;
27,358,643✔
173
    } else {
174
      // Check whether sampled site satisfies constraints
175
      accepted = satisfies_spatial_constraints(site.r) &&
1,909,626✔
176
                 satisfies_energy_constraints(site.E) &&
2,481,698✔
177
                 satisfies_time_constraints(site.time);
572,072✔
178
      if (!accepted) {
1,909,626✔
179
        ++n_reject;
1,348,266✔
180
        if (n_reject >= EXTSRC_REJECT_THRESHOLD &&
1,348,266✔
181
            static_cast<double>(n_accept) / n_reject <=
1,238,277✔
182
              EXTSRC_REJECT_FRACTION) {
183
          fatal_error("More than 95% of external source sites sampled were "
×
184
                      "rejected. Please check your source definition.");
185
        }
186

187
        // For the "kill" strategy, accept particle but set weight to 0 so that
188
        // it is terminated immediately
189
        if (rejection_strategy_ == RejectionStrategy::KILL) {
1,348,266✔
190
          accepted = true;
×
191
          site.wgt = 0.0;
×
192
        }
193
      }
194
    }
195
  }
196

197
  // Increment number of accepted samples
198
  ++n_accept;
27,920,003✔
199

200
  return site;
27,920,003✔
201
}
202

203
bool Source::satisfies_energy_constraints(double E) const
27,952,669✔
204
{
205
  return E > energy_bounds_.first && E < energy_bounds_.second;
27,952,669✔
206
}
207

208
bool Source::satisfies_time_constraints(double time) const
572,072✔
209
{
210
  return time > time_bounds_.first && time < time_bounds_.second;
572,072✔
211
}
212

213
bool Source::satisfies_spatial_constraints(Position r) const
31,835,178✔
214
{
215
  GeometryState geom_state;
31,835,178✔
216
  geom_state.r() = r;
31,835,178✔
217
  geom_state.u() = {0.0, 0.0, 1.0};
31,835,178✔
218

219
  // Reject particle if it's not in the geometry at all
220
  bool found = exhaustive_find_cell(geom_state);
31,835,178✔
221
  if (!found)
31,835,178✔
222
    return false;
375,441✔
223

224
  // Check the geometry state against specified domains
225
  bool accepted = true;
31,459,737✔
226
  if (!domain_ids_.empty()) {
31,459,737✔
227
    if (domain_type_ == DomainType::MATERIAL) {
1,426,772✔
228
      auto mat_index = geom_state.material();
×
NEW
229
      if (mat_index == MATERIAL_VOID) {
×
NEW
230
        accepted = false;
×
231
      } else {
UNCOV
232
        accepted = contains(domain_ids_, model::materials[mat_index]->id());
×
233
      }
234
    } else {
235
      for (int i = 0; i < geom_state.n_coord(); i++) {
2,820,544✔
236
        auto id = (domain_type_ == DomainType::CELL)
1,426,772✔
237
                    ? model::cells[geom_state.coord(i).cell]->id_
1,426,772✔
238
                    : model::universes[geom_state.coord(i).universe]->id_;
×
239
        if ((accepted = contains(domain_ids_, id)))
1,426,772✔
240
          break;
33,000✔
241
      }
242
    }
243
  }
244

245
  // Check if spatial site is in fissionable material
246
  if (accepted && only_fissionable_) {
31,459,737✔
247
    // Determine material
248
    auto mat_index = geom_state.material();
1,003,176✔
249
    if (mat_index == MATERIAL_VOID) {
1,003,176✔
250
      accepted = false;
×
251
    } else {
252
      accepted = model::materials[mat_index]->fissionable();
1,003,176✔
253
    }
254
  }
255

256
  return accepted;
31,459,737✔
257
}
31,835,178✔
258

259
//==============================================================================
260
// IndependentSource implementation
261
//==============================================================================
262

263
IndependentSource::IndependentSource(
1,621✔
264
  UPtrSpace space, UPtrAngle angle, UPtrDist energy, UPtrDist time)
1,621✔
265
  : space_ {std::move(space)}, angle_ {std::move(angle)},
1,621✔
266
    energy_ {std::move(energy)}, time_ {std::move(time)}
3,242✔
267
{}
1,621✔
268

269
IndependentSource::IndependentSource(pugi::xml_node node) : Source(node)
44,093✔
270
{
271
  // Check for particle type
272
  if (check_for_node(node, "particle")) {
44,093✔
273
    auto temp_str = get_node_value(node, "particle", true, true);
43,208✔
274
    if (temp_str == "neutron") {
43,208✔
275
      particle_ = ParticleType::neutron;
43,083✔
276
    } else if (temp_str == "photon") {
125✔
277
      particle_ = ParticleType::photon;
125✔
278
      settings::photon_transport = true;
125✔
279
    } else {
280
      fatal_error(std::string("Unknown source particle type: ") + temp_str);
×
281
    }
282
  }
43,208✔
283

284
  // Check for external source file
285
  if (check_for_node(node, "file")) {
44,093✔
286

287
  } else {
288

289
    // Spatial distribution for external source
290
    if (check_for_node(node, "space")) {
44,093✔
291
      space_ = SpatialDistribution::create(node.child("space"));
6,086✔
292
    } else {
293
      // If no spatial distribution specified, make it a point source
294
      space_ = UPtrSpace {new SpatialPoint()};
38,007✔
295
    }
296

297
    // For backwards compatibility, check for only fissionable setting on box
298
    // source
299
    auto space_box = dynamic_cast<SpatialBox*>(space_.get());
44,092✔
300
    if (space_box) {
44,092✔
301
      if (!only_fissionable_) {
3,340✔
302
        only_fissionable_ = space_box->only_fissionable();
2,219✔
303
      }
304
    }
305

306
    // Determine external source angular distribution
307
    if (check_for_node(node, "angle")) {
44,092✔
308
      angle_ = UnitSphereDistribution::create(node.child("angle"));
3,152✔
309
    } else {
310
      angle_ = UPtrAngle {new Isotropic()};
40,940✔
311
    }
312

313
    // Determine external source energy distribution
314
    if (check_for_node(node, "energy")) {
44,092✔
315
      pugi::xml_node node_dist = node.child("energy");
4,192✔
316
      energy_ = distribution_from_xml(node_dist);
4,192✔
317
    } else {
318
      // Default to a Watt spectrum with parameters 0.988 MeV and 2.249 MeV^-1
319
      energy_ = UPtrDist {new Watt(0.988e6, 2.249e-6)};
39,900✔
320
    }
321

322
    // Determine external source time distribution
323
    if (check_for_node(node, "time")) {
44,092✔
324
      pugi::xml_node node_dist = node.child("time");
43✔
325
      time_ = distribution_from_xml(node_dist);
43✔
326
    } else {
327
      // Default to a Constant time T=0
328
      double T[] {0.0};
44,049✔
329
      double p[] {1.0};
44,049✔
330
      time_ = UPtrDist {new Discrete {T, p, 1}};
44,049✔
331
    }
332
  }
333
}
44,092✔
334

335
SourceSite IndependentSource::sample(uint64_t* seed) const
29,204,446✔
336
{
337
  SourceSite site;
29,204,446✔
338
  site.particle = particle_;
29,204,446✔
339

340
  // Repeat sampling source location until a good site has been accepted
341
  bool accepted = false;
29,204,446✔
342
  static int n_reject = 0;
343
  static int n_accept = 0;
344

345
  while (!accepted) {
59,129,995✔
346

347
    // Sample spatial distribution
348
    site.r = space_->sample(seed);
29,925,552✔
349

350
    // Check if sampled position satisfies spatial constraints
351
    accepted = satisfies_spatial_constraints(site.r);
29,925,552✔
352

353
    // Check for rejection
354
    if (!accepted) {
29,925,552✔
355
      ++n_reject;
721,109✔
356
      if (n_reject >= EXTSRC_REJECT_THRESHOLD &&
721,109✔
357
          static_cast<double>(n_accept) / n_reject <= EXTSRC_REJECT_FRACTION) {
144,988✔
358
        fatal_error("More than 95% of external source sites sampled were "
3✔
359
                    "rejected. Please check your external source's spatial "
360
                    "definition.");
361
      }
362
    }
363
  }
364

365
  // Sample angle
366
  site.u = angle_->sample(seed);
29,204,443✔
367

368
  // Sample energy and time for neutron and photon sources
369
  if (settings::solver_type != SolverType::RANDOM_RAY) {
29,204,443✔
370
    // Check for monoenergetic source above maximum particle energy
371
    auto p = static_cast<int>(particle_);
27,358,643✔
372
    auto energy_ptr = dynamic_cast<Discrete*>(energy_.get());
27,358,643✔
373
    if (energy_ptr) {
27,358,643✔
374
      auto energies = xt::adapt(energy_ptr->x());
16,770,827✔
375
      if (xt::any(energies > data::energy_max[p])) {
16,770,827✔
376
        fatal_error("Source energy above range of energies of at least "
×
377
                    "one cross section table");
378
      }
379
    }
16,770,827✔
380

381
    while (true) {
382
      // Sample energy spectrum
383
      site.E = energy_->sample(seed);
27,358,643✔
384

385
      // Resample if energy falls above maximum particle energy
386
      if (site.E < data::energy_max[p] and
54,717,286✔
387
          (satisfies_energy_constraints(site.E)))
27,358,643✔
388
        break;
27,358,643✔
389

390
      n_reject++;
×
391
      if (n_reject >= EXTSRC_REJECT_THRESHOLD &&
×
392
          static_cast<double>(n_accept) / n_reject <= EXTSRC_REJECT_FRACTION) {
393
        fatal_error(
×
394
          "More than 95% of external source sites sampled were "
395
          "rejected. Please check your external source energy spectrum "
396
          "definition.");
397
      }
398
    }
399

400
    // Sample particle creation time
401
    site.time = time_->sample(seed);
27,358,643✔
402
  }
403

404
  // Increment number of accepted samples
405
  ++n_accept;
29,204,443✔
406

407
  return site;
29,204,443✔
408
}
409

410
//==============================================================================
411
// FileSource implementation
412
//==============================================================================
413

414
FileSource::FileSource(pugi::xml_node node) : Source(node)
63✔
415
{
416
  auto path = get_node_value(node, "file", false, true);
63✔
417
  if (ends_with(path, ".mcpl") || ends_with(path, ".mcpl.gz")) {
63✔
418
    sites_ = mcpl_source_sites(path);
16✔
419
  } else {
420
    this->load_sites_from_file(path);
47✔
421
  }
422
}
54✔
423

424
FileSource::FileSource(const std::string& path)
16✔
425
{
426
  load_sites_from_file(path);
16✔
427
}
16✔
428

429
void FileSource::load_sites_from_file(const std::string& path)
63✔
430
{
431
  // Check if source file exists
432
  if (!file_exists(path)) {
63✔
433
    fatal_error(fmt::format("Source file '{}' does not exist.", path));
×
434
  }
435

436
  // Read the source from a binary file instead of sampling from some
437
  // assumed source distribution
438
  write_message(6, "Reading source file from {}...", path);
63✔
439

440
  // Open the binary file
441
  hid_t file_id = file_open(path, 'r', true);
63✔
442

443
  // Check to make sure this is a source file
444
  std::string filetype;
63✔
445
  read_attribute(file_id, "filetype", filetype);
63✔
446
  if (filetype != "source" && filetype != "statepoint") {
63✔
447
    fatal_error("Specified starting source file not a source file type.");
×
448
  }
449

450
  // Read in the source particles
451
  read_source_bank(file_id, sites_, false);
63✔
452

453
  // Close file
454
  file_close(file_id);
54✔
455
}
54✔
456

457
SourceSite FileSource::sample(uint64_t* seed) const
175,996✔
458
{
459
  // Sample a particle randomly from list
460
  size_t i_site = sites_.size() * prn(seed);
175,996✔
461
  return sites_[i_site];
175,996✔
462
}
463

464
//==============================================================================
465
// CompiledSourceWrapper implementation
466
//==============================================================================
467

468
CompiledSourceWrapper::CompiledSourceWrapper(pugi::xml_node node) : Source(node)
32✔
469
{
470
  // Get shared library path and parameters
471
  auto path = get_node_value(node, "library", false, true);
32✔
472
  std::string parameters;
32✔
473
  if (check_for_node(node, "parameters")) {
32✔
474
    parameters = get_node_value(node, "parameters", false, true);
16✔
475
  }
476
  setup(path, parameters);
32✔
477
}
32✔
478

479
void CompiledSourceWrapper::setup(
32✔
480
  const std::string& path, const std::string& parameters)
481
{
482
#ifdef HAS_DYNAMIC_LINKING
483
  // Open the library
484
  shared_library_ = dlopen(path.c_str(), RTLD_LAZY);
32✔
485
  if (!shared_library_) {
32✔
486
    fatal_error("Couldn't open source library " + path);
×
487
  }
488

489
  // reset errors
490
  dlerror();
32✔
491

492
  // get the function to create the custom source from the library
493
  auto create_compiled_source = reinterpret_cast<create_compiled_source_t*>(
494
    dlsym(shared_library_, "openmc_create_source"));
32✔
495

496
  // check for any dlsym errors
497
  auto dlsym_error = dlerror();
32✔
498
  if (dlsym_error) {
32✔
499
    std::string error_msg = fmt::format(
500
      "Couldn't open the openmc_create_source symbol: {}", dlsym_error);
×
501
    dlclose(shared_library_);
×
502
    fatal_error(error_msg);
×
503
  }
×
504

505
  // create a pointer to an instance of the custom source
506
  compiled_source_ = create_compiled_source(parameters);
32✔
507

508
#else
509
  fatal_error("Custom source libraries have not yet been implemented for "
510
              "non-POSIX systems");
511
#endif
512
}
32✔
513

514
CompiledSourceWrapper::~CompiledSourceWrapper()
64✔
515
{
516
  // Make sure custom source is cleared before closing shared library
517
  if (compiled_source_.get())
32✔
518
    compiled_source_.reset();
32✔
519

520
#ifdef HAS_DYNAMIC_LINKING
521
  dlclose(shared_library_);
32✔
522
#else
523
  fatal_error("Custom source libraries have not yet been implemented for "
524
              "non-POSIX systems");
525
#endif
526
}
64✔
527

32✔
528
//==============================================================================
529
// MeshSource implementation
530
//==============================================================================
531

532
MeshSource::MeshSource(pugi::xml_node node) : Source(node)
533
{
534
  int32_t mesh_id = stoi(get_node_value(node, "mesh"));
535
  int32_t mesh_idx = model::mesh_map.at(mesh_id);
536
  const auto& mesh = model::meshes[mesh_idx];
537

538
  std::vector<double> strengths;
539
  // read all source distributions and populate strengths vector for MeshSpatial
32✔
540
  // object
32✔
541
  for (auto source_node : node.children("source")) {
542
    auto src = Source::create(source_node);
543
    if (auto ptr = dynamic_cast<IndependentSource*>(src.get())) {
32✔
544
      src.release();
32✔
545
      sources_.emplace_back(ptr);
546
    } else {
547
      fatal_error(
32✔
548
        "The source assigned to each element must be an IndependentSource.");
549
    }
550
    strengths.push_back(sources_.back()->strength());
551
  }
552

32✔
553
  // Set spatial distributions for each mesh element
554
  for (int elem_index = 0; elem_index < sources_.size(); ++elem_index) {
555
    sources_[elem_index]->set_space(
556
      std::make_unique<MeshElementSpatial>(*mesh, elem_index));
557
  }
558

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

567
  space_ = std::make_unique<MeshSpatial>(mesh_idx, strengths);
37,653✔
568
}
37,452✔
569

37,452✔
570
SourceSite MeshSource::sample(uint64_t* seed) const
37,452✔
571
{
37,452✔
572
  // Sample a mesh element based on the relative strengths
UNCOV
573
  int32_t element = space_->sample_element_index(seed);
×
574

575
  // Sample the distribution for the specific mesh element; note that the
576
  // spatial distribution has been set for each element using MeshElementSpatial
37,452✔
577
  return source(element)->sample_with_constraints(seed);
37,452✔
578
}
579

580
//==============================================================================
37,653✔
581
// Non-member functions
74,904✔
582
//==============================================================================
74,904✔
583

584
void initialize_source()
585
{
586
  write_message("Initializing source particles...", 5);
587

201✔
UNCOV
588
// Generation source sites from specified distribution in user input
×
589
#pragma omp parallel for
UNCOV
590
  for (int64_t i = 0; i < simulation::work_per_rank; ++i) {
×
591
    // initialize random number seed
592
    int64_t id = simulation::total_gen * settings::n_particles +
593
                 simulation::work_index[mpi::rank] + i + 1;
201✔
594
    uint64_t seed = init_seed(id, STREAM_SOURCE);
201✔
595

596
    // sample external source distribution
1,513,630✔
597
    simulation::source_bank[i] = sample_external_source(&seed);
598
  }
599

1,513,630✔
600
  // Write out initial source
601
  if (settings::write_initial_source) {
602
    write_message("Writing out initial source...", 5);
603
    std::string filename = settings::path_output + "initial_source.h5";
1,513,630✔
604
    hid_t file_id = file_open(filename, 'w', true);
605
    write_source_bank(file_id, simulation::source_bank, simulation::work_index);
606
    file_close(file_id);
607
  }
608
}
609

610
SourceSite sample_external_source(uint64_t* seed)
3,411✔
611
{
612
  // Sample from among multiple source distributions
3,411✔
613
  int i = 0;
614
  int n_sources = model::external_sources.size();
615
  if (n_sources > 1) {
616
    if (settings::uniform_source_sampling) {
1,047,964✔
617
      i = prn(seed) * n_sources;
618
    } else {
2,092,900✔
619
      i = model::external_sources_probability.sample(seed);
1,046,450✔
620
    }
1,046,450✔
621
  }
622

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

626
  // For uniform source sampling, multiply the weight by the ratio of the actual
627
  // probability of sampling source i to the biased probability of sampling
3,411✔
628
  // source i, which is (strength_i / total_strength) / (1 / n)
×
629
  if (n_sources > 1 && settings::uniform_source_sampling) {
×
630
    double total_strength = model::external_sources_probability.integral();
×
631
    site.wgt *=
×
632
      model::external_sources[i]->strength() * n_sources / total_strength;
×
633
  }
634

3,411✔
635
  // If running in MG, convert site.E to group
636
  if (!settings::run_CE) {
26,406,376✔
637
    site.E = lower_bound_index(data::mg.rev_energy_bins_.begin(),
638
      data::mg.rev_energy_bins_.end(), site.E);
639
    site.E = data::mg.num_energy_groups_ - site.E - 1.;
26,406,376✔
640
  }
26,406,376✔
641

26,406,376✔
642
  return site;
146,300✔
643
}
2,200✔
644

645
void free_memory_source()
144,100✔
646
{
647
  model::external_sources.clear();
648
}
649

650
//==============================================================================
26,406,376✔
651
// C API
652
//==============================================================================
653

654
extern "C" int openmc_sample_external_source(
655
  size_t n, uint64_t* seed, void* sites)
26,406,373✔
656
{
2,200✔
657
  if (!sites || !seed) {
2,200✔
658
    set_errmsg("Received null pointer.");
2,200✔
659
    return OPENMC_E_INVALID_ARGUMENT;
660
  }
661

662
  if (model::external_sources.empty()) {
26,406,373✔
663
    set_errmsg("No external sources have been defined.");
1,742,400✔
664
    return OPENMC_E_OUT_OF_BOUNDS;
665
  }
1,742,400✔
666

667
  auto sites_array = static_cast<SourceSite*>(sites);
668
  for (size_t i = 0; i < n; ++i) {
26,406,373✔
669
    sites_array[i] = sample_external_source(seed);
670
  }
671
  return 0;
6,735✔
672
}
673

6,735✔
674
} // namespace openmc
6,735✔
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

© 2025 Coveralls, Inc