Reference documentation for deal.II version 9.0.0
trilinos_block_sparse_matrix.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2008 - 2018 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_trilinos_block_sparse_matrix_h
17 #define dealii_trilinos_block_sparse_matrix_h
18 
19 
20 #include <deal.II/base/config.h>
21 
22 #ifdef DEAL_II_WITH_TRILINOS
23 
24 # include <deal.II/base/table.h>
25 # include <deal.II/base/template_constraints.h>
26 # include <deal.II/lac/block_matrix_base.h>
27 # include <deal.II/lac/trilinos_sparse_matrix.h>
28 # include <deal.II/lac/trilinos_parallel_block_vector.h>
29 # include <deal.II/lac/full_matrix.h>
30 # include <deal.II/lac/exceptions.h>
31 
32 # include <cmath>
33 
34 DEAL_II_NAMESPACE_OPEN
35 
36 // forward declarations
38 template <typename number> class BlockSparseMatrix;
39 
40 
41 namespace TrilinosWrappers
42 {
43 
71  class BlockSparseMatrix : public BlockMatrixBase<SparseMatrix>
72  {
73  public:
78 
83 
88  typedef BaseClass::pointer pointer;
89  typedef BaseClass::const_pointer const_pointer;
90  typedef BaseClass::reference reference;
91  typedef BaseClass::const_reference const_reference;
92  typedef BaseClass::size_type size_type;
95 
107  BlockSparseMatrix () = default;
108 
113 
119  operator = (const BlockSparseMatrix &) = default;
120 
131  operator = (const double d);
132 
146  void reinit (const size_type n_block_rows,
147  const size_type n_block_columns);
148 
154  template <typename BlockSparsityPatternType>
155  void reinit (const std::vector<Epetra_Map> &input_maps,
156  const BlockSparsityPatternType &block_sparsity_pattern,
157  const bool exchange_data = false);
158 
164  template <typename BlockSparsityPatternType>
165  void reinit (const std::vector<IndexSet> &input_maps,
166  const BlockSparsityPatternType &block_sparsity_pattern,
167  const MPI_Comm &communicator = MPI_COMM_WORLD,
168  const bool exchange_data = false);
169 
175  template <typename BlockSparsityPatternType>
176  void reinit (const BlockSparsityPatternType &block_sparsity_pattern);
177 
186  DEAL_II_DEPRECATED
187  void reinit (const std::vector<Epetra_Map> &input_maps,
188  const ::BlockSparseMatrix<double> &deal_ii_sparse_matrix,
189  const double drop_tolerance=1e-13);
190 
198  void reinit (const ::BlockSparseMatrix<double> &deal_ii_sparse_matrix,
199  const double drop_tolerance=1e-13);
200 
208  bool is_compressed () const;
209 
220  void collect_sizes ();
221 
226  size_type n_nonzero_elements () const;
227 
231  MPI_Comm get_mpi_communicator () const;
232 
242  DEAL_II_DEPRECATED
243  std::vector<Epetra_Map> domain_partitioner () const;
244 
254  DEAL_II_DEPRECATED
255  std::vector<Epetra_Map> range_partitioner () const;
256 
262  std::vector<IndexSet> locally_owned_domain_indices() const;
263 
269  std::vector<IndexSet> locally_owned_range_indices() const;
270 
277  template <typename VectorType1, typename VectorType2>
278  void vmult (VectorType1 &dst,
279  const VectorType2 &src) const;
280 
286  template <typename VectorType1, typename VectorType2>
287  void Tvmult (VectorType1 &dst,
288  const VectorType2 &src) const;
289 
302  TrilinosScalar residual (MPI::BlockVector &dst,
303  const MPI::BlockVector &x,
304  const MPI::BlockVector &b) const;
305 
313  TrilinosScalar residual (MPI::BlockVector &dst,
314  const MPI::Vector &x,
315  const MPI::BlockVector &b) const;
316 
324  TrilinosScalar residual (MPI::Vector &dst,
325  const MPI::BlockVector &x,
326  const MPI::Vector &b) const;
327 
335  TrilinosScalar residual (MPI::Vector &dst,
336  const MPI::Vector &x,
337  const MPI::Vector &b) const;
338 
344 
354  int, int, int, int,
355  << "The blocks [" << arg1 << ',' << arg2 << "] and ["
356  << arg3 << ',' << arg4 << "] have differing row numbers.");
357 
362  int, int, int, int,
363  << "The blocks [" << arg1 << ',' << arg2 << "] and ["
364  << arg3 << ',' << arg4 << "] have differing column numbers.");
366 
367  private:
371  template <typename VectorType1, typename VectorType2>
372  void vmult (VectorType1 &dst,
373  const VectorType2 &src,
374  const bool transpose,
375  const std::integral_constant<bool, true>,
376  const std::integral_constant<bool, true>) const;
377 
382  template <typename VectorType1, typename VectorType2>
383  void vmult (VectorType1 &dst,
384  const VectorType2 &src,
385  const bool transpose,
386  const std::integral_constant<bool, false>,
387  const std::integral_constant<bool, true>) const;
388 
393  template <typename VectorType1, typename VectorType2>
394  void vmult (VectorType1 &dst,
395  const VectorType2 &src,
396  const bool transpose,
397  const std::integral_constant<bool, true>,
398  const std::integral_constant<bool, false>) const;
399 
405  template <typename VectorType1, typename VectorType2>
406  void vmult (VectorType1 &dst,
407  const VectorType2 &src,
408  const bool transpose,
409  const std::integral_constant<bool, false>,
410  const std::integral_constant<bool, false>) const;
411  };
412 
413 
414 
417 // ------------- inline and template functions -----------------
418 
419 
420 
421  inline
424  {
426 
427  for (size_type r=0; r<this->n_block_rows(); ++r)
428  for (size_type c=0; c<this->n_block_cols(); ++c)
429  this->block(r,c) = d;
430 
431  return *this;
432  }
433 
434 
435 
436  inline
437  bool
439  {
440  bool compressed = true;
441  for (size_type row=0; row<n_block_rows(); ++row)
442  for (size_type col=0; col<n_block_cols(); ++col)
443  if (block(row, col).is_compressed() == false)
444  {
445  compressed = false;
446  break;
447  }
448 
449  return compressed;
450  }
451 
452 
453 
454  template <typename VectorType1, typename VectorType2>
455  inline
456  void
457  BlockSparseMatrix::vmult (VectorType1 &dst,
458  const VectorType2 &src) const
459  {
460  vmult(dst, src, false,
461  std::integral_constant<bool, IsBlockVector<VectorType1>::value>(),
462  std::integral_constant<bool, IsBlockVector<VectorType2>::value>());
463  }
464 
465 
466 
467  template <typename VectorType1, typename VectorType2>
468  inline
469  void
470  BlockSparseMatrix::Tvmult (VectorType1 &dst,
471  const VectorType2 &src) const
472  {
473  vmult(dst, src, true,
474  std::integral_constant<bool, IsBlockVector<VectorType1>::value>(),
475  std::integral_constant<bool, IsBlockVector<VectorType2>::value>());
476  }
477 
478 
479 
480  template <typename VectorType1, typename VectorType2>
481  inline
482  void
483  BlockSparseMatrix::vmult (VectorType1 &dst,
484  const VectorType2 &src,
485  const bool transpose,
486  std::integral_constant<bool, true>,
487  std::integral_constant<bool, true>) const
488  {
489  if (transpose == true)
491  else
492  BaseClass::vmult_block_block (dst, src);
493  }
494 
495 
496 
497 
498  template <typename VectorType1, typename VectorType2>
499  inline
500  void
501  BlockSparseMatrix::vmult (VectorType1 &dst,
502  const VectorType2 &src,
503  const bool transpose,
504  std::integral_constant<bool, false>,
505  std::integral_constant<bool, true>) const
506  {
507  if (transpose == true)
509  else
511  }
512 
513 
514 
515  template <typename VectorType1, typename VectorType2>
516  inline
517  void
518  BlockSparseMatrix::vmult (VectorType1 &dst,
519  const VectorType2 &src,
520  const bool transpose,
521  std::integral_constant<bool, true>,
522  std::integral_constant<bool, false>) const
523  {
524  if (transpose == true)
526  else
528  }
529 
530 
531 
532  template <typename VectorType1, typename VectorType2>
533  inline
534  void
535  BlockSparseMatrix::vmult (VectorType1 &dst,
536  const VectorType2 &src,
537  const bool transpose,
538  std::integral_constant<bool, false>,
539  std::integral_constant<bool, false>) const
540  {
541  if (transpose == true)
543  else
545  }
546 
547 
548 
549  inline
550  std::vector<IndexSet>
552  {
553  Assert (this->n_block_cols() != 0, ExcNotInitialized());
554  Assert (this->n_block_rows() != 0, ExcNotInitialized());
555 
556  std::vector<IndexSet> domain_indices;
557  for (size_type c = 0; c < this->n_block_cols(); ++c)
558  domain_indices.push_back(this->sub_objects[0][c]->locally_owned_domain_indices());
559 
560  return domain_indices;
561  }
562 
563 
564 
565  inline
566  std::vector<IndexSet>
568  {
569  Assert (this->n_block_cols() != 0, ExcNotInitialized());
570  Assert (this->n_block_rows() != 0, ExcNotInitialized());
571 
572  std::vector<IndexSet> range_indices;
573  for (size_type r = 0; r < this->n_block_rows(); ++r)
574  range_indices.push_back(this->sub_objects[r][0]->locally_owned_range_indices());
575 
576  return range_indices;
577  }
578 
579 
580 
581  namespace internal
582  {
583  namespace BlockLinearOperatorImplementation
584  {
585 
601  template <typename PayloadBlockType>
603  {
604  public:
608  typedef PayloadBlockType BlockType;
609 
619  template <typename... Args>
620  TrilinosBlockPayload (const Args &...)
621  {
622  static_assert(typeid(PayloadBlockType)==typeid(internal::LinearOperatorImplementation::TrilinosPayload),
623  "TrilinosBlockPayload can only accept a payload of type TrilinosPayload.");
624  }
625  };
626 
627  } /*namespace BlockLinearOperator*/
628  } /* namespace internal */
629 
630 
631 }/* namespace TrilinosWrappers */
632 
633 
634 DEAL_II_NAMESPACE_CLOSE
635 
636 #endif // DEAL_II_WITH_TRILINOS
637 
638 #endif // dealii_trilinos_block_sparse_matrix_h
void vmult_block_nonblock(BlockVectorType &dst, const VectorType &src) const
std::vector< Epetra_Map > range_partitioner() const
static ::ExceptionBase & ExcIncompatibleRowNumbers(int arg1, int arg2, int arg3, int arg4)
static ::ExceptionBase & ExcScalarAssignmentOnlyForZeroValue()
BlockMatrixBase< SparseMatrix > BaseClass
void vmult_nonblock_block(VectorType &dst, const BlockVectorType &src) const
static ::ExceptionBase & ExcNotInitialized()
void Tvmult_block_nonblock(BlockVectorType &dst, const VectorType &src) const
void Tvmult_block_block(BlockVectorType &dst, const BlockVectorType &src) const
void Tvmult_nonblock_block(VectorType &dst, const BlockVectorType &src) const
void vmult_block_block(BlockVectorType &dst, const BlockVectorType &src) const
void Tvmult_nonblock_nonblock(VectorType &dst, const VectorType &src) const
static ::ExceptionBase & ExcIncompatibleColNumbers(int arg1, int arg2, int arg3, int arg4)
unsigned int n_block_cols() const
std::vector< Epetra_Map > domain_partitioner() const
void vmult(VectorType1 &dst, const VectorType2 &src) const
#define Assert(cond, exc)
Definition: exceptions.h:1142
void reinit(const size_type n_block_rows, const size_type n_block_columns)
BlockType::value_type value_type
SymmetricTensor< rank_, dim, Number > transpose(const SymmetricTensor< rank_, dim, Number > &t)
void Tvmult(VectorType1 &dst, const VectorType2 &src) const
void vmult_nonblock_nonblock(VectorType &dst, const VectorType &src) const
BlockType & block(const unsigned int row, const unsigned int column)
#define DeclException4(Exception4, type1, type2, type3, type4, outsequence)
Definition: exceptions.h:382
unsigned int n_block_rows() const
std::vector< IndexSet > locally_owned_range_indices() const
TrilinosScalar residual(MPI::BlockVector &dst, const MPI::BlockVector &x, const MPI::BlockVector &b) const
BlockSparseMatrix & operator=(const BlockSparseMatrix &)=default
std::vector< IndexSet > locally_owned_domain_indices() const