Commit acaa18be authored by Andrew Heather's avatar Andrew Heather
Browse files

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.
parent f5a9ae43
/*---------------------------------------------------------------------------*\
========= |
\\ / 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 <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#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<IOdictionary&>(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::word> Foam::functionObjectState::objectResultEntries() const
{
return objectResultEntries(name_);
}
Foam::List<Foam::word> Foam::functionObjectState::objectResultEntries
(
const word& objectName
) const
{
DynamicList<word> 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;
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / 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 <http://www.gnu.org/licenses/>.
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<class Type>
bool setActive();
// Properties
//- Return true if the property exists
bool foundProperty(const word& entryName) const;
//- Retrieve generic property
template<class Type>
Type getProperty
(
const word& entryName,
const Type& defaultValue = pTraits<Type>::zero
) const;
//- Retrieve generic property
template<class Type>
void getProperty(const word& entryName, Type& value) const;
//- Add generic property
template<class Type>
void setProperty(const word& entryName, const Type& value);
//- Retrieve generic property from named object
template<class Type>
Type getObjectProperty
(
const word& objectName,
const word& entryName,
const Type& defaultValue = pTraits<Type>::zero
) const;
//- Retrieve generic property from named object
template<class Type>
void getObjectProperty
(
const word& objectName,
const word& entryName,
Type& value
) const;
//- Add generic property from named object
template<class Type>
void setObjectProperty
(
const word& objectName,
const word& entryName,
const Type& value
);
// Results
//- Add result
template<class Type>
void setResult
(
const word& entryName,
const Type& value
);
//- Add result from named object
template<class Type>
void setObjectResult
(
const word& objectName,
const word& entryName,
const Type& value
);
//- Retrieve result
template<class Type>
Type getResult
(
const word& entryName,
const Type& defaultValue = pTraits<Type>::zero
) const;
//- Retrieve result from named object
template<class Type>
Type getObjectResult
(
const word& objectName,
const word& entryName,
const Type& defaultValue = pTraits<Type>::zero
) const;
//- Retrieve result from named object
template<class Type>
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<word> objectResultEntries() const;
//- Return result entries for named object
List<word> objectResultEntries(const word& objectName) const;
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository
#include "functionObjectStateTemplates.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / 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 <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<class Type>
bool Foam::functionObjectState::setActive()
{
active_ = true;
if (!isA<Type>(obr_))
{
WarningIn
(
"void Foam::functionObjectState::setActive()"
) << "No " << Type::typeName << " available, deactivating " << name_
<< endl;
active_ = false;
}
return active_;
}
template<class Type>
Type Foam::functionObjectState::getProperty
(
const word& entryName,
const Type& defaultValue
) const
{
Type result = defaultValue;
getProperty(entryName, result);
return result;
}
template<class Type>
void Foam::functionObjectState::getProperty
(
const word& entryName,
Type& value
) const
{
getObjectProperty(name_, entryName, value);
}
template<class Type>
void Foam::functionObjectState::setProperty
(
const word& entryName,
const Type& value
)
{
setObjectProperty(name_, entryName, value);
}
template<class Type>
Type Foam::functionObjectState::getObjectProperty
(
const word& objectName,
const word& entryName,
const Type& defaultValue
) const
{
Type result = defaultValue;
getObjectProperty(objectName, entryName, result);
return result;
}
template<class Type>
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<class Type>
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<class Type>
void Foam::functionObjectState::setResult
(
const word& entryName,
const Type& value
)
{
setObjectResult(name_, entryName, value);
}
template<class Type>
void Foam::functionObjectState::setObjectResult
(
const word& objectName,
const word& entryName,
const Type& value
)
{
if (!stateDict_.found(resultsName_))
{
stateDict_.add(resultsName_,<