libMesh
sparse_matrix.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_SPARSE_MATRIX_H
21 #define LIBMESH_SPARSE_MATRIX_H
22 
23 
24 // Local includes
25 #include "libmesh/libmesh.h"
26 #include "libmesh/libmesh_common.h"
27 #include "libmesh/auto_ptr.h"
28 #include "libmesh/id_types.h"
29 #include "libmesh/reference_counted_object.h"
30 #include "libmesh/parallel_object.h"
31 
32 // C++ includes
33 #include <cstddef>
34 #include <iomanip>
35 #include <vector>
36 
37 namespace libMesh
38 {
39 
40 // forward declarations
41 template <typename T> class SparseMatrix;
42 template <typename T> class DenseMatrix;
43 class DofMap;
44 namespace SparsityPattern { class Graph; }
45 template <typename T> class NumericVector;
46 
47 // This template helper function must be declared before it
48 // can be defined below.
49 template <typename T>
50 std::ostream & operator << (std::ostream & os, const SparseMatrix<T> & m);
51 
52 
62 template <typename T>
63 class SparseMatrix : public ReferenceCountedObject<SparseMatrix<T>>,
64  public ParallelObject
65 {
66 public:
78  explicit
79  SparseMatrix (const Parallel::Communicator & comm
80  LIBMESH_CAN_DEFAULT_TO_COMMWORLD);
81 
86  virtual ~SparseMatrix ();
87 
92  static UniquePtr<SparseMatrix<T>>
93  build(const Parallel::Communicator & comm,
94  const SolverPackage solver_package = libMesh::default_solver_package());
95 
100  virtual bool initialized() const { return _is_initialized; }
101 
105  void attach_dof_map (const DofMap & dof_map)
106  { _dof_map = &dof_map; }
107 
117  virtual bool need_full_sparsity_pattern() const
118  { return false; }
119 
126 
139  virtual void init (const numeric_index_type m,
140  const numeric_index_type n,
141  const numeric_index_type m_l,
142  const numeric_index_type n_l,
143  const numeric_index_type nnz=30,
144  const numeric_index_type noz=10,
145  const numeric_index_type blocksize=1) = 0;
146 
150  virtual void init () = 0;
151 
155  virtual void clear () = 0;
156 
160  virtual void zero () = 0;
161 
165  virtual void zero_rows (std::vector<numeric_index_type> & rows, T diag_value = 0.0);
166 
171  virtual void close () = 0;
172 
178  virtual void flush () { close(); }
179 
183  virtual numeric_index_type m () const = 0;
184 
188  virtual numeric_index_type n () const = 0;
189 
194  virtual numeric_index_type row_start () const = 0;
195 
200  virtual numeric_index_type row_stop () const = 0;
201 
207  virtual void set (const numeric_index_type i,
208  const numeric_index_type j,
209  const T value) = 0;
210 
216  virtual void add (const numeric_index_type i,
217  const numeric_index_type j,
218  const T value) = 0;
219 
224  virtual void add_matrix (const DenseMatrix<T> & dm,
225  const std::vector<numeric_index_type> & rows,
226  const std::vector<numeric_index_type> & cols) = 0;
227 
232  virtual void add_matrix (const DenseMatrix<T> & dm,
233  const std::vector<numeric_index_type> & dof_indices) = 0;
234 
241  virtual void add_block_matrix (const DenseMatrix<T> & dm,
242  const std::vector<numeric_index_type> & brows,
243  const std::vector<numeric_index_type> & bcols);
244 
249  virtual void add_block_matrix (const DenseMatrix<T> & dm,
250  const std::vector<numeric_index_type> & dof_indices)
251  { this->add_block_matrix (dm, dof_indices, dof_indices); }
252 
256  virtual void add (const T a, SparseMatrix<T> & X) = 0;
257 
264  virtual T operator () (const numeric_index_type i,
265  const numeric_index_type j) const = 0;
266 
275  virtual Real l1_norm () const = 0;
276 
287  virtual Real linfty_norm () const = 0;
288 
292  virtual bool closed() const = 0;
293 
298  void print(std::ostream & os=libMesh::out, const bool sparse=false) const;
299 
327  friend std::ostream & operator << <>(std::ostream & os, const SparseMatrix<T> & m);
328 
333  virtual void print_personal(std::ostream & os=libMesh::out) const = 0;
334 
340  virtual void print_matlab(const std::string & /*name*/ = "") const
341  {
342  libmesh_not_implemented();
343  }
344 
350  virtual void create_submatrix(SparseMatrix<T> & submatrix,
351  const std::vector<numeric_index_type> & rows,
352  const std::vector<numeric_index_type> & cols) const
353  {
354  this->_get_submatrix(submatrix,
355  rows,
356  cols,
357  false); // false means DO NOT REUSE submatrix
358  }
359 
366  virtual void reinit_submatrix(SparseMatrix<T> & submatrix,
367  const std::vector<numeric_index_type> & rows,
368  const std::vector<numeric_index_type> & cols) const
369  {
370  this->_get_submatrix(submatrix,
371  rows,
372  cols,
373  true); // true means REUSE submatrix
374  }
375 
380  void vector_mult (NumericVector<T> & dest,
381  const NumericVector<T> & arg) const;
382 
387  void vector_mult_add (NumericVector<T> & dest,
388  const NumericVector<T> & arg) const;
389 
393  virtual void get_diagonal (NumericVector<T> & dest) const = 0;
394 
399  virtual void get_transpose (SparseMatrix<T> & dest) const = 0;
400 
401 protected:
402 
410  virtual void _get_submatrix(SparseMatrix<T> & /*submatrix*/,
411  const std::vector<numeric_index_type> & /*rows*/,
412  const std::vector<numeric_index_type> & /*cols*/,
413  const bool /*reuse_submatrix*/) const
414  {
415  libmesh_not_implemented();
416  }
417 
421  DofMap const * _dof_map;
422 
427 };
428 
429 
430 
431 //-----------------------------------------------------------------------
432 // SparseMatrix inline members
433 
434 // For SGI MIPSpro this implementation must occur after
435 // the full specialization of the print() member.
436 //
437 // It's generally easier to define these friend functions in the header
438 // file.
439 template <typename T>
440 std::ostream & operator << (std::ostream & os, const SparseMatrix<T> & m)
441 {
442  m.print(os);
443  return os;
444 }
445 
446 
447 } // namespace libMesh
448 
449 
450 #endif // LIBMESH_SPARSE_MATRIX_H
bool closed()
Checks that the library has been closed.
Definition: libmesh.C:281
SolverPackage
Defines an enum for various linear solver packages.
virtual void print_matlab(const std::string &="") const
Print the contents of the matrix in Matlab&#39;s sparse matrix format.
Numeric vector.
Definition: dof_map.h:66
The libMesh namespace provides an interface to certain functionality in the library.
const Number zero
.
Definition: libmesh.h:178
virtual bool initialized() const
Generic sparse matrix.
Definition: dof_map.h:65
SolverPackage default_solver_package()
Definition: libmesh.C:995
This class handles the numbering of degrees of freedom on a mesh.
Definition: dof_map.h:167
virtual void flush()
For PETSc matrix , this function is similar to close but without shrinking memory.
virtual void create_submatrix(SparseMatrix< T > &submatrix, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols) const
This function creates a matrix called "submatrix" which is defined by the row and column indices give...
dof_id_type numeric_index_type
Definition: id_types.h:92
bool _is_initialized
Flag that tells if init() has been called.
Definition: libmesh.C:255
bool _is_initialized
Flag indicating whether or not the matrix has been initialized.
void init(triangulateio &t)
Initializes the fields of t to NULL/0 as necessary.
virtual bool need_full_sparsity_pattern() const
virtual void reinit_submatrix(SparseMatrix< T > &submatrix, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols) const
This function is similar to the one above, but it allows you to reuse the existing sparsity pattern o...
void attach_dof_map(const DofMap &dof_map)
Get a pointer to the DofMap to use.
DofMap const * _dof_map
The DofMap object associated with this object.
virtual void update_sparsity_pattern(const SparsityPattern::Graph &)
Updates the matrix sparsity pattern.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
OStreamProxy out
static const bool value
Definition: xdr_io.C:108
virtual void _get_submatrix(SparseMatrix< T > &, const std::vector< numeric_index_type > &, const std::vector< numeric_index_type > &, const bool) const
Protected implementation of the create_submatrix and reinit_submatrix routines.
Defines a dense matrix for use in Finite Element-type computations.
Definition: dof_map.h:64
virtual void add_block_matrix(const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &dof_indices)
Same as add_block_matrix(), but assumes the row and column maps are the same.