PolynomialEntry.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-2018 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 "PolynomialEntry.H"
27 
28 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
29 
30 template<class Type>
32 (
33  const word& entryName,
34  const dictionary& dict
35 )
36 :
37  Function1<Type>(entryName),
38  coeffs_(),
39  canIntegrate_(true)
40 {
41  Istream& is(dict.lookup(entryName));
42  word entryType(is);
43 
44  is >> coeffs_;
45 
46  if (!coeffs_.size())
47  {
49  << "Polynomial coefficients for entry " << this->name_
50  << " are invalid (empty)" << nl << exit(FatalError);
51  }
52 
53  forAll(coeffs_, i)
54  {
55  if (mag(coeffs_[i].second() + pTraits<Type>::one) < rootVSmall)
56  {
57  canIntegrate_ = false;
58  break;
59  }
60  }
61 
62  if (debug)
63  {
64  if (!canIntegrate_)
65  {
67  << "Polynomial " << this->name_ << " cannot be integrated"
68  << endl;
69  }
70  }
71 }
72 
73 
74 template<class Type>
76 (
77  const word& entryName,
78  const List<Tuple2<Type, Type>>& coeffs
79 )
80 :
81  Function1<Type>(entryName),
82  coeffs_(coeffs),
83  canIntegrate_(true)
84 {
85  if (!coeffs_.size())
86  {
88  << "Polynomial coefficients for entry " << this->name_
89  << " are invalid (empty)" << nl << exit(FatalError);
90  }
91 
92  forAll(coeffs_, i)
93  {
94  if (mag(coeffs_[i].second() + 1) < rootVSmall)
95  {
96  canIntegrate_ = false;
97  break;
98  }
99  }
100 
101  if (debug)
102  {
103  if (!canIntegrate_)
104  {
106  << "Polynomial " << this->name_ << " cannot be integrated"
107  << endl;
108  }
109  }
110 }
111 
112 
113 template<class Type>
115 :
116  Function1<Type>(poly),
117  coeffs_(poly.coeffs_),
118  canIntegrate_(poly.canIntegrate_)
119 {}
120 
121 
122 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
123 
124 template<class Type>
126 {}
127 
128 
129 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
130 
131 template<class Type>
133 {
134  forAll(coeffs_, i)
135  {
136  Type value = coeffs_[i].first();
137  for (direction cmpt = 0; cmpt < pTraits<Type>::nComponents; cmpt++)
138  {
139  setComponent(coeffs_[i].first(), cmpt) =
140  t.userTimeToTime(component(value, cmpt));
141  }
142  }
143 }
144 
145 
146 template<class Type>
148 {
149  Type y(Zero);
150  forAll(coeffs_, i)
151  {
152  y += cmptMultiply
153  (
154  coeffs_[i].first(),
155  cmptPow(pTraits<Type>::one*x, coeffs_[i].second())
156  );
157  }
158 
159  return y;
160 }
161 
162 
163 template<class Type>
165 (
166  const scalar x1,
167  const scalar x2
168 ) const
169 {
170  Type intx(Zero);
171 
172  if (canIntegrate_)
173  {
174  forAll(coeffs_, i)
175  {
176  intx += cmptMultiply
177  (
178  cmptDivide
179  (
180  coeffs_[i].first(),
181  coeffs_[i].second() + pTraits<Type>::one
182  ),
183  cmptPow
184  (
186  coeffs_[i].second() + pTraits<Type>::one
187  )
188  - cmptPow
189  (
191  coeffs_[i].second() + pTraits<Type>::one
192  )
193  );
194  }
195  }
196 
197  return intx;
198 }
199 
200 
201 template<class Type>
203 {
205 
206  os << nl << indent << coeffs_ << token::END_STATEMENT << nl;
207 }
208 
209 
210 // ************************************************************************* //
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:226
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
A 2-tuple for storing two objects of different types.
Definition: HashTable.H:66
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:60
uint8_t direction
Definition: direction.H:45
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
Traits class for primitives.
Definition: pTraits.H:50
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
dimensioned< Type > cmptDivide(const dimensioned< Type > &, const dimensioned< Type > &)
virtual void writeData(Ostream &os) const
Write in dictionary format.
Definition: Function1.C:146
Polynomial(const word &entryName, const dictionary &dict)
scalar y
virtual scalar userTimeToTime(const scalar theta) const
Convert the user-time (e.g. CA deg) to real-time (s).
Definition: TimeState.C:52
A class for handling words, derived from string.
Definition: word.H:59
static const zero Zero
Definition: zero.H:97
virtual Type integrate(const scalar x1, const scalar x2) const
Integrate between two (scalar) values.
Scalar cmptPow(const Scalar s1, const Scalar s2)
Definition: Scalar.H:293
dimensioned< Type > cmptMultiply(const dimensioned< Type > &, const dimensioned< Type > &)
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
static const char nl
Definition: Ostream.H:265
#define WarningInFunction
Report a warning using Foam::Warning.
dimensioned< scalar > mag(const dimensioned< Type > &)
PolynomialEntry container data entry for scalars. Items are stored in a list of Tuple2&#39;s. Data is input in the form, e.g. for an entry <entryName> that describes y = x^2 + 2x^3.
virtual void convertTimeBase(const Time &t)
Convert time.
label & setComponent(label &l, const direction)
Definition: label.H:79
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
virtual ~Polynomial()
Destructor.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:576
virtual void writeData(Ostream &os) const
Write in dictionary format.
virtual Type value(const scalar x) const
Return Polynomial value.