xref: /petsc/src/dm/tests/ex11.c (revision 503c0ea9b45bcfbcebbb1ea5341243bbc69f0bea)
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   PetscCall(PetscInitialize(&argc,&argv,(char*)0,help));
18   /* Create viewers */
19   PetscCall(PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",PETSC_DECIDE,PETSC_DECIDE,600,200,&viewer));
20   PetscCall(PetscViewerDrawGetDraw(viewer,0,&draw));
21   PetscCall(PetscDrawSetDoubleBuffer(draw));
22 
23   /* Read options */
24   PetscCall(PetscOptionsGetInt(NULL,NULL,"-M",&M,NULL));
25   PetscCall(PetscOptionsGetInt(NULL,NULL,"-N",&N,NULL));
26   PetscCall(PetscOptionsGetInt(NULL,NULL,"-dof",&dof,NULL));
27   PetscCall(PetscOptionsGetInt(NULL,NULL,"-s",&s,NULL));
28   PetscCall(PetscOptionsGetInt(NULL,NULL,"-periodic_x",&wrap,NULL));
29   PetscCall(PetscOptionsGetInt(NULL,NULL,"-periodic_y",&wrap,NULL));
30 
31   /* Create distributed array and get vectors */
32   PetscCall(DMDACreate2d(PETSC_COMM_WORLD,(DMBoundaryType)bx,(DMBoundaryType)by,DMDA_STENCIL_BOX,M,N,PETSC_DECIDE,PETSC_DECIDE,dof,s,NULL,NULL,&da));
33   PetscCall(DMSetFromOptions(da));
34   PetscCall(DMSetUp(da));
35   PetscCall(DMDASetUniformCoordinates(da,0.0,1.0,0.0,1.0,0.0,0.0));
36   for (i=0; i<dof; i++) {
37     sprintf(fname,"Field %d",(int)i);
38     PetscCall(DMDASetFieldName(da,i,fname));
39   }
40 
41   PetscCall(DMView(da,viewer));
42   PetscCall(DMCreateGlobalVector(da,&global));
43   PetscCall(DMCreateLocalVector(da,&local));
44   PetscCall(DMGetCoordinates(da,&coors));
45   PetscCall(DMGetCoordinateDM(da,&dac));
46 
47   /* Set values into global vectors */
48   PetscCall(DMDAVecGetArrayDOFRead(dac,coors,&xy));
49   PetscCall(DMDAVecGetArrayDOF(da,global,&aglobal));
50   PetscCall(DMDAGetCorners(da,&xs,&ys,0,&m,&n,0));
51   for (k=0; k<dof; k++) {
52     for (j=ys; j<ys+n; j++) {
53       for (i=xs; i<xs+m; i++) {
54         aglobal[j][i][k] = PetscSinScalar(2.0*PETSC_PI*(k+1)*xy[j][i][0]);
55       }
56     }
57   }
58   PetscCall(DMDAVecRestoreArrayDOF(da,global,&aglobal));
59   PetscCall(DMDAVecRestoreArrayDOFRead(dac,coors,&xy));
60   PetscCall(DMGlobalToLocalBegin(da,global,INSERT_VALUES,local));
61   PetscCall(DMGlobalToLocalEnd(da,global,INSERT_VALUES,local));
62 
63   PetscCall(VecSet(global,0.0));
64   PetscCall(DMLocalToGlobalBegin(da,local,INSERT_VALUES,global));
65   PetscCall(DMLocalToGlobalEnd(da,local,INSERT_VALUES,global));
66   PetscCall(VecView(global,PETSC_VIEWER_STDOUT_WORLD));
67   PetscCall(VecView(global,viewer));
68 
69   /* Free memory */
70   PetscCall(PetscViewerDestroy(&viewer));
71   PetscCall(VecDestroy(&global));
72   PetscCall(VecDestroy(&local));
73   PetscCall(DMDestroy(&da));
74   PetscCall(PetscFinalize());
75   return 0;
76 }
77 
78 /*TEST
79 
80    test:
81       args: -dof 2
82       filter: grep -v -i Object
83       requires: x
84 
85    test:
86       suffix: 2
87       nsize: 2
88       args: -dof 2
89       filter: grep -v -i Object
90       requires: x
91 
92    test:
93       suffix: 3
94       nsize: 3
95       args: -dof 2
96       filter: grep -v -i Object
97       requires: x
98 
99 TEST*/
100