Actual source code: ex5.c

petsc-3.12.5 2020-03-29
Report Typos and Errors

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


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

  9: int main(int argc,char **argv)
 10: {
 11:   PetscInt         M = 10,N = 8,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:   DMBoundaryType   bx    = DM_BOUNDARY_NONE,by = DM_BOUNDARY_NONE;
 20:   DMDAStencilType  stype = DMDA_STENCIL_BOX;
 21:   PetscScalar      *lv;

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

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

 32:   /* Create distributed array and get vectors */
 33:   DMDACreate2d(PETSC_COMM_WORLD,bx,by,stype,M,N,PETSC_DECIDE,PETSC_DECIDE,1,1,NULL,NULL,&da);
 34:   DMSetFromOptions(da);
 35:   DMSetUp(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:     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]);
 49:     lv[e[3*i]] = i;
 50:   }
 51:   PetscSynchronizedFlush(PETSC_COMM_WORLD,stdout);
 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 ierr;
 68: }



 72: /*TEST

 74:    test:
 75:      requires: x

 77:    test:
 78:      suffix: 2
 79:      nsize: 2
 80:      requires: x

 82: TEST*/