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 
15 template <>
18 {
20  params.addParam<bool>(
21  "reset_dt", false, "Use when restarting a calculation to force a change in dt.");
22 
23  params.registerBase("TimeStepper");
24 
25  return params;
26 }
27 
29  : MooseObject(parameters),
30  Restartable(this, "TimeSteppers"),
31  ScalarCoupleable(this),
32  _fe_problem(parameters.have_parameter<FEProblemBase *>("_fe_problem_base")
33  ? *getParam<FEProblemBase *>("_fe_problem_base")
34  : *getParam<FEProblem *>("_fe_problem")),
35  _executioner(*getCheckedPointerParam<Transient *>("_executioner")),
36  _time(_fe_problem.time()),
37  _time_old(_fe_problem.timeOld()),
38  _t_step(_fe_problem.timeStep()),
39  _dt(_fe_problem.dt()),
40  _dt_min(_executioner.dtMin()),
41  _dt_max(_executioner.dtMax()),
42  _end_time(_executioner.endTime()),
43  _sync_times(_app.getOutputWarehouse().getSyncTimes()),
44  _timestep_tolerance(_executioner.timestepTol()),
45  _verbose(_executioner.verbose()),
46  _converged(true),
47  _reset_dt(getParam<bool>("reset_dt")),
48  _has_reset_dt(false),
49  _current_dt(declareRestartableData("current_dt", 1.0))
50 {
51 }
52 
54 
55 void
57 {
58 }
59 
60 void
62 {
63  // Delete all sync times that are at or before the begin time
64  while (!_sync_times.empty() && _time + _timestep_tolerance >= *_sync_times.begin())
65  _sync_times.erase(_sync_times.begin());
66 }
67 
68 void
70 {
71  if (_t_step < 2 || (_reset_dt && !_has_reset_dt))
72  {
73  _has_reset_dt = true;
74 
75  if (converged())
77  else
79  }
80  else
81  {
82  if (converged())
84  else
86  }
87 }
88 
89 bool
91 {
92  bool at_sync_point = false;
93 
94  std::ostringstream diag;
95 
96  // Don't let the time step size exceed maximum time step size
97  if (dt > _dt_max)
98  {
99  dt = _dt_max;
100  diag << "Limiting dt to dtmax: " << std::setw(9) << std::setprecision(6) << std::setfill('0')
101  << std::showpoint << std::left << _dt_max << std::endl;
102  }
103 
104  // Don't allow time step size to be smaller than minimum time step size
105  if (dt < _dt_min)
106  {
107  dt = _dt_min;
108  diag << "Increasing dt to dtmin: " << std::setw(9) << std::setprecision(6) << std::setfill('0')
109  << std::showpoint << std::left << _dt_min << std::endl;
110  }
111 
112  // Don't let time go beyond simulation end time (unless we're doing a half transient)
113  if (_time + dt > _end_time && !_app.halfTransient())
114  {
115  dt = _end_time - _time;
116  diag << "Limiting dt for end_time: " << std::setw(9) << std::setprecision(6)
117  << std::setfill('0') << std::showpoint << std::left << _end_time << " dt: " << std::setw(9)
118  << std::setprecision(6) << std::setfill('0') << std::showpoint << std::left << dt
119  << std::endl;
120  }
121 
122  // Adjust to a sync time if supplied
123  if (!_sync_times.empty() && _time + dt + _timestep_tolerance >= (*_sync_times.begin()))
124  {
125  dt = *_sync_times.begin() - _time;
126  diag << "Limiting dt for sync_time: " << std::setw(9) << std::setprecision(6)
127  << std::setfill('0') << std::showpoint << std::left << *_sync_times.begin()
128  << " dt: " << std::setw(9) << std::setprecision(6) << std::setfill('0') << std::showpoint
129  << std::left << dt << std::endl;
130 
131  if (dt <= 0.0)
132  {
133  _console << diag.str();
134  mooseError("Adjusting to sync_time resulted in a non-positive time step. dt: ",
135  dt,
136  " sync_time: ",
137  *_sync_times.begin(),
138  " time: ",
139  _time);
140  }
141 
142  at_sync_point = true;
143  }
144 
145  if (_verbose)
146  {
147  _console << diag.str();
148  }
149 
150  return at_sync_point;
151 }
152 
153 void
155 {
157 }
158 
159 void
161 {
162  // If there are sync times at or before the current time, delete them
163  while (!_sync_times.empty() && _time + _timestep_tolerance >= *_sync_times.begin())
164  {
165  _sync_times.erase(_sync_times.begin());
166  }
167 }
168 
169 void
171 {
173 }
174 
175 bool
177 {
178  return _converged;
179 }
180 
181 Real
183 {
184  if (_dt <= _dt_min)
185  mooseError("Solve failed and timestep already at or below dtmin, cannot continue!");
186 
187  // cut the time step in a half
188  if (0.5 * _dt >= _dt_min)
189  return 0.5 * _dt;
190  else // (0.5 * _current_dt < _dt_min)
191  return _dt_min;
192 }
193 
194 void
196 {
197  _current_dt = dt;
198 }
InputParameters validParams< TimeStepper >()
Definition: TimeStepper.C:17
Real & _timestep_tolerance
Definition: TimeStepper.h:133
virtual ~TimeStepper()
Definition: TimeStepper.C:53
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:182
Transient executioners usually loop through a number of timesteps...
Definition: Transient.h:30
A class for creating restricted objects.
Definition: Restartable.h:29
PicardSolve & picardSolve()
Return underlining PicardSolve object.
Definition: Executioner.h:111
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
Definition: FEProblem.h:24
void computeStep()
Called before a new step is started.
Definition: TimeStepper.C:69
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
Transient & _executioner
Reference to transient executioner.
Definition: TimeStepper.h:121
virtual bool constrainStep(Real &dt)
Called after computeStep() is called.
Definition: TimeStepper.C:90
bool halfTransient()
Whether or not this simulation should only run half its transient (useful for testing recovery) ...
Definition: MooseApp.h:390
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
virtual bool converged() const
If the time step converged.
Definition: TimeStepper.C:176
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:149
std::set< Real > & _sync_times
Definition: TimeStepper.h:131
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...
FEProblemBase & _fe_problem
Definition: TimeStepper.h:119
virtual void acceptStep()
This gets called when time step is accepted.
Definition: TimeStepper.C:160
virtual bool solve() override
Picard solve the FEProblem.
Definition: PicardSolve.C:114
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:42
virtual Real computeDT()=0
Called to compute _current_dt for a normal step.
TimeStepper(const InputParameters &parameters)
Definition: TimeStepper.C:28
bool _has_reset_dt
True if dt has been reset.
Definition: TimeStepper.h:145
virtual void restoreSolutions()
virtual void forceTimeStep(Real dt)
Definition: TimeStepper.C:195
bool _converged
Whether or not the previous solve converged.
Definition: TimeStepper.h:139
InputParameters validParams< MooseObject >()
Definition: MooseObject.C:25
Real & _end_time
Definition: TimeStepper.h:130
MooseApp & _app
The MooseApp this object is associated with.
Definition: MooseObject.h:177
virtual void preExecute()
Definition: TimeStepper.C:61
virtual void rejectStep()
This gets called when time step is rejected.
Definition: TimeStepper.C:170
Real & _dt_min
Definition: TimeStepper.h:128
Interface for objects that needs scalar coupling capabilities.
virtual void step()
Take a time step.
Definition: TimeStepper.C:154
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:126
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
bool _reset_dt
If true then the next dt will be computed by computeInitialDT()
Definition: TimeStepper.h:142
Real & _dt_max
Definition: TimeStepper.h:129
virtual void init()
Initialize the time stepper.
Definition: TimeStepper.C:56
bool & _verbose
should detailed diagnostic output be printed
Definition: TimeStepper.h:136
Real & _dt
Definition: TimeStepper.h:127
Real & _time
Values from executioner.
Definition: TimeStepper.h:124