From 9bf6d01acc68b390eef79c1c969c7abc17f0a07f Mon Sep 17 00:00:00 2001 From: Kai Bao Date: Thu, 6 Dec 2018 13:19:04 +0100 Subject: [PATCH] adding a test for XYTabulated2DFunction --- opm/material/common/XYTabulated2DFunction.hpp | 4 +- tests/test_2dtables.cpp | 92 +++++++++++++++++++ 2 files changed, 94 insertions(+), 2 deletions(-) diff --git a/opm/material/common/XYTabulated2DFunction.hpp b/opm/material/common/XYTabulated2DFunction.hpp index 26c2d4072..8bd01c682 100644 --- a/opm/material/common/XYTabulated2DFunction.hpp +++ b/opm/material/common/XYTabulated2DFunction.hpp @@ -57,8 +57,8 @@ public: XYTabulated2DFunction(const std::vector& x_pos, const std::vector& y_pos, const std::vector >& data, - const bool x_extrapolate, - const bool y_extrapolate) + const bool x_extrapolate = false, + const bool y_extrapolate = false) : xPos_(x_pos) , yPos_(y_pos) , samples_(data) diff --git a/tests/test_2dtables.cpp b/tests/test_2dtables.cpp index bd1af46a2..943d01b7e 100644 --- a/tests/test_2dtables.cpp +++ b/tests/test_2dtables.cpp @@ -31,6 +31,7 @@ #include #include +#include #include @@ -132,6 +133,37 @@ createUniformXTabulatedFunction2(Fn& f) return tab; } + +template +std::shared_ptr > +createXYTabulated2DFunction(Fn& f) +{ + const Scalar xMin = -2.0; + const Scalar xMax = 3.0; + const Scalar m = 50; + + const Scalar yMin = -1/2.0; + const Scalar yMax = 1/3.0; + const unsigned n = 40; + + std::vector x_samples(m); + std::vector y_samples(n); + std::vector> data(m, std::vector(n)); + + for (unsigned i = 0; i < m; ++i) { + x_samples[i] = xMin + Scalar(i)/(m - 1) * (xMax - xMin); + + for (unsigned j = 0; j < n; ++j) { + y_samples[j] = yMin + Scalar(j)/(n -1) * (yMax - yMin); + data[i][j] = f(x_samples[i], y_samples[j]); + } + } + + auto tab = std::make_shared>(x_samples, y_samples, data, true, true); + + return tab; +} + template bool compareTableWithAnalyticFn(const Table& table, Scalar xMin, @@ -162,6 +194,37 @@ bool compareTableWithAnalyticFn(const Table& table, return true; } +template +bool compareTableWithAnalyticFn2(const Table& table, + const Scalar xMin, + const Scalar xMax, + unsigned numX, + const Scalar yMin, + const Scalar yMax, + unsigned numY, + + Fn& f, + Scalar tolerance = 1e-8) +{ + // make sure that the tabulated function evaluates to the same thing as the analytic + // one (modulo tolerance) + for (unsigned i = 1; i <= numX; ++i) { + Scalar x = xMin + Scalar(i)/numX*(xMax - xMin); + + for (unsigned j = 0; j < numY; ++j) { + Scalar y = yMin + Scalar(j)/numY*(yMax - yMin); + Scalar result; + table->eval(x, y, result); + if (std::abs(result - f(x, y)) > tolerance) { + std::cerr << __FILE__ << ":" << __LINE__ << ": table->eval("< bool compareTables(const UniformTablePtr uTable, const UniformXTablePtr uXTable, @@ -358,6 +421,35 @@ inline int testAll( const typename TestType::Scalar tolerance = 1e-6 ) /*tolerance=*/1e-2)) return 1; + { + using ScalarType = typename TestType::Scalar; + + auto xytab = test.createXYTabulated2DFunction(TestType::testFn1); + const ScalarType xMin = -4.0; + const ScalarType xMax = 8.0; + const unsigned m = 250; + + const ScalarType yMin = -2. / 2.0; + const ScalarType yMax = 3. / 3.0; + const unsigned n = 170; + + // extrapolation and interpolation involved, the tolerance needs to be bigger + const ScalarType temp_tolerance = 1000. * tolerance; + + if (!test.compareTableWithAnalyticFn2(xytab, xMin, xMax, m, yMin, yMax, n, TestType::testFn1, temp_tolerance)) + return 1; + + xytab = test.createXYTabulated2DFunction(TestType::testFn2); + + if (!test.compareTableWithAnalyticFn2(xytab, xMin, xMax, m, yMin, yMax, n, TestType::testFn2, temp_tolerance)) + return 1; + + xytab = test.createXYTabulated2DFunction(TestType::testFn3); + + if (!test.compareTableWithAnalyticFn2(xytab, xMin, xMax, m, yMin, yMax, n, TestType::testFn3, temp_tolerance)) + return 1; + } + // CSV output for debugging #if 0 int m = 100;