Actual source code: ex7.c
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,I,J;
16: PetscScalar v,mone = -1.0,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,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n,&C);
26: MatSetFromOptions(C);
27: for (i=0; i<m; i++) {
28: for (j=0; j<n; j++) {
29: v = -1.0; I = j + n*i;
30: if (i>0) {J = I - n; MatSetValues(C,1,&I,1,&J,&v,INSERT_VALUES);}
31: if (i<m-1) {J = I + n; MatSetValues(C,1,&I,1,&J,&v,INSERT_VALUES);}
32: if (j>0) {J = I - 1; MatSetValues(C,1,&I,1,&J,&v,INSERT_VALUES);}
33: if (j<n-1) {J = I + 1; MatSetValues(C,1,&I,1,&J,&v,INSERT_VALUES);}
34: v = 4.0; MatSetValues(C,1,&I,1,&I,&v,INSERT_VALUES);
35: }
36: }
37: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
38: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
39: MatGetOrdering(C,MATORDERING_RCM,&perm,&iperm);
40: MatView(C,PETSC_VIEWER_STDOUT_WORLD);
41: ISView(perm,PETSC_VIEWER_STDOUT_SELF);
43: MatFactorInfoInitialize(&luinfo);
44: luinfo.fill = 2.0;
45: luinfo.dtcol = 0.0;
46: luinfo.damping = 0.0;
47: luinfo.zeropivot = 1.e-14;
48: luinfo.pivotinblocks = 1.0;
49: MatLUFactorSymbolic(C,perm,iperm,&luinfo,&LU);
50: MatLUFactorNumeric(C,&LU);
51: MatView(LU,PETSC_VIEWER_STDOUT_WORLD);
53: VecCreateSeq(PETSC_COMM_SELF,m*n,&u);
54: VecSet(&one,u);
55: VecDuplicate(u,&x);
56: VecDuplicate(u,&b);
58: MatMult(C,u,b);
59: MatSolve(LU,b,x);
61: VecView(b,PETSC_VIEWER_STDOUT_SELF);
62: VecView(x,PETSC_VIEWER_STDOUT_SELF);
64: VecAXPY(&mone,u,x);
65: VecNorm(x,NORM_2,&norm);
66: PetscPrintf(PETSC_COMM_SELF,"Norm of error %A\n",norm);
68: MatGetInfo(C,MAT_LOCAL,&info);
69: PetscPrintf(PETSC_COMM_SELF,"original matrix nonzeros = %D\n",(PetscInt)info.nz_used);
70: MatGetInfo(LU,MAT_LOCAL,&info);
71: PetscPrintf(PETSC_COMM_SELF,"factored matrix nonzeros = %D\n",(PetscInt)info.nz_used);
73: VecDestroy(u);
74: VecDestroy(b);
75: VecDestroy(x);
76: ISDestroy(perm);
77: ISDestroy(iperm);
78: MatDestroy(C);
79: MatDestroy(LU);
81: PetscFinalize();
82: return 0;
83: }