gRPC: Move all python code into the ApplicationCode/GrpcInterface folder

This commit is contained in:
Gaute Lindkvist
2019-05-22 20:23:49 +02:00
parent 9ac15ec553
commit c9d56bda9c
13 changed files with 42 additions and 12 deletions

View File

@@ -79,7 +79,7 @@ set(PROTO_FILES
"Properties"
)
set(GRPC_PYTHON_SOURCE_PATH "${CMAKE_SOURCE_DIR}/Python")
set(GRPC_PYTHON_SOURCE_PATH "${CMAKE_CURRENT_LIST_DIR}/Python")
set(GRPC_PYTHON_DEST_PATH "${CMAKE_BINARY_DIR}/Python")
foreach(proto_file ${PROTO_FILES})
@@ -150,7 +150,8 @@ if (PYTHON_EXECUTABLE AND EXISTS ${PYTHON_EXECUTABLE})
"examples/ResultValues.py"
"examples/SelectedCases.py"
"examples/AllCases.py"
"examples/SetResultValues.py"
"examples/SetActiveCellProperties.py"
"examples/SetGridProperties.py"
"tests/test_sample.py"
)
@@ -158,7 +159,7 @@ if (PYTHON_EXECUTABLE AND EXISTS ${PYTHON_EXECUTABLE})
list(APPEND GRPC_PYTHON_SOURCES_FULL_PATH "${GRPC_PYTHON_SOURCE_PATH}/${PYTHON_SCRIPT}")
endforeach()
if (MSVC)
source_group(TREE ${GRPC_PYTHON_SOURCE_PATH} FILES ${GRPC_PYTHON_SOURCES_FULL_PATH} PREFIX "Python")
source_group(TREE ${GRPC_PYTHON_SOURCE_PATH} FILES ${GRPC_PYTHON_SOURCES_FULL_PATH} PREFIX "GrpcInterface\\Python")
endif(MSVC)
endif(PYTHON_EXECUTABLE AND EXISTS ${PYTHON_EXECUTABLE})
@@ -166,5 +167,9 @@ endif(PYTHON_EXECUTABLE AND EXISTS ${PYTHON_EXECUTABLE})
list ( APPEND GRPC_HEADER_FILES ${SOURCE_GROUP_HEADER_FILES})
list ( APPEND GRPC_CPP_SOURCES ${SOURCE_GROUP_SOURCE_FILES})
CONFIGURE_FILE( ${CMAKE_SOURCE_DIR}/ApplicationCode/Adm/RiaVersionInfo.py.cmake
${GRPC_PYTHON_SOURCE_PATH}/generated/RiaVersionInfo.py
)
source_group( "GrpcInterface" FILES ${SOURCE_GROUP_HEADER_FILES} ${SOURCE_GROUP_SOURCE_FILES} ${CMAKE_CURRENT_LIST_DIR}/CMakeLists.cmake )
source_group( "GrpcInterface\\GrpcProtos" FILES ${GRPC_PROTO_FILES_FULL_PATH} )

View File

@@ -0,0 +1,3 @@
__pycache__
.pytest_cache
generated

View File

@@ -0,0 +1,272 @@
from __future__ import print_function
import grpc
import os
import sys
import socket
import logging
sys.path.insert(1, os.path.join(sys.path[0], '../generated'))
from Empty_pb2 import Empty
import CaseInfo_pb2
import CaseInfo_pb2_grpc
import Commands_pb2 as Cmd
import Commands_pb2_grpc as CmdRpc
import GridInfo_pb2
import GridInfo_pb2_grpc
import ProjectInfo_pb2
import ProjectInfo_pb2_grpc
import ResInfo_pb2
import ResInfo_pb2_grpc
import Properties_pb2
import Properties_pb2_grpc
import RiaVersionInfo
class ResInfo:
def __init__(self, channel):
self.resInfo = ResInfo_pb2_grpc.ResInfoStub(channel)
def versionMessage(self):
return self.resInfo.GetVersion(Empty())
def majorVersion(self):
return self.versionMessage().major_version
def minorVersion(self):
return self.versionMessage().minor_version
def patchVersion(self):
return self.versionMessage().patch_version
def versionString(self):
return str(self.majorVersion()) + "." + str(self.minorVersion()) + "." + str(self.patchVersion())
class CommandExecutor:
def __init__(self, channel):
self.commands = CmdRpc.CommandsStub(channel)
def execute(self, commandParams):
try:
return self.commands.Execute(commandParams)
except grpc.RpcError as e:
if e.code() == grpc.StatusCode.NOT_FOUND:
print("Command not found")
else:
print("Other error")
def setTimeStep(self, caseId, timeStep):
return self.execute(Cmd.CommandParams(setTimeStep=Cmd.SetTimeStepParams(caseId=caseId, timeStep=timeStep)))
def setMainWindowSize(self, width, height):
return self.execute(Cmd.CommandParams(setMainWindowSize=Cmd.SetMainWindowSizeParams(width=width, height=height)))
def openProject(self, path):
return self.execute(Cmd.CommandParams(openProject=Cmd.FilePathRequest(path=path)))
def loadCase(self, path):
commandReply = self.execute(Cmd.CommandParams(loadCase=Cmd.FilePathRequest(path=path)))
assert commandReply.HasField("loadCaseResult")
return commandReply.loadCaseResult.id
def closeProject(self):
return self.execute(Cmd.CommandParams(closeProject=Empty()))
def exportWellPaths(self, wellPaths=[], mdStepSize=5.0):
if isinstance(wellPaths, str):
wellPathArray = [str]
elif isinstance(wellPaths, list):
wellPathArray = wellPaths
return self.execute(Cmd.CommandParams(exportWellPaths=Cmd.ExportWellPathRequest(wellPathNames=wellPathArray, mdStepSize=mdStepSize)))
class GridInfo:
def __init__(self, channel):
self.gridInfo = GridInfo_pb2_grpc.GridInfoStub(channel)
def gridCount(self, caseId=0):
return self.gridInfo.GetGridCount(CaseInfo_pb2.Case(id=caseId)).count
def gridDimensions(self, caseId=0):
return self.gridInfo.GetGridDimensions(CaseInfo_pb2.Case(id=caseId)).dimensions
def cellCount(self, caseId=0, porosityModel='MATRIX_MODEL'):
porosityModelEnum = GridInfo_pb2.PorosityModelType.Value(porosityModel)
request = GridInfo_pb2.CellInfoRequest(case_id=caseId,
porosity_model=porosityModel)
return self.gridInfo.GetCellCount(request)
def cellInfoForActiveCells(self, caseId=0, porosityModel='MATRIX_MODEL'):
porosityModelEnum = GridInfo_pb2.PorosityModelType.Value(porosityModel)
request = GridInfo_pb2.CellInfoRequest(case_id=caseId,
porosity_model=porosityModel)
return self.gridInfo.GetCellInfoForActiveCells(request)
def timeSteps(self, caseId=0):
return self.gridInfo.GetTimeSteps(CaseInfo_pb2.Case(id=caseId))
class ProjectInfo:
def __init__(self, channel):
self.projectInfo = ProjectInfo_pb2_grpc.ProjectInfoStub(channel)
def selectedCases(self):
selected = self.projectInfo.SelectedCases(Empty())
if selected is not None:
return selected.case_info
else:
return None
def allCases(self):
cases = self.projectInfo.AllCases(Empty())
if cases is not None:
return cases.case_info
else:
return None
class Properties:
def __init__(self, channel):
self.properties = Properties_pb2_grpc.PropertiesStub(channel)
def generateResultRequestArrays(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
# We have 256B left for overhead which should be plenty
chunkSize = 8000
index = -1
while index < len(array):
chunk = Properties_pb2.ResultRequestChunk()
if index is -1:
chunk.params.CopyFrom(parameters)
index += 1;
else:
actualChunkSize = min(len(array) - index + 1, chunkSize)
chunk.values.CopyFrom(Properties_pb2.ResultArray(values = array[index:index+actualChunkSize]))
index += actualChunkSize
yield chunk
# Final empty message to signal completion
chunk = Properties_pb2.ResultRequestChunk()
yield chunk
def availableProperties(self, caseId, propertyType, porosityModel = 'MATRIX_MODEL'):
propertyTypeEnum = Properties_pb2.PropertyType.Value(propertyType)
porosityModelEnum = GridInfo_pb2.PorosityModelType.Value(porosityModel)
request = Properties_pb2.PropertiesRequest (request_case = CaseInfo_pb2.Case(id=caseId),
property_type = propertyTypeEnum,
porosity_model = porosityModelEnum)
return self.properties.GetAvailableProperties(request).property_names
def activeCellResults(self, caseId, propertyType, propertyName, timeStep, 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,
porosity_model = porosityModelEnum)
return self.properties.GetActiveCellResults(request)
def gridCellResults(self, 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)
return self.properties.GetGridResults(request)
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)
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:
request_iterator = self.generateResultRequestArrays(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)
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:
request_iterator = self.generateResultRequestArrays(values, request)
self.properties.SetGridResults(request_iterator)
except grpc.RpcError as e:
if e.code() == grpc.StatusCode.NOT_FOUND:
print("Command not found")
else:
print("Other error", e)
class Instance:
@staticmethod
def is_port_in_use(port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(0.2)
return s.connect_ex(('localhost', port)) == 0
@staticmethod
def launch():
port = 50051
portEnv = os.environ.get('RESINSIGHT_GRPC_PORT')
if portEnv:
port = int(portEnv)
resInsightExecutable = os.environ.get('RESINSIGHT_EXECUTABLE')
if resInsightExecutable is None:
print('Error: Could not launch any ResInsight instances because RESINSIGHT_EXECUTABLE is not set')
return None
while Instance.is_port_in_use(port):
port += 1
print('Port ' + str(port))
print('Trying to launch', resInsightExecutable)
pid = os.spawnl(os.P_NOWAIT, resInsightExecutable, " --grpcserver " + str(port))
print(pid)
return Instance(port)
@staticmethod
def find(startPort = 50051, endPort = 50071):
portEnv = os.environ.get('RESINSIGHT_GRPC_PORT')
if portEnv:
startPort = int(portEnv)
endPort = startPort + 20
for tryPort in range(startPort, endPort):
if Instance.is_port_in_use(tryPort):
return Instance(tryPort)
print('Error: Could not find any ResInsight instances responding between ports ' + str(startPort) + ' and ' + str(endPort))
return None
def __init__(self, port = 50051):
logging.basicConfig()
location = "localhost:" + str(port)
self.channel = grpc.insecure_channel(location)
# Main version check package
self.resInfo = ResInfo(self.channel)
try:
majorVersionOk = self.resInfo.majorVersion() == int(RiaVersionInfo.RESINSIGHT_MAJOR_VERSION)
minorVersionOk = self.resInfo.minorVersion() == int(RiaVersionInfo.RESINSIGHT_MINOR_VERSION)
if not (majorVersionOk and minorVersionOk):
raise Exception('Version of ResInsight does not match version of Python API')
except grpc.RpcError as e:
if e.code() == grpc.StatusCode.UNAVAILABLE:
print('Info: Could not find any instances at port ' + str(port))
except Exception as e:
print('Error:', e)
# Service packages
self.commands = CommandExecutor(self.channel)
self.gridInfo = GridInfo(self.channel)
self.projectInfo = ProjectInfo(self.channel)
self.properties = Properties(self.channel)

View File

@@ -0,0 +1,13 @@
import sys
import os
sys.path.insert(1, os.path.join(sys.path[0], '../api'))
import ResInsight
resInsight = ResInsight.Instance.find()
if resInsight is not None:
caseInfos = resInsight.projectInfo.allCases()
print ("Got " + str(len(caseInfos)) + " cases: ")
for caseInfo in caseInfos:
print(caseInfo.name)

View File

@@ -0,0 +1,12 @@
import sys
import os
sys.path.insert(1, os.path.join(sys.path[0], '../api'))
import ResInsight
# Load instance
resInsight = ResInsight.Instance.find()
# Run a couple of commands
resInsight.commands.setTimeStep(caseId=0, timeStep=3)
resInsight.commands.setMainWindowSize(width=800, height=500)
resInsight.commands.exportWellPaths()

View File

@@ -0,0 +1,25 @@
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)
cellCounts = resInsight.gridInfo.cellCount(caseId=0)
print("Number of active cells: " + str(cellCounts.active_cell_count))
activeCellInfoChunks = resInsight.gridInfo.cellInfoForActiveCells(caseId=0)
#print("Number of grids: " + str(gridCount))
#print(gridDimensions)
receivedActiveCells = []
for activeCellChunk in activeCellInfoChunks:
for activeCell in activeCellChunk.data:
receivedActiveCells.append(activeCell)
assert(cellCounts.active_cell_count == len(receivedActiveCells))
print("First active cell: ")
print(receivedActiveCells[0])

View File

@@ -0,0 +1,28 @@
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)
resultChunks = resInsight.properties.activeCellResults(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("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,14 @@
import sys
import os
sys.path.insert(1, os.path.join(sys.path[0], '../api'))
import ResInsight
resInsight = ResInsight.Instance.find()
if resInsight is not None:
caseInfos = resInsight.projectInfo.selectedCases()
print ("Got " + str(len(caseInfos)) + " cases: ")
for caseInfo in caseInfos:
print(caseInfo.name)

View File

@@ -0,0 +1,19 @@
import sys
import os
sys.path.insert(1, os.path.join(sys.path[0], '../api'))
import ResInsight
resInsight = ResInsight.Instance.find()
activeCellCount = resInsight.gridInfo.cellCount(caseId=0).active_cell_count
values = []
for i in range(0, activeCellCount):
values.append(i % 2 * 0.5);
timeSteps = resInsight.gridInfo.timeSteps(caseId=0)
for i in range(0, len(timeSteps.date)):
print("Applying values to all time step " + str(i))
resInsight.properties.setActiveCellResults(values, 0, 'DYNAMIC_NATIVE', 'SOIL', i)

View File

@@ -0,0 +1,16 @@
import sys
import os
sys.path.insert(1, os.path.join(sys.path[0], '../api'))
import ResInsight
resInsight = ResInsight.Instance.find()
totalCellCount = resInsight.gridInfo.cellCount(caseId=0).reservoir_cell_count
values = []
for i in range(0, totalCellCount):
values.append(i % 2 * 0.75);
print("Applying values to full grid")
resInsight.properties.setGridResults(values, 0, 'DYNAMIC_NATIVE', 'SOIL', 0)

View File

@@ -0,0 +1,51 @@
import os, sys
# Add the 'api' path to system path to be able to import modules from the 'api' folder
# python current working directory must be 'tests'
sys.path.insert(1, os.path.join(sys.path[0], '..\\api'))
import ResInsight
resInsight = ResInsight.Instance()
# content of test_sample.py
def getActiveCellCount(caseId):
activeCellInfoChunks = resInsight.gridInfo.streamActiveCellInfo(caseId)
receivedActiveCells = []
for activeCellChunk in activeCellInfoChunks:
for activeCell in activeCellChunk.data:
receivedActiveCells.append(activeCell)
return len(receivedActiveCells)
def myOpenProject(filepath):
resInsight = ResInsight.Instance()
#resInsight.commands.setMainWindowSize(width=800, height=500)
resInsight.commands.openProject(filepath)
def test_openProjectAndCountCells():
testRepositoryRoot = "d:\\gitroot-ceesol\\ResInsight-regression-test"
#casePath = testRepositoryRoot + "\\ModelData\\TEST10K_FLT_LGR_NNC\\TEST10K_FLT_LGR_NNC.EGRID"
#openEclipseCase(casePath)
# projectPath = testRepositoryRoot + "\\ProjectFiles\\ProjectFilesSmallTests\\TestCase_10K_Complete\\RegressionTest.rsp"
# projectPath = testRepositoryRoot + "\\ProjectFiles\\ProjectFilesSmallTests\\TestCase_Norne\\RegressionTest.rsp"
projectPath = testRepositoryRoot + "\\ProjectFiles\\ProjectFilesSmallTests\\TestCase_10K_Watertight\\RegressionTest.rsp"
myOpenProject(projectPath)
assert getActiveCellCount(0) == 11125
def test_openCaseAndCountCells():
testRepositoryRoot = "d:\\gitroot-ceesol\\ResInsight-regression-test"
casePath = testRepositoryRoot + "\\ModelData\\TEST10K_FLT_LGR_NNC\\TEST10K_FLT_LGR_NNC.EGRID"
resInsight.commands.loadCase(casePath)
assert getActiveCellCount(0) == 11125
resInsight.commands.closeProject()