sparse_matrix.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 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" // deprecated
28 #include "libmesh/id_types.h"
31 
32 // C++ includes
33 #include <cstddef>
34 #include <iomanip>
35 #include <vector>
36 #include <memory>
37 
38 namespace libMesh
39 {
40 
41 // forward declarations
42 template <typename T> class SparseMatrix;
43 template <typename T> class DenseMatrix;
44 class DofMap;
45 namespace SparsityPattern { class Graph; }
46 template <typename T> class NumericVector;
47 
48 // This template helper function must be declared before it
49 // can be defined below.
50 template <typename T>
51 std::ostream & operator << (std::ostream & os, const SparseMatrix<T> & m);
52 
53 
63 template <typename T>
64 class SparseMatrix : public ReferenceCountedObject<SparseMatrix<T>>,
65  public ParallelObject
66 {
67 public:
79  explicit
80  SparseMatrix (const Parallel::Communicator & comm);
81 
86  SparseMatrix (SparseMatrix &&) = default;
87  SparseMatrix (const SparseMatrix &) = default;
88  SparseMatrix & operator= (const SparseMatrix &) = default;
89  SparseMatrix & operator= (SparseMatrix &&) = default;
90  virtual ~SparseMatrix () = default;
91 
96  static std::unique_ptr<SparseMatrix<T>>
97  build(const Parallel::Communicator & comm,
98  const SolverPackage solver_package = libMesh::default_solver_package());
99 
104  virtual bool initialized() const { return _is_initialized; }
105 
109  void attach_dof_map (const DofMap & dof_map)
110  { _dof_map = &dof_map; }
111 
121  virtual bool need_full_sparsity_pattern() const
122  { return false; }
123 
130 
143  virtual void init (const numeric_index_type m,
144  const numeric_index_type n,
145  const numeric_index_type m_l,
146  const numeric_index_type n_l,
147  const numeric_index_type nnz=30,
148  const numeric_index_type noz=10,
149  const numeric_index_type blocksize=1) = 0;
150 
154  virtual void init () = 0;
155 
159  virtual void clear () = 0;
160 
164  virtual void zero () = 0;
165 
169  virtual void zero_rows (std::vector<numeric_index_type> & rows, T diag_value = 0.0);
170 
175  virtual void close () = 0;
176 
182  virtual void flush () { close(); }
183 
187  virtual numeric_index_type m () const = 0;
188 
192  virtual numeric_index_type n () const = 0;
193 
198  virtual numeric_index_type row_start () const = 0;
199 
204  virtual numeric_index_type row_stop () const = 0;
205 
211  virtual void set (const numeric_index_type i,
212  const numeric_index_type j,
213  const T value) = 0;
214 
220  virtual void add (const numeric_index_type i,
221  const numeric_index_type j,
222  const T value) = 0;
223 
228  virtual void add_matrix (const DenseMatrix<T> & dm,
229  const std::vector<numeric_index_type> & rows,
230  const std::vector<numeric_index_type> & cols) = 0;
231 
236  virtual void add_matrix (const DenseMatrix<T> & dm,
237  const std::vector<numeric_index_type> & dof_indices) = 0;
238 
245  virtual void add_block_matrix (const DenseMatrix<T> & dm,
246  const std::vector<numeric_index_type> & brows,
247  const std::vector<numeric_index_type> & bcols);
248 
253  virtual void add_block_matrix (const DenseMatrix<T> & dm,
254  const std::vector<numeric_index_type> & dof_indices)
255  { this->add_block_matrix (dm, dof_indices, dof_indices); }
256 
260  virtual void add (const T a, const SparseMatrix<T> & X) = 0;
261 
268  virtual T operator () (const numeric_index_type i,
269  const numeric_index_type j) const = 0;
270 
279  virtual Real l1_norm () const = 0;
280 
291  virtual Real linfty_norm () const = 0;
292 
296  virtual bool closed() const = 0;
297 
302  void print(std::ostream & os=libMesh::out, const bool sparse=false) const;
303 
331  friend std::ostream & operator << <>(std::ostream & os, const SparseMatrix<T> & m);
332 
337  virtual void print_personal(std::ostream & os=libMesh::out) const = 0;
338 
344  virtual void print_matlab(const std::string & /*name*/ = "") const
345  {
346  libmesh_not_implemented();
347  }
348 
354  virtual void create_submatrix(SparseMatrix<T> & submatrix,
355  const std::vector<numeric_index_type> & rows,
356  const std::vector<numeric_index_type> & cols) const
357  {
358  this->_get_submatrix(submatrix,
359  rows,
360  cols,
361  false); // false means DO NOT REUSE submatrix
362  }
363 
370  virtual void reinit_submatrix(SparseMatrix<T> & submatrix,
371  const std::vector<numeric_index_type> & rows,
372  const std::vector<numeric_index_type> & cols) const
373  {
374  this->_get_submatrix(submatrix,
375  rows,
376  cols,
377  true); // true means REUSE submatrix
378  }
379 
384  void vector_mult (NumericVector<T> & dest,
385  const NumericVector<T> & arg) const;
386 
391  void vector_mult_add (NumericVector<T> & dest,
392  const NumericVector<T> & arg) const;
393 
397  virtual void get_diagonal (NumericVector<T> & dest) const = 0;
398 
403  virtual void get_transpose (SparseMatrix<T> & dest) const = 0;
404 
405 protected:
406 
414  virtual void _get_submatrix(SparseMatrix<T> & /*submatrix*/,
415  const std::vector<numeric_index_type> & /*rows*/,
416  const std::vector<numeric_index_type> & /*cols*/,
417  const bool /*reuse_submatrix*/) const
418  {
419  libmesh_not_implemented();
420  }
421 
425  DofMap const * _dof_map;
426 
431 };
432 
433 
434 
435 //-----------------------------------------------------------------------
436 // SparseMatrix inline members
437 
438 // For SGI MIPSpro this implementation must occur after
439 // the full specialization of the print() member.
440 //
441 // It's generally easier to define these friend functions in the header
442 // file.
443 template <typename T>
444 std::ostream & operator << (std::ostream & os, const SparseMatrix<T> & m)
445 {
446  m.print(os);
447  return os;
448 }
449 
450 
451 } // namespace libMesh
452 
453 
454 #endif // LIBMESH_SPARSE_MATRIX_H
virtual void add_block_matrix(const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &brows, const std::vector< numeric_index_type > &bcols)
Definition: sparse_matrix.C:56
static std::unique_ptr< SparseMatrix< T > > build(const Parallel::Communicator &comm, const SolverPackage solver_package=libMesh::default_solver_package())
virtual bool initialized() const
virtual void create_submatrix(SparseMatrix< T > &submatrix, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols) const
virtual void print_personal(std::ostream &os=libMesh::out) const =0
SparseMatrix(const Parallel::Communicator &comm)
Definition: sparse_matrix.C:45
void vector_mult(NumericVector< T > &dest, const NumericVector< T > &arg) const
virtual ~SparseMatrix()=default
Provides a uniform interface to vector storage schemes for different linear algebra libraries...
Definition: diff_context.h:40
const Parallel::Communicator & comm() const
virtual numeric_index_type row_stop() const =0
virtual void clear()=0
virtual void add(const numeric_index_type i, const numeric_index_type j, const T value)=0
SolverPackage default_solver_package()
Definition: libmesh.C:971
Manages the degrees of freedom (DOFs) in a simulation.
Definition: dof_map.h:176
virtual void print_matlab(const std::string &="") const
virtual void flush()
virtual void add_matrix(const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols)=0
virtual void zero()=0
virtual void init()=0
dof_id_type numeric_index_type
Definition: id_types.h:92
virtual numeric_index_type m() const =0
virtual void zero_rows(std::vector< numeric_index_type > &rows, T diag_value=0.0)
virtual void get_diagonal(NumericVector< T > &dest) const =0
virtual void reinit_submatrix(SparseMatrix< T > &submatrix, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols) const
virtual bool closed() const =0
void attach_dof_map(const DofMap &dof_map)
DofMap const * _dof_map
virtual bool need_full_sparsity_pattern() const
virtual void close()=0
virtual void update_sparsity_pattern(const SparsityPattern::Graph &)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void vector_mult_add(NumericVector< T > &dest, const NumericVector< T > &arg) const
virtual numeric_index_type row_start() const =0
virtual void get_transpose(SparseMatrix< T > &dest) const =0
static const bool value
Definition: xdr_io.C:109
virtual void _get_submatrix(SparseMatrix< T > &, const std::vector< numeric_index_type > &, const std::vector< numeric_index_type > &, const bool) const
virtual Real linfty_norm() const =0
SparseMatrix & operator=(const SparseMatrix &)=default
virtual Real l1_norm() const =0
A matrix object used for finite element assembly and numerics.
Definition: dense_matrix.h:54
OStreamProxy out(std::cout)
virtual T operator()(const numeric_index_type i, const numeric_index_type j) const =0
void print(std::ostream &os=libMesh::out, const bool sparse=false) const
virtual numeric_index_type n() const =0
virtual void add_block_matrix(const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &dof_indices)