Actual source code: ex1.c

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

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

  9: int main(int argc,char **argv)
 10: {
 11:   PetscMPIInt    rank;
 12:   PetscInt       M = 10,N = 8,m = PETSC_DECIDE,n = PETSC_DECIDE;
 14:   DM             da;
 15:   PetscViewer    viewer;
 16:   Vec            local,global;
 17:   PetscScalar    value;

 19:   PetscInitialize(&argc,&argv,(char*)0,help);
 20:   PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",300,0,300,300,&viewer);

 22:   /* Read options */
 23:   PetscOptionsGetInt(NULL,"-M",&M,NULL);
 24:   PetscOptionsGetInt(NULL,"-N",&N,NULL);
 25:   PetscOptionsGetInt(NULL,"-m",&m,NULL);
 26:   PetscOptionsGetInt(NULL,"-n",&n,NULL);

 28:   /* Create distributed array and get vectors */
 29:   DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_BOX,
 30:                       M,N,m,n,1,1,NULL,NULL,&da);
 31:   DMCreateGlobalVector(da,&global);
 32:   DMCreateLocalVector(da,&local);

 34:   value = -3.0;
 35:   VecSet(global,value);
 36:   DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);
 37:   DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);

 39:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 40:   value = rank+1;
 41:   VecScale(local,value);
 42:   DMLocalToGlobalBegin(da,local,ADD_VALUES,global);
 43:   DMLocalToGlobalEnd(da,local,ADD_VALUES,global);

 45:   VecView(global,PETSC_VIEWER_STDOUT_WORLD);
 46:   DMView(da,viewer);

 48:   /* Free memory */
 49:   PetscViewerDestroy(&viewer);
 50:   VecDestroy(&local);
 51:   VecDestroy(&global);
 52:   DMDestroy(&da);
 53:   PetscFinalize();
 54:   return 0;
 55: }