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

openmc-dev / openmc / 23562589134

25 Mar 2026 08:30PM UTC coverage: 80.33% (-1.0%) from 81.326%
23562589134

Pull #3702

github

web-flow
Merge b34a5c94e into 6cd39073b
Pull Request #3702: Random Ray Kinetic Simulation Mode

17619 of 25259 branches covered (69.75%)

Branch coverage included in aggregate %.

942 of 1975 new or added lines in 21 files covered. (47.7%)

374 existing lines in 28 files now uncovered.

57996 of 68871 relevant lines covered (84.21%)

48728124.42 hits per line

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

82.86
/src/random_ray/random_ray_simulation.cpp
1
#include "openmc/random_ray/random_ray_simulation.h"
2

3
#include "openmc/capi.h"
4
#include "openmc/eigenvalue.h"
5
#include "openmc/geometry.h"
6
#include "openmc/message_passing.h"
7
#include "openmc/mgxs_interface.h"
8
#include "openmc/output.h"
9
#include "openmc/plot.h"
10
#include "openmc/random_ray/flat_source_domain.h"
11
#include "openmc/random_ray/random_ray.h"
12
#include "openmc/simulation.h"
13
#include "openmc/source.h"
14
#include "openmc/tallies/filter.h"
15
#include "openmc/tallies/tally.h"
16
#include "openmc/tallies/tally_scoring.h"
17
#include "openmc/timer.h"
18
#include "openmc/weight_windows.h"
19

20
namespace openmc {
21

22
//==============================================================================
23
// Non-member functions
24
//==============================================================================
25

26
// Enforces restrictions on inputs in random ray mode.  While there are
27
// many features that don't make sense in random ray mode, and are therefore
28
// unsupported, we limit our testing/enforcement operations only to inputs
29
// that may cause erroneous/misleading output or crashes from the solver.
30
void validate_random_ray_inputs()
567✔
31
{
32
  // Validate tallies
33
  ///////////////////////////////////////////////////////////////////
34
  for (auto& tally : model::tallies) {
1,449✔
35

36
    // Validate score types
37
    for (auto score_bin : tally->scores_) {
2,034✔
38
      switch (score_bin) {
1,152!
39
      case SCORE_FLUX:
40
      case SCORE_TOTAL:
41
      case SCORE_FISSION:
42
      case SCORE_NU_FISSION:
43
      case SCORE_EVENTS:
44
      case SCORE_KAPPA_FISSION:
45
        break;
46

47
      // TODO: add support for prompt and delayed fission
48
      case SCORE_PRECURSORS: {
36✔
49
        if (settings::kinetic_simulation && settings::create_delayed_neutrons) {
36!
50
          break;
51
        } else {
NEW
52
          fatal_error("Invalid score specified in tallies.xml. Precursors can "
×
53
                      "only be scored for kinetic simulations.");
54
        }
55
      }
56
      default:
×
57
        fatal_error(
×
58
          "Invalid score specified. Only flux, total, fission, nu-fission, "
59
          "kappa-fission and event scores are supported in random ray mode. "
60
          "(precursors are supported for kinetic simulations when delayed "
61
          "neutrons are turned on).");
62
      }
63
    }
64

65
    // Validate filter types
66
    for (auto f : tally->filters()) {
1,917✔
67
      auto& filter = *model::tally_filters[f];
1,035✔
68

69
      switch (filter.type()) {
1,035!
70
      case FilterType::CELL:
71
      case FilterType::CELL_INSTANCE:
72
      case FilterType::DISTRIBCELL:
73
      case FilterType::ENERGY:
74
      case FilterType::MATERIAL:
75
      case FilterType::MESH:
76
      case FilterType::UNIVERSE:
77
      case FilterType::PARTICLE:
78
        break;
79
      case FilterType::DELAYED_GROUP:
36✔
80
        if (settings::kinetic_simulation) {
36!
81
          break;
82
        } else {
NEW
83
          fatal_error("Invalid filter specified in tallies.xml. Kinetic "
×
84
                      "simulations is required "
85
                      "to tally with a delayed_group filter.");
86
        }
87
      default:
×
88
        fatal_error("Invalid filter specified. Only cell, cell_instance, "
×
89
                    "distribcell, energy, material, mesh, and universe filters "
90
                    "are supported in random ray mode (delayed_group is "
91
                    "supported for kinetic simulations).");
92
      }
93
    }
94
  }
95

96
  // TODO: validate kinetic data is present
97
  //  Validate MGXS data
98
  ///////////////////////////////////////////////////////////////////
99
  for (auto& material : data::mg.macro_xs_) {
2,115✔
100
    if (!material.is_isotropic) {
1,548!
101
      fatal_error("Anisotropic MGXS detected. Only isotropic XS data sets "
×
102
                  "supported in random ray mode.");
103
    }
104
    for (int g = 0; g < data::mg.num_energy_groups_; g++) {
11,574✔
105
      if (material.exists_in_model) {
10,026✔
106
        // Temperature and angle indices, if using multiple temperature
107
        // data sets and/or anisotropic data sets.
108
        // TODO: Currently assumes we are only using single temp/single angle
109
        // data.
110
        const int t = 0;
9,990✔
111
        const int a = 0;
9,990✔
112
        double sigma_t =
9,990✔
113
          material.get_xs(MgxsType::TOTAL, g, NULL, NULL, NULL, t, a);
9,990✔
114
        if (sigma_t <= 0.0) {
9,990!
115
          fatal_error("No zero or negative total macroscopic cross sections "
×
116
                      "allowed in random ray mode. If the intention is to make "
117
                      "a void material, use a cell fill of 'None' instead.");
118
        }
119
      }
120
    }
121
  }
122

123
  // Validate ray source
124
  ///////////////////////////////////////////////////////////////////
125

126
  // Check for independent source
127
  IndependentSource* is =
567!
128
    dynamic_cast<IndependentSource*>(RandomRay::ray_source_.get());
567!
129
  if (!is) {
567!
130
    fatal_error("Invalid ray source definition. Ray source must provided and "
×
131
                "be of type IndependentSource.");
132
  }
133

134
  // Check for box source
135
  SpatialDistribution* space_dist = is->space();
567!
136
  SpatialBox* sb = dynamic_cast<SpatialBox*>(space_dist);
567!
137
  if (!sb) {
567!
138
    fatal_error(
×
139
      "Invalid ray source definition -- only box sources are allowed.");
140
  }
141

142
  // Check that box source is not restricted to fissionable areas
143
  if (sb->only_fissionable()) {
567!
144
    fatal_error(
×
145
      "Invalid ray source definition -- fissionable spatial distribution "
146
      "not allowed.");
147
  }
148

149
  // Check for isotropic source
150
  UnitSphereDistribution* angle_dist = is->angle();
567!
151
  Isotropic* id = dynamic_cast<Isotropic*>(angle_dist);
567!
152
  if (!id) {
567!
153
    fatal_error("Invalid ray source definition -- only isotropic sources are "
×
154
                "allowed.");
155
  }
156

157
  // Validate external sources
158
  ///////////////////////////////////////////////////////////////////
159
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
567✔
160
    if (model::external_sources.size() < 1) {
243!
161
      fatal_error("Must provide a particle source (in addition to ray source) "
×
162
                  "in fixed source random ray mode.");
163
    }
164

165
    for (int i = 0; i < model::external_sources.size(); i++) {
486✔
166
      Source* s = model::external_sources[i].get();
243!
167

168
      // Check for independent source
169
      IndependentSource* is = dynamic_cast<IndependentSource*>(s);
243!
170

171
      if (!is) {
243!
172
        fatal_error(
×
173
          "Only IndependentSource external source types are allowed in "
174
          "random ray mode");
175
      }
176

177
      // Check for isotropic source
178
      UnitSphereDistribution* angle_dist = is->angle();
243!
179
      Isotropic* id = dynamic_cast<Isotropic*>(angle_dist);
243!
180
      if (!id) {
243!
181
        fatal_error(
×
182
          "Invalid source definition -- only isotropic external sources are "
183
          "allowed in random ray mode.");
184
      }
185

186
      // Validate that a domain ID was specified OR that it is a point source
187
      auto sp = dynamic_cast<SpatialPoint*>(is->space());
243!
188
      if (is->domain_ids().size() == 0 && !sp) {
243!
189
        fatal_error("Fixed sources must be point source or spatially "
×
190
                    "constrained by domain id (cell, material, or universe) in "
191
                    "random ray mode.");
192
      } else if (is->domain_ids().size() > 0 && sp) {
243✔
193
        // If both a domain constraint and a non-default point source location
194
        // are specified, notify user that domain constraint takes precedence.
195
        if (sp->r().x == 0.0 && sp->r().y == 0.0 && sp->r().z == 0.0) {
207!
196
          warning("Fixed source has both a domain constraint and a point "
414✔
197
                  "type spatial distribution. The domain constraint takes "
198
                  "precedence in random ray mode -- point source coordinate "
199
                  "will be ignored.");
200
        }
201
      }
202

203
      // Check that a discrete energy distribution was used
204
      Distribution* d = is->energy();
243!
205
      Discrete* dd = dynamic_cast<Discrete*>(d);
243!
206
      if (!dd) {
243!
207
        fatal_error(
×
208
          "Only discrete (multigroup) energy distributions are allowed for "
209
          "external sources in random ray mode.");
210
      }
211
    }
212
  }
213

214
  // Validate plotting files
215
  ///////////////////////////////////////////////////////////////////
216
  for (int p = 0; p < model::plots.size(); p++) {
567!
217

218
    // Get handle to OpenMC plot object
219
    const auto& openmc_plottable = model::plots[p];
×
220
    Plot* openmc_plot = dynamic_cast<Plot*>(openmc_plottable.get());
×
221

222
    // Random ray plots only support voxel plots
223
    if (!openmc_plot) {
×
224
      warning(fmt::format(
×
225
        "Plot {} will not be used for end of simulation data plotting -- only "
226
        "voxel plotting is allowed in random ray mode.",
227
        openmc_plottable->id()));
×
228
      continue;
×
229
    } else if (openmc_plot->type_ != Plot::PlotType::voxel) {
×
230
      warning(fmt::format(
×
231
        "Plot {} will not be used for end of simulation data plotting -- only "
232
        "voxel plotting is allowed in random ray mode.",
233
        openmc_plottable->id()));
×
234
      continue;
×
235
    }
236
  }
237

238
  // Warn about slow MPI domain replication, if detected
239
  ///////////////////////////////////////////////////////////////////
240
#ifdef OPENMC_MPI
241
  if (mpi::n_procs > 1) {
189✔
242
    warning(
372✔
243
      "MPI parallelism is not supported by the random ray solver. All work "
244
      "will be performed by rank 0. Domain decomposition may be implemented in "
245
      "the future to provide efficient MPI scaling.");
246
  }
247
#endif
248

249
  // Warn about instability resulting from linear sources in small regions
250
  // when generating weight windows with FW-CADIS and an overlaid mesh.
251
  ///////////////////////////////////////////////////////////////////
252
  if (RandomRay::source_shape_ == RandomRaySourceShape::LINEAR &&
567✔
253
      variance_reduction::weight_windows.size() > 0) {
198✔
254
    warning(
18✔
255
      "Linear sources may result in negative fluxes in small source regions "
256
      "generated by mesh subdivision. Negative sources may result in low "
257
      "quality FW-CADIS weight windows. We recommend you use flat source mode "
258
      "when generating weight windows with an overlaid mesh tally.");
259
  }
260
}
567✔
261

NEW
262
void openmc_reset_random_ray()
×
263
{
NEW
264
  FlatSourceDomain::volume_estimator_ = RandomRayVolumeEstimator::HYBRID;
×
NEW
265
  FlatSourceDomain::volume_normalized_flux_tallies_ = false;
×
NEW
266
  FlatSourceDomain::adjoint_ = false;
×
NEW
267
  FlatSourceDomain::mesh_domain_map_.clear();
×
NEW
268
  RandomRay::ray_source_.reset();
×
NEW
269
  RandomRay::source_shape_ = RandomRaySourceShape::FLAT;
×
NEW
270
  RandomRay::sample_method_ = RandomRaySampleMethod::PRNG;
×
NEW
271
  RandomRay::bd_order_ = 3;
×
NEW
272
  RandomRay::time_method_ = RandomRayTimeMethod::ISOTROPIC;
×
NEW
273
}
×
274

275
void write_random_ray_hdf5(hid_t group)
1,260✔
276
{
277
  hid_t random_ray_group = create_group(group, "random_ray");
1,260✔
278
  switch (RandomRay::source_shape_) {
1,260!
279
  case RandomRaySourceShape::FLAT:
1,026✔
280
    write_dataset(random_ray_group, "source_shape", "flat");
1,026✔
281
    break;
1,026✔
282
  case RandomRaySourceShape::LINEAR:
207✔
283
    write_dataset(random_ray_group, "source_shape", "linear");
207✔
284
    break;
207✔
285
  case RandomRaySourceShape::LINEAR_XY:
27✔
286
    write_dataset(random_ray_group, "source_shape", "linear xy");
27✔
287
    break;
27✔
288
  default:
289
    break;
290
  }
291

292
  switch (FlatSourceDomain::volume_estimator_) {
1,260!
293
  case RandomRayVolumeEstimator::SIMULATION_AVERAGED:
18✔
294
    write_dataset(random_ray_group, "volume_estimator", "simulation averaged");
18✔
295
    break;
18✔
296
  case RandomRayVolumeEstimator::NAIVE:
54✔
297
    write_dataset(random_ray_group, "volume_estimator", "naive");
54✔
298
    break;
54✔
299
  case RandomRayVolumeEstimator::HYBRID:
1,188✔
300
    write_dataset(random_ray_group, "volume_estimator", "hybrid");
1,188✔
301
    break;
1,188✔
302
  default:
303
    break;
304
  }
305

306
  write_dataset(
1,260✔
307
    random_ray_group, "distance_active", RandomRay::distance_active_);
308
  write_dataset(
1,260✔
309
    random_ray_group, "distance_inactive", RandomRay::distance_inactive_);
310
  write_dataset(random_ray_group, "volume_normalized_flux_tallies",
1,260✔
311
    FlatSourceDomain::volume_normalized_flux_tallies_);
312
  write_dataset(random_ray_group, "adjoint_mode", FlatSourceDomain::adjoint_);
1,260✔
313

314
  write_dataset(random_ray_group, "avg_miss_rate", RandomRay::avg_miss_rate_);
1,260✔
315
  write_dataset(
1,260✔
316
    random_ray_group, "n_source_regions", RandomRay::n_source_regions_);
317
  write_dataset(random_ray_group, "n_external_source_regions",
1,260✔
318
    RandomRay::n_external_source_regions_);
319
  write_dataset(random_ray_group, "n_geometric_intersections",
1,260✔
320
    RandomRay::total_geometric_intersections_);
321
  int64_t n_integrations =
1,260✔
322
    RandomRay::total_geometric_intersections_ * data::mg.num_energy_groups_;
1,260✔
323
  write_dataset(random_ray_group, "n_integrations", n_integrations);
1,260✔
324

325
  if (settings::kinetic_simulation && !simulation::is_initial_condition) {
1,260✔
326
    write_dataset(random_ray_group, "bd_order", RandomRay::bd_order_);
540✔
327
    switch (RandomRay::time_method_) {
540!
328
    case RandomRayTimeMethod::ISOTROPIC:
360✔
329
      write_dataset(random_ray_group, "time_method", "isotropic");
360✔
330
      break;
360✔
331
    case RandomRayTimeMethod::PROPAGATION:
180✔
332
      write_dataset(random_ray_group, "time_method", "propogation");
180✔
333
      break;
180✔
334
    default:
335
      break;
336
    }
337
  }
338
  close_group(random_ray_group);
1,260✔
339
}
1,260✔
340

341
void print_adjoint_header()
90✔
342
{
343
  if (!FlatSourceDomain::adjoint_)
90✔
344
    // If we're going to do an adjoint simulation afterwards, report that this
345
    // is the initial forward flux solve.
346
    header("FORWARD FLUX SOLVE", 3);
45✔
347
  else
348
    // Otherwise report that we are doing the adjoint simulation
349
    header("ADJOINT FLUX SOLVE", 3);
45✔
350
}
90✔
351

352
//-----------------------------------------------------------------------------
353
// Non-member functions for kinetic simulations
354

355
void rename_time_step_file(
1,728✔
356
  std::string base_filename, std::string extension, int i)
357
{
358
  // Rename file
359
  std::string old_filename_ =
1,728✔
360
    fmt::format("{0}{1}{2}", settings::path_output, base_filename, extension);
1,728✔
361
  std::string new_filename_ = fmt::format(
1,728✔
362
    "{0}{1}_{2}{3}", settings::path_output, base_filename, i, extension);
1,728✔
363

364
  const char* old_fname = old_filename_.c_str();
1,728✔
365
  const char* new_fname = new_filename_.c_str();
1,728✔
366
  std::rename(old_fname, new_fname);
1,728✔
367
}
1,728✔
368

369
//==============================================================================
370
// RandomRaySimulation implementation
371
//==============================================================================
372

373
RandomRaySimulation::RandomRaySimulation()
753✔
374
  : negroups_(data::mg.num_energy_groups_),
753!
375
    ndgroups_(data::mg.num_delayed_groups_)
753!
376
{
377
  // There are no source sites in random ray mode, so be sure to disable to
378
  // ensure we don't attempt to write source sites to statepoint
379
  settings::source_write = false;
753✔
380

381
  // Random ray mode does not have an inner loop over generations within a
382
  // batch, so set the current gen to 1
383
  simulation::current_gen = 1;
753✔
384

385
  switch (RandomRay::source_shape_) {
753!
386
  case RandomRaySourceShape::FLAT:
453✔
387
    domain_ = make_unique<FlatSourceDomain>();
453✔
388
    break;
453✔
389
  case RandomRaySourceShape::LINEAR:
300✔
390
  case RandomRaySourceShape::LINEAR_XY:
300✔
391
    domain_ = make_unique<LinearSourceDomain>();
300✔
392
    break;
300✔
393
  default:
×
394
    fatal_error("Unknown random ray source shape");
×
395
  }
396

397
  // Convert OpenMC native MGXS into a more efficient format
398
  // internal to the random ray solver
399
  domain_->flatten_xs();
753✔
400

401
  // Check if adjoint calculation is needed. If it is, we will run the forward
402
  // calculation first and then the adjoint calculation later.
403
  adjoint_needed_ = FlatSourceDomain::adjoint_;
753✔
404

405
  // Adjoint is always false for the forward calculation
406
  FlatSourceDomain::adjoint_ = false;
753✔
407

408
  // The first simulation is run after initialization
409
  is_first_simulation_ = true;
753✔
410

411
  // Initialize vectors used for baking in the initial condition during time
412
  // stepping
413
  if (settings::kinetic_simulation) {
753✔
414
    // Initialize vars used for time-consistent seed approach
415
    static_avg_k_eff_;
416
    static_k_eff_;
417
    static_fission_rate_;
418
  }
419
}
753✔
420

421
void RandomRaySimulation::apply_fixed_sources_and_mesh_domains()
753✔
422
{
423
  domain_->apply_meshes();
753✔
424
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
753✔
425
    // Transfer external source user inputs onto random ray source regions
426
    domain_->convert_external_sources();
324✔
427
    domain_->count_external_source_regions();
324✔
428
  }
429
}
753✔
430

431
void RandomRaySimulation::prepare_fixed_sources_adjoint()
60✔
432
{
433
  domain_->source_regions_.adjoint_reset();
60✔
434
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
60✔
435
    domain_->set_adjoint_sources();
48✔
436
  }
437
}
60✔
438

439
void RandomRaySimulation::prepare_adjoint_simulation()
60✔
440
{
441
  // Reset all simulation values
442
  domain_->source_regions_.simulation_reset();
60✔
443

444
  // Configure the domain for adjoint simulation
445
  FlatSourceDomain::adjoint_ = true;
60✔
446

447
  // Reset k-eff
448
  domain_->k_eff_ = 1.0;
60✔
449

450
  // Initialize adjoint fixed sources, if present
451
  prepare_fixed_sources_adjoint();
60✔
452

453
  // Transpose scattering matrix
454
  domain_->transpose_scattering_matrix();
60✔
455

456
  // Swap nu_sigma_f and chi
457
  domain_->nu_sigma_f_.swap(domain_->chi_);
60✔
458
}
60✔
459

460
void RandomRaySimulation::simulate()
1,677✔
461
{
462
  if (!is_first_simulation_) {
1,677✔
463
    if (mpi::master && adjoint_needed_)
924✔
464
      openmc::print_adjoint_header();
45✔
465

466
    // Reset the timers and reinitialize the general OpenMC datastructures if
467
    // this is after the first simulation
468
    reset_timers();
924✔
469

470
    // Initialize OpenMC general data structures
471
    openmc_simulation_init();
924✔
472
  }
473

474
  // Begin main simulation timer
475
  simulation::time_total.start();
1,677✔
476

477
  // Random ray power iteration loop
478
  while (simulation::current_batch < settings::n_batches) {
162,594✔
479
    // Initialize the current batch
480
    initialize_batch();
159,240✔
481
    initialize_generation();
159,240✔
482

483
    // MPI not supported in random ray solver, so all work is done by rank 0
484
    // TODO: Implement domain decomposition for MPI parallelism
485
    if (mpi::master) {
159,240✔
486

487
      // Reset total starting particle weight used for normalizing tallies
488
      simulation::total_weight = 1.0;
119,880✔
489

490
      // Update source term (scattering + fission (+ delayed if kinetic))
491
      domain_->update_all_neutron_sources();
119,880✔
492

493
      // Reset scalar fluxes, iteration volume tallies, and region hit flags
494
      // to zero
495
      domain_->batch_reset();
119,880✔
496

497
      // At the beginning of the simulation, if mesh subdivision is in use, we
498
      // need to swap the main source region container into the base container,
499
      // as the main source region container will be used to hold the true
500
      // subdivided source regions. The base container will therefore only
501
      // contain the external source region information, the mesh indices,
502
      // material properties, and initial guess values for the flux/source.
503

504
      // Start timer for transport
505
      simulation::time_transport.start();
119,880✔
506

507
// Transport sweep over all random rays for the iteration
508
#pragma omp parallel for schedule(dynamic)                                     \
53,280✔
509
  reduction(+ : total_geometric_intersections_)
53,280✔
510
      for (int i = 0; i < settings::n_particles; i++) {
6,994,600✔
511
        RandomRay ray(i, domain_.get());
6,928,000✔
512
        total_geometric_intersections_ +=
13,856,000✔
513
          ray.transport_history_based_single_ray();
6,928,000✔
514
      }
6,928,000✔
515

516
      simulation::time_transport.stop();
119,880✔
517

518
      // Add any newly discovered source regions to the main source region
519
      // container.
520
      domain_->finalize_discovered_source_regions();
119,880✔
521

522
      // Normalize scalar flux and update volumes
523
      domain_->normalize_scalar_flux_and_volumes(
119,880✔
524
        settings::n_particles * RandomRay::distance_active_);
525

526
      // Add source to scalar flux, compute number of FSR hits
527
      int64_t n_hits = domain_->add_source_to_scalar_flux();
119,880✔
528

529
      // Apply transport stabilization factors
530
      domain_->apply_transport_stabilization();
119,880✔
531

532
      if (settings::run_mode == RunMode::EIGENVALUE) {
119,880✔
533
        // Compute random ray k-eff for initial condition.
534
        // This keff will be preserved
535
        if (simulation::is_initial_condition) {
111,690✔
536
          domain_->compute_k_eff();
35,190✔
537
          if (settings::kinetic_simulation && simulation::k_eff_correction) {
35,190✔
538
            static_fission_rate_.push_back(domain_->fission_rate_);
15,300✔
539
            static_k_eff_.push_back(domain_->k_eff_);
15,300✔
540
          }
541
        } else {
542
          domain_->k_eff_ = static_k_eff_[simulation::current_batch - 1];
76,500✔
543
          domain_->fission_rate_ =
76,500✔
544
            static_fission_rate_[simulation::current_batch - 1];
76,500✔
545
        }
546

547
        // Store random ray k-eff into OpenMC's native k-eff variable
548
        global_tally_tracklength = domain_->k_eff_;
111,690✔
549
      }
550

551
      // Compute precursors if delayed neutrons are turned on
552
      if (settings::kinetic_simulation && settings::create_delayed_neutrons)
119,880!
553
        domain_->compute_all_precursors();
107,100✔
554

555
      // Execute all tallying tasks, if this is an active batch
556
      if (simulation::current_batch > settings::n_inactive) {
119,880✔
557

558
        // Add this iteration's scalar flux estimate to final accumulated
559
        // estimate
560
        domain_->accumulate_iteration_quantities();
58,905✔
561

562
        // Use above mapping to contribute FSR flux data to appropriate
563
        // tallies
564
        domain_->random_ray_tally();
58,905✔
565
      }
566

567
      // Set phi_old = phi_new
568
      domain_->flux_swap();
119,880✔
569
      if (settings::kinetic_simulation && settings::create_delayed_neutrons) {
119,880!
570
        domain_->precursors_swap();
107,100✔
571
      }
572

573
      // Check for any obvious insabilities/nans/infs
574
      instability_check(n_hits, domain_->k_eff_, avg_miss_rate_);
119,880✔
575
    } // End MPI master work
576

577
    // Store simulation metrics
578
    RandomRay::avg_miss_rate_ = avg_miss_rate_ / settings::n_batches;
159,240✔
579
    RandomRay::total_geometric_intersections_ = total_geometric_intersections_;
159,240✔
580
    RandomRay::n_external_source_regions_ = domain_->n_external_source_regions_;
159,240✔
581
    RandomRay::n_source_regions_ = domain_->n_source_regions();
159,240✔
582

583
    // Finalize the current batch
584
    finalize_generation();
159,240✔
585
    finalize_batch();
159,240✔
586
  } // End random ray power iteration loop
587

588
  domain_->count_external_source_regions();
1,677✔
589

590
  // End main simulation timer
591
  simulation::time_total.stop();
1,677✔
592

593
  // Normalize and save the final forward quantities
594
  domain_->normalize_final_quantities();
1,677✔
595

596
  // Finalize OpenMC
597
  openmc_simulation_finalize();
1,677✔
598

599
  // Output all simulation results
600
  output_simulation_results();
1,677✔
601

602
  // Toggle that the simulation object has been initialized after the first
603
  // simulation
604
  if (is_first_simulation_)
1,677✔
605
    is_first_simulation_ = false;
753✔
606
}
1,677✔
607

608
void RandomRaySimulation::initialize_time_step(int i)
864✔
609
{
610
  if (simulation::k_eff_correction)
864✔
611
    static_avg_k_eff_ = simulation::keff;
144✔
612
  domain_->k_eff_ = static_avg_k_eff_;
864✔
613

614
  // Increment current timestep and simuation time
615
  simulation::current_timestep = i + 1;
864✔
616

617
  // Propagate previous converted solution for kinetic simulation
618
  domain_->source_regions_.simulation_reset();
864✔
619
  domain_->propagate_final_quantities();
864✔
620
  domain_->source_regions_.time_step_reset();
864✔
621

622
  if (!simulation::is_initial_condition) {
864✔
623
    // Compute RHS backward differences
624
    domain_->compute_rhs_bd_quantities();
720✔
625

626
    // Update time dependent cross section based on the density
627
    domain_->update_material_density(i);
720✔
628

629
    simulation::current_time += settings::dt;
720✔
630
  }
631
}
864✔
632

633
void RandomRaySimulation::finalize_time_step()
864✔
634
{
635
  if (simulation::is_initial_condition) {
864✔
636
    // Initialize the BD arrays if initial condition
637
    domain_->store_time_step_quantities(false);
144✔
638
    // Toggle off initial condition and source correction
639
    simulation::is_initial_condition = false;
144✔
640
    simulation::k_eff_correction = false;
144✔
641
  } else {
642
    // Else, store final quantities for the current time step
643
    domain_->store_time_step_quantities();
720✔
644
  }
645

646
  // Rename statepoint and tallies file for the current time step
647
  rename_time_step_file(fmt::format("statepoint.{0}", settings::n_batches),
1,728✔
648
    ".h5", simulation::current_timestep);
649
  if (settings::output_tallies)
864!
650
    rename_time_step_file("tallies", ".out", simulation::current_timestep);
1,728✔
651
}
864✔
652

653
void RandomRaySimulation::output_simulation_results() const
1,677✔
654
{
655
  // Print random ray results
656
  if (mpi::master) {
1,677✔
657
    print_results_random_ray();
1,260✔
658
    if (model::plots.size() > 0) {
1,260!
659
      domain_->output_to_vtk();
×
660
    }
661
  }
662
}
1,677✔
663

664
// Apply a few sanity checks to catch obvious cases of numerical instability.
665
// Instability typically only occurs if ray density is extremely low.
666
void RandomRaySimulation::instability_check(
119,880✔
667
  int64_t n_hits, double k_eff, double& avg_miss_rate) const
668
{
669
  double percent_missed = ((domain_->n_source_regions() - n_hits) /
119,880!
670
                            static_cast<double>(domain_->n_source_regions())) *
119,880✔
671
                          100.0;
119,880✔
672
  avg_miss_rate += percent_missed;
119,880✔
673

674
  if (mpi::master) {
119,880!
675
    if (percent_missed > 10.0) {
119,880✔
676
      warning(fmt::format(
1,566✔
677
        "Very high FSR miss rate detected ({:.3f}%). Instability may occur. "
678
        "Increase ray density by adding more rays and/or active distance.",
679
        percent_missed));
680
    } else if (percent_missed > 1.0) {
119,097!
UNCOV
681
      warning(
×
UNCOV
682
        fmt::format("Elevated FSR miss rate detected ({:.3f}%). Increasing "
×
683
                    "ray density by adding more rays and/or active "
684
                    "distance may improve simulation efficiency.",
685
          percent_missed));
686
    }
687

688
    if (k_eff > 10.0 || k_eff < 0.01 || !(std::isfinite(k_eff))) {
119,880!
689
      fatal_error(fmt::format("Instability detected: k-eff = {:.5f}", k_eff));
×
690
    }
691
  }
692
}
119,880✔
693

694
// Print random ray simulation results
695
void RandomRaySimulation::print_results_random_ray() const
1,260✔
696
{
697
  using namespace simulation;
1,260✔
698

699
  if (settings::verbosity >= 6) {
1,260!
700
    double total_integrations =
1,260✔
701
      RandomRay::total_geometric_intersections_ * negroups_;
1,260✔
702
    double time_per_integration =
1,260✔
703
      simulation::time_transport.elapsed() / total_integrations;
1,260✔
704
    double misc_time = time_total.elapsed() - time_update_src.elapsed() -
1,260✔
705
                       time_transport.elapsed() - time_tallies.elapsed() -
1,260✔
706
                       time_bank_sendrecv.elapsed();
1,260✔
707

708
    if (settings::kinetic_simulation && !simulation::is_initial_condition) {
1,260✔
709
      misc_time -= time_update_bd_vectors.elapsed();
540✔
710
    }
711
    header("Simulation Statistics", 4);
1,260✔
712
    fmt::print(
1,260✔
713
      " Total Iterations                  = {}\n", settings::n_batches);
714
    fmt::print(
1,260✔
715
      " Number of Rays per Iteration      = {}\n", settings::n_particles);
716
    fmt::print(" Inactive Distance                 = {} cm\n",
1,260✔
717
      RandomRay::distance_inactive_);
718
    fmt::print(" Active Distance                   = {} cm\n",
1,260✔
719
      RandomRay::distance_active_);
720
    fmt::print(" Source Regions (SRs)              = {}\n",
1,260✔
721
      RandomRay::n_source_regions_);
722
    fmt::print(" SRs Containing External Sources   = {}\n",
1,260✔
723
      RandomRay::n_external_source_regions_);
724
    fmt::print(" Total Geometric Intersections     = {:.4e}\n",
2,520✔
725
      static_cast<double>(RandomRay::total_geometric_intersections_));
1,260✔
726
    fmt::print("   Avg per Iteration               = {:.4e}\n",
2,520✔
727
      static_cast<double>(RandomRay::total_geometric_intersections_) /
1,260✔
728
        settings::n_batches);
729
    fmt::print("   Avg per Iteration per SR        = {:.2f}\n",
2,520✔
730
      static_cast<double>(RandomRay::total_geometric_intersections_) /
1,260✔
731
        static_cast<double>(settings::n_batches) /
1,260✔
732
        RandomRay::n_source_regions_);
733
    fmt::print(" Avg SR Miss Rate per Iteration    = {:.4f}%\n",
1,260✔
734
      RandomRay::avg_miss_rate_);
735
    fmt::print(" Energy Groups                     = {}\n", negroups_);
1,260✔
736
    if (settings::kinetic_simulation)
1,260✔
737
      fmt::print(" Delay Groups                      = {}\n", ndgroups_);
756✔
738
    fmt::print(
1,260✔
739
      " Total Integrations                = {:.4e}\n", total_integrations);
740
    fmt::print("   Avg per Iteration               = {:.4e}\n",
2,520✔
741
      total_integrations / settings::n_batches);
1,260✔
742

743
    std::string estimator;
1,260!
744
    switch (domain_->volume_estimator_) {
1,260!
745
    case RandomRayVolumeEstimator::SIMULATION_AVERAGED:
18✔
746
      estimator = "Simulation Averaged";
18✔
747
      break;
748
    case RandomRayVolumeEstimator::NAIVE:
54✔
749
      estimator = "Naive";
54✔
750
      break;
751
    case RandomRayVolumeEstimator::HYBRID:
1,188✔
752
      estimator = "Hybrid";
1,188✔
753
      break;
754
    default:
×
755
      fatal_error("Invalid volume estimator type");
×
756
    }
757
    fmt::print(" Volume Estimator Type             = {}\n", estimator);
1,260✔
758

759
    std::string adjoint_true = (FlatSourceDomain::adjoint_) ? "ON" : "OFF";
3,735✔
760
    fmt::print(" Adjoint Flux Mode                 = {}\n", adjoint_true);
1,260✔
761

762
    std::string shape;
2,520!
763
    switch (RandomRay::source_shape_) {
1,260!
764
    case RandomRaySourceShape::FLAT:
1,026✔
765
      shape = "Flat";
1,026✔
766
      break;
767
    case RandomRaySourceShape::LINEAR:
207✔
768
      shape = "Linear";
207✔
769
      break;
770
    case RandomRaySourceShape::LINEAR_XY:
27✔
771
      shape = "Linear XY";
27✔
772
      break;
773
    default:
×
774
      fatal_error("Invalid random ray source shape");
×
775
    }
776
    fmt::print(" Source Shape                      = {}\n", shape);
1,260✔
777
    std::string sample_method;
2,520!
778
    switch (RandomRay::sample_method_) {
1,260!
779
    case RandomRaySampleMethod::PRNG:
1,242✔
780
      sample_method = "PRNG";
1,242✔
781
      break;
782
    case RandomRaySampleMethod::HALTON:
9✔
783
      sample_method = "Halton";
9✔
784
      break;
785
    case RandomRaySampleMethod::S2:
9✔
786
      sample_method = "PRNG S2";
9✔
787
      break;
788
    }
789
    fmt::print(" Sample Method                     = {}\n", sample_method);
1,260✔
790

791
    if (domain_->is_transport_stabilization_needed_) {
1,260✔
792
      fmt::print(" Transport XS Stabilization Used   = YES (rho = {:.3f})\n",
135✔
793
        FlatSourceDomain::diagonal_stabilization_rho_);
794
    } else {
795
      fmt::print(" Transport XS Stabilization Used   = NO\n");
1,125✔
796
    }
797
    if (settings::kinetic_simulation && !simulation::is_initial_condition) {
1,260✔
798
      std::string time_method =
540✔
799
        (RandomRay::time_method_ == RandomRayTimeMethod::ISOTROPIC)
540✔
800
          ? "ISOTROPIC"
801
          : "PROPAGATION";
720✔
802
      fmt::print(" Time Method                       = {}\n", time_method);
540✔
803
      fmt::print(
540✔
804
        " Backwards Difference Order        = {}\n", RandomRay::bd_order_);
805
    }
540✔
806

807
    header("Timing Statistics", 4);
1,260✔
808
    show_time("Total time for initialization", time_initialize.elapsed());
1,260✔
809
    show_time("Reading cross sections", time_read_xs.elapsed(), 1);
1,260✔
810
    show_time("Total simulation time", time_total.elapsed());
1,260✔
811
    show_time("Transport sweep only", time_transport.elapsed(), 1);
1,260✔
812
    show_time("Source update only", time_update_src.elapsed(), 1);
1,260✔
813
    if (settings::kinetic_simulation && settings::create_delayed_neutrons) {
1,260!
814
      show_time(
756✔
815
        "Precursor computation only", time_compute_precursors.elapsed(), 1);
816
      misc_time -= time_compute_precursors.elapsed();
756✔
817
    }
818
    show_time("Tally conversion only", time_tallies.elapsed(), 1);
1,260✔
819
    show_time("MPI source reductions only", time_bank_sendrecv.elapsed(), 1);
1,260✔
820
    show_time("Other iteration routines", misc_time, 1);
1,260✔
821
    if (settings::run_mode == RunMode::EIGENVALUE) {
1,260✔
822
      show_time("Time in inactive batches", time_inactive.elapsed());
981✔
823
    }
824
    show_time("Time in active batches", time_active.elapsed());
1,260✔
825
    show_time("Time writing statepoints", time_statepoint.elapsed());
1,260✔
826
    show_time("Total time for finalization", time_finalize.elapsed());
1,260✔
827
    show_time("Time per integration", time_per_integration);
1,260✔
828
  }
1,260✔
829

830
  if (settings::verbosity >= 4 && settings::run_mode == RunMode::EIGENVALUE) {
1,260!
831
    header("Results", 4);
981✔
832
    fmt::print(" k-effective                       = {:.5f} +/- {:.5f}\n",
981✔
833
      simulation::keff, simulation::keff_std);
834
  }
835
}
1,260✔
836

837
void openmc_finalize_random_ray()
6,915✔
838
{
839
  FlatSourceDomain::volume_estimator_ = RandomRayVolumeEstimator::HYBRID;
6,915✔
840
  FlatSourceDomain::volume_normalized_flux_tallies_ = false;
6,915✔
841
  FlatSourceDomain::adjoint_ = false;
6,915✔
842
  FlatSourceDomain::mesh_domain_map_.clear();
6,915✔
843
  RandomRay::ray_source_.reset();
6,915✔
844
  RandomRay::source_shape_ = RandomRaySourceShape::FLAT;
6,915✔
845
  RandomRay::sample_method_ = RandomRaySampleMethod::PRNG;
6,915✔
846
}
6,915✔
847

848
} // namespace openmc
849

850
//==============================================================================
851
// C API functions
852
//==============================================================================
853

854
void openmc_run_random_ray()
753✔
855
{
856
  //////////////////////////////////////////////////////////
857
  // Run forward simulation
858
  //////////////////////////////////////////////////////////
859
  if (openmc::mpi::master) {
753✔
860
    if (openmc::FlatSourceDomain::adjoint_) {
567✔
861
      openmc::FlatSourceDomain::adjoint_ = false;
45✔
862
      openmc::print_adjoint_header();
45✔
863
      openmc::FlatSourceDomain::adjoint_ = true;
45✔
864
    }
865
  }
866

867
  // Initialize OpenMC general data structures
868
  openmc_simulation_init();
753✔
869

870
  // Validate that inputs meet requirements for random ray mode
871
  if (openmc::mpi::master)
753✔
872
    openmc::validate_random_ray_inputs();
567✔
873

874
  // Initialize Random Ray Simulation Object
875
  openmc::RandomRaySimulation sim;
753✔
876

877
  // Initialize fixed sources, if present
878
  sim.apply_fixed_sources_and_mesh_domains();
753✔
879

880
  // Simulate single random ray simulation (static case)
881
  // OR get an initial estimate for scattering and fission
882
  // distributions (if fissile material exist),
883
  // and k-eff (if kinetic eigenvalue simulation)
884
  sim.simulate();
753✔
885

886
  if (openmc::settings::kinetic_simulation) {
753✔
887
    // Toggle initial condition source correction
888
    openmc::simulation::k_eff_correction = true;
144✔
889
    int i_start = -1;
144✔
890
    // Timestepping loop,
891
    for (int i = i_start; i < openmc::settings::n_timesteps; i++) {
1,008✔
892
      sim.initialize_time_step(i);
864✔
893
      sim.simulate();
864✔
894
      sim.finalize_time_step();
864✔
895
    }
896
  }
897

898
  //////////////////////////////////////////////////////////
899
  // Run adjoint simulation (if enabled)
900
  //////////////////////////////////////////////////////////
901

902
  if (sim.adjoint_needed_) {
753✔
903
    // Setup for adjoint simulation
904
    sim.prepare_adjoint_simulation();
60✔
905

906
    // Run adjoint simulation
907
    sim.simulate();
60✔
908
  }
909
}
753✔
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