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 return uniform_distribution(
171 rng_map.try_emplace(seed, std::mt19937(
static_cast<unsigned int>(seed)))
180 static std::mutex rand_mutex;
181 std::lock_guard<std::mutex> lock(rand_mutex);
182 std::uniform_real_distribution<> uniform_distribution(0., 1.);
183 const unsigned int seed =
static_cast<unsigned long>(std::time(
nullptr));
184 static std::mt19937 rng(seed);
185 return uniform_distribution(rng);
190 std::vector<std::string>
235#ifdef DEAL_II_WITH_MUPARSER
242 operator mu::Parser &()
247 operator const mu::Parser &()
const
259 template <
int dim,
typename Number>
267 template <
int dim,
typename Number>
270 const std::string &variables,
271 const std::vector<std::string> &expressions,
272 const std::map<std::string, double> &constants,
273 const bool time_dependent)
275 this->parser_data.clear();
277 this->constants = constants;
279 this->expressions = expressions;
280 AssertThrow(((time_dependent) ? dim + 1 : dim) == this->var_names.size(),
290 this->n_vars = dim + 1;
298 this->init_muparser();
299 this->initialized =
true;
304 template <
int dim,
typename Number>
308#ifdef DEAL_II_WITH_MUPARSER
314 const unsigned int n_components = expressions.size();
317 data.
parsers.reserve(n_components);
318 data.
vars.resize(this->var_names.size());
319 for (
unsigned int component = 0; component < n_components; ++component)
321 data.
parsers.emplace_back(std::make_unique<Parser>());
322 mu::Parser &parser =
dynamic_cast<Parser &
>(*data.
parsers.back());
324 for (
const auto &
constant : this->constants)
327 for (
unsigned int iv = 0; iv < this->var_names.size(); ++iv)
328 parser.DefineVar(this->var_names[iv], &data.
vars[iv]);
331 parser.DefineFun(
"if",
mu_if,
true);
332 parser.DefineOprt(
"|",
mu_or, 1);
333 parser.DefineOprt(
"&",
mu_and, 2);
334 parser.DefineFun(
"int",
mu_int,
true);
335 parser.DefineFun(
"ceil",
mu_ceil,
true);
336 parser.DefineFun(
"cot",
mu_cot,
true);
337 parser.DefineFun(
"csc",
mu_csc,
true);
338 parser.DefineFun(
"floor",
mu_floor,
true);
339 parser.DefineFun(
"sec",
mu_sec,
true);
340 parser.DefineFun(
"log",
mu_log,
true);
341 parser.DefineFun(
"pow",
mu_pow,
true);
342 parser.DefineFun(
"erfc",
mu_erfc,
true);
346 parser.DefineFun(
"rand",
mu_rand,
false);
357 std::string transformed_expression = this->expressions[component];
361 const unsigned int function_name_length =
362 current_function_name.size();
364 std::string::size_type pos = 0;
369 transformed_expression.find(current_function_name, pos);
370 if (pos == std::string::npos)
375 (pos + function_name_length <
376 transformed_expression.size()) &&
377 ((transformed_expression[pos + function_name_length] ==
379 (transformed_expression[pos + function_name_length] ==
381 transformed_expression.erase(
382 transformed_expression.begin() + pos +
383 function_name_length);
387 pos += function_name_length;
392 parser.SetExpr(transformed_expression);
394 catch (mu::ParserError &e)
396 std::cerr <<
"Message: <" << e.GetMsg() <<
">\n";
397 std::cerr <<
"Formula: <" << e.GetExpr() <<
">\n";
398 std::cerr <<
"Token: <" << e.GetToken() <<
">\n";
399 std::cerr <<
"Position: <" << e.GetPos() <<
">\n";
400 std::cerr <<
"Errc: <" << e.GetCode() <<
">" << std::endl;
409 template <
int dim,
typename Number>
413 unsigned int component)
const
415#ifdef DEAL_II_WITH_MUPARSER
421 if (data.
vars.empty())
424 for (
unsigned int i = 0; i < dim; ++i)
426 if (dim != this->n_vars)
427 data.
vars[dim] = time;
434 mu::Parser &parser =
static_cast<Parser &
>(*data.
parsers[component]);
435 return parser.Eval();
437 catch (mu::ParserError &e)
439 std::cerr <<
"Message: <" << e.GetMsg() <<
">\n";
440 std::cerr <<
"Formula: <" << e.GetExpr() <<
">\n";
441 std::cerr <<
"Token: <" << e.GetToken() <<
">\n";
442 std::cerr <<
"Position: <" << e.GetPos() <<
">\n";
443 std::cerr <<
"Errc: <" << e.GetCode() <<
">" << std::endl;
453 return std::numeric_limits<double>::signaling_NaN();
456 template <
int dim,
typename Number>
463#ifdef DEAL_II_WITH_MUPARSER
469 if (data.
vars.empty())
472 for (
unsigned int i = 0; i < dim; ++i)
474 if (dim != this->n_vars)
475 data.
vars[dim] = time;
480 for (
unsigned int component = 0; component < data.
parsers.size();
489 values[component] = parser.Eval();
492 catch (mu::ParserError &e)
494 std::cerr <<
"Message: <" << e.GetMsg() <<
">\n";
495 std::cerr <<
"Formula: <" << e.GetExpr() <<
">\n";
496 std::cerr <<
"Token: <" << e.GetToken() <<
">\n";
497 std::cerr <<
"Position: <" << e.GetPos() <<
">\n";
498 std::cerr <<
"Errc: <" << e.GetCode() <<
">" << std::endl;
510#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