Actual source code: ex11.c

petsc-3.6.4 2016-04-12
Report Typos and Errors
  2: static char help[] = "Tests various 2-dimensional DMDA routines.\n\n";

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

  9: int main(int argc,char **argv)
 10: {
 11:   PetscInt       M = 10,N = 8,dof=1,s=1,bx=0,by=0,i,n,j,k,m,wrap,xs,ys;
 13:   DM             da,dac;
 14:   PetscViewer    viewer;
 15:   Vec            local,global,coors;
 16:   PetscScalar    ***xy,***aglobal;
 17:   PetscDraw      draw;
 18:   char           fname[16];

 20:   PetscInitialize(&argc,&argv,(char*)0,help);

 22:   /* Create viewers */
 23:   PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",PETSC_DECIDE,PETSC_DECIDE,600,200,&viewer);
 24:   PetscViewerDrawGetDraw(viewer,0,&draw);
 25:   PetscDrawSetDoubleBuffer(draw);

 27:   /* Read options */
 28:   PetscOptionsGetInt(NULL,"-M",&M,NULL);
 29:   PetscOptionsGetInt(NULL,"-N",&N,NULL);
 30:   PetscOptionsGetInt(NULL,"-dof",&dof,NULL);
 31:   PetscOptionsGetInt(NULL,"-s",&s,NULL);
 32:   PetscOptionsGetInt(NULL,"-periodic_x",&wrap,NULL);
 33:   PetscOptionsGetInt(NULL,"-periodic_y",&wrap,NULL);

 35:   /* Create distributed array and get vectors */
 36:   DMDACreate2d(PETSC_COMM_WORLD,(DMBoundaryType)bx,(DMBoundaryType)by,DMDA_STENCIL_BOX,M,N,PETSC_DECIDE,
 37:                       PETSC_DECIDE,dof,s,NULL,NULL,&da);
 38:   DMDASetUniformCoordinates(da,0.0,1.0,0.0,1.0,0.0,0.0);
 39:   for (i=0; i<dof; i++) {
 40:     sprintf(fname,"Field %d",(int)i);
 41:     DMDASetFieldName(da,i,fname);
 42:   }

 44:   DMView(da,viewer);
 45:   DMCreateGlobalVector(da,&global);
 46:   DMCreateLocalVector(da,&local);
 47:   DMGetCoordinates(da,&coors);
 48:   DMGetCoordinateDM(da,&dac);

 50:   /* Set values into global vectors */
 51:   DMDAVecGetArrayDOFRead(dac,coors,&xy);
 52:   DMDAVecGetArrayDOF(da,global,&aglobal);
 53:   DMDAGetCorners(da,&xs,&ys,0,&m,&n,0);
 54:   for (k=0; k<dof; k++) {
 55:     for (j=ys; j<ys+n; j++) {
 56:       for (i=xs; i<xs+m; i++) {
 57:         aglobal[j][i][k] = PetscSinScalar(2.0*PETSC_PI*(k+1)*xy[j][i][0]);
 58:       }
 59:     }
 60:   }
 61:   DMDAVecRestoreArrayDOF(da,global,&aglobal);
 62:   DMDAVecRestoreArrayDOFRead(dac,coors,&xy);
 63:   DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);
 64:   DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);

 66:   VecSet(global,0.0);
 67:   DMLocalToGlobalBegin(da,local,INSERT_VALUES,global);
 68:   DMLocalToGlobalEnd(da,local,INSERT_VALUES,global);
 69:   VecView(global,PETSC_VIEWER_STDOUT_WORLD);
 70:   VecView(global,viewer);

 72:   /* Free memory */
 73:   PetscViewerDestroy(&viewer);
 74:   VecDestroy(&global);
 75:   VecDestroy(&local);
 76:   DMDestroy(&da);
 77:   PetscFinalize();
 78:   return 0;
 79: }