Actual source code: ex8.c
petsc-3.14.6 2021-03-30
2: static char help[] = "Tests automatic allocation of matrix storage space.\n\n";
4: #include <petscmat.h>
6: int main(int argc,char **args)
7: {
8: Mat C;
9: PetscInt i,j,m = 3,n = 3,Ii,J;
11: PetscScalar v;
12: MatInfo info;
14: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
15: PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
16: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
18: /* create the matrix for the five point stencil, YET AGAIN */
19: MatCreate(PETSC_COMM_SELF,&C);
20: MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);
21: MatSetFromOptions(C);
22: MatSetUp(C);
23: for (i=0; i<m; i++) {
24: for (j=0; j<n; j++) {
25: v = -1.0; Ii = j + n*i;
26: if (i>0) {J=Ii-n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
27: if (i<m-1) {J=Ii+n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
28: if (j>0) {J=Ii-1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
29: if (j<n-1) {J=Ii+1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
30: v = 4.0; MatSetValues(C,1,&Ii,1,&Ii,&v,INSERT_VALUES);
31: }
32: }
33: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
34: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
35: MatView(C,PETSC_VIEWER_STDOUT_SELF);
37: MatGetInfo(C,MAT_LOCAL,&info);
38: PetscPrintf(PETSC_COMM_SELF,"matrix nonzeros = %D, allocated nonzeros = %D\n",(PetscInt)info.nz_used,(PetscInt)info.nz_allocated);
40: MatDestroy(&C);
41: PetscFinalize();
42: return ierr;
43: }
46: /*TEST
48: test:
50: TEST*/