Actual source code: ex5.c

petsc-3.4.4 2014-03-13
  2: static char help[] = "Tests DMDAGetElements() and VecView() contour plotting for 2d DMDAs.\n\n";

  4: #include <petscdmda.h>
  5: #include <petscdraw.h>

  9: int main(int argc,char **argv)
 10: {
 11:   PetscInt         M = 10,N = 8,m = PETSC_DECIDE,n = PETSC_DECIDE,ne,nc,i;
 12:   const PetscInt   *e;
 13:   PetscErrorCode   ierr;
 14:   PetscBool        flg = PETSC_FALSE;
 15:   DM               da;
 16:   PetscViewer      viewer;
 17:   Vec              local,global;
 18:   PetscScalar      value;
 19:   DMDABoundaryType bx    = DMDA_BOUNDARY_NONE,by = DMDA_BOUNDARY_NONE;
 20:   DMDAStencilType  stype = DMDA_STENCIL_BOX;
 21:   PetscScalar      *lv;

 23:   PetscInitialize(&argc,&argv,(char*)0,help);
 24:   PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",300,0,300,300,&viewer);

 26:   /* Read options */
 27:   PetscOptionsGetInt(NULL,"-M",&M,NULL);
 28:   PetscOptionsGetInt(NULL,"-N",&N,NULL);
 29:   PetscOptionsGetInt(NULL,"-m",&m,NULL);
 30:   PetscOptionsGetInt(NULL,"-n",&n,NULL);
 31:   PetscOptionsGetBool(NULL,"-star_stencil",&flg,NULL);
 32:   if (flg) stype = DMDA_STENCIL_STAR;

 34:   /* Create distributed array and get vectors */
 35:   DMDACreate2d(PETSC_COMM_WORLD,bx,by,stype,M,N,m,n,1,1,NULL,NULL,&da);
 36:   DMCreateGlobalVector(da,&global);
 37:   DMCreateLocalVector(da,&local);

 39:   value = -3.0;
 40:   VecSet(global,value);
 41:   DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);
 42:   DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);

 44:   DMDASetElementType(da,DMDA_ELEMENT_P1);
 45:   DMDAGetElements(da,&ne,&nc,&e);
 46:   VecGetArray(local,&lv);
 47:   for (i=0; i<ne; i++) {
 48:     PetscPrintf(PETSC_COMM_WORLD,"i %D e[3*i] %D %D %D\n",i,e[3*i],e[3*i+1],e[3*i+2]);
 49:     lv[e[3*i]] = i;
 50:   }
 51:   VecRestoreArray(local,&lv);
 52:   DMDARestoreElements(da,&ne,&nc,&e);

 54:   DMLocalToGlobalBegin(da,local,ADD_VALUES,global);
 55:   DMLocalToGlobalEnd(da,local,ADD_VALUES,global);

 57:   DMView(da,viewer);
 58:   VecView(global,viewer);

 60:   /* Free memory */
 61:   PetscViewerDestroy(&viewer);
 62:   VecDestroy(&local);
 63:   VecDestroy(&global);
 64:   DMDestroy(&da);
 65:   PetscFinalize();
 66:   return 0;
 67: }