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