xref: /petsc/src/mat/tests/ex6.c (revision 98d129c30f3ee9fdddc40fdbc5a989b7be64f888)
1 static char help[] = "Tests reordering a matrix.\n\n";
2 
3 #include <petscmat.h>
4 
5 int main(int argc, char **args)
6 {
7   Mat         C;
8   PetscInt    i, j, m = 5, n = 5, Ii, J;
9   PetscScalar v;
10   IS          perm, iperm;
11 
12   PetscFunctionBeginUser;
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;
23       Ii = j + n * i;
24       if (i > 0) {
25         J = Ii - n;
26         PetscCall(MatSetValues(C, 1, &Ii, 1, &J, &v, INSERT_VALUES));
27       }
28       if (i < m - 1) {
29         J = Ii + n;
30         PetscCall(MatSetValues(C, 1, &Ii, 1, &J, &v, INSERT_VALUES));
31       }
32       if (j > 0) {
33         J = Ii - 1;
34         PetscCall(MatSetValues(C, 1, &Ii, 1, &J, &v, INSERT_VALUES));
35       }
36       if (j < n - 1) {
37         J = Ii + 1;
38         PetscCall(MatSetValues(C, 1, &Ii, 1, &J, &v, INSERT_VALUES));
39       }
40       v = 4.0;
41       PetscCall(MatSetValues(C, 1, &Ii, 1, &Ii, &v, INSERT_VALUES));
42     }
43   }
44   PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
45   PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
46 
47   PetscCall(MatGetOrdering(C, MATORDERINGND, &perm, &iperm));
48   PetscCall(ISView(perm, PETSC_VIEWER_STDOUT_SELF));
49   PetscCall(ISView(iperm, PETSC_VIEWER_STDOUT_SELF));
50   PetscCall(MatView(C, PETSC_VIEWER_STDOUT_SELF));
51 
52   PetscCall(ISDestroy(&perm));
53   PetscCall(ISDestroy(&iperm));
54   PetscCall(MatDestroy(&C));
55   PetscCall(PetscFinalize());
56   return 0;
57 }
58 
59 /*TEST
60 
61    test:
62 
63 TEST*/
64