Python: Add more examples and fix async streaming code

This commit is contained in:
Gaute Lindkvist 2019-05-30 18:52:38 +02:00
parent a6cdec0816
commit 596f098d43
7 changed files with 163 additions and 18 deletions

View File

@ -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"

View File

@ -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)

View File

@ -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")

View File

@ -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")

View File

@ -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])

View File

@ -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")

View File

@ -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")