-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathtest_ModelicaDoEOMC.py
More file actions
164 lines (130 loc) · 4.09 KB
/
test_ModelicaDoEOMC.py
File metadata and controls
164 lines (130 loc) · 4.09 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
import pathlib
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_doe(tmp_path: pathlib.Path) -> pathlib.Path:
# see: https://trac.openmodelica.org/OpenModelica/ticket/4052
mod = tmp_path / "M.mo"
# TODO: update for bool and string parameters; check if these can be used in DoE
mod.write_text("""
model M
parameter Integer p=1;
parameter Integer q=1;
parameter Real a = -1;
parameter Real b = -1;
Real x[p];
Real y[q];
equation
der(x) = a * fill(1.0, p);
der(y) = b * fill(1.0, q);
end M;
""")
return mod
@pytest.fixture
def param_doe() -> dict[str, list]:
param = {
# structural
'p': [1, 2],
'q': [3, 4],
# simple
'a': [5, 6],
'b': [7, 8],
}
return param
def test_ModelicaDoEOMC_local(tmp_path, model_doe, param_doe):
tmpdir = tmp_path / 'DoE'
tmpdir.mkdir(exist_ok=True)
mod = OMPython.ModelicaSystemOMC()
mod.model(
model_file=model_doe,
model_name="M",
)
doe_mod = OMPython.ModelicaDoEOMC(
mod=mod,
parameters=param_doe,
resultpath=tmpdir,
simargs={"override": {'stopTime': '1.0'}},
)
_run_ModelicaDoEOMC(doe_mod=doe_mod)
@skip_on_windows
@skip_python_older_312
def test_ModelicaDoEOMC_docker(tmp_path, model_doe, param_doe):
omcs = OMPython.OMCSessionDocker(docker="openmodelica/openmodelica:v1.25.0-minimal")
omversion = omcs.sendExpression("getVersion()")
assert isinstance(omversion, str) and omversion.startswith("OpenModelica")
mod = OMPython.ModelicaSystemOMC(
session=omcs,
)
mod.model(
model_file=model_doe,
model_name="M",
)
doe_mod = OMPython.ModelicaDoEOMC(
mod=mod,
parameters=param_doe,
simargs={"override": {'stopTime': '1.0'}},
)
_run_ModelicaDoEOMC(doe_mod=doe_mod)
@pytest.mark.skip(reason="Not able to run WSL on github")
@skip_python_older_312
def test_ModelicaDoEOMC_WSL(tmp_path, model_doe, param_doe):
omcs = OMPython.OMCSessionWSL()
omversion = omcs.sendExpression("getVersion()")
assert isinstance(omversion, str) and omversion.startswith("OpenModelica")
mod = OMPython.ModelicaSystemOMC(
session=omcs,
)
mod.model(
model_file=model_doe,
model_name="M",
)
doe_mod = OMPython.ModelicaDoEOMC(
mod=mod,
parameters=param_doe,
simargs={"override": {'stopTime': '1.0'}},
)
_run_ModelicaDoEOMC(doe_mod=doe_mod)
def _run_ModelicaDoEOMC(doe_mod):
doe_count = doe_mod.prepare()
assert doe_count == 16
doe_def = doe_mod.get_doe_definition()
assert isinstance(doe_def, dict)
assert len(doe_def.keys()) == doe_count
doe_cmd = doe_mod.get_doe_command()
assert isinstance(doe_cmd, dict)
assert len(doe_cmd.keys()) == doe_count
doe_status = doe_mod.simulate()
assert doe_status is True
doe_sol = doe_mod.get_doe_solutions()
assert isinstance(doe_sol, dict)
assert len(doe_sol.keys()) == doe_count
assert sorted(doe_def.keys()) == sorted(doe_cmd.keys())
assert sorted(doe_cmd.keys()) == sorted(doe_sol.keys())
for resultfilename in doe_def:
row = doe_def[resultfilename]
assert resultfilename in doe_sol
sol = doe_sol[resultfilename]
var_dict = {
# simple / non-structural parameters
'a': float(row['a']),
'b': float(row['b']),
# structural parameters
'p': float(row['p']),
'q': float(row['q']),
# variables using the structural parameters
f"x[{row['p']}]": float(row['a']),
f"y[{row['p']}]": float(row['b']),
}
for var in var_dict:
assert var in sol['data']
assert np.isclose(sol['data'][var][-1], var_dict[var])