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

openmc-dev / openmc / 27780945043

18 Jun 2026 06:30PM UTC coverage: 81.097% (-0.2%) from 81.34%
27780945043

Pull #3911

github

web-flow
Merge c5ade9293 into 09ee8308d
Pull Request #3911: Creating a new HDF5 nuclear data library for UQ

18119 of 26286 branches covered (68.93%)

Branch coverage included in aggregate %.

310 of 599 new or added lines in 3 files covered. (51.75%)

2247 existing lines in 53 files now uncovered.

59544 of 69480 relevant lines covered (85.7%)

40971728.6 hits per line

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

80.98
/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()
425✔
31
{
32
  // Validate tallies
33
  ///////////////////////////////////////////////////////////////////
34
  for (auto& tally : model::tallies) {
1,210✔
35

36
    // Validate score types
37
    for (auto score_bin : tally->scores_) {
1,746✔
38
      switch (score_bin) {
961!
39
      case SCORE_FLUX:
961✔
40
      case SCORE_TOTAL:
961✔
41
      case SCORE_FISSION:
961✔
42
      case SCORE_NU_FISSION:
961✔
43
      case SCORE_EVENTS:
961✔
44
      case SCORE_KAPPA_FISSION:
961✔
45
        break;
961✔
46
      default:
×
47
        fatal_error(
×
48
          "Invalid score specified. Only flux, total, fission, nu-fission, "
49
          "kappa-fission, and event scores are supported in random ray mode.");
50
      }
51
    }
52

53
    // Validate filter types
54
    for (auto f : tally->filters()) {
1,708✔
55
      auto& filter = *model::tally_filters[f];
923✔
56

57
      switch (filter.type()) {
923!
58
      case FilterType::CELL:
923✔
59
      case FilterType::CELL_INSTANCE:
923✔
60
      case FilterType::DISTRIBCELL:
923✔
61
      case FilterType::ENERGY:
923✔
62
      case FilterType::MATERIAL:
923✔
63
      case FilterType::MESH:
923✔
64
      case FilterType::UNIVERSE:
923✔
65
      case FilterType::PARTICLE:
923✔
66
        break;
923✔
67
      default:
×
68
        fatal_error("Invalid filter specified. Only cell, cell_instance, "
×
69
                    "distribcell, energy, material, mesh, and universe filters "
70
                    "are supported in random ray mode.");
71
      }
72
    }
73
  }
74

75
  // Validate MGXS data
76
  ///////////////////////////////////////////////////////////////////
77
  for (auto& material : data::mg.macro_xs_) {
1,578✔
78
    if (!material.is_isotropic) {
1,153!
79
      fatal_error("Anisotropic MGXS detected. Only isotropic XS data sets "
×
80
                  "supported in random ray mode.");
81
    }
82
    for (int g = 0; g < data::mg.num_energy_groups_; g++) {
5,907✔
83
      if (material.exists_in_model) {
4,754✔
84
        // Temperature and angle indices, if using multiple temperature
85
        // data sets and/or anisotropic data sets.
86
        // TODO: Currently assumes we are only using single temp/single angle
87
        // data.
88
        const int t = 0;
4,722✔
89
        const int a = 0;
4,722✔
90
        double sigma_t =
4,722✔
91
          material.get_xs(MgxsType::TOTAL, g, NULL, NULL, NULL, t, a);
4,722✔
92
        if (sigma_t <= 0.0) {
4,722!
93
          fatal_error("No zero or negative total macroscopic cross sections "
×
94
                      "allowed in random ray mode. If the intention is to make "
95
                      "a void material, use a cell fill of 'None' instead.");
96
        }
97
      }
98
    }
99
  }
100

101
  // Validate ray source
102
  ///////////////////////////////////////////////////////////////////
103

104
  // Check for independent source
105
  IndependentSource* is =
425!
106
    dynamic_cast<IndependentSource*>(RandomRay::ray_source_.get());
425!
107
  if (!is) {
425!
108
    fatal_error("Invalid ray source definition. Ray source must provided and "
×
109
                "be of type IndependentSource.");
110
  }
111

112
  // Check for box source
113
  SpatialDistribution* space_dist = is->space();
425!
114
  SpatialBox* sb = dynamic_cast<SpatialBox*>(space_dist);
425!
115
  if (!sb) {
425!
116
    fatal_error(
×
117
      "Invalid ray source definition -- only box sources are allowed.");
118
  }
119

120
  // Check that box source is not restricted to fissionable areas
121
  if (sb->only_fissionable()) {
425!
122
    fatal_error(
×
123
      "Invalid ray source definition -- fissionable spatial distribution "
124
      "not allowed.");
125
  }
126

127
  // Check for isotropic source
128
  UnitSphereDistribution* angle_dist = is->angle();
425!
129
  Isotropic* id = dynamic_cast<Isotropic*>(angle_dist);
425!
130
  if (!id) {
425!
131
    fatal_error("Invalid ray source definition -- only isotropic sources are "
×
132
                "allowed.");
133
  }
134

135
  // Validate external sources
136
  ///////////////////////////////////////////////////////////////////
137
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
425✔
138
    if (model::external_sources.size() < 1) {
233!
139
      fatal_error("Must provide a particle source (in addition to ray source) "
×
140
                  "in fixed source random ray mode.");
141
    }
142

143
    for (int i = 0; i < model::external_sources.size(); i++) {
466✔
144
      Source* s = model::external_sources[i].get();
233!
145

146
      // Check for independent source
147
      IndependentSource* is = dynamic_cast<IndependentSource*>(s);
233!
148

149
      if (!is) {
233!
150
        fatal_error(
×
151
          "Only IndependentSource external source types are allowed in "
152
          "random ray mode");
153
      }
154

155
      // Check for isotropic source
156
      UnitSphereDistribution* angle_dist = is->angle();
233!
157
      Isotropic* id = dynamic_cast<Isotropic*>(angle_dist);
233!
158
      if (!id) {
233!
159
        fatal_error(
×
160
          "Invalid source definition -- only isotropic external sources are "
161
          "allowed in random ray mode.");
162
      }
163

164
      // Validate that a domain ID was specified OR that it is a point source
165
      auto sp = dynamic_cast<SpatialPoint*>(is->space());
233!
166
      if (is->domain_ids().size() == 0 && !sp) {
233!
167
        fatal_error("Fixed sources must be point source or spatially "
×
168
                    "constrained by domain id (cell, material, or universe) in "
169
                    "random ray mode.");
170
      } else if (is->domain_ids().size() > 0 && sp) {
233✔
171
        // If both a domain constraint and a point source location are
172
        // specified, notify user that domain constraint takes precedence.
173
        warning("Fixed source has both a domain constraint and a point "
400✔
174
                "type spatial distribution. The domain constraint takes "
175
                "precedence in random ray mode -- point source coordinate "
176
                "will be ignored.");
177
      }
178

179
      // Check that a discrete energy distribution was used
180
      Distribution* d = is->energy();
233!
181
      Discrete* dd = dynamic_cast<Discrete*>(d);
233!
182
      if (!dd) {
233!
UNCOV
183
        fatal_error(
×
184
          "Only discrete (multigroup) energy distributions are allowed for "
185
          "external sources in random ray mode.");
186
      }
187
    }
188
  }
189

190
  // Validate adjoint sources
191
  ///////////////////////////////////////////////////////////////////
192
  if (FlatSourceDomain::adjoint_ && !model::adjoint_sources.empty()) {
425!
193
    for (int i = 0; i < model::adjoint_sources.size(); i++) {
16✔
194
      Source* s = model::adjoint_sources[i].get();
8!
195

196
      // Check for independent source
197
      IndependentSource* is = dynamic_cast<IndependentSource*>(s);
8!
198

199
      if (!is) {
8!
UNCOV
200
        fatal_error(
×
201
          "Only IndependentSource adjoint source types are allowed in "
202
          "random ray mode");
203
      }
204

205
      // Check for isotropic source
206
      UnitSphereDistribution* angle_dist = is->angle();
8!
207
      Isotropic* id = dynamic_cast<Isotropic*>(angle_dist);
8!
208
      if (!id) {
8!
UNCOV
209
        fatal_error(
×
210
          "Invalid source definition -- only isotropic adjoint sources are "
211
          "allowed in random ray mode.");
212
      }
213

214
      // Validate that a domain ID was specified OR that it is a point source
215
      auto sp = dynamic_cast<SpatialPoint*>(is->space());
8!
216
      if (is->domain_ids().size() == 0 && !sp) {
8!
UNCOV
217
        fatal_error("Adjoint sources must be point source or spatially "
×
218
                    "constrained by domain id (cell, material, or universe) in "
219
                    "random ray mode.");
220
      } else if (is->domain_ids().size() > 0 && sp) {
8!
221
        // If both a domain constraint and a point source location are
222
        // specified, notify user that domain constraint takes precedence.
223
        warning("Adjoint source has both a domain constraint and a point "
16✔
224
                "type spatial distribution. The domain constraint takes "
225
                "precedence in random ray mode -- point source coordinate "
226
                "will be ignored.");
227
      }
228

229
      // Check that a discrete energy distribution was used
230
      Distribution* d = is->energy();
8!
231
      Discrete* dd = dynamic_cast<Discrete*>(d);
8!
232
      if (!dd) {
8!
UNCOV
233
        fatal_error(
×
234
          "Only discrete (multigroup) energy distributions are allowed for "
235
          "adjoint sources in random ray mode.");
236
      }
237
    }
238
  }
239

240
  // Validate plotting files
241
  ///////////////////////////////////////////////////////////////////
242
  for (int p = 0; p < model::plots.size(); p++) {
425!
243

244
    // Get handle to OpenMC plot object
UNCOV
245
    const auto& openmc_plottable = model::plots[p];
×
UNCOV
246
    Plot* openmc_plot = dynamic_cast<Plot*>(openmc_plottable.get());
×
247

248
    // Random ray plots only support voxel plots
UNCOV
249
    if (!openmc_plot) {
×
UNCOV
250
      warning(fmt::format(
×
251
        "Plot {} will not be used for end of simulation data plotting -- only "
252
        "voxel plotting is allowed in random ray mode.",
UNCOV
253
        openmc_plottable->id()));
×
UNCOV
254
      continue;
×
UNCOV
255
    } else if (openmc_plot->type_ != Plot::PlotType::voxel) {
×
UNCOV
256
      warning(fmt::format(
×
257
        "Plot {} will not be used for end of simulation data plotting -- only "
258
        "voxel plotting is allowed in random ray mode.",
UNCOV
259
        openmc_plottable->id()));
×
UNCOV
260
      continue;
×
261
    }
262
  }
263

264
  // Warn about slow MPI domain replication, if detected
265
  ///////////////////////////////////////////////////////////////////
266
#ifdef OPENMC_MPI
267
  if (mpi::n_procs > 1) {
107✔
268
    warning(
208✔
269
      "MPI parallelism is not supported by the random ray solver. All work "
270
      "will be performed by rank 0. Domain decomposition may be implemented in "
271
      "the future to provide efficient MPI scaling.");
272
  }
273
#endif
274

275
  // Warn about instability resulting from linear sources in small regions
276
  // when generating weight windows with FW-CADIS and an overlaid mesh.
277
  ///////////////////////////////////////////////////////////////////
278
  if (RandomRay::source_shape_ == RandomRaySourceShape::LINEAR &&
425✔
279
      variance_reduction::weight_windows.size() > 0) {
176✔
280
    warning(
16✔
281
      "Linear sources may result in negative fluxes in small source regions "
282
      "generated by mesh subdivision. Negative sources may result in low "
283
      "quality FW-CADIS weight windows. We recommend you use flat source "
284
      "mode when generating weight windows with an overlaid mesh tally.");
285
  }
286
}
425✔
287

288
void openmc_finalize_random_ray()
6,224✔
289
{
290
  FlatSourceDomain::volume_estimator_ = RandomRayVolumeEstimator::HYBRID;
6,224✔
291
  FlatSourceDomain::volume_normalized_flux_tallies_ = false;
6,224✔
292
  FlatSourceDomain::adjoint_ = false;
6,224✔
293
  FlatSourceDomain::fw_cadis_local_ = false;
6,224✔
294
  FlatSourceDomain::fw_cadis_local_targets_.clear();
6,224✔
295
  FlatSourceDomain::mesh_domain_map_.clear();
6,224✔
296
  RandomRay::ray_source_.reset();
6,224✔
297
  RandomRay::source_shape_ = RandomRaySourceShape::FLAT;
6,224✔
298
  RandomRay::sample_method_ = RandomRaySampleMethod::PRNG;
6,224✔
299
}
6,224✔
300

301
//==============================================================================
302
// RandomRaySimulation implementation
303
//==============================================================================
304

305
RandomRaySimulation::RandomRaySimulation()
529✔
306
  : negroups_(data::mg.num_energy_groups_)
529!
307
{
308
  // There are no source sites in random ray mode, so be sure to disable to
309
  // ensure we don't attempt to write source sites to statepoint
310
  settings::source_write = false;
529✔
311

312
  // Random ray mode does not have an inner loop over generations within a
313
  // batch, so set the current gen to 1
314
  simulation::current_gen = 1;
529✔
315

316
  switch (RandomRay::source_shape_) {
529!
317
  case RandomRaySourceShape::FLAT:
279✔
318
    domain_ = make_unique<FlatSourceDomain>();
279✔
319
    break;
279✔
320
  case RandomRaySourceShape::LINEAR:
250✔
321
  case RandomRaySourceShape::LINEAR_XY:
250✔
322
    domain_ = make_unique<LinearSourceDomain>();
250✔
323
    break;
250✔
UNCOV
324
  default:
×
UNCOV
325
    fatal_error("Unknown random ray source shape");
×
326
  }
327

328
  // Convert OpenMC native MGXS into a more efficient format
329
  // internal to the random ray solver
330
  domain_->flatten_xs();
529✔
331
}
529✔
332

333
void RandomRaySimulation::apply_fixed_sources_and_mesh_domains()
519✔
334
{
335
  domain_->apply_meshes();
519✔
336
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
519✔
337
    // Transfer external source user inputs onto random ray source regions
338
    domain_->convert_external_sources(false);
281✔
339
    domain_->count_external_source_regions();
281✔
340
  }
341
}
519✔
342

343
void RandomRaySimulation::prepare_fw_fixed_sources_adjoint()
61✔
344
{
345
  // Prepare adjoint fixed sources using forward flux
346
  domain_->source_regions_.adjoint_reset();
61✔
347
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
61✔
348
    domain_->set_fw_adjoint_sources();
51✔
349
  }
350
}
61✔
351

352
void RandomRaySimulation::prepare_local_fixed_sources_adjoint()
10✔
353
{
354
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
10!
355
    domain_->set_local_adjoint_sources();
10✔
356
  }
357
}
10✔
358

359
void RandomRaySimulation::prepare_adjoint_simulation(bool fw_adjoint)
71✔
360
{
361
  reset_timers();
71✔
362

363
  if (mpi::master)
71✔
364
    header("ADJOINT FLUX SOLVE", 3);
57✔
365

366
  if (fw_adjoint) {
71✔
367
    // Forward simulation has already been run;
368
    // Configure the domain for adjoint simulation and
369
    // re-initialize OpenMC general data structures
370
    FlatSourceDomain::adjoint_ = true;
61✔
371

372
    openmc_simulation_init();
61✔
373

374
    prepare_fw_fixed_sources_adjoint();
61✔
375
  } else {
376
    // Initialize adjoint fixed sources
377
    domain_->apply_meshes();
10✔
378
    prepare_local_fixed_sources_adjoint();
10✔
379
    domain_->count_external_source_regions();
10✔
380
  }
381

382
  domain_->k_eff_ = 1.0;
71✔
383

384
  // Transpose scattering matrix
385
  domain_->transpose_scattering_matrix();
71✔
386

387
  // Swap nu_sigma_f and chi
388
  domain_->nu_sigma_f_.swap(domain_->chi_);
71✔
389
}
71✔
390

391
void RandomRaySimulation::simulate()
590✔
392
{
393
  // Begin main simulation timer
394
  simulation::time_total.start();
590✔
395

396
  // Random ray power iteration loop
397
  while (simulation::current_batch < settings::n_batches) {
15,292✔
398
    // Initialize the current batch
399
    initialize_batch();
14,112✔
400
    initialize_generation();
14,112✔
401

402
    // MPI not supported in random ray solver, so all work is done by rank 0
403
    // TODO: Implement domain decomposition for MPI parallelism
404
    if (mpi::master) {
14,112✔
405

406
      // Reset total starting particle weight used for normalizing tallies
407
      simulation::total_weight = 1.0;
11,612✔
408

409
      // Update source term (scattering + fission)
410
      domain_->update_all_neutron_sources();
11,612✔
411

412
      // Reset scalar fluxes, iteration volume tallies, and region hit flags
413
      // to zero
414
      domain_->batch_reset();
11,612✔
415

416
      // At the beginning of the simulation, if mesh subdivision is in use, we
417
      // need to swap the main source region container into the base container,
418
      // as the main source region container will be used to hold the true
419
      // subdivided source regions. The base container will therefore only
420
      // contain the external source region information, the mesh indices,
421
      // material properties, and initial guess values for the flux/source.
422

423
      // Start timer for transport
424
      simulation::time_transport.start();
11,612✔
425

426
// Transport sweep over all random rays for the iteration
427
#pragma omp parallel for schedule(dynamic)                                     \
7,262✔
428
  reduction(+ : total_geometric_intersections_)
7,262✔
429
      for (int i = 0; i < settings::n_particles; i++) {
636,150✔
430
        RandomRay ray(i, domain_.get());
631,800✔
431
        total_geometric_intersections_ +=
1,263,600✔
432
          ray.transport_history_based_single_ray();
631,800✔
433
      }
631,800✔
434

435
      simulation::time_transport.stop();
11,612✔
436

437
      // Add any newly discovered source regions to the main source region
438
      // container.
439
      domain_->finalize_discovered_source_regions();
11,612✔
440

441
      // Normalize scalar flux and update volumes
442
      domain_->normalize_scalar_flux_and_volumes(
11,612✔
443
        settings::n_particles * RandomRay::distance_active_);
444

445
      // Add source to scalar flux, compute number of FSR hits
446
      int64_t n_hits = domain_->add_source_to_scalar_flux();
11,612✔
447

448
      // Apply transport stabilization factors
449
      domain_->apply_transport_stabilization();
11,612✔
450

451
      if (settings::run_mode == RunMode::EIGENVALUE) {
11,612✔
452
        // Compute random ray k-eff
453
        domain_->compute_k_eff();
4,080✔
454

455
        // Store random ray k-eff into OpenMC's native k-eff variable
456
        global_tally_tracklength = domain_->k_eff_;
4,080✔
457
      }
458

459
      // Execute all tallying tasks, if this is an active batch
460
      if (simulation::current_batch > settings::n_inactive) {
11,612✔
461

462
        // Add this iteration's scalar flux estimate to final accumulated
463
        // estimate
464
        domain_->accumulate_iteration_flux();
5,446✔
465

466
        // Use above mapping to contribute FSR flux data to appropriate
467
        // tallies
468
        domain_->random_ray_tally();
5,446✔
469
      }
470

471
      // Set phi_old = phi_new
472
      domain_->flux_swap();
11,612✔
473

474
      // Check for any obvious insabilities/nans/infs
475
      instability_check(n_hits, domain_->k_eff_, avg_miss_rate_);
11,612✔
476
    } // End MPI master work
477

478
    // Finalize the current batch
479
    finalize_generation();
14,112✔
480
    finalize_batch();
14,112✔
481
  } // End random ray power iteration loop
482

483
  domain_->count_external_source_regions();
590✔
484

485
  // End main simulation timer
486
  simulation::time_total.stop();
590✔
487

488
  // Normalize and save the final forward flux
489
  double source_normalization_factor =
590✔
490
    domain_->compute_fixed_source_normalization_factor() /
590✔
491
    (settings::n_batches - settings::n_inactive);
590✔
492

493
#pragma omp parallel for
413✔
494
  for (uint64_t se = 0; se < domain_->n_source_elements(); se++) {
836,895✔
495
    domain_->source_regions_.scalar_flux_final(se) *=
836,718✔
496
      source_normalization_factor;
497
  }
498

499
  // Finalize OpenMC
500
  openmc_simulation_finalize();
590✔
501

502
  // Output all simulation results
503
  output_simulation_results();
590✔
504
}
590✔
505

506
void RandomRaySimulation::output_simulation_results() const
590✔
507
{
508
  // Print random ray results
509
  if (mpi::master) {
590✔
510
    print_results_random_ray(total_geometric_intersections_,
474✔
511
      avg_miss_rate_ / settings::n_batches, negroups_,
474✔
512
      domain_->n_source_regions(), domain_->n_external_source_regions_);
474✔
513
    if (model::plots.size() > 0) {
474!
UNCOV
514
      domain_->output_to_vtk();
×
515
    }
516
  }
517
}
590✔
518

519
// Apply a few sanity checks to catch obvious cases of numerical instability.
520
// Instability typically only occurs if ray density is extremely low.
521
void RandomRaySimulation::instability_check(
11,612✔
522
  int64_t n_hits, double k_eff, double& avg_miss_rate) const
523
{
524
  double percent_missed = ((domain_->n_source_regions() - n_hits) /
11,612!
525
                            static_cast<double>(domain_->n_source_regions())) *
11,612✔
526
                          100.0;
11,612✔
527
  avg_miss_rate += percent_missed;
11,612✔
528

529
  if (mpi::master) {
11,612!
530
    if (percent_missed > 10.0) {
11,612✔
531
      warning(fmt::format(
1,392✔
532
        "Very high FSR miss rate detected ({:.3f}%). Instability may occur. "
533
        "Increase ray density by adding more rays and/or active distance.",
534
        percent_missed));
535
    } else if (percent_missed > 1.0) {
10,916✔
536
      warning(
2!
537
        fmt::format("Elevated FSR miss rate detected ({:.3f}%). Increasing "
4!
538
                    "ray density by adding more rays and/or active "
539
                    "distance may improve simulation efficiency.",
540
          percent_missed));
541
    }
542

543
    if (k_eff > 10.0 || k_eff < 0.01 || !(std::isfinite(k_eff))) {
11,612!
UNCOV
544
      fatal_error(fmt::format("Instability detected: k-eff = {:.5f}", k_eff));
×
545
    }
546
  }
547
}
11,612✔
548

549
// Print random ray simulation results
550
void RandomRaySimulation::print_results_random_ray(
474✔
551
  uint64_t total_geometric_intersections, double avg_miss_rate, int negroups,
552
  int64_t n_source_regions, int64_t n_external_source_regions) const
553
{
554
  using namespace simulation;
474✔
555

556
  if (settings::verbosity >= 6) {
474!
557
    double total_integrations = total_geometric_intersections * negroups;
474✔
558
    double time_per_integration =
474✔
559
      simulation::time_transport.elapsed() / total_integrations;
474✔
560
    double misc_time = time_total.elapsed() - time_update_src.elapsed() -
474✔
561
                       time_transport.elapsed() - time_tallies.elapsed() -
474✔
562
                       time_bank_sendrecv.elapsed();
474✔
563

564
    header("Simulation Statistics", 4);
474✔
565
    fmt::print(
474✔
566
      " Total Iterations                  = {}\n", settings::n_batches);
567
    fmt::print(
474✔
568
      " Number of Rays per Iteration      = {}\n", settings::n_particles);
569
    fmt::print(" Inactive Distance                 = {} cm\n",
474✔
570
      RandomRay::distance_inactive_);
571
    fmt::print(" Active Distance                   = {} cm\n",
474✔
572
      RandomRay::distance_active_);
573
    fmt::print(" Source Regions (SRs)              = {}\n", n_source_regions);
474✔
574
    fmt::print(
474✔
575
      " SRs Containing External Sources   = {}\n", n_external_source_regions);
576
    fmt::print(" Total Geometric Intersections     = {:.4e}\n",
948✔
577
      static_cast<double>(total_geometric_intersections));
474✔
578
    fmt::print("   Avg per Iteration               = {:.4e}\n",
948✔
579
      static_cast<double>(total_geometric_intersections) / settings::n_batches);
474✔
580
    fmt::print("   Avg per Iteration per SR        = {:.2f}\n",
948✔
581
      static_cast<double>(total_geometric_intersections) /
474✔
582
        static_cast<double>(settings::n_batches) / n_source_regions);
474✔
583
    fmt::print(" Avg SR Miss Rate per Iteration    = {:.4f}%\n", avg_miss_rate);
474✔
584
    fmt::print(" Energy Groups                     = {}\n", negroups);
474✔
585
    fmt::print(
474✔
586
      " Total Integrations                = {:.4e}\n", total_integrations);
587
    fmt::print("   Avg per Iteration               = {:.4e}\n",
948✔
588
      total_integrations / settings::n_batches);
474✔
589

590
    std::string estimator;
474!
591
    switch (domain_->volume_estimator_) {
474!
592
    case RandomRayVolumeEstimator::SIMULATION_AVERAGED:
16✔
593
      estimator = "Simulation Averaged";
16✔
594
      break;
595
    case RandomRayVolumeEstimator::NAIVE:
74✔
596
      estimator = "Naive";
74✔
597
      break;
598
    case RandomRayVolumeEstimator::HYBRID:
384✔
599
      estimator = "Hybrid";
384✔
600
      break;
UNCOV
601
    default:
×
UNCOV
602
      fatal_error("Invalid volume estimator type");
×
603
    }
604
    fmt::print(" Volume Estimator Type             = {}\n", estimator);
474✔
605

606
    std::string adjoint_true = (FlatSourceDomain::adjoint_) ? "ON" : "OFF";
1,365✔
607
    fmt::print(" Adjoint Flux Mode                 = {}\n", adjoint_true);
474✔
608

609
    std::string shape;
948!
610
    switch (RandomRay::source_shape_) {
474!
611
    case RandomRaySourceShape::FLAT:
266✔
612
      shape = "Flat";
266✔
613
      break;
614
    case RandomRaySourceShape::LINEAR:
184✔
615
      shape = "Linear";
184✔
616
      break;
617
    case RandomRaySourceShape::LINEAR_XY:
24✔
618
      shape = "Linear XY";
24✔
619
      break;
UNCOV
620
    default:
×
UNCOV
621
      fatal_error("Invalid random ray source shape");
×
622
    }
623
    fmt::print(" Source Shape                      = {}\n", shape);
474✔
624
    std::string sample_method;
948!
625
    switch (RandomRay::sample_method_) {
474!
626
    case RandomRaySampleMethod::PRNG:
458✔
627
      sample_method = "PRNG";
458✔
628
      break;
629
    case RandomRaySampleMethod::HALTON:
8✔
630
      sample_method = "Halton";
8✔
631
      break;
632
    case RandomRaySampleMethod::S2:
8✔
633
      sample_method = "PRNG S2";
8✔
634
      break;
635
    }
636
    fmt::print(" Sample Method                     = {}\n", sample_method);
474✔
637

638
    if (domain_->is_transport_stabilization_needed_) {
474✔
639
      fmt::print(" Transport XS Stabilization Used   = YES (rho = {:.3f})\n",
8✔
640
        FlatSourceDomain::diagonal_stabilization_rho_);
641
    } else {
642
      fmt::print(" Transport XS Stabilization Used   = NO\n");
466✔
643
    }
644

645
    header("Timing Statistics", 4);
474✔
646
    show_time("Total time for initialization", time_initialize.elapsed());
474✔
647
    show_time("Reading cross sections", time_read_xs.elapsed(), 1);
474✔
648
    show_time("Total simulation time", time_total.elapsed());
474✔
649
    show_time("Transport sweep only", time_transport.elapsed(), 1);
474✔
650
    show_time("Source update only", time_update_src.elapsed(), 1);
474✔
651
    show_time("Tally conversion only", time_tallies.elapsed(), 1);
474✔
652
    show_time("MPI source reductions only", time_bank_sendrecv.elapsed(), 1);
474✔
653
    show_time("Other iteration routines", misc_time, 1);
474✔
654
    if (settings::run_mode == RunMode::EIGENVALUE) {
474✔
655
      show_time("Time in inactive batches", time_inactive.elapsed());
200✔
656
    }
657
    show_time("Time in active batches", time_active.elapsed());
474✔
658
    show_time("Time writing statepoints", time_statepoint.elapsed());
474✔
659
    show_time("Total time for finalization", time_finalize.elapsed());
474✔
660
    show_time("Time per integration", time_per_integration);
474✔
661
  }
474✔
662

663
  if (settings::verbosity >= 4 && settings::run_mode == RunMode::EIGENVALUE) {
474!
664
    header("Results", 4);
200✔
665
    fmt::print(" k-effective                       = {:.5f} +/- {:.5f}\n",
200✔
666
      simulation::keff, simulation::keff_std);
667
  }
668
}
474✔
669

670
} // namespace openmc
671

672
//==============================================================================
673
// C API functions
674
//==============================================================================
675

676
void openmc_run_random_ray()
529✔
677
{
678
  //////////////////////////////////////////////////////////
679
  // Run forward simulation
680
  //////////////////////////////////////////////////////////
681

682
  // Check if adjoint calculation is needed, and if local adjoint source(s)
683
  // are present. If an adjoint calculation is needed and no sources are
684
  // specified, we will run a forward calculation first to calculate adjoint
685
  // sources for global variance reduction, then perform an adjoint
686
  // calculation later.
687
  bool adjoint_needed = openmc::FlatSourceDomain::adjoint_;
529✔
688
  bool fw_adjoint = openmc::model::adjoint_sources.empty() && adjoint_needed;
529✔
689

690
  // If we're going to do an adjoint simulation with forward-weighted adjoint
691
  // sources afterwards, report that this is the initial forward flux solve.
692
  if (!adjoint_needed || fw_adjoint) {
529✔
693
    // Configure the domain for forward simulation
694
    openmc::FlatSourceDomain::adjoint_ = false;
519✔
695

696
    if (adjoint_needed && openmc::mpi::master)
519✔
697
      openmc::header("FORWARD FLUX SOLVE", 3);
49✔
698
  } else {
699
    // Configure domain for adjoint simulation (later)
700
    openmc::FlatSourceDomain::adjoint_ = true;
10✔
701
  }
702

703
  // Initialize OpenMC general data structures
704
  openmc_simulation_init();
529✔
705

706
  // Validate that inputs meet requirements for random ray mode
707
  if (openmc::mpi::master)
529✔
708
    openmc::validate_random_ray_inputs();
425✔
709

710
  // Initialize Random Ray Simulation Object
711
  openmc::RandomRaySimulation sim;
529✔
712

713
  if (!adjoint_needed || fw_adjoint) {
529✔
714
    // Initialize fixed sources, if present
715
    sim.apply_fixed_sources_and_mesh_domains();
519✔
716

717
    // Execute random ray simulation
718
    sim.simulate();
519✔
719
  }
720

721
  //////////////////////////////////////////////////////////
722
  // Run adjoint simulation (if enabled)
723
  //////////////////////////////////////////////////////////
724

725
  if (!adjoint_needed) {
529✔
726
    return;
458✔
727
  }
728

729
  // Setup for adjoint simulation
730
  sim.prepare_adjoint_simulation(fw_adjoint);
71✔
731

732
  // Execute random ray simulation
733
  sim.simulate();
71✔
734
}
529✔
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