DOLFIN
DOLFIN C++ interface
PETScMatrix.h
1 // Copyright (C) 2004-2012 Johan Hoffman, Johan Jansson, Anders Logg
2 // and Garth N. Wells
3 //
4 // This file is part of DOLFIN.
5 //
6 // DOLFIN is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // DOLFIN is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
18 //
19 // Modified by Andy R. Terrel 2005
20 // Modified by Garth N. Wells 2006-2009
21 // Modified by Kent-Andre Mardal 2008
22 // Modified by Ola Skavhaug 2008
23 // Modified by Fredrik Valdmanis 2011
24 //
25 // First added: 2004-01-01
26 // Last changed: 2012-08-22
27 
28 #ifndef __PETSC_MATRIX_H
29 #define __PETSC_MATRIX_H
30 
31 #ifdef HAS_PETSC
32 
33 #include <map>
34 #include <memory>
35 #include <string>
36 
37 #include <petscmat.h>
38 #include <petscsys.h>
39 
40 #include "GenericMatrix.h"
41 #include "PETScBaseMatrix.h"
42 
43 namespace dolfin
44 {
45 
46  class PETScVector;
47  class TensorLayout;
48  class VectorSpaceBasis;
49 
57 
58  class PETScMatrix : public GenericMatrix, public PETScBaseMatrix
59  {
60  public:
61 
63  PETScMatrix();
64 
66  explicit PETScMatrix(MPI_Comm comm);
67 
70  explicit PETScMatrix(Mat A);
71 
73  PETScMatrix(const PETScMatrix& A);
74 
76  virtual ~PETScMatrix();
77 
78  //--- Implementation of the GenericTensor interface ---
79 
81  void init(const TensorLayout& tensor_layout);
82 
84  bool empty() const;
85 
87  std::size_t size(std::size_t dim) const
88  { return PETScBaseMatrix::size(dim); }
89 
91  std::pair<std::int64_t, std::int64_t> local_range(std::size_t dim) const
92  { return PETScBaseMatrix::local_range(dim); }
93 
95  std::size_t nnz() const;
96 
98  virtual void zero();
99 
106  virtual void apply(std::string mode);
107 
109  MPI_Comm mpi_comm() const;
110 
112  virtual std::string str(bool verbose) const;
113 
114  //--- Implementation of the GenericMatrix interface --
115 
117  virtual std::shared_ptr<GenericMatrix> copy() const;
118 
127  virtual void init_vector(GenericVector& z, std::size_t dim) const
128  { PETScBaseMatrix::init_vector(z, dim); }
129 
131  virtual void get(double* block,
132  std::size_t m, const dolfin::la_index* rows,
133  std::size_t n, const dolfin::la_index* cols) const;
134 
136  virtual void set(const double* block,
137  std::size_t m, const dolfin::la_index* rows,
138  std::size_t n, const dolfin::la_index* cols);
139 
141  virtual void set_local(const double* block,
142  std::size_t m, const dolfin::la_index* rows,
143  std::size_t n, const dolfin::la_index* cols);
144 
146  virtual void add(const double* block,
147  std::size_t m, const dolfin::la_index* rows,
148  std::size_t n, const dolfin::la_index* cols);
149 
151  virtual void add_local(const double* block,
152  std::size_t m, const dolfin::la_index* rows,
153  std::size_t n, const dolfin::la_index* cols);
154 
156  virtual void axpy(double a, const GenericMatrix& A,
157  bool same_nonzero_pattern);
158 
160  double norm(std::string norm_type) const;
161 
163  virtual void getrow(std::size_t row,
164  std::vector<std::size_t>& columns,
165  std::vector<double>& values) const;
166 
168  virtual void setrow(std::size_t row,
169  const std::vector<std::size_t>& columns,
170  const std::vector<double>& values);
171 
173  virtual void zero(std::size_t m, const dolfin::la_index* rows);
174 
176  virtual void zero_local(std::size_t m, const dolfin::la_index* rows);
177 
179  virtual void ident(std::size_t m, const dolfin::la_index* rows);
180 
182  virtual void ident_local(std::size_t m, const dolfin::la_index* rows);
183 
184  // Matrix-vector product, y = Ax
185  virtual void mult(const GenericVector& x, GenericVector& y) const;
186 
187  // Matrix-vector product, y = A^T x
188  virtual void transpmult(const GenericVector& x, GenericVector& y) const;
189 
191  virtual void get_diagonal(GenericVector& x) const;
192 
194  virtual void set_diagonal(const GenericVector& x);
195 
197  virtual const PETScMatrix& operator*= (double a);
198 
200  virtual const PETScMatrix& operator/= (double a);
201 
203  virtual const GenericMatrix& operator= (const GenericMatrix& A);
204 
206  virtual bool is_symmetric(double tol) const;
207 
208  //--- Special functions ---
209 
211  virtual GenericLinearAlgebraFactory& factory() const;
212 
213  //--- Special PETSc Functions ---
214 
217  void set_options_prefix(std::string options_prefix);
218 
221  std::string get_options_prefix() const;
222 
224  void set_from_options();
225 
227  const PETScMatrix& operator= (const PETScMatrix& A);
228 
231  void set_nullspace(const VectorSpaceBasis& nullspace);
232 
235  void set_near_nullspace(const VectorSpaceBasis& nullspace);
236 
238  void binary_dump(std::string file_name) const;
239 
240  private:
241 
242  // Create PETSc nullspace object
243  MatNullSpace create_petsc_nullspace(const VectorSpaceBasis& nullspace) const;
244 
245  // PETSc norm types
246  static const std::map<std::string, NormType> norm_types;
247 
248  };
249 
250 }
251 
252 #endif
253 
254 #endif
virtual void ident_local(std::size_t m, const dolfin::la_index *rows)
Set given rows (local row indices) to identity matrix.
Definition: PETScMatrix.cpp:400
std::string get_options_prefix() const
Definition: PETScMatrix.cpp:623
virtual void set_diagonal(const GenericVector &x)
Set diagonal of a matrix.
Definition: PETScMatrix.cpp:492
virtual std::shared_ptr< GenericMatrix > copy() const
Return copy of matrix.
Definition: PETScMatrix.cpp:94
virtual void zero_local(std::size_t m, const dolfin::la_index *rows)
Set given rows (local row indices) to zero.
Definition: PETScMatrix.cpp:372
virtual void apply(std::string mode)
Definition: PETScMatrix.cpp:528
Definition: adapt.h:29
virtual void axpy(double a, const GenericMatrix &A, bool same_nonzero_pattern)
Add multiple of given matrix (AXPY operation)
Definition: PETScMatrix.cpp:277
virtual const PETScMatrix & operator/=(double a)
Divide matrix by given number.
Definition: PETScMatrix.cpp:590
void set_from_options()
Call PETSc function MatSetFromOptions on the PETSc Mat object.
Definition: PETScMatrix.cpp:631
Definition: PETScMatrix.h:58
virtual void set_local(const double *block, std::size_t m, const dolfin::la_index *rows, std::size_t n, const dolfin::la_index *cols)
Set block of values using local indices.
Definition: PETScMatrix.cpp:247
virtual bool is_symmetric(double tol) const
Test if matrix is symmetric.
Definition: PETScMatrix.cpp:603
std::size_t size(std::size_t dim) const
Return size of given dimension.
Definition: PETScMatrix.h:87
virtual void init_vector(GenericVector &z, std::size_t dim) const
Definition: PETScMatrix.h:127
bool empty() const
Return true if empty.
Definition: PETScMatrix.cpp:220
Definition: VectorSpaceBasis.h:33
virtual const GenericMatrix & operator=(const GenericMatrix &A)
Assignment operator.
Definition: PETScMatrix.cpp:597
virtual void add_local(const double *block, std::size_t m, const dolfin::la_index *rows, std::size_t n, const dolfin::la_index *cols)
Add block of values using local indices.
Definition: PETScMatrix.cpp:267
Definition: TensorLayout.h:41
Base class for LinearAlgebra factories.
Definition: GenericLinearAlgebraFactory.h:46
std::pair< std::int64_t, std::int64_t > local_range(std::size_t dim) const
Return local ownership range.
Definition: PETScMatrix.h:91
void binary_dump(std::string file_name) const
Dump matrix to PETSc binary format.
Definition: PETScMatrix.cpp:704
virtual std::string str(bool verbose) const
Return informal string representation (pretty-print)
Definition: PETScMatrix.cpp:720
std::size_t nnz() const
Return number of non-zero entries in matrix (collective)
Definition: PETScMatrix.cpp:568
virtual GenericLinearAlgebraFactory & factory() const
Return linear algebra backend factory.
Definition: PETScMatrix.cpp:612
virtual void ident(std::size_t m, const dolfin::la_index *rows)
Set given rows (global row indices) to identity matrix.
Definition: PETScMatrix.cpp:383
virtual void add(const double *block, std::size_t m, const dolfin::la_index *rows, std::size_t n, const dolfin::la_index *cols)
Add block of values using global indices.
Definition: PETScMatrix.cpp:257
virtual ~PETScMatrix()
Destructor.
Definition: PETScMatrix.cpp:89
double norm(std::string norm_type) const
Return norm of matrix.
Definition: PETScMatrix.cpp:509
void set_near_nullspace(const VectorSpaceBasis &nullspace)
Definition: PETScMatrix.cpp:690
void init(const TensorLayout &tensor_layout)
Initialize zero tensor using tensor layout.
Definition: PETScMatrix.cpp:99
Definition: PETScBaseMatrix.h:49
virtual const PETScMatrix & operator*=(double a)
Multiply matrix by given number.
Definition: PETScMatrix.cpp:582
This class defines a common interface for matrices.
Definition: GenericMatrix.h:46
PetscInt la_index
Index type for compatibility with linear algebra backend(s)
Definition: types.h:32
virtual void setrow(std::size_t row, const std::vector< std::size_t > &columns, const std::vector< double > &values)
Set values for given row.
Definition: PETScMatrix.cpp:337
MPI_Comm mpi_comm() const
Return MPI communicator.
Definition: PETScMatrix.cpp:563
virtual void get_diagonal(GenericVector &x) const
Get diagonal of a matrix.
Definition: PETScMatrix.cpp:475
virtual void mult(const GenericVector &x, GenericVector &y) const
Compute matrix-vector product y = Ax.
Definition: PETScMatrix.cpp:417
void init_vector(GenericVector &z, std::size_t dim) const
Definition: PETScBaseMatrix.cpp:106
std::pair< std::int64_t, std::int64_t > size() const
Definition: PETScBaseMatrix.cpp:79
virtual void getrow(std::size_t row, std::vector< std::size_t > &columns, std::vector< double > &values) const
Get non-zero values of given row.
Definition: PETScMatrix.cpp:317
This class defines a common interface for vectors.
Definition: GenericVector.h:47
virtual void zero()
Set all entries to zero and keep any sparse structure.
Definition: PETScMatrix.cpp:575
std::pair< std::int64_t, std::int64_t > local_range(std::size_t dim) const
Return local range along dimension dim.
Definition: PETScBaseMatrix.cpp:89
void set_nullspace(const VectorSpaceBasis &nullspace)
Definition: PETScMatrix.cpp:676
void set_options_prefix(std::string options_prefix)
Definition: PETScMatrix.cpp:617
PETScMatrix()
Create empty matrix (on MPI_COMM_WORLD)
Definition: PETScMatrix.cpp:56
virtual void transpmult(const GenericVector &x, GenericVector &y) const
Definition: PETScMatrix.cpp:446