-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For #495
- Loading branch information
Showing
4 changed files
with
326 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
Oops, something went wrong.