Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions OMPython/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
OMPathABC,
OMCPath,

OMSessionABC,
OMSessionRunner,

OMCSessionABC,
Expand Down Expand Up @@ -77,6 +78,7 @@
'OMPathABC',
'OMCPath',

'OMSessionABC',
'OMSessionRunner',

'OMCSessionABC',
Expand Down
2 changes: 1 addition & 1 deletion tests/test_FMIExport.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import shutil
import os
import pathlib
import shutil

import OMPython

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def mscmd_firstorder(model_firstorder):
cmd_local=mod.get_session().model_execution_local,
cmd_windows=mod.get_session().model_execution_windows,
cmd_prefix=mod.get_session().model_execution_prefix(cwd=mod.getWorkDirectory()),
model_name=mod._model_name,
model_name=mod.get_model_name(),
)

return mscmd
Expand Down
6 changes: 3 additions & 3 deletions tests/test_ModelicaDoEOMC.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,6 @@ def _run_ModelicaDoEOMC(doe_mod):
f"y[{row['p']}]": float(row['b']),
}

for var in var_dict:
assert var in sol['data']
assert np.isclose(sol['data'][var][-1], var_dict[var])
for key, val in var_dict.items():
assert key in sol['data']
assert np.isclose(sol['data'][key][-1], val)
6 changes: 3 additions & 3 deletions tests/test_ModelicaDoERunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,6 @@ def _check_runner_result(mod, doe_mod):
'b': float(row['b']),
}

for var in var_dict:
assert var in sol['data']
assert np.isclose(sol['data'][var][-1], var_dict[var])
for key, val in var_dict.items():
assert key in sol['data']
assert np.isclose(sol['data'][key][-1], val)
2 changes: 1 addition & 1 deletion tests/test_ModelicaSystemOMC.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ def test_simulate_inputs(tmp_path):
}
mod.setInputs(**inputs)
csv_file = mod._createCSVData()
assert pathlib.Path(csv_file).read_text() == """time,u1,u2,end
assert pathlib.Path(csv_file).read_text(encoding='utf-8') == """time,u1,u2,end
0.0,0.0,0.0,0
0.25,0.25,0.5,0
0.5,0.5,1.0,0
Expand Down
176 changes: 175 additions & 1 deletion tests/test_ModelicaSystemRunner.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import sys

import numpy as np
import pytest

import OMPython


skip_on_windows = pytest.mark.skipif(
sys.platform.startswith("win"),
reason="OpenModelica Docker image is Linux-only; skipping on Windows.",
)

skip_python_older_312 = pytest.mark.skipif(
sys.version_info < (3, 12),
reason="OMCPath(non-local) only working for Python >= 3.12.",
)


@pytest.fixture
def model_firstorder_content():
return """
Expand Down Expand Up @@ -37,7 +50,7 @@ def param():
}


def test_runner(model_firstorder, param):
def test_ModelicaSystemRunner_OMC(model_firstorder, param):
# create a model using ModelicaSystem
mod = OMPython.ModelicaSystemOMC()
mod.model(
Expand Down Expand Up @@ -71,6 +84,167 @@ def test_runner(model_firstorder, param):
_check_result(mod=mod, resultfile=resultfile_modr, param=param)


def test_ModelicaSystemRunner_local(model_firstorder, param):
# create a model using ModelicaSystem
mod = OMPython.ModelicaSystemOMC()
mod.model(
model_file=model_firstorder,
model_name="M",
)

resultfile_mod = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_mod.mat"
_run_simulation(mod=mod, resultfile=resultfile_mod, param=param)

# run the model using only the runner class
omcs = OMPython.OMSessionRunner(
version=mod.get_session().get_version(),
ompath_runner=OMPython.OMPathRunnerLocal,
)
modr = OMPython.ModelicaSystemRunner(
session=omcs,
work_directory=mod.getWorkDirectory(),
)
modr.setup(
model_name="M",
)

resultfile_modr = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_modr.mat"
_run_simulation(mod=modr, resultfile=resultfile_modr, param=param)

# cannot check the content as runner does not have the capability to open a result file
assert resultfile_mod.size() == resultfile_modr.size()

# check results
_check_result(mod=mod, resultfile=resultfile_mod, param=param)
_check_result(mod=mod, resultfile=resultfile_modr, param=param)


@skip_on_windows
def test_ModelicaSystemRunner_bash(model_firstorder, param):
# create a model using ModelicaSystem
mod = OMPython.ModelicaSystemOMC()
mod.model(
model_file=model_firstorder,
model_name="M",
)

resultfile_mod = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_mod.mat"
_run_simulation(mod=mod, resultfile=resultfile_mod, param=param)

# run the model using only the runner class
omcsr = OMPython.OMSessionRunner(
version=mod.get_session().get_version(),
ompath_runner=OMPython.OMPathRunnerBash,
)
modr = OMPython.ModelicaSystemRunner(
session=omcsr,
work_directory=mod.getWorkDirectory(),
)
modr.setup(
model_name="M",
)

resultfile_modr = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_modr.mat"
_run_simulation(mod=modr, resultfile=resultfile_modr, param=param)

# cannot check the content as runner does not have the capability to open a result file
assert resultfile_mod.size() == resultfile_modr.size()

# check results
_check_result(mod=mod, resultfile=resultfile_mod, param=param)
_check_result(mod=mod, resultfile=resultfile_modr, param=param)


@skip_on_windows
@skip_python_older_312
def test_ModelicaSystemRunner_bash_docker(model_firstorder, param):
omcs = OMPython.OMCSessionDocker(docker="openmodelica/openmodelica:v1.25.0-minimal")
omversion = omcs.sendExpression("getVersion()")
assert isinstance(omversion, str) and omversion.startswith("OpenModelica")

# create a model using ModelicaSystem
mod = OMPython.ModelicaSystemOMC(
session=omcs,
)
mod.model(
model_file=model_firstorder,
model_name="M",
)

resultfile_mod = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_mod.mat"
_run_simulation(mod=mod, resultfile=resultfile_mod, param=param)

# run the model using only the runner class
omcsr = OMPython.OMSessionRunner(
version=mod.get_session().get_version(),
cmd_prefix=omcs.model_execution_prefix(cwd=mod.getWorkDirectory()),
ompath_runner=OMPython.OMPathRunnerBash,
model_execution_local=False,
)
modr = OMPython.ModelicaSystemRunner(
session=omcsr,
work_directory=mod.getWorkDirectory(),
)
modr.setup(
model_name="M",
)

resultfile_modr = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_modr.mat"
_run_simulation(mod=modr, resultfile=resultfile_modr, param=param)

# cannot check the content as runner does not have the capability to open a result file
assert resultfile_mod.size() == resultfile_modr.size()

# check results
_check_result(mod=mod, resultfile=resultfile_mod, param=param)
_check_result(mod=mod, resultfile=resultfile_modr, param=param)


@pytest.mark.skip(reason="Not able to run WSL on github")
@skip_python_older_312
def test_ModelicaSystemDoE_WSL(tmp_path, model_doe, param_doe):
omcs = OMPython.OMCSessionWSL()
omversion = omcs.sendExpression("getVersion()")
assert isinstance(omversion, str) and omversion.startswith("OpenModelica")

# create a model using ModelicaSystem
mod = OMPython.ModelicaSystemOMC(
session=omcs,
)
mod.model(
model_file=model_firstorder,
model_name="M",
)

resultfile_mod = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_mod.mat"
_run_simulation(mod=mod, resultfile=resultfile_mod, param=param)

# run the model using only the runner class
omcsr = OMPython.OMSessionRunner(
version=mod.get_session().get_version(),
cmd_prefix=omcs.model_execution_prefix(cwd=mod.getWorkDirectory()),
ompath_runner=OMPython.OMPathRunnerBash,
model_execution_local=False,
)
modr = OMPython.ModelicaSystemRunner(
session=omcsr,
work_directory=mod.getWorkDirectory(),
)
modr.setup(
model_name="M",
)

resultfile_modr = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_modr.mat"
_run_simulation(mod=modr, resultfile=resultfile_modr, param=param)

# cannot check the content as runner does not have the capability to open a result file
assert resultfile_mod.size() == resultfile_modr.size()

# check results
_check_result(mod=mod, resultfile=resultfile_mod, param=param)
_check_result(mod=mod, resultfile=resultfile_modr, param=param)


def _run_simulation(mod, resultfile, param):
simOptions = {"stopTime": param['stopTime'], "stepSize": 0.1, "tolerance": 1e-8}
mod.setSimulationOptions(**simOptions)
Expand Down
Loading
Loading