Skip to content

Commit

Permalink
exit the interactive OMPython sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
adrpo committed Nov 21, 2024
1 parent 6e42432 commit 89504dc
Showing 1 changed file with 34 additions and 23 deletions.
57 changes: 34 additions & 23 deletions testmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,21 @@ def writeResult():

startJob=monotonic()

def writeResultAndExit(exitStatus, useOsExit=False):
def quit_omc(omc):
if omc is None:
return omc
try:
omc.sendExpression("quit()")
except:
pass
try:
del omc
except:
pass
omc = None
return omc

def writeResultAndExit(exitStatus, useOsExit=False, omc=None, omc_new=None):
writeResult()
print("Calling exit ...")
with open(errFile, 'a+') as fp:
Expand All @@ -65,6 +79,8 @@ def writeResultAndExit(exitStatus, useOsExit=False):
fp.write(msg % (exitStatus, monotonic()-startJob))
fp.flush()
sys.stdout.flush()
omc = quit_omc(omc)
omc_new = quit(omc_new)
if useOsExit:
os._exit(exitStatus)
else:
Expand Down Expand Up @@ -117,7 +133,7 @@ def target(res):
pass
with open(errFile, 'a+') as fp:
fp.write("Aborted the command.\n")
writeResultAndExit(0, True)
writeResultAndExit(0, True, omc, omc_new)
if res[1] is None:
res[1] = ""
if res[1] is not None:
Expand Down Expand Up @@ -311,7 +327,7 @@ def createOmcSessionNew():
def loadModels(omc, conf):
for f in conf["loadFiles"]:
if not sendExpressionTimeout(omc, 'loadFile("%s", uses=false)' % f, conf["ulimitLoadModel"]):
writeResultAndExit(0)
writeResultAndExit(0, False, omc, omc_new)
loadedFiles = sorted(omc.sendExpression("{getSourceFile(cl) for cl in getClassNames()}"))
if sorted(conf["loadFiles"]) != loadedFiles:
print("Loaded the wrong files. Expected:\n%s\nActual:\n%s" % ("\n".join(sorted(conf["loadFiles"])), "\n".join(loadedFiles)))
Expand All @@ -333,7 +349,7 @@ def loadLibraryInNewOM():
execstat["parsing"]=monotonic()-start
with open(errFile, 'a+') as fp:
fp.write("Timeout error for cmd: %s\n%s"%(cmd,str(e)))
writeResultAndExit(0, True)
writeResultAndExit(0, True, omc, omc_new)
execstat["parsing"]=monotonic()-start

try:
Expand Down Expand Up @@ -399,7 +415,7 @@ def sendExpressionOldOrNew(cmd):
except:
pass

writeResultAndExit(0)
writeResultAndExit(0, omc, omc_new)

# See which translateModel phases completed

Expand All @@ -413,14 +429,7 @@ def sendExpressionOldOrNew(cmd):
frontend = omc.sendExpression("OpenModelica.Scripting.Internal.Time.timerTock(OpenModelica.Scripting.Internal.Time.RT_CLOCK_FRONTEND)")

writeResult()
try:
omc.sendExpression("quit()")
except:
pass
try:
del omc
except:
pass
omc = quit_omc(omc)

print(execTimeTranslateModel,frontend,backend)
if backend != -1:
Expand Down Expand Up @@ -452,7 +461,7 @@ def sendExpressionOldOrNew(cmd):
fp.write(err)

if execstat["phase"] < 4:
writeResultAndExit(0)
writeResultAndExit(0, False, omc, omc_new)

start=monotonic()
try:
Expand All @@ -463,7 +472,7 @@ def sendExpressionOldOrNew(cmd):
if not os.path.exists(os.path.normpath(fmuExpectedLocation)):
err += "\nFMU was not generated in the expected location: %s" % fmuExpectedLocation
execstat["phase"]=4
writeResultAndExit(0)
writeResultAndExit(0, False, omc, omc_new)
execstat["phase"] = 5
else:
if isWin:
Expand All @@ -477,7 +486,7 @@ def sendExpressionOldOrNew(cmd):
execstat["build"] = monotonic()-start
with open(errFile, 'a+') as fp:
fp.write(str(e))
writeResultAndExit(0, True)
writeResultAndExit(0, True, omc, omc_new)

writeResult()
# Do the simulation
Expand All @@ -493,7 +502,7 @@ def sendExpressionOldOrNew(cmd):
if not conf.get("fmisimulator"):
with open(simFile,"w") as fp:
fp.write("OMSimulator not available\n")
writeResultAndExit(0)
writeResultAndExit(0, False, omc, omc_new)
fmitmpdir = "temp_%s_fmu" % conf["fileName"].replace(".","_")
with open("%s.tmpfiles" % conf["fileName"], "a+") as fp:
fp.write("%s\n" % fmitmpdir)
Expand Down Expand Up @@ -527,20 +536,19 @@ def sendExpressionOldOrNew(cmd):
execstat["phase"] = 6
except TimeoutError as e:
execstat["sim"] = monotonic()-start
writeResultAndExit(0, True)
writeResultAndExit(0, True, omc, omc_new)

if referenceFile=="":
writeResultAndExit(0)
writeResultAndExit(0, False, omc, omc_new)
if len(referenceVars)==0:
execstat["diff"] = {"time":0.0, "vars":[], "numCompared":0}
execstat["phase"]=7
writeResultAndExit(0)
writeResultAndExit(0, False, omc, omc_new)

# Check the reference file...

prefix = os.path.abspath("../files/%s.diff" % conf["fileName"]).replace('\\','/')


if not os.path.exists(os.path.normpath(resFile)):
with open(errFile, 'a+') as fp:
fp.write("TODO: How the !@#!# did the simulation report success but simulation result %s does not exist to compare? outputFormat=%s" % (resFile,outputFormat))
Expand All @@ -551,7 +559,7 @@ def sendExpressionOldOrNew(cmd):
if not sendExpressionTimeout(omc_new, 'filterSimulationResults("%s", "updated%s", vars={%s}, removeDescription=false, hintReadAllVars=false)' % (resFile, resFile, ", ".join(['"%s"' % s for s in referenceVars])), conf["ulimitOmc"]):
with open(errFile, 'a+') as fp:
fp.write("Failed to filter simulation results. Took time: %.2f\n" % (monotonic()-start))
writeResultAndExit(0)
writeResultAndExit(0, False, omc, omc_new)
os.remove(resFile)
os.rename("updated" + resFile, resFile)
with open(errFile, 'a+') as fp:
Expand All @@ -563,7 +571,7 @@ def sendExpressionOldOrNew(cmd):
except TimeoutError as e:
with open(errFile, 'a+') as fp:
fp.write("Timeout error for diffSimulationResults")
writeResultAndExit(0)
writeResultAndExit(0, False, omc, omc_new)

execstat["diff"] = {"time":monotonic()-start, "vars":[], "numCompared":len(referenceVars)}
if len(diffVars)==0 and referenceOK:
Expand Down Expand Up @@ -635,4 +643,7 @@ def sendExpressionOldOrNew(cmd):
</body>
</html>""" % (tolerance, conf["reference_reltolDiffMinMax"], conf["reference_rangeDelta"], os.path.basename(prefix + "." + var + ".csv"), var))

# quit omc_new
omc_new = quit_omc(omc_new)

writeResultAndExit(0)

0 comments on commit 89504dc

Please sign in to comment.