mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
#4423 First implementation of PdmObject exchange
This commit is contained in:
@@ -3,13 +3,15 @@ import os
|
||||
import sys
|
||||
from .Grid import Grid
|
||||
from .Properties import Properties
|
||||
from .PdmObject import PdmObject
|
||||
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'generated'))
|
||||
|
||||
import Case_pb2
|
||||
import Case_pb2_grpc
|
||||
import PdmObject_pb2
|
||||
|
||||
class Case:
|
||||
class Case (PdmObject):
|
||||
"""ResInsight case class
|
||||
|
||||
Operate on a ResInsight case specified by a Case Id integer.
|
||||
@@ -31,6 +33,7 @@ class Case:
|
||||
self.type = info.type
|
||||
self.properties = Properties(self)
|
||||
self.request = Case_pb2.CaseRequest(id=self.id)
|
||||
PdmObject.__init__(self, self.stub.GetPdmObject(self.request), self.channel)
|
||||
|
||||
def gridCount(self):
|
||||
"""Get number of grids in the case"""
|
||||
@@ -104,4 +107,7 @@ class Case:
|
||||
def daysSinceStart(self):
|
||||
"""Get a list of decimal values representing days since the start of the simulation"""
|
||||
return self.stub.GetDaysSinceStart(self.request).day_decimals
|
||||
|
||||
|
||||
def views(self):
|
||||
return self.children("ReservoirView")
|
||||
|
||||
|
||||
@@ -69,8 +69,7 @@ class Instance:
|
||||
parameters.append("--console")
|
||||
pid = os.spawnv(os.P_NOWAIT, resInsightExecutable, parameters)
|
||||
if pid:
|
||||
instance = Instance(port=port)
|
||||
instance.launched = True
|
||||
instance = Instance(port=port, launched=True)
|
||||
return instance
|
||||
return None
|
||||
|
||||
@@ -93,7 +92,7 @@ class Instance:
|
||||
|
||||
for tryPort in range(startPort, endPort):
|
||||
if Instance.__is_port_in_use(tryPort):
|
||||
return Instance(tryPort)
|
||||
return Instance(port=tryPort)
|
||||
|
||||
print('Error: Could not find any ResInsight instances responding between ports ' + str(startPort) + ' and ' + str(endPort))
|
||||
return None
|
||||
@@ -106,7 +105,7 @@ class Instance:
|
||||
except grpc.RpcError as e:
|
||||
return False, False
|
||||
|
||||
def __init__(self, port = 50051):
|
||||
def __init__(self, port = 50051, launched = False):
|
||||
""" Attempts to connect to ResInsight at aa specific port on localhost
|
||||
|
||||
Args:
|
||||
@@ -116,7 +115,7 @@ class Instance:
|
||||
location = "localhost:" + str(port)
|
||||
|
||||
self.channel = grpc.insecure_channel(location)
|
||||
self.launched = False
|
||||
self.launched = launched
|
||||
|
||||
# Main version check package
|
||||
self.app = App(self.channel)
|
||||
|
||||
78
ApplicationCode/GrpcInterface/Python/rips/PdmObject.py
Normal file
78
ApplicationCode/GrpcInterface/Python/rips/PdmObject.py
Normal file
@@ -0,0 +1,78 @@
|
||||
import grpc
|
||||
import os
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'generated'))
|
||||
|
||||
from Empty_pb2 import Empty
|
||||
import PdmObject_pb2
|
||||
import PdmObject_pb2_grpc
|
||||
|
||||
class PdmObject:
|
||||
def __init__(self, pb2Object, channel):
|
||||
self.pb2Object = pb2Object
|
||||
self.channel = channel
|
||||
self.stub = PdmObject_pb2_grpc.PdmObjectServiceStub(self.channel)
|
||||
|
||||
def address(self):
|
||||
return self.pb2Object.address
|
||||
|
||||
def classKeyword(self):
|
||||
return self.pb2Object.class_keyword
|
||||
|
||||
def keywords(self):
|
||||
listOfKeywords = []
|
||||
for keyword in self.pb2Object.parameters:
|
||||
listOfKeywords.append(keyword)
|
||||
return listOfKeywords
|
||||
|
||||
def getValue(self, keyword):
|
||||
value = self.pb2Object.parameters[keyword]
|
||||
if value.lower() == 'false':
|
||||
return False
|
||||
elif value.lower() == 'true':
|
||||
return True
|
||||
else:
|
||||
try:
|
||||
intVal = int(value)
|
||||
return intVal
|
||||
except ValueError:
|
||||
try:
|
||||
floatVal = float(value)
|
||||
return floatVal
|
||||
except ValueError:
|
||||
return value
|
||||
|
||||
def setValue(self, keyword, value):
|
||||
if isinstance(value, bool):
|
||||
if value:
|
||||
self.pb2Object.parameters[keyword] = "true"
|
||||
else:
|
||||
self.pb2Object.parameters[keyword] = "false"
|
||||
elif isinstance(value, str):
|
||||
self.pb2Object.parameters[keyword] = "\"" + str(value) + "\""
|
||||
else:
|
||||
self.pb2Object.parameters[keyword] = str(value)
|
||||
|
||||
def descendants(self, classKeyword):
|
||||
request = PdmObject_pb2.PdmChildObjectRequest(object=self.pb2Object, child_keyword=classKeyword)
|
||||
objectList = self.stub.GetDescendantPdmObjects(request).objects
|
||||
childList = []
|
||||
for object in objectList:
|
||||
childList.append(PdmObject(object, self.channel))
|
||||
return childList
|
||||
|
||||
def children(self, classKeyword):
|
||||
request = PdmObject_pb2.PdmChildObjectRequest(object=self.pb2Object, child_keyword=classKeyword)
|
||||
objectList = self.stub.GetChildPdmObjects(request).objects
|
||||
childList = []
|
||||
for object in objectList:
|
||||
childList.append(PdmObject(object, self.channel))
|
||||
return childList
|
||||
|
||||
def ancestor(self, classKeyword):
|
||||
request = PdmObject_pb2.PdmParentObjectRequest(object=self.pb2Object, parent_keyword=classKeyword)
|
||||
return PdmObject(self.stub.GetAncestorPdmObject(request), self.channel)
|
||||
|
||||
def update(self):
|
||||
self.stub.UpdateExistingPdmObject(self.pb2Object)
|
||||
@@ -4,6 +4,7 @@ import sys
|
||||
|
||||
from .Case import Case
|
||||
from .Commands import Commands
|
||||
from .PdmObject import PdmObject
|
||||
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'generated'))
|
||||
|
||||
@@ -11,7 +12,7 @@ from Empty_pb2 import Empty
|
||||
import Project_pb2
|
||||
import Project_pb2_grpc
|
||||
|
||||
class Project:
|
||||
class Project (PdmObject):
|
||||
"""ResInsight project. Not intended to be created separately.
|
||||
|
||||
Automatically created and assigned to Instance.
|
||||
@@ -19,6 +20,7 @@ class Project:
|
||||
def __init__(self, channel):
|
||||
self.channel = channel
|
||||
self.project = Project_pb2_grpc.ProjectStub(channel)
|
||||
PdmObject.__init__(self, self.project.GetPdmObject(Empty()), self.channel)
|
||||
|
||||
def open(self, path):
|
||||
"""Open a new project from the given path
|
||||
|
||||
@@ -11,3 +11,33 @@ if resInsight is not None:
|
||||
print ("Got " + str(len(cases)) + " cases: ")
|
||||
for case in cases:
|
||||
print(case.name)
|
||||
assert(case.address() is not 0)
|
||||
assert(case.classKeyword() == "EclipseCase")
|
||||
print("\n#### Case ####")
|
||||
for keyword in case.keywords():
|
||||
print (keyword + ": " + case.getValue(keyword))
|
||||
print ("\n####Project#####")
|
||||
pdmProject = case.ancestor(classKeyword="ResInsightProject")
|
||||
assert(pdmProject)
|
||||
assert(pdmProject.address() is not 0)
|
||||
assert(pdmProject.address() == resInsight.project.address())
|
||||
|
||||
for keyword in resInsight.project.keywords():
|
||||
print (keyword + ": " + resInsight.project.getValue(keyword))
|
||||
pdmViews = resInsight.project.descendants(classKeyword="ReservoirView")
|
||||
for view in pdmViews:
|
||||
print ("\n####View####")
|
||||
print(view.classKeyword(), view.address())
|
||||
for viewKeyword in view.keywords():
|
||||
print(viewKeyword + "-> " + str(view.getValue(viewKeyword)))
|
||||
view.setValue("ShowGridBox", not view.getValue("ShowGridBox"))
|
||||
view.setValue("ViewBackgroundColor", "#3388AA")
|
||||
view.update()
|
||||
|
||||
print ("\n####Cell Result####")
|
||||
cellResults = view.children(classKeyword="ResultSlot")
|
||||
for resultKeyword in cellResults[0].keywords():
|
||||
print(resultKeyword + "->" + str(cellResults[0].getValue(resultKeyword)))
|
||||
cellResults[0].setValue("ResultVariable", "SOIL")
|
||||
cellResults[0].setValue("ResultType", "DYNAMIC_NATIVE")
|
||||
cellResults[0].update()
|
||||
@@ -50,6 +50,18 @@ def test_10k(rips_instance, initializeTest):
|
||||
daysSinceStart = case.daysSinceStart()
|
||||
assert(len(daysSinceStart) == 9)
|
||||
|
||||
def test_PdmObject(rips_instance, initializeTest):
|
||||
casePath = dataroot.PATH + "/TEST10K_FLT_LGR_NNC/TEST10K_FLT_LGR_NNC.EGRID"
|
||||
case = rips_instance.project.loadCase(path=casePath)
|
||||
pdmObject = case.pdmObject()
|
||||
assert(pdmObject.address is not 0)
|
||||
assert(pdmObject.class_keyword == "EclipseCase")
|
||||
assert(pdmObject.parameters['CaseFileName'] == casePath)
|
||||
assert(int(pdmObject.parameters['CaseId']) == 0)
|
||||
for keyword in pdmObject.parameters:
|
||||
|
||||
print (keyword + ": " + pdmObject.parameters[keyword])
|
||||
|
||||
@pytest.mark.skipif(sys.platform.startswith('linux'), reason="Brugge is currently exceptionally slow on Linux")
|
||||
def test_brugge_0010(rips_instance, initializeTest):
|
||||
casePath = dataroot.PATH + "/Case_with_10_timesteps/Real10/BRUGGE_0010.EGRID"
|
||||
|
||||
Reference in New Issue
Block a user