xref: /petsc/src/dm/tests/ex2.c (revision 6c2b77d522d8aa5c8b27f04fddd7150d0d6755fb)
1 
2 static char help[] = "Tests various 1-dimensional DMDA routines.\n\n";
3 
4 #include <petscdm.h>
5 #include <petscdmda.h>
6 #include <petscdraw.h>
7 
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;
20 
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));
26 
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));
32 
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));
40 
41   value = 1;
42   PetscCall(VecSet(global, value));
43 
44   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
45   value = rank + 1;
46   PetscCall(VecScale(global, value));
47 
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"));
52 
53   /* Send ghost points to local vectors */
54   PetscCall(DMGlobalToLocalBegin(da, global, INSERT_VALUES, local));
55   PetscCall(DMGlobalToLocalEnd(da, global, INSERT_VALUES, local));
56 
57   PetscCall(PetscOptionsGetBool(NULL, NULL, "-local_print", &flg, NULL));
58   if (flg) {
59     PetscViewer sviewer;
60 
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));
72 
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 }
81 
82 /*TEST
83 
84    test:
85       nsize: 2
86       args: -nox
87       filter: grep -v " MPI process"
88       output_file: output/ex2_1.out
89       requires: x
90 
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
97 
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
104 
105 TEST*/
106