Actual source code: ex5.c

petsc-3.7.7 2017-09-25
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,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,NULL,"-M",&M,NULL);
 29:   PetscOptionsGetInt(NULL,NULL,"-N",&N,NULL);
 30:   PetscOptionsGetBool(NULL,NULL,"-star_stencil",&flg,NULL);
 31:   if (flg) stype = DMDA_STENCIL_STAR;

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

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

 43:   DMDASetElementType(da,DMDA_ELEMENT_P1);
 44:   DMDAGetElements(da,&ne,&nc,&e);
 45:   VecGetArray(local,&lv);
 46:   for (i=0; i<ne; i++) {
 47:     PetscSynchronizedPrintf(PETSC_COMM_WORLD,"i %D e[3*i] %D %D %D\n",i,e[3*i],e[3*i+1],e[3*i+2]);
 48:     lv[e[3*i]] = i;
 49:   }
 50:   PetscSynchronizedFlush(PETSC_COMM_WORLD,stdout);
 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: }