Actual source code: ex51.c

petsc-3.7.5 2017-01-01
Report Typos and Errors
  2: static char help[] = "Test PCFailedReason.\n\n";

  4: #include <petscksp.h>

  8: int main(int argc,char **args)
  9: {
 10:   Mat                A;            /* linear system matrix */
 11:   KSP                ksp;          /* linear solver context */
 12:   PC                 pc;           /* preconditioner context */
 13:   PetscErrorCode     ierr;
 14:   PetscInt           i,n = 10,col[3];
 15:   PetscMPIInt        size;
 16:   PetscScalar        value[3],alpha,beta,sx;
 17:   PetscBool          reverse=PETSC_FALSE;
 18:   KSPConvergedReason reason;
 19:   PCFailedReason     pcreason;

 21:   PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
 22:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 23:   if (size != 1) SETERRQ(PETSC_COMM_WORLD,1,"This is a uniprocessor example only!");
 24:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 25:   PetscOptionsGetBool(NULL,NULL,"-reverse",&reverse,NULL);

 27:   sx = PetscSinReal(n*PETSC_PI/2/(n+1));
 28:   alpha = 4.0*sx*sx;   /* alpha is the largest eigenvalue of the matrix */
 29:   beta = 4.0;

 31:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 32:          Create the matrix 
 33:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 34:   MatCreate(PETSC_COMM_WORLD,&A);
 35:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n);
 36:   MatSetFromOptions(A);
 37:   MatSetUp(A);

 39:   value[0] = -1.0; value[1] = 2.0; value[2] = -1.0;
 40:   for (i=1; i<n-1; i++) {
 41:     col[0] = i-1; col[1] = i; col[2] = i+1;
 42:     MatSetValues(A,1,&i,3,col,value,INSERT_VALUES);
 43:   }
 44:   i    = n - 1; col[0] = n - 2; col[1] = n - 1;
 45:   MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);
 46:   i    = 0; col[0] = 0; col[1] = 1; value[0] = 2.0; value[1] = -1.0;
 47:   MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);
 48:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 49:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

 51:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 52:                 Create the linear solver and set various options
 53:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 54:   KSPCreate(PETSC_COMM_WORLD,&ksp);
 55:   KSPSetOperators(ksp,A,A);
 56:   MatShift(A,reverse?-alpha:-beta);
 57:   KSPGetPC(ksp,&pc);
 58:   PCSetType(pc,PCLU);
 59:   KSPSetFromOptions(ksp);

 61:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 62:                       Factorize first matrix
 63:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 64:   PetscPrintf(PETSC_COMM_WORLD,"First matrix\n");
 65:   KSPSetUp(ksp);
 66:   KSPGetConvergedReason(ksp,&reason);
 67:   if (reason) {
 68:     PetscPrintf(PETSC_COMM_WORLD,"KSPSetUp() failed due to %s\n",KSPConvergedReasons[reason]);
 69:     PCGetSetUpFailedReason(pc,&pcreason);
 70:     PetscPrintf(PETSC_COMM_WORLD,"PC reason is %s\n",PCFailedReasons[pcreason]);
 71:   } else {
 72:     PetscPrintf(PETSC_COMM_WORLD,"Success!\n");
 73:   }

 75:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 76:                       Factorize second matrix
 77:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 78:   MatShift(A,reverse?alpha-beta:beta-alpha);
 79:   KSPSetOperators(ksp,A,A);

 81:   PetscPrintf(PETSC_COMM_WORLD,"Second matrix\n");
 82:   KSPSetUp(ksp);
 83:   KSPGetConvergedReason(ksp,&reason);
 84:   if (reason) {
 85:     PetscPrintf(PETSC_COMM_WORLD,"KSPSetUp() failed due to %s\n",KSPConvergedReasons[reason]);
 86:     PCGetSetUpFailedReason(pc,&pcreason);
 87:     PetscPrintf(PETSC_COMM_WORLD,"PC reason is %s\n",PCFailedReasons[pcreason]);
 88:   } else {
 89:     PetscPrintf(PETSC_COMM_WORLD,"Success!\n");
 90:     PCGetSetUpFailedReason(pc,&pcreason);
 91:     PetscPrintf(PETSC_COMM_WORLD,"PC reason is %s\n",PCFailedReasons[pcreason]);
 92:   }

 94:   /*
 95:      Free work space.
 96:   */
 97:   MatDestroy(&A);
 98:   KSPDestroy(&ksp);

100:   PetscFinalize();
101:   return ierr;
102: }