Actual source code: ex10.c

petsc-3.3-p6 2013-02-11
  2: static char help[] = "Tests various 1-dimensional DM routines.\n\n";

  4: #include <petscdmda.h>

  8: int main(int argc,char **argv)
  9: {
 10:   PetscInt       M = 13,dof=1,s=1,wrap=0,i,n,j;
 12:   DM             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:   DMDACreate1d(PETSC_COMM_WORLD,(DMDABoundaryType)wrap,M,dof,s,PETSC_NULL,&da);
 34:   DMDASetUniformCoordinates(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:     DMDASetFieldName(da,i,fname);
 38:   }

 40:   DMView(da,viewer);
 41:   DMCreateGlobalVector(da,&global);
 42:   DMCreateLocalVector(da,&local);
 43:   DMCreateLocalVector(da,&locala);
 44:   DMDAGetCoordinates(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);

 59:   VecView(global,viewer);

 61:   /* Send ghost points to local vectors */
 62:   DMGlobalToLocalBegin(da,global,INSERT_VALUES,locala);
 63:   DMGlobalToLocalEnd(da,global,INSERT_VALUES,locala);

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