Skip to content

[ModelicaSystem] split simulate() into two methods #311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 68 additions & 27 deletions OMPython/ModelicaSystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,40 +915,39 @@ 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[float] = 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.
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.
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
Parameters
----------
resultfile
simflags
simargs
timeout
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:
Expand Down Expand Up @@ -984,6 +983,48 @@ 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()
Expand Down
Loading