From 7f3fb82ba0efef35c3149f77e356ff87888f52fe Mon Sep 17 00:00:00 2001
From: Henry <Henry>
Date: Sun, 8 Mar 2015 16:29:01 +0000
Subject: [PATCH] Time: Handle precision changes on start-up and running more
 robustly Changes based on patches from and discussions with Bruno Santos
 Resolves bug-report http://www.openfoam.org/mantisbt/view.php?id=815

---
 src/OpenFOAM/db/Time/Time.C | 95 ++++++++++++++++++++++++++++++++-----
 src/OpenFOAM/db/Time/Time.H |  3 ++
 2 files changed, 85 insertions(+), 13 deletions(-)

diff --git a/src/OpenFOAM/db/Time/Time.C b/src/OpenFOAM/db/Time/Time.C
index 94c8b9cac46..2ef67d1d07c 100644
--- a/src/OpenFOAM/db/Time/Time.C
+++ b/src/OpenFOAM/db/Time/Time.C
@@ -70,8 +70,11 @@ const Foam::NamedEnum<Foam::Time::writeControls, 5>
     Foam::Time::writeControlNames_;
 
 Foam::Time::fmtflags Foam::Time::format_(Foam::Time::general);
+
 int Foam::Time::precision_(6);
 
+const int Foam::Time::maxPrecision_(3 - log10(SMALL));
+
 Foam::word Foam::Time::controlDictName("controlDict");
 
 
@@ -188,6 +191,56 @@ void Foam::Time::setControls()
     deltaTSave_ = deltaT_;
     deltaT0_ = deltaT_;
 
+    // Check if time directory exists
+    // If not increase time precision to see if it is formatted differently.
+    if (!exists(timePath(), false))
+    {
+        int oldPrecision = precision_;
+        int requiredPrecision = -1;
+        bool found = false;
+        for
+        (
+            precision_ = maxPrecision_;
+            precision_ > oldPrecision;
+            precision_--
+        )
+        {
+            // Update the time formatting
+            setTime(startTime_, 0);
+
+            // Check the existence of the time directory with the new format
+            found = exists(timePath(), false);
+
+            if (found)
+            {
+                requiredPrecision = precision_;
+            }
+        }
+
+        if (requiredPrecision > 0)
+        {
+            // Update the time precision
+            precision_ = requiredPrecision;
+
+            // Update the time formatting
+            setTime(startTime_, 0);
+
+            WarningIn("Time::setControls()")
+                << "Increasing the timePrecision from " << oldPrecision
+                << " to " << precision_
+                << " to support the formatting of the current time directory "
+                << timeName() << nl << endl;
+        }
+        else
+        {
+            // Could not find time directory so assume it is not present
+            precision_ = oldPrecision;
+
+            // Revert the time formatting
+            setTime(startTime_, 0);
+        }
+    }
+
     if (Pstream::parRun())
     {
         scalar sumStartTime = startTime_;
@@ -754,13 +807,13 @@ Foam::instant Foam::Time::findClosestTime(const scalar t) const
     label nearestIndex = -1;
     scalar deltaT = GREAT;
 
-    for (label timeI=1; timeI < timeDirs.size(); ++timeI)
+    for (label timei=1; timei < timeDirs.size(); ++timei)
     {
-        scalar diff = mag(timeDirs[timeI].value() - t);
+        scalar diff = mag(timeDirs[timei].value() - t);
         if (diff < deltaT)
         {
             deltaT = diff;
-            nearestIndex = timeI;
+            nearestIndex = timei;
         }
     }
 
@@ -788,15 +841,15 @@ Foam::label Foam::Time::findClosestTimeIndex
     label nearestIndex = -1;
     scalar deltaT = GREAT;
 
-    forAll(timeDirs, timeI)
+    forAll(timeDirs, timei)
     {
-        if (timeDirs[timeI].name() == constantName) continue;
+        if (timeDirs[timei].name() == constantName) continue;
 
-        scalar diff = mag(timeDirs[timeI].value() - t);
+        scalar diff = mag(timeDirs[timei].value() - t);
         if (diff < deltaT)
         {
             deltaT = diff;
-            nearestIndex = timeI;
+            nearestIndex = timei;
         }
     }
 
@@ -1026,9 +1079,11 @@ Foam::Time& Foam::Time::operator++()
     deltaT0_ = deltaTSave_;
     deltaTSave_ = deltaT_;
 
-    // Save old time name
+    // Save old time value and name
+    const scalar oldTimeValue = value();
     const word oldTimeName = dimensionedScalar::name();
 
+    // Increment time
     setTime(value() + deltaT_, timeIndex_ + 1);
 
     if (!subCycling_)
@@ -1041,8 +1096,16 @@ Foam::Time& Foam::Time::operator++()
     }
 
 
+    // Time value obtained by reading timeName
+    scalar timeNameValue;
+
     // Check that new time representation differs from old one
-    if (dimensionedScalar::name() == oldTimeName)
+    // reinterpretation of the word
+    if
+    (
+        readScalar(dimensionedScalar::name().c_str(), timeNameValue)
+     && (mag(timeNameValue - oldTimeValue - deltaT_) > 100*SMALL)
+    )
     {
         int oldPrecision = precision_;
         do
@@ -1050,17 +1113,23 @@ Foam::Time& Foam::Time::operator++()
             precision_++;
             setTime(value(), timeIndex());
         }
-        while (precision_ < 100 && dimensionedScalar::name() == oldTimeName);
+        while
+        (
+            precision_ < maxPrecision_
+         && readScalar(dimensionedScalar::name().c_str(), timeNameValue)
+         && (mag(timeNameValue - oldTimeValue - deltaT_) > 100*SMALL)
+        );
 
         WarningIn("Time::operator++()")
             << "Increased the timePrecision from " << oldPrecision
             << " to " << precision_
-            << " to distinguish between timeNames at time " << value()
+            << " to distinguish between timeNames at time "
+            << dimensionedScalar::name()
             << endl;
 
-        if (precision_ == 100 && precision_ != oldPrecision)
+        if (precision_ == maxPrecision_ && precision_ != oldPrecision)
         {
-            // Reached limit.
+            // Reached maxPrecision limit
             WarningIn("Time::operator++()")
                 << "Current time name " << dimensionedScalar::name()
                 << " is the old as the previous one " << oldTimeName
diff --git a/src/OpenFOAM/db/Time/Time.H b/src/OpenFOAM/db/Time/Time.H
index 0d7c83818f4..61d499cd9de 100644
--- a/src/OpenFOAM/db/Time/Time.H
+++ b/src/OpenFOAM/db/Time/Time.H
@@ -172,6 +172,9 @@ protected:
         //- Time directory name precision
         static int precision_;
 
+        //- Maximum time directory name precision
+        static const int maxPrecision_;
+
         //- Adjust the time step so that writing occurs at the specified time
         void adjustDeltaT();
 
-- 
GitLab