From 1ff9ae9ff57aaab56d1dcfb9e60ee8ee5969dd4a Mon Sep 17 00:00:00 2001
From: mattijs <mattijs>
Date: Mon, 24 Oct 2016 13:07:51 +0100
Subject: [PATCH] ENH: setTimeStep: adjust timestep for adjustableRunTime
 writeInterval. Fixes #268.

---
 .../setTimeStep/setTimeStepFunctionObject.C         | 13 +++++++++++--
 .../setTimeStep/setTimeStepFunctionObject.H         |  6 +++++-
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/src/postProcessing/functionObjects/utilities/setTimeStep/setTimeStepFunctionObject.C b/src/postProcessing/functionObjects/utilities/setTimeStep/setTimeStepFunctionObject.C
index 0ae37ba0b7d..261b7ea535e 100644
--- a/src/postProcessing/functionObjects/utilities/setTimeStep/setTimeStepFunctionObject.C
+++ b/src/postProcessing/functionObjects/utilities/setTimeStep/setTimeStepFunctionObject.C
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2013-2016 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2016 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -103,7 +103,16 @@ bool Foam::setTimeStepFunctionObject::adjustTimeStep()
         // Wanted timestep
         scalar newDeltaT = timeStepPtr_().value(time_.timeOutputValue());
 
-        const_cast<Time&>(time()).setDeltaT(newDeltaT, false);
+        static label index = -1;
+
+        if (time().timeIndex() != index)
+        {
+            // Store current time so we don't get infinite recursion (since
+            // setDeltaT calls adjustTimeStep() again)
+            index = time().timeIndex();
+            // Set time, allow deltaT to be adjusted for writeInterval purposes
+            const_cast<Time&>(time()).setDeltaT(newDeltaT, true);
+        }
 
         return true;
     }
diff --git a/src/postProcessing/functionObjects/utilities/setTimeStep/setTimeStepFunctionObject.H b/src/postProcessing/functionObjects/utilities/setTimeStep/setTimeStepFunctionObject.H
index f345ea161b7..0d46f98d9e4 100644
--- a/src/postProcessing/functionObjects/utilities/setTimeStep/setTimeStepFunctionObject.H
+++ b/src/postProcessing/functionObjects/utilities/setTimeStep/setTimeStepFunctionObject.H
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2013-2016 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2016 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -34,6 +34,10 @@ Description
     functionObjects. Supports 'enabled' flag but none of the other options
     'timeStart', 'timeEnd', 'outputControl' etc.
 
+    Note that any 'writeControl adjustableRunTime' will still be in
+    operation and will limit the wanted time step to account for the
+    - write interval and - timestep adjustment speed (time step cannot
+    change too quickly).
 
     Example of function object specification to manipulate the time step:
     \verbatim
-- 
GitLab