Build shifted variables in var_derivative! for discrete systems - #163
Merged
Merged
Conversation
`StateSelection.var_derivative!(::TearingState, ::Int)` built the new variable as `Differential(iv)(v)` for every system. The variables of a discrete system are related by shifts, and the differential form is not recognized by the discrete reassembly: the alias elimination of `[u ~ p, y ~ u(k - 1), z ~ y]` requests the shifted form of the alias target `y(k + 1)` and left `Differential(t)(y)` in the system, so that `mtkcompile` returned two unknowns and one equation. The method now builds `simplify_shifts(Shift(iv, 1)(v))` when the structure is discrete. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QBgBrXDidWG1G2KF64gAte
oscardssmith
approved these changes
Sep 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
StateSelection.var_derivative!(::TearingState, ::Int)always builds the new variable asDifferential(iv)(v). In a discrete system the variables are related by shifts, so the differential form is not recognized by the discrete reassembly. The method now buildssimplify_shifts(Shift(iv, 1)(v))when the structure is discrete.Reproduction
eliminate_perfect_aliases!callsvar_derivative!when a variable that is shifted in the system is eliminated in favour of an alias target that has no shifted form yet:The alias
z ~ yputsu(whose shifted formu(k + 1)is a variable of the system aftershift_discrete_system) in the same alias group asy(k + 1)andz(k + 1). Withy(k + 1)as the target, its shifted form is requested fromvar_derivative!, which returnsDifferential(t)(y). The compiled system then has two unknowns and one equation, andDiscreteProblemfails with "Equations (1), unknowns (2), and initial conditions (2) are of different lengths". Withz ~ 2yin place of the alias the compile is correct, since no alias elimination takes place.With this change the system compiles to the single unknown
ywithShift(t, 1)(y) ~ yₜ₊₁,yₜ₊₁ ~ p,z ~ yanduₜ₋₁ ~ y.This is the structure of every model that uses a one-sample delay whose output is connected to another block, such as
DiscreteComponents.UnitDelay; it was found while runninglinearize_hybridof SciML/ModelingToolkit.jl#5173 over the DiscreteComponents examples (finding 3 of this comment).Tests
A regression test in
lib/ModelingToolkitTearing/test/runtests.jlcompiles the model above, checks that a single unknown remains and that noDifferentialappears in the equations or observed equations, and checks the values ofyandzafter the first tick of aDiscreteProblem. Thediscrete_system,implicit_discrete_systemandclocktests of ModelingToolkit pass with this change.🤖 Generated with Claude Code
https://claude.ai/code/session_01QBgBrXDidWG1G2KF64gAte