From d6767f5de69eef1c9e5b996088e59a93443edbe7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20H=C3=A6gland?= Date: Mon, 27 Jan 2025 10:15:39 +0100 Subject: [PATCH] Remove Solver template parameter to step() Remove the template paramater Solver used with the AdaptiveTimeStepping::solve() method. Replace the template parameter with a equivalent Solver value from Opm::Properties:: --- .../timestepping/AdaptiveTimeStepping.hpp | 26 +-- .../AdaptiveTimeStepping_impl.hpp | 175 +++++++----------- 2 files changed, 77 insertions(+), 124 deletions(-) diff --git a/opm/simulators/timestepping/AdaptiveTimeStepping.hpp b/opm/simulators/timestepping/AdaptiveTimeStepping.hpp index 3c5fc805c..33e81b7fb 100644 --- a/opm/simulators/timestepping/AdaptiveTimeStepping.hpp +++ b/opm/simulators/timestepping/AdaptiveTimeStepping.hpp @@ -13,6 +13,7 @@ #include #include +#include #include #include #include @@ -79,8 +80,11 @@ template class AdaptiveTimeStepping { private: + using Model = GetPropType; using Scalar = GetPropType; - template + using Solver = NonlinearSolver; + using Simulator = GetPropType; + class SolutionTimeErrorSolverWrapper : public RelativeChangeInterface { public: @@ -91,25 +95,21 @@ private: const Solver& solver_; }; - // Forward declaration of SubStepIteration - // TODO: This forward declaration is not enough for GCC version 9.4.0 (released June 2021), - // but it works fine with GCC version 13.2.0 (released July 2023). - template class SubStepIteration; + class SubStepIteration; - template class SubStepper { public: SubStepper( AdaptiveTimeStepping& adaptive_time_stepping, + Solver &solver, const SimulatorTimer& simulator_timer, - Solver& solver, const bool is_event, const std::function& tuning_updater ); AdaptiveTimeStepping& getAdaptiveTimerStepper(); SimulatorReport run(); - friend class AdaptiveTimeStepping::template SubStepIteration; + friend class AdaptiveTimeStepping::SubStepIteration; private: bool isReservoirCouplingMaster_() const; @@ -127,17 +127,17 @@ private: double suggestedNextTimestep_() const; AdaptiveTimeStepping& adaptive_time_stepping_; - const SimulatorTimer& simulator_timer_; Solver& solver_; + const SimulatorTimer& simulator_timer_; const bool is_event_; const std::function& tuning_updater_; + Simulator& simulator_; }; - template class SubStepIteration { public: SubStepIteration( - SubStepper& substepper, + SubStepper& substepper, AdaptiveSimulatorTimer& substep_timer, const double original_time_step, const bool final_step @@ -179,7 +179,7 @@ private: bool useNewtonIteration_() const; double writeOutput_() const; - SubStepper& substepper_; + SubStepper& substepper_; AdaptiveSimulatorTimer& substep_timer_; const double original_time_step_; const bool final_step_; @@ -209,10 +209,10 @@ public: void setReservoirCouplingMaster(ReservoirCouplingMaster *reservoir_coupling_master); void setReservoirCouplingSlave(ReservoirCouplingSlave *reservoir_coupling_slave); #endif + void setSuggestedNextStep(const double x); double suggestedNextStep() const; - template SimulatorReport step(const SimulatorTimer& simulator_timer, Solver& solver, const bool is_event, diff --git a/opm/simulators/timestepping/AdaptiveTimeStepping_impl.hpp b/opm/simulators/timestepping/AdaptiveTimeStepping_impl.hpp index d627a210a..df9192b1b 100644 --- a/opm/simulators/timestepping/AdaptiveTimeStepping_impl.hpp +++ b/opm/simulators/timestepping/AdaptiveTimeStepping_impl.hpp @@ -189,13 +189,13 @@ setReservoirCouplingSlave(ReservoirCouplingSlave *reservoir_coupling_slave) } #endif + /** \brief step method that acts like the solver::step method in a sub cycle of time steps \param tuningUpdater Function used to update TUNING parameters before each time step. ACTIONX might change tuning. */ template -template SimulatorReport AdaptiveTimeStepping:: step(const SimulatorTimer& simulator_timer, @@ -207,8 +207,8 @@ step(const SimulatorTimer& simulator_timer, )> tuning_updater ) { - SubStepper sub_stepper{ - *this, simulator_timer, solver, is_event, tuning_updater, + SubStepper sub_stepper{ + *this, solver, simulator_timer, is_event, tuning_updater, }; return sub_stepper.run(); } @@ -426,12 +426,11 @@ init_(const UnitSystem& unitSystem) ************************************************/ template -template -AdaptiveTimeStepping::SubStepper:: +AdaptiveTimeStepping::SubStepper:: SubStepper( AdaptiveTimeStepping& adaptive_time_stepping, - const SimulatorTimer& simulator_timer, Solver& solver, + const SimulatorTimer& simulator_timer, const bool is_event, const std::function -template AdaptiveTimeStepping& -AdaptiveTimeStepping::SubStepper:: +AdaptiveTimeStepping::SubStepper:: getAdaptiveTimerStepper() { return adaptive_time_stepping_; } template -template SimulatorReport -AdaptiveTimeStepping::SubStepper:: +AdaptiveTimeStepping::SubStepper:: run() { #ifdef RESERVOIR_COUPLING_ENABLED @@ -483,27 +481,24 @@ run() template -template bool -AdaptiveTimeStepping::SubStepper:: +AdaptiveTimeStepping::SubStepper:: isReservoirCouplingMaster_() const { - return this->adaptive_time_stepping_.reservoir_coupling_master_ != nullptr; + return this->simulator_.reservoirCouplingMaster() != nullptr; } template -template bool -AdaptiveTimeStepping::SubStepper:: +AdaptiveTimeStepping::SubStepper:: isReservoirCouplingSlave_() const { - return this->adaptive_time_stepping_.reservoir_coupling_slave_ != nullptr; + return this->simulator_.reservoirCouplingSlave() != nullptr; } template -template void -AdaptiveTimeStepping::SubStepper:: +AdaptiveTimeStepping::SubStepper:: maybeModifySuggestedTimeStepAtBeginningOfReportStep_(const double original_time_step) { this->adaptive_time_stepping_.maybeModifySuggestedTimeStepAtBeginningOfReportStep_( @@ -515,27 +510,24 @@ maybeModifySuggestedTimeStepAtBeginningOfReportStep_(const double original_time_ // It has to be called for each substep since TUNING might have been changed for next sub step due // to ACTIONX (via NEXTSTEP) or WCYCLE keywords. template -template bool -AdaptiveTimeStepping::SubStepper:: +AdaptiveTimeStepping::SubStepper:: maybeUpdateTuning_(double elapsed, double dt, int sub_step_number) const { return this->tuning_updater_(elapsed, dt, sub_step_number); } template -template double -AdaptiveTimeStepping::SubStepper:: +AdaptiveTimeStepping::SubStepper:: maxTimeStep_() const { return this->adaptive_time_stepping_.max_time_step_; } template -template SimulatorReport -AdaptiveTimeStepping::SubStepper:: +AdaptiveTimeStepping::SubStepper:: runStepOriginal_() { auto elapsed = this->simulator_timer_.simulationTimeElapsed(); @@ -552,29 +544,27 @@ runStepOriginal_() report_step, maxTimeStep_() }; - SubStepIteration substepIteration{*this, substep_timer, original_time_step, /*final_step=*/true}; + SubStepIteration substepIteration{*this, substep_timer, original_time_step, /*final_step=*/true}; return substepIteration.run(); } #ifdef RESERVOIR_COUPLING_ENABLED template -template ReservoirCouplingMaster& -AdaptiveTimeStepping::SubStepper:: +AdaptiveTimeStepping::SubStepper:: reservoirCouplingMaster_() { - return *adaptive_time_stepping_.reservoir_coupling_master_; + return *(this->simulator_.reservoirCouplingMaster()); } #endif #ifdef RESERVOIR_COUPLING_ENABLED template -template ReservoirCouplingSlave& -AdaptiveTimeStepping::SubStepper:: +AdaptiveTimeStepping::SubStepper:: reservoirCouplingSlave_() { - return *this->adaptive_time_stepping_.reservoir_coupling_slave_; + return *(this->simulator_.reservoirCouplingSlave()); } #endif @@ -610,9 +600,8 @@ reservoirCouplingSlave_() // report step. template -template SimulatorReport -AdaptiveTimeStepping::SubStepper:: +AdaptiveTimeStepping::SubStepper:: runStepReservoirCouplingMaster_() { bool substep_done = false; @@ -644,7 +633,7 @@ runStepReservoirCouplingMaster_() bool final_step = ReservoirCoupling::Seconds::compare_gt_or_eq( current_time + current_step_length, step_end_time ); - SubStepIteration substepIteration{*this, substep_timer, current_step_length, final_step}; + SubStepIteration substepIteration{*this, substep_timer, current_step_length, final_step}; auto sub_steps_report = substepIteration.run(); report += sub_steps_report; current_time += current_step_length; @@ -659,9 +648,8 @@ runStepReservoirCouplingMaster_() #ifdef RESERVOIR_COUPLING_ENABLED template -template SimulatorReport -AdaptiveTimeStepping::SubStepper:: +AdaptiveTimeStepping::SubStepper:: runStepReservoirCouplingSlave_() { bool substep_done = false; @@ -688,7 +676,7 @@ runStepReservoirCouplingSlave_() bool final_step = ReservoirCoupling::Seconds::compare_gt_or_eq( current_time + timestep, step_end_time ); - SubStepIteration substepIteration{*this, substep_timer, timestep, final_step}; + SubStepIteration substepIteration{*this, substep_timer, timestep, final_step}; auto sub_steps_report = substepIteration.run(); report += sub_steps_report; current_time += timestep; @@ -704,9 +692,8 @@ runStepReservoirCouplingSlave_() #endif template -template double -AdaptiveTimeStepping::SubStepper:: +AdaptiveTimeStepping::SubStepper:: suggestedNextTimestep_() const { return this->adaptive_time_stepping_.suggestedNextStep(); @@ -719,10 +706,9 @@ suggestedNextTimestep_() const ************************************************/ template -template -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: SubStepIteration( - SubStepper& substepper, + SubStepper& substepper, AdaptiveSimulatorTimer& substep_timer, const double original_time_step, bool final_step @@ -736,9 +722,8 @@ SubStepIteration( } template -template SimulatorReport -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: run() { auto& simulator = solver_().model().simulator(); @@ -820,9 +805,8 @@ run() template -template bool -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: checkContinueOnUnconvergedSolution_(double dt) const { bool continue_on_uncoverged_solution = ignoreConvergenceFailure_() && dt <= minTimeStep_(); @@ -839,9 +823,8 @@ checkContinueOnUnconvergedSolution_(double dt) const } template -template void -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: checkTimeStepMaxRestartLimit_(const int restarts) const { // If we have restarted (i.e. cut the timestep) too @@ -859,9 +842,8 @@ checkTimeStepMaxRestartLimit_(const int restarts) const } template -template void -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: checkTimeStepMinLimit_(const int new_time_step) const { // If we have restarted (i.e. cut the timestep) too @@ -881,9 +863,8 @@ checkTimeStepMinLimit_(const int new_time_step) const } template -template void -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: chopTimeStep_(const double new_time_step) { setTimeStep_(new_time_step); @@ -896,9 +877,8 @@ chopTimeStep_(const double new_time_step) } template -template bool -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: chopTimeStepOrCloseFailingWells_(const int new_time_step) { bool wells_shut = false; @@ -947,18 +927,16 @@ chopTimeStepOrCloseFailingWells_(const int new_time_step) } template -template boost::posix_time::ptime -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: currentDateTime_() const { return simulatorTimer_().currentDateTime(); } template -template int -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: getNumIterations_(const SimulatorReportSingle &substep_report) const { if (useNewtonIteration_()) { @@ -970,36 +948,32 @@ getNumIterations_(const SimulatorReportSingle &substep_report) const } template -template double -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: growthFactor_() const { return this->adaptive_time_stepping_.growth_factor_; } template -template bool -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: ignoreConvergenceFailure_() const { return adaptive_time_stepping_.ignore_convergence_failure_; } template -template double -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: maxGrowth_() const { return this->adaptive_time_stepping_.max_growth_; } template -template void -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: maybeReportSubStep_(SimulatorReportSingle substep_report) const { if (timeStepVerbose_()) { @@ -1010,9 +984,8 @@ maybeReportSubStep_(SimulatorReportSingle substep_report) const } template -template double -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: maybeRestrictTimeStepGrowth_(const double dt, double dt_estimate, const int restarts) const { // limit the growth of the timestep size by the growth factor @@ -1030,9 +1003,8 @@ maybeRestrictTimeStepGrowth_(const double dt, double dt_estimate, const int rest // It has to be called for each substep since TUNING might have been changed for next sub step due // to ACTIONX (via NEXTSTEP) or WCYCLE keywords. template -template void -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: maybeUpdateTuningAndTimeStep_() { // TODO: This function is currently only called if NEXTSTEP is activated from ACTIONX or @@ -1057,36 +1029,32 @@ maybeUpdateTuningAndTimeStep_() } template -template double -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: minTimeStepBeforeClosingWells_() const { return this->adaptive_time_stepping_.min_time_step_before_shutting_problematic_wells_; } template -template double -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: minTimeStep_() const { return this->adaptive_time_stepping_.min_time_step_; } template -template double -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: restartFactor_() const { return this->adaptive_time_stepping_.restart_factor_; } template -template SimulatorReportSingle -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: runSubStep_() { SimulatorReportSingle substep_report; @@ -1138,18 +1106,16 @@ runSubStep_() } template -template void -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: setTimeStep_(double dt_estimate) { this->substep_timer_.provideTimeStepEstimate(dt_estimate); } template -template -Solver& -AdaptiveTimeStepping::SubStepIteration:: +typename AdaptiveTimeStepping::Solver& +AdaptiveTimeStepping::SubStepIteration:: solver_() const { return this->substepper_.solver_; @@ -1157,75 +1123,67 @@ solver_() const template -template int -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: solverRestartMax_() const { return this->adaptive_time_stepping_.solver_restart_max_; } template -template void -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: setSuggestedNextStep_(double step) { this->adaptive_time_stepping_.setSuggestedNextStep(step); } template -template const SimulatorTimer& -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: simulatorTimer_() const { return this->substepper_.simulator_timer_; } template -template bool -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: solverVerbose_() const { return this->adaptive_time_stepping_.solver_verbose_; } template -template boost::posix_time::ptime -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: startDateTime_() const { return simulatorTimer_().startDateTime(); } template -template double -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: suggestedNextTimestep_() const { return this->adaptive_time_stepping_.suggestedNextStep(); } template -template double -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: timeStepControlComputeEstimate_(const double dt, const int iterations, double elapsed) const { // create object to compute the time error, simply forwards the call to the model - SolutionTimeErrorSolverWrapper relative_change{solver_()}; + SolutionTimeErrorSolverWrapper relative_change{solver_()}; return this->adaptive_time_stepping_.time_step_control_->computeTimeStepSize( dt, iterations, relative_change, elapsed); } template -template bool -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: timeStepVerbose_() const { return this->adaptive_time_stepping_.timestep_verbose_; @@ -1239,9 +1197,8 @@ timeStepVerbose_() const // (and the begginning of each report step). Note that the WCYCLE keyword can also update the // suggested time step via the maybeUpdateTuning_() method. template -template void -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: updateSuggestedNextStep_() { auto suggested_next_step = this->substep_timer_.currentStepLength(); @@ -1259,18 +1216,16 @@ updateSuggestedNextStep_() } template -template bool -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: useNewtonIteration_() const { return this->adaptive_time_stepping_.use_newton_iteration_; } template -template double -AdaptiveTimeStepping::SubStepIteration:: +AdaptiveTimeStepping::SubStepIteration:: writeOutput_() const { time::StopWatch perf_timer; @@ -1285,16 +1240,14 @@ writeOutput_() const * **********************************************/ template -template -AdaptiveTimeStepping::SolutionTimeErrorSolverWrapper::SolutionTimeErrorSolverWrapper( +AdaptiveTimeStepping::SolutionTimeErrorSolverWrapper::SolutionTimeErrorSolverWrapper( const Solver& solver ) : solver_{solver} {} template -template -double AdaptiveTimeStepping::SolutionTimeErrorSolverWrapper::relativeChange() const +double AdaptiveTimeStepping::SolutionTimeErrorSolverWrapper::relativeChange() const { // returns: || u^n+1 - u^n || / || u^n+1 || return solver_.model().relativeChange();