Skip to content

Relate clocks in Integer/Boolean AssignClockToSquareWaveHold as in the Real version - #4849

Open
hubertus65 wants to merge 1 commit into
modelica:masterfrom
hubertus65:fix-Clocked-AssignClockToSquareWaveHold
Open

hubertus65 wants to merge 1 commit into
modelica:masterfrom
hubertus65:fix-Clocked-AssignClockToSquareWaveHold

Conversation

@hubertus65

Copy link
Copy Markdown
Member

The Integer and Boolean versions of Modelica.Clocked.*Signals.Sampler.Utilities.AssignClockToSquareWaveHold relate the clock of their input u to the internal square wave y2 through a "dummy condition" with identical branches:

// dummy condition to relate clock of u with y2
y2 = if u > 0 then not previous(y2) else not previous(y2);

A tool may legitimately simplify this to y2 = not previous(y2), which removes the only relation between u and y2. OpenModelica 1.27.1 does exactly that and assigns the default clock (1 s) to y2. With a 20 ms input clock, the output then toggles once per second instead of at every tick.

The Real version of the block does not have this problem. It ties the two signals with a clocked when-equation and a dummy assignment. This PR uses the same pattern in the Integer and Boolean versions:

  Integer uu;   // Boolean uu; in the Boolean version
equation
  when Clock() then
     uu = u "Dummy assignment to relate clock of u with y2";
     y2 = not previous(y2);
  end when;

Verification (OpenModelica 1.27.1)

Test: an Integer and a Boolean signal sampled with a 20 ms periodic clock, each converted with AssignClockToSquareWaveHold, simulated for 1 s.

Toggles of y in 1 s, before after
IntegerSignals version 2 (default 1 s clock) 51 (every 20 ms)
BooleanSignals version 2 (default 1 s clock) 51 (every 20 ms)

Modelica.Clocked.Examples.Elementary.RealSignals.AssignClockToSquareWaveHold is bit-identical before and after. Dymola 2026x translates the changed blocks; the local license limit prevented simulation. The test model is part of the ModelicaTest coverage work on branch coverage-improvement.

🤖 Generated with Claude Code

https://claude.ai/code/session_01UmNZkRw6HV9CeBvNk36AzT

…e Real version

The Integer and Boolean versions of AssignClockToSquareWaveHold relate the
clock of their input u to the internal square wave y2 through a "dummy
condition" with identical branches:

  y2 = if u > 0 then not previous(y2) else not previous(y2);

A tool may simplify this to y2 = not previous(y2), which drops the clock
relation. OpenModelica 1.27.1 does, and assigns the default clock (1 s)
to y2: the output toggles once per second instead of at every tick of u.

Use the clocked when-equation with a dummy assignment that the Real
version already uses:

  when Clock() then
     uu = u "Dummy assignment to relate clock of u with y2";
     y2 = not previous(y2);
  end when;

With this change, both outputs toggle at every tick of a 20 ms input clock
in OpenModelica, and the Real example AssignClockToSquareWaveHold is
bit-identical. Dymola 2026x translates the changed blocks.

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