Opened 5 years ago

Closed 3 years ago

#5838 closed defect (fixed)

Unexpected type mismatch for smallRotation

Reported by: dariomangoni@… Owned by: somebody
Priority: high Milestone: 1.19.0
Component: Frontend Version: v1.16.0-dev
Keywords: Cc:

Description

at OpenModelica v1.16.0-dev-120-g0c802bd99

smallRotation compiles with the following error:

Translation Error
[Modelica.Mechanics.MultiBody.Frames: 3043:7-3045:13]: Type mismatch in if-expression in component . True branch: {T[2, 3], -T[1, 3], T[1, 2], T[1, 1] - 1.0, T[2, 2] - 1.0, T[1, 1] * T[2, 2] - T[2, 1] * T[1, 2] - 1.0} has type Real[6], false branch: {T[2, 3], -T[1, 3], T[1, 2]} has type Real[3].

This applies both for Mechanics.MultiBody.Frames.smallRotation and for Mechanics.MultiBody.Frames.TransormationMatrices.smallRotation.

In the attachment a minimal test

Attachments (1)

TestSmallRotations.mo (1.7 KB ) - added by dariomangoni@… 5 years ago.
Mimial test

Download all attachments as: .zip

Change History (8)

by dariomangoni@…, 5 years ago

Attachment: TestSmallRotations.mo added

Mimial test

comment:1 by Per Östlund, 5 years ago

The issue is that smallRotation is declared as:

function smallRotation  "Return rotation angles valid for a small rotation and optionally residues that should be zero" 
  extends Modelica.Icons.Function;
  input TransformationMatrices.Orientation T "Orientation object to rotate frame 1 into frame 2";
  input Boolean withResidues = false "= false/true, if 'angles'/'angles and residues' are returned in phi";
  output Modelica.SIunits.Angle[if withResidues then 6 else 3] phi "The rotation angles around x-, y-, and z-axis of frame 1 to rotate frame 1 into frame 2 for a small rotation + optionally 3 residues that should be zero";
algorithm
  phi := if withResidues then {T[2, 3], -T[1, 3], T[1, 2], T[1, 1] - 1, T[2, 2] - 1, T[1, 1] * T[2, 2] - T[2, 1] * T[1, 2] - 1} else {T[2, 3], -T[1, 3], T[1, 2]};
  annotation(Inline = true); 
end smallRotation;

I'm unsure if this is even legal, since both branches of an if-expression must be type compatible unless the condition can be evaluated such that one branch is selected. withResidues is in this case an input to the function, so it can't really be evaluated for typing purposes.

The function does contain annotation(Inline = true) though, and if the if-expression was inlined it would be possible to evaluate it. However, the specification does not specify when inlining happens, and in our case it happens in the later stages of the compilation.

I will have to check how this function is actually supposed to be handled. In the meantime it's possible to work around the issue by rewriting the if-expression into an if-statement (which doesn't have the same restriction):

function smallRotation  "Return rotation angles valid for a small rotation and optionally residues that should be zero" 
  extends Modelica.Icons.Function;
  input TransformationMatrices.Orientation T "Orientation object to rotate frame 1 into frame 2";
  input Boolean withResidues = false "= false/true, if 'angles'/'angles and residues' are returned in phi";
  output Modelica.SIunits.Angle[if withResidues then 6 else 3] phi "The rotation angles around x-, y-, and z-axis of frame 1 to rotate frame 1 into frame 2 for a small rotation + optionally 3 residues that should be zero";
algorithm
  if withResidues then
    phi := {T[2, 3], -T[1, 3], T[1, 2], T[1, 1] - 1, T[2, 2] - 1, T[1, 1] * T[2, 2] - T[2, 1] * T[1, 2] - 1};
  else
    phi := {T[2, 3], -T[1, 3], T[1, 2]};
  end if;a
  annotation(Inline = true); 
end smallRotation;

comment:2 by dariomangoni@…, 5 years ago

Oh, nice!
I was not aware of these different restrictions on different ifs. Still a lot to learn...

I might put this as issue also on Modelica's GitHub so to have also their opinion.

in reply to:  2 comment:3 by Per Östlund, 5 years ago

Replying to dariomangoni@…:

I might put this as issue also on Modelica's GitHub so to have also their opinion.

I opened a ticket already, see #2494.

in reply to:  2 ; comment:4 by Francesco Casella, 5 years ago

Replying to dariomangoni@…:

Please close the ticket yourself if the if-statement workaround works for you.

Thanks!

in reply to:  4 comment:5 by Per Östlund, 5 years ago

Replying to casella:

Replying to dariomangoni@…:

Please close the ticket yourself if the if-statement workaround works for you.

Thanks!

Might as well keep it open for now since it's an issue in the MSL that needs to be resolved one way or another.

comment:6 by Francesco Casella, 5 years ago

OK, sounds good.

I'm just trying to avoid the number of open tickets to grow indefinitely, but it feels like a quintessential Quixotic task :)

comment:7 by Per Östlund, 3 years ago

Component: *unknown*Frontend
Milestone: Future1.19.0
Resolution: fixed
Status: newclosed

Seems this is still not entirely resolved in the specification, but we handle it nonetheless now.

Note: See TracTickets for help on using tickets.