Actual source code: ex3.c

  1: static char help[] = "Demonstrates the use of fast Richardson for SOR. And\n\
  2: also tests the MatSOR() routines.  Input parameters are:\n\
  3:  -n <n> : problem dimension\n\n";

  5: #include <petscksp.h>
  6: #include <petscpc.h>

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

 19:   PetscFunctionBeginUser;
 20:   PetscCall(PetscInitialize(&argc, &args, NULL, help));
 21:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));

 23:   /* Create and initialize vectors */
 24:   PetscCall(VecCreateSeq(PETSC_COMM_SELF, n, &b));
 25:   PetscCall(VecCreateSeq(PETSC_COMM_SELF, n, &ustar));
 26:   PetscCall(VecCreateSeq(PETSC_COMM_SELF, n, &u));
 27:   PetscCall(VecSet(ustar, 1.0));

 29:   /* Create and assemble matrix */
 30:   PetscCall(MatCreate(PETSC_COMM_SELF, &mat));
 31:   PetscCall(MatSetType(mat, MATSEQAIJ));
 32:   PetscCall(MatSetSizes(mat, n, n, n, n));
 33:   PetscCall(MatSetFromOptions(mat));
 34:   PetscCall(MatSeqAIJSetPreallocation(mat, 3, NULL));
 35:   PetscCall(MatSeqBAIJSetPreallocation(mat, 1, 3, NULL));
 36:   PetscCall(MatSeqSBAIJSetPreallocation(mat, 1, 3, NULL));
 37:   PetscCall(MatSeqSELLSetPreallocation(mat, 3, NULL));
 38:   value[0] = -1.0;
 39:   value[1] = 2.0;
 40:   value[2] = -1.0;
 41:   for (i = 1; i < n - 1; i++) {
 42:     col[0] = i - 1;
 43:     col[1] = i;
 44:     col[2] = i + 1;
 45:     PetscCall(MatSetValues(mat, 1, &i, 3, col, value, INSERT_VALUES));
 46:   }
 47:   i      = n - 1;
 48:   col[0] = n - 2;
 49:   col[1] = n - 1;
 50:   PetscCall(MatSetValues(mat, 1, &i, 2, col, value, INSERT_VALUES));
 51:   i        = 0;
 52:   col[0]   = 0;
 53:   col[1]   = 1;
 54:   value[0] = 2.0;
 55:   value[1] = -1.0;
 56:   PetscCall(MatSetValues(mat, 1, &i, 2, col, value, INSERT_VALUES));
 57:   PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY));
 58:   PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY));

 60:   /* Compute right-hand-side vector */
 61:   PetscCall(MatMult(mat, ustar, b));

 63:   /* Create PC context and set up data structures */
 64:   PetscCall(PCCreate(PETSC_COMM_WORLD, &pc));
 65:   PetscCall(PCSetType(pc, PCNONE));
 66:   PetscCall(PCSetFromOptions(pc));
 67:   PetscCall(PCSetOperators(pc, mat, mat));
 68:   PetscCall(PCSetUp(pc));

 70:   /* Create KSP context and set up data structures */
 71:   PetscCall(KSPCreate(PETSC_COMM_WORLD, &ksp));
 72:   PetscCall(KSPSetType(ksp, KSPRICHARDSON));
 73:   PetscCall(KSPSetFromOptions(ksp));
 74:   PetscCall(PCSetOperators(pc, mat, mat));
 75:   PetscCall(KSPSetPC(ksp, pc));
 76:   PetscCall(KSPSetUp(ksp));

 78:   /* Solve the problem */
 79:   PetscCall(KSPGetType(ksp, &kspname));
 80:   PetscCall(PCGetType(pc, &pcname));
 81:   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Running %s with %s preconditioning\n", kspname, pcname));
 82:   PetscCall(KSPSolve(ksp, b, u));
 83:   PetscCall(KSPGetIterationNumber(ksp, &its));
 84:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Number of iterations %" PetscInt_FMT "\n", its));

 86:   /* Free data structures */
 87:   PetscCall(KSPDestroy(&ksp));
 88:   PetscCall(VecDestroy(&u));
 89:   PetscCall(VecDestroy(&ustar));
 90:   PetscCall(VecDestroy(&b));
 91:   PetscCall(MatDestroy(&mat));
 92:   PetscCall(PCDestroy(&pc));
 93:   PetscCall(PetscFinalize());
 94:   return 0;
 95: }

 97: /*TEST

 99:    testset:
100:      args: -ksp_type gmres -ksp_monitor -pc_type sor -pc_sor_symmetric
101:      output_file: output/ex3_1.out
102:      test:
103:        suffix: sor_aij
104:      test:
105:        suffix: sor_seqbaij
106:        args: -mat_type seqbaij
107:      test:
108:        suffix: sor_seqsbaij
109:        args: -mat_type seqsbaij
110:      test:
111:        suffix: sor_seqsell
112:        args: -mat_type seqsell

114: TEST*/