28#ifdef DEAL_II_WITH_MUPARSER
36const std::vector<std::string> &
46 const double initial_time,
56 const std::string &constants,
57 const std::string &variable_names,
90#ifdef DEAL_II_WITH_MUPARSER
95 const std::vector<std::string> &expressions,
96 const std::map<std::string, double> &constants,
97 const bool time_dependent)
101 this->constants = constants;
103 this->expressions = expressions;
104 AssertThrow(((time_dependent) ? dim + 1 : dim) == var_names.size(),
109 AssertThrow(this->n_components == expressions.size(),
110 ExcInvalidExpressionSize(this->n_components, expressions.size()));
146 fp.get().reserve(this->n_components);
147 vars.get().resize(var_names.size());
148 for (
unsigned int component = 0; component < this->n_components; ++component)
150 fp.get().emplace_back(
new mu::Parser());
152 for (
const auto &constant : constants)
154 fp.get()[component]->DefineConst(constant.first, constant.second);
157 for (
unsigned int iv = 0; iv < var_names.size(); ++iv)
158 fp.get()[component]->DefineVar(var_names[iv], &vars.get()[iv]);
161 fp.get()[component]->DefineFun(
"if",
166 fp.get()[component]->DefineFun(
"int",
169 fp.get()[component]->DefineFun(
"ceil",
172 fp.get()[component]->DefineFun(
"cot",
175 fp.get()[component]->DefineFun(
"csc",
178 fp.get()[component]->DefineFun(
"floor",
181 fp.get()[component]->DefineFun(
"sec",
184 fp.get()[component]->DefineFun(
"log",
187 fp.get()[component]->DefineFun(
"pow",
190 fp.get()[component]->DefineFun(
"erfc",
193 fp.get()[component]->DefineFun(
"rand_seed",
196 fp.get()[component]->DefineFun(
"rand",
209 std::string transformed_expression = expressions[component];
211 for (
const auto ¤t_function_name :
214 const unsigned int function_name_length =
215 current_function_name.size();
221 pos = transformed_expression.find(current_function_name, pos);
222 if (pos == std::string::npos)
226 while ((pos + function_name_length <
227 transformed_expression.size()) &&
228 ((transformed_expression[pos + function_name_length] ==
230 (transformed_expression[pos + function_name_length] ==
232 transformed_expression.erase(
233 transformed_expression.begin() + pos +
234 function_name_length);
238 pos += function_name_length;
243 fp.get()[component]->SetExpr(transformed_expression);
245 catch (mu::ParserError &
e)
247 std::cerr <<
"Message: <" <<
e.GetMsg() <<
">\n";
248 std::cerr <<
"Formula: <" <<
e.GetExpr() <<
">\n";
249 std::cerr <<
"Token: <" <<
e.GetToken() <<
">\n";
250 std::cerr <<
"Position: <" <<
e.GetPos() <<
">\n";
251 std::cerr <<
"Errc: <" <<
e.GetCode() <<
">" << std::endl;
262 const std::string & expression,
263 const std::map<std::string, double> &constants,
264 const bool time_dependent)
277 const unsigned int component)
const
283 if (fp.get().size() == 0)
286 for (
unsigned int i = 0; i < dim; ++i)
287 vars.get()[i] = p(i);
293 return fp.get()[component]->Eval();
295 catch (mu::ParserError &
e)
297 std::cerr <<
"Message: <" <<
e.GetMsg() <<
">\n";
298 std::cerr <<
"Formula: <" <<
e.GetExpr() <<
">\n";
299 std::cerr <<
"Token: <" <<
e.GetToken() <<
">\n";
300 std::cerr <<
"Position: <" <<
e.GetPos() <<
">\n";
301 std::cerr <<
"Errc: <" <<
e.GetCode() <<
">" << std::endl;
320 if (fp.get().size() == 0)
323 for (
unsigned int i = 0; i < dim; ++i)
324 vars.get()[i] = p(i);
328 for (
unsigned int component = 0; component < this->n_components; ++component)
329 values(component) = fp.get()[component]->Eval();
338 const std::vector<std::string> &,
339 const std::map<std::string, double> &,
349 const std::map<std::string, double> &,
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 override
const std::vector< std::string > & get_expressions() const
virtual ~FunctionParser() override
void initialize(const std::string &vars, const std::vector< std::string > &expressions, const ConstMap &constants, const bool time_dependent=false)
virtual void vector_value(const Point< dim > &p, Vector< double > &values) const override
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
void init_muparser() const
std::map< std::string, double > constants
static ::ExceptionBase & ExcNeedsFunctionparser()
#define Assert(cond, exc)
static T to_value(const std::string &s, const Patterns::PatternBase &p= *Convert< T >::to_pattern())=delete
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcInternalError()
static const unsigned int max_int_value
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcNotInitialized()
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
types::global_dof_index size_type
SymmetricTensor< 2, dim, Number > e(const Tensor< 2, dim, Number > &F)
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_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)
double mu_cot(double value)
std::vector< std::string > function_names
double mu_int(double value)
double mu_or(double left, double right)
double mu_and(double left, double right)
double mu_erfc(double value)