Actual source code: dmlabel.c

petsc-master 2020-02-15
Report Typos and Errors
  1:  #include <petscdm.h>
  2:  #include <petsc/private/dmlabelimpl.h>
  3:  #include <petsc/private/sectionimpl.h>
  4:  #include <petscsf.h>
  5:  #include <petscsection.h>

  7: /*@C
  8:   DMLabelCreate - Create a DMLabel object, which is a multimap

 10:   Collective

 12:   Input parameters:
 13: + comm - The communicator, usually PETSC_COMM_SELF
 14: - name - The label name

 16:   Output parameter:
 17: . label - The DMLabel

 19:   Level: beginner

 21:   Notes:
 22:   The label name is actually usual PetscObject name.
 23:   One can get/set it with PetscObjectGetName()/PetscObjectSetName().

 25: .seealso: DMLabelDestroy()
 26: @*/
 27: PetscErrorCode DMLabelCreate(MPI_Comm comm, const char name[], DMLabel *label)
 28: {

 33:   DMInitializePackage();

 35:   PetscHeaderCreate(*label,DMLABEL_CLASSID,"DMLabel","DMLabel","DM",comm,DMLabelDestroy,DMLabelView);

 37:   (*label)->numStrata      = 0;
 38:   (*label)->defaultValue   = -1;
 39:   (*label)->stratumValues  = NULL;
 40:   (*label)->validIS        = NULL;
 41:   (*label)->stratumSizes   = NULL;
 42:   (*label)->points         = NULL;
 43:   (*label)->ht             = NULL;
 44:   (*label)->pStart         = -1;
 45:   (*label)->pEnd           = -1;
 46:   (*label)->bt             = NULL;
 47:   PetscHMapICreate(&(*label)->hmap);
 48:   PetscObjectSetName((PetscObject) *label, name);
 49:   return(0);
 50: }

 52: /*
 53:   DMLabelMakeValid_Private - Transfer stratum data from the hash format to the sorted list format

 55:   Not collective

 57:   Input parameter:
 58: + label - The DMLabel
 59: - v - The stratum value

 61:   Output parameter:
 62: . label - The DMLabel with stratum in sorted list format

 64:   Level: developer

 66: .seealso: DMLabelCreate()
 67: */
 68: static PetscErrorCode DMLabelMakeValid_Private(DMLabel label, PetscInt v)
 69: {
 70:   IS             is;
 71:   PetscInt       off = 0, *pointArray, p;

 74:   if (PetscLikely(v >= 0 && v < label->numStrata) && label->validIS[v]) return 0;
 76:   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);
 77:   PetscHSetIGetSize(label->ht[v], &label->stratumSizes[v]);
 78:   PetscMalloc1(label->stratumSizes[v], &pointArray);
 79:   PetscHSetIGetElems(label->ht[v], &off, pointArray);
 80:   PetscHSetIClear(label->ht[v]);
 81:   PetscSortInt(label->stratumSizes[v], pointArray);
 82:   if (label->bt) {
 83:     for (p = 0; p < label->stratumSizes[v]; ++p) {
 84:       const PetscInt point = pointArray[p];
 85:       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);
 86:       PetscBTSet(label->bt, point - label->pStart);
 87:     }
 88:   }
 89:   if (label->stratumSizes[v] > 0 && pointArray[label->stratumSizes[v]-1] == pointArray[0] + label->stratumSizes[v]-1) {
 90:     ISCreateStride(PETSC_COMM_SELF, label->stratumSizes[v], pointArray[0], 1, &is);
 91:     PetscFree(pointArray);
 92:   } else {
 93:     ISCreateGeneral(PETSC_COMM_SELF, label->stratumSizes[v], pointArray, PETSC_OWN_POINTER, &is);
 94:   }
 95:   PetscObjectSetName((PetscObject) is, "indices");
 96:   label->points[v]  = is;
 97:   label->validIS[v] = PETSC_TRUE;
 98:   PetscObjectStateIncrease((PetscObject) label);
 99:   return(0);
100: }

102: /*
103:   DMLabelMakeAllValid_Private - Transfer all strata from the hash format to the sorted list format

105:   Not collective

107:   Input parameter:
108: . label - The DMLabel

110:   Output parameter:
111: . label - The DMLabel with all strata in sorted list format

113:   Level: developer

115: .seealso: DMLabelCreate()
116: */
117: static PetscErrorCode DMLabelMakeAllValid_Private(DMLabel label)
118: {
119:   PetscInt       v;

123:   for (v = 0; v < label->numStrata; v++){
124:     DMLabelMakeValid_Private(label, v);
125:   }
126:   return(0);
127: }

129: /*
130:   DMLabelMakeInvalid_Private - Transfer stratum data from the sorted list format to the hash format

132:   Not collective

134:   Input parameter:
135: + label - The DMLabel
136: - v - The stratum value

138:   Output parameter:
139: . label - The DMLabel with stratum in hash format

141:   Level: developer

143: .seealso: DMLabelCreate()
144: */
145: static PetscErrorCode DMLabelMakeInvalid_Private(DMLabel label, PetscInt v)
146: {
147:   PetscInt       p;
148:   const PetscInt *points;

151:   if (PetscLikely(v >= 0 && v < label->numStrata) && !label->validIS[v]) return 0;
153:   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);
154:   if (label->points[v]) {
155:     ISGetIndices(label->points[v], &points);
156:     for (p = 0; p < label->stratumSizes[v]; ++p) {
157:       PetscHSetIAdd(label->ht[v], points[p]);
158:     }
159:     ISRestoreIndices(label->points[v],&points);
160:     ISDestroy(&(label->points[v]));
161:   }
162:   label->validIS[v] = PETSC_FALSE;
163:   return(0);
164: }

166: #if !defined(DMLABEL_LOOKUP_THRESHOLD)
167: #define DMLABEL_LOOKUP_THRESHOLD 16
168: #endif

170: PETSC_STATIC_INLINE PetscErrorCode DMLabelLookupStratum(DMLabel label, PetscInt value, PetscInt *index)
171: {
172:   PetscInt       v;

176:   *index = -1;
177:   if (label->numStrata <= DMLABEL_LOOKUP_THRESHOLD) {
178:     for (v = 0; v < label->numStrata; ++v)
179:       if (label->stratumValues[v] == value) {*index = v; break;}
180:   } else {
181:     PetscHMapIGet(label->hmap, value, index);
182:   }
183: #if defined(PETSC_USE_DEBUG)
184:   { /* Check strata hash map consistency */
185:     PetscInt len, loc = -1;
186:     PetscHMapIGetSize(label->hmap, &len);
187:     if (len != label->numStrata) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Inconsistent strata hash map size");
188:     if (label->numStrata <= DMLABEL_LOOKUP_THRESHOLD) {
189:       PetscHMapIGet(label->hmap, value, &loc);
190:     } else {
191:       for (v = 0; v < label->numStrata; ++v)
192:         if (label->stratumValues[v] == value) {loc = v; break;}
193:     }
194:     if (loc != *index) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Inconsistent strata hash map lookup");
195:   }
196: #endif
197:   return(0);
198: }

200: PETSC_STATIC_INLINE PetscErrorCode DMLabelNewStratum(DMLabel label, PetscInt value, PetscInt *index)
201: {
202:   PetscInt       v;
203:   PetscInt      *tmpV;
204:   PetscInt      *tmpS;
205:   PetscHSetI    *tmpH, ht;
206:   IS            *tmpP, is;
207:   PetscBool     *tmpB;
208:   PetscHMapI     hmap = label->hmap;

212:   v    = label->numStrata;
213:   tmpV = label->stratumValues;
214:   tmpS = label->stratumSizes;
215:   tmpH = label->ht;
216:   tmpP = label->points;
217:   tmpB = label->validIS;
218:   { /* TODO: PetscRealloc() is broken, use malloc+memcpy+free  */
219:     PetscInt   *oldV = tmpV;
220:     PetscInt   *oldS = tmpS;
221:     PetscHSetI *oldH = tmpH;
222:     IS         *oldP = tmpP;
223:     PetscBool  *oldB = tmpB;
224:     PetscMalloc((v+1)*sizeof(*tmpV), &tmpV);
225:     PetscMalloc((v+1)*sizeof(*tmpS), &tmpS);
226:     PetscMalloc((v+1)*sizeof(*tmpH), &tmpH);
227:     PetscMalloc((v+1)*sizeof(*tmpP), &tmpP);
228:     PetscMalloc((v+1)*sizeof(*tmpB), &tmpB);
229:     PetscArraycpy(tmpV, oldV, v);
230:     PetscArraycpy(tmpS, oldS, v);
231:     PetscArraycpy(tmpH, oldH, v);
232:     PetscArraycpy(tmpP, oldP, v);
233:     PetscArraycpy(tmpB, oldB, v);
234:     PetscFree(oldV);
235:     PetscFree(oldS);
236:     PetscFree(oldH);
237:     PetscFree(oldP);
238:     PetscFree(oldB);
239:   }
240:   label->numStrata     = v+1;
241:   label->stratumValues = tmpV;
242:   label->stratumSizes  = tmpS;
243:   label->ht            = tmpH;
244:   label->points        = tmpP;
245:   label->validIS       = tmpB;
246:   PetscHSetICreate(&ht);
247:   ISCreateStride(PETSC_COMM_SELF,0,0,1,&is);
248:   PetscHMapISet(hmap, value, v);
249:   tmpV[v] = value;
250:   tmpS[v] = 0;
251:   tmpH[v] = ht;
252:   tmpP[v] = is;
253:   tmpB[v] = PETSC_TRUE;
254:   PetscObjectStateIncrease((PetscObject) label);
255:   *index = v;
256:   return(0);
257: }

259: PETSC_STATIC_INLINE PetscErrorCode DMLabelLookupAddStratum(DMLabel label, PetscInt value, PetscInt *index)
260: {
263:   DMLabelLookupStratum(label, value, index);
264:   if (*index < 0) {DMLabelNewStratum(label, value, index);}
265:   return(0);
266: }

268: /*@
269:   DMLabelAddStratum - Adds a new stratum value in a DMLabel

271:   Input Parameter:
272: + label - The DMLabel
273: - value - The stratum value

275:   Level: beginner

277: .seealso:  DMLabelCreate(), DMLabelDestroy()
278: @*/
279: PetscErrorCode DMLabelAddStratum(DMLabel label, PetscInt value)
280: {
281:   PetscInt       v;

286:   DMLabelLookupAddStratum(label, value, &v);
287:   return(0);
288: }

290: /*@
291:   DMLabelAddStrata - Adds new stratum values in a DMLabel

293:   Not collective

295:   Input Parameter:
296: + label - The DMLabel
297: . numStrata - The number of stratum values
298: - stratumValues - The stratum values

300:   Level: beginner

302: .seealso:  DMLabelCreate(), DMLabelDestroy()
303: @*/
304: PetscErrorCode DMLabelAddStrata(DMLabel label, PetscInt numStrata, const PetscInt stratumValues[])
305: {
306:   PetscInt       *values, v;

312:   PetscMalloc1(numStrata, &values);
313:   PetscArraycpy(values, stratumValues, numStrata);
314:   PetscSortRemoveDupsInt(&numStrata, values);
315:   if (!label->numStrata) { /* Fast preallocation */
316:     PetscInt   *tmpV;
317:     PetscInt   *tmpS;
318:     PetscHSetI *tmpH, ht;
319:     IS         *tmpP, is;
320:     PetscBool  *tmpB;
321:     PetscHMapI  hmap = label->hmap;

323:     PetscMalloc1(numStrata, &tmpV);
324:     PetscMalloc1(numStrata, &tmpS);
325:     PetscMalloc1(numStrata, &tmpH);
326:     PetscMalloc1(numStrata, &tmpP);
327:     PetscMalloc1(numStrata, &tmpB);
328:     label->numStrata     = numStrata;
329:     label->stratumValues = tmpV;
330:     label->stratumSizes  = tmpS;
331:     label->ht            = tmpH;
332:     label->points        = tmpP;
333:     label->validIS       = tmpB;
334:     for (v = 0; v < numStrata; ++v) {
335:       PetscHSetICreate(&ht);
336:       ISCreateStride(PETSC_COMM_SELF,0,0,1,&is);
337:       PetscHMapISet(hmap, values[v], v);
338:       tmpV[v] = values[v];
339:       tmpS[v] = 0;
340:       tmpH[v] = ht;
341:       tmpP[v] = is;
342:       tmpB[v] = PETSC_TRUE;
343:     }
344:     PetscObjectStateIncrease((PetscObject) label);
345:   } else {
346:     for (v = 0; v < numStrata; ++v) {
347:       DMLabelAddStratum(label, values[v]);
348:     }
349:   }
350:   PetscFree(values);
351:   return(0);
352: }

354: /*@
355:   DMLabelAddStrataIS - Adds new stratum values in a DMLabel

357:   Not collective

359:   Input Parameter:
360: + label - The DMLabel
361: - valueIS - Index set with stratum values

363:   Level: beginner

365: .seealso:  DMLabelCreate(), DMLabelDestroy()
366: @*/
367: PetscErrorCode DMLabelAddStrataIS(DMLabel label, IS valueIS)
368: {
369:   PetscInt       numStrata;
370:   const PetscInt *stratumValues;

376:   ISGetLocalSize(valueIS, &numStrata);
377:   ISGetIndices(valueIS, &stratumValues);
378:   DMLabelAddStrata(label, numStrata, stratumValues);
379:   return(0);
380: }

382: static PetscErrorCode DMLabelView_Ascii(DMLabel label, PetscViewer viewer)
383: {
384:   PetscInt       v;
385:   PetscMPIInt    rank;

389:   MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank);
390:   PetscViewerASCIIPushSynchronized(viewer);
391:   if (label) {
392:     const char *name;

394:     PetscObjectGetName((PetscObject) label, &name);
395:     PetscViewerASCIIPrintf(viewer, "Label '%s':\n", name);
396:     if (label->bt) {PetscViewerASCIIPrintf(viewer, "  Index has been calculated in [%D, %D)\n", label->pStart, label->pEnd);}
397:     for (v = 0; v < label->numStrata; ++v) {
398:       const PetscInt value = label->stratumValues[v];
399:       const PetscInt *points;
400:       PetscInt       p;

402:       ISGetIndices(label->points[v], &points);
403:       for (p = 0; p < label->stratumSizes[v]; ++p) {
404:         PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D (%D)\n", rank, points[p], value);
405:       }
406:       ISRestoreIndices(label->points[v],&points);
407:     }
408:   }
409:   PetscViewerFlush(viewer);
410:   PetscViewerASCIIPopSynchronized(viewer);
411:   return(0);
412: }

414: /*@C
415:   DMLabelView - View the label

417:   Collective on viewer

419:   Input Parameters:
420: + label - The DMLabel
421: - viewer - The PetscViewer

423:   Level: intermediate

425: .seealso: DMLabelCreate(), DMLabelDestroy()
426: @*/
427: PetscErrorCode DMLabelView(DMLabel label, PetscViewer viewer)
428: {
429:   PetscBool      iascii;

434:   if (!viewer) {PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)label), &viewer);}
436:   if (label) {DMLabelMakeAllValid_Private(label);}
437:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);
438:   if (iascii) {
439:     DMLabelView_Ascii(label, viewer);
440:   }
441:   return(0);
442: }

444: /*@
445:   DMLabelReset - Destroys internal data structures in a DMLabel

447:   Not collective

449:   Input Parameter:
450: . label - The DMLabel

452:   Level: beginner

454: .seealso: DMLabelDestroy(), DMLabelCreate()
455: @*/
456: PetscErrorCode DMLabelReset(DMLabel label)
457: {
458:   PetscInt       v;

463:   for (v = 0; v < label->numStrata; ++v) {
464:     PetscHSetIDestroy(&label->ht[v]);
465:     ISDestroy(&label->points[v]);
466:   }
467:   label->numStrata = 0;
468:   PetscFree(label->stratumValues);
469:   PetscFree(label->stratumSizes);
470:   PetscFree(label->ht);
471:   PetscFree(label->points);
472:   PetscFree(label->validIS);
473:   PetscHMapIReset(label->hmap);
474:   label->pStart = -1;
475:   label->pEnd   = -1;
476:   PetscBTDestroy(&label->bt);
477:   return(0);
478: }

480: /*@
481:   DMLabelDestroy - Destroys a DMLabel

483:   Collective on label

485:   Input Parameter:
486: . label - The DMLabel

488:   Level: beginner

490: .seealso: DMLabelReset(), DMLabelCreate()
491: @*/
492: PetscErrorCode DMLabelDestroy(DMLabel *label)
493: {

497:   if (!*label) return(0);
499:   if (--((PetscObject)(*label))->refct > 0) {*label = NULL; return(0);}
500:   DMLabelReset(*label);
501:   PetscHMapIDestroy(&(*label)->hmap);
502:   PetscHeaderDestroy(label);
503:   return(0);
504: }

506: /*@
507:   DMLabelDuplicate - Duplicates a DMLabel

509:   Collective on label

511:   Input Parameter:
512: . label - The DMLabel

514:   Output Parameter:
515: . labelnew - location to put new vector

517:   Level: intermediate

519: .seealso: DMLabelCreate(), DMLabelDestroy()
520: @*/
521: PetscErrorCode DMLabelDuplicate(DMLabel label, DMLabel *labelnew)
522: {
523:   const char    *name;
524:   PetscInt       v;

529:   DMLabelMakeAllValid_Private(label);
530:   PetscObjectGetName((PetscObject) label, &name);
531:   DMLabelCreate(PetscObjectComm((PetscObject) label), name, labelnew);

533:   (*labelnew)->numStrata    = label->numStrata;
534:   (*labelnew)->defaultValue = label->defaultValue;
535:   PetscMalloc1(label->numStrata, &(*labelnew)->stratumValues);
536:   PetscMalloc1(label->numStrata, &(*labelnew)->stratumSizes);
537:   PetscMalloc1(label->numStrata, &(*labelnew)->ht);
538:   PetscMalloc1(label->numStrata, &(*labelnew)->points);
539:   PetscMalloc1(label->numStrata, &(*labelnew)->validIS);
540:   for (v = 0; v < label->numStrata; ++v) {
541:     PetscHSetICreate(&(*labelnew)->ht[v]);
542:     (*labelnew)->stratumValues[v]  = label->stratumValues[v];
543:     (*labelnew)->stratumSizes[v]   = label->stratumSizes[v];
544:     PetscObjectReference((PetscObject) (label->points[v]));
545:     (*labelnew)->points[v]         = label->points[v];
546:     (*labelnew)->validIS[v]        = PETSC_TRUE;
547:   }
548:   PetscHMapIDestroy(&(*labelnew)->hmap);
549:   PetscHMapIDuplicate(label->hmap,&(*labelnew)->hmap);
550:   (*labelnew)->pStart = -1;
551:   (*labelnew)->pEnd   = -1;
552:   (*labelnew)->bt     = NULL;
553:   return(0);
554: }

556: /*@
557:   DMLabelComputeIndex - Create an index structure for membership determination, automatically determining the bounds

559:   Not collective

561:   Input Parameter:
562: . label  - The DMLabel

564:   Level: intermediate

566: .seealso: DMLabelHasPoint(), DMLabelCreateIndex(), DMLabelDestroyIndex(), DMLabelGetValue(), DMLabelSetValue()
567: @*/
568: PetscErrorCode DMLabelComputeIndex(DMLabel label)
569: {
570:   PetscInt       pStart = PETSC_MAX_INT, pEnd = -1, v;

575:   DMLabelMakeAllValid_Private(label);
576:   for (v = 0; v < label->numStrata; ++v) {
577:     const PetscInt *points;
578:     PetscInt       i;

580:     ISGetIndices(label->points[v], &points);
581:     for (i = 0; i < label->stratumSizes[v]; ++i) {
582:       const PetscInt point = points[i];

584:       pStart = PetscMin(point,   pStart);
585:       pEnd   = PetscMax(point+1, pEnd);
586:     }
587:     ISRestoreIndices(label->points[v], &points);
588:   }
589:   label->pStart = pStart == PETSC_MAX_INT ? -1 : pStart;
590:   label->pEnd   = pEnd;
591:   DMLabelCreateIndex(label, label->pStart, label->pEnd);
592:   return(0);
593: }

595: /*@
596:   DMLabelCreateIndex - Create an index structure for membership determination

598:   Not collective

600:   Input Parameters:
601: + label  - The DMLabel
602: . pStart - The smallest point
603: - pEnd   - The largest point + 1

605:   Level: intermediate

607: .seealso: DMLabelHasPoint(), DMLabelComputeIndex(), DMLabelDestroyIndex(), DMLabelGetValue(), DMLabelSetValue()
608: @*/
609: PetscErrorCode DMLabelCreateIndex(DMLabel label, PetscInt pStart, PetscInt pEnd)
610: {
611:   PetscInt       v;

616:   DMLabelDestroyIndex(label);
617:   DMLabelMakeAllValid_Private(label);
618:   label->pStart = pStart;
619:   label->pEnd   = pEnd;
620:   /* This can be hooked into SetValue(),  ClearValue(), etc. for updating */
621:   PetscBTCreate(pEnd - pStart, &label->bt);
622:   for (v = 0; v < label->numStrata; ++v) {
623:     const PetscInt *points;
624:     PetscInt       i;

626:     ISGetIndices(label->points[v], &points);
627:     for (i = 0; i < label->stratumSizes[v]; ++i) {
628:       const PetscInt point = points[i];

630:       if ((point < pStart) || (point >= pEnd)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label point %D is not in [%D, %D)", point, pStart, pEnd);
631:       PetscBTSet(label->bt, point - pStart);
632:     }
633:     ISRestoreIndices(label->points[v], &points);
634:   }
635:   return(0);
636: }

638: /*@
639:   DMLabelDestroyIndex - Destroy the index structure

641:   Not collective

643:   Input Parameter:
644: . label - the DMLabel

646:   Level: intermediate

648: .seealso: DMLabelHasPoint(), DMLabelCreateIndex(), DMLabelGetValue(), DMLabelSetValue()
649: @*/
650: PetscErrorCode DMLabelDestroyIndex(DMLabel label)
651: {

656:   label->pStart = -1;
657:   label->pEnd   = -1;
658:   PetscBTDestroy(&label->bt);
659:   return(0);
660: }

662: /*@
663:   DMLabelGetBounds - Return the smallest and largest point in the label

665:   Not collective

667:   Input Parameter:
668: . label - the DMLabel

670:   Output Parameters:
671: + pStart - The smallest point
672: - pEnd   - The largest point + 1

674:   Note: This will compute an index for the label if one does not exist.

676:   Level: intermediate

678: .seealso: DMLabelHasPoint(), DMLabelCreateIndex(), DMLabelGetValue(), DMLabelSetValue()
679: @*/
680: PetscErrorCode DMLabelGetBounds(DMLabel label, PetscInt *pStart, PetscInt *pEnd)
681: {

686:   if ((label->pStart == -1) && (label->pEnd == -1)) {DMLabelComputeIndex(label);}
687:   if (pStart) {
689:     *pStart = label->pStart;
690:   }
691:   if (pEnd) {
693:     *pEnd = label->pEnd;
694:   }
695:   return(0);
696: }

698: /*@
699:   DMLabelHasValue - Determine whether a label assigns the value to any point

701:   Not collective

703:   Input Parameters:
704: + label - the DMLabel
705: - value - the value

707:   Output Parameter:
708: . contains - Flag indicating whether the label maps this value to any point

710:   Level: developer

712: .seealso: DMLabelHasPoint(), DMLabelGetValue(), DMLabelSetValue()
713: @*/
714: PetscErrorCode DMLabelHasValue(DMLabel label, PetscInt value, PetscBool *contains)
715: {
716:   PetscInt v;

722:   DMLabelLookupStratum(label, value, &v);
723:   *contains = v < 0 ? PETSC_FALSE : PETSC_TRUE;
724:   return(0);
725: }

727: /*@
728:   DMLabelHasPoint - Determine whether a label assigns a value to a point

730:   Not collective

732:   Input Parameters:
733: + label - the DMLabel
734: - point - the point

736:   Output Parameter:
737: . contains - Flag indicating whether the label maps this point to a value

739:   Note: The user must call DMLabelCreateIndex() before this function.

741:   Level: developer

743: .seealso: DMLabelCreateIndex(), DMLabelGetValue(), DMLabelSetValue()
744: @*/
745: PetscErrorCode DMLabelHasPoint(DMLabel label, PetscInt point, PetscBool *contains)
746: {

752:   DMLabelMakeAllValid_Private(label);
753: #if defined(PETSC_USE_DEBUG)
754:   if (!label->bt) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call DMLabelCreateIndex() before DMLabelHasPoint()");
755:   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);
756: #endif
757:   *contains = PetscBTLookup(label->bt, point - label->pStart) ? PETSC_TRUE : PETSC_FALSE;
758:   return(0);
759: }

761: /*@
762:   DMLabelStratumHasPoint - Return true if the stratum contains a point

764:   Not collective

766:   Input Parameters:
767: + label - the DMLabel
768: . value - the stratum value
769: - point - the point

771:   Output Parameter:
772: . contains - true if the stratum contains the point

774:   Level: intermediate

776: .seealso: DMLabelCreate(), DMLabelSetValue(), DMLabelClearValue()
777: @*/
778: PetscErrorCode DMLabelStratumHasPoint(DMLabel label, PetscInt value, PetscInt point, PetscBool *contains)
779: {
780:   PetscInt       v;

786:   *contains = PETSC_FALSE;
787:   DMLabelLookupStratum(label, value, &v);
788:   if (v < 0) return(0);

790:   if (label->validIS[v]) {
791:     PetscInt i;

793:     ISLocate(label->points[v], point, &i);
794:     if (i >= 0) *contains = PETSC_TRUE;
795:   } else {
796:     PetscBool has;

798:     PetscHSetIHas(label->ht[v], point, &has);
799:     if (has) *contains = PETSC_TRUE;
800:   }
801:   return(0);
802: }

804: /*@
805:   DMLabelGetDefaultValue - Get the default value returned by DMLabelGetValue() if a point has not been explicitly given a value.
806:   When a label is created, it is initialized to -1.

808:   Not collective

810:   Input parameter:
811: . label - a DMLabel object

813:   Output parameter:
814: . defaultValue - the default value

816:   Level: beginner

818: .seealso: DMLabelSetDefaultValue(), DMLabelGetValue(), DMLabelSetValue()
819: @*/
820: PetscErrorCode DMLabelGetDefaultValue(DMLabel label, PetscInt *defaultValue)
821: {
824:   *defaultValue = label->defaultValue;
825:   return(0);
826: }

828: /*@
829:   DMLabelSetDefaultValue - Set the default value returned by DMLabelGetValue() if a point has not been explicitly given a value.
830:   When a label is created, it is initialized to -1.

832:   Not collective

834:   Input parameter:
835: . label - a DMLabel object

837:   Output parameter:
838: . defaultValue - the default value

840:   Level: beginner

842: .seealso: DMLabelGetDefaultValue(), DMLabelGetValue(), DMLabelSetValue()
843: @*/
844: PetscErrorCode DMLabelSetDefaultValue(DMLabel label, PetscInt defaultValue)
845: {
848:   label->defaultValue = defaultValue;
849:   return(0);
850: }

852: /*@
853:   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())

855:   Not collective

857:   Input Parameters:
858: + label - the DMLabel
859: - point - the point

861:   Output Parameter:
862: . value - The point value, or the default value (-1 by default)

864:   Level: intermediate

866: .seealso: DMLabelCreate(), DMLabelSetValue(), DMLabelClearValue(), DMLabelGetDefaultValue(), DMLabelSetDefaultValue()
867: @*/
868: PetscErrorCode DMLabelGetValue(DMLabel label, PetscInt point, PetscInt *value)
869: {
870:   PetscInt       v;

876:   *value = label->defaultValue;
877:   for (v = 0; v < label->numStrata; ++v) {
878:     if (label->validIS[v]) {
879:       PetscInt i;

881:       ISLocate(label->points[v], point, &i);
882:       if (i >= 0) {
883:         *value = label->stratumValues[v];
884:         break;
885:       }
886:     } else {
887:       PetscBool has;

889:       PetscHSetIHas(label->ht[v], point, &has);
890:       if (has) {
891:         *value = label->stratumValues[v];
892:         break;
893:       }
894:     }
895:   }
896:   return(0);
897: }

899: /*@
900:   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.

902:   Not collective

904:   Input Parameters:
905: + label - the DMLabel
906: . point - the point
907: - value - The point value

909:   Level: intermediate

911: .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelClearValue(), DMLabelGetDefaultValue(), DMLabelSetDefaultValue()
912: @*/
913: PetscErrorCode DMLabelSetValue(DMLabel label, PetscInt point, PetscInt value)
914: {
915:   PetscInt       v;

920:   /* Find label value, add new entry if needed */
921:   if (value == label->defaultValue) return(0);
922:   DMLabelLookupAddStratum(label, value, &v);
923:   /* Set key */
924:   DMLabelMakeInvalid_Private(label, v);
925:   PetscHSetIAdd(label->ht[v], point);
926:   return(0);
927: }

929: /*@
930:   DMLabelClearValue - Clear the value a label assigns to a point

932:   Not collective

934:   Input Parameters:
935: + label - the DMLabel
936: . point - the point
937: - value - The point value

939:   Level: intermediate

941: .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue()
942: @*/
943: PetscErrorCode DMLabelClearValue(DMLabel label, PetscInt point, PetscInt value)
944: {
945:   PetscInt       v;

950:   /* Find label value */
951:   DMLabelLookupStratum(label, value, &v);
952:   if (v < 0) return(0);

954:   if (label->bt) {
955:     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);
956:     PetscBTClear(label->bt, point - label->pStart);
957:   }

959:   /* Delete key */
960:   DMLabelMakeInvalid_Private(label, v);
961:   PetscHSetIDel(label->ht[v], point);
962:   return(0);
963: }

965: /*@
966:   DMLabelInsertIS - Set all points in the IS to a value

968:   Not collective

970:   Input Parameters:
971: + label - the DMLabel
972: . is    - the point IS
973: - value - The point value

975:   Level: intermediate

977: .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
978: @*/
979: PetscErrorCode DMLabelInsertIS(DMLabel label, IS is, PetscInt value)
980: {
981:   PetscInt        v, n, p;
982:   const PetscInt *points;
983:   PetscErrorCode  ierr;

988:   /* Find label value, add new entry if needed */
989:   if (value == label->defaultValue) return(0);
990:   DMLabelLookupAddStratum(label, value, &v);
991:   /* Set keys */
992:   DMLabelMakeInvalid_Private(label, v);
993:   ISGetLocalSize(is, &n);
994:   ISGetIndices(is, &points);
995:   for (p = 0; p < n; ++p) {PetscHSetIAdd(label->ht[v], points[p]);}
996:   ISRestoreIndices(is, &points);
997:   return(0);
998: }

1000: /*@
1001:   DMLabelGetNumValues - Get the number of values that the DMLabel takes

1003:   Not collective

1005:   Input Parameter:
1006: . label - the DMLabel

1008:   Output Paramater:
1009: . numValues - the number of values

1011:   Level: intermediate

1013: .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
1014: @*/
1015: PetscErrorCode DMLabelGetNumValues(DMLabel label, PetscInt *numValues)
1016: {
1020:   *numValues = label->numStrata;
1021:   return(0);
1022: }

1024: /*@
1025:   DMLabelGetValueIS - Get an IS of all values that the DMlabel takes

1027:   Not collective

1029:   Input Parameter:
1030: . label - the DMLabel

1032:   Output Paramater:
1033: . is    - the value IS

1035:   Level: intermediate

1037: .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
1038: @*/
1039: PetscErrorCode DMLabelGetValueIS(DMLabel label, IS *values)
1040: {

1046:   ISCreateGeneral(PETSC_COMM_SELF, label->numStrata, label->stratumValues, PETSC_USE_POINTER, values);
1047:   return(0);
1048: }

1050: /*@
1051:   DMLabelHasStratum - Determine whether points exist with the given value

1053:   Not collective

1055:   Input Parameters:
1056: + label - the DMLabel
1057: - value - the stratum value

1059:   Output Paramater:
1060: . exists - Flag saying whether points exist

1062:   Level: intermediate

1064: .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
1065: @*/
1066: PetscErrorCode DMLabelHasStratum(DMLabel label, PetscInt value, PetscBool *exists)
1067: {
1068:   PetscInt       v;

1074:   DMLabelLookupStratum(label, value, &v);
1075:   *exists = v < 0 ? PETSC_FALSE : PETSC_TRUE;
1076:   return(0);
1077: }

1079: /*@
1080:   DMLabelGetStratumSize - Get the size of a stratum

1082:   Not collective

1084:   Input Parameters:
1085: + label - the DMLabel
1086: - value - the stratum value

1088:   Output Paramater:
1089: . size - The number of points in the stratum

1091:   Level: intermediate

1093: .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
1094: @*/
1095: PetscErrorCode DMLabelGetStratumSize(DMLabel label, PetscInt value, PetscInt *size)
1096: {
1097:   PetscInt       v;

1103:   *size = 0;
1104:   DMLabelLookupStratum(label, value, &v);
1105:   if (v < 0) return(0);
1106:   DMLabelMakeValid_Private(label, v);
1107:   *size = label->stratumSizes[v];
1108:   return(0);
1109: }

1111: /*@
1112:   DMLabelGetStratumBounds - Get the largest and smallest point of a stratum

1114:   Not collective

1116:   Input Parameters:
1117: + label - the DMLabel
1118: - value - the stratum value

1120:   Output Paramaters:
1121: + start - the smallest point in the stratum
1122: - end - the largest point in the stratum

1124:   Level: intermediate

1126: .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
1127: @*/
1128: PetscErrorCode DMLabelGetStratumBounds(DMLabel label, PetscInt value, PetscInt *start, PetscInt *end)
1129: {
1130:   PetscInt       v, min, max;

1137:   DMLabelLookupStratum(label, value, &v);
1138:   if (v < 0) return(0);
1139:   DMLabelMakeValid_Private(label, v);
1140:   if (label->stratumSizes[v] <= 0) return(0);
1141:   ISGetMinMax(label->points[v], &min, &max);
1142:   if (start) *start = min;
1143:   if (end)   *end   = max+1;
1144:   return(0);
1145: }

1147: /*@
1148:   DMLabelGetStratumIS - Get an IS with the stratum points

1150:   Not collective

1152:   Input Parameters:
1153: + label - the DMLabel
1154: - value - the stratum value

1156:   Output Paramater:
1157: . points - The stratum points

1159:   Level: intermediate

1161:   Notes:
1162:   The output IS should be destroyed when no longer needed.
1163:   Returns NULL if the stratum is empty.

1165: .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
1166: @*/
1167: PetscErrorCode DMLabelGetStratumIS(DMLabel label, PetscInt value, IS *points)
1168: {
1169:   PetscInt       v;

1175:   *points = NULL;
1176:   DMLabelLookupStratum(label, value, &v);
1177:   if (v < 0) return(0);
1178:   DMLabelMakeValid_Private(label, v);
1179:   PetscObjectReference((PetscObject) label->points[v]);
1180:   *points = label->points[v];
1181:   return(0);
1182: }

1184: /*@
1185:   DMLabelSetStratumIS - Set the stratum points using an IS

1187:   Not collective

1189:   Input Parameters:
1190: + label - the DMLabel
1191: . value - the stratum value
1192: - points - The stratum points

1194:   Level: intermediate

1196: .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
1197: @*/
1198: PetscErrorCode DMLabelSetStratumIS(DMLabel label, PetscInt value, IS is)
1199: {
1200:   PetscInt       v;

1206:   DMLabelLookupAddStratum(label, value, &v);
1207:   if (is == label->points[v]) return(0);
1208:   DMLabelClearStratum(label, value);
1209:   ISGetLocalSize(is, &(label->stratumSizes[v]));
1210:   PetscObjectReference((PetscObject)is);
1211:   ISDestroy(&(label->points[v]));
1212:   label->points[v]  = is;
1213:   label->validIS[v] = PETSC_TRUE;
1214:   PetscObjectStateIncrease((PetscObject) label);
1215:   if (label->bt) {
1216:     const PetscInt *points;
1217:     PetscInt p;

1219:     ISGetIndices(is,&points);
1220:     for (p = 0; p < label->stratumSizes[v]; ++p) {
1221:       const PetscInt point = points[p];

1223:       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);
1224:       PetscBTSet(label->bt, point - label->pStart);
1225:     }
1226:   }
1227:   return(0);
1228: }

1230: /*@
1231:   DMLabelClearStratum - Remove a stratum

1233:   Not collective

1235:   Input Parameters:
1236: + label - the DMLabel
1237: - value - the stratum value

1239:   Level: intermediate

1241: .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
1242: @*/
1243: PetscErrorCode DMLabelClearStratum(DMLabel label, PetscInt value)
1244: {
1245:   PetscInt       v;

1250:   DMLabelLookupStratum(label, value, &v);
1251:   if (v < 0) return(0);
1252:   if (label->validIS[v]) {
1253:     if (label->bt) {
1254:       PetscInt       i;
1255:       const PetscInt *points;

1257:       ISGetIndices(label->points[v], &points);
1258:       for (i = 0; i < label->stratumSizes[v]; ++i) {
1259:         const PetscInt point = points[i];

1261:         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);
1262:         PetscBTClear(label->bt, point - label->pStart);
1263:       }
1264:       ISRestoreIndices(label->points[v], &points);
1265:     }
1266:     label->stratumSizes[v] = 0;
1267:     ISDestroy(&label->points[v]);
1268:     ISCreateStride(PETSC_COMM_SELF, 0, 0, 1, &label->points[v]);
1269:     PetscObjectSetName((PetscObject) label->points[v], "indices");
1270:     PetscObjectStateIncrease((PetscObject) label);
1271:   } else {
1272:     PetscHSetIClear(label->ht[v]);
1273:   }
1274:   return(0);
1275: }

1277: /*@
1278:   DMLabelFilter - Remove all points outside of [start, end)

1280:   Not collective

1282:   Input Parameters:
1283: + label - the DMLabel
1284: . start - the first point kept
1285: - end - one more than the last point kept

1287:   Level: intermediate

1289: .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
1290: @*/
1291: PetscErrorCode DMLabelFilter(DMLabel label, PetscInt start, PetscInt end)
1292: {
1293:   PetscInt       v;

1298:   DMLabelDestroyIndex(label);
1299:   DMLabelMakeAllValid_Private(label);
1300:   for (v = 0; v < label->numStrata; ++v) {
1301:     ISGeneralFilter(label->points[v], start, end);
1302:   }
1303:   DMLabelCreateIndex(label, start, end);
1304:   return(0);
1305: }

1307: /*@
1308:   DMLabelPermute - Create a new label with permuted points

1310:   Not collective

1312:   Input Parameters:
1313: + label - the DMLabel
1314: - permutation - the point permutation

1316:   Output Parameter:
1317: . labelnew - the new label containing the permuted points

1319:   Level: intermediate

1321: .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
1322: @*/
1323: PetscErrorCode DMLabelPermute(DMLabel label, IS permutation, DMLabel *labelNew)
1324: {
1325:   const PetscInt *perm;
1326:   PetscInt        numValues, numPoints, v, q;
1327:   PetscErrorCode  ierr;

1332:   DMLabelMakeAllValid_Private(label);
1333:   DMLabelDuplicate(label, labelNew);
1334:   DMLabelGetNumValues(*labelNew, &numValues);
1335:   ISGetLocalSize(permutation, &numPoints);
1336:   ISGetIndices(permutation, &perm);
1337:   for (v = 0; v < numValues; ++v) {
1338:     const PetscInt size   = (*labelNew)->stratumSizes[v];
1339:     const PetscInt *points;
1340:     PetscInt *pointsNew;

1342:     ISGetIndices((*labelNew)->points[v],&points);
1343:     PetscMalloc1(size,&pointsNew);
1344:     for (q = 0; q < size; ++q) {
1345:       const PetscInt point = points[q];

1347:       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);
1348:       pointsNew[q] = perm[point];
1349:     }
1350:     ISRestoreIndices((*labelNew)->points[v],&points);
1351:     PetscSortInt(size, pointsNew);
1352:     ISDestroy(&((*labelNew)->points[v]));
1353:     if (size > 0 && pointsNew[size - 1] == pointsNew[0] + size - 1) {
1354:       ISCreateStride(PETSC_COMM_SELF,size,pointsNew[0],1,&((*labelNew)->points[v]));
1355:       PetscFree(pointsNew);
1356:     } else {
1357:       ISCreateGeneral(PETSC_COMM_SELF,size,pointsNew,PETSC_OWN_POINTER,&((*labelNew)->points[v]));
1358:     }
1359:     PetscObjectSetName((PetscObject) ((*labelNew)->points[v]), "indices");
1360:   }
1361:   ISRestoreIndices(permutation, &perm);
1362:   if (label->bt) {
1363:     PetscBTDestroy(&label->bt);
1364:     DMLabelCreateIndex(label, label->pStart, label->pEnd);
1365:   }
1366:   return(0);
1367: }

1369: PetscErrorCode DMLabelDistribute_Internal(DMLabel label, PetscSF sf, PetscSection *leafSection, PetscInt **leafStrata)
1370: {
1371:   MPI_Comm       comm;
1372:   PetscInt       s, l, nroots, nleaves, dof, offset, size;
1373:   PetscInt      *remoteOffsets, *rootStrata, *rootIdx;
1374:   PetscSection   rootSection;
1375:   PetscSF        labelSF;

1379:   if (label) {DMLabelMakeAllValid_Private(label);}
1380:   PetscObjectGetComm((PetscObject)sf, &comm);
1381:   /* Build a section of stratum values per point, generate the according SF
1382:      and distribute point-wise stratum values to leaves. */
1383:   PetscSFGetGraph(sf, &nroots, &nleaves, NULL, NULL);
1384:   PetscSectionCreate(comm, &rootSection);
1385:   PetscSectionSetChart(rootSection, 0, nroots);
1386:   if (label) {
1387:     for (s = 0; s < label->numStrata; ++s) {
1388:       const PetscInt *points;

1390:       ISGetIndices(label->points[s], &points);
1391:       for (l = 0; l < label->stratumSizes[s]; l++) {
1392:         PetscSectionGetDof(rootSection, points[l], &dof);
1393:         PetscSectionSetDof(rootSection, points[l], dof+1);
1394:       }
1395:       ISRestoreIndices(label->points[s], &points);
1396:     }
1397:   }
1398:   PetscSectionSetUp(rootSection);
1399:   /* Create a point-wise array of stratum values */
1400:   PetscSectionGetStorageSize(rootSection, &size);
1401:   PetscMalloc1(size, &rootStrata);
1402:   PetscCalloc1(nroots, &rootIdx);
1403:   if (label) {
1404:     for (s = 0; s < label->numStrata; ++s) {
1405:       const PetscInt *points;

1407:       ISGetIndices(label->points[s], &points);
1408:       for (l = 0; l < label->stratumSizes[s]; l++) {
1409:         const PetscInt p = points[l];
1410:         PetscSectionGetOffset(rootSection, p, &offset);
1411:         rootStrata[offset+rootIdx[p]++] = label->stratumValues[s];
1412:       }
1413:       ISRestoreIndices(label->points[s], &points);
1414:     }
1415:   }
1416:   /* Build SF that maps label points to remote processes */
1417:   PetscSectionCreate(comm, leafSection);
1418:   PetscSFDistributeSection(sf, rootSection, &remoteOffsets, *leafSection);
1419:   PetscSFCreateSectionSF(sf, rootSection, remoteOffsets, *leafSection, &labelSF);
1420:   PetscFree(remoteOffsets);
1421:   /* Send the strata for each point over the derived SF */
1422:   PetscSectionGetStorageSize(*leafSection, &size);
1423:   PetscMalloc1(size, leafStrata);
1424:   PetscSFBcastBegin(labelSF, MPIU_INT, rootStrata, *leafStrata);
1425:   PetscSFBcastEnd(labelSF, MPIU_INT, rootStrata, *leafStrata);
1426:   /* Clean up */
1427:   PetscFree(rootStrata);
1428:   PetscFree(rootIdx);
1429:   PetscSectionDestroy(&rootSection);
1430:   PetscSFDestroy(&labelSF);
1431:   return(0);
1432: }

1434: /*@
1435:   DMLabelDistribute - Create a new label pushed forward over the PetscSF

1437:   Collective on sf

1439:   Input Parameters:
1440: + label - the DMLabel
1441: - sf    - the map from old to new distribution

1443:   Output Parameter:
1444: . labelnew - the new redistributed label

1446:   Level: intermediate

1448: .seealso: DMLabelCreate(), DMLabelGetValue(), DMLabelSetValue(), DMLabelClearValue()
1449: @*/
1450: PetscErrorCode DMLabelDistribute(DMLabel label, PetscSF sf, DMLabel *labelNew)
1451: {
1452:   MPI_Comm       comm;
1453:   PetscSection   leafSection;
1454:   PetscInt       p, pStart, pEnd, s, size, dof, offset, stratum;
1455:   PetscInt      *leafStrata, *strataIdx;
1456:   PetscInt     **points;
1457:   const char    *lname = NULL;
1458:   char          *name;
1459:   PetscInt       nameSize;
1460:   PetscHSetI     stratumHash;
1461:   size_t         len = 0;
1462:   PetscMPIInt    rank;

1467:   if (label) {
1469:     DMLabelMakeAllValid_Private(label);
1470:   }
1471:   PetscObjectGetComm((PetscObject)sf, &comm);
1472:   MPI_Comm_rank(comm, &rank);
1473:   /* Bcast name */
1474:   if (!rank) {
1475:     PetscObjectGetName((PetscObject) label, &lname);
1476:     PetscStrlen(lname, &len);
1477:   }
1478:   nameSize = len;
1479:   MPI_Bcast(&nameSize, 1, MPIU_INT, 0, comm);
1480:   PetscMalloc1(nameSize+1, &name);
1481:   if (!rank) {PetscArraycpy(name, lname, nameSize+1);}
1482:   MPI_Bcast(name, nameSize+1, MPI_CHAR, 0, comm);
1483:   DMLabelCreate(PETSC_COMM_SELF, name, labelNew);
1484:   PetscFree(name);
1485:   /* Bcast defaultValue */
1486:   if (!rank) (*labelNew)->defaultValue = label->defaultValue;
1487:   MPI_Bcast(&(*labelNew)->defaultValue, 1, MPIU_INT, 0, comm);
1488:   /* Distribute stratum values over the SF and get the point mapping on the receiver */
1489:   DMLabelDistribute_Internal(label, sf, &leafSection, &leafStrata);
1490:   /* Determine received stratum values and initialise new label*/
1491:   PetscHSetICreate(&stratumHash);
1492:   PetscSectionGetStorageSize(leafSection, &size);
1493:   for (p = 0; p < size; ++p) {PetscHSetIAdd(stratumHash, leafStrata[p]);}
1494:   PetscHSetIGetSize(stratumHash, &(*labelNew)->numStrata);
1495:   PetscMalloc1((*labelNew)->numStrata, &(*labelNew)->validIS);
1496:   for (s = 0; s < (*labelNew)->numStrata; ++s) (*labelNew)->validIS[s] = PETSC_TRUE;
1497:   PetscMalloc1((*labelNew)->numStrata, &(*labelNew)->stratumValues);
1498:   /* Turn leafStrata into indices rather than stratum values */
1499:   offset = 0;
1500:   PetscHSetIGetElems(stratumHash, &offset, (*labelNew)->stratumValues);
1501:   PetscSortInt((*labelNew)->numStrata,(*labelNew)->stratumValues);
1502:   for (s = 0; s < (*labelNew)->numStrata; ++s) {
1503:     PetscHMapISet((*labelNew)->hmap, (*labelNew)->stratumValues[s], s);
1504:   }
1505:   for (p = 0; p < size; ++p) {
1506:     for (s = 0; s < (*labelNew)->numStrata; ++s) {
1507:       if (leafStrata[p] == (*labelNew)->stratumValues[s]) {leafStrata[p] = s; break;}
1508:     }
1509:   }
1510:   /* Rebuild the point strata on the receiver */
1511:   PetscCalloc1((*labelNew)->numStrata,&(*labelNew)->stratumSizes);
1512:   PetscSectionGetChart(leafSection, &pStart, &pEnd);
1513:   for (p=pStart; p<pEnd; p++) {
1514:     PetscSectionGetDof(leafSection, p, &dof);
1515:     PetscSectionGetOffset(leafSection, p, &offset);
1516:     for (s=0; s<dof; s++) {
1517:       (*labelNew)->stratumSizes[leafStrata[offset+s]]++;
1518:     }
1519:   }
1520:   PetscCalloc1((*labelNew)->numStrata,&(*labelNew)->ht);
1521:   PetscMalloc1((*labelNew)->numStrata,&(*labelNew)->points);
1522:   PetscMalloc1((*labelNew)->numStrata,&points);
1523:   for (s = 0; s < (*labelNew)->numStrata; ++s) {
1524:     PetscHSetICreate(&(*labelNew)->ht[s]);
1525:     PetscMalloc1((*labelNew)->stratumSizes[s], &(points[s]));
1526:   }
1527:   /* Insert points into new strata */
1528:   PetscCalloc1((*labelNew)->numStrata, &strataIdx);
1529:   PetscSectionGetChart(leafSection, &pStart, &pEnd);
1530:   for (p=pStart; p<pEnd; p++) {
1531:     PetscSectionGetDof(leafSection, p, &dof);
1532:     PetscSectionGetOffset(leafSection, p, &offset);
1533:     for (s=0; s<dof; s++) {
1534:       stratum = leafStrata[offset+s];
1535:       points[stratum][strataIdx[stratum]++] = p;
1536:     }
1537:   }
1538:   for (s = 0; s < (*labelNew)->numStrata; s++) {
1539:     ISCreateGeneral(PETSC_COMM_SELF,(*labelNew)->stratumSizes[s],&(points[s][0]),PETSC_OWN_POINTER,&((*labelNew)->points[s]));
1540:     PetscObjectSetName((PetscObject)((*labelNew)->points[s]),"indices");
1541:   }
1542:   PetscFree(points);
1543:   PetscHSetIDestroy(&stratumHash);
1544:   PetscFree(leafStrata);
1545:   PetscFree(strataIdx);
1546:   PetscSectionDestroy(&leafSection);
1547:   return(0);
1548: }

1550: /*@
1551:   DMLabelGather - Gather all label values from leafs into roots

1553:   Collective on sf

1555:   Input Parameters:
1556: + label - the DMLabel
1557: - sf - the Star Forest point communication map

1559:   Output Parameters:
1560: . labelNew - the new DMLabel with localised leaf values

1562:   Level: developer

1564:   Note: This is the inverse operation to DMLabelDistribute.

1566: .seealso: DMLabelDistribute()
1567: @*/
1568: PetscErrorCode DMLabelGather(DMLabel label, PetscSF sf, DMLabel *labelNew)
1569: {
1570:   MPI_Comm       comm;
1571:   PetscSection   rootSection;
1572:   PetscSF        sfLabel;
1573:   PetscSFNode   *rootPoints, *leafPoints;
1574:   PetscInt       p, s, d, nroots, nleaves, nmultiroots, idx, dof, offset;
1575:   const PetscInt *rootDegree, *ilocal;
1576:   PetscInt       *rootStrata;
1577:   const char    *lname;
1578:   char          *name;
1579:   PetscInt       nameSize;
1580:   size_t         len = 0;
1581:   PetscMPIInt    rank, size;

1587:   PetscObjectGetComm((PetscObject)sf, &comm);
1588:   MPI_Comm_rank(comm, &rank);
1589:   MPI_Comm_size(comm, &size);
1590:   /* Bcast name */
1591:   if (!rank) {
1592:     PetscObjectGetName((PetscObject) label, &lname);
1593:     PetscStrlen(lname, &len);
1594:   }
1595:   nameSize = len;
1596:   MPI_Bcast(&nameSize, 1, MPIU_INT, 0, comm);
1597:   PetscMalloc1(nameSize+1, &name);
1598:   if (!rank) {PetscArraycpy(name, lname, nameSize+1);}
1599:   MPI_Bcast(name, nameSize+1, MPI_CHAR, 0, comm);
1600:   DMLabelCreate(PETSC_COMM_SELF, name, labelNew);
1601:   PetscFree(name);
1602:   /* Gather rank/index pairs of leaves into local roots to build
1603:      an inverse, multi-rooted SF. Note that this ignores local leaf
1604:      indexing due to the use of the multiSF in PetscSFGather. */
1605:   PetscSFGetGraph(sf, &nroots, &nleaves, &ilocal, NULL);
1606:   PetscMalloc1(nroots, &leafPoints);
1607:   for (p = 0; p < nroots; ++p) leafPoints[p].rank = leafPoints[p].index = -1;
1608:   for (p = 0; p < nleaves; p++) {
1609:     PetscInt ilp = ilocal ? ilocal[p] : p;

1611:     leafPoints[ilp].index = ilp;
1612:     leafPoints[ilp].rank  = rank;
1613:   }
1614:   PetscSFComputeDegreeBegin(sf, &rootDegree);
1615:   PetscSFComputeDegreeEnd(sf, &rootDegree);
1616:   for (p = 0, nmultiroots = 0; p < nroots; ++p) nmultiroots += rootDegree[p];
1617:   PetscMalloc1(nmultiroots, &rootPoints);
1618:   PetscSFGatherBegin(sf, MPIU_2INT, leafPoints, rootPoints);
1619:   PetscSFGatherEnd(sf, MPIU_2INT, leafPoints, rootPoints);
1620:   PetscSFCreate(comm,& sfLabel);
1621:   PetscSFSetGraph(sfLabel, nroots, nmultiroots, NULL, PETSC_OWN_POINTER, rootPoints, PETSC_OWN_POINTER);
1622:   /* Migrate label over inverted SF to pull stratum values at leaves into roots. */
1623:   DMLabelDistribute_Internal(label, sfLabel, &rootSection, &rootStrata);
1624:   /* Rebuild the point strata on the receiver */
1625:   for (p = 0, idx = 0; p < nroots; p++) {
1626:     for (d = 0; d < rootDegree[p]; d++) {
1627:       PetscSectionGetDof(rootSection, idx+d, &dof);
1628:       PetscSectionGetOffset(rootSection, idx+d, &offset);
1629:       for (s = 0; s < dof; s++) {DMLabelSetValue(*labelNew, p, rootStrata[offset+s]);}
1630:     }
1631:     idx += rootDegree[p];
1632:   }
1633:   PetscFree(leafPoints);
1634:   PetscFree(rootStrata);
1635:   PetscSectionDestroy(&rootSection);
1636:   PetscSFDestroy(&sfLabel);
1637:   return(0);
1638: }

1640: /*@
1641:   DMLabelConvertToSection - Make a PetscSection/IS pair that encodes the label

1643:   Not collective

1645:   Input Parameter:
1646: . label - the DMLabel

1648:   Output Parameters:
1649: + section - the section giving offsets for each stratum
1650: - is - An IS containing all the label points

1652:   Level: developer

1654: .seealso: DMLabelDistribute()
1655: @*/
1656: PetscErrorCode DMLabelConvertToSection(DMLabel label, PetscSection *section, IS *is)
1657: {
1658:   IS              vIS;
1659:   const PetscInt *values;
1660:   PetscInt       *points;
1661:   PetscInt        nV, vS = 0, vE = 0, v, N;
1662:   PetscErrorCode  ierr;

1666:   DMLabelGetNumValues(label, &nV);
1667:   DMLabelGetValueIS(label, &vIS);
1668:   ISGetIndices(vIS, &values);
1669:   if (nV) {vS = values[0]; vE = values[0]+1;}
1670:   for (v = 1; v < nV; ++v) {
1671:     vS = PetscMin(vS, values[v]);
1672:     vE = PetscMax(vE, values[v]+1);
1673:   }
1674:   PetscSectionCreate(PETSC_COMM_SELF, section);
1675:   PetscSectionSetChart(*section, vS, vE);
1676:   for (v = 0; v < nV; ++v) {
1677:     PetscInt n;

1679:     DMLabelGetStratumSize(label, values[v], &n);
1680:     PetscSectionSetDof(*section, values[v], n);
1681:   }
1682:   PetscSectionSetUp(*section);
1683:   PetscSectionGetStorageSize(*section, &N);
1684:   PetscMalloc1(N, &points);
1685:   for (v = 0; v < nV; ++v) {
1686:     IS              is;
1687:     const PetscInt *spoints;
1688:     PetscInt        dof, off, p;

1690:     PetscSectionGetDof(*section, values[v], &dof);
1691:     PetscSectionGetOffset(*section, values[v], &off);
1692:     DMLabelGetStratumIS(label, values[v], &is);
1693:     ISGetIndices(is, &spoints);
1694:     for (p = 0; p < dof; ++p) points[off+p] = spoints[p];
1695:     ISRestoreIndices(is, &spoints);
1696:     ISDestroy(&is);
1697:   }
1698:   ISRestoreIndices(vIS, &values);
1699:   ISDestroy(&vIS);
1700:   ISCreateGeneral(PETSC_COMM_SELF, N, points, PETSC_OWN_POINTER, is);
1701:   return(0);
1702: }

1704: /*@
1705:   PetscSectionCreateGlobalSectionLabel - Create a section describing the global field layout using
1706:   the local section and an SF describing the section point overlap.

1708:   Collective on sf

1710:   Input Parameters:
1711:   + s - The PetscSection for the local field layout
1712:   . sf - The SF describing parallel layout of the section points
1713:   . includeConstraints - By default this is PETSC_FALSE, meaning that the global field vector will not possess constrained dofs
1714:   . label - The label specifying the points
1715:   - labelValue - The label stratum specifying the points

1717:   Output Parameter:
1718:   . gsection - The PetscSection for the global field layout

1720:   Note: This gives negative sizes and offsets to points not owned by this process

1722:   Level: developer

1724: .seealso: PetscSectionCreate()
1725: @*/
1726: PetscErrorCode PetscSectionCreateGlobalSectionLabel(PetscSection s, PetscSF sf, PetscBool includeConstraints, DMLabel label, PetscInt labelValue, PetscSection *gsection)
1727: {
1728:   PetscInt      *neg = NULL, *tmpOff = NULL;
1729:   PetscInt       pStart, pEnd, p, dof, cdof, off, globalOff = 0, nroots;

1736:   PetscSectionCreate(PetscObjectComm((PetscObject) s), gsection);
1737:   PetscSectionGetChart(s, &pStart, &pEnd);
1738:   PetscSectionSetChart(*gsection, pStart, pEnd);
1739:   PetscSFGetGraph(sf, &nroots, NULL, NULL, NULL);
1740:   if (nroots >= 0) {
1741:     if (nroots < pEnd-pStart) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "PetscSF nroots %d < %d section size", nroots, pEnd-pStart);
1742:     PetscCalloc1(nroots, &neg);
1743:     if (nroots > pEnd-pStart) {
1744:       PetscCalloc1(nroots, &tmpOff);
1745:     } else {
1746:       tmpOff = &(*gsection)->atlasDof[-pStart];
1747:     }
1748:   }
1749:   /* Mark ghost points with negative dof */
1750:   for (p = pStart; p < pEnd; ++p) {
1751:     PetscInt value;

1753:     DMLabelGetValue(label, p, &value);
1754:     if (value != labelValue) continue;
1755:     PetscSectionGetDof(s, p, &dof);
1756:     PetscSectionSetDof(*gsection, p, dof);
1757:     PetscSectionGetConstraintDof(s, p, &cdof);
1758:     if (!includeConstraints && cdof > 0) {PetscSectionSetConstraintDof(*gsection, p, cdof);}
1759:     if (neg) neg[p] = -(dof+1);
1760:   }
1761:   PetscSectionSetUpBC(*gsection);
1762:   if (nroots >= 0) {
1763:     PetscSFBcastBegin(sf, MPIU_INT, neg, tmpOff);
1764:     PetscSFBcastEnd(sf, MPIU_INT, neg, tmpOff);
1765:     if (nroots > pEnd-pStart) {
1766:       for (p = pStart; p < pEnd; ++p) {if (tmpOff[p] < 0) (*gsection)->atlasDof[p-pStart] = tmpOff[p];}
1767:     }
1768:   }
1769:   /* Calculate new sizes, get proccess offset, and calculate point offsets */
1770:   for (p = 0, off = 0; p < pEnd-pStart; ++p) {
1771:     cdof = (!includeConstraints && s->bc) ? s->bc->atlasDof[p] : 0;
1772:     (*gsection)->atlasOff[p] = off;
1773:     off += (*gsection)->atlasDof[p] > 0 ? (*gsection)->atlasDof[p]-cdof : 0;
1774:   }
1775:   MPI_Scan(&off, &globalOff, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject) s));
1776:   globalOff -= off;
1777:   for (p = 0, off = 0; p < pEnd-pStart; ++p) {
1778:     (*gsection)->atlasOff[p] += globalOff;
1779:     if (neg) neg[p] = -((*gsection)->atlasOff[p]+1);
1780:   }
1781:   /* Put in negative offsets for ghost points */
1782:   if (nroots >= 0) {
1783:     PetscSFBcastBegin(sf, MPIU_INT, neg, tmpOff);
1784:     PetscSFBcastEnd(sf, MPIU_INT, neg, tmpOff);
1785:     if (nroots > pEnd-pStart) {
1786:       for (p = pStart; p < pEnd; ++p) {if (tmpOff[p] < 0) (*gsection)->atlasOff[p-pStart] = tmpOff[p];}
1787:     }
1788:   }
1789:   if (nroots >= 0 && nroots > pEnd-pStart) {PetscFree(tmpOff);}
1790:   PetscFree(neg);
1791:   return(0);
1792: }

1794: typedef struct _n_PetscSectionSym_Label
1795: {
1796:   DMLabel           label;
1797:   PetscCopyMode     *modes;
1798:   PetscInt          *sizes;
1799:   const PetscInt    ***perms;
1800:   const PetscScalar ***rots;
1801:   PetscInt          (*minMaxOrients)[2];
1802:   PetscInt          numStrata; /* numStrata is only increasing, functions as a state */
1803: } PetscSectionSym_Label;

1805: static PetscErrorCode PetscSectionSymLabelReset(PetscSectionSym sym)
1806: {
1807:   PetscInt              i, j;
1808:   PetscSectionSym_Label *sl = (PetscSectionSym_Label *) sym->data;
1809:   PetscErrorCode        ierr;

1812:   for (i = 0; i <= sl->numStrata; i++) {
1813:     if (sl->modes[i] == PETSC_OWN_POINTER || sl->modes[i] == PETSC_COPY_VALUES) {
1814:       for (j = sl->minMaxOrients[i][0]; j < sl->minMaxOrients[i][1]; j++) {
1815:         if (sl->perms[i]) {PetscFree(sl->perms[i][j]);}
1816:         if (sl->rots[i]) {PetscFree(sl->rots[i][j]);}
1817:       }
1818:       if (sl->perms[i]) {
1819:         const PetscInt **perms = &sl->perms[i][sl->minMaxOrients[i][0]];

1821:         PetscFree(perms);
1822:       }
1823:       if (sl->rots[i]) {
1824:         const PetscScalar **rots = &sl->rots[i][sl->minMaxOrients[i][0]];

1826:         PetscFree(rots);
1827:       }
1828:     }
1829:   }
1830:   PetscFree5(sl->modes,sl->sizes,sl->perms,sl->rots,sl->minMaxOrients);
1831:   DMLabelDestroy(&sl->label);
1832:   sl->numStrata = 0;
1833:   return(0);
1834: }

1836: static PetscErrorCode PetscSectionSymDestroy_Label(PetscSectionSym sym)
1837: {

1841:   PetscSectionSymLabelReset(sym);
1842:   PetscFree(sym->data);
1843:   return(0);
1844: }

1846: static PetscErrorCode PetscSectionSymView_Label(PetscSectionSym sym, PetscViewer viewer)
1847: {
1848:   PetscSectionSym_Label *sl = (PetscSectionSym_Label *) sym->data;
1849:   PetscBool             isAscii;
1850:   DMLabel               label = sl->label;
1851:   const char           *name;
1852:   PetscErrorCode        ierr;

1855:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &isAscii);
1856:   if (isAscii) {
1857:     PetscInt          i, j, k;
1858:     PetscViewerFormat format;

1860:     PetscViewerGetFormat(viewer,&format);
1861:     if (label) {
1862:       PetscViewerGetFormat(viewer,&format);
1863:       if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
1864:         PetscViewerASCIIPushTab(viewer);
1865:         DMLabelView(label, viewer);
1866:         PetscViewerASCIIPopTab(viewer);
1867:       } else {
1868:         PetscObjectGetName((PetscObject) sl->label, &name);
1869:         PetscViewerASCIIPrintf(viewer,"  Label '%s'\n",name);
1870:       }
1871:     } else {
1872:       PetscViewerASCIIPrintf(viewer, "No label given\n");
1873:     }
1874:     PetscViewerASCIIPushTab(viewer);
1875:     for (i = 0; i <= sl->numStrata; i++) {
1876:       PetscInt value = i < sl->numStrata ? label->stratumValues[i] : label->defaultValue;

1878:       if (!(sl->perms[i] || sl->rots[i])) {
1879:         PetscViewerASCIIPrintf(viewer, "Symmetry for stratum value %D (%D dofs per point): no symmetries\n", value, sl->sizes[i]);
1880:       } else {
1881:       PetscViewerASCIIPrintf(viewer, "Symmetry for stratum value %D (%D dofs per point):\n", value, sl->sizes[i]);
1882:         PetscViewerASCIIPushTab(viewer);
1883:         PetscViewerASCIIPrintf(viewer, "Orientation range: [%D, %D)\n", sl->minMaxOrients[i][0], sl->minMaxOrients[i][1]);
1884:         if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
1885:           PetscViewerASCIIPushTab(viewer);
1886:           for (j = sl->minMaxOrients[i][0]; j < sl->minMaxOrients[i][1]; j++) {
1887:             if (!((sl->perms[i] && sl->perms[i][j]) || (sl->rots[i] && sl->rots[i][j]))) {
1888:               PetscViewerASCIIPrintf(viewer, "Orientation %D: identity\n",j);
1889:             } else {
1890:               PetscInt tab;

1892:               PetscViewerASCIIPrintf(viewer, "Orientation %D:\n",j);
1893:               PetscViewerASCIIPushTab(viewer);
1894:               PetscViewerASCIIGetTab(viewer,&tab);
1895:               if (sl->perms[i] && sl->perms[i][j]) {
1896:                 PetscViewerASCIIPrintf(viewer,"Permutation:");
1897:                 PetscViewerASCIISetTab(viewer,0);
1898:                 for (k = 0; k < sl->sizes[i]; k++) {PetscViewerASCIIPrintf(viewer," %D",sl->perms[i][j][k]);}
1899:                 PetscViewerASCIIPrintf(viewer,"\n");
1900:                 PetscViewerASCIISetTab(viewer,tab);
1901:               }
1902:               if (sl->rots[i] && sl->rots[i][j]) {
1903:                 PetscViewerASCIIPrintf(viewer,"Rotations:  ");
1904:                 PetscViewerASCIISetTab(viewer,0);
1905: #if defined(PETSC_USE_COMPLEX)
1906:                 for (k = 0; k < sl->sizes[i]; k++) {PetscViewerASCIIPrintf(viewer," %+f+i*%+f",PetscRealPart(sl->rots[i][j][k]),PetscImaginaryPart(sl->rots[i][j][k]));}
1907: #else
1908:                 for (k = 0; k < sl->sizes[i]; k++) {PetscViewerASCIIPrintf(viewer," %+f",sl->rots[i][j][k]);}
1909: #endif
1910:                 PetscViewerASCIIPrintf(viewer,"\n");
1911:                 PetscViewerASCIISetTab(viewer,tab);
1912:               }
1913:               PetscViewerASCIIPopTab(viewer);
1914:             }
1915:           }
1916:           PetscViewerASCIIPopTab(viewer);
1917:         }
1918:         PetscViewerASCIIPopTab(viewer);
1919:       }
1920:     }
1921:     PetscViewerASCIIPopTab(viewer);
1922:   }
1923:   return(0);
1924: }

1926: /*@
1927:   PetscSectionSymLabelSetLabel - set the label whose strata will define the points that receive symmetries

1929:   Logically collective on sym

1931:   Input parameters:
1932: + sym - the section symmetries
1933: - label - the DMLabel describing the types of points

1935:   Level: developer:

1937: .seealso: PetscSectionSymLabelSetStratum(), PetscSectionSymCreateLabel(), PetscSectionGetPointSyms()
1938: @*/
1939: PetscErrorCode PetscSectionSymLabelSetLabel(PetscSectionSym sym, DMLabel label)
1940: {
1941:   PetscSectionSym_Label *sl;
1942:   PetscErrorCode        ierr;

1946:   sl = (PetscSectionSym_Label *) sym->data;
1947:   if (sl->label && sl->label != label) {PetscSectionSymLabelReset(sym);}
1948:   if (label) {
1949:     sl->label = label;
1950:     PetscObjectReference((PetscObject) label);
1951:     DMLabelGetNumValues(label,&sl->numStrata);
1952:     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);
1953:     PetscMemzero((void *) sl->modes,(sl->numStrata+1)*sizeof(PetscCopyMode));
1954:     PetscMemzero((void *) sl->sizes,(sl->numStrata+1)*sizeof(PetscInt));
1955:     PetscMemzero((void *) sl->perms,(sl->numStrata+1)*sizeof(const PetscInt **));
1956:     PetscMemzero((void *) sl->rots,(sl->numStrata+1)*sizeof(const PetscScalar **));
1957:     PetscMemzero((void *) sl->minMaxOrients,(sl->numStrata+1)*sizeof(PetscInt[2]));
1958:   }
1959:   return(0);
1960: }

1962: /*@C
1963:   PetscSectionSymLabelSetStratum - set the symmetries for the orientations of a stratum

1965:   Logically collective on sym

1967:   InputParameters:
1968: + sym       - the section symmetries
1969: . stratum   - the stratum value in the label that we are assigning symmetries for
1970: . size      - the number of dofs for points in the stratum of the label
1971: . minOrient - the smallest orientation for a point in this stratum
1972: . maxOrient - one greater than the largest orientation for a ppoint in this stratum (i.e., orientations are in the range [minOrient, maxOrient))
1973: . mode      - how sym should copy the perms and rots arrays
1974: . perms     - NULL if there are no permutations, or (maxOrient - minOrient) permutations, one for each orientation.  A NULL permutation is the identity
1975: - 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

1977:   Level: developer

1979: .seealso: PetscSectionSymCreate(), PetscSectionSetSym(), PetscSectionGetPointSyms(), PetscSectionSymCreateLabel()
1980: @*/
1981: PetscErrorCode PetscSectionSymLabelSetStratum(PetscSectionSym sym, PetscInt stratum, PetscInt size, PetscInt minOrient, PetscInt maxOrient, PetscCopyMode mode, const PetscInt **perms, const PetscScalar **rots)
1982: {
1983:   PetscSectionSym_Label *sl;
1984:   const char            *name;
1985:   PetscInt               i, j, k;
1986:   PetscErrorCode         ierr;

1990:   sl = (PetscSectionSym_Label *) sym->data;
1991:   if (!sl->label) SETERRQ(PetscObjectComm((PetscObject)sym),PETSC_ERR_ARG_WRONGSTATE,"No label set yet");
1992:   for (i = 0; i <= sl->numStrata; i++) {
1993:     PetscInt value = (i < sl->numStrata) ? sl->label->stratumValues[i] : sl->label->defaultValue;

1995:     if (stratum == value) break;
1996:   }
1997:   PetscObjectGetName((PetscObject) sl->label, &name);
1998:   if (i > sl->numStrata) SETERRQ2(PetscObjectComm((PetscObject)sym),PETSC_ERR_ARG_OUTOFRANGE,"Stratum %D not found in label %s\n",stratum,name);
1999:   sl->sizes[i] = size;
2000:   sl->modes[i] = mode;
2001:   sl->minMaxOrients[i][0] = minOrient;
2002:   sl->minMaxOrients[i][1] = maxOrient;
2003:   if (mode == PETSC_COPY_VALUES) {
2004:     if (perms) {
2005:       PetscInt    **ownPerms;

2007:       PetscCalloc1(maxOrient - minOrient,&ownPerms);
2008:       for (j = 0; j < maxOrient-minOrient; j++) {
2009:         if (perms[j]) {
2010:           PetscMalloc1(size,&ownPerms[j]);
2011:           for (k = 0; k < size; k++) {ownPerms[j][k] = perms[j][k];}
2012:         }
2013:       }
2014:       sl->perms[i] = (const PetscInt **) &ownPerms[-minOrient];
2015:     }
2016:     if (rots) {
2017:       PetscScalar **ownRots;

2019:       PetscCalloc1(maxOrient - minOrient,&ownRots);
2020:       for (j = 0; j < maxOrient-minOrient; j++) {
2021:         if (rots[j]) {
2022:           PetscMalloc1(size,&ownRots[j]);
2023:           for (k = 0; k < size; k++) {ownRots[j][k] = rots[j][k];}
2024:         }
2025:       }
2026:       sl->rots[i] = (const PetscScalar **) &ownRots[-minOrient];
2027:     }
2028:   } else {
2029:     sl->perms[i] = perms ? &perms[-minOrient] : NULL;
2030:     sl->rots[i]  = rots ? &rots[-minOrient] : NULL;
2031:   }
2032:   return(0);
2033: }

2035: static PetscErrorCode PetscSectionSymGetPoints_Label(PetscSectionSym sym, PetscSection section, PetscInt numPoints, const PetscInt *points, const PetscInt **perms, const PetscScalar **rots)
2036: {
2037:   PetscInt              i, j, numStrata;
2038:   PetscSectionSym_Label *sl;
2039:   DMLabel               label;
2040:   PetscErrorCode        ierr;

2043:   sl = (PetscSectionSym_Label *) sym->data;
2044:   numStrata = sl->numStrata;
2045:   label     = sl->label;
2046:   for (i = 0; i < numPoints; i++) {
2047:     PetscInt point = points[2*i];
2048:     PetscInt ornt  = points[2*i+1];

2050:     for (j = 0; j < numStrata; j++) {
2051:       if (label->validIS[j]) {
2052:         PetscInt k;

2054:         ISLocate(label->points[j],point,&k);
2055:         if (k >= 0) break;
2056:       } else {
2057:         PetscBool has;

2059:         PetscHSetIHas(label->ht[j], point, &has);
2060:         if (has) break;
2061:       }
2062:     }
2063:     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);
2064:     if (perms) {perms[i] = sl->perms[j] ? sl->perms[j][ornt] : NULL;}
2065:     if (rots) {rots[i]  = sl->rots[j] ? sl->rots[j][ornt] : NULL;}
2066:   }
2067:   return(0);
2068: }

2070: PetscErrorCode PetscSectionSymCreate_Label(PetscSectionSym sym)
2071: {
2072:   PetscSectionSym_Label *sl;
2073:   PetscErrorCode        ierr;

2076:   PetscNewLog(sym,&sl);
2077:   sym->ops->getpoints = PetscSectionSymGetPoints_Label;
2078:   sym->ops->view      = PetscSectionSymView_Label;
2079:   sym->ops->destroy   = PetscSectionSymDestroy_Label;
2080:   sym->data           = (void *) sl;
2081:   return(0);
2082: }

2084: /*@
2085:   PetscSectionSymCreateLabel - Create a section symmetry that assigns one symmetry to each stratum of a label

2087:   Collective

2089:   Input Parameters:
2090: + comm - the MPI communicator for the new symmetry
2091: - label - the label defining the strata

2093:   Output Parameters:
2094: . sym - the section symmetries

2096:   Level: developer

2098: .seealso: PetscSectionSymCreate(), PetscSectionSetSym(), PetscSectionGetSym(), PetscSectionSymLabelSetStratum(), PetscSectionGetPointSyms()
2099: @*/
2100: PetscErrorCode PetscSectionSymCreateLabel(MPI_Comm comm, DMLabel label, PetscSectionSym *sym)
2101: {
2102:   PetscErrorCode        ierr;

2105:   DMInitializePackage();
2106:   PetscSectionSymCreate(comm,sym);
2107:   PetscSectionSymSetType(*sym,PETSCSECTIONSYMLABEL);
2108:   PetscSectionSymLabelSetLabel(*sym,label);
2109:   return(0);
2110: }