xref: /petsc/src/dm/tutorials/ex1.c (revision b122ec5aa1bd4469eb4e0673542fb7de3f411254)
1 
2 static char help[] = "Tests VecView() contour plotting for 2d DMDAs.\n\n";
3 
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.
9 
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
12 
13      5  6   7   8   9
14      0  1   2   3   4
15 
16 Now the default split across 2 processors with the DM  is (by rank)
17 
18     0  0   0  1   1
19     0  0   0  1   1
20 
21 So the global PETSc ordering is
22 
23     3  4  5   8  9
24     0  1  2   6  7
25 
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 */
32 
33 #include <petscdm.h>
34 #include <petscdmda.h>
35 
36 int main(int argc,char **argv)
37 {
38   PetscMPIInt      rank;
39   PetscInt         M = 10,N = 8;
40   PetscBool        flg = PETSC_FALSE;
41   DM               da;
42   PetscViewer      viewer;
43   Vec              local,global;
44   PetscScalar      value;
45   DMBoundaryType   bx    = DM_BOUNDARY_NONE,by = DM_BOUNDARY_NONE;
46   DMDAStencilType  stype = DMDA_STENCIL_BOX;
47 #if defined(PETSC_HAVE_MATLAB_ENGINE)
48   PetscViewer      mviewer;
49   PetscMPIInt      size;
50 #endif
51 
52   CHKERRQ(PetscInitialize(&argc,&argv,(char*)0,help));
53   CHKERRQ(PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",300,0,300,300,&viewer));
54 #if defined(PETSC_HAVE_MATLAB_ENGINE)
55   CHKERRMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size));
56   if (size == 1) {
57     CHKERRQ(PetscViewerMatlabOpen(PETSC_COMM_WORLD,"tmp.mat",FILE_MODE_WRITE,&mviewer));
58   }
59 #endif
60 
61   CHKERRQ(PetscOptionsGetBool(NULL,NULL,"-star_stencil",&flg,NULL));
62   if (flg) stype = DMDA_STENCIL_STAR;
63 
64   /* Create distributed array and get vectors */
65   CHKERRQ(DMDACreate2d(PETSC_COMM_WORLD,bx,by,stype,M,N,PETSC_DECIDE,PETSC_DECIDE,1,1,NULL,NULL,&da));
66   CHKERRQ(DMSetFromOptions(da));
67   CHKERRQ(DMSetUp(da));
68   CHKERRQ(DMCreateGlobalVector(da,&global));
69   CHKERRQ(DMCreateLocalVector(da,&local));
70 
71   value = -3.0;
72   CHKERRQ(VecSet(global,value));
73   CHKERRQ(DMGlobalToLocalBegin(da,global,INSERT_VALUES,local));
74   CHKERRQ(DMGlobalToLocalEnd(da,global,INSERT_VALUES,local));
75 
76   CHKERRMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
77   value = rank+1;
78   CHKERRQ(VecScale(local,value));
79   CHKERRQ(DMLocalToGlobalBegin(da,local,ADD_VALUES,global));
80   CHKERRQ(DMLocalToGlobalEnd(da,local,ADD_VALUES,global));
81 
82   flg  = PETSC_FALSE;
83   CHKERRQ(PetscOptionsGetBool(NULL,NULL, "-view_global", &flg,NULL));
84   if (flg) { /* view global vector in natural ordering */
85     CHKERRQ(VecView(global,PETSC_VIEWER_STDOUT_WORLD));
86   }
87   CHKERRQ(DMView(da,viewer));
88   CHKERRQ(VecView(global,viewer));
89 #if defined(PETSC_HAVE_MATLAB_ENGINE)
90   if (size == 1) {
91     CHKERRQ(DMView(da,mviewer));
92     CHKERRQ(VecView(global,mviewer));
93   }
94 #endif
95 
96   /* Free memory */
97 #if defined(PETSC_HAVE_MATLAB_ENGINE)
98   if (size == 1) {
99     CHKERRQ(PetscViewerDestroy(&mviewer));
100   }
101 #endif
102   CHKERRQ(PetscViewerDestroy(&viewer));
103   CHKERRQ(VecDestroy(&local));
104   CHKERRQ(VecDestroy(&global));
105   CHKERRQ(DMDestroy(&da));
106   CHKERRQ(PetscFinalize());
107   return 0;
108 }
109 
110 /*TEST
111 
112    test:
113       requires: x
114       nsize: 2
115       args: -nox
116 
117 TEST*/
118