mirror of
https://github.com/OPM/ResInsight.git
synced 2026-10-15 01:42:13 -05:00
#4430 Implement GetAvailableProperties() and GetActiveCellResults()
This commit is contained in:
1 parent
c4a4ceef29
commit
86e202aed0
8 files changed
+440
-169
No files matched your search
+146
-125
@@ -7,7 +7,7 @@ import socket
|
||||
|
||||
sys.path.insert(1, os.path.join(sys.path[0], '../generated'))
|
||||
|
||||
import Empty_pb2
|
||||
from Empty_pb2 import Empty
|
||||
import CaseInfo_pb2
|
||||
import CaseInfo_pb2_grpc
|
||||
import Commands_pb2
|
||||
@@ -18,142 +18,163 @@ import ProjectInfo_pb2
|
||||
import ProjectInfo_pb2_grpc
|
||||
import ResInfo_pb2
|
||||
import ResInfo_pb2_grpc
|
||||
import Properties_pb2
|
||||
import Properties_pb2_grpc
|
||||
import RiaVersionInfo
|
||||
|
||||
MAX_MESSAGE_LENGTH = 128 * 1024 * 1024
|
||||
|
||||
class ResInfo:
|
||||
def __init__(self, channel):
|
||||
self.resInfo = ResInfo_pb2_grpc.ResInfoStub(channel)
|
||||
def versionMessage(self):
|
||||
return self.resInfo.GetVersion(Empty_pb2.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())
|
||||
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 = Commands_pb2_grpc.CommandsStub(channel)
|
||||
|
||||
def execute(self, commandParams):
|
||||
try:
|
||||
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 __init__(self, channel):
|
||||
self.commands = Commands_pb2_grpc.CommandsStub(channel)
|
||||
|
||||
def execute(self, commandParams):
|
||||
try:
|
||||
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(Commands_pb2.CommandParams(setTimeStep=Commands_pb2.SetTimeStepParams(caseId=caseId, timeStep=timeStep)))
|
||||
|
||||
def setMainWindowSize(self, width, height):
|
||||
return self.execute(Commands_pb2.CommandParams(setMainWindowSize=Commands_pb2.SetMainWindowSizeParams(width=width, height=height)))
|
||||
def setTimeStep(self, caseId, timeStep):
|
||||
return self.execute(Commands_pb2.CommandParams(setTimeStep=Commands_pb2.SetTimeStepParams(caseId=caseId, timeStep=timeStep)))
|
||||
|
||||
def setMainWindowSize(self, width, height):
|
||||
return self.execute(Commands_pb2.CommandParams(setMainWindowSize=Commands_pb2.SetMainWindowSizeParams(width=width, height=height)))
|
||||
|
||||
def openProject(self, path):
|
||||
return self.execute(Commands_pb2.CommandParams(openProject=Commands_pb2.FilePathRequest(path=path)))
|
||||
def openProject(self, path):
|
||||
return self.execute(Commands_pb2.CommandParams(openProject=Commands_pb2.FilePathRequest(path=path)))
|
||||
|
||||
def loadCase(self, path):
|
||||
return self.execute(Commands_pb2.CommandParams(loadCase=Commands_pb2.FilePathRequest(path=path)))
|
||||
|
||||
def closeProject(self):
|
||||
return self.execute(Commands_pb2.CommandParams(closeProject=Empty_pb2.Empty()))
|
||||
def loadCase(self, path):
|
||||
return self.execute(Commands_pb2.CommandParams(loadCase=Commands_pb2.FilePathRequest(path=path)))
|
||||
|
||||
def closeProject(self):
|
||||
return self.execute(Commands_pb2.CommandParams(closeProject=Empty()))
|
||||
|
||||
class GridInfo:
|
||||
def __init__(self, channel):
|
||||
self.gridInfo = GridInfo_pb2_grpc.GridInfoStub(channel)
|
||||
|
||||
def getGridCount(self, caseId=0):
|
||||
return self.gridInfo.GetGridCount(CaseInfo_pb2.Case(id=caseId)).count
|
||||
|
||||
def getGridDimensions(self, caseId=0):
|
||||
return self.gridInfo.GetGridDimensions(CaseInfo_pb2.Case(id=caseId)).dimensions
|
||||
|
||||
def streamActiveCellInfo(self, caseId=0):
|
||||
return self.gridInfo.StreamActiveCellInfo(CaseInfo_pb2.Case(id=caseId))
|
||||
|
||||
def __init__(self, channel):
|
||||
self.gridInfo = GridInfo_pb2_grpc.GridInfoStub(channel)
|
||||
|
||||
def getGridCount(self, caseId=0):
|
||||
return self.gridInfo.GetGridCount(CaseInfo_pb2.Case(id=caseId)).count
|
||||
|
||||
def getGridDimensions(self, caseId=0):
|
||||
return self.gridInfo.GetGridDimensions(CaseInfo_pb2.Case(id=caseId)).dimensions
|
||||
|
||||
def streamActiveCellInfo(self, caseId=0):
|
||||
return self.gridInfo.StreamActiveCellInfo(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_pb2.Empty())
|
||||
if selected is not None:
|
||||
return selected.case_info
|
||||
else:
|
||||
return None
|
||||
def allCases(self):
|
||||
cases = self.projectInfo.AllCases(Empty_pb2.Empty())
|
||||
if cases is not None:
|
||||
return allCases.case_info
|
||||
else:
|
||||
return None
|
||||
|
||||
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 allCases.case_info
|
||||
else:
|
||||
return None
|
||||
|
||||
class Properties:
|
||||
def __init__(self, channel):
|
||||
self.properties = Properties_pb2_grpc.PropertiesStub(channel)
|
||||
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)
|
||||
|
||||
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
|
||||
@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
|
||||
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):
|
||||
location = "localhost:" + str(port)
|
||||
self.channel = grpc.insecure_channel(location, options=[('grpc.max_receive_message_length', MAX_MESSAGE_LENGTH)])
|
||||
def __init__(self, port = 50051):
|
||||
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)
|
||||
|
||||
# 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)
|
||||
|
||||
@@ -1,45 +1,19 @@
|
||||
from ResInsight import ResInsight
|
||||
import grpc
|
||||
import logging
|
||||
import sys
|
||||
|
||||
def run():
|
||||
# NOTE(gRPC Python Team): .close() is possible on a channel and should be
|
||||
# used in circumstances in which the with statement does not fit the needs
|
||||
# of the code.
|
||||
logging.basicConfig()
|
||||
|
||||
try:
|
||||
port = 50051
|
||||
if len(sys.argv) > 1:
|
||||
port = sys.argv[1]
|
||||
resInsight = ResInsight("localhost:" + port)
|
||||
timeStepsInfo = resInsight.grid.numberOfTimeSteps(ResInsight.Case(id=0))
|
||||
print ("Number of time steps: " + str(timeStepsInfo.value))
|
||||
resultsAllTimeSteps = []
|
||||
for timeStep in range(0, timeStepsInfo.value - 1):
|
||||
results = resInsight.grid.results(ResInsight.ResultRequest(ResInsight.Case(id=0), ResInsight.ResultAddress(0, "SOIL"), timeStep))
|
||||
print ("Got " + str(len(results.value)) + " values")
|
||||
resultsAllTimeSteps.append(results.value)
|
||||
import os
|
||||
sys.path.insert(1, os.path.join(sys.path[0], '../api'))
|
||||
import ResInsight
|
||||
|
||||
print("Have stored results array containing " + str(len(resultsAllTimeSteps)) + " time steps")
|
||||
resInsight = ResInsight.Instance.find()
|
||||
#gridCount = resInsight.gridInfo.getGridCount(caseId=0)
|
||||
#gridDimensions = resInsight.gridInfo.getAllGridDimensions(caseId=0)
|
||||
|
||||
print("Looking for first cell with a decent SOIL value")
|
||||
indexFirstProperCell = 0
|
||||
for i in range(0, len(resultsAllTimeSteps[0])):
|
||||
result = resultsAllTimeSteps[0][i]
|
||||
if indexFirstProperCell == 0 and result > 0.01:
|
||||
indexFirstProperCell = i
|
||||
|
||||
for resultsForTimeStep in resultsAllTimeSteps:
|
||||
print ("Result for cell " + str(indexFirstProperCell) + ": " + str(resultsForTimeStep[indexFirstProperCell]))
|
||||
|
||||
except grpc.RpcError as e:
|
||||
if e.code() == grpc.StatusCode.NOT_FOUND:
|
||||
print("Case id not found")
|
||||
else:
|
||||
logging.error('Other error: %s', e)
|
||||
resultChunks = resInsight.properties.activeCellResults(0, 'DYNAMIC_NATIVE', 'SOIL', 2)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run()
|
||||
results = []
|
||||
for resultChunk in resultChunks:
|
||||
for value in resultChunk.values:
|
||||
results.append(value)
|
||||
print("Number of active cells: " + str(len(results)))
|
||||
print("15th active cell: ")
|
||||
for result in results:
|
||||
print(result)
|
||||
Reference in new issue
Block a user