Skip to content

Do not distribute shifts into operator terms other than Shift and Differential - #5226

Merged
AayushSabharwal merged 1 commit into
masterfrom
mtkbase-no-shift-into-operators
Sep 29, 2026
Merged

AayushSabharwal merged 1 commit into
masterfrom
mtkbase-no-shift-into-operators

Conversation

@baggepinnen

Copy link
Copy Markdown
Contributor

Split out of #5173.

Summary

distribute_shift distributed a shift into every operator term except Sample and Hold. An operator term such as a clock change defined by another package (SynchToolkit.Latest(x, init)) denotes the value of the signal at the current tick of the receiving clock and is a symbol of its own, so distributing the back-shift that the discrete reassembly of ModelingToolkitTearing performs into it produces a reference to a nonexistent xₜ₋₁. The default for Operator subtypes is now false; Shift and Differential keep distributing, as before.

Behaviour change

Only operators defined outside ModelingToolkitBase are affected: Sample, Hold, Pre and Initial were already excluded. A package whose operator term must be entered by shifts can define distribute_shift_into_operator(::MyOperator) = true.

Tests

A regression test with an operator defined in the test file checks that the shift of a sum is distributed to the variable but not into the operator term, and that Sample, Hold, Shift and Differential behave as before.

🤖 Generated with Claude Code

https://claude.ai/code/session_01QBgBrXDidWG1G2KF64gAte

…Differential`

`distribute_shift` distributed a shift into every operator term except `Sample` and
`Hold`. An operator term such as a clock change defined by another package denotes the
value of its signal at the current tick and is a symbol of its own; distributing the
back-shift of the discrete reassembly into it produced a reference to a nonexistent
history variable. The default for operators is now `false`, with `Shift` and
`Differential` as the exceptions.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QBgBrXDidWG1G2KF64gAte
@github-actions

Copy link
Copy Markdown
Contributor

Benchmark Results (Julia vlts)

Time benchmarks
master 3db6d36... master / 3db6d36...
ODEProblem 3.15 ± 0.11 ms 3.15 ± 0.098 ms 1 ± 0.047
init 0.0544 ± 0.0081 ms 0.054 ± 0.014 ms 1.01 ± 0.29
large_parameter_init/ODEProblem 8.69 ± 0.87 ms 8.63 ± 0.89 ms 1.01 ± 0.14
large_parameter_init/init 0.0636 ± 0.021 ms 0.057 ± 0.014 ms 1.11 ± 0.45
mtkcompile 5.62 ± 0.3 ms 5.65 ± 0.34 ms 0.994 ± 0.079
sparse_analytical_jacobian/ODEProblem 10.6 ± 0.64 ms 10.6 ± 0.69 ms 1 ± 0.089
sparse_analytical_jacobian/f_iip 0.04 ± 0.01 μs 0.04 ± 0.01 μs 1 ± 0.35
sparse_analytical_jacobian/f_oop 0.239 ± 0.0043 ms 0.238 ± 0.0041 ms 1 ± 0.025
time_to_load 4.17 ± 0.036 s 4.15 ± 0.027 s 1 ± 0.011
Memory benchmarks
master 3db6d36... master / 3db6d36...
ODEProblem 0.0346 M allocs: 1.82 MB 0.0346 M allocs: 1.82 MB 1
init 0.508 k allocs: 0.0846 MB 0.508 k allocs: 0.0846 MB 1
large_parameter_init/ODEProblem 0.147 M allocs: 5.54 MB 0.147 M allocs: 5.54 MB 1
large_parameter_init/init 0.64 k allocs: 0.179 MB 0.64 k allocs: 0.179 MB 1
mtkcompile 0.0594 M allocs: 3.37 MB 0.0594 M allocs: 3.37 MB 1
sparse_analytical_jacobian/ODEProblem 0.106 M allocs: 5.33 MB 0.106 M allocs: 5.33 MB 1
sparse_analytical_jacobian/f_iip 0 allocs: 0 B 0 allocs: 0 B
sparse_analytical_jacobian/f_oop 0.634 k allocs: 19.6 kB 0.634 k allocs: 19.6 kB 1
time_to_load 0.153 k allocs: 14.6 kB 0.153 k allocs: 14.6 kB 1

@github-actions

Copy link
Copy Markdown
Contributor

Benchmark Results (Julia v1)

Time benchmarks
master 3db6d36... master / 3db6d36...
ODEProblem 2.81 ± 0.064 ms 2.82 ± 0.059 ms 0.997 ± 0.031
init 31.4 ± 8.2 μs 0.0323 ± 0.009 ms 0.973 ± 0.37
large_parameter_init/ODEProblem 7.2 ± 0.84 ms 7.16 ± 0.85 ms 1.01 ± 0.17
large_parameter_init/init 0.0357 ± 0.0033 ms 0.0354 ± 0.0034 ms 1.01 ± 0.13
mtkcompile 5.06 ± 0.33 ms 4.95 ± 0.33 ms 1.02 ± 0.095
sparse_analytical_jacobian/ODEProblem 9.25 ± 0.7 ms 9.11 ± 0.63 ms 1.01 ± 0.1
sparse_analytical_jacobian/f_iip 0.04 ± 0.01 μs 0.04 ± 0.01 μs 1 ± 0.35
sparse_analytical_jacobian/f_oop 0.0587 ± 0.0062 ms 0.0587 ± 0.0063 ms 0.999 ± 0.15
time_to_load 3.13 ± 0.043 s 3.1 ± 0.053 s 1.01 ± 0.022
Memory benchmarks
master 3db6d36... master / 3db6d36...
ODEProblem 0.034 M allocs: 1.61 MB 0.034 M allocs: 1.61 MB 1
init 0.475 k allocs: 0.0602 MB 0.475 k allocs: 0.0602 MB 1
large_parameter_init/ODEProblem 0.143 M allocs: 4.86 MB 0.144 M allocs: 4.86 MB 1
large_parameter_init/init 0.757 k allocs: 0.166 MB 0.757 k allocs: 0.166 MB 1
mtkcompile 0.0557 M allocs: 2.63 MB 0.0558 M allocs: 2.64 MB 0.994
sparse_analytical_jacobian/ODEProblem 0.102 M allocs: 4.24 MB 0.102 M allocs: 4.24 MB 1
sparse_analytical_jacobian/f_iip 0 allocs: 0 B 0 allocs: 0 B
sparse_analytical_jacobian/f_oop 0.848 k allocs: 27 kB 0.848 k allocs: 27 kB 1
time_to_load 0.201 k allocs: 12.1 kB 0.201 k allocs: 12.1 kB 1

@AayushSabharwal
AayushSabharwal merged commit d804063 into master Sep 29, 2026
101 of 111 checks passed
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.

2 participants