Skip to content

Use if-statements in Frames.smallRotation and TransformationMatrices.smallRotation - #4848

Open
hubertus65 wants to merge 1 commit into
modelica:masterfrom
hubertus65:fix-MultiBody-smallRotation-if
Open

hubertus65 wants to merge 1 commit into
modelica:masterfrom
hubertus65:fix-MultiBody-smallRotation-if

Conversation

@hubertus65

Copy link
Copy Markdown
Member

Modelica.Mechanics.MultiBody.Frames.smallRotation and Frames.TransformationMatrices.smallRotation return their result with an if-expression whose branches have different sizes. The condition is the function input withResidues:

output SI.Angle phi[if withResidues then 6 else 3];
algorithm
  phi := if withResidues then {T[2, 3], ..., T[1, 1]*T[2, 2] - T[2, 1]*T[1, 2] - 1} else {T[2, 3], -T[1, 3], T[1, 2]};

Branches of different array sizes require a parameter condition. OCT, the compiler of Modelon Impact, therefore rejects both functions:

If-expression with branches that have different array sizes must have parameter test-expression:
    {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} has size [6]
    {T[2,3], - T[1,3], T[1,2]} has size [3]

The declaration phi[if withResidues then 6 else 3] already fixes the size of phi. This PR assigns it in an if-statement instead:

  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;

Verification

The test model calls both functions with and without residues on a rotation axesRotations({1, 2, 3}, {a, 2*a, -a}) with a ramping to 1e-3.

Tool Before After
OpenModelica 1.27.1 simulates bit-identical to before (20 variables)
OCT (Modelon Impact) rejected (error above) simulates, same values as OpenModelica

Nothing else in the MSL calls these functions; they are covered by a new ModelicaTest test on branch coverage-improvement.

🤖 Generated with Claude Code

https://claude.ai/code/session_01UmNZkRw6HV9CeBvNk36AzT

…smallRotation

Both functions returned their result with an if-expression whose branches
have different sizes (6 and 3) and whose condition is the function input
withResidues:

  phi := if withResidues then {...6 elements...} else {...3 elements...};

Branches of different sizes are only allowed for a parameter condition, so
OCT (Modelon Impact) rejects both functions ("If-expression with branches
that have different array sizes must have parameter test-expression").
The size of phi is already fixed by its declaration
phi[if withResidues then 6 else 3], so assign it in an if-statement
instead. OpenModelica 1.27.1 gives bit-identical results before and after
the change for both functions with and without residues; OCT now compiles
and simulates them with the same results.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UmNZkRw6HV9CeBvNk36AzT

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant