|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Runner class for a pre-compiled model binary. It does not use OMC at all. |
| 4 | +""" |
| 5 | +import logging |
| 6 | +import os |
| 7 | +from typing import Optional |
| 8 | + |
| 9 | +# Import base class and exception |
| 10 | +from OMPython.ModelicaSystemBase import ( |
| 11 | + ModelicaSystemBase, |
| 12 | + ModelicaSystemError, |
| 13 | +) |
| 14 | +from OMPython.OMCSession import ( |
| 15 | + OMSessionBase, |
| 16 | + OMSessionRunner, |
| 17 | +) |
| 18 | + |
| 19 | +OMCSession = OMSessionBase |
| 20 | +OMCSessionDummy = OMSessionRunner |
| 21 | + |
| 22 | +# define logger using the current module name as ID |
| 23 | +logger = logging.getLogger(__name__) |
| 24 | + |
| 25 | + |
| 26 | +class ModelicaSystemRunner(ModelicaSystemBase): |
| 27 | + """ |
| 28 | + Class to simulate a Modelica model using a pre-compiled model binary. |
| 29 | + """ |
| 30 | + |
| 31 | + def __init__( |
| 32 | + self, |
| 33 | + work_directory: Optional[str | os.PathLike] = None, |
| 34 | + session: Optional[OMSessionBase] = None, |
| 35 | + ) -> None: |
| 36 | + if session is None: |
| 37 | + session = OMCSessionDummy() |
| 38 | + |
| 39 | + if not isinstance(session, OMCSessionDummy): |
| 40 | + raise ModelicaSystemError("Only working if OMCsessionDummy is used!") |
| 41 | + |
| 42 | + super().__init__( |
| 43 | + work_directory=work_directory, |
| 44 | + session=session, |
| 45 | + ) |
| 46 | + |
| 47 | + def setup( |
| 48 | + self, |
| 49 | + model_name: Optional[str] = None, |
| 50 | + variable_filter: Optional[str] = None, |
| 51 | + ) -> None: |
| 52 | + """ |
| 53 | + Needed definitions to set up the runner class. This class expects the model (defined by model_name) to exists |
| 54 | + within the working directory. At least two files are needed: |
| 55 | +
|
| 56 | + * model executable (as '<model_name>' or '<model_name>.exe'; in case of Windows additional '<model_name>.bat' |
| 57 | + is expected to evaluate the path to needed dlls |
| 58 | + * the model initialization file (as '<model_name>_init.xml') |
| 59 | + """ |
| 60 | + |
| 61 | + if self._model_name is not None: |
| 62 | + raise ModelicaSystemError("Can not reuse this instance of ModelicaSystem " |
| 63 | + f"defined for {repr(self._model_name)}!") |
| 64 | + |
| 65 | + if model_name is None or not isinstance(model_name, str): |
| 66 | + raise ModelicaSystemError("A model name must be provided!") |
| 67 | + |
| 68 | + # set variables |
| 69 | + self._model_name = model_name # Model class name |
| 70 | + self._variable_filter = variable_filter |
| 71 | + |
| 72 | + # test if the model can be executed |
| 73 | + self.check_model_executable() |
| 74 | + |
| 75 | + # read XML file |
| 76 | + xml_file = self._session.omcpath(self.getWorkDirectory()) / f"{self._model_name}_init.xml" |
| 77 | + self._xmlparse(xml_file=xml_file) |
0 commit comments