Actual source code: ex24.c

petsc-3.4.2 2013-07-02
  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,err_tol=1.e-7,res_tol=1.e-6;
 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(NULL,"-m",&m,NULL);
 26:   PetscOptionsGetInt(NULL,"-n",&n,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:   MatSetUp(C);
 35:   MatGetOwnershipRange(C,&Istart,&Iend);
 36:   for (Ii=Istart; Ii<Iend; Ii++) {
 37:     v = -1.0; i = Ii/n; j = Ii - i*n;
 38:     if (i>0)   {J = Ii - n; MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES);}
 39:     if (i<m-1) {J = Ii + n; MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES);}
 40:     if (j>0)   {J = Ii - 1; MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES);}
 41:     if (j<n-1) {J = Ii + 1; MatSetValues(C,1,&Ii,1,&J,&v,ADD_VALUES);}
 42:     v = 4.0; MatSetValues(C,1,&Ii,1,&Ii,&v,ADD_VALUES);
 43:   }
 44:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
 45:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

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

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

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

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

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

101:     KSPGetIterationNumber(ksp,&its);
102:     /* Check error */
103:     VecCopy(u,u_tmp);
104:     VecAXPY(u_tmp,none,x);
105:     VecNorm(u_tmp,NORM_2,&err_norm);
106:     MatMult(C,x,u_tmp);
107:     VecAXPY(u_tmp,none,b);
108:     VecNorm(u_tmp,NORM_2,&res_norm);

110:     PetscPrintf(PETSC_COMM_WORLD,"Number of iterations = %3D\n",its);
111:     if (res_norm > res_tol) {
112:       PetscPrintf(PETSC_COMM_WORLD,"Residual norm %G;",res_norm);
113:     }
114:     if (err_norm > err_tol) {
115:       PetscPrintf(PETSC_COMM_WORLD,"  Error norm %G.\n",err_norm);
116:     }
117:     KSPDestroy(&ksp);
118:   }

120:   /*
121:        Free work space.  All PETSc objects should be destroyed when they
122:        are no longer needed.
123:   */
124:   VecDestroy(&b);
125:   VecDestroy(&u);
126:   VecDestroy(&x);
127:   VecDestroy(&u_tmp);
128:   MatDestroy(&C);

130:   PetscFinalize();
131:   return 0;
132: }