c function is not correctly called in JModelica









up vote
1
down vote

favorite












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.










share|improve this question

























    up vote
    1
    down vote

    favorite












    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.










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      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.










      share|improve this question













      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.







      modelica fmi jmodelica






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 17:02









      Sen

      111




      111






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          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.






          share|improve this answer




















          • Thanks, I applied the modifications to the model but it seems the issue still occur.
            – Sen
            Nov 13 at 22:35










          • by the way, can you please explain why this modification can resolve the issue?
            – Sen
            Nov 13 at 22:36










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230262%2fc-function-is-not-correctly-called-in-jmodelica%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          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.






          share|improve this answer




















          • Thanks, I applied the modifications to the model but it seems the issue still occur.
            – Sen
            Nov 13 at 22:35










          • by the way, can you please explain why this modification can resolve the issue?
            – Sen
            Nov 13 at 22:36














          up vote
          1
          down vote













          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.






          share|improve this answer




















          • Thanks, I applied the modifications to the model but it seems the issue still occur.
            – Sen
            Nov 13 at 22:35










          • by the way, can you please explain why this modification can resolve the issue?
            – Sen
            Nov 13 at 22:36












          up vote
          1
          down vote










          up vote
          1
          down vote









          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 21:48









          tbeu

          29525




          29525











          • Thanks, I applied the modifications to the model but it seems the issue still occur.
            – Sen
            Nov 13 at 22:35










          • by the way, can you please explain why this modification can resolve the issue?
            – Sen
            Nov 13 at 22:36
















          • Thanks, I applied the modifications to the model but it seems the issue still occur.
            – Sen
            Nov 13 at 22:35










          • by the way, can you please explain why this modification can resolve the issue?
            – Sen
            Nov 13 at 22:36















          Thanks, I applied the modifications to the model but it seems the issue still occur.
          – Sen
          Nov 13 at 22:35




          Thanks, I applied the modifications to the model but it seems the issue still occur.
          – Sen
          Nov 13 at 22:35












          by the way, can you please explain why this modification can resolve the issue?
          – Sen
          Nov 13 at 22:36




          by the way, can you please explain why this modification can resolve the issue?
          – Sen
          Nov 13 at 22:36

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230262%2fc-function-is-not-correctly-called-in-jmodelica%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kleinkühnau

          Makov (Slowakei)

          Deutsches Schauspielhaus