-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathtest_ModelicaSystemRunner.py
More file actions
270 lines (216 loc) · 8.47 KB
/
Copy pathtest_ModelicaSystemRunner.py
File metadata and controls
270 lines (216 loc) · 8.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import sys
import numpy as np
import pytest
import OMPython
skip_on_windows = pytest.mark.skipif(
sys.platform.startswith("win"),
reason="OpenModelica Docker image is Linux-only; skipping on Windows.",
)
skip_python_older_312 = pytest.mark.skipif(
sys.version_info < (3, 12),
reason="OMCPath(non-local) only working for Python >= 3.12.",
)
@pytest.fixture
def model_firstorder_content():
return """
model M
Real x(start = 1, fixed = true);
parameter Real a = -1;
equation
der(x) = x*a;
end M;
"""
@pytest.fixture
def model_firstorder(tmp_path, model_firstorder_content):
mod = tmp_path / "M.mo"
mod.write_text(model_firstorder_content)
return mod
@pytest.fixture
def param():
x0 = 1
a = -1
tau = -1 / a
stopTime = 5*tau
return {
'x0': x0,
'a': a,
'stopTime': stopTime,
}
def test_ModelicaSystemRunner_OMC(model_firstorder, param):
# create a model using ModelicaSystem
mod = OMPython.ModelicaSystemOMC()
mod.model(
model_file=model_firstorder,
model_name="M",
)
resultfile_mod = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_mod.mat"
_run_simulation(mod=mod, resultfile=resultfile_mod, param=param)
# run the model using only the runner class
omcs = OMPython.OMSessionRunner(
version=mod.get_session().get_version(),
)
modr = OMPython.ModelicaSystemRunner(
session=omcs,
work_directory=mod.getWorkDirectory(),
)
modr.setup(
model_name="M",
)
resultfile_modr = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_modr.mat"
_run_simulation(mod=modr, resultfile=resultfile_modr, param=param)
# cannot check the content as runner does not have the capability to open a result file
assert resultfile_mod.size() == resultfile_modr.size()
# check results
_check_result(mod=mod, resultfile=resultfile_mod, param=param)
_check_result(mod=mod, resultfile=resultfile_modr, param=param)
def test_ModelicaSystemRunner_local(model_firstorder, param):
# create a model using ModelicaSystem
mod = OMPython.ModelicaSystemOMC()
mod.model(
model_file=model_firstorder,
model_name="M",
)
resultfile_mod = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_mod.mat"
_run_simulation(mod=mod, resultfile=resultfile_mod, param=param)
# run the model using only the runner class
omcs = OMPython.OMSessionRunner(
version=mod.get_session().get_version(),
ompath_runner=OMPython.OMPathRunnerLocal,
)
modr = OMPython.ModelicaSystemRunner(
session=omcs,
work_directory=mod.getWorkDirectory(),
)
modr.setup(
model_name="M",
)
resultfile_modr = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_modr.mat"
_run_simulation(mod=modr, resultfile=resultfile_modr, param=param)
# cannot check the content as runner does not have the capability to open a result file
assert resultfile_mod.size() == resultfile_modr.size()
# check results
_check_result(mod=mod, resultfile=resultfile_mod, param=param)
_check_result(mod=mod, resultfile=resultfile_modr, param=param)
@skip_on_windows
def test_ModelicaSystemRunner_bash(model_firstorder, param):
# create a model using ModelicaSystem
mod = OMPython.ModelicaSystemOMC()
mod.model(
model_file=model_firstorder,
model_name="M",
)
resultfile_mod = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_mod.mat"
_run_simulation(mod=mod, resultfile=resultfile_mod, param=param)
# run the model using only the runner class
omcsr = OMPython.OMSessionRunner(
version=mod.get_session().get_version(),
ompath_runner=OMPython.OMPathRunnerBash,
)
modr = OMPython.ModelicaSystemRunner(
session=omcsr,
work_directory=mod.getWorkDirectory(),
)
modr.setup(
model_name="M",
)
resultfile_modr = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_modr.mat"
_run_simulation(mod=modr, resultfile=resultfile_modr, param=param)
# cannot check the content as runner does not have the capability to open a result file
assert resultfile_mod.size() == resultfile_modr.size()
# check results
_check_result(mod=mod, resultfile=resultfile_mod, param=param)
_check_result(mod=mod, resultfile=resultfile_modr, param=param)
@skip_on_windows
@skip_python_older_312
def test_ModelicaSystemRunner_bash_docker(model_firstorder, param):
omcs = OMPython.OMCSessionDocker(docker="openmodelica/openmodelica:v1.25.0-minimal")
omversion = omcs.sendExpression("getVersion()")
assert isinstance(omversion, str) and omversion.startswith("OpenModelica")
# create a model using ModelicaSystem
mod = OMPython.ModelicaSystemOMC(
session=omcs,
)
mod.model(
model_file=model_firstorder,
model_name="M",
)
resultfile_mod = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_mod.mat"
_run_simulation(mod=mod, resultfile=resultfile_mod, param=param)
# run the model using only the runner class
omcsr = OMPython.OMSessionRunner(
version=mod.get_session().get_version(),
cmd_prefix=omcs.model_execution_prefix(cwd=mod.getWorkDirectory()),
ompath_runner=OMPython.OMPathRunnerBash,
model_execution_local=False,
)
modr = OMPython.ModelicaSystemRunner(
session=omcsr,
work_directory=mod.getWorkDirectory(),
)
modr.setup(
model_name="M",
)
resultfile_modr = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_modr.mat"
_run_simulation(mod=modr, resultfile=resultfile_modr, param=param)
# cannot check the content as runner does not have the capability to open a result file
assert resultfile_mod.size() == resultfile_modr.size()
# check results
_check_result(mod=mod, resultfile=resultfile_mod, param=param)
_check_result(mod=mod, resultfile=resultfile_modr, param=param)
@pytest.mark.skip(reason="Not able to run WSL on github")
@skip_python_older_312
def test_ModelicaSystemDoE_WSL(tmp_path, model_doe, param_doe):
omcs = OMPython.OMCSessionWSL()
omversion = omcs.sendExpression("getVersion()")
assert isinstance(omversion, str) and omversion.startswith("OpenModelica")
# create a model using ModelicaSystem
mod = OMPython.ModelicaSystemOMC(
session=omcs,
)
mod.model(
model_file=model_firstorder,
model_name="M",
)
resultfile_mod = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_mod.mat"
_run_simulation(mod=mod, resultfile=resultfile_mod, param=param)
# run the model using only the runner class
omcsr = OMPython.OMSessionRunner(
version=mod.get_session().get_version(),
cmd_prefix=omcs.model_execution_prefix(cwd=mod.getWorkDirectory()),
ompath_runner=OMPython.OMPathRunnerBash,
model_execution_local=False,
)
modr = OMPython.ModelicaSystemRunner(
session=omcsr,
work_directory=mod.getWorkDirectory(),
)
modr.setup(
model_name="M",
)
resultfile_modr = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_modr.mat"
_run_simulation(mod=modr, resultfile=resultfile_modr, param=param)
# cannot check the content as runner does not have the capability to open a result file
assert resultfile_mod.size() == resultfile_modr.size()
# check results
_check_result(mod=mod, resultfile=resultfile_mod, param=param)
_check_result(mod=mod, resultfile=resultfile_modr, param=param)
def _run_simulation(mod, resultfile, param):
simOptions = {"stopTime": param['stopTime'], "stepSize": 0.1, "tolerance": 1e-8}
mod.setSimulationOptions(**simOptions)
mod.simulate(resultfile=resultfile)
assert resultfile.exists()
def _check_result(mod, resultfile, param):
x = mod.getSolutions(resultfile=resultfile, varList="x")
t, x2 = mod.getSolutions(resultfile=resultfile, varList=["time", "x"])
assert (x2 == x).all()
sol_names = mod.getSolutions(resultfile=resultfile)
assert isinstance(sol_names, tuple)
assert "time" in sol_names
assert "x" in sol_names
assert "der(x)" in sol_names
with pytest.raises(OMPython.ModelicaSystemError):
mod.getSolutions(resultfile=resultfile, varList="thisVariableDoesNotExist")
assert np.isclose(t[0], 0), "time does not start at 0"
assert np.isclose(t[-1], param['stopTime']), "time does not end at stopTime"
x_analytical = param['x0'] * np.exp(param['a']*t)
assert np.isclose(x, x_analytical, rtol=1e-4).all()