libMesh
eigen_sparse_matrix.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 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 
53 template <typename T>
54 class EigenSparseMatrix final : public SparseMatrix<T>
55 {
56 
57 public:
68  EigenSparseMatrix (const Parallel::Communicator & comm);
69 
74  EigenSparseMatrix (EigenSparseMatrix &&) = default;
75  EigenSparseMatrix (const EigenSparseMatrix &) = default;
76  EigenSparseMatrix & operator= (const EigenSparseMatrix &) = default;
78  virtual ~EigenSparseMatrix () = default;
79 
83  typedef EigenSM DataType;
84  typedef Eigen::Triplet<T,eigen_idx_type> TripletType;
85 
86  virtual void init (const numeric_index_type m,
87  const numeric_index_type n,
88  const numeric_index_type m_l,
89  const numeric_index_type n_l,
90  const numeric_index_type nnz=30,
91  const numeric_index_type noz=10,
92  const numeric_index_type blocksize=1) override;
93 
94  virtual void init () override;
95 
96  virtual void clear () override;
97 
98  virtual void zero () override;
99 
100  virtual void close () override { this->_closed = true; }
101 
102  virtual numeric_index_type m () const override;
103 
104  virtual numeric_index_type n () const override;
105 
106  virtual numeric_index_type row_start () const override;
107 
108  virtual numeric_index_type row_stop () const override;
109 
110  virtual void set (const numeric_index_type i,
111  const numeric_index_type j,
112  const T value) override;
113 
114  virtual void add (const numeric_index_type i,
115  const numeric_index_type j,
116  const T value) override;
117 
118  virtual void add_matrix (const DenseMatrix<T> & dm,
119  const std::vector<numeric_index_type> & rows,
120  const std::vector<numeric_index_type> & cols) override;
121 
122  virtual void add_matrix (const DenseMatrix<T> & dm,
123  const std::vector<numeric_index_type> & dof_indices) override;
124 
125  virtual void add (const T a, const SparseMatrix<T> & X) override;
126 
127  virtual T operator () (const numeric_index_type i,
128  const numeric_index_type j) const override;
129 
130  virtual Real l1_norm () const override;
131 
132  virtual Real linfty_norm () const override;
133 
134  virtual bool closed() const override { return _closed; }
135 
136  virtual void print_personal(std::ostream & os=libMesh::out) const override { this->print(os); }
137 
138  virtual void get_diagonal (NumericVector<T> & dest) const override;
139 
140  virtual void get_transpose (SparseMatrix<T> & dest) const override;
141 
142 private:
143 
148 
152  bool _closed;
153 
157  friend class EigenSparseVector<T>;
158  friend class EigenSparseLinearSolver<T>;
159 };
160 
161 } // namespace libMesh
162 
163 #endif // #ifdef LIBMESH_HAVE_EIGEN
164 #endif // #ifdef LIBMESH_EIGEN_SPARSE_MATRIX_H
libMesh::EigenSparseMatrix::TripletType
Eigen::Triplet< T, eigen_idx_type > TripletType
Definition: eigen_sparse_matrix.h:84
libMesh::EigenSparseMatrix::print_personal
virtual void print_personal(std::ostream &os=libMesh::out) const override
Print the contents of the matrix to the screen in a package-personalized style, if available.
Definition: eigen_sparse_matrix.h:136
libMesh::EigenSparseMatrix
The EigenSparseMatrix class wraps a sparse matrix object from the Eigen library.
Definition: eigen_sparse_matrix.h:54
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::EigenSparseMatrix::closed
virtual bool closed() const override
Definition: eigen_sparse_matrix.h:134
libMesh::EigenSparseMatrix::set
virtual void set(const numeric_index_type i, const numeric_index_type j, const T value) override
Set the element (i,j) to value.
Definition: eigen_sparse_matrix.C:225
libMesh::ParallelObject::comm
const Parallel::Communicator & comm() const
Definition: parallel_object.h:94
libMesh::DenseMatrix
Defines a dense matrix for use in Finite Element-type computations.
Definition: dof_map.h:73
libMesh::EigenSparseMatrix::zero
virtual void zero() override
Set all entries to 0.
Definition: eigen_sparse_matrix.C:181
libMesh::EigenSparseMatrix::~EigenSparseMatrix
virtual ~EigenSparseMatrix()=default
libMesh::EigenSparseMatrix::close
virtual void close() override
Calls the SparseMatrix's internal assembly routines, ensuring that the values are consistent across p...
Definition: eigen_sparse_matrix.h:100
libMesh::SparseMatrix
Generic sparse matrix.
Definition: vector_fe_ex5.C:45
libMesh::NumericVector
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
Definition: vector_fe_ex5.C:43
libMesh::EigenSM
Eigen::SparseMatrix< Number, Eigen::RowMajor, eigen_idx_type > EigenSM
Definition: eigen_core_support.h:78
libMesh::EigenSparseVector
This class provides a nice interface to the Eigen C++-based data structures for serial vectors.
Definition: eigen_sparse_matrix.h:42
libMesh::EigenSparseMatrix::_closed
bool _closed
Flag indicating if the matrix has been closed yet.
Definition: eigen_sparse_matrix.h:152
libMesh::EigenSparseMatrix::EigenSparseMatrix
EigenSparseMatrix(const Parallel::Communicator &comm)
Constructor; initializes the matrix to be empty, without any structure, i.e.
Definition: eigen_sparse_matrix.C:161
libMesh::EigenSparseMatrix::get_diagonal
virtual void get_diagonal(NumericVector< T > &dest) const override
Copies the diagonal part of the matrix into dest.
Definition: eigen_sparse_matrix.C:141
libMesh::EigenSparseMatrix::DataType
EigenSM DataType
Convenient typedefs.
Definition: eigen_sparse_matrix.h:83
libMesh::numeric_index_type
dof_id_type numeric_index_type
Definition: id_types.h:99
libMesh::EigenSparseMatrix::init
virtual void init() override
Initialize this matrix using the sparsity structure computed by dof_map.
Definition: eigen_sparse_matrix.C:63
libMesh::EigenSparseMatrix::operator=
EigenSparseMatrix & operator=(const EigenSparseMatrix &)=default
libMesh::EigenSparseMatrix::linfty_norm
virtual Real linfty_norm() const override
Definition: eigen_sparse_matrix.C:314
libMesh::EigenSparseMatrix::_mat
DataType _mat
Actual Eigen::SparseMatrix<> we are wrapping.
Definition: eigen_sparse_matrix.h:147
libMesh::EigenSparseMatrix::get_transpose
virtual void get_transpose(SparseMatrix< T > &dest) const override
Copies the transpose of the matrix into dest, which may be *this.
Definition: eigen_sparse_matrix.C:151
libMesh::EigenSparseMatrix::n
virtual numeric_index_type n() const override
Definition: eigen_sparse_matrix.C:199
value
static const bool value
Definition: xdr_io.C:56
libMesh::EigenSparseLinearSolver
This class provides an interface to Eigen iterative solvers that is compatible with the libMesh Linea...
Definition: eigen_sparse_matrix.h:43
libMesh::EigenSparseMatrix::add
virtual void add(const numeric_index_type i, const numeric_index_type j, const T value) override
Add value to the element (i,j).
Definition: eigen_sparse_matrix.C:239
libMesh::EigenSparseMatrix::operator()
virtual T operator()(const numeric_index_type i, const numeric_index_type j) const override
Definition: eigen_sparse_matrix.C:278
libMesh::Real
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Definition: libmesh_common.h:121
libMesh::SparseMatrix::print
void print(std::ostream &os=libMesh::out, const bool sparse=false) const
Print the contents of the matrix to the screen in a uniform style, regardless of matrix/solver packag...
Definition: sparse_matrix.C:200
libMesh::EigenSparseMatrix::row_stop
virtual numeric_index_type row_stop() const override
Definition: eigen_sparse_matrix.C:217
libMesh::out
OStreamProxy out
libMesh::EigenSparseMatrix::add_matrix
virtual void add_matrix(const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols) override
Add the full matrix dm to the SparseMatrix.
Definition: eigen_sparse_matrix.C:121
libMesh::EigenSparseMatrix::l1_norm
virtual Real l1_norm() const override
Definition: eigen_sparse_matrix.C:291
libMesh::EigenSparseMatrix::m
virtual numeric_index_type m() const override
Definition: eigen_sparse_matrix.C:189
libMesh::EigenSparseMatrix::clear
virtual void clear() override
Restores the SparseMatrix<T> to a pristine state.
Definition: eigen_sparse_matrix.C:170
libMesh::EigenSparseMatrix::row_start
virtual numeric_index_type row_start() const override
Definition: eigen_sparse_matrix.C:209