xref: /petsc/src/vec/is/section/interface/section.c (revision b0c763681978ca2603055a54b85df49478a76f70)
1ea844a1aSMatthew Knepley /*
2ea844a1aSMatthew Knepley    This file contains routines for basic section object implementation.
3ea844a1aSMatthew Knepley */
4ea844a1aSMatthew Knepley 
5ea844a1aSMatthew Knepley #include <petsc/private/sectionimpl.h> /*I  "petscsection.h"   I*/
6ea844a1aSMatthew Knepley #include <petscsf.h>
7ea844a1aSMatthew Knepley 
8ea844a1aSMatthew Knepley PetscClassId PETSC_SECTION_CLASSID;
9ea844a1aSMatthew Knepley 
10ea844a1aSMatthew Knepley /*@
1120662ed9SBarry Smith   PetscSectionCreate - Allocates a `PetscSection` and sets the map contents to the default.
12ea844a1aSMatthew Knepley 
13ea844a1aSMatthew Knepley   Collective
14ea844a1aSMatthew Knepley 
15ea844a1aSMatthew Knepley   Input Parameters:
16ea844a1aSMatthew Knepley + comm - the MPI communicator
17ea844a1aSMatthew Knepley - s    - pointer to the section
18ea844a1aSMatthew Knepley 
19ea844a1aSMatthew Knepley   Level: beginner
20ea844a1aSMatthew Knepley 
21ea844a1aSMatthew Knepley   Notes:
22ea844a1aSMatthew Knepley   Typical calling sequence
23cab54364SBarry Smith .vb
24cab54364SBarry Smith        PetscSectionCreate(MPI_Comm,PetscSection *);!
25cab54364SBarry Smith        PetscSectionSetNumFields(PetscSection, numFields);
26cab54364SBarry Smith        PetscSectionSetChart(PetscSection,low,high);
27cab54364SBarry Smith        PetscSectionSetDof(PetscSection,point,numdof);
28cab54364SBarry Smith        PetscSectionSetUp(PetscSection);
29cab54364SBarry Smith        PetscSectionGetOffset(PetscSection,point,PetscInt *);
30cab54364SBarry Smith        PetscSectionDestroy(PetscSection);
31cab54364SBarry Smith .ve
32ea844a1aSMatthew Knepley 
3335cb6cd3SPierre Jolivet   The `PetscSection` object and methods are intended to be used in the PETSc `Vec` and `Mat` implementations. The indices returned by the `PetscSection` are appropriate for the kind of `Vec` it is associated with. For example, if the vector being indexed is a local vector, we call the section a local section. If the section indexes a global vector, we call it a global section. For parallel vectors, like global vectors, we use negative indices to indicate dofs owned by other processes.
34ea844a1aSMatthew Knepley 
35*b0c76368SJames Wright .seealso: [PetscSection](ch_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   (*s)->pStart              = -1;
45ea844a1aSMatthew Knepley   (*s)->pEnd                = -1;
46ea844a1aSMatthew Knepley   (*s)->perm                = NULL;
47ea844a1aSMatthew Knepley   (*s)->pointMajor          = PETSC_TRUE;
4887e637c6Sksagiyam   (*s)->includesConstraints = PETSC_TRUE;
49ea844a1aSMatthew Knepley   (*s)->atlasDof            = NULL;
50ea844a1aSMatthew Knepley   (*s)->atlasOff            = NULL;
51ea844a1aSMatthew Knepley   (*s)->bc                  = NULL;
52ea844a1aSMatthew Knepley   (*s)->bcIndices           = NULL;
53ea844a1aSMatthew Knepley   (*s)->setup               = PETSC_FALSE;
54ea844a1aSMatthew Knepley   (*s)->numFields           = 0;
55ea844a1aSMatthew Knepley   (*s)->fieldNames          = NULL;
56ea844a1aSMatthew Knepley   (*s)->field               = NULL;
57ea844a1aSMatthew Knepley   (*s)->useFieldOff         = PETSC_FALSE;
58b778fa18SValeria Barra   (*s)->compNames           = NULL;
59ea844a1aSMatthew Knepley   (*s)->clObj               = NULL;
60c459fbc1SJed Brown   (*s)->clHash              = NULL;
61ea844a1aSMatthew Knepley   (*s)->clSection           = NULL;
62ea844a1aSMatthew Knepley   (*s)->clPoints            = NULL;
6369c11d05SVaclav Hapla   PetscCall(PetscSectionInvalidateMaxDof_Internal(*s));
643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
65ea844a1aSMatthew Knepley }
66ea844a1aSMatthew Knepley 
67ea844a1aSMatthew Knepley /*@
68cab54364SBarry Smith   PetscSectionCopy - Creates a shallow (if possible) copy of the `PetscSection`
69ea844a1aSMatthew Knepley 
70ea844a1aSMatthew Knepley   Collective
71ea844a1aSMatthew Knepley 
72ea844a1aSMatthew Knepley   Input Parameter:
73cab54364SBarry Smith . section - the `PetscSection`
74ea844a1aSMatthew Knepley 
75ea844a1aSMatthew Knepley   Output Parameter:
76ea844a1aSMatthew Knepley . newSection - the copy
77ea844a1aSMatthew Knepley 
78ea844a1aSMatthew Knepley   Level: intermediate
79ea844a1aSMatthew Knepley 
8038b5cf2dSJacob Faibussowitsch   Developer Notes:
81cab54364SBarry Smith   What exactly does shallow mean in this context?
82cab54364SBarry Smith 
83*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionDestroy()`
84ea844a1aSMatthew Knepley @*/
85d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCopy(PetscSection section, PetscSection newSection)
86d71ae5a4SJacob Faibussowitsch {
873857f682SStefano Zampini   PetscFunctionBegin;
883857f682SStefano Zampini   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
893857f682SStefano Zampini   PetscValidHeaderSpecific(newSection, PETSC_SECTION_CLASSID, 2);
903857f682SStefano Zampini   PetscCall(PetscSectionCopy_Internal(section, newSection, NULL));
913857f682SStefano Zampini   PetscFunctionReturn(PETSC_SUCCESS);
923857f682SStefano Zampini }
933857f682SStefano Zampini 
943857f682SStefano Zampini PetscErrorCode PetscSectionCopy_Internal(PetscSection section, PetscSection newSection, PetscBT constrained_dofs)
953857f682SStefano Zampini {
96ea844a1aSMatthew Knepley   PetscSectionSym sym;
97ea844a1aSMatthew Knepley   IS              perm;
98b778fa18SValeria Barra   PetscInt        numFields, f, c, pStart, pEnd, p;
99ea844a1aSMatthew Knepley 
100ea844a1aSMatthew Knepley   PetscFunctionBegin;
101ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
102ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(newSection, PETSC_SECTION_CLASSID, 2);
1039566063dSJacob Faibussowitsch   PetscCall(PetscSectionReset(newSection));
1049566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(section, &numFields));
1059566063dSJacob Faibussowitsch   if (numFields) PetscCall(PetscSectionSetNumFields(newSection, numFields));
106ea844a1aSMatthew Knepley   for (f = 0; f < numFields; ++f) {
107b778fa18SValeria Barra     const char *fieldName = NULL, *compName = NULL;
108ea844a1aSMatthew Knepley     PetscInt    numComp = 0;
109ea844a1aSMatthew Knepley 
1109566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
1119566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldName(newSection, f, fieldName));
1129566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(section, f, &numComp));
1139566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(newSection, f, numComp));
114b778fa18SValeria Barra     for (c = 0; c < numComp; ++c) {
1159566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetComponentName(section, f, c, &compName));
1169566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetComponentName(newSection, f, c, compName));
117b778fa18SValeria Barra     }
1189566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldSym(section, f, &sym));
1199566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldSym(newSection, f, sym));
120ea844a1aSMatthew Knepley   }
1219566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
1229566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(newSection, pStart, pEnd));
1239566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetPermutation(section, &perm));
1249566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetPermutation(newSection, perm));
1259566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetSym(section, &sym));
1269566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetSym(newSection, sym));
127ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
128ea844a1aSMatthew Knepley     PetscInt  dof, cdof, fcdof = 0;
1293857f682SStefano Zampini     PetscBool force_constrained = (PetscBool)(constrained_dofs && PetscBTLookup(constrained_dofs, p - pStart));
130ea844a1aSMatthew Knepley 
1319566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(section, p, &dof));
1329566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(newSection, p, dof));
1333857f682SStefano Zampini     if (force_constrained) cdof = dof;
1343857f682SStefano Zampini     else PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
1359566063dSJacob Faibussowitsch     if (cdof) PetscCall(PetscSectionSetConstraintDof(newSection, p, cdof));
136ea844a1aSMatthew Knepley     for (f = 0; f < numFields; ++f) {
1379566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(section, p, f, &dof));
1389566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(newSection, p, f, dof));
139ea844a1aSMatthew Knepley       if (cdof) {
1403857f682SStefano Zampini         if (force_constrained) fcdof = dof;
1413857f682SStefano Zampini         else PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
1429566063dSJacob Faibussowitsch         if (fcdof) PetscCall(PetscSectionSetFieldConstraintDof(newSection, p, f, fcdof));
143ea844a1aSMatthew Knepley       }
144ea844a1aSMatthew Knepley     }
145ea844a1aSMatthew Knepley   }
1469566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(newSection));
147ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
148ea844a1aSMatthew Knepley     PetscInt        off, cdof, fcdof = 0;
149ea844a1aSMatthew Knepley     const PetscInt *cInd;
1503857f682SStefano Zampini     PetscBool       force_constrained = (PetscBool)(constrained_dofs && PetscBTLookup(constrained_dofs, p - pStart));
151ea844a1aSMatthew Knepley 
152ea844a1aSMatthew Knepley     /* Must set offsets in case they do not agree with the prefix sums */
1539566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(section, p, &off));
1549566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetOffset(newSection, p, off));
1553857f682SStefano Zampini     PetscCall(PetscSectionGetConstraintDof(newSection, p, &cdof));
156ea844a1aSMatthew Knepley     if (cdof) {
1573857f682SStefano Zampini       if (force_constrained) cInd = NULL;
1583857f682SStefano Zampini       else PetscCall(PetscSectionGetConstraintIndices(section, p, &cInd));
1599566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetConstraintIndices(newSection, p, cInd));
160ea844a1aSMatthew Knepley       for (f = 0; f < numFields; ++f) {
1613857f682SStefano Zampini         PetscCall(PetscSectionGetFieldOffset(section, p, f, &off));
1623857f682SStefano Zampini         PetscCall(PetscSectionSetFieldOffset(newSection, p, f, off));
1633857f682SStefano Zampini         PetscCall(PetscSectionGetFieldConstraintDof(newSection, p, f, &fcdof));
164ea844a1aSMatthew Knepley         if (fcdof) {
1653857f682SStefano Zampini           if (force_constrained) cInd = NULL;
1663857f682SStefano Zampini           else PetscCall(PetscSectionGetFieldConstraintIndices(section, p, f, &cInd));
1679566063dSJacob Faibussowitsch           PetscCall(PetscSectionSetFieldConstraintIndices(newSection, p, f, cInd));
168ea844a1aSMatthew Knepley         }
169ea844a1aSMatthew Knepley       }
170ea844a1aSMatthew Knepley     }
171ea844a1aSMatthew Knepley   }
1723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
173ea844a1aSMatthew Knepley }
174ea844a1aSMatthew Knepley 
175ea844a1aSMatthew Knepley /*@
176cab54364SBarry Smith   PetscSectionClone - Creates a shallow (if possible) copy of the `PetscSection`
177ea844a1aSMatthew Knepley 
178ea844a1aSMatthew Knepley   Collective
179ea844a1aSMatthew Knepley 
180ea844a1aSMatthew Knepley   Input Parameter:
181cab54364SBarry Smith . section - the `PetscSection`
182ea844a1aSMatthew Knepley 
183ea844a1aSMatthew Knepley   Output Parameter:
184ea844a1aSMatthew Knepley . newSection - the copy
185ea844a1aSMatthew Knepley 
186ea844a1aSMatthew Knepley   Level: beginner
187ea844a1aSMatthew Knepley 
18838b5cf2dSJacob Faibussowitsch   Developer Notes:
189cab54364SBarry Smith   With standard PETSc terminology this should be called `PetscSectionDuplicate()`
190cab54364SBarry Smith 
191*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionDestroy()`, `PetscSectionCopy()`
192ea844a1aSMatthew Knepley @*/
193d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionClone(PetscSection section, PetscSection *newSection)
194d71ae5a4SJacob Faibussowitsch {
195ea844a1aSMatthew Knepley   PetscFunctionBegin;
196ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
1974f572ea9SToby Isaac   PetscAssertPointer(newSection, 2);
1989566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)section), newSection));
1999566063dSJacob Faibussowitsch   PetscCall(PetscSectionCopy(section, *newSection));
2003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
201ea844a1aSMatthew Knepley }
202ea844a1aSMatthew Knepley 
203ea844a1aSMatthew Knepley /*@
204cab54364SBarry Smith   PetscSectionSetFromOptions - sets parameters in a `PetscSection` from the options database
205ea844a1aSMatthew Knepley 
20640750d41SVaclav Hapla   Collective
207ea844a1aSMatthew Knepley 
208ea844a1aSMatthew Knepley   Input Parameter:
20938b5cf2dSJacob Faibussowitsch . s - the `PetscSection`
210ea844a1aSMatthew Knepley 
21120662ed9SBarry Smith   Options Database Key:
212cab54364SBarry Smith . -petscsection_point_major - `PETSC_TRUE` for point-major order
213ea844a1aSMatthew Knepley 
214ea844a1aSMatthew Knepley   Level: intermediate
215ea844a1aSMatthew Knepley 
216*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionDestroy()`
217ea844a1aSMatthew Knepley @*/
218d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFromOptions(PetscSection s)
219d71ae5a4SJacob Faibussowitsch {
220ea844a1aSMatthew Knepley   PetscFunctionBegin;
221ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
222d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)s);
2239566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-petscsection_point_major", "The for ordering, either point major or field major", "PetscSectionSetPointMajor", s->pointMajor, &s->pointMajor, NULL));
224ea844a1aSMatthew Knepley   /* process any options handlers added with PetscObjectAddOptionsHandler() */
225dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)s, PetscOptionsObject));
226d0609cedSBarry Smith   PetscOptionsEnd();
2279566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)s, NULL, "-petscsection_view"));
2283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
229ea844a1aSMatthew Knepley }
230ea844a1aSMatthew Knepley 
231ea844a1aSMatthew Knepley /*@
232ea844a1aSMatthew Knepley   PetscSectionCompare - Compares two sections
233ea844a1aSMatthew Knepley 
23440750d41SVaclav Hapla   Collective
235ea844a1aSMatthew Knepley 
2367a7aea1fSJed Brown   Input Parameters:
237cab54364SBarry Smith + s1 - the first `PetscSection`
238cab54364SBarry Smith - s2 - the second `PetscSection`
239ea844a1aSMatthew Knepley 
240ea844a1aSMatthew Knepley   Output Parameter:
241cab54364SBarry Smith . congruent - `PETSC_TRUE` if the two sections are congruent, `PETSC_FALSE` otherwise
242ea844a1aSMatthew Knepley 
243ea844a1aSMatthew Knepley   Level: intermediate
244ea844a1aSMatthew Knepley 
245cab54364SBarry Smith   Note:
246ea844a1aSMatthew Knepley   Field names are disregarded.
247ea844a1aSMatthew Knepley 
248*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionCopy()`, `PetscSectionClone()`
249ea844a1aSMatthew Knepley @*/
250d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCompare(PetscSection s1, PetscSection s2, PetscBool *congruent)
251d71ae5a4SJacob Faibussowitsch {
252ea844a1aSMatthew Knepley   PetscInt        pStart, pEnd, nfields, ncdof, nfcdof, p, f, n1, n2;
253ea844a1aSMatthew Knepley   const PetscInt *idx1, *idx2;
254ea844a1aSMatthew Knepley   IS              perm1, perm2;
255ea844a1aSMatthew Knepley   PetscBool       flg;
256ea844a1aSMatthew Knepley   PetscMPIInt     mflg;
257ea844a1aSMatthew Knepley 
258ea844a1aSMatthew Knepley   PetscFunctionBegin;
259ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s1, PETSC_SECTION_CLASSID, 1);
260ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s2, PETSC_SECTION_CLASSID, 2);
2614f572ea9SToby Isaac   PetscAssertPointer(congruent, 3);
262ea844a1aSMatthew Knepley   flg = PETSC_FALSE;
263ea844a1aSMatthew Knepley 
2649566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)s1), PetscObjectComm((PetscObject)s2), &mflg));
265ea844a1aSMatthew Knepley   if (mflg != MPI_CONGRUENT && mflg != MPI_IDENT) {
266ea844a1aSMatthew Knepley     *congruent = PETSC_FALSE;
2673ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
268ea844a1aSMatthew Knepley   }
269ea844a1aSMatthew Knepley 
2709566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s1, &pStart, &pEnd));
2719566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s2, &n1, &n2));
272ea844a1aSMatthew Knepley   if (pStart != n1 || pEnd != n2) goto not_congruent;
273ea844a1aSMatthew Knepley 
2749566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetPermutation(s1, &perm1));
2759566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetPermutation(s2, &perm2));
276ea844a1aSMatthew Knepley   if (perm1 && perm2) {
2779566063dSJacob Faibussowitsch     PetscCall(ISEqual(perm1, perm2, congruent));
2784ad8454bSPierre Jolivet     if (!*congruent) goto not_congruent;
279ea844a1aSMatthew Knepley   } else if (perm1 != perm2) goto not_congruent;
280ea844a1aSMatthew Knepley 
281ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
2829566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(s1, p, &n1));
2839566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(s2, p, &n2));
284ea844a1aSMatthew Knepley     if (n1 != n2) goto not_congruent;
285ea844a1aSMatthew Knepley 
2869566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s1, p, &n1));
2879566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s2, p, &n2));
288ea844a1aSMatthew Knepley     if (n1 != n2) goto not_congruent;
289ea844a1aSMatthew Knepley 
2909566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s1, p, &ncdof));
2919566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s2, p, &n2));
292ea844a1aSMatthew Knepley     if (ncdof != n2) goto not_congruent;
293ea844a1aSMatthew Knepley 
2949566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintIndices(s1, p, &idx1));
2959566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintIndices(s2, p, &idx2));
2969566063dSJacob Faibussowitsch     PetscCall(PetscArraycmp(idx1, idx2, ncdof, congruent));
2974ad8454bSPierre Jolivet     if (!*congruent) goto not_congruent;
298ea844a1aSMatthew Knepley   }
299ea844a1aSMatthew Knepley 
3009566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s1, &nfields));
3019566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s2, &n2));
302ea844a1aSMatthew Knepley   if (nfields != n2) goto not_congruent;
303ea844a1aSMatthew Knepley 
304ea844a1aSMatthew Knepley   for (f = 0; f < nfields; ++f) {
3059566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s1, f, &n1));
3069566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s2, f, &n2));
307ea844a1aSMatthew Knepley     if (n1 != n2) goto not_congruent;
308ea844a1aSMatthew Knepley 
309ea844a1aSMatthew Knepley     for (p = pStart; p < pEnd; ++p) {
3109566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldOffset(s1, p, f, &n1));
3119566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldOffset(s2, p, f, &n2));
312ea844a1aSMatthew Knepley       if (n1 != n2) goto not_congruent;
313ea844a1aSMatthew Knepley 
3149566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s1, p, f, &n1));
3159566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s2, p, f, &n2));
316ea844a1aSMatthew Knepley       if (n1 != n2) goto not_congruent;
317ea844a1aSMatthew Knepley 
3189566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(s1, p, f, &nfcdof));
3199566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(s2, p, f, &n2));
320ea844a1aSMatthew Knepley       if (nfcdof != n2) goto not_congruent;
321ea844a1aSMatthew Knepley 
3229566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintIndices(s1, p, f, &idx1));
3239566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintIndices(s2, p, f, &idx2));
3249566063dSJacob Faibussowitsch       PetscCall(PetscArraycmp(idx1, idx2, nfcdof, congruent));
3254ad8454bSPierre Jolivet       if (!*congruent) goto not_congruent;
326ea844a1aSMatthew Knepley     }
327ea844a1aSMatthew Knepley   }
328ea844a1aSMatthew Knepley 
329ea844a1aSMatthew Knepley   flg = PETSC_TRUE;
330ea844a1aSMatthew Knepley not_congruent:
331462c564dSBarry Smith   PetscCallMPI(MPIU_Allreduce(&flg, congruent, 1, MPIU_BOOL, MPI_LAND, PetscObjectComm((PetscObject)s1)));
3323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
333ea844a1aSMatthew Knepley }
334ea844a1aSMatthew Knepley 
335ea844a1aSMatthew Knepley /*@
336cab54364SBarry Smith   PetscSectionGetNumFields - Returns the number of fields in a `PetscSection`, or 0 if no fields were defined.
337ea844a1aSMatthew Knepley 
33840750d41SVaclav Hapla   Not Collective
339ea844a1aSMatthew Knepley 
340ea844a1aSMatthew Knepley   Input Parameter:
341cab54364SBarry Smith . s - the `PetscSection`
342ea844a1aSMatthew Knepley 
343ea844a1aSMatthew Knepley   Output Parameter:
344ea844a1aSMatthew Knepley . numFields - the number of fields defined, or 0 if none were defined
345ea844a1aSMatthew Knepley 
346ea844a1aSMatthew Knepley   Level: intermediate
347ea844a1aSMatthew Knepley 
348*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionSetNumFields()`
349ea844a1aSMatthew Knepley @*/
350d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetNumFields(PetscSection s, PetscInt *numFields)
351d71ae5a4SJacob Faibussowitsch {
352ea844a1aSMatthew Knepley   PetscFunctionBegin;
353ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
3544f572ea9SToby Isaac   PetscAssertPointer(numFields, 2);
355ea844a1aSMatthew Knepley   *numFields = s->numFields;
3563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
357ea844a1aSMatthew Knepley }
358ea844a1aSMatthew Knepley 
359ea844a1aSMatthew Knepley /*@
360cab54364SBarry Smith   PetscSectionSetNumFields - Sets the number of fields in a `PetscSection`
361ea844a1aSMatthew Knepley 
36240750d41SVaclav Hapla   Not Collective
363ea844a1aSMatthew Knepley 
364ea844a1aSMatthew Knepley   Input Parameters:
36520662ed9SBarry Smith + s         - the `PetscSection`
366ea844a1aSMatthew Knepley - numFields - the number of fields
367ea844a1aSMatthew Knepley 
368ea844a1aSMatthew Knepley   Level: intermediate
369ea844a1aSMatthew Knepley 
370583308b6SBarry Smith   Notes:
371583308b6SBarry Smith   Calling this destroys all the information in the `PetscSection` including the chart.
372583308b6SBarry Smith 
373583308b6SBarry Smith   You must call `PetscSectionSetChart()` after calling this.
374583308b6SBarry Smith 
375*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionGetNumFields()`, `PetscSectionSetChart()`, `PetscSectionReset()`
376ea844a1aSMatthew Knepley @*/
377d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetNumFields(PetscSection s, PetscInt numFields)
378d71ae5a4SJacob Faibussowitsch {
379ea844a1aSMatthew Knepley   PetscInt f;
380ea844a1aSMatthew Knepley 
381ea844a1aSMatthew Knepley   PetscFunctionBegin;
382ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
38308401ef6SPierre Jolivet   PetscCheck(numFields > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "The number of fields %" PetscInt_FMT " must be positive", numFields);
3849566063dSJacob Faibussowitsch   PetscCall(PetscSectionReset(s));
385ea844a1aSMatthew Knepley 
386ea844a1aSMatthew Knepley   s->numFields = numFields;
3879566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(s->numFields, &s->numFieldComponents));
3889566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(s->numFields, &s->fieldNames));
3899566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(s->numFields, &s->compNames));
3909566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(s->numFields, &s->field));
391ea844a1aSMatthew Knepley   for (f = 0; f < s->numFields; ++f) {
392ea844a1aSMatthew Knepley     char name[64];
393ea844a1aSMatthew Knepley 
394ea844a1aSMatthew Knepley     s->numFieldComponents[f] = 1;
395ea844a1aSMatthew Knepley 
3969566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s), &s->field[f]));
3979566063dSJacob Faibussowitsch     PetscCall(PetscSNPrintf(name, 64, "Field_%" PetscInt_FMT, f));
3989566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, (char **)&s->fieldNames[f]));
3999566063dSJacob Faibussowitsch     PetscCall(PetscSNPrintf(name, 64, "Component_0"));
4009566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(s->numFieldComponents[f], &s->compNames[f]));
4019566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, (char **)&s->compNames[f][0]));
402ea844a1aSMatthew Knepley   }
4033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
404ea844a1aSMatthew Knepley }
405ea844a1aSMatthew Knepley 
406cc4c1da9SBarry Smith /*@
407cab54364SBarry Smith   PetscSectionGetFieldName - Returns the name of a field in the `PetscSection`
408ea844a1aSMatthew Knepley 
409ea844a1aSMatthew Knepley   Not Collective
410ea844a1aSMatthew Knepley 
411ea844a1aSMatthew Knepley   Input Parameters:
412cab54364SBarry Smith + s     - the `PetscSection`
413ea844a1aSMatthew Knepley - field - the field number
414ea844a1aSMatthew Knepley 
415ea844a1aSMatthew Knepley   Output Parameter:
416ea844a1aSMatthew Knepley . fieldName - the field name
417ea844a1aSMatthew Knepley 
418ea844a1aSMatthew Knepley   Level: intermediate
419ea844a1aSMatthew Knepley 
420cab54364SBarry Smith   Note:
421cab54364SBarry Smith   Will error if the field number is out of range
422cab54364SBarry Smith 
423*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionSetFieldName()`, `PetscSectionSetNumFields()`, `PetscSectionGetNumFields()`
424ea844a1aSMatthew Knepley @*/
425d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldName(PetscSection s, PetscInt field, const char *fieldName[])
426d71ae5a4SJacob Faibussowitsch {
427ea844a1aSMatthew Knepley   PetscFunctionBegin;
428ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
4294f572ea9SToby Isaac   PetscAssertPointer(fieldName, 3);
4302abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
431ea844a1aSMatthew Knepley   *fieldName = s->fieldNames[field];
4323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
433ea844a1aSMatthew Knepley }
434ea844a1aSMatthew Knepley 
435cc4c1da9SBarry Smith /*@
436cab54364SBarry Smith   PetscSectionSetFieldName - Sets the name of a field in the `PetscSection`
437ea844a1aSMatthew Knepley 
438ea844a1aSMatthew Knepley   Not Collective
439ea844a1aSMatthew Knepley 
440ea844a1aSMatthew Knepley   Input Parameters:
441cab54364SBarry Smith + s         - the `PetscSection`
442ea844a1aSMatthew Knepley . field     - the field number
443ea844a1aSMatthew Knepley - fieldName - the field name
444ea844a1aSMatthew Knepley 
445ea844a1aSMatthew Knepley   Level: intermediate
446ea844a1aSMatthew Knepley 
447cab54364SBarry Smith   Note:
448cab54364SBarry Smith   Will error if the field number is out of range
449cab54364SBarry Smith 
450*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSectionGetFieldName()`, `PetscSectionSetNumFields()`, `PetscSectionGetNumFields()`
451ea844a1aSMatthew Knepley @*/
452d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFieldName(PetscSection s, PetscInt field, const char fieldName[])
453d71ae5a4SJacob Faibussowitsch {
454ea844a1aSMatthew Knepley   PetscFunctionBegin;
455ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
4564f572ea9SToby Isaac   if (fieldName) PetscAssertPointer(fieldName, 3);
4572abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
4589566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->fieldNames[field]));
4599566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(fieldName, (char **)&s->fieldNames[field]));
4603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
461ea844a1aSMatthew Knepley }
462ea844a1aSMatthew Knepley 
463cc4c1da9SBarry Smith /*@
464cab54364SBarry Smith   PetscSectionGetComponentName - Gets the name of a field component in the `PetscSection`
465b778fa18SValeria Barra 
466b778fa18SValeria Barra   Not Collective
467b778fa18SValeria Barra 
468b778fa18SValeria Barra   Input Parameters:
469cab54364SBarry Smith + s     - the `PetscSection`
470b778fa18SValeria Barra . field - the field number
471cab54364SBarry Smith - comp  - the component number
472cab54364SBarry Smith 
473d5b43468SJose E. Roman   Output Parameter:
474cab54364SBarry Smith . compName - the component name
475b778fa18SValeria Barra 
476b778fa18SValeria Barra   Level: intermediate
477b778fa18SValeria Barra 
478cab54364SBarry Smith   Note:
479cab54364SBarry Smith   Will error if the field or component number do not exist
480cab54364SBarry Smith 
48138b5cf2dSJacob Faibussowitsch   Developer Notes:
482583308b6SBarry Smith   The function name should have Field in it since they are field components.
483583308b6SBarry Smith 
484*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionGetFieldName()`, `PetscSectionSetNumFields()`, `PetscSectionGetNumFields()`,
485cab54364SBarry Smith           `PetscSectionSetComponentName()`, `PetscSectionSetFieldName()`, `PetscSectionGetFieldComponents()`, `PetscSectionSetFieldComponents()`
486b778fa18SValeria Barra @*/
487d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetComponentName(PetscSection s, PetscInt field, PetscInt comp, const char *compName[])
488d71ae5a4SJacob Faibussowitsch {
489b778fa18SValeria Barra   PetscFunctionBegin;
490b778fa18SValeria Barra   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
4914f572ea9SToby Isaac   PetscAssertPointer(compName, 4);
4922abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
4932abc8c78SJacob Faibussowitsch   PetscSectionCheckValidFieldComponent(comp, s->numFieldComponents[field]);
494b778fa18SValeria Barra   *compName = s->compNames[field][comp];
4953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
496b778fa18SValeria Barra }
497b778fa18SValeria Barra 
498cc4c1da9SBarry Smith /*@
499cab54364SBarry Smith   PetscSectionSetComponentName - Sets the name of a field component in the `PetscSection`
500b778fa18SValeria Barra 
501b778fa18SValeria Barra   Not Collective
502b778fa18SValeria Barra 
503b778fa18SValeria Barra   Input Parameters:
50420662ed9SBarry Smith + s        - the `PetscSection`
505b778fa18SValeria Barra . field    - the field number
506b778fa18SValeria Barra . comp     - the component number
507b778fa18SValeria Barra - compName - the component name
508b778fa18SValeria Barra 
509583308b6SBarry Smith   Level: advanced
510b778fa18SValeria Barra 
511cab54364SBarry Smith   Note:
512cab54364SBarry Smith   Will error if the field or component number do not exist
513cab54364SBarry Smith 
51438b5cf2dSJacob Faibussowitsch   Developer Notes:
515583308b6SBarry Smith   The function name should have Field in it since they are field components.
516583308b6SBarry Smith 
517*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionGetComponentName()`, `PetscSectionSetNumFields()`, `PetscSectionGetNumFields()`,
51842747ad1SJacob Faibussowitsch           `PetscSectionSetFieldName()`, `PetscSectionGetFieldComponents()`, `PetscSectionSetFieldComponents()`
519b778fa18SValeria Barra @*/
520d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetComponentName(PetscSection s, PetscInt field, PetscInt comp, const char compName[])
521d71ae5a4SJacob Faibussowitsch {
522b778fa18SValeria Barra   PetscFunctionBegin;
523b778fa18SValeria Barra   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
5244f572ea9SToby Isaac   if (compName) PetscAssertPointer(compName, 4);
5252abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
5262abc8c78SJacob Faibussowitsch   PetscSectionCheckValidFieldComponent(comp, s->numFieldComponents[field]);
5279566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->compNames[field][comp]));
5289566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(compName, (char **)&s->compNames[field][comp]));
5293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
530b778fa18SValeria Barra }
531b778fa18SValeria Barra 
532ea844a1aSMatthew Knepley /*@
533ea844a1aSMatthew Knepley   PetscSectionGetFieldComponents - Returns the number of field components for the given field.
534ea844a1aSMatthew Knepley 
53540750d41SVaclav Hapla   Not Collective
536ea844a1aSMatthew Knepley 
537ea844a1aSMatthew Knepley   Input Parameters:
538cab54364SBarry Smith + s     - the `PetscSection`
539ea844a1aSMatthew Knepley - field - the field number
540ea844a1aSMatthew Knepley 
541ea844a1aSMatthew Knepley   Output Parameter:
542ea844a1aSMatthew Knepley . numComp - the number of field components
543ea844a1aSMatthew Knepley 
544583308b6SBarry Smith   Level: advanced
545ea844a1aSMatthew Knepley 
54638b5cf2dSJacob Faibussowitsch   Developer Notes:
547cab54364SBarry Smith   This function is misnamed. There is a Num in `PetscSectionGetNumFields()` but not in this name
548cab54364SBarry Smith 
549*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionSetFieldComponents()`, `PetscSectionGetNumFields()`,
550583308b6SBarry Smith           `PetscSectionSetComponentName()`, `PetscSectionGetComponentName()`
551ea844a1aSMatthew Knepley @*/
552d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldComponents(PetscSection s, PetscInt field, PetscInt *numComp)
553d71ae5a4SJacob Faibussowitsch {
554ea844a1aSMatthew Knepley   PetscFunctionBegin;
555ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
5564f572ea9SToby Isaac   PetscAssertPointer(numComp, 3);
5572abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
558ea844a1aSMatthew Knepley   *numComp = s->numFieldComponents[field];
5593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
560ea844a1aSMatthew Knepley }
561ea844a1aSMatthew Knepley 
562ea844a1aSMatthew Knepley /*@
563ea844a1aSMatthew Knepley   PetscSectionSetFieldComponents - Sets the number of field components for the given field.
564ea844a1aSMatthew Knepley 
56540750d41SVaclav Hapla   Not Collective
566ea844a1aSMatthew Knepley 
567ea844a1aSMatthew Knepley   Input Parameters:
56820662ed9SBarry Smith + s       - the `PetscSection`
569ea844a1aSMatthew Knepley . field   - the field number
570ea844a1aSMatthew Knepley - numComp - the number of field components
571ea844a1aSMatthew Knepley 
572583308b6SBarry Smith   Level: advanced
573ea844a1aSMatthew Knepley 
574583308b6SBarry Smith   Note:
575583308b6SBarry Smith   This number can be different than the values set with `PetscSectionSetFieldDof()`. It can be used to indicate the number of
576583308b6SBarry Smith   components in the field of the underlying physical model which may be different than the number of degrees of freedom needed
577583308b6SBarry 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
578583308b6SBarry 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.
579583308b6SBarry Smith 
580583308b6SBarry Smith   The value set with this function are not needed or used in `PetscSectionSetUp()`.
581583308b6SBarry Smith 
58238b5cf2dSJacob Faibussowitsch   Developer Notes:
583583308b6SBarry Smith   This function is misnamed. There is a Num in `PetscSectionSetNumFields()` but not in this name
584583308b6SBarry Smith 
585*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionGetFieldComponents()`, `PetscSectionSetComponentName()`,
586583308b6SBarry Smith           `PetscSectionGetComponentName()`, `PetscSectionGetNumFields()`
587ea844a1aSMatthew Knepley @*/
588d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFieldComponents(PetscSection s, PetscInt field, PetscInt numComp)
589d71ae5a4SJacob Faibussowitsch {
590b778fa18SValeria Barra   PetscInt c;
591b778fa18SValeria Barra 
592ea844a1aSMatthew Knepley   PetscFunctionBegin;
593ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
5942abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
595b778fa18SValeria Barra   if (s->compNames) {
59648a46eb9SPierre Jolivet     for (c = 0; c < s->numFieldComponents[field]; ++c) PetscCall(PetscFree(s->compNames[field][c]));
5979566063dSJacob Faibussowitsch     PetscCall(PetscFree(s->compNames[field]));
598b778fa18SValeria Barra   }
599b778fa18SValeria Barra 
600ea844a1aSMatthew Knepley   s->numFieldComponents[field] = numComp;
601b778fa18SValeria Barra   if (numComp) {
6029566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numComp, (char ***)&s->compNames[field]));
603b778fa18SValeria Barra     for (c = 0; c < numComp; ++c) {
6042abc8c78SJacob Faibussowitsch       char name[64];
6052abc8c78SJacob Faibussowitsch 
6069566063dSJacob Faibussowitsch       PetscCall(PetscSNPrintf(name, 64, "%" PetscInt_FMT, c));
6079566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(name, (char **)&s->compNames[field][c]));
608b778fa18SValeria Barra     }
609b778fa18SValeria Barra   }
6103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
611ea844a1aSMatthew Knepley }
612ea844a1aSMatthew Knepley 
613ea844a1aSMatthew Knepley /*@
614583308b6SBarry Smith   PetscSectionGetChart - Returns the range [`pStart`, `pEnd`) in which points (indices) lie for this `PetscSection` on this MPI process
615ea844a1aSMatthew Knepley 
61640750d41SVaclav Hapla   Not Collective
617ea844a1aSMatthew Knepley 
618ea844a1aSMatthew Knepley   Input Parameter:
619cab54364SBarry Smith . s - the `PetscSection`
620ea844a1aSMatthew Knepley 
621ea844a1aSMatthew Knepley   Output Parameters:
622ea844a1aSMatthew Knepley + pStart - the first point
623ea844a1aSMatthew Knepley - pEnd   - one past the last point
624ea844a1aSMatthew Knepley 
625ea844a1aSMatthew Knepley   Level: intermediate
626ea844a1aSMatthew Knepley 
627*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionSetChart()`, `PetscSectionCreate()`
628ea844a1aSMatthew Knepley @*/
629d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetChart(PetscSection s, PetscInt *pStart, PetscInt *pEnd)
630d71ae5a4SJacob Faibussowitsch {
631ea844a1aSMatthew Knepley   PetscFunctionBegin;
632ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
633ea844a1aSMatthew Knepley   if (pStart) *pStart = s->pStart;
634ea844a1aSMatthew Knepley   if (pEnd) *pEnd = s->pEnd;
6353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
636ea844a1aSMatthew Knepley }
637ea844a1aSMatthew Knepley 
638ea844a1aSMatthew Knepley /*@
639583308b6SBarry Smith   PetscSectionSetChart - Sets the range [`pStart`, `pEnd`) in which points (indices) lie for this `PetscSection` on this MPI process
640ea844a1aSMatthew Knepley 
64140750d41SVaclav Hapla   Not Collective
642ea844a1aSMatthew Knepley 
643ea844a1aSMatthew Knepley   Input Parameters:
64420662ed9SBarry Smith + s      - the `PetscSection`
645ea844a1aSMatthew Knepley . pStart - the first point
646376335faSBarry Smith - pEnd   - one past the last point, `pStart` $ \le $ `pEnd`
647ea844a1aSMatthew Knepley 
648ea844a1aSMatthew Knepley   Level: intermediate
649ea844a1aSMatthew Knepley 
650583308b6SBarry Smith   Notes:
651583308b6SBarry Smith   The charts on different MPI processes may (and often do) overlap
652583308b6SBarry Smith 
653583308b6SBarry Smith   If you intend to use `PetscSectionSetNumFields()` it must be called before this call.
654583308b6SBarry Smith 
655583308b6SBarry Smith   The chart for all fields created with `PetscSectionSetNumFields()` is the same as this chart.
656583308b6SBarry Smith 
657*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionGetChart()`, `PetscSectionCreate()`, `PetscSectionSetNumFields()`
658ea844a1aSMatthew Knepley @*/
659d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetChart(PetscSection s, PetscInt pStart, PetscInt pEnd)
660d71ae5a4SJacob Faibussowitsch {
661ea844a1aSMatthew Knepley   PetscInt f;
662ea844a1aSMatthew Knepley 
663ea844a1aSMatthew Knepley   PetscFunctionBegin;
664ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
66540196513SBarry 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);
6663ba16761SJacob Faibussowitsch   if (pStart == s->pStart && pEnd == s->pEnd) PetscFunctionReturn(PETSC_SUCCESS);
667ea844a1aSMatthew Knepley   /* Cannot Reset() because it destroys field information */
668ea844a1aSMatthew Knepley   s->setup = PETSC_FALSE;
6699566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&s->bc));
6709566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->bcIndices));
6719566063dSJacob Faibussowitsch   PetscCall(PetscFree2(s->atlasDof, s->atlasOff));
672ea844a1aSMatthew Knepley 
673ea844a1aSMatthew Knepley   s->pStart = pStart;
674ea844a1aSMatthew Knepley   s->pEnd   = pEnd;
67557508eceSPierre Jolivet   PetscCall(PetscMalloc2(pEnd - pStart, &s->atlasDof, pEnd - pStart, &s->atlasOff));
6769566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(s->atlasDof, pEnd - pStart));
67748a46eb9SPierre Jolivet   for (f = 0; f < s->numFields; ++f) PetscCall(PetscSectionSetChart(s->field[f], pStart, pEnd));
6783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
679ea844a1aSMatthew Knepley }
680ea844a1aSMatthew Knepley 
681ea844a1aSMatthew Knepley /*@
68220662ed9SBarry Smith   PetscSectionGetPermutation - Returns the permutation of [0, `pEnd` - `pStart`) or `NULL` that was set with `PetscSectionSetPermutation()`
683ea844a1aSMatthew Knepley 
68440750d41SVaclav Hapla   Not Collective
685ea844a1aSMatthew Knepley 
686ea844a1aSMatthew Knepley   Input Parameter:
687cab54364SBarry Smith . s - the `PetscSection`
688ea844a1aSMatthew Knepley 
6892fe279fdSBarry Smith   Output Parameter:
690cab54364SBarry Smith . perm - The permutation as an `IS`
691ea844a1aSMatthew Knepley 
692ea844a1aSMatthew Knepley   Level: intermediate
693ea844a1aSMatthew Knepley 
694cab54364SBarry Smith .seealso: [](sec_scatter), `IS`, `PetscSection`, `PetscSectionSetPermutation()`, `PetscSectionCreate()`
695ea844a1aSMatthew Knepley @*/
696d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetPermutation(PetscSection s, IS *perm)
697d71ae5a4SJacob Faibussowitsch {
698ea844a1aSMatthew Knepley   PetscFunctionBegin;
699ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
7009371c9d4SSatish Balay   if (perm) {
7014f572ea9SToby Isaac     PetscAssertPointer(perm, 2);
7029371c9d4SSatish Balay     *perm = s->perm;
7039371c9d4SSatish Balay   }
7043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
705ea844a1aSMatthew Knepley }
706ea844a1aSMatthew Knepley 
707ea844a1aSMatthew Knepley /*@
708583308b6SBarry Smith   PetscSectionSetPermutation - Sets a permutation of the chart for this section, [0, `pEnd` - `pStart`), which determines the order to store the `PetscSection` information
709ea844a1aSMatthew Knepley 
71040750d41SVaclav Hapla   Not Collective
711ea844a1aSMatthew Knepley 
712ea844a1aSMatthew Knepley   Input Parameters:
71320662ed9SBarry Smith + s    - the `PetscSection`
714ea844a1aSMatthew Knepley - perm - the permutation of points
715ea844a1aSMatthew Knepley 
716ea844a1aSMatthew Knepley   Level: intermediate
717ea844a1aSMatthew Knepley 
718583308b6SBarry Smith   Notes:
719583308b6SBarry Smith   The permutation must be provided before `PetscSectionSetUp()`.
720cab54364SBarry Smith 
721583308b6SBarry Smith   The data in the `PetscSection` are permuted but the access via `PetscSectionGetFieldOffset()` and `PetscSectionGetOffset()` is not changed
722583308b6SBarry Smith 
7233ec46b7bSMatthew G. Knepley   Compare to `PetscSectionPermute()`
724583308b6SBarry Smith 
725583308b6SBarry Smith .seealso: [](sec_scatter), `IS`, `PetscSection`, `PetscSectionSetUp()`, `PetscSectionGetPermutation()`, `PetscSectionPermute()`, `PetscSectionCreate()`
726ea844a1aSMatthew Knepley @*/
727d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetPermutation(PetscSection s, IS perm)
728d71ae5a4SJacob Faibussowitsch {
729ea844a1aSMatthew Knepley   PetscFunctionBegin;
730ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
731ea844a1aSMatthew Knepley   if (perm) PetscValidHeaderSpecific(perm, IS_CLASSID, 2);
73228b400f6SJacob Faibussowitsch   PetscCheck(!s->setup, PetscObjectComm((PetscObject)s), PETSC_ERR_ARG_WRONGSTATE, "Cannot set a permutation after the section is setup");
733ea844a1aSMatthew Knepley   if (s->perm != perm) {
7349566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&s->perm));
735ea844a1aSMatthew Knepley     if (perm) {
736ea844a1aSMatthew Knepley       s->perm = perm;
7379566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)s->perm));
738ea844a1aSMatthew Knepley     }
739ea844a1aSMatthew Knepley   }
7403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
741ea844a1aSMatthew Knepley }
742ea844a1aSMatthew Knepley 
7433ec46b7bSMatthew G. Knepley /*@C
7443ec46b7bSMatthew G. Knepley   PetscSectionGetBlockStarts - Returns a table indicating which points start new blocks
7453ec46b7bSMatthew G. Knepley 
746cc4c1da9SBarry Smith   Not Collective, No Fortran Support
7473ec46b7bSMatthew G. Knepley 
7483ec46b7bSMatthew G. Knepley   Input Parameter:
7493ec46b7bSMatthew G. Knepley . s - the `PetscSection`
7503ec46b7bSMatthew G. Knepley 
7513ec46b7bSMatthew G. Knepley   Output Parameter:
7523ec46b7bSMatthew G. Knepley . blockStarts - The `PetscBT` with a 1 for each point that begins a block
7533ec46b7bSMatthew G. Knepley 
7543ec46b7bSMatthew G. Knepley   Notes:
7553ec46b7bSMatthew G. Knepley   The table is on [0, `pEnd` - `pStart`).
7563ec46b7bSMatthew G. Knepley 
7573ec46b7bSMatthew G. Knepley   This information is used by `DMCreateMatrix()` to create a variable block size description which is set using `MatSetVariableBlockSizes()`.
7583ec46b7bSMatthew G. Knepley 
7593ec46b7bSMatthew G. Knepley   Level: intermediate
7603ec46b7bSMatthew G. Knepley 
7613ec46b7bSMatthew G. Knepley .seealso: [](sec_scatter), `IS`, `PetscSection`, `PetscSectionSetBlockStarts()`, `PetscSectionCreate()`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
7623ec46b7bSMatthew G. Knepley @*/
7633ec46b7bSMatthew G. Knepley PetscErrorCode PetscSectionGetBlockStarts(PetscSection s, PetscBT *blockStarts)
7643ec46b7bSMatthew G. Knepley {
7653ec46b7bSMatthew G. Knepley   PetscFunctionBegin;
7663ec46b7bSMatthew G. Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
7673ec46b7bSMatthew G. Knepley   if (blockStarts) {
7683ec46b7bSMatthew G. Knepley     PetscAssertPointer(blockStarts, 2);
7693ec46b7bSMatthew G. Knepley     *blockStarts = s->blockStarts;
7703ec46b7bSMatthew G. Knepley   }
7713ec46b7bSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
7723ec46b7bSMatthew G. Knepley }
7733ec46b7bSMatthew G. Knepley 
7743ec46b7bSMatthew G. Knepley /*@C
7753ec46b7bSMatthew G. Knepley   PetscSectionSetBlockStarts - Sets a table indicating which points start new blocks
7763ec46b7bSMatthew G. Knepley 
777cc4c1da9SBarry Smith   Not Collective, No Fortran Support
7783ec46b7bSMatthew G. Knepley 
7793ec46b7bSMatthew G. Knepley   Input Parameters:
7803ec46b7bSMatthew G. Knepley + s           - the `PetscSection`
7813ec46b7bSMatthew G. Knepley - blockStarts - The `PetscBT` with a 1 for each point that begins a block
7823ec46b7bSMatthew G. Knepley 
7833ec46b7bSMatthew G. Knepley   Level: intermediate
7843ec46b7bSMatthew G. Knepley 
7853ec46b7bSMatthew G. Knepley   Notes:
7863ec46b7bSMatthew 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.
7873ec46b7bSMatthew G. Knepley 
7883ec46b7bSMatthew G. Knepley   This information is used by `DMCreateMatrix()` to create a variable block size description which is set using `MatSetVariableBlockSizes()`.
7893ec46b7bSMatthew G. Knepley 
7903ec46b7bSMatthew G. Knepley .seealso: [](sec_scatter), `IS`, `PetscSection`, `PetscSectionGetBlockStarts()`, `PetscSectionCreate()`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
7913ec46b7bSMatthew G. Knepley @*/
7923ec46b7bSMatthew G. Knepley PetscErrorCode PetscSectionSetBlockStarts(PetscSection s, PetscBT blockStarts)
7933ec46b7bSMatthew G. Knepley {
7943ec46b7bSMatthew G. Knepley   PetscFunctionBegin;
7953ec46b7bSMatthew G. Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
7963ec46b7bSMatthew G. Knepley   if (s->blockStarts != blockStarts) {
7973ec46b7bSMatthew G. Knepley     PetscCall(PetscBTDestroy(&s->blockStarts));
7983ec46b7bSMatthew G. Knepley     s->blockStarts = blockStarts;
7993ec46b7bSMatthew G. Knepley   }
8003ec46b7bSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
8013ec46b7bSMatthew G. Knepley }
8023ec46b7bSMatthew G. Knepley 
803ea844a1aSMatthew Knepley /*@
804cab54364SBarry Smith   PetscSectionGetPointMajor - Returns the flag for dof ordering, `PETSC_TRUE` if it is point major, `PETSC_FALSE` if it is field major
805ea844a1aSMatthew Knepley 
80640750d41SVaclav Hapla   Not Collective
807ea844a1aSMatthew Knepley 
808ea844a1aSMatthew Knepley   Input Parameter:
809cab54364SBarry Smith . s - the `PetscSection`
810ea844a1aSMatthew Knepley 
811ea844a1aSMatthew Knepley   Output Parameter:
812ea844a1aSMatthew Knepley . pm - the flag for point major ordering
813ea844a1aSMatthew Knepley 
814ea844a1aSMatthew Knepley   Level: intermediate
815ea844a1aSMatthew Knepley 
816*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionSetPointMajor()`
817ea844a1aSMatthew Knepley @*/
818d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetPointMajor(PetscSection s, PetscBool *pm)
819d71ae5a4SJacob Faibussowitsch {
820ea844a1aSMatthew Knepley   PetscFunctionBegin;
821ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
8224f572ea9SToby Isaac   PetscAssertPointer(pm, 2);
823ea844a1aSMatthew Knepley   *pm = s->pointMajor;
8243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
825ea844a1aSMatthew Knepley }
826ea844a1aSMatthew Knepley 
827ea844a1aSMatthew Knepley /*@
828cab54364SBarry Smith   PetscSectionSetPointMajor - Sets the flag for dof ordering, `PETSC_TRUE` for point major, otherwise it will be field major
829ea844a1aSMatthew Knepley 
83040750d41SVaclav Hapla   Not Collective
831ea844a1aSMatthew Knepley 
832ea844a1aSMatthew Knepley   Input Parameters:
833cab54364SBarry Smith + s  - the `PetscSection`
834ea844a1aSMatthew Knepley - pm - the flag for point major ordering
835ea844a1aSMatthew Knepley 
836ea844a1aSMatthew Knepley   Level: intermediate
837ea844a1aSMatthew Knepley 
838583308b6SBarry Smith   Note:
839583308b6SBarry 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.
840583308b6SBarry Smith 
841583308b6SBarry Smith   Point major order means the degrees of freedom are stored as follows
842583308b6SBarry Smith .vb
84311e840c0SPierre Jolivet     all the degrees of freedom for each point are stored contiguously, one point after another (respecting a permutation set with `PetscSectionSetPermutation()`)
844583308b6SBarry Smith     for each point
845583308b6SBarry Smith        the degrees of freedom for each field (starting with the unnamed default field) are listed in order by field
846583308b6SBarry Smith .ve
847583308b6SBarry Smith 
848583308b6SBarry Smith   Field major order means the degrees of freedom are stored as follows
849583308b6SBarry Smith .vb
85011e840c0SPierre Jolivet     all degrees of freedom for each field (including the unnamed default field) are stored contiguously, one field after another
851583308b6SBarry Smith     for each field (started with unnamed default field)
852583308b6SBarry Smith       the degrees of freedom for each point are listed in order by point (respecting a permutation set with `PetscSectionSetPermutation()`)
853583308b6SBarry Smith .ve
854583308b6SBarry Smith 
855*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionGetPointMajor()`, `PetscSectionSetPermutation()`
856ea844a1aSMatthew Knepley @*/
857d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetPointMajor(PetscSection s, PetscBool pm)
858d71ae5a4SJacob Faibussowitsch {
859ea844a1aSMatthew Knepley   PetscFunctionBegin;
860ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
86128b400f6SJacob Faibussowitsch   PetscCheck(!s->setup, PetscObjectComm((PetscObject)s), PETSC_ERR_ARG_WRONGSTATE, "Cannot set the dof ordering after the section is setup");
862ea844a1aSMatthew Knepley   s->pointMajor = pm;
8633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
864ea844a1aSMatthew Knepley }
865ea844a1aSMatthew Knepley 
866ea844a1aSMatthew Knepley /*@
867cab54364SBarry Smith   PetscSectionGetIncludesConstraints - Returns the flag indicating if constrained dofs were included when computing offsets in the `PetscSection`.
868cab54364SBarry Smith   The value is set with `PetscSectionSetIncludesConstraints()`
86987e637c6Sksagiyam 
87040750d41SVaclav Hapla   Not Collective
87187e637c6Sksagiyam 
87287e637c6Sksagiyam   Input Parameter:
873cab54364SBarry Smith . s - the `PetscSection`
87487e637c6Sksagiyam 
87587e637c6Sksagiyam   Output Parameter:
87687e637c6Sksagiyam . includesConstraints - the flag indicating if constrained dofs were included when computing offsets
87787e637c6Sksagiyam 
87887e637c6Sksagiyam   Level: intermediate
87987e637c6Sksagiyam 
880*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionSetIncludesConstraints()`
88187e637c6Sksagiyam @*/
882d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetIncludesConstraints(PetscSection s, PetscBool *includesConstraints)
883d71ae5a4SJacob Faibussowitsch {
88487e637c6Sksagiyam   PetscFunctionBegin;
88587e637c6Sksagiyam   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
8864f572ea9SToby Isaac   PetscAssertPointer(includesConstraints, 2);
88787e637c6Sksagiyam   *includesConstraints = s->includesConstraints;
8883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
88987e637c6Sksagiyam }
89087e637c6Sksagiyam 
89187e637c6Sksagiyam /*@
89287e637c6Sksagiyam   PetscSectionSetIncludesConstraints - Sets the flag indicating if constrained dofs are to be included when computing offsets
89387e637c6Sksagiyam 
89440750d41SVaclav Hapla   Not Collective
89587e637c6Sksagiyam 
89687e637c6Sksagiyam   Input Parameters:
89720662ed9SBarry Smith + s                   - the `PetscSection`
89887e637c6Sksagiyam - includesConstraints - the flag indicating if constrained dofs are to be included when computing offsets
89987e637c6Sksagiyam 
90087e637c6Sksagiyam   Level: intermediate
90187e637c6Sksagiyam 
902*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionGetIncludesConstraints()`
90387e637c6Sksagiyam @*/
904d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetIncludesConstraints(PetscSection s, PetscBool includesConstraints)
905d71ae5a4SJacob Faibussowitsch {
90687e637c6Sksagiyam   PetscFunctionBegin;
90787e637c6Sksagiyam   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
90828b400f6SJacob Faibussowitsch   PetscCheck(!s->setup, PetscObjectComm((PetscObject)s), PETSC_ERR_ARG_WRONGSTATE, "Cannot set includesConstraints after the section is set up");
90987e637c6Sksagiyam   s->includesConstraints = includesConstraints;
9103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91187e637c6Sksagiyam }
91287e637c6Sksagiyam 
91387e637c6Sksagiyam /*@
914583308b6SBarry Smith   PetscSectionGetDof - Return the total number of degrees of freedom associated with a given point.
915ea844a1aSMatthew Knepley 
91640750d41SVaclav Hapla   Not Collective
917ea844a1aSMatthew Knepley 
918ea844a1aSMatthew Knepley   Input Parameters:
919cab54364SBarry Smith + s     - the `PetscSection`
920ea844a1aSMatthew Knepley - point - the point
921ea844a1aSMatthew Knepley 
922ea844a1aSMatthew Knepley   Output Parameter:
923ea844a1aSMatthew Knepley . numDof - the number of dof
924ea844a1aSMatthew Knepley 
925583308b6SBarry Smith   Level: intermediate
926583308b6SBarry Smith 
927583308b6SBarry Smith   Notes:
928db527f05SMatthew G. Knepley   In a global section, this size will be negative for points not owned by this process.
929db527f05SMatthew G. Knepley 
930583308b6SBarry 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
931ea844a1aSMatthew Knepley 
932*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionSetDof()`, `PetscSectionCreate()`
933ea844a1aSMatthew Knepley @*/
934d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetDof(PetscSection s, PetscInt point, PetscInt *numDof)
935d71ae5a4SJacob Faibussowitsch {
93676bd3646SJed Brown   PetscFunctionBeginHot;
937ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
9384f572ea9SToby Isaac   PetscAssertPointer(numDof, 3);
939b498ca8aSPierre 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);
940ea844a1aSMatthew Knepley   *numDof = s->atlasDof[point - s->pStart];
9413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
942ea844a1aSMatthew Knepley }
943ea844a1aSMatthew Knepley 
944ea844a1aSMatthew Knepley /*@
945583308b6SBarry Smith   PetscSectionSetDof - Sets the total number of degrees of freedom associated with a given point.
946ea844a1aSMatthew Knepley 
94740750d41SVaclav Hapla   Not Collective
948ea844a1aSMatthew Knepley 
949ea844a1aSMatthew Knepley   Input Parameters:
950cab54364SBarry Smith + s      - the `PetscSection`
951ea844a1aSMatthew Knepley . point  - the point
952376335faSBarry Smith - numDof - the number of dof, these values may be negative -(dof+1) to indicate they are off process
953ea844a1aSMatthew Knepley 
954ea844a1aSMatthew Knepley   Level: intermediate
955ea844a1aSMatthew Knepley 
956583308b6SBarry Smith   Note:
957583308b6SBarry 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
958583308b6SBarry Smith 
959*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionGetDof()`, `PetscSectionAddDof()`, `PetscSectionCreate()`
960ea844a1aSMatthew Knepley @*/
961d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetDof(PetscSection s, PetscInt point, PetscInt numDof)
962d71ae5a4SJacob Faibussowitsch {
963ea844a1aSMatthew Knepley   PetscFunctionBegin;
964ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
965c8bf3acbSVaclav 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);
966ea844a1aSMatthew Knepley   s->atlasDof[point - s->pStart] = numDof;
96769c11d05SVaclav Hapla   PetscCall(PetscSectionInvalidateMaxDof_Internal(s));
9683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
969ea844a1aSMatthew Knepley }
970ea844a1aSMatthew Knepley 
971ea844a1aSMatthew Knepley /*@
972583308b6SBarry Smith   PetscSectionAddDof - Adds to the total number of degrees of freedom associated with a given point.
973ea844a1aSMatthew Knepley 
97440750d41SVaclav Hapla   Not Collective
975ea844a1aSMatthew Knepley 
976ea844a1aSMatthew Knepley   Input Parameters:
977cab54364SBarry Smith + s      - the `PetscSection`
978ea844a1aSMatthew Knepley . point  - the point
979ea844a1aSMatthew Knepley - numDof - the number of additional dof
980ea844a1aSMatthew Knepley 
981ea844a1aSMatthew Knepley   Level: intermediate
982ea844a1aSMatthew Knepley 
983583308b6SBarry Smith   Note:
984583308b6SBarry 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
985583308b6SBarry Smith 
986*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionGetDof()`, `PetscSectionSetDof()`, `PetscSectionCreate()`
987ea844a1aSMatthew Knepley @*/
988d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionAddDof(PetscSection s, PetscInt point, PetscInt numDof)
989d71ae5a4SJacob Faibussowitsch {
99076bd3646SJed Brown   PetscFunctionBeginHot;
991ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
992b498ca8aSPierre 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);
99340196513SBarry Smith   PetscCheck(numDof >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "numDof %" PetscInt_FMT " should not be negative", numDof);
994ea844a1aSMatthew Knepley   s->atlasDof[point - s->pStart] += numDof;
99569c11d05SVaclav Hapla   PetscCall(PetscSectionInvalidateMaxDof_Internal(s));
9963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
997ea844a1aSMatthew Knepley }
998ea844a1aSMatthew Knepley 
999ea844a1aSMatthew Knepley /*@
1000ea844a1aSMatthew Knepley   PetscSectionGetFieldDof - Return the number of degrees of freedom associated with a field on a given point.
1001ea844a1aSMatthew Knepley 
100240750d41SVaclav Hapla   Not Collective
1003ea844a1aSMatthew Knepley 
1004ea844a1aSMatthew Knepley   Input Parameters:
1005cab54364SBarry Smith + s     - the `PetscSection`
1006ea844a1aSMatthew Knepley . point - the point
1007ea844a1aSMatthew Knepley - field - the field
1008ea844a1aSMatthew Knepley 
1009ea844a1aSMatthew Knepley   Output Parameter:
1010ea844a1aSMatthew Knepley . numDof - the number of dof
1011ea844a1aSMatthew Knepley 
1012ea844a1aSMatthew Knepley   Level: intermediate
1013ea844a1aSMatthew Knepley 
1014*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionSetFieldDof()`, `PetscSectionCreate()`
1015ea844a1aSMatthew Knepley @*/
1016d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldDof(PetscSection s, PetscInt point, PetscInt field, PetscInt *numDof)
1017d71ae5a4SJacob Faibussowitsch {
1018ea844a1aSMatthew Knepley   PetscFunctionBegin;
1019ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
10204f572ea9SToby Isaac   PetscAssertPointer(numDof, 4);
10212abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
10229566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetDof(s->field[field], point, numDof));
10233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1024ea844a1aSMatthew Knepley }
1025ea844a1aSMatthew Knepley 
1026ea844a1aSMatthew Knepley /*@
1027ea844a1aSMatthew Knepley   PetscSectionSetFieldDof - Sets the number of degrees of freedom associated with a field on a given point.
1028ea844a1aSMatthew Knepley 
102940750d41SVaclav Hapla   Not Collective
1030ea844a1aSMatthew Knepley 
1031ea844a1aSMatthew Knepley   Input Parameters:
1032cab54364SBarry Smith + s      - the `PetscSection`
1033ea844a1aSMatthew Knepley . point  - the point
1034ea844a1aSMatthew Knepley . field  - the field
1035376335faSBarry Smith - numDof - the number of dof, these values may be negative -(dof+1) to indicate they are off process
1036ea844a1aSMatthew Knepley 
1037ea844a1aSMatthew Knepley   Level: intermediate
1038ea844a1aSMatthew Knepley 
1039583308b6SBarry Smith   Note:
1040583308b6SBarry 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
1041583308b6SBarry Smith   the fields and the unnamed default field) is correct by also calling `PetscSectionAddDof()` or `PetscSectionSetDof()`
1042583308b6SBarry Smith 
1043583308b6SBarry Smith   This is equivalent to
1044583308b6SBarry Smith .vb
1045583308b6SBarry Smith      PetscSection fs;
1046583308b6SBarry Smith      PetscSectionGetField(s,field,&fs)
1047583308b6SBarry Smith      PetscSectionSetDof(fs,numDof)
1048583308b6SBarry Smith .ve
1049583308b6SBarry Smith 
1050*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionGetFieldDof()`, `PetscSectionCreate()`, `PetscSectionAddDof()`, `PetscSectionSetDof()`
1051ea844a1aSMatthew Knepley @*/
1052d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFieldDof(PetscSection s, PetscInt point, PetscInt field, PetscInt numDof)
1053d71ae5a4SJacob Faibussowitsch {
1054ea844a1aSMatthew Knepley   PetscFunctionBegin;
1055ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
10562abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
10579566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetDof(s->field[field], point, numDof));
10583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1059ea844a1aSMatthew Knepley }
1060ea844a1aSMatthew Knepley 
1061ea844a1aSMatthew Knepley /*@
1062ea844a1aSMatthew Knepley   PetscSectionAddFieldDof - Adds a number of degrees of freedom associated with a field on a given point.
1063ea844a1aSMatthew Knepley 
106440750d41SVaclav Hapla   Not Collective
1065ea844a1aSMatthew Knepley 
1066ea844a1aSMatthew Knepley   Input Parameters:
1067cab54364SBarry Smith + s      - the `PetscSection`
1068ea844a1aSMatthew Knepley . point  - the point
1069ea844a1aSMatthew Knepley . field  - the field
1070ea844a1aSMatthew Knepley - numDof - the number of dof
1071ea844a1aSMatthew Knepley 
1072ea844a1aSMatthew Knepley   Level: intermediate
1073ea844a1aSMatthew Knepley 
1074583308b6SBarry Smith   Notes:
1075583308b6SBarry 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
1076583308b6SBarry Smith   the fields and the unnamed default field) is correct by also calling `PetscSectionAddDof()` or `PetscSectionSetDof()`
1077583308b6SBarry Smith 
1078583308b6SBarry Smith   This is equivalent to
1079583308b6SBarry Smith .vb
1080583308b6SBarry Smith      PetscSection fs;
1081583308b6SBarry Smith      PetscSectionGetField(s,field,&fs)
1082583308b6SBarry Smith      PetscSectionAddDof(fs,numDof)
1083583308b6SBarry Smith .ve
1084583308b6SBarry Smith 
1085*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionSetFieldDof()`, `PetscSectionGetFieldDof()`, `PetscSectionCreate()`
1086ea844a1aSMatthew Knepley @*/
1087d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionAddFieldDof(PetscSection s, PetscInt point, PetscInt field, PetscInt numDof)
1088d71ae5a4SJacob Faibussowitsch {
1089ea844a1aSMatthew Knepley   PetscFunctionBegin;
1090ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
10912abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
10929566063dSJacob Faibussowitsch   PetscCall(PetscSectionAddDof(s->field[field], point, numDof));
10933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1094ea844a1aSMatthew Knepley }
1095ea844a1aSMatthew Knepley 
1096ea844a1aSMatthew Knepley /*@
1097ea844a1aSMatthew Knepley   PetscSectionGetConstraintDof - Return the number of constrained degrees of freedom associated with a given point.
1098ea844a1aSMatthew Knepley 
109940750d41SVaclav Hapla   Not Collective
1100ea844a1aSMatthew Knepley 
1101ea844a1aSMatthew Knepley   Input Parameters:
1102cab54364SBarry Smith + s     - the `PetscSection`
1103ea844a1aSMatthew Knepley - point - the point
1104ea844a1aSMatthew Knepley 
1105ea844a1aSMatthew Knepley   Output Parameter:
1106ea844a1aSMatthew Knepley . numDof - the number of dof which are fixed by constraints
1107ea844a1aSMatthew Knepley 
1108ea844a1aSMatthew Knepley   Level: intermediate
1109ea844a1aSMatthew Knepley 
1110*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionGetDof()`, `PetscSectionSetConstraintDof()`, `PetscSectionCreate()`
1111ea844a1aSMatthew Knepley @*/
1112d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetConstraintDof(PetscSection s, PetscInt point, PetscInt *numDof)
1113d71ae5a4SJacob Faibussowitsch {
1114ea844a1aSMatthew Knepley   PetscFunctionBegin;
1115ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
11164f572ea9SToby Isaac   PetscAssertPointer(numDof, 3);
1117ea844a1aSMatthew Knepley   if (s->bc) {
11189566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s->bc, point, numDof));
1119ea844a1aSMatthew Knepley   } else *numDof = 0;
11203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1121ea844a1aSMatthew Knepley }
1122ea844a1aSMatthew Knepley 
1123ea844a1aSMatthew Knepley /*@
1124ea844a1aSMatthew Knepley   PetscSectionSetConstraintDof - Set the number of constrained degrees of freedom associated with a given point.
1125ea844a1aSMatthew Knepley 
112640750d41SVaclav Hapla   Not Collective
1127ea844a1aSMatthew Knepley 
1128ea844a1aSMatthew Knepley   Input Parameters:
1129cab54364SBarry Smith + s      - the `PetscSection`
1130ea844a1aSMatthew Knepley . point  - the point
1131ea844a1aSMatthew Knepley - numDof - the number of dof which are fixed by constraints
1132ea844a1aSMatthew Knepley 
1133ea844a1aSMatthew Knepley   Level: intermediate
1134ea844a1aSMatthew Knepley 
1135*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionSetDof()`, `PetscSectionGetConstraintDof()`, `PetscSectionCreate()`
1136ea844a1aSMatthew Knepley @*/
1137d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetConstraintDof(PetscSection s, PetscInt point, PetscInt numDof)
1138d71ae5a4SJacob Faibussowitsch {
1139ea844a1aSMatthew Knepley   PetscFunctionBegin;
1140ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1141ea844a1aSMatthew Knepley   if (numDof) {
11427c0883d5SVaclav Hapla     PetscCall(PetscSectionCheckConstraints_Private(s));
11439566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(s->bc, point, numDof));
1144ea844a1aSMatthew Knepley   }
11453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1146ea844a1aSMatthew Knepley }
1147ea844a1aSMatthew Knepley 
1148ea844a1aSMatthew Knepley /*@
1149ea844a1aSMatthew Knepley   PetscSectionAddConstraintDof - Increment the number of constrained degrees of freedom associated with a given point.
1150ea844a1aSMatthew Knepley 
115140750d41SVaclav Hapla   Not Collective
1152ea844a1aSMatthew Knepley 
1153ea844a1aSMatthew Knepley   Input Parameters:
1154cab54364SBarry Smith + s      - the `PetscSection`
1155ea844a1aSMatthew Knepley . point  - the point
1156ea844a1aSMatthew Knepley - numDof - the number of additional dof which are fixed by constraints
1157ea844a1aSMatthew Knepley 
1158ea844a1aSMatthew Knepley   Level: intermediate
1159ea844a1aSMatthew Knepley 
1160*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionAddDof()`, `PetscSectionGetConstraintDof()`, `PetscSectionCreate()`
1161ea844a1aSMatthew Knepley @*/
1162d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionAddConstraintDof(PetscSection s, PetscInt point, PetscInt numDof)
1163d71ae5a4SJacob Faibussowitsch {
1164ea844a1aSMatthew Knepley   PetscFunctionBegin;
1165ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1166ea844a1aSMatthew Knepley   if (numDof) {
11677c0883d5SVaclav Hapla     PetscCall(PetscSectionCheckConstraints_Private(s));
11689566063dSJacob Faibussowitsch     PetscCall(PetscSectionAddDof(s->bc, point, numDof));
1169ea844a1aSMatthew Knepley   }
11703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1171ea844a1aSMatthew Knepley }
1172ea844a1aSMatthew Knepley 
1173ea844a1aSMatthew Knepley /*@
1174ea844a1aSMatthew Knepley   PetscSectionGetFieldConstraintDof - Return the number of constrained degrees of freedom associated with a given field on a point.
1175ea844a1aSMatthew Knepley 
117640750d41SVaclav Hapla   Not Collective
1177ea844a1aSMatthew Knepley 
1178ea844a1aSMatthew Knepley   Input Parameters:
1179cab54364SBarry Smith + s     - the `PetscSection`
1180ea844a1aSMatthew Knepley . point - the point
1181ea844a1aSMatthew Knepley - field - the field
1182ea844a1aSMatthew Knepley 
1183ea844a1aSMatthew Knepley   Output Parameter:
1184ea844a1aSMatthew Knepley . numDof - the number of dof which are fixed by constraints
1185ea844a1aSMatthew Knepley 
1186ea844a1aSMatthew Knepley   Level: intermediate
1187ea844a1aSMatthew Knepley 
1188*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionGetDof()`, `PetscSectionSetFieldConstraintDof()`, `PetscSectionCreate()`
1189ea844a1aSMatthew Knepley @*/
1190d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldConstraintDof(PetscSection s, PetscInt point, PetscInt field, PetscInt *numDof)
1191d71ae5a4SJacob Faibussowitsch {
1192ea844a1aSMatthew Knepley   PetscFunctionBegin;
1193ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
11944f572ea9SToby Isaac   PetscAssertPointer(numDof, 4);
11952abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
11969566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstraintDof(s->field[field], point, numDof));
11973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1198ea844a1aSMatthew Knepley }
1199ea844a1aSMatthew Knepley 
1200ea844a1aSMatthew Knepley /*@
1201ea844a1aSMatthew Knepley   PetscSectionSetFieldConstraintDof - Set the number of constrained degrees of freedom associated with a given field on a point.
1202ea844a1aSMatthew Knepley 
120340750d41SVaclav Hapla   Not Collective
1204ea844a1aSMatthew Knepley 
1205ea844a1aSMatthew Knepley   Input Parameters:
1206cab54364SBarry Smith + s      - the `PetscSection`
1207ea844a1aSMatthew Knepley . point  - the point
1208ea844a1aSMatthew Knepley . field  - the field
1209ea844a1aSMatthew Knepley - numDof - the number of dof which are fixed by constraints
1210ea844a1aSMatthew Knepley 
1211ea844a1aSMatthew Knepley   Level: intermediate
1212ea844a1aSMatthew Knepley 
1213*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionSetDof()`, `PetscSectionGetFieldConstraintDof()`, `PetscSectionCreate()`
1214ea844a1aSMatthew Knepley @*/
1215d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFieldConstraintDof(PetscSection s, PetscInt point, PetscInt field, PetscInt numDof)
1216d71ae5a4SJacob Faibussowitsch {
1217ea844a1aSMatthew Knepley   PetscFunctionBegin;
1218ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
12192abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
12209566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetConstraintDof(s->field[field], point, numDof));
12213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1222ea844a1aSMatthew Knepley }
1223ea844a1aSMatthew Knepley 
1224ea844a1aSMatthew Knepley /*@
1225ea844a1aSMatthew Knepley   PetscSectionAddFieldConstraintDof - Increment the number of constrained degrees of freedom associated with a given field on a point.
1226ea844a1aSMatthew Knepley 
122740750d41SVaclav Hapla   Not Collective
1228ea844a1aSMatthew Knepley 
1229ea844a1aSMatthew Knepley   Input Parameters:
1230cab54364SBarry Smith + s      - the `PetscSection`
1231ea844a1aSMatthew Knepley . point  - the point
1232ea844a1aSMatthew Knepley . field  - the field
1233ea844a1aSMatthew Knepley - numDof - the number of additional dof which are fixed by constraints
1234ea844a1aSMatthew Knepley 
1235ea844a1aSMatthew Knepley   Level: intermediate
1236ea844a1aSMatthew Knepley 
1237*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionAddDof()`, `PetscSectionGetFieldConstraintDof()`, `PetscSectionCreate()`
1238ea844a1aSMatthew Knepley @*/
1239d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionAddFieldConstraintDof(PetscSection s, PetscInt point, PetscInt field, PetscInt numDof)
1240d71ae5a4SJacob Faibussowitsch {
1241ea844a1aSMatthew Knepley   PetscFunctionBegin;
1242ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
12432abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
12449566063dSJacob Faibussowitsch   PetscCall(PetscSectionAddConstraintDof(s->field[field], point, numDof));
12453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1246ea844a1aSMatthew Knepley }
1247ea844a1aSMatthew Knepley 
1248ea844a1aSMatthew Knepley /*@
1249ea844a1aSMatthew Knepley   PetscSectionSetUpBC - Setup the subsections describing boundary conditions.
1250ea844a1aSMatthew Knepley 
125140750d41SVaclav Hapla   Not Collective
1252ea844a1aSMatthew Knepley 
1253ea844a1aSMatthew Knepley   Input Parameter:
1254cab54364SBarry Smith . s - the `PetscSection`
1255ea844a1aSMatthew Knepley 
1256ea844a1aSMatthew Knepley   Level: advanced
1257ea844a1aSMatthew Knepley 
1258*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionSetUp()`, `PetscSectionCreate()`
1259ea844a1aSMatthew Knepley @*/
1260d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetUpBC(PetscSection s)
1261d71ae5a4SJacob Faibussowitsch {
1262ea844a1aSMatthew Knepley   PetscFunctionBegin;
1263ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1264ea844a1aSMatthew Knepley   if (s->bc) {
1265ea844a1aSMatthew Knepley     const PetscInt last = (s->bc->pEnd - s->bc->pStart) - 1;
1266ea844a1aSMatthew Knepley 
12679566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(s->bc));
1268ea17275aSJose E. Roman     if (last >= 0) PetscCall(PetscMalloc1(s->bc->atlasOff[last] + s->bc->atlasDof[last], &s->bcIndices));
1269ea17275aSJose E. Roman     else s->bcIndices = NULL;
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 
1289*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionSetPermutation()`
1290ea844a1aSMatthew Knepley @*/
1291d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetUp(PetscSection s)
1292d71ae5a4SJacob Faibussowitsch {
1293357d8704SBarry Smith   PetscInt        f;
1294ea844a1aSMatthew Knepley   const PetscInt *pind   = NULL;
12956497c311SBarry Smith   PetscCount      offset = 0;
1296ea844a1aSMatthew Knepley 
1297ea844a1aSMatthew Knepley   PetscFunctionBegin;
1298ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
12993ba16761SJacob Faibussowitsch   if (s->setup) PetscFunctionReturn(PETSC_SUCCESS);
1300ea844a1aSMatthew Knepley   s->setup = PETSC_TRUE;
1301ea844a1aSMatthew Knepley   /* Set offsets and field offsets for all points */
1302ea844a1aSMatthew Knepley   /*   Assume that all fields have the same chart */
130328b400f6SJacob Faibussowitsch   PetscCheck(s->includesConstraints, PETSC_COMM_SELF, PETSC_ERR_SUP, "PetscSectionSetUp is currently unsupported for includesConstraints = PETSC_TRUE");
13049566063dSJacob Faibussowitsch   if (s->perm) PetscCall(ISGetIndices(s->perm, &pind));
1305ea844a1aSMatthew Knepley   if (s->pointMajor) {
13066497c311SBarry Smith     PetscCount foff;
1307357d8704SBarry Smith     for (PetscInt p = 0; p < s->pEnd - s->pStart; ++p) {
1308ea844a1aSMatthew Knepley       const PetscInt q = pind ? pind[p] : p;
1309ea844a1aSMatthew Knepley 
1310ea844a1aSMatthew Knepley       /* Set point offset */
1311357d8704SBarry Smith       PetscCall(PetscIntCast(offset, &s->atlasOff[q]));
1312ea844a1aSMatthew Knepley       offset += s->atlasDof[q];
1313ea844a1aSMatthew Knepley       /* Set field offset */
1314ea844a1aSMatthew Knepley       for (f = 0, foff = s->atlasOff[q]; f < s->numFields; ++f) {
1315ea844a1aSMatthew Knepley         PetscSection sf = s->field[f];
1316ea844a1aSMatthew Knepley 
1317357d8704SBarry Smith         PetscCall(PetscIntCast(foff, &sf->atlasOff[q]));
1318ea844a1aSMatthew Knepley         foff += sf->atlasDof[q];
1319ea844a1aSMatthew Knepley       }
1320ea844a1aSMatthew Knepley     }
1321ea844a1aSMatthew Knepley   } else {
1322ea844a1aSMatthew Knepley     /* Set field offsets for all points */
1323ea844a1aSMatthew Knepley     for (f = 0; f < s->numFields; ++f) {
1324ea844a1aSMatthew Knepley       PetscSection sf = s->field[f];
1325ea844a1aSMatthew Knepley 
1326357d8704SBarry Smith       for (PetscInt p = 0; p < s->pEnd - s->pStart; ++p) {
1327ea844a1aSMatthew Knepley         const PetscInt q = pind ? pind[p] : p;
1328ea844a1aSMatthew Knepley 
1329357d8704SBarry Smith         PetscCall(PetscIntCast(offset, &sf->atlasOff[q]));
1330ea844a1aSMatthew Knepley         offset += sf->atlasDof[q];
1331ea844a1aSMatthew Knepley       }
1332ea844a1aSMatthew Knepley     }
1333ea844a1aSMatthew Knepley     /* Disable point offsets since these are unused */
1334357d8704SBarry Smith     for (PetscInt p = 0; p < s->pEnd - s->pStart; ++p) s->atlasOff[p] = -1;
1335ea844a1aSMatthew Knepley   }
13369566063dSJacob Faibussowitsch   if (s->perm) PetscCall(ISRestoreIndices(s->perm, &pind));
1337ea844a1aSMatthew Knepley   /* Setup BC sections */
13389566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUpBC(s));
13399566063dSJacob Faibussowitsch   for (f = 0; f < s->numFields; ++f) PetscCall(PetscSectionSetUpBC(s->field[f]));
13403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1341ea844a1aSMatthew Knepley }
1342ea844a1aSMatthew Knepley 
1343ea844a1aSMatthew Knepley /*@
134420662ed9SBarry Smith   PetscSectionGetMaxDof - Return the maximum number of degrees of freedom on any point in the `PetscSection`
1345ea844a1aSMatthew Knepley 
134640750d41SVaclav Hapla   Not Collective
1347ea844a1aSMatthew Knepley 
13482fe279fdSBarry Smith   Input Parameter:
1349cab54364SBarry Smith . s - the `PetscSection`
1350ea844a1aSMatthew Knepley 
1351ea844a1aSMatthew Knepley   Output Parameter:
1352ea844a1aSMatthew Knepley . maxDof - the maximum dof
1353ea844a1aSMatthew Knepley 
1354ea844a1aSMatthew Knepley   Level: intermediate
1355ea844a1aSMatthew Knepley 
1356583308b6SBarry Smith   Notes:
1357cab54364SBarry Smith   The returned number is up-to-date without need for `PetscSectionSetUp()`.
135869c11d05SVaclav Hapla 
1359583308b6SBarry 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
1360583308b6SBarry Smith   the maximum over all points of the value returned by `PetscSectionGetDof()` on this MPI process
1361583308b6SBarry Smith 
1362583308b6SBarry Smith   Developer Notes:
136369c11d05SVaclav Hapla   The returned number is calculated lazily and stashed.
1364cab54364SBarry Smith 
1365cab54364SBarry Smith   A call to `PetscSectionInvalidateMaxDof_Internal()` invalidates the stashed value.
1366cab54364SBarry Smith 
1367cab54364SBarry Smith   `PetscSectionInvalidateMaxDof_Internal()` is called in `PetscSectionSetDof()`, `PetscSectionAddDof()` and `PetscSectionReset()`
1368cab54364SBarry Smith 
136920662ed9SBarry Smith   It should also be called every time `atlasDof` is modified directly.
137069c11d05SVaclav Hapla 
1371*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionGetDof()`, `PetscSectionSetDof()`, `PetscSectionAddDof()`, `PetscSectionCreate()`
1372ea844a1aSMatthew Knepley @*/
1373d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetMaxDof(PetscSection s, PetscInt *maxDof)
1374d71ae5a4SJacob Faibussowitsch {
137569c11d05SVaclav Hapla   PetscInt p;
137669c11d05SVaclav Hapla 
1377ea844a1aSMatthew Knepley   PetscFunctionBegin;
1378ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
13794f572ea9SToby Isaac   PetscAssertPointer(maxDof, 2);
13801690c2aeSBarry Smith   if (s->maxDof == PETSC_INT_MIN) {
138169c11d05SVaclav Hapla     s->maxDof = 0;
1382ad540459SPierre Jolivet     for (p = 0; p < s->pEnd - s->pStart; ++p) s->maxDof = PetscMax(s->maxDof, s->atlasDof[p]);
138369c11d05SVaclav Hapla   }
1384ea844a1aSMatthew Knepley   *maxDof = s->maxDof;
13853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1386ea844a1aSMatthew Knepley }
1387ea844a1aSMatthew Knepley 
1388ea844a1aSMatthew Knepley /*@
138920662ed9SBarry Smith   PetscSectionGetStorageSize - Return the size of an array or local `Vec` capable of holding all the degrees of freedom defined in a `PetscSection`
1390ea844a1aSMatthew Knepley 
139140750d41SVaclav Hapla   Not Collective
1392ea844a1aSMatthew Knepley 
1393ea844a1aSMatthew Knepley   Input Parameter:
1394cab54364SBarry Smith . s - the `PetscSection`
1395ea844a1aSMatthew Knepley 
1396ea844a1aSMatthew Knepley   Output Parameter:
1397ea844a1aSMatthew Knepley . size - the size of an array which can hold all the dofs
1398ea844a1aSMatthew Knepley 
1399ea844a1aSMatthew Knepley   Level: intermediate
1400ea844a1aSMatthew Knepley 
1401*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionGetOffset()`, `PetscSectionGetConstrainedStorageSize()`, `PetscSectionCreate()`
1402ea844a1aSMatthew Knepley @*/
1403d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetStorageSize(PetscSection s, PetscInt *size)
1404d71ae5a4SJacob Faibussowitsch {
1405357d8704SBarry Smith   PetscInt64 n = 0;
1406ea844a1aSMatthew Knepley 
1407ea844a1aSMatthew Knepley   PetscFunctionBegin;
1408ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
14094f572ea9SToby Isaac   PetscAssertPointer(size, 2);
1410357d8704SBarry Smith   for (PetscInt p = 0; p < s->pEnd - s->pStart; ++p) n += s->atlasDof[p] > 0 ? s->atlasDof[p] : 0;
1411357d8704SBarry Smith   PetscCall(PetscIntCast(n, size));
14123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1413ea844a1aSMatthew Knepley }
1414ea844a1aSMatthew Knepley 
1415ea844a1aSMatthew Knepley /*@
141620662ed9SBarry Smith   PetscSectionGetConstrainedStorageSize - Return the size of an array or local `Vec` capable of holding all unconstrained degrees of freedom in a `PetscSection`
1417ea844a1aSMatthew Knepley 
141840750d41SVaclav Hapla   Not Collective
1419ea844a1aSMatthew Knepley 
1420064ec1f3Sprj-   Input Parameter:
1421cab54364SBarry Smith . s - the `PetscSection`
1422ea844a1aSMatthew Knepley 
1423ea844a1aSMatthew Knepley   Output Parameter:
1424ea844a1aSMatthew Knepley . size - the size of an array which can hold all unconstrained dofs
1425ea844a1aSMatthew Knepley 
1426ea844a1aSMatthew Knepley   Level: intermediate
1427ea844a1aSMatthew Knepley 
1428*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionGetStorageSize()`, `PetscSectionGetOffset()`, `PetscSectionCreate()`
1429ea844a1aSMatthew Knepley @*/
1430d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetConstrainedStorageSize(PetscSection s, PetscInt *size)
1431d71ae5a4SJacob Faibussowitsch {
1432357d8704SBarry Smith   PetscInt64 n = 0;
1433ea844a1aSMatthew Knepley 
1434ea844a1aSMatthew Knepley   PetscFunctionBegin;
1435ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
14364f572ea9SToby Isaac   PetscAssertPointer(size, 2);
1437357d8704SBarry Smith   for (PetscInt p = 0; p < s->pEnd - s->pStart; ++p) {
1438ea844a1aSMatthew Knepley     const PetscInt cdof = s->bc ? s->bc->atlasDof[p] : 0;
1439ea844a1aSMatthew Knepley     n += s->atlasDof[p] > 0 ? s->atlasDof[p] - cdof : 0;
1440ea844a1aSMatthew Knepley   }
1441357d8704SBarry Smith   PetscCall(PetscIntCast(n, size));
14423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1443ea844a1aSMatthew Knepley }
1444ea844a1aSMatthew Knepley 
1445ea844a1aSMatthew Knepley /*@
1446583308b6SBarry Smith   PetscSectionCreateGlobalSection - Create a parallel section describing the global layout using
1447583308b6SBarry Smith   a local (sequential) `PetscSection` on each MPI process and a `PetscSF` describing the section point overlap.
1448ea844a1aSMatthew Knepley 
1449ea844a1aSMatthew Knepley   Input Parameters:
1450cab54364SBarry Smith + s                  - The `PetscSection` for the local field layout
1451cab54364SBarry Smith . sf                 - The `PetscSF` describing parallel layout of the section points (leaves are unowned local points)
1452eb9d3e4dSMatthew G. Knepley . usePermutation     - By default this is `PETSC_TRUE`, meaning any permutation of the local section is transferred to the global section
1453cab54364SBarry Smith . includeConstraints - By default this is `PETSC_FALSE`, meaning that the global field vector will not possess constrained dofs
1454cab54364SBarry Smith - localOffsets       - If `PETSC_TRUE`, use local rather than global offsets for the points
1455ea844a1aSMatthew Knepley 
1456ea844a1aSMatthew Knepley   Output Parameter:
1457cab54364SBarry Smith . gsection - The `PetscSection` for the global field layout
1458ea844a1aSMatthew Knepley 
1459ea844a1aSMatthew Knepley   Level: intermediate
1460ea844a1aSMatthew Knepley 
1461db527f05SMatthew G. Knepley   Notes:
1462583308b6SBarry Smith   On each MPI process `gsection` inherits the chart of the `s` on that process.
1463db527f05SMatthew G. Knepley 
1464583308b6SBarry 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`.
1465583308b6SBarry Smith   In those locations the value of size is -(size+1) and the value of the offset on the remote process is -(off+1).
1466cab54364SBarry Smith 
1467*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionCreateGlobalSectionCensored()`
1468ea844a1aSMatthew Knepley @*/
1469eb9d3e4dSMatthew G. Knepley PetscErrorCode PetscSectionCreateGlobalSection(PetscSection s, PetscSF sf, PetscBool usePermutation, PetscBool includeConstraints, PetscBool localOffsets, PetscSection *gsection)
1470d71ae5a4SJacob Faibussowitsch {
1471ea844a1aSMatthew Knepley   PetscSection    gs;
1472ea844a1aSMatthew Knepley   const PetscInt *pind = NULL;
1473ea844a1aSMatthew Knepley   PetscInt       *recv = NULL, *neg = NULL;
1474357d8704SBarry Smith   PetscInt        pStart, pEnd, p, dof, cdof, off, globalOff = 0, nroots, nlocal, maxleaf;
1475046d2115Sksagiyam   PetscInt        numFields, f, numComponents;
14766497c311SBarry Smith   PetscCount      foff;
1477ea844a1aSMatthew Knepley 
1478ea844a1aSMatthew Knepley   PetscFunctionBegin;
1479ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1480ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
1481eb9d3e4dSMatthew G. Knepley   PetscValidLogicalCollectiveBool(s, usePermutation, 3);
1482eb9d3e4dSMatthew G. Knepley   PetscValidLogicalCollectiveBool(s, includeConstraints, 4);
1483eb9d3e4dSMatthew G. Knepley   PetscValidLogicalCollectiveBool(s, localOffsets, 5);
1484eb9d3e4dSMatthew G. Knepley   PetscAssertPointer(gsection, 6);
148528b400f6SJacob Faibussowitsch   PetscCheck(s->pointMajor, PETSC_COMM_SELF, PETSC_ERR_SUP, "No support for field major ordering");
14869566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s), &gs));
14879566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &numFields));
14889566063dSJacob Faibussowitsch   if (numFields > 0) PetscCall(PetscSectionSetNumFields(gs, numFields));
14899566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
14909566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(gs, pStart, pEnd));
149187e637c6Sksagiyam   gs->includesConstraints = includeConstraints;
14929566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, &nroots, NULL, NULL, NULL));
1493ea844a1aSMatthew Knepley   nlocal = nroots; /* The local/leaf space matches global/root space */
1494ea844a1aSMatthew Knepley   /* Must allocate for all points visible to SF, which may be more than this section */
1495ea844a1aSMatthew Knepley   if (nroots >= 0) { /* nroots < 0 means that the graph has not been set, only happens in serial */
14969566063dSJacob Faibussowitsch     PetscCall(PetscSFGetLeafRange(sf, NULL, &maxleaf));
149708401ef6SPierre Jolivet     PetscCheck(nroots >= pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "SF roots %" PetscInt_FMT " < pEnd %" PetscInt_FMT, nroots, pEnd);
149808401ef6SPierre Jolivet     PetscCheck(maxleaf < nroots, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Max local leaf %" PetscInt_FMT " >= nroots %" PetscInt_FMT, maxleaf, nroots);
14999566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(nroots, &neg, nlocal, &recv));
15009566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(neg, nroots));
1501ea844a1aSMatthew Knepley   }
1502ea844a1aSMatthew Knepley   /* Mark all local points with negative dof */
1503ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
15049566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s, p, &dof));
15059566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(gs, p, dof));
15069566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
15079566063dSJacob Faibussowitsch     if (!includeConstraints && cdof > 0) PetscCall(PetscSectionSetConstraintDof(gs, p, cdof));
1508ea844a1aSMatthew Knepley     if (neg) neg[p] = -(dof + 1);
1509ea844a1aSMatthew Knepley   }
15109566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUpBC(gs));
15119566063dSJacob 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]));
1512ea844a1aSMatthew Knepley   if (nroots >= 0) {
15139566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(recv, nlocal));
15149566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, neg, recv, MPI_REPLACE));
15159566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, neg, recv, MPI_REPLACE));
1516ea844a1aSMatthew Knepley     for (p = pStart; p < pEnd; ++p) {
1517ea844a1aSMatthew Knepley       if (recv[p] < 0) {
1518ea844a1aSMatthew Knepley         gs->atlasDof[p - pStart] = recv[p];
15199566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
152008401ef6SPierre 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);
1521ea844a1aSMatthew Knepley       }
1522ea844a1aSMatthew Knepley     }
1523ea844a1aSMatthew Knepley   }
1524ea844a1aSMatthew Knepley   /* Calculate new sizes, get process offset, and calculate point offsets */
1525eb9d3e4dSMatthew G. Knepley   if (usePermutation && s->perm) PetscCall(ISGetIndices(s->perm, &pind));
1526ea844a1aSMatthew Knepley   for (p = 0, off = 0; p < pEnd - pStart; ++p) {
1527ea844a1aSMatthew Knepley     const PetscInt q = pind ? pind[p] : p;
1528ea844a1aSMatthew Knepley 
1529ea844a1aSMatthew Knepley     cdof            = (!includeConstraints && s->bc) ? s->bc->atlasDof[q] : 0;
1530ea844a1aSMatthew Knepley     gs->atlasOff[q] = off;
1531ea844a1aSMatthew Knepley     off += gs->atlasDof[q] > 0 ? gs->atlasDof[q] - cdof : 0;
1532ea844a1aSMatthew Knepley   }
1533ea844a1aSMatthew Knepley   if (!localOffsets) {
15349566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Scan(&off, &globalOff, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)s)));
1535ea844a1aSMatthew Knepley     globalOff -= off;
1536ea844a1aSMatthew Knepley   }
1537ea844a1aSMatthew Knepley   for (p = pStart, off = 0; p < pEnd; ++p) {
1538ea844a1aSMatthew Knepley     gs->atlasOff[p - pStart] += globalOff;
1539ea844a1aSMatthew Knepley     if (neg) neg[p] = -(gs->atlasOff[p - pStart] + 1);
1540ea844a1aSMatthew Knepley   }
1541eb9d3e4dSMatthew G. Knepley   if (usePermutation && s->perm) PetscCall(ISRestoreIndices(s->perm, &pind));
1542ea844a1aSMatthew Knepley   /* Put in negative offsets for ghost points */
1543ea844a1aSMatthew Knepley   if (nroots >= 0) {
15449566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(recv, nlocal));
15459566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, neg, recv, MPI_REPLACE));
15469566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, neg, recv, MPI_REPLACE));
1547ea844a1aSMatthew Knepley     for (p = pStart; p < pEnd; ++p) {
1548ea844a1aSMatthew Knepley       if (recv[p] < 0) gs->atlasOff[p - pStart] = recv[p];
1549ea844a1aSMatthew Knepley     }
1550ea844a1aSMatthew Knepley   }
15519566063dSJacob Faibussowitsch   PetscCall(PetscFree2(neg, recv));
1552046d2115Sksagiyam   /* Set field dofs/offsets/constraints */
1553046d2115Sksagiyam   for (f = 0; f < numFields; ++f) {
15544cd8913cSStefano Zampini     const char *name;
15554cd8913cSStefano Zampini 
1556046d2115Sksagiyam     gs->field[f]->includesConstraints = includeConstraints;
15579566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &numComponents));
15589566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(gs, f, numComponents));
15594cd8913cSStefano Zampini     PetscCall(PetscSectionGetFieldName(s, f, &name));
15604cd8913cSStefano Zampini     PetscCall(PetscSectionSetFieldName(gs, f, name));
1561046d2115Sksagiyam   }
1562046d2115Sksagiyam   for (p = pStart; p < pEnd; ++p) {
15639566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(gs, p, &off));
1564046d2115Sksagiyam     for (f = 0, foff = off; f < numFields; ++f) {
15659566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(s, p, f, &cdof));
15669566063dSJacob Faibussowitsch       if (!includeConstraints && cdof > 0) PetscCall(PetscSectionSetFieldConstraintDof(gs, p, f, cdof));
15679566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
15689566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(gs, p, f, off < 0 ? -(dof + 1) : dof));
15696497c311SBarry Smith       PetscCall(PetscSectionSetFieldOffset(gs, p, f, (PetscInt)foff));
15709566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(gs, p, f, &cdof));
1571046d2115Sksagiyam       foff = off < 0 ? foff - (dof - cdof) : foff + (dof - cdof);
1572357d8704SBarry Smith       PetscCheck(foff < PETSC_INT_MAX, PETSC_COMM_SELF, PETSC_ERR_INT_OVERFLOW, "Offsets too large for 32 bit indices");
1573046d2115Sksagiyam     }
1574046d2115Sksagiyam   }
1575046d2115Sksagiyam   for (f = 0; f < numFields; ++f) {
1576046d2115Sksagiyam     PetscSection gfs = gs->field[f];
1577046d2115Sksagiyam 
15789566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUpBC(gfs));
15799566063dSJacob 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]));
1580046d2115Sksagiyam   }
1581046d2115Sksagiyam   gs->setup = PETSC_TRUE;
15829566063dSJacob Faibussowitsch   PetscCall(PetscSectionViewFromOptions(gs, NULL, "-global_section_view"));
1583ea844a1aSMatthew Knepley   *gsection = gs;
15843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1585ea844a1aSMatthew Knepley }
1586ea844a1aSMatthew Knepley 
1587ea844a1aSMatthew Knepley /*@
1588583308b6SBarry Smith   PetscSectionCreateGlobalSectionCensored - Create a `PetscSection` describing the globallayout using
1589583308b6SBarry Smith   a local (sequential) `PetscSection` on each MPI process and an `PetscSF` describing the section point overlap.
1590ea844a1aSMatthew Knepley 
1591ea844a1aSMatthew Knepley   Input Parameters:
1592cab54364SBarry Smith + s                  - The `PetscSection` for the local field layout
1593cab54364SBarry Smith . sf                 - The `PetscSF` describing parallel layout of the section points
1594583308b6SBarry Smith . includeConstraints - By default this is `PETSC_FALSE`, meaning that the global vector will not possess constrained dofs
1595583308b6SBarry Smith . numExcludes        - The number of exclusion ranges, this must have the same value on all MPI processes
1596583308b6SBarry 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
1597ea844a1aSMatthew Knepley 
1598ea844a1aSMatthew Knepley   Output Parameter:
1599cab54364SBarry Smith . gsection - The `PetscSection` for the global field layout
1600ea844a1aSMatthew Knepley 
1601ea844a1aSMatthew Knepley   Level: advanced
1602ea844a1aSMatthew Knepley 
1603583308b6SBarry Smith   Notes:
1604583308b6SBarry Smith   On each MPI process `gsection` inherits the chart of the `s` on that process.
160520662ed9SBarry Smith 
1606583308b6SBarry 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`.
1607583308b6SBarry Smith   In those locations the value of size is -(size+1) and the value of the offset on the remote process is -(off+1).
1608583308b6SBarry Smith 
1609583308b6SBarry Smith   This routine augments `PetscSectionCreateGlobalSection()` by allowing one to exclude certain ranges in the chart of the `PetscSection`
1610cab54364SBarry Smith 
161138b5cf2dSJacob Faibussowitsch   Developer Notes:
161220662ed9SBarry Smith   This is a terrible function name
161320662ed9SBarry Smith 
1614*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionCreate()`
1615ea844a1aSMatthew Knepley @*/
1616d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCreateGlobalSectionCensored(PetscSection s, PetscSF sf, PetscBool includeConstraints, PetscInt numExcludes, const PetscInt excludes[], PetscSection *gsection)
1617d71ae5a4SJacob Faibussowitsch {
1618ea844a1aSMatthew Knepley   const PetscInt *pind = NULL;
1619ea844a1aSMatthew Knepley   PetscInt       *neg = NULL, *tmpOff = NULL;
1620357d8704SBarry Smith   PetscInt        pStart, pEnd, p, e, dof, cdof, globalOff = 0, nroots;
16216497c311SBarry Smith   PetscCount      off;
1622ea844a1aSMatthew Knepley 
1623ea844a1aSMatthew Knepley   PetscFunctionBegin;
1624ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1625ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
16264f572ea9SToby Isaac   PetscAssertPointer(gsection, 6);
16279566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s), gsection));
16289566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
16299566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(*gsection, pStart, pEnd));
16309566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, &nroots, NULL, NULL, NULL));
1631ea844a1aSMatthew Knepley   if (nroots >= 0) {
163208401ef6SPierre Jolivet     PetscCheck(nroots >= pEnd - pStart, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "PetscSF nroots %" PetscInt_FMT " < %" PetscInt_FMT " section size", nroots, pEnd - pStart);
16339566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(nroots, &neg));
1634ea844a1aSMatthew Knepley     if (nroots > pEnd - pStart) {
16359566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(nroots, &tmpOff));
1636ea844a1aSMatthew Knepley     } else {
1637ea844a1aSMatthew Knepley       tmpOff = &(*gsection)->atlasDof[-pStart];
1638ea844a1aSMatthew Knepley     }
1639ea844a1aSMatthew Knepley   }
1640ea844a1aSMatthew Knepley   /* Mark ghost points with negative dof */
1641ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
1642ea844a1aSMatthew Knepley     for (e = 0; e < numExcludes; ++e) {
1643ea844a1aSMatthew Knepley       if ((p >= excludes[e * 2 + 0]) && (p < excludes[e * 2 + 1])) {
16449566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetDof(*gsection, p, 0));
1645ea844a1aSMatthew Knepley         break;
1646ea844a1aSMatthew Knepley       }
1647ea844a1aSMatthew Knepley     }
1648ea844a1aSMatthew Knepley     if (e < numExcludes) continue;
16499566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s, p, &dof));
16509566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(*gsection, p, dof));
16519566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
16529566063dSJacob Faibussowitsch     if (!includeConstraints && cdof > 0) PetscCall(PetscSectionSetConstraintDof(*gsection, p, cdof));
1653ea844a1aSMatthew Knepley     if (neg) neg[p] = -(dof + 1);
1654ea844a1aSMatthew Knepley   }
16559566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUpBC(*gsection));
1656ea844a1aSMatthew Knepley   if (nroots >= 0) {
16579566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, neg, tmpOff, MPI_REPLACE));
16589566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, neg, tmpOff, MPI_REPLACE));
1659ea844a1aSMatthew Knepley     if (nroots > pEnd - pStart) {
16609371c9d4SSatish Balay       for (p = pStart; p < pEnd; ++p) {
16619371c9d4SSatish Balay         if (tmpOff[p] < 0) (*gsection)->atlasDof[p - pStart] = tmpOff[p];
16629371c9d4SSatish Balay       }
1663ea844a1aSMatthew Knepley     }
1664ea844a1aSMatthew Knepley   }
166535cb6cd3SPierre Jolivet   /* Calculate new sizes, get process offset, and calculate point offsets */
16669566063dSJacob Faibussowitsch   if (s->perm) PetscCall(ISGetIndices(s->perm, &pind));
1667ea844a1aSMatthew Knepley   for (p = 0, off = 0; p < pEnd - pStart; ++p) {
1668ea844a1aSMatthew Knepley     const PetscInt q = pind ? pind[p] : p;
1669ea844a1aSMatthew Knepley 
1670ea844a1aSMatthew Knepley     cdof                     = (!includeConstraints && s->bc) ? s->bc->atlasDof[q] : 0;
16716497c311SBarry Smith     (*gsection)->atlasOff[q] = (PetscInt)off;
1672ea844a1aSMatthew Knepley     off += (*gsection)->atlasDof[q] > 0 ? (*gsection)->atlasDof[q] - cdof : 0;
1673357d8704SBarry Smith     PetscCheck(off < PETSC_INT_MAX, PETSC_COMM_SELF, PETSC_ERR_INT_OVERFLOW, "Offsets too large for 32 bit indices");
16746497c311SBarry Smith   }
16759566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Scan(&off, &globalOff, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)s)));
1676ea844a1aSMatthew Knepley   globalOff -= off;
1677ea844a1aSMatthew Knepley   for (p = 0, off = 0; p < pEnd - pStart; ++p) {
1678ea844a1aSMatthew Knepley     (*gsection)->atlasOff[p] += globalOff;
1679ea844a1aSMatthew Knepley     if (neg) neg[p + pStart] = -((*gsection)->atlasOff[p] + 1);
1680ea844a1aSMatthew Knepley   }
16819566063dSJacob Faibussowitsch   if (s->perm) PetscCall(ISRestoreIndices(s->perm, &pind));
1682ea844a1aSMatthew Knepley   /* Put in negative offsets for ghost points */
1683ea844a1aSMatthew Knepley   if (nroots >= 0) {
1684ea844a1aSMatthew Knepley     if (nroots == pEnd - pStart) tmpOff = &(*gsection)->atlasOff[-pStart];
16859566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, neg, tmpOff, MPI_REPLACE));
16869566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, neg, tmpOff, MPI_REPLACE));
1687ea844a1aSMatthew Knepley     if (nroots > pEnd - pStart) {
16889371c9d4SSatish Balay       for (p = pStart; p < pEnd; ++p) {
16899371c9d4SSatish Balay         if (tmpOff[p] < 0) (*gsection)->atlasOff[p - pStart] = tmpOff[p];
16909371c9d4SSatish Balay       }
1691ea844a1aSMatthew Knepley     }
1692ea844a1aSMatthew Knepley   }
16939566063dSJacob Faibussowitsch   if (nroots >= 0 && nroots > pEnd - pStart) PetscCall(PetscFree(tmpOff));
16949566063dSJacob Faibussowitsch   PetscCall(PetscFree(neg));
16953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1696ea844a1aSMatthew Knepley }
1697ea844a1aSMatthew Knepley 
1698ea844a1aSMatthew Knepley /*@
1699583308b6SBarry Smith   PetscSectionGetPointLayout - Get a `PetscLayout` for the points with nonzero dof counts of the unnamed default field within this `PetscSection`s local chart
1700ea844a1aSMatthew Knepley 
1701cab54364SBarry Smith   Collective
1702ea844a1aSMatthew Knepley 
1703ea844a1aSMatthew Knepley   Input Parameters:
1704cab54364SBarry Smith + comm - The `MPI_Comm`
1705cab54364SBarry Smith - s    - The `PetscSection`
1706ea844a1aSMatthew Knepley 
1707ea844a1aSMatthew Knepley   Output Parameter:
170820662ed9SBarry Smith . layout - The point layout for the data that defines the section
1709ea844a1aSMatthew Knepley 
1710ea844a1aSMatthew Knepley   Level: advanced
1711ea844a1aSMatthew Knepley 
171220662ed9SBarry Smith   Notes:
1713583308b6SBarry Smith   `PetscSectionGetValueLayout()` provides similar information but counting the total number of degrees of freedom on the MPI process (excluding constrained
1714583308b6SBarry Smith   degrees of freedom).
171520662ed9SBarry Smith 
1716583308b6SBarry Smith   This count includes constrained degrees of freedom
1717583308b6SBarry Smith 
1718583308b6SBarry Smith   This is usually called on the default global section.
1719583308b6SBarry Smith 
1720583308b6SBarry Smith   Example:
1721583308b6SBarry Smith .vb
1722583308b6SBarry Smith      The chart is [2,5), point 2 has 2 dof, point 3 has 0 dof, point 4 has 1 dof
1723583308b6SBarry Smith      The local size of the `PetscLayout` is 2 since 2 points have a non-zero number of dof
1724583308b6SBarry Smith .ve
1725583308b6SBarry Smith 
172638b5cf2dSJacob Faibussowitsch   Developer Notes:
1727583308b6SBarry Smith   I find the names of these two functions extremely non-informative
1728cab54364SBarry Smith 
1729*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionGetValueLayout()`, `PetscSectionCreate()`
1730ea844a1aSMatthew Knepley @*/
1731d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetPointLayout(MPI_Comm comm, PetscSection s, PetscLayout *layout)
1732d71ae5a4SJacob Faibussowitsch {
1733ea844a1aSMatthew Knepley   PetscInt pStart, pEnd, p, localSize = 0;
1734ea844a1aSMatthew Knepley 
1735ea844a1aSMatthew Knepley   PetscFunctionBegin;
17369566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
1737ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
1738ea844a1aSMatthew Knepley     PetscInt dof;
1739ea844a1aSMatthew Knepley 
17409566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s, p, &dof));
17419120ba30SVaclav Hapla     if (dof >= 0) ++localSize;
1742ea844a1aSMatthew Knepley   }
17439566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, layout));
17449566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(*layout, localSize));
17459566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(*layout, 1));
17469566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(*layout));
17473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1748ea844a1aSMatthew Knepley }
1749ea844a1aSMatthew Knepley 
1750ea844a1aSMatthew Knepley /*@
175120662ed9SBarry Smith   PetscSectionGetValueLayout - Get the `PetscLayout` associated with the section dofs of a `PetscSection`
1752ea844a1aSMatthew Knepley 
1753cab54364SBarry Smith   Collective
1754ea844a1aSMatthew Knepley 
1755ea844a1aSMatthew Knepley   Input Parameters:
1756cab54364SBarry Smith + comm - The `MPI_Comm`
1757cab54364SBarry Smith - s    - The `PetscSection`
1758ea844a1aSMatthew Knepley 
1759ea844a1aSMatthew Knepley   Output Parameter:
1760ea844a1aSMatthew Knepley . layout - The dof layout for the section
1761ea844a1aSMatthew Knepley 
1762ea844a1aSMatthew Knepley   Level: advanced
1763ea844a1aSMatthew Knepley 
176420662ed9SBarry Smith   Notes:
1765583308b6SBarry Smith   `PetscSectionGetPointLayout()` provides similar information but only counting the number of points with nonzero degrees of freedom and
1766583308b6SBarry Smith   including the constrained degrees of freedom
176720662ed9SBarry Smith 
1768cab54364SBarry Smith   This is usually called for the default global section.
1769cab54364SBarry Smith 
1770583308b6SBarry Smith   Example:
1771583308b6SBarry Smith .vb
1772583308b6SBarry 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)
1773583308b6SBarry Smith      The local size of the `PetscLayout` is 3 since there are 3 unconstrained degrees of freedom on this MPI process
1774583308b6SBarry Smith .ve
1775583308b6SBarry Smith 
1776*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionGetPointLayout()`, `PetscSectionCreate()`
1777ea844a1aSMatthew Knepley @*/
1778d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetValueLayout(MPI_Comm comm, PetscSection s, PetscLayout *layout)
1779d71ae5a4SJacob Faibussowitsch {
1780ea844a1aSMatthew Knepley   PetscInt pStart, pEnd, p, localSize = 0;
1781ea844a1aSMatthew Knepley 
1782ea844a1aSMatthew Knepley   PetscFunctionBegin;
1783ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 2);
17844f572ea9SToby Isaac   PetscAssertPointer(layout, 3);
17859566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
1786ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
1787ea844a1aSMatthew Knepley     PetscInt dof, cdof;
1788ea844a1aSMatthew Knepley 
17899566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s, p, &dof));
17909566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
1791ea844a1aSMatthew Knepley     if (dof - cdof > 0) localSize += dof - cdof;
1792ea844a1aSMatthew Knepley   }
17939566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, layout));
17949566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(*layout, localSize));
17959566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(*layout, 1));
17969566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(*layout));
17973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1798ea844a1aSMatthew Knepley }
1799ea844a1aSMatthew Knepley 
1800ea844a1aSMatthew Knepley /*@
1801db527f05SMatthew G. Knepley   PetscSectionGetOffset - Return the offset into an array or `Vec` for the dof associated with the given point.
1802ea844a1aSMatthew Knepley 
180340750d41SVaclav Hapla   Not Collective
1804ea844a1aSMatthew Knepley 
1805ea844a1aSMatthew Knepley   Input Parameters:
1806cab54364SBarry Smith + s     - the `PetscSection`
1807ea844a1aSMatthew Knepley - point - the point
1808ea844a1aSMatthew Knepley 
1809ea844a1aSMatthew Knepley   Output Parameter:
1810ea844a1aSMatthew Knepley . offset - the offset
1811ea844a1aSMatthew Knepley 
1812ea844a1aSMatthew Knepley   Level: intermediate
1813ea844a1aSMatthew Knepley 
1814583308b6SBarry Smith   Notes:
1815376335faSBarry Smith   In a global section, `offset` will be negative for points not owned by this process.
1816583308b6SBarry Smith 
1817583308b6SBarry Smith   This is for the unnamed default field in the `PetscSection` not the named fields
1818583308b6SBarry Smith 
1819583308b6SBarry Smith   The `offset` values are different depending on a value set with `PetscSectionSetPointMajor()`
1820583308b6SBarry Smith 
1821*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionGetFieldOffset()`, `PetscSectionCreate()`, `PetscSectionSetPointMajor()`
1822ea844a1aSMatthew Knepley @*/
1823d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetOffset(PetscSection s, PetscInt point, PetscInt *offset)
1824d71ae5a4SJacob Faibussowitsch {
1825ea844a1aSMatthew Knepley   PetscFunctionBegin;
1826ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
18274f572ea9SToby Isaac   PetscAssertPointer(offset, 3);
1828b498ca8aSPierre 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);
1829ea844a1aSMatthew Knepley   *offset = s->atlasOff[point - s->pStart];
18303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1831ea844a1aSMatthew Knepley }
1832ea844a1aSMatthew Knepley 
1833ea844a1aSMatthew Knepley /*@
1834db527f05SMatthew G. Knepley   PetscSectionSetOffset - Set the offset into an array or `Vec` for the dof associated with the given point.
1835ea844a1aSMatthew Knepley 
183640750d41SVaclav Hapla   Not Collective
1837ea844a1aSMatthew Knepley 
1838ea844a1aSMatthew Knepley   Input Parameters:
1839cab54364SBarry Smith + s      - the `PetscSection`
1840ea844a1aSMatthew Knepley . point  - the point
1841376335faSBarry Smith - offset - the offset, these values may be negative indicating the values are off process
1842ea844a1aSMatthew Knepley 
1843583308b6SBarry Smith   Level: developer
1844ea844a1aSMatthew Knepley 
1845cab54364SBarry Smith   Note:
1846cab54364SBarry Smith   The user usually does not call this function, but uses `PetscSectionSetUp()`
1847cab54364SBarry Smith 
1848*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionGetFieldOffset()`, `PetscSectionCreate()`, `PetscSectionSetUp()`
1849ea844a1aSMatthew Knepley @*/
1850d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetOffset(PetscSection s, PetscInt point, PetscInt offset)
1851d71ae5a4SJacob Faibussowitsch {
1852ea844a1aSMatthew Knepley   PetscFunctionBegin;
1853ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
185408401ef6SPierre 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);
1855ea844a1aSMatthew Knepley   s->atlasOff[point - s->pStart] = offset;
18563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1857ea844a1aSMatthew Knepley }
1858ea844a1aSMatthew Knepley 
1859ea844a1aSMatthew Knepley /*@
1860db527f05SMatthew G. Knepley   PetscSectionGetFieldOffset - Return the offset into an array or `Vec` for the field dof associated with the given point.
1861ea844a1aSMatthew Knepley 
186240750d41SVaclav Hapla   Not Collective
1863ea844a1aSMatthew Knepley 
1864ea844a1aSMatthew Knepley   Input Parameters:
1865cab54364SBarry Smith + s     - the `PetscSection`
1866ea844a1aSMatthew Knepley . point - the point
1867ea844a1aSMatthew Knepley - field - the field
1868ea844a1aSMatthew Knepley 
1869ea844a1aSMatthew Knepley   Output Parameter:
1870ea844a1aSMatthew Knepley . offset - the offset
1871ea844a1aSMatthew Knepley 
1872ea844a1aSMatthew Knepley   Level: intermediate
1873ea844a1aSMatthew Knepley 
1874583308b6SBarry Smith   Notes:
1875376335faSBarry Smith   In a global section, `offset` will be negative for points not owned by this process.
1876583308b6SBarry Smith 
1877583308b6SBarry Smith   The `offset` values are different depending on a value set with `PetscSectionSetPointMajor()`
1878583308b6SBarry Smith 
1879*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionGetOffset()`, `PetscSectionCreate()`, `PetscSectionGetFieldPointOffset()`
1880ea844a1aSMatthew Knepley @*/
1881d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldOffset(PetscSection s, PetscInt point, PetscInt field, PetscInt *offset)
1882d71ae5a4SJacob Faibussowitsch {
1883ea844a1aSMatthew Knepley   PetscFunctionBegin;
1884ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
18854f572ea9SToby Isaac   PetscAssertPointer(offset, 4);
18862abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
18879566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(s->field[field], point, offset));
18883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1889ea844a1aSMatthew Knepley }
1890ea844a1aSMatthew Knepley 
1891ea844a1aSMatthew Knepley /*@
1892db527f05SMatthew G. Knepley   PetscSectionSetFieldOffset - Set the offset into an array or `Vec` for the dof associated with the given field at a point.
1893ea844a1aSMatthew Knepley 
189440750d41SVaclav Hapla   Not Collective
1895ea844a1aSMatthew Knepley 
1896ea844a1aSMatthew Knepley   Input Parameters:
189720662ed9SBarry Smith + s      - the `PetscSection`
1898ea844a1aSMatthew Knepley . point  - the point
1899ea844a1aSMatthew Knepley . field  - the field
1900376335faSBarry Smith - offset - the offset, these values may be negative indicating the values are off process
1901ea844a1aSMatthew Knepley 
1902cab54364SBarry Smith   Level: developer
1903ea844a1aSMatthew Knepley 
1904cab54364SBarry Smith   Note:
1905cab54364SBarry Smith   The user usually does not call this function, but uses `PetscSectionSetUp()`
1906ea844a1aSMatthew Knepley 
1907*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionGetFieldOffset()`, `PetscSectionSetOffset()`, `PetscSectionCreate()`, `PetscSectionSetUp()`
1908ea844a1aSMatthew Knepley @*/
1909d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFieldOffset(PetscSection s, PetscInt point, PetscInt field, PetscInt offset)
1910d71ae5a4SJacob Faibussowitsch {
1911ea844a1aSMatthew Knepley   PetscFunctionBegin;
1912ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
19132abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
19149566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetOffset(s->field[field], point, offset));
19153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1916ea844a1aSMatthew Knepley }
1917ea844a1aSMatthew Knepley 
1918ea844a1aSMatthew Knepley /*@
1919583308b6SBarry Smith   PetscSectionGetFieldPointOffset - Return the offset for the first field dof associated with the given point relative to the offset for that point for the
1920583308b6SBarry Smith   unnamed default field's first dof
1921ea844a1aSMatthew Knepley 
192240750d41SVaclav Hapla   Not Collective
1923ea844a1aSMatthew Knepley 
1924ea844a1aSMatthew Knepley   Input Parameters:
1925cab54364SBarry Smith + s     - the `PetscSection`
1926ea844a1aSMatthew Knepley . point - the point
1927ea844a1aSMatthew Knepley - field - the field
1928ea844a1aSMatthew Knepley 
1929ea844a1aSMatthew Knepley   Output Parameter:
1930ea844a1aSMatthew Knepley . offset - the offset
1931ea844a1aSMatthew Knepley 
1932ea844a1aSMatthew Knepley   Level: advanced
1933ea844a1aSMatthew Knepley 
1934cab54364SBarry Smith   Note:
1935583308b6SBarry Smith   This ignores constraints
1936cab54364SBarry Smith 
1937583308b6SBarry Smith   Example:
1938583308b6SBarry Smith .vb
1939583308b6SBarry Smith   if PetscSectionSetPointMajor(s,PETSC_TRUE)
1940583308b6SBarry Smith   The unnamed default field has 3 dof at `point`
1941583308b6SBarry Smith   Field 0 has 2 dof at `point`
1942583308b6SBarry Smith   Then PetscSectionGetFieldPointOffset(s,point,1,&offset) returns and offset of 5
1943583308b6SBarry Smith .ve
1944583308b6SBarry Smith 
1945*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionGetOffset()`, `PetscSectionCreate()`, `PetscSectionGetFieldOffset()`
1946ea844a1aSMatthew Knepley @*/
1947d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldPointOffset(PetscSection s, PetscInt point, PetscInt field, PetscInt *offset)
1948d71ae5a4SJacob Faibussowitsch {
1949ea844a1aSMatthew Knepley   PetscInt off, foff;
1950ea844a1aSMatthew Knepley 
1951ea844a1aSMatthew Knepley   PetscFunctionBegin;
1952ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
19534f572ea9SToby Isaac   PetscAssertPointer(offset, 4);
19542abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
19559566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(s, point, &off));
19569566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetOffset(s->field[field], point, &foff));
1957ea844a1aSMatthew Knepley   *offset = foff - off;
19583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1959ea844a1aSMatthew Knepley }
1960ea844a1aSMatthew Knepley 
1961ea844a1aSMatthew Knepley /*@
196220662ed9SBarry Smith   PetscSectionGetOffsetRange - Return the full range of offsets [`start`, `end`) for a `PetscSection`
1963ea844a1aSMatthew Knepley 
196440750d41SVaclav Hapla   Not Collective
1965ea844a1aSMatthew Knepley 
1966ea844a1aSMatthew Knepley   Input Parameter:
1967cab54364SBarry Smith . s - the `PetscSection`
1968ea844a1aSMatthew Knepley 
1969ea844a1aSMatthew Knepley   Output Parameters:
1970ea844a1aSMatthew Knepley + start - the minimum offset
1971ea844a1aSMatthew Knepley - end   - one more than the maximum offset
1972ea844a1aSMatthew Knepley 
1973ea844a1aSMatthew Knepley   Level: intermediate
1974ea844a1aSMatthew Knepley 
1975*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionGetOffset()`, `PetscSectionCreate()`
1976ea844a1aSMatthew Knepley @*/
1977d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetOffsetRange(PetscSection s, PetscInt *start, PetscInt *end)
1978d71ae5a4SJacob Faibussowitsch {
1979ea844a1aSMatthew Knepley   PetscInt os = 0, oe = 0, pStart, pEnd, p;
1980ea844a1aSMatthew Knepley 
1981ea844a1aSMatthew Knepley   PetscFunctionBegin;
1982ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
19839371c9d4SSatish Balay   if (s->atlasOff) {
19849371c9d4SSatish Balay     os = s->atlasOff[0];
19859371c9d4SSatish Balay     oe = s->atlasOff[0];
19869371c9d4SSatish Balay   }
19879566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
1988ea844a1aSMatthew Knepley   for (p = 0; p < pEnd - pStart; ++p) {
1989ea844a1aSMatthew Knepley     PetscInt dof = s->atlasDof[p], off = s->atlasOff[p];
1990ea844a1aSMatthew Knepley 
1991ea844a1aSMatthew Knepley     if (off >= 0) {
1992ea844a1aSMatthew Knepley       os = PetscMin(os, off);
1993ea844a1aSMatthew Knepley       oe = PetscMax(oe, off + dof);
1994ea844a1aSMatthew Knepley     }
1995ea844a1aSMatthew Knepley   }
1996ea844a1aSMatthew Knepley   if (start) *start = os;
1997ea844a1aSMatthew Knepley   if (end) *end = oe;
19983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1999ea844a1aSMatthew Knepley }
2000ea844a1aSMatthew Knepley 
2001ea844a1aSMatthew Knepley /*@
2002583308b6SBarry Smith   PetscSectionCreateSubsection - Create a new, smaller `PetscSection` composed of only selected fields
2003ea844a1aSMatthew Knepley 
200440750d41SVaclav Hapla   Collective
2005ea844a1aSMatthew Knepley 
2006d8d19677SJose E. Roman   Input Parameters:
2007cab54364SBarry Smith + s      - the `PetscSection`
2008ea844a1aSMatthew Knepley . len    - the number of subfields
2009ea844a1aSMatthew Knepley - fields - the subfield numbers
2010ea844a1aSMatthew Knepley 
2011ea844a1aSMatthew Knepley   Output Parameter:
2012ea844a1aSMatthew Knepley . subs - the subsection
2013ea844a1aSMatthew Knepley 
2014ea844a1aSMatthew Knepley   Level: advanced
2015ea844a1aSMatthew Knepley 
2016cab54364SBarry Smith   Notes:
2017583308b6SBarry Smith   The chart of `subs` is the same as the chart of `s`
2018cab54364SBarry Smith 
2019cab54364SBarry Smith   This will error if a fieldnumber is out of range
2020cab54364SBarry Smith 
2021*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionCreateSupersection()`, `PetscSectionCreate()`
2022ea844a1aSMatthew Knepley @*/
2023d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCreateSubsection(PetscSection s, PetscInt len, const PetscInt fields[], PetscSection *subs)
2024d71ae5a4SJacob Faibussowitsch {
2025b778fa18SValeria Barra   PetscInt nF, f, c, pStart, pEnd, p, maxCdof = 0;
2026ea844a1aSMatthew Knepley 
2027ea844a1aSMatthew Knepley   PetscFunctionBegin;
20283ba16761SJacob Faibussowitsch   if (!len) PetscFunctionReturn(PETSC_SUCCESS);
2029ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
20304f572ea9SToby Isaac   PetscAssertPointer(fields, 3);
20314f572ea9SToby Isaac   PetscAssertPointer(subs, 4);
20329566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &nF));
203308401ef6SPierre 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);
20349566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s), subs));
20359566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(*subs, len));
2036ea844a1aSMatthew Knepley   for (f = 0; f < len; ++f) {
2037ea844a1aSMatthew Knepley     const char     *name    = NULL;
2038ea844a1aSMatthew Knepley     PetscInt        numComp = 0;
203942a52479SJed Brown     PetscSectionSym sym;
2040ea844a1aSMatthew Knepley 
20419566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldName(s, fields[f], &name));
20429566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldName(*subs, f, name));
20439566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, fields[f], &numComp));
20449566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(*subs, f, numComp));
2045b778fa18SValeria Barra     for (c = 0; c < s->numFieldComponents[fields[f]]; ++c) {
20469566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetComponentName(s, fields[f], c, &name));
20479566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetComponentName(*subs, f, c, name));
2048b778fa18SValeria Barra     }
204942a52479SJed Brown     PetscCall(PetscSectionGetFieldSym(s, fields[f], &sym));
205042a52479SJed Brown     PetscCall(PetscSectionSetFieldSym(*subs, f, sym));
2051ea844a1aSMatthew Knepley   }
20529566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
20539566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(*subs, pStart, pEnd));
2054ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
2055ea844a1aSMatthew Knepley     PetscInt dof = 0, cdof = 0, fdof = 0, cfdof = 0;
2056ea844a1aSMatthew Knepley 
2057ea844a1aSMatthew Knepley     for (f = 0; f < len; ++f) {
20589566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, fields[f], &fdof));
20599566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(*subs, p, f, fdof));
20609566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(s, p, fields[f], &cfdof));
20619566063dSJacob Faibussowitsch       if (cfdof) PetscCall(PetscSectionSetFieldConstraintDof(*subs, p, f, cfdof));
2062ea844a1aSMatthew Knepley       dof += fdof;
2063ea844a1aSMatthew Knepley       cdof += cfdof;
2064ea844a1aSMatthew Knepley     }
20659566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(*subs, p, dof));
20669566063dSJacob Faibussowitsch     if (cdof) PetscCall(PetscSectionSetConstraintDof(*subs, p, cdof));
2067ea844a1aSMatthew Knepley     maxCdof = PetscMax(cdof, maxCdof);
2068ea844a1aSMatthew Knepley   }
20699566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(*subs));
2070ea844a1aSMatthew Knepley   if (maxCdof) {
2071ea844a1aSMatthew Knepley     PetscInt *indices;
2072ea844a1aSMatthew Knepley 
20739566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(maxCdof, &indices));
2074ea844a1aSMatthew Knepley     for (p = pStart; p < pEnd; ++p) {
2075ea844a1aSMatthew Knepley       PetscInt cdof;
2076ea844a1aSMatthew Knepley 
20779566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetConstraintDof(*subs, p, &cdof));
2078ea844a1aSMatthew Knepley       if (cdof) {
2079ea844a1aSMatthew Knepley         const PetscInt *oldIndices = NULL;
2080ea844a1aSMatthew Knepley         PetscInt        fdof = 0, cfdof = 0, fc, numConst = 0, fOff = 0;
2081ea844a1aSMatthew Knepley 
2082ea844a1aSMatthew Knepley         for (f = 0; f < len; ++f) {
20839566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(s, p, fields[f], &fdof));
20849566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(s, p, fields[f], &cfdof));
20859566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintIndices(s, p, fields[f], &oldIndices));
20869566063dSJacob Faibussowitsch           PetscCall(PetscSectionSetFieldConstraintIndices(*subs, p, f, oldIndices));
20879574d0f0SMatthew G. Knepley           for (fc = 0; fc < cfdof; ++fc) indices[numConst + fc] = oldIndices[fc] + fOff;
2088ea844a1aSMatthew Knepley           numConst += cfdof;
2089ea844a1aSMatthew Knepley           fOff += fdof;
2090ea844a1aSMatthew Knepley         }
20919566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetConstraintIndices(*subs, p, indices));
2092ea844a1aSMatthew Knepley       }
2093ea844a1aSMatthew Knepley     }
20949566063dSJacob Faibussowitsch     PetscCall(PetscFree(indices));
2095ea844a1aSMatthew Knepley   }
20963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2097ea844a1aSMatthew Knepley }
2098ea844a1aSMatthew Knepley 
2099ea844a1aSMatthew Knepley /*@
21005becf6f4SMatthew G. Knepley   PetscSectionCreateComponentSubsection - Create a new, smaller `PetscSection` composed of only selected components
21015becf6f4SMatthew G. Knepley 
21025becf6f4SMatthew G. Knepley   Collective
21035becf6f4SMatthew G. Knepley 
21045becf6f4SMatthew G. Knepley   Input Parameters:
21055becf6f4SMatthew G. Knepley + s     - the `PetscSection`
21065becf6f4SMatthew G. Knepley . len   - the number of components
21075becf6f4SMatthew G. Knepley - comps - the component numbers
21085becf6f4SMatthew G. Knepley 
21095becf6f4SMatthew G. Knepley   Output Parameter:
21105becf6f4SMatthew G. Knepley . subs - the subsection
21115becf6f4SMatthew G. Knepley 
21125becf6f4SMatthew G. Knepley   Level: advanced
21135becf6f4SMatthew G. Knepley 
21145becf6f4SMatthew G. Knepley   Notes:
21155becf6f4SMatthew G. Knepley   The chart of `subs` is the same as the chart of `s`
21165becf6f4SMatthew G. Knepley 
21175becf6f4SMatthew G. Knepley   This will error if the section has more than one field, or if a component number is out of range
21185becf6f4SMatthew G. Knepley 
2119*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionCreateSupersection()`, `PetscSectionCreate()`
21205becf6f4SMatthew G. Knepley @*/
21215becf6f4SMatthew G. Knepley PetscErrorCode PetscSectionCreateComponentSubsection(PetscSection s, PetscInt len, const PetscInt comps[], PetscSection *subs)
21225becf6f4SMatthew G. Knepley {
21235becf6f4SMatthew G. Knepley   PetscSectionSym sym;
21245becf6f4SMatthew G. Knepley   const char     *name = NULL;
21255becf6f4SMatthew G. Knepley   PetscInt        Nf, pStart, pEnd;
21265becf6f4SMatthew G. Knepley 
21275becf6f4SMatthew G. Knepley   PetscFunctionBegin;
21285becf6f4SMatthew G. Knepley   if (!len) PetscFunctionReturn(PETSC_SUCCESS);
21295becf6f4SMatthew G. Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
21305becf6f4SMatthew G. Knepley   PetscAssertPointer(comps, 3);
21315becf6f4SMatthew G. Knepley   PetscAssertPointer(subs, 4);
21325becf6f4SMatthew G. Knepley   PetscCall(PetscSectionGetNumFields(s, &Nf));
21335becf6f4SMatthew G. Knepley   PetscCheck(Nf == 1, PetscObjectComm((PetscObject)s), PETSC_ERR_ARG_WRONG, "This method can only handle one field, not %" PetscInt_FMT, Nf);
21345becf6f4SMatthew G. Knepley   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s), subs));
21355becf6f4SMatthew G. Knepley   PetscCall(PetscSectionSetNumFields(*subs, 1));
21365becf6f4SMatthew G. Knepley   PetscCall(PetscSectionGetFieldName(s, 0, &name));
21375becf6f4SMatthew G. Knepley   PetscCall(PetscSectionSetFieldName(*subs, 0, name));
21385becf6f4SMatthew G. Knepley   PetscCall(PetscSectionSetFieldComponents(*subs, 0, len));
21395becf6f4SMatthew G. Knepley   PetscCall(PetscSectionGetFieldSym(s, 0, &sym));
21405becf6f4SMatthew G. Knepley   PetscCall(PetscSectionSetFieldSym(*subs, 0, sym));
21415becf6f4SMatthew G. Knepley   for (PetscInt c = 0; c < len; ++c) {
21425becf6f4SMatthew G. Knepley     PetscCall(PetscSectionGetComponentName(s, 0, comps[c], &name));
21435becf6f4SMatthew G. Knepley     PetscCall(PetscSectionSetComponentName(*subs, 0, c, name));
21445becf6f4SMatthew G. Knepley   }
21455becf6f4SMatthew G. Knepley   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
21465becf6f4SMatthew G. Knepley   PetscCall(PetscSectionSetChart(*subs, pStart, pEnd));
21475becf6f4SMatthew G. Knepley   for (PetscInt p = pStart; p < pEnd; ++p) {
21485becf6f4SMatthew G. Knepley     PetscInt dof, cdof, cfdof;
21495becf6f4SMatthew G. Knepley 
21505becf6f4SMatthew G. Knepley     PetscCall(PetscSectionGetDof(s, p, &dof));
21515becf6f4SMatthew G. Knepley     if (!dof) continue;
21525becf6f4SMatthew G. Knepley     PetscCall(PetscSectionGetFieldConstraintDof(s, p, 0, &cfdof));
21535becf6f4SMatthew G. Knepley     PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
21545becf6f4SMatthew G. Knepley     PetscCheck(!cdof && !cfdof, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Component selection does not work with constraints");
21555becf6f4SMatthew G. Knepley     PetscCall(PetscSectionSetFieldDof(*subs, p, 0, len));
21565becf6f4SMatthew G. Knepley     PetscCall(PetscSectionSetDof(*subs, p, len));
21575becf6f4SMatthew G. Knepley   }
21585becf6f4SMatthew G. Knepley   PetscCall(PetscSectionSetUp(*subs));
21595becf6f4SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
21605becf6f4SMatthew G. Knepley }
21615becf6f4SMatthew G. Knepley 
21625becf6f4SMatthew G. Knepley /*@
2163583308b6SBarry Smith   PetscSectionCreateSupersection - Create a new, larger section composed of multiple `PetscSection`s
2164ea844a1aSMatthew Knepley 
216540750d41SVaclav Hapla   Collective
2166ea844a1aSMatthew Knepley 
2167ea844a1aSMatthew Knepley   Input Parameters:
2168ea844a1aSMatthew Knepley + s   - the input sections
2169ea844a1aSMatthew Knepley - len - the number of input sections
2170ea844a1aSMatthew Knepley 
2171ea844a1aSMatthew Knepley   Output Parameter:
2172ea844a1aSMatthew Knepley . supers - the supersection
2173ea844a1aSMatthew Knepley 
2174ea844a1aSMatthew Knepley   Level: advanced
2175ea844a1aSMatthew Knepley 
2176583308b6SBarry Smith   Notes:
2177cab54364SBarry Smith   The section offsets now refer to a new, larger vector.
2178cab54364SBarry Smith 
217938b5cf2dSJacob Faibussowitsch   Developer Notes:
2180cab54364SBarry Smith   Needs to explain how the sections are composed
2181cab54364SBarry Smith 
2182*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionCreateSubsection()`, `PetscSectionCreate()`
2183ea844a1aSMatthew Knepley @*/
2184d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCreateSupersection(PetscSection s[], PetscInt len, PetscSection *supers)
2185d71ae5a4SJacob Faibussowitsch {
21861690c2aeSBarry Smith   PetscInt Nf = 0, f, pStart = PETSC_INT_MAX, pEnd = 0, p, maxCdof = 0, i;
2187ea844a1aSMatthew Knepley 
2188ea844a1aSMatthew Knepley   PetscFunctionBegin;
21893ba16761SJacob Faibussowitsch   if (!len) PetscFunctionReturn(PETSC_SUCCESS);
2190ea844a1aSMatthew Knepley   for (i = 0; i < len; ++i) {
2191ea844a1aSMatthew Knepley     PetscInt nf, pStarti, pEndi;
2192ea844a1aSMatthew Knepley 
21939566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(s[i], &nf));
21949566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(s[i], &pStarti, &pEndi));
2195ea844a1aSMatthew Knepley     pStart = PetscMin(pStart, pStarti);
2196ea844a1aSMatthew Knepley     pEnd   = PetscMax(pEnd, pEndi);
2197ea844a1aSMatthew Knepley     Nf += nf;
2198ea844a1aSMatthew Knepley   }
21999566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s[0]), supers));
22009566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(*supers, Nf));
2201ea844a1aSMatthew Knepley   for (i = 0, f = 0; i < len; ++i) {
2202b778fa18SValeria Barra     PetscInt nf, fi, ci;
2203ea844a1aSMatthew Knepley 
22049566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(s[i], &nf));
2205ea844a1aSMatthew Knepley     for (fi = 0; fi < nf; ++fi, ++f) {
2206ea844a1aSMatthew Knepley       const char *name    = NULL;
2207ea844a1aSMatthew Knepley       PetscInt    numComp = 0;
2208ea844a1aSMatthew Knepley 
22099566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(s[i], fi, &name));
22109566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldName(*supers, f, name));
22119566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(s[i], fi, &numComp));
22129566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldComponents(*supers, f, numComp));
2213b778fa18SValeria Barra       for (ci = 0; ci < s[i]->numFieldComponents[fi]; ++ci) {
22149566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetComponentName(s[i], fi, ci, &name));
22159566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetComponentName(*supers, f, ci, name));
2216b778fa18SValeria Barra       }
2217ea844a1aSMatthew Knepley     }
2218ea844a1aSMatthew Knepley   }
22199566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(*supers, pStart, pEnd));
2220ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
2221ea844a1aSMatthew Knepley     PetscInt dof = 0, cdof = 0;
2222ea844a1aSMatthew Knepley 
2223ea844a1aSMatthew Knepley     for (i = 0, f = 0; i < len; ++i) {
2224ea844a1aSMatthew Knepley       PetscInt nf, fi, pStarti, pEndi;
2225ea844a1aSMatthew Knepley       PetscInt fdof = 0, cfdof = 0;
2226ea844a1aSMatthew Knepley 
22279566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetNumFields(s[i], &nf));
22289566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s[i], &pStarti, &pEndi));
2229ea844a1aSMatthew Knepley       if ((p < pStarti) || (p >= pEndi)) continue;
2230ea844a1aSMatthew Knepley       for (fi = 0; fi < nf; ++fi, ++f) {
22319566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldDof(s[i], p, fi, &fdof));
22329566063dSJacob Faibussowitsch         PetscCall(PetscSectionAddFieldDof(*supers, p, f, fdof));
22339566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldConstraintDof(s[i], p, fi, &cfdof));
22349566063dSJacob Faibussowitsch         if (cfdof) PetscCall(PetscSectionAddFieldConstraintDof(*supers, p, f, cfdof));
2235ea844a1aSMatthew Knepley         dof += fdof;
2236ea844a1aSMatthew Knepley         cdof += cfdof;
2237ea844a1aSMatthew Knepley       }
2238ea844a1aSMatthew Knepley     }
22399566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(*supers, p, dof));
22409566063dSJacob Faibussowitsch     if (cdof) PetscCall(PetscSectionSetConstraintDof(*supers, p, cdof));
2241ea844a1aSMatthew Knepley     maxCdof = PetscMax(cdof, maxCdof);
2242ea844a1aSMatthew Knepley   }
22439566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(*supers));
2244ea844a1aSMatthew Knepley   if (maxCdof) {
2245ea844a1aSMatthew Knepley     PetscInt *indices;
2246ea844a1aSMatthew Knepley 
22479566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(maxCdof, &indices));
2248ea844a1aSMatthew Knepley     for (p = pStart; p < pEnd; ++p) {
2249ea844a1aSMatthew Knepley       PetscInt cdof;
2250ea844a1aSMatthew Knepley 
22519566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetConstraintDof(*supers, p, &cdof));
2252ea844a1aSMatthew Knepley       if (cdof) {
2253ea844a1aSMatthew Knepley         PetscInt dof, numConst = 0, fOff = 0;
2254ea844a1aSMatthew Knepley 
2255ea844a1aSMatthew Knepley         for (i = 0, f = 0; i < len; ++i) {
2256ea844a1aSMatthew Knepley           const PetscInt *oldIndices = NULL;
2257ea844a1aSMatthew Knepley           PetscInt        nf, fi, pStarti, pEndi, fdof, cfdof, fc;
2258ea844a1aSMatthew Knepley 
22599566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetNumFields(s[i], &nf));
22609566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetChart(s[i], &pStarti, &pEndi));
2261ea844a1aSMatthew Knepley           if ((p < pStarti) || (p >= pEndi)) continue;
2262ea844a1aSMatthew Knepley           for (fi = 0; fi < nf; ++fi, ++f) {
22639566063dSJacob Faibussowitsch             PetscCall(PetscSectionGetFieldDof(s[i], p, fi, &fdof));
22649566063dSJacob Faibussowitsch             PetscCall(PetscSectionGetFieldConstraintDof(s[i], p, fi, &cfdof));
22659566063dSJacob Faibussowitsch             PetscCall(PetscSectionGetFieldConstraintIndices(s[i], p, fi, &oldIndices));
2266fcd2503dSMatthew G. Knepley             for (fc = 0; fc < cfdof; ++fc) indices[numConst + fc] = oldIndices[fc];
22679566063dSJacob Faibussowitsch             PetscCall(PetscSectionSetFieldConstraintIndices(*supers, p, f, &indices[numConst]));
2268fcd2503dSMatthew G. Knepley             for (fc = 0; fc < cfdof; ++fc) indices[numConst + fc] += fOff;
2269ea844a1aSMatthew Knepley             numConst += cfdof;
2270ea844a1aSMatthew Knepley           }
22719566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetDof(s[i], p, &dof));
2272ea844a1aSMatthew Knepley           fOff += dof;
2273ea844a1aSMatthew Knepley         }
22749566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetConstraintIndices(*supers, p, indices));
2275ea844a1aSMatthew Knepley       }
2276ea844a1aSMatthew Knepley     }
22779566063dSJacob Faibussowitsch     PetscCall(PetscFree(indices));
2278ea844a1aSMatthew Knepley   }
22793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2280ea844a1aSMatthew Knepley }
2281ea844a1aSMatthew Knepley 
228229d2e75bSMatthew G. Knepley static PetscErrorCode PetscSectionCreateSubplexSection_Private(PetscSection s, IS subpointIS, PetscBool renumberPoints, PetscSection *subs)
2283d71ae5a4SJacob Faibussowitsch {
2284ea844a1aSMatthew Knepley   const PetscInt *points = NULL, *indices = NULL;
228529d2e75bSMatthew G. Knepley   PetscInt       *spoints = NULL, *order = NULL;
228641f23ed0SMatthew G. Knepley   PetscInt        numFields, f, c, numSubpoints = 0, pStart, pEnd, p, spStart, spEnd, subp;
2287ea844a1aSMatthew Knepley 
2288ea844a1aSMatthew Knepley   PetscFunctionBegin;
2289ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
229029d2e75bSMatthew G. Knepley   PetscValidHeaderSpecific(subpointIS, IS_CLASSID, 2);
22914f572ea9SToby Isaac   PetscAssertPointer(subs, 4);
22929566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &numFields));
22939566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s), subs));
22949566063dSJacob Faibussowitsch   if (numFields) PetscCall(PetscSectionSetNumFields(*subs, numFields));
2295ea844a1aSMatthew Knepley   for (f = 0; f < numFields; ++f) {
2296ea844a1aSMatthew Knepley     const char *name    = NULL;
2297ea844a1aSMatthew Knepley     PetscInt    numComp = 0;
2298ea844a1aSMatthew Knepley 
22999566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldName(s, f, &name));
23009566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldName(*subs, f, name));
23019566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &numComp));
23029566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(*subs, f, numComp));
2303b778fa18SValeria Barra     for (c = 0; c < s->numFieldComponents[f]; ++c) {
23049566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetComponentName(s, f, c, &name));
23059566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetComponentName(*subs, f, c, name));
2306b778fa18SValeria Barra     }
2307ea844a1aSMatthew Knepley   }
2308ea844a1aSMatthew Knepley   /* For right now, we do not try to squeeze the subchart */
230929d2e75bSMatthew G. Knepley   if (subpointIS) {
231029d2e75bSMatthew G. Knepley     PetscCall(ISGetLocalSize(subpointIS, &numSubpoints));
231129d2e75bSMatthew G. Knepley     PetscCall(ISGetIndices(subpointIS, &points));
2312ea844a1aSMatthew Knepley   }
23139566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
231441f23ed0SMatthew G. Knepley   if (renumberPoints) {
231529d2e75bSMatthew G. Knepley     PetscBool sorted;
231629d2e75bSMatthew G. Knepley 
231741f23ed0SMatthew G. Knepley     spStart = 0;
231841f23ed0SMatthew G. Knepley     spEnd   = numSubpoints;
231929d2e75bSMatthew G. Knepley     PetscCall(ISSorted(subpointIS, &sorted));
232029d2e75bSMatthew G. Knepley     if (!sorted) {
232129d2e75bSMatthew G. Knepley       PetscCall(PetscMalloc2(numSubpoints, &spoints, numSubpoints, &order));
232229d2e75bSMatthew G. Knepley       PetscCall(PetscArraycpy(spoints, points, numSubpoints));
232329d2e75bSMatthew G. Knepley       for (PetscInt i = 0; i < numSubpoints; ++i) order[i] = i;
232429d2e75bSMatthew G. Knepley       PetscCall(PetscSortIntWithArray(numSubpoints, spoints, order));
232529d2e75bSMatthew G. Knepley     }
232641f23ed0SMatthew G. Knepley   } else {
232729d2e75bSMatthew G. Knepley     PetscCall(ISGetMinMax(subpointIS, &spStart, &spEnd));
232841f23ed0SMatthew G. Knepley     ++spEnd;
232941f23ed0SMatthew G. Knepley   }
233041f23ed0SMatthew G. Knepley   PetscCall(PetscSectionSetChart(*subs, spStart, spEnd));
2331ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
2332ea844a1aSMatthew Knepley     PetscInt dof, cdof, fdof = 0, cfdof = 0;
2333ea844a1aSMatthew Knepley 
233429d2e75bSMatthew G. Knepley     PetscCall(PetscFindInt(p, numSubpoints, spoints ? spoints : points, &subp));
2335ea844a1aSMatthew Knepley     if (subp < 0) continue;
233641f23ed0SMatthew G. Knepley     if (!renumberPoints) subp = p;
233729d2e75bSMatthew G. Knepley     else subp = order ? order[subp] : subp;
2338ea844a1aSMatthew Knepley     for (f = 0; f < numFields; ++f) {
23399566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &fdof));
23409566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(*subs, subp, f, fdof));
23419566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(s, p, f, &cfdof));
23429566063dSJacob Faibussowitsch       if (cfdof) PetscCall(PetscSectionSetFieldConstraintDof(*subs, subp, f, cfdof));
2343ea844a1aSMatthew Knepley     }
23449566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s, p, &dof));
23459566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(*subs, subp, dof));
23469566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
23479566063dSJacob Faibussowitsch     if (cdof) PetscCall(PetscSectionSetConstraintDof(*subs, subp, cdof));
2348ea844a1aSMatthew Knepley   }
23499566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(*subs));
2350ea844a1aSMatthew Knepley   /* Change offsets to original offsets */
2351ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
2352ea844a1aSMatthew Knepley     PetscInt off, foff = 0;
2353ea844a1aSMatthew Knepley 
235429d2e75bSMatthew G. Knepley     PetscCall(PetscFindInt(p, numSubpoints, spoints ? spoints : points, &subp));
2355ea844a1aSMatthew Knepley     if (subp < 0) continue;
235641f23ed0SMatthew G. Knepley     if (!renumberPoints) subp = p;
235729d2e75bSMatthew G. Knepley     else subp = order ? order[subp] : subp;
2358ea844a1aSMatthew Knepley     for (f = 0; f < numFields; ++f) {
23599566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldOffset(s, p, f, &foff));
23609566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldOffset(*subs, subp, f, foff));
2361ea844a1aSMatthew Knepley     }
23629566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(s, p, &off));
23639566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetOffset(*subs, subp, off));
2364ea844a1aSMatthew Knepley   }
2365ea844a1aSMatthew Knepley   /* Copy constraint indices */
236641f23ed0SMatthew G. Knepley   for (subp = spStart; subp < spEnd; ++subp) {
2367ea844a1aSMatthew Knepley     PetscInt cdof;
2368ea844a1aSMatthew Knepley 
23699566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(*subs, subp, &cdof));
2370ea844a1aSMatthew Knepley     if (cdof) {
2371ea844a1aSMatthew Knepley       for (f = 0; f < numFields; ++f) {
237241f23ed0SMatthew G. Knepley         PetscCall(PetscSectionGetFieldConstraintIndices(s, points[subp - spStart], f, &indices));
23739566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetFieldConstraintIndices(*subs, subp, f, indices));
2374ea844a1aSMatthew Knepley       }
237541f23ed0SMatthew G. Knepley       PetscCall(PetscSectionGetConstraintIndices(s, points[subp - spStart], &indices));
23769566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetConstraintIndices(*subs, subp, indices));
2377ea844a1aSMatthew Knepley     }
2378ea844a1aSMatthew Knepley   }
237929d2e75bSMatthew G. Knepley   if (subpointIS) PetscCall(ISRestoreIndices(subpointIS, &points));
238029d2e75bSMatthew G. Knepley   PetscCall(PetscFree2(spoints, order));
23813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2382ea844a1aSMatthew Knepley }
2383ea844a1aSMatthew Knepley 
238441f23ed0SMatthew G. Knepley /*@
238541f23ed0SMatthew G. Knepley   PetscSectionCreateSubmeshSection - Create a new, smaller section with support on the submesh
238641f23ed0SMatthew G. Knepley 
2387c3339decSBarry Smith   Collective
238841f23ed0SMatthew G. Knepley 
238941f23ed0SMatthew G. Knepley   Input Parameters:
2390cab54364SBarry Smith + s          - the `PetscSection`
239129d2e75bSMatthew G. Knepley - subpointIS - a sorted list of points in the original mesh which are in the submesh
239241f23ed0SMatthew G. Knepley 
239341f23ed0SMatthew G. Knepley   Output Parameter:
239441f23ed0SMatthew G. Knepley . subs - the subsection
239541f23ed0SMatthew G. Knepley 
2396cab54364SBarry Smith   Level: advanced
2397cab54364SBarry Smith 
2398583308b6SBarry Smith   Notes:
2399583308b6SBarry 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))`
2400583308b6SBarry Smith 
240120662ed9SBarry Smith   Compare this with `PetscSectionCreateSubdomainSection()` that does not map the points numbers to start at zero but leaves them as before
240241f23ed0SMatthew G. Knepley 
240338b5cf2dSJacob Faibussowitsch   Developer Notes:
240420662ed9SBarry 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`
240541f23ed0SMatthew G. Knepley 
2406*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionCreateSubdomainSection()`, `PetscSectionCreateSubsection()`, `DMPlexGetSubpointMap()`, `PetscSectionCreate()`
240741f23ed0SMatthew G. Knepley @*/
240829d2e75bSMatthew G. Knepley PetscErrorCode PetscSectionCreateSubmeshSection(PetscSection s, IS subpointIS, PetscSection *subs)
2409d71ae5a4SJacob Faibussowitsch {
241041f23ed0SMatthew G. Knepley   PetscFunctionBegin;
241129d2e75bSMatthew G. Knepley   PetscCall(PetscSectionCreateSubplexSection_Private(s, subpointIS, PETSC_TRUE, subs));
24123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
241341f23ed0SMatthew G. Knepley }
241441f23ed0SMatthew G. Knepley 
241541f23ed0SMatthew G. Knepley /*@
241641f23ed0SMatthew G. Knepley   PetscSectionCreateSubdomainSection - Create a new, smaller section with support on a subdomain of the mesh
241741f23ed0SMatthew G. Knepley 
2418c3339decSBarry Smith   Collective
241941f23ed0SMatthew G. Knepley 
242041f23ed0SMatthew G. Knepley   Input Parameters:
2421cab54364SBarry Smith + s           - the `PetscSection`
242241f23ed0SMatthew G. Knepley - subpointMap - a sorted list of points in the original mesh which are in the subdomain
242341f23ed0SMatthew G. Knepley 
242441f23ed0SMatthew G. Knepley   Output Parameter:
242541f23ed0SMatthew G. Knepley . subs - the subsection
242641f23ed0SMatthew G. Knepley 
2427cab54364SBarry Smith   Level: advanced
2428cab54364SBarry Smith 
2429583308b6SBarry Smith   Notes:
2430583308b6SBarry 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`
2431583308b6SBarry Smith   is `[min(subpointMap),max(subpointMap)+1)`
2432583308b6SBarry Smith 
243320662ed9SBarry Smith   Compare this with `PetscSectionCreateSubmeshSection()` that maps the point numbers to start at zero
243441f23ed0SMatthew G. Knepley 
2435cab54364SBarry Smith   Developer Notes:
243620662ed9SBarry 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`
2437cab54364SBarry Smith 
2438*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionCreateSubmeshSection()`, `PetscSectionCreateSubsection()`, `DMPlexGetSubpointMap()`, `PetscSectionCreate()`
243941f23ed0SMatthew G. Knepley @*/
2440d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionCreateSubdomainSection(PetscSection s, IS subpointMap, PetscSection *subs)
2441d71ae5a4SJacob Faibussowitsch {
244241f23ed0SMatthew G. Knepley   PetscFunctionBegin;
2443da8c939bSJacob Faibussowitsch   PetscCall(PetscSectionCreateSubplexSection_Private(s, subpointMap, PETSC_FALSE, subs));
24443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
244541f23ed0SMatthew G. Knepley }
244641f23ed0SMatthew G. Knepley 
2447d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscSectionView_ASCII(PetscSection s, PetscViewer viewer)
2448d71ae5a4SJacob Faibussowitsch {
2449ea844a1aSMatthew Knepley   PetscInt    p;
2450ea844a1aSMatthew Knepley   PetscMPIInt rank;
2451ea844a1aSMatthew Knepley 
2452ea844a1aSMatthew Knepley   PetscFunctionBegin;
24539566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank));
24549566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPushSynchronized(viewer));
24559566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "Process %d:\n", rank));
2456ea844a1aSMatthew Knepley   for (p = 0; p < s->pEnd - s->pStart; ++p) {
24573857f682SStefano Zampini     if (s->bc && s->bc->atlasDof[p] > 0) {
2458ea844a1aSMatthew Knepley       PetscInt b;
24596497c311SBarry Smith       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "  (%4" PetscInt_FMT ") dof %2" PetscInt_FMT " offset %3" PetscInt_FMT " constrained", p + s->pStart, s->atlasDof[p], s->atlasOff[p]));
2460083401c6SMatthew G. Knepley       if (s->bcIndices) {
246148a46eb9SPierre Jolivet         for (b = 0; b < s->bc->atlasDof[p]; ++b) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " %" PetscInt_FMT, s->bcIndices[s->bc->atlasOff[p] + b]));
2462083401c6SMatthew G. Knepley       }
24639566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n"));
2464ea844a1aSMatthew Knepley     } else {
24656497c311SBarry Smith       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "  (%4" PetscInt_FMT ") dof %2" PetscInt_FMT " offset %3" PetscInt_FMT "\n", p + s->pStart, s->atlasDof[p], s->atlasOff[p]));
2466ea844a1aSMatthew Knepley     }
2467ea844a1aSMatthew Knepley   }
24689566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(viewer));
24699566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPopSynchronized(viewer));
2470ea844a1aSMatthew Knepley   if (s->sym) {
24719566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPushTab(viewer));
24729566063dSJacob Faibussowitsch     PetscCall(PetscSectionSymView(s->sym, viewer));
24739566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPopTab(viewer));
2474ea844a1aSMatthew Knepley   }
24753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2476ea844a1aSMatthew Knepley }
2477ea844a1aSMatthew Knepley 
2478ffeef943SBarry Smith /*@
2479cab54364SBarry Smith   PetscSectionViewFromOptions - View the `PetscSection` based on values in the options database
2480fe2efc57SMark 
248140750d41SVaclav Hapla   Collective
2482fe2efc57SMark 
2483fe2efc57SMark   Input Parameters:
248420662ed9SBarry Smith + A    - the `PetscSection` object to view
2485cab54364SBarry Smith . obj  - Optional object that provides the options prefix used for the options
2486736c3998SJose E. Roman - name - command line option
2487fe2efc57SMark 
2488fe2efc57SMark   Level: intermediate
2489cab54364SBarry Smith 
249020662ed9SBarry Smith   Note:
249120662ed9SBarry Smith   See `PetscObjectViewFromOptions()` for available values of `PetscViewer` and `PetscViewerFormat`
249220662ed9SBarry Smith 
2493*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionView`, `PetscObjectViewFromOptions()`, `PetscSectionCreate()`, `PetscSectionView()`
2494fe2efc57SMark @*/
2495d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionViewFromOptions(PetscSection A, PetscObject obj, const char name[])
2496d71ae5a4SJacob Faibussowitsch {
2497fe2efc57SMark   PetscFunctionBegin;
2498fe2efc57SMark   PetscValidHeaderSpecific(A, PETSC_SECTION_CLASSID, 1);
24999566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
25003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2501fe2efc57SMark }
2502fe2efc57SMark 
2503ffeef943SBarry Smith /*@
2504cab54364SBarry Smith   PetscSectionView - Views a `PetscSection`
2505ea844a1aSMatthew Knepley 
250640750d41SVaclav Hapla   Collective
2507ea844a1aSMatthew Knepley 
2508ea844a1aSMatthew Knepley   Input Parameters:
2509cab54364SBarry Smith + s      - the `PetscSection` object to view
251038b5cf2dSJacob Faibussowitsch - viewer - the viewer
2511ea844a1aSMatthew Knepley 
2512cab54364SBarry Smith   Level: beginner
2513cab54364SBarry Smith 
25141ddd528eSksagiyam   Note:
2515cab54364SBarry Smith   `PetscSectionView()`, when viewer is of type `PETSCVIEWERHDF5`, only saves
25161ddd528eSksagiyam   distribution independent data, such as dofs, offsets, constraint dofs,
25171ddd528eSksagiyam   and constraint indices. Points that have negative dofs, for instance,
25181ddd528eSksagiyam   are not saved as they represent points owned by other processes.
25191ddd528eSksagiyam   Point numbering and rank assignment is currently not stored.
2520cab54364SBarry Smith   The saved section can be loaded with `PetscSectionLoad()`.
25211ddd528eSksagiyam 
2522*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionDestroy()`, `PetscSectionLoad()`, `PetscViewer`
2523ea844a1aSMatthew Knepley @*/
2524d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionView(PetscSection s, PetscViewer viewer)
2525d71ae5a4SJacob Faibussowitsch {
25261ddd528eSksagiyam   PetscBool isascii, ishdf5;
2527ea844a1aSMatthew Knepley   PetscInt  f;
2528ea844a1aSMatthew Knepley 
2529ea844a1aSMatthew Knepley   PetscFunctionBegin;
2530ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
25319566063dSJacob Faibussowitsch   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)s), &viewer));
2532ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
25339566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
25349566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2535ea844a1aSMatthew Knepley   if (isascii) {
25369566063dSJacob Faibussowitsch     PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)s, viewer));
2537ea844a1aSMatthew Knepley     if (s->numFields) {
25389566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " fields\n", s->numFields));
2539ea844a1aSMatthew Knepley       for (f = 0; f < s->numFields; ++f) {
25404cd8913cSStefano Zampini         PetscCall(PetscViewerASCIIPrintf(viewer, "  field %" PetscInt_FMT " \"%s\" with %" PetscInt_FMT " components\n", f, s->fieldNames[f], s->numFieldComponents[f]));
25419566063dSJacob Faibussowitsch         PetscCall(PetscSectionView_ASCII(s->field[f], viewer));
2542ea844a1aSMatthew Knepley       }
2543ea844a1aSMatthew Knepley     } else {
25449566063dSJacob Faibussowitsch       PetscCall(PetscSectionView_ASCII(s, viewer));
2545ea844a1aSMatthew Knepley     }
25461ddd528eSksagiyam   } else if (ishdf5) {
25471ddd528eSksagiyam #if PetscDefined(HAVE_HDF5)
25489566063dSJacob Faibussowitsch     PetscCall(PetscSectionView_HDF5_Internal(s, viewer));
25491ddd528eSksagiyam #else
25501ddd528eSksagiyam     SETERRQ(PetscObjectComm((PetscObject)s), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
25511ddd528eSksagiyam #endif
2552ea844a1aSMatthew Knepley   }
25533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2554ea844a1aSMatthew Knepley }
2555ea844a1aSMatthew Knepley 
2556ffeef943SBarry Smith /*@
2557cab54364SBarry Smith   PetscSectionLoad - Loads a `PetscSection`
2558fde5e3acSksagiyam 
255940750d41SVaclav Hapla   Collective
2560fde5e3acSksagiyam 
2561fde5e3acSksagiyam   Input Parameters:
2562cab54364SBarry Smith + s      - the `PetscSection` object to load
256338b5cf2dSJacob Faibussowitsch - viewer - the viewer
2564fde5e3acSksagiyam 
2565cab54364SBarry Smith   Level: beginner
2566cab54364SBarry Smith 
2567fde5e3acSksagiyam   Note:
2568cab54364SBarry Smith   `PetscSectionLoad()`, when viewer is of type `PETSCVIEWERHDF5`, loads
2569cab54364SBarry Smith   a section saved with `PetscSectionView()`. The number of processes
2570fde5e3acSksagiyam   used here (N) does not need to be the same as that used when saving.
2571fde5e3acSksagiyam   After calling this function, the chart of s on rank i will be set
2572fde5e3acSksagiyam   to [0, E_i), where \sum_{i=0}^{N-1}E_i equals to the total number of
2573fde5e3acSksagiyam   saved section points.
2574fde5e3acSksagiyam 
2575*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionDestroy()`, `PetscSectionView()`
2576fde5e3acSksagiyam @*/
2577d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionLoad(PetscSection s, PetscViewer viewer)
2578d71ae5a4SJacob Faibussowitsch {
2579fde5e3acSksagiyam   PetscBool ishdf5;
2580fde5e3acSksagiyam 
2581fde5e3acSksagiyam   PetscFunctionBegin;
2582fde5e3acSksagiyam   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2583fde5e3acSksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
25849566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2585fde5e3acSksagiyam   if (ishdf5) {
2586fde5e3acSksagiyam #if PetscDefined(HAVE_HDF5)
25879566063dSJacob Faibussowitsch     PetscCall(PetscSectionLoad_HDF5_Internal(s, viewer));
25883ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2589fde5e3acSksagiyam #else
2590fde5e3acSksagiyam     SETERRQ(PetscObjectComm((PetscObject)s), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2591fde5e3acSksagiyam #endif
259298921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)s), PETSC_ERR_SUP, "Viewer type %s not yet supported for PetscSection loading", ((PetscObject)viewer)->type_name);
2593fde5e3acSksagiyam }
2594fde5e3acSksagiyam 
2595d4a1ad33SMatthew G. Knepley /*@
2596d4a1ad33SMatthew G. Knepley   PetscSectionResetClosurePermutation - Remove any existing closure permutation
2597d4a1ad33SMatthew G. Knepley 
2598d4a1ad33SMatthew G. Knepley   Input Parameter:
2599e8e188d2SZach Atkins . section - The `PetscSection`
2600d4a1ad33SMatthew G. Knepley 
2601d4a1ad33SMatthew G. Knepley   Level: intermediate
2602d4a1ad33SMatthew G. Knepley 
2603e8e188d2SZach Atkins .seealso: `PetscSectionSetClosurePermutation()`, `PetscSectionSetClosureIndex()`, `PetscSectionReset()`
2604d4a1ad33SMatthew G. Knepley @*/
2605d4a1ad33SMatthew G. Knepley PetscErrorCode PetscSectionResetClosurePermutation(PetscSection section)
2606d71ae5a4SJacob Faibussowitsch {
2607c459fbc1SJed Brown   PetscSectionClosurePermVal clVal;
2608c459fbc1SJed Brown 
2609c459fbc1SJed Brown   PetscFunctionBegin;
26103ba16761SJacob Faibussowitsch   if (!section->clHash) PetscFunctionReturn(PETSC_SUCCESS);
2611c459fbc1SJed Brown   kh_foreach_value(section->clHash, clVal, {
26129566063dSJacob Faibussowitsch     PetscCall(PetscFree(clVal.perm));
26139566063dSJacob Faibussowitsch     PetscCall(PetscFree(clVal.invPerm));
2614c459fbc1SJed Brown   });
2615c459fbc1SJed Brown   kh_destroy(ClPerm, section->clHash);
2616c459fbc1SJed Brown   section->clHash = NULL;
26173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2618c459fbc1SJed Brown }
2619c459fbc1SJed Brown 
2620ea844a1aSMatthew Knepley /*@
2621583308b6SBarry Smith   PetscSectionReset - Frees all section data, the section is then as if `PetscSectionCreate()` had just been called.
2622ea844a1aSMatthew Knepley 
262340750d41SVaclav Hapla   Not Collective
2624ea844a1aSMatthew Knepley 
26252fe279fdSBarry Smith   Input Parameter:
2626cab54364SBarry Smith . s - the `PetscSection`
2627ea844a1aSMatthew Knepley 
2628ea844a1aSMatthew Knepley   Level: beginner
2629ea844a1aSMatthew Knepley 
2630*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionCreate()`
2631ea844a1aSMatthew Knepley @*/
2632d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionReset(PetscSection s)
2633d71ae5a4SJacob Faibussowitsch {
2634b778fa18SValeria Barra   PetscInt f, c;
2635ea844a1aSMatthew Knepley 
2636ea844a1aSMatthew Knepley   PetscFunctionBegin;
2637ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2638ea844a1aSMatthew Knepley   for (f = 0; f < s->numFields; ++f) {
26399566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&s->field[f]));
26409566063dSJacob Faibussowitsch     PetscCall(PetscFree(s->fieldNames[f]));
264148a46eb9SPierre Jolivet     for (c = 0; c < s->numFieldComponents[f]; ++c) PetscCall(PetscFree(s->compNames[f][c]));
26429566063dSJacob Faibussowitsch     PetscCall(PetscFree(s->compNames[f]));
2643ea844a1aSMatthew Knepley   }
26449566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->numFieldComponents));
26459566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->fieldNames));
26469566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->compNames));
26479566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->field));
26489566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&s->bc));
26499566063dSJacob Faibussowitsch   PetscCall(PetscFree(s->bcIndices));
26509566063dSJacob Faibussowitsch   PetscCall(PetscFree2(s->atlasDof, s->atlasOff));
26519566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&s->clSection));
26529566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&s->clPoints));
26539566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&s->perm));
26543ec46b7bSMatthew G. Knepley   PetscCall(PetscBTDestroy(&s->blockStarts));
2655d4a1ad33SMatthew G. Knepley   PetscCall(PetscSectionResetClosurePermutation(s));
26569566063dSJacob Faibussowitsch   PetscCall(PetscSectionSymDestroy(&s->sym));
26579566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&s->clSection));
26589566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&s->clPoints));
265969c11d05SVaclav Hapla   PetscCall(PetscSectionInvalidateMaxDof_Internal(s));
2660ea844a1aSMatthew Knepley   s->pStart    = -1;
2661ea844a1aSMatthew Knepley   s->pEnd      = -1;
2662ea844a1aSMatthew Knepley   s->maxDof    = 0;
2663ea844a1aSMatthew Knepley   s->setup     = PETSC_FALSE;
2664ea844a1aSMatthew Knepley   s->numFields = 0;
2665ea844a1aSMatthew Knepley   s->clObj     = NULL;
26663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2667ea844a1aSMatthew Knepley }
2668ea844a1aSMatthew Knepley 
2669ea844a1aSMatthew Knepley /*@
2670583308b6SBarry Smith   PetscSectionDestroy - Frees a `PetscSection`
2671ea844a1aSMatthew Knepley 
267240750d41SVaclav Hapla   Not Collective
2673ea844a1aSMatthew Knepley 
26742fe279fdSBarry Smith   Input Parameter:
2675cab54364SBarry Smith . s - the `PetscSection`
2676ea844a1aSMatthew Knepley 
2677ea844a1aSMatthew Knepley   Level: beginner
2678ea844a1aSMatthew Knepley 
2679*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionCreate()`, `PetscSectionReset()`
2680ea844a1aSMatthew Knepley @*/
2681d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionDestroy(PetscSection *s)
2682d71ae5a4SJacob Faibussowitsch {
2683ea844a1aSMatthew Knepley   PetscFunctionBegin;
26843ba16761SJacob Faibussowitsch   if (!*s) PetscFunctionReturn(PETSC_SUCCESS);
268562f17a49SStefano Zampini   PetscValidHeaderSpecific(*s, PETSC_SECTION_CLASSID, 1);
2686f4f49eeaSPierre Jolivet   if (--((PetscObject)*s)->refct > 0) {
2687ea844a1aSMatthew Knepley     *s = NULL;
26883ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2689ea844a1aSMatthew Knepley   }
26909566063dSJacob Faibussowitsch   PetscCall(PetscSectionReset(*s));
26919566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(s));
26923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2693ea844a1aSMatthew Knepley }
2694ea844a1aSMatthew Knepley 
2695da8c939bSJacob Faibussowitsch static PetscErrorCode VecIntGetValuesSection_Private(const PetscInt *baseArray, PetscSection s, PetscInt point, const PetscInt **values)
2696d71ae5a4SJacob Faibussowitsch {
2697ea844a1aSMatthew Knepley   const PetscInt p = point - s->pStart;
2698ea844a1aSMatthew Knepley 
2699ea844a1aSMatthew Knepley   PetscFunctionBegin;
2700ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 2);
2701ea844a1aSMatthew Knepley   *values = &baseArray[s->atlasOff[p]];
27023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2703ea844a1aSMatthew Knepley }
2704ea844a1aSMatthew Knepley 
2705da8c939bSJacob Faibussowitsch static PetscErrorCode VecIntSetValuesSection_Private(PetscInt *baseArray, PetscSection s, PetscInt point, const PetscInt values[], InsertMode mode)
2706d71ae5a4SJacob Faibussowitsch {
2707ea844a1aSMatthew Knepley   PetscInt      *array;
2708ea844a1aSMatthew Knepley   const PetscInt p           = point - s->pStart;
2709ea844a1aSMatthew Knepley   const PetscInt orientation = 0; /* Needs to be included for use in closure operations */
2710ea844a1aSMatthew Knepley   PetscInt       cDim        = 0;
2711ea844a1aSMatthew Knepley 
2712ea844a1aSMatthew Knepley   PetscFunctionBegin;
2713ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 2);
27149566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstraintDof(s, p, &cDim));
2715ea844a1aSMatthew Knepley   array = &baseArray[s->atlasOff[p]];
2716ea844a1aSMatthew Knepley   if (!cDim) {
2717ea844a1aSMatthew Knepley     if (orientation >= 0) {
2718ea844a1aSMatthew Knepley       const PetscInt dim = s->atlasDof[p];
2719ea844a1aSMatthew Knepley       PetscInt       i;
2720ea844a1aSMatthew Knepley 
2721ea844a1aSMatthew Knepley       if (mode == INSERT_VALUES) {
2722ad716f99SStefano Zampini         for (i = 0; i < dim; ++i) array[i] = values ? values[i] : i;
2723ea844a1aSMatthew Knepley       } else {
2724ea844a1aSMatthew Knepley         for (i = 0; i < dim; ++i) array[i] += values[i];
2725ea844a1aSMatthew Knepley       }
2726ea844a1aSMatthew Knepley     } else {
2727ea844a1aSMatthew Knepley       PetscInt offset = 0;
2728ea844a1aSMatthew Knepley       PetscInt j      = -1, field, i;
2729ea844a1aSMatthew Knepley 
2730ea844a1aSMatthew Knepley       for (field = 0; field < s->numFields; ++field) {
2731ea844a1aSMatthew Knepley         const PetscInt dim = s->field[field]->atlasDof[p];
2732ea844a1aSMatthew Knepley 
2733ad716f99SStefano Zampini         for (i = dim - 1; i >= 0; --i) array[++j] = values ? values[i + offset] : i + offset;
2734ea844a1aSMatthew Knepley         offset += dim;
2735ea844a1aSMatthew Knepley       }
2736ea844a1aSMatthew Knepley     }
2737ea844a1aSMatthew Knepley   } else {
2738ea844a1aSMatthew Knepley     if (orientation >= 0) {
2739ea844a1aSMatthew Knepley       const PetscInt  dim  = s->atlasDof[p];
2740ea844a1aSMatthew Knepley       PetscInt        cInd = 0, i;
2741ea844a1aSMatthew Knepley       const PetscInt *cDof;
2742ea844a1aSMatthew Knepley 
27439566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetConstraintIndices(s, point, &cDof));
2744ea844a1aSMatthew Knepley       if (mode == INSERT_VALUES) {
2745ea844a1aSMatthew Knepley         for (i = 0; i < dim; ++i) {
27469371c9d4SSatish Balay           if ((cInd < cDim) && (i == cDof[cInd])) {
27479371c9d4SSatish Balay             ++cInd;
27489371c9d4SSatish Balay             continue;
27499371c9d4SSatish Balay           }
2750ad716f99SStefano Zampini           array[i] = values ? values[i] : i;
2751ea844a1aSMatthew Knepley         }
2752ea844a1aSMatthew Knepley       } else {
2753ea844a1aSMatthew Knepley         for (i = 0; i < dim; ++i) {
27549371c9d4SSatish Balay           if ((cInd < cDim) && (i == cDof[cInd])) {
27559371c9d4SSatish Balay             ++cInd;
27569371c9d4SSatish Balay             continue;
27579371c9d4SSatish Balay           }
2758ea844a1aSMatthew Knepley           array[i] += values[i];
2759ea844a1aSMatthew Knepley         }
2760ea844a1aSMatthew Knepley       }
2761ea844a1aSMatthew Knepley     } else {
2762ea844a1aSMatthew Knepley       const PetscInt *cDof;
2763ea844a1aSMatthew Knepley       PetscInt        offset  = 0;
2764ea844a1aSMatthew Knepley       PetscInt        cOffset = 0;
2765ea844a1aSMatthew Knepley       PetscInt        j       = 0, field;
2766ea844a1aSMatthew Knepley 
27679566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetConstraintIndices(s, point, &cDof));
2768ea844a1aSMatthew Knepley       for (field = 0; field < s->numFields; ++field) {
2769ea844a1aSMatthew Knepley         const PetscInt dim  = s->field[field]->atlasDof[p];     /* PetscSectionGetFieldDof() */
2770ea844a1aSMatthew Knepley         const PetscInt tDim = s->field[field]->bc->atlasDof[p]; /* PetscSectionGetFieldConstraintDof() */
2771ea844a1aSMatthew Knepley         const PetscInt sDim = dim - tDim;
2772ea844a1aSMatthew Knepley         PetscInt       cInd = 0, i, k;
2773ea844a1aSMatthew Knepley 
2774ea844a1aSMatthew Knepley         for (i = 0, k = dim + offset - 1; i < dim; ++i, ++j, --k) {
27759371c9d4SSatish Balay           if ((cInd < sDim) && (j == cDof[cInd + cOffset])) {
27769371c9d4SSatish Balay             ++cInd;
27779371c9d4SSatish Balay             continue;
27789371c9d4SSatish Balay           }
2779ad716f99SStefano Zampini           array[j] = values ? values[k] : k;
2780ea844a1aSMatthew Knepley         }
2781ea844a1aSMatthew Knepley         offset += dim;
2782ea844a1aSMatthew Knepley         cOffset += dim - tDim;
2783ea844a1aSMatthew Knepley       }
2784ea844a1aSMatthew Knepley     }
2785ea844a1aSMatthew Knepley   }
27863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2787ea844a1aSMatthew Knepley }
2788ea844a1aSMatthew Knepley 
2789cc4c1da9SBarry Smith /*@
279020662ed9SBarry Smith   PetscSectionHasConstraints - Determine whether a `PetscSection` has constrained dofs
2791ea844a1aSMatthew Knepley 
279240750d41SVaclav Hapla   Not Collective
2793ea844a1aSMatthew Knepley 
2794ea844a1aSMatthew Knepley   Input Parameter:
2795cab54364SBarry Smith . s - The `PetscSection`
2796ea844a1aSMatthew Knepley 
2797ea844a1aSMatthew Knepley   Output Parameter:
2798ea844a1aSMatthew Knepley . hasConstraints - flag indicating that the section has constrained dofs
2799ea844a1aSMatthew Knepley 
2800ea844a1aSMatthew Knepley   Level: intermediate
2801ea844a1aSMatthew Knepley 
2802*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSectionSetConstraintIndices()`, `PetscSectionGetConstraintDof()`, `PetscSection`
2803ea844a1aSMatthew Knepley @*/
2804d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionHasConstraints(PetscSection s, PetscBool *hasConstraints)
2805d71ae5a4SJacob Faibussowitsch {
2806ea844a1aSMatthew Knepley   PetscFunctionBegin;
2807ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
28084f572ea9SToby Isaac   PetscAssertPointer(hasConstraints, 2);
2809ea844a1aSMatthew Knepley   *hasConstraints = s->bc ? PETSC_TRUE : PETSC_FALSE;
28103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2811ea844a1aSMatthew Knepley }
2812ea844a1aSMatthew Knepley 
2813ea844a1aSMatthew Knepley /*@C
281420662ed9SBarry Smith   PetscSectionGetConstraintIndices - Get the point dof numbers, in [0, dof), which are constrained for a given point
2815ea844a1aSMatthew Knepley 
281640750d41SVaclav Hapla   Not Collective
2817ea844a1aSMatthew Knepley 
2818ea844a1aSMatthew Knepley   Input Parameters:
2819cab54364SBarry Smith + s     - The `PetscSection`
2820ea844a1aSMatthew Knepley - point - The point
2821ea844a1aSMatthew Knepley 
2822ea844a1aSMatthew Knepley   Output Parameter:
2823ea844a1aSMatthew Knepley . indices - The constrained dofs
2824ea844a1aSMatthew Knepley 
2825ea844a1aSMatthew Knepley   Level: intermediate
2826ea844a1aSMatthew Knepley 
282738b5cf2dSJacob Faibussowitsch   Fortran Notes:
282820662ed9SBarry Smith   Use `PetscSectionGetConstraintIndicesF90()` and `PetscSectionRestoreConstraintIndicesF90()`
2829cab54364SBarry Smith 
2830*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSectionSetConstraintIndices()`, `PetscSectionGetConstraintDof()`, `PetscSection`
2831ea844a1aSMatthew Knepley @*/
2832cc4c1da9SBarry Smith PetscErrorCode PetscSectionGetConstraintIndices(PetscSection s, PetscInt point, const PetscInt *indices[])
2833d71ae5a4SJacob Faibussowitsch {
2834ea844a1aSMatthew Knepley   PetscFunctionBegin;
2835ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2836ea844a1aSMatthew Knepley   if (s->bc) {
2837da8c939bSJacob Faibussowitsch     PetscCall(VecIntGetValuesSection_Private(s->bcIndices, s->bc, point, indices));
2838ea844a1aSMatthew Knepley   } else *indices = NULL;
28393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2840ea844a1aSMatthew Knepley }
2841ea844a1aSMatthew Knepley 
2842cc4c1da9SBarry Smith /*@
2843ea844a1aSMatthew Knepley   PetscSectionSetConstraintIndices - Set the point dof numbers, in [0, dof), which are constrained
2844ea844a1aSMatthew Knepley 
284540750d41SVaclav Hapla   Not Collective
2846ea844a1aSMatthew Knepley 
2847ea844a1aSMatthew Knepley   Input Parameters:
2848cab54364SBarry Smith + s       - The `PetscSection`
2849ea844a1aSMatthew Knepley . point   - The point
2850ea844a1aSMatthew Knepley - indices - The constrained dofs
2851ea844a1aSMatthew Knepley 
2852ea844a1aSMatthew Knepley   Level: intermediate
2853ea844a1aSMatthew Knepley 
285438b5cf2dSJacob Faibussowitsch   Fortran Notes:
2855cab54364SBarry Smith   Use `PetscSectionSetConstraintIndicesF90()`
2856cab54364SBarry Smith 
2857*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSectionGetConstraintIndices()`, `PetscSectionGetConstraintDof()`, `PetscSection`
2858ea844a1aSMatthew Knepley @*/
2859d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetConstraintIndices(PetscSection s, PetscInt point, const PetscInt indices[])
2860d71ae5a4SJacob Faibussowitsch {
2861ea844a1aSMatthew Knepley   PetscFunctionBegin;
2862ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2863ea844a1aSMatthew Knepley   if (s->bc) {
2864d57bb9dbSMatthew G. Knepley     const PetscInt dof  = s->atlasDof[point];
2865d57bb9dbSMatthew G. Knepley     const PetscInt cdof = s->bc->atlasDof[point];
2866d57bb9dbSMatthew G. Knepley     PetscInt       d;
2867d57bb9dbSMatthew G. Knepley 
2868ad716f99SStefano Zampini     if (indices)
2869ad540459SPierre 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]);
2870da8c939bSJacob Faibussowitsch     PetscCall(VecIntSetValuesSection_Private(s->bcIndices, s->bc, point, indices, INSERT_VALUES));
2871ea844a1aSMatthew Knepley   }
28723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2873ea844a1aSMatthew Knepley }
2874ea844a1aSMatthew Knepley 
2875ea844a1aSMatthew Knepley /*@C
2876ea844a1aSMatthew Knepley   PetscSectionGetFieldConstraintIndices - Get the field dof numbers, in [0, fdof), which are constrained
2877ea844a1aSMatthew Knepley 
287840750d41SVaclav Hapla   Not Collective
2879ea844a1aSMatthew Knepley 
2880ea844a1aSMatthew Knepley   Input Parameters:
2881cab54364SBarry Smith + s     - The `PetscSection`
2882ea844a1aSMatthew Knepley . field - The field number
2883ea844a1aSMatthew Knepley - point - The point
2884ea844a1aSMatthew Knepley 
2885ea844a1aSMatthew Knepley   Output Parameter:
28869759eb26SJed Brown . indices - The constrained dofs sorted in ascending order
2887ea844a1aSMatthew Knepley 
2888ea844a1aSMatthew Knepley   Level: intermediate
2889ea844a1aSMatthew Knepley 
2890cab54364SBarry Smith   Note:
2891cab54364SBarry 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()`.
2892cab54364SBarry Smith 
289338b5cf2dSJacob Faibussowitsch   Fortran Notes:
289420662ed9SBarry Smith   Use `PetscSectionGetFieldConstraintIndicesF90()` and `PetscSectionRestoreFieldConstraintIndicesF90()`
2895cab54364SBarry Smith 
2896*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSectionSetFieldConstraintIndices()`, `PetscSectionGetConstraintIndices()`, `PetscSectionGetConstraintDof()`, `PetscSection`
2897ea844a1aSMatthew Knepley @*/
2898d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldConstraintIndices(PetscSection s, PetscInt point, PetscInt field, const PetscInt **indices)
2899d71ae5a4SJacob Faibussowitsch {
2900ea844a1aSMatthew Knepley   PetscFunctionBegin;
2901ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
29024f572ea9SToby Isaac   PetscAssertPointer(indices, 4);
29032abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
29049566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstraintIndices(s->field[field], point, indices));
29053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2906ea844a1aSMatthew Knepley }
2907ea844a1aSMatthew Knepley 
2908ea844a1aSMatthew Knepley /*@C
2909ea844a1aSMatthew Knepley   PetscSectionSetFieldConstraintIndices - Set the field dof numbers, in [0, fdof), which are constrained
2910ea844a1aSMatthew Knepley 
291140750d41SVaclav Hapla   Not Collective
2912ea844a1aSMatthew Knepley 
2913ea844a1aSMatthew Knepley   Input Parameters:
2914cab54364SBarry Smith + s       - The `PetscSection`
2915ea844a1aSMatthew Knepley . point   - The point
2916ea844a1aSMatthew Knepley . field   - The field number
2917ea844a1aSMatthew Knepley - indices - The constrained dofs
2918ea844a1aSMatthew Knepley 
2919ea844a1aSMatthew Knepley   Level: intermediate
2920ea844a1aSMatthew Knepley 
292138b5cf2dSJacob Faibussowitsch   Fortran Notes:
2922cab54364SBarry Smith   Use `PetscSectionSetFieldConstraintIndicesF90()`
2923cab54364SBarry Smith 
2924*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSectionSetConstraintIndices()`, `PetscSectionGetFieldConstraintIndices()`, `PetscSectionGetConstraintDof()`, `PetscSection`
2925ea844a1aSMatthew Knepley @*/
2926d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFieldConstraintIndices(PetscSection s, PetscInt point, PetscInt field, const PetscInt indices[])
2927d71ae5a4SJacob Faibussowitsch {
2928ea844a1aSMatthew Knepley   PetscFunctionBegin;
2929ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
29302abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
29319566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetConstraintIndices(s->field[field], point, indices));
29323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2933ea844a1aSMatthew Knepley }
2934ea844a1aSMatthew Knepley 
2935ea844a1aSMatthew Knepley /*@
2936ea844a1aSMatthew Knepley   PetscSectionPermute - Reorder the section according to the input point permutation
2937ea844a1aSMatthew Knepley 
293840750d41SVaclav Hapla   Collective
2939ea844a1aSMatthew Knepley 
2940d8d19677SJose E. Roman   Input Parameters:
2941cab54364SBarry Smith + section     - The `PetscSection` object
294238b5cf2dSJacob Faibussowitsch - permutation - The point permutation, old point p becomes new point perm[p]
2943ea844a1aSMatthew Knepley 
2944ea844a1aSMatthew Knepley   Output Parameter:
2945cab54364SBarry Smith . sectionNew - The permuted `PetscSection`
2946ea844a1aSMatthew Knepley 
2947ea844a1aSMatthew Knepley   Level: intermediate
2948ea844a1aSMatthew Knepley 
2949583308b6SBarry Smith   Note:
2950583308b6SBarry Smith   The data and the access to the data via `PetscSectionGetFieldOffset()` and `PetscSectionGetOffset()` are both changed in `sectionNew`
2951583308b6SBarry Smith 
2952583308b6SBarry Smith   Compare to `PetscSectionSetPermutation()`
2953583308b6SBarry Smith 
2954*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `IS`, `PetscSection`, `MatPermute()`, `PetscSectionSetPermutation()`
2955ea844a1aSMatthew Knepley @*/
2956d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionPermute(PetscSection section, IS permutation, PetscSection *sectionNew)
2957d71ae5a4SJacob Faibussowitsch {
2958ea844a1aSMatthew Knepley   PetscSection    s = section, sNew;
2959ea844a1aSMatthew Knepley   const PetscInt *perm;
2960b778fa18SValeria Barra   PetscInt        numFields, f, c, numPoints, pStart, pEnd, p;
2961ea844a1aSMatthew Knepley 
2962ea844a1aSMatthew Knepley   PetscFunctionBegin;
2963ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
2964ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(permutation, IS_CLASSID, 2);
29654f572ea9SToby Isaac   PetscAssertPointer(sectionNew, 3);
29669566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)s), &sNew));
29679566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &numFields));
29689566063dSJacob Faibussowitsch   if (numFields) PetscCall(PetscSectionSetNumFields(sNew, numFields));
2969ea844a1aSMatthew Knepley   for (f = 0; f < numFields; ++f) {
2970ea844a1aSMatthew Knepley     const char *name;
2971ea844a1aSMatthew Knepley     PetscInt    numComp;
2972ea844a1aSMatthew Knepley 
29739566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldName(s, f, &name));
29749566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldName(sNew, f, name));
29759566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &numComp));
29769566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(sNew, f, numComp));
2977b778fa18SValeria Barra     for (c = 0; c < s->numFieldComponents[f]; ++c) {
29789566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetComponentName(s, f, c, &name));
29799566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetComponentName(sNew, f, c, name));
2980b778fa18SValeria Barra     }
2981ea844a1aSMatthew Knepley   }
29829566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(permutation, &numPoints));
29839566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(permutation, &perm));
29849566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
29859566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(sNew, pStart, pEnd));
298608401ef6SPierre 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);
2987ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
2988ea844a1aSMatthew Knepley     PetscInt dof, cdof;
2989ea844a1aSMatthew Knepley 
29909566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(s, p, &dof));
29919566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(sNew, perm[p], dof));
29929566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
29939566063dSJacob Faibussowitsch     if (cdof) PetscCall(PetscSectionSetConstraintDof(sNew, perm[p], cdof));
2994ea844a1aSMatthew Knepley     for (f = 0; f < numFields; ++f) {
29959566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
29969566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(sNew, perm[p], f, dof));
29979566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(s, p, f, &cdof));
29989566063dSJacob Faibussowitsch       if (cdof) PetscCall(PetscSectionSetFieldConstraintDof(sNew, perm[p], f, cdof));
2999ea844a1aSMatthew Knepley     }
3000ea844a1aSMatthew Knepley   }
30019566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(sNew));
3002ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
3003ea844a1aSMatthew Knepley     const PetscInt *cind;
3004ea844a1aSMatthew Knepley     PetscInt        cdof;
3005ea844a1aSMatthew Knepley 
30069566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
3007ea844a1aSMatthew Knepley     if (cdof) {
30089566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetConstraintIndices(s, p, &cind));
30099566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetConstraintIndices(sNew, perm[p], cind));
3010ea844a1aSMatthew Knepley     }
3011ea844a1aSMatthew Knepley     for (f = 0; f < numFields; ++f) {
30129566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldConstraintDof(s, p, f, &cdof));
3013ea844a1aSMatthew Knepley       if (cdof) {
30149566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldConstraintIndices(s, p, f, &cind));
30159566063dSJacob Faibussowitsch         PetscCall(PetscSectionSetFieldConstraintIndices(sNew, perm[p], f, cind));
3016ea844a1aSMatthew Knepley       }
3017ea844a1aSMatthew Knepley     }
3018ea844a1aSMatthew Knepley   }
30199566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(permutation, &perm));
3020ea844a1aSMatthew Knepley   *sectionNew = sNew;
30213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3022ea844a1aSMatthew Knepley }
3023ea844a1aSMatthew Knepley 
3024ea844a1aSMatthew Knepley /*@
302520662ed9SBarry Smith   PetscSectionSetClosureIndex - Create an internal data structure to speed up closure queries.
3026ea844a1aSMatthew Knepley 
302740750d41SVaclav Hapla   Collective
3028ea844a1aSMatthew Knepley 
3029ea844a1aSMatthew Knepley   Input Parameters:
3030cab54364SBarry Smith + section   - The `PetscSection`
3031cab54364SBarry Smith . obj       - A `PetscObject` which serves as the key for this index
3032cab54364SBarry Smith . clSection - `PetscSection` giving the size of the closure of each point
3033cab54364SBarry Smith - clPoints  - `IS` giving the points in each closure
3034ea844a1aSMatthew Knepley 
3035ea844a1aSMatthew Knepley   Level: advanced
3036ea844a1aSMatthew Knepley 
3037cab54364SBarry Smith   Note:
303820662ed9SBarry Smith   This function creates an internal map from each point to its closure. We compress out closure points with no dofs in this section.
303920662ed9SBarry Smith 
304038b5cf2dSJacob Faibussowitsch   Developer Notes:
304120662ed9SBarry Smith   The information provided here is completely opaque
3042cab54364SBarry Smith 
3043*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionGetClosureIndex()`, `DMPlexCreateClosureIndex()`
3044ea844a1aSMatthew Knepley @*/
3045d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetClosureIndex(PetscSection section, PetscObject obj, PetscSection clSection, IS clPoints)
3046d71ae5a4SJacob Faibussowitsch {
3047ea844a1aSMatthew Knepley   PetscFunctionBegin;
30483e03ebd8SStefano Zampini   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
30493e03ebd8SStefano Zampini   PetscValidHeaderSpecific(clSection, PETSC_SECTION_CLASSID, 3);
30503e03ebd8SStefano Zampini   PetscValidHeaderSpecific(clPoints, IS_CLASSID, 4);
3051d4a1ad33SMatthew G. Knepley   if (section->clObj != obj) PetscCall(PetscSectionResetClosurePermutation(section));
3052ea844a1aSMatthew Knepley   section->clObj = obj;
30539566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)clSection));
30549566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)clPoints));
30559566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&section->clSection));
30569566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&section->clPoints));
3057ea844a1aSMatthew Knepley   section->clSection = clSection;
3058ea844a1aSMatthew Knepley   section->clPoints  = clPoints;
30593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3060ea844a1aSMatthew Knepley }
3061ea844a1aSMatthew Knepley 
3062ea844a1aSMatthew Knepley /*@
3063cab54364SBarry Smith   PetscSectionGetClosureIndex - Get the cache of points in the closure of each point in the section set with `PetscSectionSetClosureIndex()`
3064ea844a1aSMatthew Knepley 
306540750d41SVaclav Hapla   Collective
3066ea844a1aSMatthew Knepley 
3067ea844a1aSMatthew Knepley   Input Parameters:
3068cab54364SBarry Smith + section - The `PetscSection`
3069cab54364SBarry Smith - obj     - A `PetscObject` which serves as the key for this index
3070ea844a1aSMatthew Knepley 
3071ea844a1aSMatthew Knepley   Output Parameters:
3072cab54364SBarry Smith + clSection - `PetscSection` giving the size of the closure of each point
3073cab54364SBarry Smith - clPoints  - `IS` giving the points in each closure
3074ea844a1aSMatthew Knepley 
3075ea844a1aSMatthew Knepley   Level: advanced
3076ea844a1aSMatthew Knepley 
3077*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSectionSetClosureIndex()`, `DMPlexCreateClosureIndex()`
3078ea844a1aSMatthew Knepley @*/
3079d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetClosureIndex(PetscSection section, PetscObject obj, PetscSection *clSection, IS *clPoints)
3080d71ae5a4SJacob Faibussowitsch {
3081ea844a1aSMatthew Knepley   PetscFunctionBegin;
3082ea844a1aSMatthew Knepley   if (section->clObj == obj) {
3083ea844a1aSMatthew Knepley     if (clSection) *clSection = section->clSection;
3084ea844a1aSMatthew Knepley     if (clPoints) *clPoints = section->clPoints;
3085ea844a1aSMatthew Knepley   } else {
3086ea844a1aSMatthew Knepley     if (clSection) *clSection = NULL;
3087ea844a1aSMatthew Knepley     if (clPoints) *clPoints = NULL;
3088ea844a1aSMatthew Knepley   }
30893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3090ea844a1aSMatthew Knepley }
3091ea844a1aSMatthew Knepley 
3092d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetClosurePermutation_Internal(PetscSection section, PetscObject obj, PetscInt depth, PetscInt clSize, PetscCopyMode mode, PetscInt *clPerm)
3093d71ae5a4SJacob Faibussowitsch {
3094ea844a1aSMatthew Knepley   PetscInt                    i;
3095c459fbc1SJed Brown   khiter_t                    iter;
3096c459fbc1SJed Brown   int                         new_entry;
3097c459fbc1SJed Brown   PetscSectionClosurePermKey  key = {depth, clSize};
3098c459fbc1SJed Brown   PetscSectionClosurePermVal *val;
3099ea844a1aSMatthew Knepley 
3100ea844a1aSMatthew Knepley   PetscFunctionBegin;
3101ea844a1aSMatthew Knepley   if (section->clObj != obj) {
31029566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&section->clSection));
31039566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&section->clPoints));
3104ea844a1aSMatthew Knepley   }
3105ea844a1aSMatthew Knepley   section->clObj = obj;
31069566063dSJacob Faibussowitsch   if (!section->clHash) PetscCall(PetscClPermCreate(&section->clHash));
3107c459fbc1SJed Brown   iter = kh_put(ClPerm, section->clHash, key, &new_entry);
3108c459fbc1SJed Brown   val  = &kh_val(section->clHash, iter);
3109c459fbc1SJed Brown   if (!new_entry) {
31109566063dSJacob Faibussowitsch     PetscCall(PetscFree(val->perm));
31119566063dSJacob Faibussowitsch     PetscCall(PetscFree(val->invPerm));
3112c459fbc1SJed Brown   }
3113ea844a1aSMatthew Knepley   if (mode == PETSC_COPY_VALUES) {
31149566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(clSize, &val->perm));
31159566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(val->perm, clPerm, clSize));
3116ea844a1aSMatthew Knepley   } else if (mode == PETSC_OWN_POINTER) {
3117c459fbc1SJed Brown     val->perm = clPerm;
3118ea844a1aSMatthew Knepley   } else SETERRQ(PetscObjectComm(obj), PETSC_ERR_SUP, "Do not support borrowed arrays");
31199566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(clSize, &val->invPerm));
3120c459fbc1SJed Brown   for (i = 0; i < clSize; ++i) val->invPerm[clPerm[i]] = i;
31213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3122ea844a1aSMatthew Knepley }
3123ea844a1aSMatthew Knepley 
3124ea844a1aSMatthew Knepley /*@
3125c459fbc1SJed Brown   PetscSectionSetClosurePermutation - Set the dof permutation for the closure of each cell in the section, meaning clPerm[newIndex] = oldIndex.
3126ea844a1aSMatthew Knepley 
3127ea844a1aSMatthew Knepley   Not Collective
3128ea844a1aSMatthew Knepley 
3129ea844a1aSMatthew Knepley   Input Parameters:
3130cab54364SBarry Smith + section - The `PetscSection`
3131cab54364SBarry Smith . obj     - A `PetscObject` which serves as the key for this index (usually a `DM`)
3132c459fbc1SJed Brown . depth   - Depth of points on which to apply the given permutation
3133ea844a1aSMatthew Knepley - perm    - Permutation of the cell dof closure
3134ea844a1aSMatthew Knepley 
3135cab54364SBarry Smith   Level: intermediate
3136cab54364SBarry Smith 
3137cab54364SBarry Smith   Notes:
3138c459fbc1SJed Brown   The specified permutation will only be applied to points at depth whose closure size matches the length of perm.  In a
3139c459fbc1SJed Brown   mixed-topology or variable-degree finite element space, this function can be called multiple times at each depth for
3140c459fbc1SJed Brown   each topology and degree.
3141c459fbc1SJed Brown 
3142c459fbc1SJed Brown   This approach assumes that (depth, len(perm)) uniquely identifies the desired permutation; this might not be true for
3143c459fbc1SJed Brown   exotic/enriched spaces on mixed topology meshes.
3144ea844a1aSMatthew Knepley 
3145*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `IS`, `PetscSectionGetClosurePermutation()`, `PetscSectionGetClosureIndex()`, `DMPlexCreateClosureIndex()`, `PetscCopyMode`
3146ea844a1aSMatthew Knepley @*/
3147d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetClosurePermutation(PetscSection section, PetscObject obj, PetscInt depth, IS perm)
3148d71ae5a4SJacob Faibussowitsch {
3149ea844a1aSMatthew Knepley   const PetscInt *clPerm = NULL;
3150ea844a1aSMatthew Knepley   PetscInt        clSize = 0;
3151ea844a1aSMatthew Knepley 
3152ea844a1aSMatthew Knepley   PetscFunctionBegin;
3153ea844a1aSMatthew Knepley   if (perm) {
31549566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(perm, &clSize));
31559566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(perm, &clPerm));
3156ea844a1aSMatthew Knepley   }
31579566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetClosurePermutation_Internal(section, obj, depth, clSize, PETSC_COPY_VALUES, (PetscInt *)clPerm));
31589566063dSJacob Faibussowitsch   if (perm) PetscCall(ISRestoreIndices(perm, &clPerm));
31593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3160ea844a1aSMatthew Knepley }
3161ea844a1aSMatthew Knepley 
3162da8c939bSJacob Faibussowitsch static PetscErrorCode PetscSectionGetClosurePermutation_Private(PetscSection section, PetscObject obj, PetscInt depth, PetscInt size, const PetscInt *perm[])
3163d71ae5a4SJacob Faibussowitsch {
3164ea844a1aSMatthew Knepley   PetscFunctionBegin;
3165ea844a1aSMatthew Knepley   if (section->clObj == obj) {
3166c459fbc1SJed Brown     PetscSectionClosurePermKey k = {depth, size};
3167c459fbc1SJed Brown     PetscSectionClosurePermVal v;
3168da8c939bSJacob Faibussowitsch 
31699566063dSJacob Faibussowitsch     PetscCall(PetscClPermGet(section->clHash, k, &v));
3170c459fbc1SJed Brown     if (perm) *perm = v.perm;
3171ea844a1aSMatthew Knepley   } else {
3172ea844a1aSMatthew Knepley     if (perm) *perm = NULL;
3173ea844a1aSMatthew Knepley   }
31743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3175ea844a1aSMatthew Knepley }
3176ea844a1aSMatthew Knepley 
3177ea844a1aSMatthew Knepley /*@
3178ea844a1aSMatthew Knepley   PetscSectionGetClosurePermutation - Get the dof permutation for the closure of each cell in the section, meaning clPerm[newIndex] = oldIndex.
3179ea844a1aSMatthew Knepley 
318040750d41SVaclav Hapla   Not Collective
3181ea844a1aSMatthew Knepley 
3182ea844a1aSMatthew Knepley   Input Parameters:
3183cab54364SBarry Smith + section - The `PetscSection`
3184cab54364SBarry Smith . obj     - A `PetscObject` which serves as the key for this index (usually a DM)
3185c459fbc1SJed Brown . depth   - Depth stratum on which to obtain closure permutation
3186c459fbc1SJed Brown - clSize  - Closure size to be permuted (e.g., may vary with element topology and degree)
3187ea844a1aSMatthew Knepley 
3188ea844a1aSMatthew Knepley   Output Parameter:
3189ea844a1aSMatthew Knepley . perm - The dof closure permutation
3190ea844a1aSMatthew Knepley 
3191ea844a1aSMatthew Knepley   Level: intermediate
3192ea844a1aSMatthew Knepley 
3193cab54364SBarry Smith   Note:
3194cab54364SBarry Smith   The user must destroy the `IS` that is returned.
3195cab54364SBarry Smith 
3196*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `IS`, `PetscSectionSetClosurePermutation()`, `PetscSectionGetClosureInversePermutation()`, `PetscSectionGetClosureIndex()`, `PetscSectionSetClosureIndex()`, `DMPlexCreateClosureIndex()`
3197ea844a1aSMatthew Knepley @*/
3198d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetClosurePermutation(PetscSection section, PetscObject obj, PetscInt depth, PetscInt clSize, IS *perm)
3199d71ae5a4SJacob Faibussowitsch {
3200da8c939bSJacob Faibussowitsch   const PetscInt *clPerm = NULL;
3201ea844a1aSMatthew Knepley 
3202ea844a1aSMatthew Knepley   PetscFunctionBegin;
3203da8c939bSJacob Faibussowitsch   PetscCall(PetscSectionGetClosurePermutation_Private(section, obj, depth, clSize, &clPerm));
32045f82726aSMatthew 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);
32059566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, clSize, clPerm, PETSC_USE_POINTER, perm));
32063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3207ea844a1aSMatthew Knepley }
3208ea844a1aSMatthew Knepley 
3209d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetClosureInversePermutation_Internal(PetscSection section, PetscObject obj, PetscInt depth, PetscInt size, const PetscInt *perm[])
3210d71ae5a4SJacob Faibussowitsch {
3211ea844a1aSMatthew Knepley   PetscFunctionBegin;
3212c459fbc1SJed Brown   if (section->clObj == obj && section->clHash) {
3213c459fbc1SJed Brown     PetscSectionClosurePermKey k = {depth, size};
3214c459fbc1SJed Brown     PetscSectionClosurePermVal v;
32159566063dSJacob Faibussowitsch     PetscCall(PetscClPermGet(section->clHash, k, &v));
3216c459fbc1SJed Brown     if (perm) *perm = v.invPerm;
3217ea844a1aSMatthew Knepley   } else {
3218ea844a1aSMatthew Knepley     if (perm) *perm = NULL;
3219ea844a1aSMatthew Knepley   }
32203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3221ea844a1aSMatthew Knepley }
3222ea844a1aSMatthew Knepley 
3223ea844a1aSMatthew Knepley /*@
3224ea844a1aSMatthew Knepley   PetscSectionGetClosureInversePermutation - Get the inverse dof permutation for the closure of each cell in the section, meaning clPerm[oldIndex] = newIndex.
3225ea844a1aSMatthew Knepley 
322640750d41SVaclav Hapla   Not Collective
3227ea844a1aSMatthew Knepley 
3228ea844a1aSMatthew Knepley   Input Parameters:
3229cab54364SBarry Smith + section - The `PetscSection`
3230cab54364SBarry Smith . obj     - A `PetscObject` which serves as the key for this index (usually a `DM`)
3231c459fbc1SJed Brown . depth   - Depth stratum on which to obtain closure permutation
3232c459fbc1SJed Brown - clSize  - Closure size to be permuted (e.g., may vary with element topology and degree)
3233ea844a1aSMatthew Knepley 
32342fe279fdSBarry Smith   Output Parameter:
3235c459fbc1SJed Brown . perm - The dof closure permutation
3236ea844a1aSMatthew Knepley 
3237ea844a1aSMatthew Knepley   Level: intermediate
3238ea844a1aSMatthew Knepley 
3239cab54364SBarry Smith   Note:
3240cab54364SBarry Smith   The user must destroy the `IS` that is returned.
3241cab54364SBarry Smith 
3242*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `IS`, `PetscSectionSetClosurePermutation()`, `PetscSectionGetClosureIndex()`, `PetscSectionSetClosureIndex()`, `DMPlexCreateClosureIndex()`
3243ea844a1aSMatthew Knepley @*/
3244d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetClosureInversePermutation(PetscSection section, PetscObject obj, PetscInt depth, PetscInt clSize, IS *perm)
3245d71ae5a4SJacob Faibussowitsch {
3246da8c939bSJacob Faibussowitsch   const PetscInt *clPerm = NULL;
3247ea844a1aSMatthew Knepley 
3248ea844a1aSMatthew Knepley   PetscFunctionBegin;
32499566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetClosureInversePermutation_Internal(section, obj, depth, clSize, &clPerm));
32509566063dSJacob Faibussowitsch   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, clSize, clPerm, PETSC_USE_POINTER, perm));
32513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3252ea844a1aSMatthew Knepley }
3253ea844a1aSMatthew Knepley 
3254ea844a1aSMatthew Knepley /*@
3255583308b6SBarry Smith   PetscSectionGetField - Get the `PetscSection` associated with a single field
3256ea844a1aSMatthew Knepley 
3257ea844a1aSMatthew Knepley   Input Parameters:
3258cab54364SBarry Smith + s     - The `PetscSection`
3259ea844a1aSMatthew Knepley - field - The field number
3260ea844a1aSMatthew Knepley 
3261ea844a1aSMatthew Knepley   Output Parameter:
3262583308b6SBarry Smith . subs - The `PetscSection` for the given field, note the chart of `subs` is not set
3263ea844a1aSMatthew Knepley 
3264ea844a1aSMatthew Knepley   Level: intermediate
3265ea844a1aSMatthew Knepley 
3266cab54364SBarry Smith   Note:
3267cab54364SBarry Smith   Does not increase the reference count of the selected sub-section. There is no matching `PetscSectionRestoreField()`
3268cab54364SBarry Smith 
3269*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `IS`, `PetscSectionSetNumFields()`
3270ea844a1aSMatthew Knepley @*/
3271d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetField(PetscSection s, PetscInt field, PetscSection *subs)
3272d71ae5a4SJacob Faibussowitsch {
3273ea844a1aSMatthew Knepley   PetscFunctionBegin;
3274ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
32754f572ea9SToby Isaac   PetscAssertPointer(subs, 3);
32762abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, s->numFields);
3277ea844a1aSMatthew Knepley   *subs = s->field[field];
32783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3279ea844a1aSMatthew Knepley }
3280ea844a1aSMatthew Knepley 
3281ea844a1aSMatthew Knepley PetscClassId      PETSC_SECTION_SYM_CLASSID;
3282ea844a1aSMatthew Knepley PetscFunctionList PetscSectionSymList = NULL;
3283ea844a1aSMatthew Knepley 
3284ea844a1aSMatthew Knepley /*@
3285cab54364SBarry Smith   PetscSectionSymCreate - Creates an empty `PetscSectionSym` object.
3286ea844a1aSMatthew Knepley 
3287ea844a1aSMatthew Knepley   Collective
3288ea844a1aSMatthew Knepley 
3289ea844a1aSMatthew Knepley   Input Parameter:
3290ea844a1aSMatthew Knepley . comm - the MPI communicator
3291ea844a1aSMatthew Knepley 
3292ea844a1aSMatthew Knepley   Output Parameter:
3293ea844a1aSMatthew Knepley . sym - pointer to the new set of symmetries
3294ea844a1aSMatthew Knepley 
3295ea844a1aSMatthew Knepley   Level: developer
3296ea844a1aSMatthew Knepley 
3297*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSection`, `PetscSectionSym`, `PetscSectionSymDestroy()`
3298ea844a1aSMatthew Knepley @*/
3299d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymCreate(MPI_Comm comm, PetscSectionSym *sym)
3300d71ae5a4SJacob Faibussowitsch {
3301ea844a1aSMatthew Knepley   PetscFunctionBegin;
33024f572ea9SToby Isaac   PetscAssertPointer(sym, 2);
33039566063dSJacob Faibussowitsch   PetscCall(ISInitializePackage());
3304377f809aSBarry Smith 
33059566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(*sym, PETSC_SECTION_SYM_CLASSID, "PetscSectionSym", "Section Symmetry", "IS", comm, PetscSectionSymDestroy, PetscSectionSymView));
33063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3307ea844a1aSMatthew Knepley }
3308ea844a1aSMatthew Knepley 
3309cc4c1da9SBarry Smith /*@
3310cab54364SBarry Smith   PetscSectionSymSetType - Builds a `PetscSectionSym`, for a particular implementation.
3311ea844a1aSMatthew Knepley 
331240750d41SVaclav Hapla   Collective
3313ea844a1aSMatthew Knepley 
3314ea844a1aSMatthew Knepley   Input Parameters:
3315ea844a1aSMatthew Knepley + sym    - The section symmetry object
3316ea844a1aSMatthew Knepley - method - The name of the section symmetry type
3317ea844a1aSMatthew Knepley 
3318ea844a1aSMatthew Knepley   Level: developer
3319ea844a1aSMatthew Knepley 
3320*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSectionSym`, `PetscSectionSymType`, `PetscSectionSymGetType()`, `PetscSectionSymCreate()`
3321ea844a1aSMatthew Knepley @*/
3322d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymSetType(PetscSectionSym sym, PetscSectionSymType method)
3323d71ae5a4SJacob Faibussowitsch {
3324ea844a1aSMatthew Knepley   PetscErrorCode (*r)(PetscSectionSym);
3325ea844a1aSMatthew Knepley   PetscBool match;
3326ea844a1aSMatthew Knepley 
3327ea844a1aSMatthew Knepley   PetscFunctionBegin;
3328ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(sym, PETSC_SECTION_SYM_CLASSID, 1);
33299566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)sym, method, &match));
33303ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
3331ea844a1aSMatthew Knepley 
33329566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(PetscSectionSymList, method, &r));
33336adde796SStefano Zampini   PetscCheck(r, PetscObjectComm((PetscObject)sym), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscSectionSym type: %s", method);
3334dbbe0bcdSBarry Smith   PetscTryTypeMethod(sym, destroy);
3335ea844a1aSMatthew Knepley   sym->ops->destroy = NULL;
3336dbbe0bcdSBarry Smith 
33379566063dSJacob Faibussowitsch   PetscCall((*r)(sym));
33389566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)sym, method));
33393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3340ea844a1aSMatthew Knepley }
3341ea844a1aSMatthew Knepley 
3342cc4c1da9SBarry Smith /*@
3343cab54364SBarry Smith   PetscSectionSymGetType - Gets the section symmetry type name (as a string) from the `PetscSectionSym`.
3344ea844a1aSMatthew Knepley 
3345ea844a1aSMatthew Knepley   Not Collective
3346ea844a1aSMatthew Knepley 
3347ea844a1aSMatthew Knepley   Input Parameter:
3348ea844a1aSMatthew Knepley . sym - The section symmetry
3349ea844a1aSMatthew Knepley 
3350ea844a1aSMatthew Knepley   Output Parameter:
3351ea844a1aSMatthew Knepley . type - The index set type name
3352ea844a1aSMatthew Knepley 
3353ea844a1aSMatthew Knepley   Level: developer
3354ea844a1aSMatthew Knepley 
3355*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSectionSym`, `PetscSectionSymType`, `PetscSectionSymSetType()`, `PetscSectionSymCreate()`
3356ea844a1aSMatthew Knepley @*/
3357d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymGetType(PetscSectionSym sym, PetscSectionSymType *type)
3358d71ae5a4SJacob Faibussowitsch {
3359ea844a1aSMatthew Knepley   PetscFunctionBegin;
3360ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(sym, PETSC_SECTION_SYM_CLASSID, 1);
33614f572ea9SToby Isaac   PetscAssertPointer(type, 2);
3362ea844a1aSMatthew Knepley   *type = ((PetscObject)sym)->type_name;
33633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3364ea844a1aSMatthew Knepley }
3365ea844a1aSMatthew Knepley 
3366ea844a1aSMatthew Knepley /*@C
3367cab54364SBarry Smith   PetscSectionSymRegister - Registers a new section symmetry implementation
3368ea844a1aSMatthew Knepley 
3369cc4c1da9SBarry Smith   Not Collective, No Fortran Support
3370ea844a1aSMatthew Knepley 
3371ea844a1aSMatthew Knepley   Input Parameters:
33722fe279fdSBarry Smith + sname    - The name of a new user-defined creation routine
33732fe279fdSBarry Smith - function - The creation routine itself
3374ea844a1aSMatthew Knepley 
3375ea844a1aSMatthew Knepley   Level: developer
3376ea844a1aSMatthew Knepley 
3377cab54364SBarry Smith   Notes:
3378cab54364SBarry Smith   `PetscSectionSymRegister()` may be called multiple times to add several user-defined vectors
3379cab54364SBarry Smith 
3380*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSectionSym`, `PetscSectionSymType`, `PetscSectionSymCreate()`, `PetscSectionSymSetType()`
3381ea844a1aSMatthew Knepley @*/
3382d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymRegister(const char sname[], PetscErrorCode (*function)(PetscSectionSym))
3383d71ae5a4SJacob Faibussowitsch {
3384ea844a1aSMatthew Knepley   PetscFunctionBegin;
33859566063dSJacob Faibussowitsch   PetscCall(ISInitializePackage());
33869566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&PetscSectionSymList, sname, function));
33873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3388ea844a1aSMatthew Knepley }
3389ea844a1aSMatthew Knepley 
3390ea844a1aSMatthew Knepley /*@
3391ea844a1aSMatthew Knepley   PetscSectionSymDestroy - Destroys a section symmetry.
3392ea844a1aSMatthew Knepley 
339340750d41SVaclav Hapla   Collective
3394ea844a1aSMatthew Knepley 
33952fe279fdSBarry Smith   Input Parameter:
3396ea844a1aSMatthew Knepley . sym - the section symmetry
3397ea844a1aSMatthew Knepley 
3398ea844a1aSMatthew Knepley   Level: developer
3399ea844a1aSMatthew Knepley 
3400*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSectionSym`, `PetscSectionSymCreate()`
3401ea844a1aSMatthew Knepley @*/
3402d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymDestroy(PetscSectionSym *sym)
3403d71ae5a4SJacob Faibussowitsch {
3404ea844a1aSMatthew Knepley   SymWorkLink link, next;
3405ea844a1aSMatthew Knepley 
3406ea844a1aSMatthew Knepley   PetscFunctionBegin;
34073ba16761SJacob Faibussowitsch   if (!*sym) PetscFunctionReturn(PETSC_SUCCESS);
3408f4f49eeaSPierre Jolivet   PetscValidHeaderSpecific(*sym, PETSC_SECTION_SYM_CLASSID, 1);
3409f4f49eeaSPierre Jolivet   if (--((PetscObject)*sym)->refct > 0) {
34109371c9d4SSatish Balay     *sym = NULL;
34113ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
3412ea844a1aSMatthew Knepley   }
3413213acdd3SPierre Jolivet   PetscTryTypeMethod(*sym, destroy);
3414c9cc58a2SBarry Smith   PetscCheck(!(*sym)->workout, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Work array still checked out");
3415ea844a1aSMatthew Knepley   for (link = (*sym)->workin; link; link = next) {
34160c99d500SBarry Smith     PetscInt    **perms = (PetscInt **)link->perms;
34170c99d500SBarry Smith     PetscScalar **rots  = (PetscScalar **)link->rots;
34180c99d500SBarry Smith     PetscCall(PetscFree2(perms, rots));
3419ea844a1aSMatthew Knepley     next = link->next;
34209566063dSJacob Faibussowitsch     PetscCall(PetscFree(link));
3421ea844a1aSMatthew Knepley   }
3422ea844a1aSMatthew Knepley   (*sym)->workin = NULL;
34239566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(sym));
34243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3425ea844a1aSMatthew Knepley }
3426ea844a1aSMatthew Knepley 
3427ffeef943SBarry Smith /*@
3428ea844a1aSMatthew Knepley   PetscSectionSymView - Displays a section symmetry
3429ea844a1aSMatthew Knepley 
343040750d41SVaclav Hapla   Collective
3431ea844a1aSMatthew Knepley 
3432ea844a1aSMatthew Knepley   Input Parameters:
3433ea844a1aSMatthew Knepley + sym    - the index set
3434cab54364SBarry Smith - viewer - viewer used to display the set, for example `PETSC_VIEWER_STDOUT_SELF`.
3435ea844a1aSMatthew Knepley 
3436ea844a1aSMatthew Knepley   Level: developer
3437ea844a1aSMatthew Knepley 
3438cab54364SBarry Smith .seealso: `PetscSectionSym`, `PetscViewer`, `PetscViewerASCIIOpen()`
3439ea844a1aSMatthew Knepley @*/
3440d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymView(PetscSectionSym sym, PetscViewer viewer)
3441d71ae5a4SJacob Faibussowitsch {
3442ea844a1aSMatthew Knepley   PetscFunctionBegin;
3443ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(sym, PETSC_SECTION_SYM_CLASSID, 1);
344448a46eb9SPierre Jolivet   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)sym), &viewer));
3445ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
3446ea844a1aSMatthew Knepley   PetscCheckSameComm(sym, 1, viewer, 2);
34479566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)sym, viewer));
3448dbbe0bcdSBarry Smith   PetscTryTypeMethod(sym, view, viewer);
34493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3450ea844a1aSMatthew Knepley }
3451ea844a1aSMatthew Knepley 
3452ea844a1aSMatthew Knepley /*@
3453ea844a1aSMatthew Knepley   PetscSectionSetSym - Set the symmetries for the data referred to by the section
3454ea844a1aSMatthew Knepley 
345540750d41SVaclav Hapla   Collective
3456ea844a1aSMatthew Knepley 
3457ea844a1aSMatthew Knepley   Input Parameters:
3458ea844a1aSMatthew Knepley + section - the section describing data layout
3459ea844a1aSMatthew Knepley - sym     - the symmetry describing the affect of orientation on the access of the data
3460ea844a1aSMatthew Knepley 
3461ea844a1aSMatthew Knepley   Level: developer
3462ea844a1aSMatthew Knepley 
3463*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSectionSym`, `PetscSectionGetSym()`, `PetscSectionSymCreate()`
3464ea844a1aSMatthew Knepley @*/
3465d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetSym(PetscSection section, PetscSectionSym sym)
3466d71ae5a4SJacob Faibussowitsch {
3467ea844a1aSMatthew Knepley   PetscFunctionBegin;
3468ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
3469f4f49eeaSPierre Jolivet   PetscCall(PetscSectionSymDestroy(&section->sym));
3470ea844a1aSMatthew Knepley   if (sym) {
3471ea844a1aSMatthew Knepley     PetscValidHeaderSpecific(sym, PETSC_SECTION_SYM_CLASSID, 2);
3472ea844a1aSMatthew Knepley     PetscCheckSameComm(section, 1, sym, 2);
34739566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)sym));
3474ea844a1aSMatthew Knepley   }
3475ea844a1aSMatthew Knepley   section->sym = sym;
34763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3477ea844a1aSMatthew Knepley }
3478ea844a1aSMatthew Knepley 
3479ea844a1aSMatthew Knepley /*@
3480ea844a1aSMatthew Knepley   PetscSectionGetSym - Get the symmetries for the data referred to by the section
3481ea844a1aSMatthew Knepley 
348240750d41SVaclav Hapla   Not Collective
3483ea844a1aSMatthew Knepley 
34842fe279fdSBarry Smith   Input Parameter:
3485ea844a1aSMatthew Knepley . section - the section describing data layout
3486ea844a1aSMatthew Knepley 
34872fe279fdSBarry Smith   Output Parameter:
348820662ed9SBarry Smith . sym - the symmetry describing the affect of orientation on the access of the data, provided previously by `PetscSectionSetSym()`
3489ea844a1aSMatthew Knepley 
3490ea844a1aSMatthew Knepley   Level: developer
3491ea844a1aSMatthew Knepley 
3492*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSectionSym`, `PetscSectionSetSym()`, `PetscSectionSymCreate()`
3493ea844a1aSMatthew Knepley @*/
3494d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetSym(PetscSection section, PetscSectionSym *sym)
3495d71ae5a4SJacob Faibussowitsch {
3496ea844a1aSMatthew Knepley   PetscFunctionBegin;
3497ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
3498ea844a1aSMatthew Knepley   *sym = section->sym;
34993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3500ea844a1aSMatthew Knepley }
3501ea844a1aSMatthew Knepley 
3502ea844a1aSMatthew Knepley /*@
3503ea844a1aSMatthew Knepley   PetscSectionSetFieldSym - Set the symmetries for the data referred to by a field of the section
3504ea844a1aSMatthew Knepley 
350540750d41SVaclav Hapla   Collective
3506ea844a1aSMatthew Knepley 
3507ea844a1aSMatthew Knepley   Input Parameters:
3508ea844a1aSMatthew Knepley + section - the section describing data layout
3509ea844a1aSMatthew Knepley . field   - the field number
3510ea844a1aSMatthew Knepley - sym     - the symmetry describing the affect of orientation on the access of the data
3511ea844a1aSMatthew Knepley 
3512ea844a1aSMatthew Knepley   Level: developer
3513ea844a1aSMatthew Knepley 
3514*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSectionSym`, `PetscSectionGetFieldSym()`, `PetscSectionSymCreate()`
3515ea844a1aSMatthew Knepley @*/
3516d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetFieldSym(PetscSection section, PetscInt field, PetscSectionSym sym)
3517d71ae5a4SJacob Faibussowitsch {
3518ea844a1aSMatthew Knepley   PetscFunctionBegin;
3519ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
35202abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, section->numFields);
35219566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetSym(section->field[field], sym));
35223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3523ea844a1aSMatthew Knepley }
3524ea844a1aSMatthew Knepley 
3525ea844a1aSMatthew Knepley /*@
3526ea844a1aSMatthew Knepley   PetscSectionGetFieldSym - Get the symmetries for the data referred to by a field of the section
3527ea844a1aSMatthew Knepley 
352840750d41SVaclav Hapla   Collective
3529ea844a1aSMatthew Knepley 
3530ea844a1aSMatthew Knepley   Input Parameters:
3531ea844a1aSMatthew Knepley + section - the section describing data layout
3532ea844a1aSMatthew Knepley - field   - the field number
3533ea844a1aSMatthew Knepley 
35342fe279fdSBarry Smith   Output Parameter:
3535ea844a1aSMatthew Knepley . sym - the symmetry describing the affect of orientation on the access of the data
3536ea844a1aSMatthew Knepley 
3537ea844a1aSMatthew Knepley   Level: developer
3538ea844a1aSMatthew Knepley 
3539*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSectionSym`, `PetscSectionSetFieldSym()`, `PetscSectionSymCreate()`
3540ea844a1aSMatthew Knepley @*/
3541d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldSym(PetscSection section, PetscInt field, PetscSectionSym *sym)
3542d71ae5a4SJacob Faibussowitsch {
3543ea844a1aSMatthew Knepley   PetscFunctionBegin;
3544ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
35452abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field, section->numFields);
3546ea844a1aSMatthew Knepley   *sym = section->field[field]->sym;
35473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3548ea844a1aSMatthew Knepley }
3549ea844a1aSMatthew Knepley 
3550ea844a1aSMatthew Knepley /*@C
3551cab54364SBarry Smith   PetscSectionGetPointSyms - Get the symmetries for a set of points in a `PetscSection` under specific orientations.
3552ea844a1aSMatthew Knepley 
355340750d41SVaclav Hapla   Not Collective
3554ea844a1aSMatthew Knepley 
3555ea844a1aSMatthew Knepley   Input Parameters:
3556ea844a1aSMatthew Knepley + section   - the section
3557ea844a1aSMatthew Knepley . numPoints - the number of points
355820662ed9SBarry Smith - points    - an array of size 2 * `numPoints`, containing a list of (point, orientation) pairs. (An orientation is an
355920662ed9SBarry Smith               arbitrary integer: its interpretation is up to sym.  Orientations are used by `DM`: for their interpretation in that
356020662ed9SBarry Smith               context, see `DMPlexGetConeOrientation()`).
3561ea844a1aSMatthew Knepley 
3562d8d19677SJose E. Roman   Output Parameters:
356320662ed9SBarry Smith + perms - The permutations for the given orientations (or `NULL` if there is no symmetry or the permutation is the identity).
356420662ed9SBarry Smith - rots  - The field rotations symmetries for the given orientations (or `NULL` if there is no symmetry or the rotations are all
3565ea844a1aSMatthew Knepley     identity).
3566ea844a1aSMatthew Knepley 
3567ea844a1aSMatthew Knepley   Example of usage, gathering dofs into a local array (lArray) from a section array (sArray):
3568ea844a1aSMatthew Knepley .vb
3569ea844a1aSMatthew Knepley      const PetscInt    **perms;
3570ea844a1aSMatthew Knepley      const PetscScalar **rots;
3571ea844a1aSMatthew Knepley      PetscInt            lOffset;
3572ea844a1aSMatthew Knepley 
3573ea844a1aSMatthew Knepley      PetscSectionGetPointSyms(section,numPoints,points,&perms,&rots);
3574ea844a1aSMatthew Knepley      for (i = 0, lOffset = 0; i < numPoints; i++) {
3575ea844a1aSMatthew Knepley        PetscInt           point = points[2*i], dof, sOffset;
3576ea844a1aSMatthew Knepley        const PetscInt    *perm  = perms ? perms[i] : NULL;
3577ea844a1aSMatthew Knepley        const PetscScalar *rot   = rots  ? rots[i]  : NULL;
3578ea844a1aSMatthew Knepley 
3579ea844a1aSMatthew Knepley        PetscSectionGetDof(section,point,&dof);
3580ea844a1aSMatthew Knepley        PetscSectionGetOffset(section,point,&sOffset);
3581ea844a1aSMatthew Knepley 
3582ea844a1aSMatthew Knepley        if (perm) {for (j = 0; j < dof; j++) {lArray[lOffset + perm[j]]  = sArray[sOffset + j];}}
3583ea844a1aSMatthew Knepley        else      {for (j = 0; j < dof; j++) {lArray[lOffset +      j ]  = sArray[sOffset + j];}}
3584ea844a1aSMatthew Knepley        if (rot)  {for (j = 0; j < dof; j++) {lArray[lOffset +      j ] *= rot[j];             }}
3585ea844a1aSMatthew Knepley        lOffset += dof;
3586ea844a1aSMatthew Knepley      }
3587ea844a1aSMatthew Knepley      PetscSectionRestorePointSyms(section,numPoints,points,&perms,&rots);
3588ea844a1aSMatthew Knepley .ve
3589ea844a1aSMatthew Knepley 
3590ea844a1aSMatthew Knepley   Example of usage, adding dofs into a section array (sArray) from a local array (lArray):
3591ea844a1aSMatthew Knepley .vb
3592ea844a1aSMatthew Knepley      const PetscInt    **perms;
3593ea844a1aSMatthew Knepley      const PetscScalar **rots;
3594ea844a1aSMatthew Knepley      PetscInt            lOffset;
3595ea844a1aSMatthew Knepley 
3596ea844a1aSMatthew Knepley      PetscSectionGetPointSyms(section,numPoints,points,&perms,&rots);
3597ea844a1aSMatthew Knepley      for (i = 0, lOffset = 0; i < numPoints; i++) {
3598ea844a1aSMatthew Knepley        PetscInt           point = points[2*i], dof, sOffset;
3599ea844a1aSMatthew Knepley        const PetscInt    *perm  = perms ? perms[i] : NULL;
3600ea844a1aSMatthew Knepley        const PetscScalar *rot   = rots  ? rots[i]  : NULL;
3601ea844a1aSMatthew Knepley 
3602ea844a1aSMatthew Knepley        PetscSectionGetDof(section,point,&dof);
3603ea844a1aSMatthew Knepley        PetscSectionGetOffset(section,point,&sOff);
3604ea844a1aSMatthew Knepley 
3605ea844a1aSMatthew Knepley        if (perm) {for (j = 0; j < dof; j++) {sArray[sOffset + j] += lArray[lOffset + perm[j]] * (rot ? PetscConj(rot[perm[j]]) : 1.);}}
3606ea844a1aSMatthew Knepley        else      {for (j = 0; j < dof; j++) {sArray[sOffset + j] += lArray[lOffset +      j ] * (rot ? PetscConj(rot[     j ]) : 1.);}}
3607ea844a1aSMatthew Knepley        offset += dof;
3608ea844a1aSMatthew Knepley      }
3609ea844a1aSMatthew Knepley      PetscSectionRestorePointSyms(section,numPoints,points,&perms,&rots);
3610ea844a1aSMatthew Knepley .ve
3611ea844a1aSMatthew Knepley 
3612ea844a1aSMatthew Knepley   Level: developer
3613ea844a1aSMatthew Knepley 
3614583308b6SBarry Smith   Notes:
361520662ed9SBarry Smith   `PetscSectionSetSym()` must have been previously called to provide the symmetries to the `PetscSection`
361620662ed9SBarry Smith 
361720662ed9SBarry Smith   Use `PetscSectionRestorePointSyms()` when finished with the data
361820662ed9SBarry Smith 
3619*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSectionSym`, `PetscSectionRestorePointSyms()`, `PetscSectionSymCreate()`, `PetscSectionSetSym()`, `PetscSectionGetSym()`
3620ea844a1aSMatthew Knepley @*/
3621d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetPointSyms(PetscSection section, PetscInt numPoints, const PetscInt *points, const PetscInt ***perms, const PetscScalar ***rots)
3622d71ae5a4SJacob Faibussowitsch {
3623ea844a1aSMatthew Knepley   PetscSectionSym sym;
3624ea844a1aSMatthew Knepley 
3625ea844a1aSMatthew Knepley   PetscFunctionBegin;
3626ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
36274f572ea9SToby Isaac   if (numPoints) PetscAssertPointer(points, 3);
3628ea844a1aSMatthew Knepley   if (perms) *perms = NULL;
3629ea844a1aSMatthew Knepley   if (rots) *rots = NULL;
3630ea844a1aSMatthew Knepley   sym = section->sym;
3631ea844a1aSMatthew Knepley   if (sym && (perms || rots)) {
3632ea844a1aSMatthew Knepley     SymWorkLink link;
3633ea844a1aSMatthew Knepley 
3634ea844a1aSMatthew Knepley     if (sym->workin) {
3635ea844a1aSMatthew Knepley       link        = sym->workin;
3636ea844a1aSMatthew Knepley       sym->workin = sym->workin->next;
3637ea844a1aSMatthew Knepley     } else {
36384dfa11a4SJacob Faibussowitsch       PetscCall(PetscNew(&link));
3639ea844a1aSMatthew Knepley     }
3640ea844a1aSMatthew Knepley     if (numPoints > link->numPoints) {
36410c99d500SBarry Smith       PetscInt    **perms = (PetscInt **)link->perms;
36420c99d500SBarry Smith       PetscScalar **rots  = (PetscScalar **)link->rots;
36430c99d500SBarry Smith       PetscCall(PetscFree2(perms, rots));
36440c99d500SBarry Smith       PetscCall(PetscMalloc2(numPoints, (PetscInt ***)&link->perms, numPoints, (PetscScalar ***)&link->rots));
3645ea844a1aSMatthew Knepley       link->numPoints = numPoints;
3646ea844a1aSMatthew Knepley     }
3647ea844a1aSMatthew Knepley     link->next   = sym->workout;
3648ea844a1aSMatthew Knepley     sym->workout = link;
36499566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero((PetscInt **)link->perms, numPoints));
36509566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero((PetscInt **)link->rots, numPoints));
36519927e4dfSBarry Smith     PetscUseTypeMethod(sym, getpoints, section, numPoints, points, link->perms, link->rots);
3652ea844a1aSMatthew Knepley     if (perms) *perms = link->perms;
3653ea844a1aSMatthew Knepley     if (rots) *rots = link->rots;
3654ea844a1aSMatthew Knepley   }
36553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3656ea844a1aSMatthew Knepley }
3657ea844a1aSMatthew Knepley 
3658ea844a1aSMatthew Knepley /*@C
3659cab54364SBarry Smith   PetscSectionRestorePointSyms - Restore the symmetries returned by `PetscSectionGetPointSyms()`
3660ea844a1aSMatthew Knepley 
366140750d41SVaclav Hapla   Not Collective
3662ea844a1aSMatthew Knepley 
3663ea844a1aSMatthew Knepley   Input Parameters:
3664ea844a1aSMatthew Knepley + section   - the section
3665ea844a1aSMatthew Knepley . numPoints - the number of points
366638b5cf2dSJacob Faibussowitsch . points    - an array of size 2 * `numPoints`, containing a list of (point, orientation) pairs. (An orientation is an
3667cab54364SBarry Smith               arbitrary integer: its interpretation is up to sym.  Orientations are used by `DM`: for their interpretation in that
3668cab54364SBarry Smith               context, see `DMPlexGetConeOrientation()`).
366920662ed9SBarry Smith . perms     - The permutations for the given orientations: set to `NULL` at conclusion
367020662ed9SBarry Smith - rots      - The field rotations symmetries for the given orientations: set to `NULL` at conclusion
3671ea844a1aSMatthew Knepley 
3672ea844a1aSMatthew Knepley   Level: developer
3673ea844a1aSMatthew Knepley 
3674*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSectionSym`, `PetscSectionGetPointSyms()`, `PetscSectionSymCreate()`, `PetscSectionSetSym()`, `PetscSectionGetSym()`
3675ea844a1aSMatthew Knepley @*/
3676d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionRestorePointSyms(PetscSection section, PetscInt numPoints, const PetscInt *points, const PetscInt ***perms, const PetscScalar ***rots)
3677d71ae5a4SJacob Faibussowitsch {
3678ea844a1aSMatthew Knepley   PetscSectionSym sym;
3679ea844a1aSMatthew Knepley 
3680ea844a1aSMatthew Knepley   PetscFunctionBegin;
3681ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
3682ea844a1aSMatthew Knepley   sym = section->sym;
3683ea844a1aSMatthew Knepley   if (sym && (perms || rots)) {
3684ea844a1aSMatthew Knepley     SymWorkLink *p, link;
3685ea844a1aSMatthew Knepley 
3686ea844a1aSMatthew Knepley     for (p = &sym->workout; (link = *p); p = &link->next) {
3687ea844a1aSMatthew Knepley       if ((perms && link->perms == *perms) || (rots && link->rots == *rots)) {
3688ea844a1aSMatthew Knepley         *p          = link->next;
3689ea844a1aSMatthew Knepley         link->next  = sym->workin;
3690ea844a1aSMatthew Knepley         sym->workin = link;
3691ea844a1aSMatthew Knepley         if (perms) *perms = NULL;
3692ea844a1aSMatthew Knepley         if (rots) *rots = NULL;
36933ba16761SJacob Faibussowitsch         PetscFunctionReturn(PETSC_SUCCESS);
3694ea844a1aSMatthew Knepley       }
3695ea844a1aSMatthew Knepley     }
3696ea844a1aSMatthew Knepley     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Array was not checked out");
3697ea844a1aSMatthew Knepley   }
36983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3699ea844a1aSMatthew Knepley }
3700ea844a1aSMatthew Knepley 
3701ea844a1aSMatthew Knepley /*@C
3702cab54364SBarry Smith   PetscSectionGetFieldPointSyms - Get the symmetries for a set of points in a field of a `PetscSection` under specific orientations.
3703ea844a1aSMatthew Knepley 
370440750d41SVaclav Hapla   Not Collective
3705ea844a1aSMatthew Knepley 
3706ea844a1aSMatthew Knepley   Input Parameters:
3707ea844a1aSMatthew Knepley + section   - the section
3708ea844a1aSMatthew Knepley . field     - the field of the section
3709ea844a1aSMatthew Knepley . numPoints - the number of points
371020662ed9SBarry Smith - points    - an array of size 2 * `numPoints`, containing a list of (point, orientation) pairs. (An orientation is an
3711cab54364SBarry Smith     arbitrary integer: its interpretation is up to sym.  Orientations are used by `DM`: for their interpretation in that
3712cab54364SBarry Smith     context, see `DMPlexGetConeOrientation()`).
3713ea844a1aSMatthew Knepley 
3714d8d19677SJose E. Roman   Output Parameters:
371520662ed9SBarry Smith + perms - The permutations for the given orientations (or `NULL` if there is no symmetry or the permutation is the identity).
371620662ed9SBarry Smith - rots  - The field rotations symmetries for the given orientations (or `NULL` if there is no symmetry or the rotations are all
3717ea844a1aSMatthew Knepley     identity).
3718ea844a1aSMatthew Knepley 
3719ea844a1aSMatthew Knepley   Level: developer
3720ea844a1aSMatthew Knepley 
372120662ed9SBarry Smith   Notes:
372220662ed9SBarry Smith   `PetscSectionSetFieldSym()` must have been previously called to provide the symmetries to the `PetscSection`
372320662ed9SBarry Smith 
372420662ed9SBarry Smith   Use `PetscSectionRestoreFieldPointSyms()` when finished with the data
372520662ed9SBarry Smith 
3726*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSectionSym`, `PetscSectionGetPointSyms()`, `PetscSectionRestoreFieldPointSyms()`
3727ea844a1aSMatthew Knepley @*/
3728d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetFieldPointSyms(PetscSection section, PetscInt field, PetscInt numPoints, const PetscInt *points, const PetscInt ***perms, const PetscScalar ***rots)
3729d71ae5a4SJacob Faibussowitsch {
3730ea844a1aSMatthew Knepley   PetscFunctionBegin;
3731ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
373208401ef6SPierre 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);
37339566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetPointSyms(section->field[field], numPoints, points, perms, rots));
37343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3735ea844a1aSMatthew Knepley }
3736ea844a1aSMatthew Knepley 
3737ea844a1aSMatthew Knepley /*@C
3738cab54364SBarry Smith   PetscSectionRestoreFieldPointSyms - Restore the symmetries returned by `PetscSectionGetFieldPointSyms()`
3739ea844a1aSMatthew Knepley 
374040750d41SVaclav Hapla   Not Collective
3741ea844a1aSMatthew Knepley 
3742ea844a1aSMatthew Knepley   Input Parameters:
3743ea844a1aSMatthew Knepley + section   - the section
3744ea844a1aSMatthew Knepley . field     - the field number
3745ea844a1aSMatthew Knepley . numPoints - the number of points
374638b5cf2dSJacob Faibussowitsch . points    - an array of size 2 * `numPoints`, containing a list of (point, orientation) pairs. (An orientation is an
3747cab54364SBarry Smith     arbitrary integer: its interpretation is up to sym.  Orientations are used by `DM`: for their interpretation in that
3748cab54364SBarry Smith     context, see `DMPlexGetConeOrientation()`).
374920662ed9SBarry Smith . perms     - The permutations for the given orientations: set to NULL at conclusion
3750ea844a1aSMatthew Knepley - rots      - The field rotations symmetries for the given orientations: set to NULL at conclusion
3751ea844a1aSMatthew Knepley 
3752ea844a1aSMatthew Knepley   Level: developer
3753ea844a1aSMatthew Knepley 
3754*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSectionSym`, `PetscSectionRestorePointSyms()`, `petscSectionGetFieldPointSyms()`, `PetscSectionSymCreate()`, `PetscSectionSetSym()`, `PetscSectionGetSym()`
3755ea844a1aSMatthew Knepley @*/
3756d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionRestoreFieldPointSyms(PetscSection section, PetscInt field, PetscInt numPoints, const PetscInt *points, const PetscInt ***perms, const PetscScalar ***rots)
3757d71ae5a4SJacob Faibussowitsch {
3758ea844a1aSMatthew Knepley   PetscFunctionBegin;
3759ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
376008401ef6SPierre 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);
37619566063dSJacob Faibussowitsch   PetscCall(PetscSectionRestorePointSyms(section->field[field], numPoints, points, perms, rots));
37623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3763ea844a1aSMatthew Knepley }
3764ea844a1aSMatthew Knepley 
3765ea844a1aSMatthew Knepley /*@
3766b004864fSMatthew G. Knepley   PetscSectionSymCopy - Copy the symmetries, assuming that the point structure is compatible
3767b004864fSMatthew G. Knepley 
376840750d41SVaclav Hapla   Not Collective
3769b004864fSMatthew G. Knepley 
3770b004864fSMatthew G. Knepley   Input Parameter:
3771cab54364SBarry Smith . sym - the `PetscSectionSym`
3772b004864fSMatthew G. Knepley 
3773b004864fSMatthew G. Knepley   Output Parameter:
3774b004864fSMatthew G. Knepley . nsym - the equivalent symmetries
3775b004864fSMatthew G. Knepley 
3776b004864fSMatthew G. Knepley   Level: developer
3777b004864fSMatthew G. Knepley 
3778*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSectionSym`, `PetscSectionSymCreate()`, `PetscSectionSetSym()`, `PetscSectionGetSym()`, `PetscSectionSymLabelSetStratum()`, `PetscSectionGetPointSyms()`
3779b004864fSMatthew G. Knepley @*/
3780d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymCopy(PetscSectionSym sym, PetscSectionSym nsym)
3781d71ae5a4SJacob Faibussowitsch {
3782b004864fSMatthew G. Knepley   PetscFunctionBegin;
3783b004864fSMatthew G. Knepley   PetscValidHeaderSpecific(sym, PETSC_SECTION_SYM_CLASSID, 1);
3784b004864fSMatthew G. Knepley   PetscValidHeaderSpecific(nsym, PETSC_SECTION_SYM_CLASSID, 2);
3785dbbe0bcdSBarry Smith   PetscTryTypeMethod(sym, copy, nsym);
37863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3787b004864fSMatthew G. Knepley }
3788b004864fSMatthew G. Knepley 
3789b004864fSMatthew G. Knepley /*@
3790cab54364SBarry Smith   PetscSectionSymDistribute - Distribute the symmetries in accordance with the input `PetscSF`
3791b004864fSMatthew G. Knepley 
3792b004864fSMatthew G. Knepley   Collective
3793b004864fSMatthew G. Knepley 
3794b004864fSMatthew G. Knepley   Input Parameters:
3795cab54364SBarry Smith + sym         - the `PetscSectionSym`
3796b004864fSMatthew G. Knepley - migrationSF - the distribution map from roots to leaves
3797b004864fSMatthew G. Knepley 
37982fe279fdSBarry Smith   Output Parameter:
3799b004864fSMatthew G. Knepley . dsym - the redistributed symmetries
3800b004864fSMatthew G. Knepley 
3801b004864fSMatthew G. Knepley   Level: developer
3802b004864fSMatthew G. Knepley 
3803*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSectionSym`, `PetscSectionSymCreate()`, `PetscSectionSetSym()`, `PetscSectionGetSym()`, `PetscSectionSymLabelSetStratum()`, `PetscSectionGetPointSyms()`
3804b004864fSMatthew G. Knepley @*/
3805d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSymDistribute(PetscSectionSym sym, PetscSF migrationSF, PetscSectionSym *dsym)
3806d71ae5a4SJacob Faibussowitsch {
3807b004864fSMatthew G. Knepley   PetscFunctionBegin;
3808b004864fSMatthew G. Knepley   PetscValidHeaderSpecific(sym, PETSC_SECTION_SYM_CLASSID, 1);
3809b004864fSMatthew G. Knepley   PetscValidHeaderSpecific(migrationSF, PETSCSF_CLASSID, 2);
38104f572ea9SToby Isaac   PetscAssertPointer(dsym, 3);
3811dbbe0bcdSBarry Smith   PetscTryTypeMethod(sym, distribute, migrationSF, dsym);
38123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3813b004864fSMatthew G. Knepley }
3814b004864fSMatthew G. Knepley 
3815b004864fSMatthew G. Knepley /*@
381620662ed9SBarry Smith   PetscSectionGetUseFieldOffsets - Get the flag indicating if field offsets are used directly in a global section, rather than just the point offset
3817ea844a1aSMatthew Knepley 
381840750d41SVaclav Hapla   Not Collective
3819ea844a1aSMatthew Knepley 
3820ea844a1aSMatthew Knepley   Input Parameter:
3821cab54364SBarry Smith . s - the global `PetscSection`
3822ea844a1aSMatthew Knepley 
38232fe279fdSBarry Smith   Output Parameter:
3824ea844a1aSMatthew Knepley . flg - the flag
3825ea844a1aSMatthew Knepley 
3826ea844a1aSMatthew Knepley   Level: developer
3827ea844a1aSMatthew Knepley 
3828*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSectionSym`, `PetscSectionSetChart()`, `PetscSectionCreate()`
3829ea844a1aSMatthew Knepley @*/
3830d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionGetUseFieldOffsets(PetscSection s, PetscBool *flg)
3831d71ae5a4SJacob Faibussowitsch {
3832ea844a1aSMatthew Knepley   PetscFunctionBegin;
3833ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
3834ea844a1aSMatthew Knepley   *flg = s->useFieldOff;
38353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3836ea844a1aSMatthew Knepley }
3837ea844a1aSMatthew Knepley 
3838ea844a1aSMatthew Knepley /*@
3839ea844a1aSMatthew Knepley   PetscSectionSetUseFieldOffsets - Set the flag to use field offsets directly in a global section, rather than just the point offset
3840ea844a1aSMatthew Knepley 
384140750d41SVaclav Hapla   Not Collective
3842ea844a1aSMatthew Knepley 
3843ea844a1aSMatthew Knepley   Input Parameters:
3844cab54364SBarry Smith + s   - the global `PetscSection`
3845ea844a1aSMatthew Knepley - flg - the flag
3846ea844a1aSMatthew Knepley 
3847ea844a1aSMatthew Knepley   Level: developer
3848ea844a1aSMatthew Knepley 
3849*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSectionSym`, `PetscSectionGetUseFieldOffsets()`, `PetscSectionSetChart()`, `PetscSectionCreate()`
3850ea844a1aSMatthew Knepley @*/
3851d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionSetUseFieldOffsets(PetscSection s, PetscBool flg)
3852d71ae5a4SJacob Faibussowitsch {
3853ea844a1aSMatthew Knepley   PetscFunctionBegin;
3854ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
3855ea844a1aSMatthew Knepley   s->useFieldOff = flg;
38563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3857ea844a1aSMatthew Knepley }
3858ea844a1aSMatthew Knepley 
3859ea844a1aSMatthew Knepley #define PetscSectionExpandPoints_Loop(TYPE) \
3860a8f51744SPierre Jolivet   do { \
3861ea844a1aSMatthew Knepley     PetscInt i, n, o0, o1, size; \
3862ea844a1aSMatthew Knepley     TYPE    *a0 = (TYPE *)origArray, *a1; \
38639566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(s, &size)); \
38649566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(size, &a1)); \
3865ea844a1aSMatthew Knepley     for (i = 0; i < npoints; i++) { \
38669566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(origSection, points_[i], &o0)); \
38679566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(s, i, &o1)); \
38689566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(s, i, &n)); \
38699566063dSJacob Faibussowitsch       PetscCall(PetscMemcpy(&a1[o1], &a0[o0], n *unitsize)); \
3870ea844a1aSMatthew Knepley     } \
3871ea844a1aSMatthew Knepley     *newArray = (void *)a1; \
3872a8f51744SPierre Jolivet   } while (0)
3873ea844a1aSMatthew Knepley 
3874ea844a1aSMatthew Knepley /*@
3875ea844a1aSMatthew Knepley   PetscSectionExtractDofsFromArray - Extracts elements of an array corresponding to DOFs of specified points.
3876ea844a1aSMatthew Knepley 
387740750d41SVaclav Hapla   Not Collective
3878ea844a1aSMatthew Knepley 
3879ea844a1aSMatthew Knepley   Input Parameters:
3880cab54364SBarry Smith + origSection - the `PetscSection` describing the layout of the array
3881cab54364SBarry Smith . dataType    - `MPI_Datatype` describing the data type of the array (currently only `MPIU_INT`, `MPIU_SCALAR`, `MPIU_REAL`)
388220662ed9SBarry Smith . origArray   - the array; its size must be equal to the storage size of `origSection`
388320662ed9SBarry Smith - points      - `IS` with points to extract; its indices must lie in the chart of `origSection`
3884ea844a1aSMatthew Knepley 
3885ea844a1aSMatthew Knepley   Output Parameters:
3886da81f932SPierre Jolivet + newSection - the new `PetscSection` describing the layout of the new array (with points renumbered 0,1,... but preserving numbers of DOFs)
388720662ed9SBarry Smith - newArray   - the array of the extracted DOFs; its size is the storage size of `newSection`
3888ea844a1aSMatthew Knepley 
3889ea844a1aSMatthew Knepley   Level: developer
3890ea844a1aSMatthew Knepley 
3891*b0c76368SJames Wright .seealso: [PetscSection](ch_petscsection), `PetscSectionSym`, `PetscSectionGetChart()`, `PetscSectionGetDof()`, `PetscSectionGetStorageSize()`, `PetscSectionCreate()`
3892ea844a1aSMatthew Knepley @*/
3893d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSectionExtractDofsFromArray(PetscSection origSection, MPI_Datatype dataType, const void *origArray, IS points, PetscSection *newSection, void *newArray[])
3894d71ae5a4SJacob Faibussowitsch {
3895ea844a1aSMatthew Knepley   PetscSection    s;
3896ea844a1aSMatthew Knepley   const PetscInt *points_;
3897ea844a1aSMatthew Knepley   PetscInt        i, n, npoints, pStart, pEnd;
3898ea844a1aSMatthew Knepley   PetscMPIInt     unitsize;
3899ea844a1aSMatthew Knepley 
3900ea844a1aSMatthew Knepley   PetscFunctionBegin;
3901ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(origSection, PETSC_SECTION_CLASSID, 1);
39024f572ea9SToby Isaac   PetscAssertPointer(origArray, 3);
3903ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(points, IS_CLASSID, 4);
39044f572ea9SToby Isaac   if (newSection) PetscAssertPointer(newSection, 5);
39054f572ea9SToby Isaac   if (newArray) PetscAssertPointer(newArray, 6);
39069566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Type_size(dataType, &unitsize));
39079566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(points, &npoints));
39089566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(points, &points_));
39099566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(origSection, &pStart, &pEnd));
39109566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &s));
39119566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(s, 0, npoints));
3912ea844a1aSMatthew Knepley   for (i = 0; i < npoints; i++) {
3913c9cc58a2SBarry 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);
39149566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(origSection, points_[i], &n));
39159566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(s, i, n));
3916ea844a1aSMatthew Knepley   }
39179566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(s));
3918ea844a1aSMatthew Knepley   if (newArray) {
39199371c9d4SSatish Balay     if (dataType == MPIU_INT) {
39209371c9d4SSatish Balay       PetscSectionExpandPoints_Loop(PetscInt);
39219371c9d4SSatish Balay     } else if (dataType == MPIU_SCALAR) {
39229371c9d4SSatish Balay       PetscSectionExpandPoints_Loop(PetscScalar);
39239371c9d4SSatish Balay     } else if (dataType == MPIU_REAL) {
39249371c9d4SSatish Balay       PetscSectionExpandPoints_Loop(PetscReal);
39259371c9d4SSatish Balay     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "not implemented for this MPI_Datatype");
3926ea844a1aSMatthew Knepley   }
3927ea844a1aSMatthew Knepley   if (newSection) {
3928ea844a1aSMatthew Knepley     *newSection = s;
3929ea844a1aSMatthew Knepley   } else {
39309566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&s));
3931ea844a1aSMatthew Knepley   }
39329566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(points, &points_));
39333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3934ea844a1aSMatthew Knepley }
3935