We execute Python code from Dymola via the C/Python API. Therefore we used that tutorial. The tutorial works as shown, but when we combine Dymolas built-in time variable and Pythons Numpy library, we get a segfault.
Function in Dymola to call abitrary Python code:
function pyString
input String s;
output Real o;
external "C" o = pyRunString(s)
annotation(Include="
#include <Python.h>
double pyRunString(const char *str)
{
Py_Initialize();
PyRun_SimpleString(str);
Py_Finalize();
return 0.0;
}
");
end pyString;
When we call this function with following model it works in the simulation:
model CallPython
Real value;
equation
value = pyString("print(" + String(time) + ")");
end CallPython;
This model works in the simulation as well:
model CallPython
Real value;
equation
value = pyString("import numpy \nprint(" + String(0.0) + ")");
end CallPython;
But we get a segfault when we simulate this model:
model CallPython
Real value;
equation
value = pyString("import numpy \nprint(" + String(time) + ")");
end CallPython;
We are using Ubuntu20.04 with Dymola Version 2021 (64-bit), 2020-04-17, Python3.8 and Numpy 1.21.0
Does anybody know why this does not work or how we can fix it?