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