Actual source code: ex2.c


  2: static char help[] = "Tests PC and KSP on a tridiagonal matrix.  Note that most\n\
  3: users should employ the KSP interface instead of using PC directly.\n\n";

  5: #include <petscksp.h>

  7: int main(int argc, char **args)
  8: {
  9:   Mat         mat;         /* matrix */
 10:   Vec         b, ustar, u; /* vectors (RHS, exact solution, approx solution) */
 11:   PC          pc;          /* PC context */
 12:   KSP         ksp;         /* KSP context */
 13:   PetscInt    n = 10, i, its, col[3];
 14:   PetscScalar value[3];
 15:   PCType      pcname;
 16:   KSPType     kspname;
 17:   PetscReal   norm, tol = 1000. * PETSC_MACHINE_EPSILON;

 20:   PetscInitialize(&argc, &args, (char *)0, help);
 21:   /* Create and initialize vectors */
 22:   VecCreateSeq(PETSC_COMM_SELF, n, &b);
 23:   VecCreateSeq(PETSC_COMM_SELF, n, &ustar);
 24:   VecCreateSeq(PETSC_COMM_SELF, n, &u);
 25:   VecSet(ustar, 1.0);
 26:   VecSet(u, 0.0);

 28:   /* Create and assemble matrix */
 29:   MatCreateSeqAIJ(PETSC_COMM_SELF, n, n, 3, NULL, &mat);
 30:   value[0] = -1.0;
 31:   value[1] = 2.0;
 32:   value[2] = -1.0;
 33:   for (i = 1; i < n - 1; i++) {
 34:     col[0] = i - 1;
 35:     col[1] = i;
 36:     col[2] = i + 1;
 37:     MatSetValues(mat, 1, &i, 3, col, value, INSERT_VALUES);
 38:   }
 39:   i      = n - 1;
 40:   col[0] = n - 2;
 41:   col[1] = n - 1;
 42:   MatSetValues(mat, 1, &i, 2, col, value, INSERT_VALUES);
 43:   i        = 0;
 44:   col[0]   = 0;
 45:   col[1]   = 1;
 46:   value[0] = 2.0;
 47:   value[1] = -1.0;
 48:   MatSetValues(mat, 1, &i, 2, col, value, INSERT_VALUES);
 49:   MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY);
 50:   MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY);

 52:   /* Compute right-hand-side vector */
 53:   MatMult(mat, ustar, b);

 55:   /* Create PC context and set up data structures */
 56:   PCCreate(PETSC_COMM_WORLD, &pc);
 57:   PCSetType(pc, PCNONE);
 58:   PCSetFromOptions(pc);
 59:   PCSetOperators(pc, mat, mat);
 60:   PCSetUp(pc);

 62:   /* Create KSP context and set up data structures */
 63:   KSPCreate(PETSC_COMM_WORLD, &ksp);
 64:   KSPSetType(ksp, KSPRICHARDSON);
 65:   KSPSetFromOptions(ksp);
 66:   PCSetOperators(pc, mat, mat);
 67:   KSPSetPC(ksp, pc);
 68:   KSPSetUp(ksp);

 70:   /* Solve the problem */
 71:   KSPGetType(ksp, &kspname);
 72:   PCGetType(pc, &pcname);
 73:   PetscPrintf(PETSC_COMM_SELF, "Running %s with %s preconditioning\n", kspname, pcname);
 74:   KSPSolve(ksp, b, u);
 75:   VecAXPY(u, -1.0, ustar);
 76:   VecNorm(u, NORM_2, &norm);
 77:   KSPGetIterationNumber(ksp, &its);
 78:   if (norm > tol) PetscPrintf(PETSC_COMM_SELF, "2 norm of error %g Number of iterations %" PetscInt_FMT "\n", (double)norm, its);

 80:   /* Free data structures */
 81:   KSPDestroy(&ksp);
 82:   VecDestroy(&u);
 83:   VecDestroy(&ustar);
 84:   VecDestroy(&b);
 85:   MatDestroy(&mat);
 86:   PCDestroy(&pc);

 88:   PetscFinalize();
 89:   return 0;
 90: }

 92: /*TEST

 94:    test:
 95:       args: -ksp_type cg -ksp_monitor_short

 97: TEST*/