Skip to content

Commit 9bb46ef

Browse files
committed
[ModelicaSystem] rename model_definition() => model()
1 parent 4fe2f3a commit 9bb46ef

7 files changed

Lines changed: 42 additions & 38 deletions

OMPython/ModelicaSystem.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -386,12 +386,12 @@ def __init__(
386386

387387
self._model_name: Optional[str] = None
388388
self._lmodel: Optional[list[str | tuple[str, str]]] = None
389-
self._file_name: Optional[OMCPath]
389+
self._file_name: Optional[OMCPath] = None
390390
self._variable_filter: Optional[str] = None
391391

392-
def model_definition(
392+
def model(
393393
self,
394-
model: str,
394+
name: str,
395395
file: Optional[str | os.PathLike] = None,
396396
libraries: Optional[list[str | tuple[str, str]]] = None,
397397
variable_filter: Optional[str] = None,
@@ -405,7 +405,7 @@ def model_definition(
405405
Args:
406406
file: Path to the model file. Either absolute or relative to
407407
the current working directory.
408-
model: The name of the model class. If it is contained within
408+
name: The name of the model class. If it is contained within
409409
a package, "PackageName.ModelName" should be used.
410410
libraries: List of libraries to be loaded before the model itself is
411411
loaded. Two formats are supported for the list elements:
@@ -427,8 +427,12 @@ def model_definition(
427427
mod.setup_model(model="modelName", file="ModelicaModel.mo", libraries=[("Modelica","3.2.3"), "PowerSystems"])
428428
"""
429429

430-
if not isinstance(model, str):
431-
raise ModelicaSystemError("A model name must be provided (argument modelName)!")
430+
if self._model_name is not None:
431+
raise ModelicaSystemError("Can not reuse this instance of ModelicaSystem "
432+
f"defined for {repr(self._model_name)}!")
433+
434+
if not isinstance(name, str):
435+
raise ModelicaSystemError("A model name must be provided!")
432436

433437
if libraries is None:
434438
libraries = []
@@ -437,8 +441,8 @@ def model_definition(
437441
raise ModelicaSystemError(f"Invalid input type for lmodel: {type(libraries)} - list expected!")
438442

439443
# set variables
440-
self._model_name = model # Model class name
441-
self._lmodel = libraries # may be needed if model is derived from other model
444+
self._model_name = name # Model class name
445+
self._libraries = libraries # may be needed if model is derived from other model
442446
if file is not None:
443447
file_name = self._getconn.omcpath(file).resolve()
444448
else:

tests/test_FMIExport.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
def test_CauerLowPassAnalog():
88
mod = OMPython.ModelicaSystem()
9-
mod.model_definition(
10-
model="Modelica.Electrical.Analog.Examples.CauerLowPassAnalog",
9+
mod.model(
10+
name="Modelica.Electrical.Analog.Examples.CauerLowPassAnalog",
1111
libraries=["Modelica"],
1212
)
1313
tmp = pathlib.Path(mod.getWorkDirectory())
@@ -20,8 +20,8 @@ def test_CauerLowPassAnalog():
2020

2121
def test_DrumBoiler():
2222
mod = OMPython.ModelicaSystem()
23-
mod.model_definition(
24-
model="Modelica.Fluid.Examples.DrumBoiler.DrumBoiler",
23+
mod.model(
24+
name="Modelica.Fluid.Examples.DrumBoiler.DrumBoiler",
2525
libraries=["Modelica"],
2626
)
2727
tmp = pathlib.Path(mod.getWorkDirectory())

tests/test_ModelicaSystem.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ def test_ModelicaSystem_loop(model_firstorder):
3939
def worker():
4040
filePath = model_firstorder.as_posix()
4141
mod = OMPython.ModelicaSystem()
42-
mod.model_definition(
42+
mod.model(
4343
file=filePath,
44-
model="M",
44+
name="M",
4545
)
4646
mod.simulate()
4747
mod.convertMo2Fmu(fmuType="me")
@@ -53,9 +53,9 @@ def test_setParameters():
5353
omc = OMPython.OMCSessionZMQ()
5454
model_path = omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels/"
5555
mod = OMPython.ModelicaSystem()
56-
mod.model_definition(
56+
mod.model(
5757
file=model_path + "BouncingBall.mo",
58-
model="BouncingBall",
58+
name="BouncingBall",
5959
)
6060

6161
# method 1 (test depreciated variants)
@@ -87,9 +87,9 @@ def test_setSimulationOptions():
8787
omc = OMPython.OMCSessionZMQ()
8888
model_path = omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels/"
8989
mod = OMPython.ModelicaSystem()
90-
mod.model_definition(
90+
mod.model(
9191
file=model_path + "BouncingBall.mo",
92-
model="BouncingBall",
92+
name="BouncingBall",
9393
)
9494

9595
# method 1
@@ -124,9 +124,9 @@ def test_relative_path(model_firstorder):
124124
assert "/" not in model_relative
125125

126126
mod = OMPython.ModelicaSystem()
127-
mod.model_definition(
127+
mod.model(
128128
file=model_relative,
129-
model="M",
129+
name="M",
130130
)
131131
assert float(mod.getParameters("a")[0]) == -1
132132
finally:
@@ -138,9 +138,9 @@ def test_customBuildDirectory(tmp_path, model_firstorder):
138138
tmpdir = tmp_path / "tmpdir1"
139139
tmpdir.mkdir()
140140
mod = OMPython.ModelicaSystem(customBuildDirectory=tmpdir)
141-
mod.model_definition(
141+
mod.model(
142142
file=filePath,
143-
model="M",
143+
name="M",
144144
)
145145
assert pathlib.Path(mod.getWorkDirectory()).resolve() == tmpdir.resolve()
146146
result_file = tmpdir / "a.mat"
@@ -171,9 +171,9 @@ def test_getSolutions_docker(model_firstorder_content):
171171
def test_getSolutions(model_firstorder):
172172
filePath = model_firstorder.as_posix()
173173
mod = OMPython.ModelicaSystem()
174-
mod.model_definition(
174+
mod.model(
175175
file=filePath,
176-
model="M",
176+
name="M",
177177
)
178178

179179
_run_getSolutions(mod)
@@ -219,9 +219,9 @@ def test_getters(tmp_path):
219219
end M_getters;
220220
""")
221221
mod = OMPython.ModelicaSystem()
222-
mod.model_definition(
222+
mod.model(
223223
file=model_file.as_posix(),
224-
model="M_getters",
224+
name="M_getters",
225225
)
226226

227227
q = mod.getQuantities()
@@ -415,9 +415,9 @@ def test_simulate_inputs(tmp_path):
415415
end M_input;
416416
""")
417417
mod = OMPython.ModelicaSystem()
418-
mod.model_definition(
418+
mod.model(
419419
file=model_file.as_posix(),
420-
model="M_input",
420+
name="M_input",
421421
)
422422

423423
simOptions = {"stopTime": 1.0}

tests/test_ModelicaSystemCmd.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ def model_firstorder(tmp_path):
1818
@pytest.fixture
1919
def mscmd_firstorder(model_firstorder):
2020
mod = OMPython.ModelicaSystem()
21-
mod.model_definition(
21+
mod.model(
2222
file=model_firstorder.as_posix(),
23-
model="M",
23+
name="M",
2424
)
2525
mscmd = OMPython.ModelicaSystemCmd(
2626
session=mod._getconn,

tests/test_OMSessionCmd.py

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

1010
def test_isPackage2():
1111
mod = OMPython.ModelicaSystem()
12-
mod.model_definition(
13-
model="Modelica.Electrical.Analog.Examples.CauerLowPassAnalog",
12+
mod.model(
13+
name="Modelica.Electrical.Analog.Examples.CauerLowPassAnalog",
1414
libraries=["Modelica"],
1515
)
1616
omccmd = OMPython.OMCSessionCmd(session=mod._getconn)

tests/test_linearization.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ def model_linearTest(tmp_path):
2525

2626
def test_example(model_linearTest):
2727
mod = OMPython.ModelicaSystem()
28-
mod.model_definition(
28+
mod.model(
2929
file=model_linearTest,
30-
model="linearTest",
30+
name="linearTest",
3131
)
3232
[A, B, C, D] = mod.linearize()
3333
expected_matrixA = [[-3, 2, 0, 0], [-7, 0, -5, 1], [-1, 0, -1, 4], [0, 1, -1, 5]]
@@ -60,9 +60,9 @@ def test_getters(tmp_path):
6060
end Pendulum;
6161
""")
6262
mod = OMPython.ModelicaSystem()
63-
mod.model_definition(
63+
mod.model(
6464
file=model_file.as_posix(),
65-
model="Pendulum",
65+
name="Pendulum",
6666
libraries=["Modelica"],
6767
)
6868

tests/test_optimization.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ def test_optimization_example(tmp_path):
3434
""")
3535

3636
mod = OMPython.ModelicaSystem()
37-
mod.model_definition(
37+
mod.model(
3838
file=model_file.as_posix(),
39-
model="BangBang2021",
39+
name="BangBang2021",
4040
)
4141

4242
optimizationOptions = {

0 commit comments

Comments
 (0)