Actual source code: ex3.c
1: /*$Id: ex3.c,v 1.19 2001/08/07 21:30:08 bsmith Exp $*/
3: static char help[] = "Tests relaxation for dense matrices.\n\n";
5: #include petscmat.h
9: int main(int argc,char **args)
10: {
11: Mat C;
12: Vec u,x,b,e;
13: int i,n = 10,midx[3],ierr;
14: PetscScalar v[3],one = 1.0,zero = 0.0,mone = -1.0;
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,n,n,n,n,&C);
22: MatSetType(C,MATSEQDENSE);
23: VecCreateSeq(PETSC_COMM_SELF,n,&b);
24: VecCreateSeq(PETSC_COMM_SELF,n,&x);
25: VecCreateSeq(PETSC_COMM_SELF,n,&u);
26: VecCreateSeq(PETSC_COMM_SELF,n,&e);
27: VecSet(&one,u);
28: VecSet(&zero,x);
30: v[0] = -1.; v[1] = 2.; v[2] = -1.;
31: for (i=1; i<n-1; i++){
32: midx[0] = i-1; midx[1] = i; midx[2] = i+1;
33: MatSetValues(C,1,&i,3,midx,v,INSERT_VALUES);
34: }
35: i = 0; midx[0] = 0; midx[1] = 1;
36: v[0] = 2.0; v[1] = -1.;
37: MatSetValues(C,1,&i,2,midx,v,INSERT_VALUES);
38: i = n-1; midx[0] = n-2; midx[1] = n-1;
39: v[0] = -1.0; v[1] = 2.;
40: MatSetValues(C,1,&i,2,midx,v,INSERT_VALUES);
42: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
43: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
45: MatMult(C,u,b);
47: for (i=0; i<n; i++) {
48: MatRelax(C,b,omega,SOR_FORWARD_SWEEP,0.0,1,1,x);
49: VecWAXPY(&mone,x,u,e);
50: VecNorm(e,NORM_2,&norm);
51: PetscPrintf(PETSC_COMM_SELF,"2-norm of error %g\n",norm);
52: }
53: MatDestroy(C);
54: VecDestroy(x);
55: VecDestroy(b);
56: VecDestroy(u);
57: VecDestroy(e);
58: PetscFinalize();
59: return 0;
60: }
62: