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-2020 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 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
29 
30 template<class Type>
32 (
33  const word& name,
34  const dictionary& dict
35 )
36 :
38  coeffs_(),
39  canIntegrate_(true)
40 {
41  if (!dict.found(name))
42  {
43  dict.lookup("coeffs") >> coeffs_;
44  }
45  else
46  {
47  Istream& is(dict.lookup(name));
48  word entryType(is);
49 
50  if (is.eof())
51  {
52  dict.lookup("coeffs") >> coeffs_;
53  }
54  else
55  {
56  is >> coeffs_;
57  }
58  }
59 
60  if (!coeffs_.size())
61  {
63  << "Polynomial coefficients for entry " << this->name_
64  << " are invalid (empty)" << nl << exit(FatalError);
65  }
66 
67  forAll(coeffs_, i)
68  {
69  if (mag(coeffs_[i].second() + pTraits<Type>::one) < rootVSmall)
70  {
71  canIntegrate_ = false;
72  break;
73  }
74  }
75 
76  if (debug)
77  {
78  if (!canIntegrate_)
79  {
81  << "Polynomial " << this->name_ << " cannot be integrald"
82  << endl;
83  }
84  }
85 }
86 
87 
88 template<class Type>
90 (
91  const word& name,
92  const List<Tuple2<Type, Type>>& coeffs
93 )
94 :
96  coeffs_(coeffs),
97  canIntegrate_(true)
98 {
99  if (!coeffs_.size())
100  {
102  << "Polynomial coefficients for entry " << this->name_
103  << " are invalid (empty)" << nl << exit(FatalError);
104  }
105 
106  forAll(coeffs_, i)
107  {
108  if (mag(coeffs_[i].second() + 1) < rootVSmall)
109  {
110  canIntegrate_ = false;
111  break;
112  }
113  }
114 
115  if (debug)
116  {
117  if (!canIntegrate_)
118  {
120  << "Polynomial " << this->name_ << " cannot be integrald"
121  << endl;
122  }
123  }
124 }
125 
126 
127 template<class Type>
129 :
130  FieldFunction1<Type, Polynomial<Type>>(poly),
131  coeffs_(poly.coeffs_),
132  canIntegrate_(poly.canIntegrate_)
133 {}
134 
135 
136 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
137 
138 template<class Type>
140 {}
141 
142 
143 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
144 
145 template<class Type>
147 {
148  Type y(Zero);
149  forAll(coeffs_, i)
150  {
151  y += cmptMultiply
152  (
153  coeffs_[i].first(),
154  cmptPow(pTraits<Type>::one*x, coeffs_[i].second())
155  );
156  }
157 
158  return y;
159 }
160 
161 
162 template<class Type>
164 (
165  const scalar x1,
166  const scalar x2
167 ) const
168 {
169  Type intx(Zero);
170 
171  if (canIntegrate_)
172  {
173  forAll(coeffs_, i)
174  {
175  intx += cmptMultiply
176  (
177  cmptDivide
178  (
179  coeffs_[i].first(),
180  coeffs_[i].second() + pTraits<Type>::one
181  ),
182  cmptPow
183  (
185  coeffs_[i].second() + pTraits<Type>::one
186  )
187  - cmptPow
188  (
190  coeffs_[i].second() + pTraits<Type>::one
191  )
192  );
193  }
194  }
195 
196  return intx;
197 }
198 
199 
200 template<class Type>
202 {
203  writeKeyword(os, "coeffs") << coeffs_ << token::END_STATEMENT << nl;
204 }
205 
206 
207 // ************************************************************************* //
labelList first(const UList< labelPair > &p)
Definition: patchToPatch.C:38
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:663
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
Arbitrary order polynomial Function1.
Definition: Polynomial1.H:63
Ostream & writeKeyword(Foam::Ostream &os, const keyType &kw)
Write the keyword to the Ostream with the current level of indentation.
Definition: keyType.C:155
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:156
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
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
dimensioned< Type > cmptDivide(const dimensioned< Type > &, const dimensioned< Type > &)
scalar y
Polynomial(const word &name, const dictionary &dict)
Construct from name and dictionary.
Definition: Polynomial1.C:32
virtual ~Polynomial()
Destructor.
Definition: Polynomial1.C:139
virtual Type integral(const scalar x1, const scalar x2) const
Integrate between two scalar fields.
Definition: Polynomial1.C:164
A class for handling words, derived from string.
Definition: word.H:59
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
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
labelList second(const UList< labelPair > &p)
Definition: patchToPatch.C:48
#define WarningInFunction
Report a warning using Foam::Warning.
virtual void write(Ostream &os) const
Write in dictionary format.
Definition: Polynomial1.C:201
dimensioned< scalar > mag(const dimensioned< Type > &)
virtual Type value(const scalar x) const
Return Polynomial value as a function of scalar x.
Definition: Polynomial1.C:146
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:864