Skip to content

Commit e3bb33d

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 b57bd0c commit e3bb33d

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
@@ -410,7 +410,6 @@ def __init__(
410410
self.inputFlag = False # for model with input quantity
411411
self.simulationFlag = False # if the model is simulated?
412412
self.outputFlag = False
413-
self.csvFile: Optional[pathlib.Path] = None # for storing inputs condition
414413
self.resultfile: Optional[pathlib.Path] = None # for storing result file
415414
self.variableFilter = variableFilter
416415

@@ -911,6 +910,9 @@ def simulate_cmd(
911910
om_cmd.arg_set(key="overrideFile", val=overrideFile.as_posix())
912911

913912
if self.inputFlag: # if model has input quantities
913+
# csvfile is based on name used for result file
914+
csvfile = resultfile.parent / f"{resultfile.stem}.csv"
915+
914916
for i in self.inputlist:
915917
val = self.inputlist[i]
916918
if val is None:
@@ -922,9 +924,11 @@ def simulate_cmd(
922924
raise ModelicaSystemError(f"startTime not matched for Input {i}!")
923925
if float(self.simulateOptions["stopTime"]) != val[-1][0]:
924926
raise ModelicaSystemError(f"stopTime not matched for Input {i}!")
925-
self.csvFile = self.createCSVData() # create csv file
926927

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

929933
return om_cmd
930934

@@ -1169,7 +1173,11 @@ def checkValidInputs(self, name):
11691173
else:
11701174
raise ModelicaSystemError('Error!!! Value must be in tuple format')
11711175

1172-
def createCSVData(self) -> pathlib.Path:
1176+
def createCSVData(self, csvfile: Optional[pathlib.Path] = None) -> pathlib.Path:
1177+
"""
1178+
Create a csv file with inputs for the simulation/optimization of the model. If csvfile is provided as argument,
1179+
this file is used; else a generic file name is created.
1180+
"""
11731181
start_time: float = float(self.simulateOptions["startTime"])
11741182
stop_time: float = float(self.simulateOptions["stopTime"])
11751183

@@ -1210,13 +1218,14 @@ def createCSVData(self) -> pathlib.Path:
12101218
]
12111219
csv_rows.append(row)
12121220

1213-
csvFile = self.tempdir / f'{self.modelName}.csv'
1221+
if csvfile is None:
1222+
csvfile = self.tempdir / f'{self.modelName}.csv'
12141223

1215-
with open(file=csvFile, mode="w", encoding="utf-8", newline="") as fh:
1224+
with open(file=csvfile, mode="w", encoding="utf-8", newline="") as fh:
12161225
writer = csv.writer(fh)
12171226
writer.writerows(csv_rows)
12181227

1219-
return csvFile
1228+
return csvfile
12201229

12211230
# to convert Modelica model to FMU
12221231
def convertMo2Fmu(self, version="2.0", fmuType="me_cs", fileNamePrefix="<default>", includeResources=True): # 19
@@ -1332,8 +1341,8 @@ def load_module_from_path(module_name, file_path):
13321341
for l in tupleList:
13331342
if l[0] < float(self.simulateOptions["startTime"]):
13341343
raise ModelicaSystemError('Input time value is less than simulation startTime')
1335-
self.csvFile = self.createCSVData()
1336-
om_cmd.arg_set(key="csvInput", val=self.csvFile.as_posix())
1344+
csvfile = self.createCSVData()
1345+
om_cmd.arg_set(key="csvInput", val=csvfile.as_posix())
13371346

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

0 commit comments

Comments
 (0)