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

openmc-dev / openmc / 13183778412

06 Feb 2025 04:50PM UTC coverage: 82.601% (-2.3%) from 84.867%
13183778412

Pull #3087

github

web-flow
Merge f09af412b into 6e0f156d3
Pull Request #3087: wheel building with scikit build core

107123 of 129687 relevant lines covered (82.6%)

12608225.33 hits per line

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

79.47
/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
DiscreteIndex external_sources_probability;
49

50
} // namespace model
51

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

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

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

70
unique_ptr<Source> Source::create(pugi::xml_node node)
7,507✔
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")) {
7,507✔
75
    std::string source_type = get_node_value(node, "type");
6,708✔
76
    if (source_type == "independent") {
6,708✔
77
      return make_unique<IndependentSource>(node);
6,460✔
78
    } else if (source_type == "file") {
248✔
79
      return make_unique<FileSource>(node);
22✔
80
    } else if (source_type == "compiled") {
226✔
81
      return make_unique<CompiledSourceWrapper>(node);
34✔
82
    } else if (source_type == "mesh") {
192✔
83
      return make_unique<MeshSource>(node);
192✔
84
    } else {
85
      fatal_error(fmt::format("Invalid source type '{}' found.", source_type));
×
86
    }
87
  } else {
6,697✔
88
    // support legacy source format
89
    if (check_for_node(node, "file")) {
799✔
90
      return make_unique<FileSource>(node);
34✔
91
    } else if (check_for_node(node, "library")) {
765✔
92
      return make_unique<CompiledSourceWrapper>(node);
×
93
    } else {
94
      return make_unique<IndependentSource>(node);
765✔
95
    }
96
  }
97
}
98

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

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

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

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

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

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

167
  while (!accepted) {
29,500,674✔
168
    // Sample a source site without considering constraints yet
169
    site = this->sample(seed);
14,750,339✔
170

171
    if (constraints_applied()) {
14,750,335✔
172
      accepted = true;
14,365,975✔
173
    } else {
174
      // Check whether sampled site satisfies constraints
175
      accepted = satisfies_spatial_constraints(site.r) &&
384,360✔
176
                 satisfies_energy_constraints(site.E) &&
768,720✔
177
                 satisfies_time_constraints(site.time);
384,360✔
178
      if (!accepted) {
384,360✔
179
        ++n_reject;
×
180
        if (n_reject >= EXTSRC_REJECT_THRESHOLD &&
×
181
            static_cast<double>(n_accept) / n_reject <=
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) {
×
190
          accepted = true;
×
191
          site.wgt = 0.0;
×
192
        }
193
      }
194
    }
195
  }
196

197
  // Increment number of accepted samples
198
  ++n_accept;
14,750,335✔
199

200
  return site;
14,750,335✔
201
}
202

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

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

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

219
  // Reject particle if it's not in the geometry at all
220
  bool found = exhaustive_find_cell(geom_state);
15,914,495✔
221
  if (!found)
15,914,495✔
222
    return false;
398,832✔
223

224
  // Check the geometry state against specified domains
225
  bool accepted = true;
15,515,663✔
226
  if (!domain_ids_.empty()) {
15,515,663✔
227
    if (domain_type_ == DomainType::MATERIAL) {
40,000✔
228
      auto mat_index = geom_state.material();
×
229
      if (mat_index != MATERIAL_VOID) {
×
230
        accepted = contains(domain_ids_, model::materials[mat_index]->id());
×
231
      }
232
    } else {
233
      for (int i = 0; i < geom_state.n_coord(); i++) {
80,000✔
234
        auto id = (domain_type_ == DomainType::CELL)
40,000✔
235
                    ? model::cells[geom_state.coord(i).cell]->id_
40,000✔
236
                    : model::universes[geom_state.coord(i).universe]->id_;
×
237
        if ((accepted = contains(domain_ids_, id)))
40,000✔
238
          break;
×
239
      }
240
    }
241
  }
242

243
  // Check if spatial site is in fissionable material
244
  if (accepted && only_fissionable_) {
15,515,663✔
245
    // Determine material
246
    auto mat_index = geom_state.material();
869,428✔
247
    if (mat_index == MATERIAL_VOID) {
869,428✔
248
      accepted = false;
×
249
    } else {
250
      accepted = model::materials[mat_index]->fissionable();
869,428✔
251
    }
252
  }
253

254
  return accepted;
15,515,663✔
255
}
15,914,495✔
256

257
//==============================================================================
258
// IndependentSource implementation
259
//==============================================================================
260

261
IndependentSource::IndependentSource(
1,355✔
262
  UPtrSpace space, UPtrAngle angle, UPtrDist energy, UPtrDist time)
1,355✔
263
  : space_ {std::move(space)}, angle_ {std::move(angle)},
1,355✔
264
    energy_ {std::move(energy)}, time_ {std::move(time)}
2,710✔
265
{}
1,355✔
266

267
IndependentSource::IndependentSource(pugi::xml_node node) : Source(node)
7,225✔
268
{
269
  // Check for particle type
270
  if (check_for_node(node, "particle")) {
7,225✔
271
    auto temp_str = get_node_value(node, "particle", true, true);
6,460✔
272
    if (temp_str == "neutron") {
6,460✔
273
      particle_ = ParticleType::neutron;
6,349✔
274
    } else if (temp_str == "photon") {
111✔
275
      particle_ = ParticleType::photon;
111✔
276
      settings::photon_transport = true;
111✔
277
    } else {
278
      fatal_error(std::string("Unknown source particle type: ") + temp_str);
×
279
    }
280
  }
6,460✔
281

282
  // Check for external source file
283
  if (check_for_node(node, "file")) {
7,225✔
284

285
  } else {
286

287
    // Spatial distribution for external source
288
    if (check_for_node(node, "space")) {
7,225✔
289
      space_ = SpatialDistribution::create(node.child("space"));
5,292✔
290
    } else {
291
      // If no spatial distribution specified, make it a point source
292
      space_ = UPtrSpace {new SpatialPoint()};
1,933✔
293
    }
294

295
    // For backwards compatibility, check for only fissionable setting on box
296
    // source
297
    auto space_box = dynamic_cast<SpatialBox*>(space_.get());
7,224✔
298
    if (space_box) {
7,224✔
299
      if (!only_fissionable_) {
2,726✔
300
        only_fissionable_ = space_box->only_fissionable();
1,817✔
301
      }
302
    }
303

304
    // Determine external source angular distribution
305
    if (check_for_node(node, "angle")) {
7,224✔
306
      angle_ = UnitSphereDistribution::create(node.child("angle"));
3,393✔
307
    } else {
308
      angle_ = UPtrAngle {new Isotropic()};
3,831✔
309
    }
310

311
    // Determine external source energy distribution
312
    if (check_for_node(node, "energy")) {
7,224✔
313
      pugi::xml_node node_dist = node.child("energy");
4,002✔
314
      energy_ = distribution_from_xml(node_dist);
4,002✔
315
    } else {
316
      // Default to a Watt spectrum with parameters 0.988 MeV and 2.249 MeV^-1
317
      energy_ = UPtrDist {new Watt(0.988e6, 2.249e-6)};
3,222✔
318
    }
319

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

333
SourceSite IndependentSource::sample(uint64_t* seed) const
14,753,579✔
334
{
335
  SourceSite site;
14,753,579✔
336
  site.particle = particle_;
14,753,579✔
337

338
  // Repeat sampling source location until a good site has been accepted
339
  bool accepted = false;
14,753,579✔
340
  static int n_reject = 0;
341
  static int n_accept = 0;
342

343
  while (!accepted) {
30,091,710✔
344

345
    // Sample spatial distribution
346
    site.r = space_->sample(seed);
15,338,135✔
347

348
    // Check if sampled position satisfies spatial constraints
349
    accepted = satisfies_spatial_constraints(site.r);
15,338,135✔
350

351
    // Check for rejection
352
    if (!accepted) {
15,338,135✔
353
      ++n_reject;
584,560✔
354
      if (n_reject >= EXTSRC_REJECT_THRESHOLD &&
584,560✔
355
          static_cast<double>(n_accept) / n_reject <= EXTSRC_REJECT_FRACTION) {
83,087✔
356
        fatal_error("More than 95% of external source sites sampled were "
4✔
357
                    "rejected. Please check your external source's spatial "
358
                    "definition.");
359
      }
360
    }
361
  }
362

363
  // Sample angle
364
  site.u = angle_->sample(seed);
14,753,575✔
365

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

379
    while (true) {
380
      // Sample energy spectrum
381
      site.E = energy_->sample(seed);
14,173,975✔
382

383
      // Resample if energy falls above maximum particle energy
384
      if (site.E < data::energy_max[p] and
28,347,950✔
385
          (satisfies_energy_constraints(site.E)))
14,173,975✔
386
        break;
14,173,975✔
387

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

398
    // Sample particle creation time
399
    site.time = time_->sample(seed);
14,173,975✔
400
  }
401

402
  // Increment number of accepted samples
403
  ++n_accept;
14,753,575✔
404

405
  return site;
14,753,575✔
406
}
407

408
//==============================================================================
409
// FileSource implementation
410
//==============================================================================
411

412
FileSource::FileSource(pugi::xml_node node) : Source(node)
56✔
413
{
414
  auto path = get_node_value(node, "file", false, true);
56✔
415
  if (ends_with(path, ".mcpl") || ends_with(path, ".mcpl.gz")) {
56✔
416
    sites_ = mcpl_source_sites(path);
17✔
417
  } else {
418
    this->load_sites_from_file(path);
39✔
419
  }
420
}
46✔
421

422
FileSource::FileSource(const std::string& path)
17✔
423
{
424
  load_sites_from_file(path);
17✔
425
}
17✔
426

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

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

438
  // Open the binary file
439
  hid_t file_id = file_open(path, 'r', true);
56✔
440

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

448
  // Read in the source particles
449
  read_source_bank(file_id, sites_, false);
56✔
450

451
  // Close file
452
  file_close(file_id);
46✔
453
}
46✔
454

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

462
//==============================================================================
463
// CompiledSourceWrapper implementation
464
//==============================================================================
465

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

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

487
  // reset errors
488
  dlerror();
34✔
489

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

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

503
  // create a pointer to an instance of the custom source
504
  compiled_source_ = create_compiled_source(parameters);
34✔
505

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

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

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

34✔
526
//==============================================================================
527
// MeshSource implementation
528
//==============================================================================
529

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

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

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

552
  space_ = std::make_unique<MeshSpatial>(mesh_idx, strengths);
553
}
554

555
SourceSite MeshSource::sample(uint64_t* seed) const
556
{
192✔
557
  // Sample the CDF defined in initialization above
558
  int32_t element = space_->sample_element_index(seed);
192✔
559

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

1,728✔
566
  SourceSite site;
1,536✔
567
  while (true) {
1,536✔
568
    // Sample source for the chosen element and replace the position
569
    site = source(element)->sample_with_constraints(seed);
570
    site.r = r;
571

572
    // Apply other rejections
192✔
573
    if (satisfies_energy_constraints(site.E) &&
×
574
        satisfies_time_constraints(site.time)) {
575
      break;
×
576
    }
577
  }
578

192✔
579
  return site;
192✔
580
}
581

192,000✔
582
//==============================================================================
583
// Non-member functions
584
//==============================================================================
192,000✔
585

586
void initialize_source()
587
{
192,000✔
588
  write_message("Initializing source particles...", 5);
589

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

598
    // sample external source distribution
599
    simulation::source_bank[i] = sample_external_source(&seed);
384,000✔
600
  }
192,000✔
601

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

612
SourceSite sample_external_source(uint64_t* seed)
3,076✔
613
{
614
  // Sample from among multiple source distributions
3,076✔
615
  int i = 0;
616
  if (model::external_sources.size() > 1) {
617
    if (settings::uniform_source_sampling) {
618
      i = prn(seed) * model::external_sources.size();
1,129,474✔
619
    } else {
620
      i = model::external_sources_probability.sample(seed);
2,256,000✔
621
    }
1,128,000✔
622
  }
1,128,000✔
623

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

627
  // Set particle creation weight
628
  if (settings::uniform_source_sampling) {
629
    site.wgt *= model::external_sources[i]->strength();
3,076✔
630
  }
×
631

×
632
  // If running in MG, convert site.E to group
×
633
  if (!settings::run_CE) {
×
634
    site.E = lower_bound_index(data::mg.rev_energy_bins_.begin(),
×
635
      data::mg.rev_energy_bins_.end(), site.E);
636
    site.E = data::mg.num_energy_groups_ - site.E - 1.;
3,076✔
637
  }
638

14,558,339✔
639
  return site;
640
}
641

14,558,339✔
642
void free_memory_source()
14,558,339✔
643
{
156,000✔
644
  model::external_sources.clear();
×
645
}
646

156,000✔
647
//==============================================================================
648
// C API
649
//==============================================================================
650

651
extern "C" int openmc_sample_external_source(
14,558,339✔
652
  size_t n, uint64_t* seed, void* sites)
653
{
654
  if (!sites || !seed) {
14,558,335✔
655
    set_errmsg("Received null pointer.");
2,400✔
656
    return OPENMC_E_INVALID_ARGUMENT;
657
  }
658

659
  if (model::external_sources.empty()) {
14,558,335✔
660
    set_errmsg("No external sources have been defined.");
1,900,800✔
661
    return OPENMC_E_OUT_OF_BOUNDS;
662
  }
1,900,800✔
663

664
  auto sites_array = static_cast<SourceSite*>(sites);
665
  for (size_t i = 0; i < n; ++i) {
14,558,335✔
666
    sites_array[i] = sample_external_source(seed);
667
  }
668
  return 0;
5,426✔
669
}
670

5,426✔
671
} // namespace openmc
5,426✔
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