Skip to content

Commit 79dc5a4

Browse files
committed
rewrite interpolate_grid to improve type stability
1 parent 3af5ea2 commit 79dc5a4

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

src/geometry.jl

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,41 +135,60 @@ Interpolates the grid `xyz` along direction `dir`
135135
- `ydir`: Dependent variable direction `xyz` (`i=1`, `j=2`)
136136
"""
137137
function interpolate_grid(xyz, eta, interp; xdir=0, ydir=1)
138-
138+
139139
y = nothing
140-
z = nothing
141140

142141
ydim = ydir + 1
143142

144143
ni = size(xyz, ydim)
145144

146-
xyz = mapslices(xyz; dims=[1,ydim]) do xyz_i
145+
if ydir == 2
146+
xyz_new = Array{eltype(xyz)}(undef, size(xyz,1), size(xyz,2), length(eta))
147+
elseif ydir == 1
148+
xyz_new = Array{eltype(xyz)}(undef, size(xyz,1), length(eta), size(xyz,3))
149+
else
150+
error("ydir must be 1 or 2")
151+
end
152+
153+
if ydir == 1
154+
dim = 3
155+
else
156+
dim = 2
157+
end
158+
159+
for i in axes(xyz_new, dim)
160+
if ydir == 2
161+
xyz_i = xyz[:,i,:]
162+
elseif ydir == 1
163+
xyz_i = xyz[:,:,i]
164+
end
147165

148-
# get distance between each grid location
149166
if xdir == 0
150167
ds = [norm(xyz_i[:,k] - xyz_i[:,max(1,k-1)]) for k = 1:ni]
151168
else
152169
ds = [xyz_i[xdir,k] - xyz_i[xdir,max(1,k-1)] for k = 1:ni]
153170
end
154-
155171
# create interpolation vector
156172
t = cumsum(ds)
157173

158174
# normalize interpolation vector
159175
t /= t[end]
160176

161-
# interpolate x, y, and z
162177
x = interp(t, xyz_i[1,:], eta)
163178
if !isnothing(y) && ydir==2
164179
else
165180
y = interp(t, xyz_i[2,:], eta)
166181
end
167182
z = interp(t, xyz_i[3,:], eta)
168183

169-
vcat(x',y',z')
184+
if ydir == 2
185+
xyz_new[:,i,:] = vcat(x',y',z')
186+
else
187+
xyz_new[:,:,i] = vcat(x',y',z')
188+
end
170189
end
171190

172-
return xyz
191+
return xyz_new
173192
end
174193

175194
"""

0 commit comments

Comments
 (0)