Skip to content

Commit 4f35230

Browse files
committed
[test_ModelicaDoERunner] add test case for ModelicaDoERunner
1 parent edc1408 commit 4f35230

1 file changed

Lines changed: 123 additions & 0 deletions

File tree

tests/test_ModelicaDoERunner.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import pathlib
2+
import sys
3+
4+
import numpy as np
5+
import pytest
6+
7+
import OMPython
8+
9+
skip_python_older_312 = pytest.mark.skipif(
10+
sys.version_info < (3, 12),
11+
reason="OMCPath(non-local) only working for Python >= 3.12.",
12+
)
13+
14+
15+
@pytest.fixture
16+
def model_doe(tmp_path: pathlib.Path) -> pathlib.Path:
17+
# see: https://trac.openmodelica.org/OpenModelica/ticket/4052
18+
mod = tmp_path / "M.mo"
19+
# TODO: update for bool and string parameters; check if these can be used in DoE
20+
mod.write_text("""
21+
model M
22+
parameter Integer p=1;
23+
parameter Integer q=1;
24+
parameter Real a = -1;
25+
parameter Real b = -1;
26+
Real x[p];
27+
Real y[q];
28+
equation
29+
der(x) = a * fill(1.0, p);
30+
der(y) = b * fill(1.0, q);
31+
end M;
32+
""")
33+
return mod
34+
35+
36+
@pytest.fixture
37+
def param_doe() -> dict[str, list]:
38+
param = {
39+
# simple
40+
'a': [5, 6],
41+
'b': [7, 8],
42+
}
43+
return param
44+
45+
46+
def test_ModelicaDoEOMC_local(tmp_path, model_doe, param_doe):
47+
tmpdir = tmp_path / 'DoE'
48+
tmpdir.mkdir(exist_ok=True)
49+
50+
mod = OMPython.ModelicaSystemOMC()
51+
mod.model(
52+
model_file=model_doe,
53+
model_name="M",
54+
)
55+
56+
resultfile_mod = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_mod.mat"
57+
_run_simulation(mod=mod, resultfile=resultfile_mod, param=param_doe)
58+
59+
doe_mod = OMPython.ModelicaDoERunner(
60+
mod=mod,
61+
parameters=param_doe,
62+
resultpath=tmpdir,
63+
)
64+
65+
_run_ModelicaDoERunner(doe_mod=doe_mod)
66+
67+
_check_runner_result(mod=mod, doe_mod=doe_mod)
68+
69+
70+
def _run_simulation(mod, resultfile, param):
71+
simOptions = {"stopTime": 1.0, "stepSize": 0.1, "tolerance": 1e-8}
72+
mod.setSimulationOptions(**simOptions)
73+
mod.simulate(resultfile=resultfile)
74+
75+
assert resultfile.exists()
76+
77+
78+
def _run_ModelicaDoERunner(doe_mod):
79+
doe_count = doe_mod.prepare()
80+
assert doe_count == 16
81+
82+
doe_def = doe_mod.get_doe_definition()
83+
assert isinstance(doe_def, dict)
84+
assert len(doe_def.keys()) == doe_count
85+
86+
doe_cmd = doe_mod.get_doe_command()
87+
assert isinstance(doe_cmd, dict)
88+
assert len(doe_cmd.keys()) == doe_count
89+
90+
doe_status = doe_mod.simulate()
91+
assert doe_status is True
92+
93+
94+
def _check_runner_result(mod, doe_mod):
95+
doe_cmd = doe_mod.get_doe_command()
96+
doe_def = doe_mod.get_doe_definition()
97+
98+
doe_sol = OMPython.doe_get_solutions(
99+
msomc=mod,
100+
resultpath=doe_mod.get_resultpath(),
101+
doe_def=doe_def,
102+
)
103+
assert isinstance(doe_sol, dict)
104+
assert len(doe_sol.keys()) == len(doe_cmd.keys())
105+
106+
assert sorted(doe_def.keys()) == sorted(doe_cmd.keys())
107+
assert sorted(doe_cmd.keys()) == sorted(doe_sol.keys())
108+
109+
for resultfilename in doe_def:
110+
row = doe_def[resultfilename]
111+
112+
assert resultfilename in doe_sol
113+
sol = doe_sol[resultfilename]
114+
115+
var_dict = {
116+
# simple / non-structural parameters
117+
'a': float(row['a']),
118+
'b': float(row['b']),
119+
}
120+
121+
for var in var_dict:
122+
assert var in sol['data']
123+
assert np.isclose(sol['data'][var][-1], var_dict[var])

0 commit comments

Comments
 (0)