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 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 dictionary& dict
35 )
36 :
37  FieldFunction2<Type, UniformTable<Type>>(name),
38  low_(dict.lookup("low")),
39  high_(dict.lookup("high")),
40  values_(dict.lookup("values"))
41 {
42  if (values_.m() < 2 || values_.n() < 2)
43  {
45  << "Table " << nl
46  << " " << this->name_ << nl
47  << " has less than 2 entries in one or both dimensions."
48  << exit(FatalError);
49  }
50  else
51  {
52  deltax_ = (high_.first() - low_.first())/(values_.m() - 1);
53  deltay_ = (high_.second() - low_.second())/(values_.n() - 1);
54  }
55 }
56 
57 
58 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
59 
60 template<class Type>
62 (
63  scalar x,
64  scalar ndx,
65  label ix,
66  scalar y,
67  scalar ndy,
68  label iy
69 ) const
70 {
71  if (ndx < 0 || ix > values_.m() - 2)
72  {
74  << "x " << x << " out of range "
75  << low_.first() << " to " << high_.first() << nl
76  << " of table " << this->name_
77  << exit(FatalError);
78  }
79 
80  if (ndy < 0 || iy > values_.n() - 2)
81  {
83  << "y " << y << " out of range "
84  << low_.second() << " to " << high_.second() << nl
85  << " of table " << this->name_
86  << exit(FatalError);
87  }
88 }
89 
90 
91 template<class Type>
93 (
94  scalar x,
95  scalar y
96 ) const
97 {
98  const scalar ndx = (x - low_.first())/deltax_;
99  const label ix = ndx;
100 
101  const scalar ndy = (y - low_.second())/deltay_;
102  const label iy = ndy;
103 
104  checkRange(x, ndx, ix, y, ndy, iy);
105 
106  const scalar xi = low_.first() + ix*deltax_;
107  const scalar lambdax = (x - xi)/deltax_;
108 
109  // Interpolate the values at yi wrt x
110  const Type fxi =
111  values_(ix, iy)
112  + lambdax*(values_(ix + 1, iy) - values_(ix, iy));
113 
114  // Interpolate the values at yi+1 wrt x
115  const Type fxix1 =
116  values_(ix, iy + 1)
117  + lambdax*(values_(ix + 1, iy + 1) - values_(ix, iy + 1));
118 
119  const scalar yi = low_.second() + iy*deltay_;
120  const scalar lambday = (y - yi)/deltay_;
121 
122  // Interpolate wrt y
123  return fxi + lambday*(fxix1 - fxi);
124 }
125 
126 
127 template<class Type>
129 dfdp
130 (
131  scalar p,
132  scalar T
133 ) const
134 {
135  const scalar ndp = (p - low_.first())/deltax_;
136  const label ip = ndp;
137 
138  const scalar ndT = (T - low_.second())/deltay_;
139  const label iT = ndT;
140 
141  checkRange(p, ndp, ip, T, ndT, iT);
142 
143  const Type dfdpi =
144  (values_(ip + 1, iT) - values_(ip, iT))/deltax_;
145  const Type dfdpip1 =
146  (values_(ip + 1, iT + 1) - values_(ip, iT + 1))/deltax_;
147 
148  const scalar Ti = low_.second() + iT*deltay_;
149  const scalar lambdaT = (T - Ti)/deltay_;
150 
151  // Interpolate wrt T
152  return dfdpi + lambdaT*(dfdpip1 - dfdpi);
153 }
154 
155 
156 template<class Type>
158 dfdT
159 (
160  scalar p,
161  scalar T
162 ) const
163 {
164  const scalar ndp = (p - low_.first())/deltax_;
165  const label ip = ndp;
166 
167  const scalar ndT = (T - low_.second())/deltay_;
168  const label iT = ndT;
169 
170  checkRange(p, ndp, ip, T, ndT, iT);
171 
172  const Type dfdTi =
173  (values_(ip, iT + 1) - values_(ip, iT))/deltay_;
174  const Type dfdTip1 =
175  (values_(ip + 1, iT + 1) - values_(ip + 1, iT))/deltay_;
176 
177  const scalar pi = low_.first() + ip*deltax_;
178  const scalar lambdap = (p - pi)/deltax_;
179 
180  // Interpolate wrt p
181  return dfdTi + lambdap*(dfdTip1 - dfdTi);
182 }
183 
184 
185 template<class Type>
187 {
188  writeEntry(os, "low", low_);
189  writeEntry(os, "high", high_);
190  writeEntry(os, "values", values_);
191 }
192 
193 
194 // ************************************************************************* //
scalar y
const word name_
Name of entry.
Definition: Function2.H:71
Tabulated property function that linearly interpolates between the uniformTable values.
Definition: UniformTable2.H:93
UniformTable(const word &name, 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:93
void write(Ostream &os) 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:160
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
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
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
error FatalError
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
static const char nl
Definition: Ostream.H:260
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
dictionary dict
volScalarField & p