When I use OMPython to run a model using the ModelicaSystem object the output files where stored in my user directory instead of a temporary directory which I can easily delete. I would like to set the directory in which this files are storing which I think is the working directory (not sure about that).
This is my first attempt, but it didn't work:
from OMPython import OMCSessionZMQ
omc = OMCSessionZMQ()
model_path=omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels/"
simulation_path=omc.sendExpression("getTempDirectoryPath()")+"OMtmp/"
from OMPython import ModelicaSystem
mod=ModelicaSystem(model_path+"BouncingBall.mo","BouncingBall")
mod.currDir=simulation_path
mod.simulate()
My second attemp, but I can't delete the temporary directory:
import os
import errno
import shutil
from OMPython import OMCSessionZMQ
from OMPython import ModelicaSystem
omc = OMCSessionZMQ()
# Create temporary directory
simulation_path=omc.sendExpression("getTempDirectoryPath()")+"OMtmp/"
home_path=os.getcwd()
try:
os.mkdir(simulation_path)
except OSError as e:
if e.errno != errno.EEXIST:
raise
os.chdir(simulation_path)
print("Working directory->"+os.getcwd())
# Loading the model and simulating
model_path=omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels/"
mod=ModelicaSystem(model_path+"BouncingBall.mo","BouncingBall")
mod.simulate()
res=mod.getSolutions(["time","h"])
print("The heigth is "+str(res[1,-1])+" m)
#Changing the working directory to the original one
os.chdir(home_path)
# We could delete files and dierctory using: shutil.rmtree(simulation_path)
# But don't work. Plan b, delete al de files in the directory
for files in os.listdir(simulation_path):
path = os.path.join(simulation_path, files)
try:
shutil.rmtree(path)
except OSError:
os.remove(path)
# Still don't work: os.rmdir(simulation_path)