Skip to content

Commit 0ceeecc

Browse files
committed
Fix initialization with non-1-indexed array variables
1 parent a8ed1c8 commit 0ceeecc

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/systems/problem_utils.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,11 @@ function add_fallbacks!(
161161
val = @something get(varmap, arrvar, nothing) get(varmap, ttarrvar, nothing) get(
162162
fallbacks, arrvar, nothing) get(fallbacks, ttarrvar, nothing) Some(nothing)
163163
if val !== nothing
164-
val = val[idxs...]
164+
idxs = CartesianIndex(idxs...)
165+
if iscall(val) && operation(val) == StructuralTransformations.change_origin # TODO: remove hack
166+
idxs -= arguments(val)[1] - eachindex(val)[1] # subtract non-1-based offset
167+
end
168+
val = val[idxs]
165169
is_sized_array_symbolic(arrvar) && push!(arrvars, arrvar)
166170
end
167171
else

test/odesystem.jl

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,11 +1008,37 @@ for sys in [sys1, sys2]
10081008
end
10091009
end
10101010

1011-
@testset "Non-1-indexed variable array (issue #2670)" begin
1012-
@variables x(t)[0:1] # 0-indexed variable array
1011+
@testset "Non-1-indexed variable array (issue #2670 + #3659)" begin
1012+
# 0-indexed vector
1013+
@variables x(t)[0:1]
10131014
@named sys = System([x[0] ~ 0.0, D(x[1]) ~ x[0]], t, [x], [])
10141015
@test_nowarn sys = mtkcompile(sys)
10151016
@test equations(sys) == [D(x[1]) ~ 0.0]
1017+
1018+
# 1-indexed vector
1019+
@variables x(t)[1:3]
1020+
@named M = System([D(x[i]) ~ 0 for i in eachindex(x)], t)
1021+
prob = ODEProblem(mtkcompile(M), [x[1] => 1, x[2] => 2, x[3] => 3], (0.0, 1.0))
1022+
@test prob[x[1]] == 1
1023+
@test prob[x[2]] == 2
1024+
@test prob[x[3]] == 3
1025+
1026+
# non-1-indexed vector
1027+
@variables x(t)[3:5]
1028+
@named M = System([D(x[i]) ~ 0 for i in eachindex(x)], t)
1029+
prob = ODEProblem(mtkcompile(M), [x[3] => 3, x[4] => 4, x[5] => 5], (0.0, 1.0))
1030+
@test prob[x[3]] == 3
1031+
@test prob[x[4]] == 4
1032+
@test prob[x[5]] == 5
1033+
1034+
# non-(1,1)-indexed matrix
1035+
@variables x(t)[3:4,7:8]
1036+
@named M = System(vec([D(x[i]) ~ 0 for i in eachindex(x)]), t)
1037+
prob = ODEProblem(mtkcompile(M), [x[3,7] => 37, x[3,8] => 38, x[4,7] => 47, x[4,8] => 48], (0.0, 1.0))
1038+
@test prob[x[3,7]] == 37
1039+
@test prob[x[3,8]] == 38
1040+
@test prob[x[4,7]] == 47
1041+
@test prob[x[4,8]] == 48
10161042
end
10171043

10181044
# Namespacing of array variables

0 commit comments

Comments
 (0)