-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStepFunction.C
197 lines (159 loc) · 5.24 KB
/
StepFunction.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html
#include "MooseError.h"
// MOOSE includes
#include "InputParameters.h"
#include "StepFunction.h"
#include "FEProblemBase.h"
registerMooseObject(MOOSEAPPNAME, StepFunction);
InputParameters
StepFunction::validParams()
{
InputParameters params = Function::validParams();
params.addRequiredParam<Real>("start_time", "Start time for the transition from start to end value.");
params.addRequiredParam<double>("start_value", "Start value.");
params.addRequiredParam<Real>("end_time", "End time for the transition from start to end value.");
params.addRequiredParam<double>("end_value", "end value.");
MooseEnum step_function_type_choice("Linear=0 Smooth=1 Perlin=2", "Perlin");
params.addRequiredParam<MooseEnum>("step_function_type",
step_function_type_choice,
"Type of the step function used for the transition.");
params.addClassDescription("Step function acting on time (constant over space).");
return params;
}
StepFunction::StepFunction(const InputParameters & parameters)
: Function(parameters),
_step_function_type(getParam<MooseEnum>("step_function_type").getEnum<StepFunctionType>()),
_start_time(getParam<Real>("start_time")),
_start_value(getParam<double>("start_value")),
_end_time(getParam<Real>("end_time")),
_end_value(getParam<double>("end_value"))
{
}
Real
StepFunction::value(Real t, const Point & /*p*/) const
{
if (t == _last_value_t)
{
return _last_value;
};
Real y = getValueInternal(t, false);
_last_value_t = t;
_last_value = y;
return y;
}
RealGradient
StepFunction::gradient(Real /*t*/, const Point & /*p*/) const
{
// this function only depends on time
return 0;
}
Real
StepFunction::timeDerivative(Real t, const Point & /*p*/) const
{
if (t == _last_timeDerivative_t)
return _last_timeDerivative;
Real y = getValueInternal(t, true);
_last_timeDerivative_t = t;
_last_timeDerivative = y;
return y;
}
RealVectorValue
StepFunction::vectorValue(Real /*t*/, const Point & /*p*/) const
{
mooseError("The vectorValue method is not defined in StepFunction");
}
void
StepFunction::initialSetup()
{
}
double
StepFunction::getValueInternal(const Real t, const bool timeDerivative) const
{
if (timeDerivative == true)
return evaluateTimeDerivative(_step_function_type, _start_time, _start_value, _end_time, _end_value, t);
else
return evaluateValue(
_step_function_type, _start_time, _start_value, _end_time, _end_value, t);
}
double
StepFunction::evaluateValue(const StepFunctionType type,
const Real start_time,
const double start_value,
const Real end_time,
const double end_value,
const Real t)
{
if (t < start_time)
{
// we are before start_time - just return start_value
return start_value;
}
else if (t >= end_time)
{
// we are at or after end_time - just return end_value
return end_value;
}
else
{
// we are in between start_time and end_time
// let's calculate the dimensionless factor 'f' for time [0..1]
const double f = (t - start_time) / (end_time - start_time);
if (type == StepFunctionType::LINEAR)
return start_value + (end_value - start_value) * f;
if (type == StepFunctionType::SMOOTH)
{
const double p = f * f * (3.0 - 2.0 * f);
return start_value + (end_value - start_value) * p;
};
if (type == StepFunctionType::PERLIN)
{
const double p = f * f * f * (f * (6.0 * f - 15.0) + 10.0);
return start_value + (end_value - start_value) * p;
};
throw std::invalid_argument("Unsupported step function type.");
}
}
double
StepFunction::evaluateTimeDerivative(const StepFunctionType type,
const Real start_time,
const double start_value,
const Real end_time,
const double end_value,
const Real t)
{
if (t < start_time)
{
// we are before _start_time - time derivative is zero
return 0;
}
else if (t >= end_time)
{
// we are at or after _end_time - time derivative is zero
return 0;
}
else
{
// please note: this code path is never active if _start_time
// equals _end_time, so no need for checking
if (type == StepFunctionType::LINEAR)
return (end_value - start_value) / (end_time - start_time);
if (type == StepFunctionType::SMOOTH)
{
Real const x = (t - start_time) / (end_time - start_time);
return (start_value - end_value) * 6 * (-1 + x) * x;
};
if (type == StepFunctionType::PERLIN)
{
Real const x = (t - start_time) / (end_time - start_time);
return (start_value - end_value) * -30 * (-1 + x) * (-1 + x) * x * x;
};
throw std::invalid_argument("Unsupported step function type.");
}
}