|
| 1 | +import numpy as np |
| 2 | +import os |
| 3 | +import pytest |
| 4 | +import shutil |
| 5 | + |
| 6 | +import OMPython |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def model_firstorder(tmp_path): |
| 11 | + mod = tmp_path / "M.mo" |
| 12 | + mod.write_text("""model M |
| 13 | + Real x(start = 1, fixed = true); |
| 14 | + parameter Real a = -1; |
| 15 | +equation |
| 16 | + der(x) = x*a; |
| 17 | +end M; |
| 18 | +""") |
| 19 | + return mod |
| 20 | + |
| 21 | + |
| 22 | +def test_FMIImport(model_firstorder): |
| 23 | + filePath = model_firstorder.as_posix() |
| 24 | + |
| 25 | + # create model & simulate it |
| 26 | + mod1 = OMPython.ModelicaSystem() |
| 27 | + mod1.model(file=filePath, name="M") |
| 28 | + mod1.simulate() |
| 29 | + |
| 30 | + # create FMU & check |
| 31 | + fmu = mod1.convertMo2Fmu(fileNamePrefix="M") |
| 32 | + assert os.path.exists(fmu) |
| 33 | + |
| 34 | + # import FMU & check & simulate |
| 35 | + # TODO: why is '--allowNonStandardModelica=reinitInAlgorithms' needed? any example without this possible? |
| 36 | + mod2 = OMPython.ModelicaSystem(commandLineOptions=['--allowNonStandardModelica=reinitInAlgorithms']) |
| 37 | + mo = mod2.convertFmu2Mo(fmu=fmu) |
| 38 | + assert os.path.exists(mo) |
| 39 | + |
| 40 | + mod2.simulate() |
| 41 | + |
| 42 | + # get and verify result |
| 43 | + res1 = mod1.getSolutions(['time', 'x']) |
| 44 | + res2 = mod2.getSolutions(['time', 'x']) |
| 45 | + |
| 46 | + # check last value for time |
| 47 | + assert res1[0][-1] == res2[0][-1] == 1.0 |
| 48 | + # check last value for x |
| 49 | + assert np.isclose(res1[1][-1], 0.3678794515) # 0.36787945153397683 |
| 50 | + assert np.isclose(res2[1][-1], 0.3678794515) # 0.3678794515707647 |
| 51 | + |
| 52 | + # cleanup |
| 53 | + tmp2 = mod1.getWorkDirectory() |
| 54 | + shutil.rmtree(tmp2, ignore_errors=True) |
| 55 | + |
| 56 | + tmp2 = mod2.getWorkDirectory() |
| 57 | + shutil.rmtree(tmp2, ignore_errors=True) |
0 commit comments