Files
OpenFOAM-6/src/OpenFOAM/primitives/functions/Function1/PolynomialEntry/PolynomialEntry.C
Henry Weller 0b4643922a Rename DataEntry -> Function1
Function1 is an abstract base-class of run-time selectable unary
functions which may be composed of other Function1's allowing the user
to specify complex functions of a single scalar variable, e.g. time.
The implementations need not be a simple or continuous functions;
interpolated tables and polynomials are also supported.  In fact form of
mapping between a single scalar input and a single primitive type output
is supportable.

The primary application of Function1 is in time-varying boundary
conditions, it also used for other functions of time, e.g. injected mass
is spray simulations but is not limited to functions of time.
2016-02-08 16:18:07 +00:00

206 lines
5.0 KiB
C

/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 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 "PolynomialEntry.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Type>
Foam::Function1Types::Polynomial<Type>::Polynomial
(
const word& entryName,
const dictionary& dict
)
:
Function1<Type>(entryName),
coeffs_(),
canIntegrate_(true)
{
Istream& is(dict.lookup(entryName));
word entryType(is);
is >> coeffs_;
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)
{
canIntegrate_ = false;
break;
}
}
if (debug)
{
if (!canIntegrate_)
{
WarningInFunction
<< "Polynomial " << this->name_ << " cannot be integrated"
<< endl;
}
}
}
template<class Type>
Foam::Function1Types::Polynomial<Type>::Polynomial
(
const word& entryName,
const List<Tuple2<Type, Type>>& coeffs
)
:
Function1<Type>(entryName),
coeffs_(coeffs),
canIntegrate_(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() + 1) < ROOTVSMALL)
{
canIntegrate_ = false;
break;
}
}
if (debug)
{
if (!canIntegrate_)
{
WarningInFunction
<< "Polynomial " << this->name_ << " cannot be integrated"
<< endl;
}
}
}
template<class Type>
Foam::Function1Types::Polynomial<Type>::Polynomial(const Polynomial& poly)
:
Function1<Type>(poly),
coeffs_(poly.coeffs_),
canIntegrate_(poly.canIntegrate_)
{}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
template<class Type>
Foam::Function1Types::Polynomial<Type>::~Polynomial()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
void Foam::Function1Types::Polynomial<Type>::convertTimeBase(const Time& t)
{
forAll(coeffs_, i)
{
Type value = coeffs_[i].first();
for (direction cmpt = 0; cmpt < pTraits<Type>::nComponents; cmpt++)
{
setComponent(coeffs_[i].first(), cmpt) =
t.userTimeToTime(component(value, cmpt));
}
}
}
template<class Type>
Type Foam::Function1Types::Polynomial<Type>::value(const scalar x) const
{
Type y(pTraits<Type>::zero);
forAll(coeffs_, i)
{
y += cmptMultiply
(
coeffs_[i].first(),
cmptPow(pTraits<Type>::one*x, coeffs_[i].second())
);
}
return y;
}
template<class Type>
Type Foam::Function1Types::Polynomial<Type>::integrate
(
const scalar x1,
const scalar x2
) const
{
Type intx(pTraits<Type>::zero);
if (canIntegrate_)
{
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;
}
// * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * * * //
#include "PolynomialEntryIO.C"
// ************************************************************************* //