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

openmc-dev / openmc / 23910205296

02 Apr 2026 04:14PM UTC coverage: 81.224% (-0.3%) from 81.567%
23910205296

Pull #3766

github

web-flow
Merge 264dcb1ef into 8223099ed
Pull Request #3766: Approximate multigroup velocity

17579 of 25426 branches covered (69.14%)

Branch coverage included in aggregate %.

24 of 25 new or added lines in 4 files covered. (96.0%)

710 existing lines in 29 files now uncovered.

58015 of 67642 relevant lines covered (85.77%)

31291841.44 hits per line

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

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

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

30
#ifdef _OPENMP
31
#include <omp.h>
32
#endif
33
#include "openmc/tensor.h"
34

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

39
#include <fmt/format.h>
40

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

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

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

53
int openmc_run()
3,334✔
54
{
55
  openmc::simulation::time_total.start();
3,334✔
56
  openmc_simulation_init();
3,334✔
57

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

65
  int err = 0;
66
  while (status == 0 && err == 0) {
74,610✔
67
    err = openmc_next_batch(&status);
71,283✔
68
  }
69

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

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

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

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

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

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

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

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

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

113
  // Set up material nuclide index mapping
114
  for (auto& mat : model::materials) {
14,140✔
115
    mat->init_nuclide_index();
10,211✔
116
  }
117

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

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

141
  // Display header
142
  if (mpi::master) {
3,929✔
143
    if (settings::run_mode == RunMode::FIXED_SOURCE) {
3,447✔
144
      if (settings::solver_type == SolverType::MONTE_CARLO) {
1,465✔
145
        header("FIXED SOURCE TRANSPORT SIMULATION", 3);
1,277✔
146
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
188!
147
        header("FIXED SOURCE TRANSPORT SIMULATION (RANDOM RAY SOLVER)", 3);
188✔
148
      }
149
    } else if (settings::run_mode == RunMode::EIGENVALUE) {
1,982!
150
      if (settings::solver_type == SolverType::MONTE_CARLO) {
1,982✔
151
        header("K EIGENVALUE SIMULATION", 3);
1,832✔
152
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
150!
153
        header("K EIGENVALUE SIMULATION (RANDOM RAY SOLVER)", 3);
150✔
154
      }
155
      if (settings::verbosity >= 7)
1,982✔
156
        print_columns();
1,870✔
157
    }
158
  }
159

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

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

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

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

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

182
  // Clear material nuclide mapping
183
  for (auto& mat : model::materials) {
14,126✔
184
    mat->mat_nuclide_index_.clear();
20,408!
185
  }
186

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

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

195
#ifdef OPENMC_MPI
196
  broadcast_results();
1,649✔
197
#endif
198

199
  // Write tally results to tallies.out
200
  if (settings::output_tallies && mpi::master)
3,922!
201
    write_tallies();
3,249✔
202

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

209
  // Deactivate all tallies
210
  for (auto& t : model::tallies) {
18,146✔
211
    t->active_ = false;
14,224✔
212
  }
213

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

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

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

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

244
  initialize_batch();
73,527✔
245

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

250
    initialize_generation();
73,639✔
251

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

255
    // Transport loop
256
    if (settings::event_based) {
73,639!
UNCOV
257
      transport_event_based();
×
258
    } else {
259
      transport_history_based();
73,639✔
260
    }
261

262
    // Accumulate time for transport
263
    simulation::time_transport.stop();
73,632✔
264

265
    finalize_generation();
73,632✔
266
  }
267

268
  finalize_batch();
73,520✔
269

270
  // Check simulation ending criteria
271
  if (status) {
73,520!
272
    if (simulation::current_batch >= settings::n_max_batches) {
73,520✔
273
      *status = STATUS_EXIT_MAX_BATCH;
3,433✔
274
    } else if (simulation::satisfy_triggers) {
70,087✔
275
      *status = STATUS_EXIT_ON_TRIGGER;
50✔
276
    } else {
277
      *status = STATUS_EXIT_NORMAL;
70,037✔
278
    }
279
  }
280
  return 0;
281
}
282

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

288
  if (!simulation::initialized)
1,710!
289
    return false;
290
  else
291
    return contains(settings::statepoint_batch, simulation::current_batch);
3,420✔
292
}
293

294
namespace openmc {
295

296
//==============================================================================
297
// Global variables
298
//==============================================================================
299

300
namespace simulation {
301

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

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

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

327
} // namespace simulation
328

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

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

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

343
    // Allocate IFP bank
344
    if (settings::ifp_on) {
2,130✔
345
      resize_simulation_ifp_banks();
40✔
346
    }
347
  }
348

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

354
  if (settings::collision_track) {
3,929✔
355
    // Allocate collision track bank
356
    collision_track_reserve_bank();
80✔
357
  }
358
}
3,929✔
359

360
void initialize_batch()
84,499✔
361
{
362
  // Increment current batch
363
  ++simulation::current_batch;
84,499✔
364
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
84,499✔
365
    if (settings::solver_type == SolverType::RANDOM_RAY &&
34,653✔
366
        simulation::current_batch < settings::n_inactive + 1) {
7,292✔
367
      write_message(
8,732✔
368
        6, "Simulating batch {:<4} (inactive)", simulation::current_batch);
369
    } else {
370
      write_message(6, "Simulating batch {}", simulation::current_batch);
60,574✔
371
    }
372
  }
373

374
  // Reset total starting particle weight used for normalizing tallies
375
  simulation::total_weight = 0.0;
84,499✔
376

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

388
  // Manage active/inactive timers and activate tallies if necessary.
389
  if (first_inactive) {
84,439✔
390
    simulation::time_inactive.start();
1,964✔
391
  } else if (first_active) {
82,535✔
392
    simulation::time_inactive.stop();
3,902✔
393
    simulation::time_active.start();
3,902✔
394
    for (auto& t : model::tallies) {
18,114✔
395
      t->active_ = true;
14,212✔
396
    }
397
  }
398

399
  // Add user tallies to active tallies list
400
  setup_active_tallies();
84,499✔
401
}
84,499✔
402

403
void finalize_batch()
84,492✔
404
{
405
  // Reduce tallies onto master process and accumulate
406
  simulation::time_tallies.start();
84,492✔
407
  accumulate_tallies();
84,492✔
408
  simulation::time_tallies.stop();
84,492✔
409

410
  // update weight windows if needed
411
  for (const auto& wwg : variance_reduction::weight_windows_generators) {
85,714✔
412
    wwg->update();
1,222✔
413
  }
414

415
  // Reset global tally results
416
  if (simulation::current_batch <= settings::n_inactive) {
84,492✔
417
    simulation::global_tallies.fill(0.0);
16,254✔
418
    simulation::n_realizations = 0;
16,254✔
419
  }
420

421
  // Check_triggers
422
  if (mpi::master)
84,492✔
423
    check_triggers();
75,145✔
424
#ifdef OPENMC_MPI
425
  MPI_Bcast(&simulation::satisfy_triggers, 1, MPI_C_BOOL, 0, mpi::intracomm);
34,756✔
426
#endif
427
  if (simulation::satisfy_triggers ||
84,492✔
428
      (settings::trigger_on &&
1,386✔
429
        simulation::current_batch == settings::n_max_batches)) {
1,386✔
430
    settings::statepoint_batch.insert(simulation::current_batch);
76✔
431
  }
432

433
  // Write out state point if it's been specified for this batch and is not
434
  // a CMFD run instance
435
  if (contains(settings::statepoint_batch, simulation::current_batch) &&
168,984✔
436
      !settings::cmfd_run) {
4,041✔
437
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
7,776✔
438
        settings::source_write && !settings::source_separate) {
7,328✔
439
      bool b = (settings::run_mode == RunMode::EIGENVALUE);
3,345✔
440
      openmc_statepoint_write(nullptr, &b);
3,345✔
441
    } else {
442
      bool b = false;
600✔
443
      openmc_statepoint_write(nullptr, &b);
600✔
444
    }
445
  }
446

447
  if (settings::run_mode == RunMode::EIGENVALUE) {
84,492✔
448
    // Write out a separate source point if it's been specified for this batch
449
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
52,200✔
450
        settings::source_write && settings::source_separate) {
52,002✔
451

452
      // Determine width for zero padding
453
      int w = std::to_string(settings::n_max_batches).size();
38✔
454
      std::string source_point_filename = fmt::format("{0}source.{1:0{2}}",
38✔
455
        settings::path_output, simulation::current_batch, w);
38✔
456
      span<SourceSite> bankspan(simulation::source_bank);
38✔
457
      write_source_point(source_point_filename, bankspan,
76✔
458
        simulation::work_index, settings::source_mcpl_write);
459
    }
38✔
460

461
    // Write a continously-overwritten source point if requested.
462
    if (settings::source_latest) {
49,846✔
463
      auto filename = settings::path_output + "source";
80✔
464
      span<SourceSite> bankspan(simulation::source_bank);
80✔
465
      write_source_point(filename, bankspan, simulation::work_index,
160✔
466
        settings::source_mcpl_write);
467
    }
80✔
468
  }
469

470
  // Write out surface source if requested.
471
  if (settings::surf_source_write &&
84,492✔
472
      simulation::ssw_current_file <= settings::ssw_max_files) {
5,164✔
473
    bool last_batch = (simulation::current_batch == settings::n_batches);
1,048✔
474
    if (simulation::surf_source_bank.full() || last_batch) {
1,048✔
475
      // Determine appropriate filename
476
      auto filename = fmt::format("{}surface_source.{}", settings::path_output,
568✔
477
        simulation::current_batch);
568✔
478
      if (settings::ssw_max_files == 1 ||
568✔
479
          (simulation::ssw_current_file == 1 && last_batch)) {
30!
480
        filename = settings::path_output + "surface_source";
538✔
481
      }
482

483
      // Get span of source bank and calculate parallel index vector
484
      auto surf_work_index = mpi::calculate_parallel_index_vector(
568✔
485
        simulation::surf_source_bank.size());
568✔
486
      span<SourceSite> surfbankspan(simulation::surf_source_bank.begin(),
568✔
487
        simulation::surf_source_bank.size());
568✔
488

489
      // Write surface source file
490
      write_source_point(
568✔
491
        filename, surfbankspan, surf_work_index, settings::surf_mcpl_write);
492

493
      // Reset surface source bank and increment counter
494
      simulation::surf_source_bank.clear();
568✔
495
      if (!last_batch && settings::ssw_max_files >= 1) {
568!
496
        simulation::surf_source_bank.reserve(settings::ssw_max_particles);
461✔
497
      }
498
      ++simulation::ssw_current_file;
568✔
499
    }
568✔
500
  }
501
  // Write collision track file if requested
502
  if (settings::collision_track) {
84,492✔
503
    collision_track_flush_bank();
328✔
504
  }
505
}
84,492✔
506

507
void initialize_generation()
84,611✔
508
{
509
  if (settings::run_mode == RunMode::EIGENVALUE) {
84,611✔
510
    // Clear out the fission bank
511
    simulation::fission_bank.resize(0);
49,958✔
512

513
    // Count source sites if using uniform fission source weighting
514
    if (settings::ufs_on)
49,958✔
515
      ufs_count_sites();
80✔
516

517
    // Store current value of tracklength k
518
    simulation::keff_generation = simulation::global_tallies(
49,958✔
519
      GlobalTally::K_TRACKLENGTH, TallyResult::VALUE);
520
  }
521
}
84,611✔
522

523
void finalize_generation()
84,604✔
524
{
525
  auto& gt = simulation::global_tallies;
84,604✔
526

527
  // Update global tallies with the accumulation variables
528
  if (settings::run_mode == RunMode::EIGENVALUE) {
84,604✔
529
    gt(GlobalTally::K_COLLISION, TallyResult::VALUE) += global_tally_collision;
49,958✔
530
    gt(GlobalTally::K_ABSORPTION, TallyResult::VALUE) +=
49,958✔
531
      global_tally_absorption;
532
    gt(GlobalTally::K_TRACKLENGTH, TallyResult::VALUE) +=
49,958✔
533
      global_tally_tracklength;
534
  }
535
  gt(GlobalTally::LEAKAGE, TallyResult::VALUE) += global_tally_leakage;
84,604✔
536

537
  // reset tallies
538
  if (settings::run_mode == RunMode::EIGENVALUE) {
84,604✔
539
    global_tally_collision = 0.0;
49,958✔
540
    global_tally_absorption = 0.0;
49,958✔
541
    global_tally_tracklength = 0.0;
49,958✔
542
  }
543
  global_tally_leakage = 0.0;
84,604✔
544

545
  if (settings::run_mode == RunMode::EIGENVALUE &&
84,604✔
546
      settings::solver_type == SolverType::MONTE_CARLO) {
49,958✔
547
    // If using shared memory, stable sort the fission bank (by parent IDs)
548
    // so as to allow for reproducibility regardless of which order particles
549
    // are run in.
550
    sort_fission_bank();
46,278✔
551

552
    // Distribute fission bank across processors evenly
553
    synchronize_bank();
46,278✔
554
  }
555

556
  if (settings::run_mode == RunMode::EIGENVALUE) {
84,604✔
557

558
    // Calculate shannon entropy
559
    if (settings::entropy_on &&
49,958✔
560
        settings::solver_type == SolverType::MONTE_CARLO)
7,870✔
561
      shannon_entropy();
4,190✔
562

563
    // Collect results and statistics
564
    calculate_generation_keff();
49,958✔
565
    calculate_average_keff();
49,958✔
566

567
    // Write generation output
568
    if (mpi::master && settings::verbosity >= 7) {
49,958✔
569
      print_generation();
41,503✔
570
    }
571
  }
572
}
84,604✔
573

574
void initialize_history(Particle& p, int64_t index_source)
92,231,075✔
575
{
576
  // set defaults
577
  if (settings::run_mode == RunMode::EIGENVALUE) {
92,231,075✔
578
    // set defaults for eigenvalue simulations from primary bank
579
    p.from_source(&simulation::source_bank[index_source - 1]);
77,211,700✔
580
  } else if (settings::run_mode == RunMode::FIXED_SOURCE) {
15,019,375!
581
    // initialize random number seed
582
    int64_t id = (simulation::total_gen + overall_generation() - 1) *
15,019,375✔
583
                   settings::n_particles +
15,019,375✔
584
                 simulation::work_index[mpi::rank] + index_source;
15,019,375✔
585
    uint64_t seed = init_seed(id, STREAM_SOURCE);
15,019,375✔
586
    // sample from external source distribution or custom library then set
587
    auto site = sample_external_source(&seed);
15,019,375✔
588
    p.from_source(&site);
15,019,372✔
589
  }
590
  p.current_work() = index_source;
92,231,072✔
591

592
  // set identifier for particle
593
  p.id() = simulation::work_index[mpi::rank] + index_source;
92,231,072✔
594

595
  // set progeny count to zero
596
  p.n_progeny() = 0;
92,231,072✔
597

598
  // Reset particle event counter
599
  p.n_event() = 0;
92,231,072✔
600

601
  // Reset split counter
602
  p.n_split() = 0;
92,231,072✔
603

604
  // Reset weight window ratio
605
  p.ww_factor() = 0.0;
92,231,072✔
606

607
  // set particle history start weight
608
  p.wgt_born() = p.wgt();
92,231,072✔
609

610
  // Reset pulse_height_storage
611
  std::fill(p.pht_storage().begin(), p.pht_storage().end(), 0);
92,231,072✔
612

613
  // set random number seed
614
  int64_t particle_seed =
92,231,072✔
615
    (simulation::total_gen + overall_generation() - 1) * settings::n_particles +
92,231,072✔
616
    p.id();
92,231,072✔
617
  init_particle_seeds(particle_seed, p.seeds());
92,231,072✔
618

619
  // set particle trace
620
  p.trace() = false;
92,231,072✔
621
  if (simulation::current_batch == settings::trace_batch &&
92,237,072✔
622
      simulation::current_gen == settings::trace_gen &&
92,231,072!
623
      p.id() == settings::trace_particle)
6,000✔
624
    p.trace() = true;
6✔
625

626
  // Set particle track.
627
  p.write_track() = check_track_criteria(p);
92,231,072✔
628

629
  // Set the particle's initial weight window value.
630
  p.wgt_ww_born() = -1.0;
92,231,072✔
631
  apply_weight_windows(p);
92,231,072✔
632

633
  // Display message if high verbosity or trace is on
634
  if (settings::verbosity >= 9 || p.trace()) {
92,231,072!
635
    write_message("Simulating Particle {}", p.id());
12✔
636
  }
637

638
// Add particle's starting weight to count for normalizing tallies later
639
#pragma omp atomic
46,231,753✔
640
  simulation::total_weight += p.wgt();
92,231,072✔
641

642
  // Force calculation of cross-sections by setting last energy to zero
643
  if (settings::run_CE) {
92,231,072✔
644
    p.invalidate_neutron_xs();
31,126,892✔
645
  }
646

647
  // Prepare to write out particle track.
648
  if (p.write_track())
92,231,072✔
649
    add_particle_track(p);
534✔
650
}
92,231,072✔
651

652
int overall_generation()
107,388,314✔
653
{
654
  using namespace simulation;
107,388,314✔
655
  return settings::gen_per_batch * (current_batch - 1) + current_gen;
107,388,314✔
656
}
657

658
void calculate_work()
3,929✔
659
{
660
  // Determine minimum amount of particles to simulate on each processor
661
  int64_t min_work = settings::n_particles / mpi::n_procs;
3,929✔
662

663
  // Determine number of processors that have one extra particle
664
  int64_t remainder = settings::n_particles % mpi::n_procs;
3,929✔
665

666
  int64_t i_bank = 0;
3,929✔
667
  simulation::work_index.resize(mpi::n_procs + 1);
3,929✔
668
  simulation::work_index[0] = 0;
3,929✔
669
  for (int i = 0; i < mpi::n_procs; ++i) {
8,821✔
670
    // Number of particles for rank i
671
    int64_t work_i = i < remainder ? min_work + 1 : min_work;
4,892!
672

673
    // Set number of particles
674
    if (mpi::rank == i)
4,892✔
675
      simulation::work_per_rank = work_i;
3,929✔
676

677
    // Set index into source bank for rank i
678
    i_bank += work_i;
4,892✔
679
    simulation::work_index[i + 1] = i_bank;
4,892✔
680
  }
681
}
3,929✔
682

683
void initialize_data()
3,233✔
684
{
685
  // Determine minimum/maximum energy for incident neutron/photon data
686
  data::energy_max = {INFTY, INFTY, INFTY, INFTY};
3,233✔
687
  data::energy_min = {0.0, 0.0, 0.0, 0.0};
3,233✔
688

689
  for (const auto& nuc : data::nuclides) {
19,174✔
690
    if (nuc->grid_.size() >= 1) {
15,941!
691
      int neutron = ParticleType::neutron().transport_index();
15,941✔
692
      data::energy_min[neutron] =
15,941✔
693
        std::max(data::energy_min[neutron], nuc->grid_[0].energy.front());
18,890✔
694
      data::energy_max[neutron] =
15,941✔
695
        std::min(data::energy_max[neutron], nuc->grid_[0].energy.back());
19,650✔
696
    }
697
  }
698

699
  if (settings::photon_transport) {
3,233✔
700
    for (const auto& elem : data::elements) {
494✔
701
      if (elem->energy_.size() >= 1) {
332!
702
        int photon = ParticleType::photon().transport_index();
332✔
703
        int n = elem->energy_.size();
332✔
704
        data::energy_min[photon] =
664✔
705
          std::max(data::energy_min[photon], std::exp(elem->energy_(1)));
568✔
706
        data::energy_max[photon] =
332✔
707
          std::min(data::energy_max[photon], std::exp(elem->energy_(n - 1)));
494✔
708
      }
709
    }
710

711
    if (settings::electron_treatment == ElectronTreatment::TTB) {
162✔
712
      // Determine if minimum/maximum energy for bremsstrahlung is greater/less
713
      // than the current minimum/maximum
714
      if (data::ttb_e_grid.size() >= 1) {
142!
715
        int photon = ParticleType::photon().transport_index();
142✔
716
        int electron = ParticleType::electron().transport_index();
142✔
717
        int positron = ParticleType::positron().transport_index();
142✔
718
        int n_e = data::ttb_e_grid.size();
142✔
719

720
        const std::vector<int> charged = {electron, positron};
142✔
721
        for (auto t : charged) {
426✔
722
          data::energy_min[t] = std::exp(data::ttb_e_grid(1));
284✔
723
          data::energy_max[t] = std::exp(data::ttb_e_grid(n_e - 1));
284✔
724
        }
725

726
        data::energy_min[photon] =
284✔
727
          std::max(data::energy_min[photon], data::energy_min[electron]);
284!
728

729
        data::energy_max[photon] =
284✔
730
          std::min(data::energy_max[photon], data::energy_max[electron]);
284!
731
      }
142✔
732
    }
733
  }
734

735
  // Show which nuclide results in lowest energy for neutron transport
736
  for (const auto& nuc : data::nuclides) {
4,025✔
737
    // If a nuclide is present in a material that's not used in the model, its
738
    // grid has not been allocated
739
    if (nuc->grid_.size() > 0) {
3,741!
740
      double max_E = nuc->grid_[0].energy.back();
3,741✔
741
      int neutron = ParticleType::neutron().transport_index();
3,741✔
742
      if (max_E == data::energy_max[neutron]) {
3,741✔
743
        write_message(7, "Maximum neutron transport energy: {} eV for {}",
2,949✔
744
          data::energy_max[neutron], nuc->name_);
2,949✔
745
        if (mpi::master && data::energy_max[neutron] < 20.0e6) {
2,949!
746
          warning("Maximum neutron energy is below 20 MeV. This may bias "
×
747
                  "the results.");
748
        }
749
        break;
750
      }
751
    }
752
  }
753

754
  // Set up logarithmic grid for nuclides
755
  for (auto& nuc : data::nuclides) {
19,174✔
756
    nuc->init_grid();
15,941✔
757
  }
758
  int neutron = ParticleType::neutron().transport_index();
3,233✔
759
  simulation::log_spacing =
6,466✔
760
    std::log(data::energy_max[neutron] / data::energy_min[neutron]) /
3,233✔
761
    settings::n_log_bins;
762
}
3,233✔
763

764
#ifdef OPENMC_MPI
765
void broadcast_results()
1,649✔
766
{
767
  // Broadcast tally results so that each process has access to results
768
  for (auto& t : model::tallies) {
8,271✔
769
    // Create a new datatype that consists of all values for a given filter
770
    // bin and then use that to broadcast. This is done to minimize the
771
    // chance of the 'count' argument of MPI_BCAST exceeding 2**31
772
    auto& results = t->results_;
6,622✔
773

774
    auto shape = results.shape();
6,622✔
775
    int count_per_filter = shape[1] * shape[2];
6,622✔
776
    MPI_Datatype result_block;
6,622✔
777
    MPI_Type_contiguous(count_per_filter, MPI_DOUBLE, &result_block);
6,622✔
778
    MPI_Type_commit(&result_block);
6,622✔
779
    MPI_Bcast(results.data(), shape[0], result_block, 0, mpi::intracomm);
6,622✔
780
    MPI_Type_free(&result_block);
6,622✔
781
  }
6,622✔
782

783
  // Also broadcast global tally results
784
  auto& gt = simulation::global_tallies;
1,649✔
785
  MPI_Bcast(gt.data(), gt.size(), MPI_DOUBLE, 0, mpi::intracomm);
1,649✔
786

787
  // These guys are needed so that non-master processes can calculate the
788
  // combined estimate of k-effective
789
  double temp[] {
1,649✔
790
    simulation::k_col_abs, simulation::k_col_tra, simulation::k_abs_tra};
1,649✔
791
  MPI_Bcast(temp, 3, MPI_DOUBLE, 0, mpi::intracomm);
1,649✔
792
  simulation::k_col_abs = temp[0];
1,649✔
793
  simulation::k_col_tra = temp[1];
1,649✔
794
  simulation::k_abs_tra = temp[2];
1,649✔
795
}
1,649✔
796

797
#endif
798

799
void free_memory_simulation()
4,554✔
800
{
801
  simulation::k_generation.clear();
4,554✔
802
  simulation::entropy.clear();
4,554✔
803
}
4,554✔
804

805
void transport_history_based_single_particle(Particle& p)
92,231,090✔
806
{
807
  while (p.alive()) {
2,147,483,647✔
808
    p.event_calculate_xs();
2,147,483,647✔
809
    if (p.alive()) {
2,147,483,647!
810
      p.event_advance();
2,147,483,647✔
811
    }
812
    if (p.alive()) {
2,147,483,647✔
813
      if (p.collision_distance() > p.boundary().distance()) {
2,147,483,647✔
814
        p.event_cross_surface();
817,941,587✔
815
      } else if (p.alive()) {
1,559,849,049✔
816
        p.event_collide();
1,559,849,049✔
817
      }
818
    }
819
    p.event_revive_from_secondary();
2,147,483,647✔
820
  }
821
  p.event_death();
92,231,086✔
822
}
92,231,086✔
823

824
void transport_history_based()
73,639✔
825
{
826
#pragma omp parallel for schedule(runtime)
40,591✔
827
  for (int64_t i_work = 1; i_work <= simulation::work_per_rank; ++i_work) {
46,054,704✔
828
    Particle p;
46,021,662✔
829
    initialize_history(p, i_work);
46,021,662✔
830
    transport_history_based_single_particle(p);
46,021,659✔
831
  }
46,021,656✔
832
}
73,633✔
833

UNCOV
834
void transport_event_based()
×
835
{
UNCOV
836
  int64_t remaining_work = simulation::work_per_rank;
×
UNCOV
837
  int64_t source_offset = 0;
×
838

839
  // To cap the total amount of memory used to store particle object data, the
840
  // number of particles in flight at any point in time can bet set. In the case
841
  // that the maximum in flight particle count is lower than the total number
842
  // of particles that need to be run this iteration, the event-based transport
843
  // loop is executed multiple times until all particles have been completed.
UNCOV
844
  while (remaining_work > 0) {
×
845
    // Figure out # of particles to run for this subiteration
UNCOV
846
    int64_t n_particles =
×
UNCOV
847
      std::min(remaining_work, settings::max_particles_in_flight);
×
848

849
    // Initialize all particle histories for this subiteration
UNCOV
850
    process_init_events(n_particles, source_offset);
×
851

852
    // Event-based transport loop
UNCOV
853
    while (true) {
×
854
      // Determine which event kernel has the longest queue
UNCOV
855
      int64_t max = std::max({simulation::calculate_fuel_xs_queue.size(),
×
UNCOV
856
        simulation::calculate_nonfuel_xs_queue.size(),
×
UNCOV
857
        simulation::advance_particle_queue.size(),
×
UNCOV
858
        simulation::surface_crossing_queue.size(),
×
UNCOV
859
        simulation::collision_queue.size()});
×
860

861
      // Execute event with the longest queue
UNCOV
862
      if (max == 0) {
×
863
        break;
UNCOV
864
      } else if (max == simulation::calculate_fuel_xs_queue.size()) {
×
UNCOV
865
        process_calculate_xs_events(simulation::calculate_fuel_xs_queue);
×
UNCOV
866
      } else if (max == simulation::calculate_nonfuel_xs_queue.size()) {
×
UNCOV
867
        process_calculate_xs_events(simulation::calculate_nonfuel_xs_queue);
×
UNCOV
868
      } else if (max == simulation::advance_particle_queue.size()) {
×
UNCOV
869
        process_advance_particle_events();
×
UNCOV
870
      } else if (max == simulation::surface_crossing_queue.size()) {
×
UNCOV
871
        process_surface_crossing_events();
×
UNCOV
872
      } else if (max == simulation::collision_queue.size()) {
×
UNCOV
873
        process_collision_events();
×
874
      }
875
    }
876

877
    // Execute death event for all particles
UNCOV
878
    process_death_events(n_particles);
×
879

880
    // Adjust remaining work and source offset variables
UNCOV
881
    remaining_work -= n_particles;
×
UNCOV
882
    source_offset += n_particles;
×
883
  }
UNCOV
884
}
×
885

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