Skip to content

Commit f14d022

Browse files
committed
[test_ModelicaSystemRunner] update test case
* ModelicaSystemRunner & OMCPath * ModelicaSystemRunner & OMPathRunnerLocal * ModelicaSystemRunner & OMPathRunnerBash * ModelicaSystemRunner & OMPathRunnerBash using docker * ModelicaSystemRunner & OMPathRunnerBash using WSL (not tested!)
1 parent 20bb002 commit f14d022

1 file changed

Lines changed: 174 additions & 2 deletions

File tree

tests/test_ModelicaSystemRunner.py

Lines changed: 174 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
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+
skip_python_older_312 = pytest.mark.skipif(
15+
sys.version_info < (3, 12),
16+
reason="OMCPath(non-local) only working for Python >= 3.12.",
17+
)
18+
19+
720
@pytest.fixture
821
def model_firstorder_content():
922
return """
@@ -37,9 +50,43 @@ def param():
3750
}
3851

3952

40-
def test_runner(model_firstorder, param):
53+
def test_ModelicaSystemRunner_OMC(model_firstorder, param):
54+
# create a model using ModelicaSystem
55+
mod = OMPython.ModelicaSystemOMC()
56+
mod.model(
57+
model_file=model_firstorder,
58+
model_name="M",
59+
)
60+
61+
resultfile_mod = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_mod.mat"
62+
_run_simulation(mod=mod, resultfile=resultfile_mod, param=param)
63+
64+
# run the model using only the runner class
65+
omcs = OMPython.OMSessionRunner(
66+
version=mod.get_session().get_version(),
67+
)
68+
modr = OMPython.ModelicaSystemRunner(
69+
session=omcs,
70+
work_directory=mod.getWorkDirectory(),
71+
)
72+
modr.setup(
73+
model_name="M",
74+
)
75+
76+
resultfile_modr = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_modr.mat"
77+
_run_simulation(mod=modr, resultfile=resultfile_modr, param=param)
78+
79+
# cannot check the content as runner does not have the capability to open a result file
80+
assert resultfile_mod.size() == resultfile_modr.size()
81+
82+
# check results
83+
_check_result(mod=mod, resultfile=resultfile_mod, param=param)
84+
_check_result(mod=mod, resultfile=resultfile_modr, param=param)
85+
86+
87+
def test_ModelicaSystemRunner_local(model_firstorder, param):
4188
# create a model using ModelicaSystem
42-
mod = OMPython.ModelicaSystem()
89+
mod = OMPython.ModelicaSystemOMC()
4390
mod.model(
4491
model_file=model_firstorder,
4592
model_name="M",
@@ -51,9 +98,134 @@ def test_runner(model_firstorder, param):
5198
# run the model using only the runner class
5299
omcs = OMPython.OMSessionRunner(
53100
version=mod.get_session().get_version(),
101+
ompath_runner=OMPython.OMPathRunnerLocal,
102+
)
103+
modr = OMPython.ModelicaSystemRunner(
104+
session=omcs,
105+
work_directory=mod.getWorkDirectory(),
106+
)
107+
modr.setup(
108+
model_name="M",
109+
)
110+
111+
resultfile_modr = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_modr.mat"
112+
_run_simulation(mod=modr, resultfile=resultfile_modr, param=param)
113+
114+
# cannot check the content as runner does not have the capability to open a result file
115+
assert resultfile_mod.size() == resultfile_modr.size()
116+
117+
# check results
118+
_check_result(mod=mod, resultfile=resultfile_mod, param=param)
119+
_check_result(mod=mod, resultfile=resultfile_modr, param=param)
120+
121+
122+
@skip_on_windows
123+
def test_ModelicaSystemRunner_bash(model_firstorder, param):
124+
# create a model using ModelicaSystem
125+
mod = OMPython.ModelicaSystemOMC()
126+
mod.model(
127+
model_file=model_firstorder,
128+
model_name="M",
129+
)
130+
131+
resultfile_mod = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_mod.mat"
132+
_run_simulation(mod=mod, resultfile=resultfile_mod, param=param)
133+
134+
# run the model using only the runner class
135+
omcsr = OMPython.OMSessionRunner(
136+
version=mod.get_session().get_version(),
137+
ompath_runner=OMPython.OMPathRunnerBash,
138+
)
139+
modr = OMPython.ModelicaSystemRunner(
140+
session=omcsr,
141+
work_directory=mod.getWorkDirectory(),
142+
)
143+
modr.setup(
144+
model_name="M",
145+
)
146+
147+
resultfile_modr = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_modr.mat"
148+
_run_simulation(mod=modr, resultfile=resultfile_modr, param=param)
149+
150+
# cannot check the content as runner does not have the capability to open a result file
151+
assert resultfile_mod.size() == resultfile_modr.size()
152+
153+
# check results
154+
_check_result(mod=mod, resultfile=resultfile_mod, param=param)
155+
_check_result(mod=mod, resultfile=resultfile_modr, param=param)
156+
157+
158+
@skip_on_windows
159+
@skip_python_older_312
160+
def test_ModelicaSystemRunner_bash_docker(model_firstorder, param):
161+
omcs = OMPython.OMCSessionDocker(docker="openmodelica/openmodelica:v1.25.0-minimal")
162+
assert omcs.get_version() == "OpenModelica 1.25.0"
163+
164+
# create a model using ModelicaSystem
165+
mod = OMPython.ModelicaSystemOMC(
166+
session=omcs,
167+
)
168+
mod.model(
169+
model_file=model_firstorder,
170+
model_name="M",
171+
)
172+
173+
resultfile_mod = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_mod.mat"
174+
_run_simulation(mod=mod, resultfile=resultfile_mod, param=param)
175+
176+
# run the model using only the runner class
177+
omcsr = OMPython.OMSessionRunner(
178+
version=mod.get_session().get_version(),
179+
cmd_prefix=omcs.model_execution_prefix(cwd=mod.getWorkDirectory()),
180+
ompath_runner=OMPython.OMPathRunnerBash,
181+
model_execution_local=False,
54182
)
55183
modr = OMPython.ModelicaSystemRunner(
184+
session=omcsr,
185+
work_directory=mod.getWorkDirectory(),
186+
)
187+
modr.setup(
188+
model_name="M",
189+
)
190+
191+
resultfile_modr = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_modr.mat"
192+
_run_simulation(mod=modr, resultfile=resultfile_modr, param=param)
193+
194+
# cannot check the content as runner does not have the capability to open a result file
195+
assert resultfile_mod.size() == resultfile_modr.size()
196+
197+
# check results
198+
_check_result(mod=mod, resultfile=resultfile_mod, param=param)
199+
_check_result(mod=mod, resultfile=resultfile_modr, param=param)
200+
201+
202+
@pytest.mark.skip(reason="Not able to run WSL on github")
203+
@skip_python_older_312
204+
def test_ModelicaSystemDoE_WSL(tmp_path, model_doe, param_doe):
205+
omcs = OMPython.OMCSessionWSL()
206+
assert omcs.get_version() == "OpenModelica 1.25.0"
207+
208+
# create a model using ModelicaSystem
209+
mod = OMPython.ModelicaSystemOMC(
56210
session=omcs,
211+
)
212+
mod.model(
213+
model_file=model_firstorder,
214+
model_name="M",
215+
)
216+
217+
resultfile_mod = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_mod.mat"
218+
_run_simulation(mod=mod, resultfile=resultfile_mod, param=param)
219+
220+
# run the model using only the runner class
221+
omcsr = OMPython.OMSessionRunner(
222+
version=mod.get_session().get_version(),
223+
cmd_prefix=omcs.model_execution_prefix(cwd=mod.getWorkDirectory()),
224+
ompath_runner=OMPython.OMPathRunnerBash,
225+
model_execution_local=False,
226+
)
227+
modr = OMPython.ModelicaSystemRunner(
228+
session=omcsr,
57229
work_directory=mod.getWorkDirectory(),
58230
)
59231
modr.setup(

0 commit comments

Comments
 (0)