Reference documentation for deal.II version Git 7026f387cc 2019-10-15 14:19:01 -0400
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
block_matrix_array.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2001 - 2019 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.md at
12 // the top level directory of deal.II.
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 
21 #include <deal.II/base/subscriptor.h>
22 #include <deal.II/base/table.h>
23 
24 #include <deal.II/lac/block_vector.h>
25 #include <deal.II/lac/pointer_matrix.h>
26 #include <deal.II/lac/vector_memory.h>
27 
28 #include <map>
29 #include <memory>
30 #include <sstream>
31 #include <string>
32 #include <vector>
33 
34 DEAL_II_NAMESPACE_OPEN
35 
116 template <typename number = double,
117  typename BlockVectorType = BlockVector<number>>
118 class DEAL_II_DEPRECATED BlockMatrixArray : public Subscriptor
119 {
120 public:
125 
131 
135  BlockMatrixArray(const unsigned int n_block_rows,
136  const unsigned int n_block_cols);
137 
142  void
143  initialize(const unsigned int n_block_rows, const unsigned int n_block_cols);
144 
148  void
149  reinit(const unsigned int n_block_rows, const unsigned int n_block_cols);
150 
161  template <typename MatrixType>
162  void
163  enter(const MatrixType & matrix,
164  const unsigned int row,
165  const unsigned int col,
166  const number prefix = 1.,
167  const bool transpose = false);
168 
172  void
173  clear();
174 
178  unsigned int
179  n_block_rows() const;
180 
184  unsigned int
185  n_block_cols() const;
186 
190  void
191  vmult(BlockVectorType &dst, const BlockVectorType &src) const;
192 
196  void
197  vmult_add(BlockVectorType &dst, const BlockVectorType &src) const;
198 
202  void
203  Tvmult(BlockVectorType &dst, const BlockVectorType &src) const;
204 
208  void
209  Tvmult_add(BlockVectorType &dst, const BlockVectorType &src) const;
210 
215  number
216  matrix_scalar_product(const BlockVectorType &u,
217  const BlockVectorType &v) const;
218 
223  number
224  matrix_norm_square(const BlockVectorType &u) const;
225 
265  template <class StreamType>
266  void
267  print_latex(StreamType &out) const;
268 
269 protected:
279  class Entry
280  {
281  public:
286  template <typename MatrixType>
287  Entry(const MatrixType &matrix,
288  size_type row,
289  size_type col,
290  number prefix,
291  bool transpose);
292 
300  Entry(const Entry &);
301 
306  ~Entry();
307 
312 
317 
321  number prefix;
322 
326  bool transpose;
327 
332 
340  Entry &
341  operator=(const Entry &) = delete;
342  };
343 
347  std::vector<Entry> entries;
348 
349 private:
353  unsigned int block_rows;
357  unsigned int block_cols;
358 };
359 
419 template <typename number = double,
420  typename BlockVectorType = BlockVector<number>>
421 class DEAL_II_DEPRECATED BlockTrianglePrecondition
422  : private BlockMatrixArray<number, BlockVectorType>
423 {
424 public:
429 
435 
440  BlockTrianglePrecondition(const unsigned int n_blocks);
441 
445  void
446  reinit(const unsigned int n_block_rows);
447 
448 
453  template <typename MatrixType>
454  void
455  enter(const MatrixType &matrix,
456  const size_type row,
457  const size_type col,
458  const number prefix = 1.,
459  const bool transpose = false);
460 
464  void
465  vmult(BlockVectorType &dst, const BlockVectorType &src) const;
466 
470  void
471  vmult_add(BlockVectorType &dst, const BlockVectorType &src) const;
472 
476  void
477  Tvmult(BlockVectorType &dst, const BlockVectorType &src) const;
478 
482  void
483  Tvmult_add(BlockVectorType &dst, const BlockVectorType &src) const;
484 
489 
494 
502 
512  DeclException1(ExcNoDiagonal,
513  size_type,
514  << "No diagonal entry was added for block " << arg1);
515 
520  DeclException1(ExcMultipleDiagonal,
521  size_type,
522  << "Inverse diagonal entries may not be added in block "
523  << arg1);
525 private:
530  void
531  do_row(BlockVectorType &dst, size_type row_num) const;
532 
536  bool backward;
537 };
538 
539 
540 #ifndef DOXYGEN
541 //---------------------------------------------------------------------------
542 
543 template <typename number, typename BlockVectorType>
544 template <typename MatrixType>
546  const MatrixType &m,
547  size_type row,
548  size_type col,
549  number prefix,
550  bool transpose)
551  : row(row)
552  , col(col)
553  , prefix(prefix)
554  , transpose(transpose)
555  , matrix(new_pointer_matrix_base(m,
556  typename BlockVectorType::BlockType(),
557  typeid(*this).name()))
558 {}
559 
560 
561 
562 template <typename number, typename BlockVectorType>
563 template <typename MatrixType>
564 inline void
565 BlockMatrixArray<number, BlockVectorType>::enter(const MatrixType &matrix,
566  unsigned int row,
567  unsigned int col,
568  number prefix,
569  bool transpose)
570 {
571  Assert(row < n_block_rows(), ExcIndexRange(row, 0, n_block_rows()));
572  Assert(col < n_block_cols(), ExcIndexRange(col, 0, n_block_cols()));
573  entries.push_back(Entry(matrix, row, col, prefix, transpose));
574 }
575 
576 
577 template <typename number, typename BlockVectorType>
578 template <class StreamType>
579 inline void
581 {
582  out << "\\begin{array}{" << std::string(n_block_cols(), 'c') << "}"
583  << std::endl;
584 
586 
587  using NameMap =
588  std::map<const PointerMatrixBase<typename BlockVectorType::BlockType> *,
589  std::string>;
590  NameMap matrix_names;
591 
592  typename std::vector<Entry>::const_iterator m = entries.begin();
593  typename std::vector<Entry>::const_iterator end = entries.end();
594 
595  size_type matrix_number = 0;
596  for (; m != end; ++m)
597  {
598  if (matrix_names.find(m->matrix) == matrix_names.end())
599  {
600  std::pair<typename NameMap::iterator, bool> x = matrix_names.insert(
601  std::pair<
603  std::string>(m->matrix, std::string("M")));
604  std::ostringstream stream;
605  stream << matrix_number++;
606 
607  x.first->second += stream.str();
608  }
609 
610  std::ostringstream stream;
611 
612  if (!array(m->row, m->col).empty() && m->prefix >= 0)
613  stream << "+";
614  if (m->prefix != 1.)
615  stream << m->prefix << 'x';
616  stream << matrix_names.find(m->matrix)->second;
617  // stream << '(' << m->matrix << ')';
618  if (m->transpose)
619  stream << "^T";
620 
621  array(m->row, m->col) += stream.str();
622  }
623  for (unsigned int i = 0; i < n_block_rows(); ++i)
624  for (unsigned int j = 0; j < n_block_cols(); ++j)
625  {
626  out << '\t' << array(i, j);
627  if (j == n_block_cols() - 1)
628  {
629  if (i != n_block_rows() - 1)
630  out << "\\\\" << std::endl;
631  else
632  out << std::endl;
633  }
634  else
635  out << " &";
636  }
637  out << "\\end{array}" << std::endl;
638 }
639 
640 template <typename number, typename BlockVectorType>
641 template <typename MatrixType>
642 inline void
644  const MatrixType &matrix,
645  size_type row,
646  size_type col,
647  number prefix,
648  bool transpose)
649 {
651  matrix, row, col, prefix, transpose);
652 }
653 
654 
655 
656 #endif // DOXYGEN
657 
658 DEAL_II_NAMESPACE_CLOSE
659 
660 #endif
unsigned int block_cols
std::vector< Entry > entries
constexpr SymmetricTensor< rank_, dim, Number > transpose(const SymmetricTensor< rank_, dim, Number > &t)
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
PointerMatrixBase< typename BlockVectorType::BlockType > * matrix
PointerMatrixBase< VectorType > * new_pointer_matrix_base(MatrixType &matrix, const VectorType &, const char *name="PointerMatrixAux")
types::global_dof_index size_type
unsigned int n_block_cols() const
#define DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:518
#define Assert(cond, exc)
Definition: exceptions.h:1407
unsigned int n_block_rows() const
void print_latex(StreamType &out) const
unsigned int block_rows
Entry(const MatrixType &matrix, size_type row, size_type col, number prefix, bool transpose)
void enter(const MatrixType &matrix, const size_type row, const size_type col, const number prefix=1., const bool transpose=false)
unsigned int global_dof_index
Definition: types.h:89
Definition: table.h:699
bool empty() const
void enter(const MatrixType &matrix, const unsigned int row, const unsigned int col, const number prefix=1., const bool transpose=false)