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