Actual source code: ex5.c


  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>

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

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

 24:   /* Read options */
 25:   PetscOptionsGetInt(NULL,NULL,"-M",&M,NULL);
 26:   PetscOptionsGetInt(NULL,NULL,"-N",&N,NULL);
 27:   PetscOptionsGetBool(NULL,NULL,"-star_stencil",&flg,NULL);
 28:   if (flg) stype = DMDA_STENCIL_STAR;

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

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

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

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

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

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

 68: /*TEST

 70:    test:
 71:      requires: x

 73:    test:
 74:      suffix: 2
 75:      nsize: 2
 76:      requires: x

 78: TEST*/