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

openmc-dev / openmc / 8745330834

18 Apr 2024 10:10PM UTC coverage: 84.629% (-0.01%) from 84.643%
8745330834

push

github

web-flow
Random Ray Transport (#2823)

Co-authored-by: Gavin Ridley <gavin.keith.ridley@gmail.com>
Co-authored-by: Paul Romano <paul.k.romano@gmail.com>

577 of 700 new or added lines in 16 files covered. (82.43%)

3 existing lines in 2 files now uncovered.

48402 of 57193 relevant lines covered (84.63%)

34065627.92 hits per line

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

77.17
/src/settings.cpp
1
#include "openmc/settings.h"
2

3
#include <cmath>  // for ceil, pow
4
#include <limits> // for numeric_limits
5
#include <string>
6

7
#include <fmt/core.h>
8
#ifdef _OPENMP
9
#include <omp.h>
10
#endif
11

12
#include "openmc/capi.h"
13
#include "openmc/constants.h"
14
#include "openmc/container_util.h"
15
#include "openmc/distribution.h"
16
#include "openmc/distribution_multi.h"
17
#include "openmc/distribution_spatial.h"
18
#include "openmc/eigenvalue.h"
19
#include "openmc/error.h"
20
#include "openmc/file_utils.h"
21
#include "openmc/mcpl_interface.h"
22
#include "openmc/mesh.h"
23
#include "openmc/message_passing.h"
24
#include "openmc/output.h"
25
#include "openmc/plot.h"
26
#include "openmc/random_lcg.h"
27
#include "openmc/random_ray/random_ray.h"
28
#include "openmc/simulation.h"
29
#include "openmc/source.h"
30
#include "openmc/string_utils.h"
31
#include "openmc/tallies/trigger.h"
32
#include "openmc/volume_calc.h"
33
#include "openmc/weight_windows.h"
34
#include "openmc/xml_interface.h"
35

36
namespace openmc {
37

38
//==============================================================================
39
// Global variables
40
//==============================================================================
41

42
namespace settings {
43

44
// Default values for boolean flags
45
bool assume_separate {false};
46
bool check_overlaps {false};
47
bool cmfd_run {false};
48
bool confidence_intervals {false};
49
bool create_delayed_neutrons {true};
50
bool create_fission_neutrons {true};
51
bool delayed_photon_scaling {true};
52
bool entropy_on {false};
53
bool event_based {false};
54
bool legendre_to_tabular {true};
55
bool material_cell_offsets {true};
56
bool output_summary {true};
57
bool output_tallies {true};
58
bool particle_restart_run {false};
59
bool photon_transport {false};
60
bool reduce_tallies {true};
61
bool res_scat_on {false};
62
bool restart_run {false};
63
bool run_CE {true};
64
bool source_latest {false};
65
bool source_separate {false};
66
bool source_write {true};
67
bool source_mcpl_write {false};
68
bool surf_source_write {false};
69
bool surf_mcpl_write {false};
70
bool surf_source_read {false};
71
bool survival_biasing {false};
72
bool temperature_multipole {false};
73
bool trigger_on {false};
74
bool trigger_predict {false};
75
bool ufs_on {false};
76
bool urr_ptables_on {true};
77
bool weight_windows_on {false};
78
bool weight_window_checkpoint_surface {false};
79
bool weight_window_checkpoint_collision {true};
80
bool write_all_tracks {false};
81
bool write_initial_source {false};
82

83
std::string path_cross_sections;
84
std::string path_input;
85
std::string path_output;
86
std::string path_particle_restart;
87
std::string path_sourcepoint;
88
std::string path_statepoint;
89
const char* path_statepoint_c {path_statepoint.c_str()};
90
std::string weight_windows_file;
91

92
int32_t n_inactive {0};
93
int32_t max_lost_particles {10};
94
double rel_max_lost_particles {1.0e-6};
95
int32_t max_write_lost_particles {-1};
96
int32_t gen_per_batch {1};
97
int64_t n_particles {-1};
98

99
int64_t max_particles_in_flight {100000};
100
int max_particle_events {1000000};
101

102
ElectronTreatment electron_treatment {ElectronTreatment::TTB};
103
array<double, 4> energy_cutoff {0.0, 1000.0, 0.0, 0.0};
104
array<double, 4> time_cutoff {INFTY, INFTY, INFTY, INFTY};
105
int legendre_to_tabular_points {C_NONE};
106
int max_order {0};
107
int n_log_bins {8000};
108
int n_batches;
109
int n_max_batches;
110
int max_splits {1000};
111
int max_tracks {1000};
112
ResScatMethod res_scat_method {ResScatMethod::rvs};
113
double res_scat_energy_min {0.01};
114
double res_scat_energy_max {1000.0};
115
vector<std::string> res_scat_nuclides;
116
RunMode run_mode {RunMode::UNSET};
117
SolverType solver_type {SolverType::MONTE_CARLO};
118
std::unordered_set<int> sourcepoint_batch;
119
std::unordered_set<int> statepoint_batch;
120
std::unordered_set<int> source_write_surf_id;
121
int64_t max_surface_particles;
122
TemperatureMethod temperature_method {TemperatureMethod::NEAREST};
123
double temperature_tolerance {10.0};
124
double temperature_default {293.6};
125
array<double, 2> temperature_range {0.0, 0.0};
126
int trace_batch;
127
int trace_gen;
128
int64_t trace_particle;
129
vector<array<int, 3>> track_identifiers;
130
int trigger_batch_interval {1};
131
int verbosity {7};
132
double weight_cutoff {0.25};
133
double weight_survive {1.0};
134

135
} // namespace settings
136

137
//==============================================================================
138
// Functions
139
//==============================================================================
140

141
void get_run_parameters(pugi::xml_node node_base)
6,098✔
142
{
143
  using namespace settings;
144
  using namespace pugi;
145

146
  // Check number of particles
147
  if (!check_for_node(node_base, "particles")) {
6,098✔
148
    fatal_error("Need to specify number of particles.");
×
149
  }
150

151
  // Get number of particles if it wasn't specified as a command-line argument
152
  if (n_particles == -1) {
6,098✔
153
    n_particles = std::stoll(get_node_value(node_base, "particles"));
6,098✔
154
  }
155

156
  // Get maximum number of in flight particles for event-based mode
157
  if (check_for_node(node_base, "max_particles_in_flight")) {
6,098✔
158
    max_particles_in_flight =
×
159
      std::stoll(get_node_value(node_base, "max_particles_in_flight"));
×
160
  }
161

162
  // Get maximum number of events allowed per particle
163
  if (check_for_node(node_base, "max_particle_events")) {
6,098✔
164
    max_particle_events =
×
165
      std::stoll(get_node_value(node_base, "max_particle_events"));
×
166
  }
167

168
  // Get number of basic batches
169
  if (check_for_node(node_base, "batches")) {
6,098✔
170
    n_batches = std::stoi(get_node_value(node_base, "batches"));
6,098✔
171
  }
172
  if (!trigger_on)
6,098✔
173
    n_max_batches = n_batches;
5,947✔
174

175
  // Get max number of lost particles
176
  if (check_for_node(node_base, "max_lost_particles")) {
6,098✔
177
    max_lost_particles =
19✔
178
      std::stoi(get_node_value(node_base, "max_lost_particles"));
19✔
179
  }
180

181
  // Get relative number of lost particles
182
  if (check_for_node(node_base, "rel_max_lost_particles")) {
6,098✔
183
    rel_max_lost_particles =
×
184
      std::stod(get_node_value(node_base, "rel_max_lost_particles"));
×
185
  }
186

187
  // Get relative number of lost particles
188
  if (check_for_node(node_base, "max_write_lost_particles")) {
6,098✔
189
    max_write_lost_particles =
19✔
190
      std::stoi(get_node_value(node_base, "max_write_lost_particles"));
19✔
191
  }
192

193
  // Get number of inactive batches
194
  if (run_mode == RunMode::EIGENVALUE) {
6,098✔
195
    if (check_for_node(node_base, "inactive")) {
4,232✔
196
      n_inactive = std::stoi(get_node_value(node_base, "inactive"));
4,122✔
197
    }
198
    if (check_for_node(node_base, "generations_per_batch")) {
4,232✔
199
      gen_per_batch =
19✔
200
        std::stoi(get_node_value(node_base, "generations_per_batch"));
19✔
201
    }
202

203
    // Preallocate space for keff and entropy by generation
204
    int m = settings::n_max_batches * settings::gen_per_batch;
4,232✔
205
    simulation::k_generation.reserve(m);
4,232✔
206
    simulation::entropy.reserve(m);
4,232✔
207

208
    // Get the trigger information for keff
209
    if (check_for_node(node_base, "keff_trigger")) {
4,232✔
210
      xml_node node_keff_trigger = node_base.child("keff_trigger");
128✔
211

212
      if (check_for_node(node_keff_trigger, "type")) {
128✔
213
        auto temp = get_node_value(node_keff_trigger, "type", true, true);
128✔
214
        if (temp == "std_dev") {
128✔
215
          keff_trigger.metric = TriggerMetric::standard_deviation;
128✔
216
        } else if (temp == "variance") {
×
217
          keff_trigger.metric = TriggerMetric::variance;
×
218
        } else if (temp == "rel_err") {
×
219
          keff_trigger.metric = TriggerMetric::relative_error;
×
220
        } else {
221
          fatal_error("Unrecognized keff trigger type " + temp);
×
222
        }
223
      } else {
128✔
224
        fatal_error("Specify keff trigger type in settings XML");
×
225
      }
226

227
      if (check_for_node(node_keff_trigger, "threshold")) {
128✔
228
        keff_trigger.threshold =
128✔
229
          std::stod(get_node_value(node_keff_trigger, "threshold"));
128✔
230
        if (keff_trigger.threshold <= 0) {
128✔
231
          fatal_error("keff trigger threshold must be positive");
×
232
        }
233
      } else {
234
        fatal_error("Specify keff trigger threshold in settings XML");
×
235
      }
236
    }
237
  }
238

239
  // Random ray variables
240
  if (solver_type == SolverType::RANDOM_RAY) {
6,098✔
241
    xml_node random_ray_node = node_base.child("random_ray");
38✔
242
    if (check_for_node(random_ray_node, "distance_active")) {
38✔
243
      RandomRay::distance_active_ =
38✔
244
        std::stod(get_node_value(random_ray_node, "distance_active"));
38✔
245
      if (RandomRay::distance_active_ <= 0.0) {
38✔
NEW
246
        fatal_error("Random ray active distance must be greater than 0");
×
247
      }
248
    } else {
NEW
249
      fatal_error("Specify random ray active distance in settings XML");
×
250
    }
251
    if (check_for_node(random_ray_node, "distance_inactive")) {
38✔
252
      RandomRay::distance_inactive_ =
38✔
253
        std::stod(get_node_value(random_ray_node, "distance_inactive"));
38✔
254
      if (RandomRay::distance_inactive_ < 0) {
38✔
NEW
255
        fatal_error(
×
256
          "Random ray inactive distance must be greater than or equal to 0");
257
      }
258
    } else {
NEW
259
      fatal_error("Specify random ray inactive distance in settings XML");
×
260
    }
261
    if (check_for_node(random_ray_node, "source")) {
38✔
262
      xml_node source_node = random_ray_node.child("source");
38✔
263
      // Get point to list of <source> elements and make sure there is at least
264
      // one
265
      RandomRay::ray_source_ = Source::create(source_node);
38✔
266
    } else {
NEW
267
      fatal_error("Specify random ray source in settings XML");
×
268
    }
269
  }
270
}
6,098✔
271

272
void read_settings_xml()
1,841✔
273
{
274
  using namespace settings;
275
  using namespace pugi;
276
  // Check if settings.xml exists
277
  std::string filename = settings::path_input + "settings.xml";
1,841✔
278
  if (!file_exists(filename)) {
1,841✔
279
    if (run_mode != RunMode::PLOTTING) {
28✔
280
      fatal_error("Could not find any XML input files! In order to run OpenMC, "
×
281
                  "you first need a set of input files; at a minimum, this "
282
                  "includes settings.xml, geometry.xml, and materials.xml or a "
283
                  "single model XML file. Please consult the user's guide at "
284
                  "https://docs.openmc.org for further information.");
285
    } else {
286
      // The settings.xml file is optional if we just want to make a plot.
287
      return;
28✔
288
    }
289
  }
290

291
  // Parse settings.xml file
292
  xml_document doc;
1,813✔
293
  auto result = doc.load_file(filename.c_str());
1,813✔
294
  if (!result) {
1,813✔
295
    fatal_error("Error processing settings.xml file.");
×
296
  }
297

298
  // Get root element
299
  xml_node root = doc.document_element();
1,813✔
300

301
  // Verbosity
302
  if (check_for_node(root, "verbosity")) {
1,813✔
303
    verbosity = std::stoi(get_node_value(root, "verbosity"));
246✔
304
  }
305

306
  // To this point, we haven't displayed any output since we didn't know what
307
  // the verbosity is. Now that we checked for it, show the title if necessary
308
  if (mpi::master) {
1,813✔
309
    if (verbosity >= 2)
1,588✔
310
      title();
1,352✔
311
  }
312

313
  write_message("Reading settings XML file...", 5);
1,813✔
314

315
  read_settings_xml(root);
1,813✔
316
}
1,828✔
317

318
void read_settings_xml(pugi::xml_node root)
6,452✔
319
{
320
  using namespace settings;
321
  using namespace pugi;
322

323
  // Find if a multi-group or continuous-energy simulation is desired
324
  if (check_for_node(root, "energy_mode")) {
6,452✔
325
    std::string temp_str = get_node_value(root, "energy_mode", true, true);
646✔
326
    if (temp_str == "mg" || temp_str == "multi-group") {
646✔
327
      run_CE = false;
646✔
328
    } else if (temp_str == "ce" || temp_str == "continuous-energy") {
×
329
      run_CE = true;
×
330
    }
331
  }
646✔
332

333
  // Check for user meshes and allocate
334
  read_meshes(root);
6,452✔
335

336
  // Look for deprecated cross_sections.xml file in settings.xml
337
  if (check_for_node(root, "cross_sections")) {
6,452✔
338
    warning(
×
339
      "Setting cross_sections in settings.xml has been deprecated."
340
      " The cross_sections are now set in materials.xml and the "
341
      "cross_sections input to materials.xml and the OPENMC_CROSS_SECTIONS"
342
      " environment variable will take precendent over setting "
343
      "cross_sections in settings.xml.");
344
    path_cross_sections = get_node_value(root, "cross_sections");
×
345
  }
346

347
  if (!run_CE) {
6,452✔
348
    // Scattering Treatments
349
    if (check_for_node(root, "max_order")) {
646✔
350
      max_order = std::stoi(get_node_value(root, "max_order"));
19✔
351
    } else {
352
      // Set to default of largest int - 1, which means to use whatever is
353
      // contained in library. This is largest int - 1 because for legendre
354
      // scattering, a value of 1 is added to the order; adding 1 to the largest
355
      // int gets you the largest negative integer, which is not what we want.
356
      max_order = std::numeric_limits<int>::max() - 1;
627✔
357
    }
358
  }
359

360
  // Check for a trigger node and get trigger information
361
  if (check_for_node(root, "trigger")) {
6,452✔
362
    xml_node node_trigger = root.child("trigger");
170✔
363

364
    // Check if trigger(s) are to be turned on
365
    trigger_on = get_node_value_bool(node_trigger, "active");
170✔
366

367
    if (trigger_on) {
170✔
368
      if (check_for_node(node_trigger, "max_batches")) {
151✔
369
        n_max_batches = std::stoi(get_node_value(node_trigger, "max_batches"));
151✔
370
      } else {
371
        fatal_error("<max_batches> must be specified with triggers");
×
372
      }
373

374
      // Get the batch interval to check triggers
375
      if (!check_for_node(node_trigger, "batch_interval")) {
151✔
376
        trigger_predict = true;
19✔
377
      } else {
378
        trigger_batch_interval =
132✔
379
          std::stoi(get_node_value(node_trigger, "batch_interval"));
132✔
380
        if (trigger_batch_interval <= 0) {
132✔
381
          fatal_error("Trigger batch interval must be greater than zero");
×
382
        }
383
      }
384
    }
385
  }
386

387
  // Check run mode if it hasn't been set from the command line
388
  xml_node node_mode;
6,452✔
389
  if (run_mode == RunMode::UNSET) {
6,452✔
390
    if (check_for_node(root, "run_mode")) {
6,136✔
391
      std::string temp_str = get_node_value(root, "run_mode", true, true);
6,060✔
392
      if (temp_str == "eigenvalue") {
6,060✔
393
        run_mode = RunMode::EIGENVALUE;
4,156✔
394
      } else if (temp_str == "fixed source") {
1,904✔
395
        run_mode = RunMode::FIXED_SOURCE;
1,866✔
396
      } else if (temp_str == "plot") {
38✔
397
        run_mode = RunMode::PLOTTING;
×
398
      } else if (temp_str == "particle restart") {
38✔
399
        run_mode = RunMode::PARTICLE;
×
400
      } else if (temp_str == "volume") {
38✔
401
        run_mode = RunMode::VOLUME;
38✔
402
      } else {
403
        fatal_error("Unrecognized run mode: " + temp_str);
×
404
      }
405

406
      // Assume XML specifies <particles>, <batches>, etc. directly
407
      node_mode = root;
6,060✔
408
    } else {
6,060✔
409
      warning("<run_mode> should be specified.");
76✔
410

411
      // Make sure that either eigenvalue or fixed source was specified
412
      node_mode = root.child("eigenvalue");
76✔
413
      if (node_mode) {
76✔
414
        run_mode = RunMode::EIGENVALUE;
76✔
415
      } else {
416
        node_mode = root.child("fixed_source");
×
417
        if (node_mode) {
×
418
          run_mode = RunMode::FIXED_SOURCE;
×
419
        } else {
420
          fatal_error("<eigenvalue> or <fixed_source> not specified.");
×
421
        }
422
      }
423
    }
424
  }
425

426
  // Check solver type
427
  if (check_for_node(root, "random_ray")) {
6,452✔
428
    solver_type = SolverType::RANDOM_RAY;
38✔
429
    if (run_CE)
38✔
NEW
430
      fatal_error("multi-group energy mode must be specified in settings XML "
×
431
                  "when using the random ray solver.");
432
  }
433

434
  if (run_mode == RunMode::EIGENVALUE || run_mode == RunMode::FIXED_SOURCE) {
6,452✔
435
    // Read run parameters
436
    get_run_parameters(node_mode);
6,098✔
437

438
    // Check number of active batches, inactive batches, max lost particles and
439
    // particles
440
    if (n_batches <= n_inactive) {
6,098✔
441
      fatal_error("Number of active batches must be greater than zero.");
×
442
    } else if (n_inactive < 0) {
6,098✔
443
      fatal_error("Number of inactive batches must be non-negative.");
×
444
    } else if (n_particles <= 0) {
6,098✔
445
      fatal_error("Number of particles must be greater than zero.");
×
446
    } else if (max_lost_particles <= 0) {
6,098✔
447
      fatal_error("Number of max lost particles must be greater than zero.");
×
448
    } else if (rel_max_lost_particles <= 0.0 || rel_max_lost_particles >= 1.0) {
6,098✔
449
      fatal_error("Relative max lost particles must be between zero and one.");
×
450
    }
451
  }
452

453
  // Copy plotting random number seed if specified
454
  if (check_for_node(root, "plot_seed")) {
6,452✔
455
    auto seed = std::stoll(get_node_value(root, "plot_seed"));
×
456
    model::plotter_seed = seed;
×
457
  }
458

459
  // Copy random number seed if specified
460
  if (check_for_node(root, "seed")) {
6,452✔
461
    auto seed = std::stoll(get_node_value(root, "seed"));
129✔
462
    openmc_set_seed(seed);
129✔
463
  }
464

465
  // Check for electron treatment
466
  if (check_for_node(root, "electron_treatment")) {
6,452✔
467
    auto temp_str = get_node_value(root, "electron_treatment", true, true);
57✔
468
    if (temp_str == "led") {
57✔
469
      electron_treatment = ElectronTreatment::LED;
×
470
    } else if (temp_str == "ttb") {
57✔
471
      electron_treatment = ElectronTreatment::TTB;
57✔
472
    } else {
473
      fatal_error("Unrecognized electron treatment: " + temp_str + ".");
×
474
    }
475
  }
57✔
476

477
  // Check for photon transport
478
  if (check_for_node(root, "photon_transport")) {
6,452✔
479
    photon_transport = get_node_value_bool(root, "photon_transport");
213✔
480

481
    if (!run_CE && photon_transport) {
213✔
482
      fatal_error("Photon transport is not currently supported in "
×
483
                  "multigroup mode");
484
    }
485
  }
486

487
  // Number of bins for logarithmic grid
488
  if (check_for_node(root, "log_grid_bins")) {
6,452✔
489
    n_log_bins = std::stoi(get_node_value(root, "log_grid_bins"));
19✔
490
    if (n_log_bins < 1) {
19✔
491
      fatal_error("Number of bins for logarithmic grid must be greater "
×
492
                  "than zero.");
493
    }
494
  }
495

496
  // Number of OpenMP threads
497
  if (check_for_node(root, "threads")) {
6,452✔
498
    if (mpi::master)
×
499
      warning("The <threads> element has been deprecated. Use "
×
500
              "the OMP_NUM_THREADS environment variable to set the number of "
501
              "threads.");
502
  }
503

504
  // ==========================================================================
505
  // EXTERNAL SOURCE
506

507
  // Get point to list of <source> elements and make sure there is at least one
508
  for (pugi::xml_node node : root.children("source")) {
12,867✔
509
    model::external_sources.push_back(Source::create(node));
6,428✔
510
  }
511

512
  // Check if the user has specified to read surface source
513
  if (check_for_node(root, "surf_source_read")) {
6,439✔
514
    surf_source_read = true;
19✔
515
    // Get surface source read node
516
    xml_node node_ssr = root.child("surf_source_read");
19✔
517

518
    std::string path = "surface_source.h5";
19✔
519
    // Check if the user has specified different file for surface source reading
520
    if (check_for_node(node_ssr, "path")) {
19✔
521
      path = get_node_value(node_ssr, "path", false, true);
19✔
522
    }
523
    model::external_sources.push_back(make_unique<FileSource>(path));
19✔
524
  }
19✔
525

526
  // If no source specified, default to isotropic point source at origin with
527
  // Watt spectrum
528
  if (model::external_sources.empty()) {
6,439✔
529
    double T[] {0.0};
1,790✔
530
    double p[] {1.0};
1,790✔
531
    model::external_sources.push_back(make_unique<IndependentSource>(
1,790✔
532
      UPtrSpace {new SpatialPoint({0.0, 0.0, 0.0})},
3,580✔
533
      UPtrAngle {new Isotropic()}, UPtrDist {new Watt(0.988e6, 2.249e-6)},
3,580✔
534
      UPtrDist {new Discrete(T, p, 1)}));
3,580✔
535
  }
536

537
  // Check if we want to write out source
538
  if (check_for_node(root, "write_initial_source")) {
6,439✔
539
    write_initial_source = get_node_value_bool(root, "write_initial_source");
×
540
  }
541

542
  // Survival biasing
543
  if (check_for_node(root, "survival_biasing")) {
6,439✔
544
    survival_biasing = get_node_value_bool(root, "survival_biasing");
220✔
545
  }
546

547
  // Probability tables
548
  if (check_for_node(root, "ptables")) {
6,439✔
549
    urr_ptables_on = get_node_value_bool(root, "ptables");
19✔
550
  }
551

552
  // Cutoffs
553
  if (check_for_node(root, "cutoff")) {
6,439✔
554
    xml_node node_cutoff = root.child("cutoff");
128✔
555
    if (check_for_node(node_cutoff, "weight")) {
128✔
556
      weight_cutoff = std::stod(get_node_value(node_cutoff, "weight"));
19✔
557
    }
558
    if (check_for_node(node_cutoff, "weight_avg")) {
128✔
559
      weight_survive = std::stod(get_node_value(node_cutoff, "weight_avg"));
19✔
560
    }
561
    if (check_for_node(node_cutoff, "energy_neutron")) {
128✔
562
      energy_cutoff[0] =
38✔
563
        std::stod(get_node_value(node_cutoff, "energy_neutron"));
19✔
564
    } else if (check_for_node(node_cutoff, "energy")) {
109✔
565
      warning("The use of an <energy> cutoff is deprecated and should "
×
566
              "be replaced by <energy_neutron>.");
567
      energy_cutoff[0] = std::stod(get_node_value(node_cutoff, "energy"));
×
568
    }
569
    if (check_for_node(node_cutoff, "energy_photon")) {
128✔
570
      energy_cutoff[1] =
142✔
571
        std::stod(get_node_value(node_cutoff, "energy_photon"));
71✔
572
    }
573
    if (check_for_node(node_cutoff, "energy_electron")) {
128✔
574
      energy_cutoff[2] =
×
575
        std::stof(get_node_value(node_cutoff, "energy_electron"));
×
576
    }
577
    if (check_for_node(node_cutoff, "energy_positron")) {
128✔
578
      energy_cutoff[3] =
×
579
        std::stod(get_node_value(node_cutoff, "energy_positron"));
×
580
    }
581
    if (check_for_node(node_cutoff, "time_neutron")) {
128✔
582
      time_cutoff[0] = std::stod(get_node_value(node_cutoff, "time_neutron"));
19✔
583
    }
584
    if (check_for_node(node_cutoff, "time_photon")) {
128✔
585
      time_cutoff[1] = std::stod(get_node_value(node_cutoff, "time_photon"));
×
586
    }
587
    if (check_for_node(node_cutoff, "time_electron")) {
128✔
588
      time_cutoff[2] = std::stod(get_node_value(node_cutoff, "time_electron"));
×
589
    }
590
    if (check_for_node(node_cutoff, "time_positron")) {
128✔
591
      time_cutoff[3] = std::stod(get_node_value(node_cutoff, "time_positron"));
×
592
    }
593
  }
594

595
  // Particle trace
596
  if (check_for_node(root, "trace")) {
6,439✔
597
    auto temp = get_node_array<int64_t>(root, "trace");
19✔
598
    if (temp.size() != 3) {
19✔
599
      fatal_error("Must provide 3 integers for <trace> that specify the "
×
600
                  "batch, generation, and particle number.");
601
    }
602
    trace_batch = temp.at(0);
19✔
603
    trace_gen = temp.at(1);
19✔
604
    trace_particle = temp.at(2);
19✔
605
  }
19✔
606

607
  // Particle tracks
608
  if (check_for_node(root, "track")) {
6,439✔
609
    // Get values and make sure there are three per particle
610
    auto temp = get_node_array<int>(root, "track");
57✔
611
    if (temp.size() % 3 != 0) {
57✔
612
      fatal_error(
×
613
        "Number of integers specified in 'track' is not "
614
        "divisible by 3.  Please provide 3 integers per particle to be "
615
        "tracked.");
616
    }
617

618
    // Reshape into track_identifiers
619
    int n_tracks = temp.size() / 3;
57✔
620
    for (int i = 0; i < n_tracks; ++i) {
228✔
621
      track_identifiers.push_back(
171✔
622
        {temp[3 * i], temp[3 * i + 1], temp[3 * i + 2]});
171✔
623
    }
624
  }
57✔
625

626
  // Shannon Entropy mesh
627
  if (check_for_node(root, "entropy_mesh")) {
6,439✔
628
    int temp = std::stoi(get_node_value(root, "entropy_mesh"));
590✔
629
    if (model::mesh_map.find(temp) == model::mesh_map.end()) {
590✔
630
      fatal_error(fmt::format(
×
631
        "Mesh {} specified for Shannon entropy does not exist.", temp));
632
    }
633

634
    auto* m =
635
      dynamic_cast<RegularMesh*>(model::meshes[model::mesh_map.at(temp)].get());
590✔
636
    if (!m)
590✔
637
      fatal_error("Only regular meshes can be used as an entropy mesh");
×
638
    simulation::entropy_mesh = m;
590✔
639

640
    // Turn on Shannon entropy calculation
641
    entropy_on = true;
590✔
642

643
  } else if (check_for_node(root, "entropy")) {
5,849✔
644
    fatal_error(
×
645
      "Specifying a Shannon entropy mesh via the <entropy> element "
646
      "is deprecated. Please create a mesh using <mesh> and then reference "
647
      "it by specifying its ID in an <entropy_mesh> element.");
648
  }
649

650
  // Uniform fission source weighting mesh
651
  if (check_for_node(root, "ufs_mesh")) {
6,439✔
652
    auto temp = std::stoi(get_node_value(root, "ufs_mesh"));
19✔
653
    if (model::mesh_map.find(temp) == model::mesh_map.end()) {
19✔
654
      fatal_error(fmt::format("Mesh {} specified for uniform fission site "
×
655
                              "method does not exist.",
656
        temp));
657
    }
658

659
    auto* m =
660
      dynamic_cast<RegularMesh*>(model::meshes[model::mesh_map.at(temp)].get());
19✔
661
    if (!m)
19✔
662
      fatal_error("Only regular meshes can be used as a UFS mesh");
×
663
    simulation::ufs_mesh = m;
19✔
664

665
    // Turn on uniform fission source weighting
666
    ufs_on = true;
19✔
667

668
  } else if (check_for_node(root, "uniform_fs")) {
6,420✔
669
    fatal_error(
×
670
      "Specifying a UFS mesh via the <uniform_fs> element "
671
      "is deprecated. Please create a mesh using <mesh> and then reference "
672
      "it by specifying its ID in a <ufs_mesh> element.");
673
  }
674

675
  // Check if the user has specified to write state points
676
  if (check_for_node(root, "state_point")) {
6,439✔
677

678
    // Get pointer to state_point node
679
    auto node_sp = root.child("state_point");
228✔
680

681
    // Determine number of batches at which to store state points
682
    if (check_for_node(node_sp, "batches")) {
228✔
683
      // User gave specific batches to write state points
684
      auto temp = get_node_array<int>(node_sp, "batches");
228✔
685
      for (const auto& b : temp) {
703✔
686
        statepoint_batch.insert(b);
475✔
687
      }
688
    } else {
228✔
689
      // If neither were specified, write state point at last batch
690
      statepoint_batch.insert(n_batches);
×
691
    }
692
  } else {
693
    // If no <state_point> tag was present, by default write state point at
694
    // last batch only
695
    statepoint_batch.insert(n_batches);
6,211✔
696
  }
697

698
  // Check if the user has specified to write source points
699
  if (check_for_node(root, "source_point")) {
6,439✔
700
    // Get source_point node
701
    xml_node node_sp = root.child("source_point");
114✔
702

703
    // Determine batches at which to store source points
704
    if (check_for_node(node_sp, "batches")) {
114✔
705
      // User gave specific batches to write source points
706
      auto temp = get_node_array<int>(node_sp, "batches");
57✔
707
      for (const auto& b : temp) {
152✔
708
        sourcepoint_batch.insert(b);
95✔
709
      }
710
    } else {
57✔
711
      // If neither were specified, write source points with state points
712
      sourcepoint_batch = statepoint_batch;
57✔
713
    }
714

715
    // Check if the user has specified to write binary source file
716
    if (check_for_node(node_sp, "separate")) {
114✔
717
      source_separate = get_node_value_bool(node_sp, "separate");
76✔
718
    }
719
    if (check_for_node(node_sp, "write")) {
114✔
720
      source_write = get_node_value_bool(node_sp, "write");
×
721
    }
722
    if (check_for_node(node_sp, "mcpl")) {
114✔
723
      source_mcpl_write = get_node_value_bool(node_sp, "mcpl");
19✔
724

725
      // Make sure MCPL support is enabled
726
      if (source_mcpl_write && !MCPL_ENABLED) {
19✔
727
        fatal_error(
×
728
          "Your build of OpenMC does not support writing MCPL source files.");
729
      }
730
    }
731
    if (check_for_node(node_sp, "overwrite_latest")) {
114✔
732
      source_latest = get_node_value_bool(node_sp, "overwrite_latest");
19✔
733
      source_separate = source_latest;
19✔
734
    }
735
  } else {
736
    // If no <source_point> tag was present, by default we keep source bank in
737
    // statepoint file and write it out at statepoints intervals
738
    source_separate = false;
6,325✔
739
    sourcepoint_batch = statepoint_batch;
6,325✔
740
  }
741

742
  // Check if the user has specified to write surface source
743
  if (check_for_node(root, "surf_source_write")) {
6,439✔
744
    surf_source_write = true;
16✔
745
    // Get surface source write node
746
    xml_node node_ssw = root.child("surf_source_write");
16✔
747

748
    // Determine surface ids at which crossing particles are to be banked
749
    if (check_for_node(node_ssw, "surface_ids")) {
16✔
750
      auto temp = get_node_array<int>(node_ssw, "surface_ids");
16✔
751
      for (const auto& b : temp) {
32✔
752
        source_write_surf_id.insert(b);
16✔
753
      }
754
    }
16✔
755

756
    // Get maximum number of particles to be banked per surface
757
    if (check_for_node(node_ssw, "max_particles")) {
16✔
758
      max_surface_particles =
16✔
759
        std::stoll(get_node_value(node_ssw, "max_particles"));
16✔
760
    }
761
    if (check_for_node(node_ssw, "mcpl")) {
16✔
762
      surf_mcpl_write = get_node_value_bool(node_ssw, "mcpl");
×
763

764
      // Make sure MCPL support is enabled
765
      if (surf_mcpl_write && !MCPL_ENABLED) {
×
766
        fatal_error("Your build of OpenMC does not support writing MCPL "
×
767
                    "surface source files.");
768
      }
769
    }
770
  }
771

772
  // If source is not separate and is to be written out in the statepoint file,
773
  // make sure that the sourcepoint batch numbers are contained in the
774
  // statepoint list
775
  if (!source_separate) {
6,439✔
776
    for (const auto& b : sourcepoint_batch) {
12,878✔
777
      if (!contains(statepoint_batch, b)) {
6,534✔
778
        fatal_error(
×
779
          "Sourcepoint batches are not a subset of statepoint batches.");
780
      }
781
    }
782
  }
783

784
  // Check if the user has specified to not reduce tallies at the end of every
785
  // batch
786
  if (check_for_node(root, "no_reduce")) {
6,439✔
787
    reduce_tallies = !get_node_value_bool(root, "no_reduce");
×
788
  }
789

790
  // Check if the user has specified to use confidence intervals for
791
  // uncertainties rather than standard deviations
792
  if (check_for_node(root, "confidence_intervals")) {
6,439✔
793
    confidence_intervals = get_node_value_bool(root, "confidence_intervals");
19✔
794
  }
795

796
  // Check for output options
797
  if (check_for_node(root, "output")) {
6,439✔
798
    // Get pointer to output node
799
    pugi::xml_node node_output = root.child("output");
447✔
800

801
    // Check for summary option
802
    if (check_for_node(node_output, "summary")) {
447✔
803
      output_summary = get_node_value_bool(node_output, "summary");
428✔
804
    }
805

806
    // Check for ASCII tallies output option
807
    if (check_for_node(node_output, "tallies")) {
447✔
808
      output_tallies = get_node_value_bool(node_output, "tallies");
19✔
809
    }
810

811
    // Set output directory if a path has been specified
812
    if (check_for_node(node_output, "path")) {
447✔
813
      path_output = get_node_value(node_output, "path");
×
814
      if (!ends_with(path_output, "/")) {
×
815
        path_output += "/";
×
816
      }
817
    }
818
  }
819

820
  // Resonance scattering parameters
821
  if (check_for_node(root, "resonance_scattering")) {
6,439✔
822
    xml_node node_res_scat = root.child("resonance_scattering");
19✔
823

824
    // See if resonance scattering is enabled
825
    if (check_for_node(node_res_scat, "enable")) {
19✔
826
      res_scat_on = get_node_value_bool(node_res_scat, "enable");
19✔
827
    } else {
828
      res_scat_on = true;
×
829
    }
830

831
    // Determine what method is used
832
    if (check_for_node(node_res_scat, "method")) {
19✔
833
      auto temp = get_node_value(node_res_scat, "method", true, true);
19✔
834
      if (temp == "rvs") {
19✔
835
        res_scat_method = ResScatMethod::rvs;
19✔
836
      } else if (temp == "dbrc") {
×
837
        res_scat_method = ResScatMethod::dbrc;
×
838
      } else {
839
        fatal_error(
×
840
          "Unrecognized resonance elastic scattering method: " + temp + ".");
×
841
      }
842
    }
19✔
843

844
    // Minimum energy for resonance scattering
845
    if (check_for_node(node_res_scat, "energy_min")) {
19✔
846
      res_scat_energy_min =
19✔
847
        std::stod(get_node_value(node_res_scat, "energy_min"));
19✔
848
    }
849
    if (res_scat_energy_min < 0.0) {
19✔
850
      fatal_error("Lower resonance scattering energy bound is negative");
×
851
    }
852

853
    // Maximum energy for resonance scattering
854
    if (check_for_node(node_res_scat, "energy_max")) {
19✔
855
      res_scat_energy_max =
19✔
856
        std::stod(get_node_value(node_res_scat, "energy_max"));
19✔
857
    }
858
    if (res_scat_energy_max < res_scat_energy_min) {
19✔
859
      fatal_error("Upper resonance scattering energy bound is below the "
×
860
                  "lower resonance scattering energy bound.");
861
    }
862

863
    // Get resonance scattering nuclides
864
    if (check_for_node(node_res_scat, "nuclides")) {
19✔
865
      res_scat_nuclides =
866
        get_node_array<std::string>(node_res_scat, "nuclides");
19✔
867
    }
868
  }
869

870
  // Get volume calculations
871
  for (pugi::xml_node node_vol : root.children("volume_calc")) {
6,815✔
872
    model::volume_calcs.emplace_back(node_vol);
376✔
873
  }
874

875
  // Get temperature settings
876
  if (check_for_node(root, "temperature_default")) {
6,439✔
877
    temperature_default =
217✔
878
      std::stod(get_node_value(root, "temperature_default"));
217✔
879
  }
880
  if (check_for_node(root, "temperature_method")) {
6,439✔
881
    auto temp = get_node_value(root, "temperature_method", true, true);
400✔
882
    if (temp == "nearest") {
400✔
883
      temperature_method = TemperatureMethod::NEAREST;
170✔
884
    } else if (temp == "interpolation") {
230✔
885
      temperature_method = TemperatureMethod::INTERPOLATION;
230✔
886
    } else {
887
      fatal_error("Unknown temperature method: " + temp);
×
888
    }
889
  }
400✔
890
  if (check_for_node(root, "temperature_tolerance")) {
6,439✔
891
    temperature_tolerance =
216✔
892
      std::stod(get_node_value(root, "temperature_tolerance"));
216✔
893
  }
894
  if (check_for_node(root, "temperature_multipole")) {
6,439✔
895
    temperature_multipole = get_node_value_bool(root, "temperature_multipole");
38✔
896

897
    // Multipole currently doesn't work with photon transport
898
    if (temperature_multipole && photon_transport) {
38✔
899
      fatal_error("Multipole data cannot currently be used in conjunction with "
×
900
                  "photon transport.");
901
    }
902
  }
903
  if (check_for_node(root, "temperature_range")) {
6,439✔
904
    auto range = get_node_array<double>(root, "temperature_range");
×
905
    temperature_range[0] = range.at(0);
×
906
    temperature_range[1] = range.at(1);
×
907
  }
908

909
  // Check for tabular_legendre options
910
  if (check_for_node(root, "tabular_legendre")) {
6,439✔
911
    // Get pointer to tabular_legendre node
912
    xml_node node_tab_leg = root.child("tabular_legendre");
114✔
913

914
    // Check for enable option
915
    if (check_for_node(node_tab_leg, "enable")) {
114✔
916
      legendre_to_tabular = get_node_value_bool(node_tab_leg, "enable");
114✔
917
    }
918

919
    // Check for the number of points
920
    if (check_for_node(node_tab_leg, "num_points")) {
114✔
921
      legendre_to_tabular_points =
×
922
        std::stoi(get_node_value(node_tab_leg, "num_points"));
×
923
      if (legendre_to_tabular_points <= 1 && !run_CE) {
×
924
        fatal_error(
×
925
          "The 'num_points' subelement/attribute of the "
926
          "<tabular_legendre> element must contain a value greater than 1");
927
      }
928
    }
929
  }
930

931
  // Check whether create delayed neutrons in fission
932
  if (check_for_node(root, "create_delayed_neutrons")) {
6,439✔
933
    create_delayed_neutrons =
×
934
      get_node_value_bool(root, "create_delayed_neutrons");
×
935
  }
936

937
  // Check whether create fission sites
938
  if (run_mode == RunMode::FIXED_SOURCE) {
6,439✔
939
    if (check_for_node(root, "create_fission_neutrons")) {
1,865✔
940
      create_fission_neutrons =
19✔
941
        get_node_value_bool(root, "create_fission_neutrons");
19✔
942
    }
943
  }
944

945
  // Check whether to scale fission photon yields
946
  if (check_for_node(root, "delayed_photon_scaling")) {
6,439✔
947
    delayed_photon_scaling =
×
948
      get_node_value_bool(root, "delayed_photon_scaling");
×
949
  }
950

951
  // Check whether to use event-based parallelism
952
  if (check_for_node(root, "event_based")) {
6,439✔
953
    event_based = get_node_value_bool(root, "event_based");
×
954
  }
955

956
  // Check whether material cell offsets should be generated
957
  if (check_for_node(root, "material_cell_offsets")) {
6,439✔
958
    material_cell_offsets = get_node_value_bool(root, "material_cell_offsets");
×
959
  }
960

961
  // Weight window information
962
  for (pugi::xml_node node_ww : root.children("weight_windows")) {
6,519✔
963
    variance_reduction::weight_windows.emplace_back(
80✔
964
      std::make_unique<WeightWindows>(node_ww));
160✔
965
  }
966

967
  // Enable weight windows by default if one or more are present
968
  if (variance_reduction::weight_windows.size() > 0)
6,439✔
969
    settings::weight_windows_on = true;
47✔
970

971
  // read weight windows from file
972
  if (check_for_node(root, "weight_windows_file")) {
6,439✔
973
    weight_windows_file = get_node_value(root, "weight_windows_file");
×
974
  }
975

976
  // read settings for weight windows value, this will override
977
  // the automatic setting even if weight windows are present
978
  if (check_for_node(root, "weight_windows_on")) {
6,439✔
979
    weight_windows_on = get_node_value_bool(root, "weight_windows_on");
28✔
980
  }
981

982
  if (check_for_node(root, "max_splits")) {
6,439✔
983
    settings::max_splits = std::stoi(get_node_value(root, "max_splits"));
271✔
984
  }
985

986
  if (check_for_node(root, "max_tracks")) {
6,439✔
987
    settings::max_tracks = std::stoi(get_node_value(root, "max_tracks"));
57✔
988
  }
989

990
  // Create weight window generator objects
991
  if (check_for_node(root, "weight_window_generators")) {
6,439✔
992
    auto wwgs_node = root.child("weight_window_generators");
28✔
993
    for (pugi::xml_node node_wwg :
28✔
994
      wwgs_node.children("weight_windows_generator")) {
84✔
995
      variance_reduction::weight_windows_generators.emplace_back(
28✔
996
        std::make_unique<WeightWindowsGenerator>(node_wwg));
56✔
997
    }
998
    // if any of the weight windows are intended to be generated otf, make sure
999
    // they're applied
1000
    for (const auto& wwg : variance_reduction::weight_windows_generators) {
28✔
1001
      if (wwg->on_the_fly_) {
28✔
1002
        settings::weight_windows_on = true;
28✔
1003
        break;
28✔
1004
      }
1005
    }
1006
  }
1007

1008
  // Set up weight window checkpoints
1009
  if (check_for_node(root, "weight_window_checkpoints")) {
6,439✔
1010
    xml_node ww_checkpoints = root.child("weight_window_checkpoints");
×
1011
    if (check_for_node(ww_checkpoints, "collision")) {
×
1012
      weight_window_checkpoint_collision =
×
1013
        get_node_value_bool(ww_checkpoints, "collision");
×
1014
    }
1015
    if (check_for_node(ww_checkpoints, "surface")) {
×
1016
      weight_window_checkpoint_surface =
×
1017
        get_node_value_bool(ww_checkpoints, "surface");
×
1018
    }
1019
  }
1020
}
6,439✔
1021

1022
void free_memory_settings()
6,602✔
1023
{
1024
  settings::statepoint_batch.clear();
6,602✔
1025
  settings::sourcepoint_batch.clear();
6,602✔
1026
  settings::source_write_surf_id.clear();
6,602✔
1027
  settings::res_scat_nuclides.clear();
6,602✔
1028
}
6,602✔
1029

1030
//==============================================================================
1031
// C API functions
1032
//==============================================================================
1033

1034
extern "C" int openmc_set_n_batches(
70✔
1035
  int32_t n_batches, bool set_max_batches, bool add_statepoint_batch)
1036
{
1037
  if (settings::n_inactive >= n_batches) {
70✔
1038
    set_errmsg("Number of active batches must be greater than zero.");
14✔
1039
    return OPENMC_E_INVALID_ARGUMENT;
14✔
1040
  }
1041

1042
  if (simulation::current_batch >= n_batches) {
56✔
1043
    set_errmsg("Number of batches must be greater than current batch.");
14✔
1044
    return OPENMC_E_INVALID_ARGUMENT;
14✔
1045
  }
1046

1047
  if (!settings::trigger_on) {
42✔
1048
    // Set n_batches and n_max_batches to same value
1049
    settings::n_batches = n_batches;
14✔
1050
    settings::n_max_batches = n_batches;
14✔
1051
  } else {
1052
    // Set n_batches and n_max_batches based on value of set_max_batches
1053
    if (set_max_batches) {
28✔
1054
      settings::n_max_batches = n_batches;
14✔
1055
    } else {
1056
      settings::n_batches = n_batches;
14✔
1057
    }
1058
  }
1059

1060
  // Update size of k_generation and entropy
1061
  int m = settings::n_max_batches * settings::gen_per_batch;
42✔
1062
  simulation::k_generation.reserve(m);
42✔
1063
  simulation::entropy.reserve(m);
42✔
1064

1065
  // Add value of n_batches to statepoint_batch
1066
  if (add_statepoint_batch &&
70✔
1067
      !(contains(settings::statepoint_batch, n_batches)))
28✔
1068
    settings::statepoint_batch.insert(n_batches);
28✔
1069

1070
  return 0;
42✔
1071
}
1072

1073
extern "C" int openmc_get_n_batches(int* n_batches, bool get_max_batches)
5,695✔
1074
{
1075
  *n_batches = get_max_batches ? settings::n_max_batches : settings::n_batches;
5,695✔
1076

1077
  return 0;
5,695✔
1078
}
1079

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