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-------------------------------------------------------------------------------
11License
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
33template<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
64template<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
104template<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
120template<class Type>
122:
123 Function1<Type>(poly),
124 coeffs_(poly.coeffs_),
125 canIntegrate_(poly.canIntegrate_)
126{}
127
128
129// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
130
131template<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
146template<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
163template<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 (
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
201template<class Type>
203{
205
206 os << nl << indent << coeffs_;
207 os.endEntry();
208}
209
210
211// ************************************************************************* //
scalar y
virtual Type integrate(const scalar x1, const scalar x2) const
Integrate between two (scalar) values.
virtual void userTimeToTime(const Time &t)
Convert time.
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: Function1.H:96
const word const dictionary & dict
Definition: Function1.H:134
const word & entryName
Definition: Function1.H:133
An input stream of tokens.
Definition: ITstream.H:56
const token & peek() const
Failsafe peek at what the next read would return,.
Definition: ITstream.C:352
void skip(label n=1)
Move tokenIndex relative to the current position.
Definition: ITstream.C:411
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
virtual scalar userTimeToTime(const scalar theta) const
Convert the user-time (e.g. CA deg) to real-time (s).
Definition: TimeState.C:49
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:80
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition: Tuple2.H:58
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:70
virtual ITstream & stream() const =0
Return token stream, if entry is a primitive entry.
virtual bool isStream() const noexcept
Return true if this entry is a stream.
Definition: entry.H:223
@ LITERAL
String literal.
Definition: keyType.H:81
Registry of regIOobjects.
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition: one.H:62
bool isWord() const noexcept
Token is word-variant (WORD, DIRECTIVE)
Definition: tokenI.H:609
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
OBJstream os(runTime.globalPath()/outputName)
#define WarningInFunction
Report a warning using Foam::Warning.
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
Scalar cmptPow(const Scalar s1, const Scalar s2)
Definition: Scalar.H:369
label & setComponent(label &val, const direction) noexcept
Non-const access to integer-type (has no components)
Definition: label.H:123
dimensioned< Type > cmptDivide(const dimensioned< Type > &, const dimensioned< Type > &)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:342
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
uint8_t direction
Definition: direction.H:56
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
dimensioned< Type > cmptMultiply(const dimensioned< Type > &, const dimensioned< Type > &)
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333