Actual source code: ex40.c
1: static char help[] = "Tests mirror boundary conditions in 2-d.\n\n";
3: #include <petscdm.h>
4: #include <petscdmda.h>
6: int main(int argc, char **argv)
7: {
8: PetscInt M = 8, N = 8, stencil_width = 1, dof = 1, m, n, xstart, ystart, i, j, c;
9: DM da;
10: Vec global, local;
11: PetscScalar ***vglobal;
12: PetscViewer sview;
14: PetscFunctionBeginUser;
15: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
16: PetscCall(PetscOptionsGetInt(NULL, 0, "-stencil_width", &stencil_width, 0));
17: PetscCall(PetscOptionsGetInt(NULL, 0, "-dof", &dof, 0));
19: PetscCall(DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_MIRROR, DM_BOUNDARY_MIRROR, DMDA_STENCIL_STAR, M, N, PETSC_DECIDE, PETSC_DECIDE, dof, stencil_width, NULL, NULL, &da));
20: PetscCall(DMSetFromOptions(da));
21: PetscCall(DMSetUp(da));
22: PetscCall(DMDAGetCorners(da, &xstart, &ystart, 0, &m, &n, 0));
24: PetscCall(DMCreateGlobalVector(da, &global));
25: PetscCall(DMDAVecGetArrayDOF(da, global, &vglobal));
26: for (j = ystart; j < ystart + n; j++) {
27: for (i = xstart; i < xstart + m; i++) {
28: for (c = 0; c < dof; c++) vglobal[j][i][c] = 100 * j + 10 * (i + 1) + c;
29: }
30: }
31: PetscCall(DMDAVecRestoreArrayDOF(da, global, &vglobal));
33: PetscCall(DMCreateLocalVector(da, &local));
34: PetscCall(DMGlobalToLocalBegin(da, global, INSERT_VALUES, local));
35: PetscCall(DMGlobalToLocalEnd(da, global, INSERT_VALUES, local));
37: PetscCall(PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD, PETSC_COMM_SELF, &sview));
38: PetscCall(VecView(local, sview));
39: PetscCall(PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD, PETSC_COMM_SELF, &sview));
40: PetscCall(VecView(global, PETSC_VIEWER_STDOUT_WORLD));
42: PetscCall(DMDestroy(&da));
43: PetscCall(VecDestroy(&local));
44: PetscCall(VecDestroy(&global));
46: PetscCall(PetscFinalize());
47: return 0;
48: }
50: /*TEST
52: test:
54: test:
55: suffix: 2
56: nsize: 4
57: filter: grep -v "Vec Object"
59: TEST*/