28 #ifdef DEAL_II_WITH_MUPARSER
29 # include <muParser.h>
35 template <
int rank,
int dim,
typename Number>
36 const std::vector<std::string> &
44 template <
int rank,
int dim,
typename Number>
46 const double initial_time)
54 template <
int rank,
int dim,
typename Number>
56 const std::string &expression,
57 const std::string &constants,
58 const std::string &variable_names)
83 template <
int rank,
int dim,
typename Number>
87 #ifdef DEAL_II_WITH_MUPARSER
89 template <
int rank,
int dim,
typename Number>
92 const std::string & variables,
93 const std::vector<std::string> & expressions,
94 const std::map<std::string, double> &constants,
95 const bool time_dependent)
99 this->constants = constants;
101 this->expressions = expressions;
102 AssertThrow(((time_dependent) ? dim + 1 : dim) == var_names.size(),
111 ExcInvalidExpressionSize(this->
n_components, expressions.size()));
144 template <
int rank,
int dim,
typename Number>
156 vars.get().resize(var_names.size());
157 for (
unsigned int component = 0; component < this->
n_components; ++component)
159 tfp.get().emplace_back(
new mu::Parser());
161 for (
const auto &constant : constants)
163 tfp.get()[component]->DefineConst(constant.first, constant.second);
166 for (
unsigned int iv = 0; iv < var_names.size(); ++iv)
167 tfp.get()[component]->DefineVar(var_names[iv], &vars.get()[iv]);
170 tfp.get()[component]->DefineFun(
"if",
174 tfp.get()[component]->DefineOprt(
"&",
177 tfp.get()[component]->DefineFun(
"int",
180 tfp.get()[component]->DefineFun(
"ceil",
183 tfp.get()[component]->DefineFun(
"cot",
186 tfp.get()[component]->DefineFun(
"csc",
189 tfp.get()[component]->DefineFun(
"floor",
192 tfp.get()[component]->DefineFun(
"sec",
195 tfp.get()[component]->DefineFun(
"log",
198 tfp.get()[component]->DefineFun(
"pow",
201 tfp.get()[component]->DefineFun(
"erfc",
204 tfp.get()[component]->DefineFun(
"rand_seed",
207 tfp.get()[component]->DefineFun(
"rand",
220 std::string transformed_expression = expressions[component];
222 for (
const auto ¤t_function_name :
225 const unsigned int function_name_length =
226 current_function_name.size();
232 pos = transformed_expression.find(current_function_name, pos);
233 if (pos == std::string::npos)
237 while ((pos + function_name_length <
238 transformed_expression.size()) &&
239 ((transformed_expression[pos + function_name_length] ==
241 (transformed_expression[pos + function_name_length] ==
243 transformed_expression.erase(
244 transformed_expression.begin() + pos +
245 function_name_length);
249 pos += function_name_length;
254 tfp.get()[component]->SetExpr(transformed_expression);
256 catch (mu::ParserError &
e)
258 std::cerr <<
"Message: <" <<
e.GetMsg() <<
">\n";
259 std::cerr <<
"Formula: <" <<
e.GetExpr() <<
">\n";
260 std::cerr <<
"Token: <" <<
e.GetToken() <<
">\n";
261 std::cerr <<
"Position: <" <<
e.GetPos() <<
">\n";
262 std::cerr <<
"Errc: <" <<
e.GetCode() <<
">" << std::endl;
270 template <
int rank,
int dim,
typename Number>
273 const std::string & vars,
274 const std::string & expression,
275 const std::map<std::string, double> &constants,
276 const bool time_dependent)
286 template <
int rank,
int dim,
typename Number>
293 if (tfp.get().size() == 0)
296 for (
unsigned int i = 0; i < dim; ++i)
297 vars.get()[i] = p(i);
306 unsigned int component = 0;
307 for (Number *value_ptr =
value.begin_raw(); value_ptr !=
value.end_raw();
310 *value_ptr = tfp.get()[component]->Eval();
314 catch (mu::ParserError &
e)
316 std::cerr <<
"Message: <" <<
e.GetMsg() <<
">\n";
317 std::cerr <<
"Formula: <" <<
e.GetExpr() <<
">\n";
318 std::cerr <<
"Token: <" <<
e.GetToken() <<
">\n";
319 std::cerr <<
"Position: <" <<
e.GetPos() <<
">\n";
320 std::cerr <<
"Errc: <" <<
e.GetCode() <<
">" << std::endl;
331 template <
int rank,
int dim,
typename Number>
337 Assert(p.size() == values.size(),
340 for (
unsigned int i = 0; i < p.size(); ++i)
342 values[i] =
value(p[i]);
349 template <
int rank,
int dim,
typename Number>
353 const std::vector<std::string> &,
354 const std::map<std::string, double> &,
360 template <
int rank,
int dim,
typename Number>
365 const std::map<std::string, double> &,
373 template <
int rank,
int dim,
typename Number>
383 template <
int rank,
int dim,
typename Number>
396 #include "tensor_function_parser.inst"