| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2000 - 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_block_sparse_matrix_templates_h |
| 16 |
#define dealii_block_sparse_matrix_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/block_sparse_matrix.h> |
| 24 |
|
| 25 |
DEAL_II_NAMESPACE_OPEN |
| 26 |
|
| 27 |
template <typename number> |
| 28 |
BlockSparseMatrix<number>::BlockSparseMatrix( |
| 29 |
const BlockSparsityPattern &sparsity) |
| 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 |
BlockSparseMatrix<number>::reinit(sparsity); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
template <typename number> |
| 40 |
BlockSparseMatrix<number>::~BlockSparseMatrix() |
| 41 |
{ |
| 42 |
// delete previous content of |
| 43 |
// the subobjects array |
| 44 |
try |
| 45 |
{ |
| 46 |
clear(); |
| 47 |
} |
| 48 |
catch (...) |
| 49 |
{} |
| 50 |
sparsity_pattern = nullptr; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
template <typename number> |
| 56 |
BlockSparseMatrix<number> & |
| 57 |
BlockSparseMatrix<number>::operator=(const BlockSparseMatrix<number> &m) |
| 58 |
{ |
| 59 |
Assert(this->row_block_indices == m.row_block_indices, |
| 60 |
ExcBlockDimensionMismatch()); |
| 61 |
Assert(this->column_block_indices == m.column_block_indices, |
| 62 |
ExcBlockDimensionMismatch()); |
| 63 |
|
| 64 |
// this operator does not do |
| 65 |
// anything except than checking |
| 66 |
// whether the base objects want to |
| 67 |
// do something |
| 68 |
for (size_type r = 0; r < this->n_block_rows(); ++r) |
| 69 |
for (size_type c = 0; c < this->n_block_cols(); ++c) |
| 70 |
this->block(r, c) = m.block(r, c); |
| 71 |
|
| 72 |
return *this; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
template <typename number> |
| 78 |
void |
| 79 |
BlockSparseMatrix<number>::clear() |
| 80 |
{ |
| 81 |
BlockMatrixBase<SparseMatrix<number>>::clear(); |
| 82 |
sparsity_pattern = nullptr; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
template <typename number> |
| 88 |
void |
| 89 |
BlockSparseMatrix<number>::reinit(const BlockSparsityPattern &sparsity) |
| 90 |
{ |
| 91 |
// first delete previous content of |
| 92 |
// the subobjects array and delete |
| 93 |
// the table completely |
| 94 |
clear(); |
| 95 |
|
| 96 |
// then associate new sparsity |
| 97 |
// pattern and resize |
| 98 |
sparsity_pattern = &sparsity; |
| 99 |
|
| 100 |
this->row_block_indices = sparsity.row_indices; |
| 101 |
this->column_block_indices = sparsity.column_indices; |
| 102 |
|
| 103 |
this->sub_objects.reinit(sparsity.n_block_rows(), sparsity.n_block_cols()); |
| 104 |
|
| 105 |
// and reinitialize the blocks |
| 106 |
for (size_type r = 0; r < this->n_block_rows(); ++r) |
| 107 |
for (size_type c = 0; c < this->n_block_cols(); ++c) |
| 108 |
{ |
| 109 |
BlockType *p = new SparseMatrix<number>(); |
| 110 |
p->reinit(sparsity.block(r, c)); |
| 111 |
this->sub_objects[r][c] = p; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
template <typename number> |
| 118 |
bool |
| 119 |
BlockSparseMatrix<number>::empty() const |
| 120 |
{ |
| 121 |
for (size_type r = 0; r < this->n_block_rows(); ++r) |
| 122 |
for (size_type c = 0; c < this->n_block_cols(); ++c) |
| 123 |
if (this->block(r, c).empty() == false) |
| 124 |
return false; |
| 125 |
|
| 126 |
return true; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
template <typename number> |
| 132 |
typename BlockSparseMatrix<number>::size_type |
| 133 |
BlockSparseMatrix<number>::get_row_length(const size_type row) const |
| 134 |
{ |
| 135 |
return sparsity_pattern->row_length(row); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
template <typename number> |
| 141 |
typename BlockSparseMatrix<number>::size_type |
| 142 |
BlockSparseMatrix<number>::n_nonzero_elements() const |
| 143 |
{ |
| 144 |
return sparsity_pattern->n_nonzero_elements(); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
template <typename number> |
| 150 |
typename BlockSparseMatrix<number>::size_type |
| 151 |
BlockSparseMatrix<number>::n_actually_nonzero_elements( |
| 152 |
const double threshold) const |
| 153 |
{ |
| 154 |
size_type count = 0; |
| 155 |
for (size_type i = 0; i < this->n_block_rows(); ++i) |
| 156 |
for (size_type j = 0; j < this->n_block_cols(); ++j) |
| 157 |
count += this->sub_objects[i][j]->n_actually_nonzero_elements(threshold); |
| 158 |
|
| 159 |
return count; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
template <typename number> |
| 165 |
const BlockSparsityPattern & |
| 166 |
BlockSparseMatrix<number>::get_sparsity_pattern() const |
| 167 |
{ |
| 168 |
return *sparsity_pattern; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
template <typename number> |
| 174 |
void |
| 175 |
BlockSparseMatrix<number>::print_formatted(std::ostream &out, |
| 176 |
const unsigned int precision, |
| 177 |
const bool scientific, |
| 178 |
const unsigned int width, |
| 179 |
const char *zero_string, |
| 180 |
const double denominator, |
| 181 |
const char *separator) const |
| 182 |
{ |
| 183 |
for (size_type r = 0; r < this->n_block_rows(); ++r) |
| 184 |
for (size_type c = 0; c < this->n_block_cols(); ++c) |
| 185 |
{ |
| 186 |
out << "Component (" << r << ',' << c << ')' << std::endl; |
| 187 |
this->block(r, c).print_formatted(out, |
| 188 |
precision, |
| 189 |
scientific, |
| 190 |
width, |
| 191 |
zero_string, |
| 192 |
denominator, |
| 193 |
separator); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
template <typename number> |
| 200 |
std::size_t |
| 201 |
BlockSparseMatrix<number>::memory_consumption() const |
| 202 |
{ |
| 203 |
std::size_t mem = sizeof(*this); |
| 204 |
mem += MemoryConsumption::memory_consumption(this->sub_objects); |
| 205 |
for (size_type r = 0; r < this->n_block_rows(); ++r) |
| 206 |
for (size_type c = 0; c < this->n_block_cols(); ++c) |
| 207 |
mem += MemoryConsumption::memory_consumption(*this->sub_objects[r][c]); |
| 208 |
|
| 209 |
return mem; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
DEAL_II_NAMESPACE_CLOSE |
| 215 |
|
| 216 |
#endif // ifdef block_sparse_matrix_templates_h |