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

openmc-dev / openmc / 21048058101

15 Jan 2026 10:09PM UTC coverage: 81.95%. First build
21048058101

Pull #3728

github

web-flow
Merge 7298a3323 into 179048b80
Pull Request #3728: Add delay to activation of IFP tallies.

17156 of 23812 branches covered (72.05%)

Branch coverage included in aggregate %.

78 of 83 new or added lines in 9 files covered. (93.98%)

55598 of 64967 relevant lines covered (85.58%)

43406907.89 hits per line

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

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

3
#include "openmc/bank.h"
4
#include "openmc/capi.h"
5
#include "openmc/collision_track.h"
6
#include "openmc/container_util.h"
7
#include "openmc/eigenvalue.h"
8
#include "openmc/error.h"
9
#include "openmc/event.h"
10
#include "openmc/geometry_aux.h"
11
#include "openmc/ifp.h"
12
#include "openmc/material.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,982✔
54
{
55
  openmc::simulation::time_total.start();
5,982✔
56
  openmc_simulation_init();
5,982✔
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,982✔
61
  if (openmc::simulation::current_batch >= openmc::settings::n_max_batches) {
5,982✔
62
    status = openmc::STATUS_EXIT_MAX_BATCH;
11✔
63
  }
64

65
  int err = 0;
5,982✔
66
  while (status == 0 && err == 0) {
116,600!
67
    err = openmc_next_batch(&status);
110,630✔
68
  }
69

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

75
int openmc_simulation_init()
7,014✔
76
{
77
  using namespace openmc;
78

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

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

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

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

94
  // Create track file if needed
95
  if (!settings::track_identifiers.empty() || settings::write_all_tracks) {
7,003✔
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) {
7,003✔
102
    int64_t event_buffer_length =
103
      std::min(simulation::work_per_rank, settings::max_particles_in_flight);
197✔
104
    init_event_queues(event_buffer_length);
197✔
105
  }
106

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

113
  // Set up material nuclide index mapping
114
  for (auto& mat : model::materials) {
25,098✔
115
    mat->init_nuclide_index();
18,095✔
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;
7,003✔
121
  simulation::ct_current_file = 1;
7,003✔
122
  simulation::ssw_current_file = 1;
7,003✔
123
  simulation::k_generation.clear();
7,003✔
124
  simulation::entropy.clear();
7,003✔
125
  openmc_reset();
7,003✔
126

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

140
  // Display header
141
  if (mpi::master) {
7,003✔
142
    if (settings::run_mode == RunMode::FIXED_SOURCE) {
5,883✔
143
      if (settings::solver_type == SolverType::MONTE_CARLO) {
2,431✔
144
        header("FIXED SOURCE TRANSPORT SIMULATION", 3);
2,099✔
145
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
332!
146
        header("FIXED SOURCE TRANSPORT SIMULATION (RANDOM RAY SOLVER)", 3);
332✔
147
      }
148
    } else if (settings::run_mode == RunMode::EIGENVALUE) {
3,452!
149
      if (settings::solver_type == SolverType::MONTE_CARLO) {
3,452✔
150
        header("K EIGENVALUE SIMULATION", 3);
3,265✔
151
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
187!
152
        header("K EIGENVALUE SIMULATION (RANDOM RAY SOLVER)", 3);
187✔
153
      }
154
      if (settings::verbosity >= 7)
3,452✔
155
        print_columns();
3,252✔
156
    }
157
  }
158

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

164
  // Set flag indicating initialization is done
165
  simulation::initialized = true;
7,003✔
166
  return 0;
7,003✔
167
}
168

169
int openmc_simulation_finalize()
6,991✔
170
{
171
  using namespace openmc;
172

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

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

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

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

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

194
#ifdef OPENMC_MPI
195
  broadcast_results();
3,804✔
196
#endif
197

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

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

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

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

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

232
int openmc_next_batch(int* status)
114,755✔
233
{
234
  using namespace openmc;
235
  using openmc::simulation::current_gen;
236

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

243
  initialize_batch();
114,744✔
244

245
  // =======================================================================
246
  // LOOP OVER GENERATIONS
247
  for (current_gen = 1; current_gen <= settings::gen_per_batch; ++current_gen) {
229,700✔
248

249
    initialize_generation();
114,968✔
250

251
    // Start timer for transport
252
    simulation::time_transport.start();
114,968✔
253

254
    // Transport loop
255
    if (settings::event_based) {
114,968✔
256
      transport_event_based();
3,172✔
257
    } else {
258
      transport_history_based();
111,796✔
259
    }
260

261
    // Accumulate time for transport
262
    simulation::time_transport.stop();
114,956✔
263

264
    finalize_generation();
114,956✔
265
  }
266

267
  finalize_batch();
114,732✔
268

269
  // Check simulation ending criteria
270
  if (status) {
114,732!
271
    if (simulation::current_batch >= settings::n_max_batches) {
114,732✔
272
      *status = STATUS_EXIT_MAX_BATCH;
6,159✔
273
    } else if (simulation::satisfy_triggers) {
108,573✔
274
      *status = STATUS_EXIT_ON_TRIGGER;
97✔
275
    } else {
276
      *status = STATUS_EXIT_NORMAL;
108,476✔
277
    }
278
  }
279
  return 0;
114,732✔
280
}
281

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

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

293
namespace openmc {
294

295
//==============================================================================
296
// Global variables
297
//==============================================================================
298

299
namespace simulation {
300

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

322
const RegularMesh* entropy_mesh {nullptr};
323
const RegularMesh* ufs_mesh {nullptr};
324

325
vector<double> k_generation;
326
vector<int64_t> work_index;
327

328
} // namespace simulation
329

330
//==============================================================================
331
// Non-member functions
332
//==============================================================================
333

334
void allocate_banks()
7,003✔
335
{
336
  if (settings::run_mode == RunMode::EIGENVALUE &&
7,003✔
337
      settings::solver_type == SolverType::MONTE_CARLO) {
4,268✔
338
    // Allocate source bank
339
    simulation::source_bank.resize(simulation::work_per_rank);
3,996✔
340

341
    // Allocate fission bank
342
    init_fission_bank(3 * simulation::work_per_rank);
3,996✔
343

344
    // Allocate IFP bank
345
    resize_simulation_ifp_banks();
3,996✔
346
  }
347

348
  if (settings::surf_source_write) {
7,003✔
349
    // Allocate surface source bank
350
    simulation::surf_source_bank.reserve(settings::ssw_max_particles);
974✔
351
  }
352

353
  if (settings::collision_track) {
7,003✔
354
    // Allocate collision track bank
355
    collision_track_reserve_bank();
155✔
356
  }
357
}
7,003✔
358

359
void initialize_batch()
132,676✔
360
{
361
  // Increment current batch
362
  ++simulation::current_batch;
132,676✔
363
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
132,676✔
364
    if (settings::solver_type == SolverType::RANDOM_RAY &&
47,007✔
365
        simulation::current_batch < settings::n_inactive + 1) {
14,092✔
366
      write_message(
8,566✔
367
        6, "Simulating batch {:<4} (inactive)", simulation::current_batch);
368
    } else {
369
      write_message(6, "Simulating batch {}", simulation::current_batch);
38,441✔
370
    }
371
  }
372

373
  // Reset total starting particle weight used for normalizing tallies
374
  simulation::total_weight = 0.0;
132,676✔
375

376
  // Determine if this batch is the first inactive or active batch.
377
  bool first_inactive = false;
132,676✔
378
  bool first_active = false;
132,676✔
379
  if (!settings::restart_run) {
132,676✔
380
    first_inactive = settings::n_inactive > 0 && simulation::current_batch == 1;
132,508✔
381
    first_active = simulation::current_batch == settings::n_inactive + 1;
132,508✔
382
  } else if (simulation::current_batch == simulation::restart_batch + 1) {
168✔
383
    first_inactive = simulation::restart_batch < settings::n_inactive;
54✔
384
    first_active = !first_inactive;
54✔
385
  }
386

387
  // Manage active/inactive timers and activate tallies if necessary.
388
  if (first_inactive) {
132,676✔
389
    simulation::time_inactive.start();
3,606✔
390
  } else if (first_active) {
129,070✔
391
    simulation::time_inactive.stop();
6,956✔
392
    simulation::time_active.start();
6,956✔
393
    for (auto& t : model::tallies) {
33,689✔
394
      if (t->offset_ == 0)
26,733✔
395
        t->active_ = true;
26,537✔
396
    }
397
  }
398

399
  // Activate tallies which have activation offset.
400
  for (auto& t : model::tallies) {
525,719✔
401
    if (t->offset_ > 0) {
393,043✔
402
      if (!settings::restart_run) {
3,920!
403
        if (simulation::current_batch == settings::n_inactive + 1 + t->offset_)
3,920✔
404
          t->active_ = true;
196✔
NEW
405
      } else if (simulation::current_batch == simulation::restart_batch + 1) {
×
NEW
406
        if (simulation::restart_batch >= settings::n_inactive + t->offset_)
×
NEW
407
          t->active_ = true;
×
408
      }
409
    }
410
  }
411

412
  // Add user tallies to active tallies list
413
  setup_active_tallies();
132,676✔
414
}
132,676✔
415

416
void finalize_batch()
132,664✔
417
{
418
  // Reduce tallies onto master process and accumulate
419
  simulation::time_tallies.start();
132,664✔
420
  accumulate_tallies();
132,664✔
421
  simulation::time_tallies.stop();
132,664✔
422

423
  // update weight windows if needed
424
  for (const auto& wwg : variance_reduction::weight_windows_generators) {
135,081✔
425
    wwg->update();
2,417✔
426
  }
427

428
  // Reset global tally results
429
  if (simulation::current_batch <= settings::n_inactive) {
132,664✔
430
    xt::view(simulation::global_tallies, xt::all()) = 0.0;
26,755✔
431
    simulation::n_realizations = 0;
26,755✔
432
  }
433

434
  // Check_triggers
435
  if (mpi::master)
132,664✔
436
    check_triggers();
110,114✔
437
#ifdef OPENMC_MPI
438
  MPI_Bcast(&simulation::satisfy_triggers, 1, MPI_C_BOOL, 0, mpi::intracomm);
72,756✔
439
#endif
440
  if (simulation::satisfy_triggers ||
132,664✔
441
      (settings::trigger_on &&
2,645✔
442
        simulation::current_batch == settings::n_max_batches)) {
2,645✔
443
    settings::statepoint_batch.insert(simulation::current_batch);
146✔
444
  }
445

446
  // Write out state point if it's been specified for this batch and is not
447
  // a CMFD run instance
448
  if (contains(settings::statepoint_batch, simulation::current_batch) &&
139,889✔
449
      !settings::cmfd_run) {
7,225✔
450
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
13,877✔
451
        settings::source_write && !settings::source_separate) {
13,877✔
452
      bool b = (settings::run_mode == RunMode::EIGENVALUE);
5,999✔
453
      openmc_statepoint_write(nullptr, &b);
5,999✔
454
    } else {
455
      bool b = false;
1,050✔
456
      openmc_statepoint_write(nullptr, &b);
1,050✔
457
    }
458
  }
459

460
  if (settings::run_mode == RunMode::EIGENVALUE) {
132,664✔
461
    // Write out a separate source point if it's been specified for this batch
462
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
89,986✔
463
        settings::source_write && settings::source_separate) {
89,986✔
464

465
      // Determine width for zero padding
466
      int w = std::to_string(settings::n_max_batches).size();
75✔
467
      std::string source_point_filename = fmt::format("{0}source.{1:0{2}}",
468
        settings::path_output, simulation::current_batch, w);
61✔
469
      span<SourceSite> bankspan(simulation::source_bank);
75✔
470
      write_source_point(source_point_filename, bankspan,
75✔
471
        simulation::work_index, settings::source_mcpl_write);
472
    }
75✔
473

474
    // Write a continously-overwritten source point if requested.
475
    if (settings::source_latest) {
85,669✔
476
      auto filename = settings::path_output + "source";
160✔
477
      span<SourceSite> bankspan(simulation::source_bank);
160✔
478
      write_source_point(filename, bankspan, simulation::work_index,
160✔
479
        settings::source_mcpl_write);
480
    }
160✔
481
  }
482

483
  // Write out surface source if requested.
484
  if (settings::surf_source_write &&
132,664✔
485
      simulation::ssw_current_file <= settings::ssw_max_files) {
9,269✔
486
    bool last_batch = (simulation::current_batch == settings::n_batches);
1,796✔
487
    if (simulation::surf_source_bank.full() || last_batch) {
1,796✔
488
      // Determine appropriate filename
489
      auto filename = fmt::format("{}surface_source.{}", settings::path_output,
490
        simulation::current_batch);
823✔
491
      if (settings::ssw_max_files == 1 ||
1,007✔
492
          (simulation::ssw_current_file == 1 && last_batch)) {
55!
493
        filename = settings::path_output + "surface_source";
952✔
494
      }
495

496
      // Get span of source bank and calculate parallel index vector
497
      auto surf_work_index = mpi::calculate_parallel_index_vector(
498
        simulation::surf_source_bank.size());
1,007✔
499
      span<SourceSite> surfbankspan(simulation::surf_source_bank.begin(),
500
        simulation::surf_source_bank.size());
1,007✔
501

502
      // Write surface source file
503
      write_source_point(
1,007✔
504
        filename, surfbankspan, surf_work_index, settings::surf_mcpl_write);
505

506
      // Reset surface source bank and increment counter
507
      simulation::surf_source_bank.clear();
1,007✔
508
      if (!last_batch && settings::ssw_max_files >= 1) {
1,007!
509
        simulation::surf_source_bank.reserve(settings::ssw_max_particles);
825✔
510
      }
511
      ++simulation::ssw_current_file;
1,007✔
512
    }
1,007✔
513
  }
514
  // Write collision track file if requested
515
  if (settings::collision_track) {
132,664✔
516
    collision_track_flush_bank();
643✔
517
  }
518
}
132,664✔
519

520
void initialize_generation()
132,900✔
521
{
522
  if (settings::run_mode == RunMode::EIGENVALUE) {
132,900✔
523
    // Clear out the fission bank
524
    simulation::fission_bank.resize(0);
85,893✔
525

526
    // Count source sites if using uniform fission source weighting
527
    if (settings::ufs_on)
85,893✔
528
      ufs_count_sites();
160✔
529

530
    // Store current value of tracklength k
531
    simulation::keff_generation = simulation::global_tallies(
85,893✔
532
      GlobalTally::K_TRACKLENGTH, TallyResult::VALUE);
533
  }
534
}
132,900✔
535

536
void finalize_generation()
132,888✔
537
{
538
  auto& gt = simulation::global_tallies;
132,888✔
539

540
  // Update global tallies with the accumulation variables
541
  if (settings::run_mode == RunMode::EIGENVALUE) {
132,888✔
542
    gt(GlobalTally::K_COLLISION, TallyResult::VALUE) += global_tally_collision;
85,893✔
543
    gt(GlobalTally::K_ABSORPTION, TallyResult::VALUE) +=
85,893✔
544
      global_tally_absorption;
545
    gt(GlobalTally::K_TRACKLENGTH, TallyResult::VALUE) +=
85,893✔
546
      global_tally_tracklength;
547
  }
548
  gt(GlobalTally::LEAKAGE, TallyResult::VALUE) += global_tally_leakage;
132,888✔
549

550
  // reset tallies
551
  if (settings::run_mode == RunMode::EIGENVALUE) {
132,888✔
552
    global_tally_collision = 0.0;
85,893✔
553
    global_tally_absorption = 0.0;
85,893✔
554
    global_tally_tracklength = 0.0;
85,893✔
555
  }
556
  global_tally_leakage = 0.0;
132,888✔
557

558
  if (settings::run_mode == RunMode::EIGENVALUE &&
132,888✔
559
      settings::solver_type == SolverType::MONTE_CARLO) {
85,893✔
560
    // If using shared memory, stable sort the fission bank (by parent IDs)
561
    // so as to allow for reproducibility regardless of which order particles
562
    // are run in.
563
    sort_fission_bank();
82,053✔
564

565
    // Distribute fission bank across processors evenly
566
    synchronize_bank();
82,053✔
567
  }
568

569
  if (settings::run_mode == RunMode::EIGENVALUE) {
132,888✔
570

571
    // Calculate shannon entropy
572
    if (settings::entropy_on &&
85,893✔
573
        settings::solver_type == SolverType::MONTE_CARLO)
11,535✔
574
      shannon_entropy();
7,695✔
575

576
    // Collect results and statistics
577
    calculate_generation_keff();
85,893✔
578
    calculate_average_keff();
85,893✔
579

580
    // Write generation output
581
    if (mpi::master && settings::verbosity >= 7) {
85,893✔
582
      print_generation();
66,183✔
583
    }
584
  }
585
}
132,888✔
586

587
void initialize_history(Particle& p, int64_t index_source)
168,287,060✔
588
{
589
  // set defaults
590
  if (settings::run_mode == RunMode::EIGENVALUE) {
168,287,060✔
591
    // set defaults for eigenvalue simulations from primary bank
592
    p.from_source(&simulation::source_bank[index_source - 1]);
142,685,700✔
593
  } else if (settings::run_mode == RunMode::FIXED_SOURCE) {
25,601,360!
594
    // initialize random number seed
595
    int64_t id = (simulation::total_gen + overall_generation() - 1) *
25,601,360✔
596
                   settings::n_particles +
25,601,360✔
597
                 simulation::work_index[mpi::rank] + index_source;
25,601,360✔
598
    uint64_t seed = init_seed(id, STREAM_SOURCE);
25,601,360✔
599
    // sample from external source distribution or custom library then set
600
    auto site = sample_external_source(&seed);
25,601,360✔
601
    p.from_source(&site);
25,601,357✔
602
  }
603
  p.current_work() = index_source;
168,287,057✔
604

605
  // set identifier for particle
606
  p.id() = simulation::work_index[mpi::rank] + index_source;
168,287,057✔
607

608
  // set progeny count to zero
609
  p.n_progeny() = 0;
168,287,057✔
610

611
  // Reset particle event counter
612
  p.n_event() = 0;
168,287,057✔
613

614
  // Reset split counter
615
  p.n_split() = 0;
168,287,057✔
616

617
  // Reset weight window ratio
618
  p.ww_factor() = 0.0;
168,287,057✔
619

620
  // set particle history start weight
621
  p.wgt_born() = p.wgt();
168,287,057✔
622

623
  // Reset pulse_height_storage
624
  std::fill(p.pht_storage().begin(), p.pht_storage().end(), 0);
168,287,057✔
625

626
  // set random number seed
627
  int64_t particle_seed =
628
    (simulation::total_gen + overall_generation() - 1) * settings::n_particles +
168,287,057✔
629
    p.id();
168,287,057✔
630
  init_particle_seeds(particle_seed, p.seeds());
168,287,057✔
631

632
  // set particle trace
633
  p.trace() = false;
168,287,057✔
634
  if (simulation::current_batch == settings::trace_batch &&
336,585,114✔
635
      simulation::current_gen == settings::trace_gen &&
168,298,057!
636
      p.id() == settings::trace_particle)
11,000✔
637
    p.trace() = true;
11✔
638

639
  // Set particle track.
640
  p.write_track() = check_track_criteria(p);
168,287,057✔
641

642
  // Set the particle's initial weight window value.
643
  p.wgt_ww_born() = -1.0;
168,287,057✔
644
  apply_weight_windows(p);
168,287,057✔
645

646
  // Display message if high verbosity or trace is on
647
  if (settings::verbosity >= 9 || p.trace()) {
168,287,057!
648
    write_message("Simulating Particle {}", p.id());
11✔
649
  }
650

651
// Add particle's starting weight to count for normalizing tallies later
652
#pragma omp atomic
91,976,016✔
653
  simulation::total_weight += p.wgt();
168,287,057✔
654

655
  // Force calculation of cross-sections by setting last energy to zero
656
  if (settings::run_CE) {
168,287,057✔
657
    p.invalidate_neutron_xs();
56,263,057✔
658
  }
659

660
  // Prepare to write out particle track.
661
  if (p.write_track())
168,287,057✔
662
    add_particle_track(p);
1,059✔
663
}
168,287,057✔
664

665
int overall_generation()
194,122,804✔
666
{
667
  using namespace simulation;
668
  return settings::gen_per_batch * (current_batch - 1) + current_gen;
194,122,804✔
669
}
670

671
void calculate_work()
7,003✔
672
{
673
  // Determine minimum amount of particles to simulate on each processor
674
  int64_t min_work = settings::n_particles / mpi::n_procs;
7,003✔
675

676
  // Determine number of processors that have one extra particle
677
  int64_t remainder = settings::n_particles % mpi::n_procs;
7,003✔
678

679
  int64_t i_bank = 0;
7,003✔
680
  simulation::work_index.resize(mpi::n_procs + 1);
7,003✔
681
  simulation::work_index[0] = 0;
7,003✔
682
  for (int i = 0; i < mpi::n_procs; ++i) {
16,245✔
683
    // Number of particles for rank i
684
    int64_t work_i = i < remainder ? min_work + 1 : min_work;
9,242!
685

686
    // Set number of particles
687
    if (mpi::rank == i)
9,242✔
688
      simulation::work_per_rank = work_i;
7,003✔
689

690
    // Set index into source bank for rank i
691
    i_bank += work_i;
9,242✔
692
    simulation::work_index[i + 1] = i_bank;
9,242✔
693
  }
694
}
7,003✔
695

696
void initialize_data()
5,764✔
697
{
698
  // Determine minimum/maximum energy for incident neutron/photon data
699
  data::energy_max = {INFTY, INFTY, INFTY, INFTY};
5,764✔
700
  data::energy_min = {0.0, 0.0, 0.0, 0.0};
5,764✔
701

702
  for (const auto& nuc : data::nuclides) {
33,955✔
703
    if (nuc->grid_.size() >= 1) {
28,191!
704
      int neutron = static_cast<int>(ParticleType::neutron);
28,191✔
705
      data::energy_min[neutron] =
56,382✔
706
        std::max(data::energy_min[neutron], nuc->grid_[0].energy.front());
28,191✔
707
      data::energy_max[neutron] =
28,191✔
708
        std::min(data::energy_max[neutron], nuc->grid_[0].energy.back());
28,191✔
709
    }
710
  }
711

712
  if (settings::photon_transport) {
5,764✔
713
    for (const auto& elem : data::elements) {
963✔
714
      if (elem->energy_.size() >= 1) {
655!
715
        int photon = static_cast<int>(ParticleType::photon);
655✔
716
        int n = elem->energy_.size();
655✔
717
        data::energy_min[photon] =
1,310✔
718
          std::max(data::energy_min[photon], std::exp(elem->energy_(1)));
655✔
719
        data::energy_max[photon] =
1,310✔
720
          std::min(data::energy_max[photon], std::exp(elem->energy_(n - 1)));
655✔
721
      }
722
    }
723

724
    if (settings::electron_treatment == ElectronTreatment::TTB) {
308✔
725
      // Determine if minimum/maximum energy for bremsstrahlung is greater/less
726
      // than the current minimum/maximum
727
      if (data::ttb_e_grid.size() >= 1) {
275!
728
        int photon = static_cast<int>(ParticleType::photon);
275✔
729
        int electron = static_cast<int>(ParticleType::electron);
275✔
730
        int positron = static_cast<int>(ParticleType::positron);
275✔
731
        int n_e = data::ttb_e_grid.size();
275✔
732

733
        const std::vector<int> charged = {electron, positron};
275✔
734
        for (auto t : charged) {
825✔
735
          data::energy_min[t] = std::exp(data::ttb_e_grid(1));
550✔
736
          data::energy_max[t] = std::exp(data::ttb_e_grid(n_e - 1));
550✔
737
        }
738

739
        data::energy_min[photon] =
550✔
740
          std::max(data::energy_min[photon], data::energy_min[electron]);
275✔
741

742
        data::energy_max[photon] =
550✔
743
          std::min(data::energy_max[photon], data::energy_max[electron]);
275✔
744
      }
275✔
745
    }
746
  }
747

748
  // Show which nuclide results in lowest energy for neutron transport
749
  for (const auto& nuc : data::nuclides) {
7,146✔
750
    // If a nuclide is present in a material that's not used in the model, its
751
    // grid has not been allocated
752
    if (nuc->grid_.size() > 0) {
6,666!
753
      double max_E = nuc->grid_[0].energy.back();
6,666✔
754
      int neutron = static_cast<int>(ParticleType::neutron);
6,666✔
755
      if (max_E == data::energy_max[neutron]) {
6,666✔
756
        write_message(7, "Maximum neutron transport energy: {} eV for {}",
5,284✔
757
          data::energy_max[neutron], nuc->name_);
5,284✔
758
        if (mpi::master && data::energy_max[neutron] < 20.0e6) {
5,284!
759
          warning("Maximum neutron energy is below 20 MeV. This may bias "
×
760
                  "the results.");
761
        }
762
        break;
5,284✔
763
      }
764
    }
765
  }
766

767
  // Set up logarithmic grid for nuclides
768
  for (auto& nuc : data::nuclides) {
33,955✔
769
    nuc->init_grid();
28,191✔
770
  }
771
  int neutron = static_cast<int>(ParticleType::neutron);
5,764✔
772
  simulation::log_spacing =
5,764✔
773
    std::log(data::energy_max[neutron] / data::energy_min[neutron]) /
5,764✔
774
    settings::n_log_bins;
775
}
5,764✔
776

777
#ifdef OPENMC_MPI
778
void broadcast_results()
3,804✔
779
{
780
  // Broadcast tally results so that each process has access to results
781
  for (auto& t : model::tallies) {
19,719✔
782
    // Create a new datatype that consists of all values for a given filter
783
    // bin and then use that to broadcast. This is done to minimize the
784
    // chance of the 'count' argument of MPI_BCAST exceeding 2**31
785
    auto& results = t->results_;
15,915✔
786

787
    auto shape = results.shape();
15,915✔
788
    int count_per_filter = shape[1] * shape[2];
15,915✔
789
    MPI_Datatype result_block;
790
    MPI_Type_contiguous(count_per_filter, MPI_DOUBLE, &result_block);
15,915✔
791
    MPI_Type_commit(&result_block);
15,915✔
792
    MPI_Bcast(results.data(), shape[0], result_block, 0, mpi::intracomm);
15,915✔
793
    MPI_Type_free(&result_block);
15,915✔
794
  }
795

796
  // Also broadcast global tally results
797
  auto& gt = simulation::global_tallies;
3,804✔
798
  MPI_Bcast(gt.data(), gt.size(), MPI_DOUBLE, 0, mpi::intracomm);
3,804✔
799

800
  // These guys are needed so that non-master processes can calculate the
801
  // combined estimate of k-effective
802
  double temp[] {
803
    simulation::k_col_abs, simulation::k_col_tra, simulation::k_abs_tra};
3,804✔
804
  MPI_Bcast(temp, 3, MPI_DOUBLE, 0, mpi::intracomm);
3,804✔
805
  simulation::k_col_abs = temp[0];
3,804✔
806
  simulation::k_col_tra = temp[1];
3,804✔
807
  simulation::k_abs_tra = temp[2];
3,804✔
808
}
3,804✔
809

810
#endif
811

812
void free_memory_simulation()
8,050✔
813
{
814
  simulation::k_generation.clear();
8,050✔
815
  simulation::entropy.clear();
8,050✔
816
}
8,050✔
817

818
void transport_history_based_single_particle(Particle& p)
155,970,390✔
819
{
820
  while (p.alive()) {
2,147,483,647✔
821
    p.event_calculate_xs();
2,147,483,647✔
822
    if (p.alive()) {
2,147,483,647!
823
      p.event_advance();
2,147,483,647✔
824
    }
825
    if (p.alive()) {
2,147,483,647✔
826
      if (p.collision_distance() > p.boundary().distance()) {
2,147,483,647✔
827
        p.event_cross_surface();
1,351,775,575✔
828
      } else if (p.alive()) {
2,147,483,647!
829
        p.event_collide();
2,147,483,647✔
830
      }
831
    }
832
    p.event_revive_from_secondary();
2,147,483,647✔
833
  }
834
  p.event_death();
155,970,381✔
835
}
155,970,381✔
836

837
void transport_history_based()
111,796✔
838
{
839
#pragma omp parallel for schedule(runtime)
840
  for (int64_t i_work = 1; i_work <= simulation::work_per_rank; ++i_work) {
76,462,174✔
841
    Particle p;
76,411,088✔
842
    initialize_history(p, i_work);
76,411,088✔
843
    transport_history_based_single_particle(p);
76,411,085✔
844
  }
76,411,080✔
845
}
111,788✔
846

847
void transport_event_based()
3,172✔
848
{
849
  int64_t remaining_work = simulation::work_per_rank;
3,172✔
850
  int64_t source_offset = 0;
3,172✔
851

852
  // To cap the total amount of memory used to store particle object data, the
853
  // number of particles in flight at any point in time can bet set. In the case
854
  // that the maximum in flight particle count is lower than the total number
855
  // of particles that need to be run this iteration, the event-based transport
856
  // loop is executed multiple times until all particles have been completed.
857
  while (remaining_work > 0) {
6,344✔
858
    // Figure out # of particles to run for this subiteration
859
    int64_t n_particles =
860
      std::min(remaining_work, settings::max_particles_in_flight);
3,172✔
861

862
    // Initialize all particle histories for this subiteration
863
    process_init_events(n_particles, source_offset);
3,172!
864

865
    // Event-based transport loop
866
    while (true) {
867
      // Determine which event kernel has the longest queue
868
      int64_t max = std::max({simulation::calculate_fuel_xs_queue.size(),
4,744,958!
869
        simulation::calculate_nonfuel_xs_queue.size(),
2,372,479✔
870
        simulation::advance_particle_queue.size(),
2,372,479✔
871
        simulation::surface_crossing_queue.size(),
2,372,479✔
872
        simulation::collision_queue.size()});
2,372,479✔
873

874
      // Execute event with the longest queue
875
      if (max == 0) {
2,372,479✔
876
        break;
3,172✔
877
      } else if (max == simulation::calculate_fuel_xs_queue.size()) {
2,369,307✔
878
        process_calculate_xs_events(simulation::calculate_fuel_xs_queue);
425,837!
879
      } else if (max == simulation::calculate_nonfuel_xs_queue.size()) {
1,943,470✔
880
        process_calculate_xs_events(simulation::calculate_nonfuel_xs_queue);
361,263!
881
      } else if (max == simulation::advance_particle_queue.size()) {
1,582,207✔
882
        process_advance_particle_events();
781,837!
883
      } else if (max == simulation::surface_crossing_queue.size()) {
800,370✔
884
        process_surface_crossing_events();
259,810!
885
      } else if (max == simulation::collision_queue.size()) {
540,560!
886
        process_collision_events();
540,560!
887
      }
888
    }
2,369,307✔
889

890
    // Execute death event for all particles
891
    process_death_events(n_particles);
3,172!
892

893
    // Adjust remaining work and source offset variables
894
    remaining_work -= n_particles;
3,172✔
895
    source_offset += n_particles;
3,172✔
896
  }
897
}
3,172✔
898

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