Skip to content

Commit af02851

Browse files
feat: implement SciMLBase.late_binding_update_u0_p to update Initial parameters
1 parent d355ebb commit af02851

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/systems/nonlinear/initializesystem.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,33 @@ function SciMLBase.remake_initialization_data(
490490
return get(kws, :initialization_data, nothing)
491491
end
492492

493+
function SciMLBase.late_binding_update_u0_p(
494+
prob, sys::AbstractSystem, u0, p, t0, newu0, newp)
495+
u0 === missing && return newu0, newp
496+
eltype(u0) <: Pair || return newu0, newp
497+
498+
newu0 = DiffEqBase.promote_u0(newu0, newp, t0)
499+
tunables, repack, alias = SciMLStructures.canonicalize(SciMLStructures.Tunable(), newp)
500+
tunabes = DiffEqBase.promote_u0(tunables, newu0, t0)
501+
newp = repack(tunables)
502+
503+
allsyms = all_symbols(sys)
504+
for (k, v) in u0
505+
v === nothing && continue
506+
(symbolic_type(v) == NotSymbolic() && !is_array_of_symbolics(v)) || continue
507+
if k isa Symbol
508+
k2 = symbol_to_symbolic(sys, k; allsyms)
509+
# if it is returned as-is, there is no match so skip it
510+
k2 === k && continue
511+
k = k2
512+
end
513+
is_parameter(sys, Initial(k)) || continue
514+
setp(sys, Initial(k))(newp, v)
515+
end
516+
517+
return newu0, newp
518+
end
519+
493520
"""
494521
Counteracts the CSE/array variable hacks in `symbolics_tearing.jl` so it works with
495522
initialization.

0 commit comments

Comments
 (0)