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

openmc-dev / openmc / 23011177584

12 Mar 2026 03:57PM UTC coverage: 80.895% (-0.7%) from 81.566%
23011177584

Pull #3863

github

web-flow
Merge 00a6e11ae into ba94c5823
Pull Request #3863: Shared Secondary Particle Bank

16504 of 23690 branches covered (69.67%)

Branch coverage included in aggregate %.

261 of 367 new or added lines in 17 files covered. (71.12%)

609 existing lines in 46 files now uncovered.

56448 of 66491 relevant lines covered (84.9%)

25290933.62 hits per line

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

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

65
  int err = 0;
66
  while (status == 0 && err == 0) {
44,683✔
67
    err = openmc_next_batch(&status);
42,695✔
68
  }
69

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

75
int openmc_simulation_init()
2,323✔
76
{
77
  using namespace openmc;
2,323✔
78

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

83
  // Initialize nuclear data (energy limits, log grid)
84
  if (settings::run_CE) {
2,315✔
85
    initialize_data();
1,955✔
86
  }
87

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

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

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

99
  // If doing an event-based simulation, intialize the particle buffer
100
  // and event queues
101
  if (settings::event_based) {
2,315!
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) {
9,951✔
109
    t->set_strides();
7,636✔
110
    t->init_results();
7,636✔
111
  }
112

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

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

141
  // Display header
142
  if (mpi::master) {
2,315!
143
    if (settings::run_mode == RunMode::FIXED_SOURCE) {
2,315✔
144
      if (settings::solver_type == SolverType::MONTE_CARLO) {
999✔
145
        header("FIXED SOURCE TRANSPORT SIMULATION", 3);
875✔
146
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
124!
147
        header("FIXED SOURCE TRANSPORT SIMULATION (RANDOM RAY SOLVER)", 3);
124✔
148
      }
149
    } else if (settings::run_mode == RunMode::EIGENVALUE) {
1,316!
150
      if (settings::solver_type == SolverType::MONTE_CARLO) {
1,316✔
151
        header("K EIGENVALUE SIMULATION", 3);
1,216✔
152
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
100!
153
        header("K EIGENVALUE SIMULATION (RANDOM RAY SOLVER)", 3);
100✔
154
      }
155
      if (settings::verbosity >= 7)
1,316✔
156
        print_columns();
1,236✔
157
    }
158
  }
159

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

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

170
int openmc_simulation_finalize()
2,308✔
171
{
172
  using namespace openmc;
2,308✔
173

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

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

182
  // Clear material nuclide mapping
183
  for (auto& mat : model::materials) {
8,776✔
184
    mat->mat_nuclide_index_.clear();
12,936!
185
  }
186

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

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

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

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

209
  // Deactivate all tallies
210
  for (auto& t : model::tallies) {
9,944✔
211
    t->active_ = false;
7,636✔
212
  }
213

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

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

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

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

244
  initialize_batch();
44,191✔
245

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

250
    initialize_generation();
44,247✔
251

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

255
    // Transport loop
256
    if (settings::event_based) {
44,247!
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) {
44,247✔
264
        transport_history_based_shared_secondary();
208✔
265
      } else {
266
        transport_history_based();
44,039✔
267
      }
268
    }
269

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

273
    finalize_generation();
44,240✔
274
  }
275

276
  finalize_batch();
44,184✔
277

278
  // Check simulation ending criteria
279
  if (status) {
44,184!
280
    if (simulation::current_batch >= settings::n_max_batches) {
44,184✔
281
      *status = STATUS_EXIT_MAX_BATCH;
2,064✔
282
    } else if (simulation::satisfy_triggers) {
42,120✔
283
      *status = STATUS_EXIT_ON_TRIGGER;
28✔
284
    } else {
285
      *status = STATUS_EXIT_NORMAL;
42,092✔
286
    }
287
  }
288
  return 0;
289
}
290

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

296
  if (!simulation::initialized)
1,140!
297
    return false;
298
  else
299
    return contains(settings::statepoint_batch, simulation::current_batch);
2,280✔
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()
2,315✔
344
{
345
  if (settings::run_mode == RunMode::EIGENVALUE &&
2,315✔
346
      settings::solver_type == SolverType::MONTE_CARLO) {
1,316✔
347
    // Allocate source bank
348
    simulation::source_bank.resize(simulation::work_per_rank);
1,216✔
349

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

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

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

364
  if (settings::collision_track) {
2,315✔
365
    // Allocate collision track bank
366
    collision_track_reserve_bank();
44✔
367
  }
368
}
2,315✔
369

370
void initialize_batch()
49,871✔
371
{
372
  // Increment current batch
373
  ++simulation::current_batch;
49,871✔
374
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
49,871✔
375
    if (settings::solver_type == SolverType::RANDOM_RAY &&
21,315✔
376
        simulation::current_batch < settings::n_inactive + 1) {
3,640✔
377
      write_message(
4,360✔
378
        6, "Simulating batch {:<4} (inactive)", simulation::current_batch);
379
    } else {
380
      write_message(6, "Simulating batch {}", simulation::current_batch);
38,270✔
381
    }
382
  }
383

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

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

398
  // Manage active/inactive timers and activate tallies if necessary.
399
  if (first_inactive) {
49,835✔
400
    simulation::time_inactive.start();
1,080✔
401
  } else if (first_active) {
48,791✔
402
    simulation::time_inactive.stop();
2,299✔
403
    simulation::time_active.start();
2,299✔
404
    for (auto& t : model::tallies) {
9,927✔
405
      t->active_ = true;
7,628✔
406
    }
407
  }
408

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

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

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

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

431
  // Check_triggers
432
  if (mpi::master)
49,864!
433
    check_triggers();
49,864✔
434
#ifdef OPENMC_MPI
435
  MPI_Bcast(&simulation::satisfy_triggers, 1, MPI_C_BOOL, 0, mpi::intracomm);
436
#endif
437
  if (simulation::satisfy_triggers ||
49,864✔
438
      (settings::trigger_on &&
820✔
439
        simulation::current_batch == settings::n_max_batches)) {
820✔
440
    settings::statepoint_batch.insert(simulation::current_batch);
44✔
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) &&
99,728✔
446
      !settings::cmfd_run) {
2,376✔
447
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
4,560✔
448
        settings::source_write && !settings::source_separate) {
4,336✔
449
      bool b = (settings::run_mode == RunMode::EIGENVALUE);
2,004✔
450
      openmc_statepoint_write(nullptr, &b);
2,004✔
451
    } else {
452
      bool b = false;
308✔
453
      openmc_statepoint_write(nullptr, &b);
308✔
454
    }
455
  }
456

457
  if (settings::run_mode == RunMode::EIGENVALUE) {
49,864✔
458
    // Write out a separate source point if it's been specified for this batch
459
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
29,888✔
460
        settings::source_write && settings::source_separate) {
29,788✔
461

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

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

480
  // Write out surface source if requested.
481
  if (settings::surf_source_write &&
49,864✔
482
      simulation::ssw_current_file <= settings::ssw_max_files) {
3,456✔
483
    bool last_batch = (simulation::current_batch == settings::n_batches);
672✔
484
    if (simulation::surf_source_bank.full() || last_batch) {
672✔
485
      // Determine appropriate filename
486
      auto filename = fmt::format("{}surface_source.{}", settings::path_output,
376✔
487
        simulation::current_batch);
376✔
488
      if (settings::ssw_max_files == 1 ||
376✔
489
          (simulation::ssw_current_file == 1 && last_batch)) {
20!
490
        filename = settings::path_output + "surface_source";
356✔
491
      }
492

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

499
      // Write surface source file
500
      write_source_point(
376✔
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();
376✔
505
      if (!last_batch && settings::ssw_max_files >= 1) {
376!
506
        simulation::surf_source_bank.reserve(settings::ssw_max_particles);
308✔
507
      }
508
      ++simulation::ssw_current_file;
376✔
509
    }
376✔
510
  }
511
  // Write collision track file if requested
512
  if (settings::collision_track) {
49,864✔
513
    collision_track_flush_bank();
172✔
514
  }
515
}
49,864✔
516

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

523
    // Count source sites if using uniform fission source weighting
524
    if (settings::ufs_on)
28,612✔
525
      ufs_count_sites();
40✔
526

527
    // Store current value of tracklength k
528
    simulation::keff_generation = simulation::global_tallies(
28,612✔
529
      GlobalTally::K_TRACKLENGTH, TallyResult::VALUE);
530
  }
531
}
49,927✔
532

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

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

547
  // reset tallies
548
  if (settings::run_mode == RunMode::EIGENVALUE) {
49,920✔
549
    global_tally_collision = 0.0;
28,612✔
550
    global_tally_absorption = 0.0;
28,612✔
551
    global_tally_tracklength = 0.0;
28,612✔
552
  }
553
  global_tally_leakage = 0.0;
49,920✔
554

555
  if (settings::run_mode == RunMode::EIGENVALUE &&
49,920✔
556
      settings::solver_type == SolverType::MONTE_CARLO) {
28,612✔
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);
26,572✔
561

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

566
  if (settings::run_mode == RunMode::EIGENVALUE) {
49,920✔
567

568
    // Calculate shannon entropy
569
    if (settings::entropy_on &&
28,612✔
570
        settings::solver_type == SolverType::MONTE_CARLO)
4,820✔
571
      shannon_entropy();
2,780✔
572

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

577
    // Write generation output
578
    if (mpi::master && settings::verbosity >= 7) {
28,612!
579
      print_generation();
27,612✔
580
    }
581
  }
582
}
49,920✔
583

584
void sample_particle(Particle& p, int64_t index_source)
61,433,755✔
585
{
586
  // Sample a particle from the source bank
587
  if (settings::run_mode == RunMode::EIGENVALUE) {
61,433,755✔
588
    p.from_source(&simulation::source_bank[index_source - 1]);
51,472,800✔
589
  } else if (settings::run_mode == RunMode::FIXED_SOURCE) {
9,960,955!
590
    // initialize random number seed
591
    int64_t id = compute_transport_seed(compute_particle_id(index_source));
9,960,955✔
592
    uint64_t seed = init_seed(id, STREAM_SOURCE);
9,960,955✔
593
    // sample from external source distribution or custom library then set
594
    auto site = sample_external_source(&seed);
9,960,955✔
595
    p.from_source(&site);
9,960,952✔
596
  }
597
}
61,433,752✔
598

599
void initialize_history(Particle& p, int64_t index_source, bool is_secondary)
71,144,757✔
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) {
71,144,757✔
605
    sample_particle(p, index_source);
61,433,755✔
606
  }
607

608
  p.current_work() = index_source - 1;
71,144,754✔
609

610
  // set identifier for particle
611
  p.id() = compute_particle_id(index_source);
71,144,754✔
612

613
  // set progeny count to zero
614
  p.n_progeny() = 0;
71,144,754✔
615

616
  // Reset particle event counter
617
  p.n_event() = 0;
71,144,754✔
618

619
  // Initialize track counter (1 for this primary/secondary track)
620
  p.n_tracks() = 1;
71,144,754✔
621

622
  // Reset split counter
623
  p.n_split() = 0;
71,144,754✔
624

625
  // Reset weight window ratio
626
  p.ww_factor() = 0.0;
71,144,754✔
627

628
  // set particle history start weight
629
  p.wgt_born() = p.wgt();
71,144,754✔
630

631
  // Reset pulse_height_storage
632
  std::fill(p.pht_storage().begin(), p.pht_storage().end(), 0);
71,144,754✔
633

634
  // set random number seed
635
  int64_t particle_seed = compute_transport_seed(p.id());
71,144,754✔
636
  init_particle_seeds(particle_seed, p.seeds());
71,144,754✔
637

638
  // set particle trace
639
  p.trace() = false;
71,144,754✔
640
  if (simulation::current_batch == settings::trace_batch &&
71,148,754✔
641
      simulation::current_gen == settings::trace_gen &&
71,144,754!
642
      p.id() == settings::trace_particle)
4,000✔
643
    p.trace() = true;
4✔
644

645
  // Set particle track.
646
  p.write_track() = check_track_criteria(p);
71,144,754✔
647

648
  // Set the particle's initial weight window value.
649
  if (!is_secondary) {
71,144,754✔
650
    p.wgt_ww_born() = -1.0;
61,433,752✔
651
    apply_weight_windows(p);
61,433,752✔
652
  }
653

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

659
  // Add particle's starting weight to count for normalizing tallies later
660
  if (!is_secondary) {
71,144,754✔
661
#pragma omp atomic
15,374,645✔
662
    simulation::total_weight += p.wgt();
61,433,752✔
663
  }
664

665
  // Force calculation of cross-sections by setting last energy to zero
666
  if (settings::run_CE) {
71,144,754✔
667
    p.invalidate_neutron_xs();
29,494,858✔
668
  }
669

670
  // Prepare to write out particle track.
671
  if (p.write_track())
71,144,754✔
672
    add_particle_track(p);
276✔
673
}
71,144,754✔
674

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

681
int64_t compute_particle_id(int64_t index_source)
81,105,809✔
682
{
683
  if (settings::use_shared_secondary_bank) {
81,105,809✔
684
    return simulation::work_index[mpi::rank] + index_source +
10,280,622✔
685
           simulation::simulation_tracks_completed;
10,280,622✔
686
  } else {
687
    return simulation::work_index[mpi::rank] + index_source;
70,825,187✔
688
  }
689
}
690

691
int64_t compute_transport_seed(int64_t particle_id)
81,105,825✔
692
{
693
  if (settings::use_shared_secondary_bank) {
81,105,825✔
694
    return particle_id;
695
  } else {
696
    return (simulation::total_gen + overall_generation() - 1) *
70,825,199✔
697
             settings::n_particles +
698
           particle_id;
70,825,199✔
699
  }
700
}
701

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

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

710
  int64_t i_bank = 0;
5,104✔
711
  simulation::work_index.resize(mpi::n_procs + 1);
5,104✔
712
  simulation::work_index[0] = 0;
5,104✔
713
  for (int i = 0; i < mpi::n_procs; ++i) {
10,208✔
714
    // Number of particles for rank i
715
    int64_t work_i = i < remainder ? min_work + 1 : min_work;
5,104!
716

717
    // Set number of particles
718
    if (mpi::rank == i)
5,104!
719
      simulation::work_per_rank = work_i;
5,104✔
720

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

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

733
  for (const auto& nuc : data::nuclides) {
11,974✔
734
    if (nuc->grid_.size() >= 1) {
10,003!
735
      int neutron = ParticleType::neutron().transport_index();
10,003✔
736
      data::energy_min[neutron] =
10,003✔
737
        std::max(data::energy_min[neutron], nuc->grid_[0].energy.front());
11,794✔
738
      data::energy_max[neutron] =
10,003✔
739
        std::min(data::energy_max[neutron], nuc->grid_[0].energy.back());
12,254✔
740
    }
741
  }
742

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

755
    if (settings::electron_treatment == ElectronTreatment::TTB) {
120✔
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) {
108!
759
        int photon = ParticleType::photon().transport_index();
108✔
760
        int electron = ParticleType::electron().transport_index();
108✔
761
        int positron = ParticleType::positron().transport_index();
108✔
762
        int n_e = data::ttb_e_grid.size();
108✔
763

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

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

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

779
  // Show which nuclide results in lowest energy for neutron transport
780
  for (const auto& nuc : data::nuclides) {
2,447✔
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) {
2,267!
784
      double max_E = nuc->grid_[0].energy.back();
2,267✔
785
      int neutron = ParticleType::neutron().transport_index();
2,267✔
786
      if (max_E == data::energy_max[neutron]) {
2,267✔
787
        write_message(7, "Maximum neutron transport energy: {} eV for {}",
1,791✔
788
          data::energy_max[neutron], nuc->name_);
1,791✔
789
        if (mpi::master && data::energy_max[neutron] < 20.0e6) {
1,791!
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) {
11,974✔
800
    nuc->init_grid();
10,003✔
801
  }
802
  int neutron = ParticleType::neutron().transport_index();
1,971✔
803
  simulation::log_spacing =
3,942✔
804
    std::log(data::energy_max[neutron] / data::energy_min[neutron]) /
1,971✔
805
    settings::n_log_bins;
806
}
1,971✔
807

808
#ifdef OPENMC_MPI
809
void broadcast_results()
810
{
811
  // Broadcast tally results so that each process has access to results
812
  for (auto& t : model::tallies) {
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_;
817

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

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

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

841
#endif
842

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

849
void transport_history_based_single_particle(Particle& p)
71,144,770✔
850
{
851
  while (p.alive()) {
1,724,424,448✔
852
    p.event_calculate_xs();
1,653,279,682✔
853
    if (p.alive()) {
1,653,279,682!
854
      p.event_advance();
1,653,279,682✔
855
    }
856
    if (p.alive()) {
1,653,279,682✔
857
      if (p.collision_distance() > p.boundary().distance()) {
1,653,197,890✔
858
        p.event_cross_surface();
536,914,493✔
859
      } else if (p.alive()) {
1,116,283,397✔
860
        p.event_collide();
1,116,283,397✔
861
      }
862
    }
863
    p.event_check_limit_and_revive();
1,653,279,678✔
864
  }
865
  p.event_death();
71,144,766✔
866
}
71,144,766✔
867

868
void transport_history_based()
44,039✔
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) {
45,894,846✔
872
    Particle p;
45,861,822✔
873
    initialize_history(p, i_work, false);
45,861,822✔
874
    transport_history_based_single_particle(p);
45,861,819✔
875
  }
45,861,816✔
876
}
44,033✔
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()
208✔
887
{
888
  // Clear shared secondary banks from any prior use
889
  simulation::shared_secondary_bank_read.clear();
208✔
890
  simulation::shared_secondary_bank_write.clear();
208✔
891

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

898
  simulation::progeny_per_particle.resize(simulation::work_per_rank);
208✔
899
  std::fill(simulation::progeny_per_particle.begin(),
416✔
900
    simulation::progeny_per_particle.end(), 0);
208✔
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
  {
156✔
906
    vector<SourceSite> thread_bank;
156✔
907

908
#pragma omp for schedule(runtime)
909
    for (int64_t i = 1; i <= simulation::work_per_rank; i++) {
213,756✔
910
      Particle p;
213,600✔
911
      initialize_history(p, i, false);
213,600✔
912
      transport_history_based_single_particle(p);
213,600✔
913
      for (auto& site : p.local_secondary_bank()) {
656,766✔
914
        thread_bank.push_back(site);
443,166✔
915
      }
916
    }
213,600✔
917

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

927
  simulation::simulation_tracks_completed += settings::n_particles;
208✔
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;
208✔
932
  int64_t alive_secondary = 1;
208✔
933
  while (alive_secondary) {
2,789✔
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);
2,581✔
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(
2,581✔
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);
2,581✔
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) {
2,581!
955
      write_message(fmt::format(" Secondary generation {:<2}    tracks: {}",
5,162✔
956
                      n_generation_depth, alive_secondary),
957
        6);
958
    }
959

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

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

973
#pragma omp for schedule(runtime)
974
      for (int64_t i = 1; i <= simulation::shared_secondary_bank_read.size();
7,299,141✔
975
           i++) {
976
        Particle p;
7,297,200✔
977
        initialize_history(p, i, true);
7,297,200✔
978
        SourceSite& site = simulation::shared_secondary_bank_read[i - 1];
7,297,200✔
979
        p.event_revive_from_secondary(site);
7,297,200✔
980
        transport_history_based_single_particle(p);
7,297,200✔
981
        for (auto& secondary_site : p.local_secondary_bank()) {
14,151,234✔
982
          thread_bank.push_back(secondary_site);
6,854,034✔
983
        }
984
      }
7,297,200✔
985

986
      // Drain thread-local bank into the shared secondary bank (once per
987
      // thread)
988
#pragma omp critical(shared_secondary_bank)
989
      {
1,941✔
990
        for (auto& secondary_site : thread_bank) {
6,855,975✔
991
          simulation::shared_secondary_bank_write.thread_unsafe_append(
6,854,034✔
992
            secondary_site);
993
        }
994
      }
995
    } // End of transport loop over tracks in shared secondary bank
1,941✔
996
    n_generation_depth++;
2,581✔
997
    simulation::simulation_tracks_completed += alive_secondary;
2,581✔
998
  } // End of loop over secondary generations
999

1000
  // Reset work so that fission bank etc works correctly
1001
  calculate_work(settings::n_particles);
208✔
1002
}
208✔
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