Skip to content

Commit

Permalink
Merge pull request #906 from AayushSabharwal/as/remake-bug
Browse files Browse the repository at this point in the history
fix: fix `remake` not able to substitute values
  • Loading branch information
ChrisRackauckas authored Jan 16, 2025
2 parents 234dd64 + 8d0c407 commit df84eec
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/remake.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1072,10 +1072,20 @@ function _updated_u0_p_symmap(prob, u0, ::Val{true}, p, ::Val{true}, t0)
varmap[only(independent_variable_symbols(prob))] = t0
end
for (k, v) in u0
u0[k] = symbolic_type(v) === NotSymbolic() ? v : symbolic_evaluate(v, varmap)
v = symbolic_type(v) === NotSymbolic() ? v : symbolic_evaluate(v, varmap)
# if `symbolic_evaluate` can't get us a concrete value,
# use the old one from `prob`.
if symbolic_type(v) != NotSymbolic()
v = getu(prob, k)(prob)
end
u0[k] = v
end
for (k, v) in p
p[k] = symbolic_type(v) === NotSymbolic() ? v : symbolic_evaluate(v, varmap)
v = symbolic_type(v) === NotSymbolic() ? v : symbolic_evaluate(v, varmap)
if symbolic_type(v) != NotSymbolic()
v = getu(prob, k)(prob)
end
p[k] = v
end
return remake_buffer(prob, state_values(prob), keys(u0), values(u0)),
remake_buffer(prob, parameter_values(prob), keys(p), values(p))
Expand Down
17 changes: 17 additions & 0 deletions test/downstream/modelingtoolkit_remake.jl
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,20 @@ end
@test prob2.ps[p] 3.0
end
end

# https://github.com/SciML/ModelingToolkit.jl/issues/3326
@testset "Remake with non-substitute-able values" begin
@variables x1(t) x2(t) y(t)
@parameters k2 p Gamma y0 d k1
@mtkbuild sys = ODESystem([D(y) ~ p - d * y, D(x1) ~ -k1 * x1 + k2 * (Gamma - x1), x2 ~ Gamma - x1], t; defaults = Dict(y => y0, Gamma => x1 + x2))
u0 = [x1 => 1.0, x2 => 2.0]
p0 = [p => 10.0, d => 5.0, y0 => 3.0, k1 => 1.0, k2 => 2.0]
prob = ODEProblem(sys, u0, (0.0, 1.0), p0)
u0_new = [x1 => 0.1]
ps_new = [y0 => 0.3, p => 100.0]
prob2 = remake(prob; u0 = u0_new, p = ps_new)
@test prob2[x1] 0.1
@test prob2[y] 0.3
# old value retained
@test prob2.ps[Gamma] 3.0
end

0 comments on commit df84eec

Please sign in to comment.