27#ifdef DEAL_II_WITH_MUPARSER
40 return static_cast<int>(val + ((val >= 0.0) ? 0.5 : -0.5));
47 const double thenvalue,
48 const double elsevalue)
59 mu_or(
const double left,
const double right)
61 return static_cast<double>((
mu_round(left) != 0) ||
68 mu_and(
const double left,
const double right)
70 return static_cast<double>((
mu_round(left) != 0) &&
87 return std::ceil(
value);
95 return std::floor(
value);
143 return std::erf(
value);
151 return std::erfc(
value);
161 static std::mutex rand_mutex;
162 std::lock_guard<std::mutex> lock(rand_mutex);
164 std::uniform_real_distribution<> uniform_distribution(0., 1.);
168 static std::map<double, std::mt19937> rng_map;
170 if (rng_map.find(seed) == rng_map.end())
171 rng_map[seed] = std::mt19937(
static_cast<unsigned int>(seed));
173 return uniform_distribution(rng_map[seed]);
181 static std::mutex rand_mutex;
182 std::lock_guard<std::mutex> lock(rand_mutex);
183 std::uniform_real_distribution<> uniform_distribution(0., 1.);
184 const unsigned int seed =
static_cast<unsigned long>(std::time(
nullptr));
185 static std::mt19937 rng(seed);
186 return uniform_distribution(rng);
191 std::vector<std::string>
236#ifdef DEAL_II_WITH_MUPARSER
243 operator mu::Parser &()
248 operator const mu::Parser &()
const
260 template <
int dim,
typename Number>
268 template <
int dim,
typename Number>
271 const std::string &variables,
272 const std::vector<std::string> &expressions,
273 const std::map<std::string, double> &constants,
274 const bool time_dependent)
276 this->parser_data.clear();
278 this->constants = constants;
280 this->expressions = expressions;
281 AssertThrow(((time_dependent) ? dim + 1 : dim) == this->var_names.size(),
291 this->n_vars = dim + 1;
299 this->init_muparser();
300 this->initialized =
true;
305 template <
int dim,
typename Number>
309#ifdef DEAL_II_WITH_MUPARSER
315 const unsigned int n_components = expressions.size();
318 data.
parsers.reserve(n_components);
319 data.
vars.resize(this->var_names.size());
320 for (
unsigned int component = 0; component < n_components; ++component)
322 data.
parsers.emplace_back(std::make_unique<Parser>());
323 mu::Parser &parser =
dynamic_cast<Parser &
>(*data.
parsers.back());
325 for (
const auto &
constant : this->constants)
328 for (
unsigned int iv = 0; iv < this->var_names.size(); ++iv)
329 parser.DefineVar(this->var_names[iv], &data.
vars[iv]);
332 parser.DefineFun(
"if",
mu_if,
true);
333 parser.DefineOprt(
"|",
mu_or, 1);
334 parser.DefineOprt(
"&",
mu_and, 2);
335 parser.DefineFun(
"int",
mu_int,
true);
336 parser.DefineFun(
"ceil",
mu_ceil,
true);
337 parser.DefineFun(
"cot",
mu_cot,
true);
338 parser.DefineFun(
"csc",
mu_csc,
true);
339 parser.DefineFun(
"floor",
mu_floor,
true);
340 parser.DefineFun(
"sec",
mu_sec,
true);
341 parser.DefineFun(
"log",
mu_log,
true);
342 parser.DefineFun(
"pow",
mu_pow,
true);
343 parser.DefineFun(
"erfc",
mu_erfc,
true);
347 parser.DefineFun(
"rand",
mu_rand,
false);
358 std::string transformed_expression = this->expressions[component];
362 const unsigned int function_name_length =
363 current_function_name.size();
365 std::string::size_type pos = 0;
370 transformed_expression.find(current_function_name, pos);
371 if (pos == std::string::npos)
376 (pos + function_name_length <
377 transformed_expression.size()) &&
378 ((transformed_expression[pos + function_name_length] ==
380 (transformed_expression[pos + function_name_length] ==
382 transformed_expression.erase(
383 transformed_expression.begin() + pos +
384 function_name_length);
388 pos += function_name_length;
393 parser.SetExpr(transformed_expression);
395 catch (mu::ParserError &e)
397 std::cerr <<
"Message: <" << e.GetMsg() <<
">\n";
398 std::cerr <<
"Formula: <" << e.GetExpr() <<
">\n";
399 std::cerr <<
"Token: <" << e.GetToken() <<
">\n";
400 std::cerr <<
"Position: <" << e.GetPos() <<
">\n";
401 std::cerr <<
"Errc: <" << e.GetCode() <<
">" << std::endl;
410 template <
int dim,
typename Number>
414 unsigned int component)
const
416#ifdef DEAL_II_WITH_MUPARSER
422 if (data.
vars.empty())
425 for (
unsigned int i = 0; i < dim; ++i)
427 if (dim != this->n_vars)
428 data.
vars[dim] = time;
435 mu::Parser &parser =
static_cast<Parser &
>(*data.
parsers[component]);
436 return parser.Eval();
438 catch (mu::ParserError &e)
440 std::cerr <<
"Message: <" << e.GetMsg() <<
">\n";
441 std::cerr <<
"Formula: <" << e.GetExpr() <<
">\n";
442 std::cerr <<
"Token: <" << e.GetToken() <<
">\n";
443 std::cerr <<
"Position: <" << e.GetPos() <<
">\n";
444 std::cerr <<
"Errc: <" << e.GetCode() <<
">" << std::endl;
454 return std::numeric_limits<double>::signaling_NaN();
457 template <
int dim,
typename Number>
464#ifdef DEAL_II_WITH_MUPARSER
470 if (data.
vars.empty())
473 for (
unsigned int i = 0; i < dim; ++i)
475 if (dim != this->n_vars)
476 data.
vars[dim] = time;
481 for (
unsigned int component = 0; component < data.
parsers.size();
490 values[component] = parser.Eval();
493 catch (mu::ParserError &e)
495 std::cerr <<
"Message: <" << e.GetMsg() <<
">\n";
496 std::cerr <<
"Formula: <" << e.GetExpr() <<
">\n";
497 std::cerr <<
"Token: <" << e.GetToken() <<
">\n";
498 std::cerr <<
"Position: <" << e.GetPos() <<
">\n";
499 std::cerr <<
"Errc: <" << e.GetCode() <<
">" << std::endl;
511#include "mu_parser_internal.inst"
Number do_value(const Point< dim > &p, const double time, unsigned int component) const
void init_muparser() const
void do_all_values(const Point< dim > &p, const double time, ArrayView< Number > &values) const
virtual void initialize(const std::string &vars, const std::vector< std::string > &expressions, const std::map< std::string, double > &constants, const bool time_dependent=false)
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
static ::ExceptionBase & ExcNeedsFunctionparser()
#define Assert(cond, exc)
static ::ExceptionBase & ExcParseError(int arg1, std::string arg2)
#define AssertDimension(dim1, dim2)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcNotInitialized()
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
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_erf(double value)
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)
std::vector< std::string > get_function_names()
double mu_cot(double value)
double mu_int(double value)
double mu_or(double left, double right)
double mu_and(double left, double right)
double mu_erfc(double value)
::VectorizedArray< Number, width > log(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > tan(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > cos(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > sin(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > pow(const ::VectorizedArray< Number, width > &, const Number p)
std::vector< double > vars
std::vector< std::unique_ptr< muParserBase > > parsers