Loading...
Searching...
No Matches
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>
34void Foam::Function1Types::Polynomial<Type>::checkCoefficients()
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) =
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 );
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 }
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
Polynomial(const word &entryName, const dictionary &dict, const objectRegistry *obrPtr=nullptr)
Construct from entry name, dictionary and optional registry.
virtual void writeData(Ostream &os) const
Write as primitive (inline) format.
virtual Type value(const scalar x) const
Return Polynomial value.
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:92
virtual void writeData(Ostream &os) const
Write in dictionary format.
Definition Function1.C:156
Function1(const word &entryName, const objectRegistry *obrPtr=nullptr)
Construct from entry name.
Definition Function1.C:31
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
virtual scalar userTimeToTime(const scalar theta) const
Convert the user-time (e.g. CA deg) to real-time (s).
Definition TimeState.C:42
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition Time.H:75
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition Tuple2.H:51
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
A keyword and a list of tokens is an 'entry'.
Definition entry.H:66
virtual bool isStream() const noexcept
True if this entry is a stream.
Definition entry.H:267
@ LITERAL
String literal.
Definition keyType.H:82
Registry of regIOobjects.
A traits class, which is primarily used for primitives and vector-space.
Definition pTraits.H:64
A class for handling words, derived from Foam::string.
Definition word.H:66
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
OBJstream os(runTime.globalPath()/outputName)
auto & name
#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 scalarImpl.H:456
label & setComponent(label &val, const direction) noexcept
Non-const access to integer-type (has no components).
Definition label.H:160
dimensioned< Type > cmptDivide(const dimensioned< Type > &, const dimensioned< Type > &)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
Ostream & indent(Ostream &os)
Indent stream.
Definition Ostream.H:481
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
uint8_t direction
Definition direction.H:49
static constexpr const zero Zero
Global zero (0).
Definition zero.H:127
dimensioned< Type > cmptMultiply(const dimensioned< Type > &, const dimensioned< Type > &)
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299