adding polynomial class

This commit is contained in:
andy
2009-06-10 18:02:40 +01:00
parent 5554300fb9
commit 530a86e540
5 changed files with 449 additions and 0 deletions

View File

@ -0,0 +1,162 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
\\/ 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 2 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, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
\*---------------------------------------------------------------------------*/
#include "Polynomial.H"
#include "error.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<int PolySize>
Foam::Polynomial<PolySize>::Polynomial()
:
VectorSpace<Polynomial<PolySize>, scalar, PolySize>(),
name_("unknownPolynomialName")
{}
template<int PolySize>
Foam::Polynomial<PolySize>::Polynomial(const word& name, Istream& is)
:
VectorSpace<Polynomial<PolySize>, scalar, PolySize>(),
name_(is)
{
if (name_ != name)
{
FatalErrorIn
(
"Foam::Polynomial<PolySize>::Polynomial(const word&, Istream&)"
) << "Expected polynomial name " << name << " but read " << name_
<< nl << exit(FatalError);
}
VectorSpace<Polynomial<PolySize>, scalar, PolySize>::
operator=(polyType(is));
if (this->size() == 0)
{
FatalErrorIn
(
"Foam::Polynomial<PolySize>::Polynomial(const word&, Istream&)"
) << "Polynomial coefficients for entry " << name_
<< " are invalid (empty)" << nl << exit(FatalError);
}
}
template<int PolySize>
Foam::Polynomial<PolySize>::Polynomial(const Polynomial<PolySize>& poly)
:
VectorSpace<Polynomial<PolySize>, scalar, PolySize>(poly),
name_(poly.name_)
{}
template<int PolySize>
Foam::Polynomial<PolySize>::Polynomial
(
const word& name,
const Polynomial<PolySize>& poly
)
:
VectorSpace<Polynomial<PolySize>, scalar, PolySize>(poly),
name_(name)
{}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
template<int PolySize>
Foam::Polynomial<PolySize>::~Polynomial()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<int PolySize>
const Foam::word& Foam::Polynomial<PolySize>::name() const
{
return name_;
}
template<int PolySize>
Foam::scalar Foam::Polynomial<PolySize>::evaluate(const scalar x) const
{
scalar y = 0.0;
forAll(*this, i)
{
y += this->v_[i]*pow(x, i);
}
return y;
}
template<int PolySize>
Foam::scalar Foam::Polynomial<PolySize>::integrateLimits
(
const scalar x1,
const scalar x2
) const
{
scalar intx = 0.0;
forAll(*this, i)
{
intx += this->v_[i]/(i + 1)*(pow(x2, i + 1) - pow(x1, i + 1));
}
return intx;
}
template<int PolySize>
typename Foam::Polynomial<PolySize>::intPolyType
Foam::Polynomial<PolySize>::integrate(const scalar intConstant)
{
intPolyType newCoeffs;
newCoeffs[0] = intConstant;
forAll(*this, i)
{
newCoeffs[i + 1] = this->v_[i]/(i + 1);
}
return newCoeffs;
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<int PolySize>
void Foam::Polynomial<PolySize>::operator=(const Polynomial<PolySize>& poly)
{
name_ = poly.name_;
VectorSpace<Polynomial<PolySize>, scalar, PolySize>::operator=(poly);
}
// ************************************************************************* //

View File

@ -0,0 +1,180 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
\\/ 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 2 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, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Class
Foam::Polynomial
Description
Polynomial templated on size (order):
poly = sum(coeff_[i]*x^i)
where 0 <= i <= n
- integer powers, starting at zero
- evaluate(x) to evaluate the poly for a given value
- integrate(x1, x2) between two scalar values
- integrate() to return a new, intergated coeff polynomial
- increases the size (order)
SourceFiles
Polynomial.C
\*---------------------------------------------------------------------------*/
#ifndef Polynomial_H
#define Polynomial_H
#include "word.H"
#include "scalar.H"
#include "Ostream.H"
#include "VectorSpace.H"
#include "Vector.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// Forward declaration of classes
template<int PolySize>
class Polynomial;
// Forward declaration of friend functions
template<int PolySize>
Ostream& operator<<
(
Ostream&,
const Polynomial<PolySize>&
);
/*---------------------------------------------------------------------------*\
Class Polynomial Declaration
\*---------------------------------------------------------------------------*/
template<int PolySize>
class Polynomial
:
public VectorSpace<Polynomial<PolySize>, scalar, PolySize>
{
private:
// Private data
//- Polynomial name
word name_;
public:
typedef VectorSpace<Polynomial<PolySize>, scalar, PolySize> polyType;
typedef Polynomial<PolySize+1> intPolyType;
//- Run-time type information
TypeName("Polynomial")
// Constructors
//- Construct null
Polynomial();
//- Construct from name and Istream
Polynomial(const word& name, Istream& is);
//- Copy constructor
Polynomial(const Polynomial& poly);
//- Copy constructor with name
Polynomial(const word& name, const Polynomial& poly);
//- Destructor
~Polynomial();
// Member Functions
// Access
//- Return const access to the polynomial name
const word& name() const;
// Evaluation
//- Return polynomial value
scalar evaluate(const scalar x) const;
//- Return integrated polynomial coefficients
// argument becomes zeroth element (constant of integration)
intPolyType integrate(const scalar intConstant = 0.0);
//- Integrate between two values
scalar integrateLimits(const scalar x1, const scalar x2) const;
// Member operators
void operator=(const Polynomial& poly);
//- Ostream Operator
friend Ostream& operator<< <PolySize>
(
Ostream&,
const Polynomial&
);
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#define makePolynomial(PolySize) \
\
defineTemplateTypeNameAndDebugWithName \
( \
Polynomial<PolySize>, \
"Polynomial<"#PolySize">", \
0 \
)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository
# include "Polynomial.C"
# include "PolynomialIO.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -0,0 +1,52 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
\\/ 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 2 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, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
\*---------------------------------------------------------------------------*/
#include "Polynomial.H"
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
template<int PolySize>
Foam::Ostream& Foam::operator<<
(
Ostream& os,
const Polynomial<PolySize>& poly
)
{
os << poly.name_ << token::SPACE
<< static_cast
<VectorSpace<Polynomial<PolySize>, scalar, PolySize> >(poly);
// Check state of Ostream
os.check
(
"Ostream& operator<<(Ostream&, const Polynomial<PolySize>&)"
);
return os;
}
// ************************************************************************* //

View File

@ -0,0 +1,51 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
\\/ 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 2 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, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
\*---------------------------------------------------------------------------*/
#include "Polynomial.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
namespace Foam
{
makePolynomial(2);
makePolynomial(3);
makePolynomial(4);
makePolynomial(5);
makePolynomial(6);
makePolynomial(7);
makePolynomial(8);
// explicitly define max order + 1 to define the integrated form
defineTemplateTypeNameAndDebugWithName
(
Polynomial<9>,
"Polynomial<9>",
0
);
}
// ************************************************************************* //