Skip to content

Commit 797eb84

Browse files
syntronadeas31
andauthored
[ModelicaSystem] split simulate() into two methods (#311)
* [ModelicaSystem] split simulate() into two methods (1) create ModelicasystemCmd instance - simulate_cmd() (2) run it - simulate() * [ModelicaSystem] improve docstring for simulate_cmd() * [ModelicaSystem.simulate] fix header definition * [ModelicaSystem.simulate_cmd] fix type hints --------- Co-authored-by: Adeel Asghar <[email protected]>
1 parent 5f092db commit 797eb84

File tree

1 file changed

+68
-27
lines changed

1 file changed

+68
-27
lines changed

OMPython/ModelicaSystem.py

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -915,40 +915,39 @@ def getOptimizationOptions(self, names: Optional[str | list[str]] = None) -> dic
915915

916916
raise ModelicaSystemError("Unhandled input for getOptimizationOptions()")
917917

918-
def simulate(self,
919-
resultfile: Optional[str] = None,
920-
simflags: Optional[str] = None,
921-
simargs: Optional[dict[str, Optional[str | dict[str, str]]]] = None,
922-
timeout: Optional[float] = None) -> None:
923-
"""Simulate the model according to simulation options.
918+
def simulate_cmd(
919+
self,
920+
resultfile: pathlib.Path,
921+
simflags: Optional[str] = None,
922+
simargs: Optional[dict[str, Optional[str | dict[str, str]]]] = None,
923+
timeout: Optional[float] = None,
924+
) -> ModelicaSystemCmd:
925+
"""
926+
This method prepares the simulates model according to the simulation options. It returns an instance of
927+
ModelicaSystemCmd which can be used to run the simulation.
924928
925-
See setSimulationOptions().
929+
Due to the tempdir being unique for the ModelicaSystem instance, *NEVER* use this to create several simulations
930+
with the same instance of ModelicaSystem! Restart each simulation process with a new instance of ModelicaSystem.
926931
927-
Args:
928-
resultfile: Path to a custom result file
929-
simflags: String of extra command line flags for the model binary.
930-
This argument is deprecated, use simargs instead.
931-
simargs: Dict with simulation runtime flags.
932-
timeout: Maximum execution time in seconds.
932+
However, if only non-structural parameters are used, it is possible to reuse an existing instance of
933+
ModelicaSystem to create several version ModelicaSystemCmd to run the model using different settings.
933934
934-
Examples:
935-
mod.simulate()
936-
mod.simulate(resultfile="a.mat")
937-
mod.simulate(simflags="-noEventEmit -noRestart -override=e=0.3,g=10") # set runtime simulation flags, deprecated
938-
mod.simulate(simargs={"noEventEmit": None, "noRestart": None, "override": "override": {"e": 0.3, "g": 10}}) # using simargs
935+
Parameters
936+
----------
937+
resultfile
938+
simflags
939+
simargs
940+
timeout
941+
942+
Returns
943+
-------
944+
An instance if ModelicaSystemCmd to run the requested simulation.
939945
"""
940946

941947
om_cmd = ModelicaSystemCmd(runpath=self._tempdir, modelname=self._model_name, timeout=timeout)
942948

943-
if resultfile is None:
944-
# default result file generated by OM
945-
self._result_file = self._tempdir / f"{self._model_name}_res.mat"
946-
elif os.path.exists(resultfile):
947-
self._result_file = pathlib.Path(resultfile)
948-
else:
949-
self._result_file = self._tempdir / resultfile
950-
# always define the resultfile to use
951-
om_cmd.arg_set(key="r", val=self._result_file.as_posix())
949+
# always define the result file to use
950+
om_cmd.arg_set(key="r", val=resultfile.as_posix())
952951

953952
# allow runtime simulation flags from user input
954953
if simflags is not None:
@@ -984,6 +983,48 @@ def simulate(self,
984983

985984
om_cmd.arg_set(key="csvInput", val=self._csvFile.as_posix())
986985

986+
return om_cmd
987+
988+
def simulate(
989+
self,
990+
resultfile: Optional[str] = None,
991+
simflags: Optional[str] = None,
992+
simargs: Optional[dict[str, Optional[str | dict[str, str]]]] = None,
993+
timeout: Optional[float] = None,
994+
) -> None:
995+
"""Simulate the model according to simulation options.
996+
997+
See setSimulationOptions().
998+
999+
Args:
1000+
resultfile: Path to a custom result file
1001+
simflags: String of extra command line flags for the model binary.
1002+
This argument is deprecated, use simargs instead.
1003+
simargs: Dict with simulation runtime flags.
1004+
timeout: Maximum execution time in seconds.
1005+
1006+
Examples:
1007+
mod.simulate()
1008+
mod.simulate(resultfile="a.mat")
1009+
mod.simulate(simflags="-noEventEmit -noRestart -override=e=0.3,g=10") # set runtime simulation flags, deprecated
1010+
mod.simulate(simargs={"noEventEmit": None, "noRestart": None, "override": "override": {"e": 0.3, "g": 10}}) # using simargs
1011+
"""
1012+
1013+
if resultfile is None:
1014+
# default result file generated by OM
1015+
self._result_file = self._tempdir / f"{self._model_name}_res.mat"
1016+
elif os.path.exists(resultfile):
1017+
self._result_file = pathlib.Path(resultfile)
1018+
else:
1019+
self._result_file = self._tempdir / resultfile
1020+
1021+
om_cmd = self.simulate_cmd(
1022+
resultfile=self._result_file,
1023+
simflags=simflags,
1024+
simargs=simargs,
1025+
timeout=timeout,
1026+
)
1027+
9871028
# delete resultfile ...
9881029
if self._result_file.is_file():
9891030
self._result_file.unlink()

0 commit comments

Comments
 (0)