xref: /petsc/src/dm/tutorials/ex1.c (revision 030f984af8d8bb4c203755d35bded3c05b3d83ce)
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   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
52 
53   ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
54   ierr = PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",300,0,300,300,&viewer);CHKERRQ(ierr);
55 #if defined(PETSC_HAVE_MATLAB_ENGINE)
56   ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRMPI(ierr);
57   if (size == 1) {
58     ierr = PetscViewerMatlabOpen(PETSC_COMM_WORLD,"tmp.mat",FILE_MODE_WRITE,&mviewer);CHKERRQ(ierr);
59   }
60 #endif
61 
62   ierr = PetscOptionsGetBool(NULL,NULL,"-star_stencil",&flg,NULL);CHKERRQ(ierr);
63   if (flg) stype = DMDA_STENCIL_STAR;
64 
65   /* Create distributed array and get vectors */
66   ierr = DMDACreate2d(PETSC_COMM_WORLD,bx,by,stype,M,N,PETSC_DECIDE,PETSC_DECIDE,1,1,NULL,NULL,&da);CHKERRQ(ierr);
67   ierr = DMSetFromOptions(da);CHKERRQ(ierr);
68   ierr = DMSetUp(da);CHKERRQ(ierr);
69   ierr = DMCreateGlobalVector(da,&global);CHKERRQ(ierr);
70   ierr = DMCreateLocalVector(da,&local);CHKERRQ(ierr);
71 
72   value = -3.0;
73   ierr  = VecSet(global,value);CHKERRQ(ierr);
74   ierr  = DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);CHKERRQ(ierr);
75   ierr  = DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);CHKERRQ(ierr);
76 
77   ierr  = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRMPI(ierr);
78   value = rank+1;
79   ierr  = VecScale(local,value);CHKERRQ(ierr);
80   ierr  = DMLocalToGlobalBegin(da,local,ADD_VALUES,global);CHKERRQ(ierr);
81   ierr  = DMLocalToGlobalEnd(da,local,ADD_VALUES,global);CHKERRQ(ierr);
82 
83   flg  = PETSC_FALSE;
84   ierr = PetscOptionsGetBool(NULL,NULL, "-view_global", &flg,NULL);CHKERRQ(ierr);
85   if (flg) { /* view global vector in natural ordering */
86     ierr = VecView(global,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
87   }
88   ierr = DMView(da,viewer);CHKERRQ(ierr);
89   ierr = VecView(global,viewer);CHKERRQ(ierr);
90 #if defined(PETSC_HAVE_MATLAB_ENGINE)
91   if (size == 1) {
92     ierr = DMView(da,mviewer);CHKERRQ(ierr);
93     ierr = VecView(global,mviewer);CHKERRQ(ierr);
94   }
95 #endif
96 
97   /* Free memory */
98 #if defined(PETSC_HAVE_MATLAB_ENGINE)
99   if (size == 1) {
100     ierr = PetscViewerDestroy(&mviewer);CHKERRQ(ierr);
101   }
102 #endif
103   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
104   ierr = VecDestroy(&local);CHKERRQ(ierr);
105   ierr = VecDestroy(&global);CHKERRQ(ierr);
106   ierr = DMDestroy(&da);CHKERRQ(ierr);
107   ierr = PetscFinalize();
108   return ierr;
109 }
110 
111 /*TEST
112 
113    test:
114       requires: x
115       nsize: 2
116       args: -nox
117 
118 TEST*/
119