From 5424c5e5bc343e4ba4d1caec367d3f9cb69a22e1 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 5 Aug 2020 17:16:54 +0200 Subject: [PATCH] ENH: finer granularity for handling functionObject failure (#1779) - additional "errors" entry with enumerated values (default|warn|ignore|strict) for defining warning or error at construct or runtime stage - default : construct = warn, runtime = fatal - warn : construct = warn, runtime = warn - ignore : construct = silent, runtime = silent - strict : construct = fatal, runtime = fatal The errors control can be added at the top-level and/or for individual function objects. --- .../functionObject/functionObject.H | 18 +- .../functionObjectList/functionObjectList.C | 477 ++++++++++++++---- .../functionObjectList/functionObjectList.H | 84 ++- 3 files changed, 478 insertions(+), 101 deletions(-) diff --git a/src/OpenFOAM/db/functionObjects/functionObject/functionObject.H b/src/OpenFOAM/db/functionObjects/functionObject/functionObject.H index dd43486501..50b90f75d3 100644 --- a/src/OpenFOAM/db/functionObjects/functionObject/functionObject.H +++ b/src/OpenFOAM/db/functionObjects/functionObject/functionObject.H @@ -56,13 +56,15 @@ Description sub-dictionary, typically as in the following example: \verbatim - functions // sub-dictionary name under the system/controlDict file + functions // sub-dictionary name under the system/controlDict file { - + ..optional entries.. + + { // Mandatory entries - type ; - libs (FunctionObjects); + type ; + libs (FunctionObjects); // Mandatory entries defined in ... @@ -82,14 +84,14 @@ Description writeInterval 1; } - + { ... } ... - + { ... } @@ -101,6 +103,7 @@ Description Property | Description | Type | Reqd | Deflt type | Type name of function object | word | yes | - libs | Library name(s) for implementation | words | no | - + errors | Error handling (default/warn/ignore/strict) | word | no | inherits region | Name of region for multi-region cases | word | no | region0 enabled | Switch to turn function object on/off | bool | no | true log | Switch to write log info to standard output | bool | no | true @@ -112,6 +115,9 @@ Description writeInterval | Steps/time between write phases | label | no | 1 \endtable + If the \c errors entry is missing, it uses the value (if any) + specified within the top-level functionObjectList. + Time controls: \table Option | Description diff --git a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C index fc54c45ff1..b012a5c3a4 100644 --- a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C +++ b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C @@ -37,15 +37,64 @@ License #include "Tuple2.H" #include "etcFiles.H" #include "IOdictionary.H" +#include "Pstream.H" +#include "OSspecific.H" /* * * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * */ +//- Max number of warnings (per functionObject) +static constexpr const uint32_t maxWarnings = 10u; + Foam::fileName Foam::functionObjectList::functionObjectDictPath ( "caseDicts/postProcessing" ); +const Foam::Enum +< + Foam::functionObjectList::errorHandlingType +> +Foam::functionObjectList::errorHandlingNames_ +({ + { errorHandlingType::DEFAULT, "default" }, + { errorHandlingType::WARN, "warn" }, + { errorHandlingType::IGNORE, "ignore" }, + { errorHandlingType::STRICT, "strict" }, +}); + + +// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * // + +namespace Foam +{ + //- Mimic exit handling of the error class + static void exitNow(const error& err) + { + if (hasEnv("FOAM_ABORT")) + { + Perr<< nl << err << nl + << "\nFOAM aborting (FOAM_ABORT set)\n" << endl; + error::printStack(Perr); + std::abort(); + } + else if (Pstream::parRun()) + { + Perr<< nl << err << nl + << "\nFOAM parallel run exiting\n" << endl; + Pstream::exit(1); + } + else + { + Perr<< nl << err << nl + << "\nFOAM exiting\n" << endl; + std::exit(1); + } + } + +} // End namespace Foam + + // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * // void Foam::functionObjectList::createStateDict() const @@ -282,7 +331,7 @@ bool Foam::functionObjectList::readFunctionObject // Search for the functionObject dictionary fileName path = functionObjectList::findDict(funcName); - if (path == fileName::null) + if (path.empty()) { WarningInFunction << "Cannot find functionObject file " << funcName << endl; @@ -291,7 +340,7 @@ bool Foam::functionObjectList::readFunctionObject // Read the functionObject dictionary autoPtr fileStreamPtr(fileHandler().NewIFstream(path)); - ISstream& fileStream = fileStreamPtr(); + ISstream& fileStream = *fileStreamPtr; dictionary funcsDict(fileStream); dictionary* funcDictPtr = funcsDict.findDict(funcName); @@ -347,6 +396,45 @@ bool Foam::functionObjectList::readFunctionObject } +Foam::functionObjectList::errorHandlingType +Foam::functionObjectList::getOrDefaultErrorHandling +( + const word& key, + const dictionary& dict, + const errorHandlingType deflt +) const +{ + const entry* eptr = dict.findEntry(key, keyType::LITERAL); + + if (eptr) + { + if (eptr->isDict()) + { + Warning + << "The sub-dictionary '" << key + << "' masks error handling for functions" << endl; + } + else + { + const word enumName(eptr->get()); + + if (!errorHandlingNames_.found(enumName)) + { + // Failed the name lookup + FatalIOErrorInFunction(dict) + << enumName << " is not in enumeration: " + << errorHandlingNames_ << nl + << exit(FatalIOError); + } + + return errorHandlingNames_.get(enumName); + } + } + + return deflt; +} + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::functionObjectList::functionObjectList @@ -355,15 +443,7 @@ Foam::functionObjectList::functionObjectList const bool execution ) : - PtrList(), - digests_(), - indices_(), - time_(runTime), - parentDict_(runTime.controlDict()), - stateDictPtr_(), - objectsRegistryPtr_(), - execution_(execution), - updated_(false) + functionObjectList(runTime, runTime.controlDict(), execution) {} @@ -375,12 +455,14 @@ Foam::functionObjectList::functionObjectList ) : PtrList(), + errorHandling_(), digests_(), indices_(), + warnings_(), time_(runTime), parentDict_(parentDict), - stateDictPtr_(), - objectsRegistryPtr_(), + stateDictPtr_(nullptr), + objectsRegistryPtr_(nullptr), execution_(execution), updated_(false) {} @@ -442,9 +524,7 @@ Foam::autoPtr Foam::functionObjectList::New { modifiedControlDict = true; - wordList funcNames = args.getList("funcs"); - - for (const word& funcName : funcNames) + for (const word& funcName : args.getList("funcs")) { readFunctionObject ( @@ -478,17 +558,14 @@ Foam::autoPtr Foam::functionObjectList::New Foam::label Foam::functionObjectList::triggerIndex() const { - label triggeri = labelMin; - stateDict().readIfPresent("triggerIndex", triggeri); - - return triggeri; + return stateDict().getOrDefault