diff --git a/ApplicationCode/GrpcInterface/CMakeLists.cmake b/ApplicationCode/GrpcInterface/CMakeLists.cmake index b361b90186..9d90b288fe 100644 --- a/ApplicationCode/GrpcInterface/CMakeLists.cmake +++ b/ApplicationCode/GrpcInterface/CMakeLists.cmake @@ -146,6 +146,10 @@ if (PYTHON_EXECUTABLE AND EXISTS ${PYTHON_EXECUTABLE}) "api/ResInsight.py" "examples/CommandExample.py" "examples/GridInfoStreamingExample.py" + "examples/PoroPermXSync.py" + "examples/PoroPermXAsync.py" + "examples/SoilPorvAsync.py" + "examples/SoilPorvSync.py" "examples/ResultValues.py" "examples/SelectedCases.py" "examples/AllCases.py" diff --git a/ApplicationCode/GrpcInterface/Python/api/ResInsight.py b/ApplicationCode/GrpcInterface/Python/api/ResInsight.py index 7e8485d093..dc937a9233 100644 --- a/ApplicationCode/GrpcInterface/Python/api/ResInsight.py +++ b/ApplicationCode/GrpcInterface/Python/api/ResInsight.py @@ -120,7 +120,17 @@ class Properties: def __init__(self, channel): self.properties = Properties_pb2_grpc.PropertiesStub(channel) - def generateResultRequestArrays(self, array, parameters): + def generateResultRequestArrayIterator(self, values_iterator, parameters): + chunk = Properties_pb2.ResultRequestChunk() + chunk.params.CopyFrom(parameters) + yield chunk + + for values in values_iterator: + valmsg = Properties_pb2.ResultArray(values = values) + chunk.values.CopyFrom(valmsg) + yield chunk + + def generateResultRequestChunks(self, array, parameters): # Each double is 8 bytes. A good chunk size is 64KiB = 65536B # Meaning ideal number of doubles would be 8192. # However we need overhead space, so if we choose 8160 in chunk size @@ -157,7 +167,9 @@ class Properties: property_name = propertyName, time_step = timeStep, porosity_model = porosityModelEnum) - return self.properties.GetActiveCellResults(request) + for chunk in self.properties.GetActiveCellResults(request): + yield chunk + def gridCellResults(self, caseId, propertyType, propertyName, timeStep, gridIndex = 0, porosityModel = 'MATRIX_MODEL'): propertyTypeEnum = Properties_pb2.PropertyType.Value(propertyType) porosityModelEnum = GridInfo_pb2.PorosityModelType.Value(porosityModel) @@ -168,6 +180,25 @@ class Properties: grid_index = gridIndex, porosity_model = porosityModelEnum) return self.properties.GetGridResults(request) + + def setActiveCellResultsAsync(self, values_iterator, caseId, propertyType, propertyName, timeStep, gridIndex = 0, porosityModel = 'MATRIX_MODEL'): + propertyTypeEnum = Properties_pb2.PropertyType.Value(propertyType) + porosityModelEnum = GridInfo_pb2.PorosityModelType.Value(porosityModel) + request = Properties_pb2.ResultRequest(request_case = CaseInfo_pb2.Case(id=caseId), + property_type = propertyTypeEnum, + property_name = propertyName, + time_step = timeStep, + grid_index = gridIndex, + porosity_model = porosityModelEnum) + try: + reply_iterator = self.generateResultRequestArrayIterator(values_iterator, request) + self.properties.SetActiveCellResults(reply_iterator) + except grpc.RpcError as e: + if e.code() == grpc.StatusCode.NOT_FOUND: + print("Command not found") + else: + print("Other error", e) + def setActiveCellResults(self, values, caseId, propertyType, propertyName, timeStep, gridIndex = 0, porosityModel = 'MATRIX_MODEL'): propertyTypeEnum = Properties_pb2.PropertyType.Value(propertyType) porosityModelEnum = GridInfo_pb2.PorosityModelType.Value(porosityModel) @@ -178,14 +209,13 @@ class Properties: grid_index = gridIndex, porosity_model = porosityModelEnum) try: - request_iterator = self.generateResultRequestArrays(values, request) + request_iterator = self.generateResultRequestChunks(values, request) self.properties.SetActiveCellResults(request_iterator) except grpc.RpcError as e: if e.code() == grpc.StatusCode.NOT_FOUND: print("Command not found") else: print("Other error", e) - def setGridResults(self, values, caseId, propertyType, propertyName, timeStep, gridIndex = 0, porosityModel = 'MATRIX_MODEL'): propertyTypeEnum = Properties_pb2.PropertyType.Value(propertyType) porosityModelEnum = GridInfo_pb2.PorosityModelType.Value(porosityModel) diff --git a/ApplicationCode/GrpcInterface/Python/examples/PoroPermXAsync.py b/ApplicationCode/GrpcInterface/Python/examples/PoroPermXAsync.py new file mode 100644 index 0000000000..859fb3845c --- /dev/null +++ b/ApplicationCode/GrpcInterface/Python/examples/PoroPermXAsync.py @@ -0,0 +1,22 @@ +import sys +import os +sys.path.insert(1, os.path.join(sys.path[0], '../api')) +import ResInsight + +def createResult(poroChunks, permxChunks): + for (poroChunk, permxChunk) in zip(poroChunks, permxChunks): + resultChunk = [] + for (poro, permx) in zip(poroChunk.values, permxChunk.values): + resultChunk.append(poro * permx) + yield resultChunk + + + +resInsight = ResInsight.Instance.find() + +poroChunks = resInsight.properties.activeCellResults(0, 'STATIC_NATIVE', 'PORO', 0) +permxChunks = resInsight.properties.activeCellResults(0, 'STATIC_NATIVE', 'PERMX', 0) + +resInsight.properties.setActiveCellResultsAsync(createResult(poroChunks, permxChunks), 0, 'GENERATED', 'POROPERMXAS', 0) + +print("Transferred all results back") \ No newline at end of file diff --git a/ApplicationCode/GrpcInterface/Python/examples/PoroPermXSync.py b/ApplicationCode/GrpcInterface/Python/examples/PoroPermXSync.py new file mode 100644 index 0000000000..d1ca8d111c --- /dev/null +++ b/ApplicationCode/GrpcInterface/Python/examples/PoroPermXSync.py @@ -0,0 +1,29 @@ +import sys +import os +sys.path.insert(1, os.path.join(sys.path[0], '../api')) +import ResInsight + +resInsight = ResInsight.Instance.find() +#gridCount = resInsight.gridInfo.getGridCount(caseId=0) +#gridDimensions = resInsight.gridInfo.getAllGridDimensions(caseId=0) + +for i in range(0, 40): + poroChunks = resInsight.properties.activeCellResults(0, 'STATIC_NATIVE', 'PORO', 0) + poroResults = [] + for poroChunk in poroChunks: + for poro in poroChunk.values: + poroResults.append(poro) + + permxChunks = resInsight.properties.activeCellResults(0, 'STATIC_NATIVE', 'PERMX', 0) + permxResults = [] + for permxChunk in permxChunks: + for permx in permxChunk.values: + permxResults.append(permx) + + results = [] + for (poro, permx) in zip(poroResults, permxResults): + results.append(poro * permx) + + resInsight.properties.setActiveCellResults(results, 0, 'GENERATED', 'POROPERMXSY', 0) + +print("Transferred all results back") \ No newline at end of file diff --git a/ApplicationCode/GrpcInterface/Python/examples/ResultValues.py b/ApplicationCode/GrpcInterface/Python/examples/ResultValues.py index dada3b7227..c334d8c52e 100644 --- a/ApplicationCode/GrpcInterface/Python/examples/ResultValues.py +++ b/ApplicationCode/GrpcInterface/Python/examples/ResultValues.py @@ -7,22 +7,22 @@ resInsight = ResInsight.Instance.find() #gridCount = resInsight.gridInfo.getGridCount(caseId=0) #gridDimensions = resInsight.gridInfo.getAllGridDimensions(caseId=0) -resultChunks = resInsight.properties.activeCellResults(0, 'DYNAMIC_NATIVE', 'SOIL', 2) +poroChunks = resInsight.properties.activeCellResults(0, 'STATIC_NATIVE', 'PORO', 0) +poroResults = [] +for poroChunk in poroChunks: + for poro in poroChunk.values: + poroResults.append(poro) + +permxChunks = resInsight.properties.activeCellResults(0, 'STATIC_NATIVE', 'PERMX', 0) +permxResults = [] +for permxChunk in permxChunks: + for permx in permxChunk.values: + permxResults.append(permx) results = [] -for resultChunk in resultChunks: - for value in resultChunk.values: - results.append(value) +for (poro, permx) in zip(poroResults, permxResults): + results.append(poro * permx) + print("Transferred " + str(len(results)) + " cell results") print("30th active cell: ") print(results[29]) - -resultChunks = resInsight.properties.gridCellResults(0, 'DYNAMIC_NATIVE', 'SOIL', 2) - -results = [] -for resultChunk in resultChunks: - for value in resultChunk.values: - results.append(value) -print("Transferred " + str(len(results)) + " cell results") -print("124498th cell: ") -print(results[124498]) diff --git a/ApplicationCode/GrpcInterface/Python/examples/SoilPorVAsync.py b/ApplicationCode/GrpcInterface/Python/examples/SoilPorVAsync.py new file mode 100644 index 0000000000..bc31125d26 --- /dev/null +++ b/ApplicationCode/GrpcInterface/Python/examples/SoilPorVAsync.py @@ -0,0 +1,31 @@ +import sys +import os +sys.path.insert(1, os.path.join(sys.path[0], '../api')) +import ResInsight + +def createResult(soilChunks, porvChunks): + for (soilChunk, porvChunk) in zip(soilChunks, porvChunks): + resultChunk = [] + number = 0 + for (soil, porv) in zip(soilChunk.values, porvChunk.values): + resultChunk.append(soil * porv) + number += 1 + yield resultChunk + + + +resInsight = ResInsight.Instance.find() + +timeStepInfo = resInsight.gridInfo.timeSteps(0) + +porvChunks = resInsight.properties.activeCellResults(0, 'STATIC_NATIVE', 'PORV', 0) +porvArray = [] +for porvChunk in porvChunks: + porvArray.append(porvChunk) + +for i in range (0, len(timeStepInfo.date)): + soilChunks = resInsight.properties.activeCellResults(0, 'DYNAMIC_NATIVE', 'SOIL', i) + input_iterator = createResult(soilChunks, iter(porvArray)) + resInsight.properties.setActiveCellResultsAsync(input_iterator, 0, 'GENERATED', 'SOILPORVAsync', i) + +print("Transferred all results back") \ No newline at end of file diff --git a/ApplicationCode/GrpcInterface/Python/examples/SoilPorvSync.py b/ApplicationCode/GrpcInterface/Python/examples/SoilPorvSync.py new file mode 100644 index 0000000000..f17c7e838f --- /dev/null +++ b/ApplicationCode/GrpcInterface/Python/examples/SoilPorvSync.py @@ -0,0 +1,29 @@ +import sys +import os +sys.path.insert(1, os.path.join(sys.path[0], '../api')) +import ResInsight + +resInsight = ResInsight.Instance.find() +#gridCount = resInsight.gridInfo.getGridCount(caseId=0) +#gridDimensions = resInsight.gridInfo.getAllGridDimensions(caseId=0) + +porvChunks = resInsight.properties.activeCellResults(0, 'STATIC_NATIVE', 'PORV', 0) +porvResults = [] +for porvChunk in porvChunks: + for porv in porvChunk.values: + porvResults.append(porv) + +timeStepInfo = resInsight.gridInfo.timeSteps(0) + +for i in range (0, len(timeStepInfo.date)): + soilChunks = resInsight.properties.activeCellResults(0, 'DYNAMIC_NATIVE', 'SOIL', i) + soilResults = [] + for soilChunk in soilChunks: + for soil in soilChunk.values: + soilResults.append(soil) + results = [] + for (soil, porv) in zip(soilResults, porvResults): + results.append(soil * porv) + + resInsight.properties.setActiveCellResults(results, 0, 'GENERATED', 'SOILPORVSync', i) +print("Transferred all results back") \ No newline at end of file