-
-
Notifications
You must be signed in to change notification settings - Fork 51
Add default algorithm dispatches for AbstractSteadyStateProblem #673
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 all 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 | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -37,6 +37,18 @@ function SciMLBase.__solve(prob::NonlinearProblem, ::Nothing, args...; kwargs... | |||||||
) | ||||||||
end | ||||||||
|
||||||||
function SciMLBase.__init(prob::SciMLBase.AbstractSteadyStateProblem, ::Nothing, args...; kwargs...) | ||||||||
# Convert SteadyStateProblem to NonlinearProblem and use its default | ||||||||
nlprob = SciMLBase.NonlinearProblem(prob) | ||||||||
return SciMLBase.__init(nlprob, nothing, args...; kwargs...) | ||||||||
end | ||||||||
|
||||||||
function SciMLBase.__solve(prob::SciMLBase.AbstractSteadyStateProblem, ::Nothing, args...; kwargs...) | ||||||||
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
|
||||||||
# Convert SteadyStateProblem to NonlinearProblem and use its default | ||||||||
nlprob = SciMLBase.NonlinearProblem(prob) | ||||||||
return SciMLBase.__solve(nlprob, nothing, args...; kwargs...) | ||||||||
end | ||||||||
|
||||||||
function SciMLBase.__init(prob::NonlinearLeastSquaresProblem, ::Nothing, args...; kwargs...) | ||||||||
return SciMLBase.__init( | ||||||||
prob, FastShortcutNLLSPolyalg(eltype(prob.u0)), args...; kwargs... | ||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,109 @@ | ||||||||||
@testitem "Default Algorithm for AbstractSteadyStateProblem" tags=[:core] begin | ||||||||||
using SciMLBase, StaticArrays | ||||||||||
|
||||||||||
# Test with in-place function | ||||||||||
function f_iip(du, u, p, t) | ||||||||||
du[1] = 2 - 2u[1] | ||||||||||
du[2] = u[1] - 4u[2] | ||||||||||
end | ||||||||||
|
||||||||||
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) | ||||||||||
prob_iip = SteadyStateProblem(f_iip, u0) | ||||||||||
|
||||||||||
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
|
||||||||||
@testset "In-place SteadyStateProblem" begin | ||||||||||
# Test with default algorithm (nothing) | ||||||||||
sol = solve(prob_iip) | ||||||||||
@test SciMLBase.successful_retcode(sol.retcode) | ||||||||||
@test maximum(abs, sol.resid) < 1e-6 | ||||||||||
|
||||||||||
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 with explicit nothing | ||||||||||
sol = solve(prob_iip, nothing) | ||||||||||
@test SciMLBase.successful_retcode(sol.retcode) | ||||||||||
@test maximum(abs, sol.resid) < 1e-6 | ||||||||||
|
||||||||||
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 init interface | ||||||||||
cache = init(prob_iip) | ||||||||||
sol = solve!(cache) | ||||||||||
@test SciMLBase.successful_retcode(sol.retcode) | ||||||||||
@test maximum(abs, sol.resid) < 1e-6 | ||||||||||
|
||||||||||
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 init with nothing | ||||||||||
cache = init(prob_iip, nothing) | ||||||||||
sol = solve!(cache) | ||||||||||
@test SciMLBase.successful_retcode(sol.retcode) | ||||||||||
@test maximum(abs, sol.resid) < 1e-6 | ||||||||||
end | ||||||||||
|
||||||||||
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 with out-of-place function | ||||||||||
f_oop(u, p, t) = [2 - 2u[1], u[1] - 4u[2]] | ||||||||||
u0 = zeros(2) | ||||||||||
prob_oop = SteadyStateProblem(f_oop, u0) | ||||||||||
|
||||||||||
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
|
||||||||||
@testset "Out-of-place SteadyStateProblem" begin | ||||||||||
# Test with default algorithm (nothing) | ||||||||||
sol = solve(prob_oop) | ||||||||||
@test SciMLBase.successful_retcode(sol.retcode) | ||||||||||
@test maximum(abs, sol.resid) < 1e-6 | ||||||||||
|
||||||||||
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 with explicit nothing | ||||||||||
sol = solve(prob_oop, nothing) | ||||||||||
@test SciMLBase.successful_retcode(sol.retcode) | ||||||||||
@test maximum(abs, sol.resid) < 1e-6 | ||||||||||
|
||||||||||
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 init interface | ||||||||||
cache = init(prob_oop) | ||||||||||
sol = solve!(cache) | ||||||||||
@test SciMLBase.successful_retcode(sol.retcode) | ||||||||||
@test maximum(abs, sol.resid) < 1e-6 | ||||||||||
|
||||||||||
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 init with nothing | ||||||||||
cache = init(prob_oop, nothing) | ||||||||||
sol = solve!(cache) | ||||||||||
@test SciMLBase.successful_retcode(sol.retcode) | ||||||||||
@test maximum(abs, sol.resid) < 1e-6 | ||||||||||
end | ||||||||||
|
||||||||||
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 that SteadyStateProblem conversion works | ||||||||||
@testset "Problem conversion" begin | ||||||||||
# Create equivalent NonlinearProblem | ||||||||||
function f_nl(u, p) | ||||||||||
[2 - 2u[1], u[1] - 4u[2]] | ||||||||||
end | ||||||||||
|
||||||||||
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
|
||||||||||
prob_nl = NonlinearProblem(f_nl, u0) | ||||||||||
|
||||||||||
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
|
||||||||||
# Convert SteadyStateProblem to NonlinearProblem | ||||||||||
prob_converted = NonlinearProblem(prob_oop) | ||||||||||
|
||||||||||
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
|
||||||||||
# Both should solve to the same solution | ||||||||||
sol_nl = solve(prob_nl) | ||||||||||
sol_converted = solve(prob_converted) | ||||||||||
|
||||||||||
@test sol_nl.u ≈ sol_converted.u atol=1e-10 | ||||||||||
Comment on lines
+81
to
+82
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 | ||||||||||
|
||||||||||
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 with StaticArrays | ||||||||||
@testset "StaticArrays support" begin | ||||||||||
f_static(u, p, t) = @SVector [2 - 2u[1], u[1] - 4u[2]] | ||||||||||
u0_static = @SVector [0.0, 0.0] | ||||||||||
prob_static = SteadyStateProblem(f_static, u0_static) | ||||||||||
|
||||||||||
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
|
||||||||||
sol = solve(prob_static) | ||||||||||
@test SciMLBase.successful_retcode(sol.retcode) | ||||||||||
@test maximum(abs, sol.resid) < 1e-6 | ||||||||||
end | ||||||||||
|
||||||||||
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 that solve works with various problem types | ||||||||||
@testset "Mixed problem types" begin | ||||||||||
# Regular arrays | ||||||||||
prob1 = SteadyStateProblem(f_oop, [0.5, 0.5]) | ||||||||||
sol1 = solve(prob1) | ||||||||||
@test SciMLBase.successful_retcode(sol1.retcode) | ||||||||||
|
||||||||||
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
|
||||||||||
# With parameters | ||||||||||
f_param(u, p, t) = [p[1] - 2u[1], u[1] - 4u[2]] | ||||||||||
prob2 = SteadyStateProblem(f_param, [0.5, 0.5], [2.0]) | ||||||||||
sol2 = solve(prob2) | ||||||||||
@test SciMLBase.successful_retcode(sol2.retcode) | ||||||||||
end | ||||||||||
end | ||||||||||
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
|
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 🐶