xref: /petsc/src/mat/tests/ex11.c (revision ffa8c5705e8ab2cf85ee1d14dbe507a6e2eb5283)
1 
2 static char help[] = "Tests the use of MatZeroRows() for uniprocessor matrices.\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,five = 5.0;
11   IS             isrow;
12   PetscBool      keepnonzeropattern;
13 
14   PetscCall(PetscInitialize(&argc,&args,(char*)0,help));
15   /* create the matrix for the five point stencil, YET AGAIN*/
16   PetscCall(MatCreate(PETSC_COMM_SELF,&C));
17   PetscCall(MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n));
18   PetscCall(MatSetFromOptions(C));
19   PetscCall(MatSetUp(C));
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(ISCreateStride(PETSC_COMM_SELF,(m*n)/2,0,2,&isrow));
34 
35   PetscCall(PetscOptionsHasName(NULL,NULL,"-keep_nonzero_pattern",&keepnonzeropattern));
36   if (keepnonzeropattern) {
37     PetscCall(MatSetOption(C,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE));
38   }
39 
40   PetscCall(MatZeroRowsIS(C,isrow,five,0,0));
41 
42   PetscCall(MatView(C,PETSC_VIEWER_STDOUT_SELF));
43 
44   PetscCall(ISDestroy(&isrow));
45   PetscCall(MatDestroy(&C));
46   PetscCall(PetscFinalize());
47   return 0;
48 }
49 
50 /*TEST
51 
52    test:
53 
54    test:
55       suffix: 2
56       args: -mat_type seqbaij -mat_block_size 5
57 
58    test:
59       suffix: 3
60       args: -keep_nonzero_pattern
61 
62    test:
63       suffix: 4
64       args: -keep_nonzero_pattern -mat_type seqbaij -mat_block_size 5
65 
66 TEST*/
67