Skip to content
Snippets Groups Projects
Commit a4dc2cc9 authored by Andrew Heather's avatar Andrew Heather Committed by Mattijs Janssens
Browse files

ENH: Added new LimitRange Function1 wrapper

Function1 wrapper that limits the input range of another Function1

Example usage for limiting a polynomial:

    limitedPolyTest        limitRange;
    limitedPolyTestCoeffs
    {
        min         0.4;
        max         1.4;

        value       polynomial
        (
            (5 1)
            (-2 2)
            (-2 3)
            (1 4)
        );
    }

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.
parent 9f5b8d0e
Branches
Tags
1 merge request!414Feature function1 limit range
/*---------------------------------------------------------------------------*\
========= |
\\ / 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();
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / 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
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / 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;
}
// ************************************************************************* //
......@@ -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
{
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment