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
This commit is contained in:
Mark Olesen
2021-11-25 15:41:32 +01:00
committed by Mark Olesen
parent 30a2fa4b27
commit adcf41bd13
13 changed files with 458 additions and 34 deletions

View File

@ -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;

View File

@ -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_;
}

View File

@ -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();
}
// ************************************************************************* //

View File

@ -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
// ************************************************************************* //

View File

@ -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;
}
// ************************************************************************* //

View File

@ -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

View File

@ -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);