source: issm/branches/trunk-jpl-damage/src/c/toolkits/petsc/patches/MatInvert.cpp@ 12004

Last change on this file since 12004 was 12004, checked in by cborstad, 13 years ago

merged trunk-jpl into trunk-jpl-damage through revision 11990

File size: 1.8 KB
Line 
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
13void 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) _error_("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 #if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
59 MatConvert(inv, MATSEQAIJ,MAT_REUSE_MATRIX,&inv);
60 #else
61 MatConvert(inv, MATMPIAIJ,MAT_REUSE_MATRIX,&inv);
62 #endif
63
64 /*Free ressources:*/
65 MatFree(&identity);
66 VecFree(&diagonal);
67 #if _PETSC_MAJOR_ < 3 || (_PETSC_MAJOR_ == 3 && _PETSC_MINOR_ < 2)
68 KSPDestroy(ksp);
69 #else
70 KSPDestroy(&ksp);
71 #endif
72
73 /*Assign output pointers:*/
74 *pinv=inv;
75
76}
77
Note: See TracBrowser for help on using the repository browser.