Actual source code: ex2.c

  2: static char help[] = "Tests various 1-dimensional DMDA routines.\n\n";

  4: #include <petscdmda.h>

  8: int main(int argc,char **argv)
  9: {
 10:   PetscMPIInt    rank;
 11:   PetscInt       M = 13,s=1,dof=1;
 12:   DMDABoundaryType bx = DMDA_BOUNDARY_PERIODIC;
 14:   DM             da;
 15:   PetscViewer    viewer;
 16:   Vec            local,global;
 17:   PetscScalar    value;
 18:   PetscDraw      draw;
 19:   PetscBool      flg = PETSC_FALSE;

 21:   PetscInitialize(&argc,&argv,(char*)0,help);

 23:   /* Create viewers */
 24:   PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",280,480,600,200,&viewer);
 25:   PetscViewerDrawGetDraw(viewer,0,&draw);
 26:   PetscDrawSetDoubleBuffer(draw);

 28:   /* Readoptions */
 29:   PetscOptionsGetInt(PETSC_NULL,"-M",&M,PETSC_NULL);
 30:   PetscOptionsGetEnum(PETSC_NULL,"-wrap",DMDABoundaryTypes,(PetscEnum*)&bx,PETSC_NULL);
 31:   PetscOptionsGetInt(PETSC_NULL,"-dof",&dof,PETSC_NULL);
 32:   PetscOptionsGetInt(PETSC_NULL,"-s",&s,PETSC_NULL);

 34:   /* Create distributed array and get vectors */
 35:   DMDACreate1d(PETSC_COMM_WORLD,bx,M,dof,s,PETSC_NULL,&da);
 36:   DMView(da,viewer);
 37:   DMCreateGlobalVector(da,&global);
 38:   DMCreateLocalVector(da,&local);

 40:   /* Set global vector; send ghost points to local vectors */
 41:   value = 1;
 42:   VecSet(global,value);
 43:   DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);
 44:   DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);

 46:   /* Scale local vectors according to processor rank; pass to global vector */
 47:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 48:   value = rank+1;
 49:   VecScale(local,value);
 50:   DMLocalToGlobalBegin(da,local,INSERT_VALUES,global);
 51:   DMLocalToGlobalEnd(da,local,INSERT_VALUES,global);

 53:   VecView(global,viewer);
 54:   PetscPrintf(PETSC_COMM_WORLD,"\nGlobal Vector:\n");
 55:   VecView(global,PETSC_VIEWER_STDOUT_WORLD);
 56:   PetscPrintf(PETSC_COMM_WORLD,"\n");

 58:   /* Send ghost points to local vectors */
 59:   DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);
 60:   DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);

 62:   PetscOptionsGetBool(PETSC_NULL,"-local_print",&flg,PETSC_NULL);
 63:   if (flg) {
 64:     PetscViewer            sviewer;
 65:     ISLocalToGlobalMapping is;

 67:     PetscSynchronizedPrintf(PETSC_COMM_WORLD,"\nLocal Vector: processor %d\n",rank);
 68:     PetscViewerGetSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);
 69:     VecView(local,sviewer);
 70:     PetscViewerRestoreSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);
 71:     PetscSynchronizedFlush(PETSC_COMM_WORLD);

 73:     PetscSynchronizedPrintf(PETSC_COMM_WORLD,"\nLocal to global mapping: processor %d\n",rank);
 74:     PetscViewerGetSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);
 75:     DMGetLocalToGlobalMapping(da,&is);
 76:     ISLocalToGlobalMappingView(is,sviewer);
 77:     PetscViewerRestoreSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);
 78:     PetscSynchronizedFlush(PETSC_COMM_WORLD);
 79:   }

 81:   /* Free memory */
 82:   PetscViewerDestroy(&viewer);
 83:   VecDestroy(&global);
 84:   VecDestroy(&local);
 85:   DMDestroy(&da);
 86:   PetscFinalize();
 87:   return 0;
 88: }
 89: