Skip to content

Commit 827f7bd

Browse files
committed
??? run_doe
1 parent f2725c9 commit 827f7bd

1 file changed

Lines changed: 122 additions & 0 deletions

File tree

tools/run_doe.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import numpy as np
2+
import OMPython
3+
import pathlib
4+
import logging
5+
6+
logging.basicConfig(level=logging.WARNING) # DEBUG)
7+
8+
logger = logging.getLogger(__name__)
9+
10+
def model_doe(tmp_path: pathlib.Path) -> pathlib.Path:
11+
# see: https://trac.openmodelica.org/OpenModelica/ticket/4052
12+
mod = tmp_path / "M_local.mo"
13+
mod.write_text("""
14+
model M
15+
parameter Integer p=1;
16+
parameter Integer q=1;
17+
parameter Real a = -1;
18+
parameter Real b = -1;
19+
Real x[p];
20+
Real y[q];
21+
equation
22+
der(x) = a * fill(1.0, p);
23+
der(y) = b * fill(1.0, q);
24+
end M;
25+
""")
26+
return mod
27+
28+
29+
def param_doe() -> dict[str, list]:
30+
param = {
31+
# structural
32+
'p': [1, 2],
33+
'q': [3, 4],
34+
# simple
35+
'a': [5, 6],
36+
'b': [7, 8],
37+
}
38+
return param
39+
40+
41+
def test_ModelicaSystemDoE(tmp_path, model_doe, param_doe):
42+
tmpdir = tmp_path / 'DoE'
43+
tmpdir.mkdir(exist_ok=True)
44+
45+
# om = OMPython.OMCSessionZMQ()
46+
omcp = OMPython.OMCProcessDocker(docker="openmodelica/openmodelica:v1.25.0-minimal")
47+
om = OMPython.OMCSessionZMQ(omc_process=omcp)
48+
49+
model_path = om.omcpath_tempdir()
50+
model_file = model_path / 'M.mo'
51+
model_file.write_text(model_doe.read_text())
52+
53+
model_path_doe = model_path / 'simDoE'
54+
model_path_doe.mkdir(exist_ok=True)
55+
56+
doe_mod = OMPython.ModelicaSystemDoE(
57+
fileName=model_file,
58+
modelName="M",
59+
parameters=param_doe,
60+
resultpath=result_path_doe,
61+
simargs={"override": {'stopTime': 1.0}},
62+
omc_process=om.omc_process,
63+
)
64+
doe_count = doe_mod.prepare()
65+
assert doe_count == 16
66+
67+
doe_dict = doe_mod.get_doe()
68+
assert isinstance(doe_dict, dict)
69+
assert len(doe_dict.keys()) == 16
70+
71+
doe_status = doe_mod.simulate()
72+
assert doe_status is True
73+
74+
doe_sol = doe_mod.get_solutions()
75+
76+
for resultfilename in doe_dict:
77+
row = doe_dict[resultfilename]
78+
79+
assert resultfilename in doe_sol
80+
sol = doe_sol[resultfilename]
81+
82+
var_dict = {
83+
# simple / non-structural parameters
84+
'a': float(row['a']),
85+
'b': float(row['b']),
86+
# structural parameters
87+
'p': float(row['p']),
88+
'q': float(row['q']),
89+
# variables using the structural parameters
90+
f"x[{row['p']}]": float(row['a']),
91+
f"y[{row['p']}]": float(row['b']),
92+
}
93+
94+
for var in var_dict:
95+
assert var in sol['data']
96+
assert np.isclose(sol['data'][var][-1], var_dict[var])
97+
98+
99+
if __name__ == "__main__":
100+
# om = OMPython.OMCSessionZMQ()
101+
omcp = OMPython.OMCProcessDocker(docker="openmodelica/openmodelica:v1.25.0-minimal")
102+
om = OMPython.OMCSessionZMQ(omc_process=omcp)
103+
104+
tempdir_local = pathlib.Path('.') / 'DoE'
105+
tempdir_local = tempdir_local.resolve().absolute()
106+
tempdir_local.mkdir(exist_ok=True)
107+
tempdir_omcpath = om.omcpath_tempdir()
108+
109+
model_file_local = model_doe(tempdir_local)
110+
model_file_omcpath = tempdir_omcpath / 'M.mo'
111+
model_file_omcpath.write_text(model_file_local.read_text())
112+
113+
param = param_doe()
114+
115+
test_ModelicaSystemDoE(
116+
tmp_path=tempdir_omcpath,
117+
model_doe=model_file_omcpath,
118+
param_doe=param,
119+
result_path_doe=tempdir_omcpath,
120+
)
121+
122+
print("DONE")

0 commit comments

Comments
 (0)