Skip to content

Commit d26fffe

Browse files
committed
[ModelicaSystem] remove class variable csvFile; define name based on resultfile in simulate()
reason: * variable not needed / used as class variable * using name based on resultfile allows to run the same model executable several times
1 parent d585207 commit d26fffe

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

OMPython/ModelicaSystem.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,6 @@ def __init__(
419419
self.inputFlag = False # for model with input quantity
420420
self.simulationFlag = False # if the model is simulated?
421421
self.outputFlag = False
422-
self.csvFile: Optional[pathlib.Path] = None # for storing inputs condition
423422
self.resultfile: Optional[pathlib.Path] = None # for storing result file
424423
self.variableFilter = variableFilter
425424

@@ -910,6 +909,9 @@ def simulate(self, resultfile: Optional[str] = None, simflags: Optional[str] = N
910909
om_cmd.arg_set(key="overrideFile", val=overrideFile.as_posix())
911910

912911
if self.inputFlag: # if model has input quantities
912+
# csvfile is based on name used for result file
913+
csvfile = resultfile.parent / f"{resultfile.stem}.csv"
914+
913915
for i in self.inputlist:
914916
val = self.inputlist[i]
915917
if val is None:
@@ -921,9 +923,11 @@ def simulate(self, resultfile: Optional[str] = None, simflags: Optional[str] = N
921923
raise ModelicaSystemError(f"startTime not matched for Input {i}!")
922924
if float(self.simulateOptions["stopTime"]) != val[-1][0]:
923925
raise ModelicaSystemError(f"stopTime not matched for Input {i}!")
924-
self.csvFile = self.createCSVData() # create csv file
925926

926-
om_cmd.arg_set(key="csvInput", val=self.csvFile.as_posix())
927+
# write csv file and store the name
928+
csvfile = self.createCSVData(csvfile=csvfile)
929+
930+
om_cmd.arg_set(key="csvInput", val=csvfile.as_posix())
927931

928932
# delete resultfile ...
929933
if self.resultfile.is_file():
@@ -1144,7 +1148,11 @@ def checkValidInputs(self, name):
11441148
else:
11451149
raise ModelicaSystemError('Error!!! Value must be in tuple format')
11461150

1147-
def createCSVData(self) -> pathlib.Path:
1151+
def createCSVData(self, csvfile: Optional[pathlib.Path] = None) -> pathlib.Path:
1152+
"""
1153+
Create a csv file with inputs for the simulation/optimization of the model. If csvfile is provided as argument,
1154+
this file is used; else a generic file name is created.
1155+
"""
11481156
start_time: float = float(self.simulateOptions["startTime"])
11491157
stop_time: float = float(self.simulateOptions["stopTime"])
11501158

@@ -1185,13 +1193,14 @@ def createCSVData(self) -> pathlib.Path:
11851193
]
11861194
csv_rows.append(row)
11871195

1188-
csvFile = self.tempdir / f'{self.modelName}.csv'
1196+
if csvfile is None:
1197+
csvfile = self.tempdir / f'{self.modelName}.csv'
11891198

1190-
with open(file=csvFile, mode="w", encoding="utf-8", newline="") as fh:
1199+
with open(file=csvfile, mode="w", encoding="utf-8", newline="") as fh:
11911200
writer = csv.writer(fh)
11921201
writer.writerows(csv_rows)
11931202

1194-
return csvFile
1203+
return csvfile
11951204

11961205
# to convert Modelica model to FMU
11971206
def convertMo2Fmu(self, version="2.0", fmuType="me_cs", fileNamePrefix="<default>", includeResources=True): # 19
@@ -1307,8 +1316,8 @@ def load_module_from_path(module_name, file_path):
13071316
for l in tupleList:
13081317
if l[0] < float(self.simulateOptions["startTime"]):
13091318
raise ModelicaSystemError('Input time value is less than simulation startTime')
1310-
self.csvFile = self.createCSVData()
1311-
om_cmd.arg_set(key="csvInput", val=self.csvFile.as_posix())
1319+
csvfile = self.createCSVData()
1320+
om_cmd.arg_set(key="csvInput", val=csvfile.as_posix())
13121321

13131322
om_cmd.arg_set(key="l", val=str(lintime or self.linearOptions["stopTime"]))
13141323

0 commit comments

Comments
 (0)