www.mooseframework.org
TimeSequenceStepperBase.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 
11 #include "FEProblem.h"
12 #include "Transient.h"
13 
14 template <>
17 {
19  return params;
20 }
21 
23  : TimeStepper(parameters),
24  _current_step(declareRestartableData("current_step", (unsigned int)0)),
25  _time_sequence(declareRestartableData<std::vector<Real>>("time_sequence"))
26 {
27 }
28 
29 void
30 TimeSequenceStepperBase::setupSequence(const std::vector<Real> & times)
31 {
32  // In case of half transient, transient's end time needs to be reset to
33  // be able to imprint TimeSequenceStepperBase's end time
34  if (_app.halfTransient())
35  _executioner.endTime() *= 2.0;
36 
37  // only set up _time_sequence if the app is _not_ recovering
38  if (!_app.isRecovering())
39  {
40  // also we need to do something different when restarting
41  if (!_app.isRestarting())
42  {
43  // sync _executioner.startTime and endTime with _time_sequence
44  Real start_time = _executioner.getStartTime();
45  Real end_time = _executioner.endTime();
46 
47  // make sure time sequence is in ascending order
48  for (unsigned int j = 0; j < times.size() - 1; ++j)
49  if (times[j + 1] <= times[j])
50  mooseError("time_sequence must be in ascending order.");
51 
52  _time_sequence.push_back(start_time);
53  for (unsigned int j = 0; j < times.size(); ++j)
54  {
55  if (times[j] > start_time && times[j] < end_time)
56  _time_sequence.push_back(times[j]);
57  }
58  _time_sequence.push_back(end_time);
59  }
60  else
61  {
62  // in case of restart it should be allowed to modify _time_sequence if it follows the
63  // following rule:
64  // all times up to _current_step are identical
65  // 1. start time cannot be modified
66  // 2. the entries in _time_sequence and times must be equal up to entry with index
67  // _current_step
68 
70  mooseError("Timesequencestepper does not allow the start time to be modified.");
71 
72  // sync _executioner.endTime with _time_sequence
73  Real end_time = _executioner.endTime();
74 
75  // make sure time sequence is in ascending order
76  for (unsigned int j = 0; j < times.size() - 1; ++j)
77  if (times[j + 1] <= times[j])
78  mooseError("time_sequence must be in ascending order.");
79 
80  // save the restarted time_sequence
81  std::vector<Real> saved_time_sequence = _time_sequence;
82  _time_sequence.clear();
83 
84  // step 1: fill in the entries up to _current_step
85  for (unsigned int j = 0; j <= _current_step; ++j)
86  {
87  if (!MooseUtils::absoluteFuzzyEqual(times[j], saved_time_sequence[j]))
88  mooseError("The timesequence provided in the restart file must be identical to "
89  "the one in the old file up to entry number ",
90  _current_step + 1,
91  " = ",
92  saved_time_sequence[_current_step]);
93 
94  _time_sequence.push_back(saved_time_sequence[j]);
95  }
96 
97  // step 2: fill in the entries up after _current_step
98  for (unsigned int j = _current_step + 1; j < times.size(); ++j)
99  {
100  if (times[j] < end_time)
101  _time_sequence.push_back(times[j]);
102  }
103  _time_sequence.push_back(end_time);
104  }
105  }
106 
107  if (_app.halfTransient())
108  {
109  unsigned int half = (_time_sequence.size() - 1) / 2;
111  }
112 }
113 
114 void
116 {
118  if (converged())
119  _current_step++;
120 }
121 
122 Real
124 {
125  return computeDT();
126 }
127 
128 Real
130 {
132 }
133 
134 Real
136 {
137  if (computeDT() <= _dt_min)
138  mooseError("Solve failed and timestep already at or below dtmin, cannot continue!");
139 
140  // cut the time step in a half if possible
141  Real dt = 0.5 * computeDT();
142  if (dt < _dt_min)
143  dt = _dt_min;
144  _time_sequence.insert(_time_sequence.begin() + _current_step + 1,
146  return computeDT();
147 }
bool absoluteFuzzyEqual(const T &var1, const T2 &var2, const T3 &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether two variables are equal within an absolute tolerance.
Definition: MooseUtils.h:231
virtual Real computeFailedDT() override
Called to compute _current_dt after a solve has failed.
unsigned int & _current_step
the step that the time stepper is currently at
Base class for time stepping.
Definition: TimeStepper.h:26
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
TimeSequenceStepperBase(const InputParameters &parameters)
Transient & _executioner
Reference to transient executioner.
Definition: TimeStepper.h:121
bool halfTransient()
Whether or not this simulation should only run half its transient (useful for testing recovery) ...
Definition: MooseApp.h:390
void setupSequence(const std::vector< Real > &times)
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
virtual bool converged() const
If the time step converged.
Definition: TimeStepper.C:176
bool isRestarting() const
Whether or not this is a "restart" calculation.
Definition: MooseApp.C:865
Real getStartTime()
Return the start time.
Definition: Transient.h:160
std::vector< Real > & _time_sequence
stores the sequence of time points
virtual Real computeDT() override
Called to compute _current_dt for a normal step.
InputParameters validParams< TimeStepper >()
Definition: TimeStepper.C:17
Real & endTime()
Get the end time.
Definition: Transient.h:166
virtual Real computeInitialDT() override
Called to compute _current_dt for the first timestep.
MooseApp & _app
The MooseApp this object is associated with.
Definition: MooseObject.h:177
Real & _dt_min
Definition: TimeStepper.h:128
virtual void step() override
Take a time step.
InputParameters validParams< TimeSequenceStepperBase >()
virtual void step()
Take a time step.
Definition: TimeStepper.C:154
bool isRecovering() const
Whether or not this is a "recover" calculation.
Definition: MooseApp.C:859