Note: this is documentation for an old release. View the latest documentation at docs.fenicsproject.org/v0.1.0/v0.9.0/cpp
DOLFINx  0.1.0
DOLFINx 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 <functional>
13 #include <petscmat.h>
14 #include <string>
15 
16 namespace dolfinx::la
17 {
18 class SparsityPattern;
19 class VectorSpaceBasis;
20 
23 Mat create_petsc_matrix(MPI_Comm comm, const SparsityPattern& sparsity_pattern,
24  const std::string& type = std::string());
25 
28 MatNullSpace create_petsc_nullspace(MPI_Comm comm,
29  const VectorSpaceBasis& nullspace);
30 
36 
37 class PETScMatrix : public PETScOperator
38 {
39 public:
44  static std::function<int(std::int32_t, const std::int32_t*, std::int32_t,
45  const std::int32_t*, const PetscScalar*)>
46  set_fn(Mat A, InsertMode mode);
47 
53  static std::function<int(std::int32_t, const std::int32_t*, std::int32_t,
54  const std::int32_t*, const PetscScalar*)>
55  set_block_fn(Mat A, InsertMode mode);
56 
65  static std::function<int(std::int32_t, const std::int32_t*, std::int32_t,
66  const std::int32_t*, const PetscScalar*)>
67  set_block_expand_fn(Mat A, int bs0, int bs1, InsertMode mode);
68 
70  PETScMatrix(MPI_Comm comm, const SparsityPattern& sparsity_pattern,
71  const std::string& type = std::string());
72 
77  PETScMatrix(Mat A, bool inc_ref_count);
78 
79  // Copy constructor (deleted)
80  PETScMatrix(const PETScMatrix& A) = delete;
81 
83  PETScMatrix(PETScMatrix&& A) = default;
84 
86  ~PETScMatrix() = default;
87 
89  PETScMatrix& operator=(const PETScMatrix& A) = delete;
90 
92  PETScMatrix& operator=(PETScMatrix&& A) = default;
93 
97  enum class AssemblyType : std::int32_t
98  {
99  FINAL,
100  FLUSH
101  };
102 
108  void apply(AssemblyType type);
109 
111  double norm(la::Norm norm_type) const;
112 
113  //--- Special PETSc Functions ---
114 
117  void set_options_prefix(std::string options_prefix);
118 
121  std::string get_options_prefix() const;
122 
124  void set_from_options();
125 
128  void set_nullspace(const la::VectorSpaceBasis& nullspace);
129 
132  void set_near_nullspace(const la::VectorSpaceBasis& nullspace);
133 };
134 } // namespace dolfinx::la
dolfinx::la::Norm
Norm
Norm types.
Definition: utils.h:15
dolfinx::la::PETScMatrix::set_fn
static std::function< int(std::int32_t, const std::int32_t *, std::int32_t, const std::int32_t *, const PetscScalar *)> set_fn(Mat A, InsertMode mode)
Return a function with an interface for adding or inserting values into the matrix A (calls MatSetVal...
Definition: PETScMatrix.cpp:189
dolfinx::la::PETScMatrix::PETScMatrix
PETScMatrix(MPI_Comm comm, const SparsityPattern &sparsity_pattern, const std::string &type=std::string())
Create holder for a PETSc Mat object from a sparsity pattern.
Definition: PETScMatrix.cpp:271
dolfinx::la
Linear algebra interface.
Definition: sparsitybuild.h:14
dolfinx::la::PETScMatrix::set_block_fn
static std::function< int(std::int32_t, const std::int32_t *, std::int32_t, const std::int32_t *, const PetscScalar *)> set_block_fn(Mat A, InsertMode mode)
Return a function with an interface for adding or inserting values into the matrix A using blocked in...
Definition: PETScMatrix.cpp:215
dolfinx::la::PETScMatrix::apply
void apply(AssemblyType type)
Finalize assembly of tensor. The following values are recognized for the mode parameter:
Definition: PETScMatrix.cpp:313
dolfinx::la::SparsityPattern
This class provides a sparsity pattern data structure that can be used to initialize sparse matrices.
Definition: SparsityPattern.h:35
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:332
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:352
dolfinx::la::PETScMatrix::set_block_expand_fn
static std::function< int(std::int32_t, const std::int32_t *, std::int32_t, const std::int32_t *, const PetscScalar *)> set_block_expand_fn(Mat A, int bs0, int bs1, InsertMode mode)
Return a function with an interface for adding or inserting blocked values to the matrix A using non-...
Definition: PETScMatrix.cpp:241
dolfinx::la::PETScMatrix::set_from_options
void set_from_options()
Call PETSc function MatSetFromOptions on the PETSc Mat object.
Definition: PETScMatrix.cpp:346
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:373
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_matrix
Mat create_petsc_matrix(MPI_Comm comm, const SparsityPattern &sparsity_pattern, const std::string &type=std::string())
Create a PETSc Mat. Caller is responsible for destroying the returned object.
Definition: PETScMatrix.cpp:24
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:159
dolfinx::la::PETScOperator
This class is a base class for matrices that can be used in PETScKrylovSolver.
Definition: PETScOperator.h:20
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:338
dolfinx::la::PETScMatrix::AssemblyType
AssemblyType
Assembly type FINAL - corresponds to PETSc MAT_FINAL_ASSEMBLY FLUSH - corresponds to PETSc MAT_FLUSH_...
Definition: PETScMatrix.h:97
dolfinx::la::PETScMatrix::norm
double norm(la::Norm norm_type) const
Return norm of matrix.
Definition: PETScMatrix.cpp:284