Skip to content

Commit fbcc39e

Browse files
test: test lazy initialization in remake for supported problem types
1 parent 58f6852 commit fbcc39e

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

test/downstream/modelingtoolkit_remake.jl

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,45 @@ end
337337
@test sccprob4.p !== sccprob4.probs[2].p
338338
end
339339

340+
# TODO: Rewrite this test when MTK build initialization for everything
340341
@testset "Lazy initialization" begin
341-
@variables x(t) [guess = 1.0] y(t) [guess = 1.0]
342-
@parameters p=missing [guess = 1.0]
343-
@mtkbuild sys = ODESystem([D(x) ~ x, x + y ~ p], t)
344-
prob = ODEProblem(sys, [x => 1.0, y => 1.0], (0.0, 1.0))
345-
prob2 = remake(prob; u0 = [x => 2.0])
346-
@test prob2.ps[p] 3.0
342+
@variables _x(..) [guess = 1.0] y(t) [guess = 1.0]
343+
@parameters p=1.0 [guess = 1.0]
344+
@brownian a
345+
x = _x(t)
346+
347+
initprob = NonlinearProblem(nothing) do args...
348+
return 0.0
349+
end
350+
iprobmap = (_...) -> [1.0, 1.0]
351+
iprobpmap = function (orig, sol)
352+
ps = parameter_values(orig)
353+
setp(orig, p)(ps, 3.0)
354+
return ps
355+
end
356+
initdata = SciMLBase.OverrideInitData(initprob, nothing, iprobmap, iprobpmap)
357+
@test SciMLBase.is_trivial_initialization(initdata)
358+
359+
@testset "$Problem" for (SystemT, rhss, Problem, Func) in [
360+
(ODESystem, 0.0, ODEProblem, ODEFunction),
361+
(System, a, SDEProblem, SDEFunction),
362+
(ODESystem, _x(t - 0.1), DDEProblem, DDEFunction),
363+
(System, _x(t - 0.1) + a, SDDEProblem, SDDEFunction),
364+
(NonlinearSystem, y + 2, NonlinearProblem, NonlinearFunction),
365+
(NonlinearSystem, y + 2, NonlinearLeastSquaresProblem, NonlinearFunction)
366+
]
367+
is_nlsolve = SystemT == NonlinearSystem
368+
D = is_nlsolve ? (v) -> v^3 : Differential(t)
369+
sys_args = is_nlsolve ? () : (t,)
370+
prob_args = is_nlsolve ? () : ((0.0, 1.0),)
371+
372+
@mtkbuild sys = SystemT([D(x) ~ x + rhss, x + y ~ p], sys_args...)
373+
prob = Problem(sys, [x => 1.0, y => 1.0], prob_args...)
374+
func_args = isdefined(prob.f, :g) ? (prob.f.g,) : ()
375+
func = Func{true, SciMLBase.FullSpecialize}(
376+
prob.f.f, func_args...; initialization_data = initdata, sys = prob.f.sys)
377+
prob2 = remake(prob; f = func)
378+
@test SciMLBase.is_trivial_initialization(prob2)
379+
@test prob2.ps[p] 3.0
380+
end
347381
end

0 commit comments

Comments
 (0)