Polynomial.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2015 OpenFOAM Foundation
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "Polynomial.H"
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
32 template<int PolySize>
34 :
35  VectorSpace<Polynomial<PolySize>, scalar, PolySize>(),
36  logActive_(false),
37  logCoeff_(0.0)
38 {
39  for (int i = 0; i < PolySize; ++i)
40  {
41  this->v_[i] = 0.0;
42  }
43 }
44 
45 
46 template<int PolySize>
48 (
49  const Polynomial<PolySize>& poly
50 )
51 :
52  VectorSpace<Polynomial<PolySize>, scalar, PolySize>(poly),
53  logActive_(poly.logActive_),
54  logCoeff_(poly.logCoeff_)
55 {}
56 
57 
58 template<int PolySize>
59 Foam::Polynomial<PolySize>::Polynomial(const scalar coeffs[PolySize])
60 :
61  VectorSpace<Polynomial<PolySize>, scalar, PolySize>(),
62  logActive_(false),
63  logCoeff_(0.0)
64 {
65  for (int i=0; i<PolySize; i++)
66  {
67  this->v_[i] = coeffs[i];
68  }
69 }
70 
71 
72 template<int PolySize>
74 :
75  VectorSpace<Polynomial<PolySize>, scalar, PolySize>(),
76  logActive_(false),
77  logCoeff_(0.0)
78 {
79  if (coeffs.size() != PolySize)
80  {
82  << "Size mismatch: Needed " << PolySize
83  << " but given " << coeffs.size()
84  << nl << exit(FatalError);
85  }
86 
87  for (int i = 0; i < PolySize; ++i)
88  {
89  this->v_[i] = coeffs[i];
90  }
91 }
92 
93 
94 template<int PolySize>
96 :
97  VectorSpace<Polynomial<PolySize>, scalar, PolySize>(is),
98  logActive_(false),
99  logCoeff_(0.0)
100 {}
101 
102 
103 template<int PolySize>
105 :
106  VectorSpace<Polynomial<PolySize>, scalar, PolySize>(),
107  logActive_(false),
108  logCoeff_(0.0)
109 {
110  word isName(is);
111 
112  if (isName != name)
113  {
115  << "Expected polynomial name " << name << " but read " << isName
116  << nl << exit(FatalError);
117  }
118 
119  VectorSpace<Polynomial<PolySize>, scalar, PolySize>::
120  operator=(VectorSpace<Polynomial<PolySize>, scalar, PolySize>(is));
121 
122  if (this->size() == 0)
123  {
125  << "Polynomial coefficients for entry " << isName
126  << " are invalid (empty)" << nl << exit(FatalError);
127  }
128 }
129 
130 
131 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
132 
133 template<int PolySize>
135 {
136  return logActive_;
137 }
138 
139 
140 template<int PolySize>
142 {
143  return logCoeff_;
144 }
145 
146 
147 template<int PolySize>
148 Foam::scalar Foam::Polynomial<PolySize>::value(const scalar x) const
149 {
150  scalar val = this->v_[0];
151 
152  // avoid costly pow() in calculation
153  scalar powX = 1;
154  for (label i=1; i<PolySize; ++i)
155  {
156  powX *= x;
157  val += this->v_[i]*powX;
158  }
159 
160  if (logActive_)
161  {
162  val += logCoeff_*log(x);
163  }
164 
165  return val;
166 }
167 
168 
169 template<int PolySize>
170 Foam::scalar Foam::Polynomial<PolySize>::derivative(const scalar x) const
171 {
172  scalar deriv = 0;
173 
174  if (PolySize > 1)
175  {
176  // avoid costly pow() in calculation
177  deriv += this->v_[1];
178 
179  scalar powX = 1;
180  for (label i=2; i<PolySize; ++i)
181  {
182  powX *= x;
183  deriv += i*this->v_[i]*powX;
184  }
185  }
186 
187  if (logActive_)
188  {
189  deriv += logCoeff_/x;
190  }
191 
192  return deriv;
193 }
194 
195 
196 template<int PolySize>
198 (
199  const scalar x1,
200  const scalar x2
201 ) const
202 {
203  // avoid costly pow() in calculation
204  scalar powX1 = x1;
205  scalar powX2 = x2;
206 
207  scalar integ = this->v_[0]*(powX2 - powX1);
208  for (label i=1; i<PolySize; ++i)
209  {
210  powX1 *= x1;
211  powX2 *= x2;
212  integ += this->v_[i]/(i + 1)*(powX2 - powX1);
213  }
214 
215  if (logActive_)
216  {
217  integ += logCoeff_*((x2*log(x2) - x2) - (x1*log(x1) - x1));
218  }
219 
220  return integ;
221 }
222 
223 
224 template<int PolySize>
226 Foam::Polynomial<PolySize>::integral(const scalar intConstant) const
227 {
228  intPolyType newCoeffs;
229 
230  newCoeffs[0] = intConstant;
231  forAll(*this, i)
232  {
233  newCoeffs[i+1] = this->v_[i]/(i + 1);
234  }
235 
236  return newCoeffs;
237 }
238 
239 
240 template<int PolySize>
242 Foam::Polynomial<PolySize>::integralMinus1(const scalar intConstant) const
243 {
244  polyType newCoeffs;
245 
246  if (this->v_[0] > VSMALL)
247  {
248  newCoeffs.logActive_ = true;
249  newCoeffs.logCoeff_ = this->v_[0];
250  }
251 
252  newCoeffs[0] = intConstant;
253  for (label i=1; i<PolySize; ++i)
254  {
255  newCoeffs[i] = this->v_[i]/i;
256  }
257 
258  return newCoeffs;
259 }
260 
261 
262 // ************************************************************************* //
Foam::Polynomial::integralMinus1
polyType integralMinus1(const scalar intConstant=0.0) const
Return integral coefficients when lowest order is -1.
Definition: Polynomial.C:242
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::Polynomial::derivative
scalar derivative(const scalar x) const
Return derivative of the polynomial at the given x.
Definition: Polynomial.C:170
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
Foam::VectorSpace
Templated vector space.
Definition: VectorSpace.H:56
Foam::Polynomial::integral
scalar integral(const scalar x1, const scalar x2) const
Return integral between two values.
Definition: Polynomial.C:198
Foam::Polynomial::Polynomial
Polynomial()
Construct null, with all coefficients = 0.0.
Definition: Polynomial.C:33
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::Polynomial::logActive
bool logActive() const
Return true if the log term is active.
Definition: Polynomial.C:134
Foam::Polynomial::value
scalar value(const scalar x) const
Return polynomial value.
Definition: Polynomial.C:148
Polynomial.H
Foam::FatalError
error FatalError
Foam::log
dimensionedScalar log(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:262
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::Polynomial
Polynomial templated on size (order):
Definition: Polynomial.H:67
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::Polynomial::logCoeff
scalar logCoeff() const
Return the log coefficient.
Definition: Polynomial.C:141
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::UList::size
void size(const label n) noexcept
Override size to be inconsistent with allocated storage.
Definition: UListI.H:360