1 2 static char help[] = "Demonstrates the use of fast Richardson for SOR. And tests\n\ 3 the MatSOR() routines.\n\n"; 4 5 #include <petscpc.h> 6 7 int main(int argc, char **args) 8 { 9 Mat mat; 10 Vec b, u; 11 PC pc; 12 PetscInt n = 5, i, col[3]; 13 PetscScalar value[3]; 14 15 PetscFunctionBeginUser; 16 PetscCall(PetscInitialize(&argc, &args, (char *)0, help)); 17 /* Create vectors */ 18 PetscCall(VecCreateSeq(PETSC_COMM_SELF, n, &b)); 19 PetscCall(VecCreateSeq(PETSC_COMM_SELF, n, &u)); 20 21 /* Create and assemble matrix */ 22 PetscCall(MatCreateSeqDense(PETSC_COMM_SELF, n, n, NULL, &mat)); 23 value[0] = -1.0; 24 value[1] = 2.0; 25 value[2] = -1.0; 26 for (i = 1; i < n - 1; i++) { 27 col[0] = i - 1; 28 col[1] = i; 29 col[2] = i + 1; 30 PetscCall(MatSetValues(mat, 1, &i, 3, col, value, INSERT_VALUES)); 31 } 32 i = n - 1; 33 col[0] = n - 2; 34 col[1] = n - 1; 35 PetscCall(MatSetValues(mat, 1, &i, 2, col, value, INSERT_VALUES)); 36 i = 0; 37 col[0] = 0; 38 col[1] = 1; 39 value[0] = 2.0; 40 value[1] = -1.0; 41 PetscCall(MatSetValues(mat, 1, &i, 2, col, value, INSERT_VALUES)); 42 PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY)); 43 PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY)); 44 45 /* Create PC context and set up data structures */ 46 PetscCall(PCCreate(PETSC_COMM_WORLD, &pc)); 47 PetscCall(PCSetType(pc, PCSOR)); 48 PetscCall(PCSetFromOptions(pc)); 49 PetscCall(PCSetOperators(pc, mat, mat)); 50 PetscCall(PCSetUp(pc)); 51 52 value[0] = 1.0; 53 for (i = 0; i < n; i++) { 54 PetscCall(VecSet(u, 0.0)); 55 PetscCall(VecSetValues(u, 1, &i, value, INSERT_VALUES)); 56 PetscCall(VecAssemblyBegin(u)); 57 PetscCall(VecAssemblyEnd(u)); 58 PetscCall(PCApply(pc, u, b)); 59 PetscCall(VecView(b, PETSC_VIEWER_STDOUT_SELF)); 60 } 61 62 /* Free data structures */ 63 PetscCall(MatDestroy(&mat)); 64 PetscCall(PCDestroy(&pc)); 65 PetscCall(VecDestroy(&u)); 66 PetscCall(VecDestroy(&b)); 67 PetscCall(PetscFinalize()); 68 return 0; 69 } 70 71 /*TEST 72 73 test: 74 75 TEST*/ 76