xref: /petsc/doc/manual/dmstag.md (revision b59054469e8e75e7f1217f4456448261033ebd2d)
17f296bb3SBarry Smith(ch_stag)=
27f296bb3SBarry Smith
37f296bb3SBarry Smith# DMSTAG: Staggered, Structured Grid
47f296bb3SBarry Smith
5*dac9a9d1SBarry SmithFor structured (aka "regular") grids with staggered data (degrees of freedom potentially living on elements, faces, edges,
67f296bb3SBarry Smithand/or vertices), the `DMSTAG` object is available. This can
77f296bb3SBarry Smithbe useful for problems in many domains, including fluid flow, MHD, and seismology.
87f296bb3SBarry Smith
9*dac9a9d1SBarry SmithIt is possible, though extremely cumbersome, to implement a staggered-grid code using multiple `DMDA` objects, or a single
10*dac9a9d1SBarry Smithmulti-component `DMDA` object where some degrees of freedom are unused.
117f296bb3SBarry Smith`DMSTAG` was developed for two main purposes:
127f296bb3SBarry Smith
137f296bb3SBarry Smith1. To help manage some of the burden of choosing and adhering to the complex indexing conventions needed for staggered grids (in parallel)
147f296bb3SBarry Smith2. To provide a uniform abstraction for which scalable solvers and preconditioners may be developed (in particular, using `PCFIELDSPLIT` and `PCMG`).
157f296bb3SBarry Smith
167f296bb3SBarry Smith`DMSTAG` is design to behave much
177f296bb3SBarry Smithlike {ref}`DMDA <sec_struct>`, with a couple of important distinctions, and borrows some terminology
187f296bb3SBarry Smithfrom {doc}`DMPLEX <dmplex>`.
197f296bb3SBarry Smith
207f296bb3SBarry Smith## Terminology
217f296bb3SBarry Smith
227f296bb3SBarry SmithLike a `DMPLEX` object, a `DMSTAG` represents a [cell complex](https://en.wikipedia.org/wiki/CW_complex),
237f296bb3SBarry Smithdistributed in parallel over the ranks of an `MPI_Comm`. It is, however,
247f296bb3SBarry Smitha very regular complex, consisting of a structured grid of $d$-dimensional cells, with $d \in \{1,2,3\}$,
257f296bb3SBarry Smithwhich are referred to as *elements*, $d-1$ dimensional cells defining boundaries between these elements,
267f296bb3SBarry Smithand the boundaries of the domain, and in 2 or more dimensions, boundaries of *these* cells,
277f296bb3SBarry Smithall the way down to 0 dimensional cells referred to as *vertices*. In 2 dimensions, the 1-dimensional
287f296bb3SBarry Smithelement boundaries are referred to as *edges* or *faces*. In 3 dimensions, the 2-dimensional element boundaries
297f296bb3SBarry Smithare referred to as *faces* and the 1-dimensional boundaries between faces are referred to as *edges*
307f296bb3SBarry SmithThe set of cells of a given dimension is referred to as a *stratum* (which one can think of as a level in DAG representation of the mesh); a `DMSTAG` object of dimension $d$
317f296bb3SBarry Smithrepresents a complete cell complex with $d+1$ *strata* (levels).
327f296bb3SBarry Smith
33*dac9a9d1SBarry SmithIn the description of any:`ch_unstructured` the cells at each level are referred to as *points*. Thus we adopt that terminology uniformly in PETSc and
34*dac9a9d1SBarry Smithso furthermore in this document,
357f296bb3SBarry Smithpoint will refer to a cell.
367f296bb3SBarry Smith
377f296bb3SBarry SmithEach stratum has a constant number of unknowns (which may be zero) associated with each point (cell) on that level.
38*dac9a9d1SBarry SmithThe distinct unknowns associated with each point are referred to as *components*. For a `DMPLEX` there may be a different
39*dac9a9d1SBarry Smithnumber of unknowns for each point on the same level.
407f296bb3SBarry Smith
417f296bb3SBarry SmithThe structured grid, is like with `DMDA`, decomposed via a Cartesian product of decompositions in each dimension,
427f296bb3SBarry Smithgiving a rectangular local subdomain on each rank. This is extended by an element-wise stencil width
437f296bb3SBarry Smithof *ghost* elements to create an atlas of overlapping patches.
447f296bb3SBarry Smith
457f296bb3SBarry Smith## Working with vectors and operators (matrices)
467f296bb3SBarry Smith
477f296bb3SBarry Smith`DMSTAG` allows the user to reason almost entirely about a global indexing of elements.
487f296bb3SBarry SmithElement indices are simply 1-3 `PetscInt` values, starting at $0$, in the
497f296bb3SBarry Smithback, bottom, left corner of the domain. For instance, element $(1,2,3)$, in 3D,
507f296bb3SBarry Smithis the element second from the left, third from the bottom, and fourth from the back
517f296bb3SBarry Smith(regardless of how many MPI ranks are used).
527f296bb3SBarry Smith
537f296bb3SBarry SmithTo refer to points (elements, faces, edges, and vertices), a value of
547f296bb3SBarry Smith`DMStagStencilLocation` is used, relative to the element index. The element
557f296bb3SBarry Smithitself is referred to with `DMSTAG_ELEMENT`, the top right vertex (in 2D)
567f296bb3SBarry Smithor the top right edge (in 3D) with `DMSTAG_UP_RIGHT`, the back bottom left
577f296bb3SBarry Smithcorner in 3D with `DMSTAG_BACK_DOWN_LEFT`, and so on.
587f296bb3SBarry Smith
597f296bb3SBarry Smith{numref}`figure_dmstag_indexing` gives a few examples in 2D.
607f296bb3SBarry Smith
617f296bb3SBarry Smith:::{figure} /images/manual/dmstag_indexing.svg
627f296bb3SBarry Smith:name: figure_dmstag_indexing
637f296bb3SBarry Smith
647f296bb3SBarry SmithLocations in `DMSTAG` are indexed according to global element indices (here, two in 2D) and a location name. Elements have unique names but other locations can be referred to in more than one way. Element colors correspond to a parallel decomposition, but locations on the grid have names which are invariant to this. Note that the face on the top right can be referred to as being to the left of a "dummy" element $(3,3)$ outside the physical domain.
657f296bb3SBarry Smith:::
667f296bb3SBarry Smith
677f296bb3SBarry SmithCrucially, this global indexing scheme does not include any "ghost" or "padding" unknowns outside the physical domain.
687f296bb3SBarry SmithThis is useful for higher-level operations such as computing norms or developing physics-based solvers. However
697f296bb3SBarry Smith(unlike `DMDA`), this implies that the global `Vec` do not have a natural block structure, as different
707f296bb3SBarry Smithstrata have different numbers of points (e.g. in 1D there is an "extra" vertex on the right). This regular block
717f296bb3SBarry Smithstructure is, however, very useful for the *local* representation of the data, so in that case *dummy* DOF
727f296bb3SBarry Smithare included, drawn as grey in {numref}`figure_dmstag_local_global`.
737f296bb3SBarry Smith
747f296bb3SBarry Smith:::{figure} /images/manual/dmstag_local_global.svg
757f296bb3SBarry Smith:name: figure_dmstag_local_global
767f296bb3SBarry Smith
777f296bb3SBarry SmithLocal and global representations for a 2D `DMSTAG` object, 3 by 4 elements, with one degree of freedom on each of the three strata: element (squares), faces (triangles), and vertices (circles). The cell complex is parallelized across 4 MPI ranks. In the global representation, the colors correspond to which rank holds the native representation of the unknown. The 4 local representations are shown, with an (elementwise) stencil "box" stencil width of 1. Unknowns are colored by their native rank. Dummy unknowns, which correspond to no global degree of freedom, are colored grey. Note that the local representations have a natural block size of 4, and the global representation has no natural block size.
787f296bb3SBarry Smith:::
797f296bb3SBarry Smith
807f296bb3SBarry SmithFor working with `Vec` data, this approach is used to allow direct access to a multi-dimensional, regular-blocked
817f296bb3SBarry Smitharray. To avoid the user having to know about the {ref}`internal numbering conventions used <sec_dmstag_numbering>`,
827f296bb3SBarry Smithhelper functions are used to produce the proper final integer index for a given location and component, referred to as a "slot".
837f296bb3SBarry SmithSimilarly to `DMDAVecGetArrayDOF()`, this uses a $d+1$ dimensional array in $d$ dimensions.
847f296bb3SBarry SmithThe following snippet give an example of this usage.
857f296bb3SBarry Smith
867f296bb3SBarry Smith```{literalinclude} /../src/dm/impls/stag/tests/ex51.c
877f296bb3SBarry Smith:end-at: PetscCall(DMStagVecRestoreArray
887f296bb3SBarry Smith:start-at: /* Set
897f296bb3SBarry Smith```
907f296bb3SBarry Smith
917f296bb3SBarry Smith`DMSTAG` provides a stencil-based method for getting and setting entries of `Mat` and `Vec` objects.
927f296bb3SBarry SmithThe follow excerpt from <a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/dm/impls/stag/tutorials/ex1.c.html">DMSTAG Tutorial ex1</a> demonstrates
937f296bb3SBarry Smiththe idea. For more, see the manual page for `DMStagMatSetValuesStencil()`.
947f296bb3SBarry Smith
957f296bb3SBarry Smith```{literalinclude} /../src/dm/impls/stag/tutorials/ex1.c
967f296bb3SBarry Smith:end-at: PetscCall(DMStagMatSetValuesStencil
977f296bb3SBarry Smith:start-at: /* Velocity is either a BC
987f296bb3SBarry Smith```
997f296bb3SBarry Smith
1007f296bb3SBarry SmithThe array-based approach for `Vec` is likely to be more efficient than the stencil-based method just introduced above.
1017f296bb3SBarry Smith
1027f296bb3SBarry Smith## Coordinates
1037f296bb3SBarry Smith
1047f296bb3SBarry Smith`DMSTAG`, unlike `DMDA`, supports two approaches to defining coordinates. This is captured by which type of `DM`
1057f296bb3SBarry Smithis used to represent the coordinates. No default is imposed, so the user must directly or indirectly call
1067f296bb3SBarry Smith`DMStagSetCoordinateDMType()`.
1077f296bb3SBarry Smith
1087f296bb3SBarry SmithIf a second `DMSTAG` object is used to represent coordinates in "explicit" form, behavior is much like with `DMDA` - the coordinate `DM`
1097f296bb3SBarry Smithhas $d$ DOF on each stratum corresponding to coordinates associated with each point.
1107f296bb3SBarry Smith
1117f296bb3SBarry SmithIf `DMPRODUCT` is used instead, coordinates are represented by a `DMPRODUCT` object referring to a
1127f296bb3SBarry SmithCartesian product of 1D `DMSTAG` objects, each of which features explicit coordinates as just mentioned.
1137f296bb3SBarry Smith
1147f296bb3SBarry SmithNavigating these nested `DM` in `DMPRODUCT` can be tedious, but note the existence of helper functions like
1157f296bb3SBarry Smith`DMStagSetUniformCoordinatesProduct()` and `DMStagGetProductCoordinateArrays()`.
1167f296bb3SBarry Smith
1177f296bb3SBarry Smith(sec_dmstag_numbering)=
1187f296bb3SBarry Smith
1197f296bb3SBarry Smith## Numberings and internal data layout
1207f296bb3SBarry Smith
1217f296bb3SBarry SmithWhile `DMSTAG` aims to hide the details of its internal data layout, for debugging, optimization, and
1227f296bb3SBarry Smithcustomization purposes, it can be important to know how `DMSTAG` internally numbers unknowns.
1237f296bb3SBarry Smith
1247f296bb3SBarry SmithInternally, each point is canonically associated with an element (top-level point (cell)). For purposes of local,
1257f296bb3SBarry Smithregular-blocked storage, an element is grouped with lower-dimensional points left of, below ("down"), and behind ("back") it.
1267f296bb3SBarry SmithThis means that "canonical" values of `DMStagStencilLocation` are `DMSTAG_ELEMENT`, plus all entries consisting only of "LEFT", "DOWN", and "BACK". In general, these are the most efficient values to use, unless convenience dictates otherwise, as they are the ones used internally.
1277f296bb3SBarry Smith
1287f296bb3SBarry SmithWhen creating the decomposition of the domain to local ranks, and extending these local domains to handle overlapping halo regions and boundary ghost unknowns, this same per-element association is used. This has the advantage of maintaining a regular blocking, but may not be optimal in some situations in terms of data movement.
1297f296bb3SBarry Smith
1307f296bb3SBarry SmithNumberings are, like `DMDA`, based on a local "x-fastest, z-slowest" or "PETSc" ordering of elements (see {ref}`sec_ao`), with ordering of locations canonically associated with each element decided by considering unknowns on each point
1317f296bb3SBarry Smithto be located at the center of their point, and using a nested ordering of the same style. Thus, in 3-D, the ordering of the 8 canonical `DMStagStencilLocation` values associated
1327f296bb3SBarry Smithwith an element is
1337f296bb3SBarry Smith
1347f296bb3SBarry Smith```
1357f296bb3SBarry SmithDMSTAG_BACK_DOWN_LEFT
1367f296bb3SBarry SmithDMSTAG_BACK_DOWN
1377f296bb3SBarry SmithDMSTAG_BACK_LEFT
1387f296bb3SBarry SmithDMSTAG_BACK
1397f296bb3SBarry SmithDMSTAG_DOWN_LEFT
1407f296bb3SBarry SmithDMSTAG_DOWN
1417f296bb3SBarry SmithDMSTAG_LEFT
1427f296bb3SBarry SmithDMSTAG_ELEMENT
1437f296bb3SBarry Smith```
1447f296bb3SBarry Smith
1457f296bb3SBarry SmithMultiple DOF associated with a given point are stored sequentially (as with `DMDA`).
1467f296bb3SBarry Smith
1477f296bb3SBarry SmithFor local `Vec`s, this gives a regular-blocked numbering, with the same number of unknowns associated with each element, including some "dummy" unknowns which to not correspond to any (local or global) unknown in the global representation. See {numref}`figure_dmstag_numbering_local` for an example.
1487f296bb3SBarry Smith
1497f296bb3SBarry SmithIn the global representation, only physical unknowns are numbered (using the same "Z" ordering for unknowns which are present), giving irregular numbers of unknowns, depending on whether a domain boundary is present. See {numref}`figure_dmstag_numbering_global` for an example.
1507f296bb3SBarry Smith
1517f296bb3SBarry Smith:::{figure} /images/manual/dmstag_numbering_global.svg
1527f296bb3SBarry Smith:name: figure_dmstag_numbering_global
1537f296bb3SBarry Smith
1547f296bb3SBarry SmithGlobal numbering scheme for a 2D `DMSTAG` object with one DOF per stratum. Note that the numbering depends on the parallel decomposition (over 4 ranks, here).
1557f296bb3SBarry Smith:::
1567f296bb3SBarry Smith
1577f296bb3SBarry Smith:::{figure} /images/manual/dmstag_numbering_local.svg
1587f296bb3SBarry Smith:name: figure_dmstag_numbering_local
1597f296bb3SBarry Smith
1607f296bb3SBarry SmithLocal numbering scheme on rank 1 (Cf. {numref}`figure_dmstag_local_global`) for a 2D `DMSTAG` object with one DOF per stratum. Note that dummy locations (grey) are used to give a regular block size (here, 4).
1617f296bb3SBarry Smith:::
1627f296bb3SBarry Smith
1637f296bb3SBarry SmithIt should be noted that this is an *interlaced* (AoS) representation. If a segregated (SoA) representation is required,
1647f296bb3SBarry Smithone should use `DMCOMPOSITE` collecting several `DMSTAG` objects, perhaps using `DMStagCreateCompatibleDMStag()` to
1657f296bb3SBarry Smithquickly create additional `DMSTAG` objects from an initial one.
166