xref: /petsc/src/dm/tests/ex38.c (revision df4cd43f92eaa320656440c40edb1046daee8f75)
1 
2 static char help[] = "Tests DMGlobalToLocal() for 3d DA with stencil width of 2.\n\n";
3 
4 #include <petscdm.h>
5 #include <petscdmda.h>
6 
7 int main(int argc, char **argv)
8 {
9   PetscInt        N = 3, M = 2, P = 4, dof = 1, rstart, rend, i;
10   PetscInt        stencil_width = 2;
11   PetscMPIInt     rank;
12   DMBoundaryType  bx = DM_BOUNDARY_NONE, by = DM_BOUNDARY_NONE, bz = DM_BOUNDARY_NONE;
13   DMDAStencilType stencil_type = DMDA_STENCIL_STAR;
14   DM              da;
15   Vec             global, local;
16 
17   PetscFunctionBeginUser;
18   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
19   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
20   PetscCall(PetscOptionsGetInt(NULL, NULL, "-M", &M, NULL));
21   PetscCall(PetscOptionsGetInt(NULL, NULL, "-N", &N, NULL));
22   PetscCall(PetscOptionsGetInt(NULL, NULL, "-P", &P, NULL));
23   PetscCall(PetscOptionsGetInt(NULL, NULL, "-dof", &dof, NULL));
24   PetscCall(PetscOptionsGetInt(NULL, NULL, "-stencil_width", &stencil_width, NULL));
25   PetscCall(PetscOptionsGetInt(NULL, NULL, "-stencil_type", (PetscInt *)&stencil_type, NULL));
26 
27   PetscCall(DMDACreate3d(PETSC_COMM_WORLD, bx, by, bz, stencil_type, M, N, P, PETSC_DECIDE, PETSC_DECIDE, PETSC_DECIDE, dof, stencil_width, 0, 0, 0, &da));
28   PetscCall(DMSetFromOptions(da));
29   PetscCall(DMSetUp(da));
30   PetscCall(DMCreateGlobalVector(da, &global));
31   PetscCall(VecGetOwnershipRange(global, &rstart, &rend));
32   for (i = rstart; i < rend; i++) PetscCall(VecSetValue(global, i, (PetscReal)(i + 100 * rank), INSERT_VALUES));
33   PetscCall(VecAssemblyBegin(global));
34   PetscCall(VecAssemblyEnd(global));
35   PetscCall(DMCreateLocalVector(da, &local));
36   PetscCall(VecSet(local, -1));
37   PetscCall(DMGlobalToLocalBegin(da, global, INSERT_VALUES, local));
38   PetscCall(DMGlobalToLocalEnd(da, global, INSERT_VALUES, local));
39   if (rank == 0) PetscCall(VecView(local, 0));
40   PetscCall(DMDestroy(&da));
41   PetscCall(VecDestroy(&local));
42   PetscCall(VecDestroy(&global));
43   PetscCall(PetscFinalize());
44   return 0;
45 }
46