Skip to content

Commit

Permalink
fix: recalculate resid_prototype in remake_initialization_data
Browse files Browse the repository at this point in the history
  • Loading branch information
AayushSabharwal committed Dec 2, 2024
1 parent b063e6b commit c578da1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/systems/nonlinear/initializesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,16 @@ function SciMLBase.remake_initialization_data(sys::ODESystem, odefn, u0, t0, p,
newp = remake_buffer(
oldinitprob.f.sys, parameter_values(oldinitprob), pidxs, pvals)
end
initprob = remake(oldinitprob; u0 = newu0, p = newp)
if oldinitprob.f.resid_prototype === nothing
newf = oldinitprob.f
else
newf = NonlinearFunction{
SciMLBase.isinplace(oldinitprob.f), SciMLBase.specialization(oldinitprob.f)}(
oldinitprob.f;
resid_prototype = calculate_resid_prototype(
length(oldinitprob.f.resid_prototype), newu0, newp))
end
initprob = remake(oldinitprob; f = newf, u0 = newu0, p = newp)
return SciMLBase.OverrideInitData(initprob, odefn.update_initializeprob!,
odefn.initializeprobmap, odefn.initializeprobpmap)
end
Expand Down
17 changes: 17 additions & 0 deletions test/initializationsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1032,3 +1032,20 @@ end
@test prob3.f.initialization_data !== nothing
@test init(prob3)[x] 0.5
end

@testset "Issue#3246: type promotion with parameter dependent initialization_eqs" begin
@variables x(t)=1 y(t)=1
@parameters a = 1
@named sys = ODESystem([D(x) ~ 0, D(y) ~ x + a], t; initialization_eqs = [y ~ a])

ssys = structural_simplify(sys)
prob = ODEProblem(ssys, [], (0, 1), [])

@test SciMLBase.successful_retcode(solve(prob))

seta = setsym_oop(prob, [a])
(newu0, newp) = seta(prob, ForwardDiff.Dual{ForwardDiff.Tag{:tag, Float64}}.([1.0], 1))
newprob = remake(prob, u0 = newu0, p = newp)

@test SciMLBase.successful_retcode(solve(newprob))
end

0 comments on commit c578da1

Please sign in to comment.