使用 time 和 numpy 时,带有 Python API 的外部 C 函数中的 Dymola Segfault

问题描述

我们通过 C/Python API 从 Dymola 执行 Python 代码。因此我们使用了 tutorial。本教程的工作原理如图所示,但是当我们结合 Dymolas 内置时间变量和 Pythons Numpy 库时,会出现段错误

Dymola 中调用任意 Python 代码函数

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;

当我们使用以下模型调用函数时,它在模拟中起作用:

model CallPython
  Real value;
equation 
  value = pyString("print(" + String(time) + ")");
end CallPython;

这个模型也适用于模拟:

model CallPython
  Real value;
equation 
  value = pyString("import numpy \nprint(" + String(0.0) + ")");
end CallPython;

但是当我们模拟这个模型时,我们得到了一个错误

model CallPython
  Real value;
equation 
  value = pyString("import numpy \nprint(" + String(time) + ")");
end CallPython;

Segfault from last model

我们使用 Ubuntu20.04 和 Dymola 2021 版(64 位)、2020-04-17、python3.8 和 Numpy 1.21.0

有人知道为什么这不起作用或我们如何解决吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)