From 051afeb4195066370c2d188b56440964f8ea7810 Mon Sep 17 00:00:00 2001
From: Henry <Henry>
Date: Wed, 14 Nov 2012 15:47:04 +0000
Subject: [PATCH] subCycle: Updated handling of the old-time field and
 timeIndex to support outer-iterations

---
 src/OpenFOAM/algorithms/subCycle/subCycle.H | 37 ++++++++++++++++-----
 1 file changed, 29 insertions(+), 8 deletions(-)

diff --git a/src/OpenFOAM/algorithms/subCycle/subCycle.H b/src/OpenFOAM/algorithms/subCycle/subCycle.H
index a4a1ea1844c..f0c8958dd0c 100644
--- a/src/OpenFOAM/algorithms/subCycle/subCycle.H
+++ b/src/OpenFOAM/algorithms/subCycle/subCycle.H
@@ -51,8 +51,13 @@ class subCycleField
         //- Reference to the field being sub-cycled
         GeometricField& gf_;
 
+        //- Reference to the field old-time field being sub-cycled
+        //  Needed to avoid calls to oldTime() which may cause
+        //  unexpected updates of the old-time field
+        GeometricField& gf0_;
+
         //- Copy of the "real" old-time value of the field
-        GeometricField  gf0_;
+        GeometricField gf_0_;
 
 
 public:
@@ -63,19 +68,33 @@ public:
         subCycleField(GeometricField& gf)
         :
             gf_(gf),
-            gf0_(gf.oldTime())
+            gf0_(gf.oldTime()),
+            gf_0_(gf0_.name() + "_", gf0_)
         {}
 
 
     //- Destructor
     ~subCycleField()
     {
+        // Reset the old-time field
+        gf0_ = gf_0_;
+
         // Correct the time index of the field to correspond to the global time
         gf_.timeIndex() = gf_.time().timeIndex();
+        gf0_.timeIndex() = gf_.time().timeIndex();
+    }
+
 
-        // Reset the old-time field value
-        gf_.oldTime() = gf0_;
-        gf_.oldTime().timeIndex() = gf0_.timeIndex();
+    //- Correct the time index of the field to correspond to
+    //  the sub-cycling time.
+    //
+    //  The time index is incremented to protect the old-time value from
+    //  being updated at the beginning of the time-loop in the case of
+    //  outer iteration
+    void updateTimeIndex()
+    {
+        gf_.timeIndex() = gf_.time().timeIndex() + 1;
+        gf0_.timeIndex() = gf_.time().timeIndex() + 1;
     }
 };
 
@@ -106,16 +125,18 @@ public:
         //- Construct field and number of sub-cycles
         subCycle(GeometricField& gf, const label nSubCycles)
         :
-
             subCycleField<GeometricField>(gf),
             subCycleTime(const_cast<Time&>(gf.time()), nSubCycles)
-        {}
+        {
+            // Update the field time index to correspond to the sub-cycle time
+            this->updateTimeIndex();
+        }
 
 
     //- Destructor
+    //  End the subCycleTime, which restores the time state
     ~subCycle()
     {
-        // End the subCycleTime, which restores the time state
         endSubCycle();
     }
 };
-- 
GitLab