Files
OpenFOAM-12/src/OpenFOAM/primitives/functions/Function1/Polynomial1/Polynomial1.C
Will Bainbridge bf044f5c47 DimensionedFunction1: Function1 with dimensions
This can be used identically to a standard Function1. In addition, it
also permits specification of the dimensions. This allows the dimensions
to be checked. It also permits unit conversions. For example:

    <name>
    {
        type        table;

        // Dimensions
        xDimensions [CAD];  // Optional. Argument dimensions/units.
                            // Here, this specifies coordinates are in
                            // crank angle degrees (available if using
                            // engine time).
        dimensions  [mm];   // Optional. Value dimensions/units.
                            // Here, values are in mm.

        // Tablulated data
        values
        (
            (0 0)
            (60 12)         // <-- i.e., 12 mm at 60 degrees
            (180 20)
            (240 8)
            (360 0)
        );
        outOfBounds repeat;
    }

This replaces TimeFunction1, as it allows per-function control over
whether the function is considered to be one of real-time or user-time.
2023-08-08 13:11:59 +01:00

172 lines
4.2 KiB
C++

/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
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 "Polynomial1.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Type>
Foam::Function1s::Polynomial<Type>::Polynomial
(
const word& name,
const List<Tuple2<Type, Type>>& coeffs
)
:
FieldFunction1<Type, Polynomial<Type>>(name),
coeffs_(coeffs),
integrable_(true)
{
if (!coeffs_.size())
{
FatalErrorInFunction
<< "Polynomial coefficients for entry " << this->name_
<< " are invalid (empty)" << nl << exit(FatalError);
}
forAll(coeffs_, i)
{
if (mag(coeffs_[i].second() + pTraits<Type>::one) < rootVSmall)
{
integrable_ = false;
break;
}
}
if (debug)
{
if (!integrable_)
{
WarningInFunction
<< "Polynomial " << this->name_ << " cannot be integrald"
<< endl;
}
}
}
template<class Type>
Foam::Function1s::Polynomial<Type>::Polynomial
(
const word& name,
const dictionary& dict
)
:
Polynomial<Type>(name, dict.lookup("coeffs"))
{}
template<class Type>
Foam::Function1s::Polynomial<Type>::Polynomial
(
const word& name,
Istream& is
)
:
Polynomial<Type>(name, List<Tuple2<Type, Type>>(is))
{}
template<class Type>
Foam::Function1s::Polynomial<Type>::Polynomial(const Polynomial& poly)
:
FieldFunction1<Type, Polynomial<Type>>(poly),
coeffs_(poly.coeffs_),
integrable_(poly.integrable_)
{}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
template<class Type>
Foam::Function1s::Polynomial<Type>::~Polynomial()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
Type Foam::Function1s::Polynomial<Type>::value(const scalar x) const
{
Type y(Zero);
forAll(coeffs_, i)
{
y += cmptMultiply
(
coeffs_[i].first(),
cmptPow(pTraits<Type>::one*x, coeffs_[i].second())
);
}
return y;
}
template<class Type>
Type Foam::Function1s::Polynomial<Type>::integral
(
const scalar x1,
const scalar x2
) const
{
Type intx(Zero);
if (integrable_)
{
forAll(coeffs_, i)
{
intx += cmptMultiply
(
cmptDivide
(
coeffs_[i].first(),
coeffs_[i].second() + pTraits<Type>::one
),
cmptPow
(
pTraits<Type>::one*x2,
coeffs_[i].second() + pTraits<Type>::one
)
- cmptPow
(
pTraits<Type>::one*x1,
coeffs_[i].second() + pTraits<Type>::one
)
);
}
}
return intx;
}
template<class Type>
void Foam::Function1s::Polynomial<Type>::write(Ostream& os) const
{
writeKeyword(os, "coeffs") << coeffs_ << token::END_STATEMENT << nl;
}
// ************************************************************************* //