My Project
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 
33 
34 #if HAVE_OPM_COMMON
35 #include <opm/common/OpmLog/OpmLog.hpp>
36 #else
37 #include <iostream>
38 #endif
39 
40 #include <vector>
41 
42 #include <assert.h>
43 
44 namespace Opm {
45 
53 template <class Scalar>
55 {
56 public:
58  { }
59 
64  UniformTabulated2DFunction(Scalar minX, Scalar maxX, unsigned m,
65  Scalar minY, Scalar maxY, unsigned n)
66  {
67  resize(minX, maxX, m, minY, maxY, n);
68  }
69 
70  UniformTabulated2DFunction(Scalar minX, Scalar maxX, unsigned m,
71  Scalar minY, Scalar maxY, unsigned n,
72  const std::vector<std::vector<Scalar>>& vals)
73  {
74  resize(minX, maxX, m, minY, maxY, n);
75 
76  for (unsigned i = 0; i < m; ++i)
77  for (unsigned j = 0; j < n; ++j)
78  this->setSamplePoint(i, j, vals[i][j]);
79  }
80 
84  void resize(Scalar minX, Scalar maxX, unsigned m,
85  Scalar minY, Scalar maxY, unsigned n)
86  {
87  samples_.resize(m*n);
88 
89  m_ = m;
90  n_ = n;
91 
92  xMin_ = minX;
93  xMax_ = maxX;
94 
95  yMin_ = minY;
96  yMax_ = maxY;
97  }
98 
102  Scalar xMin() const
103  { return xMin_; }
104 
108  Scalar xMax() const
109  { return xMax_; }
110 
114  Scalar yMin() const
115  { return yMin_; }
116 
120  Scalar yMax() const
121  { return yMax_; }
122 
126  unsigned numX() const
127  { return m_; }
128 
132  unsigned numY() const
133  { return n_; }
134 
138  Scalar iToX(unsigned i) const
139  {
140  assert(i < numX());
141 
142  return xMin() + i*(xMax() - xMin())/(numX() - 1);
143  }
144 
148  Scalar jToY(unsigned j) const
149  {
150  assert(j < numY());
151 
152  return yMin() + j*(yMax() - yMin())/(numY() - 1);
153  }
154 
163  template <class Evaluation>
164  Evaluation xToI(const Evaluation& x) const
165  { return (x - xMin())/(xMax() - xMin())*(numX() - 1); }
166 
175  template <class Evaluation>
176  Evaluation yToJ(const Evaluation& y) const
177  { return (y - yMin())/(yMax() - yMin())*(numY() - 1); }
178 
182  template <class Evaluation>
183  bool applies(const Evaluation& x, const Evaluation& y) const
184  {
185  return
186  xMin() <= x && x <= xMax() &&
187  yMin() <= y && y <= yMax();
188  }
189 
198  template <class Evaluation>
199  Evaluation eval(const Evaluation& x, const Evaluation& y, bool extrapolate) const
200  {
201  if (!applies(x,y))
202  {
203  std::string msg = "Attempt to get tabulated value for ("
204  +std::to_string(double(scalarValue(x)))+", "+std::to_string(double(scalarValue(y)))
205  +") on a table of extent "
206  +std::to_string(xMin())+" to "+std::to_string(xMax())+" times "
207  +std::to_string(yMin())+" to "+std::to_string(yMax());
208 
209  if (!extrapolate)
210  {
211  throw NumericalIssue(msg);
212  }
213  else
214  {
215 #if HAVE_OPM_COMMON
216  OpmLog::warning("PVT Table evaluation:" + msg + ". Will use extrapolation");
217 #else
218  std::cerr << "warning: "<< msg<<std::endl;
219 #endif
220  }
221 
222  };
223 
224  Evaluation alpha = xToI(x);
225  Evaluation beta = yToJ(y);
226 
227  unsigned i =
228  static_cast<unsigned>(
229  std::max(0, std::min(static_cast<int>(numX()) - 2,
230  static_cast<int>(scalarValue(alpha)))));
231  unsigned j =
232  static_cast<unsigned>(
233  std::max(0, std::min(static_cast<int>(numY()) - 2,
234  static_cast<int>(scalarValue(beta)))));
235 
236  alpha -= i;
237  beta -= j;
238 
239  // bi-linear interpolation
240  const Evaluation& s1 = getSamplePoint(i, j)*(1.0 - alpha) + getSamplePoint(i + 1, j)*alpha;
241  const Evaluation& s2 = getSamplePoint(i, j + 1)*(1.0 - alpha) + getSamplePoint(i + 1, j + 1)*alpha;
242  return s1*(1.0 - beta) + s2*beta;
243  }
244 
250  Scalar getSamplePoint(unsigned i, unsigned j) const
251  {
252  assert(i < m_);
253  assert(j < n_);
254 
255  return samples_[j*m_ + i];
256  }
257 
263  void setSamplePoint(unsigned i, unsigned j, Scalar value)
264  {
265  assert(i < m_);
266  assert(j < n_);
267 
268  samples_[j*m_ + i] = value;
269  }
270 
271  bool operator==(const UniformTabulated2DFunction<Scalar>& data) const
272  {
273  return samples_ == data.samples_ &&
274  m_ == data.m_ &&
275  n_ == data.n_ &&
276  xMin_ == data.xMin_ &&
277  xMax_ == data.xMax_ &&
278  yMin_ == data.yMin_ &&
279  yMax_ == data.yMax_;
280  }
281 
282 
283 private:
284  // the vector which contains the values of the sample points
285  // f(x_i, y_j). don't use this directly, use getSamplePoint(i,j)
286  // instead!
287  std::vector<Scalar> samples_;
288 
289  // the number of sample points in x direction
290  unsigned m_;
291 
292  // the number of sample points in y direction
293  unsigned n_;
294 
295  // the range of the tabulation on the x axis
296  Scalar xMin_;
297  Scalar xMax_;
298 
299  // the range of the tabulation on the y axis
300  Scalar yMin_;
301  Scalar yMax_;
302 };
303 
304 } // namespace Opm
305 
306 #endif
Provides the opm-material specific exception classes.
A traits class which provides basic mathematical functions for arbitrary scalar floating point values...
Definition: Exceptions.hpp:46
Implements a scalar function that depends on two variables and which is sampled on an uniform X-Y gri...
Definition: UniformTabulated2DFunction.hpp:55
void resize(Scalar minX, Scalar maxX, unsigned m, Scalar minY, Scalar maxY, unsigned n)
Resize the tabulation to a new range.
Definition: UniformTabulated2DFunction.hpp:84
Scalar iToX(unsigned i) const
Return the position on the x-axis of the i-th interval.
Definition: UniformTabulated2DFunction.hpp:138
Scalar yMax() const
Returns the maximum of the Y coordinate of the sampling points.
Definition: UniformTabulated2DFunction.hpp:120
Evaluation xToI(const Evaluation &x) const
Return the interval index of a given position on the x-axis.
Definition: UniformTabulated2DFunction.hpp:164
UniformTabulated2DFunction(Scalar minX, Scalar maxX, unsigned m, Scalar minY, Scalar maxY, unsigned n)
Constructor where the tabulation parameters are already provided.
Definition: UniformTabulated2DFunction.hpp:64
unsigned numX() const
Returns the number of sampling points in X direction.
Definition: UniformTabulated2DFunction.hpp:126
Scalar xMax() const
Returns the maximum of the X coordinate of the sampling points.
Definition: UniformTabulated2DFunction.hpp:108
bool applies(const Evaluation &x, const Evaluation &y) const
Returns true iff a coordinate lies in the tabulated range.
Definition: UniformTabulated2DFunction.hpp:183
Scalar xMin() const
Returns the minimum of the X coordinate of the sampling points.
Definition: UniformTabulated2DFunction.hpp:102
Evaluation yToJ(const Evaluation &y) const
Return the interval index of a given position on the y-axis.
Definition: UniformTabulated2DFunction.hpp:176
Evaluation eval(const Evaluation &x, const Evaluation &y, bool extrapolate) const
Evaluate the function at a given (x,y) position.
Definition: UniformTabulated2DFunction.hpp:199
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:263
unsigned numY() const
Returns the number of sampling points in Y direction.
Definition: UniformTabulated2DFunction.hpp:132
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:250
Scalar yMin() const
Returns the minimum of the Y coordinate of the sampling points.
Definition: UniformTabulated2DFunction.hpp:114
Scalar jToY(unsigned j) const
Return the position on the y-axis of the j-th interval.
Definition: UniformTabulated2DFunction.hpp:148