Dear all,
I am writing a script that will require many interpolations in a row (10000s).
I am trying to make it such that no allocation is made during the successive calls to Itp1D ()
. Therefore Iβve allocated all required arrays for interpolation initially. However, it stills allocates significant amounts of memory. Would anyone have hints on how to reduce the amount of allocations?
Many thanks in advance!
using Plots#, MAT
@views function Itp1D( Xc, Pc, iW, wW, Xdata, Pdata, dP, Pmin )
# Interpolate in 1D: X_W -----------o---x_E
iW .= Int64.(round.(trunc.( (Pc .- Pmin)./dP ) .+ 1)) # index of the west node is database
wW .= 1.0 .- (Pc - Pdata[iW])./dP
Xc .= wW .* Xdata[iW] .+ (1.0 .- wW) .* Xdata[iW.+1]
end
@views function main()
# 1. Generate data in 1D
Pf_data = LinRange(-0.5, 0.5, 1000) # data axis
X_data = exp.(-Pf_data.^2. / 0.05.^2) # data
dP = Pf_data[2] - Pf_data[1] # data spacing info
Pf_min = Pf_data[1] # data info
# 2. Generate a 2D space
ncx, ncy = 1000, 1000
xc = LinRange(-1, 1, ncx)
yc = LinRange(-1, 1, ncy)
xc2d = repeat(xc, 1, length(yc))
yc2d = repeat(yc, 1, length(xc))'
# 3. Some pre-allocated arrays
wW = zeros(ncx,ncy)
Xc = zeros(ncx,ncy)
iW = zeros(Int64,ncx,ncy) # Achtung: has to be Int since it's an index
Pfc = yc2d./4.0
# 4. Interpolate many times
@time for i=1:40
@views Itp1D( Xc, Pfc, iW, wW, X_data, Pf_data, dP, Pf_min )
end
# 5. Viz
# p = plot( Pf_data, X_data )
# p = plot!(Pfc[:], Xc[:], seriestype = :scatter)
# display(p)
end
main()
main()