00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028 #ifndef OPM_UNIFORM_TABULATED_2D_FUNCTION_HPP
00029 #define OPM_UNIFORM_TABULATED_2D_FUNCTION_HPP
00030
00031 #include <opm/common/Exceptions.hpp>
00032 #include <opm/common/ErrorMacros.hpp>
00033 #include <opm/material/common/MathToolbox.hpp>
00034
00035
00036 #include <vector>
00037
00038 #include <assert.h>
00039
00040 namespace Opm {
00041
00049 template <class Scalar>
00050 class UniformTabulated2DFunction
00051 {
00052 public:
00053 UniformTabulated2DFunction()
00054 { }
00055
00060 UniformTabulated2DFunction(Scalar minX, Scalar maxX, unsigned m,
00061 Scalar minY, Scalar maxY, unsigned n)
00062 {
00063 resize(minX, maxX, m, minY, maxY, n);
00064 }
00065
00069 void resize(Scalar minX, Scalar maxX, unsigned m,
00070 Scalar minY, Scalar maxY, unsigned n)
00071 {
00072 samples_.resize(m*n);
00073
00074 m_ = m;
00075 n_ = n;
00076
00077 xMin_ = minX;
00078 xMax_ = maxX;
00079
00080 yMin_ = minY;
00081 yMax_ = maxY;
00082 }
00083
00087 Scalar xMin() const
00088 { return xMin_; }
00089
00093 Scalar xMax() const
00094 { return xMax_; }
00095
00099 Scalar yMin() const
00100 { return yMin_; }
00101
00105 Scalar yMax() const
00106 { return yMax_; }
00107
00111 unsigned numX() const
00112 { return m_; }
00113
00117 unsigned numY() const
00118 { return n_; }
00119
00123 Scalar iToX(unsigned i) const
00124 {
00125 assert(0 <= i && i < numX());
00126
00127 return xMin() + i*(xMax() - xMin())/(numX() - 1);
00128 }
00129
00133 Scalar jToY(unsigned j) const
00134 {
00135 assert(0 <= j && j < numY());
00136
00137 return yMin() + j*(yMax() - yMin())/(numY() - 1);
00138 }
00139
00148 template <class Evaluation>
00149 Evaluation xToI(const Evaluation& x) const
00150 { return (x - xMin())/(xMax() - xMin())*(numX() - 1); }
00151
00160 template <class Evaluation>
00161 Evaluation yToJ(const Evaluation& y) const
00162 { return (y - yMin())/(yMax() - yMin())*(numY() - 1); }
00163
00167 template <class Evaluation>
00168 bool applies(const Evaluation& x, const Evaluation& y) const
00169 {
00170 return
00171 xMin() <= x && x <= xMax() &&
00172 yMin() <= y && y <= yMax();
00173 }
00174
00181 template <class Evaluation>
00182 Evaluation eval(const Evaluation& x, const Evaluation& y) const
00183 {
00184 #ifndef NDEBUG
00185 if (!applies(x,y))
00186 {
00187 OPM_THROW(NumericalProblem,
00188 "Attempt to get tabulated value for ("
00189 << x << ", " << y
00190 << ") on a table of extend "
00191 << xMin() << " to " << xMax() << " times "
00192 << yMin() << " to " << yMax());
00193 };
00194 #endif
00195
00196 Evaluation alpha = xToI(x);
00197 Evaluation beta = yToJ(y);
00198
00199 unsigned i =
00200 static_cast<unsigned>(
00201 std::max(0, std::min(static_cast<int>(numX()) - 2,
00202 static_cast<int>(Opm::scalarValue(alpha)))));
00203 unsigned j =
00204 static_cast<unsigned>(
00205 std::max(0, std::min(static_cast<int>(numY()) - 2,
00206 static_cast<int>(Opm::scalarValue(beta)))));
00207
00208 alpha -= i;
00209 beta -= j;
00210
00211
00212 const Evaluation& s1 = getSamplePoint(i, j)*(1.0 - alpha) + getSamplePoint(i + 1, j)*alpha;
00213 const Evaluation& s2 = getSamplePoint(i, j + 1)*(1.0 - alpha) + getSamplePoint(i + 1, j + 1)*alpha;
00214 return s1*(1.0 - beta) + s2*beta;
00215 }
00216
00222 Scalar getSamplePoint(unsigned i, unsigned j) const
00223 {
00224 assert(0 <= i && i < m_);
00225 assert(0 <= j && j < n_);
00226
00227 return samples_[j*m_ + i];
00228 }
00229
00235 void setSamplePoint(unsigned i, unsigned j, Scalar value)
00236 {
00237 assert(0 <= i && i < m_);
00238 assert(0 <= j && j < n_);
00239
00240 samples_[j*m_ + i] = value;
00241 }
00242
00243 private:
00244
00245
00246
00247 std::vector<Scalar> samples_;
00248
00249
00250 unsigned m_;
00251
00252
00253 unsigned n_;
00254
00255
00256 Scalar xMin_;
00257 Scalar xMax_;
00258
00259
00260 Scalar yMin_;
00261 Scalar yMax_;
00262 };
00263 }
00264
00265 #endif