xref: /petsc/src/dm/tests/ex11.c (revision 732aec7a18f2199fb53bb9a2f3aef439a834ce31)
1 static char help[] = "Tests various 2-dimensional DMDA routines.\n\n";
2 
3 #include <petscdmda.h>
4 #include <petscdraw.h>
5 
main(int argc,char ** argv)6 int main(int argc, char **argv)
7 {
8   PetscInt       M = 10, N = 8, dof = 1, s = 1, bx = 0, by = 0, i, n, j, k, m, wrap, xs, ys;
9   DM             da, dac;
10   PetscViewer    viewer;
11   Vec            local, global, coors;
12   PetscScalar ***xy, ***aglobal;
13   PetscDraw      draw;
14   char           fname[32];
15 
16   PetscFunctionBeginUser;
17   PetscCall(PetscInitialize(&argc, &argv, NULL, 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     PetscCall(PetscSNPrintf(fname, PETSC_STATIC_ARRAY_LENGTH(fname), "Field %" PetscInt_FMT, 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++) aglobal[j][i][k] = PetscSinScalar(2.0 * PETSC_PI * (k + 1) * xy[j][i][0]);
54     }
55   }
56   PetscCall(DMDAVecRestoreArrayDOF(da, global, &aglobal));
57   PetscCall(DMDAVecRestoreArrayDOFRead(dac, coors, &xy));
58   PetscCall(DMGlobalToLocalBegin(da, global, INSERT_VALUES, local));
59   PetscCall(DMGlobalToLocalEnd(da, global, INSERT_VALUES, local));
60 
61   PetscCall(VecSet(global, 0.0));
62   PetscCall(DMLocalToGlobalBegin(da, local, INSERT_VALUES, global));
63   PetscCall(DMLocalToGlobalEnd(da, local, INSERT_VALUES, global));
64   PetscCall(VecView(global, PETSC_VIEWER_STDOUT_WORLD));
65   PetscCall(VecView(global, viewer));
66 
67   /* Free memory */
68   PetscCall(PetscViewerDestroy(&viewer));
69   PetscCall(VecDestroy(&global));
70   PetscCall(VecDestroy(&local));
71   PetscCall(DMDestroy(&da));
72   PetscCall(PetscFinalize());
73   return 0;
74 }
75 
76 /*TEST
77 
78    test:
79       args: -dof 2
80       filter: grep -v -i Object
81       requires: x
82 
83    test:
84       suffix: 2
85       nsize: 2
86       args: -dof 2
87       filter: grep -v -i Object
88       requires: x
89 
90    test:
91       suffix: 3
92       nsize: 3
93       args: -dof 2
94       filter: grep -v -i Object
95       requires: x
96 
97 TEST*/
98