xref: /petsc/src/mat/tests/ex3.c (revision ffa8c5705e8ab2cf85ee1d14dbe507a6e2eb5283)
1 
2 static char help[] = "Tests relaxation for dense matrices.\n\n";
3 
4 #include <petscmat.h>
5 
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;
13 
14   PetscCall(PetscInitialize(&argc,&args,(char*)0,help));
15   PetscCall(PetscOptionsGetReal(NULL,NULL,"-omega",&omega,NULL));
16   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
17 
18   PetscCall(MatCreate(PETSC_COMM_SELF,&C));
19   PetscCall(MatSetSizes(C,n,n,n,n));
20   PetscCall(MatSetType(C,MATSEQDENSE));
21   PetscCall(MatSetUp(C));
22   PetscCall(VecCreateSeq(PETSC_COMM_SELF,n,&b));
23   PetscCall(VecCreateSeq(PETSC_COMM_SELF,n,&x));
24   PetscCall(VecCreateSeq(PETSC_COMM_SELF,n,&u));
25   PetscCall(VecCreateSeq(PETSC_COMM_SELF,n,&e));
26   PetscCall(VecSet(u,1.0));
27   PetscCall(VecSet(x,0.0));
28 
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     PetscCall(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   PetscCall(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   PetscCall(MatSetValues(C,1,&i,2,midx,v,INSERT_VALUES));
40 
41   PetscCall(MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY));
42   PetscCall(MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY));
43 
44   PetscCall(MatMult(C,u,b));
45 
46   for (i=0; i<n; i++) {
47     PetscCall(MatSOR(C,b,omega,SOR_FORWARD_SWEEP,0.0,1,1,x));
48     PetscCall(VecWAXPY(e,-1.0,x,u));
49     PetscCall(VecNorm(e,NORM_2,&norm));
50     PetscCall(PetscPrintf(PETSC_COMM_SELF,"2-norm of error %g\n",(double)norm));
51   }
52   PetscCall(MatDestroy(&C));
53   PetscCall(VecDestroy(&x));
54   PetscCall(VecDestroy(&b));
55   PetscCall(VecDestroy(&u));
56   PetscCall(VecDestroy(&e));
57   PetscCall(PetscFinalize());
58   return 0;
59 }
60 
61 /*TEST
62 
63    test:
64 
65 TEST*/
66