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

openmc-dev / openmc / 21043561037

15 Jan 2026 07:23PM UTC coverage: 81.388% (-0.7%) from 82.044%
21043561037

Pull #3734

github

web-flow
Merge 5e79b7b6f into 179048b80
Pull Request #3734: Specify temperature from a field (structured mesh only)

16703 of 22995 branches covered (72.64%)

Branch coverage included in aggregate %.

156 of 179 new or added lines in 12 files covered. (87.15%)

843 existing lines in 36 files now uncovered.

54487 of 64475 relevant lines covered (84.51%)

27592585.27 hits per line

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

87.23
/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 "xtensor/xview.hpp"
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()
2,679✔
56
{
57
  openmc::simulation::time_total.start();
2,679✔
58
  openmc_simulation_init();
2,679✔
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;
2,679✔
63
  if (openmc::simulation::current_batch >= openmc::settings::n_max_batches) {
2,679✔
64
    status = openmc::STATUS_EXIT_MAX_BATCH;
5✔
65
  }
66

67
  int err = 0;
2,679✔
68
  while (status == 0 && err == 0) {
52,099!
69
    err = openmc_next_batch(&status);
49,425✔
70
  }
71

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

77
int openmc_simulation_init()
3,135✔
78
{
79
  using namespace openmc;
80

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

85
  // Initialize nuclear data (energy limits, log grid)
86
  if (settings::run_CE) {
3,130✔
87
    initialize_data();
2,574✔
88
  }
89

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

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

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

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

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

115
  // Set up material nuclide index mapping
116
  for (auto& mat : model::materials) {
11,310✔
117
    mat->init_nuclide_index();
8,180✔
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;
3,130✔
123
  simulation::ct_current_file = 1;
3,130✔
124
  simulation::ssw_current_file = 1;
3,130✔
125
  simulation::k_generation.clear();
3,130✔
126
  simulation::entropy.clear();
3,130✔
127
  openmc_reset();
3,130✔
128

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

142
  // Display header
143
  if (mpi::master) {
3,130✔
144
    if (settings::run_mode == RunMode::FIXED_SOURCE) {
2,683✔
145
      if (settings::solver_type == SolverType::MONTE_CARLO) {
1,106✔
146
        header("FIXED SOURCE TRANSPORT SIMULATION", 3);
956✔
147
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
150!
148
        header("FIXED SOURCE TRANSPORT SIMULATION (RANDOM RAY SOLVER)", 3);
150✔
149
      }
150
    } else if (settings::run_mode == RunMode::EIGENVALUE) {
1,577!
151
      if (settings::solver_type == SolverType::MONTE_CARLO) {
1,577✔
152
        header("K EIGENVALUE SIMULATION", 3);
1,492✔
153
      } else if (settings::solver_type == SolverType::RANDOM_RAY) {
85!
154
        header("K EIGENVALUE SIMULATION (RANDOM RAY SOLVER)", 3);
85✔
155
      }
156
      if (settings::verbosity >= 7)
1,577✔
157
        print_columns();
1,485✔
158
    }
159
  }
160

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

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

171
int openmc_simulation_finalize()
3,125✔
172
{
173
  using namespace openmc;
174

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

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

183
  // Clear material nuclide mapping
184
  for (auto& mat : model::materials) {
11,300✔
185
    mat->mat_nuclide_index_.clear();
8,175✔
186
  }
187

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

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

196
#ifdef OPENMC_MPI
197
  broadcast_results();
1,514✔
198
#endif
199

200
  // Write tally results to tallies.out
201
  if (settings::output_tallies && mpi::master)
3,125!
202
    write_tallies();
2,598✔
203

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

210
  // Deactivate all tallies
211
  for (auto& t : model::tallies) {
14,906✔
212
    t->active_ = false;
11,781✔
213
  }
214

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

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

234
int openmc_next_batch(int* status)
51,300✔
235
{
236
  using namespace openmc;
237
  using openmc::simulation::current_gen;
238

239
  // Make sure simulation has been initialized
240
  if (!simulation::initialized) {
51,300✔
241
    set_errmsg("Simulation has not been initialized yet.");
5✔
242
    return OPENMC_E_ALLOCATE;
5✔
243
  }
244

245
  initialize_batch();
51,295✔
246

247
  // =======================================================================
248
  // LOOP OVER GENERATIONS
249
  for (current_gen = 1; current_gen <= settings::gen_per_batch; ++current_gen) {
102,683✔
250

251
    initialize_generation();
51,393✔
252

253
    // Start timer for transport
254
    simulation::time_transport.start();
51,393✔
255

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

263
    // Accumulate time for transport
264
    simulation::time_transport.stop();
51,388✔
265

266
    finalize_generation();
51,388✔
267
  }
268

269
  finalize_batch();
51,290✔
270

271
  // Check simulation ending criteria
272
  if (status) {
51,290!
273
    if (simulation::current_batch >= settings::n_max_batches) {
51,290✔
274
      *status = STATUS_EXIT_MAX_BATCH;
2,761✔
275
    } else if (simulation::satisfy_triggers) {
48,529✔
276
      *status = STATUS_EXIT_ON_TRIGGER;
43✔
277
    } else {
278
      *status = STATUS_EXIT_NORMAL;
48,486✔
279
    }
280
  }
281
  return 0;
51,290✔
282
}
283

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

289
  if (!simulation::initialized)
1,425!
290
    return false;
×
291
  else
292
    return contains(settings::statepoint_batch, simulation::current_batch);
1,425✔
293
}
294

295
namespace openmc {
296

297
//==============================================================================
298
// Global variables
299
//==============================================================================
300

301
namespace simulation {
302

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

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

325
TemperatureField temperature_field;
326

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

330
} // namespace simulation
331

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

336
void allocate_banks()
3,130✔
337
{
338
  if (settings::run_mode == RunMode::EIGENVALUE &&
3,130✔
339
      settings::solver_type == SolverType::MONTE_CARLO) {
1,903✔
340
    // Allocate source bank
341
    simulation::source_bank.resize(simulation::work_per_rank);
1,784✔
342

343
    // Allocate fission bank
344
    init_fission_bank(3 * simulation::work_per_rank);
1,784✔
345

346
    // Allocate IFP bank
347
    if (settings::ifp_on) {
1,784✔
348
      resize_simulation_ifp_banks();
34✔
349
    }
350
  }
351

352
  if (settings::surf_source_write) {
3,130✔
353
    // Allocate surface source bank
354
    simulation::surf_source_bank.reserve(settings::ssw_max_particles);
447✔
355
  }
356

357
  if (settings::collision_track) {
3,130✔
358
    // Allocate collision track bank
359
    collision_track_reserve_bank();
69✔
360
  }
361
}
3,130✔
362

363
void initialize_batch()
59,135✔
364
{
365
  // Increment current batch
366
  ++simulation::current_batch;
59,135✔
367
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
59,135✔
368
    if (settings::solver_type == SolverType::RANDOM_RAY &&
20,983✔
369
        simulation::current_batch < settings::n_inactive + 1) {
6,160✔
370
      write_message(
3,745✔
371
        6, "Simulating batch {:<4} (inactive)", simulation::current_batch);
372
    } else {
373
      write_message(6, "Simulating batch {}", simulation::current_batch);
17,238✔
374
    }
375
  }
376

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

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

391
  // Manage active/inactive timers and activate tallies if necessary.
392
  if (first_inactive) {
59,135✔
393
    simulation::time_inactive.start();
1,599✔
394
  } else if (first_active) {
57,536✔
395
    simulation::time_inactive.stop();
3,108✔
396
    simulation::time_active.start();
3,108✔
397
    for (auto& t : model::tallies) {
14,879✔
398
      t->active_ = true;
11,771✔
399
    }
400
  }
401

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

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

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

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

424
  // Check_triggers
425
  if (mpi::master)
59,130✔
426
    check_triggers();
50,090✔
427
#ifdef OPENMC_MPI
428
  MPI_Bcast(&simulation::satisfy_triggers, 1, MPI_C_BOOL, 0, mpi::intracomm);
29,038✔
429
#endif
430
  if (simulation::satisfy_triggers ||
59,130✔
431
      (settings::trigger_on &&
1,181✔
432
        simulation::current_batch == settings::n_max_batches)) {
1,181✔
433
    settings::statepoint_batch.insert(simulation::current_batch);
65✔
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) &&
62,358✔
439
      !settings::cmfd_run) {
3,228✔
440
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
6,198✔
441
        settings::source_write && !settings::source_separate) {
6,198✔
442
      bool b = (settings::run_mode == RunMode::EIGENVALUE);
2,688✔
443
      openmc_statepoint_write(nullptr, &b);
2,688✔
444
    } else {
445
      bool b = false;
460✔
446
      openmc_statepoint_write(nullptr, &b);
460✔
447
    }
448
  }
449

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

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

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

473
  // Write out surface source if requested.
474
  if (settings::surf_source_write &&
59,130✔
475
      simulation::ssw_current_file <= settings::ssw_max_files) {
4,240✔
476
    bool last_batch = (simulation::current_batch == settings::n_batches);
832✔
477
    if (simulation::surf_source_bank.full() || last_batch) {
832✔
478
      // Determine appropriate filename
479
      auto filename = fmt::format("{}surface_source.{}", settings::path_output,
480
        simulation::current_batch);
278✔
481
      if (settings::ssw_max_files == 1 ||
462✔
482
          (simulation::ssw_current_file == 1 && last_batch)) {
25!
483
        filename = settings::path_output + "surface_source";
437✔
484
      }
485

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

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

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

510
void initialize_generation()
59,233✔
511
{
512
  if (settings::run_mode == RunMode::EIGENVALUE) {
59,233✔
513
    // Clear out the fission bank
514
    simulation::fission_bank.resize(0);
38,250✔
515

516
    // Count source sites if using uniform fission source weighting
517
    if (settings::ufs_on)
38,250✔
518
      ufs_count_sites();
70✔
519

520
    // Store current value of tracklength k
521
    simulation::keff_generation = simulation::global_tallies(
38,250✔
522
      GlobalTally::K_TRACKLENGTH, TallyResult::VALUE);
523
  }
524
}
59,233✔
525

526
void finalize_generation()
59,228✔
527
{
528
  auto& gt = simulation::global_tallies;
59,228✔
529

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

540
  // reset tallies
541
  if (settings::run_mode == RunMode::EIGENVALUE) {
59,228✔
542
    global_tally_collision = 0.0;
38,250✔
543
    global_tally_absorption = 0.0;
38,250✔
544
    global_tally_tracklength = 0.0;
38,250✔
545
  }
546
  global_tally_leakage = 0.0;
59,228✔
547

548
  if (settings::run_mode == RunMode::EIGENVALUE &&
59,228✔
549
      settings::solver_type == SolverType::MONTE_CARLO) {
38,250✔
550
    // If using shared memory, stable sort the fission bank (by parent IDs)
551
    // so as to allow for reproducibility regardless of which order particles
552
    // are run in.
553
    sort_fission_bank();
36,570✔
554

555
    // Distribute fission bank across processors evenly
556
    synchronize_bank();
36,570✔
557
  }
558

559
  if (settings::run_mode == RunMode::EIGENVALUE) {
59,228✔
560

561
    // Calculate shannon entropy
562
    if (settings::entropy_on &&
38,250✔
563
        settings::solver_type == SolverType::MONTE_CARLO)
5,175✔
564
      shannon_entropy();
3,495✔
565

566
    // Collect results and statistics
567
    calculate_generation_keff();
38,250✔
568
    calculate_average_keff();
38,250✔
569

570
    // Write generation output
571
    if (mpi::master && settings::verbosity >= 7) {
38,250✔
572
      print_generation();
30,190✔
573
    }
574
  }
575
}
59,228✔
576

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

595
  // set identifier for particle
596
  p.id() = simulation::work_index[mpi::rank] + index_source;
75,632,119✔
597

598
  // set progeny count to zero
599
  p.n_progeny() = 0;
75,632,119✔
600

601
  // Reset particle event counter
602
  p.n_event() = 0;
75,632,119✔
603

604
  // Reset split counter
605
  p.n_split() = 0;
75,632,119✔
606

607
  // Reset weight window ratio
608
  p.ww_factor() = 0.0;
75,632,119✔
609

610
  // set particle history start weight
611
  p.wgt_born() = p.wgt();
75,632,119✔
612

613
  // Reset pulse_height_storage
614
  std::fill(p.pht_storage().begin(), p.pht_storage().end(), 0);
75,632,119✔
615

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

622
  // set particle trace
623
  p.trace() = false;
75,632,119✔
624
  if (simulation::current_batch == settings::trace_batch &&
151,269,238✔
625
      simulation::current_gen == settings::trace_gen &&
75,637,119!
626
      p.id() == settings::trace_particle)
5,000✔
627
    p.trace() = true;
5✔
628

629
  // Set particle track.
630
  p.write_track() = check_track_criteria(p);
75,632,119✔
631

632
  // Set the particle's initial weight window value.
633
  p.wgt_ww_born() = -1.0;
75,632,119✔
634
  apply_weight_windows(p);
75,632,119✔
635

636
  // Display message if high verbosity or trace is on
637
  if (settings::verbosity >= 9 || p.trace()) {
75,632,119!
638
    write_message("Simulating Particle {}", p.id());
5✔
639
  }
640

641
// Add particle's starting weight to count for normalizing tallies later
642
#pragma omp atomic
30,330,334✔
643
  simulation::total_weight += p.wgt();
75,632,119✔
644

645
  // Force calculation of cross-sections by setting last energy to zero
646
  if (settings::run_CE) {
75,632,119✔
647
    p.invalidate_neutron_xs();
24,712,119✔
648
  }
649

650
  // Prepare to write out particle track.
651
  if (p.write_track())
75,632,119✔
652
    add_particle_track(p);
465✔
653
}
75,632,119✔
654

655
int overall_generation()
87,391,355✔
656
{
657
  using namespace simulation;
658
  return settings::gen_per_batch * (current_batch - 1) + current_gen;
87,391,355✔
659
}
660

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

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

669
  int64_t i_bank = 0;
3,130✔
670
  simulation::work_index.resize(mpi::n_procs + 1);
3,130✔
671
  simulation::work_index[0] = 0;
3,130✔
672
  for (int i = 0; i < mpi::n_procs; ++i) {
7,154✔
673
    // Number of particles for rank i
674
    int64_t work_i = i < remainder ? min_work + 1 : min_work;
4,024!
675

676
    // Set number of particles
677
    if (mpi::rank == i)
4,024✔
678
      simulation::work_per_rank = work_i;
3,130✔
679

680
    // Set index into source bank for rank i
681
    i_bank += work_i;
4,024✔
682
    simulation::work_index[i + 1] = i_bank;
4,024✔
683
  }
684
}
3,130✔
685

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

692
  for (const auto& nuc : data::nuclides) {
15,331✔
693
    if (nuc->grid_.size() >= 1) {
12,742!
694
      int neutron = static_cast<int>(ParticleType::neutron);
12,742✔
695
      data::energy_min[neutron] =
25,484✔
696
        std::max(data::energy_min[neutron], nuc->grid_[0].energy.front());
12,742✔
697
      data::energy_max[neutron] =
12,742✔
698
        std::min(data::energy_max[neutron], nuc->grid_[0].energy.back());
12,742✔
699
    }
700
  }
701

702
  if (settings::photon_transport) {
2,589✔
703
    for (const auto& elem : data::elements) {
429✔
704
      if (elem->energy_.size() >= 1) {
292!
705
        int photon = static_cast<int>(ParticleType::photon);
292✔
706
        int n = elem->energy_.size();
292✔
707
        data::energy_min[photon] =
584✔
708
          std::max(data::energy_min[photon], std::exp(elem->energy_(1)));
292✔
709
        data::energy_max[photon] =
584✔
710
          std::min(data::energy_max[photon], std::exp(elem->energy_(n - 1)));
292✔
711
      }
712
    }
713

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

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

729
        data::energy_min[photon] =
244✔
730
          std::max(data::energy_min[photon], data::energy_min[electron]);
122✔
731

732
        data::energy_max[photon] =
244✔
733
          std::min(data::energy_max[photon], data::energy_max[electron]);
122✔
734
      }
122✔
735
    }
736
  }
737

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

757
  // Set up logarithmic grid for nuclides
758
  for (auto& nuc : data::nuclides) {
15,331✔
759
    nuc->init_grid();
12,742✔
760
  }
761
  int neutron = static_cast<int>(ParticleType::neutron);
2,589✔
762
  simulation::log_spacing =
2,589✔
763
    std::log(data::energy_max[neutron] / data::energy_min[neutron]) /
2,589✔
764
    settings::n_log_bins;
765
}
2,589✔
766

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

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

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

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

800
#endif
801

802
void free_memory_simulation()
3,594✔
803
{
804
  simulation::k_generation.clear();
3,594✔
805
  simulation::entropy.clear();
3,594✔
806
}
3,594✔
807

808
void transport_history_based_single_particle(Particle& p)
75,632,134✔
809
{
810
  while (p.alive()) {
1,915,300,974✔
811
    p.event_calculate_xs();
1,839,668,843✔
812
    if (p.alive()) {
1,839,668,843!
813
      p.event_advance();
1,839,668,843✔
814
    }
815
    if (p.alive()) {
1,839,668,843!
816
      switch (p.next_event()) {
1,839,668,843!
817
      case EVENT_CROSS_SURFACE:
627,686,088✔
818
        p.event_cross_surface();
627,686,088✔
819
        break;
627,686,085✔
820
      case EVENT_COLLIDE:
1,211,740,280✔
821
        p.event_collide();
1,211,740,280✔
822
        break;
1,211,740,280✔
823
      case EVENT_CROSS_TEMPERATURE_MESH:
140,235✔
824
        p.event_cross_temperature_mesh();
140,235✔
825
        break;
140,235✔
826
      case EVENT_TIME_CUTOFF:
102,240✔
827
        p.wgt() = 0.0;
102,240✔
828
        break;
102,240✔
NEW
829
      default:
×
NEW
830
        fatal_error(fmt::format(
×
831
          "Unknown event '{}' in history-based transport!", p.next_event()));
832
        break;
833
      }
834
    }
835
    p.event_revive_from_secondary();
1,839,668,840✔
836
  }
837
  p.event_death();
75,632,131✔
838
}
75,632,131✔
839

840
void transport_history_based()
51,393✔
841
{
842
#pragma omp parallel for schedule(runtime)
843
  for (int64_t i_work = 1; i_work <= simulation::work_per_rank; ++i_work) {
45,349,747✔
844
    Particle p;
45,319,681✔
845
    initialize_history(p, i_work);
45,319,681✔
846
    transport_history_based_single_particle(p);
45,319,679✔
847
  }
45,319,676✔
848
}
51,388✔
849

UNCOV
850
void transport_event_based()
×
851
{
UNCOV
852
  int64_t remaining_work = simulation::work_per_rank;
×
UNCOV
853
  int64_t source_offset = 0;
×
854

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

865
    // Initialize all particle histories for this subiteration
UNCOV
866
    process_init_events(n_particles, source_offset);
×
867

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

877
      // Execute event with the longest queue
UNCOV
878
      if (max == 0) {
×
UNCOV
879
        break;
×
UNCOV
880
      } else if (max == simulation::calculate_fuel_xs_queue.size()) {
×
UNCOV
881
        process_calculate_xs_events(simulation::calculate_fuel_xs_queue);
×
UNCOV
882
      } else if (max == simulation::calculate_nonfuel_xs_queue.size()) {
×
UNCOV
883
        process_calculate_xs_events(simulation::calculate_nonfuel_xs_queue);
×
UNCOV
884
      } else if (max == simulation::advance_particle_queue.size()) {
×
UNCOV
885
        process_advance_particle_events();
×
UNCOV
886
      } else if (max == simulation::surface_crossing_queue.size()) {
×
UNCOV
887
        process_surface_crossing_events();
×
UNCOV
888
      } else if (max == simulation::collision_queue.size()) {
×
UNCOV
889
        process_collision_events();
×
890
      }
UNCOV
891
    }
×
892

893
    // Execute death event for all particles
UNCOV
894
    process_death_events(n_particles);
×
895

896
    // Adjust remaining work and source offset variables
UNCOV
897
    remaining_work -= n_particles;
×
UNCOV
898
    source_offset += n_particles;
×
899
  }
UNCOV
900
}
×
901

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