|
38 | 38 | import logging |
39 | 39 | import numbers |
40 | 40 | import os |
| 41 | +import pathlib |
41 | 42 | import queue |
42 | 43 | import textwrap |
43 | 44 | import threading |
@@ -322,6 +323,10 @@ def parse_simflags(simflags: str) -> dict[str, Optional[str | dict[str, Any] | n |
322 | 323 |
|
323 | 324 |
|
324 | 325 | class ModelicaSystem: |
| 326 | + """ |
| 327 | + Class to simulate a Modelica model using OpenModelica via OMCSessionZMQ. |
| 328 | + """ |
| 329 | + |
325 | 330 | def __init__( |
326 | 331 | self, |
327 | 332 | commandLineOptions: Optional[list[str]] = None, |
@@ -446,18 +451,30 @@ def model( |
446 | 451 | # set variables |
447 | 452 | self._model_name = name # Model class name |
448 | 453 | self._libraries = libraries # may be needed if model is derived from other model |
449 | | - if file is not None: |
450 | | - file_name = self._session.omcpath(file).resolve() |
451 | | - else: |
452 | | - file_name = None |
453 | | - self._file_name = file_name # Model file/package name |
454 | 454 | self._variable_filter = variable_filter |
455 | 455 |
|
456 | | - if self._file_name is not None and not self._file_name.is_file(): # if file does not exist |
457 | | - raise IOError(f"{self._file_name} does not exist!") |
458 | | - |
459 | 456 | if self._libraries: |
460 | 457 | self._loadLibrary(libraries=self._libraries) |
| 458 | + |
| 459 | + self._file_name = None |
| 460 | + if file is not None: |
| 461 | + file_path = pathlib.Path(file) |
| 462 | + # special handling for OMCProcessLocal - consider a relative path |
| 463 | + if isinstance(self._session.omc_process, OMCProcessLocal) and not file_path.is_absolute(): |
| 464 | + file_path = pathlib.Path.cwd() / file_path |
| 465 | + if not file_path.is_file(): |
| 466 | + raise IOError(f"Model file {file_path} does not exist!") |
| 467 | + |
| 468 | + self._file_name = self.getWorkDirectory() / file_path.name |
| 469 | + if (isinstance(self._session.omc_process, OMCProcessLocal) |
| 470 | + and file_path.as_posix() == self._file_name.as_posix()): |
| 471 | + pass |
| 472 | + elif self._file_name.is_file(): |
| 473 | + raise IOError(f"Simulation model file {self._file_name} exist - not overwriting!") |
| 474 | + else: |
| 475 | + content = file_path.read_text(encoding='utf-8') |
| 476 | + self._file_name.write_text(content) |
| 477 | + |
461 | 478 | if self._file_name is not None: |
462 | 479 | self._loadFile(fileName=self._file_name) |
463 | 480 |
|
@@ -1685,7 +1702,7 @@ def convertFmu2Mo( |
1685 | 1702 | raise ModelicaSystemError(f"Missing FMU file: {fmu_path.as_posix()}") |
1686 | 1703 |
|
1687 | 1704 | filename = self._requestApi(apiName='importFMU', entity=fmu_path.as_posix()) |
1688 | | - filepath = self._work_dir / filename |
| 1705 | + filepath = self.getWorkDirectory() / filename |
1689 | 1706 |
|
1690 | 1707 | # report proper error message |
1691 | 1708 | if not filepath.is_file(): |
@@ -2037,7 +2054,7 @@ def prepare(self) -> int: |
2037 | 2054 |
|
2038 | 2055 | pk_value = pc_structure[idx_structure] |
2039 | 2056 | if isinstance(pk_value, str): |
2040 | | - pk_value_str = pk_value.replace('"', '\\"') |
| 2057 | + pk_value_str = self.session().escape_str(pk_value) |
2041 | 2058 | expression = f"setParameterValue({self._model_name}, {pk_structure}, \"{pk_value_str}\")" |
2042 | 2059 | elif isinstance(pk_value, bool): |
2043 | 2060 | pk_value_bool_str = "true" if pk_value else "false" |
|
0 commit comments