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 */
 17:   PetscInt       i,n = 10,col[3];
 18:   PetscMPIInt    size;
 19:   PetscScalar    value[3];

 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,PETSC_ERR_WRONG_MPI_SIZE,"This is a uniprocessor example only!");

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

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

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

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

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

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

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

 76:   PetscFinalize();
 77:   return ierr;
 78: }

 80: /*TEST

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

 87: TEST*/