IFEM  90A354
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
PETScMatrix Class Reference

Class for representing the system matrix in PETSc format. More...

#include <PETScMatrix.h>

Inheritance diagram for PETScMatrix:
Inheritance graph
[legend]
Collaboration diagram for PETScMatrix:
Collaboration graph
[legend]

Public Member Functions

 PETScMatrix (const ProcessAdm &padm, const LinSolParams &spar)
 Constructor.
 
 ~PETScMatrix () override
 The destructor frees the dynamically allocated arrays.
 
LinAlg::MatrixType getType () const override
 Returns the matrix type.
 
SystemMatrixcopy () const override
 Creates a copy of the system matrix and returns a pointer to it.
 
size_t dim (int idim=1) const override
 Returns the dimension of the system matrix. More...
 
bool init (int maxEq, const IntMat *elms=nullptr, const IntMat *neighs=nullptr, const IntVec *part=nullptr)
 Initializes a scalar matrix based on element connections. More...
 
void preAssemble (const std::vector< IntVec > &MMNPC, size_t nel) override
 Initializes the equation sparsity pattern based on element connections. More...
 
void initAssembly (const SAM &sam, char) override
 Initializes the element assembly process. More...
 
void init () override
 Initializes the matrix to zero assuming it is properly dimensioned.
 
bool assemble (const Matrix &eM, const SAM &sam, int e) override
 Adds an element matrix into the associated system matrix. More...
 
bool assemble (const Matrix &eM, const SAM &sam, SystemVector &B, int e) override
 Adds an element matrix into the associated system matrix. More...
 
bool assemble (const Matrix &eM, const SAM &sam, SystemVector &B, const IntVec &meq) override
 Adds an element matrix into the associated system matrix. More...
 
bool assemble (const Matrix &eM, const IntVec &meq) override
 Adds an element matrix into the associated system matrix. More...
 
bool endAssembly () override
 Finalizes the system matrix assembly.
 
bool multiply (const SystemVector &B, SystemVector &C) const override
 Performs the matrix-vector multiplication C = *this * B.
 
bool solve (SystemVector &B, Real *) override
 Solves the linear system of equations for a given right-hand-side. More...
 
bool solve (const SystemVector &B, SystemVector &x) override
 Solves the linear system of equations for a given right-hand-side. More...
 
void mult (Real alpha) override
 Multiplication with a scalar.
 
bool solveEig (PETScMatrix &B, RealArray &eigVal, Matrix &eigVec, int nev, Real shift=Real(0), int iop=1)
 Solves a generalized symmetric-definite eigenproblem. More...
 
Real Linfnorm () const override
 Returns the L-infinity norm of the matrix.
 
Mat & getMatrix ()
 Returns the PETSc matrix (for assignment).
 
const Mat & getMatrix () const
 Returns the PETSc matrix (for read access).
 
const std::vector< Mat > & getBlockMatrices () const
 Get vector of block matrices. Used for tests only.
 
const std::vector< IS > & getIS () const
 Get vector of index sets.
 
bool setParameters (bool setup)
 Set the linear solver parameters (solver type, preconditioner, tolerances). More...
 
const ProcessAdmgetAdm () const
 Returns a const-ref to process administrator.
 
bool solveMultipleRhs (PETScVectors &B, Matrix &sField)
 Solve for multiple right-hand-side vectors. More...
 
const DomainDecompositiongetDD () const
 Returns a const-ref to domain decompositioning.
 
bool add (Real sigma, int ieq) override
 Adds the constant σ to the diagonal of this matrix.
 
- Public Member Functions inherited from SystemMatrix
virtual ~SystemMatrix ()
 Empty destructor.
 
virtual bool lockPattern (bool)
 Locks or unlocks the sparsity pattern.
 
virtual bool empty () const
 Checks if the matrix is empty.
 
bool isZero () const
 Checks if the matrix have no non-zero contributions.
 
virtual void compressPattern ()
 Compresses the sparsity pattern.
 
void initNonZeroEqs ()
 Initializes the nonZeroEqs flags.
 
bool flagNonZeroEqs (const IntVec &meq={})
 Flags the equations meq as pivots with non-zero contributions.
 
bool flagNonZeroEqs (const SystemMatrix &B)
 Flags the non-zero equations from B as non-zero pivots in this.
 
virtual bool augment (const SystemMatrix &, size_t, size_t)
 Augments a similar matrix symmetrically to the current matrix.
 
virtual bool truncate (Real=Real(1.0e-16))
 Truncates all small matrix elements to zero.
 
virtual bool add (const SystemMatrix &, Real=Real(1))
 Adds a matrix with similar structure to the current matrix.
 
virtual void dump (std::ostream &, LinAlg::StorageFormat, const char *=nullptr)
 Dumps the system matrix on a specified format.
 
virtual void dump (const char *fileName, std::streamsize precision=0, LinAlg::StorageFormat format=LinAlg::FLAT)
 Dumps the system matrix to specified file.
 
virtual bool load (const char *, bool=false)
 Loads the system matrix from specified file.
 
StdVector operator* (const SystemVector &b) const
 Calculates a matrix-vector product.
 
StdVector operator/ (const SystemVector &b)
 Solves a linear equation system.
 

Protected Member Functions

bool solve (const Vec &b, Vec &x, bool knoll)
 Solve a linear system.
 
 PETScMatrix (const PETScMatrix &A)=delete
 Disabled copy constructor.
 
 PETScMatrix (const ProcessAdm &padm, const PETScSolParams &spar)
 Clone matrix data.
 
void setupSparsity (const IntVec &elms, const SAM &sam)
 Setup sparsity pattern for model. More...
 
void setupBlockSparsity (const IntVec &elms, const SAM &sam)
 Setup sparsity pattern for block-matrices for a model.
 
void setupGlb2Blk (const SAM &sam)
 Calculates blocks for global eqs.
 
Mat preAllocator (const int nrows, const int ncols=0) const
 Sets up preallocator matrix.
 
std::vector< Mat > preAllocators () const
 Sets up preallocator matrices for blocks.
 
- Protected Member Functions inherited from SystemMatrix
 SystemMatrix ()
 Default constructor.
 
 SystemMatrix (const SystemMatrix &a)
 Copy constructor.
 
virtual std::ostream & write (std::ostream &os) const
 Writes the system matrix to the given output stream.
 

Protected Attributes

Mat pA
 The actual PETSc matrix.
 
PetscInt nrow
 Number of matrix rows.
 
PetscInt ncol
 Number of matrix columns.
 
KSP ksp
 Linear equation solver.
 
MatNullSpace * nsp
 Null-space of linear operator.
 
const ProcessAdmadm
 Process administrator.
 
PETScSolParams solParams
 Linear solver parameters.
 
bool setParams
 If linear solver parameters are set.
 
std::string forcedKSPType
 Force a KSP type ignoring the parameters.
 
PetscInt ISsize
 Number of index sets/elements.
 
PetscRealVec coords
 Coordinates of local nodes (x0,y0,z0,x1,y1,...)
 
ISMat dirIndexSet
 Direction ordering.
 
int nLinSolves
 Number of linear solves.
 
bool assembled
 True if PETSc matrix has been assembled.
 
bool factored
 True if PETsc matrix has been factored.
 
IS glob2LocEq = nullptr
 Index set for global-to-local equations.
 
std::vector< Mat > matvec
 Blocks for block matrices.
 
std::vector< ISisvec
 Index sets for blocks.
 
std::vector< std::array< int, 2 > > glb2Blk
 Maps equations to block and block eq.
 
std::unique_ptr< DomainDecompositionm_dd {}
 Internal partitioning information.
 

Additional Inherited Members

- Static Public Member Functions inherited from SystemMatrix
static SystemMatrixcreate (const ProcessAdm *adm, LinAlg::MatrixType mType, int num_thread_SLU=1)
 Static method creating a matrix of the given type.
 
static SystemMatrixcreate (const ProcessAdm *adm, LinAlg::MatrixType mType, const LinSolParams &spar)
 Static method creating a matrix of the given type.
 

Detailed Description

Class for representing the system matrix in PETSc format.

It is an interface to PETSc modules for assembling and solving linear systems of equations.

Member Function Documentation

◆ assemble() [1/4]

bool PETScMatrix::assemble ( const Matrix eM,
const IntVec meq 
)
overridevirtual

Adds an element matrix into the associated system matrix.

Parameters
[in]eMThe element matrix
[in]meqMatrix of element equation numbers
Returns
true on successful assembly, otherwise false

Implements SystemMatrix.

References m_dd, and pA.

◆ assemble() [2/4]

bool PETScMatrix::assemble ( const Matrix eM,
const SAM sam,
int  e 
)
overridevirtual

Adds an element matrix into the associated system matrix.

Parameters
[in]eMThe element matrix
[in]samAuxiliary data for FE assembly management
[in]eIdentifier for the element that eM belongs to
Returns
true on successful assembly, otherwise false

Implements SystemMatrix.

References adm, ProcessAdm::dd, SystemMatrix::flagNonZeroEqs(), SAM::getElmEqns(), glb2Blk, SAM::meqn, SAM::mmceq, SAM::mpmceq, utl::matrix< T >::rows(), and SAM::ttcc.

◆ assemble() [3/4]

bool PETScMatrix::assemble ( const Matrix eM,
const SAM sam,
SystemVector B,
const IntVec meq 
)
overridevirtual

Adds an element matrix into the associated system matrix.

Parameters
[in]eMThe element matrix
[in]samAuxiliary data for FE assembly management
BThe system right-hand-side vector
[in]meqMatrix of element equation numbers
Returns
true on successful assembly, otherwise false

When multi-point constraints are present, contributions from these are also added into the system right-hand-side vector, B.

Implements SystemMatrix.

References adm, utl::matrix< T >::cols(), ProcessAdm::dd, SystemMatrix::flagNonZeroEqs(), glb2Blk, SAM::meqn, SAM::mmceq, SAM::mpmceq, utl::matrix< T >::rows(), and SAM::ttcc.

◆ assemble() [4/4]

bool PETScMatrix::assemble ( const Matrix eM,
const SAM sam,
SystemVector B,
int  e 
)
overridevirtual

Adds an element matrix into the associated system matrix.

Parameters
[in]eMThe element matrix
[in]samAuxiliary data for FE assembly management
BThe system right-hand-side vector
[in]eIdentifier for the element that eM belongs to
Returns
true on successful assembly, otherwise false

When multi-point constraints are present, contributions from these are also added into the system right-hand-side vector, B.

Implements SystemMatrix.

References adm, ProcessAdm::dd, SystemMatrix::flagNonZeroEqs(), SAM::getElmEqns(), glb2Blk, SAM::meqn, SAM::mmceq, SAM::mpmceq, utl::matrix< T >::rows(), and SAM::ttcc.

◆ dim()

size_t PETScMatrix::dim ( int  idim = 1) const
overridevirtual

Returns the dimension of the system matrix.

Parameters
[in]idimWhich direction to return the dimension in.

Implements SystemMatrix.

References ncol, and nrow.

Referenced by _eig_ax(), and eig_sol_().

◆ init()

bool PETScMatrix::init ( int  maxEq,
const IntMat elms = nullptr,
const IntMat neighs = nullptr,
const IntVec part = nullptr 
)

Initializes a scalar matrix based on element connections.

Parameters
[in]maxEqMaximum equation number
[in]elmsElement nodal connection
[in]neighsElement graph
[in]partPartitioning to use

References adm, ProcessAdm::dd, DomainDecomposition::isPartitioned(), m_dd, ncol, nrow, and pA.

Referenced by ASMbase::globalL2projection().

◆ initAssembly()

void PETScMatrix::initAssembly ( const SAM sam,
char   
)
overridevirtual

Initializes the element assembly process.

Parameters
[in]samAuxiliary data describing the FE model topology, etc.

Must be called once before the element assembly loop. The PETSc data structures are initialized and the all symbolic operations that are needed before the actual assembly can start are performed.

Implements SystemMatrix.

References adm, assembled, ProcessAdm::dd, DomainDecomposition::getBlockEqs(), DomainDecomposition::getElms(), DomainDecomposition::getGlobalEq(), PETScSolParams::getLinSysType(), DomainDecomposition::getMaxEq(), DomainDecomposition::getMinEq(), DomainDecomposition::getNoBlocks(), PETScSolParams::getNoBlocks(), DomainDecomposition::isPartitioned(), isvec, matvec, ncol, SAM::nel, nrow, pA, setupBlockSparsity(), setupSparsity(), solParams, LinAlg::SPD, and LinAlg::SYMMETRIC.

◆ preAssemble()

void PETScMatrix::preAssemble ( const std::vector< IntVec > &  MMNPC,
size_t  nel 
)
overridevirtual

Initializes the equation sparsity pattern based on element connections.

Parameters
[in]MMNPCMatrix of matrices of nodal point correspondances
[in]nelNumber of elements

Reimplemented from SystemMatrix.

References endAssembly(), m_dd, nrow, pA, and preAllocator().

◆ setParameters()

bool PETScMatrix::setParameters ( bool  setup)

Set the linear solver parameters (solver type, preconditioner, tolerances).

Parameters
[in]setupTrue to setup KSP/PC
Returns
True on success

References adm, ProcessAdm::dd, forcedKSPType, PETScSolParams::getBlock(), DomainDecomposition::getBlockEqs(), PETScSolParams::getDoubleValue(), PETScSolParams::getIntValue(), SettingMap::getStringValue(), PETScSolParams::getStringValue(), isvec, ksp, matvec, PETScSolParams::setupPC(), and solParams.

Referenced by solve(), and solveEig().

◆ setupSparsity()

void PETScMatrix::setupSparsity ( const IntVec elms,
const SAM sam 
)
protected

Setup sparsity pattern for model.

Parameters
[in]elmsElements on this process
[in]samAuxiliary data describing the FE model topology, etc.

References adm, ProcessAdm::dd, endAssembly(), DomainDecomposition::getMaxEq(), DomainDecomposition::getMinEq(), pA, and preAllocator().

Referenced by initAssembly().

◆ solve() [1/2]

bool PETScMatrix::solve ( const SystemVector B,
SystemVector x 
)
overridevirtual

Solves the linear system of equations for a given right-hand-side.

Parameters
[in]BRight-hand-side vector
[out]xSolution vector

Reimplemented from SystemMatrix.

References PETScVector::getVector(), and solve().

◆ solve() [2/2]

bool PETScMatrix::solve ( SystemVector B,
Real  
)
overridevirtual

Solves the linear system of equations for a given right-hand-side.

Parameters
BRight-hand-side vector on input, solution vector on output

Implements SystemMatrix.

References PETScSolParams::getStringValue(), PETScVector::getVector(), and solParams.

Referenced by solve(), and solveMultipleRhs().

◆ solveEig()

bool PETScMatrix::solveEig ( PETScMatrix B,
RealArray eigVal,
Matrix eigVec,
int  nev,
Real  shift = Real(0),
int  iop = 1 
)

Solves a generalized symmetric-definite eigenproblem.

The eigenproblem is assumed to be on the form A x = λ B x where A ( = *this ) and B both are assumed to be symmetric and B also to be positive definite. The eigenproblem is solved by the SLEPc library subroutine EPSSolve.

See also
SLEPc library documentation.
Parameters
BSymmetric and positive definite mass matrix.
[out]eigValComputed eigenvalues
[out]eigVecComputed eigenvectors stored column by column
[in]nevThe number of eigenvalues and eigenvectors to compute
[in]shiftEigenvalue shift
[in]iopOption telling whether to factorize matrix A or B.

References adm, ProcessAdm::dd, utl::matrix< T >::fillColumn(), DomainDecomposition::getG2LEQ(), PETScSolParams::getIntValue(), DomainDecomposition::isPartitioned(), ksp, pA, utl::matrix< T >::resize(), setParameters(), and solParams.

Referenced by eig::solve().

◆ solveMultipleRhs()

bool PETScMatrix::solveMultipleRhs ( PETScVectors B,
Matrix sField 
)

Solve for multiple right-hand-side vectors.

Parameters
BVectors with right-hand-sides to solve for
sFieldResulting vectors stored as a matrix

References PETScVectors::dim(), PETScVectors::get(), m_dd, PETScVectors::size(), and solve().


The documentation for this class was generated from the following files: