Actual source code: ex11.c

  1: /*$Id: ex11.c,v 1.19 2001/08/07 03:04:42 balay Exp $*/

  3: static char help[] = "Tests various 1-dimensional DA routines.nn";

 5:  #include petscda.h
 6:  #include petscsys.h

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

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

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

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

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

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

 48: VecView(coors,PETSC_VIEWER_STDOUT_SELF);

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

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

 68:   VecView(global,viewer);

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

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