functionObjectList: Rationalized and simplified the handling of disabled functionObjects

Simplified and generalized the handling of functionObjects which fail to
construct by removing them from the list rather than maintaining an
"enabled" switch in each functionObject.
This commit is contained in:
Henry Weller
2016-05-11 09:03:52 +01:00
parent 080908732d
commit 32dbb01e96
9 changed files with 222 additions and 271 deletions

View File

@ -699,7 +699,7 @@ Foam::Time::~Time()
removeWatch(controlDict_.watchIndex()); removeWatch(controlDict_.watchIndex());
} }
// destroy function objects first // Destroy function objects first
functionObjects_.clear(); functionObjects_.clear();
} }

View File

@ -36,8 +36,6 @@ void Foam::OutputFilterFunctionObject<OutputFilter>::readDict()
{ {
dict_.readIfPresent("region", regionName_); dict_.readIfPresent("region", regionName_);
dict_.readIfPresent("dictionary", dictName_); dict_.readIfPresent("dictionary", dictName_);
dict_.readIfPresent("enabled", enabled_);
dict_.readIfPresent("storeFilter", storeFilter_);
dict_.readIfPresent("timeStart", timeStart_); dict_.readIfPresent("timeStart", timeStart_);
dict_.readIfPresent("timeEnd", timeEnd_); dict_.readIfPresent("timeEnd", timeEnd_);
dict_.readIfPresent("nStepsToStartTimeChange", nStepsToStartTimeChange_); dict_.readIfPresent("nStepsToStartTimeChange", nStepsToStartTimeChange_);
@ -48,8 +46,7 @@ template<class OutputFilter>
bool Foam::OutputFilterFunctionObject<OutputFilter>::active() const bool Foam::OutputFilterFunctionObject<OutputFilter>::active() const
{ {
return return
enabled_ time_.value() >= timeStart_
&& time_.value() >= timeStart_
&& time_.value() <= timeEnd_; && time_.value() <= timeEnd_;
} }
@ -79,10 +76,6 @@ bool Foam::OutputFilterFunctionObject<OutputFilter>::allocateFilter()
) )
); );
} }
else
{
enabled_ = false;
}
} }
else else
{ {
@ -106,20 +99,9 @@ bool Foam::OutputFilterFunctionObject<OutputFilter>::allocateFilter()
) )
); );
} }
else
{
enabled_ = false;
}
} }
return enabled_; return ptr_.valid();
}
template<class OutputFilter>
void Foam::OutputFilterFunctionObject<OutputFilter>::destroyFilter()
{
ptr_.reset();
} }
@ -138,8 +120,6 @@ Foam::OutputFilterFunctionObject<OutputFilter>::OutputFilterFunctionObject
dict_(dict), dict_(dict),
regionName_(polyMesh::defaultRegion), regionName_(polyMesh::defaultRegion),
dictName_(), dictName_(),
enabled_(true),
storeFilter_(true),
timeStart_(-VGREAT), timeStart_(-VGREAT),
timeEnd_(VGREAT), timeEnd_(VGREAT),
nStepsToStartTimeChange_ nStepsToStartTimeChange_
@ -148,40 +128,23 @@ Foam::OutputFilterFunctionObject<OutputFilter>::OutputFilterFunctionObject
), ),
outputControl_(t, dict, "output"), outputControl_(t, dict, "output"),
evaluateControl_(t, dict, "evaluate") evaluateControl_(t, dict, "evaluate")
{ {}
readDict();
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class OutputFilter>
void Foam::OutputFilterFunctionObject<OutputFilter>::on()
{
enabled_ = true;
}
template<class OutputFilter>
void Foam::OutputFilterFunctionObject<OutputFilter>::off()
{
enabled_ = false;
}
template<class OutputFilter> template<class OutputFilter>
bool Foam::OutputFilterFunctionObject<OutputFilter>::start() bool Foam::OutputFilterFunctionObject<OutputFilter>::start()
{ {
readDict(); readDict();
if (!allocateFilter())
{
FatalErrorInFunction
<< "Cannot construct " << OutputFilter::typeName
<< exit(FatalError);
}
if (enabled_ && storeFilter_)
{
return allocateFilter();
}
else
{
return true; return true;
}
} }
@ -193,11 +156,6 @@ bool Foam::OutputFilterFunctionObject<OutputFilter>::execute
{ {
if (active()) if (active())
{ {
if (!storeFilter_ && !allocateFilter())
{
return false;
}
if (evaluateControl_.output()) if (evaluateControl_.output())
{ {
ptr_->execute(); ptr_->execute();
@ -207,11 +165,6 @@ bool Foam::OutputFilterFunctionObject<OutputFilter>::execute
{ {
ptr_->write(); ptr_->write();
} }
if (!storeFilter_)
{
destroyFilter();
}
} }
return true; return true;
@ -221,13 +174,6 @@ bool Foam::OutputFilterFunctionObject<OutputFilter>::execute
template<class OutputFilter> template<class OutputFilter>
bool Foam::OutputFilterFunctionObject<OutputFilter>::end() bool Foam::OutputFilterFunctionObject<OutputFilter>::end()
{ {
if (enabled_)
{
if (!storeFilter_ && !allocateFilter())
{
return false;
}
ptr_->end(); ptr_->end();
if (outputControl_.output()) if (outputControl_.output())
@ -235,12 +181,6 @@ bool Foam::OutputFilterFunctionObject<OutputFilter>::end()
ptr_->write(); ptr_->write();
} }
if (!storeFilter_)
{
destroyFilter();
}
}
return true; return true;
} }
@ -314,6 +254,7 @@ bool Foam::OutputFilterFunctionObject<OutputFilter>::read
dict_ = dict; dict_ = dict;
outputControl_.read(dict); outputControl_.read(dict);
// Reset the OutputFilter
return start(); return start();
} }
else else

View File

@ -77,13 +77,6 @@ class OutputFilterFunctionObject
//- Dictionary name to supply required inputs //- Dictionary name to supply required inputs
word dictName_; word dictName_;
//- Switch for the execution - defaults to 'yes/on'
bool enabled_;
//- Switch to store filter in between writes or use on-the-fly
// construction - defaults to true
bool storeFilter_;
//- Activation time - defaults to -VGREAT //- Activation time - defaults to -VGREAT
scalar timeStart_; scalar timeStart_;
@ -113,9 +106,6 @@ class OutputFilterFunctionObject
//- Creates most of the data associated with this object. //- Creates most of the data associated with this object.
bool allocateFilter(); bool allocateFilter();
//- Destroys most of the data associated with this object.
void destroyFilter();
//- Returns true if active (enabled and within time bounds) //- Returns true if active (enabled and within time bounds)
bool active() const; bool active() const;
@ -148,62 +138,33 @@ public:
// Access // Access
//- Return time database //- Return time database
virtual const Time& time() const inline const Time& time() const;
{
return time_;
}
//- Return the input dictionary //- Return the input dictionary
virtual const dictionary& dict() const inline const dictionary& dict() const;
{
return dict_;
}
//- Return the region name //- Return the region name
virtual const word& regionName() const inline const word& regionName() const;
{
return regionName_;
}
//- Return the optional dictionary name //- Return the optional dictionary name
virtual const word& dictName() const inline const word& dictName() const;
{
return dictName_;
}
//- Return the enabled flag
virtual bool enabled() const
{
return enabled_;
}
//- Return the output control object //- Return the output control object
virtual const outputFilterOutputControl& outputControl() const inline const outputFilterOutputControl& outputControl() const;
{
return outputControl_;
}
//- Return the output filter //- Return the output filter
virtual const OutputFilter& outputFilter() const inline const OutputFilter& outputFilter() const;
{
return ptr_();
}
// Function object control // Function object control
//- Switch the function object on
virtual void on();
//- Switch the function object off
virtual void off();
//- Called at the start of the time-loop //- Called at the start of the time-loop
virtual bool start(); virtual bool start();
//- Called at each ++ or += of the time-loop //- Called at each ++ or += of the time-loop. forceWrite overrides
virtual bool execute(const bool forceWrite); // the usual outputControl behaviour and forces writing always
// (used in post-processing mode)
virtual bool execute(const bool forceWrite = false);
//- Called when Time::run() determines that the time-loop exits //- Called when Time::run() determines that the time-loop exits
virtual bool end(); virtual bool end();
@ -231,6 +192,10 @@ public:
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "OutputFilterFunctionObjectI.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository #ifdef NoRepository
#include "OutputFilterFunctionObject.C" #include "OutputFilterFunctionObject.C"
#endif #endif

View File

@ -0,0 +1,76 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2016 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/>.
\*---------------------------------------------------------------------------*/
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class OutputFilter>
inline const Foam::Time&
Foam::OutputFilterFunctionObject<OutputFilter>::time() const
{
return time_;
}
template<class OutputFilter>
inline const Foam::dictionary&
Foam::OutputFilterFunctionObject<OutputFilter>::dict() const
{
return dict_;
}
template<class OutputFilter>
inline const Foam::word&
Foam::OutputFilterFunctionObject<OutputFilter>::regionName() const
{
return regionName_;
}
template<class OutputFilter>
inline const Foam::word&
Foam::OutputFilterFunctionObject<OutputFilter>::dictName() const
{
return dictName_;
}
template<class OutputFilter>
inline const Foam::outputFilterOutputControl&
Foam::OutputFilterFunctionObject<OutputFilter>::outputControl() const
{
return outputControl_;
}
template<class OutputFilter>
inline const OutputFilter&
Foam::OutputFilterFunctionObject<OutputFilter>::outputFilter() const
{
return ptr_();
}
// ************************************************************************* //

View File

@ -186,8 +186,9 @@ public:
//- Called at the start of the time-loop //- Called at the start of the time-loop
virtual bool start() = 0; virtual bool start() = 0;
//- Called at each ++ or += of the time-loop. forceWrite overrides the //- Called at each ++ or += of the time-loop. forceWrite overrides
// outputControl behaviour. // the usual outputControl behaviour and forces writing always
// (used in post-processing mode)
virtual bool execute(const bool forceWrite) = 0; virtual bool execute(const bool forceWrite) = 0;
//- Called when Time::run() determines that the time-loop exits. //- Called when Time::run() determines that the time-loop exits.

View File

@ -45,7 +45,7 @@ Foam::functionObject* Foam::functionObjectList::remove
{ {
oldIndex = fnd(); oldIndex = fnd();
// retrieve the pointer and remove it from the old list // Retrieve the pointer and remove it from the old list
ptr = this->set(oldIndex, 0).ptr(); ptr = this->set(oldIndex, 0).ptr();
indices_.erase(fnd); indices_.erase(fnd);
} }
@ -168,7 +168,7 @@ void Foam::functionObjectList::on()
void Foam::functionObjectList::off() void Foam::functionObjectList::off()
{ {
// for safety, also force a read() when execution is turned back on // For safety, also force a read() when execution is turned back on
updated_ = execution_ = false; updated_ = execution_ = false;
} }
@ -274,10 +274,10 @@ bool Foam::functionObjectList::read()
bool ok = true; bool ok = true;
updated_ = execution_; updated_ = execution_;
// avoid reading/initializing if execution is off // Avoid reading/initializing if execution is off
if (!execution_) if (!execution_)
{ {
return ok; return true;
} }
// Update existing and add new functionObjects // Update existing and add new functionObjects
@ -296,9 +296,13 @@ bool Foam::functionObjectList::read()
label nFunc = 0; label nFunc = 0;
if (entryPtr->isDict()) if (!entryPtr->isDict())
{ {
// a dictionary of functionObjects FatalIOErrorInFunction(parentDict_)
<< "'functions' entry is not a dictionary"
<< exit(FatalIOError);
}
const dictionary& functionDicts = entryPtr->dict(); const dictionary& functionDicts = entryPtr->dict();
newPtrs.setSize(functionDicts.size()); newPtrs.setSize(functionDicts.size());
@ -306,21 +310,28 @@ bool Foam::functionObjectList::read()
forAllConstIter(dictionary, functionDicts, iter) forAllConstIter(dictionary, functionDicts, iter)
{ {
// safety: const word& key = iter().keyword();
if (!iter().isDict()) if (!iter().isDict())
{ {
IOWarningInFunction(parentDict_)
<< "Entry " << key << " is not a dictionary" << endl;
continue; continue;
} }
const word& key = iter().keyword();
const dictionary& dict = iter().dict(); const dictionary& dict = iter().dict();
bool enabled = dict.lookupOrDefault("enabled", true);
newDigs[nFunc] = dict.digest(); newDigs[nFunc] = dict.digest();
label oldIndex; label oldIndex;
functionObject* objPtr = remove(key, oldIndex); functionObject* objPtr = remove(key, oldIndex);
if (objPtr) if (objPtr)
{ {
// an existing functionObject, and dictionary changed if (enabled)
{
// Dictionary changed for an existing functionObject
if (newDigs[nFunc] != digests_[oldIndex]) if (newDigs[nFunc] != digests_[oldIndex])
{ {
ok = objPtr->read(dict) && ok; ok = objPtr->read(dict) && ok;
@ -328,65 +339,48 @@ bool Foam::functionObjectList::read()
} }
else else
{ {
// new functionObject // Delete the disabled functionObject
objPtr = functionObject::New(key, time_, dict).ptr(); delete objPtr;
ok = objPtr->start() && ok; objPtr = NULL;
}
newPtrs.set(nFunc, objPtr);
newIndices.insert(key, nFunc);
nFunc++;
}
}
else
{
// a list of functionObjects
PtrList<entry> functionDicts(entryPtr->stream());
newPtrs.setSize(functionDicts.size());
newDigs.setSize(functionDicts.size());
forAllIter(PtrList<entry>, functionDicts, iter)
{
// safety:
if (!iter().isDict())
{
continue; continue;
} }
const word& key = iter().keyword();
const dictionary& dict = iter().dict();
newDigs[nFunc] = dict.digest();
label oldIndex;
functionObject* objPtr = remove(key, oldIndex);
if (objPtr)
{
// an existing functionObject, and dictionary changed
if (newDigs[nFunc] != digests_[oldIndex])
{
ok = objPtr->read(dict) && ok;
} }
} else if (enabled)
else
{ {
// new functionObject autoPtr<functionObject> foPtr;
objPtr = functionObject::New(key, time_, dict).ptr();
FatalError.throwExceptions();
FatalIOError.throwExceptions();
try
{
foPtr = functionObject::New(key, time_, dict);
}
catch (...)
{}
FatalError.dontThrowExceptions();
FatalIOError.dontThrowExceptions();
if (foPtr.valid())
{
objPtr = foPtr.ptr();
ok = objPtr->start() && ok; ok = objPtr->start() && ok;
} }
}
// Insert active functionObjects into the list
if (objPtr)
{
newPtrs.set(nFunc, objPtr); newPtrs.set(nFunc, objPtr);
newIndices.insert(key, nFunc); newIndices.insert(key, nFunc);
nFunc++; nFunc++;
} }
} }
// safety:
newPtrs.setSize(nFunc); newPtrs.setSize(nFunc);
newDigs.setSize(nFunc); newDigs.setSize(nFunc);
// updating the PtrList of functionObjects also deletes any existing, // Updating the PtrList of functionObjects deletes any
// but unused functionObjects // existing unused functionObjects
PtrList<functionObject>::transfer(newPtrs); PtrList<functionObject>::transfer(newPtrs);
digests_.transfer(newDigs); digests_.transfer(newDigs);
indices_.transfer(newIndices); indices_.transfer(newIndices);

View File

@ -138,7 +138,7 @@ public:
//- Destructor //- Destructor
virtual ~functionObjectList(); ~functionObjectList();
// Member Functions // Member Functions
@ -153,46 +153,45 @@ public:
using PtrList<functionObject>::operator[]; using PtrList<functionObject>::operator[];
//- Clear the list of function objects //- Clear the list of function objects
virtual void clear(); void clear();
//- Find the ID of a given function object by name //- Find the ID of a given function object by name
virtual label findObjectID(const word& name) const; label findObjectID(const word& name) const;
//- Switch the function objects on //- Switch the function objects on
virtual void on(); void on();
//- Switch the function objects off //- Switch the function objects off
virtual void off(); void off();
//- Return the execution status (on/off) of the function objects //- Return the execution status (on/off) of the function objects
virtual bool status() const; bool status() const;
//- Called at the start of the time-loop //- Called at the start of the time-loop
virtual bool start(); bool start();
//- Called at each ++ or += of the time-loop. forceWrite overrides //- Called at each ++ or += of the time-loop. forceWrite overrides
// the usual outputControl behaviour and forces writing always // the usual outputControl behaviour and forces writing always
// (used in postprocessing mode) // (used in post-processing mode)
virtual bool execute(const bool forceWrite = false); bool execute(const bool forceWrite = false);
//- Called when Time::run() determines that the time-loop exits //- Called when Time::run() determines that the time-loop exits
virtual bool end(); bool end();
//- Called when time was set at the end of the Time::operator++ //- Called when time was set at the end of the Time::operator++
virtual bool timeSet(); bool timeSet();
//- Called at the end of Time::adjustDeltaT() if adjustTime is true //- Called at the end of Time::adjustDeltaT() if adjustTime is true
virtual bool adjustTimeStep(); bool adjustTimeStep();
//- Read and set the function objects if their data have changed //- Read and set the function objects if their data have changed
virtual bool read(); bool read();
//- Update for changes of mesh //- Update for changes of mesh
virtual void updateMesh(const mapPolyMesh& mpm); void updateMesh(const mapPolyMesh& mpm);
//- Update for changes of mesh //- Update for changes of mesh
virtual void movePoints(const polyMesh& mesh); void movePoints(const polyMesh& mesh);
}; };

View File

@ -54,24 +54,24 @@ Foam::functionObjects::setTimeStepFunctionObject::setTimeStepFunctionObject
) )
: :
functionObject(name), functionObject(name),
time_(runTime), time_(runTime)
enabled_(true)
{ {
read(dict); read(dict);
} }
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::functionObjects::setTimeStepFunctionObject::~setTimeStepFunctionObject()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void Foam::functionObjects::setTimeStepFunctionObject::on() const Foam::Time&
Foam::functionObjects::setTimeStepFunctionObject::time() const
{ {
enabled_ = true; return time_;
}
void Foam::functionObjects::setTimeStepFunctionObject::off()
{
enabled_ = false;
} }
@ -104,19 +104,13 @@ bool Foam::functionObjects::setTimeStepFunctionObject::timeSet()
bool Foam::functionObjects::setTimeStepFunctionObject::adjustTimeStep() bool Foam::functionObjects::setTimeStepFunctionObject::adjustTimeStep()
{ {
if (enabled()) const_cast<Time&>(time()).setDeltaT
{ (
// Wanted timestep timeStepPtr_().value(time_.timeOutputValue()),
scalar newDeltaT = timeStepPtr_().value(time_.timeOutputValue()); false
);
const_cast<Time&>(time()).setDeltaT(newDeltaT, false);
return true; return true;
}
else
{
return false;
}
} }
@ -125,13 +119,9 @@ bool Foam::functionObjects::setTimeStepFunctionObject::read
const dictionary& dict const dictionary& dict
) )
{ {
enabled_ = dict.lookupOrDefault("enabled", true);
if (enabled_)
{
timeStepPtr_ = Function1<scalar>::New("deltaT", dict); timeStepPtr_ = Function1<scalar>::New("deltaT", dict);
// Check that time has adjustTimeStep // Check that adjustTimeStep is active
const dictionary& controlDict = time_.controlDict(); const dictionary& controlDict = time_.controlDict();
Switch adjust; Switch adjust;
@ -142,10 +132,10 @@ bool Foam::functionObjects::setTimeStepFunctionObject::read
) )
{ {
FatalIOErrorInFunction(dict) FatalIOErrorInFunction(dict)
<< "Need to have 'adjustTimeStep' true to enable external" << "Need to set 'adjustTimeStep' true to allow timestep control"
<< " timestep control" << exit(FatalIOError); << exit(FatalIOError);
}
} }
return true; return true;
} }

View File

@ -43,7 +43,6 @@ SourceFiles
#define functionObjects_setTimeStepFunctionObject_H #define functionObjects_setTimeStepFunctionObject_H
#include "functionObject.H" #include "functionObject.H"
#include "dictionary.H"
#include "Function1.H" #include "Function1.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -66,16 +65,12 @@ class setTimeStepFunctionObject
//- Reference to the time database //- Reference to the time database
const Time& time_; const Time& time_;
//- Time step function/table
// Optional user inputs
//- Switch for the execution - defaults to 'yes/on'
bool enabled_;
//- Time step
autoPtr<Function1<scalar>> timeStepPtr_; autoPtr<Function1<scalar>> timeStepPtr_;
// Private member functions
//- Disallow default bitwise copy construct //- Disallow default bitwise copy construct
setTimeStepFunctionObject(const setTimeStepFunctionObject&); setTimeStepFunctionObject(const setTimeStepFunctionObject&);
@ -84,9 +79,11 @@ class setTimeStepFunctionObject
public: public:
//- Runtime type information //- Runtime type information
TypeName("setTimeStep"); TypeName("setTimeStep");
// Constructors // Constructors
//- Construct from components //- Construct from components
@ -98,32 +95,20 @@ public:
); );
// Destructor
virtual ~setTimeStepFunctionObject();
// Member Functions // Member Functions
// Access // Access
//- Return time database //- Return time database
virtual const Time& time() const const Time& time() const;
{
return time_;
}
//- Return the enabled flag
virtual bool enabled() const
{
return enabled_;
}
// Function object control // Function object control
//- Switch the function object on
virtual void on();
//- Switch the function object off
virtual void off();
//- Called at the start of the time-loop //- Called at the start of the time-loop
virtual bool start(); virtual bool start();
@ -147,9 +132,9 @@ public:
//- Update for changes of mesh //- Update for changes of mesh
virtual void movePoints(const polyMesh& mesh); virtual void movePoints(const polyMesh& mesh);
}; };
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace functionObjects } // End namespace functionObjects