Skip to content

Commit

Permalink
Further edits for OpenModelica integration
Browse files Browse the repository at this point in the history
For #500
  • Loading branch information
mwetter committed Apr 1, 2023
1 parent 81fadfb commit 6bc87b2
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion buildingspy/development/optimica_run.template
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def process_with_timeout(target, timeout):
p.daemon = True
start = time.time()
p.start()
if timeout > 0:
if (timeout is not None) and (timeout > 0):
p.join(timeout)
else:
p.join()
Expand Down
8 changes: 3 additions & 5 deletions buildingspy/simulate/OpenModelica.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,6 @@ def _translate_and_simulate(self, simulate):

file_name = "{}.py".format(self.modelName.replace(".", "_"))
## self._time_stamp_old_files = datetime.datetime.now()
print(f"**** Writing openmodelica script {file_name}")
print(f"*** Working directory is {worDir}")
with open(os.path.join(worDir, file_name), mode="w", encoding="utf-8") as fil:
path_to_template = os.path.join(
os.path.dirname(__file__), os.path.pardir, "development")
Expand Down Expand Up @@ -323,18 +321,18 @@ def _check_simulation_errors(self, worDir, simulate):
steps = ['translation', 'simulation'] if simulate else ['translation']
for step in steps:
if step not in js:
msg = f"Failed to invoke {step} for model {self.modelName}. Check {logFil}."
msg = f"Failed to invoke {step} for model {self.modelName}. Check {path_to_logfile}."
self._reporter.writeError(msg)
raise RuntimeError(msg)
if js[step]['success'] is not True:
# Check if there was a timeout exception
if "exception" in js[step]:
if js[step]['exception'].find("Process time") > 0:
msg = f"The {step} of {self.modelName} failed due to timeout. Check {logFil}."
msg = f"The {step} of {self.modelName} failed due to timeout. Check {path_to_logfile}."
self._reporter.writeError(msg)
raise TimeoutError(msg)
# Raise a runtime error
msg = f"The {step} of {self.modelName} failed. Check {logFil}."
msg = f"The {step} of {self.modelName} failed. Check {path_to_logfile}."
self._reporter.writeError(msg)
raise RuntimeError(msg)
return
Expand Down
16 changes: 10 additions & 6 deletions buildingspy/simulate/base_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def __init__(
self.setTolerance(1E-6)
self.setNumberOfIntervals()
# self.setSolver("radau")
self.setTimeOut(-1)
self.setTimeOut(None)
self._MODELICA_EXE = None
self._reporter = reporter.Reporter(fileName=logFilNam)
self._showProgressBar = False
Expand Down Expand Up @@ -187,14 +187,19 @@ def setNumberOfIntervals(self, n=500):
return

def setTimeOut(self, sec):
"""Sets the time out after which the simulation will be killed.
"""Sets the time out in seconds after which the simulation will be killed.
:param sec: The time out after which the simulation will be killed.
The default value is -1, which means that the simulation will
never be killed.
The default value is `None`, which means that the simulation will
never be killed. A value of `None`, `0` or negative will never time out.
"""
if (sec is not None) and (sec > 0):
to = sec
else:
to = None
self._simulator_.update(timeout=sec)

return

def setResultFile(self, resultFile):
Expand Down Expand Up @@ -381,7 +386,6 @@ def _runSimulation(self, cmd, timeout, directory, env=None):
osEnv = self.prependToModelicaPath(osEnv, os.path.abspath("."))
else:
osEnv = self.prependToModelicaPath(osEnv, os.path.dirname(self._packagePath))
print(f"*** fixme: osEnv is equal to {osEnv}")

# Run command
try:
Expand All @@ -397,7 +401,7 @@ def _runSimulation(self, cmd, timeout, directory, env=None):
killedProcess = False
# Tailored implementation of a timeout mechanism as it is not available
# through `wait` or `communicate` methods in Python 2.
if timeout > 0:
if (timeout is not None): # if timeout is not None, it is always bigger than 0
while not timeout_exceeded and pro.poll() is None:
time.sleep(0.01)
elapsedTime = (datetime.datetime.now() - self._simulationStartTime).seconds
Expand Down
1 change: 1 addition & 0 deletions buildingspy/tests/test_simulate_Dymola.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ def _deleteDirs(cases):

p = Pool()
p.map(_simulate, cases)
p.close()

# Check output for success
for cas in cases:
Expand Down
3 changes: 2 additions & 1 deletion buildingspy/tests/test_simulate_OpenModelica.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def test_timeout(self, timeout=3):
with open(os.path.join(outDir, json_log_file)) as fh:
log = fh.read()
self.assertTrue('RuntimeError: Process timeout' in log)
s.setTimeOut(-1)
s.setTimeOut(None)
s.simulate()
with open(os.path.join(outDir, json_log_file)) as fh:
log = fh.read()
Expand All @@ -336,6 +336,7 @@ def _deleteDirs(cases):

p = Pool()
p.map(_simulate, cases)
p.close()

# Check output for success
for cas in cases:
Expand Down
1 change: 1 addition & 0 deletions buildingspy/tests/test_simulate_Optimica.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ def _deleteDirs(cases):

p = Pool()
p.map(_simulate, cases)
p.close()

# Check output for success
for cas in cases:
Expand Down

0 comments on commit 6bc87b2

Please sign in to comment.