Loading...
Searching...
No Matches
GenericMatrix.h
// Copyright (C) 2006-2011 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/>.
//
// Modified by Johan Jansson 2006
// Modified by Anders Logg 2006-2012
// Modified by Ola Skavhaug 2007-2008
// Modified by Kent-Andre Mardal 2008
// Modified by Martin Alnæs 2008
// Modified by Mikael Mortensen 2011
//
// First added: 2006-04-24
// Last changed: 2012-08-20
#ifndef __GENERIC_MATRIX_H
#define __GENERIC_MATRIX_H
#include <tuple>
#include <vector>
#include <dolfin/common/constants.h>
#include "GenericTensor.h"
#include "GenericLinearOperator.h"
namespace dolfin
{
class GenericVector;
class TensorLayout;
class GenericMatrix : public GenericTensor, public GenericLinearOperator
{
public:
virtual ~GenericMatrix() {}
//--- Implementation of the GenericTensor interface ---
virtual std::size_t rank() const
{ return 2; }
virtual std::size_t size(std::size_t dim) const = 0;
virtual std::pair<std::int64_t, std::int64_t>
local_range(std::size_t dim) const = 0;
virtual std::size_t nnz() const = 0;
virtual void get(double* block, const dolfin::la_index* num_rows,
const dolfin::la_index * const * rows) const
{ get(block, num_rows[0], rows[0], num_rows[1], rows[1]); }
virtual void set(const double* block, const dolfin::la_index* num_rows,
const dolfin::la_index * const * rows)
{ set(block, num_rows[0], rows[0], num_rows[1], rows[1]); }
virtual void set_local(const double* block,
const dolfin::la_index* num_rows,
const dolfin::la_index * const * rows)
{ set_local(block, num_rows[0], rows[0], num_rows[1], rows[1]); }
virtual void add(const double* block, const dolfin::la_index* num_rows,
const dolfin::la_index * const * rows)
{ add(block, num_rows[0], rows[0], num_rows[1], rows[1]); }
virtual void add_local(const double* block,
const dolfin::la_index* num_rows,
const dolfin::la_index * const * rows)
{ add_local(block, num_rows[0], rows[0], num_rows[1], rows[1]); }
virtual void
add(const double* block,
const std::vector<ArrayView<const dolfin::la_index>>& rows)
{
add(block, rows[0].size(), rows[0].data(),
rows[1].size(), rows[1].data());
}
virtual void
add_local(const double* block,
const std::vector<ArrayView<const dolfin::la_index>>& rows)
{
add_local(block, rows[0].size(), rows[0].data(),
rows[1].size(), rows[1].data());
}
virtual void zero() = 0;
virtual void apply(std::string mode) = 0;
virtual std::string str(bool verbose) const = 0;
//--- Matrix interface ---
virtual std::shared_ptr<GenericMatrix> copy() const = 0;
virtual void init_vector(GenericVector& z, std::size_t dim) const = 0;
virtual void get(double* block,
std::size_t m, const dolfin::la_index* rows,
std::size_t n, const dolfin::la_index* cols) const = 0;
virtual void set(const double* block,
std::size_t m, const dolfin::la_index* rows,
std::size_t n, const dolfin::la_index* cols) = 0;
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) = 0;
virtual void add(const double* block,
std::size_t m, const dolfin::la_index* rows,
std::size_t n, const dolfin::la_index* cols) = 0;
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) = 0;
virtual void axpy(double a, const GenericMatrix& A,
bool same_nonzero_pattern) = 0;
virtual double norm(std::string norm_type) const = 0;
virtual void getrow(std::size_t row, std::vector<std::size_t>& columns,
std::vector<double>& values) const = 0;
virtual void setrow(std::size_t row,
const std::vector<std::size_t>& columns,
const std::vector<double>& values) = 0;
virtual void zero(std::size_t m, const dolfin::la_index* rows) = 0;
virtual void zero_local(std::size_t m, const dolfin::la_index* rows) = 0;
virtual void ident(std::size_t m, const dolfin::la_index* rows) = 0;
virtual void ident_local(std::size_t m, const dolfin::la_index* rows) = 0;
virtual void transpmult(const GenericVector& x, GenericVector& y) const = 0;
virtual void get_diagonal(GenericVector& x) const = 0;
virtual void set_diagonal(const GenericVector& x) = 0;
virtual const GenericMatrix& operator*= (double a) = 0;
virtual const GenericMatrix& operator/= (double a) = 0;
const GenericMatrix& operator+= (const GenericMatrix& A)
{
axpy(1.0, A, false);
return *this;
}
const GenericMatrix& operator-= (const GenericMatrix& A)
{
axpy(-1.0, A, false);
return *this;
}
virtual bool is_symmetric(double tol) const
{
dolfin_error("GenericMatrix.h",
"test if matrix is symmetric",
"Not implemented by current linear algebra backend");
return false;
}
virtual const GenericMatrix& operator= (const GenericMatrix& x) = 0;
//--- Convenience functions ---
virtual double operator() (dolfin::la_index i, dolfin::la_index j) const
{ double value(0); get(&value, 1, &i, 1, &j); return value; }
virtual double getitem(std::pair<dolfin::la_index,
dolfin::la_index> ij) const
{
double value(0);
get(&value, 1, &ij.first, 1, &ij.second);
return value;
}
virtual void setitem(std::pair<dolfin::la_index, dolfin::la_index> ij,
double value)
{ set(&value, 1, &ij.first, 1, &ij.second); }
virtual void ident_zeros(double tol=DOLFIN_EPS);
};
}
#endif
dolfin::ArrayView
Definition ArrayView.h:32
dolfin::GenericLinearOperator
Definition GenericLinearOperator.h:43
dolfin::GenericMatrix
This class defines a common interface for matrices.
Definition GenericMatrix.h:47
dolfin::GenericMatrix::getrow
virtual void getrow(std::size_t row, std::vector< std::size_t > &columns, std::vector< double > &values) const =0
Get non-zero values of given row (global index) on local process.
dolfin::GenericMatrix::set
virtual void set(const double *block, const dolfin::la_index *num_rows, const dolfin::la_index *const *rows)
Set block of values using global indices.
Definition GenericMatrix.h:75
dolfin::GenericMatrix::rank
virtual std::size_t rank() const
Return tensor rank (number of dimensions).
Definition GenericMatrix.h:56
dolfin::GenericMatrix::set_diagonal
virtual void set_diagonal(const GenericVector &x)=0
Set diagonal of a matrix.
dolfin::GenericMatrix::transpmult
virtual void transpmult(const GenericVector &x, GenericVector &y) const =0
dolfin::GenericMatrix::zero_local
virtual void zero_local(std::size_t m, const dolfin::la_index *rows)=0
Set given rows (local row indices) to zero.
dolfin::GenericMatrix::zero
virtual void zero(std::size_t m, const dolfin::la_index *rows)=0
Set given rows (global row indices) to zero.
dolfin::GenericMatrix::add
virtual void add(const double *block, const std::vector< ArrayView< const dolfin::la_index > > &rows)
Add block of values using global indices.
Definition GenericMatrix.h:98
dolfin::GenericMatrix::operator()
virtual double operator()(dolfin::la_index i, dolfin::la_index j) const
Get value of given entry.
Definition GenericMatrix.h:236
dolfin::GenericMatrix::size
virtual std::size_t size(std::size_t dim) const =0
Return size of given dimension.
dolfin::GenericMatrix::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)=0
Add block of values using local indices.
dolfin::GenericMatrix::get_diagonal
virtual void get_diagonal(GenericVector &x) const =0
Get diagonal of a matrix.
dolfin::GenericMatrix::add
virtual void add(const double *block, const dolfin::la_index *num_rows, const dolfin::la_index *const *rows)
Add block of values using global indices.
Definition GenericMatrix.h:86
dolfin::GenericMatrix::operator/=
virtual const GenericMatrix & operator/=(double a)=0
Divide matrix by given number.
dolfin::GenericMatrix::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)=0
Set block of values using global indices.
dolfin::GenericMatrix::copy
virtual std::shared_ptr< GenericMatrix > copy() const =0
Return copy of matrix.
dolfin::GenericMatrix::is_symmetric
virtual bool is_symmetric(double tol) const
Test if matrix is symmetric.
Definition GenericMatrix.h:222
dolfin::GenericMatrix::norm
virtual double norm(std::string norm_type) const =0
Return norm of matrix.
dolfin::GenericMatrix::operator*=
virtual const GenericMatrix & operator*=(double a)=0
Multiply matrix by given number.
dolfin::GenericMatrix::axpy
virtual void axpy(double a, const GenericMatrix &A, bool same_nonzero_pattern)=0
Add multiple of given matrix (AXPY operation).
dolfin::GenericMatrix::setrow
virtual void setrow(std::size_t row, const std::vector< std::size_t > &columns, const std::vector< double > &values)=0
Set values for given row (global index) on local process.
dolfin::GenericMatrix::ident_local
virtual void ident_local(std::size_t m, const dolfin::la_index *rows)=0
Set given rows (local row indices) to identity matrix.
dolfin::GenericMatrix::operator+=
const GenericMatrix & operator+=(const GenericMatrix &A)
Add given matrix.
Definition GenericMatrix.h:208
dolfin::GenericMatrix::ident_zeros
virtual void ident_zeros(double tol=DOLFIN_EPS)
Insert one on the diagonal for all zero rows.
Definition GenericMatrix.cpp:30
dolfin::GenericMatrix::nnz
virtual std::size_t nnz() const =0
Return number of non-zero entries in matrix (collective).
dolfin::GenericMatrix::get
virtual void get(double *block, const dolfin::la_index *num_rows, const dolfin::la_index *const *rows) const
Get block of values.
Definition GenericMatrix.h:70
dolfin::GenericMatrix::add_local
virtual void add_local(const double *block, const dolfin::la_index *num_rows, const dolfin::la_index *const *rows)
Add block of values using local indices.
Definition GenericMatrix.h:91
dolfin::GenericMatrix::str
virtual std::string str(bool verbose) const =0
Return informal string representation (pretty-print).
dolfin::GenericMatrix::operator=
virtual const GenericMatrix & operator=(const GenericMatrix &x)=0
Assignment operator.
dolfin::GenericMatrix::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 =0
Get block of values.
dolfin::GenericMatrix::zero
virtual void zero()=0
Set all entries to zero and keep any sparse structure.
dolfin::GenericMatrix::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)=0
Set block of values using local indices.
dolfin::GenericMatrix::~GenericMatrix
virtual ~GenericMatrix()
Destructor.
Definition GenericMatrix.h:51
dolfin::GenericMatrix::ident
virtual void ident(std::size_t m, const dolfin::la_index *rows)=0
Set given rows (global row indices) to identity matrix.
dolfin::GenericMatrix::set_local
virtual void set_local(const double *block, const dolfin::la_index *num_rows, const dolfin::la_index *const *rows)
Set block of values using local indices.
Definition GenericMatrix.h:80
dolfin::GenericMatrix::getitem
virtual double getitem(std::pair< dolfin::la_index, dolfin::la_index > ij) const
Get value of given entry.
Definition GenericMatrix.h:240
dolfin::GenericMatrix::operator-=
const GenericMatrix & operator-=(const GenericMatrix &A)
Subtract given matrix.
Definition GenericMatrix.h:215
dolfin::GenericMatrix::local_range
virtual std::pair< std::int64_t, std::int64_t > local_range(std::size_t dim) const =0
Return local ownership range.
dolfin::GenericMatrix::apply
virtual void apply(std::string mode)=0
Finalize assembly of tensor.
dolfin::GenericMatrix::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)=0
Add block of values using global indices.
dolfin::GenericMatrix::init_vector
virtual void init_vector(GenericVector &z, std::size_t dim) const =0
dolfin::GenericMatrix::setitem
virtual void setitem(std::pair< dolfin::la_index, dolfin::la_index > ij, double value)
Definition GenericMatrix.h:250
dolfin::GenericMatrix::add_local
virtual void add_local(const double *block, const std::vector< ArrayView< const dolfin::la_index > > &rows)
Add block of values using local indices.
Definition GenericMatrix.h:107
dolfin::GenericTensor
A common interface for arbitrary rank tensors.
Definition GenericTensor.h:49
dolfin::GenericVector
This class defines a common interface for vectors.
Definition GenericVector.h:48
dolfin::TensorLayout
Definition TensorLayout.h:42
dolfin
Definition adapt.h:30
dolfin::dolfin_error
void dolfin_error(std::string location, std::string task, std::string reason,...)
Definition log.cpp:129
dolfin::la_index
PetscInt la_index
Index type for compatibility with linear algebra backend(s).
Definition types.h:32