From 5c30d8a91be6182d8b8010ee81f26e520a3e81dc Mon Sep 17 00:00:00 2001 From: yonatanwesen Date: Tue, 17 Oct 2023 18:09:54 -0400 Subject: [PATCH 1/2] making sure kwargs are passed when converting ODEProblem to SteadyStateProblem and NonlinearProblem --- src/problems/basic_problems.jl | 6 ++--- src/problems/steady_state_problems.jl | 2 +- test/convert_tests.jl | 32 +++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/problems/basic_problems.jl b/src/problems/basic_problems.jl index 3c2430c58..5ab9d4cd6 100644 --- a/src/problems/basic_problems.jl +++ b/src/problems/basic_problems.jl @@ -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 @@ -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""" diff --git a/src/problems/steady_state_problems.jl b/src/problems/steady_state_problems.jl index ed4fd3a05..f25f15e27 100644 --- a/src/problems/steady_state_problems.jl +++ b/src/problems/steady_state_problems.jl @@ -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 diff --git a/test/convert_tests.jl b/test/convert_tests.jl index 3eec36da9..7374854c4 100644 --- a/test/convert_tests.jl +++ b/test/convert_tests.jl @@ -33,3 +33,35 @@ 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=p;a=1.0,b=2.0) + nlprob = NonlinearProblem(prob) + @test nlprob.kwargs[:p] == prob.kwargs[:p] + @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=p;a=1.0,b=2.0) + sprob = SteadyStateProblem(prob) + @test sprob.kwargs[:p] == prob.kwargs[:p] + @test sprob.kwargs[:a] == prob.kwargs[:a] + @test sprob.kwargs[:b] == prob.kwargs[:b] +end \ No newline at end of file From 2c13b5d7e81704c1009129c4eb97ee6fb01f5bf7 Mon Sep 17 00:00:00 2001 From: yonatanwesen Date: Tue, 17 Oct 2023 18:25:58 -0400 Subject: [PATCH 2/2] remove p from kwargs --- test/convert_tests.jl | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/convert_tests.jl b/test/convert_tests.jl index 7374854c4..20fba4f86 100644 --- a/test/convert_tests.jl +++ b/test/convert_tests.jl @@ -43,9 +43,8 @@ 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=p;a=1.0,b=2.0) + prob = ODEProblem(lorenz!, u0, tspan,p;a=1.0,b=2.0) nlprob = NonlinearProblem(prob) - @test nlprob.kwargs[:p] == prob.kwargs[:p] @test nlprob.kwargs[:a] == prob.kwargs[:a] @test nlprob.kwargs[:b] == prob.kwargs[:b] end @@ -59,9 +58,8 @@ 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=p;a=1.0,b=2.0) + prob = ODEProblem(lorenz!, u0, tspan,p;a=1.0,b=2.0) sprob = SteadyStateProblem(prob) - @test sprob.kwargs[:p] == prob.kwargs[:p] @test sprob.kwargs[:a] == prob.kwargs[:a] @test sprob.kwargs[:b] == prob.kwargs[:b] end \ No newline at end of file