Actual source code: ex11.c

  2: static char help[] = "Tests various 1-dimensional DA routines.\n\n";

 4:  #include petscda.h

  8: int main(int argc,char **argv)
  9: {
 10:   PetscInt       M = 5,N = 4,dof=1,s=1,wrap=0,i,n,j,k,m,cnt;
 12:   DA             da;
 13:   PetscViewer    viewer;
 14:   Vec            local,locala,global,coors;
 15:   PetscScalar    *xy,*alocal;
 16:   PetscDraw      draw;
 17:   char           fname[16];

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

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

 26:   /* Read options */
 27:   PetscOptionsGetInt(PETSC_NULL,"-M",&M,PETSC_NULL);
 28:   PetscOptionsGetInt(PETSC_NULL,"-N",&N,PETSC_NULL);
 29:   PetscOptionsGetInt(PETSC_NULL,"-dof",&dof,PETSC_NULL);
 30:   PetscOptionsGetInt(PETSC_NULL,"-s",&s,PETSC_NULL);
 31:   PetscOptionsGetInt(PETSC_NULL,"-periodic",&wrap,PETSC_NULL);

 33:   /* Create distributed array and get vectors */
 34:   DACreate2d(PETSC_COMM_WORLD,(DAPeriodicType)wrap,DA_STENCIL_BOX,M,N,PETSC_DECIDE,
 35:                     PETSC_DECIDE,dof,s,PETSC_NULL,PETSC_NULL,&da);
 36:   DASetUniformCoordinates(da,0.0,1.0,0.0,1.0,0.0,0.0);
 37:   for (i=0; i<dof; i++) {
 38:     sprintf(fname,"Field %d",(int)i);
 39:     DASetFieldName(da,i,fname);
 40:   }

 42:   DAView(da,viewer);
 43:   DACreateGlobalVector(da,&global);
 44:   DACreateLocalVector(da,&local);
 45:   DACreateLocalVector(da,&locala);
 46:   DAGetCoordinates(da,&coors);
 47:   VecGetArray(coors,&xy);

 49: VecView(coors,PETSC_VIEWER_STDOUT_SELF);

 51:   /* Set values into local vectors */
 52:   VecGetArray(local,&alocal);
 53:   DAGetGhostCorners(da,0,0,0,&m,&n,0);
 54:   n    = n/dof;
 55:   for (k=0; k<dof; k++) {
 56:     cnt = 0;
 57:     for (j=0; j<n; j++) {
 58:       for (i=0; i<m; i++) {
 59:         alocal[k+dof*cnt] = PetscSinScalar(2.0*PETSC_PI*(k+1)*xy[2*cnt]);
 60:         cnt++;
 61:       }
 62:     }
 63:   }
 64:   VecRestoreArray(local,&alocal);
 65:   VecRestoreArray(coors,&xy);
 66:   VecDestroy(coords);

 68:   DALocalToGlobal(da,local,INSERT_VALUES,global);

 70:   VecView(global,viewer);

 72:   /* Send ghost points to local vectors */
 73:   DAGlobalToLocalBegin(da,global,INSERT_VALUES,locala);
 74:   DAGlobalToLocalEnd(da,global,INSERT_VALUES,locala);

 76:   /* Free memory */
 77:   PetscViewerDestroy(viewer);
 78:   VecDestroy(global);
 79:   VecDestroy(local);
 80:   VecDestroy(locala);
 81:   DADestroy(da);
 82:   PetscFinalize();
 83:   return 0;
 84: }
 85: