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

openmc-dev / openmc / 11975265553

22 Nov 2024 03:16PM UTC coverage: 84.823% (+0.006%) from 84.817%
11975265553

push

github

web-flow
Statistical weights in IndependentSource (#3195)

Co-authored-by: Paul Wilson <paul.wilson@wisc.edu>
Co-authored-by: Paul Romano <paul.k.romano@gmail.com>

28 of 32 new or added lines in 5 files covered. (87.5%)

3 existing lines in 1 file now uncovered.

49853 of 58773 relevant lines covered (84.82%)

34037081.73 hits per line

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

85.58
/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 <algorithm> // 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

49
//==============================================================================
50
// Source implementation
51
//==============================================================================
52

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

63
  // Check for additional defined constraints
64
  read_constraints(node);
44,426✔
65
}
44,426✔
66

67
unique_ptr<Source> Source::create(pugi::xml_node node)
44,426✔
68
{
69
  // if the source type is present, use it to determine the type
70
  // of object to create
71
  if (check_for_node(node, "type")) {
44,426✔
72
    std::string source_type = get_node_value(node, "type");
43,267✔
73
    if (source_type == "independent") {
43,267✔
74
      return make_unique<IndependentSource>(node);
42,968✔
75
    } else if (source_type == "file") {
299✔
76
      return make_unique<FileSource>(node);
46✔
77
    } else if (source_type == "compiled") {
253✔
78
      return make_unique<CompiledSourceWrapper>(node);
34✔
79
    } else if (source_type == "mesh") {
219✔
80
      return make_unique<MeshSource>(node);
219✔
81
    } else {
82
      fatal_error(fmt::format("Invalid source type '{}' found.", source_type));
×
83
    }
84
  } else {
43,256✔
85
    // support legacy source format
86
    if (check_for_node(node, "file")) {
1,159✔
87
      return make_unique<FileSource>(node);
34✔
88
    } else if (check_for_node(node, "library")) {
1,125✔
89
      return make_unique<CompiledSourceWrapper>(node);
×
90
    } else {
91
      return make_unique<IndependentSource>(node);
1,125✔
92
    }
93
  }
94
}
95

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

106
  // Check for domains to reject from
107
  if (check_for_node(node, "domain_type")) {
44,426✔
108
    std::string domain_type = get_node_value(node, "domain_type");
300✔
109
    if (domain_type == "cell") {
300✔
110
      domain_type_ = DomainType::CELL;
45✔
111
    } else if (domain_type == "material") {
255✔
112
      domain_type_ = DomainType::MATERIAL;
17✔
113
    } else if (domain_type == "universe") {
238✔
114
      domain_type_ = DomainType::UNIVERSE;
238✔
115
    } else {
116
      fatal_error(
×
117
        std::string("Unrecognized domain type for constraint: " + domain_type));
×
118
    }
119

120
    auto ids = get_node_array<int>(node, "domain_ids");
300✔
121
    domain_ids_.insert(ids.begin(), ids.end());
300✔
122
  }
300✔
123

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

139
  if (check_for_node(node, "fissionable")) {
44,426✔
140
    only_fissionable_ = get_node_value_bool(node, "fissionable");
1,101✔
141
  }
142

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

157
SourceSite Source::sample_with_constraints(uint64_t* seed) const
16,635,327✔
158
{
159
  bool accepted = false;
16,635,327✔
160
  static int n_reject = 0;
161
  static int n_accept = 0;
162
  SourceSite site;
16,635,327✔
163

164
  while (!accepted) {
33,306,191✔
165
    // Sample a source site without considering constraints yet
166
    site = this->sample(seed);
16,670,868✔
167

168
    if (constraints_applied()) {
16,670,864✔
169
      accepted = true;
16,238,843✔
170
    } else {
171
      // Check whether sampled site satisfies constraints
172
      accepted = satisfies_spatial_constraints(site.r) &&
432,021✔
173
                 satisfies_energy_constraints(site.E) &&
840,308✔
174
                 satisfies_time_constraints(site.time);
408,287✔
175
      if (!accepted) {
432,021✔
176
        ++n_reject;
35,541✔
177
        if (n_reject >= EXTSRC_REJECT_THRESHOLD &&
35,541✔
178
            static_cast<double>(n_accept) / n_reject <=
179
              EXTSRC_REJECT_FRACTION) {
180
          fatal_error("More than 95% of external source sites sampled were "
×
181
                      "rejected. Please check your source definition.");
182
        }
183

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

194
  // Increment number of accepted samples
195
  ++n_accept;
16,635,323✔
196

197
  return site;
16,635,323✔
198
}
199

200
bool Source::satisfies_energy_constraints(double E) const
16,670,864✔
201
{
202
  return E > energy_bounds_.first && E < energy_bounds_.second;
16,670,864✔
203
}
204

205
bool Source::satisfies_time_constraints(double time) const
612,437✔
206
{
207
  return time > time_bounds_.first && time < time_bounds_.second;
612,437✔
208
}
209

210
bool Source::satisfies_spatial_constraints(Position r) const
19,464,420✔
211
{
212
  GeometryState geom_state;
19,464,420✔
213
  geom_state.r() = r;
19,464,420✔
214
  geom_state.u() = {0.0, 0.0, 1.0};
19,464,420✔
215

216
  // Reject particle if it's not in the geometry at all
217
  bool found = exhaustive_find_cell(geom_state);
19,464,420✔
218
  if (!found)
19,464,420✔
219
    return false;
398,976✔
220

221
  // Check the geometry state against specified domains
222
  bool accepted = true;
19,065,444✔
223
  if (!domain_ids_.empty()) {
19,065,444✔
224
    if (domain_type_ == DomainType::MATERIAL) {
1,528,672✔
225
      auto mat_index = geom_state.material();
×
226
      if (mat_index != MATERIAL_VOID) {
×
227
        accepted = contains(domain_ids_, model::materials[mat_index]->id());
×
228
      }
229
    } else {
230
      for (int i = 0; i < geom_state.n_coord(); i++) {
3,033,344✔
231
        auto id = (domain_type_ == DomainType::CELL)
1,528,672✔
232
                    ? model::cells[geom_state.coord(i).cell]->id_
1,528,672✔
233
                    : model::universes[geom_state.coord(i).universe]->id_;
×
234
        if ((accepted = contains(domain_ids_, id)))
1,528,672✔
235
          break;
24,000✔
236
      }
237
    }
238
  }
239

240
  // Check if spatial site is in fissionable material
241
  if (accepted && only_fissionable_) {
19,065,444✔
242
    // Determine material
243
    auto mat_index = geom_state.material();
1,141,048✔
244
    if (mat_index == MATERIAL_VOID) {
1,141,048✔
245
      accepted = false;
×
246
    } else {
247
      accepted = model::materials[mat_index]->fissionable();
1,141,048✔
248
    }
249
  }
250

251
  return accepted;
19,065,444✔
252
}
19,464,420✔
253

254
//==============================================================================
255
// IndependentSource implementation
256
//==============================================================================
257

258
IndependentSource::IndependentSource(
1,711✔
259
  UPtrSpace space, UPtrAngle angle, UPtrDist energy, UPtrDist time)
1,711✔
260
  : space_ {std::move(space)}, angle_ {std::move(angle)},
1,711✔
261
    energy_ {std::move(energy)}, time_ {std::move(time)}
3,422✔
262
{}
1,711✔
263

264
IndependentSource::IndependentSource(pugi::xml_node node) : Source(node)
44,093✔
265
{
266
  // Check for particle type
267
  if (check_for_node(node, "particle")) {
44,093✔
268
    auto temp_str = get_node_value(node, "particle", true, true);
42,968✔
269
    if (temp_str == "neutron") {
42,968✔
270
      particle_ = ParticleType::neutron;
42,869✔
271
    } else if (temp_str == "photon") {
99✔
272
      particle_ = ParticleType::photon;
99✔
273
      settings::photon_transport = true;
99✔
274
    } else {
275
      fatal_error(std::string("Unknown source particle type: ") + temp_str);
×
276
    }
277
  }
42,968✔
278

279
  // Check for external source file
280
  if (check_for_node(node, "file")) {
44,093✔
281

282
  } else {
283

284
    // Spatial distribution for external source
285
    if (check_for_node(node, "space")) {
44,093✔
286
      space_ = SpatialDistribution::create(node.child("space"));
6,153✔
287
    } else {
288
      // If no spatial distribution specified, make it a point source
289
      space_ = UPtrSpace {new SpatialPoint()};
37,940✔
290
    }
291

292
    // For backwards compatibility, check for only fissionable setting on box
293
    // source
294
    auto space_box = dynamic_cast<SpatialBox*>(space_.get());
44,092✔
295
    if (space_box) {
44,092✔
296
      if (!only_fissionable_) {
3,389✔
297
        only_fissionable_ = space_box->only_fissionable();
2,288✔
298
      }
299
    }
300

301
    // Determine external source angular distribution
302
    if (check_for_node(node, "angle")) {
44,092✔
303
      angle_ = UnitSphereDistribution::create(node.child("angle"));
3,417✔
304
    } else {
305
      angle_ = UPtrAngle {new Isotropic()};
40,675✔
306
    }
307

308
    // Determine external source energy distribution
309
    if (check_for_node(node, "energy")) {
44,092✔
310
      pugi::xml_node node_dist = node.child("energy");
4,207✔
311
      energy_ = distribution_from_xml(node_dist);
4,207✔
312
    } else {
313
      // Default to a Watt spectrum with parameters 0.988 MeV and 2.249 MeV^-1
314
      energy_ = UPtrDist {new Watt(0.988e6, 2.249e-6)};
39,885✔
315
    }
316

317
    // Determine external source time distribution
318
    if (check_for_node(node, "time")) {
44,092✔
319
      pugi::xml_node node_dist = node.child("time");
46✔
320
      time_ = distribution_from_xml(node_dist);
46✔
321
    } else {
322
      // Default to a Constant time T=0
323
      double T[] {0.0};
44,046✔
324
      double p[] {1.0};
44,046✔
325
      time_ = UPtrDist {new Discrete {T, p, 1}};
44,046✔
326
    }
327
  }
328
}
44,092✔
329

330
SourceSite IndependentSource::sample(uint64_t* seed) const
16,592,697✔
331
{
332
  SourceSite site;
16,592,697✔
333
  site.particle = particle_;
16,592,697✔
334

335
  // Repeat sampling source location until a good site has been accepted
336
  bool accepted = false;
16,592,697✔
337
  static int n_reject = 0;
338
  static int n_accept = 0;
339

340
  while (!accepted) {
33,988,850✔
341

342
    // Sample spatial distribution
343
    site.r = space_->sample(seed);
17,396,157✔
344

345
    // Check if sampled position satisfies spatial constraints
346
    accepted = satisfies_spatial_constraints(site.r);
17,396,157✔
347

348
    // Check for rejection
349
    if (!accepted) {
17,396,157✔
350
      ++n_reject;
803,464✔
351
      if (n_reject >= EXTSRC_REJECT_THRESHOLD &&
803,464✔
352
          static_cast<double>(n_accept) / n_reject <= EXTSRC_REJECT_FRACTION) {
140,229✔
353
        fatal_error("More than 95% of external source sites sampled were "
4✔
354
                    "rejected. Please check your external source's spatial "
355
                    "definition.");
356
      }
357
    }
358
  }
359

360
  // Sample angle
361
  site.u = angle_->sample(seed);
16,592,693✔
362

363
  // Sample energy and time for neutron and photon sources
364
  if (settings::solver_type != SolverType::RANDOM_RAY) {
16,592,693✔
365
    // Check for monoenergetic source above maximum particle energy
366
    auto p = static_cast<int>(particle_);
16,034,693✔
367
    auto energy_ptr = dynamic_cast<Discrete*>(energy_.get());
16,034,693✔
368
    if (energy_ptr) {
16,034,693✔
369
      auto energies = xt::adapt(energy_ptr->x());
5,883,540✔
370
      if (xt::any(energies > data::energy_max[p])) {
5,883,540✔
371
        fatal_error("Source energy above range of energies of at least "
×
372
                    "one cross section table");
373
      }
374
    }
5,883,540✔
375

376
    while (true) {
377
      // Sample energy spectrum
378
      site.E = energy_->sample(seed);
16,034,693✔
379

380
      // Resample if energy falls above maximum particle energy
381
      if (site.E < data::energy_max[p] and
32,069,386✔
382
          (satisfies_energy_constraints(site.E)))
16,034,693✔
383
        break;
16,034,693✔
384

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

395
    // Sample particle creation time
396
    site.time = time_->sample(seed);
16,034,693✔
397
  }
398

399
  // Increment number of accepted samples
400
  ++n_accept;
16,592,693✔
401

402
  return site;
16,592,693✔
403
}
404

405
//==============================================================================
406
// FileSource implementation
407
//==============================================================================
408

409
FileSource::FileSource(pugi::xml_node node) : Source(node)
80✔
410
{
411
  auto path = get_node_value(node, "file", false, true);
80✔
412
  if (ends_with(path, ".mcpl") || ends_with(path, ".mcpl.gz")) {
80✔
413
    sites_ = mcpl_source_sites(path);
17✔
414
  } else {
415
    this->load_sites_from_file(path);
63✔
416
  }
417
}
70✔
418

419
FileSource::FileSource(const std::string& path)
17✔
420
{
421
  load_sites_from_file(path);
17✔
422
}
17✔
423

424
void FileSource::load_sites_from_file(const std::string& path)
80✔
425
{
426
  // Check if source file exists
427
  if (!file_exists(path)) {
80✔
428
    fatal_error(fmt::format("Source file '{}' does not exist.", path));
×
429
  }
430

431
  // Read the source from a binary file instead of sampling from some
432
  // assumed source distribution
433
  write_message(6, "Reading source file from {}...", path);
80✔
434

435
  // Open the binary file
436
  hid_t file_id = file_open(path, 'r', true);
80✔
437

438
  // Check to make sure this is a source file
439
  std::string filetype;
80✔
440
  read_attribute(file_id, "filetype", filetype);
80✔
441
  if (filetype != "source" && filetype != "statepoint") {
80✔
442
    fatal_error("Specified starting source file not a source file type.");
×
443
  }
444

445
  // Read in the source particles
446
  read_source_bank(file_id, sites_, false);
80✔
447

448
  // Close file
449
  file_close(file_id);
70✔
450
}
70✔
451

452
SourceSite FileSource::sample(uint64_t* seed) const
192,021✔
453
{
454
  // Sample a particle randomly from list
455
  size_t i_site = sites_.size() * prn(seed);
192,021✔
456
  return sites_[i_site];
192,021✔
457
}
458

459
//==============================================================================
460
// CompiledSourceWrapper implementation
461
//==============================================================================
462

463
CompiledSourceWrapper::CompiledSourceWrapper(pugi::xml_node node) : Source(node)
34✔
464
{
465
  // Get shared library path and parameters
466
  auto path = get_node_value(node, "library", false, true);
34✔
467
  std::string parameters;
34✔
468
  if (check_for_node(node, "parameters")) {
34✔
469
    parameters = get_node_value(node, "parameters", false, true);
17✔
470
  }
471
  setup(path, parameters);
34✔
472
}
34✔
473

474
void CompiledSourceWrapper::setup(
34✔
475
  const std::string& path, const std::string& parameters)
476
{
477
#ifdef HAS_DYNAMIC_LINKING
478
  // Open the library
479
  shared_library_ = dlopen(path.c_str(), RTLD_LAZY);
34✔
480
  if (!shared_library_) {
34✔
481
    fatal_error("Couldn't open source library " + path);
×
482
  }
483

484
  // reset errors
485
  dlerror();
34✔
486

487
  // get the function to create the custom source from the library
488
  auto create_compiled_source = reinterpret_cast<create_compiled_source_t*>(
489
    dlsym(shared_library_, "openmc_create_source"));
34✔
490

491
  // check for any dlsym errors
492
  auto dlsym_error = dlerror();
34✔
493
  if (dlsym_error) {
34✔
494
    std::string error_msg = fmt::format(
495
      "Couldn't open the openmc_create_source symbol: {}", dlsym_error);
×
496
    dlclose(shared_library_);
×
497
    fatal_error(error_msg);
×
498
  }
×
499

500
  // create a pointer to an instance of the custom source
501
  compiled_source_ = create_compiled_source(parameters);
34✔
502

503
#else
504
  fatal_error("Custom source libraries have not yet been implemented for "
505
              "non-POSIX systems");
506
#endif
507
}
34✔
508

509
CompiledSourceWrapper::~CompiledSourceWrapper()
68✔
510
{
511
  // Make sure custom source is cleared before closing shared library
512
  if (compiled_source_.get())
34✔
513
    compiled_source_.reset();
34✔
514

515
#ifdef HAS_DYNAMIC_LINKING
516
  dlclose(shared_library_);
34✔
517
#else
518
  fatal_error("Custom source libraries have not yet been implemented for "
519
              "non-POSIX systems");
520
#endif
521
}
68✔
522

34✔
523
//==============================================================================
524
// MeshSource implementation
525
//==============================================================================
526

527
MeshSource::MeshSource(pugi::xml_node node) : Source(node)
528
{
529
  int32_t mesh_id = stoi(get_node_value(node, "mesh"));
530
  int32_t mesh_idx = model::mesh_map.at(mesh_id);
531
  const auto& mesh = model::meshes[mesh_idx];
532

533
  std::vector<double> strengths;
534
  // read all source distributions and populate strengths vector for MeshSpatial
34✔
535
  // object
34✔
536
  for (auto source_node : node.children("source")) {
537
    sources_.emplace_back(Source::create(source_node));
538
    strengths.push_back(sources_.back()->strength());
34✔
539
  }
34✔
540

541
  // the number of source distributions should either be one or equal to the
542
  // number of mesh elements
34✔
543
  if (sources_.size() > 1 && sources_.size() != mesh->n_bins()) {
544
    fatal_error(fmt::format("Incorrect number of source distributions ({}) for "
545
                            "mesh source with {} elements.",
546
      sources_.size(), mesh->n_bins()));
547
  }
34✔
548

549
  space_ = std::make_unique<MeshSpatial>(mesh_idx, strengths);
550
}
551

552
SourceSite MeshSource::sample(uint64_t* seed) const
553
{
219✔
554
  // Sample the CDF defined in initialization above
555
  int32_t element = space_->sample_element_index(seed);
219✔
556

219✔
557
  // Sample position and apply rejection on spatial domains
219✔
558
  Position r;
559
  do {
219✔
560
    r = space_->mesh()->sample_element(element, seed);
561
  } while (!this->satisfies_spatial_constraints(r));
562

37,791✔
563
  SourceSite site;
37,572✔
564
  while (true) {
37,572✔
565
    // Sample source for the chosen element and replace the position
566
    site = source(element)->sample_with_constraints(seed);
567
    site.r = r;
568

569
    // Apply other rejections
219✔
570
    if (satisfies_energy_constraints(site.E) &&
×
571
        satisfies_time_constraints(site.time)) {
572
      break;
×
573
    }
574
  }
575

219✔
576
  return site;
219✔
577
}
578

204,150✔
579
//==============================================================================
580
// Non-member functions
581
//==============================================================================
204,150✔
582

583
void initialize_source()
584
{
204,150✔
585
  write_message("Initializing source particles...", 5);
586

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

595
    // sample external source distribution
596
    simulation::source_bank[i] = sample_external_source(&seed);
408,300✔
597
  }
204,150✔
598

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

609
SourceSite sample_external_source(uint64_t* seed)
3,822✔
610
{
611
  // Determine total source strength
3,822✔
612
  double total_strength = 0.0;
613
  for (auto& s : model::external_sources)
614
    total_strength += s->strength();
615

1,315,224✔
616
  // Sample from among multiple source distributions
617
  int i = 0;
2,626,800✔
618
  if (model::external_sources.size() > 1) {
1,313,400✔
619
    if (settings::uniform_source_sampling) {
1,313,400✔
620
      i = prn(seed) * model::external_sources.size();
621
    } else {
622
      double xi = prn(seed) * total_strength;
1,313,400✔
623
      double c = 0.0;
624
      for (; i < model::external_sources.size(); ++i) {
625
        c += model::external_sources[i]->strength();
626
        if (xi < c)
3,822✔
NEW
627
          break;
×
NEW
628
      }
×
UNCOV
629
    }
×
UNCOV
630
  }
×
631

×
632
  // Sample source site from i-th source distribution
633
  SourceSite site {model::external_sources[i]->sample_with_constraints(seed)};
3,822✔
634

635
  // Set particle creation weight
16,431,177✔
636
  if (settings::uniform_source_sampling) {
637
    site.wgt *= model::external_sources[i]->strength();
638
  }
16,431,177✔
639

37,602,354✔
640
  // If running in MG, convert site.E to group
21,171,177✔
641
  if (!settings::run_CE) {
642
    site.E = lower_bound_index(data::mg.rev_energy_bins_.begin(),
643
      data::mg.rev_energy_bins_.end(), site.E);
16,431,177✔
644
    site.E = data::mg.num_energy_groups_ - site.E - 1.;
16,431,177✔
645
  }
156,000✔
UNCOV
646

×
647
  return site;
648
}
156,000✔
649

156,000✔
650
void free_memory_source()
2,505,024✔
651
{
2,505,024✔
652
  model::external_sources.clear();
2,505,024✔
653
}
156,000✔
654

655
//==============================================================================
656
// C API
657
//==============================================================================
658

659
extern "C" int openmc_sample_external_source(
16,431,177✔
660
  size_t n, uint64_t* seed, void* sites)
661
{
662
  if (!sites || !seed) {
16,431,173✔
663
    set_errmsg("Received null pointer.");
2,400✔
664
    return OPENMC_E_INVALID_ARGUMENT;
665
  }
666

667
  if (model::external_sources.empty()) {
16,431,173✔
668
    set_errmsg("No external sources have been defined.");
700,800✔
669
    return OPENMC_E_OUT_OF_BOUNDS;
670
  }
700,800✔
671

672
  auto sites_array = static_cast<SourceSite*>(sites);
673
  for (size_t i = 0; i < n; ++i) {
16,431,173✔
674
    sites_array[i] = sample_external_source(seed);
675
  }
676
  return 0;
6,851✔
677
}
678

6,851✔
679
} // namespace openmc
6,851✔
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