Mats Lindqvist
Mats Lindqvist

Reputation: 61

"Dimension sizes are not equal and destination array is not flexible."

When I call this function from my "main model" it produces the expected result, but when I call it from a different function, saving, "testing" and compilation works fine, but upon "simulation" I get the following error message: "Dimension sizes are not equal and destination array is not flexible"

The function reverses/"flips" a vector of length n, so that last element comes first.

function myReverse
  input Integer n; 
  input Real[n] v; //  "Real vector";
  output Real[n] result; // "Elements of vector v in reversed order";

algorithm

  result := vector({v[end - i + 1] for i in 1:n});

end myReverse;

Upvotes: 1

Views: 218

Answers (1)

Markus A.
Markus A.

Reputation: 7500

This is strange. I assume you are not using Dymola, because the code you provided works in Dymola 2021x.

I used the following test, simulating the model "Test":

package ReversePackage
  function myReverse
    input Real[n] v;       //  "Real vector";
    output Real[n] result; // "Elements of vector v in reversed order";

  protected 
    Integer n=size(v,1);

  algorithm 
    result := vector({v[end - i + 1] for i in 1:n});

  end myReverse;

  model Test
    extends Modelica.Icons.Example;
    Real out[2];

  equation 
    out =ReversePackage.myReverse({time,1});
  end Test;
end ReversePackage;

As an additional note: I simplified myReverse a bit by reading the size of the input-vector instead of having the size as a parameter that is passed to the function.

Just a "wild guess", but I could imagine, that the end operator causes the issue, try replacing it by n, which shouldn't be an issue as you have it anyway. Also unecessary vector() could cause issues as marco pointed out. So try to remove it as well. Both changes implemented below:

function myReverse
  input Real[n] v;       //  "Real vector";
  output Real[n] result; // "Elements of vector v in reversed order";
 
protected 
  Integer n=size(v,1);

algorithm 
  result := {v[n- i + 1] for i in 1:n};

end myReverse;

Upvotes: 2

Related Questions