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.\n\n";

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

 10: int main(int argc,char **argv)
 11: {
 12:   int         M = 5,N = 4,ierr,dof=1,s=1,wrap=0,i,n,j,k,m,cnt;
 13:   DA          da;
 14:   PetscViewer viewer;
 15:   Vec         local,locala,global,coors;
 16:   PetscScalar *xy,*alocal;
 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(PETSC_NULL,"-M",&M,PETSC_NULL);
 29:   PetscOptionsGetInt(PETSC_NULL,"-N",&N,PETSC_NULL);
 30:   PetscOptionsGetInt(PETSC_NULL,"-dof",&dof,PETSC_NULL);
 31:   PetscOptionsGetInt(PETSC_NULL,"-s",&s,PETSC_NULL);
 32:   PetscOptionsGetInt(PETSC_NULL,"-periodic",&wrap,PETSC_NULL);

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

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

 50: VecView(coors,PETSC_VIEWER_STDOUT_SELF);

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

 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: