Skip to content

Commit ed7a160

Browse files
committed
[ModelicaSystemCmd] update handling of simargs
1 parent 0b09234 commit ed7a160

1 file changed

Lines changed: 21 additions & 10 deletions

File tree

OMPython/ModelicaSystem.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,14 @@ def args_set(self, args: dict):
132132

133133
def run(self):
134134

135-
cmd = [self._exe_file.as_posix()] + [f"{key}={self._args[key]}" for key in self._args]
136-
self._run_cmd(cmd=cmd, timeout=self._timeout)
135+
cmdl = [self._exe_file.as_posix()]
136+
for key in self._args:
137+
if self._args[key] is None:
138+
cmdl.append(f"-{key}")
139+
else:
140+
cmdl.append(f"-{key}={self._args[key]}")
141+
142+
self._run_cmd(cmd=cmdl, timeout=self._timeout)
137143

138144
return True
139145

@@ -686,7 +692,7 @@ def simulate(self, resultfile: Optional[str] = None, simflags: Optional[str] = N
686692
>>> simulate()
687693
>>> simulate(resultfile="a.mat")
688694
>>> simulate(simflags="-noEventEmit -noRestart -override=e=0.3,g=10") # set runtime simulation flags
689-
>>> simulate(simargs={"-noEventEmit": None, "-noRestart": None, "-override": "e=0.3,g=10"}) # using simargs
695+
>>> simulate(simargs={"noEventEmit": None, "noRestart": None, "override": "e=0.3,g=10"}) # using simargs
690696
"""
691697

692698
om_cmd = ModelicaSystemCmd(cmdpath=pathlib.Path(self.tempdir), modelname=self.modelName, timeout=timeout)
@@ -699,7 +705,7 @@ def simulate(self, resultfile: Optional[str] = None, simflags: Optional[str] = N
699705
else:
700706
self.resultfile = (pathlib.Path(self.tempdir) / resultfile).as_posix()
701707
# always define the resultfile to use
702-
om_cmd.arg_set(key="-r", val=self.resultfile)
708+
om_cmd.arg_set(key="r", val=self.resultfile)
703709

704710
# allow runtime simulation flags from user input
705711
# TODO: merge into ModelicaSystemCmd?
@@ -710,6 +716,9 @@ def simulate(self, resultfile: Optional[str] = None, simflags: Optional[str] = N
710716

711717
args = [s for s in simflags.split(' ') if s]
712718
for arg in args:
719+
if arg[0] != '-':
720+
raise ModelicaSystemError(f"Invalid simulation flag: {arg}")
721+
arg = arg[1:]
713722
parts = arg.split('=')
714723
if len(parts) == 1:
715724
val = None
@@ -729,7 +738,7 @@ def simulate(self, resultfile: Optional[str] = None, simflags: Optional[str] = N
729738
for key, value in tmpdict.items():
730739
file.write(f"{key}={value}\n")
731740

732-
om_cmd.arg_set(key="-overrideFile", val=overrideFile.as_posix())
741+
om_cmd.arg_set(key="overrideFile", val=overrideFile.as_posix())
733742

734743
if self.inputFlag: # if model has input quantities
735744
for i in self.inputlist:
@@ -745,7 +754,7 @@ def simulate(self, resultfile: Optional[str] = None, simflags: Optional[str] = N
745754
raise ModelicaSystemError(f"stopTime not matched for Input {i}!")
746755
self.csvFile = self.createCSVData() # create csv file
747756

748-
om_cmd.arg_set(key="-csvInput", val=self.csvFile.as_posix())
757+
om_cmd.arg_set(key="csvInput", val=self.csvFile.as_posix())
749758

750759
self.simulationFlag = om_cmd.run()
751760

@@ -1070,7 +1079,7 @@ def linearize(self, lintime: Optional[float] = None, simflags: Optional[str] = N
10701079
lintime: Override linearOptions["stopTime"] value.
10711080
simflags: A string of extra command line flags for the model
10721081
binary. - depreciated in favor of simargs
1073-
simargs: A dict with command line flags and possible options
1082+
simargs: A dict with command line flags and possible options; example: "simargs={'csvInput': 'a.csv'}"
10741083
timeout: Possible timeout for the execution of OM.
10751084
10761085
Returns:
@@ -1097,7 +1106,7 @@ def linearize(self, lintime: Optional[float] = None, simflags: Optional[str] = N
10971106
for key, value in self.linearOptions.items():
10981107
file.write(f"{key}={value}\n")
10991108

1100-
om_cmd.arg_set(key="-overrideFile", val=overrideLinearFile.as_posix())
1109+
om_cmd.arg_set(key="overrideFile", val=overrideLinearFile.as_posix())
11011110

11021111
if self.inputFlag:
11031112
nameVal = self.getInputs()
@@ -1108,9 +1117,9 @@ def linearize(self, lintime: Optional[float] = None, simflags: Optional[str] = N
11081117
if l[0] < float(self.simulateOptions["startTime"]):
11091118
raise ModelicaSystemError('Input time value is less than simulation startTime')
11101119
self.csvFile = self.createCSVData()
1111-
om_cmd.arg_set(key="-csvInput", val=self.csvFile.as_posix())
1120+
om_cmd.arg_set(key="csvInput", val=self.csvFile.as_posix())
11121121

1113-
om_cmd.arg_set(key="-l", val=f"{lintime or self.linearOptions["stopTime"]}")
1122+
om_cmd.arg_set(key="l", val=f"{lintime or self.linearOptions["stopTime"]}")
11141123

11151124
# allow runtime simulation flags from user input
11161125
# TODO: merge into ModelicaSystemCmd?
@@ -1121,6 +1130,8 @@ def linearize(self, lintime: Optional[float] = None, simflags: Optional[str] = N
11211130

11221131
args = [s for s in simflags.split(' ') if s]
11231132
for arg in args:
1133+
if arg[0] != '-':
1134+
raise ModelicaSystemError(f"Invalid simulation flag: {arg}")
11241135
parts = arg.split('=')
11251136
if len(parts) == 1:
11261137
val = None

0 commit comments

Comments
 (0)