Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: add a method that creates a grid property between two input surfaces #1291

Merged
merged 4 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ignore_missing_imports = True
strict_equality = True
warn_redundant_casts = True
warn_unused_configs = True
warn_unused_ignores = True
warn_unused_ignores = False
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this as it seems that mypy messages are incorrect (the # type: ignore is needed, but mypy claims it is not)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an aside, but I've found this usually to happen when some imports are not quite right in some way


exclude = tests|docs|examples|bin

Expand Down
1 change: 1 addition & 0 deletions src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pybind11_add_module(
"${SRC}/common/geometry/volumes.cpp"
"${SRC}/grid3d/cell.cpp"
"${SRC}/grid3d/grid.cpp"
"${SRC}/grid3d/grid_surf_oper.cpp"
"${SRC}/cube/cube.cpp"
"${SRC}/regsurf/sample_grid3d.cpp"
"${SRC}/regsurf/utilities.cpp"
Expand Down
40 changes: 40 additions & 0 deletions src/lib/include/xtgeo/grid3d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <pybind11/pybind11.h> // should be included first according to pybind11 docs
#include <pybind11/numpy.h>
#include <cstddef>
#include <cstdint>
#include <optional>
#include <tuple>

Expand All @@ -19,6 +20,16 @@ grid_cell_volumes(const size_t ncol,
const py::array_t<int> &actnumsv,
const int precision,
const bool asmasked);

std::tuple<py::array_t<double>, py::array_t<double>, py::array_t<double>>
grid_cell_centers(const size_t ncol,
const size_t nrow,
const size_t nlay,
const py::array_t<double> &coordsv,
const py::array_t<float> &zcornsv,
const py::array_t<int> &actnumsv,
const bool asmasked);

std::tuple<py::array_t<double>, py::array_t<double>, py::array_t<double>>
grid_height_above_ffl(const size_t ncol,
const size_t nrow,
Expand Down Expand Up @@ -53,6 +64,30 @@ get_depth_in_cell(const double x,
const std::array<double, 24> &corners,
int option);

py::array_t<int8_t>
grid_assign_value_between_surfaces(const size_t ncol,
const size_t nrow,
const size_t nlay,
const py::array_t<double> &xmid,
const py::array_t<double> &ymid,
const py::array_t<double> &zmid,
const size_t top_ncol,
const size_t top_nrow,
const double top_xori,
const double top_yori,
const double top_xinc,
const double top_yinc,
const double top_rotation,
const py::array_t<double> &top_values,
const size_t bot_ncol,
const size_t bot_nrow,
const double bot_xori,
const double bot_yori,
const double bot_xinc,
const double bot_yinc,
const double bot_rotation,
const py::array_t<double> &bot_values);

inline void
init(py::module &m)
{
Expand All @@ -61,6 +96,8 @@ init(py::module &m)

m_grid3d.def("grid_cell_volumes", &grid_cell_volumes,
"Compute the bulk volume of cell.");
m_grid3d.def("grid_cell_centers", &grid_cell_centers,
"Compute the cells centers coordinates as 3 arrays");
m_grid3d.def("grid_height_above_ffl", &grid_height_above_ffl,
"Compute the height above a FFL (free fluid level).");
m_grid3d.def("cell_corners", &cell_corners,
Expand All @@ -71,6 +108,9 @@ init(py::module &m)
"Determine if a XY point is inside a cell, top or base.");
m_grid3d.def("get_depth_in_cell", &get_depth_in_cell,
"Determine the interpolated cell face Z from XY, top or base.");
m_grid3d.def("grid_assign_value_between_surfaces",
&grid_assign_value_between_surfaces,
"Make a property that is one if cell center is between two surfaces.");
}

} // namespace xtgeo::grid3d
Expand Down
14 changes: 14 additions & 0 deletions src/lib/include/xtgeo/regsurf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ get_xy_from_ij(const size_t i,
const size_t nrow,
const double angle_deg);

double
get_z_from_xy(const double x,
const double y,
const double xori,
const double yori,
const double xinc,
const double yinc,
const size_t ncol,
const size_t nrow,
const double angle_deg,
const py::array_t<double> &values);

std::tuple<int, int, int, int>
find_cell_range(const double xmin,
const double xmax,
Expand Down Expand Up @@ -80,6 +92,8 @@ init(py::module &m)
"Get the outer corners of a regular surface.");
m_regsurf.def("get_xy_from_ij", &get_xy_from_ij,
"Get the XY coordinates from the grid indices.");
m_regsurf.def("get_z_from_xy", &get_z_from_xy,
"Get the Z value in a regsurf from a x y point.");
m_regsurf.def("find_cell_range", &find_cell_range,
"Find the range of regular surface 2D nodes within a box.");
m_regsurf.def("sample_grid3d_layer", &sample_grid3d_layer,
Expand Down
34 changes: 0 additions & 34 deletions src/lib/include/xtgeo/xtgeo.h
Original file line number Diff line number Diff line change
Expand Up @@ -1059,26 +1059,6 @@ extern "C"
long n_swig_np_dbl_inplace_v1, // ntot,
metric m);

void grdcp3d_calc_xyz(long ncol,
long nrow,
long nlay,

double *swig_np_dbl_in_v1, // *coordsv,
long n_swig_np_dbl_in_v1, // ncoord,
float *swig_np_flt_in_v1, // *zcornsv,
long n_swig_np_flt_in_v1, // nzcorn,
int *swig_np_int_in_v1, // *actnumsv,
long n_swig_np_int_in_v1, // nactnum,

int option,

double *swig_np_dbl_aout_v1, // xarr
long n_swig_np_dbl_aout_v1,
double *swig_np_dbl_aout_v2, // yarr
long n_swig_np_dbl_aout_v2,
double *swig_np_dbl_aout_v3, // zarr
long n_swig_np_dbl_aout_v3);

void grd3d_conv_roxapi_grid(int nx,
int ny,
int nz,
Expand Down Expand Up @@ -1394,20 +1374,6 @@ extern "C"
long n_swig_np_int_inplace_v1 // nactnum
);

void grdcp3d_midpoint(long i,
long j,
long k,
long ncol,
long nrow,
long nlay,
double *coordsv,
long ncoord,
float *zcornsv,
long nzcorn,
double *x,
double *y,
double *z);

void grd3d_midpoint(int i,
int j,
int k,
Expand Down
84 changes: 0 additions & 84 deletions src/lib/src/grdcp3d_calc_xyz.c

This file was deleted.

54 changes: 0 additions & 54 deletions src/lib/src/grdcp3d_midpoint.c

This file was deleted.

54 changes: 54 additions & 0 deletions src/lib/src/grid3d/grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,60 @@ grid_cell_volumes(const size_t ncol,
return cellvols;
}

/*
* Get cell centers for a grid.
*
* @param ncol Grid dimensions ncol/nx
* @param nrow Grid dimensions nrow/ny
* @param nlay Grid dimensions nlay/nz
* @param coordsv Grid Z coordinates vector
* @param zcornsv Grid Z corners vector
* @param actumsv Active cells vector
* @param asmasked Process grid cells as masked (return NaN for inactive cells)
* @return Arrays with the X, Y, Z coordinates of the cell centers
*/
std::tuple<py::array_t<double>, py::array_t<double>, py::array_t<double>>
grid_cell_centers(const size_t ncol,
const size_t nrow,
const size_t nlay,
const py::array_t<double> &coordsv,
const py::array_t<float> &zcornsv,
const py::array_t<int> &actnumsv,
const bool asmasked = false)
{
pybind11::array_t<double> xmid({ ncol, nrow, nlay });
pybind11::array_t<double> ymid({ ncol, nrow, nlay });
pybind11::array_t<double> zmid({ ncol, nrow, nlay });
auto xmid_ = xmid.mutable_unchecked<3>();
auto ymid_ = ymid.mutable_unchecked<3>();
auto zmid_ = zmid.mutable_unchecked<3>();
auto actnumsv_ = actnumsv.unchecked<3>();

for (size_t i = 0; i < ncol; i++) {
for (size_t j = 0; j < nrow; j++) {
for (size_t k = 0; k < nlay; k++) {
size_t idx = i * nrow * nlay + j * nlay + k;
if (asmasked && actnumsv_(i, j, k) == 0) {
xmid_(i, j, k) = std::numeric_limits<double>::quiet_NaN();
ymid_(i, j, k) = std::numeric_limits<double>::quiet_NaN();
zmid_(i, j, k) = std::numeric_limits<double>::quiet_NaN();
continue;
}
auto cr =
grid3d::cell_corners(i, j, k, ncol, nrow, nlay, coordsv, zcornsv);

xmid_(i, j, k) = 0.125 * (cr[0] + cr[3] + cr[6] + cr[9] + cr[12] +
cr[15] + cr[18] + cr[21]);
ymid_(i, j, k) = 0.125 * (cr[1] + cr[4] + cr[7] + cr[10] + cr[13] +
cr[16] + cr[19] + cr[22]);
zmid_(i, j, k) = 0.125 * (cr[2] + cr[5] + cr[8] + cr[11] + cr[14] +
cr[17] + cr[20] + cr[23]);
}
}
}
return std::make_tuple(xmid, ymid, zmid);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a note, manual testing indicates that this function in pybind11 is 2-3 times faster than the corresponding function using swig (which it now replaces).


/*
* Compute cell height above ffl (free fluid level), as input to water saturation. Will
* return hbot, htop, hmid (bottom of cell, top of cell, midpoint), but compute method
Expand Down
Loading
Loading