Actual source code: ex24.c
petsc-3.12.1 2019-10-22
2: static char help[] = "Tests CG, MINRES and SYMMLQ on symmetric matrices with SBAIJ format. The preconditioner ICC only works on sequential SBAIJ format. \n\n";
4: #include <petscksp.h>
7: int main(int argc,char **args)
8: {
9: Mat C;
10: PetscScalar v,none = -1.0;
11: PetscInt i,j,Ii,J,Istart,Iend,N,m = 4,n = 4,its,k;
13: PetscMPIInt size,rank;
14: PetscReal err_norm,res_norm;
15: Vec x,b,u,u_tmp;
16: PC pc;
17: KSP ksp;
19: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
20: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
21: MPI_Comm_size(PETSC_COMM_WORLD,&size);
22: PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
23: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
24: N = m*n;
26: /* Generate matrix */
27: MatCreate(PETSC_COMM_WORLD,&C);
28: MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,N,N);
29: MatSetFromOptions(C);
30: MatSetUp(C);
31: MatGetOwnershipRange(C,&Istart,&Iend);
32: for (Ii=Istart; Ii<Iend; Ii++) {
33: v = -1.0; i = Ii/n; j = Ii - i*n;
34: if (i>0) {J = Ii - n; MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES);}
35: if (i<m-1) {J = Ii + n; MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES);}
36: if (j>0) {J = Ii - 1; MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES);}
37: if (j<n-1) {J = Ii + 1; MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES);}
38: v = 4.0; MatSetValues(C,1,&Ii,1,&Ii,&v,ADD_VALUES);
39: }
40: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
41: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
43: /* a shift can make C indefinite. Preconditioners LU, ILU (for BAIJ format) and ICC may fail */
44: /* MatShift(C,alpha); */
45: /* MatView(C,PETSC_VIEWER_STDOUT_WORLD); */
47: /* Setup and solve for system */
48: /* Create vectors. */
49: VecCreate(PETSC_COMM_WORLD,&x);
50: VecSetSizes(x,PETSC_DECIDE,N);
51: VecSetFromOptions(x);
52: VecDuplicate(x,&b);
53: VecDuplicate(x,&u);
54: VecDuplicate(x,&u_tmp);
55: /* Set exact solution u; then compute right-hand-side vector b. */
56: VecSet(u,1.0);
57: MatMult(C,u,b);
59: for (k=0; k<3; k++) {
60: if (k == 0) { /* CG */
61: KSPCreate(PETSC_COMM_WORLD,&ksp);
62: KSPSetOperators(ksp,C,C);
63: PetscPrintf(PETSC_COMM_WORLD,"\n CG: \n");
64: KSPSetType(ksp,KSPCG);
65: } else if (k == 1) { /* MINRES */
66: KSPCreate(PETSC_COMM_WORLD,&ksp);
67: KSPSetOperators(ksp,C,C);
68: PetscPrintf(PETSC_COMM_WORLD,"\n MINRES: \n");
69: KSPSetType(ksp,KSPMINRES);
70: } else { /* SYMMLQ */
71: KSPCreate(PETSC_COMM_WORLD,&ksp);
72: KSPSetOperators(ksp,C,C);
73: PetscPrintf(PETSC_COMM_WORLD,"\n SYMMLQ: \n");
74: KSPSetType(ksp,KSPSYMMLQ);
75: }
76: KSPGetPC(ksp,&pc);
77: /* PCSetType(pc,PCICC); */
78: PCSetType(pc,PCJACOBI);
79: KSPSetTolerances(ksp,1.e-7,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);
81: /*
82: Set runtime options, e.g.,
83: -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
84: These options will override those specified above as long as
85: KSPSetFromOptions() is called _after_ any other customization
86: routines.
87: */
88: KSPSetFromOptions(ksp);
90: /* Solve linear system; */
91: KSPSetUp(ksp);
92: KSPSolve(ksp,b,x);
94: KSPGetIterationNumber(ksp,&its);
95: /* Check error */
96: VecCopy(u,u_tmp);
97: VecAXPY(u_tmp,none,x);
98: VecNorm(u_tmp,NORM_2,&err_norm);
99: MatMult(C,x,u_tmp);
100: VecAXPY(u_tmp,none,b);
101: VecNorm(u_tmp,NORM_2,&res_norm);
103: PetscPrintf(PETSC_COMM_WORLD,"Number of iterations = %3D\n",its);
104: PetscPrintf(PETSC_COMM_WORLD,"Residual norm %g;",(double)res_norm);
105: PetscPrintf(PETSC_COMM_WORLD," Error norm %g.\n",(double)err_norm);
106: KSPDestroy(&ksp);
107: }
109: /*
110: Free work space. All PETSc objects should be destroyed when they
111: are no longer needed.
112: */
113: VecDestroy(&b);
114: VecDestroy(&u);
115: VecDestroy(&x);
116: VecDestroy(&u_tmp);
117: MatDestroy(&C);
119: PetscFinalize();
120: return ierr;
121: }
124: /*TEST
126: test:
127: args: -pc_type icc -mat_type seqsbaij -mat_ignore_lower_triangular
129: test:
130: suffix: 2
131: args: -pc_type icc -pc_factor_levels 2 -mat_type seqsbaij -mat_ignore_lower_triangular
133: test:
134: suffix: 3
135: nsize: 2
136: args: -pc_type bjacobi -sub_pc_type icc -mat_type mpisbaij -mat_ignore_lower_triangular -ksp_max_it 8
138: test:
139: suffix: 4
140: nsize: 2
141: args: -pc_type bjacobi -sub_pc_type icc -sub_pc_factor_levels 1 -mat_type mpisbaij -mat_ignore_lower_triangular
143: TEST*/