c function is not correctly called in JModelica

132 views Asked by At

I have one Modelica model :

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;

And the c code is also shown as below:

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

The above code works well in Dymola, but when I run it in JModelica, I got one issue:

When simulate this model in period [0,200], I expect the c function will be called by 4 times: t=10,30,90,150. But I found in Jmodelica, the c function is actually called by 24 times!

Any help to explain the above issue will be highly appreciated.

1

There are 1 answers

2
tbeu On

Just some small corrections and improvements, e.g., make it a void function.

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;

Btw, unrelated to FMI.