Switching lines makes results different

Hi, newbie here.

I have found a strange behaviour of the function below, particularly in the two bolded lines. It seems like the two vectors’ elements assume the last computed value at each i iteration, namely l[k].g_vel[i]

function Interpolate_Fields!(l::Vector{Layer}, number_of_layers, number_of_nodes, number_of_particles)
    for k in 2:number_of_layers + 1
        for i in 1:number_of_nodes
            velocity = Float64(0)
            thickness = Float64(0)
            correction = Float64(0)
            for j in 1:number_of_particles
                radius =l[k].g_pos[i] - l[k].p_pos[j]
                if (norm(radius) <= TWO * l[k].g_smooth_length[i] )
                    kernel_eval =  W(radius, l[k].g_smooth_length[i])
                    thickness = +(thickness, l[k].p_weight[j] * l[k].p_thickness[j] * kernel_eval)
                    velocity = +(velocity, l[k].p_weight[j] * l[k].p_vel[j] * kernel_eval)
                    correction = +(correction, l[k].p_weight[j] * kernel_eval )
                end
            end
            **l[k].g_thickness[i] = thickness / correction**
**            l[k].g_vel[i] = velocity / correction**
        end
    end
    println(l[2].g_vel)
    println(l[2].g_thickness)
end

For example l[k].g_thickness[1] is supposed to be equal to 1.2 and l[k].g_vel[1] equal to 600, but actually 600 is returned for both of them in this case:

            l[k].g_thickness[1] = thickness / correction 
            l[k].g_vel[1] = velocity / correction 

and 1.2 is returned for both in this other case:

            l[k].g_vel[1] = velocity / correction 
            l[k].g_thickness[1] = thickness / correction 

Am I missing something?

Thanks
Luca

You are probably assigning both labels to the same array. Use copy when you initialize them.

2 Likes