Skip to content

Conversation

@syntron
Copy link
Contributor

@syntron syntron commented Oct 30, 2024

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
Copy link

CLAassistant commented Oct 30, 2024

CLA assistant check
All committers have signed the CLA.

* difference between win and linux is only in the definition of my_env
…ction

* merge all calls to executeables into one function - _run_cmd()
* simplify & cleanup the code
@adeas31
Copy link
Member

adeas31 commented Oct 31, 2024

Please provide the example model that doesn't work.

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

@adeas31 adeas31 requested review from adeas31 and arun3688 October 31, 2024 09:12
@syntron
Copy link
Contributor Author

syntron commented Nov 4, 2024

Please provide the example model that doesn't work.

The compiler generates a <model_name>.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 = 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 ...

@adeas31
Copy link
Member

adeas31 commented Nov 4, 2024

Please provide the example model that doesn't work.
The compiler generates a <model_name>.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
Copy link
Collaborator

arun3688 commented Nov 4, 2024

@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
Copy link
Member

adeas31 commented Nov 4, 2024

@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
Copy link
Collaborator

arun3688 commented Nov 4, 2024

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
Copy link
Contributor Author

syntron commented Nov 4, 2024

@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
Copy link
Contributor Author

syntron commented Nov 4, 2024

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
Copy link
Contributor Author

syntron commented Nov 4, 2024

From [arun3688](#226 (comment)):

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 #226 (comment):

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 arun3688 merged commit 2001229 into OpenModelica:master Nov 7, 2024
4 checks passed
@arun3688
Copy link
Collaborator

arun3688 commented Nov 7, 2024

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

@adeas31
Copy link
Member

adeas31 commented Nov 7, 2024

@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
Copy link
Collaborator

arun3688 commented Nov 7, 2024

@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
Copy link
Contributor Author

syntron commented Nov 7, 2024

@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
Copy link
Contributor Author

syntron commented Nov 8, 2024

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!

@syntron syntron deleted the OMPython_syntron branch April 15, 2025 19:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants