diff --git a/opm/autodiff/AutoDiffHelpers.hpp b/opm/autodiff/AutoDiffHelpers.hpp index 86e55bb4a..8ac663727 100644 --- a/opm/autodiff/AutoDiffHelpers.hpp +++ b/opm/autodiff/AutoDiffHelpers.hpp @@ -263,6 +263,12 @@ namespace { constructSupersetSparseMatrix(const int full_size, const IntVec& indices) { const int subset_size = indices.size(); + + if (subset_size == 0) { + typename AutoDiffBlock::M mat(full_size, 0); + return mat; + } + typename AutoDiffBlock::M mat(full_size, subset_size); mat.reserve(Eigen::VectorXi::Constant(subset_size, 1)); for (int i = 0; i < subset_size; ++i) { diff --git a/tests/test_autodiffhelpers.cpp b/tests/test_autodiffhelpers.cpp index 7afdbea6f..9b04159e1 100644 --- a/tests/test_autodiffhelpers.cpp +++ b/tests/test_autodiffhelpers.cpp @@ -144,3 +144,75 @@ BOOST_AUTO_TEST_CASE(vertcatCollapseJacsTest) BOOST_CHECK((x.value() == expected_val).all()); BOOST_CHECK(x.derivative()[0] == expected_jac); } + + +BOOST_AUTO_TEST_CASE(supersetTest) +{ + typedef AutoDiffBlock ADB; + + { + ADB subset = ADB::constant(ADB::V::Ones(3)); + const int full_size = 32; + std::vector indices = {1, 3, 5}; + + AutoDiffBlock n_vals = superset(subset, indices, full_size); + BOOST_CHECK_EQUAL(n_vals.size(), full_size); + for (int i=0; i ADB; + + { + ADB subset = ADB::constant(ADB::V::Ones(0)); + const int full_size = 32; + std::vector indices = {}; + + AutoDiffBlock n_vals = superset(subset, indices, full_size); + BOOST_CHECK_EQUAL(n_vals.size(), full_size); + for (int i=0; i ADB; + + { + ADB superset = ADB::constant(ADB::V::Ones(32)); + std::vector indices = {1, 3, 5}; + + AutoDiffBlock n_vals = subset(superset, indices); + BOOST_CHECK_EQUAL(n_vals.size(), 3); + for (int i=0; i ADB; + + { + ADB superset = ADB::constant(ADB::V::Ones(32)); + std::vector indices = {}; + + AutoDiffBlock n_vals = subset(superset, indices); + BOOST_CHECK_EQUAL(n_vals.size(), 0); + } +} +