GCC Code Coverage Report


Directory: ../../../builds/dumux-repositories/
File: /builds/dumux-repositories/dumux/dumux/linear/symmetrize_constraints.hh
Date: 2024-05-04 19:09:25
Exec Total Coverage
Lines: 33 33 100.0%
Functions: 11 17 64.7%
Branches: 46 82 56.1%

Line Branch Exec Source
1 // -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 // vi: set et ts=4 sw=4 sts=4:
3 //
4 // SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5 // SPDX-License-Identifier: GPL-3.0-or-later
6 //
7 /*!
8 * \file
9 * \ingroup Linear
10 * \brief Helper function to symmetrize row-constraints in constrained linear systems
11 */
12 #ifndef DUMUX_LINEAR_SYMMETRIZE_CONSTRAINTS_HH
13 #define DUMUX_LINEAR_SYMMETRIZE_CONSTRAINTS_HH
14
15 #include <iostream>
16
17 #include <dune/common/hybridutilities.hh>
18 #include <dune/common/fmatrix.hh>
19 #include <dune/istl/bcrsmatrix.hh>
20 #include <dune/istl/multitypeblockmatrix.hh>
21
22 namespace Dumux::Detail {
23
24 // loop over dense matrix block: end of recursion (actually do some work)
25 template<class K, int rows, int cols, class VectorRow, class VectorCol>
26 void symmetrizeConstraintsImpl(Dune::FieldMatrix<K, rows, cols>& A,
27 VectorRow& bRow, const VectorCol& bCol,
28 const VectorCol& constrainedRows,
29 bool isDiagonal)
30 {
31
6/8
✓ Branch 0 taken 217451 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 552058 times.
✓ Branch 3 taken 276029 times.
✓ Branch 4 taken 276029 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1044856 times.
✓ Branch 7 taken 522428 times.
2888851 for (int j = 0; j < A.M(); ++j)
32 {
33 // all rows in this column can be eliminated by a Dirichlet row
34
6/8
✗ Branch 0 not taken.
✓ Branch 1 taken 217451 times.
✓ Branch 2 taken 7686 times.
✓ Branch 3 taken 544372 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 276029 times.
✓ Branch 6 taken 14094 times.
✓ Branch 7 taken 1030762 times.
2090394 const auto& constrainedRowsJ = constrainedRows[j];
35
6/8
✗ Branch 0 not taken.
✓ Branch 1 taken 217451 times.
✓ Branch 2 taken 7686 times.
✓ Branch 3 taken 544372 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 276029 times.
✓ Branch 6 taken 14094 times.
✓ Branch 7 taken 1030762 times.
2090394 if (constrainedRowsJ > 0.5)
36 {
37
4/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 7686 times.
✓ Branch 3 taken 7686 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 28188 times.
✓ Branch 7 taken 14094 times.
57654 for (int i = 0; i < A.N(); ++i)
38 {
39
6/16
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 7686 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 7686 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✓ Branch 12 taken 25626 times.
✓ Branch 13 taken 2562 times.
✓ Branch 14 taken 25626 times.
✓ Branch 15 taken 2562 times.
71748 auto& Aij = A[i][j];
40
3/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 7686 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 25626 times.
✓ Branch 7 taken 2562 times.
35874 auto& bi = bRow[i];
41
3/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 7686 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 25626 times.
✓ Branch 7 taken 2562 times.
35874 const auto& bj = bCol[j];
42
3/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 7686 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 25626 times.
✓ Branch 7 taken 2562 times.
35874 if (!((i == j) && isDiagonal))
43 {
44 33312 bi -= Aij*bj;
45 33312 Aij = 0.0;
46 }
47 }
48 }
49 }
50 }
51
52 // recursively loop over sub-blocks
53 template<class MBlock, class VectorRow, class VectorCol>
54 16 void symmetrizeConstraintsImpl(Dune::BCRSMatrix<MBlock>& A,
55 VectorRow& bRow, const VectorCol& bCol,
56 const VectorCol& constrainedRows,
57 bool isDiagonal)
58 {
59 16 const auto rowEnd = A.end();
60
4/4
✓ Branch 0 taken 262578 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 262578 times.
✓ Branch 3 taken 8 times.
525172 for (auto row = A.begin(); row != rowEnd; ++row)
61 {
62 1050312 const auto colEnd = row->end();
63
4/4
✓ Branch 0 taken 1291937 times.
✓ Branch 1 taken 262578 times.
✓ Branch 2 taken 1291937 times.
✓ Branch 3 taken 262578 times.
6218060 for (auto col = row->begin(); col != colEnd; ++col)
64 {
65 2583874 const auto i = row.index();
66 2583874 const auto j = col.index();
67
68 5167748 auto& Aij = A[i][j];
69 2583874 auto& bi = bRow[i];
70 2583874 const auto& bj = bCol[j];
71 2583874 const auto& dj = constrainedRows[j];
72
73 5167748 symmetrizeConstraintsImpl(Aij, bi, bj, dj, ((i == j) && isDiagonal));
74 }
75 }
76 16 }
77
78 // recursively loop over sub-blocks
79 template<class... MBlock, class VectorRow, class VectorCol>
80 2 void symmetrizeConstraintsImpl(Dune::MultiTypeBlockMatrix<MBlock...>& A,
81 VectorRow& bRow, const VectorCol& bCol,
82 const VectorCol& constrainedRows,
83 bool isDiagonal)
84 {
85 using namespace Dune::Hybrid;
86
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
18 forEach(std::make_index_sequence<Dune::MultiTypeBlockMatrix<MBlock...>::N()>{}, [&](const auto i)
87 {
88 32 forEach(std::make_index_sequence<Dune::MultiTypeBlockMatrix<MBlock...>::M()>{}, [&](const auto j)
89 {
90 8 auto& Aij = A[i][j];
91 4 auto& bi = bRow[i];
92 4 const auto& bj = bCol[j];
93 8 const auto& dj = constrainedRows[j];
94
95 4 symmetrizeConstraintsImpl(Aij, bi, bj, dj, ((i == j) && isDiagonal));
96 });
97 });
98 2 }
99
100 } // end namespace Dumux::Detail
101
102 namespace Dumux {
103
104 /*!
105 * \ingroup Linear
106 * \brief Symmetrize the constrained system Ax=b
107 * \note The function addresses the case where some rows in the linear system have been replaced
108 * to fix a degree of freedom to a fixed value. A typical use case is strongly enforced Dirichlet constraints.
109 * To symmetrize the constraints in operator A, this function eliminates the corresponding column entries by
110 * bringing them to the right-hand side. Particularly, if matrix operator A was symmetric before incorporating
111 * constraints, calling this function restores the symmetry of operator A.
112 * \note the constrainedRows vector is the same shape and type as b and contains 1 for each row
113 * that corresponds to constraints (e.g. strongly enforced Dirichlet dofs) and 0 elsewhere
114 * \note this is a specialization for Dune::BCRSMatrix
115 */
116 template<class MBlock, class Vector>
117 void symmetrizeConstraints(Dune::BCRSMatrix<MBlock>& A, Vector& b, const Vector& constrainedRows)
118 {
119 Detail::symmetrizeConstraintsImpl(A, b, b, constrainedRows, true);
120 }
121
122 /*!
123 * \ingroup Linear
124 * \brief Symmetrize the constrained system Ax=b
125 * \note The function addresses the case where some rows in the linear system have been replaced
126 * to fix a degree of freedom to a fixed value. A typical use case is strongly enforced Dirichlet constraints.
127 * To symmetrize the constraints in operator A, this function eliminates the corresponding column entries by
128 * bringing them to the right-hand side. Particularly, if matrix operator A was symmetric before incorporating
129 * constraints, calling this function restores the symmetry of operator A.
130 * \note the constrainedRows vector is the same shape and type as b and contains 1 for each row
131 * that corresponds to constraints (e.g. strongly enforced Dirichlet dofs) and 0 elsewhere
132 * \note this is a specialization for Dune::MultiTypeBlockMatrix
133 */
134 template<class... MBlock, class Vector>
135 void symmetrizeConstraints(Dune::MultiTypeBlockMatrix<MBlock...>& A, Vector& b, const Vector& constrainedRows)
136 {
137 2 Detail::symmetrizeConstraintsImpl(A, b, b, constrainedRows, true);
138 }
139
140 } // end namespace Dumux
141
142 #endif
143