Reference documentation for deal.II version 8.4.2
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 
300  size_type row;
301 
305  size_type col;
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 };
338 
396 template <typename number = double, typename BlockVectorType = BlockVector<number> >
398  : private BlockMatrixArray<number,BlockVectorType>
399 {
400 public:
405 
411 
416  BlockTrianglePrecondition (const unsigned int n_blocks);
417 
421  void reinit (const unsigned int n_block_rows);
422 
423 
428  template <typename MatrixType>
429  void enter (const MatrixType &matrix,
430  const size_type row,
431  const size_type col,
432  const number prefix = 1.,
433  const bool transpose = false);
434 
438  void vmult (BlockVectorType &dst,
439  const BlockVectorType &src) const;
440 
444  void vmult_add (BlockVectorType &dst,
445  const BlockVectorType &src) const;
446 
450  void Tvmult (BlockVectorType &dst,
451  const BlockVectorType &src) const;
452 
456  void Tvmult_add (BlockVectorType &dst,
457  const BlockVectorType &src) const;
458 
463 
468 
476 
486  DeclException1(ExcNoDiagonal,
487  size_type,
488  << "No diagonal entry was added for block " << arg1);
489 
494  DeclException1(ExcMultipleDiagonal,
495  size_type,
496  << "Inverse diagonal entries may not be added in block "
497  << arg1);
499 private:
504  void do_row (BlockVectorType &dst,
505  size_type row_num) const;
506 
510  bool backward;
511 };
512 
513 
514 #ifndef DOXYGEN
515 //---------------------------------------------------------------------------
516 
517 template <typename number, typename BlockVectorType>
518 template <typename MatrixType>
519 inline
521 (const MatrixType &m,
522  size_type row,
523  size_type col,
524  number prefix,
525  bool transpose)
526  :
527  row (row),
528  col (col),
529  prefix (prefix),
530  transpose (transpose),
531  matrix (new_pointer_matrix_base(m, typename BlockVectorType::BlockType(), typeid(*this).name()))
532 {}
533 
534 
535 
536 template <typename number, typename BlockVectorType>
537 template <typename MatrixType>
538 inline
539 void
540 BlockMatrixArray<number, BlockVectorType>::enter (const MatrixType &matrix,
541  unsigned int row,
542  unsigned int col,
543  number prefix,
544  bool transpose)
545 {
546  Assert(row<n_block_rows(), ExcIndexRange(row, 0, n_block_rows()));
547  Assert(col<n_block_cols(), ExcIndexRange(col, 0, n_block_cols()));
548  entries.push_back(Entry(matrix, row, col, prefix, transpose));
549 }
550 
551 
552 template <typename number, typename BlockVectorType>
553 template <class StreamType>
554 inline
555 void
557 {
558  out << "\\begin{array}{"
559  << std::string(n_block_cols(), 'c')
560  << "}" << std::endl;
561 
563 
564  typedef std::map<const PointerMatrixBase<typename BlockVectorType::BlockType > *, std::string> NameMap;
565  NameMap matrix_names;
566 
567  typename std::vector<Entry>::const_iterator m = entries.begin();
568  typename std::vector<Entry>::const_iterator end = entries.end();
569 
570  size_type matrix_number = 0;
571  for (; m != end ; ++m)
572  {
573  if (matrix_names.find(m->matrix) == matrix_names.end())
574  {
575  std::pair<typename NameMap::iterator, bool> x =
576  matrix_names.insert(
577  std::pair<const PointerMatrixBase<typename BlockVectorType::BlockType >*, std::string> (m->matrix,
578  std::string("M")));
579  std::ostringstream stream;
580  stream << matrix_number++;
581 
582  x.first->second += stream.str();
583  }
584 
585  std::ostringstream stream;
586 
587  if (array(m->row, m->col) != "" && m->prefix >= 0)
588  stream << "+";
589  if (m->prefix != 1.)
590  stream << m->prefix << 'x';
591  stream << matrix_names.find(m->matrix)->second;
592 // stream << '(' << m->matrix << ')';
593  if (m->transpose)
594  stream << "^T";
595 
596  array(m->row, m->col) += stream.str();
597  }
598  for (unsigned int i=0; i<n_block_rows(); ++i)
599  for (unsigned int j=0; j<n_block_cols(); ++j)
600  {
601  out << '\t' << array(i,j);
602  if (j==n_block_cols()-1)
603  {
604  if (i != n_block_rows() - 1)
605  out << "\\\\" << std::endl;
606  else
607  out << std::endl;
608  }
609  else
610  out << " &";
611  }
612  out << "\\end{array}" << std::endl;
613 }
614 
615 template <typename number, typename BlockVectorType>
616 template <typename MatrixType>
617 inline
618 void
620  size_type row,
621  size_type col,
622  number prefix,
623  bool transpose)
624 {
625  BlockMatrixArray<number, BlockVectorType>::enter(matrix, row, col, prefix, transpose);
626 }
627 
628 
629 
630 #endif // DOXYGEN
631 
632 DEAL_II_NAMESPACE_CLOSE
633 
634 #endif
unsigned int block_cols
std::vector< Entry > entries
number matrix_norm_square(const BlockVectorType &u) const
types::global_dof_index size_type
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:542
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:294
unsigned int n_block_rows() const
void print_latex(StreamType &out) const
void Tvmult(BlockVectorType &dst, const BlockVectorType &src) const
unsigned int block_rows
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)
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
PointerMatrixBase< typename BlockVectorType::BlockType > * matrix
Definition: table.h:33
number matrix_scalar_product(const BlockVectorType &u, const BlockVectorType &v) const
void enter(const MatrixType &matrix, const unsigned int row, const unsigned int col, const number prefix=1., const bool transpose=false)