@@ -11,7 +11,7 @@ function generate_initializesystem(sys::AbstractTimeDependentSystem;
11
11
default_dd_guess = Bool (0 ),
12
12
algebraic_only = false ,
13
13
check_units = true , check_defguess = false ,
14
- name = nameof (sys), extra_metadata = (;), kwargs... )
14
+ name = nameof (sys), kwargs... )
15
15
eqs = equations (sys)
16
16
if ! (eqs isa Vector{Equation})
17
17
eqs = Equation[x for x in eqs if x isa Equation]
@@ -143,17 +143,15 @@ function generate_initializesystem(sys::AbstractTimeDependentSystem;
143
143
for k in keys (defs)
144
144
defs[k] = substitute (defs[k], paramsubs)
145
145
end
146
- meta = InitializationSystemMetadata (
147
- anydict (u0map), anydict (pmap), additional_guesses,
148
- additional_initialization_eqs, extra_metadata, nothing )
146
+
149
147
return NonlinearSystem (eqs_ics,
150
148
vars,
151
149
pars;
152
150
defaults = defs,
153
151
checks = check_units,
154
152
parameter_dependencies = new_parameter_deps,
155
153
name,
156
- metadata = meta ,
154
+ is_initializesystem = true ,
157
155
kwargs... )
158
156
end
159
157
@@ -169,7 +167,7 @@ function generate_initializesystem(sys::AbstractTimeIndependentSystem;
169
167
guesses = Dict (),
170
168
algebraic_only = false ,
171
169
check_units = true , check_defguess = false ,
172
- name = nameof (sys), extra_metadata = (;), kwargs... )
170
+ name = nameof (sys), kwargs... )
173
171
eqs = equations (sys)
174
172
trueobs, eqs = unhack_observed (observed (sys), eqs)
175
173
vars = unique ([unknowns (sys); getfield .(trueobs, :lhs )])
@@ -244,17 +242,15 @@ function generate_initializesystem(sys::AbstractTimeIndependentSystem;
244
242
for k in keys (defs)
245
243
defs[k] = substitute (defs[k], paramsubs)
246
244
end
247
- meta = InitializationSystemMetadata (
248
- anydict (u0map), anydict (pmap), additional_guesses,
249
- additional_initialization_eqs, extra_metadata, nothing )
245
+
250
246
return NonlinearSystem (eqs_ics,
251
247
vars,
252
248
pars;
253
249
defaults = defs,
254
250
checks = check_units,
255
251
parameter_dependencies = new_parameter_deps,
256
252
name,
257
- metadata = meta ,
253
+ is_initializesystem = true ,
258
254
kwargs... )
259
255
end
260
256
@@ -436,64 +432,6 @@ function _has_delays(sys::AbstractSystem, ex, banned)
436
432
return any (x -> _has_delays (sys, x, banned), args)
437
433
end
438
434
439
- struct ReconstructInitializeprob
440
- getter:: Any
441
- setter:: Any
442
- end
443
-
444
- function ReconstructInitializeprob (
445
- srcsys:: AbstractSystem , dstsys:: AbstractSystem )
446
- syms = reduce (
447
- vcat, reorder_parameters (dstsys, parameters (dstsys));
448
- init = [])
449
- getter = getu (srcsys, syms)
450
- setter = setp_oop (dstsys, syms)
451
- return ReconstructInitializeprob (getter, setter)
452
- end
453
-
454
- function (rip:: ReconstructInitializeprob )(srcvalp, dstvalp)
455
- newp = rip. setter (dstvalp, rip. getter (srcvalp))
456
- if state_values (dstvalp) === nothing
457
- return nothing , newp
458
- end
459
- srcu0 = state_values (srcvalp)
460
- T = srcu0 === nothing || isempty (srcu0) ? Union{} : eltype (srcu0)
461
- if parameter_values (dstvalp) isa MTKParameters
462
- if ! isempty (newp. tunable)
463
- T = promote_type (eltype (newp. tunable), T)
464
- end
465
- elseif ! isempty (newp)
466
- T = promote_type (eltype (newp), T)
467
- end
468
- if T == eltype (state_values (dstvalp))
469
- u0 = state_values (dstvalp)
470
- elseif T != Union{}
471
- u0 = T .(state_values (dstvalp))
472
- end
473
- buf, repack, alias = SciMLStructures. canonicalize (SciMLStructures. Tunable (), newp)
474
- if eltype (buf) != T
475
- newbuf = similar (buf, T)
476
- copyto! (newbuf, buf)
477
- newp = repack (newbuf)
478
- end
479
- buf, repack, alias = SciMLStructures. canonicalize (SciMLStructures. Initials (), newp)
480
- if eltype (buf) != T
481
- newbuf = similar (buf, T)
482
- copyto! (newbuf, buf)
483
- newp = repack (newbuf)
484
- end
485
- return u0, newp
486
- end
487
-
488
- struct InitializationSystemMetadata
489
- u0map:: Dict{Any, Any}
490
- pmap:: Dict{Any, Any}
491
- additional_guesses:: Dict{Any, Any}
492
- additional_initialization_eqs:: Vector{Equation}
493
- extra_metadata:: NamedTuple
494
- oop_reconstruct_u0_p:: Union{Nothing, ReconstructInitializeprob}
495
- end
496
-
497
435
function get_possibly_array_fallback_singletons (varmap, p)
498
436
if haskey (varmap, p)
499
437
return varmap[p]
@@ -543,22 +481,19 @@ function SciMLBase.remake_initialization_data(
543
481
if u0 === missing && p === missing
544
482
return odefn. initialization_data
545
483
end
484
+
485
+ oldinitdata = odefn. initialization_data
486
+
546
487
if ! (eltype (u0) <: Pair ) && ! (eltype (p) <: Pair )
547
- oldinitdata = odefn. initialization_data
548
488
oldinitdata === nothing && return nothing
549
489
550
490
oldinitprob = oldinitdata. initializeprob
551
491
oldinitprob === nothing && return nothing
552
- if ! SciMLBase. has_sys (oldinitprob. f) || ! (oldinitprob. f. sys isa NonlinearSystem)
553
- return oldinitdata
554
- end
555
- oldinitsys = oldinitprob. f. sys
556
- meta = get_metadata (oldinitsys)
557
- if meta isa InitializationSystemMetadata && meta. oop_reconstruct_u0_p != = nothing
558
- reconstruct_fn = meta. oop_reconstruct_u0_p
559
- else
560
- reconstruct_fn = ReconstructInitializeprob (sys, oldinitsys)
561
- end
492
+
493
+ meta = oldinitdata. metadata
494
+ meta isa InitializationMetadata || return oldinitdata
495
+
496
+ reconstruct_fn = meta. oop_reconstruct_u0_p
562
497
# the history function doesn't matter because `reconstruct_fn` is only going to
563
498
# update the values of parameters, which aren't time dependent. The reason it
564
499
# is called is because `Initial` parameters are calculated from the corresponding
@@ -569,16 +504,15 @@ function SciMLBase.remake_initialization_data(
569
504
if oldinitprob. f. resid_prototype === nothing
570
505
newf = oldinitprob. f
571
506
else
572
- newf = NonlinearFunction{
573
- SciMLBase. isinplace (oldinitprob. f), SciMLBase. specialization (oldinitprob. f)}(
574
- oldinitprob. f;
507
+ newf = remake (oldinitprob. f;
575
508
resid_prototype = calculate_resid_prototype (
576
509
length (oldinitprob. f. resid_prototype), new_initu0, new_initp))
577
510
end
578
511
initprob = remake (oldinitprob; f = newf, u0 = new_initu0, p = new_initp)
579
512
return SciMLBase. OverrideInitData (initprob, oldinitdata. update_initializeprob!,
580
- oldinitdata. initializeprobmap, oldinitdata. initializeprobpmap)
513
+ oldinitdata. initializeprobmap, oldinitdata. initializeprobpmap; metadata = oldinitdata . metadata )
581
514
end
515
+
582
516
dvs = unknowns (sys)
583
517
ps = parameters (sys)
584
518
u0map = to_varmap (u0, dvs)
@@ -592,16 +526,13 @@ function SciMLBase.remake_initialization_data(
592
526
use_scc = true
593
527
initialization_eqs = Equation[]
594
528
595
- if SciMLBase. has_initializeprob (odefn)
596
- oldsys = odefn. initialization_data. initializeprob. f. sys
597
- meta = get_metadata (oldsys)
598
- if meta isa InitializationSystemMetadata
599
- u0map = merge (meta. u0map, u0map)
600
- pmap = merge (meta. pmap, pmap)
601
- merge! (guesses, meta. additional_guesses)
602
- use_scc = get (meta. extra_metadata, :use_scc , true )
603
- initialization_eqs = meta. additional_initialization_eqs
604
- end
529
+ if oldinitdata != = nothing && oldinitdata. metadata isa InitializationMetadata
530
+ meta = oldinitdata. metadata
531
+ u0map = merge (meta. u0map, u0map)
532
+ pmap = merge (meta. pmap, pmap)
533
+ merge! (guesses, meta. guesses)
534
+ use_scc = meta. use_scc
535
+ initialization_eqs = meta. additional_initialization_eqs
605
536
else
606
537
# there is no initializeprob, so the original problem construction
607
538
# had no solvable parameters and had the differential variables
@@ -662,19 +593,22 @@ function SciMLBase.late_binding_update_u0_p(
662
593
if ! (eltype (u0) <: Pair )
663
594
# if `p` is not provided or is symbolic
664
595
p === missing || eltype (p) <: Pair || return newu0, newp
665
- newu0 === nothing && return newu0, newp
666
- all (is_parameter (sys, Initial (x)) for x in unknowns (sys)) || return newu0, newp
596
+ (newu0 === nothing || isempty (newu0)) && return newu0, newp
597
+ initdata = prob. f. initialization_data
598
+ initdata === nothing && return newu0, newp
599
+ meta = initdata. metadata
600
+ meta isa InitializationMetadata || return newu0, newp
667
601
newp = p === missing ? copy (newp) : newp
668
602
initials, repack, alias = SciMLStructures. canonicalize (
669
603
SciMLStructures. Initials (), newp)
670
604
if eltype (initials) != eltype (newu0)
671
605
initials = DiffEqBase. promote_u0 (initials, newu0, t0)
672
606
newp = repack (initials)
673
607
end
674
- if length (newu0) != length (unknowns (sys) )
675
- throw (ArgumentError (" Expected `newu0` to be of same length as unknowns ($(length (unknowns (sys) )) ). Got $(typeof (newu0)) of length $(length (newu0)) " ))
608
+ if length (newu0) != length (prob . u0 )
609
+ throw (ArgumentError (" Expected `newu0` to be of same length as unknowns ($(length (prob . u0 )) ). Got $(typeof (newu0)) of length $(length (newu0)) " ))
676
610
end
677
- setp (sys, Initial .( unknowns (sys))) (newp, newu0)
611
+ meta . set_initial_unknowns! (newp, newu0)
678
612
return newu0, newp
679
613
end
680
614
714
648
Check if the given system is an initialization system.
715
649
"""
716
650
function is_initializesystem (sys:: AbstractSystem )
717
- sys isa NonlinearSystem && get_metadata (sys) isa InitializationSystemMetadata
651
+ has_is_initializesystem ( sys) && get_is_initializesystem (sys)
718
652
end
719
653
720
654
"""
0 commit comments