Reference documentation for deal.II version GIT relicensing-422-gb369f187d8 2024-04-17 18:10:02+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Private Types | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
TensorFunctionParser< rank, dim, Number > Class Template Reference

#include <deal.II/base/tensor_function_parser.h>

Inheritance diagram for TensorFunctionParser< rank, dim, Number >:
Inheritance graph
[legend]

Public Types

using ConstMap = std::map< std::string, double >
 
using value_type = Tensor< rank, dim, Number >
 
using gradient_type = Tensor< rank+1, dim, Number >
 
using time_type = typename FunctionTime< typename numbers::NumberTraits< Number >::real_type >::time_type
 

Public Member Functions

 TensorFunctionParser (const double initial_time=0.0)
 
 TensorFunctionParser (const std::string &expression, const std::string &constants="", const std::string &variable_names=default_variable_names()+",t")
 
 TensorFunctionParser (const TensorFunctionParser &)=delete
 
 TensorFunctionParser (TensorFunctionParser &&)=delete
 
TensorFunctionParseroperator= (const TensorFunctionParser &)=delete
 
TensorFunctionParseroperator= (TensorFunctionParser &&)=delete
 
virtual void initialize (const std::string &vars, const std::vector< std::string > &expressions, const ConstMap &constants, const bool time_dependent=false) override
 
void initialize (const std::string &vars, const std::string &expression, const ConstMap &constants, const bool time_dependent=false)
 
virtual Tensor< rank, dim, Number > value (const Point< dim > &p) const override
 
virtual void value_list (const std::vector< Point< dim > > &p, std::vector< Tensor< rank, dim, Number > > &values) const override
 
const std::vector< std::string > & get_expressions () const
 
virtual gradient_type gradient (const Point< dim > &p) const
 
virtual void gradient_list (const std::vector< Point< dim > > &points, std::vector< gradient_type > &gradients) const
 
Number get_time () const
 
virtual void set_time (const Number new_time)
 
virtual void advance_time (const Number delta_t)
 
template<class Archive >
void serialize (Archive &ar, const unsigned int version)
 
Subscriptor functionality

Classes derived from Subscriptor provide a facility to subscribe to this object. This is mostly used by the SmartPointer class.

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
 

Static Public Member Functions

static std::string default_variable_names ()
 
static ::ExceptionBaseExcParseError (int arg1, std::string arg2)
 
static ::ExceptionBaseExcInvalidExpressionSize (int arg1, int arg2)
 
static ::ExceptionBaseExcInUse (int arg1, std::string arg2, std::string arg3)
 
static ::ExceptionBaseExcNoSubscriber (std::string arg1, std::string arg2)
 

Protected Member Functions

void init_muparser () const
 
Number do_value (const Point< dim > &p, const double time, unsigned int component) const
 
void do_all_values (const Point< dim > &p, const double time, ArrayView< Number > &values) const
 

Protected Attributes

std::vector< std::string > expressions
 

Private Types

using map_value_type = decltype(counter_map)::value_type
 
using map_iterator = decltype(counter_map)::iterator
 

Private Member Functions

void check_no_subscribers () const noexcept
 

Private Attributes

unsigned int n_components
 
Number time
 
std::atomic< unsigned intcounter
 
std::map< std::string, unsigned intcounter_map
 
std::vector< std::atomic< bool > * > validity_pointers
 
const std::type_info * object_info
 
Threads::ThreadLocalStorage< internal::FunctionParser::ParserDataparser_data
 
std::map< std::string, double > constants
 
std::vector< std::string > var_names
 
bool initialized
 
unsigned int n_vars
 

Static Private Attributes

static std::mutex mutex
 

Detailed Description

template<int rank, int dim, typename Number = double>
class TensorFunctionParser< rank, dim, Number >

This class implements a tensor function object that gets its value by parsing a string describing this function. It is a wrapper class for the muparser library (see http://muparser.beltoforion.de/). This class is essentially an extension of the FunctionParser class to read in a TensorFunction. The class reads in an expression of length dimrank (separated by a semicolon) where the components of the tensor function are filled according to the C++ convention (fastest index is the most right one).

Note
In contrast to the FunctionParser class the TensorFunctionParser class does not support automatic differentiation.

A minimal example for the usage of the class would be:

// set up time dependent tensor function:
const std::string variables = "x,y,t";
const std::string expression =
"exp(-t)*cos(x+y);-sin(pi*x*y-t);sin(pi*x*y-t);exp(t)*cos(x+y)";
std::map<std::string,double> constants;
// TensorFunctionParser with 2+1 variables (space + time) in 2d of rank 2.
// It is necessary to tell the parser that there is an additional variable
// to be taken into account (t).
tfp.initialize(variables,
expression,
true); // flag for time dependence
// Point at which we want to evaluate the function
Point<2> point(0.0, 1.0);
// evaluate the expression at 'point':
double result = tfp.value(point);
deallog << "Function '" << expression << "'"
<< " @ " << point
<< " is: "
<< std::endl
<< result[0][0] << ' ' << result[0][1] << std::endl
<< result[1][0] << ' ' << result[1][1]
<< std::endl;
Definition point.h:111
virtual Tensor< rank, dim, Number > value(const Point< dim > &p) const override
virtual void initialize(const std::string &vars, const std::vector< std::string > &expressions, const ConstMap &constants, const bool time_dependent=false) override
LogStream deallog
Definition logstream.cc:36
static constexpr double PI
Definition numbers.h:259

See also the documentation of the FunctionParser class.

This class overloads the virtual method value() and value_list() of the TensorFunction base class with the byte compiled versions of the expressions given to the initialize() methods. Note that the class will not work unless you first call the initialize() method that accepts the text description of the function as an argument (among other things).

The syntax to describe a function follows usual programming practice, and is explained in detail at the homepage of the underlying muparser library at http://muparser.beltoforion.de/ .

Vector-valued functions can either be declared using strings where the function components are separated by semicolons, or using a vector of strings each defining one vector component.

Definition at line 105 of file tensor_function_parser.h.

Member Typedef Documentation

◆ ConstMap

template<int rank, int dim, typename Number = double>
using TensorFunctionParser< rank, dim, Number >::ConstMap = std::map<std::string, double>

Type for the constant map. Used by the initialize() method.

Definition at line 160 of file tensor_function_parser.h.

◆ value_type

template<int rank, int dim, typename Number = double>
using TensorFunction< rank, dim, Number >::value_type = Tensor<rank, dim, Number>
inherited

Alias for the return types of the value function.

Definition at line 63 of file tensor_function.h.

◆ gradient_type

template<int rank, int dim, typename Number = double>
using TensorFunction< rank, dim, Number >::gradient_type = Tensor<rank + 1, dim, Number>
inherited

Alias for the return types of the gradient functions.

Definition at line 68 of file tensor_function.h.

◆ time_type

template<int rank, int dim, typename Number = double>
using TensorFunction< rank, dim, Number >::time_type = typename FunctionTime< typename numbers::NumberTraits<Number>::real_type>::time_type
inherited

The scalar-valued real type used for representing time.

Definition at line 73 of file tensor_function.h.

◆ map_value_type

using Subscriptor::map_value_type = decltype(counter_map)::value_type
privateinherited

The data type used in counter_map.

Definition at line 229 of file subscriptor.h.

◆ map_iterator

using Subscriptor::map_iterator = decltype(counter_map)::iterator
privateinherited

The iterator type used in counter_map.

Definition at line 234 of file subscriptor.h.

Constructor & Destructor Documentation

◆ TensorFunctionParser() [1/4]

template<int rank, int dim, typename Number >
TensorFunctionParser< rank, dim, Number >::TensorFunctionParser ( const double  initial_time = 0.0)

Standard constructor. Only set initial time. This object needs to be initialized with the initialize() method before you can use it. If an attempt to use this function is made before the initialize() method has been called, then an exception is thrown.

Definition at line 39 of file tensor_function_parser.cc.

◆ TensorFunctionParser() [2/4]

template<int rank, int dim, typename Number >
TensorFunctionParser< rank, dim, Number >::TensorFunctionParser ( const std::string &  expression,
const std::string &  constants = "",
const std::string &  variable_names = default_variable_names() + ",t" 
)

Constructor for parsed functions. This object needs to be initialized with the initialize() method before you can use it. If an attempt to use this function is made before the initialize() method has been called, then an exception is thrown. Takes a semicolon separated list of expressions (one for each component of the tensor function), an optional comma-separated list of constants.

Definition at line 47 of file tensor_function_parser.cc.

◆ TensorFunctionParser() [3/4]

template<int rank, int dim, typename Number = double>
TensorFunctionParser< rank, dim, Number >::TensorFunctionParser ( const TensorFunctionParser< rank, dim, Number > &  )
delete

Copy constructor. Objects of this type can not be copied, and consequently this constructor is deleted.

◆ TensorFunctionParser() [4/4]

template<int rank, int dim, typename Number = double>
TensorFunctionParser< rank, dim, Number >::TensorFunctionParser ( TensorFunctionParser< rank, dim, Number > &&  )
delete

Move constructor. Objects of this type can not be moved, and consequently this constructor is deleted.

Member Function Documentation

◆ operator=() [1/2]

template<int rank, int dim, typename Number = double>
TensorFunctionParser & TensorFunctionParser< rank, dim, Number >::operator= ( const TensorFunctionParser< rank, dim, Number > &  )
delete

Copy operator. Objects of this type can not be copied, and consequently this operator is deleted.

◆ operator=() [2/2]

template<int rank, int dim, typename Number = double>
TensorFunctionParser & TensorFunctionParser< rank, dim, Number >::operator= ( TensorFunctionParser< rank, dim, Number > &&  )
delete

Move operator. Objects of this type can not be moved, and consequently this operator is deleted.

◆ initialize() [1/2]

template<int rank, int dim, typename Number >
void TensorFunctionParser< rank, dim, Number >::initialize ( const std::string &  vars,
const std::vector< std::string > &  expressions,
const ConstMap constants,
const bool  time_dependent = false 
)
overridevirtual

Initialize the tensor function. This method accepts the following parameters:

Parameters
[in]varsA string with the variables that will be used by the expressions to be evaluated. Note that the variables can have any name (of course different from the function names defined above!), but the order IS important. The first variable will correspond to the first component of the point in which the function is evaluated, the second variable to the second component and so forth. If this function is also time dependent, then it is necessary to specify it by setting the time_dependent parameter to true. An exception is thrown if the number of variables specified here is different from dim (if this function is not time-dependent) or from dim+1 (if it is time-dependent).
[in]expressionsA vector of strings containing the expressions that will be byte compiled by the internal parser (TensorFunctionParser). Note that the size of this vector must match exactly the number of components of the TensorFunctionParser, as declared in the constructor. If this is not the case, an exception is thrown.
[in]constantsA map of constants used to pass any necessary constant that we want to specify in our expressions (in the example above the number pi). An expression is valid if and only if it contains only defined variables and defined constants (other than the functions specified above). If a constant is given whose name is not valid (eg: constants["sin"] = 1.5;) an exception is thrown.
[in]time_dependentIf this is a time dependent function, then the last variable declared in vars is assumed to be the time variable, and this->get_time() is used to initialize it when evaluating the function. Naturally the number of variables parsed by the initialize() method in this case is dim+1. The value of this parameter defaults to false, i.e. do not consider time.

Reimplemented from internal::FunctionParser::ParserImplementation< dim, Number >.

Definition at line 73 of file tensor_function_parser.cc.

◆ initialize() [2/2]

template<int rank, int dim, typename Number >
void TensorFunctionParser< rank, dim, Number >::initialize ( const std::string &  vars,
const std::string &  expression,
const ConstMap constants,
const bool  time_dependent = false 
)

Initialize the function. Same as above, but accepts a string rather than a vector of strings. If this is a vector valued function, its components are expected to be separated by a semicolon. An exception is thrown if this method is called and the number of components successfully parsed does not match the number of components of the base function.

Definition at line 89 of file tensor_function_parser.cc.

◆ default_variable_names()

template<int rank, int dim, typename Number >
std::string TensorFunctionParser< rank, dim, Number >::default_variable_names ( )
static

A function that returns default names for variables, to be used in the first argument of the initialize() functions: it returns "x" in 1d, "x,y" in 2d, and "x,y,z" in 3d.

Definition at line 273 of file tensor_function_parser.h.

◆ value()

template<int rank, int dim, typename Number >
Tensor< rank, dim, Number > TensorFunctionParser< rank, dim, Number >::value ( const Point< dim > &  p) const
overridevirtual

Return the value of the tensor function at the given point.

Reimplemented from TensorFunction< rank, dim, Number >.

Definition at line 105 of file tensor_function_parser.cc.

◆ value_list()

template<int rank, int dim, typename Number >
void TensorFunctionParser< rank, dim, Number >::value_list ( const std::vector< Point< dim > > &  p,
std::vector< Tensor< rank, dim, Number > > &  values 
) const
overridevirtual

Return the value of the tensor function at the given point.

Reimplemented from TensorFunction< rank, dim, Number >.

Definition at line 120 of file tensor_function_parser.cc.

◆ get_expressions()

template<int rank, int dim, typename Number >
const std::vector< std::string > & TensorFunctionParser< rank, dim, Number >::get_expressions ( ) const

Return an array of function expressions (one per component), used to initialize this function.

Definition at line 31 of file tensor_function_parser.cc.

◆ gradient()

template<int rank, int dim, typename Number = double>
virtual gradient_type TensorFunction< rank, dim, Number >::gradient ( const Point< dim > &  p) const
virtualinherited

Return the gradient of the function at the given point.

Reimplemented in ConstantTensorFunction< rank, dim, Number >, and ConstantTensorFunction< rank, dim, double >.

◆ gradient_list()

template<int rank, int dim, typename Number = double>
virtual void TensorFunction< rank, dim, Number >::gradient_list ( const std::vector< Point< dim > > &  points,
std::vector< gradient_type > &  gradients 
) const
virtualinherited

Set gradients to the gradients of the function at the points. It is assumed that values already has the right size, i.e. the same size as the points array.

◆ get_time()

template<typename Number = double>
Number FunctionTime< Number >::get_time ( ) const
inherited

Return the value of the time variable.

◆ set_time()

template<typename Number = double>
virtual void FunctionTime< Number >::set_time ( const Number  new_time)
virtualinherited

Set the time to new_time, overwriting the old value.

◆ advance_time()

template<typename Number = double>
virtual void FunctionTime< Number >::advance_time ( const Number  delta_t)
virtualinherited

Advance the time by the given time step delta_t.

◆ subscribe()

void Subscriptor::subscribe ( std::atomic< bool > *const  validity,
const std::string &  identifier = "" 
) const
inherited

Subscribes a user of the object by storing the pointer validity. The subscriber may be identified by text supplied as identifier.

Definition at line 135 of file subscriptor.cc.

◆ unsubscribe()

void Subscriptor::unsubscribe ( std::atomic< bool > *const  validity,
const std::string &  identifier = "" 
) const
inherited

Unsubscribes a user from the object.

Note
The identifier and the validity pointer must be the same as the one supplied to subscribe().

Definition at line 155 of file subscriptor.cc.

◆ n_subscriptions()

unsigned int Subscriptor::n_subscriptions ( ) const
inlineinherited

Return the present number of subscriptions to this object. This allows to use this class for reference counted lifetime determination where the last one to unsubscribe also deletes the object.

Definition at line 300 of file subscriptor.h.

◆ list_subscribers() [1/2]

template<typename StreamType >
void Subscriptor::list_subscribers ( StreamType &  stream) const
inlineinherited

List the subscribers to the input stream.

Definition at line 317 of file subscriptor.h.

◆ list_subscribers() [2/2]

void Subscriptor::list_subscribers ( ) const
inherited

List the subscribers to deallog.

Definition at line 203 of file subscriptor.cc.

◆ serialize()

template<class Archive >
void Subscriptor::serialize ( Archive &  ar,
const unsigned int  version 
)
inlineinherited

Read or write the data of this object to or from a stream for the purpose of serialization using the BOOST serialization library.

This function does not actually serialize any of the member variables of this class. The reason is that what this class stores is only who subscribes to this object, but who does so at the time of storing the contents of this object does not necessarily have anything to do with who subscribes to the object when it is restored. Consequently, we do not want to overwrite the subscribers at the time of restoring, and then there is no reason to write the subscribers out in the first place.

Definition at line 309 of file subscriptor.h.

◆ check_no_subscribers()

void Subscriptor::check_no_subscribers ( ) const
privatenoexceptinherited

Check that there are no objects subscribing to this object. If this check passes then it is safe to destroy the current object. It this check fails then this function will either abort or print an error message to deallog (by using the AssertNothrow mechanism), but will not throw an exception.

Note
Since this function is just a consistency check it does nothing in release mode.
If this function is called when there is an uncaught exception then, rather than aborting, this function prints an error message to the standard error stream and returns.

Definition at line 52 of file subscriptor.cc.

◆ init_muparser()

template<int dim, typename Number >
void internal::FunctionParser::ParserImplementation< dim, Number >::init_muparser ( ) const
inherited

Set up the internal muParser objects to parse and evaluate mathematical expressions.

Definition at line 307 of file mu_parser_internal.cc.

◆ do_value()

template<int dim, typename Number >
Number internal::FunctionParser::ParserImplementation< dim, Number >::do_value ( const Point< dim > &  p,
const double  time,
unsigned int  component 
) const
inherited

Compute the value of a single component.

Definition at line 412 of file mu_parser_internal.cc.

◆ do_all_values()

template<int dim, typename Number >
void internal::FunctionParser::ParserImplementation< dim, Number >::do_all_values ( const Point< dim > &  p,
const double  time,
ArrayView< Number > &  values 
) const
inherited

Compute the values of all components.

Definition at line 459 of file mu_parser_internal.cc.

Member Data Documentation

◆ n_components

template<int rank, int dim, typename Number = double>
unsigned int TensorFunctionParser< rank, dim, Number >::n_components
private

Number of components is equal dimrank.

Definition at line 267 of file tensor_function_parser.h.

◆ time

template<typename Number = double>
Number FunctionTime< Number >::time
privateinherited

Store the present time.

Definition at line 112 of file function_time.h.

◆ counter

std::atomic<unsigned int> Subscriptor::counter
mutableprivateinherited

Store the number of objects which subscribed to this object. Initially, this number is zero, and upon destruction it shall be zero again (i.e. all objects which subscribed should have unsubscribed again).

The creator (and owner) of an object is counted in the map below if HE manages to supply identification.

We use the mutable keyword in order to allow subscription to constant objects also.

This counter may be read from and written to concurrently in multithreaded code: hence we use the std::atomic class template.

Definition at line 218 of file subscriptor.h.

◆ counter_map

std::map<std::string, unsigned int> Subscriptor::counter_map
mutableprivateinherited

In this map, we count subscriptions for each different identification string supplied to subscribe().

Definition at line 224 of file subscriptor.h.

◆ validity_pointers

std::vector<std::atomic<bool> *> Subscriptor::validity_pointers
mutableprivateinherited

In this vector, we store pointers to the validity bool in the SmartPointer objects that subscribe to this class.

Definition at line 240 of file subscriptor.h.

◆ object_info

const std::type_info* Subscriptor::object_info
mutableprivateinherited

Pointer to the typeinfo object of this object, from which we can later deduce the class name. Since this information on the derived class is neither available in the destructor, nor in the constructor, we obtain it in between and store it here.

Definition at line 248 of file subscriptor.h.

◆ mutex

std::mutex Subscriptor::mutex
staticprivateinherited

A mutex used to ensure data consistency when accessing the mutable members of this class. This lock is used in the subscribe() and unsubscribe() functions, as well as in list_subscribers().

Definition at line 271 of file subscriptor.h.

◆ expressions

template<int dim, typename Number >
std::vector<std::string> internal::FunctionParser::ParserImplementation< dim, Number >::expressions
inherited

An array of function expressions (one per component), required to initialize tfp in each thread.

Definition at line 202 of file mu_parser_internal.h.

◆ parser_data

template<int dim, typename Number >
Threads::ThreadLocalStorage<internal::FunctionParser::ParserData> internal::FunctionParser::ParserImplementation< dim, Number >::parser_data
mutableprivateinherited

The muParser objects (hidden with the PIMPL idiom) for each thread (and one for each component).

Definition at line 210 of file mu_parser_internal.h.

◆ constants

template<int dim, typename Number >
std::map<std::string, double> internal::FunctionParser::ParserImplementation< dim, Number >::constants
privateinherited

An array to keep track of all the constants, required to initialize fp in each thread.

Definition at line 216 of file mu_parser_internal.h.

◆ var_names

template<int dim, typename Number >
std::vector<std::string> internal::FunctionParser::ParserImplementation< dim, Number >::var_names
privateinherited

An array for the variable names, required to initialize fp in each thread.

Definition at line 222 of file mu_parser_internal.h.

◆ initialized

template<int dim, typename Number >
bool internal::FunctionParser::ParserImplementation< dim, Number >::initialized
privateinherited

State of usability. This variable is checked every time the function is called for evaluation. It's set to true in the initialize() methods.

Definition at line 228 of file mu_parser_internal.h.

◆ n_vars

template<int dim, typename Number >
unsigned int internal::FunctionParser::ParserImplementation< dim, Number >::n_vars
privateinherited

Number of variables. If this is also a function of time, then the number of variables is dim+1, otherwise it is dim. In the case that this is a time dependent function, the time is supposed to be the last variable. If n_vars is not identical to the number of the variables parsed by the initialize() method, then an exception is thrown.

Definition at line 237 of file mu_parser_internal.h.


The documentation for this class was generated from the following files: