From 77ac2b871a940cf8de2cc04710aaa7cb6c30c9c7 Mon Sep 17 00:00:00 2001 From: syntron Date: Fri, 13 Jun 2025 13:13:51 +0200 Subject: [PATCH 1/4] [ModelicaSystem] split simulate() into two methods (1) create ModelicasystemCmd instance - simulate_cmd() (2) run it - simulate() --- OMPython/ModelicaSystem.py | 90 ++++++++++++++++++++++++++------------ 1 file changed, 63 insertions(+), 27 deletions(-) diff --git a/OMPython/ModelicaSystem.py b/OMPython/ModelicaSystem.py index 2f02b710..d85aea1b 100644 --- a/OMPython/ModelicaSystem.py +++ b/OMPython/ModelicaSystem.py @@ -915,40 +915,36 @@ def getOptimizationOptions(self, names: Optional[str | list[str]] = None) -> dic raise ModelicaSystemError("Unhandled input for getOptimizationOptions()") - def simulate(self, - resultfile: Optional[str] = None, - simflags: Optional[str] = None, - simargs: Optional[dict[str, Optional[str | dict[str, str]]]] = None, - timeout: Optional[float] = None) -> None: - """Simulate the model according to simulation options. + def simulate_cmd( + self, + resultfile: pathlib.Path, + simflags: Optional[str] = None, + simargs: Optional[dict[str, Optional[str | dict[str, str]]]] = None, + timeout: Optional[int] = None, + ) -> ModelicaSystemCmd: + """ + This method prepares the simulates model according to the simulation options. It returns an instance of + ModelicaSystemCmd which can be used to run the simulation. - See setSimulationOptions(). + Due to the tempdir being unique for the ModelicaSystem instance, *NEVER* use this to create several simulations + with the same instance of ModelicaSystem! Restart each simulation process with a new instance of ModelicaSystem. - Args: - resultfile: Path to a custom result file - simflags: String of extra command line flags for the model binary. - This argument is deprecated, use simargs instead. - simargs: Dict with simulation runtime flags. - timeout: Maximum execution time in seconds. + Parameters + ---------- + resultfile + simflags + simargs + timeout - Examples: - mod.simulate() - mod.simulate(resultfile="a.mat") - mod.simulate(simflags="-noEventEmit -noRestart -override=e=0.3,g=10") # set runtime simulation flags, deprecated - mod.simulate(simargs={"noEventEmit": None, "noRestart": None, "override": "override": {"e": 0.3, "g": 10}}) # using simargs + Returns + ------- + An instance if ModelicaSystemCmd to run the requested simulation. """ om_cmd = ModelicaSystemCmd(runpath=self._tempdir, modelname=self._model_name, timeout=timeout) - if resultfile is None: - # default result file generated by OM - self._result_file = self._tempdir / f"{self._model_name}_res.mat" - elif os.path.exists(resultfile): - self._result_file = pathlib.Path(resultfile) - else: - self._result_file = self._tempdir / resultfile - # always define the resultfile to use - om_cmd.arg_set(key="r", val=self._result_file.as_posix()) + # always define the result file to use + om_cmd.arg_set(key="r", val=resultfile.as_posix()) # allow runtime simulation flags from user input if simflags is not None: @@ -984,6 +980,46 @@ def simulate(self, om_cmd.arg_set(key="csvInput", val=self._csvFile.as_posix()) + return om_cmd + + def simulate(self, + resultfile: Optional[str] = None, + simflags: Optional[str] = None, + simargs: Optional[dict[str, Optional[str | dict[str, str]]]] = None, + timeout: Optional[float] = None) -> None: + """Simulate the model according to simulation options. + + See setSimulationOptions(). + + Args: + resultfile: Path to a custom result file + simflags: String of extra command line flags for the model binary. + This argument is deprecated, use simargs instead. + simargs: Dict with simulation runtime flags. + timeout: Maximum execution time in seconds. + + Examples: + mod.simulate() + mod.simulate(resultfile="a.mat") + mod.simulate(simflags="-noEventEmit -noRestart -override=e=0.3,g=10") # set runtime simulation flags, deprecated + mod.simulate(simargs={"noEventEmit": None, "noRestart": None, "override": "override": {"e": 0.3, "g": 10}}) # using simargs + """ + + if resultfile is None: + # default result file generated by OM + self._result_file = self._tempdir / f"{self._model_name}_res.mat" + elif os.path.exists(resultfile): + self._result_file = pathlib.Path(resultfile) + else: + self._result_file = self._tempdir / resultfile + + om_cmd = self.simulate_cmd( + resultfile=self._result_file, + simflags=simflags, + simargs=simargs, + timeout=timeout, + ) + # delete resultfile ... if self._result_file.is_file(): self._result_file.unlink() From f1080946a3c6747c9bc72cbc93c5ad9c79737e20 Mon Sep 17 00:00:00 2001 From: syntron Date: Mon, 23 Jun 2025 21:28:24 +0200 Subject: [PATCH 2/4] [ModelicaSystem] improve docstring for simulate_cmd() --- OMPython/ModelicaSystem.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/OMPython/ModelicaSystem.py b/OMPython/ModelicaSystem.py index d85aea1b..6af2edcb 100644 --- a/OMPython/ModelicaSystem.py +++ b/OMPython/ModelicaSystem.py @@ -929,6 +929,9 @@ def simulate_cmd( Due to the tempdir being unique for the ModelicaSystem instance, *NEVER* use this to create several simulations with the same instance of ModelicaSystem! Restart each simulation process with a new instance of ModelicaSystem. + However, if only non-structural parameters are used, it is possible to reuse an existing instance of + ModelicaSystem to create several version ModelicaSystemCmd to run the model using different settings. + Parameters ---------- resultfile From 03048179ed47be0a17f4d904fdebefbc571d348a Mon Sep 17 00:00:00 2001 From: syntron Date: Mon, 7 Jul 2025 19:20:34 +0200 Subject: [PATCH 3/4] [ModelicaSystem.simulate] fix header definition --- OMPython/ModelicaSystem.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/OMPython/ModelicaSystem.py b/OMPython/ModelicaSystem.py index 6af2edcb..cc810096 100644 --- a/OMPython/ModelicaSystem.py +++ b/OMPython/ModelicaSystem.py @@ -985,11 +985,13 @@ def simulate_cmd( return om_cmd - def simulate(self, - resultfile: Optional[str] = None, - simflags: Optional[str] = None, - simargs: Optional[dict[str, Optional[str | dict[str, str]]]] = None, - timeout: Optional[float] = None) -> None: + def simulate( + self, + resultfile: Optional[str] = None, + simflags: Optional[str] = None, + simargs: Optional[dict[str, Optional[str | dict[str, str]]]] = None, + timeout: Optional[float] = None, + ) -> None: """Simulate the model according to simulation options. See setSimulationOptions(). From 47ec1f261760cbdb3dc69d304b7a24ec149bcbf9 Mon Sep 17 00:00:00 2001 From: syntron Date: Mon, 7 Jul 2025 19:21:17 +0200 Subject: [PATCH 4/4] [ModelicaSystem.simulate_cmd] fix type hints --- OMPython/ModelicaSystem.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OMPython/ModelicaSystem.py b/OMPython/ModelicaSystem.py index cc810096..6b7e356b 100644 --- a/OMPython/ModelicaSystem.py +++ b/OMPython/ModelicaSystem.py @@ -920,7 +920,7 @@ def simulate_cmd( resultfile: pathlib.Path, simflags: Optional[str] = None, simargs: Optional[dict[str, Optional[str | dict[str, str]]]] = None, - timeout: Optional[int] = None, + timeout: Optional[float] = None, ) -> ModelicaSystemCmd: """ This method prepares the simulates model according to the simulation options. It returns an instance of