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 9c4b426 commit 3dcd7e1
Show file tree
Hide file tree
Showing 4 changed files with 326 additions and 180 deletions.
83 changes: 83 additions & 0 deletions buildingspy/development/dymola_run.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
def _add_exception(return_dict, e, cmd):
import subprocess

return_dict['success'] = False

# if isinstance(e, subprocess.CalledProcessError):
# # Check if simulation terminated, and if so, get the error
# return_dict['stdout'] = e.output.decode("utf-8")
# output = return_dict['stdout']
# for line in output.split('\n'):
# if 'terminated' in line:
# # Found terminated string. Cut everything after the '|' character that OpenModelica writes.
# idx=line.rfind('|')
# msg=line[idx+1:].strip()
# # The solver terminated. Add this information to a custom exception message.
# return_dict['exception'] = f"'{' '.join(cmd)}' caused '{msg}'."
# pass

if not 'exception' in return_dict:
# Did not find 'terminated' in message, handle exception as usual
return_dict['exception'] = '{}: {}'.format(type(e).__name__, e)


def _run_process(return_dict, cmd, worDir, timeout):
import subprocess

output = subprocess.check_output(
cmd,
cwd = worDir,
timeout=timeout,
stderr=subprocess.STDOUT,
shell=False)

return_dict['success'] = True
if 'stdout' in return_dict:
return_dict['stdout'] += output.decode("utf-8")
else:
return_dict['stdout'] = output.decode("utf-8")
return

def _simulate(model, timeout):
import os
import subprocess

worDir = "{{ working_directory }}"
return_dict = {}

try:
cmd = {{ cmd }}
return_dict['cmd'] = ' '.join(cmd)
output = _run_process(return_dict, cmd, worDir, timeout)

except Exception as e:
_add_exception(return_dict, e, cmd)
return return_dict

def run():
import os
import json
import traceback
import sys

timeout = {{ time_out }}
model = "{{ model }}"
result = {"model": model,
"working_directory": "{{ working_directory }}",
"simulation": {"success": False}}

# Log file
log_file = "{}_buildingspy.json".format(model.replace(".", "_"))
try:
os.remove(log_file)
except OSError:
pass

# Simulate model
result["simulation"] = _simulate(model, timeout)

with open(log_file, "w") as log:
log.write("{}\n".format(json.dumps(result, indent=4, sort_keys=False)) )

if __name__=="__main__":
run()
11 changes: 11 additions & 0 deletions buildingspy/development/dymola_run_all.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% for model in models_underscore %}
def run_{{ model }}():
import {{ model }} as m
m.run()
{% endfor %}


if __name__=="__main__":
{% for model in models_underscore %}
run_{{ model }}()
{% endfor %}
Loading

0 comments on commit 3dcd7e1

Please sign in to comment.