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

openmc-dev / openmc / 23010841626

12 Mar 2026 03:50PM UTC coverage: 81.015% (-0.6%) from 81.566%
23010841626

Pull #3863

github

web-flow
Merge 954a87042 into ba94c5823
Pull Request #3863: Shared Secondary Particle Bank

16912 of 24191 branches covered (69.91%)

Branch coverage included in aggregate %.

323 of 429 new or added lines in 17 files covered. (75.29%)

577 existing lines in 39 files now uncovered.

56865 of 66875 relevant lines covered (85.03%)

32719674.03 hits per line

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

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

65
  int err = 0;
66
  while (status == 0 && err == 0) {
70,569✔
67
    err = openmc_next_batch(&status);
67,407✔
68
  }
69

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

75
int openmc_simulation_init()
3,719✔
76
{
77
  using namespace openmc;
3,719✔
78

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

83
  // Initialize nuclear data (energy limits, log grid)
84
  if (settings::run_CE) {
3,707✔
85
    initialize_data();
3,080✔
86
  }
87

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

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

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

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

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

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

128
  // If this is a restart run, load the state point data and binary source
129
  // file
130
  if (settings::restart_run) {
3,707✔
131
    load_state_point();
32✔
132
    write_message("Resuming simulation...", 6);
64✔
133
  } else {
134
    // Only initialize primary source bank for eigenvalue simulations
135
    if (settings::run_mode == RunMode::EIGENVALUE &&
3,675✔
136
        settings::solver_type == SolverType::MONTE_CARLO) {
2,108✔
137
      initialize_source();
1,934✔
138
    }
139
  }
140

141
  // Display header
142
  if (mpi::master) {
3,707✔
143
    if (settings::run_mode == RunMode::FIXED_SOURCE) {
3,467✔
144
      if (settings::solver_type == SolverType::MONTE_CARLO) {
1,497✔
145
        header("FIXED SOURCE TRANSPORT SIMULATION", 3);
1,311✔
146
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
186!
147
        header("FIXED SOURCE TRANSPORT SIMULATION (RANDOM RAY SOLVER)", 3);
186✔
148
      }
149
    } else if (settings::run_mode == RunMode::EIGENVALUE) {
1,970!
150
      if (settings::solver_type == SolverType::MONTE_CARLO) {
1,970✔
151
        header("K EIGENVALUE SIMULATION", 3);
1,820✔
152
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
150!
153
        header("K EIGENVALUE SIMULATION (RANDOM RAY SOLVER)", 3);
150✔
154
      }
155
      if (settings::verbosity >= 7)
1,970✔
156
        print_columns();
1,854✔
157
    }
158
  }
159

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

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

170
int openmc_simulation_finalize()
3,697✔
171
{
172
  using namespace openmc;
3,697✔
173

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

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

182
  // Clear material nuclide mapping
183
  for (auto& mat : model::materials) {
13,613✔
184
    mat->mat_nuclide_index_.clear();
19,832!
185
  }
186

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

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

195
#ifdef OPENMC_MPI
196
  broadcast_results();
812✔
197
#endif
198

199
  // Write tally results to tallies.out
200
  if (settings::output_tallies && mpi::master)
3,697!
201
    write_tallies();
3,271✔
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) {
3,697✔
206
    openmc_weight_windows_export();
66✔
207
  }
208

209
  // Deactivate all tallies
210
  for (auto& t : model::tallies) {
16,532✔
211
    t->active_ = false;
12,835✔
212
  }
213

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

228
  // Reset flags
229
  simulation::initialized = false;
3,697✔
230
  return 0;
3,697✔
231
}
232

233
int openmc_next_batch(int* status)
69,657✔
234
{
235
  using namespace openmc;
69,657✔
236
  using openmc::simulation::current_gen;
69,657✔
237

238
  // Make sure simulation has been initialized
239
  if (!simulation::initialized) {
69,657✔
240
    set_errmsg("Simulation has not been initialized yet.");
6✔
241
    return OPENMC_E_ALLOCATE;
6✔
242
  }
243

244
  initialize_batch();
69,651✔
245

246
  // =======================================================================
247
  // LOOP OVER GENERATIONS
248
  for (current_gen = 1; current_gen <= settings::gen_per_batch; ++current_gen) {
139,390✔
249

250
    initialize_generation();
69,749✔
251

252
    // Start timer for transport
253
    simulation::time_transport.start();
69,749✔
254

255
    // Transport loop
256
    if (settings::event_based) {
69,749!
NEW
257
      if (settings::use_shared_secondary_bank) {
×
NEW
258
        transport_event_based_shared_secondary();
×
259
      } else {
NEW
260
        transport_event_based();
×
261
      }
262
    } else {
263
      if (settings::use_shared_secondary_bank) {
69,749✔
264
        transport_history_based_shared_secondary();
335✔
265
      } else {
266
        transport_history_based();
69,414✔
267
      }
268
    }
269

270
    // Accumulate time for transport
271
    simulation::time_transport.stop();
69,739✔
272

273
    finalize_generation();
69,739✔
274
  }
275

276
  finalize_batch();
69,641✔
277

278
  // Check simulation ending criteria
279
  if (status) {
69,641!
280
    if (simulation::current_batch >= settings::n_max_batches) {
69,641✔
281
      *status = STATUS_EXIT_MAX_BATCH;
3,272✔
282
    } else if (simulation::satisfy_triggers) {
66,369✔
283
      *status = STATUS_EXIT_ON_TRIGGER;
46✔
284
    } else {
285
      *status = STATUS_EXIT_NORMAL;
66,323✔
286
    }
287
  }
288
  return 0;
289
}
290

291
bool openmc_is_statepoint_batch()
1,710✔
292
{
293
  using namespace openmc;
1,710✔
294
  using openmc::simulation::current_gen;
1,710✔
295

296
  if (!simulation::initialized)
1,710!
297
    return false;
298
  else
299
    return contains(settings::statepoint_batch, simulation::current_batch);
3,420✔
300
}
301

302
namespace openmc {
303

304
//==============================================================================
305
// Global variables
306
//==============================================================================
307

308
namespace simulation {
309

310
int ct_current_file;
311
int current_batch;
312
int current_gen;
313
bool initialized {false};
314
double keff {1.0};
315
double keff_std;
316
double k_col_abs {0.0};
317
double k_col_tra {0.0};
318
double k_abs_tra {0.0};
319
double log_spacing;
320
int n_lost_particles {0};
321
bool need_depletion_rx {false};
322
int restart_batch;
323
bool satisfy_triggers {false};
324
int ssw_current_file;
325
int total_gen {0};
326
double total_weight;
327
int64_t work_per_rank;
328

329
const RegularMesh* entropy_mesh {nullptr};
330
const RegularMesh* ufs_mesh {nullptr};
331

332
vector<double> k_generation;
333
vector<int64_t> work_index;
334

335
int64_t simulation_tracks_completed {0};
336

337
} // namespace simulation
338

339
//==============================================================================
340
// Non-member functions
341
//==============================================================================
342

343
void allocate_banks()
3,707✔
344
{
345
  if (settings::run_mode == RunMode::EIGENVALUE &&
3,707✔
346
      settings::solver_type == SolverType::MONTE_CARLO) {
2,140✔
347
    // Allocate source bank
348
    simulation::source_bank.resize(simulation::work_per_rank);
1,966✔
349

350
    // Allocate fission bank
351
    init_fission_bank(3 * simulation::work_per_rank);
1,966✔
352

353
    // Allocate IFP bank
354
    if (settings::ifp_on) {
1,966✔
355
      resize_simulation_ifp_banks();
38✔
356
    }
357
  }
358

359
  if (settings::surf_source_write) {
3,707✔
360
    // Allocate surface source bank
361
    simulation::surf_source_bank.reserve(settings::ssw_max_particles);
542✔
362
  }
363

364
  if (settings::collision_track) {
3,707✔
365
    // Allocate collision track bank
366
    collision_track_reserve_bank();
73✔
367
  }
368
}
3,707✔
369

370
void initialize_batch()
79,391✔
371
{
372
  // Increment current batch
373
  ++simulation::current_batch;
79,391✔
374
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
79,391✔
375
    if (settings::solver_type == SolverType::RANDOM_RAY &&
33,111✔
376
        simulation::current_batch < settings::n_inactive + 1) {
6,370✔
377
      write_message(
7,630✔
378
        6, "Simulating batch {:<4} (inactive)", simulation::current_batch);
379
    } else {
380
      write_message(6, "Simulating batch {}", simulation::current_batch);
58,592✔
381
    }
382
  }
383

384
  // Reset total starting particle weight used for normalizing tallies
385
  simulation::total_weight = 0.0;
79,391✔
386

387
  // Determine if this batch is the first inactive or active batch.
388
  bool first_inactive = false;
79,391✔
389
  bool first_active = false;
79,391✔
390
  if (!settings::restart_run) {
79,391✔
391
    first_inactive = settings::n_inactive > 0 && simulation::current_batch == 1;
79,308✔
392
    first_active = simulation::current_batch == settings::n_inactive + 1;
79,308✔
393
  } else if (simulation::current_batch == simulation::restart_batch + 1) {
83✔
394
    first_inactive = simulation::restart_batch < settings::n_inactive;
26✔
395
    first_active = !first_inactive;
26✔
396
  }
397

398
  // Manage active/inactive timers and activate tallies if necessary.
399
  if (first_inactive) {
79,334✔
400
    simulation::time_inactive.start();
1,785✔
401
  } else if (first_active) {
77,606✔
402
    simulation::time_inactive.stop();
3,683✔
403
    simulation::time_active.start();
3,683✔
404
    for (auto& t : model::tallies) {
16,506✔
405
      t->active_ = true;
12,823✔
406
    }
407
  }
408

409
  // Add user tallies to active tallies list
410
  setup_active_tallies();
79,391✔
411
}
79,391✔
412

413
void finalize_batch()
79,381✔
414
{
415
  // Reduce tallies onto master process and accumulate
416
  simulation::time_tallies.start();
79,381✔
417
  accumulate_tallies();
79,381✔
418
  simulation::time_tallies.stop();
79,381✔
419

420
  // update weight windows if needed
421
  for (const auto& wwg : variance_reduction::weight_windows_generators) {
80,481✔
422
    wwg->update();
1,100✔
423
  }
424

425
  // Reset global tally results
426
  if (simulation::current_batch <= settings::n_inactive) {
79,381✔
427
    simulation::global_tallies.fill(0.0);
15,011✔
428
    simulation::n_realizations = 0;
15,011✔
429
  }
430

431
  // Check_triggers
432
  if (mpi::master)
79,381✔
433
    check_triggers();
74,755✔
434
#ifdef OPENMC_MPI
435
  MPI_Bcast(&simulation::satisfy_triggers, 1, MPI_C_BOOL, 0, mpi::intracomm);
17,051✔
436
#endif
437
  if (simulation::satisfy_triggers ||
79,381✔
438
      (settings::trigger_on &&
1,308✔
439
        simulation::current_batch == settings::n_max_batches)) {
1,308✔
440
    settings::statepoint_batch.insert(simulation::current_batch);
71✔
441
  }
442

443
  // Write out state point if it's been specified for this batch and is not
444
  // a CMFD run instance
445
  if (contains(settings::statepoint_batch, simulation::current_batch) &&
158,762✔
446
      !settings::cmfd_run) {
3,809✔
447
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
7,321✔
448
        settings::source_write && !settings::source_separate) {
6,930✔
449
      bool b = (settings::run_mode == RunMode::EIGENVALUE);
3,183✔
450
      openmc_statepoint_write(nullptr, &b);
3,183✔
451
    } else {
452
      bool b = false;
530✔
453
      openmc_statepoint_write(nullptr, &b);
530✔
454
    }
455
  }
456

457
  if (settings::run_mode == RunMode::EIGENVALUE) {
79,381✔
458
    // Write out a separate source point if it's been specified for this batch
459
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
48,445✔
460
        settings::source_write && settings::source_separate) {
48,271✔
461

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

471
    // Write a continously-overwritten source point if requested.
472
    if (settings::source_latest) {
46,280✔
473
      auto filename = settings::path_output + "source";
70✔
474
      span<SourceSite> bankspan(simulation::source_bank);
70✔
475
      write_source_point(filename, bankspan, simulation::work_index,
140✔
476
        settings::source_mcpl_write);
477
    }
70✔
478
  }
479

480
  // Write out surface source if requested.
481
  if (settings::surf_source_write &&
79,381✔
482
      simulation::ssw_current_file <= settings::ssw_max_files) {
5,144✔
483
    bool last_batch = (simulation::current_batch == settings::n_batches);
1,004✔
484
    if (simulation::surf_source_bank.full() || last_batch) {
1,004✔
485
      // Determine appropriate filename
486
      auto filename = fmt::format("{}surface_source.{}", settings::path_output,
560✔
487
        simulation::current_batch);
560✔
488
      if (settings::ssw_max_files == 1 ||
560✔
489
          (simulation::ssw_current_file == 1 && last_batch)) {
30!
490
        filename = settings::path_output + "surface_source";
530✔
491
      }
492

493
      // Get span of source bank and calculate parallel index vector
494
      auto surf_work_index = mpi::calculate_parallel_index_vector(
560✔
495
        simulation::surf_source_bank.size());
560✔
496
      span<SourceSite> surfbankspan(simulation::surf_source_bank.begin(),
560✔
497
        simulation::surf_source_bank.size());
560✔
498

499
      // Write surface source file
500
      write_source_point(
560✔
501
        filename, surfbankspan, surf_work_index, settings::surf_mcpl_write);
502

503
      // Reset surface source bank and increment counter
504
      simulation::surf_source_bank.clear();
560✔
505
      if (!last_batch && settings::ssw_max_files >= 1) {
560!
506
        simulation::surf_source_bank.reserve(settings::ssw_max_particles);
458✔
507
      }
508
      ++simulation::ssw_current_file;
560✔
509
    }
560✔
510
  }
511
  // Write collision track file if requested
512
  if (settings::collision_track) {
79,381✔
513
    collision_track_flush_bank();
293✔
514
  }
515
}
79,381✔
516

517
void initialize_generation()
79,489✔
518
{
519
  if (settings::run_mode == RunMode::EIGENVALUE) {
79,489✔
520
    // Clear out the fission bank
521
    simulation::fission_bank.resize(0);
46,378✔
522

523
    // Count source sites if using uniform fission source weighting
524
    if (settings::ufs_on)
46,378✔
525
      ufs_count_sites();
70✔
526

527
    // Store current value of tracklength k
528
    simulation::keff_generation = simulation::global_tallies(
46,378✔
529
      GlobalTally::K_TRACKLENGTH, TallyResult::VALUE);
530
  }
531
}
79,489✔
532

533
void finalize_generation()
79,479✔
534
{
535
  auto& gt = simulation::global_tallies;
79,479✔
536

537
  // Update global tallies with the accumulation variables
538
  if (settings::run_mode == RunMode::EIGENVALUE) {
79,479✔
539
    gt(GlobalTally::K_COLLISION, TallyResult::VALUE) += global_tally_collision;
46,378✔
540
    gt(GlobalTally::K_ABSORPTION, TallyResult::VALUE) +=
46,378✔
541
      global_tally_absorption;
542
    gt(GlobalTally::K_TRACKLENGTH, TallyResult::VALUE) +=
46,378✔
543
      global_tally_tracklength;
544
  }
545
  gt(GlobalTally::LEAKAGE, TallyResult::VALUE) += global_tally_leakage;
79,479✔
546

547
  // reset tallies
548
  if (settings::run_mode == RunMode::EIGENVALUE) {
79,479✔
549
    global_tally_collision = 0.0;
46,378✔
550
    global_tally_absorption = 0.0;
46,378✔
551
    global_tally_tracklength = 0.0;
46,378✔
552
  }
553
  global_tally_leakage = 0.0;
79,479✔
554

555
  if (settings::run_mode == RunMode::EIGENVALUE &&
79,479✔
556
      settings::solver_type == SolverType::MONTE_CARLO) {
46,378✔
557
    // If using shared memory, stable sort the fission bank (by parent IDs)
558
    // so as to allow for reproducibility regardless of which order particles
559
    // are run in.
560
    sort_bank(simulation::fission_bank, true);
43,008✔
561

562
    // Distribute fission bank across processors evenly
563
    synchronize_bank();
43,008✔
564
  }
565

566
  if (settings::run_mode == RunMode::EIGENVALUE) {
79,479✔
567

568
    // Calculate shannon entropy
569
    if (settings::entropy_on &&
46,378✔
570
        settings::solver_type == SolverType::MONTE_CARLO)
7,550✔
571
      shannon_entropy();
4,180✔
572

573
    // Collect results and statistics
574
    calculate_generation_keff();
46,378✔
575
    calculate_average_keff();
46,378✔
576

577
    // Write generation output
578
    if (mpi::master && settings::verbosity >= 7) {
46,378✔
579
      print_generation();
41,418✔
580
    }
581
  }
582
}
79,479✔
583

584
void sample_particle(Particle& p, int64_t index_source)
92,145,562✔
585
{
586
  // Sample a particle from the source bank
587
  if (settings::run_mode == RunMode::EIGENVALUE) {
92,145,562✔
588
    p.from_source(&simulation::source_bank[index_source - 1]);
77,205,200✔
589
  } else if (settings::run_mode == RunMode::FIXED_SOURCE) {
14,940,362!
590
    // initialize random number seed
591
    int64_t id = compute_transport_seed(compute_particle_id(index_source));
14,940,362✔
592
    uint64_t seed = init_seed(id, STREAM_SOURCE);
14,940,362✔
593
    // sample from external source distribution or custom library then set
594
    auto site = sample_external_source(&seed);
14,940,362✔
595
    p.from_source(&site);
14,940,358✔
596
  }
597
}
92,145,558✔
598

599
void initialize_history(Particle& p, int64_t index_source, bool is_secondary)
106,740,338✔
600
{
601
  // Note: index_source is 1-based (first particle = 1), but current_work() is
602
  // stored as 0-based for direct use as an array index into
603
  // progeny_per_particle, source_bank, ifp banks, etc.
604
  if (!is_secondary) {
106,740,338✔
605
    sample_particle(p, index_source);
92,145,562✔
606
  }
607

608
  p.current_work() = index_source - 1;
106,740,334✔
609

610
  // set identifier for particle
611
  p.id() = compute_particle_id(index_source);
106,740,334✔
612

613
  // set progeny count to zero
614
  p.n_progeny() = 0;
106,740,334✔
615

616
  // Reset particle event counter
617
  p.n_event() = 0;
106,740,334✔
618

619
  // Initialize track counter (1 for this primary/secondary track)
620
  p.n_tracks() = 1;
106,740,334✔
621

622
  // Reset split counter
623
  p.n_split() = 0;
106,740,334✔
624

625
  // Reset weight window ratio
626
  p.ww_factor() = 0.0;
106,740,334✔
627

628
  // set particle history start weight
629
  p.wgt_born() = p.wgt();
106,740,334✔
630

631
  // Reset pulse_height_storage
632
  std::fill(p.pht_storage().begin(), p.pht_storage().end(), 0);
106,740,334✔
633

634
  // set random number seed
635
  int64_t particle_seed = compute_transport_seed(p.id());
106,740,334✔
636
  init_particle_seeds(particle_seed, p.seeds());
106,740,334✔
637

638
  // set particle trace
639
  p.trace() = false;
106,740,334✔
640
  if (simulation::current_batch == settings::trace_batch &&
106,746,334✔
641
      simulation::current_gen == settings::trace_gen &&
106,740,334!
642
      p.id() == settings::trace_particle)
6,000✔
643
    p.trace() = true;
6✔
644

645
  // Set particle track.
646
  p.write_track() = check_track_criteria(p);
106,740,334✔
647

648
  // Set the particle's initial weight window value.
649
  if (!is_secondary) {
106,740,334✔
650
    p.wgt_ww_born() = -1.0;
92,145,558✔
651
    apply_weight_windows(p);
92,145,558✔
652
  }
653

654
  // Display message if high verbosity or trace is on
655
  if (settings::verbosity >= 9 || p.trace()) {
106,740,334!
656
    write_message("Simulating Particle {}", p.id());
12✔
657
  }
658

659
  // Add particle's starting weight to count for normalizing tallies later
660
  if (!is_secondary) {
106,740,334✔
661
#pragma omp atomic
15,369,164✔
662
    simulation::total_weight += p.wgt();
92,145,558✔
663
  }
664

665
  // Force calculation of cross-sections by setting last energy to zero
666
  if (settings::run_CE) {
106,740,334✔
667
    p.invalidate_neutron_xs();
44,265,490✔
668
  }
669

670
  // Prepare to write out particle track.
671
  if (p.write_track())
106,740,334✔
672
    add_particle_track(p);
474✔
673
}
106,740,334✔
674

675
int overall_generation()
106,362,467✔
676
{
677
  using namespace simulation;
106,362,467✔
678
  return settings::gen_per_batch * (current_batch - 1) + current_gen;
106,362,467✔
679
}
680

681
int64_t compute_particle_id(int64_t index_source)
121,680,851✔
682
{
683
  if (settings::use_shared_secondary_bank) {
121,680,851✔
684
    return simulation::work_index[mpi::rank] + index_source +
15,449,206✔
685
           simulation::simulation_tracks_completed;
15,449,206✔
686
  } else {
687
    return simulation::work_index[mpi::rank] + index_source;
106,231,645✔
688
  }
689
}
690

691
int64_t compute_transport_seed(int64_t particle_id)
121,680,875✔
692
{
693
  if (settings::use_shared_secondary_bank) {
121,680,875✔
694
    return particle_id;
695
  } else {
696
    return (simulation::total_gen + overall_generation() - 1) *
106,231,663✔
697
             settings::n_particles +
698
           particle_id;
106,231,663✔
699
  }
700
}
701

702
void calculate_work(int64_t n_particles)
8,334✔
703
{
704
  // Determine minimum amount of particles to simulate on each processor
705
  int64_t min_work = n_particles / mpi::n_procs;
8,334✔
706

707
  // Determine number of processors that have one extra particle
708
  int64_t remainder = n_particles % mpi::n_procs;
8,334✔
709

710
  int64_t i_bank = 0;
8,334✔
711
  simulation::work_index.resize(mpi::n_procs + 1);
8,334✔
712
  simulation::work_index[0] = 0;
8,334✔
713
  for (int i = 0; i < mpi::n_procs; ++i) {
18,030✔
714
    // Number of particles for rank i
715
    int64_t work_i = i < remainder ? min_work + 1 : min_work;
9,696✔
716

717
    // Set number of particles
718
    if (mpi::rank == i)
9,696✔
719
      simulation::work_per_rank = work_i;
8,334✔
720

721
    // Set index into source bank for rank i
722
    i_bank += work_i;
9,696✔
723
    simulation::work_index[i + 1] = i_bank;
9,696✔
724
  }
725
}
8,334✔
726

727
void initialize_data()
3,104✔
728
{
729
  // Determine minimum/maximum energy for incident neutron/photon data
730
  data::energy_max = {INFTY, INFTY, INFTY, INFTY};
3,104✔
731
  data::energy_min = {0.0, 0.0, 0.0, 0.0};
3,104✔
732

733
  for (const auto& nuc : data::nuclides) {
18,625✔
734
    if (nuc->grid_.size() >= 1) {
15,521!
735
      int neutron = ParticleType::neutron().transport_index();
15,521✔
736
      data::energy_min[neutron] =
15,521✔
737
        std::max(data::energy_min[neutron], nuc->grid_[0].energy.front());
18,350✔
738
      data::energy_max[neutron] =
15,521✔
739
        std::min(data::energy_max[neutron], nuc->grid_[0].energy.back());
19,074✔
740
    }
741
  }
742

743
  if (settings::photon_transport) {
3,104✔
744
    for (const auto& elem : data::elements) {
693✔
745
      if (elem->energy_.size() >= 1) {
497!
746
        int photon = ParticleType::photon().transport_index();
497✔
747
        int n = elem->energy_.size();
497✔
748
        data::energy_min[photon] =
994✔
749
          std::max(data::energy_min[photon], std::exp(elem->energy_(1)));
820✔
750
        data::energy_max[photon] =
497✔
751
          std::min(data::energy_max[photon], std::exp(elem->energy_(n - 1)));
693✔
752
      }
753
    }
754

755
    if (settings::electron_treatment == ElectronTreatment::TTB) {
196✔
756
      // Determine if minimum/maximum energy for bremsstrahlung is greater/less
757
      // than the current minimum/maximum
758
      if (data::ttb_e_grid.size() >= 1) {
177!
759
        int photon = ParticleType::photon().transport_index();
177✔
760
        int electron = ParticleType::electron().transport_index();
177✔
761
        int positron = ParticleType::positron().transport_index();
177✔
762
        int n_e = data::ttb_e_grid.size();
177✔
763

764
        const std::vector<int> charged = {electron, positron};
177✔
765
        for (auto t : charged) {
531✔
766
          data::energy_min[t] = std::exp(data::ttb_e_grid(1));
354✔
767
          data::energy_max[t] = std::exp(data::ttb_e_grid(n_e - 1));
354✔
768
        }
769

770
        data::energy_min[photon] =
354✔
771
          std::max(data::energy_min[photon], data::energy_min[electron]);
354!
772

773
        data::energy_max[photon] =
354✔
774
          std::min(data::energy_max[photon], data::energy_max[electron]);
354!
775
      }
177✔
776
    }
777
  }
778

779
  // Show which nuclide results in lowest energy for neutron transport
780
  for (const auto& nuc : data::nuclides) {
3,856✔
781
    // If a nuclide is present in a material that's not used in the model, its
782
    // grid has not been allocated
783
    if (nuc->grid_.size() > 0) {
3,581!
784
      double max_E = nuc->grid_[0].energy.back();
3,581✔
785
      int neutron = ParticleType::neutron().transport_index();
3,581✔
786
      if (max_E == data::energy_max[neutron]) {
3,581✔
787
        write_message(7, "Maximum neutron transport energy: {} eV for {}",
2,829✔
788
          data::energy_max[neutron], nuc->name_);
2,829✔
789
        if (mpi::master && data::energy_max[neutron] < 20.0e6) {
2,829!
790
          warning("Maximum neutron energy is below 20 MeV. This may bias "
×
791
                  "the results.");
792
        }
793
        break;
794
      }
795
    }
796
  }
797

798
  // Set up logarithmic grid for nuclides
799
  for (auto& nuc : data::nuclides) {
18,625✔
800
    nuc->init_grid();
15,521✔
801
  }
802
  int neutron = ParticleType::neutron().transport_index();
3,104✔
803
  simulation::log_spacing =
6,208✔
804
    std::log(data::energy_max[neutron] / data::energy_min[neutron]) /
3,104✔
805
    settings::n_log_bins;
806
}
3,104✔
807

808
#ifdef OPENMC_MPI
809
void broadcast_results()
812✔
810
{
811
  // Broadcast tally results so that each process has access to results
812
  for (auto& t : model::tallies) {
4,102✔
813
    // Create a new datatype that consists of all values for a given filter
814
    // bin and then use that to broadcast. This is done to minimize the
815
    // chance of the 'count' argument of MPI_BCAST exceeding 2**31
816
    auto& results = t->results_;
3,290✔
817

818
    auto shape = results.shape();
3,290✔
819
    int count_per_filter = shape[1] * shape[2];
3,290✔
820
    MPI_Datatype result_block;
3,290✔
821
    MPI_Type_contiguous(count_per_filter, MPI_DOUBLE, &result_block);
3,290✔
822
    MPI_Type_commit(&result_block);
3,290✔
823
    MPI_Bcast(results.data(), shape[0], result_block, 0, mpi::intracomm);
3,290✔
824
    MPI_Type_free(&result_block);
3,290✔
825
  }
3,290✔
826

827
  // Also broadcast global tally results
828
  auto& gt = simulation::global_tallies;
812✔
829
  MPI_Bcast(gt.data(), gt.size(), MPI_DOUBLE, 0, mpi::intracomm);
812✔
830

831
  // These guys are needed so that non-master processes can calculate the
832
  // combined estimate of k-effective
833
  double temp[] {
812✔
834
    simulation::k_col_abs, simulation::k_col_tra, simulation::k_abs_tra};
812✔
835
  MPI_Bcast(temp, 3, MPI_DOUBLE, 0, mpi::intracomm);
812✔
836
  simulation::k_col_abs = temp[0];
812✔
837
  simulation::k_col_tra = temp[1];
812✔
838
  simulation::k_abs_tra = temp[2];
812✔
839
}
812✔
840

841
#endif
842

843
void free_memory_simulation()
4,294✔
844
{
845
  simulation::k_generation.clear();
4,294✔
846
  simulation::entropy.clear();
4,294✔
847
}
4,294✔
848

849
void transport_history_based_single_particle(Particle& p)
106,740,358✔
850
{
851
  while (p.alive()) {
2,147,483,647✔
852
    p.event_calculate_xs();
2,147,483,647✔
853
    if (p.alive()) {
2,147,483,647!
854
      p.event_advance();
2,147,483,647✔
855
    }
856
    if (p.alive()) {
2,147,483,647✔
857
      if (p.collision_distance() > p.boundary().distance()) {
2,147,483,647✔
858
        p.event_cross_surface();
804,599,078✔
859
      } else if (p.alive()) {
1,674,386,060✔
860
        p.event_collide();
1,674,386,060✔
861
      }
862
    }
863
    p.event_check_limit_and_revive();
2,147,483,647✔
864
  }
865
  p.event_death();
106,740,352✔
866
}
106,740,352✔
867

868
void transport_history_based()
69,414✔
869
{
870
#pragma omp parallel for schedule(runtime)
11,009✔
871
  for (int64_t i_work = 1; i_work <= simulation::work_per_rank; ++i_work) {
76,489,625✔
872
    Particle p;
76,431,229✔
873
    initialize_history(p, i_work, false);
76,431,229✔
874
    transport_history_based_single_particle(p);
76,431,225✔
875
  }
76,431,220✔
876
}
69,405✔
877

878
// The shared secondary bank transport algorithm works in two phases. In the
879
// first phase, all primary particles are sampled then transported, and their
880
// secondary particles are deposited into a shared secondary bank. The second
881
// phase occurs in a loop, where all secondary tracks in the shared secondary
882
// bank are transported. Any secondary particles generated during this phase are
883
// deposited back into the shared secondary bank. The shared secondary bank is
884
// sorted for consistent ordering and load balanced across MPI ranks. This loop
885
// continues until there are no more secondary tracks left to transport.
886
void transport_history_based_shared_secondary()
335✔
887
{
888
  // Clear shared secondary banks from any prior use
889
  simulation::shared_secondary_bank_read.clear();
335✔
890
  simulation::shared_secondary_bank_write.clear();
335✔
891

892
  if (mpi::master) {
335✔
893
    write_message(fmt::format(" Primogenitor            particles: {}",
624✔
894
                    settings::n_particles),
895
      6);
896
  }
897

898
  simulation::progeny_per_particle.resize(simulation::work_per_rank);
335✔
899
  std::fill(simulation::progeny_per_particle.begin(),
670✔
900
    simulation::progeny_per_particle.end(), 0);
335✔
901

902
  // Phase 1: Transport primary particles and deposit first generation of
903
  // secondaries in the shared secondary bank
904
#pragma omp parallel
52✔
905
  {
283✔
906
    vector<SourceSite> thread_bank;
283✔
907

908
#pragma omp for schedule(runtime)
909
    for (int64_t i = 1; i <= simulation::work_per_rank; i++) {
356,283✔
910
      Particle p;
356,000✔
911
      initialize_history(p, i, false);
356,000✔
912
      transport_history_based_single_particle(p);
356,000✔
913
      for (auto& site : p.local_secondary_bank()) {
1,094,610✔
914
        thread_bank.push_back(site);
738,610✔
915
      }
916
    }
356,000✔
917

918
    // Drain thread-local bank into the shared secondary bank (once per thread)
919
#pragma omp critical(shared_secondary_bank)
920
    {
283✔
921
      for (auto& site : thread_bank) {
738,893✔
922
        simulation::shared_secondary_bank_write.thread_unsafe_append(site);
738,610✔
923
      }
924
    }
925
  }
926

927
  simulation::simulation_tracks_completed += settings::n_particles;
335✔
928

929
  // Phase 2: Now that the secondary bank has been populated, enter loop over
930
  // all secondary generations
931
  int n_generation_depth = 1;
335✔
932
  int64_t alive_secondary = 1;
335✔
933
  while (alive_secondary) {
4,627✔
934

935
    // Sort the shared secondary bank by parent ID then progeny ID to
936
    // ensure reproducibility.
937
    sort_bank(simulation::shared_secondary_bank_write, false);
4,292✔
938

939
    // Synchronize the shared secondary bank amongst all MPI ranks, such
940
    // that each MPI rank has an approximately equal number of secondary
941
    // tracks. Also reports the total number of secondaries alive across
942
    // all MPI ranks.
943
    alive_secondary = synchronize_global_secondary_bank(
4,292✔
944
      simulation::shared_secondary_bank_write);
945

946
    // Recalculate work for each MPI rank based on number of alive secondary
947
    // tracks
948
    calculate_work(alive_secondary);
4,292✔
949

950
    // Display the number of secondary tracks in this generation. This
951
    // is useful for user monitoring so as to see if the secondary population is
952
    // exploding and to determine how many generations of secondaries are being
953
    // transported.
954
    if (mpi::master) {
4,292✔
955
      write_message(fmt::format(" Secondary generation {:<2}    tracks: {}",
7,748✔
956
                      n_generation_depth, alive_secondary),
957
        6);
958
    }
959

960
    simulation::shared_secondary_bank_read =
4,292✔
961
      std::move(simulation::shared_secondary_bank_write);
4,292✔
962
    simulation::shared_secondary_bank_write = SharedArray<SourceSite>();
4,292!
963
    simulation::progeny_per_particle.resize(
4,292✔
964
      simulation::shared_secondary_bank_read.size());
4,292✔
965
    std::fill(simulation::progeny_per_particle.begin(),
8,584✔
966
      simulation::progeny_per_particle.end(), 0);
4,292✔
967

968
    // Transport all secondary tracks from the shared secondary bank
969
#pragma omp parallel
639✔
970
    {
3,653✔
971
      vector<SourceSite> thread_bank;
3,653✔
972

973
#pragma omp for schedule(runtime)
974
      for (int64_t i = 1; i <= simulation::shared_secondary_bank_read.size();
12,165,653✔
975
           i++) {
976
        Particle p;
12,162,000✔
977
        initialize_history(p, i, true);
12,162,000✔
978
        SourceSite& site = simulation::shared_secondary_bank_read[i - 1];
12,162,000✔
979
        p.event_revive_from_secondary(site);
12,162,000✔
980
        transport_history_based_single_particle(p);
12,162,000✔
981
        for (auto& secondary_site : p.local_secondary_bank()) {
23,585,390✔
982
          thread_bank.push_back(secondary_site);
11,423,390✔
983
        }
984
      }
12,162,000✔
985

986
      // Drain thread-local bank into the shared secondary bank (once per
987
      // thread)
988
#pragma omp critical(shared_secondary_bank)
989
      {
3,653✔
990
        for (auto& secondary_site : thread_bank) {
11,427,043✔
991
          simulation::shared_secondary_bank_write.thread_unsafe_append(
11,423,390✔
992
            secondary_site);
993
        }
994
      }
995
    } // End of transport loop over tracks in shared secondary bank
3,653✔
996
    n_generation_depth++;
4,292✔
997
    simulation::simulation_tracks_completed += alive_secondary;
4,292✔
998
  } // End of loop over secondary generations
999

1000
  // Reset work so that fission bank etc works correctly
1001
  calculate_work(settings::n_particles);
335✔
1002
}
335✔
1003

UNCOV
1004
void transport_event_based()
×
1005
{
UNCOV
1006
  int64_t remaining_work = simulation::work_per_rank;
×
UNCOV
1007
  int64_t source_offset = 0;
×
1008

1009
  // To cap the total amount of memory used to store particle object data, the
1010
  // number of particles in flight at any point in time can bet set. In the case
1011
  // that the maximum in flight particle count is lower than the total number
1012
  // of particles that need to be run this iteration, the event-based transport
1013
  // loop is executed multiple times until all particles have been completed.
UNCOV
1014
  while (remaining_work > 0) {
×
1015
    // Figure out # of particles to run for this subiteration
UNCOV
1016
    int64_t n_particles =
×
UNCOV
1017
      std::min(remaining_work, settings::max_particles_in_flight);
×
1018

1019
    // Initialize all particle histories for this subiteration
UNCOV
1020
    process_init_events(n_particles, source_offset);
×
NEW
1021
    process_transport_events();
×
NEW
1022
    process_death_events(n_particles);
×
1023

1024
    // Adjust remaining work and source offset variables
NEW
1025
    remaining_work -= n_particles;
×
NEW
1026
    source_offset += n_particles;
×
1027
  }
NEW
1028
}
×
1029

NEW
1030
void transport_event_based_shared_secondary()
×
1031
{
1032
  // Clear shared secondary banks from any prior use
NEW
1033
  simulation::shared_secondary_bank_read.clear();
×
NEW
1034
  simulation::shared_secondary_bank_write.clear();
×
1035

NEW
1036
  if (mpi::master) {
×
NEW
1037
    write_message(fmt::format(" Primogenitor            particles: {}",
×
1038
                    settings::n_particles),
1039
      6);
1040
  }
1041

NEW
1042
  simulation::progeny_per_particle.resize(simulation::work_per_rank);
×
NEW
1043
  std::fill(simulation::progeny_per_particle.begin(),
×
NEW
1044
    simulation::progeny_per_particle.end(), 0);
×
1045

1046
  // Phase 1: Transport primary particles using event-based processing and
1047
  // deposit first generation of secondaries in the shared secondary bank
NEW
1048
  int64_t remaining_work = simulation::work_per_rank;
×
NEW
1049
  int64_t source_offset = 0;
×
1050

NEW
1051
  while (remaining_work > 0) {
×
NEW
1052
    int64_t n_particles =
×
NEW
1053
      std::min(remaining_work, settings::max_particles_in_flight);
×
1054

NEW
1055
    process_init_events(n_particles, source_offset);
×
NEW
1056
    process_transport_events();
×
UNCOV
1057
    process_death_events(n_particles);
×
1058

1059
    // Collect secondaries from all particle buffers into shared bank
NEW
1060
    for (int64_t i = 0; i < n_particles; i++) {
×
NEW
1061
      for (auto& site : simulation::particles[i].local_secondary_bank()) {
×
NEW
1062
        simulation::shared_secondary_bank_write.thread_unsafe_append(site);
×
1063
      }
NEW
1064
      simulation::particles[i].local_secondary_bank().clear();
×
1065
    }
1066

UNCOV
1067
    remaining_work -= n_particles;
×
UNCOV
1068
    source_offset += n_particles;
×
1069
  }
1070

NEW
1071
  simulation::simulation_tracks_completed += settings::n_particles;
×
1072

1073
  // Phase 2: Now that the secondary bank has been populated, enter loop over
1074
  // all secondary generations
NEW
1075
  int n_generation_depth = 1;
×
NEW
1076
  int64_t alive_secondary = 1;
×
NEW
1077
  while (alive_secondary) {
×
1078

1079
    // Sort the shared secondary bank by parent ID then progeny ID to
1080
    // ensure reproducibility.
NEW
1081
    sort_bank(simulation::shared_secondary_bank_write, false);
×
1082

1083
    // Synchronize the shared secondary bank amongst all MPI ranks, such
1084
    // that each MPI rank has an approximately equal number of secondary
1085
    // tracks.
NEW
1086
    alive_secondary = synchronize_global_secondary_bank(
×
1087
      simulation::shared_secondary_bank_write);
1088

1089
    // Recalculate work for each MPI rank based on number of alive secondary
1090
    // tracks
NEW
1091
    calculate_work(alive_secondary);
×
1092

NEW
1093
    if (mpi::master) {
×
NEW
1094
      write_message(fmt::format(" Secondary generation {:<2}    tracks: {}",
×
1095
                      n_generation_depth, alive_secondary),
1096
        6);
1097
    }
1098

NEW
1099
    simulation::shared_secondary_bank_read =
×
NEW
1100
      std::move(simulation::shared_secondary_bank_write);
×
NEW
1101
    simulation::shared_secondary_bank_write = SharedArray<SourceSite>();
×
NEW
1102
    simulation::progeny_per_particle.resize(
×
NEW
1103
      simulation::shared_secondary_bank_read.size());
×
NEW
1104
    std::fill(simulation::progeny_per_particle.begin(),
×
NEW
1105
      simulation::progeny_per_particle.end(), 0);
×
1106

1107
    // Ensure particle buffer is large enough for this secondary generation
NEW
1108
    int64_t sec_buffer_length = std::min(
×
NEW
1109
      static_cast<int64_t>(simulation::shared_secondary_bank_read.size()),
×
NEW
1110
      settings::max_particles_in_flight);
×
NEW
1111
    if (sec_buffer_length >
×
NEW
1112
        static_cast<int64_t>(simulation::particles.size())) {
×
NEW
1113
      init_event_queues(sec_buffer_length);
×
1114
    }
1115

1116
    // Transport secondary tracks using event-based processing
NEW
1117
    int64_t sec_remaining = simulation::shared_secondary_bank_read.size();
×
NEW
1118
    int64_t sec_offset = 0;
×
1119

NEW
1120
    while (sec_remaining > 0) {
×
NEW
1121
      int64_t n_particles =
×
NEW
1122
        std::min(sec_remaining, settings::max_particles_in_flight);
×
1123

NEW
1124
      process_init_secondary_events(
×
1125
        n_particles, sec_offset, simulation::shared_secondary_bank_read);
NEW
1126
      process_transport_events();
×
NEW
1127
      process_death_events(n_particles);
×
1128

1129
      // Collect secondaries from all particle buffers into shared bank
NEW
1130
      for (int64_t i = 0; i < n_particles; i++) {
×
NEW
1131
        for (auto& site : simulation::particles[i].local_secondary_bank()) {
×
NEW
1132
          simulation::shared_secondary_bank_write.thread_unsafe_append(site);
×
1133
        }
NEW
1134
        simulation::particles[i].local_secondary_bank().clear();
×
1135
      }
1136

NEW
1137
      sec_remaining -= n_particles;
×
NEW
1138
      sec_offset += n_particles;
×
1139
    } // End of subiteration loop over secondary tracks
NEW
1140
    n_generation_depth++;
×
NEW
1141
    simulation::simulation_tracks_completed += alive_secondary;
×
1142
  } // End of loop over secondary generations
1143

1144
  // Reset work so that fission bank etc works correctly
NEW
1145
  calculate_work(settings::n_particles);
×
UNCOV
1146
}
×
1147

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