xref: /petsc/src/dm/label/dmlabel.c (revision a2b725a8db0d6bf6cc2a1c6df7dd8029aadfff6e)
15fdea053SToby Isaac #include <petscdm.h>
2c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h>   /*I      "petscdmlabel.h"   I*/
3c58f1c22SToby Isaac #include <petsc/private/isimpl.h>        /*I      "petscis.h"        I*/
4c58f1c22SToby Isaac #include <petscsf.h>
5c58f1c22SToby Isaac 
6c58f1c22SToby Isaac /*@C
7c58f1c22SToby Isaac   DMLabelCreate - Create a DMLabel object, which is a multimap
8c58f1c22SToby Isaac 
9d67d17b1SMatthew G. Knepley   Input parameters:
10d67d17b1SMatthew G. Knepley + comm - The communicator, usually PETSC_COMM_SELF
11d67d17b1SMatthew G. Knepley - name - The label name
12c58f1c22SToby Isaac 
13c58f1c22SToby Isaac   Output parameter:
14c58f1c22SToby Isaac . label - The DMLabel
15c58f1c22SToby Isaac 
16c58f1c22SToby Isaac   Level: beginner
17c58f1c22SToby Isaac 
18c58f1c22SToby Isaac .seealso: DMLabelDestroy()
19c58f1c22SToby Isaac @*/
20d67d17b1SMatthew G. Knepley PetscErrorCode DMLabelCreate(MPI_Comm comm, const char name[], DMLabel *label)
21c58f1c22SToby Isaac {
22c58f1c22SToby Isaac   PetscErrorCode ierr;
23c58f1c22SToby Isaac 
24c58f1c22SToby Isaac   PetscFunctionBegin;
25d67d17b1SMatthew G. Knepley   PetscValidPointer(label,2);
26d67d17b1SMatthew G. Knepley   ierr = DMInitializePackage();CHKERRQ(ierr);
27c58f1c22SToby Isaac 
28d67d17b1SMatthew G. Knepley   ierr = PetscHeaderCreate(*label,DMLABEL_CLASSID,"DMLabel","DMLabel","DM",comm,DMLabelDestroy,DMLabelView);CHKERRQ(ierr);
29d67d17b1SMatthew G. Knepley 
30c58f1c22SToby Isaac   (*label)->numStrata      = 0;
315aa44df4SToby Isaac   (*label)->defaultValue   = -1;
32c58f1c22SToby Isaac   (*label)->stratumValues  = NULL;
33ad8374ffSToby Isaac   (*label)->validIS        = NULL;
34c58f1c22SToby Isaac   (*label)->stratumSizes   = NULL;
35c58f1c22SToby Isaac   (*label)->points         = NULL;
36c58f1c22SToby Isaac   (*label)->ht             = NULL;
37c58f1c22SToby Isaac   (*label)->pStart         = -1;
38c58f1c22SToby Isaac   (*label)->pEnd           = -1;
39c58f1c22SToby Isaac   (*label)->bt             = NULL;
40b9907514SLisandro Dalcin   ierr = PetscHMapICreate(&(*label)->hmap);CHKERRQ(ierr);
41d67d17b1SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *label, name);CHKERRQ(ierr);
42c58f1c22SToby Isaac   PetscFunctionReturn(0);
43c58f1c22SToby Isaac }
44c58f1c22SToby Isaac 
45c58f1c22SToby Isaac /*
46c58f1c22SToby Isaac   DMLabelMakeValid_Private - Transfer stratum data from the hash format to the sorted list format
47c58f1c22SToby Isaac 
48c58f1c22SToby Isaac   Input parameter:
49c58f1c22SToby Isaac + label - The DMLabel
50c58f1c22SToby Isaac - v - The stratum value
51c58f1c22SToby Isaac 
52c58f1c22SToby Isaac   Output parameter:
53c58f1c22SToby Isaac . label - The DMLabel with stratum in sorted list format
54c58f1c22SToby Isaac 
55c58f1c22SToby Isaac   Level: developer
56c58f1c22SToby Isaac 
57c58f1c22SToby Isaac .seealso: DMLabelCreate()
58c58f1c22SToby Isaac */
59c58f1c22SToby Isaac static PetscErrorCode DMLabelMakeValid_Private(DMLabel label, PetscInt v)
60c58f1c22SToby Isaac {
61b9907514SLisandro Dalcin   PetscInt       off = 0, *pointArray, p;
62c58f1c22SToby Isaac   PetscErrorCode ierr;
63c58f1c22SToby Isaac 
64b9907514SLisandro Dalcin   if (PetscLikely(v >= 0 && v < label->numStrata) && label->validIS[v]) return 0;
65c58f1c22SToby Isaac   PetscFunctionBegin;
660c3c4a36SLisandro Dalcin   if (v < 0 || v >= label->numStrata) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Trying to access invalid stratum %D in DMLabelMakeValid_Private\n", v);
67e8f14785SLisandro Dalcin   ierr = PetscHSetIGetSize(label->ht[v], &label->stratumSizes[v]);CHKERRQ(ierr);
68ad8374ffSToby Isaac   ierr = PetscMalloc1(label->stratumSizes[v], &pointArray);CHKERRQ(ierr);
69e8f14785SLisandro Dalcin   ierr = PetscHSetIGetElems(label->ht[v], &off, pointArray);CHKERRQ(ierr);
70b9907514SLisandro Dalcin   ierr = PetscHSetIClear(label->ht[v]);CHKERRQ(ierr);
71ad8374ffSToby Isaac   ierr = PetscSortInt(label->stratumSizes[v], pointArray);CHKERRQ(ierr);
72c58f1c22SToby Isaac   if (label->bt) {
73c58f1c22SToby Isaac     for (p = 0; p < label->stratumSizes[v]; ++p) {
74ad8374ffSToby Isaac       const PetscInt point = pointArray[p];
75c58f1c22SToby Isaac       if ((point < label->pStart) || (point >= label->pEnd)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label point %D is not in [%D, %D)", point, label->pStart, label->pEnd);
76c58f1c22SToby Isaac       ierr = PetscBTSet(label->bt, point - label->pStart);CHKERRQ(ierr);
77c58f1c22SToby Isaac     }
78c58f1c22SToby Isaac   }
79ad8374ffSToby Isaac   ierr = ISCreateGeneral(PETSC_COMM_SELF,label->stratumSizes[v],pointArray,PETSC_OWN_POINTER,&(label->points[v]));CHKERRQ(ierr);
80ad8374ffSToby Isaac   ierr = PetscObjectSetName((PetscObject) (label->points[v]), "indices");CHKERRQ(ierr);
81ad8374ffSToby Isaac   label->validIS[v] = PETSC_TRUE;
82d67d17b1SMatthew G. Knepley   ierr = PetscObjectStateIncrease((PetscObject) label);CHKERRQ(ierr);
83c58f1c22SToby Isaac   PetscFunctionReturn(0);
84c58f1c22SToby Isaac }
85c58f1c22SToby Isaac 
86c58f1c22SToby Isaac /*
87c58f1c22SToby Isaac   DMLabelMakeAllValid_Private - Transfer all strata from the hash format to the sorted list format
88c58f1c22SToby Isaac 
89c58f1c22SToby Isaac   Input parameter:
90c58f1c22SToby Isaac . label - The DMLabel
91c58f1c22SToby Isaac 
92c58f1c22SToby Isaac   Output parameter:
93c58f1c22SToby Isaac . label - The DMLabel with all strata in sorted list format
94c58f1c22SToby Isaac 
95c58f1c22SToby Isaac   Level: developer
96c58f1c22SToby Isaac 
97c58f1c22SToby Isaac .seealso: DMLabelCreate()
98c58f1c22SToby Isaac */
99c58f1c22SToby Isaac static PetscErrorCode DMLabelMakeAllValid_Private(DMLabel label)
100c58f1c22SToby Isaac {
101c58f1c22SToby Isaac   PetscInt       v;
102c58f1c22SToby Isaac   PetscErrorCode ierr;
103c58f1c22SToby Isaac 
104c58f1c22SToby Isaac   PetscFunctionBegin;
105c58f1c22SToby Isaac   for (v = 0; v < label->numStrata; v++){
106c58f1c22SToby Isaac     ierr = DMLabelMakeValid_Private(label, v);CHKERRQ(ierr);
107c58f1c22SToby Isaac   }
108c58f1c22SToby Isaac   PetscFunctionReturn(0);
109c58f1c22SToby Isaac }
110c58f1c22SToby Isaac 
111c58f1c22SToby Isaac /*
112c58f1c22SToby Isaac   DMLabelMakeInvalid_Private - Transfer stratum data from the sorted list format to the hash format
113c58f1c22SToby Isaac 
114c58f1c22SToby Isaac   Input parameter:
115c58f1c22SToby Isaac + label - The DMLabel
116c58f1c22SToby Isaac - v - The stratum value
117c58f1c22SToby Isaac 
118c58f1c22SToby Isaac   Output parameter:
119c58f1c22SToby Isaac . label - The DMLabel with stratum in hash format
120c58f1c22SToby Isaac 
121c58f1c22SToby Isaac   Level: developer
122c58f1c22SToby Isaac 
123c58f1c22SToby Isaac .seealso: DMLabelCreate()
124c58f1c22SToby Isaac */
125c58f1c22SToby Isaac static PetscErrorCode DMLabelMakeInvalid_Private(DMLabel label, PetscInt v)
126c58f1c22SToby Isaac {
127c58f1c22SToby Isaac   PetscInt       p;
128ad8374ffSToby Isaac   const PetscInt *points;
129c58f1c22SToby Isaac   PetscErrorCode ierr;
130c58f1c22SToby Isaac 
131b9907514SLisandro Dalcin   if (PetscLikely(v >= 0 && v < label->numStrata) && !label->validIS[v]) return 0;
132c58f1c22SToby Isaac   PetscFunctionBegin;
1330c3c4a36SLisandro Dalcin   if (v < 0 || v >= label->numStrata) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Trying to access invalid stratum %D in DMLabelMakeInvalid_Private\n", v);
134ad8374ffSToby Isaac   if (label->points[v]) {
135ad8374ffSToby Isaac     ierr = ISGetIndices(label->points[v], &points);CHKERRQ(ierr);
136e8f14785SLisandro Dalcin     for (p = 0; p < label->stratumSizes[v]; ++p) {
137e8f14785SLisandro Dalcin       ierr = PetscHSetIAdd(label->ht[v], points[p]);CHKERRQ(ierr);
138e8f14785SLisandro Dalcin     }
139ad8374ffSToby Isaac     ierr = ISRestoreIndices(label->points[v],&points);CHKERRQ(ierr);
140ad8374ffSToby Isaac     ierr = ISDestroy(&(label->points[v]));CHKERRQ(ierr);
141ad8374ffSToby Isaac   }
142ad8374ffSToby Isaac   label->validIS[v] = PETSC_FALSE;
143c58f1c22SToby Isaac   PetscFunctionReturn(0);
144c58f1c22SToby Isaac }
145c58f1c22SToby Isaac 
146b9907514SLisandro Dalcin #if !defined(DMLABEL_LOOKUP_THRESHOLD)
147b9907514SLisandro Dalcin #define DMLABEL_LOOKUP_THRESHOLD 16
148b9907514SLisandro Dalcin #endif
149b9907514SLisandro Dalcin 
1500c3c4a36SLisandro Dalcin PETSC_STATIC_INLINE PetscErrorCode DMLabelLookupStratum(DMLabel label, PetscInt value, PetscInt *index)
1510c3c4a36SLisandro Dalcin {
1520c3c4a36SLisandro Dalcin   PetscInt       v;
153b9907514SLisandro Dalcin   PetscErrorCode ierr;
1540e79e033SBarry Smith 
1550c3c4a36SLisandro Dalcin   PetscFunctionBegin;
1560e79e033SBarry Smith   *index = -1;
157b9907514SLisandro Dalcin   if (label->numStrata <= DMLABEL_LOOKUP_THRESHOLD) {
158b9907514SLisandro Dalcin     for (v = 0; v < label->numStrata; ++v)
159b9907514SLisandro Dalcin       if (label->stratumValues[v] == value) {*index = v; break;}
160b9907514SLisandro Dalcin   } else {
161b9907514SLisandro Dalcin     ierr = PetscHMapIGet(label->hmap, value, index);CHKERRQ(ierr);
1620e79e033SBarry Smith   }
16390e9b2aeSLisandro Dalcin #if defined(PETSC_USE_DEBUG)
16490e9b2aeSLisandro Dalcin   { /* Check strata hash map consistency */
16590e9b2aeSLisandro Dalcin     PetscInt len, loc = -1;
16690e9b2aeSLisandro Dalcin     ierr = PetscHMapIGetSize(label->hmap, &len);CHKERRQ(ierr);
16790e9b2aeSLisandro Dalcin     if (len != label->numStrata) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Inconsistent strata hash map size");
16890e9b2aeSLisandro Dalcin     if (label->numStrata <= DMLABEL_LOOKUP_THRESHOLD) {
16990e9b2aeSLisandro Dalcin       ierr = PetscHMapIGet(label->hmap, value, &loc);CHKERRQ(ierr);
17090e9b2aeSLisandro Dalcin     } else {
17190e9b2aeSLisandro Dalcin       for (v = 0; v < label->numStrata; ++v)
17290e9b2aeSLisandro Dalcin         if (label->stratumValues[v] == value) {loc = v; break;}
17390e9b2aeSLisandro Dalcin     }
17490e9b2aeSLisandro Dalcin     if (loc != *index) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Inconsistent strata hash map lookup");
17590e9b2aeSLisandro Dalcin   }
17690e9b2aeSLisandro Dalcin #endif
1770c3c4a36SLisandro Dalcin   PetscFunctionReturn(0);
1780c3c4a36SLisandro Dalcin }
1790c3c4a36SLisandro Dalcin 
180b9907514SLisandro Dalcin PETSC_STATIC_INLINE PetscErrorCode DMLabelNewStratum(DMLabel label, PetscInt value, PetscInt *index)
181c58f1c22SToby Isaac {
182b9907514SLisandro Dalcin   PetscInt       v;
183b9907514SLisandro Dalcin   PetscInt      *tmpV;
184b9907514SLisandro Dalcin   PetscInt      *tmpS;
185b9907514SLisandro Dalcin   PetscHSetI    *tmpH, ht;
186b9907514SLisandro Dalcin   IS            *tmpP, is;
187c58f1c22SToby Isaac   PetscBool     *tmpB;
188b9907514SLisandro Dalcin   PetscHMapI     hmap = label->hmap;
189c58f1c22SToby Isaac   PetscErrorCode ierr;
190c58f1c22SToby Isaac 
191c58f1c22SToby Isaac   PetscFunctionBegin;
192b9907514SLisandro Dalcin   v    = label->numStrata;
193b9907514SLisandro Dalcin   tmpV = label->stratumValues;
194b9907514SLisandro Dalcin   tmpS = label->stratumSizes;
195b9907514SLisandro Dalcin   tmpH = label->ht;
196b9907514SLisandro Dalcin   tmpP = label->points;
197b9907514SLisandro Dalcin   tmpB = label->validIS;
1988e1f8cf0SLisandro Dalcin   { /* TODO: PetscRealloc() is broken, use malloc+memcpy+free  */
1998e1f8cf0SLisandro Dalcin     PetscInt   *oldV = tmpV;
2008e1f8cf0SLisandro Dalcin     PetscInt   *oldS = tmpS;
2018e1f8cf0SLisandro Dalcin     PetscHSetI *oldH = tmpH;
2028e1f8cf0SLisandro Dalcin     IS         *oldP = tmpP;
2038e1f8cf0SLisandro Dalcin     PetscBool  *oldB = tmpB;
2048e1f8cf0SLisandro Dalcin     ierr = PetscMalloc((v+1)*sizeof(*tmpV), &tmpV);CHKERRQ(ierr);
2058e1f8cf0SLisandro Dalcin     ierr = PetscMalloc((v+1)*sizeof(*tmpS), &tmpS);CHKERRQ(ierr);
2068e1f8cf0SLisandro Dalcin     ierr = PetscMalloc((v+1)*sizeof(*tmpH), &tmpH);CHKERRQ(ierr);
2078e1f8cf0SLisandro Dalcin     ierr = PetscMalloc((v+1)*sizeof(*tmpP), &tmpP);CHKERRQ(ierr);
2088e1f8cf0SLisandro Dalcin     ierr = PetscMalloc((v+1)*sizeof(*tmpB), &tmpB);CHKERRQ(ierr);
2098e1f8cf0SLisandro Dalcin     ierr = PetscMemcpy(tmpV, oldV, v*sizeof(*tmpV));CHKERRQ(ierr);
2108e1f8cf0SLisandro Dalcin     ierr = PetscMemcpy(tmpS, oldS, v*sizeof(*tmpS));CHKERRQ(ierr);
2118e1f8cf0SLisandro Dalcin     ierr = PetscMemcpy(tmpH, oldH, v*sizeof(*tmpH));CHKERRQ(ierr);
2128e1f8cf0SLisandro Dalcin     ierr = PetscMemcpy(tmpP, oldP, v*sizeof(*tmpP));CHKERRQ(ierr);
2138e1f8cf0SLisandro Dalcin     ierr = PetscMemcpy(tmpB, oldB, v*sizeof(*tmpB));CHKERRQ(ierr);
2148e1f8cf0SLisandro Dalcin     ierr = PetscFree(oldV);CHKERRQ(ierr);
2158e1f8cf0SLisandro Dalcin     ierr = PetscFree(oldS);CHKERRQ(ierr);
2168e1f8cf0SLisandro Dalcin     ierr = PetscFree(oldH);CHKERRQ(ierr);
2178e1f8cf0SLisandro Dalcin     ierr = PetscFree(oldP);CHKERRQ(ierr);
2188e1f8cf0SLisandro Dalcin     ierr = PetscFree(oldB);CHKERRQ(ierr);
2198e1f8cf0SLisandro Dalcin   }
220b9907514SLisandro Dalcin   label->numStrata     = v+1;
221c58f1c22SToby Isaac   label->stratumValues = tmpV;
222c58f1c22SToby Isaac   label->stratumSizes  = tmpS;
223c58f1c22SToby Isaac   label->ht            = tmpH;
224c58f1c22SToby Isaac   label->points        = tmpP;
225ad8374ffSToby Isaac   label->validIS       = tmpB;
226b9907514SLisandro Dalcin   ierr = PetscHSetICreate(&ht);CHKERRQ(ierr);
227b9907514SLisandro Dalcin   ierr = ISCreateStride(PETSC_COMM_SELF,0,0,1,&is);CHKERRQ(ierr);
228b9907514SLisandro Dalcin   ierr = PetscHMapISet(hmap, value, v);CHKERRQ(ierr);
229b9907514SLisandro Dalcin   tmpV[v] = value;
230b9907514SLisandro Dalcin   tmpS[v] = 0;
231b9907514SLisandro Dalcin   tmpH[v] = ht;
232b9907514SLisandro Dalcin   tmpP[v] = is;
233b9907514SLisandro Dalcin   tmpB[v] = PETSC_TRUE;
2340c3c4a36SLisandro Dalcin   *index = v;
2350c3c4a36SLisandro Dalcin   PetscFunctionReturn(0);
2360c3c4a36SLisandro Dalcin }
2370c3c4a36SLisandro Dalcin 
238b9907514SLisandro Dalcin PETSC_STATIC_INLINE PetscErrorCode DMLabelLookupAddStratum(DMLabel label, PetscInt value, PetscInt *index)
239b9907514SLisandro Dalcin {
240b9907514SLisandro Dalcin   PetscErrorCode ierr;
241b9907514SLisandro Dalcin   PetscFunctionBegin;
242b9907514SLisandro Dalcin   ierr = DMLabelLookupStratum(label, value, index);CHKERRQ(ierr);
243b9907514SLisandro Dalcin   if (*index < 0) {ierr = DMLabelNewStratum(label, value, index);CHKERRQ(ierr);}
244b9907514SLisandro Dalcin   PetscFunctionReturn(0);
245b9907514SLisandro Dalcin }
246b9907514SLisandro Dalcin 
247b9907514SLisandro Dalcin /*@
248b9907514SLisandro Dalcin   DMLabelAddStratum - Adds a new stratum value in a DMLabel
249b9907514SLisandro Dalcin 
250b9907514SLisandro Dalcin   Input Parameter:
251b9907514SLisandro Dalcin + label - The DMLabel
252b9907514SLisandro Dalcin - value - The stratum value
253b9907514SLisandro Dalcin 
254b9907514SLisandro Dalcin   Level: beginner
255b9907514SLisandro Dalcin 
256b9907514SLisandro Dalcin .seealso:  DMLabelCreate(), DMLabelDestroy()
257b9907514SLisandro Dalcin @*/
2580c3c4a36SLisandro Dalcin PetscErrorCode DMLabelAddStratum(DMLabel label, PetscInt value)
2590c3c4a36SLisandro Dalcin {
2600c3c4a36SLisandro Dalcin   PetscInt       v;
2610c3c4a36SLisandro Dalcin   PetscErrorCode ierr;
2620c3c4a36SLisandro Dalcin 
2630c3c4a36SLisandro Dalcin   PetscFunctionBegin;
264d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
265b9907514SLisandro Dalcin   ierr = DMLabelLookupAddStratum(label, value, &v);CHKERRQ(ierr);
266b9907514SLisandro Dalcin   PetscFunctionReturn(0);
267b9907514SLisandro Dalcin }
268b9907514SLisandro Dalcin 
269b9907514SLisandro Dalcin /*@
270b9907514SLisandro Dalcin   DMLabelAddStrata - Adds new stratum values in a DMLabel
271b9907514SLisandro Dalcin 
272b9907514SLisandro Dalcin   Input Parameter:
273b9907514SLisandro Dalcin + label - The DMLabel
274b9907514SLisandro Dalcin . numStrata - The number of stratum values
275b9907514SLisandro Dalcin - stratumValues - The stratum values
276b9907514SLisandro Dalcin 
277b9907514SLisandro Dalcin   Level: beginner
278b9907514SLisandro Dalcin 
279b9907514SLisandro Dalcin .seealso:  DMLabelCreate(), DMLabelDestroy()
280b9907514SLisandro Dalcin @*/
281b9907514SLisandro Dalcin PetscErrorCode DMLabelAddStrata(DMLabel label, PetscInt numStrata, const PetscInt stratumValues[])
282b9907514SLisandro Dalcin {
283b9907514SLisandro Dalcin   PetscInt       *values, v;
284b9907514SLisandro Dalcin   PetscErrorCode ierr;
285b9907514SLisandro Dalcin 
286b9907514SLisandro Dalcin   PetscFunctionBegin;
287b9907514SLisandro Dalcin   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
288b9907514SLisandro Dalcin   if (numStrata) PetscValidIntPointer(stratumValues, 3);
289b9907514SLisandro Dalcin   ierr = PetscMalloc1(numStrata, &values);CHKERRQ(ierr);
290b9907514SLisandro Dalcin   ierr = PetscMemcpy(values, stratumValues, numStrata*sizeof(PetscInt));CHKERRQ(ierr);
291b9907514SLisandro Dalcin   ierr = PetscSortRemoveDupsInt(&numStrata, values);CHKERRQ(ierr);
292b9907514SLisandro Dalcin   if (!label->numStrata) { /* Fast preallocation */
293b9907514SLisandro Dalcin     PetscInt   *tmpV;
294b9907514SLisandro Dalcin     PetscInt   *tmpS;
295b9907514SLisandro Dalcin     PetscHSetI *tmpH, ht;
296b9907514SLisandro Dalcin     IS         *tmpP, is;
297b9907514SLisandro Dalcin     PetscBool  *tmpB;
298b9907514SLisandro Dalcin     PetscHMapI  hmap = label->hmap;
299b9907514SLisandro Dalcin 
300b9907514SLisandro Dalcin     ierr = PetscMalloc1(numStrata, &tmpV);CHKERRQ(ierr);
301b9907514SLisandro Dalcin     ierr = PetscMalloc1(numStrata, &tmpS);CHKERRQ(ierr);
302b9907514SLisandro Dalcin     ierr = PetscMalloc1(numStrata, &tmpH);CHKERRQ(ierr);
303b9907514SLisandro Dalcin     ierr = PetscMalloc1(numStrata, &tmpP);CHKERRQ(ierr);
304b9907514SLisandro Dalcin     ierr = PetscMalloc1(numStrata, &tmpB);CHKERRQ(ierr);
305b9907514SLisandro Dalcin     label->numStrata     = numStrata;
306b9907514SLisandro Dalcin     label->stratumValues = tmpV;
307b9907514SLisandro Dalcin     label->stratumSizes  = tmpS;
308b9907514SLisandro Dalcin     label->ht            = tmpH;
309b9907514SLisandro Dalcin     label->points        = tmpP;
310b9907514SLisandro Dalcin     label->validIS       = tmpB;
311b9907514SLisandro Dalcin     for (v = 0; v < numStrata; ++v) {
312b9907514SLisandro Dalcin       ierr = PetscHSetICreate(&ht);CHKERRQ(ierr);
313b9907514SLisandro Dalcin       ierr = ISCreateStride(PETSC_COMM_SELF,0,0,1,&is);CHKERRQ(ierr);
314b9907514SLisandro Dalcin       ierr = PetscHMapISet(hmap, values[v], v);CHKERRQ(ierr);
315b9907514SLisandro Dalcin       tmpV[v] = values[v];
316b9907514SLisandro Dalcin       tmpS[v] = 0;
317b9907514SLisandro Dalcin       tmpH[v] = ht;
318b9907514SLisandro Dalcin       tmpP[v] = is;
319b9907514SLisandro Dalcin       tmpB[v] = PETSC_TRUE;
320b9907514SLisandro Dalcin     }
321b9907514SLisandro Dalcin   } else {
322b9907514SLisandro Dalcin     for (v = 0; v < numStrata; ++v) {
323b9907514SLisandro Dalcin       ierr = DMLabelAddStratum(label, values[v]);CHKERRQ(ierr);
324b9907514SLisandro Dalcin     }
325b9907514SLisandro Dalcin   }
326b9907514SLisandro Dalcin   ierr = PetscFree(values);CHKERRQ(ierr);
327b9907514SLisandro Dalcin   PetscFunctionReturn(0);
328b9907514SLisandro Dalcin }
329b9907514SLisandro Dalcin 
330b9907514SLisandro Dalcin /*@
331b9907514SLisandro Dalcin   DMLabelAddStrataIS - Adds new stratum values in a DMLabel
332b9907514SLisandro Dalcin 
333b9907514SLisandro Dalcin   Input Parameter:
334b9907514SLisandro Dalcin + label - The DMLabel
335b9907514SLisandro Dalcin - valueIS - Index set with stratum values
336b9907514SLisandro Dalcin 
337b9907514SLisandro Dalcin   Level: beginner
338b9907514SLisandro Dalcin 
339b9907514SLisandro Dalcin .seealso:  DMLabelCreate(), DMLabelDestroy()
340b9907514SLisandro Dalcin @*/
341b9907514SLisandro Dalcin PetscErrorCode DMLabelAddStrataIS(DMLabel label, IS valueIS)
342b9907514SLisandro Dalcin {
343b9907514SLisandro Dalcin   PetscInt       numStrata;
344b9907514SLisandro Dalcin   const PetscInt *stratumValues;
345b9907514SLisandro Dalcin   PetscErrorCode ierr;
346b9907514SLisandro Dalcin 
347b9907514SLisandro Dalcin   PetscFunctionBegin;
348b9907514SLisandro Dalcin   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
349b9907514SLisandro Dalcin   PetscValidHeaderSpecific(valueIS, IS_CLASSID, 2);
350b9907514SLisandro Dalcin   ierr = ISGetLocalSize(valueIS, &numStrata);CHKERRQ(ierr);
351b9907514SLisandro Dalcin   ierr = ISGetIndices(valueIS, &stratumValues);CHKERRQ(ierr);
352b9907514SLisandro Dalcin   ierr = DMLabelAddStrata(label, numStrata, stratumValues);CHKERRQ(ierr);
353c58f1c22SToby Isaac   PetscFunctionReturn(0);
354c58f1c22SToby Isaac }
355c58f1c22SToby Isaac 
356c58f1c22SToby Isaac static PetscErrorCode DMLabelView_Ascii(DMLabel label, PetscViewer viewer)
357c58f1c22SToby Isaac {
358c58f1c22SToby Isaac   PetscInt       v;
359c58f1c22SToby Isaac   PetscMPIInt    rank;
360c58f1c22SToby Isaac   PetscErrorCode ierr;
361c58f1c22SToby Isaac 
362c58f1c22SToby Isaac   PetscFunctionBegin;
363c58f1c22SToby Isaac   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank);CHKERRQ(ierr);
364c58f1c22SToby Isaac   ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
365c58f1c22SToby Isaac   if (label) {
366d67d17b1SMatthew G. Knepley     const char *name;
367d67d17b1SMatthew G. Knepley 
368d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) label, &name);CHKERRQ(ierr);
369d67d17b1SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Label '%s':\n", name);CHKERRQ(ierr);
370c58f1c22SToby Isaac     if (label->bt) {ierr = PetscViewerASCIIPrintf(viewer, "  Index has been calculated in [%D, %D)\n", label->pStart, label->pEnd);CHKERRQ(ierr);}
371c58f1c22SToby Isaac     for (v = 0; v < label->numStrata; ++v) {
372c58f1c22SToby Isaac       const PetscInt value = label->stratumValues[v];
373ad8374ffSToby Isaac       const PetscInt *points;
374c58f1c22SToby Isaac       PetscInt       p;
375c58f1c22SToby Isaac 
376ad8374ffSToby Isaac       ierr = ISGetIndices(label->points[v], &points);CHKERRQ(ierr);
377c58f1c22SToby Isaac       for (p = 0; p < label->stratumSizes[v]; ++p) {
378ad8374ffSToby Isaac         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D (%D)\n", rank, points[p], value);CHKERRQ(ierr);
379c58f1c22SToby Isaac       }
380ad8374ffSToby Isaac       ierr = ISRestoreIndices(label->points[v],&points);CHKERRQ(ierr);
381c58f1c22SToby Isaac     }
382c58f1c22SToby Isaac   }
383c58f1c22SToby Isaac   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
384c58f1c22SToby Isaac   ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
385c58f1c22SToby Isaac   PetscFunctionReturn(0);
386c58f1c22SToby Isaac }
387c58f1c22SToby Isaac 
388c58f1c22SToby Isaac /*@C
389c58f1c22SToby Isaac   DMLabelView - View the label
390c58f1c22SToby Isaac 
391c58f1c22SToby Isaac   Input Parameters:
392c58f1c22SToby Isaac + label - The DMLabel
393c58f1c22SToby Isaac - viewer - The PetscViewer
394c58f1c22SToby Isaac 
395c58f1c22SToby Isaac   Level: intermediate
396c58f1c22SToby Isaac 
397c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMLabelDestroy()
398c58f1c22SToby Isaac @*/
399c58f1c22SToby Isaac PetscErrorCode DMLabelView(DMLabel label, PetscViewer viewer)
400c58f1c22SToby Isaac {
401c58f1c22SToby Isaac   PetscBool      iascii;
402c58f1c22SToby Isaac   PetscErrorCode ierr;
403c58f1c22SToby Isaac 
404c58f1c22SToby Isaac   PetscFunctionBegin;
405d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
406b9907514SLisandro Dalcin   if (!viewer) {ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)label), &viewer);CHKERRQ(ierr);}
407c58f1c22SToby Isaac   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
408c58f1c22SToby Isaac   if (label) {ierr = DMLabelMakeAllValid_Private(label);CHKERRQ(ierr);}
409c58f1c22SToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
410c58f1c22SToby Isaac   if (iascii) {
411c58f1c22SToby Isaac     ierr = DMLabelView_Ascii(label, viewer);CHKERRQ(ierr);
412c58f1c22SToby Isaac   }
413c58f1c22SToby Isaac   PetscFunctionReturn(0);
414c58f1c22SToby Isaac }
415c58f1c22SToby Isaac 
41684f0b6dfSMatthew G. Knepley /*@
417d67d17b1SMatthew G. Knepley   DMLabelReset - Destroys internal data structures in a DMLabel
418d67d17b1SMatthew G. Knepley 
419d67d17b1SMatthew G. Knepley   Input Parameter:
420d67d17b1SMatthew G. Knepley . label - The DMLabel
421d67d17b1SMatthew G. Knepley 
422d67d17b1SMatthew G. Knepley   Level: beginner
423d67d17b1SMatthew G. Knepley 
424d67d17b1SMatthew G. Knepley .seealso: DMLabelDestroy(), DMLabelCreate()
425d67d17b1SMatthew G. Knepley @*/
426d67d17b1SMatthew G. Knepley PetscErrorCode DMLabelReset(DMLabel label)
427d67d17b1SMatthew G. Knepley {
428d67d17b1SMatthew G. Knepley   PetscInt       v;
429d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
430d67d17b1SMatthew G. Knepley 
431d67d17b1SMatthew G. Knepley   PetscFunctionBegin;
432d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
433d67d17b1SMatthew G. Knepley   for (v = 0; v < label->numStrata; ++v) {
434d67d17b1SMatthew G. Knepley     ierr = PetscHSetIDestroy(&label->ht[v]);CHKERRQ(ierr);
435d67d17b1SMatthew G. Knepley     ierr = ISDestroy(&label->points[v]);CHKERRQ(ierr);
436d67d17b1SMatthew G. Knepley   }
437b9907514SLisandro Dalcin   label->numStrata = 0;
438b9907514SLisandro Dalcin   ierr = PetscFree(label->stratumValues);CHKERRQ(ierr);
439b9907514SLisandro Dalcin   ierr = PetscFree(label->stratumSizes);CHKERRQ(ierr);
440d67d17b1SMatthew G. Knepley   ierr = PetscFree(label->ht);CHKERRQ(ierr);
441d67d17b1SMatthew G. Knepley   ierr = PetscFree(label->points);CHKERRQ(ierr);
442d67d17b1SMatthew G. Knepley   ierr = PetscFree(label->validIS);CHKERRQ(ierr);
443b9907514SLisandro Dalcin   ierr = PetscHMapIReset(label->hmap);CHKERRQ(ierr);
444b9907514SLisandro Dalcin   label->pStart = -1;
445b9907514SLisandro Dalcin   label->pEnd   = -1;
446d67d17b1SMatthew G. Knepley   ierr = PetscBTDestroy(&label->bt);CHKERRQ(ierr);
447d67d17b1SMatthew G. Knepley   PetscFunctionReturn(0);
448d67d17b1SMatthew G. Knepley }
449d67d17b1SMatthew G. Knepley 
450d67d17b1SMatthew G. Knepley /*@
45184f0b6dfSMatthew G. Knepley   DMLabelDestroy - Destroys a DMLabel
45284f0b6dfSMatthew G. Knepley 
45384f0b6dfSMatthew G. Knepley   Input Parameter:
45484f0b6dfSMatthew G. Knepley . label - The DMLabel
45584f0b6dfSMatthew G. Knepley 
45684f0b6dfSMatthew G. Knepley   Level: beginner
45784f0b6dfSMatthew G. Knepley 
458d67d17b1SMatthew G. Knepley .seealso: DMLabelReset(), DMLabelCreate()
45984f0b6dfSMatthew G. Knepley @*/
460c58f1c22SToby Isaac PetscErrorCode DMLabelDestroy(DMLabel *label)
461c58f1c22SToby Isaac {
462c58f1c22SToby Isaac   PetscErrorCode ierr;
463c58f1c22SToby Isaac 
464c58f1c22SToby Isaac   PetscFunctionBegin;
465d67d17b1SMatthew G. Knepley   if (!*label) PetscFunctionReturn(0);
466d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific((*label),DMLABEL_CLASSID,1);
467b9907514SLisandro Dalcin   if (--((PetscObject)(*label))->refct > 0) {*label = NULL; PetscFunctionReturn(0);}
468d67d17b1SMatthew G. Knepley   ierr = DMLabelReset(*label);CHKERRQ(ierr);
469b9907514SLisandro Dalcin   ierr = PetscHMapIDestroy(&(*label)->hmap);CHKERRQ(ierr);
470d67d17b1SMatthew G. Knepley   ierr = PetscHeaderDestroy(label);CHKERRQ(ierr);
471c58f1c22SToby Isaac   PetscFunctionReturn(0);
472c58f1c22SToby Isaac }
473c58f1c22SToby Isaac 
47484f0b6dfSMatthew G. Knepley /*@
47584f0b6dfSMatthew G. Knepley   DMLabelDuplicate - Duplicates a DMLabel
47684f0b6dfSMatthew G. Knepley 
47784f0b6dfSMatthew G. Knepley   Input Parameter:
47884f0b6dfSMatthew G. Knepley . label - The DMLabel
47984f0b6dfSMatthew G. Knepley 
48084f0b6dfSMatthew G. Knepley   Output Parameter:
48184f0b6dfSMatthew G. Knepley . labelnew - location to put new vector
48284f0b6dfSMatthew G. Knepley 
48384f0b6dfSMatthew G. Knepley   Level: intermediate
48484f0b6dfSMatthew G. Knepley 
48584f0b6dfSMatthew G. Knepley .seealso: DMLabelCreate(), DMLabelDestroy()
48684f0b6dfSMatthew G. Knepley @*/
487c58f1c22SToby Isaac PetscErrorCode DMLabelDuplicate(DMLabel label, DMLabel *labelnew)
488c58f1c22SToby Isaac {
489d67d17b1SMatthew G. Knepley   const char    *name;
490ad8374ffSToby Isaac   PetscInt       v;
491c58f1c22SToby Isaac   PetscErrorCode ierr;
492c58f1c22SToby Isaac 
493c58f1c22SToby Isaac   PetscFunctionBegin;
494d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
495c58f1c22SToby Isaac   ierr = DMLabelMakeAllValid_Private(label);CHKERRQ(ierr);
496d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &name);CHKERRQ(ierr);
497d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PetscObjectComm((PetscObject) label), name, labelnew);CHKERRQ(ierr);
498c58f1c22SToby Isaac 
499c58f1c22SToby Isaac   (*labelnew)->numStrata    = label->numStrata;
5005aa44df4SToby Isaac   (*labelnew)->defaultValue = label->defaultValue;
501c58f1c22SToby Isaac   ierr = PetscMalloc1(label->numStrata, &(*labelnew)->stratumValues);CHKERRQ(ierr);
502c58f1c22SToby Isaac   ierr = PetscMalloc1(label->numStrata, &(*labelnew)->stratumSizes);CHKERRQ(ierr);
503c58f1c22SToby Isaac   ierr = PetscMalloc1(label->numStrata, &(*labelnew)->ht);CHKERRQ(ierr);
504c58f1c22SToby Isaac   ierr = PetscMalloc1(label->numStrata, &(*labelnew)->points);CHKERRQ(ierr);
505ad8374ffSToby Isaac   ierr = PetscMalloc1(label->numStrata, &(*labelnew)->validIS);CHKERRQ(ierr);
506c58f1c22SToby Isaac   for (v = 0; v < label->numStrata; ++v) {
507e8f14785SLisandro Dalcin     ierr = PetscHSetICreate(&(*labelnew)->ht[v]);CHKERRQ(ierr);
508c58f1c22SToby Isaac     (*labelnew)->stratumValues[v]  = label->stratumValues[v];
509c58f1c22SToby Isaac     (*labelnew)->stratumSizes[v]   = label->stratumSizes[v];
510ad8374ffSToby Isaac     ierr = PetscObjectReference((PetscObject) (label->points[v]));CHKERRQ(ierr);
511ad8374ffSToby Isaac     (*labelnew)->points[v]         = label->points[v];
512b9907514SLisandro Dalcin     (*labelnew)->validIS[v]        = PETSC_TRUE;
513c58f1c22SToby Isaac   }
514f14fe40dSLisandro Dalcin   ierr = PetscHMapIDestroy(&(*labelnew)->hmap);CHKERRQ(ierr);
515b9907514SLisandro Dalcin   ierr = PetscHMapIDuplicate(label->hmap,&(*labelnew)->hmap);CHKERRQ(ierr);
516c58f1c22SToby Isaac   (*labelnew)->pStart = -1;
517c58f1c22SToby Isaac   (*labelnew)->pEnd   = -1;
518c58f1c22SToby Isaac   (*labelnew)->bt     = NULL;
519c58f1c22SToby Isaac   PetscFunctionReturn(0);
520c58f1c22SToby Isaac }
521c58f1c22SToby Isaac 
522c6a43d28SMatthew G. Knepley /*@
523c6a43d28SMatthew G. Knepley   DMLabelComputeIndex - Create an index structure for membership determination, automatically determining the bounds
524c6a43d28SMatthew G. Knepley 
525c6a43d28SMatthew G. Knepley   Input Parameter:
526c6a43d28SMatthew G. Knepley . label  - The DMLabel
527c6a43d28SMatthew G. Knepley 
528c6a43d28SMatthew G. Knepley   Level: intermediate
529c6a43d28SMatthew G. Knepley 
530c6a43d28SMatthew G. Knepley .seealso: DMLabelHasPoint(), DMLabelCreateIndex(), DMLabelDestroyIndex(), DMLabelGetValue(), DMLabelSetValue()
531c6a43d28SMatthew G. Knepley @*/
532c6a43d28SMatthew G. Knepley PetscErrorCode DMLabelComputeIndex(DMLabel label)
533c6a43d28SMatthew G. Knepley {
534c6a43d28SMatthew G. Knepley   PetscInt       pStart = PETSC_MAX_INT, pEnd = -1, v;
535c6a43d28SMatthew G. Knepley   PetscErrorCode ierr;
536c6a43d28SMatthew G. Knepley 
537c6a43d28SMatthew G. Knepley   PetscFunctionBegin;
538c6a43d28SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
539c6a43d28SMatthew G. Knepley   ierr = DMLabelMakeAllValid_Private(label);CHKERRQ(ierr);
540c6a43d28SMatthew G. Knepley   for (v = 0; v < label->numStrata; ++v) {
541c6a43d28SMatthew G. Knepley     const PetscInt *points;
542c6a43d28SMatthew G. Knepley     PetscInt       i;
543c6a43d28SMatthew G. Knepley 
544c6a43d28SMatthew G. Knepley     ierr = ISGetIndices(label->points[v], &points);CHKERRQ(ierr);
545c6a43d28SMatthew G. Knepley     for (i = 0; i < label->stratumSizes[v]; ++i) {
546c6a43d28SMatthew G. Knepley       const PetscInt point = points[i];
547c6a43d28SMatthew G. Knepley 
548c6a43d28SMatthew G. Knepley       pStart = PetscMin(point,   pStart);
549c6a43d28SMatthew G. Knepley       pEnd   = PetscMax(point+1, pEnd);
550c6a43d28SMatthew G. Knepley     }
551c6a43d28SMatthew G. Knepley     ierr = ISRestoreIndices(label->points[v], &points);CHKERRQ(ierr);
552c6a43d28SMatthew G. Knepley   }
553c6a43d28SMatthew G. Knepley   label->pStart = pStart == PETSC_MAX_INT ? -1 : pStart;
554c6a43d28SMatthew G. Knepley   label->pEnd   = pEnd;
555c6a43d28SMatthew G. Knepley   ierr = DMLabelCreateIndex(label, label->pStart, label->pEnd);CHKERRQ(ierr);
556c6a43d28SMatthew G. Knepley   PetscFunctionReturn(0);
557c6a43d28SMatthew G. Knepley }
558c6a43d28SMatthew G. Knepley 
559c6a43d28SMatthew G. Knepley /*@
560c6a43d28SMatthew G. Knepley   DMLabelCreateIndex - Create an index structure for membership determination
561c6a43d28SMatthew G. Knepley 
562c6a43d28SMatthew G. Knepley   Input Parameters:
563c6a43d28SMatthew G. Knepley + label  - The DMLabel
564c6a43d28SMatthew G. Knepley . pStart - The smallest point
565c6a43d28SMatthew G. Knepley - pEnd   - The largest point + 1
566c6a43d28SMatthew G. Knepley 
567c6a43d28SMatthew G. Knepley   Level: intermediate
568c6a43d28SMatthew G. Knepley 
569c6a43d28SMatthew G. Knepley .seealso: DMLabelHasPoint(), DMLabelComputeIndex(), DMLabelDestroyIndex(), DMLabelGetValue(), DMLabelSetValue()
570c6a43d28SMatthew G. Knepley @*/
571c58f1c22SToby Isaac PetscErrorCode DMLabelCreateIndex(DMLabel label, PetscInt pStart, PetscInt pEnd)
572c58f1c22SToby Isaac {
573c58f1c22SToby Isaac   PetscInt       v;
574c58f1c22SToby Isaac   PetscErrorCode ierr;
575c58f1c22SToby Isaac 
576c58f1c22SToby Isaac   PetscFunctionBegin;
577d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
5780c3c4a36SLisandro Dalcin   ierr = DMLabelDestroyIndex(label);CHKERRQ(ierr);
579c58f1c22SToby Isaac   ierr = DMLabelMakeAllValid_Private(label);CHKERRQ(ierr);
580c58f1c22SToby Isaac   label->pStart = pStart;
581c58f1c22SToby Isaac   label->pEnd   = pEnd;
582c6a43d28SMatthew G. Knepley   /* This can be hooked into SetValue(),  ClearValue(), etc. for updating */
583c58f1c22SToby Isaac   ierr = PetscBTCreate(pEnd - pStart, &label->bt);CHKERRQ(ierr);
584c58f1c22SToby Isaac   for (v = 0; v < label->numStrata; ++v) {
585ad8374ffSToby Isaac     const PetscInt *points;
586c58f1c22SToby Isaac     PetscInt       i;
587c58f1c22SToby Isaac 
588ad8374ffSToby Isaac     ierr = ISGetIndices(label->points[v], &points);CHKERRQ(ierr);
589c58f1c22SToby Isaac     for (i = 0; i < label->stratumSizes[v]; ++i) {
590ad8374ffSToby Isaac       const PetscInt point = points[i];
591c58f1c22SToby Isaac 
592c58f1c22SToby Isaac       if ((point < pStart) || (point >= pEnd)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label point %D is not in [%D, %D)", point, pStart, pEnd);
593c58f1c22SToby Isaac       ierr = PetscBTSet(label->bt, point - pStart);CHKERRQ(ierr);
594c58f1c22SToby Isaac     }
595ad8374ffSToby Isaac     ierr = ISRestoreIndices(label->points[v], &points);CHKERRQ(ierr);
596c58f1c22SToby Isaac   }
597c58f1c22SToby Isaac   PetscFunctionReturn(0);
598c58f1c22SToby Isaac }
599c58f1c22SToby Isaac 
600c6a43d28SMatthew G. Knepley /*@
601c6a43d28SMatthew G. Knepley   DMLabelDestroyIndex - Destroy the index structure
602c6a43d28SMatthew G. Knepley 
603c6a43d28SMatthew G. Knepley   Input Parameter:
604c6a43d28SMatthew G. Knepley . label - the DMLabel
605c6a43d28SMatthew G. Knepley 
606c6a43d28SMatthew G. Knepley   Level: intermediate
607c6a43d28SMatthew G. Knepley 
608c6a43d28SMatthew G. Knepley .seealso: DMLabelHasPoint(), DMLabelCreateIndex(), DMLabelGetValue(), DMLabelSetValue()
609c6a43d28SMatthew G. Knepley @*/
610c58f1c22SToby Isaac PetscErrorCode DMLabelDestroyIndex(DMLabel label)
611c58f1c22SToby Isaac {
612c58f1c22SToby Isaac   PetscErrorCode ierr;
613c58f1c22SToby Isaac 
614c58f1c22SToby Isaac   PetscFunctionBegin;
615d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
616c58f1c22SToby Isaac   label->pStart = -1;
617c58f1c22SToby Isaac   label->pEnd   = -1;
6180c3c4a36SLisandro Dalcin   ierr = PetscBTDestroy(&label->bt);CHKERRQ(ierr);
619c58f1c22SToby Isaac   PetscFunctionReturn(0);
620c58f1c22SToby Isaac }
621c58f1c22SToby Isaac 
622c58f1c22SToby Isaac /*@
623c6a43d28SMatthew G. Knepley   DMLabelGetBounds - Return the smallest and largest point in the label
624c6a43d28SMatthew G. Knepley 
625c6a43d28SMatthew G. Knepley   Input Parameter:
626c6a43d28SMatthew G. Knepley . label - the DMLabel
627c6a43d28SMatthew G. Knepley 
628c6a43d28SMatthew G. Knepley   Output Parameters:
629c6a43d28SMatthew G. Knepley + pStart - The smallest point
630c6a43d28SMatthew G. Knepley - pEnd   - The largest point + 1
631c6a43d28SMatthew G. Knepley 
632c6a43d28SMatthew G. Knepley   Note: This will compute an index for the label if one does not exist.
633c6a43d28SMatthew G. Knepley 
634c6a43d28SMatthew G. Knepley   Level: intermediate
635c6a43d28SMatthew G. Knepley 
636c6a43d28SMatthew G. Knepley .seealso: DMLabelHasPoint(), DMLabelCreateIndex(), DMLabelGetValue(), DMLabelSetValue()
637c6a43d28SMatthew G. Knepley @*/
638c6a43d28SMatthew G. Knepley PetscErrorCode DMLabelGetBounds(DMLabel label, PetscInt *pStart, PetscInt *pEnd)
639c6a43d28SMatthew G. Knepley {
640c6a43d28SMatthew G. Knepley   PetscErrorCode ierr;
641c6a43d28SMatthew G. Knepley 
642c6a43d28SMatthew G. Knepley   PetscFunctionBegin;
643c6a43d28SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
644c6a43d28SMatthew G. Knepley   if ((label->pStart == -1) && (label->pEnd == -1)) {ierr = DMLabelComputeIndex(label);CHKERRQ(ierr);}
645c6a43d28SMatthew G. Knepley   if (pStart) {
646c6a43d28SMatthew G. Knepley     PetscValidPointer(pStart, 2);
647c6a43d28SMatthew G. Knepley     *pStart = label->pStart;
648c6a43d28SMatthew G. Knepley   }
649c6a43d28SMatthew G. Knepley   if (pEnd) {
650c6a43d28SMatthew G. Knepley     PetscValidPointer(pEnd, 3);
651c6a43d28SMatthew G. Knepley     *pEnd = label->pEnd;
652c6a43d28SMatthew G. Knepley   }
653c6a43d28SMatthew G. Knepley   PetscFunctionReturn(0);
654c6a43d28SMatthew G. Knepley }
655c6a43d28SMatthew G. Knepley 
656c6a43d28SMatthew G. Knepley /*@
657c58f1c22SToby Isaac   DMLabelHasValue - Determine whether a label assigns the value to any point
658c58f1c22SToby Isaac 
659c58f1c22SToby Isaac   Input Parameters:
660c58f1c22SToby Isaac + label - the DMLabel
661c58f1c22SToby Isaac - value - the value
662c58f1c22SToby Isaac 
663c58f1c22SToby Isaac   Output Parameter:
664c58f1c22SToby Isaac . contains - Flag indicating whether the label maps this value to any point
665c58f1c22SToby Isaac 
666c58f1c22SToby Isaac   Level: developer
667c58f1c22SToby Isaac 
668c58f1c22SToby Isaac .seealso: DMLabelHasPoint(), DMLabelGetValue(), DMLabelSetValue()
669c58f1c22SToby Isaac @*/
670c58f1c22SToby Isaac PetscErrorCode DMLabelHasValue(DMLabel label, PetscInt value, PetscBool *contains)
671c58f1c22SToby Isaac {
672c58f1c22SToby Isaac   PetscInt v;
6730c3c4a36SLisandro Dalcin   PetscErrorCode ierr;
674c58f1c22SToby Isaac 
675c58f1c22SToby Isaac   PetscFunctionBegin;
676d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
677c58f1c22SToby Isaac   PetscValidPointer(contains, 3);
6780c3c4a36SLisandro Dalcin   ierr = DMLabelLookupStratum(label, value, &v);CHKERRQ(ierr);
6790c3c4a36SLisandro Dalcin   *contains = v < 0 ? PETSC_FALSE : PETSC_TRUE;
680c58f1c22SToby Isaac   PetscFunctionReturn(0);
681c58f1c22SToby Isaac }
682c58f1c22SToby Isaac 
683c58f1c22SToby Isaac /*@
684c58f1c22SToby Isaac   DMLabelHasPoint - Determine whether a label assigns a value to a point
685c58f1c22SToby Isaac 
686c58f1c22SToby Isaac   Input Parameters:
687c58f1c22SToby Isaac + label - the DMLabel
688c58f1c22SToby Isaac - point - the point
689c58f1c22SToby Isaac 
690c58f1c22SToby Isaac   Output Parameter:
691c58f1c22SToby Isaac . contains - Flag indicating whether the label maps this point to a value
692c58f1c22SToby Isaac 
693c58f1c22SToby Isaac   Note: The user must call DMLabelCreateIndex() before this function.
694c58f1c22SToby Isaac 
695c58f1c22SToby Isaac   Level: developer
696c58f1c22SToby Isaac 
697c58f1c22SToby Isaac .seealso: DMLabelCreateIndex(), DMLabelGetValue(), DMLabelSetValue()
698c58f1c22SToby Isaac @*/
699c58f1c22SToby Isaac PetscErrorCode DMLabelHasPoint(DMLabel label, PetscInt point, PetscBool *contains)
700c58f1c22SToby Isaac {
701c58f1c22SToby Isaac   PetscErrorCode ierr;
702c58f1c22SToby Isaac 
703c58f1c22SToby Isaac   PetscFunctionBeginHot;
704d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
705c58f1c22SToby Isaac   PetscValidPointer(contains, 3);
706c58f1c22SToby Isaac   ierr = DMLabelMakeAllValid_Private(label);CHKERRQ(ierr);
707c58f1c22SToby Isaac #if defined(PETSC_USE_DEBUG)
708c58f1c22SToby Isaac   if (!label->bt) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call DMLabelCreateIndex() before DMLabelHasPoint()");
709c58f1c22SToby Isaac   if ((point < label->pStart) || (point >= label->pEnd)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label point %D is not in [%D, %D)", point, label->pStart, label->pEnd);
710c58f1c22SToby Isaac #endif
711c58f1c22SToby Isaac   *contains = PetscBTLookup(label->bt, point - label->pStart) ? PETSC_TRUE : PETSC_FALSE;
712c58f1c22SToby Isaac   PetscFunctionReturn(0);
713c58f1c22SToby Isaac }
714c58f1c22SToby Isaac 
715c58f1c22SToby Isaac /*@
716c58f1c22SToby Isaac   DMLabelStratumHasPoint - Return true if the stratum contains a point
717c58f1c22SToby Isaac 
718c58f1c22SToby Isaac   Input Parameters:
719c58f1c22SToby Isaac + label - the DMLabel
720c58f1c22SToby Isaac . value - the stratum value
721c58f1c22SToby Isaac - point - the point
722c58f1c22SToby Isaac 
723c58f1c22SToby Isaac   Output Parameter:
724c58f1c22SToby Isaac . contains - true if the stratum contains the point
725c58f1c22SToby Isaac 
726c58f1c22SToby Isaac   Level: intermediate
727c58f1c22SToby Isaac 
728c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMLabelSetValue(), DMLabelClearValue()
729c58f1c22SToby Isaac @*/
730c58f1c22SToby Isaac PetscErrorCode DMLabelStratumHasPoint(DMLabel label, PetscInt value, PetscInt point, PetscBool *contains)
731c58f1c22SToby Isaac {
732c58f1c22SToby Isaac   PetscInt       v;
733c58f1c22SToby Isaac   PetscErrorCode ierr;
734c58f1c22SToby Isaac 
7350c3c4a36SLisandro Dalcin   PetscFunctionBeginHot;
736d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
737c58f1c22SToby Isaac   PetscValidPointer(contains, 4);
738c58f1c22SToby Isaac   *contains = PETSC_FALSE;
7390c3c4a36SLisandro Dalcin   ierr = DMLabelLookupStratum(label, value, &v);CHKERRQ(ierr);
7400c3c4a36SLisandro Dalcin   if (v < 0) PetscFunctionReturn(0);
7410c3c4a36SLisandro Dalcin 
742ad8374ffSToby Isaac   if (label->validIS[v]) {
743c58f1c22SToby Isaac     PetscInt i;
744c58f1c22SToby Isaac 
745a2d74346SToby Isaac     ierr = ISLocate(label->points[v], point, &i);CHKERRQ(ierr);
7460c3c4a36SLisandro Dalcin     if (i >= 0) *contains = PETSC_TRUE;
747c58f1c22SToby Isaac   } else {
748c58f1c22SToby Isaac     PetscBool has;
749c58f1c22SToby Isaac 
750b9907514SLisandro Dalcin     ierr = PetscHSetIHas(label->ht[v], point, &has);CHKERRQ(ierr);
7510c3c4a36SLisandro Dalcin     if (has) *contains = PETSC_TRUE;
752c58f1c22SToby Isaac   }
753c58f1c22SToby Isaac   PetscFunctionReturn(0);
754c58f1c22SToby Isaac }
755c58f1c22SToby Isaac 
75684f0b6dfSMatthew G. Knepley /*@
7575aa44df4SToby Isaac   DMLabelGetDefaultValue - Get the default value returned by DMLabelGetValue() if a point has not been explicitly given a value.
7585aa44df4SToby Isaac   When a label is created, it is initialized to -1.
7595aa44df4SToby Isaac 
7605aa44df4SToby Isaac   Input parameter:
7615aa44df4SToby Isaac . label - a DMLabel object
7625aa44df4SToby Isaac 
7635aa44df4SToby Isaac   Output parameter:
7645aa44df4SToby Isaac . defaultValue - the default value
7655aa44df4SToby Isaac 
7665aa44df4SToby Isaac   Level: beginner
7675aa44df4SToby Isaac 
7685aa44df4SToby Isaac .seealso: DMLabelSetDefaultValue(), DMLabelGetValue(), DMLabelSetValue()
76984f0b6dfSMatthew G. Knepley @*/
7705aa44df4SToby Isaac PetscErrorCode DMLabelGetDefaultValue(DMLabel label, PetscInt *defaultValue)
7715aa44df4SToby Isaac {
7725aa44df4SToby Isaac   PetscFunctionBegin;
773d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
7745aa44df4SToby Isaac   *defaultValue = label->defaultValue;
7755aa44df4SToby Isaac   PetscFunctionReturn(0);
7765aa44df4SToby Isaac }
7775aa44df4SToby Isaac 
77884f0b6dfSMatthew G. Knepley /*@
7795aa44df4SToby Isaac   DMLabelSetDefaultValue - Set the default value returned by DMLabelGetValue() if a point has not been explicitly given a value.
7805aa44df4SToby Isaac   When a label is created, it is initialized to -1.
7815aa44df4SToby Isaac 
7825aa44df4SToby Isaac   Input parameter:
7835aa44df4SToby Isaac . label - a DMLabel object
7845aa44df4SToby Isaac 
7855aa44df4SToby Isaac   Output parameter:
7865aa44df4SToby Isaac . defaultValue - the default value
7875aa44df4SToby Isaac 
7885aa44df4SToby Isaac   Level: beginner
7895aa44df4SToby Isaac 
7905aa44df4SToby Isaac .seealso: DMLabelGetDefaultValue(), DMLabelGetValue(), DMLabelSetValue()
79184f0b6dfSMatthew G. Knepley @*/
7925aa44df4SToby Isaac PetscErrorCode DMLabelSetDefaultValue(DMLabel label, PetscInt defaultValue)
7935aa44df4SToby Isaac {
7945aa44df4SToby Isaac   PetscFunctionBegin;
795d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
7965aa44df4SToby Isaac   label->defaultValue = defaultValue;
7975aa44df4SToby Isaac   PetscFunctionReturn(0);
7985aa44df4SToby Isaac }
7995aa44df4SToby Isaac 
800c58f1c22SToby Isaac /*@
8015aa44df4SToby Isaac   DMLabelGetValue - Return the value a label assigns to a point, or the label's default value (which is initially -1, and can be changed with DMLabelSetDefaultValue())
802c58f1c22SToby Isaac 
803c58f1c22SToby Isaac   Input Parameters:
804c58f1c22SToby Isaac + label - the DMLabel
805c58f1c22SToby Isaac - point - the point
806c58f1c22SToby Isaac 
807c58f1c22SToby Isaac   Output Parameter:
8088e68afcfSMatthew G. Knepley . value - The point value, or the default value (-1 by default)
809c58f1c22SToby Isaac 
810c58f1c22SToby Isaac   Level: intermediate
811c58f1c22SToby Isaac 
8125aa44df4SToby Isaac .seealso: DMLabelCreate(), DMLabelSetValue(), DMLabelClearValue(), DMLabelGetDefaultValue(), DMLabelSetDefaultValue()
813c58f1c22SToby Isaac @*/
814c58f1c22SToby Isaac PetscErrorCode DMLabelGetValue(DMLabel label, PetscInt point, PetscInt *value)
815c58f1c22SToby Isaac {
816c58f1c22SToby Isaac   PetscInt       v;
817c58f1c22SToby Isaac   PetscErrorCode ierr;
818c58f1c22SToby Isaac 
8190c3c4a36SLisandro Dalcin   PetscFunctionBeginHot;
820d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
821c58f1c22SToby Isaac   PetscValidPointer(value, 3);
8225aa44df4SToby Isaac   *value = label->defaultValue;
823c58f1c22SToby Isaac   for (v = 0; v < label->numStrata; ++v) {
824ad8374ffSToby Isaac     if (label->validIS[v]) {
825c58f1c22SToby Isaac       PetscInt i;
826c58f1c22SToby Isaac 
827a2d74346SToby Isaac       ierr = ISLocate(label->points[v], point, &i);CHKERRQ(ierr);
828c58f1c22SToby Isaac       if (i >= 0) {
829c58f1c22SToby Isaac         *value = label->stratumValues[v];
830c58f1c22SToby Isaac         break;
831c58f1c22SToby Isaac       }
832c58f1c22SToby Isaac     } else {
833c58f1c22SToby Isaac       PetscBool has;
834c58f1c22SToby Isaac 
835b9907514SLisandro Dalcin       ierr = PetscHSetIHas(label->ht[v], point, &has);CHKERRQ(ierr);
836c58f1c22SToby Isaac       if (has) {
837c58f1c22SToby Isaac         *value = label->stratumValues[v];
838c58f1c22SToby Isaac         break;
839c58f1c22SToby Isaac       }
840c58f1c22SToby Isaac     }
841c58f1c22SToby Isaac   }
842c58f1c22SToby Isaac   PetscFunctionReturn(0);
843c58f1c22SToby Isaac }
844c58f1c22SToby Isaac 
845c58f1c22SToby Isaac /*@
846367003a6SStefano Zampini   DMLabelSetValue - Set the value a label assigns to a point.  If the value is the same as the label's default value (which is initially -1, and can be changed with DMLabelSetDefaultValue() to something different), then this function will do nothing.
847c58f1c22SToby Isaac 
848c58f1c22SToby Isaac   Input Parameters:
849c58f1c22SToby Isaac + label - the DMLabel
850c58f1c22SToby Isaac . point - the point
851c58f1c22SToby Isaac - value - The point value
852c58f1c22SToby Isaac 
853c58f1c22SToby Isaac   Level: intermediate
854c58f1c22SToby Isaac 
8555aa44df4SToby Isaac .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelClearValue(), DMLabelGetDefaultValue(), DMLabelSetDefaultValue()
856c58f1c22SToby Isaac @*/
857c58f1c22SToby Isaac PetscErrorCode DMLabelSetValue(DMLabel label, PetscInt point, PetscInt value)
858c58f1c22SToby Isaac {
859c58f1c22SToby Isaac   PetscInt       v;
860c58f1c22SToby Isaac   PetscErrorCode ierr;
861c58f1c22SToby Isaac 
862c58f1c22SToby Isaac   PetscFunctionBegin;
863d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
8640c3c4a36SLisandro Dalcin   /* Find label value, add new entry if needed */
8655aa44df4SToby Isaac   if (value == label->defaultValue) PetscFunctionReturn(0);
866b9907514SLisandro Dalcin   ierr = DMLabelLookupAddStratum(label, value, &v);CHKERRQ(ierr);
867c58f1c22SToby Isaac   /* Set key */
8680c3c4a36SLisandro Dalcin   ierr = DMLabelMakeInvalid_Private(label, v);CHKERRQ(ierr);
869e8f14785SLisandro Dalcin   ierr = PetscHSetIAdd(label->ht[v], point);CHKERRQ(ierr);
870c58f1c22SToby Isaac   PetscFunctionReturn(0);
871c58f1c22SToby Isaac }
872c58f1c22SToby Isaac 
873c58f1c22SToby Isaac /*@
874c58f1c22SToby Isaac   DMLabelClearValue - Clear the value a label assigns to a point
875c58f1c22SToby Isaac 
876c58f1c22SToby Isaac   Input Parameters:
877c58f1c22SToby Isaac + label - the DMLabel
878c58f1c22SToby Isaac . point - the point
879c58f1c22SToby Isaac - value - The point value
880c58f1c22SToby Isaac 
881c58f1c22SToby Isaac   Level: intermediate
882c58f1c22SToby Isaac 
883c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue()
884c58f1c22SToby Isaac @*/
885c58f1c22SToby Isaac PetscErrorCode DMLabelClearValue(DMLabel label, PetscInt point, PetscInt value)
886c58f1c22SToby Isaac {
887ad8374ffSToby Isaac   PetscInt       v;
888c58f1c22SToby Isaac   PetscErrorCode ierr;
889c58f1c22SToby Isaac 
890c58f1c22SToby Isaac   PetscFunctionBegin;
891d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
892c58f1c22SToby Isaac   /* Find label value */
8930c3c4a36SLisandro Dalcin   ierr = DMLabelLookupStratum(label, value, &v);CHKERRQ(ierr);
8940c3c4a36SLisandro Dalcin   if (v < 0) PetscFunctionReturn(0);
8950c3c4a36SLisandro Dalcin 
896eeed21e7SToby Isaac   if (label->bt) {
897eeed21e7SToby Isaac     if ((point < label->pStart) || (point >= label->pEnd)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label point %D is not in [%D, %D)", point, label->pStart, label->pEnd);
898eeed21e7SToby Isaac     ierr = PetscBTClear(label->bt, point - label->pStart);CHKERRQ(ierr);
899eeed21e7SToby Isaac   }
9000c3c4a36SLisandro Dalcin 
9010c3c4a36SLisandro Dalcin   /* Delete key */
9020c3c4a36SLisandro Dalcin   ierr = DMLabelMakeInvalid_Private(label, v);CHKERRQ(ierr);
903e8f14785SLisandro Dalcin   ierr = PetscHSetIDel(label->ht[v], point);CHKERRQ(ierr);
904c58f1c22SToby Isaac   PetscFunctionReturn(0);
905c58f1c22SToby Isaac }
906c58f1c22SToby Isaac 
907c58f1c22SToby Isaac /*@
908c58f1c22SToby Isaac   DMLabelInsertIS - Set all points in the IS to a value
909c58f1c22SToby Isaac 
910c58f1c22SToby Isaac   Input Parameters:
911c58f1c22SToby Isaac + label - the DMLabel
912c58f1c22SToby Isaac . is    - the point IS
913c58f1c22SToby Isaac - value - The point value
914c58f1c22SToby Isaac 
915c58f1c22SToby Isaac   Level: intermediate
916c58f1c22SToby Isaac 
917c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
918c58f1c22SToby Isaac @*/
919c58f1c22SToby Isaac PetscErrorCode DMLabelInsertIS(DMLabel label, IS is, PetscInt value)
920c58f1c22SToby Isaac {
9210c3c4a36SLisandro Dalcin   PetscInt        v, n, p;
922c58f1c22SToby Isaac   const PetscInt *points;
923c58f1c22SToby Isaac   PetscErrorCode  ierr;
924c58f1c22SToby Isaac 
925c58f1c22SToby Isaac   PetscFunctionBegin;
926d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
927c58f1c22SToby Isaac   PetscValidHeaderSpecific(is, IS_CLASSID, 2);
9280c3c4a36SLisandro Dalcin   /* Find label value, add new entry if needed */
9290c3c4a36SLisandro Dalcin   if (value == label->defaultValue) PetscFunctionReturn(0);
930b9907514SLisandro Dalcin   ierr = DMLabelLookupAddStratum(label, value, &v);CHKERRQ(ierr);
9310c3c4a36SLisandro Dalcin   /* Set keys */
9320c3c4a36SLisandro Dalcin   ierr = DMLabelMakeInvalid_Private(label, v);CHKERRQ(ierr);
933c58f1c22SToby Isaac   ierr = ISGetLocalSize(is, &n);CHKERRQ(ierr);
934c58f1c22SToby Isaac   ierr = ISGetIndices(is, &points);CHKERRQ(ierr);
935e8f14785SLisandro Dalcin   for (p = 0; p < n; ++p) {ierr = PetscHSetIAdd(label->ht[v], points[p]);CHKERRQ(ierr);}
936c58f1c22SToby Isaac   ierr = ISRestoreIndices(is, &points);CHKERRQ(ierr);
937c58f1c22SToby Isaac   PetscFunctionReturn(0);
938c58f1c22SToby Isaac }
939c58f1c22SToby Isaac 
94084f0b6dfSMatthew G. Knepley /*@
94184f0b6dfSMatthew G. Knepley   DMLabelGetNumValues - Get the number of values that the DMLabel takes
94284f0b6dfSMatthew G. Knepley 
94384f0b6dfSMatthew G. Knepley   Input Parameter:
94484f0b6dfSMatthew G. Knepley . label - the DMLabel
94584f0b6dfSMatthew G. Knepley 
94684f0b6dfSMatthew G. Knepley   Output Paramater:
94784f0b6dfSMatthew G. Knepley . numValues - the number of values
94884f0b6dfSMatthew G. Knepley 
94984f0b6dfSMatthew G. Knepley   Level: intermediate
95084f0b6dfSMatthew G. Knepley 
95184f0b6dfSMatthew G. Knepley .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
95284f0b6dfSMatthew G. Knepley @*/
953c58f1c22SToby Isaac PetscErrorCode DMLabelGetNumValues(DMLabel label, PetscInt *numValues)
954c58f1c22SToby Isaac {
955c58f1c22SToby Isaac   PetscFunctionBegin;
956d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
957c58f1c22SToby Isaac   PetscValidPointer(numValues, 2);
958c58f1c22SToby Isaac   *numValues = label->numStrata;
959c58f1c22SToby Isaac   PetscFunctionReturn(0);
960c58f1c22SToby Isaac }
961c58f1c22SToby Isaac 
96284f0b6dfSMatthew G. Knepley /*@
96384f0b6dfSMatthew G. Knepley   DMLabelGetValueIS - Get an IS of all values that the DMlabel takes
96484f0b6dfSMatthew G. Knepley 
96584f0b6dfSMatthew G. Knepley   Input Parameter:
96684f0b6dfSMatthew G. Knepley . label - the DMLabel
96784f0b6dfSMatthew G. Knepley 
96884f0b6dfSMatthew G. Knepley   Output Paramater:
96984f0b6dfSMatthew G. Knepley . is    - the value IS
97084f0b6dfSMatthew G. Knepley 
97184f0b6dfSMatthew G. Knepley   Level: intermediate
97284f0b6dfSMatthew G. Knepley 
97384f0b6dfSMatthew G. Knepley .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
97484f0b6dfSMatthew G. Knepley @*/
975c58f1c22SToby Isaac PetscErrorCode DMLabelGetValueIS(DMLabel label, IS *values)
976c58f1c22SToby Isaac {
977c58f1c22SToby Isaac   PetscErrorCode ierr;
978c58f1c22SToby Isaac 
979c58f1c22SToby Isaac   PetscFunctionBegin;
980d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
981c58f1c22SToby Isaac   PetscValidPointer(values, 2);
982c58f1c22SToby Isaac   ierr = ISCreateGeneral(PETSC_COMM_SELF, label->numStrata, label->stratumValues, PETSC_USE_POINTER, values);CHKERRQ(ierr);
983c58f1c22SToby Isaac   PetscFunctionReturn(0);
984c58f1c22SToby Isaac }
985c58f1c22SToby Isaac 
98684f0b6dfSMatthew G. Knepley /*@
98784f0b6dfSMatthew G. Knepley   DMLabelHasStratum - Determine whether points exist with the given value
98884f0b6dfSMatthew G. Knepley 
98984f0b6dfSMatthew G. Knepley   Input Parameters:
99084f0b6dfSMatthew G. Knepley + label - the DMLabel
99184f0b6dfSMatthew G. Knepley - value - the stratum value
99284f0b6dfSMatthew G. Knepley 
99384f0b6dfSMatthew G. Knepley   Output Paramater:
99484f0b6dfSMatthew G. Knepley . exists - Flag saying whether points exist
99584f0b6dfSMatthew G. Knepley 
99684f0b6dfSMatthew G. Knepley   Level: intermediate
99784f0b6dfSMatthew G. Knepley 
99884f0b6dfSMatthew G. Knepley .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
99984f0b6dfSMatthew G. Knepley @*/
1000fada774cSMatthew G. Knepley PetscErrorCode DMLabelHasStratum(DMLabel label, PetscInt value, PetscBool *exists)
1001fada774cSMatthew G. Knepley {
1002fada774cSMatthew G. Knepley   PetscInt       v;
10030c3c4a36SLisandro Dalcin   PetscErrorCode ierr;
1004fada774cSMatthew G. Knepley 
1005fada774cSMatthew G. Knepley   PetscFunctionBegin;
1006d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
1007fada774cSMatthew G. Knepley   PetscValidPointer(exists, 3);
10080c3c4a36SLisandro Dalcin   ierr = DMLabelLookupStratum(label, value, &v);CHKERRQ(ierr);
10090c3c4a36SLisandro Dalcin   *exists = v < 0 ? PETSC_FALSE : PETSC_TRUE;
1010fada774cSMatthew G. Knepley   PetscFunctionReturn(0);
1011fada774cSMatthew G. Knepley }
1012fada774cSMatthew G. Knepley 
101384f0b6dfSMatthew G. Knepley /*@
101484f0b6dfSMatthew G. Knepley   DMLabelGetStratumSize - Get the size of a stratum
101584f0b6dfSMatthew G. Knepley 
101684f0b6dfSMatthew G. Knepley   Input Parameters:
101784f0b6dfSMatthew G. Knepley + label - the DMLabel
101884f0b6dfSMatthew G. Knepley - value - the stratum value
101984f0b6dfSMatthew G. Knepley 
102084f0b6dfSMatthew G. Knepley   Output Paramater:
102184f0b6dfSMatthew G. Knepley . size - The number of points in the stratum
102284f0b6dfSMatthew G. Knepley 
102384f0b6dfSMatthew G. Knepley   Level: intermediate
102484f0b6dfSMatthew G. Knepley 
102584f0b6dfSMatthew G. Knepley .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
102684f0b6dfSMatthew G. Knepley @*/
1027c58f1c22SToby Isaac PetscErrorCode DMLabelGetStratumSize(DMLabel label, PetscInt value, PetscInt *size)
1028c58f1c22SToby Isaac {
1029c58f1c22SToby Isaac   PetscInt       v;
1030c58f1c22SToby Isaac   PetscErrorCode ierr;
1031c58f1c22SToby Isaac 
1032c58f1c22SToby Isaac   PetscFunctionBegin;
1033d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
1034c58f1c22SToby Isaac   PetscValidPointer(size, 3);
1035c58f1c22SToby Isaac   *size = 0;
10360c3c4a36SLisandro Dalcin   ierr = DMLabelLookupStratum(label, value, &v);CHKERRQ(ierr);
10370c3c4a36SLisandro Dalcin   if (v < 0) PetscFunctionReturn(0);
1038c58f1c22SToby Isaac   ierr = DMLabelMakeValid_Private(label, v);CHKERRQ(ierr);
1039c58f1c22SToby Isaac   *size = label->stratumSizes[v];
1040c58f1c22SToby Isaac   PetscFunctionReturn(0);
1041c58f1c22SToby Isaac }
1042c58f1c22SToby Isaac 
104384f0b6dfSMatthew G. Knepley /*@
104484f0b6dfSMatthew G. Knepley   DMLabelGetStratumBounds - Get the largest and smallest point of a stratum
104584f0b6dfSMatthew G. Knepley 
104684f0b6dfSMatthew G. Knepley   Input Parameters:
104784f0b6dfSMatthew G. Knepley + label - the DMLabel
104884f0b6dfSMatthew G. Knepley - value - the stratum value
104984f0b6dfSMatthew G. Knepley 
105084f0b6dfSMatthew G. Knepley   Output Paramaters:
105184f0b6dfSMatthew G. Knepley + start - the smallest point in the stratum
105284f0b6dfSMatthew G. Knepley - end - the largest point in the stratum
105384f0b6dfSMatthew G. Knepley 
105484f0b6dfSMatthew G. Knepley   Level: intermediate
105584f0b6dfSMatthew G. Knepley 
105684f0b6dfSMatthew G. Knepley .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
105784f0b6dfSMatthew G. Knepley @*/
1058c58f1c22SToby Isaac PetscErrorCode DMLabelGetStratumBounds(DMLabel label, PetscInt value, PetscInt *start, PetscInt *end)
1059c58f1c22SToby Isaac {
10600c3c4a36SLisandro Dalcin   PetscInt       v, min, max;
1061c58f1c22SToby Isaac   PetscErrorCode ierr;
1062c58f1c22SToby Isaac 
1063c58f1c22SToby Isaac   PetscFunctionBegin;
1064d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
1065c58f1c22SToby Isaac   if (start) {PetscValidPointer(start, 3); *start = 0;}
1066c58f1c22SToby Isaac   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
10670c3c4a36SLisandro Dalcin   ierr = DMLabelLookupStratum(label, value, &v);CHKERRQ(ierr);
10680c3c4a36SLisandro Dalcin   if (v < 0) PetscFunctionReturn(0);
1069c58f1c22SToby Isaac   ierr = DMLabelMakeValid_Private(label, v);CHKERRQ(ierr);
10700c3c4a36SLisandro Dalcin   if (label->stratumSizes[v] <= 0) PetscFunctionReturn(0);
1071d6cb179aSToby Isaac   ierr = ISGetMinMax(label->points[v], &min, &max);CHKERRQ(ierr);
1072d6cb179aSToby Isaac   if (start) *start = min;
1073d6cb179aSToby Isaac   if (end)   *end   = max+1;
1074c58f1c22SToby Isaac   PetscFunctionReturn(0);
1075c58f1c22SToby Isaac }
1076c58f1c22SToby Isaac 
107784f0b6dfSMatthew G. Knepley /*@
107884f0b6dfSMatthew G. Knepley   DMLabelGetStratumIS - Get an IS with the stratum points
107984f0b6dfSMatthew G. Knepley 
108084f0b6dfSMatthew G. Knepley   Input Parameters:
108184f0b6dfSMatthew G. Knepley + label - the DMLabel
108284f0b6dfSMatthew G. Knepley - value - the stratum value
108384f0b6dfSMatthew G. Knepley 
108484f0b6dfSMatthew G. Knepley   Output Paramater:
108584f0b6dfSMatthew G. Knepley . points - The stratum points
108684f0b6dfSMatthew G. Knepley 
108784f0b6dfSMatthew G. Knepley   Level: intermediate
108884f0b6dfSMatthew G. Knepley 
108984f0b6dfSMatthew G. Knepley .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
109084f0b6dfSMatthew G. Knepley @*/
1091c58f1c22SToby Isaac PetscErrorCode DMLabelGetStratumIS(DMLabel label, PetscInt value, IS *points)
1092c58f1c22SToby Isaac {
1093c58f1c22SToby Isaac   PetscInt       v;
1094c58f1c22SToby Isaac   PetscErrorCode ierr;
1095c58f1c22SToby Isaac 
1096c58f1c22SToby Isaac   PetscFunctionBegin;
1097d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
1098c58f1c22SToby Isaac   PetscValidPointer(points, 3);
1099c58f1c22SToby Isaac   *points = NULL;
11000c3c4a36SLisandro Dalcin   ierr = DMLabelLookupStratum(label, value, &v);CHKERRQ(ierr);
11010c3c4a36SLisandro Dalcin   if (v < 0) PetscFunctionReturn(0);
1102c58f1c22SToby Isaac   ierr = DMLabelMakeValid_Private(label, v);CHKERRQ(ierr);
1103ad8374ffSToby Isaac   ierr = PetscObjectReference((PetscObject) label->points[v]);CHKERRQ(ierr);
1104ad8374ffSToby Isaac   *points = label->points[v];
1105c58f1c22SToby Isaac   PetscFunctionReturn(0);
1106c58f1c22SToby Isaac }
1107c58f1c22SToby Isaac 
110884f0b6dfSMatthew G. Knepley /*@
110984f0b6dfSMatthew G. Knepley   DMLabelSetStratumIS - Set the stratum points using an IS
111084f0b6dfSMatthew G. Knepley 
111184f0b6dfSMatthew G. Knepley   Input Parameters:
111284f0b6dfSMatthew G. Knepley + label - the DMLabel
111384f0b6dfSMatthew G. Knepley . value - the stratum value
111484f0b6dfSMatthew G. Knepley - points - The stratum points
111584f0b6dfSMatthew G. Knepley 
111684f0b6dfSMatthew G. Knepley   Level: intermediate
111784f0b6dfSMatthew G. Knepley 
111884f0b6dfSMatthew G. Knepley .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
111984f0b6dfSMatthew G. Knepley @*/
11204de306b1SToby Isaac PetscErrorCode DMLabelSetStratumIS(DMLabel label, PetscInt value, IS is)
11214de306b1SToby Isaac {
11220c3c4a36SLisandro Dalcin   PetscInt       v;
11234de306b1SToby Isaac   PetscErrorCode ierr;
11244de306b1SToby Isaac 
11254de306b1SToby Isaac   PetscFunctionBegin;
1126d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
1127d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(is, IS_CLASSID, 3);
1128b9907514SLisandro Dalcin   ierr = DMLabelLookupAddStratum(label, value, &v);CHKERRQ(ierr);
11294de306b1SToby Isaac   if (is == label->points[v]) PetscFunctionReturn(0);
11304de306b1SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
11314de306b1SToby Isaac   ierr = ISGetLocalSize(is, &(label->stratumSizes[v]));CHKERRQ(ierr);
11324de306b1SToby Isaac   ierr = PetscObjectReference((PetscObject)is);CHKERRQ(ierr);
11334de306b1SToby Isaac   ierr = ISDestroy(&(label->points[v]));CHKERRQ(ierr);
11340c3c4a36SLisandro Dalcin   label->points[v] = is;
11350c3c4a36SLisandro Dalcin   label->validIS[v] = PETSC_TRUE;
11364de306b1SToby Isaac   if (label->bt) {
11374de306b1SToby Isaac     const PetscInt *points;
11384de306b1SToby Isaac     PetscInt p;
11394de306b1SToby Isaac 
11404de306b1SToby Isaac     ierr = ISGetIndices(is,&points);CHKERRQ(ierr);
11414de306b1SToby Isaac     for (p = 0; p < label->stratumSizes[v]; ++p) {
11424de306b1SToby Isaac       const PetscInt point = points[p];
11434de306b1SToby Isaac 
11444de306b1SToby Isaac       if ((point < label->pStart) || (point >= label->pEnd)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label point %D is not in [%D, %D)", point, label->pStart, label->pEnd);
11454de306b1SToby Isaac       ierr = PetscBTSet(label->bt, point - label->pStart);CHKERRQ(ierr);
11464de306b1SToby Isaac     }
11474de306b1SToby Isaac   }
11484de306b1SToby Isaac   PetscFunctionReturn(0);
11494de306b1SToby Isaac }
11504de306b1SToby Isaac 
115184f0b6dfSMatthew G. Knepley /*@
115284f0b6dfSMatthew G. Knepley   DMLabelClearStratum - Remove a stratum
11534de306b1SToby Isaac 
115484f0b6dfSMatthew G. Knepley   Input Parameters:
115584f0b6dfSMatthew G. Knepley + label - the DMLabel
115684f0b6dfSMatthew G. Knepley - value - the stratum value
115784f0b6dfSMatthew G. Knepley 
115884f0b6dfSMatthew G. Knepley   Level: intermediate
115984f0b6dfSMatthew G. Knepley 
116084f0b6dfSMatthew G. Knepley .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
116184f0b6dfSMatthew G. Knepley @*/
1162c58f1c22SToby Isaac PetscErrorCode DMLabelClearStratum(DMLabel label, PetscInt value)
1163c58f1c22SToby Isaac {
1164c58f1c22SToby Isaac   PetscInt       v;
1165c58f1c22SToby Isaac   PetscErrorCode ierr;
1166c58f1c22SToby Isaac 
1167c58f1c22SToby Isaac   PetscFunctionBegin;
1168d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
11690c3c4a36SLisandro Dalcin   ierr = DMLabelLookupStratum(label, value, &v);CHKERRQ(ierr);
11700c3c4a36SLisandro Dalcin   if (v < 0) PetscFunctionReturn(0);
11714de306b1SToby Isaac   if (label->validIS[v]) {
11724de306b1SToby Isaac     if (label->bt) {
1173c58f1c22SToby Isaac       PetscInt       i;
1174ad8374ffSToby Isaac       const PetscInt *points;
1175c58f1c22SToby Isaac 
1176ad8374ffSToby Isaac       ierr = ISGetIndices(label->points[v], &points);CHKERRQ(ierr);
1177c58f1c22SToby Isaac       for (i = 0; i < label->stratumSizes[v]; ++i) {
1178ad8374ffSToby Isaac         const PetscInt point = points[i];
1179c58f1c22SToby Isaac 
1180c58f1c22SToby Isaac         if ((point < label->pStart) || (point >= label->pEnd)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label point %D is not in [%D, %D)", point, label->pStart, label->pEnd);
1181c58f1c22SToby Isaac         ierr = PetscBTClear(label->bt, point - label->pStart);CHKERRQ(ierr);
1182c58f1c22SToby Isaac       }
1183ad8374ffSToby Isaac       ierr = ISRestoreIndices(label->points[v], &points);CHKERRQ(ierr);
1184c58f1c22SToby Isaac     }
1185c58f1c22SToby Isaac     label->stratumSizes[v] = 0;
11860c3c4a36SLisandro Dalcin     ierr = ISDestroy(&label->points[v]);CHKERRQ(ierr);
11870c3c4a36SLisandro Dalcin     ierr = ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_OWN_POINTER, &label->points[v]);CHKERRQ(ierr);
11880c3c4a36SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject) label->points[v], "indices");CHKERRQ(ierr);
1189c58f1c22SToby Isaac   } else {
1190b9907514SLisandro Dalcin     ierr = PetscHSetIClear(label->ht[v]);CHKERRQ(ierr);
1191c58f1c22SToby Isaac   }
1192c58f1c22SToby Isaac   PetscFunctionReturn(0);
1193c58f1c22SToby Isaac }
1194c58f1c22SToby Isaac 
119584f0b6dfSMatthew G. Knepley /*@
119684f0b6dfSMatthew G. Knepley   DMLabelFilter - Remove all points outside of [start, end)
119784f0b6dfSMatthew G. Knepley 
119884f0b6dfSMatthew G. Knepley   Input Parameters:
119984f0b6dfSMatthew G. Knepley + label - the DMLabel
120084f0b6dfSMatthew G. Knepley . start - the first point
120184f0b6dfSMatthew G. Knepley - end - the last point
120284f0b6dfSMatthew G. Knepley 
120384f0b6dfSMatthew G. Knepley   Level: intermediate
120484f0b6dfSMatthew G. Knepley 
120584f0b6dfSMatthew G. Knepley .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
120684f0b6dfSMatthew G. Knepley @*/
1207c58f1c22SToby Isaac PetscErrorCode DMLabelFilter(DMLabel label, PetscInt start, PetscInt end)
1208c58f1c22SToby Isaac {
1209c58f1c22SToby Isaac   PetscInt       v;
1210c58f1c22SToby Isaac   PetscErrorCode ierr;
1211c58f1c22SToby Isaac 
1212c58f1c22SToby Isaac   PetscFunctionBegin;
1213d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
12140c3c4a36SLisandro Dalcin   ierr = DMLabelDestroyIndex(label);CHKERRQ(ierr);
1215c58f1c22SToby Isaac   ierr = DMLabelMakeAllValid_Private(label);CHKERRQ(ierr);
1216c58f1c22SToby Isaac   for (v = 0; v < label->numStrata; ++v) {
1217c58f1c22SToby Isaac     PetscInt off, q;
1218ad8374ffSToby Isaac     const PetscInt *points;
1219033405d5SLisandro Dalcin     PetscInt numPointsNew = 0, *pointsNew = NULL;
1220c58f1c22SToby Isaac 
1221ad8374ffSToby Isaac     ierr = ISGetIndices(label->points[v], &points);CHKERRQ(ierr);
1222033405d5SLisandro Dalcin     for (q = 0; q < label->stratumSizes[v]; ++q)
1223033405d5SLisandro Dalcin       if (points[q] >= start && points[q] < end)
1224033405d5SLisandro Dalcin         numPointsNew++;
1225033405d5SLisandro Dalcin     ierr = PetscMalloc1(numPointsNew, &pointsNew);CHKERRQ(ierr);
1226c58f1c22SToby Isaac     for (off = 0, q = 0; q < label->stratumSizes[v]; ++q) {
1227033405d5SLisandro Dalcin       if (points[q] >= start && points[q] < end)
1228033405d5SLisandro Dalcin         pointsNew[off++] = points[q];
1229ad8374ffSToby Isaac     }
1230ad8374ffSToby Isaac     ierr = ISRestoreIndices(label->points[v],&points);CHKERRQ(ierr);
1231033405d5SLisandro Dalcin 
1232033405d5SLisandro Dalcin     label->stratumSizes[v] = numPointsNew;
1233033405d5SLisandro Dalcin     ierr = ISDestroy(&label->points[v]);CHKERRQ(ierr);
1234033405d5SLisandro Dalcin     ierr = ISCreateGeneral(PETSC_COMM_SELF,numPointsNew, pointsNew, PETSC_OWN_POINTER, &label->points[v]);CHKERRQ(ierr);
1235033405d5SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject) label->points[v], "indices");CHKERRQ(ierr);
1236c58f1c22SToby Isaac   }
1237c58f1c22SToby Isaac   ierr = DMLabelCreateIndex(label, start, end);CHKERRQ(ierr);
1238c58f1c22SToby Isaac   PetscFunctionReturn(0);
1239c58f1c22SToby Isaac }
1240c58f1c22SToby Isaac 
124184f0b6dfSMatthew G. Knepley /*@
124284f0b6dfSMatthew G. Knepley   DMLabelPermute - Create a new label with permuted points
124384f0b6dfSMatthew G. Knepley 
124484f0b6dfSMatthew G. Knepley   Input Parameters:
124584f0b6dfSMatthew G. Knepley + label - the DMLabel
124684f0b6dfSMatthew G. Knepley - permutation - the point permutation
124784f0b6dfSMatthew G. Knepley 
124884f0b6dfSMatthew G. Knepley   Output Parameter:
124984f0b6dfSMatthew G. Knepley . labelnew - the new label containing the permuted points
125084f0b6dfSMatthew G. Knepley 
125184f0b6dfSMatthew G. Knepley   Level: intermediate
125284f0b6dfSMatthew G. Knepley 
125384f0b6dfSMatthew G. Knepley .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
125484f0b6dfSMatthew G. Knepley @*/
1255c58f1c22SToby Isaac PetscErrorCode DMLabelPermute(DMLabel label, IS permutation, DMLabel *labelNew)
1256c58f1c22SToby Isaac {
1257c58f1c22SToby Isaac   const PetscInt *perm;
1258c58f1c22SToby Isaac   PetscInt        numValues, numPoints, v, q;
1259c58f1c22SToby Isaac   PetscErrorCode  ierr;
1260c58f1c22SToby Isaac 
1261c58f1c22SToby Isaac   PetscFunctionBegin;
1262d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
1263d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(permutation, IS_CLASSID, 2);
1264c58f1c22SToby Isaac   ierr = DMLabelMakeAllValid_Private(label);CHKERRQ(ierr);
1265c58f1c22SToby Isaac   ierr = DMLabelDuplicate(label, labelNew);CHKERRQ(ierr);
1266c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(*labelNew, &numValues);CHKERRQ(ierr);
1267c58f1c22SToby Isaac   ierr = ISGetLocalSize(permutation, &numPoints);CHKERRQ(ierr);
1268c58f1c22SToby Isaac   ierr = ISGetIndices(permutation, &perm);CHKERRQ(ierr);
1269c58f1c22SToby Isaac   for (v = 0; v < numValues; ++v) {
1270c58f1c22SToby Isaac     const PetscInt size   = (*labelNew)->stratumSizes[v];
1271ad8374ffSToby Isaac     const PetscInt *points;
1272ad8374ffSToby Isaac     PetscInt *pointsNew;
1273c58f1c22SToby Isaac 
1274ad8374ffSToby Isaac     ierr = ISGetIndices((*labelNew)->points[v],&points);CHKERRQ(ierr);
1275ad8374ffSToby Isaac     ierr = PetscMalloc1(size,&pointsNew);CHKERRQ(ierr);
1276c58f1c22SToby Isaac     for (q = 0; q < size; ++q) {
1277ad8374ffSToby Isaac       const PetscInt point = points[q];
1278c58f1c22SToby Isaac 
1279c58f1c22SToby Isaac       if ((point < 0) || (point >= numPoints)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label point %D is not in [0, %D) for the remapping", point, numPoints);
1280ad8374ffSToby Isaac       pointsNew[q] = perm[point];
1281c58f1c22SToby Isaac     }
1282ad8374ffSToby Isaac     ierr = ISRestoreIndices((*labelNew)->points[v],&points);CHKERRQ(ierr);
1283ad8374ffSToby Isaac     ierr = PetscSortInt(size, pointsNew);CHKERRQ(ierr);
1284ad8374ffSToby Isaac     ierr = ISDestroy(&((*labelNew)->points[v]));CHKERRQ(ierr);
1285fa8e8ae5SToby Isaac     if (size > 0 && pointsNew[size - 1] == pointsNew[0] + size - 1) {
1286fa8e8ae5SToby Isaac       ierr = ISCreateStride(PETSC_COMM_SELF,size,pointsNew[0],1,&((*labelNew)->points[v]));CHKERRQ(ierr);
1287fa8e8ae5SToby Isaac       ierr = PetscFree(pointsNew);CHKERRQ(ierr);
1288fa8e8ae5SToby Isaac     } else {
1289ad8374ffSToby Isaac       ierr = ISCreateGeneral(PETSC_COMM_SELF,size,pointsNew,PETSC_OWN_POINTER,&((*labelNew)->points[v]));CHKERRQ(ierr);
1290fa8e8ae5SToby Isaac     }
1291ad8374ffSToby Isaac     ierr = PetscObjectSetName((PetscObject) ((*labelNew)->points[v]), "indices");CHKERRQ(ierr);
1292c58f1c22SToby Isaac   }
1293c58f1c22SToby Isaac   ierr = ISRestoreIndices(permutation, &perm);CHKERRQ(ierr);
1294c58f1c22SToby Isaac   if (label->bt) {
1295c58f1c22SToby Isaac     ierr = PetscBTDestroy(&label->bt);CHKERRQ(ierr);
1296c58f1c22SToby Isaac     ierr = DMLabelCreateIndex(label, label->pStart, label->pEnd);CHKERRQ(ierr);
1297c58f1c22SToby Isaac   }
1298c58f1c22SToby Isaac   PetscFunctionReturn(0);
1299c58f1c22SToby Isaac }
1300c58f1c22SToby Isaac 
130126c55118SMichael Lange PetscErrorCode DMLabelDistribute_Internal(DMLabel label, PetscSF sf, PetscSection *leafSection, PetscInt **leafStrata)
130226c55118SMichael Lange {
130326c55118SMichael Lange   MPI_Comm       comm;
130426c55118SMichael Lange   PetscInt       s, l, nroots, nleaves, dof, offset, size;
130526c55118SMichael Lange   PetscInt      *remoteOffsets, *rootStrata, *rootIdx;
130626c55118SMichael Lange   PetscSection   rootSection;
130726c55118SMichael Lange   PetscSF        labelSF;
130826c55118SMichael Lange   PetscErrorCode ierr;
130926c55118SMichael Lange 
131026c55118SMichael Lange   PetscFunctionBegin;
131126c55118SMichael Lange   if (label) {ierr = DMLabelMakeAllValid_Private(label);CHKERRQ(ierr);}
131226c55118SMichael Lange   ierr = PetscObjectGetComm((PetscObject)sf, &comm);CHKERRQ(ierr);
131326c55118SMichael Lange   /* Build a section of stratum values per point, generate the according SF
131426c55118SMichael Lange      and distribute point-wise stratum values to leaves. */
131526c55118SMichael Lange   ierr = PetscSFGetGraph(sf, &nroots, &nleaves, NULL, NULL);CHKERRQ(ierr);
131626c55118SMichael Lange   ierr = PetscSectionCreate(comm, &rootSection);CHKERRQ(ierr);
131726c55118SMichael Lange   ierr = PetscSectionSetChart(rootSection, 0, nroots);CHKERRQ(ierr);
131826c55118SMichael Lange   if (label) {
131926c55118SMichael Lange     for (s = 0; s < label->numStrata; ++s) {
1320ad8374ffSToby Isaac       const PetscInt *points;
1321ad8374ffSToby Isaac 
1322ad8374ffSToby Isaac       ierr = ISGetIndices(label->points[s], &points);CHKERRQ(ierr);
132326c55118SMichael Lange       for (l = 0; l < label->stratumSizes[s]; l++) {
1324ad8374ffSToby Isaac         ierr = PetscSectionGetDof(rootSection, points[l], &dof);CHKERRQ(ierr);
1325ad8374ffSToby Isaac         ierr = PetscSectionSetDof(rootSection, points[l], dof+1);CHKERRQ(ierr);
132626c55118SMichael Lange       }
1327ad8374ffSToby Isaac       ierr = ISRestoreIndices(label->points[s], &points);CHKERRQ(ierr);
132826c55118SMichael Lange     }
132926c55118SMichael Lange   }
133026c55118SMichael Lange   ierr = PetscSectionSetUp(rootSection);CHKERRQ(ierr);
133126c55118SMichael Lange   /* Create a point-wise array of stratum values */
133226c55118SMichael Lange   ierr = PetscSectionGetStorageSize(rootSection, &size);CHKERRQ(ierr);
133326c55118SMichael Lange   ierr = PetscMalloc1(size, &rootStrata);CHKERRQ(ierr);
133426c55118SMichael Lange   ierr = PetscCalloc1(nroots, &rootIdx);CHKERRQ(ierr);
133526c55118SMichael Lange   if (label) {
133626c55118SMichael Lange     for (s = 0; s < label->numStrata; ++s) {
1337ad8374ffSToby Isaac       const PetscInt *points;
1338ad8374ffSToby Isaac 
1339ad8374ffSToby Isaac       ierr = ISGetIndices(label->points[s], &points);CHKERRQ(ierr);
134026c55118SMichael Lange       for (l = 0; l < label->stratumSizes[s]; l++) {
1341ad8374ffSToby Isaac         const PetscInt p = points[l];
134226c55118SMichael Lange         ierr = PetscSectionGetOffset(rootSection, p, &offset);CHKERRQ(ierr);
134326c55118SMichael Lange         rootStrata[offset+rootIdx[p]++] = label->stratumValues[s];
134426c55118SMichael Lange       }
1345ad8374ffSToby Isaac       ierr = ISRestoreIndices(label->points[s], &points);CHKERRQ(ierr);
134626c55118SMichael Lange     }
134726c55118SMichael Lange   }
134826c55118SMichael Lange   /* Build SF that maps label points to remote processes */
134926c55118SMichael Lange   ierr = PetscSectionCreate(comm, leafSection);CHKERRQ(ierr);
135026c55118SMichael Lange   ierr = PetscSFDistributeSection(sf, rootSection, &remoteOffsets, *leafSection);CHKERRQ(ierr);
135126c55118SMichael Lange   ierr = PetscSFCreateSectionSF(sf, rootSection, remoteOffsets, *leafSection, &labelSF);CHKERRQ(ierr);
135226c55118SMichael Lange   ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
135326c55118SMichael Lange   /* Send the strata for each point over the derived SF */
135426c55118SMichael Lange   ierr = PetscSectionGetStorageSize(*leafSection, &size);CHKERRQ(ierr);
135526c55118SMichael Lange   ierr = PetscMalloc1(size, leafStrata);CHKERRQ(ierr);
135626c55118SMichael Lange   ierr = PetscSFBcastBegin(labelSF, MPIU_INT, rootStrata, *leafStrata);CHKERRQ(ierr);
135726c55118SMichael Lange   ierr = PetscSFBcastEnd(labelSF, MPIU_INT, rootStrata, *leafStrata);CHKERRQ(ierr);
135826c55118SMichael Lange   /* Clean up */
135926c55118SMichael Lange   ierr = PetscFree(rootStrata);CHKERRQ(ierr);
136026c55118SMichael Lange   ierr = PetscFree(rootIdx);CHKERRQ(ierr);
136126c55118SMichael Lange   ierr = PetscSectionDestroy(&rootSection);CHKERRQ(ierr);
136226c55118SMichael Lange   ierr = PetscSFDestroy(&labelSF);CHKERRQ(ierr);
136326c55118SMichael Lange   PetscFunctionReturn(0);
136426c55118SMichael Lange }
136526c55118SMichael Lange 
136684f0b6dfSMatthew G. Knepley /*@
136784f0b6dfSMatthew G. Knepley   DMLabelDistribute - Create a new label pushed forward over the PetscSF
136884f0b6dfSMatthew G. Knepley 
136984f0b6dfSMatthew G. Knepley   Input Parameters:
137084f0b6dfSMatthew G. Knepley + label - the DMLabel
137184f0b6dfSMatthew G. Knepley - sf    - the map from old to new distribution
137284f0b6dfSMatthew G. Knepley 
137384f0b6dfSMatthew G. Knepley   Output Parameter:
137484f0b6dfSMatthew G. Knepley . labelnew - the new redistributed label
137584f0b6dfSMatthew G. Knepley 
137684f0b6dfSMatthew G. Knepley   Level: intermediate
137784f0b6dfSMatthew G. Knepley 
137884f0b6dfSMatthew G. Knepley .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
137984f0b6dfSMatthew G. Knepley @*/
1380c58f1c22SToby Isaac PetscErrorCode DMLabelDistribute(DMLabel label, PetscSF sf, DMLabel *labelNew)
1381c58f1c22SToby Isaac {
1382c58f1c22SToby Isaac   MPI_Comm       comm;
138326c55118SMichael Lange   PetscSection   leafSection;
138426c55118SMichael Lange   PetscInt       p, pStart, pEnd, s, size, dof, offset, stratum;
138526c55118SMichael Lange   PetscInt      *leafStrata, *strataIdx;
1386ad8374ffSToby Isaac   PetscInt     **points;
1387d67d17b1SMatthew G. Knepley   const char    *lname = NULL;
1388c58f1c22SToby Isaac   char          *name;
1389c58f1c22SToby Isaac   PetscInt       nameSize;
1390e8f14785SLisandro Dalcin   PetscHSetI     stratumHash;
1391c58f1c22SToby Isaac   size_t         len = 0;
139226c55118SMichael Lange   PetscMPIInt    rank;
1393c58f1c22SToby Isaac   PetscErrorCode ierr;
1394c58f1c22SToby Isaac 
1395c58f1c22SToby Isaac   PetscFunctionBegin;
1396d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
1397f018e600SMatthew G. Knepley   if (label) {
1398f018e600SMatthew G. Knepley     PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
1399f018e600SMatthew G. Knepley     ierr = DMLabelMakeAllValid_Private(label);CHKERRQ(ierr);
1400f018e600SMatthew G. Knepley   }
1401c58f1c22SToby Isaac   ierr = PetscObjectGetComm((PetscObject)sf, &comm);CHKERRQ(ierr);
1402c58f1c22SToby Isaac   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
1403c58f1c22SToby Isaac   /* Bcast name */
1404d67d17b1SMatthew G. Knepley   if (!rank) {
1405d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
1406d67d17b1SMatthew G. Knepley     ierr = PetscStrlen(lname, &len);CHKERRQ(ierr);
1407d67d17b1SMatthew G. Knepley   }
1408c58f1c22SToby Isaac   nameSize = len;
1409c58f1c22SToby Isaac   ierr = MPI_Bcast(&nameSize, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1410c58f1c22SToby Isaac   ierr = PetscMalloc1(nameSize+1, &name);CHKERRQ(ierr);
1411d67d17b1SMatthew G. Knepley   if (!rank) {ierr = PetscMemcpy(name, lname, nameSize+1);CHKERRQ(ierr);}
1412c58f1c22SToby Isaac   ierr = MPI_Bcast(name, nameSize+1, MPI_CHAR, 0, comm);CHKERRQ(ierr);
1413d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, name, labelNew);CHKERRQ(ierr);
1414c58f1c22SToby Isaac   ierr = PetscFree(name);CHKERRQ(ierr);
141577d236dfSMichael Lange   /* Bcast defaultValue */
141677d236dfSMichael Lange   if (!rank) (*labelNew)->defaultValue = label->defaultValue;
141777d236dfSMichael Lange   ierr = MPI_Bcast(&(*labelNew)->defaultValue, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
141826c55118SMichael Lange   /* Distribute stratum values over the SF and get the point mapping on the receiver */
141926c55118SMichael Lange   ierr = DMLabelDistribute_Internal(label, sf, &leafSection, &leafStrata);CHKERRQ(ierr);
14205cbdf6fcSMichael Lange   /* Determine received stratum values and initialise new label*/
1421e8f14785SLisandro Dalcin   ierr = PetscHSetICreate(&stratumHash);CHKERRQ(ierr);
142226c55118SMichael Lange   ierr = PetscSectionGetStorageSize(leafSection, &size);CHKERRQ(ierr);
1423e8f14785SLisandro Dalcin   for (p = 0; p < size; ++p) {ierr = PetscHSetIAdd(stratumHash, leafStrata[p]);CHKERRQ(ierr);}
1424e8f14785SLisandro Dalcin   ierr = PetscHSetIGetSize(stratumHash, &(*labelNew)->numStrata);CHKERRQ(ierr);
1425ad8374ffSToby Isaac   ierr = PetscMalloc1((*labelNew)->numStrata, &(*labelNew)->validIS);CHKERRQ(ierr);
1426ad8374ffSToby Isaac   for (s = 0; s < (*labelNew)->numStrata; ++s) (*labelNew)->validIS[s] = PETSC_TRUE;
14275cbdf6fcSMichael Lange   ierr = PetscMalloc1((*labelNew)->numStrata, &(*labelNew)->stratumValues);CHKERRQ(ierr);
14285cbdf6fcSMichael Lange   /* Turn leafStrata into indices rather than stratum values */
14295cbdf6fcSMichael Lange   offset = 0;
1430e8f14785SLisandro Dalcin   ierr = PetscHSetIGetElems(stratumHash, &offset, (*labelNew)->stratumValues);CHKERRQ(ierr);
1431a205f1fdSToby Isaac   ierr = PetscSortInt((*labelNew)->numStrata,(*labelNew)->stratumValues);CHKERRQ(ierr);
143290e9b2aeSLisandro Dalcin   for (s = 0; s < (*labelNew)->numStrata; ++s) {
143390e9b2aeSLisandro Dalcin     ierr = PetscHMapISet((*labelNew)->hmap, (*labelNew)->stratumValues[s], s);CHKERRQ(ierr);
143490e9b2aeSLisandro Dalcin   }
14355cbdf6fcSMichael Lange   for (p = 0; p < size; ++p) {
1436231b9e6fSMatthew G. Knepley     for (s = 0; s < (*labelNew)->numStrata; ++s) {
1437231b9e6fSMatthew G. Knepley       if (leafStrata[p] == (*labelNew)->stratumValues[s]) {leafStrata[p] = s; break;}
14385cbdf6fcSMichael Lange     }
14395cbdf6fcSMichael Lange   }
1440c58f1c22SToby Isaac   /* Rebuild the point strata on the receiver */
1441c58f1c22SToby Isaac   ierr = PetscCalloc1((*labelNew)->numStrata,&(*labelNew)->stratumSizes);CHKERRQ(ierr);
1442c58f1c22SToby Isaac   ierr = PetscSectionGetChart(leafSection, &pStart, &pEnd);CHKERRQ(ierr);
1443c58f1c22SToby Isaac   for (p=pStart; p<pEnd; p++) {
1444c58f1c22SToby Isaac     ierr = PetscSectionGetDof(leafSection, p, &dof);CHKERRQ(ierr);
1445c58f1c22SToby Isaac     ierr = PetscSectionGetOffset(leafSection, p, &offset);CHKERRQ(ierr);
1446c58f1c22SToby Isaac     for (s=0; s<dof; s++) {
1447c58f1c22SToby Isaac       (*labelNew)->stratumSizes[leafStrata[offset+s]]++;
1448c58f1c22SToby Isaac     }
1449c58f1c22SToby Isaac   }
1450c58f1c22SToby Isaac   ierr = PetscCalloc1((*labelNew)->numStrata,&(*labelNew)->ht);CHKERRQ(ierr);
1451c58f1c22SToby Isaac   ierr = PetscMalloc1((*labelNew)->numStrata,&(*labelNew)->points);CHKERRQ(ierr);
1452ad8374ffSToby Isaac   ierr = PetscMalloc1((*labelNew)->numStrata,&points);CHKERRQ(ierr);
1453c58f1c22SToby Isaac   for (s = 0; s < (*labelNew)->numStrata; ++s) {
1454e8f14785SLisandro Dalcin     ierr = PetscHSetICreate(&(*labelNew)->ht[s]);CHKERRQ(ierr);
1455ad8374ffSToby Isaac     ierr = PetscMalloc1((*labelNew)->stratumSizes[s], &(points[s]));CHKERRQ(ierr);
1456c58f1c22SToby Isaac   }
1457c58f1c22SToby Isaac   /* Insert points into new strata */
1458c58f1c22SToby Isaac   ierr = PetscCalloc1((*labelNew)->numStrata, &strataIdx);CHKERRQ(ierr);
1459c58f1c22SToby Isaac   ierr = PetscSectionGetChart(leafSection, &pStart, &pEnd);CHKERRQ(ierr);
1460c58f1c22SToby Isaac   for (p=pStart; p<pEnd; p++) {
1461c58f1c22SToby Isaac     ierr = PetscSectionGetDof(leafSection, p, &dof);CHKERRQ(ierr);
1462c58f1c22SToby Isaac     ierr = PetscSectionGetOffset(leafSection, p, &offset);CHKERRQ(ierr);
1463c58f1c22SToby Isaac     for (s=0; s<dof; s++) {
1464c58f1c22SToby Isaac       stratum = leafStrata[offset+s];
1465ad8374ffSToby Isaac       points[stratum][strataIdx[stratum]++] = p;
1466c58f1c22SToby Isaac     }
1467c58f1c22SToby Isaac   }
1468ad8374ffSToby Isaac   for (s = 0; s < (*labelNew)->numStrata; s++) {
1469ad8374ffSToby Isaac     ierr = ISCreateGeneral(PETSC_COMM_SELF,(*labelNew)->stratumSizes[s],&(points[s][0]),PETSC_OWN_POINTER,&((*labelNew)->points[s]));CHKERRQ(ierr);
1470ad8374ffSToby Isaac     ierr = PetscObjectSetName((PetscObject)((*labelNew)->points[s]),"indices");CHKERRQ(ierr);
1471ad8374ffSToby Isaac   }
1472ad8374ffSToby Isaac   ierr = PetscFree(points);CHKERRQ(ierr);
1473e8f14785SLisandro Dalcin   ierr = PetscHSetIDestroy(&stratumHash);CHKERRQ(ierr);
1474c58f1c22SToby Isaac   ierr = PetscFree(leafStrata);CHKERRQ(ierr);
1475c58f1c22SToby Isaac   ierr = PetscFree(strataIdx);CHKERRQ(ierr);
1476c58f1c22SToby Isaac   ierr = PetscSectionDestroy(&leafSection);CHKERRQ(ierr);
1477c58f1c22SToby Isaac   PetscFunctionReturn(0);
1478c58f1c22SToby Isaac }
1479c58f1c22SToby Isaac 
14807937d9ceSMichael Lange /*@
14817937d9ceSMichael Lange   DMLabelGather - Gather all label values from leafs into roots
14827937d9ceSMichael Lange 
14837937d9ceSMichael Lange   Input Parameters:
14847937d9ceSMichael Lange + label - the DMLabel
148584f0b6dfSMatthew G. Knepley - sf - the Star Forest point communication map
14867937d9ceSMichael Lange 
148784f0b6dfSMatthew G. Knepley   Output Parameters:
148884f0b6dfSMatthew G. Knepley . labelNew - the new DMLabel with localised leaf values
14897937d9ceSMichael Lange 
14907937d9ceSMichael Lange   Level: developer
14917937d9ceSMichael Lange 
14927937d9ceSMichael Lange   Note: This is the inverse operation to DMLabelDistribute.
14937937d9ceSMichael Lange 
14947937d9ceSMichael Lange .seealso: DMLabelDistribute()
14957937d9ceSMichael Lange @*/
14967937d9ceSMichael Lange PetscErrorCode DMLabelGather(DMLabel label, PetscSF sf, DMLabel *labelNew)
14977937d9ceSMichael Lange {
14987937d9ceSMichael Lange   MPI_Comm       comm;
14997937d9ceSMichael Lange   PetscSection   rootSection;
15007937d9ceSMichael Lange   PetscSF        sfLabel;
15017937d9ceSMichael Lange   PetscSFNode   *rootPoints, *leafPoints;
15027937d9ceSMichael Lange   PetscInt       p, s, d, nroots, nleaves, nmultiroots, idx, dof, offset;
15037937d9ceSMichael Lange   const PetscInt *rootDegree, *ilocal;
15047937d9ceSMichael Lange   PetscInt       *rootStrata;
1505d67d17b1SMatthew G. Knepley   const char    *lname;
15067937d9ceSMichael Lange   char          *name;
15077937d9ceSMichael Lange   PetscInt       nameSize;
15087937d9ceSMichael Lange   size_t         len = 0;
15099852e123SBarry Smith   PetscMPIInt    rank, size;
15107937d9ceSMichael Lange   PetscErrorCode ierr;
15117937d9ceSMichael Lange 
15127937d9ceSMichael Lange   PetscFunctionBegin;
1513d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
1514d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
15157937d9ceSMichael Lange   ierr = PetscObjectGetComm((PetscObject)sf, &comm);CHKERRQ(ierr);
15167937d9ceSMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
15179852e123SBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
15187937d9ceSMichael Lange   /* Bcast name */
1519d67d17b1SMatthew G. Knepley   if (!rank) {
1520d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
1521d67d17b1SMatthew G. Knepley     ierr = PetscStrlen(lname, &len);CHKERRQ(ierr);
1522d67d17b1SMatthew G. Knepley   }
15237937d9ceSMichael Lange   nameSize = len;
15247937d9ceSMichael Lange   ierr = MPI_Bcast(&nameSize, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
15257937d9ceSMichael Lange   ierr = PetscMalloc1(nameSize+1, &name);CHKERRQ(ierr);
1526d67d17b1SMatthew G. Knepley   if (!rank) {ierr = PetscMemcpy(name, lname, nameSize+1);CHKERRQ(ierr);}
15277937d9ceSMichael Lange   ierr = MPI_Bcast(name, nameSize+1, MPI_CHAR, 0, comm);CHKERRQ(ierr);
1528d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, name, labelNew);CHKERRQ(ierr);
15297937d9ceSMichael Lange   ierr = PetscFree(name);CHKERRQ(ierr);
15307937d9ceSMichael Lange   /* Gather rank/index pairs of leaves into local roots to build
15317937d9ceSMichael Lange      an inverse, multi-rooted SF. Note that this ignores local leaf
15327937d9ceSMichael Lange      indexing due to the use of the multiSF in PetscSFGather. */
15337937d9ceSMichael Lange   ierr = PetscSFGetGraph(sf, &nroots, &nleaves, &ilocal, NULL);CHKERRQ(ierr);
1534dc53bc9bSMatthew G. Knepley   ierr = PetscMalloc1(nroots, &leafPoints);CHKERRQ(ierr);
1535dc53bc9bSMatthew G. Knepley   for (p = 0; p < nroots; ++p) leafPoints[p].rank = leafPoints[p].index = -1;
15367937d9ceSMichael Lange   for (p = 0; p < nleaves; p++) {
15378212dd46SStefano Zampini     PetscInt ilp = ilocal ? ilocal[p] : p;
15388212dd46SStefano Zampini 
15398212dd46SStefano Zampini     leafPoints[ilp].index = ilp;
15408212dd46SStefano Zampini     leafPoints[ilp].rank  = rank;
15417937d9ceSMichael Lange   }
15427937d9ceSMichael Lange   ierr = PetscSFComputeDegreeBegin(sf, &rootDegree);CHKERRQ(ierr);
15437937d9ceSMichael Lange   ierr = PetscSFComputeDegreeEnd(sf, &rootDegree);CHKERRQ(ierr);
15447937d9ceSMichael Lange   for (p = 0, nmultiroots = 0; p < nroots; ++p) nmultiroots += rootDegree[p];
15457937d9ceSMichael Lange   ierr = PetscMalloc1(nmultiroots, &rootPoints);CHKERRQ(ierr);
15467937d9ceSMichael Lange   ierr = PetscSFGatherBegin(sf, MPIU_2INT, leafPoints, rootPoints);CHKERRQ(ierr);
15477937d9ceSMichael Lange   ierr = PetscSFGatherEnd(sf, MPIU_2INT, leafPoints, rootPoints);CHKERRQ(ierr);
15487937d9ceSMichael Lange   ierr = PetscSFCreate(comm,& sfLabel);CHKERRQ(ierr);
15497937d9ceSMichael Lange   ierr = PetscSFSetGraph(sfLabel, nroots, nmultiroots, NULL, PETSC_OWN_POINTER, rootPoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
15507937d9ceSMichael Lange   /* Migrate label over inverted SF to pull stratum values at leaves into roots. */
15517937d9ceSMichael Lange   ierr = DMLabelDistribute_Internal(label, sfLabel, &rootSection, &rootStrata);CHKERRQ(ierr);
15527937d9ceSMichael Lange   /* Rebuild the point strata on the receiver */
15537937d9ceSMichael Lange   for (p = 0, idx = 0; p < nroots; p++) {
15547937d9ceSMichael Lange     for (d = 0; d < rootDegree[p]; d++) {
15557937d9ceSMichael Lange       ierr = PetscSectionGetDof(rootSection, idx+d, &dof);CHKERRQ(ierr);
15567937d9ceSMichael Lange       ierr = PetscSectionGetOffset(rootSection, idx+d, &offset);CHKERRQ(ierr);
15577937d9ceSMichael Lange       for (s = 0; s < dof; s++) {ierr = DMLabelSetValue(*labelNew, p, rootStrata[offset+s]);CHKERRQ(ierr);}
15587937d9ceSMichael Lange     }
15597937d9ceSMichael Lange     idx += rootDegree[p];
15607937d9ceSMichael Lange   }
156177e0c0e7SMichael Lange   ierr = PetscFree(leafPoints);CHKERRQ(ierr);
156277e0c0e7SMichael Lange   ierr = PetscFree(rootStrata);CHKERRQ(ierr);
156377e0c0e7SMichael Lange   ierr = PetscSectionDestroy(&rootSection);CHKERRQ(ierr);
156477e0c0e7SMichael Lange   ierr = PetscSFDestroy(&sfLabel);CHKERRQ(ierr);
15657937d9ceSMichael Lange   PetscFunctionReturn(0);
15667937d9ceSMichael Lange }
15677937d9ceSMichael Lange 
156884f0b6dfSMatthew G. Knepley /*@
156984f0b6dfSMatthew G. Knepley   DMLabelConvertToSection - Make a PetscSection/IS pair that encodes the label
157084f0b6dfSMatthew G. Knepley 
157184f0b6dfSMatthew G. Knepley   Input Parameter:
157284f0b6dfSMatthew G. Knepley . label - the DMLabel
157384f0b6dfSMatthew G. Knepley 
157484f0b6dfSMatthew G. Knepley   Output Parameters:
157584f0b6dfSMatthew G. Knepley + section - the section giving offsets for each stratum
157684f0b6dfSMatthew G. Knepley - is - An IS containing all the label points
157784f0b6dfSMatthew G. Knepley 
157884f0b6dfSMatthew G. Knepley   Level: developer
157984f0b6dfSMatthew G. Knepley 
158084f0b6dfSMatthew G. Knepley .seealso: DMLabelDistribute()
158184f0b6dfSMatthew G. Knepley @*/
1582c58f1c22SToby Isaac PetscErrorCode DMLabelConvertToSection(DMLabel label, PetscSection *section, IS *is)
1583c58f1c22SToby Isaac {
1584c58f1c22SToby Isaac   IS              vIS;
1585c58f1c22SToby Isaac   const PetscInt *values;
1586c58f1c22SToby Isaac   PetscInt       *points;
1587c58f1c22SToby Isaac   PetscInt        nV, vS = 0, vE = 0, v, N;
1588c58f1c22SToby Isaac   PetscErrorCode  ierr;
1589c58f1c22SToby Isaac 
1590c58f1c22SToby Isaac   PetscFunctionBegin;
1591d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
1592c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, &nV);CHKERRQ(ierr);
1593c58f1c22SToby Isaac   ierr = DMLabelGetValueIS(label, &vIS);CHKERRQ(ierr);
1594c58f1c22SToby Isaac   ierr = ISGetIndices(vIS, &values);CHKERRQ(ierr);
1595c58f1c22SToby Isaac   if (nV) {vS = values[0]; vE = values[0]+1;}
1596c58f1c22SToby Isaac   for (v = 1; v < nV; ++v) {
1597c58f1c22SToby Isaac     vS = PetscMin(vS, values[v]);
1598c58f1c22SToby Isaac     vE = PetscMax(vE, values[v]+1);
1599c58f1c22SToby Isaac   }
1600c58f1c22SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF, section);CHKERRQ(ierr);
1601c58f1c22SToby Isaac   ierr = PetscSectionSetChart(*section, vS, vE);CHKERRQ(ierr);
1602c58f1c22SToby Isaac   for (v = 0; v < nV; ++v) {
1603c58f1c22SToby Isaac     PetscInt n;
1604c58f1c22SToby Isaac 
1605c58f1c22SToby Isaac     ierr = DMLabelGetStratumSize(label, values[v], &n);CHKERRQ(ierr);
1606c58f1c22SToby Isaac     ierr = PetscSectionSetDof(*section, values[v], n);CHKERRQ(ierr);
1607c58f1c22SToby Isaac   }
1608c58f1c22SToby Isaac   ierr = PetscSectionSetUp(*section);CHKERRQ(ierr);
1609c58f1c22SToby Isaac   ierr = PetscSectionGetStorageSize(*section, &N);CHKERRQ(ierr);
1610c58f1c22SToby Isaac   ierr = PetscMalloc1(N, &points);CHKERRQ(ierr);
1611c58f1c22SToby Isaac   for (v = 0; v < nV; ++v) {
1612c58f1c22SToby Isaac     IS              is;
1613c58f1c22SToby Isaac     const PetscInt *spoints;
1614c58f1c22SToby Isaac     PetscInt        dof, off, p;
1615c58f1c22SToby Isaac 
1616c58f1c22SToby Isaac     ierr = PetscSectionGetDof(*section, values[v], &dof);CHKERRQ(ierr);
1617c58f1c22SToby Isaac     ierr = PetscSectionGetOffset(*section, values[v], &off);CHKERRQ(ierr);
1618c58f1c22SToby Isaac     ierr = DMLabelGetStratumIS(label, values[v], &is);CHKERRQ(ierr);
1619c58f1c22SToby Isaac     ierr = ISGetIndices(is, &spoints);CHKERRQ(ierr);
1620c58f1c22SToby Isaac     for (p = 0; p < dof; ++p) points[off+p] = spoints[p];
1621c58f1c22SToby Isaac     ierr = ISRestoreIndices(is, &spoints);CHKERRQ(ierr);
1622c58f1c22SToby Isaac     ierr = ISDestroy(&is);CHKERRQ(ierr);
1623c58f1c22SToby Isaac   }
1624c58f1c22SToby Isaac   ierr = ISRestoreIndices(vIS, &values);CHKERRQ(ierr);
1625c58f1c22SToby Isaac   ierr = ISDestroy(&vIS);CHKERRQ(ierr);
1626c58f1c22SToby Isaac   ierr = ISCreateGeneral(PETSC_COMM_SELF, N, points, PETSC_OWN_POINTER, is);CHKERRQ(ierr);
1627c58f1c22SToby Isaac   PetscFunctionReturn(0);
1628c58f1c22SToby Isaac }
1629c58f1c22SToby Isaac 
163084f0b6dfSMatthew G. Knepley /*@
1631c58f1c22SToby Isaac   PetscSectionCreateGlobalSectionLabel - Create a section describing the global field layout using
1632c58f1c22SToby Isaac   the local section and an SF describing the section point overlap.
1633c58f1c22SToby Isaac 
1634c58f1c22SToby Isaac   Input Parameters:
1635c58f1c22SToby Isaac   + s - The PetscSection for the local field layout
1636c58f1c22SToby Isaac   . sf - The SF describing parallel layout of the section points
1637c58f1c22SToby Isaac   . includeConstraints - By default this is PETSC_FALSE, meaning that the global field vector will not possess constrained dofs
1638c58f1c22SToby Isaac   . label - The label specifying the points
1639c58f1c22SToby Isaac   - labelValue - The label stratum specifying the points
1640c58f1c22SToby Isaac 
1641c58f1c22SToby Isaac   Output Parameter:
1642c58f1c22SToby Isaac   . gsection - The PetscSection for the global field layout
1643c58f1c22SToby Isaac 
1644c58f1c22SToby Isaac   Note: This gives negative sizes and offsets to points not owned by this process
1645c58f1c22SToby Isaac 
1646c58f1c22SToby Isaac   Level: developer
1647c58f1c22SToby Isaac 
1648c58f1c22SToby Isaac .seealso: PetscSectionCreate()
1649c58f1c22SToby Isaac @*/
1650c58f1c22SToby Isaac PetscErrorCode PetscSectionCreateGlobalSectionLabel(PetscSection s, PetscSF sf, PetscBool includeConstraints, DMLabel label, PetscInt labelValue, PetscSection *gsection)
1651c58f1c22SToby Isaac {
1652c58f1c22SToby Isaac   PetscInt      *neg = NULL, *tmpOff = NULL;
1653c58f1c22SToby Isaac   PetscInt       pStart, pEnd, p, dof, cdof, off, globalOff = 0, nroots;
1654c58f1c22SToby Isaac   PetscErrorCode ierr;
1655c58f1c22SToby Isaac 
1656c58f1c22SToby Isaac   PetscFunctionBegin;
1657d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
1658d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
1659d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
1660c58f1c22SToby Isaac   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) s), gsection);CHKERRQ(ierr);
1661c58f1c22SToby Isaac   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
1662c58f1c22SToby Isaac   ierr = PetscSectionSetChart(*gsection, pStart, pEnd);CHKERRQ(ierr);
1663c58f1c22SToby Isaac   ierr = PetscSFGetGraph(sf, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
1664c58f1c22SToby Isaac   if (nroots >= 0) {
1665c58f1c22SToby Isaac     if (nroots < pEnd-pStart) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "PetscSF nroots %d < %d section size", nroots, pEnd-pStart);
1666c58f1c22SToby Isaac     ierr = PetscCalloc1(nroots, &neg);CHKERRQ(ierr);
1667c58f1c22SToby Isaac     if (nroots > pEnd-pStart) {
1668c58f1c22SToby Isaac       ierr = PetscCalloc1(nroots, &tmpOff);CHKERRQ(ierr);
1669c58f1c22SToby Isaac     } else {
1670c58f1c22SToby Isaac       tmpOff = &(*gsection)->atlasDof[-pStart];
1671c58f1c22SToby Isaac     }
1672c58f1c22SToby Isaac   }
1673c58f1c22SToby Isaac   /* Mark ghost points with negative dof */
1674c58f1c22SToby Isaac   for (p = pStart; p < pEnd; ++p) {
1675c58f1c22SToby Isaac     PetscInt value;
1676c58f1c22SToby Isaac 
1677c58f1c22SToby Isaac     ierr = DMLabelGetValue(label, p, &value);CHKERRQ(ierr);
1678c58f1c22SToby Isaac     if (value != labelValue) continue;
1679c58f1c22SToby Isaac     ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
1680c58f1c22SToby Isaac     ierr = PetscSectionSetDof(*gsection, p, dof);CHKERRQ(ierr);
1681c58f1c22SToby Isaac     ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
1682c58f1c22SToby Isaac     if (!includeConstraints && cdof > 0) {ierr = PetscSectionSetConstraintDof(*gsection, p, cdof);CHKERRQ(ierr);}
1683c58f1c22SToby Isaac     if (neg) neg[p] = -(dof+1);
1684c58f1c22SToby Isaac   }
1685c58f1c22SToby Isaac   ierr = PetscSectionSetUpBC(*gsection);CHKERRQ(ierr);
1686c58f1c22SToby Isaac   if (nroots >= 0) {
1687c58f1c22SToby Isaac     ierr = PetscSFBcastBegin(sf, MPIU_INT, neg, tmpOff);CHKERRQ(ierr);
1688c58f1c22SToby Isaac     ierr = PetscSFBcastEnd(sf, MPIU_INT, neg, tmpOff);CHKERRQ(ierr);
1689c58f1c22SToby Isaac     if (nroots > pEnd-pStart) {
1690c58f1c22SToby Isaac       for (p = pStart; p < pEnd; ++p) {if (tmpOff[p] < 0) (*gsection)->atlasDof[p-pStart] = tmpOff[p];}
1691c58f1c22SToby Isaac     }
1692c58f1c22SToby Isaac   }
1693c58f1c22SToby Isaac   /* Calculate new sizes, get proccess offset, and calculate point offsets */
1694c58f1c22SToby Isaac   for (p = 0, off = 0; p < pEnd-pStart; ++p) {
1695c58f1c22SToby Isaac     cdof = (!includeConstraints && s->bc) ? s->bc->atlasDof[p] : 0;
1696c58f1c22SToby Isaac     (*gsection)->atlasOff[p] = off;
1697c58f1c22SToby Isaac     off += (*gsection)->atlasDof[p] > 0 ? (*gsection)->atlasDof[p]-cdof : 0;
1698c58f1c22SToby Isaac   }
1699c58f1c22SToby Isaac   ierr       = MPI_Scan(&off, &globalOff, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject) s));CHKERRQ(ierr);
1700c58f1c22SToby Isaac   globalOff -= off;
1701c58f1c22SToby Isaac   for (p = 0, off = 0; p < pEnd-pStart; ++p) {
1702c58f1c22SToby Isaac     (*gsection)->atlasOff[p] += globalOff;
1703c58f1c22SToby Isaac     if (neg) neg[p] = -((*gsection)->atlasOff[p]+1);
1704c58f1c22SToby Isaac   }
1705c58f1c22SToby Isaac   /* Put in negative offsets for ghost points */
1706c58f1c22SToby Isaac   if (nroots >= 0) {
1707c58f1c22SToby Isaac     ierr = PetscSFBcastBegin(sf, MPIU_INT, neg, tmpOff);CHKERRQ(ierr);
1708c58f1c22SToby Isaac     ierr = PetscSFBcastEnd(sf, MPIU_INT, neg, tmpOff);CHKERRQ(ierr);
1709c58f1c22SToby Isaac     if (nroots > pEnd-pStart) {
1710c58f1c22SToby Isaac       for (p = pStart; p < pEnd; ++p) {if (tmpOff[p] < 0) (*gsection)->atlasOff[p-pStart] = tmpOff[p];}
1711c58f1c22SToby Isaac     }
1712c58f1c22SToby Isaac   }
1713c58f1c22SToby Isaac   if (nroots >= 0 && nroots > pEnd-pStart) {ierr = PetscFree(tmpOff);CHKERRQ(ierr);}
1714c58f1c22SToby Isaac   ierr = PetscFree(neg);CHKERRQ(ierr);
1715c58f1c22SToby Isaac   PetscFunctionReturn(0);
1716c58f1c22SToby Isaac }
1717c58f1c22SToby Isaac 
17185fdea053SToby Isaac typedef struct _n_PetscSectionSym_Label
17195fdea053SToby Isaac {
17205fdea053SToby Isaac   DMLabel           label;
17215fdea053SToby Isaac   PetscCopyMode     *modes;
17225fdea053SToby Isaac   PetscInt          *sizes;
17235fdea053SToby Isaac   const PetscInt    ***perms;
17245fdea053SToby Isaac   const PetscScalar ***rots;
17255fdea053SToby Isaac   PetscInt          (*minMaxOrients)[2];
17265fdea053SToby Isaac   PetscInt          numStrata; /* numStrata is only increasing, functions as a state */
17275fdea053SToby Isaac } PetscSectionSym_Label;
17285fdea053SToby Isaac 
17295fdea053SToby Isaac static PetscErrorCode PetscSectionSymLabelReset(PetscSectionSym sym)
17305fdea053SToby Isaac {
17315fdea053SToby Isaac   PetscInt              i, j;
17325fdea053SToby Isaac   PetscSectionSym_Label *sl = (PetscSectionSym_Label *) sym->data;
17335fdea053SToby Isaac   PetscErrorCode        ierr;
17345fdea053SToby Isaac 
17355fdea053SToby Isaac   PetscFunctionBegin;
17365fdea053SToby Isaac   for (i = 0; i <= sl->numStrata; i++) {
17375fdea053SToby Isaac     if (sl->modes[i] == PETSC_OWN_POINTER || sl->modes[i] == PETSC_COPY_VALUES) {
17385fdea053SToby Isaac       for (j = sl->minMaxOrients[i][0]; j < sl->minMaxOrients[i][1]; j++) {
17395fdea053SToby Isaac         if (sl->perms[i]) {ierr = PetscFree(sl->perms[i][j]);CHKERRQ(ierr);}
17405fdea053SToby Isaac         if (sl->rots[i]) {ierr = PetscFree(sl->rots[i][j]);CHKERRQ(ierr);}
17415fdea053SToby Isaac       }
17425fdea053SToby Isaac       if (sl->perms[i]) {
17435fdea053SToby Isaac         const PetscInt **perms = &sl->perms[i][sl->minMaxOrients[i][0]];
17445fdea053SToby Isaac 
17455fdea053SToby Isaac         ierr = PetscFree(perms);CHKERRQ(ierr);
17465fdea053SToby Isaac       }
17475fdea053SToby Isaac       if (sl->rots[i]) {
17485fdea053SToby Isaac         const PetscScalar **rots = &sl->rots[i][sl->minMaxOrients[i][0]];
17495fdea053SToby Isaac 
17505fdea053SToby Isaac         ierr = PetscFree(rots);CHKERRQ(ierr);
17515fdea053SToby Isaac       }
17525fdea053SToby Isaac     }
17535fdea053SToby Isaac   }
17545fdea053SToby Isaac   ierr = PetscFree5(sl->modes,sl->sizes,sl->perms,sl->rots,sl->minMaxOrients);CHKERRQ(ierr);
17555fdea053SToby Isaac   ierr = DMLabelDestroy(&sl->label);CHKERRQ(ierr);
17565fdea053SToby Isaac   sl->numStrata = 0;
17575fdea053SToby Isaac   PetscFunctionReturn(0);
17585fdea053SToby Isaac }
17595fdea053SToby Isaac 
17605fdea053SToby Isaac static PetscErrorCode PetscSectionSymDestroy_Label(PetscSectionSym sym)
17615fdea053SToby Isaac {
17625fdea053SToby Isaac   PetscErrorCode ierr;
17635fdea053SToby Isaac 
17645fdea053SToby Isaac   PetscFunctionBegin;
17655fdea053SToby Isaac   ierr = PetscSectionSymLabelReset(sym);CHKERRQ(ierr);
17665fdea053SToby Isaac   ierr = PetscFree(sym->data);CHKERRQ(ierr);
17675fdea053SToby Isaac   PetscFunctionReturn(0);
17685fdea053SToby Isaac }
17695fdea053SToby Isaac 
17705fdea053SToby Isaac static PetscErrorCode PetscSectionSymView_Label(PetscSectionSym sym, PetscViewer viewer)
17715fdea053SToby Isaac {
17725fdea053SToby Isaac   PetscSectionSym_Label *sl = (PetscSectionSym_Label *) sym->data;
17735fdea053SToby Isaac   PetscBool             isAscii;
17745fdea053SToby Isaac   DMLabel               label = sl->label;
1775d67d17b1SMatthew G. Knepley   const char           *name;
17765fdea053SToby Isaac   PetscErrorCode        ierr;
17775fdea053SToby Isaac 
17785fdea053SToby Isaac   PetscFunctionBegin;
17795fdea053SToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &isAscii);CHKERRQ(ierr);
17805fdea053SToby Isaac   if (isAscii) {
17815fdea053SToby Isaac     PetscInt          i, j, k;
17825fdea053SToby Isaac     PetscViewerFormat format;
17835fdea053SToby Isaac 
17845fdea053SToby Isaac     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
17855fdea053SToby Isaac     if (label) {
17865fdea053SToby Isaac       ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
17875fdea053SToby Isaac       if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
17885fdea053SToby Isaac         ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
17895fdea053SToby Isaac         ierr = DMLabelView(label, viewer);CHKERRQ(ierr);
17905fdea053SToby Isaac         ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
17915fdea053SToby Isaac       } else {
1792d67d17b1SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) sl->label, &name);CHKERRQ(ierr);
1793d67d17b1SMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer,"  Label '%s'\n",name);CHKERRQ(ierr);
17945fdea053SToby Isaac       }
17955fdea053SToby Isaac     } else {
17965fdea053SToby Isaac       ierr = PetscViewerASCIIPrintf(viewer, "No label given\n");CHKERRQ(ierr);
17975fdea053SToby Isaac     }
17985fdea053SToby Isaac     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
17995fdea053SToby Isaac     for (i = 0; i <= sl->numStrata; i++) {
18005fdea053SToby Isaac       PetscInt value = i < sl->numStrata ? label->stratumValues[i] : label->defaultValue;
18015fdea053SToby Isaac 
18025fdea053SToby Isaac       if (!(sl->perms[i] || sl->rots[i])) {
18035fdea053SToby Isaac         ierr = PetscViewerASCIIPrintf(viewer, "Symmetry for stratum value %D (%D dofs per point): no symmetries\n", value, sl->sizes[i]);CHKERRQ(ierr);
18045fdea053SToby Isaac       } else {
18055fdea053SToby Isaac       ierr = PetscViewerASCIIPrintf(viewer, "Symmetry for stratum value %D (%D dofs per point):\n", value, sl->sizes[i]);CHKERRQ(ierr);
18065fdea053SToby Isaac         ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
18075fdea053SToby Isaac         ierr = PetscViewerASCIIPrintf(viewer, "Orientation range: [%D, %D)\n", sl->minMaxOrients[i][0], sl->minMaxOrients[i][1]);CHKERRQ(ierr);
18085fdea053SToby Isaac         if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
18095fdea053SToby Isaac           ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
18105fdea053SToby Isaac           for (j = sl->minMaxOrients[i][0]; j < sl->minMaxOrients[i][1]; j++) {
18115fdea053SToby Isaac             if (!((sl->perms[i] && sl->perms[i][j]) || (sl->rots[i] && sl->rots[i][j]))) {
18125fdea053SToby Isaac               ierr = PetscViewerASCIIPrintf(viewer, "Orientation %D: identity\n",j);CHKERRQ(ierr);
18135fdea053SToby Isaac             } else {
18145fdea053SToby Isaac               PetscInt tab;
18155fdea053SToby Isaac 
18165fdea053SToby Isaac               ierr = PetscViewerASCIIPrintf(viewer, "Orientation %D:\n",j);CHKERRQ(ierr);
18175fdea053SToby Isaac               ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
18185fdea053SToby Isaac               ierr = PetscViewerASCIIGetTab(viewer,&tab);CHKERRQ(ierr);
18195fdea053SToby Isaac               if (sl->perms[i] && sl->perms[i][j]) {
18205fdea053SToby Isaac                 ierr = PetscViewerASCIIPrintf(viewer,"Permutation:");CHKERRQ(ierr);
18215fdea053SToby Isaac                 ierr = PetscViewerASCIISetTab(viewer,0);CHKERRQ(ierr);
18225fdea053SToby Isaac                 for (k = 0; k < sl->sizes[i]; k++) {ierr = PetscViewerASCIIPrintf(viewer," %D",sl->perms[i][j][k]);CHKERRQ(ierr);}
18235fdea053SToby Isaac                 ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
18245fdea053SToby Isaac                 ierr = PetscViewerASCIISetTab(viewer,tab);CHKERRQ(ierr);
18255fdea053SToby Isaac               }
18265fdea053SToby Isaac               if (sl->rots[i] && sl->rots[i][j]) {
18275fdea053SToby Isaac                 ierr = PetscViewerASCIIPrintf(viewer,"Rotations:  ");CHKERRQ(ierr);
18285fdea053SToby Isaac                 ierr = PetscViewerASCIISetTab(viewer,0);CHKERRQ(ierr);
18295fdea053SToby Isaac #if defined(PETSC_USE_COMPLEX)
18305fdea053SToby Isaac                 for (k = 0; k < sl->sizes[i]; k++) {ierr = PetscViewerASCIIPrintf(viewer," %+f+i*%+f",PetscRealPart(sl->rots[i][j][k]),PetscImaginaryPart(sl->rots[i][j][k]));CHKERRQ(ierr);}
18315fdea053SToby Isaac #else
18325fdea053SToby Isaac                 for (k = 0; k < sl->sizes[i]; k++) {ierr = PetscViewerASCIIPrintf(viewer," %+f",sl->rots[i][j][k]);CHKERRQ(ierr);}
18335fdea053SToby Isaac #endif
18345fdea053SToby Isaac                 ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
18355fdea053SToby Isaac                 ierr = PetscViewerASCIISetTab(viewer,tab);CHKERRQ(ierr);
18365fdea053SToby Isaac               }
18375fdea053SToby Isaac               ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
18385fdea053SToby Isaac             }
18395fdea053SToby Isaac           }
18405fdea053SToby Isaac           ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
18415fdea053SToby Isaac         }
18425fdea053SToby Isaac         ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
18435fdea053SToby Isaac       }
18445fdea053SToby Isaac     }
18455fdea053SToby Isaac     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
18465fdea053SToby Isaac   }
18475fdea053SToby Isaac   PetscFunctionReturn(0);
18485fdea053SToby Isaac }
18495fdea053SToby Isaac 
18505fdea053SToby Isaac /*@
18515fdea053SToby Isaac   PetscSectionSymLabelSetLabel - set the label whose strata will define the points that receive symmetries
18525fdea053SToby Isaac 
18535fdea053SToby Isaac   Logically collective on sym
18545fdea053SToby Isaac 
18555fdea053SToby Isaac   Input parameters:
18565fdea053SToby Isaac + sym - the section symmetries
18575fdea053SToby Isaac - label - the DMLabel describing the types of points
18585fdea053SToby Isaac 
18595fdea053SToby Isaac   Level: developer:
18605fdea053SToby Isaac 
18615fdea053SToby Isaac .seealso: PetscSectionSymLabelSetStratum(), PetscSectionSymCreateLabel(), PetscSectionGetPointSyms()
18625fdea053SToby Isaac @*/
18635fdea053SToby Isaac PetscErrorCode PetscSectionSymLabelSetLabel(PetscSectionSym sym, DMLabel label)
18645fdea053SToby Isaac {
18655fdea053SToby Isaac   PetscSectionSym_Label *sl;
18665fdea053SToby Isaac   PetscErrorCode        ierr;
18675fdea053SToby Isaac 
18685fdea053SToby Isaac   PetscFunctionBegin;
18695fdea053SToby Isaac   PetscValidHeaderSpecific(sym,PETSC_SECTION_SYM_CLASSID,1);
18705fdea053SToby Isaac   sl = (PetscSectionSym_Label *) sym->data;
18715fdea053SToby Isaac   if (sl->label && sl->label != label) {ierr = PetscSectionSymLabelReset(sym);CHKERRQ(ierr);}
18725fdea053SToby Isaac   if (label) {
18735fdea053SToby Isaac     sl->label = label;
1874d67d17b1SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
18755fdea053SToby Isaac     ierr = DMLabelGetNumValues(label,&sl->numStrata);CHKERRQ(ierr);
18761a834cf9SToby Isaac     ierr = PetscMalloc5(sl->numStrata+1,&sl->modes,sl->numStrata+1,&sl->sizes,sl->numStrata+1,&sl->perms,sl->numStrata+1,&sl->rots,sl->numStrata+1,&sl->minMaxOrients);CHKERRQ(ierr);
18771a834cf9SToby Isaac     ierr = PetscMemzero((void *) sl->modes,(sl->numStrata+1)*sizeof(PetscCopyMode));CHKERRQ(ierr);
18781a834cf9SToby Isaac     ierr = PetscMemzero((void *) sl->sizes,(sl->numStrata+1)*sizeof(PetscInt));CHKERRQ(ierr);
18791a834cf9SToby Isaac     ierr = PetscMemzero((void *) sl->perms,(sl->numStrata+1)*sizeof(const PetscInt **));CHKERRQ(ierr);
18801a834cf9SToby Isaac     ierr = PetscMemzero((void *) sl->rots,(sl->numStrata+1)*sizeof(const PetscScalar **));CHKERRQ(ierr);
18811a834cf9SToby Isaac     ierr = PetscMemzero((void *) sl->minMaxOrients,(sl->numStrata+1)*sizeof(PetscInt[2]));CHKERRQ(ierr);
18825fdea053SToby Isaac   }
18835fdea053SToby Isaac   PetscFunctionReturn(0);
18845fdea053SToby Isaac }
18855fdea053SToby Isaac 
18865fdea053SToby Isaac /*@C
18875fdea053SToby Isaac   PetscSectionSymLabelSetStratum - set the symmetries for the orientations of a stratum
18885fdea053SToby Isaac 
18895fdea053SToby Isaac   Logically collective on PetscSectionSym
18905fdea053SToby Isaac 
18915fdea053SToby Isaac   InputParameters:
18925fdea053SToby Isaac + sys       - the section symmetries
18935fdea053SToby Isaac . stratum   - the stratum value in the label that we are assigning symmetries for
18945fdea053SToby Isaac . size      - the number of dofs for points in the stratum of the label
18955fdea053SToby Isaac . minOrient - the smallest orientation for a point in this stratum
18965fdea053SToby Isaac . maxOrient - one greater than the largest orientation for a ppoint in this stratum (i.e., orientations are in the range [minOrient, maxOrient))
18975fdea053SToby Isaac . mode      - how sym should copy the perms and rots arrays
18985fdea053SToby Isaac . perms     - NULL if there are no permutations, or (maxOrient - minOrient) permutations, one for each orientation.  A NULL permutation is the identity
1899*a2b725a8SWilliam Gropp - rots      - NULL if there are no rotations, or (maxOrient - minOrient) sets of rotations, one for each orientation.  A NULL set of orientations is the identity
19005fdea053SToby Isaac 
19015fdea053SToby Isaac   Level: developer
19025fdea053SToby Isaac 
19035fdea053SToby Isaac .seealso: PetscSectionSymCreate(), PetscSectionSetSym(), PetscSectionGetPointSyms(), PetscSectionSymCreateLabel()
19045fdea053SToby Isaac @*/
19055fdea053SToby Isaac PetscErrorCode PetscSectionSymLabelSetStratum(PetscSectionSym sym, PetscInt stratum, PetscInt size, PetscInt minOrient, PetscInt maxOrient, PetscCopyMode mode, const PetscInt **perms, const PetscScalar **rots)
19065fdea053SToby Isaac {
19075fdea053SToby Isaac   PetscSectionSym_Label *sl;
1908d67d17b1SMatthew G. Knepley   const char            *name;
1909d67d17b1SMatthew G. Knepley   PetscInt               i, j, k;
19105fdea053SToby Isaac   PetscErrorCode         ierr;
19115fdea053SToby Isaac 
19125fdea053SToby Isaac   PetscFunctionBegin;
19135fdea053SToby Isaac   PetscValidHeaderSpecific(sym,PETSC_SECTION_SYM_CLASSID,1);
19145fdea053SToby Isaac   sl = (PetscSectionSym_Label *) sym->data;
19155fdea053SToby Isaac   if (!sl->label) SETERRQ(PetscObjectComm((PetscObject)sym),PETSC_ERR_ARG_WRONGSTATE,"No label set yet");
19165fdea053SToby Isaac   for (i = 0; i <= sl->numStrata; i++) {
19175fdea053SToby Isaac     PetscInt value = (i < sl->numStrata) ? sl->label->stratumValues[i] : sl->label->defaultValue;
19185fdea053SToby Isaac 
19195fdea053SToby Isaac     if (stratum == value) break;
19205fdea053SToby Isaac   }
1921d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) sl->label, &name);CHKERRQ(ierr);
1922d67d17b1SMatthew G. Knepley   if (i > sl->numStrata) SETERRQ2(PetscObjectComm((PetscObject)sym),PETSC_ERR_ARG_OUTOFRANGE,"Stratum %D not found in label %s\n",stratum,name);
19235fdea053SToby Isaac   sl->sizes[i] = size;
19245fdea053SToby Isaac   sl->modes[i] = mode;
19255fdea053SToby Isaac   sl->minMaxOrients[i][0] = minOrient;
19265fdea053SToby Isaac   sl->minMaxOrients[i][1] = maxOrient;
19275fdea053SToby Isaac   if (mode == PETSC_COPY_VALUES) {
19285fdea053SToby Isaac     if (perms) {
19295fdea053SToby Isaac       PetscInt    **ownPerms;
19305fdea053SToby Isaac 
19315fdea053SToby Isaac       ierr = PetscCalloc1(maxOrient - minOrient,&ownPerms);CHKERRQ(ierr);
19325fdea053SToby Isaac       for (j = 0; j < maxOrient-minOrient; j++) {
19335fdea053SToby Isaac         if (perms[j]) {
19345fdea053SToby Isaac           ierr = PetscMalloc1(size,&ownPerms[j]);CHKERRQ(ierr);
19355fdea053SToby Isaac           for (k = 0; k < size; k++) {ownPerms[j][k] = perms[j][k];}
19365fdea053SToby Isaac         }
19375fdea053SToby Isaac       }
19385fdea053SToby Isaac       sl->perms[i] = (const PetscInt **) &ownPerms[-minOrient];
19395fdea053SToby Isaac     }
19405fdea053SToby Isaac     if (rots) {
19415fdea053SToby Isaac       PetscScalar **ownRots;
19425fdea053SToby Isaac 
19435fdea053SToby Isaac       ierr = PetscCalloc1(maxOrient - minOrient,&ownRots);CHKERRQ(ierr);
19445fdea053SToby Isaac       for (j = 0; j < maxOrient-minOrient; j++) {
19455fdea053SToby Isaac         if (rots[j]) {
19465fdea053SToby Isaac           ierr = PetscMalloc1(size,&ownRots[j]);CHKERRQ(ierr);
19475fdea053SToby Isaac           for (k = 0; k < size; k++) {ownRots[j][k] = rots[j][k];}
19485fdea053SToby Isaac         }
19495fdea053SToby Isaac       }
19505fdea053SToby Isaac       sl->rots[i] = (const PetscScalar **) &ownRots[-minOrient];
19515fdea053SToby Isaac     }
19525fdea053SToby Isaac   } else {
19535fdea053SToby Isaac     sl->perms[i] = perms ? &perms[-minOrient] : NULL;
19545fdea053SToby Isaac     sl->rots[i]  = rots ? &rots[-minOrient] : NULL;
19555fdea053SToby Isaac   }
19565fdea053SToby Isaac   PetscFunctionReturn(0);
19575fdea053SToby Isaac }
19585fdea053SToby Isaac 
19595fdea053SToby Isaac static PetscErrorCode PetscSectionSymGetPoints_Label(PetscSectionSym sym, PetscSection section, PetscInt numPoints, const PetscInt *points, const PetscInt **perms, const PetscScalar **rots)
19605fdea053SToby Isaac {
19615fdea053SToby Isaac   PetscInt              i, j, numStrata;
19625fdea053SToby Isaac   PetscSectionSym_Label *sl;
19635fdea053SToby Isaac   DMLabel               label;
19645fdea053SToby Isaac   PetscErrorCode        ierr;
19655fdea053SToby Isaac 
19665fdea053SToby Isaac   PetscFunctionBegin;
19675fdea053SToby Isaac   sl = (PetscSectionSym_Label *) sym->data;
19685fdea053SToby Isaac   numStrata = sl->numStrata;
19695fdea053SToby Isaac   label     = sl->label;
19705fdea053SToby Isaac   for (i = 0; i < numPoints; i++) {
19715fdea053SToby Isaac     PetscInt point = points[2*i];
19725fdea053SToby Isaac     PetscInt ornt  = points[2*i+1];
19735fdea053SToby Isaac 
19745fdea053SToby Isaac     for (j = 0; j < numStrata; j++) {
19755fdea053SToby Isaac       if (label->validIS[j]) {
19765fdea053SToby Isaac         PetscInt k;
19775fdea053SToby Isaac 
19785fdea053SToby Isaac         ierr = ISLocate(label->points[j],point,&k);CHKERRQ(ierr);
19795fdea053SToby Isaac         if (k >= 0) break;
19805fdea053SToby Isaac       } else {
19815fdea053SToby Isaac         PetscBool has;
19825fdea053SToby Isaac 
1983b9907514SLisandro Dalcin         ierr = PetscHSetIHas(label->ht[j], point, &has);CHKERRQ(ierr);
19845fdea053SToby Isaac         if (has) break;
19855fdea053SToby Isaac       }
19865fdea053SToby Isaac     }
19875fdea053SToby Isaac     if ((sl->minMaxOrients[j][1] > sl->minMaxOrients[j][0]) && (ornt < sl->minMaxOrients[j][0] || ornt >= sl->minMaxOrients[j][1])) SETERRQ5(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"point %D orientation %D not in range [%D, %D) for stratum %D",point,ornt,sl->minMaxOrients[j][0],sl->minMaxOrients[j][1],j < numStrata ? label->stratumValues[j] : label->defaultValue);
19885fdea053SToby Isaac     if (perms) {perms[i] = sl->perms[j] ? sl->perms[j][ornt] : NULL;}
19895fdea053SToby Isaac     if (rots) {rots[i]  = sl->rots[j] ? sl->rots[j][ornt] : NULL;}
19905fdea053SToby Isaac   }
19915fdea053SToby Isaac   PetscFunctionReturn(0);
19925fdea053SToby Isaac }
19935fdea053SToby Isaac 
19945fdea053SToby Isaac PetscErrorCode PetscSectionSymCreate_Label(PetscSectionSym sym)
19955fdea053SToby Isaac {
19965fdea053SToby Isaac   PetscSectionSym_Label *sl;
19975fdea053SToby Isaac   PetscErrorCode        ierr;
19985fdea053SToby Isaac 
19995fdea053SToby Isaac   PetscFunctionBegin;
20005fdea053SToby Isaac   ierr = PetscNewLog(sym,&sl);CHKERRQ(ierr);
20015fdea053SToby Isaac   sym->ops->getpoints = PetscSectionSymGetPoints_Label;
20025fdea053SToby Isaac   sym->ops->view      = PetscSectionSymView_Label;
20035fdea053SToby Isaac   sym->ops->destroy   = PetscSectionSymDestroy_Label;
20045fdea053SToby Isaac   sym->data           = (void *) sl;
20055fdea053SToby Isaac   PetscFunctionReturn(0);
20065fdea053SToby Isaac }
20075fdea053SToby Isaac 
20085fdea053SToby Isaac /*@
20095fdea053SToby Isaac   PetscSectionSymCreateLabel - Create a section symmetry that assigns one symmetry to each stratum of a label
20105fdea053SToby Isaac 
20115fdea053SToby Isaac   Collective
20125fdea053SToby Isaac 
20135fdea053SToby Isaac   Input Parameters:
20145fdea053SToby Isaac + comm - the MPI communicator for the new symmetry
20155fdea053SToby Isaac - label - the label defining the strata
20165fdea053SToby Isaac 
20175fdea053SToby Isaac   Output Parameters:
20185fdea053SToby Isaac . sym - the section symmetries
20195fdea053SToby Isaac 
20205fdea053SToby Isaac   Level: developer
20215fdea053SToby Isaac 
20225fdea053SToby Isaac .seealso: PetscSectionSymCreate(), PetscSectionSetSym(), PetscSectionGetSym(), PetscSectionSymLabelSetStratum(), PetscSectionGetPointSyms()
20235fdea053SToby Isaac @*/
20245fdea053SToby Isaac PetscErrorCode PetscSectionSymCreateLabel(MPI_Comm comm, DMLabel label, PetscSectionSym *sym)
20255fdea053SToby Isaac {
20265fdea053SToby Isaac   PetscErrorCode        ierr;
20275fdea053SToby Isaac 
20285fdea053SToby Isaac   PetscFunctionBegin;
20295fdea053SToby Isaac   ierr = DMInitializePackage();CHKERRQ(ierr);
20305fdea053SToby Isaac   ierr = PetscSectionSymCreate(comm,sym);CHKERRQ(ierr);
20315fdea053SToby Isaac   ierr = PetscSectionSymSetType(*sym,PETSCSECTIONSYMLABEL);CHKERRQ(ierr);
20325fdea053SToby Isaac   ierr = PetscSectionSymLabelSetLabel(*sym,label);CHKERRQ(ierr);
20335fdea053SToby Isaac   PetscFunctionReturn(0);
20345fdea053SToby Isaac }
2035