Reference documentation for deal.II version 8.5.1
block_matrix_array.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2001 - 2016 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii__block_matrix_array_h
17 #define dealii__block_matrix_array_h
18 
19 #include <deal.II/base/config.h>
20 #include <deal.II/base/subscriptor.h>
21 #include <deal.II/base/table.h>
22 
23 #include <deal.II/lac/pointer_matrix.h>
24 #include <deal.II/lac/vector_memory.h>
25 #include <deal.II/lac/block_vector.h>
26 
27 #include <vector>
28 #include <map>
29 #include <string>
30 #include <memory>
31 #include <sstream>
32 
33 DEAL_II_NAMESPACE_OPEN
34 
113 template <typename number = double, typename BlockVectorType=BlockVector<number> >
115 {
116 public:
121 
126  BlockMatrixArray ();
127 
131  BlockMatrixArray (const unsigned int n_block_rows,
132  const unsigned int n_block_cols);
133 
138  void initialize (const unsigned int n_block_rows,
139  const unsigned int n_block_cols);
140 
144  void reinit (const unsigned int n_block_rows,
145  const unsigned int n_block_cols);
146 
157  template <typename MatrixType>
158  void enter (const MatrixType &matrix,
159  const unsigned int row,
160  const unsigned int col,
161  const number prefix = 1.,
162  const bool transpose = false);
163 
167  void clear();
168 
172  unsigned int n_block_rows () const;
173 
177  unsigned int n_block_cols () const;
178 
182  void vmult (BlockVectorType &dst,
183  const BlockVectorType &src) const;
184 
188  void vmult_add(BlockVectorType &dst,
189  const BlockVectorType &src) const;
190 
194  void Tvmult (BlockVectorType &dst,
195  const BlockVectorType &src) const;
196 
200  void Tvmult_add (BlockVectorType &dst,
201  const BlockVectorType &src) const;
202 
207  number matrix_scalar_product (const BlockVectorType &u,
208  const BlockVectorType &v) const;
209 
214  number matrix_norm_square (const BlockVectorType &u) const;
215 
255  template <class StreamType>
256  void print_latex (StreamType &out) const;
257 
258 protected:
268  class Entry
269  {
270  public:
275  template<typename MatrixType>
276  Entry (const MatrixType &matrix,
277  size_type row,
278  size_type col,
279  number prefix,
280  bool transpose);
281 
289  Entry(const Entry &);
290 
295  ~Entry();
296 
301 
306 
310  number prefix;
311 
315  bool transpose;
316 
321  };
322 
326  std::vector<Entry> entries;
327 
328 private:
332  unsigned int block_rows;
336  unsigned int block_cols;
337 
343  Entry &operator= (const Entry &);
344 };
345 
403 template <typename number = double, typename BlockVectorType = BlockVector<number> >
405  : private BlockMatrixArray<number,BlockVectorType>
406 {
407 public:
412 
418 
423  BlockTrianglePrecondition (const unsigned int n_blocks);
424 
428  void reinit (const unsigned int n_block_rows);
429 
430 
435  template <typename MatrixType>
436  void enter (const MatrixType &matrix,
437  const size_type row,
438  const size_type col,
439  const number prefix = 1.,
440  const bool transpose = false);
441 
445  void vmult (BlockVectorType &dst,
446  const BlockVectorType &src) const;
447 
451  void vmult_add (BlockVectorType &dst,
452  const BlockVectorType &src) const;
453 
457  void Tvmult (BlockVectorType &dst,
458  const BlockVectorType &src) const;
459 
463  void Tvmult_add (BlockVectorType &dst,
464  const BlockVectorType &src) const;
465 
470 
475 
483 
494  size_type,
495  << "No diagonal entry was added for block " << arg1);
496 
502  size_type,
503  << "Inverse diagonal entries may not be added in block "
504  << arg1);
506 private:
511  void do_row (BlockVectorType &dst,
512  size_type row_num) const;
513 
517  bool backward;
518 };
519 
520 
521 #ifndef DOXYGEN
522 //---------------------------------------------------------------------------
523 
524 template <typename number, typename BlockVectorType>
525 template <typename MatrixType>
526 inline
528 (const MatrixType &m,
529  size_type row,
530  size_type col,
531  number prefix,
532  bool transpose)
533  :
534  row (row),
535  col (col),
536  prefix (prefix),
538  matrix (new_pointer_matrix_base(m, typename BlockVectorType::BlockType(), typeid(*this).name()))
539 {}
540 
541 
542 
543 template <typename number, typename BlockVectorType>
544 template <typename MatrixType>
545 inline
546 void
547 BlockMatrixArray<number, BlockVectorType>::enter (const MatrixType &matrix,
548  unsigned int row,
549  unsigned int col,
550  number prefix,
551  bool transpose)
552 {
553  Assert(row<n_block_rows(), ExcIndexRange(row, 0, n_block_rows()));
554  Assert(col<n_block_cols(), ExcIndexRange(col, 0, n_block_cols()));
555  entries.push_back(Entry(matrix, row, col, prefix, transpose));
556 }
557 
558 
559 template <typename number, typename BlockVectorType>
560 template <class StreamType>
561 inline
562 void
564 {
565  out << "\\begin{array}{"
566  << std::string(n_block_cols(), 'c')
567  << "}" << std::endl;
568 
569  Table<2,std::string> array(n_block_rows(), n_block_cols());
570 
571  typedef std::map<const PointerMatrixBase<typename BlockVectorType::BlockType > *, std::string> NameMap;
572  NameMap matrix_names;
573 
574  typename std::vector<Entry>::const_iterator m = entries.begin();
575  typename std::vector<Entry>::const_iterator end = entries.end();
576 
577  size_type matrix_number = 0;
578  for (; m != end ; ++m)
579  {
580  if (matrix_names.find(m->matrix) == matrix_names.end())
581  {
582  std::pair<typename NameMap::iterator, bool> x =
583  matrix_names.insert(
584  std::pair<const PointerMatrixBase<typename BlockVectorType::BlockType >*, std::string> (m->matrix,
585  std::string("M")));
586  std::ostringstream stream;
587  stream << matrix_number++;
588 
589  x.first->second += stream.str();
590  }
591 
592  std::ostringstream stream;
593 
594  if (array(m->row, m->col) != "" && m->prefix >= 0)
595  stream << "+";
596  if (m->prefix != 1.)
597  stream << m->prefix << 'x';
598  stream << matrix_names.find(m->matrix)->second;
599 // stream << '(' << m->matrix << ')';
600  if (m->transpose)
601  stream << "^T";
602 
603  array(m->row, m->col) += stream.str();
604  }
605  for (unsigned int i=0; i<n_block_rows(); ++i)
606  for (unsigned int j=0; j<n_block_cols(); ++j)
607  {
608  out << '\t' << array(i,j);
609  if (j==n_block_cols()-1)
610  {
611  if (i != n_block_rows() - 1)
612  out << "\\\\" << std::endl;
613  else
614  out << std::endl;
615  }
616  else
617  out << " &";
618  }
619  out << "\\end{array}" << std::endl;
620 }
621 
622 template <typename number, typename BlockVectorType>
623 template <typename MatrixType>
624 inline
625 void
627  size_type row,
628  size_type col,
629  number prefix,
630  bool transpose)
631 {
633 }
634 
635 
636 
637 #endif // DOXYGEN
638 
639 DEAL_II_NAMESPACE_CLOSE
640 
641 #endif
unsigned int block_cols
std::vector< Entry > entries
number matrix_norm_square(const BlockVectorType &u) const
types::global_dof_index size_type
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
void Tvmult(BlockVectorType &dst, const BlockVectorType &src) const
PointerMatrixBase< VectorType > * new_pointer_matrix_base(MatrixType &matrix, const VectorType &, const char *name="PointerMatrixAux")
void vmult(BlockVectorType &dst, const BlockVectorType &src) const
void vmult_add(BlockVectorType &dst, const BlockVectorType &src) const
unsigned int n_block_cols() const
#define DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:564
void initialize(const unsigned int n_block_rows, const unsigned int n_block_cols)
unsigned int global_dof_index
Definition: types.h:88
#define Assert(cond, exc)
Definition: exceptions.h:313
void vmult_add(BlockVectorType &dst, const BlockVectorType &src) const
unsigned int n_block_rows() const
void print_latex(StreamType &out) const
void Tvmult(BlockVectorType &dst, const BlockVectorType &src) const
unsigned int block_rows
static ::ExceptionBase & ExcMultipleDiagonal(size_type arg1)
Entry(const MatrixType &matrix, size_type row, size_type col, number prefix, bool transpose)
void reinit(const unsigned int n_block_rows, const unsigned int n_block_cols)
Entry & operator=(const Entry &)
void enter(const MatrixType &matrix, const size_type row, const size_type col, const number prefix=1., const bool transpose=false)
types::global_dof_index size_type
void Tvmult_add(BlockVectorType &dst, const BlockVectorType &src) const
static ::ExceptionBase & ExcNoDiagonal(size_type arg1)
void Tvmult_add(BlockVectorType &dst, const BlockVectorType &src) const
PointerMatrixBase< typename BlockVectorType::BlockType > * matrix
Definition: table.h:33
void reinit(const unsigned int n_block_rows)
void vmult(BlockVectorType &dst, const BlockVectorType &src) const
void do_row(BlockVectorType &dst, size_type row_num) const
number matrix_scalar_product(const BlockVectorType &u, const BlockVectorType &v) const
DerivativeForm< 1, spacedim, dim, Number > transpose(const DerivativeForm< 1, dim, spacedim, Number > &DF)
void enter(const MatrixType &matrix, const unsigned int row, const unsigned int col, const number prefix=1., const bool transpose=false)