From 35d0c891d19c14897a29f141f6f390fe1bf08dd1 Mon Sep 17 00:00:00 2001
From: Klaus <klaus.boehnlein@tu-dresden.de>
Date: Fri, 19 Apr 2024 20:23:07 +0200
Subject: [PATCH] backup

---
 dune/microstructure/CorrectorComputer.hh      |  14 +-
 .../EffectiveQuantitiesComputer.hh            |   6 +
 ...scretekirchhoffbendingenergyprestrained.hh |  11 +-
 dune/microstructure/microproblem.hh           | 149 ++++++++++++------
 dune/microstructure/prestrainedMaterial.hh    |  29 +++-
 5 files changed, 158 insertions(+), 51 deletions(-)

diff --git a/dune/microstructure/CorrectorComputer.hh b/dune/microstructure/CorrectorComputer.hh
index e51657b8..e56f0125 100644
--- a/dune/microstructure/CorrectorComputer.hh
+++ b/dune/microstructure/CorrectorComputer.hh
@@ -46,7 +46,9 @@ public:
 protected:
 	const Basis& basis_; 
 
-  const Material& material_;
+  // const Material& material_;
+  Material& material_;
+  // Material material_;
 
   double gamma_;
 
@@ -90,8 +92,8 @@ public:
     ///////////////////////////////
     // constructor
     ///////////////////////////////
-    CorrectorComputer( const Basis& basis, 
-            const Material& material,
+    CorrectorComputer(const Basis& basis, 
+            Material& material,
             std::fstream& log, 
             const Dune::ParameterTree& parameterSet)
           : basis_(basis), 
@@ -121,6 +123,12 @@ public:
       std::cout << "Load vector assembly Timer: " << LoadVectorTimer.elapsed() << std::endl;
   };
 
+
+    void updateMaterial(Material mat)
+    {
+        this->material_ = mat;
+    }
+
     ///////////////////////////////
     // Getter
     ///////////////////////////////
diff --git a/dune/microstructure/EffectiveQuantitiesComputer.hh b/dune/microstructure/EffectiveQuantitiesComputer.hh
index b5fd0ef4..f856d26c 100644
--- a/dune/microstructure/EffectiveQuantitiesComputer.hh
+++ b/dune/microstructure/EffectiveQuantitiesComputer.hh
@@ -61,6 +61,12 @@ public:
     auto getQeff(){return Qeff_;}
     auto getBeff(){return Beff_;}
 
+
+    void updateCorrectorComputer(CorrectorComputer<Basis,Material> correctorComputer)
+    {
+        correctorComputer_ = correctorComputer;
+    }
+
   // -----------------------------------------------------------------
   // --- Compute Effective Quantities
     void computeEffectiveQuantities()
diff --git a/dune/microstructure/energies/discretekirchhoffbendingenergyprestrained.hh b/dune/microstructure/energies/discretekirchhoffbendingenergyprestrained.hh
index 3bae9612..a6c615db 100644
--- a/dune/microstructure/energies/discretekirchhoffbendingenergyprestrained.hh
+++ b/dune/microstructure/energies/discretekirchhoffbendingenergyprestrained.hh
@@ -72,7 +72,7 @@ namespace Dune::GFE
           parameterSet_(parameterSet),
           module_(pyModule),
           microstructureClass_(setupMicrostructureClass()),
-          microProblem_(setupMicroProblem())           // This is necessary since there is no default.constructor in for MicroProblem class.
+          microProblem_(setupMicroProblem())     // This is necessary since there is no default.constructor in for MicroProblem class.
     {
       setupEffectiveQuantities();
     }
@@ -93,7 +93,10 @@ namespace Dune::GFE
 
     MicroProblem setupMicroProblem() const
     {
-        return MicroProblem(parameterSet_, module_);
+        if(parameterSet_.get<bool>("macroscopically_varying_microstructure", 0))
+            return MicroProblem(microstructureClass_(0),parameterSet_, module_);  //TODO Replace 0  with Domain input type. using Domain = typename GridView::template Codim<0>::Geometry::GlobalCoordinate;
+        else 
+            return MicroProblem(microstructureClass_(),parameterSet_, module_);   //pass a constant microstructure object
     }
 
 
@@ -172,7 +175,7 @@ namespace Dune::GFE
     {
       RT energy = 0;
 
-
+  
       /**
        * @brief TEST reset effective quantities.
        * 
@@ -276,6 +279,8 @@ namespace Dune::GFE
           std::cout << "Compute effective quantities at current quadrature point." << quadPos << std::endl;
 
 
+
+            //USAGE:
           // microProblem_.updateMicrostructure(microstructureClass_(quadPoint.position)); 
           // microProblem_..getEffectiveQuantities(Beff_,Qhom_);
 
diff --git a/dune/microstructure/microproblem.hh b/dune/microstructure/microproblem.hh
index dc75d0e4..88730345 100644
--- a/dune/microstructure/microproblem.hh
+++ b/dune/microstructure/microproblem.hh
@@ -51,6 +51,25 @@
 // using namespace Dune;
 // using namespace MatrixOperations;
 
+  template <class T>
+  constexpr std::string_view type_name()
+  {
+      using namespace std;
+  #ifdef __clang__
+      string_view p = __PRETTY_FUNCTION__;
+      return string_view(p.data() + 34, p.size() - 34 - 1);
+  #elif defined(__GNUC__)
+      string_view p = __PRETTY_FUNCTION__;
+  #  if __cplusplus < 201402
+      return string_view(p.data() + 36, p.size() - 36 - 1);
+  #  else
+      return string_view(p.data() + 49, p.find(';', 49) - 49);
+  #  endif
+  #elif defined(_MSC_VER)
+      string_view p = __FUNCSIG__;
+      return string_view(p.data() + 84, p.size() - 84 - 7);
+  #endif
+  }
 
 
 //--------------------------------------------------------
@@ -65,38 +84,64 @@ class MicroProblem
                     Dune::DefaultLeafGridViewTraits<const Dune::YaspGrid<3, Dune::EquidistantOffsetCoordinates<double, 3> > > >, 1, double>, \
                     Dune::Functions::BasisFactory::Experimental::Impl::PeriodicIndexingTransformation>, 3> >  BasisType; 
 
+        using MaterialType = prestrainedMaterial<GridView>;
+
+
+        Python::Reference microstructure_;
         const Dune::ParameterTree parameterSet_;
-        Python::Module module_;
+        const Python::Module module_;
         const CellGridType grid_;
         const GridView gridView_; 
         const BasisType basis_;
 
-        Python::Reference microstructure_;
+
+        std::fstream log_; // deprecated: only used to define CorrectorComputer object. 
+
+
+        MaterialType material_;
+        CorrectorComputer<BasisType, MaterialType> correctorComputer_;
+        EffectiveQuantitiesComputer<BasisType,MaterialType> effectiveQuantitiesComputer_;
+        
 
 
     public:
+        //Constructor
+        MicroProblem(const Python::Reference microstructure,
+                     const Dune::ParameterTree& parameterSet,
+                     const Python::Module pyModule)
+            : microstructure_(microstructure), 
+            parameterSet_(parameterSet),
+            module_(pyModule),
+            grid_(createGrid()),
+            gridView_(grid_.leafGridView()),
+            basis_(createPeriodicBasis()),
+            // material_(setupMaterial(gridView_))
+            material_(gridView_,microstructure_,parameterSet_,module_),
+            correctorComputer_(basis_, material_, log_, parameterSet_),
+            effectiveQuantitiesComputer_(correctorComputer_,material_)  // Remove material dependency 
+        {};
         // //Constructor
-        // MicroProblem(Python::Reference microstructure,
-        //              const Dune::ParameterTree& parameterSet,
+        // MicroProblem(const Dune::ParameterTree& parameterSet,
         //              Python::Module pyModule)
-        //     : microstructure_(microstructure), 
-        //     parameterSet_(parameterSet),
+        //     : parameterSet_(parameterSet),
         //     module_(pyModule),
         //     grid_(createGrid()),
         //     gridView_(grid_.leafGridView()),
         //     basis_(createPeriodicBasis())
-        // {};
-        //Constructor
-        MicroProblem(const Dune::ParameterTree& parameterSet,
-                     Python::Module pyModule)
-            : parameterSet_(parameterSet),
-            module_(pyModule),
-            grid_(createGrid()),
-            gridView_(grid_.leafGridView()),
-            basis_(createPeriodicBasis())
-        {};
+        // {
+        //     initialSetup();
+        // };
+
+
+        // Create a default constructor like this?
+        // MicroProblem() = default;
+
+
+        // MaterialType setupMaterial(const GridView& gridView) const 
+        // {
+        //     return MaterialType(gridView,microstructure_,parameterSet_,module_);
+        // }
 
-        // MicroProblem(){};
 
 
         /**
@@ -152,6 +197,10 @@ class MicroProblem
         void updateMicrostructure(Python::Reference microstructure) 
         {
             microstructure_ = microstructure;
+
+            material_.updateMicrostructure(microstructure_);
+            correctorComputer_.updateMaterial(material_);
+            // effectiveQuantitiesComputer_.updateCorrectorComputer(correctorComputer_);
         }
 
 
@@ -169,50 +218,62 @@ class MicroProblem
             std::cout << "Scale ratio (gamma) set to : " << gamma << std::endl;
 
 
-            /**
-             * @brief Create Log (Remove later) 
-             * 
-             */
-            std::string baseName = parameterSet_.get("baseName", "CellProblem-result");
-            std::string outputPath = parameterSet_.get("resultPath", "../../outputs") ;
-            //--- setup Log-File
-            std::fstream log;
-            log.open(outputPath + "/" + baseName + "_log.txt" ,std::ios::out);
+            // /**
+            //  * @brief Create Log (Remove later) 
+            //  * 
+            //  */
+            // std::string baseName = parameterSet_.get("baseName", "CellProblem-result");
+            // std::string outputPath = parameterSet_.get("resultPath", "../../outputs") ;
+            // //--- setup Log-File
+            // std::fstream log;
+            // log.open(outputPath + "/" + baseName + "_log.txt" ,std::ios::out);
 
-            std::cout << "log done" << std::endl;
+            // std::cout << "log done" << std::endl;
           
-            //  Create prestrained material object
+            // //  Create prestrained material object
             auto material = prestrainedMaterial(basis_.gridView(),microstructure_,parameterSet_,module_);
 
+
+            // std::cout << "type_name<decltype(material_)>():" << type_name<decltype(material_)>() << std::endl;
+            // std::cout << "type_name<decltype(material)>():" << type_name<decltype(material)>() << std::endl;
+
+
+
+
+
+            // // std::cout << "TEST-VTK write.." << std::endl;
+            // material.writeVTKMaterialFunctions(parameterSet_.get<int>("gridLevel", 0));
+            // std::cout << "Part1 works." << std::endl;
+
             if (parameterSet_.get<bool>("write_materialFunctions", false))
-               material.writeVTKMaterialFunctions(parameterSet_.get<int>("gridLevel", 0));
+               material_.writeVTKMaterialFunctions(parameterSet_.get<int>("gridLevel", 0));
 
             std::cout << "Material setup done" << std::endl;
 
-            // Compute Correctors
-            auto correctorComputer = CorrectorComputer(basis_, material, log, parameterSet_);
+            // // Compute Correctors
+            // auto correctorComputer = CorrectorComputer(basis_, material, log, parameterSet_);
             std::cout << "Starting Corrector assembly" << std::endl;
-            correctorComputer.assemble();
+            correctorComputer_.assemble();
             std::cout << "Starting Corrector solve..." << std::endl;
-            correctorComputer.solve();
+            correctorComputer_.solve();
 
 
-            std::cout << "Corrector done" << std::endl;
-            //--- Compute effective quantities
-            auto effectiveQuantitiesComputer = EffectiveQuantitiesComputer(correctorComputer,material);
-            effectiveQuantitiesComputer.computeEffectiveQuantities();
+            // std::cout << "Corrector done" << std::endl;
+            // //--- Compute effective quantities
+            // auto effectiveQuantitiesComputer = EffectiveQuantitiesComputer(correctorComputer,material);
+            effectiveQuantitiesComputer_.computeEffectiveQuantities();
 
 
-            std::cout << "effective Quantities  done" << std::endl;
+            // std::cout << "effective Quantities  done" << std::endl;
 
-            //--- Get effective quantities
-            auto Beffvec = effectiveQuantitiesComputer.getBeff();
-            // convert CoefficientVector to matrix
+            // //--- Get effective quantities
+            auto Beffvec = effectiveQuantitiesComputer_.getBeff();
+            // // convert CoefficientVector to matrix
             auto Beff = CoefficientsToSymMatrix(Beffvec);
-            Q = effectiveQuantitiesComputer.getQeff();
+            Q = effectiveQuantitiesComputer_.getQeff();
             B = CoefficientsToSymMatrix(Beffvec);
-            // printmatrix(std::cout, Q, "Matrix Qeff", "--");
-            // printmatrix(std::cout, B, "Beff", "--");
+            printmatrix(std::cout, Q, "Matrix Qeff", "--");
+            printmatrix(std::cout, B, "Beff", "--");
         }
 
 };
diff --git a/dune/microstructure/prestrainedMaterial.hh b/dune/microstructure/prestrainedMaterial.hh
index f445fec7..ba486153 100644
--- a/dune/microstructure/prestrainedMaterial.hh
+++ b/dune/microstructure/prestrainedMaterial.hh
@@ -57,7 +57,8 @@ public:
   using IndicatorGridViewFunctionType = GridViewFunction<int(const Domain&), GridView>;
   using LocalIndicatorFunctionType = LocalFunction<int(const Domain&), typename GridViewEntitySet<GridView, 0>::Element >;
 
-protected:
+
+  // const GridView& gridView_;
   const GridView& gridView_;
   const Dune::ParameterTree parameterSet_;
 
@@ -95,6 +96,25 @@ public:
     {
        setup();
     } 
+    ~prestrainedMaterial(){std::cout << "prestrainedMaterial object destroyed!" << std::endl;};
+
+    // prestrainedMaterial() = default;
+    
+    //copy constructor 
+    prestrainedMaterial(const prestrainedMaterial& that) = default;
+
+    // Copy assignment operator
+    // prestrainedMaterial& operator=(prestrainedMaterial const& that)
+    // {
+    //   gridView_ = that.gridView_;
+    //   parameterSet_ = that.parameterSet_;
+    //   L_ = that.L_;
+    //   prestrain_ = that.prestrain_;
+    //   module_ = that.module_;
+    //   microstructure_ = that.microstructure_;
+
+    //   return *this;
+    // }
 
   
   void updateMicrostructure(const Python::Reference microstructure) 
@@ -102,6 +122,13 @@ public:
       microstructure_ = microstructure;
   }
 
+
+  void updateGamma(const double gamma) 
+  {
+      gamma_ = gamma;
+  }
+
+
 /**
  * @brief TODO: Code allows a spatially varying prestrain. However most of the time we only 
  *              use a constant prestrain. 
-- 
GitLab