28#ifdef DEAL_II_WITH_MUPARSER
41 return static_cast<int>(val + ((val >= 0.0) ? 0.5 : -0.5));
45 mu_if(
double condition,
double thenvalue,
double elsevalue)
54 mu_or(
double left,
double right)
56 return static_cast<double>((
mu_round(left) != 0) ||
63 return static_cast<double>((
mu_round(left) != 0) &&
76 return std::ceil(
value);
82 return std::floor(
value);
118 return std::erf(
value);
124 return std::erfc(
value);
132 static std::mutex rand_mutex;
133 std::lock_guard<std::mutex> lock(rand_mutex);
135 std::uniform_real_distribution<> uniform_distribution(0., 1.);
139 static std::map<double, std::mt19937> rng_map;
141 if (rng_map.find(seed) == rng_map.end())
142 rng_map[seed] = std::mt19937(
static_cast<unsigned int>(seed));
144 return uniform_distribution(rng_map[seed]);
151 static std::mutex rand_mutex;
152 std::lock_guard<std::mutex> lock(rand_mutex);
153 std::uniform_real_distribution<> uniform_distribution(0., 1.);
154 const unsigned int seed =
static_cast<unsigned long>(std::time(
nullptr));
155 static std::mt19937 rng(seed);
156 return uniform_distribution(rng);
159 std::vector<std::string>
204#ifdef DEAL_II_WITH_MUPARSER
211 operator mu::Parser &()
216 operator const mu::Parser &()
const
226 template <
int dim,
typename Number>
234 template <
int dim,
typename Number>
237 const std::string & variables,
238 const std::vector<std::string> & expressions,
239 const std::map<std::string, double> &constants,
240 const bool time_dependent)
242 this->parser_data.clear();
244 this->constants = constants;
246 this->expressions = expressions;
247 AssertThrow(((time_dependent) ? dim + 1 : dim) == this->var_names.size(),
257 this->n_vars = dim + 1;
265 this->init_muparser();
266 this->initialized =
true;
271 template <
int dim,
typename Number>
275#ifdef DEAL_II_WITH_MUPARSER
282 const unsigned int n_components = expressions.size();
285 data.
parsers.reserve(n_components);
286 data.
vars.resize(this->var_names.size());
287 for (
unsigned int component = 0; component < n_components; ++component)
289 data.
parsers.emplace_back(std::make_unique<Parser>());
290 mu::Parser &parser =
dynamic_cast<Parser &
>(*data.
parsers.back());
292 for (
const auto &
constant : this->constants)
295 for (
unsigned int iv = 0; iv < this->var_names.size(); ++iv)
296 parser.DefineVar(this->var_names[iv], &data.
vars[iv]);
299 parser.DefineFun(
"if",
mu_if,
true);
300 parser.DefineOprt(
"|",
mu_or, 1);
301 parser.DefineOprt(
"&",
mu_and, 2);
302 parser.DefineFun(
"int",
mu_int,
true);
303 parser.DefineFun(
"ceil",
mu_ceil,
true);
304 parser.DefineFun(
"cot",
mu_cot,
true);
305 parser.DefineFun(
"csc",
mu_csc,
true);
306 parser.DefineFun(
"floor",
mu_floor,
true);
307 parser.DefineFun(
"sec",
mu_sec,
true);
308 parser.DefineFun(
"log",
mu_log,
true);
309 parser.DefineFun(
"pow",
mu_pow,
true);
310 parser.DefineFun(
"erfc",
mu_erfc,
true);
314 parser.DefineFun(
"rand",
mu_rand,
false);
325 std::string transformed_expression = this->expressions[component];
329 const unsigned int function_name_length =
330 current_function_name.size();
332 std::string::size_type pos = 0;
337 transformed_expression.find(current_function_name, pos);
338 if (pos == std::string::npos)
343 (pos + function_name_length <
344 transformed_expression.size()) &&
345 ((transformed_expression[pos + function_name_length] ==
347 (transformed_expression[pos + function_name_length] ==
349 transformed_expression.erase(
350 transformed_expression.begin() + pos +
351 function_name_length);
355 pos += function_name_length;
360 parser.SetExpr(transformed_expression);
362 catch (mu::ParserError &e)
364 std::cerr <<
"Message: <" << e.GetMsg() <<
">\n";
365 std::cerr <<
"Formula: <" << e.GetExpr() <<
">\n";
366 std::cerr <<
"Token: <" << e.GetToken() <<
">\n";
367 std::cerr <<
"Position: <" << e.GetPos() <<
">\n";
368 std::cerr <<
"Errc: <" << e.GetCode() <<
">" << std::endl;
377 template <
int dim,
typename Number>
381 unsigned int component)
const
383#ifdef DEAL_II_WITH_MUPARSER
389 if (data.
vars.size() == 0)
392 for (
unsigned int i = 0; i < dim; ++i)
394 if (dim != this->n_vars)
395 data.
vars[dim] = time;
402 mu::Parser &parser =
static_cast<Parser &
>(*data.
parsers[component]);
403 return parser.Eval();
405 catch (mu::ParserError &e)
407 std::cerr <<
"Message: <" << e.GetMsg() <<
">\n";
408 std::cerr <<
"Formula: <" << e.GetExpr() <<
">\n";
409 std::cerr <<
"Token: <" << e.GetToken() <<
">\n";
410 std::cerr <<
"Position: <" << e.GetPos() <<
">\n";
411 std::cerr <<
"Errc: <" << e.GetCode() <<
">" << std::endl;
421 return std::numeric_limits<double>::signaling_NaN();
424 template <
int dim,
typename Number>
431#ifdef DEAL_II_WITH_MUPARSER
437 if (data.
vars.size() == 0)
440 for (
unsigned int i = 0; i < dim; ++i)
442 if (dim != this->n_vars)
443 data.
vars[dim] = time;
448 for (
unsigned int component = 0; component < data.
parsers.size();
457 values[component] = parser.Eval();
460 catch (mu::ParserError &e)
462 std::cerr <<
"Message: <" << e.GetMsg() <<
">\n";
463 std::cerr <<
"Formula: <" << e.GetExpr() <<
">\n";
464 std::cerr <<
"Token: <" << e.GetToken() <<
">\n";
465 std::cerr <<
"Position: <" << e.GetPos() <<
">\n";
466 std::cerr <<
"Errc: <" << e.GetCode() <<
">" << std::endl;
478#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