ScalarField
/src/core/utils/ScalarField.h Extends: FieldExpr /test/core/utils/ScalarFieldTest.cpp
A template class for handling scalar fields
Note
A field wrapped by this class doesn't guarantee any regularity condition. You can wrap any scalar field which is just evaluable at any point. The definition of a formal derivative is not required. See DifferentiableScalarField or TwiceDifferentiableScalarField for inherited classes which force specific regularity conditions.
ScalarField allows you to wrap any C++ lambda encoding the analytical expression of your field and let you access to numerical approximations of both gradient and hessian as well as compose new scalar fields using a consistent arithmetic between field objects. You can even integrate a ScalarField over any triangulated domain. See Mesh for details.
ScalarField constitutes one of the most fundamental types in the fdaPDE architecture.
Warning
The result of a field expression is not a ScalarField object. In particular a field expression is a callable object only and doesn't expose the same interface of ScalarField. See Wrapping the result of a field expression in a ScalarField object for details on how obtain a ScalarField from a field expression.
Field Expressions
Field expressions are supported by lazy evaluation via C++ expression templatates. The main goal is to let you in the position to write complex callables starting from simple ones in a formal way while preserving good performances.
Supported arithmetic operators | code example | formal notation |
operator+ | f1 + f2 | \( f_1 + f_2 \) |
operator- | f1 - f2 | \( f_1 - f_2 \) |
operator* | f1 * f2 | \( f_1 \cdot f_2 \) |
operator/ | f1 / f2 | \( f_1 / f_2 \) |
The above set of operators is available also between a ScalarField object and any integral or floating-point type.
For more informations about mesh handling and integration see Mesh and Integrator
Info
An expression of kind in the previous example has internal type FieldBinOp<ScalarField<2>, ScalarField<2>, plus<>>
. For this reason when using field expressions use always the auto
keyword to let the compiler infer the correct type.
Wrapping the result of a field expression in a ScalarField object
Even if the result of a field expression is not a ScalarField there is an usefull workaround to wrap this one in a ScalarField object. The main point is that a field expression looses informations about the domain dimensions where the resulting field should be defined. Hence by providing this information, i.e. by wrapping the field expression in a C++ lambda function, you can obtain a valid ScalarField object.
Methods
Constructor.
Args | Description |
---|---|
std::function<double(SVector<N>)> f_ |
An std::function implementing the analytical expression of the field taking an N dimensional point in input and returning a double in output |
Returns the evaluation of the field at point x. Preserves the std::function
syntax.
Args | Description |
---|---|
SVector<N>& x |
The point in where to evaluate the field |
Returns the numerical approximation of the gradient of at point x. The partial derivatives of the field are approximated via central differences according to the following expression ( denoting the normal unit vector along direction )
Args | Description |
---|---|
const SVector<N>& x |
The point in where to approximate the gradient of the field |
double step |
The value of in the central difference formula |
Warning
In principle the approximation of the partial derivatives require the field to be just evaluable at point x. Anyway if the field is not differentiable at point x the approximation might cause problems.
Returns the numerical approximation of the hessian matrix of at point x. The partial derivatives of the field are approximated via central differences according to the following expressions ( denoting the normal unit vector along direction )
Args | Description |
---|---|
const SVector<N>& x |
The point in where to approximate the hessian of the field |
double step |
The value of in the central difference formula |
Warning
In principle the approximation of the partial derivatives require the field to be just evaluable at point x. Anyway if the field is not differentiable at point x the approximation might cause problems.
Returns a VectorField representing an approximation of . The obtained approximation along a given direction is obtained using the central difference formula as in approxGradient()
.
Args | Description |
---|---|
double step |
The value of in the central difference formula |
Note
A VectorField is a callable object. Evaluate the result of gradient()
in a point to obtain the gradient approximation.
Returns a VectorField representing an approximation of . The obtained approximation along a given direction is obtained using the central difference formula as in approxGradient()
with a fixed, not-controllable, value for the step size equal to 0.001.
Note
This method is overridden in more specialized classes such DifferentiableScalarField and TwiceDifferentiableScalarField where the exact gradient is evaluated.
Returns a callable object representing an approximation of the hessian matrix . Approximation of second derivatives is obtained using the central difference formula as in approxHessian()
.
Args | Description |
---|---|
double step |
The value of in the central difference formula |
Returns a callable object representing an approximation of the hessian matrix . Approximation of second derivatives is obtained using the central difference formula as in approxHessian()
with a fixed, not-controllable, value for the step size equal to 0.001.
Note
This method is overridden in more specialized classes like TwiceDifferentiableScalarField where the exact hessian is evaluated.