From 6d18b3b7e663de00cb245146070966cd269f5e28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A5rd=20Skaflestad?= Date: Wed, 10 Mar 2021 09:54:14 +0100 Subject: [PATCH] ActiveIndexByColumn: Remove 'build' Method Do construction work in constructor instead. Suggested by: [at]joakim-hove --- opm/output/eclipse/ActiveIndexByColumns.hpp | 9 ++- .../output/eclipse/ActiveIndexByColumns.cpp | 61 +++++++++++-------- tests/test_ActiveIndexByColumns.cpp | 44 +++++++------ 3 files changed, 63 insertions(+), 51 deletions(-) diff --git a/opm/output/eclipse/ActiveIndexByColumns.hpp b/opm/output/eclipse/ActiveIndexByColumns.hpp index bbca5450f..fb24ef881 100644 --- a/opm/output/eclipse/ActiveIndexByColumns.hpp +++ b/opm/output/eclipse/ActiveIndexByColumns.hpp @@ -46,9 +46,9 @@ public: /// \param[in] cartDims Model's Cartesian dimensions. /// \param[in] getIJK Call-back routine for retrieving the Cartesian /// (I,J,K) tuple of an active cell index. - void buildMappingTables(const std::size_t numActive, - const std::array& cartDims, - const std::function(const std::size_t)>& getIJK); + explicit ActiveIndexByColumns(const std::size_t numActive, + const std::array& cartDims, + const std::function(const std::size_t)>& getIJK); /// Map active index in natural order to active index in columnar order. /// @@ -67,8 +67,7 @@ private: }; /// Build natural->columnar active cell index mapping from an EclipseGrid instance. -void buildColumnarActiveIndexMappingTables(const EclipseGrid& grid, - ActiveIndexByColumns& map); +ActiveIndexByColumns buildColumnarActiveIndexMappingTables(const EclipseGrid& grid); } // namespace Opm diff --git a/src/opm/output/eclipse/ActiveIndexByColumns.cpp b/src/opm/output/eclipse/ActiveIndexByColumns.cpp index 7227f56b4..61be46d36 100644 --- a/src/opm/output/eclipse/ActiveIndexByColumns.cpp +++ b/src/opm/output/eclipse/ActiveIndexByColumns.cpp @@ -46,6 +46,31 @@ namespace { // return ijk[2] + dims[2]*(ijk[1] + dims[1]*ijk[0]); } + + std::vector + buildMappingTables(const std::size_t numActive, + const std::array& cartDims, + const std::function(const std::size_t)>& getIJK) + { + auto natural2columnar = std::vector(numActive, 0); + + auto activeCells = std::vector(numActive, std::size_t{0}); + std::iota(activeCells.begin(), activeCells.end(), std::size_t{0}); + + std::sort(activeCells.begin(), activeCells.end(), + [&cartDims, &getIJK](const std::size_t cell1, const std::size_t cell2) -> bool + { + return columnarGlobalIdx(cartDims, getIJK(cell1)) + < columnarGlobalIdx(cartDims, getIJK(cell2)); + }); + + auto columnarActiveID = 0; + for (const auto& naturalActiveID : activeCells) { + natural2columnar[naturalActiveID] = columnarActiveID++; + } + + return natural2columnar; + } } bool Opm::ActiveIndexByColumns::operator==(const ActiveIndexByColumns& rhs) const @@ -53,35 +78,19 @@ bool Opm::ActiveIndexByColumns::operator==(const ActiveIndexByColumns& rhs) cons return this->natural2columnar_ == rhs.natural2columnar_; } -void Opm::ActiveIndexByColumns:: -buildMappingTables(const std::size_t numActive, - const std::array& cartDims, - const std::function(const std::size_t)>& getIJK) +ActiveIndexByColumns(const std::size_t numActive, + const std::array& cartDims, + const std::function(const std::size_t)>& getIJK) + : natural2columnar_{ buildMappingTables(numActive, cartDims, getIJK) } +{} + +Opm::ActiveIndexByColumns +Opm::buildColumnarActiveIndexMappingTables(const EclipseGrid& grid) { - auto activeCells = std::vector(numActive, std::size_t{0}); - std::iota(activeCells.begin(), activeCells.end(), std::size_t{0}); - - std::sort(activeCells.begin(), activeCells.end(), - [&cartDims, &getIJK](const std::size_t cell1, const std::size_t cell2) -> bool - { - return columnarGlobalIdx(cartDims, getIJK(cell1)) - < columnarGlobalIdx(cartDims, getIJK(cell2)); - }); - - this->natural2columnar_.resize(numActive, 0); - auto columnarActiveID = 0; - for (const auto& naturalActiveID : activeCells) { - this->natural2columnar_[naturalActiveID] = columnarActiveID++; - } -} - -void Opm::buildColumnarActiveIndexMappingTables(const EclipseGrid& grid, - ActiveIndexByColumns& map) -{ - map.buildMappingTables(grid.getNumActive(), grid.getNXYZ(), + return ActiveIndexByColumns { grid.getNumActive(), grid.getNXYZ(), [&grid](const std::size_t activeCell) { return grid.getIJK(grid.getGlobalIndex(activeCell)); - }); + }}; } diff --git a/tests/test_ActiveIndexByColumns.cpp b/tests/test_ActiveIndexByColumns.cpp index 010bc0091..f344952a1 100644 --- a/tests/test_ActiveIndexByColumns.cpp +++ b/tests/test_ActiveIndexByColumns.cpp @@ -35,10 +35,21 @@ BOOST_AUTO_TEST_SUITE(Basic_Mapping) BOOST_AUTO_TEST_CASE(Constructor) { - auto map = Opm::ActiveIndexByColumns{}; - auto map2 = map; + const auto cartDims = std::array{ { 1, 1, 4 } }; + const auto actIJK = std::vector> { + { 0, 0, 0 }, + { 0, 0, 1 }, + { 0, 0, 3 }, + }; - auto map3 = std::move(map2); + const auto map = Opm::ActiveIndexByColumns { actIJK.size(), cartDims, + [&actIJK](const std::size_t i) + { + return actIJK[i]; + }}; + + auto map2 = map; + const auto map3 = std::move(map2); BOOST_CHECK_MESSAGE(map3 == map, "Copied Map object must equal initial"); } @@ -51,12 +62,11 @@ BOOST_AUTO_TEST_CASE(Single_Column) { 0, 0, 3 }, }; - auto map = Opm::ActiveIndexByColumns{}; - map.buildMappingTables(actIJK.size(), cartDims, + const auto map = Opm::ActiveIndexByColumns { actIJK.size(), cartDims, [&actIJK](const std::size_t i) { return actIJK[i]; - }); + }}; BOOST_CHECK_EQUAL(map.getColumnarActiveIndex(0), 0); BOOST_CHECK_EQUAL(map.getColumnarActiveIndex(1), 1); @@ -73,12 +83,11 @@ BOOST_AUTO_TEST_CASE(Two_Columns) { 0, 0, 3 }, { 1, 0, 3 }, }; - auto map = Opm::ActiveIndexByColumns{}; - map.buildMappingTables(actIJK.size(), cartDims, + const auto map = Opm::ActiveIndexByColumns { actIJK.size(), cartDims, [&actIJK](const std::size_t i) { return actIJK[i]; - }); + }}; BOOST_CHECK_EQUAL(map.getColumnarActiveIndex(0), 0); BOOST_CHECK_EQUAL(map.getColumnarActiveIndex(1), 3); @@ -100,12 +109,11 @@ BOOST_AUTO_TEST_CASE(Four_Columns) { 0, 0, 3 }, { 1, 0, 3 }, { 0, 1, 3 }, { 1, 1, 3 }, }; - auto map = Opm::ActiveIndexByColumns{}; - map.buildMappingTables(actIJK.size(), cartDims, + const auto map = Opm::ActiveIndexByColumns { actIJK.size(), cartDims, [&actIJK](const std::size_t i) { return actIJK[i]; - }); + }}; BOOST_CHECK_EQUAL(map.getColumnarActiveIndex( 0), 0); BOOST_CHECK_EQUAL(map.getColumnarActiveIndex( 1), 6); @@ -256,8 +264,7 @@ BOOST_AUTO_TEST_CASE(Cube_3x3x3_Full) { const auto grid = Opm::EclipseGrid {{3, 3, 3}, coord_3x3x3(), zcorn_3x3x3() }; - auto map = Opm::ActiveIndexByColumns{}; - buildColumnarActiveIndexMappingTables(grid, map); + const auto map = Opm::buildColumnarActiveIndexMappingTables(grid); BOOST_CHECK_EQUAL(map.getColumnarActiveIndex( 0), 0); BOOST_CHECK_EQUAL(map.getColumnarActiveIndex( 1), 9); @@ -321,8 +328,7 @@ BOOST_AUTO_TEST_CASE(Cube_3x3x3_exclude_centre_cell) const auto actnum = actnum_3x3x3_exclude_centre_cell(); const auto grid = Opm::EclipseGrid {{3, 3, 3}, coord_3x3x3(), zcorn_3x3x3(), actnum.data() }; - auto map = Opm::ActiveIndexByColumns{}; - buildColumnarActiveIndexMappingTables(grid, map); + const auto map = Opm::buildColumnarActiveIndexMappingTables(grid); BOOST_CHECK_EQUAL(map.getColumnarActiveIndex( 0), 0); BOOST_CHECK_EQUAL(map.getColumnarActiveIndex( 1), 9); @@ -385,8 +391,7 @@ BOOST_AUTO_TEST_CASE(Cube_3x3x3_exclude_centre_column) const auto actnum = actnum_3x3x3_exclude_centre_column(); const auto grid = Opm::EclipseGrid {{3, 3, 3}, coord_3x3x3(), zcorn_3x3x3(), actnum.data() }; - auto map = Opm::ActiveIndexByColumns{}; - buildColumnarActiveIndexMappingTables(grid, map); + const auto map = Opm::buildColumnarActiveIndexMappingTables(grid); BOOST_CHECK_EQUAL(map.getColumnarActiveIndex( 0), 0); BOOST_CHECK_EQUAL(map.getColumnarActiveIndex( 1), 9); @@ -447,8 +452,7 @@ BOOST_AUTO_TEST_CASE(Cube_3x3x3_exclude_diagonals) const auto actnum = actnum_3x3x3_exclude_diagonals(); const auto grid = Opm::EclipseGrid {{3, 3, 3}, coord_3x3x3(), zcorn_3x3x3(), actnum.data() }; - auto map = Opm::ActiveIndexByColumns{}; - buildColumnarActiveIndexMappingTables(grid, map); + const auto map = Opm::buildColumnarActiveIndexMappingTables(grid); BOOST_CHECK_EQUAL(map.getColumnarActiveIndex( 0), 5); BOOST_CHECK_EQUAL(map.getColumnarActiveIndex( 1), 1);