https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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. A factor of 1 repeats the "
26 "time step with the same size, so it should be set back below 1 after a repeat.");
27 params.addParam<bool>("enable", true, "whether or not to enable the time stepper");
28 params.declareControllable("enable cutback_factor_at_failure");
29
30 params.registerBase("TimeStepper");
31 params.registerSystemAttributeName("TimeStepper");
32
33 return params;
34}
35
37 : MooseObject(parameters),
38 Restartable(this, "TimeSteppers"),
39 ScalarCoupleable(this),
40 _fe_problem(parameters.have_parameter<FEProblemBase *>("_fe_problem_base")
41 ? *getParam<FEProblemBase *>("_fe_problem_base")
42 : *getParam<FEProblem *>("_fe_problem")),
43 _executioner(*getCheckedPointerParam<TransientBase *>("_executioner")),
44 _time(_fe_problem.time()),
45 _time_old(_fe_problem.timeOld()),
46 _t_step(_fe_problem.timeStep()),
47 _dt(_fe_problem.dt()),
48 _dt_min(_executioner.dtMin()),
49 _dt_max(_executioner.dtMax()),
50 _end_time(_executioner.endTime()),
51 _sync_times(_app.getOutputWarehouse().getSyncTimes()),
52 _timestep_tolerance(_executioner.timestepTol()),
53 _verbose(_executioner.verbose()),
54 _converged(true),
55 _cutback_factor_at_failure(getParam<Real>("cutback_factor_at_failure")),
56 _reset_dt(getParam<bool>("reset_dt")),
57 _has_reset_dt(false),
58 _currently_restepping(false),
59 _failure_count(0),
60 _current_dt(declareRestartableData<Real>("current_dt", 1.0))
61{
62}
63
65
66void
70
71void
73{
74 // Delete all sync times that are at or before the begin time
75 while (!_sync_times.empty() && _time + _timestep_tolerance >= *_sync_times.begin())
76 _sync_times.erase(_sync_times.begin());
77}
78
79void
81{
82 if (_t_step < 2 || (_reset_dt && !_has_reset_dt))
83 {
84 _has_reset_dt = true;
85
86 if (converged())
88 else
90 }
91 else
92 {
93 if (converged())
95 else
97 }
98 if (_current_dt < -TOLERANCE)
99 mooseError("Negative time step detected :" + std::to_string(_current_dt) +
100 " Investigate the TimeStepper to resolve this error");
101}
102
103bool
105{
106 bool at_sync_point = false;
107
108 std::ostringstream diag;
109
110 // Don't let the time step size exceed maximum time step size
111 if (dt > _dt_max)
112 {
113 dt = _dt_max;
114 diag << "Limiting dt to dtmax: " << std::setw(9) << std::setprecision(6) << std::setfill('0')
115 << std::showpoint << std::left << _dt_max << std::endl;
116 }
117
118 // Don't allow time step size to be smaller than minimum time step size
119 if (dt < _dt_min)
120 {
121 dt = _dt_min;
122 diag << "Increasing dt to dtmin: " << std::setw(9) << std::setprecision(6) << std::setfill('0')
123 << std::showpoint << std::left << _dt_min << std::endl;
124 }
125
126 // Don't let time go beyond simulation end time (unless we're doing a half transient)
128 {
129 dt = _end_time - _time;
130 diag << "Limiting dt for end_time: " << std::setw(9) << std::setprecision(6)
131 << std::setfill('0') << std::showpoint << std::left << _end_time << " dt: " << std::setw(9)
132 << std::setprecision(6) << std::setfill('0') << std::showpoint << std::left << dt
133 << std::endl;
134 }
135
136 // Adjust to a sync time if supplied
137 if (!_sync_times.empty() && _time + dt + _timestep_tolerance >= (*_sync_times.begin()))
138 {
139 dt = *_sync_times.begin() - _time;
140 diag << "Limiting dt for sync_time: " << std::setw(9) << std::setprecision(6)
141 << std::setfill('0') << std::showpoint << std::left << *_sync_times.begin()
142 << " dt: " << std::setw(9) << std::setprecision(6) << std::setfill('0') << std::showpoint
143 << std::left << dt << std::endl;
144
145 if (dt <= 0.0)
146 {
147 _console << diag.str() << std::flush;
148 mooseError("Adjusting to sync_time resulted in a non-positive time step. dt: ",
149 dt,
150 " sync_time: ",
151 *_sync_times.begin(),
152 " time: ",
153 _time);
154 }
155
156 at_sync_point = true;
157 }
158
159 if (_verbose)
160 {
161 _console << diag.str() << std::flush;
162 }
163
164 return at_sync_point;
165}
166
167void
175
176void
178{
179 // If there are sync times at or before the current time, delete them
180 while (!_sync_times.empty() && _time + _timestep_tolerance >= *_sync_times.begin())
181 {
182 _sync_times.erase(_sync_times.begin());
183 }
184 // If we accept a step, we are no longer taking a failed step again
185 _currently_restepping = false;
186}
187
188void
195
196unsigned int
198{
199 return _failure_count;
200}
201
202bool
204{
205 return _converged;
206}
207
208Real
210{
211 if (_dt <= _dt_min)
212 mooseError("Solve failed and timestep already at or below dtmin, cannot continue!");
213
214 // cut the time step
217 else // (_cutback_factor_at_failure * _current_dt < _dt_min)
218 return _dt_min;
219}
220
221void
223{
224 _current_dt = dt;
225}
226
227void
228TimeStepper::forceNumSteps(const unsigned int num_steps)
229{
230 _executioner.forceNumSteps(num_steps);
231}
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
void restoreGeometricSearchState()
Restore geometric search state captured by the most recent backupGeometricSearchState().
virtual void restoreSolutions()
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
Definition FEProblem.h:21
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
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...
void declareControllable(const std::string &name, std::set< ExecFlagType > execute_flags={})
Declare the given parameters as controllable.
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.
void registerBase(const std::string &value)
This method must be called from every base "Moose System" to create linkage with the Action System.
void addRangeCheckedParam(const std::string &name, const T &value, const std::string &parsed_function, const std::string &doc_string)
bool testCheckpointHalfTransient() const
Whether or not this simulation should only run half its transient (useful for testing recovery)
Definition MooseApp.h:528
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:271
Every object that can be built by the factory should be derived from this class.
Definition MooseObject.h:31
static InputParameters validParams()
Definition MooseObject.C:25
MooseApp & _app
The MOOSE application this is associated with.
Definition MooseBase.h:375
A class for creating restricted objects.
Definition Restartable.h:29
Interface for objects that needs scalar coupling capabilities.
virtual bool solve()=0
Solve routine provided by this object.
TimeStepper(const InputParameters &parameters)
Definition TimeStepper.C:36
virtual Real computeDT()=0
Computes time step size after the initial time step.
virtual Real computeFailedDT()
Computes time step size after a failed time step.
const bool & _verbose
whether a detailed diagnostic output should be printed
virtual Real computeInitialDT()=0
Computes time step size for the initial time step.
virtual bool converged() const
If the time step converged.
virtual void init()
Initialize the time stepper.
Definition TimeStepper.C:67
virtual ~TimeStepper()
Definition TimeStepper.C:64
unsigned int _failure_count
Cumulative amount of steps that have failed.
Real & _current_dt
Size of the current time step as computed by the Stepper. Note that the actual dt that was taken migh...
FEProblemBase & _fe_problem
Real & _dt_max
virtual void rejectStep()
This gets called when time step is rejected.
unsigned int numFailures() const
Gets the number of failures and returns them.
bool _converged
Whether or not the previous solve converged.
bool _reset_dt
If true then the next dt will be computed by computeInitialDT()
const Real & _cutback_factor_at_failure
Cutback factor if a time step fails to converge.
static InputParameters validParams()
Definition TimeStepper.C:16
virtual void acceptStep()
This gets called when time step is accepted.
virtual void preExecute()
Definition TimeStepper.C:72
bool _currently_restepping
If we are currently solving a failed step.
TransientBase & _executioner
Reference to transient executioner.
Real & _end_time
Real & _timestep_tolerance
Real & _dt_min
std::set< Real > & _sync_times
virtual void forceNumSteps(const unsigned int num_steps)
Set the number of time steps.
virtual void forceTimeStep(Real dt)
bool _has_reset_dt
True if dt has been reset.
virtual void step()
Take a time step.
void computeStep()
Called before a new step is started.
Definition TimeStepper.C:80
virtual bool constrainStep(Real &dt)
Called after computeStep() is called.
Real & _time
Values from executioner.
Base class for transient executioners that use a FixedPointSolve solve object for multiapp-main app i...
virtual SolveObject * timeStepSolveObject()
Return the solve object wrapped by time stepper.
virtual void forceNumSteps(const unsigned int num_steps)
Set the number of time steps.