|
| 1 | +import sys |
| 2 | + |
1 | 3 | import numpy as np |
2 | 4 | import pytest |
3 | 5 |
|
4 | 6 | import OMPython |
5 | 7 |
|
6 | 8 |
|
| 9 | +skip_on_windows = pytest.mark.skipif( |
| 10 | + sys.platform.startswith("win"), |
| 11 | + reason="OpenModelica Docker image is Linux-only; skipping on Windows.", |
| 12 | +) |
| 13 | + |
| 14 | +skip_python_older_312 = pytest.mark.skipif( |
| 15 | + sys.version_info < (3, 12), |
| 16 | + reason="OMCPath(non-local) only working for Python >= 3.12.", |
| 17 | +) |
| 18 | + |
| 19 | + |
7 | 20 | @pytest.fixture |
8 | 21 | def model_firstorder_content(): |
9 | 22 | return """ |
@@ -37,7 +50,7 @@ def param(): |
37 | 50 | } |
38 | 51 |
|
39 | 52 |
|
40 | | -def test_runner(model_firstorder, param): |
| 53 | +def test_ModelicaSystemRunner_OMC(model_firstorder, param): |
41 | 54 | # create a model using ModelicaSystem |
42 | 55 | mod = OMPython.ModelicaSystemOMC() |
43 | 56 | mod.model( |
@@ -71,6 +84,165 @@ def test_runner(model_firstorder, param): |
71 | 84 | _check_result(mod=mod, resultfile=resultfile_modr, param=param) |
72 | 85 |
|
73 | 86 |
|
| 87 | +def test_ModelicaSystemRunner_local(model_firstorder, param): |
| 88 | + # create a model using ModelicaSystem |
| 89 | + mod = OMPython.ModelicaSystemOMC() |
| 90 | + mod.model( |
| 91 | + model_file=model_firstorder, |
| 92 | + model_name="M", |
| 93 | + ) |
| 94 | + |
| 95 | + resultfile_mod = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_mod.mat" |
| 96 | + _run_simulation(mod=mod, resultfile=resultfile_mod, param=param) |
| 97 | + |
| 98 | + # run the model using only the runner class |
| 99 | + omcs = OMPython.OMSessionRunner( |
| 100 | + version=mod.get_session().get_version(), |
| 101 | + ompath_runner=OMPython.OMPathRunnerLocal, |
| 102 | + ) |
| 103 | + modr = OMPython.ModelicaSystemRunner( |
| 104 | + session=omcs, |
| 105 | + work_directory=mod.getWorkDirectory(), |
| 106 | + ) |
| 107 | + modr.setup( |
| 108 | + model_name="M", |
| 109 | + ) |
| 110 | + |
| 111 | + resultfile_modr = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_modr.mat" |
| 112 | + _run_simulation(mod=modr, resultfile=resultfile_modr, param=param) |
| 113 | + |
| 114 | + # cannot check the content as runner does not have the capability to open a result file |
| 115 | + assert resultfile_mod.size() == resultfile_modr.size() |
| 116 | + |
| 117 | + # check results |
| 118 | + _check_result(mod=mod, resultfile=resultfile_mod, param=param) |
| 119 | + _check_result(mod=mod, resultfile=resultfile_modr, param=param) |
| 120 | + |
| 121 | + |
| 122 | +@skip_on_windows |
| 123 | +def test_ModelicaSystemRunner_bash(model_firstorder, param): |
| 124 | + # create a model using ModelicaSystem |
| 125 | + mod = OMPython.ModelicaSystemOMC() |
| 126 | + mod.model( |
| 127 | + model_file=model_firstorder, |
| 128 | + model_name="M", |
| 129 | + ) |
| 130 | + |
| 131 | + resultfile_mod = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_mod.mat" |
| 132 | + _run_simulation(mod=mod, resultfile=resultfile_mod, param=param) |
| 133 | + |
| 134 | + # run the model using only the runner class |
| 135 | + omcsr = OMPython.OMSessionRunner( |
| 136 | + version=mod.get_session().get_version(), |
| 137 | + ompath_runner=OMPython.OMPathRunnerBash, |
| 138 | + ) |
| 139 | + modr = OMPython.ModelicaSystemRunner( |
| 140 | + session=omcsr, |
| 141 | + work_directory=mod.getWorkDirectory(), |
| 142 | + ) |
| 143 | + modr.setup( |
| 144 | + model_name="M", |
| 145 | + ) |
| 146 | + |
| 147 | + resultfile_modr = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_modr.mat" |
| 148 | + _run_simulation(mod=modr, resultfile=resultfile_modr, param=param) |
| 149 | + |
| 150 | + # cannot check the content as runner does not have the capability to open a result file |
| 151 | + assert resultfile_mod.size() == resultfile_modr.size() |
| 152 | + |
| 153 | + # check results |
| 154 | + _check_result(mod=mod, resultfile=resultfile_mod, param=param) |
| 155 | + _check_result(mod=mod, resultfile=resultfile_modr, param=param) |
| 156 | + |
| 157 | + |
| 158 | +@skip_on_windows |
| 159 | +@skip_python_older_312 |
| 160 | +def test_ModelicaSystemRunner_bash_docker(model_firstorder, param): |
| 161 | + omcs = OMPython.OMCSessionDocker(docker="openmodelica/openmodelica:v1.25.0-minimal") |
| 162 | + assert omcs.get_version() == "OpenModelica 1.25.0" |
| 163 | + |
| 164 | + # create a model using ModelicaSystem |
| 165 | + mod = OMPython.ModelicaSystemOMC( |
| 166 | + session=omcs, |
| 167 | + ) |
| 168 | + mod.model( |
| 169 | + model_file=model_firstorder, |
| 170 | + model_name="M", |
| 171 | + ) |
| 172 | + |
| 173 | + resultfile_mod = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_mod.mat" |
| 174 | + _run_simulation(mod=mod, resultfile=resultfile_mod, param=param) |
| 175 | + |
| 176 | + # run the model using only the runner class |
| 177 | + omcsr = OMPython.OMSessionRunner( |
| 178 | + version=mod.get_session().get_version(), |
| 179 | + cmd_prefix=omcs.model_execution_prefix(cwd=mod.getWorkDirectory()), |
| 180 | + ompath_runner=OMPython.OMPathRunnerBash, |
| 181 | + model_execution_local=False, |
| 182 | + ) |
| 183 | + modr = OMPython.ModelicaSystemRunner( |
| 184 | + session=omcsr, |
| 185 | + work_directory=mod.getWorkDirectory(), |
| 186 | + ) |
| 187 | + modr.setup( |
| 188 | + model_name="M", |
| 189 | + ) |
| 190 | + |
| 191 | + resultfile_modr = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_modr.mat" |
| 192 | + _run_simulation(mod=modr, resultfile=resultfile_modr, param=param) |
| 193 | + |
| 194 | + # cannot check the content as runner does not have the capability to open a result file |
| 195 | + assert resultfile_mod.size() == resultfile_modr.size() |
| 196 | + |
| 197 | + # check results |
| 198 | + _check_result(mod=mod, resultfile=resultfile_mod, param=param) |
| 199 | + _check_result(mod=mod, resultfile=resultfile_modr, param=param) |
| 200 | + |
| 201 | + |
| 202 | +@pytest.mark.skip(reason="Not able to run WSL on github") |
| 203 | +@skip_python_older_312 |
| 204 | +def test_ModelicaSystemDoE_WSL(tmp_path, model_doe, param_doe): |
| 205 | + omcs = OMPython.OMCSessionWSL() |
| 206 | + assert omcs.get_version() == "OpenModelica 1.25.0" |
| 207 | + |
| 208 | + # create a model using ModelicaSystem |
| 209 | + mod = OMPython.ModelicaSystemOMC( |
| 210 | + session=omcs, |
| 211 | + ) |
| 212 | + mod.model( |
| 213 | + model_file=model_firstorder, |
| 214 | + model_name="M", |
| 215 | + ) |
| 216 | + |
| 217 | + resultfile_mod = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_mod.mat" |
| 218 | + _run_simulation(mod=mod, resultfile=resultfile_mod, param=param) |
| 219 | + |
| 220 | + # run the model using only the runner class |
| 221 | + omcsr = OMPython.OMSessionRunner( |
| 222 | + version=mod.get_session().get_version(), |
| 223 | + cmd_prefix=omcs.model_execution_prefix(cwd=mod.getWorkDirectory()), |
| 224 | + ompath_runner=OMPython.OMPathRunnerBash, |
| 225 | + model_execution_local=False, |
| 226 | + ) |
| 227 | + modr = OMPython.ModelicaSystemRunner( |
| 228 | + session=omcsr, |
| 229 | + work_directory=mod.getWorkDirectory(), |
| 230 | + ) |
| 231 | + modr.setup( |
| 232 | + model_name="M", |
| 233 | + ) |
| 234 | + |
| 235 | + resultfile_modr = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_modr.mat" |
| 236 | + _run_simulation(mod=modr, resultfile=resultfile_modr, param=param) |
| 237 | + |
| 238 | + # cannot check the content as runner does not have the capability to open a result file |
| 239 | + assert resultfile_mod.size() == resultfile_modr.size() |
| 240 | + |
| 241 | + # check results |
| 242 | + _check_result(mod=mod, resultfile=resultfile_mod, param=param) |
| 243 | + _check_result(mod=mod, resultfile=resultfile_modr, param=param) |
| 244 | + |
| 245 | + |
74 | 246 | def _run_simulation(mod, resultfile, param): |
75 | 247 | simOptions = {"stopTime": param['stopTime'], "stepSize": 0.1, "tolerance": 1e-8} |
76 | 248 | mod.setSimulationOptions(**simOptions) |
|
0 commit comments