Polynomial1.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "Polynomial1.H"
27 
28 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
29 
30 template<class Type>
32 (
33  const unitConversions& units,
34  List<Type> coeffs
35 )
36 {
37  unitConversion unitsXPowI(units.x);
38  forAll(coeffs, i)
39  {
40  coeffs[i] = units.value.toStandard(unitsXPowI.toUser(coeffs[i]));
41  unitsXPowI.reset(units.x*unitsXPowI);
42  }
43  return coeffs;
44 }
45 
46 
47 template<class Type>
49 (
50  const unitConversions& units,
51  List<Type> coeffs
52 )
53 {
54  unitConversion unitsXPowI(units.x);
55  forAll(coeffs, i)
56  {
57  coeffs[i] = units.value.toUser(unitsXPowI.toStandard(coeffs[i]));
58  unitsXPowI.reset(units.x*unitsXPowI);
59  }
60  return coeffs;
61 }
62 
63 
64 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
65 
66 template<class Type>
68 (
69  const word& name,
70  const unitConversions& units,
71  const dictionary& dict
72 )
73 :
74  FieldFunction1<Type, Polynomial<Type>>(name),
75  coeffs_
76  (
77  convertRead
78  (
79  dict.found("units")
80  ? Function1s::unitConversions(dict.lookup("units"))
81  : units,
82  dict.lookup("coeffs")
83  )
84  )
85 {
86  if (!coeffs_.size())
87  {
89  << typeName.capitalise() << ' ' << name
90  << " must have at least one coefficient"
91  << exit(FatalError);
92  }
93 }
94 
95 
96 template<class Type>
98 (
99  const word& name,
100  const unitConversions& units,
101  Istream& is
102 )
103 :
104  FieldFunction1<Type, Polynomial<Type>>(name),
105  coeffs_(convertRead(units, is))
106 {
107  if (!coeffs_.size())
108  {
110  << typeName.capitalise() << ' ' << name
111  << " must have at least one coefficient"
112  << exit(FatalError);
113  }
114 }
115 
116 
117 template<class Type>
119 :
120  FieldFunction1<Type, Polynomial<Type>>(poly),
121  coeffs_(poly.coeffs_)
122 {}
123 
124 
125 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
126 
127 template<class Type>
129 {}
130 
131 
132 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
133 
134 template<class Type>
136 {
137  Type y = coeffs_[coeffs_.size() - 1];
138 
139  for (label i = coeffs_.size() - 2; i >= 0; i --)
140  {
141  y = y*x + coeffs_[i];
142  }
143 
144  return y;
145 }
146 
147 
148 template<class Type>
150 (
151  const scalar x1,
152  const scalar x2
153 ) const
154 {
155  Type Sy1 = coeffs_[coeffs_.size() - 1]/coeffs_.size(), Sy2 = Sy1;
156 
157  for (label i = coeffs_.size() - 2; i >= 0; i --)
158  {
159  Sy1 = Sy1*x1 + coeffs_[i]/(i + 1);
160  Sy2 = Sy2*x2 + coeffs_[i]/(i + 1);
161  }
162 
163  return x2*Sy2 - x1*Sy1;
164 }
165 
166 
167 template<class Type>
169 (
170  Ostream& os,
171  const unitConversions& units
172 ) const
173 {
174  writeEntry(os, "coeffs", convertWrite(units, coeffs_));
175 }
176 
177 
178 // ************************************************************************* //
scalar y
bool found
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
const word & name() const
Return the name of the entry.
Definition: Function1.C:78
Arbitrary order polynomial Function1.
Definition: Polynomial1.H:73
virtual Type integral(const scalar x1, const scalar x2) const
Integrate between two scalar fields.
Definition: Polynomial1.C:150
Polynomial(const word &name, const unitConversions &units, const dictionary &dict)
Construct from name and dictionary.
Definition: Polynomial1.C:68
virtual void write(Ostream &os, const unitConversions &units) const
Write in dictionary format.
Definition: Polynomial1.C:169
virtual ~Polynomial()
Destructor.
Definition: Polynomial1.C:128
virtual Type value(const scalar x) const
Return Polynomial value as a function of scalar x.
Definition: Polynomial1.C:135
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
A class for handling words, derived from string.
Definition: word.H:62
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
void writeEntry(Ostream &os, const omega &a)
Definition: omega1.C:97
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
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:59
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
const HashTable< unitConversion > & units()
Get the table of unit conversions.
error FatalError
dictionary dict