Skip to content

Commit 05dce1f

Browse files
committed
[ModelicaSystem] split __init__() - update unittest
1 parent 3bb7614 commit 05dce1f

File tree

6 files changed

+80
-21
lines changed

6 files changed

+80
-21
lines changed

tests/test_FMIExport.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55

66

77
def test_CauerLowPassAnalog():
8-
mod = OMPython.ModelicaSystem(modelName="Modelica.Electrical.Analog.Examples.CauerLowPassAnalog",
9-
lmodel=["Modelica"])
8+
mod = OMPython.ModelicaSystem()
9+
mod.model_definition(
10+
model="Modelica.Electrical.Analog.Examples.CauerLowPassAnalog",
11+
libraries=["Modelica"],
12+
)
1013
tmp = pathlib.Path(mod.getWorkDirectory())
1114
try:
1215
fmu = mod.convertMo2Fmu(fileNamePrefix="CauerLowPassAnalog")
@@ -16,7 +19,11 @@ def test_CauerLowPassAnalog():
1619

1720

1821
def test_DrumBoiler():
19-
mod = OMPython.ModelicaSystem(modelName="Modelica.Fluid.Examples.DrumBoiler.DrumBoiler", lmodel=["Modelica"])
22+
mod = OMPython.ModelicaSystem()
23+
mod.model_definition(
24+
model="Modelica.Fluid.Examples.DrumBoiler.DrumBoiler",
25+
libraries=["Modelica"],
26+
)
2027
tmp = pathlib.Path(mod.getWorkDirectory())
2128
try:
2229
fmu = mod.convertMo2Fmu(fileNamePrefix="DrumBoiler")

tests/test_ModelicaSystem.py

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,25 @@ def model_firstorder(tmp_path):
2222
def test_ModelicaSystem_loop(model_firstorder):
2323
def worker():
2424
filePath = model_firstorder.as_posix()
25-
m = OMPython.ModelicaSystem(filePath, "M")
26-
m.simulate()
27-
m.convertMo2Fmu(fmuType="me")
25+
mod = OMPython.ModelicaSystem()
26+
mod.model_definition(
27+
file=filePath,
28+
model="M",
29+
)
30+
mod.simulate()
31+
mod.convertMo2Fmu(fmuType="me")
2832
for _ in range(10):
2933
worker()
3034

3135

3236
def test_setParameters():
3337
omc = OMPython.OMCSessionZMQ()
3438
model_path = omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels/"
35-
mod = OMPython.ModelicaSystem(model_path + "BouncingBall.mo", "BouncingBall")
39+
mod = OMPython.ModelicaSystem()
40+
mod.model_definition(
41+
file=model_path + "BouncingBall.mo",
42+
model="BouncingBall",
43+
)
3644

3745
# method 1 (test depreciated variants)
3846
mod.setParameters("e=1.234")
@@ -62,7 +70,11 @@ def test_setParameters():
6270
def test_setSimulationOptions():
6371
omc = OMPython.OMCSessionZMQ()
6472
model_path = omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels/"
65-
mod = OMPython.ModelicaSystem(fileName=model_path + "BouncingBall.mo", modelName="BouncingBall")
73+
mod = OMPython.ModelicaSystem()
74+
mod.model_definition(
75+
file=model_path + "BouncingBall.mo",
76+
model="BouncingBall",
77+
)
6678

6779
# method 1
6880
mod.setSimulationOptions(stopTime=1.234)
@@ -95,7 +107,11 @@ def test_relative_path(model_firstorder):
95107
model_relative = str(model_file)
96108
assert "/" not in model_relative
97109

98-
mod = OMPython.ModelicaSystem(fileName=model_relative, modelName="M")
110+
mod = OMPython.ModelicaSystem()
111+
mod.model_definition(
112+
file=model_relative,
113+
model="M",
114+
)
99115
assert float(mod.getParameters("a")[0]) == -1
100116
finally:
101117
model_file.unlink() # clean up the temporary file
@@ -105,17 +121,25 @@ def test_customBuildDirectory(tmp_path, model_firstorder):
105121
filePath = model_firstorder.as_posix()
106122
tmpdir = tmp_path / "tmpdir1"
107123
tmpdir.mkdir()
108-
m = OMPython.ModelicaSystem(filePath, "M", customBuildDirectory=tmpdir)
109-
assert pathlib.Path(m.getWorkDirectory()).resolve() == tmpdir.resolve()
124+
mod = OMPython.ModelicaSystem(customBuildDirectory=tmpdir)
125+
mod.model_definition(
126+
file=filePath,
127+
model="M",
128+
)
129+
assert pathlib.Path(mod.getWorkDirectory()).resolve() == tmpdir.resolve()
110130
result_file = tmpdir / "a.mat"
111131
assert not result_file.exists()
112-
m.simulate(resultfile="a.mat")
132+
mod.simulate(resultfile="a.mat")
113133
assert result_file.is_file()
114134

115135

116136
def test_getSolutions(model_firstorder):
117137
filePath = model_firstorder.as_posix()
118-
mod = OMPython.ModelicaSystem(filePath, "M")
138+
mod = OMPython.ModelicaSystem()
139+
mod.model_definition(
140+
file=filePath,
141+
model="M",
142+
)
119143
x0 = 1
120144
a = -1
121145
tau = -1 / a
@@ -154,7 +178,11 @@ def test_getters(tmp_path):
154178
y = der(x);
155179
end M_getters;
156180
""")
157-
mod = OMPython.ModelicaSystem(fileName=model_file.as_posix(), modelName="M_getters")
181+
mod = OMPython.ModelicaSystem()
182+
mod.model_definition(
183+
file=model_file.as_posix(),
184+
model="M_getters",
185+
)
158186

159187
q = mod.getQuantities()
160188
assert isinstance(q, list)
@@ -346,7 +374,11 @@ def test_simulate_inputs(tmp_path):
346374
y = x;
347375
end M_input;
348376
""")
349-
mod = OMPython.ModelicaSystem(fileName=model_file.as_posix(), modelName="M_input")
377+
mod = OMPython.ModelicaSystem()
378+
mod.model_definition(
379+
file=model_file.as_posix(),
380+
model="M_input",
381+
)
350382

351383
simOptions = {"stopTime": 1.0}
352384
mod.setSimulationOptions(**simOptions)

tests/test_ModelicaSystemCmd.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ def model_firstorder(tmp_path):
1717

1818
@pytest.fixture
1919
def mscmd_firstorder(model_firstorder):
20-
mod = OMPython.ModelicaSystem(fileName=model_firstorder.as_posix(), modelName="M")
20+
mod = OMPython.ModelicaSystem()
21+
mod.model_definition(
22+
file=model_firstorder.as_posix(),
23+
model="M",
24+
)
2125
mscmd = OMPython.ModelicaSystemCmd(runpath=mod.getWorkDirectory(), modelname=mod._model_name)
2226
return mscmd
2327

tests/test_OMSessionCmd.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ def test_isPackage():
88

99

1010
def test_isPackage2():
11-
mod = OMPython.ModelicaSystem(modelName="Modelica.Electrical.Analog.Examples.CauerLowPassAnalog",
12-
lmodel=["Modelica"])
11+
mod = OMPython.ModelicaSystem()
12+
mod.model_definition(
13+
model="Modelica.Electrical.Analog.Examples.CauerLowPassAnalog",
14+
libraries=["Modelica"],
15+
)
1316
omccmd = OMPython.OMCSessionCmd(session=mod._getconn)
1417
assert omccmd.isPackage('Modelica')
1518

tests/test_linearization.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ def model_linearTest(tmp_path):
2424

2525

2626
def test_example(model_linearTest):
27-
mod = OMPython.ModelicaSystem(model_linearTest, "linearTest")
27+
mod = OMPython.ModelicaSystem()
28+
mod.model_definition(
29+
file=model_linearTest,
30+
model="linearTest",
31+
)
2832
[A, B, C, D] = mod.linearize()
2933
expected_matrixA = [[-3, 2, 0, 0], [-7, 0, -5, 1], [-1, 0, -1, 4], [0, 1, -1, 5]]
3034
assert A == expected_matrixA, f"Matrix does not match the expected value. Got: {A}, Expected: {expected_matrixA}"
@@ -55,7 +59,12 @@ def test_getters(tmp_path):
5559
y2 = phi + u1;
5660
end Pendulum;
5761
""")
58-
mod = OMPython.ModelicaSystem(fileName=model_file.as_posix(), modelName="Pendulum", lmodel=["Modelica"])
62+
mod = OMPython.ModelicaSystem()
63+
mod.model_definition(
64+
file=model_file.as_posix(),
65+
model="Pendulum",
66+
libraries=["Modelica"],
67+
)
5968

6069
d = mod.getLinearizationOptions()
6170
assert isinstance(d, dict)

tests/test_optimization.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ def test_optimization_example(tmp_path):
3333
end BangBang2021;
3434
""")
3535

36-
mod = OMPython.ModelicaSystem(fileName=model_file.as_posix(), modelName="BangBang2021")
36+
mod = OMPython.ModelicaSystem()
37+
mod.model_definition(
38+
file=model_file.as_posix(),
39+
model="BangBang2021",
40+
)
3741

3842
optimizationOptions = {
3943
"numberOfIntervals": 16,

0 commit comments

Comments
 (0)