Repository: KratosMultiphysics/Kratos (C++)

Issue:
[GeoMechanicsApplication] max_delta_time_factor not taken into account in the c++ time incrementor
There is a small discrepancy between the c++ and python behavior for settlement. While testing with Zheng, we found that the time_stepping -> max_delta_time_factor is not taken into account in the c++ routine, but it is used in python to limit the time step size. If not defined in the project parameters, python will set it to 1000.0



Fix (patch):
diff --git a/applications/GeoMechanicsApplication/custom_workflows/adaptive_time_incrementor.cpp b/applications/GeoMechanicsApplication/custom_workflows/adaptive_time_incrementor.cpp
index 52e78fce58a0..016380476369 100644
--- a/applications/GeoMechanicsApplication/custom_workflows/adaptive_time_incrementor.cpp
+++ b/applications/GeoMechanicsApplication/custom_workflows/adaptive_time_incrementor.cpp
@@ -26,6 +26,7 @@ AdaptiveTimeIncrementor::AdaptiveTimeIncrementor(double      StartTime,
                                                  std::size_t MaxNumOfCycles,
                                                  double      ReductionFactor,
                                                  double      IncreaseFactor,
+                                                 double      MaxTimeStepFactor,
                                                  std::size_t MinNumOfIterations,
                                                  std::size_t MaxNumOfIterations) :
     TimeIncrementor(),
@@ -34,6 +35,7 @@ AdaptiveTimeIncrementor::AdaptiveTimeIncrementor(double      StartTime,
     mMaxNumOfCycles(MaxNumOfCycles),
     mReductionFactor(ReductionFactor),
     mIncreaseFactor(IncreaseFactor),
+    mMaxDeltaTime(MaxTimeStepFactor*mDeltaTime),
     mMinNumOfIterations(MinNumOfIterations),
     mMaxNumOfIterations(MaxNumOfIterations)
 {
@@ -44,9 +46,10 @@ AdaptiveTimeIncrementor::AdaptiveTimeIncrementor(double      StartTime,
                                                                 << mMinNumOfIterations
                                                                 << ") is not less than maximum number of iterations ("
                                                                 << mMaxNumOfIterations << ")";
-    KRATOS_ERROR_IF(mReductionFactor >  1.0) << "Reduction factor must not be greater than 1, but got " << mReductionFactor;
-    KRATOS_ERROR_IF(mReductionFactor <= 0.0) << "Reduction factor must be positive, but got " << mReductionFactor;
-    KRATOS_ERROR_IF(mIncreaseFactor  <  1.0) << "Increase factor must be greater than or equal to 1, but got " << mIncreaseFactor;
+    KRATOS_ERROR_IF(mReductionFactor  >  1.0) << "Reduction factor must not be greater than 1, but got " << mReductionFactor;
+    KRATOS_ERROR_IF(mReductionFactor  <= 0.0) << "Reduction factor must be positive, but got " << mReductionFactor;
+    KRATOS_ERROR_IF(mIncreaseFactor   <  1.0) << "Increase factor must be greater than or equal to 1, but got " << mIncreaseFactor;
+    KRATOS_ERROR_IF(MaxTimeStepFactor <  1.0) << "Max_delta_time_factor must be greater than or equal to 1, but got " << MaxTimeStepFactor;
 }
 
 bool AdaptiveTimeIncrementor::WantNextStep(const TimeStepEndState& rPreviousState) const
@@ -77,7 +80,7 @@ void AdaptiveTimeIncrementor::PostTimeStepExecution(const TimeStepEndState& rRes
     }
     else if (rResultantState.Converged() &&
              (rResultantState.num_of_iterations < mMinNumOfIterations)) {
-        mDeltaTime *= mIncreaseFactor;
+        mDeltaTime = std::min(mDeltaTime * mIncreaseFactor, mMaxDeltaTime);
     }
 
     // Avoid incrementing the time beyond the end time
diff --git a/applications/GeoMechanicsApplication/custom_workflows/adaptive_time_incrementor.h b/applications/GeoMechanicsApplication/custom_workflows/adaptive_time_incrementor.h
index 95ae7322839b..c9516aeae780 100644
--- a/applications/GeoMechanicsApplication/custom_workflows/adaptive_time_incrementor.h
+++ b/applications/GeoMechanicsApplication/custom_workflows/adaptive_time_incrementor.h
@@ -28,6 +28,7 @@ class AdaptiveTimeIncrementor : public TimeIncrementor
                             std::size_t MaxNumOfCycles     = 10,
                             double      ReductionFactor    = 0.5,
                             double      IncreaseFactor     = 2.0,
+                            double      MaxTimeStepFactor  = 1000.0,
                             std::size_t MinNumOfIterations = 3,
                             std::size_t MaxNumOfIterations = 15);
 
@@ -43,6 +44,7 @@ class AdaptiveTimeIncrementor : public TimeIncrementor
     std::size_t mMaxNumOfCycles;
     double      mReductionFactor;
     double      mIncreaseFactor;
+    double      mMaxDeltaTime;
     std::size_t mMinNumOfIterations;
     std::size_t mMaxNumOfIterations;
 };
diff --git a/applications/GeoMechanicsApplication/custom_workflows/dgeosettlement.cpp b/applications/GeoMechanicsApplication/custom_workflows/dgeosettlement.cpp
index edab095c9dcf..f677b74698d9 100644
--- a/applications/GeoMechanicsApplication/custom_workflows/dgeosettlement.cpp
+++ b/applications/GeoMechanicsApplication/custom_workflows/dgeosettlement.cpp
@@ -69,6 +69,11 @@ double GetIncreaseFactorFrom(const Parameters& rProjectParameters)
     return rProjectParameters["solver_settings"]["increase_factor"].GetDouble();
 }
 
+double GetMaxDeltaTimeFactorFrom(const Parameters& rProjectParameters)
+{
+   return rProjectParameters["solver_settings"]["time_stepping"]["max_delta_time_factor"].GetDouble();
+}
+
 std::size_t GetMinNumberOfIterationsFrom(const Parameters& rProjectParameters)
 {
     return static_cast<std::size_t>(rProjectParameters["solver_settings"]["min_iterations"].GetInt());
@@ -319,6 +324,7 @@ std::unique_ptr<TimeIncrementor> KratosGeoSettlement::MakeTimeIncrementor(const
                                                      GetMaxNumberOfCyclesFrom(rProjectParameters),
                                                      GetReductionFactorFrom(rProjectParameters),
                                                      GetIncreaseFactorFrom(rProjectParameters),
+                                                     GetMaxDeltaTimeFactorFrom(rProjectParameters),
                                                      GetMinNumberOfIterationsFrom(rProjectParameters),
                                                      GetMaxNumberOfIterationsFrom(rProjectParameters));
 }