From e045d6c03b5a18b768f66c71cd7ba31247b7ec3f Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Mon, 17 Jul 2017 10:47:10 +0200 Subject: [PATCH 1/4] ENH: limit file checks in the abort function object to the master process - do not recheck the abort after it has been triggered once. This reduces the output clutter and file checks. --- src/functionObjects/utilities/abort/abort.C | 132 ++++++++++-------- src/functionObjects/utilities/abort/abort.H | 27 ++-- .../gasMixing/injectorPipe/system/abort | 13 ++ .../gasMixing/injectorPipe/system/controlDict | 1 + 4 files changed, 107 insertions(+), 66 deletions(-) create mode 100644 tutorials/compressible/rhoSimpleFoam/gasMixing/injectorPipe/system/abort diff --git a/src/functionObjects/utilities/abort/abort.C b/src/functionObjects/utilities/abort/abort.C index 93055ef551..b74dd88896 100644 --- a/src/functionObjects/utilities/abort/abort.C +++ b/src/functionObjects/utilities/abort/abort.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -61,19 +61,43 @@ Foam::functionObjects::abort::actionNames_ }; -// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // +// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * // -void Foam::functionObjects::abort::removeFile() const +// file-scope +// Long description for the action name +namespace Foam { - bool hasAbort = isFile(abortFile_); - reduce(hasAbort, orOp()); - - if (hasAbort && Pstream::master()) +static std::string longDescription(const Time::stopAtControls ctrl) +{ + switch (ctrl) { - // Cleanup ABORT file (on master only) - rm(abortFile_); + case Foam::Time::saNoWriteNow : + { + return "stop without writing data"; + break; + } + + case Time::saWriteNow : + { + return "stop and write data"; + break; + } + + case Time::saNextWrite : + { + return "stop after next data write"; + break; + } + + default: + { + // Invalid choices already filtered out by Enum + return "abort"; + break; + } } } +} // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // @@ -88,13 +112,17 @@ Foam::functionObjects::abort::abort functionObject(name), time_(runTime), abortFile_("$FOAM_CASE/" + name), - action_(Time::stopAtControls::saNextWrite) + action_(Time::stopAtControls::saNextWrite), + triggered_(false) { abortFile_.expand(); read(dict); - // Remove any old files from previous runs - removeFile(); + // Cleanup old files from previous runs + if (Pstream::master()) + { + Foam::rm(abortFile_); + } } @@ -110,6 +138,13 @@ bool Foam::functionObjects::abort::read(const dictionary& dict) { functionObject::read(dict); + if (dict.readIfPresent("file", abortFile_)) + { + abortFile_.expand(); + } + + const auto oldAction = action_; + action_ = actionNames_.lookupOrDefault ( "action", @@ -117,64 +152,42 @@ bool Foam::functionObjects::abort::read(const dictionary& dict) Time::stopAtControls::saNextWrite ); - if (dict.readIfPresent("file", abortFile_)) + // User can change action and re-trigger the abort. + // eg, they had nextWrite, but actually wanted writeNow. + if (oldAction != action_) { - abortFile_.expand(); + triggered_ = false; } + Info<< type() << " activated (" + << longDescription(action_).c_str() <<")" << nl + << " File: " << abortFile_ << endl; + return true; } bool Foam::functionObjects::abort::execute() { - bool hasAbort = isFile(abortFile_); - reduce(hasAbort, orOp()); - - if (hasAbort) + // If it has been triggered (eg, nextWrite) don't need to check it again + if (!triggered_) { - switch (action_) + bool hasAbort = (Pstream::master() && isFile(abortFile_)); + Pstream::scatter(hasAbort); + + if (hasAbort) { - case Time::saNoWriteNow : + triggered_ = time_.stopAt(action_); + + if (triggered_) { - if (time_.stopAt(action_)) - { - Info<< "USER REQUESTED ABORT (timeIndex=" - << time_.timeIndex() - << "): stop without writing data" - << endl; - } - break; + Info<< "USER REQUESTED ABORT (timeIndex=" + << time_.timeIndex() + << "): " << longDescription(action_).c_str() + << endl; } - case Time::saWriteNow : - { - if (time_.stopAt(action_)) - { - Info<< "USER REQUESTED ABORT (timeIndex=" - << time_.timeIndex() - << "): stop+write data" - << endl; - } - break; - } - - case Time::saNextWrite : - { - if (time_.stopAt(action_)) - { - Info<< "USER REQUESTED ABORT (timeIndex=" - << time_.timeIndex() - << "): stop after next data write" - << endl; - } - break; - } - - default: - { - // Invalid choices already filtered out by Enum - } + Pstream::scatter(triggered_); } } @@ -190,7 +203,12 @@ bool Foam::functionObjects::abort::write() bool Foam::functionObjects::abort::end() { - removeFile(); + // Cleanup ABORT file + if (Pstream::master()) + { + Foam::rm(abortFile_); + } + return true; } diff --git a/src/functionObjects/utilities/abort/abort.H b/src/functionObjects/utilities/abort/abort.H index 3106f857cc..bf891fd6a7 100644 --- a/src/functionObjects/utilities/abort/abort.H +++ b/src/functionObjects/utilities/abort/abort.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -30,12 +30,21 @@ Group Description Watches for presence of the named file in the $FOAM_CASE directory and aborts the calculation if it is present. + The presence of the abort file is only checked on the master process. Currently the following action types are supported: - noWriteNow - writeNow - nextWrite + \heading Function object usage + \table + Property | Description | Required | Default value + type | Type name: abort | yes | + file | The abort filename | no | $FOAM_CASE/name + action | Abort action | no | nextWrite + \endtable + SourceFiles abort.C @@ -55,7 +64,7 @@ namespace functionObjects { /*---------------------------------------------------------------------------*\ - Class abort Declaration + Class abort Declaration \*---------------------------------------------------------------------------*/ class abort @@ -76,12 +85,12 @@ class abort //- The type of action Time::stopAtControls action_; + //- Only trigger action once + bool triggered_; + // Private Member Functions - //- Remove abort file. - void removeFile() const; - //- Disallow default bitwise copy construct abort(const abort&) = delete; @@ -102,7 +111,7 @@ public: ( const word& name, const Time& runTime, - const dictionary& + const dictionary& dict ); @@ -115,13 +124,13 @@ public: //- Read the dictionary settings virtual bool read(const dictionary& dict); - //- Execute, check existence of abort file and take action + //- Check existence of abort file and take action virtual bool execute(); - //- Execute, check existence of abort file and take action + //- No-op virtual bool write(); - //- Execute at the final time-loop, used for cleanup + //- Remove abort file after the final time-loop. virtual bool end(); }; diff --git a/tutorials/compressible/rhoSimpleFoam/gasMixing/injectorPipe/system/abort b/tutorials/compressible/rhoSimpleFoam/gasMixing/injectorPipe/system/abort new file mode 100644 index 0000000000..34ec5f4b51 --- /dev/null +++ b/tutorials/compressible/rhoSimpleFoam/gasMixing/injectorPipe/system/abort @@ -0,0 +1,13 @@ +// OpenFOAM dictionary -*- C++ -*- + +ABORT +{ + type abort; + libs ("libutilityFunctionObjects.so"); + //file "$FOAM_CASE/ABORT"; // default name + // action writeNow; + action nextWrite; +} + + +// ************************************************************************* // diff --git a/tutorials/compressible/rhoSimpleFoam/gasMixing/injectorPipe/system/controlDict b/tutorials/compressible/rhoSimpleFoam/gasMixing/injectorPipe/system/controlDict index 1a62f26b57..d16a8ce7e5 100644 --- a/tutorials/compressible/rhoSimpleFoam/gasMixing/injectorPipe/system/controlDict +++ b/tutorials/compressible/rhoSimpleFoam/gasMixing/injectorPipe/system/controlDict @@ -59,6 +59,7 @@ writeInterval 100; functions { + #include "abort" #include "scalarTransport" #include "sampling" } From 7a408c713b818dbf8bd43f9789f78d9d22bd55ae Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Mon, 17 Jul 2017 12:54:02 +0200 Subject: [PATCH 2/4] ENH: refactor and combine externalFileCoupler (issue #529) --- applications/test/externalCoupler/Make/files | 3 - .../test/externalCoupler/Make/options | 7 - .../test/externalFileCoupler/Make/files | 3 + .../test/externalFileCoupler/Make/options | 5 + .../Test-externalFileCoupler.C} | 6 +- .../lumpedPointMovement/lumpedPointMovement.C | 4 +- src/finiteVolume/Make/files | 3 + .../general/coupling/externalFileCoupler.C} | 128 ++-- .../general/coupling/externalFileCoupler.H} | 163 +++-- .../general/coupling/externalFileCouplerI.H | 61 ++ .../field/externalCoupled/externalCoupled.C | 613 +++++++----------- .../field/externalCoupled/externalCoupled.H | 80 +-- .../externalCoupledTemplates.C | 6 +- src/lumpedPointMotion/Make/files | 2 - src/lumpedPointMotion/lumpedPointMovement.H | 8 +- src/lumpedPointMotion/lumpedPointMovementI.H | 5 +- .../externalCoupledMultiRegionHeater/Allrun | 3 + .../externalSolver | 76 ++- .../system/externalCoupled | 3 - 19 files changed, 578 insertions(+), 601 deletions(-) delete mode 100644 applications/test/externalCoupler/Make/files delete mode 100644 applications/test/externalCoupler/Make/options create mode 100644 applications/test/externalFileCoupler/Make/files create mode 100644 applications/test/externalFileCoupler/Make/options rename applications/test/{externalCoupler/Test-externalCoupler.C => externalFileCoupler/Test-externalFileCoupler.C} (96%) rename src/{lumpedPointMotion/externalCoupler.C => finiteVolume/cfdTools/general/coupling/externalFileCoupler.C} (75%) rename src/{lumpedPointMotion/externalCoupler.H => finiteVolume/cfdTools/general/coupling/externalFileCoupler.H} (60%) create mode 100644 src/finiteVolume/cfdTools/general/coupling/externalFileCouplerI.H diff --git a/applications/test/externalCoupler/Make/files b/applications/test/externalCoupler/Make/files deleted file mode 100644 index 0e55c301e2..0000000000 --- a/applications/test/externalCoupler/Make/files +++ /dev/null @@ -1,3 +0,0 @@ -Test-externalCoupler.C - -EXE = $(FOAM_USER_APPBIN)/Test-externalCoupler diff --git a/applications/test/externalCoupler/Make/options b/applications/test/externalCoupler/Make/options deleted file mode 100644 index e2c645cc88..0000000000 --- a/applications/test/externalCoupler/Make/options +++ /dev/null @@ -1,7 +0,0 @@ -EXE_INC = \ - -I$(LIB_SRC)/finiteVolume/lnInclude \ - -I$(LIB_SRC)/lumpedPointMotion/lnInclude - -EXE_LIBS = \ - -lfiniteVolume \ - -llumpedPointMotion diff --git a/applications/test/externalFileCoupler/Make/files b/applications/test/externalFileCoupler/Make/files new file mode 100644 index 0000000000..1dc8723085 --- /dev/null +++ b/applications/test/externalFileCoupler/Make/files @@ -0,0 +1,3 @@ +Test-externalFileCoupler.C + +EXE = $(FOAM_USER_APPBIN)/Test-externalFileCoupler diff --git a/applications/test/externalFileCoupler/Make/options b/applications/test/externalFileCoupler/Make/options new file mode 100644 index 0000000000..fa15f12452 --- /dev/null +++ b/applications/test/externalFileCoupler/Make/options @@ -0,0 +1,5 @@ +EXE_INC = \ + -I$(LIB_SRC)/finiteVolume/lnInclude + +EXE_LIBS = \ + -lfiniteVolume diff --git a/applications/test/externalCoupler/Test-externalCoupler.C b/applications/test/externalFileCoupler/Test-externalFileCoupler.C similarity index 96% rename from applications/test/externalCoupler/Test-externalCoupler.C rename to applications/test/externalFileCoupler/Test-externalFileCoupler.C index 90cd38d07c..3d04076956 100644 --- a/applications/test/externalCoupler/Test-externalCoupler.C +++ b/applications/test/externalFileCoupler/Test-externalFileCoupler.C @@ -22,14 +22,14 @@ License along with OpenFOAM. If not, see . Application - Test-externalCoupler + Test-externalFileCoupler Description Test of master/slave communication etc. \*---------------------------------------------------------------------------*/ #include "argList.H" -#include "externalCoupler.H" +#include "externalFileCoupler.H" using namespace Foam; @@ -47,7 +47,7 @@ int main(int argc, char *argv[]) const label maxCount = args.optionLookupOrDefault