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

openmc-dev / openmc / 26185185014

20 May 2026 07:31PM UTC coverage: 81.49% (+0.02%) from 81.469%
26185185014

Pull #3728

github

web-flow
Merge 188f7a516 into 66497e76b
Pull Request #3728: Add delay to activation of IFP tallies.

17920 of 25807 branches covered (69.44%)

Branch coverage included in aggregate %.

79 of 84 new or added lines in 9 files covered. (94.05%)

74 existing lines in 2 files now uncovered.

59027 of 68618 relevant lines covered (86.02%)

48637902.73 hits per line

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

95.03
/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 "openmc/tensor.h"
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()
6,496✔
54
{
55
  openmc::simulation::time_total.start();
6,496✔
56
  openmc_simulation_init();
6,496✔
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;
6,496✔
61
  if (openmc::simulation::current_batch >= openmc::settings::n_max_batches) {
6,496✔
62
    status = openmc::STATUS_EXIT_MAX_BATCH;
11✔
63
  }
64

65
  int err = 0;
66
  while (status == 0 && err == 0) {
146,886✔
67
    err = openmc_next_batch(&status);
140,403✔
68
  }
69

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

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

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

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

88
  // Determine how much work each process should do
89
  calculate_work(settings::n_particles);
7,646✔
90

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

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

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

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

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

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

141
  // Display header
142
  if (mpi::master) {
7,646✔
143
    if (settings::run_mode == RunMode::FIXED_SOURCE) {
6,654✔
144
      if (settings::solver_type == SolverType::MONTE_CARLO) {
2,879✔
145
        header("FIXED SOURCE TRANSPORT SIMULATION", 3);
2,503✔
146
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
376!
147
        header("FIXED SOURCE TRANSPORT SIMULATION (RANDOM RAY SOLVER)", 3);
376✔
148
      }
149
    } else if (settings::run_mode == RunMode::EIGENVALUE) {
3,775!
150
      if (settings::solver_type == SolverType::MONTE_CARLO) {
3,775✔
151
        header("K EIGENVALUE SIMULATION", 3);
3,500✔
152
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
275!
153
        header("K EIGENVALUE SIMULATION (RANDOM RAY SOLVER)", 3);
275✔
154
      }
155
      if (settings::verbosity >= 7)
3,775✔
156
        print_columns();
3,395✔
157
    }
158
  }
159

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

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

170
int openmc_simulation_finalize()
7,633✔
171
{
172
  using namespace openmc;
7,633✔
173

174
  // Skip if simulation was never run
175
  if (!simulation::initialized)
7,633!
176
    return 0;
177

178
  // Stop active batch timer and start finalization timer
179
  simulation::time_active.stop();
7,633✔
180
  simulation::time_finalize.start();
7,633✔
181

182
  // Clear material nuclide mapping
183
  for (auto& mat : model::materials) {
27,137✔
184
    mat->mat_nuclide_index_.clear();
39,008!
185
  }
186

187
  // Close track file if open
188
  if (!settings::track_identifiers.empty() || settings::write_all_tracks) {
7,633✔
189
    close_track_file();
90✔
190
  }
191

192
  // Increment total number of generations
193
  simulation::total_gen += simulation::current_batch * settings::gen_per_batch;
7,633✔
194

195
#ifdef OPENMC_MPI
196
  broadcast_results();
3,426✔
197
#endif
198

199
  // Write tally results to tallies.out
200
  if (settings::output_tallies && mpi::master)
7,633!
201
    write_tallies();
6,295✔
202

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

209
  // Deactivate all tallies
210
  for (auto& t : model::tallies) {
34,684✔
211
    t->active_ = false;
27,051✔
212
  }
213

214
  // Stop timers and show timing statistics
215
  simulation::time_finalize.stop();
7,633✔
216
  simulation::time_total.stop();
7,633✔
217

218
#ifdef OPENMC_MPI
219
  // Reduce track count across ranks for correct reporting. In shared secondary
220
  // bank mode, all ranks already have the global count; in non-shared mode,
221
  // each rank only has its own count.
222
  if (settings::weight_windows_on && !settings::use_shared_secondary_bank) {
3,426✔
223
    int64_t total_tracks;
80✔
224
    MPI_Reduce(&simulation::simulation_tracks_completed, &total_tracks, 1,
80✔
225
      MPI_INT64_T, MPI_SUM, 0, mpi::intracomm);
226
    if (mpi::master)
80✔
227
      simulation::simulation_tracks_completed = total_tracks;
64✔
228
  }
229
#endif
230

231
  if (mpi::master) {
7,633✔
232
    if (settings::solver_type != SolverType::RANDOM_RAY) {
6,641✔
233
      if (settings::verbosity >= 6)
5,990✔
234
        print_runtime();
5,610✔
235
      if (settings::verbosity >= 4)
5,990✔
236
        print_results();
5,610✔
237
    }
238
  }
239
  if (settings::check_overlaps)
7,633!
240
    print_overlap_check();
×
241

242
  // Reset flags
243
  simulation::initialized = false;
7,633✔
244
  return 0;
7,633✔
245
}
246

247
int openmc_next_batch(int* status)
144,528✔
248
{
249
  using namespace openmc;
144,528✔
250
  using openmc::simulation::current_gen;
144,528✔
251

252
  // Make sure simulation has been initialized
253
  if (!simulation::initialized) {
144,528✔
254
    set_errmsg("Simulation has not been initialized yet.");
11✔
255
    return OPENMC_E_ALLOCATE;
11✔
256
  }
257

258
  initialize_batch();
144,517✔
259

260
  // =======================================================================
261
  // LOOP OVER GENERATIONS
262
  for (current_gen = 1; current_gen <= settings::gen_per_batch; ++current_gen) {
289,231✔
263

264
    initialize_generation();
144,727✔
265

266
    // Start timer for transport
267
    simulation::time_transport.start();
144,727✔
268

269
    // Transport loop
270
    if (settings::event_based) {
144,727✔
271
      if (settings::use_shared_secondary_bank) {
3,211✔
272
        transport_event_based_shared_secondary();
11✔
273
      } else {
274
        transport_event_based();
3,200✔
275
      }
276
    } else {
277
      if (settings::use_shared_secondary_bank) {
141,516✔
278
        transport_history_based_shared_secondary();
667✔
279
      } else {
280
        transport_history_based();
140,849✔
281
      }
282
    }
283

284
    // Accumulate time for transport
285
    simulation::time_transport.stop();
144,714✔
286

287
    finalize_generation();
144,714✔
288
  }
289

290
  finalize_batch();
144,504✔
291

292
  // Check simulation ending criteria
293
  if (status) {
144,504!
294
    if (simulation::current_batch >= settings::n_max_batches) {
144,504✔
295
      *status = STATUS_EXIT_MAX_BATCH;
6,676✔
296
    } else if (simulation::satisfy_triggers) {
137,828✔
297
      *status = STATUS_EXIT_ON_TRIGGER;
93✔
298
    } else {
299
      *status = STATUS_EXIT_NORMAL;
137,735✔
300
    }
301
  }
302
  return 0;
303
}
304

305
bool openmc_is_statepoint_batch()
3,135✔
306
{
307
  using namespace openmc;
3,135✔
308
  using openmc::simulation::current_gen;
3,135✔
309

310
  if (!simulation::initialized)
3,135!
311
    return false;
312
  else
313
    return contains(settings::statepoint_batch, simulation::current_batch);
6,270✔
314
}
315

316
namespace openmc {
317

318
//==============================================================================
319
// Global variables
320
//==============================================================================
321

322
namespace simulation {
323

324
int ct_current_file;
325
int current_batch;
326
int current_gen;
327
bool ifp_delayed_on {false};
328
bool ifp_lifetime_on {false};
329
bool initialized {false};
330
double keff {1.0};
331
double keff_std;
332
double k_col_abs {0.0};
333
double k_col_tra {0.0};
334
double k_abs_tra {0.0};
335
double log_spacing;
336
int n_lost_particles {0};
337
bool need_depletion_rx {false};
338
int restart_batch;
339
bool satisfy_triggers {false};
340
int ssw_current_file;
341
int total_gen {0};
342
double total_weight;
343
int64_t work_per_rank;
344

345
const RegularMesh* entropy_mesh {nullptr};
346
const RegularMesh* ufs_mesh {nullptr};
347

348
vector<double> k_generation;
349
vector<int64_t> work_index;
350

351
int64_t simulation_tracks_completed {0};
352

353
} // namespace simulation
354

355
//==============================================================================
356
// Non-member functions
357
//==============================================================================
358

359
void allocate_banks()
7,646✔
360
{
361
  if (settings::run_mode == RunMode::EIGENVALUE &&
7,646✔
362
      settings::solver_type == SolverType::MONTE_CARLO) {
4,461✔
363
    // Allocate source bank
364
    simulation::source_bank.resize(simulation::work_per_rank);
4,090✔
365

366
    // Allocate fission bank
367
    init_fission_bank(3 * simulation::work_per_rank);
4,090✔
368

369
    // Allocate IFP bank
370
    resize_simulation_ifp_banks();
4,090✔
371
  }
372

373
  if (settings::surf_source_write) {
7,646✔
374
    // Allocate surface source bank
375
    simulation::surf_source_bank.reserve(settings::ssw_max_particles);
1,154✔
376
  }
377

378
  if (settings::collision_track) {
7,646✔
379
    // Allocate collision track bank
380
    collision_track_reserve_bank();
148✔
381
  }
382
}
7,646✔
383

384
void initialize_batch()
165,479✔
385
{
386
  // Increment current batch
387
  ++simulation::current_batch;
165,479✔
388
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
165,479✔
389
    if (settings::solver_type == SolverType::RANDOM_RAY &&
64,786✔
390
        simulation::current_batch < settings::n_inactive + 1) {
14,112✔
391
      write_message(
16,812✔
392
        6, "Simulating batch {:<4} (inactive)", simulation::current_batch);
393
    } else {
394
      write_message(6, "Simulating batch {}", simulation::current_batch);
112,760✔
395
    }
396
  }
397

398
  // Reset total starting particle weight used for normalizing tallies
399
  simulation::total_weight = 0.0;
165,479✔
400

401
  // Determine if this batch is the first inactive or active batch.
402
  bool first_inactive = false;
165,479✔
403
  bool first_active = false;
165,479✔
404
  if (!settings::restart_run) {
165,479✔
405
    first_inactive = settings::n_inactive > 0 && simulation::current_batch == 1;
165,316✔
406
    first_active = simulation::current_batch == settings::n_inactive + 1;
165,316✔
407
  } else if (simulation::current_batch == simulation::restart_batch + 1) {
163✔
408
    first_inactive = simulation::restart_batch < settings::n_inactive;
52✔
409
    first_active = !first_inactive;
52✔
410
  }
411

412
  // Manage active/inactive timers and activate tallies if necessary.
413
  if (first_inactive) {
165,368✔
414
    simulation::time_inactive.start();
3,845✔
415
  } else if (first_active) {
161,634✔
416
    simulation::time_inactive.stop();
7,599✔
417
    simulation::time_active.start();
7,599✔
418
    for (auto& t : model::tallies) {
34,628✔
419
      if (t->offset_ == 0)
27,029✔
420
        t->active_ = true;
26,837✔
421
    }
422
  }
423

424
  // Activate tallies which have activation offset.
425
  for (auto& t : model::tallies) {
675,821✔
426
    if (t->offset_ > 0) {
510,342✔
427
      if (!settings::restart_run) {
3,840!
428
        if (simulation::current_batch == settings::n_inactive + 1 + t->offset_)
3,840✔
429
          t->active_ = true;
192✔
NEW
430
      } else if (simulation::current_batch == simulation::restart_batch + 1) {
×
NEW
431
        if (simulation::restart_batch >= settings::n_inactive + t->offset_)
×
NEW
432
          t->active_ = true;
×
433
      }
434
    }
435
  }
436

437
  // Add user tallies to active tallies list
438
  setup_active_tallies();
165,479✔
439
}
165,479✔
440

441
void finalize_batch()
165,466✔
442
{
443
  // Reduce tallies onto master process and accumulate
444
  simulation::time_tallies.start();
165,466✔
445
  accumulate_tallies();
165,466✔
446
  simulation::time_tallies.stop();
165,466✔
447

448
  // update weight windows if needed
449
  for (const auto& wwg : variance_reduction::weight_windows_generators) {
168,098✔
450
    wwg->update();
2,632✔
451
  }
452

453
  // Reset global tally results
454
  if (simulation::current_batch <= settings::n_inactive) {
165,466✔
455
    simulation::global_tallies.fill(0.0);
32,148✔
456
    simulation::n_realizations = 0;
32,148✔
457
  }
458

459
  // Check_triggers
460
  if (mpi::master)
165,466✔
461
    check_triggers();
146,667✔
462
#ifdef OPENMC_MPI
463
  MPI_Bcast(&simulation::satisfy_triggers, 1, MPI_C_BOOL, 0, mpi::intracomm);
72,448✔
464
#endif
465
  if (simulation::satisfy_triggers ||
165,466✔
466
      (settings::trigger_on &&
2,567✔
467
        simulation::current_batch == settings::n_max_batches)) {
2,567✔
468
    settings::statepoint_batch.insert(simulation::current_batch);
141✔
469
  }
470

471
  // Write out state point if it's been specified for this batch and is not
472
  // a CMFD run instance
473
  if (contains(settings::statepoint_batch, simulation::current_batch) &&
330,932✔
474
      !settings::cmfd_run) {
7,901✔
475
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
15,172✔
476
        settings::source_write && !settings::source_separate) {
14,289✔
477
      bool b = (settings::run_mode == RunMode::EIGENVALUE);
6,493✔
478
      openmc_statepoint_write(nullptr, &b);
6,493✔
479
    } else {
480
      bool b = false;
1,232✔
481
      openmc_statepoint_write(nullptr, &b);
1,232✔
482
    }
483
  }
484

485
  if (settings::run_mode == RunMode::EIGENVALUE) {
165,466✔
486
    // Write out a separate source point if it's been specified for this batch
487
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
105,180✔
488
        settings::source_write && settings::source_separate) {
104,809✔
489

490
      // Determine width for zero padding
491
      int w = std::to_string(settings::n_max_batches).size();
71✔
492
      std::string source_point_filename = fmt::format("{0}source.{1:0{2}}",
71✔
493
        settings::path_output, simulation::current_batch, w);
71✔
494
      span<SourceSite> bankspan(simulation::source_bank);
71✔
495
      write_source_point(source_point_filename, bankspan,
142✔
496
        simulation::work_index, settings::source_mcpl_write);
497
    }
71✔
498

499
    // Write a continously-overwritten source point if requested.
500
    if (settings::source_latest) {
100,693✔
501
      auto filename = settings::path_output + "source";
150✔
502
      span<SourceSite> bankspan(simulation::source_bank);
150✔
503
      write_source_point(filename, bankspan, simulation::work_index,
300✔
504
        settings::source_mcpl_write);
505
    }
150✔
506
  }
507

508
  // Write out surface source if requested.
509
  if (settings::surf_source_write &&
165,466✔
510
      simulation::ssw_current_file <= settings::ssw_max_files) {
17,669✔
511
    bool last_batch = (simulation::current_batch == settings::n_batches);
1,976✔
512
    if (simulation::surf_source_bank.full() || last_batch) {
1,976✔
513
      // Determine appropriate filename
514
      auto filename = fmt::format("{}surface_source.{}", settings::path_output,
1,187✔
515
        simulation::current_batch);
1,187✔
516
      if (settings::ssw_max_files == 1 ||
1,187✔
517
          (simulation::ssw_current_file == 1 && last_batch)) {
55!
518
        filename = settings::path_output + "surface_source";
1,132✔
519
      }
520

521
      // Get span of source bank and calculate parallel index vector
522
      auto surf_work_index = mpi::calculate_parallel_index_vector(
1,187✔
523
        simulation::surf_source_bank.size());
1,187✔
524
      span<SourceSite> surfbankspan(simulation::surf_source_bank.begin(),
1,187✔
525
        simulation::surf_source_bank.size());
1,187✔
526

527
      // Write surface source file
528
      write_source_point(
1,187✔
529
        filename, surfbankspan, surf_work_index, settings::surf_mcpl_write);
530

531
      // Reset surface source bank and increment counter
532
      simulation::surf_source_bank.clear();
1,187✔
533
      if (!last_batch && settings::ssw_max_files >= 1) {
1,187!
534
        simulation::surf_source_bank.reserve(settings::ssw_max_particles);
1,005✔
535
      }
536
      ++simulation::ssw_current_file;
1,187✔
537
    }
1,187✔
538
  }
539
  // Write collision track file if requested
540
  if (settings::collision_track) {
165,466✔
541
    collision_track_flush_bank();
608✔
542
  }
543
}
165,466✔
544

545
void initialize_generation()
165,689✔
546
{
547
  if (settings::run_mode == RunMode::EIGENVALUE) {
165,689✔
548
    // Clear out the fission bank
549
    simulation::fission_bank.resize(0);
100,903✔
550

551
    // Count source sites if using uniform fission source weighting
552
    if (settings::ufs_on)
100,903✔
553
      ufs_count_sites();
150✔
554

555
    // Store current value of tracklength k
556
    simulation::keff_generation = simulation::global_tallies(
100,903✔
557
      GlobalTally::K_TRACKLENGTH, TallyResult::VALUE);
558
  }
559
}
165,689✔
560

561
void finalize_generation()
165,676✔
562
{
563
  auto& gt = simulation::global_tallies;
165,676✔
564

565
  // Update global tallies with the accumulation variables
566
  if (settings::run_mode == RunMode::EIGENVALUE) {
165,676✔
567
    gt(GlobalTally::K_COLLISION, TallyResult::VALUE) += global_tally_collision;
100,903✔
568
    gt(GlobalTally::K_ABSORPTION, TallyResult::VALUE) +=
100,903✔
569
      global_tally_absorption;
570
    gt(GlobalTally::K_TRACKLENGTH, TallyResult::VALUE) +=
100,903✔
571
      global_tally_tracklength;
572
  }
573
  gt(GlobalTally::LEAKAGE, TallyResult::VALUE) += global_tally_leakage;
165,676✔
574

575
  // reset tallies
576
  if (settings::run_mode == RunMode::EIGENVALUE) {
165,676✔
577
    global_tally_collision = 0.0;
100,903✔
578
    global_tally_absorption = 0.0;
100,903✔
579
    global_tally_tracklength = 0.0;
100,903✔
580
  }
581
  global_tally_leakage = 0.0;
165,676✔
582

583
  if (settings::run_mode == RunMode::EIGENVALUE &&
165,676✔
584
      settings::solver_type == SolverType::MONTE_CARLO) {
100,903✔
585
    // If using shared memory, stable sort the fission bank (by parent IDs)
586
    // so as to allow for reproducibility regardless of which order particles
587
    // are run in.
588
    sort_bank(simulation::fission_bank, true);
94,053✔
589

590
    // Distribute fission bank across processors evenly
591
    synchronize_bank();
94,053✔
592
  }
593

594
  if (settings::run_mode == RunMode::EIGENVALUE) {
165,676✔
595

596
    // Calculate shannon entropy
597
    if (settings::entropy_on &&
100,903✔
598
        settings::solver_type == SolverType::MONTE_CARLO)
14,535✔
599
      shannon_entropy();
7,685✔
600

601
    // Collect results and statistics
602
    calculate_generation_keff();
100,903✔
603
    calculate_average_keff();
100,903✔
604

605
    // Write generation output
606
    if (mpi::master && settings::verbosity >= 7) {
100,903✔
607
      print_generation();
75,918✔
608
    }
609
  }
610
}
165,676✔
611

612
void sample_source_particle(Particle& p, int64_t index_source)
179,820,781✔
613
{
614
  // Sample a particle from the source bank
615
  if (settings::run_mode == RunMode::EIGENVALUE) {
179,820,781✔
616
    p.from_source(&simulation::source_bank[index_source - 1]);
151,874,700✔
617
  } else if (settings::run_mode == RunMode::FIXED_SOURCE) {
27,946,081!
618
    // initialize random number seed
619
    int64_t id = compute_transport_seed(compute_particle_id(index_source));
27,946,081✔
620
    uint64_t seed = init_seed(id, STREAM_SOURCE);
27,946,081✔
621
    // sample from external source distribution or custom library then set
622
    auto site = sample_external_source(&seed);
27,946,081✔
623
    p.from_source(&site);
27,946,077✔
624
  }
625
}
179,820,777✔
626

627
void initialize_particle_track(
201,157,812✔
628
  Particle& p, int64_t index_source, bool is_secondary)
629
{
630
  // Note: index_source is 1-based (first particle = 1), but current_work() is
631
  // stored as 0-based for direct use as an array index into
632
  // progeny_per_particle, source_bank, ifp banks, etc.
633
  if (!is_secondary) {
201,157,812✔
634
    sample_source_particle(p, index_source);
179,820,781✔
635
  }
636

637
  p.current_work() = index_source - 1;
201,157,808✔
638

639
  // set identifier for particle
640
  p.id() = compute_particle_id(index_source);
201,157,808✔
641

642
  // set progeny count to zero
643
  p.n_progeny() = 0;
201,157,808✔
644

645
  // Reset particle event counter
646
  p.n_event() = 0;
201,157,808✔
647

648
  // Initialize track counter (1 for this primary/secondary track)
649
  p.n_tracks() = 1;
201,157,808✔
650

651
  // Reset split counter
652
  p.n_split() = 0;
201,157,808✔
653

654
  // Reset weight window ratio
655
  p.ww_factor() = 0.0;
201,157,808✔
656

657
  // set particle history start weight
658
  p.wgt_born() = p.wgt();
201,157,808✔
659

660
  // Reset pulse_height_storage
661
  std::fill(p.pht_storage().begin(), p.pht_storage().end(), 0);
201,157,808✔
662

663
  // set random number seed
664
  int64_t particle_seed = compute_transport_seed(p.id());
201,157,808✔
665
  init_particle_seeds(particle_seed, p.seeds());
201,157,808✔
666

667
  // set particle trace
668
  p.trace() = false;
201,157,808✔
669
  if (simulation::current_batch == settings::trace_batch &&
201,168,808✔
670
      simulation::current_gen == settings::trace_gen &&
201,157,808!
671
      p.id() == settings::trace_particle)
11,000✔
672
    p.trace() = true;
11✔
673

674
  // Set particle track.
675
  p.write_track() = check_track_criteria(p);
201,157,808✔
676

677
  // Set the particle's initial weight window value.
678
  if (!is_secondary) {
201,157,808✔
679
    p.wgt_ww_born() = -1.0;
179,820,777✔
680
    apply_weight_windows(p);
179,820,777✔
681
  }
682

683
  // Display message if high verbosity or trace is on
684
  if (settings::verbosity >= 9 || p.trace()) {
201,157,808!
685
    write_message("Simulating Particle {}", p.id());
22✔
686
  }
687

688
  // Add particle's starting weight to count for normalizing tallies later
689
  if (!is_secondary) {
201,157,808✔
690
#pragma omp atomic
98,497,020✔
691
    simulation::total_weight += p.wgt();
179,820,777✔
692
  }
693

694
  // Force calculation of cross-sections by setting last energy to zero
695
  if (settings::run_CE) {
201,157,808✔
696
    p.invalidate_neutron_xs();
86,620,264✔
697
  }
698

699
  // Prepare to write out particle track.
700
  if (p.write_track())
201,157,808✔
701
    add_particle_track(p);
999✔
702
}
201,157,808✔
703

704
int overall_generation()
206,469,015✔
705
{
706
  using namespace simulation;
206,469,015✔
707
  return settings::gen_per_batch * (current_batch - 1) + current_gen;
206,469,015✔
708
}
709

710
int64_t compute_particle_id(int64_t index_source)
229,104,164✔
711
{
712
  if (settings::use_shared_secondary_bank) {
229,104,164✔
713
    return simulation::work_index[mpi::rank] + index_source +
22,906,056✔
714
           simulation::simulation_tracks_completed;
22,906,056✔
715
  } else {
716
    return simulation::work_index[mpi::rank] + index_source;
206,198,108✔
717
  }
718
}
719

720
int64_t compute_transport_seed(int64_t particle_id)
229,104,208✔
721
{
722
  if (settings::use_shared_secondary_bank) {
229,104,208✔
723
    return particle_id;
724
  } else {
725
    return (simulation::total_gen + overall_generation() - 1) *
206,198,141✔
726
             settings::n_particles +
727
           particle_id;
206,198,141✔
728
  }
729
}
730

731
void calculate_work(int64_t n_particles)
16,975✔
732
{
733
  // Determine minimum amount of particles to simulate on each processor
734
  int64_t min_work = n_particles / mpi::n_procs;
16,975✔
735

736
  // Determine number of processors that have one extra particle
737
  int64_t remainder = n_particles % mpi::n_procs;
16,975✔
738

739
  int64_t i_bank = 0;
16,975✔
740
  simulation::work_index.resize(mpi::n_procs + 1);
16,975✔
741
  simulation::work_index[0] = 0;
16,975✔
742
  for (int i = 0; i < mpi::n_procs; ++i) {
39,461✔
743
    // Number of particles for rank i
744
    int64_t work_i = i < remainder ? min_work + 1 : min_work;
22,486✔
745

746
    // Set number of particles
747
    if (mpi::rank == i)
22,486✔
748
      simulation::work_per_rank = work_i;
16,975✔
749

750
    // Set index into source bank for rank i
751
    i_bank += work_i;
22,486✔
752
    simulation::work_index[i + 1] = i_bank;
22,486✔
753
  }
754
}
16,975✔
755

756
void initialize_data()
6,294✔
757
{
758
  // Determine minimum/maximum energy for incident neutron/photon data
759
  data::energy_max = {INFTY, INFTY, INFTY, INFTY};
6,294✔
760
  data::energy_min = {0.0, 0.0, 0.0, 0.0};
6,294✔
761

762
  for (const auto& nuc : data::nuclides) {
38,854✔
763
    if (nuc->grid_.size() >= 1) {
32,560!
764
      int neutron = ParticleType::neutron().transport_index();
32,560✔
765
      data::energy_min[neutron] =
32,560✔
766
        std::max(data::energy_min[neutron], nuc->grid_[0].energy.front());
38,333✔
767
      data::energy_max[neutron] =
32,560✔
768
        std::min(data::energy_max[neutron], nuc->grid_[0].energy.back());
39,892✔
769
    }
770
  }
771

772
  if (settings::photon_transport) {
6,294✔
773
    for (const auto& elem : data::elements) {
1,491✔
774
      if (elem->energy_.size() >= 1) {
1,064!
775
        int photon = ParticleType::photon().transport_index();
1,064✔
776
        int n = elem->energy_.size();
1,064✔
777
        data::energy_min[photon] =
2,128✔
778
          std::max(data::energy_min[photon], std::exp(elem->energy_(1)));
1,761✔
779
        data::energy_max[photon] =
1,064✔
780
          std::min(data::energy_max[photon], std::exp(elem->energy_(n - 1)));
1,491✔
781
      }
782
    }
783

784
    if (settings::electron_treatment == ElectronTreatment::TTB) {
427✔
785
      // Determine if minimum/maximum energy for bremsstrahlung is greater/less
786
      // than the current minimum/maximum
787
      if (data::ttb_e_grid.size() >= 1) {
368!
788
        int photon = ParticleType::photon().transport_index();
368✔
789
        int electron = ParticleType::electron().transport_index();
368✔
790
        int positron = ParticleType::positron().transport_index();
368✔
791
        int n_e = data::ttb_e_grid.size();
368✔
792

793
        const std::vector<int> charged = {electron, positron};
368✔
794
        for (auto t : charged) {
1,104✔
795
          data::energy_min[t] = std::exp(data::ttb_e_grid(1));
736✔
796
          data::energy_max[t] = std::exp(data::ttb_e_grid(n_e - 1));
736✔
797
        }
798

799
        data::energy_min[photon] =
736✔
800
          std::max(data::energy_min[photon], data::energy_min[electron]);
736!
801

802
        data::energy_max[photon] =
736✔
803
          std::min(data::energy_max[photon], data::energy_max[electron]);
736!
804
      }
368✔
805
    }
806
  }
807

808
  // Show which nuclide results in lowest energy for neutron transport
809
  for (const auto& nuc : data::nuclides) {
7,913✔
810
    // If a nuclide is present in a material that's not used in the model, its
811
    // grid has not been allocated
812
    if (nuc->grid_.size() > 0) {
7,392!
813
      double max_E = nuc->grid_[0].energy.back();
7,392✔
814
      int neutron = ParticleType::neutron().transport_index();
7,392✔
815
      if (max_E == data::energy_max[neutron]) {
7,392✔
816
        write_message(7, "Maximum neutron transport energy: {} eV for {}",
5,773✔
817
          data::energy_max[neutron], nuc->name_);
5,773✔
818
        if (mpi::master && data::energy_max[neutron] < 20.0e6) {
5,773!
819
          warning("Maximum neutron energy is below 20 MeV. This may bias "
×
820
                  "the results.");
821
        }
822
        break;
823
      }
824
    }
825
  }
826

827
  // Set up logarithmic grid for nuclides
828
  for (auto& nuc : data::nuclides) {
38,854✔
829
    nuc->init_grid();
32,560✔
830
  }
831
  int neutron = ParticleType::neutron().transport_index();
6,294✔
832
  simulation::log_spacing =
12,588✔
833
    std::log(data::energy_max[neutron] / data::energy_min[neutron]) /
6,294✔
834
    settings::n_log_bins;
835
}
6,294✔
836

837
#ifdef OPENMC_MPI
838
void broadcast_results()
3,426✔
839
{
840
  // Broadcast tally results so that each process has access to results
841
  for (auto& t : model::tallies) {
16,851✔
842
    // Create a new datatype that consists of all values for a given filter
843
    // bin and then use that to broadcast. This is done to minimize the
844
    // chance of the 'count' argument of MPI_BCAST exceeding 2**31
845
    auto& results = t->results_;
13,425✔
846

847
    auto shape = results.shape();
13,425✔
848
    int count_per_filter = shape[1] * shape[2];
13,425✔
849
    MPI_Datatype result_block;
13,425✔
850
    MPI_Type_contiguous(count_per_filter, MPI_DOUBLE, &result_block);
13,425✔
851
    MPI_Type_commit(&result_block);
13,425✔
852
    MPI_Bcast(results.data(), shape[0], result_block, 0, mpi::intracomm);
13,425✔
853
    MPI_Type_free(&result_block);
13,425✔
854
  }
13,425✔
855

856
  // Also broadcast global tally results
857
  auto& gt = simulation::global_tallies;
3,426✔
858
  MPI_Bcast(gt.data(), gt.size(), MPI_DOUBLE, 0, mpi::intracomm);
3,426✔
859

860
  // These guys are needed so that non-master processes can calculate the
861
  // combined estimate of k-effective
862
  double temp[] {
3,426✔
863
    simulation::k_col_abs, simulation::k_col_tra, simulation::k_abs_tra};
3,426✔
864
  MPI_Bcast(temp, 3, MPI_DOUBLE, 0, mpi::intracomm);
3,426✔
865
  simulation::k_col_abs = temp[0];
3,426✔
866
  simulation::k_col_tra = temp[1];
3,426✔
867
  simulation::k_abs_tra = temp[2];
3,426✔
868
}
3,426✔
869

870
#endif
871

872
void free_memory_simulation()
8,658✔
873
{
874
  simulation::k_generation.clear();
8,658✔
875
  simulation::entropy.clear();
8,658✔
876
}
8,658✔
877

878
void transport_history_based_single_particle(Particle& p)
188,642,342✔
879
{
880
  while (p.alive()) {
2,147,483,647✔
881
    p.event_calculate_xs();
2,147,483,647✔
882
    if (p.alive()) {
2,147,483,647!
883
      p.event_advance();
2,147,483,647✔
884
    }
885
    if (p.alive()) {
2,147,483,647✔
886
      if (p.collision_distance() > p.boundary().distance()) {
2,147,483,647✔
887
        p.event_cross_surface();
2,147,483,647✔
888
      } else if (p.alive()) {
2,147,483,647✔
889
        p.event_collide();
2,147,483,647✔
890
      }
891
    }
892
    p.event_check_limit_and_revive();
2,147,483,647✔
893
  }
894
  p.event_death();
188,642,333✔
895
}
188,642,333✔
896

897
void transport_history_based()
140,849✔
898
{
899
#pragma omp parallel for schedule(runtime)
78,444✔
900
  for (int64_t i_work = 1; i_work <= simulation::work_per_rank; ++i_work) {
81,378,550✔
901
    Particle p;
81,316,154✔
902
    initialize_particle_track(p, i_work, false);
81,316,154✔
903
    transport_history_based_single_particle(p);
81,316,150✔
904
  }
81,316,145✔
905
}
140,840✔
906

907
// The shared secondary bank transport algorithm works in two phases. In the
908
// first phase, all primary particles are sampled then transported, and their
909
// secondary particles are deposited into a shared secondary bank. The second
910
// phase occurs in a loop, where all secondary tracks in the shared secondary
911
// bank are transported. Any secondary particles generated during this phase are
912
// deposited back into the shared secondary bank. The shared secondary bank is
913
// sorted for consistent ordering and load balanced across MPI ranks. This loop
914
// continues until there are no more secondary tracks left to transport.
915
void transport_history_based_shared_secondary()
667✔
916
{
917
  // Clear shared secondary banks from any prior use
918
  simulation::shared_secondary_bank_read.clear();
667✔
919
  simulation::shared_secondary_bank_write.clear();
667✔
920

921
  if (mpi::master) {
667✔
922
    write_message(fmt::format(" Primary source          particles: {}",
1,150✔
923
                    settings::n_particles),
924
      6);
925
  }
926

927
  simulation::progeny_per_particle.resize(simulation::work_per_rank);
667✔
928
  std::fill(simulation::progeny_per_particle.begin(),
1,334✔
929
    simulation::progeny_per_particle.end(), 0);
667✔
930

931
  // Phase 1: Transport primary particles and deposit first generation of
932
  // secondaries in the shared secondary bank
933
#pragma omp parallel
384✔
934
  {
283✔
935
    vector<SourceSite> thread_bank;
283✔
936

937
#pragma omp for schedule(runtime)
938
    for (int64_t i = 1; i <= simulation::work_per_rank; i++) {
356,058✔
939
      Particle p;
355,775✔
940
      initialize_particle_track(p, i, false);
355,775✔
941
      transport_history_based_single_particle(p);
355,775✔
942
      for (auto& site : p.local_secondary_bank()) {
1,087,225✔
943
        thread_bank.push_back(site);
731,450✔
944
      }
945
    }
355,775✔
946

947
    // Drain thread-local bank into the shared secondary bank (once per thread)
948
#pragma omp critical(SharedSecondaryBank)
949
    {
283✔
950
      for (auto& site : thread_bank) {
731,733✔
951
        simulation::shared_secondary_bank_write.thread_unsafe_append(site);
731,450✔
952
      }
953
    }
954
  }
955

956
  simulation::simulation_tracks_completed += settings::n_particles;
667✔
957

958
  // Phase 2: Now that the secondary bank has been populated, enter loop over
959
  // all secondary generations
960
  int n_generation_depth = 1;
667✔
961
  int64_t alive_secondary = 1;
667✔
962
  while (alive_secondary) {
8,912✔
963

964
    // Sort the shared secondary bank by parent ID then progeny ID to
965
    // ensure reproducibility.
966
    sort_bank(simulation::shared_secondary_bank_write, false);
8,245✔
967

968
    // Synchronize the shared secondary bank amongst all MPI ranks, such
969
    // that each MPI rank has an approximately equal number of secondary
970
    // tracks. Also reports the total number of secondaries alive across
971
    // all MPI ranks.
972
    alive_secondary = synchronize_global_secondary_bank(
8,245✔
973
      simulation::shared_secondary_bank_write);
974

975
    // Recalculate work for each MPI rank based on number of alive secondary
976
    // tracks
977
    calculate_work(alive_secondary);
8,245✔
978

979
    // Display the number of secondary tracks in this generation. This
980
    // is useful for user monitoring so as to see if the secondary population is
981
    // exploding and to determine how many generations of secondaries are being
982
    // transported.
983
    if (mpi::master) {
8,245✔
984
      write_message(fmt::format(" Secondary generation {:<2}    tracks: {}",
13,146✔
985
                      n_generation_depth, alive_secondary),
986
        6);
987
    }
988

989
    simulation::shared_secondary_bank_read =
8,245✔
990
      std::move(simulation::shared_secondary_bank_write);
8,245✔
991
    simulation::shared_secondary_bank_write = SharedArray<SourceSite>();
8,245!
992
    simulation::progeny_per_particle.resize(
8,245✔
993
      simulation::shared_secondary_bank_read.size());
8,245✔
994
    std::fill(simulation::progeny_per_particle.begin(),
16,490✔
995
      simulation::progeny_per_particle.end(), 0);
8,245✔
996

997
    // Transport all secondary tracks from the shared secondary bank
998
#pragma omp parallel
4,652✔
999
    {
3,593✔
1000
      vector<SourceSite> thread_bank;
3,593✔
1001

1002
#pragma omp for schedule(runtime)
1003
      for (int64_t i = 1; i <= simulation::shared_secondary_bank_read.size();
9,727,253✔
1004
           i++) {
1005
        Particle p;
9,723,660✔
1006
        initialize_particle_track(p, i, true);
9,723,660✔
1007
        SourceSite& site = simulation::shared_secondary_bank_read[i - 1];
9,723,660✔
1008
        p.event_revive_from_secondary(site);
9,723,660✔
1009
        transport_history_based_single_particle(p);
9,723,660✔
1010
        for (auto& secondary_site : p.local_secondary_bank()) {
18,715,870✔
1011
          thread_bank.push_back(secondary_site);
8,992,210✔
1012
        }
1013
      }
9,723,660✔
1014

1015
      // Drain thread-local bank into the shared secondary bank (once per
1016
      // thread)
1017
#pragma omp critical(SharedSecondaryBank)
1018
      {
3,593✔
1019
        for (auto& secondary_site : thread_bank) {
8,995,803✔
1020
          simulation::shared_secondary_bank_write.thread_unsafe_append(
8,992,210✔
1021
            secondary_site);
1022
        }
1023
      }
1024
    } // End of transport loop over tracks in shared secondary bank
3,593✔
1025
    n_generation_depth++;
8,245✔
1026
    simulation::simulation_tracks_completed += alive_secondary;
8,245✔
1027
  } // End of loop over secondary generations
1028

1029
  // Reset work so that fission bank etc works correctly
1030
  calculate_work(settings::n_particles);
667✔
1031
}
667✔
1032

1033
void transport_event_based()
3,200✔
1034
{
1035
  int64_t remaining_work = simulation::work_per_rank;
3,200✔
1036
  int64_t source_offset = 0;
3,200✔
1037

1038
  // To cap the total amount of memory used to store particle object data, the
1039
  // number of particles in flight at any point in time can bet set. In the case
1040
  // that the maximum in flight particle count is lower than the total number
1041
  // of particles that need to be run this iteration, the event-based transport
1042
  // loop is executed multiple times until all particles have been completed.
1043
  while (remaining_work > 0) {
6,400✔
1044
    // Figure out # of particles to run for this subiteration
1045
    int64_t n_particles =
3,200!
1046
      std::min(remaining_work, settings::max_particles_in_flight);
3,200✔
1047

1048
    // Initialize all particle histories for this subiteration
1049
    process_init_events(n_particles, source_offset);
3,200✔
1050
    process_transport_events();
3,200✔
1051
    process_death_events(n_particles);
3,200✔
1052

1053
    // Adjust remaining work and source offset variables
1054
    remaining_work -= n_particles;
3,200✔
1055
    source_offset += n_particles;
3,200✔
1056
  }
1057
}
3,200✔
1058

1059
void transport_event_based_shared_secondary()
11✔
1060
{
1061
  // Clear shared secondary banks from any prior use
1062
  simulation::shared_secondary_bank_read.clear();
11✔
1063
  simulation::shared_secondary_bank_write.clear();
11✔
1064

1065
  if (mpi::master) {
11!
1066
    write_message(fmt::format(" Primary source          particles: {}",
22!
1067
                    settings::n_particles),
1068
      6);
1069
  }
1070

1071
  simulation::progeny_per_particle.resize(simulation::work_per_rank);
11✔
1072
  std::fill(simulation::progeny_per_particle.begin(),
22✔
1073
    simulation::progeny_per_particle.end(), 0);
11✔
1074

1075
  // Phase 1: Transport primary particles using event-based processing and
1076
  // deposit first generation of secondaries in the shared secondary bank
1077
  int64_t remaining_work = simulation::work_per_rank;
11✔
1078
  int64_t source_offset = 0;
11✔
1079

1080
  while (remaining_work > 0) {
22✔
1081
    int64_t n_particles =
11!
1082
      std::min(remaining_work, settings::max_particles_in_flight);
11✔
1083

1084
    process_init_events(n_particles, source_offset);
11✔
1085
    process_transport_events();
11✔
1086
    process_death_events(n_particles);
11✔
1087

1088
    // Collect secondaries from all particle buffers into shared bank
1089
    for (int64_t i = 0; i < n_particles; i++) {
1,661✔
1090
      for (auto& site : simulation::particles[i].local_secondary_bank()) {
6,132✔
1091
        simulation::shared_secondary_bank_write.thread_unsafe_append(site);
4,482✔
1092
      }
1093
      simulation::particles[i].local_secondary_bank().clear();
3,017✔
1094
    }
1095

1096
    remaining_work -= n_particles;
11✔
1097
    source_offset += n_particles;
11✔
1098
  }
1099

1100
  simulation::simulation_tracks_completed += settings::n_particles;
11✔
1101

1102
  // Phase 2: Now that the secondary bank has been populated, enter loop over
1103
  // all secondary generations
1104
  int n_generation_depth = 1;
11✔
1105
  int64_t alive_secondary = 1;
11✔
1106
  while (alive_secondary) {
417✔
1107

1108
    // Sort the shared secondary bank by parent ID then progeny ID to
1109
    // ensure reproducibility.
1110
    sort_bank(simulation::shared_secondary_bank_write, false);
406✔
1111

1112
    // Synchronize the shared secondary bank amongst all MPI ranks, such
1113
    // that each MPI rank has an approximately equal number of secondary
1114
    // tracks.
1115
    alive_secondary = synchronize_global_secondary_bank(
406✔
1116
      simulation::shared_secondary_bank_write);
1117

1118
    // Recalculate work for each MPI rank based on number of alive secondary
1119
    // tracks
1120
    calculate_work(alive_secondary);
406✔
1121

1122
    if (mpi::master) {
406!
1123
      write_message(fmt::format(" Secondary generation {:<2}    tracks: {}",
812!
1124
                      n_generation_depth, alive_secondary),
1125
        6);
1126
    }
1127

1128
    simulation::shared_secondary_bank_read =
406✔
1129
      std::move(simulation::shared_secondary_bank_write);
406✔
1130
    simulation::shared_secondary_bank_write = SharedArray<SourceSite>();
406!
1131
    simulation::progeny_per_particle.resize(
406✔
1132
      simulation::shared_secondary_bank_read.size());
406✔
1133
    std::fill(simulation::progeny_per_particle.begin(),
812✔
1134
      simulation::progeny_per_particle.end(), 0);
406✔
1135

1136
    // Ensure particle buffer is large enough for this secondary generation
1137
    int64_t sec_buffer_length = std::min(
406!
1138
      static_cast<int64_t>(simulation::shared_secondary_bank_read.size()),
406!
1139
      settings::max_particles_in_flight);
406✔
1140
    if (sec_buffer_length >
406✔
1141
        static_cast<int64_t>(simulation::particles.size())) {
406✔
1142
      init_event_queues(sec_buffer_length);
34✔
1143
    }
1144

1145
    // Transport secondary tracks using event-based processing
1146
    int64_t sec_remaining = simulation::shared_secondary_bank_read.size();
406✔
1147
    int64_t sec_offset = 0;
406✔
1148

1149
    while (sec_remaining > 0) {
801✔
1150
      int64_t n_particles =
395!
1151
        std::min(sec_remaining, settings::max_particles_in_flight);
395✔
1152

1153
      process_init_secondary_events(
395✔
1154
        n_particles, sec_offset, simulation::shared_secondary_bank_read);
1155
      process_transport_events();
395✔
1156
      process_death_events(n_particles);
395✔
1157

1158
      // Collect secondaries from all particle buffers into shared bank
1159
      for (int64_t i = 0; i < n_particles; i++) {
180,005✔
1160
        for (auto& site : simulation::particles[i].local_secondary_bank()) {
354,738✔
1161
          simulation::shared_secondary_bank_write.thread_unsafe_append(site);
175,128✔
1162
        }
1163
        simulation::particles[i].local_secondary_bank().clear();
226,133✔
1164
      }
1165

1166
      sec_remaining -= n_particles;
395✔
1167
      sec_offset += n_particles;
395✔
1168
    } // End of subiteration loop over secondary tracks
1169
    n_generation_depth++;
406✔
1170
    simulation::simulation_tracks_completed += alive_secondary;
406✔
1171
  } // End of loop over secondary generations
1172

1173
  // Reset work so that fission bank etc works correctly
1174
  calculate_work(settings::n_particles);
11✔
1175
}
11✔
1176

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