Skip to content

Use intermediate variables for Complex if-expressions in ComplexBlocks - #4847

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

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

Conversation

@hubertus65

Copy link
Copy Markdown
Member

Four blocks of Modelica.ComplexBlocks.ComplexMath (Feedback, Gain, Division, Add3) and Modelica.ComplexBlocks.Sources.ComplexStep write a Complex if-expression inline in the equation of their output connector:

y = k*(if useConjugateInput then Modelica.ComplexMath.conj(u) else u);   // Gain
y = offset + (if time < startTime then Complex(0) else height);           // ComplexStep

OCT, the compiler of Modelon Impact, generates C code for these equations that does not compile. The code contains the record constructor of the connector type, Modelica.ComplexBlocks.Interfaces.ComplexOutput(...). As a result none of the five blocks can be used in Impact, with or without conjugated inputs.

Other blocks of the same package are not affected. Add and Product first assign the if-expression to a protected variable (u1Internal, u2Internal from Interfaces.ComplexSI2SO, or uInternal from Interfaces.ComplexSISO).

This PR applies that existing pattern to the five blocks:

  • Gain: protected uInternal, then y = k*uInternal
  • Feedback: protected u1Internal, u2Internal
  • Add3: protected u1Internal … u3Internal
  • Division: already extends ComplexSI2SO; now uses its u1Internal/u2Internal instead of repeating the if-expressions
  • ComplexStep: protected stepValue

Public interfaces, parameters and documentation are unchanged.

Verification

Model OpenModelica 1.27.1, before vs. after OCT (Impact), before OCT (Impact), after
ComplexBlocks.Examples.ShowTransferFunction bit-identical – –
ComplexBlocks.Examples.TestConversionBlock bit-identical – –
Test using Feedback, Gain, Division, Add3, with and without conjugation bit-identical C compile error simulates, same values as OpenModelica
Test using ComplexStep bit-identical C compile error simulates, same values as OpenModelica
Other ModelicaTest models (ComplexMathFunctions, ComplexMathOperators) bit-identical – –

The "after" column for OCT was produced with copies of the patched classes, since Impact ships its own MSL. The test models are part of the ModelicaTest coverage work on branch coverage-improvement.

🤖 Generated with Claude Code

https://claude.ai/code/session_01UmNZkRw6HV9CeBvNk36AzT

Feedback, Gain, Division and Add3 of ComplexBlocks.ComplexMath and
Sources.ComplexStep wrote Complex if-expressions inline in the equation
of their output connector, e.g.

  y = k*(if useConjugateInput then Modelica.ComplexMath.conj(u) else u);

OCT (Modelon Impact) generates C code that does not compile for this: it
contains the record constructor of the connector type
Modelica.ComplexBlocks.Interfaces.ComplexOutput(...). As a result these
five blocks cannot be used in Impact at all.

Assign the if-expression to a protected Complex variable first, as Add
and Product already do through the u1Internal/u2Internal variables of
Interfaces.ComplexSI2SO (Division extends that base class too and now
uses them). The public interface is unchanged.

OpenModelica 1.27.1 gives bit-identical results before and after the
change for the ComplexBlocks examples ShowTransferFunction and
TestConversionBlock and for four ModelicaTest models using all five
blocks. With the change, OCT compiles and simulates the blocks and
matches the OpenModelica 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