xref: /petsc/src/mat/tests/ex15.c (revision daa037dfd3c3bec8dc8659548d2b20b07c1dc6de)
1 
2 static char help[] = "Tests MatNorm(), MatLUFactor(), MatSolve() and MatSolveAdd().\n\n";
3 
4 #include <petscmat.h>
5 
6 int main(int argc,char **args)
7 {
8   Mat            C;
9   PetscInt       i,j,m = 3,n = 3,Ii,J;
10   PetscBool      flg;
11   PetscScalar    v;
12   IS             perm,iperm;
13   Vec            x,u,b,y;
14   PetscReal      norm,tol=PETSC_SMALL;
15   MatFactorInfo  info;
16   PetscMPIInt    size;
17 
18   PetscCall(PetscInitialize(&argc,&args,(char*)0,help));
19   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size));
20   PetscCheck(size == 1,PETSC_COMM_WORLD,PETSC_ERR_WRONG_MPI_SIZE,"This is a uniprocessor example only!");
21   PetscCall(MatCreate(PETSC_COMM_WORLD,&C));
22   PetscCall(MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n));
23   PetscCall(MatSetFromOptions(C));
24   PetscCall(MatSetUp(C));
25   PetscCall(PetscOptionsHasName(NULL,NULL,"-symmetric",&flg));
26   if (flg) {  /* Treat matrix as symmetric only if we set this flag */
27     PetscCall(MatSetOption(C,MAT_SYMMETRIC,PETSC_TRUE));
28     PetscCall(MatSetOption(C,MAT_SYMMETRY_ETERNAL,PETSC_TRUE));
29   }
30 
31   /* Create the matrix for the five point stencil, YET AGAIN */
32   for (i=0; i<m; i++) {
33     for (j=0; j<n; j++) {
34       v = -1.0;  Ii = j + n*i;
35       if (i>0)   {J = Ii - n; PetscCall(MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES));}
36       if (i<m-1) {J = Ii + n; PetscCall(MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES));}
37       if (j>0)   {J = Ii - 1; PetscCall(MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES));}
38       if (j<n-1) {J = Ii + 1; PetscCall(MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES));}
39       v = 4.0; PetscCall(MatSetValues(C,1,&Ii,1,&Ii,&v,INSERT_VALUES));
40     }
41   }
42   PetscCall(MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY));
43   PetscCall(MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY));
44   PetscCall(MatGetOrdering(C,MATORDERINGRCM,&perm,&iperm));
45   PetscCall(MatView(C,PETSC_VIEWER_STDOUT_WORLD));
46   PetscCall(ISView(perm,PETSC_VIEWER_STDOUT_SELF));
47   PetscCall(VecCreateSeq(PETSC_COMM_SELF,m*n,&u));
48   PetscCall(VecSet(u,1.0));
49   PetscCall(VecDuplicate(u,&x));
50   PetscCall(VecDuplicate(u,&b));
51   PetscCall(VecDuplicate(u,&y));
52   PetscCall(MatMult(C,u,b));
53   PetscCall(VecCopy(b,y));
54   PetscCall(VecScale(y,2.0));
55 
56   PetscCall(MatNorm(C,NORM_FROBENIUS,&norm));
57   PetscCall(PetscPrintf(PETSC_COMM_SELF,"Frobenius norm of matrix %g\n",(double)norm));
58   PetscCall(MatNorm(C,NORM_1,&norm));
59   PetscCall(PetscPrintf(PETSC_COMM_SELF,"One  norm of matrix %g\n",(double)norm));
60   PetscCall(MatNorm(C,NORM_INFINITY,&norm));
61   PetscCall(PetscPrintf(PETSC_COMM_SELF,"Infinity norm of matrix %g\n",(double)norm));
62 
63   PetscCall(MatFactorInfoInitialize(&info));
64   info.fill          = 2.0;
65   info.dtcol         = 0.0;
66   info.zeropivot     = 1.e-14;
67   info.pivotinblocks = 1.0;
68 
69   PetscCall(MatLUFactor(C,perm,iperm,&info));
70 
71   /* Test MatSolve */
72   PetscCall(MatSolve(C,b,x));
73   PetscCall(VecView(b,PETSC_VIEWER_STDOUT_SELF));
74   PetscCall(VecView(x,PETSC_VIEWER_STDOUT_SELF));
75   PetscCall(VecAXPY(x,-1.0,u));
76   PetscCall(VecNorm(x,NORM_2,&norm));
77   if (norm > tol) {
78     PetscCall(PetscPrintf(PETSC_COMM_SELF,"MatSolve: Norm of error %g\n",(double)norm));
79   }
80 
81   /* Test MatSolveAdd */
82   PetscCall(MatSolveAdd(C,b,y,x));
83   PetscCall(VecAXPY(x,-1.0,y));
84   PetscCall(VecAXPY(x,-1.0,u));
85   PetscCall(VecNorm(x,NORM_2,&norm));
86   if (norm > tol) {
87     PetscCall(PetscPrintf(PETSC_COMM_SELF,"MatSolveAdd(): Norm of error %g\n",(double)norm));
88   }
89 
90   PetscCall(ISDestroy(&perm));
91   PetscCall(ISDestroy(&iperm));
92   PetscCall(VecDestroy(&u));
93   PetscCall(VecDestroy(&y));
94   PetscCall(VecDestroy(&b));
95   PetscCall(VecDestroy(&x));
96   PetscCall(MatDestroy(&C));
97   PetscCall(PetscFinalize());
98   return 0;
99 }
100 
101 /*TEST
102 
103    test:
104 
105 TEST*/
106