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 <algorithm>
37#include <cassert>
38#include <string>
39#include <vector>
40
41namespace Opm {
42
50template <class Scalar>
52{
53public:
55 { }
56
61 UniformTabulated2DFunction(Scalar minX, Scalar maxX, unsigned m,
62 Scalar minY, Scalar maxY, unsigned n)
63 {
64 resize(minX, maxX, m, minY, maxY, n);
65 }
66
67 UniformTabulated2DFunction(Scalar minX, Scalar maxX, unsigned m,
68 Scalar minY, Scalar maxY, unsigned n,
69 const std::vector<std::vector<Scalar>>& vals)
70 {
71 resize(minX, maxX, m, minY, maxY, n);
72
73 for (unsigned i = 0; i < m; ++i)
74 for (unsigned j = 0; j < n; ++j)
75 this->setSamplePoint(i, j, vals[i][j]);
76 }
77
81 void resize(Scalar minX, Scalar maxX, unsigned m,
82 Scalar minY, Scalar maxY, unsigned n)
83 {
84 samples_.resize(m*n);
85
86 m_ = m;
87 n_ = n;
88
89 xMin_ = minX;
90 xMax_ = maxX;
91
92 yMin_ = minY;
93 yMax_ = maxY;
94 }
95
99 Scalar xMin() const
100 { return xMin_; }
101
105 Scalar xMax() const
106 { return xMax_; }
107
111 Scalar yMin() const
112 { return yMin_; }
113
117 Scalar yMax() const
118 { return yMax_; }
119
123 unsigned numX() const
124 { return m_; }
125
129 unsigned numY() const
130 { return n_; }
131
135 Scalar iToX(unsigned i) const
136 {
137 assert(i < numX());
138
139 return xMin() + i*(xMax() - xMin())/(numX() - 1);
140 }
141
145 Scalar jToY(unsigned j) const
146 {
147 assert(j < numY());
148
149 return yMin() + j*(yMax() - yMin())/(numY() - 1);
150 }
151
160 template <class Evaluation>
161 Evaluation xToI(const Evaluation& x) const
162 { return (x - xMin())/(xMax() - xMin())*(numX() - 1); }
163
172 template <class Evaluation>
173 Evaluation yToJ(const Evaluation& y) const
174 { return (y - yMin())/(yMax() - yMin())*(numY() - 1); }
175
179 template <class Evaluation>
180 bool applies(const Evaluation& x, const Evaluation& y) const
181 {
182 return
183 xMin() <= x && x <= xMax() &&
184 yMin() <= y && y <= yMax();
185 }
186
195 template <class Evaluation>
196 Evaluation eval(const Evaluation& x,
197 const Evaluation& y,
198 [[maybe_unused]] bool extrapolate) const
199 {
200#ifndef NDEBUG
201 if (!extrapolate && !applies(x,y)) {
202 std::string msg = "Attempt to get tabulated value for ("
203 +std::to_string(double(scalarValue(x)))+", "+std::to_string(double(scalarValue(y)))
204 +") on a table of extent "
205 +std::to_string(xMin())+" to "+std::to_string(xMax())+" times "
206 +std::to_string(yMin())+" to "+std::to_string(yMax());
207
208 if (!extrapolate)
209 {
210 throw NumericalProblem(msg);
211 }
212 else
213 {
214 OpmLog::warning("PVT Table evaluation:" + msg + ". Will use extrapolation");
215 }
216
217 }
218#endif
219
220 Evaluation alpha = xToI(x);
221 Evaluation beta = yToJ(y);
222
223 unsigned i =
224 static_cast<unsigned>(
225 std::max(0, std::min(static_cast<int>(numX()) - 2,
226 static_cast<int>(scalarValue(alpha)))));
227 unsigned j =
228 static_cast<unsigned>(
229 std::max(0, std::min(static_cast<int>(numY()) - 2,
230 static_cast<int>(scalarValue(beta)))));
231
232 alpha -= i;
233 beta -= j;
234
235 // bi-linear interpolation
236 const Evaluation& s1 = getSamplePoint(i, j)*(1.0 - alpha) + getSamplePoint(i + 1, j)*alpha;
237 const Evaluation& s2 = getSamplePoint(i, j + 1)*(1.0 - alpha) + getSamplePoint(i + 1, j + 1)*alpha;
238 return s1*(1.0 - beta) + s2*beta;
239 }
240
246 Scalar getSamplePoint(unsigned i, unsigned j) const
247 {
248 assert(i < m_);
249 assert(j < n_);
250
251 return samples_[j*m_ + i];
252 }
253
259 void setSamplePoint(unsigned i, unsigned j, Scalar value)
260 {
261 assert(i < m_);
262 assert(j < n_);
263
264 samples_[j*m_ + i] = value;
265 }
266
267 bool operator==(const UniformTabulated2DFunction<Scalar>& data) const
268 {
269 return samples_ == data.samples_ &&
270 m_ == data.m_ &&
271 n_ == data.n_ &&
272 xMin_ == data.xMin_ &&
273 xMax_ == data.xMax_ &&
274 yMin_ == data.yMin_ &&
275 yMax_ == data.yMax_;
276 }
277
278
279private:
280 // the vector which contains the values of the sample points
281 // f(x_i, y_j). don't use this directly, use getSamplePoint(i,j)
282 // instead!
283 std::vector<Scalar> samples_;
284
285 // the number of sample points in x direction
286 unsigned m_;
287
288 // the number of sample points in y direction
289 unsigned n_;
290
291 // the range of the tabulation on the x axis
292 Scalar xMin_;
293 Scalar xMax_;
294
295 // the range of the tabulation on the y axis
296 Scalar yMin_;
297 Scalar yMax_;
298};
299
300} // namespace Opm
301
302#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:52
void resize(Scalar minX, Scalar maxX, unsigned m, Scalar minY, Scalar maxY, unsigned n)
Resize the tabulation to a new range.
Definition UniformTabulated2DFunction.hpp:81
Scalar iToX(unsigned i) const
Return the position on the x-axis of the i-th interval.
Definition UniformTabulated2DFunction.hpp:135
Scalar yMax() const
Returns the maximum of the Y coordinate of the sampling points.
Definition UniformTabulated2DFunction.hpp:117
Evaluation xToI(const Evaluation &x) const
Return the interval index of a given position on the x-axis.
Definition UniformTabulated2DFunction.hpp:161
UniformTabulated2DFunction(Scalar minX, Scalar maxX, unsigned m, Scalar minY, Scalar maxY, unsigned n)
Constructor where the tabulation parameters are already provided.
Definition UniformTabulated2DFunction.hpp:61
unsigned numX() const
Returns the number of sampling points in X direction.
Definition UniformTabulated2DFunction.hpp:123
Scalar xMax() const
Returns the maximum of the X coordinate of the sampling points.
Definition UniformTabulated2DFunction.hpp:105
bool applies(const Evaluation &x, const Evaluation &y) const
Returns true iff a coordinate lies in the tabulated range.
Definition UniformTabulated2DFunction.hpp:180
Scalar xMin() const
Returns the minimum of the X coordinate of the sampling points.
Definition UniformTabulated2DFunction.hpp:99
Evaluation yToJ(const Evaluation &y) const
Return the interval index of a given position on the y-axis.
Definition UniformTabulated2DFunction.hpp:173
Evaluation eval(const Evaluation &x, const Evaluation &y, bool extrapolate) const
Evaluate the function at a given (x,y) position.
Definition UniformTabulated2DFunction.hpp:196
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:259
unsigned numY() const
Returns the number of sampling points in Y direction.
Definition UniformTabulated2DFunction.hpp:129
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:246
Scalar yMin() const
Returns the minimum of the Y coordinate of the sampling points.
Definition UniformTabulated2DFunction.hpp:111
Scalar jToY(unsigned j) const
Return the position on the y-axis of the j-th interval.
Definition UniformTabulated2DFunction.hpp:145
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30