Skip to content

WIP: Add inplace version of cvode #76

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

Closed
wants to merge 2 commits into from
Closed
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
18 changes: 11 additions & 7 deletions src/simple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,13 @@ end
return: a solution matrix with time steps in `t` along rows and
state variable `y` along columns
"""
function cvode(f::Function, y0::Vector{Float64}, t::Vector{Float64}, userdata::Any=nothing;
integrator=:BDF, reltol::Float64=1e-3, abstol::Float64=1e-6)
cvode(f::Function, y0::Vector{Float64}, t::AbstractVector, userdata::Any=nothing;
integrator=:BDF, reltol::Float64=1e-3, abstol::Float64=1e-6) =
cvode!(f, y0, t, zeros(length(t), length(y0)), userdata;
integrator=integrator, reltol=reltol, abstol=abstol)

function cvode!(f::Function, y0::Vector{Float64}, t::AbstractVector, y::Matrix{Float64}, userdata::Any=nothing;
Copy link
Contributor

Choose a reason for hiding this comment

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

By convention, !-functions modify their 1st argument, so I would recommend putting y forward.
Also, the size of y has to be checked and DimensionMismatch thrown if it doesn't meet expectation.
With this check done, you can annotate the for k loop with @inbounds.

Copy link
Member

@ChrisRackauckas ChrisRackauckas Nov 23, 2016

Choose a reason for hiding this comment

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

I think @alyst 's note about the function signature order is the only remaining problem before merging. Is the author still around for this? Or should I just merge and make this quick change?

integrator=:BDF, reltol::Float64=1e-3, abstol::Float64=1e-6)
if integrator==:BDF
mem = CVodeCreate(CV_BDF, CV_NEWTON)
elseif integrator==:Adams
Expand All @@ -107,27 +112,26 @@ function cvode(f::Function, y0::Vector{Float64}, t::Vector{Float64}, userdata::A
if mem == C_NULL
error("Failed to allocate CVODE solver object")
end

yres = zeros(length(t), length(y0))
try
userfun = UserFunctionAndData(f, userdata)
y0nv = NVector(y0)
flag = @checkflag CVodeInit(mem, cfunction(cvodefun, Cint, (realtype, N_Vector, N_Vector, Ref{typeof(userfun)})), t[1], convert(N_Vector, y0nv))
flag = @checkflag CVodeSetUserData(mem, userfun)
flag = @checkflag CVodeSStolerances(mem, reltol, abstol)
flag = @checkflag CVDense(mem, length(y0))
yres[1,:] = y0
y[1,:] = y0
ynv = NVector(copy(y0))
tout = [0.0]
for k in 2:length(t)
flag = @checkflag CVode(mem, t[k], ynv, tout, CV_NORMAL)
yres[k,:] = convert(Vector, ynv)
y[k,:] = convert(Vector, ynv)
end
finally
CVodeFree(Ref{CVODEMemPtr}(mem))
end
return yres
return y
end


"""
`cvode_fulloutput(f::Function, y0::Vector{Float64}, tspan::Vector{Float64}, userdata::Any = nothing;
Expand Down