27#ifdef DEAL_II_WITH_MUPARSER
40 return static_cast<int>(val + ((val >= 0.0) ? 0.5 : -0.5));
44 mu_if(
double condition,
double thenvalue,
double elsevalue)
53 mu_or(
double left,
double right)
55 return static_cast<double>((
mu_round(left) != 0) ||
62 return static_cast<double>((
mu_round(left) != 0) &&
75 return std::ceil(
value);
81 return std::floor(
value);
117 return std::erf(
value);
123 return std::erfc(
value);
131 static std::mutex rand_mutex;
132 std::lock_guard<std::mutex> lock(rand_mutex);
134 std::uniform_real_distribution<> uniform_distribution(0., 1.);
138 static std::map<double, std::mt19937> rng_map;
140 if (rng_map.find(seed) == rng_map.end())
141 rng_map[seed] = std::mt19937(
static_cast<unsigned int>(seed));
143 return uniform_distribution(rng_map[seed]);
150 static std::mutex rand_mutex;
151 std::lock_guard<std::mutex> lock(rand_mutex);
152 std::uniform_real_distribution<> uniform_distribution(0., 1.);
153 const unsigned int seed =
static_cast<unsigned long>(std::time(
nullptr));
154 static std::mt19937 rng(seed);
155 return uniform_distribution(rng);
158 std::vector<std::string>
203#ifdef DEAL_II_WITH_MUPARSER
210 operator mu::Parser &()
215 operator const mu::Parser &()
const
225 template <
int dim,
typename Number>
233 template <
int dim,
typename Number>
236 const std::string & variables,
237 const std::vector<std::string> & expressions,
238 const std::map<std::string, double> &constants,
239 const bool time_dependent)
241 this->parser_data.clear();
243 this->constants = constants;
245 this->expressions = expressions;
246 AssertThrow(((time_dependent) ? dim + 1 : dim) == this->var_names.size(),
256 this->n_vars = dim + 1;
264 this->init_muparser();
265 this->initialized =
true;
270 template <
int dim,
typename Number>
274#ifdef DEAL_II_WITH_MUPARSER
281 const unsigned int n_components = expressions.size();
284 data.
parsers.reserve(n_components);
285 data.
vars.resize(this->var_names.size());
286 for (
unsigned int component = 0; component < n_components; ++component)
288 data.
parsers.emplace_back(std::make_unique<Parser>());
289 mu::Parser &parser =
dynamic_cast<Parser &
>(*data.
parsers.back());
291 for (
const auto &
constant : this->constants)
294 for (
unsigned int iv = 0; iv < this->var_names.size(); ++iv)
295 parser.DefineVar(this->var_names[iv], &data.
vars[iv]);
298 parser.DefineFun(
"if",
mu_if,
true);
299 parser.DefineOprt(
"|",
mu_or, 1);
300 parser.DefineOprt(
"&",
mu_and, 2);
301 parser.DefineFun(
"int",
mu_int,
true);
302 parser.DefineFun(
"ceil",
mu_ceil,
true);
303 parser.DefineFun(
"cot",
mu_cot,
true);
304 parser.DefineFun(
"csc",
mu_csc,
true);
305 parser.DefineFun(
"floor",
mu_floor,
true);
306 parser.DefineFun(
"sec",
mu_sec,
true);
307 parser.DefineFun(
"log",
mu_log,
true);
308 parser.DefineFun(
"pow",
mu_pow,
true);
309 parser.DefineFun(
"erfc",
mu_erfc,
true);
311 parser.DefineFun(
"rand",
mu_rand,
true);
322 std::string transformed_expression = this->expressions[component];
326 const unsigned int function_name_length =
327 current_function_name.size();
329 std::string::size_type pos = 0;
334 transformed_expression.find(current_function_name, pos);
335 if (pos == std::string::npos)
340 (pos + function_name_length <
341 transformed_expression.size()) &&
342 ((transformed_expression[pos + function_name_length] ==
344 (transformed_expression[pos + function_name_length] ==
346 transformed_expression.erase(
347 transformed_expression.begin() + pos +
348 function_name_length);
352 pos += function_name_length;
357 parser.SetExpr(transformed_expression);
359 catch (mu::ParserError &e)
361 std::cerr <<
"Message: <" << e.GetMsg() <<
">\n";
362 std::cerr <<
"Formula: <" << e.GetExpr() <<
">\n";
363 std::cerr <<
"Token: <" << e.GetToken() <<
">\n";
364 std::cerr <<
"Position: <" << e.GetPos() <<
">\n";
365 std::cerr <<
"Errc: <" << e.GetCode() <<
">" << std::endl;
374 template <
int dim,
typename Number>
378 unsigned int component)
const
380#ifdef DEAL_II_WITH_MUPARSER
386 if (data.
vars.size() == 0)
389 for (
unsigned int i = 0; i < dim; ++i)
391 if (dim != this->n_vars)
392 data.
vars[dim] = time;
399 mu::Parser &parser =
static_cast<Parser &
>(*data.
parsers[component]);
400 return parser.Eval();
402 catch (mu::ParserError &e)
404 std::cerr <<
"Message: <" << e.GetMsg() <<
">\n";
405 std::cerr <<
"Formula: <" << e.GetExpr() <<
">\n";
406 std::cerr <<
"Token: <" << e.GetToken() <<
">\n";
407 std::cerr <<
"Position: <" << e.GetPos() <<
">\n";
408 std::cerr <<
"Errc: <" << e.GetCode() <<
">" << std::endl;
418 return std::numeric_limits<double>::signaling_NaN();
421 template <
int dim,
typename Number>
428#ifdef DEAL_II_WITH_MUPARSER
434 if (data.
vars.size() == 0)
437 for (
unsigned int i = 0; i < dim; ++i)
439 if (dim != this->n_vars)
440 data.
vars[dim] = time;
445 for (
unsigned int component = 0; component < data.
parsers.size();
454 values[component] = parser.Eval();
457 catch (mu::ParserError &e)
459 std::cerr <<
"Message: <" << e.GetMsg() <<
">\n";
460 std::cerr <<
"Formula: <" << e.GetExpr() <<
">\n";
461 std::cerr <<
"Token: <" << e.GetToken() <<
">\n";
462 std::cerr <<
"Position: <" << e.GetPos() <<
">\n";
463 std::cerr <<
"Errc: <" << e.GetCode() <<
">" << std::endl;
475#include "mu_parser_internal.inst"
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
std::vector< double > vars
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
static ::ExceptionBase & ExcNeedsFunctionparser()
std::vector< std::unique_ptr< muParserBase > > parsers
#define Assert(cond, exc)
static ::ExceptionBase & ExcParseError(int arg1, std::string arg2)
#define AssertDimension(dim1, dim2)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcNotInitialized()
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)
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)