Actual source code: ex4.c
1: /*$Id: ex4.c,v 1.45 2001/08/07 03:03:43 balay Exp $*/
3: static char help[] = "Demonstrates the use of fast Richardson for SOR. And tests\n\
4: the MatRelax() routines.\n\n";
6: #include petscpc.h
10: int main(int argc,char **args)
11: {
12: Mat mat;
13: Vec b,u;
14: PC pc;
15: int ierr,n = 5,i,col[3];
16: PetscScalar value[3],zero = 0.0;
18: PetscInitialize(&argc,&args,(char *)0,help);
20: /* Create vectors */
21: VecCreateSeq(PETSC_COMM_SELF,n,&b);
22: VecCreateSeq(PETSC_COMM_SELF,n,&u);
24: /* Create and assemble matrix */
25: MatCreateSeqDense(PETSC_COMM_SELF,n,n,PETSC_NULL,&mat);
26: value[0] = -1.0; value[1] = 2.0; value[2] = -1.0;
27: for (i=1; i<n-1; i++) {
28: col[0] = i-1; col[1] = i; col[2] = i+1;
29: MatSetValues(mat,1,&i,3,col,value,INSERT_VALUES);
30: }
31: i = n - 1; col[0] = n - 2; col[1] = n - 1;
32: MatSetValues(mat,1,&i,2,col,value,INSERT_VALUES);
33: i = 0; col[0] = 0; col[1] = 1; value[0] = 2.0; value[1] = -1.0;
34: MatSetValues(mat,1,&i,2,col,value,INSERT_VALUES);
35: MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
36: MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);
38: /* Create PC context and set up data structures */
39: PCCreate(PETSC_COMM_WORLD,&pc);
40: PCSetType(pc,PCSOR);
41: PCSetFromOptions(pc);
42: PCSetOperators(pc,mat,mat,DIFFERENT_NONZERO_PATTERN);
43: PCSetVector(pc,u);
44: PCSetUp(pc);
46: value[0] = 1.0;
47: for (i=0; i<n; i++) {
48: VecSet(&zero,u);
49: VecSetValues(u,1,&i,value,INSERT_VALUES);
50: PCApply(pc,u,b,PC_LEFT);
51: VecView(b,PETSC_VIEWER_STDOUT_SELF);
52: }
54: /* Free data structures */
55: MatDestroy(mat);
56: PCDestroy(pc);
57: VecDestroy(u);
58: VecDestroy(b);
59: PetscFinalize();
60: return 0;
61: }
62: