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