opm-simulators
AdaptiveTimeStepping.hpp
1 /*
2 */
3 #ifndef OPM_ADAPTIVE_TIME_STEPPING_HPP
4 #define OPM_ADAPTIVE_TIME_STEPPING_HPP
5 
8 
9 #include <opm/simulators/flow/rescoup/ReservoirCouplingEnabled.hpp>
10 #include <opm/simulators/timestepping/AdaptiveSimulatorTimer.hpp>
11 #include <opm/simulators/timestepping/SimulatorReport.hpp>
12 #include <opm/simulators/timestepping/SimulatorTimer.hpp>
13 #include <opm/simulators/timestepping/TimeStepControl.hpp>
14 #include <opm/simulators/timestepping/TimeStepControlInterface.hpp>
15 
16 #ifdef RESERVOIR_COUPLING_ENABLED
17 #include <opm/simulators/flow/rescoup/ReservoirCoupling.hpp>
18 #include <opm/simulators/flow/rescoup/ReservoirCouplingMaster.hpp>
19 #include <opm/simulators/flow/rescoup/ReservoirCouplingSlave.hpp>
20 #endif
21 
22 #include <functional>
23 #include <memory>
24 #include <set>
25 #include <string>
26 #include <tuple>
27 #include <vector>
28 
29 namespace Opm::Parameters {
30 
31 struct SolverContinueOnConvergenceFailure { static constexpr bool value = false; };
32 struct SolverMaxRestarts { static constexpr int value = 10; };
33 struct SolverVerbosity { static constexpr int value = 1; };
34 struct TimeStepVerbosity { static constexpr int value = 1; };
35 struct InitialTimeStepInDays { static constexpr double value = 1.0; };
36 struct FullTimeStepInitially { static constexpr bool value = false; };
37 struct TimeStepControl { static constexpr auto value = "pid+newtoniteration"; };
38 struct TimeStepControlTolerance { static constexpr double value = 1e-1; };
39 struct TimeStepControlTargetIterations { static constexpr int value = 30; };
40 struct TimeStepControlTargetNewtonIterations { static constexpr int value = 8; };
41 struct TimeStepControlDecayRate { static constexpr double value = 0.75; };
42 struct TimeStepControlGrowthRate { static constexpr double value = 1.25; };
43 struct TimeStepControlDecayDampingFactor { static constexpr double value = 1.0; };
44 struct TimeStepControlGrowthDampingFactor { static constexpr double value = 3.2; };
45 struct TimeStepControlFileName { static constexpr auto value = "timesteps"; };
46 struct MinTimeStepBeforeShuttingProblematicWellsInDays { static constexpr double value = 0.01; };
47 struct MinTimeStepBasedOnNewtonIterations { static constexpr double value = 0.0; };
48 struct TimeStepControlSafetyFactor { static constexpr double value = 0.8; };
49 struct TimeStepControlRejectCompletedStep { static constexpr bool value = false; };
50 struct TimeStepControlToleranceTestVersion { static constexpr auto value = "standard"; };
51 struct TimeStepControlMaxReductionTimeStep { static constexpr double value = 0.1; };
52 struct TimeStepControlParameters { static constexpr auto value = "0.125;0.25;0.125;0.75;0.25"; };
53 
54 } // namespace Opm::Parameters
55 
56 namespace Opm {
57 
58 struct Tuning;
59 class UnitSystem;
60 struct StepReport;
61 
62 namespace detail {
63  void logTimer(const AdaptiveSimulatorTimer& substep_timer);
64 
65  std::set<std::string>
66  consistentlyFailingWells(const std::vector<StepReport>& sr,
67  bool requireRepeatedFailures);
68  void registerAdaptiveParameters();
69 
70  std::tuple<TimeStepControlType, std::unique_ptr<TimeStepControlInterface>, bool>
71  createController(const UnitSystem& unitSystem);
72 }
73 
91 template<class TypeTag>
93 {
94 public:
117  using TuningUpdateCallback = std::function<bool(double elapsed,
118  double substep_length,
119  int sub_step_number)>;
120 
121 private:
123 
124  template <class Solver>
125  class SolutionTimeErrorSolverWrapper : public RelativeChangeInterface
126  {
127  public:
128  explicit SolutionTimeErrorSolverWrapper(const Solver& solver);
129  double relativeChange() const;
130 
131  private:
132  const Solver& solver_;
133  };
134 
135  // Forward declaration of SubStepIteration
136  template <class Solver> class SubStepIteration;
137 
152  template <class Solver>
153  class SubStepper {
154  public:
155  SubStepper(AdaptiveTimeStepping<TypeTag>& adaptive_time_stepping,
156  const SimulatorTimer& simulator_timer,
157  Solver& solver,
158  const bool is_event,
159  const TuningUpdateCallback& tuning_updater);
160 
161  AdaptiveTimeStepping<TypeTag>& getAdaptiveTimerStepper();
162 
166  SimulatorReport run();
167  friend class SubStepIteration<Solver>;
168 
169  private:
170  bool isReservoirCouplingMaster_() const;
171  bool isReservoirCouplingSlave_() const;
172 
187  void maybeModifySuggestedTimeStepAtBeginningOfReportStep_(const double originalTimeStep);
188 
191  bool maybeUpdateTuning_(double elapsed, double substep_length, int sub_step_number) const;
192 
193  double maxTimeStep_() const;
194 
197  SimulatorReport runStepOriginal_();
198 #ifdef RESERVOIR_COUPLING_ENABLED
199  ReservoirCouplingMaster<Scalar>& reservoirCouplingMaster_();
200  ReservoirCouplingSlave<Scalar>& reservoirCouplingSlave_();
201 
206  SimulatorReport runStepReservoirCouplingMaster_();
207 
211  SimulatorReport runStepReservoirCouplingSlave_();
212 #endif
213  double suggestedNextTimestep_() const;
214 
215  AdaptiveTimeStepping<TypeTag>& adaptive_time_stepping_;
216  const SimulatorTimer& simulator_timer_;
217  Solver& solver_;
218  const bool is_event_;
219  const TuningUpdateCallback& tuning_updater_;
220  };
221 
234  template <class Solver>
235  class SubStepIteration {
236  public:
237  SubStepIteration(SubStepper<Solver>& substepper,
238  AdaptiveSimulatorTimer& substep_timer,
239  const double original_time_step,
240  const bool final_step);
241 
244  SimulatorReport run();
245 
246  private:
247  bool checkContinueOnUnconvergedSolution_(double dt) const;
248  void checkTimeStepMaxRestartLimit_(const int restarts) const;
249  void checkTimeStepMinLimit_(const double new_time_step) const;
250  void chopTimeStep_(const double new_time_step);
251  bool chopTimeStepOrCloseFailingWells_(const double new_time_step);
252  boost::posix_time::ptime currentDateTime_() const;
253  int getNumIterations_(const SimulatorReportSingle& substep_report) const;
254  double growthFactor_() const;
255  bool ignoreConvergenceFailure_() const;
256  bool isReservoirCouplingMaster_() const;
257  bool isReservoirCouplingSlave_() const;
258  void markFirstSubStepAsFinished_() const;
259  void maybeReportSubStep_(SimulatorReportSingle substep_report) const;
260  double maybeRestrictTimeStepGrowth_(const double dt,
261  double dt_estimate,
262  const int restarts) const;
263  void maybeUpdateLastSubstepOfSyncTimestep_(double dt);
264 
282  void maybeUpdateTuningAndTimeStep_();
283  double maxGrowth_() const;
284  double minTimeStepBeforeClosingWells_() const;
285  double minTimeStep_() const;
286  double restartFactor_() const;
287  SimulatorReportSingle runSubStep_();
288  int solverRestartMax_() const;
289  double suggestedNextTimestep_() const;
290  void setSuggestedNextStep_(double step);
291  void setTimeStep_(double dt_estimate);
292  Solver& solver_() const;
293  bool solverVerbose_() const;
294  const SimulatorTimer& simulatorTimer_() const;
295  boost::posix_time::ptime startDateTime_() const;
296 #ifdef RESERVOIR_COUPLING_ENABLED
297  ReservoirCouplingMaster<Scalar>& reservoirCouplingMaster_() const;
298  ReservoirCouplingSlave<Scalar>& reservoirCouplingSlave_() const;
299 #endif
300  double timeStepControlComputeEstimate_(const double dt,
301  const int iterations,
302  const AdaptiveSimulatorTimer& substepTimer) const;
303  bool timeStepVerbose_() const;
304  void updateSuggestedNextStep_();
305  bool useNewtonIteration_() const;
306  double writeOutput_() const;
307 
308  SubStepper<Solver>& substepper_;
309  AdaptiveSimulatorTimer& substep_timer_;
310  const double original_time_step_;
311  const bool final_step_;
312  std::string cause_of_failure_;
313  AdaptiveTimeStepping<TypeTag>& adaptive_time_stepping_;
314  };
315 
316 public:
317  AdaptiveTimeStepping() = default;
318 
319  AdaptiveTimeStepping(const UnitSystem& unitSystem,
320  const SimulatorReport& full_report,
321  const double max_next_tstep = -1.0,
322  const bool terminalOutput = true);
323 
324  AdaptiveTimeStepping(double max_next_tstep,
325  const Tuning& tuning,
326  const UnitSystem& unitSystem,
327  const SimulatorReport& full_report,
328  const bool terminalOutput = true);
329 
330  bool operator==(const AdaptiveTimeStepping<TypeTag>& rhs) const;
331 
332  static void registerParameters();
333 
335  void setSuggestedNextStep(const double x);
336 
340  double suggestedNextStep() const;
341 
342  const TimeStepControlInterface& timeStepControl() const;
343 
362  template <class Solver>
363  SimulatorReport step(const SimulatorTimer& simulator_timer,
364  Solver& solver,
365  const bool is_event,
366  const TuningUpdateCallback& tuning_updater);
367 
377  void updateTUNING(double max_next_tstep, const Tuning& tuning);
378 
386  void updateNEXTSTEP(double max_next_tstep);
387 
388  template<class Serializer>
389  void serializeOp(Serializer& serializer);
390 
391  SimulatorReport& report();
392 
393  static AdaptiveTimeStepping<TypeTag> serializationTestObjectHardcoded();
394  static AdaptiveTimeStepping<TypeTag> serializationTestObjectPID();
395  static AdaptiveTimeStepping<TypeTag> serializationTestObjectPIDIt();
396  static AdaptiveTimeStepping<TypeTag> serializationTestObjectSimple();
397  static AdaptiveTimeStepping<TypeTag> serializationTestObject3rdOrder();
398 
399 private:
400  void maybeModifySuggestedTimeStepAtBeginningOfReportStep_(const double original_time_step,
401  const bool is_event);
402 
403  template<class Controller>
404  static AdaptiveTimeStepping<TypeTag> serializationTestObject_();
405 
406  template<class T, class Serializer>
407  void allocAndSerialize(Serializer& serializer);
408 
409  template<class T>
410  bool castAndComp(const AdaptiveTimeStepping<TypeTag>& Rhs) const;
411 
412 protected:
413  void init_(const UnitSystem& unitSystem);
414 
415  using TimeStepController = std::unique_ptr<TimeStepControlInterface>;
416 
418  TimeStepControlType time_step_control_type_{TimeStepControlType::PIDAndIterationCount};
419  TimeStepController time_step_control_{};
420  double restart_factor_{};
421  double growth_factor_{};
422  double max_growth_{};
423  double max_time_step_{};
424  double min_time_step_{};
427  bool solver_verbose_{false};
428  bool timestep_verbose_{false};
432  bool use_newton_iteration_{false};
433 
436  // We store a copy of the full simulator run report for output purposes,
437  // so it can be updated and passed to the summary writing code every
438  // substep (not just every report step).
439  SimulatorReport report_{};
440 };
441 
442 } // namespace Opm
443 
444 #include <opm/simulators/timestepping/AdaptiveTimeStepping_impl.hpp>
445 
446 #endif // OPM_ADAPTIVE_TIME_STEPPING_HPP
Definition: AdaptiveTimeStepping.hpp:32
Definition: AdaptiveTimeStepping.hpp:36
Definition: AdaptiveTimeStepping.hpp:50
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:233
Simulation timer for adaptive time stepping.
Definition: AdaptiveSimulatorTimer.hpp:40
double growth_factor_
factor to multiply time step when solver recovered from failed convergence
Definition: AdaptiveTimeStepping.hpp:421
Definition: AdaptiveTimeStepping.hpp:52
Definition: AdaptiveTimeStepping.hpp:43
Definition: ConvergenceReport.hpp:459
double suggested_next_timestep_
suggested size of next timestep
Definition: AdaptiveTimeStepping.hpp:429
Definition: AdaptiveTimeStepping.hpp:37
void setSuggestedNextStep(const double x)
Set the suggested length for the next substep [s].
Definition: AdaptiveTimeStepping_impl.hpp:299
Definition: AdaptiveTimeStepping.hpp:42
double max_growth_
factor that limits the maximum growth of a time step
Definition: AdaptiveTimeStepping.hpp:422
Definition: AdaptiveTimeStepping.hpp:34
Definition: AdaptiveTimeStepping.hpp:49
Adaptive time-stepping coordinator for the black-oil simulator.
Definition: AdaptiveTimeStepping.hpp:92
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: blackoilbioeffectsmodules.hh:45
Definition: AdaptiveTimeStepping.hpp:38
Definition: AdaptiveTimeStepping.hpp:39
Definition: AdaptiveTimeStepping.hpp:44
Definition: blackoilnewtonmethodparams.hpp:31
void updateNEXTSTEP(double max_next_tstep)
Set suggested_next_timestep_ to max_next_tstep iff max_next_tstep > 0.
Definition: AdaptiveTimeStepping_impl.hpp:324
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
bool ignore_convergence_failure_
continue instead of stop when minimum time step is reached
Definition: AdaptiveTimeStepping.hpp:425
int solver_restart_max_
how many restart of solver are allowed
Definition: AdaptiveTimeStepping.hpp:426
Definition: AdaptiveTimeStepping.hpp:51
Definition: AdaptiveTimeStepping.hpp:33
TimeStepController time_step_control_
time step control object
Definition: AdaptiveTimeStepping.hpp:419
Definition: AdaptiveTimeStepping.hpp:41
double max_time_step_
maximal allowed time step size in days
Definition: AdaptiveTimeStepping.hpp:423
TimeStepControlType time_step_control_type_
type of time step control object
Definition: AdaptiveTimeStepping.hpp:418
Definition: ReservoirCouplingMaster.hpp:38
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
The Opm property system, traits with inheritance.
Definition: AdaptiveTimeStepping.hpp:35
Definition: ReservoirCouplingSlave.hpp:40
RelativeChangeInterface.
Definition: TimeStepControlInterface.hpp:33
Definition: AdaptiveTimeStepping.hpp:31
bool full_timestep_initially_
beginning with the size of the time step from data file
Definition: AdaptiveTimeStepping.hpp:430
void updateTUNING(double max_next_tstep, const Tuning &tuning)
Apply TUNING keyword parameters.
Definition: AdaptiveTimeStepping_impl.hpp:336
bool solver_verbose_
solver verbosity
Definition: AdaptiveTimeStepping.hpp:427
bool timestep_verbose_
timestep verbosity
Definition: AdaptiveTimeStepping.hpp:428
double suggestedNextStep() const
Current suggested length for the next substep [s].
Definition: AdaptiveTimeStepping_impl.hpp:307
Definition: SimulatorReport.hpp:121
double timestep_after_event_
suggested size of timestep after an event
Definition: AdaptiveTimeStepping.hpp:431
bool use_newton_iteration_
use newton iteration count for adaptive time step control
Definition: AdaptiveTimeStepping.hpp:432
Definition: AdaptiveTimeStepping.hpp:48
Definition: SimulatorTimer.hpp:38
Defines a type tags and some fundamental properties all models.
Definition: AdaptiveTimeStepping.hpp:45
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), and WCYCLE updates.
Definition: AdaptiveTimeStepping.hpp:119
Definition: AdaptiveTimeStepping.hpp:40
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
Definition: AdaptiveTimeStepping.hpp:47