Hi, I found a very weird thing when using SparseMatrixCSC
:
K is a SparseMatrixCSC
created from some complicated process (Finite element analysis application), here I show part of it:
K = spzeros(ndof, ndof) #ndof : num of degree of freedom
# Assemble stiffness matrix
for i = 1:nel # traverse finite element
rowIdx = iK[1:8, (i-1)*8+1 : i*8]
colIdx = jK[:, i]
K_colptr, K_rowval, K_nzval_idx = build_sparseCSC_input(rowIdx[:], colIdx[:])
K_nzval = (sK[:][K_nzval_idx])[:]
# add element stiffness matrix to global
K += SparseMatrixCSC(ndof, ndof, K_colptr, K_rowval, K_nzval)
end
build_sparseCSC_input(rowIdx[:], colIdx[:])
is used to transfrom input of sparse
to input of SparseMatrixCSC
. The content of it I will show at the end.
The result of K is
8Ă—8 SparseMatrixCSC{Float64,Int64} with 151 stored entries:
[1, 1] = 0.366667
[2, 1] = 0.175
[3, 1] = 0.0833333
[4, 1] = 0.075
[5, 1] = -0.533333
[1, 1] = 0.366667
â‹®
[1, 8] = -0.175
[2, 8] = -0.183333
[5, 8] = -2.77556e-17
[6, 8] = -0.533333
[1, 8] = 0.175
[2, 8] = -0.183333
A weird thing happened: 8Ă—8 sparse matrix has 151 stored entries!
When I show K
@show K
It gives:
K =
[1, 1] = 0.366667
[2, 1] = 0.175 <== weird
[3, 1] = 0.0833333
[4, 1] = 0.075
[5, 1] = -0.533333
[1, 1] = 0.366667
[2, 1] = -0.175 <== weird
...
[2, 8] = -0.183333
[3, 8] = 0.075 <== weird
[4, 8] = 0.0833333
[8, 8] = 1.46667
[3, 8] = -0.15 <== weird
[4, 8] = 0.166667
[1, 8] = -0.175
[2, 8] = -0.183333
[5, 8] = -2.77556e-17
[6, 8] = -0.533333
[1, 8] = 0.175
[2, 8] = -0.183333
You can see there are some duplicated entries like [2,1],[2,8],[3,8] with different values!
Is this a bug of Julia SparseMatrixCSC? My Julia version is v"1.5.3"
Appendix: content of build_sparseCSC_input
, which is used to transfrom input of sparse
to input of SparseMatrixCSC
function build_sparseCSC_input(rowIdx, colIdx)
n = length(colIdx)
colptr = [1]
rowval = Array{Int}([])
nzval_idx = Array{Int}([])
used_flags = zeros(n, 1)
j = 1
m = 0 # num of allocated value
while(m < n)
for i = 1:n
if(used_flags[i] == 0) # if not used
col = colIdx[i]
if(col == j) # if the value is in the current column
push!(nzval_idx, i)
push!(rowval, rowIdx[i])
m += 1
used_flags[i] = 1 # set this entry as used
end
end
end
j += 1 # increase j to next column
push!(colptr, m+1) #update colptr
end
return colptr, rowval, nzval_idx
end