From 59908f328ecfa83d262cfe78f67e88b06438354a Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Sun, 2 Apr 2023 11:25:12 -0400 Subject: [PATCH 1/6] Update JacVec to SparseDiffTools v2 --- test/common_interface/jacobians.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/common_interface/jacobians.jl b/test/common_interface/jacobians.jl index 6c03e763..da23467a 100644 --- a/test/common_interface/jacobians.jl +++ b/test/common_interface/jacobians.jl @@ -33,7 +33,7 @@ sol9 = solve(prob, CVODE_BDF(; linear_solver = :KLU)) @test jac_called == true @test Array(sol9) ≈ Array(good_sol) -Lotka_fj = ODEFunction(Lotka; jac_prototype = JacVec(Lotka, ones(2))) +Lotka_fj = ODEFunction(Lotka; jac_prototype = JacVec((du,u)->Lotka(du,u,p,0.0), ones(2))) prob = ODEProblem(Lotka_fj, ones(2), (0.0, 10.0)) sol9 = solve(prob, CVODE_BDF(; linear_solver = :GMRES)) From 3b770a8b83568dbd63821ace221baba1ffc8fbab Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Tue, 2 May 2023 17:51:35 -0400 Subject: [PATCH 2/6] Update test/common_interface/jacobians.jl Co-authored-by: Oscar Smith --- test/common_interface/jacobians.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/common_interface/jacobians.jl b/test/common_interface/jacobians.jl index da23467a..e6d2d254 100644 --- a/test/common_interface/jacobians.jl +++ b/test/common_interface/jacobians.jl @@ -33,7 +33,7 @@ sol9 = solve(prob, CVODE_BDF(; linear_solver = :KLU)) @test jac_called == true @test Array(sol9) ≈ Array(good_sol) -Lotka_fj = ODEFunction(Lotka; jac_prototype = JacVec((du,u)->Lotka(du,u,p,0.0), ones(2))) +Lotka_fj = ODEFunction(Lotka; jac_prototype = JacVec((du,u)->Lotka(du,u,(),0.0), ones(2))) prob = ODEProblem(Lotka_fj, ones(2), (0.0, 10.0)) sol9 = solve(prob, CVODE_BDF(; linear_solver = :GMRES)) From bf865932caef35f8a3db002b04872acb4a97a8b3 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 3 May 2023 15:29:50 -0400 Subject: [PATCH 3/6] finish fixing --- src/common_interface/solve.jl | 6 +++++- src/nvector_wrapper.jl | 1 + test/common_interface/jacobians.jl | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/common_interface/solve.jl b/src/common_interface/solve.jl index fcc59b6e..4671cf14 100644 --- a/src/common_interface/solve.jl +++ b/src/common_interface/solve.jl @@ -215,7 +215,8 @@ function DiffEqBase.__init(prob::DiffEqBase.AbstractODEProblem{uType, tupType, i utmp = NVector(_u0) use_jac_prototype = (isa(prob.f.jac_prototype, SparseArrays.SparseMatrixCSC) && - LinearSolver ∈ SPARSE_SOLVERS) + LinearSolver ∈ SPARSE_SOLVERS) || + prob.f.jac_prototype isa AbstractSciMLOperator userfun = FunJac(f!, prob.f.jac, prob.p, @@ -225,6 +226,7 @@ function DiffEqBase.__init(prob::DiffEqBase.AbstractODEProblem{uType, tupType, i alg.psetup, u0, _u0) + @show userfun.jac_prototype function getcfunf(::T) where {T} @cfunction(cvodefunjac, Cint, (realtype, N_Vector, N_Vector, Ref{T})) @@ -340,7 +342,9 @@ function DiffEqBase.__init(prob::DiffEqBase.AbstractODEProblem{uType, tupType, i jac = nothing end + @show typeof(prob.f.jac_prototype) <: AbstractSciMLOperator if typeof(prob.f.jac_prototype) <: AbstractSciMLOperator + "here!!!!" function getcfunjtimes(::T) where {T} @cfunction(jactimes, Cint, diff --git a/src/nvector_wrapper.jl b/src/nvector_wrapper.jl index b4de0bc7..f5b0e585 100644 --- a/src/nvector_wrapper.jl +++ b/src/nvector_wrapper.jl @@ -77,6 +77,7 @@ Conversion happens in two steps within ccall: """ Base.cconvert(::Type{N_Vector}, v::Vector{realtype}) = convert(NVector, v) # will just return v if v is an NVector Base.unsafe_convert(::Type{N_Vector}, nv::NVector) = nv.n_v +Base.copy!(v::Vector,nv::Ptr{Sundials._generic_N_Vector}) = copy!(v,convert(NVector, nv)) Base.similar(nv::NVector) = NVector(similar(nv.v)) diff --git a/test/common_interface/jacobians.jl b/test/common_interface/jacobians.jl index e6d2d254..af8943d0 100644 --- a/test/common_interface/jacobians.jl +++ b/test/common_interface/jacobians.jl @@ -33,7 +33,7 @@ sol9 = solve(prob, CVODE_BDF(; linear_solver = :KLU)) @test jac_called == true @test Array(sol9) ≈ Array(good_sol) -Lotka_fj = ODEFunction(Lotka; jac_prototype = JacVec((du,u)->Lotka(du,u,(),0.0), ones(2))) +Lotka_fj = ODEFunction(Lotka; jac_prototype = JacVec((du,u)->Lotka(du,u,(),0.0), ones(2), SciMLBase.NullParameters())) prob = ODEProblem(Lotka_fj, ones(2), (0.0, 10.0)) sol9 = solve(prob, CVODE_BDF(; linear_solver = :GMRES)) From 6bcd20ca29445c474918e5803e8d3d41c78d8270 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 3 May 2023 15:40:44 -0400 Subject: [PATCH 4/6] better test --- test/common_interface/jacobians.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/common_interface/jacobians.jl b/test/common_interface/jacobians.jl index af8943d0..c1e91f68 100644 --- a/test/common_interface/jacobians.jl +++ b/test/common_interface/jacobians.jl @@ -21,6 +21,7 @@ end Lotka_f = ODEFunction(Lotka; jac = Lotka_jac) prob = ODEProblem(Lotka_f, ones(2), (0.0, 10.0)) good_sol = solve(prob, CVODE_BDF()) +testsol = solve(prob, CVODE_BDF(), saveat=0.1, abstol=1e-12, reltol=1e-12) @test jac_called == true Lotka_f = ODEFunction(Lotka; @@ -36,7 +37,8 @@ sol9 = solve(prob, CVODE_BDF(; linear_solver = :KLU)) Lotka_fj = ODEFunction(Lotka; jac_prototype = JacVec((du,u)->Lotka(du,u,(),0.0), ones(2), SciMLBase.NullParameters())) prob = ODEProblem(Lotka_fj, ones(2), (0.0, 10.0)) -sol9 = solve(prob, CVODE_BDF(; linear_solver = :GMRES)) +sol9 = solve(prob, CVODE_BDF(; linear_solver = :GMRES), saveat=0.1, abstol=1e-12, reltol=1e-12) +@test Array(sol9) ≈ Array(testsol) function f2!(res, du, u, p, t) res[1] = 1.01du[1] From 9f6493f14a93c2396941132b93a7b50ef7127598 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 3 May 2023 15:45:49 -0400 Subject: [PATCH 5/6] format --- src/common_interface/solve.jl | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/common_interface/solve.jl b/src/common_interface/solve.jl index 4671cf14..5132391e 100644 --- a/src/common_interface/solve.jl +++ b/src/common_interface/solve.jl @@ -215,8 +215,8 @@ function DiffEqBase.__init(prob::DiffEqBase.AbstractODEProblem{uType, tupType, i utmp = NVector(_u0) use_jac_prototype = (isa(prob.f.jac_prototype, SparseArrays.SparseMatrixCSC) && - LinearSolver ∈ SPARSE_SOLVERS) || - prob.f.jac_prototype isa AbstractSciMLOperator + LinearSolver ∈ SPARSE_SOLVERS) || + prob.f.jac_prototype isa AbstractSciMLOperator userfun = FunJac(f!, prob.f.jac, prob.p, @@ -226,7 +226,6 @@ function DiffEqBase.__init(prob::DiffEqBase.AbstractODEProblem{uType, tupType, i alg.psetup, u0, _u0) - @show userfun.jac_prototype function getcfunf(::T) where {T} @cfunction(cvodefunjac, Cint, (realtype, N_Vector, N_Vector, Ref{T})) @@ -342,7 +341,6 @@ function DiffEqBase.__init(prob::DiffEqBase.AbstractODEProblem{uType, tupType, i jac = nothing end - @show typeof(prob.f.jac_prototype) <: AbstractSciMLOperator if typeof(prob.f.jac_prototype) <: AbstractSciMLOperator "here!!!!" function getcfunjtimes(::T) where {T} From 5ee012dcde96eb3f39dd67043ae38a6cf14ef198 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Wed, 3 May 2023 15:45:56 -0400 Subject: [PATCH 6/6] more format? --- src/nvector_wrapper.jl | 2 +- test/common_interface/jacobians.jl | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/nvector_wrapper.jl b/src/nvector_wrapper.jl index f5b0e585..fe704a6d 100644 --- a/src/nvector_wrapper.jl +++ b/src/nvector_wrapper.jl @@ -77,7 +77,7 @@ Conversion happens in two steps within ccall: """ Base.cconvert(::Type{N_Vector}, v::Vector{realtype}) = convert(NVector, v) # will just return v if v is an NVector Base.unsafe_convert(::Type{N_Vector}, nv::NVector) = nv.n_v -Base.copy!(v::Vector,nv::Ptr{Sundials._generic_N_Vector}) = copy!(v,convert(NVector, nv)) +Base.copy!(v::Vector, nv::Ptr{Sundials._generic_N_Vector}) = copy!(v, convert(NVector, nv)) Base.similar(nv::NVector) = NVector(similar(nv.v)) diff --git a/test/common_interface/jacobians.jl b/test/common_interface/jacobians.jl index c1e91f68..6558a0e6 100644 --- a/test/common_interface/jacobians.jl +++ b/test/common_interface/jacobians.jl @@ -21,7 +21,7 @@ end Lotka_f = ODEFunction(Lotka; jac = Lotka_jac) prob = ODEProblem(Lotka_f, ones(2), (0.0, 10.0)) good_sol = solve(prob, CVODE_BDF()) -testsol = solve(prob, CVODE_BDF(), saveat=0.1, abstol=1e-12, reltol=1e-12) +testsol = solve(prob, CVODE_BDF(), saveat = 0.1, abstol = 1e-12, reltol = 1e-12) @test jac_called == true Lotka_f = ODEFunction(Lotka; @@ -34,10 +34,13 @@ sol9 = solve(prob, CVODE_BDF(; linear_solver = :KLU)) @test jac_called == true @test Array(sol9) ≈ Array(good_sol) -Lotka_fj = ODEFunction(Lotka; jac_prototype = JacVec((du,u)->Lotka(du,u,(),0.0), ones(2), SciMLBase.NullParameters())) +Lotka_fj = ODEFunction(Lotka; + jac_prototype = JacVec((du, u) -> Lotka(du, u, (), 0.0), ones(2), + SciMLBase.NullParameters())) prob = ODEProblem(Lotka_fj, ones(2), (0.0, 10.0)) -sol9 = solve(prob, CVODE_BDF(; linear_solver = :GMRES), saveat=0.1, abstol=1e-12, reltol=1e-12) +sol9 = solve(prob, CVODE_BDF(; linear_solver = :GMRES), saveat = 0.1, abstol = 1e-12, + reltol = 1e-12) @test Array(sol9) ≈ Array(testsol) function f2!(res, du, u, p, t)