Actual source code: ex5.c
1: static char help[] = "Tests DMDAGetElements() and VecView() contour plotting for 2d DMDAs.\n\n";
3: #include <petscdm.h>
4: #include <petscdmda.h>
6: int main(int argc, char **argv)
7: {
8: PetscInt M = 10, N = 8, ne, nc, i;
9: const PetscInt *e;
10: PetscBool flg = PETSC_FALSE;
11: DM da;
12: PetscViewer viewer;
13: Vec local, global;
14: PetscScalar value;
15: DMBoundaryType bx = DM_BOUNDARY_NONE, by = DM_BOUNDARY_NONE;
16: DMDAStencilType stype = DMDA_STENCIL_BOX;
17: PetscScalar *lv;
19: PetscFunctionBeginUser;
20: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
21: PetscCall(PetscViewerDrawOpen(PETSC_COMM_WORLD, NULL, "", 300, 0, 300, 300, &viewer));
23: /* Read options */
24: PetscCall(PetscOptionsGetInt(NULL, NULL, "-M", &M, NULL));
25: PetscCall(PetscOptionsGetInt(NULL, NULL, "-N", &N, NULL));
26: PetscCall(PetscOptionsGetBool(NULL, NULL, "-star_stencil", &flg, NULL));
27: if (flg) stype = DMDA_STENCIL_STAR;
29: /* Create distributed array and get vectors */
30: PetscCall(DMDACreate2d(PETSC_COMM_WORLD, bx, by, stype, M, N, PETSC_DECIDE, PETSC_DECIDE, 1, 1, NULL, NULL, &da));
31: PetscCall(DMSetFromOptions(da));
32: PetscCall(DMSetUp(da));
33: PetscCall(DMCreateGlobalVector(da, &global));
34: PetscCall(DMCreateLocalVector(da, &local));
36: value = -3.0;
37: PetscCall(VecSet(global, value));
38: PetscCall(DMGlobalToLocalBegin(da, global, INSERT_VALUES, local));
39: PetscCall(DMGlobalToLocalEnd(da, global, INSERT_VALUES, local));
41: PetscCall(DMDASetElementType(da, DMDA_ELEMENT_P1));
42: PetscCall(DMDAGetElements(da, &ne, &nc, &e));
43: PetscCall(VecGetArray(local, &lv));
44: for (i = 0; i < ne; i++) {
45: PetscCall(PetscSynchronizedPrintf(PETSC_COMM_WORLD, "i %" PetscInt_FMT " e[3*i] %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n", i, e[3 * i], e[3 * i + 1], e[3 * i + 2]));
46: lv[e[3 * i]] = i;
47: }
48: PetscCall(PetscSynchronizedFlush(PETSC_COMM_WORLD, stdout));
49: PetscCall(VecRestoreArray(local, &lv));
50: PetscCall(DMDARestoreElements(da, &ne, &nc, &e));
52: PetscCall(DMLocalToGlobalBegin(da, local, ADD_VALUES, global));
53: PetscCall(DMLocalToGlobalEnd(da, local, ADD_VALUES, global));
55: PetscCall(DMView(da, viewer));
56: PetscCall(VecView(global, viewer));
58: /* Free memory */
59: PetscCall(PetscViewerDestroy(&viewer));
60: PetscCall(VecDestroy(&local));
61: PetscCall(VecDestroy(&global));
62: PetscCall(DMDestroy(&da));
63: PetscCall(PetscFinalize());
64: return 0;
65: }
67: /*TEST
69: test:
70: requires: x
72: test:
73: suffix: 2
74: nsize: 2
75: requires: x
77: TEST*/