Actual source code: ex10.c

  1: /*$Id: ex10.c,v 1.20 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 = 13,ierr,dof=1,s=1,wrap=0,i,n,j;
 13:   DA          da;
 14:   PetscViewer viewer;
 15:   Vec         local,locala,global,coors;
 16:   PetscScalar *x,*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,"-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:   DACreate1d(PETSC_COMM_WORLD,(DAPeriodicType)wrap,M,dof,s,PETSC_NULL,&da);
 35:   DASetUniformCoordinates(da,0.0,1.0,0.0,0.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,&x);

 48:   /* Set values into global vectors */
 49:   VecGetArray(global,&alocal);
 50:   VecGetLocalSize(global,&n);
 51:   n    = n/dof;
 52:   for (j=0; j<dof; j++) {
 53:     for (i=0; i<n; i++) {
 54:       alocal[j+dof*i] = PetscSinScalar(2*PETSC_PI*(j+1)*x[i]);
 55:     }
 56:   }
 57:   VecRestoreArray(global,&alocal);
 58:   VecRestoreArray(coors,&x);

 60:   VecView(global,viewer);

 62:   /* Send ghost points to local vectors */
 63:   DAGlobalToLocalBegin(da,global,INSERT_VALUES,locala);
 64:   DAGlobalToLocalEnd(da,global,INSERT_VALUES,locala);

 66:   /* Free memory */
 67:   PetscViewerDestroy(viewer);
 68:   VecDestroy(global);
 69:   VecDestroy(local);
 70:   VecDestroy(locala);
 71:   DADestroy(da);
 72:   PetscFinalize();
 73:   return 0;
 74: }
 75: