IFEM  90A354
PETScMatrix.h
Go to the documentation of this file.
1 // $Id$
2 //==============================================================================
13 //==============================================================================
14 
15 #ifndef _PETSC_MATRIX_H
16 #define _PETSC_MATRIX_H
17 
18 #include "DomainDecomposition.h"
19 #include "SystemMatrix.h"
20 #include "PETScSupport.h"
21 #include "PETScSolParams.h"
22 
23 #include <array>
24 #include <memory>
25 
26 using PetscIntVec = std::vector<PetscInt>;
27 using PetscIntMat = std::vector<PetscIntVec>;
28 using PetscRealVec = std::vector<PetscReal>;
29 using ISVec = std::vector<IS>;
30 using ISMat = std::vector<ISVec>;
31 
32 
39 class PETScVector : public StdVector
40 {
41 public:
43  explicit PETScVector(const ProcessAdm& padm);
45  PETScVector(const ProcessAdm& padm, size_t n);
47  PETScVector(const ProcessAdm& padm, const Real* values, size_t n);
49  PETScVector(const PETScVector& vec);
51  ~PETScVector() override;
52 
54  LinAlg::MatrixType getType() const override { return LinAlg::PETSC; }
55 
57  SystemVector* copy() const override { return new PETScVector(*this); }
58 
60  void init(Real value) override;
61 
63  void redim(size_t n) override;
64 
66  bool endAssembly() override;
67 
69  Real L1norm() const override;
70 
72  Real L2norm() const override;
73 
75  Real Linfnorm() const override;
76 
78  Vec& getVector() { return x; }
80  const Vec& getVector() const { return x; }
81 
83  const ProcessAdm& getAdm() const { return adm; }
84 
85 protected:
86  Vec x;
87  const ProcessAdm& adm;
88 };
89 
90 
91 class PETScMatrix;
92 
98 class PETScVectors : public SystemVector
99 {
100 public:
104  PETScVectors(const PETScMatrix& A, int nvec);
105 
107  ~PETScVectors();
108 
110  LinAlg::MatrixType getType() const override { return LinAlg::PETSC; }
111 
113  SystemVector* copy() const override { return nullptr; }
114 
116  size_t dim() const override { return myDim; }
117 
119  void redim(size_t) override {}
120 
122  Real* getPtr() override { return nullptr; }
124  const Real* getRef() const override { return nullptr; }
126  const Vector& vec() const override { static Vector empty; return empty; }
127 
129  void init(Real) override {}
130 
132  void mult(Real) override {}
133 
135  void add(const SystemVector&, Real) override {}
136 
138  Real L1norm() const override { return 0.0; }
139 
141  Real L2norm() const override { return 0.0; }
142 
144  Real Linfnorm() const override { return 0.0; }
145 
149  void assemble(const Vectors& vecs, const IntVec& meqn, int = 0) override;
150 
153  Vec get(size_t idx) { return vectors[idx]; }
154 
156  size_t size() const { return vectors.size(); }
157 
158 private:
159  size_t myDim;
160  std::vector<Vec> vectors;
161  const PETScMatrix& myA;
162 };
163 
164 
171 class PETScMatrix : public SystemMatrix
172 {
173 public:
175  PETScMatrix(const ProcessAdm& padm, const LinSolParams& spar);
177  ~PETScMatrix() override;
178 
180  LinAlg::MatrixType getType() const override { return LinAlg::PETSC; }
181 
183  SystemMatrix* copy() const override;
184 
187  size_t dim(int idim = 1) const override;
188 
194  bool init(int maxEq,
195  const IntMat* elms = nullptr,
196  const IntMat* neighs = nullptr,
197  const IntVec* part = nullptr);
198 
202  void preAssemble(const std::vector<IntVec>& MMNPC, size_t nel) override;
203 
210  void initAssembly(const SAM& sam, char) override;
211 
213  void init() override;
214 
220  bool assemble(const Matrix& eM, const SAM& sam, int e) override;
230  bool assemble(const Matrix& eM, const SAM& sam, SystemVector& B, int e) override;
231 
241  bool assemble(const Matrix& eM, const SAM& sam,
242  SystemVector& B, const IntVec& meq) override;
243 
248  bool assemble(const Matrix& eM, const IntVec& meq) override;
249 
251  bool endAssembly() override;
252 
254  bool multiply(const SystemVector& B, SystemVector& C) const override;
255 
258  bool solve(SystemVector& B, Real*) override;
259 
263  bool solve(const SystemVector& B, SystemVector& x) override;
264 
266  void mult(Real alpha) override;
267 
280  bool solveEig(PETScMatrix& B, RealArray& eigVal, Matrix& eigVec,
281  int nev, Real shift = Real(0), int iop = 1);
282 
284  Real Linfnorm() const override;
285 
287  Mat& getMatrix() { return pA; }
289  const Mat& getMatrix() const { return pA; }
290 
292  const std::vector<Mat>& getBlockMatrices() const { return matvec; }
293 
295  const std::vector<IS>& getIS() const { return isvec; }
296 
300  bool setParameters(bool setup);
301 
303  const ProcessAdm& getAdm() const { return adm; }
304 
308  bool solveMultipleRhs(PETScVectors& B, Matrix& sField);
309 
311  const DomainDecomposition& getDD() const;
312 
314  bool add(Real sigma, int ieq) override;
315 
316 protected:
318  bool solve(const Vec& b, Vec& x, bool knoll);
319 
321  PETScMatrix(const PETScMatrix& A) = delete;
322 
324  PETScMatrix(const ProcessAdm& padm,
325  const PETScSolParams& spar);
326 
330  void setupSparsity(const IntVec& elms, const SAM& sam);
331 
333  void setupBlockSparsity(const IntVec& elms, const SAM& sam);
334 
336  void setupGlb2Blk(const SAM& sam);
337 
339  Mat preAllocator(const int nrows, const int ncols = 0) const;
340 
342  std::vector<Mat> preAllocators() const;
343 
344  Mat pA;
347  KSP ksp;
348  MatNullSpace* nsp;
349  const ProcessAdm& adm;
351  bool setParams;
352  std::string forcedKSPType;
357  bool assembled;
358  bool factored;
359 
360  IS glob2LocEq = nullptr;
361  std::vector<Mat> matvec;
362 
363  std::vector<IS> isvec;
364 
365  std::vector<std::array<int,2>> glb2Blk;
366  std::unique_ptr<DomainDecomposition> m_dd{};
367 };
368 
369 
371 PETScVector operator*(const SystemMatrix& A, const PETScVector& b);
372 
375 
376 #endif
std::vector< int > IntVec
General integer vector.
Definition: ASMbase.h:25
std::vector< IntVec > IntMat
General 2D integer matrix.
Definition: ASMbase.h:26
Domain decomposition related partitioning for FE models.
#define Real
The floating point type to use.
Definition: ImmersedBoundaries.h:18
std::vector< Real > RealArray
A real-valued array without algebraic operations.
Definition: ImmersedBoundaries.h:29
std::vector< Vector > Vectors
An array of real-valued vectors with algebraic operations.
Definition: MatVec.h:37
std::vector< PetscIntVec > PetscIntMat
PETSc integer matrix.
Definition: PETScMatrix.h:27
std::vector< ISVec > ISMat
Index set matrix.
Definition: PETScMatrix.h:30
std::vector< PetscReal > PetscRealVec
PETSc real vector.
Definition: PETScMatrix.h:28
std::vector< PetscInt > PetscIntVec
PETSc integer vector.
Definition: PETScMatrix.h:26
std::vector< IS > ISVec
Index set vector.
Definition: PETScMatrix.h:29
PETScVector operator/(SystemMatrix &A, const PETScVector &b)
Solve linear system.
Definition: PETScMatrix.C:1061
PETScVector operator*(const SystemMatrix &A, const PETScVector &b)
Matrix-vector product.
Definition: PETScMatrix.C:1053
Linear solver parameters for PETSc matrices.
IFEM PETSc support.
int IS
To avoid compilation failures.
Definition: PETScSupport.h:51
int PetscInt
To avoid compilation failures.
Definition: PETScSupport.h:49
General representation of system matrices and vectors.
Class containing domain decomposition related partitioning.
Definition: DomainDecomposition.h:38
Class for linear solver parameters.
Definition: LinSolParams.h:67
Class for representing the system matrix in PETSc format.
Definition: PETScMatrix.h:172
bool setParams
If linear solver parameters are set.
Definition: PETScMatrix.h:351
bool assemble(const Matrix &eM, const SAM &sam, int e) override
Adds an element matrix into the associated system matrix.
Definition: PETScMatrix.C:625
IS glob2LocEq
Index set for global-to-local equations.
Definition: PETScMatrix.h:360
const std::vector< IS > & getIS() const
Get vector of index sets.
Definition: PETScMatrix.h:295
KSP ksp
Linear equation solver.
Definition: PETScMatrix.h:347
void init() override
Initializes the matrix to zero assuming it is properly dimensioned.
Definition: PETScMatrix.C:696
std::vector< std::array< int, 2 > > glb2Blk
Maps equations to block and block eq.
Definition: PETScMatrix.h:365
void mult(Real alpha) override
Multiplication with a scalar.
Definition: PETScMatrix.C:709
ISMat dirIndexSet
Direction ordering.
Definition: PETScMatrix.h:355
bool solve(SystemVector &B, Real *) override
Solves the linear system of equations for a given right-hand-side.
Definition: PETScMatrix.C:742
bool assembled
True if PETSc matrix has been assembled.
Definition: PETScMatrix.h:357
std::vector< Mat > matvec
Blocks for block matrices.
Definition: PETScMatrix.h:361
const DomainDecomposition & getDD() const
Returns a const-ref to domain decompositioning.
Definition: PETScMatrix.C:1047
void initAssembly(const SAM &sam, char) override
Initializes the element assembly process.
Definition: PETScMatrix.C:401
LinAlg::MatrixType getType() const override
Returns the matrix type.
Definition: PETScMatrix.h:180
bool factored
True if PETsc matrix has been factored.
Definition: PETScMatrix.h:358
void setupBlockSparsity(const IntVec &elms, const SAM &sam)
Setup sparsity pattern for block-matrices for a model.
Definition: PETScMatrix.C:559
PetscInt ISsize
Number of index sets/elements.
Definition: PETScMatrix.h:353
void setupGlb2Blk(const SAM &sam)
Calculates blocks for global eqs.
Definition: PETScMatrix.C:600
const Mat & getMatrix() const
Returns the PETSc matrix (for read access).
Definition: PETScMatrix.h:289
PETScMatrix(const PETScMatrix &A)=delete
Disabled copy constructor.
void preAssemble(const std::vector< IntVec > &MMNPC, size_t nel) override
Initializes the equation sparsity pattern based on element connections.
Definition: PETScMatrix.C:352
PetscInt nrow
Number of matrix rows.
Definition: PETScMatrix.h:345
Mat pA
The actual PETSc matrix.
Definition: PETScMatrix.h:344
const ProcessAdm & adm
Process administrator.
Definition: PETScMatrix.h:349
bool endAssembly() override
Finalizes the system matrix assembly.
Definition: PETScMatrix.C:686
PETScMatrix(const ProcessAdm &padm, const LinSolParams &spar)
Constructor.
Definition: PETScMatrix.C:267
void setupSparsity(const IntVec &elms, const SAM &sam)
Setup sparsity pattern for model.
Definition: PETScMatrix.C:534
bool multiply(const SystemVector &B, SystemVector &C) const override
Performs the matrix-vector multiplication C = *this * B.
Definition: PETScMatrix.C:729
bool solveMultipleRhs(PETScVectors &B, Matrix &sField)
Solve for multiple right-hand-side vectors.
Definition: PETScMatrix.C:830
Mat preAllocator(const int nrows, const int ncols=0) const
Sets up preallocator matrix.
Definition: PETScMatrix.C:503
std::vector< IS > isvec
Index sets for blocks.
Definition: PETScMatrix.h:363
Real Linfnorm() const override
Returns the L-infinity norm of the matrix.
Definition: PETScMatrix.C:963
SystemMatrix * copy() const override
Creates a copy of the system matrix and returns a pointer to it.
Definition: PETScMatrix.C:328
bool add(Real sigma, int ieq) override
Adds the constant σ to the diagonal of this matrix.
Definition: PETScMatrix.C:715
const std::vector< Mat > & getBlockMatrices() const
Get vector of block matrices. Used for tests only.
Definition: PETScMatrix.h:292
std::unique_ptr< DomainDecomposition > m_dd
Internal partitioning information.
Definition: PETScMatrix.h:366
std::string forcedKSPType
Force a KSP type ignoring the parameters.
Definition: PETScMatrix.h:352
PETScSolParams solParams
Linear solver parameters.
Definition: PETScMatrix.h:350
size_t dim(int idim=1) const override
Returns the dimension of the system matrix.
Definition: PETScMatrix.C:341
bool setParameters(bool setup)
Set the linear solver parameters (solver type, preconditioner, tolerances).
Definition: PETScMatrix.C:971
int nLinSolves
Number of linear solves.
Definition: PETScMatrix.h:356
std::vector< Mat > preAllocators() const
Sets up preallocator matrices for blocks.
Definition: PETScMatrix.C:517
~PETScMatrix() override
The destructor frees the dynamically allocated arrays.
Definition: PETScMatrix.C:310
PetscRealVec coords
Coordinates of local nodes (x0,y0,z0,x1,y1,...)
Definition: PETScMatrix.h:354
MatNullSpace * nsp
Null-space of linear operator.
Definition: PETScMatrix.h:348
bool solveEig(PETScMatrix &B, RealArray &eigVal, Matrix &eigVec, int nev, Real shift=Real(0), int iop=1)
Solves a generalized symmetric-definite eigenproblem.
Definition: PETScMatrix.C:876
Mat & getMatrix()
Returns the PETSc matrix (for assignment).
Definition: PETScMatrix.h:287
const ProcessAdm & getAdm() const
Returns a const-ref to process administrator.
Definition: PETScMatrix.h:303
PetscInt ncol
Number of matrix columns.
Definition: PETScMatrix.h:346
Class for PETSc solver parameters.
Definition: PETScSolParams.h:47
Class for representing the system vector in PETSc format.
Definition: PETScMatrix.h:40
const ProcessAdm & getAdm() const
Return associated process administrator.
Definition: PETScMatrix.h:83
Real L2norm() const override
L2-norm of vector.
Definition: PETScMatrix.C:218
~PETScVector() override
Destructor.
Definition: PETScMatrix.C:173
PETScVector(const ProcessAdm &padm)
Constructor creating an empty vector.
Definition: PETScMatrix.C:130
const Vec & getVector() const
Returns the PETSc vector (for read access).
Definition: PETScMatrix.h:80
SystemVector * copy() const override
Creates a copy of the system vector and returns a pointer to it.
Definition: PETScMatrix.h:57
void init(Real value) override
Initializes the vector to a given scalar value.
Definition: PETScMatrix.C:180
Real Linfnorm() const override
Linfinity-norm of vector.
Definition: PETScMatrix.C:227
const ProcessAdm & adm
Process administrator.
Definition: PETScMatrix.h:87
Vec x
The actual PETSc vector.
Definition: PETScMatrix.h:86
Real L1norm() const override
L1-norm of vector.
Definition: PETScMatrix.C:209
void redim(size_t n) override
Sets the dimension of the system vector.
Definition: PETScMatrix.C:187
Vec & getVector()
Returns the PETSc vector (for assignment).
Definition: PETScMatrix.h:78
LinAlg::MatrixType getType() const override
Returns the vector type.
Definition: PETScMatrix.h:54
bool endAssembly() override
Finalizes the system vector assembly.
Definition: PETScMatrix.C:197
Class for representing a set of system vectors in PETSc format.
Definition: PETScMatrix.h:99
Vec get(size_t idx)
Returns a particular vector.
Definition: PETScMatrix.h:153
LinAlg::MatrixType getType() const override
Returns the vector type.
Definition: PETScMatrix.h:110
Real L2norm() const override
L2-norm of the vector.
Definition: PETScMatrix.h:141
~PETScVectors()
Destructor frees up the dynamically allocated vectors.
Definition: PETScMatrix.C:248
SystemVector * copy() const override
Creates a copy of the system vector and returns a pointer to it.
Definition: PETScMatrix.h:113
size_t size() const
Returns number of vectors.
Definition: PETScMatrix.h:156
const Vector & vec() const override
Reference to underlying utl::vector.
Definition: PETScMatrix.h:126
size_t dim() const override
Returns the dimension of the system vector.
Definition: PETScMatrix.h:116
void assemble(const Vectors &vecs, const IntVec &meqn, int=0) override
Adds element vectors into the system vector.
Definition: PETScMatrix.C:255
void mult(Real) override
Multiplication with a scalar.
Definition: PETScMatrix.h:132
void redim(size_t) override
Sets the dimension of the system vector.
Definition: PETScMatrix.h:119
Real L1norm() const override
L1-norm of the vector.
Definition: PETScMatrix.h:138
void add(const SystemVector &, Real) override
Addition of another system vector to this one.
Definition: PETScMatrix.h:135
void init(Real) override
Initializes the vector to a given scalar value.
Definition: PETScMatrix.h:129
const PETScMatrix & myA
Reference to matrix vectors are associated with.
Definition: PETScMatrix.h:161
const Real * getRef() const override
Reference through pointer.
Definition: PETScMatrix.h:124
Real * getPtr() override
Access through pointer.
Definition: PETScMatrix.h:122
Real Linfnorm() const override
Linfinity-norm of the vector.
Definition: PETScMatrix.h:144
PETScVectors(const PETScMatrix &A, int nvec)
Constructor creating a set of system vectors.
Definition: PETScMatrix.C:236
std::vector< Vec > vectors
Array of vectors.
Definition: PETScMatrix.h:160
size_t myDim
Global dimension of vectors.
Definition: PETScMatrix.h:159
Class for administration of MPI processes in IFEM library.
Definition: ProcessAdm.h:33
This class contains data and functions for the assembly of FE matrices.
Definition: SAM.h:39
Standard system vector stored as a single continuous array.
Definition: SystemMatrix.h:124
virtual const Vector & vec() const
Reference to underlying utl::vector.
Definition: SystemMatrix.h:171
Base class for representing a system matrix on different formats.
Definition: SystemMatrix.h:220
Base class for representing a system vector on different formats.
Definition: SystemMatrix.h:32
bool empty() const
Checks if the vector is empty.
Definition: SystemMatrix.h:61
A vector class with some added algebraic operations.
Definition: matrix.h:64
MatrixType
The available system matrix formats and associated solvers.
Definition: LinAlgenums.h:22
@ PETSC
Sparse matrices / PETSc solver.
Definition: LinAlgenums.h:27