www.mooseframework.org
TimeStepper.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 "Transient.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<Transient *>("_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  _failure_count(0),
58  _current_dt(declareRestartableData<Real>("current_dt", 1.0))
59 {
60 }
61 
63 
64 void
66 {
67 }
68 
69 void
71 {
72  // Delete all sync times that are at or before the begin time
73  while (!_sync_times.empty() && _time + _timestep_tolerance >= *_sync_times.begin())
74  _sync_times.erase(_sync_times.begin());
75 }
76 
77 void
79 {
80  if (_t_step < 2 || (_reset_dt && !_has_reset_dt))
81  {
82  _has_reset_dt = true;
83 
84  if (converged())
86  else
88  }
89  else
90  {
91  if (converged())
93  else
95  }
96  if (_current_dt < -TOLERANCE)
97  mooseError("Negative time step detected :" + std::to_string(_current_dt) +
98  " Investigate the TimeStepper to resolve this error");
99 }
100 
101 bool
103 {
104  bool at_sync_point = false;
105 
106  std::ostringstream diag;
107 
108  // Don't let the time step size exceed maximum time step size
109  if (dt > _dt_max)
110  {
111  dt = _dt_max;
112  diag << "Limiting dt to dtmax: " << std::setw(9) << std::setprecision(6) << std::setfill('0')
113  << std::showpoint << std::left << _dt_max << std::endl;
114  }
115 
116  // Don't allow time step size to be smaller than minimum time step size
117  if (dt < _dt_min)
118  {
119  dt = _dt_min;
120  diag << "Increasing dt to dtmin: " << std::setw(9) << std::setprecision(6) << std::setfill('0')
121  << std::showpoint << std::left << _dt_min << std::endl;
122  }
123 
124  // Don't let time go beyond simulation end time (unless we're doing a half transient)
126  {
127  dt = _end_time - _time;
128  diag << "Limiting dt for end_time: " << std::setw(9) << std::setprecision(6)
129  << std::setfill('0') << std::showpoint << std::left << _end_time << " dt: " << std::setw(9)
130  << std::setprecision(6) << std::setfill('0') << std::showpoint << std::left << dt
131  << std::endl;
132  }
133 
134  // Adjust to a sync time if supplied
135  if (!_sync_times.empty() && _time + dt + _timestep_tolerance >= (*_sync_times.begin()))
136  {
137  dt = *_sync_times.begin() - _time;
138  diag << "Limiting dt for sync_time: " << std::setw(9) << std::setprecision(6)
139  << std::setfill('0') << std::showpoint << std::left << *_sync_times.begin()
140  << " dt: " << std::setw(9) << std::setprecision(6) << std::setfill('0') << std::showpoint
141  << std::left << dt << std::endl;
142 
143  if (dt <= 0.0)
144  {
145  _console << diag.str();
146  mooseError("Adjusting to sync_time resulted in a non-positive time step. dt: ",
147  dt,
148  " sync_time: ",
149  *_sync_times.begin(),
150  " time: ",
151  _time);
152  }
153 
154  at_sync_point = true;
155  }
156 
157  if (_verbose)
158  {
159  _console << diag.str();
160  }
161 
162  return at_sync_point;
163 }
164 
165 void
167 {
169 
170  if (!_converged)
171  _failure_count++;
172 }
173 
174 void
176 {
177  // If there are sync times at or before the current time, delete them
178  while (!_sync_times.empty() && _time + _timestep_tolerance >= *_sync_times.begin())
179  {
180  _sync_times.erase(_sync_times.begin());
181  }
182 }
183 
184 void
186 {
188 }
189 
190 unsigned int
192 {
193  return _failure_count;
194 }
195 
196 bool
198 {
199  return _converged;
200 }
201 
202 Real
204 {
205  if (_dt <= _dt_min)
206  mooseError("Solve failed and timestep already at or below dtmin, cannot continue!");
207 
208  // cut the time step
211  else // (_cutback_factor_at_failure * _current_dt < _dt_min)
212  return _dt_min;
213 }
214 
215 void
217 {
218  _current_dt = dt;
219 }
220 
221 void
222 TimeStepper::forceNumSteps(const unsigned int num_steps)
223 {
224  _executioner.forceNumSteps(num_steps);
225 }
static InputParameters validParams()
Definition: TimeStepper.C:16
Real & _timestep_tolerance
Definition: TimeStepper.h:140
virtual ~TimeStepper()
Definition: TimeStepper.C:62
virtual Real computeInitialDT()=0
Called to compute _current_dt for the first timestep.
virtual Real computeFailedDT()
Called to compute _current_dt after a solve has failed.
Definition: TimeStepper.C:203
Transient executioners usually loop through a number of timesteps...
Definition: Transient.h:26
virtual void forceNumSteps(const unsigned int num_steps)
Set the number of time steps.
Definition: TimeStepper.C:222
A class for creating restricted objects.
Definition: Restartable.h:28
virtual void forceNumSteps(const unsigned int num_steps)
Set the number of time steps.
Definition: Transient.h:220
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
Definition: FEProblem.h:20
virtual SolveObject * timeStepSolveObject()
Return the solve object wrapped by time stepper.
Definition: Transient.h:223
void computeStep()
Called before a new step is started.
Definition: TimeStepper.C:78
unsigned int numFailures() const
Gets the number of failures and returns them.
Definition: TimeStepper.C:191
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...
Transient & _executioner
Reference to transient executioner.
Definition: TimeStepper.h:128
virtual bool constrainStep(Real &dt)
Called after computeStep() is called.
Definition: TimeStepper.C:102
virtual bool converged() const
If the time step converged.
Definition: TimeStepper.C:197
const Real _cutback_factor_at_failure
Cutback factor if a time step fails to converge.
Definition: TimeStepper.h:149
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:162
std::set< Real > & _sync_times
Definition: TimeStepper.h:138
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:143
FEProblemBase & _fe_problem
Definition: TimeStepper.h:126
virtual void acceptStep()
This gets called when time step is accepted.
Definition: TimeStepper.C:175
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:33
virtual Real computeDT()=0
Called to compute _current_dt for a normal step.
TimeStepper(const InputParameters &parameters)
Definition: TimeStepper.C:35
bool _has_reset_dt
True if dt has been reset.
Definition: TimeStepper.h:155
virtual void restoreSolutions()
bool testCheckpointHalfTransient() const
Whether or not this simulation should only run half its transient (useful for testing recovery) ...
Definition: MooseApp.h:522
virtual void forceTimeStep(Real dt)
Definition: TimeStepper.C:216
MooseApp & _app
The MOOSE application this is associated with.
Definition: MooseBase.h:69
bool _converged
Whether or not the previous solve converged.
Definition: TimeStepper.h:146
Real & _end_time
Definition: TimeStepper.h:137
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual void preExecute()
Definition: TimeStepper.C:70
virtual void rejectStep()
This gets called when time step is rejected.
Definition: TimeStepper.C:185
Real & _dt_min
Definition: TimeStepper.h:135
Interface for objects that needs scalar coupling capabilities.
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
virtual void step()
Take a time step.
Definition: TimeStepper.C:166
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an option parameter and a documentation string to the InputParameters object...
int & _t_step
Definition: TimeStepper.h:133
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.
static InputParameters validParams()
Definition: MooseObject.C:24
bool _reset_dt
If true then the next dt will be computed by computeInitialDT()
Definition: TimeStepper.h:152
unsigned int _failure_count
Cumulative amount of steps that have failed.
Definition: TimeStepper.h:158
Real & _dt_max
Definition: TimeStepper.h:136
virtual void init()
Initialize the time stepper.
Definition: TimeStepper.C:65
virtual bool solve()=0
Solve routine provided by this object.
Real & _dt
Definition: TimeStepper.h:134
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:131