Skip to content

Commit e505584

Browse files
committed
[ModelicaSystem] rename model_definition() => model()
1 parent 05dce1f commit e505584

File tree

7 files changed

+42
-38
lines changed

7 files changed

+42
-38
lines changed

OMPython/ModelicaSystem.py

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

420420
self._model_name: Optional[str] = None
421421
self._lmodel: Optional[list[str | tuple[str, str]]] = None
422-
self._file_name: Optional[OMCPath]
422+
self._file_name: Optional[OMCPath] = None
423423
self._variable_filter: Optional[str] = None
424424

425-
def model_definition(
425+
def model(
426426
self,
427-
model: str,
427+
name: str,
428428
file: Optional[str | os.PathLike] = None,
429429
libraries: Optional[list[str | tuple[str, str]]] = None,
430430
variable_filter: Optional[str] = None,
@@ -438,7 +438,7 @@ def model_definition(
438438
Args:
439439
file: Path to the model file. Either absolute or relative to
440440
the current working directory.
441-
model: The name of the model class. If it is contained within
441+
name: The name of the model class. If it is contained within
442442
a package, "PackageName.ModelName" should be used.
443443
libraries: List of libraries to be loaded before the model itself is
444444
loaded. Two formats are supported for the list elements:
@@ -460,8 +460,12 @@ def model_definition(
460460
mod.setup_model(model="modelName", file="ModelicaModel.mo", libraries=[("Modelica","3.2.3"), "PowerSystems"])
461461
"""
462462

463-
if not isinstance(model, str):
464-
raise ModelicaSystemError("A model name must be provided (argument modelName)!")
463+
if self._model_name is not None:
464+
raise ModelicaSystemError("Can not reuse this instance of ModelicaSystem "
465+
f"defined for {repr(self._model_name)}!")
466+
467+
if not isinstance(name, str):
468+
raise ModelicaSystemError("A model name must be provided!")
465469

466470
if libraries is None:
467471
libraries = []
@@ -470,8 +474,8 @@ def model_definition(
470474
raise ModelicaSystemError(f"Invalid input type for lmodel: {type(libraries)} - list expected!")
471475

472476
# set variables
473-
self._model_name = model # Model class name
474-
self._lmodel = libraries # may be needed if model is derived from other model
477+
self._model_name = name # Model class name
478+
self._libraries = libraries # may be needed if model is derived from other model
475479
if file is not None:
476480
file_name = self._getconn.omcpath(file).resolve()
477481
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
@@ -23,9 +23,9 @@ def test_ModelicaSystem_loop(model_firstorder):
2323
def worker():
2424
filePath = model_firstorder.as_posix()
2525
mod = OMPython.ModelicaSystem()
26-
mod.model_definition(
26+
mod.model(
2727
file=filePath,
28-
model="M",
28+
name="M",
2929
)
3030
mod.simulate()
3131
mod.convertMo2Fmu(fmuType="me")
@@ -37,9 +37,9 @@ def test_setParameters():
3737
omc = OMPython.OMCSessionZMQ()
3838
model_path = omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels/"
3939
mod = OMPython.ModelicaSystem()
40-
mod.model_definition(
40+
mod.model(
4141
file=model_path + "BouncingBall.mo",
42-
model="BouncingBall",
42+
name="BouncingBall",
4343
)
4444

4545
# method 1 (test depreciated variants)
@@ -71,9 +71,9 @@ def test_setSimulationOptions():
7171
omc = OMPython.OMCSessionZMQ()
7272
model_path = omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels/"
7373
mod = OMPython.ModelicaSystem()
74-
mod.model_definition(
74+
mod.model(
7575
file=model_path + "BouncingBall.mo",
76-
model="BouncingBall",
76+
name="BouncingBall",
7777
)
7878

7979
# method 1
@@ -108,9 +108,9 @@ def test_relative_path(model_firstorder):
108108
assert "/" not in model_relative
109109

110110
mod = OMPython.ModelicaSystem()
111-
mod.model_definition(
111+
mod.model(
112112
file=model_relative,
113-
model="M",
113+
name="M",
114114
)
115115
assert float(mod.getParameters("a")[0]) == -1
116116
finally:
@@ -122,9 +122,9 @@ def test_customBuildDirectory(tmp_path, model_firstorder):
122122
tmpdir = tmp_path / "tmpdir1"
123123
tmpdir.mkdir()
124124
mod = OMPython.ModelicaSystem(customBuildDirectory=tmpdir)
125-
mod.model_definition(
125+
mod.model(
126126
file=filePath,
127-
model="M",
127+
name="M",
128128
)
129129
assert pathlib.Path(mod.getWorkDirectory()).resolve() == tmpdir.resolve()
130130
result_file = tmpdir / "a.mat"
@@ -136,9 +136,9 @@ def test_customBuildDirectory(tmp_path, model_firstorder):
136136
def test_getSolutions(model_firstorder):
137137
filePath = model_firstorder.as_posix()
138138
mod = OMPython.ModelicaSystem()
139-
mod.model_definition(
139+
mod.model(
140140
file=filePath,
141-
model="M",
141+
name="M",
142142
)
143143
x0 = 1
144144
a = -1
@@ -179,9 +179,9 @@ def test_getters(tmp_path):
179179
end M_getters;
180180
""")
181181
mod = OMPython.ModelicaSystem()
182-
mod.model_definition(
182+
mod.model(
183183
file=model_file.as_posix(),
184-
model="M_getters",
184+
name="M_getters",
185185
)
186186

187187
q = mod.getQuantities()
@@ -375,9 +375,9 @@ def test_simulate_inputs(tmp_path):
375375
end M_input;
376376
""")
377377
mod = OMPython.ModelicaSystem()
378-
mod.model_definition(
378+
mod.model(
379379
file=model_file.as_posix(),
380-
model="M_input",
380+
name="M_input",
381381
)
382382

383383
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(runpath=mod.getWorkDirectory(), modelname=mod._model_name)
2626
return mscmd

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)