Reference documentation for deal.II version GIT 4e92747666 2022-12-02 08:40:02+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\}}\)
petsc_sparse_matrix.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2004 - 2021 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_petsc_sparse_matrix_h
17 #define dealii_petsc_sparse_matrix_h
18 
19 
20 #include <deal.II/base/config.h>
21 
22 #ifdef DEAL_II_WITH_PETSC
23 
24 # include <deal.II/lac/exceptions.h>
27 
28 # include <petscis.h>
29 # include <petscistypes.h>
30 
31 # include <vector>
32 
34 // forward declaration
35 # ifndef DOXYGEN
36 template <typename MatrixType>
37 class BlockMatrixBase;
38 # endif
39 
40 namespace PETScWrappers
41 {
54  class SparseMatrix : public MatrixBase
55  {
56  public:
64  struct Traits
65  {
70  static const bool zero_addition_can_be_elided = true;
71  };
72 
76  SparseMatrix();
77 
92  SparseMatrix(const size_type m,
93  const size_type n,
94  const size_type n_nonzero_per_row,
95  const bool is_symmetric = false);
96 
113  SparseMatrix(const size_type m,
114  const size_type n,
115  const std::vector<size_type> &row_lengths,
116  const bool is_symmetric = false);
117 
135  template <typename SparsityPatternType>
136  explicit SparseMatrix(const SparsityPatternType &sparsity_pattern,
137  const bool preset_nonzero_locations = true);
138 
148  SparseMatrix &
149  operator=(const double d);
150 
154  SparseMatrix(const SparseMatrix &) = delete;
155 
159  SparseMatrix &
160  operator=(const SparseMatrix &) = delete;
161 
167  void
168  reinit(const size_type m,
169  const size_type n,
170  const size_type n_nonzero_per_row,
171  const bool is_symmetric = false);
172 
178  void
179  reinit(const size_type m,
180  const size_type n,
181  const std::vector<size_type> &row_lengths,
182  const bool is_symmetric = false);
183 
209  template <typename SparsityPatternType>
210  void
211  reinit(const SparsityPatternType &sparsity_pattern,
212  const bool preset_nonzero_locations = true);
213 
219  virtual const MPI_Comm &
220  get_mpi_communicator() const override;
221 
225  size_t
226  m() const;
227 
231  size_t
232  n() const;
233 
240  void
241  mmult(SparseMatrix & C,
242  const SparseMatrix &B,
243  const MPI::Vector & V = MPI::Vector()) const;
244 
252  void
254  const SparseMatrix &B,
255  const MPI::Vector & V = MPI::Vector()) const;
256 
257  private:
263  void
264  do_reinit(const size_type m,
265  const size_type n,
266  const size_type n_nonzero_per_row,
267  const bool is_symmetric = false);
268 
272  void
273  do_reinit(const size_type m,
274  const size_type n,
275  const std::vector<size_type> &row_lengths,
276  const bool is_symmetric = false);
277 
281  template <typename SparsityPatternType>
282  void
283  do_reinit(const SparsityPatternType &sparsity_pattern,
284  const bool preset_nonzero_locations);
285 
286  // To allow calling protected prepare_add() and prepare_set().
287  friend class BlockMatrixBase<SparseMatrix>;
288  };
289 
290  namespace MPI
291  {
368  class SparseMatrix : public MatrixBase
369  {
370  public:
375 
383  struct Traits
384  {
392  static const bool zero_addition_can_be_elided = false;
393  };
394 
398  SparseMatrix();
399 
403  ~SparseMatrix() override;
404 
427  template <typename SparsityPatternType>
428  SparseMatrix(const MPI_Comm & communicator,
429  const SparsityPatternType & sparsity_pattern,
430  const std::vector<size_type> &local_rows_per_process,
431  const std::vector<size_type> &local_columns_per_process,
432  const unsigned int this_process,
433  const bool preset_nonzero_locations = true);
434 
444  SparseMatrix &
445  operator=(const value_type d);
446 
447 
452  void
453  copy_from(const SparseMatrix &other);
454 
474  template <typename SparsityPatternType>
475  void
476  reinit(const MPI_Comm & communicator,
477  const SparsityPatternType & sparsity_pattern,
478  const std::vector<size_type> &local_rows_per_process,
479  const std::vector<size_type> &local_columns_per_process,
480  const unsigned int this_process,
481  const bool preset_nonzero_locations = true);
482 
489  template <typename SparsityPatternType>
490  void
491  reinit(const IndexSet & local_rows,
492  const IndexSet & local_columns,
493  const SparsityPatternType &sparsity_pattern,
494  const MPI_Comm & communicator);
495 
501  void
502  reinit(const SparseMatrix &other);
503 
513  template <typename SparsityPatternType>
514  void
515  reinit(const IndexSet & local_rows,
516  const IndexSet & local_active_rows,
517  const IndexSet & local_columns,
518  const IndexSet & local_active_columns,
519  const SparsityPatternType &sparsity_pattern,
520  const MPI_Comm & communicator);
521 
526  virtual const MPI_Comm &
527  get_mpi_communicator() const override;
528 
537  int,
538  int,
539  << "The number of local rows " << arg1
540  << " must be larger than the total number of rows "
541  << arg2);
559  PetscScalar
560  matrix_norm_square(const Vector &v) const;
561 
570  PetscScalar
571  matrix_scalar_product(const Vector &u, const Vector &v) const;
572 
577  IndexSet
579 
585  IndexSet
587 
594  void
595  mmult(SparseMatrix & C,
596  const SparseMatrix &B,
597  const MPI::Vector & V = MPI::Vector()) const;
598 
606  void
608  const SparseMatrix &B,
609  const MPI::Vector & V = MPI::Vector()) const;
610 
611  private:
615  MPI_Comm communicator;
616 
620  template <typename SparsityPatternType>
621  void
622  do_reinit(const SparsityPatternType & sparsity_pattern,
623  const std::vector<size_type> &local_rows_per_process,
624  const std::vector<size_type> &local_columns_per_process,
625  const unsigned int this_process,
626  const bool preset_nonzero_locations);
627 
631  template <typename SparsityPatternType>
632  void
633  do_reinit(const IndexSet & local_rows,
634  const IndexSet & local_columns,
635  const SparsityPatternType &sparsity_pattern);
636 
641  template <typename SparsityPatternType>
642  void
643  do_reinit(const IndexSet & local_rows,
644  const IndexSet & local_active_rows,
645  const IndexSet & local_columns,
646  const IndexSet & local_active_columns,
647  const SparsityPatternType &sparsity_pattern);
648 
649  // To allow calling protected prepare_add() and prepare_set().
650  friend class BlockMatrixBase<SparseMatrix>;
651  };
652 
653 
654 
655  // -------- template and inline functions ----------
656 
657  inline const MPI_Comm &
659  {
660  return communicator;
661  }
662  } // namespace MPI
663 } // namespace PETScWrappers
664 
666 
667 #endif // DEAL_II_WITH_PETSC
668 
669 #endif
SparseMatrix & operator=(const value_type d)
void copy_from(const SparseMatrix &other)
void mmult(SparseMatrix &C, const SparseMatrix &B, const MPI::Vector &V=MPI::Vector()) const
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)
PetscScalar matrix_scalar_product(const Vector &u, const Vector &v) const
virtual const MPI_Comm & get_mpi_communicator() const override
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 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)
types::global_dof_index size_type
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)
virtual const MPI_Comm & get_mpi_communicator() const override
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:458
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:459
#define DeclException2(Exception2, type1, type2, outsequence)
Definition: exceptions.h:532
static ::ExceptionBase & ExcLocalRowsTooLarge(int arg1, int arg2)
static const char V
SymmetricTensor< 2, dim, Number > C(const Tensor< 2, dim, Number > &F)
SymmetricTensor< 2, dim, Number > d(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
unsigned int global_dof_index
Definition: types.h:81