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 DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
25 #include <boost/random.hpp> 28 #ifdef DEAL_II_WITH_MUPARSER 29 DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
43 DEAL_II_NAMESPACE_OPEN
49 const double initial_time)
51 Function<dim>(n_components, initial_time)
60 #ifdef DEAL_II_WITH_MUPARSER 65 const std::map<std::string, double> &
constants,
66 const bool time_dependent)
83 expressions.size()) );
119 int mu_round(
double val)
121 return static_cast<int>(val + ((val>=0.0) ? 0.5 : -0.5) );
124 double mu_if(
double condition,
double thenvalue,
double elsevalue)
126 if (mu_round(condition))
132 double mu_or(
double left,
double right)
134 return (mu_round(left)) || (mu_round(right));
137 double mu_and(
double left,
double right)
139 return (mu_round(left)) && (mu_round(right));
142 double mu_int(
double value)
144 return static_cast<double>(mu_round(value));
147 double mu_ceil(
double value)
152 double mu_floor(
double value)
157 double mu_cot(
double value)
159 return 1.0/tan(value);
162 double mu_csc(
double value)
164 return 1.0/sin(value);
167 double mu_sec(
double value)
169 return 1.0/cos(value);
172 double mu_log(
double value)
177 double mu_pow(
double a,
double b)
179 return std::pow(a, b);
182 double mu_erfc(
double value)
189 double mu_rand_seed(
double seed)
194 static boost::random::uniform_real_distribution<> uniform_distribution(0,1);
198 static std::map<double, boost::random::mt19937> rng_map;
200 if (rng_map.find(seed) == rng_map.end())
201 rng_map[seed] = boost::random::mt19937(static_cast<unsigned int>(seed));
203 return uniform_distribution(rng_map[seed]);
211 static boost::random::uniform_real_distribution<> uniform_distribution(0,1);
212 static boost::random::mt19937 rng(static_cast<unsigned long>(std::time(0)));
213 return uniform_distribution(rng);
231 for (
unsigned int component=0; component<this->
n_components; ++component)
233 for (std::map< std::string, double >::const_iterator constant =
constants.begin();
236 fp.
get()[component].DefineConst(constant->first.c_str(), constant->second);
239 for (
unsigned int iv=0; iv<
var_names.size(); ++iv)
243 fp.
get()[component].DefineFun(
"if",internal::mu_if,
true);
244 fp.
get()[component].DefineOprt(
"|", internal::mu_or, 1);
245 fp.
get()[component].DefineOprt(
"&", internal::mu_and, 2);
246 fp.
get()[component].DefineFun(
"int", internal::mu_int,
true);
247 fp.
get()[component].DefineFun(
"ceil", internal::mu_ceil,
true);
248 fp.
get()[component].DefineFun(
"cot", internal::mu_cot,
true);
249 fp.
get()[component].DefineFun(
"csc", internal::mu_csc,
true);
250 fp.
get()[component].DefineFun(
"floor", internal::mu_floor,
true);
251 fp.
get()[component].DefineFun(
"sec", internal::mu_sec,
true);
252 fp.
get()[component].DefineFun(
"log", internal::mu_log,
true);
253 fp.
get()[component].DefineFun(
"pow", internal::mu_pow,
true);
254 fp.
get()[component].DefineFun(
"erfc", internal::mu_erfc,
true);
255 fp.
get()[component].DefineFun(
"rand_seed", internal::mu_rand_seed,
true);
256 fp.
get()[component].DefineFun(
"rand", internal::mu_rand,
true);
267 std::string transformed_expression =
expressions[component];
269 const char *function_names[] =
311 for (
unsigned int f=0; f<
sizeof(function_names)/
sizeof(function_names[0]); ++f)
313 const std::string function_name = function_names[f];
314 const unsigned int function_name_length = function_name.size();
316 std::string::size_type pos = 0;
320 pos = transformed_expression.find (function_name, pos);
321 if (pos == std::string::npos)
325 while ((pos+function_name_length<transformed_expression.size())
327 ((transformed_expression[pos+function_name_length] ==
' ')
329 (transformed_expression[pos+function_name_length] ==
'\t')))
330 transformed_expression.erase (transformed_expression.begin()+pos+function_name_length);
334 pos += function_name_length;
339 fp.
get()[component].SetExpr(transformed_expression);
341 catch (mu::ParserError &e)
343 std::cerr <<
"Message: <" << e.GetMsg() <<
">\n";
344 std::cerr <<
"Formula: <" << e.GetExpr() <<
">\n";
345 std::cerr <<
"Token: <" << e.GetToken() <<
">\n";
346 std::cerr <<
"Position: <" << e.GetPos() <<
">\n";
347 std::cerr <<
"Errc: <" << e.GetCode() <<
">" << std::endl;
348 AssertThrow(
false, ExcParseError(e.GetCode(), e.GetMsg().c_str()));
357 const std::string &expression,
358 const std::map<std::string, double> &
constants,
359 const bool time_dependent)
362 constants, time_dependent);
369 const unsigned int component)
const 373 ExcIndexRange(component, 0, this->n_components));
376 if (
fp.
get().size() == 0)
379 for (
unsigned int i=0; i<dim; ++i)
386 return fp.
get()[component].Eval();
388 catch (mu::ParserError &e)
390 std::cerr <<
"Message: <" << e.GetMsg() <<
">\n";
391 std::cerr <<
"Formula: <" << e.GetExpr() <<
">\n";
392 std::cerr <<
"Token: <" << e.GetToken() <<
">\n";
393 std::cerr <<
"Position: <" << e.GetPos() <<
">\n";
394 std::cerr <<
"Errc: <" << e.GetCode() <<
">" << std::endl;
395 AssertThrow(
false, ExcParseError(e.GetCode(), e.GetMsg().c_str()));
412 if (
fp.
get().size() == 0)
415 for (
unsigned int i=0; i<dim; ++i)
420 for (
unsigned int component = 0; component < this->
n_components;
422 values(component) =
fp.
get()[component].Eval();
431 const std::vector<std::string> &,
432 const std::map<std::string, double> &,
442 const std::map<std::string, double> &,
475 DEAL_II_NAMESPACE_CLOSE
virtual double value(const Point< dim > &p, const unsigned int component=0) const
::ExceptionBase & ExcMessage(std::string arg1)
Threads::ThreadLocalStorage< std::vector< mu::Parser > > fp
#define AssertThrow(cond, exc)
const unsigned int n_components
#define Assert(cond, exc)
std::vector< std::string > split_string_list(const std::string &s, const char delimiter=',')
Threads::ThreadLocalStorage< std::vector< double > > vars
void init_muparser() const
::ExceptionBase & ExcNeedsFunctionparser()
virtual void vector_value(const Point< dim > &p, Vector< double > &values) const
std::vector< std::string > var_names
void initialize(const std::string &vars, const std::vector< std::string > &expressions, const ConstMap &constants, const bool time_dependent=false)
std::vector< std::string > expressions
FunctionParser(const unsigned int n_components=1, const double initial_time=0.0)
std::map< std::string, double > constants