Функция c неправильно вызывается в JModelica - PullRequest
0 голосов
/ 09 ноября 2018

У меня есть одна модель 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 голосов
/ 13 ноября 2018

Просто небольшие исправления и улучшения, например, сделайте его недействительной функцией.

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.

...