28#ifdef DEAL_II_WITH_MUPARSER
35template <
int rank,
int dim,
typename Number>
36const std::vector<std::string> &
44template <
int rank,
int dim,
typename Number>
46 const double initial_time)
54template <
int rank,
int dim,
typename Number>
56 const std::string &expression,
57 const std::string &constants,
58 const std::string &variable_names)
83template <
int rank,
int dim,
typename Number>
87#ifdef DEAL_II_WITH_MUPARSER
89template <
int rank,
int dim,
typename Number>
92 const std::string & variables,
93 const std::vector<std::string> & expressions,
94 const std::map<std::string, double> &constants,
95 const bool time_dependent)
99 this->constants = constants;
101 this->expressions = expressions;
102 AssertThrow(((time_dependent) ? dim + 1 : dim) == var_names.size(),
110 AssertThrow(this->n_components == expressions.size(),
111 ExcInvalidExpressionSize(this->n_components, expressions.size()));
144template <
int rank,
int dim,
typename Number>
155 tfp.get().reserve(this->n_components);
156 vars.get().resize(var_names.size());
157 for (
unsigned int component = 0; component < this->n_components; ++component)
159 tfp.get().emplace_back(
new mu::Parser());
161 for (
const auto &constant : constants)
163 tfp.get()[component]->DefineConst(constant.first, constant.second);
166 for (
unsigned int iv = 0; iv < var_names.size(); ++iv)
167 tfp.get()[component]->DefineVar(var_names[iv], &vars.get()[iv]);
170 tfp.get()[component]->DefineFun(
"if",
174 tfp.get()[component]->DefineOprt(
"&",
177 tfp.get()[component]->DefineFun(
"int",
180 tfp.get()[component]->DefineFun(
"ceil",
183 tfp.get()[component]->DefineFun(
"cot",
186 tfp.get()[component]->DefineFun(
"csc",
189 tfp.get()[component]->DefineFun(
"floor",
192 tfp.get()[component]->DefineFun(
"sec",
195 tfp.get()[component]->DefineFun(
"log",
198 tfp.get()[component]->DefineFun(
"pow",
201 tfp.get()[component]->DefineFun(
"erfc",
204 tfp.get()[component]->DefineFun(
"rand_seed",
207 tfp.get()[component]->DefineFun(
"rand",
220 std::string transformed_expression = expressions[component];
222 for (
const auto ¤t_function_name :
225 const unsigned int function_name_length =
226 current_function_name.size();
232 pos = transformed_expression.find(current_function_name, pos);
233 if (pos == std::string::npos)
237 while ((pos + function_name_length <
238 transformed_expression.size()) &&
239 ((transformed_expression[pos + function_name_length] ==
241 (transformed_expression[pos + function_name_length] ==
243 transformed_expression.erase(
244 transformed_expression.begin() + pos +
245 function_name_length);
249 pos += function_name_length;
254 tfp.get()[component]->SetExpr(transformed_expression);
256 catch (mu::ParserError &
e)
258 std::cerr <<
"Message: <" <<
e.GetMsg() <<
">\n";
259 std::cerr <<
"Formula: <" <<
e.GetExpr() <<
">\n";
260 std::cerr <<
"Token: <" <<
e.GetToken() <<
">\n";
261 std::cerr <<
"Position: <" <<
e.GetPos() <<
">\n";
262 std::cerr <<
"Errc: <" <<
e.GetCode() <<
">" << std::endl;
270template <
int rank,
int dim,
typename Number>
273 const std::string & vars,
274 const std::string & expression,
275 const std::map<std::string, double> &constants,
276 const bool time_dependent)
286template <
int rank,
int dim,
typename Number>
293 if (tfp.get().size() == 0)
296 for (
unsigned int i = 0; i < dim; ++i)
297 vars.get()[i] = p(i);
306 unsigned int component = 0;
307 for (Number *value_ptr = value.begin_raw(); value_ptr != value.end_raw();
310 *value_ptr = tfp.get()[component]->Eval();
314 catch (mu::ParserError &
e)
316 std::cerr <<
"Message: <" <<
e.GetMsg() <<
">\n";
317 std::cerr <<
"Formula: <" <<
e.GetExpr() <<
">\n";
318 std::cerr <<
"Token: <" <<
e.GetToken() <<
">\n";
319 std::cerr <<
"Position: <" <<
e.GetPos() <<
">\n";
320 std::cerr <<
"Errc: <" <<
e.GetCode() <<
">" << std::endl;
331template <
int rank,
int dim,
typename Number>
340 for (
unsigned int i = 0; i < p.size(); ++i)
349template <
int rank,
int dim,
typename Number>
353 const std::vector<std::string> &,
354 const std::map<std::string, double> &,
360template <
int rank,
int dim,
typename Number>
365 const std::map<std::string, double> &,
373template <
int rank,
int dim,
typename Number>
383template <
int rank,
int dim,
typename Number>
396#include "tensor_function_parser.inst"
TensorFunctionParser(const double initial_time=0.0)
virtual Tensor< rank, dim, Number > value(const Point< dim > &p) const override
void initialize(const std::string &vars, const std::vector< std::string > &expressions, const ConstMap &constants, const bool time_dependent=false)
virtual void value_list(const std::vector< Point< dim > > &p, std::vector< Tensor< rank, dim, Number > > &values) const override
virtual ~TensorFunctionParser() override
const std::vector< std::string > & get_expressions() const
VectorizedArray< Number, width > pow(const ::VectorizedArray< Number, width > &x, const Number p)
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
static ::ExceptionBase & ExcNeedsFunctionparser()
std::map< std::string, double > constants
#define Assert(cond, exc)
static T to_value(const std::string &s, const Patterns::PatternBase &p= *Convert< T >::to_pattern())=delete
static ::ExceptionBase & ExcInternalError()
static const unsigned int max_int_value
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcNotInitialized()
static ::ExceptionBase & ExcMessage(std::string arg1)
void init_muparser() const
#define AssertThrow(cond, exc)
types::global_dof_index size_type
SymmetricTensor< 2, dim, Number > e(const Tensor< 2, dim, Number > &F)
std::vector< std::string > split_string_list(const std::string &s, const std::string &delimiter=",")
double mu_if(double condition, double thenvalue, double elsevalue)
double mu_sec(double value)
double mu_floor(double value)
double mu_csc(double value)
double mu_pow(double a, double b)
double mu_log(double value)
double mu_rand_seed(double seed)
double mu_ceil(double value)
double mu_cot(double value)
std::vector< std::string > function_names
double mu_int(double value)
double mu_or(double left, double right)
double mu_and(double left, double right)
double mu_erfc(double value)