diff --git a/src/OpenFOAM/primitives/functions/Function1/LimitRange/LimitRange.C b/src/OpenFOAM/primitives/functions/Function1/LimitRange/LimitRange.C new file mode 100644 index 0000000000000000000000000000000000000000..8578cec6b7e8779943196bec507b58452fa8d97e --- /dev/null +++ b/src/OpenFOAM/primitives/functions/Function1/LimitRange/LimitRange.C @@ -0,0 +1,87 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2020 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 "LimitRange.H" + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +template<class Type> +void Foam::Function1Types::LimitRange<Type>::read(const dictionary& coeffs) +{ + min_ = coeffs.get<scalar>("min"); + max_ = coeffs.get<scalar>("max"); + value_ = Function1<Type>::New("value", coeffs); +} + + +template<class Type> +Foam::Function1Types::LimitRange<Type>::LimitRange +( + const word& entryName, + const dictionary& dict +) +: + Function1<Type>(entryName) +{ + read(dict); +} + + +template<class Type> +Foam::Function1Types::LimitRange<Type>::LimitRange(const LimitRange<Type>& rhs) +: + Function1<Type>(rhs), + min_(rhs.min_), + max_(rhs.max_), + value_(rhs.value_.clone()) +{} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +template<class Type> +void Foam::Function1Types::LimitRange<Type>::writeEntries(Ostream& os) const +{ + os.writeEntry("min", min_); + os.writeEntry("max", max_); + value_->writeData(os); +} + + +template<class Type> +void Foam::Function1Types::LimitRange<Type>::writeData(Ostream& os) const +{ + Function1<Type>::writeData(os); + os << token::END_STATEMENT << nl; + + os.beginBlock(word(this->name() + "Coeffs")); + writeEntries(os); + os.endBlock(); +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/functions/Function1/LimitRange/LimitRange.H b/src/OpenFOAM/primitives/functions/Function1/LimitRange/LimitRange.H new file mode 100644 index 0000000000000000000000000000000000000000..b580f90d108a798358076ebb8389f4d6ed85f0fc --- /dev/null +++ b/src/OpenFOAM/primitives/functions/Function1/LimitRange/LimitRange.H @@ -0,0 +1,179 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2020 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::LimitRange + +Description + Function1 wrapper that limits the input range of another Function1 + + Example usage for limiting a polynomial: + \verbatim + limitedPolyTest limitRange; + limitedPolyTestCoeffs + { + min 0.4; + max 1.4; + + value polynomial + ( + (5 1) + (-2 2) + (-2 3) + (1 4) + ); + } + \endverbatim + + Here the return value will be: + - poly(0.4) for x <= 0.4; + - poly(1.4) for x >= 1.4; and + - poly(x) for 0.4 < x < 1.4. + + + Example usage for limiting a table + \verbatim + limitedTableFileTest limitRange; + limitedTableFileTestCoeffs + { + min 0.4; + max 1.4; + + value tableFile; + file "<system>/fanCurve.txt"; + } + \endverbatim + + Where: + \table + Property | Description | Required + min | Minimum input value | yes + max | Maximum input value | yes + value | Function of type Function1<Type> | yes + \endtable + +SourceFiles + LimitRange.C + +\*---------------------------------------------------------------------------*/ + +#ifndef LimitRange_H +#define LimitRange_H + +#include "Function1.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ +namespace Function1Types +{ + +/*---------------------------------------------------------------------------*\ + Class LimitRange Declaration +\*---------------------------------------------------------------------------*/ + +template<class Type> +class LimitRange +: + public Function1<Type> +{ + // Private Data + + //- Minimum input value + scalar min_; + + //- Maximum input value + scalar max_; + + //- Value function + autoPtr<Function1<Type>> value_; + + + // Private Member Functions + + //- Read the coefficients from the given dictionary + void read(const dictionary& coeffs); + + +public: + + //- Runtime type information + TypeName("limitRange"); + + + // Generated Methods + + //- No copy assignment + void operator=(const LimitRange<Type>&) = delete; + + + // Constructors + + //- Construct from entry name and dictionary + LimitRange(const word& entryName, const dictionary& dict); + + //- Copy construct + explicit LimitRange(const LimitRange<Type>& rhs); + + + //- Destructor + virtual ~LimitRange() = default; + + + // Member Functions + + //- Return value for time t + virtual inline Type value(const scalar t) const; + + //- Integrate between two (scalar) values + virtual inline Type integrate(const scalar x1, const scalar x2) 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 "LimitRangeI.H" + +#ifdef NoRepository + #include "LimitRange.C" +#endif + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/functions/Function1/LimitRange/LimitRangeI.H b/src/OpenFOAM/primitives/functions/Function1/LimitRange/LimitRangeI.H new file mode 100644 index 0000000000000000000000000000000000000000..640a02c3888e468f2f814d084a0dece90c0f2da6 --- /dev/null +++ b/src/OpenFOAM/primitives/functions/Function1/LimitRange/LimitRangeI.H @@ -0,0 +1,67 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2020 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 "LimitRange.H" + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +template<class Type> +inline Type Foam::Function1Types::LimitRange<Type>::value(const scalar t) const +{ + scalar tlim = min(max(t, min_), max_); + + return value_->value(tlim); +} + + +template<class Type> +Type Foam::Function1Types::LimitRange<Type>::integrate +( + const scalar x1, + const scalar x2 +) const +{ + scalar xlim0 = min(max(x1, min_), max_); + scalar xlim1 = min(max(x2, min_), max_); + + Type intValue = value_->integrate(xlim0, xlim1); + + if (x1 < min_) + { + intValue += (min(min_, x2) - x1)*this->value(min_); + } + + if (x2 > max_) + { + intValue += (x2 - max(max_, x1))*this->value(max_); + } + + return intValue; +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/functions/Function1/makeFunction1s.C b/src/OpenFOAM/primitives/functions/Function1/makeFunction1s.C index ee4681df2b50c379a3f7d8052b10065a1d3ac1a8..3ecf9c0e123abbd7f8dc702912af30930ef55efe 100644 --- a/src/OpenFOAM/primitives/functions/Function1/makeFunction1s.C +++ b/src/OpenFOAM/primitives/functions/Function1/makeFunction1s.C @@ -38,7 +38,7 @@ License #include "Table.H" #include "TableFile.H" #include "Scale.H" - +#include "LimitRange.H" #include "fieldTypes.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -56,7 +56,8 @@ License makeFunction1Type(CSV, Type); \ makeFunction1Type(Table, Type); \ makeFunction1Type(TableFile, Type); \ - makeFunction1Type(Scale, Type); + makeFunction1Type(Scale, Type); \ + makeFunction1Type(LimitRange, Type); namespace Foam {