Actual source code: ex1.c

      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>

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

     53:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
     54:   PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",300,0,300,300,&viewer);
     55: #if defined(PETSC_HAVE_MATLAB_ENGINE)
     56:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
     57:   if (size == 1) {
     58:     PetscViewerMatlabOpen(PETSC_COMM_WORLD,"tmp.mat",FILE_MODE_WRITE,&mviewer);
     59:   }
     60: #endif

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

     65:   /* Create distributed array and get vectors */
     66:   DMDACreate2d(PETSC_COMM_WORLD,bx,by,stype,M,N,PETSC_DECIDE,PETSC_DECIDE,1,1,NULL,NULL,&da);
     67:   DMSetFromOptions(da);
     68:   DMSetUp(da);
     69:   DMCreateGlobalVector(da,&global);
     70:   DMCreateLocalVector(da,&local);

     72:   value = -3.0;
     73:   VecSet(global,value);
     74:   DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);
     75:   DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);

     77:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
     78:   value = rank+1;
     79:   VecScale(local,value);
     80:   DMLocalToGlobalBegin(da,local,ADD_VALUES,global);
     81:   DMLocalToGlobalEnd(da,local,ADD_VALUES,global);

     83:   flg  = PETSC_FALSE;
     84:   PetscOptionsGetBool(NULL,NULL, "-view_global", &flg,NULL);
     85:   if (flg) { /* view global vector in natural ordering */
     86:     VecView(global,PETSC_VIEWER_STDOUT_WORLD);
     87:   }
     88:   DMView(da,viewer);
     89:   VecView(global,viewer);
     90: #if defined(PETSC_HAVE_MATLAB_ENGINE)
     91:   if (size == 1) {
     92:     DMView(da,mviewer);
     93:     VecView(global,mviewer);
     94:   }
     95: #endif

     97:   /* Free memory */
     98: #if defined(PETSC_HAVE_MATLAB_ENGINE)
     99:   if (size == 1) {
    100:     PetscViewerDestroy(&mviewer);
    101:   }
    102: #endif
    103:   PetscViewerDestroy(&viewer);
    104:   VecDestroy(&local);
    105:   VecDestroy(&global);
    106:   DMDestroy(&da);
    107:   PetscFinalize();
    108:   return ierr;
    109: }

    113: /*TEST

    115:    test:
    116:       requires: x
    117:       nsize: 2
    118:       args: -nox

    120: TEST*/