|
49 | 49 | import warnings
|
50 | 50 | import xml.etree.ElementTree as ET
|
51 | 51 |
|
52 |
| -from OMPython.OMCSession import OMCSessionException, OMCSessionZMQ |
| 52 | +from OMPython.OMCSession import OMCSessionException, OMCSessionZMQ, OMCProcessLocal |
53 | 53 |
|
54 | 54 | # define logger using the current module name as ID
|
55 | 55 | logger = logging.getLogger(__name__)
|
@@ -303,7 +303,7 @@ def __init__(
|
303 | 303 | variableFilter: Optional[str] = None,
|
304 | 304 | customBuildDirectory: Optional[str | os.PathLike | pathlib.Path] = None,
|
305 | 305 | omhome: Optional[str] = None,
|
306 |
| - session: Optional[OMCSessionZMQ] = None, |
| 306 | + omc_process: Optional[OMCProcessLocal] = None, |
307 | 307 | build: bool = True,
|
308 | 308 | ) -> None:
|
309 | 309 | """Initialize, load and build a model.
|
@@ -331,8 +331,8 @@ def __init__(
|
331 | 331 | directory will be created.
|
332 | 332 | omhome: OPENMODELICAHOME value to be used when creating the OMC
|
333 | 333 | session.
|
334 |
| - session: OMC session to be used. If unspecified, a new session |
335 |
| - will be created. |
| 334 | + omc_process: definition of a (local) OMC process to be used. If |
| 335 | + unspecified, a new local session will be created. |
336 | 336 | build: Boolean controlling whether or not the model should be
|
337 | 337 | built when constructor is called. If False, the constructor
|
338 | 338 | simply loads the model without compiling.
|
@@ -367,10 +367,10 @@ def __init__(
|
367 | 367 | self._linearized_outputs: list[str] = [] # linearization output list
|
368 | 368 | self._linearized_states: list[str] = [] # linearization states list
|
369 | 369 |
|
370 |
| - if session is not None: |
371 |
| - if not isinstance(session, OMCSessionZMQ): |
372 |
| - raise ModelicaSystemError("Invalid session data provided!") |
373 |
| - self._getconn = session |
| 370 | + if omc_process is not None: |
| 371 | + if not isinstance(omc_process, OMCProcessLocal): |
| 372 | + raise ModelicaSystemError("Invalid (local) omc process definition provided!") |
| 373 | + self._getconn = OMCSessionZMQ(omc_process=omc_process) |
374 | 374 | else:
|
375 | 375 | self._getconn = OMCSessionZMQ(omhome=omhome)
|
376 | 376 |
|
@@ -914,40 +914,39 @@ def getOptimizationOptions(self, names: Optional[str | list[str]] = None) -> dic
|
914 | 914 |
|
915 | 915 | raise ModelicaSystemError("Unhandled input for getOptimizationOptions()")
|
916 | 916 |
|
917 |
| - def simulate(self, |
918 |
| - resultfile: Optional[str] = None, |
919 |
| - simflags: Optional[str] = None, |
920 |
| - simargs: Optional[dict[str, Optional[str | dict[str, str]]]] = None, |
921 |
| - timeout: Optional[float] = None) -> None: |
922 |
| - """Simulate the model according to simulation options. |
| 917 | + def simulate_cmd( |
| 918 | + self, |
| 919 | + resultfile: pathlib.Path, |
| 920 | + simflags: Optional[str] = None, |
| 921 | + simargs: Optional[dict[str, Optional[str | dict[str, str]]]] = None, |
| 922 | + timeout: Optional[float] = None, |
| 923 | + ) -> ModelicaSystemCmd: |
| 924 | + """ |
| 925 | + This method prepares the simulates model according to the simulation options. It returns an instance of |
| 926 | + ModelicaSystemCmd which can be used to run the simulation. |
923 | 927 |
|
924 |
| - See setSimulationOptions(). |
| 928 | + Due to the tempdir being unique for the ModelicaSystem instance, *NEVER* use this to create several simulations |
| 929 | + with the same instance of ModelicaSystem! Restart each simulation process with a new instance of ModelicaSystem. |
925 | 930 |
|
926 |
| - Args: |
927 |
| - resultfile: Path to a custom result file |
928 |
| - simflags: String of extra command line flags for the model binary. |
929 |
| - This argument is deprecated, use simargs instead. |
930 |
| - simargs: Dict with simulation runtime flags. |
931 |
| - timeout: Maximum execution time in seconds. |
| 931 | + However, if only non-structural parameters are used, it is possible to reuse an existing instance of |
| 932 | + ModelicaSystem to create several version ModelicaSystemCmd to run the model using different settings. |
932 | 933 |
|
933 |
| - Examples: |
934 |
| - mod.simulate() |
935 |
| - mod.simulate(resultfile="a.mat") |
936 |
| - mod.simulate(simflags="-noEventEmit -noRestart -override=e=0.3,g=10") # set runtime simulation flags, deprecated |
937 |
| - mod.simulate(simargs={"noEventEmit": None, "noRestart": None, "override": "override": {"e": 0.3, "g": 10}}) # using simargs |
| 934 | + Parameters |
| 935 | + ---------- |
| 936 | + resultfile |
| 937 | + simflags |
| 938 | + simargs |
| 939 | + timeout |
| 940 | +
|
| 941 | + Returns |
| 942 | + ------- |
| 943 | + An instance if ModelicaSystemCmd to run the requested simulation. |
938 | 944 | """
|
939 | 945 |
|
940 | 946 | om_cmd = ModelicaSystemCmd(runpath=self._tempdir, modelname=self._model_name, timeout=timeout)
|
941 | 947 |
|
942 |
| - if resultfile is None: |
943 |
| - # default result file generated by OM |
944 |
| - self._result_file = self._tempdir / f"{self._model_name}_res.mat" |
945 |
| - elif os.path.exists(resultfile): |
946 |
| - self._result_file = pathlib.Path(resultfile) |
947 |
| - else: |
948 |
| - self._result_file = self._tempdir / resultfile |
949 |
| - # always define the resultfile to use |
950 |
| - om_cmd.arg_set(key="r", val=self._result_file.as_posix()) |
| 948 | + # always define the result file to use |
| 949 | + om_cmd.arg_set(key="r", val=resultfile.as_posix()) |
951 | 950 |
|
952 | 951 | # allow runtime simulation flags from user input
|
953 | 952 | if simflags is not None:
|
@@ -988,6 +987,48 @@ def simulate(self,
|
988 | 987 |
|
989 | 988 | om_cmd.arg_set(key="csvInput", val=csvfile.as_posix())
|
990 | 989 |
|
| 990 | + return om_cmd |
| 991 | + |
| 992 | + def simulate( |
| 993 | + self, |
| 994 | + resultfile: Optional[str] = None, |
| 995 | + simflags: Optional[str] = None, |
| 996 | + simargs: Optional[dict[str, Optional[str | dict[str, str]]]] = None, |
| 997 | + timeout: Optional[float] = None, |
| 998 | + ) -> None: |
| 999 | + """Simulate the model according to simulation options. |
| 1000 | +
|
| 1001 | + See setSimulationOptions(). |
| 1002 | +
|
| 1003 | + Args: |
| 1004 | + resultfile: Path to a custom result file |
| 1005 | + simflags: String of extra command line flags for the model binary. |
| 1006 | + This argument is deprecated, use simargs instead. |
| 1007 | + simargs: Dict with simulation runtime flags. |
| 1008 | + timeout: Maximum execution time in seconds. |
| 1009 | +
|
| 1010 | + Examples: |
| 1011 | + mod.simulate() |
| 1012 | + mod.simulate(resultfile="a.mat") |
| 1013 | + mod.simulate(simflags="-noEventEmit -noRestart -override=e=0.3,g=10") # set runtime simulation flags, deprecated |
| 1014 | + mod.simulate(simargs={"noEventEmit": None, "noRestart": None, "override": "override": {"e": 0.3, "g": 10}}) # using simargs |
| 1015 | + """ |
| 1016 | + |
| 1017 | + if resultfile is None: |
| 1018 | + # default result file generated by OM |
| 1019 | + self._result_file = self._tempdir / f"{self._model_name}_res.mat" |
| 1020 | + elif os.path.exists(resultfile): |
| 1021 | + self._result_file = pathlib.Path(resultfile) |
| 1022 | + else: |
| 1023 | + self._result_file = self._tempdir / resultfile |
| 1024 | + |
| 1025 | + om_cmd = self.simulate_cmd( |
| 1026 | + resultfile=self._result_file, |
| 1027 | + simflags=simflags, |
| 1028 | + simargs=simargs, |
| 1029 | + timeout=timeout, |
| 1030 | + ) |
| 1031 | + |
991 | 1032 | # delete resultfile ...
|
992 | 1033 | if self._result_file.is_file():
|
993 | 1034 | self._result_file.unlink()
|
|
0 commit comments