PolynomialEntry.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 | Copyright (C) 2011-2015 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  DataEntry<Type>(entryName),
38  coeffs_(),
39  canIntegrate_(true),
40  dimensions_(dimless)
41 {
42  Istream& is(dict.lookup(entryName));
43  word entryType(is);
44 
45  token firstToken(is);
46  is.putBack(firstToken);
47  if (firstToken == token::BEGIN_SQR)
48  {
49  is >> this->dimensions_;
50  }
51 
52  is >> coeffs_;
53 
54  if (!coeffs_.size())
55  {
57  (
58  "PolynomialEntry<Type>::"
59  "PolynomialEntry(const word&, const dictionary&)"
60  ) << "PolynomialEntry coefficients for entry " << this->name_
61  << " are invalid (empty)" << nl << exit(FatalError);
62  }
63 
64  forAll(coeffs_, i)
65  {
66  if (mag(coeffs_[i].second() + pTraits<Type>::one) < ROOTVSMALL)
67  {
68  canIntegrate_ = false;
69  break;
70  }
71  }
72 
73  if (debug)
74  {
75  if (!canIntegrate_)
76  {
77  WarningIn
78  (
79  "PolynomialEntry<Type>::PolynomialEntry"
80  "(const word&, const dictionary&)"
81  ) << "PolynomialEntry " << this->name_ << " cannot be integrated"
82  << endl;
83  }
84  }
85 }
86 
87 
88 template<class Type>
90 (
91  const word& entryName,
92  const List<Tuple2<Type, Type> >& coeffs
93 )
94 :
95  DataEntry<Type>(entryName),
96  coeffs_(coeffs),
97  canIntegrate_(true),
98  dimensions_(dimless)
99 {
100  if (!coeffs_.size())
101  {
103  (
104  "Foam::PolynomialEntry<Type>::PolynomialEntry"
105  "(const word&, const List<Tuple2<Type, Type> >&)"
106  ) << "PolynomialEntry coefficients for entry " << this->name_
107  << " are invalid (empty)" << nl << exit(FatalError);
108  }
109 
110  forAll(coeffs_, i)
111  {
112  if (mag(coeffs_[i].second() + 1) < ROOTVSMALL)
113  {
114  canIntegrate_ = false;
115  break;
116  }
117  }
118 
119  if (debug)
120  {
121  if (!canIntegrate_)
122  {
123  WarningIn
124  (
125  "Foam::PolynomialEntry<Type>::PolynomialEntry"
126  "(const word&, const List<Tuple2<Type, Type> >&)"
127  ) << "PolynomialEntry " << this->name_ << " cannot be integrated"
128  << endl;
129  }
130  }
131 }
132 
133 
134 template<class Type>
136 :
137  DataEntry<Type>(poly),
138  coeffs_(poly.coeffs_),
139  canIntegrate_(poly.canIntegrate_),
140  dimensions_(poly.dimensions_)
141 {}
142 
143 
144 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
145 
146 template<class Type>
148 {}
149 
150 
151 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
152 
153 template<class Type>
155 {
156  forAll(coeffs_, i)
157  {
158  Type value = coeffs_[i].first();
159  for (direction cmpt = 0; cmpt < pTraits<Type>::nComponents; cmpt++)
160  {
161  setComponent(coeffs_[i].first(), cmpt) =
162  t.userTimeToTime(component(value, cmpt));
163  }
164  }
165 }
166 
167 
168 template<class Type>
169 Type Foam::PolynomialEntry<Type>::value(const scalar x) const
170 {
171  Type y(pTraits<Type>::zero);
172  forAll(coeffs_, i)
173  {
174  y += cmptMultiply
175  (
176  coeffs_[i].first(),
177  cmptPow(pTraits<Type>::one*x, coeffs_[i].second())
178  );
179  }
180 
181  return y;
182 }
183 
184 
185 template<class Type>
187 (
188  const scalar x1,
189  const scalar x2
190 ) const
191 {
192  Type intx(pTraits<Type>::zero);
193 
194  if (canIntegrate_)
195  {
196  forAll(coeffs_, i)
197  {
198  intx += cmptMultiply
199  (
200  cmptDivide
201  (
202  coeffs_[i].first(),
203  coeffs_[i].second() + pTraits<Type>::one
204  ),
205  cmptPow
206  (
208  coeffs_[i].second() + pTraits<Type>::one
209  )
210  - cmptPow
211  (
213  coeffs_[i].second() + pTraits<Type>::one
214  )
215  );
216  }
217  }
218 
219  return intx;
220 }
221 
222 
223 template<class Type>
225 (
226  const scalar x
227 ) const
228 {
229  return dimensioned<Type>("dimensionedValue", dimensions_, value(x));
230 }
231 
232 
233 template<class Type>
235 (
236  const scalar x1,
237  const scalar x2
238 ) const
239 {
240  return dimensioned<Type>
241  (
242  "dimensionedValue",
243  dimensions_,
244  integrate(x1, x2)
245  );
246 }
247 
248 
249 // * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * * * //
250 
251 #include "PolynomialEntryIO.C"
252 
253 
254 // ************************************************************************* //
unsigned char direction
Definition: direction.H:43
A 2-tuple for storing two objects of different types.
Definition: Tuple2.H:47
Scalar cmptPow(const Scalar s1, const Scalar s2)
Definition: Scalar.H:246
dimensioned< scalar > mag(const dimensioned< Type > &)
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
label & setComponent(label &l, const direction)
Definition: label.H:79
Type integrate(const scalar x1, const scalar x2) const
Integrate between two (scalar) values.
dimensioned< Type > dimValue(const scalar) const
Return dimensioned constant value.
A class for handling words, derived from string.
Definition: word.H:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
virtual scalar userTimeToTime(const scalar theta) const
Convert the user-time (e.g. CA deg) to real-time (s).
Definition: TimeState.C:55
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
PolynomialEntry(const word &entryName, const dictionary &dict)
Generic dimensioned Type class.
static const char nl
Definition: Ostream.H:260
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
#define WarningIn(functionName)
Report a warning using Foam::Warning.
virtual ~PolynomialEntry()
Destructor.
dimensioned< Type > cmptDivide(const dimensioned< Type > &, const dimensioned< Type > &)
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
#define forAll(list, i)
Definition: UList.H:421
Type value(const scalar x) const
Return PolynomialEntry value.
A token holds items read from Istream.
Definition: token.H:67
virtual void convertTimeBase(const Time &t)
Convert time.
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
dimensioned< Type > dimIntegrate(const scalar x1, const scalar x2) const
Integrate between two values and return 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.
Traits class for primitives.
Definition: pTraits.H:50
dimensioned< Type > cmptMultiply(const dimensioned< Type > &, const dimensioned< Type > &)
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:452
error FatalError
scalar y
const dimensionSet dimless(0, 0, 0, 0, 0, 0, 0)
Definition: dimensionSets.H:47
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: DataEntry.H:52