3.2-git
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
tabulated2dfunction.hh
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 * See the file COPYING for full copying permissions. *
5 * *
6 * This program 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 3 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. *
18 *****************************************************************************/
24#ifndef DUMUX_TABULATED_2D_FUNCTION_HH
25#define DUMUX_TABULATED_2D_FUNCTION_HH
26
27#include <vector>
28#include <cassert>
29
30namespace Dumux {
31
45template<class Scalar>
47{
48public:
53 { };
54
59 Tabulated2DFunction(Scalar xMin, Scalar xMax, int m,
60 Scalar yMin, Scalar yMax, int n)
61 {
62 resize(xMin, xMax, m, yMin, yMax, n);
63 };
64
71 void resize(Scalar xMin, Scalar xMax, int m,
72 Scalar yMin, Scalar yMax, int n)
73 {
74 samples_.resize(m*n);
75
76 m_ = m;
77 n_ = n;
78
79 xMin_ = xMin;
80 xMax_ = xMax;
81
82 yMin_ = yMin;
83 yMax_ = yMax;
84 }
85
89 Scalar iToX(int i) const
90 {
91 assert(0 <= i && i < m_);
92
93 return xMin_ + i*(xMax_ - xMin_)/(m_ - 1);
94 }
95
99 Scalar jToY(int j) const
100 {
101 assert(0 <= j && j < n_);
102
103 return yMin_ + j*(yMax_ - yMin_)/(n_ - 1);
104 }
105
114 Scalar xToI(Scalar x) const
115 {
116 return (x - xMin_)/(xMax_ - xMin_)*m_;
117 }
118
119
128 Scalar yToJ(Scalar y) const
129 {
130 return (y - yMin_)/(yMax_ - yMin_)*n_;
131 }
132
133
139 Scalar getSamplePoint(int i, int j) const
140 {
141 assert(0 <= i && i < m_);
142 assert(0 <= j && j < n_);
143
144 return samples_[j*m_ + i];
145 }
146
152 void setSamplePoint(int i, int j, Scalar value)
153 {
154 assert(0 <= i && i < m_);
155 assert(0 <= j && j < n_);
156
157 samples_[j*m_ + i] = value;
158 }
159
163 Scalar get(Scalar x, Scalar y) const
164 {
165 Scalar alpha = xToI(x);
166 Scalar beta = yToJ(y);
167
168 using std::max;
169 using std::min;
170 int i = max(0, min(m_, static_cast<int>(alpha)));
171 int j = max(0, min(n_, static_cast<int>(beta)));
172
173 alpha -= i;
174 beta -= j;
175
176 // bi-linear interpolation
177 Scalar s1 = getSamplePoint(i, j)*(1.0 - alpha) + getSamplePoint(i + 1, j)*alpha;
178 Scalar s2 = getSamplePoint(i, j + 1)*(1.0 - alpha) + getSamplePoint(i + 1, j + 1)*alpha;
179 return s1*(1.0 - beta) + s2*beta;
180 }
181
187 Scalar operator()(Scalar x, Scalar y) const
188 { return get(x, y); }
189
190private:
191 // the vector which contains the values of the sample points
192 // f(x_i, y_j). don't use this directly, use getSamplePoint(i,j)
193 // instead!
194 std::vector<Scalar> samples_;
195
196 // the number of sample points in x direction
197 int m_;
198
199 // the number of sample points in y direction
200 int n_;
201
202 // the range of the tabulation on the x axis
203 Scalar xMin_;
204 Scalar xMax_;
205
206 // the range of the tabulation on the y axis
207 Scalar yMin_;
208 Scalar yMax_;
209
210};
211
212} // end namespace Dumux
213
214#endif
Definition: adapt.hh:29
Implements tabulation for a two-dimensional function.
Definition: tabulated2dfunction.hh:47
Tabulated2DFunction()
Default constructor.
Definition: tabulated2dfunction.hh:52
Scalar iToX(int i) const
Return the position on the x-axis of the i-th interval.
Definition: tabulated2dfunction.hh:89
Scalar xToI(Scalar x) const
Return the interval index of a given position on the x-axis.
Definition: tabulated2dfunction.hh:114
Scalar getSamplePoint(int i, int j) const
Get the value of the sample point which is at the intersection of the -th interval of the x-Axis and ...
Definition: tabulated2dfunction.hh:139
Tabulated2DFunction(Scalar xMin, Scalar xMax, int m, Scalar yMin, Scalar yMax, int n)
Constructor where the tabulation parameters are already provided.
Definition: tabulated2dfunction.hh:59
Scalar yToJ(Scalar y) const
Return the interval index of a given position on the y-axis.
Definition: tabulated2dfunction.hh:128
Scalar jToY(int j) const
Return the position on the y-axis of the j-th interval.
Definition: tabulated2dfunction.hh:99
Scalar operator()(Scalar x, Scalar y) const
The () operator.
Definition: tabulated2dfunction.hh:187
Scalar get(Scalar x, Scalar y) const
Return an interpolated value.
Definition: tabulated2dfunction.hh:163
void setSamplePoint(int i, int 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: tabulated2dfunction.hh:152
void resize(Scalar xMin, Scalar xMax, int m, Scalar yMin, Scalar yMax, int n)
Resize the tabulation to a new range.
Definition: tabulated2dfunction.hh:71