Opened 7 years ago

Closed 6 years ago

Last modified 4 years ago

#4936 closed defect (fixed)

Dimension 1 has bounds 1..10, got array subscript 0

Reported by: vsanz@… Owned by: Lennart Ochel
Priority: high Milestone: 1.13.0
Component: Code Generation Version:
Keywords: Cc:

Description (last modified by Per Östlund)

When trying to simulate the following model with OM 1.13.0~dev-960-ged88ece on Linux, I get the error "Dimension 1 has bounds 1..10, got array subscript 0".

model GameofLife
  parameter Integer n=10; //dimension of the cellular space
  parameter Integer initState[:,2]= [1, 2; 2, 3; 3, 1; 3, 2; 3, 3]; //glider
  Integer state[n,n]; // current state of cells
  Integer newstate[n,n]( start = zeros(n,n)); // new state of cells
  Integer neighbors( start = 0); // number of neighbors alive

initial algorithm
    for i in 1:n loop
      for j in 1:n loop
        state[i,j] := 0;
      end for;
    end for;

    for i in 1:size(initState,1) loop
      state[initState[i,1],initState[i,2]] := 1;
    end for;

algorithm
  when sample(1,1) then // periodic iterations or steps
    for i in 0:n-1 loop // two for loops to evaluate all cells in the space
      for j in 0:n-1 loop
        neighbors := state[mod(i-1,n)+1,mod(j-1,n)+1]+
                     state[i+1,mod(j-1,n)+1]+
                     state[mod(i+1,n)+1,mod(j-1,n)+1]+
                     state[mod(i-1,n)+1,j+1]+
                     state[mod(i+1,n)+1,j+1]+
                     state[mod(i-1,n)+1,mod(j+1,n)+1]+
                     state[i+1,mod(j+1,n)+1]+
                     state[mod(i+1,n)+1,mod(j+1,n)+1];
        if state[i+1,j+1] == 1 then // if alive
          if neighbors < 2  or neighbors > 3 then
            newstate[i+1,j+1] := 0; // dies because of isolation or overpopulation (less than 2 or more than 3 neighbors alive)
          else
            newstate[i+1,j+1] := 1; // otherwise still alive
          end if;
        elseif neighbors == 3 then // if not alive
          newstate[i+1,j+1] := 1; // becomes alive because of reproduction (3 neighbors alive)
        end if;
      end for;
    end for;
    state := newstate; // update state
  end when;

end GameofLife;

Error seems to be in line 26 "neighbors := ...".

The same model works fine in Dymola.

Attachments (1)

GameofLife.mo (1.8 KB ) - added by vsanz@… 7 years ago.
model file

Download all attachments as: .zip

Change History (7)

by vsanz@…, 7 years ago

Attachment: GameofLife.mo added

model file

comment:1 by Francesco Casella, 7 years ago

Component: *unknown*Code Generation
Milestone: Future1.13.0
Owner: changed from somebody to Lennart Ochel

The reason why this model fails is that mod(x,y) is incorrectly computed within algorithm sections when x < 0.

I opened ticket #4939 on this specific issue with a much simpler test case. @vsanz, once that is fixed please check the status with the GameofLife model

comment:2 by Per Östlund, 7 years ago

Description: modified (diff)

Added code formatting to description.

comment:3 by Francesco Casella, 6 years ago

Resolution: fixed
Status: newclosed

GameOfLife now works nicely in v 1.13.0

comment:4 by gptshubham595@…, 4 years ago

Can you tell me why I am getting err in this

It is a simple nested loop
it gives err I am not declared inside if statement

How nested loops are used in open Modelica

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;

comment:5 by gptshubham595@…, 4 years ago

by mistake, replace "I am" with "i is"

in reply to:  4 comment:6 by Francesco Casella, 4 years ago

Replying to gptshubham595@…:

I just opened #6172 to keep track of this issue.

Note: See TracTickets for help on using tickets.