Reference documentation for deal.II version 8.1.0
block_matrix_array.h
1 // ---------------------------------------------------------------------
2 // @f$Id: block_matrix_array.h 30036 2013-07-18 16:55:32Z maier @f$
3 //
4 // Copyright (C) 2001 - 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_matrix_array_h
18 #define __deal2__block_matrix_array_h
19 
20 #include <deal.II/base/config.h>
21 #include <deal.II/base/subscriptor.h>
22 #include <deal.II/base/table.h>
23 
24 #include <deal.II/lac/pointer_matrix.h>
25 #include <deal.II/lac/vector_memory.h>
26 
27 #include <vector>
28 #include <map>
29 #include <string>
30 #include <memory>
31 #include <sstream>
32 
34 
35 template <typename> class BlockVector;
36 template <typename> class Vector;
37 
127 template <typename number = double>
128 class BlockMatrixArray : public Subscriptor
129 {
130 public:
135 
142  BlockMatrixArray ();
143 
148  BlockMatrixArray (const unsigned int n_block_rows,
149  const unsigned int n_block_cols);
150 
158  void initialize (const unsigned int n_block_rows,
159  const unsigned int n_block_cols);
160 
168  BlockMatrixArray (const unsigned int n_block_rows,
169  const unsigned int n_block_cols,
171 
182  void initialize (const unsigned int n_block_rows,
183  const unsigned int n_block_cols,
184  VectorMemory<Vector<number> > &mem) DEAL_II_DEPRECATED;
185 
190  void reinit (const unsigned int n_block_rows,
191  const unsigned int n_block_cols);
192 
212  template <class MATRIX>
213  void enter (const MATRIX &matrix,
214  const unsigned int row,
215  const unsigned int col,
216  const double prefix = 1.,
217  const bool transpose = false);
218 
229  template <class MATRIX>
230  void enter_aux (VectorMemory<Vector<number> > &mem,
231  const MATRIX &matrix,
232  const unsigned int row,
233  const unsigned int col,
234  const double prefix = 1.,
235  const bool transpose = false) DEAL_II_DEPRECATED;
236 
237 
242  void clear();
243 
248  unsigned int n_block_rows () const;
249 
254  unsigned int n_block_cols () const;
255 
259  void vmult (BlockVector<number> &dst,
260  const BlockVector<number> &src) const;
261 
266  void vmult_add (BlockVector<number> &dst,
267  const BlockVector<number> &src) const;
268 
273  void Tvmult (BlockVector<number> &dst,
274  const BlockVector<number> &src) const;
275 
281  void Tvmult_add (BlockVector<number> &dst,
282  const BlockVector<number> &src) const;
283 
289  number matrix_scalar_product (const BlockVector<number> &u,
290  const BlockVector<number> &v) const;
291 
298  number matrix_norm_square (const BlockVector<number> &u) const;
299 
317  template <class STREAM>
318  void print_latex (STREAM &out) const;
319 
320 protected:
333  class Entry
334  {
335  public:
343  template<class MATRIX>
344  Entry (const MATRIX &matrix,
345  size_type row, size_type col,
346  double prefix, bool transpose);
347 
361  Entry(const Entry &);
362 
369  ~Entry();
370 
375  size_type row;
376 
381  size_type col;
382 
387  double prefix;
388 
394  bool transpose;
395 
400  };
401 
406  std::vector<Entry> entries;
407 
408 private:
412  unsigned int block_rows;
416  unsigned int block_cols;
417 };
418 
479 template <typename number = double>
481  : private BlockMatrixArray<number>
482 {
483 public:
488 
496 
503  BlockTrianglePrecondition (const unsigned int n_blocks);
504 
514  BlockTrianglePrecondition (const unsigned int n_block_rows,
516  const bool backward = false) DEAL_II_DEPRECATED;
517 
528  void initialize (const unsigned int n_block_rows,
529  VectorMemory<Vector<number> > &mem,
530  const bool backward = false) DEAL_II_DEPRECATED;
531 
536  void reinit (const unsigned int n_block_rows);
537 
538 
546  template <class MATRIX>
547  void enter (const MATRIX &matrix,
548  const size_type row,
549  const size_type col,
550  const double prefix = 1.,
551  const bool transpose = false);
552 
564  template <class MATRIX>
565  void enter_aux (VectorMemory<Vector<double> > &mem,
566  const MATRIX &matrix,
567  const size_type row,
568  const size_type col,
569  const double prefix = 1.,
570  const bool transpose = false) DEAL_II_DEPRECATED;
571 
575  void vmult (BlockVector<number> &dst,
576  const BlockVector<number> &src) const;
577 
582  void vmult_add (BlockVector<number> &dst,
583  const BlockVector<number> &src) const;
584 
588  void Tvmult (BlockVector<number> &dst,
589  const BlockVector<number> &src) const;
590 
595  void Tvmult_add (BlockVector<number> &dst,
596  const BlockVector<number> &src) const;
597 
601  using BlockMatrixArray<number>::print_latex;
602 
606  using BlockMatrixArray<number>::n_block_rows;
607 
611  using BlockMatrixArray<number>::n_block_cols;
612  using BlockMatrixArray<number>::clear;
613  using BlockMatrixArray<number>::Subscriptor::subscribe;
614  using BlockMatrixArray<number>::Subscriptor::unsubscribe;
615 
626  DeclException1(ExcNoDiagonal,
627  size_type,
628  << "No diagonal entry was added for block " << arg1);
629 
637  DeclException1(ExcMultipleDiagonal,
638  size_type,
639  << "Inverse diagonal entries may not be added in block "
640  << arg1);
642 private:
649  void do_row (BlockVector<number> &dst,
650  size_type row_num) const;
651 
655  bool backward;
656 };
657 
658 
659 #ifndef DOXYGEN
660 //---------------------------------------------------------------------------
661 
662 template <typename number>
663 template <class MATRIX>
664 inline
666  const MATRIX &m,
667  size_type row,
668  size_type col,
669  double prefix,
670  bool transpose)
671  :
672  row (row),
673  col (col),
674  prefix (prefix),
675  transpose (transpose),
676  matrix (new_pointer_matrix_base(m, Vector<number>(), typeid(*this).name()))
677 {}
678 
679 
680 
681 template <typename number>
682 template <class MATRIX>
683 inline
684 void
686  const MATRIX &matrix,
687  unsigned int row,
688  unsigned int col,
689  double prefix,
690  bool transpose)
691 {
692  Assert(row<n_block_rows(), ExcIndexRange(row, 0, n_block_rows()));
693  Assert(col<n_block_cols(), ExcIndexRange(col, 0, n_block_cols()));
694  entries.push_back(Entry(matrix, row, col, prefix, transpose));
695 }
696 
697 
698 template <typename number>
699 template <class MATRIX>
700 inline
701 void
704  const MATRIX &matrix,
705  unsigned int row,
706  unsigned int col,
707  double prefix,
708  bool transpose)
709 {
710  Assert(row<n_block_rows(), ExcIndexRange(row, 0, n_block_rows()));
711  Assert(col<n_block_cols(), ExcIndexRange(col, 0, n_block_cols()));
712  entries.push_back(Entry(matrix, row, col, prefix, transpose, mem));
713 }
714 
715 
716 template<typename number>
717 struct BlockMatrixArrayPointerMatrixLess
718 {
719  bool operator () (const PointerMatrixBase<Vector<number> > *a,
720  const PointerMatrixBase<Vector<number> > *b) const
721  {
722  return *a < *b;
723  }
724 };
725 
726 
727 template <typename number>
728 template <class STREAM>
729 inline
730 void
731 BlockMatrixArray<number>::print_latex (STREAM &out) const
732 {
733  out << "\\begin{array}{"
734  << std::string(n_block_cols(), 'c')
735  << "}" << std::endl;
736 
738 
739  typedef std::map<const PointerMatrixBase<Vector<number> > *,
740  std::string, BlockMatrixArrayPointerMatrixLess<number> > NameMap;
741  NameMap matrix_names;
742 
743  typename std::vector<Entry>::const_iterator m = entries.begin();
744  typename std::vector<Entry>::const_iterator end = entries.end();
745 
746  size_type matrix_number = 0;
747  for (; m != end ; ++m)
748  {
749  if (matrix_names.find(m->matrix) == matrix_names.end())
750  {
751  std::pair<typename NameMap::iterator, bool> x =
752  matrix_names.insert(
753  std::pair<const PointerMatrixBase<Vector<number> >*, std::string> (m->matrix,
754  std::string("M")));
755  std::ostringstream stream;
756  stream << matrix_number++;
757 
758  x.first->second += stream.str();
759  }
760 
761  std::ostringstream stream;
762 
763  if (array(m->row, m->col) != "" && m->prefix >= 0)
764  stream << "+";
765  if (m->prefix != 1.)
766  stream << m->prefix << 'x';
767  stream << matrix_names.find(m->matrix)->second;
768 // stream << '(' << m->matrix << ')';
769  if (m->transpose)
770  stream << "^T";
771 
772  array(m->row, m->col) += stream.str();
773  }
774  for (unsigned int i=0; i<n_block_rows(); ++i)
775  for (unsigned int j=0; j<n_block_cols(); ++j)
776  {
777  out << '\t' << array(i,j);
778  if (j==n_block_cols()-1)
779  out << "\\\\" << std::endl;
780  else
781  out << " &";
782  }
783  out << "\\end{array}" << std::endl;
784 }
785 
786 template <typename number>
787 template <class MATRIX>
788 inline
789 void
791  size_type row, size_type col,
792  double prefix, bool transpose)
793 {
794  BlockMatrixArray<number>::enter(matrix, row, col, prefix, transpose);
795 }
796 
797 
798 
799 template <typename number>
800 template <class MATRIX>
801 inline
802 void
805  const MATRIX &matrix,
806  size_type row,
807  size_type col,
808  double prefix,
809  bool transpose)
810 {
811  BlockMatrixArray<number>::enter_aux(mem, matrix, row, col, prefix, transpose);
812 }
813 
814 
815 
816 #endif // DOXYGEN
817 
818 DEAL_II_NAMESPACE_CLOSE
819 
820 #endif
void Tvmult_add(BlockVector< number > &dst, const BlockVector< number > &src) const
void unsubscribe(const char *identifier=0) const
std::vector< Entry > entries
void subscribe(const char *identifier=0) const
void enter(const MATRIX &matrix, const unsigned int row, const unsigned int col, const double prefix=1., const bool transpose=false)
void vmult(BlockVector< number > &dst, const BlockVector< number > &src) const
unsigned int n_block_cols() const
void Tvmult(BlockVector< number > &dst, const BlockVector< number > &src) const
PointerMatrixBase< Vector< number > > * matrix
void vmult_add(BlockVector< number > &dst, const BlockVector< number > &src) const
void print_latex(STREAM &out) const
Entry(const MATRIX &matrix, size_type row, size_type col, double prefix, bool transpose)
types::global_dof_index size_type
#define DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:515
unsigned int global_dof_index
Definition: types.h:100
#define Assert(cond, exc)
Definition: exceptions.h:299
::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
void reinit(const unsigned int n_block_rows, const unsigned int n_block_cols)
BlockCompressedSparsityPattern CompressedBlockSparsityPattern DEAL_II_DEPRECATED
unsigned int block_cols
void enter_aux(VectorMemory< Vector< number > > &mem, const MATRIX &matrix, const unsigned int row, const unsigned int col, const double prefix=1., const bool transpose=false) DEAL_II_DEPRECATED
number matrix_scalar_product(const BlockVector< number > &u, const BlockVector< number > &v) const
void enter_aux(VectorMemory< Vector< double > > &mem, const MATRIX &matrix, const size_type row, const size_type col, const double prefix=1., const bool transpose=false) DEAL_II_DEPRECATED
number matrix_norm_square(const BlockVector< number > &u) const
unsigned int block_rows
void enter(const MATRIX &matrix, const size_type row, const size_type col, const double prefix=1., const bool transpose=false)
unsigned int n_block_rows() const
void initialize(const unsigned int n_block_rows, const unsigned int n_block_cols)
types::global_dof_index size_type