Skip to content

Commit 8cc8f50

Browse files
committed
[ModelicaSystem] split __init__() - update unittest
1 parent aabfa14 commit 8cc8f50

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
@@ -4,8 +4,11 @@
44

55

66
def test_CauerLowPassAnalog():
7-
mod = OMPython.ModelicaSystem(modelName="Modelica.Electrical.Analog.Examples.CauerLowPassAnalog",
8-
lmodel=["Modelica"])
7+
mod = OMPython.ModelicaSystem()
8+
mod.model_definition(
9+
model="Modelica.Electrical.Analog.Examples.CauerLowPassAnalog",
10+
libraries=["Modelica"],
11+
)
912
tmp = mod.getWorkDirectory()
1013
try:
1114
fmu = mod.convertMo2Fmu(fileNamePrefix="CauerLowPassAnalog")
@@ -15,7 +18,11 @@ def test_CauerLowPassAnalog():
1518

1619

1720
def test_DrumBoiler():
18-
mod = OMPython.ModelicaSystem(modelName="Modelica.Fluid.Examples.DrumBoiler.DrumBoiler", lmodel=["Modelica"])
21+
mod = OMPython.ModelicaSystem()
22+
mod.model_definition(
23+
model="Modelica.Fluid.Examples.DrumBoiler.DrumBoiler",
24+
libraries=["Modelica"],
25+
)
1926
tmp = mod.getWorkDirectory()
2027
try:
2128
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
3846
mod.setParameters(pvals={"e": 1.234})
@@ -61,7 +69,11 @@ def test_setParameters():
6169
def test_setSimulationOptions():
6270
omc = OMPython.OMCSessionZMQ()
6371
model_path = omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels/"
64-
mod = OMPython.ModelicaSystem(fileName=model_path + "BouncingBall.mo", modelName="BouncingBall")
72+
mod = OMPython.ModelicaSystem()
73+
mod.model_definition(
74+
file=model_path + "BouncingBall.mo",
75+
model="BouncingBall",
76+
)
6577

6678
# method 1
6779
mod.setSimulationOptions(simOptions={"stopTime": 1.234})
@@ -94,7 +106,11 @@ def test_relative_path(model_firstorder):
94106
model_relative = str(model_file)
95107
assert "/" not in model_relative
96108

97-
mod = OMPython.ModelicaSystem(fileName=model_relative, modelName="M")
109+
mod = OMPython.ModelicaSystem()
110+
mod.model_definition(
111+
file=model_relative,
112+
model="M",
113+
)
98114
assert float(mod.getParameters("a")[0]) == -1
99115
finally:
100116
model_file.unlink() # clean up the temporary file
@@ -104,17 +120,25 @@ def test_customBuildDirectory(tmp_path, model_firstorder):
104120
filePath = model_firstorder.as_posix()
105121
tmpdir = tmp_path / "tmpdir1"
106122
tmpdir.mkdir()
107-
m = OMPython.ModelicaSystem(filePath, "M", customBuildDirectory=tmpdir)
108-
assert m.getWorkDirectory().resolve() == tmpdir.resolve()
123+
mod = OMPython.ModelicaSystem(customBuildDirectory=tmpdir)
124+
mod.model_definition(
125+
file=filePath,
126+
model="M",
127+
)
128+
assert mod.getWorkDirectory().resolve() == tmpdir.resolve()
109129
result_file = tmpdir / "a.mat"
110130
assert not result_file.exists()
111-
m.simulate(resultfile="a.mat")
131+
mod.simulate(resultfile="a.mat")
112132
assert result_file.is_file()
113133

114134

115135
def test_getSolutions(model_firstorder):
116136
filePath = model_firstorder.as_posix()
117-
mod = OMPython.ModelicaSystem(filePath, "M")
137+
mod = OMPython.ModelicaSystem()
138+
mod.model_definition(
139+
file=filePath,
140+
model="M",
141+
)
118142
x0 = 1
119143
a = -1
120144
tau = -1 / a
@@ -151,7 +175,11 @@ def test_getters(tmp_path):
151175
y = der(x);
152176
end M_getters;
153177
""")
154-
mod = OMPython.ModelicaSystem(fileName=model_file.as_posix(), modelName="M_getters")
178+
mod = OMPython.ModelicaSystem()
179+
mod.model_definition(
180+
file=model_file.as_posix(),
181+
model="M_getters",
182+
)
155183

156184
q = mod.getQuantities()
157185
assert isinstance(q, list)
@@ -343,7 +371,11 @@ def test_simulate_inputs(tmp_path):
343371
y = x;
344372
end M_input;
345373
""")
346-
mod = OMPython.ModelicaSystem(fileName=model_file.as_posix(), modelName="M_input")
374+
mod = OMPython.ModelicaSystem()
375+
mod.model_definition(
376+
file=model_file.as_posix(),
377+
model="M_input",
378+
)
347379

348380
mod.setSimulationOptions(simOptions={"stopTime": 1.0})
349381

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
mod.setOptimizationOptions(optimizationOptions={"numberOfIntervals": 16,
3943
"stopTime": 1,

0 commit comments

Comments
 (0)