Skip to content

Commit 4fe2f3a

Browse files
committed
[ModelicaSystem] split __init__() - update unittest
1 parent 4c36c02 commit 4fe2f3a

6 files changed

Lines changed: 80 additions & 21 deletions

File tree

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
@@ -38,17 +38,25 @@ def model_firstorder(tmp_path, model_firstorder_content):
3838
def test_ModelicaSystem_loop(model_firstorder):
3939
def worker():
4040
filePath = model_firstorder.as_posix()
41-
m = OMPython.ModelicaSystem(filePath, "M")
42-
m.simulate()
43-
m.convertMo2Fmu(fmuType="me")
41+
mod = OMPython.ModelicaSystem()
42+
mod.model_definition(
43+
file=filePath,
44+
model="M",
45+
)
46+
mod.simulate()
47+
mod.convertMo2Fmu(fmuType="me")
4448
for _ in range(10):
4549
worker()
4650

4751

4852
def test_setParameters():
4953
omc = OMPython.OMCSessionZMQ()
5054
model_path = omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels/"
51-
mod = OMPython.ModelicaSystem(model_path + "BouncingBall.mo", "BouncingBall")
55+
mod = OMPython.ModelicaSystem()
56+
mod.model_definition(
57+
file=model_path + "BouncingBall.mo",
58+
model="BouncingBall",
59+
)
5260

5361
# method 1 (test depreciated variants)
5462
mod.setParameters("e=1.234")
@@ -78,7 +86,11 @@ def test_setParameters():
7886
def test_setSimulationOptions():
7987
omc = OMPython.OMCSessionZMQ()
8088
model_path = omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels/"
81-
mod = OMPython.ModelicaSystem(fileName=model_path + "BouncingBall.mo", modelName="BouncingBall")
89+
mod = OMPython.ModelicaSystem()
90+
mod.model_definition(
91+
file=model_path + "BouncingBall.mo",
92+
model="BouncingBall",
93+
)
8294

8395
# method 1
8496
mod.setSimulationOptions(stopTime=1.234)
@@ -111,7 +123,11 @@ def test_relative_path(model_firstorder):
111123
model_relative = str(model_file)
112124
assert "/" not in model_relative
113125

114-
mod = OMPython.ModelicaSystem(fileName=model_relative, modelName="M")
126+
mod = OMPython.ModelicaSystem()
127+
mod.model_definition(
128+
file=model_relative,
129+
model="M",
130+
)
115131
assert float(mod.getParameters("a")[0]) == -1
116132
finally:
117133
model_file.unlink() # clean up the temporary file
@@ -121,11 +137,15 @@ def test_customBuildDirectory(tmp_path, model_firstorder):
121137
filePath = model_firstorder.as_posix()
122138
tmpdir = tmp_path / "tmpdir1"
123139
tmpdir.mkdir()
124-
m = OMPython.ModelicaSystem(filePath, "M", customBuildDirectory=tmpdir)
125-
assert pathlib.Path(m.getWorkDirectory()).resolve() == tmpdir.resolve()
140+
mod = OMPython.ModelicaSystem(customBuildDirectory=tmpdir)
141+
mod.model_definition(
142+
file=filePath,
143+
model="M",
144+
)
145+
assert pathlib.Path(mod.getWorkDirectory()).resolve() == tmpdir.resolve()
126146
result_file = tmpdir / "a.mat"
127147
assert not result_file.exists()
128-
m.simulate(resultfile="a.mat")
148+
mod.simulate(resultfile="a.mat")
129149
assert result_file.is_file()
130150

131151

@@ -150,7 +170,11 @@ def test_getSolutions_docker(model_firstorder_content):
150170

151171
def test_getSolutions(model_firstorder):
152172
filePath = model_firstorder.as_posix()
153-
mod = OMPython.ModelicaSystem(filePath, "M")
173+
mod = OMPython.ModelicaSystem()
174+
mod.model_definition(
175+
file=filePath,
176+
model="M",
177+
)
154178

155179
_run_getSolutions(mod)
156180

@@ -194,7 +218,11 @@ def test_getters(tmp_path):
194218
y = der(x);
195219
end M_getters;
196220
""")
197-
mod = OMPython.ModelicaSystem(fileName=model_file.as_posix(), modelName="M_getters")
221+
mod = OMPython.ModelicaSystem()
222+
mod.model_definition(
223+
file=model_file.as_posix(),
224+
model="M_getters",
225+
)
198226

199227
q = mod.getQuantities()
200228
assert isinstance(q, list)
@@ -386,7 +414,11 @@ def test_simulate_inputs(tmp_path):
386414
y = x;
387415
end M_input;
388416
""")
389-
mod = OMPython.ModelicaSystem(fileName=model_file.as_posix(), modelName="M_input")
417+
mod = OMPython.ModelicaSystem()
418+
mod.model_definition(
419+
file=model_file.as_posix(),
420+
model="M_input",
421+
)
390422

391423
simOptions = {"stopTime": 1.0}
392424
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(
2226
session=mod._getconn,
2327
runpath=mod.getWorkDirectory(),

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)