UniformTable2.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) 2020-2024 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 "UniformTable2.H"
27 
28 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
29 
30 template<class Type>
32 (
33  const word& name,
34  const unitConversions& units,
35  const dictionary& dict
36 )
37 :
38  FieldFunction2<Type, UniformTable<Type>>(name),
39  low_(dict.lookup("low")),
40  high_(dict.lookup("high")),
41  values_(dict.lookup("values"))
42 {
43  assertNoConvertUnits(typeName, units, dict);
44 
45  if (values_.m() < 2 || values_.n() < 2)
46  {
48  << "Table " << nl
49  << " " << this->name_ << nl
50  << " has less than 2 entries in one or both dimensions."
51  << exit(FatalError);
52  }
53  else
54  {
55  deltax_ = (high_.first() - low_.first())/(values_.m() - 1);
56  deltay_ = (high_.second() - low_.second())/(values_.n() - 1);
57  }
58 }
59 
60 
61 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
62 
63 template<class Type>
65 (
66  scalar x,
67  scalar ndx,
68  label ix,
69  scalar y,
70  scalar ndy,
71  label iy
72 ) const
73 {
74  if (ndx < 0 || ix > values_.m() - 2)
75  {
77  << "x " << x << " out of range "
78  << low_.first() << " to " << high_.first() << nl
79  << " of table " << this->name_
80  << exit(FatalError);
81  }
82 
83  if (ndy < 0 || iy > values_.n() - 2)
84  {
86  << "y " << y << " out of range "
87  << low_.second() << " to " << high_.second() << nl
88  << " of table " << this->name_
89  << exit(FatalError);
90  }
91 }
92 
93 
94 template<class Type>
96 (
97  scalar x,
98  scalar y
99 ) const
100 {
101  const scalar ndx = (x - low_.first())/deltax_;
102  const label ix = ndx;
103 
104  const scalar ndy = (y - low_.second())/deltay_;
105  const label iy = ndy;
106 
107  checkRange(x, ndx, ix, y, ndy, iy);
108 
109  const scalar xi = low_.first() + ix*deltax_;
110  const scalar lambdax = (x - xi)/deltax_;
111 
112  // Interpolate the values at yi wrt x
113  const Type fxi =
114  values_(ix, iy)
115  + lambdax*(values_(ix + 1, iy) - values_(ix, iy));
116 
117  // Interpolate the values at yi+1 wrt x
118  const Type fxix1 =
119  values_(ix, iy + 1)
120  + lambdax*(values_(ix + 1, iy + 1) - values_(ix, iy + 1));
121 
122  const scalar yi = low_.second() + iy*deltay_;
123  const scalar lambday = (y - yi)/deltay_;
124 
125  // Interpolate wrt y
126  return fxi + lambday*(fxix1 - fxi);
127 }
128 
129 
130 template<class Type>
132 (
133  scalar p,
134  scalar T
135 ) const
136 {
137  const scalar ndp = (p - low_.first())/deltax_;
138  const label ip = ndp;
139 
140  const scalar ndT = (T - low_.second())/deltay_;
141  const label iT = ndT;
142 
143  checkRange(p, ndp, ip, T, ndT, iT);
144 
145  const Type dfdpi =
146  (values_(ip + 1, iT) - values_(ip, iT))/deltax_;
147  const Type dfdpip1 =
148  (values_(ip + 1, iT + 1) - values_(ip, iT + 1))/deltax_;
149 
150  const scalar Ti = low_.second() + iT*deltay_;
151  const scalar lambdaT = (T - Ti)/deltay_;
152 
153  // Interpolate wrt T
154  return dfdpi + lambdaT*(dfdpip1 - dfdpi);
155 }
156 
157 
158 template<class Type>
160 (
161  scalar p,
162  scalar T
163 ) const
164 {
165  const scalar ndp = (p - low_.first())/deltax_;
166  const label ip = ndp;
167 
168  const scalar ndT = (T - low_.second())/deltay_;
169  const label iT = ndT;
170 
171  checkRange(p, ndp, ip, T, ndT, iT);
172 
173  const Type dfdTi =
174  (values_(ip, iT + 1) - values_(ip, iT))/deltay_;
175  const Type dfdTip1 =
176  (values_(ip + 1, iT + 1) - values_(ip + 1, iT))/deltay_;
177 
178  const scalar pi = low_.first() + ip*deltax_;
179  const scalar lambdap = (p - pi)/deltax_;
180 
181  // Interpolate wrt p
182  return dfdTi + lambdap*(dfdTip1 - dfdTi);
183 }
184 
185 
186 template<class Type>
188 (
189  Ostream& os,
190  const unitConversions& units
191 ) const
192 {
193  writeEntry(os, "low", low_);
194  writeEntry(os, "high", high_);
195  writeEntry(os, "values", values_);
196 }
197 
198 
199 // ************************************************************************* //
scalar y
const word name_
Name of entry.
Definition: Function2.H:105
Tabulated property function that linearly interpolates between the uniformTable values.
Definition: UniformTable2.H:93
UniformTable(const word &name, const unitConversions &units, const dictionary &dict)
Construct from name and dictionary.
Definition: UniformTable2.C:32
virtual Type value(scalar x, scalar y) const
Evaluate the function and return the result.
Definition: UniformTable2.C:96
void write(Ostream &os, const unitConversions &units) const
Write the function coefficients.
Type dfdp(scalar p, scalar T) const
Evaluate the derivative of the function w.r.t. p.
Type dfdT(scalar p, scalar T) const
Evaluate the derivative of the function w.r.t. T.
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
const Type & second() const
Return second.
Definition: Pair.H:110
const Type & first() const
Return first.
Definition: Pair.H:98
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
const HashTable< unitConversion > & units()
Get the table of unit conversions.
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
error FatalError
void assertNoConvertUnits(const word &typeName, const Function1s::unitConversions &units, const dictionary &dict)
Generate an error in an context where unit conversions are not supported.
static const char nl
Definition: Ostream.H:266
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
dictionary dict
volScalarField & p