| 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 |
|
| 16 |
#ifndef dealii_sparse_decomposition_templates_h |
| 17 |
#define dealii_sparse_decomposition_templates_h |
| 18 |
|
| 19 |
|
| 20 |
#include <deal.II/base/config.h> |
| 21 |
|
| 22 |
#include <deal.II/base/memory_consumption.h> |
| 23 |
#include <deal.II/base/template_constraints.h> |
| 24 |
#include <deal.II/base/utilities.h> |
| 25 |
|
| 26 |
#include <deal.II/lac/sparse_decomposition.h> |
| 27 |
|
| 28 |
#include <algorithm> |
| 29 |
#include <cstring> |
| 30 |
|
| 31 |
DEAL_II_NAMESPACE_OPEN |
| 32 |
|
| 33 |
template <typename number> |
| 34 |
SparseLUDecomposition<number>::SparseLUDecomposition() |
| 35 |
: SparseMatrix<number>() |
| 36 |
, strengthen_diagonal(0) |
| 37 |
, own_sparsity(nullptr) |
| 38 |
{} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
template <typename number> |
| 43 |
SparseLUDecomposition<number>::~SparseLUDecomposition() |
| 44 |
{ |
| 45 |
// virtual functions called in constructors and destructors never use the |
| 46 |
// override in a derived class |
| 47 |
// for clarity be explicit on which function is called |
| 48 |
SparseLUDecomposition<number>::clear(); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
template <typename number> |
| 53 |
void |
| 54 |
SparseLUDecomposition<number>::clear() |
| 55 |
{ |
| 56 |
std::vector<const size_type *> tmp; |
| 57 |
tmp.swap(prebuilt_lower_bound); |
| 58 |
|
| 59 |
SparseMatrix<number>::clear(); |
| 60 |
|
| 61 |
if (own_sparsity != nullptr) |
| 62 |
{ |
| 63 |
delete own_sparsity; |
| 64 |
own_sparsity = nullptr; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
template <typename number> |
| 71 |
template <typename somenumber> |
| 72 |
void |
| 73 |
SparseLUDecomposition<number>::initialize( |
| 74 |
const SparseMatrix<somenumber> &matrix, |
| 75 |
const AdditionalData data) |
| 76 |
{ |
| 77 |
const SparsityPattern &matrix_sparsity = matrix.get_sparsity_pattern(); |
| 78 |
|
| 79 |
const SparsityPattern *sparsity_pattern_to_use = nullptr; |
| 80 |
|
| 81 |
if (data.use_this_sparsity) |
| 82 |
sparsity_pattern_to_use = data.use_this_sparsity; |
| 83 |
else if (data.use_previous_sparsity && !this->empty() && |
| 84 |
(this->m() == matrix.m())) |
| 85 |
{ |
| 86 |
// Use the sparsity that was |
| 87 |
// previously used. This is |
| 88 |
// particularly useful when |
| 89 |
// matrix entries change but |
| 90 |
// not the sparsity, as for the |
| 91 |
// case of several Newton |
| 92 |
// iteration steps on an |
| 93 |
// unchanged grid. |
| 94 |
sparsity_pattern_to_use = &this->get_sparsity_pattern(); |
| 95 |
} |
| 96 |
else if (data.extra_off_diagonals == 0) |
| 97 |
{ |
| 98 |
// Use same sparsity as matrix |
| 99 |
sparsity_pattern_to_use = &matrix_sparsity; |
| 100 |
} |
| 101 |
else |
| 102 |
{ |
| 103 |
// Create new sparsity |
| 104 |
|
| 105 |
// for the case that |
| 106 |
// own_sparsity wasn't deleted |
| 107 |
// before (e.g. by clear()), do |
| 108 |
// it here |
| 109 |
if (own_sparsity) |
| 110 |
{ |
| 111 |
// release the sparsity |
| 112 |
SparseMatrix<number>::clear(); |
| 113 |
// delete it |
| 114 |
delete own_sparsity; |
| 115 |
} |
| 116 |
|
| 117 |
// and recreate |
| 118 |
own_sparsity = new SparsityPattern(matrix_sparsity, |
| 119 |
matrix_sparsity.max_entries_per_row() + |
| 120 |
2 * data.extra_off_diagonals, |
| 121 |
data.extra_off_diagonals); |
| 122 |
own_sparsity->compress(); |
| 123 |
sparsity_pattern_to_use = own_sparsity; |
| 124 |
} |
| 125 |
|
| 126 |
// now use this sparsity pattern |
| 127 |
Assert(sparsity_pattern_to_use->n_rows() == sparsity_pattern_to_use->n_cols(), |
| 128 |
ExcMessage( |
| 129 |
"It is not possible to compute this matrix decomposition for " |
| 130 |
"matrices that are not square.")); |
| 131 |
{ |
| 132 |
std::vector<const size_type *> tmp; |
| 133 |
tmp.swap(prebuilt_lower_bound); |
| 134 |
} |
| 135 |
SparseMatrix<number>::reinit(*sparsity_pattern_to_use); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
template <typename number> |
| 141 |
void |
| 142 |
SparseLUDecomposition<number>::prebuild_lower_bound() |
| 143 |
{ |
| 144 |
const size_type *const column_numbers = |
| 145 |
this->get_sparsity_pattern().colnums.get(); |
| 146 |
const std::size_t *const rowstart_indices = |
| 147 |
this->get_sparsity_pattern().rowstart.get(); |
| 148 |
const size_type N = this->m(); |
| 149 |
|
| 150 |
prebuilt_lower_bound.resize(N); |
| 151 |
|
| 152 |
for (size_type row = 0; row < N; ++row) |
| 153 |
{ |
| 154 |
prebuilt_lower_bound[row] = |
| 155 |
Utilities::lower_bound(&column_numbers[rowstart_indices[row] + 1], |
| 156 |
&column_numbers[rowstart_indices[row + 1]], |
| 157 |
row); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
template <typename number> |
| 162 |
template <typename somenumber> |
| 163 |
void |
| 164 |
SparseLUDecomposition<number>::copy_from(const SparseMatrix<somenumber> &matrix) |
| 165 |
{ |
| 166 |
// check whether we use the same sparsity |
| 167 |
// pattern as the input matrix |
| 168 |
if (&this->get_sparsity_pattern() == &matrix.get_sparsity_pattern()) |
| 169 |
{ |
| 170 |
const somenumber *input_ptr = matrix.val.get(); |
| 171 |
number *this_ptr = this->val.get(); |
| 172 |
const number *const end_ptr = this_ptr + this->n_nonzero_elements(); |
| 173 |
if (std::is_same_v<somenumber, number> == true) |
| 174 |
std::memcpy(this_ptr, |
| 175 |
input_ptr, |
| 176 |
this->n_nonzero_elements() * sizeof(number)); |
| 177 |
else |
| 178 |
for (; this_ptr != end_ptr; ++input_ptr, ++this_ptr) |
| 179 |
*this_ptr = *input_ptr; |
| 180 |
return; |
| 181 |
} |
| 182 |
|
| 183 |
// preset the elements by zero. this needs to be written in a slightly |
| 184 |
// awkward way so that we find the corresponding function in the base class. |
| 185 |
SparseMatrix<number>::operator=(number(0)); |
| 186 |
|
| 187 |
// both allow more and less entries in the new matrix |
| 188 |
for (size_type row = 0; row < this->m(); ++row) |
| 189 |
{ |
| 190 |
typename SparseMatrix<number>::iterator index = this->begin(row); |
| 191 |
typename SparseMatrix<somenumber>::const_iterator in_index = |
| 192 |
matrix.begin(row); |
| 193 |
index->value() = in_index->value(); |
| 194 |
++index, ++in_index; |
| 195 |
while (index < this->end(row) && in_index < matrix.end(row)) |
| 196 |
{ |
| 197 |
while (index->column() < in_index->column() && index < this->end(row)) |
| 198 |
++index; |
| 199 |
while (in_index->column() < index->column() && |
| 200 |
in_index < matrix.end(row)) |
| 201 |
++in_index; |
| 202 |
|
| 203 |
index->value() = in_index->value(); |
| 204 |
++index, ++in_index; |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
template <typename number> |
| 212 |
void |
| 213 |
SparseLUDecomposition<number>::strengthen_diagonal_impl() |
| 214 |
{ |
| 215 |
for (size_type row = 0; row < this->m(); ++row) |
| 216 |
{ |
| 217 |
// get the global index of the first |
| 218 |
// non-diagonal element in this row |
| 219 |
Assert(this->m() == this->n(), ExcNotImplemented()); |
| 220 |
typename SparseMatrix<number>::iterator diagonal_element = |
| 221 |
this->begin(row); |
| 222 |
|
| 223 |
number rowsum = 0; |
| 224 |
for (typename SparseMatrix<number>::iterator p = diagonal_element + 1; |
| 225 |
p != this->end(row); |
| 226 |
++p) |
| 227 |
rowsum += std::fabs(p->value()); |
| 228 |
|
| 229 |
diagonal_element->value() += |
| 230 |
this->get_strengthen_diagonal(rowsum, row) * rowsum; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
template <typename number> |
| 237 |
std::size_t |
| 238 |
SparseLUDecomposition<number>::memory_consumption() const |
| 239 |
{ |
| 240 |
return (SparseMatrix<number>::memory_consumption() + |
| 241 |
MemoryConsumption::memory_consumption(prebuilt_lower_bound)); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
DEAL_II_NAMESPACE_CLOSE |
| 246 |
|
| 247 |
#endif // dealii_sparse_decomposition_templates_h |