Reference documentation for deal.II version 8.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
block_sparse_matrix.h
1 // ---------------------------------------------------------------------
2 // @f$Id: block_sparse_matrix.h 31932 2013-12-08 02:15:54Z heister @f$
3 //
4 // Copyright (C) 2000 - 2013 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // The deal.II library is free software; you can use it, redistribute
9 // it, and/or modify it under the terms of the GNU Lesser General
10 // Public License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 // The full text of the license can be found in the file LICENSE at
13 // the top level of the deal.II distribution.
14 //
15 // ---------------------------------------------------------------------
16 
17 #ifndef __deal2__block_sparse_matrix_h
18 #define __deal2__block_sparse_matrix_h
19 
20 
21 #include <deal.II/base/config.h>
22 #include <deal.II/base/table.h>
23 #include <deal.II/lac/block_matrix_base.h>
24 #include <deal.II/lac/block_vector.h>
25 #include <deal.II/lac/sparse_matrix.h>
26 #include <deal.II/lac/block_sparsity_pattern.h>
27 #include <deal.II/lac/exceptions.h>
28 
29 #include <cmath>
30 
32 
33 
49 template <typename number>
50 class BlockSparseMatrix : public BlockMatrixBase<SparseMatrix<number> >
51 {
52 public:
58 
63  typedef typename BaseClass::BlockType BlockType;
64 
70  typedef typename BaseClass::pointer pointer;
71  typedef typename BaseClass::const_pointer const_pointer;
72  typedef typename BaseClass::reference reference;
73  typedef typename BaseClass::const_reference const_reference;
74  typedef typename BaseClass::size_type size_type;
75  typedef typename BaseClass::iterator iterator;
77 
104 
126  BlockSparseMatrix (const BlockSparsityPattern &sparsity);
127 
131  virtual ~BlockSparseMatrix ();
132 
133 
134 
142 
159  operator = (const double d);
160 
173  void clear ();
174 
195  virtual void reinit (const BlockSparsityPattern &sparsity);
197 
209  bool empty () const;
210 
215  size_type get_row_length (const size_type row) const;
216 
226  size_type n_nonzero_elements () const;
227 
235  size_type n_actually_nonzero_elements (const double threshold = 0.0) const;
236 
249  const BlockSparsityPattern &
250  get_sparsity_pattern () const;
251 
257  std::size_t memory_consumption () const;
259 
269  template <typename block_number>
270  void vmult (BlockVector<block_number> &dst,
271  const BlockVector<block_number> &src) const;
272 
280  template <typename block_number,
281  typename nonblock_number>
282  void vmult (BlockVector<block_number> &dst,
283  const Vector<nonblock_number> &src) const;
284 
292  template <typename block_number,
293  typename nonblock_number>
294  void vmult (Vector<nonblock_number> &dst,
295  const BlockVector<block_number> &src) const;
296 
304  template <typename nonblock_number>
305  void vmult (Vector<nonblock_number> &dst,
306  const Vector<nonblock_number> &src) const;
307 
316  template <typename block_number>
318  const BlockVector<block_number> &src) const;
319 
327  template <typename block_number,
328  typename nonblock_number>
330  const Vector<nonblock_number> &src) const;
331 
339  template <typename block_number,
340  typename nonblock_number>
341  void Tvmult (Vector<nonblock_number> &dst,
342  const BlockVector<block_number> &src) const;
343 
351  template <typename nonblock_number>
352  void Tvmult (Vector<nonblock_number> &dst,
353  const Vector<nonblock_number> &src) const;
355 
375  template <class BlockVectorType>
376  void precondition_Jacobi (BlockVectorType &dst,
377  const BlockVectorType &src,
378  const number omega = 1.) const;
379 
387  template <typename number2>
389  const Vector<number2> &src,
390  const number omega = 1.) const;
392 
438  void print_formatted (std::ostream &out,
439  const unsigned int precision = 3,
440  const bool scientific = true,
441  const unsigned int width = 0,
442  const char *zero_string = " ",
443  const double denominator = 1.) const;
445 
451  DeclException0 (ExcBlockDimensionMismatch);
453 
454 private:
465 };
466 
467 
468 
470 /* ------------------------- Template functions ---------------------- */
471 
472 
473 
474 template <typename number>
475 inline
478 {
480 
481  for (size_type r=0; r<this->n_block_rows(); ++r)
482  for (size_type c=0; c<this->n_block_cols(); ++c)
483  this->block(r,c) = d;
484 
485  return *this;
486 }
487 
488 
489 
490 template <typename number>
491 template <typename block_number>
492 inline
493 void
495  const BlockVector<block_number> &src) const
496 {
497  BaseClass::vmult_block_block (dst, src);
498 }
499 
500 
501 
502 template <typename number>
503 template <typename block_number,
504  typename nonblock_number>
505 inline
506 void
508  const Vector<nonblock_number> &src) const
509 {
510  BaseClass::vmult_block_nonblock (dst, src);
511 }
512 
513 
514 
515 template <typename number>
516 template <typename block_number,
517  typename nonblock_number>
518 inline
519 void
521  const BlockVector<block_number> &src) const
522 {
523  BaseClass::vmult_nonblock_block (dst, src);
524 }
525 
526 
527 
528 template <typename number>
529 template <typename nonblock_number>
530 inline
531 void
533  const Vector<nonblock_number> &src) const
534 {
535  BaseClass::vmult_nonblock_nonblock (dst, src);
536 }
537 
538 
539 
540 template <typename number>
541 template <typename block_number>
542 inline
543 void
545  const BlockVector<block_number> &src) const
546 {
547  BaseClass::Tvmult_block_block (dst, src);
548 }
549 
550 
551 
552 template <typename number>
553 template <typename block_number,
554  typename nonblock_number>
555 inline
556 void
558  const Vector<nonblock_number> &src) const
559 {
560  BaseClass::Tvmult_block_nonblock (dst, src);
561 }
562 
563 
564 
565 template <typename number>
566 template <typename block_number,
567  typename nonblock_number>
568 inline
569 void
571  const BlockVector<block_number> &src) const
572 {
573  BaseClass::Tvmult_nonblock_block (dst, src);
574 }
575 
576 
577 
578 template <typename number>
579 template <typename nonblock_number>
580 inline
581 void
583  const Vector<nonblock_number> &src) const
584 {
585  BaseClass::Tvmult_nonblock_nonblock (dst, src);
586 }
587 
588 
589 
590 template <typename number>
591 template <class BlockVectorType>
592 inline
593 void
595 precondition_Jacobi (BlockVectorType &dst,
596  const BlockVectorType &src,
597  const number omega) const
598 {
599  Assert (this->n_block_rows() == this->n_block_cols(), ExcNotQuadratic());
600  Assert (dst.n_blocks() == this->n_block_rows(),
601  ExcDimensionMismatch(dst.n_blocks(), this->n_block_rows()));
602  Assert (src.n_blocks() == this->n_block_cols(),
603  ExcDimensionMismatch(src.n_blocks(), this->n_block_cols()));
604 
605  // do a diagonal preconditioning. uses only
606  // the diagonal blocks of the matrix
607  for (size_type i=0; i<this->n_block_rows(); ++i)
608  this->block(i,i).precondition_Jacobi (dst.block(i),
609  src.block(i),
610  omega);
611 }
612 
613 
614 
615 template <typename number>
616 template <typename number2>
617 inline
618 void
621  const Vector<number2> &src,
622  const number omega) const
623 {
624  // check number of blocks. the sizes of the
625  // single block is checked in the function
626  // we call
627  Assert (this->n_block_cols() == 1,
628  ExcMessage ("This function only works if the matrix has "
629  "a single block"));
630  Assert (this->n_block_rows() == 1,
631  ExcMessage ("This function only works if the matrix has "
632  "a single block"));
633 
634  // do a diagonal preconditioning. uses only
635  // the diagonal blocks of the matrix
636  this->block(0,0).precondition_Jacobi (dst, src, omega);
637 }
638 
639 
640 DEAL_II_NAMESPACE_CLOSE
641 
642 #endif // __deal2__block_sparse_matrix_h
virtual void reinit(const BlockSparsityPattern &sparsity)
SmartPointer< const BlockSparsityPattern, BlockSparseMatrix< number > > sparsity_pattern
BlockMatrixBase< SparseMatrix< number > > BaseClass
size_type n_nonzero_elements() const
BlockSparseMatrix & operator=(const BlockSparseMatrix &)
::ExceptionBase & ExcMessage(std::string arg1)
std::size_t memory_consumption() const
DeclException0(ExcBlockDimensionMismatch)
size_type get_row_length(const size_type row) const
#define Assert(cond, exc)
Definition: exceptions.h:299
BaseClass::BlockType BlockType
void print_formatted(std::ostream &out, const unsigned int precision=3, const bool scientific=true, const unsigned int width=0, const char *zero_string=" ", const double denominator=1.) const
const BlockSparsityPattern & get_sparsity_pattern() const
void Tvmult(BlockVector< block_number > &dst, const BlockVector< block_number > &src) const
BaseClass::value_type value_type
void precondition_Jacobi(BlockVectorType &dst, const BlockVectorType &src, const number omega=1.) const
::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
::ExceptionBase & ExcScalarAssignmentOnlyForZeroValue()
size_type n_actually_nonzero_elements(const double threshold=0.0) const
void vmult(BlockVector< block_number > &dst, const BlockVector< block_number > &src) const