xref: /petsc/src/vec/is/section/interface/section.c (revision 2fe279fdf3e687a416e4eadb7d3c7a82d60442c6)
1ea844a1aSMatthew Knepley /*
2ea844a1aSMatthew Knepley    This file contains routines for basic section object implementation.
3ea844a1aSMatthew Knepley */
4ea844a1aSMatthew Knepley 
5ea844a1aSMatthew Knepley #include <petsc/private/sectionimpl.h> /*I  "petscsection.h"   I*/
6ea844a1aSMatthew Knepley #include <petscsf.h>
7ea844a1aSMatthew Knepley 
8ea844a1aSMatthew Knepley PetscClassId PETSC_SECTION_CLASSID;
9ea844a1aSMatthew Knepley 
10ea844a1aSMatthew Knepley /*@
1120662ed9SBarry Smith   PetscSectionCreate - Allocates a `PetscSection` and sets the map contents to the default.
12ea844a1aSMatthew Knepley 
13ea844a1aSMatthew Knepley   Collective
14ea844a1aSMatthew Knepley 
15ea844a1aSMatthew Knepley   Input Parameters:
16ea844a1aSMatthew Knepley + comm - the MPI communicator
17ea844a1aSMatthew Knepley - s    - pointer to the section
18ea844a1aSMatthew Knepley 
19ea844a1aSMatthew Knepley   Level: beginner
20ea844a1aSMatthew Knepley 
21ea844a1aSMatthew Knepley   Notes:
22ea844a1aSMatthew Knepley   Typical calling sequence
23cab54364SBarry Smith .vb
24cab54364SBarry Smith        PetscSectionCreate(MPI_Comm,PetscSection *);!
25cab54364SBarry Smith        PetscSectionSetNumFields(PetscSection, numFields);
26cab54364SBarry Smith        PetscSectionSetChart(PetscSection,low,high);
27cab54364SBarry Smith        PetscSectionSetDof(PetscSection,point,numdof);
28cab54364SBarry Smith        PetscSectionSetUp(PetscSection);
29cab54364SBarry Smith        PetscSectionGetOffset(PetscSection,point,PetscInt *);
30cab54364SBarry Smith        PetscSectionDestroy(PetscSection);
31cab54364SBarry Smith .ve
32ea844a1aSMatthew Knepley 
3335cb6cd3SPierre Jolivet   The `PetscSection` object and methods are intended to be used in the PETSc `Vec` and `Mat` implementations. The indices returned by the `PetscSection` are appropriate for the kind of `Vec` it is associated with. For example, if the vector being indexed is a local vector, we call the section a local section. If the section indexes a global vector, we call it a global section. For parallel vectors, like global vectors, we use negative indices to indicate dofs owned by other processes.
34ea844a1aSMatthew Knepley 
35db527f05SMatthew G. Knepley .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionDestroy()`, `PetscSectionCreateGlobalSection()`
36ea844a1aSMatthew Knepley @*/
37d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCreate(MPI_Comm comm, PetscSection *s)
38d71ae5a4SJacob Faibussowitsch {
39ea844a1aSMatthew Knepley   PetscFunctionBegin;
40ea844a1aSMatthew Knepley   PetscValidPointer(s, 2);
419566063dSJacob Faibussowitsch   PetscCall(ISInitializePackage());
42ea844a1aSMatthew Knepley 
439566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(*s, PETSC_SECTION_CLASSID, "PetscSection", "Section", "IS", comm, PetscSectionDestroy, PetscSectionView));
44ea844a1aSMatthew Knepley 
45ea844a1aSMatthew Knepley   (*s)->pStart              = -1;
46ea844a1aSMatthew Knepley   (*s)->pEnd                = -1;
47ea844a1aSMatthew Knepley   (*s)->perm                = NULL;
48ea844a1aSMatthew Knepley   (*s)->pointMajor          = PETSC_TRUE;
4987e637c6Sksagiyam   (*s)->includesConstraints = PETSC_TRUE;
50ea844a1aSMatthew Knepley   (*s)->atlasDof            = NULL;
51ea844a1aSMatthew Knepley   (*s)->atlasOff            = NULL;
52ea844a1aSMatthew Knepley   (*s)->bc                  = NULL;
53ea844a1aSMatthew Knepley   (*s)->bcIndices           = NULL;
54ea844a1aSMatthew Knepley   (*s)->setup               = PETSC_FALSE;
55ea844a1aSMatthew Knepley   (*s)->numFields           = 0;
56ea844a1aSMatthew Knepley   (*s)->fieldNames          = NULL;
57ea844a1aSMatthew Knepley   (*s)->field               = NULL;
58ea844a1aSMatthew Knepley   (*s)->useFieldOff         = PETSC_FALSE;
59b778fa18SValeria Barra   (*s)->compNames           = NULL;
60ea844a1aSMatthew Knepley   (*s)->clObj               = NULL;
61c459fbc1SJed Brown   (*s)->clHash              = NULL;
62ea844a1aSMatthew Knepley   (*s)->clSection           = NULL;
63ea844a1aSMatthew Knepley   (*s)->clPoints            = NULL;
6469c11d05SVaclav Hapla   PetscCall(PetscSectionInvalidateMaxDof_Internal(*s));
653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
66ea844a1aSMatthew Knepley }
67ea844a1aSMatthew Knepley 
68ea844a1aSMatthew Knepley /*@
69cab54364SBarry Smith   PetscSectionCopy - Creates a shallow (if possible) copy of the `PetscSection`
70ea844a1aSMatthew Knepley 
71ea844a1aSMatthew Knepley   Collective
72ea844a1aSMatthew Knepley 
73ea844a1aSMatthew Knepley   Input Parameter:
74cab54364SBarry Smith . section - the `PetscSection`
75ea844a1aSMatthew Knepley 
76ea844a1aSMatthew Knepley   Output Parameter:
77ea844a1aSMatthew Knepley . newSection - the copy
78ea844a1aSMatthew Knepley 
79ea844a1aSMatthew Knepley   Level: intermediate
80ea844a1aSMatthew Knepley 
81cab54364SBarry Smith   Developer Note:
82cab54364SBarry Smith   What exactly does shallow mean in this context?
83cab54364SBarry Smith 
84cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionDestroy()`
85ea844a1aSMatthew Knepley @*/
86d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCopy(PetscSection section, PetscSection newSection)
87d71ae5a4SJacob Faibussowitsch {
88ea844a1aSMatthew Knepley   PetscSectionSym sym;
89ea844a1aSMatthew Knepley   IS              perm;
90b778fa18SValeria Barra   PetscInt        numFields, f, c, pStart, pEnd, p;
91ea844a1aSMatthew Knepley 
92ea844a1aSMatthew Knepley   PetscFunctionBegin;
93ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
94ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(newSection, PETSC_SECTION_CLASSID, 2);
959566063dSJacob Faibussowitsch   PetscCall(PetscSectionReset(newSection));
969566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(section, &numFields));
979566063dSJacob Faibussowitsch   if (numFields) PetscCall(PetscSectionSetNumFields(newSection, numFields));
98ea844a1aSMatthew Knepley   for (f = 0; f < numFields; ++f) {
99b778fa18SValeria Barra     const char *fieldName = NULL, *compName = NULL;
100ea844a1aSMatthew Knepley     PetscInt    numComp = 0;
101ea844a1aSMatthew Knepley 
1029566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
1039566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldName(newSection, f, fieldName));
1049566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(section, f, &numComp));
1059566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(newSection, f, numComp));
106b778fa18SValeria Barra     for (c = 0; c < numComp; ++c) {
1079566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetComponentName(section, f, c, &compName));
1089566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetComponentName(newSection, f, c, compName));
109b778fa18SValeria Barra     }
1109566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldSym(section, f, &sym));
1119566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldSym(newSection, f, sym));
112ea844a1aSMatthew Knepley   }
1139566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
1149566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(newSection, pStart, pEnd));
1159566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetPermutation(section, &perm));
1169566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetPermutation(newSection, perm));
1179566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetSym(section, &sym));
1189566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetSym(newSection, sym));
119ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
120ea844a1aSMatthew Knepley     PetscInt dof, cdof, fcdof = 0;
121ea844a1aSMatthew Knepley 
1229566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(section, p, &dof));
1239566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(newSection, p, dof));
1249566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
1259566063dSJacob Faibussowitsch     if (cdof) PetscCall(PetscSectionSetConstraintDof(newSection, p, cdof));
126ea844a1aSMatthew Knepley     for (f = 0; f < numFields; ++f) {
1279566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(section, p, f, &dof));
1289566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(newSection, p, f, dof));
129ea844a1aSMatthew Knepley       if (cdof) {
1309566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
1319566063dSJacob Faibussowitsch         if (fcdof) PetscCall(PetscSectionSetFieldConstraintDof(newSection, p, f, fcdof));
132ea844a1aSMatthew Knepley       }
133ea844a1aSMatthew Knepley     }
134ea844a1aSMatthew Knepley   }
1359566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(newSection));
136ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
137ea844a1aSMatthew Knepley     PetscInt        off, cdof, fcdof = 0;
138ea844a1aSMatthew Knepley     const PetscInt *cInd;
139ea844a1aSMatthew Knepley 
140ea844a1aSMatthew Knepley     /* Must set offsets in case they do not agree with the prefix sums */
1419566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(section, p, &off));
1429566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetOffset(newSection, p, off));
1439566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
144ea844a1aSMatthew Knepley     if (cdof) {
1459566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetConstraintIndices(section, p, &cInd));
1469566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetConstraintIndices(newSection, p, cInd));
147ea844a1aSMatthew Knepley       for (f = 0; f < numFields; ++f) {
1489566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
149ea844a1aSMatthew Knepley         if (fcdof) {
1509566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintIndices(section, p, f, &cInd));
1519566063dSJacob Faibussowitsch           PetscCall(PetscSectionSetFieldConstraintIndices(newSection, p, f, cInd));
152ea844a1aSMatthew Knepley         }
153ea844a1aSMatthew Knepley       }
154ea844a1aSMatthew Knepley     }
155ea844a1aSMatthew Knepley   }
1563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
157ea844a1aSMatthew Knepley }
158ea844a1aSMatthew Knepley 
159ea844a1aSMatthew Knepley /*@
160cab54364SBarry Smith   PetscSectionClone - Creates a shallow (if possible) copy of the `PetscSection`
161ea844a1aSMatthew Knepley 
162ea844a1aSMatthew Knepley   Collective
163ea844a1aSMatthew Knepley 
164ea844a1aSMatthew Knepley   Input Parameter:
165cab54364SBarry Smith . section - the `PetscSection`
166ea844a1aSMatthew Knepley 
167ea844a1aSMatthew Knepley   Output Parameter:
168ea844a1aSMatthew Knepley . newSection - the copy
169ea844a1aSMatthew Knepley 
170ea844a1aSMatthew Knepley   Level: beginner
171ea844a1aSMatthew Knepley 
172cab54364SBarry Smith   Developer Note:
173cab54364SBarry Smith   With standard PETSc terminology this should be called `PetscSectionDuplicate()`
174cab54364SBarry Smith 
175cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionDestroy()`, `PetscSectionCopy()`
176ea844a1aSMatthew Knepley @*/
177d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionClone(PetscSection section, PetscSection *newSection)
178d71ae5a4SJacob Faibussowitsch {
179ea844a1aSMatthew Knepley   PetscFunctionBegin;
180ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
181ea844a1aSMatthew Knepley   PetscValidPointer(newSection, 2);
1829566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)section), newSection));
1839566063dSJacob Faibussowitsch   PetscCall(PetscSectionCopy(section, *newSection));
1843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
185ea844a1aSMatthew Knepley }
186ea844a1aSMatthew Knepley 
187ea844a1aSMatthew Knepley /*@
188cab54364SBarry Smith   PetscSectionSetFromOptions - sets parameters in a `PetscSection` from the options database
189ea844a1aSMatthew Knepley 
19040750d41SVaclav Hapla   Collective
191ea844a1aSMatthew Knepley 
192ea844a1aSMatthew Knepley   Input Parameter:
193cab54364SBarry Smith . section - the `PetscSection`
194ea844a1aSMatthew Knepley 
19520662ed9SBarry Smith   Options Database Key:
196cab54364SBarry Smith . -petscsection_point_major - `PETSC_TRUE` for point-major order
197ea844a1aSMatthew Knepley 
198ea844a1aSMatthew Knepley   Level: intermediate
199ea844a1aSMatthew Knepley 
200cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionDestroy()`
201ea844a1aSMatthew Knepley @*/
202d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFromOptions(PetscSection s)
203d71ae5a4SJacob Faibussowitsch {
204ea844a1aSMatthew Knepley   PetscFunctionBegin;
205ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
206d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)s);
2079566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-petscsection_point_major", "The for ordering, either point major or field major", "PetscSectionSetPointMajor", s->pointMajor, &s->pointMajor, NULL));
208ea844a1aSMatthew Knepley   /* process any options handlers added with PetscObjectAddOptionsHandler() */
209dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)s, PetscOptionsObject));
210d0609cedSBarry Smith   PetscOptionsEnd();
2119566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)s, NULL, "-petscsection_view"));
2123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
213ea844a1aSMatthew Knepley }
214ea844a1aSMatthew Knepley 
215ea844a1aSMatthew Knepley /*@
216ea844a1aSMatthew Knepley   PetscSectionCompare - Compares two sections
217ea844a1aSMatthew Knepley 
21840750d41SVaclav Hapla   Collective
219ea844a1aSMatthew Knepley 
2207a7aea1fSJed Brown   Input Parameters:
221cab54364SBarry Smith + s1 - the first `PetscSection`
222cab54364SBarry Smith - s2 - the second `PetscSection`
223ea844a1aSMatthew Knepley 
224ea844a1aSMatthew Knepley   Output Parameter:
225cab54364SBarry Smith . congruent - `PETSC_TRUE` if the two sections are congruent, `PETSC_FALSE` otherwise
226ea844a1aSMatthew Knepley 
227ea844a1aSMatthew Knepley   Level: intermediate
228ea844a1aSMatthew Knepley 
229cab54364SBarry Smith   Note:
230ea844a1aSMatthew Knepley   Field names are disregarded.
231ea844a1aSMatthew Knepley 
232cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionCopy()`, `PetscSectionClone()`
233ea844a1aSMatthew Knepley @*/
234d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCompare(PetscSection s1, PetscSection s2, PetscBool *congruent)
235d71ae5a4SJacob Faibussowitsch {
236ea844a1aSMatthew Knepley   PetscInt        pStart, pEnd, nfields, ncdof, nfcdof, p, f, n1, n2;
237ea844a1aSMatthew Knepley   const PetscInt *idx1, *idx2;
238ea844a1aSMatthew Knepley   IS              perm1, perm2;
239ea844a1aSMatthew Knepley   PetscBool       flg;
240ea844a1aSMatthew Knepley   PetscMPIInt     mflg;
241ea844a1aSMatthew Knepley 
242ea844a1aSMatthew Knepley   PetscFunctionBegin;
243ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s1, PETSC_SECTION_CLASSID, 1);
244ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s2, PETSC_SECTION_CLASSID, 2);
245064a246eSJacob Faibussowitsch   PetscValidBoolPointer(congruent, 3);
246ea844a1aSMatthew Knepley   flg = PETSC_FALSE;
247ea844a1aSMatthew Knepley 
2489566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)s1), PetscObjectComm((PetscObject)s2), &mflg));
249ea844a1aSMatthew Knepley   if (mflg != MPI_CONGRUENT && mflg != MPI_IDENT) {
250ea844a1aSMatthew Knepley     *congruent = PETSC_FALSE;
2513ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
252ea844a1aSMatthew Knepley   }
253ea844a1aSMatthew Knepley 
2549566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s1, &pStart, &pEnd));
2559566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s2, &n1, &n2));
256ea844a1aSMatthew Knepley   if (pStart != n1 || pEnd != n2) goto not_congruent;
257ea844a1aSMatthew Knepley 
2589566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetPermutation(s1, &perm1));
2599566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetPermutation(s2, &perm2));
260ea844a1aSMatthew Knepley   if (perm1 && perm2) {
2619566063dSJacob Faibussowitsch     PetscCall(ISEqual(perm1, perm2, congruent));
262ea844a1aSMatthew Knepley     if (!(*congruent)) goto not_congruent;
263ea844a1aSMatthew Knepley   } else if (perm1 != perm2) goto not_congruent;
264ea844a1aSMatthew Knepley 
265ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
2669566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(s1, p, &n1));
2679566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(s2, p, &n2));
268ea844a1aSMatthew Knepley     if (n1 != n2) goto not_congruent;
269ea844a1aSMatthew Knepley 
2709566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s1, p, &n1));
2719566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s2, p, &n2));
272ea844a1aSMatthew Knepley     if (n1 != n2) goto not_congruent;
273ea844a1aSMatthew Knepley 
2749566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s1, p, &ncdof));
2759566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s2, p, &n2));
276ea844a1aSMatthew Knepley     if (ncdof != n2) goto not_congruent;
277ea844a1aSMatthew Knepley 
2789566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintIndices(s1, p, &idx1));
2799566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintIndices(s2, p, &idx2));
2809566063dSJacob Faibussowitsch     PetscCall(PetscArraycmp(idx1, idx2, ncdof, congruent));
281ea844a1aSMatthew Knepley     if (!(*congruent)) goto not_congruent;
282ea844a1aSMatthew Knepley   }
283ea844a1aSMatthew Knepley 
2849566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s1, &nfields));
2859566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s2, &n2));
286ea844a1aSMatthew Knepley   if (nfields != n2) goto not_congruent;
287ea844a1aSMatthew Knepley 
288ea844a1aSMatthew Knepley   for (f = 0; f < nfields; ++f) {
2899566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s1, f, &n1));
2909566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s2, f, &n2));
291ea844a1aSMatthew Knepley     if (n1 != n2) goto not_congruent;
292ea844a1aSMatthew Knepley 
293ea844a1aSMatthew Knepley     for (p = pStart; p < pEnd; ++p) {
2949566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldOffset(s1, p, f, &n1));
2959566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldOffset(s2, p, f, &n2));
296ea844a1aSMatthew Knepley       if (n1 != n2) goto not_congruent;
297ea844a1aSMatthew Knepley 
2989566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s1, p, f, &n1));
2999566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s2, p, f, &n2));
300ea844a1aSMatthew Knepley       if (n1 != n2) goto not_congruent;
301ea844a1aSMatthew Knepley 
3029566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(s1, p, f, &nfcdof));
3039566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(s2, p, f, &n2));
304ea844a1aSMatthew Knepley       if (nfcdof != n2) goto not_congruent;
305ea844a1aSMatthew Knepley 
3069566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintIndices(s1, p, f, &idx1));
3079566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintIndices(s2, p, f, &idx2));
3089566063dSJacob Faibussowitsch       PetscCall(PetscArraycmp(idx1, idx2, nfcdof, congruent));
309ea844a1aSMatthew Knepley       if (!(*congruent)) goto not_congruent;
310ea844a1aSMatthew Knepley     }
311ea844a1aSMatthew Knepley   }
312ea844a1aSMatthew Knepley 
313ea844a1aSMatthew Knepley   flg = PETSC_TRUE;
314ea844a1aSMatthew Knepley not_congruent:
3151c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&flg, congruent, 1, MPIU_BOOL, MPI_LAND, PetscObjectComm((PetscObject)s1)));
3163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
317ea844a1aSMatthew Knepley }
318ea844a1aSMatthew Knepley 
319ea844a1aSMatthew Knepley /*@
320cab54364SBarry Smith   PetscSectionGetNumFields - Returns the number of fields in a `PetscSection`, or 0 if no fields were defined.
321ea844a1aSMatthew Knepley 
32240750d41SVaclav Hapla   Not Collective
323ea844a1aSMatthew Knepley 
324ea844a1aSMatthew Knepley   Input Parameter:
325cab54364SBarry Smith . s - the `PetscSection`
326ea844a1aSMatthew Knepley 
327ea844a1aSMatthew Knepley   Output Parameter:
328ea844a1aSMatthew Knepley . numFields - the number of fields defined, or 0 if none were defined
329ea844a1aSMatthew Knepley 
330ea844a1aSMatthew Knepley   Level: intermediate
331ea844a1aSMatthew Knepley 
332cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetNumFields()`
333ea844a1aSMatthew Knepley @*/
334d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetNumFields(PetscSection s, PetscInt *numFields)
335d71ae5a4SJacob Faibussowitsch {
336ea844a1aSMatthew Knepley   PetscFunctionBegin;
337ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
338dadcf809SJacob Faibussowitsch   PetscValidIntPointer(numFields, 2);
339ea844a1aSMatthew Knepley   *numFields = s->numFields;
3403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
341ea844a1aSMatthew Knepley }
342ea844a1aSMatthew Knepley 
343ea844a1aSMatthew Knepley /*@
344cab54364SBarry Smith   PetscSectionSetNumFields - Sets the number of fields in a `PetscSection`
345ea844a1aSMatthew Knepley 
34640750d41SVaclav Hapla   Not Collective
347ea844a1aSMatthew Knepley 
348ea844a1aSMatthew Knepley   Input Parameters:
34920662ed9SBarry Smith + s - the `PetscSection`
350ea844a1aSMatthew Knepley - numFields - the number of fields
351ea844a1aSMatthew Knepley 
352ea844a1aSMatthew Knepley   Level: intermediate
353ea844a1aSMatthew Knepley 
354cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection()`, `PetscSectionGetNumFields()`
355ea844a1aSMatthew Knepley @*/
356d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetNumFields(PetscSection s, PetscInt numFields)
357d71ae5a4SJacob Faibussowitsch {
358ea844a1aSMatthew Knepley   PetscInt f;
359ea844a1aSMatthew Knepley 
360ea844a1aSMatthew Knepley   PetscFunctionBegin;
361ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
36208401ef6SPierre Jolivet   PetscCheck(numFields > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "The number of fields %" PetscInt_FMT " must be positive", numFields);
3639566063dSJacob Faibussowitsch   PetscCall(PetscSectionReset(s));
364ea844a1aSMatthew Knepley 
365ea844a1aSMatthew Knepley   s->numFields = numFields;
3669566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(s->numFields, &s->numFieldComponents));
3679566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(s->numFields, &s->fieldNames));
3689566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(s->numFields, &s->compNames));
3699566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(s->numFields, &s->field));
370ea844a1aSMatthew Knepley   for (f = 0; f < s->numFields; ++f) {
371ea844a1aSMatthew Knepley     char name[64];
372ea844a1aSMatthew Knepley 
373ea844a1aSMatthew Knepley     s->numFieldComponents[f] = 1;
374ea844a1aSMatthew Knepley 
3759566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s), &s->field[f]));
3769566063dSJacob Faibussowitsch     PetscCall(PetscSNPrintf(name, 64, "Field_%" PetscInt_FMT, f));
3779566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, (char **)&s->fieldNames[f]));
3789566063dSJacob Faibussowitsch     PetscCall(PetscSNPrintf(name, 64, "Component_0"));
3799566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(s->numFieldComponents[f], &s->compNames[f]));
3809566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, (char **)&s->compNames[f][0]));
381ea844a1aSMatthew Knepley   }
3823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
383ea844a1aSMatthew Knepley }
384ea844a1aSMatthew Knepley 
385ea844a1aSMatthew Knepley /*@C
386cab54364SBarry Smith   PetscSectionGetFieldName - Returns the name of a field in the `PetscSection`
387ea844a1aSMatthew Knepley 
388ea844a1aSMatthew Knepley   Not Collective
389ea844a1aSMatthew Knepley 
390ea844a1aSMatthew Knepley   Input Parameters:
391cab54364SBarry Smith + s     - the `PetscSection`
392ea844a1aSMatthew Knepley - field - the field number
393ea844a1aSMatthew Knepley 
394ea844a1aSMatthew Knepley   Output Parameter:
395ea844a1aSMatthew Knepley . fieldName - the field name
396ea844a1aSMatthew Knepley 
397ea844a1aSMatthew Knepley   Level: intermediate
398ea844a1aSMatthew Knepley 
399cab54364SBarry Smith   Note:
400cab54364SBarry Smith   Will error if the field number is out of range
401cab54364SBarry Smith 
402cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetFieldName()`, `PetscSectionSetNumFields()`, `PetscSectionGetNumFields()`
403ea844a1aSMatthew Knepley @*/
404d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldName(PetscSection s, PetscInt field, const char *fieldName[])
405d71ae5a4SJacob Faibussowitsch {
406ea844a1aSMatthew Knepley   PetscFunctionBegin;
407ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
408ea844a1aSMatthew Knepley   PetscValidPointer(fieldName, 3);
4092abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
410ea844a1aSMatthew Knepley   *fieldName = s->fieldNames[field];
4113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
412ea844a1aSMatthew Knepley }
413ea844a1aSMatthew Knepley 
414ea844a1aSMatthew Knepley /*@C
415cab54364SBarry Smith   PetscSectionSetFieldName - Sets the name of a field in the `PetscSection`
416ea844a1aSMatthew Knepley 
417ea844a1aSMatthew Knepley   Not Collective
418ea844a1aSMatthew Knepley 
419ea844a1aSMatthew Knepley   Input Parameters:
420cab54364SBarry Smith + s     - the `PetscSection`
421ea844a1aSMatthew Knepley . field - the field number
422ea844a1aSMatthew Knepley - fieldName - the field name
423ea844a1aSMatthew Knepley 
424ea844a1aSMatthew Knepley   Level: intermediate
425ea844a1aSMatthew Knepley 
426cab54364SBarry Smith   Note:
427cab54364SBarry Smith   Will error if the field number is out of range
428cab54364SBarry Smith 
429cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionGetFieldName()`, `PetscSectionSetNumFields()`, `PetscSectionGetNumFields()`
430ea844a1aSMatthew Knepley @*/
431d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFieldName(PetscSection s, PetscInt field, const char fieldName[])
432d71ae5a4SJacob Faibussowitsch {
433ea844a1aSMatthew Knepley   PetscFunctionBegin;
434ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
435ea844a1aSMatthew Knepley   if (fieldName) PetscValidCharPointer(fieldName, 3);
4362abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
4379566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->fieldNames[field]));
4389566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(fieldName, (char **)&s->fieldNames[field]));
4393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
440ea844a1aSMatthew Knepley }
441ea844a1aSMatthew Knepley 
442b778fa18SValeria Barra /*@C
443cab54364SBarry Smith   PetscSectionGetComponentName - Gets the name of a field component in the `PetscSection`
444b778fa18SValeria Barra 
445b778fa18SValeria Barra   Not Collective
446b778fa18SValeria Barra 
447b778fa18SValeria Barra   Input Parameters:
448cab54364SBarry Smith + s     - the `PetscSection`
449b778fa18SValeria Barra . field - the field number
450cab54364SBarry Smith - comp  - the component number
451cab54364SBarry Smith 
452d5b43468SJose E. Roman   Output Parameter:
453cab54364SBarry Smith . compName - the component name
454b778fa18SValeria Barra 
455b778fa18SValeria Barra   Level: intermediate
456b778fa18SValeria Barra 
457cab54364SBarry Smith   Note:
458cab54364SBarry Smith   Will error if the field or component number do not exist
459cab54364SBarry Smith 
460cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetFieldName()`, `PetscSectionSetNumFields()`, `PetscSectionGetNumFields()`,
461cab54364SBarry Smith           `PetscSectionSetComponentName()`, `PetscSectionSetFieldName()`, `PetscSectionGetFieldComponents()`, `PetscSectionSetFieldComponents()`
462b778fa18SValeria Barra @*/
463d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetComponentName(PetscSection s, PetscInt field, PetscInt comp, const char *compName[])
464d71ae5a4SJacob Faibussowitsch {
465b778fa18SValeria Barra   PetscFunctionBegin;
466b778fa18SValeria Barra   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
467064a246eSJacob Faibussowitsch   PetscValidPointer(compName, 4);
4682abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
4692abc8c78SJacob Faibussowitsch   PetscSectionCheckValidFieldComponent(comp, s->numFieldComponents[field]);
470b778fa18SValeria Barra   *compName = s->compNames[field][comp];
4713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
472b778fa18SValeria Barra }
473b778fa18SValeria Barra 
474b778fa18SValeria Barra /*@C
475cab54364SBarry Smith   PetscSectionSetComponentName - Sets the name of a field component in the `PetscSection`
476b778fa18SValeria Barra 
477b778fa18SValeria Barra   Not Collective
478b778fa18SValeria Barra 
479b778fa18SValeria Barra   Input Parameters:
48020662ed9SBarry Smith + s     - the `PetscSection`
481b778fa18SValeria Barra . field - the field number
482b778fa18SValeria Barra . comp  - the component number
483b778fa18SValeria Barra - compName - the component name
484b778fa18SValeria Barra 
485b778fa18SValeria Barra   Level: intermediate
486b778fa18SValeria Barra 
487cab54364SBarry Smith   Note:
488cab54364SBarry Smith   Will error if the field or component number do not exist
489cab54364SBarry Smith 
490cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetComponentName()`, `PetscSectionSetNumFields()`, `PetscSectionGetNumFields()`,
491cab54364SBarry Smith           `PetscSectionSetComponentName()`, `PetscSectionSetFieldName()`, `PetscSectionGetFieldComponents()`, `PetscSectionSetFieldComponents()`
492b778fa18SValeria Barra @*/
493d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetComponentName(PetscSection s, PetscInt field, PetscInt comp, const char compName[])
494d71ae5a4SJacob Faibussowitsch {
495b778fa18SValeria Barra   PetscFunctionBegin;
496b778fa18SValeria Barra   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
497064a246eSJacob Faibussowitsch   if (compName) PetscValidCharPointer(compName, 4);
4982abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
4992abc8c78SJacob Faibussowitsch   PetscSectionCheckValidFieldComponent(comp, s->numFieldComponents[field]);
5009566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->compNames[field][comp]));
5019566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(compName, (char **)&s->compNames[field][comp]));
5023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
503b778fa18SValeria Barra }
504b778fa18SValeria Barra 
505ea844a1aSMatthew Knepley /*@
506ea844a1aSMatthew Knepley   PetscSectionGetFieldComponents - Returns the number of field components for the given field.
507ea844a1aSMatthew Knepley 
50840750d41SVaclav Hapla   Not Collective
509ea844a1aSMatthew Knepley 
510ea844a1aSMatthew Knepley   Input Parameters:
511cab54364SBarry Smith + s - the `PetscSection`
512ea844a1aSMatthew Knepley - field - the field number
513ea844a1aSMatthew Knepley 
514ea844a1aSMatthew Knepley   Output Parameter:
515ea844a1aSMatthew Knepley . numComp - the number of field components
516ea844a1aSMatthew Knepley 
517ea844a1aSMatthew Knepley   Level: intermediate
518ea844a1aSMatthew Knepley 
519cab54364SBarry Smith   Developer Note:
520cab54364SBarry Smith   This function is misnamed. There is a Num in `PetscSectionGetNumFields()` but not in this name
521cab54364SBarry Smith 
522cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetFieldComponents()`, `PetscSectionGetNumFields()`
523ea844a1aSMatthew Knepley @*/
524d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldComponents(PetscSection s, PetscInt field, PetscInt *numComp)
525d71ae5a4SJacob Faibussowitsch {
526ea844a1aSMatthew Knepley   PetscFunctionBegin;
527ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
5282abc8c78SJacob Faibussowitsch   PetscValidIntPointer(numComp, 3);
5292abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
530ea844a1aSMatthew Knepley   *numComp = s->numFieldComponents[field];
5313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
532ea844a1aSMatthew Knepley }
533ea844a1aSMatthew Knepley 
534ea844a1aSMatthew Knepley /*@
535ea844a1aSMatthew Knepley   PetscSectionSetFieldComponents - Sets the number of field components for the given field.
536ea844a1aSMatthew Knepley 
53740750d41SVaclav Hapla   Not Collective
538ea844a1aSMatthew Knepley 
539ea844a1aSMatthew Knepley   Input Parameters:
54020662ed9SBarry Smith + s - the `PetscSection`
541ea844a1aSMatthew Knepley . field - the field number
542ea844a1aSMatthew Knepley - numComp - the number of field components
543ea844a1aSMatthew Knepley 
544ea844a1aSMatthew Knepley   Level: intermediate
545ea844a1aSMatthew Knepley 
546cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetFieldComponents()`, `PetscSectionGetNumFields()`
547ea844a1aSMatthew Knepley @*/
548d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFieldComponents(PetscSection s, PetscInt field, PetscInt numComp)
549d71ae5a4SJacob Faibussowitsch {
550b778fa18SValeria Barra   PetscInt c;
551b778fa18SValeria Barra 
552ea844a1aSMatthew Knepley   PetscFunctionBegin;
553ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
5542abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
555b778fa18SValeria Barra   if (s->compNames) {
55648a46eb9SPierre Jolivet     for (c = 0; c < s->numFieldComponents[field]; ++c) PetscCall(PetscFree(s->compNames[field][c]));
5579566063dSJacob Faibussowitsch     PetscCall(PetscFree(s->compNames[field]));
558b778fa18SValeria Barra   }
559b778fa18SValeria Barra 
560ea844a1aSMatthew Knepley   s->numFieldComponents[field] = numComp;
561b778fa18SValeria Barra   if (numComp) {
5629566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numComp, (char ***)&s->compNames[field]));
563b778fa18SValeria Barra     for (c = 0; c < numComp; ++c) {
5642abc8c78SJacob Faibussowitsch       char name[64];
5652abc8c78SJacob Faibussowitsch 
5669566063dSJacob Faibussowitsch       PetscCall(PetscSNPrintf(name, 64, "%" PetscInt_FMT, c));
5679566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(name, (char **)&s->compNames[field][c]));
568b778fa18SValeria Barra     }
569b778fa18SValeria Barra   }
5703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
571ea844a1aSMatthew Knepley }
572ea844a1aSMatthew Knepley 
573ea844a1aSMatthew Knepley /*@
57420662ed9SBarry Smith   PetscSectionGetChart - Returns the range [`pStart`, `pEnd`) in which points (indices) lie for this `PetscSection`
575ea844a1aSMatthew Knepley 
57640750d41SVaclav Hapla   Not Collective
577ea844a1aSMatthew Knepley 
578ea844a1aSMatthew Knepley   Input Parameter:
579cab54364SBarry Smith . s - the `PetscSection`
580ea844a1aSMatthew Knepley 
581ea844a1aSMatthew Knepley   Output Parameters:
582ea844a1aSMatthew Knepley + pStart - the first point
583ea844a1aSMatthew Knepley - pEnd - one past the last point
584ea844a1aSMatthew Knepley 
585ea844a1aSMatthew Knepley   Level: intermediate
586ea844a1aSMatthew Knepley 
587cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetChart()`, `PetscSectionCreate()`
588ea844a1aSMatthew Knepley @*/
589d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetChart(PetscSection s, PetscInt *pStart, PetscInt *pEnd)
590d71ae5a4SJacob Faibussowitsch {
591ea844a1aSMatthew Knepley   PetscFunctionBegin;
592ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
593ea844a1aSMatthew Knepley   if (pStart) *pStart = s->pStart;
594ea844a1aSMatthew Knepley   if (pEnd) *pEnd = s->pEnd;
5953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
596ea844a1aSMatthew Knepley }
597ea844a1aSMatthew Knepley 
598ea844a1aSMatthew Knepley /*@
59920662ed9SBarry Smith   PetscSectionSetChart - Sets the range [`pStart`, `pEnd`) in which points (indices) lie for this `PetscSection`
600ea844a1aSMatthew Knepley 
60140750d41SVaclav Hapla   Not Collective
602ea844a1aSMatthew Knepley 
603ea844a1aSMatthew Knepley   Input Parameters:
60420662ed9SBarry Smith + s - the `PetscSection`
605ea844a1aSMatthew Knepley . pStart - the first point
606ea844a1aSMatthew Knepley - pEnd - one past the last point
607ea844a1aSMatthew Knepley 
608ea844a1aSMatthew Knepley   Level: intermediate
609ea844a1aSMatthew Knepley 
610cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetChart()`, `PetscSectionCreate()`
611ea844a1aSMatthew Knepley @*/
612d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetChart(PetscSection s, PetscInt pStart, PetscInt pEnd)
613d71ae5a4SJacob Faibussowitsch {
614ea844a1aSMatthew Knepley   PetscInt f;
615ea844a1aSMatthew Knepley 
616ea844a1aSMatthew Knepley   PetscFunctionBegin;
617ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
6183ba16761SJacob Faibussowitsch   if (pStart == s->pStart && pEnd == s->pEnd) PetscFunctionReturn(PETSC_SUCCESS);
619ea844a1aSMatthew Knepley   /* Cannot Reset() because it destroys field information */
620ea844a1aSMatthew Knepley   s->setup = PETSC_FALSE;
6219566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&s->bc));
6229566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->bcIndices));
6239566063dSJacob Faibussowitsch   PetscCall(PetscFree2(s->atlasDof, s->atlasOff));
624ea844a1aSMatthew Knepley 
625ea844a1aSMatthew Knepley   s->pStart = pStart;
626ea844a1aSMatthew Knepley   s->pEnd   = pEnd;
6279566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2((pEnd - pStart), &s->atlasDof, (pEnd - pStart), &s->atlasOff));
6289566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(s->atlasDof, pEnd - pStart));
62948a46eb9SPierre Jolivet   for (f = 0; f < s->numFields; ++f) PetscCall(PetscSectionSetChart(s->field[f], pStart, pEnd));
6303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
631ea844a1aSMatthew Knepley }
632ea844a1aSMatthew Knepley 
633ea844a1aSMatthew Knepley /*@
63420662ed9SBarry Smith   PetscSectionGetPermutation - Returns the permutation of [0, `pEnd` - `pStart`) or `NULL` that was set with `PetscSectionSetPermutation()`
635ea844a1aSMatthew Knepley 
63640750d41SVaclav Hapla   Not Collective
637ea844a1aSMatthew Knepley 
638ea844a1aSMatthew Knepley   Input Parameter:
639cab54364SBarry Smith . s - the `PetscSection`
640ea844a1aSMatthew Knepley 
641*2fe279fdSBarry Smith   Output Parameter:
642cab54364SBarry Smith . perm - The permutation as an `IS`
643ea844a1aSMatthew Knepley 
644ea844a1aSMatthew Knepley   Level: intermediate
645ea844a1aSMatthew Knepley 
646cab54364SBarry Smith .seealso: [](sec_scatter), `IS`, `PetscSection`, `PetscSectionSetPermutation()`, `PetscSectionCreate()`
647ea844a1aSMatthew Knepley @*/
648d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetPermutation(PetscSection s, IS *perm)
649d71ae5a4SJacob Faibussowitsch {
650ea844a1aSMatthew Knepley   PetscFunctionBegin;
651ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
6529371c9d4SSatish Balay   if (perm) {
6539371c9d4SSatish Balay     PetscValidPointer(perm, 2);
6549371c9d4SSatish Balay     *perm = s->perm;
6559371c9d4SSatish Balay   }
6563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
657ea844a1aSMatthew Knepley }
658ea844a1aSMatthew Knepley 
659ea844a1aSMatthew Knepley /*@
66020662ed9SBarry Smith   PetscSectionSetPermutation - Sets the permutation for [0, `pEnd` - `pStart`)
661ea844a1aSMatthew Knepley 
66240750d41SVaclav Hapla   Not Collective
663ea844a1aSMatthew Knepley 
664ea844a1aSMatthew Knepley   Input Parameters:
66520662ed9SBarry Smith + s - the `PetscSection`
666ea844a1aSMatthew Knepley - perm - the permutation of points
667ea844a1aSMatthew Knepley 
668ea844a1aSMatthew Knepley   Level: intermediate
669ea844a1aSMatthew Knepley 
670cab54364SBarry Smith   Developer Note:
671cab54364SBarry Smith   What purpose does this permutation serve?
672cab54364SBarry Smith 
673cab54364SBarry Smith .seealso: [](sec_scatter), `IS`, `PetscSection`, `PetscSectionGetPermutation()`, `PetscSectionCreate()`
674ea844a1aSMatthew Knepley @*/
675d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetPermutation(PetscSection s, IS perm)
676d71ae5a4SJacob Faibussowitsch {
677ea844a1aSMatthew Knepley   PetscFunctionBegin;
678ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
679ea844a1aSMatthew Knepley   if (perm) PetscValidHeaderSpecific(perm, IS_CLASSID, 2);
68028b400f6SJacob Faibussowitsch   PetscCheck(!s->setup, PetscObjectComm((PetscObject)s), PETSC_ERR_ARG_WRONGSTATE, "Cannot set a permutation after the section is setup");
681ea844a1aSMatthew Knepley   if (s->perm != perm) {
6829566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&s->perm));
683ea844a1aSMatthew Knepley     if (perm) {
684ea844a1aSMatthew Knepley       s->perm = perm;
6859566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)s->perm));
686ea844a1aSMatthew Knepley     }
687ea844a1aSMatthew Knepley   }
6883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
689ea844a1aSMatthew Knepley }
690ea844a1aSMatthew Knepley 
691ea844a1aSMatthew Knepley /*@
692cab54364SBarry Smith   PetscSectionGetPointMajor - Returns the flag for dof ordering, `PETSC_TRUE` if it is point major, `PETSC_FALSE` if it is field major
693ea844a1aSMatthew Knepley 
69440750d41SVaclav Hapla   Not Collective
695ea844a1aSMatthew Knepley 
696ea844a1aSMatthew Knepley   Input Parameter:
697cab54364SBarry Smith . s - the `PetscSection`
698ea844a1aSMatthew Knepley 
699ea844a1aSMatthew Knepley   Output Parameter:
700ea844a1aSMatthew Knepley . pm - the flag for point major ordering
701ea844a1aSMatthew Knepley 
702ea844a1aSMatthew Knepley   Level: intermediate
703ea844a1aSMatthew Knepley 
704cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, PetscSectionSetPointMajor()`
705ea844a1aSMatthew Knepley @*/
706d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetPointMajor(PetscSection s, PetscBool *pm)
707d71ae5a4SJacob Faibussowitsch {
708ea844a1aSMatthew Knepley   PetscFunctionBegin;
709ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
710dadcf809SJacob Faibussowitsch   PetscValidBoolPointer(pm, 2);
711ea844a1aSMatthew Knepley   *pm = s->pointMajor;
7123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
713ea844a1aSMatthew Knepley }
714ea844a1aSMatthew Knepley 
715ea844a1aSMatthew Knepley /*@
716cab54364SBarry Smith   PetscSectionSetPointMajor - Sets the flag for dof ordering, `PETSC_TRUE` for point major, otherwise it will be field major
717ea844a1aSMatthew Knepley 
71840750d41SVaclav Hapla   Not Collective
719ea844a1aSMatthew Knepley 
720ea844a1aSMatthew Knepley   Input Parameters:
721cab54364SBarry Smith + s  - the `PetscSection`
722ea844a1aSMatthew Knepley - pm - the flag for point major ordering
723ea844a1aSMatthew Knepley 
724ea844a1aSMatthew Knepley   Level: intermediate
725ea844a1aSMatthew Knepley 
726cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetPointMajor()`
727ea844a1aSMatthew Knepley @*/
728d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetPointMajor(PetscSection s, PetscBool pm)
729d71ae5a4SJacob Faibussowitsch {
730ea844a1aSMatthew Knepley   PetscFunctionBegin;
731ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
73228b400f6SJacob Faibussowitsch   PetscCheck(!s->setup, PetscObjectComm((PetscObject)s), PETSC_ERR_ARG_WRONGSTATE, "Cannot set the dof ordering after the section is setup");
733ea844a1aSMatthew Knepley   s->pointMajor = pm;
7343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
735ea844a1aSMatthew Knepley }
736ea844a1aSMatthew Knepley 
737ea844a1aSMatthew Knepley /*@
738cab54364SBarry Smith   PetscSectionGetIncludesConstraints - Returns the flag indicating if constrained dofs were included when computing offsets in the `PetscSection`.
739cab54364SBarry Smith   The value is set with `PetscSectionSetIncludesConstraints()`
74087e637c6Sksagiyam 
74140750d41SVaclav Hapla   Not Collective
74287e637c6Sksagiyam 
74387e637c6Sksagiyam   Input Parameter:
744cab54364SBarry Smith . s - the `PetscSection`
74587e637c6Sksagiyam 
74687e637c6Sksagiyam   Output Parameter:
74787e637c6Sksagiyam . includesConstraints - the flag indicating if constrained dofs were included when computing offsets
74887e637c6Sksagiyam 
74987e637c6Sksagiyam   Level: intermediate
75087e637c6Sksagiyam 
751cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetIncludesConstraints()`
75287e637c6Sksagiyam @*/
753d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetIncludesConstraints(PetscSection s, PetscBool *includesConstraints)
754d71ae5a4SJacob Faibussowitsch {
75587e637c6Sksagiyam   PetscFunctionBegin;
75687e637c6Sksagiyam   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
75787e637c6Sksagiyam   PetscValidBoolPointer(includesConstraints, 2);
75887e637c6Sksagiyam   *includesConstraints = s->includesConstraints;
7593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
76087e637c6Sksagiyam }
76187e637c6Sksagiyam 
76287e637c6Sksagiyam /*@
76387e637c6Sksagiyam   PetscSectionSetIncludesConstraints - Sets the flag indicating if constrained dofs are to be included when computing offsets
76487e637c6Sksagiyam 
76540750d41SVaclav Hapla   Not Collective
76687e637c6Sksagiyam 
76787e637c6Sksagiyam   Input Parameters:
76820662ed9SBarry Smith + s  - the `PetscSection`
76987e637c6Sksagiyam - includesConstraints - the flag indicating if constrained dofs are to be included when computing offsets
77087e637c6Sksagiyam 
77187e637c6Sksagiyam   Level: intermediate
77287e637c6Sksagiyam 
773cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetIncludesConstraints()`
77487e637c6Sksagiyam @*/
775d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetIncludesConstraints(PetscSection s, PetscBool includesConstraints)
776d71ae5a4SJacob Faibussowitsch {
77787e637c6Sksagiyam   PetscFunctionBegin;
77887e637c6Sksagiyam   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
77928b400f6SJacob Faibussowitsch   PetscCheck(!s->setup, PetscObjectComm((PetscObject)s), PETSC_ERR_ARG_WRONGSTATE, "Cannot set includesConstraints after the section is set up");
78087e637c6Sksagiyam   s->includesConstraints = includesConstraints;
7813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78287e637c6Sksagiyam }
78387e637c6Sksagiyam 
78487e637c6Sksagiyam /*@
785ea844a1aSMatthew Knepley   PetscSectionGetDof - Return the number of degrees of freedom associated with a given point.
786ea844a1aSMatthew Knepley 
78740750d41SVaclav Hapla   Not Collective
788ea844a1aSMatthew Knepley 
789ea844a1aSMatthew Knepley   Input Parameters:
790cab54364SBarry Smith + s - the `PetscSection`
791ea844a1aSMatthew Knepley - point - the point
792ea844a1aSMatthew Knepley 
793ea844a1aSMatthew Knepley   Output Parameter:
794ea844a1aSMatthew Knepley . numDof - the number of dof
795ea844a1aSMatthew Knepley 
796db527f05SMatthew G. Knepley   Note:
797db527f05SMatthew G. Knepley   In a global section, this size will be negative for points not owned by this process.
798db527f05SMatthew G. Knepley 
799ea844a1aSMatthew Knepley   Level: intermediate
800ea844a1aSMatthew Knepley 
801cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetDof()`, `PetscSectionCreate()`
802ea844a1aSMatthew Knepley @*/
803d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetDof(PetscSection s, PetscInt point, PetscInt *numDof)
804d71ae5a4SJacob Faibussowitsch {
80576bd3646SJed Brown   PetscFunctionBeginHot;
806ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
807dadcf809SJacob Faibussowitsch   PetscValidIntPointer(numDof, 3);
808c8bf3acbSVaclav Hapla   PetscAssert(point >= s->pStart && point < s->pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Section point %" PetscInt_FMT " should be in [%" PetscInt_FMT ", %" PetscInt_FMT ")", point, s->pStart, s->pEnd);
809ea844a1aSMatthew Knepley   *numDof = s->atlasDof[point - s->pStart];
8103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
811ea844a1aSMatthew Knepley }
812ea844a1aSMatthew Knepley 
813ea844a1aSMatthew Knepley /*@
814ea844a1aSMatthew Knepley   PetscSectionSetDof - Sets the number of degrees of freedom associated with a given point.
815ea844a1aSMatthew Knepley 
81640750d41SVaclav Hapla   Not Collective
817ea844a1aSMatthew Knepley 
818ea844a1aSMatthew Knepley   Input Parameters:
819cab54364SBarry Smith + s - the `PetscSection`
820ea844a1aSMatthew Knepley . point - the point
821ea844a1aSMatthew Knepley - numDof - the number of dof
822ea844a1aSMatthew Knepley 
823ea844a1aSMatthew Knepley   Level: intermediate
824ea844a1aSMatthew Knepley 
825cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetDof()`, `PetscSectionAddDof()`, `PetscSectionCreate()`
826ea844a1aSMatthew Knepley @*/
827d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetDof(PetscSection s, PetscInt point, PetscInt numDof)
828d71ae5a4SJacob Faibussowitsch {
829ea844a1aSMatthew Knepley   PetscFunctionBegin;
830ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
831c8bf3acbSVaclav Hapla   PetscAssert(point >= s->pStart && point < s->pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Section point %" PetscInt_FMT " should be in [%" PetscInt_FMT ", %" PetscInt_FMT ")", point, s->pStart, s->pEnd);
832ea844a1aSMatthew Knepley   s->atlasDof[point - s->pStart] = numDof;
83369c11d05SVaclav Hapla   PetscCall(PetscSectionInvalidateMaxDof_Internal(s));
8343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
835ea844a1aSMatthew Knepley }
836ea844a1aSMatthew Knepley 
837ea844a1aSMatthew Knepley /*@
838ea844a1aSMatthew Knepley   PetscSectionAddDof - Adds to the number of degrees of freedom associated with a given point.
839ea844a1aSMatthew Knepley 
84040750d41SVaclav Hapla   Not Collective
841ea844a1aSMatthew Knepley 
842ea844a1aSMatthew Knepley   Input Parameters:
843cab54364SBarry Smith + s - the `PetscSection`
844ea844a1aSMatthew Knepley . point - the point
845ea844a1aSMatthew Knepley - numDof - the number of additional dof
846ea844a1aSMatthew Knepley 
847ea844a1aSMatthew Knepley   Level: intermediate
848ea844a1aSMatthew Knepley 
849cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetDof()`, `PetscSectionSetDof()`, `PetscSectionCreate()`
850ea844a1aSMatthew Knepley @*/
851d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionAddDof(PetscSection s, PetscInt point, PetscInt numDof)
852d71ae5a4SJacob Faibussowitsch {
85376bd3646SJed Brown   PetscFunctionBeginHot;
854ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
855c8bf3acbSVaclav Hapla   PetscAssert(point >= s->pStart && point < s->pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Section point %" PetscInt_FMT " should be in [%" PetscInt_FMT ", %" PetscInt_FMT ")", point, s->pStart, s->pEnd);
856ea844a1aSMatthew Knepley   s->atlasDof[point - s->pStart] += numDof;
85769c11d05SVaclav Hapla   PetscCall(PetscSectionInvalidateMaxDof_Internal(s));
8583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
859ea844a1aSMatthew Knepley }
860ea844a1aSMatthew Knepley 
861ea844a1aSMatthew Knepley /*@
862ea844a1aSMatthew Knepley   PetscSectionGetFieldDof - Return the number of degrees of freedom associated with a field on a given point.
863ea844a1aSMatthew Knepley 
86440750d41SVaclav Hapla   Not Collective
865ea844a1aSMatthew Knepley 
866ea844a1aSMatthew Knepley   Input Parameters:
867cab54364SBarry Smith + s - the `PetscSection`
868ea844a1aSMatthew Knepley . point - the point
869ea844a1aSMatthew Knepley - field - the field
870ea844a1aSMatthew Knepley 
871ea844a1aSMatthew Knepley   Output Parameter:
872ea844a1aSMatthew Knepley . numDof - the number of dof
873ea844a1aSMatthew Knepley 
874ea844a1aSMatthew Knepley   Level: intermediate
875ea844a1aSMatthew Knepley 
876cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetFieldDof()`, `PetscSectionCreate()`
877ea844a1aSMatthew Knepley @*/
878d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldDof(PetscSection s, PetscInt point, PetscInt field, PetscInt *numDof)
879d71ae5a4SJacob Faibussowitsch {
880ea844a1aSMatthew Knepley   PetscFunctionBegin;
881ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
8822abc8c78SJacob Faibussowitsch   PetscValidIntPointer(numDof, 4);
8832abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
8849566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetDof(s->field[field], point, numDof));
8853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
886ea844a1aSMatthew Knepley }
887ea844a1aSMatthew Knepley 
888ea844a1aSMatthew Knepley /*@
889ea844a1aSMatthew Knepley   PetscSectionSetFieldDof - Sets the number of degrees of freedom associated with a field on a given point.
890ea844a1aSMatthew Knepley 
89140750d41SVaclav Hapla   Not Collective
892ea844a1aSMatthew Knepley 
893ea844a1aSMatthew Knepley   Input Parameters:
894cab54364SBarry Smith + s - the `PetscSection`
895ea844a1aSMatthew Knepley . point - the point
896ea844a1aSMatthew Knepley . field - the field
897ea844a1aSMatthew Knepley - numDof - the number of dof
898ea844a1aSMatthew Knepley 
899ea844a1aSMatthew Knepley   Level: intermediate
900ea844a1aSMatthew Knepley 
901cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetFieldDof()`, `PetscSectionCreate()`
902ea844a1aSMatthew Knepley @*/
903d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFieldDof(PetscSection s, PetscInt point, PetscInt field, PetscInt numDof)
904d71ae5a4SJacob Faibussowitsch {
905ea844a1aSMatthew Knepley   PetscFunctionBegin;
906ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
9072abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
9089566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetDof(s->field[field], point, numDof));
9093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
910ea844a1aSMatthew Knepley }
911ea844a1aSMatthew Knepley 
912ea844a1aSMatthew Knepley /*@
913ea844a1aSMatthew Knepley   PetscSectionAddFieldDof - Adds a number of degrees of freedom associated with a field on a given point.
914ea844a1aSMatthew Knepley 
91540750d41SVaclav Hapla   Not Collective
916ea844a1aSMatthew Knepley 
917ea844a1aSMatthew Knepley   Input Parameters:
918cab54364SBarry Smith + s - the `PetscSection`
919ea844a1aSMatthew Knepley . point - the point
920ea844a1aSMatthew Knepley . field - the field
921ea844a1aSMatthew Knepley - numDof - the number of dof
922ea844a1aSMatthew Knepley 
923ea844a1aSMatthew Knepley   Level: intermediate
924ea844a1aSMatthew Knepley 
925cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetFieldDof()`, `PetscSectionGetFieldDof()`, `PetscSectionCreate()`
926ea844a1aSMatthew Knepley @*/
927d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionAddFieldDof(PetscSection s, PetscInt point, PetscInt field, PetscInt numDof)
928d71ae5a4SJacob Faibussowitsch {
929ea844a1aSMatthew Knepley   PetscFunctionBegin;
930ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
9312abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
9329566063dSJacob Faibussowitsch   PetscCall(PetscSectionAddDof(s->field[field], point, numDof));
9333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
934ea844a1aSMatthew Knepley }
935ea844a1aSMatthew Knepley 
936ea844a1aSMatthew Knepley /*@
937ea844a1aSMatthew Knepley   PetscSectionGetConstraintDof - Return the number of constrained degrees of freedom associated with a given point.
938ea844a1aSMatthew Knepley 
93940750d41SVaclav Hapla   Not Collective
940ea844a1aSMatthew Knepley 
941ea844a1aSMatthew Knepley   Input Parameters:
942cab54364SBarry Smith + s - the `PetscSection`
943ea844a1aSMatthew Knepley - point - the point
944ea844a1aSMatthew Knepley 
945ea844a1aSMatthew Knepley   Output Parameter:
946ea844a1aSMatthew Knepley . numDof - the number of dof which are fixed by constraints
947ea844a1aSMatthew Knepley 
948ea844a1aSMatthew Knepley   Level: intermediate
949ea844a1aSMatthew Knepley 
950cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetDof()`, `PetscSectionSetConstraintDof()`, `PetscSectionCreate()`
951ea844a1aSMatthew Knepley @*/
952d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetConstraintDof(PetscSection s, PetscInt point, PetscInt *numDof)
953d71ae5a4SJacob Faibussowitsch {
954ea844a1aSMatthew Knepley   PetscFunctionBegin;
955ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
956dadcf809SJacob Faibussowitsch   PetscValidIntPointer(numDof, 3);
957ea844a1aSMatthew Knepley   if (s->bc) {
9589566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s->bc, point, numDof));
959ea844a1aSMatthew Knepley   } else *numDof = 0;
9603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
961ea844a1aSMatthew Knepley }
962ea844a1aSMatthew Knepley 
963ea844a1aSMatthew Knepley /*@
964ea844a1aSMatthew Knepley   PetscSectionSetConstraintDof - Set the number of constrained degrees of freedom associated with a given point.
965ea844a1aSMatthew Knepley 
96640750d41SVaclav Hapla   Not Collective
967ea844a1aSMatthew Knepley 
968ea844a1aSMatthew Knepley   Input Parameters:
969cab54364SBarry Smith + s - the `PetscSection`
970ea844a1aSMatthew Knepley . point - the point
971ea844a1aSMatthew Knepley - numDof - the number of dof which are fixed by constraints
972ea844a1aSMatthew Knepley 
973ea844a1aSMatthew Knepley   Level: intermediate
974ea844a1aSMatthew Knepley 
975cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetDof()`, `PetscSectionGetConstraintDof()`, `PetscSectionCreate()`
976ea844a1aSMatthew Knepley @*/
977d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetConstraintDof(PetscSection s, PetscInt point, PetscInt numDof)
978d71ae5a4SJacob Faibussowitsch {
979ea844a1aSMatthew Knepley   PetscFunctionBegin;
980ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
981ea844a1aSMatthew Knepley   if (numDof) {
9827c0883d5SVaclav Hapla     PetscCall(PetscSectionCheckConstraints_Private(s));
9839566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(s->bc, point, numDof));
984ea844a1aSMatthew Knepley   }
9853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
986ea844a1aSMatthew Knepley }
987ea844a1aSMatthew Knepley 
988ea844a1aSMatthew Knepley /*@
989ea844a1aSMatthew Knepley   PetscSectionAddConstraintDof - Increment the number of constrained degrees of freedom associated with a given point.
990ea844a1aSMatthew Knepley 
99140750d41SVaclav Hapla   Not Collective
992ea844a1aSMatthew Knepley 
993ea844a1aSMatthew Knepley   Input Parameters:
994cab54364SBarry Smith + s - the `PetscSection`
995ea844a1aSMatthew Knepley . point - the point
996ea844a1aSMatthew Knepley - numDof - the number of additional dof which are fixed by constraints
997ea844a1aSMatthew Knepley 
998ea844a1aSMatthew Knepley   Level: intermediate
999ea844a1aSMatthew Knepley 
1000cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionAddDof()`, `PetscSectionGetConstraintDof()`, `PetscSectionCreate()`
1001ea844a1aSMatthew Knepley @*/
1002d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionAddConstraintDof(PetscSection s, PetscInt point, PetscInt numDof)
1003d71ae5a4SJacob Faibussowitsch {
1004ea844a1aSMatthew Knepley   PetscFunctionBegin;
1005ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1006ea844a1aSMatthew Knepley   if (numDof) {
10077c0883d5SVaclav Hapla     PetscCall(PetscSectionCheckConstraints_Private(s));
10089566063dSJacob Faibussowitsch     PetscCall(PetscSectionAddDof(s->bc, point, numDof));
1009ea844a1aSMatthew Knepley   }
10103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1011ea844a1aSMatthew Knepley }
1012ea844a1aSMatthew Knepley 
1013ea844a1aSMatthew Knepley /*@
1014ea844a1aSMatthew Knepley   PetscSectionGetFieldConstraintDof - Return the number of constrained degrees of freedom associated with a given field on a point.
1015ea844a1aSMatthew Knepley 
101640750d41SVaclav Hapla   Not Collective
1017ea844a1aSMatthew Knepley 
1018ea844a1aSMatthew Knepley   Input Parameters:
1019cab54364SBarry Smith + s - the `PetscSection`
1020ea844a1aSMatthew Knepley . point - the point
1021ea844a1aSMatthew Knepley - field - the field
1022ea844a1aSMatthew Knepley 
1023ea844a1aSMatthew Knepley   Output Parameter:
1024ea844a1aSMatthew Knepley . numDof - the number of dof which are fixed by constraints
1025ea844a1aSMatthew Knepley 
1026ea844a1aSMatthew Knepley   Level: intermediate
1027ea844a1aSMatthew Knepley 
1028cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetDof()`, `PetscSectionSetFieldConstraintDof()`, `PetscSectionCreate()`
1029ea844a1aSMatthew Knepley @*/
1030d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldConstraintDof(PetscSection s, PetscInt point, PetscInt field, PetscInt *numDof)
1031d71ae5a4SJacob Faibussowitsch {
1032ea844a1aSMatthew Knepley   PetscFunctionBegin;
1033ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
10342abc8c78SJacob Faibussowitsch   PetscValidIntPointer(numDof, 4);
10352abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
10369566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstraintDof(s->field[field], point, numDof));
10373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1038ea844a1aSMatthew Knepley }
1039ea844a1aSMatthew Knepley 
1040ea844a1aSMatthew Knepley /*@
1041ea844a1aSMatthew Knepley   PetscSectionSetFieldConstraintDof - Set the number of constrained degrees of freedom associated with a given field on a point.
1042ea844a1aSMatthew Knepley 
104340750d41SVaclav Hapla   Not Collective
1044ea844a1aSMatthew Knepley 
1045ea844a1aSMatthew Knepley   Input Parameters:
1046cab54364SBarry Smith + s - the `PetscSection`
1047ea844a1aSMatthew Knepley . point - the point
1048ea844a1aSMatthew Knepley . field - the field
1049ea844a1aSMatthew Knepley - numDof - the number of dof which are fixed by constraints
1050ea844a1aSMatthew Knepley 
1051ea844a1aSMatthew Knepley   Level: intermediate
1052ea844a1aSMatthew Knepley 
1053cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetDof()`, `PetscSectionGetFieldConstraintDof()`, `PetscSectionCreate()`
1054ea844a1aSMatthew Knepley @*/
1055d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFieldConstraintDof(PetscSection s, PetscInt point, PetscInt field, PetscInt numDof)
1056d71ae5a4SJacob Faibussowitsch {
1057ea844a1aSMatthew Knepley   PetscFunctionBegin;
1058ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
10592abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
10609566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetConstraintDof(s->field[field], point, numDof));
10613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1062ea844a1aSMatthew Knepley }
1063ea844a1aSMatthew Knepley 
1064ea844a1aSMatthew Knepley /*@
1065ea844a1aSMatthew Knepley   PetscSectionAddFieldConstraintDof - Increment the number of constrained degrees of freedom associated with a given field on a point.
1066ea844a1aSMatthew Knepley 
106740750d41SVaclav Hapla   Not Collective
1068ea844a1aSMatthew Knepley 
1069ea844a1aSMatthew Knepley   Input Parameters:
1070cab54364SBarry Smith + s - the `PetscSection`
1071ea844a1aSMatthew Knepley . point - the point
1072ea844a1aSMatthew Knepley . field - the field
1073ea844a1aSMatthew Knepley - numDof - the number of additional dof which are fixed by constraints
1074ea844a1aSMatthew Knepley 
1075ea844a1aSMatthew Knepley   Level: intermediate
1076ea844a1aSMatthew Knepley 
1077cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionAddDof()`, `PetscSectionGetFieldConstraintDof()`, `PetscSectionCreate()`
1078ea844a1aSMatthew Knepley @*/
1079d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionAddFieldConstraintDof(PetscSection s, PetscInt point, PetscInt field, PetscInt numDof)
1080d71ae5a4SJacob Faibussowitsch {
1081ea844a1aSMatthew Knepley   PetscFunctionBegin;
1082ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
10832abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
10849566063dSJacob Faibussowitsch   PetscCall(PetscSectionAddConstraintDof(s->field[field], point, numDof));
10853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1086ea844a1aSMatthew Knepley }
1087ea844a1aSMatthew Knepley 
1088ea844a1aSMatthew Knepley /*@
1089ea844a1aSMatthew Knepley   PetscSectionSetUpBC - Setup the subsections describing boundary conditions.
1090ea844a1aSMatthew Knepley 
109140750d41SVaclav Hapla   Not Collective
1092ea844a1aSMatthew Knepley 
1093ea844a1aSMatthew Knepley   Input Parameter:
1094cab54364SBarry Smith . s - the `PetscSection`
1095ea844a1aSMatthew Knepley 
1096ea844a1aSMatthew Knepley   Level: advanced
1097ea844a1aSMatthew Knepley 
1098cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetUp()`, `PetscSectionCreate()`
1099ea844a1aSMatthew Knepley @*/
1100d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetUpBC(PetscSection s)
1101d71ae5a4SJacob Faibussowitsch {
1102ea844a1aSMatthew Knepley   PetscFunctionBegin;
1103ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1104ea844a1aSMatthew Knepley   if (s->bc) {
1105ea844a1aSMatthew Knepley     const PetscInt last = (s->bc->pEnd - s->bc->pStart) - 1;
1106ea844a1aSMatthew Knepley 
11079566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(s->bc));
11088da24d32SBarry Smith     PetscCall(PetscMalloc1((last >= 0 ? s->bc->atlasOff[last] + s->bc->atlasDof[last] : 0), &s->bcIndices));
1109ea844a1aSMatthew Knepley   }
11103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1111ea844a1aSMatthew Knepley }
1112ea844a1aSMatthew Knepley 
1113ea844a1aSMatthew Knepley /*@
1114ea844a1aSMatthew Knepley   PetscSectionSetUp - Calculate offsets based upon the number of degrees of freedom for each point.
1115ea844a1aSMatthew Knepley 
111640750d41SVaclav Hapla   Not Collective
1117ea844a1aSMatthew Knepley 
1118ea844a1aSMatthew Knepley   Input Parameter:
1119cab54364SBarry Smith . s - the `PetscSection`
1120ea844a1aSMatthew Knepley 
1121ea844a1aSMatthew Knepley   Level: intermediate
1122ea844a1aSMatthew Knepley 
1123cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreate()`
1124ea844a1aSMatthew Knepley @*/
1125d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetUp(PetscSection s)
1126d71ae5a4SJacob Faibussowitsch {
1127ea844a1aSMatthew Knepley   const PetscInt *pind   = NULL;
1128ea844a1aSMatthew Knepley   PetscInt        offset = 0, foff, p, f;
1129ea844a1aSMatthew Knepley 
1130ea844a1aSMatthew Knepley   PetscFunctionBegin;
1131ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
11323ba16761SJacob Faibussowitsch   if (s->setup) PetscFunctionReturn(PETSC_SUCCESS);
1133ea844a1aSMatthew Knepley   s->setup = PETSC_TRUE;
1134ea844a1aSMatthew Knepley   /* Set offsets and field offsets for all points */
1135ea844a1aSMatthew Knepley   /*   Assume that all fields have the same chart */
113628b400f6SJacob Faibussowitsch   PetscCheck(s->includesConstraints, PETSC_COMM_SELF, PETSC_ERR_SUP, "PetscSectionSetUp is currently unsupported for includesConstraints = PETSC_TRUE");
11379566063dSJacob Faibussowitsch   if (s->perm) PetscCall(ISGetIndices(s->perm, &pind));
1138ea844a1aSMatthew Knepley   if (s->pointMajor) {
1139ea844a1aSMatthew Knepley     for (p = 0; p < s->pEnd - s->pStart; ++p) {
1140ea844a1aSMatthew Knepley       const PetscInt q = pind ? pind[p] : p;
1141ea844a1aSMatthew Knepley 
1142ea844a1aSMatthew Knepley       /* Set point offset */
1143ea844a1aSMatthew Knepley       s->atlasOff[q] = offset;
1144ea844a1aSMatthew Knepley       offset += s->atlasDof[q];
1145ea844a1aSMatthew Knepley       /* Set field offset */
1146ea844a1aSMatthew Knepley       for (f = 0, foff = s->atlasOff[q]; f < s->numFields; ++f) {
1147ea844a1aSMatthew Knepley         PetscSection sf = s->field[f];
1148ea844a1aSMatthew Knepley 
1149ea844a1aSMatthew Knepley         sf->atlasOff[q] = foff;
1150ea844a1aSMatthew Knepley         foff += sf->atlasDof[q];
1151ea844a1aSMatthew Knepley       }
1152ea844a1aSMatthew Knepley     }
1153ea844a1aSMatthew Knepley   } else {
1154ea844a1aSMatthew Knepley     /* Set field offsets for all points */
1155ea844a1aSMatthew Knepley     for (f = 0; f < s->numFields; ++f) {
1156ea844a1aSMatthew Knepley       PetscSection sf = s->field[f];
1157ea844a1aSMatthew Knepley 
1158ea844a1aSMatthew Knepley       for (p = 0; p < s->pEnd - s->pStart; ++p) {
1159ea844a1aSMatthew Knepley         const PetscInt q = pind ? pind[p] : p;
1160ea844a1aSMatthew Knepley 
1161ea844a1aSMatthew Knepley         sf->atlasOff[q] = offset;
1162ea844a1aSMatthew Knepley         offset += sf->atlasDof[q];
1163ea844a1aSMatthew Knepley       }
1164ea844a1aSMatthew Knepley     }
1165ea844a1aSMatthew Knepley     /* Disable point offsets since these are unused */
1166ad540459SPierre Jolivet     for (p = 0; p < s->pEnd - s->pStart; ++p) s->atlasOff[p] = -1;
1167ea844a1aSMatthew Knepley   }
11689566063dSJacob Faibussowitsch   if (s->perm) PetscCall(ISRestoreIndices(s->perm, &pind));
1169ea844a1aSMatthew Knepley   /* Setup BC sections */
11709566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUpBC(s));
11719566063dSJacob Faibussowitsch   for (f = 0; f < s->numFields; ++f) PetscCall(PetscSectionSetUpBC(s->field[f]));
11723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1173ea844a1aSMatthew Knepley }
1174ea844a1aSMatthew Knepley 
1175ea844a1aSMatthew Knepley /*@
117620662ed9SBarry Smith   PetscSectionGetMaxDof - Return the maximum number of degrees of freedom on any point in the `PetscSection`
1177ea844a1aSMatthew Knepley 
117840750d41SVaclav Hapla   Not Collective
1179ea844a1aSMatthew Knepley 
1180*2fe279fdSBarry Smith   Input Parameter:
1181cab54364SBarry Smith . s - the `PetscSection`
1182ea844a1aSMatthew Knepley 
1183ea844a1aSMatthew Knepley   Output Parameter:
1184ea844a1aSMatthew Knepley . maxDof - the maximum dof
1185ea844a1aSMatthew Knepley 
1186ea844a1aSMatthew Knepley   Level: intermediate
1187ea844a1aSMatthew Knepley 
1188cab54364SBarry Smith   Note:
1189cab54364SBarry Smith   The returned number is up-to-date without need for `PetscSectionSetUp()`.
119069c11d05SVaclav Hapla 
1191cab54364SBarry Smith   Developer Note:
119269c11d05SVaclav Hapla   The returned number is calculated lazily and stashed.
1193cab54364SBarry Smith 
1194cab54364SBarry Smith   A call to `PetscSectionInvalidateMaxDof_Internal()` invalidates the stashed value.
1195cab54364SBarry Smith 
1196cab54364SBarry Smith   `PetscSectionInvalidateMaxDof_Internal()` is called in `PetscSectionSetDof()`, `PetscSectionAddDof()` and `PetscSectionReset()`
1197cab54364SBarry Smith 
119820662ed9SBarry Smith   It should also be called every time `atlasDof` is modified directly.
119969c11d05SVaclav Hapla 
1200cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetDof()`, `PetscSectionSetDof()`, `PetscSectionAddDof()`, `PetscSectionCreate()`
1201ea844a1aSMatthew Knepley @*/
1202d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetMaxDof(PetscSection s, PetscInt *maxDof)
1203d71ae5a4SJacob Faibussowitsch {
120469c11d05SVaclav Hapla   PetscInt p;
120569c11d05SVaclav Hapla 
1206ea844a1aSMatthew Knepley   PetscFunctionBegin;
1207ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1208dadcf809SJacob Faibussowitsch   PetscValidIntPointer(maxDof, 2);
120969c11d05SVaclav Hapla   if (s->maxDof == PETSC_MIN_INT) {
121069c11d05SVaclav Hapla     s->maxDof = 0;
1211ad540459SPierre Jolivet     for (p = 0; p < s->pEnd - s->pStart; ++p) s->maxDof = PetscMax(s->maxDof, s->atlasDof[p]);
121269c11d05SVaclav Hapla   }
1213ea844a1aSMatthew Knepley   *maxDof = s->maxDof;
12143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1215ea844a1aSMatthew Knepley }
1216ea844a1aSMatthew Knepley 
1217ea844a1aSMatthew Knepley /*@
121820662ed9SBarry Smith   PetscSectionGetStorageSize - Return the size of an array or local `Vec` capable of holding all the degrees of freedom defined in a `PetscSection`
1219ea844a1aSMatthew Knepley 
122040750d41SVaclav Hapla   Not Collective
1221ea844a1aSMatthew Knepley 
1222ea844a1aSMatthew Knepley   Input Parameter:
1223cab54364SBarry Smith . s - the `PetscSection`
1224ea844a1aSMatthew Knepley 
1225ea844a1aSMatthew Knepley   Output Parameter:
1226ea844a1aSMatthew Knepley . size - the size of an array which can hold all the dofs
1227ea844a1aSMatthew Knepley 
1228ea844a1aSMatthew Knepley   Level: intermediate
1229ea844a1aSMatthew Knepley 
1230cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetOffset()`, `PetscSectionGetConstrainedStorageSize()`, `PetscSectionCreate()`
1231ea844a1aSMatthew Knepley @*/
1232d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetStorageSize(PetscSection s, PetscInt *size)
1233d71ae5a4SJacob Faibussowitsch {
1234ea844a1aSMatthew Knepley   PetscInt p, n = 0;
1235ea844a1aSMatthew Knepley 
1236ea844a1aSMatthew Knepley   PetscFunctionBegin;
1237ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1238dadcf809SJacob Faibussowitsch   PetscValidIntPointer(size, 2);
1239ea844a1aSMatthew Knepley   for (p = 0; p < s->pEnd - s->pStart; ++p) n += s->atlasDof[p] > 0 ? s->atlasDof[p] : 0;
1240ea844a1aSMatthew Knepley   *size = n;
12413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1242ea844a1aSMatthew Knepley }
1243ea844a1aSMatthew Knepley 
1244ea844a1aSMatthew Knepley /*@
124520662ed9SBarry Smith   PetscSectionGetConstrainedStorageSize - Return the size of an array or local `Vec` capable of holding all unconstrained degrees of freedom in a `PetscSection`
1246ea844a1aSMatthew Knepley 
124740750d41SVaclav Hapla   Not Collective
1248ea844a1aSMatthew Knepley 
1249064ec1f3Sprj-   Input Parameter:
1250cab54364SBarry Smith . s - the `PetscSection`
1251ea844a1aSMatthew Knepley 
1252ea844a1aSMatthew Knepley   Output Parameter:
1253ea844a1aSMatthew Knepley . size - the size of an array which can hold all unconstrained dofs
1254ea844a1aSMatthew Knepley 
1255ea844a1aSMatthew Knepley   Level: intermediate
1256ea844a1aSMatthew Knepley 
1257cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetStorageSize()`, `PetscSectionGetOffset()`, `PetscSectionCreate()`
1258ea844a1aSMatthew Knepley @*/
1259d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetConstrainedStorageSize(PetscSection s, PetscInt *size)
1260d71ae5a4SJacob Faibussowitsch {
1261ea844a1aSMatthew Knepley   PetscInt p, n = 0;
1262ea844a1aSMatthew Knepley 
1263ea844a1aSMatthew Knepley   PetscFunctionBegin;
1264ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1265dadcf809SJacob Faibussowitsch   PetscValidIntPointer(size, 2);
1266ea844a1aSMatthew Knepley   for (p = 0; p < s->pEnd - s->pStart; ++p) {
1267ea844a1aSMatthew Knepley     const PetscInt cdof = s->bc ? s->bc->atlasDof[p] : 0;
1268ea844a1aSMatthew Knepley     n += s->atlasDof[p] > 0 ? s->atlasDof[p] - cdof : 0;
1269ea844a1aSMatthew Knepley   }
1270ea844a1aSMatthew Knepley   *size = n;
12713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1272ea844a1aSMatthew Knepley }
1273ea844a1aSMatthew Knepley 
1274ea844a1aSMatthew Knepley /*@
1275ea844a1aSMatthew Knepley   PetscSectionCreateGlobalSection - Create a section describing the global field layout using
1276cab54364SBarry Smith   the local section and a `PetscSF` describing the section point overlap.
1277ea844a1aSMatthew Knepley 
1278ea844a1aSMatthew Knepley   Input Parameters:
1279cab54364SBarry Smith + s - The `PetscSection` for the local field layout
1280cab54364SBarry Smith . sf - The `PetscSF` describing parallel layout of the section points (leaves are unowned local points)
1281cab54364SBarry Smith . includeConstraints - By default this is `PETSC_FALSE`, meaning that the global field vector will not possess constrained dofs
1282cab54364SBarry Smith - localOffsets - If `PETSC_TRUE`, use local rather than global offsets for the points
1283ea844a1aSMatthew Knepley 
1284ea844a1aSMatthew Knepley   Output Parameter:
1285cab54364SBarry Smith . gsection - The `PetscSection` for the global field layout
1286ea844a1aSMatthew Knepley 
1287ea844a1aSMatthew Knepley   Level: intermediate
1288ea844a1aSMatthew Knepley 
1289db527f05SMatthew G. Knepley   Notes:
129020662ed9SBarry Smith   If we have a set of local sections defining the layout of a set of local vectors, and also a `PetscSF` to determine which section points are shared and the ownership,
129120662ed9SBarry Smith   we can calculate a global section defining the parallel data layout, and the associated global vector.
1292db527f05SMatthew G. Knepley 
1293cab54364SBarry Smith   This gives negative sizes and offsets to points not owned by this process
1294cab54364SBarry Smith 
129520662ed9SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionCreateGlobalSectionCensored()`
1296ea844a1aSMatthew Knepley @*/
1297d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCreateGlobalSection(PetscSection s, PetscSF sf, PetscBool includeConstraints, PetscBool localOffsets, PetscSection *gsection)
1298d71ae5a4SJacob Faibussowitsch {
1299ea844a1aSMatthew Knepley   PetscSection    gs;
1300ea844a1aSMatthew Knepley   const PetscInt *pind = NULL;
1301ea844a1aSMatthew Knepley   PetscInt       *recv = NULL, *neg = NULL;
1302046d2115Sksagiyam   PetscInt        pStart, pEnd, p, dof, cdof, off, foff, globalOff = 0, nroots, nlocal, maxleaf;
1303046d2115Sksagiyam   PetscInt        numFields, f, numComponents;
1304ea844a1aSMatthew Knepley 
1305ea844a1aSMatthew Knepley   PetscFunctionBegin;
1306ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1307ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
1308ea844a1aSMatthew Knepley   PetscValidLogicalCollectiveBool(s, includeConstraints, 3);
1309ea844a1aSMatthew Knepley   PetscValidLogicalCollectiveBool(s, localOffsets, 4);
1310ea844a1aSMatthew Knepley   PetscValidPointer(gsection, 5);
131128b400f6SJacob Faibussowitsch   PetscCheck(s->pointMajor, PETSC_COMM_SELF, PETSC_ERR_SUP, "No support for field major ordering");
13129566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s), &gs));
13139566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &numFields));
13149566063dSJacob Faibussowitsch   if (numFields > 0) PetscCall(PetscSectionSetNumFields(gs, numFields));
13159566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
13169566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(gs, pStart, pEnd));
131787e637c6Sksagiyam   gs->includesConstraints = includeConstraints;
13189566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, &nroots, NULL, NULL, NULL));
1319ea844a1aSMatthew Knepley   nlocal = nroots; /* The local/leaf space matches global/root space */
1320ea844a1aSMatthew Knepley   /* Must allocate for all points visible to SF, which may be more than this section */
1321ea844a1aSMatthew Knepley   if (nroots >= 0) { /* nroots < 0 means that the graph has not been set, only happens in serial */
13229566063dSJacob Faibussowitsch     PetscCall(PetscSFGetLeafRange(sf, NULL, &maxleaf));
132308401ef6SPierre Jolivet     PetscCheck(nroots >= pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "SF roots %" PetscInt_FMT " < pEnd %" PetscInt_FMT, nroots, pEnd);
132408401ef6SPierre Jolivet     PetscCheck(maxleaf < nroots, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Max local leaf %" PetscInt_FMT " >= nroots %" PetscInt_FMT, maxleaf, nroots);
13259566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(nroots, &neg, nlocal, &recv));
13269566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(neg, nroots));
1327ea844a1aSMatthew Knepley   }
1328ea844a1aSMatthew Knepley   /* Mark all local points with negative dof */
1329ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
13309566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s, p, &dof));
13319566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(gs, p, dof));
13329566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
13339566063dSJacob Faibussowitsch     if (!includeConstraints && cdof > 0) PetscCall(PetscSectionSetConstraintDof(gs, p, cdof));
1334ea844a1aSMatthew Knepley     if (neg) neg[p] = -(dof + 1);
1335ea844a1aSMatthew Knepley   }
13369566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUpBC(gs));
13379566063dSJacob Faibussowitsch   if (gs->bcIndices) PetscCall(PetscArraycpy(gs->bcIndices, s->bcIndices, gs->bc->atlasOff[gs->bc->pEnd - gs->bc->pStart - 1] + gs->bc->atlasDof[gs->bc->pEnd - gs->bc->pStart - 1]));
1338ea844a1aSMatthew Knepley   if (nroots >= 0) {
13399566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(recv, nlocal));
13409566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, neg, recv, MPI_REPLACE));
13419566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, neg, recv, MPI_REPLACE));
1342ea844a1aSMatthew Knepley     for (p = pStart; p < pEnd; ++p) {
1343ea844a1aSMatthew Knepley       if (recv[p] < 0) {
1344ea844a1aSMatthew Knepley         gs->atlasDof[p - pStart] = recv[p];
13459566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
134608401ef6SPierre Jolivet         PetscCheck(-(recv[p] + 1) == dof, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Global dof %" PetscInt_FMT " for point %" PetscInt_FMT " is not the unconstrained %" PetscInt_FMT, -(recv[p] + 1), p, dof);
1347ea844a1aSMatthew Knepley       }
1348ea844a1aSMatthew Knepley     }
1349ea844a1aSMatthew Knepley   }
1350ea844a1aSMatthew Knepley   /* Calculate new sizes, get process offset, and calculate point offsets */
13519566063dSJacob Faibussowitsch   if (s->perm) PetscCall(ISGetIndices(s->perm, &pind));
1352ea844a1aSMatthew Knepley   for (p = 0, off = 0; p < pEnd - pStart; ++p) {
1353ea844a1aSMatthew Knepley     const PetscInt q = pind ? pind[p] : p;
1354ea844a1aSMatthew Knepley 
1355ea844a1aSMatthew Knepley     cdof            = (!includeConstraints && s->bc) ? s->bc->atlasDof[q] : 0;
1356ea844a1aSMatthew Knepley     gs->atlasOff[q] = off;
1357ea844a1aSMatthew Knepley     off += gs->atlasDof[q] > 0 ? gs->atlasDof[q] - cdof : 0;
1358ea844a1aSMatthew Knepley   }
1359ea844a1aSMatthew Knepley   if (!localOffsets) {
13609566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Scan(&off, &globalOff, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)s)));
1361ea844a1aSMatthew Knepley     globalOff -= off;
1362ea844a1aSMatthew Knepley   }
1363ea844a1aSMatthew Knepley   for (p = pStart, off = 0; p < pEnd; ++p) {
1364ea844a1aSMatthew Knepley     gs->atlasOff[p - pStart] += globalOff;
1365ea844a1aSMatthew Knepley     if (neg) neg[p] = -(gs->atlasOff[p - pStart] + 1);
1366ea844a1aSMatthew Knepley   }
13679566063dSJacob Faibussowitsch   if (s->perm) PetscCall(ISRestoreIndices(s->perm, &pind));
1368ea844a1aSMatthew Knepley   /* Put in negative offsets for ghost points */
1369ea844a1aSMatthew Knepley   if (nroots >= 0) {
13709566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(recv, nlocal));
13719566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, neg, recv, MPI_REPLACE));
13729566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, neg, recv, MPI_REPLACE));
1373ea844a1aSMatthew Knepley     for (p = pStart; p < pEnd; ++p) {
1374ea844a1aSMatthew Knepley       if (recv[p] < 0) gs->atlasOff[p - pStart] = recv[p];
1375ea844a1aSMatthew Knepley     }
1376ea844a1aSMatthew Knepley   }
13779566063dSJacob Faibussowitsch   PetscCall(PetscFree2(neg, recv));
1378046d2115Sksagiyam   /* Set field dofs/offsets/constraints */
1379046d2115Sksagiyam   for (f = 0; f < numFields; ++f) {
1380046d2115Sksagiyam     gs->field[f]->includesConstraints = includeConstraints;
13819566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &numComponents));
13829566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(gs, f, numComponents));
1383046d2115Sksagiyam   }
1384046d2115Sksagiyam   for (p = pStart; p < pEnd; ++p) {
13859566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(gs, p, &off));
1386046d2115Sksagiyam     for (f = 0, foff = off; f < numFields; ++f) {
13879566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(s, p, f, &cdof));
13889566063dSJacob Faibussowitsch       if (!includeConstraints && cdof > 0) PetscCall(PetscSectionSetFieldConstraintDof(gs, p, f, cdof));
13899566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
13909566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(gs, p, f, off < 0 ? -(dof + 1) : dof));
13919566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldOffset(gs, p, f, foff));
13929566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(gs, p, f, &cdof));
1393046d2115Sksagiyam       foff = off < 0 ? foff - (dof - cdof) : foff + (dof - cdof);
1394046d2115Sksagiyam     }
1395046d2115Sksagiyam   }
1396046d2115Sksagiyam   for (f = 0; f < numFields; ++f) {
1397046d2115Sksagiyam     PetscSection gfs = gs->field[f];
1398046d2115Sksagiyam 
13999566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUpBC(gfs));
14009566063dSJacob Faibussowitsch     if (gfs->bcIndices) PetscCall(PetscArraycpy(gfs->bcIndices, s->field[f]->bcIndices, gfs->bc->atlasOff[gfs->bc->pEnd - gfs->bc->pStart - 1] + gfs->bc->atlasDof[gfs->bc->pEnd - gfs->bc->pStart - 1]));
1401046d2115Sksagiyam   }
1402046d2115Sksagiyam   gs->setup = PETSC_TRUE;
14039566063dSJacob Faibussowitsch   PetscCall(PetscSectionViewFromOptions(gs, NULL, "-global_section_view"));
1404ea844a1aSMatthew Knepley   *gsection = gs;
14053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1406ea844a1aSMatthew Knepley }
1407ea844a1aSMatthew Knepley 
1408ea844a1aSMatthew Knepley /*@
1409cab54364SBarry Smith   PetscSectionCreateGlobalSectionCensored - Create a `PetscSection` describing the global field layout using
1410cab54364SBarry Smith   the local section and an `PetscSF` describing the section point overlap.
1411ea844a1aSMatthew Knepley 
1412ea844a1aSMatthew Knepley   Input Parameters:
1413cab54364SBarry Smith + s - The `PetscSection` for the local field layout
1414cab54364SBarry Smith . sf - The `PetscSF` describing parallel layout of the section points
1415cab54364SBarry Smith . includeConstraints - By default this is `PETSC_FALSE`, meaning that the global field vector will not possess constrained dofs
1416ea844a1aSMatthew Knepley . numExcludes - The number of exclusion ranges
1417ea844a1aSMatthew Knepley - excludes - An array [start_0, end_0, start_1, end_1, ...] where there are numExcludes pairs
1418ea844a1aSMatthew Knepley 
1419ea844a1aSMatthew Knepley   Output Parameter:
1420cab54364SBarry Smith . gsection - The `PetscSection` for the global field layout
1421ea844a1aSMatthew Knepley 
1422ea844a1aSMatthew Knepley   Level: advanced
1423ea844a1aSMatthew Knepley 
1424cab54364SBarry Smith   Note:
142520662ed9SBarry Smith   This routine augments `PetscSectionCreateGlobalSection()` by allowing one to exclude certain ranges in the chart of the `PetscSection`
142620662ed9SBarry Smith 
1427cab54364SBarry Smith   This gives negative sizes and offsets to points not owned by this process
1428cab54364SBarry Smith 
142920662ed9SBarry Smith   Developer Note:
143020662ed9SBarry Smith   This is a terrible function name
143120662ed9SBarry Smith 
143220662ed9SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionCreateGlobalSectionCensored()`
1433ea844a1aSMatthew Knepley @*/
1434d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCreateGlobalSectionCensored(PetscSection s, PetscSF sf, PetscBool includeConstraints, PetscInt numExcludes, const PetscInt excludes[], PetscSection *gsection)
1435d71ae5a4SJacob Faibussowitsch {
1436ea844a1aSMatthew Knepley   const PetscInt *pind = NULL;
1437ea844a1aSMatthew Knepley   PetscInt       *neg = NULL, *tmpOff = NULL;
1438ea844a1aSMatthew Knepley   PetscInt        pStart, pEnd, p, e, dof, cdof, off, globalOff = 0, nroots;
1439ea844a1aSMatthew Knepley 
1440ea844a1aSMatthew Knepley   PetscFunctionBegin;
1441ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1442ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
1443ea844a1aSMatthew Knepley   PetscValidPointer(gsection, 6);
14449566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s), gsection));
14459566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
14469566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(*gsection, pStart, pEnd));
14479566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, &nroots, NULL, NULL, NULL));
1448ea844a1aSMatthew Knepley   if (nroots >= 0) {
144908401ef6SPierre Jolivet     PetscCheck(nroots >= pEnd - pStart, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "PetscSF nroots %" PetscInt_FMT " < %" PetscInt_FMT " section size", nroots, pEnd - pStart);
14509566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(nroots, &neg));
1451ea844a1aSMatthew Knepley     if (nroots > pEnd - pStart) {
14529566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(nroots, &tmpOff));
1453ea844a1aSMatthew Knepley     } else {
1454ea844a1aSMatthew Knepley       tmpOff = &(*gsection)->atlasDof[-pStart];
1455ea844a1aSMatthew Knepley     }
1456ea844a1aSMatthew Knepley   }
1457ea844a1aSMatthew Knepley   /* Mark ghost points with negative dof */
1458ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
1459ea844a1aSMatthew Knepley     for (e = 0; e < numExcludes; ++e) {
1460ea844a1aSMatthew Knepley       if ((p >= excludes[e * 2 + 0]) && (p < excludes[e * 2 + 1])) {
14619566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetDof(*gsection, p, 0));
1462ea844a1aSMatthew Knepley         break;
1463ea844a1aSMatthew Knepley       }
1464ea844a1aSMatthew Knepley     }
1465ea844a1aSMatthew Knepley     if (e < numExcludes) continue;
14669566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s, p, &dof));
14679566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(*gsection, p, dof));
14689566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
14699566063dSJacob Faibussowitsch     if (!includeConstraints && cdof > 0) PetscCall(PetscSectionSetConstraintDof(*gsection, p, cdof));
1470ea844a1aSMatthew Knepley     if (neg) neg[p] = -(dof + 1);
1471ea844a1aSMatthew Knepley   }
14729566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUpBC(*gsection));
1473ea844a1aSMatthew Knepley   if (nroots >= 0) {
14749566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, neg, tmpOff, MPI_REPLACE));
14759566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, neg, tmpOff, MPI_REPLACE));
1476ea844a1aSMatthew Knepley     if (nroots > pEnd - pStart) {
14779371c9d4SSatish Balay       for (p = pStart; p < pEnd; ++p) {
14789371c9d4SSatish Balay         if (tmpOff[p] < 0) (*gsection)->atlasDof[p - pStart] = tmpOff[p];
14799371c9d4SSatish Balay       }
1480ea844a1aSMatthew Knepley     }
1481ea844a1aSMatthew Knepley   }
148235cb6cd3SPierre Jolivet   /* Calculate new sizes, get process offset, and calculate point offsets */
14839566063dSJacob Faibussowitsch   if (s->perm) PetscCall(ISGetIndices(s->perm, &pind));
1484ea844a1aSMatthew Knepley   for (p = 0, off = 0; p < pEnd - pStart; ++p) {
1485ea844a1aSMatthew Knepley     const PetscInt q = pind ? pind[p] : p;
1486ea844a1aSMatthew Knepley 
1487ea844a1aSMatthew Knepley     cdof                     = (!includeConstraints && s->bc) ? s->bc->atlasDof[q] : 0;
1488ea844a1aSMatthew Knepley     (*gsection)->atlasOff[q] = off;
1489ea844a1aSMatthew Knepley     off += (*gsection)->atlasDof[q] > 0 ? (*gsection)->atlasDof[q] - cdof : 0;
1490ea844a1aSMatthew Knepley   }
14919566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Scan(&off, &globalOff, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)s)));
1492ea844a1aSMatthew Knepley   globalOff -= off;
1493ea844a1aSMatthew Knepley   for (p = 0, off = 0; p < pEnd - pStart; ++p) {
1494ea844a1aSMatthew Knepley     (*gsection)->atlasOff[p] += globalOff;
1495ea844a1aSMatthew Knepley     if (neg) neg[p + pStart] = -((*gsection)->atlasOff[p] + 1);
1496ea844a1aSMatthew Knepley   }
14979566063dSJacob Faibussowitsch   if (s->perm) PetscCall(ISRestoreIndices(s->perm, &pind));
1498ea844a1aSMatthew Knepley   /* Put in negative offsets for ghost points */
1499ea844a1aSMatthew Knepley   if (nroots >= 0) {
1500ea844a1aSMatthew Knepley     if (nroots == pEnd - pStart) tmpOff = &(*gsection)->atlasOff[-pStart];
15019566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, neg, tmpOff, MPI_REPLACE));
15029566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, neg, tmpOff, MPI_REPLACE));
1503ea844a1aSMatthew Knepley     if (nroots > pEnd - pStart) {
15049371c9d4SSatish Balay       for (p = pStart; p < pEnd; ++p) {
15059371c9d4SSatish Balay         if (tmpOff[p] < 0) (*gsection)->atlasOff[p - pStart] = tmpOff[p];
15069371c9d4SSatish Balay       }
1507ea844a1aSMatthew Knepley     }
1508ea844a1aSMatthew Knepley   }
15099566063dSJacob Faibussowitsch   if (nroots >= 0 && nroots > pEnd - pStart) PetscCall(PetscFree(tmpOff));
15109566063dSJacob Faibussowitsch   PetscCall(PetscFree(neg));
15113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1512ea844a1aSMatthew Knepley }
1513ea844a1aSMatthew Knepley 
1514ea844a1aSMatthew Knepley /*@
151520662ed9SBarry Smith   PetscSectionGetPointLayout - Get the `PetscLayout` associated with a `PetscSection`
1516ea844a1aSMatthew Knepley 
1517cab54364SBarry Smith   Collective
1518ea844a1aSMatthew Knepley 
1519ea844a1aSMatthew Knepley   Input Parameters:
1520cab54364SBarry Smith + comm - The `MPI_Comm`
1521cab54364SBarry Smith - s    - The `PetscSection`
1522ea844a1aSMatthew Knepley 
1523ea844a1aSMatthew Knepley   Output Parameter:
152420662ed9SBarry Smith . layout - The point layout for the data that defines the section
1525ea844a1aSMatthew Knepley 
1526ea844a1aSMatthew Knepley   Level: advanced
1527ea844a1aSMatthew Knepley 
152820662ed9SBarry Smith   Notes:
152920662ed9SBarry Smith   `PetscSectionGetValueLayout()` provides the `layout` for an array of data associated with the `PetscSection`. `PetscSectionGetPointLayout()`
153020662ed9SBarry Smith   provides the `layout` for the data that
153120662ed9SBarry Smith   defines the `PetscSection`
153220662ed9SBarry Smith 
1533cab54364SBarry Smith   This is usually called for the default global section.
1534cab54364SBarry Smith 
1535cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetValueLayout()`, `PetscSectionCreate()`
1536ea844a1aSMatthew Knepley @*/
1537d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetPointLayout(MPI_Comm comm, PetscSection s, PetscLayout *layout)
1538d71ae5a4SJacob Faibussowitsch {
1539ea844a1aSMatthew Knepley   PetscInt pStart, pEnd, p, localSize = 0;
1540ea844a1aSMatthew Knepley 
1541ea844a1aSMatthew Knepley   PetscFunctionBegin;
15429566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
1543ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
1544ea844a1aSMatthew Knepley     PetscInt dof;
1545ea844a1aSMatthew Knepley 
15469566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s, p, &dof));
15479120ba30SVaclav Hapla     if (dof >= 0) ++localSize;
1548ea844a1aSMatthew Knepley   }
15499566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, layout));
15509566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(*layout, localSize));
15519566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(*layout, 1));
15529566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(*layout));
15533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1554ea844a1aSMatthew Knepley }
1555ea844a1aSMatthew Knepley 
1556ea844a1aSMatthew Knepley /*@
155720662ed9SBarry Smith   PetscSectionGetValueLayout - Get the `PetscLayout` associated with the section dofs of a `PetscSection`
1558ea844a1aSMatthew Knepley 
1559cab54364SBarry Smith   Collective
1560ea844a1aSMatthew Knepley 
1561ea844a1aSMatthew Knepley   Input Parameters:
1562cab54364SBarry Smith + comm - The `MPI_Comm`
1563cab54364SBarry Smith - s    - The `PetscSection`
1564ea844a1aSMatthew Knepley 
1565ea844a1aSMatthew Knepley   Output Parameter:
1566ea844a1aSMatthew Knepley . layout - The dof layout for the section
1567ea844a1aSMatthew Knepley 
1568ea844a1aSMatthew Knepley   Level: advanced
1569ea844a1aSMatthew Knepley 
157020662ed9SBarry Smith   Notes:
157120662ed9SBarry Smith   `PetscSectionGetValueLayout()` provides the `layout` for an array of data associated with the `PetscSection`. `PetscSectionGetPointLayout()`
157220662ed9SBarry Smith   provides the `layout` for the data that
157320662ed9SBarry Smith   defines the `PetscSection`
157420662ed9SBarry Smith 
1575cab54364SBarry Smith   This is usually called for the default global section.
1576cab54364SBarry Smith 
1577cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetPointLayout()`, `PetscSectionCreate()`
1578ea844a1aSMatthew Knepley @*/
1579d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetValueLayout(MPI_Comm comm, PetscSection s, PetscLayout *layout)
1580d71ae5a4SJacob Faibussowitsch {
1581ea844a1aSMatthew Knepley   PetscInt pStart, pEnd, p, localSize = 0;
1582ea844a1aSMatthew Knepley 
1583ea844a1aSMatthew Knepley   PetscFunctionBegin;
1584ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 2);
1585ea844a1aSMatthew Knepley   PetscValidPointer(layout, 3);
15869566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
1587ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
1588ea844a1aSMatthew Knepley     PetscInt dof, cdof;
1589ea844a1aSMatthew Knepley 
15909566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s, p, &dof));
15919566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
1592ea844a1aSMatthew Knepley     if (dof - cdof > 0) localSize += dof - cdof;
1593ea844a1aSMatthew Knepley   }
15949566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, layout));
15959566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(*layout, localSize));
15969566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(*layout, 1));
15979566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(*layout));
15983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1599ea844a1aSMatthew Knepley }
1600ea844a1aSMatthew Knepley 
1601ea844a1aSMatthew Knepley /*@
1602db527f05SMatthew G. Knepley   PetscSectionGetOffset - Return the offset into an array or `Vec` for the dof associated with the given point.
1603ea844a1aSMatthew Knepley 
160440750d41SVaclav Hapla   Not Collective
1605ea844a1aSMatthew Knepley 
1606ea844a1aSMatthew Knepley   Input Parameters:
1607cab54364SBarry Smith + s - the `PetscSection`
1608ea844a1aSMatthew Knepley - point - the point
1609ea844a1aSMatthew Knepley 
1610ea844a1aSMatthew Knepley   Output Parameter:
1611ea844a1aSMatthew Knepley . offset - the offset
1612ea844a1aSMatthew Knepley 
1613db527f05SMatthew G. Knepley   Note:
1614db527f05SMatthew G. Knepley   In a global section, this offset will be negative for points not owned by this process.
1615db527f05SMatthew G. Knepley 
1616ea844a1aSMatthew Knepley   Level: intermediate
1617ea844a1aSMatthew Knepley 
1618cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetFieldOffset()`, `PetscSectionCreate()`
1619ea844a1aSMatthew Knepley @*/
1620d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetOffset(PetscSection s, PetscInt point, PetscInt *offset)
1621d71ae5a4SJacob Faibussowitsch {
1622ea844a1aSMatthew Knepley   PetscFunctionBegin;
1623ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1624dadcf809SJacob Faibussowitsch   PetscValidIntPointer(offset, 3);
1625ad540459SPierre Jolivet   if (PetscDefined(USE_DEBUG)) PetscCheck(!(point < s->pStart) && !(point >= s->pEnd), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Section point %" PetscInt_FMT " should be in [%" PetscInt_FMT ", %" PetscInt_FMT ")", point, s->pStart, s->pEnd);
1626ea844a1aSMatthew Knepley   *offset = s->atlasOff[point - s->pStart];
16273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1628ea844a1aSMatthew Knepley }
1629ea844a1aSMatthew Knepley 
1630ea844a1aSMatthew Knepley /*@
1631db527f05SMatthew G. Knepley   PetscSectionSetOffset - Set the offset into an array or `Vec` for the dof associated with the given point.
1632ea844a1aSMatthew Knepley 
163340750d41SVaclav Hapla   Not Collective
1634ea844a1aSMatthew Knepley 
1635ea844a1aSMatthew Knepley   Input Parameters:
1636cab54364SBarry Smith + s - the `PetscSection`
1637ea844a1aSMatthew Knepley . point - the point
1638ea844a1aSMatthew Knepley - offset - the offset
1639ea844a1aSMatthew Knepley 
1640ea844a1aSMatthew Knepley   Level: intermediate
1641ea844a1aSMatthew Knepley 
1642cab54364SBarry Smith   Note:
1643cab54364SBarry Smith   The user usually does not call this function, but uses `PetscSectionSetUp()`
1644cab54364SBarry Smith 
1645cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetFieldOffset()`, `PetscSectionCreate()`, `PetscSectionSetUp()`
1646ea844a1aSMatthew Knepley @*/
1647d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetOffset(PetscSection s, PetscInt point, PetscInt offset)
1648d71ae5a4SJacob Faibussowitsch {
1649ea844a1aSMatthew Knepley   PetscFunctionBegin;
1650ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
165108401ef6SPierre Jolivet   PetscCheck(!(point < s->pStart) && !(point >= s->pEnd), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Section point %" PetscInt_FMT " should be in [%" PetscInt_FMT ", %" PetscInt_FMT ")", point, s->pStart, s->pEnd);
1652ea844a1aSMatthew Knepley   s->atlasOff[point - s->pStart] = offset;
16533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1654ea844a1aSMatthew Knepley }
1655ea844a1aSMatthew Knepley 
1656ea844a1aSMatthew Knepley /*@
1657db527f05SMatthew G. Knepley   PetscSectionGetFieldOffset - Return the offset into an array or `Vec` for the field dof associated with the given point.
1658ea844a1aSMatthew Knepley 
165940750d41SVaclav Hapla   Not Collective
1660ea844a1aSMatthew Knepley 
1661ea844a1aSMatthew Knepley   Input Parameters:
1662cab54364SBarry Smith + s - the `PetscSection`
1663ea844a1aSMatthew Knepley . point - the point
1664ea844a1aSMatthew Knepley - field - the field
1665ea844a1aSMatthew Knepley 
1666ea844a1aSMatthew Knepley   Output Parameter:
1667ea844a1aSMatthew Knepley . offset - the offset
1668ea844a1aSMatthew Knepley 
1669db527f05SMatthew G. Knepley   Note:
1670db527f05SMatthew G. Knepley   In a global section, this offset will be negative for points not owned by this process.
1671db527f05SMatthew G. Knepley 
1672ea844a1aSMatthew Knepley   Level: intermediate
1673ea844a1aSMatthew Knepley 
1674cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetOffset()`, `PetscSectionCreate()`
1675ea844a1aSMatthew Knepley @*/
1676d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldOffset(PetscSection s, PetscInt point, PetscInt field, PetscInt *offset)
1677d71ae5a4SJacob Faibussowitsch {
1678ea844a1aSMatthew Knepley   PetscFunctionBegin;
1679ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
16802abc8c78SJacob Faibussowitsch   PetscValidIntPointer(offset, 4);
16812abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
16829566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(s->field[field], point, offset));
16833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1684ea844a1aSMatthew Knepley }
1685ea844a1aSMatthew Knepley 
1686ea844a1aSMatthew Knepley /*@
1687db527f05SMatthew G. Knepley   PetscSectionSetFieldOffset - Set the offset into an array or `Vec` for the dof associated with the given field at a point.
1688ea844a1aSMatthew Knepley 
168940750d41SVaclav Hapla   Not Collective
1690ea844a1aSMatthew Knepley 
1691ea844a1aSMatthew Knepley   Input Parameters:
169220662ed9SBarry Smith + s - the `PetscSection`
1693ea844a1aSMatthew Knepley . point - the point
1694ea844a1aSMatthew Knepley . field - the field
1695ea844a1aSMatthew Knepley - offset - the offset
1696ea844a1aSMatthew Knepley 
1697cab54364SBarry Smith   Level: developer
1698ea844a1aSMatthew Knepley 
1699cab54364SBarry Smith   Note:
1700cab54364SBarry Smith   The user usually does not call this function, but uses `PetscSectionSetUp()`
1701ea844a1aSMatthew Knepley 
1702cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetFieldOffset()`, `PetscSectionSetOffset()`, `PetscSectionCreate()`, `PetscSectionSetUp()`
1703ea844a1aSMatthew Knepley @*/
1704d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFieldOffset(PetscSection s, PetscInt point, PetscInt field, PetscInt offset)
1705d71ae5a4SJacob Faibussowitsch {
1706ea844a1aSMatthew Knepley   PetscFunctionBegin;
1707ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
17082abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
17099566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetOffset(s->field[field], point, offset));
17103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1711ea844a1aSMatthew Knepley }
1712ea844a1aSMatthew Knepley 
1713ea844a1aSMatthew Knepley /*@
1714ea844a1aSMatthew Knepley   PetscSectionGetFieldPointOffset - Return the offset on the given point for the dof associated with the given point.
1715ea844a1aSMatthew Knepley 
171640750d41SVaclav Hapla   Not Collective
1717ea844a1aSMatthew Knepley 
1718ea844a1aSMatthew Knepley   Input Parameters:
1719cab54364SBarry Smith + s - the `PetscSection`
1720ea844a1aSMatthew Knepley . point - the point
1721ea844a1aSMatthew Knepley - field - the field
1722ea844a1aSMatthew Knepley 
1723ea844a1aSMatthew Knepley   Output Parameter:
1724ea844a1aSMatthew Knepley . offset - the offset
1725ea844a1aSMatthew Knepley 
1726ea844a1aSMatthew Knepley   Level: advanced
1727ea844a1aSMatthew Knepley 
1728cab54364SBarry Smith   Note:
1729cab54364SBarry Smith   This gives the offset on a point of the field, ignoring constraints, meaning starting at the first dof for
1730cab54364SBarry Smith   this point, what number is the first dof with this field.
1731cab54364SBarry Smith 
1732cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetOffset()`, `PetscSectionCreate()`
1733ea844a1aSMatthew Knepley @*/
1734d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldPointOffset(PetscSection s, PetscInt point, PetscInt field, PetscInt *offset)
1735d71ae5a4SJacob Faibussowitsch {
1736ea844a1aSMatthew Knepley   PetscInt off, foff;
1737ea844a1aSMatthew Knepley 
1738ea844a1aSMatthew Knepley   PetscFunctionBegin;
1739ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
17402abc8c78SJacob Faibussowitsch   PetscValidIntPointer(offset, 4);
17412abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
17429566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(s, point, &off));
17439566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(s->field[field], point, &foff));
1744ea844a1aSMatthew Knepley   *offset = foff - off;
17453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1746ea844a1aSMatthew Knepley }
1747ea844a1aSMatthew Knepley 
1748ea844a1aSMatthew Knepley /*@
174920662ed9SBarry Smith   PetscSectionGetOffsetRange - Return the full range of offsets [`start`, `end`) for a `PetscSection`
1750ea844a1aSMatthew Knepley 
175140750d41SVaclav Hapla   Not Collective
1752ea844a1aSMatthew Knepley 
1753ea844a1aSMatthew Knepley   Input Parameter:
1754cab54364SBarry Smith . s - the `PetscSection`
1755ea844a1aSMatthew Knepley 
1756ea844a1aSMatthew Knepley   Output Parameters:
1757ea844a1aSMatthew Knepley + start - the minimum offset
1758ea844a1aSMatthew Knepley - end   - one more than the maximum offset
1759ea844a1aSMatthew Knepley 
1760ea844a1aSMatthew Knepley   Level: intermediate
1761ea844a1aSMatthew Knepley 
1762cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetOffset()`, `PetscSectionCreate()`
1763ea844a1aSMatthew Knepley @*/
1764d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetOffsetRange(PetscSection s, PetscInt *start, PetscInt *end)
1765d71ae5a4SJacob Faibussowitsch {
1766ea844a1aSMatthew Knepley   PetscInt os = 0, oe = 0, pStart, pEnd, p;
1767ea844a1aSMatthew Knepley 
1768ea844a1aSMatthew Knepley   PetscFunctionBegin;
1769ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
17709371c9d4SSatish Balay   if (s->atlasOff) {
17719371c9d4SSatish Balay     os = s->atlasOff[0];
17729371c9d4SSatish Balay     oe = s->atlasOff[0];
17739371c9d4SSatish Balay   }
17749566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
1775ea844a1aSMatthew Knepley   for (p = 0; p < pEnd - pStart; ++p) {
1776ea844a1aSMatthew Knepley     PetscInt dof = s->atlasDof[p], off = s->atlasOff[p];
1777ea844a1aSMatthew Knepley 
1778ea844a1aSMatthew Knepley     if (off >= 0) {
1779ea844a1aSMatthew Knepley       os = PetscMin(os, off);
1780ea844a1aSMatthew Knepley       oe = PetscMax(oe, off + dof);
1781ea844a1aSMatthew Knepley     }
1782ea844a1aSMatthew Knepley   }
1783ea844a1aSMatthew Knepley   if (start) *start = os;
1784ea844a1aSMatthew Knepley   if (end) *end = oe;
17853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1786ea844a1aSMatthew Knepley }
1787ea844a1aSMatthew Knepley 
1788ea844a1aSMatthew Knepley /*@
178920662ed9SBarry Smith   PetscSectionCreateSubsection - Create a new, smaller `PetscSection` composed of only the selected fields
1790ea844a1aSMatthew Knepley 
179140750d41SVaclav Hapla   Collective
1792ea844a1aSMatthew Knepley 
1793d8d19677SJose E. Roman   Input Parameters:
1794cab54364SBarry Smith + s      - the `PetscSection`
1795ea844a1aSMatthew Knepley . len    - the number of subfields
1796ea844a1aSMatthew Knepley - fields - the subfield numbers
1797ea844a1aSMatthew Knepley 
1798ea844a1aSMatthew Knepley   Output Parameter:
1799ea844a1aSMatthew Knepley . subs   - the subsection
1800ea844a1aSMatthew Knepley 
1801ea844a1aSMatthew Knepley   Level: advanced
1802ea844a1aSMatthew Knepley 
1803cab54364SBarry Smith   Notes:
1804cab54364SBarry Smith   The section offsets now refer to a new, smaller vector.
1805cab54364SBarry Smith 
1806cab54364SBarry Smith   This will error if a fieldnumber is out of range
1807cab54364SBarry Smith 
1808cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreateSupersection()`, `PetscSectionCreate()`
1809ea844a1aSMatthew Knepley @*/
1810d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCreateSubsection(PetscSection s, PetscInt len, const PetscInt fields[], PetscSection *subs)
1811d71ae5a4SJacob Faibussowitsch {
1812b778fa18SValeria Barra   PetscInt nF, f, c, pStart, pEnd, p, maxCdof = 0;
1813ea844a1aSMatthew Knepley 
1814ea844a1aSMatthew Knepley   PetscFunctionBegin;
18153ba16761SJacob Faibussowitsch   if (!len) PetscFunctionReturn(PETSC_SUCCESS);
1816ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1817dadcf809SJacob Faibussowitsch   PetscValidIntPointer(fields, 3);
1818ea844a1aSMatthew Knepley   PetscValidPointer(subs, 4);
18199566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &nF));
182008401ef6SPierre Jolivet   PetscCheck(len <= nF, PetscObjectComm((PetscObject)s), PETSC_ERR_ARG_WRONG, "Number of requested fields %" PetscInt_FMT " greater than number of fields %" PetscInt_FMT, len, nF);
18219566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s), subs));
18229566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(*subs, len));
1823ea844a1aSMatthew Knepley   for (f = 0; f < len; ++f) {
1824ea844a1aSMatthew Knepley     const char *name    = NULL;
1825ea844a1aSMatthew Knepley     PetscInt    numComp = 0;
1826ea844a1aSMatthew Knepley 
18279566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldName(s, fields[f], &name));
18289566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldName(*subs, f, name));
18299566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, fields[f], &numComp));
18309566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(*subs, f, numComp));
1831b778fa18SValeria Barra     for (c = 0; c < s->numFieldComponents[fields[f]]; ++c) {
18329566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetComponentName(s, fields[f], c, &name));
18339566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetComponentName(*subs, f, c, name));
1834b778fa18SValeria Barra     }
1835ea844a1aSMatthew Knepley   }
18369566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
18379566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(*subs, pStart, pEnd));
1838ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
1839ea844a1aSMatthew Knepley     PetscInt dof = 0, cdof = 0, fdof = 0, cfdof = 0;
1840ea844a1aSMatthew Knepley 
1841ea844a1aSMatthew Knepley     for (f = 0; f < len; ++f) {
18429566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, fields[f], &fdof));
18439566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(*subs, p, f, fdof));
18449566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(s, p, fields[f], &cfdof));
18459566063dSJacob Faibussowitsch       if (cfdof) PetscCall(PetscSectionSetFieldConstraintDof(*subs, p, f, cfdof));
1846ea844a1aSMatthew Knepley       dof += fdof;
1847ea844a1aSMatthew Knepley       cdof += cfdof;
1848ea844a1aSMatthew Knepley     }
18499566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(*subs, p, dof));
18509566063dSJacob Faibussowitsch     if (cdof) PetscCall(PetscSectionSetConstraintDof(*subs, p, cdof));
1851ea844a1aSMatthew Knepley     maxCdof = PetscMax(cdof, maxCdof);
1852ea844a1aSMatthew Knepley   }
18539566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(*subs));
1854ea844a1aSMatthew Knepley   if (maxCdof) {
1855ea844a1aSMatthew Knepley     PetscInt *indices;
1856ea844a1aSMatthew Knepley 
18579566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(maxCdof, &indices));
1858ea844a1aSMatthew Knepley     for (p = pStart; p < pEnd; ++p) {
1859ea844a1aSMatthew Knepley       PetscInt cdof;
1860ea844a1aSMatthew Knepley 
18619566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetConstraintDof(*subs, p, &cdof));
1862ea844a1aSMatthew Knepley       if (cdof) {
1863ea844a1aSMatthew Knepley         const PetscInt *oldIndices = NULL;
1864ea844a1aSMatthew Knepley         PetscInt        fdof = 0, cfdof = 0, fc, numConst = 0, fOff = 0;
1865ea844a1aSMatthew Knepley 
1866ea844a1aSMatthew Knepley         for (f = 0; f < len; ++f) {
18679566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(s, p, fields[f], &fdof));
18689566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(s, p, fields[f], &cfdof));
18699566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintIndices(s, p, fields[f], &oldIndices));
18709566063dSJacob Faibussowitsch           PetscCall(PetscSectionSetFieldConstraintIndices(*subs, p, f, oldIndices));
18719574d0f0SMatthew G. Knepley           for (fc = 0; fc < cfdof; ++fc) indices[numConst + fc] = oldIndices[fc] + fOff;
1872ea844a1aSMatthew Knepley           numConst += cfdof;
1873ea844a1aSMatthew Knepley           fOff += fdof;
1874ea844a1aSMatthew Knepley         }
18759566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetConstraintIndices(*subs, p, indices));
1876ea844a1aSMatthew Knepley       }
1877ea844a1aSMatthew Knepley     }
18789566063dSJacob Faibussowitsch     PetscCall(PetscFree(indices));
1879ea844a1aSMatthew Knepley   }
18803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1881ea844a1aSMatthew Knepley }
1882ea844a1aSMatthew Knepley 
1883ea844a1aSMatthew Knepley /*@
188420662ed9SBarry Smith   PetscSectionCreateSupersection - Create a new, larger section composed of multiple input `PetscSection`s
1885ea844a1aSMatthew Knepley 
188640750d41SVaclav Hapla   Collective
1887ea844a1aSMatthew Knepley 
1888ea844a1aSMatthew Knepley   Input Parameters:
1889ea844a1aSMatthew Knepley + s     - the input sections
1890ea844a1aSMatthew Knepley - len   - the number of input sections
1891ea844a1aSMatthew Knepley 
1892ea844a1aSMatthew Knepley   Output Parameter:
1893ea844a1aSMatthew Knepley . supers - the supersection
1894ea844a1aSMatthew Knepley 
1895ea844a1aSMatthew Knepley   Level: advanced
1896ea844a1aSMatthew Knepley 
1897cab54364SBarry Smith   Note:
1898cab54364SBarry Smith   The section offsets now refer to a new, larger vector.
1899cab54364SBarry Smith 
1900cab54364SBarry Smith   Developer Note:
1901cab54364SBarry Smith   Needs to explain how the sections are composed
1902cab54364SBarry Smith 
1903cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreateSubsection()`, `PetscSectionCreate()`
1904ea844a1aSMatthew Knepley @*/
1905d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCreateSupersection(PetscSection s[], PetscInt len, PetscSection *supers)
1906d71ae5a4SJacob Faibussowitsch {
1907ea844a1aSMatthew Knepley   PetscInt Nf = 0, f, pStart = PETSC_MAX_INT, pEnd = 0, p, maxCdof = 0, i;
1908ea844a1aSMatthew Knepley 
1909ea844a1aSMatthew Knepley   PetscFunctionBegin;
19103ba16761SJacob Faibussowitsch   if (!len) PetscFunctionReturn(PETSC_SUCCESS);
1911ea844a1aSMatthew Knepley   for (i = 0; i < len; ++i) {
1912ea844a1aSMatthew Knepley     PetscInt nf, pStarti, pEndi;
1913ea844a1aSMatthew Knepley 
19149566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(s[i], &nf));
19159566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(s[i], &pStarti, &pEndi));
1916ea844a1aSMatthew Knepley     pStart = PetscMin(pStart, pStarti);
1917ea844a1aSMatthew Knepley     pEnd   = PetscMax(pEnd, pEndi);
1918ea844a1aSMatthew Knepley     Nf += nf;
1919ea844a1aSMatthew Knepley   }
19209566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s[0]), supers));
19219566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(*supers, Nf));
1922ea844a1aSMatthew Knepley   for (i = 0, f = 0; i < len; ++i) {
1923b778fa18SValeria Barra     PetscInt nf, fi, ci;
1924ea844a1aSMatthew Knepley 
19259566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(s[i], &nf));
1926ea844a1aSMatthew Knepley     for (fi = 0; fi < nf; ++fi, ++f) {
1927ea844a1aSMatthew Knepley       const char *name    = NULL;
1928ea844a1aSMatthew Knepley       PetscInt    numComp = 0;
1929ea844a1aSMatthew Knepley 
19309566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(s[i], fi, &name));
19319566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldName(*supers, f, name));
19329566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(s[i], fi, &numComp));
19339566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldComponents(*supers, f, numComp));
1934b778fa18SValeria Barra       for (ci = 0; ci < s[i]->numFieldComponents[fi]; ++ci) {
19359566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetComponentName(s[i], fi, ci, &name));
19369566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetComponentName(*supers, f, ci, name));
1937b778fa18SValeria Barra       }
1938ea844a1aSMatthew Knepley     }
1939ea844a1aSMatthew Knepley   }
19409566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(*supers, pStart, pEnd));
1941ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
1942ea844a1aSMatthew Knepley     PetscInt dof = 0, cdof = 0;
1943ea844a1aSMatthew Knepley 
1944ea844a1aSMatthew Knepley     for (i = 0, f = 0; i < len; ++i) {
1945ea844a1aSMatthew Knepley       PetscInt nf, fi, pStarti, pEndi;
1946ea844a1aSMatthew Knepley       PetscInt fdof = 0, cfdof = 0;
1947ea844a1aSMatthew Knepley 
19489566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetNumFields(s[i], &nf));
19499566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s[i], &pStarti, &pEndi));
1950ea844a1aSMatthew Knepley       if ((p < pStarti) || (p >= pEndi)) continue;
1951ea844a1aSMatthew Knepley       for (fi = 0; fi < nf; ++fi, ++f) {
19529566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldDof(s[i], p, fi, &fdof));
19539566063dSJacob Faibussowitsch         PetscCall(PetscSectionAddFieldDof(*supers, p, f, fdof));
19549566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldConstraintDof(s[i], p, fi, &cfdof));
19559566063dSJacob Faibussowitsch         if (cfdof) PetscCall(PetscSectionAddFieldConstraintDof(*supers, p, f, cfdof));
1956ea844a1aSMatthew Knepley         dof += fdof;
1957ea844a1aSMatthew Knepley         cdof += cfdof;
1958ea844a1aSMatthew Knepley       }
1959ea844a1aSMatthew Knepley     }
19609566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(*supers, p, dof));
19619566063dSJacob Faibussowitsch     if (cdof) PetscCall(PetscSectionSetConstraintDof(*supers, p, cdof));
1962ea844a1aSMatthew Knepley     maxCdof = PetscMax(cdof, maxCdof);
1963ea844a1aSMatthew Knepley   }
19649566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(*supers));
1965ea844a1aSMatthew Knepley   if (maxCdof) {
1966ea844a1aSMatthew Knepley     PetscInt *indices;
1967ea844a1aSMatthew Knepley 
19689566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(maxCdof, &indices));
1969ea844a1aSMatthew Knepley     for (p = pStart; p < pEnd; ++p) {
1970ea844a1aSMatthew Knepley       PetscInt cdof;
1971ea844a1aSMatthew Knepley 
19729566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetConstraintDof(*supers, p, &cdof));
1973ea844a1aSMatthew Knepley       if (cdof) {
1974ea844a1aSMatthew Knepley         PetscInt dof, numConst = 0, fOff = 0;
1975ea844a1aSMatthew Knepley 
1976ea844a1aSMatthew Knepley         for (i = 0, f = 0; i < len; ++i) {
1977ea844a1aSMatthew Knepley           const PetscInt *oldIndices = NULL;
1978ea844a1aSMatthew Knepley           PetscInt        nf, fi, pStarti, pEndi, fdof, cfdof, fc;
1979ea844a1aSMatthew Knepley 
19809566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetNumFields(s[i], &nf));
19819566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetChart(s[i], &pStarti, &pEndi));
1982ea844a1aSMatthew Knepley           if ((p < pStarti) || (p >= pEndi)) continue;
1983ea844a1aSMatthew Knepley           for (fi = 0; fi < nf; ++fi, ++f) {
19849566063dSJacob Faibussowitsch             PetscCall(PetscSectionGetFieldDof(s[i], p, fi, &fdof));
19859566063dSJacob Faibussowitsch             PetscCall(PetscSectionGetFieldConstraintDof(s[i], p, fi, &cfdof));
19869566063dSJacob Faibussowitsch             PetscCall(PetscSectionGetFieldConstraintIndices(s[i], p, fi, &oldIndices));
1987fcd2503dSMatthew G. Knepley             for (fc = 0; fc < cfdof; ++fc) indices[numConst + fc] = oldIndices[fc];
19889566063dSJacob Faibussowitsch             PetscCall(PetscSectionSetFieldConstraintIndices(*supers, p, f, &indices[numConst]));
1989fcd2503dSMatthew G. Knepley             for (fc = 0; fc < cfdof; ++fc) indices[numConst + fc] += fOff;
1990ea844a1aSMatthew Knepley             numConst += cfdof;
1991ea844a1aSMatthew Knepley           }
19929566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetDof(s[i], p, &dof));
1993ea844a1aSMatthew Knepley           fOff += dof;
1994ea844a1aSMatthew Knepley         }
19959566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetConstraintIndices(*supers, p, indices));
1996ea844a1aSMatthew Knepley       }
1997ea844a1aSMatthew Knepley     }
19989566063dSJacob Faibussowitsch     PetscCall(PetscFree(indices));
1999ea844a1aSMatthew Knepley   }
20003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2001ea844a1aSMatthew Knepley }
2002ea844a1aSMatthew Knepley 
2003d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCreateSubplexSection_Internal(PetscSection s, IS subpointMap, PetscBool renumberPoints, PetscSection *subs)
2004d71ae5a4SJacob Faibussowitsch {
2005ea844a1aSMatthew Knepley   const PetscInt *points = NULL, *indices = NULL;
200641f23ed0SMatthew G. Knepley   PetscInt        numFields, f, c, numSubpoints = 0, pStart, pEnd, p, spStart, spEnd, subp;
2007ea844a1aSMatthew Knepley 
2008ea844a1aSMatthew Knepley   PetscFunctionBegin;
2009ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2010ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(subpointMap, IS_CLASSID, 2);
201141f23ed0SMatthew G. Knepley   PetscValidPointer(subs, 4);
20129566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &numFields));
20139566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s), subs));
20149566063dSJacob Faibussowitsch   if (numFields) PetscCall(PetscSectionSetNumFields(*subs, numFields));
2015ea844a1aSMatthew Knepley   for (f = 0; f < numFields; ++f) {
2016ea844a1aSMatthew Knepley     const char *name    = NULL;
2017ea844a1aSMatthew Knepley     PetscInt    numComp = 0;
2018ea844a1aSMatthew Knepley 
20199566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldName(s, f, &name));
20209566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldName(*subs, f, name));
20219566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &numComp));
20229566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(*subs, f, numComp));
2023b778fa18SValeria Barra     for (c = 0; c < s->numFieldComponents[f]; ++c) {
20249566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetComponentName(s, f, c, &name));
20259566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetComponentName(*subs, f, c, name));
2026b778fa18SValeria Barra     }
2027ea844a1aSMatthew Knepley   }
2028ea844a1aSMatthew Knepley   /* For right now, we do not try to squeeze the subchart */
2029ea844a1aSMatthew Knepley   if (subpointMap) {
20309566063dSJacob Faibussowitsch     PetscCall(ISGetSize(subpointMap, &numSubpoints));
20319566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subpointMap, &points));
2032ea844a1aSMatthew Knepley   }
20339566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
203441f23ed0SMatthew G. Knepley   if (renumberPoints) {
203541f23ed0SMatthew G. Knepley     spStart = 0;
203641f23ed0SMatthew G. Knepley     spEnd   = numSubpoints;
203741f23ed0SMatthew G. Knepley   } else {
203841f23ed0SMatthew G. Knepley     PetscCall(ISGetMinMax(subpointMap, &spStart, &spEnd));
203941f23ed0SMatthew G. Knepley     ++spEnd;
204041f23ed0SMatthew G. Knepley   }
204141f23ed0SMatthew G. Knepley   PetscCall(PetscSectionSetChart(*subs, spStart, spEnd));
2042ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
2043ea844a1aSMatthew Knepley     PetscInt dof, cdof, fdof = 0, cfdof = 0;
2044ea844a1aSMatthew Knepley 
20459566063dSJacob Faibussowitsch     PetscCall(PetscFindInt(p, numSubpoints, points, &subp));
2046ea844a1aSMatthew Knepley     if (subp < 0) continue;
204741f23ed0SMatthew G. Knepley     if (!renumberPoints) subp = p;
2048ea844a1aSMatthew Knepley     for (f = 0; f < numFields; ++f) {
20499566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &fdof));
20509566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(*subs, subp, f, fdof));
20519566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(s, p, f, &cfdof));
20529566063dSJacob Faibussowitsch       if (cfdof) PetscCall(PetscSectionSetFieldConstraintDof(*subs, subp, f, cfdof));
2053ea844a1aSMatthew Knepley     }
20549566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s, p, &dof));
20559566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(*subs, subp, dof));
20569566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
20579566063dSJacob Faibussowitsch     if (cdof) PetscCall(PetscSectionSetConstraintDof(*subs, subp, cdof));
2058ea844a1aSMatthew Knepley   }
20599566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(*subs));
2060ea844a1aSMatthew Knepley   /* Change offsets to original offsets */
2061ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
2062ea844a1aSMatthew Knepley     PetscInt off, foff = 0;
2063ea844a1aSMatthew Knepley 
20649566063dSJacob Faibussowitsch     PetscCall(PetscFindInt(p, numSubpoints, points, &subp));
2065ea844a1aSMatthew Knepley     if (subp < 0) continue;
206641f23ed0SMatthew G. Knepley     if (!renumberPoints) subp = p;
2067ea844a1aSMatthew Knepley     for (f = 0; f < numFields; ++f) {
20689566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldOffset(s, p, f, &foff));
20699566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldOffset(*subs, subp, f, foff));
2070ea844a1aSMatthew Knepley     }
20719566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(s, p, &off));
20729566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetOffset(*subs, subp, off));
2073ea844a1aSMatthew Knepley   }
2074ea844a1aSMatthew Knepley   /* Copy constraint indices */
207541f23ed0SMatthew G. Knepley   for (subp = spStart; subp < spEnd; ++subp) {
2076ea844a1aSMatthew Knepley     PetscInt cdof;
2077ea844a1aSMatthew Knepley 
20789566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(*subs, subp, &cdof));
2079ea844a1aSMatthew Knepley     if (cdof) {
2080ea844a1aSMatthew Knepley       for (f = 0; f < numFields; ++f) {
208141f23ed0SMatthew G. Knepley         PetscCall(PetscSectionGetFieldConstraintIndices(s, points[subp - spStart], f, &indices));
20829566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetFieldConstraintIndices(*subs, subp, f, indices));
2083ea844a1aSMatthew Knepley       }
208441f23ed0SMatthew G. Knepley       PetscCall(PetscSectionGetConstraintIndices(s, points[subp - spStart], &indices));
20859566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetConstraintIndices(*subs, subp, indices));
2086ea844a1aSMatthew Knepley     }
2087ea844a1aSMatthew Knepley   }
20889566063dSJacob Faibussowitsch   if (subpointMap) PetscCall(ISRestoreIndices(subpointMap, &points));
20893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2090ea844a1aSMatthew Knepley }
2091ea844a1aSMatthew Knepley 
209241f23ed0SMatthew G. Knepley /*@
209341f23ed0SMatthew G. Knepley   PetscSectionCreateSubmeshSection - Create a new, smaller section with support on the submesh
209441f23ed0SMatthew G. Knepley 
2095c3339decSBarry Smith   Collective
209641f23ed0SMatthew G. Knepley 
209741f23ed0SMatthew G. Knepley   Input Parameters:
2098cab54364SBarry Smith + s           - the `PetscSection`
209941f23ed0SMatthew G. Knepley - subpointMap - a sorted list of points in the original mesh which are in the submesh
210041f23ed0SMatthew G. Knepley 
210141f23ed0SMatthew G. Knepley   Output Parameter:
210241f23ed0SMatthew G. Knepley . subs - the subsection
210341f23ed0SMatthew G. Knepley 
2104cab54364SBarry Smith   Level: advanced
2105cab54364SBarry Smith 
210641f23ed0SMatthew G. Knepley   Note:
210741f23ed0SMatthew G. Knepley   The points are renumbered from 0, and the section offsets now refer to a new, smaller vector.
210820662ed9SBarry Smith   Compare this with `PetscSectionCreateSubdomainSection()` that does not map the points numbers to start at zero but leaves them as before
210941f23ed0SMatthew G. Knepley 
2110cab54364SBarry Smith   Developer Note:
211120662ed9SBarry Smith   The use of the term Submesh is confusing and needs clarification, it is not specific to meshes. It appears to be just a subset of the chart of the original `PetscSection`
211241f23ed0SMatthew G. Knepley 
2113cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreateSubdomainSection()`, `PetscSectionCreateSubsection()`, `DMPlexGetSubpointMap()`, `PetscSectionCreate()`
211441f23ed0SMatthew G. Knepley @*/
2115d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCreateSubmeshSection(PetscSection s, IS subpointMap, PetscSection *subs)
2116d71ae5a4SJacob Faibussowitsch {
211741f23ed0SMatthew G. Knepley   PetscFunctionBegin;
211841f23ed0SMatthew G. Knepley   PetscCall(PetscSectionCreateSubplexSection_Internal(s, subpointMap, PETSC_TRUE, subs));
21193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
212041f23ed0SMatthew G. Knepley }
212141f23ed0SMatthew G. Knepley 
212241f23ed0SMatthew G. Knepley /*@
212341f23ed0SMatthew G. Knepley   PetscSectionCreateSubdomainSection - Create a new, smaller section with support on a subdomain of the mesh
212441f23ed0SMatthew G. Knepley 
2125c3339decSBarry Smith   Collective
212641f23ed0SMatthew G. Knepley 
212741f23ed0SMatthew G. Knepley   Input Parameters:
2128cab54364SBarry Smith + s           - the `PetscSection`
212941f23ed0SMatthew G. Knepley - subpointMap - a sorted list of points in the original mesh which are in the subdomain
213041f23ed0SMatthew G. Knepley 
213141f23ed0SMatthew G. Knepley   Output Parameter:
213241f23ed0SMatthew G. Knepley . subs - the subsection
213341f23ed0SMatthew G. Knepley 
2134cab54364SBarry Smith   Level: advanced
2135cab54364SBarry Smith 
213641f23ed0SMatthew G. Knepley   Note:
213720662ed9SBarry Smith   The point numbers remain the same as in the larger `PetscSection`, but the section offsets now refer to a new, smaller vector.
213820662ed9SBarry Smith   Compare this with `PetscSectionCreateSubmeshSection()` that maps the point numbers to start at zero
213941f23ed0SMatthew G. Knepley 
2140cab54364SBarry Smith   Developer Notes:
214120662ed9SBarry Smith   The use of the term Subdomain is unneeded and needs clarification, it is not specific to meshes. It appears to be just a subset of the chart of the original `PetscSection`
2142cab54364SBarry Smith 
2143cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreateSubmeshSection()`, `PetscSectionCreateSubsection()`, `DMPlexGetSubpointMap()`, `PetscSectionCreate()`
214441f23ed0SMatthew G. Knepley @*/
2145d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCreateSubdomainSection(PetscSection s, IS subpointMap, PetscSection *subs)
2146d71ae5a4SJacob Faibussowitsch {
214741f23ed0SMatthew G. Knepley   PetscFunctionBegin;
214841f23ed0SMatthew G. Knepley   PetscCall(PetscSectionCreateSubplexSection_Internal(s, subpointMap, PETSC_FALSE, subs));
21493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
215041f23ed0SMatthew G. Knepley }
215141f23ed0SMatthew G. Knepley 
2152d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscSectionView_ASCII(PetscSection s, PetscViewer viewer)
2153d71ae5a4SJacob Faibussowitsch {
2154ea844a1aSMatthew Knepley   PetscInt    p;
2155ea844a1aSMatthew Knepley   PetscMPIInt rank;
2156ea844a1aSMatthew Knepley 
2157ea844a1aSMatthew Knepley   PetscFunctionBegin;
21589566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank));
21599566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPushSynchronized(viewer));
21609566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "Process %d:\n", rank));
2161ea844a1aSMatthew Knepley   for (p = 0; p < s->pEnd - s->pStart; ++p) {
2162ea844a1aSMatthew Knepley     if ((s->bc) && (s->bc->atlasDof[p] > 0)) {
2163ea844a1aSMatthew Knepley       PetscInt b;
2164ea844a1aSMatthew Knepley 
21659566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "  (%4" PetscInt_FMT ") dim %2" PetscInt_FMT " offset %3" PetscInt_FMT " constrained", p + s->pStart, s->atlasDof[p], s->atlasOff[p]));
2166083401c6SMatthew G. Knepley       if (s->bcIndices) {
216748a46eb9SPierre Jolivet         for (b = 0; b < s->bc->atlasDof[p]; ++b) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " %" PetscInt_FMT, s->bcIndices[s->bc->atlasOff[p] + b]));
2168083401c6SMatthew G. Knepley       }
21699566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n"));
2170ea844a1aSMatthew Knepley     } else {
21719566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "  (%4" PetscInt_FMT ") dim %2" PetscInt_FMT " offset %3" PetscInt_FMT "\n", p + s->pStart, s->atlasDof[p], s->atlasOff[p]));
2172ea844a1aSMatthew Knepley     }
2173ea844a1aSMatthew Knepley   }
21749566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(viewer));
21759566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPopSynchronized(viewer));
2176ea844a1aSMatthew Knepley   if (s->sym) {
21779566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPushTab(viewer));
21789566063dSJacob Faibussowitsch     PetscCall(PetscSectionSymView(s->sym, viewer));
21799566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPopTab(viewer));
2180ea844a1aSMatthew Knepley   }
21813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2182ea844a1aSMatthew Knepley }
2183ea844a1aSMatthew Knepley 
2184ea844a1aSMatthew Knepley /*@C
2185cab54364SBarry Smith    PetscSectionViewFromOptions - View the `PetscSection` based on values in the options database
2186fe2efc57SMark 
218740750d41SVaclav Hapla    Collective
2188fe2efc57SMark 
2189fe2efc57SMark    Input Parameters:
219020662ed9SBarry Smith +  A - the `PetscSection` object to view
2191cab54364SBarry Smith .  obj - Optional object that provides the options prefix used for the options
2192736c3998SJose E. Roman -  name - command line option
2193fe2efc57SMark 
2194fe2efc57SMark    Level: intermediate
2195cab54364SBarry Smith 
219620662ed9SBarry Smith    Note:
219720662ed9SBarry Smith    See `PetscObjectViewFromOptions()` for available values of `PetscViewer` and `PetscViewerFormat`
219820662ed9SBarry Smith 
2199cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionView`, `PetscObjectViewFromOptions()`, `PetscSectionCreate()`, `PetscSectionView()`
2200fe2efc57SMark @*/
2201d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionViewFromOptions(PetscSection A, PetscObject obj, const char name[])
2202d71ae5a4SJacob Faibussowitsch {
2203fe2efc57SMark   PetscFunctionBegin;
2204fe2efc57SMark   PetscValidHeaderSpecific(A, PETSC_SECTION_CLASSID, 1);
22059566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
22063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2207fe2efc57SMark }
2208fe2efc57SMark 
2209fe2efc57SMark /*@C
2210cab54364SBarry Smith   PetscSectionView - Views a `PetscSection`
2211ea844a1aSMatthew Knepley 
221240750d41SVaclav Hapla   Collective
2213ea844a1aSMatthew Knepley 
2214ea844a1aSMatthew Knepley   Input Parameters:
2215cab54364SBarry Smith + s - the `PetscSection` object to view
2216ea844a1aSMatthew Knepley - v - the viewer
2217ea844a1aSMatthew Knepley 
2218cab54364SBarry Smith   Level: beginner
2219cab54364SBarry Smith 
22201ddd528eSksagiyam   Note:
2221cab54364SBarry Smith   `PetscSectionView()`, when viewer is of type `PETSCVIEWERHDF5`, only saves
22221ddd528eSksagiyam   distribution independent data, such as dofs, offsets, constraint dofs,
22231ddd528eSksagiyam   and constraint indices. Points that have negative dofs, for instance,
22241ddd528eSksagiyam   are not saved as they represent points owned by other processes.
22251ddd528eSksagiyam   Point numbering and rank assignment is currently not stored.
2226cab54364SBarry Smith   The saved section can be loaded with `PetscSectionLoad()`.
22271ddd528eSksagiyam 
2228cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionDestroy()`, `PetscSectionLoad()`, `PetscViewer`
2229ea844a1aSMatthew Knepley @*/
2230d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionView(PetscSection s, PetscViewer viewer)
2231d71ae5a4SJacob Faibussowitsch {
22321ddd528eSksagiyam   PetscBool isascii, ishdf5;
2233ea844a1aSMatthew Knepley   PetscInt  f;
2234ea844a1aSMatthew Knepley 
2235ea844a1aSMatthew Knepley   PetscFunctionBegin;
2236ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
22379566063dSJacob Faibussowitsch   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)s), &viewer));
2238ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
22399566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
22409566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2241ea844a1aSMatthew Knepley   if (isascii) {
22429566063dSJacob Faibussowitsch     PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)s, viewer));
2243ea844a1aSMatthew Knepley     if (s->numFields) {
22449566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " fields\n", s->numFields));
2245ea844a1aSMatthew Knepley       for (f = 0; f < s->numFields; ++f) {
22469566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, "  field %" PetscInt_FMT " with %" PetscInt_FMT " components\n", f, s->numFieldComponents[f]));
22479566063dSJacob Faibussowitsch         PetscCall(PetscSectionView_ASCII(s->field[f], viewer));
2248ea844a1aSMatthew Knepley       }
2249ea844a1aSMatthew Knepley     } else {
22509566063dSJacob Faibussowitsch       PetscCall(PetscSectionView_ASCII(s, viewer));
2251ea844a1aSMatthew Knepley     }
22521ddd528eSksagiyam   } else if (ishdf5) {
22531ddd528eSksagiyam #if PetscDefined(HAVE_HDF5)
22549566063dSJacob Faibussowitsch     PetscCall(PetscSectionView_HDF5_Internal(s, viewer));
22551ddd528eSksagiyam #else
22561ddd528eSksagiyam     SETERRQ(PetscObjectComm((PetscObject)s), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
22571ddd528eSksagiyam #endif
2258ea844a1aSMatthew Knepley   }
22593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2260ea844a1aSMatthew Knepley }
2261ea844a1aSMatthew Knepley 
2262fde5e3acSksagiyam /*@C
2263cab54364SBarry Smith   PetscSectionLoad - Loads a `PetscSection`
2264fde5e3acSksagiyam 
226540750d41SVaclav Hapla   Collective
2266fde5e3acSksagiyam 
2267fde5e3acSksagiyam   Input Parameters:
2268cab54364SBarry Smith + s - the `PetscSection` object to load
2269fde5e3acSksagiyam - v - the viewer
2270fde5e3acSksagiyam 
2271cab54364SBarry Smith   Level: beginner
2272cab54364SBarry Smith 
2273fde5e3acSksagiyam   Note:
2274cab54364SBarry Smith   `PetscSectionLoad()`, when viewer is of type `PETSCVIEWERHDF5`, loads
2275cab54364SBarry Smith   a section saved with `PetscSectionView()`. The number of processes
2276fde5e3acSksagiyam   used here (N) does not need to be the same as that used when saving.
2277fde5e3acSksagiyam   After calling this function, the chart of s on rank i will be set
2278fde5e3acSksagiyam   to [0, E_i), where \sum_{i=0}^{N-1}E_i equals to the total number of
2279fde5e3acSksagiyam   saved section points.
2280fde5e3acSksagiyam 
2281cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionDestroy()`, `PetscSectionView()`
2282fde5e3acSksagiyam @*/
2283d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionLoad(PetscSection s, PetscViewer viewer)
2284d71ae5a4SJacob Faibussowitsch {
2285fde5e3acSksagiyam   PetscBool ishdf5;
2286fde5e3acSksagiyam 
2287fde5e3acSksagiyam   PetscFunctionBegin;
2288fde5e3acSksagiyam   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2289fde5e3acSksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
22909566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2291fde5e3acSksagiyam   if (ishdf5) {
2292fde5e3acSksagiyam #if PetscDefined(HAVE_HDF5)
22939566063dSJacob Faibussowitsch     PetscCall(PetscSectionLoad_HDF5_Internal(s, viewer));
22943ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2295fde5e3acSksagiyam #else
2296fde5e3acSksagiyam     SETERRQ(PetscObjectComm((PetscObject)s), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2297fde5e3acSksagiyam #endif
229898921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)s), PETSC_ERR_SUP, "Viewer type %s not yet supported for PetscSection loading", ((PetscObject)viewer)->type_name);
2299fde5e3acSksagiyam }
2300fde5e3acSksagiyam 
2301d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscSectionResetClosurePermutation(PetscSection section)
2302d71ae5a4SJacob Faibussowitsch {
2303c459fbc1SJed Brown   PetscSectionClosurePermVal clVal;
2304c459fbc1SJed Brown 
2305c459fbc1SJed Brown   PetscFunctionBegin;
23063ba16761SJacob Faibussowitsch   if (!section->clHash) PetscFunctionReturn(PETSC_SUCCESS);
2307c459fbc1SJed Brown   kh_foreach_value(section->clHash, clVal, {
23089566063dSJacob Faibussowitsch     PetscCall(PetscFree(clVal.perm));
23099566063dSJacob Faibussowitsch     PetscCall(PetscFree(clVal.invPerm));
2310c459fbc1SJed Brown   });
2311c459fbc1SJed Brown   kh_destroy(ClPerm, section->clHash);
2312c459fbc1SJed Brown   section->clHash = NULL;
23133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2314c459fbc1SJed Brown }
2315c459fbc1SJed Brown 
2316ea844a1aSMatthew Knepley /*@
2317ea844a1aSMatthew Knepley   PetscSectionReset - Frees all section data.
2318ea844a1aSMatthew Knepley 
231940750d41SVaclav Hapla   Not Collective
2320ea844a1aSMatthew Knepley 
2321*2fe279fdSBarry Smith   Input Parameter:
2322cab54364SBarry Smith . s - the `PetscSection`
2323ea844a1aSMatthew Knepley 
2324ea844a1aSMatthew Knepley   Level: beginner
2325ea844a1aSMatthew Knepley 
2326cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreate()`
2327ea844a1aSMatthew Knepley @*/
2328d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionReset(PetscSection s)
2329d71ae5a4SJacob Faibussowitsch {
2330b778fa18SValeria Barra   PetscInt f, c;
2331ea844a1aSMatthew Knepley 
2332ea844a1aSMatthew Knepley   PetscFunctionBegin;
2333ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2334ea844a1aSMatthew Knepley   for (f = 0; f < s->numFields; ++f) {
23359566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&s->field[f]));
23369566063dSJacob Faibussowitsch     PetscCall(PetscFree(s->fieldNames[f]));
233748a46eb9SPierre Jolivet     for (c = 0; c < s->numFieldComponents[f]; ++c) PetscCall(PetscFree(s->compNames[f][c]));
23389566063dSJacob Faibussowitsch     PetscCall(PetscFree(s->compNames[f]));
2339ea844a1aSMatthew Knepley   }
23409566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->numFieldComponents));
23419566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->fieldNames));
23429566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->compNames));
23439566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->field));
23449566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&s->bc));
23459566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->bcIndices));
23469566063dSJacob Faibussowitsch   PetscCall(PetscFree2(s->atlasDof, s->atlasOff));
23479566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&s->clSection));
23489566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&s->clPoints));
23499566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&s->perm));
23509566063dSJacob Faibussowitsch   PetscCall(PetscSectionResetClosurePermutation(s));
23519566063dSJacob Faibussowitsch   PetscCall(PetscSectionSymDestroy(&s->sym));
23529566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&s->clSection));
23539566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&s->clPoints));
235469c11d05SVaclav Hapla   PetscCall(PetscSectionInvalidateMaxDof_Internal(s));
2355ea844a1aSMatthew Knepley   s->pStart    = -1;
2356ea844a1aSMatthew Knepley   s->pEnd      = -1;
2357ea844a1aSMatthew Knepley   s->maxDof    = 0;
2358ea844a1aSMatthew Knepley   s->setup     = PETSC_FALSE;
2359ea844a1aSMatthew Knepley   s->numFields = 0;
2360ea844a1aSMatthew Knepley   s->clObj     = NULL;
23613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2362ea844a1aSMatthew Knepley }
2363ea844a1aSMatthew Knepley 
2364ea844a1aSMatthew Knepley /*@
2365ea844a1aSMatthew Knepley   PetscSectionDestroy - Frees a section object and frees its range if that exists.
2366ea844a1aSMatthew Knepley 
236740750d41SVaclav Hapla   Not Collective
2368ea844a1aSMatthew Knepley 
2369*2fe279fdSBarry Smith   Input Parameter:
2370cab54364SBarry Smith . s - the `PetscSection`
2371ea844a1aSMatthew Knepley 
2372ea844a1aSMatthew Knepley   Level: beginner
2373ea844a1aSMatthew Knepley 
2374cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionReset()`
2375ea844a1aSMatthew Knepley @*/
2376d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionDestroy(PetscSection *s)
2377d71ae5a4SJacob Faibussowitsch {
2378ea844a1aSMatthew Knepley   PetscFunctionBegin;
23793ba16761SJacob Faibussowitsch   if (!*s) PetscFunctionReturn(PETSC_SUCCESS);
238062f17a49SStefano Zampini   PetscValidHeaderSpecific(*s, PETSC_SECTION_CLASSID, 1);
2381ea844a1aSMatthew Knepley   if (--((PetscObject)(*s))->refct > 0) {
2382ea844a1aSMatthew Knepley     *s = NULL;
23833ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2384ea844a1aSMatthew Knepley   }
23859566063dSJacob Faibussowitsch   PetscCall(PetscSectionReset(*s));
23869566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(s));
23873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2388ea844a1aSMatthew Knepley }
2389ea844a1aSMatthew Knepley 
2390d71ae5a4SJacob Faibussowitsch PetscErrorCode VecIntGetValuesSection(PetscInt *baseArray, PetscSection s, PetscInt point, const PetscInt **values)
2391d71ae5a4SJacob Faibussowitsch {
2392ea844a1aSMatthew Knepley   const PetscInt p = point - s->pStart;
2393ea844a1aSMatthew Knepley 
2394ea844a1aSMatthew Knepley   PetscFunctionBegin;
2395ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 2);
2396ea844a1aSMatthew Knepley   *values = &baseArray[s->atlasOff[p]];
23973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2398ea844a1aSMatthew Knepley }
2399ea844a1aSMatthew Knepley 
2400d71ae5a4SJacob Faibussowitsch PetscErrorCode VecIntSetValuesSection(PetscInt *baseArray, PetscSection s, PetscInt point, const PetscInt values[], InsertMode mode)
2401d71ae5a4SJacob Faibussowitsch {
2402ea844a1aSMatthew Knepley   PetscInt      *array;
2403ea844a1aSMatthew Knepley   const PetscInt p           = point - s->pStart;
2404ea844a1aSMatthew Knepley   const PetscInt orientation = 0; /* Needs to be included for use in closure operations */
2405ea844a1aSMatthew Knepley   PetscInt       cDim        = 0;
2406ea844a1aSMatthew Knepley 
2407ea844a1aSMatthew Knepley   PetscFunctionBegin;
2408ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 2);
24099566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstraintDof(s, p, &cDim));
2410ea844a1aSMatthew Knepley   array = &baseArray[s->atlasOff[p]];
2411ea844a1aSMatthew Knepley   if (!cDim) {
2412ea844a1aSMatthew Knepley     if (orientation >= 0) {
2413ea844a1aSMatthew Knepley       const PetscInt dim = s->atlasDof[p];
2414ea844a1aSMatthew Knepley       PetscInt       i;
2415ea844a1aSMatthew Knepley 
2416ea844a1aSMatthew Knepley       if (mode == INSERT_VALUES) {
2417ea844a1aSMatthew Knepley         for (i = 0; i < dim; ++i) array[i] = values[i];
2418ea844a1aSMatthew Knepley       } else {
2419ea844a1aSMatthew Knepley         for (i = 0; i < dim; ++i) array[i] += values[i];
2420ea844a1aSMatthew Knepley       }
2421ea844a1aSMatthew Knepley     } else {
2422ea844a1aSMatthew Knepley       PetscInt offset = 0;
2423ea844a1aSMatthew Knepley       PetscInt j      = -1, field, i;
2424ea844a1aSMatthew Knepley 
2425ea844a1aSMatthew Knepley       for (field = 0; field < s->numFields; ++field) {
2426ea844a1aSMatthew Knepley         const PetscInt dim = s->field[field]->atlasDof[p];
2427ea844a1aSMatthew Knepley 
2428ea844a1aSMatthew Knepley         for (i = dim - 1; i >= 0; --i) array[++j] = values[i + offset];
2429ea844a1aSMatthew Knepley         offset += dim;
2430ea844a1aSMatthew Knepley       }
2431ea844a1aSMatthew Knepley     }
2432ea844a1aSMatthew Knepley   } else {
2433ea844a1aSMatthew Knepley     if (orientation >= 0) {
2434ea844a1aSMatthew Knepley       const PetscInt  dim  = s->atlasDof[p];
2435ea844a1aSMatthew Knepley       PetscInt        cInd = 0, i;
2436ea844a1aSMatthew Knepley       const PetscInt *cDof;
2437ea844a1aSMatthew Knepley 
24389566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetConstraintIndices(s, point, &cDof));
2439ea844a1aSMatthew Knepley       if (mode == INSERT_VALUES) {
2440ea844a1aSMatthew Knepley         for (i = 0; i < dim; ++i) {
24419371c9d4SSatish Balay           if ((cInd < cDim) && (i == cDof[cInd])) {
24429371c9d4SSatish Balay             ++cInd;
24439371c9d4SSatish Balay             continue;
24449371c9d4SSatish Balay           }
2445ea844a1aSMatthew Knepley           array[i] = values[i];
2446ea844a1aSMatthew Knepley         }
2447ea844a1aSMatthew Knepley       } else {
2448ea844a1aSMatthew Knepley         for (i = 0; i < dim; ++i) {
24499371c9d4SSatish Balay           if ((cInd < cDim) && (i == cDof[cInd])) {
24509371c9d4SSatish Balay             ++cInd;
24519371c9d4SSatish Balay             continue;
24529371c9d4SSatish Balay           }
2453ea844a1aSMatthew Knepley           array[i] += values[i];
2454ea844a1aSMatthew Knepley         }
2455ea844a1aSMatthew Knepley       }
2456ea844a1aSMatthew Knepley     } else {
2457ea844a1aSMatthew Knepley       const PetscInt *cDof;
2458ea844a1aSMatthew Knepley       PetscInt        offset  = 0;
2459ea844a1aSMatthew Knepley       PetscInt        cOffset = 0;
2460ea844a1aSMatthew Knepley       PetscInt        j       = 0, field;
2461ea844a1aSMatthew Knepley 
24629566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetConstraintIndices(s, point, &cDof));
2463ea844a1aSMatthew Knepley       for (field = 0; field < s->numFields; ++field) {
2464ea844a1aSMatthew Knepley         const PetscInt dim  = s->field[field]->atlasDof[p];     /* PetscSectionGetFieldDof() */
2465ea844a1aSMatthew Knepley         const PetscInt tDim = s->field[field]->bc->atlasDof[p]; /* PetscSectionGetFieldConstraintDof() */
2466ea844a1aSMatthew Knepley         const PetscInt sDim = dim - tDim;
2467ea844a1aSMatthew Knepley         PetscInt       cInd = 0, i, k;
2468ea844a1aSMatthew Knepley 
2469ea844a1aSMatthew Knepley         for (i = 0, k = dim + offset - 1; i < dim; ++i, ++j, --k) {
24709371c9d4SSatish Balay           if ((cInd < sDim) && (j == cDof[cInd + cOffset])) {
24719371c9d4SSatish Balay             ++cInd;
24729371c9d4SSatish Balay             continue;
24739371c9d4SSatish Balay           }
2474ea844a1aSMatthew Knepley           array[j] = values[k];
2475ea844a1aSMatthew Knepley         }
2476ea844a1aSMatthew Knepley         offset += dim;
2477ea844a1aSMatthew Knepley         cOffset += dim - tDim;
2478ea844a1aSMatthew Knepley       }
2479ea844a1aSMatthew Knepley     }
2480ea844a1aSMatthew Knepley   }
24813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2482ea844a1aSMatthew Knepley }
2483ea844a1aSMatthew Knepley 
2484ea844a1aSMatthew Knepley /*@C
248520662ed9SBarry Smith   PetscSectionHasConstraints - Determine whether a `PetscSection` has constrained dofs
2486ea844a1aSMatthew Knepley 
248740750d41SVaclav Hapla   Not Collective
2488ea844a1aSMatthew Knepley 
2489ea844a1aSMatthew Knepley   Input Parameter:
2490cab54364SBarry Smith . s - The `PetscSection`
2491ea844a1aSMatthew Knepley 
2492ea844a1aSMatthew Knepley   Output Parameter:
2493ea844a1aSMatthew Knepley . hasConstraints - flag indicating that the section has constrained dofs
2494ea844a1aSMatthew Knepley 
2495ea844a1aSMatthew Knepley   Level: intermediate
2496ea844a1aSMatthew Knepley 
2497cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSetConstraintIndices()`, `PetscSectionGetConstraintDof()`, `PetscSection`
2498ea844a1aSMatthew Knepley @*/
2499d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionHasConstraints(PetscSection s, PetscBool *hasConstraints)
2500d71ae5a4SJacob Faibussowitsch {
2501ea844a1aSMatthew Knepley   PetscFunctionBegin;
2502ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2503dadcf809SJacob Faibussowitsch   PetscValidBoolPointer(hasConstraints, 2);
2504ea844a1aSMatthew Knepley   *hasConstraints = s->bc ? PETSC_TRUE : PETSC_FALSE;
25053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2506ea844a1aSMatthew Knepley }
2507ea844a1aSMatthew Knepley 
2508ea844a1aSMatthew Knepley /*@C
250920662ed9SBarry Smith   PetscSectionGetConstraintIndices - Get the point dof numbers, in [0, dof), which are constrained for a given point
2510ea844a1aSMatthew Knepley 
251140750d41SVaclav Hapla   Not Collective
2512ea844a1aSMatthew Knepley 
2513ea844a1aSMatthew Knepley   Input Parameters:
2514cab54364SBarry Smith + s     - The `PetscSection`
2515ea844a1aSMatthew Knepley - point - The point
2516ea844a1aSMatthew Knepley 
2517ea844a1aSMatthew Knepley   Output Parameter:
2518ea844a1aSMatthew Knepley . indices - The constrained dofs
2519ea844a1aSMatthew Knepley 
2520ea844a1aSMatthew Knepley   Level: intermediate
2521ea844a1aSMatthew Knepley 
2522cab54364SBarry Smith   Fortran Note:
252320662ed9SBarry Smith   Use `PetscSectionGetConstraintIndicesF90()` and `PetscSectionRestoreConstraintIndicesF90()`
2524cab54364SBarry Smith 
2525cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSetConstraintIndices()`, `PetscSectionGetConstraintDof()`, `PetscSection`
2526ea844a1aSMatthew Knepley @*/
2527d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetConstraintIndices(PetscSection s, PetscInt point, const PetscInt **indices)
2528d71ae5a4SJacob Faibussowitsch {
2529ea844a1aSMatthew Knepley   PetscFunctionBegin;
2530ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2531ea844a1aSMatthew Knepley   if (s->bc) {
25329566063dSJacob Faibussowitsch     PetscCall(VecIntGetValuesSection(s->bcIndices, s->bc, point, indices));
2533ea844a1aSMatthew Knepley   } else *indices = NULL;
25343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2535ea844a1aSMatthew Knepley }
2536ea844a1aSMatthew Knepley 
2537ea844a1aSMatthew Knepley /*@C
2538ea844a1aSMatthew Knepley   PetscSectionSetConstraintIndices - Set the point dof numbers, in [0, dof), which are constrained
2539ea844a1aSMatthew Knepley 
254040750d41SVaclav Hapla   Not Collective
2541ea844a1aSMatthew Knepley 
2542ea844a1aSMatthew Knepley   Input Parameters:
2543cab54364SBarry Smith + s     - The `PetscSection`
2544ea844a1aSMatthew Knepley . point - The point
2545ea844a1aSMatthew Knepley - indices - The constrained dofs
2546ea844a1aSMatthew Knepley 
2547ea844a1aSMatthew Knepley   Level: intermediate
2548ea844a1aSMatthew Knepley 
2549cab54364SBarry Smith   Fortran Note:
2550cab54364SBarry Smith   Use `PetscSectionSetConstraintIndicesF90()`
2551cab54364SBarry Smith 
2552cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionGetConstraintIndices()`, `PetscSectionGetConstraintDof()`, `PetscSection`
2553ea844a1aSMatthew Knepley @*/
2554d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetConstraintIndices(PetscSection s, PetscInt point, const PetscInt indices[])
2555d71ae5a4SJacob Faibussowitsch {
2556ea844a1aSMatthew Knepley   PetscFunctionBegin;
2557ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2558ea844a1aSMatthew Knepley   if (s->bc) {
2559d57bb9dbSMatthew G. Knepley     const PetscInt dof  = s->atlasDof[point];
2560d57bb9dbSMatthew G. Knepley     const PetscInt cdof = s->bc->atlasDof[point];
2561d57bb9dbSMatthew G. Knepley     PetscInt       d;
2562d57bb9dbSMatthew G. Knepley 
2563ad540459SPierre Jolivet     for (d = 0; d < cdof; ++d) PetscCheck(indices[d] < dof, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %" PetscInt_FMT " dof %" PetscInt_FMT ", invalid constraint index[%" PetscInt_FMT "]: %" PetscInt_FMT, point, dof, d, indices[d]);
25649566063dSJacob Faibussowitsch     PetscCall(VecIntSetValuesSection(s->bcIndices, s->bc, point, indices, INSERT_VALUES));
2565ea844a1aSMatthew Knepley   }
25663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2567ea844a1aSMatthew Knepley }
2568ea844a1aSMatthew Knepley 
2569ea844a1aSMatthew Knepley /*@C
2570ea844a1aSMatthew Knepley   PetscSectionGetFieldConstraintIndices - Get the field dof numbers, in [0, fdof), which are constrained
2571ea844a1aSMatthew Knepley 
257240750d41SVaclav Hapla   Not Collective
2573ea844a1aSMatthew Knepley 
2574ea844a1aSMatthew Knepley   Input Parameters:
2575cab54364SBarry Smith + s     - The `PetscSection`
2576ea844a1aSMatthew Knepley . field  - The field number
2577ea844a1aSMatthew Knepley - point - The point
2578ea844a1aSMatthew Knepley 
2579ea844a1aSMatthew Knepley   Output Parameter:
25809759eb26SJed Brown . indices - The constrained dofs sorted in ascending order
2581ea844a1aSMatthew Knepley 
2582ea844a1aSMatthew Knepley   Level: intermediate
2583ea844a1aSMatthew Knepley 
2584cab54364SBarry Smith   Note:
2585cab54364SBarry Smith   The indices array, which is provided by the caller, must have capacity to hold the number of constrained dofs, e.g., as returned by `PetscSectionGetConstraintDof()`.
2586cab54364SBarry Smith 
2587cab54364SBarry Smith   Fortran Note:
258820662ed9SBarry Smith   Use `PetscSectionGetFieldConstraintIndicesF90()` and `PetscSectionRestoreFieldConstraintIndicesF90()`
2589cab54364SBarry Smith 
2590cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSetFieldConstraintIndices()`, `PetscSectionGetConstraintIndices()`, `PetscSectionGetConstraintDof()`, `PetscSection`
2591ea844a1aSMatthew Knepley @*/
2592d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldConstraintIndices(PetscSection s, PetscInt point, PetscInt field, const PetscInt **indices)
2593d71ae5a4SJacob Faibussowitsch {
2594ea844a1aSMatthew Knepley   PetscFunctionBegin;
2595ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2596dadcf809SJacob Faibussowitsch   PetscValidPointer(indices, 4);
25972abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
25989566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstraintIndices(s->field[field], point, indices));
25993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2600ea844a1aSMatthew Knepley }
2601ea844a1aSMatthew Knepley 
2602ea844a1aSMatthew Knepley /*@C
2603ea844a1aSMatthew Knepley   PetscSectionSetFieldConstraintIndices - Set the field dof numbers, in [0, fdof), which are constrained
2604ea844a1aSMatthew Knepley 
260540750d41SVaclav Hapla   Not Collective
2606ea844a1aSMatthew Knepley 
2607ea844a1aSMatthew Knepley   Input Parameters:
2608cab54364SBarry Smith + s       - The `PetscSection`
2609ea844a1aSMatthew Knepley . point   - The point
2610ea844a1aSMatthew Knepley . field   - The field number
2611ea844a1aSMatthew Knepley - indices - The constrained dofs
2612ea844a1aSMatthew Knepley 
2613ea844a1aSMatthew Knepley   Level: intermediate
2614ea844a1aSMatthew Knepley 
2615cab54364SBarry Smith   Fortran Note:
2616cab54364SBarry Smith   Use `PetscSectionSetFieldConstraintIndicesF90()`
2617cab54364SBarry Smith 
2618cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSetConstraintIndices()`, `PetscSectionGetFieldConstraintIndices()`, `PetscSectionGetConstraintDof()`, `PetscSection`
2619ea844a1aSMatthew Knepley @*/
2620d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFieldConstraintIndices(PetscSection s, PetscInt point, PetscInt field, const PetscInt indices[])
2621d71ae5a4SJacob Faibussowitsch {
2622ea844a1aSMatthew Knepley   PetscFunctionBegin;
2623ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
26242abc8c78SJacob Faibussowitsch   if (PetscDefined(USE_DEBUG)) {
2625e2bfaee7SMatthew G. Knepley     PetscInt nfdof;
26262abc8c78SJacob Faibussowitsch 
26279566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldConstraintDof(s, point, field, &nfdof));
2628e2bfaee7SMatthew G. Knepley     if (nfdof) PetscValidIntPointer(indices, 4);
26292abc8c78SJacob Faibussowitsch   }
26302abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
26319566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetConstraintIndices(s->field[field], point, indices));
26323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2633ea844a1aSMatthew Knepley }
2634ea844a1aSMatthew Knepley 
2635ea844a1aSMatthew Knepley /*@
2636ea844a1aSMatthew Knepley   PetscSectionPermute - Reorder the section according to the input point permutation
2637ea844a1aSMatthew Knepley 
263840750d41SVaclav Hapla   Collective
2639ea844a1aSMatthew Knepley 
2640d8d19677SJose E. Roman   Input Parameters:
2641cab54364SBarry Smith + section - The `PetscSection` object
2642ea844a1aSMatthew Knepley - perm - The point permutation, old point p becomes new point perm[p]
2643ea844a1aSMatthew Knepley 
2644ea844a1aSMatthew Knepley   Output Parameter:
2645cab54364SBarry Smith . sectionNew - The permuted `PetscSection`
2646ea844a1aSMatthew Knepley 
2647ea844a1aSMatthew Knepley   Level: intermediate
2648ea844a1aSMatthew Knepley 
2649cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `IS`, `PetscSection`, `MatPermute()`
2650ea844a1aSMatthew Knepley @*/
2651d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionPermute(PetscSection section, IS permutation, PetscSection *sectionNew)
2652d71ae5a4SJacob Faibussowitsch {
2653ea844a1aSMatthew Knepley   PetscSection    s = section, sNew;
2654ea844a1aSMatthew Knepley   const PetscInt *perm;
2655b778fa18SValeria Barra   PetscInt        numFields, f, c, numPoints, pStart, pEnd, p;
2656ea844a1aSMatthew Knepley 
2657ea844a1aSMatthew Knepley   PetscFunctionBegin;
2658ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
2659ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(permutation, IS_CLASSID, 2);
2660ea844a1aSMatthew Knepley   PetscValidPointer(sectionNew, 3);
26619566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s), &sNew));
26629566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &numFields));
26639566063dSJacob Faibussowitsch   if (numFields) PetscCall(PetscSectionSetNumFields(sNew, numFields));
2664ea844a1aSMatthew Knepley   for (f = 0; f < numFields; ++f) {
2665ea844a1aSMatthew Knepley     const char *name;
2666ea844a1aSMatthew Knepley     PetscInt    numComp;
2667ea844a1aSMatthew Knepley 
26689566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldName(s, f, &name));
26699566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldName(sNew, f, name));
26709566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &numComp));
26719566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(sNew, f, numComp));
2672b778fa18SValeria Barra     for (c = 0; c < s->numFieldComponents[f]; ++c) {
26739566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetComponentName(s, f, c, &name));
26749566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetComponentName(sNew, f, c, name));
2675b778fa18SValeria Barra     }
2676ea844a1aSMatthew Knepley   }
26779566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(permutation, &numPoints));
26789566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(permutation, &perm));
26799566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
26809566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(sNew, pStart, pEnd));
268108401ef6SPierre Jolivet   PetscCheck(numPoints >= pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Permutation size %" PetscInt_FMT " is less than largest Section point %" PetscInt_FMT, numPoints, pEnd);
2682ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
2683ea844a1aSMatthew Knepley     PetscInt dof, cdof;
2684ea844a1aSMatthew Knepley 
26859566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s, p, &dof));
26869566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(sNew, perm[p], dof));
26879566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
26889566063dSJacob Faibussowitsch     if (cdof) PetscCall(PetscSectionSetConstraintDof(sNew, perm[p], cdof));
2689ea844a1aSMatthew Knepley     for (f = 0; f < numFields; ++f) {
26909566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
26919566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(sNew, perm[p], f, dof));
26929566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(s, p, f, &cdof));
26939566063dSJacob Faibussowitsch       if (cdof) PetscCall(PetscSectionSetFieldConstraintDof(sNew, perm[p], f, cdof));
2694ea844a1aSMatthew Knepley     }
2695ea844a1aSMatthew Knepley   }
26969566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(sNew));
2697ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
2698ea844a1aSMatthew Knepley     const PetscInt *cind;
2699ea844a1aSMatthew Knepley     PetscInt        cdof;
2700ea844a1aSMatthew Knepley 
27019566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
2702ea844a1aSMatthew Knepley     if (cdof) {
27039566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetConstraintIndices(s, p, &cind));
27049566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetConstraintIndices(sNew, perm[p], cind));
2705ea844a1aSMatthew Knepley     }
2706ea844a1aSMatthew Knepley     for (f = 0; f < numFields; ++f) {
27079566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(s, p, f, &cdof));
2708ea844a1aSMatthew Knepley       if (cdof) {
27099566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldConstraintIndices(s, p, f, &cind));
27109566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetFieldConstraintIndices(sNew, perm[p], f, cind));
2711ea844a1aSMatthew Knepley       }
2712ea844a1aSMatthew Knepley     }
2713ea844a1aSMatthew Knepley   }
27149566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(permutation, &perm));
2715ea844a1aSMatthew Knepley   *sectionNew = sNew;
27163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2717ea844a1aSMatthew Knepley }
2718ea844a1aSMatthew Knepley 
2719ea844a1aSMatthew Knepley /*@
272020662ed9SBarry Smith   PetscSectionSetClosureIndex - Create an internal data structure to speed up closure queries.
2721ea844a1aSMatthew Knepley 
272240750d41SVaclav Hapla   Collective
2723ea844a1aSMatthew Knepley 
2724ea844a1aSMatthew Knepley   Input Parameters:
2725cab54364SBarry Smith + section   - The `PetscSection`
2726cab54364SBarry Smith . obj       - A `PetscObject` which serves as the key for this index
2727cab54364SBarry Smith . clSection - `PetscSection` giving the size of the closure of each point
2728cab54364SBarry Smith - clPoints  - `IS` giving the points in each closure
2729ea844a1aSMatthew Knepley 
2730ea844a1aSMatthew Knepley   Level: advanced
2731ea844a1aSMatthew Knepley 
2732cab54364SBarry Smith   Note:
273320662ed9SBarry Smith   This function creates an internal map from each point to its closure. We compress out closure points with no dofs in this section.
273420662ed9SBarry Smith 
273520662ed9SBarry Smith   Developer Note:
273620662ed9SBarry Smith   The information provided here is completely opaque
2737cab54364SBarry Smith 
2738cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetClosureIndex()`, `DMPlexCreateClosureIndex()`
2739ea844a1aSMatthew Knepley @*/
2740d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetClosureIndex(PetscSection section, PetscObject obj, PetscSection clSection, IS clPoints)
2741d71ae5a4SJacob Faibussowitsch {
2742ea844a1aSMatthew Knepley   PetscFunctionBegin;
27433e03ebd8SStefano Zampini   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
27443e03ebd8SStefano Zampini   PetscValidHeaderSpecific(clSection, PETSC_SECTION_CLASSID, 3);
27453e03ebd8SStefano Zampini   PetscValidHeaderSpecific(clPoints, IS_CLASSID, 4);
27469566063dSJacob Faibussowitsch   if (section->clObj != obj) PetscCall(PetscSectionResetClosurePermutation(section));
2747ea844a1aSMatthew Knepley   section->clObj = obj;
27489566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)clSection));
27499566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)clPoints));
27509566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&section->clSection));
27519566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&section->clPoints));
2752ea844a1aSMatthew Knepley   section->clSection = clSection;
2753ea844a1aSMatthew Knepley   section->clPoints  = clPoints;
27543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2755ea844a1aSMatthew Knepley }
2756ea844a1aSMatthew Knepley 
2757ea844a1aSMatthew Knepley /*@
2758cab54364SBarry Smith   PetscSectionGetClosureIndex - Get the cache of points in the closure of each point in the section set with `PetscSectionSetClosureIndex()`
2759ea844a1aSMatthew Knepley 
276040750d41SVaclav Hapla   Collective
2761ea844a1aSMatthew Knepley 
2762ea844a1aSMatthew Knepley   Input Parameters:
2763cab54364SBarry Smith + section   - The `PetscSection`
2764cab54364SBarry Smith - obj       - A `PetscObject` which serves as the key for this index
2765ea844a1aSMatthew Knepley 
2766ea844a1aSMatthew Knepley   Output Parameters:
2767cab54364SBarry Smith + clSection - `PetscSection` giving the size of the closure of each point
2768cab54364SBarry Smith - clPoints  - `IS` giving the points in each closure
2769ea844a1aSMatthew Knepley 
2770ea844a1aSMatthew Knepley   Level: advanced
2771ea844a1aSMatthew Knepley 
2772cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSetClosureIndex()`, `DMPlexCreateClosureIndex()`
2773ea844a1aSMatthew Knepley @*/
2774d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetClosureIndex(PetscSection section, PetscObject obj, PetscSection *clSection, IS *clPoints)
2775d71ae5a4SJacob Faibussowitsch {
2776ea844a1aSMatthew Knepley   PetscFunctionBegin;
2777ea844a1aSMatthew Knepley   if (section->clObj == obj) {
2778ea844a1aSMatthew Knepley     if (clSection) *clSection = section->clSection;
2779ea844a1aSMatthew Knepley     if (clPoints) *clPoints = section->clPoints;
2780ea844a1aSMatthew Knepley   } else {
2781ea844a1aSMatthew Knepley     if (clSection) *clSection = NULL;
2782ea844a1aSMatthew Knepley     if (clPoints) *clPoints = NULL;
2783ea844a1aSMatthew Knepley   }
27843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2785ea844a1aSMatthew Knepley }
2786ea844a1aSMatthew Knepley 
2787d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetClosurePermutation_Internal(PetscSection section, PetscObject obj, PetscInt depth, PetscInt clSize, PetscCopyMode mode, PetscInt *clPerm)
2788d71ae5a4SJacob Faibussowitsch {
2789ea844a1aSMatthew Knepley   PetscInt                    i;
2790c459fbc1SJed Brown   khiter_t                    iter;
2791c459fbc1SJed Brown   int                         new_entry;
2792c459fbc1SJed Brown   PetscSectionClosurePermKey  key = {depth, clSize};
2793c459fbc1SJed Brown   PetscSectionClosurePermVal *val;
2794ea844a1aSMatthew Knepley 
2795ea844a1aSMatthew Knepley   PetscFunctionBegin;
2796ea844a1aSMatthew Knepley   if (section->clObj != obj) {
27979566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&section->clSection));
27989566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&section->clPoints));
2799ea844a1aSMatthew Knepley   }
2800ea844a1aSMatthew Knepley   section->clObj = obj;
28019566063dSJacob Faibussowitsch   if (!section->clHash) PetscCall(PetscClPermCreate(&section->clHash));
2802c459fbc1SJed Brown   iter = kh_put(ClPerm, section->clHash, key, &new_entry);
2803c459fbc1SJed Brown   val  = &kh_val(section->clHash, iter);
2804c459fbc1SJed Brown   if (!new_entry) {
28059566063dSJacob Faibussowitsch     PetscCall(PetscFree(val->perm));
28069566063dSJacob Faibussowitsch     PetscCall(PetscFree(val->invPerm));
2807c459fbc1SJed Brown   }
2808ea844a1aSMatthew Knepley   if (mode == PETSC_COPY_VALUES) {
28099566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(clSize, &val->perm));
28109566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(val->perm, clPerm, clSize));
2811ea844a1aSMatthew Knepley   } else if (mode == PETSC_OWN_POINTER) {
2812c459fbc1SJed Brown     val->perm = clPerm;
2813ea844a1aSMatthew Knepley   } else SETERRQ(PetscObjectComm(obj), PETSC_ERR_SUP, "Do not support borrowed arrays");
28149566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(clSize, &val->invPerm));
2815c459fbc1SJed Brown   for (i = 0; i < clSize; ++i) val->invPerm[clPerm[i]] = i;
28163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2817ea844a1aSMatthew Knepley }
2818ea844a1aSMatthew Knepley 
2819ea844a1aSMatthew Knepley /*@
2820c459fbc1SJed Brown   PetscSectionSetClosurePermutation - Set the dof permutation for the closure of each cell in the section, meaning clPerm[newIndex] = oldIndex.
2821ea844a1aSMatthew Knepley 
2822ea844a1aSMatthew Knepley   Not Collective
2823ea844a1aSMatthew Knepley 
2824ea844a1aSMatthew Knepley   Input Parameters:
2825cab54364SBarry Smith + section - The `PetscSection`
2826cab54364SBarry Smith . obj     - A `PetscObject` which serves as the key for this index (usually a `DM`)
2827c459fbc1SJed Brown . depth   - Depth of points on which to apply the given permutation
2828ea844a1aSMatthew Knepley - perm    - Permutation of the cell dof closure
2829ea844a1aSMatthew Knepley 
2830cab54364SBarry Smith   Level: intermediate
2831cab54364SBarry Smith 
2832cab54364SBarry Smith   Notes:
2833c459fbc1SJed Brown   The specified permutation will only be applied to points at depth whose closure size matches the length of perm.  In a
2834c459fbc1SJed Brown   mixed-topology or variable-degree finite element space, this function can be called multiple times at each depth for
2835c459fbc1SJed Brown   each topology and degree.
2836c459fbc1SJed Brown 
2837c459fbc1SJed Brown   This approach assumes that (depth, len(perm)) uniquely identifies the desired permutation; this might not be true for
2838c459fbc1SJed Brown   exotic/enriched spaces on mixed topology meshes.
2839ea844a1aSMatthew Knepley 
2840cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `IS`, `PetscSectionGetClosurePermutation()`, `PetscSectionGetClosureIndex()`, `DMPlexCreateClosureIndex()`, `PetscCopyMode`
2841ea844a1aSMatthew Knepley @*/
2842d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetClosurePermutation(PetscSection section, PetscObject obj, PetscInt depth, IS perm)
2843d71ae5a4SJacob Faibussowitsch {
2844ea844a1aSMatthew Knepley   const PetscInt *clPerm = NULL;
2845ea844a1aSMatthew Knepley   PetscInt        clSize = 0;
2846ea844a1aSMatthew Knepley 
2847ea844a1aSMatthew Knepley   PetscFunctionBegin;
2848ea844a1aSMatthew Knepley   if (perm) {
28499566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(perm, &clSize));
28509566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(perm, &clPerm));
2851ea844a1aSMatthew Knepley   }
28529566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetClosurePermutation_Internal(section, obj, depth, clSize, PETSC_COPY_VALUES, (PetscInt *)clPerm));
28539566063dSJacob Faibussowitsch   if (perm) PetscCall(ISRestoreIndices(perm, &clPerm));
28543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2855ea844a1aSMatthew Knepley }
2856ea844a1aSMatthew Knepley 
2857d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetClosurePermutation_Internal(PetscSection section, PetscObject obj, PetscInt depth, PetscInt size, const PetscInt *perm[])
2858d71ae5a4SJacob Faibussowitsch {
2859ea844a1aSMatthew Knepley   PetscFunctionBegin;
2860ea844a1aSMatthew Knepley   if (section->clObj == obj) {
2861c459fbc1SJed Brown     PetscSectionClosurePermKey k = {depth, size};
2862c459fbc1SJed Brown     PetscSectionClosurePermVal v;
28639566063dSJacob Faibussowitsch     PetscCall(PetscClPermGet(section->clHash, k, &v));
2864c459fbc1SJed Brown     if (perm) *perm = v.perm;
2865ea844a1aSMatthew Knepley   } else {
2866ea844a1aSMatthew Knepley     if (perm) *perm = NULL;
2867ea844a1aSMatthew Knepley   }
28683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2869ea844a1aSMatthew Knepley }
2870ea844a1aSMatthew Knepley 
2871ea844a1aSMatthew Knepley /*@
2872ea844a1aSMatthew Knepley   PetscSectionGetClosurePermutation - Get the dof permutation for the closure of each cell in the section, meaning clPerm[newIndex] = oldIndex.
2873ea844a1aSMatthew Knepley 
287440750d41SVaclav Hapla   Not Collective
2875ea844a1aSMatthew Knepley 
2876ea844a1aSMatthew Knepley   Input Parameters:
2877cab54364SBarry Smith + section   - The `PetscSection`
2878cab54364SBarry Smith . obj       - A `PetscObject` which serves as the key for this index (usually a DM)
2879c459fbc1SJed Brown . depth     - Depth stratum on which to obtain closure permutation
2880c459fbc1SJed Brown - clSize    - Closure size to be permuted (e.g., may vary with element topology and degree)
2881ea844a1aSMatthew Knepley 
2882ea844a1aSMatthew Knepley   Output Parameter:
2883ea844a1aSMatthew Knepley . perm - The dof closure permutation
2884ea844a1aSMatthew Knepley 
2885ea844a1aSMatthew Knepley   Level: intermediate
2886ea844a1aSMatthew Knepley 
2887cab54364SBarry Smith   Note:
2888cab54364SBarry Smith   The user must destroy the `IS` that is returned.
2889cab54364SBarry Smith 
2890cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `IS`, `PetscSectionSetClosurePermutation()`, `PetscSectionGetClosureInversePermutation()`, `PetscSectionGetClosureIndex()`, `PetscSectionSetClosureIndex()`, `DMPlexCreateClosureIndex()`
2891ea844a1aSMatthew Knepley @*/
2892d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetClosurePermutation(PetscSection section, PetscObject obj, PetscInt depth, PetscInt clSize, IS *perm)
2893d71ae5a4SJacob Faibussowitsch {
2894ea844a1aSMatthew Knepley   const PetscInt *clPerm;
2895ea844a1aSMatthew Knepley 
2896ea844a1aSMatthew Knepley   PetscFunctionBegin;
28979566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetClosurePermutation_Internal(section, obj, depth, clSize, &clPerm));
28989566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, clSize, clPerm, PETSC_USE_POINTER, perm));
28993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2900ea844a1aSMatthew Knepley }
2901ea844a1aSMatthew Knepley 
2902d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetClosureInversePermutation_Internal(PetscSection section, PetscObject obj, PetscInt depth, PetscInt size, const PetscInt *perm[])
2903d71ae5a4SJacob Faibussowitsch {
2904ea844a1aSMatthew Knepley   PetscFunctionBegin;
2905c459fbc1SJed Brown   if (section->clObj == obj && section->clHash) {
2906c459fbc1SJed Brown     PetscSectionClosurePermKey k = {depth, size};
2907c459fbc1SJed Brown     PetscSectionClosurePermVal v;
29089566063dSJacob Faibussowitsch     PetscCall(PetscClPermGet(section->clHash, k, &v));
2909c459fbc1SJed Brown     if (perm) *perm = v.invPerm;
2910ea844a1aSMatthew Knepley   } else {
2911ea844a1aSMatthew Knepley     if (perm) *perm = NULL;
2912ea844a1aSMatthew Knepley   }
29133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2914ea844a1aSMatthew Knepley }
2915ea844a1aSMatthew Knepley 
2916ea844a1aSMatthew Knepley /*@
2917ea844a1aSMatthew Knepley   PetscSectionGetClosureInversePermutation - Get the inverse dof permutation for the closure of each cell in the section, meaning clPerm[oldIndex] = newIndex.
2918ea844a1aSMatthew Knepley 
291940750d41SVaclav Hapla   Not Collective
2920ea844a1aSMatthew Knepley 
2921ea844a1aSMatthew Knepley   Input Parameters:
2922cab54364SBarry Smith + section   - The `PetscSection`
2923cab54364SBarry Smith . obj       - A `PetscObject` which serves as the key for this index (usually a `DM`)
2924c459fbc1SJed Brown . depth     - Depth stratum on which to obtain closure permutation
2925c459fbc1SJed Brown - clSize    - Closure size to be permuted (e.g., may vary with element topology and degree)
2926ea844a1aSMatthew Knepley 
2927*2fe279fdSBarry Smith   Output Parameter:
2928c459fbc1SJed Brown . perm - The dof closure permutation
2929ea844a1aSMatthew Knepley 
2930ea844a1aSMatthew Knepley   Level: intermediate
2931ea844a1aSMatthew Knepley 
2932cab54364SBarry Smith   Note:
2933cab54364SBarry Smith   The user must destroy the `IS` that is returned.
2934cab54364SBarry Smith 
2935cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `IS`, `PetscSectionSetClosurePermutation()`, `PetscSectionGetClosureIndex()`, `PetscSectionSetClosureIndex()`, `DMPlexCreateClosureIndex()`
2936ea844a1aSMatthew Knepley @*/
2937d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetClosureInversePermutation(PetscSection section, PetscObject obj, PetscInt depth, PetscInt clSize, IS *perm)
2938d71ae5a4SJacob Faibussowitsch {
2939ea844a1aSMatthew Knepley   const PetscInt *clPerm;
2940ea844a1aSMatthew Knepley 
2941ea844a1aSMatthew Knepley   PetscFunctionBegin;
29429566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetClosureInversePermutation_Internal(section, obj, depth, clSize, &clPerm));
29439566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, clSize, clPerm, PETSC_USE_POINTER, perm));
29443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2945ea844a1aSMatthew Knepley }
2946ea844a1aSMatthew Knepley 
2947ea844a1aSMatthew Knepley /*@
2948ea844a1aSMatthew Knepley   PetscSectionGetField - Get the subsection associated with a single field
2949ea844a1aSMatthew Knepley 
2950ea844a1aSMatthew Knepley   Input Parameters:
2951cab54364SBarry Smith + s     - The `PetscSection`
2952ea844a1aSMatthew Knepley - field - The field number
2953ea844a1aSMatthew Knepley 
2954ea844a1aSMatthew Knepley   Output Parameter:
2955ea844a1aSMatthew Knepley . subs  - The subsection for the given field
2956ea844a1aSMatthew Knepley 
2957ea844a1aSMatthew Knepley   Level: intermediate
2958ea844a1aSMatthew Knepley 
2959cab54364SBarry Smith   Note:
2960cab54364SBarry Smith   Does not increase the reference count of the selected sub-section. There is no matching `PetscSectionRestoreField()`
2961cab54364SBarry Smith 
2962cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `IS`, `PetscSectionSetNumFields()`
2963ea844a1aSMatthew Knepley @*/
2964d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetField(PetscSection s, PetscInt field, PetscSection *subs)
2965d71ae5a4SJacob Faibussowitsch {
2966ea844a1aSMatthew Knepley   PetscFunctionBegin;
2967ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2968ea844a1aSMatthew Knepley   PetscValidPointer(subs, 3);
29692abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
2970ea844a1aSMatthew Knepley   *subs = s->field[field];
29713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2972ea844a1aSMatthew Knepley }
2973ea844a1aSMatthew Knepley 
2974ea844a1aSMatthew Knepley PetscClassId      PETSC_SECTION_SYM_CLASSID;
2975ea844a1aSMatthew Knepley PetscFunctionList PetscSectionSymList = NULL;
2976ea844a1aSMatthew Knepley 
2977ea844a1aSMatthew Knepley /*@
2978cab54364SBarry Smith   PetscSectionSymCreate - Creates an empty `PetscSectionSym` object.
2979ea844a1aSMatthew Knepley 
2980ea844a1aSMatthew Knepley   Collective
2981ea844a1aSMatthew Knepley 
2982ea844a1aSMatthew Knepley   Input Parameter:
2983ea844a1aSMatthew Knepley . comm - the MPI communicator
2984ea844a1aSMatthew Knepley 
2985ea844a1aSMatthew Knepley   Output Parameter:
2986ea844a1aSMatthew Knepley . sym - pointer to the new set of symmetries
2987ea844a1aSMatthew Knepley 
2988ea844a1aSMatthew Knepley   Level: developer
2989ea844a1aSMatthew Knepley 
2990cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSym`, `PetscSectionSymDestroy()`
2991ea844a1aSMatthew Knepley @*/
2992d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymCreate(MPI_Comm comm, PetscSectionSym *sym)
2993d71ae5a4SJacob Faibussowitsch {
2994ea844a1aSMatthew Knepley   PetscFunctionBegin;
2995ea844a1aSMatthew Knepley   PetscValidPointer(sym, 2);
29969566063dSJacob Faibussowitsch   PetscCall(ISInitializePackage());
29979566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(*sym, PETSC_SECTION_SYM_CLASSID, "PetscSectionSym", "Section Symmetry", "IS", comm, PetscSectionSymDestroy, PetscSectionSymView));
29983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2999ea844a1aSMatthew Knepley }
3000ea844a1aSMatthew Knepley 
3001ea844a1aSMatthew Knepley /*@C
3002cab54364SBarry Smith   PetscSectionSymSetType - Builds a `PetscSectionSym`, for a particular implementation.
3003ea844a1aSMatthew Knepley 
300440750d41SVaclav Hapla   Collective
3005ea844a1aSMatthew Knepley 
3006ea844a1aSMatthew Knepley   Input Parameters:
3007ea844a1aSMatthew Knepley + sym    - The section symmetry object
3008ea844a1aSMatthew Knepley - method - The name of the section symmetry type
3009ea844a1aSMatthew Knepley 
3010ea844a1aSMatthew Knepley   Level: developer
3011ea844a1aSMatthew Knepley 
3012cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionSymType`, `PetscSectionSymGetType()`, `PetscSectionSymCreate()`
3013ea844a1aSMatthew Knepley @*/
3014d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymSetType(PetscSectionSym sym, PetscSectionSymType method)
3015d71ae5a4SJacob Faibussowitsch {
3016ea844a1aSMatthew Knepley   PetscErrorCode (*r)(PetscSectionSym);
3017ea844a1aSMatthew Knepley   PetscBool match;
3018ea844a1aSMatthew Knepley 
3019ea844a1aSMatthew Knepley   PetscFunctionBegin;
3020ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(sym, PETSC_SECTION_SYM_CLASSID, 1);
30219566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)sym, method, &match));
30223ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
3023ea844a1aSMatthew Knepley 
30249566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(PetscSectionSymList, method, &r));
302528b400f6SJacob Faibussowitsch   PetscCheck(r, PETSC_COMM_SELF, PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscSectionSym type: %s", method);
3026dbbe0bcdSBarry Smith   PetscTryTypeMethod(sym, destroy);
3027ea844a1aSMatthew Knepley   sym->ops->destroy = NULL;
3028dbbe0bcdSBarry Smith 
30299566063dSJacob Faibussowitsch   PetscCall((*r)(sym));
30309566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)sym, method));
30313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3032ea844a1aSMatthew Knepley }
3033ea844a1aSMatthew Knepley 
3034ea844a1aSMatthew Knepley /*@C
3035cab54364SBarry Smith   PetscSectionSymGetType - Gets the section symmetry type name (as a string) from the `PetscSectionSym`.
3036ea844a1aSMatthew Knepley 
3037ea844a1aSMatthew Knepley   Not Collective
3038ea844a1aSMatthew Knepley 
3039ea844a1aSMatthew Knepley   Input Parameter:
3040ea844a1aSMatthew Knepley . sym  - The section symmetry
3041ea844a1aSMatthew Knepley 
3042ea844a1aSMatthew Knepley   Output Parameter:
3043ea844a1aSMatthew Knepley . type - The index set type name
3044ea844a1aSMatthew Knepley 
3045ea844a1aSMatthew Knepley   Level: developer
3046ea844a1aSMatthew Knepley 
3047cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionSymType`, `PetscSectionSymSetType()`, `PetscSectionSymCreate()`
3048ea844a1aSMatthew Knepley @*/
3049d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymGetType(PetscSectionSym sym, PetscSectionSymType *type)
3050d71ae5a4SJacob Faibussowitsch {
3051ea844a1aSMatthew Knepley   PetscFunctionBegin;
3052ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(sym, PETSC_SECTION_SYM_CLASSID, 1);
3053dadcf809SJacob Faibussowitsch   PetscValidPointer(type, 2);
3054ea844a1aSMatthew Knepley   *type = ((PetscObject)sym)->type_name;
30553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3056ea844a1aSMatthew Knepley }
3057ea844a1aSMatthew Knepley 
3058ea844a1aSMatthew Knepley /*@C
3059cab54364SBarry Smith   PetscSectionSymRegister - Registers a new section symmetry implementation
3060ea844a1aSMatthew Knepley 
3061ea844a1aSMatthew Knepley   Not Collective
3062ea844a1aSMatthew Knepley 
3063ea844a1aSMatthew Knepley   Input Parameters:
3064*2fe279fdSBarry Smith + sname        - The name of a new user-defined creation routine
3065*2fe279fdSBarry Smith - function - The creation routine itself
3066ea844a1aSMatthew Knepley 
3067ea844a1aSMatthew Knepley   Level: developer
3068ea844a1aSMatthew Knepley 
3069cab54364SBarry Smith   Notes:
3070cab54364SBarry Smith   `PetscSectionSymRegister()` may be called multiple times to add several user-defined vectors
3071cab54364SBarry Smith 
3072cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionSymType`, `PetscSectionSymCreate()`, `PetscSectionSymSetType()`
3073ea844a1aSMatthew Knepley @*/
3074d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymRegister(const char sname[], PetscErrorCode (*function)(PetscSectionSym))
3075d71ae5a4SJacob Faibussowitsch {
3076ea844a1aSMatthew Knepley   PetscFunctionBegin;
30779566063dSJacob Faibussowitsch   PetscCall(ISInitializePackage());
30789566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&PetscSectionSymList, sname, function));
30793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3080ea844a1aSMatthew Knepley }
3081ea844a1aSMatthew Knepley 
3082ea844a1aSMatthew Knepley /*@
3083ea844a1aSMatthew Knepley    PetscSectionSymDestroy - Destroys a section symmetry.
3084ea844a1aSMatthew Knepley 
308540750d41SVaclav Hapla    Collective
3086ea844a1aSMatthew Knepley 
3087*2fe279fdSBarry Smith    Input Parameter:
3088ea844a1aSMatthew Knepley .  sym - the section symmetry
3089ea844a1aSMatthew Knepley 
3090ea844a1aSMatthew Knepley    Level: developer
3091ea844a1aSMatthew Knepley 
3092cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionSymCreate()`, `PetscSectionSymDestroy()`
3093ea844a1aSMatthew Knepley @*/
3094d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymDestroy(PetscSectionSym *sym)
3095d71ae5a4SJacob Faibussowitsch {
3096ea844a1aSMatthew Knepley   SymWorkLink link, next;
3097ea844a1aSMatthew Knepley 
3098ea844a1aSMatthew Knepley   PetscFunctionBegin;
30993ba16761SJacob Faibussowitsch   if (!*sym) PetscFunctionReturn(PETSC_SUCCESS);
3100ea844a1aSMatthew Knepley   PetscValidHeaderSpecific((*sym), PETSC_SECTION_SYM_CLASSID, 1);
31019371c9d4SSatish Balay   if (--((PetscObject)(*sym))->refct > 0) {
31029371c9d4SSatish Balay     *sym = NULL;
31033ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
3104ea844a1aSMatthew Knepley   }
310548a46eb9SPierre Jolivet   if ((*sym)->ops->destroy) PetscCall((*(*sym)->ops->destroy)(*sym));
3106c9cc58a2SBarry Smith   PetscCheck(!(*sym)->workout, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Work array still checked out");
3107ea844a1aSMatthew Knepley   for (link = (*sym)->workin; link; link = next) {
31080c99d500SBarry Smith     PetscInt    **perms = (PetscInt **)link->perms;
31090c99d500SBarry Smith     PetscScalar **rots  = (PetscScalar **)link->rots;
31100c99d500SBarry Smith     PetscCall(PetscFree2(perms, rots));
3111ea844a1aSMatthew Knepley     next = link->next;
31129566063dSJacob Faibussowitsch     PetscCall(PetscFree(link));
3113ea844a1aSMatthew Knepley   }
3114ea844a1aSMatthew Knepley   (*sym)->workin = NULL;
31159566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(sym));
31163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3117ea844a1aSMatthew Knepley }
3118ea844a1aSMatthew Knepley 
3119ea844a1aSMatthew Knepley /*@C
3120ea844a1aSMatthew Knepley    PetscSectionSymView - Displays a section symmetry
3121ea844a1aSMatthew Knepley 
312240750d41SVaclav Hapla    Collective
3123ea844a1aSMatthew Knepley 
3124ea844a1aSMatthew Knepley    Input Parameters:
3125ea844a1aSMatthew Knepley +  sym - the index set
3126cab54364SBarry Smith -  viewer - viewer used to display the set, for example `PETSC_VIEWER_STDOUT_SELF`.
3127ea844a1aSMatthew Knepley 
3128ea844a1aSMatthew Knepley    Level: developer
3129ea844a1aSMatthew Knepley 
3130cab54364SBarry Smith .seealso:  `PetscSectionSym`, `PetscViewer`, `PetscViewerASCIIOpen()`
3131ea844a1aSMatthew Knepley @*/
3132d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymView(PetscSectionSym sym, PetscViewer viewer)
3133d71ae5a4SJacob Faibussowitsch {
3134ea844a1aSMatthew Knepley   PetscFunctionBegin;
3135ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(sym, PETSC_SECTION_SYM_CLASSID, 1);
313648a46eb9SPierre Jolivet   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)sym), &viewer));
3137ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
3138ea844a1aSMatthew Knepley   PetscCheckSameComm(sym, 1, viewer, 2);
31399566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)sym, viewer));
3140dbbe0bcdSBarry Smith   PetscTryTypeMethod(sym, view, viewer);
31413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3142ea844a1aSMatthew Knepley }
3143ea844a1aSMatthew Knepley 
3144ea844a1aSMatthew Knepley /*@
3145ea844a1aSMatthew Knepley   PetscSectionSetSym - Set the symmetries for the data referred to by the section
3146ea844a1aSMatthew Knepley 
314740750d41SVaclav Hapla   Collective
3148ea844a1aSMatthew Knepley 
3149ea844a1aSMatthew Knepley   Input Parameters:
3150ea844a1aSMatthew Knepley + section - the section describing data layout
3151ea844a1aSMatthew Knepley - sym - the symmetry describing the affect of orientation on the access of the data
3152ea844a1aSMatthew Knepley 
3153ea844a1aSMatthew Knepley   Level: developer
3154ea844a1aSMatthew Knepley 
3155cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionGetSym()`, `PetscSectionSymCreate()`
3156ea844a1aSMatthew Knepley @*/
3157d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetSym(PetscSection section, PetscSectionSym sym)
3158d71ae5a4SJacob Faibussowitsch {
3159ea844a1aSMatthew Knepley   PetscFunctionBegin;
3160ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
31619566063dSJacob Faibussowitsch   PetscCall(PetscSectionSymDestroy(&(section->sym)));
3162ea844a1aSMatthew Knepley   if (sym) {
3163ea844a1aSMatthew Knepley     PetscValidHeaderSpecific(sym, PETSC_SECTION_SYM_CLASSID, 2);
3164ea844a1aSMatthew Knepley     PetscCheckSameComm(section, 1, sym, 2);
31659566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)sym));
3166ea844a1aSMatthew Knepley   }
3167ea844a1aSMatthew Knepley   section->sym = sym;
31683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3169ea844a1aSMatthew Knepley }
3170ea844a1aSMatthew Knepley 
3171ea844a1aSMatthew Knepley /*@
3172ea844a1aSMatthew Knepley   PetscSectionGetSym - Get the symmetries for the data referred to by the section
3173ea844a1aSMatthew Knepley 
317440750d41SVaclav Hapla   Not Collective
3175ea844a1aSMatthew Knepley 
3176*2fe279fdSBarry Smith   Input Parameter:
3177ea844a1aSMatthew Knepley . section - the section describing data layout
3178ea844a1aSMatthew Knepley 
3179*2fe279fdSBarry Smith   Output Parameter:
318020662ed9SBarry Smith . sym - the symmetry describing the affect of orientation on the access of the data, provided previously by `PetscSectionSetSym()`
3181ea844a1aSMatthew Knepley 
3182ea844a1aSMatthew Knepley   Level: developer
3183ea844a1aSMatthew Knepley 
3184cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionSetSym()`, `PetscSectionSymCreate()`
3185ea844a1aSMatthew Knepley @*/
3186d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetSym(PetscSection section, PetscSectionSym *sym)
3187d71ae5a4SJacob Faibussowitsch {
3188ea844a1aSMatthew Knepley   PetscFunctionBegin;
3189ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
3190ea844a1aSMatthew Knepley   *sym = section->sym;
31913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3192ea844a1aSMatthew Knepley }
3193ea844a1aSMatthew Knepley 
3194ea844a1aSMatthew Knepley /*@
3195ea844a1aSMatthew Knepley   PetscSectionSetFieldSym - Set the symmetries for the data referred to by a field of the section
3196ea844a1aSMatthew Knepley 
319740750d41SVaclav Hapla   Collective
3198ea844a1aSMatthew Knepley 
3199ea844a1aSMatthew Knepley   Input Parameters:
3200ea844a1aSMatthew Knepley + section - the section describing data layout
3201ea844a1aSMatthew Knepley . field - the field number
3202ea844a1aSMatthew Knepley - sym - the symmetry describing the affect of orientation on the access of the data
3203ea844a1aSMatthew Knepley 
3204ea844a1aSMatthew Knepley   Level: developer
3205ea844a1aSMatthew Knepley 
3206cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionGetFieldSym()`, `PetscSectionSymCreate()`
3207ea844a1aSMatthew Knepley @*/
3208d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFieldSym(PetscSection section, PetscInt field, PetscSectionSym sym)
3209d71ae5a4SJacob Faibussowitsch {
3210ea844a1aSMatthew Knepley   PetscFunctionBegin;
3211ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
32122abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, section->numFields);
32139566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetSym(section->field[field], sym));
32143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3215ea844a1aSMatthew Knepley }
3216ea844a1aSMatthew Knepley 
3217ea844a1aSMatthew Knepley /*@
3218ea844a1aSMatthew Knepley   PetscSectionGetFieldSym - Get the symmetries for the data referred to by a field of the section
3219ea844a1aSMatthew Knepley 
322040750d41SVaclav Hapla   Collective
3221ea844a1aSMatthew Knepley 
3222ea844a1aSMatthew Knepley   Input Parameters:
3223ea844a1aSMatthew Knepley + section - the section describing data layout
3224ea844a1aSMatthew Knepley - field - the field number
3225ea844a1aSMatthew Knepley 
3226*2fe279fdSBarry Smith   Output Parameter:
3227ea844a1aSMatthew Knepley . sym - the symmetry describing the affect of orientation on the access of the data
3228ea844a1aSMatthew Knepley 
3229ea844a1aSMatthew Knepley   Level: developer
3230ea844a1aSMatthew Knepley 
3231cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionSetFieldSym()`, `PetscSectionSymCreate()`
3232ea844a1aSMatthew Knepley @*/
3233d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldSym(PetscSection section, PetscInt field, PetscSectionSym *sym)
3234d71ae5a4SJacob Faibussowitsch {
3235ea844a1aSMatthew Knepley   PetscFunctionBegin;
3236ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
32372abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, section->numFields);
3238ea844a1aSMatthew Knepley   *sym = section->field[field]->sym;
32393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3240ea844a1aSMatthew Knepley }
3241ea844a1aSMatthew Knepley 
3242ea844a1aSMatthew Knepley /*@C
3243cab54364SBarry Smith   PetscSectionGetPointSyms - Get the symmetries for a set of points in a `PetscSection` under specific orientations.
3244ea844a1aSMatthew Knepley 
324540750d41SVaclav Hapla   Not Collective
3246ea844a1aSMatthew Knepley 
3247ea844a1aSMatthew Knepley   Input Parameters:
3248ea844a1aSMatthew Knepley + section - the section
3249ea844a1aSMatthew Knepley . numPoints - the number of points
325020662ed9SBarry Smith - points - an array of size 2 * `numPoints`, containing a list of (point, orientation) pairs. (An orientation is an
325120662ed9SBarry Smith     arbitrary integer: its interpretation is up to sym.  Orientations are used by `DM`: for their interpretation in that
325220662ed9SBarry Smith     context, see `DMPlexGetConeOrientation()`).
3253ea844a1aSMatthew Knepley 
3254d8d19677SJose E. Roman   Output Parameters:
325520662ed9SBarry Smith + perms - The permutations for the given orientations (or `NULL` if there is no symmetry or the permutation is the identity).
325620662ed9SBarry Smith - rots - The field rotations symmetries for the given orientations (or `NULL` if there is no symmetry or the rotations are all
3257ea844a1aSMatthew Knepley     identity).
3258ea844a1aSMatthew Knepley 
3259ea844a1aSMatthew Knepley   Example of usage, gathering dofs into a local array (lArray) from a section array (sArray):
3260ea844a1aSMatthew Knepley .vb
3261ea844a1aSMatthew Knepley      const PetscInt    **perms;
3262ea844a1aSMatthew Knepley      const PetscScalar **rots;
3263ea844a1aSMatthew Knepley      PetscInt            lOffset;
3264ea844a1aSMatthew Knepley 
3265ea844a1aSMatthew Knepley      PetscSectionGetPointSyms(section,numPoints,points,&perms,&rots);
3266ea844a1aSMatthew Knepley      for (i = 0, lOffset = 0; i < numPoints; i++) {
3267ea844a1aSMatthew Knepley        PetscInt           point = points[2*i], dof, sOffset;
3268ea844a1aSMatthew Knepley        const PetscInt    *perm  = perms ? perms[i] : NULL;
3269ea844a1aSMatthew Knepley        const PetscScalar *rot   = rots  ? rots[i]  : NULL;
3270ea844a1aSMatthew Knepley 
3271ea844a1aSMatthew Knepley        PetscSectionGetDof(section,point,&dof);
3272ea844a1aSMatthew Knepley        PetscSectionGetOffset(section,point,&sOffset);
3273ea844a1aSMatthew Knepley 
3274ea844a1aSMatthew Knepley        if (perm) {for (j = 0; j < dof; j++) {lArray[lOffset + perm[j]]  = sArray[sOffset + j];}}
3275ea844a1aSMatthew Knepley        else      {for (j = 0; j < dof; j++) {lArray[lOffset +      j ]  = sArray[sOffset + j];}}
3276ea844a1aSMatthew Knepley        if (rot)  {for (j = 0; j < dof; j++) {lArray[lOffset +      j ] *= rot[j];             }}
3277ea844a1aSMatthew Knepley        lOffset += dof;
3278ea844a1aSMatthew Knepley      }
3279ea844a1aSMatthew Knepley      PetscSectionRestorePointSyms(section,numPoints,points,&perms,&rots);
3280ea844a1aSMatthew Knepley .ve
3281ea844a1aSMatthew Knepley 
3282ea844a1aSMatthew Knepley   Example of usage, adding dofs into a section array (sArray) from a local array (lArray):
3283ea844a1aSMatthew Knepley .vb
3284ea844a1aSMatthew Knepley      const PetscInt    **perms;
3285ea844a1aSMatthew Knepley      const PetscScalar **rots;
3286ea844a1aSMatthew Knepley      PetscInt            lOffset;
3287ea844a1aSMatthew Knepley 
3288ea844a1aSMatthew Knepley      PetscSectionGetPointSyms(section,numPoints,points,&perms,&rots);
3289ea844a1aSMatthew Knepley      for (i = 0, lOffset = 0; i < numPoints; i++) {
3290ea844a1aSMatthew Knepley        PetscInt           point = points[2*i], dof, sOffset;
3291ea844a1aSMatthew Knepley        const PetscInt    *perm  = perms ? perms[i] : NULL;
3292ea844a1aSMatthew Knepley        const PetscScalar *rot   = rots  ? rots[i]  : NULL;
3293ea844a1aSMatthew Knepley 
3294ea844a1aSMatthew Knepley        PetscSectionGetDof(section,point,&dof);
3295ea844a1aSMatthew Knepley        PetscSectionGetOffset(section,point,&sOff);
3296ea844a1aSMatthew Knepley 
3297ea844a1aSMatthew Knepley        if (perm) {for (j = 0; j < dof; j++) {sArray[sOffset + j] += lArray[lOffset + perm[j]] * (rot ? PetscConj(rot[perm[j]]) : 1.);}}
3298ea844a1aSMatthew Knepley        else      {for (j = 0; j < dof; j++) {sArray[sOffset + j] += lArray[lOffset +      j ] * (rot ? PetscConj(rot[     j ]) : 1.);}}
3299ea844a1aSMatthew Knepley        offset += dof;
3300ea844a1aSMatthew Knepley      }
3301ea844a1aSMatthew Knepley      PetscSectionRestorePointSyms(section,numPoints,points,&perms,&rots);
3302ea844a1aSMatthew Knepley .ve
3303ea844a1aSMatthew Knepley 
3304ea844a1aSMatthew Knepley   Level: developer
3305ea844a1aSMatthew Knepley 
330620662ed9SBarry Smith   Note:
330720662ed9SBarry Smith   `PetscSectionSetSym()` must have been previously called to provide the symmetries to the `PetscSection`
330820662ed9SBarry Smith 
330920662ed9SBarry Smith   Use `PetscSectionRestorePointSyms()` when finished with the data
331020662ed9SBarry Smith 
3311cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionRestorePointSyms()`, `PetscSectionSymCreate()`, `PetscSectionSetSym()`, `PetscSectionGetSym()`
3312ea844a1aSMatthew Knepley @*/
3313d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetPointSyms(PetscSection section, PetscInt numPoints, const PetscInt *points, const PetscInt ***perms, const PetscScalar ***rots)
3314d71ae5a4SJacob Faibussowitsch {
3315ea844a1aSMatthew Knepley   PetscSectionSym sym;
3316ea844a1aSMatthew Knepley 
3317ea844a1aSMatthew Knepley   PetscFunctionBegin;
3318ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
3319ea844a1aSMatthew Knepley   if (numPoints) PetscValidIntPointer(points, 3);
3320ea844a1aSMatthew Knepley   if (perms) *perms = NULL;
3321ea844a1aSMatthew Knepley   if (rots) *rots = NULL;
3322ea844a1aSMatthew Knepley   sym = section->sym;
3323ea844a1aSMatthew Knepley   if (sym && (perms || rots)) {
3324ea844a1aSMatthew Knepley     SymWorkLink link;
3325ea844a1aSMatthew Knepley 
3326ea844a1aSMatthew Knepley     if (sym->workin) {
3327ea844a1aSMatthew Knepley       link        = sym->workin;
3328ea844a1aSMatthew Knepley       sym->workin = sym->workin->next;
3329ea844a1aSMatthew Knepley     } else {
33304dfa11a4SJacob Faibussowitsch       PetscCall(PetscNew(&link));
3331ea844a1aSMatthew Knepley     }
3332ea844a1aSMatthew Knepley     if (numPoints > link->numPoints) {
33330c99d500SBarry Smith       PetscInt    **perms = (PetscInt **)link->perms;
33340c99d500SBarry Smith       PetscScalar **rots  = (PetscScalar **)link->rots;
33350c99d500SBarry Smith       PetscCall(PetscFree2(perms, rots));
33360c99d500SBarry Smith       PetscCall(PetscMalloc2(numPoints, (PetscInt ***)&link->perms, numPoints, (PetscScalar ***)&link->rots));
3337ea844a1aSMatthew Knepley       link->numPoints = numPoints;
3338ea844a1aSMatthew Knepley     }
3339ea844a1aSMatthew Knepley     link->next   = sym->workout;
3340ea844a1aSMatthew Knepley     sym->workout = link;
33419566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero((PetscInt **)link->perms, numPoints));
33429566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero((PetscInt **)link->rots, numPoints));
33439566063dSJacob Faibussowitsch     PetscCall((*sym->ops->getpoints)(sym, section, numPoints, points, link->perms, link->rots));
3344ea844a1aSMatthew Knepley     if (perms) *perms = link->perms;
3345ea844a1aSMatthew Knepley     if (rots) *rots = link->rots;
3346ea844a1aSMatthew Knepley   }
33473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3348ea844a1aSMatthew Knepley }
3349ea844a1aSMatthew Knepley 
3350ea844a1aSMatthew Knepley /*@C
3351cab54364SBarry Smith   PetscSectionRestorePointSyms - Restore the symmetries returned by `PetscSectionGetPointSyms()`
3352ea844a1aSMatthew Knepley 
335340750d41SVaclav Hapla   Not Collective
3354ea844a1aSMatthew Knepley 
3355ea844a1aSMatthew Knepley   Input Parameters:
3356ea844a1aSMatthew Knepley + section - the section
3357ea844a1aSMatthew Knepley . numPoints - the number of points
335820662ed9SBarry Smith - points - an array of size 2 * `numPoints`, containing a list of (point, orientation) pairs. (An orientation is an
3359cab54364SBarry Smith     arbitrary integer: its interpretation is up to sym.  Orientations are used by `DM`: for their interpretation in that
3360cab54364SBarry Smith     context, see `DMPlexGetConeOrientation()`).
336120662ed9SBarry Smith . perms - The permutations for the given orientations: set to `NULL` at conclusion
336220662ed9SBarry Smith - rots - The field rotations symmetries for the given orientations: set to `NULL` at conclusion
3363ea844a1aSMatthew Knepley 
3364ea844a1aSMatthew Knepley   Level: developer
3365ea844a1aSMatthew Knepley 
3366cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionGetPointSyms()`, `PetscSectionSymCreate()`, `PetscSectionSetSym()`, `PetscSectionGetSym()`
3367ea844a1aSMatthew Knepley @*/
3368d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionRestorePointSyms(PetscSection section, PetscInt numPoints, const PetscInt *points, const PetscInt ***perms, const PetscScalar ***rots)
3369d71ae5a4SJacob Faibussowitsch {
3370ea844a1aSMatthew Knepley   PetscSectionSym sym;
3371ea844a1aSMatthew Knepley 
3372ea844a1aSMatthew Knepley   PetscFunctionBegin;
3373ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
3374ea844a1aSMatthew Knepley   sym = section->sym;
3375ea844a1aSMatthew Knepley   if (sym && (perms || rots)) {
3376ea844a1aSMatthew Knepley     SymWorkLink *p, link;
3377ea844a1aSMatthew Knepley 
3378ea844a1aSMatthew Knepley     for (p = &sym->workout; (link = *p); p = &link->next) {
3379ea844a1aSMatthew Knepley       if ((perms && link->perms == *perms) || (rots && link->rots == *rots)) {
3380ea844a1aSMatthew Knepley         *p          = link->next;
3381ea844a1aSMatthew Knepley         link->next  = sym->workin;
3382ea844a1aSMatthew Knepley         sym->workin = link;
3383ea844a1aSMatthew Knepley         if (perms) *perms = NULL;
3384ea844a1aSMatthew Knepley         if (rots) *rots = NULL;
33853ba16761SJacob Faibussowitsch         PetscFunctionReturn(PETSC_SUCCESS);
3386ea844a1aSMatthew Knepley       }
3387ea844a1aSMatthew Knepley     }
3388ea844a1aSMatthew Knepley     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Array was not checked out");
3389ea844a1aSMatthew Knepley   }
33903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3391ea844a1aSMatthew Knepley }
3392ea844a1aSMatthew Knepley 
3393ea844a1aSMatthew Knepley /*@C
3394cab54364SBarry Smith   PetscSectionGetFieldPointSyms - Get the symmetries for a set of points in a field of a `PetscSection` under specific orientations.
3395ea844a1aSMatthew Knepley 
339640750d41SVaclav Hapla   Not Collective
3397ea844a1aSMatthew Knepley 
3398ea844a1aSMatthew Knepley   Input Parameters:
3399ea844a1aSMatthew Knepley + section - the section
3400ea844a1aSMatthew Knepley . field - the field of the section
3401ea844a1aSMatthew Knepley . numPoints - the number of points
340220662ed9SBarry Smith - points - an array of size 2 * `numPoints`, containing a list of (point, orientation) pairs. (An orientation is an
3403cab54364SBarry Smith     arbitrary integer: its interpretation is up to sym.  Orientations are used by `DM`: for their interpretation in that
3404cab54364SBarry Smith     context, see `DMPlexGetConeOrientation()`).
3405ea844a1aSMatthew Knepley 
3406d8d19677SJose E. Roman   Output Parameters:
340720662ed9SBarry Smith + perms - The permutations for the given orientations (or `NULL` if there is no symmetry or the permutation is the identity).
340820662ed9SBarry Smith - rots - The field rotations symmetries for the given orientations (or `NULL` if there is no symmetry or the rotations are all
3409ea844a1aSMatthew Knepley     identity).
3410ea844a1aSMatthew Knepley 
3411ea844a1aSMatthew Knepley   Level: developer
3412ea844a1aSMatthew Knepley 
341320662ed9SBarry Smith   Notes:
341420662ed9SBarry Smith   `PetscSectionSetFieldSym()` must have been previously called to provide the symmetries to the `PetscSection`
341520662ed9SBarry Smith 
341620662ed9SBarry Smith   Use `PetscSectionRestoreFieldPointSyms()` when finished with the data
341720662ed9SBarry Smith 
3418cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionGetPointSyms()`, `PetscSectionRestoreFieldPointSyms()`
3419ea844a1aSMatthew Knepley @*/
3420d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldPointSyms(PetscSection section, PetscInt field, PetscInt numPoints, const PetscInt *points, const PetscInt ***perms, const PetscScalar ***rots)
3421d71ae5a4SJacob Faibussowitsch {
3422ea844a1aSMatthew Knepley   PetscFunctionBegin;
3423ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
342408401ef6SPierre Jolivet   PetscCheck(field <= section->numFields, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "field %" PetscInt_FMT " greater than number of fields (%" PetscInt_FMT ") in section", field, section->numFields);
34259566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetPointSyms(section->field[field], numPoints, points, perms, rots));
34263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3427ea844a1aSMatthew Knepley }
3428ea844a1aSMatthew Knepley 
3429ea844a1aSMatthew Knepley /*@C
3430cab54364SBarry Smith   PetscSectionRestoreFieldPointSyms - Restore the symmetries returned by `PetscSectionGetFieldPointSyms()`
3431ea844a1aSMatthew Knepley 
343240750d41SVaclav Hapla   Not Collective
3433ea844a1aSMatthew Knepley 
3434ea844a1aSMatthew Knepley   Input Parameters:
3435ea844a1aSMatthew Knepley + section - the section
3436ea844a1aSMatthew Knepley . field - the field number
3437ea844a1aSMatthew Knepley . numPoints - the number of points
343820662ed9SBarry Smith - points - an array of size 2 * `numPoints`, containing a list of (point, orientation) pairs. (An orientation is an
3439cab54364SBarry Smith     arbitrary integer: its interpretation is up to sym.  Orientations are used by `DM`: for their interpretation in that
3440cab54364SBarry Smith     context, see `DMPlexGetConeOrientation()`).
344120662ed9SBarry Smith . perms - The permutations for the given orientations: set to NULL at conclusion
3442ea844a1aSMatthew Knepley - rots - The field rotations symmetries for the given orientations: set to NULL at conclusion
3443ea844a1aSMatthew Knepley 
3444ea844a1aSMatthew Knepley   Level: developer
3445ea844a1aSMatthew Knepley 
3446cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionRestorePointSyms()`, `petscSectionGetFieldPointSyms()`, `PetscSectionSymCreate()`, `PetscSectionSetSym()`, `PetscSectionGetSym()`
3447ea844a1aSMatthew Knepley @*/
3448d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionRestoreFieldPointSyms(PetscSection section, PetscInt field, PetscInt numPoints, const PetscInt *points, const PetscInt ***perms, const PetscScalar ***rots)
3449d71ae5a4SJacob Faibussowitsch {
3450ea844a1aSMatthew Knepley   PetscFunctionBegin;
3451ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
345208401ef6SPierre Jolivet   PetscCheck(field <= section->numFields, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "field %" PetscInt_FMT " greater than number of fields (%" PetscInt_FMT ") in section", field, section->numFields);
34539566063dSJacob Faibussowitsch   PetscCall(PetscSectionRestorePointSyms(section->field[field], numPoints, points, perms, rots));
34543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3455ea844a1aSMatthew Knepley }
3456ea844a1aSMatthew Knepley 
3457ea844a1aSMatthew Knepley /*@
3458b004864fSMatthew G. Knepley   PetscSectionSymCopy - Copy the symmetries, assuming that the point structure is compatible
3459b004864fSMatthew G. Knepley 
346040750d41SVaclav Hapla   Not Collective
3461b004864fSMatthew G. Knepley 
3462b004864fSMatthew G. Knepley   Input Parameter:
3463cab54364SBarry Smith . sym - the `PetscSectionSym`
3464b004864fSMatthew G. Knepley 
3465b004864fSMatthew G. Knepley   Output Parameter:
3466b004864fSMatthew G. Knepley . nsym - the equivalent symmetries
3467b004864fSMatthew G. Knepley 
3468b004864fSMatthew G. Knepley   Level: developer
3469b004864fSMatthew G. Knepley 
3470cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionSymCreate()`, `PetscSectionSetSym()`, `PetscSectionGetSym()`, `PetscSectionSymLabelSetStratum()`, `PetscSectionGetPointSyms()`
3471b004864fSMatthew G. Knepley @*/
3472d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymCopy(PetscSectionSym sym, PetscSectionSym nsym)
3473d71ae5a4SJacob Faibussowitsch {
3474b004864fSMatthew G. Knepley   PetscFunctionBegin;
3475b004864fSMatthew G. Knepley   PetscValidHeaderSpecific(sym, PETSC_SECTION_SYM_CLASSID, 1);
3476b004864fSMatthew G. Knepley   PetscValidHeaderSpecific(nsym, PETSC_SECTION_SYM_CLASSID, 2);
3477dbbe0bcdSBarry Smith   PetscTryTypeMethod(sym, copy, nsym);
34783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3479b004864fSMatthew G. Knepley }
3480b004864fSMatthew G. Knepley 
3481b004864fSMatthew G. Knepley /*@
3482cab54364SBarry Smith   PetscSectionSymDistribute - Distribute the symmetries in accordance with the input `PetscSF`
3483b004864fSMatthew G. Knepley 
3484b004864fSMatthew G. Knepley   Collective
3485b004864fSMatthew G. Knepley 
3486b004864fSMatthew G. Knepley   Input Parameters:
3487cab54364SBarry Smith + sym - the `PetscSectionSym`
3488b004864fSMatthew G. Knepley - migrationSF - the distribution map from roots to leaves
3489b004864fSMatthew G. Knepley 
3490*2fe279fdSBarry Smith   Output Parameter:
3491b004864fSMatthew G. Knepley . dsym - the redistributed symmetries
3492b004864fSMatthew G. Knepley 
3493b004864fSMatthew G. Knepley   Level: developer
3494b004864fSMatthew G. Knepley 
3495cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionSymCreate()`, `PetscSectionSetSym()`, `PetscSectionGetSym()`, `PetscSectionSymLabelSetStratum()`, `PetscSectionGetPointSyms()`
3496b004864fSMatthew G. Knepley @*/
3497d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymDistribute(PetscSectionSym sym, PetscSF migrationSF, PetscSectionSym *dsym)
3498d71ae5a4SJacob Faibussowitsch {
3499b004864fSMatthew G. Knepley   PetscFunctionBegin;
3500b004864fSMatthew G. Knepley   PetscValidHeaderSpecific(sym, PETSC_SECTION_SYM_CLASSID, 1);
3501b004864fSMatthew G. Knepley   PetscValidHeaderSpecific(migrationSF, PETSCSF_CLASSID, 2);
3502b004864fSMatthew G. Knepley   PetscValidPointer(dsym, 3);
3503dbbe0bcdSBarry Smith   PetscTryTypeMethod(sym, distribute, migrationSF, dsym);
35043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3505b004864fSMatthew G. Knepley }
3506b004864fSMatthew G. Knepley 
3507b004864fSMatthew G. Knepley /*@
350820662ed9SBarry Smith   PetscSectionGetUseFieldOffsets - Get the flag indicating if field offsets are used directly in a global section, rather than just the point offset
3509ea844a1aSMatthew Knepley 
351040750d41SVaclav Hapla   Not Collective
3511ea844a1aSMatthew Knepley 
3512ea844a1aSMatthew Knepley   Input Parameter:
3513cab54364SBarry Smith . s - the global `PetscSection`
3514ea844a1aSMatthew Knepley 
3515*2fe279fdSBarry Smith   Output Parameter:
3516ea844a1aSMatthew Knepley . flg - the flag
3517ea844a1aSMatthew Knepley 
3518ea844a1aSMatthew Knepley   Level: developer
3519ea844a1aSMatthew Knepley 
3520cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionSetChart()`, `PetscSectionCreate()`
3521ea844a1aSMatthew Knepley @*/
3522d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetUseFieldOffsets(PetscSection s, PetscBool *flg)
3523d71ae5a4SJacob Faibussowitsch {
3524ea844a1aSMatthew Knepley   PetscFunctionBegin;
3525ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
3526ea844a1aSMatthew Knepley   *flg = s->useFieldOff;
35273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3528ea844a1aSMatthew Knepley }
3529ea844a1aSMatthew Knepley 
3530ea844a1aSMatthew Knepley /*@
3531ea844a1aSMatthew Knepley   PetscSectionSetUseFieldOffsets - Set the flag to use field offsets directly in a global section, rather than just the point offset
3532ea844a1aSMatthew Knepley 
353340750d41SVaclav Hapla   Not Collective
3534ea844a1aSMatthew Knepley 
3535ea844a1aSMatthew Knepley   Input Parameters:
3536cab54364SBarry Smith + s   - the global `PetscSection`
3537ea844a1aSMatthew Knepley - flg - the flag
3538ea844a1aSMatthew Knepley 
3539ea844a1aSMatthew Knepley   Level: developer
3540ea844a1aSMatthew Knepley 
3541cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionGetUseFieldOffsets()`, `PetscSectionSetChart()`, `PetscSectionCreate()`
3542ea844a1aSMatthew Knepley @*/
3543d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetUseFieldOffsets(PetscSection s, PetscBool flg)
3544d71ae5a4SJacob Faibussowitsch {
3545ea844a1aSMatthew Knepley   PetscFunctionBegin;
3546ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
3547ea844a1aSMatthew Knepley   s->useFieldOff = flg;
35483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3549ea844a1aSMatthew Knepley }
3550ea844a1aSMatthew Knepley 
3551ea844a1aSMatthew Knepley #define PetscSectionExpandPoints_Loop(TYPE) \
3552ea844a1aSMatthew Knepley   { \
3553ea844a1aSMatthew Knepley     PetscInt i, n, o0, o1, size; \
3554ea844a1aSMatthew Knepley     TYPE    *a0 = (TYPE *)origArray, *a1; \
35559566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(s, &size)); \
35569566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(size, &a1)); \
3557ea844a1aSMatthew Knepley     for (i = 0; i < npoints; i++) { \
35589566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(origSection, points_[i], &o0)); \
35599566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(s, i, &o1)); \
35609566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(s, i, &n)); \
35619566063dSJacob Faibussowitsch       PetscCall(PetscMemcpy(&a1[o1], &a0[o0], n *unitsize)); \
3562ea844a1aSMatthew Knepley     } \
3563ea844a1aSMatthew Knepley     *newArray = (void *)a1; \
3564ea844a1aSMatthew Knepley   }
3565ea844a1aSMatthew Knepley 
3566ea844a1aSMatthew Knepley /*@
3567ea844a1aSMatthew Knepley   PetscSectionExtractDofsFromArray - Extracts elements of an array corresponding to DOFs of specified points.
3568ea844a1aSMatthew Knepley 
356940750d41SVaclav Hapla   Not Collective
3570ea844a1aSMatthew Knepley 
3571ea844a1aSMatthew Knepley   Input Parameters:
3572cab54364SBarry Smith + origSection - the `PetscSection` describing the layout of the array
3573cab54364SBarry Smith . dataType - `MPI_Datatype` describing the data type of the array (currently only `MPIU_INT`, `MPIU_SCALAR`, `MPIU_REAL`)
357420662ed9SBarry Smith . origArray - the array; its size must be equal to the storage size of `origSection`
357520662ed9SBarry Smith - points - `IS` with points to extract; its indices must lie in the chart of `origSection`
3576ea844a1aSMatthew Knepley 
3577ea844a1aSMatthew Knepley   Output Parameters:
3578da81f932SPierre Jolivet + newSection - the new `PetscSection` describing the layout of the new array (with points renumbered 0,1,... but preserving numbers of DOFs)
357920662ed9SBarry Smith - newArray - the array of the extracted DOFs; its size is the storage size of `newSection`
3580ea844a1aSMatthew Knepley 
3581ea844a1aSMatthew Knepley   Level: developer
3582ea844a1aSMatthew Knepley 
3583cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionGetChart()`, `PetscSectionGetDof()`, `PetscSectionGetStorageSize()`, `PetscSectionCreate()`
3584ea844a1aSMatthew Knepley @*/
3585d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionExtractDofsFromArray(PetscSection origSection, MPI_Datatype dataType, const void *origArray, IS points, PetscSection *newSection, void *newArray[])
3586d71ae5a4SJacob Faibussowitsch {
3587ea844a1aSMatthew Knepley   PetscSection    s;
3588ea844a1aSMatthew Knepley   const PetscInt *points_;
3589ea844a1aSMatthew Knepley   PetscInt        i, n, npoints, pStart, pEnd;
3590ea844a1aSMatthew Knepley   PetscMPIInt     unitsize;
3591ea844a1aSMatthew Knepley 
3592ea844a1aSMatthew Knepley   PetscFunctionBegin;
3593ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(origSection, PETSC_SECTION_CLASSID, 1);
3594ea844a1aSMatthew Knepley   PetscValidPointer(origArray, 3);
3595ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(points, IS_CLASSID, 4);
3596ea844a1aSMatthew Knepley   if (newSection) PetscValidPointer(newSection, 5);
3597ea844a1aSMatthew Knepley   if (newArray) PetscValidPointer(newArray, 6);
35989566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Type_size(dataType, &unitsize));
35999566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(points, &npoints));
36009566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(points, &points_));
36019566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(origSection, &pStart, &pEnd));
36029566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &s));
36039566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(s, 0, npoints));
3604ea844a1aSMatthew Knepley   for (i = 0; i < npoints; i++) {
3605c9cc58a2SBarry Smith     PetscCheck(points_[i] >= pStart && points_[i] < pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "point %" PetscInt_FMT " (index %" PetscInt_FMT ") in input IS out of input section's chart", points_[i], i);
36069566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(origSection, points_[i], &n));
36079566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(s, i, n));
3608ea844a1aSMatthew Knepley   }
36099566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(s));
3610ea844a1aSMatthew Knepley   if (newArray) {
36119371c9d4SSatish Balay     if (dataType == MPIU_INT) {
36129371c9d4SSatish Balay       PetscSectionExpandPoints_Loop(PetscInt);
36139371c9d4SSatish Balay     } else if (dataType == MPIU_SCALAR) {
36149371c9d4SSatish Balay       PetscSectionExpandPoints_Loop(PetscScalar);
36159371c9d4SSatish Balay     } else if (dataType == MPIU_REAL) {
36169371c9d4SSatish Balay       PetscSectionExpandPoints_Loop(PetscReal);
36179371c9d4SSatish Balay     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "not implemented for this MPI_Datatype");
3618ea844a1aSMatthew Knepley   }
3619ea844a1aSMatthew Knepley   if (newSection) {
3620ea844a1aSMatthew Knepley     *newSection = s;
3621ea844a1aSMatthew Knepley   } else {
36229566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&s));
3623ea844a1aSMatthew Knepley   }
36249566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(points, &points_));
36253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3626ea844a1aSMatthew Knepley }
3627