#8297 Python : Do not modify result container when reading grid result data

Avoid resize if data is already present
Improve functions for getting grid data and accessing single cell values
Add example for read and write of grid data
This commit is contained in:
Magne Sjaastad 2021-11-18 12:24:49 +01:00
parent d59584bd93
commit 0b327ca83b
5 changed files with 73 additions and 11 deletions

View File

@ -1,16 +1,41 @@
######################################################################
# This script sets values for SOIL for all grid cells in the first case in the project
# This script sets values for all grid cells in the first case in the project
# The script is intended to be used for TEST10K_FLT_LGR_NNC.EGRID
# This grid case contains one LGR
######################################################################
import rips
resinsight = rips.Instance.find()
case = resinsight.project.case(case_id=0)
total_cell_count = case.cell_count().reservoir_cell_count
grid = case.grid()
grid_cell_count = grid.cell_count()
print("total cell count : " + str(grid_cell_count))
values = []
for i in range(0, total_cell_count):
for i in range(0, grid_cell_count):
values.append(i % 2 * 0.75)
print("Applying values to full grid")
case.set_grid_property(values, "DYNAMIC_NATIVE", "SOIL", 0)
# Assign value to IJK grid cell at (31, 53, 21)
grid = case.grid()
property_data_index = grid.property_data_index_from_ijk(31, 53, 21)
values[property_data_index] = 1.5
print("Applying values to main grid")
case.set_grid_property(values, "STATIC_NATIVE", "MY_DATA", 0)
values_from_ri = case.grid_property("STATIC_NATIVE", "MY_DATA", 0)
assert values[property_data_index] == values_from_ri[property_data_index]
# Get LGR grid as grid index 1
grid = case.grid(1)
grid_cell_count = grid.cell_count()
print("lgr cell count : " + str(grid_cell_count))
values = []
for i in range(0, grid_cell_count):
values.append(i % 3 * 0.75)
print("Applying values to LGR grid")
case.set_grid_property(values, "STATIC_NATIVE", "MY_DATA", 0, 1)
values_from_ri = case.grid_property("STATIC_NATIVE", "MY_DATA", 0, 1)

View File

@ -125,7 +125,7 @@ def __generate_property_input_chunks(self, array, parameters):
@add_method(Case)
def grid(self, index):
def grid(self, index=0):
"""Get Grid of a given index
Arguments:

View File

@ -22,6 +22,7 @@ class Grid:
self.case = case
self.index = index
self.cached_dimensions = None
def dimensions(self):
"""The dimensions in i, j, k direction
@ -30,9 +31,13 @@ class Grid:
Vec3i: class with integer attributes i, j, k giving extent in all three dimensions.
"""
case_request = Case_pb2.CaseRequest(id=self.case.id)
return self.__stub.GetDimensions(
Grid_pb2.GridRequest(case_request=case_request, grid_index=self.index)
).dimensions
if self.cached_dimensions is None:
self.cached_dimensions = self.__stub.GetDimensions(
Grid_pb2.GridRequest(case_request=case_request, grid_index=self.index)
).dimensions
return self.cached_dimensions
def cell_centers_async(self):
"""The cells center for all cells in given grid async.
@ -86,3 +91,31 @@ class Grid:
for center in chunk.cells:
corners.append(center)
return corners
def property_data_index_from_ijk(self, i, j, k):
"""Compute property index from 1-based IJK cell address. Cell Property Result data is organized by I, J and K.
property_data_index = dims.i * dims.j * (k - 1) + dims.i * (j - 1) + (i - 1)
Returns:
int: Cell property result index from IJK
"""
dims = self.dimensions()
property_data_index = dims.i * dims.j * (k - 1) + dims.i * (j - 1) + (i - 1)
return property_data_index
def cell_count(self):
"""Cell count in grid
Returns:
int: Cell count in grid
"""
dims = self.dimensions()
count = dims.i * dims.j * dims.k
return count

View File

@ -27,6 +27,9 @@ def test_10k(rips_instance, initialize_test):
cell_centers = grid.cell_centers()
assert len(cell_centers) == (dimensions.i * dimensions.j * dimensions.k)
property_data_index = grid.property_data_index_from_ijk(31, 53, 21)
assert property_data_index == 177510
# Test a specific cell (results from ResInsight UI)
cell_index = 168143
assert math.isclose(3627.17, cell_centers[cell_index].x, abs_tol=0.1)

View File

@ -351,9 +351,10 @@ protected:
{
m_cellCount = caseData->grid( gridIndex )->cellCount();
auto resultValues = caseData->results( porosityModel )->modifiableCellScalarResult( resVarAddr, timeStepIndex );
if ( resultValues && m_cellCount > 0 )
if ( resultValues && resultValues->empty() && m_cellCount > 0 )
{
resultValues->resize( m_cellCount );
auto totalCellCount = caseData->mainGrid()->globalCellArray().size();
resultValues->resize( totalCellCount );
}
m_resultAccessor =