xref: /petsc/src/dm/tests/ex11.c (revision 21e3ffae2f3b73c0bd738cf6d0a809700fc04bb0)
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     PetscCall(PetscSNPrintf(fname, PETSC_STATIC_ARRAY_LENGTH(fname), "Field %" PetscInt_FMT, 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++) aglobal[j][i][k] = PetscSinScalar(2.0 * PETSC_PI * (k + 1) * xy[j][i][0]);
55     }
56   }
57   PetscCall(DMDAVecRestoreArrayDOF(da, global, &aglobal));
58   PetscCall(DMDAVecRestoreArrayDOFRead(dac, coors, &xy));
59   PetscCall(DMGlobalToLocalBegin(da, global, INSERT_VALUES, local));
60   PetscCall(DMGlobalToLocalEnd(da, global, INSERT_VALUES, local));
61 
62   PetscCall(VecSet(global, 0.0));
63   PetscCall(DMLocalToGlobalBegin(da, local, INSERT_VALUES, global));
64   PetscCall(DMLocalToGlobalEnd(da, local, INSERT_VALUES, global));
65   PetscCall(VecView(global, PETSC_VIEWER_STDOUT_WORLD));
66   PetscCall(VecView(global, viewer));
67 
68   /* Free memory */
69   PetscCall(PetscViewerDestroy(&viewer));
70   PetscCall(VecDestroy(&global));
71   PetscCall(VecDestroy(&local));
72   PetscCall(DMDestroy(&da));
73   PetscCall(PetscFinalize());
74   return 0;
75 }
76 
77 /*TEST
78 
79    test:
80       args: -dof 2
81       filter: grep -v -i Object
82       requires: x
83 
84    test:
85       suffix: 2
86       nsize: 2
87       args: -dof 2
88       filter: grep -v -i Object
89       requires: x
90 
91    test:
92       suffix: 3
93       nsize: 3
94       args: -dof 2
95       filter: grep -v -i Object
96       requires: x
97 
98 TEST*/
99