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

openmc-dev / openmc / 18656608910

20 Oct 2025 03:16PM UTC coverage: 81.844% (-3.4%) from 85.218%
18656608910

Pull #3454

github

web-flow
Merge 5eee478a5 into 055ea15a2
Pull Request #3454: Adding variance of variance and normality tests for tally statistics

16610 of 23118 branches covered (71.85%)

Branch coverage included in aggregate %.

188 of 312 new or added lines in 7 files covered. (60.26%)

2157 existing lines in 77 files now uncovered.

53896 of 63029 relevant lines covered (85.51%)

42554448.35 hits per line

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

94.57
/src/simulation.cpp
1
#include "openmc/simulation.h"
2

3
#include "openmc/bank.h"
4
#include "openmc/capi.h"
5
#include "openmc/container_util.h"
6
#include "openmc/eigenvalue.h"
7
#include "openmc/error.h"
8
#include "openmc/event.h"
9
#include "openmc/geometry_aux.h"
10
#include "openmc/ifp.h"
11
#include "openmc/material.h"
12
#include "openmc/mcpl_interface.h"
13
#include "openmc/message_passing.h"
14
#include "openmc/nuclide.h"
15
#include "openmc/output.h"
16
#include "openmc/particle.h"
17
#include "openmc/photon.h"
18
#include "openmc/random_lcg.h"
19
#include "openmc/settings.h"
20
#include "openmc/source.h"
21
#include "openmc/state_point.h"
22
#include "openmc/tallies/derivative.h"
23
#include "openmc/tallies/filter.h"
24
#include "openmc/tallies/tally.h"
25
#include "openmc/tallies/trigger.h"
26
#include "openmc/timer.h"
27
#include "openmc/track_output.h"
28
#include "openmc/weight_windows.h"
29

30
#ifdef _OPENMP
31
#include <omp.h>
32
#endif
33
#include "xtensor/xview.hpp"
34

35
#ifdef OPENMC_MPI
36
#include <mpi.h>
37
#endif
38

39
#include <fmt/format.h>
40

41
#include <algorithm>
42
#include <cmath>
43
#include <string>
44

45
//==============================================================================
46
// C API functions
47
//==============================================================================
48

49
// OPENMC_RUN encompasses all the main logic where iterations are performed
50
// over the batches, generations, and histories in a fixed source or
51
// k-eigenvalue calculation.
52

53
int openmc_run()
5,655✔
54
{
55
  openmc::simulation::time_total.start();
5,655✔
56
  openmc_simulation_init();
5,655✔
57

58
  // Ensure that a batch isn't executed in the case that the maximum number of
59
  // batches has already been run in a restart statepoint file
60
  int status = 0;
5,655✔
61
  if (openmc::simulation::current_batch >= openmc::settings::n_max_batches) {
5,655✔
62
    status = openmc::STATUS_EXIT_MAX_BATCH;
11✔
63
  }
64

65
  int err = 0;
5,655✔
66
  while (status == 0 && err == 0) {
100,032!
67
    err = openmc_next_batch(&status);
94,389✔
68
  }
69

70
  openmc_simulation_finalize();
5,643✔
71
  openmc::simulation::time_total.stop();
5,643✔
72
  return err;
5,643✔
73
}
74

75
int openmc_simulation_init()
6,591✔
76
{
77
  using namespace openmc;
78

79
  // Skip if simulation has already been initialized
80
  if (simulation::initialized)
6,591✔
81
    return 0;
11✔
82

83
  // Initialize nuclear data (energy limits, log grid)
84
  if (settings::run_CE) {
6,580✔
85
    initialize_data();
5,404✔
86
  }
87

88
  // Determine how much work each process should do
89
  calculate_work();
6,580✔
90

91
  // Allocate source, fission and surface source banks.
92
  allocate_banks();
6,580✔
93

94
  // Create track file if needed
95
  if (!settings::track_identifiers.empty() || settings::write_all_tracks) {
6,580✔
96
    open_track_file();
96✔
97
  }
98

99
  // If doing an event-based simulation, intialize the particle buffer
100
  // and event queues
101
  if (settings::event_based) {
6,580✔
102
    int64_t event_buffer_length =
103
      std::min(simulation::work_per_rank, settings::max_particles_in_flight);
177✔
104
    init_event_queues(event_buffer_length);
177✔
105
  }
106

107
  // Allocate tally results arrays if they're not allocated yet
108
  for (auto& t : model::tallies) {
33,259✔
109
    t->set_strides();
26,679✔
110
    t->init_results();
26,679✔
111
  }
112

113
  // Set up material nuclide index mapping
114
  for (auto& mat : model::materials) {
25,278✔
115
    mat->init_nuclide_index();
18,698✔
116
  }
117

118
  // Reset global variables -- this is done before loading state point (as that
119
  // will potentially populate k_generation and entropy)
120
  simulation::current_batch = 0;
6,580✔
121
  simulation::ssw_current_file = 1;
6,580✔
122
  simulation::k_generation.clear();
6,580✔
123
  simulation::entropy.clear();
6,580✔
124
  openmc_reset();
6,580✔
125

126
  // If this is a restart run, load the state point data and binary source
127
  // file
128
  if (settings::restart_run) {
6,580✔
129
    load_state_point();
65✔
130
    write_message("Resuming simulation...", 6);
65✔
131
  } else {
132
    // Only initialize primary source bank for eigenvalue simulations
133
    if (settings::run_mode == RunMode::EIGENVALUE &&
6,515✔
134
        settings::solver_type == SolverType::MONTE_CARLO) {
3,788✔
135
      initialize_source();
3,596✔
136
    }
137
  }
138

139
  // Display header
140
  if (mpi::master) {
6,580✔
141
    if (settings::run_mode == RunMode::FIXED_SOURCE) {
5,570✔
142
      if (settings::solver_type == SolverType::MONTE_CARLO) {
2,443✔
143
        header("FIXED SOURCE TRANSPORT SIMULATION", 3);
2,122✔
144
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
321!
145
        header("FIXED SOURCE TRANSPORT SIMULATION (RANDOM RAY SOLVER)", 3);
321✔
146
      }
147
    } else if (settings::run_mode == RunMode::EIGENVALUE) {
3,127!
148
      if (settings::solver_type == SolverType::MONTE_CARLO) {
3,127✔
149
        header("K EIGENVALUE SIMULATION", 3);
2,995✔
150
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
132!
151
        header("K EIGENVALUE SIMULATION (RANDOM RAY SOLVER)", 3);
132✔
152
      }
153
      if (settings::verbosity >= 7)
3,127✔
154
        print_columns();
2,907✔
155
    }
156
  }
157

158
  // load weight windows from file
159
  if (!settings::weight_windows_file.empty()) {
6,580!
160
    openmc_weight_windows_import(settings::weight_windows_file.c_str());
×
161
  }
162

163
  // Set flag indicating initialization is done
164
  simulation::initialized = true;
6,580✔
165
  return 0;
6,580✔
166
}
167

168
int openmc_simulation_finalize()
6,568✔
169
{
170
  using namespace openmc;
171

172
  // Skip if simulation was never run
173
  if (!simulation::initialized)
6,568!
174
    return 0;
×
175

176
  // Stop active batch timer and start finalization timer
177
  simulation::time_active.stop();
6,568✔
178
  simulation::time_finalize.start();
6,568✔
179

180
  // Clear material nuclide mapping
181
  for (auto& mat : model::materials) {
25,254✔
182
    mat->mat_nuclide_index_.clear();
18,686✔
183
  }
184

185
  // Close track file if open
186
  if (!settings::track_identifiers.empty() || settings::write_all_tracks) {
6,568✔
187
    close_track_file();
96✔
188
  }
189

190
  // Increment total number of generations
191
  simulation::total_gen += simulation::current_batch * settings::gen_per_batch;
6,568✔
192

193
#ifdef OPENMC_MPI
194
  broadcast_results();
3,716✔
195
#endif
196

197
  // Write tally results to tallies.out
198
  if (settings::output_tallies && mpi::master)
6,568!
199
    write_tallies();
5,467✔
200

201
  // If weight window generators are present in this simulation,
202
  // write a weight windows file
203
  if (variance_reduction::weight_windows_generators.size() > 0) {
6,568✔
204
    openmc_weight_windows_export();
131✔
205
  }
206

207
  // Deactivate all tallies
208
  for (auto& t : model::tallies) {
33,247✔
209
    t->active_ = false;
26,679✔
210
  }
211

212
  // Stop timers and show timing statistics
213
  simulation::time_finalize.stop();
6,568✔
214
  simulation::time_total.stop();
6,568✔
215
  if (mpi::master) {
6,568✔
216
    if (settings::solver_type != SolverType::RANDOM_RAY) {
5,558✔
217
      if (settings::verbosity >= 6)
5,105✔
218
        print_runtime();
4,885✔
219
      if (settings::verbosity >= 4)
5,105✔
220
        print_results();
4,885✔
221
    }
222
  }
223
  if (settings::check_overlaps)
6,568!
224
    print_overlap_check();
×
225

226
  // Reset flags
227
  simulation::initialized = false;
6,568✔
228
  return 0;
6,568✔
229
}
230

231
int openmc_next_batch(int* status)
98,514✔
232
{
233
  using namespace openmc;
234
  using openmc::simulation::current_gen;
235

236
  // Make sure simulation has been initialized
237
  if (!simulation::initialized) {
98,514✔
238
    set_errmsg("Simulation has not been initialized yet.");
11✔
239
    return OPENMC_E_ALLOCATE;
11✔
240
  }
241

242
  initialize_batch();
98,503✔
243

244
  // =======================================================================
245
  // LOOP OVER GENERATIONS
246
  for (current_gen = 1; current_gen <= settings::gen_per_batch; ++current_gen) {
197,218✔
247

248
    initialize_generation();
98,727✔
249

250
    // Start timer for transport
251
    simulation::time_transport.start();
98,727✔
252

253
    // Transport loop
254
    if (settings::event_based) {
98,727✔
255
      transport_event_based();
3,102✔
256
    } else {
257
      transport_history_based();
95,625✔
258
    }
259

260
    // Accumulate time for transport
261
    simulation::time_transport.stop();
98,715✔
262

263
    finalize_generation();
98,715✔
264
  }
265

266
  finalize_batch();
98,491✔
267

268
  // Check simulation ending criteria
269
  if (status) {
98,491!
270
    if (simulation::current_batch >= settings::n_max_batches) {
98,491✔
271
      *status = STATUS_EXIT_MAX_BATCH;
5,832✔
272
    } else if (simulation::satisfy_triggers) {
92,659✔
273
      *status = STATUS_EXIT_ON_TRIGGER;
97✔
274
    } else {
275
      *status = STATUS_EXIT_NORMAL;
92,562✔
276
    }
277
  }
278
  return 0;
98,491✔
279
}
280

281
bool openmc_is_statepoint_batch()
3,135✔
282
{
283
  using namespace openmc;
284
  using openmc::simulation::current_gen;
285

286
  if (!simulation::initialized)
3,135!
287
    return false;
×
288
  else
289
    return contains(settings::statepoint_batch, simulation::current_batch);
3,135✔
290
}
291

292
namespace openmc {
293

294
//==============================================================================
295
// Global variables
296
//==============================================================================
297

298
namespace simulation {
299

300
int current_batch;
301
int current_gen;
302
bool initialized {false};
303
double keff {1.0};
304
double keff_std;
305
double k_col_abs {0.0};
306
double k_col_tra {0.0};
307
double k_abs_tra {0.0};
308
double log_spacing;
309
int n_lost_particles {0};
310
bool need_depletion_rx {false};
311
int restart_batch;
312
bool satisfy_triggers {false};
313
int ssw_current_file;
314
int total_gen {0};
315
double total_weight;
316
int64_t work_per_rank;
317

318
const RegularMesh* entropy_mesh {nullptr};
319
const RegularMesh* ufs_mesh {nullptr};
320

321
vector<double> k_generation;
322
vector<int64_t> work_index;
323

324
} // namespace simulation
325

326
//==============================================================================
327
// Non-member functions
328
//==============================================================================
329

330
void allocate_banks()
6,580✔
331
{
332
  if (settings::run_mode == RunMode::EIGENVALUE &&
6,580✔
333
      settings::solver_type == SolverType::MONTE_CARLO) {
3,853✔
334
    // Allocate source bank
335
    simulation::source_bank.resize(simulation::work_per_rank);
3,661✔
336

337
    // Allocate fission bank
338
    init_fission_bank(3 * simulation::work_per_rank);
3,661✔
339

340
    // Allocate IFP bank
341
    if (settings::ifp_on) {
3,661✔
342
      resize_simulation_ifp_banks();
76✔
343
    }
344
  }
345

346
  if (settings::surf_source_write) {
6,580✔
347
    // Allocate surface source bank
348
    simulation::surf_source_bank.reserve(settings::ssw_max_particles);
928✔
349
  }
350
}
6,580✔
351

352
void initialize_batch()
115,475✔
353
{
354
  // Increment current batch
355
  ++simulation::current_batch;
115,475✔
356
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
115,475✔
357
    if (settings::solver_type == SolverType::RANDOM_RAY &&
35,030✔
358
        simulation::current_batch < settings::n_inactive + 1) {
13,932✔
359
      write_message(
8,486✔
360
        6, "Simulating batch {:<4} (inactive)", simulation::current_batch);
361
    } else {
362
      write_message(6, "Simulating batch {}", simulation::current_batch);
26,544✔
363
    }
364
  }
365

366
  // Reset total starting particle weight used for normalizing tallies
367
  simulation::total_weight = 0.0;
115,475✔
368

369
  // Determine if this batch is the first inactive or active batch.
370
  bool first_inactive = false;
115,475✔
371
  bool first_active = false;
115,475✔
372
  if (!settings::restart_run) {
115,475✔
373
    first_inactive = settings::n_inactive > 0 && simulation::current_batch == 1;
115,307✔
374
    first_active = simulation::current_batch == settings::n_inactive + 1;
115,307✔
375
  } else if (simulation::current_batch == simulation::restart_batch + 1) {
168✔
376
    first_inactive = simulation::restart_batch < settings::n_inactive;
54✔
377
    first_active = !first_inactive;
54✔
378
  }
379

380
  // Manage active/inactive timers and activate tallies if necessary.
381
  if (first_inactive) {
115,475✔
382
    simulation::time_inactive.start();
3,303✔
383
  } else if (first_active) {
112,172✔
384
    simulation::time_inactive.stop();
6,533✔
385
    simulation::time_active.start();
6,533✔
386
    for (auto& t : model::tallies) {
33,190✔
387
      t->active_ = true;
26,657✔
388
    }
389
  }
390

391
  // Add user tallies to active tallies list
392
  setup_active_tallies();
115,475✔
393
}
115,475✔
394

395
void finalize_batch()
115,463✔
396
{
397
  // Reduce tallies onto master process and accumulate
398
  simulation::time_tallies.start();
115,463✔
399
  accumulate_tallies();
115,463✔
400
  simulation::time_tallies.stop();
115,463✔
401

402
  // update weight windows if needed
403
  if (settings::solver_type != SolverType::RANDOM_RAY ||
115,463✔
404
      simulation::current_batch == settings::n_batches) {
16,972✔
405
    for (const auto& wwg : variance_reduction::weight_windows_generators) {
99,412✔
406
      wwg->update();
263✔
407
    }
408
  }
409

410
  // Reset global tally results
411
  if (simulation::current_batch <= settings::n_inactive) {
115,463✔
412
    xt::view(simulation::global_tallies, xt::all()) = 0.0;
26,368✔
413
    simulation::n_realizations = 0;
26,368✔
414
  }
415

416
  // Check_triggers
417
  if (mpi::master)
115,463✔
418
    check_triggers();
93,663✔
419
#ifdef OPENMC_MPI
420
  MPI_Bcast(&simulation::satisfy_triggers, 1, MPI_C_BOOL, 0, mpi::intracomm);
66,182✔
421
#endif
422
  if (simulation::satisfy_triggers ||
115,463✔
423
      (settings::trigger_on &&
2,645✔
424
        simulation::current_batch == settings::n_max_batches)) {
2,645✔
425
    settings::statepoint_batch.insert(simulation::current_batch);
146✔
426
  }
427

428
  // Write out state point if it's been specified for this batch and is not
429
  // a CMFD run instance
430
  if (contains(settings::statepoint_batch, simulation::current_batch) &&
122,265✔
431
      !settings::cmfd_run) {
6,802✔
432
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
13,031✔
433
        settings::source_write && !settings::source_separate) {
13,031✔
434
      bool b = (settings::run_mode == RunMode::EIGENVALUE);
5,672✔
435
      openmc_statepoint_write(nullptr, &b);
5,672✔
436
    } else {
437
      bool b = false;
954✔
438
      openmc_statepoint_write(nullptr, &b);
954✔
439
    }
440
  }
441

442
  if (settings::run_mode == RunMode::EIGENVALUE) {
115,463✔
443
    // Write out a separate source point if it's been specified for this batch
444
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
84,347✔
445
        settings::source_write && settings::source_separate) {
84,347✔
446

447
      // Determine width for zero padding
448
      int w = std::to_string(settings::n_max_batches).size();
75✔
449
      std::string source_point_filename = fmt::format("{0}source.{1:0{2}}",
450
        settings::path_output, simulation::current_batch, w);
61✔
451
      span<SourceSite> bankspan(simulation::source_bank);
75✔
452
      write_source_point(source_point_filename, bankspan,
75✔
453
        simulation::work_index, settings::source_mcpl_write);
454
    }
75✔
455

456
    // Write a continously-overwritten source point if requested.
457
    if (settings::source_latest) {
80,445✔
458
      auto filename = settings::path_output + "source";
160✔
459
      span<SourceSite> bankspan(simulation::source_bank);
160✔
460
      write_source_point(filename, bankspan, simulation::work_index,
160✔
461
        settings::source_mcpl_write);
462
    }
160✔
463
  }
464

465
  // Write out surface source if requested.
466
  if (settings::surf_source_write &&
115,463✔
467
      simulation::ssw_current_file <= settings::ssw_max_files) {
8,809✔
468
    bool last_batch = (simulation::current_batch == settings::n_batches);
1,750✔
469
    if (simulation::surf_source_bank.full() || last_batch) {
1,750✔
470
      // Determine appropriate filename
471
      auto filename = fmt::format("{}surface_source.{}", settings::path_output,
472
        simulation::current_batch);
785✔
473
      if (settings::ssw_max_files == 1 ||
961✔
474
          (simulation::ssw_current_file == 1 && last_batch)) {
55!
475
        filename = settings::path_output + "surface_source";
906✔
476
      }
477

478
      // Get span of source bank and calculate parallel index vector
479
      auto surf_work_index = mpi::calculate_parallel_index_vector(
480
        simulation::surf_source_bank.size());
961✔
481
      span<SourceSite> surfbankspan(simulation::surf_source_bank.begin(),
482
        simulation::surf_source_bank.size());
961✔
483

484
      // Write surface source file
485
      write_source_point(
961✔
486
        filename, surfbankspan, surf_work_index, settings::surf_mcpl_write);
487

488
      // Reset surface source bank and increment counter
489
      simulation::surf_source_bank.clear();
961✔
490
      if (!last_batch && settings::ssw_max_files >= 1) {
961!
491
        simulation::surf_source_bank.reserve(settings::ssw_max_particles);
779✔
492
      }
493
      ++simulation::ssw_current_file;
961✔
494
    }
961✔
495
  }
496
}
115,463✔
497

498
void initialize_generation()
115,699✔
499
{
500
  if (settings::run_mode == RunMode::EIGENVALUE) {
115,699✔
501
    // Clear out the fission bank
502
    simulation::fission_bank.resize(0);
80,669✔
503

504
    // Count source sites if using uniform fission source weighting
505
    if (settings::ufs_on)
80,669✔
506
      ufs_count_sites();
160✔
507

508
    // Store current value of tracklength k
509
    simulation::keff_generation = simulation::global_tallies(
80,669✔
510
      GlobalTally::K_TRACKLENGTH, TallyResult::VALUE);
511
  }
512
}
115,699✔
513

514
void finalize_generation()
115,687✔
515
{
516
  auto& gt = simulation::global_tallies;
115,687✔
517

518
  // Update global tallies with the accumulation variables
519
  if (settings::run_mode == RunMode::EIGENVALUE) {
115,687✔
520
    gt(GlobalTally::K_COLLISION, TallyResult::VALUE) += global_tally_collision;
80,669✔
521
    gt(GlobalTally::K_ABSORPTION, TallyResult::VALUE) +=
80,669✔
522
      global_tally_absorption;
523
    gt(GlobalTally::K_TRACKLENGTH, TallyResult::VALUE) +=
80,669✔
524
      global_tally_tracklength;
525
  }
526
  gt(GlobalTally::LEAKAGE, TallyResult::VALUE) += global_tally_leakage;
115,687✔
527

528
  // reset tallies
529
  if (settings::run_mode == RunMode::EIGENVALUE) {
115,687✔
530
    global_tally_collision = 0.0;
80,669✔
531
    global_tally_absorption = 0.0;
80,669✔
532
    global_tally_tracklength = 0.0;
80,669✔
533
  }
534
  global_tally_leakage = 0.0;
115,687✔
535

536
  if (settings::run_mode == RunMode::EIGENVALUE &&
115,687✔
537
      settings::solver_type == SolverType::MONTE_CARLO) {
80,669✔
538
    // If using shared memory, stable sort the fission bank (by parent IDs)
539
    // so as to allow for reproducibility regardless of which order particles
540
    // are run in.
541
    sort_fission_bank();
77,629✔
542

543
    // Distribute fission bank across processors evenly
544
    synchronize_bank();
77,629✔
545
  }
546

547
  if (settings::run_mode == RunMode::EIGENVALUE) {
115,687✔
548

549
    // Calculate shannon entropy
550
    if (settings::entropy_on &&
80,669✔
551
        settings::solver_type == SolverType::MONTE_CARLO)
10,735✔
552
      shannon_entropy();
7,695✔
553

554
    // Collect results and statistics
555
    calculate_generation_keff();
80,669✔
556
    calculate_average_keff();
80,669✔
557

558
    // Write generation output
559
    if (mpi::master && settings::verbosity >= 7) {
80,669✔
560
      print_generation();
61,309✔
561
    }
562
  }
563
}
115,687✔
564

565
void initialize_history(Particle& p, int64_t index_source)
160,470,907✔
566
{
567
  // set defaults
568
  if (settings::run_mode == RunMode::EIGENVALUE) {
160,470,907✔
569
    // set defaults for eigenvalue simulations from primary bank
570
    p.from_source(&simulation::source_bank[index_source - 1]);
134,653,600✔
571
  } else if (settings::run_mode == RunMode::FIXED_SOURCE) {
25,817,307!
572
    // initialize random number seed
573
    int64_t id = (simulation::total_gen + overall_generation() - 1) *
25,817,307✔
574
                   settings::n_particles +
25,817,307✔
575
                 simulation::work_index[mpi::rank] + index_source;
25,817,307✔
576
    uint64_t seed = init_seed(id, STREAM_SOURCE);
25,817,307✔
577
    // sample from external source distribution or custom library then set
578
    auto site = sample_external_source(&seed);
25,817,307✔
579
    p.from_source(&site);
25,817,304✔
580
  }
581
  p.current_work() = index_source;
160,470,904✔
582

583
  // set identifier for particle
584
  p.id() = simulation::work_index[mpi::rank] + index_source;
160,470,904✔
585

586
  // set progeny count to zero
587
  p.n_progeny() = 0;
160,470,904✔
588

589
  // Reset particle event counter
590
  p.n_event() = 0;
160,470,904✔
591

592
  // Reset split counter
593
  p.n_split() = 0;
160,470,904✔
594

595
  // Reset weight window ratio
596
  p.ww_factor() = 0.0;
160,470,904✔
597

598
  // set particle history start weight
599
  p.wgt_born() = p.wgt();
160,470,904✔
600

601
  // Reset pulse_height_storage
602
  std::fill(p.pht_storage().begin(), p.pht_storage().end(), 0);
160,470,904✔
603

604
  // set random number seed
605
  int64_t particle_seed =
606
    (simulation::total_gen + overall_generation() - 1) * settings::n_particles +
160,470,904✔
607
    p.id();
160,470,904✔
608
  init_particle_seeds(particle_seed, p.seeds());
160,470,904✔
609

610
  // set particle trace
611
  p.trace() = false;
160,470,904✔
612
  if (simulation::current_batch == settings::trace_batch &&
320,952,808✔
613
      simulation::current_gen == settings::trace_gen &&
160,481,904!
614
      p.id() == settings::trace_particle)
11,000✔
615
    p.trace() = true;
11✔
616

617
  // Set particle track.
618
  p.write_track() = check_track_criteria(p);
160,470,904✔
619

620
  // Set the particle's initial weight window value.
621
  p.wgt_ww_born() = -1.0;
160,470,904✔
622
  apply_weight_windows(p);
160,470,904✔
623

624
  // Display message if high verbosity or trace is on
625
  if (settings::verbosity >= 9 || p.trace()) {
160,470,904!
626
    write_message("Simulating Particle {}", p.id());
11✔
627
  }
628

629
// Add particle's starting weight to count for normalizing tallies later
630
#pragma omp atomic
87,903,960✔
631
  simulation::total_weight += p.wgt();
160,470,904✔
632

633
  // Force calculation of cross-sections by setting last energy to zero
634
  if (settings::run_CE) {
160,470,904✔
635
    p.invalidate_neutron_xs();
48,446,904✔
636
  }
637

638
  // Prepare to write out particle track.
639
  if (p.write_track())
160,470,904✔
640
    add_particle_track(p);
1,059✔
641
}
160,470,904✔
642

643
int overall_generation()
186,508,076✔
644
{
645
  using namespace simulation;
646
  return settings::gen_per_batch * (current_batch - 1) + current_gen;
186,508,076✔
647
}
648

649
void calculate_work()
6,580✔
650
{
651
  // Determine minimum amount of particles to simulate on each processor
652
  int64_t min_work = settings::n_particles / mpi::n_procs;
6,580✔
653

654
  // Determine number of processors that have one extra particle
655
  int64_t remainder = settings::n_particles % mpi::n_procs;
6,580✔
656

657
  int64_t i_bank = 0;
6,580✔
658
  simulation::work_index.resize(mpi::n_procs + 1);
6,580✔
659
  simulation::work_index[0] = 0;
6,580✔
660
  for (int i = 0; i < mpi::n_procs; ++i) {
15,179✔
661
    // Number of particles for rank i
662
    int64_t work_i = i < remainder ? min_work + 1 : min_work;
8,599!
663

664
    // Set number of particles
665
    if (mpi::rank == i)
8,599✔
666
      simulation::work_per_rank = work_i;
6,580✔
667

668
    // Set index into source bank for rank i
669
    i_bank += work_i;
8,599✔
670
    simulation::work_index[i + 1] = i_bank;
8,599✔
671
  }
672
}
6,580✔
673

674
void initialize_data()
5,437✔
675
{
676
  // Determine minimum/maximum energy for incident neutron/photon data
677
  data::energy_max = {INFTY, INFTY, INFTY, INFTY};
5,437✔
678
  data::energy_min = {0.0, 0.0, 0.0, 0.0};
5,437✔
679

680
  for (const auto& nuc : data::nuclides) {
31,820✔
681
    if (nuc->grid_.size() >= 1) {
26,383!
682
      int neutron = static_cast<int>(ParticleType::neutron);
26,383✔
683
      data::energy_min[neutron] =
52,766✔
684
        std::max(data::energy_min[neutron], nuc->grid_[0].energy.front());
26,383✔
685
      data::energy_max[neutron] =
26,383✔
686
        std::min(data::energy_max[neutron], nuc->grid_[0].energy.back());
26,383✔
687
    }
688
  }
689

690
  if (settings::photon_transport) {
5,437✔
691
    for (const auto& elem : data::elements) {
875✔
692
      if (elem->energy_.size() >= 1) {
589!
693
        int photon = static_cast<int>(ParticleType::photon);
589✔
694
        int n = elem->energy_.size();
589✔
695
        data::energy_min[photon] =
1,178✔
696
          std::max(data::energy_min[photon], std::exp(elem->energy_(1)));
589✔
697
        data::energy_max[photon] =
1,178✔
698
          std::min(data::energy_max[photon], std::exp(elem->energy_(n - 1)));
589✔
699
      }
700
    }
701

702
    if (settings::electron_treatment == ElectronTreatment::TTB) {
286✔
703
      // Determine if minimum/maximum energy for bremsstrahlung is greater/less
704
      // than the current minimum/maximum
705
      if (data::ttb_e_grid.size() >= 1) {
275!
706
        int photon = static_cast<int>(ParticleType::photon);
275✔
707
        int electron = static_cast<int>(ParticleType::electron);
275✔
708
        int positron = static_cast<int>(ParticleType::positron);
275✔
709
        int n_e = data::ttb_e_grid.size();
275✔
710

711
        const std::vector<int> charged = {electron, positron};
275✔
712
        for (auto t : charged) {
825✔
713
          data::energy_min[t] = std::exp(data::ttb_e_grid(1));
550✔
714
          data::energy_max[t] = std::exp(data::ttb_e_grid(n_e - 1));
550✔
715
        }
716

717
        data::energy_min[photon] =
550✔
718
          std::max(data::energy_min[photon], data::energy_min[electron]);
275✔
719

720
        data::energy_max[photon] =
550✔
721
          std::min(data::energy_max[photon], data::energy_max[electron]);
275✔
722
      }
275✔
723
    }
724
  }
725

726
  // Show which nuclide results in lowest energy for neutron transport
727
  for (const auto& nuc : data::nuclides) {
6,658✔
728
    // If a nuclide is present in a material that's not used in the model, its
729
    // grid has not been allocated
730
    if (nuc->grid_.size() > 0) {
6,211!
731
      double max_E = nuc->grid_[0].energy.back();
6,211✔
732
      int neutron = static_cast<int>(ParticleType::neutron);
6,211✔
733
      if (max_E == data::energy_max[neutron]) {
6,211✔
734
        write_message(7, "Maximum neutron transport energy: {} eV for {}",
4,990✔
735
          data::energy_max[neutron], nuc->name_);
4,990✔
736
        if (mpi::master && data::energy_max[neutron] < 20.0e6) {
4,990!
UNCOV
737
          warning("Maximum neutron energy is below 20 MeV. This may bias "
×
738
                  "the results.");
739
        }
740
        break;
4,990✔
741
      }
742
    }
743
  }
744

745
  // Set up logarithmic grid for nuclides
746
  for (auto& nuc : data::nuclides) {
31,820✔
747
    nuc->init_grid();
26,383✔
748
  }
749
  int neutron = static_cast<int>(ParticleType::neutron);
5,437✔
750
  simulation::log_spacing =
5,437✔
751
    std::log(data::energy_max[neutron] / data::energy_min[neutron]) /
5,437✔
752
    settings::n_log_bins;
753
}
5,437✔
754

755
#ifdef OPENMC_MPI
756
void broadcast_results()
3,716✔
757
{
758
  // Broadcast tally results so that each process has access to results
759
  for (auto& t : model::tallies) {
19,915✔
760
    // Create a new datatype that consists of all values for a given filter
761
    // bin and then use that to broadcast. This is done to minimize the
762
    // chance of the 'count' argument of MPI_BCAST exceeding 2**31
763
    auto& results = t->results_;
16,199✔
764

765
    auto shape = results.shape();
16,199✔
766
    int count_per_filter = shape[1] * shape[2];
16,199✔
767
    MPI_Datatype result_block;
768
    MPI_Type_contiguous(count_per_filter, MPI_DOUBLE, &result_block);
16,199✔
769
    MPI_Type_commit(&result_block);
16,199✔
770
    MPI_Bcast(results.data(), shape[0], result_block, 0, mpi::intracomm);
16,199✔
771
    MPI_Type_free(&result_block);
16,199✔
772
  }
773

774
  // Also broadcast global tally results
775
  auto& gt = simulation::global_tallies;
3,716✔
776
  MPI_Bcast(gt.data(), gt.size(), MPI_DOUBLE, 0, mpi::intracomm);
3,716✔
777

778
  // These guys are needed so that non-master processes can calculate the
779
  // combined estimate of k-effective
780
  double temp[] {
781
    simulation::k_col_abs, simulation::k_col_tra, simulation::k_abs_tra};
3,716✔
782
  MPI_Bcast(temp, 3, MPI_DOUBLE, 0, mpi::intracomm);
3,716✔
783
  simulation::k_col_abs = temp[0];
3,716✔
784
  simulation::k_col_tra = temp[1];
3,716✔
785
  simulation::k_abs_tra = temp[2];
3,716✔
786
}
3,716✔
787

788
#endif
789

790
void free_memory_simulation()
7,771✔
791
{
792
  simulation::k_generation.clear();
7,771✔
793
  simulation::entropy.clear();
7,771✔
794
}
7,771✔
795

796
void transport_history_based_single_particle(Particle& p)
148,372,737✔
797
{
798
  while (p.alive()) {
2,147,483,647✔
799
    p.event_calculate_xs();
2,147,483,647✔
800
    if (p.alive()) {
2,147,483,647!
801
      p.event_advance();
2,147,483,647✔
802
    }
803
    if (p.alive()) {
2,147,483,647✔
804
      if (p.collision_distance() > p.boundary().distance()) {
2,147,483,647✔
805
        p.event_cross_surface();
1,234,455,966✔
806
      } else if (p.alive()) {
2,147,483,647!
807
        p.event_collide();
2,147,483,647✔
808
      }
809
    }
810
    p.event_revive_from_secondary();
2,147,483,647✔
811
  }
812
  p.event_death();
148,372,728✔
813
}
148,372,728✔
814

815
void transport_history_based()
95,625✔
816
{
817
#pragma omp parallel for schedule(runtime)
818
  for (int64_t i_work = 1; i_work <= simulation::work_per_rank; ++i_work) {
72,674,539✔
819
    Particle p;
72,631,073✔
820
    initialize_history(p, i_work);
72,631,073✔
821
    transport_history_based_single_particle(p);
72,631,070✔
822
  }
72,631,065✔
823
}
95,617✔
824

825
void transport_event_based()
3,102✔
826
{
827
  int64_t remaining_work = simulation::work_per_rank;
3,102✔
828
  int64_t source_offset = 0;
3,102✔
829

830
  // To cap the total amount of memory used to store particle object data, the
831
  // number of particles in flight at any point in time can bet set. In the case
832
  // that the maximum in flight particle count is lower than the total number
833
  // of particles that need to be run this iteration, the event-based transport
834
  // loop is executed multiple times until all particles have been completed.
835
  while (remaining_work > 0) {
6,204✔
836
    // Figure out # of particles to run for this subiteration
837
    int64_t n_particles =
838
      std::min(remaining_work, settings::max_particles_in_flight);
3,102✔
839

840
    // Initialize all particle histories for this subiteration
841
    process_init_events(n_particles, source_offset);
3,102!
842

843
    // Event-based transport loop
844
    while (true) {
845
      // Determine which event kernel has the longest queue
846
      int64_t max = std::max({simulation::calculate_fuel_xs_queue.size(),
4,698,088!
847
        simulation::calculate_nonfuel_xs_queue.size(),
2,349,044✔
848
        simulation::advance_particle_queue.size(),
2,349,044✔
849
        simulation::surface_crossing_queue.size(),
2,349,044✔
850
        simulation::collision_queue.size()});
2,349,044✔
851

852
      // Execute event with the longest queue
853
      if (max == 0) {
2,349,044✔
854
        break;
3,102✔
855
      } else if (max == simulation::calculate_fuel_xs_queue.size()) {
2,345,942✔
856
        process_calculate_xs_events(simulation::calculate_fuel_xs_queue);
423,192!
857
      } else if (max == simulation::calculate_nonfuel_xs_queue.size()) {
1,922,750✔
858
        process_calculate_xs_events(simulation::calculate_nonfuel_xs_queue);
356,217!
859
      } else if (max == simulation::advance_particle_queue.size()) {
1,566,533✔
860
        process_advance_particle_events();
774,269!
861
      } else if (max == simulation::surface_crossing_queue.size()) {
792,264✔
862
        process_surface_crossing_events();
256,949!
863
      } else if (max == simulation::collision_queue.size()) {
535,315!
864
        process_collision_events();
535,315!
865
      }
866
    }
2,345,942✔
867

868
    // Execute death event for all particles
869
    process_death_events(n_particles);
3,102!
870

871
    // Adjust remaining work and source offset variables
872
    remaining_work -= n_particles;
3,102✔
873
    source_offset += n_particles;
3,102✔
874
  }
875
}
3,102✔
876

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