deal.II version GIT relicensing-6809-ge913b9bb34 2026-09-25 17:20:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
petsc_sparse_matrix.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2004 - 2026 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13#ifndef dealii_petsc_sparse_matrix_h
14#define dealii_petsc_sparse_matrix_h
15
16
17#include <deal.II/base/config.h>
18
19#ifdef DEAL_II_WITH_PETSC
20
24
25# include <petscis.h>
26# include <petscistypes.h>
27
28# include <cstddef>
29# include <vector>
30
31#endif // DEAL_II_WITH_PETSC
32
34
35#ifdef DEAL_II_WITH_PETSC
36// forward declaration
37# ifndef DOXYGEN
38template <typename MatrixType>
39class BlockMatrixBase;
40# endif
41
42namespace PETScWrappers
43{
56 class SparseMatrix : public MatrixBase
57 {
58 public:
66 struct Traits
67 {
72 static const bool zero_addition_can_be_elided = true;
73 };
74
79
86 explicit SparseMatrix(const Mat &);
87
103 const size_type n,
104 const size_type n_nonzero_per_row,
105 const bool is_symmetric = false);
106
124 const size_type n,
125 const std::vector<size_type> &row_lengths,
126 const bool is_symmetric = false);
127
145 template <typename SparsityPatternType>
146 explicit SparseMatrix(const SparsityPatternType &sparsity_pattern,
147 const bool preset_nonzero_locations = true);
148
159 operator=(const double d);
160
164 SparseMatrix(const SparseMatrix &) = delete;
165
170 operator=(const SparseMatrix &) = delete;
171
177 void
178 reinit(const size_type m,
179 const size_type n,
180 const size_type n_nonzero_per_row,
181 const bool is_symmetric = false);
182
188 void
189 reinit(const size_type m,
190 const size_type n,
191 const std::vector<size_type> &row_lengths,
192 const bool is_symmetric = false);
193
219 template <typename SparsityPatternType>
220 void
221 reinit(const SparsityPatternType &sparsity_pattern,
222 const bool preset_nonzero_locations = true);
223
227 std::size_t
228 m() const;
229
233 std::size_t
234 n() const;
235
242 void
244 const SparseMatrix &B,
245 const MPI::Vector &V = MPI::Vector()) const;
246
254 void
256 const SparseMatrix &B,
257 const MPI::Vector &V = MPI::Vector()) const;
258
259 private:
264 void
265 do_reinit(const size_type m,
266 const size_type n,
267 const size_type n_nonzero_per_row,
268 const bool is_symmetric = false);
269
273 void
274 do_reinit(const size_type m,
275 const size_type n,
276 const std::vector<size_type> &row_lengths,
277 const bool is_symmetric = false);
278
282 template <typename SparsityPatternType>
283 void
284 do_reinit(const SparsityPatternType &sparsity_pattern,
285 const bool preset_nonzero_locations);
286
287 // To allow calling protected prepare_add() and prepare_set().
288 friend class BlockMatrixBase<SparseMatrix>;
289 };
290
291 namespace MPI
292 {
370 {
371 public:
376
384 struct Traits
385 {
393 static const bool zero_addition_can_be_elided = false;
394 };
395
399 SparseMatrix();
400
407 explicit SparseMatrix(const Mat &);
408
412 ~SparseMatrix() override;
413
436 template <typename SparsityPatternType>
437 SparseMatrix(const MPI_Comm communicator,
438 const SparsityPatternType &sparsity_pattern,
439 const std::vector<size_type> &local_rows_per_process,
440 const std::vector<size_type> &local_columns_per_process,
441 const unsigned int this_process,
442 const bool preset_nonzero_locations = true);
443
454 operator=(const value_type d);
455
456
461 void
462 copy_from(const SparseMatrix &other);
463
483 template <typename SparsityPatternType>
484 void
485 reinit(const MPI_Comm communicator,
486 const SparsityPatternType &sparsity_pattern,
487 const std::vector<size_type> &local_rows_per_process,
488 const std::vector<size_type> &local_columns_per_process,
489 const unsigned int this_process,
490 const bool preset_nonzero_locations = true);
491
498 template <typename SparsityPatternType>
499 void
500 reinit(const IndexSet &local_partitioning,
501 const SparsityPatternType &sparsity_pattern,
502 const MPI_Comm communicator);
503
510 template <typename SparsityPatternType>
511 void
512 reinit(const IndexSet &local_rows,
513 const IndexSet &local_columns,
514 const SparsityPatternType &sparsity_pattern,
515 const MPI_Comm communicator);
516
522 void
523 reinit(const SparseMatrix &other);
524
534 template <typename SparsityPatternType>
535 void
536 reinit(const IndexSet &local_rows,
537 const IndexSet &local_active_rows,
538 const IndexSet &local_columns,
539 const IndexSet &local_active_columns,
540 const SparsityPatternType &sparsity_pattern,
541 const MPI_Comm communicator);
542
551 int,
552 int,
553 << "The number of local rows " << arg1
554 << " must be larger than the total number of rows "
555 << arg2);
573 PetscScalar
574 matrix_norm_square(const Vector &v) const;
575
584 PetscScalar
585 matrix_scalar_product(const Vector &u, const Vector &v) const;
586
593
601
608 void
610 const SparseMatrix &B,
611 const MPI::Vector &V = MPI::Vector()) const;
612
620 void
622 const SparseMatrix &B,
623 const MPI::Vector &V = MPI::Vector()) const;
624
625 private:
629 template <typename SparsityPatternType>
630 void
631 do_reinit(const MPI_Comm comm,
632 const SparsityPatternType &sparsity_pattern,
633 const std::vector<size_type> &local_rows_per_process,
634 const std::vector<size_type> &local_columns_per_process,
635 const unsigned int this_process,
636 const bool preset_nonzero_locations);
637
641 template <typename SparsityPatternType>
642 void
643 do_reinit(const MPI_Comm comm,
644 const IndexSet &local_rows,
645 const IndexSet &local_columns,
646 const SparsityPatternType &sparsity_pattern);
647
652 template <typename SparsityPatternType>
653 void
655 const IndexSet &local_rows,
656 const IndexSet &local_active_rows,
657 const IndexSet &local_columns,
658 const IndexSet &local_active_columns,
659 const SparsityPatternType &sparsity_pattern);
660
661 // To allow calling protected prepare_add() and prepare_set().
662 friend class BlockMatrixBase<SparseMatrix>;
663 };
664
665 } // namespace MPI
666} // namespace PETScWrappers
667
668#endif // DEAL_II_WITH_PETSC
669
671
672#endif
SparseMatrix & operator=(const value_type d)
void copy_from(const SparseMatrix &other)
void reinit(const MPI_Comm communicator, const SparsityPatternType &sparsity_pattern, const std::vector< size_type > &local_rows_per_process, const std::vector< size_type > &local_columns_per_process, const unsigned int this_process, const bool preset_nonzero_locations=true)
void mmult(SparseMatrix &C, const SparseMatrix &B, const MPI::Vector &V=MPI::Vector()) const
PetscScalar matrix_scalar_product(const Vector &u, const Vector &v) const
void Tmmult(SparseMatrix &C, const SparseMatrix &B, const MPI::Vector &V=MPI::Vector()) const
PetscScalar matrix_norm_square(const Vector &v) const
void do_reinit(const MPI_Comm comm, const SparsityPatternType &sparsity_pattern, const std::vector< size_type > &local_rows_per_process, const std::vector< size_type > &local_columns_per_process, const unsigned int this_process, const bool preset_nonzero_locations)
PetscBool is_symmetric(const double tolerance=1.e-12)
void mmult(SparseMatrix &C, const SparseMatrix &B, const MPI::Vector &V=MPI::Vector()) const
void do_reinit(const size_type m, const size_type n, const size_type n_nonzero_per_row, const bool is_symmetric=false)
void Tmmult(SparseMatrix &C, const SparseMatrix &B, const MPI::Vector &V=MPI::Vector()) const
void reinit(const size_type m, const size_type n, const size_type n_nonzero_per_row, const bool is_symmetric=false)
SparseMatrix(const SparseMatrix &)=delete
SparseMatrix & operator=(const SparseMatrix &)=delete
SparseMatrix & operator=(const double d)
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
static ::ExceptionBase & ExcLocalRowsTooLarge(int arg1, int arg2)
#define DeclException2(Exception2, type1, type2, outsequence)
const MPI_Comm comm
Definition mpi.cc:912
unsigned int global_dof_index
Definition types.h:92