Hi everyone,
I am trying to manipulate data using a vector of arrays. The aim is to store the connectivity of a Poisson finite difference stencil using vector of arrays, rather than storing them into independent vectors.
To this end, I’ve initialised a data structure Tsten
that contains at entry [1]
indices of center nodes, entries [2]
and [3]
are west and east nodes, respectively.
In the “independent vectors” version I use correspondingly TC
, TW
and TE
.
-
I want to populate the first entry of
Tsten
(Tsten[1] .= T) and this, introduce entries inTsten[2]
andTsten[3]
, which is not what I would have expected. Any idea of what is wrong there? -
I want to populate
Tsten[2]
andTsten[3]
using the functionPoissonSparsity1D_v1!
and I expect same results than using an “independent vectors” approach usingPoissonSparsity1D!
. But it is not the case. It is likely related to the previous point. Would anyone have some hints? -
Finally, it would be nice to ‘merge’
PoissonSparsity1D_v1
andPoissonSparsity1D
using multiple dispatch (such that a single function apply to either a vector of arrays or independent vectors). But this is a next step…
Cheers!
function PoissonSparsity1D!(TC, TW, TE)
TW[2:end-0] .= TC[1:end-1];
TE[1:end-1] .= TC[2:end-0];
end
function PoissonSparsity1D_v1!(x)
TC, TW, TE = x
TW[2:end-0] .= TC[1:end-1];
TE[1:end-1] .= TC[2:end-0];
end
function PrintAll(Tsten, TC, TW, TE)
println( "Center: ", TC )
println( "Center: ", Tsten[1] )
println( "West: ", TW )
println( "West: ", Tsten[2] )
println( "East: ", TE )
println( "East: ", Tsten[3] )
end
function main()
# Initialise
ncx = 5
TC, TW, TE = fill(0, ncx), fill(0, ncx), fill(0, ncx) # independent arrays
T = collect(1:ncx)
# Define and allocate vector of arrays
Tsten = Array{Vector{Int64}}(undef, 3);
fill!(Tsten, zeros(ncx))
# Initially all entries are zero
println("Check #1")
PrintAll(Tsten, TC, TW, TE)
# Now let's just put non zero values in the first vector
Tsten[1] .= T
# Why are there non-zeros in Tsten[i] with i>1?
println("Check #2 - Why are there non-zeros in Tsten[i] with i>1?")
PrintAll(Tsten, TC, TW, TE)
# Here, I try to generate the connectivity - similar results are expected
TC .= T
PoissonSparsity1D!(TC,TW,TE)
Tsten[1] .= T
PoissonSparsity1D_v1!(Tsten)
println("Check #3 - Then, results also differ there:")
PrintAll(Tsten, TC, TW, TE)
end
for it=1:1
@time main()
end