xref: /petsc/src/vec/is/section/interface/section.c (revision 28b400f66ebc7ae0049166a2294dfcd3df27e64b)
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 /*@
11ea844a1aSMatthew Knepley   PetscSectionCreate - Allocates PetscSection space 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
23ea844a1aSMatthew Knepley $       PetscSectionCreate(MPI_Comm,PetscSection *);
24ea844a1aSMatthew Knepley $       PetscSectionSetNumFields(PetscSection, numFields);
25ea844a1aSMatthew Knepley $       PetscSectionSetChart(PetscSection,low,high);
26ea844a1aSMatthew Knepley $       PetscSectionSetDof(PetscSection,point,numdof);
27ea844a1aSMatthew Knepley $       PetscSectionSetUp(PetscSection);
28ea844a1aSMatthew Knepley $       PetscSectionGetOffset(PetscSection,point,PetscInt *);
29ea844a1aSMatthew Knepley $       PetscSectionDestroy(PetscSection);
30ea844a1aSMatthew Knepley 
31ea844a1aSMatthew Knepley   The PetscSection object and methods are intended to be used in the PETSc Vec and Mat implementions; it is
32ea844a1aSMatthew Knepley   recommended they not be used in user codes unless you really gain something in their use.
33ea844a1aSMatthew Knepley 
34ea844a1aSMatthew Knepley .seealso: PetscSection, PetscSectionDestroy()
35ea844a1aSMatthew Knepley @*/
36ea844a1aSMatthew Knepley PetscErrorCode PetscSectionCreate(MPI_Comm comm, PetscSection *s)
37ea844a1aSMatthew Knepley {
38ea844a1aSMatthew Knepley   PetscFunctionBegin;
39ea844a1aSMatthew Knepley   PetscValidPointer(s,2);
405f80ce2aSJacob Faibussowitsch   CHKERRQ(ISInitializePackage());
41ea844a1aSMatthew Knepley 
425f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHeaderCreate(*s,PETSC_SECTION_CLASSID,"PetscSection","Section","IS",comm,PetscSectionDestroy,PetscSectionView));
43ea844a1aSMatthew Knepley 
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)->maxDof              = 0;
50ea844a1aSMatthew Knepley   (*s)->atlasDof            = NULL;
51ea844a1aSMatthew Knepley   (*s)->atlasOff            = NULL;
52ea844a1aSMatthew Knepley   (*s)->bc                  = NULL;
53ea844a1aSMatthew Knepley   (*s)->bcIndices           = NULL;
54ea844a1aSMatthew Knepley   (*s)->setup               = PETSC_FALSE;
55ea844a1aSMatthew Knepley   (*s)->numFields           = 0;
56ea844a1aSMatthew Knepley   (*s)->fieldNames          = NULL;
57ea844a1aSMatthew Knepley   (*s)->field               = NULL;
58ea844a1aSMatthew Knepley   (*s)->useFieldOff         = PETSC_FALSE;
59b778fa18SValeria Barra   (*s)->compNames           = NULL;
60ea844a1aSMatthew Knepley   (*s)->clObj               = NULL;
61c459fbc1SJed Brown   (*s)->clHash              = NULL;
62ea844a1aSMatthew Knepley   (*s)->clSection           = NULL;
63ea844a1aSMatthew Knepley   (*s)->clPoints            = NULL;
64ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
65ea844a1aSMatthew Knepley }
66ea844a1aSMatthew Knepley 
67ea844a1aSMatthew Knepley /*@
68ea844a1aSMatthew Knepley   PetscSectionCopy - Creates a shallow (if possible) copy of the PetscSection
69ea844a1aSMatthew Knepley 
70ea844a1aSMatthew Knepley   Collective
71ea844a1aSMatthew Knepley 
72ea844a1aSMatthew Knepley   Input Parameter:
73ea844a1aSMatthew Knepley . section - the PetscSection
74ea844a1aSMatthew Knepley 
75ea844a1aSMatthew Knepley   Output Parameter:
76ea844a1aSMatthew Knepley . newSection - the copy
77ea844a1aSMatthew Knepley 
78ea844a1aSMatthew Knepley   Level: intermediate
79ea844a1aSMatthew Knepley 
80ea844a1aSMatthew Knepley .seealso: PetscSection, PetscSectionCreate(), PetscSectionDestroy()
81ea844a1aSMatthew Knepley @*/
82ea844a1aSMatthew Knepley PetscErrorCode PetscSectionCopy(PetscSection section, PetscSection newSection)
83ea844a1aSMatthew Knepley {
84ea844a1aSMatthew Knepley   PetscSectionSym sym;
85ea844a1aSMatthew Knepley   IS              perm;
86b778fa18SValeria Barra   PetscInt        numFields, f, c, pStart, pEnd, p;
87ea844a1aSMatthew Knepley 
88ea844a1aSMatthew Knepley   PetscFunctionBegin;
89ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
90ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(newSection, PETSC_SECTION_CLASSID, 2);
915f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionReset(newSection));
925f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetNumFields(section, &numFields));
935f80ce2aSJacob Faibussowitsch   if (numFields) CHKERRQ(PetscSectionSetNumFields(newSection, numFields));
94ea844a1aSMatthew Knepley   for (f = 0; f < numFields; ++f) {
95b778fa18SValeria Barra     const char *fieldName = NULL, *compName = NULL;
96ea844a1aSMatthew Knepley     PetscInt   numComp = 0;
97ea844a1aSMatthew Knepley 
985f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetFieldName(section, f, &fieldName));
995f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetFieldName(newSection, f, fieldName));
1005f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetFieldComponents(section, f, &numComp));
1015f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetFieldComponents(newSection, f, numComp));
102b778fa18SValeria Barra     for (c = 0; c < numComp; ++c) {
1035f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetComponentName(section, f, c, &compName));
1045f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetComponentName(newSection, f, c, compName));
105b778fa18SValeria Barra     }
1065f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetFieldSym(section, f, &sym));
1075f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetFieldSym(newSection, f, sym));
108ea844a1aSMatthew Knepley   }
1095f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetChart(section, &pStart, &pEnd));
1105f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetChart(newSection, pStart, pEnd));
1115f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetPermutation(section, &perm));
1125f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetPermutation(newSection, perm));
1135f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetSym(section, &sym));
1145f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetSym(newSection, sym));
115ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
116ea844a1aSMatthew Knepley     PetscInt dof, cdof, fcdof = 0;
117ea844a1aSMatthew Knepley 
1185f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(section, p, &dof));
1195f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetDof(newSection, p, dof));
1205f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetConstraintDof(section, p, &cdof));
1215f80ce2aSJacob Faibussowitsch     if (cdof) CHKERRQ(PetscSectionSetConstraintDof(newSection, p, cdof));
122ea844a1aSMatthew Knepley     for (f = 0; f < numFields; ++f) {
1235f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetFieldDof(section, p, f, &dof));
1245f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetFieldDof(newSection, p, f, dof));
125ea844a1aSMatthew Knepley       if (cdof) {
1265f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
1275f80ce2aSJacob Faibussowitsch         if (fcdof) CHKERRQ(PetscSectionSetFieldConstraintDof(newSection, p, f, fcdof));
128ea844a1aSMatthew Knepley       }
129ea844a1aSMatthew Knepley     }
130ea844a1aSMatthew Knepley   }
1315f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetUp(newSection));
132ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
133ea844a1aSMatthew Knepley     PetscInt       off, cdof, fcdof = 0;
134ea844a1aSMatthew Knepley     const PetscInt *cInd;
135ea844a1aSMatthew Knepley 
136ea844a1aSMatthew Knepley     /* Must set offsets in case they do not agree with the prefix sums */
1375f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetOffset(section, p, &off));
1385f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetOffset(newSection, p, off));
1395f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetConstraintDof(section, p, &cdof));
140ea844a1aSMatthew Knepley     if (cdof) {
1415f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetConstraintIndices(section, p, &cInd));
1425f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetConstraintIndices(newSection, p, cInd));
143ea844a1aSMatthew Knepley       for (f = 0; f < numFields; ++f) {
1445f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
145ea844a1aSMatthew Knepley         if (fcdof) {
1465f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscSectionGetFieldConstraintIndices(section, p, f, &cInd));
1475f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscSectionSetFieldConstraintIndices(newSection, p, f, cInd));
148ea844a1aSMatthew Knepley         }
149ea844a1aSMatthew Knepley       }
150ea844a1aSMatthew Knepley     }
151ea844a1aSMatthew Knepley   }
152ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
153ea844a1aSMatthew Knepley }
154ea844a1aSMatthew Knepley 
155ea844a1aSMatthew Knepley /*@
156ea844a1aSMatthew Knepley   PetscSectionClone - Creates a shallow (if possible) copy of the PetscSection
157ea844a1aSMatthew Knepley 
158ea844a1aSMatthew Knepley   Collective
159ea844a1aSMatthew Knepley 
160ea844a1aSMatthew Knepley   Input Parameter:
161ea844a1aSMatthew Knepley . section - the PetscSection
162ea844a1aSMatthew Knepley 
163ea844a1aSMatthew Knepley   Output Parameter:
164ea844a1aSMatthew Knepley . newSection - the copy
165ea844a1aSMatthew Knepley 
166ea844a1aSMatthew Knepley   Level: beginner
167ea844a1aSMatthew Knepley 
168ea844a1aSMatthew Knepley .seealso: PetscSection, PetscSectionCreate(), PetscSectionDestroy()
169ea844a1aSMatthew Knepley @*/
170ea844a1aSMatthew Knepley PetscErrorCode PetscSectionClone(PetscSection section, PetscSection *newSection)
171ea844a1aSMatthew Knepley {
172ea844a1aSMatthew Knepley   PetscFunctionBegin;
173ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
174ea844a1aSMatthew Knepley   PetscValidPointer(newSection, 2);
1755f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionCreate(PetscObjectComm((PetscObject) section), newSection));
1765f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionCopy(section, *newSection));
177ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
178ea844a1aSMatthew Knepley }
179ea844a1aSMatthew Knepley 
180ea844a1aSMatthew Knepley /*@
181ea844a1aSMatthew Knepley   PetscSectionSetFromOptions - sets parameters in a PetscSection from the options database
182ea844a1aSMatthew Knepley 
183ea844a1aSMatthew Knepley   Collective on PetscSection
184ea844a1aSMatthew Knepley 
185ea844a1aSMatthew Knepley   Input Parameter:
186ea844a1aSMatthew Knepley . section - the PetscSection
187ea844a1aSMatthew Knepley 
188ea844a1aSMatthew Knepley   Options Database:
189147403d9SBarry Smith . -petscsection_point_major - PETSC_TRUE for point-major order
190ea844a1aSMatthew Knepley 
191ea844a1aSMatthew Knepley   Level: intermediate
192ea844a1aSMatthew Knepley 
193ea844a1aSMatthew Knepley .seealso: PetscSection, PetscSectionCreate(), PetscSectionDestroy()
194ea844a1aSMatthew Knepley @*/
195ea844a1aSMatthew Knepley PetscErrorCode PetscSectionSetFromOptions(PetscSection s)
196ea844a1aSMatthew Knepley {
197ea844a1aSMatthew Knepley   PetscErrorCode ierr;
198ea844a1aSMatthew Knepley 
199ea844a1aSMatthew Knepley   PetscFunctionBegin;
200ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
201ea844a1aSMatthew Knepley   ierr = PetscObjectOptionsBegin((PetscObject) s);CHKERRQ(ierr);
2025f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsBool("-petscsection_point_major", "The for ordering, either point major or field major", "PetscSectionSetPointMajor", s->pointMajor, &s->pointMajor, NULL));
203ea844a1aSMatthew Knepley   /* process any options handlers added with PetscObjectAddOptionsHandler() */
2045f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) s));
205ea844a1aSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
2065f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectViewFromOptions((PetscObject) s, NULL, "-petscsection_view"));
207ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
208ea844a1aSMatthew Knepley }
209ea844a1aSMatthew Knepley 
210ea844a1aSMatthew Knepley /*@
211ea844a1aSMatthew Knepley   PetscSectionCompare - Compares two sections
212ea844a1aSMatthew Knepley 
213ea844a1aSMatthew Knepley   Collective on PetscSection
214ea844a1aSMatthew Knepley 
2157a7aea1fSJed Brown   Input Parameters:
216ea844a1aSMatthew Knepley + s1 - the first PetscSection
217ea844a1aSMatthew Knepley - s2 - the second PetscSection
218ea844a1aSMatthew Knepley 
219ea844a1aSMatthew Knepley   Output Parameter:
220ea844a1aSMatthew Knepley . congruent - PETSC_TRUE if the two sections are congruent, PETSC_FALSE otherwise
221ea844a1aSMatthew Knepley 
222ea844a1aSMatthew Knepley   Level: intermediate
223ea844a1aSMatthew Knepley 
224ea844a1aSMatthew Knepley   Notes:
225ea844a1aSMatthew Knepley   Field names are disregarded.
226ea844a1aSMatthew Knepley 
227ea844a1aSMatthew Knepley .seealso: PetscSection, PetscSectionCreate(), PetscSectionCopy(), PetscSectionClone()
228ea844a1aSMatthew Knepley @*/
229ea844a1aSMatthew Knepley PetscErrorCode PetscSectionCompare(PetscSection s1, PetscSection s2, PetscBool *congruent)
230ea844a1aSMatthew Knepley {
231ea844a1aSMatthew Knepley   PetscInt pStart, pEnd, nfields, ncdof, nfcdof, p, f, n1, n2;
232ea844a1aSMatthew Knepley   const PetscInt *idx1, *idx2;
233ea844a1aSMatthew Knepley   IS perm1, perm2;
234ea844a1aSMatthew Knepley   PetscBool flg;
235ea844a1aSMatthew Knepley   PetscMPIInt mflg;
236ea844a1aSMatthew Knepley 
237ea844a1aSMatthew Knepley   PetscFunctionBegin;
238ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s1, PETSC_SECTION_CLASSID, 1);
239ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s2, PETSC_SECTION_CLASSID, 2);
240064a246eSJacob Faibussowitsch   PetscValidBoolPointer(congruent,3);
241ea844a1aSMatthew Knepley   flg = PETSC_FALSE;
242ea844a1aSMatthew Knepley 
2435f80ce2aSJacob Faibussowitsch   CHKERRMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)s1),PetscObjectComm((PetscObject)s2),&mflg));
244ea844a1aSMatthew Knepley   if (mflg != MPI_CONGRUENT && mflg != MPI_IDENT) {
245ea844a1aSMatthew Knepley     *congruent = PETSC_FALSE;
246ea844a1aSMatthew Knepley     PetscFunctionReturn(0);
247ea844a1aSMatthew Knepley   }
248ea844a1aSMatthew Knepley 
2495f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetChart(s1, &pStart, &pEnd));
2505f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetChart(s2, &n1, &n2));
251ea844a1aSMatthew Knepley   if (pStart != n1 || pEnd != n2) goto not_congruent;
252ea844a1aSMatthew Knepley 
2535f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetPermutation(s1, &perm1));
2545f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetPermutation(s2, &perm2));
255ea844a1aSMatthew Knepley   if (perm1 && perm2) {
2565f80ce2aSJacob Faibussowitsch     CHKERRQ(ISEqual(perm1, perm2, congruent));
257ea844a1aSMatthew Knepley     if (!(*congruent)) goto not_congruent;
258ea844a1aSMatthew Knepley   } else if (perm1 != perm2) goto not_congruent;
259ea844a1aSMatthew Knepley 
260ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
2615f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetOffset(s1, p, &n1));
2625f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetOffset(s2, p, &n2));
263ea844a1aSMatthew Knepley     if (n1 != n2) goto not_congruent;
264ea844a1aSMatthew Knepley 
2655f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(s1, p, &n1));
2665f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(s2, p, &n2));
267ea844a1aSMatthew Knepley     if (n1 != n2) goto not_congruent;
268ea844a1aSMatthew Knepley 
2695f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetConstraintDof(s1, p, &ncdof));
2705f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetConstraintDof(s2, p, &n2));
271ea844a1aSMatthew Knepley     if (ncdof != n2) goto not_congruent;
272ea844a1aSMatthew Knepley 
2735f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetConstraintIndices(s1, p, &idx1));
2745f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetConstraintIndices(s2, p, &idx2));
2755f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscArraycmp(idx1, idx2, ncdof, congruent));
276ea844a1aSMatthew Knepley     if (!(*congruent)) goto not_congruent;
277ea844a1aSMatthew Knepley   }
278ea844a1aSMatthew Knepley 
2795f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetNumFields(s1, &nfields));
2805f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetNumFields(s2, &n2));
281ea844a1aSMatthew Knepley   if (nfields != n2) goto not_congruent;
282ea844a1aSMatthew Knepley 
283ea844a1aSMatthew Knepley   for (f = 0; f < nfields; ++f) {
2845f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetFieldComponents(s1, f, &n1));
2855f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetFieldComponents(s2, f, &n2));
286ea844a1aSMatthew Knepley     if (n1 != n2) goto not_congruent;
287ea844a1aSMatthew Knepley 
288ea844a1aSMatthew Knepley     for (p = pStart; p < pEnd; ++p) {
2895f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetFieldOffset(s1, p, f, &n1));
2905f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetFieldOffset(s2, p, f, &n2));
291ea844a1aSMatthew Knepley       if (n1 != n2) goto not_congruent;
292ea844a1aSMatthew Knepley 
2935f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetFieldDof(s1, p, f, &n1));
2945f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetFieldDof(s2, p, f, &n2));
295ea844a1aSMatthew Knepley       if (n1 != n2) goto not_congruent;
296ea844a1aSMatthew Knepley 
2975f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetFieldConstraintDof(s1, p, f, &nfcdof));
2985f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetFieldConstraintDof(s2, p, f, &n2));
299ea844a1aSMatthew Knepley       if (nfcdof != n2) goto not_congruent;
300ea844a1aSMatthew Knepley 
3015f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetFieldConstraintIndices(s1, p, f, &idx1));
3025f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetFieldConstraintIndices(s2, p, f, &idx2));
3035f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscArraycmp(idx1, idx2, nfcdof, congruent));
304ea844a1aSMatthew Knepley       if (!(*congruent)) goto not_congruent;
305ea844a1aSMatthew Knepley     }
306ea844a1aSMatthew Knepley   }
307ea844a1aSMatthew Knepley 
308ea844a1aSMatthew Knepley   flg = PETSC_TRUE;
309ea844a1aSMatthew Knepley not_congruent:
3105f80ce2aSJacob Faibussowitsch   CHKERRMPI(MPIU_Allreduce(&flg,congruent,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)s1)));
311ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
312ea844a1aSMatthew Knepley }
313ea844a1aSMatthew Knepley 
314ea844a1aSMatthew Knepley /*@
315ea844a1aSMatthew Knepley   PetscSectionGetNumFields - Returns the number of fields, or 0 if no fields were defined.
316ea844a1aSMatthew Knepley 
317ea844a1aSMatthew Knepley   Not collective
318ea844a1aSMatthew Knepley 
319ea844a1aSMatthew Knepley   Input Parameter:
320ea844a1aSMatthew Knepley . s - the PetscSection
321ea844a1aSMatthew Knepley 
322ea844a1aSMatthew Knepley   Output Parameter:
323ea844a1aSMatthew Knepley . numFields - the number of fields defined, or 0 if none were defined
324ea844a1aSMatthew Knepley 
325ea844a1aSMatthew Knepley   Level: intermediate
326ea844a1aSMatthew Knepley 
327ea844a1aSMatthew Knepley .seealso: PetscSectionSetNumFields()
328ea844a1aSMatthew Knepley @*/
329ea844a1aSMatthew Knepley PetscErrorCode PetscSectionGetNumFields(PetscSection s, PetscInt *numFields)
330ea844a1aSMatthew Knepley {
331ea844a1aSMatthew Knepley   PetscFunctionBegin;
332ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
333ea844a1aSMatthew Knepley   PetscValidPointer(numFields,2);
334ea844a1aSMatthew Knepley   *numFields = s->numFields;
335ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
336ea844a1aSMatthew Knepley }
337ea844a1aSMatthew Knepley 
338ea844a1aSMatthew Knepley /*@
339ea844a1aSMatthew Knepley   PetscSectionSetNumFields - Sets the number of fields.
340ea844a1aSMatthew Knepley 
341ea844a1aSMatthew Knepley   Not collective
342ea844a1aSMatthew Knepley 
343ea844a1aSMatthew Knepley   Input Parameters:
344ea844a1aSMatthew Knepley + s - the PetscSection
345ea844a1aSMatthew Knepley - numFields - the number of fields
346ea844a1aSMatthew Knepley 
347ea844a1aSMatthew Knepley   Level: intermediate
348ea844a1aSMatthew Knepley 
349ea844a1aSMatthew Knepley .seealso: PetscSectionGetNumFields()
350ea844a1aSMatthew Knepley @*/
351ea844a1aSMatthew Knepley PetscErrorCode PetscSectionSetNumFields(PetscSection s, PetscInt numFields)
352ea844a1aSMatthew Knepley {
353ea844a1aSMatthew Knepley   PetscInt       f;
354ea844a1aSMatthew Knepley 
355ea844a1aSMatthew Knepley   PetscFunctionBegin;
356ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
3572c71b3e2SJacob Faibussowitsch   PetscCheckFalse(numFields <= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "The number of fields %" PetscInt_FMT " must be positive", numFields);
3585f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionReset(s));
359ea844a1aSMatthew Knepley 
360ea844a1aSMatthew Knepley   s->numFields = numFields;
3615f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(s->numFields, &s->numFieldComponents));
3625f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(s->numFields, &s->fieldNames));
3635f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(s->numFields, &s->compNames));
3645f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(s->numFields, &s->field));
365ea844a1aSMatthew Knepley   for (f = 0; f < s->numFields; ++f) {
366ea844a1aSMatthew Knepley     char name[64];
367ea844a1aSMatthew Knepley 
368ea844a1aSMatthew Knepley     s->numFieldComponents[f] = 1;
369ea844a1aSMatthew Knepley 
3705f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionCreate(PetscObjectComm((PetscObject) s), &s->field[f]));
3715f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSNPrintf(name, 64, "Field_%" PetscInt_FMT, f));
3725f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrallocpy(name, (char **) &s->fieldNames[f]));
3735f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSNPrintf(name, 64, "Component_0"));
3745f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(s->numFieldComponents[f], &s->compNames[f]));
3755f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrallocpy(name, (char **) &s->compNames[f][0]));
376ea844a1aSMatthew Knepley   }
377ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
378ea844a1aSMatthew Knepley }
379ea844a1aSMatthew Knepley 
380ea844a1aSMatthew Knepley /*@C
381ea844a1aSMatthew Knepley   PetscSectionGetFieldName - Returns the name of a field in the PetscSection
382ea844a1aSMatthew Knepley 
383ea844a1aSMatthew Knepley   Not Collective
384ea844a1aSMatthew Knepley 
385ea844a1aSMatthew Knepley   Input Parameters:
386ea844a1aSMatthew Knepley + s     - the PetscSection
387ea844a1aSMatthew Knepley - field - the field number
388ea844a1aSMatthew Knepley 
389ea844a1aSMatthew Knepley   Output Parameter:
390ea844a1aSMatthew Knepley . fieldName - the field name
391ea844a1aSMatthew Knepley 
392ea844a1aSMatthew Knepley   Level: intermediate
393ea844a1aSMatthew Knepley 
394ea844a1aSMatthew Knepley .seealso: PetscSectionSetFieldName()
395ea844a1aSMatthew Knepley @*/
396ea844a1aSMatthew Knepley PetscErrorCode PetscSectionGetFieldName(PetscSection s, PetscInt field, const char *fieldName[])
397ea844a1aSMatthew Knepley {
398ea844a1aSMatthew Knepley   PetscFunctionBegin;
399ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
400ea844a1aSMatthew Knepley   PetscValidPointer(fieldName, 3);
4012abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field,s->numFields);
402ea844a1aSMatthew Knepley   *fieldName = s->fieldNames[field];
403ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
404ea844a1aSMatthew Knepley }
405ea844a1aSMatthew Knepley 
406ea844a1aSMatthew Knepley /*@C
407ea844a1aSMatthew Knepley   PetscSectionSetFieldName - Sets the name of a field in the PetscSection
408ea844a1aSMatthew Knepley 
409ea844a1aSMatthew Knepley   Not Collective
410ea844a1aSMatthew Knepley 
411ea844a1aSMatthew Knepley   Input Parameters:
412ea844a1aSMatthew Knepley + s     - the PetscSection
413ea844a1aSMatthew Knepley . field - the field number
414ea844a1aSMatthew Knepley - fieldName - the field name
415ea844a1aSMatthew Knepley 
416ea844a1aSMatthew Knepley   Level: intermediate
417ea844a1aSMatthew Knepley 
418ea844a1aSMatthew Knepley .seealso: PetscSectionGetFieldName()
419ea844a1aSMatthew Knepley @*/
420ea844a1aSMatthew Knepley PetscErrorCode PetscSectionSetFieldName(PetscSection s, PetscInt field, const char fieldName[])
421ea844a1aSMatthew Knepley {
422ea844a1aSMatthew Knepley   PetscFunctionBegin;
423ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
424ea844a1aSMatthew Knepley   if (fieldName) PetscValidCharPointer(fieldName, 3);
4252abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field,s->numFields);
4265f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(s->fieldNames[field]));
4275f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscStrallocpy(fieldName, (char**) &s->fieldNames[field]));
428ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
429ea844a1aSMatthew Knepley }
430ea844a1aSMatthew Knepley 
431b778fa18SValeria Barra /*@C
432b778fa18SValeria Barra   PetscSectionGetComponentName - Gets the name of a field component in the PetscSection
433b778fa18SValeria Barra 
434b778fa18SValeria Barra   Not Collective
435b778fa18SValeria Barra 
436b778fa18SValeria Barra   Input Parameters:
437b778fa18SValeria Barra + s     - the PetscSection
438b778fa18SValeria Barra . field - the field number
439b778fa18SValeria Barra . comp  - the component number
440b778fa18SValeria Barra - compName - the component name
441b778fa18SValeria Barra 
442b778fa18SValeria Barra   Level: intermediate
443b778fa18SValeria Barra 
444b778fa18SValeria Barra .seealso: PetscSectionSetComponentName()
445b778fa18SValeria Barra @*/
446b778fa18SValeria Barra PetscErrorCode PetscSectionGetComponentName(PetscSection s, PetscInt field, PetscInt comp, const char *compName[])
447b778fa18SValeria Barra {
448b778fa18SValeria Barra   PetscFunctionBegin;
449b778fa18SValeria Barra   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
450064a246eSJacob Faibussowitsch   PetscValidPointer(compName, 4);
4512abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field,s->numFields);
4522abc8c78SJacob Faibussowitsch   PetscSectionCheckValidFieldComponent(comp,s->numFieldComponents[field]);
453b778fa18SValeria Barra   *compName = s->compNames[field][comp];
454b778fa18SValeria Barra   PetscFunctionReturn(0);
455b778fa18SValeria Barra }
456b778fa18SValeria Barra 
457b778fa18SValeria Barra /*@C
458b778fa18SValeria Barra   PetscSectionSetComponentName - Sets the name of a field component in the PetscSection
459b778fa18SValeria Barra 
460b778fa18SValeria Barra   Not Collective
461b778fa18SValeria Barra 
462b778fa18SValeria Barra   Input Parameters:
463b778fa18SValeria Barra + s     - the PetscSection
464b778fa18SValeria Barra . field - the field number
465b778fa18SValeria Barra . comp  - the component number
466b778fa18SValeria Barra - compName - the component name
467b778fa18SValeria Barra 
468b778fa18SValeria Barra   Level: intermediate
469b778fa18SValeria Barra 
470b778fa18SValeria Barra .seealso: PetscSectionGetComponentName()
471b778fa18SValeria Barra @*/
472b778fa18SValeria Barra PetscErrorCode PetscSectionSetComponentName(PetscSection s, PetscInt field, PetscInt comp, const char compName[])
473b778fa18SValeria Barra {
474b778fa18SValeria Barra   PetscFunctionBegin;
475b778fa18SValeria Barra   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
476064a246eSJacob Faibussowitsch   if (compName) PetscValidCharPointer(compName, 4);
4772abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field,s->numFields);
4782abc8c78SJacob Faibussowitsch   PetscSectionCheckValidFieldComponent(comp,s->numFieldComponents[field]);
4795f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(s->compNames[field][comp]));
4805f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscStrallocpy(compName, (char**) &s->compNames[field][comp]));
481b778fa18SValeria Barra   PetscFunctionReturn(0);
482b778fa18SValeria Barra }
483b778fa18SValeria Barra 
484ea844a1aSMatthew Knepley /*@
485ea844a1aSMatthew Knepley   PetscSectionGetFieldComponents - Returns the number of field components for the given field.
486ea844a1aSMatthew Knepley 
487ea844a1aSMatthew Knepley   Not collective
488ea844a1aSMatthew Knepley 
489ea844a1aSMatthew Knepley   Input Parameters:
490ea844a1aSMatthew Knepley + s - the PetscSection
491ea844a1aSMatthew Knepley - field - the field number
492ea844a1aSMatthew Knepley 
493ea844a1aSMatthew Knepley   Output Parameter:
494ea844a1aSMatthew Knepley . numComp - the number of field components
495ea844a1aSMatthew Knepley 
496ea844a1aSMatthew Knepley   Level: intermediate
497ea844a1aSMatthew Knepley 
498ea844a1aSMatthew Knepley .seealso: PetscSectionSetFieldComponents(), PetscSectionGetNumFields()
499ea844a1aSMatthew Knepley @*/
500ea844a1aSMatthew Knepley PetscErrorCode PetscSectionGetFieldComponents(PetscSection s, PetscInt field, PetscInt *numComp)
501ea844a1aSMatthew Knepley {
502ea844a1aSMatthew Knepley   PetscFunctionBegin;
503ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
5042abc8c78SJacob Faibussowitsch   PetscValidIntPointer(numComp, 3);
5052abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field,s->numFields);
506ea844a1aSMatthew Knepley   *numComp = s->numFieldComponents[field];
507ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
508ea844a1aSMatthew Knepley }
509ea844a1aSMatthew Knepley 
510ea844a1aSMatthew Knepley /*@
511ea844a1aSMatthew Knepley   PetscSectionSetFieldComponents - Sets the number of field components for the given field.
512ea844a1aSMatthew Knepley 
513ea844a1aSMatthew Knepley   Not collective
514ea844a1aSMatthew Knepley 
515ea844a1aSMatthew Knepley   Input Parameters:
516ea844a1aSMatthew Knepley + s - the PetscSection
517ea844a1aSMatthew Knepley . field - the field number
518ea844a1aSMatthew Knepley - numComp - the number of field components
519ea844a1aSMatthew Knepley 
520ea844a1aSMatthew Knepley   Level: intermediate
521ea844a1aSMatthew Knepley 
522245d9833Sprj- .seealso: PetscSectionGetFieldComponents(), PetscSectionGetNumFields()
523ea844a1aSMatthew Knepley @*/
524ea844a1aSMatthew Knepley PetscErrorCode PetscSectionSetFieldComponents(PetscSection s, PetscInt field, PetscInt numComp)
525ea844a1aSMatthew Knepley {
526b778fa18SValeria Barra   PetscInt       c;
527b778fa18SValeria Barra 
528ea844a1aSMatthew Knepley   PetscFunctionBegin;
529ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
5302abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field,s->numFields);
531b778fa18SValeria Barra   if (s->compNames) {
532b778fa18SValeria Barra     for (c = 0; c < s->numFieldComponents[field]; ++c) {
5335f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree(s->compNames[field][c]));
534b778fa18SValeria Barra     }
5355f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(s->compNames[field]));
536b778fa18SValeria Barra   }
537b778fa18SValeria Barra 
538ea844a1aSMatthew Knepley   s->numFieldComponents[field] = numComp;
539b778fa18SValeria Barra   if (numComp) {
5405f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(numComp, (char ***) &s->compNames[field]));
541b778fa18SValeria Barra     for (c = 0; c < numComp; ++c) {
5422abc8c78SJacob Faibussowitsch       char name[64];
5432abc8c78SJacob Faibussowitsch 
5445f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSNPrintf(name, 64, "%" PetscInt_FMT, c));
5455f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscStrallocpy(name, (char **) &s->compNames[field][c]));
546b778fa18SValeria Barra     }
547b778fa18SValeria Barra   }
548ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
549ea844a1aSMatthew Knepley }
550ea844a1aSMatthew Knepley 
551ea844a1aSMatthew Knepley static PetscErrorCode PetscSectionCheckConstraints_Static(PetscSection s)
552ea844a1aSMatthew Knepley {
553ea844a1aSMatthew Knepley   PetscFunctionBegin;
554ea844a1aSMatthew Knepley   if (!s->bc) {
5555f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionCreate(PETSC_COMM_SELF, &s->bc));
5565f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetChart(s->bc, s->pStart, s->pEnd));
557ea844a1aSMatthew Knepley   }
558ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
559ea844a1aSMatthew Knepley }
560ea844a1aSMatthew Knepley 
561ea844a1aSMatthew Knepley /*@
562ea844a1aSMatthew Knepley   PetscSectionGetChart - Returns the range [pStart, pEnd) in which points lie.
563ea844a1aSMatthew Knepley 
564ea844a1aSMatthew Knepley   Not collective
565ea844a1aSMatthew Knepley 
566ea844a1aSMatthew Knepley   Input Parameter:
567ea844a1aSMatthew Knepley . s - the PetscSection
568ea844a1aSMatthew Knepley 
569ea844a1aSMatthew Knepley   Output Parameters:
570ea844a1aSMatthew Knepley + pStart - the first point
571ea844a1aSMatthew Knepley - pEnd - one past the last point
572ea844a1aSMatthew Knepley 
573ea844a1aSMatthew Knepley   Level: intermediate
574ea844a1aSMatthew Knepley 
575ea844a1aSMatthew Knepley .seealso: PetscSectionSetChart(), PetscSectionCreate()
576ea844a1aSMatthew Knepley @*/
577ea844a1aSMatthew Knepley PetscErrorCode PetscSectionGetChart(PetscSection s, PetscInt *pStart, PetscInt *pEnd)
578ea844a1aSMatthew Knepley {
579ea844a1aSMatthew Knepley   PetscFunctionBegin;
580ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
581ea844a1aSMatthew Knepley   if (pStart) *pStart = s->pStart;
582ea844a1aSMatthew Knepley   if (pEnd)   *pEnd   = s->pEnd;
583ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
584ea844a1aSMatthew Knepley }
585ea844a1aSMatthew Knepley 
586ea844a1aSMatthew Knepley /*@
587ea844a1aSMatthew Knepley   PetscSectionSetChart - Sets the range [pStart, pEnd) in which points lie.
588ea844a1aSMatthew Knepley 
589ea844a1aSMatthew Knepley   Not collective
590ea844a1aSMatthew Knepley 
591ea844a1aSMatthew Knepley   Input Parameters:
592ea844a1aSMatthew Knepley + s - the PetscSection
593ea844a1aSMatthew Knepley . pStart - the first point
594ea844a1aSMatthew Knepley - pEnd - one past the last point
595ea844a1aSMatthew Knepley 
596ea844a1aSMatthew Knepley   Level: intermediate
597ea844a1aSMatthew Knepley 
598ea844a1aSMatthew Knepley .seealso: PetscSectionGetChart(), PetscSectionCreate()
599ea844a1aSMatthew Knepley @*/
600ea844a1aSMatthew Knepley PetscErrorCode PetscSectionSetChart(PetscSection s, PetscInt pStart, PetscInt pEnd)
601ea844a1aSMatthew Knepley {
602ea844a1aSMatthew Knepley   PetscInt       f;
603ea844a1aSMatthew Knepley 
604ea844a1aSMatthew Knepley   PetscFunctionBegin;
605ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
6069318fe57SMatthew G. Knepley   if (pStart == s->pStart && pEnd == s->pEnd) PetscFunctionReturn(0);
607ea844a1aSMatthew Knepley   /* Cannot Reset() because it destroys field information */
608ea844a1aSMatthew Knepley   s->setup = PETSC_FALSE;
6095f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionDestroy(&s->bc));
6105f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(s->bcIndices));
6115f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree2(s->atlasDof, s->atlasOff));
612ea844a1aSMatthew Knepley 
613ea844a1aSMatthew Knepley   s->pStart = pStart;
614ea844a1aSMatthew Knepley   s->pEnd   = pEnd;
6155f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc2((pEnd - pStart), &s->atlasDof, (pEnd - pStart), &s->atlasOff));
6165f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscArrayzero(s->atlasDof, pEnd - pStart));
617ea844a1aSMatthew Knepley   for (f = 0; f < s->numFields; ++f) {
6185f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetChart(s->field[f], pStart, pEnd));
619ea844a1aSMatthew Knepley   }
620ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
621ea844a1aSMatthew Knepley }
622ea844a1aSMatthew Knepley 
623ea844a1aSMatthew Knepley /*@
624ea844a1aSMatthew Knepley   PetscSectionGetPermutation - Returns the permutation of [0, pEnd-pStart) or NULL
625ea844a1aSMatthew Knepley 
626ea844a1aSMatthew Knepley   Not collective
627ea844a1aSMatthew Knepley 
628ea844a1aSMatthew Knepley   Input Parameter:
629ea844a1aSMatthew Knepley . s - the PetscSection
630ea844a1aSMatthew Knepley 
631ea844a1aSMatthew Knepley   Output Parameters:
632ea844a1aSMatthew Knepley . perm - The permutation as an IS
633ea844a1aSMatthew Knepley 
634ea844a1aSMatthew Knepley   Level: intermediate
635ea844a1aSMatthew Knepley 
636ea844a1aSMatthew Knepley .seealso: PetscSectionSetPermutation(), PetscSectionCreate()
637ea844a1aSMatthew Knepley @*/
638ea844a1aSMatthew Knepley PetscErrorCode PetscSectionGetPermutation(PetscSection s, IS *perm)
639ea844a1aSMatthew Knepley {
640ea844a1aSMatthew Knepley   PetscFunctionBegin;
641ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
642ea844a1aSMatthew Knepley   if (perm) {PetscValidPointer(perm, 2); *perm = s->perm;}
643ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
644ea844a1aSMatthew Knepley }
645ea844a1aSMatthew Knepley 
646ea844a1aSMatthew Knepley /*@
647ea844a1aSMatthew Knepley   PetscSectionSetPermutation - Sets the permutation for [0, pEnd-pStart)
648ea844a1aSMatthew Knepley 
649ea844a1aSMatthew Knepley   Not collective
650ea844a1aSMatthew Knepley 
651ea844a1aSMatthew Knepley   Input Parameters:
652ea844a1aSMatthew Knepley + s - the PetscSection
653ea844a1aSMatthew Knepley - perm - the permutation of points
654ea844a1aSMatthew Knepley 
655ea844a1aSMatthew Knepley   Level: intermediate
656ea844a1aSMatthew Knepley 
657ea844a1aSMatthew Knepley .seealso: PetscSectionGetPermutation(), PetscSectionCreate()
658ea844a1aSMatthew Knepley @*/
659ea844a1aSMatthew Knepley PetscErrorCode PetscSectionSetPermutation(PetscSection s, IS perm)
660ea844a1aSMatthew Knepley {
661ea844a1aSMatthew Knepley   PetscFunctionBegin;
662ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
663ea844a1aSMatthew Knepley   if (perm) PetscValidHeaderSpecific(perm, IS_CLASSID, 2);
664*28b400f6SJacob Faibussowitsch   PetscCheck(!s->setup,PetscObjectComm((PetscObject) s), PETSC_ERR_ARG_WRONGSTATE, "Cannot set a permutation after the section is setup");
665ea844a1aSMatthew Knepley   if (s->perm != perm) {
6665f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&s->perm));
667ea844a1aSMatthew Knepley     if (perm) {
668ea844a1aSMatthew Knepley       s->perm = perm;
6695f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscObjectReference((PetscObject) s->perm));
670ea844a1aSMatthew Knepley     }
671ea844a1aSMatthew Knepley   }
672ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
673ea844a1aSMatthew Knepley }
674ea844a1aSMatthew Knepley 
675ea844a1aSMatthew Knepley /*@
676ea844a1aSMatthew Knepley   PetscSectionGetPointMajor - Returns the flag for dof ordering, true if it is point major, otherwise field major
677ea844a1aSMatthew Knepley 
678ea844a1aSMatthew Knepley   Not collective
679ea844a1aSMatthew Knepley 
680ea844a1aSMatthew Knepley   Input Parameter:
681ea844a1aSMatthew Knepley . s - the PetscSection
682ea844a1aSMatthew Knepley 
683ea844a1aSMatthew Knepley   Output Parameter:
684ea844a1aSMatthew Knepley . pm - the flag for point major ordering
685ea844a1aSMatthew Knepley 
686ea844a1aSMatthew Knepley   Level: intermediate
687ea844a1aSMatthew Knepley 
688ea844a1aSMatthew Knepley .seealso: PetscSectionSetPointMajor()
689ea844a1aSMatthew Knepley @*/
690ea844a1aSMatthew Knepley PetscErrorCode PetscSectionGetPointMajor(PetscSection s, PetscBool *pm)
691ea844a1aSMatthew Knepley {
692ea844a1aSMatthew Knepley   PetscFunctionBegin;
693ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
694ea844a1aSMatthew Knepley   PetscValidPointer(pm,2);
695ea844a1aSMatthew Knepley   *pm = s->pointMajor;
696ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
697ea844a1aSMatthew Knepley }
698ea844a1aSMatthew Knepley 
699ea844a1aSMatthew Knepley /*@
700ea844a1aSMatthew Knepley   PetscSectionSetPointMajor - Sets the flag for dof ordering, true if it is point major, otherwise field major
701ea844a1aSMatthew Knepley 
702ea844a1aSMatthew Knepley   Not collective
703ea844a1aSMatthew Knepley 
704ea844a1aSMatthew Knepley   Input Parameters:
705ea844a1aSMatthew Knepley + s  - the PetscSection
706ea844a1aSMatthew Knepley - pm - the flag for point major ordering
707ea844a1aSMatthew Knepley 
708ea844a1aSMatthew Knepley   Not collective
709ea844a1aSMatthew Knepley 
710ea844a1aSMatthew Knepley   Level: intermediate
711ea844a1aSMatthew Knepley 
712ea844a1aSMatthew Knepley .seealso: PetscSectionGetPointMajor()
713ea844a1aSMatthew Knepley @*/
714ea844a1aSMatthew Knepley PetscErrorCode PetscSectionSetPointMajor(PetscSection s, PetscBool pm)
715ea844a1aSMatthew Knepley {
716ea844a1aSMatthew Knepley   PetscFunctionBegin;
717ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
718*28b400f6SJacob Faibussowitsch   PetscCheck(!s->setup,PetscObjectComm((PetscObject) s), PETSC_ERR_ARG_WRONGSTATE, "Cannot set the dof ordering after the section is setup");
719ea844a1aSMatthew Knepley   s->pointMajor = pm;
720ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
721ea844a1aSMatthew Knepley }
722ea844a1aSMatthew Knepley 
723ea844a1aSMatthew Knepley /*@
72487e637c6Sksagiyam   PetscSectionGetIncludesConstraints - Returns the flag indicating if constrained dofs were included when computing offsets
72587e637c6Sksagiyam 
72687e637c6Sksagiyam   Not collective
72787e637c6Sksagiyam 
72887e637c6Sksagiyam   Input Parameter:
72987e637c6Sksagiyam . s - the PetscSection
73087e637c6Sksagiyam 
73187e637c6Sksagiyam   Output Parameter:
73287e637c6Sksagiyam . includesConstraints - the flag indicating if constrained dofs were included when computing offsets
73387e637c6Sksagiyam 
73487e637c6Sksagiyam   Level: intermediate
73587e637c6Sksagiyam 
73687e637c6Sksagiyam .seealso: PetscSectionSetIncludesConstraints()
73787e637c6Sksagiyam @*/
73887e637c6Sksagiyam PetscErrorCode PetscSectionGetIncludesConstraints(PetscSection s, PetscBool *includesConstraints)
73987e637c6Sksagiyam {
74087e637c6Sksagiyam   PetscFunctionBegin;
74187e637c6Sksagiyam   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
74287e637c6Sksagiyam   PetscValidBoolPointer(includesConstraints,2);
74387e637c6Sksagiyam   *includesConstraints = s->includesConstraints;
74487e637c6Sksagiyam   PetscFunctionReturn(0);
74587e637c6Sksagiyam }
74687e637c6Sksagiyam 
74787e637c6Sksagiyam /*@
74887e637c6Sksagiyam   PetscSectionSetIncludesConstraints - Sets the flag indicating if constrained dofs are to be included when computing offsets
74987e637c6Sksagiyam 
75087e637c6Sksagiyam   Not collective
75187e637c6Sksagiyam 
75287e637c6Sksagiyam   Input Parameters:
75387e637c6Sksagiyam + s  - the PetscSection
75487e637c6Sksagiyam - includesConstraints - the flag indicating if constrained dofs are to be included when computing offsets
75587e637c6Sksagiyam 
75687e637c6Sksagiyam   Not collective
75787e637c6Sksagiyam 
75887e637c6Sksagiyam   Level: intermediate
75987e637c6Sksagiyam 
76087e637c6Sksagiyam .seealso: PetscSectionGetIncludesConstraints()
76187e637c6Sksagiyam @*/
76287e637c6Sksagiyam PetscErrorCode PetscSectionSetIncludesConstraints(PetscSection s, PetscBool includesConstraints)
76387e637c6Sksagiyam {
76487e637c6Sksagiyam   PetscFunctionBegin;
76587e637c6Sksagiyam   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
766*28b400f6SJacob Faibussowitsch   PetscCheck(!s->setup,PetscObjectComm((PetscObject) s), PETSC_ERR_ARG_WRONGSTATE, "Cannot set includesConstraints after the section is set up");
76787e637c6Sksagiyam   s->includesConstraints = includesConstraints;
76887e637c6Sksagiyam   PetscFunctionReturn(0);
76987e637c6Sksagiyam }
77087e637c6Sksagiyam 
77187e637c6Sksagiyam /*@
772ea844a1aSMatthew Knepley   PetscSectionGetDof - Return the number of degrees of freedom associated with a given point.
773ea844a1aSMatthew Knepley 
774ea844a1aSMatthew Knepley   Not collective
775ea844a1aSMatthew Knepley 
776ea844a1aSMatthew Knepley   Input Parameters:
777ea844a1aSMatthew Knepley + s - the PetscSection
778ea844a1aSMatthew Knepley - point - the point
779ea844a1aSMatthew Knepley 
780ea844a1aSMatthew Knepley   Output Parameter:
781ea844a1aSMatthew Knepley . numDof - the number of dof
782ea844a1aSMatthew Knepley 
783ea844a1aSMatthew Knepley   Level: intermediate
784ea844a1aSMatthew Knepley 
785ea844a1aSMatthew Knepley .seealso: PetscSectionSetDof(), PetscSectionCreate()
786ea844a1aSMatthew Knepley @*/
787ea844a1aSMatthew Knepley PetscErrorCode PetscSectionGetDof(PetscSection s, PetscInt point, PetscInt *numDof)
788ea844a1aSMatthew Knepley {
78976bd3646SJed Brown   PetscFunctionBeginHot;
790ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
791ea844a1aSMatthew Knepley   PetscValidPointer(numDof, 3);
79276bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
7932c71b3e2SJacob Faibussowitsch     PetscCheckFalse((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);
79476bd3646SJed Brown   }
795ea844a1aSMatthew Knepley   *numDof = s->atlasDof[point - s->pStart];
796ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
797ea844a1aSMatthew Knepley }
798ea844a1aSMatthew Knepley 
799ea844a1aSMatthew Knepley /*@
800ea844a1aSMatthew Knepley   PetscSectionSetDof - Sets the number of degrees of freedom associated with a given point.
801ea844a1aSMatthew Knepley 
802ea844a1aSMatthew Knepley   Not collective
803ea844a1aSMatthew Knepley 
804ea844a1aSMatthew Knepley   Input Parameters:
805ea844a1aSMatthew Knepley + s - the PetscSection
806ea844a1aSMatthew Knepley . point - the point
807ea844a1aSMatthew Knepley - numDof - the number of dof
808ea844a1aSMatthew Knepley 
809ea844a1aSMatthew Knepley   Level: intermediate
810ea844a1aSMatthew Knepley 
811ea844a1aSMatthew Knepley .seealso: PetscSectionGetDof(), PetscSectionAddDof(), PetscSectionCreate()
812ea844a1aSMatthew Knepley @*/
813ea844a1aSMatthew Knepley PetscErrorCode PetscSectionSetDof(PetscSection s, PetscInt point, PetscInt numDof)
814ea844a1aSMatthew Knepley {
815ea844a1aSMatthew Knepley   PetscFunctionBegin;
816ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
8172c71b3e2SJacob Faibussowitsch   PetscCheckFalse((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);
818ea844a1aSMatthew Knepley   s->atlasDof[point - s->pStart] = numDof;
819ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
820ea844a1aSMatthew Knepley }
821ea844a1aSMatthew Knepley 
822ea844a1aSMatthew Knepley /*@
823ea844a1aSMatthew Knepley   PetscSectionAddDof - Adds to the number of degrees of freedom associated with a given point.
824ea844a1aSMatthew Knepley 
825ea844a1aSMatthew Knepley   Not collective
826ea844a1aSMatthew Knepley 
827ea844a1aSMatthew Knepley   Input Parameters:
828ea844a1aSMatthew Knepley + s - the PetscSection
829ea844a1aSMatthew Knepley . point - the point
830ea844a1aSMatthew Knepley - numDof - the number of additional dof
831ea844a1aSMatthew Knepley 
832ea844a1aSMatthew Knepley   Level: intermediate
833ea844a1aSMatthew Knepley 
834ea844a1aSMatthew Knepley .seealso: PetscSectionGetDof(), PetscSectionSetDof(), PetscSectionCreate()
835ea844a1aSMatthew Knepley @*/
836ea844a1aSMatthew Knepley PetscErrorCode PetscSectionAddDof(PetscSection s, PetscInt point, PetscInt numDof)
837ea844a1aSMatthew Knepley {
83876bd3646SJed Brown   PetscFunctionBeginHot;
839ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
84076bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
8412c71b3e2SJacob Faibussowitsch     PetscCheckFalse((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);
84276bd3646SJed Brown   }
843ea844a1aSMatthew Knepley   s->atlasDof[point - s->pStart] += numDof;
844ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
845ea844a1aSMatthew Knepley }
846ea844a1aSMatthew Knepley 
847ea844a1aSMatthew Knepley /*@
848ea844a1aSMatthew Knepley   PetscSectionGetFieldDof - Return the number of degrees of freedom associated with a field on a given point.
849ea844a1aSMatthew Knepley 
850ea844a1aSMatthew Knepley   Not collective
851ea844a1aSMatthew Knepley 
852ea844a1aSMatthew Knepley   Input Parameters:
853ea844a1aSMatthew Knepley + s - the PetscSection
854ea844a1aSMatthew Knepley . point - the point
855ea844a1aSMatthew Knepley - field - the field
856ea844a1aSMatthew Knepley 
857ea844a1aSMatthew Knepley   Output Parameter:
858ea844a1aSMatthew Knepley . numDof - the number of dof
859ea844a1aSMatthew Knepley 
860ea844a1aSMatthew Knepley   Level: intermediate
861ea844a1aSMatthew Knepley 
862ea844a1aSMatthew Knepley .seealso: PetscSectionSetFieldDof(), PetscSectionCreate()
863ea844a1aSMatthew Knepley @*/
864ea844a1aSMatthew Knepley PetscErrorCode PetscSectionGetFieldDof(PetscSection s, PetscInt point, PetscInt field, PetscInt *numDof)
865ea844a1aSMatthew Knepley {
866ea844a1aSMatthew Knepley   PetscFunctionBegin;
867ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
8682abc8c78SJacob Faibussowitsch   PetscValidIntPointer(numDof,4);
8692abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field,s->numFields);
8705f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetDof(s->field[field], point, numDof));
871ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
872ea844a1aSMatthew Knepley }
873ea844a1aSMatthew Knepley 
874ea844a1aSMatthew Knepley /*@
875ea844a1aSMatthew Knepley   PetscSectionSetFieldDof - Sets the number of degrees of freedom associated with a field on a given point.
876ea844a1aSMatthew Knepley 
877ea844a1aSMatthew Knepley   Not collective
878ea844a1aSMatthew Knepley 
879ea844a1aSMatthew Knepley   Input Parameters:
880ea844a1aSMatthew Knepley + s - the PetscSection
881ea844a1aSMatthew Knepley . point - the point
882ea844a1aSMatthew Knepley . field - the field
883ea844a1aSMatthew Knepley - numDof - the number of dof
884ea844a1aSMatthew Knepley 
885ea844a1aSMatthew Knepley   Level: intermediate
886ea844a1aSMatthew Knepley 
887ea844a1aSMatthew Knepley .seealso: PetscSectionGetFieldDof(), PetscSectionCreate()
888ea844a1aSMatthew Knepley @*/
889ea844a1aSMatthew Knepley PetscErrorCode PetscSectionSetFieldDof(PetscSection s, PetscInt point, PetscInt field, PetscInt numDof)
890ea844a1aSMatthew Knepley {
891ea844a1aSMatthew Knepley   PetscFunctionBegin;
892ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
8932abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field,s->numFields);
8945f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetDof(s->field[field], point, numDof));
895ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
896ea844a1aSMatthew Knepley }
897ea844a1aSMatthew Knepley 
898ea844a1aSMatthew Knepley /*@
899ea844a1aSMatthew Knepley   PetscSectionAddFieldDof - Adds a number of degrees of freedom associated with a field on a given point.
900ea844a1aSMatthew Knepley 
901ea844a1aSMatthew Knepley   Not collective
902ea844a1aSMatthew Knepley 
903ea844a1aSMatthew Knepley   Input Parameters:
904ea844a1aSMatthew Knepley + s - the PetscSection
905ea844a1aSMatthew Knepley . point - the point
906ea844a1aSMatthew Knepley . field - the field
907ea844a1aSMatthew Knepley - numDof - the number of dof
908ea844a1aSMatthew Knepley 
909ea844a1aSMatthew Knepley   Level: intermediate
910ea844a1aSMatthew Knepley 
911ea844a1aSMatthew Knepley .seealso: PetscSectionSetFieldDof(), PetscSectionGetFieldDof(), PetscSectionCreate()
912ea844a1aSMatthew Knepley @*/
913ea844a1aSMatthew Knepley PetscErrorCode PetscSectionAddFieldDof(PetscSection s, PetscInt point, PetscInt field, PetscInt numDof)
914ea844a1aSMatthew Knepley {
915ea844a1aSMatthew Knepley   PetscFunctionBegin;
916ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
9172abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field,s->numFields);
9185f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionAddDof(s->field[field], point, numDof));
919ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
920ea844a1aSMatthew Knepley }
921ea844a1aSMatthew Knepley 
922ea844a1aSMatthew Knepley /*@
923ea844a1aSMatthew Knepley   PetscSectionGetConstraintDof - Return the number of constrained degrees of freedom associated with a given point.
924ea844a1aSMatthew Knepley 
925ea844a1aSMatthew Knepley   Not collective
926ea844a1aSMatthew Knepley 
927ea844a1aSMatthew Knepley   Input Parameters:
928ea844a1aSMatthew Knepley + s - the PetscSection
929ea844a1aSMatthew Knepley - point - the point
930ea844a1aSMatthew Knepley 
931ea844a1aSMatthew Knepley   Output Parameter:
932ea844a1aSMatthew Knepley . numDof - the number of dof which are fixed by constraints
933ea844a1aSMatthew Knepley 
934ea844a1aSMatthew Knepley   Level: intermediate
935ea844a1aSMatthew Knepley 
936ea844a1aSMatthew Knepley .seealso: PetscSectionGetDof(), PetscSectionSetConstraintDof(), PetscSectionCreate()
937ea844a1aSMatthew Knepley @*/
938ea844a1aSMatthew Knepley PetscErrorCode PetscSectionGetConstraintDof(PetscSection s, PetscInt point, PetscInt *numDof)
939ea844a1aSMatthew Knepley {
940ea844a1aSMatthew Knepley   PetscFunctionBegin;
941ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
942ea844a1aSMatthew Knepley   PetscValidPointer(numDof, 3);
943ea844a1aSMatthew Knepley   if (s->bc) {
9445f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(s->bc, point, numDof));
945ea844a1aSMatthew Knepley   } else *numDof = 0;
946ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
947ea844a1aSMatthew Knepley }
948ea844a1aSMatthew Knepley 
949ea844a1aSMatthew Knepley /*@
950ea844a1aSMatthew Knepley   PetscSectionSetConstraintDof - Set the number of constrained degrees of freedom associated with a given point.
951ea844a1aSMatthew Knepley 
952ea844a1aSMatthew Knepley   Not collective
953ea844a1aSMatthew Knepley 
954ea844a1aSMatthew Knepley   Input Parameters:
955ea844a1aSMatthew Knepley + s - the PetscSection
956ea844a1aSMatthew Knepley . point - the point
957ea844a1aSMatthew Knepley - numDof - the number of dof which are fixed by constraints
958ea844a1aSMatthew Knepley 
959ea844a1aSMatthew Knepley   Level: intermediate
960ea844a1aSMatthew Knepley 
961ea844a1aSMatthew Knepley .seealso: PetscSectionSetDof(), PetscSectionGetConstraintDof(), PetscSectionCreate()
962ea844a1aSMatthew Knepley @*/
963ea844a1aSMatthew Knepley PetscErrorCode PetscSectionSetConstraintDof(PetscSection s, PetscInt point, PetscInt numDof)
964ea844a1aSMatthew Knepley {
965ea844a1aSMatthew Knepley   PetscFunctionBegin;
966ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
967ea844a1aSMatthew Knepley   if (numDof) {
9685f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionCheckConstraints_Static(s));
9695f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetDof(s->bc, point, numDof));
970ea844a1aSMatthew Knepley   }
971ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
972ea844a1aSMatthew Knepley }
973ea844a1aSMatthew Knepley 
974ea844a1aSMatthew Knepley /*@
975ea844a1aSMatthew Knepley   PetscSectionAddConstraintDof - Increment the number of constrained degrees of freedom associated with a given point.
976ea844a1aSMatthew Knepley 
977ea844a1aSMatthew Knepley   Not collective
978ea844a1aSMatthew Knepley 
979ea844a1aSMatthew Knepley   Input Parameters:
980ea844a1aSMatthew Knepley + s - the PetscSection
981ea844a1aSMatthew Knepley . point - the point
982ea844a1aSMatthew Knepley - numDof - the number of additional dof which are fixed by constraints
983ea844a1aSMatthew Knepley 
984ea844a1aSMatthew Knepley   Level: intermediate
985ea844a1aSMatthew Knepley 
986ea844a1aSMatthew Knepley .seealso: PetscSectionAddDof(), PetscSectionGetConstraintDof(), PetscSectionCreate()
987ea844a1aSMatthew Knepley @*/
988ea844a1aSMatthew Knepley PetscErrorCode PetscSectionAddConstraintDof(PetscSection s, PetscInt point, PetscInt numDof)
989ea844a1aSMatthew Knepley {
990ea844a1aSMatthew Knepley   PetscFunctionBegin;
991ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
992ea844a1aSMatthew Knepley   if (numDof) {
9935f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionCheckConstraints_Static(s));
9945f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionAddDof(s->bc, point, numDof));
995ea844a1aSMatthew Knepley   }
996ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
997ea844a1aSMatthew Knepley }
998ea844a1aSMatthew Knepley 
999ea844a1aSMatthew Knepley /*@
1000ea844a1aSMatthew Knepley   PetscSectionGetFieldConstraintDof - Return the number of constrained degrees of freedom associated with a given field on a point.
1001ea844a1aSMatthew Knepley 
1002ea844a1aSMatthew Knepley   Not collective
1003ea844a1aSMatthew Knepley 
1004ea844a1aSMatthew Knepley   Input Parameters:
1005ea844a1aSMatthew Knepley + 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 which are fixed by constraints
1011ea844a1aSMatthew Knepley 
1012ea844a1aSMatthew Knepley   Level: intermediate
1013ea844a1aSMatthew Knepley 
1014ea844a1aSMatthew Knepley .seealso: PetscSectionGetDof(), PetscSectionSetFieldConstraintDof(), PetscSectionCreate()
1015ea844a1aSMatthew Knepley @*/
1016ea844a1aSMatthew Knepley PetscErrorCode PetscSectionGetFieldConstraintDof(PetscSection s, PetscInt point, PetscInt field, PetscInt *numDof)
1017ea844a1aSMatthew Knepley {
1018ea844a1aSMatthew Knepley   PetscFunctionBegin;
1019ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
10202abc8c78SJacob Faibussowitsch   PetscValidIntPointer(numDof, 4);
10212abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field,s->numFields);
10225f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetConstraintDof(s->field[field], point, numDof));
1023ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
1024ea844a1aSMatthew Knepley }
1025ea844a1aSMatthew Knepley 
1026ea844a1aSMatthew Knepley /*@
1027ea844a1aSMatthew Knepley   PetscSectionSetFieldConstraintDof - Set the number of constrained degrees of freedom associated with a given field on a point.
1028ea844a1aSMatthew Knepley 
1029ea844a1aSMatthew Knepley   Not collective
1030ea844a1aSMatthew Knepley 
1031ea844a1aSMatthew Knepley   Input Parameters:
1032ea844a1aSMatthew Knepley + s - the PetscSection
1033ea844a1aSMatthew Knepley . point - the point
1034ea844a1aSMatthew Knepley . field - the field
1035ea844a1aSMatthew Knepley - numDof - the number of dof which are fixed by constraints
1036ea844a1aSMatthew Knepley 
1037ea844a1aSMatthew Knepley   Level: intermediate
1038ea844a1aSMatthew Knepley 
1039ea844a1aSMatthew Knepley .seealso: PetscSectionSetDof(), PetscSectionGetFieldConstraintDof(), PetscSectionCreate()
1040ea844a1aSMatthew Knepley @*/
1041ea844a1aSMatthew Knepley PetscErrorCode PetscSectionSetFieldConstraintDof(PetscSection s, PetscInt point, PetscInt field, PetscInt numDof)
1042ea844a1aSMatthew Knepley {
1043ea844a1aSMatthew Knepley   PetscFunctionBegin;
1044ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
10452abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field,s->numFields);
10465f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetConstraintDof(s->field[field], point, numDof));
1047ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
1048ea844a1aSMatthew Knepley }
1049ea844a1aSMatthew Knepley 
1050ea844a1aSMatthew Knepley /*@
1051ea844a1aSMatthew Knepley   PetscSectionAddFieldConstraintDof - Increment the number of constrained degrees of freedom associated with a given field on a point.
1052ea844a1aSMatthew Knepley 
1053ea844a1aSMatthew Knepley   Not collective
1054ea844a1aSMatthew Knepley 
1055ea844a1aSMatthew Knepley   Input Parameters:
1056ea844a1aSMatthew Knepley + s - the PetscSection
1057ea844a1aSMatthew Knepley . point - the point
1058ea844a1aSMatthew Knepley . field - the field
1059ea844a1aSMatthew Knepley - numDof - the number of additional dof which are fixed by constraints
1060ea844a1aSMatthew Knepley 
1061ea844a1aSMatthew Knepley   Level: intermediate
1062ea844a1aSMatthew Knepley 
1063ea844a1aSMatthew Knepley .seealso: PetscSectionAddDof(), PetscSectionGetFieldConstraintDof(), PetscSectionCreate()
1064ea844a1aSMatthew Knepley @*/
1065ea844a1aSMatthew Knepley PetscErrorCode PetscSectionAddFieldConstraintDof(PetscSection s, PetscInt point, PetscInt field, PetscInt numDof)
1066ea844a1aSMatthew Knepley {
1067ea844a1aSMatthew Knepley   PetscFunctionBegin;
1068ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
10692abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field,s->numFields);
10705f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionAddConstraintDof(s->field[field], point, numDof));
1071ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
1072ea844a1aSMatthew Knepley }
1073ea844a1aSMatthew Knepley 
1074ea844a1aSMatthew Knepley /*@
1075ea844a1aSMatthew Knepley   PetscSectionSetUpBC - Setup the subsections describing boundary conditions.
1076ea844a1aSMatthew Knepley 
1077ea844a1aSMatthew Knepley   Not collective
1078ea844a1aSMatthew Knepley 
1079ea844a1aSMatthew Knepley   Input Parameter:
1080ea844a1aSMatthew Knepley . s - the PetscSection
1081ea844a1aSMatthew Knepley 
1082ea844a1aSMatthew Knepley   Level: advanced
1083ea844a1aSMatthew Knepley 
1084ea844a1aSMatthew Knepley .seealso: PetscSectionSetUp(), PetscSectionCreate()
1085ea844a1aSMatthew Knepley @*/
1086ea844a1aSMatthew Knepley PetscErrorCode PetscSectionSetUpBC(PetscSection s)
1087ea844a1aSMatthew Knepley {
1088ea844a1aSMatthew Knepley   PetscFunctionBegin;
1089ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1090ea844a1aSMatthew Knepley   if (s->bc) {
1091ea844a1aSMatthew Knepley     const PetscInt last = (s->bc->pEnd-s->bc->pStart) - 1;
1092ea844a1aSMatthew Knepley 
10935f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetUp(s->bc));
10945f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(last >= 0 ? s->bc->atlasOff[last] + s->bc->atlasDof[last] : 0, &s->bcIndices));
1095ea844a1aSMatthew Knepley   }
1096ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
1097ea844a1aSMatthew Knepley }
1098ea844a1aSMatthew Knepley 
1099ea844a1aSMatthew Knepley /*@
1100ea844a1aSMatthew Knepley   PetscSectionSetUp - Calculate offsets based upon the number of degrees of freedom for each point.
1101ea844a1aSMatthew Knepley 
1102ea844a1aSMatthew Knepley   Not collective
1103ea844a1aSMatthew Knepley 
1104ea844a1aSMatthew Knepley   Input Parameter:
1105ea844a1aSMatthew Knepley . s - the PetscSection
1106ea844a1aSMatthew Knepley 
1107ea844a1aSMatthew Knepley   Level: intermediate
1108ea844a1aSMatthew Knepley 
1109ea844a1aSMatthew Knepley .seealso: PetscSectionCreate()
1110ea844a1aSMatthew Knepley @*/
1111ea844a1aSMatthew Knepley PetscErrorCode PetscSectionSetUp(PetscSection s)
1112ea844a1aSMatthew Knepley {
1113ea844a1aSMatthew Knepley   const PetscInt *pind   = NULL;
1114ea844a1aSMatthew Knepley   PetscInt        offset = 0, foff, p, f;
1115ea844a1aSMatthew Knepley 
1116ea844a1aSMatthew Knepley   PetscFunctionBegin;
1117ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1118ea844a1aSMatthew Knepley   if (s->setup) PetscFunctionReturn(0);
1119ea844a1aSMatthew Knepley   s->setup = PETSC_TRUE;
1120ea844a1aSMatthew Knepley   /* Set offsets and field offsets for all points */
1121ea844a1aSMatthew Knepley   /*   Assume that all fields have the same chart */
1122*28b400f6SJacob Faibussowitsch   PetscCheck(s->includesConstraints,PETSC_COMM_SELF,PETSC_ERR_SUP,"PetscSectionSetUp is currently unsupported for includesConstraints = PETSC_TRUE");
11235f80ce2aSJacob Faibussowitsch   if (s->perm) CHKERRQ(ISGetIndices(s->perm, &pind));
1124ea844a1aSMatthew Knepley   if (s->pointMajor) {
1125ea844a1aSMatthew Knepley     for (p = 0; p < s->pEnd - s->pStart; ++p) {
1126ea844a1aSMatthew Knepley       const PetscInt q = pind ? pind[p] : p;
1127ea844a1aSMatthew Knepley 
1128ea844a1aSMatthew Knepley       /* Set point offset */
1129ea844a1aSMatthew Knepley       s->atlasOff[q] = offset;
1130ea844a1aSMatthew Knepley       offset        += s->atlasDof[q];
1131ea844a1aSMatthew Knepley       s->maxDof      = PetscMax(s->maxDof, s->atlasDof[q]);
1132ea844a1aSMatthew Knepley       /* Set field offset */
1133ea844a1aSMatthew Knepley       for (f = 0, foff = s->atlasOff[q]; f < s->numFields; ++f) {
1134ea844a1aSMatthew Knepley         PetscSection sf = s->field[f];
1135ea844a1aSMatthew Knepley 
1136ea844a1aSMatthew Knepley         sf->atlasOff[q] = foff;
1137ea844a1aSMatthew Knepley         foff += sf->atlasDof[q];
1138ea844a1aSMatthew Knepley       }
1139ea844a1aSMatthew Knepley     }
1140ea844a1aSMatthew Knepley   } else {
1141ea844a1aSMatthew Knepley     /* Set field offsets for all points */
1142ea844a1aSMatthew Knepley     for (f = 0; f < s->numFields; ++f) {
1143ea844a1aSMatthew Knepley       PetscSection sf = s->field[f];
1144ea844a1aSMatthew Knepley 
1145ea844a1aSMatthew Knepley       for (p = 0; p < s->pEnd - s->pStart; ++p) {
1146ea844a1aSMatthew Knepley         const PetscInt q = pind ? pind[p] : p;
1147ea844a1aSMatthew Knepley 
1148ea844a1aSMatthew Knepley         sf->atlasOff[q] = offset;
1149ea844a1aSMatthew Knepley         offset += sf->atlasDof[q];
1150ea844a1aSMatthew Knepley       }
1151ea844a1aSMatthew Knepley     }
1152ea844a1aSMatthew Knepley     /* Disable point offsets since these are unused */
1153ea844a1aSMatthew Knepley     for (p = 0; p < s->pEnd - s->pStart; ++p) {
1154ea844a1aSMatthew Knepley       s->atlasOff[p] = -1;
1155ea844a1aSMatthew Knepley       s->maxDof      = PetscMax(s->maxDof, s->atlasDof[p]);
1156ea844a1aSMatthew Knepley     }
1157ea844a1aSMatthew Knepley   }
11585f80ce2aSJacob Faibussowitsch   if (s->perm) CHKERRQ(ISRestoreIndices(s->perm, &pind));
1159ea844a1aSMatthew Knepley   /* Setup BC sections */
11605f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetUpBC(s));
11615f80ce2aSJacob Faibussowitsch   for (f = 0; f < s->numFields; ++f) CHKERRQ(PetscSectionSetUpBC(s->field[f]));
1162ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
1163ea844a1aSMatthew Knepley }
1164ea844a1aSMatthew Knepley 
1165ea844a1aSMatthew Knepley /*@
1166ea844a1aSMatthew Knepley   PetscSectionGetMaxDof - Return the maximum number of degrees of freedom on any point in the chart
1167ea844a1aSMatthew Knepley 
1168ea844a1aSMatthew Knepley   Not collective
1169ea844a1aSMatthew Knepley 
1170ea844a1aSMatthew Knepley   Input Parameters:
1171ea844a1aSMatthew Knepley . s - the PetscSection
1172ea844a1aSMatthew Knepley 
1173ea844a1aSMatthew Knepley   Output Parameter:
1174ea844a1aSMatthew Knepley . maxDof - the maximum dof
1175ea844a1aSMatthew Knepley 
1176ea844a1aSMatthew Knepley   Level: intermediate
1177ea844a1aSMatthew Knepley 
1178ea844a1aSMatthew Knepley .seealso: PetscSectionGetDof(), PetscSectionSetDof(), PetscSectionCreate()
1179ea844a1aSMatthew Knepley @*/
1180ea844a1aSMatthew Knepley PetscErrorCode PetscSectionGetMaxDof(PetscSection s, PetscInt *maxDof)
1181ea844a1aSMatthew Knepley {
1182ea844a1aSMatthew Knepley   PetscFunctionBegin;
1183ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1184ea844a1aSMatthew Knepley   PetscValidPointer(maxDof, 2);
1185ea844a1aSMatthew Knepley   *maxDof = s->maxDof;
1186ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
1187ea844a1aSMatthew Knepley }
1188ea844a1aSMatthew Knepley 
1189ea844a1aSMatthew Knepley /*@
1190ea844a1aSMatthew Knepley   PetscSectionGetStorageSize - Return the size of an array or local Vec capable of holding all the degrees of freedom.
1191ea844a1aSMatthew Knepley 
1192ea844a1aSMatthew Knepley   Not collective
1193ea844a1aSMatthew Knepley 
1194ea844a1aSMatthew Knepley   Input Parameter:
1195ea844a1aSMatthew Knepley . s - the PetscSection
1196ea844a1aSMatthew Knepley 
1197ea844a1aSMatthew Knepley   Output Parameter:
1198ea844a1aSMatthew Knepley . size - the size of an array which can hold all the dofs
1199ea844a1aSMatthew Knepley 
1200ea844a1aSMatthew Knepley   Level: intermediate
1201ea844a1aSMatthew Knepley 
1202ea844a1aSMatthew Knepley .seealso: PetscSectionGetOffset(), PetscSectionGetConstrainedStorageSize(), PetscSectionCreate()
1203ea844a1aSMatthew Knepley @*/
1204ea844a1aSMatthew Knepley PetscErrorCode PetscSectionGetStorageSize(PetscSection s, PetscInt *size)
1205ea844a1aSMatthew Knepley {
1206ea844a1aSMatthew Knepley   PetscInt p, n = 0;
1207ea844a1aSMatthew Knepley 
1208ea844a1aSMatthew Knepley   PetscFunctionBegin;
1209ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1210ea844a1aSMatthew Knepley   PetscValidPointer(size, 2);
1211ea844a1aSMatthew Knepley   for (p = 0; p < s->pEnd - s->pStart; ++p) n += s->atlasDof[p] > 0 ? s->atlasDof[p] : 0;
1212ea844a1aSMatthew Knepley   *size = n;
1213ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
1214ea844a1aSMatthew Knepley }
1215ea844a1aSMatthew Knepley 
1216ea844a1aSMatthew Knepley /*@
1217ea844a1aSMatthew Knepley   PetscSectionGetConstrainedStorageSize - Return the size of an array or local Vec capable of holding all unconstrained degrees of freedom.
1218ea844a1aSMatthew Knepley 
1219ea844a1aSMatthew Knepley   Not collective
1220ea844a1aSMatthew Knepley 
1221064ec1f3Sprj-   Input Parameter:
1222064ec1f3Sprj- . s - the PetscSection
1223ea844a1aSMatthew Knepley 
1224ea844a1aSMatthew Knepley   Output Parameter:
1225ea844a1aSMatthew Knepley . size - the size of an array which can hold all unconstrained dofs
1226ea844a1aSMatthew Knepley 
1227ea844a1aSMatthew Knepley   Level: intermediate
1228ea844a1aSMatthew Knepley 
1229ea844a1aSMatthew Knepley .seealso: PetscSectionGetStorageSize(), PetscSectionGetOffset(), PetscSectionCreate()
1230ea844a1aSMatthew Knepley @*/
1231ea844a1aSMatthew Knepley PetscErrorCode PetscSectionGetConstrainedStorageSize(PetscSection s, PetscInt *size)
1232ea844a1aSMatthew Knepley {
1233ea844a1aSMatthew Knepley   PetscInt p, n = 0;
1234ea844a1aSMatthew Knepley 
1235ea844a1aSMatthew Knepley   PetscFunctionBegin;
1236ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1237ea844a1aSMatthew Knepley   PetscValidPointer(size, 2);
1238ea844a1aSMatthew Knepley   for (p = 0; p < s->pEnd - s->pStart; ++p) {
1239ea844a1aSMatthew Knepley     const PetscInt cdof = s->bc ? s->bc->atlasDof[p] : 0;
1240ea844a1aSMatthew Knepley     n += s->atlasDof[p] > 0 ? s->atlasDof[p] - cdof : 0;
1241ea844a1aSMatthew Knepley   }
1242ea844a1aSMatthew Knepley   *size = n;
1243ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
1244ea844a1aSMatthew Knepley }
1245ea844a1aSMatthew Knepley 
1246ea844a1aSMatthew Knepley /*@
1247ea844a1aSMatthew Knepley   PetscSectionCreateGlobalSection - Create a section describing the global field layout using
1248ea844a1aSMatthew Knepley   the local section and an SF describing the section point overlap.
1249ea844a1aSMatthew Knepley 
1250ea844a1aSMatthew Knepley   Input Parameters:
1251ea844a1aSMatthew Knepley + s - The PetscSection for the local field layout
1252ea844a1aSMatthew Knepley . sf - The SF describing parallel layout of the section points (leaves are unowned local points)
1253ea844a1aSMatthew Knepley . includeConstraints - By default this is PETSC_FALSE, meaning that the global field vector will not possess constrained dofs
1254ea844a1aSMatthew Knepley - localOffsets - If PETSC_TRUE, use local rather than global offsets for the points
1255ea844a1aSMatthew Knepley 
1256ea844a1aSMatthew Knepley   Output Parameter:
1257ea844a1aSMatthew Knepley . gsection - The PetscSection for the global field layout
1258ea844a1aSMatthew Knepley 
1259ea844a1aSMatthew Knepley   Note: This gives negative sizes and offsets to points not owned by this process
1260ea844a1aSMatthew Knepley 
1261ea844a1aSMatthew Knepley   Level: intermediate
1262ea844a1aSMatthew Knepley 
1263ea844a1aSMatthew Knepley .seealso: PetscSectionCreate()
1264ea844a1aSMatthew Knepley @*/
1265ea844a1aSMatthew Knepley PetscErrorCode PetscSectionCreateGlobalSection(PetscSection s, PetscSF sf, PetscBool includeConstraints, PetscBool localOffsets, PetscSection *gsection)
1266ea844a1aSMatthew Knepley {
1267ea844a1aSMatthew Knepley   PetscSection    gs;
1268ea844a1aSMatthew Knepley   const PetscInt *pind = NULL;
1269ea844a1aSMatthew Knepley   PetscInt       *recv = NULL, *neg = NULL;
1270046d2115Sksagiyam   PetscInt        pStart, pEnd, p, dof, cdof, off, foff, globalOff = 0, nroots, nlocal, maxleaf;
1271046d2115Sksagiyam   PetscInt        numFields, f, numComponents;
1272ea844a1aSMatthew Knepley 
1273ea844a1aSMatthew Knepley   PetscFunctionBegin;
1274ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1275ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
1276ea844a1aSMatthew Knepley   PetscValidLogicalCollectiveBool(s, includeConstraints, 3);
1277ea844a1aSMatthew Knepley   PetscValidLogicalCollectiveBool(s, localOffsets, 4);
1278ea844a1aSMatthew Knepley   PetscValidPointer(gsection, 5);
1279*28b400f6SJacob Faibussowitsch   PetscCheck(s->pointMajor,PETSC_COMM_SELF,PETSC_ERR_SUP, "No support for field major ordering");
12805f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionCreate(PetscObjectComm((PetscObject) s), &gs));
12815f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetNumFields(s, &numFields));
12825f80ce2aSJacob Faibussowitsch   if (numFields > 0) CHKERRQ(PetscSectionSetNumFields(gs, numFields));
12835f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetChart(s, &pStart, &pEnd));
12845f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetChart(gs, pStart, pEnd));
128587e637c6Sksagiyam   gs->includesConstraints = includeConstraints;
12865f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSFGetGraph(sf, &nroots, NULL, NULL, NULL));
1287ea844a1aSMatthew Knepley   nlocal = nroots;              /* The local/leaf space matches global/root space */
1288ea844a1aSMatthew Knepley   /* Must allocate for all points visible to SF, which may be more than this section */
1289ea844a1aSMatthew Knepley   if (nroots >= 0) {             /* nroots < 0 means that the graph has not been set, only happens in serial */
12905f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSFGetLeafRange(sf, NULL, &maxleaf));
12912c71b3e2SJacob Faibussowitsch     PetscCheckFalse(nroots < pEnd,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "SF roots %" PetscInt_FMT " < pEnd %" PetscInt_FMT, nroots, pEnd);
12922c71b3e2SJacob Faibussowitsch     PetscCheckFalse(maxleaf >= nroots,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Max local leaf %" PetscInt_FMT " >= nroots %" PetscInt_FMT, maxleaf, nroots);
12935f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc2(nroots,&neg,nlocal,&recv));
12945f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscArrayzero(neg,nroots));
1295ea844a1aSMatthew Knepley   }
1296ea844a1aSMatthew Knepley   /* Mark all local points with negative dof */
1297ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
12985f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(s, p, &dof));
12995f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetDof(gs, p, dof));
13005f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetConstraintDof(s, p, &cdof));
13015f80ce2aSJacob Faibussowitsch     if (!includeConstraints && cdof > 0) CHKERRQ(PetscSectionSetConstraintDof(gs, p, cdof));
1302ea844a1aSMatthew Knepley     if (neg) neg[p] = -(dof+1);
1303ea844a1aSMatthew Knepley   }
13045f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetUpBC(gs));
13055f80ce2aSJacob Faibussowitsch   if (gs->bcIndices) CHKERRQ(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]));
1306ea844a1aSMatthew Knepley   if (nroots >= 0) {
13075f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscArrayzero(recv,nlocal));
13085f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSFBcastBegin(sf, MPIU_INT, neg, recv,MPI_REPLACE));
13095f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSFBcastEnd(sf, MPIU_INT, neg, recv,MPI_REPLACE));
1310ea844a1aSMatthew Knepley     for (p = pStart; p < pEnd; ++p) {
1311ea844a1aSMatthew Knepley       if (recv[p] < 0) {
1312ea844a1aSMatthew Knepley         gs->atlasDof[p-pStart] = recv[p];
13135f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetDof(s, p, &dof));
13142c71b3e2SJacob Faibussowitsch         PetscCheckFalse(-(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);
1315ea844a1aSMatthew Knepley       }
1316ea844a1aSMatthew Knepley     }
1317ea844a1aSMatthew Knepley   }
1318ea844a1aSMatthew Knepley   /* Calculate new sizes, get process offset, and calculate point offsets */
13195f80ce2aSJacob Faibussowitsch   if (s->perm) CHKERRQ(ISGetIndices(s->perm, &pind));
1320ea844a1aSMatthew Knepley   for (p = 0, off = 0; p < pEnd-pStart; ++p) {
1321ea844a1aSMatthew Knepley     const PetscInt q = pind ? pind[p] : p;
1322ea844a1aSMatthew Knepley 
1323ea844a1aSMatthew Knepley     cdof = (!includeConstraints && s->bc) ? s->bc->atlasDof[q] : 0;
1324ea844a1aSMatthew Knepley     gs->atlasOff[q] = off;
1325ea844a1aSMatthew Knepley     off += gs->atlasDof[q] > 0 ? gs->atlasDof[q]-cdof : 0;
1326ea844a1aSMatthew Knepley   }
1327ea844a1aSMatthew Knepley   if (!localOffsets) {
13285f80ce2aSJacob Faibussowitsch     CHKERRMPI(MPI_Scan(&off, &globalOff, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject) s)));
1329ea844a1aSMatthew Knepley     globalOff -= off;
1330ea844a1aSMatthew Knepley   }
1331ea844a1aSMatthew Knepley   for (p = pStart, off = 0; p < pEnd; ++p) {
1332ea844a1aSMatthew Knepley     gs->atlasOff[p-pStart] += globalOff;
1333ea844a1aSMatthew Knepley     if (neg) neg[p] = -(gs->atlasOff[p-pStart]+1);
1334ea844a1aSMatthew Knepley   }
13355f80ce2aSJacob Faibussowitsch   if (s->perm) CHKERRQ(ISRestoreIndices(s->perm, &pind));
1336ea844a1aSMatthew Knepley   /* Put in negative offsets for ghost points */
1337ea844a1aSMatthew Knepley   if (nroots >= 0) {
13385f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscArrayzero(recv,nlocal));
13395f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSFBcastBegin(sf, MPIU_INT, neg, recv,MPI_REPLACE));
13405f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSFBcastEnd(sf, MPIU_INT, neg, recv,MPI_REPLACE));
1341ea844a1aSMatthew Knepley     for (p = pStart; p < pEnd; ++p) {
1342ea844a1aSMatthew Knepley       if (recv[p] < 0) gs->atlasOff[p-pStart] = recv[p];
1343ea844a1aSMatthew Knepley     }
1344ea844a1aSMatthew Knepley   }
13455f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree2(neg,recv));
1346046d2115Sksagiyam   /* Set field dofs/offsets/constraints */
1347046d2115Sksagiyam   for (f = 0; f < numFields; ++f) {
1348046d2115Sksagiyam     gs->field[f]->includesConstraints = includeConstraints;
13495f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetFieldComponents(s, f, &numComponents));
13505f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetFieldComponents(gs, f, numComponents));
1351046d2115Sksagiyam   }
1352046d2115Sksagiyam   for (p = pStart; p < pEnd; ++p) {
13535f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetOffset(gs, p, &off));
1354046d2115Sksagiyam     for (f = 0, foff = off; f < numFields; ++f) {
13555f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetFieldConstraintDof(s, p, f, &cdof));
13565f80ce2aSJacob Faibussowitsch       if (!includeConstraints && cdof > 0) CHKERRQ(PetscSectionSetFieldConstraintDof(gs, p, f, cdof));
13575f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetFieldDof(s, p, f, &dof));
13585f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetFieldDof(gs, p, f, off < 0 ? -(dof + 1) : dof));
13595f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetFieldOffset(gs, p, f, foff));
13605f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetFieldConstraintDof(gs, p, f, &cdof));
1361046d2115Sksagiyam       foff = off < 0 ? foff - (dof - cdof) : foff + (dof - cdof);
1362046d2115Sksagiyam     }
1363046d2115Sksagiyam   }
1364046d2115Sksagiyam   for (f = 0; f < numFields; ++f) {
1365046d2115Sksagiyam     PetscSection gfs = gs->field[f];
1366046d2115Sksagiyam 
13675f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetUpBC(gfs));
13685f80ce2aSJacob Faibussowitsch     if (gfs->bcIndices) CHKERRQ(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]));
1369046d2115Sksagiyam   }
1370046d2115Sksagiyam   gs->setup = PETSC_TRUE;
13715f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionViewFromOptions(gs, NULL, "-global_section_view"));
1372ea844a1aSMatthew Knepley   *gsection = gs;
1373ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
1374ea844a1aSMatthew Knepley }
1375ea844a1aSMatthew Knepley 
1376ea844a1aSMatthew Knepley /*@
1377ea844a1aSMatthew Knepley   PetscSectionCreateGlobalSectionCensored - Create a section describing the global field layout using
1378ea844a1aSMatthew Knepley   the local section and an SF describing the section point overlap.
1379ea844a1aSMatthew Knepley 
1380ea844a1aSMatthew Knepley   Input Parameters:
1381ea844a1aSMatthew Knepley   + s - The PetscSection for the local field layout
1382ea844a1aSMatthew Knepley   . sf - The SF describing parallel layout of the section points
1383ea844a1aSMatthew Knepley   . includeConstraints - By default this is PETSC_FALSE, meaning that the global field vector will not possess constrained dofs
1384ea844a1aSMatthew Knepley   . numExcludes - The number of exclusion ranges
1385ea844a1aSMatthew Knepley   - excludes - An array [start_0, end_0, start_1, end_1, ...] where there are numExcludes pairs
1386ea844a1aSMatthew Knepley 
1387ea844a1aSMatthew Knepley   Output Parameter:
1388ea844a1aSMatthew Knepley   . gsection - The PetscSection for the global field layout
1389ea844a1aSMatthew Knepley 
1390ea844a1aSMatthew Knepley   Note: This gives negative sizes and offsets to points not owned by this process
1391ea844a1aSMatthew Knepley 
1392ea844a1aSMatthew Knepley   Level: advanced
1393ea844a1aSMatthew Knepley 
1394ea844a1aSMatthew Knepley .seealso: PetscSectionCreate()
1395ea844a1aSMatthew Knepley @*/
1396ea844a1aSMatthew Knepley PetscErrorCode PetscSectionCreateGlobalSectionCensored(PetscSection s, PetscSF sf, PetscBool includeConstraints, PetscInt numExcludes, const PetscInt excludes[], PetscSection *gsection)
1397ea844a1aSMatthew Knepley {
1398ea844a1aSMatthew Knepley   const PetscInt *pind = NULL;
1399ea844a1aSMatthew Knepley   PetscInt       *neg  = NULL, *tmpOff = NULL;
1400ea844a1aSMatthew Knepley   PetscInt        pStart, pEnd, p, e, dof, cdof, off, globalOff = 0, nroots;
1401ea844a1aSMatthew Knepley 
1402ea844a1aSMatthew Knepley   PetscFunctionBegin;
1403ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1404ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
1405ea844a1aSMatthew Knepley   PetscValidPointer(gsection, 6);
14065f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionCreate(PetscObjectComm((PetscObject) s), gsection));
14075f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetChart(s, &pStart, &pEnd));
14085f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetChart(*gsection, pStart, pEnd));
14095f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSFGetGraph(sf, &nroots, NULL, NULL, NULL));
1410ea844a1aSMatthew Knepley   if (nroots >= 0) {
14112c71b3e2SJacob Faibussowitsch     PetscCheckFalse(nroots < pEnd-pStart,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "PetscSF nroots %" PetscInt_FMT " < %" PetscInt_FMT " section size", nroots, pEnd-pStart);
14125f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscCalloc1(nroots, &neg));
1413ea844a1aSMatthew Knepley     if (nroots > pEnd-pStart) {
14145f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscCalloc1(nroots, &tmpOff));
1415ea844a1aSMatthew Knepley     } else {
1416ea844a1aSMatthew Knepley       tmpOff = &(*gsection)->atlasDof[-pStart];
1417ea844a1aSMatthew Knepley     }
1418ea844a1aSMatthew Knepley   }
1419ea844a1aSMatthew Knepley   /* Mark ghost points with negative dof */
1420ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
1421ea844a1aSMatthew Knepley     for (e = 0; e < numExcludes; ++e) {
1422ea844a1aSMatthew Knepley       if ((p >= excludes[e*2+0]) && (p < excludes[e*2+1])) {
14235f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionSetDof(*gsection, p, 0));
1424ea844a1aSMatthew Knepley         break;
1425ea844a1aSMatthew Knepley       }
1426ea844a1aSMatthew Knepley     }
1427ea844a1aSMatthew Knepley     if (e < numExcludes) continue;
14285f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(s, p, &dof));
14295f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetDof(*gsection, p, dof));
14305f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetConstraintDof(s, p, &cdof));
14315f80ce2aSJacob Faibussowitsch     if (!includeConstraints && cdof > 0) CHKERRQ(PetscSectionSetConstraintDof(*gsection, p, cdof));
1432ea844a1aSMatthew Knepley     if (neg) neg[p] = -(dof+1);
1433ea844a1aSMatthew Knepley   }
14345f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetUpBC(*gsection));
1435ea844a1aSMatthew Knepley   if (nroots >= 0) {
14365f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSFBcastBegin(sf, MPIU_INT, neg, tmpOff,MPI_REPLACE));
14375f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSFBcastEnd(sf, MPIU_INT, neg, tmpOff,MPI_REPLACE));
1438ea844a1aSMatthew Knepley     if (nroots > pEnd - pStart) {
1439ea844a1aSMatthew Knepley       for (p = pStart; p < pEnd; ++p) {if (tmpOff[p] < 0) (*gsection)->atlasDof[p-pStart] = tmpOff[p];}
1440ea844a1aSMatthew Knepley     }
1441ea844a1aSMatthew Knepley   }
1442ea844a1aSMatthew Knepley   /* Calculate new sizes, get proccess offset, and calculate point offsets */
14435f80ce2aSJacob Faibussowitsch   if (s->perm) CHKERRQ(ISGetIndices(s->perm, &pind));
1444ea844a1aSMatthew Knepley   for (p = 0, off = 0; p < pEnd-pStart; ++p) {
1445ea844a1aSMatthew Knepley     const PetscInt q = pind ? pind[p] : p;
1446ea844a1aSMatthew Knepley 
1447ea844a1aSMatthew Knepley     cdof = (!includeConstraints && s->bc) ? s->bc->atlasDof[q] : 0;
1448ea844a1aSMatthew Knepley     (*gsection)->atlasOff[q] = off;
1449ea844a1aSMatthew Knepley     off += (*gsection)->atlasDof[q] > 0 ? (*gsection)->atlasDof[q]-cdof : 0;
1450ea844a1aSMatthew Knepley   }
14515f80ce2aSJacob Faibussowitsch   CHKERRMPI(MPI_Scan(&off, &globalOff, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject) s)));
1452ea844a1aSMatthew Knepley   globalOff -= off;
1453ea844a1aSMatthew Knepley   for (p = 0, off = 0; p < pEnd-pStart; ++p) {
1454ea844a1aSMatthew Knepley     (*gsection)->atlasOff[p] += globalOff;
1455ea844a1aSMatthew Knepley     if (neg) neg[p+pStart] = -((*gsection)->atlasOff[p]+1);
1456ea844a1aSMatthew Knepley   }
14575f80ce2aSJacob Faibussowitsch   if (s->perm) CHKERRQ(ISRestoreIndices(s->perm, &pind));
1458ea844a1aSMatthew Knepley   /* Put in negative offsets for ghost points */
1459ea844a1aSMatthew Knepley   if (nroots >= 0) {
1460ea844a1aSMatthew Knepley     if (nroots == pEnd-pStart) tmpOff = &(*gsection)->atlasOff[-pStart];
14615f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSFBcastBegin(sf, MPIU_INT, neg, tmpOff,MPI_REPLACE));
14625f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSFBcastEnd(sf, MPIU_INT, neg, tmpOff,MPI_REPLACE));
1463ea844a1aSMatthew Knepley     if (nroots > pEnd - pStart) {
1464ea844a1aSMatthew Knepley       for (p = pStart; p < pEnd; ++p) {if (tmpOff[p] < 0) (*gsection)->atlasOff[p-pStart] = tmpOff[p];}
1465ea844a1aSMatthew Knepley     }
1466ea844a1aSMatthew Knepley   }
14675f80ce2aSJacob Faibussowitsch   if (nroots >= 0 && nroots > pEnd-pStart) CHKERRQ(PetscFree(tmpOff));
14685f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(neg));
1469ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
1470ea844a1aSMatthew Knepley }
1471ea844a1aSMatthew Knepley 
1472ea844a1aSMatthew Knepley /*@
1473ea844a1aSMatthew Knepley   PetscSectionGetPointLayout - Get the PetscLayout associated with the section points
1474ea844a1aSMatthew Knepley 
1475ea844a1aSMatthew Knepley   Collective on comm
1476ea844a1aSMatthew Knepley 
1477ea844a1aSMatthew Knepley   Input Parameters:
1478ea844a1aSMatthew Knepley + comm - The MPI_Comm
1479ea844a1aSMatthew Knepley - s    - The PetscSection
1480ea844a1aSMatthew Knepley 
1481ea844a1aSMatthew Knepley   Output Parameter:
1482ea844a1aSMatthew Knepley . layout - The point layout for the section
1483ea844a1aSMatthew Knepley 
14841681d8a4SMatthew Knepley   Note: This is usually called for the default global section.
1485ea844a1aSMatthew Knepley 
1486ea844a1aSMatthew Knepley   Level: advanced
1487ea844a1aSMatthew Knepley 
1488ea844a1aSMatthew Knepley .seealso: PetscSectionGetValueLayout(), PetscSectionCreate()
1489ea844a1aSMatthew Knepley @*/
1490ea844a1aSMatthew Knepley PetscErrorCode PetscSectionGetPointLayout(MPI_Comm comm, PetscSection s, PetscLayout *layout)
1491ea844a1aSMatthew Knepley {
1492ea844a1aSMatthew Knepley   PetscInt       pStart, pEnd, p, localSize = 0;
1493ea844a1aSMatthew Knepley 
1494ea844a1aSMatthew Knepley   PetscFunctionBegin;
14955f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetChart(s, &pStart, &pEnd));
1496ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
1497ea844a1aSMatthew Knepley     PetscInt dof;
1498ea844a1aSMatthew Knepley 
14995f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(s, p, &dof));
15009120ba30SVaclav Hapla     if (dof >= 0) ++localSize;
1501ea844a1aSMatthew Knepley   }
15025f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLayoutCreate(comm, layout));
15035f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLayoutSetLocalSize(*layout, localSize));
15045f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLayoutSetBlockSize(*layout, 1));
15055f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLayoutSetUp(*layout));
1506ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
1507ea844a1aSMatthew Knepley }
1508ea844a1aSMatthew Knepley 
1509ea844a1aSMatthew Knepley /*@
1510ea844a1aSMatthew Knepley   PetscSectionGetValueLayout - Get the PetscLayout associated with the section dofs.
1511ea844a1aSMatthew Knepley 
1512ea844a1aSMatthew Knepley   Collective on comm
1513ea844a1aSMatthew Knepley 
1514ea844a1aSMatthew Knepley   Input Parameters:
1515ea844a1aSMatthew Knepley + comm - The MPI_Comm
1516ea844a1aSMatthew Knepley - s    - The PetscSection
1517ea844a1aSMatthew Knepley 
1518ea844a1aSMatthew Knepley   Output Parameter:
1519ea844a1aSMatthew Knepley . layout - The dof layout for the section
1520ea844a1aSMatthew Knepley 
1521ea844a1aSMatthew Knepley   Note: This is usually called for the default global section.
1522ea844a1aSMatthew Knepley 
1523ea844a1aSMatthew Knepley   Level: advanced
1524ea844a1aSMatthew Knepley 
1525ea844a1aSMatthew Knepley .seealso: PetscSectionGetPointLayout(), PetscSectionCreate()
1526ea844a1aSMatthew Knepley @*/
1527ea844a1aSMatthew Knepley PetscErrorCode PetscSectionGetValueLayout(MPI_Comm comm, PetscSection s, PetscLayout *layout)
1528ea844a1aSMatthew Knepley {
1529ea844a1aSMatthew Knepley   PetscInt       pStart, pEnd, p, localSize = 0;
1530ea844a1aSMatthew Knepley 
1531ea844a1aSMatthew Knepley   PetscFunctionBegin;
1532ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 2);
1533ea844a1aSMatthew Knepley   PetscValidPointer(layout, 3);
15345f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetChart(s, &pStart, &pEnd));
1535ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
1536ea844a1aSMatthew Knepley     PetscInt dof,cdof;
1537ea844a1aSMatthew Knepley 
15385f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(s, p, &dof));
15395f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetConstraintDof(s, p, &cdof));
1540ea844a1aSMatthew Knepley     if (dof-cdof > 0) localSize += dof-cdof;
1541ea844a1aSMatthew Knepley   }
15425f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLayoutCreate(comm, layout));
15435f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLayoutSetLocalSize(*layout, localSize));
15445f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLayoutSetBlockSize(*layout, 1));
15455f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLayoutSetUp(*layout));
1546ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
1547ea844a1aSMatthew Knepley }
1548ea844a1aSMatthew Knepley 
1549ea844a1aSMatthew Knepley /*@
1550ea844a1aSMatthew Knepley   PetscSectionGetOffset - Return the offset into an array or local Vec for the dof associated with the given point.
1551ea844a1aSMatthew Knepley 
1552ea844a1aSMatthew Knepley   Not collective
1553ea844a1aSMatthew Knepley 
1554ea844a1aSMatthew Knepley   Input Parameters:
1555ea844a1aSMatthew Knepley + s - the PetscSection
1556ea844a1aSMatthew Knepley - point - the point
1557ea844a1aSMatthew Knepley 
1558ea844a1aSMatthew Knepley   Output Parameter:
1559ea844a1aSMatthew Knepley . offset - the offset
1560ea844a1aSMatthew Knepley 
1561ea844a1aSMatthew Knepley   Level: intermediate
1562ea844a1aSMatthew Knepley 
1563ea844a1aSMatthew Knepley .seealso: PetscSectionGetFieldOffset(), PetscSectionCreate()
1564ea844a1aSMatthew Knepley @*/
1565ea844a1aSMatthew Knepley PetscErrorCode PetscSectionGetOffset(PetscSection s, PetscInt point, PetscInt *offset)
1566ea844a1aSMatthew Knepley {
1567ea844a1aSMatthew Knepley   PetscFunctionBegin;
1568ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1569ea844a1aSMatthew Knepley   PetscValidPointer(offset, 3);
157076bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
15712c71b3e2SJacob Faibussowitsch     PetscCheckFalse((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);
157276bd3646SJed Brown   }
1573ea844a1aSMatthew Knepley   *offset = s->atlasOff[point - s->pStart];
1574ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
1575ea844a1aSMatthew Knepley }
1576ea844a1aSMatthew Knepley 
1577ea844a1aSMatthew Knepley /*@
1578ea844a1aSMatthew Knepley   PetscSectionSetOffset - Set the offset into an array or local Vec for the dof associated with the given point.
1579ea844a1aSMatthew Knepley 
1580ea844a1aSMatthew Knepley   Not collective
1581ea844a1aSMatthew Knepley 
1582ea844a1aSMatthew Knepley   Input Parameters:
1583ea844a1aSMatthew Knepley + s - the PetscSection
1584ea844a1aSMatthew Knepley . point - the point
1585ea844a1aSMatthew Knepley - offset - the offset
1586ea844a1aSMatthew Knepley 
1587ea844a1aSMatthew Knepley   Note: The user usually does not call this function, but uses PetscSectionSetUp()
1588ea844a1aSMatthew Knepley 
1589ea844a1aSMatthew Knepley   Level: intermediate
1590ea844a1aSMatthew Knepley 
1591ea844a1aSMatthew Knepley .seealso: PetscSectionGetFieldOffset(), PetscSectionCreate(), PetscSectionSetUp()
1592ea844a1aSMatthew Knepley @*/
1593ea844a1aSMatthew Knepley PetscErrorCode PetscSectionSetOffset(PetscSection s, PetscInt point, PetscInt offset)
1594ea844a1aSMatthew Knepley {
1595ea844a1aSMatthew Knepley   PetscFunctionBegin;
1596ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
15972c71b3e2SJacob Faibussowitsch   PetscCheckFalse((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);
1598ea844a1aSMatthew Knepley   s->atlasOff[point - s->pStart] = offset;
1599ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
1600ea844a1aSMatthew Knepley }
1601ea844a1aSMatthew Knepley 
1602ea844a1aSMatthew Knepley /*@
1603ea844a1aSMatthew Knepley   PetscSectionGetFieldOffset - Return the offset into an array or local Vec for the dof associated with the given point.
1604ea844a1aSMatthew Knepley 
1605ea844a1aSMatthew Knepley   Not collective
1606ea844a1aSMatthew Knepley 
1607ea844a1aSMatthew Knepley   Input Parameters:
1608ea844a1aSMatthew Knepley + s - the PetscSection
1609ea844a1aSMatthew Knepley . point - the point
1610ea844a1aSMatthew Knepley - field - the field
1611ea844a1aSMatthew Knepley 
1612ea844a1aSMatthew Knepley   Output Parameter:
1613ea844a1aSMatthew Knepley . offset - the offset
1614ea844a1aSMatthew Knepley 
1615ea844a1aSMatthew Knepley   Level: intermediate
1616ea844a1aSMatthew Knepley 
1617ea844a1aSMatthew Knepley .seealso: PetscSectionGetOffset(), PetscSectionCreate()
1618ea844a1aSMatthew Knepley @*/
1619ea844a1aSMatthew Knepley PetscErrorCode PetscSectionGetFieldOffset(PetscSection s, PetscInt point, PetscInt field, PetscInt *offset)
1620ea844a1aSMatthew Knepley {
1621ea844a1aSMatthew Knepley   PetscFunctionBegin;
1622ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
16232abc8c78SJacob Faibussowitsch   PetscValidIntPointer(offset, 4);
16242abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field,s->numFields);
16255f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetOffset(s->field[field], point, offset));
1626ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
1627ea844a1aSMatthew Knepley }
1628ea844a1aSMatthew Knepley 
1629ea844a1aSMatthew Knepley /*@
16301681d8a4SMatthew Knepley   PetscSectionSetFieldOffset - Set the offset into an array or local Vec for the dof associated with the given field at a point.
1631ea844a1aSMatthew Knepley 
1632ea844a1aSMatthew Knepley   Not collective
1633ea844a1aSMatthew Knepley 
1634ea844a1aSMatthew Knepley   Input Parameters:
1635ea844a1aSMatthew Knepley + s - the PetscSection
1636ea844a1aSMatthew Knepley . point - the point
1637ea844a1aSMatthew Knepley . field - the field
1638ea844a1aSMatthew Knepley - offset - the offset
1639ea844a1aSMatthew Knepley 
1640ea844a1aSMatthew Knepley   Note: The user usually does not call this function, but uses PetscSectionSetUp()
1641ea844a1aSMatthew Knepley 
1642ea844a1aSMatthew Knepley   Level: intermediate
1643ea844a1aSMatthew Knepley 
16441681d8a4SMatthew Knepley .seealso: PetscSectionGetFieldOffset(), PetscSectionSetOffset(), PetscSectionCreate(), PetscSectionSetUp()
1645ea844a1aSMatthew Knepley @*/
1646ea844a1aSMatthew Knepley PetscErrorCode PetscSectionSetFieldOffset(PetscSection s, PetscInt point, PetscInt field, PetscInt offset)
1647ea844a1aSMatthew Knepley {
1648ea844a1aSMatthew Knepley   PetscFunctionBegin;
1649ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
16502abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field,s->numFields);
16515f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetOffset(s->field[field], point, offset));
1652ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
1653ea844a1aSMatthew Knepley }
1654ea844a1aSMatthew Knepley 
1655ea844a1aSMatthew Knepley /*@
1656ea844a1aSMatthew Knepley   PetscSectionGetFieldPointOffset - Return the offset on the given point for the dof associated with the given point.
1657ea844a1aSMatthew Knepley 
1658ea844a1aSMatthew Knepley   Not collective
1659ea844a1aSMatthew Knepley 
1660ea844a1aSMatthew Knepley   Input Parameters:
1661ea844a1aSMatthew Knepley + s - the PetscSection
1662ea844a1aSMatthew Knepley . point - the point
1663ea844a1aSMatthew Knepley - field - the field
1664ea844a1aSMatthew Knepley 
1665ea844a1aSMatthew Knepley   Output Parameter:
1666ea844a1aSMatthew Knepley . offset - the offset
1667ea844a1aSMatthew Knepley 
1668ea844a1aSMatthew Knepley   Note: This gives the offset on a point of the field, ignoring constraints, meaning starting at the first dof for
1669ea844a1aSMatthew Knepley         this point, what number is the first dof with this field.
1670ea844a1aSMatthew Knepley 
1671ea844a1aSMatthew Knepley   Level: advanced
1672ea844a1aSMatthew Knepley 
1673ea844a1aSMatthew Knepley .seealso: PetscSectionGetOffset(), PetscSectionCreate()
1674ea844a1aSMatthew Knepley @*/
1675ea844a1aSMatthew Knepley PetscErrorCode PetscSectionGetFieldPointOffset(PetscSection s, PetscInt point, PetscInt field, PetscInt *offset)
1676ea844a1aSMatthew Knepley {
1677ea844a1aSMatthew Knepley   PetscInt       off, foff;
1678ea844a1aSMatthew Knepley 
1679ea844a1aSMatthew Knepley   PetscFunctionBegin;
1680ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
16812abc8c78SJacob Faibussowitsch   PetscValidIntPointer(offset, 4);
16822abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field,s->numFields);
16835f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetOffset(s, point, &off));
16845f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetOffset(s->field[field], point, &foff));
1685ea844a1aSMatthew Knepley   *offset = foff - off;
1686ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
1687ea844a1aSMatthew Knepley }
1688ea844a1aSMatthew Knepley 
1689ea844a1aSMatthew Knepley /*@
1690ea844a1aSMatthew Knepley   PetscSectionGetOffsetRange - Return the full range of offsets [start, end)
1691ea844a1aSMatthew Knepley 
1692ea844a1aSMatthew Knepley   Not collective
1693ea844a1aSMatthew Knepley 
1694ea844a1aSMatthew Knepley   Input Parameter:
1695ea844a1aSMatthew Knepley . s - the PetscSection
1696ea844a1aSMatthew Knepley 
1697ea844a1aSMatthew Knepley   Output Parameters:
1698ea844a1aSMatthew Knepley + start - the minimum offset
1699ea844a1aSMatthew Knepley - end   - one more than the maximum offset
1700ea844a1aSMatthew Knepley 
1701ea844a1aSMatthew Knepley   Level: intermediate
1702ea844a1aSMatthew Knepley 
1703ea844a1aSMatthew Knepley .seealso: PetscSectionGetOffset(), PetscSectionCreate()
1704ea844a1aSMatthew Knepley @*/
1705ea844a1aSMatthew Knepley PetscErrorCode PetscSectionGetOffsetRange(PetscSection s, PetscInt *start, PetscInt *end)
1706ea844a1aSMatthew Knepley {
1707ea844a1aSMatthew Knepley   PetscInt       os = 0, oe = 0, pStart, pEnd, p;
1708ea844a1aSMatthew Knepley 
1709ea844a1aSMatthew Knepley   PetscFunctionBegin;
1710ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1711ea844a1aSMatthew Knepley   if (s->atlasOff) {os = s->atlasOff[0]; oe = s->atlasOff[0];}
17125f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetChart(s, &pStart, &pEnd));
1713ea844a1aSMatthew Knepley   for (p = 0; p < pEnd-pStart; ++p) {
1714ea844a1aSMatthew Knepley     PetscInt dof = s->atlasDof[p], off = s->atlasOff[p];
1715ea844a1aSMatthew Knepley 
1716ea844a1aSMatthew Knepley     if (off >= 0) {
1717ea844a1aSMatthew Knepley       os = PetscMin(os, off);
1718ea844a1aSMatthew Knepley       oe = PetscMax(oe, off+dof);
1719ea844a1aSMatthew Knepley     }
1720ea844a1aSMatthew Knepley   }
1721ea844a1aSMatthew Knepley   if (start) *start = os;
1722ea844a1aSMatthew Knepley   if (end)   *end   = oe;
1723ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
1724ea844a1aSMatthew Knepley }
1725ea844a1aSMatthew Knepley 
1726ea844a1aSMatthew Knepley /*@
1727ea844a1aSMatthew Knepley   PetscSectionCreateSubsection - Create a new, smaller section composed of only the selected fields
1728ea844a1aSMatthew Knepley 
1729ea844a1aSMatthew Knepley   Collective on s
1730ea844a1aSMatthew Knepley 
1731d8d19677SJose E. Roman   Input Parameters:
1732ea844a1aSMatthew Knepley + s      - the PetscSection
1733ea844a1aSMatthew Knepley . len    - the number of subfields
1734ea844a1aSMatthew Knepley - fields - the subfield numbers
1735ea844a1aSMatthew Knepley 
1736ea844a1aSMatthew Knepley   Output Parameter:
1737ea844a1aSMatthew Knepley . subs   - the subsection
1738ea844a1aSMatthew Knepley 
1739ea844a1aSMatthew Knepley   Note: The section offsets now refer to a new, smaller vector.
1740ea844a1aSMatthew Knepley 
1741ea844a1aSMatthew Knepley   Level: advanced
1742ea844a1aSMatthew Knepley 
1743ea844a1aSMatthew Knepley .seealso: PetscSectionCreateSupersection(), PetscSectionCreate()
1744ea844a1aSMatthew Knepley @*/
1745ea844a1aSMatthew Knepley PetscErrorCode PetscSectionCreateSubsection(PetscSection s, PetscInt len, const PetscInt fields[], PetscSection *subs)
1746ea844a1aSMatthew Knepley {
1747b778fa18SValeria Barra   PetscInt       nF, f, c, pStart, pEnd, p, maxCdof = 0;
1748ea844a1aSMatthew Knepley 
1749ea844a1aSMatthew Knepley   PetscFunctionBegin;
1750ea844a1aSMatthew Knepley   if (!len) PetscFunctionReturn(0);
1751ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1752ea844a1aSMatthew Knepley   PetscValidPointer(fields, 3);
1753ea844a1aSMatthew Knepley   PetscValidPointer(subs, 4);
17545f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetNumFields(s, &nF));
17552c71b3e2SJacob Faibussowitsch   PetscCheckFalse(len > nF,PetscObjectComm((PetscObject) s), PETSC_ERR_ARG_WRONG, "Number of requested fields %" PetscInt_FMT " greater than number of fields %" PetscInt_FMT, len, nF);
17565f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionCreate(PetscObjectComm((PetscObject) s), subs));
17575f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetNumFields(*subs, len));
1758ea844a1aSMatthew Knepley   for (f = 0; f < len; ++f) {
1759ea844a1aSMatthew Knepley     const char *name   = NULL;
1760ea844a1aSMatthew Knepley     PetscInt   numComp = 0;
1761ea844a1aSMatthew Knepley 
17625f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetFieldName(s, fields[f], &name));
17635f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetFieldName(*subs, f, name));
17645f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetFieldComponents(s, fields[f], &numComp));
17655f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetFieldComponents(*subs, f, numComp));
1766b778fa18SValeria Barra     for (c = 0; c < s->numFieldComponents[fields[f]]; ++c) {
17675f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetComponentName(s, fields[f], c, &name));
17685f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetComponentName(*subs, f, c, name));
1769b778fa18SValeria Barra     }
1770ea844a1aSMatthew Knepley   }
17715f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetChart(s, &pStart, &pEnd));
17725f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetChart(*subs, pStart, pEnd));
1773ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
1774ea844a1aSMatthew Knepley     PetscInt dof = 0, cdof = 0, fdof = 0, cfdof = 0;
1775ea844a1aSMatthew Knepley 
1776ea844a1aSMatthew Knepley     for (f = 0; f < len; ++f) {
17775f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetFieldDof(s, p, fields[f], &fdof));
17785f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetFieldDof(*subs, p, f, fdof));
17795f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetFieldConstraintDof(s, p, fields[f], &cfdof));
17805f80ce2aSJacob Faibussowitsch       if (cfdof) CHKERRQ(PetscSectionSetFieldConstraintDof(*subs, p, f, cfdof));
1781ea844a1aSMatthew Knepley       dof  += fdof;
1782ea844a1aSMatthew Knepley       cdof += cfdof;
1783ea844a1aSMatthew Knepley     }
17845f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetDof(*subs, p, dof));
17855f80ce2aSJacob Faibussowitsch     if (cdof) CHKERRQ(PetscSectionSetConstraintDof(*subs, p, cdof));
1786ea844a1aSMatthew Knepley     maxCdof = PetscMax(cdof, maxCdof);
1787ea844a1aSMatthew Knepley   }
17885f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetUp(*subs));
1789ea844a1aSMatthew Knepley   if (maxCdof) {
1790ea844a1aSMatthew Knepley     PetscInt *indices;
1791ea844a1aSMatthew Knepley 
17925f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(maxCdof, &indices));
1793ea844a1aSMatthew Knepley     for (p = pStart; p < pEnd; ++p) {
1794ea844a1aSMatthew Knepley       PetscInt cdof;
1795ea844a1aSMatthew Knepley 
17965f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetConstraintDof(*subs, p, &cdof));
1797ea844a1aSMatthew Knepley       if (cdof) {
1798ea844a1aSMatthew Knepley         const PetscInt *oldIndices = NULL;
1799ea844a1aSMatthew Knepley         PetscInt       fdof = 0, cfdof = 0, fc, numConst = 0, fOff = 0;
1800ea844a1aSMatthew Knepley 
1801ea844a1aSMatthew Knepley         for (f = 0; f < len; ++f) {
18025f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscSectionGetFieldDof(s, p, fields[f], &fdof));
18035f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscSectionGetFieldConstraintDof(s, p, fields[f], &cfdof));
18045f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscSectionGetFieldConstraintIndices(s, p, fields[f], &oldIndices));
18055f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscSectionSetFieldConstraintIndices(*subs, p, f, oldIndices));
18069574d0f0SMatthew G. Knepley           for (fc = 0; fc < cfdof; ++fc) indices[numConst+fc] = oldIndices[fc] + fOff;
1807ea844a1aSMatthew Knepley           numConst += cfdof;
1808ea844a1aSMatthew Knepley           fOff     += fdof;
1809ea844a1aSMatthew Knepley         }
18105f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionSetConstraintIndices(*subs, p, indices));
1811ea844a1aSMatthew Knepley       }
1812ea844a1aSMatthew Knepley     }
18135f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(indices));
1814ea844a1aSMatthew Knepley   }
1815ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
1816ea844a1aSMatthew Knepley }
1817ea844a1aSMatthew Knepley 
1818ea844a1aSMatthew Knepley /*@
1819ea844a1aSMatthew Knepley   PetscSectionCreateSupersection - Create a new, larger section composed of the input sections
1820ea844a1aSMatthew Knepley 
1821ea844a1aSMatthew Knepley   Collective on s
1822ea844a1aSMatthew Knepley 
1823ea844a1aSMatthew Knepley   Input Parameters:
1824ea844a1aSMatthew Knepley + s     - the input sections
1825ea844a1aSMatthew Knepley - len   - the number of input sections
1826ea844a1aSMatthew Knepley 
1827ea844a1aSMatthew Knepley   Output Parameter:
1828ea844a1aSMatthew Knepley . supers - the supersection
1829ea844a1aSMatthew Knepley 
1830ea844a1aSMatthew Knepley   Note: The section offsets now refer to a new, larger vector.
1831ea844a1aSMatthew Knepley 
1832ea844a1aSMatthew Knepley   Level: advanced
1833ea844a1aSMatthew Knepley 
1834ea844a1aSMatthew Knepley .seealso: PetscSectionCreateSubsection(), PetscSectionCreate()
1835ea844a1aSMatthew Knepley @*/
1836ea844a1aSMatthew Knepley PetscErrorCode PetscSectionCreateSupersection(PetscSection s[], PetscInt len, PetscSection *supers)
1837ea844a1aSMatthew Knepley {
1838ea844a1aSMatthew Knepley   PetscInt       Nf = 0, f, pStart = PETSC_MAX_INT, pEnd = 0, p, maxCdof = 0, i;
1839ea844a1aSMatthew Knepley 
1840ea844a1aSMatthew Knepley   PetscFunctionBegin;
1841ea844a1aSMatthew Knepley   if (!len) PetscFunctionReturn(0);
1842ea844a1aSMatthew Knepley   for (i = 0; i < len; ++i) {
1843ea844a1aSMatthew Knepley     PetscInt nf, pStarti, pEndi;
1844ea844a1aSMatthew Knepley 
18455f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetNumFields(s[i], &nf));
18465f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetChart(s[i], &pStarti, &pEndi));
1847ea844a1aSMatthew Knepley     pStart = PetscMin(pStart, pStarti);
1848ea844a1aSMatthew Knepley     pEnd   = PetscMax(pEnd,   pEndi);
1849ea844a1aSMatthew Knepley     Nf += nf;
1850ea844a1aSMatthew Knepley   }
18515f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionCreate(PetscObjectComm((PetscObject) s[0]), supers));
18525f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetNumFields(*supers, Nf));
1853ea844a1aSMatthew Knepley   for (i = 0, f = 0; i < len; ++i) {
1854b778fa18SValeria Barra     PetscInt nf, fi, ci;
1855ea844a1aSMatthew Knepley 
18565f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetNumFields(s[i], &nf));
1857ea844a1aSMatthew Knepley     for (fi = 0; fi < nf; ++fi, ++f) {
1858ea844a1aSMatthew Knepley       const char *name   = NULL;
1859ea844a1aSMatthew Knepley       PetscInt   numComp = 0;
1860ea844a1aSMatthew Knepley 
18615f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetFieldName(s[i], fi, &name));
18625f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetFieldName(*supers, f, name));
18635f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetFieldComponents(s[i], fi, &numComp));
18645f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetFieldComponents(*supers, f, numComp));
1865b778fa18SValeria Barra       for (ci = 0; ci < s[i]->numFieldComponents[fi]; ++ci) {
18665f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetComponentName(s[i], fi, ci, &name));
18675f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionSetComponentName(*supers, f, ci, name));
1868b778fa18SValeria Barra       }
1869ea844a1aSMatthew Knepley     }
1870ea844a1aSMatthew Knepley   }
18715f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetChart(*supers, pStart, pEnd));
1872ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
1873ea844a1aSMatthew Knepley     PetscInt dof = 0, cdof = 0;
1874ea844a1aSMatthew Knepley 
1875ea844a1aSMatthew Knepley     for (i = 0, f = 0; i < len; ++i) {
1876ea844a1aSMatthew Knepley       PetscInt nf, fi, pStarti, pEndi;
1877ea844a1aSMatthew Knepley       PetscInt fdof = 0, cfdof = 0;
1878ea844a1aSMatthew Knepley 
18795f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetNumFields(s[i], &nf));
18805f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetChart(s[i], &pStarti, &pEndi));
1881ea844a1aSMatthew Knepley       if ((p < pStarti) || (p >= pEndi)) continue;
1882ea844a1aSMatthew Knepley       for (fi = 0; fi < nf; ++fi, ++f) {
18835f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetFieldDof(s[i], p, fi, &fdof));
18845f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionAddFieldDof(*supers, p, f, fdof));
18855f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetFieldConstraintDof(s[i], p, fi, &cfdof));
18865f80ce2aSJacob Faibussowitsch         if (cfdof) CHKERRQ(PetscSectionAddFieldConstraintDof(*supers, p, f, cfdof));
1887ea844a1aSMatthew Knepley         dof  += fdof;
1888ea844a1aSMatthew Knepley         cdof += cfdof;
1889ea844a1aSMatthew Knepley       }
1890ea844a1aSMatthew Knepley     }
18915f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetDof(*supers, p, dof));
18925f80ce2aSJacob Faibussowitsch     if (cdof) CHKERRQ(PetscSectionSetConstraintDof(*supers, p, cdof));
1893ea844a1aSMatthew Knepley     maxCdof = PetscMax(cdof, maxCdof);
1894ea844a1aSMatthew Knepley   }
18955f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetUp(*supers));
1896ea844a1aSMatthew Knepley   if (maxCdof) {
1897ea844a1aSMatthew Knepley     PetscInt *indices;
1898ea844a1aSMatthew Knepley 
18995f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(maxCdof, &indices));
1900ea844a1aSMatthew Knepley     for (p = pStart; p < pEnd; ++p) {
1901ea844a1aSMatthew Knepley       PetscInt cdof;
1902ea844a1aSMatthew Knepley 
19035f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetConstraintDof(*supers, p, &cdof));
1904ea844a1aSMatthew Knepley       if (cdof) {
1905ea844a1aSMatthew Knepley         PetscInt dof, numConst = 0, fOff = 0;
1906ea844a1aSMatthew Knepley 
1907ea844a1aSMatthew Knepley         for (i = 0, f = 0; i < len; ++i) {
1908ea844a1aSMatthew Knepley           const PetscInt *oldIndices = NULL;
1909ea844a1aSMatthew Knepley           PetscInt        nf, fi, pStarti, pEndi, fdof, cfdof, fc;
1910ea844a1aSMatthew Knepley 
19115f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscSectionGetNumFields(s[i], &nf));
19125f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscSectionGetChart(s[i], &pStarti, &pEndi));
1913ea844a1aSMatthew Knepley           if ((p < pStarti) || (p >= pEndi)) continue;
1914ea844a1aSMatthew Knepley           for (fi = 0; fi < nf; ++fi, ++f) {
19155f80ce2aSJacob Faibussowitsch             CHKERRQ(PetscSectionGetFieldDof(s[i], p, fi, &fdof));
19165f80ce2aSJacob Faibussowitsch             CHKERRQ(PetscSectionGetFieldConstraintDof(s[i], p, fi, &cfdof));
19175f80ce2aSJacob Faibussowitsch             CHKERRQ(PetscSectionGetFieldConstraintIndices(s[i], p, fi, &oldIndices));
1918fcd2503dSMatthew G. Knepley             for (fc = 0; fc < cfdof; ++fc) indices[numConst+fc] = oldIndices[fc];
19195f80ce2aSJacob Faibussowitsch             CHKERRQ(PetscSectionSetFieldConstraintIndices(*supers, p, f, &indices[numConst]));
1920fcd2503dSMatthew G. Knepley             for (fc = 0; fc < cfdof; ++fc) indices[numConst+fc] += fOff;
1921ea844a1aSMatthew Knepley             numConst += cfdof;
1922ea844a1aSMatthew Knepley           }
19235f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscSectionGetDof(s[i], p, &dof));
1924ea844a1aSMatthew Knepley           fOff += dof;
1925ea844a1aSMatthew Knepley         }
19265f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionSetConstraintIndices(*supers, p, indices));
1927ea844a1aSMatthew Knepley       }
1928ea844a1aSMatthew Knepley     }
19295f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(indices));
1930ea844a1aSMatthew Knepley   }
1931ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
1932ea844a1aSMatthew Knepley }
1933ea844a1aSMatthew Knepley 
1934ea844a1aSMatthew Knepley /*@
1935ea844a1aSMatthew Knepley   PetscSectionCreateSubmeshSection - Create a new, smaller section with support on the submesh
1936ea844a1aSMatthew Knepley 
1937ea844a1aSMatthew Knepley   Collective on s
1938ea844a1aSMatthew Knepley 
1939ea844a1aSMatthew Knepley   Input Parameters:
1940ea844a1aSMatthew Knepley + s           - the PetscSection
1941ea844a1aSMatthew Knepley - subpointMap - a sorted list of points in the original mesh which are in the submesh
1942ea844a1aSMatthew Knepley 
1943ea844a1aSMatthew Knepley   Output Parameter:
1944ea844a1aSMatthew Knepley . subs - the subsection
1945ea844a1aSMatthew Knepley 
1946ea844a1aSMatthew Knepley   Note: The section offsets now refer to a new, smaller vector.
1947ea844a1aSMatthew Knepley 
1948ea844a1aSMatthew Knepley   Level: advanced
1949ea844a1aSMatthew Knepley 
1950ea844a1aSMatthew Knepley .seealso: PetscSectionCreateSubsection(), DMPlexGetSubpointMap(), PetscSectionCreate()
1951ea844a1aSMatthew Knepley @*/
1952ea844a1aSMatthew Knepley PetscErrorCode PetscSectionCreateSubmeshSection(PetscSection s, IS subpointMap, PetscSection *subs)
1953ea844a1aSMatthew Knepley {
1954ea844a1aSMatthew Knepley   const PetscInt *points = NULL, *indices = NULL;
1955b778fa18SValeria Barra   PetscInt       numFields, f, c, numSubpoints = 0, pStart, pEnd, p, subp;
1956ea844a1aSMatthew Knepley 
1957ea844a1aSMatthew Knepley   PetscFunctionBegin;
1958ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1959ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(subpointMap, IS_CLASSID, 2);
1960ea844a1aSMatthew Knepley   PetscValidPointer(subs, 3);
19615f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetNumFields(s, &numFields));
19625f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionCreate(PetscObjectComm((PetscObject) s), subs));
19635f80ce2aSJacob Faibussowitsch   if (numFields) CHKERRQ(PetscSectionSetNumFields(*subs, numFields));
1964ea844a1aSMatthew Knepley   for (f = 0; f < numFields; ++f) {
1965ea844a1aSMatthew Knepley     const char *name   = NULL;
1966ea844a1aSMatthew Knepley     PetscInt   numComp = 0;
1967ea844a1aSMatthew Knepley 
19685f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetFieldName(s, f, &name));
19695f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetFieldName(*subs, f, name));
19705f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetFieldComponents(s, f, &numComp));
19715f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetFieldComponents(*subs, f, numComp));
1972b778fa18SValeria Barra     for (c = 0; c < s->numFieldComponents[f]; ++c) {
19735f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetComponentName(s, f, c, &name));
19745f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetComponentName(*subs, f, c, name));
1975b778fa18SValeria Barra     }
1976ea844a1aSMatthew Knepley   }
1977ea844a1aSMatthew Knepley   /* For right now, we do not try to squeeze the subchart */
1978ea844a1aSMatthew Knepley   if (subpointMap) {
19795f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetSize(subpointMap, &numSubpoints));
19805f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetIndices(subpointMap, &points));
1981ea844a1aSMatthew Knepley   }
19825f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetChart(s, &pStart, &pEnd));
19835f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetChart(*subs, 0, numSubpoints));
1984ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
1985ea844a1aSMatthew Knepley     PetscInt dof, cdof, fdof = 0, cfdof = 0;
1986ea844a1aSMatthew Knepley 
19875f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFindInt(p, numSubpoints, points, &subp));
1988ea844a1aSMatthew Knepley     if (subp < 0) continue;
1989ea844a1aSMatthew Knepley     for (f = 0; f < numFields; ++f) {
19905f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetFieldDof(s, p, f, &fdof));
19915f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetFieldDof(*subs, subp, f, fdof));
19925f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetFieldConstraintDof(s, p, f, &cfdof));
19935f80ce2aSJacob Faibussowitsch       if (cfdof) CHKERRQ(PetscSectionSetFieldConstraintDof(*subs, subp, f, cfdof));
1994ea844a1aSMatthew Knepley     }
19955f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(s, p, &dof));
19965f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetDof(*subs, subp, dof));
19975f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetConstraintDof(s, p, &cdof));
19985f80ce2aSJacob Faibussowitsch     if (cdof) CHKERRQ(PetscSectionSetConstraintDof(*subs, subp, cdof));
1999ea844a1aSMatthew Knepley   }
20005f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetUp(*subs));
2001ea844a1aSMatthew Knepley   /* Change offsets to original offsets */
2002ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
2003ea844a1aSMatthew Knepley     PetscInt off, foff = 0;
2004ea844a1aSMatthew Knepley 
20055f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFindInt(p, numSubpoints, points, &subp));
2006ea844a1aSMatthew Knepley     if (subp < 0) continue;
2007ea844a1aSMatthew Knepley     for (f = 0; f < numFields; ++f) {
20085f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetFieldOffset(s, p, f, &foff));
20095f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetFieldOffset(*subs, subp, f, foff));
2010ea844a1aSMatthew Knepley     }
20115f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetOffset(s, p, &off));
20125f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetOffset(*subs, subp, off));
2013ea844a1aSMatthew Knepley   }
2014ea844a1aSMatthew Knepley   /* Copy constraint indices */
2015ea844a1aSMatthew Knepley   for (subp = 0; subp < numSubpoints; ++subp) {
2016ea844a1aSMatthew Knepley     PetscInt cdof;
2017ea844a1aSMatthew Knepley 
20185f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetConstraintDof(*subs, subp, &cdof));
2019ea844a1aSMatthew Knepley     if (cdof) {
2020ea844a1aSMatthew Knepley       for (f = 0; f < numFields; ++f) {
20215f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetFieldConstraintIndices(s, points[subp], f, &indices));
20225f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionSetFieldConstraintIndices(*subs, subp, f, indices));
2023ea844a1aSMatthew Knepley       }
20245f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetConstraintIndices(s, points[subp], &indices));
20255f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetConstraintIndices(*subs, subp, indices));
2026ea844a1aSMatthew Knepley     }
2027ea844a1aSMatthew Knepley   }
20285f80ce2aSJacob Faibussowitsch   if (subpointMap) CHKERRQ(ISRestoreIndices(subpointMap, &points));
2029ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
2030ea844a1aSMatthew Knepley }
2031ea844a1aSMatthew Knepley 
2032ea844a1aSMatthew Knepley static PetscErrorCode PetscSectionView_ASCII(PetscSection s, PetscViewer viewer)
2033ea844a1aSMatthew Knepley {
2034ea844a1aSMatthew Knepley   PetscInt       p;
2035ea844a1aSMatthew Knepley   PetscMPIInt    rank;
2036ea844a1aSMatthew Knepley 
2037ea844a1aSMatthew Knepley   PetscFunctionBegin;
20385f80ce2aSJacob Faibussowitsch   CHKERRMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank));
20395f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerASCIIPushSynchronized(viewer));
20405f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerASCIISynchronizedPrintf(viewer, "Process %d:\n", rank));
2041ea844a1aSMatthew Knepley   for (p = 0; p < s->pEnd - s->pStart; ++p) {
2042ea844a1aSMatthew Knepley     if ((s->bc) && (s->bc->atlasDof[p] > 0)) {
2043ea844a1aSMatthew Knepley       PetscInt b;
2044ea844a1aSMatthew Knepley 
20455f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscViewerASCIISynchronizedPrintf(viewer, "  (%4" PetscInt_FMT ") dim %2" PetscInt_FMT " offset %3" PetscInt_FMT " constrained", p+s->pStart, s->atlasDof[p], s->atlasOff[p]));
2046083401c6SMatthew G. Knepley       if (s->bcIndices) {
2047ea844a1aSMatthew Knepley         for (b = 0; b < s->bc->atlasDof[p]; ++b) {
20485f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscViewerASCIISynchronizedPrintf(viewer, " %" PetscInt_FMT, s->bcIndices[s->bc->atlasOff[p]+b]));
2049ea844a1aSMatthew Knepley         }
2050083401c6SMatthew G. Knepley       }
20515f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscViewerASCIISynchronizedPrintf(viewer, "\n"));
2052ea844a1aSMatthew Knepley     } else {
20535f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscViewerASCIISynchronizedPrintf(viewer, "  (%4" PetscInt_FMT ") dim %2" PetscInt_FMT " offset %3" PetscInt_FMT "\n", p+s->pStart, s->atlasDof[p], s->atlasOff[p]));
2054ea844a1aSMatthew Knepley     }
2055ea844a1aSMatthew Knepley   }
20565f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerFlush(viewer));
20575f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerASCIIPopSynchronized(viewer));
2058ea844a1aSMatthew Knepley   if (s->sym) {
20595f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscViewerASCIIPushTab(viewer));
20605f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSymView(s->sym,viewer));
20615f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscViewerASCIIPopTab(viewer));
2062ea844a1aSMatthew Knepley   }
2063ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
2064ea844a1aSMatthew Knepley }
2065ea844a1aSMatthew Knepley 
2066ea844a1aSMatthew Knepley /*@C
2067fe2efc57SMark    PetscSectionViewFromOptions - View from Options
2068fe2efc57SMark 
2069fe2efc57SMark    Collective on PetscSection
2070fe2efc57SMark 
2071fe2efc57SMark    Input Parameters:
2072fe2efc57SMark +  A - the PetscSection object to view
2073736c3998SJose E. Roman .  obj - Optional object
2074736c3998SJose E. Roman -  name - command line option
2075fe2efc57SMark 
2076fe2efc57SMark    Level: intermediate
2077fe2efc57SMark .seealso:  PetscSection, PetscSectionView, PetscObjectViewFromOptions(), PetscSectionCreate()
2078fe2efc57SMark @*/
2079fe2efc57SMark PetscErrorCode  PetscSectionViewFromOptions(PetscSection A,PetscObject obj,const char name[])
2080fe2efc57SMark {
2081fe2efc57SMark   PetscFunctionBegin;
2082fe2efc57SMark   PetscValidHeaderSpecific(A,PETSC_SECTION_CLASSID,1);
20835f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectViewFromOptions((PetscObject)A,obj,name));
2084fe2efc57SMark   PetscFunctionReturn(0);
2085fe2efc57SMark }
2086fe2efc57SMark 
2087fe2efc57SMark /*@C
2088ea844a1aSMatthew Knepley   PetscSectionView - Views a PetscSection
2089ea844a1aSMatthew Knepley 
2090ea844a1aSMatthew Knepley   Collective on PetscSection
2091ea844a1aSMatthew Knepley 
2092ea844a1aSMatthew Knepley   Input Parameters:
2093ea844a1aSMatthew Knepley + s - the PetscSection object to view
2094ea844a1aSMatthew Knepley - v - the viewer
2095ea844a1aSMatthew Knepley 
20961ddd528eSksagiyam   Note:
20971ddd528eSksagiyam   PetscSectionView(), when viewer is of type PETSCVIEWERHDF5, only saves
20981ddd528eSksagiyam   distribution independent data, such as dofs, offsets, constraint dofs,
20991ddd528eSksagiyam   and constraint indices. Points that have negative dofs, for instance,
21001ddd528eSksagiyam   are not saved as they represent points owned by other processes.
21011ddd528eSksagiyam   Point numbering and rank assignment is currently not stored.
21021ddd528eSksagiyam   The saved section can be loaded with PetscSectionLoad().
21031ddd528eSksagiyam 
2104ea844a1aSMatthew Knepley   Level: beginner
2105ea844a1aSMatthew Knepley 
2106fde5e3acSksagiyam .seealso PetscSectionCreate(), PetscSectionDestroy(), PetscSectionLoad()
2107ea844a1aSMatthew Knepley @*/
2108ea844a1aSMatthew Knepley PetscErrorCode PetscSectionView(PetscSection s, PetscViewer viewer)
2109ea844a1aSMatthew Knepley {
21101ddd528eSksagiyam   PetscBool      isascii, ishdf5;
2111ea844a1aSMatthew Knepley   PetscInt       f;
2112ea844a1aSMatthew Knepley 
2113ea844a1aSMatthew Knepley   PetscFunctionBegin;
2114ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
21155f80ce2aSJacob Faibussowitsch   if (!viewer) CHKERRQ(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)s), &viewer));
2116ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
21175f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &isascii));
21185f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5));
2119ea844a1aSMatthew Knepley   if (isascii) {
21205f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectPrintClassNamePrefixType((PetscObject)s,viewer));
2121ea844a1aSMatthew Knepley     if (s->numFields) {
21225f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " fields\n", s->numFields));
2123ea844a1aSMatthew Knepley       for (f = 0; f < s->numFields; ++f) {
21245f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscViewerASCIIPrintf(viewer, "  field %" PetscInt_FMT " with %" PetscInt_FMT " components\n", f, s->numFieldComponents[f]));
21255f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionView_ASCII(s->field[f], viewer));
2126ea844a1aSMatthew Knepley       }
2127ea844a1aSMatthew Knepley     } else {
21285f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionView_ASCII(s, viewer));
2129ea844a1aSMatthew Knepley     }
21301ddd528eSksagiyam   } else if (ishdf5) {
21311ddd528eSksagiyam #if PetscDefined(HAVE_HDF5)
21325f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionView_HDF5_Internal(s, viewer));
21331ddd528eSksagiyam #else
21341ddd528eSksagiyam     SETERRQ(PetscObjectComm((PetscObject) s), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
21351ddd528eSksagiyam #endif
2136ea844a1aSMatthew Knepley   }
2137ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
2138ea844a1aSMatthew Knepley }
2139ea844a1aSMatthew Knepley 
2140fde5e3acSksagiyam /*@C
2141fde5e3acSksagiyam   PetscSectionLoad - Loads a PetscSection
2142fde5e3acSksagiyam 
2143fde5e3acSksagiyam   Collective on PetscSection
2144fde5e3acSksagiyam 
2145fde5e3acSksagiyam   Input Parameters:
2146fde5e3acSksagiyam + s - the PetscSection object to load
2147fde5e3acSksagiyam - v - the viewer
2148fde5e3acSksagiyam 
2149fde5e3acSksagiyam   Note:
2150fde5e3acSksagiyam   PetscSectionLoad(), when viewer is of type PETSCVIEWERHDF5, loads
2151fde5e3acSksagiyam   a section saved with PetscSectionView(). The number of processes
2152fde5e3acSksagiyam   used here (N) does not need to be the same as that used when saving.
2153fde5e3acSksagiyam   After calling this function, the chart of s on rank i will be set
2154fde5e3acSksagiyam   to [0, E_i), where \sum_{i=0}^{N-1}E_i equals to the total number of
2155fde5e3acSksagiyam   saved section points.
2156fde5e3acSksagiyam 
2157fde5e3acSksagiyam   Level: beginner
2158fde5e3acSksagiyam 
2159fde5e3acSksagiyam .seealso PetscSectionCreate(), PetscSectionDestroy(), PetscSectionView()
2160fde5e3acSksagiyam @*/
2161fde5e3acSksagiyam PetscErrorCode PetscSectionLoad(PetscSection s, PetscViewer viewer)
2162fde5e3acSksagiyam {
2163fde5e3acSksagiyam   PetscBool      ishdf5;
2164fde5e3acSksagiyam 
2165fde5e3acSksagiyam   PetscFunctionBegin;
2166fde5e3acSksagiyam   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2167fde5e3acSksagiyam   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
21685f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5));
2169fde5e3acSksagiyam   if (ishdf5) {
2170fde5e3acSksagiyam #if PetscDefined(HAVE_HDF5)
21715f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionLoad_HDF5_Internal(s, viewer));
2172fde5e3acSksagiyam     PetscFunctionReturn(0);
2173fde5e3acSksagiyam #else
2174fde5e3acSksagiyam     SETERRQ(PetscObjectComm((PetscObject) s), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2175fde5e3acSksagiyam #endif
217698921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject) s), PETSC_ERR_SUP, "Viewer type %s not yet supported for PetscSection loading", ((PetscObject)viewer)->type_name);
2177fde5e3acSksagiyam }
2178fde5e3acSksagiyam 
2179c459fbc1SJed Brown static PetscErrorCode PetscSectionResetClosurePermutation(PetscSection section)
2180c459fbc1SJed Brown {
2181c459fbc1SJed Brown   PetscSectionClosurePermVal clVal;
2182c459fbc1SJed Brown 
2183c459fbc1SJed Brown   PetscFunctionBegin;
2184c459fbc1SJed Brown   if (!section->clHash) PetscFunctionReturn(0);
2185c459fbc1SJed Brown   kh_foreach_value(section->clHash, clVal, {
21865f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree(clVal.perm));
21875f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree(clVal.invPerm));
2188c459fbc1SJed Brown     });
2189c459fbc1SJed Brown   kh_destroy(ClPerm, section->clHash);
2190c459fbc1SJed Brown   section->clHash = NULL;
2191c459fbc1SJed Brown   PetscFunctionReturn(0);
2192c459fbc1SJed Brown }
2193c459fbc1SJed Brown 
2194ea844a1aSMatthew Knepley /*@
2195ea844a1aSMatthew Knepley   PetscSectionReset - Frees all section data.
2196ea844a1aSMatthew Knepley 
2197ea844a1aSMatthew Knepley   Not collective
2198ea844a1aSMatthew Knepley 
2199ea844a1aSMatthew Knepley   Input Parameters:
2200ea844a1aSMatthew Knepley . s - the PetscSection
2201ea844a1aSMatthew Knepley 
2202ea844a1aSMatthew Knepley   Level: beginner
2203ea844a1aSMatthew Knepley 
2204ea844a1aSMatthew Knepley .seealso: PetscSection, PetscSectionCreate()
2205ea844a1aSMatthew Knepley @*/
2206ea844a1aSMatthew Knepley PetscErrorCode PetscSectionReset(PetscSection s)
2207ea844a1aSMatthew Knepley {
2208b778fa18SValeria Barra   PetscInt       f, c;
2209ea844a1aSMatthew Knepley 
2210ea844a1aSMatthew Knepley   PetscFunctionBegin;
2211ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2212ea844a1aSMatthew Knepley   for (f = 0; f < s->numFields; ++f) {
22135f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionDestroy(&s->field[f]));
22145f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(s->fieldNames[f]));
2215b146d1acSSatish Balay     for (c = 0; c < s->numFieldComponents[f]; ++c) {
22165f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree(s->compNames[f][c]));
2217b146d1acSSatish Balay     }
22185f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(s->compNames[f]));
2219ea844a1aSMatthew Knepley   }
22205f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(s->numFieldComponents));
22215f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(s->fieldNames));
22225f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(s->compNames));
22235f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(s->field));
22245f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionDestroy(&s->bc));
22255f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(s->bcIndices));
22265f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree2(s->atlasDof, s->atlasOff));
22275f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionDestroy(&s->clSection));
22285f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&s->clPoints));
22295f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&s->perm));
22305f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionResetClosurePermutation(s));
22315f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSymDestroy(&s->sym));
22325f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionDestroy(&s->clSection));
22335f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&s->clPoints));
2234ea844a1aSMatthew Knepley 
2235ea844a1aSMatthew Knepley   s->pStart    = -1;
2236ea844a1aSMatthew Knepley   s->pEnd      = -1;
2237ea844a1aSMatthew Knepley   s->maxDof    = 0;
2238ea844a1aSMatthew Knepley   s->setup     = PETSC_FALSE;
2239ea844a1aSMatthew Knepley   s->numFields = 0;
2240ea844a1aSMatthew Knepley   s->clObj     = NULL;
2241ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
2242ea844a1aSMatthew Knepley }
2243ea844a1aSMatthew Knepley 
2244ea844a1aSMatthew Knepley /*@
2245ea844a1aSMatthew Knepley   PetscSectionDestroy - Frees a section object and frees its range if that exists.
2246ea844a1aSMatthew Knepley 
2247ea844a1aSMatthew Knepley   Not collective
2248ea844a1aSMatthew Knepley 
2249ea844a1aSMatthew Knepley   Input Parameters:
2250ea844a1aSMatthew Knepley . s - the PetscSection
2251ea844a1aSMatthew Knepley 
2252ea844a1aSMatthew Knepley   Level: beginner
2253ea844a1aSMatthew Knepley 
2254ea844a1aSMatthew Knepley .seealso: PetscSection, PetscSectionCreate()
2255ea844a1aSMatthew Knepley @*/
2256ea844a1aSMatthew Knepley PetscErrorCode PetscSectionDestroy(PetscSection *s)
2257ea844a1aSMatthew Knepley {
2258ea844a1aSMatthew Knepley   PetscFunctionBegin;
2259ea844a1aSMatthew Knepley   if (!*s) PetscFunctionReturn(0);
226062f17a49SStefano Zampini   PetscValidHeaderSpecific(*s,PETSC_SECTION_CLASSID,1);
2261ea844a1aSMatthew Knepley   if (--((PetscObject)(*s))->refct > 0) {
2262ea844a1aSMatthew Knepley     *s = NULL;
2263ea844a1aSMatthew Knepley     PetscFunctionReturn(0);
2264ea844a1aSMatthew Knepley   }
22655f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionReset(*s));
22665f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHeaderDestroy(s));
2267ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
2268ea844a1aSMatthew Knepley }
2269ea844a1aSMatthew Knepley 
2270ea844a1aSMatthew Knepley PetscErrorCode VecIntGetValuesSection(PetscInt *baseArray, PetscSection s, PetscInt point, const PetscInt **values)
2271ea844a1aSMatthew Knepley {
2272ea844a1aSMatthew Knepley   const PetscInt p = point - s->pStart;
2273ea844a1aSMatthew Knepley 
2274ea844a1aSMatthew Knepley   PetscFunctionBegin;
2275ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 2);
2276ea844a1aSMatthew Knepley   *values = &baseArray[s->atlasOff[p]];
2277ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
2278ea844a1aSMatthew Knepley }
2279ea844a1aSMatthew Knepley 
2280ea844a1aSMatthew Knepley PetscErrorCode VecIntSetValuesSection(PetscInt *baseArray, PetscSection s, PetscInt point, const PetscInt values[], InsertMode mode)
2281ea844a1aSMatthew Knepley {
2282ea844a1aSMatthew Knepley   PetscInt       *array;
2283ea844a1aSMatthew Knepley   const PetscInt p           = point - s->pStart;
2284ea844a1aSMatthew Knepley   const PetscInt orientation = 0; /* Needs to be included for use in closure operations */
2285ea844a1aSMatthew Knepley   PetscInt       cDim        = 0;
2286ea844a1aSMatthew Knepley 
2287ea844a1aSMatthew Knepley   PetscFunctionBegin;
2288ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 2);
22895f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetConstraintDof(s, p, &cDim));
2290ea844a1aSMatthew Knepley   array = &baseArray[s->atlasOff[p]];
2291ea844a1aSMatthew Knepley   if (!cDim) {
2292ea844a1aSMatthew Knepley     if (orientation >= 0) {
2293ea844a1aSMatthew Knepley       const PetscInt dim = s->atlasDof[p];
2294ea844a1aSMatthew Knepley       PetscInt       i;
2295ea844a1aSMatthew Knepley 
2296ea844a1aSMatthew Knepley       if (mode == INSERT_VALUES) {
2297ea844a1aSMatthew Knepley         for (i = 0; i < dim; ++i) array[i] = values[i];
2298ea844a1aSMatthew Knepley       } else {
2299ea844a1aSMatthew Knepley         for (i = 0; i < dim; ++i) array[i] += values[i];
2300ea844a1aSMatthew Knepley       }
2301ea844a1aSMatthew Knepley     } else {
2302ea844a1aSMatthew Knepley       PetscInt offset = 0;
2303ea844a1aSMatthew Knepley       PetscInt j      = -1, field, i;
2304ea844a1aSMatthew Knepley 
2305ea844a1aSMatthew Knepley       for (field = 0; field < s->numFields; ++field) {
2306ea844a1aSMatthew Knepley         const PetscInt dim = s->field[field]->atlasDof[p];
2307ea844a1aSMatthew Knepley 
2308ea844a1aSMatthew Knepley         for (i = dim-1; i >= 0; --i) array[++j] = values[i+offset];
2309ea844a1aSMatthew Knepley         offset += dim;
2310ea844a1aSMatthew Knepley       }
2311ea844a1aSMatthew Knepley     }
2312ea844a1aSMatthew Knepley   } else {
2313ea844a1aSMatthew Knepley     if (orientation >= 0) {
2314ea844a1aSMatthew Knepley       const PetscInt dim  = s->atlasDof[p];
2315ea844a1aSMatthew Knepley       PetscInt       cInd = 0, i;
2316ea844a1aSMatthew Knepley       const PetscInt *cDof;
2317ea844a1aSMatthew Knepley 
23185f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetConstraintIndices(s, point, &cDof));
2319ea844a1aSMatthew Knepley       if (mode == INSERT_VALUES) {
2320ea844a1aSMatthew Knepley         for (i = 0; i < dim; ++i) {
2321ea844a1aSMatthew Knepley           if ((cInd < cDim) && (i == cDof[cInd])) {++cInd; continue;}
2322ea844a1aSMatthew Knepley           array[i] = values[i];
2323ea844a1aSMatthew Knepley         }
2324ea844a1aSMatthew Knepley       } else {
2325ea844a1aSMatthew Knepley         for (i = 0; i < dim; ++i) {
2326ea844a1aSMatthew Knepley           if ((cInd < cDim) && (i == cDof[cInd])) {++cInd; continue;}
2327ea844a1aSMatthew Knepley           array[i] += values[i];
2328ea844a1aSMatthew Knepley         }
2329ea844a1aSMatthew Knepley       }
2330ea844a1aSMatthew Knepley     } else {
2331ea844a1aSMatthew Knepley       const PetscInt *cDof;
2332ea844a1aSMatthew Knepley       PetscInt       offset  = 0;
2333ea844a1aSMatthew Knepley       PetscInt       cOffset = 0;
2334ea844a1aSMatthew Knepley       PetscInt       j       = 0, field;
2335ea844a1aSMatthew Knepley 
23365f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetConstraintIndices(s, point, &cDof));
2337ea844a1aSMatthew Knepley       for (field = 0; field < s->numFields; ++field) {
2338ea844a1aSMatthew Knepley         const PetscInt dim  = s->field[field]->atlasDof[p];     /* PetscSectionGetFieldDof() */
2339ea844a1aSMatthew Knepley         const PetscInt tDim = s->field[field]->bc->atlasDof[p]; /* PetscSectionGetFieldConstraintDof() */
2340ea844a1aSMatthew Knepley         const PetscInt sDim = dim - tDim;
2341ea844a1aSMatthew Knepley         PetscInt       cInd = 0, i,k;
2342ea844a1aSMatthew Knepley 
2343ea844a1aSMatthew Knepley         for (i = 0, k = dim+offset-1; i < dim; ++i, ++j, --k) {
2344ea844a1aSMatthew Knepley           if ((cInd < sDim) && (j == cDof[cInd+cOffset])) {++cInd; continue;}
2345ea844a1aSMatthew Knepley           array[j] = values[k];
2346ea844a1aSMatthew Knepley         }
2347ea844a1aSMatthew Knepley         offset  += dim;
2348ea844a1aSMatthew Knepley         cOffset += dim - tDim;
2349ea844a1aSMatthew Knepley       }
2350ea844a1aSMatthew Knepley     }
2351ea844a1aSMatthew Knepley   }
2352ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
2353ea844a1aSMatthew Knepley }
2354ea844a1aSMatthew Knepley 
2355ea844a1aSMatthew Knepley /*@C
2356ea844a1aSMatthew Knepley   PetscSectionHasConstraints - Determine whether a section has constrained dofs
2357ea844a1aSMatthew Knepley 
2358ea844a1aSMatthew Knepley   Not collective
2359ea844a1aSMatthew Knepley 
2360ea844a1aSMatthew Knepley   Input Parameter:
2361ea844a1aSMatthew Knepley . s - The PetscSection
2362ea844a1aSMatthew Knepley 
2363ea844a1aSMatthew Knepley   Output Parameter:
2364ea844a1aSMatthew Knepley . hasConstraints - flag indicating that the section has constrained dofs
2365ea844a1aSMatthew Knepley 
2366ea844a1aSMatthew Knepley   Level: intermediate
2367ea844a1aSMatthew Knepley 
2368ea844a1aSMatthew Knepley .seealso: PetscSectionSetConstraintIndices(), PetscSectionGetConstraintDof(), PetscSection
2369ea844a1aSMatthew Knepley @*/
2370ea844a1aSMatthew Knepley PetscErrorCode PetscSectionHasConstraints(PetscSection s, PetscBool *hasConstraints)
2371ea844a1aSMatthew Knepley {
2372ea844a1aSMatthew Knepley   PetscFunctionBegin;
2373ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2374ea844a1aSMatthew Knepley   PetscValidPointer(hasConstraints, 2);
2375ea844a1aSMatthew Knepley   *hasConstraints = s->bc ? PETSC_TRUE : PETSC_FALSE;
2376ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
2377ea844a1aSMatthew Knepley }
2378ea844a1aSMatthew Knepley 
2379ea844a1aSMatthew Knepley /*@C
2380ea844a1aSMatthew Knepley   PetscSectionGetConstraintIndices - Get the point dof numbers, in [0, dof), which are constrained
2381ea844a1aSMatthew Knepley 
2382ea844a1aSMatthew Knepley   Not collective
2383ea844a1aSMatthew Knepley 
2384ea844a1aSMatthew Knepley   Input Parameters:
2385ea844a1aSMatthew Knepley + s     - The PetscSection
2386ea844a1aSMatthew Knepley - point - The point
2387ea844a1aSMatthew Knepley 
2388ea844a1aSMatthew Knepley   Output Parameter:
2389ea844a1aSMatthew Knepley . indices - The constrained dofs
2390ea844a1aSMatthew Knepley 
2391ea844a1aSMatthew Knepley   Note: In Fortran, you call PetscSectionGetConstraintIndicesF90() and PetscSectionRestoreConstraintIndicesF90()
2392ea844a1aSMatthew Knepley 
2393ea844a1aSMatthew Knepley   Level: intermediate
2394ea844a1aSMatthew Knepley 
2395ea844a1aSMatthew Knepley .seealso: PetscSectionSetConstraintIndices(), PetscSectionGetConstraintDof(), PetscSection
2396ea844a1aSMatthew Knepley @*/
2397ea844a1aSMatthew Knepley PetscErrorCode PetscSectionGetConstraintIndices(PetscSection s, PetscInt point, const PetscInt **indices)
2398ea844a1aSMatthew Knepley {
2399ea844a1aSMatthew Knepley   PetscFunctionBegin;
2400ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2401ea844a1aSMatthew Knepley   if (s->bc) {
24025f80ce2aSJacob Faibussowitsch     CHKERRQ(VecIntGetValuesSection(s->bcIndices, s->bc, point, indices));
2403ea844a1aSMatthew Knepley   } else *indices = NULL;
2404ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
2405ea844a1aSMatthew Knepley }
2406ea844a1aSMatthew Knepley 
2407ea844a1aSMatthew Knepley /*@C
2408ea844a1aSMatthew Knepley   PetscSectionSetConstraintIndices - Set the point dof numbers, in [0, dof), which are constrained
2409ea844a1aSMatthew Knepley 
2410ea844a1aSMatthew Knepley   Not collective
2411ea844a1aSMatthew Knepley 
2412ea844a1aSMatthew Knepley   Input Parameters:
2413ea844a1aSMatthew Knepley + s     - The PetscSection
2414ea844a1aSMatthew Knepley . point - The point
2415ea844a1aSMatthew Knepley - indices - The constrained dofs
2416ea844a1aSMatthew Knepley 
2417ea844a1aSMatthew Knepley   Note: The Fortran is PetscSectionSetConstraintIndicesF90()
2418ea844a1aSMatthew Knepley 
2419ea844a1aSMatthew Knepley   Level: intermediate
2420ea844a1aSMatthew Knepley 
2421ea844a1aSMatthew Knepley .seealso: PetscSectionGetConstraintIndices(), PetscSectionGetConstraintDof(), PetscSection
2422ea844a1aSMatthew Knepley @*/
2423ea844a1aSMatthew Knepley PetscErrorCode PetscSectionSetConstraintIndices(PetscSection s, PetscInt point, const PetscInt indices[])
2424ea844a1aSMatthew Knepley {
2425ea844a1aSMatthew Knepley   PetscFunctionBegin;
2426ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2427ea844a1aSMatthew Knepley   if (s->bc) {
2428d57bb9dbSMatthew G. Knepley     const PetscInt dof  = s->atlasDof[point];
2429d57bb9dbSMatthew G. Knepley     const PetscInt cdof = s->bc->atlasDof[point];
2430d57bb9dbSMatthew G. Knepley     PetscInt       d;
2431d57bb9dbSMatthew G. Knepley 
2432d57bb9dbSMatthew G. Knepley     for (d = 0; d < cdof; ++d) {
2433e2bfaee7SMatthew G. Knepley       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]);
2434d57bb9dbSMatthew G. Knepley     }
24355f80ce2aSJacob Faibussowitsch     CHKERRQ(VecIntSetValuesSection(s->bcIndices, s->bc, point, indices, INSERT_VALUES));
2436ea844a1aSMatthew Knepley   }
2437ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
2438ea844a1aSMatthew Knepley }
2439ea844a1aSMatthew Knepley 
2440ea844a1aSMatthew Knepley /*@C
2441ea844a1aSMatthew Knepley   PetscSectionGetFieldConstraintIndices - Get the field dof numbers, in [0, fdof), which are constrained
2442ea844a1aSMatthew Knepley 
2443ea844a1aSMatthew Knepley   Not collective
2444ea844a1aSMatthew Knepley 
2445ea844a1aSMatthew Knepley   Input Parameters:
2446ea844a1aSMatthew Knepley + s     - The PetscSection
2447ea844a1aSMatthew Knepley . field  - The field number
2448ea844a1aSMatthew Knepley - point - The point
2449ea844a1aSMatthew Knepley 
2450ea844a1aSMatthew Knepley   Output Parameter:
24519759eb26SJed Brown . indices - The constrained dofs sorted in ascending order
2452ea844a1aSMatthew Knepley 
24539759eb26SJed Brown   Notes:
24549759eb26SJed Brown   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().
24559759eb26SJed Brown 
24569759eb26SJed Brown   Fortran Note:
24579759eb26SJed Brown   In Fortran, you call PetscSectionGetFieldConstraintIndicesF90() and PetscSectionRestoreFieldConstraintIndicesF90()
2458ea844a1aSMatthew Knepley 
2459ea844a1aSMatthew Knepley   Level: intermediate
2460ea844a1aSMatthew Knepley 
2461ea844a1aSMatthew Knepley .seealso: PetscSectionSetFieldConstraintIndices(), PetscSectionGetConstraintIndices(), PetscSectionGetConstraintDof(), PetscSection
2462ea844a1aSMatthew Knepley @*/
2463ea844a1aSMatthew Knepley PetscErrorCode PetscSectionGetFieldConstraintIndices(PetscSection s, PetscInt point, PetscInt field, const PetscInt **indices)
2464ea844a1aSMatthew Knepley {
2465ea844a1aSMatthew Knepley   PetscFunctionBegin;
2466ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
24672abc8c78SJacob Faibussowitsch   PetscValidIntPointer(indices,4);
24682abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field,s->numFields);
24695f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetConstraintIndices(s->field[field], point, indices));
2470ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
2471ea844a1aSMatthew Knepley }
2472ea844a1aSMatthew Knepley 
2473ea844a1aSMatthew Knepley /*@C
2474ea844a1aSMatthew Knepley   PetscSectionSetFieldConstraintIndices - Set the field dof numbers, in [0, fdof), which are constrained
2475ea844a1aSMatthew Knepley 
2476ea844a1aSMatthew Knepley   Not collective
2477ea844a1aSMatthew Knepley 
2478ea844a1aSMatthew Knepley   Input Parameters:
2479ea844a1aSMatthew Knepley + s       - The PetscSection
2480ea844a1aSMatthew Knepley . point   - The point
2481ea844a1aSMatthew Knepley . field   - The field number
2482ea844a1aSMatthew Knepley - indices - The constrained dofs
2483ea844a1aSMatthew Knepley 
2484ea844a1aSMatthew Knepley   Note: The Fortran is PetscSectionSetFieldConstraintIndicesF90()
2485ea844a1aSMatthew Knepley 
2486ea844a1aSMatthew Knepley   Level: intermediate
2487ea844a1aSMatthew Knepley 
2488ea844a1aSMatthew Knepley .seealso: PetscSectionSetConstraintIndices(), PetscSectionGetFieldConstraintIndices(), PetscSectionGetConstraintDof(), PetscSection
2489ea844a1aSMatthew Knepley @*/
2490ea844a1aSMatthew Knepley PetscErrorCode PetscSectionSetFieldConstraintIndices(PetscSection s, PetscInt point, PetscInt field, const PetscInt indices[])
2491ea844a1aSMatthew Knepley {
2492ea844a1aSMatthew Knepley   PetscFunctionBegin;
2493ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
24942abc8c78SJacob Faibussowitsch   if (PetscDefined(USE_DEBUG)) {
2495e2bfaee7SMatthew G. Knepley     PetscInt nfdof;
24962abc8c78SJacob Faibussowitsch 
24975f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetFieldConstraintDof(s, point, field, &nfdof));
2498e2bfaee7SMatthew G. Knepley     if (nfdof) PetscValidIntPointer(indices, 4);
24992abc8c78SJacob Faibussowitsch   }
25002abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field,s->numFields);
25015f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetConstraintIndices(s->field[field], point, indices));
2502ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
2503ea844a1aSMatthew Knepley }
2504ea844a1aSMatthew Knepley 
2505ea844a1aSMatthew Knepley /*@
2506ea844a1aSMatthew Knepley   PetscSectionPermute - Reorder the section according to the input point permutation
2507ea844a1aSMatthew Knepley 
2508ea844a1aSMatthew Knepley   Collective on PetscSection
2509ea844a1aSMatthew Knepley 
2510d8d19677SJose E. Roman   Input Parameters:
2511ea844a1aSMatthew Knepley + section - The PetscSection object
2512ea844a1aSMatthew Knepley - perm - The point permutation, old point p becomes new point perm[p]
2513ea844a1aSMatthew Knepley 
2514ea844a1aSMatthew Knepley   Output Parameter:
2515ea844a1aSMatthew Knepley . sectionNew - The permuted PetscSection
2516ea844a1aSMatthew Knepley 
2517ea844a1aSMatthew Knepley   Level: intermediate
2518ea844a1aSMatthew Knepley 
2519ea844a1aSMatthew Knepley .seealso: MatPermute()
2520ea844a1aSMatthew Knepley @*/
2521ea844a1aSMatthew Knepley PetscErrorCode PetscSectionPermute(PetscSection section, IS permutation, PetscSection *sectionNew)
2522ea844a1aSMatthew Knepley {
2523ea844a1aSMatthew Knepley   PetscSection    s = section, sNew;
2524ea844a1aSMatthew Knepley   const PetscInt *perm;
2525b778fa18SValeria Barra   PetscInt        numFields, f, c, numPoints, pStart, pEnd, p;
2526ea844a1aSMatthew Knepley 
2527ea844a1aSMatthew Knepley   PetscFunctionBegin;
2528ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 1);
2529ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(permutation, IS_CLASSID, 2);
2530ea844a1aSMatthew Knepley   PetscValidPointer(sectionNew, 3);
25315f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionCreate(PetscObjectComm((PetscObject) s), &sNew));
25325f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetNumFields(s, &numFields));
25335f80ce2aSJacob Faibussowitsch   if (numFields) CHKERRQ(PetscSectionSetNumFields(sNew, numFields));
2534ea844a1aSMatthew Knepley   for (f = 0; f < numFields; ++f) {
2535ea844a1aSMatthew Knepley     const char *name;
2536ea844a1aSMatthew Knepley     PetscInt    numComp;
2537ea844a1aSMatthew Knepley 
25385f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetFieldName(s, f, &name));
25395f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetFieldName(sNew, f, name));
25405f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetFieldComponents(s, f, &numComp));
25415f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetFieldComponents(sNew, f, numComp));
2542b778fa18SValeria Barra     for (c = 0; c < s->numFieldComponents[f]; ++c) {
25435f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetComponentName(s, f, c, &name));
25445f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetComponentName(sNew, f, c, name));
2545b778fa18SValeria Barra     }
2546ea844a1aSMatthew Knepley   }
25475f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetLocalSize(permutation, &numPoints));
25485f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetIndices(permutation, &perm));
25495f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetChart(s, &pStart, &pEnd));
25505f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetChart(sNew, pStart, pEnd));
25512c71b3e2SJacob Faibussowitsch   PetscCheckFalse(numPoints < pEnd,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Permutation size %" PetscInt_FMT " is less than largest Section point %" PetscInt_FMT, numPoints, pEnd);
2552ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
2553ea844a1aSMatthew Knepley     PetscInt dof, cdof;
2554ea844a1aSMatthew Knepley 
25555f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(s, p, &dof));
25565f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetDof(sNew, perm[p], dof));
25575f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetConstraintDof(s, p, &cdof));
25585f80ce2aSJacob Faibussowitsch     if (cdof) CHKERRQ(PetscSectionSetConstraintDof(sNew, perm[p], cdof));
2559ea844a1aSMatthew Knepley     for (f = 0; f < numFields; ++f) {
25605f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetFieldDof(s, p, f, &dof));
25615f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetFieldDof(sNew, perm[p], f, dof));
25625f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetFieldConstraintDof(s, p, f, &cdof));
25635f80ce2aSJacob Faibussowitsch       if (cdof) CHKERRQ(PetscSectionSetFieldConstraintDof(sNew, perm[p], f, cdof));
2564ea844a1aSMatthew Knepley     }
2565ea844a1aSMatthew Knepley   }
25665f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetUp(sNew));
2567ea844a1aSMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
2568ea844a1aSMatthew Knepley     const PetscInt *cind;
2569ea844a1aSMatthew Knepley     PetscInt        cdof;
2570ea844a1aSMatthew Knepley 
25715f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetConstraintDof(s, p, &cdof));
2572ea844a1aSMatthew Knepley     if (cdof) {
25735f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetConstraintIndices(s, p, &cind));
25745f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetConstraintIndices(sNew, perm[p], cind));
2575ea844a1aSMatthew Knepley     }
2576ea844a1aSMatthew Knepley     for (f = 0; f < numFields; ++f) {
25775f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetFieldConstraintDof(s, p, f, &cdof));
2578ea844a1aSMatthew Knepley       if (cdof) {
25795f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetFieldConstraintIndices(s, p, f, &cind));
25805f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionSetFieldConstraintIndices(sNew, perm[p], f, cind));
2581ea844a1aSMatthew Knepley       }
2582ea844a1aSMatthew Knepley     }
2583ea844a1aSMatthew Knepley   }
25845f80ce2aSJacob Faibussowitsch   CHKERRQ(ISRestoreIndices(permutation, &perm));
2585ea844a1aSMatthew Knepley   *sectionNew = sNew;
2586ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
2587ea844a1aSMatthew Knepley }
2588ea844a1aSMatthew Knepley 
2589ea844a1aSMatthew Knepley /*@
2590ea844a1aSMatthew Knepley   PetscSectionSetClosureIndex - Set a cache of points in the closure of each point in the section
2591ea844a1aSMatthew Knepley 
2592ea844a1aSMatthew Knepley   Collective on section
2593ea844a1aSMatthew Knepley 
2594ea844a1aSMatthew Knepley   Input Parameters:
2595ea844a1aSMatthew Knepley + section   - The PetscSection
2596ea844a1aSMatthew Knepley . obj       - A PetscObject which serves as the key for this index
2597ea844a1aSMatthew Knepley . clSection - Section giving the size of the closure of each point
2598ea844a1aSMatthew Knepley - clPoints  - IS giving the points in each closure
2599ea844a1aSMatthew Knepley 
2600ea844a1aSMatthew Knepley   Note: We compress out closure points with no dofs in this section
2601ea844a1aSMatthew Knepley 
2602ea844a1aSMatthew Knepley   Level: advanced
2603ea844a1aSMatthew Knepley 
2604ea844a1aSMatthew Knepley .seealso: PetscSectionGetClosureIndex(), DMPlexCreateClosureIndex()
2605ea844a1aSMatthew Knepley @*/
2606ea844a1aSMatthew Knepley PetscErrorCode PetscSectionSetClosureIndex(PetscSection section, PetscObject obj, PetscSection clSection, IS clPoints)
2607ea844a1aSMatthew Knepley {
2608ea844a1aSMatthew Knepley   PetscFunctionBegin;
26093e03ebd8SStefano Zampini   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,1);
26103e03ebd8SStefano Zampini   PetscValidHeaderSpecific(clSection,PETSC_SECTION_CLASSID,3);
26113e03ebd8SStefano Zampini   PetscValidHeaderSpecific(clPoints,IS_CLASSID,4);
26125f80ce2aSJacob Faibussowitsch   if (section->clObj != obj) CHKERRQ(PetscSectionResetClosurePermutation(section));
2613ea844a1aSMatthew Knepley   section->clObj     = obj;
26145f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject)clSection));
26155f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject)clPoints));
26165f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionDestroy(&section->clSection));
26175f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&section->clPoints));
2618ea844a1aSMatthew Knepley   section->clSection = clSection;
2619ea844a1aSMatthew Knepley   section->clPoints  = clPoints;
2620ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
2621ea844a1aSMatthew Knepley }
2622ea844a1aSMatthew Knepley 
2623ea844a1aSMatthew Knepley /*@
2624ea844a1aSMatthew Knepley   PetscSectionGetClosureIndex - Get the cache of points in the closure of each point in the section
2625ea844a1aSMatthew Knepley 
2626ea844a1aSMatthew Knepley   Collective on section
2627ea844a1aSMatthew Knepley 
2628ea844a1aSMatthew Knepley   Input Parameters:
2629ea844a1aSMatthew Knepley + section   - The PetscSection
2630ea844a1aSMatthew Knepley - obj       - A PetscObject which serves as the key for this index
2631ea844a1aSMatthew Knepley 
2632ea844a1aSMatthew Knepley   Output Parameters:
2633ea844a1aSMatthew Knepley + clSection - Section giving the size of the closure of each point
2634ea844a1aSMatthew Knepley - clPoints  - IS giving the points in each closure
2635ea844a1aSMatthew Knepley 
2636ea844a1aSMatthew Knepley   Note: We compress out closure points with no dofs in this section
2637ea844a1aSMatthew Knepley 
2638ea844a1aSMatthew Knepley   Level: advanced
2639ea844a1aSMatthew Knepley 
2640ea844a1aSMatthew Knepley .seealso: PetscSectionSetClosureIndex(), DMPlexCreateClosureIndex()
2641ea844a1aSMatthew Knepley @*/
2642ea844a1aSMatthew Knepley PetscErrorCode PetscSectionGetClosureIndex(PetscSection section, PetscObject obj, PetscSection *clSection, IS *clPoints)
2643ea844a1aSMatthew Knepley {
2644ea844a1aSMatthew Knepley   PetscFunctionBegin;
2645ea844a1aSMatthew Knepley   if (section->clObj == obj) {
2646ea844a1aSMatthew Knepley     if (clSection) *clSection = section->clSection;
2647ea844a1aSMatthew Knepley     if (clPoints)  *clPoints  = section->clPoints;
2648ea844a1aSMatthew Knepley   } else {
2649ea844a1aSMatthew Knepley     if (clSection) *clSection = NULL;
2650ea844a1aSMatthew Knepley     if (clPoints)  *clPoints  = NULL;
2651ea844a1aSMatthew Knepley   }
2652ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
2653ea844a1aSMatthew Knepley }
2654ea844a1aSMatthew Knepley 
2655c459fbc1SJed Brown PetscErrorCode PetscSectionSetClosurePermutation_Internal(PetscSection section, PetscObject obj, PetscInt depth, PetscInt clSize, PetscCopyMode mode, PetscInt *clPerm)
2656ea844a1aSMatthew Knepley {
2657ea844a1aSMatthew Knepley   PetscInt       i;
2658c459fbc1SJed Brown   khiter_t iter;
2659c459fbc1SJed Brown   int new_entry;
2660c459fbc1SJed Brown   PetscSectionClosurePermKey key = {depth, clSize};
2661c459fbc1SJed Brown   PetscSectionClosurePermVal *val;
2662ea844a1aSMatthew Knepley 
2663ea844a1aSMatthew Knepley   PetscFunctionBegin;
2664ea844a1aSMatthew Knepley   if (section->clObj != obj) {
26655f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionDestroy(&section->clSection));
26665f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&section->clPoints));
2667ea844a1aSMatthew Knepley   }
2668ea844a1aSMatthew Knepley   section->clObj = obj;
26695f80ce2aSJacob Faibussowitsch   if (!section->clHash) CHKERRQ(PetscClPermCreate(&section->clHash));
2670c459fbc1SJed Brown   iter = kh_put(ClPerm, section->clHash, key, &new_entry);
2671c459fbc1SJed Brown   val = &kh_val(section->clHash, iter);
2672c459fbc1SJed Brown   if (!new_entry) {
26735f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(val->perm));
26745f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(val->invPerm));
2675c459fbc1SJed Brown   }
2676ea844a1aSMatthew Knepley   if (mode == PETSC_COPY_VALUES) {
26775f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(clSize, &val->perm));
26785f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscLogObjectMemory((PetscObject) obj, clSize*sizeof(PetscInt)));
26795f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscArraycpy(val->perm, clPerm, clSize));
2680ea844a1aSMatthew Knepley   } else if (mode == PETSC_OWN_POINTER) {
2681c459fbc1SJed Brown     val->perm = clPerm;
2682ea844a1aSMatthew Knepley   } else SETERRQ(PetscObjectComm(obj), PETSC_ERR_SUP, "Do not support borrowed arrays");
26835f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(clSize, &val->invPerm));
2684c459fbc1SJed Brown   for (i = 0; i < clSize; ++i) val->invPerm[clPerm[i]] = i;
2685ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
2686ea844a1aSMatthew Knepley }
2687ea844a1aSMatthew Knepley 
2688ea844a1aSMatthew Knepley /*@
2689c459fbc1SJed Brown   PetscSectionSetClosurePermutation - Set the dof permutation for the closure of each cell in the section, meaning clPerm[newIndex] = oldIndex.
2690ea844a1aSMatthew Knepley 
2691ea844a1aSMatthew Knepley   Not Collective
2692ea844a1aSMatthew Knepley 
2693ea844a1aSMatthew Knepley   Input Parameters:
2694ea844a1aSMatthew Knepley + section - The PetscSection
2695c459fbc1SJed Brown . obj     - A PetscObject which serves as the key for this index (usually a DM)
2696c459fbc1SJed Brown . depth   - Depth of points on which to apply the given permutation
2697ea844a1aSMatthew Knepley - perm    - Permutation of the cell dof closure
2698ea844a1aSMatthew Knepley 
2699c459fbc1SJed Brown   Note:
2700c459fbc1SJed Brown   The specified permutation will only be applied to points at depth whose closure size matches the length of perm.  In a
2701c459fbc1SJed Brown   mixed-topology or variable-degree finite element space, this function can be called multiple times at each depth for
2702c459fbc1SJed Brown   each topology and degree.
2703c459fbc1SJed Brown 
2704c459fbc1SJed Brown   This approach assumes that (depth, len(perm)) uniquely identifies the desired permutation; this might not be true for
2705c459fbc1SJed Brown   exotic/enriched spaces on mixed topology meshes.
2706ea844a1aSMatthew Knepley 
2707ea844a1aSMatthew Knepley   Level: intermediate
2708ea844a1aSMatthew Knepley 
2709ea844a1aSMatthew Knepley .seealso: PetscSectionGetClosurePermutation(), PetscSectionGetClosureIndex(), DMPlexCreateClosureIndex(), PetscCopyMode
2710ea844a1aSMatthew Knepley @*/
2711c459fbc1SJed Brown PetscErrorCode PetscSectionSetClosurePermutation(PetscSection section, PetscObject obj, PetscInt depth, IS perm)
2712ea844a1aSMatthew Knepley {
2713ea844a1aSMatthew Knepley   const PetscInt *clPerm = NULL;
2714ea844a1aSMatthew Knepley   PetscInt        clSize = 0;
2715ea844a1aSMatthew Knepley 
2716ea844a1aSMatthew Knepley   PetscFunctionBegin;
2717ea844a1aSMatthew Knepley   if (perm) {
27185f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetLocalSize(perm, &clSize));
27195f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetIndices(perm, &clPerm));
2720ea844a1aSMatthew Knepley   }
27215f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetClosurePermutation_Internal(section, obj, depth, clSize, PETSC_COPY_VALUES, (PetscInt *) clPerm));
27225f80ce2aSJacob Faibussowitsch   if (perm) CHKERRQ(ISRestoreIndices(perm, &clPerm));
2723ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
2724ea844a1aSMatthew Knepley }
2725ea844a1aSMatthew Knepley 
2726c459fbc1SJed Brown PetscErrorCode PetscSectionGetClosurePermutation_Internal(PetscSection section, PetscObject obj, PetscInt depth, PetscInt size, const PetscInt *perm[])
2727ea844a1aSMatthew Knepley {
2728ea844a1aSMatthew Knepley   PetscFunctionBegin;
2729ea844a1aSMatthew Knepley   if (section->clObj == obj) {
2730c459fbc1SJed Brown     PetscSectionClosurePermKey k = {depth, size};
2731c459fbc1SJed Brown     PetscSectionClosurePermVal v;
27325f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscClPermGet(section->clHash, k, &v));
2733c459fbc1SJed Brown     if (perm) *perm = v.perm;
2734ea844a1aSMatthew Knepley   } else {
2735ea844a1aSMatthew Knepley     if (perm) *perm = NULL;
2736ea844a1aSMatthew Knepley   }
2737ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
2738ea844a1aSMatthew Knepley }
2739ea844a1aSMatthew Knepley 
2740ea844a1aSMatthew Knepley /*@
2741ea844a1aSMatthew Knepley   PetscSectionGetClosurePermutation - Get the dof permutation for the closure of each cell in the section, meaning clPerm[newIndex] = oldIndex.
2742ea844a1aSMatthew Knepley 
2743ea844a1aSMatthew Knepley   Not collective
2744ea844a1aSMatthew Knepley 
2745ea844a1aSMatthew Knepley   Input Parameters:
2746ea844a1aSMatthew Knepley + section   - The PetscSection
2747c459fbc1SJed Brown . obj       - A PetscObject which serves as the key for this index (usually a DM)
2748c459fbc1SJed Brown . depth     - Depth stratum on which to obtain closure permutation
2749c459fbc1SJed Brown - clSize    - Closure size to be permuted (e.g., may vary with element topology and degree)
2750ea844a1aSMatthew Knepley 
2751ea844a1aSMatthew Knepley   Output Parameter:
2752ea844a1aSMatthew Knepley . perm - The dof closure permutation
2753ea844a1aSMatthew Knepley 
2754c459fbc1SJed Brown   Note:
2755ea844a1aSMatthew Knepley   The user must destroy the IS that is returned.
2756ea844a1aSMatthew Knepley 
2757ea844a1aSMatthew Knepley   Level: intermediate
2758ea844a1aSMatthew Knepley 
2759ea844a1aSMatthew Knepley .seealso: PetscSectionSetClosurePermutation(), PetscSectionGetClosureInversePermutation(), PetscSectionGetClosureIndex(), PetscSectionSetClosureIndex(), DMPlexCreateClosureIndex()
2760ea844a1aSMatthew Knepley @*/
2761c459fbc1SJed Brown PetscErrorCode PetscSectionGetClosurePermutation(PetscSection section, PetscObject obj, PetscInt depth, PetscInt clSize, IS *perm)
2762ea844a1aSMatthew Knepley {
2763ea844a1aSMatthew Knepley   const PetscInt *clPerm;
2764ea844a1aSMatthew Knepley 
2765ea844a1aSMatthew Knepley   PetscFunctionBegin;
27665f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetClosurePermutation_Internal(section, obj, depth, clSize, &clPerm));
27675f80ce2aSJacob Faibussowitsch   CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, clSize, clPerm, PETSC_USE_POINTER, perm));
2768ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
2769ea844a1aSMatthew Knepley }
2770ea844a1aSMatthew Knepley 
2771c459fbc1SJed Brown PetscErrorCode PetscSectionGetClosureInversePermutation_Internal(PetscSection section, PetscObject obj, PetscInt depth, PetscInt size, const PetscInt *perm[])
2772ea844a1aSMatthew Knepley {
2773ea844a1aSMatthew Knepley   PetscFunctionBegin;
2774c459fbc1SJed Brown   if (section->clObj == obj && section->clHash) {
2775c459fbc1SJed Brown     PetscSectionClosurePermKey k = {depth, size};
2776c459fbc1SJed Brown     PetscSectionClosurePermVal v;
27775f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscClPermGet(section->clHash, k, &v));
2778c459fbc1SJed Brown     if (perm) *perm = v.invPerm;
2779ea844a1aSMatthew Knepley   } else {
2780ea844a1aSMatthew Knepley     if (perm) *perm = NULL;
2781ea844a1aSMatthew Knepley   }
2782ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
2783ea844a1aSMatthew Knepley }
2784ea844a1aSMatthew Knepley 
2785ea844a1aSMatthew Knepley /*@
2786ea844a1aSMatthew Knepley   PetscSectionGetClosureInversePermutation - Get the inverse dof permutation for the closure of each cell in the section, meaning clPerm[oldIndex] = newIndex.
2787ea844a1aSMatthew Knepley 
2788ea844a1aSMatthew Knepley   Not collective
2789ea844a1aSMatthew Knepley 
2790ea844a1aSMatthew Knepley   Input Parameters:
2791ea844a1aSMatthew Knepley + section   - The PetscSection
2792c459fbc1SJed Brown . obj       - A PetscObject which serves as the key for this index (usually a DM)
2793c459fbc1SJed Brown . depth     - Depth stratum on which to obtain closure permutation
2794c459fbc1SJed Brown - clSize    - Closure size to be permuted (e.g., may vary with element topology and degree)
2795ea844a1aSMatthew Knepley 
2796ea844a1aSMatthew Knepley   Output Parameters:
2797c459fbc1SJed Brown . perm - The dof closure permutation
2798ea844a1aSMatthew Knepley 
2799c459fbc1SJed Brown   Note:
2800ea844a1aSMatthew Knepley   The user must destroy the IS that is returned.
2801ea844a1aSMatthew Knepley 
2802ea844a1aSMatthew Knepley   Level: intermediate
2803ea844a1aSMatthew Knepley 
2804ea844a1aSMatthew Knepley .seealso: PetscSectionSetClosurePermutation(), PetscSectionGetClosureIndex(), PetscSectionSetClosureIndex(), DMPlexCreateClosureIndex()
2805ea844a1aSMatthew Knepley @*/
2806c459fbc1SJed Brown PetscErrorCode PetscSectionGetClosureInversePermutation(PetscSection section, PetscObject obj, PetscInt depth, PetscInt clSize, IS *perm)
2807ea844a1aSMatthew Knepley {
2808ea844a1aSMatthew Knepley   const PetscInt *clPerm;
2809ea844a1aSMatthew Knepley 
2810ea844a1aSMatthew Knepley   PetscFunctionBegin;
28115f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetClosureInversePermutation_Internal(section, obj, depth, clSize, &clPerm));
28125f80ce2aSJacob Faibussowitsch   CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, clSize, clPerm, PETSC_USE_POINTER, perm));
2813ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
2814ea844a1aSMatthew Knepley }
2815ea844a1aSMatthew Knepley 
2816ea844a1aSMatthew Knepley /*@
2817ea844a1aSMatthew Knepley   PetscSectionGetField - Get the subsection associated with a single field
2818ea844a1aSMatthew Knepley 
2819ea844a1aSMatthew Knepley   Input Parameters:
2820ea844a1aSMatthew Knepley + s     - The PetscSection
2821ea844a1aSMatthew Knepley - field - The field number
2822ea844a1aSMatthew Knepley 
2823ea844a1aSMatthew Knepley   Output Parameter:
2824ea844a1aSMatthew Knepley . subs  - The subsection for the given field
2825ea844a1aSMatthew Knepley 
2826ea844a1aSMatthew Knepley   Level: intermediate
2827ea844a1aSMatthew Knepley 
2828ea844a1aSMatthew Knepley .seealso: PetscSectionSetNumFields()
2829ea844a1aSMatthew Knepley @*/
2830ea844a1aSMatthew Knepley PetscErrorCode PetscSectionGetField(PetscSection s, PetscInt field, PetscSection *subs)
2831ea844a1aSMatthew Knepley {
2832ea844a1aSMatthew Knepley   PetscFunctionBegin;
2833ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s,PETSC_SECTION_CLASSID,1);
2834ea844a1aSMatthew Knepley   PetscValidPointer(subs,3);
28352abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field,s->numFields);
2836ea844a1aSMatthew Knepley   *subs = s->field[field];
2837ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
2838ea844a1aSMatthew Knepley }
2839ea844a1aSMatthew Knepley 
2840ea844a1aSMatthew Knepley PetscClassId      PETSC_SECTION_SYM_CLASSID;
2841ea844a1aSMatthew Knepley PetscFunctionList PetscSectionSymList = NULL;
2842ea844a1aSMatthew Knepley 
2843ea844a1aSMatthew Knepley /*@
2844ea844a1aSMatthew Knepley   PetscSectionSymCreate - Creates an empty PetscSectionSym object.
2845ea844a1aSMatthew Knepley 
2846ea844a1aSMatthew Knepley   Collective
2847ea844a1aSMatthew Knepley 
2848ea844a1aSMatthew Knepley   Input Parameter:
2849ea844a1aSMatthew Knepley . comm - the MPI communicator
2850ea844a1aSMatthew Knepley 
2851ea844a1aSMatthew Knepley   Output Parameter:
2852ea844a1aSMatthew Knepley . sym - pointer to the new set of symmetries
2853ea844a1aSMatthew Knepley 
2854ea844a1aSMatthew Knepley   Level: developer
2855ea844a1aSMatthew Knepley 
2856ea844a1aSMatthew Knepley .seealso: PetscSectionSym, PetscSectionSymDestroy()
2857ea844a1aSMatthew Knepley @*/
2858ea844a1aSMatthew Knepley PetscErrorCode PetscSectionSymCreate(MPI_Comm comm, PetscSectionSym *sym)
2859ea844a1aSMatthew Knepley {
2860ea844a1aSMatthew Knepley   PetscFunctionBegin;
2861ea844a1aSMatthew Knepley   PetscValidPointer(sym,2);
28625f80ce2aSJacob Faibussowitsch   CHKERRQ(ISInitializePackage());
28635f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHeaderCreate(*sym,PETSC_SECTION_SYM_CLASSID,"PetscSectionSym","Section Symmetry","IS",comm,PetscSectionSymDestroy,PetscSectionSymView));
2864ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
2865ea844a1aSMatthew Knepley }
2866ea844a1aSMatthew Knepley 
2867ea844a1aSMatthew Knepley /*@C
2868ea844a1aSMatthew Knepley   PetscSectionSymSetType - Builds a PetscSection symmetry, for a particular implementation.
2869ea844a1aSMatthew Knepley 
2870ea844a1aSMatthew Knepley   Collective on PetscSectionSym
2871ea844a1aSMatthew Knepley 
2872ea844a1aSMatthew Knepley   Input Parameters:
2873ea844a1aSMatthew Knepley + sym    - The section symmetry object
2874ea844a1aSMatthew Knepley - method - The name of the section symmetry type
2875ea844a1aSMatthew Knepley 
2876ea844a1aSMatthew Knepley   Level: developer
2877ea844a1aSMatthew Knepley 
2878ea844a1aSMatthew Knepley .seealso: PetscSectionSymGetType(), PetscSectionSymCreate()
2879ea844a1aSMatthew Knepley @*/
2880ea844a1aSMatthew Knepley PetscErrorCode  PetscSectionSymSetType(PetscSectionSym sym, PetscSectionSymType method)
2881ea844a1aSMatthew Knepley {
2882ea844a1aSMatthew Knepley   PetscErrorCode (*r)(PetscSectionSym);
2883ea844a1aSMatthew Knepley   PetscBool      match;
2884ea844a1aSMatthew Knepley 
2885ea844a1aSMatthew Knepley   PetscFunctionBegin;
2886ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(sym, PETSC_SECTION_SYM_CLASSID,1);
28875f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectTypeCompare((PetscObject) sym, method, &match));
2888ea844a1aSMatthew Knepley   if (match) PetscFunctionReturn(0);
2889ea844a1aSMatthew Knepley 
28905f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFunctionListFind(PetscSectionSymList,method,&r));
2891*28b400f6SJacob Faibussowitsch   PetscCheck(r,PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscSectionSym type: %s", method);
2892ea844a1aSMatthew Knepley   if (sym->ops->destroy) {
28935f80ce2aSJacob Faibussowitsch     CHKERRQ((*sym->ops->destroy)(sym));
2894ea844a1aSMatthew Knepley     sym->ops->destroy = NULL;
2895ea844a1aSMatthew Knepley   }
28965f80ce2aSJacob Faibussowitsch   CHKERRQ((*r)(sym));
28975f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectChangeTypeName((PetscObject)sym,method));
2898ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
2899ea844a1aSMatthew Knepley }
2900ea844a1aSMatthew Knepley 
2901ea844a1aSMatthew Knepley /*@C
2902ea844a1aSMatthew Knepley   PetscSectionSymGetType - Gets the section symmetry type name (as a string) from the PetscSectionSym.
2903ea844a1aSMatthew Knepley 
2904ea844a1aSMatthew Knepley   Not Collective
2905ea844a1aSMatthew Knepley 
2906ea844a1aSMatthew Knepley   Input Parameter:
2907ea844a1aSMatthew Knepley . sym  - The section symmetry
2908ea844a1aSMatthew Knepley 
2909ea844a1aSMatthew Knepley   Output Parameter:
2910ea844a1aSMatthew Knepley . type - The index set type name
2911ea844a1aSMatthew Knepley 
2912ea844a1aSMatthew Knepley   Level: developer
2913ea844a1aSMatthew Knepley 
2914ea844a1aSMatthew Knepley .seealso: PetscSectionSymSetType(), PetscSectionSymCreate()
2915ea844a1aSMatthew Knepley @*/
2916ea844a1aSMatthew Knepley PetscErrorCode PetscSectionSymGetType(PetscSectionSym sym, PetscSectionSymType *type)
2917ea844a1aSMatthew Knepley {
2918ea844a1aSMatthew Knepley   PetscFunctionBegin;
2919ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(sym, PETSC_SECTION_SYM_CLASSID,1);
2920ea844a1aSMatthew Knepley   PetscValidCharPointer(type,2);
2921ea844a1aSMatthew Knepley   *type = ((PetscObject)sym)->type_name;
2922ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
2923ea844a1aSMatthew Knepley }
2924ea844a1aSMatthew Knepley 
2925ea844a1aSMatthew Knepley /*@C
2926ea844a1aSMatthew Knepley   PetscSectionSymRegister - Adds a new section symmetry implementation
2927ea844a1aSMatthew Knepley 
2928ea844a1aSMatthew Knepley   Not Collective
2929ea844a1aSMatthew Knepley 
2930ea844a1aSMatthew Knepley   Input Parameters:
2931ea844a1aSMatthew Knepley + name        - The name of a new user-defined creation routine
2932ea844a1aSMatthew Knepley - create_func - The creation routine itself
2933ea844a1aSMatthew Knepley 
2934ea844a1aSMatthew Knepley   Notes:
2935ea844a1aSMatthew Knepley   PetscSectionSymRegister() may be called multiple times to add several user-defined vectors
2936ea844a1aSMatthew Knepley 
2937ea844a1aSMatthew Knepley   Level: developer
2938ea844a1aSMatthew Knepley 
2939ea844a1aSMatthew Knepley .seealso: PetscSectionSymCreate(), PetscSectionSymSetType()
2940ea844a1aSMatthew Knepley @*/
2941ea844a1aSMatthew Knepley PetscErrorCode PetscSectionSymRegister(const char sname[], PetscErrorCode (*function)(PetscSectionSym))
2942ea844a1aSMatthew Knepley {
2943ea844a1aSMatthew Knepley   PetscFunctionBegin;
29445f80ce2aSJacob Faibussowitsch   CHKERRQ(ISInitializePackage());
29455f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFunctionListAdd(&PetscSectionSymList,sname,function));
2946ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
2947ea844a1aSMatthew Knepley }
2948ea844a1aSMatthew Knepley 
2949ea844a1aSMatthew Knepley /*@
2950ea844a1aSMatthew Knepley    PetscSectionSymDestroy - Destroys a section symmetry.
2951ea844a1aSMatthew Knepley 
2952ea844a1aSMatthew Knepley    Collective on PetscSectionSym
2953ea844a1aSMatthew Knepley 
2954ea844a1aSMatthew Knepley    Input Parameters:
2955ea844a1aSMatthew Knepley .  sym - the section symmetry
2956ea844a1aSMatthew Knepley 
2957ea844a1aSMatthew Knepley    Level: developer
2958ea844a1aSMatthew Knepley 
2959ea844a1aSMatthew Knepley .seealso: PetscSectionSymCreate(), PetscSectionSymDestroy()
2960ea844a1aSMatthew Knepley @*/
2961ea844a1aSMatthew Knepley PetscErrorCode PetscSectionSymDestroy(PetscSectionSym *sym)
2962ea844a1aSMatthew Knepley {
2963ea844a1aSMatthew Knepley   SymWorkLink    link,next;
2964ea844a1aSMatthew Knepley 
2965ea844a1aSMatthew Knepley   PetscFunctionBegin;
2966ea844a1aSMatthew Knepley   if (!*sym) PetscFunctionReturn(0);
2967ea844a1aSMatthew Knepley   PetscValidHeaderSpecific((*sym),PETSC_SECTION_SYM_CLASSID,1);
29684c8fdceaSLisandro Dalcin   if (--((PetscObject)(*sym))->refct > 0) {*sym = NULL; PetscFunctionReturn(0);}
2969ea844a1aSMatthew Knepley   if ((*sym)->ops->destroy) {
29705f80ce2aSJacob Faibussowitsch     CHKERRQ((*(*sym)->ops->destroy)(*sym));
2971ea844a1aSMatthew Knepley   }
29722c71b3e2SJacob Faibussowitsch   PetscCheckFalse((*sym)->workout,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
2973ea844a1aSMatthew Knepley   for (link=(*sym)->workin; link; link=next) {
2974ea844a1aSMatthew Knepley     next = link->next;
29755f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree2(link->perms,link->rots));
29765f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(link));
2977ea844a1aSMatthew Knepley   }
2978ea844a1aSMatthew Knepley   (*sym)->workin = NULL;
29795f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHeaderDestroy(sym));
2980ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
2981ea844a1aSMatthew Knepley }
2982ea844a1aSMatthew Knepley 
2983ea844a1aSMatthew Knepley /*@C
2984ea844a1aSMatthew Knepley    PetscSectionSymView - Displays a section symmetry
2985ea844a1aSMatthew Knepley 
2986ea844a1aSMatthew Knepley    Collective on PetscSectionSym
2987ea844a1aSMatthew Knepley 
2988ea844a1aSMatthew Knepley    Input Parameters:
2989ea844a1aSMatthew Knepley +  sym - the index set
2990ea844a1aSMatthew Knepley -  viewer - viewer used to display the set, for example PETSC_VIEWER_STDOUT_SELF.
2991ea844a1aSMatthew Knepley 
2992ea844a1aSMatthew Knepley    Level: developer
2993ea844a1aSMatthew Knepley 
2994ea844a1aSMatthew Knepley .seealso: PetscViewerASCIIOpen()
2995ea844a1aSMatthew Knepley @*/
2996ea844a1aSMatthew Knepley PetscErrorCode PetscSectionSymView(PetscSectionSym sym,PetscViewer viewer)
2997ea844a1aSMatthew Knepley {
2998ea844a1aSMatthew Knepley   PetscFunctionBegin;
2999ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(sym,PETSC_SECTION_SYM_CLASSID,1);
3000ea844a1aSMatthew Knepley   if (!viewer) {
30015f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)sym),&viewer));
3002ea844a1aSMatthew Knepley   }
3003ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
3004ea844a1aSMatthew Knepley   PetscCheckSameComm(sym,1,viewer,2);
30055f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectPrintClassNamePrefixType((PetscObject)sym,viewer));
3006ea844a1aSMatthew Knepley   if (sym->ops->view) {
30075f80ce2aSJacob Faibussowitsch     CHKERRQ((*sym->ops->view)(sym,viewer));
3008ea844a1aSMatthew Knepley   }
3009ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
3010ea844a1aSMatthew Knepley }
3011ea844a1aSMatthew Knepley 
3012ea844a1aSMatthew Knepley /*@
3013ea844a1aSMatthew Knepley   PetscSectionSetSym - Set the symmetries for the data referred to by the section
3014ea844a1aSMatthew Knepley 
3015ea844a1aSMatthew Knepley   Collective on PetscSection
3016ea844a1aSMatthew Knepley 
3017ea844a1aSMatthew Knepley   Input Parameters:
3018ea844a1aSMatthew Knepley + section - the section describing data layout
3019ea844a1aSMatthew Knepley - sym - the symmetry describing the affect of orientation on the access of the data
3020ea844a1aSMatthew Knepley 
3021ea844a1aSMatthew Knepley   Level: developer
3022ea844a1aSMatthew Knepley 
3023ea844a1aSMatthew Knepley .seealso: PetscSectionGetSym(), PetscSectionSymCreate()
3024ea844a1aSMatthew Knepley @*/
3025ea844a1aSMatthew Knepley PetscErrorCode PetscSectionSetSym(PetscSection section, PetscSectionSym sym)
3026ea844a1aSMatthew Knepley {
3027ea844a1aSMatthew Knepley   PetscFunctionBegin;
3028ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,1);
30295f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSymDestroy(&(section->sym)));
3030ea844a1aSMatthew Knepley   if (sym) {
3031ea844a1aSMatthew Knepley     PetscValidHeaderSpecific(sym,PETSC_SECTION_SYM_CLASSID,2);
3032ea844a1aSMatthew Knepley     PetscCheckSameComm(section,1,sym,2);
30335f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectReference((PetscObject) sym));
3034ea844a1aSMatthew Knepley   }
3035ea844a1aSMatthew Knepley   section->sym = sym;
3036ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
3037ea844a1aSMatthew Knepley }
3038ea844a1aSMatthew Knepley 
3039ea844a1aSMatthew Knepley /*@
3040ea844a1aSMatthew Knepley   PetscSectionGetSym - Get the symmetries for the data referred to by the section
3041ea844a1aSMatthew Knepley 
3042ea844a1aSMatthew Knepley   Not collective
3043ea844a1aSMatthew Knepley 
3044ea844a1aSMatthew Knepley   Input Parameters:
3045ea844a1aSMatthew Knepley . section - the section describing data layout
3046ea844a1aSMatthew Knepley 
3047ea844a1aSMatthew Knepley   Output Parameters:
3048ea844a1aSMatthew Knepley . sym - the symmetry describing the affect of orientation on the access of the data
3049ea844a1aSMatthew Knepley 
3050ea844a1aSMatthew Knepley   Level: developer
3051ea844a1aSMatthew Knepley 
3052ea844a1aSMatthew Knepley .seealso: PetscSectionSetSym(), PetscSectionSymCreate()
3053ea844a1aSMatthew Knepley @*/
3054ea844a1aSMatthew Knepley PetscErrorCode PetscSectionGetSym(PetscSection section, PetscSectionSym *sym)
3055ea844a1aSMatthew Knepley {
3056ea844a1aSMatthew Knepley   PetscFunctionBegin;
3057ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,1);
3058ea844a1aSMatthew Knepley   *sym = section->sym;
3059ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
3060ea844a1aSMatthew Knepley }
3061ea844a1aSMatthew Knepley 
3062ea844a1aSMatthew Knepley /*@
3063ea844a1aSMatthew Knepley   PetscSectionSetFieldSym - Set the symmetries for the data referred to by a field of the section
3064ea844a1aSMatthew Knepley 
3065ea844a1aSMatthew Knepley   Collective on PetscSection
3066ea844a1aSMatthew Knepley 
3067ea844a1aSMatthew Knepley   Input Parameters:
3068ea844a1aSMatthew Knepley + section - the section describing data layout
3069ea844a1aSMatthew Knepley . field - the field number
3070ea844a1aSMatthew Knepley - sym - the symmetry describing the affect of orientation on the access of the data
3071ea844a1aSMatthew Knepley 
3072ea844a1aSMatthew Knepley   Level: developer
3073ea844a1aSMatthew Knepley 
3074ea844a1aSMatthew Knepley .seealso: PetscSectionGetFieldSym(), PetscSectionSymCreate()
3075ea844a1aSMatthew Knepley @*/
3076ea844a1aSMatthew Knepley PetscErrorCode PetscSectionSetFieldSym(PetscSection section, PetscInt field, PetscSectionSym sym)
3077ea844a1aSMatthew Knepley {
3078ea844a1aSMatthew Knepley   PetscFunctionBegin;
3079ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,1);
30802abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field,section->numFields);
30815f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetSym(section->field[field],sym));
3082ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
3083ea844a1aSMatthew Knepley }
3084ea844a1aSMatthew Knepley 
3085ea844a1aSMatthew Knepley /*@
3086ea844a1aSMatthew Knepley   PetscSectionGetFieldSym - Get the symmetries for the data referred to by a field of the section
3087ea844a1aSMatthew Knepley 
3088ea844a1aSMatthew Knepley   Collective on PetscSection
3089ea844a1aSMatthew Knepley 
3090ea844a1aSMatthew Knepley   Input Parameters:
3091ea844a1aSMatthew Knepley + section - the section describing data layout
3092ea844a1aSMatthew Knepley - field - the field number
3093ea844a1aSMatthew Knepley 
3094ea844a1aSMatthew Knepley   Output Parameters:
3095ea844a1aSMatthew Knepley . sym - the symmetry describing the affect of orientation on the access of the data
3096ea844a1aSMatthew Knepley 
3097ea844a1aSMatthew Knepley   Level: developer
3098ea844a1aSMatthew Knepley 
3099ea844a1aSMatthew Knepley .seealso: PetscSectionSetFieldSym(), PetscSectionSymCreate()
3100ea844a1aSMatthew Knepley @*/
3101ea844a1aSMatthew Knepley PetscErrorCode PetscSectionGetFieldSym(PetscSection section, PetscInt field, PetscSectionSym *sym)
3102ea844a1aSMatthew Knepley {
3103ea844a1aSMatthew Knepley   PetscFunctionBegin;
3104ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,1);
31052abc8c78SJacob Faibussowitsch   PetscSectionCheckValidField(field,section->numFields);
3106ea844a1aSMatthew Knepley   *sym = section->field[field]->sym;
3107ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
3108ea844a1aSMatthew Knepley }
3109ea844a1aSMatthew Knepley 
3110ea844a1aSMatthew Knepley /*@C
3111ea844a1aSMatthew Knepley   PetscSectionGetPointSyms - Get the symmetries for a set of points in a PetscSection under specific orientations.
3112ea844a1aSMatthew Knepley 
3113ea844a1aSMatthew Knepley   Not collective
3114ea844a1aSMatthew Knepley 
3115ea844a1aSMatthew Knepley   Input Parameters:
3116ea844a1aSMatthew Knepley + section - the section
3117ea844a1aSMatthew Knepley . numPoints - the number of points
3118ea844a1aSMatthew Knepley - points - an array of size 2 * numPoints, containing a list of (point, orientation) pairs. (An orientation is an
3119ea844a1aSMatthew Knepley     arbitrary integer: its interpretation is up to sym.  Orientations are used by DM: for their interpretation in that
3120ea844a1aSMatthew Knepley     context, see DMPlexGetConeOrientation()).
3121ea844a1aSMatthew Knepley 
3122d8d19677SJose E. Roman   Output Parameters:
3123ea844a1aSMatthew Knepley + perms - The permutations for the given orientations (or NULL if there is no symmetry or the permutation is the identity).
3124ea844a1aSMatthew Knepley - rots - The field rotations symmetries for the given orientations (or NULL if there is no symmetry or the rotations are all
3125ea844a1aSMatthew Knepley     identity).
3126ea844a1aSMatthew Knepley 
3127ea844a1aSMatthew Knepley   Example of usage, gathering dofs into a local array (lArray) from a section array (sArray):
3128ea844a1aSMatthew Knepley .vb
3129ea844a1aSMatthew Knepley      const PetscInt    **perms;
3130ea844a1aSMatthew Knepley      const PetscScalar **rots;
3131ea844a1aSMatthew Knepley      PetscInt            lOffset;
3132ea844a1aSMatthew Knepley 
3133ea844a1aSMatthew Knepley      PetscSectionGetPointSyms(section,numPoints,points,&perms,&rots);
3134ea844a1aSMatthew Knepley      for (i = 0, lOffset = 0; i < numPoints; i++) {
3135ea844a1aSMatthew Knepley        PetscInt           point = points[2*i], dof, sOffset;
3136ea844a1aSMatthew Knepley        const PetscInt    *perm  = perms ? perms[i] : NULL;
3137ea844a1aSMatthew Knepley        const PetscScalar *rot   = rots  ? rots[i]  : NULL;
3138ea844a1aSMatthew Knepley 
3139ea844a1aSMatthew Knepley        PetscSectionGetDof(section,point,&dof);
3140ea844a1aSMatthew Knepley        PetscSectionGetOffset(section,point,&sOffset);
3141ea844a1aSMatthew Knepley 
3142ea844a1aSMatthew Knepley        if (perm) {for (j = 0; j < dof; j++) {lArray[lOffset + perm[j]]  = sArray[sOffset + j];}}
3143ea844a1aSMatthew Knepley        else      {for (j = 0; j < dof; j++) {lArray[lOffset +      j ]  = sArray[sOffset + j];}}
3144ea844a1aSMatthew Knepley        if (rot)  {for (j = 0; j < dof; j++) {lArray[lOffset +      j ] *= rot[j];             }}
3145ea844a1aSMatthew Knepley        lOffset += dof;
3146ea844a1aSMatthew Knepley      }
3147ea844a1aSMatthew Knepley      PetscSectionRestorePointSyms(section,numPoints,points,&perms,&rots);
3148ea844a1aSMatthew Knepley .ve
3149ea844a1aSMatthew Knepley 
3150ea844a1aSMatthew Knepley   Example of usage, adding dofs into a section array (sArray) from a local array (lArray):
3151ea844a1aSMatthew Knepley .vb
3152ea844a1aSMatthew Knepley      const PetscInt    **perms;
3153ea844a1aSMatthew Knepley      const PetscScalar **rots;
3154ea844a1aSMatthew Knepley      PetscInt            lOffset;
3155ea844a1aSMatthew Knepley 
3156ea844a1aSMatthew Knepley      PetscSectionGetPointSyms(section,numPoints,points,&perms,&rots);
3157ea844a1aSMatthew Knepley      for (i = 0, lOffset = 0; i < numPoints; i++) {
3158ea844a1aSMatthew Knepley        PetscInt           point = points[2*i], dof, sOffset;
3159ea844a1aSMatthew Knepley        const PetscInt    *perm  = perms ? perms[i] : NULL;
3160ea844a1aSMatthew Knepley        const PetscScalar *rot   = rots  ? rots[i]  : NULL;
3161ea844a1aSMatthew Knepley 
3162ea844a1aSMatthew Knepley        PetscSectionGetDof(section,point,&dof);
3163ea844a1aSMatthew Knepley        PetscSectionGetOffset(section,point,&sOff);
3164ea844a1aSMatthew Knepley 
3165ea844a1aSMatthew Knepley        if (perm) {for (j = 0; j < dof; j++) {sArray[sOffset + j] += lArray[lOffset + perm[j]] * (rot ? PetscConj(rot[perm[j]]) : 1.);}}
3166ea844a1aSMatthew Knepley        else      {for (j = 0; j < dof; j++) {sArray[sOffset + j] += lArray[lOffset +      j ] * (rot ? PetscConj(rot[     j ]) : 1.);}}
3167ea844a1aSMatthew Knepley        offset += dof;
3168ea844a1aSMatthew Knepley      }
3169ea844a1aSMatthew Knepley      PetscSectionRestorePointSyms(section,numPoints,points,&perms,&rots);
3170ea844a1aSMatthew Knepley .ve
3171ea844a1aSMatthew Knepley 
3172ea844a1aSMatthew Knepley   Level: developer
3173ea844a1aSMatthew Knepley 
3174ea844a1aSMatthew Knepley .seealso: PetscSectionRestorePointSyms(), PetscSectionSymCreate(), PetscSectionSetSym(), PetscSectionGetSym()
3175ea844a1aSMatthew Knepley @*/
3176ea844a1aSMatthew Knepley PetscErrorCode PetscSectionGetPointSyms(PetscSection section, PetscInt numPoints, const PetscInt *points, const PetscInt ***perms, const PetscScalar ***rots)
3177ea844a1aSMatthew Knepley {
3178ea844a1aSMatthew Knepley   PetscSectionSym sym;
3179ea844a1aSMatthew Knepley 
3180ea844a1aSMatthew Knepley   PetscFunctionBegin;
3181ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,1);
3182ea844a1aSMatthew Knepley   if (numPoints) PetscValidIntPointer(points,3);
3183ea844a1aSMatthew Knepley   if (perms) *perms = NULL;
3184ea844a1aSMatthew Knepley   if (rots)  *rots  = NULL;
3185ea844a1aSMatthew Knepley   sym = section->sym;
3186ea844a1aSMatthew Knepley   if (sym && (perms || rots)) {
3187ea844a1aSMatthew Knepley     SymWorkLink link;
3188ea844a1aSMatthew Knepley 
3189ea844a1aSMatthew Knepley     if (sym->workin) {
3190ea844a1aSMatthew Knepley       link        = sym->workin;
3191ea844a1aSMatthew Knepley       sym->workin = sym->workin->next;
3192ea844a1aSMatthew Knepley     } else {
31935f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscNewLog(sym,&link));
3194ea844a1aSMatthew Knepley     }
3195ea844a1aSMatthew Knepley     if (numPoints > link->numPoints) {
31965f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree2(link->perms,link->rots));
31975f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscMalloc2(numPoints,&link->perms,numPoints,&link->rots));
3198ea844a1aSMatthew Knepley       link->numPoints = numPoints;
3199ea844a1aSMatthew Knepley     }
3200ea844a1aSMatthew Knepley     link->next   = sym->workout;
3201ea844a1aSMatthew Knepley     sym->workout = link;
32025f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscArrayzero((PetscInt**)link->perms,numPoints));
32035f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscArrayzero((PetscInt**)link->rots,numPoints));
32045f80ce2aSJacob Faibussowitsch     CHKERRQ((*sym->ops->getpoints) (sym, section, numPoints, points, link->perms, link->rots));
3205ea844a1aSMatthew Knepley     if (perms) *perms = link->perms;
3206ea844a1aSMatthew Knepley     if (rots)  *rots  = link->rots;
3207ea844a1aSMatthew Knepley   }
3208ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
3209ea844a1aSMatthew Knepley }
3210ea844a1aSMatthew Knepley 
3211ea844a1aSMatthew Knepley /*@C
3212ea844a1aSMatthew Knepley   PetscSectionRestorePointSyms - Restore the symmetries returned by PetscSectionGetPointSyms()
3213ea844a1aSMatthew Knepley 
3214ea844a1aSMatthew Knepley   Not collective
3215ea844a1aSMatthew Knepley 
3216ea844a1aSMatthew Knepley   Input Parameters:
3217ea844a1aSMatthew Knepley + section - the section
3218ea844a1aSMatthew Knepley . numPoints - the number of points
3219ea844a1aSMatthew Knepley - points - an array of size 2 * numPoints, containing a list of (point, orientation) pairs. (An orientation is an
3220ea844a1aSMatthew Knepley     arbitrary integer: its interpretation is up to sym.  Orientations are used by DM: for their interpretation in that
3221ea844a1aSMatthew Knepley     context, see DMPlexGetConeOrientation()).
3222ea844a1aSMatthew Knepley 
3223d8d19677SJose E. Roman   Output Parameters:
3224ea844a1aSMatthew Knepley + perms - The permutations for the given orientations: set to NULL at conclusion
3225ea844a1aSMatthew Knepley - rots - The field rotations symmetries for the given orientations: set to NULL at conclusion
3226ea844a1aSMatthew Knepley 
3227ea844a1aSMatthew Knepley   Level: developer
3228ea844a1aSMatthew Knepley 
3229ea844a1aSMatthew Knepley .seealso: PetscSectionGetPointSyms(), PetscSectionSymCreate(), PetscSectionSetSym(), PetscSectionGetSym()
3230ea844a1aSMatthew Knepley @*/
3231ea844a1aSMatthew Knepley PetscErrorCode PetscSectionRestorePointSyms(PetscSection section, PetscInt numPoints, const PetscInt *points, const PetscInt ***perms, const PetscScalar ***rots)
3232ea844a1aSMatthew Knepley {
3233ea844a1aSMatthew Knepley   PetscSectionSym sym;
3234ea844a1aSMatthew Knepley 
3235ea844a1aSMatthew Knepley   PetscFunctionBegin;
3236ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,1);
3237ea844a1aSMatthew Knepley   sym = section->sym;
3238ea844a1aSMatthew Knepley   if (sym && (perms || rots)) {
3239ea844a1aSMatthew Knepley     SymWorkLink *p,link;
3240ea844a1aSMatthew Knepley 
3241ea844a1aSMatthew Knepley     for (p=&sym->workout; (link=*p); p=&link->next) {
3242ea844a1aSMatthew Knepley       if ((perms && link->perms == *perms) || (rots && link->rots == *rots)) {
3243ea844a1aSMatthew Knepley         *p          = link->next;
3244ea844a1aSMatthew Knepley         link->next  = sym->workin;
3245ea844a1aSMatthew Knepley         sym->workin = link;
3246ea844a1aSMatthew Knepley         if (perms) *perms = NULL;
3247ea844a1aSMatthew Knepley         if (rots)  *rots  = NULL;
3248ea844a1aSMatthew Knepley         PetscFunctionReturn(0);
3249ea844a1aSMatthew Knepley       }
3250ea844a1aSMatthew Knepley     }
3251ea844a1aSMatthew Knepley     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
3252ea844a1aSMatthew Knepley   }
3253ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
3254ea844a1aSMatthew Knepley }
3255ea844a1aSMatthew Knepley 
3256ea844a1aSMatthew Knepley /*@C
3257ea844a1aSMatthew Knepley   PetscSectionGetFieldPointSyms - Get the symmetries for a set of points in a field of a PetscSection under specific orientations.
3258ea844a1aSMatthew Knepley 
3259ea844a1aSMatthew Knepley   Not collective
3260ea844a1aSMatthew Knepley 
3261ea844a1aSMatthew Knepley   Input Parameters:
3262ea844a1aSMatthew Knepley + section - the section
3263ea844a1aSMatthew Knepley . field - the field of the section
3264ea844a1aSMatthew Knepley . numPoints - the number of points
3265ea844a1aSMatthew Knepley - points - an array of size 2 * numPoints, containing a list of (point, orientation) pairs. (An orientation is an
3266ea844a1aSMatthew Knepley     arbitrary integer: its interpretation is up to sym.  Orientations are used by DM: for their interpretation in that
3267ea844a1aSMatthew Knepley     context, see DMPlexGetConeOrientation()).
3268ea844a1aSMatthew Knepley 
3269d8d19677SJose E. Roman   Output Parameters:
3270ea844a1aSMatthew Knepley + perms - The permutations for the given orientations (or NULL if there is no symmetry or the permutation is the identity).
3271ea844a1aSMatthew Knepley - rots - The field rotations symmetries for the given orientations (or NULL if there is no symmetry or the rotations are all
3272ea844a1aSMatthew Knepley     identity).
3273ea844a1aSMatthew Knepley 
3274ea844a1aSMatthew Knepley   Level: developer
3275ea844a1aSMatthew Knepley 
3276ea844a1aSMatthew Knepley .seealso: PetscSectionGetPointSyms(), PetscSectionRestoreFieldPointSyms()
3277ea844a1aSMatthew Knepley @*/
3278ea844a1aSMatthew Knepley PetscErrorCode PetscSectionGetFieldPointSyms(PetscSection section, PetscInt field, PetscInt numPoints, const PetscInt *points, const PetscInt ***perms, const PetscScalar ***rots)
3279ea844a1aSMatthew Knepley {
3280ea844a1aSMatthew Knepley   PetscFunctionBegin;
3281ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,1);
32822c71b3e2SJacob Faibussowitsch   PetscCheckFalse(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);
32835f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetPointSyms(section->field[field],numPoints,points,perms,rots));
3284ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
3285ea844a1aSMatthew Knepley }
3286ea844a1aSMatthew Knepley 
3287ea844a1aSMatthew Knepley /*@C
3288ea844a1aSMatthew Knepley   PetscSectionRestoreFieldPointSyms - Restore the symmetries returned by PetscSectionGetFieldPointSyms()
3289ea844a1aSMatthew Knepley 
3290ea844a1aSMatthew Knepley   Not collective
3291ea844a1aSMatthew Knepley 
3292ea844a1aSMatthew Knepley   Input Parameters:
3293ea844a1aSMatthew Knepley + section - the section
3294ea844a1aSMatthew Knepley . field - the field number
3295ea844a1aSMatthew Knepley . numPoints - the number of points
3296ea844a1aSMatthew Knepley - points - an array of size 2 * numPoints, containing a list of (point, orientation) pairs. (An orientation is an
3297ea844a1aSMatthew Knepley     arbitrary integer: its interpretation is up to sym.  Orientations are used by DM: for their interpretation in that
3298ea844a1aSMatthew Knepley     context, see DMPlexGetConeOrientation()).
3299ea844a1aSMatthew Knepley 
3300d8d19677SJose E. Roman   Output Parameters:
3301ea844a1aSMatthew Knepley + perms - The permutations for the given orientations: set to NULL at conclusion
3302ea844a1aSMatthew Knepley - rots - The field rotations symmetries for the given orientations: set to NULL at conclusion
3303ea844a1aSMatthew Knepley 
3304ea844a1aSMatthew Knepley   Level: developer
3305ea844a1aSMatthew Knepley 
3306ea844a1aSMatthew Knepley .seealso: PetscSectionRestorePointSyms(), petscSectionGetFieldPointSyms(), PetscSectionSymCreate(), PetscSectionSetSym(), PetscSectionGetSym()
3307ea844a1aSMatthew Knepley @*/
3308ea844a1aSMatthew Knepley PetscErrorCode PetscSectionRestoreFieldPointSyms(PetscSection section, PetscInt field, PetscInt numPoints, const PetscInt *points, const PetscInt ***perms, const PetscScalar ***rots)
3309ea844a1aSMatthew Knepley {
3310ea844a1aSMatthew Knepley   PetscFunctionBegin;
3311ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,1);
33122c71b3e2SJacob Faibussowitsch   PetscCheckFalse(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);
33135f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionRestorePointSyms(section->field[field],numPoints,points,perms,rots));
3314ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
3315ea844a1aSMatthew Knepley }
3316ea844a1aSMatthew Knepley 
3317ea844a1aSMatthew Knepley /*@
3318b004864fSMatthew G. Knepley   PetscSectionSymCopy - Copy the symmetries, assuming that the point structure is compatible
3319b004864fSMatthew G. Knepley 
3320b004864fSMatthew G. Knepley   Not collective
3321b004864fSMatthew G. Knepley 
3322b004864fSMatthew G. Knepley   Input Parameter:
3323b004864fSMatthew G. Knepley . sym - the PetscSectionSym
3324b004864fSMatthew G. Knepley 
3325b004864fSMatthew G. Knepley   Output Parameter:
3326b004864fSMatthew G. Knepley . nsym - the equivalent symmetries
3327b004864fSMatthew G. Knepley 
3328b004864fSMatthew G. Knepley   Level: developer
3329b004864fSMatthew G. Knepley 
3330b004864fSMatthew G. Knepley .seealso: PetscSectionSymCreate(), PetscSectionSetSym(), PetscSectionGetSym(), PetscSectionSymLabelSetStratum(), PetscSectionGetPointSyms()
3331b004864fSMatthew G. Knepley @*/
3332b004864fSMatthew G. Knepley PetscErrorCode PetscSectionSymCopy(PetscSectionSym sym, PetscSectionSym nsym)
3333b004864fSMatthew G. Knepley {
3334b004864fSMatthew G. Knepley   PetscFunctionBegin;
3335b004864fSMatthew G. Knepley   PetscValidHeaderSpecific(sym, PETSC_SECTION_SYM_CLASSID, 1);
3336b004864fSMatthew G. Knepley   PetscValidHeaderSpecific(nsym, PETSC_SECTION_SYM_CLASSID, 2);
33375f80ce2aSJacob Faibussowitsch   if (sym->ops->copy) CHKERRQ((*sym->ops->copy)(sym, nsym));
3338b004864fSMatthew G. Knepley   PetscFunctionReturn(0);
3339b004864fSMatthew G. Knepley }
3340b004864fSMatthew G. Knepley 
3341b004864fSMatthew G. Knepley /*@
3342b004864fSMatthew G. Knepley   PetscSectionSymDistribute - Distribute the symmetries in accordance with the input SF
3343b004864fSMatthew G. Knepley 
3344b004864fSMatthew G. Knepley   Collective
3345b004864fSMatthew G. Knepley 
3346b004864fSMatthew G. Knepley   Input Parameters:
3347b004864fSMatthew G. Knepley + sym - the PetscSectionSym
3348b004864fSMatthew G. Knepley - migrationSF - the distribution map from roots to leaves
3349b004864fSMatthew G. Knepley 
3350b004864fSMatthew G. Knepley   Output Parameters:
3351b004864fSMatthew G. Knepley . dsym - the redistributed symmetries
3352b004864fSMatthew G. Knepley 
3353b004864fSMatthew G. Knepley   Level: developer
3354b004864fSMatthew G. Knepley 
3355b004864fSMatthew G. Knepley .seealso: PetscSectionSymCreate(), PetscSectionSetSym(), PetscSectionGetSym(), PetscSectionSymLabelSetStratum(), PetscSectionGetPointSyms()
3356b004864fSMatthew G. Knepley @*/
3357b004864fSMatthew G. Knepley PetscErrorCode PetscSectionSymDistribute(PetscSectionSym sym, PetscSF migrationSF, PetscSectionSym *dsym)
3358b004864fSMatthew G. Knepley {
3359b004864fSMatthew G. Knepley   PetscFunctionBegin;
3360b004864fSMatthew G. Knepley   PetscValidHeaderSpecific(sym, PETSC_SECTION_SYM_CLASSID, 1);
3361b004864fSMatthew G. Knepley   PetscValidHeaderSpecific(migrationSF, PETSCSF_CLASSID, 2);
3362b004864fSMatthew G. Knepley   PetscValidPointer(dsym, 3);
33635f80ce2aSJacob Faibussowitsch   if (sym->ops->distribute) CHKERRQ((*sym->ops->distribute)(sym, migrationSF, dsym));
3364b004864fSMatthew G. Knepley   PetscFunctionReturn(0);
3365b004864fSMatthew G. Knepley }
3366b004864fSMatthew G. Knepley 
3367b004864fSMatthew G. Knepley /*@
3368ea844a1aSMatthew Knepley   PetscSectionGetUseFieldOffsets - Get the flag to use field offsets directly in a global section, rather than just the point offset
3369ea844a1aSMatthew Knepley 
3370ea844a1aSMatthew Knepley   Not collective
3371ea844a1aSMatthew Knepley 
3372ea844a1aSMatthew Knepley   Input Parameter:
3373ea844a1aSMatthew Knepley . s - the global PetscSection
3374ea844a1aSMatthew Knepley 
3375ea844a1aSMatthew Knepley   Output Parameters:
3376ea844a1aSMatthew Knepley . flg - the flag
3377ea844a1aSMatthew Knepley 
3378ea844a1aSMatthew Knepley   Level: developer
3379ea844a1aSMatthew Knepley 
3380ea844a1aSMatthew Knepley .seealso: PetscSectionSetChart(), PetscSectionCreate()
3381ea844a1aSMatthew Knepley @*/
3382ea844a1aSMatthew Knepley PetscErrorCode PetscSectionGetUseFieldOffsets(PetscSection s, PetscBool *flg)
3383ea844a1aSMatthew Knepley {
3384ea844a1aSMatthew Knepley   PetscFunctionBegin;
3385ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
3386ea844a1aSMatthew Knepley   *flg = s->useFieldOff;
3387ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
3388ea844a1aSMatthew Knepley }
3389ea844a1aSMatthew Knepley 
3390ea844a1aSMatthew Knepley /*@
3391ea844a1aSMatthew Knepley   PetscSectionSetUseFieldOffsets - Set the flag to use field offsets directly in a global section, rather than just the point offset
3392ea844a1aSMatthew Knepley 
3393ea844a1aSMatthew Knepley   Not collective
3394ea844a1aSMatthew Knepley 
3395ea844a1aSMatthew Knepley   Input Parameters:
3396ea844a1aSMatthew Knepley + s   - the global PetscSection
3397ea844a1aSMatthew Knepley - flg - the flag
3398ea844a1aSMatthew Knepley 
3399ea844a1aSMatthew Knepley   Level: developer
3400ea844a1aSMatthew Knepley 
3401ea844a1aSMatthew Knepley .seealso: PetscSectionGetUseFieldOffsets(), PetscSectionSetChart(), PetscSectionCreate()
3402ea844a1aSMatthew Knepley @*/
3403ea844a1aSMatthew Knepley PetscErrorCode PetscSectionSetUseFieldOffsets(PetscSection s, PetscBool flg)
3404ea844a1aSMatthew Knepley {
3405ea844a1aSMatthew Knepley   PetscFunctionBegin;
3406ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
3407ea844a1aSMatthew Knepley   s->useFieldOff = flg;
3408ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
3409ea844a1aSMatthew Knepley }
3410ea844a1aSMatthew Knepley 
3411ea844a1aSMatthew Knepley #define PetscSectionExpandPoints_Loop(TYPE) \
3412ea844a1aSMatthew Knepley { \
3413ea844a1aSMatthew Knepley   PetscInt i, n, o0, o1, size; \
3414ea844a1aSMatthew Knepley   TYPE *a0 = (TYPE*)origArray, *a1; \
34155f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetStorageSize(s, &size)); \
34165f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(size, &a1)); \
3417ea844a1aSMatthew Knepley   for (i=0; i<npoints; i++) { \
34185f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetOffset(origSection, points_[i], &o0)); \
34195f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetOffset(s, i, &o1)); \
34205f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(s, i, &n)); \
34215f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMemcpy(&a1[o1], &a0[o0], n*unitsize)); \
3422ea844a1aSMatthew Knepley   } \
3423ea844a1aSMatthew Knepley   *newArray = (void*)a1; \
3424ea844a1aSMatthew Knepley }
3425ea844a1aSMatthew Knepley 
3426ea844a1aSMatthew Knepley /*@
3427ea844a1aSMatthew Knepley   PetscSectionExtractDofsFromArray - Extracts elements of an array corresponding to DOFs of specified points.
3428ea844a1aSMatthew Knepley 
3429ea844a1aSMatthew Knepley   Not collective
3430ea844a1aSMatthew Knepley 
3431ea844a1aSMatthew Knepley   Input Parameters:
3432ea844a1aSMatthew Knepley + origSection - the PetscSection describing the layout of the array
3433ea844a1aSMatthew Knepley . dataType - MPI_Datatype describing the data type of the array (currently only MPIU_INT, MPIU_SCALAR, MPIU_REAL)
3434ea844a1aSMatthew Knepley . origArray - the array; its size must be equal to the storage size of origSection
3435ea844a1aSMatthew Knepley - points - IS with points to extract; its indices must lie in the chart of origSection
3436ea844a1aSMatthew Knepley 
3437ea844a1aSMatthew Knepley   Output Parameters:
3438ea844a1aSMatthew Knepley + newSection - the new PetscSection desribing the layout of the new array (with points renumbered 0,1,... but preserving numbers of DOFs)
3439ea844a1aSMatthew Knepley - newArray - the array of the extracted DOFs; its size is the storage size of newSection
3440ea844a1aSMatthew Knepley 
3441ea844a1aSMatthew Knepley   Level: developer
3442ea844a1aSMatthew Knepley 
3443ea844a1aSMatthew Knepley .seealso: PetscSectionGetChart(), PetscSectionGetDof(), PetscSectionGetStorageSize(), PetscSectionCreate()
3444ea844a1aSMatthew Knepley @*/
3445ea844a1aSMatthew Knepley PetscErrorCode PetscSectionExtractDofsFromArray(PetscSection origSection, MPI_Datatype dataType, const void *origArray, IS points, PetscSection *newSection, void *newArray[])
3446ea844a1aSMatthew Knepley {
3447ea844a1aSMatthew Knepley   PetscSection        s;
3448ea844a1aSMatthew Knepley   const PetscInt      *points_;
3449ea844a1aSMatthew Knepley   PetscInt            i, n, npoints, pStart, pEnd;
3450ea844a1aSMatthew Knepley   PetscMPIInt         unitsize;
3451ea844a1aSMatthew Knepley 
3452ea844a1aSMatthew Knepley   PetscFunctionBegin;
3453ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(origSection, PETSC_SECTION_CLASSID, 1);
3454ea844a1aSMatthew Knepley   PetscValidPointer(origArray, 3);
3455ea844a1aSMatthew Knepley   PetscValidHeaderSpecific(points, IS_CLASSID, 4);
3456ea844a1aSMatthew Knepley   if (newSection) PetscValidPointer(newSection, 5);
3457ea844a1aSMatthew Knepley   if (newArray) PetscValidPointer(newArray, 6);
34585f80ce2aSJacob Faibussowitsch   CHKERRMPI(MPI_Type_size(dataType, &unitsize));
34595f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetLocalSize(points, &npoints));
34605f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetIndices(points, &points_));
34615f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetChart(origSection, &pStart, &pEnd));
34625f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionCreate(PETSC_COMM_SELF, &s));
34635f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetChart(s, 0, npoints));
3464ea844a1aSMatthew Knepley   for (i=0; i<npoints; i++) {
34652c71b3e2SJacob Faibussowitsch     PetscCheckFalse(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);
34665f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(origSection, points_[i], &n));
34675f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetDof(s, i, n));
3468ea844a1aSMatthew Knepley   }
34695f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetUp(s));
3470ea844a1aSMatthew Knepley   if (newArray) {
3471ea844a1aSMatthew Knepley     if (dataType == MPIU_INT)           {PetscSectionExpandPoints_Loop(PetscInt);}
3472ea844a1aSMatthew Knepley     else if (dataType == MPIU_SCALAR)   {PetscSectionExpandPoints_Loop(PetscScalar);}
3473ea844a1aSMatthew Knepley     else if (dataType == MPIU_REAL)     {PetscSectionExpandPoints_Loop(PetscReal);}
3474ea844a1aSMatthew Knepley     else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "not implemented for this MPI_Datatype");
3475ea844a1aSMatthew Knepley   }
3476ea844a1aSMatthew Knepley   if (newSection) {
3477ea844a1aSMatthew Knepley     *newSection = s;
3478ea844a1aSMatthew Knepley   } else {
34795f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionDestroy(&s));
3480ea844a1aSMatthew Knepley   }
34815f80ce2aSJacob Faibussowitsch   CHKERRQ(ISRestoreIndices(points, &points_));
3482ea844a1aSMatthew Knepley   PetscFunctionReturn(0);
3483ea844a1aSMatthew Knepley }
3484