Actual source code: ex8.c

petsc-3.6.1 2015-08-06
Report Typos and Errors
  2: static char help[] = "Tests automatic allocation of matrix storage space.\n\n";

  4: #include <petscmat.h>

  8: int main(int argc,char **args)
  9: {
 10:   Mat            C;
 11:   PetscInt       i,j,m = 3,n = 3,Ii,J;
 13:   PetscScalar    v;
 14:   MatInfo        info;

 16:   PetscInitialize(&argc,&args,(char*)0,help);
 17:   PetscOptionsGetInt(NULL,"-m",&m,NULL);
 18:   PetscOptionsGetInt(NULL,"-n",&n,NULL);

 20:   /* create the matrix for the five point stencil, YET AGAIN */
 21:   MatCreate(PETSC_COMM_SELF,&C);
 22:   MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);
 23:   MatSetFromOptions(C);
 24:   MatSetUp(C);
 25:   for (i=0; i<m; i++) {
 26:     for (j=0; j<n; j++) {
 27:       v = -1.0;  Ii = j + n*i;
 28:       if (i>0)   {J=Ii-n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
 29:       if (i<m-1) {J=Ii+n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
 30:       if (j>0)   {J=Ii-1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
 31:       if (j<n-1) {J=Ii+1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
 32:       v = 4.0; MatSetValues(C,1,&Ii,1,&Ii,&v,INSERT_VALUES);
 33:     }
 34:   }
 35:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
 36:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
 37:   MatView(C,PETSC_VIEWER_STDOUT_SELF);

 39:   MatGetInfo(C,MAT_LOCAL,&info);
 40:   PetscPrintf(PETSC_COMM_SELF,"matrix nonzeros = %D, allocated nonzeros = %D\n",
 41:                      (PetscInt)info.nz_used,(PetscInt)info.nz_allocated);

 43:   MatDestroy(&C);
 44:   PetscFinalize();
 45:   return 0;
 46: }