Reference documentation for deal.II version 9.2.0
|
#include <deal.II/base/symbolic_function.h>
Public Member Functions | |
SymbolicFunction (const std::vector< Differentiation::SD::Expression > &function, const Tensor< 1, dim, Differentiation::SD::Expression > &coordinate_symbols=get_default_coordinate_symbols(), const Differentiation::SD::Expression &time_symbol=Differentiation::SD::make_symbol("t"), const Differentiation::SD::types::substitution_map &user_substitution_map={}) | |
SymbolicFunction (const std::string &expressions) | |
void | update_user_substitution_map (const Differentiation::SD::types::substitution_map &substitutions) |
void | set_additional_function_arguments (const Differentiation::SD::types::substitution_map &arguments) |
const Tensor< 1, dim, Differentiation::SD::Expression > & | get_coordinate_symbols () const |
const Differentiation::SD::Expression & | get_time_symbol () const |
const std::vector< Differentiation::SD::Expression > & | get_symbolic_function_expressions () const |
const Differentiation::SD::types::substitution_map & | get_user_substitution_map () const |
SymbolicFunction< dim, RangeNumberType > | time_derivative () const |
virtual RangeNumberType | value (const Point< dim > &p, const unsigned int component=0) const override |
virtual Tensor< 1, dim, RangeNumberType > | gradient (const Point< dim > &p, const unsigned int component=0) const override |
virtual RangeNumberType | laplacian (const Point< dim > &p, const unsigned int component=0) const override |
virtual SymmetricTensor< 2, dim, RangeNumberType > | hessian (const Point< dim > &p, const unsigned int component=0) const override |
template<typename StreamType > | |
StreamType & | print (StreamType &out) const |
Public Member Functions inherited from Function< dim, double > | |
Function (const unsigned int n_components=1, const time_type initial_time=0.0) | |
Function (const Function &f)=default | |
virtual | ~Function () override=0 |
Function & | operator= (const Function &f) |
virtual void | vector_value (const Point< dim > &p, Vector< double > &values) const |
virtual void | value_list (const std::vector< Point< dim >> &points, std::vector< double > &values, const unsigned int component=0) const |
virtual void | vector_value_list (const std::vector< Point< dim >> &points, std::vector< Vector< double >> &values) const |
virtual void | vector_values (const std::vector< Point< dim >> &points, std::vector< std::vector< double >> &values) const |
virtual void | vector_gradient (const Point< dim > &p, std::vector< Tensor< 1, dim, double >> &gradients) const |
virtual void | gradient_list (const std::vector< Point< dim >> &points, std::vector< Tensor< 1, dim, double >> &gradients, const unsigned int component=0) const |
virtual void | vector_gradients (const std::vector< Point< dim >> &points, std::vector< std::vector< Tensor< 1, dim, double >>> &gradients) const |
virtual void | vector_gradient_list (const std::vector< Point< dim >> &points, std::vector< std::vector< Tensor< 1, dim, double >>> &gradients) const |
virtual void | vector_laplacian (const Point< dim > &p, Vector< double > &values) const |
virtual void | laplacian_list (const std::vector< Point< dim >> &points, std::vector< double > &values, const unsigned int component=0) const |
virtual void | vector_laplacian_list (const std::vector< Point< dim >> &points, std::vector< Vector< double >> &values) const |
virtual void | vector_hessian (const Point< dim > &p, std::vector< SymmetricTensor< 2, dim, double >> &values) const |
virtual void | hessian_list (const std::vector< Point< dim >> &points, std::vector< SymmetricTensor< 2, dim, double >> &values, const unsigned int component=0) const |
virtual void | vector_hessian_list (const std::vector< Point< dim >> &points, std::vector< std::vector< SymmetricTensor< 2, dim, double >>> &values) const |
std::size_t | memory_consumption () const |
Public Member Functions inherited from FunctionTime< numbers::NumberTraits< double >::real_type > | |
FunctionTime (const numbers::NumberTraits< double >::real_type initial_time=numbers::NumberTraits< double >::real_type(0.0)) | |
virtual | ~FunctionTime ()=default |
numbers::NumberTraits< double >::real_type | get_time () const |
virtual void | set_time (const numbers::NumberTraits< double >::real_type new_time) |
virtual void | advance_time (const numbers::NumberTraits< double >::real_type delta_t) |
Public Member Functions inherited from Subscriptor | |
Subscriptor () | |
Subscriptor (const Subscriptor &) | |
Subscriptor (Subscriptor &&) noexcept | |
virtual | ~Subscriptor () |
Subscriptor & | operator= (const Subscriptor &) |
Subscriptor & | operator= (Subscriptor &&) noexcept |
void | subscribe (std::atomic< bool > *const validity, const std::string &identifier="") const |
void | unsubscribe (std::atomic< bool > *const validity, const std::string &identifier="") const |
unsigned int | n_subscriptions () const |
template<typename StreamType > | |
void | list_subscribers (StreamType &stream) const |
void | list_subscribers () const |
template<class Archive > | |
void | serialize (Archive &ar, const unsigned int version) |
Static Public Member Functions | |
static Tensor< 1, dim, Differentiation::SD::Expression > | get_default_coordinate_symbols () |
Static Public Member Functions inherited from Subscriptor | |
static ::ExceptionBase & | ExcInUse (int arg1, std::string arg2, std::string arg3) |
static ::ExceptionBase & | ExcNoSubscriber (std::string arg1, std::string arg2) |
Private Member Functions | |
Differentiation::SD::types::substitution_map | create_evaluation_substitution_map (const Point< dim > &point) const |
void | update_values () const |
void | update_first_derivatives () const |
void | update_second_derivatives () const |
Private Attributes | |
const std::vector< Differentiation::SD::Expression > | user_function |
Differentiation::SD::types::substitution_map | user_substitution_map |
Differentiation::SD::types::substitution_map | additional_function_arguments |
std::vector< Differentiation::SD::Expression > | function |
std::vector< Tensor< 1, dim, Differentiation::SD::Expression > > | function_gradient |
std::vector< Tensor< 2, dim, Differentiation::SD::Expression > > | function_hessian |
std::vector< Differentiation::SD::Expression > | function_laplacian |
Tensor< 1, dim, Differentiation::SD::Expression > | coordinate_symbols |
Differentiation::SD::Expression | time_symbol |
Additional Inherited Members | |
Public Types inherited from Function< dim, double > | |
using | time_type = typename FunctionTime< typename numbers::NumberTraits< double >::real_type >::time_type |
Public Types inherited from FunctionTime< numbers::NumberTraits< double >::real_type > | |
using | time_type = numbers::NumberTraits< double >::real_type |
Public Attributes inherited from Function< dim, double > | |
const unsigned int | n_components |
Static Public Attributes inherited from Function< dim, double > | |
static const unsigned int | dimension |
A Function class that leverages symbolic differentiation to compute gradients, Laplacians, Hessians, and time derivatives.
This class can be used to define functions using methods provided by the Differentiation::SD namespace. In particular, one can define a symbolic evaluation point (the argument of the function), as well as a symbolic expression.
The symbolic gradients and the symbolic Hessians are computed at construction time, and when a substitution in the symbolic functions is requested by the user using the method update_user_substitution_map().
Whenever one of the evaluation methods is called, a substitution is attempted with the coordinate symbols argument replaced by the evaluation point and the symbolic time replaced by the current time, as returned by the get_time() method. The user has to make sure that at evaluation time argument substitution provides a fully evaluated expression (i.e., no other symbols are contained in the function expression, except numerical values), or an exception will be thrown. Additional symbols can be partially evaluated or substituted by storing them in a user supplied substitution maps, that can be updated by calling update_user_substitution_map() or the set_additional_function_arguments() methods.
The simplest use case of this class is given in the following example:
where a Function with two components is defined using a string containing their expressions separated by semicolons.
A more involved example, that explicitly uses Differentiation::SD::Expression objects, is given by
Partial substitution is possible (i.e., you can define the function using additional symbols). However, as soon as you evaluate the function, you have to make sure that all extraneous symbols (i.e., those not referring to the spacial coordinate_symbols
or to the time_symbol
variable) have been substituted with numerical values, or expressions of the spatial or temporal argument, by calling the update_user_substitution_map() or the set_additional_function_arguments() methods.
If your function requires additional arguments to be evaluated, you can specify them by calling the set_additional_function_arguments() method.
If you call update_user_substitution_map() and set_additional_function_arguments() with the same argument, the effect on the function evaluation will be the same, however, the internal behaviour and function derivatives will be different. The method update_user_substitution_map() performs the substitution once (the first time it is required), and then stores internally a copy of the resulting expression, together with its derivatives (if required). These are then used in all subsequent evaluations. Calling set_additional_function_arguments() will evaluate the passed substitution map on the fly during evaluation time, after all derivatives have been computed.
Definition at line 154 of file symbolic_function.h.
Functions::SymbolicFunction< dim, RangeNumberType >::SymbolicFunction | ( | const std::vector< Differentiation::SD::Expression > & | function, |
const Tensor< 1, dim, Differentiation::SD::Expression > & | coordinate_symbols = get_default_coordinate_symbols() , |
||
const Differentiation::SD::Expression & | time_symbol = Differentiation::SD::make_symbol("t") , |
||
const Differentiation::SD::types::substitution_map & | user_substitution_map = {} |
||
) |
Constructor.
The resulting Function object will have as many components as there are entries in the vector of symbolic expressions function
.
The vector function
should contain a list of symbolic expression involving the coordinate symbols argument coordinate_symbols
and possibly the symbolic time argument time_symbol
. It is possible to define it in terms of other symbols, as long as the optional parameter user_substitution_map
replaces all symbols except coordinate_symbols
and time_symbol
. This is useful if, for example, you want to express formulas in terms of material parameters that you want to name symbolically, rather than through their numeric values when defining the formula, or when you want to express your formula in terms of polar coordinates rather than cartesian ones, and you want the symbolic engine to compute the derivatives for you. You may later update the symbol map contained in user_substitution_map
by calling update_user_substitution_map().
function | A vector of symbolic expressions of type Differentiation::SD::Expression, representing the components of this Function. |
coordinate_symbols | A tensor of symbols representing coordinates, used as input argument in the symbolic expressions contained in the function vector. The default coordinate_symbols is a Tensor<1,dim,Differentiation::SD::Expression> containing the symbols "x" for dim equal to one, "x", "y" for dim equal to two, and "x", "y", "z" for dim equal to three. |
time_symbol | A symbolic variable representing time. It defaults to a symbolic variable named "t". |
user_substitution_map | Any other symbol that may be contained in the symbolic function needs to be specified in this map. The map may be empty, and the functions may still contain unevaluated symbols, provided that you call update_user_substitution_map() and provide a replacement of all symbols except coordinate_symbols and time_symbol before any evaluation occurs. |
Functions::SymbolicFunction< dim, RangeNumberType >::SymbolicFunction | ( | const std::string & | expressions | ) |
Constructor that takes a single string that describes the function expression as a semicolon separated list of expressions.
The symbolic expression can use the default argument and the default symbolic time variable, plus any additional symbols that you may need, provided that you update the user substitution map that substitutes all of them before you try to evaluate the function or its derivatives, by calling update_user_substitution_map(), and that you provide all the additional function arguments of your function using the method set_additional_function_arguments().
void Functions::SymbolicFunction< dim, RangeNumberType >::update_user_substitution_map | ( | const Differentiation::SD::types::substitution_map & | substitutions | ) |
Store and apply the substitution map substitutions
to each symbolic component of this Function object.
Notice that this method will trigger a recomputation of the gradients, Hessians, and Laplacians of each component.
void Functions::SymbolicFunction< dim, RangeNumberType >::set_additional_function_arguments | ( | const Differentiation::SD::types::substitution_map & | arguments | ) |
Set the additional arguments
to be substituted in next evaluation step.
Notice that the arguments
are substituted after evaluating the permanent_user_substitution_map
, and after all derivatives are computed. If the additional arguments you pass still depend on the coordinate or time symbols, then evaluation of derivatives will result in a partial derivative evaluation.
This method provides a way to evaluate functions that depend on more arguments than simply the coordinates and time. If you want to compute the total derivative w.r.t. to complicated symbolic expressions, you should call update_user_substitution_map() instead.
|
static |
Return a tensor of coordinate symbols that can be used to define the expressions of this symbolic function object.
The default argument is a Tensor<1,dim,Differentiation::SD::Expression> containing the symbols "x" for dim
equal to one, "x", "y" for dim
equal to two, and "x", "y", "z" for dim
equal to three.
const Tensor<1, dim, Differentiation::SD::Expression>& Functions::SymbolicFunction< dim, RangeNumberType >::get_coordinate_symbols | ( | ) | const |
Get the actual arguments used for the coordinates in the symbolic function. This object does not include any user-defined arguments.
const Differentiation::SD::Expression& Functions::SymbolicFunction< dim, RangeNumberType >::get_time_symbol | ( | ) | const |
Get the actual symbolic time in use in this symbolic function.
const std::vector<Differentiation::SD::Expression>& Functions::SymbolicFunction< dim, RangeNumberType >::get_symbolic_function_expressions | ( | ) | const |
Get the actual symbolic expressions used in this symbolic function.
const Differentiation::SD::types::substitution_map& Functions::SymbolicFunction< dim, RangeNumberType >::get_user_substitution_map | ( | ) | const |
Get the currently stored user_substitution_map
.
SymbolicFunction<dim, RangeNumberType> Functions::SymbolicFunction< dim, RangeNumberType >::time_derivative | ( | ) | const |
Return a SymbolicFunction object that represents the time derivative of this function. The spatial argument, the symbolic time, and the currently stored user substitution map are forwarded to the new function.
|
overridevirtual |
Return the value of the function at the given point. Unless there is only one component (i.e. the function is scalar), you should state the component you want to have evaluated; it defaults to zero, i.e. the first component.
Reimplemented from Function< dim, double >.
|
overridevirtual |
Return the gradient of the specified component of the function at the given point.
Reimplemented from Function< dim, double >.
|
overridevirtual |
Compute the Laplacian of a given component at point p
.
Reimplemented from Function< dim, double >.
|
overridevirtual |
Compute the Hessian of a given component at point p
, that is the gradient of the gradient of the function.
Reimplemented from Function< dim, double >.
StreamType & Functions::SymbolicFunction< dim, RangeNumberType >::print | ( | StreamType & | out | ) | const |
Print the stored arguments and function expression, as it would be evaluated when calling the method value().
Definition at line 442 of file symbolic_function.h.
|
private |
Return a substitution map that replaces the argument with the values of point
, the symbolic time with the value of this->get_time(), and any additional arguments with the substitution map given by additional_function_arguments
.
|
private |
Recompute the symbolic value of the function, applying the user substitution map. This may be an expensive computation, and it is called only if necessary.
|
private |
Recompute the symbolic gradient of the function, applying the user substitution map. This may be an expensive computation, and it is called only if necessary.
|
private |
Recompute the symbolic Hessian and the symbolic Lapalacian of the function. This may be an expensive computation, and it is called only if necessary.
|
private |
The components of this symbolic function, before any subustitution took place. This is immutable, and generated at construction time.
Before any evaluation takes place, the user_substitution_map
is applied to this object, and the result is stored in the internal variable function.
During evaluation, the symbolic_coordinate
, the symbolic_time
, and any remaining symbols are substituted with the input evaluation point, the current time, and the content of additional_function_arguments
.
Definition at line 369 of file symbolic_function.h.
|
private |
Store the user substitution map used for expression substitutions. This may be updated with a call to update_user_substitution_map(). Notice that the function may still have unresolved symbols, provided that they are resolved by a call to set_additional_function_arguments().
Definition at line 377 of file symbolic_function.h.
|
private |
Store a user substitution map used for additional argument substitutions. This will be updated by a call to set_additional_function_arguments().
Definition at line 384 of file symbolic_function.h.
|
mutableprivate |
The actual components of this symbolic function. This is obtained from the user_function
, after applying the user_substitution_map
.
Definition at line 390 of file symbolic_function.h.
|
mutableprivate |
The gradients of each component of this symbolic function. This is obtained by computing the symbolic gradient of the object function
, that is, after applying the user_substitution_map
to user_function
.
Definition at line 398 of file symbolic_function.h.
|
mutableprivate |
The Hessians of each component of this symbolic function. This is obtained by computing the symbolic Hessian of the object function
, that is, after applying the user_substitution_map
to user_function
.
Definition at line 406 of file symbolic_function.h.
|
mutableprivate |
The Laplacians of each component of this symbolic function. This is obtained by computing the symbolic Laplacian of the object function
, that is, after applying the user_substitution_map
to user_function
.
Definition at line 413 of file symbolic_function.h.
|
private |
The coordinate symbols argument of the function.
Definition at line 418 of file symbolic_function.h.
|
mutableprivate |
The symbolic time argument of the function.
Definition at line 423 of file symbolic_function.h.