| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2022 - 2025 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_sparsity_pattern_base_h |
| 16 |
#define dealii_sparsity_pattern_base_h |
| 17 |
|
| 18 |
|
| 19 |
#include <deal.II/base/config.h> |
| 20 |
|
| 21 |
#include <deal.II/base/array_view.h> |
| 22 |
#include <deal.II/base/enable_observer_pointer.h> |
| 23 |
#include <deal.II/base/exceptions.h> |
| 24 |
#include <deal.II/base/types.h> |
| 25 |
|
| 26 |
#include <utility> |
| 27 |
|
| 28 |
DEAL_II_NAMESPACE_OPEN |
| 29 |
|
| 30 |
/** |
| 31 |
* @addtogroup Sparsity |
| 32 |
* @{ |
| 33 |
*/ |
| 34 |
|
| 35 |
/** |
| 36 |
* Base class for all sparsity patterns, defining a common interface by which |
| 37 |
* new values can be added. |
| 38 |
*/ |
| 39 |
class SparsityPatternBase : public EnableObserverPointer |
| 40 |
{ |
| 41 |
public: |
| 42 |
/** |
| 43 |
* Declare type for container size. |
| 44 |
*/ |
| 45 |
using size_type = types::global_dof_index; |
| 46 |
|
| 47 |
/** |
| 48 |
* Constructor. Sets up an empty (zero-by-zero) sparsity pattern. |
| 49 |
*/ |
| 50 |
SparsityPatternBase(); |
| 51 |
|
| 52 |
/** |
| 53 |
* Constructor. Sets up a @p rows by @p cols sparsity pattern. |
| 54 |
*/ |
| 55 |
SparsityPatternBase(const size_type rows, const size_type cols); |
| 56 |
|
| 57 |
/** |
| 58 |
* Copy constructor. |
| 59 |
*/ |
| 60 |
SparsityPatternBase(const SparsityPatternBase &sparsity_pattern) = default; |
| 61 |
|
| 62 |
/** |
| 63 |
* Move constructor. |
| 64 |
*/ |
| 65 |
SparsityPatternBase(SparsityPatternBase &&sparsity_pattern) noexcept = |
| 66 |
default; |
| 67 |
|
| 68 |
/** |
| 69 |
* Assignment operator. |
| 70 |
*/ |
| 71 |
SparsityPatternBase & |
| 72 |
operator=(const SparsityPatternBase &sparsity_pattern) = default; |
| 73 |
|
| 74 |
/** |
| 75 |
* Move assignment operator. |
| 76 |
*/ |
| 77 |
SparsityPatternBase & |
| 78 |
operator=(SparsityPatternBase &&sparsity_pattern) noexcept = default; |
| 79 |
|
| 80 |
/** |
| 81 |
* Return number of rows of this matrix, which equals the dimension of the |
| 82 |
* image space. |
| 83 |
*/ |
| 84 |
size_type |
| 85 |
n_rows() const; |
| 86 |
|
| 87 |
/** |
| 88 |
* Return number of columns of this matrix, which equals the dimension of |
| 89 |
* the range space. |
| 90 |
*/ |
| 91 |
size_type |
| 92 |
n_cols() const; |
| 93 |
|
| 94 |
/** |
| 95 |
* Optimized function for adding new entries to a given row. |
| 96 |
*/ |
| 97 |
virtual void |
| 98 |
add_row_entries(const size_type &row, |
| 99 |
const ArrayView<const size_type> &columns, |
| 100 |
const bool indices_are_sorted = false) = 0; |
| 101 |
|
| 102 |
/** |
| 103 |
* General function for adding new entries from an unsorted list of pairs. |
| 104 |
* |
| 105 |
* This function is useful when multiple entries need to be added which do |
| 106 |
* not correspond to a particular row, e.g., when assembling a flux sparsity |
| 107 |
* pattern. |
| 108 |
*/ |
| 109 |
virtual void |
| 110 |
add_entries(const ArrayView<const std::pair<size_type, size_type>> &entries); |
| 111 |
|
| 112 |
protected: |
| 113 |
/** |
| 114 |
* Internal function for updating the stored size of the sparsity pattern. |
| 115 |
*/ |
| 116 |
virtual void |
| 117 |
resize(const size_type rows, const size_type cols); |
| 118 |
|
| 119 |
/** |
| 120 |
* Number of rows that this sparsity pattern shall represent. |
| 121 |
*/ |
| 122 |
size_type rows; |
| 123 |
|
| 124 |
/** |
| 125 |
* Number of columns that this sparsity pattern shall represent. |
| 126 |
*/ |
| 127 |
size_type cols; |
| 128 |
}; |
| 129 |
|
| 130 |
/** |
| 131 |
* @} |
| 132 |
*/ |
| 133 |
|
| 134 |
|
| 135 |
/* ---------------------------- Inline functions ---------------------------- */ |
| 136 |
|
| 137 |
#ifndef DOXYGEN |
| 138 |
|
| 139 |
inline SparsityPatternBase::SparsityPatternBase() |
| 140 |
: rows(0) |
| 141 |
, cols(0) |
| 142 |
{} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
inline SparsityPatternBase::SparsityPatternBase(const size_type rows, |
| 147 |
const size_type cols) |
| 148 |
: rows(rows) |
| 149 |
, cols(cols) |
| 150 |
{} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
inline SparsityPatternBase::size_type |
| 155 |
SparsityPatternBase::n_rows() const |
| 156 |
{ |
| 157 |
return rows; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
inline SparsityPatternBase::size_type |
| 163 |
SparsityPatternBase::n_cols() const |
| 164 |
{ |
| 165 |
return cols; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
inline void |
| 171 |
SparsityPatternBase::resize(const size_type n_rows, const size_type n_cols) |
| 172 |
{ |
| 173 |
this->rows = n_rows; |
| 174 |
this->cols = n_cols; |
| 175 |
} |
| 176 |
#endif |
| 177 |
|
| 178 |
DEAL_II_NAMESPACE_CLOSE |
| 179 |
|
| 180 |
#endif |