Skip to content

Commit df59b7a

Browse files
Enable assembly of rect blocks into nxn CSR/CSC matrix, as suggested by Knut.
1 parent 4fca300 commit df59b7a

3 files changed

Lines changed: 38 additions & 12 deletions

File tree

src/assembler.jl

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,7 @@ start_assemble(K::Union{AbstractSparseMatrixCSC, Symmetric{<:Any, <:AbstractSpar
245245

246246
function start_assemble(K::AbstractSparseMatrixCSC{T}, f::Vector = T[]; fillzero::Bool = true, maxcelldofs_hint::Int = 0) where {T}
247247
fillzero && (fillzero!(K); fillzero!(f))
248-
if size(K, 1) == size(K, 2)
249-
permutation = zeros(Int, maxcelldofs_hint)
250-
sorteddofs = zeros(Int, maxcelldofs_hint)
251-
return CSCAssembler(K, f, permutation, permutation, sorteddofs, sorteddofs)
252-
else
253-
return CSCAssembler(K, f, zeros(Int, maxcelldofs_hint), zeros(Int, maxcelldofs_hint), zeros(Int, maxcelldofs_hint), zeros(Int, maxcelldofs_hint))
254-
end
248+
return CSCAssembler(K, f, zeros(Int, maxcelldofs_hint), zeros(Int, maxcelldofs_hint), zeros(Int, maxcelldofs_hint), zeros(Int, maxcelldofs_hint))
255249
end
256250
function start_assemble(K::Symmetric{T, <:SparseMatrixCSC}, f::Vector = T[]; fillzero::Bool = true, maxcelldofs_hint::Int = 0) where {T}
257251
fillzero && (fillzero!(K); fillzero!(f))
@@ -322,7 +316,7 @@ end
322316
# a specific order, which might not be the sorted order. Hence we sort them.
323317
# Note that we are not allowed to mutate `dofs` in the process.
324318
sortedcoldofs, colpermutation = _sortdofs_for_assembly!(A.colpermutation, A.sortedcoldofs, coldofs)
325-
sortedrowdofs, rowpermutation = if A.sortedcoldofs !== A.sortedrowdofs
319+
sortedrowdofs, rowpermutation = if rowdofs !== coldofs
326320
_sortdofs_for_assembly!(A.rowpermutation, A.sortedrowdofs, rowdofs)
327321
else
328322
sortedcoldofs, colpermutation

test/test_assemble.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,20 @@ import LinearAlgebra: Symmetric
6868
fe = rand(length(rdofs))
6969
assemble!(assembler, rdofs, cdofs, Ke, fe)
7070
assemble!(assembler, rdofs, cdofs, Ke, fe)
71-
@test_throws ArgumentError assemble!(assembler, rdofs, Ke, fe)
71+
@test_throws ArgumentError assemble!(assembler, rdofs, Ke, fe) # Not in sparsity pattern
72+
@test all(K[rdofs, cdofs] .== 2Ke)
73+
@test all(f[rdofs] .== 2fe)
74+
75+
# CSCAssembler: Assemble rectangular part in quadratic matrix
76+
K = SparseMatrixCSC(6, 6, [K.colptr..., 7, 7, 7], K.rowval, K.nzval)
77+
assembler = start_assemble(K, f)
78+
rdofs = [1, 4, 6]
79+
cdofs = [1, 3]
80+
Ke = rand(length(rdofs), length(cdofs))
81+
fe = rand(length(rdofs))
82+
assemble!(assembler, rdofs, cdofs, Ke, fe)
83+
assemble!(assembler, rdofs, cdofs, Ke, fe)
84+
@test_throws ArgumentError assemble!(assembler, rdofs, Ke, fe) # Not in sparsity pattern
7285
@test all(K[rdofs, cdofs] .== 2Ke)
7386
@test all(f[rdofs] .== 2fe)
7487

test/test_assembler_extensions.jl

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,35 @@ using SparseArrays, LinearAlgebra
8585
@test K sparsecsr(I, J, V)
8686
@test f [4 / 3, 2.0, 1.0]
8787

88+
# CSRAssembler: assemble with different row and col dofs
8889
I = [1, 1, 4, 4, 6, 6]
8990
J = [1, 3, 1, 3, 1, 3]
9091
V = zeros(length(I))
9192
K = sparsecsr(I, J, V)
92-
assembler = start_assemble(K)
93+
f = zeros(6)
94+
assembler = start_assemble(K, f)
95+
rdofs = [1, 4, 6]
96+
cdofs = [1, 3]
97+
Ke = rand(length(rdofs), length(cdofs))
98+
fe = rand(length(rdofs))
99+
assemble!(assembler, rdofs, cdofs, Ke, fe)
100+
assemble!(assembler, rdofs, cdofs, Ke, fe)
101+
@test_throws ArgumentError assemble!(assembler, rdofs, Ke, fe) # Not in sparsity pattern
102+
@test all(K[rdofs, cdofs] .== 2Ke)
103+
@test all(f[rdofs] .== 2fe)
104+
105+
# CSRAssembler: Assemble rectangular part in quadratic matrix
106+
K = SparseMatrixCSR(6, 6, [K.colptr..., 7, 7, 7], K.rowval, K.nzval)
107+
assembler = start_assemble(K, f)
93108
rdofs = [1, 4, 6]
94109
cdofs = [1, 3]
95110
Ke = rand(length(rdofs), length(cdofs))
96-
assemble!(assembler, rdofs, cdofs, Ke)
97-
@test (K[rdofs, cdofs] .== Ke) |> all
111+
fe = rand(length(rdofs))
112+
assemble!(assembler, rdofs, cdofs, Ke, fe)
113+
assemble!(assembler, rdofs, cdofs, Ke, fe)
114+
@test_throws ArgumentError assemble!(assembler, rdofs, Ke, fe) # Not in sparsity pattern
115+
@test all(K[rdofs, cdofs] .== 2Ke)
116+
@test all(f[rdofs] .== 2fe)
98117

99118
# Check if coupling works
100119
grid = generate_grid(Quadrilateral, (2, 2))

0 commit comments

Comments
 (0)