Actual source code: ex2.c

petsc-3.6.1 2015-08-06
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,"-M",&M,NULL);
 33:   PetscOptionsGetEnum(NULL,"-wrap",DMBoundaryTypes,(PetscEnum*)&bx,NULL);
 34:   PetscOptionsGetInt(NULL,"-dof",&dof,NULL);
 35:   PetscOptionsGetInt(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:   PetscPrintf(PETSC_COMM_WORLD,"\nGlobal Vector:\n");
 52:   VecView(global,PETSC_VIEWER_STDOUT_WORLD);
 53:   PetscPrintf(PETSC_COMM_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,"-local_print",&flg,NULL);
 60:   if (flg) {
 61:     PetscViewer            sviewer;

 63:     PetscViewerASCIISynchronizedAllow(PETSC_VIEWER_STDOUT_WORLD,PETSC_TRUE);
 64:     PetscSynchronizedPrintf(PETSC_COMM_WORLD,"\nLocal Vector: processor %d\n",rank);
 65:     PetscViewerGetSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);
 66:     VecView(local,sviewer);
 67:     PetscViewerRestoreSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);
 68:     PetscSynchronizedFlush(PETSC_COMM_WORLD,PETSC_STDOUT);
 69:   }
 70:   PetscPrintf(PETSC_COMM_WORLD,"\nLocal to global mapping\n");
 71:   DMGetLocalToGlobalMapping(da,&is);
 72:   ISLocalToGlobalMappingView(is,PETSC_VIEWER_STDOUT_WORLD);

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