Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

Use python inside model

Use python inside model

Hi,

I see that a lot of questions regarding python have been asked before, but I have not seen a suitable solution to what I want to do.

I want a python script inside my model that does one calculation. I want to send two variables from inside the OM model to the python script, then return one calculated value and use this further in the model.

OMPython seems to be more about simulating OM-models from python, which is not what I want to do. "Calling external Python Code from a Modelica model" from the documentation does not seem to work as well.

If anyone has a working way to do this or knows of any examples or methods on this, it will be greatly appreciated.

Thanks.

Re: Use python inside model

Re: Use python inside model

Thank you adrpo!

Please excuse me as I am new to this, but I am having some issues with getting it to work.

I tried changing the path to the .dll to :
Py.PythonLibraryHandle pyHandle = Py.PythonLibraryHandle("C:\Users\*Name*\AppData\Local\Programs\Python\Python310\python310.dll");
for both the Test and the Examples.

When I try running it with just simulate (with the extra C compiler flag) in OMEdit the following error message is given:

In file included from OMCallPython.Examples.Test_includes.h:4:0,
                 from OMCallPython.Examples.Test_functions.c:7:
C:/Users/.../OMCallPython/Resources/C-Sources/OMCallPython.h:21:143: fatal error: Python.h: No such file or directory
compilation terminated.
<builtin>: recipe for target 'OMCallPython.Examples.Test_functions.o' failed
mingw32-make: *** [OMCallPython.Examples.Test_functions.o] Error 1
mingw32-make: *** Waiting for unfinished jobs....
mingw32-make: Leaving directory 'C:/Users/*Name*/AppData/Local/Temp/OPENMO~1/OMEdit/OMCALL~1.TES'
Compilation process failed. Exited with code 2.

By going into the OMCallPython header file and changing the line:
#include <Python.h> /* assume Python.h is in $MODELICAUSERCFLAGS which points to the place where Python.h is via -I/path/to/python/include/ */
into
#include <C:\Users\*Name*\AppData\Local\Programs\Python\Python310\include\Python.h>

no errors are thrown, but I still can't see any output.

The changes I made might be wrong, or running it from OMEdit might also be wrong.

Do you have any suggestions to make it work?
Thanks again!

Re: Use python inside model

I would not include with the full path in the OMCallPython.h file as I don't think that would work.
But, you could give it a try with forward slashes:
#include <C:/Users/*Name*/AppData/Local/Programs/Python/Python310/include/Python.h>


I now added an image on how to set the compiler flags before compiling.
https://github.com/adrpo/OMCallPython.git
You could try to add there:
-I"C:/Users/*Name*/AppData/Local/Programs/Python/Python310/include/"
and then just include <Python.h>

Also, if you have special characters in the path to the Python.h that might be problematic. Basically if you user contains special chars åöä or so.

Another alternative is to add your include path to the Modelica code, basically change in OMCallPython/package.mo everywhere:

Code:


IncludeDirectory = "modelica://OMCallPython/Resources/C-Sources"

to:

Code:


IncludeDirectory =
  {"modelica://OMCallPython/Resources/C-Sources",
   "C:/Users/*Name*/AppData/Local/Programs/Python/Python310/include/"}

Re: Use python inside model

Thank you again adrpo.

Still it does not work for me.

I changed the OMCallPython.h file back to just:
#include <Python.h>

and tried the C/C++ Compiler Flags:
-IC:/Users/*Name*/AppData/Local/Programs/Python/Python310/include/

It now compiles and simulates without error but there is still no output.

Under equation I tried:
Py.run(pyHandle, "print(1234)");


Sending a string gave me the following error:

    print('Hello world from Python
          ^
SyntaxError: unterminated string literal (detected at line 1)
  File "<string>", line 1
    print('Hello world from Python
          ^

Thanks.

Re: Use python inside model

Seems like I had to add fflush to show the printouts. See the changes:
https://github.com/adrpo/OMCallPython.git

What do you want to send from Modelica to Python and back?

Re: Use python inside model

It works like a charm now! Much appreciate!

My goal is to send floats during simulation from Modelica to the Python function, use a python-library to perform a calculation with the floats, and return the calculation to further use this in the model.

Do you know if this is even possible? Might be troublesome to input and extract values between Modelica and Python.

Thanks.

Re: Use python inside model

For sure it would be possible to do something like described here:
https://stackoverflow.com/questions/328 … turn-value
I will give it a try next week if I have time but it should be easy to do.

Re: Use python inside model

A small update/continuation of questions on this thread.

I am still having some issues with extracting the calculated values from Python and save them as variables in Modelica.

I am able to import Python libraries and functions and use them in Modelica, with sending in Modelica variables to Python functions. All thanks to Adrpo’s Github repository (https://github.com/adrpo/OMCallPython).

I am, however, still not able to extract the calculated values and save them again as Modelica variables. The nicest way would be something like:

Code:

String x_string = String(Py.run(pyHandle, "func(" + String(x) + ")")); 

But this obviously does not work since Py.run is not defined with any output. I am quite new to C++, but I am guessing this has to do with OMCallPython.h:

Code:

omc_PyRun_SimpleString = (fnptr_omc_PyRun_SimpleString)AddressOf(omc_PythonDLL, "PyRun_SimpleString");

Which probably needs to be changed.

Since Python can print to the output console, I experimented with trying to extract the printed values from the output log file in real-time, but this seems like an unsafe and troublesome way of handling this.

If anyone has any thoughts on extracting the values from python back to Modelica variables with the use of OMCallPython, it would be appreciated!  current/big_smile

Re: Use python inside model

I am working on a new package OMCallPythonAdvanced which will send two variables to a Python function and get one back as an example.
Hopefully you will have it later this week. I will send you a message here when is available.

There are 0 guests and 0 other users also viewing this topic