Skip to content

Commit d9dcb5d

Browse files
committed
added @unsafe annotations to remaining examples.
1 parent e3adcff commit d9dcb5d

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

examples/scalar_law/PROGRAM0/main.jl

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
# Use the software at your own risk.
2222
#***********************************************************************
2323

24-
#import Base: getindex, setindex!
25-
#const getindex = Base.unsafe_getindex
26-
#const setindex! = Base.unsafe_setindex!
24+
using OffsetArrays # for @unsafe
2725

2826
function main()
2927

@@ -66,15 +64,15 @@ function main()
6664

6765
# uniform mesh:
6866
dx=(x_right-x_left)/ncells
69-
for ie in ifirst:ilast+1
67+
@unsafe for ie in ifirst:ilast+1
7068
x[ie+1]=x_left+ie*dx
7169
end
7270

7371
# initial values for diffential equation:
7472
ijump=max(ifirst-1,min(convert(Int,round(ncells*(jump-x_left)/(x_right-x_left))),ilast+1))
7573

7674
# left state to left of jump
77-
for ic=ifirst:ijump-1
75+
@unsafe for ic=ifirst:ijump-1
7876
u[ic+3]=statelft
7977
end
8078

@@ -83,13 +81,13 @@ function main()
8381
u[ijump+3]=statelft*frac+statergt*(1.0-frac)
8482

8583
# right state to right of jump
86-
for ic=ijump+1:ilast
84+
@unsafe for ic=ijump+1:ilast
8785
u[ic+3]=statergt
8886
end
8987

9088
# stable timestep (independent of time for linear advection):
9189
mindx=1.0e300
92-
for ic=ifirst:ilast
90+
@unsafe for ic=ifirst:ilast
9391
mindx=min(mindx,x[ic+2]-x[ic+1])
9492
end
9593
dt=cfl*mindx/abs(velocity)
@@ -100,24 +98,24 @@ function main()
10098
# loop over timesteps
10199
while istep < nsteps && t < tmax
102100
# right boundary condition: outgoing wave
103-
for ic=ncells:lc
101+
@unsafe for ic=ncells:lc
104102
u[ic+3]=u[ncells+2]
105103
end
106104

107105
# left boundary condition: specified value
108-
for ic=fc:-1
106+
@unsafe for ic=fc:-1
109107
u[ic+3]=statelft
110108
end
111109

112110
# upwind fluxes times dt (ie, flux time integral over cell side)
113111
# assumes velocity > 0
114112
vdt=velocity*dt
115-
for ie=ifirst:ilast+1
113+
@unsafe for ie=ifirst:ilast+1
116114
flux[ie+1]=vdt*u[ie+2]
117115
end
118116

119117
# conservative difference
120-
for ic=ifirst:ilast
118+
@unsafe for ic=ifirst:ilast
121119
u[ic+3] -= (flux[ic+2]-flux[ic+1]) / (x[ic+2]-x[ic+1])
122120
end
123121

@@ -127,7 +125,7 @@ function main()
127125
end
128126

129127
# write final results (plot later)
130-
for ic=0:ncells-1
128+
@unsafe for ic=0:ncells-1
131129
xc = (x[ic+1]+x[ic+2])*0.5
132130
uc = u[ic+3]
133131
#@printf("%e %e\n",xc,uc)

examples/scalar_law/PROGRAM0/main_sub.jl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#@inline getindex{T,N,P,IV}(V::SubArray{T,N,P,IV}, I::Union(Real, AbstractVector, Colon)...) = Base.unsafe_getindex(V, to_index(I)...)
2727
#@inline setindex!(V::SubArray, v, I::Int64...) = Base.unsafe_setindex!(V, v, I...)
2828

29+
using OffsetArrays # for @unsafe
30+
2931
macro shifted_array(T, r)
3032
:(sub(Array($T, length($r)), -minimum($r) + 2 : length($r)))
3133
end
@@ -45,23 +47,23 @@ function do_computation(nsteps, ncells, tmax, ifirst, ilast, statelft, statergt,
4547
# loop over timesteps
4648
while istep < nsteps && t < tmax
4749
# right boundary condition: outgoing wave
48-
for ic=ncells:lc
50+
@unsafe for ic=ncells:lc
4951
u[ic]=u[ncells-1]
5052
end
5153
# left boundary condition: specified value
52-
for ic=fc:-1
54+
@unsafe for ic=fc:-1
5355
u[ic]=statelft
5456
end
5557

5658
# upwind fluxes times dt (ie, flux time integral over cell side)
5759
# assumes velocity > 0
5860
vdt=velocity*dt
59-
for ie=ifirst:ilast+1
61+
@unsafe for ie=ifirst:ilast+1
6062
flux[ie]=vdt*u[ie-1]
6163
end
6264

6365
# conservative difference
64-
for ic=ifirst:ilast
66+
@unsafe for ic=ifirst:ilast
6567
u[ic] -= (flux[ic+1]-flux[ic]) / (x[ic+1]-x[ic])
6668
end
6769

@@ -113,27 +115,27 @@ function main()
113115

114116
# uniform mesh:
115117
dx=(x_right-x_left)/ncells
116-
for ie in ifirst:ilast+1
118+
@unsafe for ie in ifirst:ilast+1
117119
x[ie]=x_left+ie*dx
118120
end
119121

120122
# initial values for diffential equation:
121123
ijump=max(ifirst-1,min(convert(Int,round(ncells*(jump-x_left)/(x_right-x_left))),ilast+1))
122124
# left state to left of jump
123-
for ic=ifirst:ijump-1
125+
@unsafe for ic=ifirst:ijump-1
124126
u[ic]=statelft
125127
end
126128
# volume-weighted average in cell containing jump
127129
frac=(jump-x_left-ijump*dx)/(x_right-x_left)
128130
u[ijump]=statelft*frac+statergt*(1.0-frac)
129131
# right state to right of jump
130-
for ic=ijump+1:ilast
132+
@unsafe for ic=ijump+1:ilast
131133
u[ic]=statergt
132134
end
133135

134136
# stable timestep (independent of time for linear advection):
135137
mindx=1.0e300
136-
for ic=ifirst:ilast
138+
@unsafe for ic=ifirst:ilast
137139
mindx=min(mindx,x[ic+1]-x[ic])
138140
end
139141

@@ -142,7 +144,7 @@ function main()
142144
u = do_computation(nsteps, ncells, tmax, ifirst, ilast, statelft, statergt, velocity, dt, fc, lc, flux, x, u)
143145

144146
# write final results (plot later)
145-
for ic=0:ncells-1
147+
@unsafe for ic=0:ncells-1
146148
xc = (x[ic]+x[ic+1])*0.5
147149
uc = u[ic]
148150
#@printf("%e %e\n",xc,uc)

0 commit comments

Comments
 (0)