xref: /petsc/src/vec/is/section/interface/section.c (revision eb9d3e4d1b0d07ce7eca2be9429d4698ffa7ae7f)
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 
35583308b6SBarry 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;
404f572ea9SToby Isaac   PetscAssertPointer(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 
8138b5cf2dSJacob Faibussowitsch   Developer Notes:
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 {
883857f682SStefano Zampini   PetscFunctionBegin;
893857f682SStefano Zampini   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
903857f682SStefano Zampini   PetscValidHeaderSpecific(newSection, PETSC_SECTION_CLASSID, 2);
913857f682SStefano Zampini   PetscCall(PetscSectionCopy_Internal(section, newSection, NULL));
923857f682SStefano Zampini   PetscFunctionReturn(PETSC_SUCCESS);
933857f682SStefano Zampini }
943857f682SStefano Zampini 
953857f682SStefano Zampini PetscErrorCode PetscSectionCopy_Internal(PetscSection section, PetscSection newSection, PetscBT constrained_dofs)
963857f682SStefano Zampini {
97ea844a1aSMatthew Knepley   PetscSectionSym sym;
98ea844a1aSMatthew Knepley   IS              perm;
99b778fa18SValeria Barra   PetscInt        numFields, f, c, pStart, pEnd, p;
100ea844a1aSMatthew Knepley 
101ea844a1aSMatthew Knepley   PetscFunctionBegin;
102ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
103ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(newSection, PETSC_SECTION_CLASSID, 2);
1049566063dSJacob Faibussowitsch   PetscCall(PetscSectionReset(newSection));
1059566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(section, &numFields));
1069566063dSJacob Faibussowitsch   if (numFields) PetscCall(PetscSectionSetNumFields(newSection, numFields));
107ea844a1aSMatthew Knepley   for (f = 0; f < numFields; ++f) {
108b778fa18SValeria Barra     const char *fieldName = NULL, *compName = NULL;
109ea844a1aSMatthew Knepley     PetscInt    numComp = 0;
110ea844a1aSMatthew Knepley 
1119566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
1129566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldName(newSection, f, fieldName));
1139566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(section, f, &numComp));
1149566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(newSection, f, numComp));
115b778fa18SValeria Barra     for (c = 0; c < numComp; ++c) {
1169566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetComponentName(section, f, c, &compName));
1179566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetComponentName(newSection, f, c, compName));
118b778fa18SValeria Barra     }
1199566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldSym(section, f, &sym));
1209566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldSym(newSection, f, sym));
121ea844a1aSMatthew Knepley   }
1229566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
1239566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(newSection, pStart, pEnd));
1249566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetPermutation(section, &perm));
1259566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetPermutation(newSection, perm));
1269566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetSym(section, &sym));
1279566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetSym(newSection, sym));
128ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
129ea844a1aSMatthew Knepley     PetscInt  dof, cdof, fcdof = 0;
1303857f682SStefano Zampini     PetscBool force_constrained = (PetscBool)(constrained_dofs && PetscBTLookup(constrained_dofs, p - pStart));
131ea844a1aSMatthew Knepley 
1329566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(section, p, &dof));
1339566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(newSection, p, dof));
1343857f682SStefano Zampini     if (force_constrained) cdof = dof;
1353857f682SStefano Zampini     else PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
1369566063dSJacob Faibussowitsch     if (cdof) PetscCall(PetscSectionSetConstraintDof(newSection, p, cdof));
137ea844a1aSMatthew Knepley     for (f = 0; f < numFields; ++f) {
1389566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(section, p, f, &dof));
1399566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(newSection, p, f, dof));
140ea844a1aSMatthew Knepley       if (cdof) {
1413857f682SStefano Zampini         if (force_constrained) fcdof = dof;
1423857f682SStefano Zampini         else PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
1439566063dSJacob Faibussowitsch         if (fcdof) PetscCall(PetscSectionSetFieldConstraintDof(newSection, p, f, fcdof));
144ea844a1aSMatthew Knepley       }
145ea844a1aSMatthew Knepley     }
146ea844a1aSMatthew Knepley   }
1479566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(newSection));
148ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
149ea844a1aSMatthew Knepley     PetscInt        off, cdof, fcdof = 0;
150ea844a1aSMatthew Knepley     const PetscInt *cInd;
1513857f682SStefano Zampini     PetscBool       force_constrained = (PetscBool)(constrained_dofs && PetscBTLookup(constrained_dofs, p - pStart));
152ea844a1aSMatthew Knepley 
153ea844a1aSMatthew Knepley     /* Must set offsets in case they do not agree with the prefix sums */
1549566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(section, p, &off));
1559566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetOffset(newSection, p, off));
1563857f682SStefano Zampini     PetscCall(PetscSectionGetConstraintDof(newSection, p, &cdof));
157ea844a1aSMatthew Knepley     if (cdof) {
1583857f682SStefano Zampini       if (force_constrained) cInd = NULL;
1593857f682SStefano Zampini       else PetscCall(PetscSectionGetConstraintIndices(section, p, &cInd));
1609566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetConstraintIndices(newSection, p, cInd));
161ea844a1aSMatthew Knepley       for (f = 0; f < numFields; ++f) {
1623857f682SStefano Zampini         PetscCall(PetscSectionGetFieldOffset(section, p, f, &off));
1633857f682SStefano Zampini         PetscCall(PetscSectionSetFieldOffset(newSection, p, f, off));
1643857f682SStefano Zampini         PetscCall(PetscSectionGetFieldConstraintDof(newSection, p, f, &fcdof));
165ea844a1aSMatthew Knepley         if (fcdof) {
1663857f682SStefano Zampini           if (force_constrained) cInd = NULL;
1673857f682SStefano Zampini           else PetscCall(PetscSectionGetFieldConstraintIndices(section, p, f, &cInd));
1689566063dSJacob Faibussowitsch           PetscCall(PetscSectionSetFieldConstraintIndices(newSection, p, f, cInd));
169ea844a1aSMatthew Knepley         }
170ea844a1aSMatthew Knepley       }
171ea844a1aSMatthew Knepley     }
172ea844a1aSMatthew Knepley   }
1733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
174ea844a1aSMatthew Knepley }
175ea844a1aSMatthew Knepley 
176ea844a1aSMatthew Knepley /*@
177cab54364SBarry Smith   PetscSectionClone - Creates a shallow (if possible) copy of the `PetscSection`
178ea844a1aSMatthew Knepley 
179ea844a1aSMatthew Knepley   Collective
180ea844a1aSMatthew Knepley 
181ea844a1aSMatthew Knepley   Input Parameter:
182cab54364SBarry Smith . section - the `PetscSection`
183ea844a1aSMatthew Knepley 
184ea844a1aSMatthew Knepley   Output Parameter:
185ea844a1aSMatthew Knepley . newSection - the copy
186ea844a1aSMatthew Knepley 
187ea844a1aSMatthew Knepley   Level: beginner
188ea844a1aSMatthew Knepley 
18938b5cf2dSJacob Faibussowitsch   Developer Notes:
190cab54364SBarry Smith   With standard PETSc terminology this should be called `PetscSectionDuplicate()`
191cab54364SBarry Smith 
192cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionDestroy()`, `PetscSectionCopy()`
193ea844a1aSMatthew Knepley @*/
194d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionClone(PetscSection section, PetscSection *newSection)
195d71ae5a4SJacob Faibussowitsch {
196ea844a1aSMatthew Knepley   PetscFunctionBegin;
197ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
1984f572ea9SToby Isaac   PetscAssertPointer(newSection, 2);
1999566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)section), newSection));
2009566063dSJacob Faibussowitsch   PetscCall(PetscSectionCopy(section, *newSection));
2013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
202ea844a1aSMatthew Knepley }
203ea844a1aSMatthew Knepley 
204ea844a1aSMatthew Knepley /*@
205cab54364SBarry Smith   PetscSectionSetFromOptions - sets parameters in a `PetscSection` from the options database
206ea844a1aSMatthew Knepley 
20740750d41SVaclav Hapla   Collective
208ea844a1aSMatthew Knepley 
209ea844a1aSMatthew Knepley   Input Parameter:
21038b5cf2dSJacob Faibussowitsch . s - the `PetscSection`
211ea844a1aSMatthew Knepley 
21220662ed9SBarry Smith   Options Database Key:
213cab54364SBarry Smith . -petscsection_point_major - `PETSC_TRUE` for point-major order
214ea844a1aSMatthew Knepley 
215ea844a1aSMatthew Knepley   Level: intermediate
216ea844a1aSMatthew Knepley 
217cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionDestroy()`
218ea844a1aSMatthew Knepley @*/
219d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFromOptions(PetscSection s)
220d71ae5a4SJacob Faibussowitsch {
221ea844a1aSMatthew Knepley   PetscFunctionBegin;
222ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
223d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)s);
2249566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-petscsection_point_major", "The for ordering, either point major or field major", "PetscSectionSetPointMajor", s->pointMajor, &s->pointMajor, NULL));
225ea844a1aSMatthew Knepley   /* process any options handlers added with PetscObjectAddOptionsHandler() */
226dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)s, PetscOptionsObject));
227d0609cedSBarry Smith   PetscOptionsEnd();
2289566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)s, NULL, "-petscsection_view"));
2293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
230ea844a1aSMatthew Knepley }
231ea844a1aSMatthew Knepley 
232ea844a1aSMatthew Knepley /*@
233ea844a1aSMatthew Knepley   PetscSectionCompare - Compares two sections
234ea844a1aSMatthew Knepley 
23540750d41SVaclav Hapla   Collective
236ea844a1aSMatthew Knepley 
2377a7aea1fSJed Brown   Input Parameters:
238cab54364SBarry Smith + s1 - the first `PetscSection`
239cab54364SBarry Smith - s2 - the second `PetscSection`
240ea844a1aSMatthew Knepley 
241ea844a1aSMatthew Knepley   Output Parameter:
242cab54364SBarry Smith . congruent - `PETSC_TRUE` if the two sections are congruent, `PETSC_FALSE` otherwise
243ea844a1aSMatthew Knepley 
244ea844a1aSMatthew Knepley   Level: intermediate
245ea844a1aSMatthew Knepley 
246cab54364SBarry Smith   Note:
247ea844a1aSMatthew Knepley   Field names are disregarded.
248ea844a1aSMatthew Knepley 
249cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionCopy()`, `PetscSectionClone()`
250ea844a1aSMatthew Knepley @*/
251d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCompare(PetscSection s1, PetscSection s2, PetscBool *congruent)
252d71ae5a4SJacob Faibussowitsch {
253ea844a1aSMatthew Knepley   PetscInt        pStart, pEnd, nfields, ncdof, nfcdof, p, f, n1, n2;
254ea844a1aSMatthew Knepley   const PetscInt *idx1, *idx2;
255ea844a1aSMatthew Knepley   IS              perm1, perm2;
256ea844a1aSMatthew Knepley   PetscBool       flg;
257ea844a1aSMatthew Knepley   PetscMPIInt     mflg;
258ea844a1aSMatthew Knepley 
259ea844a1aSMatthew Knepley   PetscFunctionBegin;
260ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s1, PETSC_SECTION_CLASSID, 1);
261ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s2, PETSC_SECTION_CLASSID, 2);
2624f572ea9SToby Isaac   PetscAssertPointer(congruent, 3);
263ea844a1aSMatthew Knepley   flg = PETSC_FALSE;
264ea844a1aSMatthew Knepley 
2659566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)s1), PetscObjectComm((PetscObject)s2), &mflg));
266ea844a1aSMatthew Knepley   if (mflg != MPI_CONGRUENT && mflg != MPI_IDENT) {
267ea844a1aSMatthew Knepley     *congruent = PETSC_FALSE;
2683ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
269ea844a1aSMatthew Knepley   }
270ea844a1aSMatthew Knepley 
2719566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s1, &pStart, &pEnd));
2729566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s2, &n1, &n2));
273ea844a1aSMatthew Knepley   if (pStart != n1 || pEnd != n2) goto not_congruent;
274ea844a1aSMatthew Knepley 
2759566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetPermutation(s1, &perm1));
2769566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetPermutation(s2, &perm2));
277ea844a1aSMatthew Knepley   if (perm1 && perm2) {
2789566063dSJacob Faibussowitsch     PetscCall(ISEqual(perm1, perm2, congruent));
279ea844a1aSMatthew Knepley     if (!(*congruent)) goto not_congruent;
280ea844a1aSMatthew Knepley   } else if (perm1 != perm2) goto not_congruent;
281ea844a1aSMatthew Knepley 
282ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
2839566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(s1, p, &n1));
2849566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(s2, p, &n2));
285ea844a1aSMatthew Knepley     if (n1 != n2) goto not_congruent;
286ea844a1aSMatthew Knepley 
2879566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s1, p, &n1));
2889566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s2, p, &n2));
289ea844a1aSMatthew Knepley     if (n1 != n2) goto not_congruent;
290ea844a1aSMatthew Knepley 
2919566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s1, p, &ncdof));
2929566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s2, p, &n2));
293ea844a1aSMatthew Knepley     if (ncdof != n2) goto not_congruent;
294ea844a1aSMatthew Knepley 
2959566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintIndices(s1, p, &idx1));
2969566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintIndices(s2, p, &idx2));
2979566063dSJacob Faibussowitsch     PetscCall(PetscArraycmp(idx1, idx2, ncdof, congruent));
298ea844a1aSMatthew Knepley     if (!(*congruent)) goto not_congruent;
299ea844a1aSMatthew Knepley   }
300ea844a1aSMatthew Knepley 
3019566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s1, &nfields));
3029566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s2, &n2));
303ea844a1aSMatthew Knepley   if (nfields != n2) goto not_congruent;
304ea844a1aSMatthew Knepley 
305ea844a1aSMatthew Knepley   for (f = 0; f < nfields; ++f) {
3069566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s1, f, &n1));
3079566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s2, f, &n2));
308ea844a1aSMatthew Knepley     if (n1 != n2) goto not_congruent;
309ea844a1aSMatthew Knepley 
310ea844a1aSMatthew Knepley     for (p = pStart; p < pEnd; ++p) {
3119566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldOffset(s1, p, f, &n1));
3129566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldOffset(s2, p, f, &n2));
313ea844a1aSMatthew Knepley       if (n1 != n2) goto not_congruent;
314ea844a1aSMatthew Knepley 
3159566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s1, p, f, &n1));
3169566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s2, p, f, &n2));
317ea844a1aSMatthew Knepley       if (n1 != n2) goto not_congruent;
318ea844a1aSMatthew Knepley 
3199566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(s1, p, f, &nfcdof));
3209566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(s2, p, f, &n2));
321ea844a1aSMatthew Knepley       if (nfcdof != n2) goto not_congruent;
322ea844a1aSMatthew Knepley 
3239566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintIndices(s1, p, f, &idx1));
3249566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintIndices(s2, p, f, &idx2));
3259566063dSJacob Faibussowitsch       PetscCall(PetscArraycmp(idx1, idx2, nfcdof, congruent));
326ea844a1aSMatthew Knepley       if (!(*congruent)) goto not_congruent;
327ea844a1aSMatthew Knepley     }
328ea844a1aSMatthew Knepley   }
329ea844a1aSMatthew Knepley 
330ea844a1aSMatthew Knepley   flg = PETSC_TRUE;
331ea844a1aSMatthew Knepley not_congruent:
3321c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&flg, congruent, 1, MPIU_BOOL, MPI_LAND, PetscObjectComm((PetscObject)s1)));
3333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
334ea844a1aSMatthew Knepley }
335ea844a1aSMatthew Knepley 
336ea844a1aSMatthew Knepley /*@
337cab54364SBarry Smith   PetscSectionGetNumFields - Returns the number of fields in a `PetscSection`, or 0 if no fields were defined.
338ea844a1aSMatthew Knepley 
33940750d41SVaclav Hapla   Not Collective
340ea844a1aSMatthew Knepley 
341ea844a1aSMatthew Knepley   Input Parameter:
342cab54364SBarry Smith . s - the `PetscSection`
343ea844a1aSMatthew Knepley 
344ea844a1aSMatthew Knepley   Output Parameter:
345ea844a1aSMatthew Knepley . numFields - the number of fields defined, or 0 if none were defined
346ea844a1aSMatthew Knepley 
347ea844a1aSMatthew Knepley   Level: intermediate
348ea844a1aSMatthew Knepley 
349cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetNumFields()`
350ea844a1aSMatthew Knepley @*/
351d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetNumFields(PetscSection s, PetscInt *numFields)
352d71ae5a4SJacob Faibussowitsch {
353ea844a1aSMatthew Knepley   PetscFunctionBegin;
354ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
3554f572ea9SToby Isaac   PetscAssertPointer(numFields, 2);
356ea844a1aSMatthew Knepley   *numFields = s->numFields;
3573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
358ea844a1aSMatthew Knepley }
359ea844a1aSMatthew Knepley 
360ea844a1aSMatthew Knepley /*@
361cab54364SBarry Smith   PetscSectionSetNumFields - Sets the number of fields in a `PetscSection`
362ea844a1aSMatthew Knepley 
36340750d41SVaclav Hapla   Not Collective
364ea844a1aSMatthew Knepley 
365ea844a1aSMatthew Knepley   Input Parameters:
36620662ed9SBarry Smith + s         - the `PetscSection`
367ea844a1aSMatthew Knepley - numFields - the number of fields
368ea844a1aSMatthew Knepley 
369ea844a1aSMatthew Knepley   Level: intermediate
370ea844a1aSMatthew Knepley 
371583308b6SBarry Smith   Notes:
372583308b6SBarry Smith   Calling this destroys all the information in the `PetscSection` including the chart.
373583308b6SBarry Smith 
374583308b6SBarry Smith   You must call `PetscSectionSetChart()` after calling this.
375583308b6SBarry Smith 
376583308b6SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetNumFields()`, `PetscSectionSetChart()`, `PetscSectionReset()`
377ea844a1aSMatthew Knepley @*/
378d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetNumFields(PetscSection s, PetscInt numFields)
379d71ae5a4SJacob Faibussowitsch {
380ea844a1aSMatthew Knepley   PetscInt f;
381ea844a1aSMatthew Knepley 
382ea844a1aSMatthew Knepley   PetscFunctionBegin;
383ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
38408401ef6SPierre Jolivet   PetscCheck(numFields > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "The number of fields %" PetscInt_FMT " must be positive", numFields);
3859566063dSJacob Faibussowitsch   PetscCall(PetscSectionReset(s));
386ea844a1aSMatthew Knepley 
387ea844a1aSMatthew Knepley   s->numFields = numFields;
3889566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(s->numFields, &s->numFieldComponents));
3899566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(s->numFields, &s->fieldNames));
3909566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(s->numFields, &s->compNames));
3919566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(s->numFields, &s->field));
392ea844a1aSMatthew Knepley   for (f = 0; f < s->numFields; ++f) {
393ea844a1aSMatthew Knepley     char name[64];
394ea844a1aSMatthew Knepley 
395ea844a1aSMatthew Knepley     s->numFieldComponents[f] = 1;
396ea844a1aSMatthew Knepley 
3979566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s), &s->field[f]));
3989566063dSJacob Faibussowitsch     PetscCall(PetscSNPrintf(name, 64, "Field_%" PetscInt_FMT, f));
3999566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, (char **)&s->fieldNames[f]));
4009566063dSJacob Faibussowitsch     PetscCall(PetscSNPrintf(name, 64, "Component_0"));
4019566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(s->numFieldComponents[f], &s->compNames[f]));
4029566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, (char **)&s->compNames[f][0]));
403ea844a1aSMatthew Knepley   }
4043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
405ea844a1aSMatthew Knepley }
406ea844a1aSMatthew Knepley 
407ea844a1aSMatthew Knepley /*@C
408cab54364SBarry Smith   PetscSectionGetFieldName - Returns the name of a field in the `PetscSection`
409ea844a1aSMatthew Knepley 
410ea844a1aSMatthew Knepley   Not Collective
411ea844a1aSMatthew Knepley 
412ea844a1aSMatthew Knepley   Input Parameters:
413cab54364SBarry Smith + s     - the `PetscSection`
414ea844a1aSMatthew Knepley - field - the field number
415ea844a1aSMatthew Knepley 
416ea844a1aSMatthew Knepley   Output Parameter:
417ea844a1aSMatthew Knepley . fieldName - the field name
418ea844a1aSMatthew Knepley 
419ea844a1aSMatthew Knepley   Level: intermediate
420ea844a1aSMatthew Knepley 
421cab54364SBarry Smith   Note:
422cab54364SBarry Smith   Will error if the field number is out of range
423cab54364SBarry Smith 
424cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetFieldName()`, `PetscSectionSetNumFields()`, `PetscSectionGetNumFields()`
425ea844a1aSMatthew Knepley @*/
426d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldName(PetscSection s, PetscInt field, const char *fieldName[])
427d71ae5a4SJacob Faibussowitsch {
428ea844a1aSMatthew Knepley   PetscFunctionBegin;
429ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
4304f572ea9SToby Isaac   PetscAssertPointer(fieldName, 3);
4312abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
432ea844a1aSMatthew Knepley   *fieldName = s->fieldNames[field];
4333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
434ea844a1aSMatthew Knepley }
435ea844a1aSMatthew Knepley 
436ea844a1aSMatthew Knepley /*@C
437cab54364SBarry Smith   PetscSectionSetFieldName - Sets the name of a field in the `PetscSection`
438ea844a1aSMatthew Knepley 
439ea844a1aSMatthew Knepley   Not Collective
440ea844a1aSMatthew Knepley 
441ea844a1aSMatthew Knepley   Input Parameters:
442cab54364SBarry Smith + s         - the `PetscSection`
443ea844a1aSMatthew Knepley . field     - the field number
444ea844a1aSMatthew Knepley - fieldName - the field name
445ea844a1aSMatthew Knepley 
446ea844a1aSMatthew Knepley   Level: intermediate
447ea844a1aSMatthew Knepley 
448cab54364SBarry Smith   Note:
449cab54364SBarry Smith   Will error if the field number is out of range
450cab54364SBarry Smith 
451cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionGetFieldName()`, `PetscSectionSetNumFields()`, `PetscSectionGetNumFields()`
452ea844a1aSMatthew Knepley @*/
453d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFieldName(PetscSection s, PetscInt field, const char fieldName[])
454d71ae5a4SJacob Faibussowitsch {
455ea844a1aSMatthew Knepley   PetscFunctionBegin;
456ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
4574f572ea9SToby Isaac   if (fieldName) PetscAssertPointer(fieldName, 3);
4582abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
4599566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->fieldNames[field]));
4609566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(fieldName, (char **)&s->fieldNames[field]));
4613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
462ea844a1aSMatthew Knepley }
463ea844a1aSMatthew Knepley 
464b778fa18SValeria Barra /*@C
465cab54364SBarry Smith   PetscSectionGetComponentName - Gets the name of a field component in the `PetscSection`
466b778fa18SValeria Barra 
467b778fa18SValeria Barra   Not Collective
468b778fa18SValeria Barra 
469b778fa18SValeria Barra   Input Parameters:
470cab54364SBarry Smith + s     - the `PetscSection`
471b778fa18SValeria Barra . field - the field number
472cab54364SBarry Smith - comp  - the component number
473cab54364SBarry Smith 
474d5b43468SJose E. Roman   Output Parameter:
475cab54364SBarry Smith . compName - the component name
476b778fa18SValeria Barra 
477b778fa18SValeria Barra   Level: intermediate
478b778fa18SValeria Barra 
479cab54364SBarry Smith   Note:
480cab54364SBarry Smith   Will error if the field or component number do not exist
481cab54364SBarry Smith 
48238b5cf2dSJacob Faibussowitsch   Developer Notes:
483583308b6SBarry Smith   The function name should have Field in it since they are field components.
484583308b6SBarry Smith 
485cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetFieldName()`, `PetscSectionSetNumFields()`, `PetscSectionGetNumFields()`,
486cab54364SBarry Smith           `PetscSectionSetComponentName()`, `PetscSectionSetFieldName()`, `PetscSectionGetFieldComponents()`, `PetscSectionSetFieldComponents()`
487b778fa18SValeria Barra @*/
488d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetComponentName(PetscSection s, PetscInt field, PetscInt comp, const char *compName[])
489d71ae5a4SJacob Faibussowitsch {
490b778fa18SValeria Barra   PetscFunctionBegin;
491b778fa18SValeria Barra   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
4924f572ea9SToby Isaac   PetscAssertPointer(compName, 4);
4932abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
4942abc8c78SJacob Faibussowitsch   PetscSectionCheckValidFieldComponent(comp, s->numFieldComponents[field]);
495b778fa18SValeria Barra   *compName = s->compNames[field][comp];
4963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
497b778fa18SValeria Barra }
498b778fa18SValeria Barra 
499b778fa18SValeria Barra /*@C
500cab54364SBarry Smith   PetscSectionSetComponentName - Sets the name of a field component in the `PetscSection`
501b778fa18SValeria Barra 
502b778fa18SValeria Barra   Not Collective
503b778fa18SValeria Barra 
504b778fa18SValeria Barra   Input Parameters:
50520662ed9SBarry Smith + s        - the `PetscSection`
506b778fa18SValeria Barra . field    - the field number
507b778fa18SValeria Barra . comp     - the component number
508b778fa18SValeria Barra - compName - the component name
509b778fa18SValeria Barra 
510583308b6SBarry Smith   Level: advanced
511b778fa18SValeria Barra 
512cab54364SBarry Smith   Note:
513cab54364SBarry Smith   Will error if the field or component number do not exist
514cab54364SBarry Smith 
51538b5cf2dSJacob Faibussowitsch   Developer Notes:
516583308b6SBarry Smith   The function name should have Field in it since they are field components.
517583308b6SBarry Smith 
518cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetComponentName()`, `PetscSectionSetNumFields()`, `PetscSectionGetNumFields()`,
51942747ad1SJacob Faibussowitsch           `PetscSectionSetFieldName()`, `PetscSectionGetFieldComponents()`, `PetscSectionSetFieldComponents()`
520b778fa18SValeria Barra @*/
521d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetComponentName(PetscSection s, PetscInt field, PetscInt comp, const char compName[])
522d71ae5a4SJacob Faibussowitsch {
523b778fa18SValeria Barra   PetscFunctionBegin;
524b778fa18SValeria Barra   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
5254f572ea9SToby Isaac   if (compName) PetscAssertPointer(compName, 4);
5262abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
5272abc8c78SJacob Faibussowitsch   PetscSectionCheckValidFieldComponent(comp, s->numFieldComponents[field]);
5289566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->compNames[field][comp]));
5299566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(compName, (char **)&s->compNames[field][comp]));
5303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
531b778fa18SValeria Barra }
532b778fa18SValeria Barra 
533ea844a1aSMatthew Knepley /*@
534ea844a1aSMatthew Knepley   PetscSectionGetFieldComponents - Returns the number of field components for the given field.
535ea844a1aSMatthew Knepley 
53640750d41SVaclav Hapla   Not Collective
537ea844a1aSMatthew Knepley 
538ea844a1aSMatthew Knepley   Input Parameters:
539cab54364SBarry Smith + s     - the `PetscSection`
540ea844a1aSMatthew Knepley - field - the field number
541ea844a1aSMatthew Knepley 
542ea844a1aSMatthew Knepley   Output Parameter:
543ea844a1aSMatthew Knepley . numComp - the number of field components
544ea844a1aSMatthew Knepley 
545583308b6SBarry Smith   Level: advanced
546ea844a1aSMatthew Knepley 
54738b5cf2dSJacob Faibussowitsch   Developer Notes:
548cab54364SBarry Smith   This function is misnamed. There is a Num in `PetscSectionGetNumFields()` but not in this name
549cab54364SBarry Smith 
550583308b6SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetFieldComponents()`, `PetscSectionGetNumFields()`,
551583308b6SBarry Smith           `PetscSectionSetComponentName()`, `PetscSectionGetComponentName()`
552ea844a1aSMatthew Knepley @*/
553d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldComponents(PetscSection s, PetscInt field, PetscInt *numComp)
554d71ae5a4SJacob Faibussowitsch {
555ea844a1aSMatthew Knepley   PetscFunctionBegin;
556ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
5574f572ea9SToby Isaac   PetscAssertPointer(numComp, 3);
5582abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
559ea844a1aSMatthew Knepley   *numComp = s->numFieldComponents[field];
5603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
561ea844a1aSMatthew Knepley }
562ea844a1aSMatthew Knepley 
563ea844a1aSMatthew Knepley /*@
564ea844a1aSMatthew Knepley   PetscSectionSetFieldComponents - Sets the number of field components for the given field.
565ea844a1aSMatthew Knepley 
56640750d41SVaclav Hapla   Not Collective
567ea844a1aSMatthew Knepley 
568ea844a1aSMatthew Knepley   Input Parameters:
56920662ed9SBarry Smith + s       - the `PetscSection`
570ea844a1aSMatthew Knepley . field   - the field number
571ea844a1aSMatthew Knepley - numComp - the number of field components
572ea844a1aSMatthew Knepley 
573583308b6SBarry Smith   Level: advanced
574ea844a1aSMatthew Knepley 
575583308b6SBarry Smith   Note:
576583308b6SBarry Smith   This number can be different than the values set with `PetscSectionSetFieldDof()`. It can be used to indicate the number of
577583308b6SBarry Smith   components in the field of the underlying physical model which may be different than the number of degrees of freedom needed
578583308b6SBarry 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
579583308b6SBarry 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.
580583308b6SBarry Smith 
581583308b6SBarry Smith   The value set with this function are not needed or used in `PetscSectionSetUp()`.
582583308b6SBarry Smith 
58338b5cf2dSJacob Faibussowitsch   Developer Notes:
584583308b6SBarry Smith   This function is misnamed. There is a Num in `PetscSectionSetNumFields()` but not in this name
585583308b6SBarry Smith 
586583308b6SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetFieldComponents()`, `PetscSectionSetComponentName()`,
587583308b6SBarry Smith           `PetscSectionGetComponentName()`, `PetscSectionGetNumFields()`
588ea844a1aSMatthew Knepley @*/
589d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFieldComponents(PetscSection s, PetscInt field, PetscInt numComp)
590d71ae5a4SJacob Faibussowitsch {
591b778fa18SValeria Barra   PetscInt c;
592b778fa18SValeria Barra 
593ea844a1aSMatthew Knepley   PetscFunctionBegin;
594ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
5952abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
596b778fa18SValeria Barra   if (s->compNames) {
59748a46eb9SPierre Jolivet     for (c = 0; c < s->numFieldComponents[field]; ++c) PetscCall(PetscFree(s->compNames[field][c]));
5989566063dSJacob Faibussowitsch     PetscCall(PetscFree(s->compNames[field]));
599b778fa18SValeria Barra   }
600b778fa18SValeria Barra 
601ea844a1aSMatthew Knepley   s->numFieldComponents[field] = numComp;
602b778fa18SValeria Barra   if (numComp) {
6039566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numComp, (char ***)&s->compNames[field]));
604b778fa18SValeria Barra     for (c = 0; c < numComp; ++c) {
6052abc8c78SJacob Faibussowitsch       char name[64];
6062abc8c78SJacob Faibussowitsch 
6079566063dSJacob Faibussowitsch       PetscCall(PetscSNPrintf(name, 64, "%" PetscInt_FMT, c));
6089566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(name, (char **)&s->compNames[field][c]));
609b778fa18SValeria Barra     }
610b778fa18SValeria Barra   }
6113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
612ea844a1aSMatthew Knepley }
613ea844a1aSMatthew Knepley 
614ea844a1aSMatthew Knepley /*@
615583308b6SBarry Smith   PetscSectionGetChart - Returns the range [`pStart`, `pEnd`) in which points (indices) lie for this `PetscSection` on this MPI process
616ea844a1aSMatthew Knepley 
61740750d41SVaclav Hapla   Not Collective
618ea844a1aSMatthew Knepley 
619ea844a1aSMatthew Knepley   Input Parameter:
620cab54364SBarry Smith . s - the `PetscSection`
621ea844a1aSMatthew Knepley 
622ea844a1aSMatthew Knepley   Output Parameters:
623ea844a1aSMatthew Knepley + pStart - the first point
624ea844a1aSMatthew Knepley - pEnd   - one past the last point
625ea844a1aSMatthew Knepley 
626ea844a1aSMatthew Knepley   Level: intermediate
627ea844a1aSMatthew Knepley 
628cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetChart()`, `PetscSectionCreate()`
629ea844a1aSMatthew Knepley @*/
630d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetChart(PetscSection s, PetscInt *pStart, PetscInt *pEnd)
631d71ae5a4SJacob Faibussowitsch {
632ea844a1aSMatthew Knepley   PetscFunctionBegin;
633ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
634ea844a1aSMatthew Knepley   if (pStart) *pStart = s->pStart;
635ea844a1aSMatthew Knepley   if (pEnd) *pEnd = s->pEnd;
6363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
637ea844a1aSMatthew Knepley }
638ea844a1aSMatthew Knepley 
639ea844a1aSMatthew Knepley /*@
640583308b6SBarry Smith   PetscSectionSetChart - Sets the range [`pStart`, `pEnd`) in which points (indices) lie for this `PetscSection` on this MPI process
641ea844a1aSMatthew Knepley 
64240750d41SVaclav Hapla   Not Collective
643ea844a1aSMatthew Knepley 
644ea844a1aSMatthew Knepley   Input Parameters:
64520662ed9SBarry Smith + s      - the `PetscSection`
646ea844a1aSMatthew Knepley . pStart - the first point
647376335faSBarry Smith - pEnd   - one past the last point, `pStart` $ \le $ `pEnd`
648ea844a1aSMatthew Knepley 
649ea844a1aSMatthew Knepley   Level: intermediate
650ea844a1aSMatthew Knepley 
651583308b6SBarry Smith   Notes:
652583308b6SBarry Smith   The charts on different MPI processes may (and often do) overlap
653583308b6SBarry Smith 
654583308b6SBarry Smith   If you intend to use `PetscSectionSetNumFields()` it must be called before this call.
655583308b6SBarry Smith 
656583308b6SBarry Smith   The chart for all fields created with `PetscSectionSetNumFields()` is the same as this chart.
657583308b6SBarry Smith 
658583308b6SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetChart()`, `PetscSectionCreate()`, `PetscSectionSetNumFields()`
659ea844a1aSMatthew Knepley @*/
660d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetChart(PetscSection s, PetscInt pStart, PetscInt pEnd)
661d71ae5a4SJacob Faibussowitsch {
662ea844a1aSMatthew Knepley   PetscInt f;
663ea844a1aSMatthew Knepley 
664ea844a1aSMatthew Knepley   PetscFunctionBegin;
665ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
66640196513SBarry Smith   PetscCheck(pEnd >= pStart, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Chart pEnd %" PetscInt_FMT " cannot be smaller than chart pStart %" PetscInt_FMT, pEnd, pStart);
6673ba16761SJacob Faibussowitsch   if (pStart == s->pStart && pEnd == s->pEnd) PetscFunctionReturn(PETSC_SUCCESS);
668ea844a1aSMatthew Knepley   /* Cannot Reset() because it destroys field information */
669ea844a1aSMatthew Knepley   s->setup = PETSC_FALSE;
6709566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&s->bc));
6719566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->bcIndices));
6729566063dSJacob Faibussowitsch   PetscCall(PetscFree2(s->atlasDof, s->atlasOff));
673ea844a1aSMatthew Knepley 
674ea844a1aSMatthew Knepley   s->pStart = pStart;
675ea844a1aSMatthew Knepley   s->pEnd   = pEnd;
6769566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2((pEnd - pStart), &s->atlasDof, (pEnd - pStart), &s->atlasOff));
6779566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(s->atlasDof, pEnd - pStart));
67848a46eb9SPierre Jolivet   for (f = 0; f < s->numFields; ++f) PetscCall(PetscSectionSetChart(s->field[f], pStart, pEnd));
6793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
680ea844a1aSMatthew Knepley }
681ea844a1aSMatthew Knepley 
682ea844a1aSMatthew Knepley /*@
68320662ed9SBarry Smith   PetscSectionGetPermutation - Returns the permutation of [0, `pEnd` - `pStart`) or `NULL` that was set with `PetscSectionSetPermutation()`
684ea844a1aSMatthew Knepley 
68540750d41SVaclav Hapla   Not Collective
686ea844a1aSMatthew Knepley 
687ea844a1aSMatthew Knepley   Input Parameter:
688cab54364SBarry Smith . s - the `PetscSection`
689ea844a1aSMatthew Knepley 
6902fe279fdSBarry Smith   Output Parameter:
691cab54364SBarry Smith . perm - The permutation as an `IS`
692ea844a1aSMatthew Knepley 
693ea844a1aSMatthew Knepley   Level: intermediate
694ea844a1aSMatthew Knepley 
695cab54364SBarry Smith .seealso: [](sec_scatter), `IS`, `PetscSection`, `PetscSectionSetPermutation()`, `PetscSectionCreate()`
696ea844a1aSMatthew Knepley @*/
697d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetPermutation(PetscSection s, IS *perm)
698d71ae5a4SJacob Faibussowitsch {
699ea844a1aSMatthew Knepley   PetscFunctionBegin;
700ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
7019371c9d4SSatish Balay   if (perm) {
7024f572ea9SToby Isaac     PetscAssertPointer(perm, 2);
7039371c9d4SSatish Balay     *perm = s->perm;
7049371c9d4SSatish Balay   }
7053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
706ea844a1aSMatthew Knepley }
707ea844a1aSMatthew Knepley 
708ea844a1aSMatthew Knepley /*@
709583308b6SBarry Smith   PetscSectionSetPermutation - Sets a permutation of the chart for this section, [0, `pEnd` - `pStart`), which determines the order to store the `PetscSection` information
710ea844a1aSMatthew Knepley 
71140750d41SVaclav Hapla   Not Collective
712ea844a1aSMatthew Knepley 
713ea844a1aSMatthew Knepley   Input Parameters:
71420662ed9SBarry Smith + s    - the `PetscSection`
715ea844a1aSMatthew Knepley - perm - the permutation of points
716ea844a1aSMatthew Knepley 
717ea844a1aSMatthew Knepley   Level: intermediate
718ea844a1aSMatthew Knepley 
719583308b6SBarry Smith   Notes:
720583308b6SBarry Smith   The permutation must be provided before `PetscSectionSetUp()`.
721cab54364SBarry Smith 
722583308b6SBarry Smith   The data in the `PetscSection` are permuted but the access via `PetscSectionGetFieldOffset()` and `PetscSectionGetOffset()` is not changed
723583308b6SBarry Smith 
7243ec46b7bSMatthew G. Knepley   Compare to `PetscSectionPermute()`
725583308b6SBarry Smith 
726583308b6SBarry Smith .seealso: [](sec_scatter), `IS`, `PetscSection`, `PetscSectionSetUp()`, `PetscSectionGetPermutation()`, `PetscSectionPermute()`, `PetscSectionCreate()`
727ea844a1aSMatthew Knepley @*/
728d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetPermutation(PetscSection s, IS perm)
729d71ae5a4SJacob Faibussowitsch {
730ea844a1aSMatthew Knepley   PetscFunctionBegin;
731ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
732ea844a1aSMatthew Knepley   if (perm) PetscValidHeaderSpecific(perm, IS_CLASSID, 2);
73328b400f6SJacob Faibussowitsch   PetscCheck(!s->setup, PetscObjectComm((PetscObject)s), PETSC_ERR_ARG_WRONGSTATE, "Cannot set a permutation after the section is setup");
734ea844a1aSMatthew Knepley   if (s->perm != perm) {
7359566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&s->perm));
736ea844a1aSMatthew Knepley     if (perm) {
737ea844a1aSMatthew Knepley       s->perm = perm;
7389566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)s->perm));
739ea844a1aSMatthew Knepley     }
740ea844a1aSMatthew Knepley   }
7413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
742ea844a1aSMatthew Knepley }
743ea844a1aSMatthew Knepley 
7443ec46b7bSMatthew G. Knepley /*@C
7453ec46b7bSMatthew G. Knepley   PetscSectionGetBlockStarts - Returns a table indicating which points start new blocks
7463ec46b7bSMatthew G. Knepley 
7473ec46b7bSMatthew G. Knepley   Not Collective
7483ec46b7bSMatthew G. Knepley 
7493ec46b7bSMatthew G. Knepley   Input Parameter:
7503ec46b7bSMatthew G. Knepley . s - the `PetscSection`
7513ec46b7bSMatthew G. Knepley 
7523ec46b7bSMatthew G. Knepley   Output Parameter:
7533ec46b7bSMatthew G. Knepley . blockStarts - The `PetscBT` with a 1 for each point that begins a block
7543ec46b7bSMatthew G. Knepley 
7553ec46b7bSMatthew G. Knepley   Notes:
7563ec46b7bSMatthew G. Knepley   The table is on [0, `pEnd` - `pStart`).
7573ec46b7bSMatthew G. Knepley 
7583ec46b7bSMatthew G. Knepley   This information is used by `DMCreateMatrix()` to create a variable block size description which is set using `MatSetVariableBlockSizes()`.
7593ec46b7bSMatthew G. Knepley 
7603ec46b7bSMatthew G. Knepley   Level: intermediate
7613ec46b7bSMatthew G. Knepley 
7623ec46b7bSMatthew G. Knepley .seealso: [](sec_scatter), `IS`, `PetscSection`, `PetscSectionSetBlockStarts()`, `PetscSectionCreate()`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
7633ec46b7bSMatthew G. Knepley @*/
7643ec46b7bSMatthew G. Knepley PetscErrorCode PetscSectionGetBlockStarts(PetscSection s, PetscBT *blockStarts)
7653ec46b7bSMatthew G. Knepley {
7663ec46b7bSMatthew G. Knepley   PetscFunctionBegin;
7673ec46b7bSMatthew G. Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
7683ec46b7bSMatthew G. Knepley   if (blockStarts) {
7693ec46b7bSMatthew G. Knepley     PetscAssertPointer(blockStarts, 2);
7703ec46b7bSMatthew G. Knepley     *blockStarts = s->blockStarts;
7713ec46b7bSMatthew G. Knepley   }
7723ec46b7bSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
7733ec46b7bSMatthew G. Knepley }
7743ec46b7bSMatthew G. Knepley 
7753ec46b7bSMatthew G. Knepley /*@C
7763ec46b7bSMatthew G. Knepley   PetscSectionSetBlockStarts - Sets a table indicating which points start new blocks
7773ec46b7bSMatthew G. Knepley 
7783ec46b7bSMatthew G. Knepley   Not Collective
7793ec46b7bSMatthew G. Knepley 
7803ec46b7bSMatthew G. Knepley   Input Parameters:
7813ec46b7bSMatthew G. Knepley + s           - the `PetscSection`
7823ec46b7bSMatthew G. Knepley - blockStarts - The `PetscBT` with a 1 for each point that begins a block
7833ec46b7bSMatthew G. Knepley 
7843ec46b7bSMatthew G. Knepley   Level: intermediate
7853ec46b7bSMatthew G. Knepley 
7863ec46b7bSMatthew G. Knepley   Notes:
7873ec46b7bSMatthew G. Knepley   The table is on [0, `pEnd` - `pStart`). PETSc takes ownership of the `PetscBT` when it is passed in and will destroy it. The user should not destroy it.
7883ec46b7bSMatthew G. Knepley 
7893ec46b7bSMatthew G. Knepley   This information is used by `DMCreateMatrix()` to create a variable block size description which is set using `MatSetVariableBlockSizes()`.
7903ec46b7bSMatthew G. Knepley 
7913ec46b7bSMatthew G. Knepley .seealso: [](sec_scatter), `IS`, `PetscSection`, `PetscSectionGetBlockStarts()`, `PetscSectionCreate()`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
7923ec46b7bSMatthew G. Knepley @*/
7933ec46b7bSMatthew G. Knepley PetscErrorCode PetscSectionSetBlockStarts(PetscSection s, PetscBT blockStarts)
7943ec46b7bSMatthew G. Knepley {
7953ec46b7bSMatthew G. Knepley   PetscFunctionBegin;
7963ec46b7bSMatthew G. Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
7973ec46b7bSMatthew G. Knepley   if (s->blockStarts != blockStarts) {
7983ec46b7bSMatthew G. Knepley     PetscCall(PetscBTDestroy(&s->blockStarts));
7993ec46b7bSMatthew G. Knepley     s->blockStarts = blockStarts;
8003ec46b7bSMatthew G. Knepley   }
8013ec46b7bSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
8023ec46b7bSMatthew G. Knepley }
8033ec46b7bSMatthew G. Knepley 
804ea844a1aSMatthew Knepley /*@
805cab54364SBarry Smith   PetscSectionGetPointMajor - Returns the flag for dof ordering, `PETSC_TRUE` if it is point major, `PETSC_FALSE` if it is field major
806ea844a1aSMatthew Knepley 
80740750d41SVaclav Hapla   Not Collective
808ea844a1aSMatthew Knepley 
809ea844a1aSMatthew Knepley   Input Parameter:
810cab54364SBarry Smith . s - the `PetscSection`
811ea844a1aSMatthew Knepley 
812ea844a1aSMatthew Knepley   Output Parameter:
813ea844a1aSMatthew Knepley . pm - the flag for point major ordering
814ea844a1aSMatthew Knepley 
815ea844a1aSMatthew Knepley   Level: intermediate
816ea844a1aSMatthew Knepley 
81738b5cf2dSJacob Faibussowitsch .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetPointMajor()`
818ea844a1aSMatthew Knepley @*/
819d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetPointMajor(PetscSection s, PetscBool *pm)
820d71ae5a4SJacob Faibussowitsch {
821ea844a1aSMatthew Knepley   PetscFunctionBegin;
822ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
8234f572ea9SToby Isaac   PetscAssertPointer(pm, 2);
824ea844a1aSMatthew Knepley   *pm = s->pointMajor;
8253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
826ea844a1aSMatthew Knepley }
827ea844a1aSMatthew Knepley 
828ea844a1aSMatthew Knepley /*@
829cab54364SBarry Smith   PetscSectionSetPointMajor - Sets the flag for dof ordering, `PETSC_TRUE` for point major, otherwise it will be field major
830ea844a1aSMatthew Knepley 
83140750d41SVaclav Hapla   Not Collective
832ea844a1aSMatthew Knepley 
833ea844a1aSMatthew Knepley   Input Parameters:
834cab54364SBarry Smith + s  - the `PetscSection`
835ea844a1aSMatthew Knepley - pm - the flag for point major ordering
836ea844a1aSMatthew Knepley 
837ea844a1aSMatthew Knepley   Level: intermediate
838ea844a1aSMatthew Knepley 
839583308b6SBarry Smith   Note:
840583308b6SBarry 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.
841583308b6SBarry Smith 
842583308b6SBarry Smith   Point major order means the degrees of freedom are stored as follows
843583308b6SBarry Smith .vb
84411e840c0SPierre Jolivet     all the degrees of freedom for each point are stored contiguously, one point after another (respecting a permutation set with `PetscSectionSetPermutation()`)
845583308b6SBarry Smith     for each point
846583308b6SBarry Smith        the degrees of freedom for each field (starting with the unnamed default field) are listed in order by field
847583308b6SBarry Smith .ve
848583308b6SBarry Smith 
849583308b6SBarry Smith   Field major order means the degrees of freedom are stored as follows
850583308b6SBarry Smith .vb
85111e840c0SPierre Jolivet     all degrees of freedom for each field (including the unnamed default field) are stored contiguously, one field after another
852583308b6SBarry Smith     for each field (started with unnamed default field)
853583308b6SBarry Smith       the degrees of freedom for each point are listed in order by point (respecting a permutation set with `PetscSectionSetPermutation()`)
854583308b6SBarry Smith .ve
855583308b6SBarry Smith 
856583308b6SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetPointMajor()`, `PetscSectionSetPermutation()`
857ea844a1aSMatthew Knepley @*/
858d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetPointMajor(PetscSection s, PetscBool pm)
859d71ae5a4SJacob Faibussowitsch {
860ea844a1aSMatthew Knepley   PetscFunctionBegin;
861ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
86228b400f6SJacob Faibussowitsch   PetscCheck(!s->setup, PetscObjectComm((PetscObject)s), PETSC_ERR_ARG_WRONGSTATE, "Cannot set the dof ordering after the section is setup");
863ea844a1aSMatthew Knepley   s->pointMajor = pm;
8643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
865ea844a1aSMatthew Knepley }
866ea844a1aSMatthew Knepley 
867ea844a1aSMatthew Knepley /*@
868cab54364SBarry Smith   PetscSectionGetIncludesConstraints - Returns the flag indicating if constrained dofs were included when computing offsets in the `PetscSection`.
869cab54364SBarry Smith   The value is set with `PetscSectionSetIncludesConstraints()`
87087e637c6Sksagiyam 
87140750d41SVaclav Hapla   Not Collective
87287e637c6Sksagiyam 
87387e637c6Sksagiyam   Input Parameter:
874cab54364SBarry Smith . s - the `PetscSection`
87587e637c6Sksagiyam 
87687e637c6Sksagiyam   Output Parameter:
87787e637c6Sksagiyam . includesConstraints - the flag indicating if constrained dofs were included when computing offsets
87887e637c6Sksagiyam 
87987e637c6Sksagiyam   Level: intermediate
88087e637c6Sksagiyam 
881cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetIncludesConstraints()`
88287e637c6Sksagiyam @*/
883d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetIncludesConstraints(PetscSection s, PetscBool *includesConstraints)
884d71ae5a4SJacob Faibussowitsch {
88587e637c6Sksagiyam   PetscFunctionBegin;
88687e637c6Sksagiyam   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
8874f572ea9SToby Isaac   PetscAssertPointer(includesConstraints, 2);
88887e637c6Sksagiyam   *includesConstraints = s->includesConstraints;
8893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
89087e637c6Sksagiyam }
89187e637c6Sksagiyam 
89287e637c6Sksagiyam /*@
89387e637c6Sksagiyam   PetscSectionSetIncludesConstraints - Sets the flag indicating if constrained dofs are to be included when computing offsets
89487e637c6Sksagiyam 
89540750d41SVaclav Hapla   Not Collective
89687e637c6Sksagiyam 
89787e637c6Sksagiyam   Input Parameters:
89820662ed9SBarry Smith + s                   - the `PetscSection`
89987e637c6Sksagiyam - includesConstraints - the flag indicating if constrained dofs are to be included when computing offsets
90087e637c6Sksagiyam 
90187e637c6Sksagiyam   Level: intermediate
90287e637c6Sksagiyam 
903cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetIncludesConstraints()`
90487e637c6Sksagiyam @*/
905d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetIncludesConstraints(PetscSection s, PetscBool includesConstraints)
906d71ae5a4SJacob Faibussowitsch {
90787e637c6Sksagiyam   PetscFunctionBegin;
90887e637c6Sksagiyam   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
90928b400f6SJacob Faibussowitsch   PetscCheck(!s->setup, PetscObjectComm((PetscObject)s), PETSC_ERR_ARG_WRONGSTATE, "Cannot set includesConstraints after the section is set up");
91087e637c6Sksagiyam   s->includesConstraints = includesConstraints;
9113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91287e637c6Sksagiyam }
91387e637c6Sksagiyam 
91487e637c6Sksagiyam /*@
915583308b6SBarry Smith   PetscSectionGetDof - Return the total number of degrees of freedom associated with a given point.
916ea844a1aSMatthew Knepley 
91740750d41SVaclav Hapla   Not Collective
918ea844a1aSMatthew Knepley 
919ea844a1aSMatthew Knepley   Input Parameters:
920cab54364SBarry Smith + s     - the `PetscSection`
921ea844a1aSMatthew Knepley - point - the point
922ea844a1aSMatthew Knepley 
923ea844a1aSMatthew Knepley   Output Parameter:
924ea844a1aSMatthew Knepley . numDof - the number of dof
925ea844a1aSMatthew Knepley 
926583308b6SBarry Smith   Level: intermediate
927583308b6SBarry Smith 
928583308b6SBarry Smith   Notes:
929db527f05SMatthew G. Knepley   In a global section, this size will be negative for points not owned by this process.
930db527f05SMatthew G. Knepley 
931583308b6SBarry 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
932ea844a1aSMatthew Knepley 
933cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetDof()`, `PetscSectionCreate()`
934ea844a1aSMatthew Knepley @*/
935d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetDof(PetscSection s, PetscInt point, PetscInt *numDof)
936d71ae5a4SJacob Faibussowitsch {
93776bd3646SJed Brown   PetscFunctionBeginHot;
938ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
9394f572ea9SToby Isaac   PetscAssertPointer(numDof, 3);
940b498ca8aSPierre Jolivet   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);
941ea844a1aSMatthew Knepley   *numDof = s->atlasDof[point - s->pStart];
9423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
943ea844a1aSMatthew Knepley }
944ea844a1aSMatthew Knepley 
945ea844a1aSMatthew Knepley /*@
946583308b6SBarry Smith   PetscSectionSetDof - Sets the total number of degrees of freedom associated with a given point.
947ea844a1aSMatthew Knepley 
94840750d41SVaclav Hapla   Not Collective
949ea844a1aSMatthew Knepley 
950ea844a1aSMatthew Knepley   Input Parameters:
951cab54364SBarry Smith + s      - the `PetscSection`
952ea844a1aSMatthew Knepley . point  - the point
953376335faSBarry Smith - numDof - the number of dof, these values may be negative -(dof+1) to indicate they are off process
954ea844a1aSMatthew Knepley 
955ea844a1aSMatthew Knepley   Level: intermediate
956ea844a1aSMatthew Knepley 
957583308b6SBarry Smith   Note:
958583308b6SBarry 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
959583308b6SBarry Smith 
960cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetDof()`, `PetscSectionAddDof()`, `PetscSectionCreate()`
961ea844a1aSMatthew Knepley @*/
962d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetDof(PetscSection s, PetscInt point, PetscInt numDof)
963d71ae5a4SJacob Faibussowitsch {
964ea844a1aSMatthew Knepley   PetscFunctionBegin;
965ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
966c8bf3acbSVaclav 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);
967ea844a1aSMatthew Knepley   s->atlasDof[point - s->pStart] = numDof;
96869c11d05SVaclav Hapla   PetscCall(PetscSectionInvalidateMaxDof_Internal(s));
9693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
970ea844a1aSMatthew Knepley }
971ea844a1aSMatthew Knepley 
972ea844a1aSMatthew Knepley /*@
973583308b6SBarry Smith   PetscSectionAddDof - Adds to the total number of degrees of freedom associated with a given point.
974ea844a1aSMatthew Knepley 
97540750d41SVaclav Hapla   Not Collective
976ea844a1aSMatthew Knepley 
977ea844a1aSMatthew Knepley   Input Parameters:
978cab54364SBarry Smith + s      - the `PetscSection`
979ea844a1aSMatthew Knepley . point  - the point
980ea844a1aSMatthew Knepley - numDof - the number of additional dof
981ea844a1aSMatthew Knepley 
982ea844a1aSMatthew Knepley   Level: intermediate
983ea844a1aSMatthew Knepley 
984583308b6SBarry Smith   Note:
985583308b6SBarry 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
986583308b6SBarry Smith 
987cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetDof()`, `PetscSectionSetDof()`, `PetscSectionCreate()`
988ea844a1aSMatthew Knepley @*/
989d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionAddDof(PetscSection s, PetscInt point, PetscInt numDof)
990d71ae5a4SJacob Faibussowitsch {
99176bd3646SJed Brown   PetscFunctionBeginHot;
992ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
993b498ca8aSPierre Jolivet   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);
99440196513SBarry Smith   PetscCheck(numDof >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "numDof %" PetscInt_FMT " should not be negative", numDof);
995ea844a1aSMatthew Knepley   s->atlasDof[point - s->pStart] += numDof;
99669c11d05SVaclav Hapla   PetscCall(PetscSectionInvalidateMaxDof_Internal(s));
9973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
998ea844a1aSMatthew Knepley }
999ea844a1aSMatthew Knepley 
1000ea844a1aSMatthew Knepley /*@
1001ea844a1aSMatthew Knepley   PetscSectionGetFieldDof - Return the number of degrees of freedom associated with a field on a given point.
1002ea844a1aSMatthew Knepley 
100340750d41SVaclav Hapla   Not Collective
1004ea844a1aSMatthew Knepley 
1005ea844a1aSMatthew Knepley   Input Parameters:
1006cab54364SBarry Smith + s     - the `PetscSection`
1007ea844a1aSMatthew Knepley . point - the point
1008ea844a1aSMatthew Knepley - field - the field
1009ea844a1aSMatthew Knepley 
1010ea844a1aSMatthew Knepley   Output Parameter:
1011ea844a1aSMatthew Knepley . numDof - the number of dof
1012ea844a1aSMatthew Knepley 
1013ea844a1aSMatthew Knepley   Level: intermediate
1014ea844a1aSMatthew Knepley 
1015cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetFieldDof()`, `PetscSectionCreate()`
1016ea844a1aSMatthew Knepley @*/
1017d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldDof(PetscSection s, PetscInt point, PetscInt field, PetscInt *numDof)
1018d71ae5a4SJacob Faibussowitsch {
1019ea844a1aSMatthew Knepley   PetscFunctionBegin;
1020ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
10214f572ea9SToby Isaac   PetscAssertPointer(numDof, 4);
10222abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
10239566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetDof(s->field[field], point, numDof));
10243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1025ea844a1aSMatthew Knepley }
1026ea844a1aSMatthew Knepley 
1027ea844a1aSMatthew Knepley /*@
1028ea844a1aSMatthew Knepley   PetscSectionSetFieldDof - Sets the number of degrees of freedom associated with a field on a given point.
1029ea844a1aSMatthew Knepley 
103040750d41SVaclav Hapla   Not Collective
1031ea844a1aSMatthew Knepley 
1032ea844a1aSMatthew Knepley   Input Parameters:
1033cab54364SBarry Smith + s      - the `PetscSection`
1034ea844a1aSMatthew Knepley . point  - the point
1035ea844a1aSMatthew Knepley . field  - the field
1036376335faSBarry Smith - numDof - the number of dof, these values may be negative -(dof+1) to indicate they are off process
1037ea844a1aSMatthew Knepley 
1038ea844a1aSMatthew Knepley   Level: intermediate
1039ea844a1aSMatthew Knepley 
1040583308b6SBarry Smith   Note:
1041583308b6SBarry 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
1042583308b6SBarry Smith   the fields and the unnamed default field) is correct by also calling `PetscSectionAddDof()` or `PetscSectionSetDof()`
1043583308b6SBarry Smith 
1044583308b6SBarry Smith   This is equivalent to
1045583308b6SBarry Smith .vb
1046583308b6SBarry Smith      PetscSection fs;
1047583308b6SBarry Smith      PetscSectionGetField(s,field,&fs)
1048583308b6SBarry Smith      PetscSectionSetDof(fs,numDof)
1049583308b6SBarry Smith .ve
1050583308b6SBarry Smith 
1051583308b6SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetFieldDof()`, `PetscSectionCreate()`, `PetscSectionAddDof()`, `PetscSectionSetDof()`
1052ea844a1aSMatthew Knepley @*/
1053d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFieldDof(PetscSection s, PetscInt point, PetscInt field, PetscInt numDof)
1054d71ae5a4SJacob Faibussowitsch {
1055ea844a1aSMatthew Knepley   PetscFunctionBegin;
1056ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
10572abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
10589566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetDof(s->field[field], point, numDof));
10593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1060ea844a1aSMatthew Knepley }
1061ea844a1aSMatthew Knepley 
1062ea844a1aSMatthew Knepley /*@
1063ea844a1aSMatthew Knepley   PetscSectionAddFieldDof - Adds a number of degrees of freedom associated with a field on a given point.
1064ea844a1aSMatthew Knepley 
106540750d41SVaclav Hapla   Not Collective
1066ea844a1aSMatthew Knepley 
1067ea844a1aSMatthew Knepley   Input Parameters:
1068cab54364SBarry Smith + s      - the `PetscSection`
1069ea844a1aSMatthew Knepley . point  - the point
1070ea844a1aSMatthew Knepley . field  - the field
1071ea844a1aSMatthew Knepley - numDof - the number of dof
1072ea844a1aSMatthew Knepley 
1073ea844a1aSMatthew Knepley   Level: intermediate
1074ea844a1aSMatthew Knepley 
1075583308b6SBarry Smith   Notes:
1076583308b6SBarry 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
1077583308b6SBarry Smith   the fields and the unnamed default field) is correct by also calling `PetscSectionAddDof()` or `PetscSectionSetDof()`
1078583308b6SBarry Smith 
1079583308b6SBarry Smith   This is equivalent to
1080583308b6SBarry Smith .vb
1081583308b6SBarry Smith      PetscSection fs;
1082583308b6SBarry Smith      PetscSectionGetField(s,field,&fs)
1083583308b6SBarry Smith      PetscSectionAddDof(fs,numDof)
1084583308b6SBarry Smith .ve
1085583308b6SBarry Smith 
1086cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetFieldDof()`, `PetscSectionGetFieldDof()`, `PetscSectionCreate()`
1087ea844a1aSMatthew Knepley @*/
1088d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionAddFieldDof(PetscSection s, PetscInt point, PetscInt field, PetscInt numDof)
1089d71ae5a4SJacob Faibussowitsch {
1090ea844a1aSMatthew Knepley   PetscFunctionBegin;
1091ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
10922abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
10939566063dSJacob Faibussowitsch   PetscCall(PetscSectionAddDof(s->field[field], point, numDof));
10943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1095ea844a1aSMatthew Knepley }
1096ea844a1aSMatthew Knepley 
1097ea844a1aSMatthew Knepley /*@
1098ea844a1aSMatthew Knepley   PetscSectionGetConstraintDof - Return the number of constrained degrees of freedom associated with a given point.
1099ea844a1aSMatthew Knepley 
110040750d41SVaclav Hapla   Not Collective
1101ea844a1aSMatthew Knepley 
1102ea844a1aSMatthew Knepley   Input Parameters:
1103cab54364SBarry Smith + s     - the `PetscSection`
1104ea844a1aSMatthew Knepley - point - the point
1105ea844a1aSMatthew Knepley 
1106ea844a1aSMatthew Knepley   Output Parameter:
1107ea844a1aSMatthew Knepley . numDof - the number of dof which are fixed by constraints
1108ea844a1aSMatthew Knepley 
1109ea844a1aSMatthew Knepley   Level: intermediate
1110ea844a1aSMatthew Knepley 
1111cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetDof()`, `PetscSectionSetConstraintDof()`, `PetscSectionCreate()`
1112ea844a1aSMatthew Knepley @*/
1113d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetConstraintDof(PetscSection s, PetscInt point, PetscInt *numDof)
1114d71ae5a4SJacob Faibussowitsch {
1115ea844a1aSMatthew Knepley   PetscFunctionBegin;
1116ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
11174f572ea9SToby Isaac   PetscAssertPointer(numDof, 3);
1118ea844a1aSMatthew Knepley   if (s->bc) {
11199566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s->bc, point, numDof));
1120ea844a1aSMatthew Knepley   } else *numDof = 0;
11213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1122ea844a1aSMatthew Knepley }
1123ea844a1aSMatthew Knepley 
1124ea844a1aSMatthew Knepley /*@
1125ea844a1aSMatthew Knepley   PetscSectionSetConstraintDof - Set the number of constrained degrees of freedom associated with a given point.
1126ea844a1aSMatthew Knepley 
112740750d41SVaclav Hapla   Not Collective
1128ea844a1aSMatthew Knepley 
1129ea844a1aSMatthew Knepley   Input Parameters:
1130cab54364SBarry Smith + s      - the `PetscSection`
1131ea844a1aSMatthew Knepley . point  - the point
1132ea844a1aSMatthew Knepley - numDof - the number of dof which are fixed by constraints
1133ea844a1aSMatthew Knepley 
1134ea844a1aSMatthew Knepley   Level: intermediate
1135ea844a1aSMatthew Knepley 
1136cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetDof()`, `PetscSectionGetConstraintDof()`, `PetscSectionCreate()`
1137ea844a1aSMatthew Knepley @*/
1138d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetConstraintDof(PetscSection s, PetscInt point, PetscInt numDof)
1139d71ae5a4SJacob Faibussowitsch {
1140ea844a1aSMatthew Knepley   PetscFunctionBegin;
1141ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1142ea844a1aSMatthew Knepley   if (numDof) {
11437c0883d5SVaclav Hapla     PetscCall(PetscSectionCheckConstraints_Private(s));
11449566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(s->bc, point, numDof));
1145ea844a1aSMatthew Knepley   }
11463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1147ea844a1aSMatthew Knepley }
1148ea844a1aSMatthew Knepley 
1149ea844a1aSMatthew Knepley /*@
1150ea844a1aSMatthew Knepley   PetscSectionAddConstraintDof - Increment the number of constrained degrees of freedom associated with a given point.
1151ea844a1aSMatthew Knepley 
115240750d41SVaclav Hapla   Not Collective
1153ea844a1aSMatthew Knepley 
1154ea844a1aSMatthew Knepley   Input Parameters:
1155cab54364SBarry Smith + s      - the `PetscSection`
1156ea844a1aSMatthew Knepley . point  - the point
1157ea844a1aSMatthew Knepley - numDof - the number of additional dof which are fixed by constraints
1158ea844a1aSMatthew Knepley 
1159ea844a1aSMatthew Knepley   Level: intermediate
1160ea844a1aSMatthew Knepley 
1161cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionAddDof()`, `PetscSectionGetConstraintDof()`, `PetscSectionCreate()`
1162ea844a1aSMatthew Knepley @*/
1163d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionAddConstraintDof(PetscSection s, PetscInt point, PetscInt numDof)
1164d71ae5a4SJacob Faibussowitsch {
1165ea844a1aSMatthew Knepley   PetscFunctionBegin;
1166ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1167ea844a1aSMatthew Knepley   if (numDof) {
11687c0883d5SVaclav Hapla     PetscCall(PetscSectionCheckConstraints_Private(s));
11699566063dSJacob Faibussowitsch     PetscCall(PetscSectionAddDof(s->bc, point, numDof));
1170ea844a1aSMatthew Knepley   }
11713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1172ea844a1aSMatthew Knepley }
1173ea844a1aSMatthew Knepley 
1174ea844a1aSMatthew Knepley /*@
1175ea844a1aSMatthew Knepley   PetscSectionGetFieldConstraintDof - Return the number of constrained degrees of freedom associated with a given field on a point.
1176ea844a1aSMatthew Knepley 
117740750d41SVaclav Hapla   Not Collective
1178ea844a1aSMatthew Knepley 
1179ea844a1aSMatthew Knepley   Input Parameters:
1180cab54364SBarry Smith + s     - the `PetscSection`
1181ea844a1aSMatthew Knepley . point - the point
1182ea844a1aSMatthew Knepley - field - the field
1183ea844a1aSMatthew Knepley 
1184ea844a1aSMatthew Knepley   Output Parameter:
1185ea844a1aSMatthew Knepley . numDof - the number of dof which are fixed by constraints
1186ea844a1aSMatthew Knepley 
1187ea844a1aSMatthew Knepley   Level: intermediate
1188ea844a1aSMatthew Knepley 
1189cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetDof()`, `PetscSectionSetFieldConstraintDof()`, `PetscSectionCreate()`
1190ea844a1aSMatthew Knepley @*/
1191d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldConstraintDof(PetscSection s, PetscInt point, PetscInt field, PetscInt *numDof)
1192d71ae5a4SJacob Faibussowitsch {
1193ea844a1aSMatthew Knepley   PetscFunctionBegin;
1194ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
11954f572ea9SToby Isaac   PetscAssertPointer(numDof, 4);
11962abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
11979566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstraintDof(s->field[field], point, numDof));
11983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1199ea844a1aSMatthew Knepley }
1200ea844a1aSMatthew Knepley 
1201ea844a1aSMatthew Knepley /*@
1202ea844a1aSMatthew Knepley   PetscSectionSetFieldConstraintDof - Set the number of constrained degrees of freedom associated with a given field on a point.
1203ea844a1aSMatthew Knepley 
120440750d41SVaclav Hapla   Not Collective
1205ea844a1aSMatthew Knepley 
1206ea844a1aSMatthew Knepley   Input Parameters:
1207cab54364SBarry Smith + s      - the `PetscSection`
1208ea844a1aSMatthew Knepley . point  - the point
1209ea844a1aSMatthew Knepley . field  - the field
1210ea844a1aSMatthew Knepley - numDof - the number of dof which are fixed by constraints
1211ea844a1aSMatthew Knepley 
1212ea844a1aSMatthew Knepley   Level: intermediate
1213ea844a1aSMatthew Knepley 
1214cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetDof()`, `PetscSectionGetFieldConstraintDof()`, `PetscSectionCreate()`
1215ea844a1aSMatthew Knepley @*/
1216d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFieldConstraintDof(PetscSection s, PetscInt point, PetscInt field, PetscInt numDof)
1217d71ae5a4SJacob Faibussowitsch {
1218ea844a1aSMatthew Knepley   PetscFunctionBegin;
1219ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
12202abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
12219566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetConstraintDof(s->field[field], point, numDof));
12223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1223ea844a1aSMatthew Knepley }
1224ea844a1aSMatthew Knepley 
1225ea844a1aSMatthew Knepley /*@
1226ea844a1aSMatthew Knepley   PetscSectionAddFieldConstraintDof - Increment the number of constrained degrees of freedom associated with a given field on a point.
1227ea844a1aSMatthew Knepley 
122840750d41SVaclav Hapla   Not Collective
1229ea844a1aSMatthew Knepley 
1230ea844a1aSMatthew Knepley   Input Parameters:
1231cab54364SBarry Smith + s      - the `PetscSection`
1232ea844a1aSMatthew Knepley . point  - the point
1233ea844a1aSMatthew Knepley . field  - the field
1234ea844a1aSMatthew Knepley - numDof - the number of additional dof which are fixed by constraints
1235ea844a1aSMatthew Knepley 
1236ea844a1aSMatthew Knepley   Level: intermediate
1237ea844a1aSMatthew Knepley 
1238cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionAddDof()`, `PetscSectionGetFieldConstraintDof()`, `PetscSectionCreate()`
1239ea844a1aSMatthew Knepley @*/
1240d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionAddFieldConstraintDof(PetscSection s, PetscInt point, PetscInt field, PetscInt numDof)
1241d71ae5a4SJacob Faibussowitsch {
1242ea844a1aSMatthew Knepley   PetscFunctionBegin;
1243ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
12442abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
12459566063dSJacob Faibussowitsch   PetscCall(PetscSectionAddConstraintDof(s->field[field], point, numDof));
12463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1247ea844a1aSMatthew Knepley }
1248ea844a1aSMatthew Knepley 
1249ea844a1aSMatthew Knepley /*@
1250ea844a1aSMatthew Knepley   PetscSectionSetUpBC - Setup the subsections describing boundary conditions.
1251ea844a1aSMatthew Knepley 
125240750d41SVaclav Hapla   Not Collective
1253ea844a1aSMatthew Knepley 
1254ea844a1aSMatthew Knepley   Input Parameter:
1255cab54364SBarry Smith . s - the `PetscSection`
1256ea844a1aSMatthew Knepley 
1257ea844a1aSMatthew Knepley   Level: advanced
1258ea844a1aSMatthew Knepley 
1259cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSetUp()`, `PetscSectionCreate()`
1260ea844a1aSMatthew Knepley @*/
1261d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetUpBC(PetscSection s)
1262d71ae5a4SJacob Faibussowitsch {
1263ea844a1aSMatthew Knepley   PetscFunctionBegin;
1264ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1265ea844a1aSMatthew Knepley   if (s->bc) {
1266ea844a1aSMatthew Knepley     const PetscInt last = (s->bc->pEnd - s->bc->pStart) - 1;
1267ea844a1aSMatthew Knepley 
12689566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(s->bc));
12698da24d32SBarry Smith     PetscCall(PetscMalloc1((last >= 0 ? s->bc->atlasOff[last] + s->bc->atlasDof[last] : 0), &s->bcIndices));
1270ea844a1aSMatthew Knepley   }
12713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1272ea844a1aSMatthew Knepley }
1273ea844a1aSMatthew Knepley 
1274ea844a1aSMatthew Knepley /*@
1275583308b6SBarry Smith   PetscSectionSetUp - Calculate offsets based upon the number of degrees of freedom for each point in preparation for use of the `PetscSection`
1276ea844a1aSMatthew Knepley 
127740750d41SVaclav Hapla   Not Collective
1278ea844a1aSMatthew Knepley 
1279ea844a1aSMatthew Knepley   Input Parameter:
1280cab54364SBarry Smith . s - the `PetscSection`
1281ea844a1aSMatthew Knepley 
1282ea844a1aSMatthew Knepley   Level: intermediate
1283ea844a1aSMatthew Knepley 
1284583308b6SBarry Smith   Notes:
1285583308b6SBarry Smith   If used, `PetscSectionSetPermutation()` must be called before this routine.
1286583308b6SBarry Smith 
1287583308b6SBarry Smith   `PetscSectionSetPointMajor()`, cannot be called after this routine.
1288583308b6SBarry Smith 
1289583308b6SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionSetPermutation()`
1290ea844a1aSMatthew Knepley @*/
1291d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetUp(PetscSection s)
1292d71ae5a4SJacob Faibussowitsch {
1293ea844a1aSMatthew Knepley   const PetscInt *pind   = NULL;
1294ea844a1aSMatthew Knepley   PetscInt        offset = 0, foff, p, f;
1295ea844a1aSMatthew Knepley 
1296ea844a1aSMatthew Knepley   PetscFunctionBegin;
1297ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
12983ba16761SJacob Faibussowitsch   if (s->setup) PetscFunctionReturn(PETSC_SUCCESS);
1299ea844a1aSMatthew Knepley   s->setup = PETSC_TRUE;
1300ea844a1aSMatthew Knepley   /* Set offsets and field offsets for all points */
1301ea844a1aSMatthew Knepley   /*   Assume that all fields have the same chart */
130228b400f6SJacob Faibussowitsch   PetscCheck(s->includesConstraints, PETSC_COMM_SELF, PETSC_ERR_SUP, "PetscSectionSetUp is currently unsupported for includesConstraints = PETSC_TRUE");
13039566063dSJacob Faibussowitsch   if (s->perm) PetscCall(ISGetIndices(s->perm, &pind));
1304ea844a1aSMatthew Knepley   if (s->pointMajor) {
1305ea844a1aSMatthew Knepley     for (p = 0; p < s->pEnd - s->pStart; ++p) {
1306ea844a1aSMatthew Knepley       const PetscInt q = pind ? pind[p] : p;
1307ea844a1aSMatthew Knepley 
1308ea844a1aSMatthew Knepley       /* Set point offset */
1309ea844a1aSMatthew Knepley       s->atlasOff[q] = offset;
1310ea844a1aSMatthew Knepley       offset += s->atlasDof[q];
1311ea844a1aSMatthew Knepley       /* Set field offset */
1312ea844a1aSMatthew Knepley       for (f = 0, foff = s->atlasOff[q]; f < s->numFields; ++f) {
1313ea844a1aSMatthew Knepley         PetscSection sf = s->field[f];
1314ea844a1aSMatthew Knepley 
1315ea844a1aSMatthew Knepley         sf->atlasOff[q] = foff;
1316ea844a1aSMatthew Knepley         foff += sf->atlasDof[q];
1317ea844a1aSMatthew Knepley       }
1318ea844a1aSMatthew Knepley     }
1319ea844a1aSMatthew Knepley   } else {
1320ea844a1aSMatthew Knepley     /* Set field offsets for all points */
1321ea844a1aSMatthew Knepley     for (f = 0; f < s->numFields; ++f) {
1322ea844a1aSMatthew Knepley       PetscSection sf = s->field[f];
1323ea844a1aSMatthew Knepley 
1324ea844a1aSMatthew Knepley       for (p = 0; p < s->pEnd - s->pStart; ++p) {
1325ea844a1aSMatthew Knepley         const PetscInt q = pind ? pind[p] : p;
1326ea844a1aSMatthew Knepley 
1327ea844a1aSMatthew Knepley         sf->atlasOff[q] = offset;
1328ea844a1aSMatthew Knepley         offset += sf->atlasDof[q];
1329ea844a1aSMatthew Knepley       }
1330ea844a1aSMatthew Knepley     }
1331ea844a1aSMatthew Knepley     /* Disable point offsets since these are unused */
1332ad540459SPierre Jolivet     for (p = 0; p < s->pEnd - s->pStart; ++p) s->atlasOff[p] = -1;
1333ea844a1aSMatthew Knepley   }
13349566063dSJacob Faibussowitsch   if (s->perm) PetscCall(ISRestoreIndices(s->perm, &pind));
1335ea844a1aSMatthew Knepley   /* Setup BC sections */
13369566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUpBC(s));
13379566063dSJacob Faibussowitsch   for (f = 0; f < s->numFields; ++f) PetscCall(PetscSectionSetUpBC(s->field[f]));
13383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1339ea844a1aSMatthew Knepley }
1340ea844a1aSMatthew Knepley 
1341ea844a1aSMatthew Knepley /*@
134220662ed9SBarry Smith   PetscSectionGetMaxDof - Return the maximum number of degrees of freedom on any point in the `PetscSection`
1343ea844a1aSMatthew Knepley 
134440750d41SVaclav Hapla   Not Collective
1345ea844a1aSMatthew Knepley 
13462fe279fdSBarry Smith   Input Parameter:
1347cab54364SBarry Smith . s - the `PetscSection`
1348ea844a1aSMatthew Knepley 
1349ea844a1aSMatthew Knepley   Output Parameter:
1350ea844a1aSMatthew Knepley . maxDof - the maximum dof
1351ea844a1aSMatthew Knepley 
1352ea844a1aSMatthew Knepley   Level: intermediate
1353ea844a1aSMatthew Knepley 
1354583308b6SBarry Smith   Notes:
1355cab54364SBarry Smith   The returned number is up-to-date without need for `PetscSectionSetUp()`.
135669c11d05SVaclav Hapla 
1357583308b6SBarry 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
1358583308b6SBarry Smith   the maximum over all points of the value returned by `PetscSectionGetDof()` on this MPI process
1359583308b6SBarry Smith 
1360583308b6SBarry Smith   Developer Notes:
136169c11d05SVaclav Hapla   The returned number is calculated lazily and stashed.
1362cab54364SBarry Smith 
1363cab54364SBarry Smith   A call to `PetscSectionInvalidateMaxDof_Internal()` invalidates the stashed value.
1364cab54364SBarry Smith 
1365cab54364SBarry Smith   `PetscSectionInvalidateMaxDof_Internal()` is called in `PetscSectionSetDof()`, `PetscSectionAddDof()` and `PetscSectionReset()`
1366cab54364SBarry Smith 
136720662ed9SBarry Smith   It should also be called every time `atlasDof` is modified directly.
136869c11d05SVaclav Hapla 
1369cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetDof()`, `PetscSectionSetDof()`, `PetscSectionAddDof()`, `PetscSectionCreate()`
1370ea844a1aSMatthew Knepley @*/
1371d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetMaxDof(PetscSection s, PetscInt *maxDof)
1372d71ae5a4SJacob Faibussowitsch {
137369c11d05SVaclav Hapla   PetscInt p;
137469c11d05SVaclav Hapla 
1375ea844a1aSMatthew Knepley   PetscFunctionBegin;
1376ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
13774f572ea9SToby Isaac   PetscAssertPointer(maxDof, 2);
137869c11d05SVaclav Hapla   if (s->maxDof == PETSC_MIN_INT) {
137969c11d05SVaclav Hapla     s->maxDof = 0;
1380ad540459SPierre Jolivet     for (p = 0; p < s->pEnd - s->pStart; ++p) s->maxDof = PetscMax(s->maxDof, s->atlasDof[p]);
138169c11d05SVaclav Hapla   }
1382ea844a1aSMatthew Knepley   *maxDof = s->maxDof;
13833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1384ea844a1aSMatthew Knepley }
1385ea844a1aSMatthew Knepley 
1386ea844a1aSMatthew Knepley /*@
138720662ed9SBarry Smith   PetscSectionGetStorageSize - Return the size of an array or local `Vec` capable of holding all the degrees of freedom defined in a `PetscSection`
1388ea844a1aSMatthew Knepley 
138940750d41SVaclav Hapla   Not Collective
1390ea844a1aSMatthew Knepley 
1391ea844a1aSMatthew Knepley   Input Parameter:
1392cab54364SBarry Smith . s - the `PetscSection`
1393ea844a1aSMatthew Knepley 
1394ea844a1aSMatthew Knepley   Output Parameter:
1395ea844a1aSMatthew Knepley . size - the size of an array which can hold all the dofs
1396ea844a1aSMatthew Knepley 
1397ea844a1aSMatthew Knepley   Level: intermediate
1398ea844a1aSMatthew Knepley 
1399cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetOffset()`, `PetscSectionGetConstrainedStorageSize()`, `PetscSectionCreate()`
1400ea844a1aSMatthew Knepley @*/
1401d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetStorageSize(PetscSection s, PetscInt *size)
1402d71ae5a4SJacob Faibussowitsch {
1403ea844a1aSMatthew Knepley   PetscInt p, n = 0;
1404ea844a1aSMatthew Knepley 
1405ea844a1aSMatthew Knepley   PetscFunctionBegin;
1406ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
14074f572ea9SToby Isaac   PetscAssertPointer(size, 2);
1408ea844a1aSMatthew Knepley   for (p = 0; p < s->pEnd - s->pStart; ++p) n += s->atlasDof[p] > 0 ? s->atlasDof[p] : 0;
1409ea844a1aSMatthew Knepley   *size = n;
14103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1411ea844a1aSMatthew Knepley }
1412ea844a1aSMatthew Knepley 
1413ea844a1aSMatthew Knepley /*@
141420662ed9SBarry Smith   PetscSectionGetConstrainedStorageSize - Return the size of an array or local `Vec` capable of holding all unconstrained degrees of freedom in a `PetscSection`
1415ea844a1aSMatthew Knepley 
141640750d41SVaclav Hapla   Not Collective
1417ea844a1aSMatthew Knepley 
1418064ec1f3Sprj-   Input Parameter:
1419cab54364SBarry Smith . s - the `PetscSection`
1420ea844a1aSMatthew Knepley 
1421ea844a1aSMatthew Knepley   Output Parameter:
1422ea844a1aSMatthew Knepley . size - the size of an array which can hold all unconstrained dofs
1423ea844a1aSMatthew Knepley 
1424ea844a1aSMatthew Knepley   Level: intermediate
1425ea844a1aSMatthew Knepley 
1426cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetStorageSize()`, `PetscSectionGetOffset()`, `PetscSectionCreate()`
1427ea844a1aSMatthew Knepley @*/
1428d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetConstrainedStorageSize(PetscSection s, PetscInt *size)
1429d71ae5a4SJacob Faibussowitsch {
1430ea844a1aSMatthew Knepley   PetscInt p, n = 0;
1431ea844a1aSMatthew Knepley 
1432ea844a1aSMatthew Knepley   PetscFunctionBegin;
1433ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
14344f572ea9SToby Isaac   PetscAssertPointer(size, 2);
1435ea844a1aSMatthew Knepley   for (p = 0; p < s->pEnd - s->pStart; ++p) {
1436ea844a1aSMatthew Knepley     const PetscInt cdof = s->bc ? s->bc->atlasDof[p] : 0;
1437ea844a1aSMatthew Knepley     n += s->atlasDof[p] > 0 ? s->atlasDof[p] - cdof : 0;
1438ea844a1aSMatthew Knepley   }
1439ea844a1aSMatthew Knepley   *size = n;
14403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1441ea844a1aSMatthew Knepley }
1442ea844a1aSMatthew Knepley 
1443ea844a1aSMatthew Knepley /*@
1444583308b6SBarry Smith   PetscSectionCreateGlobalSection - Create a parallel section describing the global layout using
1445583308b6SBarry Smith   a local (sequential) `PetscSection` on each MPI process and a `PetscSF` describing the section point overlap.
1446ea844a1aSMatthew Knepley 
1447ea844a1aSMatthew Knepley   Input Parameters:
1448cab54364SBarry Smith + s                  - The `PetscSection` for the local field layout
1449cab54364SBarry Smith . sf                 - The `PetscSF` describing parallel layout of the section points (leaves are unowned local points)
1450*eb9d3e4dSMatthew G. Knepley . usePermutation     - By default this is `PETSC_TRUE`, meaning any permutation of the local section is transferred to the global section
1451cab54364SBarry Smith . includeConstraints - By default this is `PETSC_FALSE`, meaning that the global field vector will not possess constrained dofs
1452cab54364SBarry Smith - localOffsets       - If `PETSC_TRUE`, use local rather than global offsets for the points
1453ea844a1aSMatthew Knepley 
1454ea844a1aSMatthew Knepley   Output Parameter:
1455cab54364SBarry Smith . gsection - The `PetscSection` for the global field layout
1456ea844a1aSMatthew Knepley 
1457ea844a1aSMatthew Knepley   Level: intermediate
1458ea844a1aSMatthew Knepley 
1459db527f05SMatthew G. Knepley   Notes:
1460583308b6SBarry Smith   On each MPI process `gsection` inherits the chart of the `s` on that process.
1461db527f05SMatthew G. Knepley 
1462583308b6SBarry 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`.
1463583308b6SBarry Smith   In those locations the value of size is -(size+1) and the value of the offset on the remote process is -(off+1).
1464cab54364SBarry Smith 
146520662ed9SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionCreateGlobalSectionCensored()`
1466ea844a1aSMatthew Knepley @*/
1467*eb9d3e4dSMatthew G. Knepley PetscErrorCode PetscSectionCreateGlobalSection(PetscSection s, PetscSF sf, PetscBool usePermutation, PetscBool includeConstraints, PetscBool localOffsets, PetscSection *gsection)
1468d71ae5a4SJacob Faibussowitsch {
1469ea844a1aSMatthew Knepley   PetscSection    gs;
1470ea844a1aSMatthew Knepley   const PetscInt *pind = NULL;
1471ea844a1aSMatthew Knepley   PetscInt       *recv = NULL, *neg = NULL;
1472046d2115Sksagiyam   PetscInt        pStart, pEnd, p, dof, cdof, off, foff, globalOff = 0, nroots, nlocal, maxleaf;
1473046d2115Sksagiyam   PetscInt        numFields, f, numComponents;
1474ea844a1aSMatthew Knepley 
1475ea844a1aSMatthew Knepley   PetscFunctionBegin;
1476ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1477ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
1478*eb9d3e4dSMatthew G. Knepley   PetscValidLogicalCollectiveBool(s, usePermutation, 3);
1479*eb9d3e4dSMatthew G. Knepley   PetscValidLogicalCollectiveBool(s, includeConstraints, 4);
1480*eb9d3e4dSMatthew G. Knepley   PetscValidLogicalCollectiveBool(s, localOffsets, 5);
1481*eb9d3e4dSMatthew G. Knepley   PetscAssertPointer(gsection, 6);
148228b400f6SJacob Faibussowitsch   PetscCheck(s->pointMajor, PETSC_COMM_SELF, PETSC_ERR_SUP, "No support for field major ordering");
14839566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s), &gs));
14849566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &numFields));
14859566063dSJacob Faibussowitsch   if (numFields > 0) PetscCall(PetscSectionSetNumFields(gs, numFields));
14869566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
14879566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(gs, pStart, pEnd));
148887e637c6Sksagiyam   gs->includesConstraints = includeConstraints;
14899566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, &nroots, NULL, NULL, NULL));
1490ea844a1aSMatthew Knepley   nlocal = nroots; /* The local/leaf space matches global/root space */
1491ea844a1aSMatthew Knepley   /* Must allocate for all points visible to SF, which may be more than this section */
1492ea844a1aSMatthew Knepley   if (nroots >= 0) { /* nroots < 0 means that the graph has not been set, only happens in serial */
14939566063dSJacob Faibussowitsch     PetscCall(PetscSFGetLeafRange(sf, NULL, &maxleaf));
149408401ef6SPierre Jolivet     PetscCheck(nroots >= pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "SF roots %" PetscInt_FMT " < pEnd %" PetscInt_FMT, nroots, pEnd);
149508401ef6SPierre Jolivet     PetscCheck(maxleaf < nroots, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Max local leaf %" PetscInt_FMT " >= nroots %" PetscInt_FMT, maxleaf, nroots);
14969566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(nroots, &neg, nlocal, &recv));
14979566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(neg, nroots));
1498ea844a1aSMatthew Knepley   }
1499ea844a1aSMatthew Knepley   /* Mark all local points with negative dof */
1500ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
15019566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s, p, &dof));
15029566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(gs, p, dof));
15039566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
15049566063dSJacob Faibussowitsch     if (!includeConstraints && cdof > 0) PetscCall(PetscSectionSetConstraintDof(gs, p, cdof));
1505ea844a1aSMatthew Knepley     if (neg) neg[p] = -(dof + 1);
1506ea844a1aSMatthew Knepley   }
15079566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUpBC(gs));
15089566063dSJacob 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]));
1509ea844a1aSMatthew Knepley   if (nroots >= 0) {
15109566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(recv, nlocal));
15119566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, neg, recv, MPI_REPLACE));
15129566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, neg, recv, MPI_REPLACE));
1513ea844a1aSMatthew Knepley     for (p = pStart; p < pEnd; ++p) {
1514ea844a1aSMatthew Knepley       if (recv[p] < 0) {
1515ea844a1aSMatthew Knepley         gs->atlasDof[p - pStart] = recv[p];
15169566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
151708401ef6SPierre 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);
1518ea844a1aSMatthew Knepley       }
1519ea844a1aSMatthew Knepley     }
1520ea844a1aSMatthew Knepley   }
1521ea844a1aSMatthew Knepley   /* Calculate new sizes, get process offset, and calculate point offsets */
1522*eb9d3e4dSMatthew G. Knepley   if (usePermutation && s->perm) PetscCall(ISGetIndices(s->perm, &pind));
1523ea844a1aSMatthew Knepley   for (p = 0, off = 0; p < pEnd - pStart; ++p) {
1524ea844a1aSMatthew Knepley     const PetscInt q = pind ? pind[p] : p;
1525ea844a1aSMatthew Knepley 
1526ea844a1aSMatthew Knepley     cdof            = (!includeConstraints && s->bc) ? s->bc->atlasDof[q] : 0;
1527ea844a1aSMatthew Knepley     gs->atlasOff[q] = off;
1528ea844a1aSMatthew Knepley     off += gs->atlasDof[q] > 0 ? gs->atlasDof[q] - cdof : 0;
1529ea844a1aSMatthew Knepley   }
1530ea844a1aSMatthew Knepley   if (!localOffsets) {
15319566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Scan(&off, &globalOff, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)s)));
1532ea844a1aSMatthew Knepley     globalOff -= off;
1533ea844a1aSMatthew Knepley   }
1534ea844a1aSMatthew Knepley   for (p = pStart, off = 0; p < pEnd; ++p) {
1535ea844a1aSMatthew Knepley     gs->atlasOff[p - pStart] += globalOff;
1536ea844a1aSMatthew Knepley     if (neg) neg[p] = -(gs->atlasOff[p - pStart] + 1);
1537ea844a1aSMatthew Knepley   }
1538*eb9d3e4dSMatthew G. Knepley   if (usePermutation && s->perm) PetscCall(ISRestoreIndices(s->perm, &pind));
1539ea844a1aSMatthew Knepley   /* Put in negative offsets for ghost points */
1540ea844a1aSMatthew Knepley   if (nroots >= 0) {
15419566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(recv, nlocal));
15429566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, neg, recv, MPI_REPLACE));
15439566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, neg, recv, MPI_REPLACE));
1544ea844a1aSMatthew Knepley     for (p = pStart; p < pEnd; ++p) {
1545ea844a1aSMatthew Knepley       if (recv[p] < 0) gs->atlasOff[p - pStart] = recv[p];
1546ea844a1aSMatthew Knepley     }
1547ea844a1aSMatthew Knepley   }
15489566063dSJacob Faibussowitsch   PetscCall(PetscFree2(neg, recv));
1549046d2115Sksagiyam   /* Set field dofs/offsets/constraints */
1550046d2115Sksagiyam   for (f = 0; f < numFields; ++f) {
15514cd8913cSStefano Zampini     const char *name;
15524cd8913cSStefano Zampini 
1553046d2115Sksagiyam     gs->field[f]->includesConstraints = includeConstraints;
15549566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &numComponents));
15559566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(gs, f, numComponents));
15564cd8913cSStefano Zampini     PetscCall(PetscSectionGetFieldName(s, f, &name));
15574cd8913cSStefano Zampini     PetscCall(PetscSectionSetFieldName(gs, f, name));
1558046d2115Sksagiyam   }
1559046d2115Sksagiyam   for (p = pStart; p < pEnd; ++p) {
15609566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(gs, p, &off));
1561046d2115Sksagiyam     for (f = 0, foff = off; f < numFields; ++f) {
15629566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(s, p, f, &cdof));
15639566063dSJacob Faibussowitsch       if (!includeConstraints && cdof > 0) PetscCall(PetscSectionSetFieldConstraintDof(gs, p, f, cdof));
15649566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
15659566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(gs, p, f, off < 0 ? -(dof + 1) : dof));
15669566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldOffset(gs, p, f, foff));
15679566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(gs, p, f, &cdof));
1568046d2115Sksagiyam       foff = off < 0 ? foff - (dof - cdof) : foff + (dof - cdof);
1569046d2115Sksagiyam     }
1570046d2115Sksagiyam   }
1571046d2115Sksagiyam   for (f = 0; f < numFields; ++f) {
1572046d2115Sksagiyam     PetscSection gfs = gs->field[f];
1573046d2115Sksagiyam 
15749566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUpBC(gfs));
15759566063dSJacob 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]));
1576046d2115Sksagiyam   }
1577046d2115Sksagiyam   gs->setup = PETSC_TRUE;
15789566063dSJacob Faibussowitsch   PetscCall(PetscSectionViewFromOptions(gs, NULL, "-global_section_view"));
1579ea844a1aSMatthew Knepley   *gsection = gs;
15803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1581ea844a1aSMatthew Knepley }
1582ea844a1aSMatthew Knepley 
1583ea844a1aSMatthew Knepley /*@
1584583308b6SBarry Smith   PetscSectionCreateGlobalSectionCensored - Create a `PetscSection` describing the globallayout using
1585583308b6SBarry Smith   a local (sequential) `PetscSection` on each MPI process and an `PetscSF` describing the section point overlap.
1586ea844a1aSMatthew Knepley 
1587ea844a1aSMatthew Knepley   Input Parameters:
1588cab54364SBarry Smith + s                  - The `PetscSection` for the local field layout
1589cab54364SBarry Smith . sf                 - The `PetscSF` describing parallel layout of the section points
1590583308b6SBarry Smith . includeConstraints - By default this is `PETSC_FALSE`, meaning that the global vector will not possess constrained dofs
1591583308b6SBarry Smith . numExcludes        - The number of exclusion ranges, this must have the same value on all MPI processes
1592583308b6SBarry 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
1593ea844a1aSMatthew Knepley 
1594ea844a1aSMatthew Knepley   Output Parameter:
1595cab54364SBarry Smith . gsection - The `PetscSection` for the global field layout
1596ea844a1aSMatthew Knepley 
1597ea844a1aSMatthew Knepley   Level: advanced
1598ea844a1aSMatthew Knepley 
1599583308b6SBarry Smith   Notes:
1600583308b6SBarry Smith   On each MPI process `gsection` inherits the chart of the `s` on that process.
160120662ed9SBarry Smith 
1602583308b6SBarry 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`.
1603583308b6SBarry Smith   In those locations the value of size is -(size+1) and the value of the offset on the remote process is -(off+1).
1604583308b6SBarry Smith 
1605583308b6SBarry Smith   This routine augments `PetscSectionCreateGlobalSection()` by allowing one to exclude certain ranges in the chart of the `PetscSection`
1606cab54364SBarry Smith 
160738b5cf2dSJacob Faibussowitsch   Developer Notes:
160820662ed9SBarry Smith   This is a terrible function name
160920662ed9SBarry Smith 
161042747ad1SJacob Faibussowitsch .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreate()`
1611ea844a1aSMatthew Knepley @*/
1612d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCreateGlobalSectionCensored(PetscSection s, PetscSF sf, PetscBool includeConstraints, PetscInt numExcludes, const PetscInt excludes[], PetscSection *gsection)
1613d71ae5a4SJacob Faibussowitsch {
1614ea844a1aSMatthew Knepley   const PetscInt *pind = NULL;
1615ea844a1aSMatthew Knepley   PetscInt       *neg = NULL, *tmpOff = NULL;
1616ea844a1aSMatthew Knepley   PetscInt        pStart, pEnd, p, e, dof, cdof, off, globalOff = 0, nroots;
1617ea844a1aSMatthew Knepley 
1618ea844a1aSMatthew Knepley   PetscFunctionBegin;
1619ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1620ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
16214f572ea9SToby Isaac   PetscAssertPointer(gsection, 6);
16229566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s), gsection));
16239566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
16249566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(*gsection, pStart, pEnd));
16259566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, &nroots, NULL, NULL, NULL));
1626ea844a1aSMatthew Knepley   if (nroots >= 0) {
162708401ef6SPierre Jolivet     PetscCheck(nroots >= pEnd - pStart, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "PetscSF nroots %" PetscInt_FMT " < %" PetscInt_FMT " section size", nroots, pEnd - pStart);
16289566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(nroots, &neg));
1629ea844a1aSMatthew Knepley     if (nroots > pEnd - pStart) {
16309566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(nroots, &tmpOff));
1631ea844a1aSMatthew Knepley     } else {
1632ea844a1aSMatthew Knepley       tmpOff = &(*gsection)->atlasDof[-pStart];
1633ea844a1aSMatthew Knepley     }
1634ea844a1aSMatthew Knepley   }
1635ea844a1aSMatthew Knepley   /* Mark ghost points with negative dof */
1636ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
1637ea844a1aSMatthew Knepley     for (e = 0; e < numExcludes; ++e) {
1638ea844a1aSMatthew Knepley       if ((p >= excludes[e * 2 + 0]) && (p < excludes[e * 2 + 1])) {
16399566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetDof(*gsection, p, 0));
1640ea844a1aSMatthew Knepley         break;
1641ea844a1aSMatthew Knepley       }
1642ea844a1aSMatthew Knepley     }
1643ea844a1aSMatthew Knepley     if (e < numExcludes) continue;
16449566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s, p, &dof));
16459566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(*gsection, p, dof));
16469566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
16479566063dSJacob Faibussowitsch     if (!includeConstraints && cdof > 0) PetscCall(PetscSectionSetConstraintDof(*gsection, p, cdof));
1648ea844a1aSMatthew Knepley     if (neg) neg[p] = -(dof + 1);
1649ea844a1aSMatthew Knepley   }
16509566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUpBC(*gsection));
1651ea844a1aSMatthew Knepley   if (nroots >= 0) {
16529566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, neg, tmpOff, MPI_REPLACE));
16539566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, neg, tmpOff, MPI_REPLACE));
1654ea844a1aSMatthew Knepley     if (nroots > pEnd - pStart) {
16559371c9d4SSatish Balay       for (p = pStart; p < pEnd; ++p) {
16569371c9d4SSatish Balay         if (tmpOff[p] < 0) (*gsection)->atlasDof[p - pStart] = tmpOff[p];
16579371c9d4SSatish Balay       }
1658ea844a1aSMatthew Knepley     }
1659ea844a1aSMatthew Knepley   }
166035cb6cd3SPierre Jolivet   /* Calculate new sizes, get process offset, and calculate point offsets */
16619566063dSJacob Faibussowitsch   if (s->perm) PetscCall(ISGetIndices(s->perm, &pind));
1662ea844a1aSMatthew Knepley   for (p = 0, off = 0; p < pEnd - pStart; ++p) {
1663ea844a1aSMatthew Knepley     const PetscInt q = pind ? pind[p] : p;
1664ea844a1aSMatthew Knepley 
1665ea844a1aSMatthew Knepley     cdof                     = (!includeConstraints && s->bc) ? s->bc->atlasDof[q] : 0;
1666ea844a1aSMatthew Knepley     (*gsection)->atlasOff[q] = off;
1667ea844a1aSMatthew Knepley     off += (*gsection)->atlasDof[q] > 0 ? (*gsection)->atlasDof[q] - cdof : 0;
1668ea844a1aSMatthew Knepley   }
16699566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Scan(&off, &globalOff, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)s)));
1670ea844a1aSMatthew Knepley   globalOff -= off;
1671ea844a1aSMatthew Knepley   for (p = 0, off = 0; p < pEnd - pStart; ++p) {
1672ea844a1aSMatthew Knepley     (*gsection)->atlasOff[p] += globalOff;
1673ea844a1aSMatthew Knepley     if (neg) neg[p + pStart] = -((*gsection)->atlasOff[p] + 1);
1674ea844a1aSMatthew Knepley   }
16759566063dSJacob Faibussowitsch   if (s->perm) PetscCall(ISRestoreIndices(s->perm, &pind));
1676ea844a1aSMatthew Knepley   /* Put in negative offsets for ghost points */
1677ea844a1aSMatthew Knepley   if (nroots >= 0) {
1678ea844a1aSMatthew Knepley     if (nroots == pEnd - pStart) tmpOff = &(*gsection)->atlasOff[-pStart];
16799566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, neg, tmpOff, MPI_REPLACE));
16809566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, neg, tmpOff, MPI_REPLACE));
1681ea844a1aSMatthew Knepley     if (nroots > pEnd - pStart) {
16829371c9d4SSatish Balay       for (p = pStart; p < pEnd; ++p) {
16839371c9d4SSatish Balay         if (tmpOff[p] < 0) (*gsection)->atlasOff[p - pStart] = tmpOff[p];
16849371c9d4SSatish Balay       }
1685ea844a1aSMatthew Knepley     }
1686ea844a1aSMatthew Knepley   }
16879566063dSJacob Faibussowitsch   if (nroots >= 0 && nroots > pEnd - pStart) PetscCall(PetscFree(tmpOff));
16889566063dSJacob Faibussowitsch   PetscCall(PetscFree(neg));
16893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1690ea844a1aSMatthew Knepley }
1691ea844a1aSMatthew Knepley 
1692ea844a1aSMatthew Knepley /*@
1693583308b6SBarry Smith   PetscSectionGetPointLayout - Get a `PetscLayout` for the points with nonzero dof counts of the unnamed default field within this `PetscSection`s local chart
1694ea844a1aSMatthew Knepley 
1695cab54364SBarry Smith   Collective
1696ea844a1aSMatthew Knepley 
1697ea844a1aSMatthew Knepley   Input Parameters:
1698cab54364SBarry Smith + comm - The `MPI_Comm`
1699cab54364SBarry Smith - s    - The `PetscSection`
1700ea844a1aSMatthew Knepley 
1701ea844a1aSMatthew Knepley   Output Parameter:
170220662ed9SBarry Smith . layout - The point layout for the data that defines the section
1703ea844a1aSMatthew Knepley 
1704ea844a1aSMatthew Knepley   Level: advanced
1705ea844a1aSMatthew Knepley 
170620662ed9SBarry Smith   Notes:
1707583308b6SBarry Smith   `PetscSectionGetValueLayout()` provides similar information but counting the total number of degrees of freedom on the MPI process (excluding constrained
1708583308b6SBarry Smith   degrees of freedom).
170920662ed9SBarry Smith 
1710583308b6SBarry Smith   This count includes constrained degrees of freedom
1711583308b6SBarry Smith 
1712583308b6SBarry Smith   This is usually called on the default global section.
1713583308b6SBarry Smith 
1714583308b6SBarry Smith   Example:
1715583308b6SBarry Smith .vb
1716583308b6SBarry Smith      The chart is [2,5), point 2 has 2 dof, point 3 has 0 dof, point 4 has 1 dof
1717583308b6SBarry Smith      The local size of the `PetscLayout` is 2 since 2 points have a non-zero number of dof
1718583308b6SBarry Smith .ve
1719583308b6SBarry Smith 
172038b5cf2dSJacob Faibussowitsch   Developer Notes:
1721583308b6SBarry Smith   I find the names of these two functions extremely non-informative
1722cab54364SBarry Smith 
1723cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetValueLayout()`, `PetscSectionCreate()`
1724ea844a1aSMatthew Knepley @*/
1725d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetPointLayout(MPI_Comm comm, PetscSection s, PetscLayout *layout)
1726d71ae5a4SJacob Faibussowitsch {
1727ea844a1aSMatthew Knepley   PetscInt pStart, pEnd, p, localSize = 0;
1728ea844a1aSMatthew Knepley 
1729ea844a1aSMatthew Knepley   PetscFunctionBegin;
17309566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
1731ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
1732ea844a1aSMatthew Knepley     PetscInt dof;
1733ea844a1aSMatthew Knepley 
17349566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s, p, &dof));
17359120ba30SVaclav Hapla     if (dof >= 0) ++localSize;
1736ea844a1aSMatthew Knepley   }
17379566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, layout));
17389566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(*layout, localSize));
17399566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(*layout, 1));
17409566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(*layout));
17413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1742ea844a1aSMatthew Knepley }
1743ea844a1aSMatthew Knepley 
1744ea844a1aSMatthew Knepley /*@
174520662ed9SBarry Smith   PetscSectionGetValueLayout - Get the `PetscLayout` associated with the section dofs of a `PetscSection`
1746ea844a1aSMatthew Knepley 
1747cab54364SBarry Smith   Collective
1748ea844a1aSMatthew Knepley 
1749ea844a1aSMatthew Knepley   Input Parameters:
1750cab54364SBarry Smith + comm - The `MPI_Comm`
1751cab54364SBarry Smith - s    - The `PetscSection`
1752ea844a1aSMatthew Knepley 
1753ea844a1aSMatthew Knepley   Output Parameter:
1754ea844a1aSMatthew Knepley . layout - The dof layout for the section
1755ea844a1aSMatthew Knepley 
1756ea844a1aSMatthew Knepley   Level: advanced
1757ea844a1aSMatthew Knepley 
175820662ed9SBarry Smith   Notes:
1759583308b6SBarry Smith   `PetscSectionGetPointLayout()` provides similar information but only counting the number of points with nonzero degrees of freedom and
1760583308b6SBarry Smith   including the constrained degrees of freedom
176120662ed9SBarry Smith 
1762cab54364SBarry Smith   This is usually called for the default global section.
1763cab54364SBarry Smith 
1764583308b6SBarry Smith   Example:
1765583308b6SBarry Smith .vb
1766583308b6SBarry 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)
1767583308b6SBarry Smith      The local size of the `PetscLayout` is 3 since there are 3 unconstrained degrees of freedom on this MPI process
1768583308b6SBarry Smith .ve
1769583308b6SBarry Smith 
1770cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetPointLayout()`, `PetscSectionCreate()`
1771ea844a1aSMatthew Knepley @*/
1772d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetValueLayout(MPI_Comm comm, PetscSection s, PetscLayout *layout)
1773d71ae5a4SJacob Faibussowitsch {
1774ea844a1aSMatthew Knepley   PetscInt pStart, pEnd, p, localSize = 0;
1775ea844a1aSMatthew Knepley 
1776ea844a1aSMatthew Knepley   PetscFunctionBegin;
1777ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 2);
17784f572ea9SToby Isaac   PetscAssertPointer(layout, 3);
17799566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
1780ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
1781ea844a1aSMatthew Knepley     PetscInt dof, cdof;
1782ea844a1aSMatthew Knepley 
17839566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s, p, &dof));
17849566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
1785ea844a1aSMatthew Knepley     if (dof - cdof > 0) localSize += dof - cdof;
1786ea844a1aSMatthew Knepley   }
17879566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, layout));
17889566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(*layout, localSize));
17899566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(*layout, 1));
17909566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(*layout));
17913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1792ea844a1aSMatthew Knepley }
1793ea844a1aSMatthew Knepley 
1794ea844a1aSMatthew Knepley /*@
1795db527f05SMatthew G. Knepley   PetscSectionGetOffset - Return the offset into an array or `Vec` for the dof associated with the given point.
1796ea844a1aSMatthew Knepley 
179740750d41SVaclav Hapla   Not Collective
1798ea844a1aSMatthew Knepley 
1799ea844a1aSMatthew Knepley   Input Parameters:
1800cab54364SBarry Smith + s     - the `PetscSection`
1801ea844a1aSMatthew Knepley - point - the point
1802ea844a1aSMatthew Knepley 
1803ea844a1aSMatthew Knepley   Output Parameter:
1804ea844a1aSMatthew Knepley . offset - the offset
1805ea844a1aSMatthew Knepley 
1806ea844a1aSMatthew Knepley   Level: intermediate
1807ea844a1aSMatthew Knepley 
1808583308b6SBarry Smith   Notes:
1809376335faSBarry Smith   In a global section, `offset` will be negative for points not owned by this process.
1810583308b6SBarry Smith 
1811583308b6SBarry Smith   This is for the unnamed default field in the `PetscSection` not the named fields
1812583308b6SBarry Smith 
1813583308b6SBarry Smith   The `offset` values are different depending on a value set with `PetscSectionSetPointMajor()`
1814583308b6SBarry Smith 
1815583308b6SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetFieldOffset()`, `PetscSectionCreate()`, `PetscSectionSetPointMajor()`
1816ea844a1aSMatthew Knepley @*/
1817d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetOffset(PetscSection s, PetscInt point, PetscInt *offset)
1818d71ae5a4SJacob Faibussowitsch {
1819ea844a1aSMatthew Knepley   PetscFunctionBegin;
1820ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
18214f572ea9SToby Isaac   PetscAssertPointer(offset, 3);
1822b498ca8aSPierre Jolivet   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);
1823ea844a1aSMatthew Knepley   *offset = s->atlasOff[point - s->pStart];
18243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1825ea844a1aSMatthew Knepley }
1826ea844a1aSMatthew Knepley 
1827ea844a1aSMatthew Knepley /*@
1828db527f05SMatthew G. Knepley   PetscSectionSetOffset - Set the offset into an array or `Vec` for the dof associated with the given point.
1829ea844a1aSMatthew Knepley 
183040750d41SVaclav Hapla   Not Collective
1831ea844a1aSMatthew Knepley 
1832ea844a1aSMatthew Knepley   Input Parameters:
1833cab54364SBarry Smith + s      - the `PetscSection`
1834ea844a1aSMatthew Knepley . point  - the point
1835376335faSBarry Smith - offset - the offset, these values may be negative indicating the values are off process
1836ea844a1aSMatthew Knepley 
1837583308b6SBarry Smith   Level: developer
1838ea844a1aSMatthew Knepley 
1839cab54364SBarry Smith   Note:
1840cab54364SBarry Smith   The user usually does not call this function, but uses `PetscSectionSetUp()`
1841cab54364SBarry Smith 
1842cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetFieldOffset()`, `PetscSectionCreate()`, `PetscSectionSetUp()`
1843ea844a1aSMatthew Knepley @*/
1844d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetOffset(PetscSection s, PetscInt point, PetscInt offset)
1845d71ae5a4SJacob Faibussowitsch {
1846ea844a1aSMatthew Knepley   PetscFunctionBegin;
1847ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
184808401ef6SPierre 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);
1849ea844a1aSMatthew Knepley   s->atlasOff[point - s->pStart] = offset;
18503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1851ea844a1aSMatthew Knepley }
1852ea844a1aSMatthew Knepley 
1853ea844a1aSMatthew Knepley /*@
1854db527f05SMatthew G. Knepley   PetscSectionGetFieldOffset - Return the offset into an array or `Vec` for the field dof associated with the given point.
1855ea844a1aSMatthew Knepley 
185640750d41SVaclav Hapla   Not Collective
1857ea844a1aSMatthew Knepley 
1858ea844a1aSMatthew Knepley   Input Parameters:
1859cab54364SBarry Smith + s     - the `PetscSection`
1860ea844a1aSMatthew Knepley . point - the point
1861ea844a1aSMatthew Knepley - field - the field
1862ea844a1aSMatthew Knepley 
1863ea844a1aSMatthew Knepley   Output Parameter:
1864ea844a1aSMatthew Knepley . offset - the offset
1865ea844a1aSMatthew Knepley 
1866ea844a1aSMatthew Knepley   Level: intermediate
1867ea844a1aSMatthew Knepley 
1868583308b6SBarry Smith   Notes:
1869376335faSBarry Smith   In a global section, `offset` will be negative for points not owned by this process.
1870583308b6SBarry Smith 
1871583308b6SBarry Smith   The `offset` values are different depending on a value set with `PetscSectionSetPointMajor()`
1872583308b6SBarry Smith 
1873583308b6SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetOffset()`, `PetscSectionCreate()`, `PetscSectionGetFieldPointOffset()`
1874ea844a1aSMatthew Knepley @*/
1875d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldOffset(PetscSection s, PetscInt point, PetscInt field, PetscInt *offset)
1876d71ae5a4SJacob Faibussowitsch {
1877ea844a1aSMatthew Knepley   PetscFunctionBegin;
1878ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
18794f572ea9SToby Isaac   PetscAssertPointer(offset, 4);
18802abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
18819566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(s->field[field], point, offset));
18823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1883ea844a1aSMatthew Knepley }
1884ea844a1aSMatthew Knepley 
1885ea844a1aSMatthew Knepley /*@
1886db527f05SMatthew G. Knepley   PetscSectionSetFieldOffset - Set the offset into an array or `Vec` for the dof associated with the given field at a point.
1887ea844a1aSMatthew Knepley 
188840750d41SVaclav Hapla   Not Collective
1889ea844a1aSMatthew Knepley 
1890ea844a1aSMatthew Knepley   Input Parameters:
189120662ed9SBarry Smith + s      - the `PetscSection`
1892ea844a1aSMatthew Knepley . point  - the point
1893ea844a1aSMatthew Knepley . field  - the field
1894376335faSBarry Smith - offset - the offset, these values may be negative indicating the values are off process
1895ea844a1aSMatthew Knepley 
1896cab54364SBarry Smith   Level: developer
1897ea844a1aSMatthew Knepley 
1898cab54364SBarry Smith   Note:
1899cab54364SBarry Smith   The user usually does not call this function, but uses `PetscSectionSetUp()`
1900ea844a1aSMatthew Knepley 
1901cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetFieldOffset()`, `PetscSectionSetOffset()`, `PetscSectionCreate()`, `PetscSectionSetUp()`
1902ea844a1aSMatthew Knepley @*/
1903d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFieldOffset(PetscSection s, PetscInt point, PetscInt field, PetscInt offset)
1904d71ae5a4SJacob Faibussowitsch {
1905ea844a1aSMatthew Knepley   PetscFunctionBegin;
1906ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
19072abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
19089566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetOffset(s->field[field], point, offset));
19093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1910ea844a1aSMatthew Knepley }
1911ea844a1aSMatthew Knepley 
1912ea844a1aSMatthew Knepley /*@
1913583308b6SBarry Smith   PetscSectionGetFieldPointOffset - Return the offset for the first field dof associated with the given point relative to the offset for that point for the
1914583308b6SBarry Smith   unnamed default field's first dof
1915ea844a1aSMatthew Knepley 
191640750d41SVaclav Hapla   Not Collective
1917ea844a1aSMatthew Knepley 
1918ea844a1aSMatthew Knepley   Input Parameters:
1919cab54364SBarry Smith + s     - the `PetscSection`
1920ea844a1aSMatthew Knepley . point - the point
1921ea844a1aSMatthew Knepley - field - the field
1922ea844a1aSMatthew Knepley 
1923ea844a1aSMatthew Knepley   Output Parameter:
1924ea844a1aSMatthew Knepley . offset - the offset
1925ea844a1aSMatthew Knepley 
1926ea844a1aSMatthew Knepley   Level: advanced
1927ea844a1aSMatthew Knepley 
1928cab54364SBarry Smith   Note:
1929583308b6SBarry Smith   This ignores constraints
1930cab54364SBarry Smith 
1931583308b6SBarry Smith   Example:
1932583308b6SBarry Smith .vb
1933583308b6SBarry Smith   if PetscSectionSetPointMajor(s,PETSC_TRUE)
1934583308b6SBarry Smith   The unnamed default field has 3 dof at `point`
1935583308b6SBarry Smith   Field 0 has 2 dof at `point`
1936583308b6SBarry Smith   Then PetscSectionGetFieldPointOffset(s,point,1,&offset) returns and offset of 5
1937583308b6SBarry Smith .ve
1938583308b6SBarry Smith 
1939583308b6SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetOffset()`, `PetscSectionCreate()`, `PetscSectionGetFieldOffset()`
1940ea844a1aSMatthew Knepley @*/
1941d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldPointOffset(PetscSection s, PetscInt point, PetscInt field, PetscInt *offset)
1942d71ae5a4SJacob Faibussowitsch {
1943ea844a1aSMatthew Knepley   PetscInt off, foff;
1944ea844a1aSMatthew Knepley 
1945ea844a1aSMatthew Knepley   PetscFunctionBegin;
1946ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
19474f572ea9SToby Isaac   PetscAssertPointer(offset, 4);
19482abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
19499566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(s, point, &off));
19509566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(s->field[field], point, &foff));
1951ea844a1aSMatthew Knepley   *offset = foff - off;
19523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1953ea844a1aSMatthew Knepley }
1954ea844a1aSMatthew Knepley 
1955ea844a1aSMatthew Knepley /*@
195620662ed9SBarry Smith   PetscSectionGetOffsetRange - Return the full range of offsets [`start`, `end`) for a `PetscSection`
1957ea844a1aSMatthew Knepley 
195840750d41SVaclav Hapla   Not Collective
1959ea844a1aSMatthew Knepley 
1960ea844a1aSMatthew Knepley   Input Parameter:
1961cab54364SBarry Smith . s - the `PetscSection`
1962ea844a1aSMatthew Knepley 
1963ea844a1aSMatthew Knepley   Output Parameters:
1964ea844a1aSMatthew Knepley + start - the minimum offset
1965ea844a1aSMatthew Knepley - end   - one more than the maximum offset
1966ea844a1aSMatthew Knepley 
1967ea844a1aSMatthew Knepley   Level: intermediate
1968ea844a1aSMatthew Knepley 
1969cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetOffset()`, `PetscSectionCreate()`
1970ea844a1aSMatthew Knepley @*/
1971d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetOffsetRange(PetscSection s, PetscInt *start, PetscInt *end)
1972d71ae5a4SJacob Faibussowitsch {
1973ea844a1aSMatthew Knepley   PetscInt os = 0, oe = 0, pStart, pEnd, p;
1974ea844a1aSMatthew Knepley 
1975ea844a1aSMatthew Knepley   PetscFunctionBegin;
1976ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
19779371c9d4SSatish Balay   if (s->atlasOff) {
19789371c9d4SSatish Balay     os = s->atlasOff[0];
19799371c9d4SSatish Balay     oe = s->atlasOff[0];
19809371c9d4SSatish Balay   }
19819566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
1982ea844a1aSMatthew Knepley   for (p = 0; p < pEnd - pStart; ++p) {
1983ea844a1aSMatthew Knepley     PetscInt dof = s->atlasDof[p], off = s->atlasOff[p];
1984ea844a1aSMatthew Knepley 
1985ea844a1aSMatthew Knepley     if (off >= 0) {
1986ea844a1aSMatthew Knepley       os = PetscMin(os, off);
1987ea844a1aSMatthew Knepley       oe = PetscMax(oe, off + dof);
1988ea844a1aSMatthew Knepley     }
1989ea844a1aSMatthew Knepley   }
1990ea844a1aSMatthew Knepley   if (start) *start = os;
1991ea844a1aSMatthew Knepley   if (end) *end = oe;
19923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1993ea844a1aSMatthew Knepley }
1994ea844a1aSMatthew Knepley 
1995ea844a1aSMatthew Knepley /*@
1996583308b6SBarry Smith   PetscSectionCreateSubsection - Create a new, smaller `PetscSection` composed of only selected fields
1997ea844a1aSMatthew Knepley 
199840750d41SVaclav Hapla   Collective
1999ea844a1aSMatthew Knepley 
2000d8d19677SJose E. Roman   Input Parameters:
2001cab54364SBarry Smith + s      - the `PetscSection`
2002ea844a1aSMatthew Knepley . len    - the number of subfields
2003ea844a1aSMatthew Knepley - fields - the subfield numbers
2004ea844a1aSMatthew Knepley 
2005ea844a1aSMatthew Knepley   Output Parameter:
2006ea844a1aSMatthew Knepley . subs - the subsection
2007ea844a1aSMatthew Knepley 
2008ea844a1aSMatthew Knepley   Level: advanced
2009ea844a1aSMatthew Knepley 
2010cab54364SBarry Smith   Notes:
2011583308b6SBarry Smith   The chart of `subs` is the same as the chart of `s`
2012cab54364SBarry Smith 
2013cab54364SBarry Smith   This will error if a fieldnumber is out of range
2014cab54364SBarry Smith 
2015cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreateSupersection()`, `PetscSectionCreate()`
2016ea844a1aSMatthew Knepley @*/
2017d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCreateSubsection(PetscSection s, PetscInt len, const PetscInt fields[], PetscSection *subs)
2018d71ae5a4SJacob Faibussowitsch {
2019b778fa18SValeria Barra   PetscInt nF, f, c, pStart, pEnd, p, maxCdof = 0;
2020ea844a1aSMatthew Knepley 
2021ea844a1aSMatthew Knepley   PetscFunctionBegin;
20223ba16761SJacob Faibussowitsch   if (!len) PetscFunctionReturn(PETSC_SUCCESS);
2023ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
20244f572ea9SToby Isaac   PetscAssertPointer(fields, 3);
20254f572ea9SToby Isaac   PetscAssertPointer(subs, 4);
20269566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &nF));
202708401ef6SPierre 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);
20289566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s), subs));
20299566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(*subs, len));
2030ea844a1aSMatthew Knepley   for (f = 0; f < len; ++f) {
2031ea844a1aSMatthew Knepley     const char     *name    = NULL;
2032ea844a1aSMatthew Knepley     PetscInt        numComp = 0;
203342a52479SJed Brown     PetscSectionSym sym;
2034ea844a1aSMatthew Knepley 
20359566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldName(s, fields[f], &name));
20369566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldName(*subs, f, name));
20379566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, fields[f], &numComp));
20389566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(*subs, f, numComp));
2039b778fa18SValeria Barra     for (c = 0; c < s->numFieldComponents[fields[f]]; ++c) {
20409566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetComponentName(s, fields[f], c, &name));
20419566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetComponentName(*subs, f, c, name));
2042b778fa18SValeria Barra     }
204342a52479SJed Brown     PetscCall(PetscSectionGetFieldSym(s, fields[f], &sym));
204442a52479SJed Brown     PetscCall(PetscSectionSetFieldSym(*subs, f, sym));
2045ea844a1aSMatthew Knepley   }
20469566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
20479566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(*subs, pStart, pEnd));
2048ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
2049ea844a1aSMatthew Knepley     PetscInt dof = 0, cdof = 0, fdof = 0, cfdof = 0;
2050ea844a1aSMatthew Knepley 
2051ea844a1aSMatthew Knepley     for (f = 0; f < len; ++f) {
20529566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, fields[f], &fdof));
20539566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(*subs, p, f, fdof));
20549566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(s, p, fields[f], &cfdof));
20559566063dSJacob Faibussowitsch       if (cfdof) PetscCall(PetscSectionSetFieldConstraintDof(*subs, p, f, cfdof));
2056ea844a1aSMatthew Knepley       dof += fdof;
2057ea844a1aSMatthew Knepley       cdof += cfdof;
2058ea844a1aSMatthew Knepley     }
20599566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(*subs, p, dof));
20609566063dSJacob Faibussowitsch     if (cdof) PetscCall(PetscSectionSetConstraintDof(*subs, p, cdof));
2061ea844a1aSMatthew Knepley     maxCdof = PetscMax(cdof, maxCdof);
2062ea844a1aSMatthew Knepley   }
20639566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(*subs));
2064ea844a1aSMatthew Knepley   if (maxCdof) {
2065ea844a1aSMatthew Knepley     PetscInt *indices;
2066ea844a1aSMatthew Knepley 
20679566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(maxCdof, &indices));
2068ea844a1aSMatthew Knepley     for (p = pStart; p < pEnd; ++p) {
2069ea844a1aSMatthew Knepley       PetscInt cdof;
2070ea844a1aSMatthew Knepley 
20719566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetConstraintDof(*subs, p, &cdof));
2072ea844a1aSMatthew Knepley       if (cdof) {
2073ea844a1aSMatthew Knepley         const PetscInt *oldIndices = NULL;
2074ea844a1aSMatthew Knepley         PetscInt        fdof = 0, cfdof = 0, fc, numConst = 0, fOff = 0;
2075ea844a1aSMatthew Knepley 
2076ea844a1aSMatthew Knepley         for (f = 0; f < len; ++f) {
20779566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(s, p, fields[f], &fdof));
20789566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(s, p, fields[f], &cfdof));
20799566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintIndices(s, p, fields[f], &oldIndices));
20809566063dSJacob Faibussowitsch           PetscCall(PetscSectionSetFieldConstraintIndices(*subs, p, f, oldIndices));
20819574d0f0SMatthew G. Knepley           for (fc = 0; fc < cfdof; ++fc) indices[numConst + fc] = oldIndices[fc] + fOff;
2082ea844a1aSMatthew Knepley           numConst += cfdof;
2083ea844a1aSMatthew Knepley           fOff += fdof;
2084ea844a1aSMatthew Knepley         }
20859566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetConstraintIndices(*subs, p, indices));
2086ea844a1aSMatthew Knepley       }
2087ea844a1aSMatthew Knepley     }
20889566063dSJacob Faibussowitsch     PetscCall(PetscFree(indices));
2089ea844a1aSMatthew Knepley   }
20903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2091ea844a1aSMatthew Knepley }
2092ea844a1aSMatthew Knepley 
2093ea844a1aSMatthew Knepley /*@
2094583308b6SBarry Smith   PetscSectionCreateSupersection - Create a new, larger section composed of multiple `PetscSection`s
2095ea844a1aSMatthew Knepley 
209640750d41SVaclav Hapla   Collective
2097ea844a1aSMatthew Knepley 
2098ea844a1aSMatthew Knepley   Input Parameters:
2099ea844a1aSMatthew Knepley + s   - the input sections
2100ea844a1aSMatthew Knepley - len - the number of input sections
2101ea844a1aSMatthew Knepley 
2102ea844a1aSMatthew Knepley   Output Parameter:
2103ea844a1aSMatthew Knepley . supers - the supersection
2104ea844a1aSMatthew Knepley 
2105ea844a1aSMatthew Knepley   Level: advanced
2106ea844a1aSMatthew Knepley 
2107583308b6SBarry Smith   Notes:
2108cab54364SBarry Smith   The section offsets now refer to a new, larger vector.
2109cab54364SBarry Smith 
211038b5cf2dSJacob Faibussowitsch   Developer Notes:
2111cab54364SBarry Smith   Needs to explain how the sections are composed
2112cab54364SBarry Smith 
2113cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreateSubsection()`, `PetscSectionCreate()`
2114ea844a1aSMatthew Knepley @*/
2115d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCreateSupersection(PetscSection s[], PetscInt len, PetscSection *supers)
2116d71ae5a4SJacob Faibussowitsch {
2117ea844a1aSMatthew Knepley   PetscInt Nf = 0, f, pStart = PETSC_MAX_INT, pEnd = 0, p, maxCdof = 0, i;
2118ea844a1aSMatthew Knepley 
2119ea844a1aSMatthew Knepley   PetscFunctionBegin;
21203ba16761SJacob Faibussowitsch   if (!len) PetscFunctionReturn(PETSC_SUCCESS);
2121ea844a1aSMatthew Knepley   for (i = 0; i < len; ++i) {
2122ea844a1aSMatthew Knepley     PetscInt nf, pStarti, pEndi;
2123ea844a1aSMatthew Knepley 
21249566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(s[i], &nf));
21259566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(s[i], &pStarti, &pEndi));
2126ea844a1aSMatthew Knepley     pStart = PetscMin(pStart, pStarti);
2127ea844a1aSMatthew Knepley     pEnd   = PetscMax(pEnd, pEndi);
2128ea844a1aSMatthew Knepley     Nf += nf;
2129ea844a1aSMatthew Knepley   }
21309566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s[0]), supers));
21319566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(*supers, Nf));
2132ea844a1aSMatthew Knepley   for (i = 0, f = 0; i < len; ++i) {
2133b778fa18SValeria Barra     PetscInt nf, fi, ci;
2134ea844a1aSMatthew Knepley 
21359566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(s[i], &nf));
2136ea844a1aSMatthew Knepley     for (fi = 0; fi < nf; ++fi, ++f) {
2137ea844a1aSMatthew Knepley       const char *name    = NULL;
2138ea844a1aSMatthew Knepley       PetscInt    numComp = 0;
2139ea844a1aSMatthew Knepley 
21409566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(s[i], fi, &name));
21419566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldName(*supers, f, name));
21429566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(s[i], fi, &numComp));
21439566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldComponents(*supers, f, numComp));
2144b778fa18SValeria Barra       for (ci = 0; ci < s[i]->numFieldComponents[fi]; ++ci) {
21459566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetComponentName(s[i], fi, ci, &name));
21469566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetComponentName(*supers, f, ci, name));
2147b778fa18SValeria Barra       }
2148ea844a1aSMatthew Knepley     }
2149ea844a1aSMatthew Knepley   }
21509566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(*supers, pStart, pEnd));
2151ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
2152ea844a1aSMatthew Knepley     PetscInt dof = 0, cdof = 0;
2153ea844a1aSMatthew Knepley 
2154ea844a1aSMatthew Knepley     for (i = 0, f = 0; i < len; ++i) {
2155ea844a1aSMatthew Knepley       PetscInt nf, fi, pStarti, pEndi;
2156ea844a1aSMatthew Knepley       PetscInt fdof = 0, cfdof = 0;
2157ea844a1aSMatthew Knepley 
21589566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetNumFields(s[i], &nf));
21599566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s[i], &pStarti, &pEndi));
2160ea844a1aSMatthew Knepley       if ((p < pStarti) || (p >= pEndi)) continue;
2161ea844a1aSMatthew Knepley       for (fi = 0; fi < nf; ++fi, ++f) {
21629566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldDof(s[i], p, fi, &fdof));
21639566063dSJacob Faibussowitsch         PetscCall(PetscSectionAddFieldDof(*supers, p, f, fdof));
21649566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldConstraintDof(s[i], p, fi, &cfdof));
21659566063dSJacob Faibussowitsch         if (cfdof) PetscCall(PetscSectionAddFieldConstraintDof(*supers, p, f, cfdof));
2166ea844a1aSMatthew Knepley         dof += fdof;
2167ea844a1aSMatthew Knepley         cdof += cfdof;
2168ea844a1aSMatthew Knepley       }
2169ea844a1aSMatthew Knepley     }
21709566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(*supers, p, dof));
21719566063dSJacob Faibussowitsch     if (cdof) PetscCall(PetscSectionSetConstraintDof(*supers, p, cdof));
2172ea844a1aSMatthew Knepley     maxCdof = PetscMax(cdof, maxCdof);
2173ea844a1aSMatthew Knepley   }
21749566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(*supers));
2175ea844a1aSMatthew Knepley   if (maxCdof) {
2176ea844a1aSMatthew Knepley     PetscInt *indices;
2177ea844a1aSMatthew Knepley 
21789566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(maxCdof, &indices));
2179ea844a1aSMatthew Knepley     for (p = pStart; p < pEnd; ++p) {
2180ea844a1aSMatthew Knepley       PetscInt cdof;
2181ea844a1aSMatthew Knepley 
21829566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetConstraintDof(*supers, p, &cdof));
2183ea844a1aSMatthew Knepley       if (cdof) {
2184ea844a1aSMatthew Knepley         PetscInt dof, numConst = 0, fOff = 0;
2185ea844a1aSMatthew Knepley 
2186ea844a1aSMatthew Knepley         for (i = 0, f = 0; i < len; ++i) {
2187ea844a1aSMatthew Knepley           const PetscInt *oldIndices = NULL;
2188ea844a1aSMatthew Knepley           PetscInt        nf, fi, pStarti, pEndi, fdof, cfdof, fc;
2189ea844a1aSMatthew Knepley 
21909566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetNumFields(s[i], &nf));
21919566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetChart(s[i], &pStarti, &pEndi));
2192ea844a1aSMatthew Knepley           if ((p < pStarti) || (p >= pEndi)) continue;
2193ea844a1aSMatthew Knepley           for (fi = 0; fi < nf; ++fi, ++f) {
21949566063dSJacob Faibussowitsch             PetscCall(PetscSectionGetFieldDof(s[i], p, fi, &fdof));
21959566063dSJacob Faibussowitsch             PetscCall(PetscSectionGetFieldConstraintDof(s[i], p, fi, &cfdof));
21969566063dSJacob Faibussowitsch             PetscCall(PetscSectionGetFieldConstraintIndices(s[i], p, fi, &oldIndices));
2197fcd2503dSMatthew G. Knepley             for (fc = 0; fc < cfdof; ++fc) indices[numConst + fc] = oldIndices[fc];
21989566063dSJacob Faibussowitsch             PetscCall(PetscSectionSetFieldConstraintIndices(*supers, p, f, &indices[numConst]));
2199fcd2503dSMatthew G. Knepley             for (fc = 0; fc < cfdof; ++fc) indices[numConst + fc] += fOff;
2200ea844a1aSMatthew Knepley             numConst += cfdof;
2201ea844a1aSMatthew Knepley           }
22029566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetDof(s[i], p, &dof));
2203ea844a1aSMatthew Knepley           fOff += dof;
2204ea844a1aSMatthew Knepley         }
22059566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetConstraintIndices(*supers, p, indices));
2206ea844a1aSMatthew Knepley       }
2207ea844a1aSMatthew Knepley     }
22089566063dSJacob Faibussowitsch     PetscCall(PetscFree(indices));
2209ea844a1aSMatthew Knepley   }
22103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2211ea844a1aSMatthew Knepley }
2212ea844a1aSMatthew Knepley 
2213da8c939bSJacob Faibussowitsch static PetscErrorCode PetscSectionCreateSubplexSection_Private(PetscSection s, IS subpointMap, PetscBool renumberPoints, PetscSection *subs)
2214d71ae5a4SJacob Faibussowitsch {
2215ea844a1aSMatthew Knepley   const PetscInt *points = NULL, *indices = NULL;
221641f23ed0SMatthew G. Knepley   PetscInt        numFields, f, c, numSubpoints = 0, pStart, pEnd, p, spStart, spEnd, subp;
2217ea844a1aSMatthew Knepley 
2218ea844a1aSMatthew Knepley   PetscFunctionBegin;
2219ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2220ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(subpointMap, IS_CLASSID, 2);
22214f572ea9SToby Isaac   PetscAssertPointer(subs, 4);
22229566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &numFields));
22239566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s), subs));
22249566063dSJacob Faibussowitsch   if (numFields) PetscCall(PetscSectionSetNumFields(*subs, numFields));
2225ea844a1aSMatthew Knepley   for (f = 0; f < numFields; ++f) {
2226ea844a1aSMatthew Knepley     const char *name    = NULL;
2227ea844a1aSMatthew Knepley     PetscInt    numComp = 0;
2228ea844a1aSMatthew Knepley 
22299566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldName(s, f, &name));
22309566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldName(*subs, f, name));
22319566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &numComp));
22329566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(*subs, f, numComp));
2233b778fa18SValeria Barra     for (c = 0; c < s->numFieldComponents[f]; ++c) {
22349566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetComponentName(s, f, c, &name));
22359566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetComponentName(*subs, f, c, name));
2236b778fa18SValeria Barra     }
2237ea844a1aSMatthew Knepley   }
2238ea844a1aSMatthew Knepley   /* For right now, we do not try to squeeze the subchart */
2239ea844a1aSMatthew Knepley   if (subpointMap) {
22409566063dSJacob Faibussowitsch     PetscCall(ISGetSize(subpointMap, &numSubpoints));
22419566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subpointMap, &points));
2242ea844a1aSMatthew Knepley   }
22439566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
224441f23ed0SMatthew G. Knepley   if (renumberPoints) {
224541f23ed0SMatthew G. Knepley     spStart = 0;
224641f23ed0SMatthew G. Knepley     spEnd   = numSubpoints;
224741f23ed0SMatthew G. Knepley   } else {
224841f23ed0SMatthew G. Knepley     PetscCall(ISGetMinMax(subpointMap, &spStart, &spEnd));
224941f23ed0SMatthew G. Knepley     ++spEnd;
225041f23ed0SMatthew G. Knepley   }
225141f23ed0SMatthew G. Knepley   PetscCall(PetscSectionSetChart(*subs, spStart, spEnd));
2252ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
2253ea844a1aSMatthew Knepley     PetscInt dof, cdof, fdof = 0, cfdof = 0;
2254ea844a1aSMatthew Knepley 
22559566063dSJacob Faibussowitsch     PetscCall(PetscFindInt(p, numSubpoints, points, &subp));
2256ea844a1aSMatthew Knepley     if (subp < 0) continue;
225741f23ed0SMatthew G. Knepley     if (!renumberPoints) subp = p;
2258ea844a1aSMatthew Knepley     for (f = 0; f < numFields; ++f) {
22599566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &fdof));
22609566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(*subs, subp, f, fdof));
22619566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(s, p, f, &cfdof));
22629566063dSJacob Faibussowitsch       if (cfdof) PetscCall(PetscSectionSetFieldConstraintDof(*subs, subp, f, cfdof));
2263ea844a1aSMatthew Knepley     }
22649566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s, p, &dof));
22659566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(*subs, subp, dof));
22669566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
22679566063dSJacob Faibussowitsch     if (cdof) PetscCall(PetscSectionSetConstraintDof(*subs, subp, cdof));
2268ea844a1aSMatthew Knepley   }
22699566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(*subs));
2270ea844a1aSMatthew Knepley   /* Change offsets to original offsets */
2271ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
2272ea844a1aSMatthew Knepley     PetscInt off, foff = 0;
2273ea844a1aSMatthew Knepley 
22749566063dSJacob Faibussowitsch     PetscCall(PetscFindInt(p, numSubpoints, points, &subp));
2275ea844a1aSMatthew Knepley     if (subp < 0) continue;
227641f23ed0SMatthew G. Knepley     if (!renumberPoints) subp = p;
2277ea844a1aSMatthew Knepley     for (f = 0; f < numFields; ++f) {
22789566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldOffset(s, p, f, &foff));
22799566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldOffset(*subs, subp, f, foff));
2280ea844a1aSMatthew Knepley     }
22819566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(s, p, &off));
22829566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetOffset(*subs, subp, off));
2283ea844a1aSMatthew Knepley   }
2284ea844a1aSMatthew Knepley   /* Copy constraint indices */
228541f23ed0SMatthew G. Knepley   for (subp = spStart; subp < spEnd; ++subp) {
2286ea844a1aSMatthew Knepley     PetscInt cdof;
2287ea844a1aSMatthew Knepley 
22889566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(*subs, subp, &cdof));
2289ea844a1aSMatthew Knepley     if (cdof) {
2290ea844a1aSMatthew Knepley       for (f = 0; f < numFields; ++f) {
229141f23ed0SMatthew G. Knepley         PetscCall(PetscSectionGetFieldConstraintIndices(s, points[subp - spStart], f, &indices));
22929566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetFieldConstraintIndices(*subs, subp, f, indices));
2293ea844a1aSMatthew Knepley       }
229441f23ed0SMatthew G. Knepley       PetscCall(PetscSectionGetConstraintIndices(s, points[subp - spStart], &indices));
22959566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetConstraintIndices(*subs, subp, indices));
2296ea844a1aSMatthew Knepley     }
2297ea844a1aSMatthew Knepley   }
22989566063dSJacob Faibussowitsch   if (subpointMap) PetscCall(ISRestoreIndices(subpointMap, &points));
22993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2300ea844a1aSMatthew Knepley }
2301ea844a1aSMatthew Knepley 
230241f23ed0SMatthew G. Knepley /*@
230341f23ed0SMatthew G. Knepley   PetscSectionCreateSubmeshSection - Create a new, smaller section with support on the submesh
230441f23ed0SMatthew G. Knepley 
2305c3339decSBarry Smith   Collective
230641f23ed0SMatthew G. Knepley 
230741f23ed0SMatthew G. Knepley   Input Parameters:
2308cab54364SBarry Smith + s           - the `PetscSection`
230941f23ed0SMatthew G. Knepley - subpointMap - a sorted list of points in the original mesh which are in the submesh
231041f23ed0SMatthew G. Knepley 
231141f23ed0SMatthew G. Knepley   Output Parameter:
231241f23ed0SMatthew G. Knepley . subs - the subsection
231341f23ed0SMatthew G. Knepley 
2314cab54364SBarry Smith   Level: advanced
2315cab54364SBarry Smith 
2316583308b6SBarry Smith   Notes:
2317583308b6SBarry 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))`
2318583308b6SBarry Smith 
231920662ed9SBarry Smith   Compare this with `PetscSectionCreateSubdomainSection()` that does not map the points numbers to start at zero but leaves them as before
232041f23ed0SMatthew G. Knepley 
232138b5cf2dSJacob Faibussowitsch   Developer Notes:
232220662ed9SBarry 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`
232341f23ed0SMatthew G. Knepley 
2324cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreateSubdomainSection()`, `PetscSectionCreateSubsection()`, `DMPlexGetSubpointMap()`, `PetscSectionCreate()`
232541f23ed0SMatthew G. Knepley @*/
2326d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCreateSubmeshSection(PetscSection s, IS subpointMap, PetscSection *subs)
2327d71ae5a4SJacob Faibussowitsch {
232841f23ed0SMatthew G. Knepley   PetscFunctionBegin;
2329da8c939bSJacob Faibussowitsch   PetscCall(PetscSectionCreateSubplexSection_Private(s, subpointMap, PETSC_TRUE, subs));
23303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
233141f23ed0SMatthew G. Knepley }
233241f23ed0SMatthew G. Knepley 
233341f23ed0SMatthew G. Knepley /*@
233441f23ed0SMatthew G. Knepley   PetscSectionCreateSubdomainSection - Create a new, smaller section with support on a subdomain of the mesh
233541f23ed0SMatthew G. Knepley 
2336c3339decSBarry Smith   Collective
233741f23ed0SMatthew G. Knepley 
233841f23ed0SMatthew G. Knepley   Input Parameters:
2339cab54364SBarry Smith + s           - the `PetscSection`
234041f23ed0SMatthew G. Knepley - subpointMap - a sorted list of points in the original mesh which are in the subdomain
234141f23ed0SMatthew G. Knepley 
234241f23ed0SMatthew G. Knepley   Output Parameter:
234341f23ed0SMatthew G. Knepley . subs - the subsection
234441f23ed0SMatthew G. Knepley 
2345cab54364SBarry Smith   Level: advanced
2346cab54364SBarry Smith 
2347583308b6SBarry Smith   Notes:
2348583308b6SBarry 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`
2349583308b6SBarry Smith   is `[min(subpointMap),max(subpointMap)+1)`
2350583308b6SBarry Smith 
235120662ed9SBarry Smith   Compare this with `PetscSectionCreateSubmeshSection()` that maps the point numbers to start at zero
235241f23ed0SMatthew G. Knepley 
2353cab54364SBarry Smith   Developer Notes:
235420662ed9SBarry 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`
2355cab54364SBarry Smith 
2356cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreateSubmeshSection()`, `PetscSectionCreateSubsection()`, `DMPlexGetSubpointMap()`, `PetscSectionCreate()`
235741f23ed0SMatthew G. Knepley @*/
2358d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCreateSubdomainSection(PetscSection s, IS subpointMap, PetscSection *subs)
2359d71ae5a4SJacob Faibussowitsch {
236041f23ed0SMatthew G. Knepley   PetscFunctionBegin;
2361da8c939bSJacob Faibussowitsch   PetscCall(PetscSectionCreateSubplexSection_Private(s, subpointMap, PETSC_FALSE, subs));
23623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
236341f23ed0SMatthew G. Knepley }
236441f23ed0SMatthew G. Knepley 
2365d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscSectionView_ASCII(PetscSection s, PetscViewer viewer)
2366d71ae5a4SJacob Faibussowitsch {
2367ea844a1aSMatthew Knepley   PetscInt    p;
2368ea844a1aSMatthew Knepley   PetscMPIInt rank;
2369ea844a1aSMatthew Knepley 
2370ea844a1aSMatthew Knepley   PetscFunctionBegin;
23719566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank));
23729566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPushSynchronized(viewer));
23739566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "Process %d:\n", rank));
2374ea844a1aSMatthew Knepley   for (p = 0; p < s->pEnd - s->pStart; ++p) {
23753857f682SStefano Zampini     if (s->bc && s->bc->atlasDof[p] > 0) {
2376ea844a1aSMatthew Knepley       PetscInt b;
23779566063dSJacob 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]));
2378083401c6SMatthew G. Knepley       if (s->bcIndices) {
237948a46eb9SPierre Jolivet         for (b = 0; b < s->bc->atlasDof[p]; ++b) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " %" PetscInt_FMT, s->bcIndices[s->bc->atlasOff[p] + b]));
2380083401c6SMatthew G. Knepley       }
23819566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n"));
2382ea844a1aSMatthew Knepley     } else {
23839566063dSJacob 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]));
2384ea844a1aSMatthew Knepley     }
2385ea844a1aSMatthew Knepley   }
23869566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(viewer));
23879566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPopSynchronized(viewer));
2388ea844a1aSMatthew Knepley   if (s->sym) {
23899566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPushTab(viewer));
23909566063dSJacob Faibussowitsch     PetscCall(PetscSectionSymView(s->sym, viewer));
23919566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPopTab(viewer));
2392ea844a1aSMatthew Knepley   }
23933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2394ea844a1aSMatthew Knepley }
2395ea844a1aSMatthew Knepley 
2396ea844a1aSMatthew Knepley /*@C
2397cab54364SBarry Smith   PetscSectionViewFromOptions - View the `PetscSection` based on values in the options database
2398fe2efc57SMark 
239940750d41SVaclav Hapla   Collective
2400fe2efc57SMark 
2401fe2efc57SMark   Input Parameters:
240220662ed9SBarry Smith + A    - the `PetscSection` object to view
2403cab54364SBarry Smith . obj  - Optional object that provides the options prefix used for the options
2404736c3998SJose E. Roman - name - command line option
2405fe2efc57SMark 
2406fe2efc57SMark   Level: intermediate
2407cab54364SBarry Smith 
240820662ed9SBarry Smith   Note:
240920662ed9SBarry Smith   See `PetscObjectViewFromOptions()` for available values of `PetscViewer` and `PetscViewerFormat`
241020662ed9SBarry Smith 
2411cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionView`, `PetscObjectViewFromOptions()`, `PetscSectionCreate()`, `PetscSectionView()`
2412fe2efc57SMark @*/
2413d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionViewFromOptions(PetscSection A, PetscObject obj, const char name[])
2414d71ae5a4SJacob Faibussowitsch {
2415fe2efc57SMark   PetscFunctionBegin;
2416fe2efc57SMark   PetscValidHeaderSpecific(A, PETSC_SECTION_CLASSID, 1);
24179566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
24183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2419fe2efc57SMark }
2420fe2efc57SMark 
2421fe2efc57SMark /*@C
2422cab54364SBarry Smith   PetscSectionView - Views a `PetscSection`
2423ea844a1aSMatthew Knepley 
242440750d41SVaclav Hapla   Collective
2425ea844a1aSMatthew Knepley 
2426ea844a1aSMatthew Knepley   Input Parameters:
2427cab54364SBarry Smith + s      - the `PetscSection` object to view
242838b5cf2dSJacob Faibussowitsch - viewer - the viewer
2429ea844a1aSMatthew Knepley 
2430cab54364SBarry Smith   Level: beginner
2431cab54364SBarry Smith 
24321ddd528eSksagiyam   Note:
2433cab54364SBarry Smith   `PetscSectionView()`, when viewer is of type `PETSCVIEWERHDF5`, only saves
24341ddd528eSksagiyam   distribution independent data, such as dofs, offsets, constraint dofs,
24351ddd528eSksagiyam   and constraint indices. Points that have negative dofs, for instance,
24361ddd528eSksagiyam   are not saved as they represent points owned by other processes.
24371ddd528eSksagiyam   Point numbering and rank assignment is currently not stored.
2438cab54364SBarry Smith   The saved section can be loaded with `PetscSectionLoad()`.
24391ddd528eSksagiyam 
2440cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionDestroy()`, `PetscSectionLoad()`, `PetscViewer`
2441ea844a1aSMatthew Knepley @*/
2442d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionView(PetscSection s, PetscViewer viewer)
2443d71ae5a4SJacob Faibussowitsch {
24441ddd528eSksagiyam   PetscBool isascii, ishdf5;
2445ea844a1aSMatthew Knepley   PetscInt  f;
2446ea844a1aSMatthew Knepley 
2447ea844a1aSMatthew Knepley   PetscFunctionBegin;
2448ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
24499566063dSJacob Faibussowitsch   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)s), &viewer));
2450ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
24519566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
24529566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2453ea844a1aSMatthew Knepley   if (isascii) {
24549566063dSJacob Faibussowitsch     PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)s, viewer));
2455ea844a1aSMatthew Knepley     if (s->numFields) {
24569566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " fields\n", s->numFields));
2457ea844a1aSMatthew Knepley       for (f = 0; f < s->numFields; ++f) {
24584cd8913cSStefano Zampini         PetscCall(PetscViewerASCIIPrintf(viewer, "  field %" PetscInt_FMT " \"%s\" with %" PetscInt_FMT " components\n", f, s->fieldNames[f], s->numFieldComponents[f]));
24599566063dSJacob Faibussowitsch         PetscCall(PetscSectionView_ASCII(s->field[f], viewer));
2460ea844a1aSMatthew Knepley       }
2461ea844a1aSMatthew Knepley     } else {
24629566063dSJacob Faibussowitsch       PetscCall(PetscSectionView_ASCII(s, viewer));
2463ea844a1aSMatthew Knepley     }
24641ddd528eSksagiyam   } else if (ishdf5) {
24651ddd528eSksagiyam #if PetscDefined(HAVE_HDF5)
24669566063dSJacob Faibussowitsch     PetscCall(PetscSectionView_HDF5_Internal(s, viewer));
24671ddd528eSksagiyam #else
24681ddd528eSksagiyam     SETERRQ(PetscObjectComm((PetscObject)s), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
24691ddd528eSksagiyam #endif
2470ea844a1aSMatthew Knepley   }
24713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2472ea844a1aSMatthew Knepley }
2473ea844a1aSMatthew Knepley 
2474fde5e3acSksagiyam /*@C
2475cab54364SBarry Smith   PetscSectionLoad - Loads a `PetscSection`
2476fde5e3acSksagiyam 
247740750d41SVaclav Hapla   Collective
2478fde5e3acSksagiyam 
2479fde5e3acSksagiyam   Input Parameters:
2480cab54364SBarry Smith + s      - the `PetscSection` object to load
248138b5cf2dSJacob Faibussowitsch - viewer - the viewer
2482fde5e3acSksagiyam 
2483cab54364SBarry Smith   Level: beginner
2484cab54364SBarry Smith 
2485fde5e3acSksagiyam   Note:
2486cab54364SBarry Smith   `PetscSectionLoad()`, when viewer is of type `PETSCVIEWERHDF5`, loads
2487cab54364SBarry Smith   a section saved with `PetscSectionView()`. The number of processes
2488fde5e3acSksagiyam   used here (N) does not need to be the same as that used when saving.
2489fde5e3acSksagiyam   After calling this function, the chart of s on rank i will be set
2490fde5e3acSksagiyam   to [0, E_i), where \sum_{i=0}^{N-1}E_i equals to the total number of
2491fde5e3acSksagiyam   saved section points.
2492fde5e3acSksagiyam 
2493cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionDestroy()`, `PetscSectionView()`
2494fde5e3acSksagiyam @*/
2495d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionLoad(PetscSection s, PetscViewer viewer)
2496d71ae5a4SJacob Faibussowitsch {
2497fde5e3acSksagiyam   PetscBool ishdf5;
2498fde5e3acSksagiyam 
2499fde5e3acSksagiyam   PetscFunctionBegin;
2500fde5e3acSksagiyam   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2501fde5e3acSksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
25029566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2503fde5e3acSksagiyam   if (ishdf5) {
2504fde5e3acSksagiyam #if PetscDefined(HAVE_HDF5)
25059566063dSJacob Faibussowitsch     PetscCall(PetscSectionLoad_HDF5_Internal(s, viewer));
25063ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2507fde5e3acSksagiyam #else
2508fde5e3acSksagiyam     SETERRQ(PetscObjectComm((PetscObject)s), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2509fde5e3acSksagiyam #endif
251098921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)s), PETSC_ERR_SUP, "Viewer type %s not yet supported for PetscSection loading", ((PetscObject)viewer)->type_name);
2511fde5e3acSksagiyam }
2512fde5e3acSksagiyam 
2513d4a1ad33SMatthew G. Knepley /*@
2514d4a1ad33SMatthew G. Knepley   PetscSectionResetClosurePermutation - Remove any existing closure permutation
2515d4a1ad33SMatthew G. Knepley 
2516d4a1ad33SMatthew G. Knepley   Input Parameter:
2517e8e188d2SZach Atkins . section - The `PetscSection`
2518d4a1ad33SMatthew G. Knepley 
2519d4a1ad33SMatthew G. Knepley   Level: intermediate
2520d4a1ad33SMatthew G. Knepley 
2521e8e188d2SZach Atkins .seealso: `PetscSectionSetClosurePermutation()`, `PetscSectionSetClosureIndex()`, `PetscSectionReset()`
2522d4a1ad33SMatthew G. Knepley @*/
2523d4a1ad33SMatthew G. Knepley PetscErrorCode PetscSectionResetClosurePermutation(PetscSection section)
2524d71ae5a4SJacob Faibussowitsch {
2525c459fbc1SJed Brown   PetscSectionClosurePermVal clVal;
2526c459fbc1SJed Brown 
2527c459fbc1SJed Brown   PetscFunctionBegin;
25283ba16761SJacob Faibussowitsch   if (!section->clHash) PetscFunctionReturn(PETSC_SUCCESS);
2529c459fbc1SJed Brown   kh_foreach_value(section->clHash, clVal, {
25309566063dSJacob Faibussowitsch     PetscCall(PetscFree(clVal.perm));
25319566063dSJacob Faibussowitsch     PetscCall(PetscFree(clVal.invPerm));
2532c459fbc1SJed Brown   });
2533c459fbc1SJed Brown   kh_destroy(ClPerm, section->clHash);
2534c459fbc1SJed Brown   section->clHash = NULL;
25353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2536c459fbc1SJed Brown }
2537c459fbc1SJed Brown 
2538ea844a1aSMatthew Knepley /*@
2539583308b6SBarry Smith   PetscSectionReset - Frees all section data, the section is then as if `PetscSectionCreate()` had just been called.
2540ea844a1aSMatthew Knepley 
254140750d41SVaclav Hapla   Not Collective
2542ea844a1aSMatthew Knepley 
25432fe279fdSBarry Smith   Input Parameter:
2544cab54364SBarry Smith . s - the `PetscSection`
2545ea844a1aSMatthew Knepley 
2546ea844a1aSMatthew Knepley   Level: beginner
2547ea844a1aSMatthew Knepley 
2548cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreate()`
2549ea844a1aSMatthew Knepley @*/
2550d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionReset(PetscSection s)
2551d71ae5a4SJacob Faibussowitsch {
2552b778fa18SValeria Barra   PetscInt f, c;
2553ea844a1aSMatthew Knepley 
2554ea844a1aSMatthew Knepley   PetscFunctionBegin;
2555ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2556ea844a1aSMatthew Knepley   for (f = 0; f < s->numFields; ++f) {
25579566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&s->field[f]));
25589566063dSJacob Faibussowitsch     PetscCall(PetscFree(s->fieldNames[f]));
255948a46eb9SPierre Jolivet     for (c = 0; c < s->numFieldComponents[f]; ++c) PetscCall(PetscFree(s->compNames[f][c]));
25609566063dSJacob Faibussowitsch     PetscCall(PetscFree(s->compNames[f]));
2561ea844a1aSMatthew Knepley   }
25629566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->numFieldComponents));
25639566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->fieldNames));
25649566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->compNames));
25659566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->field));
25669566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&s->bc));
25679566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->bcIndices));
25689566063dSJacob Faibussowitsch   PetscCall(PetscFree2(s->atlasDof, s->atlasOff));
25699566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&s->clSection));
25709566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&s->clPoints));
25719566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&s->perm));
25723ec46b7bSMatthew G. Knepley   PetscCall(PetscBTDestroy(&s->blockStarts));
2573d4a1ad33SMatthew G. Knepley   PetscCall(PetscSectionResetClosurePermutation(s));
25749566063dSJacob Faibussowitsch   PetscCall(PetscSectionSymDestroy(&s->sym));
25759566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&s->clSection));
25769566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&s->clPoints));
257769c11d05SVaclav Hapla   PetscCall(PetscSectionInvalidateMaxDof_Internal(s));
2578ea844a1aSMatthew Knepley   s->pStart    = -1;
2579ea844a1aSMatthew Knepley   s->pEnd      = -1;
2580ea844a1aSMatthew Knepley   s->maxDof    = 0;
2581ea844a1aSMatthew Knepley   s->setup     = PETSC_FALSE;
2582ea844a1aSMatthew Knepley   s->numFields = 0;
2583ea844a1aSMatthew Knepley   s->clObj     = NULL;
25843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2585ea844a1aSMatthew Knepley }
2586ea844a1aSMatthew Knepley 
2587ea844a1aSMatthew Knepley /*@
2588583308b6SBarry Smith   PetscSectionDestroy - Frees a `PetscSection`
2589ea844a1aSMatthew Knepley 
259040750d41SVaclav Hapla   Not Collective
2591ea844a1aSMatthew Knepley 
25922fe279fdSBarry Smith   Input Parameter:
2593cab54364SBarry Smith . s - the `PetscSection`
2594ea844a1aSMatthew Knepley 
2595ea844a1aSMatthew Knepley   Level: beginner
2596ea844a1aSMatthew Knepley 
2597cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionReset()`
2598ea844a1aSMatthew Knepley @*/
2599d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionDestroy(PetscSection *s)
2600d71ae5a4SJacob Faibussowitsch {
2601ea844a1aSMatthew Knepley   PetscFunctionBegin;
26023ba16761SJacob Faibussowitsch   if (!*s) PetscFunctionReturn(PETSC_SUCCESS);
260362f17a49SStefano Zampini   PetscValidHeaderSpecific(*s, PETSC_SECTION_CLASSID, 1);
2604ea844a1aSMatthew Knepley   if (--((PetscObject)(*s))->refct > 0) {
2605ea844a1aSMatthew Knepley     *s = NULL;
26063ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2607ea844a1aSMatthew Knepley   }
26089566063dSJacob Faibussowitsch   PetscCall(PetscSectionReset(*s));
26099566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(s));
26103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2611ea844a1aSMatthew Knepley }
2612ea844a1aSMatthew Knepley 
2613da8c939bSJacob Faibussowitsch static PetscErrorCode VecIntGetValuesSection_Private(const PetscInt *baseArray, PetscSection s, PetscInt point, const PetscInt **values)
2614d71ae5a4SJacob Faibussowitsch {
2615ea844a1aSMatthew Knepley   const PetscInt p = point - s->pStart;
2616ea844a1aSMatthew Knepley 
2617ea844a1aSMatthew Knepley   PetscFunctionBegin;
2618ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 2);
2619ea844a1aSMatthew Knepley   *values = &baseArray[s->atlasOff[p]];
26203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2621ea844a1aSMatthew Knepley }
2622ea844a1aSMatthew Knepley 
2623da8c939bSJacob Faibussowitsch static PetscErrorCode VecIntSetValuesSection_Private(PetscInt *baseArray, PetscSection s, PetscInt point, const PetscInt values[], InsertMode mode)
2624d71ae5a4SJacob Faibussowitsch {
2625ea844a1aSMatthew Knepley   PetscInt      *array;
2626ea844a1aSMatthew Knepley   const PetscInt p           = point - s->pStart;
2627ea844a1aSMatthew Knepley   const PetscInt orientation = 0; /* Needs to be included for use in closure operations */
2628ea844a1aSMatthew Knepley   PetscInt       cDim        = 0;
2629ea844a1aSMatthew Knepley 
2630ea844a1aSMatthew Knepley   PetscFunctionBegin;
2631ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 2);
26329566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstraintDof(s, p, &cDim));
2633ea844a1aSMatthew Knepley   array = &baseArray[s->atlasOff[p]];
2634ea844a1aSMatthew Knepley   if (!cDim) {
2635ea844a1aSMatthew Knepley     if (orientation >= 0) {
2636ea844a1aSMatthew Knepley       const PetscInt dim = s->atlasDof[p];
2637ea844a1aSMatthew Knepley       PetscInt       i;
2638ea844a1aSMatthew Knepley 
2639ea844a1aSMatthew Knepley       if (mode == INSERT_VALUES) {
2640ad716f99SStefano Zampini         for (i = 0; i < dim; ++i) array[i] = values ? values[i] : i;
2641ea844a1aSMatthew Knepley       } else {
2642ea844a1aSMatthew Knepley         for (i = 0; i < dim; ++i) array[i] += values[i];
2643ea844a1aSMatthew Knepley       }
2644ea844a1aSMatthew Knepley     } else {
2645ea844a1aSMatthew Knepley       PetscInt offset = 0;
2646ea844a1aSMatthew Knepley       PetscInt j      = -1, field, i;
2647ea844a1aSMatthew Knepley 
2648ea844a1aSMatthew Knepley       for (field = 0; field < s->numFields; ++field) {
2649ea844a1aSMatthew Knepley         const PetscInt dim = s->field[field]->atlasDof[p];
2650ea844a1aSMatthew Knepley 
2651ad716f99SStefano Zampini         for (i = dim - 1; i >= 0; --i) array[++j] = values ? values[i + offset] : i + offset;
2652ea844a1aSMatthew Knepley         offset += dim;
2653ea844a1aSMatthew Knepley       }
2654ea844a1aSMatthew Knepley     }
2655ea844a1aSMatthew Knepley   } else {
2656ea844a1aSMatthew Knepley     if (orientation >= 0) {
2657ea844a1aSMatthew Knepley       const PetscInt  dim  = s->atlasDof[p];
2658ea844a1aSMatthew Knepley       PetscInt        cInd = 0, i;
2659ea844a1aSMatthew Knepley       const PetscInt *cDof;
2660ea844a1aSMatthew Knepley 
26619566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetConstraintIndices(s, point, &cDof));
2662ea844a1aSMatthew Knepley       if (mode == INSERT_VALUES) {
2663ea844a1aSMatthew Knepley         for (i = 0; i < dim; ++i) {
26649371c9d4SSatish Balay           if ((cInd < cDim) && (i == cDof[cInd])) {
26659371c9d4SSatish Balay             ++cInd;
26669371c9d4SSatish Balay             continue;
26679371c9d4SSatish Balay           }
2668ad716f99SStefano Zampini           array[i] = values ? values[i] : i;
2669ea844a1aSMatthew Knepley         }
2670ea844a1aSMatthew Knepley       } else {
2671ea844a1aSMatthew Knepley         for (i = 0; i < dim; ++i) {
26729371c9d4SSatish Balay           if ((cInd < cDim) && (i == cDof[cInd])) {
26739371c9d4SSatish Balay             ++cInd;
26749371c9d4SSatish Balay             continue;
26759371c9d4SSatish Balay           }
2676ea844a1aSMatthew Knepley           array[i] += values[i];
2677ea844a1aSMatthew Knepley         }
2678ea844a1aSMatthew Knepley       }
2679ea844a1aSMatthew Knepley     } else {
2680ea844a1aSMatthew Knepley       const PetscInt *cDof;
2681ea844a1aSMatthew Knepley       PetscInt        offset  = 0;
2682ea844a1aSMatthew Knepley       PetscInt        cOffset = 0;
2683ea844a1aSMatthew Knepley       PetscInt        j       = 0, field;
2684ea844a1aSMatthew Knepley 
26859566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetConstraintIndices(s, point, &cDof));
2686ea844a1aSMatthew Knepley       for (field = 0; field < s->numFields; ++field) {
2687ea844a1aSMatthew Knepley         const PetscInt dim  = s->field[field]->atlasDof[p];     /* PetscSectionGetFieldDof() */
2688ea844a1aSMatthew Knepley         const PetscInt tDim = s->field[field]->bc->atlasDof[p]; /* PetscSectionGetFieldConstraintDof() */
2689ea844a1aSMatthew Knepley         const PetscInt sDim = dim - tDim;
2690ea844a1aSMatthew Knepley         PetscInt       cInd = 0, i, k;
2691ea844a1aSMatthew Knepley 
2692ea844a1aSMatthew Knepley         for (i = 0, k = dim + offset - 1; i < dim; ++i, ++j, --k) {
26939371c9d4SSatish Balay           if ((cInd < sDim) && (j == cDof[cInd + cOffset])) {
26949371c9d4SSatish Balay             ++cInd;
26959371c9d4SSatish Balay             continue;
26969371c9d4SSatish Balay           }
2697ad716f99SStefano Zampini           array[j] = values ? values[k] : k;
2698ea844a1aSMatthew Knepley         }
2699ea844a1aSMatthew Knepley         offset += dim;
2700ea844a1aSMatthew Knepley         cOffset += dim - tDim;
2701ea844a1aSMatthew Knepley       }
2702ea844a1aSMatthew Knepley     }
2703ea844a1aSMatthew Knepley   }
27043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2705ea844a1aSMatthew Knepley }
2706ea844a1aSMatthew Knepley 
2707ea844a1aSMatthew Knepley /*@C
270820662ed9SBarry Smith   PetscSectionHasConstraints - Determine whether a `PetscSection` has constrained dofs
2709ea844a1aSMatthew Knepley 
271040750d41SVaclav Hapla   Not Collective
2711ea844a1aSMatthew Knepley 
2712ea844a1aSMatthew Knepley   Input Parameter:
2713cab54364SBarry Smith . s - The `PetscSection`
2714ea844a1aSMatthew Knepley 
2715ea844a1aSMatthew Knepley   Output Parameter:
2716ea844a1aSMatthew Knepley . hasConstraints - flag indicating that the section has constrained dofs
2717ea844a1aSMatthew Knepley 
2718ea844a1aSMatthew Knepley   Level: intermediate
2719ea844a1aSMatthew Knepley 
2720cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSetConstraintIndices()`, `PetscSectionGetConstraintDof()`, `PetscSection`
2721ea844a1aSMatthew Knepley @*/
2722d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionHasConstraints(PetscSection s, PetscBool *hasConstraints)
2723d71ae5a4SJacob Faibussowitsch {
2724ea844a1aSMatthew Knepley   PetscFunctionBegin;
2725ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
27264f572ea9SToby Isaac   PetscAssertPointer(hasConstraints, 2);
2727ea844a1aSMatthew Knepley   *hasConstraints = s->bc ? PETSC_TRUE : PETSC_FALSE;
27283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2729ea844a1aSMatthew Knepley }
2730ea844a1aSMatthew Knepley 
2731ea844a1aSMatthew Knepley /*@C
273220662ed9SBarry Smith   PetscSectionGetConstraintIndices - Get the point dof numbers, in [0, dof), which are constrained for a given point
2733ea844a1aSMatthew Knepley 
273440750d41SVaclav Hapla   Not Collective
2735ea844a1aSMatthew Knepley 
2736ea844a1aSMatthew Knepley   Input Parameters:
2737cab54364SBarry Smith + s     - The `PetscSection`
2738ea844a1aSMatthew Knepley - point - The point
2739ea844a1aSMatthew Knepley 
2740ea844a1aSMatthew Knepley   Output Parameter:
2741ea844a1aSMatthew Knepley . indices - The constrained dofs
2742ea844a1aSMatthew Knepley 
2743ea844a1aSMatthew Knepley   Level: intermediate
2744ea844a1aSMatthew Knepley 
274538b5cf2dSJacob Faibussowitsch   Fortran Notes:
274620662ed9SBarry Smith   Use `PetscSectionGetConstraintIndicesF90()` and `PetscSectionRestoreConstraintIndicesF90()`
2747cab54364SBarry Smith 
2748cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSetConstraintIndices()`, `PetscSectionGetConstraintDof()`, `PetscSection`
2749ea844a1aSMatthew Knepley @*/
2750d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetConstraintIndices(PetscSection s, PetscInt point, const PetscInt **indices)
2751d71ae5a4SJacob Faibussowitsch {
2752ea844a1aSMatthew Knepley   PetscFunctionBegin;
2753ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2754ea844a1aSMatthew Knepley   if (s->bc) {
2755da8c939bSJacob Faibussowitsch     PetscCall(VecIntGetValuesSection_Private(s->bcIndices, s->bc, point, indices));
2756ea844a1aSMatthew Knepley   } else *indices = NULL;
27573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2758ea844a1aSMatthew Knepley }
2759ea844a1aSMatthew Knepley 
2760ea844a1aSMatthew Knepley /*@C
2761ea844a1aSMatthew Knepley   PetscSectionSetConstraintIndices - Set the point dof numbers, in [0, dof), which are constrained
2762ea844a1aSMatthew Knepley 
276340750d41SVaclav Hapla   Not Collective
2764ea844a1aSMatthew Knepley 
2765ea844a1aSMatthew Knepley   Input Parameters:
2766cab54364SBarry Smith + s       - The `PetscSection`
2767ea844a1aSMatthew Knepley . point   - The point
2768ea844a1aSMatthew Knepley - indices - The constrained dofs
2769ea844a1aSMatthew Knepley 
2770ea844a1aSMatthew Knepley   Level: intermediate
2771ea844a1aSMatthew Knepley 
277238b5cf2dSJacob Faibussowitsch   Fortran Notes:
2773cab54364SBarry Smith   Use `PetscSectionSetConstraintIndicesF90()`
2774cab54364SBarry Smith 
2775cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionGetConstraintIndices()`, `PetscSectionGetConstraintDof()`, `PetscSection`
2776ea844a1aSMatthew Knepley @*/
2777d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetConstraintIndices(PetscSection s, PetscInt point, const PetscInt indices[])
2778d71ae5a4SJacob Faibussowitsch {
2779ea844a1aSMatthew Knepley   PetscFunctionBegin;
2780ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2781ea844a1aSMatthew Knepley   if (s->bc) {
2782d57bb9dbSMatthew G. Knepley     const PetscInt dof  = s->atlasDof[point];
2783d57bb9dbSMatthew G. Knepley     const PetscInt cdof = s->bc->atlasDof[point];
2784d57bb9dbSMatthew G. Knepley     PetscInt       d;
2785d57bb9dbSMatthew G. Knepley 
2786ad716f99SStefano Zampini     if (indices)
2787ad540459SPierre 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]);
2788da8c939bSJacob Faibussowitsch     PetscCall(VecIntSetValuesSection_Private(s->bcIndices, s->bc, point, indices, INSERT_VALUES));
2789ea844a1aSMatthew Knepley   }
27903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2791ea844a1aSMatthew Knepley }
2792ea844a1aSMatthew Knepley 
2793ea844a1aSMatthew Knepley /*@C
2794ea844a1aSMatthew Knepley   PetscSectionGetFieldConstraintIndices - Get the field dof numbers, in [0, fdof), which are constrained
2795ea844a1aSMatthew Knepley 
279640750d41SVaclav Hapla   Not Collective
2797ea844a1aSMatthew Knepley 
2798ea844a1aSMatthew Knepley   Input Parameters:
2799cab54364SBarry Smith + s     - The `PetscSection`
2800ea844a1aSMatthew Knepley . field - The field number
2801ea844a1aSMatthew Knepley - point - The point
2802ea844a1aSMatthew Knepley 
2803ea844a1aSMatthew Knepley   Output Parameter:
28049759eb26SJed Brown . indices - The constrained dofs sorted in ascending order
2805ea844a1aSMatthew Knepley 
2806ea844a1aSMatthew Knepley   Level: intermediate
2807ea844a1aSMatthew Knepley 
2808cab54364SBarry Smith   Note:
2809cab54364SBarry 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()`.
2810cab54364SBarry Smith 
281138b5cf2dSJacob Faibussowitsch   Fortran Notes:
281220662ed9SBarry Smith   Use `PetscSectionGetFieldConstraintIndicesF90()` and `PetscSectionRestoreFieldConstraintIndicesF90()`
2813cab54364SBarry Smith 
2814cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSetFieldConstraintIndices()`, `PetscSectionGetConstraintIndices()`, `PetscSectionGetConstraintDof()`, `PetscSection`
2815ea844a1aSMatthew Knepley @*/
2816d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldConstraintIndices(PetscSection s, PetscInt point, PetscInt field, const PetscInt **indices)
2817d71ae5a4SJacob Faibussowitsch {
2818ea844a1aSMatthew Knepley   PetscFunctionBegin;
2819ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
28204f572ea9SToby Isaac   PetscAssertPointer(indices, 4);
28212abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
28229566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstraintIndices(s->field[field], point, indices));
28233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2824ea844a1aSMatthew Knepley }
2825ea844a1aSMatthew Knepley 
2826ea844a1aSMatthew Knepley /*@C
2827ea844a1aSMatthew Knepley   PetscSectionSetFieldConstraintIndices - Set the field dof numbers, in [0, fdof), which are constrained
2828ea844a1aSMatthew Knepley 
282940750d41SVaclav Hapla   Not Collective
2830ea844a1aSMatthew Knepley 
2831ea844a1aSMatthew Knepley   Input Parameters:
2832cab54364SBarry Smith + s       - The `PetscSection`
2833ea844a1aSMatthew Knepley . point   - The point
2834ea844a1aSMatthew Knepley . field   - The field number
2835ea844a1aSMatthew Knepley - indices - The constrained dofs
2836ea844a1aSMatthew Knepley 
2837ea844a1aSMatthew Knepley   Level: intermediate
2838ea844a1aSMatthew Knepley 
283938b5cf2dSJacob Faibussowitsch   Fortran Notes:
2840cab54364SBarry Smith   Use `PetscSectionSetFieldConstraintIndicesF90()`
2841cab54364SBarry Smith 
2842cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSetConstraintIndices()`, `PetscSectionGetFieldConstraintIndices()`, `PetscSectionGetConstraintDof()`, `PetscSection`
2843ea844a1aSMatthew Knepley @*/
2844d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFieldConstraintIndices(PetscSection s, PetscInt point, PetscInt field, const PetscInt indices[])
2845d71ae5a4SJacob Faibussowitsch {
2846ea844a1aSMatthew Knepley   PetscFunctionBegin;
2847ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
28482abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
28499566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetConstraintIndices(s->field[field], point, indices));
28503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2851ea844a1aSMatthew Knepley }
2852ea844a1aSMatthew Knepley 
2853ea844a1aSMatthew Knepley /*@
2854ea844a1aSMatthew Knepley   PetscSectionPermute - Reorder the section according to the input point permutation
2855ea844a1aSMatthew Knepley 
285640750d41SVaclav Hapla   Collective
2857ea844a1aSMatthew Knepley 
2858d8d19677SJose E. Roman   Input Parameters:
2859cab54364SBarry Smith + section     - The `PetscSection` object
286038b5cf2dSJacob Faibussowitsch - permutation - The point permutation, old point p becomes new point perm[p]
2861ea844a1aSMatthew Knepley 
2862ea844a1aSMatthew Knepley   Output Parameter:
2863cab54364SBarry Smith . sectionNew - The permuted `PetscSection`
2864ea844a1aSMatthew Knepley 
2865ea844a1aSMatthew Knepley   Level: intermediate
2866ea844a1aSMatthew Knepley 
2867583308b6SBarry Smith   Note:
2868583308b6SBarry Smith   The data and the access to the data via `PetscSectionGetFieldOffset()` and `PetscSectionGetOffset()` are both changed in `sectionNew`
2869583308b6SBarry Smith 
2870583308b6SBarry Smith   Compare to `PetscSectionSetPermutation()`
2871583308b6SBarry Smith 
2872583308b6SBarry Smith .seealso: [PetscSection](sec_petscsection), `IS`, `PetscSection`, `MatPermute()`, `PetscSectionSetPermutation()`
2873ea844a1aSMatthew Knepley @*/
2874d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionPermute(PetscSection section, IS permutation, PetscSection *sectionNew)
2875d71ae5a4SJacob Faibussowitsch {
2876ea844a1aSMatthew Knepley   PetscSection    s = section, sNew;
2877ea844a1aSMatthew Knepley   const PetscInt *perm;
2878b778fa18SValeria Barra   PetscInt        numFields, f, c, numPoints, pStart, pEnd, p;
2879ea844a1aSMatthew Knepley 
2880ea844a1aSMatthew Knepley   PetscFunctionBegin;
2881ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
2882ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(permutation, IS_CLASSID, 2);
28834f572ea9SToby Isaac   PetscAssertPointer(sectionNew, 3);
28849566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s), &sNew));
28859566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &numFields));
28869566063dSJacob Faibussowitsch   if (numFields) PetscCall(PetscSectionSetNumFields(sNew, numFields));
2887ea844a1aSMatthew Knepley   for (f = 0; f < numFields; ++f) {
2888ea844a1aSMatthew Knepley     const char *name;
2889ea844a1aSMatthew Knepley     PetscInt    numComp;
2890ea844a1aSMatthew Knepley 
28919566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldName(s, f, &name));
28929566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldName(sNew, f, name));
28939566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &numComp));
28949566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(sNew, f, numComp));
2895b778fa18SValeria Barra     for (c = 0; c < s->numFieldComponents[f]; ++c) {
28969566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetComponentName(s, f, c, &name));
28979566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetComponentName(sNew, f, c, name));
2898b778fa18SValeria Barra     }
2899ea844a1aSMatthew Knepley   }
29009566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(permutation, &numPoints));
29019566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(permutation, &perm));
29029566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
29039566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(sNew, pStart, pEnd));
290408401ef6SPierre 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);
2905ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
2906ea844a1aSMatthew Knepley     PetscInt dof, cdof;
2907ea844a1aSMatthew Knepley 
29089566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s, p, &dof));
29099566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(sNew, perm[p], dof));
29109566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
29119566063dSJacob Faibussowitsch     if (cdof) PetscCall(PetscSectionSetConstraintDof(sNew, perm[p], cdof));
2912ea844a1aSMatthew Knepley     for (f = 0; f < numFields; ++f) {
29139566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
29149566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(sNew, perm[p], f, dof));
29159566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(s, p, f, &cdof));
29169566063dSJacob Faibussowitsch       if (cdof) PetscCall(PetscSectionSetFieldConstraintDof(sNew, perm[p], f, cdof));
2917ea844a1aSMatthew Knepley     }
2918ea844a1aSMatthew Knepley   }
29199566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(sNew));
2920ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
2921ea844a1aSMatthew Knepley     const PetscInt *cind;
2922ea844a1aSMatthew Knepley     PetscInt        cdof;
2923ea844a1aSMatthew Knepley 
29249566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
2925ea844a1aSMatthew Knepley     if (cdof) {
29269566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetConstraintIndices(s, p, &cind));
29279566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetConstraintIndices(sNew, perm[p], cind));
2928ea844a1aSMatthew Knepley     }
2929ea844a1aSMatthew Knepley     for (f = 0; f < numFields; ++f) {
29309566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(s, p, f, &cdof));
2931ea844a1aSMatthew Knepley       if (cdof) {
29329566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldConstraintIndices(s, p, f, &cind));
29339566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetFieldConstraintIndices(sNew, perm[p], f, cind));
2934ea844a1aSMatthew Knepley       }
2935ea844a1aSMatthew Knepley     }
2936ea844a1aSMatthew Knepley   }
29379566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(permutation, &perm));
2938ea844a1aSMatthew Knepley   *sectionNew = sNew;
29393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2940ea844a1aSMatthew Knepley }
2941ea844a1aSMatthew Knepley 
2942ea844a1aSMatthew Knepley /*@
294320662ed9SBarry Smith   PetscSectionSetClosureIndex - Create an internal data structure to speed up closure queries.
2944ea844a1aSMatthew Knepley 
294540750d41SVaclav Hapla   Collective
2946ea844a1aSMatthew Knepley 
2947ea844a1aSMatthew Knepley   Input Parameters:
2948cab54364SBarry Smith + section   - The `PetscSection`
2949cab54364SBarry Smith . obj       - A `PetscObject` which serves as the key for this index
2950cab54364SBarry Smith . clSection - `PetscSection` giving the size of the closure of each point
2951cab54364SBarry Smith - clPoints  - `IS` giving the points in each closure
2952ea844a1aSMatthew Knepley 
2953ea844a1aSMatthew Knepley   Level: advanced
2954ea844a1aSMatthew Knepley 
2955cab54364SBarry Smith   Note:
295620662ed9SBarry Smith   This function creates an internal map from each point to its closure. We compress out closure points with no dofs in this section.
295720662ed9SBarry Smith 
295838b5cf2dSJacob Faibussowitsch   Developer Notes:
295920662ed9SBarry Smith   The information provided here is completely opaque
2960cab54364SBarry Smith 
2961cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionGetClosureIndex()`, `DMPlexCreateClosureIndex()`
2962ea844a1aSMatthew Knepley @*/
2963d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetClosureIndex(PetscSection section, PetscObject obj, PetscSection clSection, IS clPoints)
2964d71ae5a4SJacob Faibussowitsch {
2965ea844a1aSMatthew Knepley   PetscFunctionBegin;
29663e03ebd8SStefano Zampini   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
29673e03ebd8SStefano Zampini   PetscValidHeaderSpecific(clSection, PETSC_SECTION_CLASSID, 3);
29683e03ebd8SStefano Zampini   PetscValidHeaderSpecific(clPoints, IS_CLASSID, 4);
2969d4a1ad33SMatthew G. Knepley   if (section->clObj != obj) PetscCall(PetscSectionResetClosurePermutation(section));
2970ea844a1aSMatthew Knepley   section->clObj = obj;
29719566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)clSection));
29729566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)clPoints));
29739566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&section->clSection));
29749566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&section->clPoints));
2975ea844a1aSMatthew Knepley   section->clSection = clSection;
2976ea844a1aSMatthew Knepley   section->clPoints  = clPoints;
29773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2978ea844a1aSMatthew Knepley }
2979ea844a1aSMatthew Knepley 
2980ea844a1aSMatthew Knepley /*@
2981cab54364SBarry Smith   PetscSectionGetClosureIndex - Get the cache of points in the closure of each point in the section set with `PetscSectionSetClosureIndex()`
2982ea844a1aSMatthew Knepley 
298340750d41SVaclav Hapla   Collective
2984ea844a1aSMatthew Knepley 
2985ea844a1aSMatthew Knepley   Input Parameters:
2986cab54364SBarry Smith + section - The `PetscSection`
2987cab54364SBarry Smith - obj     - A `PetscObject` which serves as the key for this index
2988ea844a1aSMatthew Knepley 
2989ea844a1aSMatthew Knepley   Output Parameters:
2990cab54364SBarry Smith + clSection - `PetscSection` giving the size of the closure of each point
2991cab54364SBarry Smith - clPoints  - `IS` giving the points in each closure
2992ea844a1aSMatthew Knepley 
2993ea844a1aSMatthew Knepley   Level: advanced
2994ea844a1aSMatthew Knepley 
2995cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSetClosureIndex()`, `DMPlexCreateClosureIndex()`
2996ea844a1aSMatthew Knepley @*/
2997d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetClosureIndex(PetscSection section, PetscObject obj, PetscSection *clSection, IS *clPoints)
2998d71ae5a4SJacob Faibussowitsch {
2999ea844a1aSMatthew Knepley   PetscFunctionBegin;
3000ea844a1aSMatthew Knepley   if (section->clObj == obj) {
3001ea844a1aSMatthew Knepley     if (clSection) *clSection = section->clSection;
3002ea844a1aSMatthew Knepley     if (clPoints) *clPoints = section->clPoints;
3003ea844a1aSMatthew Knepley   } else {
3004ea844a1aSMatthew Knepley     if (clSection) *clSection = NULL;
3005ea844a1aSMatthew Knepley     if (clPoints) *clPoints = NULL;
3006ea844a1aSMatthew Knepley   }
30073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3008ea844a1aSMatthew Knepley }
3009ea844a1aSMatthew Knepley 
3010d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetClosurePermutation_Internal(PetscSection section, PetscObject obj, PetscInt depth, PetscInt clSize, PetscCopyMode mode, PetscInt *clPerm)
3011d71ae5a4SJacob Faibussowitsch {
3012ea844a1aSMatthew Knepley   PetscInt                    i;
3013c459fbc1SJed Brown   khiter_t                    iter;
3014c459fbc1SJed Brown   int                         new_entry;
3015c459fbc1SJed Brown   PetscSectionClosurePermKey  key = {depth, clSize};
3016c459fbc1SJed Brown   PetscSectionClosurePermVal *val;
3017ea844a1aSMatthew Knepley 
3018ea844a1aSMatthew Knepley   PetscFunctionBegin;
3019ea844a1aSMatthew Knepley   if (section->clObj != obj) {
30209566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&section->clSection));
30219566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&section->clPoints));
3022ea844a1aSMatthew Knepley   }
3023ea844a1aSMatthew Knepley   section->clObj = obj;
30249566063dSJacob Faibussowitsch   if (!section->clHash) PetscCall(PetscClPermCreate(&section->clHash));
3025c459fbc1SJed Brown   iter = kh_put(ClPerm, section->clHash, key, &new_entry);
3026c459fbc1SJed Brown   val  = &kh_val(section->clHash, iter);
3027c459fbc1SJed Brown   if (!new_entry) {
30289566063dSJacob Faibussowitsch     PetscCall(PetscFree(val->perm));
30299566063dSJacob Faibussowitsch     PetscCall(PetscFree(val->invPerm));
3030c459fbc1SJed Brown   }
3031ea844a1aSMatthew Knepley   if (mode == PETSC_COPY_VALUES) {
30329566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(clSize, &val->perm));
30339566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(val->perm, clPerm, clSize));
3034ea844a1aSMatthew Knepley   } else if (mode == PETSC_OWN_POINTER) {
3035c459fbc1SJed Brown     val->perm = clPerm;
3036ea844a1aSMatthew Knepley   } else SETERRQ(PetscObjectComm(obj), PETSC_ERR_SUP, "Do not support borrowed arrays");
30379566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(clSize, &val->invPerm));
3038c459fbc1SJed Brown   for (i = 0; i < clSize; ++i) val->invPerm[clPerm[i]] = i;
30393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3040ea844a1aSMatthew Knepley }
3041ea844a1aSMatthew Knepley 
3042ea844a1aSMatthew Knepley /*@
3043c459fbc1SJed Brown   PetscSectionSetClosurePermutation - Set the dof permutation for the closure of each cell in the section, meaning clPerm[newIndex] = oldIndex.
3044ea844a1aSMatthew Knepley 
3045ea844a1aSMatthew Knepley   Not Collective
3046ea844a1aSMatthew Knepley 
3047ea844a1aSMatthew Knepley   Input Parameters:
3048cab54364SBarry Smith + section - The `PetscSection`
3049cab54364SBarry Smith . obj     - A `PetscObject` which serves as the key for this index (usually a `DM`)
3050c459fbc1SJed Brown . depth   - Depth of points on which to apply the given permutation
3051ea844a1aSMatthew Knepley - perm    - Permutation of the cell dof closure
3052ea844a1aSMatthew Knepley 
3053cab54364SBarry Smith   Level: intermediate
3054cab54364SBarry Smith 
3055cab54364SBarry Smith   Notes:
3056c459fbc1SJed Brown   The specified permutation will only be applied to points at depth whose closure size matches the length of perm.  In a
3057c459fbc1SJed Brown   mixed-topology or variable-degree finite element space, this function can be called multiple times at each depth for
3058c459fbc1SJed Brown   each topology and degree.
3059c459fbc1SJed Brown 
3060c459fbc1SJed Brown   This approach assumes that (depth, len(perm)) uniquely identifies the desired permutation; this might not be true for
3061c459fbc1SJed Brown   exotic/enriched spaces on mixed topology meshes.
3062ea844a1aSMatthew Knepley 
3063cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `IS`, `PetscSectionGetClosurePermutation()`, `PetscSectionGetClosureIndex()`, `DMPlexCreateClosureIndex()`, `PetscCopyMode`
3064ea844a1aSMatthew Knepley @*/
3065d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetClosurePermutation(PetscSection section, PetscObject obj, PetscInt depth, IS perm)
3066d71ae5a4SJacob Faibussowitsch {
3067ea844a1aSMatthew Knepley   const PetscInt *clPerm = NULL;
3068ea844a1aSMatthew Knepley   PetscInt        clSize = 0;
3069ea844a1aSMatthew Knepley 
3070ea844a1aSMatthew Knepley   PetscFunctionBegin;
3071ea844a1aSMatthew Knepley   if (perm) {
30729566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(perm, &clSize));
30739566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(perm, &clPerm));
3074ea844a1aSMatthew Knepley   }
30759566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetClosurePermutation_Internal(section, obj, depth, clSize, PETSC_COPY_VALUES, (PetscInt *)clPerm));
30769566063dSJacob Faibussowitsch   if (perm) PetscCall(ISRestoreIndices(perm, &clPerm));
30773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3078ea844a1aSMatthew Knepley }
3079ea844a1aSMatthew Knepley 
3080da8c939bSJacob Faibussowitsch static PetscErrorCode PetscSectionGetClosurePermutation_Private(PetscSection section, PetscObject obj, PetscInt depth, PetscInt size, const PetscInt *perm[])
3081d71ae5a4SJacob Faibussowitsch {
3082ea844a1aSMatthew Knepley   PetscFunctionBegin;
3083ea844a1aSMatthew Knepley   if (section->clObj == obj) {
3084c459fbc1SJed Brown     PetscSectionClosurePermKey k = {depth, size};
3085c459fbc1SJed Brown     PetscSectionClosurePermVal v;
3086da8c939bSJacob Faibussowitsch 
30879566063dSJacob Faibussowitsch     PetscCall(PetscClPermGet(section->clHash, k, &v));
3088c459fbc1SJed Brown     if (perm) *perm = v.perm;
3089ea844a1aSMatthew Knepley   } else {
3090ea844a1aSMatthew Knepley     if (perm) *perm = NULL;
3091ea844a1aSMatthew Knepley   }
30923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3093ea844a1aSMatthew Knepley }
3094ea844a1aSMatthew Knepley 
3095ea844a1aSMatthew Knepley /*@
3096ea844a1aSMatthew Knepley   PetscSectionGetClosurePermutation - Get the dof permutation for the closure of each cell in the section, meaning clPerm[newIndex] = oldIndex.
3097ea844a1aSMatthew Knepley 
309840750d41SVaclav Hapla   Not Collective
3099ea844a1aSMatthew Knepley 
3100ea844a1aSMatthew Knepley   Input Parameters:
3101cab54364SBarry Smith + section - The `PetscSection`
3102cab54364SBarry Smith . obj     - A `PetscObject` which serves as the key for this index (usually a DM)
3103c459fbc1SJed Brown . depth   - Depth stratum on which to obtain closure permutation
3104c459fbc1SJed Brown - clSize  - Closure size to be permuted (e.g., may vary with element topology and degree)
3105ea844a1aSMatthew Knepley 
3106ea844a1aSMatthew Knepley   Output Parameter:
3107ea844a1aSMatthew Knepley . perm - The dof closure permutation
3108ea844a1aSMatthew Knepley 
3109ea844a1aSMatthew Knepley   Level: intermediate
3110ea844a1aSMatthew Knepley 
3111cab54364SBarry Smith   Note:
3112cab54364SBarry Smith   The user must destroy the `IS` that is returned.
3113cab54364SBarry Smith 
3114cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `IS`, `PetscSectionSetClosurePermutation()`, `PetscSectionGetClosureInversePermutation()`, `PetscSectionGetClosureIndex()`, `PetscSectionSetClosureIndex()`, `DMPlexCreateClosureIndex()`
3115ea844a1aSMatthew Knepley @*/
3116d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetClosurePermutation(PetscSection section, PetscObject obj, PetscInt depth, PetscInt clSize, IS *perm)
3117d71ae5a4SJacob Faibussowitsch {
3118da8c939bSJacob Faibussowitsch   const PetscInt *clPerm = NULL;
3119ea844a1aSMatthew Knepley 
3120ea844a1aSMatthew Knepley   PetscFunctionBegin;
3121da8c939bSJacob Faibussowitsch   PetscCall(PetscSectionGetClosurePermutation_Private(section, obj, depth, clSize, &clPerm));
31225f82726aSMatthew G. Knepley   PetscCheck(clPerm, PetscObjectComm((PetscObject)obj), PETSC_ERR_ARG_WRONG, "There is no closure permutation associated with this object for depth %" PetscInt_FMT " of size %" PetscInt_FMT, depth, clSize);
31239566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, clSize, clPerm, PETSC_USE_POINTER, perm));
31243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3125ea844a1aSMatthew Knepley }
3126ea844a1aSMatthew Knepley 
3127d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetClosureInversePermutation_Internal(PetscSection section, PetscObject obj, PetscInt depth, PetscInt size, const PetscInt *perm[])
3128d71ae5a4SJacob Faibussowitsch {
3129ea844a1aSMatthew Knepley   PetscFunctionBegin;
3130c459fbc1SJed Brown   if (section->clObj == obj && section->clHash) {
3131c459fbc1SJed Brown     PetscSectionClosurePermKey k = {depth, size};
3132c459fbc1SJed Brown     PetscSectionClosurePermVal v;
31339566063dSJacob Faibussowitsch     PetscCall(PetscClPermGet(section->clHash, k, &v));
3134c459fbc1SJed Brown     if (perm) *perm = v.invPerm;
3135ea844a1aSMatthew Knepley   } else {
3136ea844a1aSMatthew Knepley     if (perm) *perm = NULL;
3137ea844a1aSMatthew Knepley   }
31383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3139ea844a1aSMatthew Knepley }
3140ea844a1aSMatthew Knepley 
3141ea844a1aSMatthew Knepley /*@
3142ea844a1aSMatthew Knepley   PetscSectionGetClosureInversePermutation - Get the inverse dof permutation for the closure of each cell in the section, meaning clPerm[oldIndex] = newIndex.
3143ea844a1aSMatthew Knepley 
314440750d41SVaclav Hapla   Not Collective
3145ea844a1aSMatthew Knepley 
3146ea844a1aSMatthew Knepley   Input Parameters:
3147cab54364SBarry Smith + section - The `PetscSection`
3148cab54364SBarry Smith . obj     - A `PetscObject` which serves as the key for this index (usually a `DM`)
3149c459fbc1SJed Brown . depth   - Depth stratum on which to obtain closure permutation
3150c459fbc1SJed Brown - clSize  - Closure size to be permuted (e.g., may vary with element topology and degree)
3151ea844a1aSMatthew Knepley 
31522fe279fdSBarry Smith   Output Parameter:
3153c459fbc1SJed Brown . perm - The dof closure permutation
3154ea844a1aSMatthew Knepley 
3155ea844a1aSMatthew Knepley   Level: intermediate
3156ea844a1aSMatthew Knepley 
3157cab54364SBarry Smith   Note:
3158cab54364SBarry Smith   The user must destroy the `IS` that is returned.
3159cab54364SBarry Smith 
3160cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `IS`, `PetscSectionSetClosurePermutation()`, `PetscSectionGetClosureIndex()`, `PetscSectionSetClosureIndex()`, `DMPlexCreateClosureIndex()`
3161ea844a1aSMatthew Knepley @*/
3162d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetClosureInversePermutation(PetscSection section, PetscObject obj, PetscInt depth, PetscInt clSize, IS *perm)
3163d71ae5a4SJacob Faibussowitsch {
3164da8c939bSJacob Faibussowitsch   const PetscInt *clPerm = NULL;
3165ea844a1aSMatthew Knepley 
3166ea844a1aSMatthew Knepley   PetscFunctionBegin;
31679566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetClosureInversePermutation_Internal(section, obj, depth, clSize, &clPerm));
31689566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, clSize, clPerm, PETSC_USE_POINTER, perm));
31693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3170ea844a1aSMatthew Knepley }
3171ea844a1aSMatthew Knepley 
3172ea844a1aSMatthew Knepley /*@
3173583308b6SBarry Smith   PetscSectionGetField - Get the `PetscSection` associated with a single field
3174ea844a1aSMatthew Knepley 
3175ea844a1aSMatthew Knepley   Input Parameters:
3176cab54364SBarry Smith + s     - The `PetscSection`
3177ea844a1aSMatthew Knepley - field - The field number
3178ea844a1aSMatthew Knepley 
3179ea844a1aSMatthew Knepley   Output Parameter:
3180583308b6SBarry Smith . subs - The `PetscSection` for the given field, note the chart of `subs` is not set
3181ea844a1aSMatthew Knepley 
3182ea844a1aSMatthew Knepley   Level: intermediate
3183ea844a1aSMatthew Knepley 
3184cab54364SBarry Smith   Note:
3185cab54364SBarry Smith   Does not increase the reference count of the selected sub-section. There is no matching `PetscSectionRestoreField()`
3186cab54364SBarry Smith 
3187cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `IS`, `PetscSectionSetNumFields()`
3188ea844a1aSMatthew Knepley @*/
3189d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetField(PetscSection s, PetscInt field, PetscSection *subs)
3190d71ae5a4SJacob Faibussowitsch {
3191ea844a1aSMatthew Knepley   PetscFunctionBegin;
3192ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
31934f572ea9SToby Isaac   PetscAssertPointer(subs, 3);
31942abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
3195ea844a1aSMatthew Knepley   *subs = s->field[field];
31963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3197ea844a1aSMatthew Knepley }
3198ea844a1aSMatthew Knepley 
3199ea844a1aSMatthew Knepley PetscClassId      PETSC_SECTION_SYM_CLASSID;
3200ea844a1aSMatthew Knepley PetscFunctionList PetscSectionSymList = NULL;
3201ea844a1aSMatthew Knepley 
3202ea844a1aSMatthew Knepley /*@
3203cab54364SBarry Smith   PetscSectionSymCreate - Creates an empty `PetscSectionSym` object.
3204ea844a1aSMatthew Knepley 
3205ea844a1aSMatthew Knepley   Collective
3206ea844a1aSMatthew Knepley 
3207ea844a1aSMatthew Knepley   Input Parameter:
3208ea844a1aSMatthew Knepley . comm - the MPI communicator
3209ea844a1aSMatthew Knepley 
3210ea844a1aSMatthew Knepley   Output Parameter:
3211ea844a1aSMatthew Knepley . sym - pointer to the new set of symmetries
3212ea844a1aSMatthew Knepley 
3213ea844a1aSMatthew Knepley   Level: developer
3214ea844a1aSMatthew Knepley 
3215cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSection`, `PetscSectionSym`, `PetscSectionSymDestroy()`
3216ea844a1aSMatthew Knepley @*/
3217d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymCreate(MPI_Comm comm, PetscSectionSym *sym)
3218d71ae5a4SJacob Faibussowitsch {
3219ea844a1aSMatthew Knepley   PetscFunctionBegin;
32204f572ea9SToby Isaac   PetscAssertPointer(sym, 2);
32219566063dSJacob Faibussowitsch   PetscCall(ISInitializePackage());
32229566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(*sym, PETSC_SECTION_SYM_CLASSID, "PetscSectionSym", "Section Symmetry", "IS", comm, PetscSectionSymDestroy, PetscSectionSymView));
32233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3224ea844a1aSMatthew Knepley }
3225ea844a1aSMatthew Knepley 
3226ea844a1aSMatthew Knepley /*@C
3227cab54364SBarry Smith   PetscSectionSymSetType - Builds a `PetscSectionSym`, for a particular implementation.
3228ea844a1aSMatthew Knepley 
322940750d41SVaclav Hapla   Collective
3230ea844a1aSMatthew Knepley 
3231ea844a1aSMatthew Knepley   Input Parameters:
3232ea844a1aSMatthew Knepley + sym    - The section symmetry object
3233ea844a1aSMatthew Knepley - method - The name of the section symmetry type
3234ea844a1aSMatthew Knepley 
3235ea844a1aSMatthew Knepley   Level: developer
3236ea844a1aSMatthew Knepley 
3237cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionSymType`, `PetscSectionSymGetType()`, `PetscSectionSymCreate()`
3238ea844a1aSMatthew Knepley @*/
3239d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymSetType(PetscSectionSym sym, PetscSectionSymType method)
3240d71ae5a4SJacob Faibussowitsch {
3241ea844a1aSMatthew Knepley   PetscErrorCode (*r)(PetscSectionSym);
3242ea844a1aSMatthew Knepley   PetscBool match;
3243ea844a1aSMatthew Knepley 
3244ea844a1aSMatthew Knepley   PetscFunctionBegin;
3245ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(sym, PETSC_SECTION_SYM_CLASSID, 1);
32469566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)sym, method, &match));
32473ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
3248ea844a1aSMatthew Knepley 
32499566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(PetscSectionSymList, method, &r));
32506adde796SStefano Zampini   PetscCheck(r, PetscObjectComm((PetscObject)sym), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscSectionSym type: %s", method);
3251dbbe0bcdSBarry Smith   PetscTryTypeMethod(sym, destroy);
3252ea844a1aSMatthew Knepley   sym->ops->destroy = NULL;
3253dbbe0bcdSBarry Smith 
32549566063dSJacob Faibussowitsch   PetscCall((*r)(sym));
32559566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)sym, method));
32563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3257ea844a1aSMatthew Knepley }
3258ea844a1aSMatthew Knepley 
3259ea844a1aSMatthew Knepley /*@C
3260cab54364SBarry Smith   PetscSectionSymGetType - Gets the section symmetry type name (as a string) from the `PetscSectionSym`.
3261ea844a1aSMatthew Knepley 
3262ea844a1aSMatthew Knepley   Not Collective
3263ea844a1aSMatthew Knepley 
3264ea844a1aSMatthew Knepley   Input Parameter:
3265ea844a1aSMatthew Knepley . sym - The section symmetry
3266ea844a1aSMatthew Knepley 
3267ea844a1aSMatthew Knepley   Output Parameter:
3268ea844a1aSMatthew Knepley . type - The index set type name
3269ea844a1aSMatthew Knepley 
3270ea844a1aSMatthew Knepley   Level: developer
3271ea844a1aSMatthew Knepley 
3272cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionSymType`, `PetscSectionSymSetType()`, `PetscSectionSymCreate()`
3273ea844a1aSMatthew Knepley @*/
3274d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymGetType(PetscSectionSym sym, PetscSectionSymType *type)
3275d71ae5a4SJacob Faibussowitsch {
3276ea844a1aSMatthew Knepley   PetscFunctionBegin;
3277ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(sym, PETSC_SECTION_SYM_CLASSID, 1);
32784f572ea9SToby Isaac   PetscAssertPointer(type, 2);
3279ea844a1aSMatthew Knepley   *type = ((PetscObject)sym)->type_name;
32803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3281ea844a1aSMatthew Knepley }
3282ea844a1aSMatthew Knepley 
3283ea844a1aSMatthew Knepley /*@C
3284cab54364SBarry Smith   PetscSectionSymRegister - Registers a new section symmetry implementation
3285ea844a1aSMatthew Knepley 
3286ea844a1aSMatthew Knepley   Not Collective
3287ea844a1aSMatthew Knepley 
3288ea844a1aSMatthew Knepley   Input Parameters:
32892fe279fdSBarry Smith + sname    - The name of a new user-defined creation routine
32902fe279fdSBarry Smith - function - The creation routine itself
3291ea844a1aSMatthew Knepley 
3292ea844a1aSMatthew Knepley   Level: developer
3293ea844a1aSMatthew Knepley 
3294cab54364SBarry Smith   Notes:
3295cab54364SBarry Smith   `PetscSectionSymRegister()` may be called multiple times to add several user-defined vectors
3296cab54364SBarry Smith 
3297cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionSymType`, `PetscSectionSymCreate()`, `PetscSectionSymSetType()`
3298ea844a1aSMatthew Knepley @*/
3299d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymRegister(const char sname[], PetscErrorCode (*function)(PetscSectionSym))
3300d71ae5a4SJacob Faibussowitsch {
3301ea844a1aSMatthew Knepley   PetscFunctionBegin;
33029566063dSJacob Faibussowitsch   PetscCall(ISInitializePackage());
33039566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&PetscSectionSymList, sname, function));
33043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3305ea844a1aSMatthew Knepley }
3306ea844a1aSMatthew Knepley 
3307ea844a1aSMatthew Knepley /*@
3308ea844a1aSMatthew Knepley   PetscSectionSymDestroy - Destroys a section symmetry.
3309ea844a1aSMatthew Knepley 
331040750d41SVaclav Hapla   Collective
3311ea844a1aSMatthew Knepley 
33122fe279fdSBarry Smith   Input Parameter:
3313ea844a1aSMatthew Knepley . sym - the section symmetry
3314ea844a1aSMatthew Knepley 
3315ea844a1aSMatthew Knepley   Level: developer
3316ea844a1aSMatthew Knepley 
331742747ad1SJacob Faibussowitsch .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionSymCreate()`
3318ea844a1aSMatthew Knepley @*/
3319d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymDestroy(PetscSectionSym *sym)
3320d71ae5a4SJacob Faibussowitsch {
3321ea844a1aSMatthew Knepley   SymWorkLink link, next;
3322ea844a1aSMatthew Knepley 
3323ea844a1aSMatthew Knepley   PetscFunctionBegin;
33243ba16761SJacob Faibussowitsch   if (!*sym) PetscFunctionReturn(PETSC_SUCCESS);
3325ea844a1aSMatthew Knepley   PetscValidHeaderSpecific((*sym), PETSC_SECTION_SYM_CLASSID, 1);
33269371c9d4SSatish Balay   if (--((PetscObject)(*sym))->refct > 0) {
33279371c9d4SSatish Balay     *sym = NULL;
33283ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
3329ea844a1aSMatthew Knepley   }
3330213acdd3SPierre Jolivet   PetscTryTypeMethod(*sym, destroy);
3331c9cc58a2SBarry Smith   PetscCheck(!(*sym)->workout, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Work array still checked out");
3332ea844a1aSMatthew Knepley   for (link = (*sym)->workin; link; link = next) {
33330c99d500SBarry Smith     PetscInt    **perms = (PetscInt **)link->perms;
33340c99d500SBarry Smith     PetscScalar **rots  = (PetscScalar **)link->rots;
33350c99d500SBarry Smith     PetscCall(PetscFree2(perms, rots));
3336ea844a1aSMatthew Knepley     next = link->next;
33379566063dSJacob Faibussowitsch     PetscCall(PetscFree(link));
3338ea844a1aSMatthew Knepley   }
3339ea844a1aSMatthew Knepley   (*sym)->workin = NULL;
33409566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(sym));
33413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3342ea844a1aSMatthew Knepley }
3343ea844a1aSMatthew Knepley 
3344ea844a1aSMatthew Knepley /*@C
3345ea844a1aSMatthew Knepley   PetscSectionSymView - Displays a section symmetry
3346ea844a1aSMatthew Knepley 
334740750d41SVaclav Hapla   Collective
3348ea844a1aSMatthew Knepley 
3349ea844a1aSMatthew Knepley   Input Parameters:
3350ea844a1aSMatthew Knepley + sym    - the index set
3351cab54364SBarry Smith - viewer - viewer used to display the set, for example `PETSC_VIEWER_STDOUT_SELF`.
3352ea844a1aSMatthew Knepley 
3353ea844a1aSMatthew Knepley   Level: developer
3354ea844a1aSMatthew Knepley 
3355cab54364SBarry Smith .seealso: `PetscSectionSym`, `PetscViewer`, `PetscViewerASCIIOpen()`
3356ea844a1aSMatthew Knepley @*/
3357d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymView(PetscSectionSym sym, PetscViewer viewer)
3358d71ae5a4SJacob Faibussowitsch {
3359ea844a1aSMatthew Knepley   PetscFunctionBegin;
3360ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(sym, PETSC_SECTION_SYM_CLASSID, 1);
336148a46eb9SPierre Jolivet   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)sym), &viewer));
3362ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
3363ea844a1aSMatthew Knepley   PetscCheckSameComm(sym, 1, viewer, 2);
33649566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)sym, viewer));
3365dbbe0bcdSBarry Smith   PetscTryTypeMethod(sym, view, viewer);
33663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3367ea844a1aSMatthew Knepley }
3368ea844a1aSMatthew Knepley 
3369ea844a1aSMatthew Knepley /*@
3370ea844a1aSMatthew Knepley   PetscSectionSetSym - Set the symmetries for the data referred to by the section
3371ea844a1aSMatthew Knepley 
337240750d41SVaclav Hapla   Collective
3373ea844a1aSMatthew Knepley 
3374ea844a1aSMatthew Knepley   Input Parameters:
3375ea844a1aSMatthew Knepley + section - the section describing data layout
3376ea844a1aSMatthew Knepley - sym     - the symmetry describing the affect of orientation on the access of the data
3377ea844a1aSMatthew Knepley 
3378ea844a1aSMatthew Knepley   Level: developer
3379ea844a1aSMatthew Knepley 
3380cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionGetSym()`, `PetscSectionSymCreate()`
3381ea844a1aSMatthew Knepley @*/
3382d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetSym(PetscSection section, PetscSectionSym sym)
3383d71ae5a4SJacob Faibussowitsch {
3384ea844a1aSMatthew Knepley   PetscFunctionBegin;
3385ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
33869566063dSJacob Faibussowitsch   PetscCall(PetscSectionSymDestroy(&(section->sym)));
3387ea844a1aSMatthew Knepley   if (sym) {
3388ea844a1aSMatthew Knepley     PetscValidHeaderSpecific(sym, PETSC_SECTION_SYM_CLASSID, 2);
3389ea844a1aSMatthew Knepley     PetscCheckSameComm(section, 1, sym, 2);
33909566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)sym));
3391ea844a1aSMatthew Knepley   }
3392ea844a1aSMatthew Knepley   section->sym = sym;
33933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3394ea844a1aSMatthew Knepley }
3395ea844a1aSMatthew Knepley 
3396ea844a1aSMatthew Knepley /*@
3397ea844a1aSMatthew Knepley   PetscSectionGetSym - Get the symmetries for the data referred to by the section
3398ea844a1aSMatthew Knepley 
339940750d41SVaclav Hapla   Not Collective
3400ea844a1aSMatthew Knepley 
34012fe279fdSBarry Smith   Input Parameter:
3402ea844a1aSMatthew Knepley . section - the section describing data layout
3403ea844a1aSMatthew Knepley 
34042fe279fdSBarry Smith   Output Parameter:
340520662ed9SBarry Smith . sym - the symmetry describing the affect of orientation on the access of the data, provided previously by `PetscSectionSetSym()`
3406ea844a1aSMatthew Knepley 
3407ea844a1aSMatthew Knepley   Level: developer
3408ea844a1aSMatthew Knepley 
3409cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionSetSym()`, `PetscSectionSymCreate()`
3410ea844a1aSMatthew Knepley @*/
3411d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetSym(PetscSection section, PetscSectionSym *sym)
3412d71ae5a4SJacob Faibussowitsch {
3413ea844a1aSMatthew Knepley   PetscFunctionBegin;
3414ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
3415ea844a1aSMatthew Knepley   *sym = section->sym;
34163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3417ea844a1aSMatthew Knepley }
3418ea844a1aSMatthew Knepley 
3419ea844a1aSMatthew Knepley /*@
3420ea844a1aSMatthew Knepley   PetscSectionSetFieldSym - Set the symmetries for the data referred to by a field of the section
3421ea844a1aSMatthew Knepley 
342240750d41SVaclav Hapla   Collective
3423ea844a1aSMatthew Knepley 
3424ea844a1aSMatthew Knepley   Input Parameters:
3425ea844a1aSMatthew Knepley + section - the section describing data layout
3426ea844a1aSMatthew Knepley . field   - the field number
3427ea844a1aSMatthew Knepley - sym     - the symmetry describing the affect of orientation on the access of the data
3428ea844a1aSMatthew Knepley 
3429ea844a1aSMatthew Knepley   Level: developer
3430ea844a1aSMatthew Knepley 
3431cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionGetFieldSym()`, `PetscSectionSymCreate()`
3432ea844a1aSMatthew Knepley @*/
3433d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFieldSym(PetscSection section, PetscInt field, PetscSectionSym sym)
3434d71ae5a4SJacob Faibussowitsch {
3435ea844a1aSMatthew Knepley   PetscFunctionBegin;
3436ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
34372abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, section->numFields);
34389566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetSym(section->field[field], sym));
34393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3440ea844a1aSMatthew Knepley }
3441ea844a1aSMatthew Knepley 
3442ea844a1aSMatthew Knepley /*@
3443ea844a1aSMatthew Knepley   PetscSectionGetFieldSym - Get the symmetries for the data referred to by a field of the section
3444ea844a1aSMatthew Knepley 
344540750d41SVaclav Hapla   Collective
3446ea844a1aSMatthew Knepley 
3447ea844a1aSMatthew Knepley   Input Parameters:
3448ea844a1aSMatthew Knepley + section - the section describing data layout
3449ea844a1aSMatthew Knepley - field   - the field number
3450ea844a1aSMatthew Knepley 
34512fe279fdSBarry Smith   Output Parameter:
3452ea844a1aSMatthew Knepley . sym - the symmetry describing the affect of orientation on the access of the data
3453ea844a1aSMatthew Knepley 
3454ea844a1aSMatthew Knepley   Level: developer
3455ea844a1aSMatthew Knepley 
3456cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionSetFieldSym()`, `PetscSectionSymCreate()`
3457ea844a1aSMatthew Knepley @*/
3458d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldSym(PetscSection section, PetscInt field, PetscSectionSym *sym)
3459d71ae5a4SJacob Faibussowitsch {
3460ea844a1aSMatthew Knepley   PetscFunctionBegin;
3461ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
34622abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, section->numFields);
3463ea844a1aSMatthew Knepley   *sym = section->field[field]->sym;
34643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3465ea844a1aSMatthew Knepley }
3466ea844a1aSMatthew Knepley 
3467ea844a1aSMatthew Knepley /*@C
3468cab54364SBarry Smith   PetscSectionGetPointSyms - Get the symmetries for a set of points in a `PetscSection` under specific orientations.
3469ea844a1aSMatthew Knepley 
347040750d41SVaclav Hapla   Not Collective
3471ea844a1aSMatthew Knepley 
3472ea844a1aSMatthew Knepley   Input Parameters:
3473ea844a1aSMatthew Knepley + section   - the section
3474ea844a1aSMatthew Knepley . numPoints - the number of points
347520662ed9SBarry Smith - points    - an array of size 2 * `numPoints`, containing a list of (point, orientation) pairs. (An orientation is an
347620662ed9SBarry Smith     arbitrary integer: its interpretation is up to sym.  Orientations are used by `DM`: for their interpretation in that
347720662ed9SBarry Smith     context, see `DMPlexGetConeOrientation()`).
3478ea844a1aSMatthew Knepley 
3479d8d19677SJose E. Roman   Output Parameters:
348020662ed9SBarry Smith + perms - The permutations for the given orientations (or `NULL` if there is no symmetry or the permutation is the identity).
348120662ed9SBarry Smith - rots  - The field rotations symmetries for the given orientations (or `NULL` if there is no symmetry or the rotations are all
3482ea844a1aSMatthew Knepley     identity).
3483ea844a1aSMatthew Knepley 
3484ea844a1aSMatthew Knepley   Example of usage, gathering dofs into a local array (lArray) from a section array (sArray):
3485ea844a1aSMatthew Knepley .vb
3486ea844a1aSMatthew Knepley      const PetscInt    **perms;
3487ea844a1aSMatthew Knepley      const PetscScalar **rots;
3488ea844a1aSMatthew Knepley      PetscInt            lOffset;
3489ea844a1aSMatthew Knepley 
3490ea844a1aSMatthew Knepley      PetscSectionGetPointSyms(section,numPoints,points,&perms,&rots);
3491ea844a1aSMatthew Knepley      for (i = 0, lOffset = 0; i < numPoints; i++) {
3492ea844a1aSMatthew Knepley        PetscInt           point = points[2*i], dof, sOffset;
3493ea844a1aSMatthew Knepley        const PetscInt    *perm  = perms ? perms[i] : NULL;
3494ea844a1aSMatthew Knepley        const PetscScalar *rot   = rots  ? rots[i]  : NULL;
3495ea844a1aSMatthew Knepley 
3496ea844a1aSMatthew Knepley        PetscSectionGetDof(section,point,&dof);
3497ea844a1aSMatthew Knepley        PetscSectionGetOffset(section,point,&sOffset);
3498ea844a1aSMatthew Knepley 
3499ea844a1aSMatthew Knepley        if (perm) {for (j = 0; j < dof; j++) {lArray[lOffset + perm[j]]  = sArray[sOffset + j];}}
3500ea844a1aSMatthew Knepley        else      {for (j = 0; j < dof; j++) {lArray[lOffset +      j ]  = sArray[sOffset + j];}}
3501ea844a1aSMatthew Knepley        if (rot)  {for (j = 0; j < dof; j++) {lArray[lOffset +      j ] *= rot[j];             }}
3502ea844a1aSMatthew Knepley        lOffset += dof;
3503ea844a1aSMatthew Knepley      }
3504ea844a1aSMatthew Knepley      PetscSectionRestorePointSyms(section,numPoints,points,&perms,&rots);
3505ea844a1aSMatthew Knepley .ve
3506ea844a1aSMatthew Knepley 
3507ea844a1aSMatthew Knepley   Example of usage, adding dofs into a section array (sArray) from a local array (lArray):
3508ea844a1aSMatthew Knepley .vb
3509ea844a1aSMatthew Knepley      const PetscInt    **perms;
3510ea844a1aSMatthew Knepley      const PetscScalar **rots;
3511ea844a1aSMatthew Knepley      PetscInt            lOffset;
3512ea844a1aSMatthew Knepley 
3513ea844a1aSMatthew Knepley      PetscSectionGetPointSyms(section,numPoints,points,&perms,&rots);
3514ea844a1aSMatthew Knepley      for (i = 0, lOffset = 0; i < numPoints; i++) {
3515ea844a1aSMatthew Knepley        PetscInt           point = points[2*i], dof, sOffset;
3516ea844a1aSMatthew Knepley        const PetscInt    *perm  = perms ? perms[i] : NULL;
3517ea844a1aSMatthew Knepley        const PetscScalar *rot   = rots  ? rots[i]  : NULL;
3518ea844a1aSMatthew Knepley 
3519ea844a1aSMatthew Knepley        PetscSectionGetDof(section,point,&dof);
3520ea844a1aSMatthew Knepley        PetscSectionGetOffset(section,point,&sOff);
3521ea844a1aSMatthew Knepley 
3522ea844a1aSMatthew Knepley        if (perm) {for (j = 0; j < dof; j++) {sArray[sOffset + j] += lArray[lOffset + perm[j]] * (rot ? PetscConj(rot[perm[j]]) : 1.);}}
3523ea844a1aSMatthew Knepley        else      {for (j = 0; j < dof; j++) {sArray[sOffset + j] += lArray[lOffset +      j ] * (rot ? PetscConj(rot[     j ]) : 1.);}}
3524ea844a1aSMatthew Knepley        offset += dof;
3525ea844a1aSMatthew Knepley      }
3526ea844a1aSMatthew Knepley      PetscSectionRestorePointSyms(section,numPoints,points,&perms,&rots);
3527ea844a1aSMatthew Knepley .ve
3528ea844a1aSMatthew Knepley 
3529ea844a1aSMatthew Knepley   Level: developer
3530ea844a1aSMatthew Knepley 
3531583308b6SBarry Smith   Notes:
353220662ed9SBarry Smith   `PetscSectionSetSym()` must have been previously called to provide the symmetries to the `PetscSection`
353320662ed9SBarry Smith 
353420662ed9SBarry Smith   Use `PetscSectionRestorePointSyms()` when finished with the data
353520662ed9SBarry Smith 
3536cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionRestorePointSyms()`, `PetscSectionSymCreate()`, `PetscSectionSetSym()`, `PetscSectionGetSym()`
3537ea844a1aSMatthew Knepley @*/
3538d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetPointSyms(PetscSection section, PetscInt numPoints, const PetscInt *points, const PetscInt ***perms, const PetscScalar ***rots)
3539d71ae5a4SJacob Faibussowitsch {
3540ea844a1aSMatthew Knepley   PetscSectionSym sym;
3541ea844a1aSMatthew Knepley 
3542ea844a1aSMatthew Knepley   PetscFunctionBegin;
3543ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
35444f572ea9SToby Isaac   if (numPoints) PetscAssertPointer(points, 3);
3545ea844a1aSMatthew Knepley   if (perms) *perms = NULL;
3546ea844a1aSMatthew Knepley   if (rots) *rots = NULL;
3547ea844a1aSMatthew Knepley   sym = section->sym;
3548ea844a1aSMatthew Knepley   if (sym && (perms || rots)) {
3549ea844a1aSMatthew Knepley     SymWorkLink link;
3550ea844a1aSMatthew Knepley 
3551ea844a1aSMatthew Knepley     if (sym->workin) {
3552ea844a1aSMatthew Knepley       link        = sym->workin;
3553ea844a1aSMatthew Knepley       sym->workin = sym->workin->next;
3554ea844a1aSMatthew Knepley     } else {
35554dfa11a4SJacob Faibussowitsch       PetscCall(PetscNew(&link));
3556ea844a1aSMatthew Knepley     }
3557ea844a1aSMatthew Knepley     if (numPoints > link->numPoints) {
35580c99d500SBarry Smith       PetscInt    **perms = (PetscInt **)link->perms;
35590c99d500SBarry Smith       PetscScalar **rots  = (PetscScalar **)link->rots;
35600c99d500SBarry Smith       PetscCall(PetscFree2(perms, rots));
35610c99d500SBarry Smith       PetscCall(PetscMalloc2(numPoints, (PetscInt ***)&link->perms, numPoints, (PetscScalar ***)&link->rots));
3562ea844a1aSMatthew Knepley       link->numPoints = numPoints;
3563ea844a1aSMatthew Knepley     }
3564ea844a1aSMatthew Knepley     link->next   = sym->workout;
3565ea844a1aSMatthew Knepley     sym->workout = link;
35669566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero((PetscInt **)link->perms, numPoints));
35679566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero((PetscInt **)link->rots, numPoints));
35689927e4dfSBarry Smith     PetscUseTypeMethod(sym, getpoints, section, numPoints, points, link->perms, link->rots);
3569ea844a1aSMatthew Knepley     if (perms) *perms = link->perms;
3570ea844a1aSMatthew Knepley     if (rots) *rots = link->rots;
3571ea844a1aSMatthew Knepley   }
35723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3573ea844a1aSMatthew Knepley }
3574ea844a1aSMatthew Knepley 
3575ea844a1aSMatthew Knepley /*@C
3576cab54364SBarry Smith   PetscSectionRestorePointSyms - Restore the symmetries returned by `PetscSectionGetPointSyms()`
3577ea844a1aSMatthew Knepley 
357840750d41SVaclav Hapla   Not Collective
3579ea844a1aSMatthew Knepley 
3580ea844a1aSMatthew Knepley   Input Parameters:
3581ea844a1aSMatthew Knepley + section   - the section
3582ea844a1aSMatthew Knepley . numPoints - the number of points
358338b5cf2dSJacob Faibussowitsch . points    - an array of size 2 * `numPoints`, containing a list of (point, orientation) pairs. (An orientation is an
3584cab54364SBarry Smith     arbitrary integer: its interpretation is up to sym.  Orientations are used by `DM`: for their interpretation in that
3585cab54364SBarry Smith     context, see `DMPlexGetConeOrientation()`).
358620662ed9SBarry Smith . perms     - The permutations for the given orientations: set to `NULL` at conclusion
358720662ed9SBarry Smith - rots      - The field rotations symmetries for the given orientations: set to `NULL` at conclusion
3588ea844a1aSMatthew Knepley 
3589ea844a1aSMatthew Knepley   Level: developer
3590ea844a1aSMatthew Knepley 
3591cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionGetPointSyms()`, `PetscSectionSymCreate()`, `PetscSectionSetSym()`, `PetscSectionGetSym()`
3592ea844a1aSMatthew Knepley @*/
3593d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionRestorePointSyms(PetscSection section, PetscInt numPoints, const PetscInt *points, const PetscInt ***perms, const PetscScalar ***rots)
3594d71ae5a4SJacob Faibussowitsch {
3595ea844a1aSMatthew Knepley   PetscSectionSym sym;
3596ea844a1aSMatthew Knepley 
3597ea844a1aSMatthew Knepley   PetscFunctionBegin;
3598ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
3599ea844a1aSMatthew Knepley   sym = section->sym;
3600ea844a1aSMatthew Knepley   if (sym && (perms || rots)) {
3601ea844a1aSMatthew Knepley     SymWorkLink *p, link;
3602ea844a1aSMatthew Knepley 
3603ea844a1aSMatthew Knepley     for (p = &sym->workout; (link = *p); p = &link->next) {
3604ea844a1aSMatthew Knepley       if ((perms && link->perms == *perms) || (rots && link->rots == *rots)) {
3605ea844a1aSMatthew Knepley         *p          = link->next;
3606ea844a1aSMatthew Knepley         link->next  = sym->workin;
3607ea844a1aSMatthew Knepley         sym->workin = link;
3608ea844a1aSMatthew Knepley         if (perms) *perms = NULL;
3609ea844a1aSMatthew Knepley         if (rots) *rots = NULL;
36103ba16761SJacob Faibussowitsch         PetscFunctionReturn(PETSC_SUCCESS);
3611ea844a1aSMatthew Knepley       }
3612ea844a1aSMatthew Knepley     }
3613ea844a1aSMatthew Knepley     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Array was not checked out");
3614ea844a1aSMatthew Knepley   }
36153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3616ea844a1aSMatthew Knepley }
3617ea844a1aSMatthew Knepley 
3618ea844a1aSMatthew Knepley /*@C
3619cab54364SBarry Smith   PetscSectionGetFieldPointSyms - Get the symmetries for a set of points in a field of a `PetscSection` under specific orientations.
3620ea844a1aSMatthew Knepley 
362140750d41SVaclav Hapla   Not Collective
3622ea844a1aSMatthew Knepley 
3623ea844a1aSMatthew Knepley   Input Parameters:
3624ea844a1aSMatthew Knepley + section   - the section
3625ea844a1aSMatthew Knepley . field     - the field of the section
3626ea844a1aSMatthew Knepley . numPoints - the number of points
362720662ed9SBarry Smith - points    - an array of size 2 * `numPoints`, containing a list of (point, orientation) pairs. (An orientation is an
3628cab54364SBarry Smith     arbitrary integer: its interpretation is up to sym.  Orientations are used by `DM`: for their interpretation in that
3629cab54364SBarry Smith     context, see `DMPlexGetConeOrientation()`).
3630ea844a1aSMatthew Knepley 
3631d8d19677SJose E. Roman   Output Parameters:
363220662ed9SBarry Smith + perms - The permutations for the given orientations (or `NULL` if there is no symmetry or the permutation is the identity).
363320662ed9SBarry Smith - rots  - The field rotations symmetries for the given orientations (or `NULL` if there is no symmetry or the rotations are all
3634ea844a1aSMatthew Knepley     identity).
3635ea844a1aSMatthew Knepley 
3636ea844a1aSMatthew Knepley   Level: developer
3637ea844a1aSMatthew Knepley 
363820662ed9SBarry Smith   Notes:
363920662ed9SBarry Smith   `PetscSectionSetFieldSym()` must have been previously called to provide the symmetries to the `PetscSection`
364020662ed9SBarry Smith 
364120662ed9SBarry Smith   Use `PetscSectionRestoreFieldPointSyms()` when finished with the data
364220662ed9SBarry Smith 
3643cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionGetPointSyms()`, `PetscSectionRestoreFieldPointSyms()`
3644ea844a1aSMatthew Knepley @*/
3645d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldPointSyms(PetscSection section, PetscInt field, PetscInt numPoints, const PetscInt *points, const PetscInt ***perms, const PetscScalar ***rots)
3646d71ae5a4SJacob Faibussowitsch {
3647ea844a1aSMatthew Knepley   PetscFunctionBegin;
3648ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
364908401ef6SPierre 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);
36509566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetPointSyms(section->field[field], numPoints, points, perms, rots));
36513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3652ea844a1aSMatthew Knepley }
3653ea844a1aSMatthew Knepley 
3654ea844a1aSMatthew Knepley /*@C
3655cab54364SBarry Smith   PetscSectionRestoreFieldPointSyms - Restore the symmetries returned by `PetscSectionGetFieldPointSyms()`
3656ea844a1aSMatthew Knepley 
365740750d41SVaclav Hapla   Not Collective
3658ea844a1aSMatthew Knepley 
3659ea844a1aSMatthew Knepley   Input Parameters:
3660ea844a1aSMatthew Knepley + section   - the section
3661ea844a1aSMatthew Knepley . field     - the field number
3662ea844a1aSMatthew Knepley . numPoints - the number of points
366338b5cf2dSJacob Faibussowitsch . points    - an array of size 2 * `numPoints`, containing a list of (point, orientation) pairs. (An orientation is an
3664cab54364SBarry Smith     arbitrary integer: its interpretation is up to sym.  Orientations are used by `DM`: for their interpretation in that
3665cab54364SBarry Smith     context, see `DMPlexGetConeOrientation()`).
366620662ed9SBarry Smith . perms     - The permutations for the given orientations: set to NULL at conclusion
3667ea844a1aSMatthew Knepley - rots      - The field rotations symmetries for the given orientations: set to NULL at conclusion
3668ea844a1aSMatthew Knepley 
3669ea844a1aSMatthew Knepley   Level: developer
3670ea844a1aSMatthew Knepley 
3671cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionRestorePointSyms()`, `petscSectionGetFieldPointSyms()`, `PetscSectionSymCreate()`, `PetscSectionSetSym()`, `PetscSectionGetSym()`
3672ea844a1aSMatthew Knepley @*/
3673d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionRestoreFieldPointSyms(PetscSection section, PetscInt field, PetscInt numPoints, const PetscInt *points, const PetscInt ***perms, const PetscScalar ***rots)
3674d71ae5a4SJacob Faibussowitsch {
3675ea844a1aSMatthew Knepley   PetscFunctionBegin;
3676ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
367708401ef6SPierre 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);
36789566063dSJacob Faibussowitsch   PetscCall(PetscSectionRestorePointSyms(section->field[field], numPoints, points, perms, rots));
36793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3680ea844a1aSMatthew Knepley }
3681ea844a1aSMatthew Knepley 
3682ea844a1aSMatthew Knepley /*@
3683b004864fSMatthew G. Knepley   PetscSectionSymCopy - Copy the symmetries, assuming that the point structure is compatible
3684b004864fSMatthew G. Knepley 
368540750d41SVaclav Hapla   Not Collective
3686b004864fSMatthew G. Knepley 
3687b004864fSMatthew G. Knepley   Input Parameter:
3688cab54364SBarry Smith . sym - the `PetscSectionSym`
3689b004864fSMatthew G. Knepley 
3690b004864fSMatthew G. Knepley   Output Parameter:
3691b004864fSMatthew G. Knepley . nsym - the equivalent symmetries
3692b004864fSMatthew G. Knepley 
3693b004864fSMatthew G. Knepley   Level: developer
3694b004864fSMatthew G. Knepley 
3695cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionSymCreate()`, `PetscSectionSetSym()`, `PetscSectionGetSym()`, `PetscSectionSymLabelSetStratum()`, `PetscSectionGetPointSyms()`
3696b004864fSMatthew G. Knepley @*/
3697d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymCopy(PetscSectionSym sym, PetscSectionSym nsym)
3698d71ae5a4SJacob Faibussowitsch {
3699b004864fSMatthew G. Knepley   PetscFunctionBegin;
3700b004864fSMatthew G. Knepley   PetscValidHeaderSpecific(sym, PETSC_SECTION_SYM_CLASSID, 1);
3701b004864fSMatthew G. Knepley   PetscValidHeaderSpecific(nsym, PETSC_SECTION_SYM_CLASSID, 2);
3702dbbe0bcdSBarry Smith   PetscTryTypeMethod(sym, copy, nsym);
37033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3704b004864fSMatthew G. Knepley }
3705b004864fSMatthew G. Knepley 
3706b004864fSMatthew G. Knepley /*@
3707cab54364SBarry Smith   PetscSectionSymDistribute - Distribute the symmetries in accordance with the input `PetscSF`
3708b004864fSMatthew G. Knepley 
3709b004864fSMatthew G. Knepley   Collective
3710b004864fSMatthew G. Knepley 
3711b004864fSMatthew G. Knepley   Input Parameters:
3712cab54364SBarry Smith + sym         - the `PetscSectionSym`
3713b004864fSMatthew G. Knepley - migrationSF - the distribution map from roots to leaves
3714b004864fSMatthew G. Knepley 
37152fe279fdSBarry Smith   Output Parameter:
3716b004864fSMatthew G. Knepley . dsym - the redistributed symmetries
3717b004864fSMatthew G. Knepley 
3718b004864fSMatthew G. Knepley   Level: developer
3719b004864fSMatthew G. Knepley 
3720cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionSymCreate()`, `PetscSectionSetSym()`, `PetscSectionGetSym()`, `PetscSectionSymLabelSetStratum()`, `PetscSectionGetPointSyms()`
3721b004864fSMatthew G. Knepley @*/
3722d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymDistribute(PetscSectionSym sym, PetscSF migrationSF, PetscSectionSym *dsym)
3723d71ae5a4SJacob Faibussowitsch {
3724b004864fSMatthew G. Knepley   PetscFunctionBegin;
3725b004864fSMatthew G. Knepley   PetscValidHeaderSpecific(sym, PETSC_SECTION_SYM_CLASSID, 1);
3726b004864fSMatthew G. Knepley   PetscValidHeaderSpecific(migrationSF, PETSCSF_CLASSID, 2);
37274f572ea9SToby Isaac   PetscAssertPointer(dsym, 3);
3728dbbe0bcdSBarry Smith   PetscTryTypeMethod(sym, distribute, migrationSF, dsym);
37293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3730b004864fSMatthew G. Knepley }
3731b004864fSMatthew G. Knepley 
3732b004864fSMatthew G. Knepley /*@
373320662ed9SBarry Smith   PetscSectionGetUseFieldOffsets - Get the flag indicating if field offsets are used directly in a global section, rather than just the point offset
3734ea844a1aSMatthew Knepley 
373540750d41SVaclav Hapla   Not Collective
3736ea844a1aSMatthew Knepley 
3737ea844a1aSMatthew Knepley   Input Parameter:
3738cab54364SBarry Smith . s - the global `PetscSection`
3739ea844a1aSMatthew Knepley 
37402fe279fdSBarry Smith   Output Parameter:
3741ea844a1aSMatthew Knepley . flg - the flag
3742ea844a1aSMatthew Knepley 
3743ea844a1aSMatthew Knepley   Level: developer
3744ea844a1aSMatthew Knepley 
3745cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionSetChart()`, `PetscSectionCreate()`
3746ea844a1aSMatthew Knepley @*/
3747d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetUseFieldOffsets(PetscSection s, PetscBool *flg)
3748d71ae5a4SJacob Faibussowitsch {
3749ea844a1aSMatthew Knepley   PetscFunctionBegin;
3750ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
3751ea844a1aSMatthew Knepley   *flg = s->useFieldOff;
37523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3753ea844a1aSMatthew Knepley }
3754ea844a1aSMatthew Knepley 
3755ea844a1aSMatthew Knepley /*@
3756ea844a1aSMatthew Knepley   PetscSectionSetUseFieldOffsets - Set the flag to use field offsets directly in a global section, rather than just the point offset
3757ea844a1aSMatthew Knepley 
375840750d41SVaclav Hapla   Not Collective
3759ea844a1aSMatthew Knepley 
3760ea844a1aSMatthew Knepley   Input Parameters:
3761cab54364SBarry Smith + s   - the global `PetscSection`
3762ea844a1aSMatthew Knepley - flg - the flag
3763ea844a1aSMatthew Knepley 
3764ea844a1aSMatthew Knepley   Level: developer
3765ea844a1aSMatthew Knepley 
3766cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionGetUseFieldOffsets()`, `PetscSectionSetChart()`, `PetscSectionCreate()`
3767ea844a1aSMatthew Knepley @*/
3768d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetUseFieldOffsets(PetscSection s, PetscBool flg)
3769d71ae5a4SJacob Faibussowitsch {
3770ea844a1aSMatthew Knepley   PetscFunctionBegin;
3771ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
3772ea844a1aSMatthew Knepley   s->useFieldOff = flg;
37733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3774ea844a1aSMatthew Knepley }
3775ea844a1aSMatthew Knepley 
3776ea844a1aSMatthew Knepley #define PetscSectionExpandPoints_Loop(TYPE) \
3777a8f51744SPierre Jolivet   do { \
3778ea844a1aSMatthew Knepley     PetscInt i, n, o0, o1, size; \
3779ea844a1aSMatthew Knepley     TYPE    *a0 = (TYPE *)origArray, *a1; \
37809566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(s, &size)); \
37819566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(size, &a1)); \
3782ea844a1aSMatthew Knepley     for (i = 0; i < npoints; i++) { \
37839566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(origSection, points_[i], &o0)); \
37849566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(s, i, &o1)); \
37859566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(s, i, &n)); \
37869566063dSJacob Faibussowitsch       PetscCall(PetscMemcpy(&a1[o1], &a0[o0], n *unitsize)); \
3787ea844a1aSMatthew Knepley     } \
3788ea844a1aSMatthew Knepley     *newArray = (void *)a1; \
3789a8f51744SPierre Jolivet   } while (0)
3790ea844a1aSMatthew Knepley 
3791ea844a1aSMatthew Knepley /*@
3792ea844a1aSMatthew Knepley   PetscSectionExtractDofsFromArray - Extracts elements of an array corresponding to DOFs of specified points.
3793ea844a1aSMatthew Knepley 
379440750d41SVaclav Hapla   Not Collective
3795ea844a1aSMatthew Knepley 
3796ea844a1aSMatthew Knepley   Input Parameters:
3797cab54364SBarry Smith + origSection - the `PetscSection` describing the layout of the array
3798cab54364SBarry Smith . dataType    - `MPI_Datatype` describing the data type of the array (currently only `MPIU_INT`, `MPIU_SCALAR`, `MPIU_REAL`)
379920662ed9SBarry Smith . origArray   - the array; its size must be equal to the storage size of `origSection`
380020662ed9SBarry Smith - points      - `IS` with points to extract; its indices must lie in the chart of `origSection`
3801ea844a1aSMatthew Knepley 
3802ea844a1aSMatthew Knepley   Output Parameters:
3803da81f932SPierre Jolivet + newSection - the new `PetscSection` describing the layout of the new array (with points renumbered 0,1,... but preserving numbers of DOFs)
380420662ed9SBarry Smith - newArray   - the array of the extracted DOFs; its size is the storage size of `newSection`
3805ea844a1aSMatthew Knepley 
3806ea844a1aSMatthew Knepley   Level: developer
3807ea844a1aSMatthew Knepley 
3808cab54364SBarry Smith .seealso: [PetscSection](sec_petscsection), `PetscSectionSym`, `PetscSectionGetChart()`, `PetscSectionGetDof()`, `PetscSectionGetStorageSize()`, `PetscSectionCreate()`
3809ea844a1aSMatthew Knepley @*/
3810d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionExtractDofsFromArray(PetscSection origSection, MPI_Datatype dataType, const void *origArray, IS points, PetscSection *newSection, void *newArray[])
3811d71ae5a4SJacob Faibussowitsch {
3812ea844a1aSMatthew Knepley   PetscSection    s;
3813ea844a1aSMatthew Knepley   const PetscInt *points_;
3814ea844a1aSMatthew Knepley   PetscInt        i, n, npoints, pStart, pEnd;
3815ea844a1aSMatthew Knepley   PetscMPIInt     unitsize;
3816ea844a1aSMatthew Knepley 
3817ea844a1aSMatthew Knepley   PetscFunctionBegin;
3818ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(origSection, PETSC_SECTION_CLASSID, 1);
38194f572ea9SToby Isaac   PetscAssertPointer(origArray, 3);
3820ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(points, IS_CLASSID, 4);
38214f572ea9SToby Isaac   if (newSection) PetscAssertPointer(newSection, 5);
38224f572ea9SToby Isaac   if (newArray) PetscAssertPointer(newArray, 6);
38239566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Type_size(dataType, &unitsize));
38249566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(points, &npoints));
38259566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(points, &points_));
38269566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(origSection, &pStart, &pEnd));
38279566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &s));
38289566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(s, 0, npoints));
3829ea844a1aSMatthew Knepley   for (i = 0; i < npoints; i++) {
3830c9cc58a2SBarry 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);
38319566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(origSection, points_[i], &n));
38329566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(s, i, n));
3833ea844a1aSMatthew Knepley   }
38349566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(s));
3835ea844a1aSMatthew Knepley   if (newArray) {
38369371c9d4SSatish Balay     if (dataType == MPIU_INT) {
38379371c9d4SSatish Balay       PetscSectionExpandPoints_Loop(PetscInt);
38389371c9d4SSatish Balay     } else if (dataType == MPIU_SCALAR) {
38399371c9d4SSatish Balay       PetscSectionExpandPoints_Loop(PetscScalar);
38409371c9d4SSatish Balay     } else if (dataType == MPIU_REAL) {
38419371c9d4SSatish Balay       PetscSectionExpandPoints_Loop(PetscReal);
38429371c9d4SSatish Balay     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "not implemented for this MPI_Datatype");
3843ea844a1aSMatthew Knepley   }
3844ea844a1aSMatthew Knepley   if (newSection) {
3845ea844a1aSMatthew Knepley     *newSection = s;
3846ea844a1aSMatthew Knepley   } else {
38479566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&s));
3848ea844a1aSMatthew Knepley   }
38499566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(points, &points_));
38503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3851ea844a1aSMatthew Knepley }
3852