https://mooseframework.inl.gov
CompositionDT.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 "CompositionDT.h"
11 #include "MooseApp.h"
12 #include "Transient.h"
14 #include "IterationAdaptiveDT.h"
15 #include "FEProblemBase.h"
16 
17 #include <limits>
18 
20 
23 {
24  auto params = emptyInputParameters();
25 
26  params.addParam<Real>("initial_dt", "Initial value of dt");
27  params.addParam<std::vector<std::string>>(
28  "lower_bound",
29  {},
30  "The maximum of these TimeSteppers will form the lower bound on the time "
31  "step size. A single or multiple time steppers may be specified.");
32 
33  return params;
34 }
35 
38 {
41 
42  params.addClassDescription("The time stepper takes all the other time steppers as input and "
43  "returns the minimum time step size.");
44 
45  return params;
46 }
47 
49  : TimeStepper(parameters),
50  _has_initial_dt(isParamValid("initial_dt")),
51  _initial_dt(_has_initial_dt ? getParam<Real>("initial_dt") : 0.),
52  _lower_bound(getParam<std::vector<std::string>>("lower_bound").begin(),
53  getParam<std::vector<std::string>>("lower_bound").end()),
54  _current_time_stepper(nullptr),
55  _largest_bound_time_stepper(nullptr),
56  _closest_time_sequence_stepper(nullptr)
57 {
58  // Make sure the steppers in "lower_bound" exist
59  const auto time_steppers = getTimeSteppers();
60  for (const auto & time_stepper_name : _lower_bound)
61  if (std::find_if(time_steppers.begin(),
62  time_steppers.end(),
63  [&time_stepper_name](const auto & ts)
64  { return ts->name() == time_stepper_name; }) == time_steppers.end() &&
65  _lower_bound.size() != 0)
66  paramError(
67  "lower_bound", "Failed to find a timestepper with the name '", time_stepper_name, "'");
68 }
69 
70 template <typename Lambda>
71 void
73 {
74  for (auto & ts : getTimeSteppers())
75  act(*ts);
76 }
77 
78 void
80 {
81  actOnTimeSteppers([](auto & ts) { ts.init(); });
82 }
83 
84 void
86 {
87  actOnTimeSteppers([](auto & ts) { ts.preExecute(); });
88 }
89 
90 void
92 {
93  actOnTimeSteppers([](auto & ts) { ts.preSolve(); });
94 }
95 
96 void
98 {
99  actOnTimeSteppers([](auto & ts) { ts.postSolve(); });
100 }
101 
102 void
104 {
105  actOnTimeSteppers([](auto & ts) { ts.postExecute(); });
106 }
107 
108 void
110 {
111  actOnTimeSteppers([](auto & ts) { ts.preStep(); });
112 }
113 
114 void
116 {
117  actOnTimeSteppers([](auto & ts) { ts.postStep(); });
118 }
119 
120 bool
122 {
123  bool at_sync_point = TimeStepper::constrainStep(dt);
124  const auto time_steppers = getTimeSteppers();
125  for (auto & ts : time_steppers)
126  if (ts->constrainStep(dt))
127  return true;
128  return at_sync_point;
129 }
130 
131 Real
133 {
135 }
136 
137 Real
139 {
140  const auto time_steppers = getTimeSteppers();
141  // Note : compositionDT requires other active time steppers as input so no active time steppers
142  // or only compositionDT is active is not allowed
143  if (time_steppers.size() < 1)
144  mooseError("No TimeStepper(s) are currently active to compute a timestep");
145 
146  std::set<std::pair<Real, TimeStepper *>, CompareFirst> dts, bound_dt;
147 
148  for (auto & ts : time_steppers)
149  if (!dynamic_cast<TimeSequenceStepperBase *>(ts))
150  {
151  ts->computeStep();
152  const auto dt = ts->getCurrentDT();
153 
154  if (_lower_bound.count(ts->name()))
155  bound_dt.emplace(dt, ts);
156  else
157  dts.emplace(dt, ts);
158  }
159 
160  _current_time_stepper = dts.size() ? dts.begin()->second : nullptr;
161  _largest_bound_time_stepper = bound_dt.size() ? (--bound_dt.end())->second : nullptr;
162 
163  _dt = produceCompositionDT(dts, bound_dt);
164 
165  return _dt;
166 }
167 
168 Real
170 {
171  const auto time_steppers = getTimeSteppers();
172 
173  std::vector<TimeSequenceStepperBase *> time_sequence_steppers;
174  for (auto & ts : time_steppers)
175  if (auto tss = dynamic_cast<TimeSequenceStepperBase *>(ts))
176  time_sequence_steppers.push_back(tss);
177 
178  if (time_sequence_steppers.empty())
179  return 0;
180 
181  Real next_time_to_hit = std::numeric_limits<Real>::max();
182  for (auto & tss : time_sequence_steppers)
183  {
184  Real ts_time_to_hit = tss->getNextTimeInSequence();
185  if (ts_time_to_hit - _time <= _dt_min)
186  {
187  tss->increaseCurrentStep();
188  ts_time_to_hit = tss->getNextTimeInSequence();
189  }
190  if (next_time_to_hit > ts_time_to_hit)
191  {
193  next_time_to_hit = ts_time_to_hit;
194  }
195  }
196  return next_time_to_hit;
197 }
198 
199 Real
201  std::set<std::pair<Real, TimeStepper *>, CompareFirst> & dts,
202  std::set<std::pair<Real, TimeStepper *>, CompareFirst> & bound_dts)
203 {
204  Real minDT, lower_bound, dt;
205  minDT = lower_bound = dt = 0.0;
206  if (!dts.empty())
207  minDT = dts.begin()->first;
208  if (!bound_dts.empty())
209  lower_bound = bound_dts.rbegin()->first;
210 
211  if (minDT > lower_bound)
212  dt = minDT;
213  else
214  {
215  dt = lower_bound;
217  }
218 
219  auto ts = getSequenceSteppersNextTime();
220 
221  if (ts != 0 && (ts - _time) < dt)
222  {
224  return std::min((ts - _time), dt);
225  }
226  else
227  return dt;
228 }
229 
230 std::vector<TimeStepper *>
232 {
233  std::vector<TimeStepper *> time_steppers;
235  .query()
236  .condition<AttribSystem>("TimeStepper")
237  .queryInto(time_steppers);
238 
239  // Remove CompositionDT from time_steppers vector to avoid recursive call
240  time_steppers.erase(std::remove(time_steppers.begin(), time_steppers.end(), this),
241  time_steppers.end());
242  return time_steppers;
243 }
244 
245 void
247 {
249  {
251  if (!converged())
252  _failure_count++;
253  }
254  else
256 }
257 
258 void
260 {
261  actOnTimeSteppers([](auto & ts) { ts.acceptStep(); });
262 }
263 
264 void
266 {
267  actOnTimeSteppers([](auto & ts) { ts.rejectStep(); });
268 }
269 
270 bool
272 {
274 }
TimeStepper * _largest_bound_time_stepper
static InputParameters compositionDTParams()
Definition: CompositionDT.C:22
static InputParameters validParams()
Definition: TimeStepper.C:16
virtual void preStep() override final
virtual void rejectStep() override final
This gets called when time step is rejected for all input time steppers.
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
Definition: MooseBase.h:435
TimeStepper * _current_time_stepper
const bool _has_initial_dt
TimeSequenceStepperBase * _closest_time_sequence_stepper
virtual void postStep() override final
virtual void preSolve() override final
Definition: CompositionDT.C:91
registerMooseObject("MooseApp", CompositionDT)
void actOnTimeSteppers(Lambda &&act)
Definition: CompositionDT.C:72
Base class for time stepping.
Definition: TimeStepper.h:22
virtual void step() override final
Functions called after the current DT is computed.
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:102
virtual bool converged() const
If the time step converged.
Definition: TimeStepper.C:197
InputParameters emptyInputParameters()
auto max(const L &left, const R &right)
static InputParameters validParams()
Definition: CompositionDT.C:37
Comparator for sorting by the value of dt for the TimeStepper sets which stored the pairs of the dt a...
Definition: CompositionDT.h:45
FEProblemBase & _fe_problem
Definition: TimeStepper.h:120
A TimeStepper that takes time steppers as inputs and computes the minimum time step size among all ti...
Definition: CompositionDT.h:27
virtual bool converged() const override final
The _current_time_stepper is used to check whether convergence was reached on the time step...
Real produceCompositionDT(std::set< std::pair< Real, TimeStepper *>, CompareFirst > &dts, std::set< std::pair< Real, TimeStepper *>, CompareFirst > &bound_dts)
Find the composed time step size by selecting the minimum value and compare it with the lower bound i...
TheWarehouse & theWarehouse() const
virtual void preExecute() override final
Definition: CompositionDT.C:85
virtual void acceptStep() override final
This gets called when time step is accepted for all input time steppers.
const std::set< std::string > _lower_bound
std::vector< TimeStepper * > getTimeSteppers()
Internal method for querying TheWarehouse for the currently active timesteppers.
CompositionDT(const InputParameters &parameters)
Definition: CompositionDT.C:48
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Query query()
query creates and returns an initialized a query object for querying objects from the warehouse...
Definition: TheWarehouse.h:466
Real & _dt_min
Definition: TimeStepper.h:129
Real getSequenceSteppersNextTime()
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:267
virtual void step()
Take a time step.
Definition: TimeStepper.C:166
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
virtual Real computeDT() override final
Computes time step size after the initial time step.
QueryCache & condition(Args &&... args)
Adds a new condition to the query.
Definition: TheWarehouse.h:284
virtual void postExecute() override final
virtual void init() override final
Initialize all the input time stepper(s).
Definition: CompositionDT.C:79
virtual Real computeInitialDT() override final
Computes time step size for the initial time step.
unsigned int _failure_count
Cumulative amount of steps that have failed.
Definition: TimeStepper.h:152
auto min(const L &left, const R &right)
Real & _time
Values from executioner.
Definition: TimeStepper.h:125
virtual bool constrainStep(Real &dt) override final
Called after computeStep() is called.
const Real _initial_dt
virtual void postSolve() override final
Definition: CompositionDT.C:97