Skip to content

Commit

Permalink
Merge pull request #524 from yonatanwesen/yd/pass-kwargs
Browse files Browse the repository at this point in the history
kwargs are passed when converting ODEProblem to SteadyStateProblem and NonlinearProblem
  • Loading branch information
ChrisRackauckas authored Oct 17, 2023
2 parents 7afe983 + 2c13b5d commit 578352c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/problems/basic_problems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ struct NonlinearProblem{uType, isinplace, P, F, K, PT} <:
`isinplace` optionally sets whether the function is inplace or not.
This is determined automatically, but not inferred.
"""
function NonlinearProblem{iip}(f, u0, p = NullParameters()) where {iip}
NonlinearProblem{iip}(NonlinearFunction{iip}(f), u0, p)
function NonlinearProblem{iip}(f, u0, p = NullParameters(); kwargs...) where {iip}
NonlinearProblem{iip}(NonlinearFunction{iip}(f), u0, p; kwargs...)
end
end

Expand Down Expand Up @@ -309,7 +309,7 @@ this is interpreted in the form of the steady state problem, i.e.
find the ODE's solution at time ``t = \\infty``
"""
function NonlinearProblem(prob::AbstractODEProblem)
NonlinearProblem{isinplace(prob)}(prob.f, prob.u0, prob.p)
NonlinearProblem{isinplace(prob)}(prob.f, prob.u0, prob.p; prob.kwargs...)
end

@doc doc"""
Expand Down
2 changes: 1 addition & 1 deletion src/problems/steady_state_problems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,5 @@ $(SIGNATURES)
Define a steady state problem from a standard ODE problem.
"""
function SteadyStateProblem(prob::AbstractODEProblem)
SteadyStateProblem{isinplace(prob)}(prob.f, prob.u0, prob.p)
SteadyStateProblem{isinplace(prob)}(prob.f, prob.u0, prob.p; prob.kwargs...)
end
30 changes: 30 additions & 0 deletions test/convert_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,33 @@ end
prob = ODEProblem(lorenz!, u0, tspan)
nlprob = NonlinearProblem(prob)
end

@testset "Convert ODEProblem with kwargs to NonlinearProblem" begin
function lorenz!(du, u, p, t)
du[1] = p[1]*(u[2] - u[1])
du[2] = u[1] * (p[2] - u[3]) - u[2]
du[3] = u[1] * u[2] - p[3] * u[3]
end
u0 = [1.0; 0.0; 0.0]
tspan = (0.0, 100.0)
p = [10.0,28.0,8/3]
prob = ODEProblem(lorenz!, u0, tspan,p;a=1.0,b=2.0)
nlprob = NonlinearProblem(prob)
@test nlprob.kwargs[:a] == prob.kwargs[:a]
@test nlprob.kwargs[:b] == prob.kwargs[:b]
end

@testset "Convert ODEProblem with kwargs to SteadyStateProblem" begin
function lorenz!(du, u, p, t)
du[1] = p[1]*(u[2] - u[1])
du[2] = u[1] * (p[2] - u[3]) - u[2]
du[3] = u[1] * u[2] - p[3] * u[3]
end
u0 = [1.0; 0.0; 0.0]
tspan = (0.0, 100.0)
p = [10.0,28.0,8/3]
prob = ODEProblem(lorenz!, u0, tspan,p;a=1.0,b=2.0)
sprob = SteadyStateProblem(prob)
@test sprob.kwargs[:a] == prob.kwargs[:a]
@test sprob.kwargs[:b] == prob.kwargs[:b]
end

0 comments on commit 578352c

Please sign in to comment.