Actual source code: ex2.c
1: static char help[] = "Tests PC and KSP on a tridiagonal matrix. Note that most\n\
2: users should employ the KSP interface instead of using PC directly.\n\n";
4: #include <petscksp.h>
6: int main(int argc, char **args)
7: {
8: Mat mat; /* matrix */
9: Vec b, ustar, u; /* vectors (RHS, exact solution, approx solution) */
10: PC pc; /* PC context */
11: KSP ksp; /* KSP context */
12: PetscInt n = 10, i, its, col[3];
13: PetscScalar value[3];
14: PCType pcname;
15: KSPType kspname;
16: PetscReal norm, tol = 1000. * PETSC_MACHINE_EPSILON;
18: PetscFunctionBeginUser;
19: PetscCall(PetscInitialize(&argc, &args, NULL, help));
20: /* Create and initialize vectors */
21: PetscCall(VecCreateSeq(PETSC_COMM_SELF, n, &b));
22: PetscCall(VecCreateSeq(PETSC_COMM_SELF, n, &ustar));
23: PetscCall(VecCreateSeq(PETSC_COMM_SELF, n, &u));
24: PetscCall(VecSet(ustar, 1.0));
26: /* Create and assemble matrix */
27: PetscCall(MatCreateSeqAIJ(PETSC_COMM_SELF, n, n, 3, NULL, &mat));
28: value[0] = -1.0;
29: value[1] = 2.0;
30: value[2] = -1.0;
31: for (i = 1; i < n - 1; i++) {
32: col[0] = i - 1;
33: col[1] = i;
34: col[2] = i + 1;
35: PetscCall(MatSetValues(mat, 1, &i, 3, col, value, INSERT_VALUES));
36: }
37: i = n - 1;
38: col[0] = n - 2;
39: col[1] = n - 1;
40: PetscCall(MatSetValues(mat, 1, &i, 2, col, value, INSERT_VALUES));
41: i = 0;
42: col[0] = 0;
43: col[1] = 1;
44: value[0] = 2.0;
45: value[1] = -1.0;
46: PetscCall(MatSetValues(mat, 1, &i, 2, col, value, INSERT_VALUES));
47: PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY));
48: PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY));
50: /* Compute right-hand-side vector */
51: PetscCall(MatMult(mat, ustar, b));
53: /* Create PC context and set up data structures */
54: PetscCall(PCCreate(PETSC_COMM_WORLD, &pc));
55: PetscCall(PCSetType(pc, PCNONE));
56: PetscCall(PCSetFromOptions(pc));
57: PetscCall(PCSetOperators(pc, mat, mat));
58: PetscCall(PCSetUp(pc));
60: /* Create KSP context and set up data structures */
61: PetscCall(KSPCreate(PETSC_COMM_WORLD, &ksp));
62: PetscCall(KSPSetType(ksp, KSPRICHARDSON));
63: PetscCall(KSPSetFromOptions(ksp));
64: PetscCall(PCSetOperators(pc, mat, mat));
65: PetscCall(KSPSetPC(ksp, pc));
66: PetscCall(KSPSetUp(ksp));
68: /* Solve the problem */
69: PetscCall(KSPGetType(ksp, &kspname));
70: PetscCall(PCGetType(pc, &pcname));
71: PetscCall(PetscPrintf(PETSC_COMM_SELF, "Running %s with %s preconditioning\n", kspname, pcname));
72: PetscCall(KSPSolve(ksp, b, u));
73: PetscCall(VecAXPY(u, -1.0, ustar));
74: PetscCall(VecNorm(u, NORM_2, &norm));
75: PetscCall(KSPGetIterationNumber(ksp, &its));
76: if (norm > tol) PetscCall(PetscPrintf(PETSC_COMM_SELF, "2 norm of error %g Number of iterations %" PetscInt_FMT "\n", (double)norm, its));
78: /* Free data structures */
79: PetscCall(KSPDestroy(&ksp));
80: PetscCall(VecDestroy(&u));
81: PetscCall(VecDestroy(&ustar));
82: PetscCall(VecDestroy(&b));
83: PetscCall(MatDestroy(&mat));
84: PetscCall(PCDestroy(&pc));
86: PetscCall(PetscFinalize());
87: return 0;
88: }
90: /*TEST
92: test:
93: args: -ksp_type cg -ksp_monitor
95: TEST*/