Skip to content

Commit 5b81a11

Browse files
fix: update initials with non-symbolic u0 in remake
1 parent 606a043 commit 5b81a11

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/systems/nonlinear/initializesystem.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,17 @@ end
655655
function SciMLBase.late_binding_update_u0_p(
656656
prob, sys::AbstractSystem, u0, p, t0, newu0, newp)
657657
u0 === missing && return newu0, (p === missing ? copy(newp) : newp)
658-
eltype(u0) <: Pair || return newu0, (p === missing ? copy(newp) : newp)
658+
if !(eltype(u0) <: Pair)
659+
p === missing || return newu0, newp
660+
newu0 === nothing && return newu0, newp
661+
newp = p === missing ? copy(newp) : newp
662+
initials, repack, alias = SciMLStructures.canonicalize(
663+
SciMLStructures.Initials(), newp)
664+
initials = DiffEqBase.promote_u0(initials, newu0, t0)
665+
newp = repack(initials)
666+
setp(sys, Initial.(unknowns(sys)))(newp, newu0)
667+
return newu0, newp
668+
end
659669

660670
newp = p === missing ? copy(newp) : newp
661671
newu0 = DiffEqBase.promote_u0(newu0, newp, t0)

test/initializationsystem.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,3 +1476,14 @@ end
14761476
@test sol.ps[Γ[1]] 5.0
14771477
end
14781478
end
1479+
1480+
@testset "Issue#3504: Update initials when `remake` called with non-symbolic `u0`" begin
1481+
@variables x(t) y(t)
1482+
@parameters c1 c2
1483+
@mtkbuild sys = ODESystem([D(x) ~ -c1 * x + c2 * y, D(y) ~ c1 * x - c2 * y], t)
1484+
prob1 = ODEProblem(sys, [1.0, 2.0], (0.0, 1.0), [c1 => 1.0, c2 => 2.0])
1485+
prob2 = remake(prob1, u0 = [2.0, 3.0])
1486+
integ1 = init(prob1, Tsit5())
1487+
integ2 = init(prob2, Tsit5())
1488+
@test integ2.u [2.0, 3.0]
1489+
end

0 commit comments

Comments
 (0)