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