From ac8af99bb7001a83b375b04463d6a2bccda541e2 Mon Sep 17 00:00:00 2001
From: andy <andy>
Date: Tue, 25 Sep 2012 10:44:27 +0100
Subject: [PATCH] ENH: Added new writeDictionary function object

---
 .../functionObjects/IO/Make/files             |   3 +
 .../IO/writeDictionary/IOwriteDictionary.H    |  49 +++++
 .../IO/writeDictionary/writeDictionary.C      | 199 ++++++++++++++++++
 .../IO/writeDictionary/writeDictionary.H      | 155 ++++++++++++++
 .../writeDictionaryFunctionObject.C           |  42 ++++
 .../writeDictionaryFunctionObject.H           |  54 +++++
 6 files changed, 502 insertions(+)
 create mode 100644 src/postProcessing/functionObjects/IO/writeDictionary/IOwriteDictionary.H
 create mode 100644 src/postProcessing/functionObjects/IO/writeDictionary/writeDictionary.C
 create mode 100644 src/postProcessing/functionObjects/IO/writeDictionary/writeDictionary.H
 create mode 100644 src/postProcessing/functionObjects/IO/writeDictionary/writeDictionaryFunctionObject.C
 create mode 100644 src/postProcessing/functionObjects/IO/writeDictionary/writeDictionaryFunctionObject.H

diff --git a/src/postProcessing/functionObjects/IO/Make/files b/src/postProcessing/functionObjects/IO/Make/files
index 8aeea600e4f..95065c4b3f8 100644
--- a/src/postProcessing/functionObjects/IO/Make/files
+++ b/src/postProcessing/functionObjects/IO/Make/files
@@ -4,6 +4,9 @@ partialWrite/partialWriteFunctionObject.C
 removeRegisteredObject/removeRegisteredObject.C
 removeRegisteredObject/removeRegisteredObjectFunctionObject.C
 
+writeDictionary/writeDictionary.C
+writeDictionary/writeDictionaryFunctionObject.C
+
 writeRegisteredObject/writeRegisteredObject.C
 writeRegisteredObject/writeRegisteredObjectFunctionObject.C
 
diff --git a/src/postProcessing/functionObjects/IO/writeDictionary/IOwriteDictionary.H b/src/postProcessing/functionObjects/IO/writeDictionary/IOwriteDictionary.H
new file mode 100644
index 00000000000..87718cd6dcb
--- /dev/null
+++ b/src/postProcessing/functionObjects/IO/writeDictionary/IOwriteDictionary.H
@@ -0,0 +1,49 @@
+/*---------------------------------------------------------------------------*\
+  =========                 |
+  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
+   \\    /   O peration     |
+    \\  /    A nd           | Copyright (C) 2012 OpenFOAM Foundation
+     \\/     M anipulation  |
+-------------------------------------------------------------------------------
+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/>.
+
+Typedef
+    Foam::IOwriteDictionary
+
+Description
+    Instance of the generic IOOutputFilter for writeDictionary.
+
+\*---------------------------------------------------------------------------*/
+
+#ifndef IOwriteDictionary_H
+#define IOwriteDictionary_H
+
+#include "writeDictionary.H"
+#include "IOOutputFilter.H"
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+namespace Foam
+{
+    typedef IOOutputFilter<writeDictionary> IOwriteDictionary;
+}
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+#endif
+
+// ************************************************************************* //
diff --git a/src/postProcessing/functionObjects/IO/writeDictionary/writeDictionary.C b/src/postProcessing/functionObjects/IO/writeDictionary/writeDictionary.C
new file mode 100644
index 00000000000..f10a84eece7
--- /dev/null
+++ b/src/postProcessing/functionObjects/IO/writeDictionary/writeDictionary.C
@@ -0,0 +1,199 @@
+/*---------------------------------------------------------------------------*\
+  =========                 |
+  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
+   \\    /   O peration     |
+    \\  /    A nd           | Copyright (C) 2012 OpenFOAM Foundation
+     \\/     M anipulation  |
+-------------------------------------------------------------------------------
+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 "writeDictionary.H"
+#include "dictionary.H"
+#include "Time.H"
+
+// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
+
+defineTypeNameAndDebug(Foam::writeDictionary, 0);
+
+// * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
+
+bool Foam::writeDictionary::tryFolder
+(
+    const label dictI,
+    const word& location,
+    bool& firstDict
+)
+{
+    IOobject dictIO
+    (
+        dictNames_[dictI],
+        location,
+        obr_,
+        IOobject::MUST_READ
+    );
+
+    if (dictIO.headerOk())
+    {
+        IOdictionary dict(dictIO);
+
+        if (dict.digest() != digests_[dictI])
+        {
+            if (firstDict)
+            {
+                Info<< type() << " output:" << nl << endl;
+
+                IOobject::writeDivider(Info);
+                Info<< endl;
+                firstDict = false;
+            }
+
+            Info<< dict.dictName() << dict << nl;
+
+            IOobject::writeDivider(Info);
+
+            digests_[dictI] = dict.digest();
+        }
+
+        return true;
+    }
+
+    return false;
+}
+
+
+// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
+
+Foam::writeDictionary::writeDictionary
+(
+    const word& name,
+    const objectRegistry& obr,
+    const dictionary& dict,
+    const bool loadFromFiles
+)
+:
+    name_(name),
+    obr_(obr),
+    dictNames_(),
+    digests_()
+{
+    read(dict);
+
+    execute();
+}
+
+
+// * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
+
+Foam::writeDictionary::~writeDictionary()
+{}
+
+
+// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
+
+void Foam::writeDictionary::read(const dictionary& dict)
+{
+    dict.lookup("dictNames") >> dictNames_;
+
+    digests_.setSize(dictNames_.size(), SHA1Digest());
+
+    Info<< type() << ": monitoring dictionaries:" << nl;
+    if (dictNames_.size())
+    {
+        forAll(dictNames_, i)
+        {
+            Info<< "    " << dictNames_[i] << endl;
+        }
+    }
+    else
+    {
+        Info<< "    none" << nl;
+    }
+    Info<< endl;
+}
+
+
+void Foam::writeDictionary::execute()
+{
+    bool firstDict = true;
+    forAll(dictNames_, i)
+    {
+        if (obr_.foundObject<dictionary>(dictNames_[i]))
+        {
+            const dictionary& dict =
+                obr_.lookupObject<dictionary>(dictNames_[i]);
+
+            if (dict.digest() != digests_[i])
+            {
+                if (firstDict)
+                {
+                    Info<< type() << " output:" << nl << endl;
+
+                    IOobject::writeDivider(Info);
+                    Info<< endl;
+                    firstDict = false;
+                }
+
+                digests_[i] = dict.digest();
+
+                Info<< dict.dictName() << dict << nl;
+                IOobject::writeDivider(Info);
+                Info<< endl;
+            }
+        }
+        else
+        {
+            bool processed = tryFolder(i, obr_.time().timeName(), firstDict);
+
+            if (!processed)
+            {
+                processed = tryFolder(i, obr_.time().constant(), firstDict);
+            }
+
+            if (!processed)
+            {
+                processed = tryFolder(i, obr_.time().system(), firstDict);
+            }
+
+            if (!processed)
+            {
+                Info<< "    Unable to locate dictionary " << dictNames_[i]
+                    << nl << endl;
+            }
+            else
+            {
+                Info<< endl;
+            }
+        }
+    }
+}
+
+
+void Foam::writeDictionary::end()
+{
+    // do nothing
+}
+
+
+void Foam::writeDictionary::write()
+{
+    // do nothing
+}
+
+
+// ************************************************************************* //
diff --git a/src/postProcessing/functionObjects/IO/writeDictionary/writeDictionary.H b/src/postProcessing/functionObjects/IO/writeDictionary/writeDictionary.H
new file mode 100644
index 00000000000..fb7a2952fd1
--- /dev/null
+++ b/src/postProcessing/functionObjects/IO/writeDictionary/writeDictionary.H
@@ -0,0 +1,155 @@
+/*---------------------------------------------------------------------------*\
+  =========                 |
+  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
+   \\    /   O peration     |
+    \\  /    A nd           | Copyright (C) 2012 OpenFOAM Foundation
+     \\/     M anipulation  |
+-------------------------------------------------------------------------------
+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::writeDictionary
+
+Description
+    This function object writes dictionaries on start-up, and on change
+
+SourceFiles
+    writeDictionary.C
+    IOwriteDictionary.H
+
+\*---------------------------------------------------------------------------*/
+
+#ifndef writeDictionary_H
+#define writeDictionary_H
+
+#include "pointFieldFwd.H"
+#include "wordList.H"
+#include "runTimeSelectionTables.H"
+#include "SHA1Digest.H"
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+namespace Foam
+{
+
+// Forward declaration of classes
+class objectRegistry;
+class dictionary;
+class mapPolyMesh;
+
+/*---------------------------------------------------------------------------*\
+                       Class writeDictionary Declaration
+\*---------------------------------------------------------------------------*/
+
+class writeDictionary
+{
+protected:
+
+    // Private data
+
+        //- Name of this set of writeDictionary
+        word name_;
+
+        //- Reference to the database
+        const objectRegistry& obr_;
+
+        //- Names of dictionaries to monitor
+        wordList dictNames_;
+
+        //- List of changed dictionaries (only those registered to database)
+        List<SHA1Digest> digests_;
+
+
+    // Private Member Functions
+
+        //- Helper function to write the dictionary if found at location
+        bool tryFolder
+        (
+            const label dictI,
+            const word& location,
+            bool& firstDict
+        );
+
+        //- Disallow default bitwise copy construct
+        writeDictionary(const writeDictionary&);
+
+        //- Disallow default bitwise assignment
+        void operator=(const writeDictionary&);
+
+
+public:
+
+    //- Runtime type information
+    TypeName("writeDictionary");
+
+
+    // Constructors
+
+        //- Construct for given objectRegistry and dictionary.
+        //  Allow the possibility to load fields from files
+        writeDictionary
+        (
+            const word& name,
+            const objectRegistry&,
+            const dictionary&,
+            const bool loadFromFiles = false
+        );
+
+
+    //- Destructor
+    virtual ~writeDictionary();
+
+
+    // Member Functions
+
+        //- Return name of the writeDictionary
+        virtual const word& name() const
+        {
+            return name_;
+        }
+
+        //- Read the writeDictionary data
+        virtual void read(const dictionary&);
+
+        //- Execute, currently does nothing
+        virtual void execute();
+
+        //- Execute at the final time-loop, currently does nothing
+        virtual void end();
+
+        //- Write the writeDictionary
+        virtual void write();
+
+        //- Update for changes of mesh
+        virtual void updateMesh(const mapPolyMesh&)
+        {}
+
+        //- Update for changes of mesh
+        virtual void movePoints(const pointField&)
+        {}
+};
+
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+} // End namespace Foam
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+#endif
+
+// ************************************************************************* //
diff --git a/src/postProcessing/functionObjects/IO/writeDictionary/writeDictionaryFunctionObject.C b/src/postProcessing/functionObjects/IO/writeDictionary/writeDictionaryFunctionObject.C
new file mode 100644
index 00000000000..712e7d9b397
--- /dev/null
+++ b/src/postProcessing/functionObjects/IO/writeDictionary/writeDictionaryFunctionObject.C
@@ -0,0 +1,42 @@
+/*---------------------------------------------------------------------------*\
+  =========                 |
+  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
+   \\    /   O peration     |
+    \\  /    A nd           | Copyright (C) 2012 OpenFOAM Foundation
+     \\/     M anipulation  |
+-------------------------------------------------------------------------------
+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 "writeDictionaryFunctionObject.H"
+
+// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
+
+namespace Foam
+{
+    defineNamedTemplateTypeNameAndDebug(writeDictionaryFunctionObject, 0);
+
+    addToRunTimeSelectionTable
+    (
+        functionObject,
+        writeDictionaryFunctionObject,
+        dictionary
+    );
+}
+
+// ************************************************************************* //
diff --git a/src/postProcessing/functionObjects/IO/writeDictionary/writeDictionaryFunctionObject.H b/src/postProcessing/functionObjects/IO/writeDictionary/writeDictionaryFunctionObject.H
new file mode 100644
index 00000000000..2f53fd5d2e9
--- /dev/null
+++ b/src/postProcessing/functionObjects/IO/writeDictionary/writeDictionaryFunctionObject.H
@@ -0,0 +1,54 @@
+/*---------------------------------------------------------------------------*\
+  =========                 |
+  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
+   \\    /   O peration     |
+    \\  /    A nd           | Copyright (C) 2012 OpenFOAM Foundation
+     \\/     M anipulation  |
+-------------------------------------------------------------------------------
+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/>.
+
+Typedef
+    Foam::writeDictionaryFunctionObject
+
+Description
+    FunctionObject wrapper around writeDictionary to allow them to be
+    created via the functions entry within controlDict.
+
+SourceFiles
+    writeDictionaryFunctionObject.C
+
+\*---------------------------------------------------------------------------*/
+
+#ifndef writeDictionaryFunctionObject_H
+#define writeDictionaryFunctionObject_H
+
+#include "writeDictionary.H"
+#include "OutputFilterFunctionObject.H"
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+namespace Foam
+{
+    typedef OutputFilterFunctionObject<writeDictionary>
+        writeDictionaryFunctionObject;
+}
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
+#endif
+
+// ************************************************************************* //
-- 
GitLab