From adcf41bd13e5d341807200e134ff122520045517 Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@esi-group.com> Date: Thu, 25 Nov 2021 15:41:32 +0100 Subject: [PATCH] ENH: add Function1 wrapping for functionObject trigger Returns a 0/1 value corresponding to function object trigger levels. Usage: \verbatim <entryName> functionObjectTrigger; <entryName>Coeffs { triggers (1 3 5); defaultValue false; // Default when no triggers activated } \endverbatim ENH: add reset() method for Constant Function1 ENH: allow forced change of trigger index - the triggers are normally increase only, but can now override this optionally --- .../functionObjectList/functionObjectList.C | 2 +- .../functionObjectProperties.C | 30 ++- .../functionObjectProperties.H | 13 +- .../stateFunctionObject/stateFunctionObject.C | 17 +- .../stateFunctionObject/stateFunctionObject.H | 13 +- .../timeControl/timeControlFunctionObject.H | 9 +- .../functions/Function1/Constant/Constant.H | 3 + .../functions/Function1/Constant/ConstantI.H | 9 +- .../FunctionObjectTrigger.C | 106 +++++++++++ .../FunctionObjectTrigger.H | 178 ++++++++++++++++++ .../FunctionObjectTriggerI.H | 89 +++++++++ .../FunctionObjectValue/FunctionObjectValue.H | 8 +- .../functions/Function1/makeFunction1s.C | 5 + 13 files changed, 453 insertions(+), 29 deletions(-) create mode 100644 src/OpenFOAM/primitives/functions/Function1/FunctionObjectTrigger/FunctionObjectTrigger.C create mode 100644 src/OpenFOAM/primitives/functions/Function1/FunctionObjectTrigger/FunctionObjectTrigger.H create mode 100644 src/OpenFOAM/primitives/functions/Function1/FunctionObjectTrigger/FunctionObjectTriggerI.H diff --git a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C index 8aa4168d6d5..6558b2543e4 100644 --- a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C +++ b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C @@ -509,7 +509,7 @@ Foam::autoPtr<Foam::functionObjectList> Foam::functionObjectList::New Foam::label Foam::functionObjectList::triggerIndex() const { - return propsDict().getOrDefault<label>("triggerIndex", labelMin); + return propsDict().getTrigger(); } diff --git a/src/OpenFOAM/db/functionObjects/functionObjectProperties/functionObjectProperties.C b/src/OpenFOAM/db/functionObjects/functionObjectProperties/functionObjectProperties.C index 81a78a16300..3a229817713 100644 --- a/src/OpenFOAM/db/functionObjects/functionObjectProperties/functionObjectProperties.C +++ b/src/OpenFOAM/db/functionObjects/functionObjectProperties/functionObjectProperties.C @@ -79,26 +79,38 @@ Foam::dictionary& Foam::functionObjects::properties::getObjectDict } +void Foam::functionObjects::properties::clearTrigger() +{ + remove("triggerIndex"); +} + + +Foam::label Foam::functionObjects::properties::getTrigger() const +{ + // Like getOrDefault, but without reporting missing entry (noisy) + label idx = labelMin; + readIfPresent("triggerIndex", idx); + return idx; +} + + bool Foam::functionObjects::properties::setTrigger ( - const label triggeri + const label triggeri, + bool increaseOnly ) { - label oldTriggeri = getOrDefault<label>("triggerIndex", labelMin); + const label currTriggeri = getTrigger(); - if (triggeri > oldTriggeri) + if (increaseOnly ? (triggeri > currTriggeri) : (triggeri != currTriggeri)) { set("triggerIndex", triggeri); return true; } - return false; -} - + // TBD: any special handling for triggeri == labelMin - eg, clearTrigger() -Foam::label Foam::functionObjects::properties::getTrigger() const -{ - return getOrDefault<label>("triggerIndex", labelMin); + return false; } diff --git a/src/OpenFOAM/db/functionObjects/functionObjectProperties/functionObjectProperties.H b/src/OpenFOAM/db/functionObjects/functionObjectProperties/functionObjectProperties.H index 2e058bfeaf9..2860f400ba9 100644 --- a/src/OpenFOAM/db/functionObjects/functionObjectProperties/functionObjectProperties.H +++ b/src/OpenFOAM/db/functionObjects/functionObjectProperties/functionObjectProperties.H @@ -117,11 +117,20 @@ public: const word& entryName ) const; + //- Remove the trigger index from the properties + void clearTrigger(); + //- Get the current trigger index label getTrigger() const; - //- Set the current trigger index - bool setTrigger(const label triggeri); + //- Set the trigger index. Normally only if greater than current + // + // \param triggeri the new trigger index + // \param increaseOnly (default) only change if new index + // is greater than the current index. + // + // \return True if the index changed + bool setTrigger(const label triggeri, bool increaseOnly = true); //- Set dictionary from named object, return true if set bool getObjectDict diff --git a/src/OpenFOAM/db/functionObjects/stateFunctionObject/stateFunctionObject.C b/src/OpenFOAM/db/functionObjects/stateFunctionObject/stateFunctionObject.C index 773dbafecfb..945445fa873 100644 --- a/src/OpenFOAM/db/functionObjects/stateFunctionObject/stateFunctionObject.C +++ b/src/OpenFOAM/db/functionObjects/stateFunctionObject/stateFunctionObject.C @@ -74,12 +74,9 @@ Foam::dictionary& Foam::functionObjects::stateFunctionObject::propertyDict() } -bool Foam::functionObjects::stateFunctionObject::setTrigger -( - const label triggeri -) +void Foam::functionObjects::stateFunctionObject::clearTrigger() { - return stateDict().setTrigger(triggeri); + return stateDict().clearTrigger(); } @@ -89,6 +86,16 @@ Foam::label Foam::functionObjects::stateFunctionObject::getTrigger() const } +bool Foam::functionObjects::stateFunctionObject::setTrigger +( + const label triggeri, + bool increaseOnly +) +{ + return stateDict().setTrigger(triggeri, increaseOnly); +} + + bool Foam::functionObjects::stateFunctionObject::foundProperty ( const word& entryName diff --git a/src/OpenFOAM/db/functionObjects/stateFunctionObject/stateFunctionObject.H b/src/OpenFOAM/db/functionObjects/stateFunctionObject/stateFunctionObject.H index 7dce6ee473c..71144f92249 100644 --- a/src/OpenFOAM/db/functionObjects/stateFunctionObject/stateFunctionObject.H +++ b/src/OpenFOAM/db/functionObjects/stateFunctionObject/stateFunctionObject.H @@ -118,11 +118,20 @@ public: //- Return true if the property exists bool foundProperty(const word& entryName) const; + //- Remove the trigger index from the properties + void clearTrigger(); + //- Get the current trigger index label getTrigger() const; - //- Set the current trigger index - bool setTrigger(const label triggeri); + //- Set the trigger index. Normally only if greater than current + // + // \param triggeri the new trigger index + // \param increaseOnly (default) only change if new index + // is greater than the current index. + // + // \return True if the index changed + bool setTrigger(const label triggeri, bool increaseOnly = true); //- Set dictionary, return true if set bool getDict diff --git a/src/OpenFOAM/db/functionObjects/timeControl/timeControlFunctionObject.H b/src/OpenFOAM/db/functionObjects/timeControl/timeControlFunctionObject.H index 44016ec16b9..d12035a5869 100644 --- a/src/OpenFOAM/db/functionObjects/timeControl/timeControlFunctionObject.H +++ b/src/OpenFOAM/db/functionObjects/timeControl/timeControlFunctionObject.H @@ -80,8 +80,7 @@ class timeControl { public: - - // Public enumerations + // Public Enumerations //- Control mode enum class controlMode @@ -97,7 +96,7 @@ public: private: - // Private data + // Private Data //- Input dictionary dictionary dict_; @@ -108,13 +107,13 @@ private: //- Control mode (combination of time/trigger behaviour) controlMode controlMode_; - //- Activation time - defaults to -VGREAT + //- Activation time - defaults to -VGREAT (ie, active) scalar timeStart_; //- De-activation time - defaults to VGREAT scalar timeEnd_; - //- Activation trigger index - defaults to labelMin + //- Activation trigger index - defaults to labelMax (ie, inactive) label triggerStart_; //- De-activation trigger index - defaults to labelMax diff --git a/src/OpenFOAM/primitives/functions/Function1/Constant/Constant.H b/src/OpenFOAM/primitives/functions/Function1/Constant/Constant.H index 965bb7d4643..e7916f4313b 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Constant/Constant.H +++ b/src/OpenFOAM/primitives/functions/Function1/Constant/Constant.H @@ -131,6 +131,9 @@ public: // Member Functions + //- Change the constant value + inline void reset(const Type& val); + //- Return constant value virtual inline Type value(const scalar) const; diff --git a/src/OpenFOAM/primitives/functions/Function1/Constant/ConstantI.H b/src/OpenFOAM/primitives/functions/Function1/Constant/ConstantI.H index 5e05dd30f88..ed120b5d2da 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Constant/ConstantI.H +++ b/src/OpenFOAM/primitives/functions/Function1/Constant/ConstantI.H @@ -31,7 +31,14 @@ License // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template<class Type> -inline Type Foam::Function1Types::Constant<Type>::value(const scalar x) const +inline void Foam::Function1Types::Constant<Type>::reset(const Type& val) +{ + value_ = val; +} + + +template<class Type> +inline Type Foam::Function1Types::Constant<Type>::value(const scalar) const { return value_; } diff --git a/src/OpenFOAM/primitives/functions/Function1/FunctionObjectTrigger/FunctionObjectTrigger.C b/src/OpenFOAM/primitives/functions/Function1/FunctionObjectTrigger/FunctionObjectTrigger.C new file mode 100644 index 00000000000..cc94b94eb58 --- /dev/null +++ b/src/OpenFOAM/primitives/functions/Function1/FunctionObjectTrigger/FunctionObjectTrigger.C @@ -0,0 +1,106 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2021 OpenCFD Ltd. +------------------------------------------------------------------------------- +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/>. + +\*---------------------------------------------------------------------------*/ + +#include "FunctionObjectTrigger.H" + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +template<class Type> +void Foam::Function1Types::FunctionObjectTrigger<Type>::read +( + const dictionary& coeffs +) +{ + triggers_ = coeffs.get<labelList>("triggers", keyType::LITERAL); + defaultValue_ = + coeffs.getOrDefault("defaultValue", false, keyType::LITERAL); +} + + +template<class Type> +Foam::Function1Types::FunctionObjectTrigger<Type>::FunctionObjectTrigger +( + const word& entryName, + const dictionary& dict, + const objectRegistry* obrPtr +) +: + Function1<Type>(entryName, dict, obrPtr), + triggers_(), + defaultValue_(false) +{ + read(dict); +} + + +template<class Type> +Foam::Function1Types::FunctionObjectTrigger<Type>::FunctionObjectTrigger +( + const FunctionObjectTrigger<Type>& rhs +) +: + Function1<Type>(rhs), + triggers_(rhs.triggers_), + defaultValue_(rhs.defaultValue_) +{} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +template<class Type> +void Foam::Function1Types::FunctionObjectTrigger<Type>::writeEntries +( + Ostream& os +) const +{ + os.writeKeyword("triggers"); + flatOutput(triggers_); + os.endEntry(); + + if (defaultValue_) + { + os.writeEntry("default", "true"); + } +} + + +template<class Type> +void Foam::Function1Types::FunctionObjectTrigger<Type>::writeData +( + Ostream& os +) const +{ + Function1<Type>::writeData(os); + os.endEntry(); + + os.beginBlock(word(this->name() + "Coeffs")); + writeEntries(os); + os.endBlock(); +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/functions/Function1/FunctionObjectTrigger/FunctionObjectTrigger.H b/src/OpenFOAM/primitives/functions/Function1/FunctionObjectTrigger/FunctionObjectTrigger.H new file mode 100644 index 00000000000..a534c8153e0 --- /dev/null +++ b/src/OpenFOAM/primitives/functions/Function1/FunctionObjectTrigger/FunctionObjectTrigger.H @@ -0,0 +1,178 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2021 OpenCFD Ltd. +------------------------------------------------------------------------------- +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/>. + +Class + Foam::Function1Types::FunctionObjectTrigger + +Description + Returns a 0/1 value corresponding to function object trigger levels. + + Usage: + \verbatim + <entryName> functionObjectTrigger; + <entryName>Coeffs + { + triggers (1 3 5); + defaultValue false; // Optional + } + \endverbatim + + Where: + \table + Property | Description | Required + triggers | List of active trigger states to check for | yes | + defaultValue | Treatment for unactivated trigger state | no | false + \endtable + + In some circumstances, it can be useful to treat an unactivated trigger + as being true. This is the role of the "defaultValue" keyword. + +Note +- does not implement integrate() + +SourceFiles + FunctionObjectTrigger.C + FunctionObjectTriggerI.H + +\*---------------------------------------------------------------------------*/ + +#ifndef Function1Types_FunctionObjectTrigger_H +#define Function1Types_FunctionObjectTrigger_H + +#include "Function1.H" +#include "labelList.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ +namespace Function1Types +{ + +/*---------------------------------------------------------------------------*\ + Class FunctionObjectTrigger Declaration +\*---------------------------------------------------------------------------*/ + +template<class Type> +class FunctionObjectTrigger +: + public Function1<Type> +{ + + // Private Data + + //- Trigger indices when it is considered active + labelList triggers_; + + //- Treatment for unactivated trigger state (true/false) + bool defaultValue_; + + + // Private Member Functions + + //- Is the trigger considered active? + inline bool active() const; + + //- Read the coefficients from the given dictionary + void read(const dictionary& coeffs); + + +public: + + //- Runtime type information + TypeName("functionObjectTrigger"); + + + // Generated Methods + + //- No copy assignment + void operator=(const FunctionObjectTrigger<Type>&) = delete; + + + // Constructors + + //- Construct from entry name, dictionary and optional registry + FunctionObjectTrigger + ( + const word& entryName, + const dictionary& dict, + const objectRegistry* obrPtr = nullptr + ); + + //- Copy construct + explicit FunctionObjectTrigger(const FunctionObjectTrigger<Type>& rhs); + + //- Construct and return a clone + virtual tmp<Function1<Type>> clone() const + { + return tmp<Function1<Type>>(new FunctionObjectTrigger<Type>(*this)); + } + + + //- Destructor + virtual ~FunctionObjectTrigger() = default; + + + // Member Functions + + //- Return the trigger indices + inline const labelList& triggers() const noexcept; + + //- Change the trigger indices + inline void resetTriggers(const labelUList& indices); + + + //- Return 0/1 value at current time + virtual inline Type value(const scalar /*unused*/) const; + + //- Integrate between two (scalar) values. Not implemented! + virtual inline Type integrate(const scalar, const scalar) const; + + //- Write in dictionary format + virtual void writeData(Ostream& os) const; + + //- Write coefficient entries in dictionary format + void writeEntries(Ostream& os) const; +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Function1Types +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#include "FunctionObjectTriggerI.H" + +#ifdef NoRepository + #include "FunctionObjectTrigger.C" +#endif + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/functions/Function1/FunctionObjectTrigger/FunctionObjectTriggerI.H b/src/OpenFOAM/primitives/functions/Function1/FunctionObjectTrigger/FunctionObjectTriggerI.H new file mode 100644 index 00000000000..83208c63b4a --- /dev/null +++ b/src/OpenFOAM/primitives/functions/Function1/FunctionObjectTrigger/FunctionObjectTriggerI.H @@ -0,0 +1,89 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2021 OpenCFD Ltd. +------------------------------------------------------------------------------- +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/>. + +\*---------------------------------------------------------------------------*/ + +#include "FunctionObjectTrigger.H" + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +template<class Type> +inline bool Foam::Function1Types::FunctionObjectTrigger<Type>::active() const +{ + if (triggers_.empty()) + { + return false; + } + + const label idx = this->time().functionObjects().triggerIndex(); + + return + ( + idx == labelMin ? defaultValue_ : triggers_.found(idx) + ); +} + + +template<class Type> +inline const Foam::labelList& +Foam::Function1Types::FunctionObjectTrigger<Type>::triggers() const noexcept +{ + return triggers_; +} + + +template<class Type> +inline void Foam::Function1Types::FunctionObjectTrigger<Type>::resetTriggers +( + const labelUList& indices +) +{ + triggers_ = indices; +} + + +template<class Type> +inline Type Foam::Function1Types::FunctionObjectTrigger<Type>::value +( + const scalar /*unused*/ +) const +{ + return this->active() ? pTraits<Type>::one : pTraits<Type>::zero; +} + + +template<class Type> +inline Type Foam::Function1Types::FunctionObjectTrigger<Type>::integrate +( + const scalar x1, + const scalar x2 +) const +{ + NotImplemented; + return pTraits<Type>::zero; +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/functions/Function1/FunctionObjectValue/FunctionObjectValue.H b/src/OpenFOAM/primitives/functions/Function1/FunctionObjectValue/FunctionObjectValue.H index 501b2b1a727..103965f937d 100644 --- a/src/OpenFOAM/primitives/functions/Function1/FunctionObjectValue/FunctionObjectValue.H +++ b/src/OpenFOAM/primitives/functions/Function1/FunctionObjectValue/FunctionObjectValue.H @@ -40,12 +40,12 @@ Description } \endverbatim - Note - does not implement integrate() SourceFiles FunctionObjectValue.C + FunctionObjectValueI.H \*---------------------------------------------------------------------------*/ @@ -129,10 +129,10 @@ public: // Member Functions - //- Return value for time t - virtual inline Type value(const scalar t) const; + //- Return value at current time + virtual inline Type value(const scalar /*unused*/) const; - //- Integrate between two (scalar) values + //- Integrate between two (scalar) values. Not implemented! virtual Type integrate(const scalar x1, const scalar x2) const; //- Write in dictionary format diff --git a/src/OpenFOAM/primitives/functions/Function1/makeFunction1s.C b/src/OpenFOAM/primitives/functions/Function1/makeFunction1s.C index 179209c655b..03279221ada 100644 --- a/src/OpenFOAM/primitives/functions/Function1/makeFunction1s.C +++ b/src/OpenFOAM/primitives/functions/Function1/makeFunction1s.C @@ -39,6 +39,7 @@ License #include "TableFile.H" #include "Scale.H" #include "InputValueMapper.H" +#include "FunctionObjectTrigger.H" #include "FunctionObjectValue.H" #include "fieldTypes.H" @@ -73,6 +74,10 @@ namespace Foam makeFunction1(label); makeFunction1Type(Constant, label); + makeFunction1Type(FunctionObjectTrigger, label); + makeFunction1Type(FunctionObjectTrigger, scalar); + // Only (label/scalar) makes sense for triggers + makeFunction1s(scalar); makeFunction1s(vector); makeFunction1s(sphericalTensor); -- GitLab