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

openmc-dev / openmc / 22921477266

10 Mar 2026 07:52PM UTC coverage: 81.549% (-0.2%) from 81.721%
22921477266

Pull #3810

github

web-flow
Merge e43e6e7bc into 1dc4aa988
Pull Request #3810: Implementation of migration area score

17578 of 25300 branches covered (69.48%)

Branch coverage included in aggregate %.

37 of 52 new or added lines in 5 files covered. (71.15%)

2158 existing lines in 69 files now uncovered.

57994 of 67371 relevant lines covered (86.08%)

46104384.03 hits per line

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

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

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

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

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

39
#include <fmt/format.h>
40

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

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

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

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

65
  int err = 0;
66
  while (status == 0 && err == 0) {
137,319✔
67
    err = openmc_next_batch(&status);
131,236✔
68
  }
69

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

75
int openmc_simulation_init()
7,223✔
76
{
77
  using namespace openmc;
7,223✔
78

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

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

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

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

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

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

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

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

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

141
  // Display header
142
  if (mpi::master) {
7,201✔
143
    if (settings::run_mode == RunMode::FIXED_SOURCE) {
6,253✔
144
      if (settings::solver_type == SolverType::MONTE_CARLO) {
2,654✔
145
        header("FIXED SOURCE TRANSPORT SIMULATION", 3);
2,311✔
146
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
343!
147
        header("FIXED SOURCE TRANSPORT SIMULATION (RANDOM RAY SOLVER)", 3);
343✔
148
      }
149
    } else if (settings::run_mode == RunMode::EIGENVALUE) {
3,599!
150
      if (settings::solver_type == SolverType::MONTE_CARLO) {
3,599✔
151
        header("K EIGENVALUE SIMULATION", 3);
3,324✔
152
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
275!
153
        header("K EIGENVALUE SIMULATION (RANDOM RAY SOLVER)", 3);
275✔
154
      }
155
      if (settings::verbosity >= 7)
3,599✔
156
        print_columns();
3,395✔
157
    }
158
  }
159

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

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

170
int openmc_simulation_finalize()
7,188✔
171
{
172
  using namespace openmc;
7,188✔
173

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

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

182
  // Clear material nuclide mapping
183
  for (auto& mat : model::materials) {
25,796✔
184
    mat->mat_nuclide_index_.clear();
37,216!
185
  }
186

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

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

195
#ifdef OPENMC_MPI
196
  broadcast_results();
3,237✔
197
#endif
198

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

209
  // Deactivate all tallies
210
  for (auto& t : model::tallies) {
33,652✔
211
    t->active_ = false;
26,464✔
212
  }
213

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

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

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

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

244
  initialize_batch();
135,350✔
245

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

250
    initialize_generation();
135,560✔
251

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

255
    // Transport loop
256
    if (settings::event_based) {
135,560✔
257
      transport_event_based();
3,203✔
258
    } else {
259
      transport_history_based();
132,357✔
260
    }
261

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

265
    finalize_generation();
135,547✔
266
  }
267

268
  finalize_batch();
135,337✔
269

270
  // Check simulation ending criteria
271
  if (status) {
135,337!
272
    if (simulation::current_batch >= settings::n_max_batches) {
135,337✔
273
      *status = STATUS_EXIT_MAX_BATCH;
6,276✔
274
    } else if (simulation::satisfy_triggers) {
129,061✔
275
      *status = STATUS_EXIT_ON_TRIGGER;
93✔
276
    } else {
277
      *status = STATUS_EXIT_NORMAL;
128,968✔
278
    }
279
  }
280
  return 0;
281
}
282

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

288
  if (!simulation::initialized)
3,135!
289
    return false;
290
  else
291
    return contains(settings::statepoint_batch, simulation::current_batch);
6,270✔
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
bool migration_present {false};
313
int n_lost_particles {0};
314
bool need_depletion_rx {false};
315
bool nonvacuum_boundary_present {false};
316
int restart_batch;
317
bool satisfy_triggers {false};
318
int ssw_current_file;
319
int total_gen {0};
320
double total_weight;
321
int64_t work_per_rank;
322

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

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

329
} // namespace simulation
330

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

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

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

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

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

356
  if (settings::collision_track) {
7,201✔
357
    // Allocate collision track bank
358
    collision_track_reserve_bank();
148✔
359
  }
360
}
7,201✔
361

362
void initialize_batch()
155,862✔
363
{
364
  // Increment current batch
365
  ++simulation::current_batch;
155,862✔
366
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
155,862✔
367
    if (settings::solver_type == SolverType::RANDOM_RAY &&
63,529✔
368
        simulation::current_batch < settings::n_inactive + 1) {
13,662✔
369
      write_message(
16,362✔
370
        6, "Simulating batch {:<4} (inactive)", simulation::current_batch);
371
    } else {
372
      write_message(6, "Simulating batch {}", simulation::current_batch);
110,696✔
373
    }
374
  }
375

376
  // Reset total starting particle weight used for normalizing tallies
377
  simulation::total_weight = 0.0;
155,862✔
378

379
  // Determine if this batch is the first inactive or active batch.
380
  bool first_inactive = false;
155,862✔
381
  bool first_active = false;
155,862✔
382
  if (!settings::restart_run) {
155,862✔
383
    first_inactive = settings::n_inactive > 0 && simulation::current_batch == 1;
155,699✔
384
    first_active = simulation::current_batch == settings::n_inactive + 1;
155,699✔
385
  } else if (simulation::current_batch == simulation::restart_batch + 1) {
163✔
386
    first_inactive = simulation::restart_batch < settings::n_inactive;
52✔
387
    first_active = !first_inactive;
52✔
388
  }
389

390
  // Manage active/inactive timers and activate tallies if necessary.
391
  if (first_inactive) {
155,751✔
392
    simulation::time_inactive.start();
3,624✔
393
  } else if (first_active) {
152,238✔
394
    simulation::time_inactive.stop();
7,154✔
395
    simulation::time_active.start();
7,154✔
396
    for (auto& t : model::tallies) {
33,596✔
397
      t->active_ = true;
26,442✔
398
    }
399
  }
400

401
  // Add user tallies to active tallies list
402
  setup_active_tallies();
155,862✔
403
}
155,862✔
404

405
void finalize_batch()
155,849✔
406
{
407
  // Reduce tallies onto master process and accumulate
408
  simulation::time_tallies.start();
155,849✔
409
  accumulate_tallies();
155,849✔
410
  simulation::time_tallies.stop();
155,849✔
411

412
  // update weight windows if needed
413
  for (const auto& wwg : variance_reduction::weight_windows_generators) {
158,126✔
414
    wwg->update();
2,277✔
415
  }
416

417
  // Reset global tally results
418
  if (simulation::current_batch <= settings::n_inactive) {
155,849✔
419
    simulation::global_tallies.fill(0.0);
30,163✔
420
    simulation::n_realizations = 0;
30,163✔
421
  }
422

423
  // Check_triggers
424
  if (mpi::master)
155,849✔
425
    check_triggers();
137,250✔
426
#ifdef OPENMC_MPI
427
  MPI_Bcast(&simulation::satisfy_triggers, 1, MPI_C_BOOL, 0, mpi::intracomm);
68,834✔
428
#endif
429
  if (simulation::satisfy_triggers ||
155,849✔
430
      (settings::trigger_on &&
2,567✔
431
        simulation::current_batch == settings::n_max_batches)) {
2,567✔
432
    settings::statepoint_batch.insert(simulation::current_batch);
141✔
433
  }
434

435
  // Write out state point if it's been specified for this batch and is not
436
  // a CMFD run instance
437
  if (contains(settings::statepoint_batch, simulation::current_batch) &&
311,698✔
438
      !settings::cmfd_run) {
7,412✔
439
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
14,260✔
440
        settings::source_write && !settings::source_separate) {
13,422✔
441
      bool b = (settings::run_mode == RunMode::EIGENVALUE);
6,115✔
442
      openmc_statepoint_write(nullptr, &b);
6,115✔
443
    } else {
444
      bool b = false;
1,121✔
445
      openmc_statepoint_write(nullptr, &b);
1,121✔
446
    }
447
  }
448

449
  if (settings::run_mode == RunMode::EIGENVALUE) {
155,849✔
450
    // Write out a separate source point if it's been specified for this batch
451
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
96,666✔
452
        settings::source_write && settings::source_separate) {
96,295✔
453

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

463
    // Write a continously-overwritten source point if requested.
464
    if (settings::source_latest) {
92,333✔
465
      auto filename = settings::path_output + "source";
150✔
466
      span<SourceSite> bankspan(simulation::source_bank);
150✔
467
      write_source_point(filename, bankspan, simulation::work_index,
300✔
468
        settings::source_mcpl_write);
469
    }
150✔
470
  }
471

472
  // Write out surface source if requested.
473
  if (settings::surf_source_write &&
155,849✔
474
      simulation::ssw_current_file <= settings::ssw_max_files) {
9,309✔
475
    bool last_batch = (simulation::current_batch == settings::n_batches);
1,800✔
476
    if (simulation::surf_source_bank.full() || last_batch) {
1,800✔
477
      // Determine appropriate filename
478
      auto filename = fmt::format("{}surface_source.{}", settings::path_output,
1,011✔
479
        simulation::current_batch);
1,011✔
480
      if (settings::ssw_max_files == 1 ||
1,011✔
481
          (simulation::ssw_current_file == 1 && last_batch)) {
55!
482
        filename = settings::path_output + "surface_source";
956✔
483
      }
484

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

491
      // Write surface source file
492
      write_source_point(
1,011✔
493
        filename, surfbankspan, surf_work_index, settings::surf_mcpl_write);
494

495
      // Reset surface source bank and increment counter
496
      simulation::surf_source_bank.clear();
1,011✔
497
      if (!last_batch && settings::ssw_max_files >= 1) {
1,011!
498
        simulation::surf_source_bank.reserve(settings::ssw_max_particles);
829✔
499
      }
500
      ++simulation::ssw_current_file;
1,011✔
501
    }
1,011✔
502
  }
503
  // Write collision track file if requested
504
  if (settings::collision_track) {
155,849✔
505
    collision_track_flush_bank();
608✔
506
  }
507
}
155,849✔
508

509
void initialize_generation()
156,072✔
510
{
511
  if (settings::run_mode == RunMode::EIGENVALUE) {
156,072✔
512
    // Clear out the fission bank
513
    simulation::fission_bank.resize(0);
92,543✔
514

515
    // Count source sites if using uniform fission source weighting
516
    if (settings::ufs_on)
92,543✔
517
      ufs_count_sites();
150✔
518

519
    // Store current value of tracklength k
520
    simulation::keff_generation = simulation::global_tallies(
92,543✔
521
      GlobalTally::K_TRACKLENGTH, TallyResult::VALUE);
522
  }
523
}
156,072✔
524

525
void finalize_generation()
156,059✔
526
{
527
  auto& gt = simulation::global_tallies;
156,059✔
528

529
  // Update global tallies with the accumulation variables
530
  if (settings::run_mode == RunMode::EIGENVALUE) {
156,059✔
531
    gt(GlobalTally::K_COLLISION, TallyResult::VALUE) += global_tally_collision;
92,543✔
532
    gt(GlobalTally::K_ABSORPTION, TallyResult::VALUE) +=
92,543✔
533
      global_tally_absorption;
534
    gt(GlobalTally::K_TRACKLENGTH, TallyResult::VALUE) +=
92,543✔
535
      global_tally_tracklength;
536
  }
537
  gt(GlobalTally::LEAKAGE, TallyResult::VALUE) += global_tally_leakage;
156,059✔
538

539
  // reset tallies
540
  if (settings::run_mode == RunMode::EIGENVALUE) {
156,059✔
541
    global_tally_collision = 0.0;
92,543✔
542
    global_tally_absorption = 0.0;
92,543✔
543
    global_tally_tracklength = 0.0;
92,543✔
544
  }
545
  global_tally_leakage = 0.0;
156,059✔
546

547
  if (settings::run_mode == RunMode::EIGENVALUE &&
156,059✔
548
      settings::solver_type == SolverType::MONTE_CARLO) {
92,543✔
549
    // If using shared memory, stable sort the fission bank (by parent IDs)
550
    // so as to allow for reproducibility regardless of which order particles
551
    // are run in.
552
    sort_fission_bank();
85,693✔
553

554
    // Distribute fission bank across processors evenly
555
    synchronize_bank();
85,693✔
556
  }
557

558
  if (settings::run_mode == RunMode::EIGENVALUE) {
156,059✔
559

560
    // Calculate shannon entropy
561
    if (settings::entropy_on &&
92,543✔
562
        settings::solver_type == SolverType::MONTE_CARLO)
14,535✔
563
      shannon_entropy();
7,685✔
564

565
    // Collect results and statistics
566
    calculate_generation_keff();
92,543✔
567
    calculate_average_keff();
92,543✔
568

569
    // Write generation output
570
    if (mpi::master && settings::verbosity >= 7) {
92,543✔
571
      print_generation();
75,918✔
572
    }
573
  }
574
}
156,059✔
575

576
void initialize_history(Particle& p, int64_t index_source)
171,112,951✔
577
{
578
  // set defaults
579
  if (settings::run_mode == RunMode::EIGENVALUE) {
171,112,951✔
580
    // set defaults for eigenvalue simulations from primary bank
581
    p.from_source(&simulation::source_bank[index_source - 1]);
141,534,700✔
582
  } else if (settings::run_mode == RunMode::FIXED_SOURCE) {
29,578,251!
583
    // initialize random number seed
584
    int64_t id = (simulation::total_gen + overall_generation() - 1) *
29,578,251✔
585
                   settings::n_particles +
29,578,251✔
586
                 simulation::work_index[mpi::rank] + index_source;
29,578,251✔
587
    uint64_t seed = init_seed(id, STREAM_SOURCE);
29,578,251✔
588
    // sample from external source distribution or custom library then set
589
    auto site = sample_external_source(&seed);
29,578,251✔
590
    p.from_source(&site);
29,578,247✔
591
  }
592
  p.current_work() = index_source;
171,112,947✔
593

594
  // set identifier for particle
595
  p.id() = simulation::work_index[mpi::rank] + index_source;
171,112,947✔
596

597
  // set progeny count to zero
598
  p.n_progeny() = 0;
171,112,947✔
599

600
  // Reset particle event counter
601
  p.n_event() = 0;
171,112,947✔
602

603
  // Reset split counter
604
  p.n_split() = 0;
171,112,947✔
605

606
  // Reset weight window ratio
607
  p.ww_factor() = 0.0;
171,112,947✔
608

609
  // set particle history start weight
610
  p.wgt_born() = p.wgt();
171,112,947✔
611

612
  // Reset pulse_height_storage
613
  std::fill(p.pht_storage().begin(), p.pht_storage().end(), 0);
171,112,947✔
614

615
  // set random number seed
616
  int64_t particle_seed =
171,112,947✔
617
    (simulation::total_gen + overall_generation() - 1) * settings::n_particles +
171,112,947✔
618
    p.id();
171,112,947✔
619
  init_particle_seeds(particle_seed, p.seeds());
171,112,947✔
620

621
  // set particle trace
622
  p.trace() = false;
171,112,947✔
623
  if (simulation::current_batch == settings::trace_batch &&
171,123,947✔
624
      simulation::current_gen == settings::trace_gen &&
171,112,947!
625
      p.id() == settings::trace_particle)
11,000✔
626
    p.trace() = true;
11✔
627

628
  // Set particle track.
629
  p.write_track() = check_track_criteria(p);
171,112,947✔
630

631
  // Set the particle's initial weight window value.
632
  p.wgt_ww_born() = -1.0;
171,112,947✔
633
  apply_weight_windows(p);
171,112,947✔
634

635
  // Display message if high verbosity or trace is on
636
  if (settings::verbosity >= 9 || p.trace()) {
171,112,947!
637
    write_message("Simulating Particle {}", p.id());
22✔
638
  }
639

640
// Add particle's starting weight to count for normalizing tallies later
641
#pragma omp atomic
93,491,612✔
642
  simulation::total_weight += p.wgt();
171,112,947✔
643

644
  // Force calculation of cross-sections by setting last energy to zero
645
  if (settings::run_CE) {
171,112,947✔
646
    p.invalidate_neutron_xs();
59,088,947✔
647
  }
648

649
  // Prepare to write out particle track.
650
  if (p.write_track())
171,112,947✔
651
    add_particle_track(p);
999✔
652
}
171,112,947✔
653

654
int overall_generation()
200,945,605✔
655
{
656
  using namespace simulation;
200,945,605✔
657
  return settings::gen_per_batch * (current_batch - 1) + current_gen;
200,945,605✔
658
}
659

660
void calculate_work()
7,201✔
661
{
662
  // Determine minimum amount of particles to simulate on each processor
663
  int64_t min_work = settings::n_particles / mpi::n_procs;
7,201✔
664

665
  // Determine number of processors that have one extra particle
666
  int64_t remainder = settings::n_particles % mpi::n_procs;
7,201✔
667

668
  int64_t i_bank = 0;
7,201✔
669
  simulation::work_index.resize(mpi::n_procs + 1);
7,201✔
670
  simulation::work_index[0] = 0;
7,201✔
671
  for (int i = 0; i < mpi::n_procs; ++i) {
16,297✔
672
    // Number of particles for rank i
673
    int64_t work_i = i < remainder ? min_work + 1 : min_work;
9,096!
674

675
    // Set number of particles
676
    if (mpi::rank == i)
9,096✔
677
      simulation::work_per_rank = work_i;
7,201✔
678

679
    // Set index into source bank for rank i
680
    i_bank += work_i;
9,096✔
681
    simulation::work_index[i + 1] = i_bank;
9,096✔
682
  }
683
}
7,201✔
684

685
void initialize_data()
5,909✔
686
{
687
  // Determine minimum/maximum energy for incident neutron/photon data
688
  data::energy_max = {INFTY, INFTY, INFTY, INFTY};
5,909✔
689
  data::energy_min = {0.0, 0.0, 0.0, 0.0};
5,909✔
690

691
  for (const auto& nuc : data::nuclides) {
34,886✔
692
    if (nuc->grid_.size() >= 1) {
28,977!
693
      int neutron = ParticleType::neutron().transport_index();
28,977✔
694
      data::energy_min[neutron] =
28,977✔
695
        std::max(data::energy_min[neutron], nuc->grid_[0].energy.front());
34,367✔
696
      data::energy_max[neutron] =
28,977✔
697
        std::min(data::energy_max[neutron], nuc->grid_[0].energy.back());
35,750✔
698
    }
699
  }
700

701
  if (settings::photon_transport) {
5,909✔
702
    for (const auto& elem : data::elements) {
917✔
703
      if (elem->energy_.size() >= 1) {
616!
704
        int photon = ParticleType::photon().transport_index();
616✔
705
        int n = elem->energy_.size();
616✔
706
        data::energy_min[photon] =
1,232✔
707
          std::max(data::energy_min[photon], std::exp(elem->energy_(1)));
1,054✔
708
        data::energy_max[photon] =
616✔
709
          std::min(data::energy_max[photon], std::exp(elem->energy_(n - 1)));
917✔
710
      }
711
    }
712

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

722
        const std::vector<int> charged = {electron, positron};
264✔
723
        for (auto t : charged) {
792✔
724
          data::energy_min[t] = std::exp(data::ttb_e_grid(1));
528✔
725
          data::energy_max[t] = std::exp(data::ttb_e_grid(n_e - 1));
528✔
726
        }
727

728
        data::energy_min[photon] =
528✔
729
          std::max(data::energy_min[photon], data::energy_min[electron]);
528!
730

731
        data::energy_max[photon] =
528✔
732
          std::min(data::energy_max[photon], data::energy_max[electron]);
528!
733
      }
264✔
734
    }
735
  }
736

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

756
  // Set up logarithmic grid for nuclides
757
  for (auto& nuc : data::nuclides) {
34,886✔
758
    nuc->init_grid();
28,977✔
759
  }
760
  int neutron = ParticleType::neutron().transport_index();
5,909✔
761
  simulation::log_spacing =
11,818✔
762
    std::log(data::energy_max[neutron] / data::energy_min[neutron]) /
5,909✔
763
    settings::n_log_bins;
764
}
5,909✔
765

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

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

785
  // Also broadcast global tally results
786
  auto& gt = simulation::global_tallies;
3,237✔
787
  MPI_Bcast(gt.data(), gt.size(), MPI_DOUBLE, 0, mpi::intracomm);
3,237✔
788

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

799
#endif
800

801
void free_memory_simulation()
8,302✔
802
{
803
  simulation::k_generation.clear();
8,302✔
804
  simulation::entropy.clear();
8,302✔
805
}
8,302✔
806

807
void transport_history_based_single_particle(Particle& p)
158,760,430✔
808
{
809
  while (p.alive()) {
2,147,483,647✔
810
    p.event_calculate_xs();
2,147,483,647✔
811
    if (p.alive()) {
2,147,483,647!
812
      p.event_advance();
2,147,483,647✔
813
    }
814
    if (p.alive()) {
2,147,483,647✔
815
      if (p.collision_distance() > p.boundary().distance()) {
2,147,483,647✔
816
        p.event_cross_surface();
1,445,667,055✔
817
      } else if (p.alive()) {
2,147,483,647✔
818
        p.event_collide();
2,147,483,647✔
819
      }
820
    }
821
    p.event_revive_from_secondary();
2,147,483,647✔
822
  }
823
  p.event_death();
158,760,421✔
824
}
158,760,421✔
825

826
void transport_history_based()
132,357✔
827
{
828
#pragma omp parallel for schedule(runtime)
73,834✔
829
  for (int64_t i_work = 1; i_work <= simulation::work_per_rank; ++i_work) {
77,755,993✔
830
    Particle p;
77,697,479✔
831
    initialize_history(p, i_work);
77,697,479✔
832
    transport_history_based_single_particle(p);
77,697,475✔
833
  }
77,697,470✔
834
}
132,348✔
835

836
void transport_event_based()
3,203✔
837
{
838
  int64_t remaining_work = simulation::work_per_rank;
3,203✔
839
  int64_t source_offset = 0;
3,203✔
840

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

851
    // Initialize all particle histories for this subiteration
852
    process_init_events(n_particles, source_offset);
3,203✔
853

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

863
      // Execute event with the longest queue
864
      if (max == 0) {
2,933,769✔
865
        break;
866
      } else if (max == simulation::calculate_fuel_xs_queue.size()) {
2,930,566✔
867
        process_calculate_xs_events(simulation::calculate_fuel_xs_queue);
425,858✔
868
      } else if (max == simulation::calculate_nonfuel_xs_queue.size()) {
2,504,708✔
869
        process_calculate_xs_events(simulation::calculate_nonfuel_xs_queue);
548,344✔
870
      } else if (max == simulation::advance_particle_queue.size()) {
1,956,364✔
871
        process_advance_particle_events();
968,928✔
872
      } else if (max == simulation::surface_crossing_queue.size()) {
987,436✔
873
        process_surface_crossing_events();
262,520✔
874
      } else if (max == simulation::collision_queue.size()) {
724,916!
875
        process_collision_events();
724,916✔
876
      }
877
    }
878

879
    // Execute death event for all particles
880
    process_death_events(n_particles);
3,203✔
881

882
    // Adjust remaining work and source offset variables
883
    remaining_work -= n_particles;
3,203✔
884
    source_offset += n_particles;
3,203✔
885
  }
886
}
3,203✔
887

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