Actual source code: ex51.c

petsc-3.14.4 2021-02-03
Report Typos and Errors
  1: static char help[] = "Test PCFailedReason.\n\n";

  3: #include <petscksp.h>

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

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

 24:   sx = PetscSinReal(n*PETSC_PI/2/(n+1));
 25:   alpha = 4.0*sx*sx;   /* alpha is the largest eigenvalue of the matrix */
 26:   beta = 4.0;

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

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

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

 58:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 59:                       Factorize first matrix
 60:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 61:   PetscPrintf(PETSC_COMM_WORLD,"First matrix\n");
 62:   KSPSetUp(ksp);
 63:   KSPGetConvergedReason(ksp,&reason);
 64:   if (reason < 0) {
 65:     PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ksp)),PETSC_VIEWER_DEFAULT);
 66:     KSPConvergedReasonView(ksp,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ksp)));
 67:     PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ksp)));
 68:   } else {
 69:     PetscPrintf(PETSC_COMM_WORLD,"Success!\n");
 70:   }

 72:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 73:                       Factorize second matrix
 74:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 75:   MatShift(A,reverse?alpha-beta:beta-alpha);
 76:   KSPSetOperators(ksp,A,A);

 78:   PetscPrintf(PETSC_COMM_WORLD,"Second matrix\n");
 79:   KSPSetUp(ksp);
 80:   KSPGetConvergedReason(ksp,&reason);
 81:   if (reason < 0) {
 82:     PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ksp)),PETSC_VIEWER_DEFAULT);
 83:     KSPConvergedReasonView(ksp,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ksp)));
 84:     PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ksp)));
 85:   } else {
 86:     PetscPrintf(PETSC_COMM_WORLD,"Success!\n");
 87:     PCGetFailedReason(pc,&pcreason);
 88:     PetscPrintf(PETSC_COMM_WORLD,"PC failed reason is %s\n",PCFailedReasons[pcreason]);
 89:   }

 91:   /*
 92:      Free work space.
 93:   */
 94:   MatDestroy(&A);
 95:   KSPDestroy(&ksp);

 97:   PetscFinalize();
 98:   return ierr;
 99: }


102: /*TEST

104:    test:
105:       args: -reverse

107:    test:
108:       suffix: 2
109:       args: -reverse -pc_type cholesky

111: TEST*/