Skip to content

Commit c208c11

Browse files
Merge pull request #4378 from SciML/as/re-complete
fix: fix old `IndexCache` influencing subsequent `complete` calls
2 parents 33dd257 + 1fc21c1 commit c208c11

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

lib/ModelingToolkitBase/src/systems/abstractsystem.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,9 @@ function complete(
717717
@set! sys.parameter_bindings_graph = nothing
718718
end
719719
if split && has_index_cache(sys)
720+
# The `IndexCache` constructor uses `is_parameter` and family. If the system already
721+
# contains an index cache, it'll get wrong results.
722+
@set! sys.index_cache = nothing
720723
@set! sys.index_cache = IndexCache(sys)
721724
# Ideally we'd do `get_ps` but if `flatten = false`
722725
# we don't get all of them. So we call `parameters`.

lib/ModelingToolkitBase/test/index_cache.jl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using ModelingToolkitBase, SymbolicIndexingInterface, SciMLStructures
2-
using ModelingToolkitBase: t_nounits as t
2+
using ModelingToolkitBase: t_nounits as t, D_nounits as D, SymbolicDiscreteCallback
3+
using Symbolics: unwrap
4+
using Setfield: @set!
35
using Test
46

57
# Ensure indexes of array symbolics are cached appropriately
@@ -123,3 +125,17 @@ end
123125
@test length(parameters(ss)) == 1
124126
@test !is_timeseries_parameter(ss, p_1)
125127
end
128+
129+
@testset "Old index cache doesn't influence new index cache construction" begin
130+
@variables x(t)
131+
@discretes d(t)
132+
@named sys = System([D(x) ~ t + 2], t)
133+
sys = complete(sys)
134+
@set! sys.discrete_events = [SymbolicDiscreteCallback(x > 1, [d ~ Pre(d) + 1]; discrete_parameters = [d])]
135+
@set! sys.ps = [unwrap(d)]
136+
# This used to throw, since `d` wasn't a parameter before and thus not present in
137+
# the `IndexCache`. This failed the construction of the subsequent `IndexCache`, since
138+
# it uses `is_parameter` to check if `discrete_parameters` are in the parameters of
139+
# the system.
140+
@test_nowarn complete(sys)
141+
end

0 commit comments

Comments
 (0)