version 3.10-dev
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// SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
12#ifndef DUMUX_TABULATED_2D_FUNCTION_HH
13#define DUMUX_TABULATED_2D_FUNCTION_HH
14
15#include <vector>
16#include <cassert>
17#include <algorithm>
18
19namespace Dumux {
20
34template<class Scalar>
36{
37public:
42 { };
43
48 Tabulated2DFunction(Scalar xMin, Scalar xMax, int m,
49 Scalar yMin, Scalar yMax, int n)
50 {
51 resize(xMin, xMax, m, yMin, yMax, n);
52 };
53
60 void resize(Scalar xMin, Scalar xMax, int m,
61 Scalar yMin, Scalar yMax, int n)
62 {
63 samples_.resize(m*n);
64
65 m_ = m;
66 n_ = n;
67
68 xMin_ = xMin;
69 xMax_ = xMax;
70
71 yMin_ = yMin;
72 yMax_ = yMax;
73 }
74
78 Scalar iToX(int i) const
79 {
80 assert(0 <= i && i < m_);
81
82 return xMin_ + i*(xMax_ - xMin_)/(m_ - 1);
83 }
84
88 Scalar jToY(int j) const
89 {
90 assert(0 <= j && j < n_);
91
92 return yMin_ + j*(yMax_ - yMin_)/(n_ - 1);
93 }
94
103 Scalar xToI(Scalar x) const
104 {
105 return (x - xMin_)/(xMax_ - xMin_)*(m_ - 1);
106 }
107
108
117 Scalar yToJ(Scalar y) const
118 {
119 return (y - yMin_)/(yMax_ - yMin_)*(n_ - 1);
120 }
121
122
128 Scalar getSamplePoint(int i, int j) const
129 {
130 assert(0 <= i && i < m_);
131 assert(0 <= j && j < n_);
132
133 return samples_[j*m_ + i];
134 }
135
141 void setSamplePoint(int i, int j, Scalar value)
142 {
143 assert(0 <= i && i < m_);
144 assert(0 <= j && j < n_);
145
146 samples_[j*m_ + i] = value;
147 }
148
152 Scalar get(Scalar x, Scalar y) const
153 {
154 Scalar alpha = xToI(x);
155 Scalar beta = yToJ(y);
156
157 using std::clamp;
158 int i = clamp(static_cast<int>(alpha), 0, m_ - 2);
159 int j = clamp(static_cast<int>(beta), 0, n_ - 2);
160
161 alpha -= i;
162 beta -= j;
163
164 // bi-linear interpolation
165 Scalar s1 = getSamplePoint(i, j)*(1.0 - alpha) + getSamplePoint(i + 1, j)*alpha;
166 Scalar s2 = getSamplePoint(i, j + 1)*(1.0 - alpha) + getSamplePoint(i + 1, j + 1)*alpha;
167 return s1*(1.0 - beta) + s2*beta;
168 }
169
175 Scalar operator()(Scalar x, Scalar y) const
176 { return get(x, y); }
177
178private:
179 // the vector which contains the values of the sample points
180 // f(x_i, y_j). don't use this directly, use getSamplePoint(i,j)
181 // instead!
182 std::vector<Scalar> samples_;
183
184 // the number of sample points in x direction
185 int m_;
186
187 // the number of sample points in y direction
188 int n_;
189
190 // the range of the tabulation on the x axis
191 Scalar xMin_;
192 Scalar xMax_;
193
194 // the range of the tabulation on the y axis
195 Scalar yMin_;
196 Scalar yMax_;
197
198};
199
200} // end namespace Dumux
201
202#endif
Implements tabulation for a two-dimensional function.
Definition: tabulated2dfunction.hh:36
Tabulated2DFunction()
Default constructor.
Definition: tabulated2dfunction.hh:41
Scalar iToX(int i) const
Return the position on the x-axis of the i-th interval.
Definition: tabulated2dfunction.hh:78
Scalar xToI(Scalar x) const
Return the interval index of a given position on the x-axis.
Definition: tabulated2dfunction.hh:103
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:128
Tabulated2DFunction(Scalar xMin, Scalar xMax, int m, Scalar yMin, Scalar yMax, int n)
Constructor where the tabulation parameters are already provided.
Definition: tabulated2dfunction.hh:48
Scalar yToJ(Scalar y) const
Return the interval index of a given position on the y-axis.
Definition: tabulated2dfunction.hh:117
Scalar jToY(int j) const
Return the position on the y-axis of the j-th interval.
Definition: tabulated2dfunction.hh:88
Scalar operator()(Scalar x, Scalar y) const
The () operator.
Definition: tabulated2dfunction.hh:175
Scalar get(Scalar x, Scalar y) const
Return an interpolated value.
Definition: tabulated2dfunction.hh:152
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:141
void resize(Scalar xMin, Scalar xMax, int m, Scalar yMin, Scalar yMax, int n)
Resize the tabulation to a new range.
Definition: tabulated2dfunction.hh:60
Definition: adapt.hh:17