Search code examples
modelica

Loop through modelica array fails


I am using openmodelica and I am trying to loop through an array in order to find the maximum value. I was able to reduce my code to a very simple test case that still gives the error. Is this something that I am doing wrong, or is this a bug in openmodelica? Here is a very simple case that does give the error:

package TestLoop
  model ItemA
    Real p;
  end ItemA;

  model ItemB
    ItemA a[n];
    parameter Integer n = 5;
    Real p;
  equation
    for i in 1:n loop
      a[i].p = time;
    end for;
  algorithm
    for i in 1:n loop
      p := a[i].p;
    end for;
  end ItemB;
end TestLoop;

The problem is in my algorithm section. Here is the error that I am getting:

TestLoop.ItemB.c:155:13: warning: implicit declaration of function '$Pa$lB' is invalid in C99 [-Wimplicit-function-declaration]
      $Pp = $Pa$lB(modelica_integer)$Pi$rB$Pp;
            ^
TestLoop.ItemB.c:155:20: error: unexpected type name 'modelica_integer': expected expression
      $Pp = $Pa$lB(modelica_integer)$Pi$rB$Pp;
                   ^
1 warning and 1 error generated.

Any suggestions for why this might be, or how I can work around it? If I replace the assignment with a fixed value, p:=a[1].p;, the code does run (although that is not useful to me). What I ultimately want to do in the algorithm section is find the largest value of a[n].p, where I do have an equation section that does useful calculations into the array of items.


Solution

  • Yes, the code generation is an error of OpenModelica (it does not like unknown array indexes). Your problem is very easy to solve in a single line though (one of the following):

    p = max(r for r in a.p);
    p = max(a.p);