Skip to content

Commit 9643653

Browse files
committed
?? MSDOE working without clone / one OMCSession
1 parent 0ee0aab commit 9643653

2 files changed

Lines changed: 28 additions & 42 deletions

File tree

OMPython/ModelicaSystem.py

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def definition(self) -> OMCSessionRunData:
212212
cmd_timeout=self._timeout,
213213
)
214214

215-
omc_run_data_updated = self._session.omc_run_data_update(omc_run_data)
215+
omc_run_data_updated = self._session.omc_run_data_update(omc_run_data=omc_run_data)
216216

217217
return omc_run_data_updated
218218

@@ -1808,20 +1808,10 @@ def prepare(self) -> int:
18081808

18091809
self._doe_def = {}
18101810
for idx_pc_structure, pc_structure in enumerate(param_structure_combinations):
1811-
mod_structure_dir = self._mod.getWorkDirectory() / f"DOE_{idx_pc_structure:09d}"
1812-
mod_structure_dir.mkdir()
1813-
1814-
mod_structure = ModelicaSystem(
1815-
fileName=self._file_name,
1816-
modelName=self._model_name,
1817-
lmodel=self._lmodel,
1818-
commandLineOptions=self._command_line_options,
1819-
variableFilter=self._variable_filter,
1820-
customBuildDirectory=mod_structure_dir,
1821-
omhome=self._omhome,
1822-
omc_process=self._omc_process,
1823-
build=False,
1824-
)
1811+
1812+
build_dir = self._resultpath / f"DOE_{idx_pc_structure:09d}"
1813+
build_dir.mkdir()
1814+
self._mod.setTempDirectory(build_dir)
18251815

18261816
sim_param_structure = {}
18271817
for idx_structure, pk_structure in enumerate(param_structure.keys()):
@@ -1835,12 +1825,12 @@ def prepare(self) -> int:
18351825
expression = f"setParameterValue({self._model_name}, {pk_structure}, {pk_value_bool_str});"
18361826
else:
18371827
expression = f"setParameterValue({self._model_name}, {pk_structure}, {pk_value})"
1838-
res = mod_structure.sendExpression(expression)
1828+
res = self._mod.sendExpression(expression)
18391829
if not res:
18401830
raise ModelicaSystemError(f"Cannot set structural parameter {self._model_name}.{pk_structure} "
18411831
f"to {pk_value} using {repr(expression)}")
18421832

1843-
mod_structure.buildModel(variableFilter=self._variable_filter)
1833+
self._mod.buildModel(variableFilter=self._variable_filter)
18441834

18451835
for idx_pc_simple, pc_simple in enumerate(param_simple_combinations):
18461836
# TODO: check mypy error message / type hints
@@ -1870,9 +1860,9 @@ def prepare(self) -> int:
18701860

18711861
self._doe_def[resfilename] = df_data
18721862

1873-
mod_structure.setParameters(sim_param_simple)
1863+
self._mod.setParameters(sim_param_simple)
18741864

1875-
mscmd = mod_structure.simulate_cmd(
1865+
mscmd = self._mod.simulate_cmd(
18761866
result_file=resultfile,
18771867
timeout=self._timeout,
18781868
)
@@ -1882,19 +1872,11 @@ def prepare(self) -> int:
18821872

18831873
cmd_definition = mscmd.definition()
18841874

1885-
# TODO: fix path to executable; copy it using OMCPath.copy() => no clone; only one OMC!
1886-
# exe = exe.parent / (DOE name)- exe.name
1887-
18881875
doe_sim[resultfile.name] = cmd_definition
18891876

18901877
del mscmd
18911878

1892-
logger.info(f'Status OMC: {mod_structure._getconn.sendExpression("getVersion()")}')
1893-
1894-
# cleanup; ensure that there is not too much RAM consumption
1895-
logger.warning("DEL")
1896-
del mod_structure._getconn
1897-
del mod_structure
1879+
logger.info(f'Status OMC: {self._mod._getconn.sendExpression("getVersion()")}')
18981880

18991881
logger.info(f"Prepared {len(doe_sim)} simulation definitions for the defined DoE.")
19001882

tools/run_doe.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ def param_doe() -> dict[str, list]:
3838
return param
3939

4040

41-
def test_ModelicaSystemDoE(tmp_path, model_doe, param_doe):
42-
tmpdir = tmp_path / 'DoE'
43-
tmpdir.mkdir(exist_ok=True)
41+
def test_ModelicaSystemDoE(tmp_path, model_doe, param_doe, om):
42+
# tmpdir = tmp_path / 'DoE'
43+
# tmpdir.mkdir(exist_ok=True)
4444

4545
# om = OMPython.OMCSessionZMQ()
46-
omcp = OMPython.OMCProcessDocker(docker="openmodelica/openmodelica:v1.25.0-minimal")
47-
om = OMPython.OMCSessionZMQ(omc_process=omcp)
46+
# omcp = OMPython.OMCProcessDocker(docker="openmodelica/openmodelica:v1.25.0-minimal")
47+
# om = OMPython.OMCSessionZMQ(omc_process=omcp)
4848

4949
model_path = om.omcpath_tempdir()
5050
model_file = model_path / 'M.mo'
@@ -64,14 +64,14 @@ def test_ModelicaSystemDoE(tmp_path, model_doe, param_doe):
6464
doe_count = doe_mod.prepare()
6565
assert doe_count == 16
6666

67-
doe_dict = doe_mod.get_doe()
67+
doe_dict = doe_mod.get_doe_definition()
6868
assert isinstance(doe_dict, dict)
6969
assert len(doe_dict.keys()) == 16
7070

7171
doe_status = doe_mod.simulate()
7272
assert doe_status is True
7373

74-
doe_sol = doe_mod.get_solutions()
74+
doe_sol = doe_mod.get_doe_solutions()
7575

7676
for resultfilename in doe_dict:
7777
row = doe_dict[resultfilename]
@@ -93,29 +93,33 @@ def test_ModelicaSystemDoE(tmp_path, model_doe, param_doe):
9393

9494
for var in var_dict:
9595
assert var in sol['data']
96+
# logger.info(f"{resultfilename}:{var} {sol['data'][var][-1]} <> {var_dict[var]}")
9697
assert np.isclose(sol['data'][var][-1], var_dict[var])
9798

99+
doe_def = doe_mod.get_doe_definition()
100+
doe_cmd = doe_mod.get_doe_command()
101+
102+
# logger.warning(doe_def)
103+
# logger.warning(doe_cmd)
104+
# logger.warning(doe_sol)
105+
98106

99107
if __name__ == "__main__":
100108
# om = OMPython.OMCSessionZMQ()
101109
omcp = OMPython.OMCProcessDocker(docker="openmodelica/openmodelica:v1.25.0-minimal")
102110
om = OMPython.OMCSessionZMQ(omc_process=omcp)
103111

104-
tempdir_local = pathlib.Path('.') / 'DoE'
105-
tempdir_local = tempdir_local.resolve().absolute()
106-
tempdir_local.mkdir(exist_ok=True)
107112
tempdir_omcpath = om.omcpath_tempdir()
108113

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())
114+
model_file_local = model_doe(tempdir_omcpath)
112115

113116
param = param_doe()
114117

115118
test_ModelicaSystemDoE(
116119
tmp_path=tempdir_omcpath,
117-
model_doe=model_file_omcpath,
120+
model_doe=model_file_local,
118121
param_doe=param,
122+
om=om,
119123
)
120124

121125
print("DONE")

0 commit comments

Comments
 (0)