From 5c0525a4ddd6e16298f759e54729b817eaf53e08 Mon Sep 17 00:00:00 2001
From: David Wells <drwells@email.unc.edu>
Date: Sat, 21 May 2022 00:03:01 -0400
Subject: [PATCH] Move some more fields into the function parser base class.

---
 include/deal.II/base/function_parser.h        | 33 ----------------
 include/deal.II/base/mu_parser_internal.h     | 38 +++++++++++++++++++
 include/deal.II/base/tensor_function_parser.h | 33 ----------------
 source/base/function_parser.cc                | 32 +++++++---------
 source/base/tensor_function_parser.cc         | 28 ++++++--------
 5 files changed, 64 insertions(+), 100 deletions(-)

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<std::string, double> constants;
-
-  /**
-   * An array for the variable names, required to initialize fp in each
-   * thread.
-   */
-  std::vector<std::string> 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<std::string> 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<internal::FunctionParser::ParserData>
         parser_data;
+
+      /**
+       * An array to keep track of all the constants, required to initialize fp
+       * in each thread.
+       */
+      std::map<std::string, double> constants;
+
+      /**
+       * An array for the variable names, required to initialize fp in each
+       * thread.
+       */
+      std::vector<std::string> var_names;
+
+      /**
+       * An array of function expressions (one per component), required to
+       * initialize tfp in each thread.
+       */
+      std::vector<std::string> 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<std::string, double> constants;
-
-  /**
-   * An array for the variable names, required to initialize tfp in each
-   * thread.
-   */
-  std::vector<std::string> 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<std::string> 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 dim<sup>rank</sup>.
    */
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 <int dim>
 const std::vector<std::string> &
 FunctionParser<dim>::get_expressions() const
 {
-  return expressions;
+  return this->expressions;
 }
 
 
@@ -46,8 +46,6 @@ FunctionParser<dim>::FunctionParser(const unsigned int n_components,
                                     const double       initial_time,
                                     const double       h)
   : AutoDerivativeFunction<dim>(h, n_components, initial_time)
-  , initialized(false)
-  , n_vars(0)
 {}
 
 
@@ -59,8 +57,6 @@ FunctionParser<dim>::FunctionParser(const std::string &expression,
   : AutoDerivativeFunction<dim>(
       h,
       Utilities::split_string_list(expression, ';').size())
-  , initialized(false)
-  , n_vars(0)
 {
   auto constants_map = Patterns::Tools::Convert<ConstMap>::to_value(
     constants,
@@ -92,7 +88,7 @@ FunctionParser<dim>::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<dim>::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<dim>::initialize(const std::string &             variables,
   init_muparser();
 
   // finally set the initialization bit
-  initialized = true;
+  this->initialized = true;
 }
 
 namespace internal
@@ -163,7 +159,7 @@ FunctionParser<dim>::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<dim>::init_muparser() const
         dynamic_cast<internal::FunctionParserImplementation::Parser &>(
           *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<dim>::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 &current_function_name :
                internal::FunctionParser::get_function_names())
@@ -276,7 +272,7 @@ double
 FunctionParser<dim>::value(const Point<dim> & 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<dim>::value(const Point<dim> & 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<dim>::vector_value(const Point<dim> &p,
                                   Vector<double> &  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<dim>::vector_value(const Point<dim> &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 <int rank, int dim, typename Number>
 const std::vector<std::string> &
 TensorFunctionParser<rank, dim, Number>::get_expressions() const
 {
-  return expressions;
+  return this->expressions;
 }
 
 
@@ -46,8 +46,6 @@ template <int rank, int dim, typename Number>
 TensorFunctionParser<rank, dim, Number>::TensorFunctionParser(
   const double initial_time)
   : TensorFunction<rank, dim, Number>(initial_time)
-  , initialized(false)
-  , n_vars(0)
   , n_components(Utilities::pow(dim, rank))
 {}
 
@@ -58,8 +56,6 @@ TensorFunctionParser<rank, dim, Number>::TensorFunctionParser(
   const std::string &constants,
   const std::string &variable_names)
   : TensorFunction<rank, dim, Number>()
-  , initialized(false)
-  , n_vars(0)
   , n_components(Utilities::pow(dim, rank))
 {
   auto constants_map = Patterns::Tools::Convert<ConstMap>::to_value(
@@ -93,7 +89,7 @@ TensorFunctionParser<rank, dim, Number>::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<rank, dim, Number>::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<rank, dim, Number>::initialize(
   init_muparser();
 
   // finally set the initialization bit
-  initialized = true;
+  this->initialized = true;
 }
 
 
@@ -178,7 +174,7 @@ TensorFunctionParser<rank, dim, Number>::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<rank, dim, Number>::init_muparser() const
         dynamic_cast<internal::TensorFunctionParserImplementation::Parser &>(
           *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<rank, dim, Number>::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 &current_function_name :
                internal::FunctionParser::get_function_names())
@@ -291,7 +287,7 @@ template <int rank, int dim, typename Number>
 Tensor<rank, dim, Number>
 TensorFunctionParser<rank, dim, Number>::value(const Point<dim> &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<rank, dim, Number>::value(const Point<dim> &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<Number, Tensor<rank, dim, Number>::n_independent_components>
-- 
2.39.5