Actual source code: ex24.c

  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


  9: int main(int argc,char **args)
 10: {
 11:   Mat            C;
 12:   PetscScalar    v,none = -1.0;
 13:   PetscInt       i,j,Ii,J,Istart,Iend,N,m = 4,n = 4,its,k;
 15:   PetscMPIInt    size,rank;
 16:   PetscReal      err_norm,res_norm;
 17:   Vec            x,b,u,u_tmp;
 18:   PetscRandom    r;
 19:   PC             pc;
 20:   KSP            ksp;

 22:   PetscInitialize(&argc,&args,(char *)0,help);
 23:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 24:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 25:   PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);
 26:   PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
 27:   N = m*n;


 30:   /* Generate matrix */
 31:   MatCreate(PETSC_COMM_WORLD,&C);
 32:   MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,N,N);
 33:   MatSetFromOptions(C);
 34:   MatGetOwnershipRange(C,&Istart,&Iend);
 35:   for (Ii=Istart; Ii<Iend; Ii++) {
 36:     v = -1.0; i = Ii/n; j = Ii - i*n;
 37:     if (i>0)   {J = Ii - n; MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES);}
 38:     if (i<m-1) {J = Ii + n; MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES);}
 39:     if (j>0)   {J = Ii - 1; MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES);}
 40:     if (j<n-1) {J = Ii + 1; MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES);}
 41:     v = 4.0; MatSetValues(C,1,&Ii,1,&Ii,&v,ADD_VALUES);
 42:   }
 43:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
 44:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

 46:   /* a shift can make C indefinite. Preconditioners LU, ILU (for BAIJ format) and ICC may fail */
 47:   /* MatShift(C,alpha); */
 48:   /* MatView(C,PETSC_VIEWER_STDOUT_WORLD); */

 50:   /* Setup and solve for system */
 51:   /* Create vectors.  */
 52:   VecCreate(PETSC_COMM_WORLD,&x);
 53:   VecSetSizes(x,PETSC_DECIDE,N);
 54:   VecSetFromOptions(x);
 55:   VecDuplicate(x,&b);
 56:   VecDuplicate(x,&u);
 57:   VecDuplicate(x,&u_tmp);
 58:   /* Set exact solution u; then compute right-hand-side vector b. */
 59:   PetscRandomCreate(PETSC_COMM_SELF,&r);
 60:   PetscRandomSetFromOptions(r);
 61:   VecSetRandom(u,r);
 62:   PetscRandomDestroy(r);
 63:   MatMult(C,u,b);

 65:   for (k=0; k<3; k++){
 66:     if (k == 0){                              /* CG  */
 67:       KSPCreate(PETSC_COMM_WORLD,&ksp);
 68:       KSPSetOperators(ksp,C,C,DIFFERENT_NONZERO_PATTERN);
 69:       PetscPrintf(PETSC_COMM_WORLD,"\n CG: \n");
 70:       KSPSetType(ksp,KSPCG);
 71:     } else if (k == 1){                       /* MINRES */
 72:       KSPCreate(PETSC_COMM_WORLD,&ksp);
 73:       KSPSetOperators(ksp,C,C,DIFFERENT_NONZERO_PATTERN);
 74:       PetscPrintf(PETSC_COMM_WORLD,"\n MINRES: \n");
 75:       KSPSetType(ksp,KSPMINRES);
 76:     } else {                                 /* SYMMLQ */
 77:       KSPCreate(PETSC_COMM_WORLD,&ksp);
 78:       KSPSetOperators(ksp,C,C,DIFFERENT_NONZERO_PATTERN);
 79:       PetscPrintf(PETSC_COMM_WORLD,"\n SYMMLQ: \n");
 80:       KSPSetType(ksp,KSPSYMMLQ);
 81:     }
 82:     KSPGetPC(ksp,&pc);
 83:     /* PCSetType(pc,PCICC); */
 84:     PCSetType(pc,PCJACOBI);
 85:     KSPSetTolerances(ksp,1.e-7,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);

 87:     /*
 88:     Set runtime options, e.g.,
 89:         -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
 90:     These options will override those specified above as long as
 91:     KSPSetFromOptions() is called _after_ any other customization
 92:     routines.
 93:     */
 94:     KSPSetFromOptions(ksp);

 96:     /* Solve linear system; */
 97:     KSPSetUp(ksp);
 98:     KSPSolve(ksp,b,x);

100:     KSPGetIterationNumber(ksp,&its);
101:   /* Check error */
102:     VecCopy(u,u_tmp);
103:     VecAXPY(u_tmp,none,x);
104:     VecNorm(u_tmp,NORM_2,&err_norm);
105:     MatMult(C,x,u_tmp);
106:     VecAXPY(u_tmp,none,b);
107:     VecNorm(u_tmp,NORM_2,&res_norm);
108: 
109:     PetscPrintf(PETSC_COMM_WORLD,"Number of iterations = %3D\n",its);
110:     PetscPrintf(PETSC_COMM_WORLD,"Residual norm %A;",res_norm);
111:     PetscPrintf(PETSC_COMM_WORLD,"  Error norm %A.\n",err_norm);

113:     KSPDestroy(ksp);
114:   }
115: 
116:   /* 
117:        Free work space.  All PETSc objects should be destroyed when they
118:        are no longer needed.
119:   */
120:   VecDestroy(b);
121:   VecDestroy(u);
122:   VecDestroy(x);
123:   VecDestroy(u_tmp);
124:   MatDestroy(C);

126:   PetscFinalize();
127:   return 0;
128: }