#4549 Add python method to get selected cells.

Equivalent to GetSelectedCells in Octave.
This commit is contained in:
Kristian Bendiksen
2020-02-19 15:20:34 +01:00
parent c64281c525
commit 8a1fa38435
6 changed files with 248 additions and 2 deletions

View File

@@ -0,0 +1,43 @@
############################################################################
# This example prints center and corners for the currently selected cells
# in ResInsight
############################################################################
import rips
resinsight = rips.Instance.find()
if resinsight is not None:
cases = resinsight.project.cases()
print ("Got " + str(len(cases)) + " cases: ")
for case in cases:
print(case.name)
cells = case.selected_cells()
print("Found " + str(len(cells)) + " selected cells")
for cell in cells:
print("Selected cell: [{}, {}, {}] grid: {}".format(cell.ijk.i+1, cell.ijk.j+1, cell.ijk.k+1, cell.grid_index))
# Get the grid and dimensions
grid = case.grids()[cell.grid_index]
dimensions = grid.dimensions()
# Map ijk to cell index
cell_index = dimensions.i * dimensions.j * cell.ijk.k + dimensions.i * cell.ijk.j + cell.ijk.i
# Print the cell center
cell_centers = grid.cell_centers()
cell_center = cell_centers[cell_index]
print("Cell center: [{}, {}, {}]".format(cell_center.x, cell_center.y, cell_center.z))
# Print the cell corners
cell_corners = grid.cell_corners()[cell_index]
print("Cell corners:")
print("c0:\n" + str(cell_corners.c0))
print("c1:\n" + str(cell_corners.c1))
print("c2:\n" + str(cell_corners.c2))
print("c3:\n" + str(cell_corners.c3))
print("c4:\n" + str(cell_corners.c4))
print("c5:\n" + str(cell_corners.c5))
print("c6:\n" + str(cell_corners.c6))
print("c7:\n" + str(cell_corners.c7))

View File

@@ -895,3 +895,24 @@ class Case(PdmObject):
for value in chunk.cells:
cell_corners.append(value)
return cell_corners
def selected_cells_async(self):
"""Get the selected cells. Async, so returns an iterator.
Returns:
An iterator to a chunk object containing an array of cells.
Loop through the chunks and then the cells within the chunk to get all cells.
"""
return self.__case_stub.GetSelectedCells(self.__request)
def selected_cells(self):
"""Get the selected cells. Synchronous, so returns a list.
Returns:
A list of Cells.
"""
cells = []
generator = self.selected_cells_async()
for chunk in generator:
for value in chunk.cells:
cells.append(value)
return cells

View File

@@ -172,3 +172,8 @@ def test_exportFlowCharacteristics(rips_instance, initialize_test):
file_name = tmpdirname + "/exportFlowChar.txt"
case.export_flow_characteristics(time_steps=8, producers=[], injectors = "I01", file_name = file_name)
def test_selected_cells(rips_instance, initialize_test):
case = rips_instance.project.load_case(dataroot.PATH + "/TEST10K_FLT_LGR_NNC/TEST10K_FLT_LGR_NNC.EGRID")
assert(case.name == "TEST10K_FLT_LGR_NNC")
selected_cells = case.selected_cells()
assert(len(selected_cells) == 0)