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#include <opm/common/utility/gpuDecorators.hpp>
36
37#include <algorithm>
38#include <cassert>
39#include <string>
40#include <vector>
41
42// forward declaration of the class so the function in the next namespace can be declared
43template <class Scalar, class ContainerT = std::vector<Scalar>>
45
46// declaration of make_view in correct namespace so friend function can be declared in the class
47namespace Opm::gpuistl
48{
49 template <class ViewType, class ScalarT, class ContainerType>
51}
52
53namespace Opm {
54
62template <class Scalar, class ContainerT = std::vector<Scalar>>
64{
65public:
66 OPM_HOST_DEVICE UniformTabulated2DFunction()
67 { }
68
69
70 // Intended for construction of UniformTabulated2DFunction<double, GPUBuffer>
71 UniformTabulated2DFunction(Scalar minX, Scalar maxX, unsigned m,
72 Scalar minY, Scalar maxY, unsigned n,
73 const ContainerT& samples)
74 : samples_(samples), m_(m), n_(n), xMin_(minX), yMin_(minY), xMax_(maxX), yMax_(maxY){
75 }
76
81 UniformTabulated2DFunction(Scalar minX, Scalar maxX, unsigned m,
82 Scalar minY, Scalar maxY, unsigned n)
83 {
84 resize(minX, maxX, m, minY, maxY, n);
85 }
86
87 UniformTabulated2DFunction(Scalar minX, Scalar maxX, unsigned m,
88 Scalar minY, Scalar maxY, unsigned n,
89 const std::vector<std::vector<Scalar>>& vals)
90 {
91 resize(minX, maxX, m, minY, maxY, n);
92
93 for (unsigned i = 0; i < m; ++i)
94 for (unsigned j = 0; j < n; ++j)
95 this->setSamplePoint(i, j, vals[i][j]);
96 }
97
98 // Both CO2Tables and H2Tables have values of dimes [200][500]
99 // suboptimal hardcoding for now but easier than templating this function etc
100 UniformTabulated2DFunction(Scalar minX, Scalar maxX, unsigned m,
101 Scalar minY, Scalar maxY, unsigned n,
102 const double vals[200][500])
103 {
104 resize(minX, maxX, m, minY, maxY, n);
105
106 for (unsigned i = 0; i < m; ++i)
107 for (unsigned j = 0; j < n; ++j)
108 this->setSamplePoint(i, j, vals[i][j]);
109 }
110
114 void resize(Scalar minX, Scalar maxX, unsigned m,
115 Scalar minY, Scalar maxY, unsigned n)
116 {
117 samples_.resize(m*n);
118
119 m_ = m;
120 n_ = n;
121
122 xMin_ = minX;
123 xMax_ = maxX;
124
125 yMin_ = minY;
126 yMax_ = maxY;
127 }
128
132 OPM_HOST_DEVICE Scalar xMin() const
133 { return xMin_; }
134
138 OPM_HOST_DEVICE Scalar xMax() const
139 { return xMax_; }
140
144 OPM_HOST_DEVICE Scalar yMin() const
145 { return yMin_; }
146
150 OPM_HOST_DEVICE Scalar yMax() const
151 { return yMax_; }
152
156 OPM_HOST_DEVICE unsigned numX() const
157 { return m_; }
158
162 OPM_HOST_DEVICE unsigned numY() const
163 { return n_; }
164
168 OPM_HOST_DEVICE const ContainerT& samples() const
169 { return samples_; }
170
171
175 OPM_HOST_DEVICE Scalar iToX(unsigned i) const
176 {
177 assert(i < numX());
178
179 return xMin() + i*(xMax() - xMin())/(numX() - 1);
180 }
181
185 OPM_HOST_DEVICE Scalar jToY(unsigned j) const
186 {
187 assert(j < numY());
188
189 return yMin() + j*(yMax() - yMin())/(numY() - 1);
190 }
191
200 template <class Evaluation>
201 OPM_HOST_DEVICE Evaluation xToI(const Evaluation& x) const
202 { return (x - xMin())/(xMax() - xMin())*(numX() - 1); }
203
212 template <class Evaluation>
213 OPM_HOST_DEVICE Evaluation yToJ(const Evaluation& y) const
214 { return (y - yMin())/(yMax() - yMin())*(numY() - 1); }
215
219 template <class Evaluation>
220 OPM_HOST_DEVICE bool applies(const Evaluation& x, const Evaluation& y) const
221 {
222 return
223 xMin() <= x && x <= xMax() &&
224 yMin() <= y && y <= yMax();
225 }
226
235 template <class Evaluation>
236 OPM_HOST_DEVICE Evaluation eval(const Evaluation& x,
237 const Evaluation& y,
238 [[maybe_unused]] bool extrapolate) const
239 {
240#ifndef NDEBUG
241#if !OPM_IS_INSIDE_DEVICE_FUNCTION
242 if (!extrapolate && !applies(x,y)) {
243 std::string msg = "Attempt to get tabulated value for ("
244 +std::to_string(double(scalarValue(x)))+", "+std::to_string(double(scalarValue(y)))
245 +") on a table of extent "
246 +std::to_string(xMin())+" to "+std::to_string(xMax())+" times "
247 +std::to_string(yMin())+" to "+std::to_string(yMax());
248
249 throw NumericalProblem(msg);
250 }
251#endif
252#endif
253
254 Evaluation alpha = xToI(x);
255 Evaluation beta = yToJ(y);
256
257 unsigned i =
258 static_cast<unsigned>(
259 std::max(0, std::min(static_cast<int>(numX()) - 2,
260 static_cast<int>(scalarValue(alpha)))));
261 unsigned j =
262 static_cast<unsigned>(
263 std::max(0, std::min(static_cast<int>(numY()) - 2,
264 static_cast<int>(scalarValue(beta)))));
265
266 alpha -= i;
267 beta -= j;
268
269 // bi-linear interpolation
270 const Evaluation& s1 = getSamplePoint(i, j)*(1.0 - alpha) + getSamplePoint(i + 1, j)*alpha;
271 const Evaluation& s2 = getSamplePoint(i, j + 1)*(1.0 - alpha) + getSamplePoint(i + 1, j + 1)*alpha;
272 return s1*(1.0 - beta) + s2*beta;
273 }
274
280 OPM_HOST_DEVICE Scalar getSamplePoint(unsigned i, unsigned j) const
281 {
282 assert(i < m_);
283 assert(j < n_);
284
285 return samples_[j*m_ + i];
286 }
287
293 void setSamplePoint(unsigned i, unsigned j, Scalar value)
294 {
295 assert(i < m_);
296 assert(j < n_);
297
298 samples_[j*m_ + i] = value;
299 }
300
301 OPM_HOST_DEVICE bool operator==(const UniformTabulated2DFunction<Scalar>& data) const
302 {
303 return samples_ == data.samples_ &&
304 m_ == data.m_ &&
305 n_ == data.n_ &&
306 xMin_ == data.xMin_ &&
307 xMax_ == data.xMax_ &&
308 yMin_ == data.yMin_ &&
309 yMax_ == data.yMax_;
310 }
311
312private:
313 template <class ViewType, class ScalarT, class Container>
315
316 // the vector which contains the values of the sample points
317 // f(x_i, y_j). don't use this directly, use getSamplePoint(i,j)
318 // instead!
319 ContainerT samples_;
320
321 // the number of sample points in x direction
322 unsigned m_;
323
324 // the number of sample points in y direction
325 unsigned n_;
326
327 // the range of the tabulation on the x axis
328 Scalar xMin_;
329 Scalar xMax_;
330
331 // the range of the tabulation on the y axis
332 Scalar yMin_;
333 Scalar yMax_;
334};
335
336} // namespace Opm
337
338namespace Opm::gpuistl{
339 template<class GPUContainer, class ScalarT>
341 copy_to_gpu(const UniformTabulated2DFunction<ScalarT>& tab){
342 return UniformTabulated2DFunction<ScalarT, GPUContainer>(tab.xMin(), tab.xMax(), tab.numX(), tab.yMin(), tab.yMax(), tab.numY(), GPUContainer(tab.samples()));
343 }
344
345 template <class ViewType, class ScalarT, class ContainerType>
348
349 ViewType newTab = make_view<typename ViewType::value_type>(tab.samples_);
350
351 return UniformTabulated2DFunction<ScalarT, ViewType>(tab.xMin(), tab.xMax(), tab.numX(), tab.yMin(), tab.yMax(), tab.numY(), newTab);
352 }
353}
354
355#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:64
void resize(Scalar minX, Scalar maxX, unsigned m, Scalar minY, Scalar maxY, unsigned n)
Resize the tabulation to a new range.
Definition UniformTabulated2DFunction.hpp:114
OPM_HOST_DEVICE Scalar xMin() const
Returns the minimum of the X coordinate of the sampling points.
Definition UniformTabulated2DFunction.hpp:132
OPM_HOST_DEVICE const ContainerT & samples() const
Returns the sampling points.
Definition UniformTabulated2DFunction.hpp:168
OPM_HOST_DEVICE 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:280
OPM_HOST_DEVICE unsigned numY() const
Returns the number of sampling points in Y direction.
Definition UniformTabulated2DFunction.hpp:162
OPM_HOST_DEVICE Scalar yMin() const
Returns the minimum of the Y coordinate of the sampling points.
Definition UniformTabulated2DFunction.hpp:144
OPM_HOST_DEVICE bool applies(const Evaluation &x, const Evaluation &y) const
Returns true iff a coordinate lies in the tabulated range.
Definition UniformTabulated2DFunction.hpp:220
OPM_HOST_DEVICE Evaluation yToJ(const Evaluation &y) const
Return the interval index of a given position on the y-axis.
Definition UniformTabulated2DFunction.hpp:213
OPM_HOST_DEVICE unsigned numX() const
Returns the number of sampling points in X direction.
Definition UniformTabulated2DFunction.hpp:156
OPM_HOST_DEVICE Scalar jToY(unsigned j) const
Return the position on the y-axis of the j-th interval.
Definition UniformTabulated2DFunction.hpp:185
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:293
OPM_HOST_DEVICE Evaluation eval(const Evaluation &x, const Evaluation &y, bool extrapolate) const
Evaluate the function at a given (x,y) position.
Definition UniformTabulated2DFunction.hpp:236
UniformTabulated2DFunction(Scalar minX, Scalar maxX, unsigned m, Scalar minY, Scalar maxY, unsigned n)
Constructor where the tabulation parameters are already provided.
Definition UniformTabulated2DFunction.hpp:81
OPM_HOST_DEVICE Scalar yMax() const
Returns the maximum of the Y coordinate of the sampling points.
Definition UniformTabulated2DFunction.hpp:150
OPM_HOST_DEVICE Evaluation xToI(const Evaluation &x) const
Return the interval index of a given position on the x-axis.
Definition UniformTabulated2DFunction.hpp:201
OPM_HOST_DEVICE Scalar iToX(unsigned i) const
Return the position on the x-axis of the i-th interval.
Definition UniformTabulated2DFunction.hpp:175
OPM_HOST_DEVICE Scalar xMax() const
Returns the maximum of the X coordinate of the sampling points.
Definition UniformTabulated2DFunction.hpp:138
Definition UniformTabulated2DFunction.hpp:44
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30