Actual source code: ex11.c
petsc-3.9.4 2018-09-11
2: static char help[] = "Tests various 2-dimensional DMDA routines.\n\n";
4: #include <petscdmda.h>
5: #include <petscdraw.h>
7: int main(int argc,char **argv)
8: {
9: PetscInt M = 10,N = 8,dof=1,s=1,bx=0,by=0,i,n,j,k,m,wrap,xs,ys;
11: DM da,dac;
12: PetscViewer viewer;
13: Vec local,global,coors;
14: PetscScalar ***xy,***aglobal;
15: PetscDraw draw;
16: char fname[32];
18: PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
19: /* Create viewers */
20: PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",PETSC_DECIDE,PETSC_DECIDE,600,200,&viewer);
21: PetscViewerDrawGetDraw(viewer,0,&draw);
22: PetscDrawSetDoubleBuffer(draw);
24: /* Read options */
25: PetscOptionsGetInt(NULL,NULL,"-M",&M,NULL);
26: PetscOptionsGetInt(NULL,NULL,"-N",&N,NULL);
27: PetscOptionsGetInt(NULL,NULL,"-dof",&dof,NULL);
28: PetscOptionsGetInt(NULL,NULL,"-s",&s,NULL);
29: PetscOptionsGetInt(NULL,NULL,"-periodic_x",&wrap,NULL);
30: PetscOptionsGetInt(NULL,NULL,"-periodic_y",&wrap,NULL);
32: /* Create distributed array and get vectors */
33: DMDACreate2d(PETSC_COMM_WORLD,(DMBoundaryType)bx,(DMBoundaryType)by,DMDA_STENCIL_BOX,M,N,PETSC_DECIDE,PETSC_DECIDE,dof,s,NULL,NULL,&da);
34: DMSetFromOptions(da);
35: DMSetUp(da);
36: DMDASetUniformCoordinates(da,0.0,1.0,0.0,1.0,0.0,0.0);
37: for (i=0; i<dof; i++) {
38: sprintf(fname,"Field %d",(int)i);
39: DMDASetFieldName(da,i,fname);
40: }
42: DMView(da,viewer);
43: DMCreateGlobalVector(da,&global);
44: DMCreateLocalVector(da,&local);
45: DMGetCoordinates(da,&coors);
46: DMGetCoordinateDM(da,&dac);
48: /* Set values into global vectors */
49: DMDAVecGetArrayDOFRead(dac,coors,&xy);
50: DMDAVecGetArrayDOF(da,global,&aglobal);
51: DMDAGetCorners(da,&xs,&ys,0,&m,&n,0);
52: for (k=0; k<dof; k++) {
53: for (j=ys; j<ys+n; j++) {
54: for (i=xs; i<xs+m; i++) {
55: aglobal[j][i][k] = PetscSinScalar(2.0*PETSC_PI*(k+1)*xy[j][i][0]);
56: }
57: }
58: }
59: DMDAVecRestoreArrayDOF(da,global,&aglobal);
60: DMDAVecRestoreArrayDOFRead(dac,coors,&xy);
61: DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);
62: DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);
64: VecSet(global,0.0);
65: DMLocalToGlobalBegin(da,local,INSERT_VALUES,global);
66: DMLocalToGlobalEnd(da,local,INSERT_VALUES,global);
67: VecView(global,PETSC_VIEWER_STDOUT_WORLD);
68: VecView(global,viewer);
70: /* Free memory */
71: PetscViewerDestroy(&viewer);
72: VecDestroy(&global);
73: VecDestroy(&local);
74: DMDestroy(&da);
75: PetscFinalize();
76: return ierr;
77: }
79: /*TEST
81: test:
82: args: -dof 2
83: filter: grep -v -i Object
84: requires: x
86: test:
87: suffix: 2
88: nsize: 2
89: args: -dof 2
90: filter: grep -v -i Object
91: requires: x
93: test:
94: suffix: 3
95: nsize: 3
96: args: -dof 2
97: filter: grep -v -i Object
98: requires: x
100: TEST*/