xref: /petsc/src/dm/tests/ex11.c (revision ebead697dbf761eb322f829370bbe90b3bd93fa3)
1 
2 static char help[] = "Tests various 2-dimensional DMDA routines.\n\n";
3 
4 #include <petscdmda.h>
5 #include <petscdraw.h>
6 
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;
10   DM             da,dac;
11   PetscViewer    viewer;
12   Vec            local,global,coors;
13   PetscScalar    ***xy,***aglobal;
14   PetscDraw      draw;
15   char           fname[32];
16 
17   PetscFunctionBeginUser;
18   PetscCall(PetscInitialize(&argc,&argv,(char*)0,help));
19   /* Create viewers */
20   PetscCall(PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",PETSC_DECIDE,PETSC_DECIDE,600,200,&viewer));
21   PetscCall(PetscViewerDrawGetDraw(viewer,0,&draw));
22   PetscCall(PetscDrawSetDoubleBuffer(draw));
23 
24   /* Read options */
25   PetscCall(PetscOptionsGetInt(NULL,NULL,"-M",&M,NULL));
26   PetscCall(PetscOptionsGetInt(NULL,NULL,"-N",&N,NULL));
27   PetscCall(PetscOptionsGetInt(NULL,NULL,"-dof",&dof,NULL));
28   PetscCall(PetscOptionsGetInt(NULL,NULL,"-s",&s,NULL));
29   PetscCall(PetscOptionsGetInt(NULL,NULL,"-periodic_x",&wrap,NULL));
30   PetscCall(PetscOptionsGetInt(NULL,NULL,"-periodic_y",&wrap,NULL));
31 
32   /* Create distributed array and get vectors */
33   PetscCall(DMDACreate2d(PETSC_COMM_WORLD,(DMBoundaryType)bx,(DMBoundaryType)by,DMDA_STENCIL_BOX,M,N,PETSC_DECIDE,PETSC_DECIDE,dof,s,NULL,NULL,&da));
34   PetscCall(DMSetFromOptions(da));
35   PetscCall(DMSetUp(da));
36   PetscCall(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     PetscCall(DMDASetFieldName(da,i,fname));
40   }
41 
42   PetscCall(DMView(da,viewer));
43   PetscCall(DMCreateGlobalVector(da,&global));
44   PetscCall(DMCreateLocalVector(da,&local));
45   PetscCall(DMGetCoordinates(da,&coors));
46   PetscCall(DMGetCoordinateDM(da,&dac));
47 
48   /* Set values into global vectors */
49   PetscCall(DMDAVecGetArrayDOFRead(dac,coors,&xy));
50   PetscCall(DMDAVecGetArrayDOF(da,global,&aglobal));
51   PetscCall(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   PetscCall(DMDAVecRestoreArrayDOF(da,global,&aglobal));
60   PetscCall(DMDAVecRestoreArrayDOFRead(dac,coors,&xy));
61   PetscCall(DMGlobalToLocalBegin(da,global,INSERT_VALUES,local));
62   PetscCall(DMGlobalToLocalEnd(da,global,INSERT_VALUES,local));
63 
64   PetscCall(VecSet(global,0.0));
65   PetscCall(DMLocalToGlobalBegin(da,local,INSERT_VALUES,global));
66   PetscCall(DMLocalToGlobalEnd(da,local,INSERT_VALUES,global));
67   PetscCall(VecView(global,PETSC_VIEWER_STDOUT_WORLD));
68   PetscCall(VecView(global,viewer));
69 
70   /* Free memory */
71   PetscCall(PetscViewerDestroy(&viewer));
72   PetscCall(VecDestroy(&global));
73   PetscCall(VecDestroy(&local));
74   PetscCall(DMDestroy(&da));
75   PetscCall(PetscFinalize());
76   return 0;
77 }
78 
79 /*TEST
80 
81    test:
82       args: -dof 2
83       filter: grep -v -i Object
84       requires: x
85 
86    test:
87       suffix: 2
88       nsize: 2
89       args: -dof 2
90       filter: grep -v -i Object
91       requires: x
92 
93    test:
94       suffix: 3
95       nsize: 3
96       args: -dof 2
97       filter: grep -v -i Object
98       requires: x
99 
100 TEST*/
101