@@ -189,7 +189,7 @@ void validate_random_ray_inputs()
189189
190190 // Validate adjoint sources
191191 // /////////////////////////////////////////////////////////////////
192- if (FlatSourceDomain::adjoint_ && !model::adjoint_sources.empty ()) {
192+ if (FlatSourceDomain::adjoint_requested_ && !model::adjoint_sources.empty ()) {
193193 for (int i = 0 ; i < model::adjoint_sources.size (); i++) {
194194 Source* s = model::adjoint_sources[i].get ();
195195
@@ -289,7 +289,8 @@ void openmc_finalize_random_ray()
289289{
290290 FlatSourceDomain::volume_estimator_ = RandomRayVolumeEstimator::HYBRID ;
291291 FlatSourceDomain::volume_normalized_flux_tallies_ = false ;
292- FlatSourceDomain::adjoint_ = false ;
292+ FlatSourceDomain::adjoint_requested_ = false ;
293+ FlatSourceDomain::solve_ = RandomRaySolve::FORWARD ;
293294 FlatSourceDomain::fw_cadis_local_ = false ;
294295 FlatSourceDomain::fw_cadis_local_targets_.clear ();
295296 FlatSourceDomain::mesh_domain_map_.clear ();
@@ -356,19 +357,17 @@ void RandomRaySimulation::prepare_local_fixed_sources_adjoint()
356357 }
357358}
358359
359- void RandomRaySimulation::prepare_adjoint_simulation (bool fw_adjoint )
360+ void RandomRaySimulation::prepare_adjoint_simulation (bool from_forward )
360361{
361362 reset_timers ();
362363
363364 if (mpi::master)
364365 header (" ADJOINT FLUX SOLVE" , 3 );
365366
366- if (fw_adjoint) {
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 ;
371-
367+ if (from_forward) {
368+ // The forward solve has already run. Re-initialize OpenMC's general data
369+ // structures for the adjoint solve and derive the adjoint source from the
370+ // forward flux.
372371 openmc_simulation_init ();
373372
374373 prepare_fw_fixed_sources_adjoint ();
@@ -603,7 +602,8 @@ void RandomRaySimulation::print_results_random_ray(
603602 }
604603 fmt::print (" Volume Estimator Type = {}\n " , estimator);
605604
606- std::string adjoint_true = (FlatSourceDomain::adjoint_) ? " ON" : " OFF" ;
605+ std::string adjoint_true =
606+ (FlatSourceDomain::solve_ == RandomRaySolve::ADJOINT ) ? " ON" : " OFF" ;
607607 fmt::print (" Adjoint Flux Mode = {}\n " , adjoint_true);
608608
609609 std::string shape;
@@ -675,60 +675,49 @@ void RandomRaySimulation::print_results_random_ray(
675675
676676void openmc_run_random_ray ()
677677{
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_;
688- bool fw_adjoint = openmc::model::adjoint_sources.empty () && adjoint_needed;
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) {
693- // Configure the domain for forward simulation
694- openmc::FlatSourceDomain::adjoint_ = false ;
695-
696- if (adjoint_needed && openmc::mpi::master)
697- openmc::header (" FORWARD FLUX SOLVE" , 3 );
678+ using namespace openmc ;
679+
680+ // Determine which solves to run. If adjoint results are requested and no
681+ // user-defined adjoint source is present, an initial forward solve is needed
682+ // to construct the adjoint source from the forward flux (FW-CADIS). If the
683+ // user has defined an adjoint source, the forward solve is skipped and only
684+ // the adjoint solve is run.
685+ const bool run_adjoint = FlatSourceDomain::adjoint_requested_;
686+ const bool have_adjoint_source = !model::adjoint_sources.empty ();
687+ const bool run_forward = !(run_adjoint && have_adjoint_source);
688+
689+ // Set the initial solve type
690+ if (!run_forward) {
691+ FlatSourceDomain::solve_ = RandomRaySolve::ADJOINT ;
692+ } else if (run_adjoint) {
693+ FlatSourceDomain::solve_ = RandomRaySolve::FORWARD_FOR_ADJOINT ;
698694 } else {
699- // Configure domain for adjoint simulation (later)
700- openmc::FlatSourceDomain::adjoint_ = true ;
695+ FlatSourceDomain::solve_ = RandomRaySolve::FORWARD ;
701696 }
702697
703698 // Initialize OpenMC general data structures
704699 openmc_simulation_init ();
705700
706701 // Validate that inputs meet requirements for random ray mode
707- if (openmc:: mpi::master)
708- openmc:: validate_random_ray_inputs ();
702+ if (mpi::master)
703+ validate_random_ray_inputs ();
709704
710705 // Initialize Random Ray Simulation Object
711- openmc:: RandomRaySimulation sim;
706+ RandomRaySimulation sim;
712707
713- if (!adjoint_needed || fw_adjoint) {
714- // Initialize fixed sources, if present
708+ // Run the forward solve
709+ if (run_forward) {
710+ // When an adjoint solve follows, report this as the initial forward solve
711+ if (run_adjoint && mpi::master)
712+ header (" FORWARD FLUX SOLVE" , 3 );
715713 sim.apply_fixed_sources_and_mesh_domains ();
716-
717- // Execute random ray simulation
718714 sim.simulate ();
719715 }
720716
721- // ////////////////////////////////////////////////////////
722- // Run adjoint simulation (if enabled)
723- // ////////////////////////////////////////////////////////
724-
725- if (!adjoint_needed) {
726- return ;
717+ // Run the adjoint solve
718+ if (run_adjoint) {
719+ FlatSourceDomain::solve_ = RandomRaySolve::ADJOINT ;
720+ sim.prepare_adjoint_simulation (run_forward);
721+ sim.simulate ();
727722 }
728-
729- // Setup for adjoint simulation
730- sim.prepare_adjoint_simulation (fw_adjoint);
731-
732- // Execute random ray simulation
733- sim.simulate ();
734723}
0 commit comments