OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

Polynomial.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 2008-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "Polynomial.H"
00028 
00029 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00030 
00031 template<int PolySize>
00032 Foam::Polynomial<PolySize>::Polynomial()
00033 :
00034     VectorSpace<Polynomial<PolySize>, scalar, PolySize>(),
00035     logActive_(false),
00036     logCoeff_(0.0)
00037 {}
00038 
00039 
00040 template<int PolySize>
00041 Foam::Polynomial<PolySize>::Polynomial(const word& name, Istream& is)
00042 :
00043     VectorSpace<Polynomial<PolySize>, scalar, PolySize>(),
00044     logActive_(false),
00045     logCoeff_(0.0)
00046 {
00047     word isName(is);
00048 
00049     if (isName != name)
00050     {
00051         FatalErrorIn
00052         (
00053             "Polynomial<PolySize>::Polynomial(const word&, Istream&)"
00054         )   << "Expected polynomial name " << name << " but read " << isName
00055             << nl << exit(FatalError);
00056     }
00057 
00058     VectorSpace<Polynomial<PolySize>, scalar, PolySize>::
00059         operator=(VectorSpace<Polynomial<PolySize>, scalar, PolySize>(is));
00060 
00061     if (this->size() == 0)
00062     {
00063         FatalErrorIn
00064         (
00065             "Polynomial<PolySize>::Polynomial(const word&, Istream&)"
00066         )   << "Polynomial coefficients for entry " << isName
00067             << " are invalid (empty)" << nl << exit(FatalError);
00068     }
00069 }
00070 
00071 
00072 template<int PolySize>
00073 Foam::Polynomial<PolySize>::Polynomial
00074 (
00075     const Polynomial<PolySize>& poly
00076 )
00077 :
00078     VectorSpace<Polynomial<PolySize>, scalar, PolySize>(poly),
00079     logActive_(poly.logActive_),
00080     logCoeff_(poly.logCoeff_)
00081 {}
00082 
00083 
00084 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00085 
00086 template<int PolySize>
00087 bool& Foam::Polynomial<PolySize>::logActive()
00088 {
00089     return logActive_;
00090 }
00091 
00092 
00093 template<int PolySize>
00094 Foam::scalar& Foam::Polynomial<PolySize>::logCoeff()
00095 {
00096     return logCoeff_;
00097 }
00098 
00099 
00100 template<int PolySize>
00101 Foam::scalar Foam::Polynomial<PolySize>::evaluate(const scalar x) const
00102 {
00103     scalar y = this->v_[0];
00104 
00105     for (label i=1; i<PolySize; i++)
00106     {
00107         y += this->v_[i]*pow(x, i);
00108     }
00109 
00110     if (logActive_)
00111     {
00112         y += logCoeff_*log(x);
00113     }
00114 
00115     return y;
00116 }
00117 
00118 
00119 template<int PolySize>
00120 Foam::scalar Foam::Polynomial<PolySize>::integrateLimits
00121 (
00122     const scalar x1,
00123     const scalar x2
00124 ) const
00125 {
00126     if (logActive_)
00127     {
00128         FatalErrorIn
00129         (
00130             "scalar Polynomial<PolySize>::integrateLimits"
00131             "("
00132                 "const scalar, "
00133                 "const scalar"
00134             ") const"
00135         )   << "Cannot integrate polynomial with logarithmic coefficients"
00136             << nl << abort(FatalError);
00137     }
00138 
00139     intPolyType poly = this->integrate();
00140 
00141     return poly.evaluate(x2) - poly.evaluate(x1);
00142 }
00143 
00144 
00145 template<int PolySize>
00146 typename Foam::Polynomial<PolySize>::intPolyType
00147 Foam::Polynomial<PolySize>::integrate(const scalar intConstant)
00148 {
00149     intPolyType newCoeffs;
00150 
00151     newCoeffs[0] = intConstant;
00152     forAll(*this, i)
00153     {
00154         newCoeffs[i + 1] = this->v_[i]/(i + 1);
00155     }
00156 
00157     return newCoeffs;
00158 }
00159 
00160 
00161 template<int PolySize>
00162 typename Foam::Polynomial<PolySize>::polyType
00163 Foam::Polynomial<PolySize>::integrateMinus1(const scalar intConstant)
00164 {
00165     polyType newCoeffs;
00166 
00167     if (this->v_[0] > VSMALL)
00168     {
00169         newCoeffs.logActive() = true;
00170         newCoeffs.logCoeff() = this->v_[0];
00171     }
00172 
00173     newCoeffs[0] = intConstant;
00174 
00175     if (PolySize > 0)
00176     {
00177         for (label i=1; i<PolySize; i++)
00178         {
00179             newCoeffs[i] = this->v_[i]/i;
00180         }
00181     }
00182 
00183     return newCoeffs;
00184 }
00185 
00186 
00187 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd