xref: /petsc/src/mat/tests/ex6.c (revision ebead697dbf761eb322f829370bbe90b3bd93fa3)
1 
2 static char help[] = "Tests reordering a matrix.\n\n";
3 
4 #include <petscmat.h>
5 
6 int main(int argc,char **args)
7 {
8   Mat            C;
9   PetscInt       i,j,m = 5,n = 5,Ii,J;
10   PetscScalar    v;
11   IS             perm,iperm;
12 
13   PetscFunctionBeginUser;
14   PetscCall(PetscInitialize(&argc,&args,(char*)0,help));
15   PetscCall(MatCreate(PETSC_COMM_SELF,&C));
16   PetscCall(MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n));
17   PetscCall(MatSetFromOptions(C));
18   PetscCall(MatSetUp(C));
19 
20   /* create the matrix for the five point stencil, YET AGAIN*/
21   for (i=0; i<m; i++) {
22     for (j=0; j<n; j++) {
23       v = -1.0;  Ii = j + n*i;
24       if (i>0)   {J = Ii - n; PetscCall(MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES));}
25       if (i<m-1) {J = Ii + n; PetscCall(MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES));}
26       if (j>0)   {J = Ii - 1; PetscCall(MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES));}
27       if (j<n-1) {J = Ii + 1; PetscCall(MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES));}
28       v = 4.0; PetscCall(MatSetValues(C,1,&Ii,1,&Ii,&v,INSERT_VALUES));
29     }
30   }
31   PetscCall(MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY));
32   PetscCall(MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY));
33 
34   PetscCall(MatGetOrdering(C,MATORDERINGND,&perm,&iperm));
35   PetscCall(ISView(perm,PETSC_VIEWER_STDOUT_SELF));
36   PetscCall(ISView(iperm,PETSC_VIEWER_STDOUT_SELF));
37   PetscCall(MatView(C,PETSC_VIEWER_STDOUT_SELF));
38 
39   PetscCall(ISDestroy(&perm));
40   PetscCall(ISDestroy(&iperm));
41   PetscCall(MatDestroy(&C));
42   PetscCall(PetscFinalize());
43   return 0;
44 }
45 
46 /*TEST
47 
48    test:
49 
50 TEST*/
51