Skip to content

ModelicaSystem - use OMCPath #322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 57 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
8d19d26
[ModelicaSystem] add type hints for set*() functions and rename argum…
syntron Jun 23, 2025
9ac6a0e
[ModelicaSystem] add _prepare_inputdata()
syntron Jun 23, 2025
980ac33
[ModelicaSystem] update _set_method_helper()
syntron Jun 23, 2025
7c24f10
[ModelicaSystem] improve definition of _prepare_inputdata()
syntron Jun 23, 2025
b864a6c
[ModelicaSystem] rename _prepare_inputdata() => _prepare_input_data()
syntron Jun 23, 2025
3f180a6
[ModelicaSystem] update setInput()
syntron Jun 23, 2025
55051d9
update tests - use new dict based input for set*() methods
syntron Jun 23, 2025
3d8b83a
[ModelicaSystem] add type hint for return value of isParameterChangea…
syntron Jun 23, 2025
1b5917c
[ModelicaSystem] fix type hint for _prepare_input_data() - use dict[s…
syntron Jun 23, 2025
b5766f4
[ModelicaSystem] setInput() - handly input data as list of tuples
syntron Jun 23, 2025
9ef9d69
update tests - use new dict based input for setInput() method
syntron Jun 23, 2025
2fc67b9
[test_linearization] fix setInput() call
syntron Jun 24, 2025
bbd8740
[ModelicaSystem] simplify _set_method_helper()
syntron Jun 26, 2025
675a648
[ModelicaSystem] improve setInputs() - reduce spaces / cleanup
syntron Jun 26, 2025
9fcd39a
[ModelicaSystem] fix rebase fallout
syntron Jul 7, 2025
92651c5
[ModelicaSystem] fix rebase fallout 2
syntron Jul 7, 2025
71aa67b
[OMCPath] add class
syntron Jun 28, 2025
28585c7
[OMCPath] add implementation using OMC via sendExpression()
syntron Jun 27, 2025
3ce71b9
[OMCPath] add pytest (only docker at the moment)
syntron Jun 28, 2025
e452c98
[OMCPath] TODO items
syntron Jun 28, 2025
a262189
[test_OMCPath] mypy fix
syntron Jul 2, 2025
70a9c65
[test_OMCPath] fix end of file
syntron Jul 2, 2025
9240ff6
[test_OMCPath] define test using OMCSessionZMQ() locally
syntron Jul 3, 2025
7af3eb7
add TODO - need to check Python versions
syntron Jul 6, 2025
d5445c6
[test_OMCPath] activate docker based on test_docker
syntron Jul 6, 2025
4c9f0ca
[ModelicaSystem.linearize] do not execute python file but use ast to …
syntron Jul 9, 2025
191cd59
[ModelicaSystem.linearize] remove old check / use of file in current dir
syntron Jul 9, 2025
2802d9f
[ModelicaSystem.linearize] fix mypy
syntron Jul 10, 2025
d695899
[ModelicaSystem] remove _has_inputs - is defined by _inputs empty or not
syntron Jul 7, 2025
4fa05f1
[test_ModelicaSystem] cleanup
syntron Jul 7, 2025
ab0beae
[ModelicaSystem] add spelling fix (fox codespell)
syntron Jul 11, 2025
f10e4e0
[ModelicaSystem] update handling of xml_file
syntron Jul 9, 2025
4ed4bc9
[ModelicaSystem] replace ET.parse() with ET.ElementTree(ET.fromstring())
syntron Jul 9, 2025
111326f
[ModelicaSystem._xmlparse] mypy fixes & cleanup
syntron Jul 10, 2025
f958964
[ModelicaSystem] remove class variable _xml_file
syntron Jul 10, 2025
aa74b36
[OMCPath] add more functionality and docstrings
syntron Jul 11, 2025
89cf543
[OMCPath] remove TODO entries
syntron Jul 11, 2025
8e1745c
[OMCPath] define limited compatibility for Python < 3.12
syntron Jul 11, 2025
e793a93
[OMCSEssionZMQ] use OMCpath
syntron Jul 11, 2025
0ceb2bf
[OMCSessionZMQ] create a tempdir using omcpath_tempdir()
syntron Jul 11, 2025
43288dc
[OMCPath] fix mypy
syntron Jul 11, 2025
3029138
[OMCPath] add warning message for Python < 3.12
syntron Jul 11, 2025
a5f2ad6
[OMCPath] try to make mypy happy ...
syntron Jul 11, 2025
9416597
[test_OMCPath] only for Python >= 3.12
syntron Jul 11, 2025
c45b4f7
[test_OMCPath] update test
syntron Jul 11, 2025
30cb70e
[ModelicaSystem] simplify handling of inputs
syntron Jul 11, 2025
b822bf5
Merge branch 'ModelicaSystem_rewrite_set_functions' into ModelicaSyst…
syntron Jul 12, 2025
3ba84ab
[ModelicaSystem] fix mypy warning - value can have different types in…
syntron Jul 12, 2025
4b58c03
[OMCPath._omc_resolve] use sendExpression() with parsed=False
syntron Jul 12, 2025
c71b137
[test_OMCPath] cleanup; use the same code for local OMC and docker ba…
syntron Jul 12, 2025
a97bdd0
[test_OMCPath] define test for WSL
syntron Jul 12, 2025
2dadf3e
[test_OMCPath] use omcpath_tempdir() instead of hard-coded tempdir de…
syntron Jul 12, 2025
a89221d
Merge branch 'ModelicaSystem_xml' into OMCPath_merge
syntron Jul 12, 2025
111b209
Merge branch 'ModelicaSystem_linearize' into OMCPath_merge
syntron Jul 12, 2025
04d09ba
[ModelicaSystem] use OMCPath for nearly all file system interactions
syntron Jul 11, 2025
9c860e4
test only for Python 3.12 for now
syntron Jul 11, 2025
466740c
[test_ModelicaSystem] fix test_customBuildDirectory()
syntron Jul 11, 2025
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: 1 addition & 1 deletion .github/workflows/Test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
timeout-minutes: 30
strategy:
matrix:
python-version: ['3.10', '3.12']
python-version: ['3.12']
os: ['ubuntu-latest', 'windows-latest']
omc-version: ['stable']

Expand Down
563 changes: 348 additions & 215 deletions OMPython/ModelicaSystem.py

Large diffs are not rendered by default.

199 changes: 199 additions & 0 deletions OMPython/OMCSession.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,169 @@ def getClassNames(self, className=None, recursive=False, qualified=False, sort=F
return self._ask(question='getClassNames', opt=opt)


class OMCPathReal(pathlib.PurePosixPath):
"""
Implementation of a basic Path object which uses OMC as backend. The connection to OMC is provided via a
OMCSessionZMQ session object.
"""

def __init__(self, *path, session: OMCSessionZMQ):
super().__init__(*path)
self._session = session

def with_segments(self, *pathsegments):
"""
Create a new OMCPath object with the given path segments.

The original definition of Path is overridden to ensure session is set.
"""
return type(self)(*pathsegments, session=self._session)

def is_file(self) -> bool:
"""
Check if the path is a regular file.
"""
return self._session.sendExpression(f'regularFileExists("{self.as_posix()}")')

def is_dir(self) -> bool:
"""
Check if the path is a directory.
"""
return self._session.sendExpression(f'directoryExists("{self.as_posix()}")')

def read_text(self, encoding=None, errors=None) -> str:
"""
Read the content of the file represented by this path as text.

The additional arguments `encoding` and `errors` are only defined for compatibility with Path() definitions.
"""
return self._session.sendExpression(f'readFile("{self.as_posix()}")')

def write_text(self, data: str, encoding=None, errors=None, newline=None) -> bool:
"""
Write text data to the file represented by this path.

The additional arguments `encoding`, `errors`, and `newline` are only defined for compatibility with Path()
definitions.
"""
if not isinstance(data, str):
raise TypeError('data must be str, not %s' %
data.__class__.__name__)

return self._session.sendExpression(f'writeFile("{self.as_posix()}", "{data}", false)')

def mkdir(self, mode=0o777, parents=False, exist_ok=False):
"""
Create a directory at the path represented by this OMCPath object.

The additional arguments `mode`, and `parents` are only defined for compatibility with Path() definitions.
"""
if self.is_dir() and not exist_ok:
raise FileExistsError(f"Directory {self.as_posix()} already exists!")

return self._session.sendExpression(f'mkdir("{self.as_posix()}")')

def cwd(self):
"""
Returns the current working directory as an OMCPath object.
"""
cwd_str = self._session.sendExpression('cd()')
return OMCPath(cwd_str, session=self._session)

def unlink(self, missing_ok: bool = False) -> bool:
"""
Unlink (delete) the file or directory represented by this path.
"""
res = self._session.sendExpression(f'deleteFile("{self.as_posix()}")')
if not res and not missing_ok:
raise FileNotFoundError(f"Cannot delete file {self.as_posix()} - it does not exists!")
return res

def resolve(self, strict: bool = False) -> OMCPath:
"""
Resolve the path to an absolute path. This is done based on available OMC functions.
"""
if strict and not (self.is_file() or self.is_dir()):
raise OMCSessionException(f"Path {self.as_posix()} does not exist!")

if self.is_file():
omcpath = self._omc_resolve(self.parent.as_posix()) / self.name
elif self.is_dir():
omcpath = self._omc_resolve(self.as_posix())
else:
raise OMCSessionException(f"Path {self.as_posix()} is neither a file nor a directory!")

return omcpath

def _omc_resolve(self, pathstr: str) -> OMCPath:
"""
Internal function to resolve the path of the OMCPath object using OMC functions *WITHOUT* changing the cwd
within OMC.
"""
expression = ('omcpath_cwd := cd(); '
f'omcpath_check := cd("{pathstr}"); ' # check requested pathstring
'cd(omcpath_cwd)')

try:
result = self._session.sendExpression(command=expression, parsed=False)
result_parts = result.split('\n')
pathstr_resolved = result_parts[1]
pathstr_resolved = pathstr_resolved[1:-1] # remove quotes

omcpath_resolved = self._session.omcpath(pathstr_resolved)
except OMCSessionException as ex:
raise OMCSessionException(f"OMCPath resolve failed for {pathstr}!") from ex

if not omcpath_resolved.is_file() and not omcpath_resolved.is_dir():
raise OMCSessionException(f"OMCPath resolve failed for {pathstr} - path does not exist!")

return omcpath_resolved

def absolute(self) -> OMCPath:
"""
Resolve the path to an absolute path. This is done by calling resolve() as it is the best we can do
using OMC functions.
"""
return self.resolve(strict=True)

def exists(self) -> bool:
"""
Semi replacement for pathlib.Path.exists().
"""
return self.is_file() or self.is_dir()

def size(self) -> int:
"""
Get the size of the file in bytes - this is a extra function and the best we can do using OMC.
"""
if not self.is_file():
raise OMCSessionException(f"Path {self.as_posix()} is not a file!")

res = self._session.sendExpression(f'stat("{self.as_posix()}")')
if res[0]:
return int(res[1])

raise OMCSessionException(f"Error reading file size for path {self.as_posix()}!")


if sys.version_info < (3, 12):
warnings.warn(
message="Python < 3.12 - using a limited compatibility class as OMCPath replacement.",
category=DeprecationWarning,
stacklevel=1,
)

class OMCPathCompatibility(pathlib.PosixPath):

def size(self) -> int:
return self.stat().st_size

OMCPath = OMCPathCompatibility # noqa: F811

else:
OMCPath = OMCPathReal


class OMCSessionZMQ:

def __init__(
Expand Down Expand Up @@ -322,6 +485,42 @@ def __del__(self):

self.omc_zmq = None

def omcpath(self, *path) -> OMCPath:
"""
Create an OMCPath object based on the given path segments and the current OMC session.
"""

# fallback solution for Python < 3.12; a modified pathlib.Path object is used as OMCPath replacement
if sys.version_info < (3, 12):
# noinspection PyArgumentList
return OMCPath(*path)
else:
return OMCPath(*path, session=self)

def omcpath_tempdir(self) -> OMCPath:
"""
Get a temporary directory using OMC.
"""
names = [str(uuid.uuid4()) for _ in range(100)]

tempdir_str = self.sendExpression("getTempDirectoryPath()")
tempdir_base = self.omcpath(tempdir_str)
tempdir: Optional[OMCPath] = None
for name in names:
# create a unique temporary directory name
tempdir = tempdir_base / name

if tempdir.exists():
continue

tempdir.mkdir(parents=True, exist_ok=False)
break

if tempdir is None or not tempdir.is_dir():
raise OMCSessionException("Cannot create a temporary directory!")

return tempdir

def execute(self, command: str):
warnings.warn("This function is depreciated and will be removed in future versions; "
"please use sendExpression() instead", DeprecationWarning, stacklevel=2)
Expand Down
44 changes: 23 additions & 21 deletions tests/test_ModelicaSystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def test_setParameters():
mod = OMPython.ModelicaSystem(model_path + "BouncingBall.mo", "BouncingBall")

# method 1
mod.setParameters("e=1.234")
mod.setParameters("g=321.0")
mod.setParameters(pvals={"e": 1.234})
mod.setParameters(pvals={"g": 321.0})
assert mod.getParameters("e") == ["1.234"]
assert mod.getParameters("g") == ["321.0"]
assert mod.getParameters() == {
Expand All @@ -47,7 +47,7 @@ def test_setParameters():
mod.getParameters("thisParameterDoesNotExist")

# method 2
mod.setParameters(["e=21.3", "g=0.12"])
mod.setParameters(pvals={"e": 21.3, "g": 0.12})
assert mod.getParameters() == {
"e": "21.3",
"g": "0.12",
Expand All @@ -64,8 +64,8 @@ def test_setSimulationOptions():
mod = OMPython.ModelicaSystem(fileName=model_path + "BouncingBall.mo", modelName="BouncingBall")

# method 1
mod.setSimulationOptions("stopTime=1.234")
mod.setSimulationOptions("tolerance=1.1e-08")
mod.setSimulationOptions(simOptions={"stopTime": 1.234})
mod.setSimulationOptions(simOptions={"tolerance": 1.1e-08})
assert mod.getSimulationOptions("stopTime") == ["1.234"]
assert mod.getSimulationOptions("tolerance") == ["1.1e-08"]
assert mod.getSimulationOptions(["tolerance", "stopTime"]) == ["1.1e-08", "1.234"]
Expand All @@ -77,7 +77,7 @@ def test_setSimulationOptions():
mod.getSimulationOptions("thisOptionDoesNotExist")

# method 2
mod.setSimulationOptions(["stopTime=2.1", "tolerance=1.2e-08"])
mod.setSimulationOptions(simOptions={"stopTime": 2.1, "tolerance": "1.2e-08"})
d = mod.getSimulationOptions()
assert d["stopTime"] == "2.1"
assert d["tolerance"] == "1.2e-08"
Expand Down Expand Up @@ -105,7 +105,7 @@ def test_customBuildDirectory(tmp_path, model_firstorder):
tmpdir = tmp_path / "tmpdir1"
tmpdir.mkdir()
m = OMPython.ModelicaSystem(filePath, "M", customBuildDirectory=tmpdir)
assert m.getWorkDirectory().resolve() == tmpdir.resolve()
assert pathlib.Path(m.getWorkDirectory()).resolve() == tmpdir.resolve()
result_file = tmpdir / "a.mat"
assert not result_file.exists()
m.simulate(resultfile="a.mat")
Expand All @@ -119,7 +119,7 @@ def test_getSolutions(model_firstorder):
a = -1
tau = -1 / a
stopTime = 5*tau
mod.setSimulationOptions([f"stopTime={stopTime}", "stepSize=0.1", "tolerance=1e-8"])
mod.setSimulationOptions(simOptions={"stopTime": stopTime, "stepSize": 0.1, "tolerance": 1e-8})
mod.simulate()

x = mod.getSolutions("x")
Expand Down Expand Up @@ -298,7 +298,7 @@ def test_getters(tmp_path):
x0 = 1.0
x_analytical = -b/a + (x0 + b/a) * np.exp(a * stopTime)
dx_analytical = (x0 + b/a) * a * np.exp(a * stopTime)
mod.setSimulationOptions(f"stopTime={stopTime}")
mod.setSimulationOptions(simOptions={"stopTime": stopTime})
mod.simulate()

# getOutputs after simulate()
Expand Down Expand Up @@ -327,7 +327,7 @@ def test_getters(tmp_path):
mod.getContinuous("a") # a is a parameter

with pytest.raises(OMPython.ModelicaSystemError):
mod.setSimulationOptions("thisOptionDoesNotExist=3")
mod.setSimulationOptions(simOptions={"thisOptionDoesNotExist": 3})


def test_simulate_inputs(tmp_path):
Expand All @@ -345,7 +345,7 @@ def test_simulate_inputs(tmp_path):
""")
mod = OMPython.ModelicaSystem(fileName=model_file.as_posix(), modelName="M_input")

mod.setSimulationOptions("stopTime=1.0")
mod.setSimulationOptions(simOptions={"stopTime": 1.0})

# integrate zero (no setInputs call) - it should default to None -> 0
assert mod.getInputs() == {
Expand All @@ -357,20 +357,24 @@ def test_simulate_inputs(tmp_path):
assert np.isclose(y[-1], 0.0)

# integrate a constant
mod.setInputs("u1=2.5")
mod.setInputs(name={"u1": 2.5})
assert mod.getInputs() == {
"u1": [
(0.0, 2.5),
(1.0, 2.5),
],
"u2": None,
# u2 is set due to the call to simulate() above
"u2": [
(0.0, 0.0),
(1.0, 0.0),
],
}
mod.simulate()
y = mod.getSolutions("y")[0]
assert np.isclose(y[-1], 2.5)

# now let's integrate the sum of two ramps
mod.setInputs("u1=[(0.0, 0.0), (0.5, 2), (1.0, 0)]")
mod.setInputs(name={"u1": [(0.0, 0.0), (0.5, 2), (1.0, 0)]})
assert mod.getInputs("u1") == [[
(0.0, 0.0),
(0.5, 2.0),
Expand All @@ -383,19 +387,17 @@ def test_simulate_inputs(tmp_path):
# let's try some edge cases
# unmatched startTime
with pytest.raises(OMPython.ModelicaSystemError):
mod.setInputs("u1=[(-0.5, 0.0), (1.0, 1)]")
mod.setInputs(name={"u1": [(-0.5, 0.0), (1.0, 1)]})
mod.simulate()
# unmatched stopTime
with pytest.raises(OMPython.ModelicaSystemError):
mod.setInputs("u1=[(0.0, 0.0), (0.5, 1)]")
mod.setInputs(name={"u1": [(0.0, 0.0), (0.5, 1)]})
mod.simulate()

# Let's use both inputs, but each one with different number of of
# Let's use both inputs, but each one with different number of
# samples. This has an effect when generating the csv file.
mod.setInputs([
"u1=[(0.0, 0), (1.0, 1)]",
"u2=[(0.0, 0), (0.25, 0.5), (0.5, 1.0), (1.0, 0)]",
])
mod.setInputs(name={"u1": [(0.0, 0), (1.0, 1)],
"u2": [(0.0, 0), (0.25, 0.5), (0.5, 1.0), (1.0, 0)]})
csv_file = mod._createCSVData()
assert pathlib.Path(csv_file).read_text() == """time,u1,u2,end
0.0,0.0,0.0,0
Expand Down
Loading
Loading