Actual source code: ex128.c
2: static char help[] = "Tests ILU and ICC factorization with and without matrix ordering on seqsbaij format. Modified from ex30.c\n\
3: Input parameters are:\n\
4: -lf <level> : level of fill for ILU (default is 0)\n\
5: -lu : use full LU or Cholesky factorization\n\
6: -m <value>,-n <value> : grid dimensions\n\
7: Note that most users should employ the KSP interface to the\n\
8: linear solvers instead of using the factorization routines\n\
9: directly.\n\n";
11: #include <petscmat.h>
15: int main(int argc,char **args)
16: {
17: Mat C,sC,sA;
18: PetscInt i,j,m = 5,n = 5,Ii,J,lf = 0;
20: PetscBool CHOLESKY=PETSC_FALSE,TRIANGULAR=PETSC_FALSE,flg;
21: PetscScalar v;
22: IS row,col;
23: MatFactorInfo info;
24: Vec x,y,b,ytmp;
25: PetscReal norm2;
26: PetscRandom rdm;
27: PetscMPIInt size;
29: PetscInitialize(&argc,&args,(char *)0,help);
30: MPI_Comm_size(PETSC_COMM_WORLD,&size);
31: if (size != 1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"This is a uniprocessor example only!");
32: PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);
33: PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
34: PetscOptionsGetInt(PETSC_NULL,"-lf",&lf,PETSC_NULL);
36: MatCreate(PETSC_COMM_SELF,&C);
37: MatSetSizes(C,m*n,m*n,m*n,m*n);
38: MatSetFromOptions(C);
40: /* Create matrix C in seqaij format and sC in seqsbaij. (This is five-point stencil with some extra elements) */
41: for (i=0; i<m; i++) {
42: for (j=0; j<n; j++) {
43: v = -1.0; Ii = j + n*i;
44: J = Ii - n; if (J>=0) {MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
45: J = Ii + n; if (J<m*n) {MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
46: J = Ii - 1; if (J>=0) {MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
47: J = Ii + 1; if (J<m*n) {MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
48: v = 4.0; MatSetValues(C,1,&Ii,1,&Ii,&v,INSERT_VALUES);
49: }
50: }
51: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
52: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
54: MatIsSymmetric(C,0.0,&flg);
55: if (!flg) SETERRQ(PETSC_COMM_SELF,1,"C is non-symmetric");
56: MatConvert(C,MATSEQSBAIJ,MAT_INITIAL_MATRIX,&sC);
58: /* Create vectors for error checking */
59: MatGetVecs(C,&x,&b);
60: VecDuplicate(x,&y);
61: VecDuplicate(x,&ytmp);
62: PetscRandomCreate(PETSC_COMM_SELF,&rdm);
63: PetscRandomSetFromOptions(rdm);
64: VecSetRandom(x,rdm);
65: MatMult(C,x,b);
67: MatGetOrdering(C,MATORDERINGNATURAL,&row,&col);
69: /* Compute CHOLESKY or ICC factor sA */
70: MatFactorInfoInitialize(&info);
71: info.fill = 1.0;
72: info.diagonal_fill = 0;
73: info.zeropivot = 0.0;
74: PetscOptionsHasName(PETSC_NULL,"-cholesky",&CHOLESKY);
75: if (CHOLESKY){
76: printf("Test CHOLESKY...\n");
77: MatGetFactor(sC,MATSOLVERPETSC,MAT_FACTOR_CHOLESKY,&sA);
78: MatCholeskyFactorSymbolic(sA,sC,row,&info);
79: } else {
80: printf("Test ICC...\n");
81: info.levels = lf;
82: MatGetFactor(sC,MATSOLVERPETSC,MAT_FACTOR_ICC,&sA);
83: MatICCFactorSymbolic(sA,sC,row,&info);
84: }
85: MatCholeskyFactorNumeric(sA,sC,&info);
87: /* test MatForwardSolve() and MatBackwardSolve() with matrix reordering on aij matrix C */
88: if (CHOLESKY){
89: PetscOptionsHasName(PETSC_NULL,"-triangular_solve",&TRIANGULAR);
90: if (TRIANGULAR){
91: printf("Test MatForwardSolve...\n");
92: MatForwardSolve(sA,b,ytmp);
93: printf("Test MatBackwardSolve...\n");
94: MatBackwardSolve(sA,ytmp,y);
95: VecAXPY(y,-1.0,x);
96: VecNorm(y,NORM_2,&norm2);
97: if (norm2 > 1.e-14){
98: PetscPrintf(PETSC_COMM_SELF,"MatForwardSolve and BackwardSolve: Norm of error=%G\n",norm2);
99: }
100: }
101: }
103: MatSolve(sA,b,y);
104: MatDestroy(&sC);
105: MatDestroy(&sA);
106: VecAXPY(y,-1.0,x);
107: VecNorm(y,NORM_2,&norm2);
108: if (lf == -1 && norm2 > 1.e-14){
109: PetscPrintf(PETSC_COMM_SELF, " reordered SEQAIJ: Cholesky/ICC levels %d, residual %g\n",lf,norm2);
110: }
111:
112: /* Free data structures */
113: MatDestroy(&C);
114: ISDestroy(&row);
115: ISDestroy(&col);
116: PetscRandomDestroy(&rdm);
117: VecDestroy(&x);
118: VecDestroy(&y);
119: VecDestroy(&ytmp);
120: VecDestroy(&b);
121: PetscFinalize();
122: return 0;
123: }