Skip to content

Commit

Permalink
Added option to specify timeout
Browse files Browse the repository at this point in the history
For #495
  • Loading branch information
mwetter committed Nov 9, 2022
1 parent 8211ad0 commit bc3448b
Showing 1 changed file with 85 additions and 79 deletions.
164 changes: 85 additions & 79 deletions buildingspy/development/regressiontest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1201,8 +1201,8 @@ def _verify_model_exists(model_name):

if self._modelica_tool == 'dymola':
for ent in self._data:
ent['dymola']['time_out'] = 10 #fixme 300
else: # Non-dymola
ent['dymola']['time_out'] = 10 # fixme 300
else: # Non-dymola
def_dic = {}
def_dic[self._modelica_tool] = {
'translate': True,
Expand Down Expand Up @@ -1290,7 +1290,6 @@ def _verify_model_exists(model_name):
if self._reporter.getNumberOfErrors() > 0:
raise ValueError(f"Wrong specification in {conf_file_name}.")


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

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

if iChe > 0:
Expand Down Expand Up @@ -3101,7 +3103,6 @@ def _write_runscript_dymola(self, iPro, tra_data_pro):
for tra_data in tra_data_pro:
self._write_dymola_script(iPro, tra_data)


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

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

#if self._isPresentAndTrue(
# if self._isPresentAndTrue(
# 'translate',
# tra_data['dymola']) or self._isPresentAndTrue(
# 'exportFMU',
# tra_data['dymola']):
#nItem = nItem + 1
#nItem = nItem + 1

# iItem = 0
# Write unit tests for this process
Expand All @@ -3200,7 +3201,7 @@ def _print_end_of_json(isLastItem, fileHandle, logFileName):
tra_data['dymola']) or self._isPresentAndTrue(
'exportFMU',
tra_data['dymola']):
isLastItem = True # (iItem == nItem - 1)
isLastItem = True # (iItem == nItem - 1)
mosFilNam = os.path.join(self.getLibraryName(),
"Resources", "Scripts", "Dymola",
tra_data['ScriptFile'])
Expand Down Expand Up @@ -3564,7 +3565,6 @@ def _write_run_all_script(self, iPro, tra_data_pro):
txt += " import os;\n"
fil.write(txt)


def _write_python_runscripts(self, iPro, tra_data_pro):
""" Write the Python runfiles for all experiments in tra_data_pro.
Expand Down Expand Up @@ -3632,7 +3632,7 @@ def _write_python_runscripts(self, iPro, tra_data_pro):
lambda m: '[{}]'.format(m.group()),
re.sub(' ', '', x)) for x in result_variables]
)
else: # dymola
else: # dymola
# assemble command
cmd = list()
cmd.append(f"{self.getModelicaCommand()}")
Expand All @@ -3644,14 +3644,14 @@ def _write_python_runscripts(self, iPro, tra_data_pro):
model=model,
working_directory=directory,
library_name=self.getLibraryName(),
#ncp=dat[self._modelica_tool]['ncp'],
#rtol=dat[self._modelica_tool]['rtol'],
#solver=dat[self._modelica_tool]['solver'],
#start_time='mod.get_default_experiment_start_time()',
#final_time='mod.get_default_experiment_stop_time()',
#simulate=dat[self._modelica_tool]['simulate'],
# ncp=dat[self._modelica_tool]['ncp'],
# rtol=dat[self._modelica_tool]['rtol'],
# solver=dat[self._modelica_tool]['solver'],
# start_time='mod.get_default_experiment_start_time()',
# final_time='mod.get_default_experiment_stop_time()',
# simulate=dat[self._modelica_tool]['simulate'],
time_out=dat[self._modelica_tool]['time_out'],
cmd = cmd
cmd=cmd
)

file_name = os.path.join(directory, "{}.py".format(model.replace(".", "_")))
Expand Down Expand Up @@ -3747,8 +3747,10 @@ def _get(model, key, data):
temp['model'] = case['model']
temp['simulation'] = {}
temp['simulation']['elapsed_time'] = case['simulate']['elapsed_time'] if 'elapsed_time' in case['simulate'] else 0
temp['simulation']['start_time'] = case['simulate']['start_time'] if 'start_time' in case['simulate'] else _get(case['model'], 'startTime', self._data)
temp['simulation']['final_time'] = case['simulate']['final_time'] if 'final_time' in case['simulate'] else _get(case['model'], 'stopTime', self._data)
temp['simulation']['start_time'] = case['simulate']['start_time'] if 'start_time' in case['simulate'] else _get(
case['model'], 'startTime', self._data)
temp['simulation']['final_time'] = case['simulate']['final_time'] if 'final_time' in case['simulate'] else _get(
case['model'], 'stopTime', self._data)
temp['simulation']['jacobians'] = case['simulate']['jacobians'] if 'jacobians' in case['simulate'] else 0
temp['simulation']['state_events'] = case['simulate']['state_events'] if 'state_events' in case['simulate'] else 0
temp['simulation']['success'] = case['simulate']['result']
Expand Down Expand Up @@ -3841,9 +3843,9 @@ def run(self):
tem_dir = []
libNam = self.getLibraryName()
for di in self._temDir:
#if self._modelica_tool == 'dymola':
# if self._modelica_tool == 'dymola':
# tem_dir.append(os.path.join(di, libNam))
#else:
# else:
# tem_dir.append(di)
tem_dir.append(di)

Expand Down Expand Up @@ -3895,64 +3897,68 @@ def run(self):
stat = list()
for d in self._temDir:
for temLogFilNam in glob.glob(
os.path.join(d, self.getLibraryName(), '*.statistics.log')):
if os.path.exists(temLogFilNam):
with open(temLogFilNam.replace('Temp\tmp', 'Temp\\tmp'), mode="r", encoding="utf-8-sig") as temSta:
try:
jsonLog = json.load(temSta)
cas = jsonLog["testCase"]
# Iterate over all test cases of this output file
for ele in cas:
stat.append(ele)
except json.decoder.JSONDecodeError as e:
# If a run timed out, then temLogFilNam is not a valid json file
# because the file is written on the fly, and dymola did not finish
# writing all of it, which results in an invalid file.
# Check if /tmp/tmp-Buildings-1-o_m7nj7p/Buildings_Examples_VAVReheat_ASHRAE2006_buildingspy.json
# exists
modelName = os.path.split(temLogFilNam)[1].replace('.statistics.log', '')
buiLogNam = os.path.join(
d,
f"{modelName.replace('.', '_')}_buildingspy.json")
if os.path.exists(buiLogNam):
# Read the log file of the python script that invoked dymola
with open(buiLogNam, mode="r", encoding="utf-8-sig") as buiLog:
jsonBui = json.load(buiLog)
# Build up the entry for reporting the case
if "simulation" in jsonBui and "exception" in jsonBui["simulation"]:
exception = ''.join(jsonBui['simulation']['exception'])
else:
exception = f"JSONDecodeError in {temLogFilNam}: {str(e)}"
ele = {
"model": modelName,
"simulate": {
"command": ''.join(jsonBui['simulation']['cmd']),
"result": False,
"exception": exception
}
}
self._reporter.writeError(
f"Model '{modelName}' failed: {exception}")
stat.append(ele)
# Add the failure also to self._data so that _checkReferencePoints is not trying to read the output.
for ele in self._data:
if ele['model_name'] == modelName:
if "simulation" in ele[self._modelica_tool]:
ele[self._modelica_tool]['simulation']['success'] = False
else:
ele[self._modelica_tool]['simulation'] = {'success': False}
else:
os.path.join(d, self.getLibraryName(), '*.statistics.log')):
if os.path.exists(temLogFilNam):
with open(temLogFilNam.replace('Temp\tmp', 'Temp\\tmp'), mode="r", encoding="utf-8-sig") as temSta:
try:
jsonLog = json.load(temSta)
cas = jsonLog["testCase"]
# Iterate over all test cases of this output file
for ele in cas:
stat.append(ele)
except json.decoder.JSONDecodeError as e:
# If a run timed out, then temLogFilNam is not a valid json file
# because the file is written on the fly, and dymola did not finish
# writing all of it, which results in an invalid file.
# Check if /tmp/tmp-Buildings-1-o_m7nj7p/Buildings_Examples_VAVReheat_ASHRAE2006_buildingspy.json
# exists
modelName = os.path.split(temLogFilNam)[
1].replace('.statistics.log', '')
buiLogNam = os.path.join(
d,
f"{modelName.replace('.', '_')}_buildingspy.json")
if os.path.exists(buiLogNam):
# Read the log file of the python script that invoked dymola
with open(buiLogNam, mode="r", encoding="utf-8-sig") as buiLog:
jsonBui = json.load(buiLog)
# Build up the entry for reporting the case
if "simulation" in jsonBui and "exception" in jsonBui["simulation"]:
exception = ''.join(
jsonBui['simulation']['exception'])
else:
exception = f"JSONDecodeError in {temLogFilNam}: {str(e)}"
ele = {
"model": modelName,
"simulate": {
"command": ''.join(
jsonBui['simulation']['cmd']),
"result": False,
"exception": exception}}
self._reporter.writeError(
f"Decoding '{temLogFilNam}' failed and '{buiLogNam}' does not exist: {e}")
raise
except ValueError as e:
f"Model '{modelName}' failed: {exception}")
stat.append(ele)
# Add the failure also to self._data so that
# _checkReferencePoints is not trying to read the
# output.
for ele in self._data:
if ele['model_name'] == modelName:
if "simulation" in ele[self._modelica_tool]:
ele[self._modelica_tool]['simulation']['success'] = False
else:
ele[self._modelica_tool]['simulation'] = {
'success': False}
else:
self._reporter.writeError(
"Loading '%s' failed: %s" % (temLogFilNam, e))
f"Decoding '{temLogFilNam}' failed and '{buiLogNam}' does not exist: {e}")
raise
else:
self._reporter.writeError(
"Log file '" + temLogFilNam + "' does not exist.\n")
retVal = 1
except ValueError as e:
self._reporter.writeError(
"Loading '%s' failed: %s" % (temLogFilNam, e))
raise
else:
self._reporter.writeError(
"Log file '" + temLogFilNam + "' does not exist.\n")
retVal = 1
# Dump an array of testCase objects
# dump to a string first using json.dumps instead of json.dump
json_string = json.dumps({"testCase": stat},
Expand Down

0 comments on commit bc3448b

Please sign in to comment.