-
-
Notifications
You must be signed in to change notification settings - Fork 51
Support LinearProblems in SCCs #633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -7,64 +7,71 @@ end | |||||||||||||||||
@testitem "Manual SCC" setup=[CoreRootfindTesting] tags=[:core] begin | ||||||||||||||||||
using NonlinearSolveFirstOrder | ||||||||||||||||||
function f(du, u, p) | ||||||||||||||||||
du[1] = cos(u[2]) - u[1] | ||||||||||||||||||
du[2] = sin(u[1] + u[2]) + u[2] | ||||||||||||||||||
du[3] = 2u[4] + u[3] + 1.0 | ||||||||||||||||||
du[4] = u[5]^2 + u[4] | ||||||||||||||||||
du[5] = u[3]^2 + u[5] | ||||||||||||||||||
du[6] = u[1] + u[2] + u[3] + u[4] + u[5] + 2.0u[6] + 2.5u[7] + 1.5u[8] | ||||||||||||||||||
du[7] = u[1] + u[2] + u[3] + 2.0u[4] + u[5] + 4.0u[6] - 1.5u[7] + 1.5u[8] | ||||||||||||||||||
du[8] = u[1] + 2.0u[2] + 3.0u[3] + 5.0u[4] + 6.0u[5] + u[6] - u[7] - u[8] | ||||||||||||||||||
du[1]=cos(u[2])-u[1] | ||||||||||||||||||
du[2]=sin(u[1]+u[2])+u[2] | ||||||||||||||||||
du[3]=2u[4]+u[3]+1.0 | ||||||||||||||||||
du[4]=u[5]^2+u[4] | ||||||||||||||||||
du[5]=u[3]^2+u[5] | ||||||||||||||||||
du[6]=u[1]+u[2]+u[3]+u[4]+u[5]+2.0u[6]+2.5u[7]+1.5u[8] | ||||||||||||||||||
du[7]=u[1]+u[2]+u[3]+2.0u[4]+u[5]+4.0u[6]-1.5u[7]+1.5u[8] | ||||||||||||||||||
du[8]=u[1]+2.0u[2]+3.0u[3]+5.0u[4]+6.0u[5]+u[6]-u[7]-u[8] | ||||||||||||||||||
end | ||||||||||||||||||
prob = NonlinearProblem(f, zeros(8)) | ||||||||||||||||||
sol = solve(prob, NewtonRaphson()) | ||||||||||||||||||
prob=NonlinearProblem(f, zeros(8)) | ||||||||||||||||||
sol=solve(prob, NewtonRaphson()) | ||||||||||||||||||
Comment on lines
+19
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
u0 = zeros(2) | ||||||||||||||||||
p = zeros(3) | ||||||||||||||||||
u0=zeros(2) | ||||||||||||||||||
p=zeros(3) | ||||||||||||||||||
Comment on lines
+22
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
function f1(du, u, p) | ||||||||||||||||||
du[1] = cos(u[2]) - u[1] | ||||||||||||||||||
du[2] = sin(u[1] + u[2]) + u[2] | ||||||||||||||||||
du[1]=cos(u[2])-u[1] | ||||||||||||||||||
du[2]=sin(u[1]+u[2])+u[2] | ||||||||||||||||||
Comment on lines
+26
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||
end | ||||||||||||||||||
explicitfun1(p, sols) = nothing | ||||||||||||||||||
prob1 = NonlinearProblem( | ||||||||||||||||||
explicitfun1(p, sols)=nothing | ||||||||||||||||||
prob1=NonlinearProblem( | ||||||||||||||||||
Comment on lines
+29
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||
NonlinearFunction{true, SciMLBase.NoSpecialize}(f1), zeros(2), p) | ||||||||||||||||||
sol1 = solve(prob1, NewtonRaphson()) | ||||||||||||||||||
sol1=solve(prob1, NewtonRaphson()) | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
function f2(du, u, p) | ||||||||||||||||||
du[1] = 2u[2] + u[1] + 1.0 | ||||||||||||||||||
du[2] = u[3]^2 + u[2] | ||||||||||||||||||
du[3] = u[1]^2 + u[3] | ||||||||||||||||||
du[1]=2u[2]+u[1]+1.0 | ||||||||||||||||||
du[2]=u[3]^2+u[2] | ||||||||||||||||||
du[3]=u[1]^2+u[3] | ||||||||||||||||||
Comment on lines
+35
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||
end | ||||||||||||||||||
explicitfun2(p, sols) = nothing | ||||||||||||||||||
prob2 = NonlinearProblem( | ||||||||||||||||||
explicitfun2(p, sols)=nothing | ||||||||||||||||||
prob2=NonlinearProblem( | ||||||||||||||||||
Comment on lines
+39
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||
NonlinearFunction{true, SciMLBase.NoSpecialize}(f2), zeros(3), p) | ||||||||||||||||||
sol2 = solve(prob2, NewtonRaphson()) | ||||||||||||||||||
sol2=solve(prob2, NewtonRaphson()) | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
function f3(du, u, p) | ||||||||||||||||||
du[1] = p[1] + 2.0u[1] + 2.5u[2] + 1.5u[3] | ||||||||||||||||||
du[2] = p[2] + 4.0u[1] - 1.5u[2] + 1.5u[3] | ||||||||||||||||||
du[3] = p[3] + +u[1] - u[2] - u[3] | ||||||||||||||||||
end | ||||||||||||||||||
prob3 = NonlinearProblem( | ||||||||||||||||||
NonlinearFunction{true, SciMLBase.NoSpecialize}(f3), zeros(3), p) | ||||||||||||||||||
# Convert f3 to a LinearProblem since it's linear in u | ||||||||||||||||||
# du = Au + b where A is the coefficient matrix and b is from parameters | ||||||||||||||||||
A3=[2.0 2.5 1.5; 4.0 -1.5 1.5; 1.0 -1.0 -1.0] | ||||||||||||||||||
b3=p # b will be updated by explicitfun3 | ||||||||||||||||||
prob3=LinearProblem(A3, b3, zeros(3)) | ||||||||||||||||||
Comment on lines
+46
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||
function explicitfun3(p, sols) | ||||||||||||||||||
p[1] = sols[1][1] + sols[1][2] + sols[2][1] + sols[2][2] + sols[2][3] | ||||||||||||||||||
p[2] = sols[1][1] + sols[1][2] + sols[2][1] + 2.0sols[2][2] + sols[2][3] | ||||||||||||||||||
p[3] = sols[1][1] + 2.0sols[1][2] + 3.0sols[2][1] + 5.0sols[2][2] + | ||||||||||||||||||
6.0sols[2][3] | ||||||||||||||||||
p[1]=-(sols[1][1]+sols[1][2]+sols[2][1]+sols[2][2]+sols[2][3]) | ||||||||||||||||||
p[2]=-(sols[1][1]+sols[1][2]+sols[2][1]+2.0sols[2][2]+sols[2][3]) | ||||||||||||||||||
p[3]=-(sols[1][1]+2.0sols[1][2]+3.0sols[2][1]+5.0sols[2][2]+ | ||||||||||||||||||
6.0sols[2][3]) | ||||||||||||||||||
Comment on lines
+50
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||
end | ||||||||||||||||||
explicitfun3(p, [sol1, sol2]) | ||||||||||||||||||
sol3 = solve(prob3, NewtonRaphson()) | ||||||||||||||||||
manualscc = [sol1; sol2; sol3] | ||||||||||||||||||
sol3=solve(prob3) # LinearProblem uses default linear solver | ||||||||||||||||||
manualscc=reduce(vcat, (sol1, sol2, sol3)) | ||||||||||||||||||
Comment on lines
+56
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
sccprob = SciMLBase.SCCNonlinearProblem([prob1, prob2, prob3], | ||||||||||||||||||
sccprob=SciMLBase.SCCNonlinearProblem((prob1, prob2, prob3), | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||
SciMLBase.Void{Any}.([explicitfun1, explicitfun2, explicitfun3])) | ||||||||||||||||||
scc_sol = solve(sccprob, NewtonRaphson()) | ||||||||||||||||||
|
||||||||||||||||||
# Test with SCCAlg that handles both nonlinear and linear problems | ||||||||||||||||||
using SCCNonlinearSolve | ||||||||||||||||||
scc_alg=SCCNonlinearSolve.SCCAlg(nlalg = NewtonRaphson(), linalg = nothing) | ||||||||||||||||||
scc_sol=solve(sccprob, scc_alg) | ||||||||||||||||||
Comment on lines
+64
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||
@test sol ≈ manualscc ≈ scc_sol | ||||||||||||||||||
|
||||||||||||||||||
# Backwards compat of alg choice | ||||||||||||||||||
scc_sol=solve(sccprob, NewtonRaphson()) | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||
@test sol ≈ manualscc ≈ scc_sol | ||||||||||||||||||
|
||||||||||||||||||
import NonlinearSolve # Required for Default | ||||||||||||||||||
|
||||||||||||||||||
scc_sol = solve(sccprob) | ||||||||||||||||||
@test sol ≈ manualscc ≈ scc_sol | ||||||||||||||||||
# Test default interface | ||||||||||||||||||
scc_sol_default=solve(sccprob) | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||
@test sol ≈ manualscc ≈ scc_sol_default | ||||||||||||||||||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[JuliaFormatter] reported by reviewdog 🐶