https://mooseframework.inl.gov
Loading...
Searching...
No Matches
FunctionDT.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 "FunctionDT.h"
11#include "Function.h"
12#include "PiecewiseBase.h"
13#include <limits>
14
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",
27 std::numeric_limits<Real>::max(),
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.");
36 "Timestepper whose steps vary over time according to a user-defined function");
37
38 return params;
39}
40
42 : TimeStepper(parameters),
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"))
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"))
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
102void
107
108void
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
116Real
118{
119 return computeDT();
120}
121
122Real
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;
136 if (MooseUtils::relativeFuzzyGreaterEqual(_time, _time_t.back()))
137 {
138 i = _time_t.size();
139 }
140 else
141 {
142 for (; i < _time_t.size() - 1; i++)
143 if (MooseUtils::relativeFuzzyLessThan(_time, _time_t[i + 1]))
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
168void
registerMooseObject("MooseApp", FunctionDT)
FunctionDT(const InputParameters &parameters)
Definition FunctionDT.C:41
std::unique_ptr< LinearInterpolation > _time_ipol
Piecewise linear definition of time stepping.
Definition FunctionDT.h:45
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
bool _use_function
true, if we are using _function, false if we are using _time_ipol
Definition FunctionDT.h:39
const std::vector< Real > & _time_t
Definition FunctionDT.h:35
virtual Real computeDT() override
Computes time step size after the initial time step.
Definition FunctionDT.C:123
const std::vector< Real > & _time_dt
Definition FunctionDT.h:36
virtual Real computeInitialDT() override
Computes time step size for the initial time step.
Definition FunctionDT.C:117
virtual void postStep() override
Definition FunctionDT.C:169
bool _interpolate
Whether or not to interpolate DT between times.
Definition FunctionDT.h:51
Real _growth_factor
Definition FunctionDT.h:47
static InputParameters validParams()
Definition FunctionDT.C:18
virtual void init() override
Initialize the time stepper.
Definition FunctionDT.C:103
std::vector< Real > _time_knots
Definition FunctionDT.h:53
Real _min_dt
Definition FunctionDT.h:48
void removeOldKnots()
Definition FunctionDT.C:109
Interface for objects that need to use functions.
const Function & getFunction(const std::string &name) const
Get a function with a given name.
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:30
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object.
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.
const std::string & name() const
Get the name of the class.
Definition MooseBase.h:103
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:271
const std::string & _name
The name of this class.
Definition MooseBase.h:381
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition MooseBase.h:199
Function base which provides a piecewise approximation to a specified (x,y) point data set.
virtual Real functionSize() const
virtual Real domain(const int i) const
const Point & _point_zero
Zero point.
void mooseDeprecated(Args &&... args) const
Base class for time stepping.
Definition TimeStepper.h:23
static InputParameters validParams()
Definition TimeStepper.C:16
Real & _time
Values from executioner.