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 : 19 : 20 : #ifndef LIBMESH_EIGEN_SPARSE_MATRIX_H 21 : #define LIBMESH_EIGEN_SPARSE_MATRIX_H 22 : 23 : #include "libmesh/libmesh_config.h" 24 : 25 : #ifdef LIBMESH_HAVE_EIGEN 26 : 27 : // Local includes 28 : #include "libmesh/sparse_matrix.h" 29 : #include "libmesh/eigen_core_support.h" 30 : 31 : // Eigen includes 32 : 33 : // C++ includes 34 : #include <algorithm> 35 : #include <cstddef> 36 : 37 : namespace libMesh 38 : { 39 : 40 : // Forward declarations 41 : template <typename T> class DenseMatrix; 42 : template <typename T> class EigenSparseVector; 43 : template <typename T> class EigenSparseLinearSolver; 44 : 45 : /** 46 : * The EigenSparseMatrix class wraps a sparse matrix object from the 47 : * Eigen library. All overridden virtual functions are documented in 48 : * sparse_matrix.h. 49 : * 50 : * \author Benjamin S. Kirk 51 : * \date 2013 52 : */ 53 : template <typename T> 54 0 : class EigenSparseMatrix final : public SparseMatrix<T> 55 : { 56 : 57 : public: 58 : /** 59 : * Constructor; initializes the matrix to be empty, without any 60 : * structure, i.e. the matrix is not usable at all. This 61 : * constructor is therefore only useful for matrices which are 62 : * members of a class. All other matrices should be created at a 63 : * point in the data flow where all necessary information is 64 : * available. 65 : * 66 : * You have to initialize the matrix before usage with \p init(...). 67 : */ 68 : EigenSparseMatrix (const Parallel::Communicator & comm); 69 : 70 : /** 71 : * The 5 special functions can be defaulted for this class, as it 72 : * does not manage any memory itself. 73 : */ 74 : EigenSparseMatrix (EigenSparseMatrix &&) = default; 75 284 : EigenSparseMatrix (const EigenSparseMatrix &) = default; 76 0 : EigenSparseMatrix & operator= (const EigenSparseMatrix &) = default; 77 : EigenSparseMatrix & operator= (EigenSparseMatrix &&) = default; 78 454 : virtual ~EigenSparseMatrix () = default; 79 : 80 0 : virtual SolverPackage solver_package() override 81 : { 82 0 : return EIGEN_SOLVERS; 83 : } 84 : 85 0 : virtual SparseMatrix<T> & operator=(const SparseMatrix<T> & v) override 86 : { 87 0 : *this = cast_ref<const EigenSparseMatrix<T> &>(v); 88 0 : return *this; 89 : } 90 : 91 : /** 92 : * Convenient typedefs 93 : */ 94 : typedef EigenSM DataType; 95 : typedef Eigen::Triplet<T,eigen_idx_type> TripletType; 96 : 97 : virtual void init (const numeric_index_type m, 98 : const numeric_index_type n, 99 : const numeric_index_type m_l, 100 : const numeric_index_type n_l, 101 : const numeric_index_type nnz=30, 102 : const numeric_index_type noz=10, 103 : const numeric_index_type blocksize=1) override; 104 : 105 : virtual void init (ParallelType = PARALLEL) override; 106 : 107 : virtual void clear () override; 108 : 109 : virtual void zero () override; 110 : 111 : virtual std::unique_ptr<SparseMatrix<T>> zero_clone () const override; 112 : 113 : virtual std::unique_ptr<SparseMatrix<T>> clone () const override; 114 : 115 3751 : virtual void close () override { this->_closed = true; } 116 : 117 : virtual numeric_index_type m () const override; 118 : 119 : virtual numeric_index_type n () const override; 120 : 121 : virtual numeric_index_type row_start () const override; 122 : 123 : virtual numeric_index_type row_stop () const override; 124 : 125 : virtual numeric_index_type col_start () const override; 126 : 127 : virtual numeric_index_type col_stop () const override; 128 : 129 : virtual void set (const numeric_index_type i, 130 : const numeric_index_type j, 131 : const T value) override; 132 : 133 : virtual void add (const numeric_index_type i, 134 : const numeric_index_type j, 135 : const T value) override; 136 : 137 : virtual void add_matrix (const DenseMatrix<T> & dm, 138 : const std::vector<numeric_index_type> & rows, 139 : const std::vector<numeric_index_type> & cols) override; 140 : 141 : virtual void add_matrix (const DenseMatrix<T> & dm, 142 : const std::vector<numeric_index_type> & dof_indices) override; 143 : 144 : virtual void add (const T a, const SparseMatrix<T> & X) override; 145 : 146 : virtual T operator () (const numeric_index_type i, 147 : const numeric_index_type j) const override; 148 : 149 : virtual Real l1_norm () const override; 150 : 151 : virtual Real linfty_norm () const override; 152 : 153 0 : virtual bool closed() const override { return _closed; } 154 : 155 0 : virtual void print_personal(std::ostream & os=libMesh::out) const override { this->print(os); } 156 : 157 : virtual void get_diagonal (NumericVector<T> & dest) const override; 158 : 159 : virtual void get_transpose (SparseMatrix<T> & dest) const override; 160 : 161 : virtual void get_row(numeric_index_type i, 162 : std::vector<numeric_index_type> & indices, 163 : std::vector<T> & values) const override; 164 : private: 165 : 166 : /** 167 : * Actual Eigen::SparseMatrix<> we are wrapping. 168 : */ 169 : DataType _mat; 170 : 171 : /** 172 : * Flag indicating if the matrix has been closed yet. 173 : */ 174 : bool _closed; 175 : 176 : /** 177 : * Make other Eigen datatypes friends 178 : */ 179 : friend class EigenSparseVector<T>; 180 : friend class EigenSparseLinearSolver<T>; 181 : }; 182 : 183 : } // namespace libMesh 184 : 185 : #endif // #ifdef LIBMESH_HAVE_EIGEN 186 : #endif // #ifdef LIBMESH_EIGEN_SPARSE_MATRIX_H