From 2ebed5ec7123ff8750931a65ab57a7bc7683aaa1 Mon Sep 17 00:00:00 2001 From: Henry Weller Date: Mon, 30 Sep 2019 11:03:20 +0100 Subject: [PATCH] functionObject: Added executeAtStart By default most functionObjects now execute and write at the start-time except those that perform averaging (fieldAverage, dsmcFields) which cannot be executed until the end of the first time-step. There is also a new optional functionObject dictionary entry "executeAtStart" which defaults to true but can be set false if the execution and results of the functionObject are not required or appropriate at the start-time. A result of this change is that time logs of forces, sampling etc. now include a values for time 0. --- .../functionObject/functionObject.C | 12 ++++- .../functionObject/functionObject.H | 20 +++++--- .../functionObjectList/functionObjectList.C | 50 ++++++++++++------- .../timeControl/timeControlFunctionObject.C | 26 +++++++++- .../timeControl/timeControlFunctionObject.H | 4 ++ .../field/fieldAverage/fieldAverage.C | 4 +- .../field/fieldAverage/fieldAverage.H | 6 +++ .../lagrangian/dsmcFields/dsmcFields.H | 6 +++ 8 files changed, 97 insertions(+), 31 deletions(-) diff --git a/src/OpenFOAM/db/functionObjects/functionObject/functionObject.C b/src/OpenFOAM/db/functionObjects/functionObject/functionObject.C index aa0ce500b2..0ffbb25218 100644 --- a/src/OpenFOAM/db/functionObjects/functionObject/functionObject.C +++ b/src/OpenFOAM/db/functionObjects/functionObject/functionObject.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -44,7 +44,8 @@ bool Foam::functionObject::postProcess(false); Foam::functionObject::functionObject(const word& name) : name_(name), - log(postProcess) + log(false), + executeAtStart_(true) {} @@ -128,12 +129,19 @@ bool Foam::functionObject::read(const dictionary& dict) if (!postProcess) { log = dict.lookupOrDefault("log", true); + executeAtStart_ = dict.lookupOrDefault("executeAtStart", true); } return true; } +bool Foam::functionObject::executeAtStart() const +{ + return executeAtStart_; +} + + bool Foam::functionObject::end() { return true; diff --git a/src/OpenFOAM/db/functionObjects/functionObject/functionObject.H b/src/OpenFOAM/db/functionObjects/functionObject/functionObject.H index 3e5a5f6a1a..0c3a549301 100644 --- a/src/OpenFOAM/db/functionObjects/functionObject/functionObject.H +++ b/src/OpenFOAM/db/functionObjects/functionObject/functionObject.H @@ -32,7 +32,7 @@ Description field and derived quantities. Alternatively, the same actions can be executed after the simulation using the \c -postProcess command-line option. - \subsection secFunctionObjects Using function objects + \subsection secFunctionObjects Using functionObjects FunctionObjects are selected by additional entries in the $FOAM_CASE/system/controlDict dictionary. Each object is listed in the \c @@ -60,7 +60,7 @@ Description Where: \table Property | Description | Required | Default value - type | Type of function object | yes | + type | Type of functionObject | yes | libs | Libraries containing implementation | yes | region | Name of region for multi-region cases | no | enabled | On/off switch | no | yes @@ -92,7 +92,7 @@ Description libraries and the \c libs entry is used to specify which library should be loaded. - Each function object has two separate run phases: + Each functionObject has two separate run phases: - The \c execute phase is meant to be used for updating calculations or for management tasks. @@ -104,7 +104,7 @@ Class Foam::functionObject Description - Abstract base-class for Time/database function objects. + Abstract base-class for Time/database functionObjects. See also Foam::functionObjectList @@ -158,6 +158,9 @@ public: //- Switch write log to Info Switch log; + //- Switch write log to Info + Switch executeAtStart_; + // Declare run-time constructor selection tables @@ -207,9 +210,12 @@ public: //- Return the name of this functionObject const word& name() const; - //- Read and set the function object if its data have changed + //- Read and set the functionObject if its data have changed virtual bool read(const dictionary&); + //- Return true if the functionObject should be executed at the start + virtual bool executeAtStart() const; + //- Called at each ++ or += of the time-loop. // postProcess overrides the usual executeControl behaviour and // forces execution (used in post-processing mode) @@ -224,12 +230,12 @@ public: // By default it simply calls execute(). virtual bool end(); - //- Called by Time::setDeltaT(). Allows the function object to override + //- Called by Time::setDeltaT(). Allows the functionObject to override // the time-step value. // Returns whether or not the value was overridden. virtual bool setTimeStep(); - //- Called by Time::adjustTimeStep(). Allows the function object to + //- Called by Time::adjustTimeStep(). Allows the functionObject to // insert a write time earlier than that already in use by the run // time. Returns the write time, or vGreat. virtual scalar timeToNextWrite(); diff --git a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C index 427d5bb7fd..9dea8bcc4e 100644 --- a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C +++ b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C @@ -565,11 +565,11 @@ void Foam::functionObjectList::clear() Foam::label Foam::functionObjectList::findObjectID(const word& name) const { - forAll(*this, objectI) + forAll(*this, oi) { - if (operator[](objectI).name() == name) + if (operator[](oi).name() == name) { - return objectI; + return oi; } } @@ -598,7 +598,21 @@ bool Foam::functionObjectList::status() const bool Foam::functionObjectList::start() { - return read(); + bool ok = read(); + + if (execution_) + { + forAll(*this, oi) + { + if (operator[](oi).executeAtStart()) + { + ok = operator[](oi).execute() && ok; + ok = operator[](oi).write() && ok; + } + } + } + + return ok; } @@ -613,10 +627,10 @@ bool Foam::functionObjectList::execute() read(); } - forAll(*this, objectI) + forAll(*this, oi) { - ok = operator[](objectI).execute() && ok; - ok = operator[](objectI).write() && ok; + ok = operator[](oi).execute() && ok; + ok = operator[](oi).write() && ok; } } @@ -635,9 +649,9 @@ bool Foam::functionObjectList::end() read(); } - forAll(*this, objectI) + forAll(*this, oi) { - ok = operator[](objectI).end() && ok; + ok = operator[](oi).end() && ok; } } @@ -658,11 +672,11 @@ bool Foam::functionObjectList::setTimeStep() wordList names; - forAll(*this, objectI) + forAll(*this, oi) { - if (operator[](objectI).setTimeStep()) + if (operator[](oi).setTimeStep()) { - names.append(operator[](objectI).name()); + names.append(operator[](oi).name()); set = true; } } @@ -693,9 +707,9 @@ Foam::scalar Foam::functionObjectList::timeToNextWrite() read(); } - forAll(*this, objectI) + forAll(*this, oi) { - result = min(result, operator[](objectI).timeToNextWrite()); + result = min(result, operator[](oi).timeToNextWrite()); } } @@ -870,9 +884,9 @@ void Foam::functionObjectList::updateMesh(const mapPolyMesh& mpm) { if (execution_) { - forAll(*this, objectI) + forAll(*this, oi) { - operator[](objectI).updateMesh(mpm); + operator[](oi).updateMesh(mpm); } } } @@ -882,9 +896,9 @@ void Foam::functionObjectList::movePoints(const polyMesh& mesh) { if (execution_) { - forAll(*this, objectI) + forAll(*this, oi) { - operator[](objectI).movePoints(mesh); + operator[](oi).movePoints(mesh); } } } diff --git a/src/OpenFOAM/db/functionObjects/timeControl/timeControlFunctionObject.C b/src/OpenFOAM/db/functionObjects/timeControl/timeControlFunctionObject.C index 6fd3d21ef1..8080ec58fc 100644 --- a/src/OpenFOAM/db/functionObjects/timeControl/timeControlFunctionObject.C +++ b/src/OpenFOAM/db/functionObjects/timeControl/timeControlFunctionObject.C @@ -93,9 +93,23 @@ Foam::functionObjects::timeControl::timeControl // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // +bool Foam::functionObjects::timeControl::executeAtStart() const +{ + return foPtr_->executeAtStart(); +} + + bool Foam::functionObjects::timeControl::execute() { - if (active() && (postProcess || executeControl_.execute())) + if + ( + active() + && ( + postProcess + || executeControl_.execute() + || (executeAtStart() && time_.timeIndex() == time_.startTimeIndex()) + ) + ) { foPtr_->execute(); } @@ -106,7 +120,15 @@ bool Foam::functionObjects::timeControl::execute() bool Foam::functionObjects::timeControl::write() { - if (active() && (postProcess || writeControl_.execute())) + if + ( + active() + && ( + postProcess + || writeControl_.execute() + || (executeAtStart() && time_.timeIndex() == time_.startTimeIndex()) + ) + ) { foPtr_->write(); } diff --git a/src/OpenFOAM/db/functionObjects/timeControl/timeControlFunctionObject.H b/src/OpenFOAM/db/functionObjects/timeControl/timeControlFunctionObject.H index 0890070c78..9d5ee66836 100644 --- a/src/OpenFOAM/db/functionObjects/timeControl/timeControlFunctionObject.H +++ b/src/OpenFOAM/db/functionObjects/timeControl/timeControlFunctionObject.H @@ -142,6 +142,10 @@ public: // Function object control + //- Return true if the functionObject should be executed + // at the start + virtual bool executeAtStart() const; + //- Called at each ++ or += of the time-loop. // postProcess overrides the usual executeControl behaviour and // forces execution (used in post-processing mode) diff --git a/src/functionObjects/field/fieldAverage/fieldAverage.C b/src/functionObjects/field/fieldAverage/fieldAverage.C index 257c200d81..2cb1c45041 100644 --- a/src/functionObjects/field/fieldAverage/fieldAverage.C +++ b/src/functionObjects/field/fieldAverage/fieldAverage.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -151,7 +151,7 @@ void Foam::functionObjects::fieldAverage::calcAverages() periodIndex_++; } - Log << type() << " " << name() << " write:" << nl + Log << type() << " " << name() << nl << " Calculating averages" << nl; addMeanSqrToPrime2Mean(); diff --git a/src/functionObjects/field/fieldAverage/fieldAverage.H b/src/functionObjects/field/fieldAverage/fieldAverage.H index cc9ef558c6..5ef49cfaa1 100644 --- a/src/functionObjects/field/fieldAverage/fieldAverage.H +++ b/src/functionObjects/field/fieldAverage/fieldAverage.H @@ -286,6 +286,12 @@ public: //- Read the field average data virtual bool read(const dictionary&); + //- Do not average at the start of the run + virtual bool executeAtStart() const + { + return false; + } + //- Calculate the field averages virtual bool execute(); diff --git a/src/functionObjects/lagrangian/dsmcFields/dsmcFields.H b/src/functionObjects/lagrangian/dsmcFields/dsmcFields.H index 0082585272..c9613eb90b 100644 --- a/src/functionObjects/lagrangian/dsmcFields/dsmcFields.H +++ b/src/functionObjects/lagrangian/dsmcFields/dsmcFields.H @@ -89,6 +89,12 @@ public: //- Read the dsmcFields data virtual bool read(const dictionary&); + //- Do not evaluate the state at the start of the run + virtual bool executeAtStart() const + { + return false; + } + //- Do nothing virtual bool execute();