Skip to content

Commit cc9550e

Browse files
fixup! test: test initialization on NonlinearProblem and NonlinearLeastSquaresProblem
1 parent d0b6460 commit cc9550e

File tree

1 file changed

+130
-37
lines changed

1 file changed

+130
-37
lines changed

test/initializationsystem.jl

Lines changed: 130 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -829,10 +829,33 @@ end
829829
@brownian a
830830
x = _x(t)
831831

832-
@testset "$Problem" for (Problem, alg, rhss) in [
833-
(ODEProblem, Tsit5(), 0), (SDEProblem, ImplicitEM(), a),
834-
(DDEProblem, MethodOfSteps(Tsit5()), _x(t - 0.1)),
835-
(SDDEProblem, ImplicitEM(), _x(t - 0.1) + a)]
832+
@testset "$Problem with $(SciMLBase.parameterless_type(typeof(alg)))" for (System, Problem, alg, rhss) in [
833+
(System, ODEProblem, Tsit5(), 0),
834+
(System, SDEProblem, ImplicitEM(), a),
835+
(System, DDEProblem, MethodOfSteps(Tsit5()), _x(t - 0.1)),
836+
(System, SDDEProblem, ImplicitEM(), _x(t - 0.1) + a),
837+
# polyalg cache
838+
(NonlinearSystemWrapper, NonlinearProblemWrapper,
839+
FastShortcutNonlinearPolyalg(), 0),
840+
# generalized first order cache
841+
(NonlinearSystemWrapper, NonlinearProblemWrapper, NewtonRaphson(), 0),
842+
# quasi newton cache
843+
(NonlinearSystemWrapper, NonlinearProblemWrapper, Klement(), 0),
844+
# noinit cache
845+
(NonlinearSystemWrapper, NonlinearProblemWrapper, SimpleNewtonRaphson(), 0),
846+
# DFSane cache
847+
(NonlinearSystemWrapper, NonlinearProblemWrapper, DFSane(), 0),
848+
# Least squares
849+
# polyalg cache
850+
(NonlinearSystemWrapper, NLLSProblemWrapper, FastShortcutNLLSPolyalg(), 0),
851+
# generalized first order cache
852+
(NonlinearSystemWrapper, NLLSProblemWrapper, LevenbergMarquardt(), 0),
853+
# noinit cache
854+
(NonlinearSystemWrapper, NLLSProblemWrapper, SimpleGaussNewton(), 0)
855+
]
856+
is_nlsolve = alg isa SciMLBase.AbstractNonlinearAlgorithm
857+
D = is_nlsolve ? v -> v^3 : Differential(t)
858+
836859
@mtkbuild sys = System(
837860
[D(x) ~ 2x + r + rhss], t; parameter_dependencies = [r ~ p + 2q, q ~ p + 3],
838861
guesses = [p => 1.0])
@@ -850,21 +873,44 @@ end
850873
@brownian a b
851874
x = _x(t)
852875

853-
@testset "$Problem" for (Problem, alg, rhss) in [
854-
(ODEProblem, Tsit5(), zeros(2)), (SDEProblem, ImplicitEM(), [a, b]),
855-
(DDEProblem, MethodOfSteps(Tsit5()), [_x(t - 0.1), 0.0]),
856-
(SDDEProblem, ImplicitEM(), [_x(t - 0.1) + a, b])]
876+
@testset "$Problem with $(SciMLBase.parameterless_type(typeof(alg)))" for (System, Problem, alg, rhss) in [
877+
(System, ODEProblem, Tsit5(), zeros(2)),
878+
(System, SDEProblem, ImplicitEM(), [a, b]),
879+
(System, DDEProblem, MethodOfSteps(Tsit5()), [_x(t - 0.1), 0.0]),
880+
(System, SDDEProblem, ImplicitEM(), [_x(t - 0.1) + a, b]),
881+
# polyalg cache
882+
(NonlinearSystemWrapper, NonlinearProblemWrapper,
883+
FastShortcutNonlinearPolyalg(), zeros(2)),
884+
# generalized first order cache
885+
(NonlinearSystemWrapper, NonlinearProblemWrapper, NewtonRaphson(), zeros(2)),
886+
# quasi newton cache
887+
(NonlinearSystemWrapper, NonlinearProblemWrapper, Klement(), zeros(2)),
888+
# noinit cache
889+
(NonlinearSystemWrapper, NonlinearProblemWrapper, SimpleNewtonRaphson(), zeros(2)),
890+
# DFSane cache
891+
(NonlinearSystemWrapper, NonlinearProblemWrapper, DFSane(), zeros(2)),
892+
# Least squares
893+
# polyalg cache
894+
(NonlinearSystemWrapper, NLLSProblemWrapper, FastShortcutNLLSPolyalg(), zeros(2)),
895+
# generalized first order cache
896+
(NonlinearSystemWrapper, NLLSProblemWrapper, LevenbergMarquardt(), zeros(2)),
897+
# noinit cache
898+
(NonlinearSystemWrapper, NLLSProblemWrapper, SimpleGaussNewton(), zeros(2))
899+
]
900+
is_nlsolve = alg isa SciMLBase.AbstractNonlinearAlgorithm
901+
D = is_nlsolve ? v -> v^3 : Differential(t)
902+
857903
@mtkbuild sys = System(
858904
[D(x) ~ x + rhss[1], p ~ x + y + rhss[2]], t; defaults = [p => missing], guesses = [
859905
x => 0.0, p => 0.0])
860906
prob = Problem(sys, [x => 1.0, y => 1.0], (0.0, 1.0))
861907
@test init(prob, alg).ps[p] 2.0
862908
# nonsensical value for y just to test that equations work
863-
prob2 = remake(prob; u0 = [x => 1.0, y => 2x + exp(t)])
864-
@test init(prob2, alg).ps[p] 4.0
909+
prob2 = remake(prob; u0 = [x => 1.0, y => 2x + exp(x)])
910+
@test init(prob2, alg).ps[p] 3 + exp(1)
865911
# solve for `x` given `p` and `y`
866-
prob3 = remake(prob; u0 = [x => nothing, y => 1.0], p = [p => 2x + exp(t)])
867-
@test init(prob3, alg)[x] 0.0
912+
prob3 = remake(prob; u0 = [x => nothing, y => 1.0], p = [p => 2x + exp(y)])
913+
@test init(prob3, alg)[x] 1 - exp(1)
868914
@test_logs (:warn, r"overdetermined") remake(
869915
prob; u0 = [x => 1.0, y => 2.0], p = [p => 4.0])
870916
prob4 = remake(prob; u0 = [x => 1.0, y => 2.0], p = [p => 4.0])
@@ -880,44 +926,68 @@ end
880926
@brownian a
881927
x = _x(t)
882928

883-
@testset "$Problem" for (Problem, alg, rhss) in [
884-
(ODEProblem, Tsit5(), 0), (SDEProblem, ImplicitEM(), a),
885-
(DDEProblem, MethodOfSteps(Tsit5()), _x(t - 0.1)),
886-
(SDDEProblem, ImplicitEM(), _x(t - 0.1) + a)]
929+
@testset "$Problem with $(SciMLBase.parameterless_type(typeof(alg)))" for (System, Problem, alg, rhss) in [
930+
(System, ODEProblem, Tsit5(), 0),
931+
(System, SDEProblem, ImplicitEM(), a),
932+
(System, DDEProblem, MethodOfSteps(Tsit5()), _x(t - 0.1)),
933+
(System, SDDEProblem, ImplicitEM(), _x(t - 0.1) + a),
934+
# polyalg cache
935+
(NonlinearSystemWrapper, NonlinearProblemWrapper,
936+
FastShortcutNonlinearPolyalg(), 0),
937+
# generalized first order cache
938+
(NonlinearSystemWrapper, NonlinearProblemWrapper, NewtonRaphson(), 0),
939+
# quasi newton cache
940+
(NonlinearSystemWrapper, NonlinearProblemWrapper, Klement(), 0),
941+
# noinit cache
942+
(NonlinearSystemWrapper, NonlinearProblemWrapper, SimpleNewtonRaphson(), 0),
943+
# DFSane cache
944+
(NonlinearSystemWrapper, NonlinearProblemWrapper, DFSane(), 0),
945+
# Least squares
946+
# polyalg cache
947+
(NonlinearSystemWrapper, NLLSProblemWrapper, FastShortcutNLLSPolyalg(), 0),
948+
# generalized first order cache
949+
(NonlinearSystemWrapper, NLLSProblemWrapper, LevenbergMarquardt(), 0),
950+
# noinit cache
951+
(NonlinearSystemWrapper, NLLSProblemWrapper, SimpleGaussNewton(), 0)
952+
]
953+
is_nlsolve = alg isa SciMLBase.AbstractNonlinearAlgorithm
954+
D = is_nlsolve ? v -> v^3 : Differential(t)
955+
alge_eqs = [y^2 * q + q^2 * x ~ 0, z * p - p^2 * x * z ~ 0]
956+
887957
@mtkbuild sys = System(
888-
[D(x) ~ x * p + y * q + rhss, y^2 * q + q^2 * x ~ 0, z * p - p^2 * x * z ~ 0],
958+
[D(x) ~ x * p + y^2 * q + rhss; alge_eqs],
889959
t; guesses = [x => 0.0, y => 0.0, z => 0.0, p => 0.0, q => 0.0])
890-
prob = Problem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0, q => missing])
960+
prob = Problem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0, q => missing]; initialization_eqs = is_nlsolve ? alge_eqs : [])
891961
@test is_variable(prob.f.initialization_data.initializeprob, q)
892962
ps = prob.p
893963
newps = SciMLStructures.replace(Tunable(), ps, ForwardDiff.Dual.(ps.tunable))
894964
prob2 = remake(prob; p = newps)
895-
@test eltype(prob2.f.initialization_data.initializeprob.u0) <: ForwardDiff.Dual
965+
@test eltype(state_values(prob2.f.initialization_data.initializeprob)) <: ForwardDiff.Dual
896966
@test eltype(prob2.f.initialization_data.initializeprob.p.tunable) <:
897967
ForwardDiff.Dual
898-
@test prob2.f.initialization_data.initializeprob.u0
899-
prob.f.initialization_data.initializeprob.u0
968+
@test state_values(prob2.f.initialization_data.initializeprob)
969+
state_values(prob.f.initialization_data.initializeprob)
900970

901971
prob2 = remake(prob; u0 = ForwardDiff.Dual.(prob.u0))
902-
@test eltype(prob2.f.initialization_data.initializeprob.u0) <: ForwardDiff.Dual
972+
@test eltype(state_values(prob2.f.initialization_data.initializeprob)) <: ForwardDiff.Dual
903973
@test eltype(prob2.f.initialization_data.initializeprob.p.tunable) <: Float64
904-
@test prob2.f.initialization_data.initializeprob.u0
905-
prob.f.initialization_data.initializeprob.u0
974+
@test state_values(prob2.f.initialization_data.initializeprob)
975+
state_values(prob.f.initialization_data.initializeprob)
906976

907977
prob2 = remake(prob; u0 = ForwardDiff.Dual.(prob.u0), p = newps)
908-
@test eltype(prob2.f.initialization_data.initializeprob.u0) <: ForwardDiff.Dual
978+
@test eltype(state_values(prob2.f.initialization_data.initializeprob)) <: ForwardDiff.Dual
909979
@test eltype(prob2.f.initialization_data.initializeprob.p.tunable) <:
910980
ForwardDiff.Dual
911-
@test prob2.f.initialization_data.initializeprob.u0
912-
prob.f.initialization_data.initializeprob.u0
981+
@test state_values(prob2.f.initialization_data.initializeprob)
982+
state_values(prob.f.initialization_data.initializeprob)
913983

914984
prob2 = remake(prob; u0 = [x => ForwardDiff.Dual(1.0)],
915985
p = [p => ForwardDiff.Dual(1.0), q => missing])
916-
@test eltype(prob2.f.initialization_data.initializeprob.u0) <: ForwardDiff.Dual
986+
@test eltype(state_values(prob2.f.initialization_data.initializeprob)) <: ForwardDiff.Dual
917987
@test eltype(prob2.f.initialization_data.initializeprob.p.tunable) <:
918988
ForwardDiff.Dual
919-
@test prob2.f.initialization_data.initializeprob.u0
920-
prob.f.initialization_data.initializeprob.u0
989+
@test state_values(prob2.f.initialization_data.initializeprob)
990+
state_values(prob.f.initialization_data.initializeprob)
921991
end
922992
end
923993

@@ -927,19 +997,42 @@ end
927997
@brownian a
928998
x = _x(t)
929999

930-
@testset "$Problem" for (Problem, alg, rhss) in [
931-
(ODEProblem, Tsit5(), 0), (SDEProblem, ImplicitEM(), a),
932-
(DDEProblem, MethodOfSteps(Tsit5()), _x(t - 0.1)),
933-
(SDDEProblem, ImplicitEM(), _x(t - 0.1) + a)]
1000+
@testset "$Problem with $(SciMLBase.parameterless_type(typeof(alg)))" for (System, Problem, alg, rhss) in [
1001+
(System, ODEProblem, Tsit5(), 0),
1002+
(System, SDEProblem, ImplicitEM(), a),
1003+
(System, DDEProblem, MethodOfSteps(Tsit5()), _x(t - 0.1)),
1004+
(System, SDDEProblem, ImplicitEM(), _x(t - 0.1) + a),
1005+
# polyalg cache
1006+
(NonlinearSystemWrapper, NonlinearProblemWrapper,
1007+
FastShortcutNonlinearPolyalg(), 0),
1008+
# generalized first order cache
1009+
(NonlinearSystemWrapper, NonlinearProblemWrapper, NewtonRaphson(), 0),
1010+
# quasi newton cache
1011+
(NonlinearSystemWrapper, NonlinearProblemWrapper, Klement(), 0),
1012+
# noinit cache
1013+
(NonlinearSystemWrapper, NonlinearProblemWrapper, SimpleNewtonRaphson(), 0),
1014+
# DFSane cache
1015+
(NonlinearSystemWrapper, NonlinearProblemWrapper, DFSane(), 0),
1016+
# Least squares
1017+
# polyalg cache
1018+
(NonlinearSystemWrapper, NLLSProblemWrapper, FastShortcutNLLSPolyalg(), 0),
1019+
# generalized first order cache
1020+
(NonlinearSystemWrapper, NLLSProblemWrapper, LevenbergMarquardt(), 0),
1021+
# noinit cache
1022+
(NonlinearSystemWrapper, NLLSProblemWrapper, SimpleGaussNewton(), 0)
1023+
]
1024+
is_nlsolve = alg isa SciMLBase.AbstractNonlinearAlgorithm
1025+
D = is_nlsolve ? v -> v^3 : Differential(t)
1026+
alge_eqs = [y^2 + 4y * p^2 ~ x^3]
9341027
@mtkbuild sys = System(
935-
[D(x) ~ x + p * y + rhss, y^2 + 4y * p^2 ~ x], t; guesses = [
1028+
[D(x) ~ x + p * y^2 + rhss; alge_eqs], t; guesses = [
9361029
y => 1.0, p => 1.0])
937-
prob = Problem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0])
1030+
prob = Problem(sys, [x => 1.0], (0.0, 1.0), [p => 1.0]; initialization_eqs = is_nlsolve ? alge_eqs : [])
9381031
@test is_variable(prob.f.initialization_data.initializeprob, y)
9391032
prob2 = @test_nowarn remake(prob; p = [p => 3.0]) # ensure no over/under-determined warning
9401033
@test is_variable(prob.f.initialization_data.initializeprob, y)
9411034

942-
prob = Problem(sys, [y => 1.0, x => 2.0], (0.0, 1.0), [p => missing])
1035+
prob = Problem(sys, [y => 1.0, x => 2.0], (0.0, 1.0), [p => missing]; initialization_eqs = is_nlsolve ? alge_eqs : [])
9431036
@test is_variable(prob.f.initialization_data.initializeprob, p)
9441037
prob2 = @test_nowarn remake(prob; u0 = [y => 0.5])
9451038
@test is_variable(prob.f.initialization_data.initializeprob, p)

0 commit comments

Comments
 (0)