Actual source code: ex1.c
petsc-3.4.2 2013-07-02

      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 <petscdmda.h>

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

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

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

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

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

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

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

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