Actual source code: ex53.c
1: static char help[] = "Test local-to-local for DMStag.\n\n";
3: #include <petscdmstag.h>
5: int main(int argc, char **argv)
6: {
7: DM dm;
8: PetscInt dim, start, end, i;
9: PetscBool flg;
10: Vec g, l1, l2;
11: PetscMPIInt rank;
12: PetscScalar value;
13: PetscReal norm;
15: PetscFunctionBeginUser;
16: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
18: PetscCall(PetscOptionsGetInt(NULL, NULL, "-dim", &dim, &flg));
19: PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG, "Supply -dim option");
21: if (dim == 1) PetscCall(DMStagCreate1d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, 64, 1, 1, DMSTAG_STENCIL_BOX, 1, NULL, &dm));
22: else if (dim == 2) PetscCall(DMStagCreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, 8, 8, PETSC_DECIDE, PETSC_DECIDE, 1, 1, 1, DMSTAG_STENCIL_BOX, 1, NULL, NULL, &dm));
23: else if (dim == 3) PetscCall(DMStagCreate3d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, 4, 4, 4, PETSC_DECIDE, PETSC_DECIDE, PETSC_DECIDE, 1, 1, 1, 1, DMSTAG_STENCIL_BOX, 1, NULL, NULL, NULL, &dm));
24: else SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP, "dim must be 1, 2, or 3");
25: PetscCall(DMSetFromOptions(dm));
26: PetscCall(DMSetUp(dm));
28: PetscCall(DMCreateGlobalVector(dm, &g));
29: PetscCall(DMCreateLocalVector(dm, &l1));
30: PetscCall(VecDuplicate(l1, &l2));
32: PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
33: PetscCall(VecGetOwnershipRange(g, &start, &end));
34: for (i = start; i < end; ++i) {
35: value = rank + i;
36: PetscCall(VecSetValues(g, 1, &i, &value, INSERT_VALUES));
37: }
38: PetscCall(VecAssemblyBegin(g));
39: PetscCall(VecAssemblyEnd(g));
41: PetscCall(DMGlobalToLocalBegin(dm, g, INSERT_VALUES, l1));
42: PetscCall(DMGlobalToLocalEnd(dm, g, INSERT_VALUES, l1));
44: PetscCall(DMLocalToLocalBegin(dm, l1, INSERT_VALUES, l2));
45: PetscCall(DMLocalToLocalEnd(dm, l1, INSERT_VALUES, l2));
47: /* l1 and l2 must be same. */
48: PetscCall(VecAXPY(l2, -1.0, l1));
49: PetscCall(VecNorm(l2, NORM_MAX, &norm));
50: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &norm, 1, MPIU_REAL, MPIU_MAX, PETSC_COMM_WORLD));
51: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "norm = %g\n", (double)norm));
53: PetscCall(VecDestroy(&g));
54: PetscCall(VecDestroy(&l1));
55: PetscCall(VecDestroy(&l2));
56: PetscCall(DMDestroy(&dm));
57: PetscCall(PetscFinalize());
58: return 0;
59: }
61: /*TEST
63: test:
64: suffix: 1
65: nsize: 2
66: args: -dim 1
68: test:
69: suffix: 2
70: nsize: 3
71: args: -dim 1 -stag_boundary_type_x none
72: output_file: output/ex53_1.out
74: test:
75: suffix: 3
76: nsize: 4
77: args: -dim 1 -stag_boundary_type_x periodic
78: output_file: output/ex53_1.out
80: test:
81: suffix: 4
82: nsize: 4
83: args: -dim 2
84: output_file: output/ex53_1.out
86: test:
87: suffix: 5
88: nsize: 4
89: args: -dim 2 -stag_boundary_type_x none -stag_stencil_type star
90: output_file: output/ex53_1.out
92: test:
93: suffix: 6
94: nsize: 6
95: args: -dim 2 -stag_boundary_type_y periodic -stag_stencil_width 2 -stag_dof_0 0 -stag_dof_1 1 -stag_dof_2 0
96: output_file: output/ex53_1.out
98: test:
99: suffix: 7
100: nsize: 8
101: args: -dim 3
102: output_file: output/ex53_1.out
104: test:
105: suffix: 8
106: nsize: 8
107: args: -dim 3 -stag_boundary_type_x none -stag_boundary_type_y periodic
108: output_file: output/ex53_1.out
110: test:
111: suffix: 9
112: nsize: 12
113: args: -dim 3 -stag_boundary_type_x none -stag_boundary_type_y none -stag_boundary_type_z none -stag_stencil_type star -stag_dof_0 0 -stag_dof_1 0 -stag_dof_2 0 -stag_dof_3 1
114: output_file: output/ex53_1.out
116: TEST*/