Actual source code: ex2.c

petsc-3.9.4 2018-09-11
Report Typos and Errors

  2: static char help[] = "Tests DMDAGlobalToNaturalAllCreate() using contour plotting for 2d DMDAs.\n\n";


  5:  #include <petscdm.h>
  6:  #include <petscdmda.h>
  7:  #include <petscdraw.h>

  9: int main(int argc,char **argv)
 10: {
 11:   PetscInt         i,j,M = 10,N = 8,m = PETSC_DECIDE,n = PETSC_DECIDE;
 12:   PetscMPIInt      rank;
 13:   PetscErrorCode   ierr;
 14:   PetscBool        flg = PETSC_FALSE;
 15:   DM               da;
 16:   PetscViewer      viewer;
 17:   Vec              localall,global;
 18:   PetscScalar      value,*vlocal;
 19:   DMBoundaryType   bx    = DM_BOUNDARY_NONE,by = DM_BOUNDARY_NONE;
 20:   DMDAStencilType  stype = DMDA_STENCIL_BOX;
 21:   VecScatter       tolocalall,fromlocalall;
 22:   PetscInt         start,end;

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

 27:   /* Read options */
 28:   PetscOptionsGetInt(NULL,NULL,"-M",&M,NULL);
 29:   PetscOptionsGetInt(NULL,NULL,"-N",&N,NULL);
 30:   PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
 31:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 32:   PetscOptionsGetBool(NULL,NULL,"-star_stencil",&flg,NULL);
 33:   if (flg) stype = DMDA_STENCIL_STAR;

 35:   /* Create distributed array and get vectors */
 36:   DMDACreate2d(PETSC_COMM_WORLD,bx,by,stype,M,N,m,n,1,1,NULL,NULL,&da);
 37:   DMSetFromOptions(da);
 38:   DMSetUp(da);
 39: 
 40:   DMCreateGlobalVector(da,&global);
 41:   VecCreateSeq(PETSC_COMM_SELF,M*N,&localall);

 43:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 44:   VecGetOwnershipRange(global,&start,&end);
 45:   for (i=start; i<end; i++) {
 46:     value = 5.0*rank;
 47:     VecSetValues(global,1,&i,&value,INSERT_VALUES);
 48:   }
 49:   VecView(global,viewer);

 51:   /*
 52:      Create Scatter from global DMDA parallel vector to local vector that
 53:    contains all entries
 54:   */
 55:   DMDAGlobalToNaturalAllCreate(da,&tolocalall);
 56:   DMDANaturalAllToGlobalCreate(da,&fromlocalall);

 58:   VecScatterBegin(tolocalall,global,localall,INSERT_VALUES,SCATTER_FORWARD);
 59:   VecScatterEnd(tolocalall,global,localall,INSERT_VALUES,SCATTER_FORWARD);

 61:   VecGetArray(localall,&vlocal);
 62:   for (j=0; j<N; j++) {
 63:     for (i=0; i<M; i++) {
 64:       *vlocal++ += i + j*M;
 65:     }
 66:   }
 67:   VecRestoreArray(localall,&vlocal);

 69:   /* scatter back to global vector */
 70:   VecScatterBegin(fromlocalall,localall,global,INSERT_VALUES,SCATTER_FORWARD);
 71:   VecScatterEnd(fromlocalall,localall,global,INSERT_VALUES,SCATTER_FORWARD);

 73:   VecView(global,viewer);

 75:   /* Free memory */
 76:   VecScatterDestroy(&tolocalall);
 77:   VecScatterDestroy(&fromlocalall);
 78:   PetscViewerDestroy(&viewer);
 79:   VecDestroy(&localall);
 80:   VecDestroy(&global);
 81:   DMDestroy(&da);
 82:   PetscFinalize();
 83:   return ierr;
 84: }



 88: /*TEST

 90:    build:
 91:      requires: !complex

 93:    test:
 94:       nsize: 3
 95:       TODO: Need to develop comparison test

 97: TEST*/