Skip to content

Commit c5c8120

Browse files
AayushSabharwalChrisRackauckas
authored andcommitted
fix: fix vector-form SCCNonlinearProblem with linear SCCs
1 parent d8e65c8 commit c5c8120

2 files changed

Lines changed: 30 additions & 23 deletions

File tree

lib/SCCNonlinearSolve/src/SCCNonlinearSolve.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ function solve_single_scc(alg, prob, explicitfun, sols; kwargs...)
8787
sol = SciMLBase.solve(SciMLBase.remake(prob; A, b), alg.linalg; kwargs...)
8888
# LinearSolution may have resid=nothing, so compute it: resid = A*u - b
8989
resid = isnothing(sol.resid) ? A * sol.u - b : sol.resid
90-
SciMLBase.build_linear_solution(
91-
alg.linalg, sol.u, resid, nothing, retcode = sol.retcode
90+
nlprob = NonlinearProblem{true}(Returns(nothing), sol.u, prob.p)
91+
SciMLBase.strip_solution(
92+
SciMLBase.build_solution(nlprob, nothing, sol.u, resid, retcode = sol.retcode)
9293
)
9394
else
9495
sol = SciMLBase.solve(prob, alg.nlalg; kwargs...)
@@ -116,7 +117,7 @@ function iteratively_build_sols(alg, probs::AbstractVector, explicitfuns::Abstra
116117
}
117118
sols = Vector{ST}(undef, length(probs))
118119
for i in eachindex(probs)
119-
sols[i] = solve_single_scc(alg, probs[i], explicitfuns[i], view(sols, 1:(i - 1)); kwargs...)
120+
sols[i] = solve_single_scc(alg, probs[i], explicitfuns[i], view(sols, 1:(i - 1)); kwargs...)::ST
120121
end
121122
return sols
122123
end

lib/SCCNonlinearSolve/test/core_tests.jl

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,36 +60,42 @@ end
6060
sol3 = solve(prob3) # LinearProblem uses default linear solver
6161
manualscc = reduce(vcat, (sol1, sol2, sol3))
6262

63-
sccprob = SciMLBase.SCCNonlinearProblem(
63+
sccprob1 = SciMLBase.SCCNonlinearProblem(
6464
(prob1, prob2, prob3),
6565
SciMLBase.Void{Any}.([explicitfun1, explicitfun2, explicitfun3])
6666
)
67+
sccprob2 = SciMLBase.SCCNonlinearProblem(
68+
[prob1, prob2, prob3],
69+
SciMLBase.Void{Any}.([explicitfun1, explicitfun2, explicitfun3])
70+
)
6771

6872
# Test with SCCAlg that handles both nonlinear and linear problems
6973
using SCCNonlinearSolve
7074
scc_alg = SCCNonlinearSolve.SCCAlg(nlalg = NewtonRaphson(), linalg = nothing)
71-
scc_sol = solve(sccprob, scc_alg)
72-
@test sol manualscc scc_sol
73-
@test scc_sol.original === nothing # default: store_original = Val(false)
74-
75-
# Test store_original = Val(true) for debugging
76-
scc_alg_debug = SCCNonlinearSolve.SCCAlg(
77-
nlalg = NewtonRaphson(), linalg = nothing, store_original = Val(true),
78-
)
79-
scc_sol_debug = solve(sccprob, scc_alg_debug)
80-
@test sol manualscc scc_sol_debug
81-
@test scc_sol_debug.original !== nothing
82-
@test length(scc_sol_debug.original) == 3 # 3 sub-problems
75+
@testset "$type" for (type, sccprob) in [("tuple-form", sccprob1), ("array-form", sccprob2)]
76+
scc_sol = solve(sccprob, scc_alg)
77+
@test sol manualscc scc_sol
78+
@test scc_sol.original === nothing # default: store_original = Val(false)
79+
80+
# Test store_original = Val(true) for debugging
81+
scc_alg_debug = SCCNonlinearSolve.SCCAlg(
82+
nlalg = NewtonRaphson(), linalg = nothing, store_original = Val(true),
83+
)
84+
scc_sol_debug = solve(sccprob, scc_alg_debug)
85+
@test sol manualscc scc_sol_debug
86+
@test scc_sol_debug.original !== nothing
87+
@test length(scc_sol_debug.original) == 3 # 3 sub-problems
8388

84-
# Backwards compat of alg choice
85-
scc_sol = solve(sccprob, NewtonRaphson())
86-
@test sol manualscc scc_sol
89+
# Backwards compat of alg choice
90+
scc_sol = solve(sccprob, NewtonRaphson())
91+
@test sol manualscc scc_sol
8792

88-
import NonlinearSolve # Required for Default
93+
import NonlinearSolve # Required for Default
8994

90-
# Test default interface
91-
scc_sol_default = solve(sccprob)
92-
@test sol manualscc scc_sol_default
95+
# Test default interface
96+
scc_sol_default = solve(sccprob)
97+
@test sol manualscc scc_sol_default
98+
end
9399
end
94100

95101
@testitem "SCCNonlinearProblem solve without explicit u0 (issue #758)" setup = [CoreRootfindTesting] tags = [:core] begin

0 commit comments

Comments
 (0)