Skip to content

Fix initialization with non-1-indexed array variables #3660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/systems/problem_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@ function add_fallbacks!(
val = @something get(varmap, arrvar, nothing) get(varmap, ttarrvar, nothing) get(
fallbacks, arrvar, nothing) get(fallbacks, ttarrvar, nothing) Some(nothing)
if val !== nothing
val = val[idxs...]
idxs = CartesianIndex(idxs...)
if iscall(val) && operation(val) == StructuralTransformations.change_origin # TODO: remove hack
idxs -= arguments(val)[1] - eachindex(val)[1] # subtract non-1-based offset
end
val = val[idxs]
is_sized_array_symbolic(arrvar) && push!(arrvars, arrvar)
end
else
Expand Down
30 changes: 28 additions & 2 deletions test/odesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1008,11 +1008,37 @@ for sys in [sys1, sys2]
end
end

@testset "Non-1-indexed variable array (issue #2670)" begin
@variables x(t)[0:1] # 0-indexed variable array
@testset "Non-1-indexed variable array (issue #2670 + #3659)" begin
# 0-indexed vector
@variables x(t)[0:1]
@named sys = System([x[0] ~ 0.0, D(x[1]) ~ x[0]], t, [x], [])
@test_nowarn sys = mtkcompile(sys)
@test equations(sys) == [D(x[1]) ~ 0.0]

# 1-indexed vector
@variables x(t)[1:3]
@named M = System([D(x[i]) ~ 0 for i in eachindex(x)], t)
prob = ODEProblem(mtkcompile(M), [x[1] => 1, x[2] => 2, x[3] => 3], (0.0, 1.0))
@test prob[x[1]] == 1
@test prob[x[2]] == 2
@test prob[x[3]] == 3

# non-1-indexed vector
@variables x(t)[3:5]
@named M = System([D(x[i]) ~ 0 for i in eachindex(x)], t)
prob = ODEProblem(mtkcompile(M), [x[3] => 3, x[4] => 4, x[5] => 5], (0.0, 1.0))
@test prob[x[3]] == 3
@test prob[x[4]] == 4
@test prob[x[5]] == 5

# non-(1,1)-indexed matrix
@variables x(t)[3:4,7:8]
@named M = System(vec([D(x[i]) ~ 0 for i in eachindex(x)]), t)
prob = ODEProblem(mtkcompile(M), [x[3,7] => 37, x[3,8] => 38, x[4,7] => 47, x[4,8] => 48], (0.0, 1.0))
@test prob[x[3,7]] == 37
@test prob[x[3,8]] == 38
@test prob[x[4,7]] == 47
@test prob[x[4,8]] == 48
end

# Namespacing of array variables
Expand Down
Loading