Merge pull request #3070 from bska/simplify-griddims-ijk

Streamline GridDims::getIJK()
This commit is contained in:
Markus Blatt 2022-07-05 14:23:41 +02:00 committed by GitHub
commit 7dd56d2bde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 111 additions and 101 deletions

View File

@ -22,6 +22,7 @@
#include <opm/input/eclipse/EclipseState/Grid/GridDims.hpp> #include <opm/input/eclipse/EclipseState/Grid/GridDims.hpp>
#include <array> #include <array>
#include <vector>
namespace Opm namespace Opm
{ {

View File

@ -21,8 +21,7 @@
#define OPM_PARSER_GRIDDIMS_HPP #define OPM_PARSER_GRIDDIMS_HPP
#include <array> #include <array>
#include <stdexcept> #include <cstddef>
#include <vector>
namespace Opm { namespace Opm {
class Deck; class Deck;
@ -31,32 +30,30 @@ namespace Opm {
class GridDims class GridDims
{ {
public: public:
GridDims(); GridDims();
explicit GridDims(std::array<int, 3> xyz); explicit GridDims(const std::array<int, 3>& xyz);
GridDims(size_t nx, size_t ny, size_t nz); GridDims(std::size_t nx, std::size_t ny, std::size_t nz);
static GridDims serializeObject(); static GridDims serializeObject();
explicit GridDims(const Deck& deck); 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; std::array<int, 3> getNXYZ() const;
size_t getNZ() const;
size_t operator[](int dim) const;
const std::array<int, 3> 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<int, 3> getIJK(std::size_t globalIndex) const;
const std::array<int, 3> 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(std::size_t i, std::size_t j, std::size_t k) const;
void assertIJK(size_t i, size_t j, size_t k) const;
bool operator==(const GridDims& data) const; bool operator==(const GridDims& data) const;
@ -69,9 +66,9 @@ namespace Opm {
} }
protected: protected:
size_t m_nx; std::size_t m_nx;
size_t m_ny; std::size_t m_ny;
size_t m_nz; std::size_t m_nz;
private: private:
void init(const DeckKeyword& keyword); void init(const DeckKeyword& keyword);

View File

@ -17,147 +17,159 @@
along with OPM. If not, see <http://www.gnu.org/licenses/>. along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <array> #include <opm/input/eclipse/EclipseState/Grid/GridDims.hpp>
#include <stdexcept>
#include <vector>
#include <opm/io/eclipse/EGrid.hpp> #include <opm/io/eclipse/EGrid.hpp>
#include <opm/input/eclipse/Parser/ParserKeywords/D.hpp> // DIMENS
#include <opm/input/eclipse/Parser/ParserKeywords/G.hpp> // GDFILE
#include <opm/input/eclipse/Parser/ParserKeywords/S.hpp> // SPECGRID
#include <opm/input/eclipse/Deck/Deck.hpp> #include <opm/input/eclipse/Deck/Deck.hpp>
#include <opm/input/eclipse/Deck/DeckKeyword.hpp> #include <opm/input/eclipse/Deck/DeckKeyword.hpp>
#include <opm/input/eclipse/Deck/DeckRecord.hpp> #include <opm/input/eclipse/Deck/DeckRecord.hpp>
#include <opm/input/eclipse/EclipseState/Grid/GridDims.hpp> #include <array>
#include <stdexcept>
#include <opm/input/eclipse/Parser/ParserKeywords/D.hpp> // DIMENS #include <vector>
#include <opm/input/eclipse/Parser/ParserKeywords/S.hpp> // SPECGRID
namespace Opm { namespace Opm {
GridDims::GridDims(std::array<int, 3> xyz) :
GridDims(xyz[0], xyz[1], xyz[2])
{
}
GridDims::GridDims(size_t nx, size_t ny, size_t nz) : GridDims::GridDims()
m_nx(nx), m_ny(ny), m_nz(nz) : m_nx(0), m_ny(0), m_nz(0)
{ {}
}
GridDims::GridDims(const std::array<int, 3>& 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 GridDims::serializeObject()
{ {
GridDims result; return { 1, 2, 3 };
result.m_nx = 1;
result.m_ny = 2;
result.m_nz = 3;
return result;
} }
GridDims::GridDims(const Deck& deck) { GridDims::GridDims(const Deck& deck)
if (deck.hasKeyword("SPECGRID")) {
init(deck["SPECGRID"].back()); if (deck.hasKeyword<ParserKeywords::SPECGRID>()) {
else if (deck.hasKeyword("DIMENS")) this->init(deck[ParserKeywords::SPECGRID::keywordName].back());
init(deck["DIMENS"].back()); }
else if (deck.hasKeyword("GDFILE")) else if (deck.hasKeyword<ParserKeywords::DIMENS>()) {
binary_init(deck); this->init(deck[ParserKeywords::DIMENS::keywordName].back());
else }
throw std::invalid_argument("Must have either SPECGRID or DIMENS to indicate grid dimensions"); else if (deck.hasKeyword<ParserKeywords::GDFILE>()) {
this->binary_init(deck);
}
else {
throw std::invalid_argument {
"Must have either SPECGRID or DIMENS "
"to indicate grid dimensions"
};
}
} }
size_t GridDims::getNX() const { std::size_t GridDims::getNX() const { return this->m_nx; }
return 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 { std::size_t GridDims::operator[](int dim) const
return m_ny; {
}
size_t GridDims::getNZ() const {
return m_nz;
}
size_t GridDims::operator[](int dim) const {
switch (dim) { switch (dim) {
case 0: case 0: return this->getNX();
return this->m_nx; case 1: return this->getNY();
break; case 2: return this->getNZ();
case 1:
return this->m_ny;
break;
case 2:
return this->m_nz;
break;
default: default:
throw std::invalid_argument("Invalid argument dim:" + std::to_string(dim)); throw std::invalid_argument("Invalid argument dim:" + std::to_string(dim));
} }
} }
const std::array<int, 3> GridDims::getNXYZ() const { std::array<int, 3> GridDims::getNXYZ() const
return std::array<int, 3> {{int( m_nx ), int( m_ny ), int( m_nz )}}; {
return {
static_cast<int>(this->getNX()),
static_cast<int>(this->getNY()),
static_cast<int>(this->getNZ())
};
} }
size_t GridDims::getGlobalIndex(size_t i, size_t j, size_t k) const { std::size_t
return (i + j * getNX() + k * getNX() * getNY()); 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<int, 3> GridDims::getIJK(size_t globalIndex) const { std::array<int, 3>
std::array<int, 3> r = { { 0, 0, 0 } }; GridDims::getIJK(std::size_t globalIndex) const
int k = globalIndex / (getNX() * getNY()); {
globalIndex -= k * (getNX() * getNY()); auto ijk = std::array<int, 3>{};
int j = globalIndex / getNX();
globalIndex -= j * getNX(); ijk[0] = globalIndex % this->getNX(); globalIndex /= this->getNX();
int i = globalIndex; ijk[1] = globalIndex % this->getNY(); globalIndex /= this->getNY();
r[0] = i; ijk[2] = globalIndex;
r[1] = j;
r[2] = k; return ijk;
return r;
} }
size_t GridDims::getCartesianSize() const { std::size_t GridDims::getCartesianSize() const
{
return m_nx * m_ny * m_nz; 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()) if (globalIndex >= getCartesianSize())
throw std::invalid_argument("input global index above valid range"); 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()) if (i >= getNX() || j >= getNY() || k >= getNZ())
throw std::invalid_argument("input IJK index above valid range"); 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 // keyword must be DIMENS or SPECGRID
inline std::array< int, 3 > readDims(const DeckKeyword& keyword) { inline std::array<int, 3> readDims(const DeckKeyword& keyword)
{
const auto& record = keyword.getRecord(0); const auto& record = keyword.getRecord(0);
return { { record.getItem("NX").get<int>(0), return {
record.getItem("NY").get<int>(0), record.getItem("NX").get<int>(0),
record.getItem("NZ").get<int>(0) } }; record.getItem("NY").get<int>(0),
record.getItem("NZ").get<int>(0)
};
} }
void GridDims::init(const DeckKeyword& keyword) { void GridDims::init(const DeckKeyword& keyword)
auto dims = readDims(keyword); {
const auto dims = readDims(keyword);
m_nx = dims[0]; m_nx = dims[0];
m_ny = dims[1]; m_ny = dims[1];
m_nz = dims[2]; 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 DeckKeyword& gdfile_kw = deck["GDFILE"].back();
const std::string& gdfile_arg = gdfile_kw.getRecord(0).getItem("filename").get<std::string>(0); const std::string& gdfile_arg = gdfile_kw.getRecord(0).getItem("filename").get<std::string>(0);
const EclIO::EGrid egrid( deck.makeDeckPath(gdfile_arg) ); const EclIO::EGrid egrid( deck.makeDeckPath(gdfile_arg) );
const auto& dimens = egrid.dimension(); const auto& dimens = egrid.dimension();
m_nx = dimens[0]; m_nx = dimens[0];
m_ny = dimens[1]; m_ny = dimens[1];
m_nz = dimens[2]; m_nz = dimens[2];
} }
bool GridDims::operator==(const GridDims& data) const { bool GridDims::operator==(const GridDims& data) const
{
return this->getNXYZ() == data.getNXYZ(); return this->getNXYZ() == data.getNXYZ();
} }