To handle matrices in sparse storage format using the Petsc library. More...
Public Member Functions | |
PETScMatrix () | |
Default constructor. More... | |
PETScMatrix (size_t nr, size_t nc) | |
Constructor that initializes current instance as a dense matrix. More... | |
PETScMatrix (size_t size) | |
Constructor that initializes current instance as a dense matrix. More... | |
PETScMatrix (Mesh &mesh, size_t dof=0) | |
Constructor using a Mesh instance. More... | |
PETScMatrix (const vector< std::pair< size_t, size_t > > &I, int opt=1) | |
Constructor for a square matrix using non zero row and column indices. More... | |
PETScMatrix (const PETScMatrix &m) | |
Copy constructor. | |
~PETScMatrix (void) | |
Destructor. | |
void | Identity () |
Define matrix as identity matrix. | |
void | Diagonal () |
Define matrix as a diagonal one. | |
void | Diagonal (const T_ &a) |
Define matrix as a diagonal one with diagonal entries equal to a | |
void | setAIJ (const vector< int > &nnz) |
void | setAIJ_MPI (const vector< int > &diag_nnz, const vector< int > &off_nnz) |
void | setMesh (Mesh &mesh, size_t dof=0) |
Determine mesh graph and initialize matrix. More... | |
void | setPartition (Partition &p) |
Set a Partition instance in the class. More... | |
void | setRank (int np, int r=0) |
Set number of processors and processor rank. More... | |
void | setOneDOF () |
Activate 1-DOF per node option. | |
void | setSides () |
Activate Sides option. | |
void | setSymmetric () |
Set matrix as a symmetric one. | |
void | DiagPrescribe (PETScVect< T_ > &b, const PETScVect< T_ > &u) |
Impose by a diagonal method an essential boundary condition using the Mesh instance provided by the constructor. More... | |
void | setSize (size_t size) |
Set size of matrix (case where it's a square matrix). More... | |
void | setSize (size_t nr, size_t nc) |
Set size (number of rows) of matrix. More... | |
void | getRange (int istart, int iend) |
Return the range of matrix rows owned by this processor. More... | |
void | setGraph (const vector< std::pair< size_t, size_t > > &I, int opt=1) |
Set graph of matrix by giving a vector of its nonzero entries. More... | |
T_ | operator() (size_t i, size_t j) const |
Operator () More... | |
size_t | getNbRows () const |
Return number of matrix rows. | |
size_t | getNbColumns () const |
Return number of matrix columns. | |
size_t | getLength () const |
Return length of matrix. More... | |
void | getMesh (Mesh &mesh) |
Get Mesh instance whose reference will be stored in current instance of PETScMatrix. | |
void | Mult (const PETScVect< T_ > &x, PETScVect< T_ > &y) const |
Multiply matrix by vector and save in another one. More... | |
void | MultAdd (const PETScVect< T_ > &x, PETScVect< T_ > &y) const |
Multiply matrix by vector x and add to y . More... | |
void | MultAdd (T_ a, const PETScVect< T_ > &x, PETScVect< T_ > &y) const |
Multiply matrix by vector a*x and add to y . More... | |
void | set (size_t i, size_t j, const T_ &a) |
Assign a value to an entry of the matrix. More... | |
void | add (size_t i, size_t j, const T_ &a) |
Add a value to an entry of the matrix. More... | |
void | set (vector< int > &ir, vector< int > &ic, vector< T_ > &val) |
Assign values to a portion of the matrix. More... | |
void | operator= (const T_ &a) |
Operator = More... | |
void | clear () |
Set all matrix entries to zero. | |
void | Laplace1D (real_t h, bool mpi=false) |
Sets the matrix as the one for the Laplace equation in 1-D. More... | |
void | Laplace2D (size_t nx, size_t ny, bool mpi=false) |
Sets the matrix as the one for the Laplace equation in 2-D. More... | |
int | solve (PETScVect< T_ > &b) |
Solve the linear system of equations. More... | |
int | solve (const PETScVect< T_ > &b, PETScVect< T_ > &x) |
Solve the linear system of equations. More... | |
void | setSolver (string solver, string prec, real_t toler=1.e-12, int max_it=1000) |
Choose solver and preconditioner for an iterative procedure. More... | |
T_ * | get () const |
Return C-Array. More... | |
T_ | get (size_t i, size_t j) const |
Return entry (i,j) of matrix if this one is stored, 0 otherwise. More... | |
operator Mat () const | |
Casting operator. More... | |
PetscReal | getNorm1 () const |
Get 1-norm of matrix. | |
PetscReal | getFrobeniusNorm () const |
Get Frobenius norm of matrix. | |
PetscReal | getNormMax () const |
Get infinity norm of matrix. | |
void | Assembly (const Element &el, T_ *a) |
Assembly of element matrix into global matrix. More... | |
void | Assembly (const Side &sd, T_ *a) |
Assembly of side matrix into global matrix. More... | |
void | setAssembly () |
Matrix assembly. More... | |
void | setMPI () |
Activate MPI option. | |
Detailed Description
template<class T_>
class OFELI::PETScMatrix< T_ >
To handle matrices in sparse storage format using the Petsc library.
- Warning
- This class is available only when OFELI has been installed with Petsc.
- Template Parameters
-
T_ Data type (double, float, complex<double>, ...)
Constructor & Destructor Documentation
PETScMatrix | ( | ) |
Default constructor.
Initialize a zero-dimension matrix
PETScMatrix | ( | size_t | nr, |
size_t | nc | ||
) |
Constructor that initializes current instance as a dense matrix.
Normally, for a dense matrix this is not the right class.
- Parameters
-
[in] nr Number of matrix rows. [in] nc Number of matrix columns.
PETScMatrix | ( | size_t | size | ) |
Constructor that initializes current instance as a dense matrix.
Normally, for a dense matrix this is not the right class.
- Parameters
-
[in] size Number of matrix rows (and columns).
PETScMatrix | ( | Mesh & | mesh, |
size_t | dof = 0 |
||
) |
Constructor using a Mesh instance.
- Parameters
-
[in] mesh Mesh instance from which matrix graph is extracted. [in] dof Option parameter, with default value 0
.
dof=1
means that only one degree of freedom for each node (or element or side) is taken to determine matrix structure. The valuedof=0
means that matrix structure is determined using all DOFs.
PETScMatrix | ( | const vector< std::pair< size_t, size_t > > & | I, |
int | opt = 1 |
||
) |
Constructor for a square matrix using non zero row and column indices.
- Parameters
-
[in] I Vector containing pairs of row and column indices [in] opt Flag indicating if vectors I is cleaned and ordered (opt=1) or not (opt=0). In the latter case, this vector can have the same contents more than once and are not necessarily ordered
Member Function Documentation
void setAIJ | ( | const vector< int > & | nnz | ) |
- Parameters
-
[in] nnz
void setAIJ_MPI | ( | const vector< int > & | diag_nnz, |
const vector< int > & | off_nnz | ||
) |
- Parameters
-
[in] diag_nnz [in] off_nnz
void setMesh | ( | Mesh & | mesh, |
size_t | dof = 0 |
||
) |
Determine mesh graph and initialize matrix.
This member function is called by constructor with the same arguments
- Parameters
-
[in] mesh Mesh instance for which matrix graph is determined. [in] dof Option parameter, with default value 0
.
dof=1
means that only one degree of freedom for each node (or element or side) is taken to determine matrix structure. The valuedof=0
means that matrix structure is determined using all DOFs.
void setPartition | ( | Partition & | p | ) |
void setRank | ( | int | np, |
int | r = 0 |
||
) |
Set number of processors and processor rank.
- Parameters
-
[in] np Total number of processors. [in] r Rank of current processor [Default: 0
- Warning
- If this member function is not called, only one processor is used and then sequential computing is involved.
Impose by a diagonal method an essential boundary condition using the Mesh instance provided by the constructor.
This member function modifies diagonal terms in matrix and terms in vector that correspond to degrees of freedom with nonzero code in order to impose a boundary condition. The penalty parameter is defined by default equal to 1.e20. It can be modified by member function setPenal(..).
void setSize | ( | size_t | size | ) |
Set size of matrix (case where it's a square matrix).
- Parameters
-
[in] size Number of rows and columns.
void setSize | ( | size_t | nr, |
size_t | nc | ||
) |
Set size (number of rows) of matrix.
- Parameters
-
[in] nr Number of rows [in] nc Number of columns
void getRange | ( | int | istart, |
int | iend | ||
) |
Return the range of matrix rows owned by this processor.
- Parameters
-
[out] istart Index of the first local row [out] iend Index of the last local row
void setGraph | ( | const vector< std::pair< size_t, size_t > > & | I, |
int | opt = 1 |
||
) |
Set graph of matrix by giving a vector of its nonzero entries.
- Parameters
-
[in] I Vector containing pairs of row and column indices [in] opt Flag indicating if vector I
is cleaned and ordered (opt=1
: default) or not (opt=0
). In the latter case, this vector can have the same contents more than once and are not necessarily ordered
T_ operator() | ( | size_t | i, |
size_t | j | ||
) | const |
Operator ()
- Parameters
-
[in] i Row index [in] j Column index
size_t getLength | ( | ) | const |
Return length of matrix.
The length is the total number of stored elements in the matrix
Multiply matrix by vector and save in another one.
- Parameters
-
[in] x Vector to multiply by matrix [out] y Vector that contains on output the result.
Multiply matrix by vector x
and add to y
.
- Parameters
-
[in] x Vector to multiply by matrix [out] y Vector to add to the result. y
contains on output the result.
Multiply matrix by vector a*x
and add to y
.
- Parameters
-
[in] a Constant to multiply by matrix [in] x Vector to multiply by matrix [out] y Vector to add to the result. y
contains on output the result.
void set | ( | size_t | i, |
size_t | j, | ||
const T_ & | a | ||
) |
Assign a value to an entry of the matrix.
- Parameters
-
[in] i Row index [in] j Column index [in] a Value to assign to a(i,j)
void add | ( | size_t | i, |
size_t | j, | ||
const T_ & | a | ||
) |
Add a value to an entry of the matrix.
- Parameters
-
[in] i Row index [in] j Column index [in] a Constant value to add to a(i,j)
void set | ( | vector< int > & | ir, |
vector< int > & | ic, | ||
vector< T_ > & | val | ||
) |
Assign values to a portion of the matrix.
- Parameters
-
[in] ir Vector of row indexes to assign (instance of class vector
)[in] ic Vector of column indexes to assign (instance of class vector
)[in] val Vector of values to assign (instance of class vector
)
void operator= | ( | const T_ & | a | ) |
Operator =
Assign constant value a
to matrix diagonal entries
void Laplace1D | ( | real_t | h, |
bool | mpi = false |
||
) |
Sets the matrix as the one for the Laplace equation in 1-D.
The matrix is initialized as the one resulting from P1 finite element discretization of the classical elliptic operator -u'' = f with homogeneous Dirichlet boundary conditions
- Remarks
- This function is available for real valued matrices only.
- Parameters
-
[in] h Mesh size (assumed constant) [in] mpi true if MPI is used for parallel computing, false if not (sequential), [Default: false
]
void Laplace2D | ( | size_t | nx, |
size_t | ny, | ||
bool | mpi = false |
||
) |
Sets the matrix as the one for the Laplace equation in 2-D.
The matrix is initialized as the one resulting from P1 finite element discretization of the classical elliptic operator -Delta u = f with homogeneous Dirichlet boundary conditions
- Remarks
- This function is available for real valued matrices only.
- Parameters
-
[in] nx Number of unknowns in the x
-direction[in] ny Number of unknowns in the y
-direction[in] mpi true if MPI is used for parallel computing, false if not (sequential), [Default: false
]
int solve | ( | PETScVect< T_ > & | b | ) |
Solve the linear system of equations.
The default parameters are: the Conjugate Gradient method and the Jacobi method for preconditioner. To change these values, call function setSolver before this function
- Parameters
-
[in,out] b Vector that contains right-hand side on input and solution on output
- Returns
- Number of actual performed iterations
Solve the linear system of equations.
The default parameters are: the Conjugate Gradient method and the Jacobi method for preconditioner. To change these values, call function setSolver before this function
- Parameters
-
[in] b Vector that contains right-hand side [out] x Vector that contains the obtained solution
- Returns
- Number of actual performed iterations
void setSolver | ( | string | solver, |
string | prec, | ||
real_t | toler = 1.e-12 , |
||
int | max_it = 1000 |
||
) |
Choose solver and preconditioner for an iterative procedure.
- Parameters
-
[in] solver Option to choose iterative solver among the macros (see PETSc documentation for more details): -
KSPRICHARDSON
: The Richardson iterative method (Default damping parameter is1.0
) -
KSPCHEBYSHEV
: The Chebyshev iterative method -
KSPCG
: The conjugate gradient method [Default] -
KSPCGNE
: The CG method for normal equations (without explicitly forming the productA^TA
-
KSPGMRES
: The GMRES iterative method (see A Generalized Minimal Residual Algorithm for Solving Nonsymmetric Linear Systems. Y. Saad and M. H. Schultz, SIAM J. Sci. Stat. Comput. Vo|. 7, No. 3, July 1986, pp. 856-869) -
KSPFGMRES
: The Flexible GMRES method (with restart) -
KSPLGMRES
: The 'augmented' standard GMRES method where the subspace uses approximations to the error from previous restart cycles -
KSPTCQMR
: A variant of QMR (quasi minimal residual) developed by Tony Chan -
KSPBCGS
: The BiCGStab (Stabilized version of BiConjugate Gradient Squared) method -
KSPIBCGS
: The IBiCGStab (Improved Stabilized version of BiConjugate Gradient Squared) method in an alternative form to have only a single global reduction operation instead of the usual 3 (or 4) -
KSPFBCGS
: The flexible BiCGStab method. -
KSPCGS
: The CGS (Conjugate Gradient Squared) method -
KSPTFQMR
: A transpose free QMR (quasi minimal residual) -
KSPCR
: The conjugate residuals method -
KSPLSQR
: The LSQR method -
KSPBICG
: The Biconjugate gradient method (similar to running the conjugate gradient on the normal equations) -
KSPMINRES
: The MINRES (Minimum Residual) method -
KSPSYMMLQ
: The SYMMLQ method -
KSPGCR
: The Generalized Conjugate Residual method
[in] prec Option to choose preconditioner in an enumerated variable -
PCJACOBI
: [Default] Jacobi (i.e. diagonal scaling) preconditioning -
PCBJACOBI
: Block Jacobi preconditioning, each block is (approximately) solved with its own KSP object -
PCSOR
: (S)SOR (successive over relaxation, Gauss-Seidel) preconditioning -
PCEISENSTAT
: An implementation of SSOR (symmetric successive over relaxation, symmetric Gauss-Seidel) preconditioning that incorporates Eisenstat's trick to reduce the amount of computation needed -
PCICC
: Incomplete Cholesky factorization preconditioners -
PCILU
: Incomplete factorization preconditioners -
PCASM
: Use the (restricted) additive Schwarz method, each block is (approximately) solved with its own KSP object -
PCLU
: Uses a direct solver, based on LU factorization, as a preconditioner -
PCCHOLESKY
: Uses a direct solver, based on Cholesky factorization, as a preconditioner
[in] toler Tolerance for convergence [Default: 1.e-12
][in] max_it Maximum number of allowed iterations [Default: 1000
] -
T_* get | ( | ) | const |
Return C-Array.
Non zero terms of matrix is stored row by row.
T_ get | ( | size_t | i, |
size_t | j | ||
) | const |
Return entry (i,j)
of matrix if this one is stored, 0
otherwise.
- Parameters
-
[in] i Row index [in] j Column index
operator Mat | ( | ) | const |
Casting operator.
This member functions enables casting an instance of class PETScMatrix into the Petsc
matrix type Mat
. This is useful when one wants to usr any Petsc function that is not available in the wrapper (class PETScWrapper) or PETScMatrix.
void Assembly | ( | const Element & | el, |
T_ * | a | ||
) |
Assembly of element matrix into global matrix.
- Parameters
-
[in] el Reference to element instance [in] a Element matrix as a C-array
void Assembly | ( | const Side & | sd, |
T_ * | a | ||
) |
Assembly of side matrix into global matrix.
- Parameters
-
[in] sd Reference to side instance [in] a Side matrix as a C-array
void setAssembly | ( | ) |
Matrix assembly.
This function assembles matrix (begins and ends)