Skip to content

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

Merged
merged 2 commits into from
Aug 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/default.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ function SciMLBase.__solve(prob::NonlinearProblem, ::Nothing, args...; kwargs...
)
end

function SciMLBase.__init(prob::SciMLBase.AbstractSteadyStateProblem, ::Nothing, args...; kwargs...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
function SciMLBase.__init(prob::SciMLBase.AbstractSteadyStateProblem, ::Nothing, args...; kwargs...)
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...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
function SciMLBase.__solve(prob::SciMLBase.AbstractSteadyStateProblem, ::Nothing, args...; kwargs...)
function SciMLBase.__solve(
prob::SciMLBase.AbstractSteadyStateProblem, ::Nothing, args...; kwargs...)

# 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...
Expand Down
109 changes: 109 additions & 0 deletions test/default_alg_tests.jl
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

Copy link
Contributor

Choose a reason for hiding this comment

The 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)

Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The 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)

Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

prob_nl = NonlinearProblem(f_nl, u0)

Copy link
Contributor

Choose a reason for hiding this comment

The 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)

Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
@test sol_nl.u sol_converted.u atol=1e-10
@test sol_nl.usol_converted.u atol=1e-10

end

Copy link
Contributor

Choose a reason for hiding this comment

The 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)

Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The 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)

Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
end
end

Loading