IFEM 90A354
Function.h
Go to the documentation of this file.
1// $Id$
2//==============================================================================
12//==============================================================================
13
14#ifndef UTL_FUNCTION_H
15#define UTL_FUNCTION_H
16
17#include "matrixnd.h"
18#include "Tensor.h"
19#include "Vec3.h"
20#include <cstddef>
21
22
23namespace utl
24{
29 template<class Arg, class Result>
31 {
32 protected:
35
36 public:
38 virtual ~Function() {}
39
41 virtual bool isZero() const { return false; }
43 virtual bool isConstant() const { return true; }
44
45 protected:
47 virtual Result evaluate(const Arg& x) const = 0;
48
49 public:
51 Result operator()(const Arg& x) const { return this->evaluate(x); }
52
53 typedef Arg Input;
54 typedef Result Output;
55 };
56
57
63 template<class Arg, class Result>
65 {
66 protected:
69
70 public:
72 virtual ~Function2() {}
73
75 virtual bool isZero() const { return false; }
76
77 protected:
79 virtual Result evaluate(const Arg& x, const Arg& y) const = 0;
80
81 public:
83 Result operator()(const Arg& x, const Arg& y) const
84 { return this->evaluate(x,y); }
85
86 typedef Arg Input;
87 typedef Result Output;
88 };
89
90
96 template<class Result>
97 class SpatialFunction : public Function<Vec3,Result>
98 {
99 protected:
101 explicit SpatialFunction(const Result& val) : zero(val) {}
102
103 public:
105 virtual Result deriv(const Vec3&, int) const { return zero; }
107 virtual Result dderiv(const Vec3&, int, int) const { return zero; }
108
109 protected:
111 virtual std::vector<Real> evalGradient(const Vec3&) const { return {}; }
113 virtual std::vector<Real> evalHessian(const Vec3&) const { return {}; }
115 virtual std::vector<Real> evalTimeDerivative(const Vec3&) const { return {}; }
116
117 Result zero;
118 };
119}
120
121
126class ScalarFunc : public utl::Function<Real,Real>
127{
128protected:
131
132public:
134 Real eval(Real x) const { return this->evaluate(x); }
136 virtual Real deriv(Real) const { return Real(0); }
137};
138
139
147{
148protected:
151
152public:
154 virtual ~FunctionBase() {}
155
157 virtual unsigned char getType() const = 0;
158
160 virtual std::vector<Real> getValue(const Vec3&) const = 0;
162 virtual Real getScalarValue(const Vec3&) const = 0;
163
165 size_t dim() const { return ncmp; }
166
168 virtual bool initPatch(size_t) { return true; }
169
171 virtual bool inDomain(const Vec3&) const { return true; }
172
174 virtual void setParam(const std::string&, Real) {}
176 void setParam(const std::string& name, const Vec3& value)
177 {
178 this->setParam(name + "x", value.x);
179 this->setParam(name + "y", value.y);
180 this->setParam(name + "z", value.z);
181 }
182
183protected:
184 size_t ncmp;
185};
186
187
192class RealFunc : public utl::SpatialFunction<Real>, public FunctionBase
193{
194protected:
197
198public:
200 unsigned char getType() const override { return 1; }
201
203 std::vector<Real> getValue(const Vec3& X) const override
204 {
205 return std::vector<Real>(1,this->evaluate(X));
206 }
207
209 virtual Vec3 gradient(const Vec3& X) const
210 {
211 Vec3 result;
212 for (size_t d = 1; d <= 3; ++d)
213 result[d-1] = this->deriv(X,d);
214
215 return result;
216 }
217
219 virtual SymmTensor hessian(const Vec3& X) const
220 {
221 SymmTensor result(3);
222 for (size_t d1 = 1; d1 <= 3; ++d1)
223 for (size_t d2 = d1; d2 <= 3; ++d2)
224 result(d1,d2) = this->dderiv(X, d1, d2);
225
226 return result;
227 }
228
230 Real getScalarValue(const Vec3& X) const override { return this->evaluate(X); }
231
233 Real timeDerivative(const Vec3& X) const { return this->deriv(X,4); }
234};
235
236
241class VecFunc : public utl::SpatialFunction<Vec3>, public FunctionBase
242{
243protected:
245 explicit VecFunc(size_t n = 3) : utl::SpatialFunction<Vec3>(Vec3())
246 {
247 ncmp = n;
248 }
249
250public:
252 unsigned char getType() const override { return 2; }
253
255 std::vector<Real> getValue(const Vec3& X) const override
256 {
257 return this->evaluate(X).vec(ncmp);
258 }
259
261 Real getScalarValue(const Vec3& X) const override
262 {
263 return this->evaluate(X).length();
264 }
265
267 Tensor gradient(const Vec3& X) const
268 {
269 Tensor result(ncmp);
270 result = this->evalGradient(X);
271 return result;
272 }
273
276 {
278 result.fill(this->evalHessian(X).data());
279 return result;
280 }
281
283 Vec3 timeDerivative(const Vec3& X) const { return this->evalTimeDerivative(X); }
284};
285
286
291class TractionFunc : public utl::Function2<Vec3,Vec3>
292{
293public:
295 virtual bool isNormalPressure() const { return false; }
296
298 virtual Vec3 timeDerivative(const Vec3&, const Vec3&) const { return Vec3(); }
299};
300
301#endif
#define Real
The floating point type to use.
Definition ImmersedBoundaries.h:18
Representation of second-order tensors with some basic operations.
Representation of a point in 3D space with some basic operations.
Base class for unary spatial functions of arbitrary result type.
Definition Function.h:147
virtual std::vector< Real > getValue(const Vec3 &) const =0
Returns the function value as an array.
FunctionBase()
The constructor is protected to allow sub-class instances only.
Definition Function.h:150
virtual ~FunctionBase()
Empty destructor.
Definition Function.h:154
virtual Real getScalarValue(const Vec3 &) const =0
Returns a representative scalar equivalent of the function value.
void setParam(const std::string &name, const Vec3 &value)
Sets additional parameter values in the function.
Definition Function.h:176
virtual unsigned char getType() const =0
Returns the function type flag.
size_t ncmp
Number of components in the return value.
Definition Function.h:184
size_t dim() const
Returns the number of components of the return value.
Definition Function.h:165
virtual bool inDomain(const Vec3 &) const
Checks if a specified point is within the function domain.
Definition Function.h:171
virtual bool initPatch(size_t)
Sets the active patch.
Definition Function.h:168
virtual void setParam(const std::string &, Real)
Sets an additional parameter in the function.
Definition Function.h:174
Scalar-valued unary function of a spatial point.
Definition Function.h:193
Real getScalarValue(const Vec3 &X) const override
Returns a representative scalar equivalent of the function value.
Definition Function.h:230
RealFunc()
The constructor is protected to allow sub-class instances only.
Definition Function.h:196
std::vector< Real > getValue(const Vec3 &X) const override
Returns the function value as an array.
Definition Function.h:203
unsigned char getType() const override
Returns the function type flag.
Definition Function.h:200
virtual Vec3 gradient(const Vec3 &X) const
Evaluates first derivatives of the function.
Definition Function.h:209
virtual SymmTensor hessian(const Vec3 &X) const
Evaluates second derivatives of the function.
Definition Function.h:219
Real timeDerivative(const Vec3 &X) const
Returns the time derivative of the function.
Definition Function.h:233
Scalar-valued unary function of a scalar value.
Definition Function.h:127
virtual Real deriv(Real) const
Returns the first-derivative of the function.
Definition Function.h:136
ScalarFunc()
The constructor is protected to allow sub-class instances only.
Definition Function.h:130
Real eval(Real x) const
Returns the function value for the argument x.
Definition Function.h:134
Simple class for representing a symmetric second-order tensor.
Definition Tensor.h:183
Simple class for representing a non-symmetric second-order tensor.
Definition Tensor.h:28
Vector-valued binary function of a spatial point and normal vector.
Definition Function.h:292
virtual bool isNormalPressure() const
Returns whether the traction is always normal to the face or not.
Definition Function.h:295
virtual Vec3 timeDerivative(const Vec3 &, const Vec3 &) const
Returns the time-derivative of the function.
Definition Function.h:298
Simple class for representing a point in 3D space.
Definition Vec3.h:27
Real & x
Reference to X-component.
Definition Vec3.h:31
Real & z
Reference to Z-component.
Definition Vec3.h:33
Real & y
Reference to Y-component.
Definition Vec3.h:32
Vector-valued unary function of a spatial point.
Definition Function.h:242
utl::matrix3d< Real > hessian(const Vec3 &X) const
Evaluates second derivatives of the function.
Definition Function.h:275
std::vector< Real > getValue(const Vec3 &X) const override
Returns the function value as an array.
Definition Function.h:255
Vec3 timeDerivative(const Vec3 &X) const
Evaluates time derivatives of the function.
Definition Function.h:283
unsigned char getType() const override
Returns the function type flag.
Definition Function.h:252
Tensor gradient(const Vec3 &X) const
Evaluates first derivatives of the function.
Definition Function.h:267
Real getScalarValue(const Vec3 &X) const override
Returns a representative scalar equivalent of the function value.
Definition Function.h:261
VecFunc(size_t n=3)
The constructor is protected to allow sub-class instances only.
Definition Function.h:245
Base class for binary function of arbitrary result and argument type.
Definition Function.h:65
virtual Result evaluate(const Arg &x, const Arg &y) const =0
Evaluates the function for the arguments x and y.
virtual ~Function2()
Empty destructor.
Definition Function.h:72
Result Output
Output type.
Definition Function.h:87
Result operator()(const Arg &x, const Arg &y) const
Operator returning the function value for the given arguments.
Definition Function.h:83
Function2()
The constructor is protected to allow sub-class instances only.
Definition Function.h:68
virtual bool isZero() const
Returns whether the function is identically zero or not.
Definition Function.h:75
Arg Input
Input type.
Definition Function.h:86
Base class for unary functions of arbitrary result and argument type.
Definition Function.h:31
virtual ~Function()
Empty destructor.
Definition Function.h:38
virtual bool isZero() const
Returns whether the function is identically zero or not.
Definition Function.h:41
Result operator()(const Arg &x) const
Operator returning the function value for the given argument.
Definition Function.h:51
Result Output
Output type.
Definition Function.h:54
virtual Result evaluate(const Arg &x) const =0
Evaluates the function for the argument x.
Function()
The constructor is protected to allow sub-class instances only.
Definition Function.h:34
Arg Input
Input type.
Definition Function.h:53
virtual bool isConstant() const
Returns whether the function is time-independent or not.
Definition Function.h:43
Base class for unary spatial function of arbitrary result type.
Definition Function.h:98
virtual std::vector< Real > evalGradient(const Vec3 &) const
Returns the gradient of the function as a 1D array.
Definition Function.h:111
virtual Result dderiv(const Vec3 &, int, int) const
Returns a second-derivative of the function.
Definition Function.h:107
virtual std::vector< Real > evalTimeDerivative(const Vec3 &) const
Returns the time derivatives of the function as a 1D array.
Definition Function.h:115
virtual std::vector< Real > evalHessian(const Vec3 &) const
Returns the hessian of the function as a 1D array.
Definition Function.h:113
virtual Result deriv(const Vec3 &, int) const
Returns a first-derivative of the function.
Definition Function.h:105
SpatialFunction(const Result &val)
The constructor is protected to allow sub-class instances only.
Definition Function.h:101
Result zero
Return value for default implementations of derivatives.
Definition Function.h:117
Three-dimensional rectangular matrix with some algebraic operations.
Definition matrixnd.h:30
void fill(T s)
Fill the matrix with a scalar value.
Definition matrix.h:411
Simple template classes for dense multi-dimensional matrices.
General utility classes and functions.
Definition SIMoptions.h:22