3.5-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#include <algorithm>
30
31namespace Dumux {
32
46template<class Scalar>
48{
49public:
54 { };
55
60 Tabulated2DFunction(Scalar xMin, Scalar xMax, int m,
61 Scalar yMin, Scalar yMax, int n)
62 {
63 resize(xMin, xMax, m, yMin, yMax, n);
64 };
65
72 void resize(Scalar xMin, Scalar xMax, int m,
73 Scalar yMin, Scalar yMax, int n)
74 {
75 samples_.resize(m*n);
76
77 m_ = m;
78 n_ = n;
79
80 xMin_ = xMin;
81 xMax_ = xMax;
82
83 yMin_ = yMin;
84 yMax_ = yMax;
85 }
86
90 Scalar iToX(int i) const
91 {
92 assert(0 <= i && i < m_);
93
94 return xMin_ + i*(xMax_ - xMin_)/(m_ - 1);
95 }
96
100 Scalar jToY(int j) const
101 {
102 assert(0 <= j && j < n_);
103
104 return yMin_ + j*(yMax_ - yMin_)/(n_ - 1);
105 }
106
115 Scalar xToI(Scalar x) const
116 {
117 return (x - xMin_)/(xMax_ - xMin_)*(m_ - 1);
118 }
119
120
129 Scalar yToJ(Scalar y) const
130 {
131 return (y - yMin_)/(yMax_ - yMin_)*(n_ - 1);
132 }
133
134
140 Scalar getSamplePoint(int i, int j) const
141 {
142 assert(0 <= i && i < m_);
143 assert(0 <= j && j < n_);
144
145 return samples_[j*m_ + i];
146 }
147
153 void setSamplePoint(int i, int j, Scalar value)
154 {
155 assert(0 <= i && i < m_);
156 assert(0 <= j && j < n_);
157
158 samples_[j*m_ + i] = value;
159 }
160
164 Scalar get(Scalar x, Scalar y) const
165 {
166 Scalar alpha = xToI(x);
167 Scalar beta = yToJ(y);
168
169 using std::clamp;
170 int i = clamp(static_cast<int>(alpha), 0, m_ - 2);
171 int j = clamp(static_cast<int>(beta), 0, n_ - 2);
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:48
Tabulated2DFunction()
Default constructor.
Definition: tabulated2dfunction.hh:53
Scalar iToX(int i) const
Return the position on the x-axis of the i-th interval.
Definition: tabulated2dfunction.hh:90
Scalar xToI(Scalar x) const
Return the interval index of a given position on the x-axis.
Definition: tabulated2dfunction.hh:115
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:140
Tabulated2DFunction(Scalar xMin, Scalar xMax, int m, Scalar yMin, Scalar yMax, int n)
Constructor where the tabulation parameters are already provided.
Definition: tabulated2dfunction.hh:60
Scalar yToJ(Scalar y) const
Return the interval index of a given position on the y-axis.
Definition: tabulated2dfunction.hh:129
Scalar jToY(int j) const
Return the position on the y-axis of the j-th interval.
Definition: tabulated2dfunction.hh:100
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:164
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:153
void resize(Scalar xMin, Scalar xMax, int m, Scalar yMin, Scalar yMax, int n)
Resize the tabulation to a new range.
Definition: tabulated2dfunction.hh:72