xref: /petsc/src/dm/tests/ex51.c (revision df4cd43f92eaa320656440c40edb1046daee8f75)
1 
2 static char help[] = "Tests DMDAGlobalToNaturalAllCreate() using contour plotting for 2d DMDAs.\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   PetscInt        i, j, M = 10, N = 8, m = PETSC_DECIDE, n = PETSC_DECIDE;
11   PetscMPIInt     rank;
12   PetscBool       flg = PETSC_FALSE;
13   DM              da;
14   PetscViewer     viewer;
15   Vec             localall, global;
16   PetscScalar     value, *vlocal;
17   DMBoundaryType  bx = DM_BOUNDARY_NONE, by = DM_BOUNDARY_NONE;
18   DMDAStencilType stype = DMDA_STENCIL_BOX;
19   VecScatter      tolocalall, fromlocalall;
20   PetscInt        start, end;
21 
22   PetscFunctionBeginUser;
23   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
24   PetscCall(PetscViewerDrawOpen(PETSC_COMM_WORLD, 0, "", 300, 0, 300, 300, &viewer));
25 
26   /* Read options */
27   PetscCall(PetscOptionsGetInt(NULL, NULL, "-M", &M, NULL));
28   PetscCall(PetscOptionsGetInt(NULL, NULL, "-N", &N, NULL));
29   PetscCall(PetscOptionsGetInt(NULL, NULL, "-m", &m, NULL));
30   PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
31   PetscCall(PetscOptionsGetBool(NULL, NULL, "-star_stencil", &flg, NULL));
32   if (flg) stype = DMDA_STENCIL_STAR;
33 
34   /* Create distributed array and get vectors */
35   PetscCall(DMDACreate2d(PETSC_COMM_WORLD, bx, by, stype, M, N, m, n, 1, 1, NULL, NULL, &da));
36   PetscCall(DMSetFromOptions(da));
37   PetscCall(DMSetUp(da));
38 
39   PetscCall(DMCreateGlobalVector(da, &global));
40   PetscCall(VecCreateSeq(PETSC_COMM_SELF, M * N, &localall));
41 
42   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
43   PetscCall(VecGetOwnershipRange(global, &start, &end));
44   for (i = start; i < end; i++) {
45     value = 5.0 * rank;
46     PetscCall(VecSetValues(global, 1, &i, &value, INSERT_VALUES));
47   }
48   PetscCall(VecAssemblyBegin(global));
49   PetscCall(VecAssemblyEnd(global));
50   PetscCall(VecView(global, viewer));
51 
52   /*
53      Create Scatter from global DMDA parallel vector to local vector that
54    contains all entries
55   */
56   PetscCall(DMDAGlobalToNaturalAllCreate(da, &tolocalall));
57   PetscCall(DMDANaturalAllToGlobalCreate(da, &fromlocalall));
58 
59   PetscCall(VecScatterBegin(tolocalall, global, localall, INSERT_VALUES, SCATTER_FORWARD));
60   PetscCall(VecScatterEnd(tolocalall, global, localall, INSERT_VALUES, SCATTER_FORWARD));
61 
62   PetscCall(VecGetArray(localall, &vlocal));
63   for (j = 0; j < N; j++) {
64     for (i = 0; i < M; i++) *vlocal++ += i + j * M;
65   }
66   PetscCall(VecRestoreArray(localall, &vlocal));
67 
68   /* scatter back to global vector */
69   PetscCall(VecScatterBegin(fromlocalall, localall, global, INSERT_VALUES, SCATTER_FORWARD));
70   PetscCall(VecScatterEnd(fromlocalall, localall, global, INSERT_VALUES, SCATTER_FORWARD));
71 
72   PetscCall(VecView(global, viewer));
73 
74   /* Free memory */
75   PetscCall(VecScatterDestroy(&tolocalall));
76   PetscCall(VecScatterDestroy(&fromlocalall));
77   PetscCall(PetscViewerDestroy(&viewer));
78   PetscCall(VecDestroy(&localall));
79   PetscCall(VecDestroy(&global));
80   PetscCall(DMDestroy(&da));
81   PetscCall(PetscFinalize());
82   return 0;
83 }
84 
85 /*TEST
86 
87    build:
88      requires: !complex
89 
90    test:
91       nsize: 3
92 
93 TEST*/
94