Search code examples
modelicaopenmodelica

Modelica Coding Standards / Modelica When Statement


Greeting Everyone,

Happy New Year 2020

I am using Openmodelica 1.14 release version on 64 bit windows 7 system.

I am facing some trouble with ‘when’ statement in OMSimulator. While searching for solution, I came across a closed ticket #2664 in Openmodelica. I can still see the reported issue in the current release version of Openmodelica.

I am including the relevant files of ticket # 2664.

model SimpleTest "just a simple model - Compilation etc."
  Modelica.Blocks.Interfaces.IntegerInput u annotation(Placement(visible = true, transformation(origin = {-100, 40}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {-80, 40}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  Modelica.Blocks.Interfaces.IntegerOutput y annotation(Placement(visible = true, transformation(origin = {100, 20}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {100, 20}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
algorithm
  when change(u) then
    y := y + 2;
  end when;
  annotation(Icon(coordinateSystem(extent = {{-100, -100}, {100, 100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2, 2})), Diagram(coordinateSystem(extent = {{-100, -100}, {100, 100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2, 2}), graphics = {Rectangle(origin = {-6.15, 2.93}, fillColor = {0, 133, 199}, fillPattern = FillPattern.HorizontalCylinder, extent = {{-77.89, 83.75}, {88.14, -92.53}})}));
end SimpleTest;

Is SimpleTest.mo as per Modelica standards?

While compilation of SimpleTest.mo , it throws a Translation Warning

Assuming fixed start value for the following 1 variables:
         y:DISCRETE(flow=false fixed = false ) SimpleTest type: Integer

How to avoid this error?


Solution

  • It is just a warning. When you define a discrete variable like this such that it depends on it's previous value in a when condition, it has to have a fixed start value. Just providing a start value kind of is a guess value for the compiler, when you fix it you tell the compiler that it has to use this value for the initialization.

    If you don't provide a start value it is set to zero, if you don't fix it, the compiler fixes it automatically (resulting in the warning).

    Simple example:

    Integer y(start=0, fixed=true);
    

    Applied on your model:

    model SimpleTest "just a simple model - Compilation etc."
      Modelica.Blocks.Interfaces.IntegerInput u annotation(Placement(visible = true, transformation(origin = {-100, 40}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {-80, 40}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
      Modelica.Blocks.Interfaces.IntegerOutput y(start=0, fixed=true) annotation(Placement(visible = true, transformation(origin = {100, 20}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {100, 20}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
    algorithm
      when change(u) then
        y := y + 2;
      end when;
      annotation(Icon(coordinateSystem(extent = {{-100, -100}, {100, 100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2, 2})), Diagram(coordinateSystem(extent = {{-100, -100}, {100, 100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2, 2}), graphics = {Rectangle(origin = {-6.15, 2.93}, fillColor = {0, 133, 199}, fillPattern = FillPattern.HorizontalCylinder, extent = {{-77.89, 83.75}, {88.14, -92.53}})}));
    end SimpleTest;