Skip to content

Commit 4b8871c

Browse files
Merge pull request #67 from alyst/handles
Improve wrappers, add handles and NVector
2 parents 0b154e4 + a65a152 commit 4b8871c

23 files changed

+5427
-5234
lines changed

examples/cvode_Roberts_dns.jl

Lines changed: 38 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,76 @@
1-
2-
using Sundials
1+
using Sundials, Compat
32

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

6-
function f(t, y, ydot, user_data)
7-
y = Sundials.asarray(y)
8-
ydot = Sundials.asarray(ydot)
5+
function f(t, y_nv, ydot_nv, user_data)
6+
y = convert(Vector, y_nv)
7+
ydot = convert(Vector, ydot_nv)
98
ydot[1] = -0.04*y[1] + 1.0e4*y[2]*y[3]
109
ydot[3] = 3.0e7*y[2]*y[2]
1110
ydot[2] = -ydot[1] - ydot[3]
12-
return Int32(0)
11+
return Sundials.CV_SUCCESS
1312
end
1413

1514

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

18-
function g(t, y, gout, user_data)
19-
y = Sundials.asarray(y)
20-
gout = Sundials.asarray(gout, (2,))
17+
function g(t, y_nv, gout_ptr, user_data)
18+
y = convert(Vector, y_nv)
19+
gout = Sundials.asarray(gout_ptr, (2,))
2120
gout[1] = y[1] - 0.0001
2221
gout[2] = y[3] - 0.01
23-
return Int32(0)
22+
return Sundials.CV_SUCCESS
2423
end
2524

2625
## Jacobian routine. Compute J(t,y) = df/dy.
27-
28-
# Using StrPack
29-
# -- it works
30-
# -- it's clunky and dependent on Sundials version
31-
# Note: this is for Sundials v 2.4; the structure changed for v 2.5
32-
# Because of this, I'm commenting this out.
33-
## using StrPack
34-
## @struct type J_DlsMat
35-
## typ::Int32
36-
## M::Int32
37-
## N::Int32
38-
## ldim::Int32
39-
## mu::Int32
40-
## ml::Int32
41-
## s_mu::Int32
42-
## data::Ptr{Float64}
43-
## ldata::Int32
44-
## cols::Ptr{Ptr{Float64}}
45-
## end
46-
# Note: Here is the (untested) structure for v 2.5
47-
## using StrPack
48-
## @struct type J_DlsMat
49-
## typ::Int32
50-
## M::Int
51-
## N::Int
52-
## ldim::Int
53-
## mu::Int
54-
## ml::Int
55-
## s_mu::Int
56-
## data::Ptr{Float64}
57-
## ldata::Int
58-
## cols::Ptr{Ptr{Float64}}
59-
## end
60-
61-
# The following works if the code above is uncommented.
62-
function Jac(N, t, y, fy, Jptr, user_data,
63-
tmp1, tmp2, tmp3)
64-
y = Sundials.asarray(y)
65-
dlsmat = unpack(IOString(pointer_to_array(convert(Ptr{UInt8}, Jptr),
66-
(sum(map(sizeof, J_DlsMat.types))+10,))),
67-
J_DlsMat)
68-
J = pointer_to_array(unsafe_ref(dlsmat.cols), (int(neq), int(neq)), false)
26+
# broken -- needs a wrapper from Sundials._DlsMat to Matrix and Jac user function wrapper
27+
function Jac(N, t, ny, fy, Jptr, user_data, tmp1, tmp2, tmp3)
28+
y = convert(Vector, ny)
29+
dlsmat = unpack(IOString(@compat unsafe_wrap(convert(Ptr{UInt8}, Jptr),
30+
(sum(map(sizeof, Sundials._DlsMat))+10,), false)),
31+
Sundials._DlsMat)
32+
J = @compat unsafe_wrap(unsafe_ref(dlsmat.cols), (Int(neq), Int(neq)), false)
6933
J[1,1] = -0.04
7034
J[1,2] = 1.0e4*y[3]
7135
J[1,3] = 1.0e4*y[2]
7236
J[2,1] = 0.04
7337
J[2,2] = -1.0e4*y[3] - 6.0e7*y[2]
7438
J[2,3] = -1.0e4*y[2]
7539
J[3,2] = 6.0e7*y[2]
76-
return Int32(0)
40+
return Sundials.CV_SUCCESS
7741
end
7842

79-
neq = 3
43+
const neq = 3
8044

81-
t0 = 0.0
82-
t1 = 0.4
83-
tmult = 10.0
84-
nout = 12
85-
y = [1.0,0.0,0.0]
86-
reltol = 1e-4
87-
abstol = [1e-8, 1e-14, 1e-6]
45+
const t0 = 0.0
46+
const t1 = 0.4
47+
const tmult = 10.0
48+
const nout = 12
49+
const y0 = [1.0, 0.0, 0.0]
50+
const reltol = 1e-4
51+
const abstol = [1e-8, 1e-14, 1e-6]
8852

8953
cvode_mem = Sundials.CVodeCreate(Sundials.CV_BDF, Sundials.CV_NEWTON)
90-
flag = Sundials.CVodeInit(cvode_mem, f, t0, y)
91-
flag = Sundials.CVodeSVtolerances(cvode_mem, reltol, abstol)
92-
flag = Sundials.CVodeRootInit(cvode_mem, 2, g)
93-
flag = Sundials.CVDense(cvode_mem, neq)
94-
## flag = Sundials.CVDlsSetDenseJacFn(cvode_mem, Jac) # works, but clunky, see above
54+
Sundials.@checkflag Sundials.CVodeInit(cvode_mem, f, t0, y0)
55+
Sundials.@checkflag Sundials.CVodeSVtolerances(cvode_mem, reltol, abstol)
56+
Sundials.@checkflag Sundials.CVodeRootInit(cvode_mem, 2, g)
57+
Sundials.@checkflag Sundials.CVDense(cvode_mem, neq)
58+
#Sundials.@checkflag Sundials.CVDlsSetDenseJacFn(cvode_mem, Jac)
9559

9660
iout = 0
9761
tout = t1
62+
const t = [t0]
9863

99-
rootsfound = round(Int32,[0, 0])
100-
t = [t0]
101-
102-
while true
64+
while iout < nout
65+
y = similar(y0)
10366
flag = Sundials.CVode(cvode_mem, tout, y, t, Sundials.CV_NORMAL)
104-
println("T = ", tout, ", Y = ", y)
67+
println("T=", tout, ", Y=", y)
10568
if flag == Sundials.CV_ROOT_RETURN
106-
flagr = Sundials.CVodeGetRootInfo(cvode_mem, pointer(rootsfound))
107-
println("roots = ", rootsfound)
108-
end
109-
if flag == Sundials.CV_SUCCESS
69+
rootsfound = zeros(Cint, 2)
70+
Sundials.@checkflag Sundials.CVodeGetRootInfo(cvode_mem, rootsfound)
71+
println("roots=", rootsfound)
72+
elseif flag == Sundials.CV_SUCCESS
11073
iout += 1
11174
tout *= tmult
11275
end
113-
if iout == nout break end
11476
end

examples/cvode_Roberts_simplified.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ function f(t, y, ydot)
66
ydot[1] = -0.04*y[1] + 1.0e4*y[2]*y[3]
77
ydot[3] = 3.0e7*y[2]*y[2]
88
ydot[2] = -ydot[1] - ydot[3]
9-
return(Int32(0))
9+
return Sundials.CV_SUCCESS
1010
end
11-
t = [0.0; 4 * logspace(-1., 7., 9)]
12-
res = Sundials.cvode(f, [1.0, 0.0, 0.0], t)
11+
12+
const t = [0.0; 4 * logspace(-1., 7., 9)]
13+
const y0 = [1.0, 0.0, 0.0]
14+
res = Sundials.cvode(f, y0, t)
15+
16+
ts1, res3 = Sundials.cvode_fulloutput(f, y0, [0.0, 1.0])

examples/ida_Cable.jl

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,35 @@ using Grid ## for interpolating grids
1515
##
1616

1717
## Length of cable in um
18-
L = 4000
18+
const L = 4000
1919

2020
## Number of space steps to simulate
21-
dx = 10
22-
xsteps = round(Int,L/dx)
21+
const dx = 10.0
22+
const xsteps = round(Int, L/dx)
2323

2424
## Number of timesteps to simulate
25-
tf = 10.0
26-
dt = 0.1
27-
timesteps = round(Int, tf/dt)
28-
t = 0.0:dt:(dt*(timesteps-1))
25+
const tf = 10.0
26+
const dt = 0.1
27+
const timesteps = round(Int, tf/dt)
28+
const t = collect(0.0:dt:(dt*(timesteps-1)))
2929

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

3636

3737
## Define the injected current J
38-
J = zeros(timesteps,xsteps)
39-
I_in = 0.1
38+
const J = zeros(timesteps, xsteps)
39+
const I_in = 0.1
4040
## When to start injecting current
41-
I_delay = 1.0
41+
const I_delay = 1.0
4242
## Duration of injected current
43-
I_dur = 5.0
43+
const I_dur = 5.0
4444
J[max(1,round(Int,(I_delay)/dt)):round(Int,(I_delay+I_dur)/dt),round(Int,1*xsteps/2)] = I_in
4545

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

4848

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

5555
function cableres(t, u, up, r)
56-
5756
r[:] = u ## Initialize r to u, to take care of boundary equations.
5857

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

6665
end
6766

68-
return (0)
67+
return Sundials.CV_SUCCESS
6968
end
7069

7170

7271
function initial()
73-
7472
u = zeros(xsteps)
7573

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

8886
return (u,up,id)
89-
9087
end
9188

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

9793
neq = length(y0)
9894
mem = Sundials.IDACreate()
99-
flag = Sundials.IDAInit(mem, cfunction(Sundials.idasolfun, Int32,
100-
(Sundials.realtype, Sundials.N_Vector, Sundials.N_Vector, Sundials.N_Vector, Ref{Function})),
101-
t[1], nvector(y0), nvector(yp0))
102-
assert(flag == 0)
103-
flag = Sundials.IDASetId(mem,nvector(id))
104-
assert(flag == 0)
105-
flag = Sundials.IDASetUserData(mem, f)
106-
assert(flag == 0)
107-
flag = Sundials.IDASStolerances(mem, reltol, abstol)
108-
assert(flag == 0)
109-
mu = xsteps-1
110-
ml = xsteps-1
111-
flag = Sundials.IDABand(mem, neq, mu, ml)
112-
##flag = Sundials.IDADense(mem, neq)
113-
assert(flag == 0)
95+
Sundials.@checkflag Sundials.IDAInit(mem, cfunction(Sundials.idasolfun, Cint,
96+
(Sundials.realtype, Sundials.N_Vector, Sundials.N_Vector, Sundials.N_Vector, Ref{Function})),
97+
t[1], y0, yp0)
98+
Sundials.@checkflag Sundials.IDASetId(mem, id)
99+
Sundials.@checkflag Sundials.IDASetUserData(mem, f)
100+
Sundials.@checkflag Sundials.IDASStolerances(mem, reltol, abstol)
101+
Sundials.@checkflag Sundials.IDABand(mem, neq, xsteps-1, xsteps-1)
102+
##Sundials.@checkflag Sundials.IDADense(mem, neq)
114103
rtest = zeros(neq)
115-
flag = Sundials.IDACalcIC(mem, Sundials.IDA_YA_YDP_INIT, t[2])
116-
assert(flag == 0)
117-
yres = zeros(length(t), length(y0))
118-
ypres = zeros(length(t), length(y0))
119-
yres[1,:] = y0
120-
ypres[1,:] = yp0
104+
Sundials.@checkflag Sundials.IDACalcIC(mem, Sundials.IDA_YA_YDP_INIT, t[2])
105+
yres = zeros(Float64, length(y0), length(t))
106+
ypres = zeros(Float64, length(y0), length(t))
107+
yres[:, 1] = y0
108+
ypres[:, 1] = yp0
121109
y = copy(y0)
122110
yp = copy(yp0)
123111
tout = [0.0]
124112
for k in 2:length(t)
125-
retval = Sundials.IDASolve(mem, t[k], tout, y, yp, Sundials.IDA_NORMAL)
126-
yres[k,:] = y[:]
127-
ypres[k,:] = yp[:]
113+
Sundials.@checkflag Sundials.IDASolve(mem, t[k], tout, y, yp, Sundials.IDA_NORMAL)
114+
yres[:, k] = y
115+
ypres[:, k] = yp
128116
end
129117
return yres, ypres
130118
end

0 commit comments

Comments
 (0)