Actual source code: ex2.c

petsc-3.4.5 2014-06-29
  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:   PetscMPIInt      rank;
 12:   PetscInt         M  = 13,s=1,dof=1;
 13:   DMDABoundaryType bx = DMDA_BOUNDARY_PERIODIC;
 14:   PetscErrorCode   ierr;
 15:   DM               da;
 16:   PetscViewer      viewer;
 17:   Vec              local,global;
 18:   PetscScalar      value;
 19:   PetscDraw        draw;
 20:   PetscBool        flg = PETSC_FALSE;

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

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

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

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

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

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

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

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

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

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

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

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