From 5377ff3442b3411f787c959af96c36c3fbbf4ecd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A5rd=20Skaflestad?= Date: Fri, 1 Jul 2022 13:55:29 +0200 Subject: [PATCH] Streamline GridDims::getIJK() This commit switches the Cartesian -> IJK decomposition in GridDims from K->J->I into the I->J->K order. The latter is simpler. While here, also consistently use 'std::size_t' instead of 'size_t'. --- opm/common/utility/ActiveGridCells.hpp | 1 + .../eclipse/EclipseState/Grid/GridDims.hpp | 35 ++-- .../eclipse/EclipseState/Grid/GridDims.cpp | 176 ++++++++++-------- 3 files changed, 111 insertions(+), 101 deletions(-) diff --git a/opm/common/utility/ActiveGridCells.hpp b/opm/common/utility/ActiveGridCells.hpp index d08d553c6..00ffd2be0 100644 --- a/opm/common/utility/ActiveGridCells.hpp +++ b/opm/common/utility/ActiveGridCells.hpp @@ -22,6 +22,7 @@ #include #include +#include namespace Opm { diff --git a/opm/input/eclipse/EclipseState/Grid/GridDims.hpp b/opm/input/eclipse/EclipseState/Grid/GridDims.hpp index 54d8de312..afdf980f7 100644 --- a/opm/input/eclipse/EclipseState/Grid/GridDims.hpp +++ b/opm/input/eclipse/EclipseState/Grid/GridDims.hpp @@ -21,8 +21,7 @@ #define OPM_PARSER_GRIDDIMS_HPP #include -#include -#include +#include namespace Opm { class Deck; @@ -31,32 +30,30 @@ namespace Opm { class GridDims { public: - GridDims(); - explicit GridDims(std::array xyz); - GridDims(size_t nx, size_t ny, size_t nz); + explicit GridDims(const std::array& xyz); + GridDims(std::size_t nx, std::size_t ny, std::size_t nz); static GridDims serializeObject(); explicit GridDims(const Deck& deck); - size_t getNX() const; + std::size_t getNX() const; + std::size_t getNY() const; + std::size_t getNZ() const; + std::size_t operator[](int dim) const; - size_t getNY() const; - size_t getNZ() const; - size_t operator[](int dim) const; + std::array getNXYZ() const; - const std::array getNXYZ() const; + std::size_t getGlobalIndex(std::size_t i, std::size_t j, std::size_t k) const; - size_t getGlobalIndex(size_t i, size_t j, size_t k) const; + std::array getIJK(std::size_t globalIndex) const; - const std::array getIJK(size_t globalIndex) const; + std::size_t getCartesianSize() const; - size_t getCartesianSize() const; + void assertGlobalIndex(std::size_t globalIndex) const; - void assertGlobalIndex(size_t globalIndex) const; - - void assertIJK(size_t i, size_t j, size_t k) const; + void assertIJK(std::size_t i, std::size_t j, std::size_t k) const; bool operator==(const GridDims& data) const; @@ -69,9 +66,9 @@ namespace Opm { } protected: - size_t m_nx; - size_t m_ny; - size_t m_nz; + std::size_t m_nx; + std::size_t m_ny; + std::size_t m_nz; private: void init(const DeckKeyword& keyword); diff --git a/src/opm/input/eclipse/EclipseState/Grid/GridDims.cpp b/src/opm/input/eclipse/EclipseState/Grid/GridDims.cpp index e9a7430d3..23fa9e2bb 100644 --- a/src/opm/input/eclipse/EclipseState/Grid/GridDims.cpp +++ b/src/opm/input/eclipse/EclipseState/Grid/GridDims.cpp @@ -17,147 +17,159 @@ along with OPM. If not, see . */ -#include -#include -#include +#include #include +#include // DIMENS +#include // GDFILE +#include // SPECGRID + #include #include #include -#include - -#include // DIMENS -#include // SPECGRID +#include +#include +#include namespace Opm { - GridDims::GridDims(std::array xyz) : - GridDims(xyz[0], xyz[1], xyz[2]) - { - } - GridDims::GridDims(size_t nx, size_t ny, size_t nz) : - m_nx(nx), m_ny(ny), m_nz(nz) - { - } + GridDims::GridDims() + : m_nx(0), m_ny(0), m_nz(0) + {} + + GridDims::GridDims(const std::array& xyz) + : GridDims(xyz[0], xyz[1], xyz[2]) + {} + + GridDims::GridDims(const std::size_t nx, + const std::size_t ny, + const std::size_t nz) + : m_nx(nx), m_ny(ny), m_nz(nz) + {} GridDims GridDims::serializeObject() { - GridDims result; - result.m_nx = 1; - result.m_ny = 2; - result.m_nz = 3; - - return result; + return { 1, 2, 3 }; } - GridDims::GridDims(const Deck& deck) { - if (deck.hasKeyword("SPECGRID")) - init(deck["SPECGRID"].back()); - else if (deck.hasKeyword("DIMENS")) - init(deck["DIMENS"].back()); - else if (deck.hasKeyword("GDFILE")) - binary_init(deck); - else - throw std::invalid_argument("Must have either SPECGRID or DIMENS to indicate grid dimensions"); + GridDims::GridDims(const Deck& deck) + { + if (deck.hasKeyword()) { + this->init(deck[ParserKeywords::SPECGRID::keywordName].back()); + } + else if (deck.hasKeyword()) { + this->init(deck[ParserKeywords::DIMENS::keywordName].back()); + } + else if (deck.hasKeyword()) { + this->binary_init(deck); + } + else { + throw std::invalid_argument { + "Must have either SPECGRID or DIMENS " + "to indicate grid dimensions" + }; + } } - size_t GridDims::getNX() const { - return m_nx; - } + std::size_t GridDims::getNX() const { return this->m_nx; } + std::size_t GridDims::getNY() const { return this->m_ny; } + std::size_t GridDims::getNZ() const { return this->m_nz; } - size_t GridDims::getNY() const { - return m_ny; - } - - size_t GridDims::getNZ() const { - return m_nz; - } - - size_t GridDims::operator[](int dim) const { + std::size_t GridDims::operator[](int dim) const + { switch (dim) { - case 0: - return this->m_nx; - break; - case 1: - return this->m_ny; - break; - case 2: - return this->m_nz; - break; + case 0: return this->getNX(); + case 1: return this->getNY(); + case 2: return this->getNZ(); + default: throw std::invalid_argument("Invalid argument dim:" + std::to_string(dim)); } } - const std::array GridDims::getNXYZ() const { - return std::array {{int( m_nx ), int( m_ny ), int( m_nz )}}; + std::array GridDims::getNXYZ() const + { + return { + static_cast(this->getNX()), + static_cast(this->getNY()), + static_cast(this->getNZ()) + }; } - size_t GridDims::getGlobalIndex(size_t i, size_t j, size_t k) const { - return (i + j * getNX() + k * getNX() * getNY()); + std::size_t + GridDims::getGlobalIndex(const std::size_t i, + const std::size_t j, + const std::size_t k) const + { + return i + this->getNX()*(j + k*this->getNY()); } - const std::array GridDims::getIJK(size_t globalIndex) const { - std::array r = { { 0, 0, 0 } }; - int k = globalIndex / (getNX() * getNY()); - globalIndex -= k * (getNX() * getNY()); - int j = globalIndex / getNX(); - globalIndex -= j * getNX(); - int i = globalIndex; - r[0] = i; - r[1] = j; - r[2] = k; - return r; + std::array + GridDims::getIJK(std::size_t globalIndex) const + { + auto ijk = std::array{}; + + ijk[0] = globalIndex % this->getNX(); globalIndex /= this->getNX(); + ijk[1] = globalIndex % this->getNY(); globalIndex /= this->getNY(); + ijk[2] = globalIndex; + + return ijk; } - size_t GridDims::getCartesianSize() const { + std::size_t GridDims::getCartesianSize() const + { return m_nx * m_ny * m_nz; } - void GridDims::assertGlobalIndex(size_t globalIndex) const { + void GridDims::assertGlobalIndex(std::size_t globalIndex) const + { if (globalIndex >= getCartesianSize()) throw std::invalid_argument("input global index above valid range"); } - void GridDims::assertIJK(size_t i, size_t j, size_t k) const { + void GridDims::assertIJK(const std::size_t i, + const std::size_t j, + const std::size_t k) const + { if (i >= getNX() || j >= getNY() || k >= getNZ()) throw std::invalid_argument("input IJK index above valid range"); } - GridDims::GridDims() : - m_nx(0), m_ny(0), m_nz(0) - { - } - // keyword must be DIMENS or SPECGRID - inline std::array< int, 3 > readDims(const DeckKeyword& keyword) { + inline std::array readDims(const DeckKeyword& keyword) + { const auto& record = keyword.getRecord(0); - return { { record.getItem("NX").get(0), - record.getItem("NY").get(0), - record.getItem("NZ").get(0) } }; + return { + record.getItem("NX").get(0), + record.getItem("NY").get(0), + record.getItem("NZ").get(0) + }; } - void GridDims::init(const DeckKeyword& keyword) { - auto dims = readDims(keyword); + void GridDims::init(const DeckKeyword& keyword) + { + const auto dims = readDims(keyword); m_nx = dims[0]; m_ny = dims[1]; m_nz = dims[2]; } - void GridDims::binary_init(const Deck& deck) { + void GridDims::binary_init(const Deck& deck) + { const DeckKeyword& gdfile_kw = deck["GDFILE"].back(); const std::string& gdfile_arg = gdfile_kw.getRecord(0).getItem("filename").get(0); const EclIO::EGrid egrid( deck.makeDeckPath(gdfile_arg) ); + const auto& dimens = egrid.dimension(); m_nx = dimens[0]; m_ny = dimens[1]; m_nz = dimens[2]; } - bool GridDims::operator==(const GridDims& data) const { + bool GridDims::operator==(const GridDims& data) const + { return this->getNXYZ() == data.getNXYZ(); }