Skip to content

Commit

Permalink
Add more functions to the new api (#941)
Browse files Browse the repository at this point in the history
  • Loading branch information
lochel authored Feb 10, 2021
1 parent a138290 commit 50daf1c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 7 deletions.
2 changes: 1 addition & 1 deletion doc/UsersGuide/source/api/getResultFile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Gets the result filename and buffer size of the given model cref.
#PYTHON#
.. code-block:: python
# not available
filename, bufferSize, status = oms.getResultFile(cref)
#END#

Expand Down
30 changes: 29 additions & 1 deletion src/OMSimulatorPython/Model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from OMSimulator import Scope, Types
from OMSimulator.NewAPI import *


class Model:
Expand Down Expand Up @@ -30,6 +29,11 @@ def simulate(self):
if Types.Status(status) != Types.Status.OK:
raise Exception('error {}'.format(Types.Status(status)))

def doStep(self):
status = Scope._capi.doStep(self.cref)
if Types.Status(status) != Types.Status.OK:
raise Exception('error {}'.format(Types.Status(status)))

def terminate(self):
status = Scope._capi.terminate(self.cref)
if Types.Status(status) != Types.Status.OK:
Expand All @@ -39,6 +43,13 @@ def terminate(self):
def cref(self):
return self._cref

@property
def time(self):
value, status = Scope._capi.getTime(self.cref)
if Types.Status(status) != Types.Status.OK:
raise Exception('error {}'.format(Types.Status(status)))
return value

@property
def startTime(self):
value, status = Scope._capi.getStartTime(self.cref)
Expand All @@ -64,3 +75,20 @@ def stopTime(self, value: float):
status = Scope._capi.setStopTime(self.cref, value)
if Types.Status(status) != Types.Status.OK:
raise Exception('error {}'.format(Types.Status(status)))

@property
def resultFile(self):
file, _, status = Scope._capi.getResultFile(self.cref)
if Types.Status(status) != Types.Status.OK:
raise Exception('error {}'.format(Types.Status(status)))
return file

@resultFile.setter
def resultFile(self, file: str):
_, bufferSize, status = Scope._capi.getResultFile(self.cref)
if Types.Status(status) != Types.Status.OK:
raise Exception('error {}'.format(Types.Status(status)))

status = Scope._capi.setResultFile(self.cref, file, bufferSize)
if Types.Status(status) != Types.Status.OK:
raise Exception('error {}'.format(Types.Status(status)))
14 changes: 12 additions & 2 deletions src/OMSimulatorPython/NewAPI.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from OMSimulator import Scope, Types
from OMSimulator.Model import Model
from OMSimulator import Model, Scope, Types


def newModel(cref):
Expand All @@ -9,3 +8,14 @@ def newModel(cref):
else:
raise Exception('error {}'.format(Types.Status(status)))
return Model(cref)

def setTempDirectory(dir):
status = Scope._capi.setTempDirectory(dir)
if Types.Status(status) != Types.Status.OK:
raise Exception('error {}'.format(Types.Status(status)))

def importFile(file):
cref, status = Scope._capi.importFile(file)
if Types.Status(status) != Types.Status.OK:
raise Exception('error {}'.format(Types.Status(status)))
return cref
2 changes: 2 additions & 0 deletions src/OMSimulatorPython/OMSimulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def __init__(self, temp_directory=None):
if temp_directory is not None:
self.setTempDirectory(temp_directory)

self.getResultFile = Scope._capi.getResultFile

def __enter__(self):
return self

Expand Down
1 change: 1 addition & 0 deletions src/OMSimulatorPython/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
OpenModelica FMI & TLM based simulator'''

from OMSimulator import Scope, Types
from OMSimulator.Model import Model
from OMSimulator.NewAPI import *
from OMSimulator.OMSimulator import OMSimulator

Expand Down
13 changes: 10 additions & 3 deletions src/OMSimulatorPython/capi.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ def __init__(self):
self.obj.oms_getInteger.restype = ctypes.c_int
self.obj.oms_getReal.argtypes = [ctypes.c_char_p, ctypes.POINTER(ctypes.c_double)]
self.obj.oms_getReal.restype = ctypes.c_int
self.obj.oms_getResultFile.argtypes = [ctypes.c_char_p, ctypes.POINTER(ctypes.c_char_p), ctypes.POINTER(ctypes.c_int)]
self.obj.oms_getResultFile.restype = ctypes.c_int
self.obj.oms_getSolver.argtypes = [ctypes.c_char_p, ctypes.POINTER(ctypes.c_int)]
self.obj.oms_getSolver.restype = ctypes.c_int
self.obj.oms_getStartTime.argtypes = [ctypes.c_char_p]
Expand Down Expand Up @@ -197,7 +199,7 @@ def exportDependencyGraphs(self, cref, initialization, event, simulation):
def exportSnapshot(self, ident):
contents = ctypes.c_char_p()
status = self.obj.oms_exportSnapshot(ident.encode(), ctypes.byref(contents))
return [contents.value.decode("utf-8"), status]
return [contents.value.decode('utf-8'), status]
def exportSSMTemplate(self, ident, filename):
return self.obj.oms_exportSSMTemplate(ident.encode(), filename.encode())
def exportSSVTemplate(self, ident, filename):
Expand All @@ -220,6 +222,11 @@ def getReal(self, cref):
value = ctypes.c_double()
status = self.obj.oms_getReal(cref.encode(), ctypes.byref(value))
return [value.value, status]
def getResultFile(self, cref):
filename = ctypes.c_char_p()
bufferSize = ctypes.c_int()
status = self.obj.oms_getResultFile(cref.encode(), ctypes.byref(filename), ctypes.byref(bufferSize))
return [filename.value.decode('utf-8'), bufferSize.value, status]
def getSolver(self, cref):
value = ctypes.c_int()
status = self.obj.oms_getSolver(cref.encode(), ctypes.byref(value))
Expand Down Expand Up @@ -262,11 +269,11 @@ def instantiate(self, cref):
def list(self, ident):
contents = ctypes.c_char_p()
status = self.obj.oms_list(ident.encode(), ctypes.byref(contents))
return [contents.value.decode("utf-8"), status]
return [contents.value.decode('utf-8'), status]
def listUnconnectedConnectors(self, ident):
contents = ctypes.c_char_p()
status = self.obj.oms_listUnconnectedConnectors(ident.encode(), ctypes.byref(contents))
return [contents.value.decode("utf-8"), status]
return [contents.value.decode('utf-8'), status]
def loadSnapshot(self, ident, snapshot):
newCref = ctypes.c_char_p()
status = self.obj.oms_loadSnapshot(ident.encode(), snapshot.encode(), ctypes.byref(newCref))
Expand Down

0 comments on commit 50daf1c

Please sign in to comment.