deal.II version GIT relicensing-6834-g5b78e6bcdf 2026-10-01 11:20:01+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\}}\)
Loading...
Searching...
No Matches
trilinos_block_sparse_matrix.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2008 - 2026 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13#ifndef dealii_trilinos_block_sparse_matrix_h
14#define dealii_trilinos_block_sparse_matrix_h
15
16
17#include <deal.II/base/config.h>
18
19#ifndef DEAL_II_TRILINOS_WITH_EPETRA
20
23
24#endif
25
26#ifdef DEAL_II_TRILINOS_WITH_EPETRA
28
34
35# include <cmath>
36
37#endif
38
40
41#ifdef DEAL_II_TRILINOS_WITH_EPETRA
42// forward declarations
43# ifndef DOXYGEN
45template <typename number>
47# endif
48
49namespace TrilinosWrappers
50{
78 class BlockSparseMatrix : public BlockMatrixBase<SparseMatrix>
79 {
80 public:
85
90
102
114 BlockSparseMatrix() = default;
115
119 ~BlockSparseMatrix() override;
120
126 operator=(const BlockSparseMatrix &) = default;
127
138 operator=(const double d);
139
153 void
154 reinit(const size_type n_block_rows, const size_type n_block_columns);
155
161 template <typename BlockSparsityPatternType>
162 void
163 reinit(const std::vector<IndexSet> &input_maps,
164 const BlockSparsityPatternType &block_sparsity_pattern,
165 const MPI_Comm communicator = MPI_COMM_WORLD,
166 const bool exchange_data = false);
167
173 template <typename BlockSparsityPatternType>
174 void
175 reinit(const BlockSparsityPatternType &block_sparsity_pattern);
176
183 void
184 reinit(
185 const std::vector<IndexSet> &parallel_partitioning,
186 const ::BlockSparseMatrix<double> &dealii_block_sparse_matrix,
187 const MPI_Comm communicator = MPI_COMM_WORLD,
188 const double drop_tolerance = 1e-13);
189
197 void
198 reinit(const ::BlockSparseMatrix<double> &deal_ii_sparse_matrix,
199 const double drop_tolerance = 1e-13);
200
208 bool
209 is_compressed() const;
210
221 void
223
228 std::uint64_t
229 n_nonzero_elements() const;
230
235 get_mpi_communicator() const;
236
242 std::vector<IndexSet>
244
250 std::vector<IndexSet>
252
259 template <typename VectorType1, typename VectorType2>
260 void
261 vmult(VectorType1 &dst, const VectorType2 &src) const;
262
268 template <typename VectorType1, typename VectorType2>
269 void
270 Tvmult(VectorType1 &dst, const VectorType2 &src) const;
271
286 const MPI::BlockVector &x,
287 const MPI::BlockVector &b) const;
288
298 const MPI::Vector &x,
299 const MPI::BlockVector &b) const;
300
310 const MPI::BlockVector &x,
311 const MPI::Vector &b) const;
312
322 const MPI::Vector &x,
323 const MPI::Vector &b) const;
324
330
340 int,
341 int,
342 int,
343 int,
344 << "The blocks [" << arg1 << ',' << arg2 << "] and [" << arg3
345 << ',' << arg4 << "] have differing row numbers.");
346
351 int,
352 int,
353 int,
354 int,
355 << "The blocks [" << arg1 << ',' << arg2 << "] and [" << arg3
356 << ',' << arg4 << "] have differing column numbers.");
359 private:
363 template <typename VectorType1, typename VectorType2>
364 void
365 vmult(VectorType1 &dst,
366 const VectorType2 &src,
367 const bool transpose,
368 const std::bool_constant<true>,
369 const std::bool_constant<true>) const;
370
375 template <typename VectorType1, typename VectorType2>
376 void
377 vmult(VectorType1 &dst,
378 const VectorType2 &src,
379 const bool transpose,
380 const std::bool_constant<false>,
381 const std::bool_constant<true>) const;
382
387 template <typename VectorType1, typename VectorType2>
388 void
389 vmult(VectorType1 &dst,
390 const VectorType2 &src,
391 const bool transpose,
392 const std::bool_constant<true>,
393 const std::bool_constant<false>) const;
394
400 template <typename VectorType1, typename VectorType2>
401 void
402 vmult(VectorType1 &dst,
403 const VectorType2 &src,
404 const bool transpose,
405 const std::bool_constant<false>,
406 const std::bool_constant<false>) const;
407 };
408
409
410
413 // ------------- inline and template functions -----------------
414
415
416
417 inline BlockSparseMatrix &
419 {
421
422 for (size_type r = 0; r < this->n_block_rows(); ++r)
423 for (size_type c = 0; c < this->n_block_cols(); ++c)
424 this->block(r, c) = d;
425
426 return *this;
427 }
428
429
430
431 inline bool
433 {
434 bool compressed = true;
435 for (size_type row = 0; row < n_block_rows(); ++row)
436 for (size_type col = 0; col < n_block_cols(); ++col)
437 if (block(row, col).is_compressed() == false)
438 {
439 compressed = false;
440 break;
441 }
442
443 return compressed;
444 }
445
446
447
448 template <typename VectorType1, typename VectorType2>
449 inline void
450 BlockSparseMatrix::vmult(VectorType1 &dst, const VectorType2 &src) const
451 {
452 vmult(dst,
453 src,
454 false,
455 std::bool_constant<IsBlockVector<VectorType1>::value>(),
456 std::bool_constant<IsBlockVector<VectorType2>::value>());
457 }
458
459
460
461 template <typename VectorType1, typename VectorType2>
462 inline void
463 BlockSparseMatrix::Tvmult(VectorType1 &dst, const VectorType2 &src) const
464 {
465 vmult(dst,
466 src,
467 true,
468 std::bool_constant<IsBlockVector<VectorType1>::value>(),
469 std::bool_constant<IsBlockVector<VectorType2>::value>());
470 }
471
472
473
474 template <typename VectorType1, typename VectorType2>
475 inline void
476 BlockSparseMatrix::vmult(VectorType1 &dst,
477 const VectorType2 &src,
478 const bool transpose,
479 std::bool_constant<true>,
480 std::bool_constant<true>) const
481 {
482 if (transpose == true)
484 else
486 }
487
488
489
490 template <typename VectorType1, typename VectorType2>
491 inline void
492 BlockSparseMatrix::vmult(VectorType1 &dst,
493 const VectorType2 &src,
494 const bool transpose,
495 std::bool_constant<false>,
496 std::bool_constant<true>) const
497 {
498 if (transpose == true)
500 else
502 }
503
504
505
506 template <typename VectorType1, typename VectorType2>
507 inline void
508 BlockSparseMatrix::vmult(VectorType1 &dst,
509 const VectorType2 &src,
510 const bool transpose,
511 std::bool_constant<true>,
512 std::bool_constant<false>) const
513 {
514 if (transpose == true)
516 else
518 }
519
520
521
522 template <typename VectorType1, typename VectorType2>
523 inline void
524 BlockSparseMatrix::vmult(VectorType1 &dst,
525 const VectorType2 &src,
526 const bool transpose,
527 std::bool_constant<false>,
528 std::bool_constant<false>) const
529 {
530 if (transpose == true)
532 else
534 }
535
536
537
538 inline std::vector<IndexSet>
540 {
541 Assert(this->n_block_cols() != 0, ExcNotInitialized());
542 Assert(this->n_block_rows() != 0, ExcNotInitialized());
543
544 std::vector<IndexSet> domain_indices;
545 domain_indices.reserve(this->n_block_cols());
546 for (size_type c = 0; c < this->n_block_cols(); ++c)
547 domain_indices.push_back(
548 this->sub_objects[0][c]->locally_owned_domain_indices());
549
550 return domain_indices;
551 }
552
553
554
555 inline std::vector<IndexSet>
557 {
558 Assert(this->n_block_cols() != 0, ExcNotInitialized());
559 Assert(this->n_block_rows() != 0, ExcNotInitialized());
560
561 std::vector<IndexSet> range_indices;
562 range_indices.reserve(this->n_block_rows());
563 for (size_type r = 0; r < this->n_block_rows(); ++r)
564 range_indices.push_back(
565 this->sub_objects[r][0]->locally_owned_range_indices());
566
567 return range_indices;
568 }
569
570
571
572 namespace internal
573 {
574 namespace BlockLinearOperatorImplementation
575 {
590 template <typename PayloadBlockType>
592 {
593 public:
597 using BlockType = PayloadBlockType;
598
608 template <typename... Args>
609 TrilinosBlockPayload(const Args &...)
610 {
611 static_assert(
612 std::is_same_v<
613 PayloadBlockType,
615 "TrilinosBlockPayload can only accept a payload of type TrilinosPayload.");
616 }
617 };
618
619 } // namespace BlockLinearOperatorImplementation
620 } /* namespace internal */
621
622
623} /* namespace TrilinosWrappers */
624
625#endif
626
628
629#endif // dealii_trilinos_block_sparse_matrix_h
void Tvmult_nonblock_nonblock(VectorType &dst, const VectorType &src) const
void vmult_nonblock_nonblock(VectorType &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
unsigned int n_block_rows() const
MatrixIterator< BlockMatrixIterators::Accessor< BlockMatrixBase, true > > const_iterator
void vmult_block_block(BlockVectorType &dst, const BlockVectorType &src) const
types::global_dof_index size_type
typename BlockType::value_type value_type
void vmult_nonblock_block(VectorType &dst, const BlockVectorType &src) const
unsigned int n_block_cols() const
MatrixIterator< BlockMatrixIterators::Accessor< BlockMatrixBase, false > > iterator
BlockType & block(const unsigned int row, const unsigned int column)
void vmult_block_nonblock(BlockVectorType &dst, const VectorType &src) const
void Tvmult_block_nonblock(BlockVectorType &dst, const VectorType &src) const
BlockSparseMatrix & operator=(const BlockSparseMatrix &)=default
TrilinosScalar residual(MPI::BlockVector &dst, const MPI::BlockVector &x, const MPI::BlockVector &b) const
void Tvmult(VectorType1 &dst, const VectorType2 &src) const
std::vector< IndexSet > locally_owned_domain_indices() const
void reinit(const size_type n_block_rows, const size_type n_block_columns)
std::vector< IndexSet > locally_owned_range_indices() const
void vmult(VectorType1 &dst, const VectorType2 &src) const
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
DerivativeForm< 1, spacedim, dim, Number > transpose(const DerivativeForm< 1, dim, spacedim, Number > &DF)
static ::ExceptionBase & ExcIncompatibleRowNumbers(int arg1, int arg2, int arg3, int arg4)
#define DeclException4(Exception4, type1, type2, type3, type4, outsequence)
static ::ExceptionBase & ExcScalarAssignmentOnlyForZeroValue()
#define Assert(cond, exc)
static ::ExceptionBase & ExcIncompatibleColNumbers(int arg1, int arg2, int arg3, int arg4)
static ::ExceptionBase & ExcNotInitialized()
double TrilinosScalar
Definition types.h:188