Actual source code: ex70.c


  2: static char help[] = "Solves an ill-conditioned tridiagonal linear system with KSP for testing GMRES breakdown tolerance.\n\n";

  4: #include <petscksp.h>

  6: int main(int argc,char **args)
  7: {
  8:   Vec            x, b, u;      /* approx solution, RHS, exact solution */
  9:   Mat            A;            /* linear system matrix */
 10:   KSP            ksp;          /* linear solver context */
 11:   PetscInt       i,n = 10,col[3];
 12:   PetscMPIInt    size;
 13:   PetscScalar    value[3];

 15:   PetscInitialize(&argc,&args,(char*)0,help);
 16:   MPI_Comm_size(PETSC_COMM_WORLD,&size);

 19:   /*
 20:      Create vectors.  Note that we form 1 vector from scratch and
 21:      then duplicate as needed.
 22:   */
 23:   VecCreate(PETSC_COMM_WORLD,&x);
 24:   PetscObjectSetName((PetscObject) x,"Solution");
 25:   VecSetSizes(x,PETSC_DECIDE,n);
 26:   VecSetFromOptions(x);
 27:   VecDuplicate(x,&b);
 28:   VecDuplicate(x,&u);

 30:   MatCreate(PETSC_COMM_WORLD,&A);
 31:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n);
 32:   MatSetFromOptions(A);
 33:   MatSetUp(A);

 35:   /*
 36:      Set big off-diag values to make the system ill-conditioned
 37:   */
 38:   value[0] = 10.0; value[1] = 2.0; value[2] = 1.0;
 39:   for (i=1; i<n-1; i++) {
 40:     col[0] = i-1; col[1] = i; col[2] = i+1;
 41:     MatSetValues(A,1,&i,3,col,value,INSERT_VALUES);
 42:   }
 43:   i    = n - 1; col[0] = n - 2; col[1] = n - 1;
 44:   MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);
 45:   i    = 0; col[0] = 0; col[1] = 1; value[0] = 2.0; value[1] = -1.0;
 46:   MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);
 47:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 48:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

 50:   VecSet(u,1.0);
 51:   MatMult(A,u,b);

 53:   KSPCreate(PETSC_COMM_WORLD,&ksp);
 54:   KSPSetOperators(ksp,A,A);
 55:   KSPSetFromOptions(ksp);
 56:   KSPSolve(ksp,b,x);

 58:   KSPSetInitialGuessNonzero(ksp,PETSC_TRUE);
 59:   PetscOptionsInsertString(NULL,"-ksp_type preonly -ksp_initial_guess_nonzero false");
 60:   PetscOptionsClearValue(NULL,"-ksp_converged_reason");
 61:   KSPSetFromOptions(ksp);
 62:   KSPSolve(ksp,b,x);

 64:   VecDestroy(&x);
 65:   VecDestroy(&u);
 66:   VecDestroy(&b);
 67:   MatDestroy(&A);
 68:   KSPDestroy(&ksp);

 70:   PetscFinalize();
 71:   return 0;
 72: }

 74: /*TEST

 76:    test:
 77:       requires: double !complex
 78:       args: -ksp_rtol  1e-18 -pc_type sor -ksp_converged_reason -ksp_gmres_breakdown_tolerance 1.e-9
 79:       output_file: output/ex70.out

 81: TEST*/