My Project
Loading...
Searching...
No Matches
UniformTabulated2DFunction.hpp
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3/*
4 This file is part of the Open Porous Media project (OPM).
5
6 OPM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
10
11 OPM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with OPM. If not, see <http://www.gnu.org/licenses/>.
18
19 Consult the COPYING file in the top-level source directory of this
20 module for the precise wording of the license and the list of
21 copyright holders.
22*/
28#ifndef OPM_UNIFORM_TABULATED_2D_FUNCTION_HPP
29#define OPM_UNIFORM_TABULATED_2D_FUNCTION_HPP
30
31#include <opm/common/OpmLog/OpmLog.hpp>
33
35
36#include <vector>
37
38#include <assert.h>
39
40namespace Opm {
41
49template <class Scalar>
51{
52public:
54 { }
55
60 UniformTabulated2DFunction(Scalar minX, Scalar maxX, unsigned m,
61 Scalar minY, Scalar maxY, unsigned n)
62 {
63 resize(minX, maxX, m, minY, maxY, n);
64 }
65
66 UniformTabulated2DFunction(Scalar minX, Scalar maxX, unsigned m,
67 Scalar minY, Scalar maxY, unsigned n,
68 const std::vector<std::vector<Scalar>>& vals)
69 {
70 resize(minX, maxX, m, minY, maxY, n);
71
72 for (unsigned i = 0; i < m; ++i)
73 for (unsigned j = 0; j < n; ++j)
74 this->setSamplePoint(i, j, vals[i][j]);
75 }
76
80 void resize(Scalar minX, Scalar maxX, unsigned m,
81 Scalar minY, Scalar maxY, unsigned n)
82 {
83 samples_.resize(m*n);
84
85 m_ = m;
86 n_ = n;
87
88 xMin_ = minX;
89 xMax_ = maxX;
90
91 yMin_ = minY;
92 yMax_ = maxY;
93 }
94
98 Scalar xMin() const
99 { return xMin_; }
100
104 Scalar xMax() const
105 { return xMax_; }
106
110 Scalar yMin() const
111 { return yMin_; }
112
116 Scalar yMax() const
117 { return yMax_; }
118
122 unsigned numX() const
123 { return m_; }
124
128 unsigned numY() const
129 { return n_; }
130
134 Scalar iToX(unsigned i) const
135 {
136 assert(i < numX());
137
138 return xMin() + i*(xMax() - xMin())/(numX() - 1);
139 }
140
144 Scalar jToY(unsigned j) const
145 {
146 assert(j < numY());
147
148 return yMin() + j*(yMax() - yMin())/(numY() - 1);
149 }
150
159 template <class Evaluation>
160 Evaluation xToI(const Evaluation& x) const
161 { return (x - xMin())/(xMax() - xMin())*(numX() - 1); }
162
171 template <class Evaluation>
172 Evaluation yToJ(const Evaluation& y) const
173 { return (y - yMin())/(yMax() - yMin())*(numY() - 1); }
174
178 template <class Evaluation>
179 bool applies(const Evaluation& x, const Evaluation& y) const
180 {
181 return
182 xMin() <= x && x <= xMax() &&
183 yMin() <= y && y <= yMax();
184 }
185
194 template <class Evaluation>
195 Evaluation eval(const Evaluation& x, const Evaluation& y, bool extrapolate) const
196 {
197 if (!applies(x,y))
198 {
199 std::string msg = "Attempt to get tabulated value for ("
200 +std::to_string(double(scalarValue(x)))+", "+std::to_string(double(scalarValue(y)))
201 +") on a table of extent "
202 +std::to_string(xMin())+" to "+std::to_string(xMax())+" times "
203 +std::to_string(yMin())+" to "+std::to_string(yMax());
204
205 if (!extrapolate)
206 {
207 throw NumericalProblem(msg);
208 }
209 else
210 {
211 OpmLog::warning("PVT Table evaluation:" + msg + ". Will use extrapolation");
212 }
213
214 };
215
216 Evaluation alpha = xToI(x);
217 Evaluation beta = yToJ(y);
218
219 unsigned i =
220 static_cast<unsigned>(
221 std::max(0, std::min(static_cast<int>(numX()) - 2,
222 static_cast<int>(scalarValue(alpha)))));
223 unsigned j =
224 static_cast<unsigned>(
225 std::max(0, std::min(static_cast<int>(numY()) - 2,
226 static_cast<int>(scalarValue(beta)))));
227
228 alpha -= i;
229 beta -= j;
230
231 // bi-linear interpolation
232 const Evaluation& s1 = getSamplePoint(i, j)*(1.0 - alpha) + getSamplePoint(i + 1, j)*alpha;
233 const Evaluation& s2 = getSamplePoint(i, j + 1)*(1.0 - alpha) + getSamplePoint(i + 1, j + 1)*alpha;
234 return s1*(1.0 - beta) + s2*beta;
235 }
236
242 Scalar getSamplePoint(unsigned i, unsigned j) const
243 {
244 assert(i < m_);
245 assert(j < n_);
246
247 return samples_[j*m_ + i];
248 }
249
255 void setSamplePoint(unsigned i, unsigned j, Scalar value)
256 {
257 assert(i < m_);
258 assert(j < n_);
259
260 samples_[j*m_ + i] = value;
261 }
262
263 bool operator==(const UniformTabulated2DFunction<Scalar>& data) const
264 {
265 return samples_ == data.samples_ &&
266 m_ == data.m_ &&
267 n_ == data.n_ &&
268 xMin_ == data.xMin_ &&
269 xMax_ == data.xMax_ &&
270 yMin_ == data.yMin_ &&
271 yMax_ == data.yMax_;
272 }
273
274
275private:
276 // the vector which contains the values of the sample points
277 // f(x_i, y_j). don't use this directly, use getSamplePoint(i,j)
278 // instead!
279 std::vector<Scalar> samples_;
280
281 // the number of sample points in x direction
282 unsigned m_;
283
284 // the number of sample points in y direction
285 unsigned n_;
286
287 // the range of the tabulation on the x axis
288 Scalar xMin_;
289 Scalar xMax_;
290
291 // the range of the tabulation on the y axis
292 Scalar yMin_;
293 Scalar yMax_;
294};
295
296} // namespace Opm
297
298#endif
Provides the OPM specific exception classes.
A traits class which provides basic mathematical functions for arbitrary scalar floating point values...
Definition Exceptions.hpp:40
Implements a scalar function that depends on two variables and which is sampled on an uniform X-Y gri...
Definition UniformTabulated2DFunction.hpp:51
void resize(Scalar minX, Scalar maxX, unsigned m, Scalar minY, Scalar maxY, unsigned n)
Resize the tabulation to a new range.
Definition UniformTabulated2DFunction.hpp:80
Scalar iToX(unsigned i) const
Return the position on the x-axis of the i-th interval.
Definition UniformTabulated2DFunction.hpp:134
Scalar yMax() const
Returns the maximum of the Y coordinate of the sampling points.
Definition UniformTabulated2DFunction.hpp:116
Evaluation xToI(const Evaluation &x) const
Return the interval index of a given position on the x-axis.
Definition UniformTabulated2DFunction.hpp:160
UniformTabulated2DFunction(Scalar minX, Scalar maxX, unsigned m, Scalar minY, Scalar maxY, unsigned n)
Constructor where the tabulation parameters are already provided.
Definition UniformTabulated2DFunction.hpp:60
unsigned numX() const
Returns the number of sampling points in X direction.
Definition UniformTabulated2DFunction.hpp:122
Scalar xMax() const
Returns the maximum of the X coordinate of the sampling points.
Definition UniformTabulated2DFunction.hpp:104
bool applies(const Evaluation &x, const Evaluation &y) const
Returns true iff a coordinate lies in the tabulated range.
Definition UniformTabulated2DFunction.hpp:179
Scalar xMin() const
Returns the minimum of the X coordinate of the sampling points.
Definition UniformTabulated2DFunction.hpp:98
Evaluation yToJ(const Evaluation &y) const
Return the interval index of a given position on the y-axis.
Definition UniformTabulated2DFunction.hpp:172
Evaluation eval(const Evaluation &x, const Evaluation &y, bool extrapolate) const
Evaluate the function at a given (x,y) position.
Definition UniformTabulated2DFunction.hpp:195
void setSamplePoint(unsigned i, unsigned j, Scalar value)
Set the value of the sample point which is at the intersection of the -th interval of the x-Axis and ...
Definition UniformTabulated2DFunction.hpp:255
unsigned numY() const
Returns the number of sampling points in Y direction.
Definition UniformTabulated2DFunction.hpp:128
Scalar getSamplePoint(unsigned i, unsigned j) const
Get the value of the sample point which is at the intersection of the -th interval of the x-Axis and ...
Definition UniformTabulated2DFunction.hpp:242
Scalar yMin() const
Returns the minimum of the Y coordinate of the sampling points.
Definition UniformTabulated2DFunction.hpp:110
Scalar jToY(unsigned j) const
Return the position on the y-axis of the j-th interval.
Definition UniformTabulated2DFunction.hpp:144
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30