I have the following function:
function residual!(res, yd, y::MVector{S, SimFloat}, s::KPS3, time) where S
if false
T = S-2 # T: three times the number of particles excluding the origin
segments = div(T,6) - KITE_PARTICLES
# unpack the vectors y and yd
# extract the data for the winch simulation
length, v_reel_out = y[end-1], y[end]
lengthd, v_reel_outd = yd[end-1], yd[end]
# extract the data of the particles
y_ = @view y[1:end-2]
yd_ = @view yd[1:end-2]
# unpack the vectors y and yd
part = reshape(SVector{T}(y_), Size(3, div(T,6), 2))
partd = reshape(SVector{T}(yd_), Size(3, div(T,6), 2))
# pos1 = part[:,:,1]
# pos1, vel1 = part[:,:,1], part[:,:,2]
# pos = SVector{div(T,6)+1}(if i==1 SVector(0.0,0,0) else SVector(pos1[:,i-1]) end for i in 1:div(T,6)+1)
# vel = SVector{div(T,6)+1}(if i==1 SVector(0.0,0,0) else SVector(vel1[:,i-1]) end for i in 1:div(T,6)+1)
# posd1, veld1 = partd[:,:,1], partd[:,:,2]
# posd = SVector{div(T,6)+1}(if i==1 SVector(0.0,0,0) else SVector(posd1[:,i-1]) end for i in 1:div(T,6)+1)
# veld = SVector{div(T,6)+1}(if i==1 SVector(0.0,0,0) else SVector(veld1[:,i-1]) end for i in 1:div(T,6)+1)
else
part = reshape(SVector{S}(y), Size(3, div(S,6), 2))
partd = reshape(SVector{S}(yd), Size(3, div(S,6), 2))
pos1, vel1 = part[:,:,1], part[:,:,2]
pos = SVector{div(S,6)+1}(if i==1 SVector(0.0,0,0) else SVector(pos1[:,i-1]) end for i in 1:div(S,6)+1)
vel = SVector{div(S,6)+1}(if i==1 SVector(0.0,0,0) else SVector(vel1[:,i-1]) end for i in 1:div(S,6)+1)
posd1, veld1 = partd[:,:,1], partd[:,:,2]
posd = SVector{div(S,6)+1}(if i==1 SVector(0.0,0,0) else SVector(posd1[:,i-1]) end for i in 1:div(S,6)+1)
veld = SVector{div(S,6)+1}(if i==1 SVector(0.0,0,0) else SVector(veld1[:,i-1]) end for i in 1:div(S,6)+1)
end
# update parameters
pos_kite = pos[div(S,6)+1]
s.vel_kite .= vel[div(S,6)+1]
delta_t = time - s.t_0
delta_v = s.v_reel_out - s.last_v_reel_out
s.segment_length = (s.l_tether + s.last_v_reel_out * delta_t + 0.5 * delta_v * delta_t^2) / div(S,6)
s.c_spring = s.set.c_spring / s.segment_length
s.damping = s.set.damping / s.segment_length
s.beta = calc_elevation(s)
# call core calculation routines
vec_c = SVector{3, SimFloat}(pos[s.set.segments] - pos_kite) # convert to SVector to avoid allocations
v_app = SVector{3, SimFloat}(s.v_wind - s.vel_kite)
calc_set_cl_cd!(s, vec_c, v_app)
calc_aero_forces(s, pos_kite, s.vel_kite, s.rho, s.steering) # force at the kite
loop(s, pos, vel, posd, veld, s.res1, s.res2)
# copy and flatten result
for i in 2:div(S,6)+1
for j in 1:3
@inbounds res[3*(i-2)+j] = s.res1[i][j]
@inbounds res[3*(div(S,6))+3*(i-2)+j] = s.res2[i][j]
end
end
if norm(res) < 1e5
# println(norm(res))
for i in eachindex(pos)
@inbounds s.pos[i] .= pos[i]
end
end
# @assert ! isnan(norm(res))
s.iter += 1
nothing
end
It does not allocate any memory. If I uncomment the line # pos1 = part[:,:,1]
it suddenly allocates a lot of memory, even though this part of the code is never executed. How can that happen?
The full test can be found here: KiteModels.jl/bench3.jl at main Β· ufechner7/KiteModels.jl Β· GitHub