Loading...
Searching...
No Matches
PETScMatrix.h
// Copyright (C) 2004-2012 Johan Hoffman, Johan Jansson, Anders Logg
// 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/>.
//
// Modified by Andy R. Terrel 2005
// Modified by Garth N. Wells 2006-2009
// Modified by Kent-Andre Mardal 2008
// Modified by Ola Skavhaug 2008
// Modified by Fredrik Valdmanis 2011
//
// First added: 2004-01-01
// Last changed: 2012-08-22
#ifndef __PETSC_MATRIX_H
#define __PETSC_MATRIX_H
#ifdef HAS_PETSC
#include <map>
#include <memory>
#include <string>
#include <petscmat.h>
#include <petscsys.h>
#include "GenericMatrix.h"
#include "PETScBaseMatrix.h"
namespace dolfin
{
class PETScVector;
class TensorLayout;
class VectorSpaceBasis;
class PETScMatrix : public GenericMatrix, public PETScBaseMatrix
{
public:
PETScMatrix();
explicit PETScMatrix(MPI_Comm comm);
explicit PETScMatrix(Mat A);
PETScMatrix(const PETScMatrix& A);
virtual ~PETScMatrix();
//--- Implementation of the GenericTensor interface ---
void init(const TensorLayout& tensor_layout);
bool is_nest();
void set_nest(std::vector<Mat> mats);
bool empty() const;
std::size_t size(std::size_t dim) const
{ return PETScBaseMatrix::size(dim); }
std::pair<std::int64_t, std::int64_t> local_range(std::size_t dim) const
{ return PETScBaseMatrix::local_range(dim); }
std::size_t nnz() const;
virtual void zero();
virtual void apply(std::string mode);
MPI_Comm mpi_comm() const;
virtual std::string str(bool verbose) const;
//--- Implementation of the GenericMatrix interface --
virtual std::shared_ptr<GenericMatrix> copy() const;
virtual void init_vector(GenericVector& z, std::size_t dim) const
{ PETScBaseMatrix::init_vector(z, dim); }
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);
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);
virtual void axpy(double a, const GenericMatrix& A,
bool same_nonzero_pattern);
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,
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);
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);
// Matrix-vector product, y = Ax
virtual void mult(const GenericVector& x, GenericVector& y) const;
// Matrix-vector product, y = A^T x
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 PETScMatrix& operator*= (double a);
virtual const PETScMatrix& operator/= (double a);
virtual const GenericMatrix& operator= (const GenericMatrix& A);
virtual bool is_symmetric(double tol) const;
//--- Special functions ---
virtual GenericLinearAlgebraFactory& factory() const;
//--- Special PETSc Functions ---
void set_options_prefix(std::string options_prefix);
std::string get_options_prefix() const;
void set_from_options();
const PETScMatrix& operator= (const PETScMatrix& A);
void set_nullspace(const VectorSpaceBasis& nullspace);
void set_near_nullspace(const VectorSpaceBasis& nullspace);
void binary_dump(std::string file_name) const;
void convert_to_aij();
private:
// Create PETSc nullspace object
MatNullSpace create_petsc_nullspace(const VectorSpaceBasis& nullspace) const;
// PETSc norm types
static const std::map<std::string, NormType> norm_types;
};
}
#endif
#endif
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::PETScBaseMatrix::size
std::pair< std::int64_t, std::int64_t > size() const
Definition PETScBaseMatrix.cpp:79
dolfin::PETScBaseMatrix::init_vector
void init_vector(GenericVector &z, std::size_t dim) const
Definition PETScBaseMatrix.cpp:106
dolfin::PETScBaseMatrix::local_range
std::pair< std::int64_t, std::int64_t > local_range(std::size_t dim) const
Return local range along dimension dim.
Definition PETScBaseMatrix.cpp:89
dolfin::PETScBaseMatrix::PETScBaseMatrix
PETScBaseMatrix()
Constructor.
Definition PETScBaseMatrix.h:54
dolfin::PETScMatrix
Definition PETScMatrix.h:59
dolfin::PETScMatrix::set_diagonal
virtual void set_diagonal(const GenericVector &x)
Set diagonal of a matrix.
Definition PETScMatrix.cpp:517
dolfin::PETScMatrix::mult
virtual void mult(const GenericVector &x, GenericVector &y) const
Compute matrix-vector product y = Ax.
Definition PETScMatrix.cpp:442
dolfin::PETScMatrix::norm
double norm(std::string norm_type) const
Return norm of matrix.
Definition PETScMatrix.cpp:534
dolfin::PETScMatrix::local_range
std::pair< std::int64_t, std::int64_t > local_range(std::size_t dim) const
Return local ownership range.
Definition PETScMatrix.h:97
dolfin::PETScMatrix::transpmult
virtual void transpmult(const GenericVector &x, GenericVector &y) const
Definition PETScMatrix.cpp:471
dolfin::PETScMatrix::get_diagonal
virtual void get_diagonal(GenericVector &x) const
Get diagonal of a matrix.
Definition PETScMatrix.cpp:500
dolfin::PETScMatrix::operator=
virtual const GenericMatrix & operator=(const GenericMatrix &A)
Assignment operator.
Definition PETScMatrix.cpp:622
dolfin::PETScMatrix::set_nest
void set_nest(std::vector< Mat > mats)
Initialize or update a nested matrix from the list of its blocks.
Definition PETScMatrix.cpp:229
dolfin::PETScMatrix::factory
virtual GenericLinearAlgebraFactory & factory() const
Return linear algebra backend factory.
Definition PETScMatrix.cpp:637
dolfin::PETScMatrix::~PETScMatrix
virtual ~PETScMatrix()
Destructor.
Definition PETScMatrix.cpp:90
dolfin::PETScMatrix::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 PETScMatrix.cpp:282
dolfin::PETScMatrix::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 PETScMatrix.cpp:252
dolfin::PETScMatrix::is_symmetric
virtual bool is_symmetric(double tol) const
Test if matrix is symmetric.
Definition PETScMatrix.cpp:628
dolfin::PETScMatrix::PETScMatrix
PETScMatrix()
Create empty matrix (on MPI_COMM_WORLD).
Definition PETScMatrix.cpp:57
dolfin::PETScMatrix::binary_dump
void binary_dump(std::string file_name) const
Dump matrix to PETSc binary format.
Definition PETScMatrix.cpp:731
dolfin::PETScMatrix::str
virtual std::string str(bool verbose) const
Return informal string representation (pretty-print).
Definition PETScMatrix.cpp:747
dolfin::PETScMatrix::is_nest
bool is_nest()
Check is the matrix as nest type.
Definition PETScMatrix.cpp:221
dolfin::PETScMatrix::convert_to_aij
void convert_to_aij()
Convert matrix to AIJ format.
Definition PETScMatrix.cpp:804
dolfin::PETScMatrix::operator/=
virtual const PETScMatrix & operator/=(double a)
Divide matrix by given number.
Definition PETScMatrix.cpp:615
dolfin::PETScMatrix::size
std::size_t size(std::size_t dim) const
Return size of given dimension.
Definition PETScMatrix.h:93
dolfin::PETScMatrix::init_vector
virtual void init_vector(GenericVector &z, std::size_t dim) const
Definition PETScMatrix.h:133
dolfin::PETScMatrix::nnz
std::size_t nnz() const
Return number of non-zero entries in matrix (collective).
Definition PETScMatrix.cpp:593
dolfin::PETScMatrix::apply
virtual void apply(std::string mode)
Definition PETScMatrix.cpp:553
dolfin::PETScMatrix::empty
bool empty() const
Return true if empty.
Definition PETScMatrix.cpp:245
dolfin::PETScMatrix::init
void init(const TensorLayout &tensor_layout)
Initialize zero tensor using tensor layout.
Definition PETScMatrix.cpp:100
dolfin::PETScMatrix::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 PETScMatrix.cpp:272
dolfin::PETScMatrix::set_options_prefix
void set_options_prefix(std::string options_prefix)
Definition PETScMatrix.cpp:642
dolfin::PETScMatrix::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 PETScMatrix.cpp:262
dolfin::PETScMatrix::copy
virtual std::shared_ptr< GenericMatrix > copy() const
Return copy of matrix.
Definition PETScMatrix.cpp:95
dolfin::PETScMatrix::setrow
virtual void setrow(std::size_t row, const std::vector< std::size_t > &columns, const std::vector< double > &values)
Set values for given row.
Definition PETScMatrix.cpp:362
dolfin::PETScMatrix::zero_local
virtual void zero_local(std::size_t m, const dolfin::la_index *rows)
Set given rows (local row indices) to zero.
Definition PETScMatrix.cpp:397
dolfin::PETScMatrix::operator*=
virtual const PETScMatrix & operator*=(double a)
Multiply matrix by given number.
Definition PETScMatrix.cpp:607
dolfin::PETScMatrix::set_nullspace
void set_nullspace(const VectorSpaceBasis &nullspace)
Definition PETScMatrix.cpp:703
dolfin::PETScMatrix::ident_local
virtual void ident_local(std::size_t m, const dolfin::la_index *rows)
Set given rows (local row indices) to identity matrix.
Definition PETScMatrix.cpp:425
dolfin::PETScMatrix::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 PETScMatrix.cpp:292
dolfin::PETScMatrix::mpi_comm
MPI_Comm mpi_comm() const
Return MPI communicator.
Definition PETScMatrix.cpp:588
dolfin::PETScMatrix::ident
virtual void ident(std::size_t m, const dolfin::la_index *rows)
Set given rows (global row indices) to identity matrix.
Definition PETScMatrix.cpp:408
dolfin::PETScMatrix::set_near_nullspace
void set_near_nullspace(const VectorSpaceBasis &nullspace)
Definition PETScMatrix.cpp:717
dolfin::PETScMatrix::zero
virtual void zero()
Set all entries to zero and keep any sparse structure.
Definition PETScMatrix.cpp:600
dolfin::PETScMatrix::set_from_options
void set_from_options()
Call PETSc function MatSetFromOptions on the PETSc Mat object.
Definition PETScMatrix.cpp:656
dolfin::PETScMatrix::get_options_prefix
std::string get_options_prefix() const
Definition PETScMatrix.cpp:648
dolfin::PETScMatrix::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 PETScMatrix.cpp:342
dolfin::PETScMatrix::axpy
virtual void axpy(double a, const GenericMatrix &A, bool same_nonzero_pattern)
Add multiple of given matrix (AXPY operation).
Definition PETScMatrix.cpp:302
dolfin::PETScVector
Definition PETScVector.h:61
dolfin::TensorLayout
Definition TensorLayout.h:42
dolfin::VectorSpaceBasis
Definition VectorSpaceBasis.h:34
dolfin
Definition adapt.h:30
dolfin::la_index
PetscInt la_index
Index type for compatibility with linear algebra backend(s).
Definition types.h:32