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

openmc-dev / openmc / 21899763772

11 Feb 2026 09:34AM UTC coverage: 81.695% (-0.2%) from 81.905%
21899763772

Pull #3795

github

web-flow
Merge ee5b4c070 into 3f20a5e22
Pull Request #3795: Source activity

16830 of 23342 branches covered (72.1%)

Branch coverage included in aggregate %.

27 of 59 new or added lines in 3 files covered. (45.76%)

308 existing lines in 25 files now uncovered.

55334 of 64991 relevant lines covered (85.14%)

44935365.75 hits per line

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

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

65
  int err = 0;
5,454 ✔
66
  while (status == 0 && err == 0) {
124,752 !
67
    err = openmc_next_batch(&status);
119,309 ✔
68
  }
69

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

75
int openmc_simulation_init()
6,482 ✔
76
{
77
  using namespace openmc;
78

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

83
  // Initialize nuclear data (energy limits, log grid)
84
  if (settings::run_CE) {
6,462 ✔
85
    initialize_data();
5,242 ✔
86
  }
87

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

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

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

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

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

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

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

140
  // Display header
141
  if (mpi::master) {
6,462 ✔
142
    if (settings::run_mode == RunMode::FIXED_SOURCE) {
5,549 ✔
143
      if (settings::solver_type == SolverType::MONTE_CARLO) {
2,315 ✔
144
        header("FIXED SOURCE TRANSPORT SIMULATION", 3);
2,015 ✔
145
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
300 !
146
        header("FIXED SOURCE TRANSPORT SIMULATION (RANDOM RAY SOLVER)", 3);
300 ✔
147
      }
148
    } else if (settings::run_mode == RunMode::EIGENVALUE) {
3,234 !
149
      if (settings::solver_type == SolverType::MONTE_CARLO) {
3,234 ✔
150
        header("K EIGENVALUE SIMULATION", 3);
2,984 ✔
151
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
250 !
152
        header("K EIGENVALUE SIMULATION (RANDOM RAY SOLVER)", 3);
250 ✔
153
      }
154
      if (settings::verbosity >= 7)
3,234 ✔
155
        print_columns();
3,050 ✔
156
    }
157
  }
158

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

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

169
int openmc_simulation_finalize()
6,451 ✔
170
{
171
  using namespace openmc;
172

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

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

181
  // Clear material nuclide mapping
182
  for (auto& mat : model::materials) {
23,266 ✔
183
    mat->mat_nuclide_index_.clear();
16,815 ✔
184
  }
185

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

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

194
#ifdef OPENMC_MPI
195
  broadcast_results();
3,126 ✔
196
#endif
197

198
  // Write tally results to tallies.out
199
  if (settings::output_tallies && mpi::master)
6,451 !
200
    write_tallies();
5,228 ✔
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) {
6,451 ✔
205
    openmc_weight_windows_export();
114 ✔
206
  }
207

208
  // Deactivate all tallies
209
  for (auto& t : model::tallies) {
30,827 ✔
210
    t->active_ = false;
24,376 ✔
211
  }
212

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

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

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

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

243
  initialize_batch();
123,049 ✔
244

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

249
    initialize_generation();
123,245 ✔
250

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

254
    // Transport loop
255
    if (settings::event_based) {
123,245 ✔
256
      transport_event_based();
3,172 ✔
257
    } else {
258
      transport_history_based();
120,073 ✔
259
    }
260

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

264
    finalize_generation();
123,234 ✔
265
  }
266

267
  finalize_batch();
123,038 ✔
268

269
  // Check simulation ending criteria
270
  if (status) {
123,038 !
271
    if (simulation::current_batch >= settings::n_max_batches) {
123,038 ✔
272
      *status = STATUS_EXIT_MAX_BATCH;
5,617 ✔
273
    } else if (simulation::satisfy_triggers) {
117,421 ✔
274
      *status = STATUS_EXIT_ON_TRIGGER;
86 ✔
275
    } else {
276
      *status = STATUS_EXIT_NORMAL;
117,335 ✔
277
    }
278
  }
279
  return 0;
123,038 ✔
280
}
281

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

287
  if (!simulation::initialized)
2,850 !
288
    return false;
×
289
  else
290
    return contains(settings::statepoint_batch, simulation::current_batch);
2,850 ✔
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 initialized {false};
305
double keff {1.0};
306
double keff_std;
307
double k_col_abs {0.0};
308
double k_col_tra {0.0};
309
double k_abs_tra {0.0};
310
double log_spacing;
311
int n_lost_particles {0};
312
bool need_depletion_rx {false};
313
int restart_batch;
314
bool satisfy_triggers {false};
315
int ssw_current_file;
316
int total_gen {0};
317
double total_weight;
318
int64_t work_per_rank;
319

320
const RegularMesh* entropy_mesh {nullptr};
321
const RegularMesh* ufs_mesh {nullptr};
322

323
vector<double> k_generation;
324
vector<int64_t> work_index;
325
vector<double> decay_times;
326

327
} // namespace simulation
328

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

333
void allocate_banks()
6,462 ✔
334
{
335
  if (settings::run_mode == RunMode::EIGENVALUE &&
6,462 ✔
336
      settings::solver_type == SolverType::MONTE_CARLO) {
3,910 ✔
337
    // Allocate source bank
338
    simulation::source_bank.resize(simulation::work_per_rank);
3,564 ✔
339

340
    // Allocate fission bank
341
    init_fission_bank(3 * simulation::work_per_rank);
3,564 ✔
342

343
    // Allocate IFP bank
344
    if (settings::ifp_on) {
3,564 ✔
345
      resize_simulation_ifp_banks();
68 ✔
346
    }
347
  }
348

349
  if (settings::surf_source_write) {
6,462 ✔
350
    // Allocate surface source bank
351
    simulation::surf_source_bank.reserve(settings::ssw_max_particles);
875 ✔
352
  }
353

354
  if (settings::collision_track) {
6,462 ✔
355
    // Allocate collision track bank
356
    collision_track_reserve_bank();
137 ✔
357
  }
358
}
6,462 ✔
359

360
void compute_decay_times()
56,805 ✔
361
{
362
  // If activity-based timing is not enabled, clear and return
363
  if (!settings::activity_based_timing) {
56,805 !
364
    simulation::decay_times.clear();
56,805 ✔
365
    return;
56,805 ✔
366
  }
367

NEW
368
  int n_sources = model::external_sources.size();
×
NEW
369
  int64_t global_start = simulation::work_index[mpi::rank];
×
NEW
370
  int64_t global_end = global_start + simulation::work_per_rank;
×
371

372
  // Per-source Poisson process state: RNG seed and cumulative time
NEW
373
  vector<uint64_t> source_seeds(n_sources);
×
NEW
374
  vector<double> cumulative_times(n_sources, 0.0);
×
NEW
375
  vector<double> mean_dts(n_sources);
×
NEW
376
  for (int s = 0; s < n_sources; ++s) {
×
NEW
377
    source_seeds[s] = init_seed(
×
NEW
378
      simulation::current_batch * (n_sources + 1) + s + 1, STREAM_SOURCE);
×
NEW
379
    double activity = model::external_sources[s]->strength();
×
NEW
380
    mean_dts[s] = (activity > 0.0) ? 1.0 / activity : 0.0;
×
381
  }
382

383
  // Single pass over all global particles up to this rank's end.
384
  // For each particle, replicate the source selection, advance that
385
  // source's Poisson clock, and store the time if it belongs to this rank.
NEW
386
  simulation::decay_times.resize(simulation::work_per_rank);
×
NEW
387
  for (int64_t i = 0; i < global_end; ++i) {
×
388
    // Replicate source selection RNG from sample_external_source
NEW
389
    int64_t id = (simulation::total_gen + overall_generation() - 1) *
×
NEW
390
                   settings::n_particles +
×
NEW
391
                 i + 1;
×
NEW
392
    uint64_t seed = init_seed(id, STREAM_SOURCE);
×
393

394
    int src_idx;
NEW
395
    if (settings::uniform_source_sampling) {
×
NEW
396
      src_idx = static_cast<int>(prn(&seed) * n_sources);
×
NEW
397
      if (src_idx >= n_sources)
×
NEW
398
        src_idx = n_sources - 1;
×
399
    } else {
NEW
400
      src_idx = model::external_sources_probability.sample(&seed);
×
401
    }
402

403
    // Advance this source's Poisson clock
NEW
404
    if (mean_dts[src_idx] > 0.0) {
×
NEW
405
      cumulative_times[src_idx] +=
×
NEW
406
        -mean_dts[src_idx] * std::log(1.0 - prn(&source_seeds[src_idx]));
×
407
    }
408

409
    // Store if this particle belongs to this rank
NEW
410
    if (i >= global_start) {
×
NEW
411
      simulation::decay_times[i - global_start] = cumulative_times[src_idx];
×
412
    }
413
  }
NEW
414
}
×
415

416
void initialize_batch()
141,709 ✔
417
{
418
  // Increment current batch
419
  ++simulation::current_batch;
141,709 ✔
420
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
141,709 ✔
421
    if (settings::solver_type == SolverType::RANDOM_RAY &&
56,805 ✔
422
        simulation::current_batch < settings::n_inactive + 1) {
12,320 ✔
423
      write_message(
7,490 ✔
424
        6, "Simulating batch {:<4} (inactive)", simulation::current_batch);
425
    } else {
426
      write_message(6, "Simulating batch {}", simulation::current_batch);
49,315 ✔
427
    }
428
  }
429

430
  // Pre-compute decay times for activity-based timing (fixed-source only)
431
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
141,709 ✔
432
    compute_decay_times();
56,805 ✔
433
  }
434

435
  // Reset total starting particle weight used for normalizing tallies
436
  simulation::total_weight = 0.0;
141,709 ✔
437

438
  // Determine if this batch is the first inactive or active batch.
439
  bool first_inactive = false;
141,709 ✔
440
  bool first_active = false;
141,709 ✔
441
  if (!settings::restart_run) {
141,709 ✔
442
    first_inactive = settings::n_inactive > 0 && simulation::current_batch == 1;
141,559 ✔
443
    first_active = simulation::current_batch == settings::n_inactive + 1;
141,559 ✔
444
  } else if (simulation::current_batch == simulation::restart_batch + 1) {
150 ✔
445
    first_inactive = simulation::restart_batch < settings::n_inactive;
48 ✔
446
    first_active = !first_inactive;
48 ✔
447
  }
448

449
  // Manage active/inactive timers and activate tallies if necessary.
450
  if (first_inactive) {
141,709 ✔
451
    simulation::time_inactive.start();
3,316 ✔
452
  } else if (first_active) {
138,393 ✔
453
    simulation::time_inactive.stop();
6,420 ✔
454
    simulation::time_active.start();
6,420 ✔
455
    for (auto& t : model::tallies) {
30,776 ✔
456
      t->active_ = true;
24,356 ✔
457
    }
458
  }
459

460
  // Add user tallies to active tallies list
461
  setup_active_tallies();
141,709 ✔
462
}
141,709 ✔
463

464
void finalize_batch()
141,698 ✔
465
{
466
  // Reduce tallies onto master process and accumulate
467
  simulation::time_tallies.start();
141,698 ✔
468
  accumulate_tallies();
141,698 ✔
469
  simulation::time_tallies.stop();
141,698 ✔
470

471
  // update weight windows if needed
472
  for (const auto& wwg : variance_reduction::weight_windows_generators) {
143,808 ✔
473
    wwg->update();
2,110 ✔
474
  }
475

476
  // Reset global tally results
477
  if (simulation::current_batch <= settings::n_inactive) {
141,698 ✔
478
    xt::view(simulation::global_tallies, xt::all()) = 0.0;
27,648 ✔
479
    simulation::n_realizations = 0;
27,648 ✔
480
  }
481

482
  // Check_triggers
483
  if (mpi::master)
141,698 ✔
484
    check_triggers();
123,468 ✔
485
#ifdef OPENMC_MPI
486
  MPI_Bcast(&simulation::satisfy_triggers, 1, MPI_C_BOOL, 0, mpi::intracomm);
67,570 ✔
487
#endif
488
  if (simulation::satisfy_triggers ||
141,698 ✔
489
      (settings::trigger_on &&
2,362 ✔
490
        simulation::current_batch == settings::n_max_batches)) {
2,362 ✔
491
    settings::statepoint_batch.insert(simulation::current_batch);
130 ✔
492
  }
493

494
  // Write out state point if it's been specified for this batch and is not
495
  // a CMFD run instance
496
  if (contains(settings::statepoint_batch, simulation::current_batch) &&
148,357 ✔
497
      !settings::cmfd_run) {
6,659 ✔
498
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
12,802 ✔
499
        settings::source_write && !settings::source_separate) {
12,802 ✔
500
      bool b = (settings::run_mode == RunMode::EIGENVALUE);
5,471 ✔
501
      openmc_statepoint_write(nullptr, &b);
5,471 ✔
502
    } else {
503
      bool b = false;
1,028 ✔
504
      openmc_statepoint_write(nullptr, &b);
1,028 ✔
505
    }
506
  }
507

508
  if (settings::run_mode == RunMode::EIGENVALUE) {
141,698 ✔
509
    // Write out a separate source point if it's been specified for this batch
510
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
88,858 ✔
511
        settings::source_write && settings::source_separate) {
88,858 ✔
512

513
      // Determine width for zero padding
514
      int w = std::to_string(settings::n_max_batches).size();
66 ✔
515
      std::string source_point_filename = fmt::format("{0}source.{1:0{2}}",
516
        settings::path_output, simulation::current_batch, w);
52 ✔
517
      span<SourceSite> bankspan(simulation::source_bank);
66 ✔
518
      write_source_point(source_point_filename, bankspan,
66 ✔
519
        simulation::work_index, settings::source_mcpl_write);
520
    }
66 ✔
521

522
    // Write a continously-overwritten source point if requested.
523
    if (settings::source_latest) {
84,904 ✔
524
      auto filename = settings::path_output + "source";
140 ✔
525
      span<SourceSite> bankspan(simulation::source_bank);
140 ✔
526
      write_source_point(filename, bankspan, simulation::work_index,
140 ✔
527
        settings::source_mcpl_write);
528
    }
140 ✔
529
  }
530

531
  // Write out surface source if requested.
532
  if (settings::surf_source_write &&
141,698 ✔
533
      simulation::ssw_current_file <= settings::ssw_max_files) {
8,385 ✔
534
    bool last_batch = (simulation::current_batch == settings::n_batches);
1,584 ✔
535
    if (simulation::surf_source_bank.full() || last_batch) {
1,584 ✔
536
      // Determine appropriate filename
537
      auto filename = fmt::format("{}surface_source.{}", settings::path_output,
538
        simulation::current_batch);
721 ✔
539
      if (settings::ssw_max_files == 1 ||
905 ✔
540
          (simulation::ssw_current_file == 1 && last_batch)) {
50 !
541
        filename = settings::path_output + "surface_source";
855 ✔
542
      }
543

544
      // Get span of source bank and calculate parallel index vector
545
      auto surf_work_index = mpi::calculate_parallel_index_vector(
546
        simulation::surf_source_bank.size());
905 ✔
547
      span<SourceSite> surfbankspan(simulation::surf_source_bank.begin(),
548
        simulation::surf_source_bank.size());
905 ✔
549

550
      // Write surface source file
551
      write_source_point(
905 ✔
552
        filename, surfbankspan, surf_work_index, settings::surf_mcpl_write);
553

554
      // Reset surface source bank and increment counter
555
      simulation::surf_source_bank.clear();
905 ✔
556
      if (!last_batch && settings::ssw_max_files >= 1) {
905 !
557
        simulation::surf_source_bank.reserve(settings::ssw_max_particles);
745 ✔
558
      }
559
      ++simulation::ssw_current_file;
905 ✔
560
    }
905 ✔
561
  }
562
  // Write collision track file if requested
563
  if (settings::collision_track) {
141,698 ✔
564
    collision_track_flush_bank();
565 ✔
565
  }
566
}
141,698 ✔
567

568
void initialize_generation()
141,905 ✔
569
{
570
  if (settings::run_mode == RunMode::EIGENVALUE) {
141,905 ✔
571
    // Clear out the fission bank
572
    simulation::fission_bank.resize(0);
85,100 ✔
573

574
    // Count source sites if using uniform fission source weighting
575
    if (settings::ufs_on)
85,100 ✔
576
      ufs_count_sites();
140 ✔
577

578
    // Store current value of tracklength k
579
    simulation::keff_generation = simulation::global_tallies(
85,100 ✔
580
      GlobalTally::K_TRACKLENGTH, TallyResult::VALUE);
581
  }
582
}
141,905 ✔
583

584
void finalize_generation()
141,894 ✔
585
{
586
  auto& gt = simulation::global_tallies;
141,894 ✔
587

588
  // Update global tallies with the accumulation variables
589
  if (settings::run_mode == RunMode::EIGENVALUE) {
141,894 ✔
590
    gt(GlobalTally::K_COLLISION, TallyResult::VALUE) += global_tally_collision;
85,100 ✔
591
    gt(GlobalTally::K_ABSORPTION, TallyResult::VALUE) +=
85,100 ✔
592
      global_tally_absorption;
593
    gt(GlobalTally::K_TRACKLENGTH, TallyResult::VALUE) +=
85,100 ✔
594
      global_tally_tracklength;
595
  }
596
  gt(GlobalTally::LEAKAGE, TallyResult::VALUE) += global_tally_leakage;
141,894 ✔
597

598
  // reset tallies
599
  if (settings::run_mode == RunMode::EIGENVALUE) {
141,894 ✔
600
    global_tally_collision = 0.0;
85,100 ✔
601
    global_tally_absorption = 0.0;
85,100 ✔
602
    global_tally_tracklength = 0.0;
85,100 ✔
603
  }
604
  global_tally_leakage = 0.0;
141,894 ✔
605

606
  if (settings::run_mode == RunMode::EIGENVALUE &&
141,894 ✔
607
      settings::solver_type == SolverType::MONTE_CARLO) {
85,100 ✔
608
    // If using shared memory, stable sort the fission bank (by parent IDs)
609
    // so as to allow for reproducibility regardless of which order particles
610
    // are run in.
611
    sort_fission_bank();
78,760 ✔
612

613
    // Distribute fission bank across processors evenly
614
    synchronize_bank();
78,760 ✔
615
  }
616

617
  if (settings::run_mode == RunMode::EIGENVALUE) {
141,894 ✔
618

619
    // Calculate shannon entropy
620
    if (settings::entropy_on &&
85,100 ✔
621
        settings::solver_type == SolverType::MONTE_CARLO)
13,330 ✔
622
      shannon_entropy();
6,990 ✔
623

624
    // Collect results and statistics
625
    calculate_generation_keff();
85,100 ✔
626
    calculate_average_keff();
85,100 ✔
627

628
    // Write generation output
629
    if (mpi::master && settings::verbosity >= 7) {
85,100 ✔
630
      print_generation();
68,780 ✔
631
    }
632
  }
633
}
141,894 ✔
634

635
void initialize_history(Particle& p, int64_t index_source)
153,212,927 ✔
636
{
637
  // set defaults
638
  if (settings::run_mode == RunMode::EIGENVALUE) {
153,212,927 ✔
639
    // set defaults for eigenvalue simulations from primary bank
640
    p.from_source(&simulation::source_bank[index_source - 1]);
128,506,000 ✔
641
  } else if (settings::run_mode == RunMode::FIXED_SOURCE) {
24,706,927 !
642
    // initialize random number seed
643
    int64_t id = (simulation::total_gen + overall_generation() - 1) *
24,706,927 ✔
644
                   settings::n_particles +
24,706,927 ✔
645
                 simulation::work_index[mpi::rank] + index_source;
24,706,927 ✔
646
    uint64_t seed = init_seed(id, STREAM_SOURCE);
24,706,927 ✔
647
    // sample from external source distribution or custom library then set
648
    auto site = sample_external_source(&seed);
24,706,927 ✔
649
    p.from_source(&site);
24,706,924 ✔
650
    // Override particle time with decay time if activity-based timing is on
651
    if (settings::activity_based_timing) {
24,706,924 !
NEW
652
      double decay_time = simulation::decay_times[index_source - 1];
×
NEW
653
      p.time() = decay_time;
×
NEW
654
      p.time_last() = decay_time;
×
655
    }
656
  }
657
  p.current_work() = index_source;
153,212,924 ✔
658

659
  // set identifier for particle
660
  p.id() = simulation::work_index[mpi::rank] + index_source;
153,212,924 ✔
661

662
  // set progeny count to zero
663
  p.n_progeny() = 0;
153,212,924 ✔
664

665
  // Reset particle event counter
666
  p.n_event() = 0;
153,212,924 ✔
667

668
  // Reset split counter
669
  p.n_split() = 0;
153,212,924 ✔
670

671
  // Reset weight window ratio
672
  p.ww_factor() = 0.0;
153,212,924 ✔
673

674
  // set particle history start weight
675
  p.wgt_born() = p.wgt();
153,212,924 ✔
676

677
  // Reset pulse_height_storage
678
  std::fill(p.pht_storage().begin(), p.pht_storage().end(), 0);
153,212,924 ✔
679

680
  // set random number seed
681
  int64_t particle_seed =
682
    (simulation::total_gen + overall_generation() - 1) * settings::n_particles +
153,212,924 ✔
683
    p.id();
153,212,924 ✔
684
  init_particle_seeds(particle_seed, p.seeds());
153,212,924 ✔
685

686
  // set particle trace
687
  p.trace() = false;
153,212,924 ✔
688
  if (simulation::current_batch == settings::trace_batch &&
306,435,848 ✔
689
      simulation::current_gen == settings::trace_gen &&
153,222,924 !
690
      p.id() == settings::trace_particle)
10,000 ✔
691
    p.trace() = true;
10 ✔
692

693
  // Set particle track.
694
  p.write_track() = check_track_criteria(p);
153,212,924 ✔
695

696
  // Set the particle's initial weight window value.
697
  p.wgt_ww_born() = -1.0;
153,212,924 ✔
698
  apply_weight_windows(p);
153,212,924 ✔
699

700
  // Display message if high verbosity or trace is on
701
  if (settings::verbosity >= 9 || p.trace()) {
153,212,924 !
702
    write_message("Simulating Particle {}", p.id());
10 ✔
703
  }
704

705
// Add particle's starting weight to count for normalizing tallies later
706
#pragma omp atomic
76,746,023 ✔
707
  simulation::total_weight += p.wgt();
153,212,924 ✔
708

709
  // Force calculation of cross-sections by setting last energy to zero
710
  if (settings::run_CE) {
153,212,924 ✔
711
    p.invalidate_neutron_xs();
51,372,924 ✔
712
  }
713

714
  // Prepare to write out particle track.
715
  if (p.write_track())
153,212,924 ✔
716
    add_particle_track(p);
930 ✔
717
}
153,212,924 ✔
718

719
int overall_generation()
178,152,721 ✔
720
{
721
  using namespace simulation;
722
  return settings::gen_per_batch * (current_batch - 1) + current_gen;
178,152,721 ✔
723
}
724

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

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

733
  int64_t i_bank = 0;
6,462 ✔
734
  simulation::work_index.resize(mpi::n_procs + 1);
6,462 ✔
735
  simulation::work_index[0] = 0;
6,462 ✔
736
  for (int i = 0; i < mpi::n_procs; ++i) {
14,750 ✔
737
    // Number of particles for rank i
738
    int64_t work_i = i < remainder ? min_work + 1 : min_work;
8,288 !
739

740
    // Set number of particles
741
    if (mpi::rank == i)
8,288 ✔
742
      simulation::work_per_rank = work_i;
6,462 ✔
743

744
    // Set index into source bank for rank i
745
    i_bank += work_i;
8,288 ✔
746
    simulation::work_index[i + 1] = i_bank;
8,288 ✔
747
  }
748
}
6,462 ✔
749

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

756
  for (const auto& nuc : data::nuclides) {
31,531 ✔
757
    if (nuc->grid_.size() >= 1) {
26,259 !
758
      int neutron = ParticleType::neutron().transport_index();
26,259 ✔
759
      data::energy_min[neutron] =
52,518 ✔
760
        std::max(data::energy_min[neutron], nuc->grid_[0].energy.front());
26,259 ✔
761
      data::energy_max[neutron] =
26,259 ✔
762
        std::min(data::energy_max[neutron], nuc->grid_[0].energy.back());
26,259 ✔
763
    }
764
  }
765

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

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

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

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

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

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

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

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

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

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

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

864
#endif
865

866
void free_memory_simulation()
7,429 ✔
867
{
868
  simulation::k_generation.clear();
7,429 ✔
869
  simulation::entropy.clear();
7,429 ✔
870
  simulation::decay_times.clear();
7,429 ✔
871
}
7,429 ✔
872

873
void transport_history_based_single_particle(Particle& p)
141,076,254 ✔
874
{
875
  while (p.alive()) {
2,147,483,647 ✔
876
    p.event_calculate_xs();
2,147,483,647 ✔
877
    if (p.alive()) {
2,147,483,647 !
878
      p.event_advance();
2,147,483,647 ✔
879
    }
880
    if (p.alive()) {
2,147,483,647 ✔
881
      if (p.collision_distance() > p.boundary().distance()) {
2,147,483,647 ✔
882
        p.event_cross_surface();
1,297,937,267 ✔
883
      } else if (p.alive()) {
2,147,483,647 !
884
        p.event_collide();
2,147,483,647 ✔
885
      }
886
    }
887
    p.event_revive_from_secondary();
2,147,483,647 ✔
888
  }
889
  p.event_death();
141,076,246 ✔
890
}
141,076,246 ✔
891

892
void transport_history_based()
120,073 ✔
893
{
894
#pragma omp parallel for schedule(runtime)
895
  for (int64_t i_work = 1; i_work <= simulation::work_per_rank; ++i_work) {
76,622,674 ✔
896
    Particle p;
76,561,088 ✔
897
    initialize_history(p, i_work);
76,561,088 ✔
898
    transport_history_based_single_particle(p);
76,561,085 ✔
899
  }
76,561,080 ✔
900
}
120,065 ✔
901

902
void transport_event_based()
3,172 ✔
903
{
904
  int64_t remaining_work = simulation::work_per_rank;
3,172 ✔
905
  int64_t source_offset = 0;
3,172 ✔
906

907
  // To cap the total amount of memory used to store particle object data, the
908
  // number of particles in flight at any point in time can bet set. In the case
909
  // that the maximum in flight particle count is lower than the total number
910
  // of particles that need to be run this iteration, the event-based transport
911
  // loop is executed multiple times until all particles have been completed.
912
  while (remaining_work > 0) {
6,344 ✔
913
    // Figure out # of particles to run for this subiteration
914
    int64_t n_particles =
915
      std::min(remaining_work, settings::max_particles_in_flight);
3,172 ✔
916

917
    // Initialize all particle histories for this subiteration
918
    process_init_events(n_particles, source_offset);
3,172 !
919

920
    // Event-based transport loop
921
    while (true) {
922
      // Determine which event kernel has the longest queue
923
      int64_t max = std::max({simulation::calculate_fuel_xs_queue.size(),
4,743,840 !
924
        simulation::calculate_nonfuel_xs_queue.size(),
2,371,920 ✔
925
        simulation::advance_particle_queue.size(),
2,371,920 ✔
926
        simulation::surface_crossing_queue.size(),
2,371,920 ✔
927
        simulation::collision_queue.size()});
2,371,920 ✔
928

929
      // Execute event with the longest queue
930
      if (max == 0) {
2,371,920 ✔
931
        break;
3,172 ✔
932
      } else if (max == simulation::calculate_fuel_xs_queue.size()) {
2,368,748 ✔
933
        process_calculate_xs_events(simulation::calculate_fuel_xs_queue);
425,668 !
934
      } else if (max == simulation::calculate_nonfuel_xs_queue.size()) {
1,943,080 ✔
935
        process_calculate_xs_events(simulation::calculate_nonfuel_xs_queue);
361,260 !
936
      } else if (max == simulation::advance_particle_queue.size()) {
1,581,820 ✔
937
        process_advance_particle_events();
781,657 !
938
      } else if (max == simulation::surface_crossing_queue.size()) {
800,163 ✔
939
        process_surface_crossing_events();
259,774 !
940
      } else if (max == simulation::collision_queue.size()) {
540,389 !
941
        process_collision_events();
540,389 !
942
      }
943
    }
2,368,748 ✔
944

945
    // Execute death event for all particles
946
    process_death_events(n_particles);
3,172 !
947

948
    // Adjust remaining work and source offset variables
949
    remaining_work -= n_particles;
3,172 ✔
950
    source_offset += n_particles;
3,172 ✔
951
  }
952
}
3,172 ✔
953

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

© 2026 Coveralls, Inc