$extrastylesheet
sparse_matrix.h
Go to the documentation of this file.
00001 // The libMesh Finite Element Library.
00002 // Copyright (C) 2002-2014 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
00003 
00004 // This library is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public
00006 // License as published by the Free Software Foundation; either
00007 // version 2.1 of the License, or (at your option) any later version.
00008 
00009 // This library is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // Lesser General Public License for more details.
00013 
00014 // You should have received a copy of the GNU Lesser General Public
00015 // License along with this library; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018 
00019 
00020 #ifndef LIBMESH_SPARSE_MATRIX_H
00021 #define LIBMESH_SPARSE_MATRIX_H
00022 
00023 
00024 // Local includes
00025 #include "libmesh/libmesh.h"
00026 #include "libmesh/libmesh_common.h"
00027 #include "libmesh/auto_ptr.h"
00028 #include "libmesh/id_types.h"
00029 #include "libmesh/reference_counted_object.h"
00030 #include "libmesh/parallel_object.h"
00031 
00032 // C++ includes
00033 #include <cstddef>
00034 #include <iomanip>
00035 #include <vector>
00036 
00037 namespace libMesh
00038 {
00039 
00040 // forward declarations
00041 template <typename T> class SparseMatrix;
00042 template <typename T> class DenseMatrix;
00043 class DofMap;
00044 namespace SparsityPattern { class Graph; }
00045 template <typename T> class NumericVector;
00046 
00047 // This template helper function must be declared before it
00048 // can be defined below.
00049 template <typename T>
00050 std::ostream& operator << (std::ostream& os, const SparseMatrix<T>& m);
00051 
00052 
00064 template <typename T>
00065 class SparseMatrix : public ReferenceCountedObject<SparseMatrix<T> >,
00066                      public ParallelObject
00067 {
00068 public:
00084   explicit
00085   SparseMatrix (const Parallel::Communicator &comm
00086                 LIBMESH_CAN_DEFAULT_TO_COMMWORLD);
00087 
00093   virtual ~SparseMatrix ();
00094 
00099   static UniquePtr<SparseMatrix<T> >
00100   build(const Parallel::Communicator &comm,
00101         const SolverPackage solver_package = libMesh::default_solver_package());
00102 
00107   virtual bool initialized() const { return _is_initialized; }
00108 
00112   void attach_dof_map (const DofMap& dof_map)
00113   { _dof_map = &dof_map; }
00114 
00122   virtual bool need_full_sparsity_pattern() const
00123   { return false; }
00124 
00130   virtual void update_sparsity_pattern (const SparsityPattern::Graph &) {}
00131 
00142   virtual void init (const numeric_index_type m,
00143                      const numeric_index_type n,
00144                      const numeric_index_type m_l,
00145                      const numeric_index_type n_l,
00146                      const numeric_index_type nnz=30,
00147                      const numeric_index_type noz=10,
00148                      const numeric_index_type blocksize=1) = 0;
00149 
00153   virtual void init () = 0;
00154 
00161   virtual void clear () = 0;
00162 
00166   virtual void zero () = 0;
00167 
00171   virtual void zero_rows (std::vector<numeric_index_type> & rows, T diag_value = 0.0);
00172 
00178   virtual void close () const = 0;
00179 
00184   virtual numeric_index_type m () const = 0;
00185 
00190   virtual numeric_index_type n () const = 0;
00191 
00196   virtual numeric_index_type row_start () const = 0;
00197 
00202   virtual numeric_index_type row_stop () const = 0;
00203 
00210   virtual void set (const numeric_index_type i,
00211                     const numeric_index_type j,
00212                     const T value) = 0;
00213 
00222   virtual void add (const numeric_index_type i,
00223                     const numeric_index_type j,
00224                     const T value) = 0;
00225 
00232   virtual void add_matrix (const DenseMatrix<T> &dm,
00233                            const std::vector<numeric_index_type> &rows,
00234                            const std::vector<numeric_index_type> &cols) = 0;
00235 
00240   virtual void add_matrix (const DenseMatrix<T> &dm,
00241                            const std::vector<numeric_index_type> &dof_indices) = 0;
00242 
00250   virtual void add_block_matrix (const DenseMatrix<T> &dm,
00251                                  const std::vector<numeric_index_type> &brows,
00252                                  const std::vector<numeric_index_type> &bcols);
00253 
00258   virtual void add_block_matrix (const DenseMatrix<T> &dm,
00259                                  const std::vector<numeric_index_type> &dof_indices)
00260   { this->add_block_matrix (dm, dof_indices, dof_indices); }
00261 
00267   virtual void add (const T, SparseMatrix<T> &) = 0;
00268 
00276   virtual T operator () (const numeric_index_type i,
00277                          const numeric_index_type j) const = 0;
00278 
00289   virtual Real l1_norm () const = 0;
00290 
00302   virtual Real linfty_norm () const = 0;
00303 
00308   virtual bool closed() const = 0;
00309 
00315   void print(std::ostream& os=libMesh::out, const bool sparse=false) const;
00316 
00342   friend std::ostream& operator << <>(std::ostream& os, const SparseMatrix<T>& m);
00343 
00348   virtual void print_personal(std::ostream& os=libMesh::out) const = 0;
00349 
00356   virtual void print_matlab(const std::string& /*name*/ = "") const
00357   {
00358     libmesh_not_implemented();
00359   }
00360 
00366   virtual void create_submatrix(SparseMatrix<T>& submatrix,
00367                                 const std::vector<numeric_index_type>& rows,
00368                                 const std::vector<numeric_index_type>& cols) const
00369   {
00370     this->_get_submatrix(submatrix,
00371                          rows,
00372                          cols,
00373                          false); // false means DO NOT REUSE submatrix
00374   }
00375 
00382   virtual void reinit_submatrix(SparseMatrix<T>& submatrix,
00383                                 const std::vector<numeric_index_type>& rows,
00384                                 const std::vector<numeric_index_type>& cols) const
00385   {
00386     this->_get_submatrix(submatrix,
00387                          rows,
00388                          cols,
00389                          true); // true means REUSE submatrix
00390   }
00391 
00396   void vector_mult (NumericVector<T>& dest,
00397                     const NumericVector<T>& arg) const;
00398 
00402   void vector_mult_add (NumericVector<T>& dest,
00403                         const NumericVector<T>& arg) const;
00404 
00408   virtual void get_diagonal (NumericVector<T>& dest) const = 0;
00409 
00414   virtual void get_transpose (SparseMatrix<T>& dest) const = 0;
00415 
00416 protected:
00417 
00423   virtual void _get_submatrix(SparseMatrix<T>& ,
00424                               const std::vector<numeric_index_type>& ,
00425                               const std::vector<numeric_index_type>& ,
00426                               const bool) const
00427   {
00428     libmesh_not_implemented();
00429   }
00430 
00434   DofMap const *_dof_map;
00435 
00440   bool _is_initialized;
00441 };
00442 
00443 
00444 
00445 //-----------------------------------------------------------------------
00446 // SparseMatrix inline members
00447 
00448 // For SGI MIPSpro this implementation must occur after
00449 // the full specialization of the print() member.
00450 //
00451 // It's generally easier to define these friend functions in the header
00452 // file.
00453 template <typename T>
00454 std::ostream& operator << (std::ostream& os, const SparseMatrix<T>& m)
00455 {
00456   m.print(os);
00457   return os;
00458 }
00459 
00460 
00461 } // namespace libMesh
00462 
00463 
00464 #endif // LIBMESH_SPARSE_MATRIX_H