xref: /petsc/src/mat/tests/ex24.c (revision ebead697dbf761eb322f829370bbe90b3bd93fa3)
1 
2 static char help[] = "Tests the different MatColoring implementatons and ISColoringTestValid() \n\
3                       Modifed from the code contributed by Ali Berk Kahraman. \n\n";
4 #include <petscmat.h>
5 
6 PetscErrorCode FormJacobian(Mat A)
7 {
8   PetscInt       M,ownbegin,ownend,i,j;
9   PetscScalar    dummy=0.0;
10 
11   PetscFunctionBeginUser;
12   PetscCall(MatGetSize(A,&M,NULL));
13   PetscCall(MatGetOwnershipRange(A,&ownbegin,&ownend));
14 
15   for (i=ownbegin; i<ownend; i++) {
16     for (j=i-3; j<i+3; j++) {
17       if (j >= 0 && j < M) {
18         PetscCall(MatSetValues(A,1,&i,1,&j,&dummy,INSERT_VALUES));
19       }
20     }
21   }
22   PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
23   PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
24   PetscFunctionReturn(0);
25 }
26 
27 int main(int argc, char *argv[])
28 {
29   Mat            J;
30   PetscMPIInt    size;
31   PetscInt       M=8;
32   ISColoring     iscoloring;
33   MatColoring    coloring;
34 
35   PetscFunctionBeginUser;
36   PetscCall(PetscInitialize(&argc,&argv,(char*)0,help));
37   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size));
38 
39   PetscCall(MatCreate(PETSC_COMM_WORLD,&J));
40   PetscCall(MatSetSizes(J, PETSC_DECIDE, PETSC_DECIDE, M, M));
41   PetscCall(MatSetFromOptions(J));
42   PetscCall(MatSetUp(J));
43 
44   PetscCall(FormJacobian(J));
45   PetscCall(MatView(J,PETSC_VIEWER_STDOUT_WORLD));
46 
47   /*
48     Color the matrix, i.e. determine groups of columns that share no common
49     rows. These columns in the Jacobian can all be computed simultaneously.
50    */
51   PetscCall(MatColoringCreate(J, &coloring));
52   PetscCall(MatColoringSetType(coloring,MATCOLORINGGREEDY));
53   PetscCall(MatColoringSetFromOptions(coloring));
54   PetscCall(MatColoringApply(coloring, &iscoloring));
55 
56   if (size == 1) {
57     PetscCall(MatISColoringTest(J,iscoloring));
58   }
59 
60   PetscCall(ISColoringDestroy(&iscoloring));
61   PetscCall(MatColoringDestroy(&coloring));
62   PetscCall(MatDestroy(&J));
63   PetscCall(PetscFinalize());
64   return 0;
65 }
66 
67 /*TEST
68 
69    test:
70       suffix: sl
71       requires: !complex double !defined(PETSC_USE_64BIT_INDICES)
72       args: -mat_coloring_type sl
73       output_file: output/ex24_1.out
74 
75    test:
76       suffix: lf
77       requires: !complex double !defined(PETSC_USE_64BIT_INDICES)
78       args: -mat_coloring_type lf
79       output_file: output/ex24_1.out
80 
81    test:
82       suffix: id
83       requires: !complex double !defined(PETSC_USE_64BIT_INDICES)
84       args: -mat_coloring_type id
85       output_file: output/ex24_1.out
86 
87 TEST*/
88