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-2019 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 :
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 :
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  FieldFunction1<Type, Polynomial<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  Type y(Zero);
135  forAll(coeffs_, i)
136  {
137  y += cmptMultiply
138  (
139  coeffs_[i].first(),
140  cmptPow(pTraits<Type>::one*x, coeffs_[i].second())
141  );
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 intx(Zero);
156 
157  if (canIntegrate_)
158  {
159  forAll(coeffs_, i)
160  {
161  intx += cmptMultiply
162  (
163  cmptDivide
164  (
165  coeffs_[i].first(),
166  coeffs_[i].second() + pTraits<Type>::one
167  ),
168  cmptPow
169  (
171  coeffs_[i].second() + pTraits<Type>::one
172  )
173  - cmptPow
174  (
176  coeffs_[i].second() + pTraits<Type>::one
177  )
178  );
179  }
180  }
181 
182  return intx;
183 }
184 
185 
186 template<class Type>
188 {
190 
191  os << nl << indent << coeffs_ << token::END_STATEMENT << nl;
192 }
193 
194 
195 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:221
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.
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:158
#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:65
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
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:251
Traits class for primitives.
Definition: pTraits.H:50
virtual void writeData(Ostream &os) const
Write in dictionary format.
dimensioned< Type > cmptDivide(const dimensioned< Type > &, const dimensioned< Type > &)
virtual void writeData(Ostream &os) const
Write in dictionary format.
Definition: Function1.C:98
scalar y
virtual ~Polynomial()
Destructor.
A class for handling words, derived from string.
Definition: word.H:59
virtual Type integrate(const scalar x1, const scalar x2) const
Integrate between two (scalar) values.
static const zero Zero
Definition: zero.H:97
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:54
static const char nl
Definition: Ostream.H:260
Polynomial(const word &entryName, const dictionary &dict)
#define WarningInFunction
Report a warning using Foam::Warning.
dimensioned< scalar > mag(const dimensioned< Type > &)
virtual Type value(const scalar x) const
Return Polynomial value.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:812