Skip to content

Commit f5c4a76

Browse files
committed
Add tests to check Symmetric diverges to std SP and that Int32 type works
1 parent 5c52092 commit f5c4a76

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

src/Dofs/sparsity_pattern.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,12 @@ arguments `args` and keyword arguments `kwargs`.
413413
copy(K)`) instead.
414414
"""
415415
function allocate_matrix(::Type{MatrixType}, dh::DofHandler, args...; kwargs...) where {MatrixType}
416-
_can_use_fastsp(MatrixType, args...; kwargs...) && return allocate_matrix(MatrixType, FastSparsityPattern(dh, args...; kwargs...))
416+
_get_Ti(::Type{<:AbstractMatrix}) = Int
417+
_get_Ti(::Type{<:AbstractSparseMatrix{<:Any, Ti}}) where {Ti} = Ti
418+
if _can_use_fastsp(MatrixType, args...; kwargs...)
419+
fsp = FastSparsityPattern(_get_Ti(MatrixType), dh, args...; kwargs...)
420+
return allocate_matrix(MatrixType, fsp)
421+
end
417422
sp = init_sparsity_pattern(dh)
418423
add_sparsity_entries!(sp, dh, args...; kwargs...)
419424
return allocate_matrix(MatrixType, sp)

test/test_sparsity_patterns.jl

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Ferrite, Test, SparseArrays, Random
22
using SparseMatricesCSR: SparseMatrixCSR
3+
using LinearAlgebra
34

45
# Minimal implementation of a custom sparsity pattern
56
struct TestPattern <: Ferrite.AbstractSparsityPattern
@@ -334,15 +335,18 @@ end
334335
end
335336

336337
@testset "FastSparsityPattern" begin
337-
# Internal fast-path for supported cases
338-
for CT in (Line, Quadrilateral, Tetrahedron)
338+
function fsp_test_create_dh(CT)
339339
RS = getrefshape(CT)
340340
dim = Ferrite.getrefdim(RS)
341341
grid = generate_grid(CT, ntuple(_ -> 5, dim))
342342
dh = DofHandler(grid)
343343
add!(dh, :a, Lagrange{RS, 1}())
344344
add!(dh, :b, Lagrange{RS, 2}()^dim)
345-
close!(dh)
345+
return close!(dh)
346+
end
347+
# Internal fast-path for supported cases
348+
for CT in (Line, Quadrilateral, Tetrahedron)
349+
dh = fsp_test_create_dh(CT)
346350
sp = add_sparsity_entries!(init_sparsity_pattern(dh), dh)
347351
fsp = Ferrite.FastSparsityPattern(dh)
348352
K1 = allocate_matrix(sp)
@@ -357,4 +361,28 @@ end
357361
K2_csr.nzval .= 1:length(K2_csr.nzval)
358362
@test K1_csr == K2_csr
359363
end
364+
# Test different number types (Int32, Float32)
365+
for Tv in (Float32, Float64)
366+
for Ti in (Int32, Int64)
367+
dh = fsp_test_create_dh(Quadrilateral)
368+
MatrixType = SparseMatrixCSC{Float64, Ti}
369+
K1 = allocate_matrix(MatrixType, dh)
370+
@test isa(K1, MatrixType)
371+
compare_matrices(K1, allocate_matrix(dh))
372+
373+
MatrixTypeCSR = SparseMatrixCSR{1, Tv, Ti}
374+
K1_csr = allocate_matrix(MatrixTypeCSR, dh)
375+
@test isa(K1_csr, MatrixTypeCSR)
376+
end
377+
end
378+
379+
# Test logic for Symmetric matrices works (will use SparsityPattern)
380+
dh = fsp_test_create_dh(Triangle)
381+
for MatrixType in (
382+
Symmetric{Float64, SparseMatrixCSC{Float64, Int}},
383+
# Symmetric{Float64, SparseMatrixCSR{1, Float64, Int}} # Does not work, see #1311
384+
)
385+
K1 = allocate_matrix(MatrixType, dh)
386+
@test isa(K1, MatrixType)
387+
end
360388
end

0 commit comments

Comments
 (0)