17 #include <deal.II/base/function_parser.h> 18 #include <deal.II/base/utilities.h> 19 #include <deal.II/base/thread_management.h> 20 #include <deal.II/lac/vector.h> 24 #include <boost/random.hpp> 25 #include <boost/math/special_functions/erf.hpp> 27 #ifdef DEAL_II_WITH_MUPARSER 40 DEAL_II_NAMESPACE_OPEN
46 const double initial_time,
63 #ifdef DEAL_II_WITH_MUPARSER 67 const std::vector<std::string> &expressions,
68 const std::map<std::string, double> &constants,
69 const bool time_dependent)
73 this->constants = constants;
75 this->expressions = expressions;
76 AssertThrow(((time_dependent)?dim+1:dim) == var_names.size(),
86 expressions.size()) );
122 int mu_round(
double val)
124 return static_cast<int>(val + ((val>=0.0) ? 0.5 : -0.5) );
127 double mu_if(
double condition,
double thenvalue,
double elsevalue)
129 if (mu_round(condition))
135 double mu_or(
double left,
double right)
137 return (mu_round(left)) || (mu_round(right));
140 double mu_and(
double left,
double right)
142 return (mu_round(left)) && (mu_round(right));
145 double mu_int(
double value)
147 return static_cast<double>(mu_round(value));
150 double mu_ceil(
double value)
155 double mu_floor(
double value)
160 double mu_cot(
double value)
162 return 1.0/tan(value);
165 double mu_csc(
double value)
167 return 1.0/sin(value);
170 double mu_sec(
double value)
172 return 1.0/cos(value);
175 double mu_log(
double value)
180 double mu_pow(
double a,
double b)
182 return std::pow(a, b);
185 double mu_erfc(
double value)
187 return boost::math::erfc(value);
192 double mu_rand_seed(
double seed)
197 static boost::random::uniform_real_distribution<> uniform_distribution(0,1);
201 static std::map<double, boost::random::mt19937> rng_map;
203 if (rng_map.find(seed) == rng_map.end())
204 rng_map[seed] = boost::random::mt19937(static_cast<unsigned int>(seed));
206 return uniform_distribution(rng_map[seed]);
214 static boost::random::uniform_real_distribution<> uniform_distribution(0,1);
215 static boost::random::mt19937 rng(static_cast<unsigned long>(std::time(
nullptr)));
216 return uniform_distribution(rng);
234 vars.get().resize(var_names.size());
235 for (
unsigned int component=0; component<this->
n_components; ++component)
237 fp.get().emplace_back(
new mu::Parser());
239 for (std::map< std::string, double >::const_iterator constant = constants.begin();
240 constant != constants.end(); ++constant)
242 fp.get()[component]->DefineConst(constant->first.c_str(), constant->second);
245 for (
unsigned int iv=0; iv<var_names.size(); ++iv)
246 fp.get()[component]->DefineVar(var_names[iv].c_str(), &vars.get()[iv]);
249 fp.get()[component]->DefineFun(
"if",internal::mu_if,
true);
250 fp.get()[component]->DefineOprt(
"|", internal::mu_or, 1);
251 fp.get()[component]->DefineOprt(
"&", internal::mu_and, 2);
252 fp.get()[component]->DefineFun(
"int", internal::mu_int,
true);
253 fp.get()[component]->DefineFun(
"ceil", internal::mu_ceil,
true);
254 fp.get()[component]->DefineFun(
"cot", internal::mu_cot,
true);
255 fp.get()[component]->DefineFun(
"csc", internal::mu_csc,
true);
256 fp.get()[component]->DefineFun(
"floor", internal::mu_floor,
true);
257 fp.get()[component]->DefineFun(
"sec", internal::mu_sec,
true);
258 fp.get()[component]->DefineFun(
"log", internal::mu_log,
true);
259 fp.get()[component]->DefineFun(
"pow", internal::mu_pow,
true);
260 fp.get()[component]->DefineFun(
"erfc", internal::mu_erfc,
true);
261 fp.get()[component]->DefineFun(
"rand_seed", internal::mu_rand_seed,
true);
262 fp.get()[component]->DefineFun(
"rand", internal::mu_rand,
true);
273 std::string transformed_expression = expressions[component];
275 const char *function_names[] =
317 for (
unsigned int f=0; f<
sizeof(function_names)/
sizeof(function_names[0]); ++f)
319 const std::string function_name = function_names[f];
320 const unsigned int function_name_length = function_name.size();
322 std::string::size_type pos = 0;
326 pos = transformed_expression.find (function_name, pos);
327 if (pos == std::string::npos)
331 while ((pos+function_name_length<transformed_expression.size())
333 ((transformed_expression[pos+function_name_length] ==
' ')
335 (transformed_expression[pos+function_name_length] ==
'\t')))
336 transformed_expression.erase (transformed_expression.begin()+pos+function_name_length);
340 pos += function_name_length;
345 fp.get()[component]->SetExpr(transformed_expression);
347 catch (mu::ParserError &e)
349 std::cerr <<
"Message: <" << e.GetMsg() <<
">\n";
350 std::cerr <<
"Formula: <" << e.GetExpr() <<
">\n";
351 std::cerr <<
"Token: <" << e.GetToken() <<
">\n";
352 std::cerr <<
"Position: <" << e.GetPos() <<
">\n";
353 std::cerr <<
"Errc: <" << e.GetCode() <<
">" << std::endl;
354 AssertThrow(
false, ExcParseError(e.GetCode(), e.GetMsg().c_str()));
363 const std::string &expression,
364 const std::map<std::string, double> &constants,
365 const bool time_dependent)
368 constants, time_dependent);
375 const unsigned int component)
const 382 if (fp.get().size() == 0)
385 for (
unsigned int i=0; i<dim; ++i)
386 vars.get()[i] = p(i);
388 vars.get()[dim] = this->get_time();
392 return fp.get()[component]->Eval();
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;
401 AssertThrow(
false, ExcParseError(e.GetCode(), e.GetMsg().c_str()));
418 if (fp.get().size() == 0)
421 for (
unsigned int i=0; i<dim; ++i)
422 vars.get()[i] = p(i);
424 vars.get()[dim] = this->get_time();
426 for (
unsigned int component = 0; component < this->
n_components;
428 values(component) = fp.get()[component]->Eval();
437 const std::vector<std::string> &,
438 const std::map<std::string, double> &,
448 const std::map<std::string, double> &,
481 DEAL_II_NAMESPACE_CLOSE
std::vector< std::string > split_string_list(const std::string &s, const std::string &delimiter=",")
static ::ExceptionBase & ExcNeedsFunctionparser()
FunctionParser(const unsigned int n_components=1, const double initial_time=0.0, const double h=1e-8)
virtual double value(const Point< dim > &p, const unsigned int component=0) const
static ::ExceptionBase & ExcNotInitialized()
#define AssertThrow(cond, exc)
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
void init_muparser() const
virtual void vector_value(const Point< dim > &p, Vector< double > &values) const
void initialize(const std::string &vars, const std::vector< std::string > &expressions, const ConstMap &constants, const bool time_dependent=false)
unsigned int n_components(const DoFHandler< dim, spacedim > &dh)
static ::ExceptionBase & ExcInternalError()