include/deal.II/lac/chunk_sparsity_pattern.h

00001 //---------------------------------------------------------------------------
00002 //    @f$Id: chunk_sparsity_pattern.h 25345 2012-03-31 08:37:04Z bangerth @f$
00003 //
00004 //    Copyright (C) 2008, 2012 by the deal.II authors
00005 //
00006 //    This file is subject to QPL and may not be  distributed
00007 //    without copyright and license information. Please refer
00008 //    to the file deal.II/doc/license.html for the  text  and
00009 //    further information on this license.
00010 //
00011 //---------------------------------------------------------------------------
00012 #ifndef __deal2__chunk_sparsity_pattern_h
00013 #define __deal2__chunk_sparsity_pattern_h
00014 
00015 
00016 #include <deal.II/base/config.h>
00017 #include <deal.II/base/exceptions.h>
00018 #include <deal.II/base/subscriptor.h>
00019 #include <deal.II/base/vector_slice.h>
00020 
00021 #include <deal.II/lac/sparsity_pattern.h>
00022 
00023 #include <vector>
00024 #include <iostream>
00025 
00026 DEAL_II_NAMESPACE_OPEN
00027 
00028 
00029 template <typename> class ChunkSparseMatrix;
00030 
00031 
00046 class ChunkSparsityPattern : public Subscriptor
00047 {
00048   public:
00049 
00074     static const unsigned int invalid_entry = SparsityPattern::invalid_entry;
00075 
00086     ChunkSparsityPattern ();
00087 
00119     ChunkSparsityPattern (const ChunkSparsityPattern &);
00120 
00136     ChunkSparsityPattern (const unsigned int m,
00137                           const unsigned int n,
00138                           const unsigned int max_chunks_per_row,
00139                           const unsigned int chunk_size,
00140                           const bool optimize_diagonal = true);
00141 
00160     ChunkSparsityPattern (const unsigned int               m,
00161                           const unsigned int               n,
00162                           const std::vector<unsigned int>& row_lengths,
00163                           const unsigned int chunk_size,
00164                           const bool optimize_diagonal = true);
00165 
00179     ChunkSparsityPattern (const unsigned int n,
00180                           const unsigned int max_per_row,
00181                           const unsigned int chunk_size);
00182 
00197     ChunkSparsityPattern (const unsigned int               m,
00198                           const std::vector<unsigned int>& row_lengths,
00199                           const unsigned int               chunk_size,
00200                           const bool optimize_diagonal = true);
00201 
00205     ~ChunkSparsityPattern ();
00206 
00215     ChunkSparsityPattern & operator = (const ChunkSparsityPattern &);
00216 
00228     void reinit (const unsigned int m,
00229                  const unsigned int n,
00230                  const unsigned int max_per_row,
00231                  const unsigned int chunk_size,
00232                  const bool optimize_diagonal = true);
00233 
00259     void reinit (const unsigned int               m,
00260                  const unsigned int               n,
00261                  const std::vector<unsigned int> &row_lengths,
00262                  const unsigned int chunk_size,
00263                  const bool optimize_diagonal = true);
00264 
00269     void reinit (const unsigned int               m,
00270                  const unsigned int               n,
00271                  const VectorSlice<const std::vector<unsigned int> > &row_lengths,
00272                  const unsigned int chunk_size,
00273                  const bool optimize_diagonal = true);
00274 
00295     void compress ();
00296 
00435     template <typename ForwardIterator>
00436     void copy_from (const unsigned int    n_rows,
00437                     const unsigned int    n_cols,
00438                     const ForwardIterator begin,
00439                     const ForwardIterator end,
00440                     const unsigned int chunk_size,
00441                     const bool optimize_diagonal = true);
00442 
00452     template <typename SparsityType>
00453     void copy_from (const SparsityType &csp,
00454                     const unsigned int  chunk_size,
00455                     const bool          optimize_diagonal = true);
00456 
00468     template <typename number>
00469     void copy_from (const FullMatrix<number> &matrix,
00470                     const unsigned int chunk_size,
00471                     const bool optimize_diagonal = true);
00472 
00479     bool empty () const;
00480 
00486     unsigned int get_chunk_size () const;
00487 
00496     unsigned int max_entries_per_row () const;
00497 
00506     void add (const unsigned int i,
00507               const unsigned int j);
00508 
00520     void symmetrize ();
00521 
00527     inline unsigned int n_rows () const;
00528 
00534     inline unsigned int n_cols () const;
00535 
00540     bool exists (const unsigned int i,
00541                  const unsigned int j) const;
00542 
00546     unsigned int row_length (const unsigned int row) const;
00547 
00558     unsigned int bandwidth () const;
00559 
00572     unsigned int n_nonzero_elements () const;
00573 
00578     bool is_compressed () const;
00579 
00614     bool optimize_diagonal () const;
00615 
00639     bool stores_only_added_elements () const;
00640 
00661     void block_write (std::ostream &out) const;
00662 
00686     void block_read (std::istream &in);
00687 
00697     void print (std::ostream &out) const;
00698 
00723     void print_gnuplot (std::ostream &out) const;
00724 
00731     std::size_t memory_consumption () const;
00732 
00738     DeclException1 (ExcInvalidNumber,
00739                     int,
00740                     << "The provided number is invalid here: " << arg1);
00744     DeclException2 (ExcInvalidIndex,
00745                     int, int,
00746                     << "The given index " << arg1
00747                     << " should be less than " << arg2 << ".");
00751     DeclException2 (ExcNotEnoughSpace,
00752                     int, int,
00753                     << "Upon entering a new entry to row " << arg1
00754                     << ": there was no free entry any more. " << std::endl
00755                     << "(Maximum number of entries for this row: "
00756                     << arg2 << "; maybe the matrix is already compressed?)");
00760     DeclException0 (ExcNotCompressed);
00764     DeclException0 (ExcMatrixIsCompressed);
00768     DeclException0 (ExcEmptyObject);
00772     DeclException0 (ExcInvalidConstructorCall);
00782     DeclException0 (ExcDiagonalNotOptimized);
00786     DeclException2 (ExcIteratorRange,
00787                     int, int,
00788                     << "The iterators denote a range of " << arg1
00789                     << " elements, but the given number of rows was " << arg2);
00793     DeclException0 (ExcMETISNotInstalled);
00797     DeclException1 (ExcInvalidNumberOfPartitions,
00798                     int,
00799                     << "The number of partitions you gave is " << arg1
00800                     << ", but must be greater than zero.");
00804     DeclException2 (ExcInvalidArraySize,
00805                     int, int,
00806                     << "The array has size " << arg1 << " but should have size "
00807                     << arg2);
00809   private:
00814     unsigned int rows;
00815 
00820     unsigned int cols;
00821 
00825     unsigned int chunk_size;
00826 
00833     SparsityPattern sparsity_pattern;
00834 
00839     template <typename> friend class ChunkSparseMatrix;
00840 };
00841 
00842 
00844 /*---------------------- Inline functions -----------------------------------*/
00845 
00846 #ifndef DOXYGEN
00847 
00848 
00849 inline
00850 unsigned int
00851 ChunkSparsityPattern::n_rows () const
00852 {
00853   return rows;
00854 }
00855 
00856 
00857 inline
00858 unsigned int
00859 ChunkSparsityPattern::n_cols () const
00860 {
00861   return cols;
00862 }
00863 
00864 
00865 
00866 inline
00867 unsigned int
00868 ChunkSparsityPattern::get_chunk_size () const
00869 {
00870   return chunk_size;
00871 }
00872 
00873 
00874 
00875 inline
00876 bool
00877 ChunkSparsityPattern::is_compressed () const
00878 {
00879   return sparsity_pattern.compressed;
00880 }
00881 
00882 
00883 inline
00884 bool
00885 ChunkSparsityPattern::optimize_diagonal () const
00886 {
00887   return sparsity_pattern.diagonal_optimized;
00888 }
00889 
00890 
00891 
00892 template <typename ForwardIterator>
00893 void
00894 ChunkSparsityPattern::copy_from (const unsigned int    n_rows,
00895                                  const unsigned int    n_cols,
00896                                  const ForwardIterator begin,
00897                                  const ForwardIterator end,
00898                                  const unsigned int    chunk_size,
00899                                  const bool optimize_diag)
00900 {
00901   Assert (static_cast<unsigned int>(std::distance (begin, end)) == n_rows,
00902           ExcIteratorRange (std::distance (begin, end), n_rows));
00903 
00904                                    // first determine row lengths for
00905                                    // each row. if the matrix is
00906                                    // quadratic, then we might have to
00907                                    // add an additional entry for the
00908                                    // diagonal, if that is not yet
00909                                    // present. as we have to call
00910                                    // compress anyway later on, don't
00911                                    // bother to check whether that
00912                                    // diagonal entry is in a certain
00913                                    // row or not
00914   const bool is_square = optimize_diag && (n_rows == n_cols);
00915   std::vector<unsigned int> row_lengths;
00916   row_lengths.reserve(n_rows);
00917   for (ForwardIterator i=begin; i!=end; ++i)
00918     row_lengths.push_back (std::distance (i->begin(), i->end())
00919                            +
00920                            (is_square ? 1 : 0));
00921   reinit (n_rows, n_cols, row_lengths, chunk_size, is_square);
00922 
00923                                    // now enter all the elements into
00924                                    // the matrix
00925   unsigned int row = 0;
00926   typedef typename std::iterator_traits<ForwardIterator>::value_type::const_iterator inner_iterator;
00927   for (ForwardIterator i=begin; i!=end; ++i, ++row)
00928     {
00929       const inner_iterator end_of_row = i->end();
00930       for (inner_iterator j=i->begin(); j!=end_of_row; ++j)
00931         {
00932           const unsigned int col
00933             = internal::SparsityPatternTools::get_column_index_from_iterator(*j);
00934           Assert (col < n_cols, ExcInvalidIndex(col,n_cols));
00935 
00936           add (row, col);
00937         }
00938     }
00939 
00940                                    // finally compress
00941                                    // everything. this also sorts the
00942                                    // entries within each row
00943   compress ();
00944 }
00945 
00946 
00947 #endif // DOXYGEN
00948 
00949 DEAL_II_NAMESPACE_CLOSE
00950 
00951 #endif
00952 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

deal.II documentation generated on Thu May 17 2012 20:04:40 by doxygen 1.7.3