xref: /petsc/src/mat/tests/ex6.c (revision ffa8c5705e8ab2cf85ee1d14dbe507a6e2eb5283)
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   PetscCall(PetscInitialize(&argc,&args,(char*)0,help));
14   PetscCall(MatCreate(PETSC_COMM_SELF,&C));
15   PetscCall(MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n));
16   PetscCall(MatSetFromOptions(C));
17   PetscCall(MatSetUp(C));
18 
19   /* create the matrix for the five point stencil, YET AGAIN*/
20   for (i=0; i<m; i++) {
21     for (j=0; j<n; j++) {
22       v = -1.0;  Ii = j + n*i;
23       if (i>0)   {J = Ii - n; PetscCall(MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES));}
24       if (i<m-1) {J = Ii + n; PetscCall(MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES));}
25       if (j>0)   {J = Ii - 1; PetscCall(MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES));}
26       if (j<n-1) {J = Ii + 1; PetscCall(MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES));}
27       v = 4.0; PetscCall(MatSetValues(C,1,&Ii,1,&Ii,&v,INSERT_VALUES));
28     }
29   }
30   PetscCall(MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY));
31   PetscCall(MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY));
32 
33   PetscCall(MatGetOrdering(C,MATORDERINGND,&perm,&iperm));
34   PetscCall(ISView(perm,PETSC_VIEWER_STDOUT_SELF));
35   PetscCall(ISView(iperm,PETSC_VIEWER_STDOUT_SELF));
36   PetscCall(MatView(C,PETSC_VIEWER_STDOUT_SELF));
37 
38   PetscCall(ISDestroy(&perm));
39   PetscCall(ISDestroy(&iperm));
40   PetscCall(MatDestroy(&C));
41   PetscCall(PetscFinalize());
42   return 0;
43 }
44 
45 /*TEST
46 
47    test:
48 
49 TEST*/
50