xref: /petsc/src/vec/is/section/interface/section.c (revision 583308b62f6bac49336d8ce626babc1f47654b31)
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 
35*583308b6SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetChart()`, `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 
354*583308b6SBarry Smith   Notes:
355*583308b6SBarry Smith   Calling this destroys all the information in the `PetscSection` including the chart.
356*583308b6SBarry Smith 
357*583308b6SBarry Smith   You must call `PetscSectionSetChart()` after calling this.
358*583308b6SBarry Smith 
359*583308b6SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetNumFields()`, `PetscSectionSetChart()`, `PetscSectionReset()`
360ea844a1aSMatthew Knepley @*/
361d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetNumFields(PetscSection s, PetscInt numFields)
362d71ae5a4SJacob Faibussowitsch {
363ea844a1aSMatthew Knepley   PetscInt f;
364ea844a1aSMatthew Knepley 
365ea844a1aSMatthew Knepley   PetscFunctionBegin;
366ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
36708401ef6SPierre Jolivet   PetscCheck(numFields > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "The number of fields %" PetscInt_FMT " must be positive", numFields);
3689566063dSJacob Faibussowitsch   PetscCall(PetscSectionReset(s));
369ea844a1aSMatthew Knepley 
370ea844a1aSMatthew Knepley   s->numFields = numFields;
3719566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(s->numFields, &s->numFieldComponents));
3729566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(s->numFields, &s->fieldNames));
3739566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(s->numFields, &s->compNames));
3749566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(s->numFields, &s->field));
375ea844a1aSMatthew Knepley   for (f = 0; f < s->numFields; ++f) {
376ea844a1aSMatthew Knepley     char name[64];
377ea844a1aSMatthew Knepley 
378ea844a1aSMatthew Knepley     s->numFieldComponents[f] = 1;
379ea844a1aSMatthew Knepley 
3809566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s), &s->field[f]));
3819566063dSJacob Faibussowitsch     PetscCall(PetscSNPrintf(name, 64, "Field_%" PetscInt_FMT, f));
3829566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, (char **)&s->fieldNames[f]));
3839566063dSJacob Faibussowitsch     PetscCall(PetscSNPrintf(name, 64, "Component_0"));
3849566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(s->numFieldComponents[f], &s->compNames[f]));
3859566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, (char **)&s->compNames[f][0]));
386ea844a1aSMatthew Knepley   }
3873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
388ea844a1aSMatthew Knepley }
389ea844a1aSMatthew Knepley 
390ea844a1aSMatthew Knepley /*@C
391cab54364SBarry Smith   PetscSectionGetFieldName - Returns the name of a field in the `PetscSection`
392ea844a1aSMatthew Knepley 
393ea844a1aSMatthew Knepley   Not Collective
394ea844a1aSMatthew Knepley 
395ea844a1aSMatthew Knepley   Input Parameters:
396cab54364SBarry Smith + s     - the `PetscSection`
397ea844a1aSMatthew Knepley - field - the field number
398ea844a1aSMatthew Knepley 
399ea844a1aSMatthew Knepley   Output Parameter:
400ea844a1aSMatthew Knepley . fieldName - the field name
401ea844a1aSMatthew Knepley 
402ea844a1aSMatthew Knepley   Level: intermediate
403ea844a1aSMatthew Knepley 
404cab54364SBarry Smith   Note:
405cab54364SBarry Smith   Will error if the field number is out of range
406cab54364SBarry Smith 
407cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetFieldName()`, `PetscSectionSetNumFields()`, `PetscSectionGetNumFields()`
408ea844a1aSMatthew Knepley @*/
409d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldName(PetscSection s, PetscInt field, const char *fieldName[])
410d71ae5a4SJacob Faibussowitsch {
411ea844a1aSMatthew Knepley   PetscFunctionBegin;
412ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
413ea844a1aSMatthew Knepley   PetscValidPointer(fieldName, 3);
4142abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
415ea844a1aSMatthew Knepley   *fieldName = s->fieldNames[field];
4163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
417ea844a1aSMatthew Knepley }
418ea844a1aSMatthew Knepley 
419ea844a1aSMatthew Knepley /*@C
420cab54364SBarry Smith   PetscSectionSetFieldName - Sets the name of a field in the `PetscSection`
421ea844a1aSMatthew Knepley 
422ea844a1aSMatthew Knepley   Not Collective
423ea844a1aSMatthew Knepley 
424ea844a1aSMatthew Knepley   Input Parameters:
425cab54364SBarry Smith + s     - the `PetscSection`
426ea844a1aSMatthew Knepley . field - the field number
427ea844a1aSMatthew Knepley - fieldName - the field name
428ea844a1aSMatthew Knepley 
429ea844a1aSMatthew Knepley   Level: intermediate
430ea844a1aSMatthew Knepley 
431cab54364SBarry Smith   Note:
432cab54364SBarry Smith   Will error if the field number is out of range
433cab54364SBarry Smith 
434cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionGetFieldName()`, `PetscSectionSetNumFields()`, `PetscSectionGetNumFields()`
435ea844a1aSMatthew Knepley @*/
436d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFieldName(PetscSection s, PetscInt field, const char fieldName[])
437d71ae5a4SJacob Faibussowitsch {
438ea844a1aSMatthew Knepley   PetscFunctionBegin;
439ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
440ea844a1aSMatthew Knepley   if (fieldName) PetscValidCharPointer(fieldName, 3);
4412abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
4429566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->fieldNames[field]));
4439566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(fieldName, (char **)&s->fieldNames[field]));
4443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
445ea844a1aSMatthew Knepley }
446ea844a1aSMatthew Knepley 
447b778fa18SValeria Barra /*@C
448cab54364SBarry Smith   PetscSectionGetComponentName - Gets the name of a field component in the `PetscSection`
449b778fa18SValeria Barra 
450b778fa18SValeria Barra   Not Collective
451b778fa18SValeria Barra 
452b778fa18SValeria Barra   Input Parameters:
453cab54364SBarry Smith + s     - the `PetscSection`
454b778fa18SValeria Barra . field - the field number
455cab54364SBarry Smith - comp  - the component number
456cab54364SBarry Smith 
457d5b43468SJose E. Roman   Output Parameter:
458cab54364SBarry Smith . compName - the component name
459b778fa18SValeria Barra 
460b778fa18SValeria Barra   Level: intermediate
461b778fa18SValeria Barra 
462cab54364SBarry Smith   Note:
463cab54364SBarry Smith   Will error if the field or component number do not exist
464cab54364SBarry Smith 
465*583308b6SBarry Smith   Developer Note:
466*583308b6SBarry Smith   The function name should have Field in it since they are field components.
467*583308b6SBarry Smith 
468cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetFieldName()`, `PetscSectionSetNumFields()`, `PetscSectionGetNumFields()`,
469cab54364SBarry Smith           `PetscSectionSetComponentName()`, `PetscSectionSetFieldName()`, `PetscSectionGetFieldComponents()`, `PetscSectionSetFieldComponents()`
470b778fa18SValeria Barra @*/
471d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetComponentName(PetscSection s, PetscInt field, PetscInt comp, const char *compName[])
472d71ae5a4SJacob Faibussowitsch {
473b778fa18SValeria Barra   PetscFunctionBegin;
474b778fa18SValeria Barra   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
475064a246eSJacob Faibussowitsch   PetscValidPointer(compName, 4);
4762abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
4772abc8c78SJacob Faibussowitsch   PetscSectionCheckValidFieldComponent(comp, s->numFieldComponents[field]);
478b778fa18SValeria Barra   *compName = s->compNames[field][comp];
4793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
480b778fa18SValeria Barra }
481b778fa18SValeria Barra 
482b778fa18SValeria Barra /*@C
483cab54364SBarry Smith   PetscSectionSetComponentName - Sets the name of a field component in the `PetscSection`
484b778fa18SValeria Barra 
485b778fa18SValeria Barra   Not Collective
486b778fa18SValeria Barra 
487b778fa18SValeria Barra   Input Parameters:
48820662ed9SBarry Smith + s     - the `PetscSection`
489b778fa18SValeria Barra . field - the field number
490b778fa18SValeria Barra . comp  - the component number
491b778fa18SValeria Barra - compName - the component name
492b778fa18SValeria Barra 
493*583308b6SBarry Smith   Level: advanced
494b778fa18SValeria Barra 
495cab54364SBarry Smith   Note:
496cab54364SBarry Smith   Will error if the field or component number do not exist
497cab54364SBarry Smith 
498*583308b6SBarry Smith   Developer Note:
499*583308b6SBarry Smith   The function name should have Field in it since they are field components.
500*583308b6SBarry Smith 
501cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetComponentName()`, `PetscSectionSetNumFields()`, `PetscSectionGetNumFields()`,
502cab54364SBarry Smith           `PetscSectionSetComponentName()`, `PetscSectionSetFieldName()`, `PetscSectionGetFieldComponents()`, `PetscSectionSetFieldComponents()`
503b778fa18SValeria Barra @*/
504d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetComponentName(PetscSection s, PetscInt field, PetscInt comp, const char compName[])
505d71ae5a4SJacob Faibussowitsch {
506b778fa18SValeria Barra   PetscFunctionBegin;
507b778fa18SValeria Barra   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
508064a246eSJacob Faibussowitsch   if (compName) PetscValidCharPointer(compName, 4);
5092abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
5102abc8c78SJacob Faibussowitsch   PetscSectionCheckValidFieldComponent(comp, s->numFieldComponents[field]);
5119566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->compNames[field][comp]));
5129566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(compName, (char **)&s->compNames[field][comp]));
5133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
514b778fa18SValeria Barra }
515b778fa18SValeria Barra 
516ea844a1aSMatthew Knepley /*@
517ea844a1aSMatthew Knepley   PetscSectionGetFieldComponents - Returns the number of field components for the given field.
518ea844a1aSMatthew Knepley 
51940750d41SVaclav Hapla   Not Collective
520ea844a1aSMatthew Knepley 
521ea844a1aSMatthew Knepley   Input Parameters:
522cab54364SBarry Smith + s - the `PetscSection`
523ea844a1aSMatthew Knepley - field - the field number
524ea844a1aSMatthew Knepley 
525ea844a1aSMatthew Knepley   Output Parameter:
526ea844a1aSMatthew Knepley . numComp - the number of field components
527ea844a1aSMatthew Knepley 
528*583308b6SBarry Smith   Level: advanced
529ea844a1aSMatthew Knepley 
530cab54364SBarry Smith   Developer Note:
531cab54364SBarry Smith   This function is misnamed. There is a Num in `PetscSectionGetNumFields()` but not in this name
532cab54364SBarry Smith 
533*583308b6SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetFieldComponents()`, `PetscSectionGetNumFields()`,
534*583308b6SBarry Smith           `PetscSectionSetComponentName()`, `PetscSectionGetComponentName()`
535ea844a1aSMatthew Knepley @*/
536d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldComponents(PetscSection s, PetscInt field, PetscInt *numComp)
537d71ae5a4SJacob Faibussowitsch {
538ea844a1aSMatthew Knepley   PetscFunctionBegin;
539ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
5402abc8c78SJacob Faibussowitsch   PetscValidIntPointer(numComp, 3);
5412abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
542ea844a1aSMatthew Knepley   *numComp = s->numFieldComponents[field];
5433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
544ea844a1aSMatthew Knepley }
545ea844a1aSMatthew Knepley 
546ea844a1aSMatthew Knepley /*@
547ea844a1aSMatthew Knepley   PetscSectionSetFieldComponents - Sets the number of field components for the given field.
548ea844a1aSMatthew Knepley 
54940750d41SVaclav Hapla   Not Collective
550ea844a1aSMatthew Knepley 
551ea844a1aSMatthew Knepley   Input Parameters:
55220662ed9SBarry Smith + s - the `PetscSection`
553ea844a1aSMatthew Knepley . field - the field number
554ea844a1aSMatthew Knepley - numComp - the number of field components
555ea844a1aSMatthew Knepley 
556*583308b6SBarry Smith   Level: advanced
557ea844a1aSMatthew Knepley 
558*583308b6SBarry Smith   Note:
559*583308b6SBarry Smith   This number can be different than the values set with `PetscSectionSetFieldDof()`. It can be used to indicate the number of
560*583308b6SBarry Smith   components in the field of the underlying physical model which may be different than the number of degrees of freedom needed
561*583308b6SBarry Smith   at a point in a discretization. For example, if in three dimensions the field is velocity, it will have 3 components, u, v, and w but
562*583308b6SBarry Smith   an face based model for velocity (where the velocity normal to the face is stored) there is only 1 dof for each face point.
563*583308b6SBarry Smith 
564*583308b6SBarry Smith   The value set with this function are not needed or used in `PetscSectionSetUp()`.
565*583308b6SBarry Smith 
566*583308b6SBarry Smith   Developer Note:
567*583308b6SBarry Smith   This function is misnamed. There is a Num in `PetscSectionSetNumFields()` but not in this name
568*583308b6SBarry Smith 
569*583308b6SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetFieldComponents()`, `PetscSectionSetComponentName()`,
570*583308b6SBarry Smith           `PetscSectionGetComponentName()`, `PetscSectionGetNumFields()`
571ea844a1aSMatthew Knepley @*/
572d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFieldComponents(PetscSection s, PetscInt field, PetscInt numComp)
573d71ae5a4SJacob Faibussowitsch {
574b778fa18SValeria Barra   PetscInt c;
575b778fa18SValeria Barra 
576ea844a1aSMatthew Knepley   PetscFunctionBegin;
577ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
5782abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
579b778fa18SValeria Barra   if (s->compNames) {
58048a46eb9SPierre Jolivet     for (c = 0; c < s->numFieldComponents[field]; ++c) PetscCall(PetscFree(s->compNames[field][c]));
5819566063dSJacob Faibussowitsch     PetscCall(PetscFree(s->compNames[field]));
582b778fa18SValeria Barra   }
583b778fa18SValeria Barra 
584ea844a1aSMatthew Knepley   s->numFieldComponents[field] = numComp;
585b778fa18SValeria Barra   if (numComp) {
5869566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numComp, (char ***)&s->compNames[field]));
587b778fa18SValeria Barra     for (c = 0; c < numComp; ++c) {
5882abc8c78SJacob Faibussowitsch       char name[64];
5892abc8c78SJacob Faibussowitsch 
5909566063dSJacob Faibussowitsch       PetscCall(PetscSNPrintf(name, 64, "%" PetscInt_FMT, c));
5919566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(name, (char **)&s->compNames[field][c]));
592b778fa18SValeria Barra     }
593b778fa18SValeria Barra   }
5943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
595ea844a1aSMatthew Knepley }
596ea844a1aSMatthew Knepley 
597ea844a1aSMatthew Knepley /*@
598*583308b6SBarry Smith   PetscSectionGetChart - Returns the range [`pStart`, `pEnd`) in which points (indices) lie for this `PetscSection` on this MPI process
599ea844a1aSMatthew Knepley 
60040750d41SVaclav Hapla   Not Collective
601ea844a1aSMatthew Knepley 
602ea844a1aSMatthew Knepley   Input Parameter:
603cab54364SBarry Smith . s - the `PetscSection`
604ea844a1aSMatthew Knepley 
605ea844a1aSMatthew Knepley   Output Parameters:
606ea844a1aSMatthew Knepley + pStart - the first point
607ea844a1aSMatthew Knepley - pEnd - one past the last point
608ea844a1aSMatthew Knepley 
609ea844a1aSMatthew Knepley   Level: intermediate
610ea844a1aSMatthew Knepley 
611cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetChart()`, `PetscSectionCreate()`
612ea844a1aSMatthew Knepley @*/
613d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetChart(PetscSection s, PetscInt *pStart, PetscInt *pEnd)
614d71ae5a4SJacob Faibussowitsch {
615ea844a1aSMatthew Knepley   PetscFunctionBegin;
616ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
617ea844a1aSMatthew Knepley   if (pStart) *pStart = s->pStart;
618ea844a1aSMatthew Knepley   if (pEnd) *pEnd = s->pEnd;
6193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
620ea844a1aSMatthew Knepley }
621ea844a1aSMatthew Knepley 
622ea844a1aSMatthew Knepley /*@
623*583308b6SBarry Smith   PetscSectionSetChart - Sets the range [`pStart`, `pEnd`) in which points (indices) lie for this `PetscSection` on this MPI process
624ea844a1aSMatthew Knepley 
62540750d41SVaclav Hapla   Not Collective
626ea844a1aSMatthew Knepley 
627ea844a1aSMatthew Knepley   Input Parameters:
62820662ed9SBarry Smith + s - the `PetscSection`
629ea844a1aSMatthew Knepley . pStart - the first point
630ea844a1aSMatthew Knepley - pEnd - one past the last point
631ea844a1aSMatthew Knepley 
632ea844a1aSMatthew Knepley   Level: intermediate
633ea844a1aSMatthew Knepley 
634*583308b6SBarry Smith   Notes:
635*583308b6SBarry Smith   The charts on different MPI processes may (and often do) overlap
636*583308b6SBarry Smith 
637*583308b6SBarry Smith   If you intend to use `PetscSectionSetNumFields()` it must be called before this call.
638*583308b6SBarry Smith 
639*583308b6SBarry Smith   The chart for all fields created with `PetscSectionSetNumFields()` is the same as this chart.
640*583308b6SBarry Smith 
641*583308b6SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetChart()`, `PetscSectionCreate()`, `PetscSectionSetNumFields()`
642ea844a1aSMatthew Knepley @*/
643d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetChart(PetscSection s, PetscInt pStart, PetscInt pEnd)
644d71ae5a4SJacob Faibussowitsch {
645ea844a1aSMatthew Knepley   PetscInt f;
646ea844a1aSMatthew Knepley 
647ea844a1aSMatthew Knepley   PetscFunctionBegin;
648ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
6493ba16761SJacob Faibussowitsch   if (pStart == s->pStart && pEnd == s->pEnd) PetscFunctionReturn(PETSC_SUCCESS);
650ea844a1aSMatthew Knepley   /* Cannot Reset() because it destroys field information */
651ea844a1aSMatthew Knepley   s->setup = PETSC_FALSE;
6529566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&s->bc));
6539566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->bcIndices));
6549566063dSJacob Faibussowitsch   PetscCall(PetscFree2(s->atlasDof, s->atlasOff));
655ea844a1aSMatthew Knepley 
656ea844a1aSMatthew Knepley   s->pStart = pStart;
657ea844a1aSMatthew Knepley   s->pEnd   = pEnd;
6589566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2((pEnd - pStart), &s->atlasDof, (pEnd - pStart), &s->atlasOff));
6599566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(s->atlasDof, pEnd - pStart));
66048a46eb9SPierre Jolivet   for (f = 0; f < s->numFields; ++f) PetscCall(PetscSectionSetChart(s->field[f], pStart, pEnd));
6613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
662ea844a1aSMatthew Knepley }
663ea844a1aSMatthew Knepley 
664ea844a1aSMatthew Knepley /*@
66520662ed9SBarry Smith   PetscSectionGetPermutation - Returns the permutation of [0, `pEnd` - `pStart`) or `NULL` that was set with `PetscSectionSetPermutation()`
666ea844a1aSMatthew Knepley 
66740750d41SVaclav Hapla   Not Collective
668ea844a1aSMatthew Knepley 
669ea844a1aSMatthew Knepley   Input Parameter:
670cab54364SBarry Smith . s - the `PetscSection`
671ea844a1aSMatthew Knepley 
6722fe279fdSBarry Smith   Output Parameter:
673cab54364SBarry Smith . perm - The permutation as an `IS`
674ea844a1aSMatthew Knepley 
675ea844a1aSMatthew Knepley   Level: intermediate
676ea844a1aSMatthew Knepley 
677cab54364SBarry Smith .seealso: [](sec_scatter), `IS`, `PetscSection`, `PetscSectionSetPermutation()`, `PetscSectionCreate()`
678ea844a1aSMatthew Knepley @*/
679d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetPermutation(PetscSection s, IS *perm)
680d71ae5a4SJacob Faibussowitsch {
681ea844a1aSMatthew Knepley   PetscFunctionBegin;
682ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
6839371c9d4SSatish Balay   if (perm) {
6849371c9d4SSatish Balay     PetscValidPointer(perm, 2);
6859371c9d4SSatish Balay     *perm = s->perm;
6869371c9d4SSatish Balay   }
6873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
688ea844a1aSMatthew Knepley }
689ea844a1aSMatthew Knepley 
690ea844a1aSMatthew Knepley /*@
691*583308b6SBarry Smith   PetscSectionSetPermutation - Sets a permutation of the chart for this section, [0, `pEnd` - `pStart`), which determines the order to store the `PetscSection` information
692ea844a1aSMatthew Knepley 
69340750d41SVaclav Hapla   Not Collective
694ea844a1aSMatthew Knepley 
695ea844a1aSMatthew Knepley   Input Parameters:
69620662ed9SBarry Smith + s - the `PetscSection`
697ea844a1aSMatthew Knepley - perm - the permutation of points
698ea844a1aSMatthew Knepley 
699ea844a1aSMatthew Knepley   Level: intermediate
700ea844a1aSMatthew Knepley 
701*583308b6SBarry Smith   Notes:
702*583308b6SBarry Smith   The permutation must be provided before `PetscSectionSetUp()`.
703cab54364SBarry Smith 
704*583308b6SBarry Smith   The data in the `PetscSection` are permuted but the access via `PetscSectionGetFieldOffset()` and `PetscSectionGetOffset()` is not changed
705*583308b6SBarry Smith 
706*583308b6SBarry Smith   Compart to `PetscSectionPermute()`
707*583308b6SBarry Smith 
708*583308b6SBarry Smith .seealso: [](sec_scatter), `IS`, `PetscSection`, `PetscSectionSetUp()`, `PetscSectionGetPermutation()`, `PetscSectionPermute()`, `PetscSectionCreate()`
709ea844a1aSMatthew Knepley @*/
710d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetPermutation(PetscSection s, IS perm)
711d71ae5a4SJacob Faibussowitsch {
712ea844a1aSMatthew Knepley   PetscFunctionBegin;
713ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
714ea844a1aSMatthew Knepley   if (perm) PetscValidHeaderSpecific(perm, IS_CLASSID, 2);
71528b400f6SJacob Faibussowitsch   PetscCheck(!s->setup, PetscObjectComm((PetscObject)s), PETSC_ERR_ARG_WRONGSTATE, "Cannot set a permutation after the section is setup");
716ea844a1aSMatthew Knepley   if (s->perm != perm) {
7179566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&s->perm));
718ea844a1aSMatthew Knepley     if (perm) {
719ea844a1aSMatthew Knepley       s->perm = perm;
7209566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)s->perm));
721ea844a1aSMatthew Knepley     }
722ea844a1aSMatthew Knepley   }
7233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
724ea844a1aSMatthew Knepley }
725ea844a1aSMatthew Knepley 
726ea844a1aSMatthew Knepley /*@
727cab54364SBarry Smith   PetscSectionGetPointMajor - Returns the flag for dof ordering, `PETSC_TRUE` if it is point major, `PETSC_FALSE` if it is field major
728ea844a1aSMatthew Knepley 
72940750d41SVaclav Hapla   Not Collective
730ea844a1aSMatthew Knepley 
731ea844a1aSMatthew Knepley   Input Parameter:
732cab54364SBarry Smith . s - the `PetscSection`
733ea844a1aSMatthew Knepley 
734ea844a1aSMatthew Knepley   Output Parameter:
735ea844a1aSMatthew Knepley . pm - the flag for point major ordering
736ea844a1aSMatthew Knepley 
737ea844a1aSMatthew Knepley   Level: intermediate
738ea844a1aSMatthew Knepley 
739cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, PetscSectionSetPointMajor()`
740ea844a1aSMatthew Knepley @*/
741d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetPointMajor(PetscSection s, PetscBool *pm)
742d71ae5a4SJacob Faibussowitsch {
743ea844a1aSMatthew Knepley   PetscFunctionBegin;
744ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
745dadcf809SJacob Faibussowitsch   PetscValidBoolPointer(pm, 2);
746ea844a1aSMatthew Knepley   *pm = s->pointMajor;
7473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
748ea844a1aSMatthew Knepley }
749ea844a1aSMatthew Knepley 
750ea844a1aSMatthew Knepley /*@
751cab54364SBarry Smith   PetscSectionSetPointMajor - Sets the flag for dof ordering, `PETSC_TRUE` for point major, otherwise it will be field major
752ea844a1aSMatthew Knepley 
75340750d41SVaclav Hapla   Not Collective
754ea844a1aSMatthew Knepley 
755ea844a1aSMatthew Knepley   Input Parameters:
756cab54364SBarry Smith + s  - the `PetscSection`
757ea844a1aSMatthew Knepley - pm - the flag for point major ordering
758ea844a1aSMatthew Knepley 
759ea844a1aSMatthew Knepley   Level: intermediate
760ea844a1aSMatthew Knepley 
761*583308b6SBarry Smith   Note:
762*583308b6SBarry Smith   Field-major order is not recommended unless you are managing the entire problem yourself, since many higher-level functions in PETSc depend on point-major order.
763*583308b6SBarry Smith 
764*583308b6SBarry Smith   Point major order means the degrees of freedom are stored as follows
765*583308b6SBarry Smith .vb
766*583308b6SBarry Smith     all the degrees of freedom for each point are stored contiquously, one point after another (respecting a permutation set with `PetscSectionSetPermutation()`)
767*583308b6SBarry Smith     for each point
768*583308b6SBarry Smith        the degrees of freedom for each field (starting with the unnamed default field) are listed in order by field
769*583308b6SBarry Smith .ve
770*583308b6SBarry Smith 
771*583308b6SBarry Smith   Field major order means the degrees of freedom are stored as follows
772*583308b6SBarry Smith .vb
773*583308b6SBarry Smith     all degrees of freedom for each field (including the unnamed default field) are stored contiquously, one field after another
774*583308b6SBarry Smith     for each field (started with unnamed default field)
775*583308b6SBarry Smith       the degrees of freedom for each point are listed in order by point (respecting a permutation set with `PetscSectionSetPermutation()`)
776*583308b6SBarry Smith .ve
777*583308b6SBarry Smith 
778*583308b6SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetPointMajor()`, `PetscSectionSetPermutation()`
779ea844a1aSMatthew Knepley @*/
780d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetPointMajor(PetscSection s, PetscBool pm)
781d71ae5a4SJacob Faibussowitsch {
782ea844a1aSMatthew Knepley   PetscFunctionBegin;
783ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
78428b400f6SJacob Faibussowitsch   PetscCheck(!s->setup, PetscObjectComm((PetscObject)s), PETSC_ERR_ARG_WRONGSTATE, "Cannot set the dof ordering after the section is setup");
785ea844a1aSMatthew Knepley   s->pointMajor = pm;
7863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
787ea844a1aSMatthew Knepley }
788ea844a1aSMatthew Knepley 
789ea844a1aSMatthew Knepley /*@
790cab54364SBarry Smith   PetscSectionGetIncludesConstraints - Returns the flag indicating if constrained dofs were included when computing offsets in the `PetscSection`.
791cab54364SBarry Smith   The value is set with `PetscSectionSetIncludesConstraints()`
79287e637c6Sksagiyam 
79340750d41SVaclav Hapla   Not Collective
79487e637c6Sksagiyam 
79587e637c6Sksagiyam   Input Parameter:
796cab54364SBarry Smith . s - the `PetscSection`
79787e637c6Sksagiyam 
79887e637c6Sksagiyam   Output Parameter:
79987e637c6Sksagiyam . includesConstraints - the flag indicating if constrained dofs were included when computing offsets
80087e637c6Sksagiyam 
80187e637c6Sksagiyam   Level: intermediate
80287e637c6Sksagiyam 
803cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetIncludesConstraints()`
80487e637c6Sksagiyam @*/
805d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetIncludesConstraints(PetscSection s, PetscBool *includesConstraints)
806d71ae5a4SJacob Faibussowitsch {
80787e637c6Sksagiyam   PetscFunctionBegin;
80887e637c6Sksagiyam   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
80987e637c6Sksagiyam   PetscValidBoolPointer(includesConstraints, 2);
81087e637c6Sksagiyam   *includesConstraints = s->includesConstraints;
8113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81287e637c6Sksagiyam }
81387e637c6Sksagiyam 
81487e637c6Sksagiyam /*@
81587e637c6Sksagiyam   PetscSectionSetIncludesConstraints - Sets the flag indicating if constrained dofs are to be included when computing offsets
81687e637c6Sksagiyam 
81740750d41SVaclav Hapla   Not Collective
81887e637c6Sksagiyam 
81987e637c6Sksagiyam   Input Parameters:
82020662ed9SBarry Smith + s  - the `PetscSection`
82187e637c6Sksagiyam - includesConstraints - the flag indicating if constrained dofs are to be included when computing offsets
82287e637c6Sksagiyam 
82387e637c6Sksagiyam   Level: intermediate
82487e637c6Sksagiyam 
825cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetIncludesConstraints()`
82687e637c6Sksagiyam @*/
827d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetIncludesConstraints(PetscSection s, PetscBool includesConstraints)
828d71ae5a4SJacob Faibussowitsch {
82987e637c6Sksagiyam   PetscFunctionBegin;
83087e637c6Sksagiyam   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
83128b400f6SJacob Faibussowitsch   PetscCheck(!s->setup, PetscObjectComm((PetscObject)s), PETSC_ERR_ARG_WRONGSTATE, "Cannot set includesConstraints after the section is set up");
83287e637c6Sksagiyam   s->includesConstraints = includesConstraints;
8333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
83487e637c6Sksagiyam }
83587e637c6Sksagiyam 
83687e637c6Sksagiyam /*@
837*583308b6SBarry Smith   PetscSectionGetDof - Return the total number of degrees of freedom associated with a given point.
838ea844a1aSMatthew Knepley 
83940750d41SVaclav Hapla   Not Collective
840ea844a1aSMatthew Knepley 
841ea844a1aSMatthew Knepley   Input Parameters:
842cab54364SBarry Smith + s - the `PetscSection`
843ea844a1aSMatthew Knepley - point - the point
844ea844a1aSMatthew Knepley 
845ea844a1aSMatthew Knepley   Output Parameter:
846ea844a1aSMatthew Knepley . numDof - the number of dof
847ea844a1aSMatthew Knepley 
848*583308b6SBarry Smith   Level: intermediate
849*583308b6SBarry Smith 
850*583308b6SBarry Smith   Notes:
851db527f05SMatthew G. Knepley   In a global section, this size will be negative for points not owned by this process.
852db527f05SMatthew G. Knepley 
853*583308b6SBarry Smith   This number is for the unnamed default field at the given point plus all degrees of freedom associated with all fields at that point
854ea844a1aSMatthew Knepley 
855cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetDof()`, `PetscSectionCreate()`
856ea844a1aSMatthew Knepley @*/
857d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetDof(PetscSection s, PetscInt point, PetscInt *numDof)
858d71ae5a4SJacob Faibussowitsch {
85976bd3646SJed Brown   PetscFunctionBeginHot;
860ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
861dadcf809SJacob Faibussowitsch   PetscValidIntPointer(numDof, 3);
862c8bf3acbSVaclav 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);
863ea844a1aSMatthew Knepley   *numDof = s->atlasDof[point - s->pStart];
8643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
865ea844a1aSMatthew Knepley }
866ea844a1aSMatthew Knepley 
867ea844a1aSMatthew Knepley /*@
868*583308b6SBarry Smith   PetscSectionSetDof - Sets the total number of degrees of freedom associated with a given point.
869ea844a1aSMatthew Knepley 
87040750d41SVaclav Hapla   Not Collective
871ea844a1aSMatthew Knepley 
872ea844a1aSMatthew Knepley   Input Parameters:
873cab54364SBarry Smith + s - the `PetscSection`
874ea844a1aSMatthew Knepley . point - the point
875ea844a1aSMatthew Knepley - numDof - the number of dof
876ea844a1aSMatthew Knepley 
877ea844a1aSMatthew Knepley   Level: intermediate
878ea844a1aSMatthew Knepley 
879*583308b6SBarry Smith   Note:
880*583308b6SBarry Smith   This number is for the unnamed default field at the given point plus all degrees of freedom associated with all fields at that point
881*583308b6SBarry Smith 
882cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetDof()`, `PetscSectionAddDof()`, `PetscSectionCreate()`
883ea844a1aSMatthew Knepley @*/
884d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetDof(PetscSection s, PetscInt point, PetscInt numDof)
885d71ae5a4SJacob Faibussowitsch {
886ea844a1aSMatthew Knepley   PetscFunctionBegin;
887ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
888c8bf3acbSVaclav 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);
889ea844a1aSMatthew Knepley   s->atlasDof[point - s->pStart] = numDof;
89069c11d05SVaclav Hapla   PetscCall(PetscSectionInvalidateMaxDof_Internal(s));
8913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
892ea844a1aSMatthew Knepley }
893ea844a1aSMatthew Knepley 
894ea844a1aSMatthew Knepley /*@
895*583308b6SBarry Smith   PetscSectionAddDof - Adds to the total number of degrees of freedom associated with a given point.
896ea844a1aSMatthew Knepley 
89740750d41SVaclav Hapla   Not Collective
898ea844a1aSMatthew Knepley 
899ea844a1aSMatthew Knepley   Input Parameters:
900cab54364SBarry Smith + s - the `PetscSection`
901ea844a1aSMatthew Knepley . point - the point
902ea844a1aSMatthew Knepley - numDof - the number of additional dof
903ea844a1aSMatthew Knepley 
904ea844a1aSMatthew Knepley   Level: intermediate
905ea844a1aSMatthew Knepley 
906*583308b6SBarry Smith   Note:
907*583308b6SBarry Smith   This number is for the unnamed default field at the given point plus all degrees of freedom associated with all fields at that point
908*583308b6SBarry Smith 
909cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetDof()`, `PetscSectionSetDof()`, `PetscSectionCreate()`
910ea844a1aSMatthew Knepley @*/
911d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionAddDof(PetscSection s, PetscInt point, PetscInt numDof)
912d71ae5a4SJacob Faibussowitsch {
91376bd3646SJed Brown   PetscFunctionBeginHot;
914ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
915c8bf3acbSVaclav 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);
916ea844a1aSMatthew Knepley   s->atlasDof[point - s->pStart] += numDof;
91769c11d05SVaclav Hapla   PetscCall(PetscSectionInvalidateMaxDof_Internal(s));
9183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
919ea844a1aSMatthew Knepley }
920ea844a1aSMatthew Knepley 
921ea844a1aSMatthew Knepley /*@
922ea844a1aSMatthew Knepley   PetscSectionGetFieldDof - Return the number of degrees of freedom associated with a field on a given point.
923ea844a1aSMatthew Knepley 
92440750d41SVaclav Hapla   Not Collective
925ea844a1aSMatthew Knepley 
926ea844a1aSMatthew Knepley   Input Parameters:
927cab54364SBarry Smith + s - the `PetscSection`
928ea844a1aSMatthew Knepley . point - the point
929ea844a1aSMatthew Knepley - field - the field
930ea844a1aSMatthew Knepley 
931ea844a1aSMatthew Knepley   Output Parameter:
932ea844a1aSMatthew Knepley . numDof - the number of dof
933ea844a1aSMatthew Knepley 
934ea844a1aSMatthew Knepley   Level: intermediate
935ea844a1aSMatthew Knepley 
936cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetFieldDof()`, `PetscSectionCreate()`
937ea844a1aSMatthew Knepley @*/
938d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldDof(PetscSection s, PetscInt point, PetscInt field, PetscInt *numDof)
939d71ae5a4SJacob Faibussowitsch {
940ea844a1aSMatthew Knepley   PetscFunctionBegin;
941ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
9422abc8c78SJacob Faibussowitsch   PetscValidIntPointer(numDof, 4);
9432abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
9449566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetDof(s->field[field], point, numDof));
9453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
946ea844a1aSMatthew Knepley }
947ea844a1aSMatthew Knepley 
948ea844a1aSMatthew Knepley /*@
949ea844a1aSMatthew Knepley   PetscSectionSetFieldDof - Sets the number of degrees of freedom associated with a field on a given point.
950ea844a1aSMatthew Knepley 
95140750d41SVaclav Hapla   Not Collective
952ea844a1aSMatthew Knepley 
953ea844a1aSMatthew Knepley   Input Parameters:
954cab54364SBarry Smith + s - the `PetscSection`
955ea844a1aSMatthew Knepley . point - the point
956ea844a1aSMatthew Knepley . field - the field
957ea844a1aSMatthew Knepley - numDof - the number of dof
958ea844a1aSMatthew Knepley 
959ea844a1aSMatthew Knepley   Level: intermediate
960ea844a1aSMatthew Knepley 
961*583308b6SBarry Smith   Note:
962*583308b6SBarry Smith   When setting the number of dof for a field at a point one must also ensure the count of the total number of dof at the point (summed over
963*583308b6SBarry Smith   the fields and the unnamed default field) is correct by also calling `PetscSectionAddDof()` or `PetscSectionSetDof()`
964*583308b6SBarry Smith 
965*583308b6SBarry Smith   This is equivalent to
966*583308b6SBarry Smith .vb
967*583308b6SBarry Smith      PetscSection fs;
968*583308b6SBarry Smith      PetscSectionGetField(s,field,&fs)
969*583308b6SBarry Smith      PetscSectionSetDof(fs,numDof)
970*583308b6SBarry Smith .ve
971*583308b6SBarry Smith 
972*583308b6SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetFieldDof()`, `PetscSectionCreate()`, `PetscSectionAddDof()`, `PetscSectionSetDof()`
973ea844a1aSMatthew Knepley @*/
974d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFieldDof(PetscSection s, PetscInt point, PetscInt field, PetscInt numDof)
975d71ae5a4SJacob Faibussowitsch {
976ea844a1aSMatthew Knepley   PetscFunctionBegin;
977ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
9782abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
9799566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetDof(s->field[field], point, numDof));
9803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
981ea844a1aSMatthew Knepley }
982ea844a1aSMatthew Knepley 
983ea844a1aSMatthew Knepley /*@
984ea844a1aSMatthew Knepley   PetscSectionAddFieldDof - Adds a number of degrees of freedom associated with a field on a given point.
985ea844a1aSMatthew Knepley 
98640750d41SVaclav Hapla   Not Collective
987ea844a1aSMatthew Knepley 
988ea844a1aSMatthew Knepley   Input Parameters:
989cab54364SBarry Smith + s - the `PetscSection`
990ea844a1aSMatthew Knepley . point - the point
991ea844a1aSMatthew Knepley . field - the field
992ea844a1aSMatthew Knepley - numDof - the number of dof
993ea844a1aSMatthew Knepley 
994ea844a1aSMatthew Knepley   Level: intermediate
995ea844a1aSMatthew Knepley 
996*583308b6SBarry Smith   Notes:
997*583308b6SBarry Smith   When adding to the number of dof for a field at a point one must also ensure the count of the total number of dof at the point (summed over
998*583308b6SBarry Smith   the fields and the unnamed default field) is correct by also calling `PetscSectionAddDof()` or `PetscSectionSetDof()`
999*583308b6SBarry Smith 
1000*583308b6SBarry Smith   This is equivalent to
1001*583308b6SBarry Smith .vb
1002*583308b6SBarry Smith      PetscSection fs;
1003*583308b6SBarry Smith      PetscSectionGetField(s,field,&fs)
1004*583308b6SBarry Smith      PetscSectionAddDof(fs,numDof)
1005*583308b6SBarry Smith .ve
1006*583308b6SBarry Smith 
1007cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetFieldDof()`, `PetscSectionGetFieldDof()`, `PetscSectionCreate()`
1008ea844a1aSMatthew Knepley @*/
1009d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionAddFieldDof(PetscSection s, PetscInt point, PetscInt field, PetscInt numDof)
1010d71ae5a4SJacob Faibussowitsch {
1011ea844a1aSMatthew Knepley   PetscFunctionBegin;
1012ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
10132abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
10149566063dSJacob Faibussowitsch   PetscCall(PetscSectionAddDof(s->field[field], point, numDof));
10153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1016ea844a1aSMatthew Knepley }
1017ea844a1aSMatthew Knepley 
1018ea844a1aSMatthew Knepley /*@
1019ea844a1aSMatthew Knepley   PetscSectionGetConstraintDof - Return the number of constrained degrees of freedom associated with a given point.
1020ea844a1aSMatthew Knepley 
102140750d41SVaclav Hapla   Not Collective
1022ea844a1aSMatthew Knepley 
1023ea844a1aSMatthew Knepley   Input Parameters:
1024cab54364SBarry Smith + s - the `PetscSection`
1025ea844a1aSMatthew Knepley - point - the point
1026ea844a1aSMatthew Knepley 
1027ea844a1aSMatthew Knepley   Output Parameter:
1028ea844a1aSMatthew Knepley . numDof - the number of dof which are fixed by constraints
1029ea844a1aSMatthew Knepley 
1030ea844a1aSMatthew Knepley   Level: intermediate
1031ea844a1aSMatthew Knepley 
1032cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetDof()`, `PetscSectionSetConstraintDof()`, `PetscSectionCreate()`
1033ea844a1aSMatthew Knepley @*/
1034d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetConstraintDof(PetscSection s, PetscInt point, PetscInt *numDof)
1035d71ae5a4SJacob Faibussowitsch {
1036ea844a1aSMatthew Knepley   PetscFunctionBegin;
1037ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1038dadcf809SJacob Faibussowitsch   PetscValidIntPointer(numDof, 3);
1039ea844a1aSMatthew Knepley   if (s->bc) {
10409566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s->bc, point, numDof));
1041ea844a1aSMatthew Knepley   } else *numDof = 0;
10423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1043ea844a1aSMatthew Knepley }
1044ea844a1aSMatthew Knepley 
1045ea844a1aSMatthew Knepley /*@
1046ea844a1aSMatthew Knepley   PetscSectionSetConstraintDof - Set the number of constrained degrees of freedom associated with a given point.
1047ea844a1aSMatthew Knepley 
104840750d41SVaclav Hapla   Not Collective
1049ea844a1aSMatthew Knepley 
1050ea844a1aSMatthew Knepley   Input Parameters:
1051cab54364SBarry Smith + s - the `PetscSection`
1052ea844a1aSMatthew Knepley . point - the point
1053ea844a1aSMatthew Knepley - numDof - the number of dof which are fixed by constraints
1054ea844a1aSMatthew Knepley 
1055ea844a1aSMatthew Knepley   Level: intermediate
1056ea844a1aSMatthew Knepley 
1057cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetDof()`, `PetscSectionGetConstraintDof()`, `PetscSectionCreate()`
1058ea844a1aSMatthew Knepley @*/
1059d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetConstraintDof(PetscSection s, PetscInt point, PetscInt numDof)
1060d71ae5a4SJacob Faibussowitsch {
1061ea844a1aSMatthew Knepley   PetscFunctionBegin;
1062ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1063ea844a1aSMatthew Knepley   if (numDof) {
10647c0883d5SVaclav Hapla     PetscCall(PetscSectionCheckConstraints_Private(s));
10659566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(s->bc, point, numDof));
1066ea844a1aSMatthew Knepley   }
10673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1068ea844a1aSMatthew Knepley }
1069ea844a1aSMatthew Knepley 
1070ea844a1aSMatthew Knepley /*@
1071ea844a1aSMatthew Knepley   PetscSectionAddConstraintDof - Increment the number of constrained degrees of freedom associated with a given point.
1072ea844a1aSMatthew Knepley 
107340750d41SVaclav Hapla   Not Collective
1074ea844a1aSMatthew Knepley 
1075ea844a1aSMatthew Knepley   Input Parameters:
1076cab54364SBarry Smith + s - the `PetscSection`
1077ea844a1aSMatthew Knepley . point - the point
1078ea844a1aSMatthew Knepley - numDof - the number of additional dof which are fixed by constraints
1079ea844a1aSMatthew Knepley 
1080ea844a1aSMatthew Knepley   Level: intermediate
1081ea844a1aSMatthew Knepley 
1082cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionAddDof()`, `PetscSectionGetConstraintDof()`, `PetscSectionCreate()`
1083ea844a1aSMatthew Knepley @*/
1084d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionAddConstraintDof(PetscSection s, PetscInt point, PetscInt numDof)
1085d71ae5a4SJacob Faibussowitsch {
1086ea844a1aSMatthew Knepley   PetscFunctionBegin;
1087ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1088ea844a1aSMatthew Knepley   if (numDof) {
10897c0883d5SVaclav Hapla     PetscCall(PetscSectionCheckConstraints_Private(s));
10909566063dSJacob Faibussowitsch     PetscCall(PetscSectionAddDof(s->bc, point, numDof));
1091ea844a1aSMatthew Knepley   }
10923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1093ea844a1aSMatthew Knepley }
1094ea844a1aSMatthew Knepley 
1095ea844a1aSMatthew Knepley /*@
1096ea844a1aSMatthew Knepley   PetscSectionGetFieldConstraintDof - Return the number of constrained degrees of freedom associated with a given field on a point.
1097ea844a1aSMatthew Knepley 
109840750d41SVaclav Hapla   Not Collective
1099ea844a1aSMatthew Knepley 
1100ea844a1aSMatthew Knepley   Input Parameters:
1101cab54364SBarry Smith + s - the `PetscSection`
1102ea844a1aSMatthew Knepley . point - the point
1103ea844a1aSMatthew Knepley - field - the field
1104ea844a1aSMatthew Knepley 
1105ea844a1aSMatthew Knepley   Output Parameter:
1106ea844a1aSMatthew Knepley . numDof - the number of dof which are fixed by constraints
1107ea844a1aSMatthew Knepley 
1108ea844a1aSMatthew Knepley   Level: intermediate
1109ea844a1aSMatthew Knepley 
1110cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetDof()`, `PetscSectionSetFieldConstraintDof()`, `PetscSectionCreate()`
1111ea844a1aSMatthew Knepley @*/
1112d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldConstraintDof(PetscSection s, PetscInt point, PetscInt field, PetscInt *numDof)
1113d71ae5a4SJacob Faibussowitsch {
1114ea844a1aSMatthew Knepley   PetscFunctionBegin;
1115ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
11162abc8c78SJacob Faibussowitsch   PetscValidIntPointer(numDof, 4);
11172abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
11189566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstraintDof(s->field[field], point, numDof));
11193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1120ea844a1aSMatthew Knepley }
1121ea844a1aSMatthew Knepley 
1122ea844a1aSMatthew Knepley /*@
1123ea844a1aSMatthew Knepley   PetscSectionSetFieldConstraintDof - Set the number of constrained degrees of freedom associated with a given field on a point.
1124ea844a1aSMatthew Knepley 
112540750d41SVaclav Hapla   Not Collective
1126ea844a1aSMatthew Knepley 
1127ea844a1aSMatthew Knepley   Input Parameters:
1128cab54364SBarry Smith + s - the `PetscSection`
1129ea844a1aSMatthew Knepley . point - the point
1130ea844a1aSMatthew Knepley . field - the field
1131ea844a1aSMatthew Knepley - numDof - the number of dof which are fixed by constraints
1132ea844a1aSMatthew Knepley 
1133ea844a1aSMatthew Knepley   Level: intermediate
1134ea844a1aSMatthew Knepley 
1135cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetDof()`, `PetscSectionGetFieldConstraintDof()`, `PetscSectionCreate()`
1136ea844a1aSMatthew Knepley @*/
1137d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFieldConstraintDof(PetscSection s, PetscInt point, PetscInt field, PetscInt numDof)
1138d71ae5a4SJacob Faibussowitsch {
1139ea844a1aSMatthew Knepley   PetscFunctionBegin;
1140ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
11412abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
11429566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetConstraintDof(s->field[field], point, numDof));
11433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1144ea844a1aSMatthew Knepley }
1145ea844a1aSMatthew Knepley 
1146ea844a1aSMatthew Knepley /*@
1147ea844a1aSMatthew Knepley   PetscSectionAddFieldConstraintDof - Increment the number of constrained degrees of freedom associated with a given field on a point.
1148ea844a1aSMatthew Knepley 
114940750d41SVaclav Hapla   Not Collective
1150ea844a1aSMatthew Knepley 
1151ea844a1aSMatthew Knepley   Input Parameters:
1152cab54364SBarry Smith + s - the `PetscSection`
1153ea844a1aSMatthew Knepley . point - the point
1154ea844a1aSMatthew Knepley . field - the field
1155ea844a1aSMatthew Knepley - numDof - the number of additional dof which are fixed by constraints
1156ea844a1aSMatthew Knepley 
1157ea844a1aSMatthew Knepley   Level: intermediate
1158ea844a1aSMatthew Knepley 
1159cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionAddDof()`, `PetscSectionGetFieldConstraintDof()`, `PetscSectionCreate()`
1160ea844a1aSMatthew Knepley @*/
1161d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionAddFieldConstraintDof(PetscSection s, PetscInt point, PetscInt field, PetscInt numDof)
1162d71ae5a4SJacob Faibussowitsch {
1163ea844a1aSMatthew Knepley   PetscFunctionBegin;
1164ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
11652abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
11669566063dSJacob Faibussowitsch   PetscCall(PetscSectionAddConstraintDof(s->field[field], point, numDof));
11673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1168ea844a1aSMatthew Knepley }
1169ea844a1aSMatthew Knepley 
1170ea844a1aSMatthew Knepley /*@
1171ea844a1aSMatthew Knepley   PetscSectionSetUpBC - Setup the subsections describing boundary conditions.
1172ea844a1aSMatthew Knepley 
117340750d41SVaclav Hapla   Not Collective
1174ea844a1aSMatthew Knepley 
1175ea844a1aSMatthew Knepley   Input Parameter:
1176cab54364SBarry Smith . s - the `PetscSection`
1177ea844a1aSMatthew Knepley 
1178ea844a1aSMatthew Knepley   Level: advanced
1179ea844a1aSMatthew Knepley 
1180cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetUp()`, `PetscSectionCreate()`
1181ea844a1aSMatthew Knepley @*/
1182d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetUpBC(PetscSection s)
1183d71ae5a4SJacob Faibussowitsch {
1184ea844a1aSMatthew Knepley   PetscFunctionBegin;
1185ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1186ea844a1aSMatthew Knepley   if (s->bc) {
1187ea844a1aSMatthew Knepley     const PetscInt last = (s->bc->pEnd - s->bc->pStart) - 1;
1188ea844a1aSMatthew Knepley 
11899566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(s->bc));
11908da24d32SBarry Smith     PetscCall(PetscMalloc1((last >= 0 ? s->bc->atlasOff[last] + s->bc->atlasDof[last] : 0), &s->bcIndices));
1191ea844a1aSMatthew Knepley   }
11923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1193ea844a1aSMatthew Knepley }
1194ea844a1aSMatthew Knepley 
1195ea844a1aSMatthew Knepley /*@
1196*583308b6SBarry Smith   PetscSectionSetUp - Calculate offsets based upon the number of degrees of freedom for each point in preparation for use of the `PetscSection`
1197ea844a1aSMatthew Knepley 
119840750d41SVaclav Hapla   Not Collective
1199ea844a1aSMatthew Knepley 
1200ea844a1aSMatthew Knepley   Input Parameter:
1201cab54364SBarry Smith . s - the `PetscSection`
1202ea844a1aSMatthew Knepley 
1203ea844a1aSMatthew Knepley   Level: intermediate
1204ea844a1aSMatthew Knepley 
1205*583308b6SBarry Smith   Notes:
1206*583308b6SBarry Smith   If used, `PetscSectionSetPermutation()` must be called before this routine.
1207*583308b6SBarry Smith 
1208*583308b6SBarry Smith   `PetscSectionSetPointMajor()`, cannot be called after this routine.
1209*583308b6SBarry Smith 
1210*583308b6SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionSetPermutation()`
1211ea844a1aSMatthew Knepley @*/
1212d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetUp(PetscSection s)
1213d71ae5a4SJacob Faibussowitsch {
1214ea844a1aSMatthew Knepley   const PetscInt *pind   = NULL;
1215ea844a1aSMatthew Knepley   PetscInt        offset = 0, foff, p, f;
1216ea844a1aSMatthew Knepley 
1217ea844a1aSMatthew Knepley   PetscFunctionBegin;
1218ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
12193ba16761SJacob Faibussowitsch   if (s->setup) PetscFunctionReturn(PETSC_SUCCESS);
1220ea844a1aSMatthew Knepley   s->setup = PETSC_TRUE;
1221ea844a1aSMatthew Knepley   /* Set offsets and field offsets for all points */
1222ea844a1aSMatthew Knepley   /*   Assume that all fields have the same chart */
122328b400f6SJacob Faibussowitsch   PetscCheck(s->includesConstraints, PETSC_COMM_SELF, PETSC_ERR_SUP, "PetscSectionSetUp is currently unsupported for includesConstraints = PETSC_TRUE");
12249566063dSJacob Faibussowitsch   if (s->perm) PetscCall(ISGetIndices(s->perm, &pind));
1225ea844a1aSMatthew Knepley   if (s->pointMajor) {
1226ea844a1aSMatthew Knepley     for (p = 0; p < s->pEnd - s->pStart; ++p) {
1227ea844a1aSMatthew Knepley       const PetscInt q = pind ? pind[p] : p;
1228ea844a1aSMatthew Knepley 
1229ea844a1aSMatthew Knepley       /* Set point offset */
1230ea844a1aSMatthew Knepley       s->atlasOff[q] = offset;
1231ea844a1aSMatthew Knepley       offset += s->atlasDof[q];
1232ea844a1aSMatthew Knepley       /* Set field offset */
1233ea844a1aSMatthew Knepley       for (f = 0, foff = s->atlasOff[q]; f < s->numFields; ++f) {
1234ea844a1aSMatthew Knepley         PetscSection sf = s->field[f];
1235ea844a1aSMatthew Knepley 
1236ea844a1aSMatthew Knepley         sf->atlasOff[q] = foff;
1237ea844a1aSMatthew Knepley         foff += sf->atlasDof[q];
1238ea844a1aSMatthew Knepley       }
1239ea844a1aSMatthew Knepley     }
1240ea844a1aSMatthew Knepley   } else {
1241ea844a1aSMatthew Knepley     /* Set field offsets for all points */
1242ea844a1aSMatthew Knepley     for (f = 0; f < s->numFields; ++f) {
1243ea844a1aSMatthew Knepley       PetscSection sf = s->field[f];
1244ea844a1aSMatthew Knepley 
1245ea844a1aSMatthew Knepley       for (p = 0; p < s->pEnd - s->pStart; ++p) {
1246ea844a1aSMatthew Knepley         const PetscInt q = pind ? pind[p] : p;
1247ea844a1aSMatthew Knepley 
1248ea844a1aSMatthew Knepley         sf->atlasOff[q] = offset;
1249ea844a1aSMatthew Knepley         offset += sf->atlasDof[q];
1250ea844a1aSMatthew Knepley       }
1251ea844a1aSMatthew Knepley     }
1252ea844a1aSMatthew Knepley     /* Disable point offsets since these are unused */
1253ad540459SPierre Jolivet     for (p = 0; p < s->pEnd - s->pStart; ++p) s->atlasOff[p] = -1;
1254ea844a1aSMatthew Knepley   }
12559566063dSJacob Faibussowitsch   if (s->perm) PetscCall(ISRestoreIndices(s->perm, &pind));
1256ea844a1aSMatthew Knepley   /* Setup BC sections */
12579566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUpBC(s));
12589566063dSJacob Faibussowitsch   for (f = 0; f < s->numFields; ++f) PetscCall(PetscSectionSetUpBC(s->field[f]));
12593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1260ea844a1aSMatthew Knepley }
1261ea844a1aSMatthew Knepley 
1262ea844a1aSMatthew Knepley /*@
126320662ed9SBarry Smith   PetscSectionGetMaxDof - Return the maximum number of degrees of freedom on any point in the `PetscSection`
1264ea844a1aSMatthew Knepley 
126540750d41SVaclav Hapla   Not Collective
1266ea844a1aSMatthew Knepley 
12672fe279fdSBarry Smith   Input Parameter:
1268cab54364SBarry Smith . s - the `PetscSection`
1269ea844a1aSMatthew Knepley 
1270ea844a1aSMatthew Knepley   Output Parameter:
1271ea844a1aSMatthew Knepley . maxDof - the maximum dof
1272ea844a1aSMatthew Knepley 
1273ea844a1aSMatthew Knepley   Level: intermediate
1274ea844a1aSMatthew Knepley 
1275*583308b6SBarry Smith   Notes:
1276cab54364SBarry Smith   The returned number is up-to-date without need for `PetscSectionSetUp()`.
127769c11d05SVaclav Hapla 
1278*583308b6SBarry Smith   This is the maximum over all points of the sum of the number of dof in the unnamed default field plus all named fields. This is equivalent to
1279*583308b6SBarry Smith   the maximum over all points of the value returned by `PetscSectionGetDof()` on this MPI process
1280*583308b6SBarry Smith 
1281*583308b6SBarry Smith   Developer Notes:
128269c11d05SVaclav Hapla   The returned number is calculated lazily and stashed.
1283cab54364SBarry Smith 
1284cab54364SBarry Smith   A call to `PetscSectionInvalidateMaxDof_Internal()` invalidates the stashed value.
1285cab54364SBarry Smith 
1286cab54364SBarry Smith   `PetscSectionInvalidateMaxDof_Internal()` is called in `PetscSectionSetDof()`, `PetscSectionAddDof()` and `PetscSectionReset()`
1287cab54364SBarry Smith 
128820662ed9SBarry Smith   It should also be called every time `atlasDof` is modified directly.
128969c11d05SVaclav Hapla 
1290cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetDof()`, `PetscSectionSetDof()`, `PetscSectionAddDof()`, `PetscSectionCreate()`
1291ea844a1aSMatthew Knepley @*/
1292d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetMaxDof(PetscSection s, PetscInt *maxDof)
1293d71ae5a4SJacob Faibussowitsch {
129469c11d05SVaclav Hapla   PetscInt p;
129569c11d05SVaclav Hapla 
1296ea844a1aSMatthew Knepley   PetscFunctionBegin;
1297ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1298dadcf809SJacob Faibussowitsch   PetscValidIntPointer(maxDof, 2);
129969c11d05SVaclav Hapla   if (s->maxDof == PETSC_MIN_INT) {
130069c11d05SVaclav Hapla     s->maxDof = 0;
1301ad540459SPierre Jolivet     for (p = 0; p < s->pEnd - s->pStart; ++p) s->maxDof = PetscMax(s->maxDof, s->atlasDof[p]);
130269c11d05SVaclav Hapla   }
1303ea844a1aSMatthew Knepley   *maxDof = s->maxDof;
13043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1305ea844a1aSMatthew Knepley }
1306ea844a1aSMatthew Knepley 
1307ea844a1aSMatthew Knepley /*@
130820662ed9SBarry Smith   PetscSectionGetStorageSize - Return the size of an array or local `Vec` capable of holding all the degrees of freedom defined in a `PetscSection`
1309ea844a1aSMatthew Knepley 
131040750d41SVaclav Hapla   Not Collective
1311ea844a1aSMatthew Knepley 
1312ea844a1aSMatthew Knepley   Input Parameter:
1313cab54364SBarry Smith . s - the `PetscSection`
1314ea844a1aSMatthew Knepley 
1315ea844a1aSMatthew Knepley   Output Parameter:
1316ea844a1aSMatthew Knepley . size - the size of an array which can hold all the dofs
1317ea844a1aSMatthew Knepley 
1318ea844a1aSMatthew Knepley   Level: intermediate
1319ea844a1aSMatthew Knepley 
1320cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetOffset()`, `PetscSectionGetConstrainedStorageSize()`, `PetscSectionCreate()`
1321ea844a1aSMatthew Knepley @*/
1322d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetStorageSize(PetscSection s, PetscInt *size)
1323d71ae5a4SJacob Faibussowitsch {
1324ea844a1aSMatthew Knepley   PetscInt p, n = 0;
1325ea844a1aSMatthew Knepley 
1326ea844a1aSMatthew Knepley   PetscFunctionBegin;
1327ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1328dadcf809SJacob Faibussowitsch   PetscValidIntPointer(size, 2);
1329ea844a1aSMatthew Knepley   for (p = 0; p < s->pEnd - s->pStart; ++p) n += s->atlasDof[p] > 0 ? s->atlasDof[p] : 0;
1330ea844a1aSMatthew Knepley   *size = n;
13313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1332ea844a1aSMatthew Knepley }
1333ea844a1aSMatthew Knepley 
1334ea844a1aSMatthew Knepley /*@
133520662ed9SBarry Smith   PetscSectionGetConstrainedStorageSize - Return the size of an array or local `Vec` capable of holding all unconstrained degrees of freedom in a `PetscSection`
1336ea844a1aSMatthew Knepley 
133740750d41SVaclav Hapla   Not Collective
1338ea844a1aSMatthew Knepley 
1339064ec1f3Sprj-   Input Parameter:
1340cab54364SBarry Smith . s - the `PetscSection`
1341ea844a1aSMatthew Knepley 
1342ea844a1aSMatthew Knepley   Output Parameter:
1343ea844a1aSMatthew Knepley . size - the size of an array which can hold all unconstrained dofs
1344ea844a1aSMatthew Knepley 
1345ea844a1aSMatthew Knepley   Level: intermediate
1346ea844a1aSMatthew Knepley 
1347cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetStorageSize()`, `PetscSectionGetOffset()`, `PetscSectionCreate()`
1348ea844a1aSMatthew Knepley @*/
1349d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetConstrainedStorageSize(PetscSection s, PetscInt *size)
1350d71ae5a4SJacob Faibussowitsch {
1351ea844a1aSMatthew Knepley   PetscInt p, n = 0;
1352ea844a1aSMatthew Knepley 
1353ea844a1aSMatthew Knepley   PetscFunctionBegin;
1354ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1355dadcf809SJacob Faibussowitsch   PetscValidIntPointer(size, 2);
1356ea844a1aSMatthew Knepley   for (p = 0; p < s->pEnd - s->pStart; ++p) {
1357ea844a1aSMatthew Knepley     const PetscInt cdof = s->bc ? s->bc->atlasDof[p] : 0;
1358ea844a1aSMatthew Knepley     n += s->atlasDof[p] > 0 ? s->atlasDof[p] - cdof : 0;
1359ea844a1aSMatthew Knepley   }
1360ea844a1aSMatthew Knepley   *size = n;
13613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1362ea844a1aSMatthew Knepley }
1363ea844a1aSMatthew Knepley 
1364ea844a1aSMatthew Knepley /*@
1365*583308b6SBarry Smith   PetscSectionCreateGlobalSection - Create a parallel section describing the global layout using
1366*583308b6SBarry Smith   a local (sequential) `PetscSection` on each MPI process and a `PetscSF` describing the section point overlap.
1367ea844a1aSMatthew Knepley 
1368ea844a1aSMatthew Knepley   Input Parameters:
1369cab54364SBarry Smith + s - The `PetscSection` for the local field layout
1370cab54364SBarry Smith . sf - The `PetscSF` describing parallel layout of the section points (leaves are unowned local points)
1371cab54364SBarry Smith . includeConstraints - By default this is `PETSC_FALSE`, meaning that the global field vector will not possess constrained dofs
1372cab54364SBarry Smith - localOffsets - If `PETSC_TRUE`, use local rather than global offsets for the points
1373ea844a1aSMatthew Knepley 
1374ea844a1aSMatthew Knepley   Output Parameter:
1375cab54364SBarry Smith . gsection - The `PetscSection` for the global field layout
1376ea844a1aSMatthew Knepley 
1377ea844a1aSMatthew Knepley   Level: intermediate
1378ea844a1aSMatthew Knepley 
1379db527f05SMatthew G. Knepley   Notes:
1380*583308b6SBarry Smith   On each MPI process `gsection` inherits the chart of the `s` on that process.
1381db527f05SMatthew G. Knepley 
1382*583308b6SBarry Smith   This sets negative sizes and offsets to points not owned by this process as defined by `sf` but that are within the local value of the chart of `gsection`.
1383*583308b6SBarry Smith   In those locations the value of size is -(size+1) and the value of the offset on the remote process is -(off+1).
1384cab54364SBarry Smith 
138520662ed9SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionCreateGlobalSectionCensored()`
1386ea844a1aSMatthew Knepley @*/
1387d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCreateGlobalSection(PetscSection s, PetscSF sf, PetscBool includeConstraints, PetscBool localOffsets, PetscSection *gsection)
1388d71ae5a4SJacob Faibussowitsch {
1389ea844a1aSMatthew Knepley   PetscSection    gs;
1390ea844a1aSMatthew Knepley   const PetscInt *pind = NULL;
1391ea844a1aSMatthew Knepley   PetscInt       *recv = NULL, *neg = NULL;
1392046d2115Sksagiyam   PetscInt        pStart, pEnd, p, dof, cdof, off, foff, globalOff = 0, nroots, nlocal, maxleaf;
1393046d2115Sksagiyam   PetscInt        numFields, f, numComponents;
1394ea844a1aSMatthew Knepley 
1395ea844a1aSMatthew Knepley   PetscFunctionBegin;
1396ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1397ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
1398ea844a1aSMatthew Knepley   PetscValidLogicalCollectiveBool(s, includeConstraints, 3);
1399ea844a1aSMatthew Knepley   PetscValidLogicalCollectiveBool(s, localOffsets, 4);
1400ea844a1aSMatthew Knepley   PetscValidPointer(gsection, 5);
140128b400f6SJacob Faibussowitsch   PetscCheck(s->pointMajor, PETSC_COMM_SELF, PETSC_ERR_SUP, "No support for field major ordering");
14029566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s), &gs));
14039566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &numFields));
14049566063dSJacob Faibussowitsch   if (numFields > 0) PetscCall(PetscSectionSetNumFields(gs, numFields));
14059566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
14069566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(gs, pStart, pEnd));
140787e637c6Sksagiyam   gs->includesConstraints = includeConstraints;
14089566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, &nroots, NULL, NULL, NULL));
1409ea844a1aSMatthew Knepley   nlocal = nroots; /* The local/leaf space matches global/root space */
1410ea844a1aSMatthew Knepley   /* Must allocate for all points visible to SF, which may be more than this section */
1411ea844a1aSMatthew Knepley   if (nroots >= 0) { /* nroots < 0 means that the graph has not been set, only happens in serial */
14129566063dSJacob Faibussowitsch     PetscCall(PetscSFGetLeafRange(sf, NULL, &maxleaf));
141308401ef6SPierre Jolivet     PetscCheck(nroots >= pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "SF roots %" PetscInt_FMT " < pEnd %" PetscInt_FMT, nroots, pEnd);
141408401ef6SPierre Jolivet     PetscCheck(maxleaf < nroots, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Max local leaf %" PetscInt_FMT " >= nroots %" PetscInt_FMT, maxleaf, nroots);
14159566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(nroots, &neg, nlocal, &recv));
14169566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(neg, nroots));
1417ea844a1aSMatthew Knepley   }
1418ea844a1aSMatthew Knepley   /* Mark all local points with negative dof */
1419ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
14209566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s, p, &dof));
14219566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(gs, p, dof));
14229566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
14239566063dSJacob Faibussowitsch     if (!includeConstraints && cdof > 0) PetscCall(PetscSectionSetConstraintDof(gs, p, cdof));
1424ea844a1aSMatthew Knepley     if (neg) neg[p] = -(dof + 1);
1425ea844a1aSMatthew Knepley   }
14269566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUpBC(gs));
14279566063dSJacob 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]));
1428ea844a1aSMatthew Knepley   if (nroots >= 0) {
14299566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(recv, nlocal));
14309566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, neg, recv, MPI_REPLACE));
14319566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, neg, recv, MPI_REPLACE));
1432ea844a1aSMatthew Knepley     for (p = pStart; p < pEnd; ++p) {
1433ea844a1aSMatthew Knepley       if (recv[p] < 0) {
1434ea844a1aSMatthew Knepley         gs->atlasDof[p - pStart] = recv[p];
14359566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
143608401ef6SPierre 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);
1437ea844a1aSMatthew Knepley       }
1438ea844a1aSMatthew Knepley     }
1439ea844a1aSMatthew Knepley   }
1440ea844a1aSMatthew Knepley   /* Calculate new sizes, get process offset, and calculate point offsets */
14419566063dSJacob Faibussowitsch   if (s->perm) PetscCall(ISGetIndices(s->perm, &pind));
1442ea844a1aSMatthew Knepley   for (p = 0, off = 0; p < pEnd - pStart; ++p) {
1443ea844a1aSMatthew Knepley     const PetscInt q = pind ? pind[p] : p;
1444ea844a1aSMatthew Knepley 
1445ea844a1aSMatthew Knepley     cdof            = (!includeConstraints && s->bc) ? s->bc->atlasDof[q] : 0;
1446ea844a1aSMatthew Knepley     gs->atlasOff[q] = off;
1447ea844a1aSMatthew Knepley     off += gs->atlasDof[q] > 0 ? gs->atlasDof[q] - cdof : 0;
1448ea844a1aSMatthew Knepley   }
1449ea844a1aSMatthew Knepley   if (!localOffsets) {
14509566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Scan(&off, &globalOff, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)s)));
1451ea844a1aSMatthew Knepley     globalOff -= off;
1452ea844a1aSMatthew Knepley   }
1453ea844a1aSMatthew Knepley   for (p = pStart, off = 0; p < pEnd; ++p) {
1454ea844a1aSMatthew Knepley     gs->atlasOff[p - pStart] += globalOff;
1455ea844a1aSMatthew Knepley     if (neg) neg[p] = -(gs->atlasOff[p - pStart] + 1);
1456ea844a1aSMatthew Knepley   }
14579566063dSJacob Faibussowitsch   if (s->perm) PetscCall(ISRestoreIndices(s->perm, &pind));
1458ea844a1aSMatthew Knepley   /* Put in negative offsets for ghost points */
1459ea844a1aSMatthew Knepley   if (nroots >= 0) {
14609566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(recv, nlocal));
14619566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, neg, recv, MPI_REPLACE));
14629566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, neg, recv, MPI_REPLACE));
1463ea844a1aSMatthew Knepley     for (p = pStart; p < pEnd; ++p) {
1464ea844a1aSMatthew Knepley       if (recv[p] < 0) gs->atlasOff[p - pStart] = recv[p];
1465ea844a1aSMatthew Knepley     }
1466ea844a1aSMatthew Knepley   }
14679566063dSJacob Faibussowitsch   PetscCall(PetscFree2(neg, recv));
1468046d2115Sksagiyam   /* Set field dofs/offsets/constraints */
1469046d2115Sksagiyam   for (f = 0; f < numFields; ++f) {
1470046d2115Sksagiyam     gs->field[f]->includesConstraints = includeConstraints;
14719566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &numComponents));
14729566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(gs, f, numComponents));
1473046d2115Sksagiyam   }
1474046d2115Sksagiyam   for (p = pStart; p < pEnd; ++p) {
14759566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(gs, p, &off));
1476046d2115Sksagiyam     for (f = 0, foff = off; f < numFields; ++f) {
14779566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(s, p, f, &cdof));
14789566063dSJacob Faibussowitsch       if (!includeConstraints && cdof > 0) PetscCall(PetscSectionSetFieldConstraintDof(gs, p, f, cdof));
14799566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
14809566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(gs, p, f, off < 0 ? -(dof + 1) : dof));
14819566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldOffset(gs, p, f, foff));
14829566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(gs, p, f, &cdof));
1483046d2115Sksagiyam       foff = off < 0 ? foff - (dof - cdof) : foff + (dof - cdof);
1484046d2115Sksagiyam     }
1485046d2115Sksagiyam   }
1486046d2115Sksagiyam   for (f = 0; f < numFields; ++f) {
1487046d2115Sksagiyam     PetscSection gfs = gs->field[f];
1488046d2115Sksagiyam 
14899566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUpBC(gfs));
14909566063dSJacob 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]));
1491046d2115Sksagiyam   }
1492046d2115Sksagiyam   gs->setup = PETSC_TRUE;
14939566063dSJacob Faibussowitsch   PetscCall(PetscSectionViewFromOptions(gs, NULL, "-global_section_view"));
1494ea844a1aSMatthew Knepley   *gsection = gs;
14953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1496ea844a1aSMatthew Knepley }
1497ea844a1aSMatthew Knepley 
1498ea844a1aSMatthew Knepley /*@
1499*583308b6SBarry Smith   PetscSectionCreateGlobalSectionCensored - Create a `PetscSection` describing the globallayout using
1500*583308b6SBarry Smith   a local (sequential) `PetscSection` on each MPI process and an `PetscSF` describing the section point overlap.
1501ea844a1aSMatthew Knepley 
1502ea844a1aSMatthew Knepley   Input Parameters:
1503cab54364SBarry Smith + s - The `PetscSection` for the local field layout
1504cab54364SBarry Smith . sf - The `PetscSF` describing parallel layout of the section points
1505*583308b6SBarry Smith . includeConstraints - By default this is `PETSC_FALSE`, meaning that the global vector will not possess constrained dofs
1506*583308b6SBarry Smith . numExcludes - The number of exclusion ranges, this must have the same value on all MPI processes
1507*583308b6SBarry Smith - excludes - An array [start_0, end_0, start_1, end_1, ...] where there are `numExcludes` pairs and must have the same values on all MPI processes
1508ea844a1aSMatthew Knepley 
1509ea844a1aSMatthew Knepley   Output Parameter:
1510cab54364SBarry Smith . gsection - The `PetscSection` for the global field layout
1511ea844a1aSMatthew Knepley 
1512ea844a1aSMatthew Knepley   Level: advanced
1513ea844a1aSMatthew Knepley 
1514*583308b6SBarry Smith   Notes:
1515*583308b6SBarry Smith   On each MPI process `gsection` inherits the chart of the `s` on that process.
151620662ed9SBarry Smith 
1517*583308b6SBarry Smith   This sets negative sizes and offsets to points not owned by this process as defined by `sf` but that are within the local value of the chart of `gsection`.
1518*583308b6SBarry Smith   In those locations the value of size is -(size+1) and the value of the offset on the remote process is -(off+1).
1519*583308b6SBarry Smith 
1520*583308b6SBarry Smith   This routine augments `PetscSectionCreateGlobalSection()` by allowing one to exclude certain ranges in the chart of the `PetscSection`
1521cab54364SBarry Smith 
152220662ed9SBarry Smith   Developer Note:
152320662ed9SBarry Smith   This is a terrible function name
152420662ed9SBarry Smith 
152520662ed9SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionCreateGlobalSectionCensored()`
1526ea844a1aSMatthew Knepley @*/
1527d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCreateGlobalSectionCensored(PetscSection s, PetscSF sf, PetscBool includeConstraints, PetscInt numExcludes, const PetscInt excludes[], PetscSection *gsection)
1528d71ae5a4SJacob Faibussowitsch {
1529ea844a1aSMatthew Knepley   const PetscInt *pind = NULL;
1530ea844a1aSMatthew Knepley   PetscInt       *neg = NULL, *tmpOff = NULL;
1531ea844a1aSMatthew Knepley   PetscInt        pStart, pEnd, p, e, dof, cdof, off, globalOff = 0, nroots;
1532ea844a1aSMatthew Knepley 
1533ea844a1aSMatthew Knepley   PetscFunctionBegin;
1534ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1535ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
1536ea844a1aSMatthew Knepley   PetscValidPointer(gsection, 6);
15379566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s), gsection));
15389566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
15399566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(*gsection, pStart, pEnd));
15409566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, &nroots, NULL, NULL, NULL));
1541ea844a1aSMatthew Knepley   if (nroots >= 0) {
154208401ef6SPierre Jolivet     PetscCheck(nroots >= pEnd - pStart, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "PetscSF nroots %" PetscInt_FMT " < %" PetscInt_FMT " section size", nroots, pEnd - pStart);
15439566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(nroots, &neg));
1544ea844a1aSMatthew Knepley     if (nroots > pEnd - pStart) {
15459566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(nroots, &tmpOff));
1546ea844a1aSMatthew Knepley     } else {
1547ea844a1aSMatthew Knepley       tmpOff = &(*gsection)->atlasDof[-pStart];
1548ea844a1aSMatthew Knepley     }
1549ea844a1aSMatthew Knepley   }
1550ea844a1aSMatthew Knepley   /* Mark ghost points with negative dof */
1551ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
1552ea844a1aSMatthew Knepley     for (e = 0; e < numExcludes; ++e) {
1553ea844a1aSMatthew Knepley       if ((p >= excludes[e * 2 + 0]) && (p < excludes[e * 2 + 1])) {
15549566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetDof(*gsection, p, 0));
1555ea844a1aSMatthew Knepley         break;
1556ea844a1aSMatthew Knepley       }
1557ea844a1aSMatthew Knepley     }
1558ea844a1aSMatthew Knepley     if (e < numExcludes) continue;
15599566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s, p, &dof));
15609566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(*gsection, p, dof));
15619566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
15629566063dSJacob Faibussowitsch     if (!includeConstraints && cdof > 0) PetscCall(PetscSectionSetConstraintDof(*gsection, p, cdof));
1563ea844a1aSMatthew Knepley     if (neg) neg[p] = -(dof + 1);
1564ea844a1aSMatthew Knepley   }
15659566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUpBC(*gsection));
1566ea844a1aSMatthew Knepley   if (nroots >= 0) {
15679566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, neg, tmpOff, MPI_REPLACE));
15689566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, neg, tmpOff, MPI_REPLACE));
1569ea844a1aSMatthew Knepley     if (nroots > pEnd - pStart) {
15709371c9d4SSatish Balay       for (p = pStart; p < pEnd; ++p) {
15719371c9d4SSatish Balay         if (tmpOff[p] < 0) (*gsection)->atlasDof[p - pStart] = tmpOff[p];
15729371c9d4SSatish Balay       }
1573ea844a1aSMatthew Knepley     }
1574ea844a1aSMatthew Knepley   }
157535cb6cd3SPierre Jolivet   /* Calculate new sizes, get process offset, and calculate point offsets */
15769566063dSJacob Faibussowitsch   if (s->perm) PetscCall(ISGetIndices(s->perm, &pind));
1577ea844a1aSMatthew Knepley   for (p = 0, off = 0; p < pEnd - pStart; ++p) {
1578ea844a1aSMatthew Knepley     const PetscInt q = pind ? pind[p] : p;
1579ea844a1aSMatthew Knepley 
1580ea844a1aSMatthew Knepley     cdof                     = (!includeConstraints && s->bc) ? s->bc->atlasDof[q] : 0;
1581ea844a1aSMatthew Knepley     (*gsection)->atlasOff[q] = off;
1582ea844a1aSMatthew Knepley     off += (*gsection)->atlasDof[q] > 0 ? (*gsection)->atlasDof[q] - cdof : 0;
1583ea844a1aSMatthew Knepley   }
15849566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Scan(&off, &globalOff, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)s)));
1585ea844a1aSMatthew Knepley   globalOff -= off;
1586ea844a1aSMatthew Knepley   for (p = 0, off = 0; p < pEnd - pStart; ++p) {
1587ea844a1aSMatthew Knepley     (*gsection)->atlasOff[p] += globalOff;
1588ea844a1aSMatthew Knepley     if (neg) neg[p + pStart] = -((*gsection)->atlasOff[p] + 1);
1589ea844a1aSMatthew Knepley   }
15909566063dSJacob Faibussowitsch   if (s->perm) PetscCall(ISRestoreIndices(s->perm, &pind));
1591ea844a1aSMatthew Knepley   /* Put in negative offsets for ghost points */
1592ea844a1aSMatthew Knepley   if (nroots >= 0) {
1593ea844a1aSMatthew Knepley     if (nroots == pEnd - pStart) tmpOff = &(*gsection)->atlasOff[-pStart];
15949566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, neg, tmpOff, MPI_REPLACE));
15959566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, neg, tmpOff, MPI_REPLACE));
1596ea844a1aSMatthew Knepley     if (nroots > pEnd - pStart) {
15979371c9d4SSatish Balay       for (p = pStart; p < pEnd; ++p) {
15989371c9d4SSatish Balay         if (tmpOff[p] < 0) (*gsection)->atlasOff[p - pStart] = tmpOff[p];
15999371c9d4SSatish Balay       }
1600ea844a1aSMatthew Knepley     }
1601ea844a1aSMatthew Knepley   }
16029566063dSJacob Faibussowitsch   if (nroots >= 0 && nroots > pEnd - pStart) PetscCall(PetscFree(tmpOff));
16039566063dSJacob Faibussowitsch   PetscCall(PetscFree(neg));
16043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1605ea844a1aSMatthew Knepley }
1606ea844a1aSMatthew Knepley 
1607ea844a1aSMatthew Knepley /*@
1608*583308b6SBarry Smith   PetscSectionGetPointLayout - Get a `PetscLayout` for the points with nonzero dof counts of the unnamed default field within this `PetscSection`s local chart
1609ea844a1aSMatthew Knepley 
1610cab54364SBarry Smith   Collective
1611ea844a1aSMatthew Knepley 
1612ea844a1aSMatthew Knepley   Input Parameters:
1613cab54364SBarry Smith + comm - The `MPI_Comm`
1614cab54364SBarry Smith - s    - The `PetscSection`
1615ea844a1aSMatthew Knepley 
1616ea844a1aSMatthew Knepley   Output Parameter:
161720662ed9SBarry Smith . layout - The point layout for the data that defines the section
1618ea844a1aSMatthew Knepley 
1619ea844a1aSMatthew Knepley   Level: advanced
1620ea844a1aSMatthew Knepley 
162120662ed9SBarry Smith   Notes:
1622*583308b6SBarry Smith   `PetscSectionGetValueLayout()` provides similar information but counting the total number of degrees of freedom on the MPI process (excluding constrained
1623*583308b6SBarry Smith   degrees of freedom).
162420662ed9SBarry Smith 
1625*583308b6SBarry Smith   This count includes constrained degrees of freedom
1626*583308b6SBarry Smith 
1627*583308b6SBarry Smith   This is usually called on the default global section.
1628*583308b6SBarry Smith 
1629*583308b6SBarry Smith   Example:
1630*583308b6SBarry Smith .vb
1631*583308b6SBarry Smith      The chart is [2,5), point 2 has 2 dof, point 3 has 0 dof, point 4 has 1 dof
1632*583308b6SBarry Smith      The local size of the `PetscLayout` is 2 since 2 points have a non-zero number of dof
1633*583308b6SBarry Smith .ve
1634*583308b6SBarry Smith 
1635*583308b6SBarry Smith   Developer Note:
1636*583308b6SBarry Smith   I find the names of these two functions extremely non-informative
1637cab54364SBarry Smith 
1638cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetValueLayout()`, `PetscSectionCreate()`
1639ea844a1aSMatthew Knepley @*/
1640d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetPointLayout(MPI_Comm comm, PetscSection s, PetscLayout *layout)
1641d71ae5a4SJacob Faibussowitsch {
1642ea844a1aSMatthew Knepley   PetscInt pStart, pEnd, p, localSize = 0;
1643ea844a1aSMatthew Knepley 
1644ea844a1aSMatthew Knepley   PetscFunctionBegin;
16459566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
1646ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
1647ea844a1aSMatthew Knepley     PetscInt dof;
1648ea844a1aSMatthew Knepley 
16499566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s, p, &dof));
16509120ba30SVaclav Hapla     if (dof >= 0) ++localSize;
1651ea844a1aSMatthew Knepley   }
16529566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, layout));
16539566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(*layout, localSize));
16549566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(*layout, 1));
16559566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(*layout));
16563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1657ea844a1aSMatthew Knepley }
1658ea844a1aSMatthew Knepley 
1659ea844a1aSMatthew Knepley /*@
166020662ed9SBarry Smith   PetscSectionGetValueLayout - Get the `PetscLayout` associated with the section dofs of a `PetscSection`
1661ea844a1aSMatthew Knepley 
1662cab54364SBarry Smith   Collective
1663ea844a1aSMatthew Knepley 
1664ea844a1aSMatthew Knepley   Input Parameters:
1665cab54364SBarry Smith + comm - The `MPI_Comm`
1666cab54364SBarry Smith - s    - The `PetscSection`
1667ea844a1aSMatthew Knepley 
1668ea844a1aSMatthew Knepley   Output Parameter:
1669ea844a1aSMatthew Knepley . layout - The dof layout for the section
1670ea844a1aSMatthew Knepley 
1671ea844a1aSMatthew Knepley   Level: advanced
1672ea844a1aSMatthew Knepley 
167320662ed9SBarry Smith   Notes:
1674*583308b6SBarry Smith   `PetscSectionGetPointLayout()` provides similar information but only counting the number of points with nonzero degrees of freedom and
1675*583308b6SBarry Smith   including the constrained degrees of freedom
167620662ed9SBarry Smith 
1677cab54364SBarry Smith   This is usually called for the default global section.
1678cab54364SBarry Smith 
1679*583308b6SBarry Smith   Example:
1680*583308b6SBarry Smith .vb
1681*583308b6SBarry Smith      The chart is [2,5), point 2 has 4 dof (2 constrained), point 3 has 0 dof, point 4 has 1 dof (not constrained)
1682*583308b6SBarry Smith      The local size of the `PetscLayout` is 3 since there are 3 unconstrained degrees of freedom on this MPI process
1683*583308b6SBarry Smith .ve
1684*583308b6SBarry Smith 
1685cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetPointLayout()`, `PetscSectionCreate()`
1686ea844a1aSMatthew Knepley @*/
1687d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetValueLayout(MPI_Comm comm, PetscSection s, PetscLayout *layout)
1688d71ae5a4SJacob Faibussowitsch {
1689ea844a1aSMatthew Knepley   PetscInt pStart, pEnd, p, localSize = 0;
1690ea844a1aSMatthew Knepley 
1691ea844a1aSMatthew Knepley   PetscFunctionBegin;
1692ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 2);
1693ea844a1aSMatthew Knepley   PetscValidPointer(layout, 3);
16949566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
1695ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
1696ea844a1aSMatthew Knepley     PetscInt dof, cdof;
1697ea844a1aSMatthew Knepley 
16989566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s, p, &dof));
16999566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
1700ea844a1aSMatthew Knepley     if (dof - cdof > 0) localSize += dof - cdof;
1701ea844a1aSMatthew Knepley   }
17029566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, layout));
17039566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(*layout, localSize));
17049566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(*layout, 1));
17059566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(*layout));
17063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1707ea844a1aSMatthew Knepley }
1708ea844a1aSMatthew Knepley 
1709ea844a1aSMatthew Knepley /*@
1710db527f05SMatthew G. Knepley   PetscSectionGetOffset - Return the offset into an array or `Vec` for the dof associated with the given point.
1711ea844a1aSMatthew Knepley 
171240750d41SVaclav Hapla   Not Collective
1713ea844a1aSMatthew Knepley 
1714ea844a1aSMatthew Knepley   Input Parameters:
1715cab54364SBarry Smith + s - the `PetscSection`
1716ea844a1aSMatthew Knepley - point - the point
1717ea844a1aSMatthew Knepley 
1718ea844a1aSMatthew Knepley   Output Parameter:
1719ea844a1aSMatthew Knepley . offset - the offset
1720ea844a1aSMatthew Knepley 
1721ea844a1aSMatthew Knepley   Level: intermediate
1722ea844a1aSMatthew Knepley 
1723*583308b6SBarry Smith   Notes:
1724*583308b6SBarry Smith   In a global section, this offset will be negative for points not owned by this process.
1725*583308b6SBarry Smith 
1726*583308b6SBarry Smith   This is for the unnamed default field in the `PetscSection` not the named fields
1727*583308b6SBarry Smith 
1728*583308b6SBarry Smith   The `offset` values are different depending on a value set with `PetscSectionSetPointMajor()`
1729*583308b6SBarry Smith 
1730*583308b6SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetFieldOffset()`, `PetscSectionCreate()`, `PetscSectionSetPointMajor()`
1731ea844a1aSMatthew Knepley @*/
1732d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetOffset(PetscSection s, PetscInt point, PetscInt *offset)
1733d71ae5a4SJacob Faibussowitsch {
1734ea844a1aSMatthew Knepley   PetscFunctionBegin;
1735ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1736dadcf809SJacob Faibussowitsch   PetscValidIntPointer(offset, 3);
1737ad540459SPierre 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);
1738ea844a1aSMatthew Knepley   *offset = s->atlasOff[point - s->pStart];
17393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1740ea844a1aSMatthew Knepley }
1741ea844a1aSMatthew Knepley 
1742ea844a1aSMatthew Knepley /*@
1743db527f05SMatthew G. Knepley   PetscSectionSetOffset - Set the offset into an array or `Vec` for the dof associated with the given point.
1744ea844a1aSMatthew Knepley 
174540750d41SVaclav Hapla   Not Collective
1746ea844a1aSMatthew Knepley 
1747ea844a1aSMatthew Knepley   Input Parameters:
1748cab54364SBarry Smith + s - the `PetscSection`
1749ea844a1aSMatthew Knepley . point - the point
1750ea844a1aSMatthew Knepley - offset - the offset
1751ea844a1aSMatthew Knepley 
1752*583308b6SBarry Smith   Level: developer
1753ea844a1aSMatthew Knepley 
1754cab54364SBarry Smith   Note:
1755cab54364SBarry Smith   The user usually does not call this function, but uses `PetscSectionSetUp()`
1756cab54364SBarry Smith 
1757cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetFieldOffset()`, `PetscSectionCreate()`, `PetscSectionSetUp()`
1758ea844a1aSMatthew Knepley @*/
1759d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetOffset(PetscSection s, PetscInt point, PetscInt offset)
1760d71ae5a4SJacob Faibussowitsch {
1761ea844a1aSMatthew Knepley   PetscFunctionBegin;
1762ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
176308401ef6SPierre 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);
1764ea844a1aSMatthew Knepley   s->atlasOff[point - s->pStart] = offset;
17653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1766ea844a1aSMatthew Knepley }
1767ea844a1aSMatthew Knepley 
1768ea844a1aSMatthew Knepley /*@
1769db527f05SMatthew G. Knepley   PetscSectionGetFieldOffset - Return the offset into an array or `Vec` for the field dof associated with the given point.
1770ea844a1aSMatthew Knepley 
177140750d41SVaclav Hapla   Not Collective
1772ea844a1aSMatthew Knepley 
1773ea844a1aSMatthew Knepley   Input Parameters:
1774cab54364SBarry Smith + s - the `PetscSection`
1775ea844a1aSMatthew Knepley . point - the point
1776ea844a1aSMatthew Knepley - field - the field
1777ea844a1aSMatthew Knepley 
1778ea844a1aSMatthew Knepley   Output Parameter:
1779ea844a1aSMatthew Knepley . offset - the offset
1780ea844a1aSMatthew Knepley 
1781ea844a1aSMatthew Knepley   Level: intermediate
1782ea844a1aSMatthew Knepley 
1783*583308b6SBarry Smith   Notes:
1784*583308b6SBarry Smith   In a global section, this offset will be negative for points not owned by this process.
1785*583308b6SBarry Smith 
1786*583308b6SBarry Smith   The `offset` values are different depending on a value set with `PetscSectionSetPointMajor()`
1787*583308b6SBarry Smith 
1788*583308b6SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetOffset()`, `PetscSectionCreate()`, `PetscSectionGetFieldPointOffset()`
1789ea844a1aSMatthew Knepley @*/
1790d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldOffset(PetscSection s, PetscInt point, PetscInt field, PetscInt *offset)
1791d71ae5a4SJacob Faibussowitsch {
1792ea844a1aSMatthew Knepley   PetscFunctionBegin;
1793ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
17942abc8c78SJacob Faibussowitsch   PetscValidIntPointer(offset, 4);
17952abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
17969566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(s->field[field], point, offset));
17973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1798ea844a1aSMatthew Knepley }
1799ea844a1aSMatthew Knepley 
1800ea844a1aSMatthew Knepley /*@
1801db527f05SMatthew G. Knepley   PetscSectionSetFieldOffset - Set the offset into an array or `Vec` for the dof associated with the given field at a point.
1802ea844a1aSMatthew Knepley 
180340750d41SVaclav Hapla   Not Collective
1804ea844a1aSMatthew Knepley 
1805ea844a1aSMatthew Knepley   Input Parameters:
180620662ed9SBarry Smith + s - the `PetscSection`
1807ea844a1aSMatthew Knepley . point - the point
1808ea844a1aSMatthew Knepley . field - the field
1809ea844a1aSMatthew Knepley - offset - the offset
1810ea844a1aSMatthew Knepley 
1811cab54364SBarry Smith   Level: developer
1812ea844a1aSMatthew Knepley 
1813cab54364SBarry Smith   Note:
1814cab54364SBarry Smith   The user usually does not call this function, but uses `PetscSectionSetUp()`
1815ea844a1aSMatthew Knepley 
1816cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetFieldOffset()`, `PetscSectionSetOffset()`, `PetscSectionCreate()`, `PetscSectionSetUp()`
1817ea844a1aSMatthew Knepley @*/
1818d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFieldOffset(PetscSection s, PetscInt point, PetscInt field, PetscInt offset)
1819d71ae5a4SJacob Faibussowitsch {
1820ea844a1aSMatthew Knepley   PetscFunctionBegin;
1821ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
18222abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
18239566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetOffset(s->field[field], point, offset));
18243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1825ea844a1aSMatthew Knepley }
1826ea844a1aSMatthew Knepley 
1827ea844a1aSMatthew Knepley /*@
1828*583308b6SBarry Smith   PetscSectionGetFieldPointOffset - Return the offset for the first field dof associated with the given point relative to the offset for that point for the
1829*583308b6SBarry Smith   unnamed default field's first dof
1830ea844a1aSMatthew Knepley 
183140750d41SVaclav Hapla   Not Collective
1832ea844a1aSMatthew Knepley 
1833ea844a1aSMatthew Knepley   Input Parameters:
1834cab54364SBarry Smith + s - the `PetscSection`
1835ea844a1aSMatthew Knepley . point - the point
1836ea844a1aSMatthew Knepley - field - the field
1837ea844a1aSMatthew Knepley 
1838ea844a1aSMatthew Knepley   Output Parameter:
1839ea844a1aSMatthew Knepley . offset - the offset
1840ea844a1aSMatthew Knepley 
1841ea844a1aSMatthew Knepley   Level: advanced
1842ea844a1aSMatthew Knepley 
1843cab54364SBarry Smith   Note:
1844*583308b6SBarry Smith   This ignores constraints
1845cab54364SBarry Smith 
1846*583308b6SBarry Smith   Example:
1847*583308b6SBarry Smith .vb
1848*583308b6SBarry Smith   if PetscSectionSetPointMajor(s,PETSC_TRUE)
1849*583308b6SBarry Smith   The unnamed default field has 3 dof at `point`
1850*583308b6SBarry Smith   Field 0 has 2 dof at `point`
1851*583308b6SBarry Smith   Then PetscSectionGetFieldPointOffset(s,point,1,&offset) returns and offset of 5
1852*583308b6SBarry Smith .ve
1853*583308b6SBarry Smith 
1854*583308b6SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetOffset()`, `PetscSectionCreate()`, `PetscSectionGetFieldOffset()`
1855ea844a1aSMatthew Knepley @*/
1856d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldPointOffset(PetscSection s, PetscInt point, PetscInt field, PetscInt *offset)
1857d71ae5a4SJacob Faibussowitsch {
1858ea844a1aSMatthew Knepley   PetscInt off, foff;
1859ea844a1aSMatthew Knepley 
1860ea844a1aSMatthew Knepley   PetscFunctionBegin;
1861ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
18622abc8c78SJacob Faibussowitsch   PetscValidIntPointer(offset, 4);
18632abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
18649566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(s, point, &off));
18659566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(s->field[field], point, &foff));
1866ea844a1aSMatthew Knepley   *offset = foff - off;
18673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1868ea844a1aSMatthew Knepley }
1869ea844a1aSMatthew Knepley 
1870ea844a1aSMatthew Knepley /*@
187120662ed9SBarry Smith   PetscSectionGetOffsetRange - Return the full range of offsets [`start`, `end`) for a `PetscSection`
1872ea844a1aSMatthew Knepley 
187340750d41SVaclav Hapla   Not Collective
1874ea844a1aSMatthew Knepley 
1875ea844a1aSMatthew Knepley   Input Parameter:
1876cab54364SBarry Smith . s - the `PetscSection`
1877ea844a1aSMatthew Knepley 
1878ea844a1aSMatthew Knepley   Output Parameters:
1879ea844a1aSMatthew Knepley + start - the minimum offset
1880ea844a1aSMatthew Knepley - end   - one more than the maximum offset
1881ea844a1aSMatthew Knepley 
1882ea844a1aSMatthew Knepley   Level: intermediate
1883ea844a1aSMatthew Knepley 
1884cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetOffset()`, `PetscSectionCreate()`
1885ea844a1aSMatthew Knepley @*/
1886d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetOffsetRange(PetscSection s, PetscInt *start, PetscInt *end)
1887d71ae5a4SJacob Faibussowitsch {
1888ea844a1aSMatthew Knepley   PetscInt os = 0, oe = 0, pStart, pEnd, p;
1889ea844a1aSMatthew Knepley 
1890ea844a1aSMatthew Knepley   PetscFunctionBegin;
1891ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
18929371c9d4SSatish Balay   if (s->atlasOff) {
18939371c9d4SSatish Balay     os = s->atlasOff[0];
18949371c9d4SSatish Balay     oe = s->atlasOff[0];
18959371c9d4SSatish Balay   }
18969566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
1897ea844a1aSMatthew Knepley   for (p = 0; p < pEnd - pStart; ++p) {
1898ea844a1aSMatthew Knepley     PetscInt dof = s->atlasDof[p], off = s->atlasOff[p];
1899ea844a1aSMatthew Knepley 
1900ea844a1aSMatthew Knepley     if (off >= 0) {
1901ea844a1aSMatthew Knepley       os = PetscMin(os, off);
1902ea844a1aSMatthew Knepley       oe = PetscMax(oe, off + dof);
1903ea844a1aSMatthew Knepley     }
1904ea844a1aSMatthew Knepley   }
1905ea844a1aSMatthew Knepley   if (start) *start = os;
1906ea844a1aSMatthew Knepley   if (end) *end = oe;
19073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1908ea844a1aSMatthew Knepley }
1909ea844a1aSMatthew Knepley 
1910ea844a1aSMatthew Knepley /*@
1911*583308b6SBarry Smith   PetscSectionCreateSubsection - Create a new, smaller `PetscSection` composed of only selected fields
1912ea844a1aSMatthew Knepley 
191340750d41SVaclav Hapla   Collective
1914ea844a1aSMatthew Knepley 
1915d8d19677SJose E. Roman   Input Parameters:
1916cab54364SBarry Smith + s      - the `PetscSection`
1917ea844a1aSMatthew Knepley . len    - the number of subfields
1918ea844a1aSMatthew Knepley - fields - the subfield numbers
1919ea844a1aSMatthew Knepley 
1920ea844a1aSMatthew Knepley   Output Parameter:
1921ea844a1aSMatthew Knepley . subs   - the subsection
1922ea844a1aSMatthew Knepley 
1923ea844a1aSMatthew Knepley   Level: advanced
1924ea844a1aSMatthew Knepley 
1925cab54364SBarry Smith   Notes:
1926*583308b6SBarry Smith   The chart of `subs` is the same as the chart of `s`
1927cab54364SBarry Smith 
1928cab54364SBarry Smith   This will error if a fieldnumber is out of range
1929cab54364SBarry Smith 
1930cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreateSupersection()`, `PetscSectionCreate()`
1931ea844a1aSMatthew Knepley @*/
1932d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCreateSubsection(PetscSection s, PetscInt len, const PetscInt fields[], PetscSection *subs)
1933d71ae5a4SJacob Faibussowitsch {
1934b778fa18SValeria Barra   PetscInt nF, f, c, pStart, pEnd, p, maxCdof = 0;
1935ea844a1aSMatthew Knepley 
1936ea844a1aSMatthew Knepley   PetscFunctionBegin;
19373ba16761SJacob Faibussowitsch   if (!len) PetscFunctionReturn(PETSC_SUCCESS);
1938ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1939dadcf809SJacob Faibussowitsch   PetscValidIntPointer(fields, 3);
1940ea844a1aSMatthew Knepley   PetscValidPointer(subs, 4);
19419566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &nF));
194208401ef6SPierre 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);
19439566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s), subs));
19449566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(*subs, len));
1945ea844a1aSMatthew Knepley   for (f = 0; f < len; ++f) {
1946ea844a1aSMatthew Knepley     const char *name    = NULL;
1947ea844a1aSMatthew Knepley     PetscInt    numComp = 0;
1948ea844a1aSMatthew Knepley 
19499566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldName(s, fields[f], &name));
19509566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldName(*subs, f, name));
19519566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, fields[f], &numComp));
19529566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(*subs, f, numComp));
1953b778fa18SValeria Barra     for (c = 0; c < s->numFieldComponents[fields[f]]; ++c) {
19549566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetComponentName(s, fields[f], c, &name));
19559566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetComponentName(*subs, f, c, name));
1956b778fa18SValeria Barra     }
1957ea844a1aSMatthew Knepley   }
19589566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
19599566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(*subs, pStart, pEnd));
1960ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
1961ea844a1aSMatthew Knepley     PetscInt dof = 0, cdof = 0, fdof = 0, cfdof = 0;
1962ea844a1aSMatthew Knepley 
1963ea844a1aSMatthew Knepley     for (f = 0; f < len; ++f) {
19649566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, fields[f], &fdof));
19659566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(*subs, p, f, fdof));
19669566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(s, p, fields[f], &cfdof));
19679566063dSJacob Faibussowitsch       if (cfdof) PetscCall(PetscSectionSetFieldConstraintDof(*subs, p, f, cfdof));
1968ea844a1aSMatthew Knepley       dof += fdof;
1969ea844a1aSMatthew Knepley       cdof += cfdof;
1970ea844a1aSMatthew Knepley     }
19719566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(*subs, p, dof));
19729566063dSJacob Faibussowitsch     if (cdof) PetscCall(PetscSectionSetConstraintDof(*subs, p, cdof));
1973ea844a1aSMatthew Knepley     maxCdof = PetscMax(cdof, maxCdof);
1974ea844a1aSMatthew Knepley   }
19759566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(*subs));
1976ea844a1aSMatthew Knepley   if (maxCdof) {
1977ea844a1aSMatthew Knepley     PetscInt *indices;
1978ea844a1aSMatthew Knepley 
19799566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(maxCdof, &indices));
1980ea844a1aSMatthew Knepley     for (p = pStart; p < pEnd; ++p) {
1981ea844a1aSMatthew Knepley       PetscInt cdof;
1982ea844a1aSMatthew Knepley 
19839566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetConstraintDof(*subs, p, &cdof));
1984ea844a1aSMatthew Knepley       if (cdof) {
1985ea844a1aSMatthew Knepley         const PetscInt *oldIndices = NULL;
1986ea844a1aSMatthew Knepley         PetscInt        fdof = 0, cfdof = 0, fc, numConst = 0, fOff = 0;
1987ea844a1aSMatthew Knepley 
1988ea844a1aSMatthew Knepley         for (f = 0; f < len; ++f) {
19899566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(s, p, fields[f], &fdof));
19909566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(s, p, fields[f], &cfdof));
19919566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintIndices(s, p, fields[f], &oldIndices));
19929566063dSJacob Faibussowitsch           PetscCall(PetscSectionSetFieldConstraintIndices(*subs, p, f, oldIndices));
19939574d0f0SMatthew G. Knepley           for (fc = 0; fc < cfdof; ++fc) indices[numConst + fc] = oldIndices[fc] + fOff;
1994ea844a1aSMatthew Knepley           numConst += cfdof;
1995ea844a1aSMatthew Knepley           fOff += fdof;
1996ea844a1aSMatthew Knepley         }
19979566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetConstraintIndices(*subs, p, indices));
1998ea844a1aSMatthew Knepley       }
1999ea844a1aSMatthew Knepley     }
20009566063dSJacob Faibussowitsch     PetscCall(PetscFree(indices));
2001ea844a1aSMatthew Knepley   }
20023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2003ea844a1aSMatthew Knepley }
2004ea844a1aSMatthew Knepley 
2005ea844a1aSMatthew Knepley /*@
2006*583308b6SBarry Smith   PetscSectionCreateSupersection - Create a new, larger section composed of multiple `PetscSection`s
2007ea844a1aSMatthew Knepley 
200840750d41SVaclav Hapla   Collective
2009ea844a1aSMatthew Knepley 
2010ea844a1aSMatthew Knepley   Input Parameters:
2011ea844a1aSMatthew Knepley + s     - the input sections
2012ea844a1aSMatthew Knepley - len   - the number of input sections
2013ea844a1aSMatthew Knepley 
2014ea844a1aSMatthew Knepley   Output Parameter:
2015ea844a1aSMatthew Knepley . supers - the supersection
2016ea844a1aSMatthew Knepley 
2017ea844a1aSMatthew Knepley   Level: advanced
2018ea844a1aSMatthew Knepley 
2019*583308b6SBarry Smith   Notes:
2020cab54364SBarry Smith   The section offsets now refer to a new, larger vector.
2021cab54364SBarry Smith 
2022cab54364SBarry Smith   Developer Note:
2023cab54364SBarry Smith   Needs to explain how the sections are composed
2024cab54364SBarry Smith 
2025cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreateSubsection()`, `PetscSectionCreate()`
2026ea844a1aSMatthew Knepley @*/
2027d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCreateSupersection(PetscSection s[], PetscInt len, PetscSection *supers)
2028d71ae5a4SJacob Faibussowitsch {
2029ea844a1aSMatthew Knepley   PetscInt Nf = 0, f, pStart = PETSC_MAX_INT, pEnd = 0, p, maxCdof = 0, i;
2030ea844a1aSMatthew Knepley 
2031ea844a1aSMatthew Knepley   PetscFunctionBegin;
20323ba16761SJacob Faibussowitsch   if (!len) PetscFunctionReturn(PETSC_SUCCESS);
2033ea844a1aSMatthew Knepley   for (i = 0; i < len; ++i) {
2034ea844a1aSMatthew Knepley     PetscInt nf, pStarti, pEndi;
2035ea844a1aSMatthew Knepley 
20369566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(s[i], &nf));
20379566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(s[i], &pStarti, &pEndi));
2038ea844a1aSMatthew Knepley     pStart = PetscMin(pStart, pStarti);
2039ea844a1aSMatthew Knepley     pEnd   = PetscMax(pEnd, pEndi);
2040ea844a1aSMatthew Knepley     Nf += nf;
2041ea844a1aSMatthew Knepley   }
20429566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s[0]), supers));
20439566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(*supers, Nf));
2044ea844a1aSMatthew Knepley   for (i = 0, f = 0; i < len; ++i) {
2045b778fa18SValeria Barra     PetscInt nf, fi, ci;
2046ea844a1aSMatthew Knepley 
20479566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(s[i], &nf));
2048ea844a1aSMatthew Knepley     for (fi = 0; fi < nf; ++fi, ++f) {
2049ea844a1aSMatthew Knepley       const char *name    = NULL;
2050ea844a1aSMatthew Knepley       PetscInt    numComp = 0;
2051ea844a1aSMatthew Knepley 
20529566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(s[i], fi, &name));
20539566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldName(*supers, f, name));
20549566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(s[i], fi, &numComp));
20559566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldComponents(*supers, f, numComp));
2056b778fa18SValeria Barra       for (ci = 0; ci < s[i]->numFieldComponents[fi]; ++ci) {
20579566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetComponentName(s[i], fi, ci, &name));
20589566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetComponentName(*supers, f, ci, name));
2059b778fa18SValeria Barra       }
2060ea844a1aSMatthew Knepley     }
2061ea844a1aSMatthew Knepley   }
20629566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(*supers, pStart, pEnd));
2063ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
2064ea844a1aSMatthew Knepley     PetscInt dof = 0, cdof = 0;
2065ea844a1aSMatthew Knepley 
2066ea844a1aSMatthew Knepley     for (i = 0, f = 0; i < len; ++i) {
2067ea844a1aSMatthew Knepley       PetscInt nf, fi, pStarti, pEndi;
2068ea844a1aSMatthew Knepley       PetscInt fdof = 0, cfdof = 0;
2069ea844a1aSMatthew Knepley 
20709566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetNumFields(s[i], &nf));
20719566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s[i], &pStarti, &pEndi));
2072ea844a1aSMatthew Knepley       if ((p < pStarti) || (p >= pEndi)) continue;
2073ea844a1aSMatthew Knepley       for (fi = 0; fi < nf; ++fi, ++f) {
20749566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldDof(s[i], p, fi, &fdof));
20759566063dSJacob Faibussowitsch         PetscCall(PetscSectionAddFieldDof(*supers, p, f, fdof));
20769566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldConstraintDof(s[i], p, fi, &cfdof));
20779566063dSJacob Faibussowitsch         if (cfdof) PetscCall(PetscSectionAddFieldConstraintDof(*supers, p, f, cfdof));
2078ea844a1aSMatthew Knepley         dof += fdof;
2079ea844a1aSMatthew Knepley         cdof += cfdof;
2080ea844a1aSMatthew Knepley       }
2081ea844a1aSMatthew Knepley     }
20829566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(*supers, p, dof));
20839566063dSJacob Faibussowitsch     if (cdof) PetscCall(PetscSectionSetConstraintDof(*supers, p, cdof));
2084ea844a1aSMatthew Knepley     maxCdof = PetscMax(cdof, maxCdof);
2085ea844a1aSMatthew Knepley   }
20869566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(*supers));
2087ea844a1aSMatthew Knepley   if (maxCdof) {
2088ea844a1aSMatthew Knepley     PetscInt *indices;
2089ea844a1aSMatthew Knepley 
20909566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(maxCdof, &indices));
2091ea844a1aSMatthew Knepley     for (p = pStart; p < pEnd; ++p) {
2092ea844a1aSMatthew Knepley       PetscInt cdof;
2093ea844a1aSMatthew Knepley 
20949566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetConstraintDof(*supers, p, &cdof));
2095ea844a1aSMatthew Knepley       if (cdof) {
2096ea844a1aSMatthew Knepley         PetscInt dof, numConst = 0, fOff = 0;
2097ea844a1aSMatthew Knepley 
2098ea844a1aSMatthew Knepley         for (i = 0, f = 0; i < len; ++i) {
2099ea844a1aSMatthew Knepley           const PetscInt *oldIndices = NULL;
2100ea844a1aSMatthew Knepley           PetscInt        nf, fi, pStarti, pEndi, fdof, cfdof, fc;
2101ea844a1aSMatthew Knepley 
21029566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetNumFields(s[i], &nf));
21039566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetChart(s[i], &pStarti, &pEndi));
2104ea844a1aSMatthew Knepley           if ((p < pStarti) || (p >= pEndi)) continue;
2105ea844a1aSMatthew Knepley           for (fi = 0; fi < nf; ++fi, ++f) {
21069566063dSJacob Faibussowitsch             PetscCall(PetscSectionGetFieldDof(s[i], p, fi, &fdof));
21079566063dSJacob Faibussowitsch             PetscCall(PetscSectionGetFieldConstraintDof(s[i], p, fi, &cfdof));
21089566063dSJacob Faibussowitsch             PetscCall(PetscSectionGetFieldConstraintIndices(s[i], p, fi, &oldIndices));
2109fcd2503dSMatthew G. Knepley             for (fc = 0; fc < cfdof; ++fc) indices[numConst + fc] = oldIndices[fc];
21109566063dSJacob Faibussowitsch             PetscCall(PetscSectionSetFieldConstraintIndices(*supers, p, f, &indices[numConst]));
2111fcd2503dSMatthew G. Knepley             for (fc = 0; fc < cfdof; ++fc) indices[numConst + fc] += fOff;
2112ea844a1aSMatthew Knepley             numConst += cfdof;
2113ea844a1aSMatthew Knepley           }
21149566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetDof(s[i], p, &dof));
2115ea844a1aSMatthew Knepley           fOff += dof;
2116ea844a1aSMatthew Knepley         }
21179566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetConstraintIndices(*supers, p, indices));
2118ea844a1aSMatthew Knepley       }
2119ea844a1aSMatthew Knepley     }
21209566063dSJacob Faibussowitsch     PetscCall(PetscFree(indices));
2121ea844a1aSMatthew Knepley   }
21223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2123ea844a1aSMatthew Knepley }
2124ea844a1aSMatthew Knepley 
2125d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCreateSubplexSection_Internal(PetscSection s, IS subpointMap, PetscBool renumberPoints, PetscSection *subs)
2126d71ae5a4SJacob Faibussowitsch {
2127ea844a1aSMatthew Knepley   const PetscInt *points = NULL, *indices = NULL;
212841f23ed0SMatthew G. Knepley   PetscInt        numFields, f, c, numSubpoints = 0, pStart, pEnd, p, spStart, spEnd, subp;
2129ea844a1aSMatthew Knepley 
2130ea844a1aSMatthew Knepley   PetscFunctionBegin;
2131ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2132ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(subpointMap, IS_CLASSID, 2);
213341f23ed0SMatthew G. Knepley   PetscValidPointer(subs, 4);
21349566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &numFields));
21359566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s), subs));
21369566063dSJacob Faibussowitsch   if (numFields) PetscCall(PetscSectionSetNumFields(*subs, numFields));
2137ea844a1aSMatthew Knepley   for (f = 0; f < numFields; ++f) {
2138ea844a1aSMatthew Knepley     const char *name    = NULL;
2139ea844a1aSMatthew Knepley     PetscInt    numComp = 0;
2140ea844a1aSMatthew Knepley 
21419566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldName(s, f, &name));
21429566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldName(*subs, f, name));
21439566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &numComp));
21449566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(*subs, f, numComp));
2145b778fa18SValeria Barra     for (c = 0; c < s->numFieldComponents[f]; ++c) {
21469566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetComponentName(s, f, c, &name));
21479566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetComponentName(*subs, f, c, name));
2148b778fa18SValeria Barra     }
2149ea844a1aSMatthew Knepley   }
2150ea844a1aSMatthew Knepley   /* For right now, we do not try to squeeze the subchart */
2151ea844a1aSMatthew Knepley   if (subpointMap) {
21529566063dSJacob Faibussowitsch     PetscCall(ISGetSize(subpointMap, &numSubpoints));
21539566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subpointMap, &points));
2154ea844a1aSMatthew Knepley   }
21559566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
215641f23ed0SMatthew G. Knepley   if (renumberPoints) {
215741f23ed0SMatthew G. Knepley     spStart = 0;
215841f23ed0SMatthew G. Knepley     spEnd   = numSubpoints;
215941f23ed0SMatthew G. Knepley   } else {
216041f23ed0SMatthew G. Knepley     PetscCall(ISGetMinMax(subpointMap, &spStart, &spEnd));
216141f23ed0SMatthew G. Knepley     ++spEnd;
216241f23ed0SMatthew G. Knepley   }
216341f23ed0SMatthew G. Knepley   PetscCall(PetscSectionSetChart(*subs, spStart, spEnd));
2164ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
2165ea844a1aSMatthew Knepley     PetscInt dof, cdof, fdof = 0, cfdof = 0;
2166ea844a1aSMatthew Knepley 
21679566063dSJacob Faibussowitsch     PetscCall(PetscFindInt(p, numSubpoints, points, &subp));
2168ea844a1aSMatthew Knepley     if (subp < 0) continue;
216941f23ed0SMatthew G. Knepley     if (!renumberPoints) subp = p;
2170ea844a1aSMatthew Knepley     for (f = 0; f < numFields; ++f) {
21719566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &fdof));
21729566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(*subs, subp, f, fdof));
21739566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(s, p, f, &cfdof));
21749566063dSJacob Faibussowitsch       if (cfdof) PetscCall(PetscSectionSetFieldConstraintDof(*subs, subp, f, cfdof));
2175ea844a1aSMatthew Knepley     }
21769566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s, p, &dof));
21779566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(*subs, subp, dof));
21789566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
21799566063dSJacob Faibussowitsch     if (cdof) PetscCall(PetscSectionSetConstraintDof(*subs, subp, cdof));
2180ea844a1aSMatthew Knepley   }
21819566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(*subs));
2182ea844a1aSMatthew Knepley   /* Change offsets to original offsets */
2183ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
2184ea844a1aSMatthew Knepley     PetscInt off, foff = 0;
2185ea844a1aSMatthew Knepley 
21869566063dSJacob Faibussowitsch     PetscCall(PetscFindInt(p, numSubpoints, points, &subp));
2187ea844a1aSMatthew Knepley     if (subp < 0) continue;
218841f23ed0SMatthew G. Knepley     if (!renumberPoints) subp = p;
2189ea844a1aSMatthew Knepley     for (f = 0; f < numFields; ++f) {
21909566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldOffset(s, p, f, &foff));
21919566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldOffset(*subs, subp, f, foff));
2192ea844a1aSMatthew Knepley     }
21939566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(s, p, &off));
21949566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetOffset(*subs, subp, off));
2195ea844a1aSMatthew Knepley   }
2196ea844a1aSMatthew Knepley   /* Copy constraint indices */
219741f23ed0SMatthew G. Knepley   for (subp = spStart; subp < spEnd; ++subp) {
2198ea844a1aSMatthew Knepley     PetscInt cdof;
2199ea844a1aSMatthew Knepley 
22009566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(*subs, subp, &cdof));
2201ea844a1aSMatthew Knepley     if (cdof) {
2202ea844a1aSMatthew Knepley       for (f = 0; f < numFields; ++f) {
220341f23ed0SMatthew G. Knepley         PetscCall(PetscSectionGetFieldConstraintIndices(s, points[subp - spStart], f, &indices));
22049566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetFieldConstraintIndices(*subs, subp, f, indices));
2205ea844a1aSMatthew Knepley       }
220641f23ed0SMatthew G. Knepley       PetscCall(PetscSectionGetConstraintIndices(s, points[subp - spStart], &indices));
22079566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetConstraintIndices(*subs, subp, indices));
2208ea844a1aSMatthew Knepley     }
2209ea844a1aSMatthew Knepley   }
22109566063dSJacob Faibussowitsch   if (subpointMap) PetscCall(ISRestoreIndices(subpointMap, &points));
22113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2212ea844a1aSMatthew Knepley }
2213ea844a1aSMatthew Knepley 
221441f23ed0SMatthew G. Knepley /*@
221541f23ed0SMatthew G. Knepley   PetscSectionCreateSubmeshSection - Create a new, smaller section with support on the submesh
221641f23ed0SMatthew G. Knepley 
2217c3339decSBarry Smith   Collective
221841f23ed0SMatthew G. Knepley 
221941f23ed0SMatthew G. Knepley   Input Parameters:
2220cab54364SBarry Smith + s           - the `PetscSection`
222141f23ed0SMatthew G. Knepley - subpointMap - a sorted list of points in the original mesh which are in the submesh
222241f23ed0SMatthew G. Knepley 
222341f23ed0SMatthew G. Knepley   Output Parameter:
222441f23ed0SMatthew G. Knepley . subs - the subsection
222541f23ed0SMatthew G. Knepley 
2226cab54364SBarry Smith   Level: advanced
2227cab54364SBarry Smith 
2228*583308b6SBarry Smith   Notes:
2229*583308b6SBarry Smith   The points are renumbered from 0, and the section offsets now refer to a new, smaller vector. That is the chart of `subs` is `[0,sizeof(subpointmap))`
2230*583308b6SBarry Smith 
223120662ed9SBarry Smith   Compare this with `PetscSectionCreateSubdomainSection()` that does not map the points numbers to start at zero but leaves them as before
223241f23ed0SMatthew G. Knepley 
2233cab54364SBarry Smith   Developer Note:
223420662ed9SBarry 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`
223541f23ed0SMatthew G. Knepley 
2236cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreateSubdomainSection()`, `PetscSectionCreateSubsection()`, `DMPlexGetSubpointMap()`, `PetscSectionCreate()`
223741f23ed0SMatthew G. Knepley @*/
2238d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCreateSubmeshSection(PetscSection s, IS subpointMap, PetscSection *subs)
2239d71ae5a4SJacob Faibussowitsch {
224041f23ed0SMatthew G. Knepley   PetscFunctionBegin;
224141f23ed0SMatthew G. Knepley   PetscCall(PetscSectionCreateSubplexSection_Internal(s, subpointMap, PETSC_TRUE, subs));
22423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
224341f23ed0SMatthew G. Knepley }
224441f23ed0SMatthew G. Knepley 
224541f23ed0SMatthew G. Knepley /*@
224641f23ed0SMatthew G. Knepley   PetscSectionCreateSubdomainSection - Create a new, smaller section with support on a subdomain of the mesh
224741f23ed0SMatthew G. Knepley 
2248c3339decSBarry Smith   Collective
224941f23ed0SMatthew G. Knepley 
225041f23ed0SMatthew G. Knepley   Input Parameters:
2251cab54364SBarry Smith + s           - the `PetscSection`
225241f23ed0SMatthew G. Knepley - subpointMap - a sorted list of points in the original mesh which are in the subdomain
225341f23ed0SMatthew G. Knepley 
225441f23ed0SMatthew G. Knepley   Output Parameter:
225541f23ed0SMatthew G. Knepley . subs - the subsection
225641f23ed0SMatthew G. Knepley 
2257cab54364SBarry Smith   Level: advanced
2258cab54364SBarry Smith 
2259*583308b6SBarry Smith   Notes:
2260*583308b6SBarry Smith   The point numbers remain the same as in the larger `PetscSection`, but the section offsets now refer to a new, smaller vector. The chart of `subs`
2261*583308b6SBarry Smith   is `[min(subpointMap),max(subpointMap)+1)`
2262*583308b6SBarry Smith 
226320662ed9SBarry Smith   Compare this with `PetscSectionCreateSubmeshSection()` that maps the point numbers to start at zero
226441f23ed0SMatthew G. Knepley 
2265cab54364SBarry Smith   Developer Notes:
226620662ed9SBarry 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`
2267cab54364SBarry Smith 
2268cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreateSubmeshSection()`, `PetscSectionCreateSubsection()`, `DMPlexGetSubpointMap()`, `PetscSectionCreate()`
226941f23ed0SMatthew G. Knepley @*/
2270d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCreateSubdomainSection(PetscSection s, IS subpointMap, PetscSection *subs)
2271d71ae5a4SJacob Faibussowitsch {
227241f23ed0SMatthew G. Knepley   PetscFunctionBegin;
227341f23ed0SMatthew G. Knepley   PetscCall(PetscSectionCreateSubplexSection_Internal(s, subpointMap, PETSC_FALSE, subs));
22743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
227541f23ed0SMatthew G. Knepley }
227641f23ed0SMatthew G. Knepley 
2277d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscSectionView_ASCII(PetscSection s, PetscViewer viewer)
2278d71ae5a4SJacob Faibussowitsch {
2279ea844a1aSMatthew Knepley   PetscInt    p;
2280ea844a1aSMatthew Knepley   PetscMPIInt rank;
2281ea844a1aSMatthew Knepley 
2282ea844a1aSMatthew Knepley   PetscFunctionBegin;
22839566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank));
22849566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPushSynchronized(viewer));
22859566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "Process %d:\n", rank));
2286ea844a1aSMatthew Knepley   for (p = 0; p < s->pEnd - s->pStart; ++p) {
2287ea844a1aSMatthew Knepley     if ((s->bc) && (s->bc->atlasDof[p] > 0)) {
2288ea844a1aSMatthew Knepley       PetscInt b;
2289ea844a1aSMatthew Knepley 
22909566063dSJacob 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]));
2291083401c6SMatthew G. Knepley       if (s->bcIndices) {
229248a46eb9SPierre Jolivet         for (b = 0; b < s->bc->atlasDof[p]; ++b) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " %" PetscInt_FMT, s->bcIndices[s->bc->atlasOff[p] + b]));
2293083401c6SMatthew G. Knepley       }
22949566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n"));
2295ea844a1aSMatthew Knepley     } else {
22969566063dSJacob 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]));
2297ea844a1aSMatthew Knepley     }
2298ea844a1aSMatthew Knepley   }
22999566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(viewer));
23009566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPopSynchronized(viewer));
2301ea844a1aSMatthew Knepley   if (s->sym) {
23029566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPushTab(viewer));
23039566063dSJacob Faibussowitsch     PetscCall(PetscSectionSymView(s->sym, viewer));
23049566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPopTab(viewer));
2305ea844a1aSMatthew Knepley   }
23063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2307ea844a1aSMatthew Knepley }
2308ea844a1aSMatthew Knepley 
2309ea844a1aSMatthew Knepley /*@C
2310cab54364SBarry Smith    PetscSectionViewFromOptions - View the `PetscSection` based on values in the options database
2311fe2efc57SMark 
231240750d41SVaclav Hapla    Collective
2313fe2efc57SMark 
2314fe2efc57SMark    Input Parameters:
231520662ed9SBarry Smith +  A - the `PetscSection` object to view
2316cab54364SBarry Smith .  obj - Optional object that provides the options prefix used for the options
2317736c3998SJose E. Roman -  name - command line option
2318fe2efc57SMark 
2319fe2efc57SMark    Level: intermediate
2320cab54364SBarry Smith 
232120662ed9SBarry Smith    Note:
232220662ed9SBarry Smith    See `PetscObjectViewFromOptions()` for available values of `PetscViewer` and `PetscViewerFormat`
232320662ed9SBarry Smith 
2324cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionView`, `PetscObjectViewFromOptions()`, `PetscSectionCreate()`, `PetscSectionView()`
2325fe2efc57SMark @*/
2326d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionViewFromOptions(PetscSection A, PetscObject obj, const char name[])
2327d71ae5a4SJacob Faibussowitsch {
2328fe2efc57SMark   PetscFunctionBegin;
2329fe2efc57SMark   PetscValidHeaderSpecific(A, PETSC_SECTION_CLASSID, 1);
23309566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
23313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2332fe2efc57SMark }
2333fe2efc57SMark 
2334fe2efc57SMark /*@C
2335cab54364SBarry Smith   PetscSectionView - Views a `PetscSection`
2336ea844a1aSMatthew Knepley 
233740750d41SVaclav Hapla   Collective
2338ea844a1aSMatthew Knepley 
2339ea844a1aSMatthew Knepley   Input Parameters:
2340cab54364SBarry Smith + s - the `PetscSection` object to view
2341ea844a1aSMatthew Knepley - v - the viewer
2342ea844a1aSMatthew Knepley 
2343cab54364SBarry Smith   Level: beginner
2344cab54364SBarry Smith 
23451ddd528eSksagiyam   Note:
2346cab54364SBarry Smith   `PetscSectionView()`, when viewer is of type `PETSCVIEWERHDF5`, only saves
23471ddd528eSksagiyam   distribution independent data, such as dofs, offsets, constraint dofs,
23481ddd528eSksagiyam   and constraint indices. Points that have negative dofs, for instance,
23491ddd528eSksagiyam   are not saved as they represent points owned by other processes.
23501ddd528eSksagiyam   Point numbering and rank assignment is currently not stored.
2351cab54364SBarry Smith   The saved section can be loaded with `PetscSectionLoad()`.
23521ddd528eSksagiyam 
2353cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionDestroy()`, `PetscSectionLoad()`, `PetscViewer`
2354ea844a1aSMatthew Knepley @*/
2355d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionView(PetscSection s, PetscViewer viewer)
2356d71ae5a4SJacob Faibussowitsch {
23571ddd528eSksagiyam   PetscBool isascii, ishdf5;
2358ea844a1aSMatthew Knepley   PetscInt  f;
2359ea844a1aSMatthew Knepley 
2360ea844a1aSMatthew Knepley   PetscFunctionBegin;
2361ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
23629566063dSJacob Faibussowitsch   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)s), &viewer));
2363ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
23649566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
23659566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2366ea844a1aSMatthew Knepley   if (isascii) {
23679566063dSJacob Faibussowitsch     PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)s, viewer));
2368ea844a1aSMatthew Knepley     if (s->numFields) {
23699566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " fields\n", s->numFields));
2370ea844a1aSMatthew Knepley       for (f = 0; f < s->numFields; ++f) {
23719566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, "  field %" PetscInt_FMT " with %" PetscInt_FMT " components\n", f, s->numFieldComponents[f]));
23729566063dSJacob Faibussowitsch         PetscCall(PetscSectionView_ASCII(s->field[f], viewer));
2373ea844a1aSMatthew Knepley       }
2374ea844a1aSMatthew Knepley     } else {
23759566063dSJacob Faibussowitsch       PetscCall(PetscSectionView_ASCII(s, viewer));
2376ea844a1aSMatthew Knepley     }
23771ddd528eSksagiyam   } else if (ishdf5) {
23781ddd528eSksagiyam #if PetscDefined(HAVE_HDF5)
23799566063dSJacob Faibussowitsch     PetscCall(PetscSectionView_HDF5_Internal(s, viewer));
23801ddd528eSksagiyam #else
23811ddd528eSksagiyam     SETERRQ(PetscObjectComm((PetscObject)s), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
23821ddd528eSksagiyam #endif
2383ea844a1aSMatthew Knepley   }
23843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2385ea844a1aSMatthew Knepley }
2386ea844a1aSMatthew Knepley 
2387fde5e3acSksagiyam /*@C
2388cab54364SBarry Smith   PetscSectionLoad - Loads a `PetscSection`
2389fde5e3acSksagiyam 
239040750d41SVaclav Hapla   Collective
2391fde5e3acSksagiyam 
2392fde5e3acSksagiyam   Input Parameters:
2393cab54364SBarry Smith + s - the `PetscSection` object to load
2394fde5e3acSksagiyam - v - the viewer
2395fde5e3acSksagiyam 
2396cab54364SBarry Smith   Level: beginner
2397cab54364SBarry Smith 
2398fde5e3acSksagiyam   Note:
2399cab54364SBarry Smith   `PetscSectionLoad()`, when viewer is of type `PETSCVIEWERHDF5`, loads
2400cab54364SBarry Smith   a section saved with `PetscSectionView()`. The number of processes
2401fde5e3acSksagiyam   used here (N) does not need to be the same as that used when saving.
2402fde5e3acSksagiyam   After calling this function, the chart of s on rank i will be set
2403fde5e3acSksagiyam   to [0, E_i), where \sum_{i=0}^{N-1}E_i equals to the total number of
2404fde5e3acSksagiyam   saved section points.
2405fde5e3acSksagiyam 
2406cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionDestroy()`, `PetscSectionView()`
2407fde5e3acSksagiyam @*/
2408d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionLoad(PetscSection s, PetscViewer viewer)
2409d71ae5a4SJacob Faibussowitsch {
2410fde5e3acSksagiyam   PetscBool ishdf5;
2411fde5e3acSksagiyam 
2412fde5e3acSksagiyam   PetscFunctionBegin;
2413fde5e3acSksagiyam   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2414fde5e3acSksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
24159566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2416fde5e3acSksagiyam   if (ishdf5) {
2417fde5e3acSksagiyam #if PetscDefined(HAVE_HDF5)
24189566063dSJacob Faibussowitsch     PetscCall(PetscSectionLoad_HDF5_Internal(s, viewer));
24193ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2420fde5e3acSksagiyam #else
2421fde5e3acSksagiyam     SETERRQ(PetscObjectComm((PetscObject)s), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2422fde5e3acSksagiyam #endif
242398921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)s), PETSC_ERR_SUP, "Viewer type %s not yet supported for PetscSection loading", ((PetscObject)viewer)->type_name);
2424fde5e3acSksagiyam }
2425fde5e3acSksagiyam 
2426d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscSectionResetClosurePermutation(PetscSection section)
2427d71ae5a4SJacob Faibussowitsch {
2428c459fbc1SJed Brown   PetscSectionClosurePermVal clVal;
2429c459fbc1SJed Brown 
2430c459fbc1SJed Brown   PetscFunctionBegin;
24313ba16761SJacob Faibussowitsch   if (!section->clHash) PetscFunctionReturn(PETSC_SUCCESS);
2432c459fbc1SJed Brown   kh_foreach_value(section->clHash, clVal, {
24339566063dSJacob Faibussowitsch     PetscCall(PetscFree(clVal.perm));
24349566063dSJacob Faibussowitsch     PetscCall(PetscFree(clVal.invPerm));
2435c459fbc1SJed Brown   });
2436c459fbc1SJed Brown   kh_destroy(ClPerm, section->clHash);
2437c459fbc1SJed Brown   section->clHash = NULL;
24383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2439c459fbc1SJed Brown }
2440c459fbc1SJed Brown 
2441ea844a1aSMatthew Knepley /*@
2442*583308b6SBarry Smith   PetscSectionReset - Frees all section data, the section is then as if `PetscSectionCreate()` had just been called.
2443ea844a1aSMatthew Knepley 
244440750d41SVaclav Hapla   Not Collective
2445ea844a1aSMatthew Knepley 
24462fe279fdSBarry Smith   Input Parameter:
2447cab54364SBarry Smith . s - the `PetscSection`
2448ea844a1aSMatthew Knepley 
2449ea844a1aSMatthew Knepley   Level: beginner
2450ea844a1aSMatthew Knepley 
2451cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreate()`
2452ea844a1aSMatthew Knepley @*/
2453d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionReset(PetscSection s)
2454d71ae5a4SJacob Faibussowitsch {
2455b778fa18SValeria Barra   PetscInt f, c;
2456ea844a1aSMatthew Knepley 
2457ea844a1aSMatthew Knepley   PetscFunctionBegin;
2458ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2459ea844a1aSMatthew Knepley   for (f = 0; f < s->numFields; ++f) {
24609566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&s->field[f]));
24619566063dSJacob Faibussowitsch     PetscCall(PetscFree(s->fieldNames[f]));
246248a46eb9SPierre Jolivet     for (c = 0; c < s->numFieldComponents[f]; ++c) PetscCall(PetscFree(s->compNames[f][c]));
24639566063dSJacob Faibussowitsch     PetscCall(PetscFree(s->compNames[f]));
2464ea844a1aSMatthew Knepley   }
24659566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->numFieldComponents));
24669566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->fieldNames));
24679566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->compNames));
24689566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->field));
24699566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&s->bc));
24709566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->bcIndices));
24719566063dSJacob Faibussowitsch   PetscCall(PetscFree2(s->atlasDof, s->atlasOff));
24729566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&s->clSection));
24739566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&s->clPoints));
24749566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&s->perm));
24759566063dSJacob Faibussowitsch   PetscCall(PetscSectionResetClosurePermutation(s));
24769566063dSJacob Faibussowitsch   PetscCall(PetscSectionSymDestroy(&s->sym));
24779566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&s->clSection));
24789566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&s->clPoints));
247969c11d05SVaclav Hapla   PetscCall(PetscSectionInvalidateMaxDof_Internal(s));
2480ea844a1aSMatthew Knepley   s->pStart    = -1;
2481ea844a1aSMatthew Knepley   s->pEnd      = -1;
2482ea844a1aSMatthew Knepley   s->maxDof    = 0;
2483ea844a1aSMatthew Knepley   s->setup     = PETSC_FALSE;
2484ea844a1aSMatthew Knepley   s->numFields = 0;
2485ea844a1aSMatthew Knepley   s->clObj     = NULL;
24863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2487ea844a1aSMatthew Knepley }
2488ea844a1aSMatthew Knepley 
2489ea844a1aSMatthew Knepley /*@
2490*583308b6SBarry Smith   PetscSectionDestroy - Frees a `PetscSection`
2491ea844a1aSMatthew Knepley 
249240750d41SVaclav Hapla   Not Collective
2493ea844a1aSMatthew Knepley 
24942fe279fdSBarry Smith   Input Parameter:
2495cab54364SBarry Smith . s - the `PetscSection`
2496ea844a1aSMatthew Knepley 
2497ea844a1aSMatthew Knepley   Level: beginner
2498ea844a1aSMatthew Knepley 
2499cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionReset()`
2500ea844a1aSMatthew Knepley @*/
2501d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionDestroy(PetscSection *s)
2502d71ae5a4SJacob Faibussowitsch {
2503ea844a1aSMatthew Knepley   PetscFunctionBegin;
25043ba16761SJacob Faibussowitsch   if (!*s) PetscFunctionReturn(PETSC_SUCCESS);
250562f17a49SStefano Zampini   PetscValidHeaderSpecific(*s, PETSC_SECTION_CLASSID, 1);
2506ea844a1aSMatthew Knepley   if (--((PetscObject)(*s))->refct > 0) {
2507ea844a1aSMatthew Knepley     *s = NULL;
25083ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2509ea844a1aSMatthew Knepley   }
25109566063dSJacob Faibussowitsch   PetscCall(PetscSectionReset(*s));
25119566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(s));
25123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2513ea844a1aSMatthew Knepley }
2514ea844a1aSMatthew Knepley 
2515d71ae5a4SJacob Faibussowitsch PetscErrorCode VecIntGetValuesSection(PetscInt *baseArray, PetscSection s, PetscInt point, const PetscInt **values)
2516d71ae5a4SJacob Faibussowitsch {
2517ea844a1aSMatthew Knepley   const PetscInt p = point - s->pStart;
2518ea844a1aSMatthew Knepley 
2519ea844a1aSMatthew Knepley   PetscFunctionBegin;
2520ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 2);
2521ea844a1aSMatthew Knepley   *values = &baseArray[s->atlasOff[p]];
25223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2523ea844a1aSMatthew Knepley }
2524ea844a1aSMatthew Knepley 
2525d71ae5a4SJacob Faibussowitsch PetscErrorCode VecIntSetValuesSection(PetscInt *baseArray, PetscSection s, PetscInt point, const PetscInt values[], InsertMode mode)
2526d71ae5a4SJacob Faibussowitsch {
2527ea844a1aSMatthew Knepley   PetscInt      *array;
2528ea844a1aSMatthew Knepley   const PetscInt p           = point - s->pStart;
2529ea844a1aSMatthew Knepley   const PetscInt orientation = 0; /* Needs to be included for use in closure operations */
2530ea844a1aSMatthew Knepley   PetscInt       cDim        = 0;
2531ea844a1aSMatthew Knepley 
2532ea844a1aSMatthew Knepley   PetscFunctionBegin;
2533ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 2);
25349566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstraintDof(s, p, &cDim));
2535ea844a1aSMatthew Knepley   array = &baseArray[s->atlasOff[p]];
2536ea844a1aSMatthew Knepley   if (!cDim) {
2537ea844a1aSMatthew Knepley     if (orientation >= 0) {
2538ea844a1aSMatthew Knepley       const PetscInt dim = s->atlasDof[p];
2539ea844a1aSMatthew Knepley       PetscInt       i;
2540ea844a1aSMatthew Knepley 
2541ea844a1aSMatthew Knepley       if (mode == INSERT_VALUES) {
2542ea844a1aSMatthew Knepley         for (i = 0; i < dim; ++i) array[i] = values[i];
2543ea844a1aSMatthew Knepley       } else {
2544ea844a1aSMatthew Knepley         for (i = 0; i < dim; ++i) array[i] += values[i];
2545ea844a1aSMatthew Knepley       }
2546ea844a1aSMatthew Knepley     } else {
2547ea844a1aSMatthew Knepley       PetscInt offset = 0;
2548ea844a1aSMatthew Knepley       PetscInt j      = -1, field, i;
2549ea844a1aSMatthew Knepley 
2550ea844a1aSMatthew Knepley       for (field = 0; field < s->numFields; ++field) {
2551ea844a1aSMatthew Knepley         const PetscInt dim = s->field[field]->atlasDof[p];
2552ea844a1aSMatthew Knepley 
2553ea844a1aSMatthew Knepley         for (i = dim - 1; i >= 0; --i) array[++j] = values[i + offset];
2554ea844a1aSMatthew Knepley         offset += dim;
2555ea844a1aSMatthew Knepley       }
2556ea844a1aSMatthew Knepley     }
2557ea844a1aSMatthew Knepley   } else {
2558ea844a1aSMatthew Knepley     if (orientation >= 0) {
2559ea844a1aSMatthew Knepley       const PetscInt  dim  = s->atlasDof[p];
2560ea844a1aSMatthew Knepley       PetscInt        cInd = 0, i;
2561ea844a1aSMatthew Knepley       const PetscInt *cDof;
2562ea844a1aSMatthew Knepley 
25639566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetConstraintIndices(s, point, &cDof));
2564ea844a1aSMatthew Knepley       if (mode == INSERT_VALUES) {
2565ea844a1aSMatthew Knepley         for (i = 0; i < dim; ++i) {
25669371c9d4SSatish Balay           if ((cInd < cDim) && (i == cDof[cInd])) {
25679371c9d4SSatish Balay             ++cInd;
25689371c9d4SSatish Balay             continue;
25699371c9d4SSatish Balay           }
2570ea844a1aSMatthew Knepley           array[i] = values[i];
2571ea844a1aSMatthew Knepley         }
2572ea844a1aSMatthew Knepley       } else {
2573ea844a1aSMatthew Knepley         for (i = 0; i < dim; ++i) {
25749371c9d4SSatish Balay           if ((cInd < cDim) && (i == cDof[cInd])) {
25759371c9d4SSatish Balay             ++cInd;
25769371c9d4SSatish Balay             continue;
25779371c9d4SSatish Balay           }
2578ea844a1aSMatthew Knepley           array[i] += values[i];
2579ea844a1aSMatthew Knepley         }
2580ea844a1aSMatthew Knepley       }
2581ea844a1aSMatthew Knepley     } else {
2582ea844a1aSMatthew Knepley       const PetscInt *cDof;
2583ea844a1aSMatthew Knepley       PetscInt        offset  = 0;
2584ea844a1aSMatthew Knepley       PetscInt        cOffset = 0;
2585ea844a1aSMatthew Knepley       PetscInt        j       = 0, field;
2586ea844a1aSMatthew Knepley 
25879566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetConstraintIndices(s, point, &cDof));
2588ea844a1aSMatthew Knepley       for (field = 0; field < s->numFields; ++field) {
2589ea844a1aSMatthew Knepley         const PetscInt dim  = s->field[field]->atlasDof[p];     /* PetscSectionGetFieldDof() */
2590ea844a1aSMatthew Knepley         const PetscInt tDim = s->field[field]->bc->atlasDof[p]; /* PetscSectionGetFieldConstraintDof() */
2591ea844a1aSMatthew Knepley         const PetscInt sDim = dim - tDim;
2592ea844a1aSMatthew Knepley         PetscInt       cInd = 0, i, k;
2593ea844a1aSMatthew Knepley 
2594ea844a1aSMatthew Knepley         for (i = 0, k = dim + offset - 1; i < dim; ++i, ++j, --k) {
25959371c9d4SSatish Balay           if ((cInd < sDim) && (j == cDof[cInd + cOffset])) {
25969371c9d4SSatish Balay             ++cInd;
25979371c9d4SSatish Balay             continue;
25989371c9d4SSatish Balay           }
2599ea844a1aSMatthew Knepley           array[j] = values[k];
2600ea844a1aSMatthew Knepley         }
2601ea844a1aSMatthew Knepley         offset += dim;
2602ea844a1aSMatthew Knepley         cOffset += dim - tDim;
2603ea844a1aSMatthew Knepley       }
2604ea844a1aSMatthew Knepley     }
2605ea844a1aSMatthew Knepley   }
26063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2607ea844a1aSMatthew Knepley }
2608ea844a1aSMatthew Knepley 
2609ea844a1aSMatthew Knepley /*@C
261020662ed9SBarry Smith   PetscSectionHasConstraints - Determine whether a `PetscSection` has constrained dofs
2611ea844a1aSMatthew Knepley 
261240750d41SVaclav Hapla   Not Collective
2613ea844a1aSMatthew Knepley 
2614ea844a1aSMatthew Knepley   Input Parameter:
2615cab54364SBarry Smith . s - The `PetscSection`
2616ea844a1aSMatthew Knepley 
2617ea844a1aSMatthew Knepley   Output Parameter:
2618ea844a1aSMatthew Knepley . hasConstraints - flag indicating that the section has constrained dofs
2619ea844a1aSMatthew Knepley 
2620ea844a1aSMatthew Knepley   Level: intermediate
2621ea844a1aSMatthew Knepley 
2622cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSetConstraintIndices()`, `PetscSectionGetConstraintDof()`, `PetscSection`
2623ea844a1aSMatthew Knepley @*/
2624d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionHasConstraints(PetscSection s, PetscBool *hasConstraints)
2625d71ae5a4SJacob Faibussowitsch {
2626ea844a1aSMatthew Knepley   PetscFunctionBegin;
2627ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2628dadcf809SJacob Faibussowitsch   PetscValidBoolPointer(hasConstraints, 2);
2629ea844a1aSMatthew Knepley   *hasConstraints = s->bc ? PETSC_TRUE : PETSC_FALSE;
26303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2631ea844a1aSMatthew Knepley }
2632ea844a1aSMatthew Knepley 
2633ea844a1aSMatthew Knepley /*@C
263420662ed9SBarry Smith   PetscSectionGetConstraintIndices - Get the point dof numbers, in [0, dof), which are constrained for a given point
2635ea844a1aSMatthew Knepley 
263640750d41SVaclav Hapla   Not Collective
2637ea844a1aSMatthew Knepley 
2638ea844a1aSMatthew Knepley   Input Parameters:
2639cab54364SBarry Smith + s     - The `PetscSection`
2640ea844a1aSMatthew Knepley - point - The point
2641ea844a1aSMatthew Knepley 
2642ea844a1aSMatthew Knepley   Output Parameter:
2643ea844a1aSMatthew Knepley . indices - The constrained dofs
2644ea844a1aSMatthew Knepley 
2645ea844a1aSMatthew Knepley   Level: intermediate
2646ea844a1aSMatthew Knepley 
2647cab54364SBarry Smith   Fortran Note:
264820662ed9SBarry Smith   Use `PetscSectionGetConstraintIndicesF90()` and `PetscSectionRestoreConstraintIndicesF90()`
2649cab54364SBarry Smith 
2650cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSetConstraintIndices()`, `PetscSectionGetConstraintDof()`, `PetscSection`
2651ea844a1aSMatthew Knepley @*/
2652d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetConstraintIndices(PetscSection s, PetscInt point, const PetscInt **indices)
2653d71ae5a4SJacob Faibussowitsch {
2654ea844a1aSMatthew Knepley   PetscFunctionBegin;
2655ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2656ea844a1aSMatthew Knepley   if (s->bc) {
26579566063dSJacob Faibussowitsch     PetscCall(VecIntGetValuesSection(s->bcIndices, s->bc, point, indices));
2658ea844a1aSMatthew Knepley   } else *indices = NULL;
26593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2660ea844a1aSMatthew Knepley }
2661ea844a1aSMatthew Knepley 
2662ea844a1aSMatthew Knepley /*@C
2663ea844a1aSMatthew Knepley   PetscSectionSetConstraintIndices - Set the point dof numbers, in [0, dof), which are constrained
2664ea844a1aSMatthew Knepley 
266540750d41SVaclav Hapla   Not Collective
2666ea844a1aSMatthew Knepley 
2667ea844a1aSMatthew Knepley   Input Parameters:
2668cab54364SBarry Smith + s     - The `PetscSection`
2669ea844a1aSMatthew Knepley . point - The point
2670ea844a1aSMatthew Knepley - indices - The constrained dofs
2671ea844a1aSMatthew Knepley 
2672ea844a1aSMatthew Knepley   Level: intermediate
2673ea844a1aSMatthew Knepley 
2674cab54364SBarry Smith   Fortran Note:
2675cab54364SBarry Smith   Use `PetscSectionSetConstraintIndicesF90()`
2676cab54364SBarry Smith 
2677cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionGetConstraintIndices()`, `PetscSectionGetConstraintDof()`, `PetscSection`
2678ea844a1aSMatthew Knepley @*/
2679d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetConstraintIndices(PetscSection s, PetscInt point, const PetscInt indices[])
2680d71ae5a4SJacob Faibussowitsch {
2681ea844a1aSMatthew Knepley   PetscFunctionBegin;
2682ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2683ea844a1aSMatthew Knepley   if (s->bc) {
2684d57bb9dbSMatthew G. Knepley     const PetscInt dof  = s->atlasDof[point];
2685d57bb9dbSMatthew G. Knepley     const PetscInt cdof = s->bc->atlasDof[point];
2686d57bb9dbSMatthew G. Knepley     PetscInt       d;
2687d57bb9dbSMatthew G. Knepley 
2688ad540459SPierre 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]);
26899566063dSJacob Faibussowitsch     PetscCall(VecIntSetValuesSection(s->bcIndices, s->bc, point, indices, INSERT_VALUES));
2690ea844a1aSMatthew Knepley   }
26913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2692ea844a1aSMatthew Knepley }
2693ea844a1aSMatthew Knepley 
2694ea844a1aSMatthew Knepley /*@C
2695ea844a1aSMatthew Knepley   PetscSectionGetFieldConstraintIndices - Get the field dof numbers, in [0, fdof), which are constrained
2696ea844a1aSMatthew Knepley 
269740750d41SVaclav Hapla   Not Collective
2698ea844a1aSMatthew Knepley 
2699ea844a1aSMatthew Knepley   Input Parameters:
2700cab54364SBarry Smith + s     - The `PetscSection`
2701ea844a1aSMatthew Knepley . field  - The field number
2702ea844a1aSMatthew Knepley - point - The point
2703ea844a1aSMatthew Knepley 
2704ea844a1aSMatthew Knepley   Output Parameter:
27059759eb26SJed Brown . indices - The constrained dofs sorted in ascending order
2706ea844a1aSMatthew Knepley 
2707ea844a1aSMatthew Knepley   Level: intermediate
2708ea844a1aSMatthew Knepley 
2709cab54364SBarry Smith   Note:
2710cab54364SBarry 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()`.
2711cab54364SBarry Smith 
2712cab54364SBarry Smith   Fortran Note:
271320662ed9SBarry Smith   Use `PetscSectionGetFieldConstraintIndicesF90()` and `PetscSectionRestoreFieldConstraintIndicesF90()`
2714cab54364SBarry Smith 
2715cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSetFieldConstraintIndices()`, `PetscSectionGetConstraintIndices()`, `PetscSectionGetConstraintDof()`, `PetscSection`
2716ea844a1aSMatthew Knepley @*/
2717d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldConstraintIndices(PetscSection s, PetscInt point, PetscInt field, const PetscInt **indices)
2718d71ae5a4SJacob Faibussowitsch {
2719ea844a1aSMatthew Knepley   PetscFunctionBegin;
2720ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2721dadcf809SJacob Faibussowitsch   PetscValidPointer(indices, 4);
27222abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
27239566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstraintIndices(s->field[field], point, indices));
27243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2725ea844a1aSMatthew Knepley }
2726ea844a1aSMatthew Knepley 
2727ea844a1aSMatthew Knepley /*@C
2728ea844a1aSMatthew Knepley   PetscSectionSetFieldConstraintIndices - Set the field dof numbers, in [0, fdof), which are constrained
2729ea844a1aSMatthew Knepley 
273040750d41SVaclav Hapla   Not Collective
2731ea844a1aSMatthew Knepley 
2732ea844a1aSMatthew Knepley   Input Parameters:
2733cab54364SBarry Smith + s       - The `PetscSection`
2734ea844a1aSMatthew Knepley . point   - The point
2735ea844a1aSMatthew Knepley . field   - The field number
2736ea844a1aSMatthew Knepley - indices - The constrained dofs
2737ea844a1aSMatthew Knepley 
2738ea844a1aSMatthew Knepley   Level: intermediate
2739ea844a1aSMatthew Knepley 
2740cab54364SBarry Smith   Fortran Note:
2741cab54364SBarry Smith   Use `PetscSectionSetFieldConstraintIndicesF90()`
2742cab54364SBarry Smith 
2743cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSetConstraintIndices()`, `PetscSectionGetFieldConstraintIndices()`, `PetscSectionGetConstraintDof()`, `PetscSection`
2744ea844a1aSMatthew Knepley @*/
2745d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFieldConstraintIndices(PetscSection s, PetscInt point, PetscInt field, const PetscInt indices[])
2746d71ae5a4SJacob Faibussowitsch {
2747ea844a1aSMatthew Knepley   PetscFunctionBegin;
2748ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
27492abc8c78SJacob Faibussowitsch   if (PetscDefined(USE_DEBUG)) {
2750e2bfaee7SMatthew G. Knepley     PetscInt nfdof;
27512abc8c78SJacob Faibussowitsch 
27529566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldConstraintDof(s, point, field, &nfdof));
2753e2bfaee7SMatthew G. Knepley     if (nfdof) PetscValidIntPointer(indices, 4);
27542abc8c78SJacob Faibussowitsch   }
27552abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
27569566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetConstraintIndices(s->field[field], point, indices));
27573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2758ea844a1aSMatthew Knepley }
2759ea844a1aSMatthew Knepley 
2760ea844a1aSMatthew Knepley /*@
2761ea844a1aSMatthew Knepley   PetscSectionPermute - Reorder the section according to the input point permutation
2762ea844a1aSMatthew Knepley 
276340750d41SVaclav Hapla   Collective
2764ea844a1aSMatthew Knepley 
2765d8d19677SJose E. Roman   Input Parameters:
2766cab54364SBarry Smith + section - The `PetscSection` object
2767ea844a1aSMatthew Knepley - perm - The point permutation, old point p becomes new point perm[p]
2768ea844a1aSMatthew Knepley 
2769ea844a1aSMatthew Knepley   Output Parameter:
2770cab54364SBarry Smith . sectionNew - The permuted `PetscSection`
2771ea844a1aSMatthew Knepley 
2772ea844a1aSMatthew Knepley   Level: intermediate
2773ea844a1aSMatthew Knepley 
2774*583308b6SBarry Smith   Note:
2775*583308b6SBarry Smith   The data and the access to the data via `PetscSectionGetFieldOffset()` and `PetscSectionGetOffset()` are both changed in `sectionNew`
2776*583308b6SBarry Smith 
2777*583308b6SBarry Smith   Compare to `PetscSectionSetPermutation()`
2778*583308b6SBarry Smith 
2779*583308b6SBarry Smith .seealso: [PetscSection](sec_petscsection), `IS`, `PetscSection`, `MatPermute()`, `PetscSectionSetPermutation()`
2780ea844a1aSMatthew Knepley @*/
2781d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionPermute(PetscSection section, IS permutation, PetscSection *sectionNew)
2782d71ae5a4SJacob Faibussowitsch {
2783ea844a1aSMatthew Knepley   PetscSection    s = section, sNew;
2784ea844a1aSMatthew Knepley   const PetscInt *perm;
2785b778fa18SValeria Barra   PetscInt        numFields, f, c, numPoints, pStart, pEnd, p;
2786ea844a1aSMatthew Knepley 
2787ea844a1aSMatthew Knepley   PetscFunctionBegin;
2788ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
2789ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(permutation, IS_CLASSID, 2);
2790ea844a1aSMatthew Knepley   PetscValidPointer(sectionNew, 3);
27919566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s), &sNew));
27929566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &numFields));
27939566063dSJacob Faibussowitsch   if (numFields) PetscCall(PetscSectionSetNumFields(sNew, numFields));
2794ea844a1aSMatthew Knepley   for (f = 0; f < numFields; ++f) {
2795ea844a1aSMatthew Knepley     const char *name;
2796ea844a1aSMatthew Knepley     PetscInt    numComp;
2797ea844a1aSMatthew Knepley 
27989566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldName(s, f, &name));
27999566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldName(sNew, f, name));
28009566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &numComp));
28019566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(sNew, f, numComp));
2802b778fa18SValeria Barra     for (c = 0; c < s->numFieldComponents[f]; ++c) {
28039566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetComponentName(s, f, c, &name));
28049566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetComponentName(sNew, f, c, name));
2805b778fa18SValeria Barra     }
2806ea844a1aSMatthew Knepley   }
28079566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(permutation, &numPoints));
28089566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(permutation, &perm));
28099566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
28109566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(sNew, pStart, pEnd));
281108401ef6SPierre 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);
2812ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
2813ea844a1aSMatthew Knepley     PetscInt dof, cdof;
2814ea844a1aSMatthew Knepley 
28159566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s, p, &dof));
28169566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(sNew, perm[p], dof));
28179566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
28189566063dSJacob Faibussowitsch     if (cdof) PetscCall(PetscSectionSetConstraintDof(sNew, perm[p], cdof));
2819ea844a1aSMatthew Knepley     for (f = 0; f < numFields; ++f) {
28209566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
28219566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(sNew, perm[p], f, dof));
28229566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(s, p, f, &cdof));
28239566063dSJacob Faibussowitsch       if (cdof) PetscCall(PetscSectionSetFieldConstraintDof(sNew, perm[p], f, cdof));
2824ea844a1aSMatthew Knepley     }
2825ea844a1aSMatthew Knepley   }
28269566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(sNew));
2827ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
2828ea844a1aSMatthew Knepley     const PetscInt *cind;
2829ea844a1aSMatthew Knepley     PetscInt        cdof;
2830ea844a1aSMatthew Knepley 
28319566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
2832ea844a1aSMatthew Knepley     if (cdof) {
28339566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetConstraintIndices(s, p, &cind));
28349566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetConstraintIndices(sNew, perm[p], cind));
2835ea844a1aSMatthew Knepley     }
2836ea844a1aSMatthew Knepley     for (f = 0; f < numFields; ++f) {
28379566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(s, p, f, &cdof));
2838ea844a1aSMatthew Knepley       if (cdof) {
28399566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldConstraintIndices(s, p, f, &cind));
28409566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetFieldConstraintIndices(sNew, perm[p], f, cind));
2841ea844a1aSMatthew Knepley       }
2842ea844a1aSMatthew Knepley     }
2843ea844a1aSMatthew Knepley   }
28449566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(permutation, &perm));
2845ea844a1aSMatthew Knepley   *sectionNew = sNew;
28463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2847ea844a1aSMatthew Knepley }
2848ea844a1aSMatthew Knepley 
2849ea844a1aSMatthew Knepley /*@
285020662ed9SBarry Smith   PetscSectionSetClosureIndex - Create an internal data structure to speed up closure queries.
2851ea844a1aSMatthew Knepley 
285240750d41SVaclav Hapla   Collective
2853ea844a1aSMatthew Knepley 
2854ea844a1aSMatthew Knepley   Input Parameters:
2855cab54364SBarry Smith + section   - The `PetscSection`
2856cab54364SBarry Smith . obj       - A `PetscObject` which serves as the key for this index
2857cab54364SBarry Smith . clSection - `PetscSection` giving the size of the closure of each point
2858cab54364SBarry Smith - clPoints  - `IS` giving the points in each closure
2859ea844a1aSMatthew Knepley 
2860ea844a1aSMatthew Knepley   Level: advanced
2861ea844a1aSMatthew Knepley 
2862cab54364SBarry Smith   Note:
286320662ed9SBarry Smith   This function creates an internal map from each point to its closure. We compress out closure points with no dofs in this section.
286420662ed9SBarry Smith 
286520662ed9SBarry Smith   Developer Note:
286620662ed9SBarry Smith   The information provided here is completely opaque
2867cab54364SBarry Smith 
2868cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetClosureIndex()`, `DMPlexCreateClosureIndex()`
2869ea844a1aSMatthew Knepley @*/
2870d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetClosureIndex(PetscSection section, PetscObject obj, PetscSection clSection, IS clPoints)
2871d71ae5a4SJacob Faibussowitsch {
2872ea844a1aSMatthew Knepley   PetscFunctionBegin;
28733e03ebd8SStefano Zampini   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
28743e03ebd8SStefano Zampini   PetscValidHeaderSpecific(clSection, PETSC_SECTION_CLASSID, 3);
28753e03ebd8SStefano Zampini   PetscValidHeaderSpecific(clPoints, IS_CLASSID, 4);
28769566063dSJacob Faibussowitsch   if (section->clObj != obj) PetscCall(PetscSectionResetClosurePermutation(section));
2877ea844a1aSMatthew Knepley   section->clObj = obj;
28789566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)clSection));
28799566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)clPoints));
28809566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&section->clSection));
28819566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&section->clPoints));
2882ea844a1aSMatthew Knepley   section->clSection = clSection;
2883ea844a1aSMatthew Knepley   section->clPoints  = clPoints;
28843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2885ea844a1aSMatthew Knepley }
2886ea844a1aSMatthew Knepley 
2887ea844a1aSMatthew Knepley /*@
2888cab54364SBarry Smith   PetscSectionGetClosureIndex - Get the cache of points in the closure of each point in the section set with `PetscSectionSetClosureIndex()`
2889ea844a1aSMatthew Knepley 
289040750d41SVaclav Hapla   Collective
2891ea844a1aSMatthew Knepley 
2892ea844a1aSMatthew Knepley   Input Parameters:
2893cab54364SBarry Smith + section   - The `PetscSection`
2894cab54364SBarry Smith - obj       - A `PetscObject` which serves as the key for this index
2895ea844a1aSMatthew Knepley 
2896ea844a1aSMatthew Knepley   Output Parameters:
2897cab54364SBarry Smith + clSection - `PetscSection` giving the size of the closure of each point
2898cab54364SBarry Smith - clPoints  - `IS` giving the points in each closure
2899ea844a1aSMatthew Knepley 
2900ea844a1aSMatthew Knepley   Level: advanced
2901ea844a1aSMatthew Knepley 
2902cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSetClosureIndex()`, `DMPlexCreateClosureIndex()`
2903ea844a1aSMatthew Knepley @*/
2904d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetClosureIndex(PetscSection section, PetscObject obj, PetscSection *clSection, IS *clPoints)
2905d71ae5a4SJacob Faibussowitsch {
2906ea844a1aSMatthew Knepley   PetscFunctionBegin;
2907ea844a1aSMatthew Knepley   if (section->clObj == obj) {
2908ea844a1aSMatthew Knepley     if (clSection) *clSection = section->clSection;
2909ea844a1aSMatthew Knepley     if (clPoints) *clPoints = section->clPoints;
2910ea844a1aSMatthew Knepley   } else {
2911ea844a1aSMatthew Knepley     if (clSection) *clSection = NULL;
2912ea844a1aSMatthew Knepley     if (clPoints) *clPoints = NULL;
2913ea844a1aSMatthew Knepley   }
29143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2915ea844a1aSMatthew Knepley }
2916ea844a1aSMatthew Knepley 
2917d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetClosurePermutation_Internal(PetscSection section, PetscObject obj, PetscInt depth, PetscInt clSize, PetscCopyMode mode, PetscInt *clPerm)
2918d71ae5a4SJacob Faibussowitsch {
2919ea844a1aSMatthew Knepley   PetscInt                    i;
2920c459fbc1SJed Brown   khiter_t                    iter;
2921c459fbc1SJed Brown   int                         new_entry;
2922c459fbc1SJed Brown   PetscSectionClosurePermKey  key = {depth, clSize};
2923c459fbc1SJed Brown   PetscSectionClosurePermVal *val;
2924ea844a1aSMatthew Knepley 
2925ea844a1aSMatthew Knepley   PetscFunctionBegin;
2926ea844a1aSMatthew Knepley   if (section->clObj != obj) {
29279566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&section->clSection));
29289566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&section->clPoints));
2929ea844a1aSMatthew Knepley   }
2930ea844a1aSMatthew Knepley   section->clObj = obj;
29319566063dSJacob Faibussowitsch   if (!section->clHash) PetscCall(PetscClPermCreate(&section->clHash));
2932c459fbc1SJed Brown   iter = kh_put(ClPerm, section->clHash, key, &new_entry);
2933c459fbc1SJed Brown   val  = &kh_val(section->clHash, iter);
2934c459fbc1SJed Brown   if (!new_entry) {
29359566063dSJacob Faibussowitsch     PetscCall(PetscFree(val->perm));
29369566063dSJacob Faibussowitsch     PetscCall(PetscFree(val->invPerm));
2937c459fbc1SJed Brown   }
2938ea844a1aSMatthew Knepley   if (mode == PETSC_COPY_VALUES) {
29399566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(clSize, &val->perm));
29409566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(val->perm, clPerm, clSize));
2941ea844a1aSMatthew Knepley   } else if (mode == PETSC_OWN_POINTER) {
2942c459fbc1SJed Brown     val->perm = clPerm;
2943ea844a1aSMatthew Knepley   } else SETERRQ(PetscObjectComm(obj), PETSC_ERR_SUP, "Do not support borrowed arrays");
29449566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(clSize, &val->invPerm));
2945c459fbc1SJed Brown   for (i = 0; i < clSize; ++i) val->invPerm[clPerm[i]] = i;
29463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2947ea844a1aSMatthew Knepley }
2948ea844a1aSMatthew Knepley 
2949ea844a1aSMatthew Knepley /*@
2950c459fbc1SJed Brown   PetscSectionSetClosurePermutation - Set the dof permutation for the closure of each cell in the section, meaning clPerm[newIndex] = oldIndex.
2951ea844a1aSMatthew Knepley 
2952ea844a1aSMatthew Knepley   Not Collective
2953ea844a1aSMatthew Knepley 
2954ea844a1aSMatthew Knepley   Input Parameters:
2955cab54364SBarry Smith + section - The `PetscSection`
2956cab54364SBarry Smith . obj     - A `PetscObject` which serves as the key for this index (usually a `DM`)
2957c459fbc1SJed Brown . depth   - Depth of points on which to apply the given permutation
2958ea844a1aSMatthew Knepley - perm    - Permutation of the cell dof closure
2959ea844a1aSMatthew Knepley 
2960cab54364SBarry Smith   Level: intermediate
2961cab54364SBarry Smith 
2962cab54364SBarry Smith   Notes:
2963c459fbc1SJed Brown   The specified permutation will only be applied to points at depth whose closure size matches the length of perm.  In a
2964c459fbc1SJed Brown   mixed-topology or variable-degree finite element space, this function can be called multiple times at each depth for
2965c459fbc1SJed Brown   each topology and degree.
2966c459fbc1SJed Brown 
2967c459fbc1SJed Brown   This approach assumes that (depth, len(perm)) uniquely identifies the desired permutation; this might not be true for
2968c459fbc1SJed Brown   exotic/enriched spaces on mixed topology meshes.
2969ea844a1aSMatthew Knepley 
2970cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `IS`, `PetscSectionGetClosurePermutation()`, `PetscSectionGetClosureIndex()`, `DMPlexCreateClosureIndex()`, `PetscCopyMode`
2971ea844a1aSMatthew Knepley @*/
2972d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetClosurePermutation(PetscSection section, PetscObject obj, PetscInt depth, IS perm)
2973d71ae5a4SJacob Faibussowitsch {
2974ea844a1aSMatthew Knepley   const PetscInt *clPerm = NULL;
2975ea844a1aSMatthew Knepley   PetscInt        clSize = 0;
2976ea844a1aSMatthew Knepley 
2977ea844a1aSMatthew Knepley   PetscFunctionBegin;
2978ea844a1aSMatthew Knepley   if (perm) {
29799566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(perm, &clSize));
29809566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(perm, &clPerm));
2981ea844a1aSMatthew Knepley   }
29829566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetClosurePermutation_Internal(section, obj, depth, clSize, PETSC_COPY_VALUES, (PetscInt *)clPerm));
29839566063dSJacob Faibussowitsch   if (perm) PetscCall(ISRestoreIndices(perm, &clPerm));
29843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2985ea844a1aSMatthew Knepley }
2986ea844a1aSMatthew Knepley 
2987d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetClosurePermutation_Internal(PetscSection section, PetscObject obj, PetscInt depth, PetscInt size, const PetscInt *perm[])
2988d71ae5a4SJacob Faibussowitsch {
2989ea844a1aSMatthew Knepley   PetscFunctionBegin;
2990ea844a1aSMatthew Knepley   if (section->clObj == obj) {
2991c459fbc1SJed Brown     PetscSectionClosurePermKey k = {depth, size};
2992c459fbc1SJed Brown     PetscSectionClosurePermVal v;
29939566063dSJacob Faibussowitsch     PetscCall(PetscClPermGet(section->clHash, k, &v));
2994c459fbc1SJed Brown     if (perm) *perm = v.perm;
2995ea844a1aSMatthew Knepley   } else {
2996ea844a1aSMatthew Knepley     if (perm) *perm = NULL;
2997ea844a1aSMatthew Knepley   }
29983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2999ea844a1aSMatthew Knepley }
3000ea844a1aSMatthew Knepley 
3001ea844a1aSMatthew Knepley /*@
3002ea844a1aSMatthew Knepley   PetscSectionGetClosurePermutation - Get the dof permutation for the closure of each cell in the section, meaning clPerm[newIndex] = oldIndex.
3003ea844a1aSMatthew Knepley 
300440750d41SVaclav Hapla   Not Collective
3005ea844a1aSMatthew Knepley 
3006ea844a1aSMatthew Knepley   Input Parameters:
3007cab54364SBarry Smith + section   - The `PetscSection`
3008cab54364SBarry Smith . obj       - A `PetscObject` which serves as the key for this index (usually a DM)
3009c459fbc1SJed Brown . depth     - Depth stratum on which to obtain closure permutation
3010c459fbc1SJed Brown - clSize    - Closure size to be permuted (e.g., may vary with element topology and degree)
3011ea844a1aSMatthew Knepley 
3012ea844a1aSMatthew Knepley   Output Parameter:
3013ea844a1aSMatthew Knepley . perm - The dof closure permutation
3014ea844a1aSMatthew Knepley 
3015ea844a1aSMatthew Knepley   Level: intermediate
3016ea844a1aSMatthew Knepley 
3017cab54364SBarry Smith   Note:
3018cab54364SBarry Smith   The user must destroy the `IS` that is returned.
3019cab54364SBarry Smith 
3020cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `IS`, `PetscSectionSetClosurePermutation()`, `PetscSectionGetClosureInversePermutation()`, `PetscSectionGetClosureIndex()`, `PetscSectionSetClosureIndex()`, `DMPlexCreateClosureIndex()`
3021ea844a1aSMatthew Knepley @*/
3022d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetClosurePermutation(PetscSection section, PetscObject obj, PetscInt depth, PetscInt clSize, IS *perm)
3023d71ae5a4SJacob Faibussowitsch {
3024ea844a1aSMatthew Knepley   const PetscInt *clPerm;
3025ea844a1aSMatthew Knepley 
3026ea844a1aSMatthew Knepley   PetscFunctionBegin;
30279566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetClosurePermutation_Internal(section, obj, depth, clSize, &clPerm));
30289566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, clSize, clPerm, PETSC_USE_POINTER, perm));
30293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3030ea844a1aSMatthew Knepley }
3031ea844a1aSMatthew Knepley 
3032d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetClosureInversePermutation_Internal(PetscSection section, PetscObject obj, PetscInt depth, PetscInt size, const PetscInt *perm[])
3033d71ae5a4SJacob Faibussowitsch {
3034ea844a1aSMatthew Knepley   PetscFunctionBegin;
3035c459fbc1SJed Brown   if (section->clObj == obj && section->clHash) {
3036c459fbc1SJed Brown     PetscSectionClosurePermKey k = {depth, size};
3037c459fbc1SJed Brown     PetscSectionClosurePermVal v;
30389566063dSJacob Faibussowitsch     PetscCall(PetscClPermGet(section->clHash, k, &v));
3039c459fbc1SJed Brown     if (perm) *perm = v.invPerm;
3040ea844a1aSMatthew Knepley   } else {
3041ea844a1aSMatthew Knepley     if (perm) *perm = NULL;
3042ea844a1aSMatthew Knepley   }
30433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3044ea844a1aSMatthew Knepley }
3045ea844a1aSMatthew Knepley 
3046ea844a1aSMatthew Knepley /*@
3047ea844a1aSMatthew Knepley   PetscSectionGetClosureInversePermutation - Get the inverse dof permutation for the closure of each cell in the section, meaning clPerm[oldIndex] = newIndex.
3048ea844a1aSMatthew Knepley 
304940750d41SVaclav Hapla   Not Collective
3050ea844a1aSMatthew Knepley 
3051ea844a1aSMatthew Knepley   Input Parameters:
3052cab54364SBarry Smith + section   - The `PetscSection`
3053cab54364SBarry Smith . obj       - A `PetscObject` which serves as the key for this index (usually a `DM`)
3054c459fbc1SJed Brown . depth     - Depth stratum on which to obtain closure permutation
3055c459fbc1SJed Brown - clSize    - Closure size to be permuted (e.g., may vary with element topology and degree)
3056ea844a1aSMatthew Knepley 
30572fe279fdSBarry Smith   Output Parameter:
3058c459fbc1SJed Brown . perm - The dof closure permutation
3059ea844a1aSMatthew Knepley 
3060ea844a1aSMatthew Knepley   Level: intermediate
3061ea844a1aSMatthew Knepley 
3062cab54364SBarry Smith   Note:
3063cab54364SBarry Smith   The user must destroy the `IS` that is returned.
3064cab54364SBarry Smith 
3065cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `IS`, `PetscSectionSetClosurePermutation()`, `PetscSectionGetClosureIndex()`, `PetscSectionSetClosureIndex()`, `DMPlexCreateClosureIndex()`
3066ea844a1aSMatthew Knepley @*/
3067d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetClosureInversePermutation(PetscSection section, PetscObject obj, PetscInt depth, PetscInt clSize, IS *perm)
3068d71ae5a4SJacob Faibussowitsch {
3069ea844a1aSMatthew Knepley   const PetscInt *clPerm;
3070ea844a1aSMatthew Knepley 
3071ea844a1aSMatthew Knepley   PetscFunctionBegin;
30729566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetClosureInversePermutation_Internal(section, obj, depth, clSize, &clPerm));
30739566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, clSize, clPerm, PETSC_USE_POINTER, perm));
30743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3075ea844a1aSMatthew Knepley }
3076ea844a1aSMatthew Knepley 
3077ea844a1aSMatthew Knepley /*@
3078*583308b6SBarry Smith   PetscSectionGetField - Get the `PetscSection` associated with a single field
3079ea844a1aSMatthew Knepley 
3080ea844a1aSMatthew Knepley   Input Parameters:
3081cab54364SBarry Smith + s     - The `PetscSection`
3082ea844a1aSMatthew Knepley - field - The field number
3083ea844a1aSMatthew Knepley 
3084ea844a1aSMatthew Knepley   Output Parameter:
3085*583308b6SBarry Smith . subs  - The `PetscSection` for the given field, note the chart of `subs` is not set
3086ea844a1aSMatthew Knepley 
3087ea844a1aSMatthew Knepley   Level: intermediate
3088ea844a1aSMatthew Knepley 
3089cab54364SBarry Smith   Note:
3090cab54364SBarry Smith   Does not increase the reference count of the selected sub-section. There is no matching `PetscSectionRestoreField()`
3091cab54364SBarry Smith 
3092cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `IS`, `PetscSectionSetNumFields()`
3093ea844a1aSMatthew Knepley @*/
3094d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetField(PetscSection s, PetscInt field, PetscSection *subs)
3095d71ae5a4SJacob Faibussowitsch {
3096ea844a1aSMatthew Knepley   PetscFunctionBegin;
3097ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
3098ea844a1aSMatthew Knepley   PetscValidPointer(subs, 3);
30992abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
3100ea844a1aSMatthew Knepley   *subs = s->field[field];
31013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3102ea844a1aSMatthew Knepley }
3103ea844a1aSMatthew Knepley 
3104ea844a1aSMatthew Knepley PetscClassId      PETSC_SECTION_SYM_CLASSID;
3105ea844a1aSMatthew Knepley PetscFunctionList PetscSectionSymList = NULL;
3106ea844a1aSMatthew Knepley 
3107ea844a1aSMatthew Knepley /*@
3108cab54364SBarry Smith   PetscSectionSymCreate - Creates an empty `PetscSectionSym` object.
3109ea844a1aSMatthew Knepley 
3110ea844a1aSMatthew Knepley   Collective
3111ea844a1aSMatthew Knepley 
3112ea844a1aSMatthew Knepley   Input Parameter:
3113ea844a1aSMatthew Knepley . comm - the MPI communicator
3114ea844a1aSMatthew Knepley 
3115ea844a1aSMatthew Knepley   Output Parameter:
3116ea844a1aSMatthew Knepley . sym - pointer to the new set of symmetries
3117ea844a1aSMatthew Knepley 
3118ea844a1aSMatthew Knepley   Level: developer
3119ea844a1aSMatthew Knepley 
3120cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSym`, `PetscSectionSymDestroy()`
3121ea844a1aSMatthew Knepley @*/
3122d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymCreate(MPI_Comm comm, PetscSectionSym *sym)
3123d71ae5a4SJacob Faibussowitsch {
3124ea844a1aSMatthew Knepley   PetscFunctionBegin;
3125ea844a1aSMatthew Knepley   PetscValidPointer(sym, 2);
31269566063dSJacob Faibussowitsch   PetscCall(ISInitializePackage());
31279566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(*sym, PETSC_SECTION_SYM_CLASSID, "PetscSectionSym", "Section Symmetry", "IS", comm, PetscSectionSymDestroy, PetscSectionSymView));
31283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3129ea844a1aSMatthew Knepley }
3130ea844a1aSMatthew Knepley 
3131ea844a1aSMatthew Knepley /*@C
3132cab54364SBarry Smith   PetscSectionSymSetType - Builds a `PetscSectionSym`, for a particular implementation.
3133ea844a1aSMatthew Knepley 
313440750d41SVaclav Hapla   Collective
3135ea844a1aSMatthew Knepley 
3136ea844a1aSMatthew Knepley   Input Parameters:
3137ea844a1aSMatthew Knepley + sym    - The section symmetry object
3138ea844a1aSMatthew Knepley - method - The name of the section symmetry type
3139ea844a1aSMatthew Knepley 
3140ea844a1aSMatthew Knepley   Level: developer
3141ea844a1aSMatthew Knepley 
3142cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionSymType`, `PetscSectionSymGetType()`, `PetscSectionSymCreate()`
3143ea844a1aSMatthew Knepley @*/
3144d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymSetType(PetscSectionSym sym, PetscSectionSymType method)
3145d71ae5a4SJacob Faibussowitsch {
3146ea844a1aSMatthew Knepley   PetscErrorCode (*r)(PetscSectionSym);
3147ea844a1aSMatthew Knepley   PetscBool match;
3148ea844a1aSMatthew Knepley 
3149ea844a1aSMatthew Knepley   PetscFunctionBegin;
3150ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(sym, PETSC_SECTION_SYM_CLASSID, 1);
31519566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)sym, method, &match));
31523ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
3153ea844a1aSMatthew Knepley 
31549566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(PetscSectionSymList, method, &r));
315528b400f6SJacob Faibussowitsch   PetscCheck(r, PETSC_COMM_SELF, PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscSectionSym type: %s", method);
3156dbbe0bcdSBarry Smith   PetscTryTypeMethod(sym, destroy);
3157ea844a1aSMatthew Knepley   sym->ops->destroy = NULL;
3158dbbe0bcdSBarry Smith 
31599566063dSJacob Faibussowitsch   PetscCall((*r)(sym));
31609566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)sym, method));
31613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3162ea844a1aSMatthew Knepley }
3163ea844a1aSMatthew Knepley 
3164ea844a1aSMatthew Knepley /*@C
3165cab54364SBarry Smith   PetscSectionSymGetType - Gets the section symmetry type name (as a string) from the `PetscSectionSym`.
3166ea844a1aSMatthew Knepley 
3167ea844a1aSMatthew Knepley   Not Collective
3168ea844a1aSMatthew Knepley 
3169ea844a1aSMatthew Knepley   Input Parameter:
3170ea844a1aSMatthew Knepley . sym  - The section symmetry
3171ea844a1aSMatthew Knepley 
3172ea844a1aSMatthew Knepley   Output Parameter:
3173ea844a1aSMatthew Knepley . type - The index set type name
3174ea844a1aSMatthew Knepley 
3175ea844a1aSMatthew Knepley   Level: developer
3176ea844a1aSMatthew Knepley 
3177cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionSymType`, `PetscSectionSymSetType()`, `PetscSectionSymCreate()`
3178ea844a1aSMatthew Knepley @*/
3179d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymGetType(PetscSectionSym sym, PetscSectionSymType *type)
3180d71ae5a4SJacob Faibussowitsch {
3181ea844a1aSMatthew Knepley   PetscFunctionBegin;
3182ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(sym, PETSC_SECTION_SYM_CLASSID, 1);
3183dadcf809SJacob Faibussowitsch   PetscValidPointer(type, 2);
3184ea844a1aSMatthew Knepley   *type = ((PetscObject)sym)->type_name;
31853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3186ea844a1aSMatthew Knepley }
3187ea844a1aSMatthew Knepley 
3188ea844a1aSMatthew Knepley /*@C
3189cab54364SBarry Smith   PetscSectionSymRegister - Registers a new section symmetry implementation
3190ea844a1aSMatthew Knepley 
3191ea844a1aSMatthew Knepley   Not Collective
3192ea844a1aSMatthew Knepley 
3193ea844a1aSMatthew Knepley   Input Parameters:
31942fe279fdSBarry Smith + sname        - The name of a new user-defined creation routine
31952fe279fdSBarry Smith - function - The creation routine itself
3196ea844a1aSMatthew Knepley 
3197ea844a1aSMatthew Knepley   Level: developer
3198ea844a1aSMatthew Knepley 
3199cab54364SBarry Smith   Notes:
3200cab54364SBarry Smith   `PetscSectionSymRegister()` may be called multiple times to add several user-defined vectors
3201cab54364SBarry Smith 
3202cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionSymType`, `PetscSectionSymCreate()`, `PetscSectionSymSetType()`
3203ea844a1aSMatthew Knepley @*/
3204d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymRegister(const char sname[], PetscErrorCode (*function)(PetscSectionSym))
3205d71ae5a4SJacob Faibussowitsch {
3206ea844a1aSMatthew Knepley   PetscFunctionBegin;
32079566063dSJacob Faibussowitsch   PetscCall(ISInitializePackage());
32089566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&PetscSectionSymList, sname, function));
32093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3210ea844a1aSMatthew Knepley }
3211ea844a1aSMatthew Knepley 
3212ea844a1aSMatthew Knepley /*@
3213ea844a1aSMatthew Knepley    PetscSectionSymDestroy - Destroys a section symmetry.
3214ea844a1aSMatthew Knepley 
321540750d41SVaclav Hapla    Collective
3216ea844a1aSMatthew Knepley 
32172fe279fdSBarry Smith    Input Parameter:
3218ea844a1aSMatthew Knepley .  sym - the section symmetry
3219ea844a1aSMatthew Knepley 
3220ea844a1aSMatthew Knepley    Level: developer
3221ea844a1aSMatthew Knepley 
3222cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionSymCreate()`, `PetscSectionSymDestroy()`
3223ea844a1aSMatthew Knepley @*/
3224d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymDestroy(PetscSectionSym *sym)
3225d71ae5a4SJacob Faibussowitsch {
3226ea844a1aSMatthew Knepley   SymWorkLink link, next;
3227ea844a1aSMatthew Knepley 
3228ea844a1aSMatthew Knepley   PetscFunctionBegin;
32293ba16761SJacob Faibussowitsch   if (!*sym) PetscFunctionReturn(PETSC_SUCCESS);
3230ea844a1aSMatthew Knepley   PetscValidHeaderSpecific((*sym), PETSC_SECTION_SYM_CLASSID, 1);
32319371c9d4SSatish Balay   if (--((PetscObject)(*sym))->refct > 0) {
32329371c9d4SSatish Balay     *sym = NULL;
32333ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
3234ea844a1aSMatthew Knepley   }
323548a46eb9SPierre Jolivet   if ((*sym)->ops->destroy) PetscCall((*(*sym)->ops->destroy)(*sym));
3236c9cc58a2SBarry Smith   PetscCheck(!(*sym)->workout, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Work array still checked out");
3237ea844a1aSMatthew Knepley   for (link = (*sym)->workin; link; link = next) {
32380c99d500SBarry Smith     PetscInt    **perms = (PetscInt **)link->perms;
32390c99d500SBarry Smith     PetscScalar **rots  = (PetscScalar **)link->rots;
32400c99d500SBarry Smith     PetscCall(PetscFree2(perms, rots));
3241ea844a1aSMatthew Knepley     next = link->next;
32429566063dSJacob Faibussowitsch     PetscCall(PetscFree(link));
3243ea844a1aSMatthew Knepley   }
3244ea844a1aSMatthew Knepley   (*sym)->workin = NULL;
32459566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(sym));
32463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3247ea844a1aSMatthew Knepley }
3248ea844a1aSMatthew Knepley 
3249ea844a1aSMatthew Knepley /*@C
3250ea844a1aSMatthew Knepley    PetscSectionSymView - Displays a section symmetry
3251ea844a1aSMatthew Knepley 
325240750d41SVaclav Hapla    Collective
3253ea844a1aSMatthew Knepley 
3254ea844a1aSMatthew Knepley    Input Parameters:
3255ea844a1aSMatthew Knepley +  sym - the index set
3256cab54364SBarry Smith -  viewer - viewer used to display the set, for example `PETSC_VIEWER_STDOUT_SELF`.
3257ea844a1aSMatthew Knepley 
3258ea844a1aSMatthew Knepley    Level: developer
3259ea844a1aSMatthew Knepley 
3260cab54364SBarry Smith .seealso:  `PetscSectionSym`, `PetscViewer`, `PetscViewerASCIIOpen()`
3261ea844a1aSMatthew Knepley @*/
3262d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymView(PetscSectionSym sym, PetscViewer viewer)
3263d71ae5a4SJacob Faibussowitsch {
3264ea844a1aSMatthew Knepley   PetscFunctionBegin;
3265ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(sym, PETSC_SECTION_SYM_CLASSID, 1);
326648a46eb9SPierre Jolivet   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)sym), &viewer));
3267ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
3268ea844a1aSMatthew Knepley   PetscCheckSameComm(sym, 1, viewer, 2);
32699566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)sym, viewer));
3270dbbe0bcdSBarry Smith   PetscTryTypeMethod(sym, view, viewer);
32713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3272ea844a1aSMatthew Knepley }
3273ea844a1aSMatthew Knepley 
3274ea844a1aSMatthew Knepley /*@
3275ea844a1aSMatthew Knepley   PetscSectionSetSym - Set the symmetries for the data referred to by the section
3276ea844a1aSMatthew Knepley 
327740750d41SVaclav Hapla   Collective
3278ea844a1aSMatthew Knepley 
3279ea844a1aSMatthew Knepley   Input Parameters:
3280ea844a1aSMatthew Knepley + section - the section describing data layout
3281ea844a1aSMatthew Knepley - sym - the symmetry describing the affect of orientation on the access of the data
3282ea844a1aSMatthew Knepley 
3283ea844a1aSMatthew Knepley   Level: developer
3284ea844a1aSMatthew Knepley 
3285cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionGetSym()`, `PetscSectionSymCreate()`
3286ea844a1aSMatthew Knepley @*/
3287d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetSym(PetscSection section, PetscSectionSym sym)
3288d71ae5a4SJacob Faibussowitsch {
3289ea844a1aSMatthew Knepley   PetscFunctionBegin;
3290ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
32919566063dSJacob Faibussowitsch   PetscCall(PetscSectionSymDestroy(&(section->sym)));
3292ea844a1aSMatthew Knepley   if (sym) {
3293ea844a1aSMatthew Knepley     PetscValidHeaderSpecific(sym, PETSC_SECTION_SYM_CLASSID, 2);
3294ea844a1aSMatthew Knepley     PetscCheckSameComm(section, 1, sym, 2);
32959566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)sym));
3296ea844a1aSMatthew Knepley   }
3297ea844a1aSMatthew Knepley   section->sym = sym;
32983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3299ea844a1aSMatthew Knepley }
3300ea844a1aSMatthew Knepley 
3301ea844a1aSMatthew Knepley /*@
3302ea844a1aSMatthew Knepley   PetscSectionGetSym - Get the symmetries for the data referred to by the section
3303ea844a1aSMatthew Knepley 
330440750d41SVaclav Hapla   Not Collective
3305ea844a1aSMatthew Knepley 
33062fe279fdSBarry Smith   Input Parameter:
3307ea844a1aSMatthew Knepley . section - the section describing data layout
3308ea844a1aSMatthew Knepley 
33092fe279fdSBarry Smith   Output Parameter:
331020662ed9SBarry Smith . sym - the symmetry describing the affect of orientation on the access of the data, provided previously by `PetscSectionSetSym()`
3311ea844a1aSMatthew Knepley 
3312ea844a1aSMatthew Knepley   Level: developer
3313ea844a1aSMatthew Knepley 
3314cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionSetSym()`, `PetscSectionSymCreate()`
3315ea844a1aSMatthew Knepley @*/
3316d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetSym(PetscSection section, PetscSectionSym *sym)
3317d71ae5a4SJacob Faibussowitsch {
3318ea844a1aSMatthew Knepley   PetscFunctionBegin;
3319ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
3320ea844a1aSMatthew Knepley   *sym = section->sym;
33213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3322ea844a1aSMatthew Knepley }
3323ea844a1aSMatthew Knepley 
3324ea844a1aSMatthew Knepley /*@
3325ea844a1aSMatthew Knepley   PetscSectionSetFieldSym - Set the symmetries for the data referred to by a field of the section
3326ea844a1aSMatthew Knepley 
332740750d41SVaclav Hapla   Collective
3328ea844a1aSMatthew Knepley 
3329ea844a1aSMatthew Knepley   Input Parameters:
3330ea844a1aSMatthew Knepley + section - the section describing data layout
3331ea844a1aSMatthew Knepley . field - the field number
3332ea844a1aSMatthew Knepley - sym - the symmetry describing the affect of orientation on the access of the data
3333ea844a1aSMatthew Knepley 
3334ea844a1aSMatthew Knepley   Level: developer
3335ea844a1aSMatthew Knepley 
3336cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionGetFieldSym()`, `PetscSectionSymCreate()`
3337ea844a1aSMatthew Knepley @*/
3338d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFieldSym(PetscSection section, PetscInt field, PetscSectionSym sym)
3339d71ae5a4SJacob Faibussowitsch {
3340ea844a1aSMatthew Knepley   PetscFunctionBegin;
3341ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
33422abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, section->numFields);
33439566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetSym(section->field[field], sym));
33443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3345ea844a1aSMatthew Knepley }
3346ea844a1aSMatthew Knepley 
3347ea844a1aSMatthew Knepley /*@
3348ea844a1aSMatthew Knepley   PetscSectionGetFieldSym - Get the symmetries for the data referred to by a field of the section
3349ea844a1aSMatthew Knepley 
335040750d41SVaclav Hapla   Collective
3351ea844a1aSMatthew Knepley 
3352ea844a1aSMatthew Knepley   Input Parameters:
3353ea844a1aSMatthew Knepley + section - the section describing data layout
3354ea844a1aSMatthew Knepley - field - the field number
3355ea844a1aSMatthew Knepley 
33562fe279fdSBarry Smith   Output Parameter:
3357ea844a1aSMatthew Knepley . sym - the symmetry describing the affect of orientation on the access of the data
3358ea844a1aSMatthew Knepley 
3359ea844a1aSMatthew Knepley   Level: developer
3360ea844a1aSMatthew Knepley 
3361cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionSetFieldSym()`, `PetscSectionSymCreate()`
3362ea844a1aSMatthew Knepley @*/
3363d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldSym(PetscSection section, PetscInt field, PetscSectionSym *sym)
3364d71ae5a4SJacob Faibussowitsch {
3365ea844a1aSMatthew Knepley   PetscFunctionBegin;
3366ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
33672abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, section->numFields);
3368ea844a1aSMatthew Knepley   *sym = section->field[field]->sym;
33693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3370ea844a1aSMatthew Knepley }
3371ea844a1aSMatthew Knepley 
3372ea844a1aSMatthew Knepley /*@C
3373cab54364SBarry Smith   PetscSectionGetPointSyms - Get the symmetries for a set of points in a `PetscSection` under specific orientations.
3374ea844a1aSMatthew Knepley 
337540750d41SVaclav Hapla   Not Collective
3376ea844a1aSMatthew Knepley 
3377ea844a1aSMatthew Knepley   Input Parameters:
3378ea844a1aSMatthew Knepley + section - the section
3379ea844a1aSMatthew Knepley . numPoints - the number of points
338020662ed9SBarry Smith - points - an array of size 2 * `numPoints`, containing a list of (point, orientation) pairs. (An orientation is an
338120662ed9SBarry Smith     arbitrary integer: its interpretation is up to sym.  Orientations are used by `DM`: for their interpretation in that
338220662ed9SBarry Smith     context, see `DMPlexGetConeOrientation()`).
3383ea844a1aSMatthew Knepley 
3384d8d19677SJose E. Roman   Output Parameters:
338520662ed9SBarry Smith + perms - The permutations for the given orientations (or `NULL` if there is no symmetry or the permutation is the identity).
338620662ed9SBarry Smith - rots - The field rotations symmetries for the given orientations (or `NULL` if there is no symmetry or the rotations are all
3387ea844a1aSMatthew Knepley     identity).
3388ea844a1aSMatthew Knepley 
3389ea844a1aSMatthew Knepley   Example of usage, gathering dofs into a local array (lArray) from a section array (sArray):
3390ea844a1aSMatthew Knepley .vb
3391ea844a1aSMatthew Knepley      const PetscInt    **perms;
3392ea844a1aSMatthew Knepley      const PetscScalar **rots;
3393ea844a1aSMatthew Knepley      PetscInt            lOffset;
3394ea844a1aSMatthew Knepley 
3395ea844a1aSMatthew Knepley      PetscSectionGetPointSyms(section,numPoints,points,&perms,&rots);
3396ea844a1aSMatthew Knepley      for (i = 0, lOffset = 0; i < numPoints; i++) {
3397ea844a1aSMatthew Knepley        PetscInt           point = points[2*i], dof, sOffset;
3398ea844a1aSMatthew Knepley        const PetscInt    *perm  = perms ? perms[i] : NULL;
3399ea844a1aSMatthew Knepley        const PetscScalar *rot   = rots  ? rots[i]  : NULL;
3400ea844a1aSMatthew Knepley 
3401ea844a1aSMatthew Knepley        PetscSectionGetDof(section,point,&dof);
3402ea844a1aSMatthew Knepley        PetscSectionGetOffset(section,point,&sOffset);
3403ea844a1aSMatthew Knepley 
3404ea844a1aSMatthew Knepley        if (perm) {for (j = 0; j < dof; j++) {lArray[lOffset + perm[j]]  = sArray[sOffset + j];}}
3405ea844a1aSMatthew Knepley        else      {for (j = 0; j < dof; j++) {lArray[lOffset +      j ]  = sArray[sOffset + j];}}
3406ea844a1aSMatthew Knepley        if (rot)  {for (j = 0; j < dof; j++) {lArray[lOffset +      j ] *= rot[j];             }}
3407ea844a1aSMatthew Knepley        lOffset += dof;
3408ea844a1aSMatthew Knepley      }
3409ea844a1aSMatthew Knepley      PetscSectionRestorePointSyms(section,numPoints,points,&perms,&rots);
3410ea844a1aSMatthew Knepley .ve
3411ea844a1aSMatthew Knepley 
3412ea844a1aSMatthew Knepley   Example of usage, adding dofs into a section array (sArray) from a local array (lArray):
3413ea844a1aSMatthew Knepley .vb
3414ea844a1aSMatthew Knepley      const PetscInt    **perms;
3415ea844a1aSMatthew Knepley      const PetscScalar **rots;
3416ea844a1aSMatthew Knepley      PetscInt            lOffset;
3417ea844a1aSMatthew Knepley 
3418ea844a1aSMatthew Knepley      PetscSectionGetPointSyms(section,numPoints,points,&perms,&rots);
3419ea844a1aSMatthew Knepley      for (i = 0, lOffset = 0; i < numPoints; i++) {
3420ea844a1aSMatthew Knepley        PetscInt           point = points[2*i], dof, sOffset;
3421ea844a1aSMatthew Knepley        const PetscInt    *perm  = perms ? perms[i] : NULL;
3422ea844a1aSMatthew Knepley        const PetscScalar *rot   = rots  ? rots[i]  : NULL;
3423ea844a1aSMatthew Knepley 
3424ea844a1aSMatthew Knepley        PetscSectionGetDof(section,point,&dof);
3425ea844a1aSMatthew Knepley        PetscSectionGetOffset(section,point,&sOff);
3426ea844a1aSMatthew Knepley 
3427ea844a1aSMatthew Knepley        if (perm) {for (j = 0; j < dof; j++) {sArray[sOffset + j] += lArray[lOffset + perm[j]] * (rot ? PetscConj(rot[perm[j]]) : 1.);}}
3428ea844a1aSMatthew Knepley        else      {for (j = 0; j < dof; j++) {sArray[sOffset + j] += lArray[lOffset +      j ] * (rot ? PetscConj(rot[     j ]) : 1.);}}
3429ea844a1aSMatthew Knepley        offset += dof;
3430ea844a1aSMatthew Knepley      }
3431ea844a1aSMatthew Knepley      PetscSectionRestorePointSyms(section,numPoints,points,&perms,&rots);
3432ea844a1aSMatthew Knepley .ve
3433ea844a1aSMatthew Knepley 
3434ea844a1aSMatthew Knepley   Level: developer
3435ea844a1aSMatthew Knepley 
3436*583308b6SBarry Smith   Notes:
343720662ed9SBarry Smith   `PetscSectionSetSym()` must have been previously called to provide the symmetries to the `PetscSection`
343820662ed9SBarry Smith 
343920662ed9SBarry Smith   Use `PetscSectionRestorePointSyms()` when finished with the data
344020662ed9SBarry Smith 
3441cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionRestorePointSyms()`, `PetscSectionSymCreate()`, `PetscSectionSetSym()`, `PetscSectionGetSym()`
3442ea844a1aSMatthew Knepley @*/
3443d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetPointSyms(PetscSection section, PetscInt numPoints, const PetscInt *points, const PetscInt ***perms, const PetscScalar ***rots)
3444d71ae5a4SJacob Faibussowitsch {
3445ea844a1aSMatthew Knepley   PetscSectionSym sym;
3446ea844a1aSMatthew Knepley 
3447ea844a1aSMatthew Knepley   PetscFunctionBegin;
3448ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
3449ea844a1aSMatthew Knepley   if (numPoints) PetscValidIntPointer(points, 3);
3450ea844a1aSMatthew Knepley   if (perms) *perms = NULL;
3451ea844a1aSMatthew Knepley   if (rots) *rots = NULL;
3452ea844a1aSMatthew Knepley   sym = section->sym;
3453ea844a1aSMatthew Knepley   if (sym && (perms || rots)) {
3454ea844a1aSMatthew Knepley     SymWorkLink link;
3455ea844a1aSMatthew Knepley 
3456ea844a1aSMatthew Knepley     if (sym->workin) {
3457ea844a1aSMatthew Knepley       link        = sym->workin;
3458ea844a1aSMatthew Knepley       sym->workin = sym->workin->next;
3459ea844a1aSMatthew Knepley     } else {
34604dfa11a4SJacob Faibussowitsch       PetscCall(PetscNew(&link));
3461ea844a1aSMatthew Knepley     }
3462ea844a1aSMatthew Knepley     if (numPoints > link->numPoints) {
34630c99d500SBarry Smith       PetscInt    **perms = (PetscInt **)link->perms;
34640c99d500SBarry Smith       PetscScalar **rots  = (PetscScalar **)link->rots;
34650c99d500SBarry Smith       PetscCall(PetscFree2(perms, rots));
34660c99d500SBarry Smith       PetscCall(PetscMalloc2(numPoints, (PetscInt ***)&link->perms, numPoints, (PetscScalar ***)&link->rots));
3467ea844a1aSMatthew Knepley       link->numPoints = numPoints;
3468ea844a1aSMatthew Knepley     }
3469ea844a1aSMatthew Knepley     link->next   = sym->workout;
3470ea844a1aSMatthew Knepley     sym->workout = link;
34719566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero((PetscInt **)link->perms, numPoints));
34729566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero((PetscInt **)link->rots, numPoints));
34739566063dSJacob Faibussowitsch     PetscCall((*sym->ops->getpoints)(sym, section, numPoints, points, link->perms, link->rots));
3474ea844a1aSMatthew Knepley     if (perms) *perms = link->perms;
3475ea844a1aSMatthew Knepley     if (rots) *rots = link->rots;
3476ea844a1aSMatthew Knepley   }
34773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3478ea844a1aSMatthew Knepley }
3479ea844a1aSMatthew Knepley 
3480ea844a1aSMatthew Knepley /*@C
3481cab54364SBarry Smith   PetscSectionRestorePointSyms - Restore the symmetries returned by `PetscSectionGetPointSyms()`
3482ea844a1aSMatthew Knepley 
348340750d41SVaclav Hapla   Not Collective
3484ea844a1aSMatthew Knepley 
3485ea844a1aSMatthew Knepley   Input Parameters:
3486ea844a1aSMatthew Knepley + section - the section
3487ea844a1aSMatthew Knepley . numPoints - the number of points
348820662ed9SBarry Smith - points - an array of size 2 * `numPoints`, containing a list of (point, orientation) pairs. (An orientation is an
3489cab54364SBarry Smith     arbitrary integer: its interpretation is up to sym.  Orientations are used by `DM`: for their interpretation in that
3490cab54364SBarry Smith     context, see `DMPlexGetConeOrientation()`).
349120662ed9SBarry Smith . perms - The permutations for the given orientations: set to `NULL` at conclusion
349220662ed9SBarry Smith - rots - The field rotations symmetries for the given orientations: set to `NULL` at conclusion
3493ea844a1aSMatthew Knepley 
3494ea844a1aSMatthew Knepley   Level: developer
3495ea844a1aSMatthew Knepley 
3496cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionGetPointSyms()`, `PetscSectionSymCreate()`, `PetscSectionSetSym()`, `PetscSectionGetSym()`
3497ea844a1aSMatthew Knepley @*/
3498d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionRestorePointSyms(PetscSection section, PetscInt numPoints, const PetscInt *points, const PetscInt ***perms, const PetscScalar ***rots)
3499d71ae5a4SJacob Faibussowitsch {
3500ea844a1aSMatthew Knepley   PetscSectionSym sym;
3501ea844a1aSMatthew Knepley 
3502ea844a1aSMatthew Knepley   PetscFunctionBegin;
3503ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
3504ea844a1aSMatthew Knepley   sym = section->sym;
3505ea844a1aSMatthew Knepley   if (sym && (perms || rots)) {
3506ea844a1aSMatthew Knepley     SymWorkLink *p, link;
3507ea844a1aSMatthew Knepley 
3508ea844a1aSMatthew Knepley     for (p = &sym->workout; (link = *p); p = &link->next) {
3509ea844a1aSMatthew Knepley       if ((perms && link->perms == *perms) || (rots && link->rots == *rots)) {
3510ea844a1aSMatthew Knepley         *p          = link->next;
3511ea844a1aSMatthew Knepley         link->next  = sym->workin;
3512ea844a1aSMatthew Knepley         sym->workin = link;
3513ea844a1aSMatthew Knepley         if (perms) *perms = NULL;
3514ea844a1aSMatthew Knepley         if (rots) *rots = NULL;
35153ba16761SJacob Faibussowitsch         PetscFunctionReturn(PETSC_SUCCESS);
3516ea844a1aSMatthew Knepley       }
3517ea844a1aSMatthew Knepley     }
3518ea844a1aSMatthew Knepley     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Array was not checked out");
3519ea844a1aSMatthew Knepley   }
35203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3521ea844a1aSMatthew Knepley }
3522ea844a1aSMatthew Knepley 
3523ea844a1aSMatthew Knepley /*@C
3524cab54364SBarry Smith   PetscSectionGetFieldPointSyms - Get the symmetries for a set of points in a field of a `PetscSection` under specific orientations.
3525ea844a1aSMatthew Knepley 
352640750d41SVaclav Hapla   Not Collective
3527ea844a1aSMatthew Knepley 
3528ea844a1aSMatthew Knepley   Input Parameters:
3529ea844a1aSMatthew Knepley + section - the section
3530ea844a1aSMatthew Knepley . field - the field of the section
3531ea844a1aSMatthew Knepley . numPoints - the number of points
353220662ed9SBarry Smith - points - an array of size 2 * `numPoints`, containing a list of (point, orientation) pairs. (An orientation is an
3533cab54364SBarry Smith     arbitrary integer: its interpretation is up to sym.  Orientations are used by `DM`: for their interpretation in that
3534cab54364SBarry Smith     context, see `DMPlexGetConeOrientation()`).
3535ea844a1aSMatthew Knepley 
3536d8d19677SJose E. Roman   Output Parameters:
353720662ed9SBarry Smith + perms - The permutations for the given orientations (or `NULL` if there is no symmetry or the permutation is the identity).
353820662ed9SBarry Smith - rots - The field rotations symmetries for the given orientations (or `NULL` if there is no symmetry or the rotations are all
3539ea844a1aSMatthew Knepley     identity).
3540ea844a1aSMatthew Knepley 
3541ea844a1aSMatthew Knepley   Level: developer
3542ea844a1aSMatthew Knepley 
354320662ed9SBarry Smith   Notes:
354420662ed9SBarry Smith   `PetscSectionSetFieldSym()` must have been previously called to provide the symmetries to the `PetscSection`
354520662ed9SBarry Smith 
354620662ed9SBarry Smith   Use `PetscSectionRestoreFieldPointSyms()` when finished with the data
354720662ed9SBarry Smith 
3548cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionGetPointSyms()`, `PetscSectionRestoreFieldPointSyms()`
3549ea844a1aSMatthew Knepley @*/
3550d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldPointSyms(PetscSection section, PetscInt field, PetscInt numPoints, const PetscInt *points, const PetscInt ***perms, const PetscScalar ***rots)
3551d71ae5a4SJacob Faibussowitsch {
3552ea844a1aSMatthew Knepley   PetscFunctionBegin;
3553ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
355408401ef6SPierre 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);
35559566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetPointSyms(section->field[field], numPoints, points, perms, rots));
35563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3557ea844a1aSMatthew Knepley }
3558ea844a1aSMatthew Knepley 
3559ea844a1aSMatthew Knepley /*@C
3560cab54364SBarry Smith   PetscSectionRestoreFieldPointSyms - Restore the symmetries returned by `PetscSectionGetFieldPointSyms()`
3561ea844a1aSMatthew Knepley 
356240750d41SVaclav Hapla   Not Collective
3563ea844a1aSMatthew Knepley 
3564ea844a1aSMatthew Knepley   Input Parameters:
3565ea844a1aSMatthew Knepley + section - the section
3566ea844a1aSMatthew Knepley . field - the field number
3567ea844a1aSMatthew Knepley . numPoints - the number of points
356820662ed9SBarry Smith - points - an array of size 2 * `numPoints`, containing a list of (point, orientation) pairs. (An orientation is an
3569cab54364SBarry Smith     arbitrary integer: its interpretation is up to sym.  Orientations are used by `DM`: for their interpretation in that
3570cab54364SBarry Smith     context, see `DMPlexGetConeOrientation()`).
357120662ed9SBarry Smith . perms - The permutations for the given orientations: set to NULL at conclusion
3572ea844a1aSMatthew Knepley - rots - The field rotations symmetries for the given orientations: set to NULL at conclusion
3573ea844a1aSMatthew Knepley 
3574ea844a1aSMatthew Knepley   Level: developer
3575ea844a1aSMatthew Knepley 
3576cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionRestorePointSyms()`, `petscSectionGetFieldPointSyms()`, `PetscSectionSymCreate()`, `PetscSectionSetSym()`, `PetscSectionGetSym()`
3577ea844a1aSMatthew Knepley @*/
3578d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionRestoreFieldPointSyms(PetscSection section, PetscInt field, PetscInt numPoints, const PetscInt *points, const PetscInt ***perms, const PetscScalar ***rots)
3579d71ae5a4SJacob Faibussowitsch {
3580ea844a1aSMatthew Knepley   PetscFunctionBegin;
3581ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
358208401ef6SPierre 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);
35839566063dSJacob Faibussowitsch   PetscCall(PetscSectionRestorePointSyms(section->field[field], numPoints, points, perms, rots));
35843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3585ea844a1aSMatthew Knepley }
3586ea844a1aSMatthew Knepley 
3587ea844a1aSMatthew Knepley /*@
3588b004864fSMatthew G. Knepley   PetscSectionSymCopy - Copy the symmetries, assuming that the point structure is compatible
3589b004864fSMatthew G. Knepley 
359040750d41SVaclav Hapla   Not Collective
3591b004864fSMatthew G. Knepley 
3592b004864fSMatthew G. Knepley   Input Parameter:
3593cab54364SBarry Smith . sym - the `PetscSectionSym`
3594b004864fSMatthew G. Knepley 
3595b004864fSMatthew G. Knepley   Output Parameter:
3596b004864fSMatthew G. Knepley . nsym - the equivalent symmetries
3597b004864fSMatthew G. Knepley 
3598b004864fSMatthew G. Knepley   Level: developer
3599b004864fSMatthew G. Knepley 
3600cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionSymCreate()`, `PetscSectionSetSym()`, `PetscSectionGetSym()`, `PetscSectionSymLabelSetStratum()`, `PetscSectionGetPointSyms()`
3601b004864fSMatthew G. Knepley @*/
3602d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymCopy(PetscSectionSym sym, PetscSectionSym nsym)
3603d71ae5a4SJacob Faibussowitsch {
3604b004864fSMatthew G. Knepley   PetscFunctionBegin;
3605b004864fSMatthew G. Knepley   PetscValidHeaderSpecific(sym, PETSC_SECTION_SYM_CLASSID, 1);
3606b004864fSMatthew G. Knepley   PetscValidHeaderSpecific(nsym, PETSC_SECTION_SYM_CLASSID, 2);
3607dbbe0bcdSBarry Smith   PetscTryTypeMethod(sym, copy, nsym);
36083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3609b004864fSMatthew G. Knepley }
3610b004864fSMatthew G. Knepley 
3611b004864fSMatthew G. Knepley /*@
3612cab54364SBarry Smith   PetscSectionSymDistribute - Distribute the symmetries in accordance with the input `PetscSF`
3613b004864fSMatthew G. Knepley 
3614b004864fSMatthew G. Knepley   Collective
3615b004864fSMatthew G. Knepley 
3616b004864fSMatthew G. Knepley   Input Parameters:
3617cab54364SBarry Smith + sym - the `PetscSectionSym`
3618b004864fSMatthew G. Knepley - migrationSF - the distribution map from roots to leaves
3619b004864fSMatthew G. Knepley 
36202fe279fdSBarry Smith   Output Parameter:
3621b004864fSMatthew G. Knepley . dsym - the redistributed symmetries
3622b004864fSMatthew G. Knepley 
3623b004864fSMatthew G. Knepley   Level: developer
3624b004864fSMatthew G. Knepley 
3625cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionSymCreate()`, `PetscSectionSetSym()`, `PetscSectionGetSym()`, `PetscSectionSymLabelSetStratum()`, `PetscSectionGetPointSyms()`
3626b004864fSMatthew G. Knepley @*/
3627d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymDistribute(PetscSectionSym sym, PetscSF migrationSF, PetscSectionSym *dsym)
3628d71ae5a4SJacob Faibussowitsch {
3629b004864fSMatthew G. Knepley   PetscFunctionBegin;
3630b004864fSMatthew G. Knepley   PetscValidHeaderSpecific(sym, PETSC_SECTION_SYM_CLASSID, 1);
3631b004864fSMatthew G. Knepley   PetscValidHeaderSpecific(migrationSF, PETSCSF_CLASSID, 2);
3632b004864fSMatthew G. Knepley   PetscValidPointer(dsym, 3);
3633dbbe0bcdSBarry Smith   PetscTryTypeMethod(sym, distribute, migrationSF, dsym);
36343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3635b004864fSMatthew G. Knepley }
3636b004864fSMatthew G. Knepley 
3637b004864fSMatthew G. Knepley /*@
363820662ed9SBarry Smith   PetscSectionGetUseFieldOffsets - Get the flag indicating if field offsets are used directly in a global section, rather than just the point offset
3639ea844a1aSMatthew Knepley 
364040750d41SVaclav Hapla   Not Collective
3641ea844a1aSMatthew Knepley 
3642ea844a1aSMatthew Knepley   Input Parameter:
3643cab54364SBarry Smith . s - the global `PetscSection`
3644ea844a1aSMatthew Knepley 
36452fe279fdSBarry Smith   Output Parameter:
3646ea844a1aSMatthew Knepley . flg - the flag
3647ea844a1aSMatthew Knepley 
3648ea844a1aSMatthew Knepley   Level: developer
3649ea844a1aSMatthew Knepley 
3650cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionSetChart()`, `PetscSectionCreate()`
3651ea844a1aSMatthew Knepley @*/
3652d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetUseFieldOffsets(PetscSection s, PetscBool *flg)
3653d71ae5a4SJacob Faibussowitsch {
3654ea844a1aSMatthew Knepley   PetscFunctionBegin;
3655ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
3656ea844a1aSMatthew Knepley   *flg = s->useFieldOff;
36573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3658ea844a1aSMatthew Knepley }
3659ea844a1aSMatthew Knepley 
3660ea844a1aSMatthew Knepley /*@
3661ea844a1aSMatthew Knepley   PetscSectionSetUseFieldOffsets - Set the flag to use field offsets directly in a global section, rather than just the point offset
3662ea844a1aSMatthew Knepley 
366340750d41SVaclav Hapla   Not Collective
3664ea844a1aSMatthew Knepley 
3665ea844a1aSMatthew Knepley   Input Parameters:
3666cab54364SBarry Smith + s   - the global `PetscSection`
3667ea844a1aSMatthew Knepley - flg - the flag
3668ea844a1aSMatthew Knepley 
3669ea844a1aSMatthew Knepley   Level: developer
3670ea844a1aSMatthew Knepley 
3671cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionGetUseFieldOffsets()`, `PetscSectionSetChart()`, `PetscSectionCreate()`
3672ea844a1aSMatthew Knepley @*/
3673d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetUseFieldOffsets(PetscSection s, PetscBool flg)
3674d71ae5a4SJacob Faibussowitsch {
3675ea844a1aSMatthew Knepley   PetscFunctionBegin;
3676ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
3677ea844a1aSMatthew Knepley   s->useFieldOff = flg;
36783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3679ea844a1aSMatthew Knepley }
3680ea844a1aSMatthew Knepley 
3681ea844a1aSMatthew Knepley #define PetscSectionExpandPoints_Loop(TYPE) \
3682ea844a1aSMatthew Knepley   { \
3683ea844a1aSMatthew Knepley     PetscInt i, n, o0, o1, size; \
3684ea844a1aSMatthew Knepley     TYPE    *a0 = (TYPE *)origArray, *a1; \
36859566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(s, &size)); \
36869566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(size, &a1)); \
3687ea844a1aSMatthew Knepley     for (i = 0; i < npoints; i++) { \
36889566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(origSection, points_[i], &o0)); \
36899566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(s, i, &o1)); \
36909566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(s, i, &n)); \
36919566063dSJacob Faibussowitsch       PetscCall(PetscMemcpy(&a1[o1], &a0[o0], n *unitsize)); \
3692ea844a1aSMatthew Knepley     } \
3693ea844a1aSMatthew Knepley     *newArray = (void *)a1; \
3694ea844a1aSMatthew Knepley   }
3695ea844a1aSMatthew Knepley 
3696ea844a1aSMatthew Knepley /*@
3697ea844a1aSMatthew Knepley   PetscSectionExtractDofsFromArray - Extracts elements of an array corresponding to DOFs of specified points.
3698ea844a1aSMatthew Knepley 
369940750d41SVaclav Hapla   Not Collective
3700ea844a1aSMatthew Knepley 
3701ea844a1aSMatthew Knepley   Input Parameters:
3702cab54364SBarry Smith + origSection - the `PetscSection` describing the layout of the array
3703cab54364SBarry Smith . dataType - `MPI_Datatype` describing the data type of the array (currently only `MPIU_INT`, `MPIU_SCALAR`, `MPIU_REAL`)
370420662ed9SBarry Smith . origArray - the array; its size must be equal to the storage size of `origSection`
370520662ed9SBarry Smith - points - `IS` with points to extract; its indices must lie in the chart of `origSection`
3706ea844a1aSMatthew Knepley 
3707ea844a1aSMatthew Knepley   Output Parameters:
3708da81f932SPierre Jolivet + newSection - the new `PetscSection` describing the layout of the new array (with points renumbered 0,1,... but preserving numbers of DOFs)
370920662ed9SBarry Smith - newArray - the array of the extracted DOFs; its size is the storage size of `newSection`
3710ea844a1aSMatthew Knepley 
3711ea844a1aSMatthew Knepley   Level: developer
3712ea844a1aSMatthew Knepley 
3713cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionGetChart()`, `PetscSectionGetDof()`, `PetscSectionGetStorageSize()`, `PetscSectionCreate()`
3714ea844a1aSMatthew Knepley @*/
3715d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionExtractDofsFromArray(PetscSection origSection, MPI_Datatype dataType, const void *origArray, IS points, PetscSection *newSection, void *newArray[])
3716d71ae5a4SJacob Faibussowitsch {
3717ea844a1aSMatthew Knepley   PetscSection    s;
3718ea844a1aSMatthew Knepley   const PetscInt *points_;
3719ea844a1aSMatthew Knepley   PetscInt        i, n, npoints, pStart, pEnd;
3720ea844a1aSMatthew Knepley   PetscMPIInt     unitsize;
3721ea844a1aSMatthew Knepley 
3722ea844a1aSMatthew Knepley   PetscFunctionBegin;
3723ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(origSection, PETSC_SECTION_CLASSID, 1);
3724ea844a1aSMatthew Knepley   PetscValidPointer(origArray, 3);
3725ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(points, IS_CLASSID, 4);
3726ea844a1aSMatthew Knepley   if (newSection) PetscValidPointer(newSection, 5);
3727ea844a1aSMatthew Knepley   if (newArray) PetscValidPointer(newArray, 6);
37289566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Type_size(dataType, &unitsize));
37299566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(points, &npoints));
37309566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(points, &points_));
37319566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(origSection, &pStart, &pEnd));
37329566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &s));
37339566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(s, 0, npoints));
3734ea844a1aSMatthew Knepley   for (i = 0; i < npoints; i++) {
3735c9cc58a2SBarry 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);
37369566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(origSection, points_[i], &n));
37379566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(s, i, n));
3738ea844a1aSMatthew Knepley   }
37399566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(s));
3740ea844a1aSMatthew Knepley   if (newArray) {
37419371c9d4SSatish Balay     if (dataType == MPIU_INT) {
37429371c9d4SSatish Balay       PetscSectionExpandPoints_Loop(PetscInt);
37439371c9d4SSatish Balay     } else if (dataType == MPIU_SCALAR) {
37449371c9d4SSatish Balay       PetscSectionExpandPoints_Loop(PetscScalar);
37459371c9d4SSatish Balay     } else if (dataType == MPIU_REAL) {
37469371c9d4SSatish Balay       PetscSectionExpandPoints_Loop(PetscReal);
37479371c9d4SSatish Balay     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "not implemented for this MPI_Datatype");
3748ea844a1aSMatthew Knepley   }
3749ea844a1aSMatthew Knepley   if (newSection) {
3750ea844a1aSMatthew Knepley     *newSection = s;
3751ea844a1aSMatthew Knepley   } else {
37529566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&s));
3753ea844a1aSMatthew Knepley   }
37549566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(points, &points_));
37553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3756ea844a1aSMatthew Knepley }
3757