Opened 4 years ago

Last modified 3 years ago

#6172 accepted defect

Issue with for loop index variables in conditional expression within algorithm

Reported by: Francesco Casella Owned by: Mahder Alemseged Gebremedhin
Priority: high Milestone:
Component: Code Generation Version:
Keywords: Cc: gptshubham595@…

Description

Consider the following test model

class matrixAdd
  parameter Real[3,2] m={{1,2},{2,3},{4,5}};
  parameter Real[3,2] a={{2,2},{2,2},{2,2}};
  Real[3,2] b;
  Real max;
algorithm
  max:=0;
  for i in 1:3 loop
    for j in 1:2 loop
      b[i,j]:=m[i,j]+a[i,j];
      if (max<b[i,j]) then
          max:=b[i,j];
      end if;
    end for;
  end for;
end matrixAdd;

It causes the C compilation to break with

matrixAdd_05evt.c: In function 'matrixAdd_function_ZeroCrossings':
matrixAdd_05evt.c:51:161: error: 'i' undeclared (first use in this function)
   tmp0 = LessZC(data->localData[0]->realVars[6] /* max variable */, (&data->localData[0]->realVars[0] /* b[1,1] variable */)[calc_base_index_dims_subs(2, 3, 2, i, ((modelica_integer) 1))], data->simulationInfo->storedRelations[0]);
                                                                                                                                                                 ^
matrixAdd_05evt.c:51:161: note: each undeclared identifier is reported only once for each function it appears in
matrixAdd_05evt.c: In function 'matrixAdd_function_updateRelations':
matrixAdd_05evt.c:78:163: error: 'i' undeclared (first use in this function)
     tmp2 = LessZC(data->localData[0]->realVars[6] /* max variable */, (&data->localData[0]->realVars[0] /* b[1,1] variable */)[calc_base_index_dims_subs(2, 3, 2, i, ((modelica_integer) 1))], data->simulationInfo->storedRelations[0]);

Change History (6)

comment:1 by Mahder Alemseged Gebremedhin, 4 years ago

Status: newaccepted

comment:2 by Mahder Alemseged Gebremedhin, 4 years ago

This is an issue with zero crossing expression creations in the BackEnd. It was operating under the assumption that there is only one iterator to replace at a time. So if you have nested loops the iterator from the outside loop is ignored when the inner one comes in.

I attempted a fix for it in (https://github.com/OpenModelica/OpenModelica/pull/6878)

Unfortunately, there are 17 tests failing due to different variable values. I will investigate more tomorrow.

comment:3 by Philip Hannebohm, 4 years ago

This looks crazy. How do you even do zero crossings in an algorithm section, when the variables get overwritten in each for-loop iteration?

If I understand this correctly every condition depends not only on time/state, but also on every previous condition in the loop. Does that mean an event gets triggered every time an element of b becomes larger than all the previous elements, even if the overall maximum stays the same?

The more I think about this the less I understand it...

comment:4 by Francesco Casella, 4 years ago

Any further discoveries on this issue?

comment:5 by Francesco Casella, 4 years ago

Milestone: 1.17.01.18.0

Retargeted to 1.18.0 because of 1.17.0 timed release.

comment:6 by Francesco Casella, 3 years ago

Milestone: 1.18.0

Ticket retargeted after milestone closed

Note: See TracTickets for help on using tickets.