Skip to content

Commit a23d913

Browse files
authored
Merge branch 'master' into use_items
2 parents 168606f + 6b863ff commit a23d913

8 files changed

Lines changed: 244 additions & 186 deletions

File tree

.github/workflows/FMITest.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
steps:
1919
- uses: actions/checkout@v5
2020
- name: "Set up OpenModelica Compiler"
21-
uses: OpenModelica/setup-openmodelica@v1.0.2
21+
uses: OpenModelica/setup-openmodelica@v1.0.4
2222
with:
2323
version: ${{ matrix.omc-version }}
2424
packages: |

.github/workflows/Test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141
run: 'pre-commit run --all-files'
4242

4343
- name: "Set up OpenModelica Compiler"
44-
uses: OpenModelica/setup-openmodelica@v1.0.2
44+
uses: OpenModelica/setup-openmodelica@v1.0.4
4545
with:
4646
version: ${{ matrix.omc-version }}
4747
packages: |

OMPython/ModelicaSystem.py

Lines changed: 190 additions & 147 deletions
Large diffs are not rendered by default.

OMPython/OMCSession.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,8 @@ def sendExpression(self, command: str, parsed: bool = True) -> Any:
639639
# see: https://build.openmodelica.org/Documentation/OpenModelica.Scripting.ErrorLevel.html
640640
if log_level == 'error':
641641
raise OMCSessionException(msg)
642-
elif log_level == 'warning':
642+
643+
if log_level == 'warning':
643644
logger.warning(msg)
644645
elif log_level == 'notification':
645646
logger.info(msg)
@@ -769,7 +770,7 @@ class OMCProcessLocal(OMCProcess):
769770
def __init__(
770771
self,
771772
timeout: float = 10.00,
772-
omhome: Optional[str] = None,
773+
omhome: Optional[str | os.PathLike] = None,
773774
) -> None:
774775

775776
super().__init__(timeout=timeout)
@@ -782,7 +783,7 @@ def __init__(
782783
self._omc_port = self._omc_port_get()
783784

784785
@staticmethod
785-
def _omc_home_get(omhome: Optional[str] = None) -> pathlib.Path:
786+
def _omc_home_get(omhome: Optional[str | os.PathLike] = None) -> pathlib.Path:
786787
# use the provided path
787788
if omhome is not None:
788789
return pathlib.Path(omhome)
@@ -854,7 +855,7 @@ def __init__(
854855
self,
855856
timeout: float = 10.00,
856857
dockerExtraArgs: Optional[list] = None,
857-
dockerOpenModelicaPath: str = "omc",
858+
dockerOpenModelicaPath: str | os.PathLike = "omc",
858859
dockerNetwork: Optional[str] = None,
859860
port: Optional[int] = None,
860861
) -> None:
@@ -864,7 +865,7 @@ def __init__(
864865
dockerExtraArgs = []
865866

866867
self._dockerExtraArgs = dockerExtraArgs
867-
self._dockerOpenModelicaPath = dockerOpenModelicaPath
868+
self._dockerOpenModelicaPath = pathlib.PurePosixPath(dockerOpenModelicaPath)
868869
self._dockerNetwork = dockerNetwork
869870

870871
self._interactivePort = port
@@ -970,7 +971,7 @@ def __init__(
970971
timeout: float = 10.00,
971972
docker: Optional[str] = None,
972973
dockerExtraArgs: Optional[list] = None,
973-
dockerOpenModelicaPath: str = "omc",
974+
dockerOpenModelicaPath: str | os.PathLike = "omc",
974975
dockerNetwork: Optional[str] = None,
975976
port: Optional[int] = None,
976977
) -> None:
@@ -1053,7 +1054,7 @@ def _docker_omc_cmd(
10531054
]
10541055
+ self._dockerExtraArgs
10551056
+ dockerNetworkStr
1056-
+ [self._docker, self._dockerOpenModelicaPath]
1057+
+ [self._docker, self._dockerOpenModelicaPath.as_posix()]
10571058
+ omc_path_and_args_list
10581059
+ extraFlags)
10591060

@@ -1113,7 +1114,7 @@ def __init__(
11131114
timeout: float = 10.00,
11141115
dockerContainer: Optional[str] = None,
11151116
dockerExtraArgs: Optional[list] = None,
1116-
dockerOpenModelicaPath: str = "omc",
1117+
dockerOpenModelicaPath: str | os.PathLike = "omc",
11171118
dockerNetwork: Optional[str] = None,
11181119
port: Optional[int] = None,
11191120
) -> None:
@@ -1165,7 +1166,7 @@ def _docker_omc_cmd(self, omc_path_and_args_list) -> list:
11651166
"--user", str(self._getuid()),
11661167
]
11671168
+ self._dockerExtraArgs
1168-
+ [self._dockerCid, self._dockerOpenModelicaPath]
1169+
+ [self._dockerCid, self._dockerOpenModelicaPath.as_posix()]
11691170
+ omc_path_and_args_list
11701171
+ extraFlags)
11711172

tests/test_FMIExport.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import OMPython
22
import shutil
33
import os
4+
import pathlib
45

56

67
def test_CauerLowPassAnalog():
78
mod = OMPython.ModelicaSystem(modelName="Modelica.Electrical.Analog.Examples.CauerLowPassAnalog",
89
lmodel=["Modelica"])
9-
tmp = mod.getWorkDirectory()
10+
tmp = pathlib.Path(mod.getWorkDirectory())
1011
try:
1112
fmu = mod.convertMo2Fmu(fileNamePrefix="CauerLowPassAnalog")
1213
assert os.path.exists(fmu)
@@ -16,7 +17,7 @@ def test_CauerLowPassAnalog():
1617

1718
def test_DrumBoiler():
1819
mod = OMPython.ModelicaSystem(modelName="Modelica.Fluid.Examples.DrumBoiler.DrumBoiler", lmodel=["Modelica"])
19-
tmp = mod.getWorkDirectory()
20+
tmp = pathlib.Path(mod.getWorkDirectory())
2021
try:
2122
fmu = mod.convertMo2Fmu(fileNamePrefix="DrumBoiler")
2223
assert os.path.exists(fmu)

tests/test_ModelicaSystem.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ def test_setParameters():
3434
model_path = omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels/"
3535
mod = OMPython.ModelicaSystem(model_path + "BouncingBall.mo", "BouncingBall")
3636

37-
# method 1
38-
mod.setParameters(pvals={"e": 1.234})
39-
mod.setParameters(pvals={"g": 321.0})
37+
# method 1 (test depreciated variants)
38+
mod.setParameters("e=1.234")
39+
mod.setParameters(["g=321.0"])
4040
assert mod.getParameters("e") == ["1.234"]
4141
assert mod.getParameters("g") == ["321.0"]
4242
assert mod.getParameters() == {
@@ -46,8 +46,9 @@ def test_setParameters():
4646
with pytest.raises(KeyError):
4747
mod.getParameters("thisParameterDoesNotExist")
4848

49-
# method 2
50-
mod.setParameters(pvals={"e": 21.3, "g": 0.12})
49+
# method 2 (new style)
50+
pvals = {"e": 21.3, "g": 0.12}
51+
mod.setParameters(**pvals)
5152
assert mod.getParameters() == {
5253
"e": "21.3",
5354
"g": "0.12",
@@ -64,8 +65,8 @@ def test_setSimulationOptions():
6465
mod = OMPython.ModelicaSystem(fileName=model_path + "BouncingBall.mo", modelName="BouncingBall")
6566

6667
# method 1
67-
mod.setSimulationOptions(simOptions={"stopTime": 1.234})
68-
mod.setSimulationOptions(simOptions={"tolerance": 1.1e-08})
68+
mod.setSimulationOptions(stopTime=1.234)
69+
mod.setSimulationOptions(tolerance=1.1e-08)
6970
assert mod.getSimulationOptions("stopTime") == ["1.234"]
7071
assert mod.getSimulationOptions("tolerance") == ["1.1e-08"]
7172
assert mod.getSimulationOptions(["tolerance", "stopTime"]) == ["1.1e-08", "1.234"]
@@ -77,7 +78,7 @@ def test_setSimulationOptions():
7778
mod.getSimulationOptions("thisOptionDoesNotExist")
7879

7980
# method 2
80-
mod.setSimulationOptions(simOptions={"stopTime": 2.1, "tolerance": "1.2e-08"})
81+
mod.setSimulationOptions(stopTime=2.1, tolerance=1.2e-08)
8182
d = mod.getSimulationOptions()
8283
assert d["stopTime"] == "2.1"
8384
assert d["tolerance"] == "1.2e-08"
@@ -105,7 +106,7 @@ def test_customBuildDirectory(tmp_path, model_firstorder):
105106
tmpdir = tmp_path / "tmpdir1"
106107
tmpdir.mkdir()
107108
m = OMPython.ModelicaSystem(filePath, "M", customBuildDirectory=tmpdir)
108-
assert m.getWorkDirectory().resolve() == tmpdir.resolve()
109+
assert pathlib.Path(m.getWorkDirectory()).resolve() == tmpdir.resolve()
109110
result_file = tmpdir / "a.mat"
110111
assert not result_file.exists()
111112
m.simulate(resultfile="a.mat")
@@ -119,7 +120,9 @@ def test_getSolutions(model_firstorder):
119120
a = -1
120121
tau = -1 / a
121122
stopTime = 5*tau
122-
mod.setSimulationOptions(simOptions={"stopTime": stopTime, "stepSize": 0.1, "tolerance": 1e-8})
123+
124+
simOptions = {"stopTime": stopTime, "stepSize": 0.1, "tolerance": 1e-8}
125+
mod.setSimulationOptions(**simOptions)
123126
mod.simulate()
124127

125128
x = mod.getSolutions("x")
@@ -298,7 +301,7 @@ def test_getters(tmp_path):
298301
x0 = 1.0
299302
x_analytical = -b/a + (x0 + b/a) * np.exp(a * stopTime)
300303
dx_analytical = (x0 + b/a) * a * np.exp(a * stopTime)
301-
mod.setSimulationOptions(simOptions={"stopTime": stopTime})
304+
mod.setSimulationOptions(stopTime=stopTime)
302305
mod.simulate()
303306

304307
# getOutputs after simulate()
@@ -327,7 +330,7 @@ def test_getters(tmp_path):
327330
mod.getContinuous("a") # a is a parameter
328331

329332
with pytest.raises(OMPython.ModelicaSystemError):
330-
mod.setSimulationOptions(simOptions={"thisOptionDoesNotExist": 3})
333+
mod.setSimulationOptions(thisOptionDoesNotExist=3)
331334

332335

333336
def test_simulate_inputs(tmp_path):
@@ -345,7 +348,8 @@ def test_simulate_inputs(tmp_path):
345348
""")
346349
mod = OMPython.ModelicaSystem(fileName=model_file.as_posix(), modelName="M_input")
347350

348-
mod.setSimulationOptions(simOptions={"stopTime": 1.0})
351+
simOptions = {"stopTime": 1.0}
352+
mod.setSimulationOptions(**simOptions)
349353

350354
# integrate zero (no setInputs call) - it should default to None -> 0
351355
assert mod.getInputs() == {
@@ -357,7 +361,7 @@ def test_simulate_inputs(tmp_path):
357361
assert np.isclose(y[-1], 0.0)
358362

359363
# integrate a constant
360-
mod.setInputs(name={"u1": 2.5})
364+
mod.setInputs(u1=2.5)
361365
assert mod.getInputs() == {
362366
"u1": [
363367
(0.0, 2.5),
@@ -374,7 +378,8 @@ def test_simulate_inputs(tmp_path):
374378
assert np.isclose(y[-1], 2.5)
375379

376380
# now let's integrate the sum of two ramps
377-
mod.setInputs(name={"u1": [(0.0, 0.0), (0.5, 2), (1.0, 0)]})
381+
inputs = {"u1": [(0.0, 0.0), (0.5, 2), (1.0, 0)]}
382+
mod.setInputs(**inputs)
378383
assert mod.getInputs("u1") == [[
379384
(0.0, 0.0),
380385
(0.5, 2.0),
@@ -387,17 +392,20 @@ def test_simulate_inputs(tmp_path):
387392
# let's try some edge cases
388393
# unmatched startTime
389394
with pytest.raises(OMPython.ModelicaSystemError):
390-
mod.setInputs(name={"u1": [(-0.5, 0.0), (1.0, 1)]})
395+
mod.setInputs(u1=[(-0.5, 0.0), (1.0, 1)])
391396
mod.simulate()
392397
# unmatched stopTime
393398
with pytest.raises(OMPython.ModelicaSystemError):
394-
mod.setInputs(name={"u1": [(0.0, 0.0), (0.5, 1)]})
399+
mod.setInputs(u1=[(0.0, 0.0), (0.5, 1)])
395400
mod.simulate()
396401

397402
# Let's use both inputs, but each one with different number of
398403
# samples. This has an effect when generating the csv file.
399-
mod.setInputs(name={"u1": [(0.0, 0), (1.0, 1)],
400-
"u2": [(0.0, 0), (0.25, 0.5), (0.5, 1.0), (1.0, 0)]})
404+
inputs = {
405+
"u1": [(0.0, 0), (1.0, 1)],
406+
"u2": [(0.0, 0), (0.25, 0.5), (0.5, 1.0), (1.0, 0)],
407+
}
408+
mod.setInputs(**inputs)
401409
csv_file = mod._createCSVData()
402410
assert pathlib.Path(csv_file).read_text() == """time,u1,u2,end
403411
0.0,0.0,0.0,0

tests/test_linearization.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ def test_getters(tmp_path):
6262
assert "startTime" in d
6363
assert "stopTime" in d
6464
assert mod.getLinearizationOptions(["stopTime", "startTime"]) == [d["stopTime"], d["startTime"]]
65-
mod.setLinearizationOptions(linearizationOptions={"stopTime": 0.02})
65+
mod.setLinearizationOptions(stopTime=0.02)
6666
assert mod.getLinearizationOptions("stopTime") == ["0.02"]
6767

68-
mod.setInputs(name={"u1": 10, "u2": 0})
68+
mod.setInputs(u1=10, u2=0)
6969
[A, B, C, D] = mod.linearize()
7070
g = float(mod.getParameters("g")[0])
7171
l = float(mod.getParameters("l")[0])

tests/test_optimization.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,13 @@ def test_optimization_example(tmp_path):
3535

3636
mod = OMPython.ModelicaSystem(fileName=model_file.as_posix(), modelName="BangBang2021")
3737

38-
mod.setOptimizationOptions(optimizationOptions={"numberOfIntervals": 16,
39-
"stopTime": 1,
40-
"stepSize": 0.001,
41-
"tolerance": 1e-8})
38+
optimizationOptions = {
39+
"numberOfIntervals": 16,
40+
"stopTime": 1,
41+
"stepSize": 0.001,
42+
"tolerance": 1e-8,
43+
}
44+
mod.setOptimizationOptions(**optimizationOptions)
4245

4346
# test the getter
4447
assert mod.getOptimizationOptions()["stopTime"] == "1"
@@ -47,7 +50,9 @@ def test_optimization_example(tmp_path):
4750

4851
r = mod.optimize()
4952
# it is necessary to specify resultfile, otherwise it wouldn't find it.
50-
time, f, v = mod.getSolutions(["time", "f", "v"], resultfile=r["resultFile"])
53+
resultfile_str = r["resultFile"]
54+
resultfile_omcpath = mod._getconn.omcpath(resultfile_str)
55+
time, f, v = mod.getSolutions(["time", "f", "v"], resultfile=resultfile_omcpath.as_posix())
5156
assert np.isclose(f[0], 10)
5257
assert np.isclose(f[-1], -10)
5358

0 commit comments

Comments
 (0)