Search code examples
modelicaopenmodelica

Modelica integerChange block not working as intended?


I'm trying to use the integerChange block from the Modelica Standard library It doesn't seem to work however. What am i doing wrong? I would have expected a spike at each change,but i get a constant "false". I'm using OpenModelica 1.17

Here is the simple model

model integerChangeTest
  Modelica.Blocks.Math.IntegerChange integerChange annotation(
    Placement(visible = true, transformation(origin = {26, 24}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  Modelica.Blocks.Math.RealToInteger realToInteger annotation(
    Placement(visible = true, transformation(origin = {-6, 24}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  Modelica.Blocks.Sources.Sine sine(amplitude = 5, freqHz = 5) annotation(
    Placement(visible = true, transformation(origin = {-48, 26}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
equation
  connect(realToInteger.y, integerChange.u) annotation(
    Line(points = {{5, 24}, {13, 24}}, color = {255, 127, 0}));
  connect(sine.y, realToInteger.u) annotation(
    Line(points = {{-37, 26}, {-19, 26}, {-19, 24}}, color = {0, 0, 127}));
  annotation(
    uses(Modelica(version = "3.2.3")));
end integerChangeTest;

enter image description here


Solution

  • The block works, but plotting change(x) is complicated in many Modelica tools.

    The reason is that at an event there are a number of intermediate values, and to avoid plotting too many values one common solution is to just store the first and last; that also simplifies the implementation since it avoids a callback for storing values during the event iteration. Unfortunately change is only true in intermediate values during event iterations - and thus plotting it becomes meaningless.

    I don't know if OpenModelica has some special mode for including them as well.

    If you want to see that it changes you can use the code in the comment or graphically add not+OnDelay

    model integerChangeTest
      Modelica.Blocks.Math.IntegerChange integerChange annotation (
        Placement(visible = true, transformation(origin = {26, 24}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
      Modelica.Blocks.Math.RealToInteger realToInteger annotation (
        Placement(visible = true, transformation(origin = {-6, 24}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
      Modelica.Blocks.Sources.Sine sine(amplitude = 5, freqHz = 5) annotation (
        Placement(visible = true, transformation(origin = {-48, 26}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
      Modelica.Blocks.Logical.Not not1
        annotation (Placement(transformation(extent={{46,14},{66,34}})));
      Modelica.Blocks.MathBoolean.OnDelay onDelay(delayTime=1e-3)
        annotation (Placement(transformation(extent={{82,20},{90,28}})));
    equation 
      connect(realToInteger.y, integerChange.u) annotation (
        Line(points={{5,24},{14,24}},      color = {255, 127, 0}));
      connect(sine.y, realToInteger.u) annotation (
        Line(points={{-37,26},{-18,26},{-18,24}},        color = {0, 0, 127}));
      connect(integerChange.y, not1.u)
        annotation (Line(points={{37,24},{44,24}}, color={255,0,255}));
      connect(onDelay.u, not1.y)
        annotation (Line(points={{80.4,24},{67,24}}, color={255,0,255}));
      annotation (
        uses(Modelica(version="3.2.2")));
    end integerChangeTest;