Skip to content

Assign the real and imaginary parts of k separately in ComplexBlocks.Routing.Extractor - #4850

Open
hubertus65 wants to merge 1 commit into
modelica:masterfrom
hubertus65:fix-ComplexBlocks-Extractor-if
Open

hubertus65 wants to merge 1 commit into
modelica:masterfrom
hubertus65:fix-ComplexBlocks-Extractor-if

Conversation

@hubertus65

Copy link
Copy Markdown
Member

Modelica.ComplexBlocks.Routing.Extractor sets its protected Complex selection vector k in a when-equation:

when {initial(),change(index)} then
  for i in 1:nin loop
    k[i] = if index == i then Complex(1,0) else Complex(0,0);
  end for;
end when;

OpenModelica 1.27.1 cannot generate code for a record assignment inside a when-equation, so no model that uses Extractor builds:

Error: Template error: No runtime support for this record assignment: extractor2.k[1] = if extractor2.index == 1 then Complex(1.0, 0.0) else Complex(0.0, 0.0).

The error is not specific to the if-expression. k[i] = Complex(if index == i then 1 else 0, 0) fails the same way. This PR assigns the two Real fields instead, which OpenModelica supports:

    k[i].re = if index == i then 1 else 0;
    k[i].im = 0;

Verification

Test: Extractor with nin=3, driven by an IntegerStep index from 1 to 3 (and out of range with allowOutOfRange=true) at t = 0.5.

Tool Before After
OpenModelica 1.27.1 code generation error (above) simulates; matches OCT within 1e-15, except at the event instant itself
OCT (Modelon Impact) simulates simulates; same final values

The OCT "after" result was produced with a copy of the changed block, since Impact ships its own MSL. The test model is part of the ModelicaTest coverage work on branch coverage-improvement.

🤖 Generated with Claude Code

https://claude.ai/code/session_01UmNZkRw6HV9CeBvNk36AzT

…Routing.Extractor

Extractor sets its internal Complex selection vector k in a when-equation:

  k[i] = if index == i then Complex(1,0) else Complex(0,0);

OpenModelica 1.27.1 cannot generate code for a record assignment in a
when-equation ("No runtime support for this record assignment"), whatever
its right-hand side, so models using Extractor do not build. Assign the
two Real fields instead:

  k[i].re = if index == i then 1 else 0;
  k[i].im = 0;

With this change OpenModelica simulates a test with Extractor (with and
without allowOutOfRange) and matches the results of OCT (Modelon Impact)
for the original code, except at the event instant itself. OCT compiles
and simulates the changed block 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