From ec7ad61e4697288faed7637a0851862c2c9885d9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Felix=20M=C3=BCller?= <felix.mueller2@mailbox.tu-dresden.de>
Date: Wed, 23 May 2018 15:37:55 +0200
Subject: [PATCH] removed explicit contructor argument for ScalContents' names,
 added helper function to construct ScalContents on access

---
 src/amdis/AdaptInfo.cpp |  14 ++---
 src/amdis/AdaptInfo.hpp | 111 +++++++++++++++++++++-------------------
 2 files changed, 62 insertions(+), 63 deletions(-)

diff --git a/src/amdis/AdaptInfo.cpp b/src/amdis/AdaptInfo.cpp
index 88367d9e..f76b26ce 100644
--- a/src/amdis/AdaptInfo.cpp
+++ b/src/amdis/AdaptInfo.cpp
@@ -25,7 +25,7 @@ namespace AMDiS
   }
 
 
-  AdaptInfo::AdaptInfo(std::string name, List contentNames)
+  AdaptInfo::AdaptInfo(std::string name)
     : name(name)
   {
     // init();
@@ -41,10 +41,6 @@ namespace AMDiS
     Parameters::get(name + "->max timestep", maxTimestep);
     Parameters::get(name + "->number of timesteps", nTimesteps);
     Parameters::get(name + "->time tolerance", globalTimeTolerance);
-
-    for (auto it = contentNames.begin(); it != contentNames.end(); it++) {
-      scalContents[*it] = std::make_unique<ScalContent>(ScalContent(name + "[" + *it + "]"));
-    }
   }
 
 
@@ -56,13 +52,13 @@ namespace AMDiS
       std::cout << "    Time error estimate     ["<<i<<"] = "
                 << getTimeEstCombined(i) << "\n"
                 << "    Time error estimate sum ["<<i<<"] = "
-                << it->second->est_t_sum << "\n"
+                << it->second.est_t_sum << "\n"
                 << "    Time error estimate max ["<<i<<"] = "
-                << it->second->est_t_max << "\n"
+                << it->second.est_t_max << "\n"
                 << "    Time error low bound    ["<<i<<"] = "
-                << it->second->timeErrLow << "\n"
+                << it->second.timeErrLow << "\n"
                 << "    Time error high bound   ["<<i<<"] = "
-                << it->second->timeTolerance << "\n";
+                << it->second.timeTolerance << "\n";
     }
   }
 
diff --git a/src/amdis/AdaptInfo.hpp b/src/amdis/AdaptInfo.hpp
index 9107d556..24d65066 100644
--- a/src/amdis/AdaptInfo.hpp
+++ b/src/amdis/AdaptInfo.hpp
@@ -7,6 +7,7 @@
 #include <limits>
 #include <map>
 #include <string>
+#include <utility>
 #include <vector>
 
 // AMDiS includes
@@ -26,8 +27,6 @@ namespace AMDiS
    */
   class AdaptInfo
   {
-    using List = typename std::vector<std::string>;
-
   protected:
     /** \brief
      * Stores adapt infos for a scalar problem or for one component of a
@@ -75,7 +74,7 @@ namespace AMDiS
 
   public:
     /// Constructor.
-    explicit AdaptInfo(std::string name, List compNames = {"0"});
+    explicit AdaptInfo(std::string name);
 
     /// Destructor.
     virtual ~AdaptInfo() {}
@@ -87,7 +86,7 @@ namespace AMDiS
     virtual bool spaceToleranceReached() const
     {
       for (auto it = scalContents.begin(); it != scalContents.end(); it++)      {
-        if (!(it->second->est_sum < it->second->spaceTolerance))
+        if (!(it->second.est_sum < it->second.spaceTolerance))
           return false;
       }
 
@@ -97,14 +96,14 @@ namespace AMDiS
     /// Returns whether space tolerance of component associated with key is reached.
     virtual bool spaceToleranceReached(std::string key) const
     {
-      if (!(scalContents.at(key)->est_sum < scalContents.at(key)->spaceTolerance))
+      if (!(getScalContent(key).est_sum < getScalContent(key).spaceTolerance))
         return false;
       else
         return true;
     }
 
     template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
-    bool spaceToleranceReached(TP& tp) const
+    bool spaceToleranceReached(const TP& tp) const
     {
       return spaceToleranceReached(to_string(tp));
     }
@@ -113,7 +112,7 @@ namespace AMDiS
     virtual bool timeToleranceReached() const
     {
       for (auto it = scalContents.begin(); it != scalContents.end(); it++)
-        if (!(getTimeEstCombined(it->first) < it->second->timeTolerance))
+        if (!(getTimeEstCombined(it->first) < it->second.timeTolerance))
           return false;
 
       return true;
@@ -122,14 +121,14 @@ namespace AMDiS
     /// Returns whether time tolerance of component associated with key is reached.
     virtual bool timeToleranceReached(std::string key) const
     {
-      if (!(getTimeEstCombined(key) < scalContents.at(key)->timeTolerance))
+      if (!(getTimeEstCombined(key) < getScalContent(key).timeTolerance))
         return false;
       else
         return true;
     }
 
     template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
-    bool timeToleranceReached(TP& tp) const
+    bool timeToleranceReached(const TP& tp) const
     {
       return timeToleranceReached(to_string(tp));
     }
@@ -138,7 +137,7 @@ namespace AMDiS
     virtual bool timeErrorLow() const
     {
       for (auto it = scalContents.begin(); it != scalContents.end(); it++)
-        if (!(getTimeEstCombined(it->first) < it->second->timeErrLow))
+        if (!(getTimeEstCombined(it->first) < it->second.timeErrLow))
           return false;
 
       return true;
@@ -149,12 +148,12 @@ namespace AMDiS
     double getTimeEstCombined(std::string key) const
     {
       return
-        scalContents.at(key)->est_t_max * scalContents.at(key)->fac_max +
-        scalContents.at(key)->est_t_sum * scalContents.at(key)->fac_sum;
+        getScalContent(key).est_t_max * getScalContent(key).fac_max +
+        getScalContent(key).est_t_sum * getScalContent(key).fac_sum;
     }
 
     template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
-    double getTimeEstCombined(TP& tp) const
+    double getTimeEstCombined(const TP& tp) const
     {
       return getTimeEstCombined(to_string(tp));
     }
@@ -285,11 +284,11 @@ namespace AMDiS
     /// Sets \ref est_sum.
     void setEstSum(double e, std::string key)
     {
-      scalContents.at(key)->est_sum = e;
+      getScalContent(key).est_sum = e;
     }
 
     template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
-    void setEstSum(double e, TP& tp)
+    void setEstSum(double e, const TP& tp)
     {
       setEstSum(e, to_string(tp));
     }
@@ -297,11 +296,11 @@ namespace AMDiS
     /// Sets \ref est_max.
     void setEstMax(double e, std::string key)
     {
-      scalContents.at(key)->est_max = e;
+      getScalContent(key).est_max = e;
     }
 
     template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
-    void setEstMax(double e, TP& tp)
+    void setEstMax(double e, const TP& tp)
     {
       setEstMax(e, to_string(tp));
     }
@@ -309,11 +308,11 @@ namespace AMDiS
     /// Sets \ref est_max.
     void setTimeEstMax(double e, std::string key)
     {
-      scalContents.at(key)->est_t_max = e;
+      getScalContent(key).est_t_max = e;
     }
 
     template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
-    void setTimeEstMax(double e, TP& tp)
+    void setTimeEstMax(double e, const TP& tp)
     {
       setTimeEstMax(e, to_string(tp));
     }
@@ -321,11 +320,11 @@ namespace AMDiS
     /// Sets \ref est_t_sum.
     void setTimeEstSum(double e, std::string key)
     {
-      scalContents.at(key)->est_t_sum = e;
+      getScalContent(key).est_t_sum = e;
     }
 
     template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
-    void setTimeEstSum(double e, TP& tp)
+    void setTimeEstSum(double e, const TP& tp)
     {
       setTimeEstSum(e, to_string(tp));
     }
@@ -336,11 +335,11 @@ namespace AMDiS
       AMDIS_FUNCNAME_DBG("AdaptInfo::getEstSum()");
       test_exit_dbg(scalContents.count(key) == 1, "Wrong key for adaptInfo!\n");
 
-      return scalContents.at(key)->est_sum;
+      return getScalContent(key).est_sum;
     }
 
     template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
-    double getEstSum(TP& tp)
+    double getEstSum(const TP& tp)
     {
       return getEstSum(to_string(tp));
     }
@@ -348,11 +347,11 @@ namespace AMDiS
     /// Returns \ref est_t_sum.
     double getEstTSum(std::string key) const
     {
-      return scalContents.at(key)->est_t_sum;
+      return getScalContent(key).est_t_sum;
     }
 
     template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
-    double getEstTSum(TP& tp)
+    double getEstTSum(const TP& tp)
     {
       return getEstTSum(to_string(tp));
     }
@@ -363,11 +362,11 @@ namespace AMDiS
       AMDIS_FUNCNAME_DBG("AdaptInfo::getEstSum()");
       test_exit_dbg(scalContents.count(key) == 1, "Wrong key for adaptInfo!\n");
 
-      return scalContents.at(key)->est_max;
+      return getScalContent(key).est_max;
     }
 
     template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
-    double getEstMax(TP& tp)
+    double getEstMax(const TP& tp)
     {
       return getEstMax(to_string(tp));
     }
@@ -375,11 +374,11 @@ namespace AMDiS
     /// Returns \ref est_max.
     double getTimeEstMax(std::string key) const
     {
-      return scalContents.at(key)->est_t_max;
+      return getScalContent(key).est_t_max;
     }
 
     template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
-    double getTimeEstmax(TP& tp)
+    double getTimeEstmax(const TP& tp)
     {
       return getTimeEstMax(to_string(tp));
     }
@@ -387,11 +386,11 @@ namespace AMDiS
     /// Returns \ref est_t_sum.
     double getTimeEstSum(std::string key) const
     {
-      return scalContents.at(key)->est_t_sum;
+      return getScalContent(key).est_t_sum;
     }
 
     template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
-    double getTimeEstSum(TP& tp)
+    double getTimeEstSum(const TP& tp)
     {
       return getTimeEstSum(to_string(tp));
     }
@@ -410,11 +409,11 @@ namespace AMDiS
     /// Returns \ref spaceTolerance.
     double getSpaceTolerance(std::string key) const
     {
-      return scalContents.at(key)->spaceTolerance;
+      return getScalContent(key).spaceTolerance;
     }
 
     template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
-    double getSpaceTolerance(TP& tp)
+    double getSpaceTolerance(const TP& tp)
     {
       return getSpaceTolerance(to_string(tp));
     }
@@ -422,11 +421,11 @@ namespace AMDiS
     /// Sets \ref spaceTolerance.
     void setSpaceTolerance(std::string key, double tol)
     {
-      scalContents.at(key)->spaceTolerance = tol;
+      getScalContent(key).spaceTolerance = tol;
     }
 
     template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
-    void setSpaceTolerance(TP& tp, double tol)
+    void setSpaceTolerance(const TP& tp, double tol)
     {
       return setSpaceTolerance(to_string(tp), tol);
     }
@@ -434,11 +433,11 @@ namespace AMDiS
     /// Returns \ref timeTolerance.
     double getTimeTolerance(std::string key) const
     {
-      return scalContents.at(key)->timeTolerance;
+      return getScalContent(key).timeTolerance;
     }
 
     template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
-    double getTimeTolerance(TP& tp)
+    double getTimeTolerance(const TP& tp)
     {
       return getTimeTolerance(to_string(tp));
     }
@@ -446,11 +445,11 @@ namespace AMDiS
     /// Returns \ref timeRelativeTolerance.
     double getTimeRelativeTolerance(std::string key) const
     {
-      return scalContents.at(key)->timeRelativeTolerance;
+      return getScalContent(key).timeRelativeTolerance;
     }
 
     template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
-    double getTimeRelativeTolerance(TP& tp)
+    double getTimeRelativeTolerance(const TP& tp)
     {
       return getTimeRelativeTolerance(to_string(tp));
     }
@@ -576,11 +575,11 @@ namespace AMDiS
     /// Returns \ref timeErrLow.
     double getTimeErrLow(std::string key) const
     {
-      return scalContents.at(key)->timeErrLow;
+      return getScalContent(key).timeErrLow;
     }
 
     template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
-    double getTimeErrLow(TP& tp)
+    double getTimeErrLow(const TP& tp)
     {
       return getTimeErrLow(to_string(tp));
     }
@@ -588,11 +587,11 @@ namespace AMDiS
     /// Returns whether coarsening is allowed or not.
     bool isCoarseningAllowed(std::string key) const
     {
-      return (scalContents.at(key)->coarsenAllowed == 1);
+      return (getScalContent(key).coarsenAllowed == 1);
     }
 
     template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
-    bool isCoarseningAllowed(TP& tp)
+    bool isCoarseningAllowed(const TP& tp)
     {
       return isCoarseningAllowed(to_string(tp));
     }
@@ -600,11 +599,11 @@ namespace AMDiS
     /// Returns whether coarsening is allowed or not.
     bool isRefinementAllowed(std::string key) const
     {
-      return (scalContents.at(key)->refinementAllowed == 1);
+      return (getScalContent(key).refinementAllowed == 1);
     }
 
     template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
-    bool isRefinementAllowed(TP& tp)
+    bool isRefinementAllowed(const TP& tp)
     {
       return isRefinementAllowed(to_string(tp));
     }
@@ -612,11 +611,11 @@ namespace AMDiS
     ///
     void allowRefinement(bool allow, std::string key)
     {
-      scalContents.at(key)->refinementAllowed = allow;
+      getScalContent(key).refinementAllowed = allow;
     }
 
     template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
-    void allowRefinement(bool allow, TP& tp)
+    void allowRefinement(bool allow, const TP& tp)
     {
       return allowRefinement(allow, to_string(tp));
     }
@@ -624,11 +623,11 @@ namespace AMDiS
     ///
     void allowCoarsening(bool allow, std::string key)
     {
-      scalContents.at(key)->coarsenAllowed = allow;
+      getScalContent(key).coarsenAllowed = allow;
     }
 
     template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
-    void allowCoarsening(bool allow, TP& tp)
+    void allowCoarsening(bool allow, const TP& tp)
     {
       return allowCoarsening(allow, to_string(tp));
     }
@@ -700,9 +699,6 @@ namespace AMDiS
       rosenbrockMode = b;
     }
 
-    /// Creates new scalContents with the given size.
-    void setScalContents(List names);
-
     /** \brief
      * Resets timestep, current time and time boundaries without
      * any check. Is used by the parareal algorithm.
@@ -718,6 +714,13 @@ namespace AMDiS
       timestepNumber = 0;
     }
 
+  private:
+    ScalContent& getScalContent(std::string key) const
+    {
+      auto result = scalContents.emplace(std::piecewise_construct, std::forward_as_tuple(key), std::forward_as_tuple(name + "[" + key + "]") );
+      return result.first->second;
+    }
+
   protected:
     /// Name.
     std::string name;
@@ -789,8 +792,8 @@ namespace AMDiS
     /// tolerance for the overall time error
     double globalTimeTolerance = 1.0;
 
-    /// Scalar adapt infos.
-    std::map<std::string, std::unique_ptr<ScalContent>> scalContents;
+    /// Scalar adapt infos
+    mutable std::map<std::string, ScalContent> scalContents;
 
     /// Is true, if the adaptive procedure was deserialized from a file. TODO: remove deserialization
     bool deserialized = false;
-- 
GitLab