Skip to content

Commit 47743b7

Browse files
committed
cleanup wrapper generator
* add comments * don't cd() to the directory where the headers are generated * don't require JULIAHOME env variable
1 parent f7b6bec commit 47743b7

9 files changed

+199
-253
lines changed

examples/cvode_Roberts_dns.jl

Lines changed: 32 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ using Sundials, Compat
22

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

5-
function f(t, y, ydot, user_data)
6-
y = Sundials.asarray(y)
7-
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)
88
ydot[1] = -0.04*y[1] + 1.0e4*y[2]*y[3]
99
ydot[3] = 3.0e7*y[2]*y[2]
1010
ydot[2] = -ydot[1] - ydot[3]
@@ -14,56 +14,21 @@ end
1414

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

17-
function g(t, y, gout, user_data)
18-
y = Sundials.asarray(y)
19-
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,))
2020
gout[1] = y[1] - 0.0001
2121
gout[2] = y[3] - 0.01
2222
return Sundials.CV_SUCCESS
2323
end
2424

2525
## Jacobian routine. Compute J(t,y) = df/dy.
26-
27-
# Using StrPack
28-
# -- it works
29-
# -- it's clunky and dependent on Sundials version
30-
# Note: this is for Sundials v 2.4; the structure changed for v 2.5
31-
# Because of this, I'm commenting this out.
32-
## using StrPack
33-
## @struct type J_DlsMat
34-
## typ::Int32
35-
## M::Int32
36-
## N::Int32
37-
## ldim::Int32
38-
## mu::Int32
39-
## ml::Int32
40-
## s_mu::Int32
41-
## data::Ptr{Float64}
42-
## ldata::Int32
43-
## cols::Ptr{Ptr{Float64}}
44-
## end
45-
# Note: Here is the (untested) structure for v 2.5
46-
## using StrPack
47-
## @struct type J_DlsMat
48-
## typ::Int32
49-
## M::Int
50-
## N::Int
51-
## ldim::Int
52-
## mu::Int
53-
## ml::Int
54-
## s_mu::Int
55-
## data::Ptr{Float64}
56-
## ldata::Int
57-
## cols::Ptr{Ptr{Float64}}
58-
## end
59-
60-
# The following works if the code above is uncommented.
61-
function Jac(N, t, y, fy, Jptr, user_data,
62-
tmp1, tmp2, tmp3)
63-
y = Sundials.asarray(y)
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)
6429
dlsmat = unpack(IOString(@compat unsafe_wrap(convert(Ptr{UInt8}, Jptr),
65-
(sum(map(sizeof, J_DlsMat.types))+10,), false)),
66-
J_DlsMat)
30+
(sum(map(sizeof, Sundials._DlsMat))+10,), false)),
31+
Sundials._DlsMat)
6732
J = @compat unsafe_wrap(unsafe_ref(dlsmat.cols), (Int(neq), Int(neq)), false)
6833
J[1,1] = -0.04
6934
J[1,2] = 1.0e4*y[3]
@@ -75,39 +40,37 @@ function Jac(N, t, y, fy, Jptr, user_data,
7540
return Sundials.CV_SUCCESS
7641
end
7742

78-
neq = 3
43+
const neq = 3
7944

80-
t0 = 0.0
81-
t1 = 0.4
82-
tmult = 10.0
83-
nout = 12
84-
y = [1.0,0.0,0.0]
85-
reltol = 1e-4
86-
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]
8752

8853
cvode_mem = Sundials.CVodeCreate(Sundials.CV_BDF, Sundials.CV_NEWTON)
89-
flag = Sundials.CVodeInit(cvode_mem, f, t0, y)
90-
flag = Sundials.CVodeSVtolerances(cvode_mem, reltol, abstol)
91-
flag = Sundials.CVodeRootInit(cvode_mem, Cint(2), g)
92-
flag = Sundials.CVDense(cvode_mem, neq)
93-
## 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)
9459

9560
iout = 0
9661
tout = t1
62+
const t = [t0]
9763

98-
rootsfound = zeros(Cint, 2)
99-
t = [t0]
100-
101-
while true
64+
while iout < nout
65+
y = similar(y0)
10266
flag = Sundials.CVode(cvode_mem, tout, y, t, Sundials.CV_NORMAL)
103-
println("T = ", tout, ", Y = ", y)
67+
println("T=", tout, ", Y=", y)
10468
if flag == Sundials.CV_ROOT_RETURN
105-
flagr = Sundials.CVodeGetRootInfo(cvode_mem, rootsfound)
106-
println("roots = ", rootsfound)
107-
end
108-
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
10973
iout += 1
11074
tout *= tmult
11175
end
112-
if iout == nout break end
11376
end

examples/cvode_Roberts_simplified.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ function f(t, y, ydot)
99
return Sundials.CV_SUCCESS
1010
end
1111

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

1616
ts1, res3 = Sundials.cvode_fulloutput(f, y0, [0.0, 1.0])

examples/ida_Cable.jl

Lines changed: 33 additions & 42 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
##
@@ -86,42 +86,33 @@ function initial()
8686
return (u,up,id)
8787
end
8888

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

9493
neq = length(y0)
9594
mem = Sundials.IDACreate()
96-
flag = Sundials.IDAInit(mem, cfunction(Sundials.idasolfun, Int32,
97-
(Sundials.realtype, Sundials.N_Vector, Sundials.N_Vector, Sundials.N_Vector, Ref{Function})),
98-
t[1], nvector(y0), nvector(yp0))
99-
assert(flag == Sundials.CV_SUCCESS)
100-
flag = Sundials.IDASetId(mem,nvector(id))
101-
assert(flag == Sundials.CV_SUCCESS)
102-
flag = Sundials.IDASetUserData(mem, f)
103-
assert(flag == Sundials.CV_SUCCESS)
104-
flag = Sundials.IDASStolerances(mem, reltol, abstol)
105-
assert(flag == Sundials.CV_SUCCESS)
106-
mu = xsteps-1
107-
ml = xsteps-1
108-
flag = Sundials.IDABand(mem, neq, mu, ml)
109-
##flag = Sundials.IDADense(mem, neq)
110-
assert(flag == Sundials.CV_SUCCESS)
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)
111103
rtest = zeros(neq)
112-
flag = Sundials.IDACalcIC(mem, Sundials.IDA_YA_YDP_INIT, t[2])
113-
assert(flag == Sundials.CV_SUCCESS)
114-
yres = zeros(length(t), length(y0))
115-
ypres = zeros(length(t), length(y0))
116-
yres[1,:] = y0
117-
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
118109
y = copy(y0)
119110
yp = copy(yp0)
120111
tout = [0.0]
121112
for k in 2:length(t)
122-
retval = Sundials.IDASolve(mem, t[k], tout, y, yp, Sundials.IDA_NORMAL)
123-
yres[k,:] = y[:]
124-
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
125116
end
126117
return yres, ypres
127118
end

examples/ida_Heat2D.jl

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -102,51 +102,40 @@ function initial()
102102
return (u,up,id,constraints)
103103
end
104104

105-
nvector = Sundials.nvector
106105
function idabandsol(f::Function, y0::Vector{Float64}, yp0::Vector{Float64},
107106
id::Vector{Float64}, constraints::Vector{Float64},
108107
t::Vector{Float64};
109108
reltol::Float64=1e-4, abstol::Float64=1e-6)
110-
111109
neq = length(y0)
112110
mem = Sundials.IDACreate()
113-
flag = Sundials.IDAInit(mem, cfunction(Sundials.idasolfun, Int32,
114-
(Sundials.realtype, Sundials.N_Vector, Sundials.N_Vector, Sundials.N_Vector, Ref{Function})),
115-
t[1], nvector(y0), nvector(yp0))
116-
assert(flag == Sundials.CV_SUCCESS)
117-
flag = Sundials.IDASetId(mem,nvector(id))
118-
assert(flag == Sundials.CV_SUCCESS)
119-
flag = Sundials.IDASetConstraints(mem,nvector(constraints))
120-
assert(flag == Sundials.CV_SUCCESS)
121-
flag = Sundials.IDASetUserData(mem, f)
122-
assert(flag == Sundials.CV_SUCCESS)
123-
flag = Sundials.IDASStolerances(mem, reltol, abstol)
124-
assert(flag == Sundials.CV_SUCCESS)
125-
mu = MGRID
126-
ml = MGRID
127-
flag = Sundials.IDABand(mem, neq, mu, ml)
128-
assert(flag == Sundials.CV_SUCCESS)
111+
Sundials.@checkflag Sundials.IDAInit(mem, cfunction(Sundials.idasolfun, Cint,
112+
(Sundials.realtype, Sundials.N_Vector, Sundials.N_Vector, Sundials.N_Vector, Ref{Function})),
113+
t[1], y0, yp0)
114+
Sundials.@checkflag Sundials.IDASetId(mem, id)
115+
Sundials.@checkflag Sundials.IDASetConstraints(mem, constraints)
116+
Sundials.@checkflag Sundials.IDASetUserData(mem, f)
117+
Sundials.@checkflag Sundials.IDASStolerances(mem, reltol, abstol)
118+
Sundials.@checkflag Sundials.IDABand(mem, neq, MGRID, MGRID)
129119
rtest = zeros(neq)
130-
flag = Sundials.IDACalcIC(mem, Sundials.IDA_YA_YDP_INIT, t[2])
131-
assert(flag == Sundials.CV_SUCCESS)
132-
yres = zeros(length(t), length(y0))
133-
ypres = zeros(length(t), length(y0))
134-
yres[1,:] = y0
135-
ypres[1,:] = yp0
120+
Sundials.@checkflag Sundials.IDACalcIC(mem, Sundials.IDA_YA_YDP_INIT, t[2])
121+
yres = zeros(Float64, length(y0), length(t))
122+
ypres = zeros(Float64, length(y0), length(t))
123+
yres[:, 1] = y0
124+
ypres[:, 1] = yp0
136125
y = copy(y0)
137126
yp = copy(yp0)
138127
tout = [0.0]
139128
for k in 2:length(t)
140-
retval = Sundials.IDASolve(mem, t[k], tout, y, yp, Sundials.IDA_NORMAL)
141-
yres[k,:] = y[:]
142-
ypres[k,:] = yp[:]
129+
Sundials.@checkflag Sundials.IDASolve(mem, t[k], tout, y, yp, Sundials.IDA_NORMAL)
130+
yres[:, k] = y
131+
ypres[:, k] = yp
143132
end
144133
return yres, ypres
145134
end
146135

147-
nsteps = 10
148-
tstep = 0.005
149-
t = 0.0:tstep:(tstep*nsteps)
136+
const nsteps = 10
137+
const tstep = 0.005
138+
const t = collect(0.0:tstep:(tstep*nsteps))
150139
u0, up0, id, constraints = initial()
151140

152141
yout, ypout = idabandsol(heatres, u0, up0, id, constraints, map(x -> x, t),

0 commit comments

Comments
 (0)