From f2bc83b536c27321e433b5e57a4d5c185474804c Mon Sep 17 00:00:00 2001 From: wolf Date: Fri, 1 Dec 2000 13:11:32 +0000 Subject: [PATCH] Purge the block size template args from BlockSparseMatrix as well. git-svn-id: https://svn.dealii.org/trunk@3519 0785d39b-7218-0410-832d-ea1e28bc413d --- .../deal.II/include/dofs/dof_constraints.h | 6 +- .../include/dofs/dof_constraints.templates.h | 8 +- deal.II/deal.II/include/numerics/matrices.h | 7 +- .../deal.II/source/dofs/dof_constraints.cc | 6 +- deal.II/deal.II/source/numerics/matrices.cc | 22 +- deal.II/lac/include/lac/block_sparse_matrix.h | 244 ++++++++++-------- .../lac/block_sparse_matrix.templates.h | 96 +++++-- .../lac/include/lac/block_sparsity_pattern.h | 2 +- deal.II/lac/source/block_sparse_matrix.cc | 16 +- tests/lac/block_matrices.cc | 2 +- 10 files changed, 238 insertions(+), 171 deletions(-) diff --git a/deal.II/deal.II/include/dofs/dof_constraints.h b/deal.II/deal.II/include/dofs/dof_constraints.h index 743b48809c..c713c04821 100644 --- a/deal.II/deal.II/include/dofs/dof_constraints.h +++ b/deal.II/deal.II/include/dofs/dof_constraints.h @@ -22,7 +22,7 @@ class SparsityPattern; class BlockSparsityPattern; template class SparseMatrix; -template class BlockSparseMatrix; +template class BlockSparseMatrix; class BlockIndices; /** @@ -287,8 +287,8 @@ class ConstraintMatrix : public Subscriptor * condenses square block sparse * matrices. */ - template - void condense (BlockSparseMatrix &sparsity) const; + template + void condense (BlockSparseMatrix &matrix) const; /** * Condense the given vector @p{uncondensed} diff --git a/deal.II/deal.II/include/dofs/dof_constraints.templates.h b/deal.II/deal.II/include/dofs/dof_constraints.templates.h index 20dcee477b..e82c027bbd 100644 --- a/deal.II/deal.II/include/dofs/dof_constraints.templates.h +++ b/deal.II/deal.II/include/dofs/dof_constraints.templates.h @@ -262,10 +262,12 @@ ConstraintMatrix::condense (SparseMatrix &uncondensed) const -template +template void -ConstraintMatrix::condense (BlockSparseMatrix &uncondensed) const +ConstraintMatrix::condense (BlockSparseMatrix &uncondensed) const { + const unsigned int blocks = uncondensed.n_block_rows(); + const BlockSparsityPattern & sparsity = uncondensed.get_sparsity_pattern (); @@ -275,6 +277,8 @@ ConstraintMatrix::condense (BlockSparseMatrix &uncondensed ExcMatrixNotSquare()); Assert (sparsity.n_block_rows() == sparsity.n_block_cols(), ExcMatrixNotSquare()); + Assert (sparsity.n_block_rows() == sparsity.n_block_cols(), + ExcMatrixNotSquare()); Assert (sparsity.get_column_indices() == sparsity.get_row_indices(), ExcMatrixNotSquare()); diff --git a/deal.II/deal.II/include/numerics/matrices.h b/deal.II/deal.II/include/numerics/matrices.h index f623e2ed71..c8be9c7cb2 100644 --- a/deal.II/deal.II/include/numerics/matrices.h +++ b/deal.II/deal.II/include/numerics/matrices.h @@ -23,7 +23,7 @@ template class Vector; template class FullMatrix; template class SparseMatrix; -template class BlockSparseMatrix; +template class BlockSparseMatrix; template class BlockVector; template class DoFHandler; @@ -457,13 +457,12 @@ class MatrixTools : public MatrixCreator * works for block sparse * matrices and block vectors */ - template static void apply_boundary_values (const map &boundary_values, - BlockSparseMatrix &matrix, + BlockSparseMatrix &matrix, BlockVector &solution, BlockVector &right_hand_side, - const bool eliminate_columns = true); + const bool eliminate_columns = true); /** * Exception diff --git a/deal.II/deal.II/source/dofs/dof_constraints.cc b/deal.II/deal.II/source/dofs/dof_constraints.cc index 44ef429305..07d40af788 100644 --- a/deal.II/deal.II/source/dofs/dof_constraints.cc +++ b/deal.II/deal.II/source/dofs/dof_constraints.cc @@ -689,11 +689,7 @@ matrix_functions_2; #undef MatrixType // block sparse matrices are only implemented for one of the two matrix functions -#define MatrixType BlockSparseMatrix -matrix_functions_2; -#undef MatrixType - -#define MatrixType BlockSparseMatrix +#define MatrixType BlockSparseMatrix matrix_functions_2; #undef MatrixType diff --git a/deal.II/deal.II/source/numerics/matrices.cc b/deal.II/deal.II/source/numerics/matrices.cc index 9b9172c178..499cce06de 100644 --- a/deal.II/deal.II/source/numerics/matrices.cc +++ b/deal.II/deal.II/source/numerics/matrices.cc @@ -685,20 +685,23 @@ MatrixTools::apply_boundary_values (const map &boundar template -template void MatrixTools::apply_boundary_values (const map &boundary_values, - BlockSparseMatrix &matrix, + BlockSparseMatrix &matrix, BlockVector &solution, BlockVector &right_hand_side, const bool preserve_symmetry) { + const unsigned int blocks = matrix.n_block_rows(); + Assert (matrix.n() == matrix.m(), ExcDimensionsDontMatch(matrix.n(), matrix.m())); Assert (matrix.n() == right_hand_side.size(), ExcDimensionsDontMatch(matrix.n(), right_hand_side.size())); Assert (matrix.n() == solution.size(), ExcDimensionsDontMatch(matrix.n(), solution.size())); + Assert (matrix.n_block_rows() == matrix.n_block_cols(), + ExcMatrixNotBlockSquare()); Assert (matrix.get_sparsity_pattern().get_row_indices() == matrix.get_sparsity_pattern().get_column_indices(), ExcMatrixNotBlockSquare()); @@ -1398,18 +1401,9 @@ template void MatrixTools:: apply_boundary_values (const map &, - BlockSparseMatrix &, - BlockVector &, - BlockVector &, - const bool); - -template -void -MatrixTools:: -apply_boundary_values (const map &, - BlockSparseMatrix &, - BlockVector &, - BlockVector &, + BlockSparseMatrix &, + BlockVector &, + BlockVector &, const bool); diff --git a/deal.II/lac/include/lac/block_sparse_matrix.h b/deal.II/lac/include/lac/block_sparse_matrix.h index 82bd776c51..c58032e848 100644 --- a/deal.II/lac/include/lac/block_sparse_matrix.h +++ b/deal.II/lac/include/lac/block_sparse_matrix.h @@ -35,6 +35,9 @@ template class BlockVector; * preconditioners, where each block belongs to a specific component * of the equation you are presently discretizing. * + * Note that the number of blocks and rows are implicitely determined + * by the sparsity pattern objects used. + * * * @sect2{On template instantiations} * @@ -49,7 +52,7 @@ template class BlockVector; * * @author Wolfgang Bangerth, 2000 */ -template +template class BlockSparseMatrix : public Subscriptor { public: @@ -60,19 +63,25 @@ class BlockSparseMatrix : public Subscriptor typedef number value_type; /** - * Constructor; initializes the matrix to - * be empty, without any structure, i.e. - * the matrix is not usable at all. This - * constructor is therefore only useful - * for matrices which are members of a - * class. All other matrices should be - * created at a point in the data flow - * where all necessary information is + * Constructor; initializes the + * matrix to be empty, without + * any structure, i.e. the + * matrix is not usable at + * all. This constructor is + * therefore only useful for + * matrices which are members of + * a class. All other matrices + * should be created at a point + * in the data flow where all + * necessary information is * available. * - * You have to initialize - * the matrix before usage with - * @p{reinit(BlockSparsityPattern)}. + * You have to initialize the + * matrix before usage with + * @p{reinit(BlockSparsityPattern)}. The + * number of blocks per row and + * column are then determined by + * that function. */ BlockSparseMatrix (); @@ -99,15 +108,21 @@ class BlockSparseMatrix : public Subscriptor */ BlockSparseMatrix (const BlockSparsityPattern &sparsity); + /** + * Destructor. + */ + virtual ~BlockSparseMatrix (); + /** * Pseudo operator only copying - * empty objects. + * empty objects. The sizes of + * the block matrices need to be + * the same. */ - BlockSparseMatrix & - operator = (const BlockSparseMatrix &); - + BlockSparseMatrix & + operator = (const BlockSparseMatrix &); /** * Reinitialize the object but @@ -116,8 +131,10 @@ class BlockSparseMatrix : public Subscriptor * necessary if you @p{reinit}'d * the sparsity structure and * want to update the size of the - * matrix. It only calls @p{reinit} - * on the sub-matrices. + * matrix. It only calls + * @p{reinit} on the + * sub-matrices. The size of this + * matrix is unchanged. * * If the sparsity pattern has * not changed, then the effect @@ -179,14 +196,18 @@ class BlockSparseMatrix : public Subscriptor virtual void clear (); /** - * Return the number of blocks in a - * column. + * Return the number of blocks in + * a column. Returns zero if no + * sparsity pattern is presently + * associated to this matrix. */ unsigned int n_block_rows () const; /** - * Return the number of blocks in a - * row. + * Return the number of blocks in + * a row. Returns zero if no + * sparsity pattern is presently + * associated to this matrix. */ unsigned int n_block_cols () const; @@ -276,8 +297,8 @@ class BlockSparseMatrix : public Subscriptor * reference to @p{this}. */ template - BlockSparseMatrix & - copy_from (const BlockSparseMatrix &source); + BlockSparseMatrix & + copy_from (const BlockSparseMatrix &source); /** * Add @p{matrix} scaled by @@ -296,7 +317,7 @@ class BlockSparseMatrix : public Subscriptor */ template void add_scaled (const number factor, - const BlockSparseMatrix &matrix); + const BlockSparseMatrix &matrix); /** * Return the value of the entry (i,j). @@ -307,7 +328,8 @@ class BlockSparseMatrix : public Subscriptor * throws an exception if the wanted * element does not exist in the matrix. */ - number operator () (const unsigned int i, const unsigned int j) const; + number operator () (const unsigned int i, + const unsigned int j) const; /** @@ -316,7 +338,7 @@ class BlockSparseMatrix : public Subscriptor * being this matrix. */ template - void vmult (BlockVector &dst, + void vmult (BlockVector &dst, const BlockVector &src) const; /** @@ -328,7 +350,7 @@ class BlockSparseMatrix : public Subscriptor * transposed matrix. */ template - void Tvmult (BlockVector &dst, + void Tvmult (BlockVector &dst, const BlockVector &src) const; /** @@ -338,7 +360,7 @@ class BlockSparseMatrix : public Subscriptor * matrix. */ template - void vmult_add (BlockVector &dst, + void vmult_add (BlockVector &dst, const BlockVector &src) const; /** @@ -350,7 +372,7 @@ class BlockSparseMatrix : public Subscriptor * the transposed matrix. */ template - void Tvmult_add (BlockVector &dst, + void Tvmult_add (BlockVector &dst, const BlockVector &src) const; /** @@ -386,7 +408,7 @@ class BlockSparseMatrix : public Subscriptor */ template somenumber - matrix_scalar_product (const BlockVector &u, + matrix_scalar_product (const BlockVector &u, const BlockVector &v) const; /** @@ -400,9 +422,9 @@ class BlockSparseMatrix : public Subscriptor * into @p{dst}. */ template - somenumber residual (BlockVector &dst, + somenumber residual (BlockVector &dst, const BlockVector &x, - const BlockVector &b) const; + const BlockVector &b) const; /** * Apply the Jacobi @@ -418,9 +440,9 @@ class BlockSparseMatrix : public Subscriptor * for this operation. */ template - void precondition_Jacobi (BlockVector &dst, + void precondition_Jacobi (BlockVector &dst, const BlockVector &src, - const number omega = 1.) const; + const number omega = 1.) const; /** * Return a (constant) reference @@ -448,8 +470,33 @@ class BlockSparseMatrix : public Subscriptor * Exception */ DeclException0 (ExcMatrixNotBlockSquare); + + /** + * Exception. + */ + DeclException0 (ExcIncompatibleObjects); private: + /** + * Number of block rows in this + * matrix. Is set by default to + * zero, and is only changed if a + * sparsity pattern is given to + * the constructor or the + * @p{reinit} function. + */ + unsigned int rows; + + /** + * Number of block columns in this + * matrix. Is set by default to + * zero, and is only changed if a + * sparsity pattern is given to + * the constructor or the + * @p{reinit} function. + */ + unsigned int columns; + /** * Pointer to the block sparsity * pattern used for this @@ -464,7 +511,7 @@ class BlockSparseMatrix : public Subscriptor /** * Array of sub-matrices. */ - SparseMatrix sub_objects[rows][columns]; + vector > > > sub_objects; }; @@ -473,60 +520,60 @@ class BlockSparseMatrix : public Subscriptor /* ------------------------- Template functions ---------------------- */ -template +template inline SparseMatrix & -BlockSparseMatrix::block (const unsigned int row, - const unsigned int column) +BlockSparseMatrix::block (const unsigned int row, + const unsigned int column) { Assert (row +template inline const SparseMatrix & -BlockSparseMatrix::block (const unsigned int row, - const unsigned int column) const +BlockSparseMatrix::block (const unsigned int row, + const unsigned int column) const { Assert (row +template inline unsigned int -BlockSparseMatrix::m () const +BlockSparseMatrix::m () const { return sparsity_pattern->n_rows(); }; -template +template inline unsigned int -BlockSparseMatrix::n () const +BlockSparseMatrix::n () const { return sparsity_pattern->n_cols(); }; -template +template inline void -BlockSparseMatrix::set (const unsigned int i, - const unsigned int j, - const number value) +BlockSparseMatrix::set (const unsigned int i, + const unsigned int j, + const number value) { const pair row_index = sparsity_pattern->row_indices.global_to_local (i), @@ -538,12 +585,12 @@ BlockSparseMatrix::set (const unsigned int i, -template +template inline void -BlockSparseMatrix::add (const unsigned int i, - const unsigned int j, - const number value) +BlockSparseMatrix::add (const unsigned int i, + const unsigned int j, + const number value) { const pair row_index = sparsity_pattern->row_indices.global_to_local (i), @@ -555,11 +602,11 @@ BlockSparseMatrix::add (const unsigned int i, -template +template inline number -BlockSparseMatrix::operator () (const unsigned int i, - const unsigned int j) const +BlockSparseMatrix::operator () (const unsigned int i, + const unsigned int j) const { const pair row_index = sparsity_pattern->row_indices.global_to_local (i), @@ -571,11 +618,11 @@ BlockSparseMatrix::operator () (const unsigned int i, -template +template template -BlockSparseMatrix & -BlockSparseMatrix:: -copy_from (const BlockSparseMatrix &source) +BlockSparseMatrix & +BlockSparseMatrix:: +copy_from (const BlockSparseMatrix &source) { for (unsigned int r=0; r &source) -template +template template void -BlockSparseMatrix:: +BlockSparseMatrix:: add_scaled (const number factor, - const BlockSparseMatrix &matrix) + const BlockSparseMatrix &matrix) { for (unsigned int r=0; r +template template void -BlockSparseMatrix:: -vmult (BlockVector &dst, - const BlockVector &src) const +BlockSparseMatrix::vmult (BlockVector &dst, + const BlockVector &src) const { Assert (dst.n_blocks() == rows, ExcDimensionMismatch(dst.n_blocks(), rows)); @@ -618,19 +664,18 @@ vmult (BlockVector &dst, block(row,0).vmult (dst.block(row), src.block(0)); for (unsigned int col=1; col +template template void -BlockSparseMatrix:: -vmult_add (BlockVector &dst, - const BlockVector &src) const +BlockSparseMatrix::vmult_add (BlockVector &dst, + const BlockVector &src) const { Assert (dst.n_blocks() == rows, ExcDimensionMismatch(dst.n_blocks(), rows)); @@ -642,20 +687,19 @@ vmult_add (BlockVector &dst, block(row,0).vmult_add (dst.block(row), src.block(0)); for (unsigned int col=1; col +template template void -BlockSparseMatrix:: -Tvmult (BlockVector &/*dst*/, - const BlockVector &/*src*/) const +BlockSparseMatrix::Tvmult (BlockVector &/*dst*/, + const BlockVector &/*src*/) const { // presently not // implemented. should be simple, @@ -666,12 +710,11 @@ Tvmult (BlockVector &/*dst*/, -template +template template void -BlockSparseMatrix:: -Tvmult_add (BlockVector &/*dst*/, - const BlockVector &/*src*/) const +BlockSparseMatrix::Tvmult_add (BlockVector &/*dst*/, + const BlockVector &/*src*/) const { // presently not // implemented. should be simple, @@ -682,11 +725,10 @@ Tvmult_add (BlockVector &/*dst*/, -template +template template somenumber -BlockSparseMatrix:: -matrix_norm_square (const BlockVector &v) const +BlockSparseMatrix::matrix_norm_square (const BlockVector &v) const { Assert (rows == columns, ExcMatrixNotBlockSquare()); Assert (v.n_blocks() == rows, @@ -705,10 +747,10 @@ matrix_norm_square (const BlockVector &v) const -template +template template somenumber -BlockSparseMatrix:: +BlockSparseMatrix:: matrix_scalar_product (const BlockVector &u, const BlockVector &v) const { @@ -727,10 +769,10 @@ matrix_scalar_product (const BlockVector &u, -template +template template somenumber -BlockSparseMatrix:: +BlockSparseMatrix:: residual (BlockVector &dst, const BlockVector &x, const BlockVector &b) const @@ -780,10 +822,10 @@ residual (BlockVector &dst, -template +template template void -BlockSparseMatrix:: +BlockSparseMatrix:: precondition_Jacobi (BlockVector &dst, const BlockVector &src, const number omega) const @@ -801,23 +843,23 @@ precondition_Jacobi (BlockVector &dst, }; -template +template inline unsigned int -BlockSparseMatrix::n_block_cols () const +BlockSparseMatrix::n_block_cols () const { return columns; -} +}; -template +template inline unsigned int -BlockSparseMatrix::n_block_rows () const +BlockSparseMatrix::n_block_rows () const { return rows; -} +}; #endif // __deal2__block_sparse_matrix_h diff --git a/deal.II/lac/include/lac/block_sparse_matrix.templates.h b/deal.II/lac/include/lac/block_sparse_matrix.templates.h index 52abe1a3df..672c1c3e6b 100644 --- a/deal.II/lac/include/lac/block_sparse_matrix.templates.h +++ b/deal.II/lac/include/lac/block_sparse_matrix.templates.h @@ -19,27 +19,49 @@ -template -BlockSparseMatrix::BlockSparseMatrix () : +template +BlockSparseMatrix::BlockSparseMatrix () : + rows (0), + columns (0), sparsity_pattern (0) {}; -template -BlockSparseMatrix:: -BlockSparseMatrix (const BlockSparsityPattern &sparsity) +template +BlockSparseMatrix:: +BlockSparseMatrix (const BlockSparsityPattern &sparsity) : + rows (0), + columns (0) { reinit (sparsity); }; -template -BlockSparseMatrix & -BlockSparseMatrix:: -operator = (const BlockSparseMatrix &m) +template +BlockSparseMatrix::~BlockSparseMatrix () { + // delete previous content of + // the subobjects array + for (unsigned int r=0; r *p = sub_objects[r][c]; + sub_objects[r][c] = 0; + delete p; + }; +}; + + + +template +BlockSparseMatrix & +BlockSparseMatrix:: +operator = (const BlockSparseMatrix &m) +{ + Assert (rows == m.rows, ExcIncompatibleObjects()); + Assert (columns == m.columns, ExcIncompatibleObjects()); // this operator does not do // anything except than checking // whether the base objects want to @@ -52,9 +74,9 @@ operator = (const BlockSparseMatrix &m) -template +template void -BlockSparseMatrix::reinit () +BlockSparseMatrix::reinit () { for (unsigned int r=0; r::reinit () -template +template void -BlockSparseMatrix:: +BlockSparseMatrix:: reinit (const BlockSparsityPattern &sparsity) { + // first delete previous content of + // the subobjects array + for (unsigned int r=0; r *p = sub_objects[r][c]; + sub_objects[r][c] = 0; + delete p; + }; + sub_objects.clear (); + + // then associate new sparsity + // pattern and resize sparsity_pattern = &sparsity; - + rows = sparsity.n_block_rows(); + columns = sparsity.n_block_cols(); + sub_objects = vector > > > + (rows, vector > > (columns, 0)); + + // and reinitialize the blocks for (unsigned int r=0; r(); + block(r,c).reinit (sparsity.block(r,c)); + }; }; -template +template void -BlockSparseMatrix::clear () +BlockSparseMatrix::clear () { sparsity_pattern = 0; for (unsigned int r=0; r::clear () -template +template bool -BlockSparseMatrix::empty () const +BlockSparseMatrix::empty () const { for (unsigned int r=0; r::empty () const -template +template unsigned int -BlockSparseMatrix::n_nonzero_elements () const +BlockSparseMatrix::n_nonzero_elements () const { return sparsity_pattern->n_nonzero_elements (); }; -template +template const BlockSparsityPattern & -BlockSparseMatrix::get_sparsity_pattern () const +BlockSparseMatrix::get_sparsity_pattern () const { return *sparsity_pattern; }; -template +template unsigned int -BlockSparseMatrix::memory_consumption () const +BlockSparseMatrix::memory_consumption () const { unsigned int mem = sizeof(*this); + mem += MemoryConsumption::memory_consumption (sub_objects); for (unsigned int r=0; r + template friend class BlockSparseMatrix; }; diff --git a/deal.II/lac/source/block_sparse_matrix.cc b/deal.II/lac/source/block_sparse_matrix.cc index 504d7c5bfc..6be484a433 100644 --- a/deal.II/lac/source/block_sparse_matrix.cc +++ b/deal.II/lac/source/block_sparse_matrix.cc @@ -14,17 +14,5 @@ #include // explicit instantiations -template class BlockSparseMatrix; -template class BlockSparseMatrix; - -template class BlockSparseMatrix; -template class BlockSparseMatrix; - - - -template class BlockSparseMatrix; -template class BlockSparseMatrix; - -template class BlockSparseMatrix; -template class BlockSparseMatrix; - +template class BlockSparseMatrix; +template class BlockSparseMatrix; diff --git a/tests/lac/block_matrices.cc b/tests/lac/block_matrices.cc index dee86931f3..acb0600740 100644 --- a/tests/lac/block_matrices.cc +++ b/tests/lac/block_matrices.cc @@ -124,7 +124,7 @@ void test () // now make a matrix with this // sparsity pattern - BlockSparseMatrix bsm (bsp); + BlockSparseMatrix bsm (bsp); deallog << total_nonzero_elements << "==" << bsm.n_nonzero_elements() << endl; -- 2.39.5