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

openmc-dev / openmc / 21949996581

12 Feb 2026 02:10PM UTC coverage: 81.778% (-0.1%) from 81.905%
21949996581

Pull #3795

github

web-flow
Merge dd362849e into a3426cf83
Pull Request #3795: Source activity

17372 of 24363 branches covered (71.3%)

Branch coverage included in aggregate %.

62 of 97 new or added lines in 5 files covered. (63.92%)

228 existing lines in 5 files now uncovered.

56421 of 65873 relevant lines covered (85.65%)

44528283.57 hits per line

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

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

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

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

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

40
#include <fmt/format.h>
41

42
#include <algorithm>
43
#include <cmath>
44
#include <limits>
45
#include <string>
46

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

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

55
int openmc_run()
5,501✔
56
{
57
  openmc::simulation::time_total.start();
5,501✔
58
  openmc_simulation_init();
5,501✔
59

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

67
  int err = 0;
5,501✔
68
  while (status == 0 && err == 0) {
125,307!
69
    err = openmc_next_batch(&status);
119,817✔
70
  }
71

72
  openmc_simulation_finalize();
5,490✔
73
  openmc::simulation::time_total.stop();
5,490✔
74
  return err;
5,490✔
75
}
76

77
int openmc_simulation_init()
6,532✔
78
{
79
  using namespace openmc;
80

81
  // Skip if simulation has already been initialized
82
  if (simulation::initialized)
6,532✔
83
    return 0;
20✔
84

85
  // Initialize nuclear data (energy limits, log grid)
86
  if (settings::run_CE) {
6,512✔
87
    initialize_data();
5,290✔
88
  }
89

90
  // Determine how much work each process should do
91
  calculate_work();
6,512✔
92

93
  // Allocate source, fission and surface source banks.
94
  allocate_banks();
6,512✔
95

96
  // Create track file if needed
97
  if (!settings::track_identifiers.empty() || settings::write_all_tracks) {
6,512✔
98
    open_track_file();
84✔
99
  }
100

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

109
  // Allocate tally results arrays if they're not allocated yet
110
  for (auto& t : model::tallies) {
30,943✔
111
    t->set_strides();
24,431✔
112
    t->init_results();
24,431✔
113
  }
114

115
  // Set up material nuclide index mapping
116
  for (auto& mat : model::materials) {
23,444✔
117
    mat->init_nuclide_index();
16,932✔
118
  }
119

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

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

142
  // Display header
143
  if (mpi::master) {
6,512✔
144
    if (settings::run_mode == RunMode::FIXED_SOURCE) {
5,584✔
145
      if (settings::solver_type == SolverType::MONTE_CARLO) {
2,334✔
146
        header("FIXED SOURCE TRANSPORT SIMULATION", 3);
2,032✔
147
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
302!
148
        header("FIXED SOURCE TRANSPORT SIMULATION (RANDOM RAY SOLVER)", 3);
302✔
149
      }
150
    } else if (settings::run_mode == RunMode::EIGENVALUE) {
3,250!
151
      if (settings::solver_type == SolverType::MONTE_CARLO) {
3,250✔
152
        header("K EIGENVALUE SIMULATION", 3);
3,000✔
153
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
250!
154
        header("K EIGENVALUE SIMULATION (RANDOM RAY SOLVER)", 3);
250✔
155
      }
156
      if (settings::verbosity >= 7)
3,250✔
157
        print_columns();
3,066✔
158
    }
159
  }
160

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

166
  // Set flag indicating initialization is done
167
  simulation::initialized = true;
6,512✔
168
  return 0;
6,512✔
169
}
170

171
int openmc_simulation_finalize()
6,501✔
172
{
173
  using namespace openmc;
174

175
  // Skip if simulation was never run
176
  if (!simulation::initialized)
6,501!
177
    return 0;
×
178

179
  // Stop active batch timer and start finalization timer
180
  simulation::time_active.stop();
6,501✔
181
  simulation::time_finalize.start();
6,501✔
182

183
  // Clear material nuclide mapping
184
  for (auto& mat : model::materials) {
23,422✔
185
    mat->mat_nuclide_index_.clear();
16,921✔
186
  }
187

188
  // Close track file if open
189
  if (!settings::track_identifiers.empty() || settings::write_all_tracks) {
6,501✔
190
    close_track_file();
84✔
191
  }
192

193
  // Increment total number of generations
194
  simulation::total_gen += simulation::current_batch * settings::gen_per_batch;
6,501✔
195

196
#ifdef OPENMC_MPI
197
  broadcast_results();
3,176✔
198
#endif
199

200
  // Write tally results to tallies.out
201
  if (settings::output_tallies && mpi::master)
6,501!
202
    write_tallies();
5,259✔
203

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

210
  // Deactivate all tallies
211
  for (auto& t : model::tallies) {
30,932✔
212
    t->active_ = false;
24,431✔
213
  }
214

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

229
  // Reset flags
230
  simulation::initialized = false;
6,501✔
231
  return 0;
6,501✔
232
}
233

234
int openmc_next_batch(int* status)
123,567✔
235
{
236
  using namespace openmc;
237
  using openmc::simulation::current_gen;
238

239
  // Make sure simulation has been initialized
240
  if (!simulation::initialized) {
123,567✔
241
    set_errmsg("Simulation has not been initialized yet.");
10✔
242
    return OPENMC_E_ALLOCATE;
10✔
243
  }
244

245
  initialize_batch();
123,557✔
246

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

251
    initialize_generation();
123,753✔
252

253
    // Start timer for transport
254
    simulation::time_transport.start();
123,753✔
255

256
    // Transport loop
257
    if (settings::event_based) {
123,753✔
258
      transport_event_based();
3,172✔
259
    } else {
260
      transport_history_based();
120,581✔
261
    }
262

263
    // Accumulate time for transport
264
    simulation::time_transport.stop();
123,742✔
265

266
    finalize_generation();
123,742✔
267
  }
268

269
  finalize_batch();
123,546✔
270

271
  // Check simulation ending criteria
272
  if (status) {
123,546!
273
    if (simulation::current_batch >= settings::n_max_batches) {
123,546✔
274
      *status = STATUS_EXIT_MAX_BATCH;
5,664✔
275
    } else if (simulation::satisfy_triggers) {
117,882✔
276
      *status = STATUS_EXIT_ON_TRIGGER;
86✔
277
    } else {
278
      *status = STATUS_EXIT_NORMAL;
117,796✔
279
    }
280
  }
281
  return 0;
123,546✔
282
}
283

284
bool openmc_is_statepoint_batch()
2,850✔
285
{
286
  using namespace openmc;
287
  using openmc::simulation::current_gen;
288

289
  if (!simulation::initialized)
2,850!
290
    return false;
×
291
  else
292
    return contains(settings::statepoint_batch, simulation::current_batch);
2,850✔
293
}
294

295
namespace openmc {
296

297
//==============================================================================
298
// Global variables
299
//==============================================================================
300

301
namespace simulation {
302

303
int ct_current_file;
304
int current_batch;
305
int current_gen;
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
vector<double> decay_times;
328

329
} // namespace simulation
330

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

335
void allocate_banks()
6,512✔
336
{
337
  if (settings::run_mode == RunMode::EIGENVALUE &&
6,512✔
338
      settings::solver_type == SolverType::MONTE_CARLO) {
3,932✔
339
    // Allocate source bank
340
    simulation::source_bank.resize(simulation::work_per_rank);
3,586✔
341

342
    // Allocate fission bank
343
    init_fission_bank(3 * simulation::work_per_rank);
3,586✔
344

345
    // Allocate IFP bank
346
    if (settings::ifp_on) {
3,586✔
347
      resize_simulation_ifp_banks();
68✔
348
    }
349
  }
350

351
  if (settings::surf_source_write) {
6,512✔
352
    // Allocate surface source bank
353
    simulation::surf_source_bank.reserve(settings::ssw_max_particles);
887✔
354
  }
355

356
  if (settings::collision_track) {
6,512✔
357
    // Allocate collision track bank
358
    collision_track_reserve_bank();
137✔
359
  }
360
}
6,512✔
361

362
void compute_decay_times()
57,205✔
363
{
364
  int n_sources = model::external_sources.size();
57,205✔
365

366
  // Check each source for a PoissonProcess time distribution
367
  vector<double> mean_dts(n_sources, 0.0);
57,205✔
368
  bool any_poisson = false;
57,205✔
369
  for (int s = 0; s < n_sources; ++s) {
147,090✔
370
    auto* indep =
371
      dynamic_cast<IndependentSource*>(model::external_sources[s].get());
89,885!
372
    if (indep) {
89,885✔
373
      auto* poisson = dynamic_cast<const PoissonProcess*>(indep->time());
87,695!
374
      if (poisson) {
87,695!
NEW
375
        double rate = poisson->rate();
×
NEW
376
        mean_dts[s] = (rate > 0.0) ? 1.0 / rate : 0.0;
×
NEW
377
        any_poisson = true;
×
378
      }
379
    }
380
  }
381

382
  // If no sources use PoissonProcess timing, clear and return
383
  if (!any_poisson) {
57,205!
384
    simulation::decay_times.clear();
57,205✔
385
    return;
57,205✔
386
  }
387

NEW
388
  int64_t global_start = simulation::work_index[mpi::rank];
×
NEW
389
  int64_t global_end = global_start + simulation::work_per_rank;
×
390

391
  // Per-source Poisson process state: RNG seed and cumulative time
NEW
392
  vector<uint64_t> source_seeds(n_sources);
×
NEW
393
  vector<double> cumulative_times(n_sources, 0.0);
×
NEW
394
  for (int s = 0; s < n_sources; ++s) {
×
NEW
395
    source_seeds[s] = init_seed(
×
NEW
396
      simulation::current_batch * (n_sources + 1) + s + 1, STREAM_SOURCE);
×
397
  }
398

399
  // Single pass over all global particles up to this rank's end.
400
  // For each particle, replicate the source selection, advance that
401
  // source's Poisson clock, and store the time if it belongs to this rank.
402
  // Non-Poisson sources get NaN so initialize_history() can skip them.
NEW
403
  simulation::decay_times.resize(simulation::work_per_rank);
×
NEW
404
  for (int64_t i = 0; i < global_end; ++i) {
×
405
    // Replicate source selection RNG from sample_external_source
NEW
406
    int64_t id = (simulation::total_gen + overall_generation() - 1) *
×
NEW
407
                   settings::n_particles +
×
NEW
408
                 i + 1;
×
NEW
409
    uint64_t seed = init_seed(id, STREAM_SOURCE);
×
410

411
    int src_idx;
NEW
412
    if (settings::uniform_source_sampling) {
×
NEW
413
      src_idx = static_cast<int>(prn(&seed) * n_sources);
×
NEW
414
      if (src_idx >= n_sources)
×
NEW
415
        src_idx = n_sources - 1;
×
416
    } else {
NEW
417
      src_idx = model::external_sources_probability.sample(&seed);
×
418
    }
419

420
    // Advance this source's Poisson clock (or mark NaN for non-Poisson)
NEW
421
    double decay_time = std::numeric_limits<double>::quiet_NaN();
×
NEW
422
    if (mean_dts[src_idx] > 0.0) {
×
NEW
423
      cumulative_times[src_idx] +=
×
NEW
424
        -mean_dts[src_idx] * std::log(1.0 - prn(&source_seeds[src_idx]));
×
NEW
425
      decay_time = cumulative_times[src_idx];
×
426
    }
427

428
    // Store if this particle belongs to this rank
NEW
429
    if (i >= global_start) {
×
NEW
430
      simulation::decay_times[i - global_start] = decay_time;
×
431
    }
432
  }
433
}
57,205!
434

435
void initialize_batch()
142,229✔
436
{
437
  // Increment current batch
438
  ++simulation::current_batch;
142,229✔
439
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
142,229✔
440
    if (settings::solver_type == SolverType::RANDOM_RAY &&
57,205✔
441
        simulation::current_batch < settings::n_inactive + 1) {
12,332✔
442
      write_message(
7,496✔
443
        6, "Simulating batch {:<4} (inactive)", simulation::current_batch);
444
    } else {
445
      write_message(6, "Simulating batch {}", simulation::current_batch);
49,709✔
446
    }
447
  }
448

449
  // Pre-compute decay times for activity-based timing (fixed-source only)
450
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
142,229✔
451
    compute_decay_times();
57,205✔
452
  }
453

454
  // Reset total starting particle weight used for normalizing tallies
455
  simulation::total_weight = 0.0;
142,229✔
456

457
  // Determine if this batch is the first inactive or active batch.
458
  bool first_inactive = false;
142,229✔
459
  bool first_active = false;
142,229✔
460
  if (!settings::restart_run) {
142,229✔
461
    first_inactive = settings::n_inactive > 0 && simulation::current_batch == 1;
142,079✔
462
    first_active = simulation::current_batch == settings::n_inactive + 1;
142,079✔
463
  } else if (simulation::current_batch == simulation::restart_batch + 1) {
150✔
464
    first_inactive = simulation::restart_batch < settings::n_inactive;
48✔
465
    first_active = !first_inactive;
48✔
466
  }
467

468
  // Manage active/inactive timers and activate tallies if necessary.
469
  if (first_inactive) {
142,229✔
470
    simulation::time_inactive.start();
3,330✔
471
  } else if (first_active) {
138,899✔
472
    simulation::time_inactive.stop();
6,469✔
473
    simulation::time_active.start();
6,469✔
474
    for (auto& t : model::tallies) {
30,880✔
475
      t->active_ = true;
24,411✔
476
    }
477
  }
478

479
  // Add user tallies to active tallies list
480
  setup_active_tallies();
142,229✔
481
}
142,229✔
482

483
void finalize_batch()
142,218✔
484
{
485
  // Reduce tallies onto master process and accumulate
486
  simulation::time_tallies.start();
142,218✔
487
  accumulate_tallies();
142,218✔
488
  simulation::time_tallies.stop();
142,218✔
489

490
  // update weight windows if needed
491
  for (const auto& wwg : variance_reduction::weight_windows_generators) {
144,340✔
492
    wwg->update();
2,122✔
493
  }
494

495
  // Reset global tally results
496
  if (simulation::current_batch <= settings::n_inactive) {
142,218✔
497
    xt::view(simulation::global_tallies, xt::all()) = 0.0;
27,674✔
498
    simulation::n_realizations = 0;
27,674✔
499
  }
500

501
  // Check_triggers
502
  if (mpi::master)
142,218✔
503
    check_triggers();
123,863✔
504
#ifdef OPENMC_MPI
505
  MPI_Bcast(&simulation::satisfy_triggers, 1, MPI_C_BOOL, 0, mpi::intracomm);
68,090✔
506
#endif
507
  if (simulation::satisfy_triggers ||
142,218✔
508
      (settings::trigger_on &&
2,362✔
509
        simulation::current_batch == settings::n_max_batches)) {
2,362✔
510
    settings::statepoint_batch.insert(simulation::current_batch);
130✔
511
  }
512

513
  // Write out state point if it's been specified for this batch and is not
514
  // a CMFD run instance
515
  if (contains(settings::statepoint_batch, simulation::current_batch) &&
148,926✔
516
      !settings::cmfd_run) {
6,708✔
517
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
12,900✔
518
        settings::source_write && !settings::source_separate) {
12,900✔
519
      bool b = (settings::run_mode == RunMode::EIGENVALUE);
5,518✔
520
      openmc_statepoint_write(nullptr, &b);
5,518✔
521
    } else {
522
      bool b = false;
1,030✔
523
      openmc_statepoint_write(nullptr, &b);
1,030✔
524
    }
525
  }
526

527
  if (settings::run_mode == RunMode::EIGENVALUE) {
142,218✔
528
    // Write out a separate source point if it's been specified for this batch
529
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
89,000✔
530
        settings::source_write && settings::source_separate) {
89,000✔
531

532
      // Determine width for zero padding
533
      int w = std::to_string(settings::n_max_batches).size();
66✔
534
      std::string source_point_filename = fmt::format("{0}source.{1:0{2}}",
535
        settings::path_output, simulation::current_batch, w);
52✔
536
      span<SourceSite> bankspan(simulation::source_bank);
66✔
537
      write_source_point(source_point_filename, bankspan,
66✔
538
        simulation::work_index, settings::source_mcpl_write);
539
    }
66✔
540

541
    // Write a continously-overwritten source point if requested.
542
    if (settings::source_latest) {
85,024✔
543
      auto filename = settings::path_output + "source";
140✔
544
      span<SourceSite> bankspan(simulation::source_bank);
140✔
545
      write_source_point(filename, bankspan, simulation::work_index,
140✔
546
        settings::source_mcpl_write);
547
    }
140✔
548
  }
549

550
  // Write out surface source if requested.
551
  if (settings::surf_source_write &&
142,218✔
552
      simulation::ssw_current_file <= settings::ssw_max_files) {
8,445✔
553
    bool last_batch = (simulation::current_batch == settings::n_batches);
1,632✔
554
    if (simulation::surf_source_bank.full() || last_batch) {
1,632✔
555
      // Determine appropriate filename
556
      auto filename = fmt::format("{}surface_source.{}", settings::path_output,
557
        simulation::current_batch);
733✔
558
      if (settings::ssw_max_files == 1 ||
917✔
559
          (simulation::ssw_current_file == 1 && last_batch)) {
50!
560
        filename = settings::path_output + "surface_source";
867✔
561
      }
562

563
      // Get span of source bank and calculate parallel index vector
564
      auto surf_work_index = mpi::calculate_parallel_index_vector(
565
        simulation::surf_source_bank.size());
917✔
566
      span<SourceSite> surfbankspan(simulation::surf_source_bank.begin(),
567
        simulation::surf_source_bank.size());
917✔
568

569
      // Write surface source file
570
      write_source_point(
917✔
571
        filename, surfbankspan, surf_work_index, settings::surf_mcpl_write);
572

573
      // Reset surface source bank and increment counter
574
      simulation::surf_source_bank.clear();
917✔
575
      if (!last_batch && settings::ssw_max_files >= 1) {
917!
576
        simulation::surf_source_bank.reserve(settings::ssw_max_particles);
752✔
577
      }
578
      ++simulation::ssw_current_file;
917✔
579
    }
917✔
580
  }
581
  // Write collision track file if requested
582
  if (settings::collision_track) {
142,218✔
583
    collision_track_flush_bank();
565✔
584
  }
585
}
142,218✔
586

587
void initialize_generation()
142,425✔
588
{
589
  if (settings::run_mode == RunMode::EIGENVALUE) {
142,425✔
590
    // Clear out the fission bank
591
    simulation::fission_bank.resize(0);
85,220✔
592

593
    // Count source sites if using uniform fission source weighting
594
    if (settings::ufs_on)
85,220✔
595
      ufs_count_sites();
140✔
596

597
    // Store current value of tracklength k
598
    simulation::keff_generation = simulation::global_tallies(
85,220✔
599
      GlobalTally::K_TRACKLENGTH, TallyResult::VALUE);
600
  }
601
}
142,425✔
602

603
void finalize_generation()
142,414✔
604
{
605
  auto& gt = simulation::global_tallies;
142,414✔
606

607
  // Update global tallies with the accumulation variables
608
  if (settings::run_mode == RunMode::EIGENVALUE) {
142,414✔
609
    gt(GlobalTally::K_COLLISION, TallyResult::VALUE) += global_tally_collision;
85,220✔
610
    gt(GlobalTally::K_ABSORPTION, TallyResult::VALUE) +=
85,220✔
611
      global_tally_absorption;
612
    gt(GlobalTally::K_TRACKLENGTH, TallyResult::VALUE) +=
85,220✔
613
      global_tally_tracklength;
614
  }
615
  gt(GlobalTally::LEAKAGE, TallyResult::VALUE) += global_tally_leakage;
142,414✔
616

617
  // reset tallies
618
  if (settings::run_mode == RunMode::EIGENVALUE) {
142,414✔
619
    global_tally_collision = 0.0;
85,220✔
620
    global_tally_absorption = 0.0;
85,220✔
621
    global_tally_tracklength = 0.0;
85,220✔
622
  }
623
  global_tally_leakage = 0.0;
142,414✔
624

625
  if (settings::run_mode == RunMode::EIGENVALUE &&
142,414✔
626
      settings::solver_type == SolverType::MONTE_CARLO) {
85,220✔
627
    // If using shared memory, stable sort the fission bank (by parent IDs)
628
    // so as to allow for reproducibility regardless of which order particles
629
    // are run in.
630
    sort_fission_bank();
78,880✔
631

632
    // Distribute fission bank across processors evenly
633
    synchronize_bank();
78,880✔
634
  }
635

636
  if (settings::run_mode == RunMode::EIGENVALUE) {
142,414✔
637

638
    // Calculate shannon entropy
639
    if (settings::entropy_on &&
85,220✔
640
        settings::solver_type == SolverType::MONTE_CARLO)
13,330✔
641
      shannon_entropy();
6,990✔
642

643
    // Collect results and statistics
644
    calculate_generation_keff();
85,220✔
645
    calculate_average_keff();
85,220✔
646

647
    // Write generation output
648
    if (mpi::master && settings::verbosity >= 7) {
85,220✔
649
      print_generation();
68,865✔
650
    }
651
  }
652
}
142,414✔
653

654
void initialize_history(Particle& p, int64_t index_source)
153,313,927✔
655
{
656
  // set defaults
657
  if (settings::run_mode == RunMode::EIGENVALUE) {
153,313,927✔
658
    // set defaults for eigenvalue simulations from primary bank
659
    p.from_source(&simulation::source_bank[index_source - 1]);
128,516,500✔
660
  } else if (settings::run_mode == RunMode::FIXED_SOURCE) {
24,797,427!
661
    // initialize random number seed
662
    int64_t id = (simulation::total_gen + overall_generation() - 1) *
24,797,427✔
663
                   settings::n_particles +
24,797,427✔
664
                 simulation::work_index[mpi::rank] + index_source;
24,797,427✔
665
    uint64_t seed = init_seed(id, STREAM_SOURCE);
24,797,427✔
666
    // sample from external source distribution or custom library then set
667
    auto site = sample_external_source(&seed);
24,797,427✔
668
    p.from_source(&site);
24,797,424✔
669
    // Override particle time with decay time if Poisson process timing is used
670
    if (!simulation::decay_times.empty()) {
24,797,424!
NEW
671
      double decay_time = simulation::decay_times[index_source - 1];
×
NEW
672
      if (!std::isnan(decay_time)) {
×
NEW
673
        p.time() = decay_time;
×
NEW
674
        p.time_last() = decay_time;
×
675
      }
676
    }
677
  }
678
  p.current_work() = index_source;
153,313,924✔
679

680
  // set identifier for particle
681
  p.id() = simulation::work_index[mpi::rank] + index_source;
153,313,924✔
682

683
  // set progeny count to zero
684
  p.n_progeny() = 0;
153,313,924✔
685

686
  // Reset particle event counter
687
  p.n_event() = 0;
153,313,924✔
688

689
  // Reset split counter
690
  p.n_split() = 0;
153,313,924✔
691

692
  // Reset weight window ratio
693
  p.ww_factor() = 0.0;
153,313,924✔
694

695
  // set particle history start weight
696
  p.wgt_born() = p.wgt();
153,313,924✔
697

698
  // Reset pulse_height_storage
699
  std::fill(p.pht_storage().begin(), p.pht_storage().end(), 0);
153,313,924✔
700

701
  // set random number seed
702
  int64_t particle_seed =
703
    (simulation::total_gen + overall_generation() - 1) * settings::n_particles +
153,313,924✔
704
    p.id();
153,313,924✔
705
  init_particle_seeds(particle_seed, p.seeds());
153,313,924✔
706

707
  // set particle trace
708
  p.trace() = false;
153,313,924✔
709
  if (simulation::current_batch == settings::trace_batch &&
306,637,848✔
710
      simulation::current_gen == settings::trace_gen &&
153,323,924!
711
      p.id() == settings::trace_particle)
10,000✔
712
    p.trace() = true;
10✔
713

714
  // Set particle track.
715
  p.write_track() = check_track_criteria(p);
153,313,924✔
716

717
  // Set the particle's initial weight window value.
718
  p.wgt_ww_born() = -1.0;
153,313,924✔
719
  apply_weight_windows(p);
153,313,924✔
720

721
  // Display message if high verbosity or trace is on
722
  if (settings::verbosity >= 9 || p.trace()) {
153,313,924!
723
    write_message("Simulating Particle {}", p.id());
10✔
724
  }
725

726
// Add particle's starting weight to count for normalizing tallies later
727
#pragma omp atomic
92,152,574✔
728
  simulation::total_weight += p.wgt();
153,313,924✔
729

730
  // Force calculation of cross-sections by setting last energy to zero
731
  if (settings::run_CE) {
153,313,924✔
732
    p.invalidate_neutron_xs();
51,473,924✔
733
  }
734

735
  // Prepare to write out particle track.
736
  if (p.write_track())
153,313,924✔
737
    add_particle_track(p);
930✔
738
}
153,313,924✔
739

740
int overall_generation()
178,344,546✔
741
{
742
  using namespace simulation;
743
  return settings::gen_per_batch * (current_batch - 1) + current_gen;
178,344,546✔
744
}
745

746
void calculate_work()
6,512✔
747
{
748
  // Determine minimum amount of particles to simulate on each processor
749
  int64_t min_work = settings::n_particles / mpi::n_procs;
6,512✔
750

751
  // Determine number of processors that have one extra particle
752
  int64_t remainder = settings::n_particles % mpi::n_procs;
6,512✔
753

754
  int64_t i_bank = 0;
6,512✔
755
  simulation::work_index.resize(mpi::n_procs + 1);
6,512✔
756
  simulation::work_index[0] = 0;
6,512✔
757
  for (int i = 0; i < mpi::n_procs; ++i) {
14,879✔
758
    // Number of particles for rank i
759
    int64_t work_i = i < remainder ? min_work + 1 : min_work;
8,367!
760

761
    // Set number of particles
762
    if (mpi::rank == i)
8,367✔
763
      simulation::work_per_rank = work_i;
6,512✔
764

765
    // Set index into source bank for rank i
766
    i_bank += work_i;
8,367✔
767
    simulation::work_index[i + 1] = i_bank;
8,367✔
768
  }
769
}
6,512✔
770

771
void initialize_data()
5,320✔
772
{
773
  // Determine minimum/maximum energy for incident neutron/photon data
774
  data::energy_max = {INFTY, INFTY, INFTY, INFTY};
5,320✔
775
  data::energy_min = {0.0, 0.0, 0.0, 0.0};
5,320✔
776

777
  for (const auto& nuc : data::nuclides) {
31,730✔
778
    if (nuc->grid_.size() >= 1) {
26,410!
779
      int neutron = ParticleType::neutron().transport_index();
26,410✔
780
      data::energy_min[neutron] =
52,820✔
781
        std::max(data::energy_min[neutron], nuc->grid_[0].energy.front());
26,410✔
782
      data::energy_max[neutron] =
26,410✔
783
        std::min(data::energy_max[neutron], nuc->grid_[0].energy.back());
26,410✔
784
    }
785
  }
786

787
  if (settings::photon_transport) {
5,320✔
788
    for (const auto& elem : data::elements) {
818✔
789
      if (elem->energy_.size() >= 1) {
554!
790
        int photon = ParticleType::photon().transport_index();
554✔
791
        int n = elem->energy_.size();
554✔
792
        data::energy_min[photon] =
1,108✔
793
          std::max(data::energy_min[photon], std::exp(elem->energy_(1)));
554✔
794
        data::energy_max[photon] =
1,108✔
795
          std::min(data::energy_max[photon], std::exp(elem->energy_(n - 1)));
554✔
796
      }
797
    }
798

799
    if (settings::electron_treatment == ElectronTreatment::TTB) {
264✔
800
      // Determine if minimum/maximum energy for bremsstrahlung is greater/less
801
      // than the current minimum/maximum
802
      if (data::ttb_e_grid.size() >= 1) {
244!
803
        int photon = ParticleType::photon().transport_index();
244✔
804
        int electron = ParticleType::electron().transport_index();
244✔
805
        int positron = ParticleType::positron().transport_index();
244✔
806
        int n_e = data::ttb_e_grid.size();
244✔
807

808
        const std::vector<int> charged = {electron, positron};
244✔
809
        for (auto t : charged) {
732✔
810
          data::energy_min[t] = std::exp(data::ttb_e_grid(1));
488✔
811
          data::energy_max[t] = std::exp(data::ttb_e_grid(n_e - 1));
488✔
812
        }
813

814
        data::energy_min[photon] =
488✔
815
          std::max(data::energy_min[photon], data::energy_min[electron]);
244✔
816

817
        data::energy_max[photon] =
488✔
818
          std::min(data::energy_max[photon], data::energy_max[electron]);
244✔
819
      }
244✔
820
    }
821
  }
822

823
  // Show which nuclide results in lowest energy for neutron transport
824
  for (const auto& nuc : data::nuclides) {
6,644✔
825
    // If a nuclide is present in a material that's not used in the model, its
826
    // grid has not been allocated
827
    if (nuc->grid_.size() > 0) {
6,210!
828
      double max_E = nuc->grid_[0].energy.back();
6,210✔
829
      int neutron = ParticleType::neutron().transport_index();
6,210✔
830
      if (max_E == data::energy_max[neutron]) {
6,210✔
831
        write_message(7, "Maximum neutron transport energy: {} eV for {}",
4,886✔
832
          data::energy_max[neutron], nuc->name_);
4,886✔
833
        if (mpi::master && data::energy_max[neutron] < 20.0e6) {
4,886!
834
          warning("Maximum neutron energy is below 20 MeV. This may bias "
×
835
                  "the results.");
836
        }
837
        break;
4,886✔
838
      }
839
    }
840
  }
841

842
  // Set up logarithmic grid for nuclides
843
  for (auto& nuc : data::nuclides) {
31,730✔
844
    nuc->init_grid();
26,410✔
845
  }
846
  int neutron = ParticleType::neutron().transport_index();
5,320✔
847
  simulation::log_spacing =
5,320✔
848
    std::log(data::energy_max[neutron] / data::energy_min[neutron]) /
5,320✔
849
    settings::n_log_bins;
850
}
5,320✔
851

852
#ifdef OPENMC_MPI
853
void broadcast_results()
3,176✔
854
{
855
  // Broadcast tally results so that each process has access to results
856
  for (auto& t : model::tallies) {
16,275✔
857
    // Create a new datatype that consists of all values for a given filter
858
    // bin and then use that to broadcast. This is done to minimize the
859
    // chance of the 'count' argument of MPI_BCAST exceeding 2**31
860
    auto& results = t->results_;
13,099✔
861

862
    auto shape = results.shape();
13,099✔
863
    int count_per_filter = shape[1] * shape[2];
13,099✔
864
    MPI_Datatype result_block;
865
    MPI_Type_contiguous(count_per_filter, MPI_DOUBLE, &result_block);
13,099✔
866
    MPI_Type_commit(&result_block);
13,099✔
867
    MPI_Bcast(results.data(), shape[0], result_block, 0, mpi::intracomm);
13,099✔
868
    MPI_Type_free(&result_block);
13,099✔
869
  }
870

871
  // Also broadcast global tally results
872
  auto& gt = simulation::global_tallies;
3,176✔
873
  MPI_Bcast(gt.data(), gt.size(), MPI_DOUBLE, 0, mpi::intracomm);
3,176✔
874

875
  // These guys are needed so that non-master processes can calculate the
876
  // combined estimate of k-effective
877
  double temp[] {
878
    simulation::k_col_abs, simulation::k_col_tra, simulation::k_abs_tra};
3,176✔
879
  MPI_Bcast(temp, 3, MPI_DOUBLE, 0, mpi::intracomm);
3,176✔
880
  simulation::k_col_abs = temp[0];
3,176✔
881
  simulation::k_col_tra = temp[1];
3,176✔
882
  simulation::k_abs_tra = temp[2];
3,176✔
883
}
3,176✔
884

885
#endif
886

887
void free_memory_simulation()
7,505✔
888
{
889
  simulation::k_generation.clear();
7,505✔
890
  simulation::entropy.clear();
7,505✔
891
  simulation::decay_times.clear();
7,505✔
892
}
7,505✔
893

894
void transport_history_based_single_particle(Particle& p)
141,176,654✔
895
{
896
  while (p.alive()) {
2,147,483,647✔
897
    p.event_calculate_xs();
2,147,483,647✔
898
    if (p.alive()) {
2,147,483,647!
899
      p.event_advance();
2,147,483,647✔
900
    }
901
    if (p.alive()) {
2,147,483,647✔
902
      if (p.collision_distance() > p.boundary().distance()) {
2,147,483,647✔
903
        p.event_cross_surface();
1,311,365,465✔
904
      } else if (p.alive()) {
2,147,483,647!
905
        p.event_collide();
2,147,483,647✔
906
      }
907
    }
908
    p.event_revive_from_secondary();
2,147,483,647✔
909
  }
910
  p.event_death();
141,176,646✔
911
}
141,176,646✔
912

913
void transport_history_based()
120,581✔
914
{
915
#pragma omp parallel for schedule(runtime)
916
  for (int64_t i_work = 1; i_work <= simulation::work_per_rank; ++i_work) {
61,301,627✔
917
    Particle p;
61,254,355✔
918
    initialize_history(p, i_work);
61,254,355✔
919
    transport_history_based_single_particle(p);
61,254,352✔
920
  }
61,254,348✔
921
}
120,574✔
922

923
void transport_event_based()
3,172✔
924
{
925
  int64_t remaining_work = simulation::work_per_rank;
3,172✔
926
  int64_t source_offset = 0;
3,172✔
927

928
  // To cap the total amount of memory used to store particle object data, the
929
  // number of particles in flight at any point in time can bet set. In the case
930
  // that the maximum in flight particle count is lower than the total number
931
  // of particles that need to be run this iteration, the event-based transport
932
  // loop is executed multiple times until all particles have been completed.
933
  while (remaining_work > 0) {
6,344✔
934
    // Figure out # of particles to run for this subiteration
935
    int64_t n_particles =
936
      std::min(remaining_work, settings::max_particles_in_flight);
3,172✔
937

938
    // Initialize all particle histories for this subiteration
939
    process_init_events(n_particles, source_offset);
3,172!
940

941
    // Event-based transport loop
942
    while (true) {
943
      // Determine which event kernel has the longest queue
944
      int64_t max = std::max({simulation::calculate_fuel_xs_queue.size(),
4,643,952!
945
        simulation::calculate_nonfuel_xs_queue.size(),
2,321,976✔
946
        simulation::advance_particle_queue.size(),
2,321,976✔
947
        simulation::surface_crossing_queue.size(),
2,321,976✔
948
        simulation::collision_queue.size()});
2,321,976✔
949

950
      // Execute event with the longest queue
951
      if (max == 0) {
2,321,976✔
952
        break;
3,172✔
953
      } else if (max == simulation::calculate_fuel_xs_queue.size()) {
2,318,804✔
954
        process_calculate_xs_events(simulation::calculate_fuel_xs_queue);
425,668!
955
      } else if (max == simulation::calculate_nonfuel_xs_queue.size()) {
1,893,136✔
956
        process_calculate_xs_events(simulation::calculate_nonfuel_xs_queue);
344,612!
957
      } else if (max == simulation::advance_particle_queue.size()) {
1,548,524✔
958
        process_advance_particle_events();
765,009!
959
      } else if (max == simulation::surface_crossing_queue.size()) {
783,515✔
960
        process_surface_crossing_events();
259,780!
961
      } else if (max == simulation::collision_queue.size()) {
523,735!
962
        process_collision_events();
523,735!
963
      }
964
    }
2,318,804✔
965

966
    // Execute death event for all particles
967
    process_death_events(n_particles);
3,172!
968

969
    // Adjust remaining work and source offset variables
970
    remaining_work -= n_particles;
3,172✔
971
    source_offset += n_particles;
3,172✔
972
  }
973
}
3,172✔
974

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