|
| 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 | + simargs={"override": {'stopTime': '1.0'}}, |
| 64 | + ) |
| 65 | + |
| 66 | + _run_ModelicaDoERunner(doe_mod=doe_mod) |
| 67 | + |
| 68 | + _check_runner_result(mod=mod, doe_mod=doe_mod) |
| 69 | + |
| 70 | + |
| 71 | +def _run_simulation(mod, resultfile, param): |
| 72 | + simOptions = {"stopTime": param['stopTime'], "stepSize": 0.1, "tolerance": 1e-8} |
| 73 | + mod.setSimulationOptions(**simOptions) |
| 74 | + mod.simulate(resultfile=resultfile) |
| 75 | + |
| 76 | + assert resultfile.exists() |
| 77 | + |
| 78 | + |
| 79 | +def _run_ModelicaDoERunner(doe_mod): |
| 80 | + doe_count = doe_mod.prepare() |
| 81 | + assert doe_count == 16 |
| 82 | + |
| 83 | + doe_def = doe_mod.get_doe_definition() |
| 84 | + assert isinstance(doe_def, dict) |
| 85 | + assert len(doe_def.keys()) == doe_count |
| 86 | + |
| 87 | + doe_cmd = doe_mod.get_doe_command() |
| 88 | + assert isinstance(doe_cmd, dict) |
| 89 | + assert len(doe_cmd.keys()) == doe_count |
| 90 | + |
| 91 | + doe_status = doe_mod.simulate() |
| 92 | + assert doe_status is True |
| 93 | + |
| 94 | + |
| 95 | +def _check_runner_result(mod, doe_mod): |
| 96 | + doe_cmd = doe_mod.get_doe_command() |
| 97 | + doe_def = doe_mod.get_doe_definition() |
| 98 | + |
| 99 | + doe_sol = OMPython.doe_get_solutions( |
| 100 | + msomc=mod, |
| 101 | + resultpath=doe_mod.get_resultpath(), |
| 102 | + doe_def=doe_def, |
| 103 | + ) |
| 104 | + assert isinstance(doe_sol, dict) |
| 105 | + assert len(doe_sol.keys()) == len(doe_cmd.keys()) |
| 106 | + |
| 107 | + assert sorted(doe_def.keys()) == sorted(doe_cmd.keys()) |
| 108 | + assert sorted(doe_cmd.keys()) == sorted(doe_sol.keys()) |
| 109 | + |
| 110 | + for resultfilename in doe_def: |
| 111 | + row = doe_def[resultfilename] |
| 112 | + |
| 113 | + assert resultfilename in doe_sol |
| 114 | + sol = doe_sol[resultfilename] |
| 115 | + |
| 116 | + var_dict = { |
| 117 | + # simple / non-structural parameters |
| 118 | + 'a': float(row['a']), |
| 119 | + 'b': float(row['b']), |
| 120 | + } |
| 121 | + |
| 122 | + for var in var_dict: |
| 123 | + assert var in sol['data'] |
| 124 | + assert np.isclose(sol['data'][var][-1], var_dict[var]) |
0 commit comments