Skip to content

Improve wrappers, add handles and NVector #67

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 24 commits into from
Sep 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
cd53580
wrap gen: use readdir() instead of pipeing `ls`
alyst Jul 19, 2016
9d08705
wrap gen: generate typed pointers to Sundials objs
alyst Jul 20, 2016
ba27040
wrap gen: make user_data vars of Any type
alyst Jul 20, 2016
8f163a1
wrap gen: allow typeify to return vector of expressions
alyst Jul 20, 2016
5483b84
wrap gen: add wrappers supporting Julia objects
alyst Aug 22, 2016
de84e41
basic test for cvode_fulloutput()
alyst Aug 24, 2016
b0c8fd3
fix typo
alyst Aug 27, 2016
3755f01
wrap gen: don't wrap in pointer() Ptr{FILE} arg
alyst Jul 24, 2016
2a8b483
add similar(NVector)
alyst Jul 22, 2016
efa6e75
wrap gen: don't wrap headers included from other headers
alyst Jul 27, 2016
d0da5ea
wrap gen: explicit Clong conversion for low-level methods
alyst Jul 27, 2016
c5f8d18
remove legacy wrapper code
alyst Jul 27, 2016
12c85f6
wrap gen: don't duplicate code to check for arg wrapping
alyst Jul 27, 2016
ff49447
wrap gen: Julia -> C function pointer wrappers
alyst Jul 27, 2016
aee12ae
update examples and add to the Pkg.test()
alyst Aug 22, 2016
482bd53
replace "return 0" with explicit "return SUCCESS"
alyst Aug 16, 2016
70302d1
whitespace fixes
alyst Aug 16, 2016
d77b7ae
runtests: more robust examples_path
alyst Aug 27, 2016
00e5979
runtests: run each example in separate let block
alyst Aug 27, 2016
4bbb556
convert(NVector, N_Vector)
alyst Aug 28, 2016
a489e89
wrap gen: create local NVector var in hilevel wrapper
alyst Aug 28, 2016
f7b6bec
wrap gen: normalize include path
alyst Aug 28, 2016
b277395
cleanup examples
alyst Aug 29, 2016
a65a152
cleanup wrapper generator
alyst Aug 29, 2016
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
114 changes: 38 additions & 76 deletions examples/cvode_Roberts_dns.jl
Original file line number Diff line number Diff line change
@@ -1,114 +1,76 @@

using Sundials
using Sundials, Compat

## f routine. Compute function f(t,y).

function f(t, y, ydot, user_data)
y = Sundials.asarray(y)
ydot = Sundials.asarray(ydot)
function f(t, y_nv, ydot_nv, user_data)
y = convert(Vector, y_nv)
ydot = convert(Vector, ydot_nv)
ydot[1] = -0.04*y[1] + 1.0e4*y[2]*y[3]
ydot[3] = 3.0e7*y[2]*y[2]
ydot[2] = -ydot[1] - ydot[3]
return Int32(0)
return Sundials.CV_SUCCESS
end


## g routine. Compute functions g_i(t,y) for i = 0,1.

function g(t, y, gout, user_data)
y = Sundials.asarray(y)
gout = Sundials.asarray(gout, (2,))
function g(t, y_nv, gout_ptr, user_data)
y = convert(Vector, y_nv)
gout = Sundials.asarray(gout_ptr, (2,))
gout[1] = y[1] - 0.0001
gout[2] = y[3] - 0.01
return Int32(0)
return Sundials.CV_SUCCESS
end

## Jacobian routine. Compute J(t,y) = df/dy.

# Using StrPack
# -- it works
# -- it's clunky and dependent on Sundials version
# Note: this is for Sundials v 2.4; the structure changed for v 2.5
# Because of this, I'm commenting this out.
## using StrPack
## @struct type J_DlsMat
## typ::Int32
## M::Int32
## N::Int32
## ldim::Int32
## mu::Int32
## ml::Int32
## s_mu::Int32
## data::Ptr{Float64}
## ldata::Int32
## cols::Ptr{Ptr{Float64}}
## end
# Note: Here is the (untested) structure for v 2.5
## using StrPack
## @struct type J_DlsMat
## typ::Int32
## M::Int
## N::Int
## ldim::Int
## mu::Int
## ml::Int
## s_mu::Int
## data::Ptr{Float64}
## ldata::Int
## cols::Ptr{Ptr{Float64}}
## end

# The following works if the code above is uncommented.
function Jac(N, t, y, fy, Jptr, user_data,
tmp1, tmp2, tmp3)
y = Sundials.asarray(y)
dlsmat = unpack(IOString(pointer_to_array(convert(Ptr{UInt8}, Jptr),
(sum(map(sizeof, J_DlsMat.types))+10,))),
J_DlsMat)
J = pointer_to_array(unsafe_ref(dlsmat.cols), (int(neq), int(neq)), false)
# broken -- needs a wrapper from Sundials._DlsMat to Matrix and Jac user function wrapper
function Jac(N, t, ny, fy, Jptr, user_data, tmp1, tmp2, tmp3)
y = convert(Vector, ny)
dlsmat = unpack(IOString(@compat unsafe_wrap(convert(Ptr{UInt8}, Jptr),
(sum(map(sizeof, Sundials._DlsMat))+10,), false)),
Sundials._DlsMat)
J = @compat unsafe_wrap(unsafe_ref(dlsmat.cols), (Int(neq), Int(neq)), false)
J[1,1] = -0.04
J[1,2] = 1.0e4*y[3]
J[1,3] = 1.0e4*y[2]
J[2,1] = 0.04
J[2,2] = -1.0e4*y[3] - 6.0e7*y[2]
J[2,3] = -1.0e4*y[2]
J[3,2] = 6.0e7*y[2]
return Int32(0)
return Sundials.CV_SUCCESS
end

neq = 3
const neq = 3

t0 = 0.0
t1 = 0.4
tmult = 10.0
nout = 12
y = [1.0,0.0,0.0]
reltol = 1e-4
abstol = [1e-8, 1e-14, 1e-6]
const t0 = 0.0
const t1 = 0.4
const tmult = 10.0
const nout = 12
const y0 = [1.0, 0.0, 0.0]
const reltol = 1e-4
const abstol = [1e-8, 1e-14, 1e-6]

cvode_mem = Sundials.CVodeCreate(Sundials.CV_BDF, Sundials.CV_NEWTON)
flag = Sundials.CVodeInit(cvode_mem, f, t0, y)
flag = Sundials.CVodeSVtolerances(cvode_mem, reltol, abstol)
flag = Sundials.CVodeRootInit(cvode_mem, 2, g)
flag = Sundials.CVDense(cvode_mem, neq)
## flag = Sundials.CVDlsSetDenseJacFn(cvode_mem, Jac) # works, but clunky, see above
Sundials.@checkflag Sundials.CVodeInit(cvode_mem, f, t0, y0)
Sundials.@checkflag Sundials.CVodeSVtolerances(cvode_mem, reltol, abstol)
Sundials.@checkflag Sundials.CVodeRootInit(cvode_mem, 2, g)
Sundials.@checkflag Sundials.CVDense(cvode_mem, neq)
#Sundials.@checkflag Sundials.CVDlsSetDenseJacFn(cvode_mem, Jac)

iout = 0
tout = t1
const t = [t0]

rootsfound = round(Int32,[0, 0])
t = [t0]

while true
while iout < nout
y = similar(y0)
flag = Sundials.CVode(cvode_mem, tout, y, t, Sundials.CV_NORMAL)
println("T = ", tout, ", Y = ", y)
println("T=", tout, ", Y=", y)
if flag == Sundials.CV_ROOT_RETURN
flagr = Sundials.CVodeGetRootInfo(cvode_mem, pointer(rootsfound))
println("roots = ", rootsfound)
end
if flag == Sundials.CV_SUCCESS
rootsfound = zeros(Cint, 2)
Sundials.@checkflag Sundials.CVodeGetRootInfo(cvode_mem, rootsfound)
println("roots=", rootsfound)
elseif flag == Sundials.CV_SUCCESS
iout += 1
tout *= tmult
end
if iout == nout break end
end
10 changes: 7 additions & 3 deletions examples/cvode_Roberts_simplified.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ function f(t, y, ydot)
ydot[1] = -0.04*y[1] + 1.0e4*y[2]*y[3]
ydot[3] = 3.0e7*y[2]*y[2]
ydot[2] = -ydot[1] - ydot[3]
return(Int32(0))
return Sundials.CV_SUCCESS
end
t = [0.0; 4 * logspace(-1., 7., 9)]
res = Sundials.cvode(f, [1.0, 0.0, 0.0], t)

const t = [0.0; 4 * logspace(-1., 7., 9)]
const y0 = [1.0, 0.0, 0.0]
res = Sundials.cvode(f, y0, t)

ts1, res3 = Sundials.cvode_fulloutput(f, y0, [0.0, 1.0])
80 changes: 34 additions & 46 deletions examples/ida_Cable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,35 @@ using Grid ## for interpolating grids
##

## Length of cable in um
L = 4000
const L = 4000

## Number of space steps to simulate
dx = 10
xsteps = round(Int,L/dx)
const dx = 10.0
const xsteps = round(Int, L/dx)

## Number of timesteps to simulate
tf = 10.0
dt = 0.1
timesteps = round(Int, tf/dt)
t = 0.0:dt:(dt*(timesteps-1))
const tf = 10.0
const dt = 0.1
const timesteps = round(Int, tf/dt)
const t = collect(0.0:dt:(dt*(timesteps-1)))

d = 4.0 * 1e-4 ## Cable diameter in cm
R_m = 2.5e11 ## Membrane resistance in Ohms/cm^2
G_m = 1.0/R_m ## Membrane conductance
R_i = 30.0 ## Longitudinal resistivity in Ohms*cm
C_m = 1e-6 ## Membrane capacitance in F/cm^2
const d = 4.0 * 1e-4 ## Cable diameter in cm
const R_m = 2.5e11 ## Membrane resistance in Ohms/cm^2
const G_m = 1.0/R_m ## Membrane conductance
const R_i = 30.0 ## Longitudinal resistivity in Ohms*cm
const C_m = 1e-6 ## Membrane capacitance in F/cm^2


## Define the injected current J
J = zeros(timesteps,xsteps)
I_in = 0.1
const J = zeros(timesteps, xsteps)
const I_in = 0.1
## When to start injecting current
I_delay = 1.0
const I_delay = 1.0
## Duration of injected current
I_dur = 5.0
const I_dur = 5.0
J[max(1,round(Int,(I_delay)/dt)):round(Int,(I_delay+I_dur)/dt),round(Int,1*xsteps/2)] = I_in

G_J = map(i -> CoordInterpGrid(t, vec(J[:,i]), 0.0, InterpQuadratic),1:xsteps)
const G_J = map(i -> CoordInterpGrid(t, vec(J[:,i]), 0.0, InterpQuadratic),1:xsteps)


##
Expand All @@ -53,7 +53,6 @@ G_J = map(i -> CoordInterpGrid(t, vec(J[:,i]), 0.0, InterpQuadratic),1:xsteps)
##

function cableres(t, u, up, r)

r[:] = u ## Initialize r to u, to take care of boundary equations.

## Loop over segments; set res = up - (central difference).
Expand All @@ -65,12 +64,11 @@ function cableres(t, u, up, r)

end

return (0)
return Sundials.CV_SUCCESS
end


function initial()

u = zeros(xsteps)

u[2:xsteps-2] = -60.0 ## initial value -60 mV
Expand All @@ -86,45 +84,35 @@ function initial()
up[:] = -1.0 * r

return (u,up,id)

end

nvector = Sundials.nvector
function idabandsol(f::Function, y0::Vector{Float64}, yp0::Vector{Float64},
id::Vector{Float64}, t::Vector{Float64};
reltol::Float64=1e-4, abstol::Float64=1e-6)

neq = length(y0)
mem = Sundials.IDACreate()
flag = Sundials.IDAInit(mem, cfunction(Sundials.idasolfun, Int32,
(Sundials.realtype, Sundials.N_Vector, Sundials.N_Vector, Sundials.N_Vector, Ref{Function})),
t[1], nvector(y0), nvector(yp0))
assert(flag == 0)
flag = Sundials.IDASetId(mem,nvector(id))
assert(flag == 0)
flag = Sundials.IDASetUserData(mem, f)
assert(flag == 0)
flag = Sundials.IDASStolerances(mem, reltol, abstol)
assert(flag == 0)
mu = xsteps-1
ml = xsteps-1
flag = Sundials.IDABand(mem, neq, mu, ml)
##flag = Sundials.IDADense(mem, neq)
assert(flag == 0)
Sundials.@checkflag Sundials.IDAInit(mem, cfunction(Sundials.idasolfun, Cint,
(Sundials.realtype, Sundials.N_Vector, Sundials.N_Vector, Sundials.N_Vector, Ref{Function})),
t[1], y0, yp0)
Sundials.@checkflag Sundials.IDASetId(mem, id)
Sundials.@checkflag Sundials.IDASetUserData(mem, f)
Sundials.@checkflag Sundials.IDASStolerances(mem, reltol, abstol)
Sundials.@checkflag Sundials.IDABand(mem, neq, xsteps-1, xsteps-1)
##Sundials.@checkflag Sundials.IDADense(mem, neq)
rtest = zeros(neq)
flag = Sundials.IDACalcIC(mem, Sundials.IDA_YA_YDP_INIT, t[2])
assert(flag == 0)
yres = zeros(length(t), length(y0))
ypres = zeros(length(t), length(y0))
yres[1,:] = y0
ypres[1,:] = yp0
Sundials.@checkflag Sundials.IDACalcIC(mem, Sundials.IDA_YA_YDP_INIT, t[2])
yres = zeros(Float64, length(y0), length(t))
ypres = zeros(Float64, length(y0), length(t))
yres[:, 1] = y0
ypres[:, 1] = yp0
y = copy(y0)
yp = copy(yp0)
tout = [0.0]
for k in 2:length(t)
retval = Sundials.IDASolve(mem, t[k], tout, y, yp, Sundials.IDA_NORMAL)
yres[k,:] = y[:]
ypres[k,:] = yp[:]
Sundials.@checkflag Sundials.IDASolve(mem, t[k], tout, y, yp, Sundials.IDA_NORMAL)
yres[:, k] = y
ypres[:, k] = yp
end
return yres, ypres
end
Expand Down
Loading