Actual source code: ex5.c

petsc-3.3-p7 2013-05-11
  2: static char help[] = "Tests DMDAGetElements() and VecView() contour plotting for 2d DMDAs.\n\n";

  4: #include <petscdmda.h>

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

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

 25:   /* Read options */
 26:   PetscOptionsGetInt(PETSC_NULL,"-M",&M,PETSC_NULL);
 27:   PetscOptionsGetInt(PETSC_NULL,"-N",&N,PETSC_NULL);
 28:   PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);
 29:   PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
 30:   PetscOptionsGetBool(PETSC_NULL,"-star_stencil",&flg,PETSC_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,m,n,1,1,PETSC_NULL,PETSC_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:     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]);
 48:     lv[e[3*i]] = i;
 49:   }
 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: }
 67: