Opened 6 years ago

Closed 3 years ago

Last modified 3 years ago

#5172 closed defect (fixed)

[Replaceable] Binding equation in element redeclaration doesn't work

Reported by: igor.timofeyev@… Owned by: Per Östlund
Priority: normal Milestone: 1.19.0
Component: New Instantiation Version:
Keywords: Binding redeclaration replaceable Cc: Peter Fritzson

Description

Binding in Base2 should work according to specification, but it doesn't (as a result y2 = 10 in Base2)

package abc

class Base1
  replaceable Real y2 = 10;
end Base1;

class Base2
  extends Base1;
  redeclare Integer y2= 5;
end Base2;

end abc;

Change History (8)

comment:1 by Francesco Casella, 6 years ago

I see two problems with this example. The first is that I understand you should put the redeclare in the extends clause:

class Base2
  extends Base1(redeclare Integer y2= 5);
end Base2;

The current front-end gives the wrong result y2 = 10, but if you try the new front-end with the debug flag -d=newInst, you get the correct result. Please note that the new front-end will be the default from release 2.0.0 next year and we are no longer fixing remaining issues in the old front-end.

The second problem is that, in fact, even the model shown above is illegal, because Integer is not a sub-type of Real, so the compiler should issue an error, or at least a warning. This one would instead be legal

class Base2
  extends Base1(redeclare Modelica.SIunits.Length y2= 5);
end Base2;

and works nicely with the new front end (and in Dymola, for good measure).

comment:2 by Francesco Casella, 6 years ago

Component: BackendNew Instantiation
Milestone: 1.13.02.0.0
Owner: changed from Lennart Ochel to Per Östlund
Status: newassigned

in reply to:  1 comment:3 by Per Östlund, 6 years ago

Replying to casella:

I see two problems with this example. The first is that I understand you should put the redeclare in the extends clause:

The redeclare in the description is a "redeclare as element", rather than a "redeclare as modifier". Both types of redeclare are valid, so that part is fine. The type is wrong though as you say, but we don't check that yet.

comment:4 by igor.timofeyev@…, 6 years ago

Yes, "redeclare as element" is used here.

Regarding redeclaration of Real to Integer - it shouldn't be correct (according to the spec, Real is not a subtype of Integer), but this example is taken from "Principles of Object-Oriented Modeling and Simulation with Modelica 3.3" (Peter Fritzson, second edition) - see p.157, section 4.3.1. And it works (both in OpenModelica and SystemModeler). So that's quite strange.

comment:5 by Francesco Casella, 6 years ago

Cc: Peter Fritzson added

Strange indeed. If I wrote a model with a replaceable Real variable y2, I should be auhorized to use der(y2) in the model equation, since I expect y2 to be a subtype of Real, hence amenable to differentiation. In fact, adding a unit or a changing the max attribute doesn't create any problem from this point of view. But if I redeclare it as Integer, the model could be broken, as there's no such thing as the derivative of an Integer variable. Unless of course if the Integer is constant (then it could be interpreted as zero), but in general it could be broken.

Compilers should check these constraints statically to create a safe environment that avoids modellers getting into trouble with a-priori analysis. If they don't, they just leave the burden on the end user. It's not an ideal situation, but you can leave with it. Much worse is to have compilers that do not compile legal code, or compile it into wrong simulation code. I think we are currently mostly focusing on this kind of issues.

comment:6 by Francesco Casella, 6 years ago

Milestone: 2.0.02.1.0

comment:7 by Per Östlund, 3 years ago

Milestone: 2.1.01.18.0
Resolution: fixed
Status: assignedclosed

The new frontend is now the default and handles element redeclarations correctly. It still doesn't check constraining types, but that's covered by other tickets (like #1061) so I think we can close this one.

comment:8 by Francesco Casella, 3 years ago

Milestone: 1.18.01.19.0
Note: See TracTickets for help on using tickets.