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/Exceptions.hpp>
32 #include <opm/common/ErrorMacros.hpp>
34 
35 
36 #include <vector>
37 
38 #include <assert.h>
39 
40 namespace Opm {
41 
49 template <class Scalar>
51 {
52 public:
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 
69  void resize(Scalar minX, Scalar maxX, unsigned m,
70  Scalar minY, Scalar maxY, unsigned n)
71  {
72  samples_.resize(m*n);
73 
74  m_ = m;
75  n_ = n;
76 
77  xMin_ = minX;
78  xMax_ = maxX;
79 
80  yMin_ = minY;
81  yMax_ = maxY;
82  }
83 
87  Scalar xMin() const
88  { return xMin_; }
89 
93  Scalar xMax() const
94  { return xMax_; }
95 
99  Scalar yMin() const
100  { return yMin_; }
101 
105  Scalar yMax() const
106  { return yMax_; }
107 
111  unsigned numX() const
112  { return m_; }
113 
117  unsigned numY() const
118  { return n_; }
119 
123  Scalar iToX(unsigned i) const
124  {
125  assert(0 <= i && i < numX());
126 
127  return xMin() + i*(xMax() - xMin())/(numX() - 1);
128  }
129 
133  Scalar jToY(unsigned j) const
134  {
135  assert(0 <= j && j < numY());
136 
137  return yMin() + j*(yMax() - yMin())/(numY() - 1);
138  }
139 
148  template <class Evaluation>
149  Evaluation xToI(const Evaluation& x) const
150  { return (x - xMin())/(xMax() - xMin())*(numX() - 1); }
151 
160  template <class Evaluation>
161  Evaluation yToJ(const Evaluation& y) const
162  { return (y - yMin())/(yMax() - yMin())*(numY() - 1); }
163 
167  template <class Evaluation>
168  bool applies(const Evaluation& x, const Evaluation& y) const
169  {
170  return
171  xMin() <= x && x <= xMax() &&
172  yMin() <= y && y <= yMax();
173  }
174 
181  template <class Evaluation>
182  Evaluation eval(const Evaluation& x, const Evaluation& y) const
183  {
184 #ifndef NDEBUG
185  if (!applies(x,y))
186  {
187  OPM_THROW(NumericalProblem,
188  "Attempt to get tabulated value for ("
189  << x << ", " << y
190  << ") on a table of extend "
191  << xMin() << " to " << xMax() << " times "
192  << yMin() << " to " << yMax());
193  };
194 #endif
195 
196  Evaluation alpha = xToI(x);
197  Evaluation beta = yToJ(y);
198 
199  unsigned i =
200  static_cast<unsigned>(
201  std::max(0, std::min(static_cast<int>(numX()) - 2,
202  static_cast<int>(Opm::scalarValue(alpha)))));
203  unsigned j =
204  static_cast<unsigned>(
205  std::max(0, std::min(static_cast<int>(numY()) - 2,
206  static_cast<int>(Opm::scalarValue(beta)))));
207 
208  alpha -= i;
209  beta -= j;
210 
211  // bi-linear interpolation
212  const Evaluation& s1 = getSamplePoint(i, j)*(1.0 - alpha) + getSamplePoint(i + 1, j)*alpha;
213  const Evaluation& s2 = getSamplePoint(i, j + 1)*(1.0 - alpha) + getSamplePoint(i + 1, j + 1)*alpha;
214  return s1*(1.0 - beta) + s2*beta;
215  }
216 
222  Scalar getSamplePoint(unsigned i, unsigned j) const
223  {
224  assert(0 <= i && i < m_);
225  assert(0 <= j && j < n_);
226 
227  return samples_[j*m_ + i];
228  }
229 
235  void setSamplePoint(unsigned i, unsigned j, Scalar value)
236  {
237  assert(0 <= i && i < m_);
238  assert(0 <= j && j < n_);
239 
240  samples_[j*m_ + i] = value;
241  }
242 
243 private:
244  // the vector which contains the values of the sample points
245  // f(x_i, y_j). don't use this directly, use getSamplePoint(i,j)
246  // instead!
247  std::vector<Scalar> samples_;
248 
249  // the number of sample points in x direction
250  unsigned m_;
251 
252  // the number of sample points in y direction
253  unsigned n_;
254 
255  // the range of the tabulation on the x axis
256  Scalar xMin_;
257  Scalar xMax_;
258 
259  // the range of the tabulation on the y axis
260  Scalar yMin_;
261  Scalar yMax_;
262 };
263 } // namespace Opm
264 
265 #endif
void resize(Scalar minX, Scalar maxX, unsigned m, Scalar minY, Scalar maxY, unsigned n)
Resize the tabulation to a new range.
Definition: UniformTabulated2DFunction.hpp:69
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:222
A traits class which provides basic mathematical functions for arbitrary scalar floating point values...
Scalar xMin() const
Returns the minimum of the X coordinate of the sampling points.
Definition: UniformTabulated2DFunction.hpp:87
Scalar jToY(unsigned j) const
Return the position on the y-axis of the j-th interval.
Definition: UniformTabulated2DFunction.hpp:133
Evaluation yToJ(const Evaluation &y) const
Return the interval index of a given position on the y-axis.
Definition: UniformTabulated2DFunction.hpp:161
Scalar xMax() const
Returns the maximum of the X coordinate of the sampling points.
Definition: UniformTabulated2DFunction.hpp:93
unsigned numY() const
Returns the number of sampling points in Y direction.
Definition: UniformTabulated2DFunction.hpp:117
Definition: Air_Mesitylene.hpp:33
Scalar yMax() const
Returns the maximum of the Y coordinate of the sampling points.
Definition: UniformTabulated2DFunction.hpp:105
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
bool applies(const Evaluation &x, const Evaluation &y) const
Returns true iff a coordinate lies in the tabulated range.
Definition: UniformTabulated2DFunction.hpp:168
Scalar yMin() const
Returns the minimum of the Y coordinate of the sampling points.
Definition: UniformTabulated2DFunction.hpp:99
Evaluation xToI(const Evaluation &x) const
Return the interval index of a given position on the x-axis.
Definition: UniformTabulated2DFunction.hpp:149
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:235
Evaluation eval(const Evaluation &x, const Evaluation &y) const
Evaluate the function at a given (x,y) position.
Definition: UniformTabulated2DFunction.hpp:182
Scalar iToX(unsigned i) const
Return the position on the x-axis of the i-th interval.
Definition: UniformTabulated2DFunction.hpp:123
Implements a scalar function that depends on two variables and which is sampled on an uniform X-Y gri...
Definition: UniformTabulated2DFunction.hpp:50
unsigned numX() const
Returns the number of sampling points in X direction.
Definition: UniformTabulated2DFunction.hpp:111