Skip to content

Commit

Permalink
Use sparse! and spzeros! in more places (#1084)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre authored Sep 30, 2024
1 parent 053e13c commit 971c4dc
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 18 deletions.
6 changes: 2 additions & 4 deletions ext/FerriteMetis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ if VERSION >= v"1.10.0-DEV.90"

using Ferrite:
Ferrite, CellIterator, ConstraintHandler, DofHandler, DofOrder, celldofs, ndofs,
ndofs_per_cell
ndofs_per_cell, spzeros!!
using Metis.LibMetis: idx_t
using Metis: Metis
using SparseArrays: sparse

struct MetisOrder <: DofOrder.Ext{Metis}
coupling::Union{Matrix{Bool},Nothing}
Expand Down Expand Up @@ -75,8 +74,7 @@ function Ferrite.compute_renumber_permutation(
end
@assert length(I) == length(J) == idx
N = ndofs(dh)
# TODO: Use spzeros! in Julia 1.10.
S = sparse(I, J, zeros(Float32, length(I)), N, N)
S = spzeros!!(Float32, I, J, N, N)

# Add entries from affine constraints
if ch !== nothing
Expand Down
2 changes: 1 addition & 1 deletion src/Dofs/ConstraintHandler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ function create_constraint_matrix(ch::ConstraintHandler{dh,T}) where {dh,T}
end
g[ch.prescribed_dofs] .= ch.inhomogeneities

C = sparse(I, J, V, ndofs(ch.dh), length(ch.free_dofs))
C = sparse!!(I, J, V, ndofs(ch.dh), length(ch.free_dofs))

return C, g
end
Expand Down
3 changes: 1 addition & 2 deletions src/Ferrite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ using NearestNeighbors:
using OrderedCollections:
OrderedSet
using SparseArrays:
SparseArrays, SparseMatrixCSC, nonzeros, nzrange, rowvals, sparse,
AbstractSparseMatrixCSC
SparseArrays, SparseMatrixCSC, nonzeros, nzrange, rowvals, AbstractSparseMatrixCSC
using StaticArrays:
StaticArrays, MArray, MMatrix, SArray, SMatrix, SVector
using WriteVTK:
Expand Down
19 changes: 9 additions & 10 deletions src/arrayutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,14 @@ function fillzero!(A::Symmetric{T,<:SparseMatrixCSC}) where T
end

# Compat and convenience layer around SparseArrays.spzeros! (SparseArrays.jl#284, SparseArrays.jl#315)
function spzeros!!(::Type{Tv}, I::Vector{Ti}, J::Vector{Ti}, m::Int, n::Int) where {Tv, Ti <: Integer}
@assert length(I) == length(J)
@static if isdefined(SparseArrays, :spzeros!)
klasttouch = Vector{Ti}(undef, n)
csrrowptr = Vector{Ti}(undef, m + 1)
csrcolval = Vector{Ti}(undef, length(I))
S = SparseArrays.spzeros!(Tv, I, J, m, n, klasttouch, csrrowptr, csrcolval, I, J)
else
S = sparse(I, J, zeros(Tv, length(I)), m, n)
if VERSION >= v"1.10.0"
const sparse!! = SparseArrays.sparse!
const spzeros!! = SparseArrays.spzeros!
else
function sparse!!(I::AbstractVector, J::AbstractVector, V::AbstractVector, m::Integer, n::Integer)
return SparseArrays.sparse(I, J, V, m, n)
end
function spzeros!!(::Type{T}, I::AbstractVector, J::AbstractVector, m::Integer, n::Integer) where T
return sparse!!(I, J, zeros(T, length(I)), m, n)
end
return S
end
2 changes: 1 addition & 1 deletion src/assembler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function finish_assemble(a::COOAssembler)
# Create the matrix
nrows = a.nrows == -1 ? maximum(a.I) : a.nrows
ncols = a.ncols == -1 ? maximum(a.J) : a.ncols
K = sparse(a.I, a.J, a.V, nrows, ncols)
K = sparse!!(a.I, a.J, a.V, nrows, ncols)
# Finalize the vector
f = a.f
if !isempty(f)
Expand Down

0 comments on commit 971c4dc

Please sign in to comment.