polynomialFunction.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 "polynomialFunction.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35  defineTypeNameAndDebug(polynomialFunction, 0);
36 }
37 
38 
39 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
40 
41 
42 Foam::polynomialFunction Foam::polynomialFunction::cloneIntegral
43 (
44  const polynomialFunction& poly,
45  const scalar intConstant
46 )
47 {
48  polynomialFunction newPoly(poly.size()+1);
49 
50  newPoly[0] = intConstant;
51  forAll(poly, i)
52  {
53  newPoly[i+1] = poly[i]/(i + 1);
54  }
55 
56  return newPoly;
57 }
58 
59 
60 Foam::polynomialFunction Foam::polynomialFunction::cloneIntegralMinus1
61 (
62  const polynomialFunction& poly,
63  const scalar intConstant
64 )
65 {
66  polynomialFunction newPoly(poly.size()+1);
67 
68  if (poly[0] > VSMALL)
69  {
70  newPoly.logActive_ = true;
71  newPoly.logCoeff_ = poly[0];
72  }
73 
74  newPoly[0] = intConstant;
75  for (label i=1; i < poly.size(); ++i)
76  {
77  newPoly[i] = poly[i]/i;
78  }
79 
80  return newPoly;
81 }
82 
83 
84 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
85 
87 :
88  scalarList(order, Zero),
89  logActive_(false),
90  logCoeff_(0.0)
91 {
92  if (this->empty())
93  {
95  << "polynomialFunction coefficients are invalid (empty)"
96  << nl << exit(FatalError);
97  }
98 }
99 
100 
102 :
103  scalarList(poly),
104  logActive_(poly.logActive_),
105  logCoeff_(poly.logCoeff_)
106 {}
107 
108 
110 :
111  scalarList(coeffs),
112  logActive_(false),
113  logCoeff_(0.0)
114 {
115  if (this->empty())
116  {
118  << "polynomialFunction coefficients are invalid (empty)"
119  << nl << exit(FatalError);
120  }
121 }
122 
123 
125 :
126  scalarList(is),
127  logActive_(false),
128  logCoeff_(0.0)
129 {
130  if (this->empty())
131  {
133  << "polynomialFunction coefficients are invalid (empty)"
134  << nl << exit(FatalError);
135  }
136 }
137 
138 
139 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
140 
142 {}
143 
144 
145 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
146 
148 {
149  return logActive_;
150 }
151 
152 
154 {
155  return logCoeff_;
156 }
157 
158 
159 Foam::scalar Foam::polynomialFunction::value(const scalar x) const
160 {
161  const scalarList& coeffs = *this;
162  scalar val = coeffs[0];
163 
164  // avoid costly pow() in calculation
165  scalar powX = x;
166  for (label i=1; i<coeffs.size(); ++i)
167  {
168  val += coeffs[i]*powX;
169  powX *= x;
170  }
171 
172  if (logActive_)
173  {
174  val += this->logCoeff_*log(x);
175  }
176 
177  return val;
178 }
179 
180 
182 (
183  const scalar x1,
184  const scalar x2
185 ) const
186 {
187  const scalarList& coeffs = *this;
188 
189  if (logActive_)
190  {
192  << "Cannot integrate polynomial with logarithmic coefficients"
193  << nl << abort(FatalError);
194  }
195 
196  // avoid costly pow() in calculation
197  scalar powX1 = x1;
198  scalar powX2 = x2;
199 
200  scalar val = coeffs[0]*(powX2 - powX1);
201  for (label i=1; i<coeffs.size(); ++i)
202  {
203  val += coeffs[i]/(i + 1)*(powX2 - powX1);
204  powX1 *= x1;
205  powX2 *= x2;
206  }
207 
208  return val;
209 }
210 
211 
213 Foam::polynomialFunction::integral(const scalar intConstant) const
214 {
215  return cloneIntegral(*this, intConstant);
216 }
217 
218 
220 Foam::polynomialFunction::integralMinus1(const scalar intConstant) const
221 {
222  return cloneIntegralMinus1(*this, intConstant);
223 }
224 
225 
226 // * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
227 
230 {
231  scalarList& coeffs = *this;
232 
233  if (coeffs.size() > poly.size())
234  {
235  forAll(poly, i)
236  {
237  coeffs[i] += poly[i];
238  }
239  }
240  else
241  {
242  coeffs.setSize(poly.size(), 0.0);
243 
244  forAll(coeffs, i)
245  {
246  coeffs[i] += poly[i];
247  }
248  }
249 
250  return *this;
251 }
252 
253 
256 {
257  scalarList& coeffs = *this;
258 
259  if (coeffs.size() > poly.size())
260  {
261  forAll(poly, i)
262  {
263  coeffs[i] -= poly[i];
264  }
265  }
266  else
267  {
268  coeffs.setSize(poly.size(), 0.0);
269 
270  forAll(coeffs, i)
271  {
272  coeffs[i] -= poly[i];
273  }
274  }
275 
276  return *this;
277 }
278 
279 
282 {
283  scalarList& coeffs = *this;
284  forAll(coeffs, i)
285  {
286  coeffs[i] *= s;
287  }
288 
289  return *this;
290 }
291 
292 
295 {
296  scalarList& coeffs = *this;
297  forAll(coeffs, i)
298  {
299  coeffs[i] /= s;
300  }
301 
302  return *this;
303 }
304 
305 
306 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
307 
309 {
310  // output like VectorSpace
311  os << token::BEGIN_LIST;
312 
313  if (!poly.empty())
314  {
315  for (int i=0; i<poly.size()-1; i++)
316  {
317  os << poly[i] << token::SPACE;
318  }
319  os << poly.last();
320  }
321  os << token::END_LIST;
322 
323 
324  // Check state of Ostream
325  os.check(FUNCTION_NAME);
326 
327  return os;
328 }
329 
330 
331 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
332 
334 Foam::operator+
335 (
336  const polynomialFunction& p1,
337  const polynomialFunction& p2
338 )
339 {
340  polynomialFunction poly(p1);
341  return poly += p2;
342 }
343 
344 
346 Foam::operator-
347 (
348  const polynomialFunction& p1,
349  const polynomialFunction& p2
350 )
351 {
352  polynomialFunction poly(p1);
353  return poly -= p2;
354 }
355 
356 
358 Foam::operator*
359 (
360  const scalar s,
361  const polynomialFunction& p
362 )
363 {
364  polynomialFunction poly(p);
365  return poly *= s;
366 }
367 
368 
370 Foam::operator/
371 (
372  const scalar s,
373  const polynomialFunction& p
374 )
375 {
376  polynomialFunction poly(p);
377  return poly /= s;
378 }
379 
380 
382 Foam::operator*
383 (
384  const polynomialFunction& p,
385  const scalar s
386 )
387 {
388  polynomialFunction poly(p);
389  return poly *= s;
390 }
391 
392 
394 Foam::operator/
395 (
396  const polynomialFunction& p,
397  const scalar s
398 )
399 {
400  polynomialFunction poly(p);
401  return poly /= s;
402 }
403 
404 
405 // ************************************************************************* //
Foam::polynomialFunction::integralMinus1
polynomialFunction integralMinus1(const scalar intConstant=0.0) const
Return integral coefficients when lowest order is -1.
Definition: polynomialFunction.C:220
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
Foam::polynomialFunction::integral
polynomialFunction integral(const scalar intConstant=0.0) const
Return integral coefficients.
Definition: polynomialFunction.C:213
p
volScalarField & p
Definition: createFieldRefs.H:8
s
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Definition: gmvOutputSpray.H:25
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
Foam::polynomialFunction::~polynomialFunction
virtual ~polynomialFunction()
Destructor.
Definition: polynomialFunction.C:141
Foam::polynomialFunction::integrate
scalar integrate(const scalar x1, const scalar x2) const
Integrate between two values.
Definition: polynomialFunction.C:182
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
Foam::polynomialFunction::operator*=
polynomialFunction & operator*=(const scalar)
Definition: polynomialFunction.C:281
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::polynomialFunction::logCoeff
scalar logCoeff() const
Return the log coefficient.
Definition: polynomialFunction.C:153
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:51
Foam::FatalError
error FatalError
Foam::log
dimensionedScalar log(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:262
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:137
Foam::polynomialFunction::value
scalar value(const scalar x) const
Return polynomial value.
Definition: polynomialFunction.C:159
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::polynomialFunction::operator+=
polynomialFunction & operator+=(const polynomialFunction &)
Definition: polynomialFunction.C:229
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::List< scalar >
Foam::token::SPACE
Space [isspace].
Definition: token.H:112
Foam::polynomialFunction::operator-=
polynomialFunction & operator-=(const polynomialFunction &)
Definition: polynomialFunction.C:255
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
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:261
Foam::token::END_LIST
End list [isseparator].
Definition: token.H:118
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
polynomialFunction.H
Foam::polynomialFunction::operator/=
polynomialFunction & operator/=(const scalar)
Definition: polynomialFunction.C:294
Foam::polynomialFunction
Polynomial function representation.
Definition: polynomialFunction.H:76
Foam::List::setSize
void setSize(const label newSize)
Alias for resize(const label)
Definition: ListI.H:146
Foam::token::BEGIN_LIST
Begin list [isseparator].
Definition: token.H:117
Foam::polynomialFunction::logActive
bool logActive() const
Return true if the log term is active.
Definition: polynomialFunction.C:147
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &)
Definition: boundaryPatch.C:102
Foam::polynomialFunction::polynomialFunction
polynomialFunction(const label)
Construct a particular size, with all coefficients = 0.0.
Definition: polynomialFunction.C:86