From bc25f9ab185ca47872b546c0aa6cc522961cfb55 Mon Sep 17 00:00:00 2001
From: mattijs <mattijs>
Date: Thu, 6 Apr 2017 10:26:16 +0100
Subject: [PATCH 1/3] BUG: timeActivatedFileUpdate: was potentially rereading
 itself!

Fixed by setting flag which then gets queried by Time. Fixes #420.
---
 src/OpenFOAM/db/Time/Time.C                   | 11 +++++++
 .../functionObject/functionObject.C           |  8 ++++-
 .../functionObject/functionObject.H           |  5 ++-
 .../functionObjectList/functionObjectList.C   | 17 +++++++++-
 .../functionObjectList/functionObjectList.H   |  5 ++-
 .../timeControl/timeControlFunctionObject.C   | 11 +++++++
 .../timeControl/timeControlFunctionObject.H   |  5 ++-
 .../timeActivatedFileUpdate.C                 | 31 +++++++++----------
 .../timeActivatedFileUpdate.H                 |  8 ++++-
 9 files changed, 79 insertions(+), 22 deletions(-)

diff --git a/src/OpenFOAM/db/Time/Time.C b/src/OpenFOAM/db/Time/Time.C
index 6aabf93eab..59d09611e5 100644
--- a/src/OpenFOAM/db/Time/Time.C
+++ b/src/OpenFOAM/db/Time/Time.C
@@ -943,6 +943,17 @@ bool Foam::Time::run() const
                 addProfiling(functionObjects, "functionObjects.execute()");
                 functionObjects_.execute();
             }
+
+            // Check if the execution of functionObjects require re-reading
+            // any files. This moves effect of e.g. 'timeActivatedFileUpdate'
+            // one time step forward. Note that we cannot call
+            // readModifiedObjects from within timeActivatedFileUpdate since
+            // it might re-read the functionObjects themselves (and delete
+            // the timeActivatedFileUpdate one)
+            if (functionObjects_.filesModified())
+            {
+                const_cast<Time&>(*this).readModifiedObjects();
+            }
         }
 
         // Update the "running" status following the
diff --git a/src/OpenFOAM/db/functionObjects/functionObject/functionObject.C b/src/OpenFOAM/db/functionObjects/functionObject/functionObject.C
index b0a92bc5c1..4ce4821b46 100644
--- a/src/OpenFOAM/db/functionObjects/functionObject/functionObject.C
+++ b/src/OpenFOAM/db/functionObjects/functionObject/functionObject.C
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -146,6 +146,12 @@ bool Foam::functionObject::adjustTimeStep()
 }
 
 
+bool Foam::functionObject::filesModified() const
+{
+    return false;
+}
+
+
 void Foam::functionObject::updateMesh(const mapPolyMesh&)
 {}
 
diff --git a/src/OpenFOAM/db/functionObjects/functionObject/functionObject.H b/src/OpenFOAM/db/functionObjects/functionObject/functionObject.H
index 9c7d56a340..367c0094ee 100644
--- a/src/OpenFOAM/db/functionObjects/functionObject/functionObject.H
+++ b/src/OpenFOAM/db/functionObjects/functionObject/functionObject.H
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -233,6 +233,9 @@ public:
         //- Called at the end of Time::adjustDeltaT() if adjustTime is true
         virtual bool adjustTimeStep();
 
+        //- Did any file get changed during execution?
+        virtual bool filesModified() const;
+
         //- Update for changes of mesh
         virtual void updateMesh(const mapPolyMesh& mpm);
 
diff --git a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C
index 142da30fa6..d64e264eda 100644
--- a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C
+++ b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-     \\/     M anipulation  | Copyright (C) 2015-2016 OpenCFD Ltd.
+     \\/     M anipulation  | Copyright (C) 2015-2017 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -819,6 +819,21 @@ bool Foam::functionObjectList::read()
 }
 
 
+bool Foam::functionObjectList::filesModified() const
+{
+    bool ok = false;
+    if (execution_)
+    {
+        forAll(*this, objectI)
+        {
+            bool changed = operator[](objectI).filesModified();
+            ok = ok || changed;
+        }
+    }
+    return ok;
+}
+
+
 void Foam::functionObjectList::updateMesh(const mapPolyMesh& mpm)
 {
     if (execution_)
diff --git a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.H b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.H
index 41e6a1fd46..2a3efb45ed 100644
--- a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.H
+++ b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.H
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-     \\/     M anipulation  | Copyright (C) 2015-2016 OpenCFD Ltd.
+     \\/     M anipulation  | Copyright (C) 2015-2017 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -264,6 +264,9 @@ public:
         //- Called at the end of Time::adjustDeltaT() if adjustTime is true
         bool adjustTimeStep();
 
+        //- Did any file get changed during execution?
+        bool filesModified() const;
+
         //- Update for changes of mesh
         void updateMesh(const mapPolyMesh& mpm);
 
diff --git a/src/OpenFOAM/db/functionObjects/timeControl/timeControlFunctionObject.C b/src/OpenFOAM/db/functionObjects/timeControl/timeControlFunctionObject.C
index 02b174d419..8e99aebe07 100644
--- a/src/OpenFOAM/db/functionObjects/timeControl/timeControlFunctionObject.C
+++ b/src/OpenFOAM/db/functionObjects/timeControl/timeControlFunctionObject.C
@@ -220,6 +220,17 @@ bool Foam::functionObjects::timeControl::read
 }
 
 
+bool Foam::functionObjects::timeControl::filesModified() const
+{
+    bool mod = false;
+    if (active())
+    {
+        mod = foPtr_->filesModified();
+    }
+    return mod;
+}
+
+
 void Foam::functionObjects::timeControl::updateMesh
 (
     const mapPolyMesh& mpm
diff --git a/src/OpenFOAM/db/functionObjects/timeControl/timeControlFunctionObject.H b/src/OpenFOAM/db/functionObjects/timeControl/timeControlFunctionObject.H
index 9108d12f52..aac5343c42 100644
--- a/src/OpenFOAM/db/functionObjects/timeControl/timeControlFunctionObject.H
+++ b/src/OpenFOAM/db/functionObjects/timeControl/timeControlFunctionObject.H
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2016 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -177,6 +177,9 @@ public:
             //- Called at the end of Time::adjustDeltaT() if adjustTime is true
             virtual bool adjustTimeStep();
 
+            //- Did any file get changed during execution?
+            virtual bool filesModified() const;
+
             //- Read and set the function object if its data have changed
             virtual bool read(const dictionary&);
 
diff --git a/src/functionObjects/utilities/timeActivatedFileUpdate/timeActivatedFileUpdate.C b/src/functionObjects/utilities/timeActivatedFileUpdate/timeActivatedFileUpdate.C
index 18b233e64c..cd74fc9ae1 100644
--- a/src/functionObjects/utilities/timeActivatedFileUpdate/timeActivatedFileUpdate.C
+++ b/src/functionObjects/utilities/timeActivatedFileUpdate/timeActivatedFileUpdate.C
@@ -48,11 +48,10 @@ namespace functionObjects
 
 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
 
-void Foam::functionObjects::timeActivatedFileUpdate::updateFile
-(
-    const bool checkFiles
-)
+void Foam::functionObjects::timeActivatedFileUpdate::updateFile()
 {
+    modified_ = false;
+
     label i = lastIndex_;
     while
     (
@@ -73,14 +72,7 @@ void Foam::functionObjects::timeActivatedFileUpdate::updateFile
         mv(destFile, fileToUpdate_);
         lastIndex_ = i;
 
-        if (checkFiles)
-        {
-            // Do an early check to avoid an additional iteration before
-            // any changes are picked up (see Time::run : does readModified
-            // before executing FOs). Note we have to protect the read
-            // constructor of *this from triggering this behaviour.
-            const_cast<Time&>(time_).Time::readModifiedObjects();
-        }
+        modified_ = true;
     }
 }
 
@@ -98,7 +90,8 @@ Foam::functionObjects::timeActivatedFileUpdate::timeActivatedFileUpdate
     time_(runTime),
     fileToUpdate_("unknown-fileToUpdate"),
     timeVsFile_(),
-    lastIndex_(-1)
+    lastIndex_(-1),
+    modified_(false)
 {
     read(dict);
 }
@@ -142,8 +135,8 @@ bool Foam::functionObjects::timeActivatedFileUpdate::read
             << timeVsFile_[i].second() << endl;
     }
 
-    // Copy starting files. Avoid recursion by not checking for modified files.
-    updateFile(false);
+    // Copy starting files
+    updateFile();
 
     return true;
 }
@@ -151,7 +144,7 @@ bool Foam::functionObjects::timeActivatedFileUpdate::read
 
 bool Foam::functionObjects::timeActivatedFileUpdate::execute()
 {
-    updateFile(true);
+    updateFile();
 
     return true;
 }
@@ -163,4 +156,10 @@ bool Foam::functionObjects::timeActivatedFileUpdate::write()
 }
 
 
+bool Foam::functionObjects::timeActivatedFileUpdate::filesModified() const
+{
+    return modified_;
+}
+
+
 // ************************************************************************* //
diff --git a/src/functionObjects/utilities/timeActivatedFileUpdate/timeActivatedFileUpdate.H b/src/functionObjects/utilities/timeActivatedFileUpdate/timeActivatedFileUpdate.H
index 4e0981ee33..e6fbedcc82 100644
--- a/src/functionObjects/utilities/timeActivatedFileUpdate/timeActivatedFileUpdate.H
+++ b/src/functionObjects/utilities/timeActivatedFileUpdate/timeActivatedFileUpdate.H
@@ -106,11 +106,14 @@ class timeActivatedFileUpdate
         //- Index of last file copied
         label lastIndex_;
 
+        //- Has anything been copied?
+        bool modified_;
+
 
     // Private Member Functions
 
         //- Update file
-        void updateFile(const bool checkFiles);
+        void updateFile();
 
         //- Disallow default bitwise copy construct
         timeActivatedFileUpdate(const timeActivatedFileUpdate&);
@@ -150,6 +153,9 @@ public:
 
         //- Do nothing
         virtual bool write();
+
+        //- Did any file get changed during execution?
+        virtual bool filesModified() const;
 };
 
 
-- 
GitLab


From b6a57b454b80489c51e8370f0416b9c424802216 Mon Sep 17 00:00:00 2001
From: mattijs <mattijs>
Date: Thu, 6 Apr 2017 14:45:16 +0100
Subject: [PATCH 2/3] ENH: timeActivatedFileUpdate: avoid copying on master;
 simplified logic; Fixes #420.

---
 src/OpenFOAM/db/Time/Time.C                          |  1 +
 src/OpenFOAM/db/Time/TimePaths.C                     |  3 +++
 src/OpenFOAM/db/Time/TimePaths.H                     |  9 +++++++++
 src/OpenFOAM/global/argList/argList.C                | 12 +++++++-----
 src/OpenFOAM/global/argList/argList.H                |  5 +++++
 src/OpenFOAM/global/argList/argListI.H               |  8 +++++++-
 .../timeActivatedFileUpdate.C                        | 11 +++++++----
 7 files changed, 39 insertions(+), 10 deletions(-)

diff --git a/src/OpenFOAM/db/Time/Time.C b/src/OpenFOAM/db/Time/Time.C
index 59d09611e5..b1a67daf30 100644
--- a/src/OpenFOAM/db/Time/Time.C
+++ b/src/OpenFOAM/db/Time/Time.C
@@ -468,6 +468,7 @@ Foam::Time::Time
     (
         args.parRunControl().parRun(),
         args.rootPath(),
+        args.distributed(),
         args.globalCaseName(),
         args.caseName(),
         systemName,
diff --git a/src/OpenFOAM/db/Time/TimePaths.C b/src/OpenFOAM/db/Time/TimePaths.C
index ae5b5d182f..05126252c6 100644
--- a/src/OpenFOAM/db/Time/TimePaths.C
+++ b/src/OpenFOAM/db/Time/TimePaths.C
@@ -70,6 +70,7 @@ Foam::TimePaths::TimePaths
 :
     processorCase_(false),
     rootPath_(rootPath),
+    distributed_(false),
     globalCaseName_(caseName),
     case_(caseName),
     system_(systemName),
@@ -84,6 +85,7 @@ Foam::TimePaths::TimePaths
 (
     const bool processorCase,
     const fileName& rootPath,
+    const bool distributed,
     const fileName& globalCaseName,
     const fileName& caseName,
     const word& systemName,
@@ -92,6 +94,7 @@ Foam::TimePaths::TimePaths
 :
     processorCase_(processorCase),
     rootPath_(rootPath),
+    distributed_(distributed),
     globalCaseName_(globalCaseName),
     case_(caseName),
     system_(systemName),
diff --git a/src/OpenFOAM/db/Time/TimePaths.H b/src/OpenFOAM/db/Time/TimePaths.H
index f7bbc25d29..32674008b2 100644
--- a/src/OpenFOAM/db/Time/TimePaths.H
+++ b/src/OpenFOAM/db/Time/TimePaths.H
@@ -53,6 +53,7 @@ class TimePaths
 
         bool processorCase_;
         const fileName rootPath_;
+        bool distributed_;
         fileName globalCaseName_;
         fileName case_;
         const word system_;
@@ -84,6 +85,7 @@ public:
         (
             const bool processorCase,
             const fileName& rootPath,
+            const bool distributed,
             const fileName& globalCaseName,
             const fileName& caseName,
             const word& systemName,
@@ -139,6 +141,13 @@ public:
                 return constant_;
             }
 
+            //- Is case running with parallel distributed directories
+            //  (i.e. not NFS mounted)
+            bool distributed() const
+            {
+                return distributed_;
+            }
+
             //- Return constant name for the case
             //  which for parallel runs returns ../constant()
             fileName caseConstant() const;
diff --git a/src/OpenFOAM/global/argList/argList.C b/src/OpenFOAM/global/argList/argList.C
index 1139bbb8f1..264d75d2e1 100644
--- a/src/OpenFOAM/global/argList/argList.C
+++ b/src/OpenFOAM/global/argList/argList.C
@@ -429,7 +429,8 @@ Foam::argList::argList
 )
 :
     args_(argc),
-    options_(argc)
+    options_(argc),
+    distributed_(false)
 {
     // Check if this run is a parallel run by searching for any parallel option
     // If found call runPar which might filter argv
@@ -669,6 +670,7 @@ void Foam::argList::parse
             label dictNProcs = -1;
             if (options_.found("roots"))
             {
+                distributed_ = true;
                 source = "-roots";
                 IStringStream is(options_["roots"]);
                 roots = readList<fileName>(is);
@@ -700,7 +702,7 @@ void Foam::argList::parse
                     decompDict.lookup("numberOfSubdomains")
                 );
 
-                if (decompDict.lookupOrDefault("distributed", false))
+                if (decompDict.lookupOrDefault("distributed", distributed_))
                 {
                     decompDict.lookup("roots") >> roots;
                 }
@@ -771,7 +773,7 @@ void Foam::argList::parse
                     options_.set("case", roots[slave-1]/globalCase_);
 
                     OPstream toSlave(Pstream::scheduled, slave);
-                    toSlave << args_ << options_;
+                    toSlave << args_ << options_ << roots.size();
                 }
                 options_.erase("case");
 
@@ -818,7 +820,7 @@ void Foam::argList::parse
                 )
                 {
                     OPstream toSlave(Pstream::scheduled, slave);
-                    toSlave << args_ << options_;
+                    toSlave << args_ << options_ << roots.size();
                 }
             }
         }
@@ -826,7 +828,7 @@ void Foam::argList::parse
         {
             // Collect the master's argument list
             IPstream fromMaster(Pstream::scheduled, Pstream::masterNo());
-            fromMaster >> args_ >> options_;
+            fromMaster >> args_ >> options_ >> distributed_;
 
             // Establish rootPath_/globalCase_/case_ for slave
             getRootCase();
diff --git a/src/OpenFOAM/global/argList/argList.H b/src/OpenFOAM/global/argList/argList.H
index f263574453..8ff04458c4 100644
--- a/src/OpenFOAM/global/argList/argList.H
+++ b/src/OpenFOAM/global/argList/argList.H
@@ -109,6 +109,7 @@ class argList
 
         word executable_;
         fileName rootPath_;
+        bool distributed_;
         fileName globalCase_;
         fileName case_;
         string argListStr_;
@@ -221,6 +222,10 @@ public:
             //- Return root path
             inline const fileName& rootPath() const;
 
+            //- Return distributed flag (i.e. are rootPaths different on
+            //  different machines)
+            inline bool distributed() const;
+
             //- Return case name (parallel run) or global case (serial run)
             inline const fileName& caseName() const;
 
diff --git a/src/OpenFOAM/global/argList/argListI.H b/src/OpenFOAM/global/argList/argListI.H
index 47ad29a58e..28caff6acb 100644
--- a/src/OpenFOAM/global/argList/argListI.H
+++ b/src/OpenFOAM/global/argList/argListI.H
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2013 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -39,6 +39,12 @@ inline const Foam::fileName& Foam::argList::rootPath() const
 }
 
 
+inline bool Foam::argList::distributed() const
+{
+    return distributed_;
+}
+
+
 inline const Foam::fileName& Foam::argList::caseName() const
 {
     return case_;
diff --git a/src/functionObjects/utilities/timeActivatedFileUpdate/timeActivatedFileUpdate.C b/src/functionObjects/utilities/timeActivatedFileUpdate/timeActivatedFileUpdate.C
index cd74fc9ae1..ed48e24fcf 100644
--- a/src/functionObjects/utilities/timeActivatedFileUpdate/timeActivatedFileUpdate.C
+++ b/src/functionObjects/utilities/timeActivatedFileUpdate/timeActivatedFileUpdate.C
@@ -67,11 +67,14 @@ void Foam::functionObjects::timeActivatedFileUpdate::updateFile()
         Log << nl << type() << ": copying file" << nl << timeVsFile_[i].second()
             << nl << "to:" << nl << fileToUpdate_ << nl << endl;
 
-        fileName destFile(fileToUpdate_ + Foam::name(pid()));
-        cp(timeVsFile_[i].second(), destFile);
-        mv(destFile, fileToUpdate_);
+        if (Pstream::master() || time_.distributed())
+        {
+            // Slaves do not copy if running non-distributed
+            fileName destFile(fileToUpdate_ + Foam::name(pid()));
+            cp(timeVsFile_[i].second(), destFile);
+            mv(destFile, fileToUpdate_);
+        }
         lastIndex_ = i;
-
         modified_ = true;
     }
 }
-- 
GitLab


From 898e0eb4aae61ee7dfc440973dddd4c57547804b Mon Sep 17 00:00:00 2001
From: mattijs <mattijs>
Date: Wed, 19 Apr 2017 09:28:12 +0100
Subject: [PATCH 3/3] BUG: timeActivatedFileUpdate: fix setting of distributed
 from decomposeParDict. Fixes #420.

---
 src/OpenFOAM/global/argList/argList.C | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/OpenFOAM/global/argList/argList.C b/src/OpenFOAM/global/argList/argList.C
index 264d75d2e1..198a1b4e20 100644
--- a/src/OpenFOAM/global/argList/argList.C
+++ b/src/OpenFOAM/global/argList/argList.C
@@ -702,8 +702,9 @@ void Foam::argList::parse
                     decompDict.lookup("numberOfSubdomains")
                 );
 
-                if (decompDict.lookupOrDefault("distributed", distributed_))
+                if (decompDict.lookupOrDefault("distributed", false))
                 {
+                    distributed_ = true;
                     decompDict.lookup("roots") >> roots;
                 }
             }
-- 
GitLab