Forward-shift whole-array references in shift_discrete_system - #162
Closed
greekera1000 wants to merge 1 commit into
Closed
greekera1000 wants to merge 1 commit into
greekera1000 wants to merge 1 commit into
Conversation
Contributor
|
Did you see #161 ? |
Author
|
sorry , I hadn't seen #161. It's the same root cause and fix, and yours already includes tests, so I'll close this one in favor of it. For what it's worth, I independently verified locally that the reproducer from SciML/ModelingToolkit.jl#5169 gives |
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.
Fixes SciML/ModelingToolkit.jl#5169.
Problem
When a discrete partition uses an array variable as a whole at a previous tick
(e.g.
ud ~ kp * combine(xd(k - 1))with a registeredcombine(::AbstractVector)),the compiled partition's observed equation referenced
xdₜ₋₂instead ofxdₜ₋₁,a variable that is neither an unknown nor a parameter, so codegen failed with
UndefVarError.Cause
shift_discrete_systemcollects variables withsearch_variables!, which descendsthrough
Shiftand finds the whole arrayxd. It then skips anything not infullvars, butfullvarsonly holds scalarized elements (xd[i],Shift(-1)(xd)[i]), never the whole array. So the elements were shifted forwardwhile the whole-array reference
Shift(-1)(xd)insidecombine(...)was left at -1.When
udlater became observed,backshift_exprapplied anotherShift(-1),giving
Shift(-2)(xd)→xdₜ₋₂.Elementwise use (
xd(k-1)[1]) and scalars were unaffected because those terms arethemselves in
fullvars.Fix
Add
is_scalarized_in: an array-shaped variable counts as present if all its scalarelements are in
fullvars, so it gets an entry indiscmapand is shiftedconsistently with its elements.
Testing
Ran the reproducer from the issue with this branch:
[(xdₜ₋₁(t))[2], (xdₜ₋₁(t))[1]]ud(t) ~ combine(xdₜ₋₁(t))*kp(previouslyxdₜ₋₂)disclosure:The root-cause analysis and pr description were done with the help of Claude
I reviewed the change and verified it locally by running the reproducer from the issue.