From: David Wells Date: Sat, 21 May 2022 04:03:01 +0000 (-0400) Subject: Move some more fields into the function parser base class. X-Git-Tag: v9.4.0-rc1~132^2~5 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5c0525a4ddd6e16298f759e54729b817eaf53e08;p=dealii.git Move some more fields into the function parser base class. --- diff --git a/include/deal.II/base/function_parser.h b/include/deal.II/base/function_parser.h index 05c81c7a23..396f6ae008 100644 --- a/include/deal.II/base/function_parser.h +++ b/include/deal.II/base/function_parser.h @@ -381,18 +381,6 @@ public: //@} private: - /** - * An array to keep track of all the constants, required to initialize fp in - * each thread. - */ - std::map constants; - - /** - * An array for the variable names, required to initialize fp in each - * thread. - */ - std::vector var_names; - /** * Initialize fp and vars on the current thread. This function may only be * called once per thread. A thread can test whether the function has @@ -401,27 +389,6 @@ private: */ void init_muparser() const; - - /** - * An array of function expressions (one per component), required to - * initialize fp in each thread. - */ - std::vector expressions; - - /** - * State of usability. This variable is checked every time the function is - * called for evaluation. It's set to true in the initialize() methods. - */ - bool initialized; - - /** - * Number of variables. If this is also a function of time, then the number - * of variables is dim+1, otherwise it is dim. In the case that this is a - * time dependent function, the time is supposed to be the last variable. If - * #n_vars is not identical to the number of the variables parsed by the - * initialize() method, then an exception is thrown. - */ - unsigned int n_vars; }; diff --git a/include/deal.II/base/mu_parser_internal.h b/include/deal.II/base/mu_parser_internal.h index 39ffa8c098..a8bdd42b78 100644 --- a/include/deal.II/base/mu_parser_internal.h +++ b/include/deal.II/base/mu_parser_internal.h @@ -92,12 +92,50 @@ namespace internal class ParserImplementation { public: + ParserImplementation() + : initialized(false) + , n_vars(0) + {} + /** * The muParser objects (hidden with the PIMPL idiom) for each thread (and * one for each component). */ mutable Threads::ThreadLocalStorage parser_data; + + /** + * An array to keep track of all the constants, required to initialize fp + * in each thread. + */ + std::map constants; + + /** + * An array for the variable names, required to initialize fp in each + * thread. + */ + std::vector var_names; + + /** + * An array of function expressions (one per component), required to + * initialize tfp in each thread. + */ + std::vector expressions; + + /** + * State of usability. This variable is checked every time the function is + * called for evaluation. It's set to true in the initialize() methods. + */ + bool initialized; + + /** + * Number of variables. If this is also a function of time, then the + * number of variables is dim+1, otherwise it is dim. In the case that + * this is a time dependent function, the time is supposed to be the last + * variable. If #n_vars is not identical to the number of the variables + * parsed by the initialize() method, then an exception is thrown. + */ + unsigned int n_vars; }; int diff --git a/include/deal.II/base/tensor_function_parser.h b/include/deal.II/base/tensor_function_parser.h index 568febb6b2..2a757524c9 100644 --- a/include/deal.II/base/tensor_function_parser.h +++ b/include/deal.II/base/tensor_function_parser.h @@ -263,18 +263,6 @@ public: //@} private: - /** - * An array to keep track of all the constants, required to initialize tfp in - * each thread. - */ - std::map constants; - - /** - * An array for the variable names, required to initialize tfp in each - * thread. - */ - std::vector var_names; - /** * Initialize tfp and vars on the current thread. This function may only be * called once per thread. A thread can test whether the function has @@ -284,27 +272,6 @@ private: void init_muparser() const; - /** - * An array of function expressions (one per component), required to - * initialize tfp in each thread. - */ - std::vector expressions; - - /** - * State of usability. This variable is checked every time the function is - * called for evaluation. It's set to true in the initialize() methods. - */ - bool initialized; - - /** - * Number of variables. If this is also a function of time, then the number - * of variables is dim+1, otherwise it is dim. In the case that this is a - * time dependent function, the time is supposed to be the last variable. If - * #n_vars is not identical to the number of the variables parsed by the - * initialize() method, then an exception is thrown. - */ - unsigned int n_vars; - /** * Number of components is equal dimrank. */ diff --git a/source/base/function_parser.cc b/source/base/function_parser.cc index 09037c51eb..b64138b88d 100644 --- a/source/base/function_parser.cc +++ b/source/base/function_parser.cc @@ -36,7 +36,7 @@ template const std::vector & FunctionParser::get_expressions() const { - return expressions; + return this->expressions; } @@ -46,8 +46,6 @@ FunctionParser::FunctionParser(const unsigned int n_components, const double initial_time, const double h) : AutoDerivativeFunction(h, n_components, initial_time) - , initialized(false) - , n_vars(0) {} @@ -59,8 +57,6 @@ FunctionParser::FunctionParser(const std::string &expression, : AutoDerivativeFunction( h, Utilities::split_string_list(expression, ';').size()) - , initialized(false) - , n_vars(0) { auto constants_map = Patterns::Tools::Convert::to_value( constants, @@ -92,7 +88,7 @@ FunctionParser::initialize(const std::string & variables, this->constants = constants; this->var_names = Utilities::split_string_list(variables, ','); this->expressions = expressions; - AssertThrow(((time_dependent) ? dim + 1 : dim) == var_names.size(), + AssertThrow(((time_dependent) ? dim + 1 : dim) == this->var_names.size(), ExcMessage("Wrong number of variables")); // We check that the number of components of this function matches the @@ -107,9 +103,9 @@ FunctionParser::initialize(const std::string & variables, // to the dimension. Once we parsed the variables string, if none of this is // the case, then an exception is thrown. if (time_dependent) - n_vars = dim + 1; + this->n_vars = dim + 1; else - n_vars = dim; + this->n_vars = dim; // create a parser object for the current thread we can then query in // value() and vector_value(). this is not strictly necessary because a user @@ -118,7 +114,7 @@ FunctionParser::initialize(const std::string & variables, init_muparser(); // finally set the initialization bit - initialized = true; + this->initialized = true; } namespace internal @@ -163,7 +159,7 @@ FunctionParser::init_muparser() const // initialize the objects for the current thread data.parsers.reserve(this->n_components); - data.vars.resize(var_names.size()); + data.vars.resize(this->var_names.size()); for (unsigned int component = 0; component < this->n_components; ++component) { data.parsers.emplace_back( @@ -172,11 +168,11 @@ FunctionParser::init_muparser() const dynamic_cast( *data.parsers.back()); - for (const auto &constant : constants) + for (const auto &constant : this->constants) parser.DefineConst(constant.first, constant.second); - for (unsigned int iv = 0; iv < var_names.size(); ++iv) - parser.DefineVar(var_names[iv], &data.vars[iv]); + for (unsigned int iv = 0; iv < this->var_names.size(); ++iv) + parser.DefineVar(this->var_names[iv], &data.vars[iv]); // define some compatibility functions: parser.DefineFun("if", internal::FunctionParser::mu_if, true); @@ -206,7 +202,7 @@ FunctionParser::init_muparser() const // (the fparser library) but also makes no real sense. // consequently, in the expressions we set, remove any space // we may find after function names - std::string transformed_expression = expressions[component]; + std::string transformed_expression = this->expressions[component]; for (const auto ¤t_function_name : internal::FunctionParser::get_function_names()) @@ -276,7 +272,7 @@ double FunctionParser::value(const Point & p, const unsigned int component) const { - Assert(initialized == true, ExcNotInitialized()); + Assert(this->initialized == true, ExcNotInitialized()); AssertIndexRange(component, this->n_components); // initialize the parser if that hasn't happened yet on the current thread @@ -286,7 +282,7 @@ FunctionParser::value(const Point & p, for (unsigned int i = 0; i < dim; ++i) data.vars[i] = p(i); - if (dim != n_vars) + if (dim != this->n_vars) data.vars[dim] = this->get_time(); try @@ -321,7 +317,7 @@ void FunctionParser::vector_value(const Point &p, Vector & values) const { - Assert(initialized == true, ExcNotInitialized()); + Assert(this->initialized == true, ExcNotInitialized()); Assert(values.size() == this->n_components, ExcDimensionMismatch(values.size(), this->n_components)); @@ -333,7 +329,7 @@ FunctionParser::vector_value(const Point &p, for (unsigned int i = 0; i < dim; ++i) data.vars[i] = p(i); - if (dim != n_vars) + if (dim != this->n_vars) data.vars[dim] = this->get_time(); for (unsigned int component = 0; component < this->n_components; ++component) diff --git a/source/base/tensor_function_parser.cc b/source/base/tensor_function_parser.cc index 6396a0062e..cb20799ef1 100644 --- a/source/base/tensor_function_parser.cc +++ b/source/base/tensor_function_parser.cc @@ -37,7 +37,7 @@ template const std::vector & TensorFunctionParser::get_expressions() const { - return expressions; + return this->expressions; } @@ -46,8 +46,6 @@ template TensorFunctionParser::TensorFunctionParser( const double initial_time) : TensorFunction(initial_time) - , initialized(false) - , n_vars(0) , n_components(Utilities::pow(dim, rank)) {} @@ -58,8 +56,6 @@ TensorFunctionParser::TensorFunctionParser( const std::string &constants, const std::string &variable_names) : TensorFunction() - , initialized(false) - , n_vars(0) , n_components(Utilities::pow(dim, rank)) { auto constants_map = Patterns::Tools::Convert::to_value( @@ -93,7 +89,7 @@ TensorFunctionParser::initialize( this->constants = constants; this->var_names = Utilities::split_string_list(variables, ','); this->expressions = expressions; - AssertThrow(((time_dependent) ? dim + 1 : dim) == var_names.size(), + AssertThrow(((time_dependent) ? dim + 1 : dim) == this->var_names.size(), ExcMessage("Wrong number of variables")); // We check that the number of @@ -118,9 +114,9 @@ TensorFunctionParser::initialize( // none of this is the case, then // an exception is thrown. if (time_dependent) - n_vars = dim + 1; + this->n_vars = dim + 1; else - n_vars = dim; + this->n_vars = dim; // create a parser object for the current thread we can then query // in value() and vector_value(). this is not strictly necessary @@ -130,7 +126,7 @@ TensorFunctionParser::initialize( init_muparser(); // finally set the initialization bit - initialized = true; + this->initialized = true; } @@ -178,7 +174,7 @@ TensorFunctionParser::init_muparser() const // initialize the objects for the current thread data.parsers.reserve(this->n_components); - data.vars.resize(var_names.size()); + data.vars.resize(this->var_names.size()); for (unsigned int component = 0; component < this->n_components; ++component) { data.parsers.emplace_back( @@ -188,11 +184,11 @@ TensorFunctionParser::init_muparser() const dynamic_cast( *data.parsers.back()); - for (const auto &constant : constants) + for (const auto &constant : this->constants) parser.DefineConst(constant.first, constant.second); - for (unsigned int iv = 0; iv < var_names.size(); ++iv) - parser.DefineVar(var_names[iv], &data.vars[iv]); + for (unsigned int iv = 0; iv < this->var_names.size(); ++iv) + parser.DefineVar(this->var_names[iv], &data.vars[iv]); // define some compatibility functions: parser.DefineFun("if", internal::FunctionParser::mu_if, true); @@ -221,7 +217,7 @@ TensorFunctionParser::init_muparser() const // (the fparser library) but also makes no real sense. // consequently, in the expressions we set, remove any space // we may find after function names - std::string transformed_expression = expressions[component]; + std::string transformed_expression = this->expressions[component]; for (const auto ¤t_function_name : internal::FunctionParser::get_function_names()) @@ -291,7 +287,7 @@ template Tensor TensorFunctionParser::value(const Point &p) const { - Assert(initialized == true, ExcNotInitialized()); + Assert(this->initialized == true, ExcNotInitialized()); // initialize the parser if that hasn't happened yet on the current thread internal::FunctionParser::ParserData &data = this->parser_data.get(); @@ -300,7 +296,7 @@ TensorFunctionParser::value(const Point &p) const for (unsigned int i = 0; i < dim; ++i) data.vars[i] = p(i); - if (dim != n_vars) + if (dim != this->n_vars) data.vars[dim] = this->get_time(); std::array::n_independent_components>