Actual source code: ex1.c

petsc-3.8.4 2018-03-24
Report Typos and Errors

  2: static char help[] = "Tests various DM routines.\n\n";

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

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

 17:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 18:   PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",300,0,300,300,&viewer);

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

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

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

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

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

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