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

openmc-dev / openmc / 25930342666

15 May 2026 04:56PM UTC coverage: 81.414% (+0.09%) from 81.324%
25930342666

Pull #3734

github

web-flow
Merge 0af48acaf into d56cda254
Pull Request #3734: Specify temperature from a field (structured mesh only)

17919 of 25855 branches covered (69.31%)

Branch coverage included in aggregate %.

275 of 305 new or added lines in 18 files covered. (90.16%)

1321 existing lines in 29 files now uncovered.

58986 of 68607 relevant lines covered (85.98%)

47884537.9 hits per line

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

94.94
/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/constants.h"
7
#include "openmc/container_util.h"
8
#include "openmc/eigenvalue.h"
9
#include "openmc/error.h"
10
#include "openmc/event.h"
11
#include "openmc/field.h"
12
#include "openmc/geometry_aux.h"
13
#include "openmc/ifp.h"
14
#include "openmc/material.h"
15
#include "openmc/message_passing.h"
16
#include "openmc/nuclide.h"
17
#include "openmc/output.h"
18
#include "openmc/particle.h"
19
#include "openmc/photon.h"
20
#include "openmc/random_lcg.h"
21
#include "openmc/settings.h"
22
#include "openmc/source.h"
23
#include "openmc/state_point.h"
24
#include "openmc/tallies/derivative.h"
25
#include "openmc/tallies/filter.h"
26
#include "openmc/tallies/tally.h"
27
#include "openmc/tallies/trigger.h"
28
#include "openmc/timer.h"
29
#include "openmc/track_output.h"
30
#include "openmc/weight_windows.h"
31

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

37
#ifdef OPENMC_MPI
38
#include <mpi.h>
39
#endif
40

41
#include <fmt/format.h>
42

43
#include <algorithm>
44
#include <cmath>
45
#include <string>
46

47
//==============================================================================
48
// C API functions
49
//==============================================================================
50

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

55
int openmc_run()
6,413✔
56
{
57
  openmc::simulation::time_total.start();
6,413✔
58
  openmc_simulation_init();
6,413✔
59

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

67
  int err = 0;
68
  while (status == 0 && err == 0) {
147,870✔
69
    err = openmc_next_batch(&status);
141,470✔
70
  }
71

72
  openmc_simulation_finalize();
6,400✔
73
  openmc::simulation::time_total.stop();
6,400✔
74
  return err;
6,400✔
75
}
76

77
int openmc_simulation_init()
7,585✔
78
{
79
  using namespace openmc;
7,585✔
80

81
  // Skip if simulation has already been initialized
82
  if (simulation::initialized)
7,585✔
83
    return 0;
84

85
  // Initialize nuclear data (energy limits, log grid)
86
  if (settings::run_CE) {
7,563✔
87
    initialize_data();
6,182✔
88
  }
89

90
  // Determine how much work each process should do
91
  calculate_work();
7,563✔
92

93
  // Allocate source, fission and surface source banks.
94
  allocate_banks();
7,563✔
95

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

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

109
  // Allocate tally results arrays if they're not allocated yet
110
  for (auto& t : model::tallies) {
34,531✔
111
    t->set_strides();
26,968✔
112
    t->init_results();
26,968✔
113
  }
114

115
  // Set up material nuclide index mapping
116
  for (auto& mat : model::materials) {
26,999✔
117
    mat->init_nuclide_index();
19,436✔
118
  }
119

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

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

143
  // Display header
144
  if (mpi::master) {
7,563✔
145
    if (settings::run_mode == RunMode::FIXED_SOURCE) {
6,583✔
146
      if (settings::solver_type == SolverType::MONTE_CARLO) {
2,742✔
147
        header("FIXED SOURCE TRANSPORT SIMULATION", 3);
2,366✔
148
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
376!
149
        header("FIXED SOURCE TRANSPORT SIMULATION (RANDOM RAY SOLVER)", 3);
376✔
150
      }
151
    } else if (settings::run_mode == RunMode::EIGENVALUE) {
3,841!
152
      if (settings::solver_type == SolverType::MONTE_CARLO) {
3,841✔
153
        header("K EIGENVALUE SIMULATION", 3);
3,566✔
154
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
275!
155
        header("K EIGENVALUE SIMULATION (RANDOM RAY SOLVER)", 3);
275✔
156
      }
157
      if (settings::verbosity >= 7)
3,841✔
158
        print_columns();
3,461✔
159
    }
160
  }
161

162
  // load weight windows from file
163
  if (!settings::weight_windows_file.empty()) {
7,563!
164
    openmc_weight_windows_import(settings::weight_windows_file.c_str());
×
165
  }
166

167
  // Set flag indicating initialization is done
168
  simulation::initialized = true;
7,563✔
169
  return 0;
7,563✔
170
}
171

172
int openmc_simulation_finalize()
7,550✔
173
{
174
  using namespace openmc;
7,550✔
175

176
  // Skip if simulation was never run
177
  if (!simulation::initialized)
7,550!
178
    return 0;
179

180
  // Stop active batch timer and start finalization timer
181
  simulation::time_active.stop();
7,550✔
182
  simulation::time_finalize.start();
7,550✔
183

184
  // Clear material nuclide mapping
185
  for (auto& mat : model::materials) {
26,973✔
186
    mat->mat_nuclide_index_.clear();
38,846!
187
  }
188

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

194
  // Increment total number of generations
195
  simulation::total_gen += simulation::current_batch * settings::gen_per_batch;
7,550✔
196

197
#ifdef OPENMC_MPI
198
  broadcast_results();
3,389✔
199
#endif
200

201
  // Write tally results to tallies.out
202
  if (settings::output_tallies && mpi::master)
7,550!
203
    write_tallies();
6,224✔
204

205
  // If weight window generators are present in this simulation,
206
  // write a weight windows file
207
  if (variance_reduction::weight_windows_generators.size() > 0) {
7,550✔
208
    openmc_weight_windows_export();
155✔
209
  }
210

211
  // Deactivate all tallies
212
  for (auto& t : model::tallies) {
34,518✔
213
    t->active_ = false;
26,968✔
214
  }
215

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

230
  // Reset flags
231
  simulation::initialized = false;
7,550✔
232
  return 0;
7,550✔
233
}
234

235
int openmc_next_batch(int* status)
145,595✔
236
{
237
  using namespace openmc;
145,595✔
238
  using openmc::simulation::current_gen;
145,595✔
239

240
  // Make sure simulation has been initialized
241
  if (!simulation::initialized) {
145,595✔
242
    set_errmsg("Simulation has not been initialized yet.");
11✔
243
    return OPENMC_E_ALLOCATE;
11✔
244
  }
245

246
  initialize_batch();
145,584✔
247

248
  // =======================================================================
249
  // LOOP OVER GENERATIONS
250
  for (current_gen = 1; current_gen <= settings::gen_per_batch; ++current_gen) {
291,365✔
251

252
    initialize_generation();
145,794✔
253

254
    // Start timer for transport
255
    simulation::time_transport.start();
145,794✔
256

257
    // Transport loop
258
    if (settings::event_based) {
145,794✔
259
      transport_event_based();
3,303✔
260
    } else {
261
      transport_history_based();
142,491✔
262
    }
263

264
    // Accumulate time for transport
265
    simulation::time_transport.stop();
145,781✔
266

267
    finalize_generation();
145,781✔
268
  }
269

270
  finalize_batch();
145,571✔
271

272
  // Check simulation ending criteria
273
  if (status) {
145,571!
274
    if (simulation::current_batch >= settings::n_max_batches) {
145,571✔
275
      *status = STATUS_EXIT_MAX_BATCH;
6,593✔
276
    } else if (simulation::satisfy_triggers) {
138,978✔
277
      *status = STATUS_EXIT_ON_TRIGGER;
93✔
278
    } else {
279
      *status = STATUS_EXIT_NORMAL;
138,885✔
280
    }
281
  }
282
  return 0;
283
}
284

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

290
  if (!simulation::initialized)
3,135!
291
    return false;
292
  else
293
    return contains(settings::statepoint_batch, simulation::current_batch);
6,270✔
294
}
295

296
namespace openmc {
297

298
//==============================================================================
299
// Global variables
300
//==============================================================================
301

302
namespace simulation {
303

304
int ct_current_file;
305
int current_batch;
306
int current_gen;
307
bool initialized {false};
308
double keff {1.0};
309
double keff_std;
310
double k_col_abs {0.0};
311
double k_col_tra {0.0};
312
double k_abs_tra {0.0};
313
double log_spacing;
314
int n_lost_particles {0};
315
bool need_depletion_rx {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
TemperatureField temperature_field;
327

328
vector<double> k_generation;
329
vector<int64_t> work_index;
330

331
} // namespace simulation
332

333
//==============================================================================
334
// Non-member functions
335
//==============================================================================
336

337
void allocate_banks()
7,563✔
338
{
339
  if (settings::run_mode == RunMode::EIGENVALUE &&
7,563✔
340
      settings::solver_type == SolverType::MONTE_CARLO) {
4,551✔
341
    // Allocate source bank
342
    simulation::source_bank.resize(simulation::work_per_rank);
4,180✔
343

344
    // Allocate fission bank
345
    init_fission_bank(3 * simulation::work_per_rank);
4,180✔
346

347
    // Allocate IFP bank
348
    if (settings::ifp_on) {
4,180✔
349
      resize_simulation_ifp_banks();
74✔
350
    }
351
  }
352

353
  if (settings::surf_source_write) {
7,563✔
354
    // Allocate surface source bank
355
    simulation::surf_source_bank.reserve(settings::ssw_max_particles);
1,154✔
356
  }
357

358
  if (settings::collision_track) {
7,563✔
359
    // Allocate collision track bank
360
    collision_track_reserve_bank();
148✔
361
  }
362
}
7,563✔
363

364
void initialize_batch()
166,546✔
365
{
366
  // Increment current batch
367
  ++simulation::current_batch;
166,546✔
368
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
166,546✔
369
    if (settings::solver_type == SolverType::RANDOM_RAY &&
64,053✔
370
        simulation::current_batch < settings::n_inactive + 1) {
14,112✔
371
      write_message(
16,812✔
372
        6, "Simulating batch {:<4} (inactive)", simulation::current_batch);
373
    } else {
374
      write_message(6, "Simulating batch {}", simulation::current_batch);
111,294✔
375
    }
376
  }
377

378
  // Reset total starting particle weight used for normalizing tallies
379
  simulation::total_weight = 0.0;
166,546✔
380

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

392
  // Manage active/inactive timers and activate tallies if necessary.
393
  if (first_inactive) {
166,435✔
394
    simulation::time_inactive.start();
3,845✔
395
  } else if (first_active) {
162,701✔
396
    simulation::time_inactive.stop();
7,516✔
397
    simulation::time_active.start();
7,516✔
398
    for (auto& t : model::tallies) {
34,462✔
399
      t->active_ = true;
26,946✔
400
    }
401
  }
402

403
  // Add user tallies to active tallies list
404
  setup_active_tallies();
166,546✔
405
}
166,546✔
406

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

414
  // update weight windows if needed
415
  for (const auto& wwg : variance_reduction::weight_windows_generators) {
169,110✔
416
    wwg->update();
2,577✔
417
  }
418

419
  // Reset global tally results
420
  if (simulation::current_batch <= settings::n_inactive) {
166,533✔
421
    simulation::global_tallies.fill(0.0);
32,148✔
422
    simulation::n_realizations = 0;
32,148✔
423
  }
424

425
  // Check_triggers
426
  if (mpi::master)
166,533✔
427
    check_triggers();
147,414✔
428
#ifdef OPENMC_MPI
429
  MPI_Bcast(&simulation::satisfy_triggers, 1, MPI_C_BOOL, 0, mpi::intracomm);
73,050✔
430
#endif
431
  if (simulation::satisfy_triggers ||
166,533✔
432
      (settings::trigger_on &&
2,567✔
433
        simulation::current_batch == settings::n_max_batches)) {
2,567✔
434
    settings::statepoint_batch.insert(simulation::current_batch);
141✔
435
  }
436

437
  // Write out state point if it's been specified for this batch and is not
438
  // a CMFD run instance
439
  if (contains(settings::statepoint_batch, simulation::current_batch) &&
333,066✔
440
      !settings::cmfd_run) {
7,818✔
441
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
15,006✔
442
        settings::source_write && !settings::source_separate) {
14,123✔
443
      bool b = (settings::run_mode == RunMode::EIGENVALUE);
6,410✔
444
      openmc_statepoint_write(nullptr, &b);
6,410✔
445
    } else {
446
      bool b = false;
1,232✔
447
      openmc_statepoint_write(nullptr, &b);
1,232✔
448
    }
449
  }
450

451
  if (settings::run_mode == RunMode::EIGENVALUE) {
166,533✔
452
    // Write out a separate source point if it's been specified for this batch
453
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
107,070✔
454
        settings::source_write && settings::source_separate) {
106,699✔
455

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

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

474
  // Write out surface source if requested.
475
  if (settings::surf_source_write &&
166,533✔
476
      simulation::ssw_current_file <= settings::ssw_max_files) {
17,669✔
477
    bool last_batch = (simulation::current_batch == settings::n_batches);
1,976✔
478
    if (simulation::surf_source_bank.full() || last_batch) {
1,976✔
479
      // Determine appropriate filename
480
      auto filename = fmt::format("{}surface_source.{}", settings::path_output,
1,187✔
481
        simulation::current_batch);
1,187✔
482
      if (settings::ssw_max_files == 1 ||
1,187✔
483
          (simulation::ssw_current_file == 1 && last_batch)) {
55!
484
        filename = settings::path_output + "surface_source";
1,132✔
485
      }
486

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

493
      // Write surface source file
494
      write_source_point(
1,187✔
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();
1,187✔
499
      if (!last_batch && settings::ssw_max_files >= 1) {
1,187!
500
        simulation::surf_source_bank.reserve(settings::ssw_max_particles);
1,005✔
501
      }
502
      ++simulation::ssw_current_file;
1,187✔
503
    }
1,187✔
504
  }
505
  // Write collision track file if requested
506
  if (settings::collision_track) {
166,533✔
507
    collision_track_flush_bank();
608✔
508
  }
509
}
166,533✔
510

511
void initialize_generation()
166,756✔
512
{
513
  if (settings::run_mode == RunMode::EIGENVALUE) {
166,756✔
514
    // Clear out the fission bank
515
    simulation::fission_bank.resize(0);
102,703✔
516

517
    // Count source sites if using uniform fission source weighting
518
    if (settings::ufs_on)
102,703✔
519
      ufs_count_sites();
150✔
520

521
    // Store current value of tracklength k
522
    simulation::keff_generation = simulation::global_tallies(
102,703✔
523
      GlobalTally::K_TRACKLENGTH, TallyResult::VALUE);
524
  }
525
}
166,756✔
526

527
void finalize_generation()
166,743✔
528
{
529
  auto& gt = simulation::global_tallies;
166,743✔
530

531
  // Update global tallies with the accumulation variables
532
  if (settings::run_mode == RunMode::EIGENVALUE) {
166,743✔
533
    gt(GlobalTally::K_COLLISION, TallyResult::VALUE) += global_tally_collision;
102,703✔
534
    gt(GlobalTally::K_ABSORPTION, TallyResult::VALUE) +=
102,703✔
535
      global_tally_absorption;
536
    gt(GlobalTally::K_TRACKLENGTH, TallyResult::VALUE) +=
102,703✔
537
      global_tally_tracklength;
538
  }
539
  gt(GlobalTally::LEAKAGE, TallyResult::VALUE) += global_tally_leakage;
166,743✔
540

541
  // reset tallies
542
  if (settings::run_mode == RunMode::EIGENVALUE) {
166,743✔
543
    global_tally_collision = 0.0;
102,703✔
544
    global_tally_absorption = 0.0;
102,703✔
545
    global_tally_tracklength = 0.0;
102,703✔
546
  }
547
  global_tally_leakage = 0.0;
166,743✔
548

549
  if (settings::run_mode == RunMode::EIGENVALUE &&
166,743✔
550
      settings::solver_type == SolverType::MONTE_CARLO) {
102,703✔
551
    // If using shared memory, stable sort the fission bank (by parent IDs)
552
    // so as to allow for reproducibility regardless of which order particles
553
    // are run in.
554
    sort_fission_bank();
95,853✔
555

556
    // Distribute fission bank across processors evenly
557
    synchronize_bank();
95,853✔
558
  }
559

560
  if (settings::run_mode == RunMode::EIGENVALUE) {
166,743✔
561

562
    // Calculate shannon entropy
563
    if (settings::entropy_on &&
102,703✔
564
        settings::solver_type == SolverType::MONTE_CARLO)
14,535✔
565
      shannon_entropy();
7,685✔
566

567
    // Collect results and statistics
568
    calculate_generation_keff();
102,703✔
569
    calculate_average_keff();
102,703✔
570

571
    // Write generation output
572
    if (mpi::master && settings::verbosity >= 7) {
102,703✔
573
      print_generation();
77,238✔
574
    }
575
  }
576
}
166,743✔
577

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

596
  // set identifier for particle
597
  p.id() = simulation::work_index[mpi::rank] + index_source;
177,878,277✔
598

599
  // set progeny count to zero
600
  p.n_progeny() = 0;
177,878,277✔
601

602
  // Reset particle event counter
603
  p.n_event() = 0;
177,878,277✔
604

605
  // Reset split counter
606
  p.n_split() = 0;
177,878,277✔
607

608
  // Reset weight window ratio
609
  p.ww_factor() = 0.0;
177,878,277✔
610

611
  // set particle history start weight
612
  p.wgt_born() = p.wgt();
177,878,277✔
613

614
  // Reset pulse_height_storage
615
  std::fill(p.pht_storage().begin(), p.pht_storage().end(), 0);
177,878,277✔
616

617
  // set random number seed
618
  int64_t particle_seed =
177,878,277✔
619
    (simulation::total_gen + overall_generation() - 1) * settings::n_particles +
177,878,277✔
620
    p.id();
177,878,277✔
621
  init_particle_seeds(particle_seed, p.seeds());
177,878,277✔
622

623
  // set particle trace
624
  p.trace() = false;
177,878,277✔
625
  if (simulation::current_batch == settings::trace_batch &&
177,889,277✔
626
      simulation::current_gen == settings::trace_gen &&
177,878,277!
627
      p.id() == settings::trace_particle)
11,000✔
628
    p.trace() = true;
11✔
629

630
  // Set particle track.
631
  p.write_track() = check_track_criteria(p);
177,878,277✔
632

633
  // Set the particle's initial weight window value.
634
  p.wgt_ww_born() = -1.0;
177,878,277✔
635
  apply_weight_windows(p);
177,878,277✔
636

637
  // Display message if high verbosity or trace is on
638
  if (settings::verbosity >= 9 || p.trace()) {
177,878,277!
639
    write_message("Simulating Particle {}", p.id());
22✔
640
  }
641

642
// Add particle's starting weight to count for normalizing tallies later
643
#pragma omp atomic
97,368,480✔
644
  simulation::total_weight += p.wgt();
177,878,277✔
645

646
  // Force calculation of cross-sections by setting last energy to zero
647
  if (settings::run_CE) {
177,878,277✔
648
    p.invalidate_neutron_xs();
65,853,947✔
649
  }
650

651
  // Prepare to write out particle track.
652
  if (p.write_track())
177,878,277✔
653
    add_particle_track(p);
999✔
654
}
177,878,277✔
655

656
int overall_generation()
205,873,905✔
657
{
658
  using namespace simulation;
205,873,905✔
659
  return settings::gen_per_batch * (current_batch - 1) + current_gen;
205,873,905✔
660
}
661

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

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

670
  int64_t i_bank = 0;
7,563✔
671
  simulation::work_index.resize(mpi::n_procs + 1);
7,563✔
672
  simulation::work_index[0] = 0;
7,563✔
673
  for (int i = 0; i < mpi::n_procs; ++i) {
17,085✔
674
    // Number of particles for rank i
675
    int64_t work_i = i < remainder ? min_work + 1 : min_work;
9,522!
676

677
    // Set number of particles
678
    if (mpi::rank == i)
9,522✔
679
      simulation::work_per_rank = work_i;
7,563✔
680

681
    // Set index into source bank for rank i
682
    i_bank += work_i;
9,522✔
683
    simulation::work_index[i + 1] = i_bank;
9,522✔
684
  }
685
}
7,563✔
686

687
void initialize_data()
6,215✔
688
{
689
  // Determine minimum/maximum energy for incident neutron/photon data
690
  data::energy_max = {INFTY, INFTY, INFTY, INFTY};
6,215✔
691
  data::energy_min = {0.0, 0.0, 0.0, 0.0};
6,215✔
692

693
  for (const auto& nuc : data::nuclides) {
38,456✔
694
    if (nuc->grid_.size() >= 1) {
32,241!
695
      int neutron = ParticleType::neutron().transport_index();
32,241✔
696
      data::energy_min[neutron] =
32,241✔
697
        std::max(data::energy_min[neutron], nuc->grid_[0].energy.front());
37,937✔
698
      data::energy_max[neutron] =
32,241✔
699
        std::min(data::energy_max[neutron], nuc->grid_[0].energy.back());
39,496✔
700
    }
701
  }
702

703
  if (settings::photon_transport) {
6,215✔
704
    for (const auto& elem : data::elements) {
1,027✔
705
      if (elem->energy_.size() >= 1) {
693!
706
        int photon = ParticleType::photon().transport_index();
693✔
707
        int n = elem->energy_.size();
693✔
708
        data::energy_min[photon] =
1,386✔
709
          std::max(data::energy_min[photon], std::exp(elem->energy_(1)));
1,186✔
710
        data::energy_max[photon] =
693✔
711
          std::min(data::energy_max[photon], std::exp(elem->energy_(n - 1)));
1,027✔
712
      }
713
    }
714

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

724
        const std::vector<int> charged = {electron, positron};
275✔
725
        for (auto t : charged) {
825✔
726
          data::energy_min[t] = std::exp(data::ttb_e_grid(1));
550✔
727
          data::energy_max[t] = std::exp(data::ttb_e_grid(n_e - 1));
550✔
728
        }
729

730
        data::energy_min[photon] =
550✔
731
          std::max(data::energy_min[photon], data::energy_min[electron]);
550!
732

733
        data::energy_max[photon] =
550✔
734
          std::min(data::energy_max[photon], data::energy_max[electron]);
550!
735
      }
275✔
736
    }
737
  }
738

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

758
  // Set up logarithmic grid for nuclides
759
  for (auto& nuc : data::nuclides) {
38,456✔
760
    nuc->init_grid();
32,241✔
761
  }
762
  int neutron = ParticleType::neutron().transport_index();
6,215✔
763
  simulation::log_spacing =
12,430✔
764
    std::log(data::energy_max[neutron] / data::energy_min[neutron]) /
6,215✔
765
    settings::n_log_bins;
766
}
6,215✔
767

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

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

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

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

801
#endif
802

803
void free_memory_simulation()
8,561✔
804
{
805
  simulation::k_generation.clear();
8,561✔
806
  simulation::entropy.clear();
8,561✔
807
}
8,561✔
808

809
void transport_history_based_single_particle(Particle& p)
165,701,760✔
810
{
811
  while (p.alive()) {
2,147,483,647✔
812
    p.event_calculate_xs();
2,147,483,647✔
813
    if (p.alive()) {
2,147,483,647!
814
      p.event_advance();
2,147,483,647✔
815
    }
816
    if (p.alive()) {
2,147,483,647!
817
      switch (p.next_event().event_type) {
2,147,483,647!
818
      case EVENT_CROSS_SURFACE:
2,147,483,647✔
819
        p.event_cross_surface();
2,147,483,647✔
820
        break;
2,147,483,647✔
821
      case EVENT_COLLIDE:
2,147,483,647✔
822
        p.event_collide();
2,147,483,647✔
823
        break;
2,147,483,647✔
824
      case EVENT_TIME_CUTOFF:
223,928✔
825
        p.wgt() = 0.0;
223,928✔
826
        break;
223,928✔
NEW
827
      default:
×
NEW
828
        fatal_error(
×
NEW
829
          fmt::format("Unknown event '{}' in history-based transport!",
×
NEW
830
            p.next_event().event_type));
×
831
        break;
832
      }
833
    }
834
    p.event_revive_from_secondary();
2,147,483,647✔
835
  }
836
  p.event_death();
165,701,751✔
837
}
165,701,751✔
838

839
void transport_history_based()
142,491✔
840
{
841
#pragma omp parallel for schedule(runtime)
79,398✔
842
  for (int64_t i_work = 1; i_work <= simulation::work_per_rank; ++i_work) {
80,835,713✔
843
    Particle p;
80,772,629✔
844
    initialize_history(p, i_work);
80,772,629✔
845
    transport_history_based_single_particle(p);
80,772,625✔
846
  }
80,772,620✔
847
}
142,482✔
848

849
void transport_event_based()
3,303✔
850
{
851
  int64_t remaining_work = simulation::work_per_rank;
3,303✔
852
  int64_t source_offset = 0;
3,303✔
853

854
  // To cap the total amount of memory used to store particle object data, the
855
  // number of particles in flight at any point in time can bet set. In the case
856
  // that the maximum in flight particle count is lower than the total number
857
  // of particles that need to be run this iteration, the event-based transport
858
  // loop is executed multiple times until all particles have been completed.
859
  while (remaining_work > 0) {
6,606✔
860
    // Figure out # of particles to run for this subiteration
861
    int64_t n_particles =
3,303!
862
      std::min(remaining_work, settings::max_particles_in_flight);
3,303✔
863

864
    // Initialize all particle histories for this subiteration
865
    process_init_events(n_particles, source_offset);
3,303✔
866

867
    // Event-based transport loop
868
    while (true) {
2,958,718✔
869
      // Determine which event kernel has the longest queue
870
      int64_t max = std::max({simulation::calculate_fuel_xs_queue.size(),
2,958,718✔
871
        simulation::calculate_nonfuel_xs_queue.size(),
2,958,718✔
872
        simulation::advance_particle_queue.size(),
2,958,718✔
873
        simulation::surface_crossing_queue.size(),
2,958,718✔
874
        simulation::collision_queue.size()});
2,958,718✔
875

876
      // Execute event with the longest queue
877
      if (max == 0) {
2,958,718✔
878
        break;
879
      } else if (max == simulation::calculate_fuel_xs_queue.size()) {
2,955,415✔
880
        process_calculate_xs_events(simulation::calculate_fuel_xs_queue);
435,863✔
881
      } else if (max == simulation::calculate_nonfuel_xs_queue.size()) {
2,519,552✔
882
        process_calculate_xs_events(simulation::calculate_nonfuel_xs_queue);
546,502✔
883
      } else if (max == simulation::advance_particle_queue.size()) {
1,973,050✔
884
        process_advance_particle_events();
977,074✔
885
      } else if (max == simulation::surface_crossing_queue.size()) {
995,976✔
886
        process_surface_crossing_events();
267,051✔
887
      } else if (max == simulation::collision_queue.size()) {
728,925!
888
        process_collision_events();
728,925✔
889
      }
890
    }
891

892
    // Execute death event for all particles
893
    process_death_events(n_particles);
3,303✔
894

895
    // Adjust remaining work and source offset variables
896
    remaining_work -= n_particles;
3,303✔
897
    source_offset += n_particles;
3,303✔
898
  }
899
}
3,303✔
900

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