Peano
Loading...
Searching...
No Matches
petsc::LinearEquationSystem Class Reference

Class to capture all of the PETSc objects needed by some experiements. More...

#include <LinearEquationSystem.h>

Collaboration diagram for petsc::LinearEquationSystem:

Public Types

enum class  PreconditionerType : int { None , LU , JACOBI }
 Enum class to allow us to customise different different petsc preconditioner types. More...
 
enum class  KSPSolverType : int { GMRES , CG }
 Enum class to allow us to customise different different petsc solver types. More...
 

Public Member Functions

 LinearEquationSystem ()
 Trivial constructor.
 
virtual ~LinearEquationSystem ()
 
void printMatrix ()
 Overload to print the matrix to terminal.
 
void printMatrix (std::string filename)
 Overload to print the matrix to file.
 
void printRhs ()
 Overload to print the rhs to terminal.
 
void printRhs (std::string filename)
 Overload to print the rhs to file.
 
void printX ()
 Overload to print the solution to terminal.
 
void printX (std::string filename)
 Overload to print the solution to terminal.
 
int getDoFs () const
 Returns number of DoFs in the system.
 
void init (int dofs)
 method to intialise the matrix and two vectors each we _dofs entries
 
double get (int i) const
 Getter method to inspect the value of the solution vector at a given index.
 
void insert (int row, int col, double val)
 Overload to insert val into the matrix, at given row, column position.
 
void increment (int row, int col, double val)
 Overload to increment existing val in the matrix, at given row, column position.
 
void insert (int row, double val)
 Overload to insert val into the rhs vector, at given row position.
 
void increment (int row, double val)
 Overload to increment existing val in the rhs vector, at given row position.
 
template<int rows, int columns>
void insertMatrix (int row, int col, const tarch::la::Matrix< rows, columns, double > &stencil)
 Method to insert a tarch::la::Matrix into the PETSc matrix.
 
void solve (PreconditionerType pctype=PreconditionerType::None, KSPSolverType ksptype=KSPSolverType::GMRES)
 Method to hand over to petsc and solve.
 
void cleanUpPetscObjects ()
 Method to clean up PETSc objects.
 
PCType getPreconditionerTypeFromEnum (PreconditionerType type)
 
KSPType getKSPSolverTypeFromEnum (KSPSolverType type)
 

Private Attributes

int _dofs
 
Mat _A
 
Vec _x
 
Vec _b
 

Static Private Attributes

static tarch::logging::Log _log
 

Detailed Description

Class to capture all of the PETSc objects needed by some experiements.

The default constructor will set the number of "dofs" to be -1. The initialisation of most of the PETSc objects are deferred until the true number of dofs is known, so as not to interfere with Peano's MPI handing.

This class supports creating a (square) matrix (member variable _A), a RHS vector (_b), and a member variable _x, which will have the solution written into it.

The intended workflow is as follows:

  • Call the constructor at some early stage during the main function
  • Use Peano to establish how many true degrees of freedom you wish to have
  • Send this number into the init() routine. This will set the sizes of the PETSc matrices and vectors
  • Use the increment() function to insert values into the matrix and rhs vector. There is an overload for each. The solution vector currently is set to all zeros.
  • Handover to PETSc to solve by calling the solve() routine. We currently use no preconditioner and a GMRES solver. These options can be seen in the body of solve()
  • Use the get() routine to inspect the solution vector at each index.

At present, we only support a handful of preconditioners and solvers, each of which can yield quite different results. More info on which preconditioners are supported can be found here and solvers can be found here (see table 6).

Definition at line 46 of file LinearEquationSystem.h.

Member Enumeration Documentation

◆ KSPSolverType

Enum class to allow us to customise different different petsc solver types.

See documentation of this class for more info on what solvers are supported within petsc.

Enumerator
GMRES 
CG 

Definition at line 177 of file LinearEquationSystem.h.

◆ PreconditionerType

Enum class to allow us to customise different different petsc preconditioner types.

See documentation of this class for more info on what preconditioners are supported within petsc.

Enumerator
None 
LU 
JACOBI 

Definition at line 166 of file LinearEquationSystem.h.

Constructor & Destructor Documentation

◆ LinearEquationSystem()

petsc::LinearEquationSystem::LinearEquationSystem ( )

Trivial constructor.

Sets the dofs to be -1. Modify this in init()

◆ ~LinearEquationSystem()

virtual petsc::LinearEquationSystem::~LinearEquationSystem ( )
virtual

Member Function Documentation

◆ cleanUpPetscObjects()

void petsc::LinearEquationSystem::cleanUpPetscObjects ( )

Method to clean up PETSc objects.

Call this in some late stage during the main function. We would ideally put these in the destructor of this class, but the destructor is typically called after Peano has already cleaned up its MPI routines. This causes a (trivial) MPI error at the very end of execution. For now, we put this stuff here so as not to cause any issues.

◆ get()

double petsc::LinearEquationSystem::get ( int  i) const

Getter method to inspect the value of the solution vector at a given index.

Only call after solve() has completed.

Parameters
inti

◆ getDoFs()

int petsc::LinearEquationSystem::getDoFs ( ) const

Returns number of DoFs in the system.

Equivalent to number of rows in the matrix, and number of entries in the solution/rhs vector

◆ getKSPSolverTypeFromEnum()

KSPType petsc::LinearEquationSystem::getKSPSolverTypeFromEnum ( KSPSolverType  type)

◆ getPreconditionerTypeFromEnum()

PCType petsc::LinearEquationSystem::getPreconditionerTypeFromEnum ( PreconditionerType  type)

◆ increment() [1/2]

void petsc::LinearEquationSystem::increment ( int  row,
double  val 
)

Overload to increment existing val in the rhs vector, at given row position.

Parameters
introw - row index of where we insert
doubleval - value to insert

◆ increment() [2/2]

void petsc::LinearEquationSystem::increment ( int  row,
int  col,
double  val 
)

Overload to increment existing val in the matrix, at given row, column position.

Parameters
introw - row index of where we insert
intcol - col index of where we insert
doubleval - value to insert

◆ init()

void petsc::LinearEquationSystem::init ( int  dofs)

method to intialise the matrix and two vectors each we _dofs entries

Parameters
intdofs

◆ insert() [1/2]

void petsc::LinearEquationSystem::insert ( int  row,
double  val 
)

Overload to insert val into the rhs vector, at given row position.

Parameters
introw - row index of where we insert
doubleval - value to insert

◆ insert() [2/2]

void petsc::LinearEquationSystem::insert ( int  row,
int  col,
double  val 
)

Overload to insert val into the matrix, at given row, column position.

Parameters
introw - row index of where we insert
intcol - col index of where we insert
doubleval - value to insert

◆ insertMatrix()

template<int rows, int columns>
void petsc::LinearEquationSystem::insertMatrix ( int  row,
int  col,
const tarch::la::Matrix< rows, columns, double > &  stencil 
)

Method to insert a tarch::la::Matrix into the PETSc matrix.

The (0,0) entry of the supplied matrix will be placed at (row, col) in the PETSc matrix. We put the implementation in the header so that the templates work.

Parameters
introw - starting row where we wish to insert
intcol - starting col where we wish to insert
consttarch::la::Matrix<rows,columns,double>& stencil

Definition at line 145 of file LinearEquationSystem.h.

References _A.

◆ printMatrix() [1/2]

void petsc::LinearEquationSystem::printMatrix ( )

Overload to print the matrix to terminal.

◆ printMatrix() [2/2]

void petsc::LinearEquationSystem::printMatrix ( std::string  filename)

Overload to print the matrix to file.

Parameters
std::stringfilename

◆ printRhs() [1/2]

void petsc::LinearEquationSystem::printRhs ( )

Overload to print the rhs to terminal.

◆ printRhs() [2/2]

void petsc::LinearEquationSystem::printRhs ( std::string  filename)

Overload to print the rhs to file.

Parameters
std::stringfilename

◆ printX() [1/2]

void petsc::LinearEquationSystem::printX ( )

Overload to print the solution to terminal.

◆ printX() [2/2]

void petsc::LinearEquationSystem::printX ( std::string  filename)

Overload to print the solution to terminal.

Parameters
std::stringfilename

◆ solve()

void petsc::LinearEquationSystem::solve ( PreconditionerType  pctype = PreconditionerType::None,
KSPSolverType  ksptype = KSPSolverType::GMRES 
)

Method to hand over to petsc and solve.

Field Documentation

◆ _A

Mat petsc::LinearEquationSystem::_A
private

Definition at line 206 of file LinearEquationSystem.h.

Referenced by insertMatrix().

◆ _b

Vec petsc::LinearEquationSystem::_b
private

Definition at line 208 of file LinearEquationSystem.h.

◆ _dofs

int petsc::LinearEquationSystem::_dofs
private

Definition at line 204 of file LinearEquationSystem.h.

◆ _log

tarch::logging::Log petsc::LinearEquationSystem::_log
staticprivate

Definition at line 202 of file LinearEquationSystem.h.

◆ _x

Vec petsc::LinearEquationSystem::_x
private

Definition at line 207 of file LinearEquationSystem.h.


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