Actual source code: ex20.c
1: static char help[] = "Test DMStag transfer operators, on a faces-only grid.\n\n";
3: #include <petscdm.h>
4: #include <petscdmstag.h>
6: int main(int argc, char **argv)
7: {
8: DM dm;
9: PetscInt dim;
10: PetscBool flg, dump;
12: PetscFunctionBeginUser;
13: PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
14: PetscCall(PetscOptionsGetInt(NULL, NULL, "-dim", &dim, &flg));
15: PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG, "Supply -dim option\n");
16: if (dim == 1) PetscCall(DMStagCreate1d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, 4, 1, 0, DMSTAG_STENCIL_BOX, 1, NULL, &dm));
17: else if (dim == 2) PetscCall(DMStagCreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, 4, 4, PETSC_DECIDE, PETSC_DECIDE, 0, 1, 0, DMSTAG_STENCIL_BOX, 1, NULL, NULL, &dm));
18: 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, 0, 0, 1, 0, DMSTAG_STENCIL_BOX, 1, NULL, NULL, NULL, &dm));
19: else SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP, "dim must be 1, 2, or 3");
20: PetscCall(DMSetFromOptions(dm));
21: PetscCall(DMSetUp(dm));
23: /* Flags to dump binary or ASCII output */
24: dump = PETSC_FALSE;
25: PetscCall(PetscOptionsGetBool(NULL, NULL, "-dump", &dump, NULL));
27: /* Directly create a coarsened DM and transfer operators */
28: {
29: DM dmCoarse;
30: PetscCall(DMCoarsen(dm, MPI_COMM_NULL, &dmCoarse));
31: {
32: Mat Ai;
33: PetscCall(DMCreateInterpolation(dmCoarse, dm, &Ai, NULL));
34: if (dump) {
35: PetscViewer viewer;
36: PetscCall(PetscViewerBinaryOpen(PetscObjectComm((PetscObject)dm), "matI.pbin", FILE_MODE_WRITE, &viewer));
37: PetscCall(MatView(Ai, viewer));
38: PetscCall(PetscViewerDestroy(&viewer));
39: }
40: PetscCall(MatDestroy(&Ai));
41: }
42: {
43: Mat Ar;
44: PetscCall(DMCreateRestriction(dmCoarse, dm, &Ar));
45: if (dump) {
46: PetscViewer viewer;
47: PetscCall(PetscViewerBinaryOpen(PetscObjectComm((PetscObject)dm), "matR.pbin", FILE_MODE_WRITE, &viewer));
48: PetscCall(MatView(Ar, viewer));
49: PetscCall(PetscViewerDestroy(&viewer));
50: }
51: PetscCall(MatDestroy(&Ar));
52: }
53: PetscCall(DMDestroy(&dmCoarse));
54: }
56: PetscCall(DMDestroy(&dm));
57: PetscCall(PetscFinalize());
58: return 0;
59: }
61: /*TEST
63: test:
64: suffix: 1
65: nsize: 1
66: args: -dim 1
68: test:
69: suffix: 2
70: nsize: 1
71: args: -dim 2
73: TEST*/