From dfb466d4ae37c628b0239eb451cf4998746bbf9b Mon Sep 17 00:00:00 2001 From: syntron Date: Fri, 31 Oct 2025 16:59:00 +0100 Subject: [PATCH 1/6] [ModelicaSystem] consider relativ path if ModelicaSystem is run locally --- OMPython/ModelicaSystem.py | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/OMPython/ModelicaSystem.py b/OMPython/ModelicaSystem.py index 241b9ffc..edb5ed4d 100644 --- a/OMPython/ModelicaSystem.py +++ b/OMPython/ModelicaSystem.py @@ -38,6 +38,7 @@ import logging import numbers import os +import pathlib import queue import textwrap import threading @@ -446,18 +447,30 @@ def model( # set variables self._model_name = name # Model class name self._libraries = libraries # may be needed if model is derived from other model - if file is not None: - file_name = self._session.omcpath(file).resolve() - else: - file_name = None - self._file_name = file_name # Model file/package name self._variable_filter = variable_filter - if self._file_name is not None and not self._file_name.is_file(): # if file does not exist - raise IOError(f"{self._file_name} does not exist!") - if self._libraries: self._loadLibrary(libraries=self._libraries) + + self._file_name = None + if file is not None: + file_path = pathlib.Path(file) + # special handling for OMCProcessLocal - consider a relative path + if isinstance(self._session.omc_process, OMCProcessLocal) and not file_path.is_absolute(): + file_path = pathlib.Path.cwd() / file_path + if not file_path.is_file(): + raise IOError(f"Model file {file_path} does not exist!") + + self._file_name = self.getWorkDirectory() / file_path.name + if (isinstance(self._session.omc_process, OMCProcessLocal) + and file_path.as_posix() == self._file_name.as_posix()): + pass + elif self._file_name.is_file(): + raise IOError(f"Simulation model file {self._file_name} exist - not overwriting!") + else: + content = file_path.read_text(encoding='utf-8') + self._file_name.write_text(content) + if self._file_name is not None: self._loadFile(fileName=self._file_name) @@ -1685,7 +1698,7 @@ def convertFmu2Mo( raise ModelicaSystemError(f"Missing FMU file: {fmu_path.as_posix()}") filename = self._requestApi(apiName='importFMU', entity=fmu_path.as_posix()) - filepath = self._work_dir / filename + filepath = self._session.omcpath(filename) # report proper error message if not filepath.is_file(): From 07349a03d3e5a199e95fd941626b522e99315323 Mon Sep 17 00:00:00 2001 From: syntron Date: Wed, 5 Nov 2025 15:21:45 +0100 Subject: [PATCH 2/6] [ModelicaSystem] fix filename for model based on imported FMU --- OMPython/ModelicaSystem.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OMPython/ModelicaSystem.py b/OMPython/ModelicaSystem.py index edb5ed4d..1f0d2a09 100644 --- a/OMPython/ModelicaSystem.py +++ b/OMPython/ModelicaSystem.py @@ -1698,7 +1698,7 @@ def convertFmu2Mo( raise ModelicaSystemError(f"Missing FMU file: {fmu_path.as_posix()}") filename = self._requestApi(apiName='importFMU', entity=fmu_path.as_posix()) - filepath = self._session.omcpath(filename) + filepath = self.getWorkDirectory() / filename # report proper error message if not filepath.is_file(): From 998f811356b5437f31f55c1ca2751df628173b0c Mon Sep 17 00:00:00 2001 From: syntron Date: Tue, 4 Nov 2025 18:27:56 +0100 Subject: [PATCH 3/6] Revert "[test_ModelicaSystem] test_relative_path will fail at the moment; a fix is available" This reverts commit 95a97a409c12642cc270069c2d75b8ae82713b23. --- tests/test_ModelicaSystem.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_ModelicaSystem.py b/tests/test_ModelicaSystem.py index c268a003..7ac71fb1 100644 --- a/tests/test_ModelicaSystem.py +++ b/tests/test_ModelicaSystem.py @@ -112,7 +112,6 @@ def test_setSimulationOptions(): assert d["tolerance"] == "1.2e-08" -@pytest.mark.skip("will fail / fix available") def test_relative_path(model_firstorder): cwd = pathlib.Path.cwd() (fd, name) = tempfile.mkstemp(prefix='tmpOMPython.tests', dir=cwd, text=True) From b5ffcc415a65ed47c752602971397cf705c9fd6f Mon Sep 17 00:00:00 2001 From: syntron Date: Tue, 4 Nov 2025 19:27:13 +0100 Subject: [PATCH 4/6] [test_ModelicaSystem] fix test_setParameters & test_setSimulationOptions --- tests/test_ModelicaSystem.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/test_ModelicaSystem.py b/tests/test_ModelicaSystem.py index 7ac71fb1..be1f7a8c 100644 --- a/tests/test_ModelicaSystem.py +++ b/tests/test_ModelicaSystem.py @@ -51,10 +51,11 @@ def worker(): def test_setParameters(): omc = OMPython.OMCSessionZMQ() - model_path = omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels/" + model_path_str = omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels" + model_path = omc.omcpath(model_path_str) mod = OMPython.ModelicaSystem() mod.model( - file=model_path + "BouncingBall.mo", + file=model_path / "BouncingBall.mo", name="BouncingBall", ) @@ -85,10 +86,11 @@ def test_setParameters(): def test_setSimulationOptions(): omc = OMPython.OMCSessionZMQ() - model_path = omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels/" + model_path_str = omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels" + model_path = omc.omcpath(model_path_str) mod = OMPython.ModelicaSystem() mod.model( - file=model_path + "BouncingBall.mo", + file=model_path / "BouncingBall.mo", name="BouncingBall", ) From 36e4763e980edcd6dd8d81e69cd8bae6a40c118b Mon Sep 17 00:00:00 2001 From: syntron Date: Wed, 5 Nov 2025 20:42:32 +0100 Subject: [PATCH 5/6] [test_ModelicaSystem] fix tests - use local file as it is copied if needed --- tests/test_ModelicaSystem.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/tests/test_ModelicaSystem.py b/tests/test_ModelicaSystem.py index be1f7a8c..79a94d61 100644 --- a/tests/test_ModelicaSystem.py +++ b/tests/test_ModelicaSystem.py @@ -153,30 +153,25 @@ def test_customBuildDirectory(tmp_path, model_firstorder): @skip_on_windows @skip_python_older_312 -def test_getSolutions_docker(model_firstorder_content): +def test_getSolutions_docker(model_firstorder): omcp = OMPython.OMCProcessDocker(docker="openmodelica/openmodelica:v1.25.0-minimal") omc = OMPython.OMCSessionZMQ(omc_process=omcp) - modelpath = omc.omcpath_tempdir() / 'M.mo' - modelpath.write_text(model_firstorder_content) - - file_path = pathlib.Path(modelpath) mod = OMPython.ModelicaSystem( omc_process=omc.omc_process, ) mod.model( name="M", - file=file_path, + file=model_firstorder.as_posix(), ) _run_getSolutions(mod) def test_getSolutions(model_firstorder): - filePath = model_firstorder.as_posix() mod = OMPython.ModelicaSystem() mod.model( - file=filePath, + file=model_firstorder.as_posix(), name="M", ) From 18dec079c15013f228cbfddb5998da15c083ed34 Mon Sep 17 00:00:00 2001 From: syntron Date: Fri, 31 Oct 2025 17:02:24 +0100 Subject: [PATCH 6/6] [test_ModelicSystemDoE] simplify test_ModelicaSystemDoE_local --- tests/test_ModelicaSystemDoE.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tests/test_ModelicaSystemDoE.py b/tests/test_ModelicaSystemDoE.py index a5ab37e1..72d9eeea 100644 --- a/tests/test_ModelicaSystemDoE.py +++ b/tests/test_ModelicaSystemDoE.py @@ -71,15 +71,13 @@ def test_ModelicaSystemDoE_docker(tmp_path, model_doe, param_doe): omc = OMPython.OMCSessionZMQ(omc_process=omcp) assert omc.sendExpression("getVersion()") == "OpenModelica 1.25.0" - modelpath = omc.omcpath_tempdir() / 'M.mo' - modelpath.write_text(model_doe.read_text()) - + modelpath = omc.omcpath_tempdir() doe_mod = OMPython.ModelicaSystemDoE( - fileName=modelpath.as_posix(), + fileName=model_doe.as_posix(), modelName="M", parameters=param_doe, omc_process=omcp, - resultpath=modelpath.parent, + resultpath=modelpath, simargs={"override": {'stopTime': 1.0}}, )