OpenModelica / OMPython

A Python interface to OpenModelica communicating via CORBA or ZeroMQ
Other
107 stars 58 forks source link

Om python syntron #226

Closed syntron closed 1 week ago

syntron commented 2 weeks ago

Related Issues

Use OMPython with models which include external libraries (with compiled C code). Currently, the dll files are not found if these libraries are added via the lmodel option.

Purpose

Allow building such models.

Approach

Adding the path based on the settings in lmodel to the environment variable.

Additionally, some cleanup of the code to execute a (Modelica) command is included (=> _run_cmd()).

CLAassistant commented 2 weeks ago

CLA assistant check
All committers have signed the CLA.

adeas31 commented 2 weeks ago

Please provide the example model that doesn't work.

The compiler generates a .bat file on Windows that contains list of all the dependencies. We should read from that file and set the process environment accordingly.

syntron commented 1 week ago

Please provide the example model that doesn't work.

The compiler generates a .bat file on Windows that contains list of all the dependencies. We should read from that file and set the process environment accordingly.

Thanks for reviewing the code!

Thus, your proposal would be to read the *.bat file and set the path based on the content in this file?

My change is based on the interpretation, that the path is build using the variable dllPath which is created within Python; thus, I could add the needed content directly at this point.

Some background: I try to use SDF tables within OpenModelica (https://github.com/ScientificDataFormat/SDF-Modelica). This works fine if I use the tool directly; however using OMPython I get no direct error - it just fails because the executable does not find the needed library (ModelicaSDF.dll).

The attached files are: (1) python test script - test_OMPython.py.txt (2) output using master (SHA1 = c6a204e5d6f5fe83dd3b746a403480ae2c82d09a) - syntron_OMPython-A.txt (3) output using the new commits - syntron_OMPython-B.txt

Besides the above listed point to run the model with the external library in OMPython, I also would like to create an FMU. However, this also fails and I expect a similar root cause ...

adeas31 commented 1 week ago

Please provide the example model that doesn't work. The compiler generates a .bat file on Windows that contains list of all the dependencies. We should read from that file and set the process environment accordingly.

Thanks for reviewing the code!

Thus, your proposal would be to read the *.bat file and set the path based on the content in this file?

Yes.

My change is based on the interpretation, that the path is build using the variable dllPath which is created within Python; thus, I could add the needed content directly at this point.

Some background: I try to use SDF tables within OpenModelica (https://github.com/ScientificDataFormat/SDF-Modelica). This works fine if I use the tool directly; however using OMPython I get no direct error - it just fails because the executable does not find the needed library (ModelicaSDF.dll).

I suppose you mean it works fine from OMEdit, right? OMEdit reads the batch file and sets the process path correctly. We need to do the same for OMPython.

The attached files are: (1) python test script - test_OMPython.py.txt (2) output using master (SHA1 = c6a204e) - syntron_OMPython-A.txt (3) output using the new commits - syntron_OMPython-B.txt

Besides the above listed point to run the model with the external library in OMPython, I also would like to create an FMU. However, this also fails and I expect a similar root cause ...

arun3688 commented 1 week ago

@adeas31 OMPython does use the .exe file to simulate the model, may be we should just change the API to use the .bat to simulate.

@syntron Can you provide your python script on how you call the API, that could help to solve the issue

adeas31 commented 1 week ago

@adeas31 OMPython does use the .exe file to simulate the model, may be we should just change the API to use the .bat to simulate.

OMPython ModelicaSystem calls buildModel and then runs the generated executable. buildModel will create the batch file which should be used to set the environment for executable.

arun3688 commented 1 week ago

OMPython ModelicaSystem calls buildModel and then runs the generated executable. buildModel will create the batch file which should be used to set the environment for executable.

sure i will change the simulate API to use the batch file to set up the environment

syntron commented 1 week ago

@syntron Can you provide your python script on how you call the API, that could help to solve the issue

The example script is attached to my comment above => see (1) python test script - test_OMPython.py.txt (rename from .py.txt to .py)

syntron commented 1 week ago

OMPython ModelicaSystem calls buildModel and then runs the generated executable. buildModel will create the batch file which should be used to set the environment for executable.

sure i will change the simulate API to use the batch file to set up the environment

OK; I'm ready to test the change.

Would the definition of _run_cmd() still be interesting? It would run for .exe and .bat files ...

syntron commented 1 week ago

From [arun3688](https://github.com/arun3688):

OMPython ModelicaSystem calls buildModel and then runs the generated executable. buildModel will create the batch file which should be used to set the environment for executable.

sure i will change the simulate API to use the batch file to set up the environment

From https://github.com/OpenModelica/OMPython/pull/226#issuecomment-2454435178:

Besides the above listed point to run the model with the external library in OMPython, I also would like to create an FMU. However, this also fails and I expect a similar root cause ...

Would something like this also help for FMU creation?

arun3688 commented 1 week ago

@syntron I have added some tests and the PR looks good thanks for the contribution

adeas31 commented 1 week ago

@arun3688 this was not supposed to be merged. The implementation is unnecessary. The correct way is to read the batch file and the set the process environment as I commented above.

arun3688 commented 1 week ago

@adeas31 Yes that is true but this is basically some refactoring of code to run the executable for simulation and linearization, i will do the batch file implementation in a separate PR

syntron commented 1 week ago

@syntron I have added some tests and the PR looks good thanks for the contribution

Thanks for the merge! I was first confused about the commit message - it is the meassage from the first of 3 commits in the pull request ...

Any hint where to look to get FMU compilation working for external libraries?

syntron commented 1 week ago

Any hint where to look to get FMU compilation working for external libraries?

I just tested it - with the development version 1.25 (exactly: OpenModelica-v1.25.0-dev-106-g982563026f-64bit.exe) it is working fine!