- Index
- » Users
- » ben558
- » Profile
Posts
Posts
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!
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.
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.
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!
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.
Hi everyone,
I am kind of new to OpenModelica and am trying to recreate example 2/3 from chapter 14.1.6 : Hybrid vehicle minimum consumption, in the OpenModelicaUserGuide.
I am, however, unable to do so, and was wondering if anyone has the complete model working and would be so kind to share it.
Thanks
- Index
- » Users
- » ben558
- » Profile