Actual source code: ex2.c
2: static char help[] = "Tests various 1-dimensional DMDA routines.\n\n";
4: #include <petscdm.h>
5: #include <petscdmda.h>
6: #include <petscdraw.h>
8: int main(int argc, char **argv)
9: {
10: PetscMPIInt rank;
11: PetscInt M = 13, s = 1, dof = 1;
12: DMBoundaryType bx = DM_BOUNDARY_PERIODIC;
13: DM da;
14: PetscViewer viewer;
15: Vec local, global;
16: PetscScalar value;
17: PetscDraw draw;
18: PetscBool flg = PETSC_FALSE;
19: ISLocalToGlobalMapping is;
21: PetscFunctionBeginUser;
22: PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
23: PetscCall(PetscViewerDrawOpen(PETSC_COMM_WORLD, 0, "", 280, 480, 600, 200, &viewer));
24: PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
25: PetscCall(PetscDrawSetDoubleBuffer(draw));
27: /* Readoptions */
28: PetscCall(PetscOptionsGetInt(NULL, NULL, "-M", &M, NULL));
29: PetscCall(PetscOptionsGetEnum(NULL, NULL, "-wrap", DMBoundaryTypes, (PetscEnum *)&bx, NULL));
30: PetscCall(PetscOptionsGetInt(NULL, NULL, "-dof", &dof, NULL));
31: PetscCall(PetscOptionsGetInt(NULL, NULL, "-s", &s, NULL));
33: /* Create distributed array and get vectors */
34: PetscCall(DMDACreate1d(PETSC_COMM_WORLD, bx, M, dof, s, NULL, &da));
35: PetscCall(DMSetFromOptions(da));
36: PetscCall(DMSetUp(da));
37: PetscCall(DMView(da, viewer));
38: PetscCall(DMCreateGlobalVector(da, &global));
39: PetscCall(DMCreateLocalVector(da, &local));
41: value = 1;
42: PetscCall(VecSet(global, value));
44: PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
45: value = rank + 1;
46: PetscCall(VecScale(global, value));
48: PetscCall(VecView(global, viewer));
49: PetscCall(PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD, "\nGlobal Vector:\n"));
50: PetscCall(VecView(global, PETSC_VIEWER_STDOUT_WORLD));
51: PetscCall(PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD, "\n"));
53: /* Send ghost points to local vectors */
54: PetscCall(DMGlobalToLocalBegin(da, global, INSERT_VALUES, local));
55: PetscCall(DMGlobalToLocalEnd(da, global, INSERT_VALUES, local));
57: PetscCall(PetscOptionsGetBool(NULL, NULL, "-local_print", &flg, NULL));
58: if (flg) {
59: PetscViewer sviewer;
61: PetscCall(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD));
62: PetscCall(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD, "\nLocal Vector: processor %d\n", rank));
63: PetscCall(PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD, PETSC_COMM_SELF, &sviewer));
64: PetscCall(VecView(local, sviewer));
65: PetscCall(PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD, PETSC_COMM_SELF, &sviewer));
66: PetscCall(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD));
67: PetscCall(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD));
68: }
69: PetscCall(PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD, "\nLocal to global mapping\n"));
70: PetscCall(DMGetLocalToGlobalMapping(da, &is));
71: PetscCall(ISLocalToGlobalMappingView(is, PETSC_VIEWER_STDOUT_WORLD));
73: /* Free memory */
74: PetscCall(PetscViewerDestroy(&viewer));
75: PetscCall(VecDestroy(&global));
76: PetscCall(VecDestroy(&local));
77: PetscCall(DMDestroy(&da));
78: PetscCall(PetscFinalize());
79: return 0;
80: }
82: /*TEST
84: test:
85: nsize: 2
86: args: -nox
87: filter: grep -v " MPI process"
88: output_file: output/ex2_1.out
89: requires: x
91: test:
92: suffix: 2
93: nsize: 3
94: args: -wrap none -local_print -nox
95: filter: grep -v "Vec Object: Vec"
96: requires: x
98: test:
99: suffix: 3
100: nsize: 3
101: args: -wrap ghosted -local_print -nox
102: filter: grep -v "Vec Object: Vec"
103: requires: x
105: TEST*/