| 1 | /*!\file: MatInvert.cpp
|
|---|
| 2 | * \brief invert petsc matrix using LU factorization, and multiple right hand sides.
|
|---|
| 3 | */
|
|---|
| 4 |
|
|---|
| 5 | /*Petsc includes: */
|
|---|
| 6 | #include "petscmat.h"
|
|---|
| 7 | #include "petscvec.h"
|
|---|
| 8 | #include "petscksp.h"
|
|---|
| 9 |
|
|---|
| 10 | #include "../../../shared/shared.h"
|
|---|
| 11 | #include "../../../include/include.h"
|
|---|
| 12 |
|
|---|
| 13 | void MatInvert(Mat* pinv, Mat matrix){
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 | /*output: */
|
|---|
| 17 | Mat inv=NULL;
|
|---|
| 18 |
|
|---|
| 19 | /*intermediary: */
|
|---|
| 20 | int M,N;
|
|---|
| 21 | double sparsity=.001;
|
|---|
| 22 |
|
|---|
| 23 | Mat factored_matrix=NULL;
|
|---|
| 24 | Mat identity=NULL;
|
|---|
| 25 | Vec diagonal=NULL;
|
|---|
| 26 | KSP ksp=NULL;
|
|---|
| 27 | PC pc=NULL;
|
|---|
| 28 |
|
|---|
| 29 | /*Some checks: */
|
|---|
| 30 | MatGetSize(matrix,&M,&N);
|
|---|
| 31 | if(M!=N) _error2_("trying to invert a non square matrix!");
|
|---|
| 32 |
|
|---|
| 33 | /*Create identitiy matrix: */
|
|---|
| 34 | identity=NewMat(M,N,sparsity);
|
|---|
| 35 | diagonal=NewVec(M);
|
|---|
| 36 | VecSet(diagonal,1.0);
|
|---|
| 37 | MatDiagonalSet(identity,diagonal,INSERT_VALUES);
|
|---|
| 38 | MatAssemblyBegin(identity,MAT_FINAL_ASSEMBLY);
|
|---|
| 39 | MatAssemblyEnd(identity,MAT_FINAL_ASSEMBLY);
|
|---|
| 40 | MatConvert(identity, MATMPIDENSE,MAT_REUSE_MATRIX,&identity);
|
|---|
| 41 |
|
|---|
| 42 | /*Initialize inverse: */
|
|---|
| 43 | MatDuplicate(identity,MAT_DO_NOT_COPY_VALUES,&inv);
|
|---|
| 44 |
|
|---|
| 45 | /* Compute X=inv(A) by MatMatSolve(), from tests in petsc test27 */
|
|---|
| 46 | KSPCreate(PETSC_COMM_WORLD,&ksp);
|
|---|
| 47 | KSPSetOperators(ksp,matrix,matrix,DIFFERENT_NONZERO_PATTERN);
|
|---|
| 48 | KSPGetPC(ksp,&pc);
|
|---|
| 49 | PCSetType(pc,PCLU);
|
|---|
| 50 | KSPSetUp(ksp);
|
|---|
| 51 | //PCGetFactoredMatrix(pc,&factored_matrix); //not found any replacement yet
|
|---|
| 52 | MatMatSolve(factored_matrix,identity,inv);
|
|---|
| 53 |
|
|---|
| 54 | /*Assemble inverse: */
|
|---|
| 55 | MatAssemblyBegin(inv,MAT_FINAL_ASSEMBLY);
|
|---|
| 56 | MatAssemblyEnd(inv,MAT_FINAL_ASSEMBLY);
|
|---|
| 57 |
|
|---|
| 58 | MatConvert(inv, MATMPIAIJ,MAT_REUSE_MATRIX,&inv);
|
|---|
| 59 |
|
|---|
| 60 | /*Free ressources:*/
|
|---|
| 61 | MatFree(&identity);
|
|---|
| 62 | VecFree(&diagonal);
|
|---|
| 63 | #if _PETSC_MAJOR_ < 3 || (_PETSC_MAJOR_ == 3 && _PETSC_MINOR_ < 2)
|
|---|
| 64 | KSPDestroy(ksp);
|
|---|
| 65 | #else
|
|---|
| 66 | KSPDestroy(&ksp);
|
|---|
| 67 | #endif
|
|---|
| 68 |
|
|---|
| 69 | /*Assign output pointers:*/
|
|---|
| 70 | *pinv=inv;
|
|---|
| 71 |
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|