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

openmc-dev / openmc / 12776996362

14 Jan 2025 09:49PM UTC coverage: 84.938% (+0.2%) from 84.729%
12776996362

Pull #3133

github

web-flow
Merge 0495246d9 into 549cc0973
Pull Request #3133: Kinetics parameters using Iterated Fission Probability

318 of 330 new or added lines in 10 files covered. (96.36%)

1658 existing lines in 66 files now uncovered.

50402 of 59340 relevant lines covered (84.94%)

33987813.96 hits per line

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

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

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

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

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

38
#include <fmt/format.h>
39

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

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

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

52
int openmc_run()
5,366✔
53
{
54
  openmc::simulation::time_total.start();
5,366✔
55
  openmc_simulation_init();
5,366✔
56

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

64
  int err = 0;
5,366✔
65
  while (status == 0 && err == 0) {
89,819✔
66
    err = openmc_next_batch(&status);
84,467✔
67
  }
68

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

74
int openmc_simulation_init()
6,231✔
75
{
76
  using namespace openmc;
77

78
  // Skip if simulation has already been initialized
79
  if (simulation::initialized)
6,231✔
80
    return 0;
12✔
81

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

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

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

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

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

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

112
  // Set up material nuclide index mapping
113
  for (auto& mat : model::materials) {
26,848✔
114
    mat->init_nuclide_index();
20,629✔
115
  }
116

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

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

138
  // Display header
139
  if (mpi::master) {
6,219✔
140
    if (settings::run_mode == RunMode::FIXED_SOURCE) {
5,334✔
141
      if (settings::solver_type == SolverType::MONTE_CARLO) {
1,953✔
142
        header("FIXED SOURCE TRANSPORT SIMULATION", 3);
1,749✔
143
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
204✔
144
        header("FIXED SOURCE TRANSPORT SIMULATION (RANDOM RAY SOLVER)", 3);
204✔
145
      }
146
    } else if (settings::run_mode == RunMode::EIGENVALUE) {
3,381✔
147
      if (settings::solver_type == SolverType::MONTE_CARLO) {
3,381✔
148
        header("K EIGENVALUE SIMULATION", 3);
3,309✔
149
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
72✔
150
        header("K EIGENVALUE SIMULATION (RANDOM RAY SOLVER)", 3);
72✔
151
      }
152
      if (settings::verbosity >= 7)
3,381✔
153
        print_columns();
3,114✔
154
    }
155
  }
156

157
  // load weight windows from file
158
  if (!settings::weight_windows_file.empty()) {
6,219✔
UNCOV
159
    openmc_weight_windows_import(settings::weight_windows_file.c_str());
×
160
  }
161

162
  // Set flag indicating initialization is done
163
  simulation::initialized = true;
6,219✔
164
  return 0;
6,219✔
165
}
166

167
int openmc_simulation_finalize()
6,205✔
168
{
169
  using namespace openmc;
170

171
  // Skip if simulation was never run
172
  if (!simulation::initialized)
6,205✔
UNCOV
173
    return 0;
×
174

175
  // Stop active batch timer and start finalization timer
176
  simulation::time_active.stop();
6,205✔
177
  simulation::time_finalize.start();
6,205✔
178

179
  // Clear material nuclide mapping
180
  for (auto& mat : model::materials) {
26,820✔
181
    mat->mat_nuclide_index_.clear();
20,615✔
182
  }
183

184
  // Close track file if open
185
  if (!settings::track_identifiers.empty() || settings::write_all_tracks) {
6,205✔
186
    close_track_file();
102✔
187
  }
188

189
  // Increment total number of generations
190
  simulation::total_gen += simulation::current_batch * settings::gen_per_batch;
6,205✔
191

192
#ifdef OPENMC_MPI
193
  broadcast_results();
3,302✔
194
#endif
195

196
  // Write tally results to tallies.out
197
  if (settings::output_tallies && mpi::master)
6,205✔
198
    write_tallies();
5,308✔
199

200
  // If weight window generators are present in this simulation,
201
  // write a weight windows file
202
  if (variance_reduction::weight_windows_generators.size() > 0) {
6,205✔
203
    openmc_weight_windows_export();
24✔
204
  }
205

206
  // Deactivate all tallies
207
  for (auto& t : model::tallies) {
33,414✔
208
    t->active_ = false;
27,209✔
209
  }
210

211
  // Stop timers and show timing statistics
212
  simulation::time_finalize.stop();
6,205✔
213
  simulation::time_total.stop();
6,205✔
214
  if (mpi::master) {
6,205✔
215
    if (settings::solver_type != SolverType::RANDOM_RAY) {
5,320✔
216
      if (settings::verbosity >= 6)
5,044✔
217
        print_runtime();
4,777✔
218
      if (settings::verbosity >= 4)
5,044✔
219
        print_results();
4,777✔
220
    }
221
  }
222
  if (settings::check_overlaps)
6,205✔
UNCOV
223
    print_overlap_check();
×
224

225
  // Reset flags
226
  simulation::initialized = false;
6,205✔
227
  return 0;
6,205✔
228
}
229

230
int openmc_next_batch(int* status)
92,672✔
231
{
232
  using namespace openmc;
233
  using openmc::simulation::current_gen;
234

235
  // Make sure simulation has been initialized
236
  if (!simulation::initialized) {
92,672✔
237
    set_errmsg("Simulation has not been initialized yet.");
12✔
238
    return OPENMC_E_ALLOCATE;
12✔
239
  }
240

241
  initialize_batch();
92,660✔
242

243
  // =======================================================================
244
  // LOOP OVER GENERATIONS
245
  for (current_gen = 1; current_gen <= settings::gen_per_batch; ++current_gen) {
185,544✔
246

247
    initialize_generation();
92,898✔
248

249
    // Start timer for transport
250
    simulation::time_transport.start();
92,898✔
251

252
    // Transport loop
253
    if (settings::event_based) {
92,898✔
254
      transport_event_based();
3,041✔
255
    } else {
256
      transport_history_based();
89,857✔
257
    }
258

259
    // Accumulate time for transport
260
    simulation::time_transport.stop();
92,884✔
261

262
    finalize_generation();
92,884✔
263
  }
264

265
  finalize_batch();
92,646✔
266

267
  // Check simulation ending criteria
268
  if (status) {
92,646✔
269
    if (simulation::current_batch >= settings::n_max_batches) {
92,646✔
270
      *status = STATUS_EXIT_MAX_BATCH;
5,789✔
271
    } else if (simulation::satisfy_triggers) {
86,857✔
272
      *status = STATUS_EXIT_ON_TRIGGER;
70✔
273
    } else {
274
      *status = STATUS_EXIT_NORMAL;
86,787✔
275
    }
276
  }
277
  return 0;
92,646✔
278
}
279

280
bool openmc_is_statepoint_batch()
7,125✔
281
{
282
  using namespace openmc;
283
  using openmc::simulation::current_gen;
284

285
  if (!simulation::initialized)
7,125✔
UNCOV
286
    return false;
×
287
  else
288
    return contains(settings::statepoint_batch, simulation::current_batch);
7,125✔
289
}
290

291
namespace openmc {
292

293
//==============================================================================
294
// Global variables
295
//==============================================================================
296

297
namespace simulation {
298

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

317
const RegularMesh* entropy_mesh {nullptr};
318
const RegularMesh* ufs_mesh {nullptr};
319

320
vector<double> k_generation;
321
vector<int64_t> work_index;
322

323
} // namespace simulation
324

325
//==============================================================================
326
// Non-member functions
327
//==============================================================================
328

329
void allocate_banks()
6,219✔
330
{
331
  if (settings::run_mode == RunMode::EIGENVALUE &&
6,219✔
332
      settings::solver_type == SolverType::MONTE_CARLO) {
4,057✔
333
    // Allocate source bank
334
    simulation::source_bank.resize(simulation::work_per_rank);
3,955✔
335

336
    // Allocate fission bank
337
    init_fission_bank(3 * simulation::work_per_rank);
3,955✔
338

339
    // Allocate IFP bank
340
    if (settings::ifp) {
3,955✔
341
      if (settings::ifp_parameter == IFPParameter::BetaEffective ||
17✔
342
          settings::ifp_parameter == IFPParameter::Both) {
17✔
343
        simulation::ifp_source_delayed_group_bank.resize(
17✔
344
          simulation::work_per_rank);
345
        simulation::ifp_fission_delayed_group_bank.resize(
17✔
346
          3 * simulation::work_per_rank);
17✔
347
      }
348
      if (settings::ifp_parameter == IFPParameter::GenerationTime ||
17✔
349
          settings::ifp_parameter == IFPParameter::Both) {
17✔
350
        simulation::ifp_source_lifetime_bank.resize(simulation::work_per_rank);
17✔
351
        simulation::ifp_fission_lifetime_bank.resize(
17✔
352
          3 * simulation::work_per_rank);
17✔
353
      }
354
    }
355
  }
356

357
  if (settings::surf_source_write) {
6,219✔
358
    // Allocate surface source bank
359
    simulation::surf_source_bank.reserve(settings::ssw_max_particles);
629✔
360
  }
361
}
6,219✔
362

363
void initialize_batch()
104,050✔
364
{
365
  // Increment current batch
366
  ++simulation::current_batch;
104,050✔
367
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
104,050✔
368
    if (settings::solver_type == SolverType::RANDOM_RAY &&
24,105✔
369
        simulation::current_batch < settings::n_inactive + 1) {
9,350✔
370
      write_message(
5,950✔
371
        6, "Simulating batch {:<4} (inactive)", simulation::current_batch);
372
    } else {
373
      write_message(6, "Simulating batch {}", simulation::current_batch);
18,155✔
374
    }
375
  }
376

377
  // Reset total starting particle weight used for normalizing tallies
378
  simulation::total_weight = 0.0;
104,050✔
379

380
  // Determine if this batch is the first inactive or active batch.
381
  bool first_inactive = false;
104,050✔
382
  bool first_active = false;
104,050✔
383
  if (!settings::restart_run) {
104,050✔
384
    first_inactive = settings::n_inactive > 0 && simulation::current_batch == 1;
103,736✔
385
    first_active = simulation::current_batch == settings::n_inactive + 1;
103,736✔
386
  } else if (simulation::current_batch == simulation::restart_batch + 1) {
314✔
387
    first_inactive = simulation::restart_batch < settings::n_inactive;
71✔
388
    first_active = !first_inactive;
71✔
389
  }
390

391
  // Manage active/inactive timers and activate tallies if necessary.
392
  if (first_inactive) {
104,050✔
393
    simulation::time_inactive.start();
3,185✔
394
  } else if (first_active) {
100,865✔
395
    simulation::time_inactive.stop();
6,192✔
396
    simulation::time_active.start();
6,192✔
397
    for (auto& t : model::tallies) {
33,377✔
398
      t->active_ = true;
27,185✔
399
    }
400
  }
401

402
  // Add user tallies to active tallies list
403
  setup_active_tallies();
104,050✔
404
}
104,050✔
405

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

413
  // update weight windows if needed
414
  for (const auto& wwg : variance_reduction::weight_windows_generators) {
104,156✔
415
    wwg->update();
120✔
416
  }
417

418
  // Reset global tally results
419
  if (simulation::current_batch <= settings::n_inactive) {
104,036✔
420
    xt::view(simulation::global_tallies, xt::all()) = 0.0;
21,884✔
421
    simulation::n_realizations = 0;
21,884✔
422
  }
423

424
  // Check_triggers
425
  if (mpi::master)
104,036✔
426
    check_triggers();
84,441✔
427
#ifdef OPENMC_MPI
428
  MPI_Bcast(&simulation::satisfy_triggers, 1, MPI_C_BOOL, 0, mpi::intracomm);
57,697✔
429
#endif
430
  if (simulation::satisfy_triggers ||
104,036✔
431
      (settings::trigger_on &&
3,224✔
432
        simulation::current_batch == settings::n_max_batches)) {
3,224✔
433
    settings::statepoint_batch.insert(simulation::current_batch);
157✔
434
  }
435

436
  // Write out state point if it's been specified for this batch and is not
437
  // a CMFD run instance
438
  if (contains(settings::statepoint_batch, simulation::current_batch) &&
110,539✔
439
      !settings::cmfd_run) {
6,503✔
440
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
11,969✔
441
        settings::source_write && !settings::source_separate) {
11,969✔
442
      bool b = (settings::run_mode == RunMode::EIGENVALUE);
5,407✔
443
      openmc_statepoint_write(nullptr, &b);
5,407✔
444
    } else {
445
      bool b = false;
696✔
446
      openmc_statepoint_write(nullptr, &b);
696✔
447
    }
448
  }
449

450
  if (settings::run_mode == RunMode::EIGENVALUE) {
104,036✔
451
    // Write out a separate source point if it's been specified for this batch
452
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
84,078✔
453
        settings::source_write && settings::source_separate) {
84,078✔
454

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

464
    // Write a continously-overwritten source point if requested.
465
    if (settings::source_latest) {
79,945✔
466
      // note: correct file extension appended automatically
467
      auto filename = settings::path_output + "source";
170✔
468
      gsl::span<SourceSite> bankspan(simulation::source_bank);
170✔
469
      write_source_point(filename.c_str(), bankspan, simulation::work_index,
170✔
470
        settings::source_mcpl_write);
471
    }
170✔
472
  }
473

474
  // Write out surface source if requested.
475
  if (settings::surf_source_write &&
104,036✔
476
      simulation::ssw_current_file <= settings::ssw_max_files) {
4,153✔
477
    bool last_batch = (simulation::current_batch == settings::n_batches);
1,530✔
478
    if (simulation::surf_source_bank.full() || last_batch) {
1,530✔
479
      // Determine appropriate filename
480
      auto filename = fmt::format("{}surface_source.{}", settings::path_output,
481
        simulation::current_batch);
553✔
482
      if (settings::ssw_max_files == 1 ||
665✔
483
          (simulation::ssw_current_file == 1 && last_batch)) {
60✔
484
        filename = settings::path_output + "surface_source";
605✔
485
      }
486

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

493
      // Write surface source file
494
      write_source_point(
665✔
495
        filename, surfbankspan, surf_work_index, settings::surf_mcpl_write);
496

497
      // Reset surface source bank and increment counter
498
      simulation::surf_source_bank.clear();
665✔
499
      if (!last_batch && settings::ssw_max_files >= 1) {
665✔
500
        simulation::surf_source_bank.reserve(settings::ssw_max_particles);
464✔
501
      }
502
      ++simulation::ssw_current_file;
665✔
503
    }
665✔
504
  }
505
}
104,036✔
506

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

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

517
    // Store current value of tracklength k
518
    simulation::keff_generation = simulation::global_tallies(
80,183✔
519
      GlobalTally::K_TRACKLENGTH, TallyResult::VALUE);
520
  }
521
}
104,288✔
522

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

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

537
  // reset tallies
538
  if (settings::run_mode == RunMode::EIGENVALUE) {
104,274✔
539
    global_tally_collision = 0.0;
80,183✔
540
    global_tally_absorption = 0.0;
80,183✔
541
    global_tally_tracklength = 0.0;
80,183✔
542
  }
543
  global_tally_leakage = 0.0;
104,274✔
544

545
  if (settings::run_mode == RunMode::EIGENVALUE &&
104,274✔
546
      settings::solver_type == SolverType::MONTE_CARLO) {
80,183✔
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();
78,143✔
551

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

556
  if (settings::run_mode == RunMode::EIGENVALUE) {
104,274✔
557

558
    // Calculate shannon entropy
559
    if (settings::entropy_on &&
80,183✔
560
        settings::solver_type == SolverType::MONTE_CARLO)
14,135✔
561
      shannon_entropy();
12,095✔
562

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

567
    // Write generation output
568
    if (mpi::master && settings::verbosity >= 7) {
80,183✔
569
      print_generation();
60,707✔
570
    }
571
  }
572
}
104,274✔
573

574
void initialize_history(Particle& p, int64_t index_source)
161,105,794✔
575
{
576
  // set defaults
577
  if (settings::run_mode == RunMode::EIGENVALUE) {
161,105,794✔
578
    // set defaults for eigenvalue simulations from primary bank
579
    p.from_source(&simulation::source_bank[index_source - 1]);
148,568,600✔
580
  } else if (settings::run_mode == RunMode::FIXED_SOURCE) {
12,537,194✔
581
    // initialize random number seed
582
    int64_t id = (simulation::total_gen + overall_generation() - 1) *
12,537,194✔
583
                   settings::n_particles +
12,537,194✔
584
                 simulation::work_index[mpi::rank] + index_source;
12,537,194✔
585
    uint64_t seed = init_seed(id, STREAM_SOURCE);
12,537,194✔
586
    // sample from external source distribution or custom library then set
587
    auto site = sample_external_source(&seed);
12,537,194✔
588
    p.from_source(&site);
12,537,190✔
589
  }
590
  p.current_work() = index_source;
161,105,790✔
591

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

595
  // set progeny count to zero
596
  p.n_progeny() = 0;
161,105,790✔
597

598
  // Reset particle event counter
599
  p.n_event() = 0;
161,105,790✔
600

601
  // Reset split counter
602
  p.n_split() = 0;
161,105,790✔
603

604
  // Reset weight window ratio
605
  p.ww_factor() = 0.0;
161,105,790✔
606

607
  // Reset pulse_height_storage
608
  std::fill(p.pht_storage().begin(), p.pht_storage().end(), 0);
161,105,790✔
609

610
  // set random number seed
611
  int64_t particle_seed =
612
    (simulation::total_gen + overall_generation() - 1) * settings::n_particles +
161,105,790✔
613
    p.id();
161,105,790✔
614
  init_particle_seeds(particle_seed, p.seeds());
161,105,790✔
615

616
  // set particle trace
617
  p.trace() = false;
161,105,790✔
618
  if (simulation::current_batch == settings::trace_batch &&
322,223,580✔
619
      simulation::current_gen == settings::trace_gen &&
161,117,790✔
620
      p.id() == settings::trace_particle)
12,000✔
621
    p.trace() = true;
12✔
622

623
  // Set particle track.
624
  p.write_track() = check_track_criteria(p);
161,105,790✔
625

626
  // Display message if high verbosity or trace is on
627
  if (settings::verbosity >= 9 || p.trace()) {
161,105,790✔
628
    write_message("Simulating Particle {}", p.id());
12✔
629
  }
630

631
// Add paricle's starting weight to count for normalizing tallies later
632
#pragma omp atomic
80,793,969✔
633
  simulation::total_weight += p.wgt();
161,105,790✔
634

635
  // Force calculation of cross-sections by setting last energy to zero
636
  if (settings::run_CE) {
161,105,790✔
637
    p.invalidate_neutron_xs();
40,097,790✔
638
  }
639

640
  // Prepare to write out particle track.
641
  if (p.write_track())
161,105,790✔
642
    add_particle_track(p);
1,128✔
643
}
161,105,790✔
644

645
int overall_generation()
173,862,298✔
646
{
647
  using namespace simulation;
648
  return settings::gen_per_batch * (current_batch - 1) + current_gen;
173,862,298✔
649
}
650

651
void calculate_work()
6,219✔
652
{
653
  // Determine minimum amount of particles to simulate on each processor
654
  int64_t min_work = settings::n_particles / mpi::n_procs;
6,219✔
655

656
  // Determine number of processors that have one extra particle
657
  int64_t remainder = settings::n_particles % mpi::n_procs;
6,219✔
658

659
  int64_t i_bank = 0;
6,219✔
660
  simulation::work_index.resize(mpi::n_procs + 1);
6,219✔
661
  simulation::work_index[0] = 0;
6,219✔
662
  for (int i = 0; i < mpi::n_procs; ++i) {
14,207✔
663
    // Number of particles for rank i
664
    int64_t work_i = i < remainder ? min_work + 1 : min_work;
7,988✔
665

666
    // Set number of particles
667
    if (mpi::rank == i)
7,988✔
668
      simulation::work_per_rank = work_i;
6,219✔
669

670
    // Set index into source bank for rank i
671
    i_bank += work_i;
7,988✔
672
    simulation::work_index[i + 1] = i_bank;
7,988✔
673
  }
674
}
6,219✔
675

676
void initialize_data()
5,337✔
677
{
678
  // Determine minimum/maximum energy for incident neutron/photon data
679
  data::energy_max = {INFTY, INFTY};
5,337✔
680
  data::energy_min = {0.0, 0.0};
5,337✔
681
  for (const auto& nuc : data::nuclides) {
35,689✔
682
    if (nuc->grid_.size() >= 1) {
30,352✔
683
      int neutron = static_cast<int>(ParticleType::neutron);
30,352✔
684
      data::energy_min[neutron] =
60,704✔
685
        std::max(data::energy_min[neutron], nuc->grid_[0].energy.front());
30,352✔
686
      data::energy_max[neutron] =
30,352✔
687
        std::min(data::energy_max[neutron], nuc->grid_[0].energy.back());
30,352✔
688
    }
689
  }
690

691
  if (settings::photon_transport) {
5,337✔
692
    for (const auto& elem : data::elements) {
781✔
693
      if (elem->energy_.size() >= 1) {
539✔
694
        int photon = static_cast<int>(ParticleType::photon);
539✔
695
        int n = elem->energy_.size();
539✔
696
        data::energy_min[photon] =
1,078✔
697
          std::max(data::energy_min[photon], std::exp(elem->energy_(1)));
539✔
698
        data::energy_max[photon] =
1,078✔
699
          std::min(data::energy_max[photon], std::exp(elem->energy_(n - 1)));
539✔
700
      }
701
    }
702

703
    if (settings::electron_treatment == ElectronTreatment::TTB) {
242✔
704
      // Determine if minimum/maximum energy for bremsstrahlung is greater/less
705
      // than the current minimum/maximum
706
      if (data::ttb_e_grid.size() >= 1) {
242✔
707
        int photon = static_cast<int>(ParticleType::photon);
242✔
708
        int n_e = data::ttb_e_grid.size();
242✔
709
        data::energy_min[photon] =
484✔
710
          std::max(data::energy_min[photon], std::exp(data::ttb_e_grid(1)));
242✔
711
        data::energy_max[photon] = std::min(
242✔
712
          data::energy_max[photon], std::exp(data::ttb_e_grid(n_e - 1)));
484✔
713
      }
714
    }
715
  }
716

717
  // Show which nuclide results in lowest energy for neutron transport
718
  for (const auto& nuc : data::nuclides) {
6,597✔
719
    // If a nuclide is present in a material that's not used in the model, its
720
    // grid has not been allocated
721
    if (nuc->grid_.size() > 0) {
6,152✔
722
      double max_E = nuc->grid_[0].energy.back();
6,152✔
723
      int neutron = static_cast<int>(ParticleType::neutron);
6,152✔
724
      if (max_E == data::energy_max[neutron]) {
6,152✔
725
        write_message(7, "Maximum neutron transport energy: {} eV for {}",
4,892✔
726
          data::energy_max[neutron], nuc->name_);
4,892✔
727
        if (mpi::master && data::energy_max[neutron] < 20.0e6) {
4,892✔
UNCOV
728
          warning("Maximum neutron energy is below 20 MeV. This may bias "
×
729
                  "the results.");
730
        }
731
        break;
4,892✔
732
      }
733
    }
734
  }
735

736
  // Set up logarithmic grid for nuclides
737
  for (auto& nuc : data::nuclides) {
35,689✔
738
    nuc->init_grid();
30,352✔
739
  }
740
  int neutron = static_cast<int>(ParticleType::neutron);
5,337✔
741
  simulation::log_spacing =
5,337✔
742
    std::log(data::energy_max[neutron] / data::energy_min[neutron]) /
5,337✔
743
    settings::n_log_bins;
744
}
5,337✔
745

746
#ifdef OPENMC_MPI
747
void broadcast_results()
3,302✔
748
{
749
  // Broadcast tally results so that each process has access to results
750
  for (auto& t : model::tallies) {
19,118✔
751
    // Create a new datatype that consists of all values for a given filter
752
    // bin and then use that to broadcast. This is done to minimize the
753
    // chance of the 'count' argument of MPI_BCAST exceeding 2**31
754
    auto& results = t->results_;
15,816✔
755

756
    auto shape = results.shape();
15,816✔
757
    int count_per_filter = shape[1] * shape[2];
15,816✔
758
    MPI_Datatype result_block;
759
    MPI_Type_contiguous(count_per_filter, MPI_DOUBLE, &result_block);
15,816✔
760
    MPI_Type_commit(&result_block);
15,816✔
761
    MPI_Bcast(results.data(), shape[0], result_block, 0, mpi::intracomm);
15,816✔
762
    MPI_Type_free(&result_block);
15,816✔
763
  }
764

765
  // Also broadcast global tally results
766
  auto& gt = simulation::global_tallies;
3,302✔
767
  MPI_Bcast(gt.data(), gt.size(), MPI_DOUBLE, 0, mpi::intracomm);
3,302✔
768

769
  // These guys are needed so that non-master processes can calculate the
770
  // combined estimate of k-effective
771
  double temp[] {
772
    simulation::k_col_abs, simulation::k_col_tra, simulation::k_abs_tra};
3,302✔
773
  MPI_Bcast(temp, 3, MPI_DOUBLE, 0, mpi::intracomm);
3,302✔
774
  simulation::k_col_abs = temp[0];
3,302✔
775
  simulation::k_col_tra = temp[1];
3,302✔
776
  simulation::k_abs_tra = temp[2];
3,302✔
777
}
3,302✔
778

779
#endif
780

781
void free_memory_simulation()
6,931✔
782
{
783
  simulation::k_generation.clear();
6,931✔
784
  simulation::entropy.clear();
6,931✔
785
}
6,931✔
786

787
void transport_history_based_single_particle(Particle& p)
150,072,426✔
788
{
789
  while (p.alive()) {
2,147,483,647✔
790
    p.event_calculate_xs();
2,147,483,647✔
791
    if (p.alive()) {
2,147,483,647✔
792
      p.event_advance();
2,147,483,647✔
793
    }
794
    if (p.alive()) {
2,147,483,647✔
795
      if (p.collision_distance() > p.boundary().distance) {
2,147,483,647✔
796
        p.event_cross_surface();
1,370,052,739✔
797
      } else if (p.alive()) {
2,147,483,647✔
798
        p.event_collide();
2,147,483,647✔
799
      }
800
    }
801
    p.event_revive_from_secondary();
2,147,483,647✔
802
  }
803
  p.event_death();
150,072,416✔
804
}
150,072,416✔
805

806
void transport_history_based()
89,857✔
807
{
808
#pragma omp parallel for schedule(runtime)
809
  for (int64_t i_work = 1; i_work <= simulation::work_per_rank; ++i_work) {
80,578,371✔
810
    Particle p;
80,534,262✔
811
    initialize_history(p, i_work);
80,534,262✔
812
    transport_history_based_single_particle(p);
80,534,258✔
813
  }
80,534,252✔
814
}
89,847✔
815

816
void transport_event_based()
3,041✔
817
{
818
  int64_t remaining_work = simulation::work_per_rank;
3,041✔
819
  int64_t source_offset = 0;
3,041✔
820

821
  // To cap the total amount of memory used to store particle object data, the
822
  // number of particles in flight at any point in time can bet set. In the case
823
  // that the maximum in flight particle count is lower than the total number
824
  // of particles that need to be run this iteration, the event-based transport
825
  // loop is executed multiple times until all particles have been completed.
826
  while (remaining_work > 0) {
6,082✔
827
    // Figure out # of particles to run for this subiteration
828
    int64_t n_particles =
829
      std::min(remaining_work, settings::max_particles_in_flight);
3,041✔
830

831
    // Initialize all particle histories for this subiteration
832
    process_init_events(n_particles, source_offset);
3,041✔
833

834
    // Event-based transport loop
835
    while (true) {
836
      // Determine which event kernel has the longest queue
837
      int64_t max = std::max({simulation::calculate_fuel_xs_queue.size(),
4,736,322✔
838
        simulation::calculate_nonfuel_xs_queue.size(),
2,368,161✔
839
        simulation::advance_particle_queue.size(),
2,368,161✔
840
        simulation::surface_crossing_queue.size(),
2,368,161✔
841
        simulation::collision_queue.size()});
2,368,161✔
842

843
      // Execute event with the longest queue
844
      if (max == 0) {
2,368,161✔
845
        break;
3,041✔
846
      } else if (max == simulation::calculate_fuel_xs_queue.size()) {
2,365,120✔
847
        process_calculate_xs_events(simulation::calculate_fuel_xs_queue);
422,565✔
848
      } else if (max == simulation::calculate_nonfuel_xs_queue.size()) {
1,942,555✔
849
        process_calculate_xs_events(simulation::calculate_nonfuel_xs_queue);
362,918✔
850
      } else if (max == simulation::advance_particle_queue.size()) {
1,579,637✔
851
        process_advance_particle_events();
780,620✔
852
      } else if (max == simulation::surface_crossing_queue.size()) {
799,017✔
853
        process_surface_crossing_events();
256,082✔
854
      } else if (max == simulation::collision_queue.size()) {
542,935✔
855
        process_collision_events();
542,935✔
856
      }
857
    }
2,365,120✔
858

859
    // Execute death event for all particles
860
    process_death_events(n_particles);
3,041✔
861

862
    // Adjust remaining work and source offset variables
863
    remaining_work -= n_particles;
3,041✔
864
    source_offset += n_particles;
3,041✔
865
  }
866
}
3,041✔
867

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