My Project
Loading...
Searching...
No Matches
AdaptiveTimeStepping.hpp
1/*
2*/
3#ifndef OPM_ADAPTIVE_TIME_STEPPING_HPP
4#define OPM_ADAPTIVE_TIME_STEPPING_HPP
5
6#include <dune/common/version.hh>
7#include <dune/istl/istlexception.hh>
8
9#include <opm/common/OpmLog/OpmLog.hpp>
10
11#include <opm/input/eclipse/Schedule/Tuning.hpp>
12
15
16#include <opm/simulators/timestepping/AdaptiveSimulatorTimer.hpp>
17#include <opm/simulators/timestepping/SimulatorReport.hpp>
18#include <opm/simulators/timestepping/SimulatorTimer.hpp>
19#include <opm/simulators/timestepping/TimeStepControl.hpp>
20#include <opm/simulators/timestepping/TimeStepControlInterface.hpp>
21
22#if HAVE_MPI
23#define RESERVOIR_COUPLING_ENABLED
24#endif
25#ifdef RESERVOIR_COUPLING_ENABLED
26#include <opm/simulators/flow/ReservoirCoupling.hpp>
27#include <opm/simulators/flow/ReservoirCouplingMaster.hpp>
28#include <opm/simulators/flow/ReservoirCouplingSlave.hpp>
29#endif
30
31#include <functional>
32#include <memory>
33#include <set>
34#include <sstream>
35#include <stdexcept>
36#include <string>
37#include <tuple>
38#include <utility>
39#include <vector>
40
41namespace Opm::Parameters {
42
43struct SolverContinueOnConvergenceFailure { static constexpr bool value = false; };
44struct SolverMaxRestarts { static constexpr int value = 10; };
45struct SolverVerbosity { static constexpr int value = 1; };
46struct TimeStepVerbosity { static constexpr int value = 1; };
47struct InitialTimeStepInDays { static constexpr double value = 1.0; };
48struct FullTimeStepInitially { static constexpr bool value = false; };
49struct TimeStepControl { static constexpr auto value = "pid+newtoniteration"; };
50struct TimeStepControlTolerance { static constexpr double value = 1e-1; };
51struct TimeStepControlTargetIterations { static constexpr int value = 30; };
52struct TimeStepControlTargetNewtonIterations { static constexpr int value = 8; };
53struct TimeStepControlDecayRate { static constexpr double value = 0.75; };
54struct TimeStepControlGrowthRate { static constexpr double value = 1.25; };
55struct TimeStepControlDecayDampingFactor { static constexpr double value = 1.0; };
56struct TimeStepControlGrowthDampingFactor { static constexpr double value = 3.2; };
57struct TimeStepControlFileName { static constexpr auto value = "timesteps"; };
58struct MinTimeStepBeforeShuttingProblematicWellsInDays { static constexpr double value = 0.01; };
59struct MinTimeStepBasedOnNewtonIterations { static constexpr double value = 0.0; };
60struct TimeStepControlSafetyFactor { static constexpr double value = 0.8; };
61
62} // namespace Opm::Parameters
63
64namespace Opm {
65
66class UnitSystem;
67struct StepReport;
68
69namespace detail {
70 void logTimer(const AdaptiveSimulatorTimer& substep_timer);
71
72 std::set<std::string> consistentlyFailingWells(const std::vector<StepReport>& sr, bool requireRepeatedFailures);
73 void registerAdaptiveParameters();
74
75 std::tuple<TimeStepControlType, std::unique_ptr<TimeStepControlInterface>, bool>
76 createController(const UnitSystem& unitSystem);
77}
78
79template<class TypeTag>
81{
82private:
84 template <class Solver>
85 class SolutionTimeErrorSolverWrapper : public RelativeChangeInterface
86 {
87 public:
88 explicit SolutionTimeErrorSolverWrapper(const Solver& solver);
89 double relativeChange() const;
90
91 private:
92 const Solver& solver_;
93 };
94
95 // Forward declaration of SubStepIteration
96 // TODO: This forward declaration is not enough for GCC version 9.4.0 (released June 2021),
97 // but it works fine with GCC version 13.2.0 (released July 2023).
98 template <class Solver> class SubStepIteration;
99
100 template <class Solver>
101 class SubStepper {
102 public:
103 SubStepper(
106 Solver& solver,
107 const bool is_event,
108 const std::function<bool(const double, const double, const int)>& tuning_updater
109 );
110
111 AdaptiveTimeStepping<TypeTag>& getAdaptiveTimerStepper();
112 SimulatorReport run();
113 friend class AdaptiveTimeStepping<TypeTag>::template SubStepIteration<Solver>;
114
115 private:
116 bool isReservoirCouplingMaster_() const;
117 bool isReservoirCouplingSlave_() const;
118 void maybeModifySuggestedTimeStepAtBeginningOfReportStep_(const double originalTimeStep);
119 bool maybeUpdateTuning_(double elapsed, double dt, int sub_step_number) const;
120 double maxTimeStep_() const;
121 SimulatorReport runStepOriginal_();
122#ifdef RESERVOIR_COUPLING_ENABLED
127#endif
128 double suggestedNextTimestep_() const;
129
130 AdaptiveTimeStepping<TypeTag>& adaptive_time_stepping_;
131 const SimulatorTimer& simulator_timer_;
132 Solver& solver_;
133 const bool is_event_;
134 const std::function<bool(double elapsed, double dt, int sub_step_number)>& tuning_updater_;
135 };
136
137 template <class Solver>
138 class SubStepIteration {
139 public:
140 SubStepIteration(
141 SubStepper<Solver>& substepper,
142 AdaptiveSimulatorTimer& substep_timer,
143 const double original_time_step,
144 const bool final_step
145 );
146
147 SimulatorReport run();
148
149 private:
150 bool checkContinueOnUnconvergedSolution_(double dt) const;
151 void checkTimeStepMaxRestartLimit_(const int restarts) const;
152 void checkTimeStepMinLimit_(const double new_time_step) const;
153 void chopTimeStep_(const double new_time_step);
154 bool chopTimeStepOrCloseFailingWells_(const double new_time_step);
155 boost::posix_time::ptime currentDateTime_() const;
156 int getNumIterations_(const SimulatorReportSingle &substep_report) const;
157 double growthFactor_() const;
158 bool ignoreConvergenceFailure_() const;
159 void maybeReportSubStep_(SimulatorReportSingle substep_report) const;
160 double maybeRestrictTimeStepGrowth_(
161 const double dt, double dt_estimate, const int restarts) const;
162 void maybeUpdateTuningAndTimeStep_();
163 double maxGrowth_() const;
164 double minTimeStepBeforeClosingWells_() const;
165 double minTimeStep_() const;
166 double restartFactor_() const;
167 SimulatorReportSingle runSubStep_();
168 int solverRestartMax_() const;
169 double suggestedNextTimestep_() const;
170 void setSuggestedNextStep_(double step);
171 void setTimeStep_(double dt_estimate);
172 Solver& solver_() const;
173 bool solverVerbose_() const;
174 const SimulatorTimer& simulatorTimer_() const;
175 boost::posix_time::ptime startDateTime_() const;
176 double timeStepControlComputeEstimate_(
177 const double dt, const int iterations, AdaptiveSimulatorTimer& substepTimer) const;
178 bool timeStepVerbose_() const;
179 void updateSuggestedNextStep_();
180 bool useNewtonIteration_() const;
181 double writeOutput_() const;
182
183 SubStepper<Solver>& substepper_;
184 AdaptiveSimulatorTimer& substep_timer_;
185 const double original_time_step_;
186 const bool final_step_;
187 std::string cause_of_failure_;
188 AdaptiveTimeStepping<TypeTag>& adaptive_time_stepping_;
189 };
190
191public:
192 AdaptiveTimeStepping() = default;
193
195 const UnitSystem& unitSystem,
197 const double max_next_tstep = -1.0,
198 const bool terminalOutput = true
199 );
200
202 double max_next_tstep,
203 const Tuning& tuning,
204 const UnitSystem& unitSystem,
206 const bool terminalOutput = true
207 );
208 bool operator==(const AdaptiveTimeStepping<TypeTag>& rhs);
209
210 static void registerParameters();
211#ifdef RESERVOIR_COUPLING_ENABLED
214#endif
215 void setSuggestedNextStep(const double x);
216 double suggestedNextStep() const;
217
218 template <class Solver>
220 Solver& solver,
221 const bool is_event,
222 const std::function<bool(const double, const double, const int)>
224
225 void updateTUNING(double max_next_tstep, const Tuning& tuning);
226 void updateNEXTSTEP(double max_next_tstep);
227
228 template<class Serializer>
229 void serializeOp(Serializer& serializer);
230
231 SimulatorReport& report();
232
233 static AdaptiveTimeStepping<TypeTag> serializationTestObjectHardcoded();
234 static AdaptiveTimeStepping<TypeTag> serializationTestObjectPID();
235 static AdaptiveTimeStepping<TypeTag> serializationTestObjectPIDIt();
236 static AdaptiveTimeStepping<TypeTag> serializationTestObjectSimple();
237 static AdaptiveTimeStepping<TypeTag> serializationTestObject3rdOrder();
238
239private:
240 void maybeModifySuggestedTimeStepAtBeginningOfReportStep_(const double original_time_step,
241 const bool is_event);
242
243 template<class Controller>
244 static AdaptiveTimeStepping<TypeTag> serializationTestObject_();
245
246 template<class T, class Serializer>
247 void allocAndSerialize(Serializer& serializer);
248
249 template<class T>
250 bool castAndComp(const AdaptiveTimeStepping<TypeTag>& Rhs) const;
251
252protected:
253 void init_(const UnitSystem& unitSystem);
254
255 using TimeStepController = std::unique_ptr<TimeStepControlInterface>;
256
257 TimeStepControlType time_step_control_type_;
258 TimeStepController time_step_control_;
261 double max_growth_;
272
275#ifdef RESERVOIR_COUPLING_ENABLED
278#endif
279 // We store a copy of the full simulator run report for output purposes,
280 // so it can be updated and passed to the summary writing code every
281 // substep (not just every report step).
282 SimulatorReport report_;
283};
284
285} // namespace Opm
286
287#include <opm/simulators/timestepping/AdaptiveTimeStepping_impl.hpp>
288#endif // OPM_ADAPTIVE_TIME_STEPPING_HPP
Defines a type tags and some fundamental properties all models.
Simulation timer for adaptive time stepping.
Definition AdaptiveSimulatorTimer.hpp:41
Definition AdaptiveTimeStepping.hpp:81
double max_growth_
factor that limits the maximum growth of a time step
Definition AdaptiveTimeStepping.hpp:261
double max_time_step_
maximal allowed time step size in days
Definition AdaptiveTimeStepping.hpp:262
bool solver_verbose_
solver verbosity
Definition AdaptiveTimeStepping.hpp:266
int solver_restart_max_
how many restart of solver are allowed
Definition AdaptiveTimeStepping.hpp:265
double timestep_after_event_
suggested size of timestep after an event
Definition AdaptiveTimeStepping.hpp:270
bool ignore_convergence_failure_
continue instead of stop when minimum time step is reached
Definition AdaptiveTimeStepping.hpp:264
double suggested_next_timestep_
suggested size of next timestep
Definition AdaptiveTimeStepping.hpp:268
TimeStepControlType time_step_control_type_
type of time step control object
Definition AdaptiveTimeStepping.hpp:257
SimulatorReport step(const SimulatorTimer &simulator_timer, Solver &solver, const bool is_event, const std::function< bool(const double, const double, const int)> tuning_updater)
step method that acts like the solver::step method in a sub cycle of time steps
Definition AdaptiveTimeStepping_impl.hpp:208
bool full_timestep_initially_
beginning with the size of the time step from data file
Definition AdaptiveTimeStepping.hpp:269
double growth_factor_
factor to multiply time step when solver recovered from failed convergence
Definition AdaptiveTimeStepping.hpp:260
double restart_factor_
factor to multiply time step with when solver fails to converge
Definition AdaptiveTimeStepping.hpp:259
double min_time_step_
minimal allowed time step size before throwing
Definition AdaptiveTimeStepping.hpp:263
TimeStepController time_step_control_
time step control object
Definition AdaptiveTimeStepping.hpp:258
double min_time_step_before_shutting_problematic_wells_
< shut problematic wells when time step size in days are less than this
Definition AdaptiveTimeStepping.hpp:274
bool timestep_verbose_
timestep verbosity
Definition AdaptiveTimeStepping.hpp:267
bool use_newton_iteration_
use newton iteration count for adaptive time step control
Definition AdaptiveTimeStepping.hpp:271
RelativeChangeInterface.
Definition TimeStepControlInterface.hpp:34
Definition ReservoirCouplingMaster.hpp:35
Definition ReservoirCouplingSlave.hpp:35
Definition SimulatorTimer.hpp:39
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition blackoilboundaryratevector.hh:37
constexpr auto getPropValue()
get the value data member of a property
Definition propertysystem.hh:242
typename Properties::Detail::GetPropImpl< TypeTag, Property >::type::type GetPropType
get the type alias defined in the property (equivalent to old macro GET_PROP_TYPE(....
Definition propertysystem.hh:235
The Opm property system, traits with inheritance.
Definition AdaptiveTimeStepping.hpp:48
Definition AdaptiveTimeStepping.hpp:47
Definition AdaptiveTimeStepping.hpp:59
Definition AdaptiveTimeStepping.hpp:43
Definition AdaptiveTimeStepping.hpp:44
Definition AdaptiveTimeStepping.hpp:45
Definition AdaptiveTimeStepping.hpp:55
Definition AdaptiveTimeStepping.hpp:53
Definition AdaptiveTimeStepping.hpp:57
Definition AdaptiveTimeStepping.hpp:56
Definition AdaptiveTimeStepping.hpp:54
Definition AdaptiveTimeStepping.hpp:60
Definition AdaptiveTimeStepping.hpp:51
Definition AdaptiveTimeStepping.hpp:52
Definition AdaptiveTimeStepping.hpp:50
Definition AdaptiveTimeStepping.hpp:49
Definition AdaptiveTimeStepping.hpp:46
A struct for returning timing data from a simulator to its caller.
Definition SimulatorReport.hpp:34
Definition SimulatorReport.hpp:100
Definition ConvergenceReport.hpp:447