From e4f7c306ab8f28040a63866defafc9687fbb4a2d Mon Sep 17 00:00:00 2001
From: mattijs <mattijs>
Date: Tue, 16 Oct 2012 14:15:14 +0100
Subject: [PATCH] BUG: Time: have separate timer for secondary write interval

---
 src/OpenFOAM/db/Time/Time.C      | 44 +++++++++++++++++++++-----------
 src/OpenFOAM/db/Time/TimeIO.C    | 22 ++++++++++++++++
 src/OpenFOAM/db/Time/TimeState.C |  3 ++-
 src/OpenFOAM/db/Time/TimeState.H |  4 +--
 4 files changed, 55 insertions(+), 18 deletions(-)

diff --git a/src/OpenFOAM/db/Time/Time.C b/src/OpenFOAM/db/Time/Time.C
index 19a6c2b321f..155c4c2eb48 100644
--- a/src/OpenFOAM/db/Time/Time.C
+++ b/src/OpenFOAM/db/Time/Time.C
@@ -79,21 +79,35 @@ Foam::word Foam::Time::controlDictName("controlDict");
 
 void Foam::Time::adjustDeltaT()
 {
+    bool adjustTime = false;
+    scalar timeToNextWrite = VGREAT;
+
     if (writeControl_ == wcAdjustableRunTime)
     {
-        scalar interval = writeInterval_;
-        if (secondaryWriteControl_ == wcAdjustableRunTime)
-        {
-            interval = min(interval, secondaryWriteInterval_);
-        }
-
-
-        scalar timeToNextWrite = max
+        adjustTime = true;
+        timeToNextWrite = max
+        (
+            0.0,
+            (outputTimeIndex_ + 1)*writeInterval_ - (value() - startTime_)
+        );
+    }
+    if (secondaryWriteControl_ == wcAdjustableRunTime)
+    {
+        adjustTime = true;
+        timeToNextWrite = max
         (
             0.0,
-            (outputTimeIndex_ + 1)*interval - (value() - startTime_)
+            min
+            (
+                timeToNextWrite,
+                (secondaryOutputTimeIndex_ + 1)*secondaryWriteInterval_
+              - (value() - startTime_)
+            )
         );
+    }
 
+    if (adjustTime)
+    {
         scalar nSteps = timeToNextWrite/deltaT_ - SMALL;
 
         // For tiny deltaT the label can overflow!
@@ -1130,10 +1144,10 @@ Foam::Time& Foam::Time::operator++()
                   / secondaryWriteInterval_
                 );
 
-                if (outputIndex > outputTimeIndex_)
+                if (outputIndex > secondaryOutputTimeIndex_)
                 {
                     outputTime_ = true;
-                    outputTimeIndex_ = outputIndex;
+                    secondaryOutputTimeIndex_ = outputIndex;
                 }
             }
             break;
@@ -1145,10 +1159,10 @@ Foam::Time& Foam::Time::operator++()
                     returnReduce(elapsedCpuTime(), maxOp<double>())
                   / secondaryWriteInterval_
                 );
-                if (outputIndex > outputTimeIndex_)
+                if (outputIndex > secondaryOutputTimeIndex_)
                 {
                     outputTime_ = true;
-                    outputTimeIndex_ = outputIndex;
+                    secondaryOutputTimeIndex_ = outputIndex;
                 }
             }
             break;
@@ -1160,10 +1174,10 @@ Foam::Time& Foam::Time::operator++()
                     returnReduce(label(elapsedClockTime()), maxOp<label>())
                   / secondaryWriteInterval_
                 );
-                if (outputIndex > outputTimeIndex_)
+                if (outputIndex > secondaryOutputTimeIndex_)
                 {
                     outputTime_ = true;
-                    outputTimeIndex_ = outputIndex;
+                    secondaryOutputTimeIndex_ = outputIndex;
                 }
             }
             break;
diff --git a/src/OpenFOAM/db/Time/TimeIO.C b/src/OpenFOAM/db/Time/TimeIO.C
index 00f1beefbea..6a80618404e 100644
--- a/src/OpenFOAM/db/Time/TimeIO.C
+++ b/src/OpenFOAM/db/Time/TimeIO.C
@@ -51,6 +51,8 @@ void Foam::Time::readDict()
     }
 
     scalar oldWriteInterval = writeInterval_;
+    scalar oldSecondaryWriteInterval = secondaryWriteInterval_;
+
     if (controlDict_.readIfPresent("writeInterval", writeInterval_))
     {
         if (writeControl_ == wcTimeStep && label(writeInterval_) < 1)
@@ -124,6 +126,26 @@ void Foam::Time::readDict()
             break;
         }
     }
+    if (oldSecondaryWriteInterval != secondaryWriteInterval_)
+    {
+        switch (secondaryWriteControl_)
+        {
+            case wcRunTime:
+            case wcAdjustableRunTime:
+                // Recalculate secondaryOutputTimeIndex_ to be in units of
+                // current writeInterval.
+                secondaryOutputTimeIndex_ = label
+                (
+                    secondaryOutputTimeIndex_
+                  * oldSecondaryWriteInterval
+                  / secondaryWriteInterval_
+                );
+            break;
+
+            default:
+            break;
+        }
+    }
 
     if (controlDict_.readIfPresent("purgeWrite", purgeWrite_))
     {
diff --git a/src/OpenFOAM/db/Time/TimeState.C b/src/OpenFOAM/db/Time/TimeState.C
index b02b87bbfc1..500ca47d43f 100644
--- a/src/OpenFOAM/db/Time/TimeState.C
+++ b/src/OpenFOAM/db/Time/TimeState.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2011-2012 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -37,6 +37,7 @@ Foam::TimeState::TimeState()
     deltaT0_(0),
     deltaTchanged_(false),
     outputTimeIndex_(0),
+    secondaryOutputTimeIndex_(0),
     outputTime_(false)
 {}
 
diff --git a/src/OpenFOAM/db/Time/TimeState.H b/src/OpenFOAM/db/Time/TimeState.H
index 1c59ca85a3d..d721529480e 100644
--- a/src/OpenFOAM/db/Time/TimeState.H
+++ b/src/OpenFOAM/db/Time/TimeState.H
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
+    \\  /    A nd           | Copyright (C) 2011-2012 OpenFOAM Foundation
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
 License
@@ -61,9 +61,9 @@ protected:
         bool deltaTchanged_;
 
         label outputTimeIndex_;
+        label secondaryOutputTimeIndex_;
         bool  outputTime_;
 
-
 public:
 
     // Constructors
-- 
GitLab