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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2020-2021 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "PolynomialEntry.H"
30 
31 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
32 
33 template<class Type>
35 {
36  if (coeffs_.empty())
37  {
39  << "Invalid (empty) polynomial coefficients for "
40  << this->name() << nl
41  << exit(FatalError);
42  }
43 
44  for (const auto& coeff : coeffs_)
45  {
46  if (mag(coeff.second() + pTraits<Type>::one) < ROOTVSMALL)
47  {
48  canIntegrate_ = false;
49  break;
50  }
51  }
52 
53  if (debug && !canIntegrate_)
54  {
56  << "Polynomial " << this->name() << " cannot be integrated"
57  << endl;
58  }
59 }
60 
61 
62 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
63 
64 template<class Type>
66 (
67  const word& entryName,
68  const dictionary& dict,
69  const objectRegistry* obrPtr
70 )
71 :
72  Function1<Type>(entryName, dict, obrPtr),
73  coeffs_(),
74  canIntegrate_(true)
75 {
76  const entry* eptr = dict.findEntry(entryName, keyType::LITERAL);
77 
78  if (eptr && eptr->isStream())
79  {
80  // Primitive (inline) format. Eg,
81  // key polynomial ((0 0) (10 1));
82 
83  ITstream& is = eptr->stream();
84  if (is.peek().isWord())
85  {
86  is.skip(); // Discard leading 'polynomial'
87  }
88  is >> this->coeffs_;
89  dict.checkITstream(is, entryName);
90  }
91  else
92  {
93  // Dictionary format - "coeffs" lookup. Eg,
94  //
95  // key { type polynomial; coeffs ((0 0) (10 1)); }
96 
97  dict.readEntry("coeffs", this->coeffs_);
98  }
99 
100  this->checkCoefficients();
101 }
102 
103 
104 template<class Type>
106 (
107  const word& entryName,
108  const List<Tuple2<Type, Type>>& coeffs,
109  const objectRegistry* obrPtr
110 )
111 :
112  Function1<Type>(entryName, obrPtr),
113  coeffs_(coeffs),
114  canIntegrate_(true)
115 {
116  this->checkCoefficients();
117 }
118 
119 
120 template<class Type>
122 :
123  Function1<Type>(poly),
124  coeffs_(poly.coeffs_),
125  canIntegrate_(poly.canIntegrate_)
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_;
207  os.endEntry();
208 }
209 
210 
211 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
Foam::setComponent
label & setComponent(label &l, const direction)
Definition: label.H:123
Foam::component
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
Definition: FieldFieldFunctions.C:44
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::Function1Types::Polynomial::Polynomial
Polynomial(const word &entryName, const dictionary &dict, const objectRegistry *obrPtr=nullptr)
Construct from entry name, dictionary and optional registry.
Definition: PolynomialEntry.C:66
Foam::cmptPow
Scalar cmptPow(const Scalar s1, const Scalar s2)
Definition: Scalar.H:364
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::Function1Types::Polynomial::writeData
virtual void writeData(Ostream &os) const
Write as primitive (inline) format.
Definition: PolynomialEntry.C:202
Foam::entry::stream
virtual ITstream & stream() const =0
Return token stream, if entry is a primitive entry.
Foam::cmptMultiply
dimensioned< Type > cmptMultiply(const dimensioned< Type > &, const dimensioned< Type > &)
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::Function1Types::Polynomial::integrate
virtual Type integrate(const scalar x1, const scalar x2) const
Integrate between two (scalar) values.
Definition: PolynomialEntry.C:165
Foam::entry::isStream
virtual bool isStream() const noexcept
Return true if this entry is a stream.
Definition: entry.H:223
Foam::TimeState::userTimeToTime
virtual scalar userTimeToTime(const scalar theta) const
Convert the user-time (e.g. CA deg) to real-time (s).
Definition: TimeState.C:49
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::Function1
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: propellerInfo.H:291
Foam::token::isWord
bool isWord() const noexcept
Token is word-variant (WORD, DIRECTIVE)
Definition: tokenI.H:609
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::ITstream
An input stream of tokens.
Definition: ITstream.H:52
Foam::Function1::writeData
virtual void writeData(Ostream &os) const
Write in dictionary format.
Definition: Function1.C:174
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
PolynomialEntry.H
Foam::indent
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:339
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::ITstream::skip
void skip(label n=1)
Move tokenIndex relative to the current position.
Definition: ITstream.C:411
Foam::cmptDivide
dimensioned< Type > cmptDivide(const dimensioned< Type > &, const dimensioned< Type > &)
Foam::Function1Types::Polynomial
Definition: PolynomialEntry.H:80
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::pTraits
A traits class, which is primarily used for primitives.
Definition: pTraits.H:56
Foam::Function1Types::Polynomial::value
virtual Type value(const scalar x) const
Return Polynomial value.
Definition: PolynomialEntry.C:147
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::direction
uint8_t direction
Definition: direction.H:52
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::Tuple2
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition: stringOps.H:60
Foam::ITstream::peek
const token & peek() const
Failsafe peek at what the next read would return,.
Definition: ITstream.C:352
Foam::Function1Types::Polynomial::userTimeToTime
virtual void userTimeToTime(const Time &t)
Convert time.
Definition: PolynomialEntry.C:132
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:328
y
scalar y
Definition: LISASMDCalcMethod1.H:14