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

openmc-dev / openmc / 4812163747

pending completion
4812163747

push

github

GitHub
Merge pull request #2494 from aprilnovak/delete-tally

1 of 2 new or added lines in 1 file covered. (50.0%)

124 existing lines in 19 files now uncovered.

42440 of 50752 relevant lines covered (83.62%)

78971666.53 hits per line

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

97.92
/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

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

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

37
#include <fmt/format.h>
38

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

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

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

51
int openmc_run()
4,585✔
52
{
53
  openmc::simulation::time_total.start();
4,585✔
54
  openmc_simulation_init();
4,585✔
55

56
  int err = 0;
4,573✔
57
  int status = 0;
4,573✔
58
  while (status == 0 && err == 0) {
87,960✔
59
    err = openmc_next_batch(&status);
83,387✔
60
  }
61

62
  openmc_simulation_finalize();
4,573✔
63
  openmc::simulation::time_total.stop();
4,573✔
64
  return err;
4,573✔
65
}
66

67
int openmc_simulation_init()
5,523✔
68
{
69
  using namespace openmc;
70

71
  // Skip if simulation has already been initialized
72
  if (simulation::initialized)
5,523✔
73
    return 0;
14✔
74

75
  // Initialize nuclear data (energy limits, log grid)
76
  if (settings::run_CE) {
5,509✔
77
    initialize_data();
4,939✔
78
  }
79

80
  // Determine how much work each process should do
81
  calculate_work();
5,509✔
82

83
  // Allocate source, fission and surface source banks.
84
  allocate_banks();
5,509✔
85

86
  // Create track file if needed
87
  if (!settings::track_identifiers.empty() || settings::write_all_tracks) {
5,509✔
88
    open_track_file();
114✔
89
  }
90

91
  // If doing an event-based simulation, intialize the particle buffer
92
  // and event queues
93
  if (settings::event_based) {
5,509✔
94
    int64_t event_buffer_length =
95
      std::min(simulation::work_per_rank, settings::max_particles_in_flight);
117✔
96
    init_event_queues(event_buffer_length);
117✔
97
  }
98

99
  // Allocate tally results arrays if they're not allocated yet
100
  for (auto& t : model::tallies) {
35,608✔
101
    t->set_strides();
30,099✔
102
    t->init_results();
30,099✔
103
  }
104

105
  // Set up material nuclide index mapping
106
  for (auto& mat : model::materials) {
25,597✔
107
    mat->init_nuclide_index();
20,088✔
108
  }
109

110
  // Reset global variables -- this is done before loading state point (as that
111
  // will potentially populate k_generation and entropy)
112
  simulation::current_batch = 0;
5,509✔
113
  simulation::k_generation.clear();
5,509✔
114
  simulation::entropy.clear();
5,509✔
115
  openmc_reset();
5,509✔
116

117
  // If this is a restart run, load the state point data and binary source
118
  // file
119
  if (settings::restart_run) {
5,509✔
120
    load_state_point();
134✔
121
    write_message("Resuming simulation...", 6);
122✔
122
  } else {
123
    // Only initialize primary source bank for eigenvalue simulations
124
    if (settings::run_mode == RunMode::EIGENVALUE) {
5,375✔
125
      initialize_source();
4,093✔
126
    }
127
  }
128

129
  // Display header
130
  if (mpi::master) {
5,497✔
131
    if (settings::run_mode == RunMode::FIXED_SOURCE) {
4,768✔
132
      header("FIXED SOURCE TRANSPORT SIMULATION", 3);
1,173✔
133
    } else if (settings::run_mode == RunMode::EIGENVALUE) {
3,595✔
134
      header("K EIGENVALUE SIMULATION", 3);
3,595✔
135
      if (settings::verbosity >= 7)
3,595✔
136
        print_columns();
3,286✔
137
    }
138
  }
139

140
  // Set flag indicating initialization is done
141
  simulation::initialized = true;
5,497✔
142
  return 0;
5,497✔
143
}
144

145
int openmc_simulation_finalize()
5,497✔
146
{
147
  using namespace openmc;
148

149
  // Skip if simulation was never run
150
  if (!simulation::initialized)
5,497✔
151
    return 0;
×
152

153
  // Stop active batch timer and start finalization timer
154
  simulation::time_active.stop();
5,497✔
155
  simulation::time_finalize.start();
5,497✔
156

157
  // Clear material nuclide mapping
158
  for (auto& mat : model::materials) {
25,573✔
159
    mat->mat_nuclide_index_.clear();
20,076✔
160
  }
161

162
  // Close track file if open
163
  if (!settings::track_identifiers.empty() || settings::write_all_tracks) {
5,497✔
164
    close_track_file();
114✔
165
  }
166

167
  // Increment total number of generations
168
  simulation::total_gen += simulation::current_batch * settings::gen_per_batch;
5,497✔
169

170
#ifdef OPENMC_MPI
171
  broadcast_results();
2,868✔
172
#endif
173

174
  // Write tally results to tallies.out
175
  if (settings::output_tallies && mpi::master)
5,497✔
176
    write_tallies();
4,754✔
177

178
  // Deactivate all tallies
179
  for (auto& t : model::tallies) {
35,572✔
180
    t->active_ = false;
30,075✔
181
  }
182

183
  // Stop timers and show timing statistics
184
  simulation::time_finalize.stop();
5,497✔
185
  simulation::time_total.stop();
5,497✔
186
  if (mpi::master) {
5,497✔
187
    if (settings::verbosity >= 6)
4,768✔
188
      print_runtime();
4,459✔
189
    if (settings::verbosity >= 4)
4,768✔
190
      print_results();
4,459✔
191
  }
192
  if (settings::check_overlaps)
5,497✔
193
    print_overlap_check();
×
194

195
  // Reset flags
196
  simulation::initialized = false;
5,497✔
197
  return 0;
5,497✔
198
}
199

200
int openmc_next_batch(int* status)
100,607✔
201
{
202
  using namespace openmc;
203
  using openmc::simulation::current_gen;
204

205
  // Make sure simulation has been initialized
206
  if (!simulation::initialized) {
100,607✔
207
    set_errmsg("Simulation has not been initialized yet.");
14✔
208
    return OPENMC_E_ALLOCATE;
14✔
209
  }
210

211
  initialize_batch();
100,593✔
212

213
  // =======================================================================
214
  // LOOP OVER GENERATIONS
215
  for (current_gen = 1; current_gen <= settings::gen_per_batch; ++current_gen) {
201,452✔
216

217
    initialize_generation();
100,859✔
218

219
    // Start timer for transport
220
    simulation::time_transport.start();
100,859✔
221

222
    // Transport loop
223
    if (settings::event_based) {
100,859✔
224
      transport_event_based();
2,943✔
225
    } else {
226
      transport_history_based();
97,916✔
227
    }
228

229
    // Accumulate time for transport
230
    simulation::time_transport.stop();
100,859✔
231

232
    finalize_generation();
100,859✔
233
  }
234

235
  finalize_batch();
100,593✔
236

237
  // Check simulation ending criteria
238
  if (status) {
100,593✔
239
    if (simulation::current_batch >= settings::n_max_batches) {
100,593✔
240
      *status = STATUS_EXIT_MAX_BATCH;
5,515✔
241
    } else if (simulation::satisfy_triggers) {
95,078✔
242
      *status = STATUS_EXIT_ON_TRIGGER;
66✔
243
    } else {
244
      *status = STATUS_EXIT_NORMAL;
95,012✔
245
    }
246
  }
247
  return 0;
100,593✔
248
}
249

250
bool openmc_is_statepoint_batch()
15,960✔
251
{
252
  using namespace openmc;
253
  using openmc::simulation::current_gen;
254

255
  if (!simulation::initialized)
15,960✔
256
    return false;
×
257
  else
258
    return contains(settings::statepoint_batch, simulation::current_batch);
15,960✔
259
}
260

261
namespace openmc {
262

263
//==============================================================================
264
// Global variables
265
//==============================================================================
266

267
namespace simulation {
268

269
int current_batch;
270
int current_gen;
271
bool initialized {false};
272
double keff {1.0};
273
double keff_std;
274
double k_col_abs {0.0};
275
double k_col_tra {0.0};
276
double k_abs_tra {0.0};
277
double log_spacing;
278
int n_lost_particles {0};
279
bool need_depletion_rx {false};
280
int restart_batch;
281
bool satisfy_triggers {false};
282
int total_gen {0};
283
double total_weight;
284
int64_t work_per_rank;
285

286
const RegularMesh* entropy_mesh {nullptr};
287
const RegularMesh* ufs_mesh {nullptr};
288

289
vector<double> k_generation;
290
vector<int64_t> work_index;
291

292
} // namespace simulation
293

294
//==============================================================================
295
// Non-member functions
296
//==============================================================================
297

298
void allocate_banks()
5,509✔
299
{
300
  if (settings::run_mode == RunMode::EIGENVALUE) {
5,509✔
301
    // Allocate source bank
302
    simulation::source_bank.resize(simulation::work_per_rank);
4,227✔
303

304
    // Allocate fission bank
305
    init_fission_bank(3 * simulation::work_per_rank);
4,227✔
306
  }
307

308
  if (settings::surf_source_write) {
5,509✔
309
    // Allocate surface source bank
310
    simulation::surf_source_bank.reserve(settings::max_surface_particles);
19✔
311
  }
312
}
5,509✔
313

314
void initialize_batch()
100,593✔
315
{
316
  // Increment current batch
317
  ++simulation::current_batch;
100,593✔
318

319
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
100,593✔
320
    write_message(6, "Simulating batch {}", simulation::current_batch);
9,698✔
321
  }
322

323
  // Reset total starting particle weight used for normalizing tallies
324
  simulation::total_weight = 0.0;
100,593✔
325

326
  // Determine if this batch is the first inactive or active batch.
327
  bool first_inactive = false;
100,593✔
328
  bool first_active = false;
100,593✔
329
  if (!settings::restart_run) {
100,593✔
330
    first_inactive = settings::n_inactive > 0 && simulation::current_batch == 1;
100,030✔
331
    first_active = simulation::current_batch == settings::n_inactive + 1;
100,030✔
332
  } else if (simulation::current_batch == simulation::restart_batch + 1) {
563✔
333
    first_inactive = simulation::restart_batch < settings::n_inactive;
122✔
334
    first_active = !first_inactive;
122✔
335
  }
336

337
  // Manage active/inactive timers and activate tallies if necessary.
338
  if (first_inactive) {
100,593✔
339
    simulation::time_inactive.start();
3,100✔
340
  } else if (first_active) {
97,493✔
341
    simulation::time_inactive.stop();
5,497✔
342
    simulation::time_active.start();
5,497✔
343
    for (auto& t : model::tallies) {
35,572✔
344
      t->active_ = true;
30,075✔
345
    }
346
  }
347

348
  // Add user tallies to active tallies list
349
  setup_active_tallies();
100,593✔
350
}
100,593✔
351

352
void finalize_batch()
100,593✔
353
{
354
  // Reduce tallies onto master process and accumulate
355
  simulation::time_tallies.start();
100,593✔
356
  accumulate_tallies();
100,593✔
357
  simulation::time_tallies.stop();
100,593✔
358

359
  // Reset global tally results
360
  if (simulation::current_batch <= settings::n_inactive) {
100,593✔
361
    xt::view(simulation::global_tallies, xt::all()) = 0.0;
19,784✔
362
    simulation::n_realizations = 0;
19,784✔
363
  }
364

365
  // Check_triggers
366
  if (mpi::master)
100,593✔
367
    check_triggers();
84,768✔
368
#ifdef OPENMC_MPI
369
  MPI_Bcast(&simulation::satisfy_triggers, 1, MPI_C_BOOL, 0, mpi::intracomm);
54,078✔
370
#endif
371
  if (simulation::satisfy_triggers ||
100,593✔
372
      (settings::trigger_on &&
3,132✔
373
        simulation::current_batch == settings::n_max_batches)) {
3,132✔
374
    settings::statepoint_batch.insert(simulation::current_batch);
151✔
375
  }
376

377
  // Write out state point if it's been specified for this batch and is not
378
  // a CMFD run instance
379
  if (contains(settings::statepoint_batch, simulation::current_batch) &&
106,472✔
380
      !settings::cmfd_run) {
5,879✔
381
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
9,711✔
382
        settings::source_write && !settings::source_separate) {
9,711✔
383
      bool b = (settings::run_mode == RunMode::EIGENVALUE);
4,652✔
384
      openmc_statepoint_write(nullptr, &b);
4,652✔
385
    } else {
386
      bool b = false;
331✔
387
      openmc_statepoint_write(nullptr, &b);
331✔
388
    }
389
  }
390

391
  if (settings::run_mode == RunMode::EIGENVALUE) {
100,593✔
392
    // Write out a separate source point if it's been specified for this batch
393
    if (contains(settings::sourcepoint_batch, simulation::current_batch) &&
95,237✔
394
        settings::source_write && settings::source_separate) {
95,237✔
395

396
      // Determine width for zero padding
397
      int w = std::to_string(settings::n_max_batches).size();
76✔
398
      std::string source_point_filename = fmt::format("{0}source.{1:0{2}}",
399
        settings::path_output, simulation::current_batch, w);
76✔
400
      gsl::span<SourceSite> bankspan(simulation::source_bank);
76✔
401
      if (settings::source_mcpl_write) {
76✔
402
        write_mcpl_source_point(
19✔
403
          source_point_filename.c_str(), bankspan, simulation::work_index);
404
      } else {
405
        write_source_point(
57✔
406
          source_point_filename.c_str(), bankspan, simulation::work_index);
407
      }
408
    }
76✔
409

410
    // Write a continously-overwritten source point if requested.
411
    if (settings::source_latest) {
90,895✔
412

413
      // note: correct file extension appended automatically
414
      auto filename = settings::path_output + "source";
190✔
415
      gsl::span<SourceSite> bankspan(simulation::source_bank);
190✔
416
      if (settings::source_mcpl_write) {
190✔
UNCOV
417
        write_mcpl_source_point(
×
418
          filename.c_str(), bankspan, simulation::work_index);
419
      } else {
420
        write_source_point(filename.c_str(), bankspan, simulation::work_index);
190✔
421
      }
422
    }
190✔
423
  }
424

425
  // Write out surface source if requested.
426
  if (settings::surf_source_write &&
100,593✔
427
      simulation::current_batch == settings::n_batches) {
190✔
428
    auto filename = settings::path_output + "surface_source";
19✔
429
    auto surf_work_index =
430
      mpi::calculate_parallel_index_vector(simulation::surf_source_bank.size());
19✔
431
    gsl::span<SourceSite> surfbankspan(simulation::surf_source_bank.begin(),
432
      simulation::surf_source_bank.size());
19✔
433
    if (settings::surf_mcpl_write) {
19✔
UNCOV
434
      write_mcpl_source_point(filename.c_str(), surfbankspan, surf_work_index);
×
435
    } else {
436
      write_source_point(filename.c_str(), surfbankspan, surf_work_index);
19✔
437
    }
438
  }
19✔
439
}
100,593✔
440

441
void initialize_generation()
100,859✔
442
{
443
  if (settings::run_mode == RunMode::EIGENVALUE) {
100,859✔
444
    // Clear out the fission bank
445
    simulation::fission_bank.resize(0);
91,161✔
446

447
    // Count source sites if using uniform fission source weighting
448
    if (settings::ufs_on)
91,161✔
449
      ufs_count_sites();
190✔
450

451
    // Store current value of tracklength k
452
    simulation::keff_generation = simulation::global_tallies(
91,161✔
453
      GlobalTally::K_TRACKLENGTH, TallyResult::VALUE);
454
  }
455
}
100,859✔
456

457
void finalize_generation()
100,859✔
458
{
459
  auto& gt = simulation::global_tallies;
100,859✔
460

461
  // Update global tallies with the accumulation variables
462
  if (settings::run_mode == RunMode::EIGENVALUE) {
100,859✔
463
    gt(GlobalTally::K_COLLISION, TallyResult::VALUE) += global_tally_collision;
91,161✔
464
    gt(GlobalTally::K_ABSORPTION, TallyResult::VALUE) +=
91,161✔
465
      global_tally_absorption;
466
    gt(GlobalTally::K_TRACKLENGTH, TallyResult::VALUE) +=
91,161✔
467
      global_tally_tracklength;
468
  }
469
  gt(GlobalTally::LEAKAGE, TallyResult::VALUE) += global_tally_leakage;
100,859✔
470

471
  // reset tallies
472
  if (settings::run_mode == RunMode::EIGENVALUE) {
100,859✔
473
    global_tally_collision = 0.0;
91,161✔
474
    global_tally_absorption = 0.0;
91,161✔
475
    global_tally_tracklength = 0.0;
91,161✔
476
  }
477
  global_tally_leakage = 0.0;
100,859✔
478

479
  if (settings::run_mode == RunMode::EIGENVALUE) {
100,859✔
480
    // If using shared memory, stable sort the fission bank (by parent IDs)
481
    // so as to allow for reproducibility regardless of which order particles
482
    // are run in.
483
    sort_fission_bank();
91,161✔
484

485
    // Distribute fission bank across processors evenly
486
    synchronize_bank();
91,161✔
487

488
    // Calculate shannon entropy
489
    if (settings::entropy_on)
91,161✔
490
      shannon_entropy();
21,750✔
491

492
    // Collect results and statistics
493
    calculate_generation_keff();
91,161✔
494
    calculate_average_keff();
91,161✔
495

496
    // Write generation output
497
    if (mpi::master && settings::verbosity >= 7) {
91,161✔
498
      print_generation();
72,054✔
499
    }
500
  }
501
}
100,859✔
502

503
void initialize_history(Particle& p, int64_t index_source)
192,110,520✔
504
{
505
  // set defaults
506
  if (settings::run_mode == RunMode::EIGENVALUE) {
192,110,520✔
507
    // set defaults for eigenvalue simulations from primary bank
508
    p.from_source(&simulation::source_bank[index_source - 1]);
179,444,200✔
509
  } else if (settings::run_mode == RunMode::FIXED_SOURCE) {
12,666,320✔
510
    // initialize random number seed
511
    int64_t id = (simulation::total_gen + overall_generation() - 1) *
12,666,320✔
512
                   settings::n_particles +
12,666,320✔
513
                 simulation::work_index[mpi::rank] + index_source;
12,666,320✔
514
    uint64_t seed = init_seed(id, STREAM_SOURCE);
12,666,320✔
515
    // sample from external source distribution or custom library then set
516
    auto site = sample_external_source(&seed);
12,666,320✔
517
    p.from_source(&site);
12,666,320✔
518
  }
519
  p.current_work() = index_source;
192,110,520✔
520

521
  // set identifier for particle
522
  p.id() = simulation::work_index[mpi::rank] + index_source;
192,110,520✔
523

524
  // set progeny count to zero
525
  p.n_progeny() = 0;
192,110,520✔
526

527
  // Reset particle event counter
528
  p.n_event() = 0;
192,110,520✔
529

530
  // Reset split counter
531
  p.n_split() = 0;
192,110,520✔
532

533
  // Reset weight window ratio
534
  p.ww_factor() = 0.0;
192,110,520✔
535

536
  // set random number seed
537
  int64_t particle_seed =
538
    (simulation::total_gen + overall_generation() - 1) * settings::n_particles +
192,110,520✔
539
    p.id();
192,110,520✔
540
  init_particle_seeds(particle_seed, p.seeds());
192,110,520✔
541

542
  // set particle trace
543
  p.trace() = false;
192,110,520✔
544
  if (simulation::current_batch == settings::trace_batch &&
384,235,040✔
545
      simulation::current_gen == settings::trace_gen &&
192,124,520✔
546
      p.id() == settings::trace_particle)
14,000✔
547
    p.trace() = true;
14✔
548

549
  // Set particle track.
550
  p.write_track() = check_track_criteria(p);
192,110,520✔
551

552
  // Display message if high verbosity or trace is on
553
  if (settings::verbosity >= 9 || p.trace()) {
192,110,520✔
554
    write_message("Simulating Particle {}", p.id());
14✔
555
  }
556

557
// Add paricle's starting weight to count for normalizing tallies later
558
#pragma omp atomic
85,071,345✔
559
  simulation::total_weight += p.wgt();
192,110,520✔
560

561
  // Force calculation of cross-sections by setting last energy to zero
562
  if (settings::run_CE) {
192,110,520✔
563
    p.invalidate_neutron_xs();
51,074,520✔
564
  }
565

566
  // Prepare to write out particle track.
567
  if (p.write_track())
192,110,520✔
568
    add_particle_track(p);
1,266✔
569
}
192,110,520✔
570

571
int overall_generation()
205,031,314✔
572
{
573
  using namespace simulation;
574
  return settings::gen_per_batch * (current_batch - 1) + current_gen;
205,031,314✔
575
}
576

577
void calculate_work()
5,509✔
578
{
579
  // Determine minimum amount of particles to simulate on each processor
580
  int64_t min_work = settings::n_particles / mpi::n_procs;
5,509✔
581

582
  // Determine number of processors that have one extra particle
583
  int64_t remainder = settings::n_particles % mpi::n_procs;
5,509✔
584

585
  int64_t i_bank = 0;
5,509✔
586
  simulation::work_index.resize(mpi::n_procs + 1);
5,509✔
587
  simulation::work_index[0] = 0;
5,509✔
588
  for (int i = 0; i < mpi::n_procs; ++i) {
12,475✔
589
    // Number of particles for rank i
590
    int64_t work_i = i < remainder ? min_work + 1 : min_work;
6,966✔
591

592
    // Set number of particles
593
    if (mpi::rank == i)
6,966✔
594
      simulation::work_per_rank = work_i;
5,509✔
595

596
    // Set index into source bank for rank i
597
    i_bank += work_i;
6,966✔
598
    simulation::work_index[i + 1] = i_bank;
6,966✔
599
  }
600
}
5,509✔
601

602
void initialize_data()
4,967✔
603
{
604
  // Determine minimum/maximum energy for incident neutron/photon data
605
  data::energy_max = {INFTY, INFTY};
4,967✔
606
  data::energy_min = {0.0, 0.0};
4,967✔
607
  for (const auto& nuc : data::nuclides) {
34,801✔
608
    if (nuc->grid_.size() >= 1) {
29,834✔
609
      int neutron = static_cast<int>(ParticleType::neutron);
29,834✔
610
      data::energy_min[neutron] =
59,668✔
611
        std::max(data::energy_min[neutron], nuc->grid_[0].energy.front());
29,834✔
612
      data::energy_max[neutron] =
29,834✔
613
        std::min(data::energy_max[neutron], nuc->grid_[0].energy.back());
29,834✔
614
    }
615
  }
616

617
  if (settings::photon_transport) {
4,967✔
618
    for (const auto& elem : data::elements) {
830✔
619
      if (elem->energy_.size() >= 1) {
575✔
620
        int photon = static_cast<int>(ParticleType::photon);
575✔
621
        int n = elem->energy_.size();
575✔
622
        data::energy_min[photon] =
1,150✔
623
          std::max(data::energy_min[photon], std::exp(elem->energy_(1)));
575✔
624
        data::energy_max[photon] =
1,150✔
625
          std::min(data::energy_max[photon], std::exp(elem->energy_(n - 1)));
575✔
626
      }
627
    }
628

629
    if (settings::electron_treatment == ElectronTreatment::TTB) {
255✔
630
      // Determine if minimum/maximum energy for bremsstrahlung is greater/less
631
      // than the current minimum/maximum
632
      if (data::ttb_e_grid.size() >= 1) {
255✔
633
        int photon = static_cast<int>(ParticleType::photon);
255✔
634
        int n_e = data::ttb_e_grid.size();
255✔
635
        data::energy_min[photon] =
510✔
636
          std::max(data::energy_min[photon], std::exp(data::ttb_e_grid(1)));
255✔
637
        data::energy_max[photon] = std::min(
255✔
638
          data::energy_max[photon], std::exp(data::ttb_e_grid(n_e - 1)));
510✔
639
      }
640
    }
641
  }
642

643
  // Show which nuclide results in lowest energy for neutron transport
644
  for (const auto& nuc : data::nuclides) {
5,866✔
645
    // If a nuclide is present in a material that's not used in the model, its
646
    // grid has not been allocated
647
    if (nuc->grid_.size() > 0) {
5,604✔
648
      double max_E = nuc->grid_[0].energy.back();
5,604✔
649
      int neutron = static_cast<int>(ParticleType::neutron);
5,604✔
650
      if (max_E == data::energy_max[neutron]) {
5,604✔
651
        write_message(7, "Maximum neutron transport energy: {} eV for {}",
4,705✔
652
          data::energy_max[neutron], nuc->name_);
4,705✔
653
        if (mpi::master && data::energy_max[neutron] < 20.0e6) {
4,705✔
UNCOV
654
          warning("Maximum neutron energy is below 20 MeV. This may bias "
×
655
                  "the results.");
656
        }
657
        break;
4,705✔
658
      }
659
    }
660
  }
661

662
  // Set up logarithmic grid for nuclides
663
  for (auto& nuc : data::nuclides) {
34,801✔
664
    nuc->init_grid();
29,834✔
665
  }
666
  int neutron = static_cast<int>(ParticleType::neutron);
4,967✔
667
  simulation::log_spacing =
4,967✔
668
    std::log(data::energy_max[neutron] / data::energy_min[neutron]) /
4,967✔
669
    settings::n_log_bins;
670
}
4,967✔
671

672
#ifdef OPENMC_MPI
673
void broadcast_results()
2,868✔
674
{
675
  // Broadcast tally results so that each process has access to results
676
  for (auto& t : model::tallies) {
19,637✔
677
    // Create a new datatype that consists of all values for a given filter
678
    // bin and then use that to broadcast. This is done to minimize the
679
    // chance of the 'count' argument of MPI_BCAST exceeding 2**31
680
    auto& results = t->results_;
16,769✔
681

682
    auto shape = results.shape();
16,769✔
683
    int count_per_filter = shape[1] * shape[2];
16,769✔
684
    MPI_Datatype result_block;
685
    MPI_Type_contiguous(count_per_filter, MPI_DOUBLE, &result_block);
16,769✔
686
    MPI_Type_commit(&result_block);
16,769✔
687
    MPI_Bcast(results.data(), shape[0], result_block, 0, mpi::intracomm);
16,769✔
688
    MPI_Type_free(&result_block);
16,769✔
689
  }
690

691
  // Also broadcast global tally results
692
  auto& gt = simulation::global_tallies;
2,868✔
693
  MPI_Bcast(gt.data(), gt.size(), MPI_DOUBLE, 0, mpi::intracomm);
2,868✔
694

695
  // These guys are needed so that non-master processes can calculate the
696
  // combined estimate of k-effective
697
  double temp[] {
698
    simulation::k_col_abs, simulation::k_col_tra, simulation::k_abs_tra};
2,868✔
699
  MPI_Bcast(temp, 3, MPI_DOUBLE, 0, mpi::intracomm);
2,868✔
700
  simulation::k_col_abs = temp[0];
2,868✔
701
  simulation::k_col_tra = temp[1];
2,868✔
702
  simulation::k_abs_tra = temp[2];
2,868✔
703
}
2,868✔
704

705
#endif
706

707
void free_memory_simulation()
5,951✔
708
{
709
  simulation::k_generation.clear();
5,951✔
710
  simulation::entropy.clear();
5,951✔
711
}
5,951✔
712

713
void transport_history_based_single_particle(Particle& p)
4,818,513,707✔
714
{
715
  while (true) {
716
    p.event_calculate_xs();
4,818,513,707✔
717
    if (!p.alive())
4,818,513,707✔
UNCOV
718
      break;
×
719
    p.event_advance();
4,818,513,707✔
720
    if (p.collision_distance() > p.boundary().distance) {
4,818,513,707✔
721
      p.event_cross_surface();
1,786,562,451✔
722
    } else {
723
      p.event_collide();
3,031,951,256✔
724
    }
725
    p.event_revive_from_secondary();
4,818,513,707✔
726
    if (!p.alive())
4,818,513,707✔
727
      break;
181,171,148✔
728
  }
729
  p.event_death();
181,171,148✔
730
}
181,171,148✔
731

732
void transport_history_based()
97,916✔
733
{
734
#pragma omp parallel for schedule(runtime)
735
  for (int64_t i_work = 1; i_work <= simulation::work_per_rank; ++i_work) {
107,094,519✔
736
    Particle p;
107,043,040✔
737
    initialize_history(p, i_work);
107,043,040✔
738
    transport_history_based_single_particle(p);
107,043,040✔
739
  }
107,043,040✔
740
}
97,916✔
741

742
void transport_event_based()
2,943✔
743
{
744
  int64_t remaining_work = simulation::work_per_rank;
2,943✔
745
  int64_t source_offset = 0;
2,943✔
746

747
  // To cap the total amount of memory used to store particle object data, the
748
  // number of particles in flight at any point in time can bet set. In the case
749
  // that the maximum in flight particle count is lower than the total number
750
  // of particles that need to be run this iteration, the event-based transport
751
  // loop is executed multiple times until all particles have been completed.
752
  while (remaining_work > 0) {
5,886✔
753
    // Figure out # of particles to run for this subiteration
754
    int64_t n_particles =
755
      std::min(remaining_work, settings::max_particles_in_flight);
2,943✔
756

757
    // Initialize all particle histories for this subiteration
758
    process_init_events(n_particles, source_offset);
2,943✔
759

760
    // Event-based transport loop
761
    while (true) {
762
      // Determine which event kernel has the longest queue
763
      int64_t max = std::max({simulation::calculate_fuel_xs_queue.size(),
4,668,384✔
764
        simulation::calculate_nonfuel_xs_queue.size(),
2,334,192✔
765
        simulation::advance_particle_queue.size(),
2,334,192✔
766
        simulation::surface_crossing_queue.size(),
2,334,192✔
767
        simulation::collision_queue.size()});
2,334,192✔
768

769
      // Execute event with the longest queue
770
      if (max == 0) {
2,334,192✔
771
        break;
2,943✔
772
      } else if (max == simulation::calculate_fuel_xs_queue.size()) {
2,331,249✔
773
        process_calculate_xs_events(simulation::calculate_fuel_xs_queue);
416,571✔
774
      } else if (max == simulation::calculate_nonfuel_xs_queue.size()) {
1,914,678✔
775
        process_calculate_xs_events(simulation::calculate_nonfuel_xs_queue);
357,538✔
776
      } else if (max == simulation::advance_particle_queue.size()) {
1,557,140✔
777
        process_advance_particle_events();
769,630✔
778
      } else if (max == simulation::surface_crossing_queue.size()) {
787,510✔
779
        process_surface_crossing_events();
247,229✔
780
      } else if (max == simulation::collision_queue.size()) {
540,281✔
781
        process_collision_events();
540,281✔
782
      }
783
    }
2,331,249✔
784

785
    // Execute death event for all particles
786
    process_death_events(n_particles);
2,943✔
787

788
    // Adjust remaining work and source offset variables
789
    remaining_work -= n_particles;
2,943✔
790
    source_offset += n_particles;
2,943✔
791
  }
792
}
2,943✔
793

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