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

openmc-dev / openmc / 23285088002

19 Mar 2026 07:51AM UTC coverage: 80.71% (-0.7%) from 81.447%
23285088002

Pull #3886

github

web-flow
Merge 9cdb2d588 into 3ce6cbfdd
Pull Request #3886: Implement python tally types

16322 of 23494 branches covered (69.47%)

Branch coverage included in aggregate %.

215 of 264 new or added lines in 11 files covered. (81.44%)

713 existing lines in 49 files now uncovered.

56449 of 66670 relevant lines covered (84.67%)

14035992.85 hits per line

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

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

65
  int err = 0;
66
  while (status == 0 && err == 0) {
22,220✔
67
    err = openmc_next_batch(&status);
21,250✔
68
  }
69

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

75
int openmc_simulation_init()
1,138✔
76
{
77
  using namespace openmc;
1,138✔
78

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

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

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

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

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

99
  // If doing an event-based simulation, intialize the particle buffer
100
  // and event queues
101
  if (settings::event_based) {
1,134!
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) {
4,928✔
109
    t->set_strides();
3,794✔
110
    t->init_results();
3,794✔
111
  }
112

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

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

141
  // Display header
142
  if (mpi::master) {
1,134!
143
    if (settings::run_mode == RunMode::FIXED_SOURCE) {
1,134✔
144
      if (settings::solver_type == SolverType::MONTE_CARLO) {
476✔
145
        header("FIXED SOURCE TRANSPORT SIMULATION", 3);
414✔
146
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
62!
147
        header("FIXED SOURCE TRANSPORT SIMULATION (RANDOM RAY SOLVER)", 3);
62✔
148
      }
149
    } else if (settings::run_mode == RunMode::EIGENVALUE) {
658!
150
      if (settings::solver_type == SolverType::MONTE_CARLO) {
658✔
151
        header("K EIGENVALUE SIMULATION", 3);
608✔
152
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
50!
153
        header("K EIGENVALUE SIMULATION (RANDOM RAY SOLVER)", 3);
50✔
154
      }
155
      if (settings::verbosity >= 7)
658✔
156
        print_columns();
618✔
157
    }
158
  }
159

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

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

170
int openmc_simulation_finalize()
1,130✔
171
{
172
  using namespace openmc;
1,130✔
173

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

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

182
  // Clear material nuclide mapping
183
  for (auto& mat : model::materials) {
4,340✔
184
    mat->mat_nuclide_index_.clear();
6,420!
185
  }
186

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

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

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

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

209
  // Deactivate all tallies
210
  for (auto& t : model::tallies) {
4,924✔
211
    t->active_ = false;
3,794✔
212
  }
213

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

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

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

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

244
  initialize_batch();
21,998✔
245

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

250
    initialize_generation();
22,026✔
251

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

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

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

265
    finalize_generation();
22,022✔
266
  }
267

268
  finalize_batch();
21,994✔
269

270
  // Check simulation ending criteria
271
  if (status) {
21,994!
272
    if (simulation::current_batch >= settings::n_max_batches) {
21,994✔
273
      *status = STATUS_EXIT_MAX_BATCH;
1,008✔
274
    } else if (simulation::satisfy_triggers) {
20,986✔
275
      *status = STATUS_EXIT_ON_TRIGGER;
14✔
276
    } else {
277
      *status = STATUS_EXIT_NORMAL;
20,972✔
278
    }
279
  }
280
  return 0;
281
}
282

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

288
  if (!simulation::initialized)
570!
289
    return false;
290
  else
291
    return contains(settings::statepoint_batch, simulation::current_batch);
1,140✔
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()
1,134✔
334
{
335
  if (settings::run_mode == RunMode::EIGENVALUE &&
1,134✔
336
      settings::solver_type == SolverType::MONTE_CARLO) {
658✔
337
    // Allocate source bank
338
    simulation::source_bank.resize(simulation::work_per_rank);
608✔
339

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

343
    // Allocate IFP bank
344
    if (settings::ifp_on) {
608✔
345
      resize_simulation_ifp_banks();
12✔
346
    }
347
  }
348

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

354
  if (settings::collision_track) {
1,134✔
355
    // Allocate collision track bank
356
    collision_track_reserve_bank();
22✔
357
  }
358
}
1,134✔
359

360
void initialize_batch()
24,838✔
361
{
362
  // Increment current batch
363
  ++simulation::current_batch;
24,838✔
364
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
24,838✔
365
    if (settings::solver_type == SolverType::RANDOM_RAY &&
10,560✔
366
        simulation::current_batch < settings::n_inactive + 1) {
1,820✔
367
      write_message(
2,180✔
368
        6, "Simulating batch {:<4} (inactive)", simulation::current_batch);
369
    } else {
370
      write_message(6, "Simulating batch {}", simulation::current_batch);
18,940✔
371
    }
372
  }
373

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

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

388
  // Manage active/inactive timers and activate tallies if necessary.
389
  if (first_inactive) {
24,820✔
390
    simulation::time_inactive.start();
540✔
391
  } else if (first_active) {
24,298✔
392
    simulation::time_inactive.stop();
1,126✔
393
    simulation::time_active.start();
1,126✔
394
    for (auto& t : model::tallies) {
4,916✔
395
      t->active_ = true;
3,790✔
396
    }
397
  }
398

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

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

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

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

421
  // Check_triggers
422
  if (mpi::master)
24,834!
423
    check_triggers();
24,834✔
424
#ifdef OPENMC_MPI
425
  MPI_Bcast(&simulation::satisfy_triggers, 1, MPI_C_BOOL, 0, mpi::intracomm);
426
#endif
427
  if (simulation::satisfy_triggers ||
24,834✔
428
      (settings::trigger_on &&
410✔
429
        simulation::current_batch == settings::n_max_batches)) {
410✔
430
    settings::statepoint_batch.insert(simulation::current_batch);
22✔
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) &&
49,668✔
436
      !settings::cmfd_run) {
1,164✔
437
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
2,232✔
438
        settings::source_write && !settings::source_separate) {
2,120✔
439
      bool b = (settings::run_mode == RunMode::EIGENVALUE);
978✔
440
      openmc_statepoint_write(nullptr, &b);
978✔
441
    } else {
442
      bool b = false;
154✔
443
      openmc_statepoint_write(nullptr, &b);
154✔
444
    }
445
  }
446

447
  if (settings::run_mode == RunMode::EIGENVALUE) {
24,834✔
448
    // Write out a separate source point if it's been specified for this batch
449
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
14,944✔
450
        settings::source_write && settings::source_separate) {
14,894✔
451

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

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

470
  // Write out surface source if requested.
471
  if (settings::surf_source_write &&
24,834✔
472
      simulation::ssw_current_file <= settings::ssw_max_files) {
1,728✔
473
    bool last_batch = (simulation::current_batch == settings::n_batches);
336✔
474
    if (simulation::surf_source_bank.full() || last_batch) {
336✔
475
      // Determine appropriate filename
476
      auto filename = fmt::format("{}surface_source.{}", settings::path_output,
188✔
477
        simulation::current_batch);
188✔
478
      if (settings::ssw_max_files == 1 ||
188✔
479
          (simulation::ssw_current_file == 1 && last_batch)) {
10!
480
        filename = settings::path_output + "surface_source";
178✔
481
      }
482

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

489
      // Write surface source file
490
      write_source_point(
188✔
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();
188✔
495
      if (!last_batch && settings::ssw_max_files >= 1) {
188!
496
        simulation::surf_source_bank.reserve(settings::ssw_max_particles);
154✔
497
      }
498
      ++simulation::ssw_current_file;
188✔
499
    }
188✔
500
  }
501
  // Write collision track file if requested
502
  if (settings::collision_track) {
24,834✔
503
    collision_track_flush_bank();
86✔
504
  }
505
}
24,834✔
506

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

513
    // Count source sites if using uniform fission source weighting
514
    if (settings::ufs_on)
14,306✔
515
      ufs_count_sites();
20✔
516

517
    // Store current value of tracklength k
518
    simulation::keff_generation = simulation::global_tallies(
14,306✔
519
      GlobalTally::K_TRACKLENGTH, TallyResult::VALUE);
520
  }
521
}
24,866✔
522

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

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

537
  // reset tallies
538
  if (settings::run_mode == RunMode::EIGENVALUE) {
24,862✔
539
    global_tally_collision = 0.0;
14,306✔
540
    global_tally_absorption = 0.0;
14,306✔
541
    global_tally_tracklength = 0.0;
14,306✔
542
  }
543
  global_tally_leakage = 0.0;
24,862✔
544

545
  if (settings::run_mode == RunMode::EIGENVALUE &&
24,862✔
546
      settings::solver_type == SolverType::MONTE_CARLO) {
14,306✔
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();
13,286✔
551

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

556
  if (settings::run_mode == RunMode::EIGENVALUE) {
24,862✔
557

558
    // Calculate shannon entropy
559
    if (settings::entropy_on &&
14,306✔
560
        settings::solver_type == SolverType::MONTE_CARLO)
2,410✔
561
      shannon_entropy();
1,390✔
562

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

567
    // Write generation output
568
    if (mpi::master && settings::verbosity >= 7) {
14,306!
569
      print_generation();
13,806✔
570
    }
571
  }
572
}
24,862✔
573

574
void initialize_history(Particle& p, int64_t index_source)
30,681,048✔
575
{
576
  // set defaults
577
  if (settings::run_mode == RunMode::EIGENVALUE) {
30,681,048✔
578
    // set defaults for eigenvalue simulations from primary bank
579
    p.from_source(&simulation::source_bank[index_source - 1]);
25,736,400✔
580
  } else if (settings::run_mode == RunMode::FIXED_SOURCE) {
4,944,648!
581
    // initialize random number seed
582
    int64_t id = (simulation::total_gen + overall_generation() - 1) *
4,944,648✔
583
                   settings::n_particles +
4,944,648✔
584
                 simulation::work_index[mpi::rank] + index_source;
4,944,648✔
585
    uint64_t seed = init_seed(id, STREAM_SOURCE);
4,944,648✔
586
    // sample from external source distribution or custom library then set
587
    auto site = sample_external_source(&seed);
4,944,648✔
588
    p.from_source(&site);
4,944,646✔
589
  }
590
  p.current_work() = index_source;
30,681,046✔
591

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

595
  // set progeny count to zero
596
  p.n_progeny() = 0;
30,681,046✔
597

598
  // Reset particle event counter
599
  p.n_event() = 0;
30,681,046✔
600

601
  // Reset split counter
602
  p.n_split() = 0;
30,681,046✔
603

604
  // Reset weight window ratio
605
  p.ww_factor() = 0.0;
30,681,046✔
606

607
  // set particle history start weight
608
  p.wgt_born() = p.wgt();
30,681,046✔
609

610
  // Reset pulse_height_storage
611
  std::fill(p.pht_storage().begin(), p.pht_storage().end(), 0);
30,681,046✔
612

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

619
  // set particle trace
620
  p.trace() = false;
30,681,046✔
621
  if (simulation::current_batch == settings::trace_batch &&
30,683,046✔
622
      simulation::current_gen == settings::trace_gen &&
30,681,046!
623
      p.id() == settings::trace_particle)
2,000✔
624
    p.trace() = true;
2✔
625

626
  // Set particle track.
627
  p.write_track() = check_track_criteria(p);
30,681,046✔
628

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

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

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

642
  // Force calculation of cross-sections by setting last energy to zero
643
  if (settings::run_CE) {
30,681,046✔
644
    p.invalidate_neutron_xs();
10,313,046✔
645
  }
646

647
  // Prepare to write out particle track.
648
  if (p.write_track())
30,681,046✔
649
    add_particle_track(p);
138✔
650
}
30,681,046✔
651

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

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

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

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

673
    // Set number of particles
674
    if (mpi::rank == i)
1,134!
675
      simulation::work_per_rank = work_i;
1,134✔
676

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

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

689
  for (const auto& nuc : data::nuclides) {
5,828✔
690
    if (nuc->grid_.size() >= 1) {
4,866!
691
      int neutron = ParticleType::neutron().transport_index();
4,866✔
692
      data::energy_min[neutron] =
4,866✔
693
        std::max(data::energy_min[neutron], nuc->grid_[0].energy.front());
5,738✔
694
      data::energy_max[neutron] =
4,866✔
695
        std::min(data::energy_max[neutron], nuc->grid_[0].energy.back());
5,968✔
696
    }
697
  }
698

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

711
    if (settings::electron_treatment == ElectronTreatment::TTB) {
46✔
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) {
40!
715
        int photon = ParticleType::photon().transport_index();
40✔
716
        int electron = ParticleType::electron().transport_index();
40✔
717
        int positron = ParticleType::positron().transport_index();
40✔
718
        int n_e = data::ttb_e_grid.size();
40✔
719

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

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

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

735
  // Show which nuclide results in lowest energy for neutron transport
736
  for (const auto& nuc : data::nuclides) {
1,200✔
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) {
1,110!
740
      double max_E = nuc->grid_[0].energy.back();
1,110✔
741
      int neutron = ParticleType::neutron().transport_index();
1,110✔
742
      if (max_E == data::energy_max[neutron]) {
1,110✔
743
        write_message(7, "Maximum neutron transport energy: {} eV for {}",
872✔
744
          data::energy_max[neutron], nuc->name_);
872✔
745
        if (mpi::master && data::energy_max[neutron] < 20.0e6) {
872!
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) {
5,828✔
756
    nuc->init_grid();
4,866✔
757
  }
758
  int neutron = ParticleType::neutron().transport_index();
962✔
759
  simulation::log_spacing =
1,924✔
760
    std::log(data::energy_max[neutron] / data::energy_min[neutron]) /
962✔
761
    settings::n_log_bins;
762
}
962✔
763

764
#ifdef OPENMC_MPI
765
void broadcast_results()
766
{
767
  // Broadcast tally results so that each process has access to results
768
  for (auto& t : model::tallies) {
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_;
773

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

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

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

797
#endif
798

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

805
void transport_history_based_single_particle(Particle& p)
30,681,052✔
806
{
807
  while (p.alive()) {
817,346,840✔
808
    p.event_calculate_xs();
786,665,790✔
809
    if (p.alive()) {
786,665,790!
810
      p.event_advance();
786,665,790✔
811
    }
812
    if (p.alive()) {
786,665,790✔
813
      if (p.collision_distance() > p.boundary().distance()) {
786,624,894✔
814
        p.event_cross_surface();
268,338,185✔
815
      } else if (p.alive()) {
518,286,709✔
816
        p.event_collide();
518,286,709✔
817
      }
818
    }
819
    p.event_revive_from_secondary();
786,665,788✔
820
  }
821
  p.event_death();
30,681,050✔
822
}
30,681,050✔
823

824
void transport_history_based()
22,026✔
825
{
826
#pragma omp parallel for schedule(runtime)
827
  for (int64_t i_work = 1; i_work <= simulation::work_per_rank; ++i_work) {
30,703,070✔
828
    Particle p;
30,681,048✔
829
    initialize_history(p, i_work);
30,681,048✔
830
    transport_history_based_single_particle(p);
30,681,046✔
831
  }
30,681,044✔
832
}
22,022✔
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