xref: /petsc/doc/manual/section.md (revision b5f0bcd6e9e8ed97648738542f5163d94f7b1782)
17f296bb3SBarry Smith(ch_petscsection)=
27f296bb3SBarry Smith
37f296bb3SBarry Smith# PetscSection: Connecting Grids to Data
47f296bb3SBarry Smith
57f296bb3SBarry SmithThe strongest links between solvers and discretizations are
67f296bb3SBarry Smith
77f296bb3SBarry Smith- the relationship between the layout of data over a mesh (or similar structure) and the data layout in arrays and `Vec` used for computation,
87f296bb3SBarry Smith- data partitioning, and
97f296bb3SBarry Smith- ordering of data.
107f296bb3SBarry Smith
117f296bb3SBarry SmithTo enable modularity, we encode the operations above in simple data
1234b254c5SRichard Tran Millsstructures that can be understood by the linear algebraic and solver components of PETSc (`Vec`, `Mat`, `KSP`, `PC`, `SNES`, `TS`, `Tao`, `PetscRegressor`)
137f296bb3SBarry Smithwithout explicit reference to the mesh (topology) or discretization (analysis).
147f296bb3SBarry Smith
157f296bb3SBarry SmithWhile `PetscSection` is currently only employed for `DMPlex`, `DMForest`, and `DMNetwork` mesh descriptions, much of its operation is general enough to be utilized for other types of discretizations.
167f296bb3SBarry SmithThis section will explain the basic concepts of a `PetscSection` that are generalizable to other mesh descriptions.
177f296bb3SBarry Smith
187f296bb3SBarry Smith(sec_petscsection_concept)=
197f296bb3SBarry Smith
207f296bb3SBarry Smith## General concept
217f296bb3SBarry Smith
227f296bb3SBarry SmithSpecific entries (or collections of entries) in a `Vec` (or a simple array) can be associated with a "location" on a mesh (or other types of data structure) using the `PetscSection` object.
237f296bb3SBarry SmithA **point** is a `PetscInt` that serves as an abstract "index" into arrays from iterable sets, such as k-cells in a mesh.
247f296bb3SBarry SmithOther iterable set examples can be as simple as the points of a finite difference grid, or cells of a finite volume grid, or as complex as the topological entities of an unstructured mesh (cells, faces, edges, and vertices).
257f296bb3SBarry Smith
267f296bb3SBarry SmithAt it's most basic, a `PetscSection` is a mapping between the mesh points and a tuple `(ndof, offset)`, where `ndof` is the number of values stored at that mesh point and `offset` is the location in the array of that data.
277f296bb3SBarry SmithSo given the tuple for a mesh point, its data can be accessed by `array[offset + d]`, where `d` in `[0, ndof)` is the dof to access.
287f296bb3SBarry Smith
297f296bb3SBarry Smith### Charts: Defining mesh points
307f296bb3SBarry Smith
317f296bb3SBarry SmithThe mesh points for a `PetscSection` must be contiguously numbered and are defined to be in some range $[\mathrm{pStart}, \mathrm{pEnd})$, which is called a **chart**.
327f296bb3SBarry SmithThe chart of a `PetscSection` is set via `PetscSectionSetChart()`.
337f296bb3SBarry SmithNote that even though the mesh points must be contiguously numbered, the indexes into the array (defined by each `(ndof, offset)` tuple) associated with the `PetscSection` need not be.
347f296bb3SBarry SmithIn other words, there may be elements in the array that are not associated with any mesh points, though this is not often the case.
357f296bb3SBarry Smith
367f296bb3SBarry Smith### Defining the (ndof, offset) tuple
377f296bb3SBarry Smith
387f296bb3SBarry SmithDefining the `(ndof, offset)` tuple for each mesh point generally first starts with setting the `ndof` for each point, which is done using `PetscSectionSetDof()`.
397f296bb3SBarry SmithThis associates a set of degrees of freedom (dof), (a small space $\{e_k\}\ 0 < k < ndof$), with every point.
407f296bb3SBarry SmithIf `ndof` is not set for a mesh point, it is assumed to be 0.
417f296bb3SBarry Smith
427f296bb3SBarry SmithThe offset for each mesh point is usually set automatically by `PetscSectionSetUp()`.
437f296bb3SBarry SmithThis will concatenate each mesh point's dofs together in the order of the mesh points.
447f296bb3SBarry SmithThis concatenation can be done in a different order by setting a permutation, which is described in {any}`sec_petscsection_permutation`.
457f296bb3SBarry Smith
467f296bb3SBarry SmithAlternatively, the offset for each mesh point can be set manually by `PetscSectionSetOffset()`, though this is not commonly needed.
477f296bb3SBarry Smith
487f296bb3SBarry SmithOnce the tuples are created, the `PetscSection` is ready to use.
497f296bb3SBarry Smith
507f296bb3SBarry Smith### Basic Setup Example
517f296bb3SBarry Smith
527f296bb3SBarry SmithTo summarize, the sequence for constructing a basic `PetscSection` is the following:
537f296bb3SBarry Smith
547f296bb3SBarry Smith1. Specify the range of points, or chart, with `PetscSectionSetChart()`.
557f296bb3SBarry Smith2. Specify the number of dofs per point with `PetscSectionSetDof()`. Any values not set will be zero.
567f296bb3SBarry Smith3. Set up the `PetscSection` with `PetscSectionSetUp()`.
577f296bb3SBarry Smith
587f296bb3SBarry Smith## Multiple Fields
597f296bb3SBarry Smith
607f296bb3SBarry SmithIn many discretizations, it is useful to differentiate between different kinds of dofs present on a mesh.
617f296bb3SBarry SmithFor example, a dof attached to a cell point might represent pressure while dofs on vertices might represent velocity or displacement.
627f296bb3SBarry SmithA `PetscSection` can represent this additional structure with what are called **fields**.
637f296bb3SBarry Smith**Fields** are indexed contiguously from `[0, num_fields)`.
647f296bb3SBarry SmithTo set the number of fields for a `PetscSection`, call `PetscSectionSetNumFields()`.
657f296bb3SBarry Smith
667f296bb3SBarry SmithInternally, each field is stored in a separate `PetscSection`.
677f296bb3SBarry SmithIn fact, all the concepts and functions presented in {any}`sec_petscsection_concept` were actually applied onto the **default field**, which is indexed as `0`.
687f296bb3SBarry SmithThe fields inherit the same chart as the "parent" `PetscSection`.
697f296bb3SBarry Smith
707f296bb3SBarry Smith### Setting Up Multiple Fields
717f296bb3SBarry Smith
727f296bb3SBarry SmithSetup for a `PetscSection` with multiple fields is nearly identical to setup for a single field.
737f296bb3SBarry Smith
747f296bb3SBarry SmithThe sequence for constructing such a `PetscSection` is the following:
757f296bb3SBarry Smith
767f296bb3SBarry Smith1. Specify the range of points, or chart, with `PetscSectionSetChart()`. All fields share the same chart.
777f296bb3SBarry Smith2. Specify the number of fields with `PetscSectionSetNumFields()`.
787f296bb3SBarry Smith3. Set the number of dof for each point on each field with `PetscSectionSetFieldDof()`. Any values not set will be zero.
797f296bb3SBarry Smith4. Set the **total** number of dof for each point with `PetscSectionSetDof()`. Thus value must be greater than or equal to the sum of the values set with
807f296bb3SBarry Smith   `PetscSectionSetFieldDof()` at that point. Again, values not set will be zero.
817f296bb3SBarry Smith5. Set up the `PetscSection` with `PetscSectionSetUp()`.
827f296bb3SBarry Smith
837f296bb3SBarry Smith### Point Major or Field Major
847f296bb3SBarry Smith
857f296bb3SBarry SmithA `PetscSection` with one field and and offsets set in `PetscSectionSetUp()` may be thought of as defining a two dimensional array indexed by point in the outer dimension with a variable length inner dimension indexed by the dof at that point: $v[\mathrm{pStart} <= point < \mathrm{pEnd}][0 <= dof < \mathrm{ndof}]$ [^petscsection-footnote].
867f296bb3SBarry Smith
877f296bb3SBarry SmithWith multiple fields, this array is now three dimensional, with the outer dimensions being both indexed by mesh points and field points.
887f296bb3SBarry SmithThus, there is a choice on whether to index by points first, or by fields first.
897f296bb3SBarry SmithIn other words, will the array be laid out in a point-major or field-major fashion.
907f296bb3SBarry Smith
917f296bb3SBarry SmithPoint-major ordering corresponds to $v[\mathrm{pStart} <= point < \mathrm{pEnd}][0 <= field < \mathrm{num\_fields}][0 <= dof < \mathrm{ndof}]$.
927f296bb3SBarry SmithAll the dofs for each mesh point are stored contiguously, meaning the fields are **interlaced**.
937f296bb3SBarry SmithField-major ordering corresponds to $v[0 <= field < \mathrm{num\_fields}][\mathrm{pStart} <= point < \mathrm{pEnd}][0 <= dof < \mathrm{ndof}]$.
947f296bb3SBarry SmithThe all the dofs for each field are stored contiguously, meaning the points are **interlaced**.
957f296bb3SBarry Smith
967f296bb3SBarry SmithConsider a `PetscSection` with 2 fields and 2 points (from 0 to 2). Let the 0th field have `ndof=1` for each point and the 1st field have `ndof=2` for each point.
977f296bb3SBarry SmithDenote each array entry $(p_i, f_i, d_i)$ for $p_i$ being the ith point, $f_i$ being the ith field, and $d_i$ being the ith dof.
987f296bb3SBarry Smith
997f296bb3SBarry SmithPoint-major order would result in:
1007f296bb3SBarry Smith
1017f296bb3SBarry Smith$$
1027f296bb3SBarry Smith[(p_0, f_0, d_0), (p_0, f_1, d_0), (p_0, f_1, d_1),\\ (p_1, f_0, d_0), (p_1, f_1, d_0), (p_1, f_1, d_1)]
1037f296bb3SBarry Smith$$
1047f296bb3SBarry Smith
1057f296bb3SBarry SmithConversely, field-major ordering would result in:
1067f296bb3SBarry Smith
1077f296bb3SBarry Smith$$
1087f296bb3SBarry Smith[(p_0, f_0, d_0), (p_1, f_0, d_0),\\ (p_0, f_1, d_0), (p_0, f_1, d_1), (p_1, f_1, d_0), (p_1, f_1, d_1)]
1097f296bb3SBarry Smith$$
1107f296bb3SBarry Smith
1117f296bb3SBarry SmithNote that dofs are always contiguous, regardless of the outer dimensional ordering.
1127f296bb3SBarry Smith
1137f296bb3SBarry SmithSetting the which ordering is done with `PetscSectionSetPointMajor()`, where `PETSC_TRUE` sets point-major and `PETSC_FALSE` sets field major.
1147f296bb3SBarry Smith
1157f296bb3SBarry Smith**NOTE:** The current default is for point-major, and many operations on `DMPlex` will only work with this ordering. Field-major ordering is provided mainly for compatibility with external packages, such as LibMesh.
1167f296bb3SBarry Smith
1177f296bb3SBarry Smith## Working with data
1187f296bb3SBarry Smith
1197f296bb3SBarry SmithOnce a `PetscSection` has been created one can use `PetscSectionGetStorageSize()` to determine the total number of entries that can be stored in an array or `Vec` accessible by the `PetscSection`.
1207f296bb3SBarry SmithThis is most often used when creating a new `Vec` for a `PetscSection` such as:
1217f296bb3SBarry Smith
1227f296bb3SBarry Smith```
1237f296bb3SBarry SmithPetscSectionGetStorageSize(s, &n);
1247f296bb3SBarry SmithVecSetSizes(localVec, n, PETSC_DETERMINE);
1257f296bb3SBarry SmithVecSetFromOptions(localVec);
1267f296bb3SBarry Smith```
1277f296bb3SBarry Smith
1287f296bb3SBarry SmithThe memory locations in the associated array are found using an **offset** which can be obtained with:
1297f296bb3SBarry Smith
1307f296bb3SBarry SmithSingle-field `PetscSection`:
1317f296bb3SBarry Smith
1327f296bb3SBarry Smith```
1337f296bb3SBarry SmithPetscSectionGetOffset(PetscSection, PetscInt point, PetscInt &offset);
1347f296bb3SBarry Smith```
1357f296bb3SBarry Smith
1367f296bb3SBarry SmithMulti-field `PetscSection`:
1377f296bb3SBarry Smith
1387f296bb3SBarry Smith```
1397f296bb3SBarry SmithPetscSectionGetFieldOffset(PetscSection, PetscInt point, PetscInt field, PetscInt &offset);
1407f296bb3SBarry Smith```
1417f296bb3SBarry Smith
1427f296bb3SBarry SmithThe value in the array is then accessed with `array[offset + d]`, where `d` in `[0, ndof)` is the dof to access.
1437f296bb3SBarry Smith
1447f296bb3SBarry Smith## Global Sections: Constrained and Distributed Data
1457f296bb3SBarry Smith
1467f296bb3SBarry SmithTo handle distributed data and data with constraints, we use a pair of `PetscSections` called the `localSection` and `globalSection`.
1477f296bb3SBarry SmithTheir use for each is described below.
1487f296bb3SBarry Smith
1497f296bb3SBarry Smith### Distributed Data
1507f296bb3SBarry Smith
1517f296bb3SBarry Smith`PetscSection` can also be applied to distributed problems as well.
1527f296bb3SBarry SmithThis is done using the same local/global system described in {any}`sec_localglobal`.
1537f296bb3SBarry SmithTo do this, we introduce three new concepts; a `localSection`, `globalSection`, `pointSF`, and `sectionSF`.
1547f296bb3SBarry Smith
155*2f04c522SBarry SmithAssume the mesh points of the "global" mesh are partitioned among processes and that some mesh points are shared between multiple processes (i.e there is an overlap in the partitions).
1567f296bb3SBarry SmithThe shared mesh points define the ghost/halo points needed in many PDE problems.
1577f296bb3SBarry SmithFor each shared mesh point, appoint one process to be the owner of that mesh point.
1587f296bb3SBarry SmithTo describe this parallel mesh point layout, we use a `PetscSF` and call it the `pointSF`.
1597f296bb3SBarry SmithThe `pointSF` describes which processes "own" which mesh points and which process is the owner of each shared mesh point.
1607f296bb3SBarry Smith
1617f296bb3SBarry SmithNext, for each process define a `PetscSection` that describes the mapping between that process's partition (including shared mesh points) and the data stored on it and call it the `localSection`.
1627f296bb3SBarry SmithThe `localSection` describes the layout of the local vector.
1637f296bb3SBarry SmithTo generate the `globalSection` we use `PetscSectionCreateGlobalSection()`, which takes the `localSection` and `pointSF` as inputs.
1647f296bb3SBarry SmithThe global section returns $-(dof+1)$ for the number of dofs on an unowned (ghost) point, and traditionally $-(off+1)$ for its offset on the owning process.
1657f296bb3SBarry SmithThis behavior of the offsets is controlled via an argument to `PetscSectionCreateGlobalSection()`.
1667f296bb3SBarry SmithThe `globalSection` can be used to create global vectors, just as the local section is used to create local vectors.
1677f296bb3SBarry Smith
1687f296bb3SBarry SmithTo perform the global-to-local and local-to-global communication, we define `sectionSF` to be the `PetscSF` describing the mapping between the local and global vectors.
1697f296bb3SBarry SmithThis is generated via `PetscSFSetGraphSection()`.
1707f296bb3SBarry SmithUsing `PetscSFBcastBegin()` will send data from the global vector to the local vector, while `PetscSFReduceBegin()` will send data from the local vector to the global vector.
1717f296bb3SBarry Smith
1727f296bb3SBarry SmithIf using `DM`, this entire process is done automatically.
1737f296bb3SBarry SmithThe `localSection`, `globalSection`, `pointSF`, and `sectionSF` on a `DM` can be obtained via `DMGetLocalSection()`, `DMGetGlobalSection()`, `DMGetPointSF()`, and `DMGetSectionSF()`, respectively.
1747f296bb3SBarry SmithAdditionally, communication from global to local vectors and vice versa can be done via `DMGlobalToLocal()` and `DMLocalToGlobal()` as described in {any}`sec_localglobal`.
1757f296bb3SBarry SmithNote that not all `DM` types use this system, such as `DMDA` (see {any}`sec_struct`).
1767f296bb3SBarry Smith
1777f296bb3SBarry Smith### Constrained Data
1787f296bb3SBarry Smith
1797f296bb3SBarry SmithIn addition to describing parallel data, the `localSection`/`globalSection` pair can be used to describe *constrained* dofs
1807f296bb3SBarry SmithThese constraints usually represent essential (Dirichlet) boundary conditions, or algebraic constraints.
1817f296bb3SBarry SmithThey are dofs that have a given fixed value, so they are present in local vectors for finite element/volume assembly or finite difference stencil application purposes, but generally absent from global vectors since they are not unknowns in the algebraic solves.
1827f296bb3SBarry Smith
1837f296bb3SBarry SmithConstraints should be indicated in the `localSection`.
1847f296bb3SBarry SmithUse `PetscSectionSetConstraintDof()` to set the number of constrained dofs for a given point, and `PetscSectionSetConstraintIndices()` to indicate which dofs on the given point are constrained.
1857f296bb3SBarry SmithThis must be done before `PetscSectionCreateGlobalSection()` is called to create the `globalSection`.
1867f296bb3SBarry Smith
1877f296bb3SBarry SmithNote that it is possible to have constraints set in a `localSection`, but have the `globalSection` be generated to include those constraints.
1887f296bb3SBarry SmithThis is useful when doing some form of post-processing of a solution where you want to access all data (see `DMGetOutputDM()` for example).
1897f296bb3SBarry SmithSee `PetscSectionCreateGlobalSection()` for more details on this.
1907f296bb3SBarry Smith
1917f296bb3SBarry Smith(sec_petscsection_permutation)=
1927f296bb3SBarry Smith
1937f296bb3SBarry Smith## Permutation: Changing the order of array data
1947f296bb3SBarry Smith
1957f296bb3SBarry SmithBy default, when `PetscSectionSetUp()` is called, the data laid out in the associated array is assumed to be in the same order of the grid points.
1967f296bb3SBarry SmithFor example, the DoFs associated with grid point 0 appear directly before grid point 1, which appears before grid point 2, etc.
1977f296bb3SBarry Smith
1987f296bb3SBarry SmithIt may be desired to have a different the ordering of data in the array than the order of grid points defined by a section.
1997f296bb3SBarry SmithFor example, one may want grid points associated with the boundary of a domain to appear before points associated with the interior of the domain.
2007f296bb3SBarry Smith
2017f296bb3SBarry SmithThis can be accomplished by either changing the indexes of the grid points themselves, or by informing the section of the change in array ordering.
2027f296bb3SBarry SmithEither method uses an `IS` to define the permutation.
2037f296bb3SBarry Smith
2047f296bb3SBarry SmithTo change the indices of the grid points, call `PetscSectionPermute()` to generate a new `PetscSection` with the desired grid point permutation.
2057f296bb3SBarry Smith
2067f296bb3SBarry SmithTo just change the array layout without changing the grid point indexing, call `PetscSectionSetPermutation()`.
2077f296bb3SBarry SmithThis must be called before `PetscSectionSetUp()` and will only affect the calculation of the offsets for each grid point.
2087f296bb3SBarry Smith
2097f296bb3SBarry Smith% TODO: Add example to demonstrate the difference between the two permutation methods
2107f296bb3SBarry Smith
2117f296bb3SBarry Smith## DMPlex Specific Functionality: Obtaining data from the array
2127f296bb3SBarry Smith
2137f296bb3SBarry SmithA vanilla `PetscSection` (what's been described up till now) gives a relatively naive perspective on the underlying data; it doesn't describe how DoFs attached to a single grid point are ordered or how different grid points relate to each other.
2147f296bb3SBarry SmithA `PetscSection` can store and use this extra information in the form of **closures**, **symmetries**, and **closure permutations**.
2157f296bb3SBarry SmithThese features currently target `DMPlex` and other unstructured grid descriptions.
2167f296bb3SBarry SmithA description of those features will be left to {any}`ch_unstructured`.
2177f296bb3SBarry Smith
2187f296bb3SBarry Smith```{rubric} Footnotes
2197f296bb3SBarry Smith```
2207f296bb3SBarry Smith
2217f296bb3SBarry Smith[^petscsection-footnote]: A `PetscSection` can be thought of as a generalization of `PetscLayout`, in the same way that a fiber bundle is a generalization
2227f296bb3SBarry Smith    of the normal Euclidean basis used in linear algebra. With `PetscLayout`, we associate a unit vector ($e_i$) with every
2237f296bb3SBarry Smith    point in the space, and just divide up points between processes.
2247f296bb3SBarry Smith    Conversely, `PetscSection` associates multiple unit vectors with every mesh point (one for each dof) and divides the mesh points between processes using a `PetscSF` to define the distribution.
2257f296bb3SBarry Smith
2267f296bb3SBarry Smith```{eval-rst}
2277f296bb3SBarry Smith.. bibliography:: /petsc.bib
2287f296bb3SBarry Smith    :filter: docname in docnames
2297f296bb3SBarry Smith```
230