From 32dbb01e96e46e7c942e04651ffe7a212bd3c99e Mon Sep 17 00:00:00 2001 From: Henry Weller Date: Wed, 11 May 2016 09:03:52 +0100 Subject: [PATCH] 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. --- src/OpenFOAM/db/Time/Time.C | 2 +- .../OutputFilterFunctionObject.C | 89 +++---------- .../OutputFilterFunctionObject.H | 63 ++------- .../OutputFilterFunctionObjectI.H | 76 +++++++++++ .../functionObject/functionObject.H | 5 +- .../functionObjectList/functionObjectList.C | 120 +++++++++--------- .../functionObjectList/functionObjectList.H | 31 +++-- .../setTimeStep/setTimeStepFunctionObject.C | 70 +++++----- .../setTimeStep/setTimeStepFunctionObject.H | 37 ++---- 9 files changed, 222 insertions(+), 271 deletions(-) create mode 100644 src/OpenFOAM/db/functionObjects/OutputFilterFunctionObject/OutputFilterFunctionObjectI.H diff --git a/src/OpenFOAM/db/Time/Time.C b/src/OpenFOAM/db/Time/Time.C index b0b147465b..8e58c9ddd0 100644 --- a/src/OpenFOAM/db/Time/Time.C +++ b/src/OpenFOAM/db/Time/Time.C @@ -699,7 +699,7 @@ Foam::Time::~Time() removeWatch(controlDict_.watchIndex()); } - // destroy function objects first + // Destroy function objects first functionObjects_.clear(); } diff --git a/src/OpenFOAM/db/functionObjects/OutputFilterFunctionObject/OutputFilterFunctionObject.C b/src/OpenFOAM/db/functionObjects/OutputFilterFunctionObject/OutputFilterFunctionObject.C index 7c81faefc1..73c6dd2927 100644 --- a/src/OpenFOAM/db/functionObjects/OutputFilterFunctionObject/OutputFilterFunctionObject.C +++ b/src/OpenFOAM/db/functionObjects/OutputFilterFunctionObject/OutputFilterFunctionObject.C @@ -36,8 +36,6 @@ void Foam::OutputFilterFunctionObject::readDict() { dict_.readIfPresent("region", regionName_); dict_.readIfPresent("dictionary", dictName_); - dict_.readIfPresent("enabled", enabled_); - dict_.readIfPresent("storeFilter", storeFilter_); dict_.readIfPresent("timeStart", timeStart_); dict_.readIfPresent("timeEnd", timeEnd_); dict_.readIfPresent("nStepsToStartTimeChange", nStepsToStartTimeChange_); @@ -48,8 +46,7 @@ template bool Foam::OutputFilterFunctionObject::active() const { return - enabled_ - && time_.value() >= timeStart_ + time_.value() >= timeStart_ && time_.value() <= timeEnd_; } @@ -79,10 +76,6 @@ bool Foam::OutputFilterFunctionObject::allocateFilter() ) ); } - else - { - enabled_ = false; - } } else { @@ -106,20 +99,9 @@ bool Foam::OutputFilterFunctionObject::allocateFilter() ) ); } - else - { - enabled_ = false; - } } - return enabled_; -} - - -template -void Foam::OutputFilterFunctionObject::destroyFilter() -{ - ptr_.reset(); + return ptr_.valid(); } @@ -138,8 +120,6 @@ Foam::OutputFilterFunctionObject::OutputFilterFunctionObject dict_(dict), regionName_(polyMesh::defaultRegion), dictName_(), - enabled_(true), - storeFilter_(true), timeStart_(-VGREAT), timeEnd_(VGREAT), nStepsToStartTimeChange_ @@ -148,40 +128,23 @@ Foam::OutputFilterFunctionObject::OutputFilterFunctionObject ), outputControl_(t, dict, "output"), evaluateControl_(t, dict, "evaluate") -{ - readDict(); -} +{} // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -template -void Foam::OutputFilterFunctionObject::on() -{ - enabled_ = true; -} - - -template -void Foam::OutputFilterFunctionObject::off() -{ - enabled_ = false; -} - - template bool Foam::OutputFilterFunctionObject::start() { 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::execute { if (active()) { - if (!storeFilter_ && !allocateFilter()) - { - return false; - } - if (evaluateControl_.output()) { ptr_->execute(); @@ -207,11 +165,6 @@ bool Foam::OutputFilterFunctionObject::execute { ptr_->write(); } - - if (!storeFilter_) - { - destroyFilter(); - } } return true; @@ -221,24 +174,11 @@ bool Foam::OutputFilterFunctionObject::execute template bool Foam::OutputFilterFunctionObject::end() { - if (enabled_) + ptr_->end(); + + if (outputControl_.output()) { - if (!storeFilter_ && !allocateFilter()) - { - return false; - } - - ptr_->end(); - - if (outputControl_.output()) - { - ptr_->write(); - } - - if (!storeFilter_) - { - destroyFilter(); - } + ptr_->write(); } return true; @@ -314,6 +254,7 @@ bool Foam::OutputFilterFunctionObject::read dict_ = dict; outputControl_.read(dict); + // Reset the OutputFilter return start(); } else diff --git a/src/OpenFOAM/db/functionObjects/OutputFilterFunctionObject/OutputFilterFunctionObject.H b/src/OpenFOAM/db/functionObjects/OutputFilterFunctionObject/OutputFilterFunctionObject.H index 5cd592ee83..4e1d6bbb2c 100644 --- a/src/OpenFOAM/db/functionObjects/OutputFilterFunctionObject/OutputFilterFunctionObject.H +++ b/src/OpenFOAM/db/functionObjects/OutputFilterFunctionObject/OutputFilterFunctionObject.H @@ -77,13 +77,6 @@ class OutputFilterFunctionObject //- Dictionary name to supply required inputs 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 scalar timeStart_; @@ -113,9 +106,6 @@ class OutputFilterFunctionObject //- Creates most of the data associated with this object. bool allocateFilter(); - //- Destroys most of the data associated with this object. - void destroyFilter(); - //- Returns true if active (enabled and within time bounds) bool active() const; @@ -148,62 +138,33 @@ public: // Access //- Return time database - virtual const Time& time() const - { - return time_; - } + inline const Time& time() const; //- Return the input dictionary - virtual const dictionary& dict() const - { - return dict_; - } + inline const dictionary& dict() const; //- Return the region name - virtual const word& regionName() const - { - return regionName_; - } + inline const word& regionName() const; //- Return the optional dictionary name - virtual const word& dictName() const - { - return dictName_; - } - - //- Return the enabled flag - virtual bool enabled() const - { - return enabled_; - } + inline const word& dictName() const; //- Return the output control object - virtual const outputFilterOutputControl& outputControl() const - { - return outputControl_; - } + inline const outputFilterOutputControl& outputControl() const; //- Return the output filter - virtual const OutputFilter& outputFilter() const - { - return ptr_(); - } + inline const OutputFilter& outputFilter() const; // 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 virtual bool start(); - //- Called at each ++ or += of the time-loop - virtual bool execute(const bool forceWrite); + //- Called at each ++ or += of the time-loop. forceWrite overrides + // 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 virtual bool end(); @@ -231,6 +192,10 @@ public: // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +#include "OutputFilterFunctionObjectI.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + #ifdef NoRepository #include "OutputFilterFunctionObject.C" #endif diff --git a/src/OpenFOAM/db/functionObjects/OutputFilterFunctionObject/OutputFilterFunctionObjectI.H b/src/OpenFOAM/db/functionObjects/OutputFilterFunctionObject/OutputFilterFunctionObjectI.H new file mode 100644 index 0000000000..6a23b5914e --- /dev/null +++ b/src/OpenFOAM/db/functionObjects/OutputFilterFunctionObject/OutputFilterFunctionObjectI.H @@ -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 . + +\*---------------------------------------------------------------------------*/ + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +template +inline const Foam::Time& +Foam::OutputFilterFunctionObject::time() const +{ + return time_; +} + + +template +inline const Foam::dictionary& +Foam::OutputFilterFunctionObject::dict() const +{ + return dict_; +} + + +template +inline const Foam::word& +Foam::OutputFilterFunctionObject::regionName() const +{ + return regionName_; +} + + +template +inline const Foam::word& +Foam::OutputFilterFunctionObject::dictName() const +{ + return dictName_; +} + + +template +inline const Foam::outputFilterOutputControl& +Foam::OutputFilterFunctionObject::outputControl() const +{ + return outputControl_; +} + + +template +inline const OutputFilter& +Foam::OutputFilterFunctionObject::outputFilter() const +{ + return ptr_(); +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/db/functionObjects/functionObject/functionObject.H b/src/OpenFOAM/db/functionObjects/functionObject/functionObject.H index 3f25a1d518..58f3b8ab1f 100644 --- a/src/OpenFOAM/db/functionObjects/functionObject/functionObject.H +++ b/src/OpenFOAM/db/functionObjects/functionObject/functionObject.H @@ -186,8 +186,9 @@ public: //- Called at the start of the time-loop virtual bool start() = 0; - //- Called at each ++ or += of the time-loop. forceWrite overrides the - // outputControl behaviour. + //- Called at each ++ or += of the time-loop. forceWrite overrides + // the usual outputControl behaviour and forces writing always + // (used in post-processing mode) virtual bool execute(const bool forceWrite) = 0; //- Called when Time::run() determines that the time-loop exits. diff --git a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C index eaaae35cc6..83f30b5912 100644 --- a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C +++ b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C @@ -45,7 +45,7 @@ Foam::functionObject* Foam::functionObjectList::remove { 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(); indices_.erase(fnd); } @@ -168,7 +168,7 @@ void Foam::functionObjectList::on() 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; } @@ -274,10 +274,10 @@ bool Foam::functionObjectList::read() bool ok = true; updated_ = execution_; - // avoid reading/initializing if execution is off + // Avoid reading/initializing if execution is off if (!execution_) { - return ok; + return true; } // Update existing and add new functionObjects @@ -296,31 +296,42 @@ bool Foam::functionObjectList::read() label nFunc = 0; - if (entryPtr->isDict()) + if (!entryPtr->isDict()) { - // a dictionary of functionObjects - const dictionary& functionDicts = entryPtr->dict(); + FatalIOErrorInFunction(parentDict_) + << "'functions' entry is not a dictionary" + << exit(FatalIOError); + } - newPtrs.setSize(functionDicts.size()); - newDigs.setSize(functionDicts.size()); + const dictionary& functionDicts = entryPtr->dict(); - forAllConstIter(dictionary, functionDicts, iter) + newPtrs.setSize(functionDicts.size()); + newDigs.setSize(functionDicts.size()); + + forAllConstIter(dictionary, functionDicts, iter) + { + const word& key = iter().keyword(); + + if (!iter().isDict()) { - // safety: - if (!iter().isDict()) - { - continue; - } - const word& key = iter().keyword(); - const dictionary& dict = iter().dict(); + IOWarningInFunction(parentDict_) + << "Entry " << key << " is not a dictionary" << endl; + continue; + } - newDigs[nFunc] = dict.digest(); + const dictionary& dict = iter().dict(); + bool enabled = dict.lookupOrDefault("enabled", true); - label oldIndex; - functionObject* objPtr = remove(key, oldIndex); - if (objPtr) + newDigs[nFunc] = dict.digest(); + + label oldIndex; + functionObject* objPtr = remove(key, oldIndex); + + if (objPtr) + { + if (enabled) { - // an existing functionObject, and dictionary changed + // Dictionary changed for an existing functionObject if (newDigs[nFunc] != digests_[oldIndex]) { ok = objPtr->read(dict) && ok; @@ -328,65 +339,48 @@ bool Foam::functionObjectList::read() } else { - // new functionObject - objPtr = functionObject::New(key, time_, dict).ptr(); - ok = objPtr->start() && ok; - } - - newPtrs.set(nFunc, objPtr); - newIndices.insert(key, nFunc); - nFunc++; - } - } - else - { - // a list of functionObjects - PtrList functionDicts(entryPtr->stream()); - - newPtrs.setSize(functionDicts.size()); - newDigs.setSize(functionDicts.size()); - - forAllIter(PtrList, functionDicts, iter) - { - // safety: - if (!iter().isDict()) - { + // Delete the disabled functionObject + delete objPtr; + objPtr = NULL; continue; } - const word& key = iter().keyword(); - const dictionary& dict = iter().dict(); + } + else if (enabled) + { + autoPtr foPtr; - newDigs[nFunc] = dict.digest(); - - label oldIndex; - functionObject* objPtr = remove(key, oldIndex); - if (objPtr) + FatalError.throwExceptions(); + FatalIOError.throwExceptions(); + try { - // an existing functionObject, and dictionary changed - if (newDigs[nFunc] != digests_[oldIndex]) - { - ok = objPtr->read(dict) && ok; - } + foPtr = functionObject::New(key, time_, dict); } - else + catch (...) + {} + FatalError.dontThrowExceptions(); + FatalIOError.dontThrowExceptions(); + + if (foPtr.valid()) { - // new functionObject - objPtr = functionObject::New(key, time_, dict).ptr(); + objPtr = foPtr.ptr(); ok = objPtr->start() && ok; } + } + // Insert active functionObjects into the list + if (objPtr) + { newPtrs.set(nFunc, objPtr); newIndices.insert(key, nFunc); nFunc++; } } - // safety: newPtrs.setSize(nFunc); newDigs.setSize(nFunc); - // updating the PtrList of functionObjects also deletes any existing, - // but unused functionObjects + // Updating the PtrList of functionObjects deletes any + // existing unused functionObjects PtrList::transfer(newPtrs); digests_.transfer(newDigs); indices_.transfer(newIndices); diff --git a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.H b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.H index f1ccbb34dd..41b43b4c5f 100644 --- a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.H +++ b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.H @@ -138,7 +138,7 @@ public: //- Destructor - virtual ~functionObjectList(); + ~functionObjectList(); // Member Functions @@ -153,46 +153,45 @@ public: using PtrList::operator[]; //- Clear the list of function objects - virtual void clear(); + void clear(); //- 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 - virtual void on(); + void on(); //- Switch the function objects off - virtual void off(); + void off(); //- 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 - virtual bool start(); + bool start(); //- Called at each ++ or += of the time-loop. forceWrite overrides // the usual outputControl behaviour and forces writing always - // (used in postprocessing mode) - virtual bool execute(const bool forceWrite = false); + // (used in post-processing mode) + bool execute(const bool forceWrite = false); //- 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++ - virtual bool timeSet(); + bool timeSet(); //- 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 - virtual bool read(); + bool read(); //- Update for changes of mesh - virtual void updateMesh(const mapPolyMesh& mpm); + void updateMesh(const mapPolyMesh& mpm); //- Update for changes of mesh - virtual void movePoints(const polyMesh& mesh); + void movePoints(const polyMesh& mesh); }; diff --git a/src/postProcessing/functionObjects/utilities/setTimeStep/setTimeStepFunctionObject.C b/src/postProcessing/functionObjects/utilities/setTimeStep/setTimeStepFunctionObject.C index 6ea307928d..52c1394cb4 100644 --- a/src/postProcessing/functionObjects/utilities/setTimeStep/setTimeStepFunctionObject.C +++ b/src/postProcessing/functionObjects/utilities/setTimeStep/setTimeStepFunctionObject.C @@ -54,24 +54,24 @@ Foam::functionObjects::setTimeStepFunctionObject::setTimeStepFunctionObject ) : functionObject(name), - time_(runTime), - enabled_(true) + time_(runTime) { read(dict); } +// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // + +Foam::functionObjects::setTimeStepFunctionObject::~setTimeStepFunctionObject() +{} + + // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -void Foam::functionObjects::setTimeStepFunctionObject::on() +const Foam::Time& +Foam::functionObjects::setTimeStepFunctionObject::time() const { - enabled_ = true; -} - - -void Foam::functionObjects::setTimeStepFunctionObject::off() -{ - enabled_ = false; + return time_; } @@ -104,19 +104,13 @@ bool Foam::functionObjects::setTimeStepFunctionObject::timeSet() bool Foam::functionObjects::setTimeStepFunctionObject::adjustTimeStep() { - if (enabled()) - { - // Wanted timestep - scalar newDeltaT = timeStepPtr_().value(time_.timeOutputValue()); + const_cast(time()).setDeltaT + ( + timeStepPtr_().value(time_.timeOutputValue()), + false + ); - const_cast(time()).setDeltaT(newDeltaT, false); - - return true; - } - else - { - return false; - } + return true; } @@ -125,27 +119,23 @@ bool Foam::functionObjects::setTimeStepFunctionObject::read const dictionary& dict ) { - enabled_ = dict.lookupOrDefault("enabled", true); + timeStepPtr_ = Function1::New("deltaT", dict); - if (enabled_) + // Check that adjustTimeStep is active + const dictionary& controlDict = time_.controlDict(); + + Switch adjust; + if + ( + !controlDict.readIfPresent("adjustTimeStep", adjust) + || !adjust + ) { - timeStepPtr_ = Function1::New("deltaT", dict); - - // Check that time has adjustTimeStep - const dictionary& controlDict = time_.controlDict(); - - Switch adjust; - if - ( - !controlDict.readIfPresent("adjustTimeStep", adjust) - || !adjust - ) - { - FatalIOErrorInFunction(dict) - << "Need to have 'adjustTimeStep' true to enable external" - << " timestep control" << exit(FatalIOError); - } + FatalIOErrorInFunction(dict) + << "Need to set 'adjustTimeStep' true to allow timestep control" + << exit(FatalIOError); } + return true; } diff --git a/src/postProcessing/functionObjects/utilities/setTimeStep/setTimeStepFunctionObject.H b/src/postProcessing/functionObjects/utilities/setTimeStep/setTimeStepFunctionObject.H index 021b5c8fa6..8be4b8580d 100644 --- a/src/postProcessing/functionObjects/utilities/setTimeStep/setTimeStepFunctionObject.H +++ b/src/postProcessing/functionObjects/utilities/setTimeStep/setTimeStepFunctionObject.H @@ -43,7 +43,6 @@ SourceFiles #define functionObjects_setTimeStepFunctionObject_H #include "functionObject.H" -#include "dictionary.H" #include "Function1.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -66,15 +65,11 @@ class setTimeStepFunctionObject //- Reference to the time database const Time& time_; + //- Time step function/table + autoPtr> timeStepPtr_; - // Optional user inputs - - //- Switch for the execution - defaults to 'yes/on' - bool enabled_; - - //- Time step - autoPtr> timeStepPtr_; + // Private member functions //- Disallow default bitwise copy construct setTimeStepFunctionObject(const setTimeStepFunctionObject&); @@ -84,9 +79,11 @@ class setTimeStepFunctionObject public: + //- Runtime type information TypeName("setTimeStep"); + // Constructors //- Construct from components @@ -98,32 +95,20 @@ public: ); + // Destructor + virtual ~setTimeStepFunctionObject(); + + // Member Functions // Access //- Return time database - virtual const Time& time() const - { - return time_; - } - - //- Return the enabled flag - virtual bool enabled() const - { - return enabled_; - } + const Time& time() const; // 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 virtual bool start(); @@ -147,9 +132,9 @@ public: //- Update for changes of mesh virtual void movePoints(const polyMesh& mesh); - }; + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace functionObjects