xref: /petsc/src/dm/impls/stag/tests/ex20.c (revision 609caa7c8c030312b00807b4f015fd827bb80932)
1 static char help[] = "Test DMStag transfer operators.\n\n";
2 
3 #include <petscdm.h>
4 #include <petscdmstag.h>
5 
main(int argc,char ** argv)6 int main(int argc, char **argv)
7 {
8   DM        dmc, dmf;
9   PetscInt  dim;
10   PetscBool flg;
11 
12   PetscFunctionBeginUser;
13   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
14   PetscCall(PetscOptionsGetInt(NULL, NULL, "-dim", &dim, &flg));
15   PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG, "Supply -dim option");
16   if (dim == 1) PetscCall(DMStagCreate1d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, 3, 1, 1, DMSTAG_STENCIL_BOX, 1, NULL, &dmc));
17   else if (dim == 2) PetscCall(DMStagCreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, 3, 3, PETSC_DECIDE, PETSC_DECIDE, 1, 1, 1, DMSTAG_STENCIL_BOX, 1, NULL, NULL, &dmc));
18   else if (dim == 3) PetscCall(DMStagCreate3d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, 3, 3, 3, PETSC_DECIDE, PETSC_DECIDE, PETSC_DECIDE, 1, 1, 1, 1, DMSTAG_STENCIL_BOX, 1, NULL, NULL, NULL, &dmc));
19   else SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP, "dim must be 1, 2, or 3");
20   PetscCall(DMSetFromOptions(dmc));
21   PetscCall(DMSetUp(dmc));
22 
23   /* Directly create a coarsened DM and transfer operators */
24   PetscCall(DMRefine(dmc, MPI_COMM_NULL, &dmf));
25   {
26     Mat       Ai;
27     Vec       vc, vf;
28     PetscInt  size;
29     PetscReal norm;
30 
31     PetscCall(DMCreateInterpolation(dmc, dmf, &Ai, NULL));
32     PetscCall(MatCreateVecs(Ai, &vc, &vf));
33     PetscCall(VecSet(vc, 1.0));
34     PetscCall(MatMult(Ai, vc, vf));
35     PetscCall(VecGetSize(vf, &size));
36     PetscCall(VecNorm(vf, NORM_1, &norm));
37     PetscCheck((norm - size) / (PetscReal)size <= PETSC_MACHINE_EPSILON * 10.0, PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Numerical test failed");
38     PetscCall(MatDestroy(&Ai));
39     PetscCall(VecDestroy(&vc));
40     PetscCall(VecDestroy(&vf));
41   }
42   {
43     Mat       Ar;
44     Vec       vf, vc;
45     PetscInt  size;
46     PetscReal norm;
47 
48     PetscCall(DMCreateRestriction(dmc, dmf, &Ar));
49     PetscCall(MatCreateVecs(Ar, &vf, &vc));
50     PetscCall(VecSet(vf, 1.0));
51     PetscCall(MatMult(Ar, vf, vc));
52     PetscCall(VecGetSize(vc, &size));
53     PetscCall(VecNorm(vc, NORM_1, &norm));
54     PetscCheck((norm - size) / (PetscReal)size <= PETSC_MACHINE_EPSILON * 10.0, PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Numerical test failed");
55     PetscCall(MatDestroy(&Ar));
56     PetscCall(VecDestroy(&vf));
57     PetscCall(VecDestroy(&vc));
58   }
59   PetscCall(DMDestroy(&dmf));
60 
61   PetscCall(DMDestroy(&dmc));
62   PetscCall(PetscFinalize());
63   return 0;
64 }
65 
66 /*TEST
67 
68    test:
69       suffix: 1d
70       nsize: 1
71       args: -dim 1
72       output_file: output/empty.out
73 
74    test:
75       suffix: 1d_ratio
76       nsize: 1
77       args: -dim 1 -stag_refine_x 3
78       output_file: output/empty.out
79 
80    test:
81       suffix: 1d_par
82       nsize: 2
83       args: -dim 1 -stag_grid_x 6
84       output_file: output/empty.out
85 
86    test:
87       suffix: 2d
88       nsize: 1
89       args: -dim 2
90       output_file: output/empty.out
91 
92    test:
93       suffix: 2d_ratio
94       nsize: 1
95       args: -dim 2 -stag_refine_x 3 -stag_refine_y 4
96       output_file: output/empty.out
97 
98    test:
99       suffix: 2d_par
100       nsize: 4
101       args: -dim 2 -stag_grid_x 6 -stag_grid_y 7
102       output_file: output/empty.out
103 
104    test:
105       suffix: 3d
106       nsize: 1
107       args: -dim 3
108       output_file: output/empty.out
109 
110    test:
111       suffix: 3d_ratio
112       nsize: 1
113       args: -dim 3 -stag_refine_x 3 -stag_refine_y 4 -stag_refine_z 5
114       output_file: output/empty.out
115 
116    test:
117       suffix: 3d_par
118       nsize: 8
119       args: -dim 3 -stag_grid_x 6 -stag_grid_y 7 -stag_grid_z 8
120       output_file: output/empty.out
121 
122 TEST*/
123