Actual source code: ex1.c

petsc-3.5.4 2015-05-23
Report Typos and Errors
  2: static char help[] = "Tests VecView() contour plotting for 2d DMDAs.\n\n";

  4: /*
  5:   MATLAB must be installed to configure PETSc to have MATLAB engine.
  6: Unless you have specific important reasons for using the MATLAB engine, we do not
  7: recommend it. If you want to use MATLAB for visualization and maybe a little post processing
  8: then you can use the socket viewer and send the data to MATLAB via that.

 10:   VecView() on DMDA vectors first puts the Vec elements into global natural ordering before printing (or plotting)
 11: them. In 2d 5 by 2 DMDA this means the numbering is

 13:      5  6   7   8   9
 14:      0  1   2   3   4

 16: Now the default split across 2 processors with the DM  is (by rank)

 18:     0  0   0  1   1
 19:     0  0   0  1   1

 21: So the global PETSc ordering is

 23:     3  4  5   8  9
 24:     0  1  2   6  7

 26: Use the options
 27:      -da_grid_x <nx> - number of grid points in x direction, if M < 0
 28:      -da_grid_y <ny> - number of grid points in y direction, if N < 0
 29:      -da_processors_x <MX> number of processors in x directio
 30:      -da_processors_y <MY> number of processors in x direction
 31: */

 33: #include <petscdm.h>
 34: #include <petscdmda.h>

 38: int main(int argc,char **argv)
 39: {
 40:   PetscMPIInt      rank;
 41:   PetscInt         M = -10,N = -8;
 42:   PetscErrorCode   ierr;
 43:   PetscBool        flg = PETSC_FALSE;
 44:   DM               da;
 45:   PetscViewer      viewer;
 46:   Vec              local,global;
 47:   PetscScalar      value;
 48:   DMBoundaryType   bx    = DM_BOUNDARY_NONE,by = DM_BOUNDARY_NONE;
 49:   DMDAStencilType  stype = DMDA_STENCIL_BOX;
 50: #if defined(PETSC_HAVE_MATLAB_ENGINE)
 51:   PetscViewer mviewer;
 52: #endif

 54:   PetscInitialize(&argc,&argv,(char*)0,help);
 55:   PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",300,0,300,300,&viewer);
 56: #if defined(PETSC_HAVE_MATLAB_ENGINE)
 57:   PetscViewerMatlabOpen(PETSC_COMM_WORLD,"tmp.mat",FILE_MODE_WRITE,&mviewer);
 58: #endif

 60:   PetscOptionsGetBool(NULL,"-star_stencil",&flg,NULL);
 61:   if (flg) stype = DMDA_STENCIL_STAR;

 63:   /* Create distributed array and get vectors */
 64:   DMDACreate2d(PETSC_COMM_WORLD,bx,by,stype,M,N,PETSC_DECIDE,PETSC_DECIDE,1,1,NULL,NULL,&da);
 65:   DMCreateGlobalVector(da,&global);
 66:   DMCreateLocalVector(da,&local);

 68:   value = -3.0;
 69:   VecSet(global,value);
 70:   DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);
 71:   DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);

 73:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 74:   value = rank+1;
 75:   VecScale(local,value);
 76:   DMLocalToGlobalBegin(da,local,ADD_VALUES,global);
 77:   DMLocalToGlobalEnd(da,local,ADD_VALUES,global);

 79:   flg  = PETSC_FALSE;
 80:   PetscOptionsGetBool(NULL, "-view_global", &flg,NULL);
 81:   if (flg) { /* view global vector in natural ordering */
 82:     VecView(global,PETSC_VIEWER_STDOUT_WORLD);
 83:   }
 84:   DMView(da,viewer);
 85:   VecView(global,viewer);
 86: #if defined(PETSC_HAVE_MATLAB_ENGINE)
 87:   DMView(da,mviewer);
 88:   VecView(global,mviewer);
 89: #endif

 91:   /* Free memory */
 92: #if defined(PETSC_HAVE_MATLAB_ENGINE)
 93:   PetscViewerDestroy(&mviewer);
 94: #endif
 95:   PetscViewerDestroy(&viewer);
 96:   VecDestroy(&local);
 97:   VecDestroy(&global);
 98:   DMDestroy(&da);
 99:   PetscFinalize();
100:   return 0;
101: }