xref: /petsc/doc/tutorials/meshing/guide_to_subsurface.md (revision aa9a5b67dde7169d6e428ea38b8433e2afcb1019)
1*aa9a5b67SBarry Smith# Meshing for Subsurface Flows in PETSc
2*aa9a5b67SBarry Smith
3*aa9a5b67SBarry SmithThis tutorials guides users in creating meshes for the TDyCore simulation framework for subsurface flows. The user inputs a surface mesh, a refinement prescription, and an extrusion prescription in order to create the simulation mesh.
4*aa9a5b67SBarry Smith
5*aa9a5b67SBarry SmithReading the ASCII Output
6*aa9a5b67SBarry Smith
7*aa9a5b67SBarry SmithFor example, a very simple mesh would start with a square surface mesh divided into two triangles, which is then extruded to form two triangular prisms. This is the first test in the DMPlex tutorial code ex10,
8*aa9a5b67SBarry Smith
9*aa9a5b67SBarry Smith```console
10*aa9a5b67SBarry Smith$ make -f ./gmakefile test globsearch="dm_impls_plex_tutorials-ex10_0"
11*aa9a5b67SBarry Smith```
12*aa9a5b67SBarry Smith
13*aa9a5b67SBarry Smithwhich outputs
14*aa9a5b67SBarry Smith
15*aa9a5b67SBarry Smith```{literalinclude} /../src/dm/impls/plex/tutorials/output/ex10_0.out
16*aa9a5b67SBarry Smith```
17*aa9a5b67SBarry Smith
18*aa9a5b67SBarry SmithWe can see that there are two 3-cells, meaning three-dimensional cells, and from the `celltype` label we see that those cells have celltype 9, meaning they are triangular prisms. The original surface mesh had 5 edges, so we would expect 10 edges for the two surfaces and four edges connecting those surfaces. This is exactly what we see, since there are 14 1-cells, but 4 of them noted in parentheses are tensor cells created by extrusion. We can see this another way in the celltype label, where there are ten mesh points of type 1, meaning segments, and four mesh points of type 2, meaning tensor products of a vertex and segment. Similarly, there are 9 2-cells, but 5 of them stretch between the two surfaces, meaning they are tensor products of two segments.
19*aa9a5b67SBarry Smith
20*aa9a5b67SBarry SmithRegular Refinement of Simplex Meshes
21*aa9a5b67SBarry Smith
22*aa9a5b67SBarry SmithWe can regularly refine the surface before extrusion using `-dm_refine <k>`, where `k` is the number of refinements,
23*aa9a5b67SBarry Smith
24*aa9a5b67SBarry Smith```console
25*aa9a5b67SBarry Smith$ make -f ./gmakefile test globsearch="dm_impls_plex_tutorials-ex10_1" EXTRA_OPTIONS="-srf_dm_refine 2 -srf_dm_view draw -draw_save $PETSC_DIR/surface.png -draw_save_single_file"
26*aa9a5b67SBarry Smith```
27*aa9a5b67SBarry Smith
28*aa9a5b67SBarry Smithwhich produces the following surface
29*aa9a5b67SBarry Smith
30*aa9a5b67SBarry Smith:::{figure} /images/tutorials/meshing/surface.png
31*aa9a5b67SBarry Smith:align: center
32*aa9a5b67SBarry Smith
33*aa9a5b67SBarry Smith**Surface mesh refined twice**
34*aa9a5b67SBarry Smith:::
35*aa9a5b67SBarry Smith
36*aa9a5b67SBarry Smithand the extruded mesh can be visualized using VTK. Here I make the image using Paraview, and give the extrusion 3 layers
37*aa9a5b67SBarry Smith
38*aa9a5b67SBarry Smith```console
39*aa9a5b67SBarry Smith$ make -f ./gmakefile test globsearch="dm_impls_plex_tutorials-ex10_1" EXTRA_OPTIONS="-dm_view hdf5:$PETSC_DIR/mesh.h5 -dm_extrude 3"
40*aa9a5b67SBarry Smith$ $PETSC_DIR/lib/petsc/bin/petsc_gen_xmdf.py mesh.h5
41*aa9a5b67SBarry Smith```
42*aa9a5b67SBarry Smith
43*aa9a5b67SBarry Smith:::{figure} /images/tutorials/meshing/extrusion.png
44*aa9a5b67SBarry Smith:align: center
45*aa9a5b67SBarry Smith
46*aa9a5b67SBarry Smith**Extruded mesh with refined surface**
47*aa9a5b67SBarry Smith:::
48*aa9a5b67SBarry Smith
49*aa9a5b67SBarry SmithWe can similarly look at this in parallel. Test 2 uses three refinements and three extrusion layers on five processes
50*aa9a5b67SBarry Smith
51*aa9a5b67SBarry Smith```console
52*aa9a5b67SBarry Smith$ make -f ./gmakefile test globsearch="dm_impls_plex_tutorials-ex10_2" EXTRA_OPTIONS="-dm_view hdf5:$PETSC_DIR/mesh.h5 -dm_partition_view -petscpartitioner_type parmetis"
53*aa9a5b67SBarry Smith$ $PETSC_DIR/lib/petsc/bin/petsc_gen_xmdf.py mesh.h5
54*aa9a5b67SBarry Smith```
55*aa9a5b67SBarry Smith
56*aa9a5b67SBarry Smith:::{figure} /images/tutorials/meshing/extrusionParallel.png
57*aa9a5b67SBarry Smith:align: center
58*aa9a5b67SBarry Smith
59*aa9a5b67SBarry Smith**Parallel extruded mesh with refined surface**
60*aa9a5b67SBarry Smith:::
61*aa9a5b67SBarry Smith
62*aa9a5b67SBarry SmithAdaptive Refinement of Simplex Meshes
63*aa9a5b67SBarry Smith
64*aa9a5b67SBarry SmithAdaptive refinement of simplicial meshes is somewhat tricky when we demand that the meshes be conforming, as we do in this case. We would like different grid cells to have different levels of refinement, for example headwaters cells in a watershed be refined twice, while river channel cells be refined four times. In order to differentiate between cells, we first mark the cells on the surface using a `DMLabel`. We can do this programmatically,
65*aa9a5b67SBarry Smith
66*aa9a5b67SBarry Smith```{literalinclude} /../src/dm/impls/plex/tutorials/ex10.c
67*aa9a5b67SBarry Smith:append: '}'
68*aa9a5b67SBarry Smith:end-at: PetscFunctionReturn(PETSC_SUCCESS);
69*aa9a5b67SBarry Smith:start-at: static PetscErrorCode CreateDomainLabel(
70*aa9a5b67SBarry Smith```
71*aa9a5b67SBarry Smith
72*aa9a5b67SBarry Smithor you can label the mesh using a GUI, such as GMsh, and PETSc will read the label values from the input file.
73*aa9a5b67SBarry Smith
74*aa9a5b67SBarry SmithWe next create a label marking each cell in the mesh with an action, such as `DM_ADAPT_REFINE` or `DM_ADAPT_COARSEN`. We do this based on a volume constraint, namely that cells with a certain label value should have a certain volume. You could, of course, choose a more complex strategy, but here we just want a clear criterion. We can give volume constraints for label value `v` using the command line argument `-volume_constraint_<v> <vol>`. The mesh is then refined iteratively, checking the volume constraints each time,
75*aa9a5b67SBarry Smith
76*aa9a5b67SBarry Smith```{literalinclude} /../src/dm/impls/plex/tutorials/ex10.c
77*aa9a5b67SBarry Smith:append: '}'
78*aa9a5b67SBarry Smith:end-at: PetscCall(DMLabelDestroy(&adaptLabel));
79*aa9a5b67SBarry Smith:start-at: while (adapt) {
80*aa9a5b67SBarry Smith```
81*aa9a5b67SBarry Smith
82*aa9a5b67SBarry SmithTest 3 from `ex10` constrains the headwater cells (with marker 1) to have volume less than 0.01, and the river channel cells (with marker 2) to be smaller than 0.000625
83*aa9a5b67SBarry Smith
84*aa9a5b67SBarry Smith```{literalinclude} /../src/dm/impls/plex/tutorials/ex10.c
85*aa9a5b67SBarry Smith:lines: 1-3
86*aa9a5b67SBarry Smith:start-at: 'suffix: 3'
87*aa9a5b67SBarry Smith```
88*aa9a5b67SBarry Smith
89*aa9a5b67SBarry SmithWe can look at a parallel run using extra options for the test system
90*aa9a5b67SBarry Smith
91*aa9a5b67SBarry Smith```console
92*aa9a5b67SBarry Smith$ make -f ./gmakefile test globsearch="dm_impls_plex_tutorials-ex10_3" EXTRA_OPTIONS="-dm_view hdf5:$PETSC_DIR/mesh.h5 -dm_partition_view -petscpartitioner_type parmetis" NP=5
93*aa9a5b67SBarry Smith$ $PETSC_DIR/lib/petsc/bin/petsc_gen_xmdf.py mesh.h5
94*aa9a5b67SBarry Smith```
95*aa9a5b67SBarry Smith
96*aa9a5b67SBarry Smith:::{figure} /images/tutorials/meshing/extrusionAdaptiveParallel.png
97*aa9a5b67SBarry Smith:align: center
98*aa9a5b67SBarry Smith
99*aa9a5b67SBarry Smith**Parallel extruded mesh with adaptively refined surface**
100*aa9a5b67SBarry Smith:::
101*aa9a5b67SBarry Smith
102*aa9a5b67SBarry SmithBy turning on `PetscInfo`, we can see what decisions the refiner is making
103*aa9a5b67SBarry Smith
104*aa9a5b67SBarry Smith```console
105*aa9a5b67SBarry Smith$ make -f ./gmakefile test globsearch="dm_impls_plex_tutorials-ex10_3" EXTRA_OPTIONS="-info :dm"
106*aa9a5b67SBarry Smith#       > [0] AdaptMesh(): Adapted mesh, marking 12 cells for refinement, and 0 cells for coarsening
107*aa9a5b67SBarry Smith#       > [0] AdaptMesh(): Adapted mesh, marking 29 cells for refinement, and 0 cells for coarsening
108*aa9a5b67SBarry Smith#       > [0] AdaptMesh(): Adapted mesh, marking 84 cells for refinement, and 0 cells for coarsening
109*aa9a5b67SBarry Smith#       > [0] AdaptMesh(): Adapted mesh, marking 10 cells for refinement, and 0 cells for coarsening
110*aa9a5b67SBarry Smith```
111