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'.
This commit is contained in:
Bård Skaflestad 2022-07-01 13:55:29 +02:00
parent 336f7a4159
commit 5377ff3442
3 changed files with 111 additions and 101 deletions

View File

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

View File

@ -21,8 +21,7 @@
#define OPM_PARSER_GRIDDIMS_HPP
#include <array>
#include <stdexcept>
#include <vector>
#include <cstddef>
namespace Opm {
class Deck;
@ -31,32 +30,30 @@ namespace Opm {
class GridDims
{
public:
GridDims();
explicit GridDims(std::array<int, 3> xyz);
GridDims(size_t nx, size_t ny, size_t nz);
explicit GridDims(const std::array<int, 3>& 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<int, 3> getNXYZ() 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(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);

View File

@ -17,147 +17,159 @@
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <array>
#include <stdexcept>
#include <vector>
#include <opm/input/eclipse/EclipseState/Grid/GridDims.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/DeckKeyword.hpp>
#include <opm/input/eclipse/Deck/DeckRecord.hpp>
#include <opm/input/eclipse/EclipseState/Grid/GridDims.hpp>
#include <opm/input/eclipse/Parser/ParserKeywords/D.hpp> // DIMENS
#include <opm/input/eclipse/Parser/ParserKeywords/S.hpp> // SPECGRID
#include <array>
#include <stdexcept>
#include <vector>
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) :
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<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 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<ParserKeywords::SPECGRID>()) {
this->init(deck[ParserKeywords::SPECGRID::keywordName].back());
}
else if (deck.hasKeyword<ParserKeywords::DIMENS>()) {
this->init(deck[ParserKeywords::DIMENS::keywordName].back());
}
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 {
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<int, 3> GridDims::getNXYZ() const {
return std::array<int, 3> {{int( m_nx ), int( m_ny ), int( m_nz )}};
std::array<int, 3> GridDims::getNXYZ() const
{
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 {
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<int, 3> GridDims::getIJK(size_t globalIndex) const {
std::array<int, 3> 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<int, 3>
GridDims::getIJK(std::size_t globalIndex) const
{
auto ijk = std::array<int, 3>{};
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<int, 3> readDims(const DeckKeyword& keyword)
{
const auto& record = keyword.getRecord(0);
return { { record.getItem("NX").get<int>(0),
record.getItem("NY").get<int>(0),
record.getItem("NZ").get<int>(0) } };
return {
record.getItem("NX").get<int>(0),
record.getItem("NY").get<int>(0),
record.getItem("NZ").get<int>(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<std::string>(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();
}