From 0adcd1ec47305576d9c9d91adec171b78e9bcb95 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Wed, 15 May 2019 09:21:45 +0100
Subject: [PATCH] ENH: timeInfo function object (#1320)

- records execution and wallclock times to postProcessing/
  which can be more convenient than parsing a log file.
---
 src/functionObjects/utilities/Make/files      |   1 +
 .../utilities/timeInfo/timeInfo.C             | 131 ++++++++++++++
 .../utilities/timeInfo/timeInfo.H             | 161 ++++++++++++++++++
 .../simpleFoam/pipeCyclic/system/controlDict  |   1 +
 .../simpleFoam/pipeCyclic/system/timeInfo     |  15 ++
 5 files changed, 309 insertions(+)
 create mode 100644 src/functionObjects/utilities/timeInfo/timeInfo.C
 create mode 100644 src/functionObjects/utilities/timeInfo/timeInfo.H
 create mode 100644 tutorials/incompressible/simpleFoam/pipeCyclic/system/timeInfo

diff --git a/src/functionObjects/utilities/Make/files b/src/functionObjects/utilities/Make/files
index 79c11e83e2c..c3f384ee696 100644
--- a/src/functionObjects/utilities/Make/files
+++ b/src/functionObjects/utilities/Make/files
@@ -15,6 +15,7 @@ removeRegisteredObject/removeRegisteredObject.C
 parProfiling/parProfiling.C
 
 solverInfo/solverInfo.C
+timeInfo/timeInfo.C
 
 runTimeControl/runTimeControl.C
 runTimeControl/runTimeCondition/runTimeCondition/runTimeCondition.C
diff --git a/src/functionObjects/utilities/timeInfo/timeInfo.C b/src/functionObjects/utilities/timeInfo/timeInfo.C
new file mode 100644
index 00000000000..ba5767de8e5
--- /dev/null
+++ b/src/functionObjects/utilities/timeInfo/timeInfo.C
@@ -0,0 +1,131 @@
+/*---------------------------------------------------------------------------*\
+  =========                 |
+  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
+   \\    /   O peration     |
+    \\  /    A nd           | Copyright (C) 2019 OpenCFD Ltd.
+     \\/     M anipulation  |
+-------------------------------------------------------------------------------
+                            | Copyright (C) 2018 OpenFOAM Foundation
+-------------------------------------------------------------------------------
+License
+    This file is part of OpenFOAM.
+    OpenFOAM is free software: you can redistribute it and/or modify it
+    under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
+    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+    for more details.
+    You should have received a copy of the GNU General Public License
+    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
+\*---------------------------------------------------------------------------*/
+
+#include "timeInfo.H"
+#include "addToRunTimeSelectionTable.H"
+
+// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
+
+namespace Foam
+{
+namespace functionObjects
+{
+    defineTypeNameAndDebug(timeInfo, 0);
+
+    addToRunTimeSelectionTable
+    (
+        functionObject,
+        timeInfo,
+        dictionary
+    );
+}
+}
+
+
+// * * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * //
+
+void Foam::functionObjects::timeInfo::writeFileHeader(Ostream& os)
+{
+    writeCommented(os, "Time");
+    writeTabbed(os, "cpuTime");
+    writeTabbed(os, "clockTime");
+
+    if (perTimeStep_)
+    {
+        writeTabbed(os, "cpu/step");
+        writeTabbed(os, "clock/step");
+    }
+
+    os << nl;
+}
+
+
+// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
+
+Foam::functionObjects::timeInfo::timeInfo
+(
+    const word& name,
+    const Time& runTime,
+    const dictionary& dict
+)
+:
+    timeFunctionObject(name, runTime),
+    writeFile(time_, name, typeName, dict),
+    cpuTime0_(Zero),
+    clockTime0_(Zero),
+    perTimeStep_(false)
+{
+    read(dict);
+    writeFileHeader(file());
+}
+
+
+// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
+
+bool Foam::functionObjects::timeInfo::read(const dictionary& dict)
+{
+    timeFunctionObject::read(dict);
+    writeFile::read(dict);
+
+    perTimeStep_ = dict.lookupOrDefault("perTimeStep", false);
+    return true;
+}
+
+
+bool Foam::functionObjects::timeInfo::execute()
+{
+    return true;
+}
+
+
+bool Foam::functionObjects::timeInfo::write()
+{
+    if (Pstream::master())
+    {
+        writeTime(file());
+
+        const scalar cpuTimeNow(time_.elapsedCpuTime());
+        const scalar clockTimeNow(time_.elapsedClockTime());
+
+        file()
+            << tab << cpuTimeNow
+            << tab << clockTimeNow;
+
+        if (perTimeStep_)
+        {
+            file()
+                << tab << (cpuTimeNow - cpuTime0_)
+                << tab << (clockTimeNow - clockTime0_);
+
+            cpuTime0_ = cpuTimeNow;
+            clockTime0_ = clockTimeNow;
+        }
+
+        file() << nl;
+    }
+
+    return true;
+}
+
+
+// ************************************************************************* //
diff --git a/src/functionObjects/utilities/timeInfo/timeInfo.H b/src/functionObjects/utilities/timeInfo/timeInfo.H
new file mode 100644
index 00000000000..ca27a8d89c5
--- /dev/null
+++ b/src/functionObjects/utilities/timeInfo/timeInfo.H
@@ -0,0 +1,161 @@
+/*---------------------------------------------------------------------------*\
+  =========                 |
+  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
+   \\    /   O peration     |
+    \\  /    A nd           | Copyright (C) 2019 OpenCFD Ltd.
+     \\/     M anipulation  |
+-------------------------------------------------------------------------------
+                            | Copyright (C) 2018 OpenFOAM Foundation
+-------------------------------------------------------------------------------
+License
+    This file is part of OpenFOAM.
+
+    OpenFOAM is free software: you can redistribute it and/or modify it
+    under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
+    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+    for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
+
+Class
+    Foam::functionObjects::timeInfo
+
+Description
+    Writes the run time (time-step), cpuTime and clockTime -
+    optionally with cpuTime and clockTime change for each time step.
+
+    Example of function object specification:
+    \verbatim
+    time
+    {
+        type            timeInfo;
+
+        libs            ("libutilityFunctionObjects.so");
+
+        writeControl    timeStep;
+        writeInterval   1;
+
+        perTimeStep     no;
+    }
+    \endverbatim
+
+    Where the entries comprise:
+    \table
+        Property      | Description                         | Required | Default
+        type          | Type name: timeInfo                 | yes |
+        writeToFile   | Write information to file           | no  | yes
+        perTimeStep   | Write value per interval/step       | no  | no
+    \endtable
+
+    The initial per-step value is likely to be inaccurate and should
+    mostly be ignored.
+
+See also
+    Foam::functionObject
+    Foam::timeFunctionObject
+    Foam::functionObjects::writeFile
+
+SourceFiles
+    timeInfo.C
+
+\*---------------------------------------------------------------------------*/
+
+#ifndef functionObjects_timeInfo_H
+#define functionObjects_timeInfo_H
+
+#include "timeFunctionObject.H"
+#include "writeFile.H"
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+namespace Foam
+{
+namespace functionObjects
+{
+
+/*---------------------------------------------------------------------------*\
+                          Class timeInfo Declaration
+\*---------------------------------------------------------------------------*/
+
+class timeInfo
+:
+    public timeFunctionObject,
+    public writeFile
+{
+    // Private Member Data
+
+        //- The cpuTime from last write/time-step (for perTimeStep)
+        scalar cpuTime0_;
+
+        //- The clockTime from last write/time-step (for perTimeStep)
+        scalar clockTime0_;
+
+        //- Flag to write cpuTime and clockTime per time-step
+        bool perTimeStep_;
+
+
+protected:
+
+    // Protected Member Functions
+
+        //- Output file header information
+        virtual void writeFileHeader(Ostream& os);
+
+        //- No copy construct
+        timeInfo(const timeInfo&) = delete;
+
+        //- No copy assignment
+        void operator=(const timeInfo&) = delete;
+
+
+public:
+
+    //- Runtime type information
+    TypeName("timeInfo");
+
+
+    // Constructors
+
+        //- Construct from Time and dictionary
+        timeInfo
+        (
+            const word& name,
+            const Time& runTime,
+            const dictionary& dict
+        );
+
+
+    //- Destructor
+    virtual ~timeInfo() = default;
+
+
+    // Member Functions
+
+        //- Read the controls
+        virtual bool read(const dictionary& dict);
+
+        //- Execute, does nothing
+        virtual bool execute();
+
+        //- Write the timeInfo
+        virtual bool write();
+};
+
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+} // End namespace functionObjects
+} // End namespace Foam
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+#endif
+
+// ************************************************************************* //
+
diff --git a/tutorials/incompressible/simpleFoam/pipeCyclic/system/controlDict b/tutorials/incompressible/simpleFoam/pipeCyclic/system/controlDict
index 7c7299ea034..644a080d011 100644
--- a/tutorials/incompressible/simpleFoam/pipeCyclic/system/controlDict
+++ b/tutorials/incompressible/simpleFoam/pipeCyclic/system/controlDict
@@ -49,6 +49,7 @@ functions
 {
     #include "coordinateTransform"
     #include "momentum"
+    #include "timeInfo"
 }
 
 
diff --git a/tutorials/incompressible/simpleFoam/pipeCyclic/system/timeInfo b/tutorials/incompressible/simpleFoam/pipeCyclic/system/timeInfo
new file mode 100644
index 00000000000..ba6558a1aae
--- /dev/null
+++ b/tutorials/incompressible/simpleFoam/pipeCyclic/system/timeInfo
@@ -0,0 +1,15 @@
+// -*- C++ -*-
+// Record execution/clock time
+time
+{
+    type    timeInfo;
+    libs    ("libutilityFunctionObjects.so");
+    log     true;
+
+    // writeToFile     true;
+
+    perTimeStep     true;
+}
+
+
+// ************************************************************************* //
-- 
GitLab