From acaa18be6c2bbcd96c38772a9873784e39b30a3c Mon Sep 17 00:00:00 2001 From: Andrew Heather Date: Mon, 5 Oct 2015 15:10:23 +0100 Subject: [PATCH] ENH: Added new functionObjectState class This new class provides function objects with a database (dictionary) to store current state information to enable smooth restart behaviour. Additionally, current results can be stored so that they can be accessed between different objects. --- .../functionObjectState/functionObjectState.C | 173 ++++++++++++ .../functionObjectState/functionObjectState.H | 255 ++++++++++++++++++ .../functionObjectStateTemplates.C | 242 +++++++++++++++++ 3 files changed, 670 insertions(+) create mode 100644 src/OpenFOAM/db/functionObjects/functionObjectState/functionObjectState.C create mode 100644 src/OpenFOAM/db/functionObjects/functionObjectState/functionObjectState.H create mode 100644 src/OpenFOAM/db/functionObjects/functionObjectState/functionObjectStateTemplates.C diff --git a/src/OpenFOAM/db/functionObjects/functionObjectState/functionObjectState.C b/src/OpenFOAM/db/functionObjects/functionObjectState/functionObjectState.C new file mode 100644 index 0000000000..37d57d06f1 --- /dev/null +++ b/src/OpenFOAM/db/functionObjects/functionObjectState/functionObjectState.C @@ -0,0 +1,173 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2015 OpenFOAM Foundation + \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd +------------------------------------------------------------------------------- +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 . + +\*---------------------------------------------------------------------------*/ + +#include "functionObjectState.H" +#include "Time.H" + +const Foam::word Foam::functionObjectState::resultsName_ = "results"; + + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +Foam::functionObjectState::functionObjectState +( + const objectRegistry& obr, + const word& name +) +: + obr_(obr), + name_(name), + active_(true), + stateDict_ + ( + const_cast(obr.time().functionObjects().stateDict()) + ) +{} + + +// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // + +Foam::functionObjectState::~functionObjectState() +{} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +const Foam::word& Foam::functionObjectState::name() const +{ + return name_; +} + + +bool Foam::functionObjectState::active() const +{ + return active_; +} + + +const Foam::IOdictionary& Foam::functionObjectState::stateDict() const +{ + return stateDict_; +} + + +Foam::dictionary& Foam::functionObjectState::propertyDict() +{ + if (!stateDict_.found(name_)) + { + stateDict_.add(name_, dictionary()); + } + + return stateDict_.subDict(name_); +} + + +bool Foam::functionObjectState::foundProperty(const word& entryName) const +{ + if (stateDict_.found(name_)) + { + const dictionary& baseDict = stateDict_.subDict(name_); + return baseDict.found(entryName); + } + + return false; +} + + +Foam::word Foam::functionObjectState::resultType(const word& entryName) const +{ + return objectResultType(name_, entryName); +} + + +Foam::word Foam::functionObjectState::objectResultType +( + const word& objectName, + const word& entryName +) const +{ + word result = word::null; + + if (stateDict_.found(resultsName_)) + { + const dictionary& resultsDict = stateDict_.subDict(resultsName_); + + if (resultsDict.found(objectName)) + { + const dictionary& objectDict = resultsDict.subDict(objectName); + + forAllConstIter(dictionary, objectDict, iter) + { + const dictionary& dict = iter().dict(); + + if (dict.found(entryName)) + { + return dict.dictName(); + } + } + } + } + + return result; +} + + +Foam::List Foam::functionObjectState::objectResultEntries() const +{ + return objectResultEntries(name_); +} + + +Foam::List Foam::functionObjectState::objectResultEntries +( + const word& objectName +) const +{ + DynamicList result(2); + + if (stateDict_.found(resultsName_)) + { + const dictionary& resultsDict = stateDict_.subDict(resultsName_); + + if (resultsDict.found(objectName)) + { + const dictionary& objectDict = resultsDict.subDict(objectName); + + forAllConstIter(dictionary, objectDict, iter) + { + const dictionary& dict = iter().dict(); + result.append(dict.toc()); + } + } + } + + wordList entries; + entries.transfer(result); + + return entries; +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/db/functionObjects/functionObjectState/functionObjectState.H b/src/OpenFOAM/db/functionObjects/functionObjectState/functionObjectState.H new file mode 100644 index 0000000000..c3c8b94c7a --- /dev/null +++ b/src/OpenFOAM/db/functionObjects/functionObjectState/functionObjectState.H @@ -0,0 +1,255 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2015 OpenFOAM Foundation + \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd +------------------------------------------------------------------------------- +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 . + +Class + Foam::functionObjectState + +Description + Base class for function objects, adding functionality to read/write state + information (data required for smooth restart behaviour) and results + to/from the state dictionary + +See Also + Foam::functionObject + +SourceFiles + functionObjectState.C + +\*---------------------------------------------------------------------------*/ + +#ifndef functionObjectState_H +#define functionObjectState_H + +#include "objectRegistry.H" +#include "IOdictionary.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +/*---------------------------------------------------------------------------*\ + Class functionObjectState Declaration +\*---------------------------------------------------------------------------*/ + +class functionObjectState +{ +private: + + // Private data + + //- Name of the results dictionary + static const word resultsName_; + + //- Reference to the database + const objectRegistry& obr_; + + +protected: + + // Protected data + + //- Name of model + const word name_; + + //- Flag to indicate whether the object is active + bool active_; + + //- Reference to the state dictionary + IOdictionary& stateDict_; + + +protected: + + // Protected Member Functions + + //- Disallow default bitwise copy construct + functionObjectState(const functionObjectState&); + + //- Disallow default bitwise assignment + void operator=(const functionObjectState&); + + +public: + + // Constructors + + //- Construct from components + functionObjectState + ( + const objectRegistry& obr, + const word& name + ); + + + //- Destructor + virtual ~functionObjectState(); + + + // Member Functions + + //- Return the name + const word& name() const; + + //- Return the active flag + bool active() const; + + //- Return access to the state dictionary + const IOdictionary& stateDict() const; + + //- Return access to the property dictionary + dictionary& propertyDict(); + + //- Set the active status by querying objectRegistry type + // returns new active status + template + bool setActive(); + + + // Properties + + //- Return true if the property exists + bool foundProperty(const word& entryName) const; + + //- Retrieve generic property + template + Type getProperty + ( + const word& entryName, + const Type& defaultValue = pTraits::zero + ) const; + + //- Retrieve generic property + template + void getProperty(const word& entryName, Type& value) const; + + //- Add generic property + template + void setProperty(const word& entryName, const Type& value); + + //- Retrieve generic property from named object + template + Type getObjectProperty + ( + const word& objectName, + const word& entryName, + const Type& defaultValue = pTraits::zero + ) const; + + //- Retrieve generic property from named object + template + void getObjectProperty + ( + const word& objectName, + const word& entryName, + Type& value + ) const; + + //- Add generic property from named object + template + void setObjectProperty + ( + const word& objectName, + const word& entryName, + const Type& value + ); + + + // Results + + //- Add result + template + void setResult + ( + const word& entryName, + const Type& value + ); + + //- Add result from named object + template + void setObjectResult + ( + const word& objectName, + const word& entryName, + const Type& value + ); + + //- Retrieve result + template + Type getResult + ( + const word& entryName, + const Type& defaultValue = pTraits::zero + ) const; + + //- Retrieve result from named object + template + Type getObjectResult + ( + const word& objectName, + const word& entryName, + const Type& defaultValue = pTraits::zero + ) const; + + //- Retrieve result from named object + template + void getObjectResult + ( + const word& objectName, + const word& entryName, + Type& value + ) const; + + //- Retrieve the result type + word resultType(const word& entryName) const; + + //- Return the type of result + word objectResultType + ( + const word& objectName, + const word& entryName + ) const; + + //- Retrieve the result entries + List objectResultEntries() const; + + //- Return result entries for named object + List objectResultEntries(const word& objectName) const; +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#ifdef NoRepository + #include "functionObjectStateTemplates.C" +#endif + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/db/functionObjects/functionObjectState/functionObjectStateTemplates.C b/src/OpenFOAM/db/functionObjects/functionObjectState/functionObjectStateTemplates.C new file mode 100644 index 0000000000..fb27b2668c --- /dev/null +++ b/src/OpenFOAM/db/functionObjects/functionObjectState/functionObjectStateTemplates.C @@ -0,0 +1,242 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2015 OpenFOAM Foundation + \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd +------------------------------------------------------------------------------- +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 . + +\*---------------------------------------------------------------------------*/ + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +template +bool Foam::functionObjectState::setActive() +{ + active_ = true; + + if (!isA(obr_)) + { + WarningIn + ( + "void Foam::functionObjectState::setActive()" + ) << "No " << Type::typeName << " available, deactivating " << name_ + << endl; + + active_ = false; + } + + return active_; +} + + +template +Type Foam::functionObjectState::getProperty +( + const word& entryName, + const Type& defaultValue +) const +{ + Type result = defaultValue; + getProperty(entryName, result); + return result; +} + + +template +void Foam::functionObjectState::getProperty +( + const word& entryName, + Type& value +) const +{ + getObjectProperty(name_, entryName, value); +} + + +template +void Foam::functionObjectState::setProperty +( + const word& entryName, + const Type& value +) +{ + setObjectProperty(name_, entryName, value); +} + + +template +Type Foam::functionObjectState::getObjectProperty +( + const word& objectName, + const word& entryName, + const Type& defaultValue +) const +{ + Type result = defaultValue; + getObjectProperty(objectName, entryName, result); + return result; +} + + +template +void Foam::functionObjectState::getObjectProperty +( + const word& objectName, + const word& entryName, + Type& value +) const +{ + if (stateDict_.found(objectName)) + { + const dictionary& baseDict = stateDict_.subDict(objectName); + if (baseDict.found(entryName)) + { + if (baseDict.isDict(entryName)) + { + value = baseDict.subDict(entryName); + } + else + { + baseDict.lookup(entryName) >> value; + } + } + } +} + + +template +void Foam::functionObjectState::setObjectProperty +( + const word& objectName, + const word& entryName, + const Type& value +) +{ + if (!stateDict_.found(objectName)) + { + stateDict_.add(objectName, dictionary()); + } + + dictionary& baseDict = stateDict_.subDict(objectName); + baseDict.add(entryName, value, true); +} + + +template +void Foam::functionObjectState::setResult +( + const word& entryName, + const Type& value +) +{ + setObjectResult(name_, entryName, value); +} + + +template +void Foam::functionObjectState::setObjectResult +( + const word& objectName, + const word& entryName, + const Type& value +) +{ + if (!stateDict_.found(resultsName_)) + { + stateDict_.add(resultsName_, dictionary()); + } + + dictionary& resultsDict = stateDict_.subDict(resultsName_); + + if (!resultsDict.found(objectName)) + { + resultsDict.add(name_, dictionary()); + } + + dictionary& objectDict = resultsDict.subDict(objectName); + + const word& dictTypeName = pTraits::typeName; + + if (!objectDict.found(dictTypeName)) + { + objectDict.add(dictTypeName, dictionary()); + } + + dictionary& resultTypeDict = objectDict.subDict(dictTypeName); + + resultTypeDict.add(entryName, value, true); +} + + +template +Type Foam::functionObjectState::getResult +( + const word& entryName, + const Type& defaultValue +) const +{ + return getObjectResult(name_, entryName, defaultValue); +} + + +template +Type Foam::functionObjectState::getObjectResult +( + const word& objectName, + const word& entryName, + const Type& defaultValue +) const +{ + Type result = defaultValue; + getObjectResult(objectName, entryName, result); + return result; +} + + +template +void Foam::functionObjectState::getObjectResult +( + const word& objectName, + const word& entryName, + Type& value +) const +{ + if (stateDict_.found(resultsName_)) + { + const dictionary& resultsDict = stateDict_.subDict(resultsName_); + + if (resultsDict.found(objectName)) + { + const dictionary& objectDict = resultsDict.subDict(objectName); + + const word& dictTypeName = pTraits::typeName; + + if (objectDict.found(dictTypeName)) + { + const dictionary& resultTypeDict = + objectDict.subDict(dictTypeName); + + resultTypeDict.readIfPresent(entryName, value); + } + } + } +} + + +// ************************************************************************* //