From 8511bc8ae74145d5d92dfd0f91dd00bbc23a9ca7 Mon Sep 17 00:00:00 2001
From: Dmitri Naumov <dmitri.naumov@ufz.de>
Date: Wed, 7 Jun 2023 18:30:27 +0200
Subject: [PATCH] [MatL/S] Add initialization of internal state vars

First implementation for MFront only.
---
 MaterialLib/SolidModels/MFront/MFront.h       |  9 +++++
 .../SolidModels/MFront/MFrontGeneric.h        | 40 +++++++++++++++++--
 MaterialLib/SolidModels/MechanicsBase.h       |  7 ++++
 3 files changed, 53 insertions(+), 3 deletions(-)

diff --git a/MaterialLib/SolidModels/MFront/MFront.h b/MaterialLib/SolidModels/MFront/MFront.h
index 40cce4a2d3f..6be9fd61f9e 100644
--- a/MaterialLib/SolidModels/MFront/MFront.h
+++ b/MaterialLib/SolidModels/MFront/MFront.h
@@ -42,6 +42,15 @@ public:
         return Base::createMaterialStateVariables();
     }
 
+    void initializeInternalStateVariables(
+        double const t,
+        ParameterLib::SpatialPosition const& x,
+        typename MechanicsBase<DisplacementDim>::MaterialStateVariables&
+            material_state_variables) const override
+    {
+        Base::initializeInternalStateVariables(t, x, material_state_variables);
+    }
+
     std::optional<std::tuple<KelvinVector,
                              std::unique_ptr<typename MechanicsBase<
                                  DisplacementDim>::MaterialStateVariables>,
diff --git a/MaterialLib/SolidModels/MFront/MFrontGeneric.h b/MaterialLib/SolidModels/MFront/MFrontGeneric.h
index bab6bc1a80d..3de579b3baf 100644
--- a/MaterialLib/SolidModels/MFront/MFrontGeneric.h
+++ b/MaterialLib/SolidModels/MFront/MFrontGeneric.h
@@ -228,6 +228,8 @@ public:
         MathLib::KelvinVector::KelvinVectorType<DisplacementDim>;
     using KelvinMatrix =
         MathLib::KelvinVector::KelvinMatrixType<DisplacementDim>;
+    using InternalVariable =
+        typename MechanicsBase<DisplacementDim>::InternalVariable;
 
     MFrontGeneric(
         mgis::behaviour::Behaviour&& behaviour,
@@ -375,6 +377,41 @@ public:
             equivalent_plastic_strain_offset_, _behaviour);
     }
 
+    void initializeInternalStateVariables(
+        double const t,
+        ParameterLib::SpatialPosition const& x,
+        typename MechanicsBase<DisplacementDim>::MaterialStateVariables&
+            material_state_variables) const
+    {
+        assert(dynamic_cast<MaterialStateVariablesMFront<DisplacementDim>*>(
+            &material_state_variables));
+
+        auto& state =
+            static_cast<MaterialStateVariablesMFront<DisplacementDim>&>(
+                material_state_variables);
+
+        auto const& ivs = getInternalVariables();
+
+        for (auto& [name, parameter] : _state_variables_initial_properties)
+        {
+            // find corresponding internal variable
+            auto const& iv = BaseLib::findElementOrError(
+                begin(ivs),
+                end(ivs),
+                [name = name](InternalVariable const& iv)
+                { return iv.name == name; },
+                fmt::format("Internal variable `{:s}' not found.", name));
+
+            // evaluate parameter
+            std::vector<double> values = (*parameter)(t, x);
+
+            // copy parameter data into iv
+            auto const values_span = iv.reference(state);
+            assert(values.size() == values_span.size());
+            std::copy_n(begin(values), values_span.size(), values_span.begin());
+        }
+    }
+
     std::optional<std::tuple<OGSMFrontThermodynamicForcesData,
                              std::unique_ptr<typename MechanicsBase<
                                  DisplacementDim>::MaterialStateVariables>,
@@ -534,9 +571,6 @@ public:
                     behaviour_data.K, Q, _behaviour)));
     }
 
-    using InternalVariable =
-        typename MechanicsBase<DisplacementDim>::InternalVariable;
-
     std::vector<InternalVariable> getInternalVariables() const
     {
         std::vector<InternalVariable> internal_variables;
diff --git a/MaterialLib/SolidModels/MechanicsBase.h b/MaterialLib/SolidModels/MechanicsBase.h
index f6458436600..7886845d5d8 100644
--- a/MaterialLib/SolidModels/MechanicsBase.h
+++ b/MaterialLib/SolidModels/MechanicsBase.h
@@ -71,6 +71,13 @@ struct MechanicsBase
         return std::make_unique<MaterialStateVariables>();
     }
 
+    virtual void initializeInternalStateVariables(
+        double const /*t*/,
+        ParameterLib::SpatialPosition const& /*x*/,
+        typename MechanicsBase<DisplacementDim>::MaterialStateVariables&) const
+    {
+    }
+
     using KelvinVector =
         MathLib::KelvinVector::KelvinVectorType<DisplacementDim>;
     using KelvinMatrix =
-- 
GitLab