使用 Dymola 2017。
情况 A) 调用外部 c 脚本
我已经成功实现了没有“# include ”语句的简单外部 c 函数:
模型功能:
function chirp
input Modelica.SIunits.AngularVelocity w_start;
input Modelica.SIunits.AngularVelocity w_end;
input Real A;
input Real M;
input Real t;
output Real u "output signal";
external "C" u=chirp(w_start,w_end,A,M,t)
annotation(IncludeDirectory="modelica://ExternalFuncTest/Resources/Source/", Include="#include \"chirp.c\"");
end chirp;
C 脚本:
double chirp(double w1, double w2, double A, double M, double time)
{
double res;
res=A*cos(w1*time+(w2-w1)*time*time/(2*M));
return res;
}
情况 B) 调用 .dll 文件中的外部函数
我还成功地在编译的 dll 中调用外部函数:
模型功能:
function bessel_Jn
"Bessel function of the 1st kind (regular cylindrical) of order n"
extends Modelica.Icons.Function;
input Integer n;
input Real x;
output Real y;
external "C" y=gsl_sf_bessel_Jn(n,x) annotation(LibraryDirectory="modelica://ExternalFuncTest/Resources/Source/gsl-1.8/", Library="libgsl");
end bessel_Jn;
情况 C) 调用外部 C 脚本,该脚本通过标头使用外部 .dll 中的函数
我现在想做的是创建一个可以做更多有趣事情的 c 函数。我当前的方法是在引用编译的 dll(在本例中是 GNU 科学库的编译版本)的 c 函数中包含头文件。这个例子有标题(尽管它暂时没有做任何事情)。
模型功能:
function chirp
input Modelica.SIunits.AngularVelocity w_start;
input Modelica.SIunits.AngularVelocity w_end;
input Real A;
input Real M;
input Real t;
output Real u "output signal";
external "C" u=chirp(w_start,w_end,A,M,t)
annotation(LibraryDirectory="modelica://ExternalFuncTest/Resources/Source/gsl-1.8/", Library="libgsl",
IncludeDirectory="modelica://ExternalFuncTest/Resources/Source/", Include="#include \"chirp.c\"");
end chirp;
C-cript:
#include <gsl/gsl_sf_bessel.h> //<-- note the additional header
double chirp(double w1, double w2, double A, double M, double time)
{
double res;
res=A*cos(w1*time+(w2-w1)*time*time/(2*M));
return res;
}
当尝试调用上面的函数时,错误表明翻译失败,并且由于头文件的存在而没有其他错误。如果头文件被注释掉,该函数将按预期运行。
如果您对如何正确实施此功能有任何见解,请告诉我。谢谢。
供参考:下图是外部c脚本和.dll的路径。
Wrong Path: Note gsl header folder is within gsl-1.8 folder
Correct Path: Note gsl header folder is at the same level as gsl-1.8 folder
UPDATE:标头有效,但函数调用导致翻译失败
我已经更新了 c 脚本,现在调用一个应该由标头处理的函数。在目前的状态下,它不会起作用。也许它无法找到 .dll 文件,尽管它在 modelica 代码中指定了?我是否必须在 C 脚本中包含 load .dll 命令?
#include <gsl/gsl_sf_bessel.h>
double chirp(double w1, double w2, double A, double M, double time)
{
double res;
double y;
res=A*cos(w1*time+(w2-w1)*time*time/(2*M));
y = gsl_sf_bessel_j0(time); // <-- Calls a function from the .dll file using the header
return res;
}