-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
remake.jl
721 lines (643 loc) · 24 KB
/
remake.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
@generated function struct_as_namedtuple(st)
A = (Expr(:(=), n, :(st.$n)) for n in setdiff(fieldnames(st), (:kwargs,)))
Expr(:tuple, A...)
end
Base.@pure function remaker_of(prob::T) where {T <: AbstractSciMLProblem}
parameterless_type(T){isinplace(prob)}
end
Base.@pure remaker_of(alg::T) where {T} = parameterless_type(T)
# Define `remaker_of` for the types that does not (make sense to)
# implement `isinplace` trait:
for T in [
NoiseProblem,
SplitFunction, # TODO: use isinplace path for type-stability
TwoPointBVPFunction # EnsembleProblem,
]
@eval remaker_of(::$T) = $T
end
"""
remake(thing; <keyword arguments>)
Re-construct `thing` with new field values specified by the keyword
arguments.
"""
function remake(thing; kwargs...)
_remake_internal(thing; kwargs...)
end
function _remake_internal(thing; kwargs...)
T = remaker_of(thing)
named_thing = struct_as_namedtuple(thing)
if :kwargs ∈ fieldnames(typeof(thing))
if :args ∈ fieldnames(typeof(thing))
named_thing = Base.structdiff(named_thing, (; args = ()))
if :args ∉ keys(kwargs)
k = Base.structdiff(named_thing, (; args = ()))
if :kwargs ∉ keys(kwargs)
T(; named_thing..., thing.kwargs..., kwargs...)
else
T(; named_thing..., kwargs[:kwargs]...)
end
else
kwargs2 = Base.structdiff((; kwargs...), (; args = ()))
if :kwargs ∉ keys(kwargs)
T(kwargs[:args]...; named_thing..., thing.kwargs..., kwargs2...)
else
T(kwargs[:args]...; named_thing..., kwargs2[:kwargs]...)
end
end
else
if :kwargs ∉ keys(kwargs)
T(; named_thing..., thing.kwargs..., kwargs...)
else
T(; named_thing..., kwargs[:kwargs]...)
end
end
else
T(; named_thing..., kwargs...)
end
end
function isrecompile(prob::ODEProblem{iip}) where {iip}
(prob.f isa ODEFunction) ? !isfunctionwrapper(prob.f.f) : true
end
"""
remake(prob::AbstractSciMLProblem; u0 = missing, p = missing, interpret_symbolicmap = true, use_defaults = false)
Remake the given problem `prob`. If `u0` or `p` are given, they will be used instead
of the unknowns/parameters of the problem. Either of them can be a symbolic map if
the problem has an associated system. If `interpret_symbolicmap == false`, `p` will never
be interpreted as a symbolic map and used as-is for parameters. `use_defaults` allows
controlling whether the default values from the system will be used to calculate missing
values in the symbolic map passed to `u0` or `p`. It is only valid when either `u0` or
`p` have been explicitly provided as a symbolic map and the problem has an associated
system.
"""
function remake(prob::AbstractSciMLProblem; u0 = missing,
p = missing, interpret_symbolicmap = true, use_defaults = false, kwargs...)
u0, p = updated_u0_p(prob, u0, p; interpret_symbolicmap, use_defaults)
_remake_internal(prob; kwargs..., u0, p)
end
function remake(prob::AbstractIntervalNonlinearProblem; p = missing,
interpret_symbolicmap = true, use_defaults = false, kwargs...)
_, p = updated_u0_p(prob, [], p; interpret_symbolicmap, use_defaults)
_remake_internal(prob; kwargs..., p)
end
function remake(prob::AbstractNoiseProblem; kwargs...)
_remake_internal(prob; kwargs...)
end
function remake(
prob::AbstractIntegralProblem; p = missing, interpret_symbolicmap = true, use_defaults = false, kwargs...)
_, p = updated_u0_p(prob, nothing, p; interpret_symbolicmap, use_defaults)
_remake_internal(prob; kwargs..., p)
end
"""
remake(prob::ODEProblem; f = missing, u0 = missing, tspan = missing,
p = missing, kwargs = missing, _kwargs...)
Remake the given `ODEProblem`.
If `u0` or `p` are given as symbolic maps `ModelingToolkit.jl` has to be loaded.
"""
function remake(prob::ODEProblem; f = missing,
u0 = missing,
tspan = missing,
p = missing,
kwargs = missing,
interpret_symbolicmap = true,
use_defaults = false,
_kwargs...)
if tspan === missing
tspan = prob.tspan
end
newu0, newp = updated_u0_p(prob, u0, p; interpret_symbolicmap, use_defaults)
iip = isinplace(prob)
if f === missing
initializeprob, initializeprobmap = remake_initializeprob(
prob.f.sys, prob.f, u0 === missing ? newu0 : u0,
tspan[1], p === missing ? newp : p)
if specialization(prob.f) === FunctionWrapperSpecialize
ptspan = promote_tspan(tspan)
if iip
_f = ODEFunction{iip, FunctionWrapperSpecialize}(
wrapfun_iip(
unwrapped_f(prob.f.f),
(newu0, newu0, newp,
ptspan[1]));
initializeprob, initializeprobmap)
else
_f = ODEFunction{iip, FunctionWrapperSpecialize}(
wrapfun_oop(
unwrapped_f(prob.f.f),
(newu0, newp,
ptspan[1]));
initializeprob, initializeprobmap)
end
else
_f = prob.f
if __has_initializeprob(_f)
props = getproperties(_f)
@reset props.initializeprob = initializeprob
props = values(props)
_f = parameterless_type(_f){
iip, specialization(_f), map(typeof, props)...}(props...)
end
if __has_initializeprobmap(_f)
props = getproperties(_f)
@reset props.initializeprobmap = initializeprobmap
props = values(props)
_f = parameterless_type(_f){
iip, specialization(_f), map(typeof, props)...}(props...)
end
end
elseif f isa AbstractODEFunction
_f = f
elseif specialization(prob.f) === FunctionWrapperSpecialize
ptspan = promote_tspan(tspan)
if iip
_f = ODEFunction{iip, FunctionWrapperSpecialize}(wrapfun_iip(f,
(newu0, newu0, newp,
ptspan[1])))
else
_f = ODEFunction{iip, FunctionWrapperSpecialize}(wrapfun_oop(f,
(newu0, newp, ptspan[1])))
end
else
_f = ODEFunction{isinplace(prob), specialization(prob.f)}(f)
end
if kwargs === missing
ODEProblem{isinplace(prob)}(
_f, newu0, tspan, newp, prob.problem_type; prob.kwargs...,
_kwargs...)
else
ODEProblem{isinplace(prob)}(_f, newu0, tspan, newp, prob.problem_type; kwargs...)
end
end
"""
remake_initializeprob(sys, scimlfn, u0, t0, p)
Re-create the initialization problem present in the function `scimlfn`, using the
associated system `sys`, and the new values of `u0`, initial time `t0` and `p`. By
default, returns `nothing, nothing` if `scimlfn` does not have an initialization
problem, and `scimlfn.initializeprob, scimlfn.initializeprobmap` if it does.
"""
function remake_initializeprob(sys, scimlfn, u0, t0, p)
if !has_initializeprob(scimlfn)
return nothing, nothing
end
return scimlfn.initializeprob, scimlfn.initializeprobmap
end
"""
remake(prob::BVProblem; f = missing, u0 = missing, tspan = missing,
p = missing, kwargs = missing, problem_type = missing, _kwargs...)
Remake the given `BVProblem`.
"""
function remake(prob::BVProblem{uType, tType, iip, nlls}; f = missing, bc = missing,
u0 = missing, tspan = missing, p = missing, kwargs = missing, problem_type = missing,
interpret_symbolicmap = true, use_defaults = false, _kwargs...) where {
uType, tType, iip, nlls}
if tspan === missing
tspan = prob.tspan
end
u0, p = updated_u0_p(prob, u0, p; interpret_symbolicmap, use_defaults)
if problem_type === missing
problem_type = prob.problem_type
end
twopoint = problem_type isa TwoPointBVProblem
if bc === missing
bc = prob.f.bc
end
if f === missing
_f = prob.f
elseif f isa BVPFunction
_f = f
bc = f.bc
elseif specialization(prob.f) === FunctionWrapperSpecialize
ptspan = promote_tspan(tspan)
if iip
_f = BVPFunction{iip, FunctionWrapperSpecialize, twopoint}(
wrapfun_iip(f,
(u0, u0, p, ptspan[1])), bc; prob.f.bcresid_prototype)
else
_f = BVPFunction{iip, FunctionWrapperSpecialize, twopoint}(
wrapfun_oop(f,
(u0, p, ptspan[1])), bc; prob.f.bcresid_prototype)
end
else
_f = BVPFunction{isinplace(prob), specialization(prob.f), twopoint}(f, bc;
prob.f.bcresid_prototype)
end
if kwargs === missing
BVProblem{iip}(
_f, bc, u0, tspan, p; problem_type, nlls = Val(nlls), prob.kwargs...,
_kwargs...)
else
BVProblem{iip}(_f, bc, u0, tspan, p; problem_type, nlls = Val(nlls), kwargs...)
end
end
"""
remake(prob::SDEProblem; f = missing, g = missing, u0 = missing, tspan = missing,
p = missing, noise = missing, noise_rate_prototype = missing,
seed = missing, kwargs = missing, _kwargs...)
Remake the given `SDEProblem`.
"""
function remake(prob::SDEProblem;
f = missing,
g = missing,
u0 = missing,
tspan = missing,
p = missing,
noise = missing,
noise_rate_prototype = missing,
interpret_symbolicmap = true,
use_defaults = false,
seed = missing,
kwargs = missing,
_kwargs...)
if tspan === missing
tspan = prob.tspan
end
u0, p = updated_u0_p(prob, u0, p; interpret_symbolicmap, use_defaults)
if noise === missing
noise = prob.noise
end
if noise_rate_prototype === missing
noise_rate_prototype = prob.noise_rate_prototype
end
if seed === missing
seed = prob.seed
end
if f === missing && g === missing
f = prob.f
g = prob.g
elseif f !== missing && g === missing
g = prob.g
elseif f === missing && g !== missing
if prob.f isa SDEFunction
f = remake(prob.f; g = g)
else
f = SDEFunction(prob.f, g)
end
else
if f isa SDEFunction
f = remake(f; g = g)
else
f = SDEFunction(f, g)
end
end
iip = isinplace(prob)
if kwargs === missing
SDEProblem{iip}(f,
u0,
tspan,
p;
noise,
noise_rate_prototype,
seed,
prob.kwargs...,
_kwargs...)
else
SDEProblem{iip}(f, u0, tspan, p; noise, noise_rate_prototype, seed, kwargs...)
end
end
"""
remake(func::SDEFunction; f = missing, g = missing,
mass_matrix = missing, analytic = missing, kwargs...)
Remake the given `SDEFunction`.
"""
function remake(func::SDEFunction;
f = missing,
g = missing,
mass_matrix = missing,
analytic = missing,
kwargs...)
if f === missing
f = func.f
end
if g === missing
g = func.g
end
if mass_matrix === missing
mass_matrix = func.mass_matrix
end
if analytic === missing
analytic = func.analytic
end
return SDEFunction(f, g; mass_matrix, analytic, kwargs...)
end
"""
remake(prob::OptimizationProblem; f = missing, u0 = missing, p = missing,
lb = missing, ub = missing, int = missing, lcons = missing, ucons = missing,
sense = missing, kwargs = missing, _kwargs...)
Remake the given `OptimizationProblem`.
If `u0` or `p` are given as symbolic maps `ModelingToolkit.jl` has to be loaded.
"""
function remake(prob::OptimizationProblem;
f = missing,
u0 = missing,
p = missing,
lb = missing,
ub = missing,
int = missing,
lcons = missing,
ucons = missing,
sense = missing,
kwargs = missing,
interpret_symbolicmap = true,
use_defaults = false,
_kwargs...)
u0, p = updated_u0_p(prob, u0, p; interpret_symbolicmap, use_defaults)
if f === missing
f = prob.f
end
if lb === missing
lb = prob.lb
end
if ub === missing
ub = prob.ub
end
if int === missing
int = prob.int
end
if lcons === missing
lcons = prob.lcons
end
if ucons === missing
ucons = prob.ucons
end
if sense === missing
sense = prob.sense
end
if kwargs === missing
OptimizationProblem{isinplace(prob)}(f = f, u0 = u0, p = p, lb = lb,
ub = ub, int = int,
lcons = lcons, ucons = ucons,
sense = sense; prob.kwargs..., _kwargs...)
else
OptimizationProblem{isinplace(prob)}(f = f, u0 = u0, p = p, lb = lb,
ub = ub, int = int,
lcons = lcons, ucons = ucons,
sense = sense; kwargs...)
end
end
"""
remake(prob::NonlinearProblem; f = missing, u0 = missing, p = missing,
problem_type = missing, kwargs = missing, _kwargs...)
Remake the given `NonlinearProblem`.
If `u0` or `p` are given as symbolic maps `ModelingToolkit.jl` has to be loaded.
"""
function remake(prob::NonlinearProblem;
f = missing,
u0 = missing,
p = missing,
problem_type = missing,
kwargs = missing,
interpret_symbolicmap = true,
use_defaults = false,
_kwargs...)
u0, p = updated_u0_p(prob, u0, p; interpret_symbolicmap, use_defaults)
if f === missing
f = prob.f
end
if problem_type === missing
problem_type = prob.problem_type
end
if kwargs === missing
NonlinearProblem{isinplace(prob)}(f = f, u0 = u0, p = p,
problem_type = problem_type; prob.kwargs...,
_kwargs...)
else
NonlinearProblem{isinplace(prob)}(f = f, u0 = u0, p = p,
problem_type = problem_type; kwargs...)
end
end
"""
remake(prob::NonlinearLeastSquaresProblem; f = missing, u0 = missing, p = missing,
kwargs = missing, _kwargs...)
Remake the given `NonlinearLeastSquaresProblem`.
"""
function remake(prob::NonlinearLeastSquaresProblem; f = missing, u0 = missing, p = missing,
interpret_symbolicmap = true, use_defaults = false, kwargs = missing, _kwargs...)
u0, p = updated_u0_p(prob, u0, p; interpret_symbolicmap, use_defaults)
if f === missing
f = prob.f
end
if kwargs === missing
return NonlinearLeastSquaresProblem{isinplace(prob)}(; f, u0, p, prob.kwargs...,
_kwargs...)
else
return NonlinearLeastSquaresProblem{isinplace(prob)}(; f, u0, p, kwargs...)
end
end
function varmap_has_var(varmap, var)
haskey(varmap, var) || hasname(var) && haskey(varmap, getname(var))
end
function varmap_get(varmap, var, default = nothing)
if haskey(varmap, var)
return varmap[var]
end
if hasname(var)
name = getname(var)
if haskey(varmap, name)
return varmap[name]
end
end
return default
end
anydict(d) = Dict{Any, Any}(d)
anydict() = Dict{Any, Any}()
function _updated_u0_p_internal(
prob, ::Missing, ::Missing; interpret_symbolicmap = true, use_defaults = false)
return state_values(prob), parameter_values(prob)
end
function _updated_u0_p_internal(
prob, ::Missing, p; interpret_symbolicmap = true, use_defaults = false)
u0 = state_values(prob)
if p isa AbstractArray && isempty(p)
return _updated_u0_p_internal(
prob, u0, parameter_values(prob); interpret_symbolicmap)
end
eltype(p) <: Pair && interpret_symbolicmap || return u0, p
defs = default_values(prob)
p = fill_p(prob, anydict(p); defs, use_defaults)
return _updated_u0_p_symmap(prob, u0, Val(false), p, Val(true))
end
function _updated_u0_p_internal(
prob, u0, ::Missing; interpret_symbolicmap = true, use_defaults = false)
p = parameter_values(prob)
eltype(u0) <: Pair || return u0, p
defs = default_values(prob)
u0 = fill_u0(prob, anydict(u0); defs, use_defaults)
return _updated_u0_p_symmap(prob, u0, Val(true), p, Val(false))
end
function _updated_u0_p_internal(
prob, u0, p; interpret_symbolicmap = true, use_defaults = false)
isu0symbolic = eltype(u0) <: Pair
ispsymbolic = eltype(p) <: Pair && interpret_symbolicmap
if !isu0symbolic && !ispsymbolic
return u0, p
end
defs = default_values(prob)
if isu0symbolic
u0 = fill_u0(prob, anydict(u0); defs, use_defaults)
end
if ispsymbolic
p = fill_p(prob, anydict(p); defs, use_defaults)
end
return _updated_u0_p_symmap(prob, u0, Val(isu0symbolic), p, Val(ispsymbolic))
end
function fill_u0(prob, u0; defs = nothing, use_defaults = false)
vsyms = variable_symbols(prob)
sym_to_idx = anydict()
idx_to_sym = anydict()
idx_to_val = anydict()
for (k, v) in u0
idx = variable_index(prob, k)
idx === nothing && continue
sym_to_idx[k] = idx
idx_to_sym[idx] = k
idx_to_val[idx] = v
end
for sym in vsyms
haskey(sym_to_idx, sym) && continue
idx = variable_index(prob, sym)
haskey(idx_to_val, idx) && continue
sym_to_idx[sym] = idx
idx_to_sym[idx] = sym
idx_to_val[idx] = if defs !== nothing &&
(defval = varmap_get(defs, sym)) !== nothing &&
(symbolic_type(defval) != NotSymbolic() || use_defaults)
defval
else
getu(prob, sym)(prob)
end
end
newvals = anydict()
for (idx, val) in idx_to_val
newvals[idx_to_sym[idx]] = val
end
for (k, v) in u0
haskey(sym_to_idx, k) && continue
newvals[k] = v
end
return newvals
end
function fill_p(prob, p; defs = nothing, use_defaults = false)
psyms = parameter_symbols(prob)
sym_to_idx = anydict()
idx_to_sym = anydict()
idx_to_val = anydict()
for (k, v) in p
idx = parameter_index(prob, k)
idx === nothing && continue
sym_to_idx[k] = idx
idx_to_sym[idx] = k
idx_to_val[idx] = v
end
for sym in psyms
haskey(sym_to_idx, sym) && continue
idx = parameter_index(prob, sym)
haskey(idx_to_val, idx) && continue
sym_to_idx[sym] = idx
idx_to_sym[idx] = sym
idx_to_val[idx] = if defs !== nothing &&
(defval = varmap_get(defs, sym)) !== nothing &&
(symbolic_type(defval) != NotSymbolic() || use_defaults)
defval
else
getp(prob, sym)(prob)
end
end
newvals = anydict()
for (idx, val) in idx_to_val
newvals[idx_to_sym[idx]] = val
end
for (k, v) in p
haskey(sym_to_idx, k) && continue
newvals[k] = v
end
return newvals
end
function _updated_u0_p_symmap(prob, u0, ::Val{true}, p, ::Val{false})
isdep = any(symbolic_type(v) !== NotSymbolic() for (_, v) in u0)
isdep || return remake_buffer(prob, state_values(prob), keys(u0), values(u0)), p
u0 = anydict(k => symbolic_type(v) === NotSymbolic() ? v : symbolic_evaluate(v, u0)
for (k, v) in u0)
isdep = any(symbolic_type(v) !== NotSymbolic() for (_, v) in u0)
isdep || return remake_buffer(prob, state_values(prob), keys(u0), values(u0)), p
# FIXME: need to provide `u` since the observed function expects it.
# This is sort of an implicit dependency on MTK. The values of `u` won't actually be
# used, since any state symbols in the expression were substituted out earlier.
temp_state = ProblemState(; u = state_values(prob), p = p)
u0 = anydict(k => symbolic_type(v) === NotSymbolic() ? v : getu(prob, v)(temp_state)
for (k, v) in u0)
return remake_buffer(prob, state_values(prob), keys(u0), values(u0)), p
end
function _updated_u0_p_symmap(prob, u0, ::Val{false}, p, ::Val{true})
isdep = any(symbolic_type(v) !== NotSymbolic() for (_, v) in p)
isdep || return u0, remake_buffer(prob, parameter_values(prob), keys(p), values(p))
p = anydict(k => symbolic_type(v) === NotSymbolic() ? v : symbolic_evaluate(v, p)
for (k, v) in p)
isdep = any(symbolic_type(v) !== NotSymbolic() for (_, v) in p)
isdep || return u0, remake_buffer(prob, parameter_values(prob), keys(p), values(p))
# FIXME: need to provide `p` since the observed function expects an `MTKParameters`
# this is sort of an implicit dependency on MTK. The values of `p` won't actually be
# used, since any parameter symbols in the expression were substituted out earlier.
temp_state = ProblemState(; u = u0, p = parameter_values(prob))
p = anydict(k => symbolic_type(v) === NotSymbolic() ? v : getu(prob, v)(temp_state)
for (k, v) in p)
return u0, remake_buffer(prob, parameter_values(prob), keys(p), values(p))
end
function _updated_u0_p_symmap(prob, u0, ::Val{true}, p, ::Val{true})
isu0dep = any(symbolic_type(v) !== NotSymbolic() for (_, v) in u0)
ispdep = any(symbolic_type(v) !== NotSymbolic() for (_, v) in p)
if !isu0dep && !ispdep
return remake_buffer(prob, state_values(prob), keys(u0), values(u0)),
remake_buffer(prob, parameter_values(prob), keys(p), values(p))
end
if !isu0dep
u0 = remake_buffer(prob, state_values(prob), keys(u0), values(u0))
return _updated_u0_p_symmap(prob, u0, Val(false), p, Val(true))
end
if !ispdep
p = remake_buffer(prob, parameter_values(prob), keys(p), values(p))
return _updated_u0_p_symmap(prob, u0, Val(true), p, Val(false))
end
varmap = merge(u0, p)
u0 = anydict(k => symbolic_type(v) === NotSymbolic() ? v : symbolic_evaluate(v, varmap)
for (k, v) in u0)
p = anydict(k => symbolic_type(v) === NotSymbolic() ? v : symbolic_evaluate(v, varmap)
for (k, v) in p)
return remake_buffer(prob, state_values(prob), keys(u0), values(u0)),
remake_buffer(prob, parameter_values(prob), keys(p), values(p))
end
function updated_u0_p(prob, u0, p; interpret_symbolicmap = true, use_defaults = false)
if u0 === missing && p === missing
return state_values(prob), parameter_values(prob)
end
if !has_sys(prob.f)
if interpret_symbolicmap && eltype(p) !== Union{} && eltype(p) <: Pair
throw(ArgumentError("This problem does not support symbolic maps with " *
"`remake`, i.e. it does not have a symbolic origin. Please use `remake`" *
"with the `p` keyword argument as a vector of values (paying attention to" *
"parameter order) or pass `interpret_symbolicmap = false` as a keyword argument"))
end
if eltype(u0) !== Union{} && eltype(u0) <: Pair
throw(ArgumentError("This problem does not support symbolic maps with" *
" remake, i.e. it does not have a symbolic origin. Please use `remake`" *
"with the `u0` keyword argument as a vector of values, paying attention to the order."))
end
return (u0 === missing ? state_values(prob) : u0),
(p === missing ? parameter_values(prob) : p)
end
return _updated_u0_p_internal(prob, u0, p; interpret_symbolicmap, use_defaults)
end
# overloaded in MTK to intercept symbolic remake
function process_p_u0_symbolic(prob, p, u0)
if prob isa Union{AbstractDEProblem, OptimizationProblem, NonlinearProblem}
throw(ArgumentError("Please load `ModelingToolkit.jl` in order to support symbolic remake."))
else
throw(ArgumentError("Symbolic remake for $(typeof(prob)) is currently not supported, consider opening an issue."))
end
end
function remake(thing::AbstractJumpProblem; kwargs...)
parameterless_type(thing)(remake(thing.prob; kwargs...))
end
function remake(thing::AbstractEnsembleProblem; kwargs...)
T = parameterless_type(thing)
en_kwargs = [k for k in kwargs if first(k) ∈ fieldnames(T)]
T(remake(thing.prob; setdiff(kwargs, en_kwargs)...); en_kwargs...)
end