-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathtest_FMIImport.py
More file actions
59 lines (46 loc) · 1.47 KB
/
test_FMIImport.py
File metadata and controls
59 lines (46 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import os
import shutil
import numpy as np
import pytest
import OMPython
@pytest.fixture
def model_firstorder(tmp_path):
mod = tmp_path / "M.mo"
mod.write_text("""model M
Real x(start = 1, fixed = true);
parameter Real a = -1;
equation
der(x) = x*a;
end M;
""")
return mod
def test_FMIImport(model_firstorder):
# create model & simulate it
mod1 = OMPython.ModelicaSystemOMC()
mod1.model(
model_file=model_firstorder,
model_name="M",
)
mod1.simulate()
# create FMU & check
fmu = mod1.convertMo2Fmu(fileNamePrefix="M")
assert os.path.exists(fmu)
# import FMU & check & simulate
# TODO: why is '--allowNonStandardModelica=reinitInAlgorithms' needed? any example without this possible?
mod2 = OMPython.ModelicaSystemOMC(command_line_options=['--allowNonStandardModelica=reinitInAlgorithms'])
mo = mod2.convertFmu2Mo(fmu=fmu)
assert os.path.exists(mo)
mod2.simulate()
# get and verify result
res1 = mod1.getSolutions(['time', 'x'])
res2 = mod2.getSolutions(['time', 'x'])
# check last value for time
assert res1[0][-1] == res2[0][-1] == 1.0
# check last value for x
assert np.isclose(res1[1][-1], 0.3678794515) # 0.36787945153397683
assert np.isclose(res2[1][-1], 0.3678794515) # 0.3678794515707647
# cleanup
tmp2 = mod1.getWorkDirectory()
shutil.rmtree(tmp2, ignore_errors=True)
tmp2 = mod2.getWorkDirectory()
shutil.rmtree(tmp2, ignore_errors=True)