Repository navigation
Simulation with Resultfile or Flags Fail to execute #206
Description
Description
When calling
OMPython.simulaute(resultfile='/home/user/modelica/testfile0.mat')
with a result file (and most likely also with other flags, the following log message is shown
Failed to simulate simulation: [Errno 2] No such file or directory: '/tmp/tmp37u5_obx/Package.Simulation -r=/home/user/modelica/testfile0.mat'
Steps to reproduce
Minimal Example that fails
from os.path import expanduser
from OMPython import ModelicaSystem
home_dir = expanduser("~")
model_path = home_dir + '/modelica/resultfilebug/'
model_file_name = 'BouncingBall.mo'
model_name = 'BouncingBall'
mod = ModelicaSystem(model_path + model_file_name, model_name )
mod.buildModel()
result_file_path = home_dir + '/modelica/resultfilebug/resultfiles/'
result_file_name = 'resultfile_0.mat'
mod.simulate(resultfile=result_file_path+result_file_name)
Throws followingexception:
Exception has occurred: FileNotFoundError (note: full exception trace is shown but execution is paused at: _run_module_as_main)
[Errno 2] No such file or directory: '/tmp/tmpj5k_1p6k/BouncingBall -r=~/modelica/resultfilebug/resultfiles/resultfile_0.mat'
Even though the file exists in my file system
ls /tmp/tmpqmbz5d57/BouncingBall
/tmp/tmpqmbz5d57/BouncingBall
Running
user@machine:~$ /tmp/tmpqmbz5d57/BouncingBall -r=/home/user/modelica/resultfilebug/resultfiles/resultfile_0.mat
LOG_ASSERT | debug | simulation_input_xml.c: Error: can not read file BouncingBall_init.xml as setup file to the generated simulation code.
Execution failed!
While
user@machine:/tmp/tmpq5rla9at$ ./BouncingBall -r=/home/user/modelica/resultfilebug/resultfiles/resultfile_0.mat
LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
LOG_SUCCESS | info | The simulation finished successfully.
Expected behavior
The model should just build and return the following log messages
LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
LOG_SUCCESS | info | The simulation finished successfully.
Screenshots
If applicable, add screenshots to help explain your problem.
Version and OS
- Python Version: Python 3.10.12
- OMPython Version: 3.5.1
- OpenModelica Version: 1.22.3
- OS: Windows 11 64 bit - Code run from WSL :Ubuntu 22.04.4 LTS
Additional context
When digging into the problem and debugging I came across this:
The error message suggests that the subprocess is unable to find the executable file /tmp/tmpqmbz5d57/BouncingBall -r=/home/user/modelica/testfile0.mat
The issue here is that the provided cmd is being interpreted as a single command with spaces in its name. Instead, you should separate the executable path and the command-line arguments into separate elements of the cmd list.
So the problem is created within
p = subprocess.Popen(cmd, env=my_env)
A solution to the problem is how cmd is created. If it is a list with
- getExeFile
- override
- csvinput
- r
- simflags
as seperate list entries, it works with my system configuration
Activity
NilsDressler commentedon Apr 19, 2024
I also found a working solution that made the code work for my system.
Before I make a pull request I'm wondering the following.
What format are the simFlags expected to be ?
A long string containing the flags like
mod.simulate(simflags='-flag1 -flag2 -...')or a list containing the flags as sting
mod.simulate(simflags=['-flag1', '-flag2' '-...'])I couldn't really find an answer to that in the documentation.
I can make a solution that works for both cases, but that might be unnecessary complicated if just on of them is a valid input.
fixes issue OpenModelica#206
InigoGastesi commentedon Apr 23, 2024
Hi, I have the same problem, I have found out that OMPython creates a command like this
But doing the following test works nice
So I suppose that the format of simFlags are like this
mod.simulate(simflags=['-flag1', '-flag2' '-...'])j-emils commentedon Apr 23, 2024
I have the same problem.
NilsDressler commentedon Apr 23, 2024
@InigoGastesi
Yes this was also the solution i found and how i fixed i in the commit i made.
If you give the subprocess a python list containing the path to the executable at index 0 and then all the flags in the subsequent indices, it works. Compared to the current implementation you only have to remove the initial blank space from the flags.
InigoGastesi commentedon Apr 24, 2024
@NilsDressler I using your commit now in my project, thank you
arun3688 commentedon Apr 24, 2024
@NilsDressler the issue is fixed with this commit fa56e30 and thanks for @NilsDressler for fixing the issue
InigoGastesi commentedon Apr 24, 2024
Hi @arun3688, I think that this issue is not fixed in that commit. In that commit the only think that is fixed is the problem with the subprocess
arun3688 commentedon Apr 24, 2024
@InigoGastesi Yes that is true, but still the issue fixes the subprocess problem in linux.
@NilsDressler It is not allowed to provide result file path in the
simulate()API, you can only provide the name for the result file and the result file will always be generated in the temp directory. see the docs here https://openmodelica.org/doc/OpenModelicaUsersGuide/1.22/ompython.html#simulationThe model can be simulated using the simulate API in the following ways,
An example of how to use the simflags is also provided in the documentation
InigoGastesi commentedon Apr 24, 2024
@arun3688 I'm checking your last comment and in the following code
I have this error
I checked it and the file exists. Also, I tried do the same without .mat extension and the same problem appears. As you can see in the print there is only filename
arun3688 commentedon Apr 24, 2024
@InigoGastesi what happens if you directly use
mod.simulate(resultfile="voltage_torqueVariable_size_50.mat")InigoGastesi commentedon Apr 24, 2024
Same error :(
arun3688 commentedon Apr 24, 2024
@InigoGastesi strange what happens if you just run
mod.simulate()5 remaining items
arun3688 commentedon Apr 26, 2024
@InigoGastesi @NilsDressler I can reproduce the issue, I will fix it today.
arun3688 commentedon Apr 26, 2024
@NilsDressler @InigoGastesi the issue is fixed with this commit 69a502e
Actually it is possible to provide result file path in the as said in the documentation
simulate(resultfile="C:/resultFile/test.mat")API, see the example below, update your package usingpython -m pip install -U https://github.com/OpenModelica/OMPython/archive/master.zipAn example usage is
Usage
casella commentedon May 2, 2024
@NilsDressler please check with the nightly build and close the ticket if it works for you. Thanks!
j-emils commentedon May 6, 2024
I'm still experiencing issues with this:
arun3688 commentedon May 6, 2024
@j-emils I can see that you are directly using the subprocess to execute the executable and the error message says it cannot find the executable in that location, also i see you are not using
ModelicaSystemconstructor, It would be good if you can provide a minimal example to reproduce the issue, I suggest you to change to the executable directory before using the subprocess commandj-emils commentedon May 6, 2024
@arun3688, we are using the ModelicaSystem constructor, it's just that we have created a wrapper around it. Maybe there's a dependency on the OpenModelica version? It's failing with version 1.22.0.
arun3688 commentedon May 6, 2024
@j-emils please uninstall your OMPython package and install it with the latest master,
pip uninstall OMPythonand then
pip install git+https://github.com/OpenModelica/OMPython.git/@masterj-emils commentedon May 6, 2024
@arun3688, I'm still getting the same error. If I change the call with subprocess to use a list instead of a string it works, analogous to the proposal from @NilsDressler . Will try to create an example for you to test.
NilsDressler commentedon May 6, 2024
@casella If the latest nightly referred to 891528c then it still does not work for my minimal example.
I will make a pull request for the solution provided by me
casella commentedon May 7, 2024
@arun3688 can you please follow this up? Thanks!
arun3688 commentedon May 7, 2024
@casella the issue is fixed with the this commit #214
casella commentedon May 7, 2024
Great, thanks @arun3688!