Line data Source code
1 : // The libMesh Finite Element Library. 2 : // Copyright (C) 2002-2025 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner 3 : 4 : // This library is free software; you can redistribute it and/or 5 : // modify it under the terms of the GNU Lesser General Public 6 : // License as published by the Free Software Foundation; either 7 : // version 2.1 of the License, or (at your option) any later version. 8 : 9 : // This library is distributed in the hope that it will be useful, 10 : // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 : // Lesser General Public License for more details. 13 : 14 : // You should have received a copy of the GNU Lesser General Public 15 : // License along with this library; if not, write to the Free Software 16 : // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 : 18 : #ifndef LIBMESH_DIAGONAL_MATRIX_H 19 : #define LIBMESH_DIAGONAL_MATRIX_H 20 : 21 : #include "libmesh/id_types.h" 22 : #include "libmesh/sparse_matrix.h" 23 : 24 : #include <memory> 25 : 26 : namespace libMesh 27 : { 28 : template <typename> 29 : class NumericVector; 30 : namespace Parallel 31 : { 32 : class Communicator; 33 : } 34 : 35 : /** 36 : * Diagonal matrix class whose underlying storage is a vector 37 : * 38 : * \author Alexander D. Lindsay 39 : * \date 2019 40 : */ 41 : template <typename T> 42 : class DiagonalMatrix : public SparseMatrix<T> 43 : { 44 : public: 45 : /** 46 : * Constructor; initializes the matrix to be empty, without any 47 : * structure, i.e. the matrix is not usable at all. This 48 : * constructor is therefore only useful for matrices which are 49 : * members of a class. All other matrices should be created at a 50 : * point in the data flow where all necessary information is 51 : * available. 52 : * 53 : * You have to initialize the matrix before usage with 54 : * \p init(...). 55 : */ 56 : explicit DiagonalMatrix(const Parallel::Communicator & comm); 57 : 58 : /** 59 : * This class does not manually manage any memory, so the destructor 60 : * can be safely defaulted. 61 : */ 62 213 : virtual ~DiagonalMatrix() = default; 63 : 64 0 : virtual SolverPackage solver_package() override 65 : { 66 0 : return DIAGONAL_MATRIX; 67 : } 68 : 69 : /** 70 : * unique pointers can be moved but not copied 71 : */ 72 : DiagonalMatrix(DiagonalMatrix &&) = default; 73 : DiagonalMatrix & operator=(DiagonalMatrix &&) = default; 74 : DiagonalMatrix & operator=(const DiagonalMatrix &) = delete; 75 0 : virtual SparseMatrix<T> & operator=(const SparseMatrix<T> &) override 76 : { 77 0 : libmesh_not_implemented(); 78 : return *this; 79 : } 80 : 81 : /** 82 : * Copy contents from vec into underlying diagonal storage 83 : */ 84 : DiagonalMatrix & operator=(const NumericVector<T> & vec); 85 : 86 : /** 87 : * Move contents from vec into underlying diagonal storage 88 : */ 89 : DiagonalMatrix & operator=(NumericVector<T> && vec); 90 : 91 : 92 : virtual 93 : void init(const numeric_index_type m, 94 : const numeric_index_type n, 95 : const numeric_index_type m_l, 96 : const numeric_index_type n_l, 97 : const numeric_index_type nnz = 30, 98 : const numeric_index_type noz = 10, 99 : const numeric_index_type blocksize = 1) override; 100 : 101 : virtual void init(ParallelType type = PARALLEL) override; 102 : 103 : /** 104 : * Initialize with a NumericVector \p other, e.g. duplicate the storage 105 : * allocation of \p other. This function DOES NOT copy the vector entries. 106 : * If you set fast = false, the initialized entries are explicitly zeroed, 107 : * otherwise their values are indeterminate. 108 : */ 109 : virtual void init(const NumericVector<T> & other, const bool fast = false); 110 : 111 : /** 112 : * Initialize with DiagonalMatrix \p other, e.g. duplicate the storage 113 : * allocation of the underlying NumericVector in \p other. This function 114 : * DOES NOT copy the vector entries. If you set fast = false, the 115 : * initialized entries are explicitly zeroed, otherwise their values 116 : * are indeterminate. 117 : */ 118 : virtual void init(const DiagonalMatrix<T> & other, const bool fast = false); 119 : 120 : virtual void clear() override; 121 : 122 : virtual void zero() override; 123 : 124 : virtual std::unique_ptr<SparseMatrix<T>> zero_clone () const override; 125 : 126 : virtual std::unique_ptr<SparseMatrix<T>> clone () const override; 127 : 128 : virtual void close() override; 129 : 130 : virtual numeric_index_type m() const override; 131 : 132 : virtual numeric_index_type n() const override; 133 : 134 : virtual numeric_index_type row_start() const override; 135 : 136 : virtual numeric_index_type row_stop() const override; 137 : 138 : virtual numeric_index_type col_start() const override; 139 : 140 : virtual numeric_index_type col_stop() const override; 141 : 142 : virtual void set(const numeric_index_type i, const numeric_index_type j, const T value) override; 143 : 144 : virtual void add(const numeric_index_type i, const numeric_index_type j, const T value) override; 145 : 146 : virtual 147 : void add_matrix(const DenseMatrix<T> & dm, 148 : const std::vector<numeric_index_type> & rows, 149 : const std::vector<numeric_index_type> & cols) override; 150 : 151 : virtual 152 : void add_matrix(const DenseMatrix<T> & dm, 153 : const std::vector<numeric_index_type> & dof_indices) override; 154 : 155 : virtual void add(const T a, const SparseMatrix<T> & X) override; 156 : 157 : virtual T operator()(const numeric_index_type i, const numeric_index_type j) const override; 158 : 159 : virtual Real l1_norm() const override; 160 : 161 : virtual Real linfty_norm() const override; 162 : 163 : virtual bool closed() const override; 164 : 165 : virtual void print_personal(std::ostream & os = libMesh::out) const override; 166 : 167 : virtual void get_diagonal(NumericVector<T> & dest) const override; 168 : 169 : virtual void get_transpose(SparseMatrix<T> & dest) const override; 170 : 171 : virtual void get_row(numeric_index_type i, 172 : std::vector<numeric_index_type> & indices, 173 : std::vector<T> & values) const override; 174 : 175 : virtual void zero_rows(std::vector<numeric_index_type> & rows, T val = 0) override; 176 : 177 : const NumericVector<T> & diagonal() const; 178 : 179 : virtual void restore_original_nonzero_pattern() override; 180 : 181 : protected: 182 : /// Underlying diagonal matrix storage 183 : std::unique_ptr<NumericVector<T>> _diagonal; 184 : }; 185 : } // namespace libMesh 186 : 187 : #endif // LIBMESH_DIAGONAL_MATRIX_H