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 <functional>
14 #include <petscmat.h>
15 #include <string>
16 
17 namespace dolfinx::la
18 {
19 class SparsityPattern;
20 class VectorSpaceBasis;
21 
24 Mat create_petsc_matrix(MPI_Comm comm, const SparsityPattern& sparsity_pattern);
25 
28 MatNullSpace create_petsc_nullspace(MPI_Comm comm,
29  const VectorSpaceBasis& nullspace);
30 
36 
37 class PETScMatrix : public PETScOperator
38 {
39 public:
42  static std::function<int(std::int32_t, const std::int32_t*, std::int32_t,
43  const std::int32_t*, const PetscScalar*)>
44  add_fn(Mat A);
45 
47  PETScMatrix(MPI_Comm comm, const SparsityPattern& sparsity_pattern);
48 
53  explicit PETScMatrix(Mat A, bool inc_ref_count = true);
54 
55  // Copy constructor (deleted)
56  PETScMatrix(const PETScMatrix& A) = delete;
57 
59  PETScMatrix(PETScMatrix&& A) = default;
60 
62  ~PETScMatrix() = default;
63 
65  PETScMatrix& operator=(const PETScMatrix& A) = delete;
66 
68  PETScMatrix& operator=(PETScMatrix&& A) = default;
69 
73  enum class AssemblyType : std::int32_t
74  {
75  FINAL,
76  FLUSH
77  };
78 
84  void apply(AssemblyType type);
85 
87  double norm(la::Norm norm_type) const;
88 
89  //--- Special PETSc Functions ---
90 
93  void set_options_prefix(std::string options_prefix);
94 
97  std::string get_options_prefix() const;
98 
100  void set_from_options();
101 
104  void set_nullspace(const la::VectorSpaceBasis& nullspace);
105 
108  void set_near_nullspace(const la::VectorSpaceBasis& nullspace);
109 };
110 } // namespace dolfinx::la
dolfinx::la::Norm
Norm
Norm types.
Definition: utils.h:13
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:187
dolfinx::la::create_petsc_matrix
Mat create_petsc_matrix(MPI_Comm comm, const SparsityPattern &sparsity_pattern)
Create a PETSc Mat. Caller is responsible for destroying the returned object.
Definition: PETScMatrix.cpp:24
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:228
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::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:247
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:267
dolfinx::la::PETScMatrix::add_fn
static std::function< int(std::int32_t, const std::int32_t *, std::int32_t, const std::int32_t *, const PetscScalar *)> add_fn(Mat A)
Return a function with an interface for adding values to the matrix A.
Definition: PETScMatrix.cpp:163
dolfinx::la::PETScMatrix::set_from_options
void set_from_options()
Call PETSc function MatSetFromOptions on the PETSc Mat object.
Definition: PETScMatrix.cpp:261
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:37
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:288
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::create_petsc_nullspace
MatNullSpace create_petsc_nullspace(MPI_Comm comm, const VectorSpaceBasis &nullspace)
Create PETSc MatNullSpace. Caller is responsible for destruction returned object.
Definition: PETScMatrix.cpp:133
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:253
dolfinx::la::PETScMatrix::AssemblyType
AssemblyType
Assembly type FINAL - corresponds to PETSc MAT_FINAL_ASSEMBLY FLUSH - corresponds to PETSc MAT_FLUSH_...
Definition: PETScMatrix.h:73
dolfinx::la::PETScMatrix::norm
double norm(la::Norm norm_type) const
Return norm of matrix.
Definition: PETScMatrix.cpp:199