Actual source code: ex3.c
2: static char help[] = "Tests relaxation for dense matrices.\n\n";
4: #include petscmat.h
8: int main(int argc,char **args)
9: {
10: Mat C;
11: Vec u,x,b,e;
12: PetscInt i,n = 10,midx[3];
14: PetscScalar v[3];
15: PetscReal omega = 1.0,norm;
17: PetscInitialize(&argc,&args,(char *)0,help);
18: PetscOptionsGetReal(PETSC_NULL,"-omega",&omega,PETSC_NULL);
19: PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
21: MatCreate(PETSC_COMM_SELF,&C);
22: MatSetSizes(C,n,n,n,n);
23: MatSetType(C,MATSEQDENSE);
24: VecCreateSeq(PETSC_COMM_SELF,n,&b);
25: VecCreateSeq(PETSC_COMM_SELF,n,&x);
26: VecCreateSeq(PETSC_COMM_SELF,n,&u);
27: VecCreateSeq(PETSC_COMM_SELF,n,&e);
28: VecSet(u,1.0);
29: VecSet(x,0.0);
31: v[0] = -1.; v[1] = 2.; v[2] = -1.;
32: for (i=1; i<n-1; i++){
33: midx[0] = i-1; midx[1] = i; midx[2] = i+1;
34: MatSetValues(C,1,&i,3,midx,v,INSERT_VALUES);
35: }
36: i = 0; midx[0] = 0; midx[1] = 1;
37: v[0] = 2.0; v[1] = -1.;
38: MatSetValues(C,1,&i,2,midx,v,INSERT_VALUES);
39: i = n-1; midx[0] = n-2; midx[1] = n-1;
40: v[0] = -1.0; v[1] = 2.;
41: MatSetValues(C,1,&i,2,midx,v,INSERT_VALUES);
43: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
44: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
46: MatMult(C,u,b);
48: for (i=0; i<n; i++) {
49: MatRelax(C,b,omega,SOR_FORWARD_SWEEP,0.0,1,1,x);
50: VecWAXPY(e,-1.0,x,u);
51: VecNorm(e,NORM_2,&norm);
52: PetscPrintf(PETSC_COMM_SELF,"2-norm of error %G\n",norm);
53: }
54: MatDestroy(C);
55: VecDestroy(x);
56: VecDestroy(b);
57: VecDestroy(u);
58: VecDestroy(e);
59: PetscFinalize();
60: return 0;
61: }
63: