Actual source code: ex1.c
2: static char help[] = "Tests VecView() contour plotting for 2d DAs.\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 DA vectors first puts the Vec elements into global natural ordering before printing (or plotting)
11: them. In 2d 5 by 2 DA 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 DA 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 petscda.h
37: int main(int argc,char **argv)
38: {
39: PetscMPIInt rank;
40: PetscInt M = -10,N = -8;
42: PetscTruth flg = PETSC_FALSE;
43: DA da;
44: PetscViewer viewer;
45: Vec local,global;
46: PetscScalar value;
47: DAPeriodicType ptype = DA_NONPERIODIC;
48: DAStencilType stype = DA_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: PetscOptionsGetTruth(PETSC_NULL,"-star_stencil",&flg,PETSC_NULL);
60: if (flg) stype = DA_STENCIL_STAR;
61:
62: /* Create distributed array and get vectors */
63: DACreate2d(PETSC_COMM_WORLD,ptype,stype,M,N,PETSC_DECIDE,PETSC_DECIDE,1,1,PETSC_NULL,PETSC_NULL,&da);
64: DACreateGlobalVector(da,&global);
65: DACreateLocalVector(da,&local);
67: value = -3.0;
68: VecSet(global,value);
69: DAGlobalToLocalBegin(da,global,INSERT_VALUES,local);
70: DAGlobalToLocalEnd(da,global,INSERT_VALUES,local);
72: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
73: value = rank+1;
74: VecScale(local,value);
75: DALocalToGlobal(da,local,ADD_VALUES,global);
76:
77: flg = PETSC_FALSE;
78: PetscOptionsGetTruth(PETSC_NULL, "-view_global", &flg,PETSC_NULL);
79: if (flg) { /* view global vector in natural ordering */
80: VecView(global,PETSC_VIEWER_STDOUT_WORLD);
81: }
82: DAView(da,viewer);
83: VecView(global,viewer);
84: #if defined(PETSC_HAVE_MATLAB_ENGINE)
85: DAView(da,mviewer);
86: VecView(global,mviewer);
87: #endif
89: /* Free memory */
90: #if defined(PETSC_HAVE_MATLAB_ENGINE)
91: PetscViewerDestroy(mviewer);
92: #endif
93: PetscViewerDestroy(viewer);
94: VecDestroy(local);
95: VecDestroy(global);
96: DADestroy(da);
97: PetscFinalize();
98: return 0;
99: }
100: