Actual source code: ex2.c

petsc-3.7.3 2016-08-01
Report Typos and Errors
  2: static char help[] = "Tests various 1-dimensional DMDA routines.\n\n";

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

 10: int main(int argc,char **argv)
 11: {
 12:   PetscMPIInt            rank;
 13:   PetscInt               M  = 13,s=1,dof=1;
 14:   DMBoundaryType         bx = DM_BOUNDARY_PERIODIC;
 15:   PetscErrorCode         ierr;
 16:   DM                     da;
 17:   PetscViewer            viewer;
 18:   Vec                    local,global;
 19:   PetscScalar            value;
 20:   PetscDraw              draw;
 21:   PetscBool              flg = PETSC_FALSE;
 22:   ISLocalToGlobalMapping is;

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

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

 31:   /* Readoptions */
 32:   PetscOptionsGetInt(NULL,NULL,"-M",&M,NULL);
 33:   PetscOptionsGetEnum(NULL,NULL,"-wrap",DMBoundaryTypes,(PetscEnum*)&bx,NULL);
 34:   PetscOptionsGetInt(NULL,NULL,"-dof",&dof,NULL);
 35:   PetscOptionsGetInt(NULL,NULL,"-s",&s,NULL);

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

 43:   value = 1;
 44:   VecSet(global,value);

 46:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 47:   value = rank+1;
 48:   VecScale(global,value);

 50:   VecView(global,viewer);
 51:   PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD,"\nGlobal Vector:\n");
 52:   VecView(global,PETSC_VIEWER_STDOUT_WORLD);
 53:   PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD,"\n");

 55:   /* Send ghost points to local vectors */
 56:   DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);
 57:   DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);

 59:   PetscOptionsGetBool(NULL,NULL,"-local_print",&flg,NULL);
 60:   if (flg) {
 61:     PetscViewer            sviewer;

 63:     PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD);
 64:     PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"\nLocal Vector: processor %d\n",rank);
 65:     PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer);
 66:     VecView(local,sviewer);
 67:     PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer);
 68:     PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD);
 69:     PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD);
 70:   }
 71:   PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD,"\nLocal to global mapping\n");
 72:   DMGetLocalToGlobalMapping(da,&is);
 73:   ISLocalToGlobalMappingView(is,PETSC_VIEWER_STDOUT_WORLD);

 75:   /* Free memory */
 76:   PetscViewerDestroy(&viewer);
 77:   VecDestroy(&global);
 78:   VecDestroy(&local);
 79:   DMDestroy(&da);
 80:   PetscFinalize();
 81:   return 0;
 82: }