From 610c2909695aea1c114cd574a6eb0990a082cba7 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Tue, 28 Nov 2017 10:00:15 +0100
Subject: [PATCH] ENH: added functionObject::execute(int) method

- this is a provision for defining execute actions that can be called
  largely independently of the normal time-loop constraints. This can
  be useful to provide hooks for sub-cycling, or to define an action
  that can be triggered manually or on some other event.
---
 .../functionObject/functionObject.C           |  6 +++
 .../functionObject/functionObject.H           | 10 ++++-
 .../functionObjectList/functionObjectList.C   | 45 +++++++++++++++++++
 .../functionObjectList/functionObjectList.H   | 32 +++++++++----
 .../timeControl/timeControlFunctionObject.C   | 18 ++++++--
 .../timeControl/timeControlFunctionObject.H   |  3 ++
 6 files changed, 100 insertions(+), 14 deletions(-)

diff --git a/src/OpenFOAM/db/functionObjects/functionObject/functionObject.C b/src/OpenFOAM/db/functionObjects/functionObject/functionObject.C
index 51e1323ec8d..a13a0441573 100644
--- a/src/OpenFOAM/db/functionObjects/functionObject/functionObject.C
+++ b/src/OpenFOAM/db/functionObjects/functionObject/functionObject.C
@@ -141,6 +141,12 @@ bool Foam::functionObject::read(const dictionary& dict)
 }
 
 
+bool Foam::functionObject::execute(const label)
+{
+    return true;
+}
+
+
 bool Foam::functionObject::end()
 {
     return true;
diff --git a/src/OpenFOAM/db/functionObjects/functionObject/functionObject.H b/src/OpenFOAM/db/functionObjects/functionObject/functionObject.H
index febb76de10c..3f47feba8c4 100644
--- a/src/OpenFOAM/db/functionObjects/functionObject/functionObject.H
+++ b/src/OpenFOAM/db/functionObjects/functionObject/functionObject.H
@@ -222,13 +222,19 @@ public:
         const word& name() const;
 
         //- Read and set the function object if its data have changed
-        virtual bool read(const dictionary&);
+        virtual bool read(const dictionary& dict);
 
         //- Called at each ++ or += of the time-loop.
         //  postProcess overrides the usual executeControl behaviour and
         //  forces execution (used in post-processing mode)
         virtual bool execute() = 0;
 
+        //- Execute using the specified subIndex.
+        //  The base implementation is a no-op.
+        //  \param subIndex an execution sub-index corresponding to a
+        //      sub-cycle or something similar.
+        virtual bool execute(const label subIndex);
+
         //- Called at each ++ or += of the time-loop.
         //  postProcess overrides the usual writeControl behaviour and
         //  forces writing always (used in post-processing mode)
@@ -245,9 +251,11 @@ public:
         virtual bool filesModified() const;
 
         //- Update for changes of mesh
+        //  The base implementation is a no-op.
         virtual void updateMesh(const mapPolyMesh& mpm);
 
         //- Update for changes of mesh
+        //  The base implementation is a no-op.
         virtual void movePoints(const polyMesh& mesh);
 };
 
diff --git a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C
index 31bf1d40d76..98174a669e7 100644
--- a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C
+++ b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C
@@ -32,6 +32,7 @@ License
 //#include "IFstream.H"
 #include "dictionaryEntry.H"
 #include "stringOps.H"
+#include "wordRes.H"
 #include "Tuple2.H"
 #include "etcFiles.H"
 #include "IOdictionary.H"
@@ -579,6 +580,7 @@ bool Foam::functionObjectList::execute()
             }
         }
     }
+
     // Force writing of state dictionary after function object execution
     if (time_.writeTime())
     {
@@ -600,6 +602,49 @@ bool Foam::functionObjectList::execute()
 }
 
 
+bool Foam::functionObjectList::execute(const label subIndex)
+{
+    bool ok = execution_;
+
+    if (ok)
+    {
+        forAll(*this, obji)
+        {
+            functionObject& funcObj = operator[](obji);
+
+            ok = funcObj.execute(subIndex) && ok;
+        }
+    }
+
+    return ok;
+}
+
+
+bool Foam::functionObjectList::execute
+(
+    const wordRes& functionNames,
+    const label subIndex
+)
+{
+    bool ok = execution_;
+
+    if (ok && functionNames.size())
+    {
+        forAll(*this, obji)
+        {
+            functionObject& funcObj = operator[](obji);
+
+            if (functionNames.match(funcObj.name()))
+            {
+                ok = funcObj.execute(subIndex) && ok;
+            }
+        }
+    }
+
+    return ok;
+}
+
+
 bool Foam::functionObjectList::end()
 {
     bool ok = true;
diff --git a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.H b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.H
index c90318727b9..e32b01e25b0 100644
--- a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.H
+++ b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.H
@@ -52,8 +52,10 @@ SourceFiles
 namespace Foam
 {
 
-class mapPolyMesh;
+// Forward declarations
 class argList;
+class mapPolyMesh;
+class wordRes;
 
 /*---------------------------------------------------------------------------*\
                      Class functionObjectList Declaration
@@ -94,19 +96,19 @@ class functionObjectList
         void createStateDict() const;
 
         //- Remove and return the function object pointer by name,
-        //  and returns the old index via the parameter.
+        //- and returns the old index via the parameter.
         //  Returns a nullptr (and index -1) if it didn't exist
-        functionObject* remove(const word&, label& oldIndex);
+        functionObject* remove(const word& key, label& oldIndex);
 
         //- Search the specified directory for functionObject
-        //  configuration files, add to the given map and recurse
+        //- configuration files, add to the given map and recurse
         static void listDir(const fileName& dir, HashSet<word>& foMap);
 
         //- Disallow default bitwise copy construct
-        functionObjectList(const functionObjectList&);
+        functionObjectList(const functionObjectList&) = delete;
 
         //- Disallow default bitwise assignment
-        void operator=(const functionObjectList&);
+        void operator=(const functionObjectList&) = delete;
 
 
 public:
@@ -114,7 +116,7 @@ public:
     // Static data members
 
         //- Default relative path to the directory structure
-        //  containing the functionObject dictionary files
+        //- containing the functionObject dictionary files
         static fileName functionObjectDictPath;
 
 
@@ -188,7 +190,7 @@ public:
         label findObjectID(const word& name) const;
 
         //- Print a list of functionObject configuration files in
-        //  user/group/shipped directories.
+        //- user/group/shipped directories.
         //  The search scheme allows for version-specific and
         //  version-independent files using the following hierarchy:
         //  - \b user settings:
@@ -205,7 +207,7 @@ public:
         static void list();
 
         //- Search for functionObject dictionary file in
-        //  user/group/shipped directories.
+        //- user/group/shipped directories.
         //  The search scheme allows for version-specific and
         //  version-independent files using the following hierarchy:
         //  - \b user settings:
@@ -258,6 +260,18 @@ public:
         //  forces execution (used in post-processing mode)
         bool execute();
 
+        //- Execute function objects using the specified subIndex.
+        //  \param subIndex an execution sub-index corresponding to a
+        //      sub-cycle or something similar
+        bool execute(const label subIndex);
+
+        //- Execute a subset of function objects using the specified subIndex.
+        //  \param functionNames names or regex of existing functions to
+        //      execute
+        //  \param subIndex an execution sub-index corresponding to a
+        //      sub-cycle or something similar
+        bool execute(const wordRes& functionNames, const label subIndex);
+
         //- Called when Time::run() determines that the time-loop exits
         bool end();
 
diff --git a/src/OpenFOAM/db/functionObjects/timeControl/timeControlFunctionObject.C b/src/OpenFOAM/db/functionObjects/timeControl/timeControlFunctionObject.C
index bbb495cb98d..96e088fe2dd 100644
--- a/src/OpenFOAM/db/functionObjects/timeControl/timeControlFunctionObject.C
+++ b/src/OpenFOAM/db/functionObjects/timeControl/timeControlFunctionObject.C
@@ -450,6 +450,18 @@ bool Foam::functionObjects::timeControl::execute()
 }
 
 
+bool Foam::functionObjects::timeControl::execute(const label subIndex)
+{
+    if (active())
+    {
+        // Call underlying function object directly
+        foPtr_->execute(subIndex);
+    }
+
+    return true;
+}
+
+
 bool Foam::functionObjects::timeControl::write()
 {
     if (active() && (postProcess || writeControl_.execute()))
@@ -726,10 +738,8 @@ bool Foam::functionObjects::timeControl::read(const dictionary& dict)
 
         return true;
     }
-    else
-    {
-        return false;
-    }
+
+    return false;
 }
 
 
diff --git a/src/OpenFOAM/db/functionObjects/timeControl/timeControlFunctionObject.H b/src/OpenFOAM/db/functionObjects/timeControl/timeControlFunctionObject.H
index 4b4bca78e16..18a3e51461c 100644
--- a/src/OpenFOAM/db/functionObjects/timeControl/timeControlFunctionObject.H
+++ b/src/OpenFOAM/db/functionObjects/timeControl/timeControlFunctionObject.H
@@ -206,6 +206,9 @@ public:
             //  forces execution (used in post-processing mode)
             virtual bool execute();
 
+            //- Execute using the specified subIndex.
+            virtual bool execute(const label subIndex);
+
             //- Called at each ++ or += of the time-loop.
             //  postProcess overrides the usual writeControl behaviour and
             //  forces writing (used in post-processing mode)
-- 
GitLab