deal.II version GIT relicensing-6917-gb466a1809f 2026-10-16 06:30: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 DEAL_II_DEPRECATED_EARLY
438 SparseMatrix(const MPI_Comm communicator,
439 const SparsityPatternType &sparsity_pattern,
440 const std::vector<size_type> &local_rows_per_process,
441 const std::vector<size_type> &local_columns_per_process,
442 const unsigned int this_process,
443 const bool preset_nonzero_locations = true);
444
455 operator=(const value_type d);
456
457
462 void
463 copy_from(const SparseMatrix &other);
464
484 template <typename SparsityPatternType>
485 DEAL_II_DEPRECATED_EARLY void
486 reinit(const MPI_Comm communicator,
487 const SparsityPatternType &sparsity_pattern,
488 const std::vector<size_type> &local_rows_per_process,
489 const std::vector<size_type> &local_columns_per_process,
490 const unsigned int this_process,
491 const bool preset_nonzero_locations = true);
492
499 template <typename SparsityPatternType>
500 void
501 reinit(const IndexSet &local_partitioning,
502 const SparsityPatternType &sparsity_pattern,
503 const MPI_Comm communicator);
504
511 template <typename SparsityPatternType>
512 void
513 reinit(const IndexSet &local_rows,
514 const IndexSet &local_columns,
515 const SparsityPatternType &sparsity_pattern,
516 const MPI_Comm communicator);
517
523 void
524 reinit(const SparseMatrix &other);
525
535 template <typename SparsityPatternType>
536 void
537 reinit(const IndexSet &local_rows,
538 const IndexSet &local_active_rows,
539 const IndexSet &local_columns,
540 const IndexSet &local_active_columns,
541 const SparsityPatternType &sparsity_pattern,
542 const MPI_Comm communicator);
543
552 int,
553 int,
554 << "The number of local rows " << arg1
555 << " must be larger than the total number of rows "
556 << arg2);
574 PetscScalar
575 matrix_norm_square(const Vector &v) const;
576
585 PetscScalar
586 matrix_scalar_product(const Vector &u, const Vector &v) const;
587
594
602
609 void
611 const SparseMatrix &B,
612 const MPI::Vector &V = MPI::Vector()) const;
613
621 void
623 const SparseMatrix &B,
624 const MPI::Vector &V = MPI::Vector()) const;
625
626 private:
630 template <typename SparsityPatternType>
631 DEAL_II_DEPRECATED_EARLY void
632 do_reinit(const MPI_Comm comm,
633 const SparsityPatternType &sparsity_pattern,
634 const std::vector<size_type> &local_rows_per_process,
635 const std::vector<size_type> &local_columns_per_process,
636 const unsigned int this_process,
637 const bool preset_nonzero_locations);
638
642 template <typename SparsityPatternType>
643 void
644 do_reinit(const MPI_Comm comm,
645 const IndexSet &local_rows,
646 const IndexSet &local_columns,
647 const SparsityPatternType &sparsity_pattern);
648
653 template <typename SparsityPatternType>
654 void
656 const IndexSet &local_rows,
657 const IndexSet &local_active_rows,
658 const IndexSet &local_columns,
659 const IndexSet &local_active_columns,
660 const SparsityPatternType &sparsity_pattern);
661
662 // To allow calling protected prepare_add() and prepare_set().
663 friend class BlockMatrixBase<SparseMatrix>;
664 };
665
666 } // namespace MPI
667} // namespace PETScWrappers
668
669#endif // DEAL_II_WITH_PETSC
670
672
673#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