DOLFIN
DOLFIN C++ interface
GenericMatrix.h
1 // Copyright (C) 2006-2011 Garth N. Wells
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // Modified by Johan Jansson 2006
19 // Modified by Anders Logg 2006-2012
20 // Modified by Ola Skavhaug 2007-2008
21 // Modified by Kent-Andre Mardal 2008
22 // Modified by Martin Alnæs 2008
23 // Modified by Mikael Mortensen 2011
24 //
25 // First added: 2006-04-24
26 // Last changed: 2012-08-20
27 
28 #ifndef __GENERIC_MATRIX_H
29 #define __GENERIC_MATRIX_H
30 
31 #include <tuple>
32 #include <vector>
33 #include <dolfin/common/constants.h>
34 #include "GenericTensor.h"
35 #include "GenericLinearOperator.h"
36 
37 
38 namespace dolfin
39 {
40 
41  class GenericVector;
42  class TensorLayout;
43 
45 
47  {
48  public:
49 
51  virtual ~GenericMatrix() {}
52 
53  //--- Implementation of the GenericTensor interface ---
54 
56  virtual std::size_t rank() const
57  { return 2; }
58 
60  virtual std::size_t size(std::size_t dim) const = 0;
61 
63  virtual std::pair<std::int64_t, std::int64_t>
64  local_range(std::size_t dim) const = 0;
65 
67  virtual std::size_t nnz() const = 0;
68 
70  virtual void get(double* block, const dolfin::la_index* num_rows,
71  const dolfin::la_index * const * rows) const
72  { get(block, num_rows[0], rows[0], num_rows[1], rows[1]); }
73 
75  virtual void set(const double* block, const dolfin::la_index* num_rows,
76  const dolfin::la_index * const * rows)
77  { set(block, num_rows[0], rows[0], num_rows[1], rows[1]); }
78 
80  virtual void set_local(const double* block,
81  const dolfin::la_index* num_rows,
82  const dolfin::la_index * const * rows)
83  { set_local(block, num_rows[0], rows[0], num_rows[1], rows[1]); }
84 
86  virtual void add(const double* block, const dolfin::la_index* num_rows,
87  const dolfin::la_index * const * rows)
88  { add(block, num_rows[0], rows[0], num_rows[1], rows[1]); }
89 
91  virtual void add_local(const double* block,
92  const dolfin::la_index* num_rows,
93  const dolfin::la_index * const * rows)
94  { add_local(block, num_rows[0], rows[0], num_rows[1], rows[1]); }
95 
97  virtual void
98  add(const double* block,
99  const std::vector<ArrayView<const dolfin::la_index>>& rows)
100  {
101  add(block, rows[0].size(), rows[0].data(),
102  rows[1].size(), rows[1].data());
103  }
104 
106  virtual void
107  add_local(const double* block,
108  const std::vector<ArrayView<const dolfin::la_index>>& rows)
109  {
110  add_local(block, rows[0].size(), rows[0].data(),
111  rows[1].size(), rows[1].data());
112  }
113 
115  virtual void zero() = 0;
116 
118  virtual void apply(std::string mode) = 0;
119 
121  virtual std::string str(bool verbose) const = 0;
122 
123  //--- Matrix interface ---
124 
126  virtual std::shared_ptr<GenericMatrix> copy() const = 0;
127 
136  virtual void init_vector(GenericVector& z, std::size_t dim) const = 0;
137 
139  virtual void get(double* block,
140  std::size_t m, const dolfin::la_index* rows,
141  std::size_t n, const dolfin::la_index* cols) const = 0;
142 
144  virtual void set(const double* block,
145  std::size_t m, const dolfin::la_index* rows,
146  std::size_t n, const dolfin::la_index* cols) = 0;
147 
149  virtual void set_local(const double* block,
150  std::size_t m, const dolfin::la_index* rows,
151  std::size_t n, const dolfin::la_index* cols) = 0;
152 
154  virtual void add(const double* block,
155  std::size_t m, const dolfin::la_index* rows,
156  std::size_t n, const dolfin::la_index* cols) = 0;
157 
159  virtual void add_local(const double* block,
160  std::size_t m, const dolfin::la_index* rows,
161  std::size_t n, const dolfin::la_index* cols) = 0;
162 
164  virtual void axpy(double a, const GenericMatrix& A,
165  bool same_nonzero_pattern) = 0;
166 
168  virtual double norm(std::string norm_type) const = 0;
169 
171  virtual void getrow(std::size_t row, std::vector<std::size_t>& columns,
172  std::vector<double>& values) const = 0;
173 
175  virtual void setrow(std::size_t row,
176  const std::vector<std::size_t>& columns,
177  const std::vector<double>& values) = 0;
178 
180  virtual void zero(std::size_t m, const dolfin::la_index* rows) = 0;
181 
183  virtual void zero_local(std::size_t m, const dolfin::la_index* rows) = 0;
184 
186  virtual void ident(std::size_t m, const dolfin::la_index* rows) = 0;
187 
189  virtual void ident_local(std::size_t m, const dolfin::la_index* rows) = 0;
190 
193  virtual void transpmult(const GenericVector& x, GenericVector& y) const = 0;
194 
196  virtual void get_diagonal(GenericVector& x) const = 0;
197 
199  virtual void set_diagonal(const GenericVector& x) = 0;
200 
202  virtual const GenericMatrix& operator*= (double a) = 0;
203 
205  virtual const GenericMatrix& operator/= (double a) = 0;
206 
209  {
210  axpy(1.0, A, false);
211  return *this;
212  }
213 
216  {
217  axpy(-1.0, A, false);
218  return *this;
219  }
220 
222  virtual bool is_symmetric(double tol) const
223  {
224  dolfin_error("GenericMatrix.h",
225  "test if matrix is symmetric",
226  "Not implemented by current linear algebra backend");
227  return false;
228  }
229 
231  virtual const GenericMatrix& operator= (const GenericMatrix& x) = 0;
232 
233  //--- Convenience functions ---
234 
236  virtual double operator() (dolfin::la_index i, dolfin::la_index j) const
237  { double value(0); get(&value, 1, &i, 1, &j); return value; }
238 
240  virtual double getitem(std::pair<dolfin::la_index,
241  dolfin::la_index> ij) const
242  {
243  double value(0);
244  get(&value, 1, &ij.first, 1, &ij.second);
245  return value;
246  }
247 
250  virtual void setitem(std::pair<dolfin::la_index, dolfin::la_index> ij,
251  double value)
252  { set(&value, 1, &ij.first, 1, &ij.second); }
253 
255  virtual void ident_zeros(double tol=DOLFIN_EPS);
256  };
257 
258 }
259 
260 #endif
virtual double getitem(std::pair< dolfin::la_index, dolfin::la_index > ij) const
Get value of given entry.
Definition: GenericMatrix.h:240
virtual void setrow(std::size_t row, const std::vector< std::size_t > &columns, const std::vector< double > &values)=0
Set values for given row (global index) on local process.
virtual void add_local(const double *block, const std::vector< ArrayView< const dolfin::la_index >> &rows)
Add block of values using local indices.
Definition: GenericMatrix.h:107
virtual const GenericMatrix & operator/=(double a)=0
Divide matrix by given number.
virtual std::shared_ptr< GenericMatrix > copy() const =0
Return copy of matrix.
Definition: adapt.h:29
virtual void transpmult(const GenericVector &x, GenericVector &y) const =0
virtual void set_diagonal(const GenericVector &x)=0
Set diagonal of a matrix.
virtual void set_local(const double *block, const dolfin::la_index *num_rows, const dolfin::la_index *const *rows)
Set block of values using local indices.
Definition: GenericMatrix.h:80
virtual std::size_t nnz() const =0
Return number of non-zero entries in matrix (collective)
Definition: GenericLinearOperator.h:42
virtual bool is_symmetric(double tol) const
Test if matrix is symmetric.
Definition: GenericMatrix.h:222
virtual const GenericMatrix & operator=(const GenericMatrix &x)=0
Assignment operator.
virtual std::pair< std::int64_t, std::int64_t > local_range(std::size_t dim) const =0
Return local ownership range.
virtual void zero()=0
Set all entries to zero and keep any sparse structure.
virtual void setitem(std::pair< dolfin::la_index, dolfin::la_index > ij, double value)
Definition: GenericMatrix.h:250
virtual void ident_local(std::size_t m, const dolfin::la_index *rows)=0
Set given rows (local row indices) to identity matrix.
const GenericMatrix & operator+=(const GenericMatrix &A)
Add given matrix.
Definition: GenericMatrix.h:208
virtual std::size_t rank() const
Return tensor rank (number of dimensions)
Definition: GenericMatrix.h:56
virtual void apply(std::string mode)=0
Finalize assembly of tensor.
virtual void get_diagonal(GenericVector &x) const =0
Get diagonal of a matrix.
virtual std::size_t size(std::size_t dim) const =0
Return size of given dimension.
virtual void getrow(std::size_t row, std::vector< std::size_t > &columns, std::vector< double > &values) const =0
Get non-zero values of given row (global index) on local process.
A common interface for arbitrary rank tensors.
Definition: GenericTensor.h:48
virtual void add(const double *block, const dolfin::la_index *num_rows, const dolfin::la_index *const *rows)
Add block of values using global indices.
Definition: GenericMatrix.h:86
Definition: ArrayView.h:31
virtual ~GenericMatrix()
Destructor.
Definition: GenericMatrix.h:51
virtual double operator()(dolfin::la_index i, dolfin::la_index j) const
Get value of given entry.
Definition: GenericMatrix.h:236
This class defines a common interface for matrices.
Definition: GenericMatrix.h:46
virtual void init_vector(GenericVector &z, std::size_t dim) const =0
PetscInt la_index
Index type for compatibility with linear algebra backend(s)
Definition: types.h:32
virtual void ident(std::size_t m, const dolfin::la_index *rows)=0
Set given rows (global row indices) to identity matrix.
void dolfin_error(std::string location, std::string task, std::string reason,...)
Definition: log.cpp:129
virtual void zero_local(std::size_t m, const dolfin::la_index *rows)=0
Set given rows (local row indices) to zero.
const GenericMatrix & operator-=(const GenericMatrix &A)
Subtract given matrix.
Definition: GenericMatrix.h:215
virtual std::string str(bool verbose) const =0
Return informal string representation (pretty-print)
virtual void ident_zeros(double tol=DOLFIN_EPS)
Insert one on the diagonal for all zero rows.
Definition: GenericMatrix.cpp:30
virtual void axpy(double a, const GenericMatrix &A, bool same_nonzero_pattern)=0
Add multiple of given matrix (AXPY operation)
virtual const GenericMatrix & operator*=(double a)=0
Multiply matrix by given number.
virtual void add(const double *block, const std::vector< ArrayView< const dolfin::la_index >> &rows)
Add block of values using global indices.
Definition: GenericMatrix.h:98
virtual double norm(std::string norm_type) const =0
Return norm of matrix.
This class defines a common interface for vectors.
Definition: GenericVector.h:47
virtual void add_local(const double *block, const dolfin::la_index *num_rows, const dolfin::la_index *const *rows)
Add block of values using local indices.
Definition: GenericMatrix.h:91