www.mooseframework.org
FunctionDT.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 "FunctionDT.h"
11 #include "Function.h"
12 #include "PiecewiseLinear.h"
13 #include "PiecewiseConstant.h"
14 #include <limits>
15 
16 registerMooseObject("MooseApp", FunctionDT);
17 
18 template <>
21 {
23  // TODO: This will be required when time_t and time_dt is removed
24  params.addParam<FunctionName>(
25  "function", "The name of the time-dependent function that prescribes the time step size.");
26  params.addParam<std::vector<Real>>("time_t", "The values of t");
27  params.addParam<std::vector<Real>>("time_dt", "The values of dt");
28  params.addParam<Real>("growth_factor",
29  std::numeric_limits<Real>::max(),
30  "Maximum ratio of new to previous timestep sizes following a step that "
31  "required the time step to be cut due to a failed solve.");
32  params.addParam<Real>("min_dt", 0, "The minimal dt to take.");
33  // TODO: this can be removed when time_t and time_dt is removed
34  params.addParam<bool>("interpolate",
35  true,
36  "Whether or not to interpolate DT between times. "
37  "This is true by default for historical reasons.");
38  params.addClassDescription(
39  "Timestepper whose steps vary over time according to a user-defined fuction");
40 
41  return params;
42 }
43 
45  : TimeStepper(parameters),
46  FunctionInterface(this),
47  _time_t(getParam<std::vector<Real>>("time_t")),
48  _time_dt(getParam<std::vector<Real>>("time_dt")),
49  _function(nullptr),
50  _growth_factor(getParam<Real>("growth_factor")),
51  _cutback_occurred(false),
52  _min_dt(getParam<Real>("min_dt")),
53  _interpolate(getParam<bool>("interpolate"))
54 {
55  // TODO: remove this when `time_t` and `time_dt` is removed
56  if ((isParamValid("time_t") && isParamValid("time_dt")) && !isParamValid("function"))
58  ": Using `time_t` and `time_dt` parameter is deprecated. Switch your input "
59  "file to using `function` parameter.\n",
60  " 1. Build a new function. If `interpolate` parameter is true use type = "
61  "PiecewiseLinear. If it was false, use PiecewiseConstant.\n",
62  " 2. Copy `time_t` parameter into your function and rename it to `x`.\n",
63  " 3. Copy `time_dt` parameter into your function and rename it to `y`.\n",
64  " 4. Use the `function` parameter in your time stepper and pass your new "
65  "function name into it.\n");
66  else if ((isParamValid("time_t") && isParamValid("time_dt")) && isParamValid("function"))
67  mooseError(name(),
68  ": Using `time_t`, `_time_dt` and `function` at the same time. Use only `function`, "
69  "`time_t` and _time_dt is deprecated.");
70  else if (!isParamValid("function"))
71  mooseError(name(),
72  ": Please, specify a function (using the `function` parameter) that will prescribe "
73  "the time step size.");
74 
75  if (isParamValid("time_t") && isParamValid("time_dt"))
76  {
77  try
78  {
79  _time_ipol = libmesh_make_unique<LinearInterpolation>(_time_t, _time_dt);
80  }
81  catch (std::domain_error & e)
82  {
83  mooseError("In FunctionDT ", _name, ": ", e.what());
84  }
85 
87  _use_function = false;
88  }
89  else
90  {
91  _function = &getFunction("function");
92  // If dt is given by piece-wise linear and constant function, we add the domain into
93  // _time_knots, so that the time stepper hits those time points
94  if (dynamic_cast<PiecewiseLinear *>(_function) || dynamic_cast<PiecewiseConstant *>(_function))
95  {
96  Piecewise * pw = dynamic_cast<Piecewise *>(_function);
97  unsigned int n_knots = pw->functionSize();
98  for (unsigned int i = 0; i < n_knots; i++)
99  _time_knots.push_back(pw->domain(i));
100  }
101  _use_function = true;
102  }
103 }
104 
105 void
107 {
108  removeOldKnots();
109 }
110 
111 void
113 {
114  while ((_time_knots.size() > 0) &&
115  (*_time_knots.begin() <= _time || std::abs(*_time_knots.begin() - _time) < 1e-10))
116  _time_knots.erase(_time_knots.begin());
117 }
118 
119 Real
121 {
122  return computeDT();
123 }
124 
125 Real
127 {
128  Real local_dt = 0;
129 
130  if (_use_function)
131  local_dt = _function->value(_time, _point_zero);
132  else
133  {
134  if (_interpolate)
135  local_dt = _time_ipol->sample(_time);
136  else // Find where we are
137  {
138  unsigned int i = 0;
140  {
141  i = _time_t.size();
142  }
143  else
144  {
145  for (; i < _time_t.size() - 1; i++)
147  break;
148  }
149 
150  // Use the last dt after the end
151  if (i == _time_t.size())
152  local_dt = _time_dt.back();
153  else
154  local_dt = _time_dt[i];
155  }
156  }
157 
158  // sync to time knot
159  if ((_time_knots.size() > 0) && (_time + local_dt >= (*_time_knots.begin())))
160  local_dt = (*_time_knots.begin()) - _time;
161  // honor minimal dt
162  if (local_dt < _min_dt)
163  local_dt = _min_dt;
164 
165  if (_cutback_occurred && (local_dt > _dt * _growth_factor))
166  local_dt = _dt * _growth_factor;
167  _cutback_occurred = false;
168 
169  return local_dt;
170 }
171 
172 void
174 {
175  removeOldKnots();
176 }
177 
178 void
180 {
181  _cutback_occurred = true;
183 }
registerMooseObject("MooseApp", FunctionDT)
virtual Real value(Real t, const Point &p)
Override this to evaluate the scalar function at point (t,x,y,z), by default this returns zero...
Definition: Function.C:38
MetaPhysicL::DualNumber< T, D > abs(const MetaPhysicL::DualNumber< T, D > &in)
std::unique_ptr< LinearInterpolation > _time_ipol
Piecewise linear definition of time stepping.
Definition: FunctionDT.h:48
bool _use_function
true, if we are using _function, false if we are using _time_ipol
Definition: FunctionDT.h:42
const Point & _point_zero
Zero point.
std::vector< Real > _time_knots
Definition: FunctionDT.h:58
Function & getFunction(const std::string &name)
Get a function with a given name.
Base class for time stepping.
Definition: TimeStepper.h:26
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const std::vector< Real > & _time_t
Definition: FunctionDT.h:38
bool _interpolate
Whether or not to interpolate DT between times.
Definition: FunctionDT.h:56
virtual void postStep() override
Definition: FunctionDT.C:173
virtual Real domain(const int i)
Definition: PiecewiseBase.C:70
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
bool relativeFuzzyGreaterEqual(const T &var1, const T2 &var2, const T3 &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether a variable is greater than or equal to another variable within a relative t...
Definition: MooseUtils.h:369
InputParameters validParams< FunctionDT >()
Definition: FunctionDT.C:20
virtual Real functionSize()
Definition: PiecewiseBase.C:64
void removeOldKnots()
Definition: FunctionDT.C:112
virtual void init() override
Initialize the time stepper.
Definition: FunctionDT.C:106
virtual Real computeDT() override
Called to compute _current_dt for a normal step.
Definition: FunctionDT.C:126
InputParameters validParams< TimeStepper >()
Definition: TimeStepper.C:17
const std::string & _name
The name of this object, reference to value stored in InputParameters.
Definition: MooseObject.h:183
bool _cutback_occurred
True if cut back of the time step occurred.
Definition: FunctionDT.h:52
Function * _function
The time-dependent function specifying the time step size (turn this into a reference then time_t and...
Definition: FunctionDT.h:45
void mooseDeprecated(Args &&... args) const
Definition: MooseObject.h:161
virtual void rejectStep()
This gets called when time step is rejected.
Definition: TimeStepper.C:170
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:59
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...
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...
virtual Real computeInitialDT() override
Called to compute _current_dt for the first timestep.
Definition: FunctionDT.C:120
virtual void rejectStep() override
This gets called when time step is rejected.
Definition: FunctionDT.C:179
Real _min_dt
Definition: FunctionDT.h:53
Real _growth_factor
Definition: FunctionDT.h:50
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseObject.h:89
bool relativeFuzzyLessThan(const T &var1, const T2 &var2, const T3 &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether a variable is less than another variable within a relative tolerance...
Definition: MooseUtils.h:442
Real & _dt
Definition: TimeStepper.h:127
FunctionDT(const InputParameters &parameters)
Definition: FunctionDT.C:44
Function which provides a piecewise approximation to a provided (x,y) point data set generated from f...
Definition: Piecewise.h:25
Interface for objects that need to use functions.
const std::vector< Real > & _time_dt
Definition: FunctionDT.h:39
Real & _time
Values from executioner.
Definition: TimeStepper.h:124