Actual source code: ex10.c

petsc-3.6.1 2015-08-06
Report Typos and Errors
  2: static char help[] = "Tests various 1-dimensional DMDA routines.\n\n";

  4: #include <petscdmda.h>
  5: #include <petscdraw.h>

  9: int main(int argc,char **argv)
 10: {
 11:   PetscInt       M = 13,dof=1,s=1,wrap=0,i,n,j;
 13:   DM             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(NULL,"-M",&M,NULL);
 29:   PetscOptionsGetInt(NULL,"-dof",&dof,NULL);
 30:   PetscOptionsGetInt(NULL,"-s",&s,NULL);
 31:   PetscOptionsGetInt(NULL,"-periodic",&wrap,NULL);

 33:   /* Create distributed array and get vectors */
 34:   DMDACreate1d(PETSC_COMM_WORLD,(DMBoundaryType)wrap,M,dof,s,NULL,&da);
 35:   DMDASetUniformCoordinates(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",(int)i);
 38:     DMDASetFieldName(da,i,fname);
 39:   }

 41:   DMView(da,viewer);
 42:   DMCreateGlobalVector(da,&global);
 43:   DMCreateLocalVector(da,&local);
 44:   DMCreateLocalVector(da,&locala);
 45:   DMGetCoordinates(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:   DMGlobalToLocalBegin(da,global,INSERT_VALUES,locala);
 64:   DMGlobalToLocalEnd(da,global,INSERT_VALUES,locala);

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