xref: /petsc/src/mat/tests/ex24.c (revision 40badf4fbc550ac1f60bd080eaff6de6d55b946d)
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   CHKERRQ(MatGetSize(A,&M,NULL));
13   CHKERRQ(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         CHKERRQ(MatSetValues(A,1,&i,1,&j,&dummy,INSERT_VALUES));
19       }
20     }
21   }
22   CHKERRQ(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
23   CHKERRQ(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
24   PetscFunctionReturn(0);
25 }
26 
27 int main(int argc, char *argv[])
28 {
29   PetscErrorCode ierr;
30   Mat            J;
31   PetscMPIInt    size;
32   PetscInt       M=8;
33   ISColoring     iscoloring;
34   MatColoring    coloring;
35 
36   ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
37   CHKERRMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size));
38 
39   CHKERRQ(MatCreate(PETSC_COMM_WORLD,&J));
40   CHKERRQ(MatSetSizes(J, PETSC_DECIDE, PETSC_DECIDE, M, M));
41   CHKERRQ(MatSetFromOptions(J));
42   CHKERRQ(MatSetUp(J));
43 
44   CHKERRQ(FormJacobian(J));
45   CHKERRQ(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   CHKERRQ(MatColoringCreate(J, &coloring));
52   CHKERRQ(MatColoringSetType(coloring,MATCOLORINGGREEDY));
53   CHKERRQ(MatColoringSetFromOptions(coloring));
54   CHKERRQ(MatColoringApply(coloring, &iscoloring));
55 
56   if (size == 1) {
57     CHKERRQ(MatISColoringTest(J,iscoloring));
58   }
59 
60   CHKERRQ(ISColoringDestroy(&iscoloring));
61   CHKERRQ(MatColoringDestroy(&coloring));
62   CHKERRQ(MatDestroy(&J));
63   ierr = PetscFinalize();
64   return ierr;
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