sparse_matrix.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2014 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"
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 
64 template <typename T>
65 class SparseMatrix : public ReferenceCountedObject<SparseMatrix<T> >,
66  public ParallelObject
67 {
68 public:
84  explicit
85  SparseMatrix (const Parallel::Communicator &comm
86  LIBMESH_CAN_DEFAULT_TO_COMMWORLD);
87 
93  virtual ~SparseMatrix ();
94 
99  static AutoPtr<SparseMatrix<T> >
100  build(const Parallel::Communicator &comm,
101  const SolverPackage solver_package = libMesh::default_solver_package());
102 
107  virtual bool initialized() const { return _is_initialized; }
108 
113  { _dof_map = &dof_map; }
114 
122  virtual bool need_full_sparsity_pattern() const
123  { return false; }
124 
131 
142  virtual void init (const numeric_index_type m,
143  const numeric_index_type n,
144  const numeric_index_type m_l,
145  const numeric_index_type n_l,
146  const numeric_index_type nnz=30,
147  const numeric_index_type noz=10,
148  const numeric_index_type blocksize=1) = 0;
149 
153  virtual void init () = 0;
154 
161  virtual void clear () = 0;
162 
166  virtual void zero () = 0;
167 
171  virtual void zero_rows (std::vector<numeric_index_type> & rows, T diag_value = 0.0);
172 
178  virtual void close () const = 0;
179 
184  virtual numeric_index_type m () const = 0;
185 
190  virtual numeric_index_type n () const = 0;
191 
196  virtual numeric_index_type row_start () const = 0;
197 
202  virtual numeric_index_type row_stop () const = 0;
203 
210  virtual void set (const numeric_index_type i,
211  const numeric_index_type j,
212  const T value) = 0;
213 
222  virtual void add (const numeric_index_type i,
223  const numeric_index_type j,
224  const T value) = 0;
225 
232  virtual void add_matrix (const DenseMatrix<T> &dm,
233  const std::vector<numeric_index_type> &rows,
234  const std::vector<numeric_index_type> &cols) = 0;
235 
240  virtual void add_matrix (const DenseMatrix<T> &dm,
241  const std::vector<numeric_index_type> &dof_indices) = 0;
242 
250  virtual void add_block_matrix (const DenseMatrix<T> &dm,
251  const std::vector<numeric_index_type> &brows,
252  const std::vector<numeric_index_type> &bcols);
253 
258  virtual void add_block_matrix (const DenseMatrix<T> &dm,
259  const std::vector<numeric_index_type> &dof_indices)
260  { this->add_block_matrix (dm, dof_indices, dof_indices); }
261 
267  virtual void add (const T, SparseMatrix<T> &) = 0;
268 
276  virtual T operator () (const numeric_index_type i,
277  const numeric_index_type j) const = 0;
278 
289  virtual Real l1_norm () const = 0;
290 
302  virtual Real linfty_norm () const = 0;
303 
308  virtual bool closed() const = 0;
309 
315  void print(std::ostream& os=libMesh::out, const bool sparse=false) const;
316 
342  friend std::ostream& operator << <>(std::ostream& os, const SparseMatrix<T>& m);
343 
348  virtual void print_personal(std::ostream& os=libMesh::out) const = 0;
349 
356  virtual void print_matlab(const std::string name="NULL") const
357  {
358  libMesh::err << "ERROR: Not Implemented in base class yet!" << std::endl;
359  libMesh::err << "ERROR writing MATLAB file " << name << std::endl;
360  libmesh_error();
361  }
362 
368  virtual void create_submatrix(SparseMatrix<T>& submatrix,
369  const std::vector<numeric_index_type>& rows,
370  const std::vector<numeric_index_type>& cols) const
371  {
372  this->_get_submatrix(submatrix,
373  rows,
374  cols,
375  false); // false means DO NOT REUSE submatrix
376  }
377 
384  virtual void reinit_submatrix(SparseMatrix<T>& submatrix,
385  const std::vector<numeric_index_type>& rows,
386  const std::vector<numeric_index_type>& cols) const
387  {
388  this->_get_submatrix(submatrix,
389  rows,
390  cols,
391  true); // true means REUSE submatrix
392  }
393 
398  void vector_mult (NumericVector<T>& dest,
399  const NumericVector<T>& arg) const;
400 
404  void vector_mult_add (NumericVector<T>& dest,
405  const NumericVector<T>& arg) const;
406 
410  virtual void get_diagonal (NumericVector<T>& dest) const = 0;
411 
416  virtual void get_transpose (SparseMatrix<T>& dest) const = 0;
417 
418 protected:
419 
426  const std::vector<numeric_index_type>& ,
427  const std::vector<numeric_index_type>& ,
428  const bool) const
429  {
430  libMesh::err << "Error! This function is not yet implemented in the base class!"
431  << std::endl;
432  libmesh_error();
433  }
434 
438  DofMap const *_dof_map;
439 
445 };
446 
447 
448 
449 //-----------------------------------------------------------------------
450 // SparseMatrix inline members
451 
452 // For SGI MIPSpro this implementation must occur after
453 // the full specialization of the print() member.
454 //
455 // It's generally easier to define these friend functions in the header
456 // file.
457 template <typename T>
458 std::ostream& operator << (std::ostream& os, const SparseMatrix<T>& m)
459 {
460  m.print(os);
461  return os;
462 }
463 
464 
465 } // namespace libMesh
466 
467 
468 #endif // LIBMESH_SPARSE_MATRIX_H

Site Created By: libMesh Developers
Last modified: February 07 2014 16:57:07 UTC

Hosted By:
SourceForge.net Logo