Skip to content
11 changes: 8 additions & 3 deletions .github/workflows/Test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ on:
- 'v*' # only publish when pushing version tags (e.g., v1.0.0)
pull_request:
workflow_dispatch:
inputs:
pytest_args:
description: 'Extra pytest arguments'
required: false
default: ''

jobs:
test:
runs-on: ${{ matrix.os }}
timeout-minutes: 45
timeout-minutes: 120
strategy:
matrix:
# test for:
Expand Down Expand Up @@ -73,7 +78,7 @@ jobs:
verbose: true
emoji: true
job-summary: true
custom-arguments: '-v ./tests'
custom-arguments: '-v ${{ inputs.pytest_args }} ./tests'
click-to-expand: true
report-title: 'Test Report'

Expand All @@ -83,7 +88,7 @@ jobs:
verbose: true
emoji: true
job-summary: true
custom-arguments: '-v ./tests_v400'
custom-arguments: '-v ${{ inputs.pytest_args }} ./tests_v400'
click-to-expand: true
report-title: 'Test Report (v4.0.0 compatibility layer)'

Expand Down
4 changes: 2 additions & 2 deletions OMPython/ModelicaSystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def __init__(
cmd_prefix: list[str],
cmd_local: bool = False,
cmd_windows: bool = False,
timeout: float = 10.0,
timeout: float = 300.0,
model_name: Optional[str] = None,
) -> None:
if model_name is None:
Expand Down Expand Up @@ -2835,7 +2835,7 @@ def __init__(
self,
runpath: pathlib.Path,
modelname: str,
timeout: float = 10.0,
timeout: float = 300.0,
) -> None:
super().__init__(
runpath=runpath,
Expand Down
2 changes: 1 addition & 1 deletion OMPython/OMCSession.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ def __init__(
self.model_execution_local = False

# store variables
self._timeout = 10.0
self._timeout = 300.0
self.set_timeout(timeout=timeout)
# command prefix (to be used for docker or WSL)
self._cmd_prefix: list[str] = []
Expand Down
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