Skip to content

Commit 3de5e6d

Browse files
committed
??? fix runner
1 parent bc9eff5 commit 3de5e6d

2 files changed

Lines changed: 63 additions & 6 deletions

File tree

OMPython/OMCSession.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ def __init__(
644644
self.omc_process = omc_process
645645

646646
def __del__(self):
647-
if hasattr('omc_process', self):
647+
if hasattr(self, 'omc_process'):
648648
del self.omc_process
649649

650650
@staticmethod
@@ -2078,20 +2078,24 @@ def __init__(
20782078
self,
20792079
timeout: float = 10.00,
20802080
version: str = "1.27.0",
2081-
cmd_prefix: Optional[list[str]] = None,
20822081
ompath_runner: Type[OMPathRunnerABC] = OMPathRunnerLocal,
2082+
cmd_prefix: Optional[list[str]] = None,
2083+
model_execution_local: bool = True,
20832084
) -> None:
20842085
super().__init__(timeout=timeout)
2085-
self.model_execution_local = True
20862086
self._version = version
20872087

20882088
if not issubclass(ompath_runner, OMPathRunnerABC):
20892089
raise OMCSessionException(f"Invalid OMPathRunner class: {type(ompath_runner)}!")
20902090
self._ompath_runner = ompath_runner
20912091

2092+
self.model_execution_local = model_execution_local
20922093
if cmd_prefix is not None:
20932094
self._cmd_prefix = cmd_prefix
20942095

2096+
# TODO: some checking?!
2097+
# if ompath_runner == Type[OMPathRunnerBash]:
2098+
20952099
def __post_init__(self) -> None:
20962100
"""
20972101
No connection to an OMC server is created by this class!
@@ -2101,7 +2105,7 @@ def model_execution_prefix(self, cwd: Optional[OMPathABC] = None) -> list[str]:
21012105
"""
21022106
Helper function which returns a command prefix.
21032107
"""
2104-
return []
2108+
return self.get_cmd_prefix()
21052109

21062110
def get_version(self) -> str:
21072111
"""
@@ -2112,9 +2116,10 @@ def get_version(self) -> str:
21122116

21132117
def set_workdir(self, workdir: OMPathABC) -> None:
21142118
"""
2115-
Set the workdir for this session.
2119+
Set the workdir for this session. For OMSessionRunner this is a nop. The workdir must be defined within the
2120+
definition of cmd_prefix.
21162121
"""
2117-
os.chdir(workdir.as_posix())
2122+
pass
21182123

21192124
def omcpath(self, *path) -> OMPathABC:
21202125
"""

tests/test_ModelicaSystemRunner.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
import sys
2+
13
import numpy as np
24
import pytest
35

46
import OMPython
57

68

9+
skip_on_windows = pytest.mark.skipif(
10+
sys.platform.startswith("win"),
11+
reason="OpenModelica Docker image is Linux-only; skipping on Windows.",
12+
)
13+
14+
715
@pytest.fixture
816
def model_firstorder_content():
917
return """
@@ -71,6 +79,50 @@ def test_ModelicaSystemRunner(model_firstorder, param):
7179
_check_result(mod=mod, resultfile=resultfile_modr, param=param)
7280

7381

82+
@skip_on_windows
83+
def test_ModelicaSystemRunner_bash_docker(model_firstorder, param):
84+
omcp = OMPython.OMCSessionDocker(docker="openmodelica/openmodelica:v1.25.0-minimal")
85+
om = OMPython.OMCSessionZMQ(omc_process=omcp)
86+
assert om.sendExpression("getVersion()") == "OpenModelica 1.25.0"
87+
88+
# create a model using ModelicaSystem
89+
mod = OMPython.ModelicaSystemOMC(
90+
session=omcp,
91+
)
92+
mod.model(
93+
model_file=model_firstorder,
94+
model_name="M",
95+
)
96+
97+
resultfile_mod = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_mod.mat"
98+
_run_simulation(mod=mod, resultfile=resultfile_mod, param=param)
99+
100+
# run the model using only the runner class
101+
omcs = OMPython.OMSessionRunner(
102+
version=mod.get_session().get_version(),
103+
cmd_prefix=omcp.model_execution_prefix(cwd=mod.getWorkDirectory()),
104+
ompath_runner=OMPython.OMPathRunnerBash,
105+
model_execution_local=False,
106+
)
107+
modr = OMPython.ModelicaSystemRunner(
108+
session=omcs,
109+
work_directory=mod.getWorkDirectory(),
110+
)
111+
modr.setup(
112+
model_name="M",
113+
)
114+
115+
resultfile_modr = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_modr.mat"
116+
_run_simulation(mod=modr, resultfile=resultfile_modr, param=param)
117+
118+
# cannot check the content as runner does not have the capability to open a result file
119+
assert resultfile_mod.size() == resultfile_modr.size()
120+
121+
# check results
122+
_check_result(mod=mod, resultfile=resultfile_mod, param=param)
123+
_check_result(mod=mod, resultfile=resultfile_modr, param=param)
124+
125+
74126
def _run_simulation(mod, resultfile, param):
75127
simOptions = {"stopTime": param['stopTime'], "stepSize": 0.1, "tolerance": 1e-8}
76128
mod.setSimulationOptions(**simOptions)

0 commit comments

Comments
 (0)