Actual source code: ex22.c

petsc-3.5.4 2015-05-23
Report Typos and Errors
  2: static char help[] = "Tests MatSetValuesBlockedStencil() in 3d.\n\n";

  4: #include <petscmat.h>
  5: #include <petscdm.h>
  6: #include <petscdmda.h>

 10: int main(int argc,char **argv)
 11: {
 12:   PetscInt        M = 3,N = 4,P = 2,s = 1,w = 2,i, m = PETSC_DECIDE,n = PETSC_DECIDE,p = PETSC_DECIDE;
 13:   PetscErrorCode  ierr;
 14:   DM              da;
 15:   Mat             mat;
 16:   DMDAStencilType stencil_type = DMDA_STENCIL_BOX;
 17:   PetscBool       flg          = PETSC_FALSE;
 18:   MatStencil      idx[2],idy[2];
 19:   PetscScalar     *values;

 21:   PetscInitialize(&argc,&argv,(char*)0,help);

 23:   /* Read options */
 24:   PetscOptionsGetInt(NULL,"-M",&M,NULL);
 25:   PetscOptionsGetInt(NULL,"-N",&N,NULL);
 26:   PetscOptionsGetInt(NULL,"-P",&P,NULL);
 27:   PetscOptionsGetInt(NULL,"-m",&m,NULL);
 28:   PetscOptionsGetInt(NULL,"-n",&n,NULL);
 29:   PetscOptionsGetInt(NULL,"-p",&p,NULL);
 30:   PetscOptionsGetInt(NULL,"-s",&s,NULL);
 31:   PetscOptionsGetInt(NULL,"-w",&w,NULL);
 32:   PetscOptionsGetBool(NULL,"-star",&flg,NULL);
 33:   if (flg) stencil_type =  DMDA_STENCIL_STAR;

 35:   /* Create distributed array and get vectors */
 36:   DMDACreate3d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,stencil_type,M,N,P,m,n,p,w,s,
 37:                       0,0,0,&da);

 39:   DMSetMatType(da,MATMPIBAIJ);
 40:   DMCreateMatrix(da,&mat);

 42:   idx[0].i = 1;   idx[0].j = 1; idx[0].k = 0;
 43:   idx[1].i = 2;   idx[1].j = 1; idx[1].k = 0;
 44:   idy[0].i = 1;   idy[0].j = 2; idy[0].k = 0;
 45:   idy[1].i = 2;   idy[1].j = 2; idy[1].k = 0;
 46:   PetscMalloc1(2*2*w*w,&values);
 47:   for (i=0; i<2*2*w*w; i++) values[i] = i;
 48:   MatSetValuesBlockedStencil(mat,2,idx,2,idy,values,INSERT_VALUES);
 49:   MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
 50:   MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);

 52:   /* Free memory */
 53:   PetscFree(values);
 54:   MatDestroy(&mat);
 55:   DMDestroy(&da);
 56:   PetscFinalize();
 57:   return 0;
 58: }