version 3.10-dev
Dumux::FunctionFromStringExpression< numVars, Scalar > Class Template Reference

Evaluating string math expressions containing named variables. More...

#include <dumux/common/functionfromstringexpression.hh>

Description

template<std::size_t numVars, class Scalar = double>
class Dumux::FunctionFromStringExpression< numVars, Scalar >
Template Parameters
numVarsnumber of variables in the expression; number of function arguments of the call operator
Scalartype of numerical values in the expression

Example usage

// Create a callable f(x,t) from a string expression containing variable literals.
// The constructor compiles the expression in the constructor making calls efficient.
// The constructor throws a Dune::IOError with detailed info if parsing fails.
std::string expr = getParam("Problem.Function"); // e.g. "5*x + x*sin(x*t)"
FunctionFromStringExpression<2> f(expr, "xt"); // variables "x" and "t"
// evaluate function, result is double (the default scalar type)
const double x = 1.0, t = 2.0;
const double result = f(x, t);
T getParam(Args &&... args)
A free function to get a parameter from the parameter tree singleton.
Definition: parameters.hh:139

For variables with several characters construct

// variables "pos" and "time"
FunctionFromStringExpression<2> f(expr, std::array<std::string, 2>{{"pos", "time"}});

Public Member Functions

 FunctionFromStringExpression (const std::string &expression, const std::array< std::string, numVars > &variableNames)
 Constructor from math expression and array of variable names. More...
 
 FunctionFromStringExpression (const std::string &expression, std::string_view variableNames)
 Delegating constructor using all characters of a string as variables. More...
 
template<class S , std::enable_if_t< std::is_convertible_v< Scalar, S >, int > = 0>
Scalar operator() (const std::array< S, numVars > &params) const
 
template<class S , std::enable_if_t< std::is_convertible_v< Scalar, S >, int > = 0>
Scalar operator() (const Dune::FieldVector< S, numVars > &params) const
 
template<class ... Params, std::enable_if_t<(sizeof...(Params)==numVars) &&(std::is_convertible_v< Scalar, std::decay_t< Params > > &&...), int > = 0>
Scalar operator() (Params &&... params) const
 
void setVerbosity (unsigned int v)
 

Static Public Attributes

static constexpr std::size_t numVariables = numVars
 

Constructor & Destructor Documentation

◆ FunctionFromStringExpression() [1/2]

template<std::size_t numVars, class Scalar = double>
Dumux::FunctionFromStringExpression< numVars, Scalar >::FunctionFromStringExpression ( const std::string &  expression,
const std::array< std::string, numVars > &  variableNames 
)
inline

◆ FunctionFromStringExpression() [2/2]

template<std::size_t numVars, class Scalar = double>
Dumux::FunctionFromStringExpression< numVars, Scalar >::FunctionFromStringExpression ( const std::string &  expression,
std::string_view  variableNames 
)
inline
Note
Calling FunctionFromStringExpression(expr, "xt") uses "x" and "t" as variable names

Member Function Documentation

◆ operator()() [1/3]

template<std::size_t numVars, class Scalar = double>
template<class S , std::enable_if_t< std::is_convertible_v< Scalar, S >, int > = 0>
Scalar Dumux::FunctionFromStringExpression< numVars, Scalar >::operator() ( const Dune::FieldVector< S, numVars > &  params) const
inline

◆ operator()() [2/3]

template<std::size_t numVars, class Scalar = double>
template<class S , std::enable_if_t< std::is_convertible_v< Scalar, S >, int > = 0>
Scalar Dumux::FunctionFromStringExpression< numVars, Scalar >::operator() ( const std::array< S, numVars > &  params) const
inline

◆ operator()() [3/3]

template<std::size_t numVars, class Scalar = double>
template<class ... Params, std::enable_if_t<(sizeof...(Params)==numVars) &&(std::is_convertible_v< Scalar, std::decay_t< Params > > &&...), int > = 0>
Scalar Dumux::FunctionFromStringExpression< numVars, Scalar >::operator() ( Params &&...  params) const
inline

◆ setVerbosity()

template<std::size_t numVars, class Scalar = double>
void Dumux::FunctionFromStringExpression< numVars, Scalar >::setVerbosity ( unsigned int  v)
inline

Member Data Documentation

◆ numVariables

template<std::size_t numVars, class Scalar = double>
constexpr std::size_t Dumux::FunctionFromStringExpression< numVars, Scalar >::numVariables = numVars
staticconstexpr

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