From 7eb79d5f953d1bbb01884204b623efd349e1df5b Mon Sep 17 00:00:00 2001 From: Marc Fehling Date: Thu, 3 Mar 2022 22:30:28 -0700 Subject: [PATCH] Added `n_nonzero_elements` to PETSc `BlockSparseMatrix`. --- include/deal.II/lac/petsc_block_sparse_matrix.h | 8 ++++++++ source/lac/petsc_parallel_block_sparse_matrix.cc | 11 +++++++++++ 2 files changed, 19 insertions(+) diff --git a/include/deal.II/lac/petsc_block_sparse_matrix.h b/include/deal.II/lac/petsc_block_sparse_matrix.h index d7ebe0819c..60ac2c3e93 100644 --- a/include/deal.II/lac/petsc_block_sparse_matrix.h +++ b/include/deal.II/lac/petsc_block_sparse_matrix.h @@ -250,6 +250,14 @@ namespace PETScWrappers std::vector locally_owned_range_indices() const; + /** + * Return the number of nonzero elements of this matrix. Actually, it + * returns the number of entries in the sparsity pattern; if any of the + * entries should happen to be zero, it is counted anyway. + */ + size_type + n_nonzero_elements() const; + /** * Return a reference to the MPI communicator object in use with this * matrix. diff --git a/source/lac/petsc_parallel_block_sparse_matrix.cc b/source/lac/petsc_parallel_block_sparse_matrix.cc index 598f3a57ff..0d0a553838 100644 --- a/source/lac/petsc_parallel_block_sparse_matrix.cc +++ b/source/lac/petsc_parallel_block_sparse_matrix.cc @@ -133,6 +133,17 @@ namespace PETScWrappers return index_sets; } + BlockSparseMatrix::size_type + BlockSparseMatrix::n_nonzero_elements() const + { + size_type n_nonzero = 0; + for (size_type rows = 0; rows < this->n_block_rows(); ++rows) + for (size_type cols = 0; cols < this->n_block_cols(); ++cols) + n_nonzero += this->block(rows, cols).n_nonzero_elements(); + + return n_nonzero; + } + const MPI_Comm & BlockSparseMatrix::get_mpi_communicator() const { -- 2.39.5