xref: /petsc/src/dm/impls/plex/tutorials/ex2.c (revision cf4936a9770457e08a27f0b0f36952ff9843e1b2)
1*cf4936a9SMatthew G. Knepley static char help[] = "PETSc Annual Meeting 2025: Meshing Tutorial.\n\n\n";
2*cf4936a9SMatthew G. Knepley 
3*cf4936a9SMatthew G. Knepley #include <petsc.h>
4*cf4936a9SMatthew G. Knepley 
5*cf4936a9SMatthew G. Knepley static PetscErrorCode CreateMesh(MPI_Comm comm, DM *dm)
6*cf4936a9SMatthew G. Knepley {
7*cf4936a9SMatthew G. Knepley   PetscFunctionBeginUser;
8*cf4936a9SMatthew G. Knepley   PetscCall(DMCreate(comm, dm));
9*cf4936a9SMatthew G. Knepley   PetscCall(DMSetType(*dm, DMPLEX));
10*cf4936a9SMatthew G. Knepley   PetscCall(DMSetFromOptions(*dm));
11*cf4936a9SMatthew G. Knepley   PetscCall(DMViewFromOptions(*dm, NULL, "-dm_view"));
12*cf4936a9SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
13*cf4936a9SMatthew G. Knepley }
14*cf4936a9SMatthew G. Knepley 
15*cf4936a9SMatthew G. Knepley int main(int argc, char **argv)
16*cf4936a9SMatthew G. Knepley {
17*cf4936a9SMatthew G. Knepley   DM dm;
18*cf4936a9SMatthew G. Knepley 
19*cf4936a9SMatthew G. Knepley   PetscFunctionBeginUser;
20*cf4936a9SMatthew G. Knepley   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
21*cf4936a9SMatthew G. Knepley   PetscCall(CreateMesh(PETSC_COMM_WORLD, &dm));
22*cf4936a9SMatthew G. Knepley   PetscCall(DMDestroy(&dm));
23*cf4936a9SMatthew G. Knepley   PetscCall(PetscFinalize());
24*cf4936a9SMatthew G. Knepley   return PETSC_SUCCESS;
25*cf4936a9SMatthew G. Knepley }
26*cf4936a9SMatthew G. Knepley 
27*cf4936a9SMatthew G. Knepley /*TEST
28*cf4936a9SMatthew G. Knepley 
29*cf4936a9SMatthew G. Knepley   # Draw a square with X, use with -draw_pause -1
30*cf4936a9SMatthew G. Knepley   test:
31*cf4936a9SMatthew G. Knepley     suffix: 0
32*cf4936a9SMatthew G. Knepley     requires: triangle x
33*cf4936a9SMatthew G. Knepley     args: -dm_view draw -draw_pause 0
34*cf4936a9SMatthew G. Knepley 
35*cf4936a9SMatthew G. Knepley   # Draw a square with PyVista
36*cf4936a9SMatthew G. Knepley   test:
37*cf4936a9SMatthew G. Knepley     suffix: 1
38*cf4936a9SMatthew G. Knepley     requires: triangle pyvista
39*cf4936a9SMatthew G. Knepley     args: -dm_view pyvista
40*cf4936a9SMatthew G. Knepley     output_file: output/ex2_0.out
41*cf4936a9SMatthew G. Knepley 
42*cf4936a9SMatthew G. Knepley   # Refine the square
43*cf4936a9SMatthew G. Knepley   test:
44*cf4936a9SMatthew G. Knepley     suffix: 2
45*cf4936a9SMatthew G. Knepley     requires: triangle pyvista
46*cf4936a9SMatthew G. Knepley     args: -dm_view pyvista -dm_refine 1
47*cf4936a9SMatthew G. Knepley     output_file: output/ex2_0.out
48*cf4936a9SMatthew G. Knepley 
49*cf4936a9SMatthew G. Knepley   # Refine the square three times
50*cf4936a9SMatthew G. Knepley   test:
51*cf4936a9SMatthew G. Knepley     suffix: 3
52*cf4936a9SMatthew G. Knepley     requires: triangle pyvista
53*cf4936a9SMatthew G. Knepley     args: -dm_view pyvista -dm_refine 3
54*cf4936a9SMatthew G. Knepley     output_file: output/ex2_0.out
55*cf4936a9SMatthew G. Knepley 
56*cf4936a9SMatthew G. Knepley   # Refine the cube three times
57*cf4936a9SMatthew G. Knepley   test:
58*cf4936a9SMatthew G. Knepley     suffix: 4
59*cf4936a9SMatthew G. Knepley     requires: ctetgen pyvista
60*cf4936a9SMatthew G. Knepley     args: -dm_view pyvista -dm_plex_dim 3 -dm_refine 3
61*cf4936a9SMatthew G. Knepley     output_file: output/ex2_0.out
62*cf4936a9SMatthew G. Knepley 
63*cf4936a9SMatthew G. Knepley   # Draw a sphere with PyVista (all we get is an icosahedron)
64*cf4936a9SMatthew G. Knepley   test:
65*cf4936a9SMatthew G. Knepley     suffix: 5
66*cf4936a9SMatthew G. Knepley     requires: pyvista
67*cf4936a9SMatthew G. Knepley     args: -dm_view pyvista -dm_plex_shape sphere
68*cf4936a9SMatthew G. Knepley     output_file: output/ex2_0.out
69*cf4936a9SMatthew G. Knepley 
70*cf4936a9SMatthew G. Knepley   # Refine the sphere three times
71*cf4936a9SMatthew G. Knepley   test:
72*cf4936a9SMatthew G. Knepley     suffix: 6
73*cf4936a9SMatthew G. Knepley     requires: pyvista
74*cf4936a9SMatthew G. Knepley     args: -dm_view pyvista -dm_plex_shape sphere -dm_refine 3
75*cf4936a9SMatthew G. Knepley     output_file: output/ex2_0.out
76*cf4936a9SMatthew G. Knepley 
77*cf4936a9SMatthew G. Knepley   # Show the 3-sphere
78*cf4936a9SMatthew G. Knepley   test:
79*cf4936a9SMatthew G. Knepley     suffix: 7
80*cf4936a9SMatthew G. Knepley     args: -dm_view -dm_plex_shape sphere -dm_plex_dim 3
81*cf4936a9SMatthew G. Knepley 
82*cf4936a9SMatthew G. Knepley   # Extrude the sphere
83*cf4936a9SMatthew G. Knepley   test:
84*cf4936a9SMatthew G. Knepley     suffix: 8
85*cf4936a9SMatthew G. Knepley     requires: pyvista
86*cf4936a9SMatthew G. Knepley     args: -dm_view pyvista -dm_plex_shape sphere -dm_refine 1 \
87*cf4936a9SMatthew G. Knepley           -dm_plex_transform_type extrude -dm_plex_transform_extrude_layers 3 \
88*cf4936a9SMatthew G. Knepley             -dm_plex_transform_extrude_use_tensor 0 -dm_plex_transform_extrude_thickness 0.3
89*cf4936a9SMatthew G. Knepley     output_file: output/ex2_0.out
90*cf4936a9SMatthew G. Knepley 
91*cf4936a9SMatthew G. Knepley   # Extrude the sphere with cutaway
92*cf4936a9SMatthew G. Knepley   test:
93*cf4936a9SMatthew G. Knepley     suffix: 9
94*cf4936a9SMatthew G. Knepley     requires: pyvista
95*cf4936a9SMatthew G. Knepley     args: -dm_view pyvista -view_pyvista_clip 1.,0.,0. -dm_plex_shape sphere -dm_refine 1 \
96*cf4936a9SMatthew G. Knepley           -dm_plex_transform_type extrude -dm_plex_transform_extrude_layers 3 \
97*cf4936a9SMatthew G. Knepley             -dm_plex_transform_extrude_use_tensor 0 -dm_plex_transform_extrude_thickness 0.3
98*cf4936a9SMatthew G. Knepley     output_file: output/ex2_0.out
99*cf4936a9SMatthew G. Knepley 
100*cf4936a9SMatthew G. Knepley   # Extrude the refined sphere
101*cf4936a9SMatthew G. Knepley   test:
102*cf4936a9SMatthew G. Knepley     suffix: 10
103*cf4936a9SMatthew G. Knepley     requires: pyvista
104*cf4936a9SMatthew G. Knepley     args: -dm_view pyvista -view_pyvista_clip 1.,0.,0. -dm_plex_shape sphere -dm_refine 3 \
105*cf4936a9SMatthew G. Knepley           -dm_plex_option_phases ext_ \
106*cf4936a9SMatthew G. Knepley             -ext_dm_refine 1 -ext_dm_plex_transform_type extrude -ext_dm_plex_transform_extrude_layers 3 \
107*cf4936a9SMatthew G. Knepley               -ext_dm_plex_transform_extrude_use_tensor 0 -ext_dm_plex_transform_extrude_thickness 0.5
108*cf4936a9SMatthew G. Knepley     output_file: output/ex2_0.out
109*cf4936a9SMatthew G. Knepley 
110*cf4936a9SMatthew G. Knepley   # Extrude the refined sphere
111*cf4936a9SMatthew G. Knepley   test:
112*cf4936a9SMatthew G. Knepley     suffix: 11
113*cf4936a9SMatthew G. Knepley     requires: pyvista
114*cf4936a9SMatthew G. Knepley     args: -dm_view pyvista -view_pyvista_clip 1.,0.,0. -dm_plex_shape sphere -dm_refine 3 \
115*cf4936a9SMatthew G. Knepley           -dm_plex_option_phases ext_,ref_ \
116*cf4936a9SMatthew G. Knepley             -ext_dm_refine 1 -ext_dm_plex_transform_type extrude -ext_dm_plex_transform_extrude_layers 3 \
117*cf4936a9SMatthew G. Knepley               -ext_dm_plex_transform_extrude_use_tensor 0 -ext_dm_plex_transform_extrude_thickness 0.5 \
118*cf4936a9SMatthew G. Knepley             -ref_dm_refine 1
119*cf4936a9SMatthew G. Knepley     output_file: output/ex2_0.out
120*cf4936a9SMatthew G. Knepley 
121*cf4936a9SMatthew G. Knepley   # Refine the Schwartz surface
122*cf4936a9SMatthew G. Knepley   test:
123*cf4936a9SMatthew G. Knepley     suffix: 12
124*cf4936a9SMatthew G. Knepley     requires: pyvista
125*cf4936a9SMatthew G. Knepley     args: -dm_view pyvista -dm_plex_shape schwarz_p -dm_plex_tps_extent 3,2 -dm_plex_tps_refine 2
126*cf4936a9SMatthew G. Knepley     output_file: output/ex2_0.out
127*cf4936a9SMatthew G. Knepley 
128*cf4936a9SMatthew G. Knepley   # Refine and extrude the Schwartz surface with given thickness
129*cf4936a9SMatthew G. Knepley   test:
130*cf4936a9SMatthew G. Knepley     suffix: 13
131*cf4936a9SMatthew G. Knepley     requires: pyvista
132*cf4936a9SMatthew G. Knepley     args: -dm_view pyvista -dm_plex_shape schwarz_p -dm_plex_tps_extent 3,2,2 -dm_plex_tps_refine 3 \
133*cf4936a9SMatthew G. Knepley             -dm_plex_tps_thickness 0.4 -dm_plex_tps_layers 3
134*cf4936a9SMatthew G. Knepley     output_file: output/ex2_0.out
135*cf4936a9SMatthew G. Knepley 
136*cf4936a9SMatthew G. Knepley   # Filter the square
137*cf4936a9SMatthew G. Knepley   test:
138*cf4936a9SMatthew G. Knepley     suffix: 14
139*cf4936a9SMatthew G. Knepley     requires: triangle pyvista
140*cf4936a9SMatthew G. Knepley     args: -dm_view pyvista -dm_refine 1 \
141*cf4936a9SMatthew G. Knepley           -dm_plex_transform_type transform_filter -dm_plex_transform_active filter_cells -dm_plex_label_filter_cells 0,1,2
142*cf4936a9SMatthew G. Knepley     output_file: output/ex2_0.out
143*cf4936a9SMatthew G. Knepley 
144*cf4936a9SMatthew G. Knepley   # Filter a cap on the sphere
145*cf4936a9SMatthew G. Knepley   test:
146*cf4936a9SMatthew G. Knepley     suffix: 15
147*cf4936a9SMatthew G. Knepley     requires: pyvista
148*cf4936a9SMatthew G. Knepley     args: -dm_view pyvista -dm_plex_shape sphere -dm_refine 4 \
149*cf4936a9SMatthew G. Knepley           -dm_plex_option_phases filt_ \
150*cf4936a9SMatthew G. Knepley             -filt_dm_refine 1 -filt_dm_plex_transform_type transform_filter \
151*cf4936a9SMatthew G. Knepley               -filt_dm_plex_transform_active filter_cells -dm_plex_label_filter_cells 0
152*cf4936a9SMatthew G. Knepley     output_file: output/ex2_0.out
153*cf4936a9SMatthew G. Knepley 
154*cf4936a9SMatthew G. Knepley   # Filter the sphere minus a cap
155*cf4936a9SMatthew G. Knepley   test:
156*cf4936a9SMatthew G. Knepley     suffix: 16
157*cf4936a9SMatthew G. Knepley     requires: pyvista
158*cf4936a9SMatthew G. Knepley     args: -dm_view pyvista -dm_plex_shape sphere -dm_refine 4 \
159*cf4936a9SMatthew G. Knepley           -dm_plex_option_phases filt_ \
160*cf4936a9SMatthew G. Knepley             -filt_dm_refine 1 -filt_dm_plex_transform_type transform_filter \
161*cf4936a9SMatthew G. Knepley               -filt_dm_plex_transform_active filter_cells \
162*cf4936a9SMatthew G. Knepley               -dm_plex_label_filter_cells 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18
163*cf4936a9SMatthew G. Knepley     output_file: output/ex2_0.out
164*cf4936a9SMatthew G. Knepley 
165*cf4936a9SMatthew G. Knepley   # Convert sphere to quads
166*cf4936a9SMatthew G. Knepley   test:
167*cf4936a9SMatthew G. Knepley     suffix: 17
168*cf4936a9SMatthew G. Knepley     requires: pyvista
169*cf4936a9SMatthew G. Knepley     args: -dm_view pyvista -dm_plex_shape sphere -dm_refine 3 \
170*cf4936a9SMatthew G. Knepley           -dm_plex_option_phases conv_ -conv_dm_refine 1 -conv_dm_plex_transform_type refine_tobox
171*cf4936a9SMatthew G. Knepley     output_file: output/ex2_0.out
172*cf4936a9SMatthew G. Knepley 
173*cf4936a9SMatthew G. Knepley   # Load and refine the nozzle
174*cf4936a9SMatthew G. Knepley   test:
175*cf4936a9SMatthew G. Knepley     suffix: 18
176*cf4936a9SMatthew G. Knepley     requires: pyvista egads datafilespath
177*cf4936a9SMatthew G. Knepley     args: -dm_view pyvista -dm_plex_filename /Users/knepley/PETSc4/petsc/petsc-dev/share/petsc/datafiles/meshes/nozzle.stp -dm_refine 3
178*cf4936a9SMatthew G. Knepley     output_file: output/ex2_0.out
179*cf4936a9SMatthew G. Knepley 
180*cf4936a9SMatthew G. Knepley   # Load and refine the nozzle, and convert to quads
181*cf4936a9SMatthew G. Knepley   test:
182*cf4936a9SMatthew G. Knepley     suffix: 19
183*cf4936a9SMatthew G. Knepley     requires: pyvista egads datafilespath
184*cf4936a9SMatthew G. Knepley     args: -dm_view pyvista -dm_plex_filename /Users/knepley/PETSc4/petsc/petsc-dev/share/petsc/datafiles/meshes/nozzle.stp -dm_refine 3 \
185*cf4936a9SMatthew G. Knepley           -dm_plex_option_phases conv_ -conv_dm_refine 1 -conv_dm_plex_transform_type refine_tobox
186*cf4936a9SMatthew G. Knepley     output_file: output/ex2_0.out
187*cf4936a9SMatthew G. Knepley 
188*cf4936a9SMatthew G. Knepley TEST*/
189