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  Copyright (C) 2013 by Andreas Lauser
5 
6  This file is part of the Open Porous Media project (OPM).
7 
8  OPM is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 2 of the License, or
11  (at your option) any later version.
12 
13  OPM is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with OPM. If not, see <http://www.gnu.org/licenses/>.
20 */
26 #ifndef OPM_UNIFORM_TABULATED_2D_FUNCTION_HPP
27 #define OPM_UNIFORM_TABULATED_2D_FUNCTION_HPP
28 
29 #include <opm/common/Exceptions.hpp>
30 #include <opm/common/ErrorMacros.hpp>
32 
33 
34 #include <vector>
35 
36 #include <assert.h>
37 
38 namespace Opm {
39 
47 template <class Scalar>
49 {
50 public:
52  { }
53 
58  UniformTabulated2DFunction(Scalar xMin, Scalar xMax, unsigned m,
59  Scalar yMin, Scalar yMax, unsigned n)
60  {
61  resize(xMin, xMax, m, yMin, yMax, n);
62  }
63 
67  void resize(Scalar xMin, Scalar xMax, unsigned m,
68  Scalar yMin, Scalar yMax, unsigned n)
69  {
70  samples_.resize(m*n);
71 
72  m_ = m;
73  n_ = n;
74 
75  xMin_ = xMin;
76  xMax_ = xMax;
77 
78  yMin_ = yMin;
79  yMax_ = yMax;
80  }
81 
85  Scalar xMin() const
86  { return xMin_; }
87 
91  Scalar xMax() const
92  { return xMax_; }
93 
97  Scalar yMin() const
98  { return yMin_; }
99 
103  Scalar yMax() const
104  { return yMax_; }
105 
109  unsigned numX() const
110  { return m_; }
111 
115  unsigned numY() const
116  { return n_; }
117 
121  Scalar iToX(unsigned i) const
122  {
123  assert(0 <= i && i < numX());
124 
125  return xMin() + i*(xMax() - xMin())/(numX() - 1);
126  }
127 
131  Scalar jToY(unsigned j) const
132  {
133  assert(0 <= j && j < numY());
134 
135  return yMin() + j*(yMax() - yMin())/(numY() - 1);
136  }
137 
146  template <class Evaluation>
147  Evaluation xToI(const Evaluation& x) const
148  { return (x - xMin())/(xMax() - xMin())*(numX() - 1); }
149 
158  template <class Evaluation>
159  Evaluation yToJ(const Evaluation& y) const
160  { return (y - yMin())/(yMax() - yMin())*(numY() - 1); }
161 
165  template <class Evaluation>
166  bool applies(const Evaluation& x, const Evaluation& y) const
167  {
168  return
169  xMin() <= x && x <= xMax() &&
170  yMin() <= y && y <= yMax();
171  }
172 
179  template <class Evaluation>
180  Evaluation eval(const Evaluation& x, const Evaluation& y) const
181  {
182  typedef MathToolbox<Evaluation> Toolbox;
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 = std::max(0U, std::min(static_cast<unsigned>(numX()) - 2,
200  static_cast<unsigned>(Toolbox::value(alpha))));
201  unsigned j = std::max(0U, std::min(static_cast<unsigned>(numY()) - 2,
202  static_cast<unsigned>(Toolbox::value(beta))));
203 
204  alpha -= i;
205  beta -= j;
206 
207  // bi-linear interpolation
208  const Evaluation& s1 = getSamplePoint(i, j)*(1.0 - alpha) + getSamplePoint(i + 1, j)*alpha;
209  const Evaluation& s2 = getSamplePoint(i, j + 1)*(1.0 - alpha) + getSamplePoint(i + 1, j + 1)*alpha;
210  return s1*(1.0 - beta) + s2*beta;
211  }
212 
218  Scalar getSamplePoint(unsigned i, unsigned j) const
219  {
220  assert(0 <= i && i < m_);
221  assert(0 <= j && j < n_);
222 
223  return samples_[j*m_ + i];
224  }
225 
231  void setSamplePoint(unsigned i, unsigned j, Scalar value)
232  {
233  assert(0 <= i && i < m_);
234  assert(0 <= j && j < n_);
235 
236  samples_[j*m_ + i] = value;
237  }
238 
239 private:
240  // the vector which contains the values of the sample points
241  // f(x_i, y_j). don't use this directly, use getSamplePoint(i,j)
242  // instead!
243  std::vector<Scalar> samples_;
244 
245  // the number of sample points in x direction
246  unsigned m_;
247 
248  // the number of sample points in y direction
249  unsigned n_;
250 
251  // the range of the tabulation on the x axis
252  Scalar xMin_;
253  Scalar xMax_;
254 
255  // the range of the tabulation on the y axis
256  Scalar yMin_;
257  Scalar yMax_;
258 };
259 } // namespace Opm
260 
261 #endif
Evaluation eval(const Evaluation &x, const Evaluation &y) const
Evaluate the function at a given (x,y) position.
Definition: UniformTabulated2DFunction.hpp:180
Evaluation yToJ(const Evaluation &y) const
Return the interval index of a given position on the y-axis.
Definition: UniformTabulated2DFunction.hpp:159
bool applies(const Evaluation &x, const Evaluation &y) const
Returns true iff a coordinate lies in the tabulated range.
Definition: UniformTabulated2DFunction.hpp:166
Scalar yMax() const
Returns the maximum of the Y coordinate of the sampling points.
Definition: UniformTabulated2DFunction.hpp:103
Definition: MathToolbox.hpp:39
Definition: Air_Mesitylene.hpp:31
Evaluation< Scalar, VarSetTag, numVars > max(const Evaluation< Scalar, VarSetTag, numVars > &x1, const Evaluation< Scalar, VarSetTag, numVars > &x2)
Definition: Math.hpp:114
Scalar jToY(unsigned j) const
Return the position on the y-axis of the j-th interval.
Definition: UniformTabulated2DFunction.hpp:131
UniformTabulated2DFunction()
Definition: UniformTabulated2DFunction.hpp:51
Scalar iToX(unsigned i) const
Return the position on the x-axis of the i-th interval.
Definition: UniformTabulated2DFunction.hpp:121
Scalar xMax() const
Returns the maximum of the X coordinate of the sampling points.
Definition: UniformTabulated2DFunction.hpp:91
Scalar yMin() const
Returns the minimum of the Y coordinate of the sampling points.
Definition: UniformTabulated2DFunction.hpp:97
Implements a scalar function that depends on two variables and which is sampled on an uniform X-Y gri...
Definition: UniformTabulated2DFunction.hpp:48
Scalar xMin() const
Returns the minimum of the X coordinate of the sampling points.
Definition: UniformTabulated2DFunction.hpp:85
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:218
Evaluation< Scalar, VarSetTag, numVars > min(const Evaluation< Scalar, VarSetTag, numVars > &x1, const Evaluation< Scalar, VarSetTag, numVars > &x2)
Definition: Math.hpp:61
void resize(Scalar xMin, Scalar xMax, unsigned m, Scalar yMin, Scalar yMax, unsigned n)
Resize the tabulation to a new range.
Definition: UniformTabulated2DFunction.hpp:67
UniformTabulated2DFunction(Scalar xMin, Scalar xMax, unsigned m, Scalar yMin, Scalar yMax, unsigned n)
Constructor where the tabulation parameters are already provided.
Definition: UniformTabulated2DFunction.hpp:58
unsigned numY() const
Returns the number of sampling points in Y direction.
Definition: UniformTabulated2DFunction.hpp:115
unsigned numX() const
Returns the number of sampling points in X direction.
Definition: UniformTabulated2DFunction.hpp:109
Evaluation xToI(const Evaluation &x) const
Return the interval index of a given position on the x-axis.
Definition: UniformTabulated2DFunction.hpp:147
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:231
A traits class which provides basic mathematical functions for arbitrary scalar floating point values...