Skip to content

Commit 46fc62e

Browse files
format
1 parent b635875 commit 46fc62e

File tree

2 files changed

+42
-41
lines changed

2 files changed

+42
-41
lines changed

lib/SCCNonlinearSolve/src/SCCNonlinearSolve.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ Algorithm for solving Strongly Connected Component (SCC) problems containing
1212
both nonlinear and linear subproblems.
1313
1414
### Keyword Arguments
15-
- `nlalg`: Algorithm to use for solving NonlinearProblem components
16-
- `linalg`: Algorithm to use for solving LinearProblem components
15+
16+
- `nlalg`: Algorithm to use for solving NonlinearProblem components
17+
- `linalg`: Algorithm to use for solving LinearProblem components
1718
"""
1819
struct SCCAlg{N, L}
1920
nlalg::N
@@ -34,7 +35,7 @@ iteratively_build_sols(alg, sols; kwargs...) = sols
3435
function iteratively_build_sols(alg, sols, (prob, explicitfun), args...; kwargs...)
3536
explicitfun(
3637
SymbolicIndexingInterface.parameter_values(prob), sols)
37-
38+
3839
_sol = if prob isa SciMLBase.LinearProblem
3940
sol = SciMLBase.solve(prob, alg.linalg; kwargs...)
4041
SciMLBase.build_linear_solution(
@@ -50,7 +51,8 @@ end
5051

5152
function CommonSolve.solve(prob::SciMLBase.SCCNonlinearProblem, alg::SCCAlg; kwargs...)
5253
numscc = length(prob.probs)
53-
sols = iteratively_build_sols(alg, (), zip(prob.probs, prob.explicitfuns!)...; kwargs...)
54+
sols = iteratively_build_sols(
55+
alg, (), zip(prob.probs, prob.explicitfuns!)...; kwargs...)
5456

5557
# TODO: fix allocations with a lazy concatenation
5658
u = reduce(vcat, sols)
@@ -66,5 +68,4 @@ function CommonSolve.solve(prob::SciMLBase.SCCNonlinearProblem, alg::SCCAlg; kwa
6668
SciMLBase.build_solution(prob, alg, u, resid; retcode, original = sols)
6769
end
6870

69-
7071
end

lib/SCCNonlinearSolve/test/core_tests.jl

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,67 +7,67 @@ end
77
@testitem "Manual SCC" setup=[CoreRootfindTesting] tags=[:core] begin
88
using NonlinearSolveFirstOrder
99
function f(du, u, p)
10-
du[1] = cos(u[2]) - u[1]
11-
du[2] = sin(u[1] + u[2]) + u[2]
12-
du[3] = 2u[4] + u[3] + 1.0
13-
du[4] = u[5]^2 + u[4]
14-
du[5] = u[3]^2 + u[5]
15-
du[6] = u[1] + u[2] + u[3] + u[4] + u[5] + 2.0u[6] + 2.5u[7] + 1.5u[8]
16-
du[7] = u[1] + u[2] + u[3] + 2.0u[4] + u[5] + 4.0u[6] - 1.5u[7] + 1.5u[8]
17-
du[8] = u[1] + 2.0u[2] + 3.0u[3] + 5.0u[4] + 6.0u[5] + u[6] - u[7] - u[8]
10+
du[1]=cos(u[2])-u[1]
11+
du[2]=sin(u[1]+u[2])+u[2]
12+
du[3]=2u[4]+u[3]+1.0
13+
du[4]=u[5]^2+u[4]
14+
du[5]=u[3]^2+u[5]
15+
du[6]=u[1]+u[2]+u[3]+u[4]+u[5]+2.0u[6]+2.5u[7]+1.5u[8]
16+
du[7]=u[1]+u[2]+u[3]+2.0u[4]+u[5]+4.0u[6]-1.5u[7]+1.5u[8]
17+
du[8]=u[1]+2.0u[2]+3.0u[3]+5.0u[4]+6.0u[5]+u[6]-u[7]-u[8]
1818
end
19-
prob = NonlinearProblem(f, zeros(8))
20-
sol = solve(prob, NewtonRaphson())
19+
prob=NonlinearProblem(f, zeros(8))
20+
sol=solve(prob, NewtonRaphson())
2121

22-
u0 = zeros(2)
23-
p = zeros(3)
22+
u0=zeros(2)
23+
p=zeros(3)
2424

2525
function f1(du, u, p)
26-
du[1] = cos(u[2]) - u[1]
27-
du[2] = sin(u[1] + u[2]) + u[2]
26+
du[1]=cos(u[2])-u[1]
27+
du[2]=sin(u[1]+u[2])+u[2]
2828
end
29-
explicitfun1(p, sols) = nothing
30-
prob1 = NonlinearProblem(
29+
explicitfun1(p, sols)=nothing
30+
prob1=NonlinearProblem(
3131
NonlinearFunction{true, SciMLBase.NoSpecialize}(f1), zeros(2), p)
32-
sol1 = solve(prob1, NewtonRaphson())
32+
sol1=solve(prob1, NewtonRaphson())
3333

3434
function f2(du, u, p)
35-
du[1] = 2u[2] + u[1] + 1.0
36-
du[2] = u[3]^2 + u[2]
37-
du[3] = u[1]^2 + u[3]
35+
du[1]=2u[2]+u[1]+1.0
36+
du[2]=u[3]^2+u[2]
37+
du[3]=u[1]^2+u[3]
3838
end
39-
explicitfun2(p, sols) = nothing
40-
prob2 = NonlinearProblem(
39+
explicitfun2(p, sols)=nothing
40+
prob2=NonlinearProblem(
4141
NonlinearFunction{true, SciMLBase.NoSpecialize}(f2), zeros(3), p)
42-
sol2 = solve(prob2, NewtonRaphson())
42+
sol2=solve(prob2, NewtonRaphson())
4343

4444
# Convert f3 to a LinearProblem since it's linear in u
4545
# du = Au + b where A is the coefficient matrix and b is from parameters
46-
A3 = [2.0 2.5 1.5; 4.0 -1.5 1.5; 1.0 -1.0 -1.0]
47-
b3 = p # b will be updated by explicitfun3
48-
prob3 = LinearProblem(A3, b3, zeros(3))
46+
A3=[2.0 2.5 1.5; 4.0 -1.5 1.5; 1.0 -1.0 -1.0]
47+
b3=p # b will be updated by explicitfun3
48+
prob3=LinearProblem(A3, b3, zeros(3))
4949
function explicitfun3(p, sols)
50-
p[1] = -(sols[1][1] + sols[1][2] + sols[2][1] + sols[2][2] + sols[2][3])
51-
p[2] = -(sols[1][1] + sols[1][2] + sols[2][1] + 2.0sols[2][2] + sols[2][3])
52-
p[3] = -(sols[1][1] + 2.0sols[1][2] + 3.0sols[2][1] + 5.0sols[2][2] +
50+
p[1]=-(sols[1][1]+sols[1][2]+sols[2][1]+sols[2][2]+sols[2][3])
51+
p[2]=-(sols[1][1]+sols[1][2]+sols[2][1]+2.0sols[2][2]+sols[2][3])
52+
p[3]=-(sols[1][1]+2.0sols[1][2]+3.0sols[2][1]+5.0sols[2][2]+
5353
6.0sols[2][3])
5454
end
5555
explicitfun3(p, [sol1, sol2])
56-
sol3 = solve(prob3) # LinearProblem uses default linear solver
57-
manualscc = reduce(vcat,(sol1, sol2, sol3))
56+
sol3=solve(prob3) # LinearProblem uses default linear solver
57+
manualscc=reduce(vcat, (sol1, sol2, sol3))
5858

59-
sccprob = SciMLBase.SCCNonlinearProblem((prob1, prob2, prob3),
59+
sccprob=SciMLBase.SCCNonlinearProblem((prob1, prob2, prob3),
6060
SciMLBase.Void{Any}.([explicitfun1, explicitfun2, explicitfun3]))
61-
61+
6262
# Test with SCCAlg that handles both nonlinear and linear problems
6363
using SCCNonlinearSolve
64-
scc_alg = SCCNonlinearSolve.SCCAlg(nlalg = NewtonRaphson(), linalg = nothing)
65-
scc_sol = solve(sccprob, scc_alg)
64+
scc_alg=SCCNonlinearSolve.SCCAlg(nlalg = NewtonRaphson(), linalg = nothing)
65+
scc_sol=solve(sccprob, scc_alg)
6666
@test sol manualscc scc_sol
6767

6868
import NonlinearSolve # Required for Default
6969

7070
# Test default interface
71-
scc_sol_default = solve(sccprob)
71+
scc_sol_default=solve(sccprob)
7272
@test sol manualscc scc_sol_default
7373
end

0 commit comments

Comments
 (0)