28#ifndef OPM_INTERVAL_TABULATED_2D_FUNCTION_HPP
29#define OPM_INTERVAL_TABULATED_2D_FUNCTION_HPP
49template <
class Scalar>
56 template <
class DataContainer>
58 const std::vector<Scalar>&
yPos,
59 const DataContainer& data,
71 for (
unsigned i = 0; i <
xPos.size() - 1; ++ i) {
73 throw std::runtime_error(
"The array for the x-positions is not strictly increasing!");
76 for (
unsigned i = 0; i <
yPos.size() - 1; ++ i) {
78 throw std::runtime_error(
"The array for the y-positions is not strictly increasing!");
83 if (
numX() != samples_.size())
84 throw std::runtime_error(
"numX() is not equal to the number of rows of the sampling points");
86 for (
unsigned xIdx = 0; xIdx <
numX(); ++xIdx) {
87 if (samples_[xIdx].size() !=
numY()) {
88 std::ostringstream oss;
89 oss <<
"The " << xIdx <<
"-th row of the sampling points has different size than numY() ";
90 throw std::runtime_error(oss.str());
99 {
return xPos_.size(); }
105 {
return yPos_.size(); }
111 {
return xPos_.front(); }
117 {
return xPos_.back(); }
123 {
return yPos_.front(); }
129 {
return yPos_.back(); }
131 const std::vector<Scalar>&
xPos()
const
134 const std::vector<Scalar>&
yPos()
const
137 const std::vector<std::vector<Scalar>>&
samples()
const
141 {
return xExtrapolate_; }
144 {
return yExtrapolate_; }
147 return this->
xPos() == data.
xPos() &&
158 {
return samples_[i][j]; }
163 template <
class Evaluation>
164 bool applies(
const Evaluation& x,
const Evaluation& y)
const
170 template <
class Evaluation>
172 {
return xMin() <= x && x <=
xMax(); }
177 template <
class Evaluation>
179 {
return yMin() <= y && y <=
yMax(); }
189 template <
typename Evaluation>
190 Evaluation
eval(
const Evaluation& x,
const Evaluation& y)
const
193 std::ostringstream oss;
194 oss <<
"Attempt to get undefined table value (" << x <<
", " << y <<
")";
200 const unsigned i = xSegmentIndex_(x);
201 const unsigned j = ySegmentIndex_(y);
204 const Evaluation alpha = xToAlpha(x, i);
205 const Evaluation beta = yToBeta(y, j);
207 const Evaluation s1 =
valueAt(i, j) * (1.0 - beta) +
valueAt(i, j + 1) * beta;
208 const Evaluation s2 =
valueAt(i + 1, j) * (1.0 - beta) +
valueAt(i + 1, j + 1) * beta;
214 return s1*(1.0 - alpha) + s2*alpha;
219 std::vector<Scalar> xPos_;
221 std::vector<Scalar> yPos_;
223 std::vector<std::vector<Scalar> > samples_;
225 bool xExtrapolate_ =
false;
226 bool yExtrapolate_ =
false;
231 template <
class Evaluation>
232 unsigned xSegmentIndex_(
const Evaluation& x)
const
234 assert(xExtrapolate_ ||
appliesX(x) );
236 return segmentIndex_(x, xPos_);
242 template <
class Evaluation>
243 unsigned ySegmentIndex_(
const Evaluation& y)
const
245 assert(yExtrapolate_ ||
appliesY(y) );
247 return segmentIndex_(y, yPos_);
251 template <
class Evaluation>
252 static unsigned segmentIndex_(
const Evaluation& v,
const std::vector<Scalar>& vPos)
254 const unsigned n = vPos.size();
257 if (v <= vPos.front() || n == 2)
259 else if (v >= vPos.back())
262 assert(n > 2 && v > vPos.front() && v < vPos.back());
267 size_t upperIdx = vPos.size() - 1;
268 while (lowerIdx + 1 < upperIdx) {
269 size_t pivotIdx = (lowerIdx + upperIdx) / 2;
270 if (v < vPos[pivotIdx])
276 assert(vPos[lowerIdx] <= v);
277 assert(v <= vPos[lowerIdx + 1]);
287 template <
class Evaluation>
288 Evaluation xToAlpha(
const Evaluation& x,
unsigned xSegmentIdx)
const
290 Scalar x1 = xPos_[xSegmentIdx];
291 Scalar x2 = xPos_[xSegmentIdx + 1];
292 return (x - x1)/(x2 - x1);
301 template <
class Evaluation>
302 Evaluation yToBeta(
const Evaluation& y,
unsigned ySegmentIdx)
const
304 Scalar y1 = yPos_[ySegmentIdx];
305 Scalar y2 = yPos_[ySegmentIdx + 1];
306 return (y - y1)/(y2 - y1);
Provides the opm-material specific exception classes.
Some templates to wrap the valgrind client request macros.
Implements a function that depends on two variables.
Definition: IntervalTabulated2DFunction.hpp:51
size_t numY() const
Returns the number of sampling points in Y direction.
Definition: IntervalTabulated2DFunction.hpp:104
Evaluation eval(const Evaluation &x, const Evaluation &y) const
Evaluate the function at a given (x,y) position.
Definition: IntervalTabulated2DFunction.hpp:190
IntervalTabulated2DFunction()
Definition: IntervalTabulated2DFunction.hpp:53
bool appliesX(const Evaluation &x) const
Returns true if a coordinate lies in the tabulated range on the x direction.
Definition: IntervalTabulated2DFunction.hpp:171
Scalar xMin() const
Returns the minimum of the X coordinate of the sampling points.
Definition: IntervalTabulated2DFunction.hpp:110
IntervalTabulated2DFunction(const std::vector< Scalar > &xPos, const std::vector< Scalar > &yPos, const DataContainer &data, const bool xExtrapolate=false, const bool yExtrapolate=false)
Definition: IntervalTabulated2DFunction.hpp:57
const std::vector< Scalar > & xPos() const
Definition: IntervalTabulated2DFunction.hpp:131
bool xExtrapolate() const
Definition: IntervalTabulated2DFunction.hpp:140
Scalar yMin() const
Returns the minimum of the Y coordinate of the sampling points.
Definition: IntervalTabulated2DFunction.hpp:122
size_t numX() const
Returns the number of sampling points in X direction.
Definition: IntervalTabulated2DFunction.hpp:98
bool appliesY(const Evaluation &y) const
Returns true if a coordinate lies in the tabulated range on the y direction.
Definition: IntervalTabulated2DFunction.hpp:178
bool applies(const Evaluation &x, const Evaluation &y) const
Returns true if a coordinate lies in the tabulated range.
Definition: IntervalTabulated2DFunction.hpp:164
Scalar xMax() const
Returns the maximum of the X coordinate of the sampling points.
Definition: IntervalTabulated2DFunction.hpp:116
bool yExtrapolate() const
Definition: IntervalTabulated2DFunction.hpp:143
Scalar yMax() const
Returns the maximum of the Y coordinate of the sampling points.
Definition: IntervalTabulated2DFunction.hpp:128
Scalar valueAt(size_t i, size_t j) const
Returns the value of a sampling point.
Definition: IntervalTabulated2DFunction.hpp:157
bool operator==(const IntervalTabulated2DFunction< Scalar > &data) const
Definition: IntervalTabulated2DFunction.hpp:146
const std::vector< std::vector< Scalar > > & samples() const
Definition: IntervalTabulated2DFunction.hpp:137
const std::vector< Scalar > & yPos() const
Definition: IntervalTabulated2DFunction.hpp:134
Definition: Exceptions.hpp:46
bool CheckDefined(const T &value)
Make valgrind complain if any of the memory occupied by an object is undefined.
Definition: Valgrind.hpp:74
Definition: Air_Mesitylene.hpp:34