Actual source code: ex7.c
petsc-3.7.3 2016-08-01
2: static char help[] = "Tests matrix factorization. Note that most users should\n\
3: employ the KSP interface to the linear solvers instead of using the factorization\n\
4: routines directly.\n\n";
6: #include <petscmat.h>
10: int main(int argc,char **args)
11: {
12: Mat C,LU;
13: MatInfo info;
14: PetscInt i,j,m = 3,n = 3,Ii,J;
16: PetscScalar v,one = 1.0;
17: IS perm,iperm;
18: Vec x,u,b;
19: PetscReal norm;
20: MatFactorInfo luinfo;
22: PetscInitialize(&argc,&args,(char*)0,help);
24: /* Create the matrix for the five point stencil, YET AGAIN */
25: MatCreate(PETSC_COMM_SELF,&C);
26: MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);
27: MatSetFromOptions(C);
28: MatSetUp(C);
29: for (i=0; i<m; i++) {
30: for (j=0; j<n; j++) {
31: v = -1.0; Ii = j + n*i;
32: if (i>0) {J = Ii - n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
33: if (i<m-1) {J = Ii + n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
34: if (j>0) {J = Ii - 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
35: if (j<n-1) {J = Ii + 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
36: v = 4.0; MatSetValues(C,1,&Ii,1,&Ii,&v,INSERT_VALUES);
37: }
38: }
39: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
40: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
41: MatGetOrdering(C,MATORDERINGRCM,&perm,&iperm);
42: MatView(C,PETSC_VIEWER_STDOUT_WORLD);
43: ISView(perm,PETSC_VIEWER_STDOUT_SELF);
45: MatFactorInfoInitialize(&luinfo);
47: luinfo.fill = 2.0;
48: luinfo.dtcol = 0.0;
49: luinfo.zeropivot = 1.e-14;
50: luinfo.pivotinblocks = 1.0;
52: MatGetFactor(C,MATSOLVERPETSC,MAT_FACTOR_LU,&LU);
53: MatLUFactorSymbolic(LU,C,perm,iperm,&luinfo);
54: MatLUFactorNumeric(LU,C,&luinfo);
56: VecCreateSeq(PETSC_COMM_SELF,m*n,&u);
57: VecSet(u,one);
58: VecDuplicate(u,&x);
59: VecDuplicate(u,&b);
61: MatMult(C,u,b);
62: MatSolve(LU,b,x);
64: VecView(b,PETSC_VIEWER_STDOUT_SELF);
65: VecView(x,PETSC_VIEWER_STDOUT_SELF);
67: VecAXPY(x,-1.0,u);
68: VecNorm(x,NORM_2,&norm);
69: PetscPrintf(PETSC_COMM_SELF,"Norm of error %g\n",(double)norm);
71: MatGetInfo(C,MAT_LOCAL,&info);
72: PetscPrintf(PETSC_COMM_SELF,"original matrix nonzeros = %D\n",(PetscInt)info.nz_used);
73: MatGetInfo(LU,MAT_LOCAL,&info);
74: PetscPrintf(PETSC_COMM_SELF,"factored matrix nonzeros = %D\n",(PetscInt)info.nz_used);
76: VecDestroy(&u);
77: VecDestroy(&b);
78: VecDestroy(&x);
79: ISDestroy(&perm);
80: ISDestroy(&iperm);
81: MatDestroy(&C);
82: MatDestroy(&LU);
84: PetscFinalize();
85: return 0;
86: }