-1

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)
Alberto
  • 1
  • 1

1 Answers1

0

You could use tempfile to create a tmp directory and switch to it using python os.chdir function.

Something like that :

import os
import tempfile
from OMPython import OMCSessionZMQ
from OMPython import ModelicaSystem

# Create temporary directory in /tmp
tmpDir = tempfile.mkdtemp(prefix="/tmp/")
os.chdir(tmpDir)
print(f"Working in {tmpDir}")

omc = OMCSessionZMQ()
model_path=omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels/"
mod=ModelicaSystem(model_path+"BouncingBall.mo","BouncingBall")
mod.simulate()

You can create your temporary directory elsewhere, create a directory without using tempfile, remove it at the end of the code, etc ...

Also I saw this issue https://github.com/OpenModelica/OMPython/issues/34 talking about it.

Paul
  • 305
  • 1
  • 7
  • For some reason that I do not understand, this code does not work for me. However, that idea works. However I can't delete the directory because it says there is another process using it. Any idea how to delete the directory as well? – Alberto Jul 05 '23 at 10:13
  • I was trying the code on Linux, if you are on Windows that might be the reason. I'm not really sure about the error. You could open an other question if you can't find ! – Paul Jul 07 '23 at 10:09