test_2dtables: convert to boost::test

This commit is contained in:
Arne Morten Kvarving
2023-05-25 22:53:25 +02:00
parent 9e10a598f1
commit e650889965

View File

@@ -29,6 +29,9 @@
*/
#include "config.h"
#define BOOST_TEST_MODULE 2DTables
#include <boost/test/unit_test.hpp>
#include <opm/material/common/UniformXTabulated2DFunction.hpp>
#include <opm/material/common/UniformTabulated2DFunction.hpp>
#include <opm/material/common/IntervalTabulated2DFunction.hpp>
@@ -52,7 +55,7 @@ struct Test
{ return x*y; }
template <class Fn>
std::shared_ptr<Opm::UniformTabulated2DFunction<Scalar> >
Opm::UniformTabulated2DFunction<Scalar>
createUniformTabulatedFunction(Fn& f)
{
Scalar xMin = -2.0;
@@ -63,14 +66,13 @@ struct Test
Scalar yMax = 1/3.0;
unsigned n = 40;
auto tab = std::make_shared<Opm::UniformTabulated2DFunction<Scalar>>(
xMin, xMax, m,
yMin, yMax, n);
Opm::UniformTabulated2DFunction<Scalar> tab(xMin, xMax, m, yMin, yMax, n);
for (unsigned i = 0; i < m; ++i) {
Scalar x = xMin + Scalar(i)/(m - 1) * (xMax - xMin);
for (unsigned j = 0; j < n; ++j) {
Scalar y = yMin + Scalar(j)/(n - 1) * (yMax - yMin);
tab->setSamplePoint(i, j, f(x, y));
tab.setSamplePoint(i, j, f(x, y));
}
}
@@ -78,7 +80,7 @@ struct Test
}
template <class Fn>
std::shared_ptr<Opm::UniformXTabulated2DFunction<Scalar> >
Opm::UniformXTabulated2DFunction<Scalar>
createUniformXTabulatedFunction(Fn& f)
{
Scalar xMin = -2.0;
@@ -88,13 +90,13 @@ struct Test
Scalar yMin = -1/2.0;
Scalar yMax = 1/3.0;
unsigned n = 40;
auto tab = std::make_shared<Opm::UniformXTabulated2DFunction<Scalar>>(Opm::UniformXTabulated2DFunction<Scalar>::InterpolationPolicy::Vertical);
Opm::UniformXTabulated2DFunction<Scalar> tab(Opm::UniformXTabulated2DFunction<Scalar>::InterpolationPolicy::Vertical);
for (unsigned i = 0; i < m; ++i) {
Scalar x = xMin + Scalar(i)/(m - 1) * (xMax - xMin);
tab->appendXPos(x);
tab.appendXPos(x);
for (unsigned j = 0; j < n; ++j) {
Scalar y = yMin + Scalar(j)/(n -1) * (yMax - yMin);
tab->appendSamplePoint(i, y, f(x, y));
tab.appendSamplePoint(i, y, f(x, y));
}
}
@@ -103,7 +105,7 @@ struct Test
template <class Fn>
std::shared_ptr<Opm::UniformXTabulated2DFunction<Scalar> >
Opm::UniformXTabulated2DFunction<Scalar>
createUniformXTabulatedFunction2(Fn& f)
{
Scalar xMin = -2.0;
@@ -113,17 +115,17 @@ struct Test
Scalar yMin = - 4.0;
Scalar yMax = 5.0;
auto tab = std::make_shared<Opm::UniformXTabulated2DFunction<Scalar>>(Opm::UniformXTabulated2DFunction<Scalar>::InterpolationPolicy::Vertical);
Opm::UniformXTabulated2DFunction<Scalar> tab(Opm::UniformXTabulated2DFunction<Scalar>::InterpolationPolicy::Vertical);
for (unsigned i = 0; i < m; ++i) {
Scalar x = xMin + Scalar(i)/(m - 1) * (xMax - xMin);
tab->appendXPos(x);
tab.appendXPos(x);
Scalar n = i + 10;
for (unsigned j = 0; j < n; ++j) {
Scalar y = yMin + Scalar(j)/(n -1) * (yMax - yMin);
tab->appendSamplePoint(i, y, f(x, y));
tab.appendSamplePoint(i, y, f(x, y));
}
}
@@ -132,7 +134,7 @@ struct Test
template <class Fn>
std::shared_ptr<Opm::IntervalTabulated2DFunction<Scalar> >
Opm::IntervalTabulated2DFunction<Scalar>
createIntervalTabulated2DFunction(Fn& f)
{
const Scalar xMin = -2.0;
@@ -156,7 +158,7 @@ struct Test
}
}
return std::make_shared<Opm::IntervalTabulated2DFunction<Scalar>>(xSamples, ySamples, data, true, true);
return Opm::IntervalTabulated2DFunction<Scalar>(xSamples, ySamples, data, true, true);
}
template <class Fn, class Table>
@@ -179,10 +181,7 @@ struct Test
for (unsigned j = 0; j < numY; ++j) {
Scalar y = yMin + Scalar(j)/numY*(yMax - yMin);
if (std::abs(table->eval(x, y, false) - f(x, y)) > tolerance) {
std::cerr << __FILE__ << ":" << __LINE__ << ": table->eval("<<x<<","<<y<<") != f("<<x<<","<<y<<"): " << table->eval(x,y, false) << " != " << f(x,y) << "\n";
return false;
}
BOOST_CHECK_SMALL(table.eval(x, y, false) - f(x, y), tolerance);
}
}
@@ -197,7 +196,6 @@ struct Test
const Scalar yMin,
const Scalar yMax,
unsigned numY,
Fn& f,
Scalar tolerance = 1e-8)
{
@@ -208,273 +206,201 @@ struct Test
for (unsigned j = 0; j < numY; ++j) {
Scalar y = yMin + Scalar(j)/numY*(yMax - yMin);
Scalar result = table->eval(x, y);
if (std::abs(result - f(x, y)) > tolerance) {
std::cerr << __FILE__ << ":" << __LINE__ << ": table->eval("<<x<<","<<y<<") != f("<<x<<","<<y<<"): " << result << " != " << f(x,y) << "\n";
return false;
}
BOOST_CHECK_SMALL(table.eval(x, y) - f(x, y), tolerance);
}
}
return true;
}
template <class UniformTablePtr, class UniformXTablePtr, class Fn>
bool compareTables(const UniformTablePtr uTable,
const UniformXTablePtr uXTable,
template <class UniformTable, class UniformXTable, class Fn>
void compareTables(const UniformTable& uTable,
const UniformXTable& uXTable,
Fn& f,
Scalar tolerance = 1e-8)
{
// make sure the uniform and the non-uniform tables exhibit the same dimensions
if (std::abs(uTable->xMin() - uXTable->xMin()) > tolerance) {
std::cerr << __FILE__ << ":" << __LINE__ << ": uTable->xMin() != uXTable->xMin(): " << uTable->xMin() << " != " << uXTable->xMin() << "\n";
return false;
}
if (std::abs(uTable->xMax() - uXTable->xMax()) > tolerance) {
std::cerr << __FILE__ << ":" << __LINE__ << ": uTable->xMax() != uXTable->xMax(): " << uTable->xMax() << " != " << uXTable->xMax() << "\n";
return false;
}
if (uTable->numX() != uXTable->numX()) {
std::cerr << __FILE__ << ":" << __LINE__ << ": uTable->numX() != uXTable->numX(): " << uTable->numX() << " != " << uXTable->numX() << "\n";
return false;
}
for (unsigned i = 0; i < uTable->numX(); ++i) {
if (std::abs(uTable->yMin() - uXTable->yMin(i)) > tolerance) {
std::cerr << __FILE__ << ":" << __LINE__ << ": uTable->yMin() != uXTable->yMin("<<i<<"): " << uTable->yMin() << " != " << uXTable->yMin(i) << "\n";
return false;
}
if (std::abs(uTable->yMax() - uXTable->yMax(i)) > tolerance) {
std::cerr << __FILE__ << ":" << __LINE__ << ": uTable->yMax() != uXTable->yMax("<<i<<"): " << uTable->yMax() << " != " << uXTable->yMax(i) << "\n";
return false;
}
if (uTable->numY() != uXTable->numY(i)) {
std::cerr << __FILE__ << ":" << __LINE__ << ": uTable->numY() != uXTable->numY("<<i<<"): " << uTable->numY() << " != " << uXTable->numY(i) << "\n";
return false;
}
BOOST_CHECK_SMALL(uTable.xMin() - uXTable.xMin(), tolerance);
BOOST_CHECK_SMALL(uTable.xMax() - uXTable.xMax(), tolerance);
BOOST_CHECK_EQUAL(uTable.numX(), uXTable.numX());
for (unsigned i = 0; i < uTable.numX(); ++i) {
BOOST_CHECK_SMALL(uTable.yMin() - uXTable.yMin(i), tolerance);
BOOST_CHECK_SMALL(uTable.yMax() - uXTable.yMax(i), tolerance);
BOOST_CHECK_EQUAL(uTable.numY(), uXTable.numY(i));
}
// make sure that the x and y values are identical
for (unsigned i = 0; i < uTable->numX(); ++i) {
if (std::abs(uTable->iToX(i) - uXTable->iToX(i)) > tolerance) {
std::cerr << __FILE__ << ":" << __LINE__ << ": uTable->iToX("<<i<<") != uXTable->iToX("<<i<<"): " << uTable->iToX(i) << " != " << uXTable->iToX(i) << "\n";
return false;
}
for (unsigned j = 0; j < uTable->numY(); ++j) {
if (std::abs(uTable->jToY(j) - uXTable->jToY(i, j)) > tolerance) {
std::cerr << __FILE__ << ":" << __LINE__ << ": uTable->jToY("<<j<<") != uXTable->jToY("<<i<<","<<j<<"): " << uTable->jToY(i) << " != " << uXTable->jToY(i, j) << "\n";
return false;
}
for (unsigned i = 0; i < uTable.numX(); ++i) {
BOOST_CHECK_SMALL(uTable.iToX(i) - uXTable.iToX(i), tolerance);
for (unsigned j = 0; j < uTable.numY(); ++j) {
BOOST_CHECK_SMALL(uTable.jToY(j) - uXTable.jToY(i,j), tolerance);
}
}
// check that the appicable range is correct. Note that due to rounding errors it is
// check that the applicable range is correct. Note that due to rounding errors it is
// undefined whether the table applies to the boundary of the tabulated domain or not
Scalar xMin = uTable->xMin();
Scalar yMin = uTable->yMin();
Scalar xMax = uTable->xMax();
Scalar yMax = uTable->yMax();
Scalar xMin = uTable.xMin();
Scalar yMin = uTable.yMin();
Scalar xMax = uTable.xMax();
Scalar yMax = uTable.yMax();
Scalar x = xMin - tolerance;
Scalar y = yMin - tolerance;
if (uTable->applies(x, y)) {
std::cerr << __FILE__ << ":" << __LINE__ << ": uTable->applies("<<x<<","<<y<<")\n";
return false;
}
if (uXTable->applies(x, y)) {
std::cerr << __FILE__ << ":" << __LINE__ << ": uXTable->applies("<<x<<","<<y<<")\n";
return false;
for (int yMod = -1; yMod < 2; yMod += 2) {
for (int xMod = -1; xMod < 2; xMod += 2) {
Scalar x = xMin + xMod * tolerance;
Scalar y = yMin + yMod * tolerance;
BOOST_CHECK_MESSAGE(uTable.applies(x,y) == (xMod > 0 && yMod > 0),
"uTable.applies(" << x << "," << y << ")");
BOOST_CHECK_MESSAGE(uTable.applies(x,y) == (xMod > 0 && yMod > 0),
"uXTable.applies(" << x << "," << y << ")");
}
}
x = xMin - tolerance;
y = yMin + tolerance;
if (uTable->applies(x, y)) {
std::cerr << __FILE__ << ":" << __LINE__ << ": uTable->applies("<<x<<","<<y<<")\n";
return false;
}
if (uXTable->applies(x, y)) {
std::cerr << __FILE__ << ":" << __LINE__ << ": uXTable->applies("<<x<<","<<y<<")\n";
return false;
}
x = xMin + tolerance;
y = yMin - tolerance;
if (uTable->applies(x, y)) {
std::cerr << __FILE__ << ":" << __LINE__ << ": uTable->applies("<<x<<","<<y<<")\n";
return false;
}
if (uXTable->applies(x, y)) {
std::cerr << __FILE__ << ":" << __LINE__ << ": uXTable->applies("<<x<<","<<y<<")\n";
return false;
}
x = xMin + tolerance;
y = yMin + tolerance;
if (!uTable->applies(x, y)) {
std::cerr << __FILE__ << ":" << __LINE__ << ": !uTable->applies("<<x<<","<<y<<")\n";
return false;
}
if (!uXTable->applies(x, y)) {
std::cerr << __FILE__ << ":" << __LINE__ << ": !uXTable->applies("<<x<<","<<y<<")\n";
return false;
}
x = xMax + tolerance;
y = yMax + tolerance;
if (uTable->applies(x, y)) {
std::cerr << __FILE__ << ":" << __LINE__ << ": uTable->applies("<<x<<","<<y<<")\n";
return false;
}
if (uXTable->applies(x, y)) {
std::cerr << __FILE__ << ":" << __LINE__ << ": uXTable->applies("<<x<<","<<y<<")\n";
return false;
}
x = xMax - tolerance;
y = yMax + tolerance;
if (uTable->applies(x, y)) {
std::cerr << __FILE__ << ":" << __LINE__ << ": uTable->applies("<<x<<","<<y<<")\n";
return false;
}
if (uXTable->applies(x, y)) {
std::cerr << __FILE__ << ":" << __LINE__ << ": uXTable->applies("<<x<<","<<y<<")\n";
return false;
}
x = xMax + tolerance;
y = yMax - tolerance;
if (uTable->applies(x, y)) {
std::cerr << __FILE__ << ":" << __LINE__ << ": uTable->applies("<<x<<","<<y<<")\n";
return false;
}
if (uXTable->applies(x, y)) {
std::cerr << __FILE__ << ":" << __LINE__ << ": uXTable->applies("<<x<<","<<y<<")\n";
return false;
}
x = xMax - tolerance;
y = yMax - tolerance;
if (!uTable->applies(x, y)) {
std::cerr << __FILE__ << ":" << __LINE__ << ": !uTable->applies("<<x<<","<<y<<")\n";
return false;
}
if (!uXTable->applies(x, y)) {
std::cerr << __FILE__ << ":" << __LINE__ << ": !uXTable->applies("<<x<<","<<y<<")\n";
return false;
for (int xMod = -1; xMod < 2; xMod += 2) {
for (int yMod = -1; yMod < 2; yMod += 2) {
Scalar x = xMax + xMod * tolerance;
Scalar y = yMax + yMod * tolerance;
BOOST_CHECK_MESSAGE(uTable.applies(x,y) == (xMod < 0 && yMod < 0),
"uTable.applies(" << x << "," << y << ")");
BOOST_CHECK_MESSAGE(uXTable.applies(x,y) == (xMod < 0 && yMod < 0),
"uXTable.applies(" << x << "," << y << + ")");
}
}
// make sure that the function values at the sampling points are identical and that
// they correspond to the analytic function
unsigned m2 = uTable->numX()*5;
unsigned n2 = uTable->numY()*5;
if (!compareTableWithAnalyticFn(uTable,
xMin, xMax, m2,
yMin, yMax, n2,
f,
tolerance))
return false;
if (!compareTableWithAnalyticFn(uXTable,
xMin, xMax, m2,
yMin, yMax, n2,
f,
tolerance))
return false;
unsigned m2 = uTable.numX()*5;
unsigned n2 = uTable.numY()*5;
compareTableWithAnalyticFn(uTable,
xMin, xMax, m2,
yMin, yMax, n2,
f,
tolerance);
return true;
compareTableWithAnalyticFn(uXTable,
xMin, xMax, m2,
yMin, yMax, n2,
f,
tolerance);
}
};
using Types = std::tuple<float,double>;
template <class TestType>
inline int testAll(const typename TestType::Scalar tolerance = 1e-6)
BOOST_AUTO_TEST_CASE_TEMPLATE(UniformTabulatedFunction1, Scalar, Types)
{
TestType test;
auto uniformTab = test.createUniformTabulatedFunction(TestType::testFn1);
auto uniformXTab = test.createUniformXTabulatedFunction(TestType::testFn1);
if (!test.compareTables(uniformTab, uniformXTab, TestType::testFn1, tolerance))
return 1;
uniformTab = test.createUniformTabulatedFunction(TestType::testFn2);
uniformXTab = test.createUniformXTabulatedFunction(TestType::testFn2);
if (!test.compareTables(uniformTab, uniformXTab, TestType::testFn2, tolerance))
return 1;
uniformTab = test.createUniformTabulatedFunction(TestType::testFn3);
uniformXTab = test.createUniformXTabulatedFunction(TestType::testFn3);
if (!test.compareTables(uniformTab, uniformXTab, TestType::testFn3, /*tolerance=*/1e-2))
return 1;
uniformXTab = test.createUniformXTabulatedFunction2(TestType::testFn3);
if (!test.compareTableWithAnalyticFn(uniformXTab,
-2.0, 3.0, 100,
-4.0, 5.0, 100,
TestType::testFn3,
/*tolerance=*/1e-2))
return 1;
{
using ScalarType = typename TestType::Scalar;
auto xytab = test.createIntervalTabulated2DFunction(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 tmpTolerance = 1000. * tolerance;
if (!test.compareTableWithAnalyticFn2(xytab, xMin, xMax, m, yMin, yMax, n, TestType::testFn1, tmpTolerance))
return 1;
xytab = test.createIntervalTabulated2DFunction(TestType::testFn2);
if (!test.compareTableWithAnalyticFn2(xytab, xMin, xMax, m, yMin, yMax, n, TestType::testFn2, tmpTolerance))
return 1;
xytab = test.createIntervalTabulated2DFunction(TestType::testFn3);
if (!test.compareTableWithAnalyticFn2(xytab, xMin, xMax, m, yMin, yMax, n, TestType::testFn3, tmpTolerance))
return 1;
Test<Scalar> test;
Scalar tolerance;
if constexpr (std::is_same_v<Scalar,float>) {
tolerance = 1e-5;
} else {
tolerance = 1e-11;
}
// CSV output for debugging
#if 0
int m = 100;
int n = 100;
Scalar xMin = -3.0;
Scalar xMax = 4.0;
Scalar yMin = -1;
Scalar yMax = 1;
for (int i = 0; i < m; ++i) {
Scalar x = xMin + Scalar(i)/m * (xMax - xMin);
for (int j = 0; j < n; ++j) {
Scalar y = yMin + Scalar(j)/n * (yMax - yMin);
std::cout << x << " "
<< y << " "
<< uniformXTab->eval(x,y,true) << "\n";
}
std::cout << "\n";
}
#endif
return 0;
auto uniformTab = test.createUniformTabulatedFunction(test.testFn1);
auto uniformXTab = test.createUniformXTabulatedFunction(test.testFn1);
test.compareTables(uniformTab, uniformXTab, test.testFn1, tolerance);
}
int main()
BOOST_AUTO_TEST_CASE_TEMPLATE(UniformTabulatedFunction2, Scalar, Types)
{
if (testAll<Test<double> >(1e-12))
return 1;
if (testAll<Test<float> >(1e-6))
return 1;
return 0;
Test<Scalar> test;
Scalar tolerance;
if constexpr (std::is_same_v<Scalar,float>) {
tolerance = 1e-6;
} else {
tolerance = 1e-12;
}
auto uniformTab = test.createUniformTabulatedFunction(test.testFn2);
auto uniformXTab = test.createUniformXTabulatedFunction(test.testFn2);
test.compareTables(uniformTab, uniformXTab, test.testFn2, tolerance);
}
BOOST_AUTO_TEST_CASE_TEMPLATE(UniformTabulatedFunction3, Scalar, Types)
{
Test<Scalar> test;
auto uniformTab = test.createUniformTabulatedFunction(test.testFn3);
auto uniformXTab = test.createUniformXTabulatedFunction(test.testFn3);
test.compareTables(uniformTab, uniformXTab, test.testFn3, 1e-2);
}
BOOST_AUTO_TEST_CASE_TEMPLATE(UniformXTabulatedFunction2, Scalar, Types)
{
Test<Scalar> test;
auto uniformXTab = test.createUniformXTabulatedFunction2(test.testFn3);
test.compareTableWithAnalyticFn(uniformXTab,
-2.0, 3.0, 100,
-4.0, 5.0, 100,
test.testFn3,
1e-2);
}
BOOST_AUTO_TEST_CASE_TEMPLATE(IntervalTabulatedFunction1, Scalar, Types)
{
Test<Scalar> test;
const Scalar xMin = -4.0;
const Scalar xMax = 8.0;
const unsigned m = 250;
const Scalar yMin = -2. / 2.0;
const Scalar yMax = 3. / 3.0;
const unsigned n = 170;
// extrapolation and interpolation involved, the tolerance needs to be bigger
Scalar tolerance;
if constexpr (std::is_same_v<Scalar,float>) {
tolerance = 1e-3;
} else {
tolerance = 1e-9;
}
auto xytab = test.createIntervalTabulated2DFunction(test.testFn1);
test.compareTableWithAnalyticFn2(xytab, xMin, xMax, m,
yMin, yMax, n, test.testFn1, tolerance);
}
BOOST_AUTO_TEST_CASE_TEMPLATE(IntervalTabulatedFunction2, Scalar, Types)
{
Test<Scalar> test;
const Scalar xMin = -4.0;
const Scalar xMax = 8.0;
const unsigned m = 250;
const Scalar yMin = -2. / 2.0;
const Scalar yMax = 3. / 3.0;
const unsigned n = 170;
// extrapolation and interpolation involved, the tolerance needs to be bigger
Scalar tolerance;
if constexpr (std::is_same_v<Scalar,float>) {
tolerance = 1e-3;
} else {
tolerance = 1e-9;
}
auto xytab = test.createIntervalTabulated2DFunction(test.testFn2);
test.compareTableWithAnalyticFn2(xytab, xMin, xMax, m,
yMin, yMax, n, test.testFn2, tolerance);
}
BOOST_AUTO_TEST_CASE_TEMPLATE(IntervalTabulatedFunction3, Scalar, Types)
{
Test<Scalar> test;
const Scalar xMin = -4.0;
const Scalar xMax = 8.0;
const unsigned m = 250;
const Scalar yMin = -2. / 2.0;
const Scalar yMax = 3. / 3.0;
const unsigned n = 170;
// extrapolation and interpolation involved, the tolerance needs to be bigger
Scalar tolerance;
if constexpr (std::is_same_v<Scalar,float>) {
tolerance = 1e-3;
} else {
tolerance = 1e-9;
}
auto xytab = test.createIntervalTabulated2DFunction(test.testFn3);
test.compareTableWithAnalyticFn2(xytab, xMin, xMax, m,
yMin, yMax, n, test.testFn3, tolerance);
}