21 #include "libmesh/libmesh_config.h" 23 #ifdef LIBMESH_HAVE_EIGEN 25 #include "libmesh/eigen_sparse_vector.h" 26 #include "libmesh/eigen_sparse_matrix.h" 27 #include "libmesh/dense_matrix.h" 28 #include "libmesh/dof_map.h" 29 #include "libmesh/sparsity_pattern.h" 51 libmesh_assert_equal_to (m_in, m_l);
52 libmesh_assert_equal_to (n_in, n_l);
53 libmesh_assert_greater (nnz, 0);
55 _mat.resize(m_in, n_in);
56 _mat.reserve(Eigen::Matrix<numeric_index_type, Eigen::Dynamic, 1>::Constant(m_in,nnz));
88 libmesh_assert_equal_to (m_l, n_rows);
89 libmesh_assert_equal_to (n_l, n_cols);
91 const std::vector<numeric_index_type> & n_nz = this->_sp->get_n_nz();
96 const std::vector<numeric_index_type> & n_oz = this->_sp->get_n_oz();
100 libmesh_assert_equal_to (n_nz.size(), n_l);
101 libmesh_assert_equal_to (n_oz.size(), n_l);
109 _mat.resize(n_rows,n_cols);
114 libmesh_assert_equal_to (n_rows, this->m());
115 libmesh_assert_equal_to (n_cols, this->n());
120 template <
typename T>
122 const std::vector<numeric_index_type> & rows,
123 const std::vector<numeric_index_type> & cols)
127 unsigned int n_rows = cast_int<unsigned int>(rows.size());
128 unsigned int n_cols = cast_int<unsigned int>(cols.size());
129 libmesh_assert_equal_to (dm.
m(), n_rows);
130 libmesh_assert_equal_to (dm.
n(), n_cols);
133 for (
unsigned int i=0; i<n_rows; i++)
134 for (
unsigned int j=0; j<n_cols; j++)
135 this->add(rows[i],cols[j],dm(i,j));
140 template <
typename T>
145 dest.
_vec = _mat.diagonal();
152 template <
typename T>
157 dest.
_mat = _mat.transpose();
165 template <
typename T>
174 template <
typename T>
185 template <
typename T>
194 const std::vector<numeric_index_type> & n_nz = this->_sp->get_n_nz();
201 template <
typename T>
206 auto ret = std::make_unique<EigenSparseMatrix<T>>(*this);
214 template <
typename T>
217 return std::make_unique<EigenSparseMatrix<T>>(*this);
222 template <
typename T>
227 return cast_int<numeric_index_type>(_mat.rows());
232 template <
typename T>
237 return cast_int<numeric_index_type>(_mat.cols());
242 template <
typename T>
250 template <
typename T>
258 template <
typename T>
266 template <
typename T>
274 template <
typename T>
280 libmesh_assert_less (i, this->m());
281 libmesh_assert_less (j, this->n());
283 _mat.coeffRef(i,j) =
value;
288 template <
typename T>
294 libmesh_assert_less (i, this->m());
295 libmesh_assert_less (j, this->n());
297 _mat.coeffRef(i,j) +=
value;
302 template <
typename T>
304 const std::vector<numeric_index_type> & dof_indices)
306 this->add_matrix (dm, dof_indices, dof_indices);
311 template <
typename T>
315 libmesh_assert_equal_to (this->m(), X_in.
m());
316 libmesh_assert_equal_to (this->n(), X_in.
n());
319 cast_ref<const EigenSparseMatrix<T> &> (X_in);
327 template <
typename T>
332 libmesh_assert_less (i, this->m());
333 libmesh_assert_less (j, this->n());
335 return _mat.coeff(i,j);
340 template <
typename T>
347 std::vector<Real> abs_col_sums(this->n());
353 EigenSM::InnerIterator it(_mat, row);
355 abs_col_sums[it.col()] += std::abs(it.value());
358 return *(std::max_element(abs_col_sums.begin(), abs_col_sums.end()));
363 template <
typename T>
366 Real max_abs_row_sum = 0.;
372 Real current_abs_row_sum = 0.;
373 EigenSM::InnerIterator it(_mat, row);
375 current_abs_row_sum += std::abs(it.value());
377 max_abs_row_sum = std::max(max_abs_row_sum, current_abs_row_sum);
380 return max_abs_row_sum;
385 template <
typename T>
387 std::vector<numeric_index_type> & indices,
388 std::vector<T> & values)
const 394 static_assert(EigenSM::IsRowMajor);
396 for (EigenSM::InnerIterator it(_mat, i); it; ++it)
398 indices.push_back(it.col());
399 values.push_back(it.value());
412 #endif // #ifdef LIBMESH_HAVE_EIGEN virtual void set(const numeric_index_type i, const numeric_index_type j, const T value) override
Set the element (i,j) to value.
The EigenSparseMatrix class wraps a sparse matrix object from the Eigen library.
virtual void init(const numeric_index_type m, const numeric_index_type n, const numeric_index_type m_l, const numeric_index_type n_l, const numeric_index_type nnz=30, const numeric_index_type noz=10, const numeric_index_type blocksize=1) override
Initialize SparseMatrix with the specified sizes.
virtual void add(const numeric_index_type i, const numeric_index_type j, const T value) override
Add value to the element (i,j).
virtual numeric_index_type m() const override
virtual numeric_index_type row_stop() const override
DataType _vec
Actual Eigen::SparseVector<> we are wrapping.
bool _closed
Flag indicating if the matrix has been closed yet.
virtual std::unique_ptr< SparseMatrix< T > > clone() const override
Provides a uniform interface to vector storage schemes for different linear algebra libraries...
The libMesh namespace provides an interface to certain functionality in the library.
virtual void close() override
Calls the NumericVector's internal assembly routines, ensuring that the values are consistent across ...
DataType _mat
Actual Eigen::SparseMatrix<> we are wrapping.
virtual void get_transpose(SparseMatrix< T > &dest) const override
Copies the transpose of the matrix into dest, which may be *this.
virtual void zero() override
Set all entries to 0.
virtual numeric_index_type n() const override
virtual void get_diagonal(NumericVector< T > &dest) const override
Copies the diagonal part of the matrix into dest.
dof_id_type numeric_index_type
bool _is_initialized
Flag that tells if init() has been called.
EigenSparseMatrix(const Parallel::Communicator &comm)
Constructor; initializes the matrix to be empty, without any structure, i.e.
bool _is_initialized
Flag indicating whether or not the matrix has been initialized.
virtual numeric_index_type m() const =0
virtual numeric_index_type col_stop() const override
virtual T operator()(const numeric_index_type i, const numeric_index_type j) const override
virtual numeric_index_type row_start() const override
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual Real linfty_norm() const override
virtual numeric_index_type col_start() const override
virtual void clear() override
Restores the SparseMatrix<T> to a pristine state.
virtual void get_row(numeric_index_type i, std::vector< numeric_index_type > &indices, std::vector< T > &values) const override
Get a row from the matrix.
IntRange< T > make_range(T beg, T end)
The 2-parameter make_range() helper function returns an IntRange<T> when both input parameters are of...
bool initialized()
Checks that library initialization has been done.
virtual std::unique_ptr< SparseMatrix< T > > zero_clone() const override
virtual Real l1_norm() const override
Defines a dense matrix for use in Finite Element-type computations.
This class provides a nice interface to the Eigen C++-based data structures for serial vectors...
virtual numeric_index_type n() const =0
ParallelType
Defines an enum for parallel data structure types.
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.