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

openmc-dev / openmc / 21038208051

15 Jan 2026 04:16PM UTC coverage: 81.058%. First build
21038208051

Pull #3728

github

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

16002 of 22207 branches covered (72.06%)

Branch coverage included in aggregate %.

51 of 56 new or added lines in 9 files covered. (91.07%)

53718 of 63805 relevant lines covered (84.19%)

27672089.5 hits per line

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

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

3
#include "openmc/bank.h"
4
#include "openmc/capi.h"
5
#include "openmc/collision_track.h"
6
#include "openmc/container_util.h"
7
#include "openmc/eigenvalue.h"
8
#include "openmc/error.h"
9
#include "openmc/event.h"
10
#include "openmc/geometry_aux.h"
11
#include "openmc/ifp.h"
12
#include "openmc/material.h"
13
#include "openmc/message_passing.h"
14
#include "openmc/nuclide.h"
15
#include "openmc/output.h"
16
#include "openmc/particle.h"
17
#include "openmc/photon.h"
18
#include "openmc/random_lcg.h"
19
#include "openmc/settings.h"
20
#include "openmc/source.h"
21
#include "openmc/state_point.h"
22
#include "openmc/tallies/derivative.h"
23
#include "openmc/tallies/filter.h"
24
#include "openmc/tallies/tally.h"
25
#include "openmc/tallies/trigger.h"
26
#include "openmc/timer.h"
27
#include "openmc/track_output.h"
28
#include "openmc/weight_windows.h"
29

30
#ifdef _OPENMP
31
#include <omp.h>
32
#endif
33
#include "xtensor/xview.hpp"
34

35
#ifdef OPENMC_MPI
36
#include <mpi.h>
37
#endif
38

39
#include <fmt/format.h>
40

41
#include <algorithm>
42
#include <cmath>
43
#include <string>
44

45
//==============================================================================
46
// C API functions
47
//==============================================================================
48

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

53
int openmc_run()
2,298✔
54
{
55
  openmc::simulation::time_total.start();
2,298✔
56
  openmc_simulation_init();
2,298✔
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;
2,298✔
61
  if (openmc::simulation::current_batch >= openmc::settings::n_max_batches) {
2,298✔
62
    status = openmc::STATUS_EXIT_MAX_BATCH;
5✔
63
  }
64

65
  int err = 0;
2,298✔
66
  while (status == 0 && err == 0) {
44,688!
67
    err = openmc_next_batch(&status);
42,398✔
68
  }
69

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

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

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

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

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

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

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

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

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

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

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

140
  // Display header
141
  if (mpi::master) {
2,653!
142
    if (settings::run_mode == RunMode::FIXED_SOURCE) {
2,653✔
143
      if (settings::solver_type == SolverType::MONTE_CARLO) {
1,093✔
144
        header("FIXED SOURCE TRANSPORT SIMULATION", 3);
943✔
145
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
150!
146
        header("FIXED SOURCE TRANSPORT SIMULATION (RANDOM RAY SOLVER)", 3);
150✔
147
      }
148
    } else if (settings::run_mode == RunMode::EIGENVALUE) {
1,560!
149
      if (settings::solver_type == SolverType::MONTE_CARLO) {
1,560✔
150
        header("K EIGENVALUE SIMULATION", 3);
1,475✔
151
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
85!
152
        header("K EIGENVALUE SIMULATION (RANDOM RAY SOLVER)", 3);
85✔
153
      }
154
      if (settings::verbosity >= 7)
1,560✔
155
        print_columns();
1,460✔
156
    }
157
  }
158

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

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

169
int openmc_simulation_finalize()
2,645✔
170
{
171
  using namespace openmc;
172

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

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

181
  // Clear material nuclide mapping
182
  for (auto& mat : model::materials) {
10,360✔
183
    mat->mat_nuclide_index_.clear();
7,715✔
184
  }
185

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

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

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

198
  // Write tally results to tallies.out
199
  if (settings::output_tallies && mpi::master)
2,645!
200
    write_tallies();
2,565✔
201

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

208
  // Deactivate all tallies
209
  for (auto& t : model::tallies) {
11,670✔
210
    t->active_ = false;
9,025✔
211
  }
212

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

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

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

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

243
  initialize_batch();
44,268✔
244

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

249
    initialize_generation();
44,338✔
250

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

254
    // Transport loop
255
    if (settings::event_based) {
44,338✔
256
      transport_event_based();
3,172✔
257
    } else {
258
      transport_history_based();
41,166✔
259
    }
260

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

264
    finalize_generation();
44,330✔
265
  }
266

267
  finalize_batch();
44,260✔
268

269
  // Check simulation ending criteria
270
  if (status) {
44,260!
271
    if (simulation::current_batch >= settings::n_max_batches) {
44,260✔
272
      *status = STATUS_EXIT_MAX_BATCH;
2,385✔
273
    } else if (simulation::satisfy_triggers) {
41,875✔
274
      *status = STATUS_EXIT_ON_TRIGGER;
35✔
275
    } else {
276
      *status = STATUS_EXIT_NORMAL;
41,840✔
277
    }
278
  }
279
  return 0;
44,260✔
280
}
281

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

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

293
namespace openmc {
294

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

299
namespace simulation {
300

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

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

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

328
} // namespace simulation
329

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

334
void allocate_banks()
2,653✔
335
{
336
  if (settings::run_mode == RunMode::EIGENVALUE &&
2,653✔
337
      settings::solver_type == SolverType::MONTE_CARLO) {
1,560✔
338
    // Allocate source bank
339
    simulation::source_bank.resize(simulation::work_per_rank);
1,475✔
340

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

344
    // Allocate IFP bank
345
    resize_simulation_ifp_banks();
1,475✔
346
  }
347

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

353
  if (settings::collision_track) {
2,653✔
354
    // Allocate collision track bank
355
    collision_track_reserve_bank();
54✔
356
  }
357
}
2,653✔
358

359
void initialize_batch()
49,868✔
360
{
361
  // Increment current batch
362
  ++simulation::current_batch;
49,868✔
363
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
49,868✔
364
    if (settings::solver_type == SolverType::RANDOM_RAY &&
18,698✔
365
        simulation::current_batch < settings::n_inactive + 1) {
4,400✔
366
      write_message(
2,675✔
367
        6, "Simulating batch {:<4} (inactive)", simulation::current_batch);
368
    } else {
369
      write_message(6, "Simulating batch {}", simulation::current_batch);
16,023✔
370
    }
371
  }
372

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

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

387
  // Manage active/inactive timers and activate tallies if necessary.
388
  if (first_inactive) {
49,868✔
389
    simulation::time_inactive.start();
1,265✔
390
  } else if (first_active) {
48,603✔
391
    simulation::time_inactive.stop();
2,633✔
392
    simulation::time_active.start();
2,633✔
393
    for (auto& t : model::tallies) {
11,648✔
394
      if (t->offset_ == 0)
9,015✔
395
        t->active_ = true;
8,935✔
396
    }
397
  }
398

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

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

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

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

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

434
  // Check_triggers
435
  if (mpi::master)
49,860!
436
    check_triggers();
49,860✔
437
#ifdef OPENMC_MPI
438
  MPI_Bcast(&simulation::satisfy_triggers, 1, MPI_C_BOOL, 0, mpi::intracomm);
439
#endif
440
  if (simulation::satisfy_triggers ||
49,860✔
441
      (settings::trigger_on &&
1,025✔
442
        simulation::current_batch == settings::n_max_batches)) {
1,025✔
443
    settings::statepoint_batch.insert(simulation::current_batch);
55✔
444
  }
445

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

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

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

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

483
  // Write out surface source if requested.
484
  if (settings::surf_source_write &&
49,860✔
485
      simulation::ssw_current_file <= settings::ssw_max_files) {
4,225✔
486
    bool last_batch = (simulation::current_batch == settings::n_batches);
760✔
487
    if (simulation::surf_source_bank.full() || last_batch) {
760✔
488
      // Determine appropriate filename
489
      auto filename = fmt::format("{}surface_source.{}", settings::path_output,
490
        simulation::current_batch);
451✔
491
      if (settings::ssw_max_files == 1 ||
451✔
492
          (simulation::ssw_current_file == 1 && last_batch)) {
25!
493
        filename = settings::path_output + "surface_source";
426✔
494
      }
495

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

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

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

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

526
    // Count source sites if using uniform fission source weighting
527
    if (settings::ufs_on)
31,240✔
528
      ufs_count_sites();
50✔
529

530
    // Store current value of tracklength k
531
    simulation::keff_generation = simulation::global_tallies(
31,240✔
532
      GlobalTally::K_TRACKLENGTH, TallyResult::VALUE);
533
  }
534
}
49,938✔
535

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

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

550
  // reset tallies
551
  if (settings::run_mode == RunMode::EIGENVALUE) {
49,930✔
552
    global_tally_collision = 0.0;
31,240✔
553
    global_tally_absorption = 0.0;
31,240✔
554
    global_tally_tracklength = 0.0;
31,240✔
555
  }
556
  global_tally_leakage = 0.0;
49,930✔
557

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

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

569
  if (settings::run_mode == RunMode::EIGENVALUE) {
49,930✔
570

571
    // Calculate shannon entropy
572
    if (settings::entropy_on &&
31,240✔
573
        settings::solver_type == SolverType::MONTE_CARLO)
4,675✔
574
      shannon_entropy();
3,475✔
575

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

580
    // Write generation output
581
    if (mpi::master && settings::verbosity >= 7) {
31,240!
582
      print_generation();
29,990✔
583
    }
584
  }
585
}
49,930✔
586

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

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

608
  // set progeny count to zero
609
  p.n_progeny() = 0;
76,411,085✔
610

611
  // Reset particle event counter
612
  p.n_event() = 0;
76,411,085✔
613

614
  // Reset split counter
615
  p.n_split() = 0;
76,411,085✔
616

617
  // Reset weight window ratio
618
  p.ww_factor() = 0.0;
76,411,085✔
619

620
  // set particle history start weight
621
  p.wgt_born() = p.wgt();
76,411,085✔
622

623
  // Reset pulse_height_storage
624
  std::fill(p.pht_storage().begin(), p.pht_storage().end(), 0);
76,411,085✔
625

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

632
  // set particle trace
633
  p.trace() = false;
76,411,085✔
634
  if (simulation::current_batch == settings::trace_batch &&
152,827,170✔
635
      simulation::current_gen == settings::trace_gen &&
76,416,085!
636
      p.id() == settings::trace_particle)
5,000✔
637
    p.trace() = true;
5✔
638

639
  // Set particle track.
640
  p.write_track() = check_track_criteria(p);
76,411,085✔
641

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

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

651
// Add particle's starting weight to count for normalizing tallies later
652
#pragma omp atomic
30,627,594✔
653
  simulation::total_weight += p.wgt();
76,411,085✔
654

655
  // Force calculation of cross-sections by setting last energy to zero
656
  if (settings::run_CE) {
76,411,085✔
657
    p.invalidate_neutron_xs();
25,491,085✔
658
  }
659

660
  // Prepare to write out particle track.
661
  if (p.write_track())
76,411,085✔
662
    add_particle_track(p);
345✔
663
}
76,411,085✔
664

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

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

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

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

686
    // Set number of particles
687
    if (mpi::rank == i)
2,653!
688
      simulation::work_per_rank = work_i;
2,653✔
689

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

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

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

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

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

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

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

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

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

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

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

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

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

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

810
#endif
811

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

818
void transport_history_based_single_particle(Particle& p)
64,094,400✔
819
{
820
  while (p.alive()) {
1,675,617,432✔
821
    p.event_calculate_xs();
1,611,523,037✔
822
    if (p.alive()) {
1,611,523,037!
823
      p.event_advance();
1,611,523,037✔
824
    }
825
    if (p.alive()) {
1,611,523,037✔
826
      if (p.collision_distance() > p.boundary().distance()) {
1,611,421,797✔
827
        p.event_cross_surface();
586,912,630✔
828
      } else if (p.alive()) {
1,024,509,167!
829
        p.event_collide();
1,024,509,167✔
830
      }
831
    }
832
    p.event_revive_from_secondary();
1,611,523,032✔
833
  }
834
  p.event_death();
64,094,395✔
835
}
64,094,395✔
836

837
void transport_history_based()
41,166✔
838
{
839
#pragma omp parallel for schedule(runtime)
840
  for (int64_t i_work = 1; i_work <= simulation::work_per_rank; ++i_work) {
45,879,480✔
841
    Particle p;
45,852,822✔
842
    initialize_history(p, i_work);
45,852,822✔
843
    transport_history_based_single_particle(p);
45,852,819✔
844
  }
45,852,816✔
845
}
41,160✔
846

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

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

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

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

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

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

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

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

© 2026 Coveralls, Inc