Actual source code: ex5.c

petsc-3.6.1 2015-08-06
Report Typos and Errors
  2: static char help[] = "Tests DMDAGetElements() and VecView() contour plotting for 2d DMDAs.\n\n";

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

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

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

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

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

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

 45:   DMDASetElementType(da,DMDA_ELEMENT_P1);
 46:   DMDAGetElements(da,&ne,&nc,&e);
 47:   VecGetArray(local,&lv);
 48:   for (i=0; i<ne; i++) {
 49:     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]);
 50:     lv[e[3*i]] = i;
 51:   }
 52:   VecRestoreArray(local,&lv);
 53:   DMDARestoreElements(da,&ne,&nc,&e);

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

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

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