Loading...
Searching...
No Matches
EigenMatrix.h
// Copyright (C) 2015 Chris Richardson and Garth N. Wells
//
// This file is part of DOLFIN.
//
// DOLFIN is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// DOLFIN is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
#ifndef __DOLFIN_EIGEN_MATRIX_H
#define __DOLFIN_EIGEN_MATRIX_H
#include <iomanip>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <Eigen/Sparse>
#include <dolfin/common/MPI.h>
#include <dolfin/common/types.h>
#include "EigenVector.h"
#include "GenericMatrix.h"
#include "GenericVector.h"
#include "TensorLayout.h"
namespace dolfin
{
class EigenMatrix : public GenericMatrix
{
public:
typedef Eigen::SparseMatrix<double, Eigen::RowMajor, int> eigen_matrix_type;
EigenMatrix();
EigenMatrix(std::size_t M, std::size_t N);
EigenMatrix(const EigenMatrix& A);
virtual ~EigenMatrix();
//--- Implementation of the GenericTensor interface ---
virtual void init(const TensorLayout& tensor_layout);
virtual bool empty() const
{ return size(0) == 0; }
virtual std::size_t size(std::size_t dim) const;
virtual std::pair<std::int64_t, std::int64_t>
local_range(std::size_t dim) const
{ return {0, size(dim)}; }
std::size_t nnz() const;
virtual void zero();
virtual void apply(std::string mode);
virtual MPI_Comm mpi_comm() const
{ return _mpi_comm.comm(); }
virtual std::string str(bool verbose) const;
//--- Implementation of the GenericMatrix interface ---
virtual std::shared_ptr<GenericMatrix> copy() const;
virtual void resize(std::size_t M, std::size_t N);
virtual void init_vector(GenericVector& z, std::size_t dim) const;
virtual void get(double* block, std::size_t m, const dolfin::la_index* rows,
std::size_t n, const dolfin::la_index* cols) const;
virtual void set(const double* block, std::size_t m,
const dolfin::la_index* rows, std::size_t n,
const dolfin::la_index* cols);
virtual void set_local(const double* block, std::size_t m,
const dolfin::la_index* rows, std::size_t n,
const dolfin::la_index* cols)
{ set(block, m, rows, n, cols); }
virtual void add(const double* block, std::size_t m,
const dolfin::la_index* rows, std::size_t n,
const dolfin::la_index* cols);
virtual void add_local(const double* block, std::size_t m,
const dolfin::la_index* rows, std::size_t n,
const dolfin::la_index* cols)
{ add(block, m, rows, n, cols); }
virtual void axpy(double a, const GenericMatrix& A,
bool same_nonzero_pattern);
virtual double norm(std::string norm_type) const;
virtual void getrow(std::size_t row, std::vector<std::size_t>& columns,
std::vector<double>& values) const;
virtual void setrow(std::size_t row_idx,
const std::vector<std::size_t>& columns,
const std::vector<double>& values);
virtual void zero(std::size_t m, const dolfin::la_index* rows);
virtual void zero_local(std::size_t m, const dolfin::la_index* rows)
{ zero(m, rows); }
virtual void ident(std::size_t m, const dolfin::la_index* rows);
virtual void ident_local(std::size_t m, const dolfin::la_index* rows)
{ ident(m, rows); }
virtual void mult(const GenericVector& x, GenericVector& y) const;
virtual void transpmult(const GenericVector& x, GenericVector& y) const;
virtual void get_diagonal(GenericVector& x) const;
virtual void set_diagonal(const GenericVector& x);
virtual const EigenMatrix& operator*= (double a);
virtual const EigenMatrix& operator/= (double a);
virtual const GenericMatrix& operator= (const GenericMatrix& A);
virtual std::tuple<const int*, const int*, const double*, std::size_t>
data() const;
//--- Special functions ---
virtual GenericLinearAlgebraFactory& factory() const;
//--- Special Eigen functions ---
const eigen_matrix_type& mat() const
{ return _matA; }
eigen_matrix_type& mat()
{ return _matA; }
void compress()
{ _matA.makeCompressed(); }
double operator() (dolfin::la_index i, dolfin::la_index j) const
{ return _matA.coeff(i, j); }
const EigenMatrix& operator= (const EigenMatrix& A);
private:
// MPI communicator
dolfin::MPI::Comm _mpi_comm;
// Eigen matrix object - row major access
eigen_matrix_type _matA;
};
}
#endif
dolfin::EigenMatrix
Definition EigenMatrix.h:47
dolfin::EigenMatrix::add_local
virtual void add_local(const double *block, std::size_t m, const dolfin::la_index *rows, std::size_t n, const dolfin::la_index *cols)
Add block of values using local indices.
Definition EigenMatrix.h:135
dolfin::EigenMatrix::mpi_comm
virtual MPI_Comm mpi_comm() const
Return MPI communicator.
Definition EigenMatrix.h:92
dolfin::EigenMatrix::str
virtual std::string str(bool verbose) const
Return informal string representation (pretty-print).
Definition EigenMatrix.cpp:357
dolfin::EigenMatrix::mult
virtual void mult(const GenericVector &x, GenericVector &y) const
Matrix-vector product, y = Ax.
Definition EigenMatrix.cpp:230
dolfin::EigenMatrix::set_local
virtual void set_local(const double *block, std::size_t m, const dolfin::la_index *rows, std::size_t n, const dolfin::la_index *cols)
Set block of values using local indices.
Definition EigenMatrix.h:124
dolfin::EigenMatrix::set_diagonal
virtual void set_diagonal(const GenericVector &x)
Set diagonal of a matrix.
Definition EigenMatrix.cpp:271
dolfin::EigenMatrix::axpy
virtual void axpy(double a, const GenericMatrix &A, bool same_nonzero_pattern)
Add multiple of given matrix (AXPY operation).
Definition EigenMatrix.cpp:422
dolfin::EigenMatrix::operator()
double operator()(dolfin::la_index i, dolfin::la_index j) const
Access value of given entry.
Definition EigenMatrix.h:216
dolfin::EigenMatrix::operator/=
virtual const EigenMatrix & operator/=(double a)
Divide matrix by given number.
Definition EigenMatrix.cpp:320
dolfin::EigenMatrix::ident
virtual void ident(std::size_t m, const dolfin::la_index *rows)
Set given rows to identity matrix.
Definition EigenMatrix.cpp:195
dolfin::EigenMatrix::mat
const eigen_matrix_type & mat() const
Return reference to Eigen matrix (const version).
Definition EigenMatrix.h:204
dolfin::EigenMatrix::add
virtual void add(const double *block, std::size_t m, const dolfin::la_index *rows, std::size_t n, const dolfin::la_index *cols)
Add block of values using global indices.
Definition EigenMatrix.cpp:156
dolfin::EigenMatrix::local_range
virtual std::pair< std::int64_t, std::int64_t > local_range(std::size_t dim) const
Return local ownership range.
Definition EigenMatrix.h:79
dolfin::EigenMatrix::~EigenMatrix
virtual ~EigenMatrix()
Destructor.
Definition EigenMatrix.cpp:47
dolfin::EigenMatrix::apply
virtual void apply(std::string mode)
Finalize assembly of tensor.
Definition EigenMatrix.cpp:417
dolfin::EigenMatrix::factory
virtual GenericLinearAlgebraFactory & factory() const
Return linear algebra backend factory.
Definition EigenMatrix.cpp:25
dolfin::EigenMatrix::norm
virtual double norm(std::string norm_type) const
Return norm of matrix.
Definition EigenMatrix.cpp:77
dolfin::EigenMatrix::eigen_matrix_type
Eigen::SparseMatrix< double, Eigen::RowMajor, int > eigen_matrix_type
Eigen Matrix type.
Definition EigenMatrix.h:51
dolfin::EigenMatrix::data
virtual std::tuple< const int *, const int *, const double *, std::size_t > data() const
Definition EigenMatrix.cpp:342
dolfin::EigenMatrix::operator*=
virtual const EigenMatrix & operator*=(double a)
Multiply matrix by given number.
Definition EigenMatrix.cpp:314
dolfin::EigenMatrix::init_vector
virtual void init_vector(GenericVector &z, std::size_t dim) const
Definition EigenMatrix.cpp:139
dolfin::EigenMatrix::zero
virtual void zero()
Set all entries to zero and keep any sparse structure.
Definition EigenMatrix.cpp:180
dolfin::EigenMatrix::empty
virtual bool empty() const
Return true if empty.
Definition EigenMatrix.h:71
dolfin::EigenMatrix::EigenMatrix
EigenMatrix()
Create empty matrix.
Definition EigenMatrix.cpp:30
dolfin::EigenMatrix::setrow
virtual void setrow(std::size_t row_idx, const std::vector< std::size_t > &columns, const std::vector< double > &values)
Set values for given row.
Definition EigenMatrix.cpp:129
dolfin::EigenMatrix::get
virtual void get(double *block, std::size_t m, const dolfin::la_index *rows, std::size_t n, const dolfin::la_index *cols) const
Get block of values.
Definition EigenMatrix.cpp:168
dolfin::EigenMatrix::mat
eigen_matrix_type & mat()
Return reference to Eigen matrix (non-const version).
Definition EigenMatrix.h:208
dolfin::EigenMatrix::getrow
virtual void getrow(std::size_t row, std::vector< std::size_t > &columns, std::vector< double > &values) const
Get non-zero values of given row.
Definition EigenMatrix.cpp:107
dolfin::EigenMatrix::ident_local
virtual void ident_local(std::size_t m, const dolfin::la_index *rows)
Set given rows to identity matrix.
Definition EigenMatrix.h:167
dolfin::EigenMatrix::init
virtual void init(const TensorLayout &tensor_layout)
Initialize zero tensor using tenor layout.
Definition EigenMatrix.cpp:384
dolfin::EigenMatrix::nnz
std::size_t nnz() const
Return number of non-zero entries in matrix.
Definition EigenMatrix.cpp:412
dolfin::EigenMatrix::zero_local
virtual void zero_local(std::size_t m, const dolfin::la_index *rows)
Set given rows (local row indices) to zero.
Definition EigenMatrix.h:160
dolfin::EigenMatrix::set
virtual void set(const double *block, std::size_t m, const dolfin::la_index *rows, std::size_t n, const dolfin::la_index *cols)
Set block of values using global indices.
Definition EigenMatrix.cpp:144
dolfin::EigenMatrix::get_diagonal
virtual void get_diagonal(GenericVector &x) const
Get diagonal of a matrix.
Definition EigenMatrix.cpp:257
dolfin::EigenMatrix::copy
virtual std::shared_ptr< GenericMatrix > copy() const
Return copy of matrix.
Definition EigenMatrix.cpp:52
dolfin::EigenMatrix::compress
void compress()
Compress matrix (eliminate all zeros from a sparse matrix).
Definition EigenMatrix.h:212
dolfin::EigenMatrix::operator=
virtual const GenericMatrix & operator=(const GenericMatrix &A)
Assignment operator.
Definition EigenMatrix.cpp:326
dolfin::EigenMatrix::size
virtual std::size_t size(std::size_t dim) const
Return size of given dimension.
Definition EigenMatrix.cpp:65
dolfin::EigenMatrix::resize
virtual void resize(std::size_t M, std::size_t N)
Resize matrix to M x N.
Definition EigenMatrix.cpp:57
dolfin::EigenMatrix::transpmult
virtual void transpmult(const GenericVector &x, GenericVector &y) const
Matrix-vector product, y = A^T x.
Definition EigenMatrix.cpp:285
dolfin::GenericLinearAlgebraFactory
Base class for LinearAlgebra factories.
Definition GenericLinearAlgebraFactory.h:47
dolfin::GenericMatrix
This class defines a common interface for matrices.
Definition GenericMatrix.h:47
dolfin::GenericVector
This class defines a common interface for vectors.
Definition GenericVector.h:48
dolfin::MPI::Comm
Definition MPI.h:77
dolfin::TensorLayout
Definition TensorLayout.h:42
dolfin
Definition adapt.h:30
dolfin::la_index
PetscInt la_index
Index type for compatibility with linear algebra backend(s).
Definition types.h:32