Skip to content

Commit bc3448b

Browse files
committed
Added option to specify timeout
For #495
1 parent 8211ad0 commit bc3448b

File tree

1 file changed

+85
-79
lines changed

1 file changed

+85
-79
lines changed

buildingspy/development/regressiontest.py

+85-79
Original file line numberDiff line numberDiff line change
@@ -1201,8 +1201,8 @@ def _verify_model_exists(model_name):
12011201

12021202
if self._modelica_tool == 'dymola':
12031203
for ent in self._data:
1204-
ent['dymola']['time_out'] = 10 #fixme 300
1205-
else: # Non-dymola
1204+
ent['dymola']['time_out'] = 10 # fixme 300
1205+
else: # Non-dymola
12061206
def_dic = {}
12071207
def_dic[self._modelica_tool] = {
12081208
'translate': True,
@@ -1290,7 +1290,6 @@ def _verify_model_exists(model_name):
12901290
if self._reporter.getNumberOfErrors() > 0:
12911291
raise ValueError(f"Wrong specification in {conf_file_name}.")
12921292

1293-
12941293
def _checkDataDictionary(self):
12951294
""" Check if the data used to run the regression tests do not have duplicate ``*.fmu`` files
12961295
and ``*.mat`` names.
@@ -2909,7 +2908,8 @@ def _checkSimulationError(self, errorFile):
29092908
for k, v in list(self._error_dict.get_dictionary().items()):
29102909
# For OPTIMICA, we neither have simulate nor FMUExport
29112910
if ele[key][k] > 0:
2912-
self._reporter.writeWarning(v["model_message"].format(ele[key]["command"]))
2911+
self._reporter.writeWarning(
2912+
v["model_message"].format(ele[key]["command"]))
29132913
self._error_dict.increment_counter(k)
29142914

29152915
if hasTranslationError and logFil is not None:
@@ -2921,8 +2921,10 @@ def _checkSimulationError(self, errorFile):
29212921
with open(logFil, "r") as f2:
29222922
f.write(f2.read())
29232923
else:
2924-
# Logfile does not exists, which may be because simulation was terminated due to time out
2925-
f.write(f"Log file {logFil} does not exist, this can happen if the process was terminated due to time out.")
2924+
# Logfile does not exists, which may be because simulation was terminated
2925+
# due to time out
2926+
f.write(
2927+
f"Log file {logFil} does not exist, this can happen if the process was terminated due to time out.")
29262928
f.write("\n\n\n")
29272929

29282930
if iChe > 0:
@@ -3101,7 +3103,6 @@ def _write_runscript_dymola(self, iPro, tra_data_pro):
31013103
for tra_data in tra_data_pro:
31023104
self._write_dymola_script(iPro, tra_data)
31033105

3104-
31053106
def _write_dymola_script(self, iPro, tra_data):
31063107
"""Create the run_modelName.mos script for the current model and for Dymola,
31073108
and return the number of generated regression tests.
@@ -3141,7 +3142,7 @@ def _print_end_of_json(isLastItem, fileHandle, logFileName):
31413142

31423143
statistics_log = f"{tra_data['model_name']}.statistics.log"
31433144
runFil = open(os.path.join(self._temDir[iPro], self.getLibraryName(),
3144-
f"run_{tra_data['model_name']}.mos"), mode="w", encoding="utf-8")
3145+
f"run_{tra_data['model_name']}.mos"), mode="w", encoding="utf-8")
31453146
runFil.write(
31463147
f"""// File autogenerated for process {iPro + 1} of {self._nPro}
31473148
// File created for execution by {self._modelica_tool}. Do not edit.
@@ -3185,12 +3186,12 @@ def _print_end_of_json(isLastItem, fileHandle, logFileName):
31853186
Modelica.Utilities.Streams.print("{\"testCase\" : [", "%s");
31863187
""" % statistics_log)
31873188

3188-
#if self._isPresentAndTrue(
3189+
# if self._isPresentAndTrue(
31893190
# 'translate',
31903191
# tra_data['dymola']) or self._isPresentAndTrue(
31913192
# 'exportFMU',
31923193
# tra_data['dymola']):
3193-
#nItem = nItem + 1
3194+
#nItem = nItem + 1
31943195

31953196
# iItem = 0
31963197
# Write unit tests for this process
@@ -3200,7 +3201,7 @@ def _print_end_of_json(isLastItem, fileHandle, logFileName):
32003201
tra_data['dymola']) or self._isPresentAndTrue(
32013202
'exportFMU',
32023203
tra_data['dymola']):
3203-
isLastItem = True # (iItem == nItem - 1)
3204+
isLastItem = True # (iItem == nItem - 1)
32043205
mosFilNam = os.path.join(self.getLibraryName(),
32053206
"Resources", "Scripts", "Dymola",
32063207
tra_data['ScriptFile'])
@@ -3564,7 +3565,6 @@ def _write_run_all_script(self, iPro, tra_data_pro):
35643565
txt += " import os;\n"
35653566
fil.write(txt)
35663567

3567-
35683568
def _write_python_runscripts(self, iPro, tra_data_pro):
35693569
""" Write the Python runfiles for all experiments in tra_data_pro.
35703570
@@ -3632,7 +3632,7 @@ def _write_python_runscripts(self, iPro, tra_data_pro):
36323632
lambda m: '[{}]'.format(m.group()),
36333633
re.sub(' ', '', x)) for x in result_variables]
36343634
)
3635-
else: # dymola
3635+
else: # dymola
36363636
# assemble command
36373637
cmd = list()
36383638
cmd.append(f"{self.getModelicaCommand()}")
@@ -3644,14 +3644,14 @@ def _write_python_runscripts(self, iPro, tra_data_pro):
36443644
model=model,
36453645
working_directory=directory,
36463646
library_name=self.getLibraryName(),
3647-
#ncp=dat[self._modelica_tool]['ncp'],
3648-
#rtol=dat[self._modelica_tool]['rtol'],
3649-
#solver=dat[self._modelica_tool]['solver'],
3650-
#start_time='mod.get_default_experiment_start_time()',
3651-
#final_time='mod.get_default_experiment_stop_time()',
3652-
#simulate=dat[self._modelica_tool]['simulate'],
3647+
# ncp=dat[self._modelica_tool]['ncp'],
3648+
# rtol=dat[self._modelica_tool]['rtol'],
3649+
# solver=dat[self._modelica_tool]['solver'],
3650+
# start_time='mod.get_default_experiment_start_time()',
3651+
# final_time='mod.get_default_experiment_stop_time()',
3652+
# simulate=dat[self._modelica_tool]['simulate'],
36533653
time_out=dat[self._modelica_tool]['time_out'],
3654-
cmd = cmd
3654+
cmd=cmd
36553655
)
36563656

36573657
file_name = os.path.join(directory, "{}.py".format(model.replace(".", "_")))
@@ -3747,8 +3747,10 @@ def _get(model, key, data):
37473747
temp['model'] = case['model']
37483748
temp['simulation'] = {}
37493749
temp['simulation']['elapsed_time'] = case['simulate']['elapsed_time'] if 'elapsed_time' in case['simulate'] else 0
3750-
temp['simulation']['start_time'] = case['simulate']['start_time'] if 'start_time' in case['simulate'] else _get(case['model'], 'startTime', self._data)
3751-
temp['simulation']['final_time'] = case['simulate']['final_time'] if 'final_time' in case['simulate'] else _get(case['model'], 'stopTime', self._data)
3750+
temp['simulation']['start_time'] = case['simulate']['start_time'] if 'start_time' in case['simulate'] else _get(
3751+
case['model'], 'startTime', self._data)
3752+
temp['simulation']['final_time'] = case['simulate']['final_time'] if 'final_time' in case['simulate'] else _get(
3753+
case['model'], 'stopTime', self._data)
37523754
temp['simulation']['jacobians'] = case['simulate']['jacobians'] if 'jacobians' in case['simulate'] else 0
37533755
temp['simulation']['state_events'] = case['simulate']['state_events'] if 'state_events' in case['simulate'] else 0
37543756
temp['simulation']['success'] = case['simulate']['result']
@@ -3841,9 +3843,9 @@ def run(self):
38413843
tem_dir = []
38423844
libNam = self.getLibraryName()
38433845
for di in self._temDir:
3844-
#if self._modelica_tool == 'dymola':
3846+
# if self._modelica_tool == 'dymola':
38453847
# tem_dir.append(os.path.join(di, libNam))
3846-
#else:
3848+
# else:
38473849
# tem_dir.append(di)
38483850
tem_dir.append(di)
38493851

@@ -3895,64 +3897,68 @@ def run(self):
38953897
stat = list()
38963898
for d in self._temDir:
38973899
for temLogFilNam in glob.glob(
3898-
os.path.join(d, self.getLibraryName(), '*.statistics.log')):
3899-
if os.path.exists(temLogFilNam):
3900-
with open(temLogFilNam.replace('Temp\tmp', 'Temp\\tmp'), mode="r", encoding="utf-8-sig") as temSta:
3901-
try:
3902-
jsonLog = json.load(temSta)
3903-
cas = jsonLog["testCase"]
3904-
# Iterate over all test cases of this output file
3905-
for ele in cas:
3906-
stat.append(ele)
3907-
except json.decoder.JSONDecodeError as e:
3908-
# If a run timed out, then temLogFilNam is not a valid json file
3909-
# because the file is written on the fly, and dymola did not finish
3910-
# writing all of it, which results in an invalid file.
3911-
# Check if /tmp/tmp-Buildings-1-o_m7nj7p/Buildings_Examples_VAVReheat_ASHRAE2006_buildingspy.json
3912-
# exists
3913-
modelName = os.path.split(temLogFilNam)[1].replace('.statistics.log', '')
3914-
buiLogNam = os.path.join(
3915-
d,
3916-
f"{modelName.replace('.', '_')}_buildingspy.json")
3917-
if os.path.exists(buiLogNam):
3918-
# Read the log file of the python script that invoked dymola
3919-
with open(buiLogNam, mode="r", encoding="utf-8-sig") as buiLog:
3920-
jsonBui = json.load(buiLog)
3921-
# Build up the entry for reporting the case
3922-
if "simulation" in jsonBui and "exception" in jsonBui["simulation"]:
3923-
exception = ''.join(jsonBui['simulation']['exception'])
3924-
else:
3925-
exception = f"JSONDecodeError in {temLogFilNam}: {str(e)}"
3926-
ele = {
3927-
"model": modelName,
3928-
"simulate": {
3929-
"command": ''.join(jsonBui['simulation']['cmd']),
3930-
"result": False,
3931-
"exception": exception
3932-
}
3933-
}
3934-
self._reporter.writeError(
3935-
f"Model '{modelName}' failed: {exception}")
3936-
stat.append(ele)
3937-
# Add the failure also to self._data so that _checkReferencePoints is not trying to read the output.
3938-
for ele in self._data:
3939-
if ele['model_name'] == modelName:
3940-
if "simulation" in ele[self._modelica_tool]:
3941-
ele[self._modelica_tool]['simulation']['success'] = False
3942-
else:
3943-
ele[self._modelica_tool]['simulation'] = {'success': False}
3944-
else:
3900+
os.path.join(d, self.getLibraryName(), '*.statistics.log')):
3901+
if os.path.exists(temLogFilNam):
3902+
with open(temLogFilNam.replace('Temp\tmp', 'Temp\\tmp'), mode="r", encoding="utf-8-sig") as temSta:
3903+
try:
3904+
jsonLog = json.load(temSta)
3905+
cas = jsonLog["testCase"]
3906+
# Iterate over all test cases of this output file
3907+
for ele in cas:
3908+
stat.append(ele)
3909+
except json.decoder.JSONDecodeError as e:
3910+
# If a run timed out, then temLogFilNam is not a valid json file
3911+
# because the file is written on the fly, and dymola did not finish
3912+
# writing all of it, which results in an invalid file.
3913+
# Check if /tmp/tmp-Buildings-1-o_m7nj7p/Buildings_Examples_VAVReheat_ASHRAE2006_buildingspy.json
3914+
# exists
3915+
modelName = os.path.split(temLogFilNam)[
3916+
1].replace('.statistics.log', '')
3917+
buiLogNam = os.path.join(
3918+
d,
3919+
f"{modelName.replace('.', '_')}_buildingspy.json")
3920+
if os.path.exists(buiLogNam):
3921+
# Read the log file of the python script that invoked dymola
3922+
with open(buiLogNam, mode="r", encoding="utf-8-sig") as buiLog:
3923+
jsonBui = json.load(buiLog)
3924+
# Build up the entry for reporting the case
3925+
if "simulation" in jsonBui and "exception" in jsonBui["simulation"]:
3926+
exception = ''.join(
3927+
jsonBui['simulation']['exception'])
3928+
else:
3929+
exception = f"JSONDecodeError in {temLogFilNam}: {str(e)}"
3930+
ele = {
3931+
"model": modelName,
3932+
"simulate": {
3933+
"command": ''.join(
3934+
jsonBui['simulation']['cmd']),
3935+
"result": False,
3936+
"exception": exception}}
39453937
self._reporter.writeError(
3946-
f"Decoding '{temLogFilNam}' failed and '{buiLogNam}' does not exist: {e}")
3947-
raise
3948-
except ValueError as e:
3938+
f"Model '{modelName}' failed: {exception}")
3939+
stat.append(ele)
3940+
# Add the failure also to self._data so that
3941+
# _checkReferencePoints is not trying to read the
3942+
# output.
3943+
for ele in self._data:
3944+
if ele['model_name'] == modelName:
3945+
if "simulation" in ele[self._modelica_tool]:
3946+
ele[self._modelica_tool]['simulation']['success'] = False
3947+
else:
3948+
ele[self._modelica_tool]['simulation'] = {
3949+
'success': False}
3950+
else:
39493951
self._reporter.writeError(
3950-
"Loading '%s' failed: %s" % (temLogFilNam, e))
3952+
f"Decoding '{temLogFilNam}' failed and '{buiLogNam}' does not exist: {e}")
39513953
raise
3952-
else:
3953-
self._reporter.writeError(
3954-
"Log file '" + temLogFilNam + "' does not exist.\n")
3955-
retVal = 1
3954+
except ValueError as e:
3955+
self._reporter.writeError(
3956+
"Loading '%s' failed: %s" % (temLogFilNam, e))
3957+
raise
3958+
else:
3959+
self._reporter.writeError(
3960+
"Log file '" + temLogFilNam + "' does not exist.\n")
3961+
retVal = 1
39563962
# Dump an array of testCase objects
39573963
# dump to a string first using json.dumps instead of json.dump
39583964
json_string = json.dumps({"testCase": stat},

0 commit comments

Comments
 (0)