Actual source code: ex3.c
1: /*$Id: ex3.c,v 1.16 2001/03/23 23:22:29 balay Exp $*/
3: static char help[] = "Tests relaxation for dense matrices.nn";
5: #include "petscmat.h"
7: int main(int argc,char **args)
8: {
9: Mat C;
10: Vec u,x,b,e;
11: int i,n = 10,midx[3],ierr;
12: Scalar v[3],one = 1.0,zero = 0.0,mone = -1.0;
13: double omega = 1.0,norm;
15: PetscInitialize(&argc,&args,(char *)0,help);
16: PetscOptionsGetDouble(PETSC_NULL,"-omega",&omega,PETSC_NULL);
17: PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
19: MatCreateSeqDense(PETSC_COMM_SELF,n,n,PETSC_NULL,&C);
20: VecCreateSeq(PETSC_COMM_SELF,n,&b);
21: VecCreateSeq(PETSC_COMM_SELF,n,&x);
22: VecCreateSeq(PETSC_COMM_SELF,n,&u);
23: VecCreateSeq(PETSC_COMM_SELF,n,&e);
24: VecSet(&one,u);
25: VecSet(&zero,x);
27: v[0] = -1.; v[1] = 2.; v[2] = -1.;
28: for (i=1; i<n-1; i++){
29: midx[0] = i-1; midx[1] = i; midx[2] = i+1;
30: MatSetValues(C,1,&i,3,midx,v,INSERT_VALUES);
31: }
32: i = 0; midx[0] = 0; midx[1] = 1;
33: v[0] = 2.0; v[1] = -1.;
34: MatSetValues(C,1,&i,2,midx,v,INSERT_VALUES);
35: i = n-1; midx[0] = n-2; midx[1] = n-1;
36: v[0] = -1.0; v[1] = 2.;
37: MatSetValues(C,1,&i,2,midx,v,INSERT_VALUES);
39: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
40: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
42: MatMult(C,u,b);
44: for (i=0; i<n; i++) {
45: MatRelax(C,b,omega,SOR_FORWARD_SWEEP,0.0,1,x);
46: VecWAXPY(&mone,x,u,e);
47: VecNorm(e,NORM_2,&norm);
48: PetscPrintf(PETSC_COMM_SELF,"2-norm of error %gn",norm);
49: }
50: MatDestroy(C);
51: VecDestroy(x);
52: VecDestroy(b);
53: VecDestroy(u);
54: VecDestroy(e);
55: PetscFinalize();
56: return 0;
57: }
59: