| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2002 - 2023 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
#ifndef dealii_sparse_mic_templates_h |
| 16 |
#define dealii_sparse_mic_templates_h |
| 17 |
|
| 18 |
|
| 19 |
#include <deal.II/base/config.h> |
| 20 |
|
| 21 |
#include <deal.II/base/memory_consumption.h> |
| 22 |
|
| 23 |
#include <deal.II/lac/sparse_mic.h> |
| 24 |
#include <deal.II/lac/vector.h> |
| 25 |
|
| 26 |
DEAL_II_NAMESPACE_OPEN |
| 27 |
|
| 28 |
template <typename number> |
| 29 |
SparseMIC<number>::~SparseMIC() |
| 30 |
{ |
| 31 |
// virtual functions called in constructors and destructors never use the |
| 32 |
// override in a derived class |
| 33 |
// for clarity be explicit on which function is called |
| 34 |
SparseMIC<number>::clear(); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
template <typename number> |
| 39 |
void |
| 40 |
SparseMIC<number>::clear() |
| 41 |
{ |
| 42 |
{ |
| 43 |
std::vector<number> tmp; |
| 44 |
tmp.swap(diag); |
| 45 |
} |
| 46 |
{ |
| 47 |
std::vector<number> tmp; |
| 48 |
tmp.swap(inv_diag); |
| 49 |
} |
| 50 |
{ |
| 51 |
std::vector<number> tmp; |
| 52 |
tmp.swap(inner_sums); |
| 53 |
} |
| 54 |
|
| 55 |
SparseLUDecomposition<number>::clear(); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
template <typename number> |
| 60 |
template <typename somenumber> |
| 61 |
inline void |
| 62 |
SparseMIC<number>::initialize(const SparseMatrix<somenumber> &matrix, |
| 63 |
const AdditionalData &data) |
| 64 |
{ |
| 65 |
Assert(matrix.m() == matrix.n(), ExcNotQuadratic()); |
| 66 |
Assert(data.strengthen_diagonal >= 0, |
| 67 |
ExcInvalidStrengthening(data.strengthen_diagonal)); |
| 68 |
|
| 69 |
SparseLUDecomposition<number>::initialize(matrix, data); |
| 70 |
this->strengthen_diagonal = data.strengthen_diagonal; |
| 71 |
this->prebuild_lower_bound(); |
| 72 |
this->copy_from(matrix); |
| 73 |
|
| 74 |
Assert(this->m() == this->n(), ExcNotQuadratic()); |
| 75 |
Assert(matrix.m() == this->m(), ExcDimensionMismatch(matrix.m(), this->m())); |
| 76 |
|
| 77 |
if (data.strengthen_diagonal > 0) |
| 78 |
this->strengthen_diagonal_impl(); |
| 79 |
|
| 80 |
// MIC implementation: (S. Margenov lectures) |
| 81 |
// x[i] = a[i][i] - sum(k=1, i-1, |
| 82 |
// a[i][k]/x[k]*sum(j=k+1, N, a[k][j])) |
| 83 |
|
| 84 |
// TODO: for sake of simplicity, |
| 85 |
// those are placed here. A better |
| 86 |
// implementation would store this |
| 87 |
// values in the underlying sparse |
| 88 |
// matrix itself. |
| 89 |
diag.resize(this->m()); |
| 90 |
inv_diag.resize(this->m()); |
| 91 |
inner_sums.resize(this->m()); |
| 92 |
|
| 93 |
// precalc sum(j=k+1, N, a[k][j])) |
| 94 |
for (size_type row = 0; row < this->m(); ++row) |
| 95 |
inner_sums[row] = get_rowsum(row); |
| 96 |
|
| 97 |
for (size_type row = 0; row < this->m(); ++row) |
| 98 |
{ |
| 99 |
const number temp = this->begin(row)->value(); |
| 100 |
number temp1 = 0; |
| 101 |
|
| 102 |
// work on the lower left part of the matrix. we know |
| 103 |
// it's symmetric, so we can work with this alone |
| 104 |
for (typename SparseMatrix<somenumber>::const_iterator p = |
| 105 |
matrix.begin(row) + 1; |
| 106 |
(p != matrix.end(row)) && (p->column() < row); |
| 107 |
++p) |
| 108 |
temp1 += p->value() / diag[p->column()] * inner_sums[p->column()]; |
| 109 |
|
| 110 |
Assert(temp - temp1 > 0, ExcStrengthenDiagonalTooSmall()); |
| 111 |
diag[row] = temp - temp1; |
| 112 |
|
| 113 |
inv_diag[row] = 1.0 / diag[row]; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
template <typename number> |
| 120 |
inline number |
| 121 |
SparseMIC<number>::get_rowsum(const size_type row) const |
| 122 |
{ |
| 123 |
Assert(this->m() == this->n(), ExcNotQuadratic()); |
| 124 |
|
| 125 |
number rowsum = 0; |
| 126 |
for (typename SparseMatrix<number>::const_iterator p = this->begin(row) + 1; |
| 127 |
p != this->end(row); |
| 128 |
++p) |
| 129 |
if (p->column() > row) |
| 130 |
rowsum += p->value(); |
| 131 |
|
| 132 |
return rowsum; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
template <typename number> |
| 138 |
template <typename somenumber> |
| 139 |
void |
| 140 |
SparseMIC<number>::vmult(Vector<somenumber> &dst, |
| 141 |
const Vector<somenumber> &src) const |
| 142 |
{ |
| 143 |
Assert(dst.size() == src.size(), |
| 144 |
ExcDimensionMismatch(dst.size(), src.size())); |
| 145 |
Assert(dst.size() == this->m(), ExcDimensionMismatch(dst.size(), this->m())); |
| 146 |
|
| 147 |
const size_type N = dst.size(); |
| 148 |
// We assume the underlying matrix A is: A = X - L - U, where -L and -U are |
| 149 |
// strictly lower- and upper- diagonal parts of the system. |
| 150 |
// |
| 151 |
// Solve (X-L)X{-1}(X-U) x = b in 3 steps: |
| 152 |
dst = src; |
| 153 |
for (size_type row = 0; row < N; ++row) |
| 154 |
{ |
| 155 |
// Now: (X-L)u = b |
| 156 |
|
| 157 |
// get start of this row. skip |
| 158 |
// the diagonal element |
| 159 |
for (typename SparseMatrix<number>::const_iterator p = |
| 160 |
this->begin(row) + 1; |
| 161 |
(p != this->end(row)) && (p->column() < row); |
| 162 |
++p) |
| 163 |
dst(row) -= p->value() * dst(p->column()); |
| 164 |
|
| 165 |
dst(row) *= inv_diag[row]; |
| 166 |
} |
| 167 |
|
| 168 |
// Now: v = Xu |
| 169 |
for (size_type row = 0; row < N; ++row) |
| 170 |
dst(row) *= diag[row]; |
| 171 |
|
| 172 |
// x = (X-U)v |
| 173 |
for (int row = N - 1; row >= 0; --row) |
| 174 |
{ |
| 175 |
// get end of this row |
| 176 |
for (typename SparseMatrix<number>::const_iterator p = |
| 177 |
this->begin(row) + 1; |
| 178 |
p != this->end(row); |
| 179 |
++p) |
| 180 |
if (p->column() > static_cast<size_type>(row)) |
| 181 |
dst(row) -= p->value() * dst(p->column()); |
| 182 |
|
| 183 |
dst(row) *= inv_diag[row]; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
// Exists for full compatibility with the LinearOperator class |
| 189 |
template <typename number> |
| 190 |
template <typename somenumber> |
| 191 |
void |
| 192 |
SparseMIC<number>::Tvmult(Vector<somenumber> & /*dst*/, |
| 193 |
const Vector<somenumber> & /*src*/) const |
| 194 |
{ |
| 195 |
AssertThrow(false, ExcNotImplemented()); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
template <typename number> |
| 201 |
std::size_t |
| 202 |
SparseMIC<number>::memory_consumption() const |
| 203 |
{ |
| 204 |
return (SparseLUDecomposition<number>::memory_consumption() + |
| 205 |
MemoryConsumption::memory_consumption(diag) + |
| 206 |
MemoryConsumption::memory_consumption(inv_diag) + |
| 207 |
MemoryConsumption::memory_consumption(inner_sums)); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
DEAL_II_NAMESPACE_CLOSE |
| 213 |
|
| 214 |
#endif // dealii_sparse_mic_templates_h |