Reference documentation for deal.II version GIT 9042b9283b 2023-12-02 14:50:02+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
block_sparse_matrix.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2000 - 2022 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_sparse_matrix_h
17 #define dealii_block_sparse_matrix_h
18 
19 
20 #include <deal.II/base/config.h>
21 
25 #include <deal.II/lac/exceptions.h>
27 
28 #include <cmath>
29 
31 
32 
49 template <typename number>
50 class BlockSparseMatrix : public BlockMatrixBase<SparseMatrix<number>>
51 {
52 public:
57 
61  using BlockType = typename BaseClass::BlockType;
62 
67  using pointer = typename BaseClass::pointer;
69  using reference = typename BaseClass::reference;
71  using size_type = typename BaseClass::size_type;
72  using iterator = typename BaseClass::iterator;
74 
90  BlockSparseMatrix() = default;
91 
105 
109  virtual ~BlockSparseMatrix() override;
110 
111 
112 
119 
129  operator=(const double d);
130 
139  void
140  clear();
141 
156  virtual void
157  reinit(const BlockSparsityPattern &sparsity);
168  bool
169  empty() const;
170 
174  size_type
175  get_row_length(const size_type row) const;
176 
182  size_type
184 
190  size_type
191  n_actually_nonzero_elements(const double threshold = 0.0) const;
192 
201  const BlockSparsityPattern &
203 
208  std::size_t
220  template <typename block_number>
221  void
223  const BlockVector<block_number> &src) const;
224 
229  template <typename block_number, typename nonblock_number>
230  void
232  const Vector<nonblock_number> &src) const;
233 
238  template <typename block_number, typename nonblock_number>
239  void
241  const BlockVector<block_number> &src) const;
242 
247  template <typename nonblock_number>
248  void
249  vmult(Vector<nonblock_number> &dst, const Vector<nonblock_number> &src) const;
250 
256  template <typename block_number>
257  void
259  const BlockVector<block_number> &src) const;
260 
265  template <typename block_number, typename nonblock_number>
266  void
268  const Vector<nonblock_number> &src) const;
269 
274  template <typename block_number, typename nonblock_number>
275  void
277  const BlockVector<block_number> &src) const;
278 
283  template <typename nonblock_number>
284  void
286  const Vector<nonblock_number> &src) const;
300  template <typename BlockVectorType>
301  void
302  precondition_Jacobi(BlockVectorType &dst,
303  const BlockVectorType &src,
304  const number omega = 1.) const;
305 
311  template <typename number2>
312  void
314  const Vector<number2> &src,
315  const number omega = 1.) const;
344  void
345  print_formatted(std::ostream &out,
346  const unsigned int precision = 3,
347  const bool scientific = true,
348  const unsigned int width = 0,
349  const char *zero_string = " ",
350  const double denominator = 1.,
351  const char *separator = " ") const;
364 private:
372 };
373 
374 
375 
377 /* ------------------------- Template functions ---------------------- */
378 
379 
380 
381 template <typename number>
384 {
386 
387  for (size_type r = 0; r < this->n_block_rows(); ++r)
388  for (size_type c = 0; c < this->n_block_cols(); ++c)
389  this->block(r, c) = d;
390 
391  return *this;
392 }
393 
394 
395 
396 template <typename number>
397 template <typename block_number>
398 inline void
400  const BlockVector<block_number> &src) const
401 {
402  BaseClass::vmult_block_block(dst, src);
403 }
404 
405 
406 
407 template <typename number>
408 template <typename block_number, typename nonblock_number>
409 inline void
411  const Vector<nonblock_number> &src) const
412 {
413  BaseClass::vmult_block_nonblock(dst, src);
414 }
415 
416 
417 
418 template <typename number>
419 template <typename block_number, typename nonblock_number>
420 inline void
422  const BlockVector<block_number> &src) const
423 {
424  BaseClass::vmult_nonblock_block(dst, src);
425 }
426 
427 
428 
429 template <typename number>
430 template <typename nonblock_number>
431 inline void
433  const Vector<nonblock_number> &src) const
434 {
435  BaseClass::vmult_nonblock_nonblock(dst, src);
436 }
437 
438 
439 
440 template <typename number>
441 template <typename block_number>
442 inline void
444  const BlockVector<block_number> &src) const
445 {
446  BaseClass::Tvmult_block_block(dst, src);
447 }
448 
449 
450 
451 template <typename number>
452 template <typename block_number, typename nonblock_number>
453 inline void
455  const Vector<nonblock_number> &src) const
456 {
457  BaseClass::Tvmult_block_nonblock(dst, src);
458 }
459 
460 
461 
462 template <typename number>
463 template <typename block_number, typename nonblock_number>
464 inline void
466  const BlockVector<block_number> &src) const
467 {
468  BaseClass::Tvmult_nonblock_block(dst, src);
469 }
470 
471 
472 
473 template <typename number>
474 template <typename nonblock_number>
475 inline void
477  const Vector<nonblock_number> &src) const
478 {
479  BaseClass::Tvmult_nonblock_nonblock(dst, src);
480 }
481 
482 
483 
484 template <typename number>
485 template <typename BlockVectorType>
486 inline void
488  const BlockVectorType &src,
489  const number omega) const
490 {
491  Assert(this->n_block_rows() == this->n_block_cols(), ExcNotQuadratic());
492  Assert(dst.n_blocks() == this->n_block_rows(),
493  ExcDimensionMismatch(dst.n_blocks(), this->n_block_rows()));
494  Assert(src.n_blocks() == this->n_block_cols(),
495  ExcDimensionMismatch(src.n_blocks(), this->n_block_cols()));
496 
497  // do a diagonal preconditioning. uses only
498  // the diagonal blocks of the matrix
499  for (size_type i = 0; i < this->n_block_rows(); ++i)
500  this->block(i, i).precondition_Jacobi(dst.block(i), src.block(i), omega);
501 }
502 
503 
504 
505 template <typename number>
506 template <typename number2>
507 inline void
509  const Vector<number2> &src,
510  const number omega) const
511 {
512  // check number of blocks. the sizes of the
513  // single block is checked in the function
514  // we call
515  Assert(this->n_block_cols() == 1,
516  ExcMessage("This function only works if the matrix has "
517  "a single block"));
518  Assert(this->n_block_rows() == 1,
519  ExcMessage("This function only works if the matrix has "
520  "a single block"));
521 
522  // do a diagonal preconditioning. uses only
523  // the diagonal blocks of the matrix
524  this->block(0, 0).precondition_Jacobi(dst, src, omega);
525 }
526 
527 
529 
530 #endif // dealii_block_sparse_matrix_h
MatrixIterator< BlockMatrixIterators::Accessor< BlockMatrixBase, true > > const_iterator
typename BlockType::value_type value_type
MatrixIterator< BlockMatrixIterators::Accessor< BlockMatrixBase, false > > iterator
BlockSparseMatrix(const BlockSparsityPattern &sparsity)
typename BaseClass::value_type value_type
typename BaseClass::iterator iterator
typename BaseClass::const_pointer const_pointer
BlockSparseMatrix()=default
virtual void reinit(const BlockSparsityPattern &sparsity)
size_type n_nonzero_elements() const
std::size_t memory_consumption() const
const BlockSparsityPattern & get_sparsity_pattern() const
bool empty() const
virtual ~BlockSparseMatrix() override
typename BaseClass::pointer pointer
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 char *separator=" ") const
typename BaseClass::const_reference const_reference
size_type n_actually_nonzero_elements(const double threshold=0.0) const
typename BaseClass::size_type size_type
SmartPointer< const BlockSparsityPattern, BlockSparseMatrix< number > > sparsity_pattern
typename BaseClass::const_iterator const_iterator
void Tvmult(BlockVector< block_number > &dst, const BlockVector< block_number > &src) const
BlockSparseMatrix & operator=(const BlockSparseMatrix &)
void vmult(BlockVector< block_number > &dst, const BlockVector< block_number > &src) const
typename BaseClass::BlockType BlockType
typename BaseClass::reference reference
size_type get_row_length(const size_type row) const
void precondition_Jacobi(BlockVectorType &dst, const BlockVectorType &src, const number omega=1.) const
Definition: vector.h:110
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:477
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:478
#define DeclException0(Exception0)
Definition: exceptions.h:472
static ::ExceptionBase & ExcScalarAssignmentOnlyForZeroValue()
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
#define Assert(cond, exc)
Definition: exceptions.h:1631
static ::ExceptionBase & ExcMessage(std::string arg1)
static ::ExceptionBase & ExcNotQuadratic()
static ::ExceptionBase & ExcBlockDimensionMismatch()
SymmetricTensor< 2, dim, Number > d(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)