Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

Array of components not working with nested "for" statements.

Array of components not working with nested "for" statements.

Hello,

I have created a simple model with an array of array connectors and compilation fails when using nested for loops to assign values. Find the code below:

The connector I use:

connector Signal
  parameter Integer n = 1 "Signal Array Dimension";
  Real values[n] "Real Input Values";
end Signal;


The test model:

model ArrayOfSignalsTest
  parameter Integer numberOfSignals1 = 2;
  parameter Integer numberOfSignals2[numberOfSignals1] = {3,3};
protected
  Signal mySignals[numberOfSignals1](n = numberOfSignals2);
equation
  for j in 1:numberOfSignals1 loop
    for i in 1:numberOfSignals2[j] loop
      mySignals[j].values[i] = 1;
    end for;
  end for;
end ArrayOfSignalsTest;


When I check the model in OMEdit, I get the following result:

Check of ArrayOfSignalsTest completed successfully.


Class ArrayOfSignalsTest has 6 equation(s) and 6 variable(s).
6 of these are trivial equation(s).


Then when I run the model in OMEdit I get the following error:

---- Error 1 : 08:33:08 ----
Unable to simulate the Model 'ArrayOfSignalsTest'
Following Error has occurred.

Simulation failed for model: ArrayOfSignalsTest
[/home/..../file.mo:84:7-84:31:writable] Error: Illegal subscript [i] for dimensions 2, 3 in component mySignals
[/home/..../file.mo:84:7-84:31:writable] Error: Variable mySignals[j].values[i] not found in scope ArrayOfSignalsTest.$for loop scope$.$for loop scope$.$for loop scope$.$for loop scope$
Error: Error occured while flattening model ArrayOfSignalsTest


I don't know why this error occurs and how can I solve it.

Any help about this error would be appreciated.

Thank you in advance.

(by the way, note the mistyping in the word "occured" in the last sentence of the OMEdit output...)

Re: Array of components not working with nested "for" statements.

The problem is that you are doing something similar to:

Code:


XXX[2] a;
a[3] := ...;

That is, the index is out of bounds (and boy would such an error message be easier to interpret...)

Re: Array of components not working with nested "for" statements.

Dear  sjoelund.se, thank you for your fast reply, however, I don't get it...I'm sorry...

I think the behavior of my code should be the following:

Signal mySignals[numberOfSignals1](n = numberOfSignals2); should create an array of Signals of length 2, such that:

mySignals[1] should have n = 3, this way mySignals[1].values[1], mySignals[1].values[2] and mySignals[1].values[3] should exist.
mySignals[2] should have n = 3, this way mySignals[2].values[1], mySignals[2].values[2] and mySignals[2].values[3] should exist.

Then, I expect that the for loop means what follows:

for j in 1:numberOfSignals1 loop == for j in 1:2 loop, so j takes values 1 and 2, then:
  when j=1:
    for i in 1:numberOfSignals2[1] == for i in 1:3 loop, so i takes values 1, 2 and 3, then:
      when i = 1: mySignals[j].values[i] = 1 results in mySignals[1].values[1] = 1 --> indexes seem to be legal;
      when i = 2: mySignals[j].values[i] = 1 results in mySignals[1].values[2] = 1 --> indexes seem to be legal;
      when i = 3: mySignals[j].values[i] = 1 results in mySignals[1].values[3] = 1 --> indexes seem to be legal;
    end for;
  when j=2:
    for i in 1:numberOfSignals2[2] == for i in 1:3 loop, so i takes values 1, 2 and 3, then:
      when i = 1: mySignals[j].values[i] = 1 results in mySignals[2].values[1] = 1 --> indexes seem to be legal;
      when i = 2: mySignals[j].values[i] = 1 results in mySignals[2].values[2] = 1 --> indexes seem to be legal;
      when i = 3: mySignals[j].values[i] = 1 results in mySignals[2].values[3] = 1 --> indexes seem to be legal;
    end for;
end for;

This way, although the error message is really easy to interpret, as you said, I haven't understood when the indexes are out of bounds. I guess I'm misinterpreting some behavior in the Signal declaration or in the for loop.

Would you mind explaining me what is the actual behavior of my code?

If you think this shouldn't be a forum topic, you can write me an e-mail to jgbarberena@gmail.com.

Thank you for your time,

Re: Array of components not working with nested "for" statements.

Oh, I read the order of the indices wrong (just assumed it was i outer-most). The code should work... As should this smaller example:

Code:

connector Signal 

  parameter Integer n;
  Real values[n];
end Signal;

model ArrayOfSignalsTest
  Signal mySignals[1](each n = 3);
equation
  mySignals[1].values[2] = 1;
end ArrayOfSignalsTest;

I'll create a bug for it: http://openmodelica.org:8080/cb/issue/1 … ation=true

Re: Array of components not working with nested "for" statements.

Per fixed this in r9956. If you are using Linux/OSX nightly builds, it will be out in 26 hours or so. If you use Windows, I suppose you need to wait for the next 1.8 beta or nightly build.

Re: Array of components not working with nested "for" statements.

Thank you again,

I've updated to rev 9956 and run both yours and my code, both have worked.

However, I'm sad to tell you that a bug is still there. The following code doesn't work and I suppose it should:

model ArrayOfSignalsTest

  Signal mySignals[2](n = {3,4});

equation

  mySignals[1].values[1] = 1;
  mySignals[1].values[2] = 1;
  mySignals[1].values[3] = 1;

  mySignals[2].values[1] = 1;
  mySignals[2].values[2] = 1;
  mySignals[2].values[3] = 1;
  mySignals[2].values[4] = 1;

end ArrayOfSignalsTest;


The error message is slightly different, what I get is the following:

---- Error 8 : 13:36:34 ----
Unable to simulate the Model 'ArrayOfSignalsTest'
Following Error has occurred.

Simulation failed for model: ArrayOfSignalsTest
[<interactive>:14:3-14:29:writable] Error: Illegal subscript [4] for dimensions 3 in component mySignals[2]
[<interactive>:14:3-14:29:writable] Error: Variable mySignals[2].values[4] not found in scope ArrayOfSignalsTest
Error: Error occured while flattening model ArrayOfSignalsTest


In this case, what I think is failing is the declaration of the Signal array with different "n" value. The code works for n={4,3}...

Re: Array of components not working with nested "for" statements.

That will create a jagged array, and I suppose OpenModelica will have some major issues with it (as you found out).

Basically, the assumption is that the following kind of code should work:

Code:

model N

  parameter Integer n;
  Real r[n];
end N;

model M
  N[2] n(n = {3,4});
equation
  n[:].r = {{1,2,3},{4,5,6/*,7*/}}; // The 7th element cannot be used here, because any array has to be rectangular...
end M;

The code as written works in OpenModelica, but I believe it should not. Loading this code in Dymola 7.4 causes a segmentation fault! I suppose I should open up a modelica.org ticket to ask for clarification if this is allowed or not current/smile

The following works in Dymola, but not OpenModelica, and I believe this should be legal at least current/smile

Code:

model M

  N[2] n(n = {3,4});
equation
  n[1].r = {1,2,3};
  n[2].r = {4,5,6,7};
end M;

Re: Array of components not working with nested "for" statements.

Thank you again,

I also think this should be legal in Modelica.

I hope these problems could be solved as soon as possible by the OpenModelica developers.

There are 0 guests and 0 other users also viewing this topic
You are here: