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

openmc-dev / openmc / 28612311732

02 Jul 2026 06:21PM UTC coverage: 81.29% (+0.009%) from 81.281%
28612311732

Pull #3734

github

web-flow
Merge b05aeac7a into df6f94300
Pull Request #3734: Specify temperature from a field (structured mesh only)

18335 of 26578 branches covered (68.99%)

Branch coverage included in aggregate %.

292 of 323 new or added lines in 18 files covered. (90.4%)

318 existing lines in 12 files now uncovered.

59539 of 69220 relevant lines covered (86.01%)

49339464.46 hits per line

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

95.2
/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/constants.h"
7
#include "openmc/container_util.h"
8
#include "openmc/eigenvalue.h"
9
#include "openmc/error.h"
10
#include "openmc/event.h"
11
#include "openmc/field.h"
12
#include "openmc/geometry_aux.h"
13
#include "openmc/ifp.h"
14
#include "openmc/material.h"
15
#include "openmc/message_passing.h"
16
#include "openmc/nuclide.h"
17
#include "openmc/output.h"
18
#include "openmc/particle.h"
19
#include "openmc/photon.h"
20
#include "openmc/random_lcg.h"
21
#include "openmc/random_ray/flat_source_domain.h"
22
#include "openmc/settings.h"
23
#include "openmc/source.h"
24
#include "openmc/state_point.h"
25
#include "openmc/tallies/derivative.h"
26
#include "openmc/tallies/filter.h"
27
#include "openmc/tallies/tally.h"
28
#include "openmc/tallies/trigger.h"
29
#include "openmc/timer.h"
30
#include "openmc/track_output.h"
31
#include "openmc/weight_windows.h"
32

33
#ifdef _OPENMP
34
#include <omp.h>
35
#endif
36
#include "openmc/tensor.h"
37

38
#ifdef OPENMC_MPI
39
#include <mpi.h>
40
#endif
41

42
#include <fmt/format.h>
43

44
#include <algorithm>
45
#include <cmath>
46
#include <string>
47

48
//==============================================================================
49
// C API functions
50
//==============================================================================
51

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

56
int openmc_run()
6,853 ✔
57
{
58
  openmc::simulation::time_total.start();
6,853 ✔
59
  openmc_simulation_init();
6,853 ✔
60

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

68
  int err = 0;
69
  while (status == 0 && err == 0) {
151,722 ✔
70
    err = openmc_next_batch(&status);
144,882 ✔
71
  }
72

73
  openmc_simulation_finalize();
6,840 ✔
74
  openmc::simulation::time_total.stop();
6,840 ✔
75
  return err;
6,840 ✔
76
}
77

78
int openmc_simulation_init()
8,025 ✔
79
{
80
  using namespace openmc;
8,025 ✔
81

82
  // Skip if simulation has already been initialized
83
  if (simulation::initialized)
8,025 ✔
84
    return 0;
85

86
  // Initialize nuclear data (energy limits, log grid)
87
  if (settings::run_CE) {
8,003 ✔
88
    initialize_data();
6,592 ✔
89
  }
90

91
  // Determine how much work each process should do
92
  calculate_work(settings::n_particles);
8,003 ✔
93

94
  // Allocate source, fission and surface source banks.
95
  allocate_banks();
8,003 ✔
96

97
  // Create track file if needed
98
  if (!settings::track_identifiers.empty() || settings::write_all_tracks) {
8,003 ✔
99
    open_track_file();
90 ✔
100
  }
101

102
  // If doing an event-based simulation, intialize the particle buffer
103
  // and event queues
104
  if (settings::event_based) {
8,003 ✔
105
    int64_t event_buffer_length =
233 !
106
      std::min(simulation::work_per_rank, settings::max_particles_in_flight);
233 ✔
107
    init_event_queues(event_buffer_length);
233 ✔
108
  }
109

110
  // Allocate tally results arrays if they're not allocated yet
111
  for (auto& t : model::tallies) {
35,747 ✔
112
    t->set_strides();
27,744 ✔
113
    t->init_results();
27,744 ✔
114
  }
115

116
  // Set up material nuclide index mapping
117
  for (auto& mat : model::materials) {
27,929 ✔
118
    mat->init_nuclide_index();
19,926 ✔
119
  }
120

121
  // Reset global variables -- this is done before loading state point (as that
122
  // will potentially populate k_generation and entropy)
123
  simulation::current_batch = 0;
8,003 ✔
124
  simulation::ct_current_file = 1;
8,003 ✔
125
  simulation::ssw_current_file = 1;
8,003 ✔
126
  simulation::k_generation.clear();
8,003 ✔
127
  simulation::entropy.clear();
8,003 ✔
128
  reset_source_rejection_counters();
8,003 ✔
129
  openmc_reset();
8,003 ✔
130

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

144
  // Display header
145
  if (mpi::master) {
8,003 ✔
146
    if (settings::run_mode == RunMode::FIXED_SOURCE) {
6,943 ✔
147
      if (settings::solver_type == SolverType::MONTE_CARLO) {
3,019 ✔
148
        header("FIXED SOURCE TRANSPORT SIMULATION", 3);
2,643 ✔
149
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
376 !
150
        header("FIXED SOURCE TRANSPORT SIMULATION (RANDOM RAY SOLVER)", 3);
376 ✔
151
      }
152
    } else if (settings::run_mode == RunMode::EIGENVALUE) {
3,924 !
153
      if (settings::solver_type == SolverType::MONTE_CARLO) {
3,924 ✔
154
        header("K EIGENVALUE SIMULATION", 3);
3,649 ✔
155
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
275 !
156
        header("K EIGENVALUE SIMULATION (RANDOM RAY SOLVER)", 3);
275 ✔
157
      }
158
      if (settings::verbosity >= 7)
3,924 ✔
159
        print_columns();
3,543 ✔
160
    }
161
  }
162

163
  // load weight windows from file
164
  if (!settings::weight_windows_file.empty()) {
8,003 !
UNCOV
165
    openmc_weight_windows_import(settings::weight_windows_file.c_str());
×
166
  }
167

168
  // Set flag indicating initialization is done
169
  simulation::initialized = true;
8,003 ✔
170
  return 0;
8,003 ✔
171
}
172

173
int openmc_simulation_finalize()
7,990 ✔
174
{
175
  using namespace openmc;
7,990 ✔
176

177
  // Skip if simulation was never run
178
  if (!simulation::initialized)
7,990 !
179
    return 0;
180

181
  // Stop active batch timer and start finalization timer
182
  simulation::time_active.stop();
7,990 ✔
183
  simulation::time_finalize.start();
7,990 ✔
184

185
  // Clear material nuclide mapping
186
  for (auto& mat : model::materials) {
27,903 ✔
187
    mat->mat_nuclide_index_.clear();
39,826 !
188
  }
189

190
  // Close track file if open
191
  if (!settings::track_identifiers.empty() || settings::write_all_tracks) {
7,990 ✔
192
    close_track_file();
90 ✔
193
  }
194

195
  // Increment total number of generations
196
  simulation::total_gen += simulation::current_batch * settings::gen_per_batch;
7,990 ✔
197

198
#ifdef OPENMC_MPI
199
  broadcast_results();
3,603 ✔
200
#endif
201

202
  // Write tally results to tallies.out
203
  if (settings::output_tallies && mpi::master)
7,990 !
204
    write_tallies();
6,584 ✔
205

206
  // If weight window generators are present in this simulation, write a
207
  // weight windows file. This is skipped during the forward solve of an
208
  // adjoint (FW-CADIS) run, where only the adjoint-derived weight windows
209
  // are meaningful.
210
  if (variance_reduction::weight_windows_generators.size() > 0 &&
7,990 ✔
211
      FlatSourceDomain::solve_ != RandomRaySolve::FORWARD_FOR_ADJOINT) {
166 ✔
212
    openmc_weight_windows_export();
105 ✔
213
  }
214

215
  // Deactivate all tallies
216
  for (auto& t : model::tallies) {
35,734 ✔
217
    t->active_ = false;
27,744 ✔
218
  }
219

220
  // Stop timers and show timing statistics
221
  simulation::time_finalize.stop();
7,990 ✔
222
  simulation::time_total.stop();
7,990 ✔
223

224
#ifdef OPENMC_MPI
225
  // Reduce track count across ranks for correct reporting. In shared secondary
226
  // bank mode, all ranks already have the global count; in non-shared mode,
227
  // each rank only has its own count.
228
  if (settings::weight_windows_on && !settings::use_shared_secondary_bank) {
3,603 ✔
229
    int64_t total_tracks;
92 ✔
230
    MPI_Reduce(&simulation::simulation_tracks_completed, &total_tracks, 1,
92 ✔
231
      MPI_INT64_T, MPI_SUM, 0, mpi::intracomm);
232
    if (mpi::master)
92 ✔
233
      simulation::simulation_tracks_completed = total_tracks;
76 ✔
234
  }
235
#endif
236

237
  if (mpi::master) {
7,990 ✔
238
    if (settings::solver_type != SolverType::RANDOM_RAY) {
6,930 ✔
239
      if (settings::verbosity >= 6)
6,279 ✔
240
        print_runtime();
5,898 ✔
241
      if (settings::verbosity >= 4)
6,279 ✔
242
        print_results();
5,898 ✔
243
    }
244
  }
245
  if (settings::check_overlaps)
7,990 !
UNCOV
246
    print_overlap_check();
×
247

248
  // Reset flags
249
  simulation::initialized = false;
7,990 ✔
250
  return 0;
7,990 ✔
251
}
252

253
int openmc_next_batch(int* status)
149,007 ✔
254
{
255
  using namespace openmc;
149,007 ✔
256
  using openmc::simulation::current_gen;
149,007 ✔
257

258
  // Make sure simulation has been initialized
259
  if (!simulation::initialized) {
149,007 ✔
260
    set_errmsg("Simulation has not been initialized yet.");
11 ✔
261
    return OPENMC_E_ALLOCATE;
11 ✔
262
  }
263

264
  initialize_batch();
148,996 ✔
265

266
  // =======================================================================
267
  // LOOP OVER GENERATIONS
268
  for (current_gen = 1; current_gen <= settings::gen_per_batch; ++current_gen) {
298,189 ✔
269

270
    initialize_generation();
149,206 ✔
271

272
    // Start timer for transport
273
    simulation::time_transport.start();
149,206 ✔
274

275
    // Transport loop
276
    if (settings::event_based) {
149,206 ✔
277
      if (settings::use_shared_secondary_bank) {
3,461 ✔
278
        transport_event_based_shared_secondary();
11 ✔
279
      } else {
280
        transport_event_based();
3,450 ✔
281
      }
282
    } else {
283
      if (settings::use_shared_secondary_bank) {
145,745 ✔
284
        transport_history_based_shared_secondary();
667 ✔
285
      } else {
286
        transport_history_based();
145,078 ✔
287
      }
288
    }
289

290
    // Accumulate time for transport
291
    simulation::time_transport.stop();
149,193 ✔
292

293
    finalize_generation();
149,193 ✔
294
  }
295

296
  finalize_batch();
148,983 ✔
297

298
  // Check simulation ending criteria
299
  if (status) {
148,983 !
300
    if (simulation::current_batch >= settings::n_max_batches) {
148,983 ✔
301
      *status = STATUS_EXIT_MAX_BATCH;
7,033 ✔
302
    } else if (simulation::satisfy_triggers) {
141,950 ✔
303
      *status = STATUS_EXIT_ON_TRIGGER;
93 ✔
304
    } else {
305
      *status = STATUS_EXIT_NORMAL;
141,857 ✔
306
    }
307
  }
308
  return 0;
309
}
310

311
bool openmc_is_statepoint_batch()
3,135 ✔
312
{
313
  using namespace openmc;
3,135 ✔
314
  using openmc::simulation::current_gen;
3,135 ✔
315

316
  if (!simulation::initialized)
3,135 !
317
    return false;
318
  else
319
    return contains(settings::statepoint_batch, simulation::current_batch);
6,270 ✔
320
}
321

322
namespace openmc {
323

324
//==============================================================================
325
// Global variables
326
//==============================================================================
327

328
namespace simulation {
329

330
int ct_current_file;
331
int current_batch;
332
int current_gen;
333
bool initialized {false};
334
double keff {1.0};
335
double keff_std;
336
double k_col_abs {0.0};
337
double k_col_tra {0.0};
338
double k_abs_tra {0.0};
339
double log_spacing;
340
int n_lost_particles {0};
341
bool need_depletion_rx {false};
342
int restart_batch;
343
bool satisfy_triggers {false};
344
int ssw_current_file;
345
int total_gen {0};
346
double total_weight;
347
int64_t work_per_rank;
348

349
const RegularMesh* entropy_mesh {nullptr};
350
const RegularMesh* ufs_mesh {nullptr};
351

352
TemperatureField temperature_field;
353

354
vector<double> k_generation;
355
vector<int64_t> work_index;
356

357
int64_t simulation_tracks_completed {0};
358

359
} // namespace simulation
360

361
//==============================================================================
362
// Non-member functions
363
//==============================================================================
364

365
void allocate_banks()
8,003 ✔
366
{
367
  if (settings::run_mode == RunMode::EIGENVALUE &&
8,003 ✔
368
      settings::solver_type == SolverType::MONTE_CARLO) {
4,670 ✔
369
    // Allocate source bank
370
    simulation::source_bank.resize(simulation::work_per_rank);
4,299 ✔
371

372
    // Allocate fission bank
373
    init_fission_bank(3 * simulation::work_per_rank);
4,299 ✔
374

375
    // Allocate IFP bank
376
    if (settings::ifp_on) {
4,299 ✔
377
      resize_simulation_ifp_banks();
74 ✔
378
    }
379
  }
380

381
  if (settings::surf_source_write) {
8,003 ✔
382
    // Allocate surface source bank
383
    simulation::surf_source_bank.reserve(settings::ssw_max_particles);
1,155 ✔
384
  }
385

386
  if (settings::collision_track) {
8,003 ✔
387
    // Allocate collision track bank
388
    collision_track_reserve_bank();
160 ✔
389
  }
390
}
8,003 ✔
391

392
void initialize_batch()
169,958 ✔
393
{
394
  // Increment current batch
395
  ++simulation::current_batch;
169,958 ✔
396
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
169,958 ✔
397
    if (settings::solver_type == SolverType::RANDOM_RAY &&
65,225 ✔
398
        simulation::current_batch < settings::n_inactive + 1) {
14,112 ✔
399
      write_message(
16,812 ✔
400
        6, "Simulating batch {:<4} (inactive)", simulation::current_batch);
401
    } else {
402
      write_message(6, "Simulating batch {}", simulation::current_batch);
113,638 ✔
403
    }
404
  }
405

406
  // Reset total starting particle weight used for normalizing tallies
407
  simulation::total_weight = 0.0;
169,958 ✔
408

409
  // Determine if this batch is the first inactive or active batch.
410
  bool first_inactive = false;
169,958 ✔
411
  bool first_active = false;
169,958 ✔
412
  if (!settings::restart_run) {
169,958 ✔
413
    first_inactive = settings::n_inactive > 0 && simulation::current_batch == 1;
169,795 ✔
414
    first_active = simulation::current_batch == settings::n_inactive + 1;
169,795 ✔
415
  } else if (simulation::current_batch == simulation::restart_batch + 1) {
163 ✔
416
    first_inactive = simulation::restart_batch < settings::n_inactive;
52 ✔
417
    first_active = !first_inactive;
52 ✔
418
  }
419

420
  // Manage active/inactive timers and activate tallies if necessary.
421
  if (first_inactive) {
169,847 ✔
422
    simulation::time_inactive.start();
3,866 ✔
423
  } else if (first_active) {
166,092 ✔
424
    simulation::time_inactive.stop();
7,956 ✔
425
    simulation::time_active.start();
7,956 ✔
426
    for (auto& t : model::tallies) {
35,678 ✔
427
      t->active_ = true;
27,722 ✔
428
    }
429
  }
430

431
  // Add user tallies to active tallies list
432
  setup_active_tallies();
169,958 ✔
433
}
169,958 ✔
434

435
void finalize_batch()
169,945 ✔
436
{
437
  // Reduce tallies onto master process and accumulate
438
  simulation::time_tallies.start();
169,945 ✔
439
  accumulate_tallies();
169,945 ✔
440
  simulation::time_tallies.stop();
169,945 ✔
441

442
  // update weight windows if needed
443
  for (const auto& wwg : variance_reduction::weight_windows_generators) {
172,577 ✔
444
    wwg->update();
2,632 ✔
445
  }
446

447
  // Reset global tally results
448
  if (simulation::current_batch <= settings::n_inactive) {
169,945 ✔
449
    simulation::global_tallies.fill(0.0);
32,613 ✔
450
    simulation::n_realizations = 0;
32,613 ✔
451
  }
452

453
  // Check_triggers
454
  if (mpi::master)
169,945 ✔
455
    check_triggers();
149,986 ✔
456
#ifdef OPENMC_MPI
457
  MPI_Bcast(&simulation::satisfy_triggers, 1, MPI_C_BOOL, 0, mpi::intracomm);
74,894 ✔
458
#endif
459
  if (simulation::satisfy_triggers ||
169,945 ✔
460
      (settings::trigger_on &&
2,567 ✔
461
        simulation::current_batch == settings::n_max_batches)) {
2,567 ✔
462
    settings::statepoint_batch.insert(simulation::current_batch);
141 ✔
463
  }
464

465
  // Write out state point if it's been specified for this batch and is not
466
  // a CMFD run instance
467
  if (contains(settings::statepoint_batch, simulation::current_batch) &&
339,890 ✔
468
      !settings::cmfd_run) {
8,258 ✔
469
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
15,885 ✔
470
        settings::source_write && !settings::source_separate) {
15,002 ✔
471
      bool b = (settings::run_mode == RunMode::EIGENVALUE);
6,849 ✔
472
      openmc_statepoint_write(nullptr, &b);
6,849 ✔
473
    } else {
474
      bool b = false;
1,233 ✔
475
      openmc_statepoint_write(nullptr, &b);
1,233 ✔
476
    }
477
  }
478

479
  if (settings::run_mode == RunMode::EIGENVALUE) {
169,945 ✔
480
    // Write out a separate source point if it's been specified for this batch
481
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
109,428 ✔
482
        settings::source_write && settings::source_separate) {
109,057 ✔
483

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

493
    // Write a continously-overwritten source point if requested.
494
    if (settings::source_latest) {
104,733 ✔
495
      auto filename = settings::path_output + "source";
150 ✔
496
      span<SourceSite> bankspan(simulation::source_bank);
150 ✔
497
      write_source_point(filename, bankspan, simulation::work_index,
300 ✔
498
        settings::source_mcpl_write);
499
    }
150 ✔
500
  }
501

502
  // Write out surface source if requested.
503
  if (settings::surf_source_write &&
169,945 ✔
504
      simulation::ssw_current_file <= settings::ssw_max_files) {
17,699 ✔
505
    bool last_batch = (simulation::current_batch == settings::n_batches);
1,977 ✔
506
    if (simulation::surf_source_bank.full() || last_batch) {
1,977 ✔
507
      // Determine appropriate filename
508
      auto filename = fmt::format("{}surface_source.{}", settings::path_output,
1,188 ✔
509
        simulation::current_batch);
1,188 ✔
510
      if (settings::ssw_max_files == 1 ||
1,188 ✔
511
          (simulation::ssw_current_file == 1 && last_batch)) {
55 !
512
        filename = settings::path_output + "surface_source";
1,133 ✔
513
      }
514

515
      // Get span of source bank and calculate parallel index vector
516
      auto surf_work_index = mpi::calculate_parallel_index_vector(
1,188 ✔
517
        simulation::surf_source_bank.size());
1,188 ✔
518
      span<SourceSite> surfbankspan(simulation::surf_source_bank.begin(),
1,188 ✔
519
        simulation::surf_source_bank.size());
1,188 ✔
520

521
      // Write surface source file
522
      write_source_point(
1,188 ✔
523
        filename, surfbankspan, surf_work_index, settings::surf_mcpl_write);
524

525
      // Reset surface source bank and increment counter
526
      simulation::surf_source_bank.clear();
1,188 ✔
527
      if (!last_batch && settings::ssw_max_files >= 1) {
1,188 !
528
        simulation::surf_source_bank.reserve(settings::ssw_max_particles);
1,006 ✔
529
      }
530
      ++simulation::ssw_current_file;
1,188 ✔
531
    }
1,188 ✔
532
  }
533
  // Write collision track file if requested
534
  if (settings::collision_track) {
169,945 ✔
535
    collision_track_flush_bank();
580 ✔
536
  }
537
}
169,945 ✔
538

539
void initialize_generation()
170,168 ✔
540
{
541
  if (settings::run_mode == RunMode::EIGENVALUE) {
170,168 ✔
542
    // Clear out the fission bank
543
    simulation::fission_bank.resize(0);
104,943 ✔
544

545
    // Count source sites if using uniform fission source weighting
546
    if (settings::ufs_on)
104,943 ✔
547
      ufs_count_sites();
150 ✔
548

549
    // Store current value of tracklength k
550
    simulation::keff_generation = simulation::global_tallies(
104,943 ✔
551
      GlobalTally::K_TRACKLENGTH, TallyResult::VALUE);
552
  }
553
}
170,168 ✔
554

555
void finalize_generation()
170,155 ✔
556
{
557
  auto& gt = simulation::global_tallies;
170,155 ✔
558

559
  // Update global tallies with the accumulation variables
560
  if (settings::run_mode == RunMode::EIGENVALUE) {
170,155 ✔
561
    gt(GlobalTally::K_COLLISION, TallyResult::VALUE) += global_tally_collision;
104,943 ✔
562
    gt(GlobalTally::K_ABSORPTION, TallyResult::VALUE) +=
104,943 ✔
563
      global_tally_absorption;
564
    gt(GlobalTally::K_TRACKLENGTH, TallyResult::VALUE) +=
104,943 ✔
565
      global_tally_tracklength;
566
  }
567
  gt(GlobalTally::LEAKAGE, TallyResult::VALUE) += global_tally_leakage;
170,155 ✔
568

569
  // reset tallies
570
  if (settings::run_mode == RunMode::EIGENVALUE) {
170,155 ✔
571
    global_tally_collision = 0.0;
104,943 ✔
572
    global_tally_absorption = 0.0;
104,943 ✔
573
    global_tally_tracklength = 0.0;
104,943 ✔
574
  }
575
  global_tally_leakage = 0.0;
170,155 ✔
576

577
  if (settings::run_mode == RunMode::EIGENVALUE &&
170,155 ✔
578
      settings::solver_type == SolverType::MONTE_CARLO) {
104,943 ✔
579
    // If using shared memory, stable sort the fission bank (by parent IDs)
580
    // so as to allow for reproducibility regardless of which order particles
581
    // are run in.
582
    sort_bank(simulation::fission_bank, true);
98,093 ✔
583

584
    // Distribute fission bank across processors evenly
585
    synchronize_bank();
98,093 ✔
586
  }
587

588
  if (settings::run_mode == RunMode::EIGENVALUE) {
170,155 ✔
589

590
    // Calculate shannon entropy
591
    if (settings::entropy_on &&
104,943 ✔
592
        settings::solver_type == SolverType::MONTE_CARLO)
14,535 ✔
593
      shannon_entropy();
7,685 ✔
594

595
    // Collect results and statistics
596
    calculate_generation_keff();
104,943 ✔
597
    calculate_average_keff();
104,943 ✔
598

599
    // Write generation output
600
    if (mpi::master && settings::verbosity >= 7) {
104,943 ✔
601
      print_generation();
78,808 ✔
602
    }
603
  }
604
}
170,155 ✔
605

606
void sample_source_particle(Particle& p, int64_t index_source)
178,472,522 ✔
607
{
608
  // Sample a particle from the source bank
609
  if (settings::run_mode == RunMode::EIGENVALUE) {
178,472,522 ✔
610
    p.from_source(&simulation::source_bank[index_source - 1]);
150,478,000 ✔
611
  } else if (settings::run_mode == RunMode::FIXED_SOURCE) {
27,994,522 !
612
    // initialize random number seed
613
    int64_t id = compute_transport_seed(compute_particle_id(index_source));
27,994,522 ✔
614
    uint64_t seed = init_seed(id, STREAM_SOURCE);
27,994,522 ✔
615
    // sample from external source distribution or custom library then set
616
    auto site = sample_external_source(&seed);
27,994,522 ✔
617
    p.from_source(&site);
27,994,518 ✔
618
  }
619
}
178,472,518 ✔
620

621
void initialize_particle_track(
199,843,675 ✔
622
  Particle& p, int64_t index_source, bool is_secondary)
623
{
624
  // Note: index_source is 1-based (first particle = 1), but current_work() is
625
  // stored as 0-based for direct use as an array index into
626
  // progeny_per_particle, source_bank, ifp banks, etc.
627
  if (!is_secondary) {
199,843,675 ✔
628
    sample_source_particle(p, index_source);
178,472,522 ✔
629
  }
630

631
  p.current_work() = index_source - 1;
199,843,671 ✔
632

633
  // set identifier for particle
634
  p.id() = compute_particle_id(index_source);
199,843,671 ✔
635

636
  // set progeny count to zero
637
  p.n_progeny() = 0;
199,843,671 ✔
638

639
  // Reset particle event counter
640
  p.n_event() = 0;
199,843,671 ✔
641

642
  // Initialize track counter (1 for this primary/secondary track)
643
  p.n_tracks() = 1;
199,843,671 ✔
644

645
  // Reset split counter
646
  p.n_split() = 0;
199,843,671 ✔
647

648
  // Reset weight window ratio
649
  p.ww_factor() = 0.0;
199,843,671 ✔
650

651
  // set particle history start weight
652
  p.wgt_born() = p.wgt();
199,843,671 ✔
653

654
  // Reset pulse_height_storage
655
  std::fill(p.pht_storage().begin(), p.pht_storage().end(), 0);
199,843,671 ✔
656

657
  // set random number seed
658
  int64_t particle_seed = compute_transport_seed(p.id());
199,843,671 ✔
659
  init_particle_seeds(particle_seed, p.seeds());
199,843,671 ✔
660

661
  // set particle trace
662
  p.trace() = false;
199,843,671 ✔
663
  if (simulation::current_batch == settings::trace_batch &&
199,854,671 ✔
664
      simulation::current_gen == settings::trace_gen &&
199,843,671 !
665
      p.id() == settings::trace_particle)
11,000 ✔
666
    p.trace() = true;
11 ✔
667

668
  // Set particle track.
669
  p.write_track() = check_track_criteria(p);
199,843,671 ✔
670

671
  // Set the particle's initial weight window value.
672
  if (!is_secondary) {
199,843,671 ✔
673
    p.wgt_ww_born() = -1.0;
178,472,518 ✔
674
    apply_weight_windows(p);
178,472,518 ✔
675
  }
676

677
  // Display message if high verbosity or trace is on
678
  if (settings::verbosity >= 9 || p.trace()) {
199,843,671 !
679
    write_message("Simulating Particle {}", p.id());
22 ✔
680
  }
681

682
  // Add particle's starting weight to count for normalizing tallies later
683
  if (!is_secondary) {
199,843,671 ✔
684
#pragma omp atomic
98,162,335 ✔
685
    simulation::total_weight += p.wgt();
178,472,518 ✔
686
  }
687

688
  // Force calculation of cross-sections by setting last energy to zero
689
  if (settings::run_CE) {
199,843,671 ✔
690
    p.invalidate_neutron_xs();
85,295,127 ✔
691
  }
692

693
  // Prepare to write out particle track.
694
  if (p.write_track())
199,843,671 ✔
695
    add_particle_track(p);
999 ✔
696
}
199,843,671 ✔
697

698
int overall_generation()
205,180,167 ✔
699
{
700
  using namespace simulation;
205,180,167 ✔
701
  return settings::gen_per_batch * (current_batch - 1) + current_gen;
205,180,167 ✔
702
}
703

704
int64_t compute_particle_id(int64_t index_source)
227,838,468 ✔
705
{
706
  if (settings::use_shared_secondary_bank) {
227,838,468 ✔
707
    return simulation::work_index[mpi::rank] + index_source +
22,940,178 ✔
708
           simulation::simulation_tracks_completed;
22,940,178 ✔
709
  } else {
710
    return simulation::work_index[mpi::rank] + index_source;
204,898,290 ✔
711
  }
712
}
713

714
int64_t compute_transport_seed(int64_t particle_id)
227,838,512 ✔
715
{
716
  if (settings::use_shared_secondary_bank) {
227,838,512 ✔
717
    return particle_id;
718
  } else {
719
    return (simulation::total_gen + overall_generation() - 1) *
204,898,323 ✔
720
             settings::n_particles +
721
           particle_id;
204,898,323 ✔
722
  }
723
}
724

725
void calculate_work(int64_t n_particles)
17,306 ✔
726
{
727
  // Determine minimum amount of particles to simulate on each processor
728
  int64_t min_work = n_particles / mpi::n_procs;
17,306 ✔
729

730
  // Determine number of processors that have one extra particle
731
  int64_t remainder = n_particles % mpi::n_procs;
17,306 ✔
732

733
  int64_t i_bank = 0;
17,306 ✔
734
  simulation::work_index.resize(mpi::n_procs + 1);
17,306 ✔
735
  simulation::work_index[0] = 0;
17,306 ✔
736
  for (int i = 0; i < mpi::n_procs; ++i) {
40,243 ✔
737
    // Number of particles for rank i
738
    int64_t work_i = i < remainder ? min_work + 1 : min_work;
22,937 ✔
739

740
    // Set number of particles
741
    if (mpi::rank == i)
22,937 ✔
742
      simulation::work_per_rank = work_i;
17,306 ✔
743

744
    // Set index into source bank for rank i
745
    i_bank += work_i;
22,937 ✔
746
    simulation::work_index[i + 1] = i_bank;
22,937 ✔
747
  }
748
}
17,306 ✔
749

750
void initialize_data()
6,636 ✔
751
{
752
  // Determine minimum/maximum energy for incident neutron/photon data
753
  data::energy_max = {INFTY, INFTY, INFTY, INFTY};
6,636 ✔
754
  data::energy_min = {0.0, 0.0, 0.0, 0.0};
6,636 ✔
755

756
  for (const auto& nuc : data::nuclides) {
41,221 ✔
757
    if (nuc->grid_.size() >= 1) {
34,585 !
758
      int neutron = ParticleType::neutron().transport_index();
34,585 ✔
759
      data::energy_min[neutron] =
34,585 ✔
760
        std::max(data::energy_min[neutron], nuc->grid_[0].energy.front());
40,700 ✔
761
      data::energy_max[neutron] =
34,585 ✔
762
        std::min(data::energy_max[neutron], nuc->grid_[0].energy.back());
42,265 ✔
763
    }
764
  }
765

766
  if (settings::photon_transport) {
6,636 ✔
767
    for (const auto& elem : data::elements) {
1,999 ✔
768
      if (elem->energy_.size() >= 1) {
1,465 !
769
        int photon = ParticleType::photon().transport_index();
1,465 ✔
770
        int n = elem->energy_.size();
1,465 ✔
771
        data::energy_min[photon] =
2,930 ✔
772
          std::max(data::energy_min[photon], std::exp(elem->energy_(1)));
2,401 ✔
773
        data::energy_max[photon] =
1,465 ✔
774
          std::min(data::energy_max[photon], std::exp(elem->energy_(n - 1)));
1,999 ✔
775
      }
776
    }
777

778
    if (settings::electron_treatment == ElectronTreatment::TTB) {
534 ✔
779
      // Determine if minimum/maximum energy for bremsstrahlung is greater/less
780
      // than the current minimum/maximum
781
      if (data::ttb_e_grid.size() >= 1) {
475 !
782
        int photon = ParticleType::photon().transport_index();
475 ✔
783
        int electron = ParticleType::electron().transport_index();
475 ✔
784
        int positron = ParticleType::positron().transport_index();
475 ✔
785
        int n_e = data::ttb_e_grid.size();
475 ✔
786

787
        const std::vector<int> charged = {electron, positron};
475 ✔
788
        for (auto t : charged) {
1,425 ✔
789
          data::energy_min[t] = std::exp(data::ttb_e_grid(1));
950 ✔
790
          data::energy_max[t] = std::exp(data::ttb_e_grid(n_e - 1));
950 ✔
791
        }
792

793
        data::energy_min[photon] =
950 ✔
794
          std::max(data::energy_min[photon], data::energy_min[electron]);
950 !
795

796
        data::energy_max[photon] =
950 ✔
797
          std::min(data::energy_max[photon], data::energy_max[electron]);
950 !
798
      }
475 ✔
799
    }
800
  }
801

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

821
  // Set up logarithmic grid for nuclides
822
  for (auto& nuc : data::nuclides) {
41,221 ✔
823
    nuc->init_grid();
34,585 ✔
824
  }
825
  int neutron = ParticleType::neutron().transport_index();
6,636 ✔
826
  simulation::log_spacing =
13,272 ✔
827
    std::log(data::energy_max[neutron] / data::energy_min[neutron]) /
6,636 ✔
828
    settings::n_log_bins;
829
}
6,636 ✔
830

831
#ifdef OPENMC_MPI
832
void broadcast_results()
3,603 ✔
833
{
834
  // Broadcast tally results so that each process has access to results
835
  for (auto& t : model::tallies) {
17,385 ✔
836
    // Create a new datatype that consists of all values for a given filter
837
    // bin and then use that to broadcast. This is done to minimize the
838
    // chance of the 'count' argument of MPI_BCAST exceeding 2**31
839
    auto& results = t->results_;
13,782 ✔
840

841
    auto shape = results.shape();
13,782 ✔
842
    int count_per_filter = shape[1] * shape[2];
13,782 ✔
843
    MPI_Datatype result_block;
13,782 ✔
844
    MPI_Type_contiguous(count_per_filter, MPI_DOUBLE, &result_block);
13,782 ✔
845
    MPI_Type_commit(&result_block);
13,782 ✔
846
    MPI_Bcast(results.data(), shape[0], result_block, 0, mpi::intracomm);
13,782 ✔
847
    MPI_Type_free(&result_block);
13,782 ✔
848
  }
13,782 ✔
849

850
  // Also broadcast global tally results
851
  auto& gt = simulation::global_tallies;
3,603 ✔
852
  MPI_Bcast(gt.data(), gt.size(), MPI_DOUBLE, 0, mpi::intracomm);
3,603 ✔
853

854
  // These guys are needed so that non-master processes can calculate the
855
  // combined estimate of k-effective
856
  double temp[] {
3,603 ✔
857
    simulation::k_col_abs, simulation::k_col_tra, simulation::k_abs_tra};
3,603 ✔
858
  MPI_Bcast(temp, 3, MPI_DOUBLE, 0, mpi::intracomm);
3,603 ✔
859
  simulation::k_col_abs = temp[0];
3,603 ✔
860
  simulation::k_col_tra = temp[1];
3,603 ✔
861
  simulation::k_abs_tra = temp[2];
3,603 ✔
862
}
3,603 ✔
863

864
#endif
865

866
void free_memory_simulation()
9,163 ✔
867
{
868
  simulation::k_generation.clear();
9,163 ✔
869
  simulation::entropy.clear();
9,163 ✔
870
}
9,163 ✔
871

872
void transport_history_based_single_particle(Particle& p)
187,459,618 ✔
873
{
874
  while (p.alive()) {
2,147,483,647 ✔
875
    p.event_calculate_xs();
2,147,483,647 ✔
876
    if (p.alive()) {
2,147,483,647 !
877
      p.event_advance();
2,147,483,647 ✔
878
    }
879
    if (p.alive()) {
2,147,483,647 !
880
      switch (p.next_event().event_type) {
2,147,483,647 !
881
      case EVENT_CROSS_SURFACE:
2,147,483,647 ✔
882
        p.event_cross_surface();
2,147,483,647 ✔
883
        break;
2,147,483,647 ✔
884
      case EVENT_COLLIDE:
2,147,483,647 ✔
885
        p.event_collide();
2,147,483,647 ✔
886
        break;
2,147,483,647 ✔
887
      case EVENT_TIME_CUTOFF:
223,928 ✔
888
        p.wgt() = 0.0;
223,928 ✔
889
        break;
223,928 ✔
NEW
890
      default:
×
UNCOV
891
        fatal_error(
×
UNCOV
892
          fmt::format("Unknown event '{}' in history-based transport!",
×
UNCOV
893
            p.next_event().event_type));
×
894
        break;
895
      }
896
    }
897
    p.event_check_limit_and_revive();
2,147,483,647 ✔
898
  }
899
  p.event_death();
187,459,609 ✔
900
}
187,459,609 ✔
901

902
void transport_history_based()
145,078 ✔
903
{
904
#pragma omp parallel for schedule(runtime)
80,963 ✔
905
  for (int64_t i_work = 1; i_work <= simulation::work_per_rank; ++i_work) {
80,729,915 ✔
906
    Particle p;
80,665,809 ✔
907
    initialize_particle_track(p, i_work, false);
80,665,809 ✔
908
    transport_history_based_single_particle(p);
80,665,805 ✔
909
  }
80,665,800 ✔
910
}
145,069 ✔
911

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

926
  if (mpi::master) {
667 ✔
927
    write_message(fmt::format(" Primary source          particles: {}",
1,150 ✔
928
                    settings::n_particles),
929
      6);
930
  }
931

932
  simulation::progeny_per_particle.resize(simulation::work_per_rank);
667 ✔
933
  std::fill(simulation::progeny_per_particle.begin(),
1,334 ✔
934
    simulation::progeny_per_particle.end(), 0);
667 ✔
935

936
  // Phase 1: Transport primary particles and deposit first generation of
937
  // secondaries in the shared secondary bank
938
#pragma omp parallel
384 ✔
939
  {
283 ✔
940
    vector<SourceSite> thread_bank;
283 ✔
941

942
#pragma omp for schedule(runtime)
943
    for (int64_t i = 1; i <= simulation::work_per_rank; i++) {
356,058 ✔
944
      Particle p;
355,775 ✔
945
      initialize_particle_track(p, i, false);
355,775 ✔
946
      transport_history_based_single_particle(p);
355,775 ✔
947
      for (auto& site : p.local_secondary_bank()) {
1,087,360 ✔
948
        thread_bank.push_back(site);
731,585 ✔
949
      }
950
    }
355,775 ✔
951

952
    // Drain thread-local bank into the shared secondary bank (once per thread)
953
#pragma omp critical(SharedSecondaryBank)
954
    {
283 ✔
955
      for (auto& site : thread_bank) {
731,868 ✔
956
        simulation::shared_secondary_bank_write.thread_unsafe_append(site);
731,585 ✔
957
      }
958
    }
959
  }
960

961
  simulation::simulation_tracks_completed += settings::n_particles;
667 ✔
962

963
  // Phase 2: Now that the secondary bank has been populated, enter loop over
964
  // all secondary generations
965
  int n_generation_depth = 1;
667 ✔
966
  int64_t alive_secondary = 1;
667 ✔
967
  while (alive_secondary) {
8,888 ✔
968

969
    // Sort the shared secondary bank by parent ID then progeny ID to
970
    // ensure reproducibility.
971
    sort_bank(simulation::shared_secondary_bank_write, false);
8,221 ✔
972

973
    // Synchronize the shared secondary bank amongst all MPI ranks, such
974
    // that each MPI rank has an approximately equal number of secondary
975
    // tracks. Also reports the total number of secondaries alive across
976
    // all MPI ranks.
977
    alive_secondary = synchronize_global_secondary_bank(
8,221 ✔
978
      simulation::shared_secondary_bank_write);
979

980
    // Recalculate work for each MPI rank based on number of alive secondary
981
    // tracks
982
    calculate_work(alive_secondary);
8,221 ✔
983

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

994
    simulation::shared_secondary_bank_read =
8,221 ✔
995
      std::move(simulation::shared_secondary_bank_write);
8,221 ✔
996
    simulation::shared_secondary_bank_write = SharedArray<SourceSite>();
8,221 !
997
    simulation::progeny_per_particle.resize(
8,221 ✔
998
      simulation::shared_secondary_bank_read.size());
8,221 ✔
999
    std::fill(simulation::progeny_per_particle.begin(),
8,221 ✔
1000
      simulation::progeny_per_particle.end(), 0);
8,221 ✔
1001

1002
    // Transport all secondary tracks from the shared secondary bank
1003
#pragma omp parallel
4,640 ✔
1004
    {
3,581 ✔
1005
      vector<SourceSite> thread_bank;
3,581 ✔
1006

1007
#pragma omp for schedule(runtime)
1008
      for (int64_t i = 1; i <= simulation::shared_secondary_bank_read.size();
9,728,676 ✔
1009
           i++) {
1010
        Particle p;
9,725,095 ✔
1011
        initialize_particle_track(p, i, true);
9,725,095 ✔
1012
        SourceSite& site = simulation::shared_secondary_bank_read[i - 1];
9,725,095 ✔
1013
        p.event_revive_from_secondary(site);
9,725,095 ✔
1014
        transport_history_based_single_particle(p);
9,725,095 ✔
1015
        for (auto& secondary_site : p.local_secondary_bank()) {
18,718,605 ✔
1016
          thread_bank.push_back(secondary_site);
8,993,510 ✔
1017
        }
1018
      }
9,725,095 ✔
1019

1020
      // Drain thread-local bank into the shared secondary bank (once per
1021
      // thread)
1022
#pragma omp critical(SharedSecondaryBank)
1023
      {
3,581 ✔
1024
        for (auto& secondary_site : thread_bank) {
8,997,091 ✔
1025
          simulation::shared_secondary_bank_write.thread_unsafe_append(
8,993,510 ✔
1026
            secondary_site);
1027
        }
1028
      }
1029
    } // End of transport loop over tracks in shared secondary bank
3,581 ✔
1030
    n_generation_depth++;
8,221 ✔
1031
    simulation::simulation_tracks_completed += alive_secondary;
8,221 ✔
1032
  } // End of loop over secondary generations
1033

1034
  // Reset work so that fission bank etc works correctly
1035
  calculate_work(settings::n_particles);
667 ✔
1036
}
667 ✔
1037

1038
void transport_event_based()
3,450 ✔
1039
{
1040
  int64_t remaining_work = simulation::work_per_rank;
3,450 ✔
1041
  int64_t source_offset = 0;
3,450 ✔
1042

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

1053
    // Initialize all particle histories for this subiteration
1054
    process_init_events(n_particles, source_offset);
3,450 ✔
1055
    process_transport_events();
3,450 ✔
1056
    process_death_events(n_particles);
3,450 ✔
1057

1058
    // Adjust remaining work and source offset variables
1059
    remaining_work -= n_particles;
3,450 ✔
1060
    source_offset += n_particles;
3,450 ✔
1061
  }
1062
}
3,450 ✔
1063

1064
void transport_event_based_shared_secondary()
11 ✔
1065
{
1066
  // Clear shared secondary banks from any prior use
1067
  simulation::shared_secondary_bank_read.clear();
11 ✔
1068
  simulation::shared_secondary_bank_write.clear();
11 ✔
1069

1070
  if (mpi::master) {
11 !
1071
    write_message(fmt::format(" Primary source          particles: {}",
22 !
1072
                    settings::n_particles),
1073
      6);
1074
  }
1075

1076
  simulation::progeny_per_particle.resize(simulation::work_per_rank);
11 ✔
1077
  std::fill(simulation::progeny_per_particle.begin(),
22 ✔
1078
    simulation::progeny_per_particle.end(), 0);
11 ✔
1079

1080
  // Phase 1: Transport primary particles using event-based processing and
1081
  // deposit first generation of secondaries in the shared secondary bank
1082
  int64_t remaining_work = simulation::work_per_rank;
11 ✔
1083
  int64_t source_offset = 0;
11 ✔
1084

1085
  while (remaining_work > 0) {
22 ✔
1086
    int64_t n_particles =
11 !
1087
      std::min(remaining_work, settings::max_particles_in_flight);
11 ✔
1088

1089
    process_init_events(n_particles, source_offset);
11 ✔
1090
    process_transport_events();
11 ✔
1091
    process_death_events(n_particles);
11 ✔
1092

1093
    // Collect secondaries from all particle buffers into shared bank
1094
    for (int64_t i = 0; i < n_particles; i++) {
1,661 ✔
1095
      for (auto& site : simulation::particles[i].local_secondary_bank()) {
6,159 ✔
1096
        simulation::shared_secondary_bank_write.thread_unsafe_append(site);
4,509 ✔
1097
      }
1098
      simulation::particles[i].local_secondary_bank().clear();
3,016 ✔
1099
    }
1100

1101
    remaining_work -= n_particles;
11 ✔
1102
    source_offset += n_particles;
11 ✔
1103
  }
1104

1105
  simulation::simulation_tracks_completed += settings::n_particles;
11 ✔
1106

1107
  // Phase 2: Now that the secondary bank has been populated, enter loop over
1108
  // all secondary generations
1109
  int n_generation_depth = 1;
11 ✔
1110
  int64_t alive_secondary = 1;
11 ✔
1111
  while (alive_secondary) {
415 ✔
1112

1113
    // Sort the shared secondary bank by parent ID then progeny ID to
1114
    // ensure reproducibility.
1115
    sort_bank(simulation::shared_secondary_bank_write, false);
404 ✔
1116

1117
    // Synchronize the shared secondary bank amongst all MPI ranks, such
1118
    // that each MPI rank has an approximately equal number of secondary
1119
    // tracks.
1120
    alive_secondary = synchronize_global_secondary_bank(
404 ✔
1121
      simulation::shared_secondary_bank_write);
1122

1123
    // Recalculate work for each MPI rank based on number of alive secondary
1124
    // tracks
1125
    calculate_work(alive_secondary);
404 ✔
1126

1127
    if (mpi::master) {
404 !
1128
      write_message(fmt::format(" Secondary generation {:<2}    tracks: {}",
808 !
1129
                      n_generation_depth, alive_secondary),
1130
        6);
1131
    }
1132

1133
    simulation::shared_secondary_bank_read =
404 ✔
1134
      std::move(simulation::shared_secondary_bank_write);
404 ✔
1135
    simulation::shared_secondary_bank_write = SharedArray<SourceSite>();
404 !
1136
    simulation::progeny_per_particle.resize(
404 ✔
1137
      simulation::shared_secondary_bank_read.size());
404 ✔
1138
    std::fill(simulation::progeny_per_particle.begin(),
808 ✔
1139
      simulation::progeny_per_particle.end(), 0);
404 ✔
1140

1141
    // Ensure particle buffer is large enough for this secondary generation
1142
    int64_t sec_buffer_length = std::min(
404 !
1143
      static_cast<int64_t>(simulation::shared_secondary_bank_read.size()),
404 !
1144
      settings::max_particles_in_flight);
404 ✔
1145
    if (sec_buffer_length >
404 ✔
1146
        static_cast<int64_t>(simulation::particles.size())) {
404 ✔
1147
      init_event_queues(sec_buffer_length);
35 ✔
1148
    }
1149

1150
    // Transport secondary tracks using event-based processing
1151
    int64_t sec_remaining = simulation::shared_secondary_bank_read.size();
404 ✔
1152
    int64_t sec_offset = 0;
404 ✔
1153

1154
    while (sec_remaining > 0) {
797 ✔
1155
      int64_t n_particles =
393 !
1156
        std::min(sec_remaining, settings::max_particles_in_flight);
393 ✔
1157

1158
      process_init_secondary_events(
393 ✔
1159
        n_particles, sec_offset, simulation::shared_secondary_bank_read);
1160
      process_transport_events();
393 ✔
1161
      process_death_events(n_particles);
393 ✔
1162

1163
      // Collect secondaries from all particle buffers into shared bank
1164
      for (int64_t i = 0; i < n_particles; i++) {
180,290 ✔
1165
        for (auto& site : simulation::particles[i].local_secondary_bank()) {
355,285 ✔
1166
          simulation::shared_secondary_bank_write.thread_unsafe_append(site);
175,388 ✔
1167
        }
1168
        simulation::particles[i].local_secondary_bank().clear();
226,415 ✔
1169
      }
1170

1171
      sec_remaining -= n_particles;
393 ✔
1172
      sec_offset += n_particles;
393 ✔
1173
    } // End of subiteration loop over secondary tracks
1174
    n_generation_depth++;
404 ✔
1175
    simulation::simulation_tracks_completed += alive_secondary;
404 ✔
1176
  } // End of loop over secondary generations
1177

1178
  // Reset work so that fission bank etc works correctly
1179
  calculate_work(settings::n_particles);
11 ✔
1180
}
11 ✔
1181

1182
} // namespace openmc
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc