xref: /petsc/src/dm/label/dmlabel.c (revision 609dae6e2730bae419e2deb5e31e4255cfb16660)
15fdea053SToby Isaac #include <petscdm.h>
2c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h>   /*I      "petscdmlabel.h"   I*/
3ea844a1aSMatthew Knepley #include <petsc/private/sectionimpl.h>   /*I      "petscsection.h"   I*/
4c58f1c22SToby Isaac #include <petscsf.h>
5ea844a1aSMatthew Knepley #include <petscsection.h>
6c58f1c22SToby Isaac 
7c58f1c22SToby Isaac /*@C
8c58f1c22SToby Isaac   DMLabelCreate - Create a DMLabel object, which is a multimap
9c58f1c22SToby Isaac 
105b5e7992SMatthew G. Knepley   Collective
115b5e7992SMatthew G. Knepley 
12d67d17b1SMatthew G. Knepley   Input parameters:
13d67d17b1SMatthew G. Knepley + comm - The communicator, usually PETSC_COMM_SELF
14d67d17b1SMatthew G. Knepley - name - The label name
15c58f1c22SToby Isaac 
16c58f1c22SToby Isaac   Output parameter:
17c58f1c22SToby Isaac . label - The DMLabel
18c58f1c22SToby Isaac 
19c58f1c22SToby Isaac   Level: beginner
20c58f1c22SToby Isaac 
2105ab7a9fSVaclav Hapla   Notes:
2205ab7a9fSVaclav Hapla   The label name is actually usual PetscObject name.
2305ab7a9fSVaclav Hapla   One can get/set it with PetscObjectGetName()/PetscObjectSetName().
2405ab7a9fSVaclav Hapla 
25c58f1c22SToby Isaac .seealso: DMLabelDestroy()
26c58f1c22SToby Isaac @*/
27d67d17b1SMatthew G. Knepley PetscErrorCode DMLabelCreate(MPI_Comm comm, const char name[], DMLabel *label)
28c58f1c22SToby Isaac {
29c58f1c22SToby Isaac   PetscErrorCode ierr;
30c58f1c22SToby Isaac 
31c58f1c22SToby Isaac   PetscFunctionBegin;
32064a246eSJacob Faibussowitsch   PetscValidPointer(label,3);
33d67d17b1SMatthew G. Knepley   ierr = DMInitializePackage();CHKERRQ(ierr);
34c58f1c22SToby Isaac 
35d67d17b1SMatthew G. Knepley   ierr = PetscHeaderCreate(*label,DMLABEL_CLASSID,"DMLabel","DMLabel","DM",comm,DMLabelDestroy,DMLabelView);CHKERRQ(ierr);
36d67d17b1SMatthew G. Knepley 
37c58f1c22SToby Isaac   (*label)->numStrata      = 0;
385aa44df4SToby Isaac   (*label)->defaultValue   = -1;
39c58f1c22SToby Isaac   (*label)->stratumValues  = NULL;
40ad8374ffSToby Isaac   (*label)->validIS        = NULL;
41c58f1c22SToby Isaac   (*label)->stratumSizes   = NULL;
42c58f1c22SToby Isaac   (*label)->points         = NULL;
43c58f1c22SToby Isaac   (*label)->ht             = NULL;
44c58f1c22SToby Isaac   (*label)->pStart         = -1;
45c58f1c22SToby Isaac   (*label)->pEnd           = -1;
46c58f1c22SToby Isaac   (*label)->bt             = NULL;
47b9907514SLisandro Dalcin   ierr = PetscHMapICreate(&(*label)->hmap);CHKERRQ(ierr);
48d67d17b1SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *label, name);CHKERRQ(ierr);
49c58f1c22SToby Isaac   PetscFunctionReturn(0);
50c58f1c22SToby Isaac }
51c58f1c22SToby Isaac 
52c58f1c22SToby Isaac /*
53c58f1c22SToby Isaac   DMLabelMakeValid_Private - Transfer stratum data from the hash format to the sorted list format
54c58f1c22SToby Isaac 
555b5e7992SMatthew G. Knepley   Not collective
565b5e7992SMatthew G. Knepley 
57c58f1c22SToby Isaac   Input parameter:
58c58f1c22SToby Isaac + label - The DMLabel
59c58f1c22SToby Isaac - v - The stratum value
60c58f1c22SToby Isaac 
61c58f1c22SToby Isaac   Output parameter:
62c58f1c22SToby Isaac . label - The DMLabel with stratum in sorted list format
63c58f1c22SToby Isaac 
64c58f1c22SToby Isaac   Level: developer
65c58f1c22SToby Isaac 
66c58f1c22SToby Isaac .seealso: DMLabelCreate()
67c58f1c22SToby Isaac */
68c58f1c22SToby Isaac static PetscErrorCode DMLabelMakeValid_Private(DMLabel label, PetscInt v)
69c58f1c22SToby Isaac {
70277ea44aSLisandro Dalcin   IS             is;
71b9907514SLisandro Dalcin   PetscInt       off = 0, *pointArray, p;
72c58f1c22SToby Isaac   PetscErrorCode ierr;
73c58f1c22SToby Isaac 
74b9907514SLisandro Dalcin   if (PetscLikely(v >= 0 && v < label->numStrata) && label->validIS[v]) return 0;
75c58f1c22SToby Isaac   PetscFunctionBegin;
760c3c4a36SLisandro 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);
77e8f14785SLisandro Dalcin   ierr = PetscHSetIGetSize(label->ht[v], &label->stratumSizes[v]);CHKERRQ(ierr);
78ad8374ffSToby Isaac   ierr = PetscMalloc1(label->stratumSizes[v], &pointArray);CHKERRQ(ierr);
79e8f14785SLisandro Dalcin   ierr = PetscHSetIGetElems(label->ht[v], &off, pointArray);CHKERRQ(ierr);
80b9907514SLisandro Dalcin   ierr = PetscHSetIClear(label->ht[v]);CHKERRQ(ierr);
81ad8374ffSToby Isaac   ierr = PetscSortInt(label->stratumSizes[v], pointArray);CHKERRQ(ierr);
82c58f1c22SToby Isaac   if (label->bt) {
83c58f1c22SToby Isaac     for (p = 0; p < label->stratumSizes[v]; ++p) {
84ad8374ffSToby Isaac       const PetscInt point = pointArray[p];
85c58f1c22SToby 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);
86c58f1c22SToby Isaac       ierr = PetscBTSet(label->bt, point - label->pStart);CHKERRQ(ierr);
87c58f1c22SToby Isaac     }
88c58f1c22SToby Isaac   }
89ba2698f1SMatthew G. Knepley   if (label->stratumSizes[v] > 0 && pointArray[label->stratumSizes[v]-1] == pointArray[0] + label->stratumSizes[v]-1) {
90ba2698f1SMatthew G. Knepley     ierr = ISCreateStride(PETSC_COMM_SELF, label->stratumSizes[v], pointArray[0], 1, &is);CHKERRQ(ierr);
91ba2698f1SMatthew G. Knepley     ierr = PetscFree(pointArray);CHKERRQ(ierr);
92ba2698f1SMatthew G. Knepley   } else {
93277ea44aSLisandro Dalcin     ierr = ISCreateGeneral(PETSC_COMM_SELF, label->stratumSizes[v], pointArray, PETSC_OWN_POINTER, &is);CHKERRQ(ierr);
94ba2698f1SMatthew G. Knepley   }
95277ea44aSLisandro Dalcin   ierr = PetscObjectSetName((PetscObject) is, "indices");CHKERRQ(ierr);
96277ea44aSLisandro Dalcin   label->points[v]  = is;
97ad8374ffSToby Isaac   label->validIS[v] = PETSC_TRUE;
98d67d17b1SMatthew G. Knepley   ierr = PetscObjectStateIncrease((PetscObject) label);CHKERRQ(ierr);
99c58f1c22SToby Isaac   PetscFunctionReturn(0);
100c58f1c22SToby Isaac }
101c58f1c22SToby Isaac 
102c58f1c22SToby Isaac /*
103c58f1c22SToby Isaac   DMLabelMakeAllValid_Private - Transfer all strata from the hash format to the sorted list format
104c58f1c22SToby Isaac 
1055b5e7992SMatthew G. Knepley   Not collective
1065b5e7992SMatthew G. Knepley 
107c58f1c22SToby Isaac   Input parameter:
108c58f1c22SToby Isaac . label - The DMLabel
109c58f1c22SToby Isaac 
110c58f1c22SToby Isaac   Output parameter:
111c58f1c22SToby Isaac . label - The DMLabel with all strata in sorted list format
112c58f1c22SToby Isaac 
113c58f1c22SToby Isaac   Level: developer
114c58f1c22SToby Isaac 
115c58f1c22SToby Isaac .seealso: DMLabelCreate()
116c58f1c22SToby Isaac */
117c58f1c22SToby Isaac static PetscErrorCode DMLabelMakeAllValid_Private(DMLabel label)
118c58f1c22SToby Isaac {
119c58f1c22SToby Isaac   PetscInt       v;
120c58f1c22SToby Isaac   PetscErrorCode ierr;
121c58f1c22SToby Isaac 
122c58f1c22SToby Isaac   PetscFunctionBegin;
123c58f1c22SToby Isaac   for (v = 0; v < label->numStrata; v++) {
124c58f1c22SToby Isaac     ierr = DMLabelMakeValid_Private(label, v);CHKERRQ(ierr);
125c58f1c22SToby Isaac   }
126c58f1c22SToby Isaac   PetscFunctionReturn(0);
127c58f1c22SToby Isaac }
128c58f1c22SToby Isaac 
129c58f1c22SToby Isaac /*
130c58f1c22SToby Isaac   DMLabelMakeInvalid_Private - Transfer stratum data from the sorted list format to the hash format
131c58f1c22SToby Isaac 
1325b5e7992SMatthew G. Knepley   Not collective
1335b5e7992SMatthew G. Knepley 
134c58f1c22SToby Isaac   Input parameter:
135c58f1c22SToby Isaac + label - The DMLabel
136c58f1c22SToby Isaac - v - The stratum value
137c58f1c22SToby Isaac 
138c58f1c22SToby Isaac   Output parameter:
139c58f1c22SToby Isaac . label - The DMLabel with stratum in hash format
140c58f1c22SToby Isaac 
141c58f1c22SToby Isaac   Level: developer
142c58f1c22SToby Isaac 
143c58f1c22SToby Isaac .seealso: DMLabelCreate()
144c58f1c22SToby Isaac */
145c58f1c22SToby Isaac static PetscErrorCode DMLabelMakeInvalid_Private(DMLabel label, PetscInt v)
146c58f1c22SToby Isaac {
147c58f1c22SToby Isaac   PetscInt       p;
148ad8374ffSToby Isaac   const PetscInt *points;
149c58f1c22SToby Isaac   PetscErrorCode ierr;
150c58f1c22SToby Isaac 
151b9907514SLisandro Dalcin   if (PetscLikely(v >= 0 && v < label->numStrata) && !label->validIS[v]) return 0;
152c58f1c22SToby Isaac   PetscFunctionBegin;
1530c3c4a36SLisandro 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);
154ad8374ffSToby Isaac   if (label->points[v]) {
155ad8374ffSToby Isaac     ierr = ISGetIndices(label->points[v], &points);CHKERRQ(ierr);
156e8f14785SLisandro Dalcin     for (p = 0; p < label->stratumSizes[v]; ++p) {
157e8f14785SLisandro Dalcin       ierr = PetscHSetIAdd(label->ht[v], points[p]);CHKERRQ(ierr);
158e8f14785SLisandro Dalcin     }
159ad8374ffSToby Isaac     ierr = ISRestoreIndices(label->points[v],&points);CHKERRQ(ierr);
160ad8374ffSToby Isaac     ierr = ISDestroy(&(label->points[v]));CHKERRQ(ierr);
161ad8374ffSToby Isaac   }
162ad8374ffSToby Isaac   label->validIS[v] = PETSC_FALSE;
163c58f1c22SToby Isaac   PetscFunctionReturn(0);
164c58f1c22SToby Isaac }
165c58f1c22SToby Isaac 
166b9907514SLisandro Dalcin #if !defined(DMLABEL_LOOKUP_THRESHOLD)
167b9907514SLisandro Dalcin #define DMLABEL_LOOKUP_THRESHOLD 16
168b9907514SLisandro Dalcin #endif
169b9907514SLisandro Dalcin 
1700c3c4a36SLisandro Dalcin PETSC_STATIC_INLINE PetscErrorCode DMLabelLookupStratum(DMLabel label, PetscInt value, PetscInt *index)
1710c3c4a36SLisandro Dalcin {
1720c3c4a36SLisandro Dalcin   PetscInt       v;
173b9907514SLisandro Dalcin   PetscErrorCode ierr;
1740e79e033SBarry Smith 
1750c3c4a36SLisandro Dalcin   PetscFunctionBegin;
1760e79e033SBarry Smith   *index = -1;
177b9907514SLisandro Dalcin   if (label->numStrata <= DMLABEL_LOOKUP_THRESHOLD) {
178b9907514SLisandro Dalcin     for (v = 0; v < label->numStrata; ++v)
179b9907514SLisandro Dalcin       if (label->stratumValues[v] == value) {*index = v; break;}
180b9907514SLisandro Dalcin   } else {
181b9907514SLisandro Dalcin     ierr = PetscHMapIGet(label->hmap, value, index);CHKERRQ(ierr);
1820e79e033SBarry Smith   }
18376bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) { /* Check strata hash map consistency */
18490e9b2aeSLisandro Dalcin     PetscInt len, loc = -1;
18590e9b2aeSLisandro Dalcin     ierr = PetscHMapIGetSize(label->hmap, &len);CHKERRQ(ierr);
18690e9b2aeSLisandro Dalcin     if (len != label->numStrata) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Inconsistent strata hash map size");
18790e9b2aeSLisandro Dalcin     if (label->numStrata <= DMLABEL_LOOKUP_THRESHOLD) {
18890e9b2aeSLisandro Dalcin       ierr = PetscHMapIGet(label->hmap, value, &loc);CHKERRQ(ierr);
18990e9b2aeSLisandro Dalcin     } else {
19090e9b2aeSLisandro Dalcin       for (v = 0; v < label->numStrata; ++v)
19190e9b2aeSLisandro Dalcin         if (label->stratumValues[v] == value) {loc = v; break;}
19290e9b2aeSLisandro Dalcin     }
19390e9b2aeSLisandro Dalcin     if (loc != *index) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Inconsistent strata hash map lookup");
19490e9b2aeSLisandro Dalcin   }
1950c3c4a36SLisandro Dalcin   PetscFunctionReturn(0);
1960c3c4a36SLisandro Dalcin }
1970c3c4a36SLisandro Dalcin 
198b9907514SLisandro Dalcin PETSC_STATIC_INLINE PetscErrorCode DMLabelNewStratum(DMLabel label, PetscInt value, PetscInt *index)
199c58f1c22SToby Isaac {
200b9907514SLisandro Dalcin   PetscInt       v;
201b9907514SLisandro Dalcin   PetscInt      *tmpV;
202b9907514SLisandro Dalcin   PetscInt      *tmpS;
203b9907514SLisandro Dalcin   PetscHSetI    *tmpH, ht;
204b9907514SLisandro Dalcin   IS            *tmpP, is;
205c58f1c22SToby Isaac   PetscBool     *tmpB;
206b9907514SLisandro Dalcin   PetscHMapI     hmap = label->hmap;
207c58f1c22SToby Isaac   PetscErrorCode ierr;
208c58f1c22SToby Isaac 
209c58f1c22SToby Isaac   PetscFunctionBegin;
210b9907514SLisandro Dalcin   v    = label->numStrata;
211b9907514SLisandro Dalcin   tmpV = label->stratumValues;
212b9907514SLisandro Dalcin   tmpS = label->stratumSizes;
213b9907514SLisandro Dalcin   tmpH = label->ht;
214b9907514SLisandro Dalcin   tmpP = label->points;
215b9907514SLisandro Dalcin   tmpB = label->validIS;
2168e1f8cf0SLisandro Dalcin   { /* TODO: PetscRealloc() is broken, use malloc+memcpy+free  */
2178e1f8cf0SLisandro Dalcin     PetscInt   *oldV = tmpV;
2188e1f8cf0SLisandro Dalcin     PetscInt   *oldS = tmpS;
2198e1f8cf0SLisandro Dalcin     PetscHSetI *oldH = tmpH;
2208e1f8cf0SLisandro Dalcin     IS         *oldP = tmpP;
2218e1f8cf0SLisandro Dalcin     PetscBool  *oldB = tmpB;
2228e1f8cf0SLisandro Dalcin     ierr = PetscMalloc((v+1)*sizeof(*tmpV), &tmpV);CHKERRQ(ierr);
2238e1f8cf0SLisandro Dalcin     ierr = PetscMalloc((v+1)*sizeof(*tmpS), &tmpS);CHKERRQ(ierr);
2248e1f8cf0SLisandro Dalcin     ierr = PetscMalloc((v+1)*sizeof(*tmpH), &tmpH);CHKERRQ(ierr);
2258e1f8cf0SLisandro Dalcin     ierr = PetscMalloc((v+1)*sizeof(*tmpP), &tmpP);CHKERRQ(ierr);
2268e1f8cf0SLisandro Dalcin     ierr = PetscMalloc((v+1)*sizeof(*tmpB), &tmpB);CHKERRQ(ierr);
227580bdb30SBarry Smith     ierr = PetscArraycpy(tmpV, oldV, v);CHKERRQ(ierr);
228580bdb30SBarry Smith     ierr = PetscArraycpy(tmpS, oldS, v);CHKERRQ(ierr);
229580bdb30SBarry Smith     ierr = PetscArraycpy(tmpH, oldH, v);CHKERRQ(ierr);
230580bdb30SBarry Smith     ierr = PetscArraycpy(tmpP, oldP, v);CHKERRQ(ierr);
231580bdb30SBarry Smith     ierr = PetscArraycpy(tmpB, oldB, v);CHKERRQ(ierr);
2328e1f8cf0SLisandro Dalcin     ierr = PetscFree(oldV);CHKERRQ(ierr);
2338e1f8cf0SLisandro Dalcin     ierr = PetscFree(oldS);CHKERRQ(ierr);
2348e1f8cf0SLisandro Dalcin     ierr = PetscFree(oldH);CHKERRQ(ierr);
2358e1f8cf0SLisandro Dalcin     ierr = PetscFree(oldP);CHKERRQ(ierr);
2368e1f8cf0SLisandro Dalcin     ierr = PetscFree(oldB);CHKERRQ(ierr);
2378e1f8cf0SLisandro Dalcin   }
238b9907514SLisandro Dalcin   label->numStrata     = v+1;
239c58f1c22SToby Isaac   label->stratumValues = tmpV;
240c58f1c22SToby Isaac   label->stratumSizes  = tmpS;
241c58f1c22SToby Isaac   label->ht            = tmpH;
242c58f1c22SToby Isaac   label->points        = tmpP;
243ad8374ffSToby Isaac   label->validIS       = tmpB;
244b9907514SLisandro Dalcin   ierr = PetscHSetICreate(&ht);CHKERRQ(ierr);
245b9907514SLisandro Dalcin   ierr = ISCreateStride(PETSC_COMM_SELF,0,0,1,&is);CHKERRQ(ierr);
246b9907514SLisandro Dalcin   ierr = PetscHMapISet(hmap, value, v);CHKERRQ(ierr);
247b9907514SLisandro Dalcin   tmpV[v] = value;
248b9907514SLisandro Dalcin   tmpS[v] = 0;
249b9907514SLisandro Dalcin   tmpH[v] = ht;
250b9907514SLisandro Dalcin   tmpP[v] = is;
251b9907514SLisandro Dalcin   tmpB[v] = PETSC_TRUE;
252277ea44aSLisandro Dalcin   ierr = PetscObjectStateIncrease((PetscObject) label);CHKERRQ(ierr);
2530c3c4a36SLisandro Dalcin   *index = v;
2540c3c4a36SLisandro Dalcin   PetscFunctionReturn(0);
2550c3c4a36SLisandro Dalcin }
2560c3c4a36SLisandro Dalcin 
257b9907514SLisandro Dalcin PETSC_STATIC_INLINE PetscErrorCode DMLabelLookupAddStratum(DMLabel label, PetscInt value, PetscInt *index)
258b9907514SLisandro Dalcin {
259b9907514SLisandro Dalcin   PetscErrorCode ierr;
260b9907514SLisandro Dalcin   PetscFunctionBegin;
261b9907514SLisandro Dalcin   ierr = DMLabelLookupStratum(label, value, index);CHKERRQ(ierr);
262b9907514SLisandro Dalcin   if (*index < 0) {ierr = DMLabelNewStratum(label, value, index);CHKERRQ(ierr);}
263b9907514SLisandro Dalcin   PetscFunctionReturn(0);
264b9907514SLisandro Dalcin }
265b9907514SLisandro Dalcin 
2669e63cc69SVaclav Hapla PETSC_STATIC_INLINE PetscErrorCode DMLabelGetStratumSize_Private(DMLabel label, PetscInt v, PetscInt *size)
2679e63cc69SVaclav Hapla {
2689e63cc69SVaclav Hapla   PetscErrorCode ierr;
2699e63cc69SVaclav Hapla 
2709e63cc69SVaclav Hapla   PetscFunctionBegin;
2719e63cc69SVaclav Hapla   *size = 0;
2729e63cc69SVaclav Hapla   if (v < 0) PetscFunctionReturn(0);
2739e63cc69SVaclav Hapla   if (label->validIS[v]) {
2749e63cc69SVaclav Hapla     *size = label->stratumSizes[v];
2759e63cc69SVaclav Hapla   } else {
2769e63cc69SVaclav Hapla     ierr = PetscHSetIGetSize(label->ht[v], size);CHKERRQ(ierr);
2779e63cc69SVaclav Hapla   }
2789e63cc69SVaclav Hapla   PetscFunctionReturn(0);
2799e63cc69SVaclav Hapla }
2809e63cc69SVaclav Hapla 
281b9907514SLisandro Dalcin /*@
282b9907514SLisandro Dalcin   DMLabelAddStratum - Adds a new stratum value in a DMLabel
283b9907514SLisandro Dalcin 
284d8d19677SJose E. Roman   Input Parameters:
285b9907514SLisandro Dalcin + label - The DMLabel
286b9907514SLisandro Dalcin - value - The stratum value
287b9907514SLisandro Dalcin 
288b9907514SLisandro Dalcin   Level: beginner
289b9907514SLisandro Dalcin 
290b9907514SLisandro Dalcin .seealso:  DMLabelCreate(), DMLabelDestroy()
291b9907514SLisandro Dalcin @*/
2920c3c4a36SLisandro Dalcin PetscErrorCode DMLabelAddStratum(DMLabel label, PetscInt value)
2930c3c4a36SLisandro Dalcin {
2940c3c4a36SLisandro Dalcin   PetscInt       v;
2950c3c4a36SLisandro Dalcin   PetscErrorCode ierr;
2960c3c4a36SLisandro Dalcin 
2970c3c4a36SLisandro Dalcin   PetscFunctionBegin;
298d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
299b9907514SLisandro Dalcin   ierr = DMLabelLookupAddStratum(label, value, &v);CHKERRQ(ierr);
300b9907514SLisandro Dalcin   PetscFunctionReturn(0);
301b9907514SLisandro Dalcin }
302b9907514SLisandro Dalcin 
303b9907514SLisandro Dalcin /*@
304b9907514SLisandro Dalcin   DMLabelAddStrata - Adds new stratum values in a DMLabel
305b9907514SLisandro Dalcin 
3065b5e7992SMatthew G. Knepley   Not collective
3075b5e7992SMatthew G. Knepley 
308d8d19677SJose E. Roman   Input Parameters:
309b9907514SLisandro Dalcin + label - The DMLabel
310b9907514SLisandro Dalcin . numStrata - The number of stratum values
311b9907514SLisandro Dalcin - stratumValues - The stratum values
312b9907514SLisandro Dalcin 
313b9907514SLisandro Dalcin   Level: beginner
314b9907514SLisandro Dalcin 
315b9907514SLisandro Dalcin .seealso:  DMLabelCreate(), DMLabelDestroy()
316b9907514SLisandro Dalcin @*/
317b9907514SLisandro Dalcin PetscErrorCode DMLabelAddStrata(DMLabel label, PetscInt numStrata, const PetscInt stratumValues[])
318b9907514SLisandro Dalcin {
319b9907514SLisandro Dalcin   PetscInt       *values, v;
320b9907514SLisandro Dalcin   PetscErrorCode ierr;
321b9907514SLisandro Dalcin 
322b9907514SLisandro Dalcin   PetscFunctionBegin;
323b9907514SLisandro Dalcin   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
324b9907514SLisandro Dalcin   if (numStrata) PetscValidIntPointer(stratumValues, 3);
325b9907514SLisandro Dalcin   ierr = PetscMalloc1(numStrata, &values);CHKERRQ(ierr);
326580bdb30SBarry Smith   ierr = PetscArraycpy(values, stratumValues, numStrata);CHKERRQ(ierr);
327b9907514SLisandro Dalcin   ierr = PetscSortRemoveDupsInt(&numStrata, values);CHKERRQ(ierr);
328b9907514SLisandro Dalcin   if (!label->numStrata) { /* Fast preallocation */
329b9907514SLisandro Dalcin     PetscInt   *tmpV;
330b9907514SLisandro Dalcin     PetscInt   *tmpS;
331b9907514SLisandro Dalcin     PetscHSetI *tmpH, ht;
332b9907514SLisandro Dalcin     IS         *tmpP, is;
333b9907514SLisandro Dalcin     PetscBool  *tmpB;
334b9907514SLisandro Dalcin     PetscHMapI  hmap = label->hmap;
335b9907514SLisandro Dalcin 
336b9907514SLisandro Dalcin     ierr = PetscMalloc1(numStrata, &tmpV);CHKERRQ(ierr);
337b9907514SLisandro Dalcin     ierr = PetscMalloc1(numStrata, &tmpS);CHKERRQ(ierr);
338b9907514SLisandro Dalcin     ierr = PetscMalloc1(numStrata, &tmpH);CHKERRQ(ierr);
339b9907514SLisandro Dalcin     ierr = PetscMalloc1(numStrata, &tmpP);CHKERRQ(ierr);
340b9907514SLisandro Dalcin     ierr = PetscMalloc1(numStrata, &tmpB);CHKERRQ(ierr);
341b9907514SLisandro Dalcin     label->numStrata     = numStrata;
342b9907514SLisandro Dalcin     label->stratumValues = tmpV;
343b9907514SLisandro Dalcin     label->stratumSizes  = tmpS;
344b9907514SLisandro Dalcin     label->ht            = tmpH;
345b9907514SLisandro Dalcin     label->points        = tmpP;
346b9907514SLisandro Dalcin     label->validIS       = tmpB;
347b9907514SLisandro Dalcin     for (v = 0; v < numStrata; ++v) {
348b9907514SLisandro Dalcin       ierr = PetscHSetICreate(&ht);CHKERRQ(ierr);
349b9907514SLisandro Dalcin       ierr = ISCreateStride(PETSC_COMM_SELF,0,0,1,&is);CHKERRQ(ierr);
350b9907514SLisandro Dalcin       ierr = PetscHMapISet(hmap, values[v], v);CHKERRQ(ierr);
351b9907514SLisandro Dalcin       tmpV[v] = values[v];
352b9907514SLisandro Dalcin       tmpS[v] = 0;
353b9907514SLisandro Dalcin       tmpH[v] = ht;
354b9907514SLisandro Dalcin       tmpP[v] = is;
355b9907514SLisandro Dalcin       tmpB[v] = PETSC_TRUE;
356b9907514SLisandro Dalcin     }
357277ea44aSLisandro Dalcin     ierr = PetscObjectStateIncrease((PetscObject) label);CHKERRQ(ierr);
358b9907514SLisandro Dalcin   } else {
359b9907514SLisandro Dalcin     for (v = 0; v < numStrata; ++v) {
360b9907514SLisandro Dalcin       ierr = DMLabelAddStratum(label, values[v]);CHKERRQ(ierr);
361b9907514SLisandro Dalcin     }
362b9907514SLisandro Dalcin   }
363b9907514SLisandro Dalcin   ierr = PetscFree(values);CHKERRQ(ierr);
364b9907514SLisandro Dalcin   PetscFunctionReturn(0);
365b9907514SLisandro Dalcin }
366b9907514SLisandro Dalcin 
367b9907514SLisandro Dalcin /*@
368b9907514SLisandro Dalcin   DMLabelAddStrataIS - Adds new stratum values in a DMLabel
369b9907514SLisandro Dalcin 
3705b5e7992SMatthew G. Knepley   Not collective
3715b5e7992SMatthew G. Knepley 
372d8d19677SJose E. Roman   Input Parameters:
373b9907514SLisandro Dalcin + label - The DMLabel
374b9907514SLisandro Dalcin - valueIS - Index set with stratum values
375b9907514SLisandro Dalcin 
376b9907514SLisandro Dalcin   Level: beginner
377b9907514SLisandro Dalcin 
378b9907514SLisandro Dalcin .seealso:  DMLabelCreate(), DMLabelDestroy()
379b9907514SLisandro Dalcin @*/
380b9907514SLisandro Dalcin PetscErrorCode DMLabelAddStrataIS(DMLabel label, IS valueIS)
381b9907514SLisandro Dalcin {
382b9907514SLisandro Dalcin   PetscInt       numStrata;
383b9907514SLisandro Dalcin   const PetscInt *stratumValues;
384b9907514SLisandro Dalcin   PetscErrorCode ierr;
385b9907514SLisandro Dalcin 
386b9907514SLisandro Dalcin   PetscFunctionBegin;
387b9907514SLisandro Dalcin   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
388b9907514SLisandro Dalcin   PetscValidHeaderSpecific(valueIS, IS_CLASSID, 2);
389b9907514SLisandro Dalcin   ierr = ISGetLocalSize(valueIS, &numStrata);CHKERRQ(ierr);
390b9907514SLisandro Dalcin   ierr = ISGetIndices(valueIS, &stratumValues);CHKERRQ(ierr);
391b9907514SLisandro Dalcin   ierr = DMLabelAddStrata(label, numStrata, stratumValues);CHKERRQ(ierr);
392c58f1c22SToby Isaac   PetscFunctionReturn(0);
393c58f1c22SToby Isaac }
394c58f1c22SToby Isaac 
395c58f1c22SToby Isaac static PetscErrorCode DMLabelView_Ascii(DMLabel label, PetscViewer viewer)
396c58f1c22SToby Isaac {
397c58f1c22SToby Isaac   PetscInt       v;
398c58f1c22SToby Isaac   PetscMPIInt    rank;
399c58f1c22SToby Isaac   PetscErrorCode ierr;
400c58f1c22SToby Isaac 
401c58f1c22SToby Isaac   PetscFunctionBegin;
402ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank);CHKERRMPI(ierr);
403c58f1c22SToby Isaac   ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
404c58f1c22SToby Isaac   if (label) {
405d67d17b1SMatthew G. Knepley     const char *name;
406d67d17b1SMatthew G. Knepley 
407d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) label, &name);CHKERRQ(ierr);
408d67d17b1SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Label '%s':\n", name);CHKERRQ(ierr);
409c58f1c22SToby Isaac     if (label->bt) {ierr = PetscViewerASCIIPrintf(viewer, "  Index has been calculated in [%D, %D)\n", label->pStart, label->pEnd);CHKERRQ(ierr);}
410c58f1c22SToby Isaac     for (v = 0; v < label->numStrata; ++v) {
411c58f1c22SToby Isaac       const PetscInt value = label->stratumValues[v];
412ad8374ffSToby Isaac       const PetscInt *points;
413c58f1c22SToby Isaac       PetscInt       p;
414c58f1c22SToby Isaac 
415ad8374ffSToby Isaac       ierr = ISGetIndices(label->points[v], &points);CHKERRQ(ierr);
416c58f1c22SToby Isaac       for (p = 0; p < label->stratumSizes[v]; ++p) {
417ad8374ffSToby Isaac         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D (%D)\n", rank, points[p], value);CHKERRQ(ierr);
418c58f1c22SToby Isaac       }
419ad8374ffSToby Isaac       ierr = ISRestoreIndices(label->points[v],&points);CHKERRQ(ierr);
420c58f1c22SToby Isaac     }
421c58f1c22SToby Isaac   }
422c58f1c22SToby Isaac   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
423c58f1c22SToby Isaac   ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
424c58f1c22SToby Isaac   PetscFunctionReturn(0);
425c58f1c22SToby Isaac }
426c58f1c22SToby Isaac 
427c58f1c22SToby Isaac /*@C
428c58f1c22SToby Isaac   DMLabelView - View the label
429c58f1c22SToby Isaac 
4305b5e7992SMatthew G. Knepley   Collective on viewer
4315b5e7992SMatthew G. Knepley 
432c58f1c22SToby Isaac   Input Parameters:
433c58f1c22SToby Isaac + label - The DMLabel
434c58f1c22SToby Isaac - viewer - The PetscViewer
435c58f1c22SToby Isaac 
436c58f1c22SToby Isaac   Level: intermediate
437c58f1c22SToby Isaac 
438c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMLabelDestroy()
439c58f1c22SToby Isaac @*/
440c58f1c22SToby Isaac PetscErrorCode DMLabelView(DMLabel label, PetscViewer viewer)
441c58f1c22SToby Isaac {
442c58f1c22SToby Isaac   PetscBool      iascii;
443c58f1c22SToby Isaac   PetscErrorCode ierr;
444c58f1c22SToby Isaac 
445c58f1c22SToby Isaac   PetscFunctionBegin;
446d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
447b9907514SLisandro Dalcin   if (!viewer) {ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)label), &viewer);CHKERRQ(ierr);}
448c58f1c22SToby Isaac   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
449c58f1c22SToby Isaac   if (label) {ierr = DMLabelMakeAllValid_Private(label);CHKERRQ(ierr);}
450c58f1c22SToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
451c58f1c22SToby Isaac   if (iascii) {
452c58f1c22SToby Isaac     ierr = DMLabelView_Ascii(label, viewer);CHKERRQ(ierr);
453c58f1c22SToby Isaac   }
454c58f1c22SToby Isaac   PetscFunctionReturn(0);
455c58f1c22SToby Isaac }
456c58f1c22SToby Isaac 
45784f0b6dfSMatthew G. Knepley /*@
458d67d17b1SMatthew G. Knepley   DMLabelReset - Destroys internal data structures in a DMLabel
459d67d17b1SMatthew G. Knepley 
4605b5e7992SMatthew G. Knepley   Not collective
4615b5e7992SMatthew G. Knepley 
462d67d17b1SMatthew G. Knepley   Input Parameter:
463d67d17b1SMatthew G. Knepley . label - The DMLabel
464d67d17b1SMatthew G. Knepley 
465d67d17b1SMatthew G. Knepley   Level: beginner
466d67d17b1SMatthew G. Knepley 
467d67d17b1SMatthew G. Knepley .seealso: DMLabelDestroy(), DMLabelCreate()
468d67d17b1SMatthew G. Knepley @*/
469d67d17b1SMatthew G. Knepley PetscErrorCode DMLabelReset(DMLabel label)
470d67d17b1SMatthew G. Knepley {
471d67d17b1SMatthew G. Knepley   PetscInt       v;
472d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
473d67d17b1SMatthew G. Knepley 
474d67d17b1SMatthew G. Knepley   PetscFunctionBegin;
475d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
476d67d17b1SMatthew G. Knepley   for (v = 0; v < label->numStrata; ++v) {
477d67d17b1SMatthew G. Knepley     ierr = PetscHSetIDestroy(&label->ht[v]);CHKERRQ(ierr);
478d67d17b1SMatthew G. Knepley     ierr = ISDestroy(&label->points[v]);CHKERRQ(ierr);
479d67d17b1SMatthew G. Knepley   }
480b9907514SLisandro Dalcin   label->numStrata = 0;
481b9907514SLisandro Dalcin   ierr = PetscFree(label->stratumValues);CHKERRQ(ierr);
482b9907514SLisandro Dalcin   ierr = PetscFree(label->stratumSizes);CHKERRQ(ierr);
483d67d17b1SMatthew G. Knepley   ierr = PetscFree(label->ht);CHKERRQ(ierr);
484d67d17b1SMatthew G. Knepley   ierr = PetscFree(label->points);CHKERRQ(ierr);
485d67d17b1SMatthew G. Knepley   ierr = PetscFree(label->validIS);CHKERRQ(ierr);
486f9244615SMatthew G. Knepley   label->stratumValues = NULL;
487f9244615SMatthew G. Knepley   label->stratumSizes  = NULL;
488f9244615SMatthew G. Knepley   label->ht            = NULL;
489f9244615SMatthew G. Knepley   label->points        = NULL;
490f9244615SMatthew G. Knepley   label->validIS       = NULL;
491b9907514SLisandro Dalcin   ierr = PetscHMapIReset(label->hmap);CHKERRQ(ierr);
492b9907514SLisandro Dalcin   label->pStart = -1;
493b9907514SLisandro Dalcin   label->pEnd   = -1;
494d67d17b1SMatthew G. Knepley   ierr = PetscBTDestroy(&label->bt);CHKERRQ(ierr);
495d67d17b1SMatthew G. Knepley   PetscFunctionReturn(0);
496d67d17b1SMatthew G. Knepley }
497d67d17b1SMatthew G. Knepley 
498d67d17b1SMatthew G. Knepley /*@
49984f0b6dfSMatthew G. Knepley   DMLabelDestroy - Destroys a DMLabel
50084f0b6dfSMatthew G. Knepley 
5015b5e7992SMatthew G. Knepley   Collective on label
5025b5e7992SMatthew G. Knepley 
50384f0b6dfSMatthew G. Knepley   Input Parameter:
50484f0b6dfSMatthew G. Knepley . label - The DMLabel
50584f0b6dfSMatthew G. Knepley 
50684f0b6dfSMatthew G. Knepley   Level: beginner
50784f0b6dfSMatthew G. Knepley 
508d67d17b1SMatthew G. Knepley .seealso: DMLabelReset(), DMLabelCreate()
50984f0b6dfSMatthew G. Knepley @*/
510c58f1c22SToby Isaac PetscErrorCode DMLabelDestroy(DMLabel *label)
511c58f1c22SToby Isaac {
512c58f1c22SToby Isaac   PetscErrorCode ierr;
513c58f1c22SToby Isaac 
514c58f1c22SToby Isaac   PetscFunctionBegin;
515d67d17b1SMatthew G. Knepley   if (!*label) PetscFunctionReturn(0);
516d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific((*label),DMLABEL_CLASSID,1);
517b9907514SLisandro Dalcin   if (--((PetscObject)(*label))->refct > 0) {*label = NULL; PetscFunctionReturn(0);}
518d67d17b1SMatthew G. Knepley   ierr = DMLabelReset(*label);CHKERRQ(ierr);
519b9907514SLisandro Dalcin   ierr = PetscHMapIDestroy(&(*label)->hmap);CHKERRQ(ierr);
520d67d17b1SMatthew G. Knepley   ierr = PetscHeaderDestroy(label);CHKERRQ(ierr);
521c58f1c22SToby Isaac   PetscFunctionReturn(0);
522c58f1c22SToby Isaac }
523c58f1c22SToby Isaac 
52484f0b6dfSMatthew G. Knepley /*@
52584f0b6dfSMatthew G. Knepley   DMLabelDuplicate - Duplicates a DMLabel
52684f0b6dfSMatthew G. Knepley 
5275b5e7992SMatthew G. Knepley   Collective on label
5285b5e7992SMatthew G. Knepley 
52984f0b6dfSMatthew G. Knepley   Input Parameter:
53084f0b6dfSMatthew G. Knepley . label - The DMLabel
53184f0b6dfSMatthew G. Knepley 
53284f0b6dfSMatthew G. Knepley   Output Parameter:
53384f0b6dfSMatthew G. Knepley . labelnew - location to put new vector
53484f0b6dfSMatthew G. Knepley 
53584f0b6dfSMatthew G. Knepley   Level: intermediate
53684f0b6dfSMatthew G. Knepley 
53784f0b6dfSMatthew G. Knepley .seealso: DMLabelCreate(), DMLabelDestroy()
53884f0b6dfSMatthew G. Knepley @*/
539c58f1c22SToby Isaac PetscErrorCode DMLabelDuplicate(DMLabel label, DMLabel *labelnew)
540c58f1c22SToby Isaac {
541d67d17b1SMatthew G. Knepley   const char    *name;
542ad8374ffSToby Isaac   PetscInt       v;
543c58f1c22SToby Isaac   PetscErrorCode ierr;
544c58f1c22SToby Isaac 
545c58f1c22SToby Isaac   PetscFunctionBegin;
546d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
547c58f1c22SToby Isaac   ierr = DMLabelMakeAllValid_Private(label);CHKERRQ(ierr);
548d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &name);CHKERRQ(ierr);
549d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PetscObjectComm((PetscObject) label), name, labelnew);CHKERRQ(ierr);
550c58f1c22SToby Isaac 
551c58f1c22SToby Isaac   (*labelnew)->numStrata    = label->numStrata;
5525aa44df4SToby Isaac   (*labelnew)->defaultValue = label->defaultValue;
553c58f1c22SToby Isaac   ierr = PetscMalloc1(label->numStrata, &(*labelnew)->stratumValues);CHKERRQ(ierr);
554c58f1c22SToby Isaac   ierr = PetscMalloc1(label->numStrata, &(*labelnew)->stratumSizes);CHKERRQ(ierr);
555c58f1c22SToby Isaac   ierr = PetscMalloc1(label->numStrata, &(*labelnew)->ht);CHKERRQ(ierr);
556c58f1c22SToby Isaac   ierr = PetscMalloc1(label->numStrata, &(*labelnew)->points);CHKERRQ(ierr);
557ad8374ffSToby Isaac   ierr = PetscMalloc1(label->numStrata, &(*labelnew)->validIS);CHKERRQ(ierr);
558c58f1c22SToby Isaac   for (v = 0; v < label->numStrata; ++v) {
559e8f14785SLisandro Dalcin     ierr = PetscHSetICreate(&(*labelnew)->ht[v]);CHKERRQ(ierr);
560c58f1c22SToby Isaac     (*labelnew)->stratumValues[v]  = label->stratumValues[v];
561c58f1c22SToby Isaac     (*labelnew)->stratumSizes[v]   = label->stratumSizes[v];
562ad8374ffSToby Isaac     ierr = PetscObjectReference((PetscObject) (label->points[v]));CHKERRQ(ierr);
563ad8374ffSToby Isaac     (*labelnew)->points[v]         = label->points[v];
564b9907514SLisandro Dalcin     (*labelnew)->validIS[v]        = PETSC_TRUE;
565c58f1c22SToby Isaac   }
566f14fe40dSLisandro Dalcin   ierr = PetscHMapIDestroy(&(*labelnew)->hmap);CHKERRQ(ierr);
567b9907514SLisandro Dalcin   ierr = PetscHMapIDuplicate(label->hmap,&(*labelnew)->hmap);CHKERRQ(ierr);
568c58f1c22SToby Isaac   (*labelnew)->pStart = -1;
569c58f1c22SToby Isaac   (*labelnew)->pEnd   = -1;
570c58f1c22SToby Isaac   (*labelnew)->bt     = NULL;
571c58f1c22SToby Isaac   PetscFunctionReturn(0);
572c58f1c22SToby Isaac }
573c58f1c22SToby Isaac 
574*609dae6eSVaclav Hapla /*@C
575*609dae6eSVaclav Hapla   DMLabelCompare - Compare two DMLabel objects
576*609dae6eSVaclav Hapla 
577*609dae6eSVaclav Hapla   Collective on dmA
578*609dae6eSVaclav Hapla 
579*609dae6eSVaclav Hapla   Input Parameters:
580*609dae6eSVaclav Hapla + l0 - First DMLabel
581*609dae6eSVaclav Hapla - l1 - Second DMLabel
582*609dae6eSVaclav Hapla 
583*609dae6eSVaclav Hapla   Output Parameters
584*609dae6eSVaclav Hapla + same    - (Optional) Flag whether the two labels are equal
585*609dae6eSVaclav Hapla - message - (Optional) Message describing the difference, or NULL if there is no difference
586*609dae6eSVaclav Hapla 
587*609dae6eSVaclav Hapla   Level: intermediate
588*609dae6eSVaclav Hapla 
589*609dae6eSVaclav Hapla   Notes:
590*609dae6eSVaclav Hapla   If both same and message is passed as NULL, and difference is found, an error is thrown with a message describing the difference.
591*609dae6eSVaclav Hapla 
592*609dae6eSVaclav Hapla   Message must be freed by user.
593*609dae6eSVaclav Hapla 
594*609dae6eSVaclav Hapla   For the comparison, we ignore the order of stratum values, and strata with no points.
595*609dae6eSVaclav Hapla 
596*609dae6eSVaclav Hapla   Fortran Notes:
597*609dae6eSVaclav Hapla   This function is currently not available from Fortran.
598*609dae6eSVaclav Hapla 
599*609dae6eSVaclav Hapla .seealso: DMCompareLabels(), DMLabelGetNumValues(), DMLabelGetDefaultValue(), DMLabelGetNonEmptyStratumValuesIS(), DMLabelGetStratumIS()
600*609dae6eSVaclav Hapla @*/
601*609dae6eSVaclav Hapla PetscErrorCode DMLabelCompare(DMLabel l0, DMLabel l1, PetscBool *same, char **message)
602*609dae6eSVaclav Hapla {
603*609dae6eSVaclav Hapla   const char     *name0, *name1;
604*609dae6eSVaclav Hapla   char            msg[PETSC_MAX_PATH_LEN] = "";
605*609dae6eSVaclav Hapla   PetscErrorCode  ierr;
606*609dae6eSVaclav Hapla 
607*609dae6eSVaclav Hapla   PetscFunctionBegin;
608*609dae6eSVaclav Hapla   PetscValidHeaderSpecific(l0, DMLABEL_CLASSID, 1);
609*609dae6eSVaclav Hapla   PetscValidHeaderSpecific(l1, DMLABEL_CLASSID, 2);
610*609dae6eSVaclav Hapla   if (same) PetscValidBoolPointer(same, 3);
611*609dae6eSVaclav Hapla   if (message) PetscValidPointer(message, 4);
612*609dae6eSVaclav Hapla   ierr = PetscObjectGetName((PetscObject)l0, &name0);CHKERRQ(ierr);
613*609dae6eSVaclav Hapla   ierr = PetscObjectGetName((PetscObject)l1, &name1);CHKERRQ(ierr);
614*609dae6eSVaclav Hapla   {
615*609dae6eSVaclav Hapla     PetscMPIInt   result;
616*609dae6eSVaclav Hapla 
617*609dae6eSVaclav Hapla     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)l0), PetscObjectComm((PetscObject)l1), &result);CHKERRMPI(ierr);
618*609dae6eSVaclav Hapla     if (result != MPI_CONGRUENT && result != MPI_IDENT) {
619*609dae6eSVaclav Hapla       ierr = PetscSNPrintf(msg, sizeof(msg), "DMLabel l0 \"%s\" has different communicator than DMLabel l1 \"%s\"", name0, name1);CHKERRQ(ierr);
620*609dae6eSVaclav Hapla       goto finish;
621*609dae6eSVaclav Hapla     }
622*609dae6eSVaclav Hapla   }
623*609dae6eSVaclav Hapla   {
624*609dae6eSVaclav Hapla     PetscInt v0, v1;
625*609dae6eSVaclav Hapla 
626*609dae6eSVaclav Hapla     ierr = DMLabelGetDefaultValue(l0, &v0);CHKERRQ(ierr);
627*609dae6eSVaclav Hapla     ierr = DMLabelGetDefaultValue(l1, &v1);CHKERRQ(ierr);
628*609dae6eSVaclav Hapla     if (v0 != v1) {
629*609dae6eSVaclav Hapla       ierr = PetscSNPrintf(msg, sizeof(msg), "Default value of DMLabel l0 \"%s\" = %D != %D = Default value of DMLabel l1 \"%s\"", name0, v0, v1, name1);CHKERRQ(ierr);
630*609dae6eSVaclav Hapla       goto finish;
631*609dae6eSVaclav Hapla     }
632*609dae6eSVaclav Hapla   }
633*609dae6eSVaclav Hapla   {
634*609dae6eSVaclav Hapla     IS              is0, is1;
635*609dae6eSVaclav Hapla     PetscBool       flg;
636*609dae6eSVaclav Hapla 
637*609dae6eSVaclav Hapla     ierr = DMLabelGetNonEmptyStratumValuesIS(l0, &is0);CHKERRQ(ierr);
638*609dae6eSVaclav Hapla     ierr = DMLabelGetNonEmptyStratumValuesIS(l1, &is1);CHKERRQ(ierr);
639*609dae6eSVaclav Hapla     ierr = ISEqual(is0, is1, &flg);CHKERRQ(ierr);
640*609dae6eSVaclav Hapla     ierr = ISDestroy(&is0);CHKERRQ(ierr);
641*609dae6eSVaclav Hapla     ierr = ISDestroy(&is1);CHKERRQ(ierr);
642*609dae6eSVaclav Hapla     if (!flg) {
643*609dae6eSVaclav Hapla       ierr = PetscSNPrintf(msg, sizeof(msg), "Stratum values in DMLabel l0 \"%s\" are different than in DMLabel l1 \"%s\"", name0, name1);CHKERRQ(ierr);
644*609dae6eSVaclav Hapla       goto finish;
645*609dae6eSVaclav Hapla     }
646*609dae6eSVaclav Hapla   }
647*609dae6eSVaclav Hapla   {
648*609dae6eSVaclav Hapla     PetscInt i, nValues;
649*609dae6eSVaclav Hapla 
650*609dae6eSVaclav Hapla     ierr = DMLabelGetNumValues(l0, &nValues);CHKERRQ(ierr);
651*609dae6eSVaclav Hapla     for (i=0; i<nValues; i++) {
652*609dae6eSVaclav Hapla       const PetscInt  v = l0->stratumValues[i];
653*609dae6eSVaclav Hapla       PetscInt        n;
654*609dae6eSVaclav Hapla       IS              is0, is1;
655*609dae6eSVaclav Hapla       PetscBool       flg;
656*609dae6eSVaclav Hapla 
657*609dae6eSVaclav Hapla       ierr = DMLabelGetStratumSize_Private(l0, i, &n);CHKERRQ(ierr);
658*609dae6eSVaclav Hapla       if (!n) continue;
659*609dae6eSVaclav Hapla       ierr = DMLabelGetStratumIS(l0, v, &is0);CHKERRQ(ierr);
660*609dae6eSVaclav Hapla       ierr = DMLabelGetStratumIS(l1, v, &is1);CHKERRQ(ierr);
661*609dae6eSVaclav Hapla       ierr = ISEqualUnsorted(is0, is1, &flg);CHKERRQ(ierr);
662*609dae6eSVaclav Hapla       ierr = ISDestroy(&is0);CHKERRQ(ierr);
663*609dae6eSVaclav Hapla       ierr = ISDestroy(&is1);CHKERRQ(ierr);
664*609dae6eSVaclav Hapla       if (!flg) {
665*609dae6eSVaclav Hapla         ierr = PetscSNPrintf(msg, sizeof(msg), "Stratum #%D with value %D contains different points in DMLabel l0 \"%s\" and DMLabel l1 \"%s\"", i, v, name0, name1);CHKERRQ(ierr);
666*609dae6eSVaclav Hapla         goto finish;
667*609dae6eSVaclav Hapla       }
668*609dae6eSVaclav Hapla     }
669*609dae6eSVaclav Hapla   }
670*609dae6eSVaclav Hapla   if (same) *same = PETSC_TRUE;
671*609dae6eSVaclav Hapla finish:
672*609dae6eSVaclav Hapla   if (msg[0] && !same && !message) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, msg);
673*609dae6eSVaclav Hapla   if (same) *same = (PetscBool) !msg[0];
674*609dae6eSVaclav Hapla   if (message) {
675*609dae6eSVaclav Hapla     *message = NULL;
676*609dae6eSVaclav Hapla     if (msg[0]) {
677*609dae6eSVaclav Hapla       ierr = PetscStrallocpy(msg, message);CHKERRQ(ierr);
678*609dae6eSVaclav Hapla     }
679*609dae6eSVaclav Hapla   }
680*609dae6eSVaclav Hapla   PetscFunctionReturn(0);
681*609dae6eSVaclav Hapla }
682*609dae6eSVaclav Hapla 
683c6a43d28SMatthew G. Knepley /*@
684c6a43d28SMatthew G. Knepley   DMLabelComputeIndex - Create an index structure for membership determination, automatically determining the bounds
685c6a43d28SMatthew G. Knepley 
6865b5e7992SMatthew G. Knepley   Not collective
6875b5e7992SMatthew G. Knepley 
688c6a43d28SMatthew G. Knepley   Input Parameter:
689c6a43d28SMatthew G. Knepley . label  - The DMLabel
690c6a43d28SMatthew G. Knepley 
691c6a43d28SMatthew G. Knepley   Level: intermediate
692c6a43d28SMatthew G. Knepley 
693c6a43d28SMatthew G. Knepley .seealso: DMLabelHasPoint(), DMLabelCreateIndex(), DMLabelDestroyIndex(), DMLabelGetValue(), DMLabelSetValue()
694c6a43d28SMatthew G. Knepley @*/
695c6a43d28SMatthew G. Knepley PetscErrorCode DMLabelComputeIndex(DMLabel label)
696c6a43d28SMatthew G. Knepley {
697c6a43d28SMatthew G. Knepley   PetscInt       pStart = PETSC_MAX_INT, pEnd = -1, v;
698c6a43d28SMatthew G. Knepley   PetscErrorCode ierr;
699c6a43d28SMatthew G. Knepley 
700c6a43d28SMatthew G. Knepley   PetscFunctionBegin;
701c6a43d28SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
702c6a43d28SMatthew G. Knepley   ierr = DMLabelMakeAllValid_Private(label);CHKERRQ(ierr);
703c6a43d28SMatthew G. Knepley   for (v = 0; v < label->numStrata; ++v) {
704c6a43d28SMatthew G. Knepley     const PetscInt *points;
705c6a43d28SMatthew G. Knepley     PetscInt       i;
706c6a43d28SMatthew G. Knepley 
707c6a43d28SMatthew G. Knepley     ierr = ISGetIndices(label->points[v], &points);CHKERRQ(ierr);
708c6a43d28SMatthew G. Knepley     for (i = 0; i < label->stratumSizes[v]; ++i) {
709c6a43d28SMatthew G. Knepley       const PetscInt point = points[i];
710c6a43d28SMatthew G. Knepley 
711c6a43d28SMatthew G. Knepley       pStart = PetscMin(point,   pStart);
712c6a43d28SMatthew G. Knepley       pEnd   = PetscMax(point+1, pEnd);
713c6a43d28SMatthew G. Knepley     }
714c6a43d28SMatthew G. Knepley     ierr = ISRestoreIndices(label->points[v], &points);CHKERRQ(ierr);
715c6a43d28SMatthew G. Knepley   }
716c6a43d28SMatthew G. Knepley   label->pStart = pStart == PETSC_MAX_INT ? -1 : pStart;
717c6a43d28SMatthew G. Knepley   label->pEnd   = pEnd;
718c6a43d28SMatthew G. Knepley   ierr = DMLabelCreateIndex(label, label->pStart, label->pEnd);CHKERRQ(ierr);
719c6a43d28SMatthew G. Knepley   PetscFunctionReturn(0);
720c6a43d28SMatthew G. Knepley }
721c6a43d28SMatthew G. Knepley 
722c6a43d28SMatthew G. Knepley /*@
723c6a43d28SMatthew G. Knepley   DMLabelCreateIndex - Create an index structure for membership determination
724c6a43d28SMatthew G. Knepley 
7255b5e7992SMatthew G. Knepley   Not collective
7265b5e7992SMatthew G. Knepley 
727c6a43d28SMatthew G. Knepley   Input Parameters:
728c6a43d28SMatthew G. Knepley + label  - The DMLabel
729c6a43d28SMatthew G. Knepley . pStart - The smallest point
730c6a43d28SMatthew G. Knepley - pEnd   - The largest point + 1
731c6a43d28SMatthew G. Knepley 
732c6a43d28SMatthew G. Knepley   Level: intermediate
733c6a43d28SMatthew G. Knepley 
734c6a43d28SMatthew G. Knepley .seealso: DMLabelHasPoint(), DMLabelComputeIndex(), DMLabelDestroyIndex(), DMLabelGetValue(), DMLabelSetValue()
735c6a43d28SMatthew G. Knepley @*/
736c58f1c22SToby Isaac PetscErrorCode DMLabelCreateIndex(DMLabel label, PetscInt pStart, PetscInt pEnd)
737c58f1c22SToby Isaac {
738c58f1c22SToby Isaac   PetscInt       v;
739c58f1c22SToby Isaac   PetscErrorCode ierr;
740c58f1c22SToby Isaac 
741c58f1c22SToby Isaac   PetscFunctionBegin;
742d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
7430c3c4a36SLisandro Dalcin   ierr = DMLabelDestroyIndex(label);CHKERRQ(ierr);
744c58f1c22SToby Isaac   ierr = DMLabelMakeAllValid_Private(label);CHKERRQ(ierr);
745c58f1c22SToby Isaac   label->pStart = pStart;
746c58f1c22SToby Isaac   label->pEnd   = pEnd;
747c6a43d28SMatthew G. Knepley   /* This can be hooked into SetValue(),  ClearValue(), etc. for updating */
748c58f1c22SToby Isaac   ierr = PetscBTCreate(pEnd - pStart, &label->bt);CHKERRQ(ierr);
749c58f1c22SToby Isaac   for (v = 0; v < label->numStrata; ++v) {
750ad8374ffSToby Isaac     const PetscInt *points;
751c58f1c22SToby Isaac     PetscInt       i;
752c58f1c22SToby Isaac 
753ad8374ffSToby Isaac     ierr = ISGetIndices(label->points[v], &points);CHKERRQ(ierr);
754c58f1c22SToby Isaac     for (i = 0; i < label->stratumSizes[v]; ++i) {
755ad8374ffSToby Isaac       const PetscInt point = points[i];
756c58f1c22SToby Isaac 
757c58f1c22SToby 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);
758c58f1c22SToby Isaac       ierr = PetscBTSet(label->bt, point - pStart);CHKERRQ(ierr);
759c58f1c22SToby Isaac     }
760ad8374ffSToby Isaac     ierr = ISRestoreIndices(label->points[v], &points);CHKERRQ(ierr);
761c58f1c22SToby Isaac   }
762c58f1c22SToby Isaac   PetscFunctionReturn(0);
763c58f1c22SToby Isaac }
764c58f1c22SToby Isaac 
765c6a43d28SMatthew G. Knepley /*@
766c6a43d28SMatthew G. Knepley   DMLabelDestroyIndex - Destroy the index structure
767c6a43d28SMatthew G. Knepley 
7685b5e7992SMatthew G. Knepley   Not collective
7695b5e7992SMatthew G. Knepley 
770c6a43d28SMatthew G. Knepley   Input Parameter:
771c6a43d28SMatthew G. Knepley . label - the DMLabel
772c6a43d28SMatthew G. Knepley 
773c6a43d28SMatthew G. Knepley   Level: intermediate
774c6a43d28SMatthew G. Knepley 
775c6a43d28SMatthew G. Knepley .seealso: DMLabelHasPoint(), DMLabelCreateIndex(), DMLabelGetValue(), DMLabelSetValue()
776c6a43d28SMatthew G. Knepley @*/
777c58f1c22SToby Isaac PetscErrorCode DMLabelDestroyIndex(DMLabel label)
778c58f1c22SToby Isaac {
779c58f1c22SToby Isaac   PetscErrorCode ierr;
780c58f1c22SToby Isaac 
781c58f1c22SToby Isaac   PetscFunctionBegin;
782d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
783c58f1c22SToby Isaac   label->pStart = -1;
784c58f1c22SToby Isaac   label->pEnd   = -1;
7850c3c4a36SLisandro Dalcin   ierr = PetscBTDestroy(&label->bt);CHKERRQ(ierr);
786c58f1c22SToby Isaac   PetscFunctionReturn(0);
787c58f1c22SToby Isaac }
788c58f1c22SToby Isaac 
789c58f1c22SToby Isaac /*@
790c6a43d28SMatthew G. Knepley   DMLabelGetBounds - Return the smallest and largest point in the label
791c6a43d28SMatthew G. Knepley 
7925b5e7992SMatthew G. Knepley   Not collective
7935b5e7992SMatthew G. Knepley 
794c6a43d28SMatthew G. Knepley   Input Parameter:
795c6a43d28SMatthew G. Knepley . label - the DMLabel
796c6a43d28SMatthew G. Knepley 
797c6a43d28SMatthew G. Knepley   Output Parameters:
798c6a43d28SMatthew G. Knepley + pStart - The smallest point
799c6a43d28SMatthew G. Knepley - pEnd   - The largest point + 1
800c6a43d28SMatthew G. Knepley 
801c6a43d28SMatthew G. Knepley   Note: This will compute an index for the label if one does not exist.
802c6a43d28SMatthew G. Knepley 
803c6a43d28SMatthew G. Knepley   Level: intermediate
804c6a43d28SMatthew G. Knepley 
805c6a43d28SMatthew G. Knepley .seealso: DMLabelHasPoint(), DMLabelCreateIndex(), DMLabelGetValue(), DMLabelSetValue()
806c6a43d28SMatthew G. Knepley @*/
807c6a43d28SMatthew G. Knepley PetscErrorCode DMLabelGetBounds(DMLabel label, PetscInt *pStart, PetscInt *pEnd)
808c6a43d28SMatthew G. Knepley {
809c6a43d28SMatthew G. Knepley   PetscErrorCode ierr;
810c6a43d28SMatthew G. Knepley 
811c6a43d28SMatthew G. Knepley   PetscFunctionBegin;
812c6a43d28SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
813c6a43d28SMatthew G. Knepley   if ((label->pStart == -1) && (label->pEnd == -1)) {ierr = DMLabelComputeIndex(label);CHKERRQ(ierr);}
814c6a43d28SMatthew G. Knepley   if (pStart) {
815534a8f05SLisandro Dalcin     PetscValidIntPointer(pStart, 2);
816c6a43d28SMatthew G. Knepley     *pStart = label->pStart;
817c6a43d28SMatthew G. Knepley   }
818c6a43d28SMatthew G. Knepley   if (pEnd) {
819534a8f05SLisandro Dalcin     PetscValidIntPointer(pEnd, 3);
820c6a43d28SMatthew G. Knepley     *pEnd = label->pEnd;
821c6a43d28SMatthew G. Knepley   }
822c6a43d28SMatthew G. Knepley   PetscFunctionReturn(0);
823c6a43d28SMatthew G. Knepley }
824c6a43d28SMatthew G. Knepley 
825c6a43d28SMatthew G. Knepley /*@
826c58f1c22SToby Isaac   DMLabelHasValue - Determine whether a label assigns the value to any point
827c58f1c22SToby Isaac 
8285b5e7992SMatthew G. Knepley   Not collective
8295b5e7992SMatthew G. Knepley 
830c58f1c22SToby Isaac   Input Parameters:
831c58f1c22SToby Isaac + label - the DMLabel
832c58f1c22SToby Isaac - value - the value
833c58f1c22SToby Isaac 
834c58f1c22SToby Isaac   Output Parameter:
835c58f1c22SToby Isaac . contains - Flag indicating whether the label maps this value to any point
836c58f1c22SToby Isaac 
837c58f1c22SToby Isaac   Level: developer
838c58f1c22SToby Isaac 
839c58f1c22SToby Isaac .seealso: DMLabelHasPoint(), DMLabelGetValue(), DMLabelSetValue()
840c58f1c22SToby Isaac @*/
841c58f1c22SToby Isaac PetscErrorCode DMLabelHasValue(DMLabel label, PetscInt value, PetscBool *contains)
842c58f1c22SToby Isaac {
843c58f1c22SToby Isaac   PetscInt v;
8440c3c4a36SLisandro Dalcin   PetscErrorCode ierr;
845c58f1c22SToby Isaac 
846c58f1c22SToby Isaac   PetscFunctionBegin;
847d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
848534a8f05SLisandro Dalcin   PetscValidBoolPointer(contains, 3);
8490c3c4a36SLisandro Dalcin   ierr = DMLabelLookupStratum(label, value, &v);CHKERRQ(ierr);
8500c3c4a36SLisandro Dalcin   *contains = v < 0 ? PETSC_FALSE : PETSC_TRUE;
851c58f1c22SToby Isaac   PetscFunctionReturn(0);
852c58f1c22SToby Isaac }
853c58f1c22SToby Isaac 
854c58f1c22SToby Isaac /*@
855c58f1c22SToby Isaac   DMLabelHasPoint - Determine whether a label assigns a value to a point
856c58f1c22SToby Isaac 
8575b5e7992SMatthew G. Knepley   Not collective
8585b5e7992SMatthew G. Knepley 
859c58f1c22SToby Isaac   Input Parameters:
860c58f1c22SToby Isaac + label - the DMLabel
861c58f1c22SToby Isaac - point - the point
862c58f1c22SToby Isaac 
863c58f1c22SToby Isaac   Output Parameter:
864c58f1c22SToby Isaac . contains - Flag indicating whether the label maps this point to a value
865c58f1c22SToby Isaac 
866c58f1c22SToby Isaac   Note: The user must call DMLabelCreateIndex() before this function.
867c58f1c22SToby Isaac 
868c58f1c22SToby Isaac   Level: developer
869c58f1c22SToby Isaac 
870c58f1c22SToby Isaac .seealso: DMLabelCreateIndex(), DMLabelGetValue(), DMLabelSetValue()
871c58f1c22SToby Isaac @*/
872c58f1c22SToby Isaac PetscErrorCode DMLabelHasPoint(DMLabel label, PetscInt point, PetscBool *contains)
873c58f1c22SToby Isaac {
874c58f1c22SToby Isaac   PetscErrorCode ierr;
875c58f1c22SToby Isaac 
876c58f1c22SToby Isaac   PetscFunctionBeginHot;
877d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
878534a8f05SLisandro Dalcin   PetscValidBoolPointer(contains, 3);
879c58f1c22SToby Isaac   ierr = DMLabelMakeAllValid_Private(label);CHKERRQ(ierr);
88076bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
881c58f1c22SToby Isaac     if (!label->bt) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call DMLabelCreateIndex() before DMLabelHasPoint()");
882c58f1c22SToby 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);
88376bd3646SJed Brown   }
884c58f1c22SToby Isaac   *contains = PetscBTLookup(label->bt, point - label->pStart) ? PETSC_TRUE : PETSC_FALSE;
885c58f1c22SToby Isaac   PetscFunctionReturn(0);
886c58f1c22SToby Isaac }
887c58f1c22SToby Isaac 
888c58f1c22SToby Isaac /*@
889c58f1c22SToby Isaac   DMLabelStratumHasPoint - Return true if the stratum contains a point
890c58f1c22SToby Isaac 
8915b5e7992SMatthew G. Knepley   Not collective
8925b5e7992SMatthew G. Knepley 
893c58f1c22SToby Isaac   Input Parameters:
894c58f1c22SToby Isaac + label - the DMLabel
895c58f1c22SToby Isaac . value - the stratum value
896c58f1c22SToby Isaac - point - the point
897c58f1c22SToby Isaac 
898c58f1c22SToby Isaac   Output Parameter:
899c58f1c22SToby Isaac . contains - true if the stratum contains the point
900c58f1c22SToby Isaac 
901c58f1c22SToby Isaac   Level: intermediate
902c58f1c22SToby Isaac 
903c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMLabelSetValue(), DMLabelClearValue()
904c58f1c22SToby Isaac @*/
905c58f1c22SToby Isaac PetscErrorCode DMLabelStratumHasPoint(DMLabel label, PetscInt value, PetscInt point, PetscBool *contains)
906c58f1c22SToby Isaac {
907c58f1c22SToby Isaac   PetscInt       v;
908c58f1c22SToby Isaac   PetscErrorCode ierr;
909c58f1c22SToby Isaac 
9100c3c4a36SLisandro Dalcin   PetscFunctionBeginHot;
911d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
912534a8f05SLisandro Dalcin   PetscValidBoolPointer(contains, 4);
913c58f1c22SToby Isaac   *contains = PETSC_FALSE;
9140c3c4a36SLisandro Dalcin   ierr = DMLabelLookupStratum(label, value, &v);CHKERRQ(ierr);
9150c3c4a36SLisandro Dalcin   if (v < 0) PetscFunctionReturn(0);
9160c3c4a36SLisandro Dalcin 
917ad8374ffSToby Isaac   if (label->validIS[v]) {
918c58f1c22SToby Isaac     PetscInt i;
919c58f1c22SToby Isaac 
920a2d74346SToby Isaac     ierr = ISLocate(label->points[v], point, &i);CHKERRQ(ierr);
9210c3c4a36SLisandro Dalcin     if (i >= 0) *contains = PETSC_TRUE;
922c58f1c22SToby Isaac   } else {
923c58f1c22SToby Isaac     PetscBool has;
924c58f1c22SToby Isaac 
925b9907514SLisandro Dalcin     ierr = PetscHSetIHas(label->ht[v], point, &has);CHKERRQ(ierr);
9260c3c4a36SLisandro Dalcin     if (has) *contains = PETSC_TRUE;
927c58f1c22SToby Isaac   }
928c58f1c22SToby Isaac   PetscFunctionReturn(0);
929c58f1c22SToby Isaac }
930c58f1c22SToby Isaac 
93184f0b6dfSMatthew G. Knepley /*@
9325aa44df4SToby Isaac   DMLabelGetDefaultValue - Get the default value returned by DMLabelGetValue() if a point has not been explicitly given a value.
9335aa44df4SToby Isaac   When a label is created, it is initialized to -1.
9345aa44df4SToby Isaac 
9355b5e7992SMatthew G. Knepley   Not collective
9365b5e7992SMatthew G. Knepley 
9375aa44df4SToby Isaac   Input parameter:
9385aa44df4SToby Isaac . label - a DMLabel object
9395aa44df4SToby Isaac 
9405aa44df4SToby Isaac   Output parameter:
9415aa44df4SToby Isaac . defaultValue - the default value
9425aa44df4SToby Isaac 
9435aa44df4SToby Isaac   Level: beginner
9445aa44df4SToby Isaac 
9455aa44df4SToby Isaac .seealso: DMLabelSetDefaultValue(), DMLabelGetValue(), DMLabelSetValue()
94684f0b6dfSMatthew G. Knepley @*/
9475aa44df4SToby Isaac PetscErrorCode DMLabelGetDefaultValue(DMLabel label, PetscInt *defaultValue)
9485aa44df4SToby Isaac {
9495aa44df4SToby Isaac   PetscFunctionBegin;
950d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
9515aa44df4SToby Isaac   *defaultValue = label->defaultValue;
9525aa44df4SToby Isaac   PetscFunctionReturn(0);
9535aa44df4SToby Isaac }
9545aa44df4SToby Isaac 
95584f0b6dfSMatthew G. Knepley /*@
9565aa44df4SToby Isaac   DMLabelSetDefaultValue - Set the default value returned by DMLabelGetValue() if a point has not been explicitly given a value.
9575aa44df4SToby Isaac   When a label is created, it is initialized to -1.
9585aa44df4SToby Isaac 
9595b5e7992SMatthew G. Knepley   Not collective
9605b5e7992SMatthew G. Knepley 
9615aa44df4SToby Isaac   Input parameter:
9625aa44df4SToby Isaac . label - a DMLabel object
9635aa44df4SToby Isaac 
9645aa44df4SToby Isaac   Output parameter:
9655aa44df4SToby Isaac . defaultValue - the default value
9665aa44df4SToby Isaac 
9675aa44df4SToby Isaac   Level: beginner
9685aa44df4SToby Isaac 
9695aa44df4SToby Isaac .seealso: DMLabelGetDefaultValue(), DMLabelGetValue(), DMLabelSetValue()
97084f0b6dfSMatthew G. Knepley @*/
9715aa44df4SToby Isaac PetscErrorCode DMLabelSetDefaultValue(DMLabel label, PetscInt defaultValue)
9725aa44df4SToby Isaac {
9735aa44df4SToby Isaac   PetscFunctionBegin;
974d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
9755aa44df4SToby Isaac   label->defaultValue = defaultValue;
9765aa44df4SToby Isaac   PetscFunctionReturn(0);
9775aa44df4SToby Isaac }
9785aa44df4SToby Isaac 
979c58f1c22SToby Isaac /*@
9805aa44df4SToby 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())
981c58f1c22SToby Isaac 
9825b5e7992SMatthew G. Knepley   Not collective
9835b5e7992SMatthew G. Knepley 
984c58f1c22SToby Isaac   Input Parameters:
985c58f1c22SToby Isaac + label - the DMLabel
986c58f1c22SToby Isaac - point - the point
987c58f1c22SToby Isaac 
988c58f1c22SToby Isaac   Output Parameter:
9898e68afcfSMatthew G. Knepley . value - The point value, or the default value (-1 by default)
990c58f1c22SToby Isaac 
991c58f1c22SToby Isaac   Level: intermediate
992c58f1c22SToby Isaac 
9935aa44df4SToby Isaac .seealso: DMLabelCreate(), DMLabelSetValue(), DMLabelClearValue(), DMLabelGetDefaultValue(), DMLabelSetDefaultValue()
994c58f1c22SToby Isaac @*/
995c58f1c22SToby Isaac PetscErrorCode DMLabelGetValue(DMLabel label, PetscInt point, PetscInt *value)
996c58f1c22SToby Isaac {
997c58f1c22SToby Isaac   PetscInt       v;
998c58f1c22SToby Isaac   PetscErrorCode ierr;
999c58f1c22SToby Isaac 
10000c3c4a36SLisandro Dalcin   PetscFunctionBeginHot;
1001d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
1002c58f1c22SToby Isaac   PetscValidPointer(value, 3);
10035aa44df4SToby Isaac   *value = label->defaultValue;
1004c58f1c22SToby Isaac   for (v = 0; v < label->numStrata; ++v) {
1005ad8374ffSToby Isaac     if (label->validIS[v]) {
1006c58f1c22SToby Isaac       PetscInt i;
1007c58f1c22SToby Isaac 
1008a2d74346SToby Isaac       ierr = ISLocate(label->points[v], point, &i);CHKERRQ(ierr);
1009c58f1c22SToby Isaac       if (i >= 0) {
1010c58f1c22SToby Isaac         *value = label->stratumValues[v];
1011c58f1c22SToby Isaac         break;
1012c58f1c22SToby Isaac       }
1013c58f1c22SToby Isaac     } else {
1014c58f1c22SToby Isaac       PetscBool has;
1015c58f1c22SToby Isaac 
1016b9907514SLisandro Dalcin       ierr = PetscHSetIHas(label->ht[v], point, &has);CHKERRQ(ierr);
1017c58f1c22SToby Isaac       if (has) {
1018c58f1c22SToby Isaac         *value = label->stratumValues[v];
1019c58f1c22SToby Isaac         break;
1020c58f1c22SToby Isaac       }
1021c58f1c22SToby Isaac     }
1022c58f1c22SToby Isaac   }
1023c58f1c22SToby Isaac   PetscFunctionReturn(0);
1024c58f1c22SToby Isaac }
1025c58f1c22SToby Isaac 
1026c58f1c22SToby Isaac /*@
1027367003a6SStefano 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.
1028c58f1c22SToby Isaac 
10295b5e7992SMatthew G. Knepley   Not collective
10305b5e7992SMatthew G. Knepley 
1031c58f1c22SToby Isaac   Input Parameters:
1032c58f1c22SToby Isaac + label - the DMLabel
1033c58f1c22SToby Isaac . point - the point
1034c58f1c22SToby Isaac - value - The point value
1035c58f1c22SToby Isaac 
1036c58f1c22SToby Isaac   Level: intermediate
1037c58f1c22SToby Isaac 
10385aa44df4SToby Isaac .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelClearValue(), DMLabelGetDefaultValue(), DMLabelSetDefaultValue()
1039c58f1c22SToby Isaac @*/
1040c58f1c22SToby Isaac PetscErrorCode DMLabelSetValue(DMLabel label, PetscInt point, PetscInt value)
1041c58f1c22SToby Isaac {
1042c58f1c22SToby Isaac   PetscInt       v;
1043c58f1c22SToby Isaac   PetscErrorCode ierr;
1044c58f1c22SToby Isaac 
1045c58f1c22SToby Isaac   PetscFunctionBegin;
1046d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
10470c3c4a36SLisandro Dalcin   /* Find label value, add new entry if needed */
10485aa44df4SToby Isaac   if (value == label->defaultValue) PetscFunctionReturn(0);
1049b9907514SLisandro Dalcin   ierr = DMLabelLookupAddStratum(label, value, &v);CHKERRQ(ierr);
1050c58f1c22SToby Isaac   /* Set key */
10510c3c4a36SLisandro Dalcin   ierr = DMLabelMakeInvalid_Private(label, v);CHKERRQ(ierr);
1052e8f14785SLisandro Dalcin   ierr = PetscHSetIAdd(label->ht[v], point);CHKERRQ(ierr);
1053c58f1c22SToby Isaac   PetscFunctionReturn(0);
1054c58f1c22SToby Isaac }
1055c58f1c22SToby Isaac 
1056c58f1c22SToby Isaac /*@
1057c58f1c22SToby Isaac   DMLabelClearValue - Clear the value a label assigns to a point
1058c58f1c22SToby Isaac 
10595b5e7992SMatthew G. Knepley   Not collective
10605b5e7992SMatthew G. Knepley 
1061c58f1c22SToby Isaac   Input Parameters:
1062c58f1c22SToby Isaac + label - the DMLabel
1063c58f1c22SToby Isaac . point - the point
1064c58f1c22SToby Isaac - value - The point value
1065c58f1c22SToby Isaac 
1066c58f1c22SToby Isaac   Level: intermediate
1067c58f1c22SToby Isaac 
1068c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue()
1069c58f1c22SToby Isaac @*/
1070c58f1c22SToby Isaac PetscErrorCode DMLabelClearValue(DMLabel label, PetscInt point, PetscInt value)
1071c58f1c22SToby Isaac {
1072ad8374ffSToby Isaac   PetscInt       v;
1073c58f1c22SToby Isaac   PetscErrorCode ierr;
1074c58f1c22SToby Isaac 
1075c58f1c22SToby Isaac   PetscFunctionBegin;
1076d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
1077c58f1c22SToby Isaac   /* Find label value */
10780c3c4a36SLisandro Dalcin   ierr = DMLabelLookupStratum(label, value, &v);CHKERRQ(ierr);
10790c3c4a36SLisandro Dalcin   if (v < 0) PetscFunctionReturn(0);
10800c3c4a36SLisandro Dalcin 
1081eeed21e7SToby Isaac   if (label->bt) {
1082eeed21e7SToby 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);
1083eeed21e7SToby Isaac     ierr = PetscBTClear(label->bt, point - label->pStart);CHKERRQ(ierr);
1084eeed21e7SToby Isaac   }
10850c3c4a36SLisandro Dalcin 
10860c3c4a36SLisandro Dalcin   /* Delete key */
10870c3c4a36SLisandro Dalcin   ierr = DMLabelMakeInvalid_Private(label, v);CHKERRQ(ierr);
1088e8f14785SLisandro Dalcin   ierr = PetscHSetIDel(label->ht[v], point);CHKERRQ(ierr);
1089c58f1c22SToby Isaac   PetscFunctionReturn(0);
1090c58f1c22SToby Isaac }
1091c58f1c22SToby Isaac 
1092c58f1c22SToby Isaac /*@
1093c58f1c22SToby Isaac   DMLabelInsertIS - Set all points in the IS to a value
1094c58f1c22SToby Isaac 
10955b5e7992SMatthew G. Knepley   Not collective
10965b5e7992SMatthew G. Knepley 
1097c58f1c22SToby Isaac   Input Parameters:
1098c58f1c22SToby Isaac + label - the DMLabel
1099c58f1c22SToby Isaac . is    - the point IS
1100c58f1c22SToby Isaac - value - The point value
1101c58f1c22SToby Isaac 
1102c58f1c22SToby Isaac   Level: intermediate
1103c58f1c22SToby Isaac 
1104c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
1105c58f1c22SToby Isaac @*/
1106c58f1c22SToby Isaac PetscErrorCode DMLabelInsertIS(DMLabel label, IS is, PetscInt value)
1107c58f1c22SToby Isaac {
11080c3c4a36SLisandro Dalcin   PetscInt        v, n, p;
1109c58f1c22SToby Isaac   const PetscInt *points;
1110c58f1c22SToby Isaac   PetscErrorCode  ierr;
1111c58f1c22SToby Isaac 
1112c58f1c22SToby Isaac   PetscFunctionBegin;
1113d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
1114c58f1c22SToby Isaac   PetscValidHeaderSpecific(is, IS_CLASSID, 2);
11150c3c4a36SLisandro Dalcin   /* Find label value, add new entry if needed */
11160c3c4a36SLisandro Dalcin   if (value == label->defaultValue) PetscFunctionReturn(0);
1117b9907514SLisandro Dalcin   ierr = DMLabelLookupAddStratum(label, value, &v);CHKERRQ(ierr);
11180c3c4a36SLisandro Dalcin   /* Set keys */
11190c3c4a36SLisandro Dalcin   ierr = DMLabelMakeInvalid_Private(label, v);CHKERRQ(ierr);
1120c58f1c22SToby Isaac   ierr = ISGetLocalSize(is, &n);CHKERRQ(ierr);
1121c58f1c22SToby Isaac   ierr = ISGetIndices(is, &points);CHKERRQ(ierr);
1122e8f14785SLisandro Dalcin   for (p = 0; p < n; ++p) {ierr = PetscHSetIAdd(label->ht[v], points[p]);CHKERRQ(ierr);}
1123c58f1c22SToby Isaac   ierr = ISRestoreIndices(is, &points);CHKERRQ(ierr);
1124c58f1c22SToby Isaac   PetscFunctionReturn(0);
1125c58f1c22SToby Isaac }
1126c58f1c22SToby Isaac 
112784f0b6dfSMatthew G. Knepley /*@
112884f0b6dfSMatthew G. Knepley   DMLabelGetNumValues - Get the number of values that the DMLabel takes
112984f0b6dfSMatthew G. Knepley 
11305b5e7992SMatthew G. Knepley   Not collective
11315b5e7992SMatthew G. Knepley 
113284f0b6dfSMatthew G. Knepley   Input Parameter:
113384f0b6dfSMatthew G. Knepley . label - the DMLabel
113484f0b6dfSMatthew G. Knepley 
113501d2d390SJose E. Roman   Output Parameter:
113684f0b6dfSMatthew G. Knepley . numValues - the number of values
113784f0b6dfSMatthew G. Knepley 
113884f0b6dfSMatthew G. Knepley   Level: intermediate
113984f0b6dfSMatthew G. Knepley 
114084f0b6dfSMatthew G. Knepley .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
114184f0b6dfSMatthew G. Knepley @*/
1142c58f1c22SToby Isaac PetscErrorCode DMLabelGetNumValues(DMLabel label, PetscInt *numValues)
1143c58f1c22SToby Isaac {
1144c58f1c22SToby Isaac   PetscFunctionBegin;
1145d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
1146c58f1c22SToby Isaac   PetscValidPointer(numValues, 2);
1147c58f1c22SToby Isaac   *numValues = label->numStrata;
1148c58f1c22SToby Isaac   PetscFunctionReturn(0);
1149c58f1c22SToby Isaac }
1150c58f1c22SToby Isaac 
115184f0b6dfSMatthew G. Knepley /*@
115284f0b6dfSMatthew G. Knepley   DMLabelGetValueIS - Get an IS of all values that the DMlabel takes
115384f0b6dfSMatthew G. Knepley 
11545b5e7992SMatthew G. Knepley   Not collective
11555b5e7992SMatthew G. Knepley 
115684f0b6dfSMatthew G. Knepley   Input Parameter:
115784f0b6dfSMatthew G. Knepley . label - the DMLabel
115884f0b6dfSMatthew G. Knepley 
115901d2d390SJose E. Roman   Output Parameter:
116084f0b6dfSMatthew G. Knepley . is    - the value IS
116184f0b6dfSMatthew G. Knepley 
116284f0b6dfSMatthew G. Knepley   Level: intermediate
116384f0b6dfSMatthew G. Knepley 
11641d04cbbeSVaclav Hapla   Notes:
11651d04cbbeSVaclav Hapla   The output IS should be destroyed when no longer needed.
11661d04cbbeSVaclav Hapla   Strata which are allocated but empty [DMLabelGetStratumSize() yields 0] are counted.
11671d04cbbeSVaclav Hapla   If you need to count only nonempty strata, use DMLabelGetNonEmptyStratumValuesIS().
11681d04cbbeSVaclav Hapla 
11691d04cbbeSVaclav Hapla .seealso: DMLabelGetNonEmptyStratumValuesIS(), DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
117084f0b6dfSMatthew G. Knepley @*/
1171c58f1c22SToby Isaac PetscErrorCode DMLabelGetValueIS(DMLabel label, IS *values)
1172c58f1c22SToby Isaac {
1173c58f1c22SToby Isaac   PetscErrorCode ierr;
1174c58f1c22SToby Isaac 
1175c58f1c22SToby Isaac   PetscFunctionBegin;
1176d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
1177c58f1c22SToby Isaac   PetscValidPointer(values, 2);
1178c58f1c22SToby Isaac   ierr = ISCreateGeneral(PETSC_COMM_SELF, label->numStrata, label->stratumValues, PETSC_USE_POINTER, values);CHKERRQ(ierr);
1179c58f1c22SToby Isaac   PetscFunctionReturn(0);
1180c58f1c22SToby Isaac }
1181c58f1c22SToby Isaac 
118284f0b6dfSMatthew G. Knepley /*@
11831d04cbbeSVaclav Hapla   DMLabelGetNonEmptyStratumValuesIS - Get an IS of all values that the DMlabel takes
11841d04cbbeSVaclav Hapla 
11851d04cbbeSVaclav Hapla   Not collective
11861d04cbbeSVaclav Hapla 
11871d04cbbeSVaclav Hapla   Input Parameter:
11881d04cbbeSVaclav Hapla . label - the DMLabel
11891d04cbbeSVaclav Hapla 
11901d04cbbeSVaclav Hapla   Output Paramater:
11911d04cbbeSVaclav Hapla . is    - the value IS
11921d04cbbeSVaclav Hapla 
11931d04cbbeSVaclav Hapla   Level: intermediate
11941d04cbbeSVaclav Hapla 
11951d04cbbeSVaclav Hapla   Notes:
11961d04cbbeSVaclav Hapla   The output IS should be destroyed when no longer needed.
11971d04cbbeSVaclav Hapla   This is similar to DMLabelGetValueIS() but counts only nonempty strata.
11981d04cbbeSVaclav Hapla 
11991d04cbbeSVaclav Hapla .seealso: DMLabelGetValueIS(), DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
12001d04cbbeSVaclav Hapla @*/
12011d04cbbeSVaclav Hapla PetscErrorCode DMLabelGetNonEmptyStratumValuesIS(DMLabel label, IS *values)
12021d04cbbeSVaclav Hapla {
12031d04cbbeSVaclav Hapla   PetscInt        i, j;
12041d04cbbeSVaclav Hapla   PetscInt       *valuesArr;
12051d04cbbeSVaclav Hapla   PetscErrorCode  ierr;
12061d04cbbeSVaclav Hapla 
12071d04cbbeSVaclav Hapla   PetscFunctionBegin;
12081d04cbbeSVaclav Hapla   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
12091d04cbbeSVaclav Hapla   PetscValidPointer(values, 2);
12101d04cbbeSVaclav Hapla   ierr = PetscMalloc1(label->numStrata, &valuesArr);CHKERRQ(ierr);
12111d04cbbeSVaclav Hapla   for (i = 0, j = 0; i < label->numStrata; i++) {
12121d04cbbeSVaclav Hapla     PetscInt        n;
12131d04cbbeSVaclav Hapla 
12141d04cbbeSVaclav Hapla     ierr = DMLabelGetStratumSize_Private(label, i, &n);CHKERRQ(ierr);
12151d04cbbeSVaclav Hapla     if (n) valuesArr[j++] = label->stratumValues[i];
12161d04cbbeSVaclav Hapla   }
12171d04cbbeSVaclav Hapla   if (j == label->numStrata) {
12181d04cbbeSVaclav Hapla     ierr = ISCreateGeneral(PETSC_COMM_SELF, label->numStrata, label->stratumValues, PETSC_USE_POINTER, values);CHKERRQ(ierr);
12191d04cbbeSVaclav Hapla   } else {
12201d04cbbeSVaclav Hapla     ierr = ISCreateGeneral(PETSC_COMM_SELF, j, valuesArr, PETSC_COPY_VALUES, values);CHKERRQ(ierr);
12211d04cbbeSVaclav Hapla   }
12221d04cbbeSVaclav Hapla   ierr = PetscFree(valuesArr);CHKERRQ(ierr);
12231d04cbbeSVaclav Hapla   PetscFunctionReturn(0);
12241d04cbbeSVaclav Hapla }
12251d04cbbeSVaclav Hapla 
12261d04cbbeSVaclav Hapla /*@
1227d123abd9SMatthew G. Knepley   DMLabelGetValueIndex - Get the index of a given value in the list of values for the DMlabel, or -1 if it is not present
1228d123abd9SMatthew G. Knepley 
1229d123abd9SMatthew G. Knepley   Not collective
1230d123abd9SMatthew G. Knepley 
1231d123abd9SMatthew G. Knepley   Input Parameters:
1232d123abd9SMatthew G. Knepley + label - the DMLabel
123397bb3fdcSJose E. Roman - value - the value
1234d123abd9SMatthew G. Knepley 
123501d2d390SJose E. Roman   Output Parameter:
1236d123abd9SMatthew G. Knepley . index - the index of value in the list of values
1237d123abd9SMatthew G. Knepley 
1238d123abd9SMatthew G. Knepley   Level: intermediate
1239d123abd9SMatthew G. Knepley 
1240d123abd9SMatthew G. Knepley .seealso: DMLabelGetValueIS(), DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
1241d123abd9SMatthew G. Knepley @*/
1242d123abd9SMatthew G. Knepley PetscErrorCode DMLabelGetValueIndex(DMLabel label, PetscInt value, PetscInt *index)
1243d123abd9SMatthew G. Knepley {
1244d123abd9SMatthew G. Knepley   PetscInt v;
1245d123abd9SMatthew G. Knepley 
1246d123abd9SMatthew G. Knepley   PetscFunctionBegin;
1247d123abd9SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
1248d123abd9SMatthew G. Knepley   PetscValidPointer(index, 3);
1249d123abd9SMatthew G. Knepley   /* Do not assume they are sorted */
1250d123abd9SMatthew G. Knepley   for (v = 0; v < label->numStrata; ++v) if (label->stratumValues[v] == value) break;
1251d123abd9SMatthew G. Knepley   if (v >= label->numStrata) *index = -1;
1252d123abd9SMatthew G. Knepley   else                       *index = v;
1253d123abd9SMatthew G. Knepley   PetscFunctionReturn(0);
1254d123abd9SMatthew G. Knepley }
1255d123abd9SMatthew G. Knepley 
1256d123abd9SMatthew G. Knepley /*@
125784f0b6dfSMatthew G. Knepley   DMLabelHasStratum - Determine whether points exist with the given value
125884f0b6dfSMatthew G. Knepley 
12595b5e7992SMatthew G. Knepley   Not collective
12605b5e7992SMatthew G. Knepley 
126184f0b6dfSMatthew G. Knepley   Input Parameters:
126284f0b6dfSMatthew G. Knepley + label - the DMLabel
126384f0b6dfSMatthew G. Knepley - value - the stratum value
126484f0b6dfSMatthew G. Knepley 
126501d2d390SJose E. Roman   Output Parameter:
126684f0b6dfSMatthew G. Knepley . exists - Flag saying whether points exist
126784f0b6dfSMatthew G. Knepley 
126884f0b6dfSMatthew G. Knepley   Level: intermediate
126984f0b6dfSMatthew G. Knepley 
127084f0b6dfSMatthew G. Knepley .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
127184f0b6dfSMatthew G. Knepley @*/
1272fada774cSMatthew G. Knepley PetscErrorCode DMLabelHasStratum(DMLabel label, PetscInt value, PetscBool *exists)
1273fada774cSMatthew G. Knepley {
1274fada774cSMatthew G. Knepley   PetscInt       v;
12750c3c4a36SLisandro Dalcin   PetscErrorCode ierr;
1276fada774cSMatthew G. Knepley 
1277fada774cSMatthew G. Knepley   PetscFunctionBegin;
1278d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
1279fada774cSMatthew G. Knepley   PetscValidPointer(exists, 3);
12800c3c4a36SLisandro Dalcin   ierr = DMLabelLookupStratum(label, value, &v);CHKERRQ(ierr);
12810c3c4a36SLisandro Dalcin   *exists = v < 0 ? PETSC_FALSE : PETSC_TRUE;
1282fada774cSMatthew G. Knepley   PetscFunctionReturn(0);
1283fada774cSMatthew G. Knepley }
1284fada774cSMatthew G. Knepley 
128584f0b6dfSMatthew G. Knepley /*@
128684f0b6dfSMatthew G. Knepley   DMLabelGetStratumSize - Get the size of a stratum
128784f0b6dfSMatthew G. Knepley 
12885b5e7992SMatthew G. Knepley   Not collective
12895b5e7992SMatthew G. Knepley 
129084f0b6dfSMatthew G. Knepley   Input Parameters:
129184f0b6dfSMatthew G. Knepley + label - the DMLabel
129284f0b6dfSMatthew G. Knepley - value - the stratum value
129384f0b6dfSMatthew G. Knepley 
129401d2d390SJose E. Roman   Output Parameter:
129584f0b6dfSMatthew G. Knepley . size - The number of points in the stratum
129684f0b6dfSMatthew G. Knepley 
129784f0b6dfSMatthew G. Knepley   Level: intermediate
129884f0b6dfSMatthew G. Knepley 
129984f0b6dfSMatthew G. Knepley .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
130084f0b6dfSMatthew G. Knepley @*/
1301c58f1c22SToby Isaac PetscErrorCode DMLabelGetStratumSize(DMLabel label, PetscInt value, PetscInt *size)
1302c58f1c22SToby Isaac {
1303c58f1c22SToby Isaac   PetscInt       v;
1304c58f1c22SToby Isaac   PetscErrorCode ierr;
1305c58f1c22SToby Isaac 
1306c58f1c22SToby Isaac   PetscFunctionBegin;
1307d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
1308c58f1c22SToby Isaac   PetscValidPointer(size, 3);
13090c3c4a36SLisandro Dalcin   ierr = DMLabelLookupStratum(label, value, &v);CHKERRQ(ierr);
13109e63cc69SVaclav Hapla   ierr = DMLabelGetStratumSize_Private(label, v, size);CHKERRQ(ierr);
1311c58f1c22SToby Isaac   PetscFunctionReturn(0);
1312c58f1c22SToby Isaac }
1313c58f1c22SToby Isaac 
131484f0b6dfSMatthew G. Knepley /*@
131584f0b6dfSMatthew G. Knepley   DMLabelGetStratumBounds - Get the largest and smallest point of a stratum
131684f0b6dfSMatthew G. Knepley 
13175b5e7992SMatthew G. Knepley   Not collective
13185b5e7992SMatthew G. Knepley 
131984f0b6dfSMatthew G. Knepley   Input Parameters:
132084f0b6dfSMatthew G. Knepley + label - the DMLabel
132184f0b6dfSMatthew G. Knepley - value - the stratum value
132284f0b6dfSMatthew G. Knepley 
132301d2d390SJose E. Roman   Output Parameters:
132484f0b6dfSMatthew G. Knepley + start - the smallest point in the stratum
132584f0b6dfSMatthew G. Knepley - end - the largest point in the stratum
132684f0b6dfSMatthew G. Knepley 
132784f0b6dfSMatthew G. Knepley   Level: intermediate
132884f0b6dfSMatthew G. Knepley 
132984f0b6dfSMatthew G. Knepley .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
133084f0b6dfSMatthew G. Knepley @*/
1331c58f1c22SToby Isaac PetscErrorCode DMLabelGetStratumBounds(DMLabel label, PetscInt value, PetscInt *start, PetscInt *end)
1332c58f1c22SToby Isaac {
13330c3c4a36SLisandro Dalcin   PetscInt       v, min, max;
1334c58f1c22SToby Isaac   PetscErrorCode ierr;
1335c58f1c22SToby Isaac 
1336c58f1c22SToby Isaac   PetscFunctionBegin;
1337d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
1338cade499fSVaclav Hapla   if (start) {PetscValidPointer(start, 3); *start = -1;}
1339cade499fSVaclav Hapla   if (end)   {PetscValidPointer(end,   4); *end   = -1;}
13400c3c4a36SLisandro Dalcin   ierr = DMLabelLookupStratum(label, value, &v);CHKERRQ(ierr);
13410c3c4a36SLisandro Dalcin   if (v < 0) PetscFunctionReturn(0);
1342c58f1c22SToby Isaac   ierr = DMLabelMakeValid_Private(label, v);CHKERRQ(ierr);
13430c3c4a36SLisandro Dalcin   if (label->stratumSizes[v] <= 0) PetscFunctionReturn(0);
1344d6cb179aSToby Isaac   ierr = ISGetMinMax(label->points[v], &min, &max);CHKERRQ(ierr);
1345d6cb179aSToby Isaac   if (start) *start = min;
1346d6cb179aSToby Isaac   if (end)   *end   = max+1;
1347c58f1c22SToby Isaac   PetscFunctionReturn(0);
1348c58f1c22SToby Isaac }
1349c58f1c22SToby Isaac 
135084f0b6dfSMatthew G. Knepley /*@
135184f0b6dfSMatthew G. Knepley   DMLabelGetStratumIS - Get an IS with the stratum points
135284f0b6dfSMatthew G. Knepley 
13535b5e7992SMatthew G. Knepley   Not collective
13545b5e7992SMatthew G. Knepley 
135584f0b6dfSMatthew G. Knepley   Input Parameters:
135684f0b6dfSMatthew G. Knepley + label - the DMLabel
135784f0b6dfSMatthew G. Knepley - value - the stratum value
135884f0b6dfSMatthew G. Knepley 
135901d2d390SJose E. Roman   Output Parameter:
136084f0b6dfSMatthew G. Knepley . points - The stratum points
136184f0b6dfSMatthew G. Knepley 
136284f0b6dfSMatthew G. Knepley   Level: intermediate
136384f0b6dfSMatthew G. Knepley 
1364593ce467SVaclav Hapla   Notes:
1365593ce467SVaclav Hapla   The output IS should be destroyed when no longer needed.
1366593ce467SVaclav Hapla   Returns NULL if the stratum is empty.
1367593ce467SVaclav Hapla 
136884f0b6dfSMatthew G. Knepley .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
136984f0b6dfSMatthew G. Knepley @*/
1370c58f1c22SToby Isaac PetscErrorCode DMLabelGetStratumIS(DMLabel label, PetscInt value, IS *points)
1371c58f1c22SToby Isaac {
1372c58f1c22SToby Isaac   PetscInt       v;
1373c58f1c22SToby Isaac   PetscErrorCode ierr;
1374c58f1c22SToby Isaac 
1375c58f1c22SToby Isaac   PetscFunctionBegin;
1376d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
1377c58f1c22SToby Isaac   PetscValidPointer(points, 3);
1378c58f1c22SToby Isaac   *points = NULL;
13790c3c4a36SLisandro Dalcin   ierr = DMLabelLookupStratum(label, value, &v);CHKERRQ(ierr);
13800c3c4a36SLisandro Dalcin   if (v < 0) PetscFunctionReturn(0);
1381c58f1c22SToby Isaac   ierr = DMLabelMakeValid_Private(label, v);CHKERRQ(ierr);
1382ad8374ffSToby Isaac   ierr = PetscObjectReference((PetscObject) label->points[v]);CHKERRQ(ierr);
1383ad8374ffSToby Isaac   *points = label->points[v];
1384c58f1c22SToby Isaac   PetscFunctionReturn(0);
1385c58f1c22SToby Isaac }
1386c58f1c22SToby Isaac 
138784f0b6dfSMatthew G. Knepley /*@
138884f0b6dfSMatthew G. Knepley   DMLabelSetStratumIS - Set the stratum points using an IS
138984f0b6dfSMatthew G. Knepley 
13905b5e7992SMatthew G. Knepley   Not collective
13915b5e7992SMatthew G. Knepley 
139284f0b6dfSMatthew G. Knepley   Input Parameters:
139384f0b6dfSMatthew G. Knepley + label - the DMLabel
139484f0b6dfSMatthew G. Knepley . value - the stratum value
139584f0b6dfSMatthew G. Knepley - points - The stratum points
139684f0b6dfSMatthew G. Knepley 
139784f0b6dfSMatthew G. Knepley   Level: intermediate
139884f0b6dfSMatthew G. Knepley 
139984f0b6dfSMatthew G. Knepley .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
140084f0b6dfSMatthew G. Knepley @*/
14014de306b1SToby Isaac PetscErrorCode DMLabelSetStratumIS(DMLabel label, PetscInt value, IS is)
14024de306b1SToby Isaac {
14030c3c4a36SLisandro Dalcin   PetscInt       v;
14044de306b1SToby Isaac   PetscErrorCode ierr;
14054de306b1SToby Isaac 
14064de306b1SToby Isaac   PetscFunctionBegin;
1407d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
1408d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(is, IS_CLASSID, 3);
1409b9907514SLisandro Dalcin   ierr = DMLabelLookupAddStratum(label, value, &v);CHKERRQ(ierr);
14104de306b1SToby Isaac   if (is == label->points[v]) PetscFunctionReturn(0);
14114de306b1SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
14124de306b1SToby Isaac   ierr = ISGetLocalSize(is, &(label->stratumSizes[v]));CHKERRQ(ierr);
14134de306b1SToby Isaac   ierr = PetscObjectReference((PetscObject)is);CHKERRQ(ierr);
14144de306b1SToby Isaac   ierr = ISDestroy(&(label->points[v]));CHKERRQ(ierr);
14150c3c4a36SLisandro Dalcin   label->points[v]  = is;
14160c3c4a36SLisandro Dalcin   label->validIS[v] = PETSC_TRUE;
1417277ea44aSLisandro Dalcin   ierr = PetscObjectStateIncrease((PetscObject) label);CHKERRQ(ierr);
14184de306b1SToby Isaac   if (label->bt) {
14194de306b1SToby Isaac     const PetscInt *points;
14204de306b1SToby Isaac     PetscInt p;
14214de306b1SToby Isaac 
14224de306b1SToby Isaac     ierr = ISGetIndices(is,&points);CHKERRQ(ierr);
14234de306b1SToby Isaac     for (p = 0; p < label->stratumSizes[v]; ++p) {
14244de306b1SToby Isaac       const PetscInt point = points[p];
14254de306b1SToby Isaac 
14264de306b1SToby 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);
14274de306b1SToby Isaac       ierr = PetscBTSet(label->bt, point - label->pStart);CHKERRQ(ierr);
14284de306b1SToby Isaac     }
14294de306b1SToby Isaac   }
14304de306b1SToby Isaac   PetscFunctionReturn(0);
14314de306b1SToby Isaac }
14324de306b1SToby Isaac 
143384f0b6dfSMatthew G. Knepley /*@
143484f0b6dfSMatthew G. Knepley   DMLabelClearStratum - Remove a stratum
14354de306b1SToby Isaac 
14365b5e7992SMatthew G. Knepley   Not collective
14375b5e7992SMatthew G. Knepley 
143884f0b6dfSMatthew G. Knepley   Input Parameters:
143984f0b6dfSMatthew G. Knepley + label - the DMLabel
144084f0b6dfSMatthew G. Knepley - value - the stratum value
144184f0b6dfSMatthew G. Knepley 
144284f0b6dfSMatthew G. Knepley   Level: intermediate
144384f0b6dfSMatthew G. Knepley 
144484f0b6dfSMatthew G. Knepley .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
144584f0b6dfSMatthew G. Knepley @*/
1446c58f1c22SToby Isaac PetscErrorCode DMLabelClearStratum(DMLabel label, PetscInt value)
1447c58f1c22SToby Isaac {
1448c58f1c22SToby Isaac   PetscInt       v;
1449c58f1c22SToby Isaac   PetscErrorCode ierr;
1450c58f1c22SToby Isaac 
1451c58f1c22SToby Isaac   PetscFunctionBegin;
1452d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
14530c3c4a36SLisandro Dalcin   ierr = DMLabelLookupStratum(label, value, &v);CHKERRQ(ierr);
14540c3c4a36SLisandro Dalcin   if (v < 0) PetscFunctionReturn(0);
14554de306b1SToby Isaac   if (label->validIS[v]) {
14564de306b1SToby Isaac     if (label->bt) {
1457c58f1c22SToby Isaac       PetscInt       i;
1458ad8374ffSToby Isaac       const PetscInt *points;
1459c58f1c22SToby Isaac 
1460ad8374ffSToby Isaac       ierr = ISGetIndices(label->points[v], &points);CHKERRQ(ierr);
1461c58f1c22SToby Isaac       for (i = 0; i < label->stratumSizes[v]; ++i) {
1462ad8374ffSToby Isaac         const PetscInt point = points[i];
1463c58f1c22SToby Isaac 
1464c58f1c22SToby 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);
1465c58f1c22SToby Isaac         ierr = PetscBTClear(label->bt, point - label->pStart);CHKERRQ(ierr);
1466c58f1c22SToby Isaac       }
1467ad8374ffSToby Isaac       ierr = ISRestoreIndices(label->points[v], &points);CHKERRQ(ierr);
1468c58f1c22SToby Isaac     }
1469c58f1c22SToby Isaac     label->stratumSizes[v] = 0;
14700c3c4a36SLisandro Dalcin     ierr = ISDestroy(&label->points[v]);CHKERRQ(ierr);
1471277ea44aSLisandro Dalcin     ierr = ISCreateStride(PETSC_COMM_SELF, 0, 0, 1, &label->points[v]);CHKERRQ(ierr);
14720c3c4a36SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject) label->points[v], "indices");CHKERRQ(ierr);
1473277ea44aSLisandro Dalcin     ierr = PetscObjectStateIncrease((PetscObject) label);CHKERRQ(ierr);
1474c58f1c22SToby Isaac   } else {
1475b9907514SLisandro Dalcin     ierr = PetscHSetIClear(label->ht[v]);CHKERRQ(ierr);
1476c58f1c22SToby Isaac   }
1477c58f1c22SToby Isaac   PetscFunctionReturn(0);
1478c58f1c22SToby Isaac }
1479c58f1c22SToby Isaac 
148084f0b6dfSMatthew G. Knepley /*@
1481412e9a14SMatthew G. Knepley   DMLabelSetStratumBounds - Efficiently give a contiguous set of points a given label value
1482412e9a14SMatthew G. Knepley 
1483412e9a14SMatthew G. Knepley   Not collective
1484412e9a14SMatthew G. Knepley 
1485412e9a14SMatthew G. Knepley   Input Parameters:
1486412e9a14SMatthew G. Knepley + label  - The DMLabel
1487412e9a14SMatthew G. Knepley . value  - The label value for all points
1488412e9a14SMatthew G. Knepley . pStart - The first point
1489412e9a14SMatthew G. Knepley - pEnd   - A point beyond all marked points
1490412e9a14SMatthew G. Knepley 
1491412e9a14SMatthew G. Knepley   Note: The marks points are [pStart, pEnd), and only the bounds are stored.
1492412e9a14SMatthew G. Knepley 
1493412e9a14SMatthew G. Knepley   Level: intermediate
1494412e9a14SMatthew G. Knepley 
1495412e9a14SMatthew G. Knepley .seealso: DMLabelCreate(), DMLabelSetStratumIS(), DMLabelGetStratumIS()
1496412e9a14SMatthew G. Knepley @*/
1497412e9a14SMatthew G. Knepley PetscErrorCode DMLabelSetStratumBounds(DMLabel label, PetscInt value, PetscInt pStart, PetscInt pEnd)
1498412e9a14SMatthew G. Knepley {
1499412e9a14SMatthew G. Knepley   IS             pIS;
1500412e9a14SMatthew G. Knepley   PetscErrorCode ierr;
1501412e9a14SMatthew G. Knepley 
1502412e9a14SMatthew G. Knepley   PetscFunctionBegin;
1503412e9a14SMatthew G. Knepley   ierr = ISCreateStride(PETSC_COMM_SELF, pEnd - pStart, pStart, 1, &pIS);CHKERRQ(ierr);
1504412e9a14SMatthew G. Knepley   ierr = DMLabelSetStratumIS(label, value, pIS);CHKERRQ(ierr);
1505412e9a14SMatthew G. Knepley   ierr = ISDestroy(&pIS);CHKERRQ(ierr);
1506412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
1507412e9a14SMatthew G. Knepley }
1508412e9a14SMatthew G. Knepley 
1509412e9a14SMatthew G. Knepley /*@
1510d123abd9SMatthew G. Knepley   DMLabelGetStratumPointIndex - Get the index of a point in a given stratum
1511d123abd9SMatthew G. Knepley 
1512d123abd9SMatthew G. Knepley   Not collective
1513d123abd9SMatthew G. Knepley 
1514d123abd9SMatthew G. Knepley   Input Parameters:
1515d123abd9SMatthew G. Knepley + label  - The DMLabel
1516d123abd9SMatthew G. Knepley . value  - The label value
1517d123abd9SMatthew G. Knepley - p      - A point with this value
1518d123abd9SMatthew G. Knepley 
1519d123abd9SMatthew G. Knepley   Output Parameter:
1520d123abd9SMatthew G. Knepley . index  - The index of this point in the stratum, or -1 if the point is not in the stratum or the stratum does not exist
1521d123abd9SMatthew G. Knepley 
1522d123abd9SMatthew G. Knepley   Level: intermediate
1523d123abd9SMatthew G. Knepley 
1524d123abd9SMatthew G. Knepley .seealso: DMLabelGetValueIndex(), DMLabelGetStratumIS(), DMLabelCreate()
1525d123abd9SMatthew G. Knepley @*/
1526d123abd9SMatthew G. Knepley PetscErrorCode DMLabelGetStratumPointIndex(DMLabel label, PetscInt value, PetscInt p, PetscInt *index)
1527d123abd9SMatthew G. Knepley {
1528d123abd9SMatthew G. Knepley   const PetscInt *indices;
1529d123abd9SMatthew G. Knepley   PetscInt        v;
1530d123abd9SMatthew G. Knepley   PetscErrorCode  ierr;
1531d123abd9SMatthew G. Knepley 
1532d123abd9SMatthew G. Knepley   PetscFunctionBegin;
1533d123abd9SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
1534d123abd9SMatthew G. Knepley   PetscValidPointer(index, 4);
1535d123abd9SMatthew G. Knepley   *index = -1;
1536d123abd9SMatthew G. Knepley   ierr = DMLabelLookupStratum(label, value, &v);CHKERRQ(ierr);
1537d123abd9SMatthew G. Knepley   if (v < 0) PetscFunctionReturn(0);
1538d123abd9SMatthew G. Knepley   ierr = DMLabelMakeValid_Private(label, v);CHKERRQ(ierr);
1539d123abd9SMatthew G. Knepley   ierr = ISGetIndices(label->points[v], &indices);CHKERRQ(ierr);
1540d123abd9SMatthew G. Knepley   ierr = PetscFindInt(p, label->stratumSizes[v], indices, index);CHKERRQ(ierr);
1541d123abd9SMatthew G. Knepley   ierr = ISRestoreIndices(label->points[v], &indices);CHKERRQ(ierr);
1542d123abd9SMatthew G. Knepley   PetscFunctionReturn(0);
1543d123abd9SMatthew G. Knepley }
1544d123abd9SMatthew G. Knepley 
1545d123abd9SMatthew G. Knepley /*@
154684f0b6dfSMatthew G. Knepley   DMLabelFilter - Remove all points outside of [start, end)
154784f0b6dfSMatthew G. Knepley 
15485b5e7992SMatthew G. Knepley   Not collective
15495b5e7992SMatthew G. Knepley 
155084f0b6dfSMatthew G. Knepley   Input Parameters:
155184f0b6dfSMatthew G. Knepley + label - the DMLabel
155222cd2cdaSVaclav Hapla . start - the first point kept
155322cd2cdaSVaclav Hapla - end - one more than the last point kept
155484f0b6dfSMatthew G. Knepley 
155584f0b6dfSMatthew G. Knepley   Level: intermediate
155684f0b6dfSMatthew G. Knepley 
155784f0b6dfSMatthew G. Knepley .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
155884f0b6dfSMatthew G. Knepley @*/
1559c58f1c22SToby Isaac PetscErrorCode DMLabelFilter(DMLabel label, PetscInt start, PetscInt end)
1560c58f1c22SToby Isaac {
1561c58f1c22SToby Isaac   PetscInt       v;
1562c58f1c22SToby Isaac   PetscErrorCode ierr;
1563c58f1c22SToby Isaac 
1564c58f1c22SToby Isaac   PetscFunctionBegin;
1565d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
15660c3c4a36SLisandro Dalcin   ierr = DMLabelDestroyIndex(label);CHKERRQ(ierr);
1567c58f1c22SToby Isaac   ierr = DMLabelMakeAllValid_Private(label);CHKERRQ(ierr);
1568c58f1c22SToby Isaac   for (v = 0; v < label->numStrata; ++v) {
15699106b633SVaclav Hapla     ierr = ISGeneralFilter(label->points[v], start, end);CHKERRQ(ierr);
1570c58f1c22SToby Isaac   }
1571c58f1c22SToby Isaac   ierr = DMLabelCreateIndex(label, start, end);CHKERRQ(ierr);
1572c58f1c22SToby Isaac   PetscFunctionReturn(0);
1573c58f1c22SToby Isaac }
1574c58f1c22SToby Isaac 
157584f0b6dfSMatthew G. Knepley /*@
157684f0b6dfSMatthew G. Knepley   DMLabelPermute - Create a new label with permuted points
157784f0b6dfSMatthew G. Knepley 
15785b5e7992SMatthew G. Knepley   Not collective
15795b5e7992SMatthew G. Knepley 
158084f0b6dfSMatthew G. Knepley   Input Parameters:
158184f0b6dfSMatthew G. Knepley + label - the DMLabel
158284f0b6dfSMatthew G. Knepley - permutation - the point permutation
158384f0b6dfSMatthew G. Knepley 
158484f0b6dfSMatthew G. Knepley   Output Parameter:
158584f0b6dfSMatthew G. Knepley . labelnew - the new label containing the permuted points
158684f0b6dfSMatthew G. Knepley 
158784f0b6dfSMatthew G. Knepley   Level: intermediate
158884f0b6dfSMatthew G. Knepley 
158984f0b6dfSMatthew G. Knepley .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
159084f0b6dfSMatthew G. Knepley @*/
1591c58f1c22SToby Isaac PetscErrorCode DMLabelPermute(DMLabel label, IS permutation, DMLabel *labelNew)
1592c58f1c22SToby Isaac {
1593c58f1c22SToby Isaac   const PetscInt *perm;
1594c58f1c22SToby Isaac   PetscInt        numValues, numPoints, v, q;
1595c58f1c22SToby Isaac   PetscErrorCode  ierr;
1596c58f1c22SToby Isaac 
1597c58f1c22SToby Isaac   PetscFunctionBegin;
1598d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
1599d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(permutation, IS_CLASSID, 2);
1600c58f1c22SToby Isaac   ierr = DMLabelMakeAllValid_Private(label);CHKERRQ(ierr);
1601c58f1c22SToby Isaac   ierr = DMLabelDuplicate(label, labelNew);CHKERRQ(ierr);
1602c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(*labelNew, &numValues);CHKERRQ(ierr);
1603c58f1c22SToby Isaac   ierr = ISGetLocalSize(permutation, &numPoints);CHKERRQ(ierr);
1604c58f1c22SToby Isaac   ierr = ISGetIndices(permutation, &perm);CHKERRQ(ierr);
1605c58f1c22SToby Isaac   for (v = 0; v < numValues; ++v) {
1606c58f1c22SToby Isaac     const PetscInt size   = (*labelNew)->stratumSizes[v];
1607ad8374ffSToby Isaac     const PetscInt *points;
1608ad8374ffSToby Isaac     PetscInt *pointsNew;
1609c58f1c22SToby Isaac 
1610ad8374ffSToby Isaac     ierr = ISGetIndices((*labelNew)->points[v],&points);CHKERRQ(ierr);
1611ad8374ffSToby Isaac     ierr = PetscMalloc1(size,&pointsNew);CHKERRQ(ierr);
1612c58f1c22SToby Isaac     for (q = 0; q < size; ++q) {
1613ad8374ffSToby Isaac       const PetscInt point = points[q];
1614c58f1c22SToby Isaac 
1615c58f1c22SToby 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);
1616ad8374ffSToby Isaac       pointsNew[q] = perm[point];
1617c58f1c22SToby Isaac     }
1618ad8374ffSToby Isaac     ierr = ISRestoreIndices((*labelNew)->points[v],&points);CHKERRQ(ierr);
1619ad8374ffSToby Isaac     ierr = PetscSortInt(size, pointsNew);CHKERRQ(ierr);
1620ad8374ffSToby Isaac     ierr = ISDestroy(&((*labelNew)->points[v]));CHKERRQ(ierr);
1621fa8e8ae5SToby Isaac     if (size > 0 && pointsNew[size - 1] == pointsNew[0] + size - 1) {
1622fa8e8ae5SToby Isaac       ierr = ISCreateStride(PETSC_COMM_SELF,size,pointsNew[0],1,&((*labelNew)->points[v]));CHKERRQ(ierr);
1623fa8e8ae5SToby Isaac       ierr = PetscFree(pointsNew);CHKERRQ(ierr);
1624fa8e8ae5SToby Isaac     } else {
1625ad8374ffSToby Isaac       ierr = ISCreateGeneral(PETSC_COMM_SELF,size,pointsNew,PETSC_OWN_POINTER,&((*labelNew)->points[v]));CHKERRQ(ierr);
1626fa8e8ae5SToby Isaac     }
1627ad8374ffSToby Isaac     ierr = PetscObjectSetName((PetscObject) ((*labelNew)->points[v]), "indices");CHKERRQ(ierr);
1628c58f1c22SToby Isaac   }
1629c58f1c22SToby Isaac   ierr = ISRestoreIndices(permutation, &perm);CHKERRQ(ierr);
1630c58f1c22SToby Isaac   if (label->bt) {
1631c58f1c22SToby Isaac     ierr = PetscBTDestroy(&label->bt);CHKERRQ(ierr);
1632c58f1c22SToby Isaac     ierr = DMLabelCreateIndex(label, label->pStart, label->pEnd);CHKERRQ(ierr);
1633c58f1c22SToby Isaac   }
1634c58f1c22SToby Isaac   PetscFunctionReturn(0);
1635c58f1c22SToby Isaac }
1636c58f1c22SToby Isaac 
163726c55118SMichael Lange PetscErrorCode DMLabelDistribute_Internal(DMLabel label, PetscSF sf, PetscSection *leafSection, PetscInt **leafStrata)
163826c55118SMichael Lange {
163926c55118SMichael Lange   MPI_Comm       comm;
164026c55118SMichael Lange   PetscInt       s, l, nroots, nleaves, dof, offset, size;
164126c55118SMichael Lange   PetscInt      *remoteOffsets, *rootStrata, *rootIdx;
164226c55118SMichael Lange   PetscSection   rootSection;
164326c55118SMichael Lange   PetscSF        labelSF;
164426c55118SMichael Lange   PetscErrorCode ierr;
164526c55118SMichael Lange 
164626c55118SMichael Lange   PetscFunctionBegin;
164726c55118SMichael Lange   if (label) {ierr = DMLabelMakeAllValid_Private(label);CHKERRQ(ierr);}
164826c55118SMichael Lange   ierr = PetscObjectGetComm((PetscObject)sf, &comm);CHKERRQ(ierr);
164926c55118SMichael Lange   /* Build a section of stratum values per point, generate the according SF
165026c55118SMichael Lange      and distribute point-wise stratum values to leaves. */
165126c55118SMichael Lange   ierr = PetscSFGetGraph(sf, &nroots, &nleaves, NULL, NULL);CHKERRQ(ierr);
165226c55118SMichael Lange   ierr = PetscSectionCreate(comm, &rootSection);CHKERRQ(ierr);
165326c55118SMichael Lange   ierr = PetscSectionSetChart(rootSection, 0, nroots);CHKERRQ(ierr);
165426c55118SMichael Lange   if (label) {
165526c55118SMichael Lange     for (s = 0; s < label->numStrata; ++s) {
1656ad8374ffSToby Isaac       const PetscInt *points;
1657ad8374ffSToby Isaac 
1658ad8374ffSToby Isaac       ierr = ISGetIndices(label->points[s], &points);CHKERRQ(ierr);
165926c55118SMichael Lange       for (l = 0; l < label->stratumSizes[s]; l++) {
1660ad8374ffSToby Isaac         ierr = PetscSectionGetDof(rootSection, points[l], &dof);CHKERRQ(ierr);
1661ad8374ffSToby Isaac         ierr = PetscSectionSetDof(rootSection, points[l], dof+1);CHKERRQ(ierr);
166226c55118SMichael Lange       }
1663ad8374ffSToby Isaac       ierr = ISRestoreIndices(label->points[s], &points);CHKERRQ(ierr);
166426c55118SMichael Lange     }
166526c55118SMichael Lange   }
166626c55118SMichael Lange   ierr = PetscSectionSetUp(rootSection);CHKERRQ(ierr);
166726c55118SMichael Lange   /* Create a point-wise array of stratum values */
166826c55118SMichael Lange   ierr = PetscSectionGetStorageSize(rootSection, &size);CHKERRQ(ierr);
166926c55118SMichael Lange   ierr = PetscMalloc1(size, &rootStrata);CHKERRQ(ierr);
167026c55118SMichael Lange   ierr = PetscCalloc1(nroots, &rootIdx);CHKERRQ(ierr);
167126c55118SMichael Lange   if (label) {
167226c55118SMichael Lange     for (s = 0; s < label->numStrata; ++s) {
1673ad8374ffSToby Isaac       const PetscInt *points;
1674ad8374ffSToby Isaac 
1675ad8374ffSToby Isaac       ierr = ISGetIndices(label->points[s], &points);CHKERRQ(ierr);
167626c55118SMichael Lange       for (l = 0; l < label->stratumSizes[s]; l++) {
1677ad8374ffSToby Isaac         const PetscInt p = points[l];
167826c55118SMichael Lange         ierr = PetscSectionGetOffset(rootSection, p, &offset);CHKERRQ(ierr);
167926c55118SMichael Lange         rootStrata[offset+rootIdx[p]++] = label->stratumValues[s];
168026c55118SMichael Lange       }
1681ad8374ffSToby Isaac       ierr = ISRestoreIndices(label->points[s], &points);CHKERRQ(ierr);
168226c55118SMichael Lange     }
168326c55118SMichael Lange   }
168426c55118SMichael Lange   /* Build SF that maps label points to remote processes */
168526c55118SMichael Lange   ierr = PetscSectionCreate(comm, leafSection);CHKERRQ(ierr);
168626c55118SMichael Lange   ierr = PetscSFDistributeSection(sf, rootSection, &remoteOffsets, *leafSection);CHKERRQ(ierr);
168726c55118SMichael Lange   ierr = PetscSFCreateSectionSF(sf, rootSection, remoteOffsets, *leafSection, &labelSF);CHKERRQ(ierr);
168826c55118SMichael Lange   ierr = PetscFree(remoteOffsets);CHKERRQ(ierr);
168926c55118SMichael Lange   /* Send the strata for each point over the derived SF */
169026c55118SMichael Lange   ierr = PetscSectionGetStorageSize(*leafSection, &size);CHKERRQ(ierr);
169126c55118SMichael Lange   ierr = PetscMalloc1(size, leafStrata);CHKERRQ(ierr);
1692ad227feaSJunchao Zhang   ierr = PetscSFBcastBegin(labelSF, MPIU_INT, rootStrata, *leafStrata,MPI_REPLACE);CHKERRQ(ierr);
1693ad227feaSJunchao Zhang   ierr = PetscSFBcastEnd(labelSF, MPIU_INT, rootStrata, *leafStrata,MPI_REPLACE);CHKERRQ(ierr);
169426c55118SMichael Lange   /* Clean up */
169526c55118SMichael Lange   ierr = PetscFree(rootStrata);CHKERRQ(ierr);
169626c55118SMichael Lange   ierr = PetscFree(rootIdx);CHKERRQ(ierr);
169726c55118SMichael Lange   ierr = PetscSectionDestroy(&rootSection);CHKERRQ(ierr);
169826c55118SMichael Lange   ierr = PetscSFDestroy(&labelSF);CHKERRQ(ierr);
169926c55118SMichael Lange   PetscFunctionReturn(0);
170026c55118SMichael Lange }
170126c55118SMichael Lange 
170284f0b6dfSMatthew G. Knepley /*@
170384f0b6dfSMatthew G. Knepley   DMLabelDistribute - Create a new label pushed forward over the PetscSF
170484f0b6dfSMatthew G. Knepley 
17055b5e7992SMatthew G. Knepley   Collective on sf
17065b5e7992SMatthew G. Knepley 
170784f0b6dfSMatthew G. Knepley   Input Parameters:
170884f0b6dfSMatthew G. Knepley + label - the DMLabel
170984f0b6dfSMatthew G. Knepley - sf    - the map from old to new distribution
171084f0b6dfSMatthew G. Knepley 
171184f0b6dfSMatthew G. Knepley   Output Parameter:
171284f0b6dfSMatthew G. Knepley . labelnew - the new redistributed label
171384f0b6dfSMatthew G. Knepley 
171484f0b6dfSMatthew G. Knepley   Level: intermediate
171584f0b6dfSMatthew G. Knepley 
171684f0b6dfSMatthew G. Knepley .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
171784f0b6dfSMatthew G. Knepley @*/
1718c58f1c22SToby Isaac PetscErrorCode DMLabelDistribute(DMLabel label, PetscSF sf, DMLabel *labelNew)
1719c58f1c22SToby Isaac {
1720c58f1c22SToby Isaac   MPI_Comm       comm;
172126c55118SMichael Lange   PetscSection   leafSection;
172226c55118SMichael Lange   PetscInt       p, pStart, pEnd, s, size, dof, offset, stratum;
172326c55118SMichael Lange   PetscInt      *leafStrata, *strataIdx;
1724ad8374ffSToby Isaac   PetscInt     **points;
1725d67d17b1SMatthew G. Knepley   const char    *lname = NULL;
1726c58f1c22SToby Isaac   char          *name;
1727c58f1c22SToby Isaac   PetscInt       nameSize;
1728e8f14785SLisandro Dalcin   PetscHSetI     stratumHash;
1729c58f1c22SToby Isaac   size_t         len = 0;
173026c55118SMichael Lange   PetscMPIInt    rank;
1731c58f1c22SToby Isaac   PetscErrorCode ierr;
1732c58f1c22SToby Isaac 
1733c58f1c22SToby Isaac   PetscFunctionBegin;
1734d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
1735f018e600SMatthew G. Knepley   if (label) {
1736f018e600SMatthew G. Knepley     PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
1737f018e600SMatthew G. Knepley     ierr = DMLabelMakeAllValid_Private(label);CHKERRQ(ierr);
1738f018e600SMatthew G. Knepley   }
1739c58f1c22SToby Isaac   ierr = PetscObjectGetComm((PetscObject)sf, &comm);CHKERRQ(ierr);
1740ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
1741c58f1c22SToby Isaac   /* Bcast name */
1742dd400576SPatrick Sanan   if (rank == 0) {
1743d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
1744d67d17b1SMatthew G. Knepley     ierr = PetscStrlen(lname, &len);CHKERRQ(ierr);
1745d67d17b1SMatthew G. Knepley   }
1746c58f1c22SToby Isaac   nameSize = len;
1747ffc4695bSBarry Smith   ierr = MPI_Bcast(&nameSize, 1, MPIU_INT, 0, comm);CHKERRMPI(ierr);
1748c58f1c22SToby Isaac   ierr = PetscMalloc1(nameSize+1, &name);CHKERRQ(ierr);
1749dd400576SPatrick Sanan   if (rank == 0) {ierr = PetscArraycpy(name, lname, nameSize+1);CHKERRQ(ierr);}
1750ffc4695bSBarry Smith   ierr = MPI_Bcast(name, nameSize+1, MPI_CHAR, 0, comm);CHKERRMPI(ierr);
1751d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, name, labelNew);CHKERRQ(ierr);
1752c58f1c22SToby Isaac   ierr = PetscFree(name);CHKERRQ(ierr);
175377d236dfSMichael Lange   /* Bcast defaultValue */
1754dd400576SPatrick Sanan   if (rank == 0) (*labelNew)->defaultValue = label->defaultValue;
1755ffc4695bSBarry Smith   ierr = MPI_Bcast(&(*labelNew)->defaultValue, 1, MPIU_INT, 0, comm);CHKERRMPI(ierr);
175626c55118SMichael Lange   /* Distribute stratum values over the SF and get the point mapping on the receiver */
175726c55118SMichael Lange   ierr = DMLabelDistribute_Internal(label, sf, &leafSection, &leafStrata);CHKERRQ(ierr);
17585cbdf6fcSMichael Lange   /* Determine received stratum values and initialise new label*/
1759e8f14785SLisandro Dalcin   ierr = PetscHSetICreate(&stratumHash);CHKERRQ(ierr);
176026c55118SMichael Lange   ierr = PetscSectionGetStorageSize(leafSection, &size);CHKERRQ(ierr);
1761e8f14785SLisandro Dalcin   for (p = 0; p < size; ++p) {ierr = PetscHSetIAdd(stratumHash, leafStrata[p]);CHKERRQ(ierr);}
1762e8f14785SLisandro Dalcin   ierr = PetscHSetIGetSize(stratumHash, &(*labelNew)->numStrata);CHKERRQ(ierr);
1763ad8374ffSToby Isaac   ierr = PetscMalloc1((*labelNew)->numStrata, &(*labelNew)->validIS);CHKERRQ(ierr);
1764ad8374ffSToby Isaac   for (s = 0; s < (*labelNew)->numStrata; ++s) (*labelNew)->validIS[s] = PETSC_TRUE;
17655cbdf6fcSMichael Lange   ierr = PetscMalloc1((*labelNew)->numStrata, &(*labelNew)->stratumValues);CHKERRQ(ierr);
17665cbdf6fcSMichael Lange   /* Turn leafStrata into indices rather than stratum values */
17675cbdf6fcSMichael Lange   offset = 0;
1768e8f14785SLisandro Dalcin   ierr = PetscHSetIGetElems(stratumHash, &offset, (*labelNew)->stratumValues);CHKERRQ(ierr);
1769a205f1fdSToby Isaac   ierr = PetscSortInt((*labelNew)->numStrata,(*labelNew)->stratumValues);CHKERRQ(ierr);
177090e9b2aeSLisandro Dalcin   for (s = 0; s < (*labelNew)->numStrata; ++s) {
177190e9b2aeSLisandro Dalcin     ierr = PetscHMapISet((*labelNew)->hmap, (*labelNew)->stratumValues[s], s);CHKERRQ(ierr);
177290e9b2aeSLisandro Dalcin   }
17735cbdf6fcSMichael Lange   for (p = 0; p < size; ++p) {
1774231b9e6fSMatthew G. Knepley     for (s = 0; s < (*labelNew)->numStrata; ++s) {
1775231b9e6fSMatthew G. Knepley       if (leafStrata[p] == (*labelNew)->stratumValues[s]) {leafStrata[p] = s; break;}
17765cbdf6fcSMichael Lange     }
17775cbdf6fcSMichael Lange   }
1778c58f1c22SToby Isaac   /* Rebuild the point strata on the receiver */
1779c58f1c22SToby Isaac   ierr = PetscCalloc1((*labelNew)->numStrata,&(*labelNew)->stratumSizes);CHKERRQ(ierr);
1780c58f1c22SToby Isaac   ierr = PetscSectionGetChart(leafSection, &pStart, &pEnd);CHKERRQ(ierr);
1781c58f1c22SToby Isaac   for (p=pStart; p<pEnd; p++) {
1782c58f1c22SToby Isaac     ierr = PetscSectionGetDof(leafSection, p, &dof);CHKERRQ(ierr);
1783c58f1c22SToby Isaac     ierr = PetscSectionGetOffset(leafSection, p, &offset);CHKERRQ(ierr);
1784c58f1c22SToby Isaac     for (s=0; s<dof; s++) {
1785c58f1c22SToby Isaac       (*labelNew)->stratumSizes[leafStrata[offset+s]]++;
1786c58f1c22SToby Isaac     }
1787c58f1c22SToby Isaac   }
1788c58f1c22SToby Isaac   ierr = PetscCalloc1((*labelNew)->numStrata,&(*labelNew)->ht);CHKERRQ(ierr);
1789c58f1c22SToby Isaac   ierr = PetscMalloc1((*labelNew)->numStrata,&(*labelNew)->points);CHKERRQ(ierr);
1790ad8374ffSToby Isaac   ierr = PetscMalloc1((*labelNew)->numStrata,&points);CHKERRQ(ierr);
1791c58f1c22SToby Isaac   for (s = 0; s < (*labelNew)->numStrata; ++s) {
1792e8f14785SLisandro Dalcin     ierr = PetscHSetICreate(&(*labelNew)->ht[s]);CHKERRQ(ierr);
1793ad8374ffSToby Isaac     ierr = PetscMalloc1((*labelNew)->stratumSizes[s], &(points[s]));CHKERRQ(ierr);
1794c58f1c22SToby Isaac   }
1795c58f1c22SToby Isaac   /* Insert points into new strata */
1796c58f1c22SToby Isaac   ierr = PetscCalloc1((*labelNew)->numStrata, &strataIdx);CHKERRQ(ierr);
1797c58f1c22SToby Isaac   ierr = PetscSectionGetChart(leafSection, &pStart, &pEnd);CHKERRQ(ierr);
1798c58f1c22SToby Isaac   for (p=pStart; p<pEnd; p++) {
1799c58f1c22SToby Isaac     ierr = PetscSectionGetDof(leafSection, p, &dof);CHKERRQ(ierr);
1800c58f1c22SToby Isaac     ierr = PetscSectionGetOffset(leafSection, p, &offset);CHKERRQ(ierr);
1801c58f1c22SToby Isaac     for (s=0; s<dof; s++) {
1802c58f1c22SToby Isaac       stratum = leafStrata[offset+s];
1803ad8374ffSToby Isaac       points[stratum][strataIdx[stratum]++] = p;
1804c58f1c22SToby Isaac     }
1805c58f1c22SToby Isaac   }
1806ad8374ffSToby Isaac   for (s = 0; s < (*labelNew)->numStrata; s++) {
1807ad8374ffSToby Isaac     ierr = ISCreateGeneral(PETSC_COMM_SELF,(*labelNew)->stratumSizes[s],&(points[s][0]),PETSC_OWN_POINTER,&((*labelNew)->points[s]));CHKERRQ(ierr);
1808ad8374ffSToby Isaac     ierr = PetscObjectSetName((PetscObject)((*labelNew)->points[s]),"indices");CHKERRQ(ierr);
1809ad8374ffSToby Isaac   }
1810ad8374ffSToby Isaac   ierr = PetscFree(points);CHKERRQ(ierr);
1811e8f14785SLisandro Dalcin   ierr = PetscHSetIDestroy(&stratumHash);CHKERRQ(ierr);
1812c58f1c22SToby Isaac   ierr = PetscFree(leafStrata);CHKERRQ(ierr);
1813c58f1c22SToby Isaac   ierr = PetscFree(strataIdx);CHKERRQ(ierr);
1814c58f1c22SToby Isaac   ierr = PetscSectionDestroy(&leafSection);CHKERRQ(ierr);
1815c58f1c22SToby Isaac   PetscFunctionReturn(0);
1816c58f1c22SToby Isaac }
1817c58f1c22SToby Isaac 
18187937d9ceSMichael Lange /*@
18197937d9ceSMichael Lange   DMLabelGather - Gather all label values from leafs into roots
18207937d9ceSMichael Lange 
18215b5e7992SMatthew G. Knepley   Collective on sf
18225b5e7992SMatthew G. Knepley 
18237937d9ceSMichael Lange   Input Parameters:
18247937d9ceSMichael Lange + label - the DMLabel
182584f0b6dfSMatthew G. Knepley - sf - the Star Forest point communication map
18267937d9ceSMichael Lange 
182784f0b6dfSMatthew G. Knepley   Output Parameters:
182884f0b6dfSMatthew G. Knepley . labelNew - the new DMLabel with localised leaf values
18297937d9ceSMichael Lange 
18307937d9ceSMichael Lange   Level: developer
18317937d9ceSMichael Lange 
18327937d9ceSMichael Lange   Note: This is the inverse operation to DMLabelDistribute.
18337937d9ceSMichael Lange 
18347937d9ceSMichael Lange .seealso: DMLabelDistribute()
18357937d9ceSMichael Lange @*/
18367937d9ceSMichael Lange PetscErrorCode DMLabelGather(DMLabel label, PetscSF sf, DMLabel *labelNew)
18377937d9ceSMichael Lange {
18387937d9ceSMichael Lange   MPI_Comm       comm;
18397937d9ceSMichael Lange   PetscSection   rootSection;
18407937d9ceSMichael Lange   PetscSF        sfLabel;
18417937d9ceSMichael Lange   PetscSFNode   *rootPoints, *leafPoints;
18427937d9ceSMichael Lange   PetscInt       p, s, d, nroots, nleaves, nmultiroots, idx, dof, offset;
18437937d9ceSMichael Lange   const PetscInt *rootDegree, *ilocal;
18447937d9ceSMichael Lange   PetscInt       *rootStrata;
1845d67d17b1SMatthew G. Knepley   const char    *lname;
18467937d9ceSMichael Lange   char          *name;
18477937d9ceSMichael Lange   PetscInt       nameSize;
18487937d9ceSMichael Lange   size_t         len = 0;
18499852e123SBarry Smith   PetscMPIInt    rank, size;
18507937d9ceSMichael Lange   PetscErrorCode ierr;
18517937d9ceSMichael Lange 
18527937d9ceSMichael Lange   PetscFunctionBegin;
1853d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
1854d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
18557937d9ceSMichael Lange   ierr = PetscObjectGetComm((PetscObject)sf, &comm);CHKERRQ(ierr);
1856ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
1857ffc4695bSBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRMPI(ierr);
18587937d9ceSMichael Lange   /* Bcast name */
1859dd400576SPatrick Sanan   if (rank == 0) {
1860d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
1861d67d17b1SMatthew G. Knepley     ierr = PetscStrlen(lname, &len);CHKERRQ(ierr);
1862d67d17b1SMatthew G. Knepley   }
18637937d9ceSMichael Lange   nameSize = len;
1864ffc4695bSBarry Smith   ierr = MPI_Bcast(&nameSize, 1, MPIU_INT, 0, comm);CHKERRMPI(ierr);
18657937d9ceSMichael Lange   ierr = PetscMalloc1(nameSize+1, &name);CHKERRQ(ierr);
1866dd400576SPatrick Sanan   if (rank == 0) {ierr = PetscArraycpy(name, lname, nameSize+1);CHKERRQ(ierr);}
1867ffc4695bSBarry Smith   ierr = MPI_Bcast(name, nameSize+1, MPI_CHAR, 0, comm);CHKERRMPI(ierr);
1868d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, name, labelNew);CHKERRQ(ierr);
18697937d9ceSMichael Lange   ierr = PetscFree(name);CHKERRQ(ierr);
18707937d9ceSMichael Lange   /* Gather rank/index pairs of leaves into local roots to build
18717937d9ceSMichael Lange      an inverse, multi-rooted SF. Note that this ignores local leaf
18727937d9ceSMichael Lange      indexing due to the use of the multiSF in PetscSFGather. */
18737937d9ceSMichael Lange   ierr = PetscSFGetGraph(sf, &nroots, &nleaves, &ilocal, NULL);CHKERRQ(ierr);
1874dc53bc9bSMatthew G. Knepley   ierr = PetscMalloc1(nroots, &leafPoints);CHKERRQ(ierr);
1875dc53bc9bSMatthew G. Knepley   for (p = 0; p < nroots; ++p) leafPoints[p].rank = leafPoints[p].index = -1;
18767937d9ceSMichael Lange   for (p = 0; p < nleaves; p++) {
18778212dd46SStefano Zampini     PetscInt ilp = ilocal ? ilocal[p] : p;
18788212dd46SStefano Zampini 
18798212dd46SStefano Zampini     leafPoints[ilp].index = ilp;
18808212dd46SStefano Zampini     leafPoints[ilp].rank  = rank;
18817937d9ceSMichael Lange   }
18827937d9ceSMichael Lange   ierr = PetscSFComputeDegreeBegin(sf, &rootDegree);CHKERRQ(ierr);
18837937d9ceSMichael Lange   ierr = PetscSFComputeDegreeEnd(sf, &rootDegree);CHKERRQ(ierr);
18847937d9ceSMichael Lange   for (p = 0, nmultiroots = 0; p < nroots; ++p) nmultiroots += rootDegree[p];
18857937d9ceSMichael Lange   ierr = PetscMalloc1(nmultiroots, &rootPoints);CHKERRQ(ierr);
18867937d9ceSMichael Lange   ierr = PetscSFGatherBegin(sf, MPIU_2INT, leafPoints, rootPoints);CHKERRQ(ierr);
18877937d9ceSMichael Lange   ierr = PetscSFGatherEnd(sf, MPIU_2INT, leafPoints, rootPoints);CHKERRQ(ierr);
18887937d9ceSMichael Lange   ierr = PetscSFCreate(comm,& sfLabel);CHKERRQ(ierr);
18897937d9ceSMichael Lange   ierr = PetscSFSetGraph(sfLabel, nroots, nmultiroots, NULL, PETSC_OWN_POINTER, rootPoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
18907937d9ceSMichael Lange   /* Migrate label over inverted SF to pull stratum values at leaves into roots. */
18917937d9ceSMichael Lange   ierr = DMLabelDistribute_Internal(label, sfLabel, &rootSection, &rootStrata);CHKERRQ(ierr);
18927937d9ceSMichael Lange   /* Rebuild the point strata on the receiver */
18937937d9ceSMichael Lange   for (p = 0, idx = 0; p < nroots; p++) {
18947937d9ceSMichael Lange     for (d = 0; d < rootDegree[p]; d++) {
18957937d9ceSMichael Lange       ierr = PetscSectionGetDof(rootSection, idx+d, &dof);CHKERRQ(ierr);
18967937d9ceSMichael Lange       ierr = PetscSectionGetOffset(rootSection, idx+d, &offset);CHKERRQ(ierr);
18977937d9ceSMichael Lange       for (s = 0; s < dof; s++) {ierr = DMLabelSetValue(*labelNew, p, rootStrata[offset+s]);CHKERRQ(ierr);}
18987937d9ceSMichael Lange     }
18997937d9ceSMichael Lange     idx += rootDegree[p];
19007937d9ceSMichael Lange   }
190177e0c0e7SMichael Lange   ierr = PetscFree(leafPoints);CHKERRQ(ierr);
190277e0c0e7SMichael Lange   ierr = PetscFree(rootStrata);CHKERRQ(ierr);
190377e0c0e7SMichael Lange   ierr = PetscSectionDestroy(&rootSection);CHKERRQ(ierr);
190477e0c0e7SMichael Lange   ierr = PetscSFDestroy(&sfLabel);CHKERRQ(ierr);
19057937d9ceSMichael Lange   PetscFunctionReturn(0);
19067937d9ceSMichael Lange }
19077937d9ceSMichael Lange 
190884f0b6dfSMatthew G. Knepley /*@
190984f0b6dfSMatthew G. Knepley   DMLabelConvertToSection - Make a PetscSection/IS pair that encodes the label
191084f0b6dfSMatthew G. Knepley 
19115b5e7992SMatthew G. Knepley   Not collective
19125b5e7992SMatthew G. Knepley 
191384f0b6dfSMatthew G. Knepley   Input Parameter:
191484f0b6dfSMatthew G. Knepley . label - the DMLabel
191584f0b6dfSMatthew G. Knepley 
191684f0b6dfSMatthew G. Knepley   Output Parameters:
191784f0b6dfSMatthew G. Knepley + section - the section giving offsets for each stratum
191884f0b6dfSMatthew G. Knepley - is - An IS containing all the label points
191984f0b6dfSMatthew G. Knepley 
192084f0b6dfSMatthew G. Knepley   Level: developer
192184f0b6dfSMatthew G. Knepley 
192284f0b6dfSMatthew G. Knepley .seealso: DMLabelDistribute()
192384f0b6dfSMatthew G. Knepley @*/
1924c58f1c22SToby Isaac PetscErrorCode DMLabelConvertToSection(DMLabel label, PetscSection *section, IS *is)
1925c58f1c22SToby Isaac {
1926c58f1c22SToby Isaac   IS              vIS;
1927c58f1c22SToby Isaac   const PetscInt *values;
1928c58f1c22SToby Isaac   PetscInt       *points;
1929c58f1c22SToby Isaac   PetscInt        nV, vS = 0, vE = 0, v, N;
1930c58f1c22SToby Isaac   PetscErrorCode  ierr;
1931c58f1c22SToby Isaac 
1932c58f1c22SToby Isaac   PetscFunctionBegin;
1933d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 1);
1934c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, &nV);CHKERRQ(ierr);
1935c58f1c22SToby Isaac   ierr = DMLabelGetValueIS(label, &vIS);CHKERRQ(ierr);
1936c58f1c22SToby Isaac   ierr = ISGetIndices(vIS, &values);CHKERRQ(ierr);
1937c58f1c22SToby Isaac   if (nV) {vS = values[0]; vE = values[0]+1;}
1938c58f1c22SToby Isaac   for (v = 1; v < nV; ++v) {
1939c58f1c22SToby Isaac     vS = PetscMin(vS, values[v]);
1940c58f1c22SToby Isaac     vE = PetscMax(vE, values[v]+1);
1941c58f1c22SToby Isaac   }
1942c58f1c22SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF, section);CHKERRQ(ierr);
1943c58f1c22SToby Isaac   ierr = PetscSectionSetChart(*section, vS, vE);CHKERRQ(ierr);
1944c58f1c22SToby Isaac   for (v = 0; v < nV; ++v) {
1945c58f1c22SToby Isaac     PetscInt n;
1946c58f1c22SToby Isaac 
1947c58f1c22SToby Isaac     ierr = DMLabelGetStratumSize(label, values[v], &n);CHKERRQ(ierr);
1948c58f1c22SToby Isaac     ierr = PetscSectionSetDof(*section, values[v], n);CHKERRQ(ierr);
1949c58f1c22SToby Isaac   }
1950c58f1c22SToby Isaac   ierr = PetscSectionSetUp(*section);CHKERRQ(ierr);
1951c58f1c22SToby Isaac   ierr = PetscSectionGetStorageSize(*section, &N);CHKERRQ(ierr);
1952c58f1c22SToby Isaac   ierr = PetscMalloc1(N, &points);CHKERRQ(ierr);
1953c58f1c22SToby Isaac   for (v = 0; v < nV; ++v) {
1954c58f1c22SToby Isaac     IS              is;
1955c58f1c22SToby Isaac     const PetscInt *spoints;
1956c58f1c22SToby Isaac     PetscInt        dof, off, p;
1957c58f1c22SToby Isaac 
1958c58f1c22SToby Isaac     ierr = PetscSectionGetDof(*section, values[v], &dof);CHKERRQ(ierr);
1959c58f1c22SToby Isaac     ierr = PetscSectionGetOffset(*section, values[v], &off);CHKERRQ(ierr);
1960c58f1c22SToby Isaac     ierr = DMLabelGetStratumIS(label, values[v], &is);CHKERRQ(ierr);
1961c58f1c22SToby Isaac     ierr = ISGetIndices(is, &spoints);CHKERRQ(ierr);
1962c58f1c22SToby Isaac     for (p = 0; p < dof; ++p) points[off+p] = spoints[p];
1963c58f1c22SToby Isaac     ierr = ISRestoreIndices(is, &spoints);CHKERRQ(ierr);
1964c58f1c22SToby Isaac     ierr = ISDestroy(&is);CHKERRQ(ierr);
1965c58f1c22SToby Isaac   }
1966c58f1c22SToby Isaac   ierr = ISRestoreIndices(vIS, &values);CHKERRQ(ierr);
1967c58f1c22SToby Isaac   ierr = ISDestroy(&vIS);CHKERRQ(ierr);
1968c58f1c22SToby Isaac   ierr = ISCreateGeneral(PETSC_COMM_SELF, N, points, PETSC_OWN_POINTER, is);CHKERRQ(ierr);
1969c58f1c22SToby Isaac   PetscFunctionReturn(0);
1970c58f1c22SToby Isaac }
1971c58f1c22SToby Isaac 
197284f0b6dfSMatthew G. Knepley /*@
1973c58f1c22SToby Isaac   PetscSectionCreateGlobalSectionLabel - Create a section describing the global field layout using
1974c58f1c22SToby Isaac   the local section and an SF describing the section point overlap.
1975c58f1c22SToby Isaac 
19765b5e7992SMatthew G. Knepley   Collective on sf
19775b5e7992SMatthew G. Knepley 
1978c58f1c22SToby Isaac   Input Parameters:
1979c58f1c22SToby Isaac   + s - The PetscSection for the local field layout
1980c58f1c22SToby Isaac   . sf - The SF describing parallel layout of the section points
1981c58f1c22SToby Isaac   . includeConstraints - By default this is PETSC_FALSE, meaning that the global field vector will not possess constrained dofs
1982c58f1c22SToby Isaac   . label - The label specifying the points
1983c58f1c22SToby Isaac   - labelValue - The label stratum specifying the points
1984c58f1c22SToby Isaac 
1985c58f1c22SToby Isaac   Output Parameter:
1986c58f1c22SToby Isaac   . gsection - The PetscSection for the global field layout
1987c58f1c22SToby Isaac 
1988c58f1c22SToby Isaac   Note: This gives negative sizes and offsets to points not owned by this process
1989c58f1c22SToby Isaac 
1990c58f1c22SToby Isaac   Level: developer
1991c58f1c22SToby Isaac 
1992c58f1c22SToby Isaac .seealso: PetscSectionCreate()
1993c58f1c22SToby Isaac @*/
1994c58f1c22SToby Isaac PetscErrorCode PetscSectionCreateGlobalSectionLabel(PetscSection s, PetscSF sf, PetscBool includeConstraints, DMLabel label, PetscInt labelValue, PetscSection *gsection)
1995c58f1c22SToby Isaac {
1996c58f1c22SToby Isaac   PetscInt      *neg = NULL, *tmpOff = NULL;
1997c58f1c22SToby Isaac   PetscInt       pStart, pEnd, p, dof, cdof, off, globalOff = 0, nroots;
1998c58f1c22SToby Isaac   PetscErrorCode ierr;
1999c58f1c22SToby Isaac 
2000c58f1c22SToby Isaac   PetscFunctionBegin;
2001d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(s, PETSC_SECTION_CLASSID, 1);
2002d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
2003d67d17b1SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
2004c58f1c22SToby Isaac   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) s), gsection);CHKERRQ(ierr);
2005c58f1c22SToby Isaac   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
2006c58f1c22SToby Isaac   ierr = PetscSectionSetChart(*gsection, pStart, pEnd);CHKERRQ(ierr);
2007c58f1c22SToby Isaac   ierr = PetscSFGetGraph(sf, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
2008c58f1c22SToby Isaac   if (nroots >= 0) {
2009c58f1c22SToby Isaac     if (nroots < pEnd-pStart) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "PetscSF nroots %d < %d section size", nroots, pEnd-pStart);
2010c58f1c22SToby Isaac     ierr = PetscCalloc1(nroots, &neg);CHKERRQ(ierr);
2011c58f1c22SToby Isaac     if (nroots > pEnd-pStart) {
2012c58f1c22SToby Isaac       ierr = PetscCalloc1(nroots, &tmpOff);CHKERRQ(ierr);
2013c58f1c22SToby Isaac     } else {
2014c58f1c22SToby Isaac       tmpOff = &(*gsection)->atlasDof[-pStart];
2015c58f1c22SToby Isaac     }
2016c58f1c22SToby Isaac   }
2017c58f1c22SToby Isaac   /* Mark ghost points with negative dof */
2018c58f1c22SToby Isaac   for (p = pStart; p < pEnd; ++p) {
2019c58f1c22SToby Isaac     PetscInt value;
2020c58f1c22SToby Isaac 
2021c58f1c22SToby Isaac     ierr = DMLabelGetValue(label, p, &value);CHKERRQ(ierr);
2022c58f1c22SToby Isaac     if (value != labelValue) continue;
2023c58f1c22SToby Isaac     ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
2024c58f1c22SToby Isaac     ierr = PetscSectionSetDof(*gsection, p, dof);CHKERRQ(ierr);
2025c58f1c22SToby Isaac     ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2026c58f1c22SToby Isaac     if (!includeConstraints && cdof > 0) {ierr = PetscSectionSetConstraintDof(*gsection, p, cdof);CHKERRQ(ierr);}
2027c58f1c22SToby Isaac     if (neg) neg[p] = -(dof+1);
2028c58f1c22SToby Isaac   }
2029c58f1c22SToby Isaac   ierr = PetscSectionSetUpBC(*gsection);CHKERRQ(ierr);
2030c58f1c22SToby Isaac   if (nroots >= 0) {
2031ad227feaSJunchao Zhang     ierr = PetscSFBcastBegin(sf, MPIU_INT, neg, tmpOff,MPI_REPLACE);CHKERRQ(ierr);
2032ad227feaSJunchao Zhang     ierr = PetscSFBcastEnd(sf, MPIU_INT, neg, tmpOff,MPI_REPLACE);CHKERRQ(ierr);
2033c58f1c22SToby Isaac     if (nroots > pEnd-pStart) {
2034c58f1c22SToby Isaac       for (p = pStart; p < pEnd; ++p) {if (tmpOff[p] < 0) (*gsection)->atlasDof[p-pStart] = tmpOff[p];}
2035c58f1c22SToby Isaac     }
2036c58f1c22SToby Isaac   }
2037c58f1c22SToby Isaac   /* Calculate new sizes, get proccess offset, and calculate point offsets */
2038c58f1c22SToby Isaac   for (p = 0, off = 0; p < pEnd-pStart; ++p) {
2039c58f1c22SToby Isaac     cdof = (!includeConstraints && s->bc) ? s->bc->atlasDof[p] : 0;
2040c58f1c22SToby Isaac     (*gsection)->atlasOff[p] = off;
2041c58f1c22SToby Isaac     off += (*gsection)->atlasDof[p] > 0 ? (*gsection)->atlasDof[p]-cdof : 0;
2042c58f1c22SToby Isaac   }
204355b25c41SPierre Jolivet   ierr       = MPI_Scan(&off, &globalOff, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject) s));CHKERRMPI(ierr);
2044c58f1c22SToby Isaac   globalOff -= off;
2045c58f1c22SToby Isaac   for (p = 0, off = 0; p < pEnd-pStart; ++p) {
2046c58f1c22SToby Isaac     (*gsection)->atlasOff[p] += globalOff;
2047c58f1c22SToby Isaac     if (neg) neg[p] = -((*gsection)->atlasOff[p]+1);
2048c58f1c22SToby Isaac   }
2049c58f1c22SToby Isaac   /* Put in negative offsets for ghost points */
2050c58f1c22SToby Isaac   if (nroots >= 0) {
2051ad227feaSJunchao Zhang     ierr = PetscSFBcastBegin(sf, MPIU_INT, neg, tmpOff,MPI_REPLACE);CHKERRQ(ierr);
2052ad227feaSJunchao Zhang     ierr = PetscSFBcastEnd(sf, MPIU_INT, neg, tmpOff,MPI_REPLACE);CHKERRQ(ierr);
2053c58f1c22SToby Isaac     if (nroots > pEnd-pStart) {
2054c58f1c22SToby Isaac       for (p = pStart; p < pEnd; ++p) {if (tmpOff[p] < 0) (*gsection)->atlasOff[p-pStart] = tmpOff[p];}
2055c58f1c22SToby Isaac     }
2056c58f1c22SToby Isaac   }
2057c58f1c22SToby Isaac   if (nroots >= 0 && nroots > pEnd-pStart) {ierr = PetscFree(tmpOff);CHKERRQ(ierr);}
2058c58f1c22SToby Isaac   ierr = PetscFree(neg);CHKERRQ(ierr);
2059c58f1c22SToby Isaac   PetscFunctionReturn(0);
2060c58f1c22SToby Isaac }
2061c58f1c22SToby Isaac 
20625fdea053SToby Isaac typedef struct _n_PetscSectionSym_Label
20635fdea053SToby Isaac {
20645fdea053SToby Isaac   DMLabel           label;
20655fdea053SToby Isaac   PetscCopyMode     *modes;
20665fdea053SToby Isaac   PetscInt          *sizes;
20675fdea053SToby Isaac   const PetscInt    ***perms;
20685fdea053SToby Isaac   const PetscScalar ***rots;
20695fdea053SToby Isaac   PetscInt          (*minMaxOrients)[2];
20705fdea053SToby Isaac   PetscInt          numStrata; /* numStrata is only increasing, functions as a state */
20715fdea053SToby Isaac } PetscSectionSym_Label;
20725fdea053SToby Isaac 
20735fdea053SToby Isaac static PetscErrorCode PetscSectionSymLabelReset(PetscSectionSym sym)
20745fdea053SToby Isaac {
20755fdea053SToby Isaac   PetscInt              i, j;
20765fdea053SToby Isaac   PetscSectionSym_Label *sl = (PetscSectionSym_Label *) sym->data;
20775fdea053SToby Isaac   PetscErrorCode        ierr;
20785fdea053SToby Isaac 
20795fdea053SToby Isaac   PetscFunctionBegin;
20805fdea053SToby Isaac   for (i = 0; i <= sl->numStrata; i++) {
20815fdea053SToby Isaac     if (sl->modes[i] == PETSC_OWN_POINTER || sl->modes[i] == PETSC_COPY_VALUES) {
20825fdea053SToby Isaac       for (j = sl->minMaxOrients[i][0]; j < sl->minMaxOrients[i][1]; j++) {
20835fdea053SToby Isaac         if (sl->perms[i]) {ierr = PetscFree(sl->perms[i][j]);CHKERRQ(ierr);}
20845fdea053SToby Isaac         if (sl->rots[i]) {ierr = PetscFree(sl->rots[i][j]);CHKERRQ(ierr);}
20855fdea053SToby Isaac       }
20865fdea053SToby Isaac       if (sl->perms[i]) {
20875fdea053SToby Isaac         const PetscInt **perms = &sl->perms[i][sl->minMaxOrients[i][0]];
20885fdea053SToby Isaac 
20895fdea053SToby Isaac         ierr = PetscFree(perms);CHKERRQ(ierr);
20905fdea053SToby Isaac       }
20915fdea053SToby Isaac       if (sl->rots[i]) {
20925fdea053SToby Isaac         const PetscScalar **rots = &sl->rots[i][sl->minMaxOrients[i][0]];
20935fdea053SToby Isaac 
20945fdea053SToby Isaac         ierr = PetscFree(rots);CHKERRQ(ierr);
20955fdea053SToby Isaac       }
20965fdea053SToby Isaac     }
20975fdea053SToby Isaac   }
20985fdea053SToby Isaac   ierr = PetscFree5(sl->modes,sl->sizes,sl->perms,sl->rots,sl->minMaxOrients);CHKERRQ(ierr);
20995fdea053SToby Isaac   ierr = DMLabelDestroy(&sl->label);CHKERRQ(ierr);
21005fdea053SToby Isaac   sl->numStrata = 0;
21015fdea053SToby Isaac   PetscFunctionReturn(0);
21025fdea053SToby Isaac }
21035fdea053SToby Isaac 
21045fdea053SToby Isaac static PetscErrorCode PetscSectionSymDestroy_Label(PetscSectionSym sym)
21055fdea053SToby Isaac {
21065fdea053SToby Isaac   PetscErrorCode ierr;
21075fdea053SToby Isaac 
21085fdea053SToby Isaac   PetscFunctionBegin;
21095fdea053SToby Isaac   ierr = PetscSectionSymLabelReset(sym);CHKERRQ(ierr);
21105fdea053SToby Isaac   ierr = PetscFree(sym->data);CHKERRQ(ierr);
21115fdea053SToby Isaac   PetscFunctionReturn(0);
21125fdea053SToby Isaac }
21135fdea053SToby Isaac 
21145fdea053SToby Isaac static PetscErrorCode PetscSectionSymView_Label(PetscSectionSym sym, PetscViewer viewer)
21155fdea053SToby Isaac {
21165fdea053SToby Isaac   PetscSectionSym_Label *sl = (PetscSectionSym_Label *) sym->data;
21175fdea053SToby Isaac   PetscBool             isAscii;
21185fdea053SToby Isaac   DMLabel               label = sl->label;
2119d67d17b1SMatthew G. Knepley   const char           *name;
21205fdea053SToby Isaac   PetscErrorCode        ierr;
21215fdea053SToby Isaac 
21225fdea053SToby Isaac   PetscFunctionBegin;
21235fdea053SToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &isAscii);CHKERRQ(ierr);
21245fdea053SToby Isaac   if (isAscii) {
21255fdea053SToby Isaac     PetscInt          i, j, k;
21265fdea053SToby Isaac     PetscViewerFormat format;
21275fdea053SToby Isaac 
21285fdea053SToby Isaac     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
21295fdea053SToby Isaac     if (label) {
21305fdea053SToby Isaac       ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
21315fdea053SToby Isaac       if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
21325fdea053SToby Isaac         ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
21335fdea053SToby Isaac         ierr = DMLabelView(label, viewer);CHKERRQ(ierr);
21345fdea053SToby Isaac         ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
21355fdea053SToby Isaac       } else {
2136d67d17b1SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) sl->label, &name);CHKERRQ(ierr);
2137d67d17b1SMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer,"  Label '%s'\n",name);CHKERRQ(ierr);
21385fdea053SToby Isaac       }
21395fdea053SToby Isaac     } else {
21405fdea053SToby Isaac       ierr = PetscViewerASCIIPrintf(viewer, "No label given\n");CHKERRQ(ierr);
21415fdea053SToby Isaac     }
21425fdea053SToby Isaac     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
21435fdea053SToby Isaac     for (i = 0; i <= sl->numStrata; i++) {
21445fdea053SToby Isaac       PetscInt value = i < sl->numStrata ? label->stratumValues[i] : label->defaultValue;
21455fdea053SToby Isaac 
21465fdea053SToby Isaac       if (!(sl->perms[i] || sl->rots[i])) {
21475fdea053SToby Isaac         ierr = PetscViewerASCIIPrintf(viewer, "Symmetry for stratum value %D (%D dofs per point): no symmetries\n", value, sl->sizes[i]);CHKERRQ(ierr);
21485fdea053SToby Isaac       } else {
21495fdea053SToby Isaac       ierr = PetscViewerASCIIPrintf(viewer, "Symmetry for stratum value %D (%D dofs per point):\n", value, sl->sizes[i]);CHKERRQ(ierr);
21505fdea053SToby Isaac         ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
21515fdea053SToby Isaac         ierr = PetscViewerASCIIPrintf(viewer, "Orientation range: [%D, %D)\n", sl->minMaxOrients[i][0], sl->minMaxOrients[i][1]);CHKERRQ(ierr);
21525fdea053SToby Isaac         if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
21535fdea053SToby Isaac           ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
21545fdea053SToby Isaac           for (j = sl->minMaxOrients[i][0]; j < sl->minMaxOrients[i][1]; j++) {
21555fdea053SToby Isaac             if (!((sl->perms[i] && sl->perms[i][j]) || (sl->rots[i] && sl->rots[i][j]))) {
21565fdea053SToby Isaac               ierr = PetscViewerASCIIPrintf(viewer, "Orientation %D: identity\n",j);CHKERRQ(ierr);
21575fdea053SToby Isaac             } else {
21585fdea053SToby Isaac               PetscInt tab;
21595fdea053SToby Isaac 
21605fdea053SToby Isaac               ierr = PetscViewerASCIIPrintf(viewer, "Orientation %D:\n",j);CHKERRQ(ierr);
21615fdea053SToby Isaac               ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
21625fdea053SToby Isaac               ierr = PetscViewerASCIIGetTab(viewer,&tab);CHKERRQ(ierr);
21635fdea053SToby Isaac               if (sl->perms[i] && sl->perms[i][j]) {
21645fdea053SToby Isaac                 ierr = PetscViewerASCIIPrintf(viewer,"Permutation:");CHKERRQ(ierr);
21655fdea053SToby Isaac                 ierr = PetscViewerASCIISetTab(viewer,0);CHKERRQ(ierr);
21665fdea053SToby Isaac                 for (k = 0; k < sl->sizes[i]; k++) {ierr = PetscViewerASCIIPrintf(viewer," %D",sl->perms[i][j][k]);CHKERRQ(ierr);}
21675fdea053SToby Isaac                 ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
21685fdea053SToby Isaac                 ierr = PetscViewerASCIISetTab(viewer,tab);CHKERRQ(ierr);
21695fdea053SToby Isaac               }
21705fdea053SToby Isaac               if (sl->rots[i] && sl->rots[i][j]) {
21715fdea053SToby Isaac                 ierr = PetscViewerASCIIPrintf(viewer,"Rotations:  ");CHKERRQ(ierr);
21725fdea053SToby Isaac                 ierr = PetscViewerASCIISetTab(viewer,0);CHKERRQ(ierr);
21735fdea053SToby Isaac #if defined(PETSC_USE_COMPLEX)
21745fdea053SToby 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);}
21755fdea053SToby Isaac #else
21765fdea053SToby Isaac                 for (k = 0; k < sl->sizes[i]; k++) {ierr = PetscViewerASCIIPrintf(viewer," %+f",sl->rots[i][j][k]);CHKERRQ(ierr);}
21775fdea053SToby Isaac #endif
21785fdea053SToby Isaac                 ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
21795fdea053SToby Isaac                 ierr = PetscViewerASCIISetTab(viewer,tab);CHKERRQ(ierr);
21805fdea053SToby Isaac               }
21815fdea053SToby Isaac               ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
21825fdea053SToby Isaac             }
21835fdea053SToby Isaac           }
21845fdea053SToby Isaac           ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
21855fdea053SToby Isaac         }
21865fdea053SToby Isaac         ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
21875fdea053SToby Isaac       }
21885fdea053SToby Isaac     }
21895fdea053SToby Isaac     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
21905fdea053SToby Isaac   }
21915fdea053SToby Isaac   PetscFunctionReturn(0);
21925fdea053SToby Isaac }
21935fdea053SToby Isaac 
21945fdea053SToby Isaac /*@
21955fdea053SToby Isaac   PetscSectionSymLabelSetLabel - set the label whose strata will define the points that receive symmetries
21965fdea053SToby Isaac 
21975fdea053SToby Isaac   Logically collective on sym
21985fdea053SToby Isaac 
21995fdea053SToby Isaac   Input parameters:
22005fdea053SToby Isaac + sym - the section symmetries
22015fdea053SToby Isaac - label - the DMLabel describing the types of points
22025fdea053SToby Isaac 
22035fdea053SToby Isaac   Level: developer:
22045fdea053SToby Isaac 
22055fdea053SToby Isaac .seealso: PetscSectionSymLabelSetStratum(), PetscSectionSymCreateLabel(), PetscSectionGetPointSyms()
22065fdea053SToby Isaac @*/
22075fdea053SToby Isaac PetscErrorCode PetscSectionSymLabelSetLabel(PetscSectionSym sym, DMLabel label)
22085fdea053SToby Isaac {
22095fdea053SToby Isaac   PetscSectionSym_Label *sl;
22105fdea053SToby Isaac   PetscErrorCode        ierr;
22115fdea053SToby Isaac 
22125fdea053SToby Isaac   PetscFunctionBegin;
22135fdea053SToby Isaac   PetscValidHeaderSpecific(sym,PETSC_SECTION_SYM_CLASSID,1);
22145fdea053SToby Isaac   sl = (PetscSectionSym_Label *) sym->data;
22155fdea053SToby Isaac   if (sl->label && sl->label != label) {ierr = PetscSectionSymLabelReset(sym);CHKERRQ(ierr);}
22165fdea053SToby Isaac   if (label) {
22175fdea053SToby Isaac     sl->label = label;
2218d67d17b1SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
22195fdea053SToby Isaac     ierr = DMLabelGetNumValues(label,&sl->numStrata);CHKERRQ(ierr);
22201a834cf9SToby 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);
22211a834cf9SToby Isaac     ierr = PetscMemzero((void *) sl->modes,(sl->numStrata+1)*sizeof(PetscCopyMode));CHKERRQ(ierr);
22221a834cf9SToby Isaac     ierr = PetscMemzero((void *) sl->sizes,(sl->numStrata+1)*sizeof(PetscInt));CHKERRQ(ierr);
22231a834cf9SToby Isaac     ierr = PetscMemzero((void *) sl->perms,(sl->numStrata+1)*sizeof(const PetscInt **));CHKERRQ(ierr);
22241a834cf9SToby Isaac     ierr = PetscMemzero((void *) sl->rots,(sl->numStrata+1)*sizeof(const PetscScalar **));CHKERRQ(ierr);
22251a834cf9SToby Isaac     ierr = PetscMemzero((void *) sl->minMaxOrients,(sl->numStrata+1)*sizeof(PetscInt[2]));CHKERRQ(ierr);
22265fdea053SToby Isaac   }
22275fdea053SToby Isaac   PetscFunctionReturn(0);
22285fdea053SToby Isaac }
22295fdea053SToby Isaac 
22305fdea053SToby Isaac /*@C
22315fdea053SToby Isaac   PetscSectionSymLabelSetStratum - set the symmetries for the orientations of a stratum
22325fdea053SToby Isaac 
22335b5e7992SMatthew G. Knepley   Logically collective on sym
22345fdea053SToby Isaac 
22355fdea053SToby Isaac   InputParameters:
22365b5e7992SMatthew G. Knepley + sym       - the section symmetries
22375fdea053SToby Isaac . stratum   - the stratum value in the label that we are assigning symmetries for
22385fdea053SToby Isaac . size      - the number of dofs for points in the stratum of the label
22395fdea053SToby Isaac . minOrient - the smallest orientation for a point in this stratum
22405fdea053SToby Isaac . maxOrient - one greater than the largest orientation for a ppoint in this stratum (i.e., orientations are in the range [minOrient, maxOrient))
22415fdea053SToby Isaac . mode      - how sym should copy the perms and rots arrays
22425fdea053SToby Isaac . perms     - NULL if there are no permutations, or (maxOrient - minOrient) permutations, one for each orientation.  A NULL permutation is the identity
2243a2b725a8SWilliam 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
22445fdea053SToby Isaac 
22455fdea053SToby Isaac   Level: developer
22465fdea053SToby Isaac 
22475fdea053SToby Isaac .seealso: PetscSectionSymCreate(), PetscSectionSetSym(), PetscSectionGetPointSyms(), PetscSectionSymCreateLabel()
22485fdea053SToby Isaac @*/
22495fdea053SToby Isaac PetscErrorCode PetscSectionSymLabelSetStratum(PetscSectionSym sym, PetscInt stratum, PetscInt size, PetscInt minOrient, PetscInt maxOrient, PetscCopyMode mode, const PetscInt **perms, const PetscScalar **rots)
22505fdea053SToby Isaac {
22515fdea053SToby Isaac   PetscSectionSym_Label *sl;
2252d67d17b1SMatthew G. Knepley   const char            *name;
2253d67d17b1SMatthew G. Knepley   PetscInt               i, j, k;
22545fdea053SToby Isaac   PetscErrorCode         ierr;
22555fdea053SToby Isaac 
22565fdea053SToby Isaac   PetscFunctionBegin;
22575fdea053SToby Isaac   PetscValidHeaderSpecific(sym,PETSC_SECTION_SYM_CLASSID,1);
22585fdea053SToby Isaac   sl = (PetscSectionSym_Label *) sym->data;
22595fdea053SToby Isaac   if (!sl->label) SETERRQ(PetscObjectComm((PetscObject)sym),PETSC_ERR_ARG_WRONGSTATE,"No label set yet");
22605fdea053SToby Isaac   for (i = 0; i <= sl->numStrata; i++) {
22615fdea053SToby Isaac     PetscInt value = (i < sl->numStrata) ? sl->label->stratumValues[i] : sl->label->defaultValue;
22625fdea053SToby Isaac 
22635fdea053SToby Isaac     if (stratum == value) break;
22645fdea053SToby Isaac   }
2265d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) sl->label, &name);CHKERRQ(ierr);
2266d67d17b1SMatthew G. Knepley   if (i > sl->numStrata) SETERRQ2(PetscObjectComm((PetscObject)sym),PETSC_ERR_ARG_OUTOFRANGE,"Stratum %D not found in label %s\n",stratum,name);
22675fdea053SToby Isaac   sl->sizes[i] = size;
22685fdea053SToby Isaac   sl->modes[i] = mode;
22695fdea053SToby Isaac   sl->minMaxOrients[i][0] = minOrient;
22705fdea053SToby Isaac   sl->minMaxOrients[i][1] = maxOrient;
22715fdea053SToby Isaac   if (mode == PETSC_COPY_VALUES) {
22725fdea053SToby Isaac     if (perms) {
22735fdea053SToby Isaac       PetscInt    **ownPerms;
22745fdea053SToby Isaac 
22755fdea053SToby Isaac       ierr = PetscCalloc1(maxOrient - minOrient,&ownPerms);CHKERRQ(ierr);
22765fdea053SToby Isaac       for (j = 0; j < maxOrient-minOrient; j++) {
22775fdea053SToby Isaac         if (perms[j]) {
22785fdea053SToby Isaac           ierr = PetscMalloc1(size,&ownPerms[j]);CHKERRQ(ierr);
22795fdea053SToby Isaac           for (k = 0; k < size; k++) {ownPerms[j][k] = perms[j][k];}
22805fdea053SToby Isaac         }
22815fdea053SToby Isaac       }
22825fdea053SToby Isaac       sl->perms[i] = (const PetscInt **) &ownPerms[-minOrient];
22835fdea053SToby Isaac     }
22845fdea053SToby Isaac     if (rots) {
22855fdea053SToby Isaac       PetscScalar **ownRots;
22865fdea053SToby Isaac 
22875fdea053SToby Isaac       ierr = PetscCalloc1(maxOrient - minOrient,&ownRots);CHKERRQ(ierr);
22885fdea053SToby Isaac       for (j = 0; j < maxOrient-minOrient; j++) {
22895fdea053SToby Isaac         if (rots[j]) {
22905fdea053SToby Isaac           ierr = PetscMalloc1(size,&ownRots[j]);CHKERRQ(ierr);
22915fdea053SToby Isaac           for (k = 0; k < size; k++) {ownRots[j][k] = rots[j][k];}
22925fdea053SToby Isaac         }
22935fdea053SToby Isaac       }
22945fdea053SToby Isaac       sl->rots[i] = (const PetscScalar **) &ownRots[-minOrient];
22955fdea053SToby Isaac     }
22965fdea053SToby Isaac   } else {
22975fdea053SToby Isaac     sl->perms[i] = perms ? &perms[-minOrient] : NULL;
22985fdea053SToby Isaac     sl->rots[i]  = rots ? &rots[-minOrient] : NULL;
22995fdea053SToby Isaac   }
23005fdea053SToby Isaac   PetscFunctionReturn(0);
23015fdea053SToby Isaac }
23025fdea053SToby Isaac 
23035fdea053SToby Isaac static PetscErrorCode PetscSectionSymGetPoints_Label(PetscSectionSym sym, PetscSection section, PetscInt numPoints, const PetscInt *points, const PetscInt **perms, const PetscScalar **rots)
23045fdea053SToby Isaac {
23055fdea053SToby Isaac   PetscInt              i, j, numStrata;
23065fdea053SToby Isaac   PetscSectionSym_Label *sl;
23075fdea053SToby Isaac   DMLabel               label;
23085fdea053SToby Isaac   PetscErrorCode        ierr;
23095fdea053SToby Isaac 
23105fdea053SToby Isaac   PetscFunctionBegin;
23115fdea053SToby Isaac   sl = (PetscSectionSym_Label *) sym->data;
23125fdea053SToby Isaac   numStrata = sl->numStrata;
23135fdea053SToby Isaac   label     = sl->label;
23145fdea053SToby Isaac   for (i = 0; i < numPoints; i++) {
23155fdea053SToby Isaac     PetscInt point = points[2*i];
23165fdea053SToby Isaac     PetscInt ornt  = points[2*i+1];
23175fdea053SToby Isaac 
23185fdea053SToby Isaac     for (j = 0; j < numStrata; j++) {
23195fdea053SToby Isaac       if (label->validIS[j]) {
23205fdea053SToby Isaac         PetscInt k;
23215fdea053SToby Isaac 
23225fdea053SToby Isaac         ierr = ISLocate(label->points[j],point,&k);CHKERRQ(ierr);
23235fdea053SToby Isaac         if (k >= 0) break;
23245fdea053SToby Isaac       } else {
23255fdea053SToby Isaac         PetscBool has;
23265fdea053SToby Isaac 
2327b9907514SLisandro Dalcin         ierr = PetscHSetIHas(label->ht[j], point, &has);CHKERRQ(ierr);
23285fdea053SToby Isaac         if (has) break;
23295fdea053SToby Isaac       }
23305fdea053SToby Isaac     }
23315fdea053SToby 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);
23325fdea053SToby Isaac     if (perms) {perms[i] = sl->perms[j] ? sl->perms[j][ornt] : NULL;}
23335fdea053SToby Isaac     if (rots) {rots[i]  = sl->rots[j] ? sl->rots[j][ornt] : NULL;}
23345fdea053SToby Isaac   }
23355fdea053SToby Isaac   PetscFunctionReturn(0);
23365fdea053SToby Isaac }
23375fdea053SToby Isaac 
23385fdea053SToby Isaac PetscErrorCode PetscSectionSymCreate_Label(PetscSectionSym sym)
23395fdea053SToby Isaac {
23405fdea053SToby Isaac   PetscSectionSym_Label *sl;
23415fdea053SToby Isaac   PetscErrorCode        ierr;
23425fdea053SToby Isaac 
23435fdea053SToby Isaac   PetscFunctionBegin;
23445fdea053SToby Isaac   ierr = PetscNewLog(sym,&sl);CHKERRQ(ierr);
23455fdea053SToby Isaac   sym->ops->getpoints = PetscSectionSymGetPoints_Label;
23465fdea053SToby Isaac   sym->ops->view      = PetscSectionSymView_Label;
23475fdea053SToby Isaac   sym->ops->destroy   = PetscSectionSymDestroy_Label;
23485fdea053SToby Isaac   sym->data           = (void *) sl;
23495fdea053SToby Isaac   PetscFunctionReturn(0);
23505fdea053SToby Isaac }
23515fdea053SToby Isaac 
23525fdea053SToby Isaac /*@
23535fdea053SToby Isaac   PetscSectionSymCreateLabel - Create a section symmetry that assigns one symmetry to each stratum of a label
23545fdea053SToby Isaac 
23555fdea053SToby Isaac   Collective
23565fdea053SToby Isaac 
23575fdea053SToby Isaac   Input Parameters:
23585fdea053SToby Isaac + comm - the MPI communicator for the new symmetry
23595fdea053SToby Isaac - label - the label defining the strata
23605fdea053SToby Isaac 
23615fdea053SToby Isaac   Output Parameters:
23625fdea053SToby Isaac . sym - the section symmetries
23635fdea053SToby Isaac 
23645fdea053SToby Isaac   Level: developer
23655fdea053SToby Isaac 
23665fdea053SToby Isaac .seealso: PetscSectionSymCreate(), PetscSectionSetSym(), PetscSectionGetSym(), PetscSectionSymLabelSetStratum(), PetscSectionGetPointSyms()
23675fdea053SToby Isaac @*/
23685fdea053SToby Isaac PetscErrorCode PetscSectionSymCreateLabel(MPI_Comm comm, DMLabel label, PetscSectionSym *sym)
23695fdea053SToby Isaac {
23705fdea053SToby Isaac   PetscErrorCode        ierr;
23715fdea053SToby Isaac 
23725fdea053SToby Isaac   PetscFunctionBegin;
23735fdea053SToby Isaac   ierr = DMInitializePackage();CHKERRQ(ierr);
23745fdea053SToby Isaac   ierr = PetscSectionSymCreate(comm,sym);CHKERRQ(ierr);
23755fdea053SToby Isaac   ierr = PetscSectionSymSetType(*sym,PETSCSECTIONSYMLABEL);CHKERRQ(ierr);
23765fdea053SToby Isaac   ierr = PetscSectionSymLabelSetLabel(*sym,label);CHKERRQ(ierr);
23775fdea053SToby Isaac   PetscFunctionReturn(0);
23785fdea053SToby Isaac }
2379