From 4c2a70fa40c806003780d4569d6d6127d21b9531 Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Thu, 18 Nov 2021 12:24:49 +0100 Subject: [PATCH] #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 --- .../PythonExamples/set_grid_properties.py | 35 ++++++++++++++--- GrpcInterface/Python/rips/case.py | 2 +- GrpcInterface/Python/rips/grid.py | 39 +++++++++++++++++-- GrpcInterface/Python/rips/tests/test_grids.py | 3 ++ GrpcInterface/RiaGrpcPropertiesService.cpp | 5 ++- 5 files changed, 73 insertions(+), 11 deletions(-) diff --git a/GrpcInterface/Python/rips/PythonExamples/set_grid_properties.py b/GrpcInterface/Python/rips/PythonExamples/set_grid_properties.py index 216cb173c2..3ee412a893 100644 --- a/GrpcInterface/Python/rips/PythonExamples/set_grid_properties.py +++ b/GrpcInterface/Python/rips/PythonExamples/set_grid_properties.py @@ -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) diff --git a/GrpcInterface/Python/rips/case.py b/GrpcInterface/Python/rips/case.py index 9962a9b7e9..d97dcd504c 100644 --- a/GrpcInterface/Python/rips/case.py +++ b/GrpcInterface/Python/rips/case.py @@ -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: diff --git a/GrpcInterface/Python/rips/grid.py b/GrpcInterface/Python/rips/grid.py index a040065651..41317718a7 100644 --- a/GrpcInterface/Python/rips/grid.py +++ b/GrpcInterface/Python/rips/grid.py @@ -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 diff --git a/GrpcInterface/Python/rips/tests/test_grids.py b/GrpcInterface/Python/rips/tests/test_grids.py index c910d950fd..002c9b342f 100644 --- a/GrpcInterface/Python/rips/tests/test_grids.py +++ b/GrpcInterface/Python/rips/tests/test_grids.py @@ -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) diff --git a/GrpcInterface/RiaGrpcPropertiesService.cpp b/GrpcInterface/RiaGrpcPropertiesService.cpp index 84fb304db3..b4aab850c1 100644 --- a/GrpcInterface/RiaGrpcPropertiesService.cpp +++ b/GrpcInterface/RiaGrpcPropertiesService.cpp @@ -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 =