DOLFIN-X
DOLFIN-X C++ interface
PETScMatrix.h
1 // Copyright (C) 2004-2018 Johan Hoffman, Johan Jansson, Anders Logg and Garth
2 // N. Wells
3 //
4 // This file is part of DOLFINX (https://www.fenicsproject.org)
5 //
6 // SPDX-License-Identifier: LGPL-3.0-or-later
7 
8 #pragma once
9 
10 #include "PETScOperator.h"
11 #include "utils.h"
12 #include <array>
13 #include <petscmat.h>
14 #include <string>
15 
16 namespace dolfinx::la
17 {
18 class SparsityPattern;
19 class VectorSpaceBasis;
20 
26 
27 class PETScMatrix : public PETScOperator
28 {
29 public:
31  PETScMatrix(MPI_Comm comm, const SparsityPattern& sparsity_pattern);
32 
37  explicit PETScMatrix(Mat A, bool inc_ref_count = true);
38 
39  // Copy constructor (deleted)
40  PETScMatrix(const PETScMatrix& A) = delete;
41 
43  PETScMatrix(PETScMatrix&& A) = default;
44 
46  ~PETScMatrix() = default;
47 
49  PETScMatrix& operator=(const PETScMatrix& A) = delete;
50 
52  PETScMatrix& operator=(PETScMatrix&& A) = default;
53 
57  enum class AssemblyType : std::int32_t
58  {
59  FINAL,
60  FLUSH
61  };
62 
68  void apply(AssemblyType type);
69 
71  void set(const PetscScalar* block, std::size_t m, const PetscInt* rows,
72  std::size_t n, const PetscInt* cols);
73 
75  void add_local(const PetscScalar* block, std::size_t m, const PetscInt* rows,
76  std::size_t n, const PetscInt* cols);
77 
79  double norm(la::Norm norm_type) const;
80 
81  //--- Special PETSc Functions ---
82 
85  void set_options_prefix(std::string options_prefix);
86 
89  std::string get_options_prefix() const;
90 
92  void set_from_options();
93 
96  void set_nullspace(const la::VectorSpaceBasis& nullspace);
97 
100  void set_near_nullspace(const la::VectorSpaceBasis& nullspace);
101 };
102 } // namespace dolfinx::la
dolfinx::la::Norm
Norm
Norm types.
Definition: utils.h:34
dolfinx::la::PETScMatrix::PETScMatrix
PETScMatrix(MPI_Comm comm, const SparsityPattern &sparsity_pattern)
Create holder of a PETSc Mat object from a sparsity pattern.
Definition: PETScMatrix.cpp:23
dolfinx::la
Linear algebra interface.
Definition: DiscreteOperators.h:19
dolfinx::la::PETScMatrix::apply
void apply(AssemblyType type)
Finalize assembly of tensor. The following values are recognized for the mode parameter:
Definition: PETScMatrix.cpp:85
dolfinx::la::SparsityPattern
This class provides a sparsity pattern data structure that can be used to initialize sparse matrices.
Definition: SparsityPattern.h:36
dolfinx::la::PETScMatrix::add_local
void add_local(const PetscScalar *block, std::size_t m, const PetscInt *rows, std::size_t n, const PetscInt *cols)
Add block of values using local indices.
Definition: PETScMatrix.cpp:45
dolfinx::la::PETScMatrix::set_options_prefix
void set_options_prefix(std::string options_prefix)
Sets the prefix used by PETSc when searching the options database.
Definition: PETScMatrix.cpp:104
dolfinx::la::PETScMatrix::operator=
PETScMatrix & operator=(const PETScMatrix &A)=delete
Assignment operator (deleted)
dolfinx::la::PETScMatrix::~PETScMatrix
~PETScMatrix()=default
Destructor.
dolfinx::la::PETScMatrix::set_nullspace
void set_nullspace(const la::VectorSpaceBasis &nullspace)
Attach nullspace to matrix (typically used by Krylov solvers when solving singular systems)
Definition: PETScMatrix.cpp:124
dolfinx::la::PETScMatrix::set_from_options
void set_from_options()
Call PETSc function MatSetFromOptions on the PETSc Mat object.
Definition: PETScMatrix.cpp:118
dolfinx::la::PETScMatrix::set
void set(const PetscScalar *block, std::size_t m, const PetscInt *rows, std::size_t n, const PetscInt *cols)
Set block of values using global indices.
Definition: PETScMatrix.cpp:35
dolfinx::la::PETScMatrix
It is a simple wrapper for a PETSc matrix pointer (Mat). Its main purpose is to assist memory managem...
Definition: PETScMatrix.h:27
dolfinx::la::PETScMatrix::set_near_nullspace
void set_near_nullspace(const la::VectorSpaceBasis &nullspace)
Attach 'near' nullspace to matrix (used by preconditioners, such as smoothed aggregation algerbraic m...
Definition: PETScMatrix.cpp:145
dolfinx::la::VectorSpaceBasis
This class defines a basis for vector spaces, typically used for expressing nullspaces of singular op...
Definition: VectorSpaceBasis.h:21
dolfinx::la::PETScOperator
This class is a base class for matrices that can be used in PETScKrylovSolver.
Definition: PETScOperator.h:22
dolfinx::la::PETScMatrix::get_options_prefix
std::string get_options_prefix() const
Returns the prefix used by PETSc when searching the options database.
Definition: PETScMatrix.cpp:110
dolfinx::la::PETScMatrix::AssemblyType
AssemblyType
Assembly type FINAL - corresponds to PETSc MAT_FINAL_ASSEMBLY FLUSH - corresponds to PETSc MAT_FLUSH_...
Definition: PETScMatrix.h:57
dolfinx::la::PETScMatrix::norm
double norm(la::Norm norm_type) const
Return norm of matrix.
Definition: PETScMatrix.cpp:56