https://mooseframework.inl.gov
TimeStepper.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "TimeStepper.h"
11 #include "FEProblem.h"
12 #include "TransientBase.h"
13 #include "MooseApp.h"
14 
17 {
19  params.addParam<bool>(
20  "reset_dt", false, "Use when restarting a calculation to force a change in dt.");
21  params.addRangeCheckedParam<Real>(
22  "cutback_factor_at_failure",
23  0.5,
24  "cutback_factor_at_failure>0 & cutback_factor_at_failure<1",
25  "Factor to apply to timestep if a time step fails to converge.");
26  params.addParam<bool>("enable", true, "whether or not to enable the time stepper");
27  params.declareControllable("enable");
28 
29  params.registerBase("TimeStepper");
30  params.registerSystemAttributeName("TimeStepper");
31 
32  return params;
33 }
34 
36  : MooseObject(parameters),
37  Restartable(this, "TimeSteppers"),
38  ScalarCoupleable(this),
39  _fe_problem(parameters.have_parameter<FEProblemBase *>("_fe_problem_base")
40  ? *getParam<FEProblemBase *>("_fe_problem_base")
41  : *getParam<FEProblem *>("_fe_problem")),
42  _executioner(*getCheckedPointerParam<TransientBase *>("_executioner")),
43  _time(_fe_problem.time()),
44  _time_old(_fe_problem.timeOld()),
45  _t_step(_fe_problem.timeStep()),
46  _dt(_fe_problem.dt()),
47  _dt_min(_executioner.dtMin()),
48  _dt_max(_executioner.dtMax()),
49  _end_time(_executioner.endTime()),
50  _sync_times(_app.getOutputWarehouse().getSyncTimes()),
51  _timestep_tolerance(_executioner.timestepTol()),
52  _verbose(_executioner.verbose()),
53  _converged(true),
54  _cutback_factor_at_failure(getParam<Real>("cutback_factor_at_failure")),
55  _reset_dt(getParam<bool>("reset_dt")),
56  _has_reset_dt(false),
57  _currently_restepping(false),
58  _failure_count(0),
59  _current_dt(declareRestartableData<Real>("current_dt", 1.0))
60 {
61 }
62 
64 
65 void
67 {
68 }
69 
70 void
72 {
73  // Delete all sync times that are at or before the begin time
74  while (!_sync_times.empty() && _time + _timestep_tolerance >= *_sync_times.begin())
75  _sync_times.erase(_sync_times.begin());
76 }
77 
78 void
80 {
81  if (_t_step < 2 || (_reset_dt && !_has_reset_dt))
82  {
83  _has_reset_dt = true;
84 
85  if (converged())
87  else
89  }
90  else
91  {
92  if (converged())
94  else
96  }
97  if (_current_dt < -TOLERANCE)
98  mooseError("Negative time step detected :" + std::to_string(_current_dt) +
99  " Investigate the TimeStepper to resolve this error");
100 }
101 
102 bool
104 {
105  bool at_sync_point = false;
106 
107  std::ostringstream diag;
108 
109  // Don't let the time step size exceed maximum time step size
110  if (dt > _dt_max)
111  {
112  dt = _dt_max;
113  diag << "Limiting dt to dtmax: " << std::setw(9) << std::setprecision(6) << std::setfill('0')
114  << std::showpoint << std::left << _dt_max << std::endl;
115  }
116 
117  // Don't allow time step size to be smaller than minimum time step size
118  if (dt < _dt_min)
119  {
120  dt = _dt_min;
121  diag << "Increasing dt to dtmin: " << std::setw(9) << std::setprecision(6) << std::setfill('0')
122  << std::showpoint << std::left << _dt_min << std::endl;
123  }
124 
125  // Don't let time go beyond simulation end time (unless we're doing a half transient)
127  {
128  dt = _end_time - _time;
129  diag << "Limiting dt for end_time: " << std::setw(9) << std::setprecision(6)
130  << std::setfill('0') << std::showpoint << std::left << _end_time << " dt: " << std::setw(9)
131  << std::setprecision(6) << std::setfill('0') << std::showpoint << std::left << dt
132  << std::endl;
133  }
134 
135  // Adjust to a sync time if supplied
136  if (!_sync_times.empty() && _time + dt + _timestep_tolerance >= (*_sync_times.begin()))
137  {
138  dt = *_sync_times.begin() - _time;
139  diag << "Limiting dt for sync_time: " << std::setw(9) << std::setprecision(6)
140  << std::setfill('0') << std::showpoint << std::left << *_sync_times.begin()
141  << " dt: " << std::setw(9) << std::setprecision(6) << std::setfill('0') << std::showpoint
142  << std::left << dt << std::endl;
143 
144  if (dt <= 0.0)
145  {
146  _console << diag.str();
147  mooseError("Adjusting to sync_time resulted in a non-positive time step. dt: ",
148  dt,
149  " sync_time: ",
150  *_sync_times.begin(),
151  " time: ",
152  _time);
153  }
154 
155  at_sync_point = true;
156  }
157 
158  if (_verbose)
159  {
160  _console << diag.str();
161  }
162 
163  return at_sync_point;
164 }
165 
166 void
168 {
170 
171  if (!_converged)
172  _failure_count++;
173 }
174 
175 void
177 {
178  // If there are sync times at or before the current time, delete them
179  while (!_sync_times.empty() && _time + _timestep_tolerance >= *_sync_times.begin())
180  {
181  _sync_times.erase(_sync_times.begin());
182  }
183  // If we accept a step, we are no longer taking a failed step again
184  _currently_restepping = false;
185 }
186 
187 void
189 {
190  _currently_restepping = true;
192 }
193 
194 unsigned int
196 {
197  return _failure_count;
198 }
199 
200 bool
202 {
203  return _converged;
204 }
205 
206 Real
208 {
209  if (_dt <= _dt_min)
210  mooseError("Solve failed and timestep already at or below dtmin, cannot continue!");
211 
212  // cut the time step
215  else // (_cutback_factor_at_failure * _current_dt < _dt_min)
216  return _dt_min;
217 }
218 
219 void
221 {
222  _current_dt = dt;
223 }
224 
225 void
226 TimeStepper::forceNumSteps(const unsigned int num_steps)
227 {
228  _executioner.forceNumSteps(num_steps);
229 }
TransientBase & _executioner
Reference to transient executioner.
Definition: TimeStepper.h:126
static InputParameters validParams()
Definition: TimeStepper.C:16
Real & _timestep_tolerance
Definition: TimeStepper.h:138
virtual ~TimeStepper()
Definition: TimeStepper.C:63
virtual Real computeInitialDT()=0
Computes time step size for the initial time step.
virtual Real computeFailedDT()
Computes time step size after a failed time step.
Definition: TimeStepper.C:207
virtual void forceNumSteps(const unsigned int num_steps)
Set the number of time steps.
Definition: TimeStepper.C:226
A class for creating restricted objects.
Definition: Restartable.h:28
virtual SolveObject * timeStepSolveObject()
Return the solve object wrapped by time stepper.
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
Definition: FEProblem.h:20
void computeStep()
Called before a new step is started.
Definition: TimeStepper.C:79
unsigned int numFailures() const
Gets the number of failures and returns them.
Definition: TimeStepper.C:195
void registerSystemAttributeName(const std::string &value)
This method is used to define the MOOSE system name that is used by the TheWarehouse object for stori...
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
virtual bool constrainStep(Real &dt)
Called after computeStep() is called.
Definition: TimeStepper.C:103
virtual bool converged() const
If the time step converged.
Definition: TimeStepper.C:201
const Real _cutback_factor_at_failure
Cutback factor if a time step fails to converge.
Definition: TimeStepper.h:147
Real & _current_dt
Size of the current time step as computed by the Stepper. Note that the actual dt that was taken migh...
Definition: TimeStepper.h:163
std::set< Real > & _sync_times
Definition: TimeStepper.h:136
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
void registerBase(const std::string &value)
This method must be called from every base "Moose System" to create linkage with the Action System...
const bool & _verbose
whether a detailed diagnostic output should be printed
Definition: TimeStepper.h:141
FEProblemBase & _fe_problem
Definition: TimeStepper.h:124
virtual void acceptStep()
This gets called when time step is accepted.
Definition: TimeStepper.C:176
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:28
virtual Real computeDT()=0
Computes time step size after the initial time step.
TimeStepper(const InputParameters &parameters)
Definition: TimeStepper.C:35
bool _has_reset_dt
True if dt has been reset.
Definition: TimeStepper.h:153
virtual void forceNumSteps(const unsigned int num_steps)
Set the number of time steps.
virtual void restoreSolutions()
bool testCheckpointHalfTransient() const
Whether or not this simulation should only run half its transient (useful for testing recovery) ...
Definition: MooseApp.h:523
Base class for transient executioners that use a FixedPointSolve solve object for multiapp-main app i...
Definition: TransientBase.h:27
virtual void forceTimeStep(Real dt)
Definition: TimeStepper.C:220
MooseApp & _app
The MOOSE application this is associated with.
Definition: MooseBase.h:385
bool _converged
Whether or not the previous solve converged.
Definition: TimeStepper.h:144
Real & _end_time
Definition: TimeStepper.h:135
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual void preExecute()
Definition: TimeStepper.C:71
virtual void rejectStep()
This gets called when time step is rejected.
Definition: TimeStepper.C:188
Real & _dt_min
Definition: TimeStepper.h:133
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Definition: MooseBase.h:281
Interface for objects that needs scalar coupling capabilities.
virtual void step()
Take a time step.
Definition: TimeStepper.C:167
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object...
int & _t_step
Definition: TimeStepper.h:131
void addRangeCheckedParam(const std::string &name, const T &value, const std::string &parsed_function, const std::string &doc_string)
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
bool _currently_restepping
If we are currently solving a failed step.
Definition: TimeStepper.h:156
static InputParameters validParams()
Definition: MooseObject.C:25
bool _reset_dt
If true then the next dt will be computed by computeInitialDT()
Definition: TimeStepper.h:150
unsigned int _failure_count
Cumulative amount of steps that have failed.
Definition: TimeStepper.h:159
Real & _dt_max
Definition: TimeStepper.h:134
virtual void init()
Initialize the time stepper.
Definition: TimeStepper.C:66
virtual bool solve()=0
Solve routine provided by this object.
Real & _dt
Definition: TimeStepper.h:132
void declareControllable(const std::string &name, std::set< ExecFlagType > execute_flags={})
Declare the given parameters as controllable.
Real & _time
Values from executioner.
Definition: TimeStepper.h:129