Opened 11 years ago

Closed 11 years ago

#2555 closed defect (fixed)

Missing cast for integer array

Reported by: Leonardo Laguna Owned by: Martin Sjölund
Priority: high Milestone: 1.9.1
Component: Frontend Version: trunk
Keywords: Cc:

Description

I have the following model that is producing an integer array that is not cast to real array. This case we think should be handled by the frontend.

Check the record 'SomeData' that is initialized with an array of numbers without decimal point.

model MissingCast
record SomeData
  parameter Real[10] data={1,2,3,4,5,6,7,8,9,10}; /* Integer numbers */
end SomeData;

function getData
  input Real x;
  output Real y;
protected
  SomeData data = SomeData();
  Integer i;
  Boolean finished;
  Real[:] v = data.data;
algorithm
  /* Just some code to avoid evaluate */
  finished:=false;
  i:=1;
  while (not finished) loop
    if x>v[i] then
       finished := true;
    end if;
    i:=i+1;
  end while;
  y:=v[i];
end getData;

Real value;

equation

value = getData(0);

end MissingCast;

If you flatten the model you will see the following code:

"function MissingCast.SomeData \"Automatically generated record constructor for MissingCast.SomeData\"
  input Real[10] data = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
  output SomeData res;
end MissingCast.SomeData;

function MissingCast.getData
  input Real x;
  output Real y;
  protected MissingCast.SomeData data = MissingCast.SomeData({1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
  protected Integer i;
  protected Boolean finished;
  protected Real[10] v = {data.data[1], data.data[2], data.data[3], data.data[4], data.data[5], data.data[6], data.data[7], data.data[8], data.data[9], data.data[10]};
algorithm
  finished := false;
  i := 1;
  while not finished loop
    if x > v[i] then
      finished := true;
    end if;
    i := 1 + i;
  end while;
  y := v[i];
end MissingCast.getData;

class MissingCast
  Real value;
equation
  value = MissingCast.getData(0.0);
end MissingCast;
"

The first thing to notice is that the record constructor successfully sets the type of the array.

However in the body of the function 'MissingCast.getData' there is the following binding:

protected MissingCast.SomeData data = MissingCast.SomeData({1, 2, 3, 4, 5, 6, 7, 8, 9, 10});

The array that appears there should be of type real, and if the array is not converted to real, at least should have a cast.

Change History (4)

comment:1 by Adrian Pop, 11 years ago

Owner: changed from somebody to Adrian Pop
Status: newaccepted

Current OpenModelica trunk/ actually crashes on this because we try to do dll evaluation.

comment:2 by Martin Sjölund, 11 years ago

You get the wrong result (and/or a simulation crash) even if the cast is fixed manually in the code.
This is because the array wants to index outside its bounds.

I propose to use the following model instead:

model MissingCast
record SomeData
  parameter Real[10] data={1,2,3,4,5,6,7,8,9,10}; /* Integer numbers */
end SomeData;

function getData
  input Real x;
  output Real y;
protected
  SomeData data = SomeData();
  Integer i;
  Boolean finished;
  Real[10] v;
algorithm
  v := data.data;
  /* Just some code to avoid evaluate */
  finished:=false;
  i:=1;
  while (not finished) and i<size(v,1) loop
    if x>data.data[i] then
       finished := true;
    end if;
    i:=i+1;
  end while;
  y:=v[i];
print(String(y) + "\n");
end getData;

Real value;

equation

value = getData(0);

end MissingCast;

comment:3 by Martin Sjölund, 11 years ago

Owner: changed from Adrian Pop to Martin Sjölund
Status: acceptedassigned

comment:4 by Martin Sjölund, 11 years ago

Resolution: fixed
Status: assignedclosed

Fixed in r19238.

Note: See TracTickets for help on using tickets.