xref: /petsc/src/mat/tests/ex24.c (revision ffa8c5705e8ab2cf85ee1d14dbe507a6e2eb5283)
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   PetscCall(PetscInitialize(&argc,&argv,(char*)0,help));
36   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size));
37 
38   PetscCall(MatCreate(PETSC_COMM_WORLD,&J));
39   PetscCall(MatSetSizes(J, PETSC_DECIDE, PETSC_DECIDE, M, M));
40   PetscCall(MatSetFromOptions(J));
41   PetscCall(MatSetUp(J));
42 
43   PetscCall(FormJacobian(J));
44   PetscCall(MatView(J,PETSC_VIEWER_STDOUT_WORLD));
45 
46   /*
47     Color the matrix, i.e. determine groups of columns that share no common
48     rows. These columns in the Jacobian can all be computed simultaneously.
49    */
50   PetscCall(MatColoringCreate(J, &coloring));
51   PetscCall(MatColoringSetType(coloring,MATCOLORINGGREEDY));
52   PetscCall(MatColoringSetFromOptions(coloring));
53   PetscCall(MatColoringApply(coloring, &iscoloring));
54 
55   if (size == 1) {
56     PetscCall(MatISColoringTest(J,iscoloring));
57   }
58 
59   PetscCall(ISColoringDestroy(&iscoloring));
60   PetscCall(MatColoringDestroy(&coloring));
61   PetscCall(MatDestroy(&J));
62   PetscCall(PetscFinalize());
63   return 0;
64 }
65 
66 /*TEST
67 
68    test:
69       suffix: sl
70       requires: !complex double !defined(PETSC_USE_64BIT_INDICES)
71       args: -mat_coloring_type sl
72       output_file: output/ex24_1.out
73 
74    test:
75       suffix: lf
76       requires: !complex double !defined(PETSC_USE_64BIT_INDICES)
77       args: -mat_coloring_type lf
78       output_file: output/ex24_1.out
79 
80    test:
81       suffix: id
82       requires: !complex double !defined(PETSC_USE_64BIT_INDICES)
83       args: -mat_coloring_type id
84       output_file: output/ex24_1.out
85 
86 TEST*/
87