20#ifndef OPM_ADAPTIVE_TIME_STEPPING_IMPL_HPP
21#define OPM_ADAPTIVE_TIME_STEPPING_IMPL_HPP
24#ifndef OPM_ADAPTIVE_TIME_STEPPING_HPP
30#include <dune/common/timer.hh>
31#include <dune/istl/istlexception.hh>
33#include <opm/common/Exceptions.hpp>
34#include <opm/common/ErrorMacros.hpp>
35#include <opm/common/OpmLog/OpmLog.hpp>
37#include <opm/grid/utility/StopWatch.hpp>
39#include <opm/input/eclipse/Schedule/Tuning.hpp>
41#include <opm/input/eclipse/Units/Units.hpp>
42#include <opm/input/eclipse/Units/UnitSystem.hpp>
54#include <boost/date_time/posix_time/posix_time.hpp>
55#include <fmt/format.h>
56#include <fmt/ranges.h>
64template<
class TypeTag>
68 const double max_next_tstep,
69 const bool terminal_output
71 : time_step_control_{}
72 , restart_factor_{Parameters::
Get<Parameters::SolverRestartFactor<Scalar>>()}
73 , growth_factor_{Parameters::
Get<Parameters::SolverGrowthFactor<Scalar>>()}
74 , max_growth_{Parameters::
Get<Parameters::SolverMaxGrowth<Scalar>>()}
76 Parameters::
Get<Parameters::SolverMaxTimeStepInDays<Scalar>>() * 24 * 60 * 60}
78 unit_system.to_si(UnitSystem::measure::time,
79 Parameters::
Get<Parameters::SolverMinTimeStep<Scalar>>())}
80 , ignore_convergence_failure_{
81 Parameters::
Get<Parameters::SolverContinueOnConvergenceFailure>()}
82 , solver_restart_max_{Parameters::
Get<Parameters::SolverMaxRestarts>()}
83 , solver_verbose_{Parameters::
Get<Parameters::SolverVerbosity>() > 0 && terminal_output}
84 , timestep_verbose_{Parameters::
Get<Parameters::TimeStepVerbosity>() > 0 && terminal_output}
85 , suggested_next_timestep_{
86 (max_next_tstep <= 0 ? Parameters::
Get<Parameters::InitialTimeStepInDays>()
87 : max_next_tstep) * 24 * 60 * 60}
88 , full_timestep_initially_{Parameters::
Get<Parameters::FullTimeStepInitially>()}
89 , timestep_after_event_{
90 Parameters::
Get<Parameters::TimeStepAfterEventInDays<Scalar>>() * 24 * 60 * 60}
91 , use_newton_iteration_{false}
92 , min_time_step_before_shutting_problematic_wells_{
93 Parameters::
Get<Parameters::MinTimeStepBeforeShuttingProblematicWellsInDays>() * unit::day}
105template<
class TypeTag>
108 const Tuning& tuning,
109 const UnitSystem& unit_system,
111 const bool terminal_output
113 : time_step_control_{}
114 , restart_factor_{tuning.TSFCNV}
115 , growth_factor_{tuning.TFDIFF}
116 , max_growth_{tuning.TSFMAX}
117 , max_time_step_{tuning.TSMAXZ}
118 , min_time_step_{tuning.TSMINZ}
119 , ignore_convergence_failure_{true}
120 , solver_restart_max_{Parameters::
Get<Parameters::SolverMaxRestarts>()}
121 , solver_verbose_{Parameters::
Get<Parameters::SolverVerbosity>() > 0 && terminal_output}
122 , timestep_verbose_{Parameters::
Get<Parameters::TimeStepVerbosity>() > 0 && terminal_output}
123 , suggested_next_timestep_{
124 max_next_tstep <= 0 ? Parameters::
Get<Parameters::InitialTimeStepInDays>() * 24 * 60 * 60
126 , full_timestep_initially_{Parameters::
Get<Parameters::FullTimeStepInitially>()}
127 , timestep_after_event_{tuning.TMAXWC}
128 , use_newton_iteration_{false}
129 , min_time_step_before_shutting_problematic_wells_{
130 Parameters::
Get<Parameters::MinTimeStepBeforeShuttingProblematicWellsInDays>() * unit::day}
136template<
class TypeTag>
148 switch (this->time_step_control_type_) {
150 result = castAndComp<HardcodedTimeStepControl>(rhs);
153 result = castAndComp<PIDAndIterationCountTimeStepControl>(rhs);
156 result = castAndComp<SimpleIterationCountTimeStepControl>(rhs);
159 result = castAndComp<PIDTimeStepControl>(rhs);
162 result = castAndComp<General3rdOrderController>(rhs);
178 this->min_time_step_before_shutting_problematic_wells_ ==
182template<
class TypeTag>
187 registerEclTimeSteppingParameters<Scalar>();
192template<
class TypeTag>
193template <
class Solver>
201 SubStepper<Solver> sub_stepper{
202 *
this, simulator_timer, solver, is_event, tuning_updater,
204 return sub_stepper.run();
207template<
class TypeTag>
208template<
class Serializer>
213 serializer(this->time_step_control_type_);
214 switch (this->time_step_control_type_) {
216 allocAndSerialize<HardcodedTimeStepControl>(serializer);
219 allocAndSerialize<PIDAndIterationCountTimeStepControl>(serializer);
222 allocAndSerialize<SimpleIterationCountTimeStepControl>(serializer);
225 allocAndSerialize<PIDTimeStepControl>(serializer);
228 allocAndSerialize<General3rdOrderController>(serializer);
231 serializer(this->restart_factor_);
232 serializer(this->growth_factor_);
233 serializer(this->max_growth_);
234 serializer(this->max_time_step_);
235 serializer(this->min_time_step_);
236 serializer(this->ignore_convergence_failure_);
237 serializer(this->solver_restart_max_);
238 serializer(this->solver_verbose_);
239 serializer(this->timestep_verbose_);
240 serializer(this->suggested_next_timestep_);
241 serializer(this->full_timestep_initially_);
242 serializer(this->timestep_after_event_);
243 serializer(this->use_newton_iteration_);
244 serializer(this->min_time_step_before_shutting_problematic_wells_);
247template<
class TypeTag>
255template<
class TypeTag>
260 return serializationTestObject_<HardcodedTimeStepControl>();
263template<
class TypeTag>
268 return serializationTestObject_<PIDTimeStepControl>();
271template<
class TypeTag>
276 return serializationTestObject_<PIDAndIterationCountTimeStepControl>();
279template<
class TypeTag>
284 return serializationTestObject_<SimpleIterationCountTimeStepControl>();
287template<
class TypeTag>
292 return serializationTestObject_<General3rdOrderController>();
296template<
class TypeTag>
301 this->suggested_next_timestep_ = x;
304template<
class TypeTag>
309 return this->suggested_next_timestep_;
312template<
class TypeTag>
317 return *this->time_step_control_;
321template<
class TypeTag>
328 if (max_next_tstep > 0) {
329 this->suggested_next_timestep_ = max_next_tstep;
333template<
class TypeTag>
336updateTUNING(
double max_next_tstep,
const Tuning& tuning)
338 this->restart_factor_ = tuning.TSFCNV;
339 this->growth_factor_ = tuning.TFDIFF;
340 this->max_growth_ = tuning.TSFMAX;
341 this->max_time_step_ = tuning.TSMAXZ;
342 updateNEXTSTEP(max_next_tstep);
343 this->timestep_after_event_ = tuning.TMAXWC;
350template<
class TypeTag>
351template<
class T,
class Serializer>
356 if (!serializer.isSerializing()) {
357 this->time_step_control_ = std::make_unique<T>();
359 serializer(*
static_cast<T*
>(this->time_step_control_.get()));
362template<
class TypeTag>
365AdaptiveTimeStepping<TypeTag>::
366castAndComp(
const AdaptiveTimeStepping<TypeTag>& Rhs)
const
368 const T* lhs =
static_cast<const T*
>(this->time_step_control_.get());
369 const T* rhs =
static_cast<const T*
>(Rhs.time_step_control_.get());
373template<
class TypeTag>
375AdaptiveTimeStepping<TypeTag>::
376maybeModifySuggestedTimeStepAtBeginningOfReportStep_(
const double original_time_step,
380 if (this->suggested_next_timestep_ < 0) {
381 this->suggested_next_timestep_ = this->restart_factor_ * original_time_step;
384 if (this->full_timestep_initially_) {
385 this->suggested_next_timestep_ = original_time_step;
389 if (is_event && this->timestep_after_event_ > 0) {
390 this->suggested_next_timestep_ = this->timestep_after_event_;
394template<
class TypeTag>
395template<
class Controller>
396AdaptiveTimeStepping<TypeTag>
397AdaptiveTimeStepping<TypeTag>::
398serializationTestObject_()
400 AdaptiveTimeStepping<TypeTag> result;
402 result.restart_factor_ = 1.0;
403 result.growth_factor_ = 2.0;
404 result.max_growth_ = 3.0;
405 result.max_time_step_ = 4.0;
406 result.min_time_step_ = 5.0;
407 result.ignore_convergence_failure_ =
true;
408 result.solver_restart_max_ = 6;
409 result.solver_verbose_ =
true;
410 result.timestep_verbose_ =
true;
411 result.suggested_next_timestep_ = 7.0;
412 result.full_timestep_initially_ =
true;
413 result.use_newton_iteration_ =
true;
414 result.min_time_step_before_shutting_problematic_wells_ = 9.0;
415 result.time_step_control_type_ = Controller::Type;
416 result.time_step_control_ =
417 std::make_unique<Controller>(Controller::serializationTestObject());
426template<
class TypeTag>
428init_(
const UnitSystem& unitSystem)
430 std::tie(time_step_control_type_,
434 if (this->growth_factor_ < 1.0) {
435 OPM_THROW(std::runtime_error,
436 "Growth factor cannot be less than 1.");
446template<
class TypeTag>
447template<
class Solver>
453 const TuningUpdateCallback& tuning_updater)
454 : adaptive_time_stepping_{adaptive_time_stepping}
455 , simulator_timer_{simulator_timer}
457 , is_event_{is_event}
458 , tuning_updater_{tuning_updater}
462template<
class TypeTag>
463template<
class Solver>
464AdaptiveTimeStepping<TypeTag>&
465AdaptiveTimeStepping<TypeTag>::SubStepper<Solver>::
466getAdaptiveTimerStepper()
468 return adaptive_time_stepping_;
471template<
class TypeTag>
472template<
class Solver>
474AdaptiveTimeStepping<TypeTag>::SubStepper<Solver>::
477#ifdef RESERVOIR_COUPLING_ENABLED
478 if (isReservoirCouplingSlave_() && reservoirCouplingSlave_().activated()) {
479 return runStepReservoirCouplingSlave_();
481 else if (isReservoirCouplingMaster_() && reservoirCouplingMaster_().activated()) {
482 return runStepReservoirCouplingMaster_();
485 return runStepOriginal_();
488 return runStepOriginal_();
496#ifdef RESERVOIR_COUPLING_ENABLED
497template<
class TypeTag>
498template<
class Solver>
500AdaptiveTimeStepping<TypeTag>::SubStepper<Solver>::
501isReservoirCouplingMaster_()
const
503 return this->solver_.model().simulator().reservoirCouplingMaster() !=
nullptr;
506template<
class TypeTag>
507template<
class Solver>
509AdaptiveTimeStepping<TypeTag>::SubStepper<Solver>::
510isReservoirCouplingSlave_()
const
512 return this->solver_.model().simulator().reservoirCouplingSlave() !=
nullptr;
516template<
class TypeTag>
517template<
class Solver>
519AdaptiveTimeStepping<TypeTag>::SubStepper<Solver>::
520maybeModifySuggestedTimeStepAtBeginningOfReportStep_(
const double original_time_step)
522 this->adaptive_time_stepping_.maybeModifySuggestedTimeStepAtBeginningOfReportStep_(
523 original_time_step, this->is_event_
530template<
class TypeTag>
531template<
class Solver>
533AdaptiveTimeStepping<TypeTag>::SubStepper<Solver>::
534maybeUpdateTuning_(
double elapsed,
double substep_length,
int sub_step_number)
const
536 return this->tuning_updater_(elapsed, substep_length, sub_step_number);
539template<
class TypeTag>
540template<
class Solver>
542AdaptiveTimeStepping<TypeTag>::SubStepper<Solver>::
545 return this->adaptive_time_stepping_.max_time_step_;
548template <
class TypeTag>
549template <
class Solver>
551AdaptiveTimeStepping<TypeTag>::SubStepper<Solver>::
554 const auto elapsed = this->simulator_timer_.simulationTimeElapsed();
555 const auto original_time_step = this->simulator_timer_.currentStepLength();
556 const auto report_step = this->simulator_timer_.reportStepNum();
557 maybeUpdateTuning_(elapsed, suggestedNextTimestep_(), 0);
558 maybeModifySuggestedTimeStepAtBeginningOfReportStep_(original_time_step);
560 AdaptiveSimulatorTimer substep_timer{
561 this->simulator_timer_.startDateTime(),
564 suggestedNextTimestep_(),
568 SubStepIteration<Solver> substepIteration{*
this, substep_timer, original_time_step,
true};
569 return substepIteration.run();
572#ifdef RESERVOIR_COUPLING_ENABLED
573template <
class TypeTag>
574template <
class Solver>
575ReservoirCouplingMaster<typename AdaptiveTimeStepping<TypeTag>::Scalar>&
576AdaptiveTimeStepping<TypeTag>::SubStepper<Solver>::
577reservoirCouplingMaster_()
579 return *(this->solver_.model().simulator().reservoirCouplingMaster());
583#ifdef RESERVOIR_COUPLING_ENABLED
584template <
class TypeTag>
585template <
class Solver>
586ReservoirCouplingSlave<typename AdaptiveTimeStepping<TypeTag>::Scalar>&
587AdaptiveTimeStepping<TypeTag>::SubStepper<Solver>::
588reservoirCouplingSlave_()
590 return *(this->solver_.model().simulator().reservoirCouplingSlave());
594#ifdef RESERVOIR_COUPLING_ENABLED
625template <
class TypeTag>
626template <
class Solver>
628AdaptiveTimeStepping<TypeTag>::SubStepper<Solver>::
629runStepReservoirCouplingMaster_()
632 const double original_time_step = this->simulator_timer_.currentStepLength();
633 double current_time{this->simulator_timer_.simulationTimeElapsed()};
634 double step_end_time = current_time + original_time_step;
635 auto current_step_length = original_time_step;
636 auto report_step_idx = this->simulator_timer_.currentStepNum();
637 if (report_step_idx == 0 && iteration == 0) {
638 reservoirCouplingMaster_().initTimeStepping();
640 SimulatorReport report;
642 reservoirCouplingMaster_().maybeReceiveActivationHandshakeFromSlaves(current_time);
644 reservoirCouplingMaster_().sendDontTerminateSignalToSlaves();
645 reservoirCouplingMaster_().receiveNextReportDateFromSlaves();
646 bool start_of_report_step = (iteration == 0);
647 if (start_of_report_step) {
648 reservoirCouplingMaster_().initStartOfReportStep(report_step_idx);
650 current_step_length = reservoirCouplingMaster_().maybeChopSubStep(
651 current_step_length, current_time);
652 auto num_active = reservoirCouplingMaster_().numActivatedSlaves();
653 OpmLog::info(fmt::format(
654 "\nChoosing next sync time between master and {} active slave {}: {:.2f} days",
655 num_active, (num_active == 1 ?
"process" :
"processes"),
656 current_step_length / unit::day
658 reservoirCouplingMaster_().sendNextTimeStepToSlaves(current_step_length);
659 if (start_of_report_step) {
660 maybeUpdateTuning_(current_time, suggestedNextTimestep_(), 0);
661 maybeModifySuggestedTimeStepAtBeginningOfReportStep_(current_step_length);
663 AdaptiveSimulatorTimer substep_timer{
664 this->simulator_timer_.startDateTime(),
667 suggestedNextTimestep_(),
668 this->simulator_timer_.reportStepNum(),
672 current_time + current_step_length, step_end_time
677 reservoirCouplingMaster_().setFirstSubstepOfSyncTimestep(
true);
681 reservoirCouplingMaster_().setNeedsSlaveDataReceive(
true);
682 SubStepIteration<Solver> substepIteration{*
this, substep_timer, current_step_length, final_step};
683 const auto sub_steps_report = substepIteration.run();
684 report += sub_steps_report;
685 current_time += current_step_length;
695#ifdef RESERVOIR_COUPLING_ENABLED
696template <
class TypeTag>
697template <
class Solver>
699AdaptiveTimeStepping<TypeTag>::SubStepper<Solver>::
700runStepReservoirCouplingSlave_()
703 const double original_time_step = this->simulator_timer_.currentStepLength();
704 double current_time{this->simulator_timer_.simulationTimeElapsed()};
705 double step_end_time = current_time + original_time_step;
706 SimulatorReport report;
707 auto report_step_idx = this->simulator_timer_.currentStepNum();
708 if (report_step_idx == 0 && iteration == 0) {
709 reservoirCouplingSlave_().initTimeStepping();
712 bool start_of_report_step = (iteration == 0);
713 if (reservoirCouplingSlave_().maybeReceiveTerminateSignalFromMaster()) {
717 reservoirCouplingSlave_().sendNextReportDateToMasterProcess();
718 const auto timestep = reservoirCouplingSlave_().receiveNextTimeStepFromMaster();
719 if (start_of_report_step) {
720 maybeUpdateTuning_(current_time, suggestedNextTimestep_(), 0);
721 maybeModifySuggestedTimeStepAtBeginningOfReportStep_(timestep);
723 AdaptiveSimulatorTimer substep_timer{
724 this->simulator_timer_.startDateTime(),
727 suggestedNextTimestep_(),
728 this->simulator_timer_.reportStepNum(),
732 current_time + timestep, step_end_time
737 reservoirCouplingSlave_().setFirstSubstepOfSyncTimestep(
true);
738 SubStepIteration<Solver> substepIteration{*
this, substep_timer, timestep, final_step};
739 const auto sub_steps_report = substepIteration.run();
740 report += sub_steps_report;
741 current_time += timestep;
752template <
class TypeTag>
753template <
class Solver>
755AdaptiveTimeStepping<TypeTag>::SubStepper<Solver>::
756suggestedNextTimestep_()
const
758 return this->adaptive_time_stepping_.suggestedNextStep();
767template<
class TypeTag>
768template<
class Solver>
769AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
770SubStepIteration(SubStepper<Solver>& substepper,
771 AdaptiveSimulatorTimer& substep_timer,
772 const double original_time_step,
774 : substepper_{substepper}
775 , substep_timer_{substep_timer}
776 , original_time_step_{original_time_step}
777 , final_step_{final_step}
778 , adaptive_time_stepping_{substepper.getAdaptiveTimerStepper()}
782template <
class TypeTag>
783template <
class Solver>
785AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
788 auto& simulator = solver_().model().simulator();
789 auto& problem = simulator.problem();
792 SimulatorReport report;
795 while (!this->substep_timer_.done()) {
799 maybeUpdateTuningAndTimeStep_();
801 const double dt = this->substep_timer_.currentStepLength();
802 if (timeStepVerbose_()) {
806 maybeUpdateLastSubstepOfSyncTimestep_(dt);
807 auto substep_report = runSubStep_();
808 markFirstSubStepAsFinished_();
810 if (substep_report.converged || checkContinueOnUnconvergedSolution_(dt)) {
811 Dune::Timer perfTimer;
814 problem.setSubStepReport(substep_report);
815 auto& full_report = adaptive_time_stepping_.report();
816 full_report += substep_report;
817 problem.setSimulationReport(full_report);
818 problem.endTimeStep();
819 substep_report.pre_post_time += perfTimer.stop();
821 report += substep_report;
823 ++this->substep_timer_;
825 const int iterations = getNumIterations_(substep_report);
826 auto dt_estimate = timeStepControlComputeEstimate_(
827 dt, iterations, this->substep_timer_);
829 assert(dt_estimate > 0);
830 dt_estimate = maybeRestrictTimeStepGrowth_(dt, dt_estimate, restarts);
833 maybeReportSubStep_(substep_report);
834 if (this->final_step_ && this->substep_timer_.done()) {
839 report.success.output_write_time += writeOutput_();
843 setTimeStep_(dt_estimate);
845 report.success.converged = this->substep_timer_.done();
846 this->substep_timer_.setLastStepFailed(
false);
849 report += substep_report;
850 this->substep_timer_.setLastStepFailed(
true);
851 checkTimeStepMaxRestartLimit_(restarts);
853 double new_time_step = restartFactor_() * dt;
854 if (substep_report.time_step_rejected) {
855 const double tol = Parameters::Get<Parameters::TimeStepControlTolerance>();
856 const double safetyFactor = Parameters::Get<Parameters::TimeStepControlSafetyFactor>();
857 const double temp_time_step = std::sqrt(safetyFactor * tol / solver_().model().relativeChange()) * dt;
858 if (temp_time_step < dt) {
859 new_time_step = temp_time_step;
862 checkTimeStepMinLimit_(new_time_step);
863 bool wells_shut =
false;
864 if (new_time_step > minTimeStepBeforeClosingWells_()) {
865 chopTimeStep_(new_time_step);
867 wells_shut = chopTimeStepOrCloseFailingWells_(new_time_step);
876 problem.setNextTimeStepSize(this->substep_timer_.currentStepLength());
878 updateSuggestedNextStep_();
888template<
class TypeTag>
889template<
class Solver>
891AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
892checkContinueOnUnconvergedSolution_(
double dt)
const
894 const bool continue_on_uncoverged_solution = ignoreConvergenceFailure_() && dt <= minTimeStep_();
895 if (continue_on_uncoverged_solution && solverVerbose_()) {
897 const auto msg = fmt::format(
898 "Solver failed to converge but timestep {} is smaller or equal to {}\n"
899 "which is the minimum threshold given by option --solver-min-time-step\n",
902 OpmLog::problem(msg);
904 return continue_on_uncoverged_solution;
907template<
class TypeTag>
908template<
class Solver>
910AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
911checkTimeStepMaxRestartLimit_(
const int restarts)
const
915 if (restarts >= solverRestartMax_()) {
916 const auto msg = fmt::format(
917 fmt::runtime(
"Solver failed to converge after cutting timestep {} times."), restarts
919 if (solverVerbose_()) {
923 throw TimeSteppingBreakdown{msg};
927template<
class TypeTag>
928template<
class Solver>
930AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
931checkTimeStepMinLimit_(
const double new_time_step)
const
933 using Meas = UnitSystem::measure;
936 if (new_time_step < minTimeStep_()) {
937 std::string msg =
"Solver failed to converge after cutting timestep to ";
938 if (Parameters::Get<Parameters::EnableTuning>()) {
939 const UnitSystem& unit_system = solver_().model().simulator().vanguard().eclState().getDeckUnitSystem();
941 "{:.3E} {}\nwhich is the minimum threshold given by the TUNING keyword\n",
942 unit_system.from_si(Meas::time, minTimeStep_()),
943 unit_system.name(Meas::time)
948 "{:.3E} DAYS\nwhich is the minimum threshold given by option --solver-min-time-step\n",
949 minTimeStep_() / 86400.0
952 if (solverVerbose_()) {
956 throw TimeSteppingBreakdown{msg};
960template<
class TypeTag>
961template<
class Solver>
963AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
964chopTimeStep_(
const double new_time_step)
966 setTimeStep_(new_time_step);
967 if (solverVerbose_()) {
968 const auto msg = fmt::format(fmt::runtime(
"{}\nTimestep chopped to {} days\n"),
969 this->cause_of_failure_,
970 unit::convert::to(this->substep_timer_.currentStepLength(), unit::day));
971 OpmLog::problem(msg);
975template<
class TypeTag>
976template<
class Solver>
978AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
979chopTimeStepOrCloseFailingWells_(
const double new_time_step)
981 bool wells_shut =
false;
988 const bool requireRepeatedFailures =
989 new_time_step > (minTimeStepBeforeClosingWells_() * restartFactor_() * restartFactor_());
990 const std::set<std::string> failing_wells =
993 if (failing_wells.empty()) {
995 chopTimeStep_(new_time_step);
998 std::vector<std::string> shut_wells;
999 for (
const auto& well : failing_wells) {
1000 const bool was_shut =
1001 solver_().model().wellModel().forceShutWellByName(well,
1002 this->substep_timer_.simulationTimeElapsed(),
1005 shut_wells.push_back(well);
1009 if (shut_wells.empty()) {
1010 for (
const auto& well : failing_wells) {
1011 const bool was_shut =
1012 solver_().model().wellModel().forceShutWellByName(well,
1013 this->substep_timer_.simulationTimeElapsed(),
1016 shut_wells.push_back(well);
1021 if (shut_wells.empty()) {
1022 chopTimeStep_(new_time_step);
1025 if (solverVerbose_()) {
1026 const std::string msg =
1027 fmt::format(fmt::runtime(
"\nProblematic well(s) were shut: {}"
1028 "(retrying timestep)\n"),
1029 fmt::join(shut_wells,
" "));
1030 OpmLog::problem(msg);
1037template<
class TypeTag>
1038template<
class Solver>
1039boost::posix_time::ptime
1040AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1041currentDateTime_()
const
1043 return simulatorTimer_().currentDateTime();
1046template<
class TypeTag>
1047template<
class Solver>
1049AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1050getNumIterations_(
const SimulatorReportSingle &substep_report)
const
1052 if (useNewtonIteration_()) {
1053 return substep_report.total_newton_iterations;
1056 return substep_report.total_linear_iterations;
1060template<
class TypeTag>
1061template<
class Solver>
1063AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1064growthFactor_()
const
1066 return this->adaptive_time_stepping_.growth_factor_;
1069template<
class TypeTag>
1070template<
class Solver>
1072AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1073ignoreConvergenceFailure_()
const
1075 return adaptive_time_stepping_.ignore_convergence_failure_;
1078template<
class TypeTag>
1079template<
class Solver>
1081AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1082isReservoirCouplingMaster_()
const
1084 return this->substepper_.isReservoirCouplingMaster_();
1087template<
class TypeTag>
1088template<
class Solver>
1090AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1091isReservoirCouplingSlave_()
const
1093 return this->substepper_.isReservoirCouplingSlave_();
1096template<
class TypeTag>
1097template<
class Solver>
1099AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1100markFirstSubStepAsFinished_()
const
1102#ifdef RESERVOIR_COUPLING_ENABLED
1106 if (isReservoirCouplingMaster_()) {
1107 reservoirCouplingMaster_().setFirstSubstepOfSyncTimestep(
false);
1109 else if (isReservoirCouplingSlave_()) {
1110 reservoirCouplingSlave_().setFirstSubstepOfSyncTimestep(
false);
1116template<
class TypeTag>
1117template<
class Solver>
1119AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1122 return this->adaptive_time_stepping_.max_growth_;
1125template<
class TypeTag>
1126template<
class Solver>
1128AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1129maybeReportSubStep_(SimulatorReportSingle substep_report)
const
1131 if (timeStepVerbose_()) {
1132 std::ostringstream ss;
1133 substep_report.reportStep(ss);
1134 OpmLog::info(ss.str());
1138template<
class TypeTag>
1139template<
class Solver>
1141AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1142maybeRestrictTimeStepGrowth_(
const double dt,
double dt_estimate,
const int restarts)
const
1145 dt_estimate = std::min(dt_estimate,
double(maxGrowth_() * dt));
1146 assert(dt_estimate > 0);
1149 dt_estimate = std::min(growthFactor_() * dt, dt_estimate);
1156template<
class TypeTag>
1157template<
class Solver>
1159AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1160maybeUpdateLastSubstepOfSyncTimestep_([[maybe_unused]]
const double dt)
1162#ifdef RESERVOIR_COUPLING_ENABLED
1167 if (isReservoirCouplingSlave_()) {
1169 this->substep_timer_.simulationTimeElapsed() + dt,
1170 this->substep_timer_.totalTime()
1172 reservoirCouplingSlave_().setLastSubstepOfSyncTimestep(is_last);
1180template<
class TypeTag>
1181template<
class Solver>
1183AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1184maybeUpdateTuningAndTimeStep_()
1192 const auto old_value = suggestedNextTimestep_();
1193 if (this->substepper_.maybeUpdateTuning_(this->substep_timer_.simulationTimeElapsed(),
1194 this->substep_timer_.currentStepLength(),
1195 this->substep_timer_.currentStepNum()))
1202 setTimeStep_(suggestedNextTimestep_());
1203 setSuggestedNextStep_(old_value);
1207template<
class TypeTag>
1208template<
class Solver>
1210AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1211minTimeStepBeforeClosingWells_()
const
1213 return this->adaptive_time_stepping_.min_time_step_before_shutting_problematic_wells_;
1216template<
class TypeTag>
1217template<
class Solver>
1219AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1222 return this->adaptive_time_stepping_.min_time_step_;
1225#ifdef RESERVOIR_COUPLING_ENABLED
1226template<
class TypeTag>
1227template<
class Solver>
1228ReservoirCouplingMaster<typename AdaptiveTimeStepping<TypeTag>::Scalar>&
1229AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1230reservoirCouplingMaster_()
const
1232 return this->substepper_.reservoirCouplingMaster_();
1235template<
class TypeTag>
1236template<
class Solver>
1237ReservoirCouplingSlave<typename AdaptiveTimeStepping<TypeTag>::Scalar>&
1238AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1239reservoirCouplingSlave_()
const
1241 return this->substepper_.reservoirCouplingSlave_();
1245template<
class TypeTag>
1246template<
class Solver>
1248AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1249restartFactor_()
const
1251 return this->adaptive_time_stepping_.restart_factor_;
1254template<
class TypeTag>
1255template<
class Solver>
1256SimulatorReportSingle
1257AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1260 SimulatorReportSingle substep_report;
1262 auto handleFailure = [
this, &substep_report]
1263 (
const std::string& failure_reason,
const std::exception& e,
bool log_exception =
true)
1265 substep_report = solver_().failureReport();
1266 this->cause_of_failure_ = failure_reason;
1267 if (log_exception && solverVerbose_()) {
1268 OpmLog::debug(std::string(
"Caught Exception: ") + e.what());
1273 substep_report = solver_().step(this->substep_timer_, &this->adaptive_time_stepping_.timeStepControl());
1274 if (solverVerbose_()) {
1276 OpmLog::debug(
"Overall linear iterations used: "
1280 catch (
const TooManyIterations& e) {
1281 handleFailure(
"Solver convergence failure - Iteration limit reached", e);
1283 catch (
const TimeSteppingBreakdown& e) {
1284 handleFailure(e.what(), e);
1286 catch (
const ConvergenceMonitorFailure& e) {
1287 handleFailure(
"Convergence monitor failure", e,
false);
1289 catch (
const LinearSolverProblem& e) {
1290 handleFailure(
"Linear solver convergence failure", e);
1292 catch (
const NumericalProblem& e) {
1293 handleFailure(
"Solver convergence failure - Numerical problem encountered", e);
1295 catch (
const std::runtime_error& e) {
1296 handleFailure(
"Runtime error encountered", e);
1298 catch (
const Dune::ISTLError& e) {
1299 handleFailure(
"ISTL error - Time step too large", e);
1301 catch (
const Dune::MatrixBlockError& e) {
1302 handleFailure(
"Matrix block error", e);
1305 return substep_report;
1308template<
class TypeTag>
1309template<
class Solver>
1311AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1312setTimeStep_(
double dt_estimate)
1314 this->substep_timer_.provideTimeStepEstimate(dt_estimate);
1317template<
class TypeTag>
1318template<
class Solver>
1320AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1323 return this->substepper_.solver_;
1327template<
class TypeTag>
1328template<
class Solver>
1330AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1331solverRestartMax_()
const
1333 return this->adaptive_time_stepping_.solver_restart_max_;
1336template<
class TypeTag>
1337template<
class Solver>
1339AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1340setSuggestedNextStep_(
double step)
1342 this->adaptive_time_stepping_.setSuggestedNextStep(step);
1345template <
class TypeTag>
1346template <
class Solver>
1347const SimulatorTimer&
1348AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1349simulatorTimer_()
const
1351 return this->substepper_.simulator_timer_;
1354template <
class TypeTag>
1355template <
class Solver>
1357AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1358solverVerbose_()
const
1360 return this->adaptive_time_stepping_.solver_verbose_;
1363template<
class TypeTag>
1364template<
class Solver>
1365boost::posix_time::ptime
1366AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1367startDateTime_()
const
1369 return simulatorTimer_().startDateTime();
1372template <
class TypeTag>
1373template <
class Solver>
1375AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1376suggestedNextTimestep_()
const
1378 return this->adaptive_time_stepping_.suggestedNextStep();
1381template <
class TypeTag>
1382template <
class Solver>
1384AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1385timeStepControlComputeEstimate_(
const double dt,
const int iterations,
1386 const AdaptiveSimulatorTimer& substepTimer)
const
1389 const SolutionTimeErrorSolverWrapper<Solver> relative_change{solver_()};
1390 return this->adaptive_time_stepping_.time_step_control_->computeTimeStepSize(
1391 dt, iterations, relative_change, substepTimer);
1394template <
class TypeTag>
1395template <
class Solver>
1397AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1398timeStepVerbose_()
const
1400 return this->adaptive_time_stepping_.timestep_verbose_;
1410template <
class TypeTag>
1411template <
class Solver>
1413AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1414updateSuggestedNextStep_()
1416 auto suggested_next_step = this->substep_timer_.currentStepLength();
1417 if (! std::isfinite(suggested_next_step)) {
1418 suggested_next_step = this->original_time_step_;
1420 if (timeStepVerbose_()) {
1421 std::ostringstream ss;
1422 this->substep_timer_.report(ss);
1423 ss <<
"Suggested next step size = "
1424 << unit::convert::to(suggested_next_step, unit::day) <<
" (days)" << std::endl;
1425 OpmLog::debug(ss.str());
1427 setSuggestedNextStep_(suggested_next_step);
1430template <
class TypeTag>
1431template <
class Solver>
1433AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1434useNewtonIteration_()
const
1436 return this->adaptive_time_stepping_.use_newton_iteration_;
1439template <
class TypeTag>
1440template <
class Solver>
1442AdaptiveTimeStepping<TypeTag>::SubStepIteration<Solver>::
1445 time::StopWatch perf_timer;
1447 auto& problem = solver_().model().simulator().problem();
1448 problem.writeOutput(
true);
1449 return perf_timer.secsSinceStart();
1456template<
class TypeTag>
1457template<
class Solver>
1458AdaptiveTimeStepping<TypeTag>::
1459SolutionTimeErrorSolverWrapper<Solver>::
1460SolutionTimeErrorSolverWrapper(
const Solver& solver)
1464template<
class TypeTag>
1465template<
class Solver>
1466double AdaptiveTimeStepping<TypeTag>::SolutionTimeErrorSolverWrapper<Solver>::relativeChange()
const
1469 return solver_.model().relativeChange();
Adaptive time-stepping coordinator for the black-oil simulator.
Definition: AdaptiveTimeStepping.hpp:93
double max_growth_
factor that limits the maximum growth of a time step
Definition: AdaptiveTimeStepping.hpp:422
double max_time_step_
maximal allowed time step size in days
Definition: AdaptiveTimeStepping.hpp:423
bool solver_verbose_
solver verbosity
Definition: AdaptiveTimeStepping.hpp:427
int solver_restart_max_
how many restart of solver are allowed
Definition: AdaptiveTimeStepping.hpp:426
double timestep_after_event_
suggested size of timestep after an event
Definition: AdaptiveTimeStepping.hpp:431
void init_(const UnitSystem &unitSystem)
Definition: AdaptiveTimeStepping_impl.hpp:428
void setSuggestedNextStep(const double x)
Set the suggested length for the next substep [s].
Definition: AdaptiveTimeStepping_impl.hpp:299
double suggestedNextStep() const
Definition: AdaptiveTimeStepping_impl.hpp:307
bool operator==(const AdaptiveTimeStepping< TypeTag > &rhs) const
Definition: AdaptiveTimeStepping_impl.hpp:139
static AdaptiveTimeStepping< TypeTag > serializationTestObjectSimple()
Definition: AdaptiveTimeStepping_impl.hpp:282
bool ignore_convergence_failure_
continue instead of stop when minimum time step is reached
Definition: AdaptiveTimeStepping.hpp:425
void serializeOp(Serializer &serializer)
Definition: AdaptiveTimeStepping_impl.hpp:211
void updateTUNING(double max_next_tstep, const Tuning &tuning)
Apply TUNING keyword parameters.
Definition: AdaptiveTimeStepping_impl.hpp:336
TimeStepControlType time_step_control_type_
type of time step control object
Definition: AdaptiveTimeStepping.hpp:418
const TimeStepControlInterface & timeStepControl() const
Definition: AdaptiveTimeStepping_impl.hpp:315
std::function< bool(double elapsed, double substep_length, int sub_step_number)> TuningUpdateCallback
Callback invoked at the start of each substep to apply TUNING, NEXTSTEP (via ACTIONX),...
Definition: AdaptiveTimeStepping.hpp:119
bool full_timestep_initially_
beginning with the size of the time step from data file
Definition: AdaptiveTimeStepping.hpp:430
SimulatorReport step(const SimulatorTimer &simulator_timer, Solver &solver, const bool is_event, const TuningUpdateCallback &tuning_updater)
Run one report step by orchestrating adaptive substepping.
Definition: AdaptiveTimeStepping_impl.hpp:196
double growth_factor_
factor to multiply time step when solver recovered from failed convergence
Definition: AdaptiveTimeStepping.hpp:421
double restart_factor_
factor to multiply time step with when solver fails to converge
Definition: AdaptiveTimeStepping.hpp:420
double min_time_step_
minimal allowed time step size before throwing
Definition: AdaptiveTimeStepping.hpp:424
void updateNEXTSTEP(double max_next_tstep)
Set suggested_next_timestep_ to max_next_tstep iff max_next_tstep > 0.
Definition: AdaptiveTimeStepping_impl.hpp:324
static AdaptiveTimeStepping< TypeTag > serializationTestObjectHardcoded()
Definition: AdaptiveTimeStepping_impl.hpp:258
AdaptiveTimeStepping()=default
TimeStepController time_step_control_
time step control object
Definition: AdaptiveTimeStepping.hpp:419
static AdaptiveTimeStepping< TypeTag > serializationTestObjectPIDIt()
Definition: AdaptiveTimeStepping_impl.hpp:274
double min_time_step_before_shutting_problematic_wells_
< shut problematic wells when time step size in days are less than this
Definition: AdaptiveTimeStepping.hpp:435
static AdaptiveTimeStepping< TypeTag > serializationTestObject3rdOrder()
Definition: AdaptiveTimeStepping_impl.hpp:290
SimulatorReport & report()
Definition: AdaptiveTimeStepping_impl.hpp:250
static AdaptiveTimeStepping< TypeTag > serializationTestObjectPID()
Definition: AdaptiveTimeStepping_impl.hpp:266
static void registerParameters()
Definition: AdaptiveTimeStepping_impl.hpp:185
bool use_newton_iteration_
use newton iteration count for adaptive time step control
Definition: AdaptiveTimeStepping.hpp:432
Definition: SimulatorTimer.hpp:39
Definition: TimeStepControlInterface.hpp:51
auto Get(bool errorIfNotRegistered=true)
Retrieve a runtime parameter.
Definition: parametersystem.hpp:187
void logTimer(const AdaptiveSimulatorTimer &substep_timer)
void registerAdaptiveParameters()
std::set< std::string > consistentlyFailingWells(const std::vector< StepReport > &sr, bool requireRepeatedFailures)
std::tuple< TimeStepControlType, std::unique_ptr< TimeStepControlInterface >, bool > createController(const UnitSystem &unitSystem)
Definition: blackoilbioeffectsmodules.hh:45
std::string to_string(const ConvergenceReport::ReservoirFailure::Type t)
This file provides the infrastructure to retrieve run-time parameters.
static bool compare_gt_or_eq(double a, double b)
Determines if a is greater than b within the specified tolerance.
Definition: SimulatorReport.hpp:122