From 6abb4421884a48105dbee4ba8e6e2fcfc1e24678 Mon Sep 17 00:00:00 2001 From: Luca Heltai Date: Sat, 21 Jul 2018 16:42:26 +0200 Subject: [PATCH] Add a new constructor for function parser. --- include/deal.II/base/function_parser.h | 12 +++++++++++ source/base/function_parser.cc | 28 ++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/include/deal.II/base/function_parser.h b/include/deal.II/base/function_parser.h index 7efdedf006..7cfb2615d3 100644 --- a/include/deal.II/base/function_parser.h +++ b/include/deal.II/base/function_parser.h @@ -211,6 +211,18 @@ public: const double initial_time = 0.0, const double h = 1e-8); + /** + * Constructor for Parsed functions. Take directly a semi-colon separated + * list of expressions (one for each component of the function), an optional + * comma-separated list of constants, variable names and step for the + * computation of first order derviatives by finite difference. + */ + FunctionParser(const std::string &expression, + const std::string &constants = "", + const std::string &variable_names = default_variable_names() + + ",t", + const double h = 1e-8); + /** * Destructor. Explicitly delete the FunctionParser objects (there is one * for each component of the function). diff --git a/source/base/function_parser.cc b/source/base/function_parser.cc index 3b96b8a5cd..10e1dd95e9 100644 --- a/source/base/function_parser.cc +++ b/source/base/function_parser.cc @@ -15,6 +15,7 @@ #include +#include #include #include @@ -53,6 +54,33 @@ FunctionParser::FunctionParser(const unsigned int n_components, {} +template +FunctionParser::FunctionParser(const std::string &expression, + const std::string &constants, + const std::string &variable_names, + const double h) + : AutoDerivativeFunction( + h, + Utilities::split_string_list(expression, ';').size()) + , initialized(false) + , n_vars(0) +{ + auto constants_map = Patterns::Tools::Convert::to_value( + constants, + Patterns::Map(Patterns::Anything(), + Patterns::Double(), + 0, + Patterns::Map::max_int_value, + ",", + "=") + .clone()); + initialize(variable_names, + expression, + constants_map, + Utilities::split_string_list(variable_names, ",").size() == + dim + 1); +} + // We deliberately delay the definition of the default destructor // so that we don't need to include the definition of mu::Parser -- 2.39.5