sparse_matrix.h
Go to the documentation of this file.00001 // The libMesh Finite Element Library. 00002 // Copyright (C) 2002-2012 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 00031 // C++ includes 00032 #include <cstddef> 00033 #include <iomanip> 00034 #include <vector> 00035 00036 namespace libMesh 00037 { 00038 00039 // forward declarations 00040 template <typename T> class SparseMatrix; 00041 template <typename T> class DenseMatrix; 00042 class DofMap; 00043 namespace SparsityPattern { class Graph; } 00044 template <typename T> class NumericVector; 00045 00046 // This template helper function must be declared before it 00047 // can be defined below. 00048 template <typename T> 00049 std::ostream& operator << (std::ostream& os, const SparseMatrix<T>& m); 00050 00051 00063 template <typename T> 00064 class SparseMatrix : public ReferenceCountedObject<SparseMatrix<T> > 00065 { 00066 public: 00082 SparseMatrix (); 00083 00089 virtual ~SparseMatrix (); 00090 00095 static AutoPtr<SparseMatrix<T> > 00096 build(const SolverPackage solver_package = libMesh::default_solver_package()); 00097 00102 virtual bool initialized() const { return _is_initialized; } 00103 00107 void attach_dof_map (const DofMap& dof_map) 00108 { _dof_map = &dof_map; } 00109 00117 virtual bool need_full_sparsity_pattern() const 00118 { return false; } 00119 00125 virtual void update_sparsity_pattern (const SparsityPattern::Graph &) {} 00126 00135 virtual void init (const numeric_index_type m, 00136 const numeric_index_type n, 00137 const numeric_index_type m_l, 00138 const numeric_index_type n_l, 00139 const numeric_index_type nnz=30, 00140 const numeric_index_type noz=10) = 0; 00141 00145 virtual void init () = 0; 00146 00153 virtual void clear () = 0; 00154 00158 virtual void zero () = 0; 00159 00163 virtual void zero_rows (std::vector<numeric_index_type> & rows, T diag_value = 0.0); 00164 00170 virtual void close () const = 0; 00171 00176 virtual numeric_index_type m () const = 0; 00177 00182 virtual numeric_index_type n () const = 0; 00183 00188 virtual numeric_index_type row_start () const = 0; 00189 00194 virtual numeric_index_type row_stop () const = 0; 00195 00202 virtual void set (const numeric_index_type i, 00203 const numeric_index_type j, 00204 const T value) = 0; 00205 00214 virtual void add (const numeric_index_type i, 00215 const numeric_index_type j, 00216 const T value) = 0; 00217 00224 virtual void add_matrix (const DenseMatrix<T> &dm, 00225 const std::vector<numeric_index_type> &rows, 00226 const std::vector<numeric_index_type> &cols) = 0; 00227 00232 virtual void add_matrix (const DenseMatrix<T> &dm, 00233 const std::vector<numeric_index_type> &dof_indices) = 0; 00234 00240 virtual void add (const T, SparseMatrix<T> &) = 0; 00241 00249 virtual T operator () (const numeric_index_type i, 00250 const numeric_index_type j) const = 0; 00251 00262 virtual Real l1_norm () const = 0; 00263 00275 virtual Real linfty_norm () const = 0; 00276 00281 virtual bool closed() const = 0; 00282 00288 void print(std::ostream& os=libMesh::out, const bool sparse=false) const; 00289 00315 friend std::ostream& operator << <>(std::ostream& os, const SparseMatrix<T>& m); 00316 00321 virtual void print_personal(std::ostream& os=libMesh::out) const = 0; 00322 00329 virtual void print_matlab(const std::string name="NULL") const 00330 { 00331 libMesh::err << "ERROR: Not Implemented in base class yet!" << std::endl; 00332 libMesh::err << "ERROR writing MATLAB file " << name << std::endl; 00333 libmesh_error(); 00334 } 00335 00341 virtual void create_submatrix(SparseMatrix<T>& submatrix, 00342 const std::vector<numeric_index_type>& rows, 00343 const std::vector<numeric_index_type>& cols) const 00344 { 00345 this->_get_submatrix(submatrix, 00346 rows, 00347 cols, 00348 false); // false means DO NOT REUSE submatrix 00349 } 00350 00357 virtual void reinit_submatrix(SparseMatrix<T>& submatrix, 00358 const std::vector<numeric_index_type>& rows, 00359 const std::vector<numeric_index_type>& cols) const 00360 { 00361 this->_get_submatrix(submatrix, 00362 rows, 00363 cols, 00364 true); // true means REUSE submatrix 00365 } 00366 00371 void vector_mult (NumericVector<T>& dest, 00372 const NumericVector<T>& arg) const; 00373 00377 void vector_mult_add (NumericVector<T>& dest, 00378 const NumericVector<T>& arg) const; 00379 00383 virtual void get_diagonal (NumericVector<T>& dest) const = 0; 00384 00389 virtual void get_transpose (SparseMatrix<T>& dest) const = 0; 00390 00391 protected: 00392 00398 virtual void _get_submatrix(SparseMatrix<T>& , 00399 const std::vector<numeric_index_type>& , 00400 const std::vector<numeric_index_type>& , 00401 const bool) const 00402 { 00403 libMesh::err << "Error! This function is not yet implemented in the base class!" 00404 << std::endl; 00405 libmesh_error(); 00406 } 00407 00411 DofMap const *_dof_map; 00412 00417 bool _is_initialized; 00418 }; 00419 00420 00421 00422 //----------------------------------------------------------------------- 00423 // SparseMatrix inline members 00424 00425 // For SGI MIPSpro this implementation must occur after 00426 // the full specialization of the print() member. 00427 // 00428 // It's generally easier to define these friend functions in the header 00429 // file. 00430 template <typename T> 00431 std::ostream& operator << (std::ostream& os, const SparseMatrix<T>& m) 00432 { 00433 m.print(os); 00434 return os; 00435 } 00436 00437 00438 } // namespace libMesh 00439 00440 00441 #endif // LIBMESH_SPARSE_MATRIX_H
Site Created By: libMesh Developers
Last modified: February 05 2013 19:54:48 UTC
Hosted By: