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