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: /*T
  5:    Concepts: KSP^solving an ill-conditioned system of linear equations for testing GMRES breakdown tolerance
  6:    Processors: 1
  7: T*/

  9: #include <petscksp.h>

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

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

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

 35:   MatCreate(PETSC_COMM_WORLD,&A);
 36:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n);
 37:   MatSetFromOptions(A);
 38:   MatSetUp(A);

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

 55:   VecSet(u,1.0);
 56:   MatMult(A,u,b);

 58:   KSPCreate(PETSC_COMM_WORLD,&ksp);
 59:   KSPSetOperators(ksp,A,A);
 60:   KSPSetFromOptions(ksp);
 61:   KSPSolve(ksp,b,x);

 63:   KSPSetInitialGuessNonzero(ksp,PETSC_TRUE);
 64:   PetscOptionsInsertString(NULL,"-ksp_type preonly -ksp_initial_guess_nonzero false");
 65:   PetscOptionsClearValue(NULL,"-ksp_converged_reason");
 66:   KSPSetFromOptions(ksp);
 67:   KSPSolve(ksp,b,x);

 69:   VecDestroy(&x);
 70:   VecDestroy(&u);
 71:   VecDestroy(&b);
 72:   MatDestroy(&A);
 73:   KSPDestroy(&ksp);

 75:   PetscFinalize();
 76:   return 0;
 77: }

 79: /*TEST

 81:    test:
 82:       requires: double !complex
 83:       args: -ksp_rtol  1e-18 -pc_type sor -ksp_converged_reason -ksp_gmres_breakdown_tolerance 1.e-9
 84:       output_file: output/ex70.out

 86: TEST*/