【问题标题】:c function is not correctly called in JModelica在 JModelica 中未正确调用 c 函数
【发布时间】:2018-11-09 17:02:45
【问题描述】:

我有一个 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次!

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

【问题讨论】:

    标签: modelica fmi jmodelica


    【解决方案1】:

    只是一些小的更正和改进,例如,使其成为 void 函数。

    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 无关。

    【讨论】:

    • 谢谢,我对模型应用了修改,但问题似乎仍然存在。
    • 对了,你能解释一下为什么这个修改可以解决问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    相关资源
    最近更新 更多