From 4611425cbf7d521a4e5a46314efb1d18e515f155 Mon Sep 17 00:00:00 2001 From: heister Date: Sat, 14 Dec 2013 20:00:35 +0000 Subject: [PATCH] implement muparser (units are not working) git-svn-id: https://svn.dealii.org/branches/branch_muparser@32011 0785d39b-7218-0410-832d-ea1e28bc413d --- .../include/deal.II/base/function_parser.h | 13 +++- deal.II/source/base/function_parser.cc | 68 ++++++++++++++++++- 2 files changed, 77 insertions(+), 4 deletions(-) diff --git a/deal.II/include/deal.II/base/function_parser.h b/deal.II/include/deal.II/base/function_parser.h index 7f1429eae4..a5b6d8f613 100644 --- a/deal.II/include/deal.II/base/function_parser.h +++ b/deal.II/include/deal.II/base/function_parser.h @@ -26,10 +26,17 @@ #include #include +#ifdef DEAL_II_WITH_MUPARSER +namespace mu +{ + class Parser; +} +#else namespace fparser { class FunctionParser; } +#endif DEAL_II_NAMESPACE_OPEN @@ -541,8 +548,12 @@ private: * the array equals the number of * vector components. */ +#ifdef DEAL_II_WITH_MUPARSER + std::vector var_names; + mu::Parser *fp; +#else fparser::FunctionParser *fp; - +#endif /** * State of usability. This * variable is checked every time diff --git a/deal.II/source/base/function_parser.cc b/deal.II/source/base/function_parser.cc index 285ff50a7a..9c08475d13 100644 --- a/deal.II/source/base/function_parser.cc +++ b/deal.II/source/base/function_parser.cc @@ -19,7 +19,12 @@ #include #include -#ifdef DEAL_II_WITH_FUNCTIONPARSER + +#ifdef DEAL_II_WITH_MUPARSER +#include +#include + +#elif defined(DEAL_II_WITH_FUNCTIONPARSER) #include namespace fparser { @@ -47,7 +52,12 @@ FunctionParser::FunctionParser(const unsigned int n_components, Function(n_components, initial_time), fp (0) { + +#ifdef DEAL_II_WITH_MUPARSER + fp = new mu::Parser[n_components]; +#else fp = new fparser::FunctionParser[n_components]; +#endif } @@ -75,6 +85,11 @@ void FunctionParser::initialize (const std::string &varia use_degrees); } +double constant_eval(double x, double c) +{ + return x*c; +} + template @@ -94,7 +109,10 @@ void FunctionParser::initialize (const std::string &variables, ExcInvalidExpressionSize(this->n_components, expressions.size()) ); - +#ifdef DEAL_II_WITH_MUPARSER + var_names = Utilities::split_string_list(variables, ','); + AssertThrow(((time_dependent)?dim+1:dim) == var_names.size(), ExcMessage("wrong number of variables")); +#endif for (unsigned int i=0; in_components; ++i) { @@ -104,31 +122,52 @@ void FunctionParser::initialize (const std::string &variables, std::map< std::string, double >::const_iterator unit = units.begin(), endu = units.end(); + for (; unit != endu; ++unit) { +#ifdef DEAL_II_WITH_MUPARSER + // TODO: + // fp[i].DefinePostfixOprt(unit->first.c_str(), +// std_cxx1x::bind(&constant_eval, std_cxx1x:_1, unit->second)); + + AssertThrow(false, ExcNotImplemented()); + +#else const bool success = fp[i].AddUnit(unit->first, unit->second); AssertThrow (success, ExcMessage("Invalid Unit Name [error adding a unit]")); +#endif } - // Add the various constants to // the parser. std::map< std::string, double >::const_iterator constant = constants.begin(), endc = constants.end(); + + for (; constant != endc; ++constant) { +#ifdef DEAL_II_WITH_MUPARSER + fp[i].DefineConst(constant->first.c_str(), constant->second); +#else const bool success = fp[i].AddConstant(constant->first, constant->second); AssertThrow (success, ExcMessage("Invalid Constant Name")); +#endif } +#ifdef DEAL_II_WITH_MUPARSER + AssertThrow(!use_degrees, ExcNotImplemented()); + + fp[i].SetExpr(expressions[i]); +#else const int ret_value = fp[i].Parse(expressions[i], variables, use_degrees); AssertThrow (ret_value == -1, ExcParseError(ret_value, fp[i].ErrorMsg())); +#endif // The fact that the parser did // not throw an error does not @@ -232,11 +271,19 @@ double FunctionParser::value (const Point &p, if (dim != n_vars) vars[dim] = this->get_time(); +#ifdef DEAL_II_WITH_MUPARSER + for (unsigned int iv=0;iv::vector_value (const Point &p, if (dim != n_vars) vars[dim] = this->get_time(); +#ifdef DEAL_II_WITH_MUPARSER + + for (unsigned int component = 0; component < this->n_components; + ++component) + { + for (unsigned int iv=0;ivn_components; ++component) { @@ -272,6 +333,7 @@ void FunctionParser::vector_value (const Point &p, AssertThrow(fp[component].EvalError() == 0, ExcMessage(fp[component].ErrorMsg())); } +#endif } #else -- 2.39.5