Actual source code: ex10.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 = 13,dof=1,s=1,wrap=0,i,n,j;
 12:   DA             da;
 13:   PetscViewer    viewer;
 14:   Vec            local,locala,global,coors;
 15:   PetscScalar    *x,*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,"-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:   DACreate1d(PETSC_COMM_WORLD,(DAPeriodicType)wrap,M,dof,s,PETSC_NULL,&da);
 34:   DASetUniformCoordinates(da,0.0,1.0,0.0,0.0,0.0,0.0);
 35:   for (i=0; i<dof; i++) {
 36:     sprintf(fname,"Field %d",(int)i);
 37:     DASetFieldName(da,i,fname);
 38:   }

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

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

 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: