Actual source code: ex11.c
petsc-3.8.4 2018-03-24
2: static char help[] = "Tests the use of MatZeroRows() for uniprocessor matrices.\n\n";
4: #include <petscmat.h>
6: int main(int argc,char **args)
7: {
8: Mat C;
9: PetscInt i,j,m = 5,n = 5,Ii,J;
11: PetscScalar v,five = 5.0;
12: IS isrow;
13: PetscBool keepnonzeropattern;
15: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
16: /* create the matrix for the five point stencil, YET AGAIN*/
17: MatCreate(PETSC_COMM_SELF,&C);
18: MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);
19: MatSetFromOptions(C);
20: MatSetUp(C);
21: for (i=0; i<m; i++) {
22: for (j=0; j<n; j++) {
23: v = -1.0; Ii = j + n*i;
24: if (i>0) {J = Ii - n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
25: if (i<m-1) {J = Ii + n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
26: if (j>0) {J = Ii - 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
27: if (j<n-1) {J = Ii + 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
28: v = 4.0; MatSetValues(C,1,&Ii,1,&Ii,&v,INSERT_VALUES);
29: }
30: }
31: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
32: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
34: ISCreateStride(PETSC_COMM_SELF,(m*n)/2,0,2,&isrow);
36: PetscOptionsHasName(NULL,NULL,"-keep_nonzero_pattern",&keepnonzeropattern);
37: if (keepnonzeropattern) {
38: MatSetOption(C,MAT_KEEP_NONZERO_PATTERN,PETSC_TRUE);
39: }
41: MatZeroRowsIS(C,isrow,five,0,0);
43: MatView(C,PETSC_VIEWER_STDOUT_SELF);
45: ISDestroy(&isrow);
46: MatDestroy(&C);
47: PetscFinalize();
48: return ierr;
49: }