xref: /petsc/src/dm/tests/ex44.c (revision 66af8762ec03dbef0e079729eb2a1734a35ed7ff)
1 static char help[] = "Tests various DMComposite routines.\n\n";
2 
3 #include <petscdm.h>
4 #include <petscdmda.h>
5 #include <petscdmcomposite.h>
6 
7 int main(int argc, char **argv)
8 {
9   PetscMPIInt rank;
10   DM          da1, da2, packer;
11   Vec         local, global, globals[2], buffer;
12   PetscScalar value;
13   PetscViewer viewer;
14 
15   PetscFunctionBeginUser;
16   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
17 
18   PetscCall(DMCompositeCreate(PETSC_COMM_WORLD, &packer));
19   PetscCall(DMDACreate1d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, 8, 1, 1, NULL, &da1));
20   PetscCall(DMSetFromOptions(da1));
21   PetscCall(DMSetUp(da1));
22   PetscCall(DMCompositeAddDM(packer, da1));
23   PetscCall(DMDACreate1d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, 6, 1, 1, NULL, &da2));
24   PetscCall(DMSetFromOptions(da2));
25   PetscCall(DMSetUp(da2));
26   PetscCall(DMCompositeAddDM(packer, da2));
27 
28   PetscCall(DMCreateGlobalVector(packer, &global));
29   PetscCall(DMCreateLocalVector(packer, &local));
30   PetscCall(DMCreateLocalVector(packer, &buffer));
31 
32   PetscCall(DMCompositeGetAccessArray(packer, global, 2, NULL, globals));
33   value = 1;
34   PetscCall(VecSet(globals[0], value));
35   value = -1;
36   PetscCall(VecSet(globals[1], value));
37   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
38   value = rank + 1;
39   PetscCall(VecScale(globals[0], value));
40   PetscCall(VecScale(globals[1], value));
41   PetscCall(DMCompositeRestoreAccessArray(packer, global, 2, NULL, globals));
42 
43   /* Test GlobalToLocal in insert mode */
44   PetscCall(DMGlobalToLocalBegin(packer, global, INSERT_VALUES, local));
45   PetscCall(DMGlobalToLocalEnd(packer, global, INSERT_VALUES, local));
46 
47   PetscCall(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD));
48   PetscCall(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD, "\nLocal Vector: processor %d\n", rank));
49   PetscCall(PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD, PETSC_COMM_SELF, &viewer));
50   PetscCall(VecView(local, viewer));
51   PetscCall(PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD, PETSC_COMM_SELF, &viewer));
52   PetscCall(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD));
53   PetscCall(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD));
54 
55   /* Test LocalToGlobal in insert mode */
56   PetscCall(DMLocalToGlobalBegin(packer, local, INSERT_VALUES, global));
57   PetscCall(DMLocalToGlobalEnd(packer, local, INSERT_VALUES, global));
58 
59   PetscCall(VecView(global, PETSC_VIEWER_STDOUT_WORLD));
60 
61   /* Test LocalToLocal in insert mode */
62   PetscCall(DMLocalToLocalBegin(packer, local, INSERT_VALUES, buffer));
63   PetscCall(DMLocalToLocalEnd(packer, local, INSERT_VALUES, buffer));
64 
65   PetscCall(PetscViewerASCIIPushSynchronized(PETSC_VIEWER_STDOUT_WORLD));
66   PetscCall(PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD, "\nLocal Vector: processor %d\n", rank));
67   PetscCall(PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD, PETSC_COMM_SELF, &viewer));
68   PetscCall(VecView(buffer, viewer));
69   PetscCall(PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD, PETSC_COMM_SELF, &viewer));
70   PetscCall(PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD));
71   PetscCall(PetscViewerASCIIPopSynchronized(PETSC_VIEWER_STDOUT_WORLD));
72 
73   PetscCall(VecDestroy(&buffer));
74   PetscCall(VecDestroy(&local));
75   PetscCall(VecDestroy(&global));
76   PetscCall(DMDestroy(&packer));
77   PetscCall(DMDestroy(&da2));
78   PetscCall(DMDestroy(&da1));
79 
80   PetscCall(PetscFinalize());
81   return 0;
82 }
83 
84 /*TEST
85 
86    test:
87       nsize: 3
88 
89 TEST*/
90