Skip to content

Commit

Permalink
Merge pull request #14592 from luca-heltai/petsc-block-matrix-to-nestmat
Browse files Browse the repository at this point in the history
PETScWrappers:BlockSparseMatrix: create PETSc MATNEST
  • Loading branch information
drwells committed Dec 21, 2022
2 parents 9d8b573 + 35c5658 commit f161e5a
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 1 deletion.
3 changes: 3 additions & 0 deletions doc/news/changes/minor/20221220StefanoZampini-b
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
New: PETScWrappers:BlockSparseMatrix now is also a PETSc MATNEST type.
<br>
(Stefano Zampini, 2022/12/20)
22 changes: 21 additions & 1 deletion include/deal.II/lac/petsc_block_sparse_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ namespace PETScWrappers
/**
* Destructor.
*/
~BlockSparseMatrix() override = default;
~BlockSparseMatrix() override;

/**
* Pseudo copy operator only copying empty objects. The sizes of the
Expand Down Expand Up @@ -271,6 +271,26 @@ namespace PETScWrappers
* protected.
*/
using BlockMatrixBase<SparseMatrix>::clear;

/**
* Conversion operator to gain access to the underlying PETSc type. If you
* do this, you cut this class off some information it may need, so this
* conversion operator should only be used if you know what you do. In
* particular, it should only be used for read-only operations into the
* matrix.
*/
operator const Mat &() const;

/**
* Return a reference to the underlying PETSc type. It can be used to
* modify the underlying data, so use it only when you know what you
* are doing.
*/
Mat &
petsc_matrix();

private:
Mat petsc_nest_matrix = nullptr;
};


Expand Down
35 changes: 35 additions & 0 deletions source/lac/petsc_parallel_block_sparse_matrix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// ---------------------------------------------------------------------

#include <deal.II/lac/petsc_block_sparse_matrix.h>
#include <deal.II/lac/petsc_compatibility.h>

#ifdef DEAL_II_WITH_PETSC

Expand All @@ -31,6 +32,11 @@ namespace PETScWrappers
return *this;
}

BlockSparseMatrix::~BlockSparseMatrix()
{
PetscErrorCode ierr = destroy_matrix(petsc_nest_matrix);
AssertNothrow(ierr == 0, ExcPETScError(ierr));
}

# ifndef DOXYGEN
void
Expand Down Expand Up @@ -111,6 +117,24 @@ namespace PETScWrappers
BlockSparseMatrix::collect_sizes()
{
BaseClass::collect_sizes();

auto m = this->n_block_cols();
auto n = this->n_block_cols();

PetscErrorCode ierr = destroy_matrix(petsc_nest_matrix);
AssertThrow(ierr == 0, ExcPETScError(ierr));
std::vector<Mat> psub_objects(m * n);
for (unsigned int r = 0; r < m; r++)
for (unsigned int c = 0; c < n; c++)
psub_objects[r * n + c] = this->sub_objects[r][c]->petsc_matrix();
ierr = MatCreateNest(get_mpi_communicator(),
m,
NULL,
n,
NULL,
psub_objects.data(),
&petsc_nest_matrix);
AssertThrow(ierr == 0, ExcPETScError(ierr));
}

std::vector<IndexSet>
Expand Down Expand Up @@ -152,6 +176,17 @@ namespace PETScWrappers
return block(0, 0).get_mpi_communicator();
}

BlockSparseMatrix::operator const Mat &() const
{
return petsc_nest_matrix;
}

Mat &
BlockSparseMatrix::petsc_matrix()
{
return petsc_nest_matrix;
}

} // namespace MPI
} // namespace PETScWrappers

Expand Down
4 changes: 4 additions & 0 deletions tests/petsc/block_matrices_01.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ test()
pbsm.reinit(rows, cols, bdsp, MPI_COMM_WORLD);
deallog << "nonzeros BlockSparseMatrix: " << pbsm.n_nonzero_elements()
<< std::endl;

// Extract the PETSc MATNEST and use print from PETScWrappers::MatrixBase
PETScWrappers::MatrixBase tmp(pbsm.petsc_matrix());
tmp.print(deallog.get_file_stream());
}


Expand Down
9 changes: 9 additions & 0 deletions tests/petsc/block_matrices_01.mpirun=1.output
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@

DEAL::nonzeros BlockSparsityPattern: 9
DEAL::nonzeros BlockSparseMatrix: 9
(0,0) 0.00000
(1,0) 0.00000
(1,1) 0.00000
(2,2) 0.00000
(3,2) 0.00000
(3,3) 0.00000
(4,2) 0.00000
(4,3) 0.00000
(4,4) 0.00000

0 comments on commit f161e5a

Please sign in to comment.