JModelica中未正确调用C函数

时间:2018-11-09 17:02:45

标签: modelica fmi jmodelica

我有一个Modelica模型:

model test
  Real x;
  Real y (start=10);

function try
    input Real x;
    output Real y; 
    external "C" testC(x,y)
    annotation(Include="#include <test.c>");
end try;

function round
    input Real u;
    input Real accuracy;
    output Real y;
algorithm
    y :=if (u > 0) then floor(u/accuracy + 0.5)*accuracy else         ceil(u/accuracy - 0.5)*accuracy;
end round;

algorithm
  x:=round(time, 60);
  when time>=pre(y) then
      y:=try(x);
  end when;
end test;

c代码也如下所示:

int testC(double x, double* y)
{
   puts("run ex");
   *y=x+30;
}

上面的代码在Dymola中运行良好,但是当我在JModelica中运行它时,我遇到了一个问题:

当在周期[0,200]中模拟此模型时,我希望c函数将被调用4次:t = 10,30,90,150。但是我在Jmodelica中发现,c函数实际上被调用了24次!

对解释上述问题的任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

仅需进行一些小的更正和改进,例如使其成为空函数。

model Test
  Real x;
  discrete Real y(start=10, fixed=true);

function try
    input Real x;
    output Real y; 
    external "C" testC(x,y)
    annotation(Include="
void testC(double x, double* y)
{
   *y=x+30;
}");
end try;

function round
    input Real u;
    input Real accuracy;
    output Real y;
algorithm
    y :=if (u > 0) then floor(u/accuracy + 0.5)*accuracy else ceil(u/accuracy - 0.5)*accuracy;
end round;

algorithm
  x:=round(time, 60);
  when time>=pre(y) then
      y:=try(x);
  end when;
annotation(experiment(StopTime=200));
end Test;

顺便说一句,与FMI无关。