|
| 1 | +import numpy as np |
| 2 | +import OMPython |
| 3 | +import pathlib |
| 4 | +import pytest |
| 5 | +import sys |
| 6 | + |
| 7 | +skip_on_windows = pytest.mark.skipif( |
| 8 | + sys.platform.startswith("win"), |
| 9 | + reason="OpenModelica Docker image is Linux-only; skipping on Windows.", |
| 10 | +) |
| 11 | + |
| 12 | +skip_python_older_312 = pytest.mark.skipif( |
| 13 | + sys.version_info < (3, 12), |
| 14 | + reason="OMCPath(non-local) only working for Python >= 3.12.", |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def model_doe(tmp_path: pathlib.Path) -> pathlib.Path: |
| 20 | + # see: https://trac.openmodelica.org/OpenModelica/ticket/4052 |
| 21 | + mod = tmp_path / "M.mo" |
| 22 | + # TODO: update for bool and string parameters; check if these can be used in DoE |
| 23 | + mod.write_text(""" |
| 24 | +model M |
| 25 | + parameter Integer p=1; |
| 26 | + parameter Integer q=1; |
| 27 | + parameter Real a = -1; |
| 28 | + parameter Real b = -1; |
| 29 | + Real x[p]; |
| 30 | + Real y[q]; |
| 31 | +equation |
| 32 | + der(x) = a * fill(1.0, p); |
| 33 | + der(y) = b * fill(1.0, q); |
| 34 | +end M; |
| 35 | +""") |
| 36 | + return mod |
| 37 | + |
| 38 | + |
| 39 | +@pytest.fixture |
| 40 | +def param_doe() -> dict[str, list]: |
| 41 | + param = { |
| 42 | + # structural |
| 43 | + 'p': [1, 2], |
| 44 | + 'q': [3, 4], |
| 45 | + # simple |
| 46 | + 'a': [5, 6], |
| 47 | + 'b': [7, 8], |
| 48 | + } |
| 49 | + return param |
| 50 | + |
| 51 | + |
| 52 | +def test_ModelicaSystemDoE_local(tmp_path, model_doe, param_doe): |
| 53 | + tmpdir = tmp_path / 'DoE' |
| 54 | + tmpdir.mkdir(exist_ok=True) |
| 55 | + |
| 56 | + doe_mod = OMPython.ModelicaSystemDoE( |
| 57 | + fileName=model_doe.as_posix(), |
| 58 | + modelName="M", |
| 59 | + parameters=param_doe, |
| 60 | + resultpath=tmpdir, |
| 61 | + simargs={"override": {'stopTime': 1.0}}, |
| 62 | + ) |
| 63 | + |
| 64 | + _run_ModelicaSystemDoe(doe_mod=doe_mod) |
| 65 | + |
| 66 | + |
| 67 | +@skip_on_windows |
| 68 | +@skip_python_older_312 |
| 69 | +def test_ModelicaSystemDoE_docker(tmp_path, model_doe, param_doe): |
| 70 | + omcp = OMPython.OMCProcessDocker(docker="openmodelica/openmodelica:v1.25.0-minimal") |
| 71 | + omc = OMPython.OMCSessionZMQ(omc_process=omcp) |
| 72 | + assert omc.sendExpression("getVersion()") == "OpenModelica 1.25.0" |
| 73 | + |
| 74 | + modelpath = omc.omcpath_tempdir() / 'M.mo' |
| 75 | + modelpath.write_text(model_doe.read_text()) |
| 76 | + |
| 77 | + doe_mod = OMPython.ModelicaSystemDoE( |
| 78 | + fileName=modelpath.as_posix(), |
| 79 | + modelName="M", |
| 80 | + parameters=param_doe, |
| 81 | + omc_process=omcp, |
| 82 | + resultpath=modelpath.parent, |
| 83 | + simargs={"override": {'stopTime': 1.0}}, |
| 84 | + ) |
| 85 | + |
| 86 | + _run_ModelicaSystemDoe(doe_mod=doe_mod) |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.skip(reason="Not able to run WSL on github") |
| 90 | +@skip_python_older_312 |
| 91 | +def test_ModelicaSystemDoE_WSL(tmp_path, model_doe, param_doe): |
| 92 | + tmpdir = tmp_path / 'DoE' |
| 93 | + tmpdir.mkdir(exist_ok=True) |
| 94 | + |
| 95 | + doe_mod = OMPython.ModelicaSystemDoE( |
| 96 | + fileName=model_doe.as_posix(), |
| 97 | + modelName="M", |
| 98 | + parameters=param_doe, |
| 99 | + resultpath=tmpdir, |
| 100 | + simargs={"override": {'stopTime': 1.0}}, |
| 101 | + ) |
| 102 | + |
| 103 | + _run_ModelicaSystemDoe(doe_mod=doe_mod) |
| 104 | + |
| 105 | + |
| 106 | +def _run_ModelicaSystemDoe(doe_mod): |
| 107 | + doe_count = doe_mod.prepare() |
| 108 | + assert doe_count == 16 |
| 109 | + |
| 110 | + doe_def = doe_mod.get_doe_definition() |
| 111 | + assert isinstance(doe_def, dict) |
| 112 | + assert len(doe_def.keys()) == doe_count |
| 113 | + |
| 114 | + doe_cmd = doe_mod.get_doe_command() |
| 115 | + assert isinstance(doe_cmd, dict) |
| 116 | + assert len(doe_cmd.keys()) == doe_count |
| 117 | + |
| 118 | + doe_status = doe_mod.simulate() |
| 119 | + assert doe_status is True |
| 120 | + |
| 121 | + doe_sol = doe_mod.get_doe_solutions() |
| 122 | + assert isinstance(doe_sol, dict) |
| 123 | + assert len(doe_sol.keys()) == doe_count |
| 124 | + |
| 125 | + assert sorted(doe_def.keys()) == sorted(doe_cmd.keys()) |
| 126 | + assert sorted(doe_cmd.keys()) == sorted(doe_sol.keys()) |
| 127 | + |
| 128 | + for resultfilename in doe_def: |
| 129 | + row = doe_def[resultfilename] |
| 130 | + |
| 131 | + assert resultfilename in doe_sol |
| 132 | + sol = doe_sol[resultfilename] |
| 133 | + |
| 134 | + var_dict = { |
| 135 | + # simple / non-structural parameters |
| 136 | + 'a': float(row['a']), |
| 137 | + 'b': float(row['b']), |
| 138 | + # structural parameters |
| 139 | + 'p': float(row['p']), |
| 140 | + 'q': float(row['q']), |
| 141 | + # variables using the structural parameters |
| 142 | + f"x[{row['p']}]": float(row['a']), |
| 143 | + f"y[{row['p']}]": float(row['b']), |
| 144 | + } |
| 145 | + |
| 146 | + for var in var_dict: |
| 147 | + assert var in sol['data'] |
| 148 | + assert np.isclose(sol['data'][var][-1], var_dict[var]) |
0 commit comments