Actual source code: plex.c

  1: #include <petsc/private/dmpleximpl.h>
  2: #include <petsc/private/dmlabelimpl.h>
  3: #include <petsc/private/isimpl.h>
  4: #include <petsc/private/vecimpl.h>
  5: #include <petsc/private/glvisvecimpl.h>
  6: #include <petscsf.h>
  7: #include <petscds.h>
  8: #include <petscdraw.h>
  9: #include <petscdmfield.h>
 10: #include <petscdmplextransform.h>

 12: /* Logging support */
 13: PetscLogEvent DMPLEX_Interpolate, DMPLEX_Partition, DMPLEX_Distribute, DMPLEX_DistributeCones, DMPLEX_DistributeLabels, DMPLEX_DistributeSF, DMPLEX_DistributeOverlap, DMPLEX_DistributeField, DMPLEX_DistributeData, DMPLEX_Migrate, DMPLEX_InterpolateSF, DMPLEX_GlobalToNaturalBegin, DMPLEX_GlobalToNaturalEnd, DMPLEX_NaturalToGlobalBegin, DMPLEX_NaturalToGlobalEnd, DMPLEX_Stratify, DMPLEX_Symmetrize, DMPLEX_Preallocate, DMPLEX_ResidualFEM, DMPLEX_JacobianFEM, DMPLEX_InterpolatorFEM, DMPLEX_InjectorFEM, DMPLEX_IntegralFEM, DMPLEX_CreateGmsh, DMPLEX_RebalanceSharedPoints, DMPLEX_PartSelf, DMPLEX_PartLabelInvert, DMPLEX_PartLabelCreateSF, DMPLEX_PartStratSF, DMPLEX_CreatePointSF, DMPLEX_LocatePoints, DMPLEX_TopologyView, DMPLEX_LabelsView, DMPLEX_CoordinatesView, DMPLEX_SectionView, DMPLEX_GlobalVectorView, DMPLEX_LocalVectorView, DMPLEX_TopologyLoad, DMPLEX_LabelsLoad, DMPLEX_CoordinatesLoad, DMPLEX_SectionLoad, DMPLEX_GlobalVectorLoad, DMPLEX_LocalVectorLoad;
 14: PetscLogEvent DMPLEX_RebalBuildGraph, DMPLEX_RebalRewriteSF, DMPLEX_RebalGatherGraph, DMPLEX_RebalPartition, DMPLEX_RebalScatterPart;

 16: PETSC_EXTERN PetscErrorCode VecView_MPI(Vec, PetscViewer);

 18: /*@
 19:   DMPlexIsSimplex - Is the first cell in this mesh a simplex?

 21:   Input Parameter:
 22: . dm      - The `DMPLEX` object

 24:   Output Parameter:
 25: . simplex - Flag checking for a simplex

 27:   Level: intermediate

 29:   Note:
 30:   This just gives the first range of cells found. If the mesh has several cell types, it will only give the first.
 31:   If the mesh has no cells, this returns `PETSC_FALSE`.

 33: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetSimplexOrBoxCells()`, `DMPlexGetCellType()`, `DMPlexGetHeightStratum()`, `DMPolytopeTypeGetNumVertices()`
 34: @*/
 35: PetscErrorCode DMPlexIsSimplex(DM dm, PetscBool *simplex)
 36: {
 37:   DMPolytopeType ct;
 38:   PetscInt       cStart, cEnd;

 40:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
 41:   if (cEnd <= cStart) {
 42:     *simplex = PETSC_FALSE;
 43:     return 0;
 44:   }
 45:   DMPlexGetCellType(dm, cStart, &ct);
 46:   *simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
 47:   return 0;
 48: }

 50: /*@
 51:   DMPlexGetSimplexOrBoxCells - Get the range of cells which are neither prisms nor ghost FV cells

 53:   Input Parameters:
 54: + dm     - The `DMPLEX` object
 55: - height - The cell height in the Plex, 0 is the default

 57:   Output Parameters:
 58: + cStart - The first "normal" cell
 59: - cEnd   - The upper bound on "normal"" cells

 61:   Level: developer

 63:   Note:
 64:   This just gives the first range of cells found. If the mesh has several cell types, it will only give the first.

 66: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexConstructGhostCells()`, `DMPlexGetGhostCellStratum()`
 67: @*/
 68: PetscErrorCode DMPlexGetSimplexOrBoxCells(DM dm, PetscInt height, PetscInt *cStart, PetscInt *cEnd)
 69: {
 70:   DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
 71:   PetscInt       cS, cE, c;

 73:   DMPlexGetHeightStratum(dm, PetscMax(height, 0), &cS, &cE);
 74:   for (c = cS; c < cE; ++c) {
 75:     DMPolytopeType cct;

 77:     DMPlexGetCellType(dm, c, &cct);
 78:     if ((PetscInt)cct < 0) break;
 79:     switch (cct) {
 80:     case DM_POLYTOPE_POINT:
 81:     case DM_POLYTOPE_SEGMENT:
 82:     case DM_POLYTOPE_TRIANGLE:
 83:     case DM_POLYTOPE_QUADRILATERAL:
 84:     case DM_POLYTOPE_TETRAHEDRON:
 85:     case DM_POLYTOPE_HEXAHEDRON:
 86:       ct = cct;
 87:       break;
 88:     default:
 89:       break;
 90:     }
 91:     if (ct != DM_POLYTOPE_UNKNOWN) break;
 92:   }
 93:   if (ct != DM_POLYTOPE_UNKNOWN) {
 94:     DMLabel ctLabel;

 96:     DMPlexGetCellTypeLabel(dm, &ctLabel);
 97:     DMLabelGetStratumBounds(ctLabel, ct, &cS, &cE);
 98:     // Reset label for fast lookup
 99:     DMLabelMakeAllInvalid_Internal(ctLabel);
100:   }
101:   if (cStart) *cStart = cS;
102:   if (cEnd) *cEnd = cE;
103:   return 0;
104: }

106: PetscErrorCode DMPlexGetFieldType_Internal(DM dm, PetscSection section, PetscInt field, PetscInt *sStart, PetscInt *sEnd, PetscViewerVTKFieldType *ft)
107: {
108:   PetscInt cdim, pStart, pEnd, vStart, vEnd, cStart, cEnd;
109:   PetscInt vcdof[2] = {0, 0}, globalvcdof[2];

111:   *ft = PETSC_VTK_INVALID;
112:   DMGetCoordinateDim(dm, &cdim);
113:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
114:   DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);
115:   PetscSectionGetChart(section, &pStart, &pEnd);
116:   if (field >= 0) {
117:     if ((vStart >= pStart) && (vStart < pEnd)) PetscSectionGetFieldDof(section, vStart, field, &vcdof[0]);
118:     if ((cStart >= pStart) && (cStart < pEnd)) PetscSectionGetFieldDof(section, cStart, field, &vcdof[1]);
119:   } else {
120:     if ((vStart >= pStart) && (vStart < pEnd)) PetscSectionGetDof(section, vStart, &vcdof[0]);
121:     if ((cStart >= pStart) && (cStart < pEnd)) PetscSectionGetDof(section, cStart, &vcdof[1]);
122:   }
123:   MPI_Allreduce(vcdof, globalvcdof, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));
124:   if (globalvcdof[0]) {
125:     *sStart = vStart;
126:     *sEnd   = vEnd;
127:     if (globalvcdof[0] == cdim) *ft = PETSC_VTK_POINT_VECTOR_FIELD;
128:     else *ft = PETSC_VTK_POINT_FIELD;
129:   } else if (globalvcdof[1]) {
130:     *sStart = cStart;
131:     *sEnd   = cEnd;
132:     if (globalvcdof[1] == cdim) *ft = PETSC_VTK_CELL_VECTOR_FIELD;
133:     else *ft = PETSC_VTK_CELL_FIELD;
134:   } else {
135:     if (field >= 0) {
136:       const char *fieldname;

138:       PetscSectionGetFieldName(section, field, &fieldname);
139:       PetscInfo((PetscObject)dm, "Could not classify VTK output type of section field %" PetscInt_FMT " \"%s\"\n", field, fieldname);
140:     } else {
141:       PetscInfo((PetscObject)dm, "Could not classify VTK output type of section\n");
142:     }
143:   }
144:   return 0;
145: }

147: /*@
148:   DMPlexVecView1D - Plot many 1D solutions on the same line graph

150:   Collective on dm

152:   Input Parameters:
153: + dm - The `DMPLEX` object
154: . n  - The number of vectors
155: . u  - The array of local vectors
156: - viewer - The `PetscViewer`

158:   Level: advanced

160: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `VecViewFromOptions()`, `VecView()`
161: @*/
162: PetscErrorCode DMPlexVecView1D(DM dm, PetscInt n, Vec u[], PetscViewer viewer)
163: {
164:   PetscDS            ds;
165:   PetscDraw          draw = NULL;
166:   PetscDrawLG        lg;
167:   Vec                coordinates;
168:   const PetscScalar *coords, **sol;
169:   PetscReal         *vals;
170:   PetscInt          *Nc;
171:   PetscInt           Nf, f, c, Nl, l, i, vStart, vEnd, v;
172:   char             **names;

174:   DMGetDS(dm, &ds);
175:   PetscDSGetNumFields(ds, &Nf);
176:   PetscDSGetTotalComponents(ds, &Nl);
177:   PetscDSGetComponents(ds, &Nc);

179:   PetscViewerDrawGetDraw(viewer, 0, &draw);
180:   if (!draw) return 0;
181:   PetscDrawLGCreate(draw, n * Nl, &lg);

183:   PetscMalloc3(n, &sol, n * Nl, &names, n * Nl, &vals);
184:   for (i = 0, l = 0; i < n; ++i) {
185:     const char *vname;

187:     PetscObjectGetName((PetscObject)u[i], &vname);
188:     for (f = 0; f < Nf; ++f) {
189:       PetscObject disc;
190:       const char *fname;
191:       char        tmpname[PETSC_MAX_PATH_LEN];

193:       PetscDSGetDiscretization(ds, f, &disc);
194:       /* TODO Create names for components */
195:       for (c = 0; c < Nc[f]; ++c, ++l) {
196:         PetscObjectGetName(disc, &fname);
197:         PetscStrcpy(tmpname, vname);
198:         PetscStrlcat(tmpname, ":", PETSC_MAX_PATH_LEN);
199:         PetscStrlcat(tmpname, fname, PETSC_MAX_PATH_LEN);
200:         PetscStrallocpy(tmpname, &names[l]);
201:       }
202:     }
203:   }
204:   PetscDrawLGSetLegend(lg, (const char *const *)names);
205:   /* Just add P_1 support for now */
206:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
207:   DMGetCoordinatesLocal(dm, &coordinates);
208:   VecGetArrayRead(coordinates, &coords);
209:   for (i = 0; i < n; ++i) VecGetArrayRead(u[i], &sol[i]);
210:   for (v = vStart; v < vEnd; ++v) {
211:     PetscScalar *x, *svals;

213:     DMPlexPointLocalRead(dm, v, coords, &x);
214:     for (i = 0; i < n; ++i) {
215:       DMPlexPointLocalRead(dm, v, sol[i], &svals);
216:       for (l = 0; l < Nl; ++l) vals[i * Nl + l] = PetscRealPart(svals[l]);
217:     }
218:     PetscDrawLGAddCommonPoint(lg, PetscRealPart(x[0]), vals);
219:   }
220:   VecRestoreArrayRead(coordinates, &coords);
221:   for (i = 0; i < n; ++i) VecRestoreArrayRead(u[i], &sol[i]);
222:   for (l = 0; l < n * Nl; ++l) PetscFree(names[l]);
223:   PetscFree3(sol, names, vals);

225:   PetscDrawLGDraw(lg);
226:   PetscDrawLGDestroy(&lg);
227:   return 0;
228: }

230: static PetscErrorCode VecView_Plex_Local_Draw_1D(Vec u, PetscViewer viewer)
231: {
232:   DM dm;

234:   VecGetDM(u, &dm);
235:   DMPlexVecView1D(dm, 1, &u, viewer);
236:   return 0;
237: }

239: static PetscErrorCode VecView_Plex_Local_Draw_2D(Vec v, PetscViewer viewer)
240: {
241:   DM                 dm;
242:   PetscSection       s;
243:   PetscDraw          draw, popup;
244:   DM                 cdm;
245:   PetscSection       coordSection;
246:   Vec                coordinates;
247:   const PetscScalar *coords, *array;
248:   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
249:   PetscReal          vbound[2], time;
250:   PetscBool          flg;
251:   PetscInt           dim, Nf, f, Nc, comp, vStart, vEnd, cStart, cEnd, c, N, level, step, w = 0;
252:   const char        *name;
253:   char               title[PETSC_MAX_PATH_LEN];

255:   PetscViewerDrawGetDraw(viewer, 0, &draw);
256:   VecGetDM(v, &dm);
257:   DMGetCoordinateDim(dm, &dim);
258:   DMGetLocalSection(dm, &s);
259:   PetscSectionGetNumFields(s, &Nf);
260:   DMGetCoarsenLevel(dm, &level);
261:   DMGetCoordinateDM(dm, &cdm);
262:   DMGetLocalSection(cdm, &coordSection);
263:   DMGetCoordinatesLocal(dm, &coordinates);
264:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
265:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);

267:   PetscObjectGetName((PetscObject)v, &name);
268:   DMGetOutputSequenceNumber(dm, &step, &time);

270:   VecGetLocalSize(coordinates, &N);
271:   VecGetArrayRead(coordinates, &coords);
272:   for (c = 0; c < N; c += dim) {
273:     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));
274:     bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
275:     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c + 1]));
276:     bound[3] = PetscMax(bound[3], PetscRealPart(coords[c + 1]));
277:   }
278:   VecRestoreArrayRead(coordinates, &coords);
279:   PetscDrawClear(draw);

281:   /* Could implement something like DMDASelectFields() */
282:   for (f = 0; f < Nf; ++f) {
283:     DM          fdm = dm;
284:     Vec         fv  = v;
285:     IS          fis;
286:     char        prefix[PETSC_MAX_PATH_LEN];
287:     const char *fname;

289:     PetscSectionGetFieldComponents(s, f, &Nc);
290:     PetscSectionGetFieldName(s, f, &fname);

292:     if (v->hdr.prefix) PetscStrncpy(prefix, v->hdr.prefix, sizeof(prefix));
293:     else prefix[0] = '\0';
294:     if (Nf > 1) {
295:       DMCreateSubDM(dm, 1, &f, &fis, &fdm);
296:       VecGetSubVector(v, fis, &fv);
297:       PetscStrlcat(prefix, fname, sizeof(prefix));
298:       PetscStrlcat(prefix, "_", sizeof(prefix));
299:     }
300:     for (comp = 0; comp < Nc; ++comp, ++w) {
301:       PetscInt nmax = 2;

303:       PetscViewerDrawGetDraw(viewer, w, &draw);
304:       if (Nc > 1) PetscSNPrintf(title, sizeof(title), "%s:%s_%" PetscInt_FMT " Step: %" PetscInt_FMT " Time: %.4g", name, fname, comp, step, (double)time);
305:       else PetscSNPrintf(title, sizeof(title), "%s:%s Step: %" PetscInt_FMT " Time: %.4g", name, fname, step, (double)time);
306:       PetscDrawSetTitle(draw, title);

308:       /* TODO Get max and min only for this component */
309:       PetscOptionsGetRealArray(NULL, prefix, "-vec_view_bounds", vbound, &nmax, &flg);
310:       if (!flg) {
311:         VecMin(fv, NULL, &vbound[0]);
312:         VecMax(fv, NULL, &vbound[1]);
313:         if (vbound[1] <= vbound[0]) vbound[1] = vbound[0] + 1.0;
314:       }
315:       PetscDrawGetPopup(draw, &popup);
316:       PetscDrawScalePopup(popup, vbound[0], vbound[1]);
317:       PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);

319:       VecGetArrayRead(fv, &array);
320:       for (c = cStart; c < cEnd; ++c) {
321:         PetscScalar *coords = NULL, *a   = NULL;
322:         PetscInt     numCoords, color[4] = {-1, -1, -1, -1};

324:         DMPlexPointLocalRead(fdm, c, array, &a);
325:         if (a) {
326:           color[0] = PetscDrawRealToColor(PetscRealPart(a[comp]), vbound[0], vbound[1]);
327:           color[1] = color[2] = color[3] = color[0];
328:         } else {
329:           PetscScalar *vals = NULL;
330:           PetscInt     numVals, va;

332:           DMPlexVecGetClosure(fdm, NULL, fv, c, &numVals, &vals);
334:           switch (numVals / Nc) {
335:           case 3: /* P1 Triangle */
336:           case 4: /* P1 Quadrangle */
337:             for (va = 0; va < numVals / Nc; ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va * Nc + comp]), vbound[0], vbound[1]);
338:             break;
339:           case 6: /* P2 Triangle */
340:           case 8: /* P2 Quadrangle */
341:             for (va = 0; va < numVals / (Nc * 2); ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va * Nc + comp + numVals / (Nc * 2)]), vbound[0], vbound[1]);
342:             break;
343:           default:
344:             SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of values for cell closure %" PetscInt_FMT " cannot be handled", numVals / Nc);
345:           }
346:           DMPlexVecRestoreClosure(fdm, NULL, fv, c, &numVals, &vals);
347:         }
348:         DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);
349:         switch (numCoords) {
350:         case 6:
351:         case 12: /* Localized triangle */
352:           PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), color[0], color[1], color[2]);
353:           break;
354:         case 8:
355:         case 16: /* Localized quadrilateral */
356:           PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), color[0], color[1], color[2]);
357:           PetscDrawTriangle(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), color[2], color[3], color[0]);
358:           break;
359:         default:
360:           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %" PetscInt_FMT " coordinates", numCoords);
361:         }
362:         DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);
363:       }
364:       VecRestoreArrayRead(fv, &array);
365:       PetscDrawFlush(draw);
366:       PetscDrawPause(draw);
367:       PetscDrawSave(draw);
368:     }
369:     if (Nf > 1) {
370:       VecRestoreSubVector(v, fis, &fv);
371:       ISDestroy(&fis);
372:       DMDestroy(&fdm);
373:     }
374:   }
375:   return 0;
376: }

378: static PetscErrorCode VecView_Plex_Local_Draw(Vec v, PetscViewer viewer)
379: {
380:   DM        dm;
381:   PetscDraw draw;
382:   PetscInt  dim;
383:   PetscBool isnull;

385:   PetscViewerDrawGetDraw(viewer, 0, &draw);
386:   PetscDrawIsNull(draw, &isnull);
387:   if (isnull) return 0;

389:   VecGetDM(v, &dm);
390:   DMGetCoordinateDim(dm, &dim);
391:   switch (dim) {
392:   case 1:
393:     VecView_Plex_Local_Draw_1D(v, viewer);
394:     break;
395:   case 2:
396:     VecView_Plex_Local_Draw_2D(v, viewer);
397:     break;
398:   default:
399:     SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_SUP, "Cannot draw meshes of dimension %" PetscInt_FMT ". Try PETSCVIEWERGLVIS", dim);
400:   }
401:   return 0;
402: }

404: static PetscErrorCode VecView_Plex_Local_VTK(Vec v, PetscViewer viewer)
405: {
406:   DM                      dm;
407:   Vec                     locv;
408:   const char             *name;
409:   PetscSection            section;
410:   PetscInt                pStart, pEnd;
411:   PetscInt                numFields;
412:   PetscViewerVTKFieldType ft;

414:   VecGetDM(v, &dm);
415:   DMCreateLocalVector(dm, &locv); /* VTK viewer requires exclusive ownership of the vector */
416:   PetscObjectGetName((PetscObject)v, &name);
417:   PetscObjectSetName((PetscObject)locv, name);
418:   VecCopy(v, locv);
419:   DMGetLocalSection(dm, &section);
420:   PetscSectionGetNumFields(section, &numFields);
421:   if (!numFields) {
422:     DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft);
423:     PetscViewerVTKAddField(viewer, (PetscObject)dm, DMPlexVTKWriteAll, PETSC_DEFAULT, ft, PETSC_TRUE, (PetscObject)locv);
424:   } else {
425:     PetscInt f;

427:     for (f = 0; f < numFields; f++) {
428:       DMPlexGetFieldType_Internal(dm, section, f, &pStart, &pEnd, &ft);
429:       if (ft == PETSC_VTK_INVALID) continue;
430:       PetscObjectReference((PetscObject)locv);
431:       PetscViewerVTKAddField(viewer, (PetscObject)dm, DMPlexVTKWriteAll, f, ft, PETSC_TRUE, (PetscObject)locv);
432:     }
433:     VecDestroy(&locv);
434:   }
435:   return 0;
436: }

438: PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
439: {
440:   DM        dm;
441:   PetscBool isvtk, ishdf5, isdraw, isglvis, iscgns;

443:   VecGetDM(v, &dm);
445:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERVTK, &isvtk);
446:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
447:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw);
448:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERGLVIS, &isglvis);
449:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERCGNS, &iscgns);
450:   if (isvtk || ishdf5 || isdraw || isglvis || iscgns) {
451:     PetscInt    i, numFields;
452:     PetscObject fe;
453:     PetscBool   fem  = PETSC_FALSE;
454:     Vec         locv = v;
455:     const char *name;
456:     PetscInt    step;
457:     PetscReal   time;

459:     DMGetNumFields(dm, &numFields);
460:     for (i = 0; i < numFields; i++) {
461:       DMGetField(dm, i, NULL, &fe);
462:       if (fe->classid == PETSCFE_CLASSID) {
463:         fem = PETSC_TRUE;
464:         break;
465:       }
466:     }
467:     if (fem) {
468:       PetscObject isZero;

470:       DMGetLocalVector(dm, &locv);
471:       PetscObjectGetName((PetscObject)v, &name);
472:       PetscObjectSetName((PetscObject)locv, name);
473:       PetscObjectQuery((PetscObject)v, "__Vec_bc_zero__", &isZero);
474:       PetscObjectCompose((PetscObject)locv, "__Vec_bc_zero__", isZero);
475:       VecCopy(v, locv);
476:       DMGetOutputSequenceNumber(dm, NULL, &time);
477:       DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);
478:     }
479:     if (isvtk) {
480:       VecView_Plex_Local_VTK(locv, viewer);
481:     } else if (ishdf5) {
482: #if defined(PETSC_HAVE_HDF5)
483:       VecView_Plex_Local_HDF5_Internal(locv, viewer);
484: #else
485:       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
486: #endif
487:     } else if (isdraw) {
488:       VecView_Plex_Local_Draw(locv, viewer);
489:     } else if (isglvis) {
490:       DMGetOutputSequenceNumber(dm, &step, NULL);
491:       PetscViewerGLVisSetSnapId(viewer, step);
492:       VecView_GLVis(locv, viewer);
493:     } else if (iscgns) {
494: #if defined(PETSC_HAVE_CGNS)
495:       VecView_Plex_Local_CGNS(locv, viewer);
496: #else
497:       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "CGNS not supported in this build.\nPlease reconfigure using --download-cgns");
498: #endif
499:     }
500:     if (fem) {
501:       PetscObjectCompose((PetscObject)locv, "__Vec_bc_zero__", NULL);
502:       DMRestoreLocalVector(dm, &locv);
503:     }
504:   } else {
505:     PetscBool isseq;

507:     PetscObjectTypeCompare((PetscObject)v, VECSEQ, &isseq);
508:     if (isseq) VecView_Seq(v, viewer);
509:     else VecView_MPI(v, viewer);
510:   }
511:   return 0;
512: }

514: PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
515: {
516:   DM        dm;
517:   PetscBool isvtk, ishdf5, isdraw, isglvis, isexodusii, iscgns;

519:   VecGetDM(v, &dm);
521:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERVTK, &isvtk);
522:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
523:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw);
524:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERGLVIS, &isglvis);
525:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERCGNS, &iscgns);
526:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWEREXODUSII, &isexodusii);
527:   if (isvtk || isdraw || isglvis || iscgns) {
528:     Vec         locv;
529:     PetscObject isZero;
530:     const char *name;

532:     DMGetLocalVector(dm, &locv);
533:     PetscObjectGetName((PetscObject)v, &name);
534:     PetscObjectSetName((PetscObject)locv, name);
535:     DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);
536:     DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);
537:     PetscObjectQuery((PetscObject)v, "__Vec_bc_zero__", &isZero);
538:     PetscObjectCompose((PetscObject)locv, "__Vec_bc_zero__", isZero);
539:     VecView_Plex_Local(locv, viewer);
540:     PetscObjectCompose((PetscObject)locv, "__Vec_bc_zero__", NULL);
541:     DMRestoreLocalVector(dm, &locv);
542:   } else if (ishdf5) {
543: #if defined(PETSC_HAVE_HDF5)
544:     VecView_Plex_HDF5_Internal(v, viewer);
545: #else
546:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
547: #endif
548:   } else if (isexodusii) {
549: #if defined(PETSC_HAVE_EXODUSII)
550:     VecView_PlexExodusII_Internal(v, viewer);
551: #else
552:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "ExodusII not supported in this build.\nPlease reconfigure using --download-exodusii");
553: #endif
554:   } else {
555:     PetscBool isseq;

557:     PetscObjectTypeCompare((PetscObject)v, VECSEQ, &isseq);
558:     if (isseq) VecView_Seq(v, viewer);
559:     else VecView_MPI(v, viewer);
560:   }
561:   return 0;
562: }

564: PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
565: {
566:   DM                dm;
567:   MPI_Comm          comm;
568:   PetscViewerFormat format;
569:   Vec               v;
570:   PetscBool         isvtk, ishdf5;

572:   VecGetDM(originalv, &dm);
573:   PetscObjectGetComm((PetscObject)originalv, &comm);
575:   PetscViewerGetFormat(viewer, &format);
576:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
577:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERVTK, &isvtk);
578:   if (format == PETSC_VIEWER_NATIVE) {
579:     /* Natural ordering is the common case for DMDA, NATIVE means plain vector, for PLEX is the opposite */
580:     /* this need a better fix */
581:     if (dm->useNatural) {
582:       if (dm->sfNatural) {
583:         const char *vecname;
584:         PetscInt    n, nroots;

586:         VecGetLocalSize(originalv, &n);
587:         PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);
588:         if (n == nroots) {
589:           DMGetGlobalVector(dm, &v);
590:           DMPlexGlobalToNaturalBegin(dm, originalv, v);
591:           DMPlexGlobalToNaturalEnd(dm, originalv, v);
592:           PetscObjectGetName((PetscObject)originalv, &vecname);
593:           PetscObjectSetName((PetscObject)v, vecname);
594:         } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
595:       } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
596:     } else v = originalv;
597:   } else v = originalv;

599:   if (ishdf5) {
600: #if defined(PETSC_HAVE_HDF5)
601:     VecView_Plex_HDF5_Native_Internal(v, viewer);
602: #else
603:     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
604: #endif
605:   } else if (isvtk) {
606:     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
607:   } else {
608:     PetscBool isseq;

610:     PetscObjectTypeCompare((PetscObject)v, VECSEQ, &isseq);
611:     if (isseq) VecView_Seq(v, viewer);
612:     else VecView_MPI(v, viewer);
613:   }
614:   if (v != originalv) DMRestoreGlobalVector(dm, &v);
615:   return 0;
616: }

618: PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
619: {
620:   DM        dm;
621:   PetscBool ishdf5;

623:   VecGetDM(v, &dm);
625:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
626:   if (ishdf5) {
627:     DM          dmBC;
628:     Vec         gv;
629:     const char *name;

631:     DMGetOutputDM(dm, &dmBC);
632:     DMGetGlobalVector(dmBC, &gv);
633:     PetscObjectGetName((PetscObject)v, &name);
634:     PetscObjectSetName((PetscObject)gv, name);
635:     VecLoad_Default(gv, viewer);
636:     DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);
637:     DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);
638:     DMRestoreGlobalVector(dmBC, &gv);
639:   } else VecLoad_Default(v, viewer);
640:   return 0;
641: }

643: PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
644: {
645:   DM        dm;
646:   PetscBool ishdf5, isexodusii;

648:   VecGetDM(v, &dm);
650:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
651:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWEREXODUSII, &isexodusii);
652:   if (ishdf5) {
653: #if defined(PETSC_HAVE_HDF5)
654:     VecLoad_Plex_HDF5_Internal(v, viewer);
655: #else
656:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
657: #endif
658:   } else if (isexodusii) {
659: #if defined(PETSC_HAVE_EXODUSII)
660:     VecLoad_PlexExodusII_Internal(v, viewer);
661: #else
662:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "ExodusII not supported in this build.\nPlease reconfigure using --download-exodusii");
663: #endif
664:   } else VecLoad_Default(v, viewer);
665:   return 0;
666: }

668: PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
669: {
670:   DM                dm;
671:   PetscViewerFormat format;
672:   PetscBool         ishdf5;

674:   VecGetDM(originalv, &dm);
676:   PetscViewerGetFormat(viewer, &format);
677:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
678:   if (format == PETSC_VIEWER_NATIVE) {
679:     if (dm->useNatural) {
680:       if (dm->sfNatural) {
681:         if (ishdf5) {
682: #if defined(PETSC_HAVE_HDF5)
683:           Vec         v;
684:           const char *vecname;

686:           DMGetGlobalVector(dm, &v);
687:           PetscObjectGetName((PetscObject)originalv, &vecname);
688:           PetscObjectSetName((PetscObject)v, vecname);
689:           VecLoad_Plex_HDF5_Native_Internal(v, viewer);
690:           DMPlexNaturalToGlobalBegin(dm, v, originalv);
691:           DMPlexNaturalToGlobalEnd(dm, v, originalv);
692:           DMRestoreGlobalVector(dm, &v);
693: #else
694:           SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
695: #endif
696:         } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
697:       }
698:     } else VecLoad_Default(originalv, viewer);
699:   }
700:   return 0;
701: }

703: PETSC_UNUSED static PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
704: {
705:   PetscSection       coordSection;
706:   Vec                coordinates;
707:   DMLabel            depthLabel, celltypeLabel;
708:   const char        *name[4];
709:   const PetscScalar *a;
710:   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;

712:   DMGetDimension(dm, &dim);
713:   DMGetCoordinatesLocal(dm, &coordinates);
714:   DMGetCoordinateSection(dm, &coordSection);
715:   DMPlexGetDepthLabel(dm, &depthLabel);
716:   DMPlexGetCellTypeLabel(dm, &celltypeLabel);
717:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
718:   PetscSectionGetChart(coordSection, &pStart, &pEnd);
719:   VecGetArrayRead(coordinates, &a);
720:   name[0]       = "vertex";
721:   name[1]       = "edge";
722:   name[dim - 1] = "face";
723:   name[dim]     = "cell";
724:   for (c = cStart; c < cEnd; ++c) {
725:     PetscInt *closure = NULL;
726:     PetscInt  closureSize, cl, ct;

728:     DMLabelGetValue(celltypeLabel, c, &ct);
729:     PetscViewerASCIIPrintf(viewer, "Geometry for cell %" PetscInt_FMT " polytope type %s:\n", c, DMPolytopeTypes[ct]);
730:     DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
731:     PetscViewerASCIIPushTab(viewer);
732:     for (cl = 0; cl < closureSize * 2; cl += 2) {
733:       PetscInt point = closure[cl], depth, dof, off, d, p;

735:       if ((point < pStart) || (point >= pEnd)) continue;
736:       PetscSectionGetDof(coordSection, point, &dof);
737:       if (!dof) continue;
738:       DMLabelGetValue(depthLabel, point, &depth);
739:       PetscSectionGetOffset(coordSection, point, &off);
740:       PetscViewerASCIIPrintf(viewer, "%s %" PetscInt_FMT " coords:", name[depth], point);
741:       for (p = 0; p < dof / dim; ++p) {
742:         PetscViewerASCIIPrintf(viewer, " (");
743:         for (d = 0; d < dim; ++d) {
744:           if (d > 0) PetscViewerASCIIPrintf(viewer, ", ");
745:           PetscViewerASCIIPrintf(viewer, "%g", (double)PetscRealPart(a[off + p * dim + d]));
746:         }
747:         PetscViewerASCIIPrintf(viewer, ")");
748:       }
749:       PetscViewerASCIIPrintf(viewer, "\n");
750:     }
751:     DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
752:     PetscViewerASCIIPopTab(viewer);
753:   }
754:   VecRestoreArrayRead(coordinates, &a);
755:   return 0;
756: }

758: typedef enum {
759:   CS_CARTESIAN,
760:   CS_POLAR,
761:   CS_CYLINDRICAL,
762:   CS_SPHERICAL
763: } CoordSystem;
764: const char *CoordSystems[] = {"cartesian", "polar", "cylindrical", "spherical", "CoordSystem", "CS_", NULL};

766: static PetscErrorCode DMPlexView_Ascii_Coordinates(PetscViewer viewer, CoordSystem cs, PetscInt dim, const PetscScalar x[])
767: {
768:   PetscInt i;

770:   if (dim > 3) {
771:     for (i = 0; i < dim; ++i) PetscViewerASCIISynchronizedPrintf(viewer, " %g", (double)PetscRealPart(x[i]));
772:   } else {
773:     PetscReal coords[3], trcoords[3] = {0., 0., 0.};

775:     for (i = 0; i < dim; ++i) coords[i] = PetscRealPart(x[i]);
776:     switch (cs) {
777:     case CS_CARTESIAN:
778:       for (i = 0; i < dim; ++i) trcoords[i] = coords[i];
779:       break;
780:     case CS_POLAR:
782:       trcoords[0] = PetscSqrtReal(PetscSqr(coords[0]) + PetscSqr(coords[1]));
783:       trcoords[1] = PetscAtan2Real(coords[1], coords[0]);
784:       break;
785:     case CS_CYLINDRICAL:
787:       trcoords[0] = PetscSqrtReal(PetscSqr(coords[0]) + PetscSqr(coords[1]));
788:       trcoords[1] = PetscAtan2Real(coords[1], coords[0]);
789:       trcoords[2] = coords[2];
790:       break;
791:     case CS_SPHERICAL:
793:       trcoords[0] = PetscSqrtReal(PetscSqr(coords[0]) + PetscSqr(coords[1]) + PetscSqr(coords[2]));
794:       trcoords[1] = PetscAtan2Real(PetscSqrtReal(PetscSqr(coords[0]) + PetscSqr(coords[1])), coords[2]);
795:       trcoords[2] = PetscAtan2Real(coords[1], coords[0]);
796:       break;
797:     }
798:     for (i = 0; i < dim; ++i) PetscViewerASCIISynchronizedPrintf(viewer, " %g", (double)trcoords[i]);
799:   }
800:   return 0;
801: }

803: static PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
804: {
805:   DM_Plex          *mesh = (DM_Plex *)dm->data;
806:   DM                cdm, cdmCell;
807:   PetscSection      coordSection, coordSectionCell;
808:   Vec               coordinates, coordinatesCell;
809:   PetscViewerFormat format;

811:   DMGetCoordinateDM(dm, &cdm);
812:   DMGetCoordinateSection(dm, &coordSection);
813:   DMGetCoordinatesLocal(dm, &coordinates);
814:   DMGetCellCoordinateDM(dm, &cdmCell);
815:   DMGetCellCoordinateSection(dm, &coordSectionCell);
816:   DMGetCellCoordinatesLocal(dm, &coordinatesCell);
817:   PetscViewerGetFormat(viewer, &format);
818:   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
819:     const char *name;
820:     PetscInt    dim, cellHeight, maxConeSize, maxSupportSize;
821:     PetscInt    pStart, pEnd, p, numLabels, l;
822:     PetscMPIInt rank, size;

824:     MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
825:     MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);
826:     PetscObjectGetName((PetscObject)dm, &name);
827:     DMPlexGetChart(dm, &pStart, &pEnd);
828:     DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);
829:     DMGetDimension(dm, &dim);
830:     DMPlexGetVTKCellHeight(dm, &cellHeight);
831:     if (name) PetscViewerASCIIPrintf(viewer, "%s in %" PetscInt_FMT " dimension%s:\n", name, dim, dim == 1 ? "" : "s");
832:     else PetscViewerASCIIPrintf(viewer, "Mesh in %" PetscInt_FMT " dimension%s:\n", dim, dim == 1 ? "" : "s");
833:     if (cellHeight) PetscViewerASCIIPrintf(viewer, "  Cells are at height %" PetscInt_FMT "\n", cellHeight);
834:     PetscViewerASCIIPrintf(viewer, "Supports:\n");
835:     PetscViewerASCIIPushSynchronized(viewer);
836:     PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max support size: %" PetscInt_FMT "\n", rank, maxSupportSize);
837:     for (p = pStart; p < pEnd; ++p) {
838:       PetscInt dof, off, s;

840:       PetscSectionGetDof(mesh->supportSection, p, &dof);
841:       PetscSectionGetOffset(mesh->supportSection, p, &off);
842:       for (s = off; s < off + dof; ++s) PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %" PetscInt_FMT " ----> %" PetscInt_FMT "\n", rank, p, mesh->supports[s]);
843:     }
844:     PetscViewerFlush(viewer);
845:     PetscViewerASCIIPrintf(viewer, "Cones:\n");
846:     PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max cone size: %" PetscInt_FMT "\n", rank, maxConeSize);
847:     for (p = pStart; p < pEnd; ++p) {
848:       PetscInt dof, off, c;

850:       PetscSectionGetDof(mesh->coneSection, p, &dof);
851:       PetscSectionGetOffset(mesh->coneSection, p, &off);
852:       for (c = off; c < off + dof; ++c) PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %" PetscInt_FMT " <---- %" PetscInt_FMT " (%" PetscInt_FMT ")\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);
853:     }
854:     PetscViewerFlush(viewer);
855:     PetscViewerASCIIPopSynchronized(viewer);
856:     if (coordSection && coordinates) {
857:       CoordSystem        cs = CS_CARTESIAN;
858:       const PetscScalar *array, *arrayCell = NULL;
859:       PetscInt           Nf, Nc, pvStart, pvEnd, pcStart = PETSC_MAX_INT, pcEnd = PETSC_MIN_INT, pStart, pEnd, p;
860:       PetscMPIInt        rank;
861:       const char        *name;

863:       PetscOptionsGetEnum(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_coord_system", CoordSystems, (PetscEnum *)&cs, NULL);
864:       MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank);
865:       PetscSectionGetNumFields(coordSection, &Nf);
867:       PetscSectionGetFieldComponents(coordSection, 0, &Nc);
868:       PetscSectionGetChart(coordSection, &pvStart, &pvEnd);
869:       if (coordSectionCell) PetscSectionGetChart(coordSectionCell, &pcStart, &pcEnd);
870:       pStart = PetscMin(pvStart, pcStart);
871:       pEnd   = PetscMax(pvEnd, pcEnd);
872:       PetscObjectGetName((PetscObject)coordinates, &name);
873:       PetscViewerASCIIPrintf(viewer, "%s with %" PetscInt_FMT " fields\n", name, Nf);
874:       PetscViewerASCIIPrintf(viewer, "  field 0 with %" PetscInt_FMT " components\n", Nc);
875:       if (cs != CS_CARTESIAN) PetscViewerASCIIPrintf(viewer, "  output coordinate system: %s\n", CoordSystems[cs]);

877:       VecGetArrayRead(coordinates, &array);
878:       if (coordinatesCell) VecGetArrayRead(coordinatesCell, &arrayCell);
879:       PetscViewerASCIIPushSynchronized(viewer);
880:       PetscViewerASCIISynchronizedPrintf(viewer, "Process %d:\n", rank);
881:       for (p = pStart; p < pEnd; ++p) {
882:         PetscInt dof, off;

884:         if (p >= pvStart && p < pvEnd) {
885:           PetscSectionGetDof(coordSection, p, &dof);
886:           PetscSectionGetOffset(coordSection, p, &off);
887:           if (dof) {
888:             PetscViewerASCIISynchronizedPrintf(viewer, "  (%4" PetscInt_FMT ") dim %2" PetscInt_FMT " offset %3" PetscInt_FMT, p, dof, off);
889:             DMPlexView_Ascii_Coordinates(viewer, cs, dof, &array[off]);
890:             PetscViewerASCIISynchronizedPrintf(viewer, "\n");
891:           }
892:         }
893:         if (cdmCell && p >= pcStart && p < pcEnd) {
894:           PetscSectionGetDof(coordSectionCell, p, &dof);
895:           PetscSectionGetOffset(coordSectionCell, p, &off);
896:           if (dof) {
897:             PetscViewerASCIISynchronizedPrintf(viewer, "  (%4" PetscInt_FMT ") dim %2" PetscInt_FMT " offset %3" PetscInt_FMT, p, dof, off);
898:             DMPlexView_Ascii_Coordinates(viewer, cs, dof, &arrayCell[off]);
899:             PetscViewerASCIISynchronizedPrintf(viewer, "\n");
900:           }
901:         }
902:       }
903:       PetscViewerFlush(viewer);
904:       PetscViewerASCIIPopSynchronized(viewer);
905:       VecRestoreArrayRead(coordinates, &array);
906:       if (coordinatesCell) VecRestoreArrayRead(coordinatesCell, &arrayCell);
907:     }
908:     DMGetNumLabels(dm, &numLabels);
909:     if (numLabels) PetscViewerASCIIPrintf(viewer, "Labels:\n");
910:     for (l = 0; l < numLabels; ++l) {
911:       DMLabel     label;
912:       PetscBool   isdepth;
913:       const char *name;

915:       DMGetLabelName(dm, l, &name);
916:       PetscStrcmp(name, "depth", &isdepth);
917:       if (isdepth) continue;
918:       DMGetLabel(dm, name, &label);
919:       DMLabelView(label, viewer);
920:     }
921:     if (size > 1) {
922:       PetscSF sf;

924:       DMGetPointSF(dm, &sf);
925:       PetscSFView(sf, viewer);
926:     }
927:     PetscViewerFlush(viewer);
928:   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
929:     const char  *name, *color;
930:     const char  *defcolors[3]  = {"gray", "orange", "green"};
931:     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
932:     char         lname[PETSC_MAX_PATH_LEN];
933:     PetscReal    scale      = 2.0;
934:     PetscReal    tikzscale  = 1.0;
935:     PetscBool    useNumbers = PETSC_TRUE, drawNumbers[4], drawColors[4], useLabels, useColors, plotEdges, drawHasse = PETSC_FALSE;
936:     double       tcoords[3];
937:     PetscScalar *coords;
938:     PetscInt     numLabels, l, numColors, numLColors, dim, d, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p, n;
939:     PetscMPIInt  rank, size;
940:     char       **names, **colors, **lcolors;
941:     PetscBool    flg, lflg;
942:     PetscBT      wp = NULL;
943:     PetscInt     pEnd, pStart;

945:     DMGetDimension(dm, &dim);
946:     DMPlexGetDepth(dm, &depth);
947:     DMGetNumLabels(dm, &numLabels);
948:     numLabels  = PetscMax(numLabels, 10);
949:     numColors  = 10;
950:     numLColors = 10;
951:     PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors);
952:     PetscOptionsGetReal(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_scale", &scale, NULL);
953:     PetscOptionsGetReal(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_tikzscale", &tikzscale, NULL);
954:     PetscOptionsGetBool(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);
955:     for (d = 0; d < 4; ++d) drawNumbers[d] = useNumbers;
956:     for (d = 0; d < 4; ++d) drawColors[d] = PETSC_TRUE;
957:     n = 4;
958:     PetscOptionsGetBoolArray(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_numbers_depth", drawNumbers, &n, &flg);
960:     PetscOptionsGetBoolArray(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_colors_depth", drawColors, &n, &flg);
962:     PetscOptionsGetStringArray(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);
963:     if (!useLabels) numLabels = 0;
964:     PetscOptionsGetStringArray(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);
965:     if (!useColors) {
966:       numColors = 3;
967:       for (c = 0; c < numColors; ++c) PetscStrallocpy(defcolors[c], &colors[c]);
968:     }
969:     PetscOptionsGetStringArray(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);
970:     if (!useColors) {
971:       numLColors = 4;
972:       for (c = 0; c < numLColors; ++c) PetscStrallocpy(deflcolors[c], &lcolors[c]);
973:     }
974:     PetscOptionsGetString(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_label_filter", lname, sizeof(lname), &lflg);
975:     plotEdges = (PetscBool)(depth > 1 && drawNumbers[1] && dim < 3);
976:     PetscOptionsGetBool(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_edges", &plotEdges, &flg);
978:     if (depth < dim) plotEdges = PETSC_FALSE;
979:     PetscOptionsGetBool(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_hasse", &drawHasse, NULL);

981:     /* filter points with labelvalue != labeldefaultvalue */
982:     DMPlexGetChart(dm, &pStart, &pEnd);
983:     DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
984:     DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);
985:     DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
986:     if (lflg) {
987:       DMLabel lbl;

989:       DMGetLabel(dm, lname, &lbl);
990:       if (lbl) {
991:         PetscInt val, defval;

993:         DMLabelGetDefaultValue(lbl, &defval);
994:         PetscBTCreate(pEnd - pStart, &wp);
995:         for (c = pStart; c < pEnd; c++) {
996:           PetscInt *closure = NULL;
997:           PetscInt  closureSize;

999:           DMLabelGetValue(lbl, c, &val);
1000:           if (val == defval) continue;

1002:           DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
1003:           for (p = 0; p < closureSize * 2; p += 2) PetscBTSet(wp, closure[p] - pStart);
1004:           DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
1005:         }
1006:       }
1007:     }

1009:     MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
1010:     MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);
1011:     PetscObjectGetName((PetscObject)dm, &name);
1012:     PetscCall(PetscViewerASCIIPrintf(viewer, "\
1013: \\documentclass[tikz]{standalone}\n\n\
1014: \\usepackage{pgflibraryshapes}\n\
1015: \\usetikzlibrary{backgrounds}\n\
1016: \\usetikzlibrary{arrows}\n\
1017: \\begin{document}\n"));
1018:     if (size > 1) {
1019:       PetscViewerASCIIPrintf(viewer, "%s for process ", name);
1020:       for (p = 0; p < size; ++p) {
1021:         if (p) PetscViewerASCIIPrintf(viewer, (p == size - 1) ? ", and " : ", ");
1022:         PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%" PetscInt_FMT "}", colors[p % numColors], p);
1023:       }
1024:       PetscViewerASCIIPrintf(viewer, ".\n\n\n");
1025:     }
1026:     if (drawHasse) {
1027:       PetscInt maxStratum = PetscMax(vEnd - vStart, PetscMax(eEnd - eStart, cEnd - cStart));

1029:       PetscViewerASCIIPrintf(viewer, "\\newcommand{\\vStart}{%" PetscInt_FMT "}\n", vStart);
1030:       PetscViewerASCIIPrintf(viewer, "\\newcommand{\\vEnd}{%" PetscInt_FMT "}\n", vEnd - 1);
1031:       PetscViewerASCIIPrintf(viewer, "\\newcommand{\\numVertices}{%" PetscInt_FMT "}\n", vEnd - vStart);
1032:       PetscViewerASCIIPrintf(viewer, "\\newcommand{\\vShift}{%.2f}\n", 3 + (maxStratum - (vEnd - vStart)) / 2.);
1033:       PetscViewerASCIIPrintf(viewer, "\\newcommand{\\eStart}{%" PetscInt_FMT "}\n", eStart);
1034:       PetscViewerASCIIPrintf(viewer, "\\newcommand{\\eEnd}{%" PetscInt_FMT "}\n", eEnd - 1);
1035:       PetscViewerASCIIPrintf(viewer, "\\newcommand{\\eShift}{%.2f}\n", 3 + (maxStratum - (eEnd - eStart)) / 2.);
1036:       PetscViewerASCIIPrintf(viewer, "\\newcommand{\\numEdges}{%" PetscInt_FMT "}\n", eEnd - eStart);
1037:       PetscViewerASCIIPrintf(viewer, "\\newcommand{\\cStart}{%" PetscInt_FMT "}\n", cStart);
1038:       PetscViewerASCIIPrintf(viewer, "\\newcommand{\\cEnd}{%" PetscInt_FMT "}\n", cEnd - 1);
1039:       PetscViewerASCIIPrintf(viewer, "\\newcommand{\\numCells}{%" PetscInt_FMT "}\n", cEnd - cStart);
1040:       PetscViewerASCIIPrintf(viewer, "\\newcommand{\\cShift}{%.2f}\n", 3 + (maxStratum - (cEnd - cStart)) / 2.);
1041:     }
1042:     PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", (double)tikzscale);

1044:     /* Plot vertices */
1045:     VecGetArray(coordinates, &coords);
1046:     PetscViewerASCIIPushSynchronized(viewer);
1047:     for (v = vStart; v < vEnd; ++v) {
1048:       PetscInt  off, dof, d;
1049:       PetscBool isLabeled = PETSC_FALSE;

1051:       if (wp && !PetscBTLookup(wp, v - pStart)) continue;
1052:       PetscSectionGetDof(coordSection, v, &dof);
1053:       PetscSectionGetOffset(coordSection, v, &off);
1054:       PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");
1056:       for (d = 0; d < dof; ++d) {
1057:         tcoords[d] = (double)(scale * PetscRealPart(coords[off + d]));
1058:         tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
1059:       }
1060:       /* Rotate coordinates since PGF makes z point out of the page instead of up */
1061:       if (dim == 3) {
1062:         PetscReal tmp = tcoords[1];
1063:         tcoords[1]    = tcoords[2];
1064:         tcoords[2]    = -tmp;
1065:       }
1066:       for (d = 0; d < dof; ++d) {
1067:         if (d > 0) PetscViewerASCIISynchronizedPrintf(viewer, ",");
1068:         PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);
1069:       }
1070:       if (drawHasse) color = colors[0 % numColors];
1071:       else color = colors[rank % numColors];
1072:       for (l = 0; l < numLabels; ++l) {
1073:         PetscInt val;
1074:         DMGetLabelValue(dm, names[l], v, &val);
1075:         if (val >= 0) {
1076:           color     = lcolors[l % numLColors];
1077:           isLabeled = PETSC_TRUE;
1078:           break;
1079:         }
1080:       }
1081:       if (drawNumbers[0]) {
1082:         PetscViewerASCIISynchronizedPrintf(viewer, ") node(%" PetscInt_FMT "_%d) [draw,shape=circle,color=%s] {%" PetscInt_FMT "};\n", v, rank, color, v);
1083:       } else if (drawColors[0]) {
1084:         PetscViewerASCIISynchronizedPrintf(viewer, ") node(%" PetscInt_FMT "_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);
1085:       } else PetscViewerASCIISynchronizedPrintf(viewer, ") node(%" PetscInt_FMT "_%d) [] {};\n", v, rank);
1086:     }
1087:     VecRestoreArray(coordinates, &coords);
1088:     PetscViewerFlush(viewer);
1089:     /* Plot edges */
1090:     if (plotEdges) {
1091:       VecGetArray(coordinates, &coords);
1092:       PetscViewerASCIIPrintf(viewer, "\\path\n");
1093:       for (e = eStart; e < eEnd; ++e) {
1094:         const PetscInt *cone;
1095:         PetscInt        coneSize, offA, offB, dof, d;

1097:         if (wp && !PetscBTLookup(wp, e - pStart)) continue;
1098:         DMPlexGetConeSize(dm, e, &coneSize);
1100:         DMPlexGetCone(dm, e, &cone);
1101:         PetscSectionGetDof(coordSection, cone[0], &dof);
1102:         PetscSectionGetOffset(coordSection, cone[0], &offA);
1103:         PetscSectionGetOffset(coordSection, cone[1], &offB);
1104:         PetscViewerASCIISynchronizedPrintf(viewer, "(");
1105:         for (d = 0; d < dof; ++d) {
1106:           tcoords[d] = (double)(0.5 * scale * PetscRealPart(coords[offA + d] + coords[offB + d]));
1107:           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
1108:         }
1109:         /* Rotate coordinates since PGF makes z point out of the page instead of up */
1110:         if (dim == 3) {
1111:           PetscReal tmp = tcoords[1];
1112:           tcoords[1]    = tcoords[2];
1113:           tcoords[2]    = -tmp;
1114:         }
1115:         for (d = 0; d < dof; ++d) {
1116:           if (d > 0) PetscViewerASCIISynchronizedPrintf(viewer, ",");
1117:           PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);
1118:         }
1119:         if (drawHasse) color = colors[1 % numColors];
1120:         else color = colors[rank % numColors];
1121:         for (l = 0; l < numLabels; ++l) {
1122:           PetscInt val;
1123:           DMGetLabelValue(dm, names[l], v, &val);
1124:           if (val >= 0) {
1125:             color = lcolors[l % numLColors];
1126:             break;
1127:           }
1128:         }
1129:         PetscViewerASCIISynchronizedPrintf(viewer, ") node(%" PetscInt_FMT "_%d) [draw,shape=circle,color=%s] {%" PetscInt_FMT "} --\n", e, rank, color, e);
1130:       }
1131:       VecRestoreArray(coordinates, &coords);
1132:       PetscViewerFlush(viewer);
1133:       PetscViewerASCIIPrintf(viewer, "(0,0);\n");
1134:     }
1135:     /* Plot cells */
1136:     if (dim == 3 || !drawNumbers[1]) {
1137:       for (e = eStart; e < eEnd; ++e) {
1138:         const PetscInt *cone;

1140:         if (wp && !PetscBTLookup(wp, e - pStart)) continue;
1141:         color = colors[rank % numColors];
1142:         for (l = 0; l < numLabels; ++l) {
1143:           PetscInt val;
1144:           DMGetLabelValue(dm, names[l], e, &val);
1145:           if (val >= 0) {
1146:             color = lcolors[l % numLColors];
1147:             break;
1148:           }
1149:         }
1150:         DMPlexGetCone(dm, e, &cone);
1151:         PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%" PetscInt_FMT "_%d) -- (%" PetscInt_FMT "_%d);\n", color, cone[0], rank, cone[1], rank);
1152:       }
1153:     } else {
1154:       DMPolytopeType ct;

1156:       /* Drawing a 2D polygon */
1157:       for (c = cStart; c < cEnd; ++c) {
1158:         if (wp && !PetscBTLookup(wp, c - pStart)) continue;
1159:         DMPlexGetCellType(dm, c, &ct);
1160:         if (ct == DM_POLYTOPE_SEG_PRISM_TENSOR || ct == DM_POLYTOPE_TRI_PRISM_TENSOR || ct == DM_POLYTOPE_QUAD_PRISM_TENSOR) {
1161:           const PetscInt *cone;
1162:           PetscInt        coneSize, e;

1164:           DMPlexGetCone(dm, c, &cone);
1165:           DMPlexGetConeSize(dm, c, &coneSize);
1166:           for (e = 0; e < coneSize; ++e) {
1167:             const PetscInt *econe;

1169:             DMPlexGetCone(dm, cone[e], &econe);
1170:             PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%" PetscInt_FMT "_%d) -- (%" PetscInt_FMT "_%d) -- (%" PetscInt_FMT "_%d);\n", colors[rank % numColors], econe[0], rank, cone[e], rank, econe[1], rank);
1171:           }
1172:         } else {
1173:           PetscInt *closure = NULL;
1174:           PetscInt  closureSize, Nv = 0, v;

1176:           DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
1177:           for (p = 0; p < closureSize * 2; p += 2) {
1178:             const PetscInt point = closure[p];

1180:             if ((point >= vStart) && (point < vEnd)) closure[Nv++] = point;
1181:           }
1182:           PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank % numColors]);
1183:           for (v = 0; v <= Nv; ++v) {
1184:             const PetscInt vertex = closure[v % Nv];

1186:             if (v > 0) {
1187:               if (plotEdges) {
1188:                 const PetscInt *edge;
1189:                 PetscInt        endpoints[2], ne;

1191:                 endpoints[0] = closure[v - 1];
1192:                 endpoints[1] = vertex;
1193:                 DMPlexGetJoin(dm, 2, endpoints, &ne, &edge);
1195:                 PetscViewerASCIISynchronizedPrintf(viewer, " -- (%" PetscInt_FMT "_%d) -- ", edge[0], rank);
1196:                 DMPlexRestoreJoin(dm, 2, endpoints, &ne, &edge);
1197:               } else PetscViewerASCIISynchronizedPrintf(viewer, " -- ");
1198:             }
1199:             PetscViewerASCIISynchronizedPrintf(viewer, "(%" PetscInt_FMT "_%d)", vertex, rank);
1200:           }
1201:           PetscViewerASCIISynchronizedPrintf(viewer, ";\n");
1202:           DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
1203:         }
1204:       }
1205:     }
1206:     for (c = cStart; c < cEnd; ++c) {
1207:       double             ccoords[3] = {0.0, 0.0, 0.0};
1208:       PetscBool          isLabeled  = PETSC_FALSE;
1209:       PetscScalar       *cellCoords = NULL;
1210:       const PetscScalar *array;
1211:       PetscInt           numCoords, cdim, d;
1212:       PetscBool          isDG;

1214:       if (wp && !PetscBTLookup(wp, c - pStart)) continue;
1215:       DMGetCoordinateDim(dm, &cdim);
1216:       DMPlexGetCellCoordinates(dm, c, &isDG, &numCoords, &array, &cellCoords);
1218:       PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");
1219:       for (p = 0; p < numCoords / cdim; ++p) {
1220:         for (d = 0; d < cdim; ++d) {
1221:           tcoords[d] = (double)(scale * PetscRealPart(cellCoords[p * cdim + d]));
1222:           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
1223:         }
1224:         /* Rotate coordinates since PGF makes z point out of the page instead of up */
1225:         if (cdim == 3) {
1226:           PetscReal tmp = tcoords[1];
1227:           tcoords[1]    = tcoords[2];
1228:           tcoords[2]    = -tmp;
1229:         }
1230:         for (d = 0; d < dim; ++d) ccoords[d] += tcoords[d];
1231:       }
1232:       for (d = 0; d < cdim; ++d) ccoords[d] /= (numCoords / cdim);
1233:       DMPlexRestoreCellCoordinates(dm, c, &isDG, &numCoords, &array, &cellCoords);
1234:       for (d = 0; d < cdim; ++d) {
1235:         if (d > 0) PetscViewerASCIISynchronizedPrintf(viewer, ",");
1236:         PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)ccoords[d]);
1237:       }
1238:       if (drawHasse) color = colors[depth % numColors];
1239:       else color = colors[rank % numColors];
1240:       for (l = 0; l < numLabels; ++l) {
1241:         PetscInt val;
1242:         DMGetLabelValue(dm, names[l], c, &val);
1243:         if (val >= 0) {
1244:           color     = lcolors[l % numLColors];
1245:           isLabeled = PETSC_TRUE;
1246:           break;
1247:         }
1248:       }
1249:       if (drawNumbers[dim]) {
1250:         PetscViewerASCIISynchronizedPrintf(viewer, ") node(%" PetscInt_FMT "_%d) [draw,shape=circle,color=%s] {%" PetscInt_FMT "};\n", c, rank, color, c);
1251:       } else if (drawColors[dim]) {
1252:         PetscViewerASCIISynchronizedPrintf(viewer, ") node(%" PetscInt_FMT "_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", c, rank, !isLabeled ? 1 : 2, color);
1253:       } else PetscViewerASCIISynchronizedPrintf(viewer, ") node(%" PetscInt_FMT "_%d) [] {};\n", c, rank);
1254:     }
1255:     if (drawHasse) {
1256:       color = colors[depth % numColors];
1257:       PetscViewerASCIIPrintf(viewer, "%% Cells\n");
1258:       PetscViewerASCIIPrintf(viewer, "\\foreach \\c in {\\cStart,...,\\cEnd}\n");
1259:       PetscViewerASCIIPrintf(viewer, "{\n");
1260:       PetscViewerASCIIPrintf(viewer, "  \\node(\\c_%d) [draw,shape=circle,color=%s,minimum size = 6mm] at (\\cShift+\\c-\\cStart,0) {\\c};\n", rank, color);
1261:       PetscViewerASCIIPrintf(viewer, "}\n");

1263:       color = colors[1 % numColors];
1264:       PetscViewerASCIIPrintf(viewer, "%% Edges\n");
1265:       PetscViewerASCIIPrintf(viewer, "\\foreach \\e in {\\eStart,...,\\eEnd}\n");
1266:       PetscViewerASCIIPrintf(viewer, "{\n");
1267:       PetscViewerASCIIPrintf(viewer, "  \\node(\\e_%d) [draw,shape=circle,color=%s,minimum size = 6mm] at (\\eShift+\\e-\\eStart,1) {\\e};\n", rank, color);
1268:       PetscViewerASCIIPrintf(viewer, "}\n");

1270:       color = colors[0 % numColors];
1271:       PetscViewerASCIIPrintf(viewer, "%% Vertices\n");
1272:       PetscViewerASCIIPrintf(viewer, "\\foreach \\v in {\\vStart,...,\\vEnd}\n");
1273:       PetscViewerASCIIPrintf(viewer, "{\n");
1274:       PetscViewerASCIIPrintf(viewer, "  \\node(\\v_%d) [draw,shape=circle,color=%s,minimum size = 6mm] at (\\vShift+\\v-\\vStart,2) {\\v};\n", rank, color);
1275:       PetscViewerASCIIPrintf(viewer, "}\n");

1277:       for (p = pStart; p < pEnd; ++p) {
1278:         const PetscInt *cone;
1279:         PetscInt        coneSize, cp;

1281:         DMPlexGetCone(dm, p, &cone);
1282:         DMPlexGetConeSize(dm, p, &coneSize);
1283:         for (cp = 0; cp < coneSize; ++cp) PetscViewerASCIIPrintf(viewer, "\\draw[->, shorten >=1pt] (%" PetscInt_FMT "_%d) -- (%" PetscInt_FMT "_%d);\n", cone[cp], rank, p, rank);
1284:       }
1285:     }
1286:     PetscViewerFlush(viewer);
1287:     PetscViewerASCIIPopSynchronized(viewer);
1288:     PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");
1289:     PetscViewerASCIIPrintf(viewer, "\\end{document}\n");
1290:     for (l = 0; l < numLabels; ++l) PetscFree(names[l]);
1291:     for (c = 0; c < numColors; ++c) PetscFree(colors[c]);
1292:     for (c = 0; c < numLColors; ++c) PetscFree(lcolors[c]);
1293:     PetscFree3(names, colors, lcolors);
1294:     PetscBTDestroy(&wp);
1295:   } else if (format == PETSC_VIEWER_LOAD_BALANCE) {
1296:     Vec                    cown, acown;
1297:     VecScatter             sct;
1298:     ISLocalToGlobalMapping g2l;
1299:     IS                     gid, acis;
1300:     MPI_Comm               comm, ncomm = MPI_COMM_NULL;
1301:     MPI_Group              ggroup, ngroup;
1302:     PetscScalar           *array, nid;
1303:     const PetscInt        *idxs;
1304:     PetscInt              *idxs2, *start, *adjacency, *work;
1305:     PetscInt64             lm[3], gm[3];
1306:     PetscInt               i, c, cStart, cEnd, cum, numVertices, ect, ectn, cellHeight;
1307:     PetscMPIInt            d1, d2, rank;

1309:     PetscObjectGetComm((PetscObject)dm, &comm);
1310:     MPI_Comm_rank(comm, &rank);
1311: #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
1312:     MPI_Comm_split_type(comm, MPI_COMM_TYPE_SHARED, rank, MPI_INFO_NULL, &ncomm);
1313: #endif
1314:     if (ncomm != MPI_COMM_NULL) {
1315:       MPI_Comm_group(comm, &ggroup);
1316:       MPI_Comm_group(ncomm, &ngroup);
1317:       d1 = 0;
1318:       MPI_Group_translate_ranks(ngroup, 1, &d1, ggroup, &d2);
1319:       nid = d2;
1320:       MPI_Group_free(&ggroup);
1321:       MPI_Group_free(&ngroup);
1322:       MPI_Comm_free(&ncomm);
1323:     } else nid = 0.0;

1325:     /* Get connectivity */
1326:     DMPlexGetVTKCellHeight(dm, &cellHeight);
1327:     DMPlexCreatePartitionerGraph(dm, cellHeight, &numVertices, &start, &adjacency, &gid);

1329:     /* filter overlapped local cells */
1330:     DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);
1331:     ISGetIndices(gid, &idxs);
1332:     ISGetLocalSize(gid, &cum);
1333:     PetscMalloc1(cum, &idxs2);
1334:     for (c = cStart, cum = 0; c < cEnd; c++) {
1335:       if (idxs[c - cStart] < 0) continue;
1336:       idxs2[cum++] = idxs[c - cStart];
1337:     }
1338:     ISRestoreIndices(gid, &idxs);
1340:     ISDestroy(&gid);
1341:     ISCreateGeneral(comm, numVertices, idxs2, PETSC_OWN_POINTER, &gid);

1343:     /* support for node-aware cell locality */
1344:     ISCreateGeneral(comm, start[numVertices], adjacency, PETSC_USE_POINTER, &acis);
1345:     VecCreateSeq(PETSC_COMM_SELF, start[numVertices], &acown);
1346:     VecCreateMPI(comm, numVertices, PETSC_DECIDE, &cown);
1347:     VecGetArray(cown, &array);
1348:     for (c = 0; c < numVertices; c++) array[c] = nid;
1349:     VecRestoreArray(cown, &array);
1350:     VecScatterCreate(cown, acis, acown, NULL, &sct);
1351:     VecScatterBegin(sct, cown, acown, INSERT_VALUES, SCATTER_FORWARD);
1352:     VecScatterEnd(sct, cown, acown, INSERT_VALUES, SCATTER_FORWARD);
1353:     ISDestroy(&acis);
1354:     VecScatterDestroy(&sct);
1355:     VecDestroy(&cown);

1357:     /* compute edgeCut */
1358:     for (c = 0, cum = 0; c < numVertices; c++) cum = PetscMax(cum, start[c + 1] - start[c]);
1359:     PetscMalloc1(cum, &work);
1360:     ISLocalToGlobalMappingCreateIS(gid, &g2l);
1361:     ISLocalToGlobalMappingSetType(g2l, ISLOCALTOGLOBALMAPPINGHASH);
1362:     ISDestroy(&gid);
1363:     VecGetArray(acown, &array);
1364:     for (c = 0, ect = 0, ectn = 0; c < numVertices; c++) {
1365:       PetscInt totl;

1367:       totl = start[c + 1] - start[c];
1368:       ISGlobalToLocalMappingApply(g2l, IS_GTOLM_MASK, totl, adjacency + start[c], NULL, work);
1369:       for (i = 0; i < totl; i++) {
1370:         if (work[i] < 0) {
1371:           ect += 1;
1372:           ectn += (array[i + start[c]] != nid) ? 0 : 1;
1373:         }
1374:       }
1375:     }
1376:     PetscFree(work);
1377:     VecRestoreArray(acown, &array);
1378:     lm[0] = numVertices > 0 ? numVertices : PETSC_MAX_INT;
1379:     lm[1] = -numVertices;
1380:     MPIU_Allreduce(lm, gm, 2, MPIU_INT64, MPI_MIN, comm);
1381:     PetscViewerASCIIPrintf(viewer, "  Cell balance: %.2f (max %" PetscInt_FMT ", min %" PetscInt_FMT, -((double)gm[1]) / ((double)gm[0]), -(PetscInt)gm[1], (PetscInt)gm[0]);
1382:     lm[0] = ect;                     /* edgeCut */
1383:     lm[1] = ectn;                    /* node-aware edgeCut */
1384:     lm[2] = numVertices > 0 ? 0 : 1; /* empty processes */
1385:     MPIU_Allreduce(lm, gm, 3, MPIU_INT64, MPI_SUM, comm);
1386:     PetscViewerASCIIPrintf(viewer, ", empty %" PetscInt_FMT ")\n", (PetscInt)gm[2]);
1387: #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
1388:     PetscViewerASCIIPrintf(viewer, "  Edge Cut: %" PetscInt_FMT " (on node %.3f)\n", (PetscInt)(gm[0] / 2), gm[0] ? ((double)(gm[1])) / ((double)gm[0]) : 1.);
1389: #else
1390:     PetscViewerASCIIPrintf(viewer, "  Edge Cut: %" PetscInt_FMT " (on node %.3f)\n", (PetscInt)(gm[0] / 2), 0.0);
1391: #endif
1392:     ISLocalToGlobalMappingDestroy(&g2l);
1393:     PetscFree(start);
1394:     PetscFree(adjacency);
1395:     VecDestroy(&acown);
1396:   } else {
1397:     const char    *name;
1398:     PetscInt      *sizes, *hybsizes, *ghostsizes;
1399:     PetscInt       locDepth, depth, cellHeight, dim, d;
1400:     PetscInt       pStart, pEnd, p, gcStart, gcEnd, gcNum;
1401:     PetscInt       numLabels, l, maxSize = 17;
1402:     DMPolytopeType ct0 = DM_POLYTOPE_UNKNOWN;
1403:     MPI_Comm       comm;
1404:     PetscMPIInt    size, rank;

1406:     PetscObjectGetComm((PetscObject)dm, &comm);
1407:     MPI_Comm_size(comm, &size);
1408:     MPI_Comm_rank(comm, &rank);
1409:     DMGetDimension(dm, &dim);
1410:     DMPlexGetVTKCellHeight(dm, &cellHeight);
1411:     PetscObjectGetName((PetscObject)dm, &name);
1412:     if (name) PetscViewerASCIIPrintf(viewer, "%s in %" PetscInt_FMT " dimension%s:\n", name, dim, dim == 1 ? "" : "s");
1413:     else PetscViewerASCIIPrintf(viewer, "Mesh in %" PetscInt_FMT " dimension%s:\n", dim, dim == 1 ? "" : "s");
1414:     if (cellHeight) PetscViewerASCIIPrintf(viewer, "  Cells are at height %" PetscInt_FMT "\n", cellHeight);
1415:     DMPlexGetDepth(dm, &locDepth);
1416:     MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);
1417:     DMPlexGetGhostCellStratum(dm, &gcStart, &gcEnd);
1418:     gcNum = gcEnd - gcStart;
1419:     if (size < maxSize) PetscCalloc3(size, &sizes, size, &hybsizes, size, &ghostsizes);
1420:     else PetscCalloc3(3, &sizes, 3, &hybsizes, 3, &ghostsizes);
1421:     for (d = 0; d <= depth; d++) {
1422:       PetscInt Nc[2] = {0, 0}, ict;

1424:       DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);
1425:       if (pStart < pEnd) DMPlexGetCellType(dm, pStart, &ct0);
1426:       ict = ct0;
1427:       MPI_Bcast(&ict, 1, MPIU_INT, 0, comm);
1428:       ct0 = (DMPolytopeType)ict;
1429:       for (p = pStart; p < pEnd; ++p) {
1430:         DMPolytopeType ct;

1432:         DMPlexGetCellType(dm, p, &ct);
1433:         if (ct == ct0) ++Nc[0];
1434:         else ++Nc[1];
1435:       }
1436:       if (size < maxSize) {
1437:         MPI_Gather(&Nc[0], 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);
1438:         MPI_Gather(&Nc[1], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);
1439:         if (d == depth) MPI_Gather(&gcNum, 1, MPIU_INT, ghostsizes, 1, MPIU_INT, 0, comm);
1440:         PetscViewerASCIIPrintf(viewer, "  Number of %" PetscInt_FMT "-cells per rank:", (depth == 1) && d ? dim : d);
1441:         for (p = 0; p < size; ++p) {
1442:           if (rank == 0) {
1443:             PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT, sizes[p] + hybsizes[p]);
1444:             if (hybsizes[p] > 0) PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ")", hybsizes[p]);
1445:             if (ghostsizes[p] > 0) PetscViewerASCIIPrintf(viewer, " [%" PetscInt_FMT "]", ghostsizes[p]);
1446:           }
1447:         }
1448:       } else {
1449:         PetscInt locMinMax[2];

1451:         locMinMax[0] = Nc[0] + Nc[1];
1452:         locMinMax[1] = Nc[0] + Nc[1];
1453:         PetscGlobalMinMaxInt(comm, locMinMax, sizes);
1454:         locMinMax[0] = Nc[1];
1455:         locMinMax[1] = Nc[1];
1456:         PetscGlobalMinMaxInt(comm, locMinMax, hybsizes);
1457:         if (d == depth) {
1458:           locMinMax[0] = gcNum;
1459:           locMinMax[1] = gcNum;
1460:           PetscGlobalMinMaxInt(comm, locMinMax, ghostsizes);
1461:         }
1462:         PetscViewerASCIIPrintf(viewer, "  Min/Max of %" PetscInt_FMT "-cells per rank:", (depth == 1) && d ? dim : d);
1463:         PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT "/%" PetscInt_FMT, sizes[0], sizes[1]);
1464:         if (hybsizes[0] > 0) PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT "/%" PetscInt_FMT ")", hybsizes[0], hybsizes[1]);
1465:         if (ghostsizes[0] > 0) PetscViewerASCIIPrintf(viewer, " [%" PetscInt_FMT "/%" PetscInt_FMT "]", ghostsizes[0], ghostsizes[1]);
1466:       }
1467:       PetscViewerASCIIPrintf(viewer, "\n");
1468:     }
1469:     PetscFree3(sizes, hybsizes, ghostsizes);
1470:     {
1471:       const PetscReal *maxCell;
1472:       const PetscReal *L;
1473:       PetscBool        localized;

1475:       DMGetPeriodicity(dm, &maxCell, NULL, &L);
1476:       DMGetCoordinatesLocalized(dm, &localized);
1477:       if (L || localized) {
1478:         PetscViewerASCIIPrintf(viewer, "Periodic mesh");
1479:         PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);
1480:         if (L) {
1481:           PetscViewerASCIIPrintf(viewer, " (");
1482:           for (d = 0; d < dim; ++d) {
1483:             if (d > 0) PetscViewerASCIIPrintf(viewer, ", ");
1484:             PetscViewerASCIIPrintf(viewer, "%s", L[d] > 0.0 ? "PERIODIC" : "NONE");
1485:           }
1486:           PetscViewerASCIIPrintf(viewer, ")");
1487:         }
1488:         PetscViewerASCIIPrintf(viewer, " coordinates %s\n", localized ? "localized" : "not localized");
1489:         PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);
1490:       }
1491:     }
1492:     DMGetNumLabels(dm, &numLabels);
1493:     if (numLabels) PetscViewerASCIIPrintf(viewer, "Labels:\n");
1494:     for (l = 0; l < numLabels; ++l) {
1495:       DMLabel         label;
1496:       const char     *name;
1497:       IS              valueIS;
1498:       const PetscInt *values;
1499:       PetscInt        numValues, v;

1501:       DMGetLabelName(dm, l, &name);
1502:       DMGetLabel(dm, name, &label);
1503:       DMLabelGetNumValues(label, &numValues);
1504:       PetscViewerASCIIPrintf(viewer, "  %s: %" PetscInt_FMT " strata with value/size (", name, numValues);
1505:       DMLabelGetValueIS(label, &valueIS);
1506:       ISGetIndices(valueIS, &values);
1507:       PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);
1508:       for (v = 0; v < numValues; ++v) {
1509:         PetscInt size;

1511:         DMLabelGetStratumSize(label, values[v], &size);
1512:         if (v > 0) PetscViewerASCIIPrintf(viewer, ", ");
1513:         PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " (%" PetscInt_FMT ")", values[v], size);
1514:       }
1515:       PetscViewerASCIIPrintf(viewer, ")\n");
1516:       PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);
1517:       ISRestoreIndices(valueIS, &values);
1518:       ISDestroy(&valueIS);
1519:     }
1520:     {
1521:       char    **labelNames;
1522:       PetscInt  Nl = numLabels;
1523:       PetscBool flg;

1525:       PetscMalloc1(Nl, &labelNames);
1526:       PetscOptionsGetStringArray(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_view_labels", labelNames, &Nl, &flg);
1527:       for (l = 0; l < Nl; ++l) {
1528:         DMLabel label;

1530:         DMHasLabel(dm, labelNames[l], &flg);
1531:         if (flg) {
1532:           DMGetLabel(dm, labelNames[l], &label);
1533:           DMLabelView(label, viewer);
1534:         }
1535:         PetscFree(labelNames[l]);
1536:       }
1537:       PetscFree(labelNames);
1538:     }
1539:     /* If no fields are specified, people do not want to see adjacency */
1540:     if (dm->Nf) {
1541:       PetscInt f;

1543:       for (f = 0; f < dm->Nf; ++f) {
1544:         const char *name;

1546:         PetscObjectGetName(dm->fields[f].disc, &name);
1547:         if (numLabels) PetscViewerASCIIPrintf(viewer, "Field %s:\n", name);
1548:         PetscViewerASCIIPushTab(viewer);
1549:         if (dm->fields[f].label) DMLabelView(dm->fields[f].label, viewer);
1550:         if (dm->fields[f].adjacency[0]) {
1551:           if (dm->fields[f].adjacency[1]) PetscViewerASCIIPrintf(viewer, "adjacency FVM++\n");
1552:           else PetscViewerASCIIPrintf(viewer, "adjacency FVM\n");
1553:         } else {
1554:           if (dm->fields[f].adjacency[1]) PetscViewerASCIIPrintf(viewer, "adjacency FEM\n");
1555:           else PetscViewerASCIIPrintf(viewer, "adjacency FUNKY\n");
1556:         }
1557:         PetscViewerASCIIPopTab(viewer);
1558:       }
1559:     }
1560:     DMGetCoarseDM(dm, &cdm);
1561:     if (cdm) {
1562:       PetscViewerASCIIPushTab(viewer);
1563:       DMPlexView_Ascii(cdm, viewer);
1564:       PetscViewerASCIIPopTab(viewer);
1565:     }
1566:   }
1567:   return 0;
1568: }

1570: static PetscErrorCode DMPlexDrawCell(DM dm, PetscDraw draw, PetscInt cell, const PetscScalar coords[])
1571: {
1572:   DMPolytopeType ct;
1573:   PetscMPIInt    rank;
1574:   PetscInt       cdim;

1576:   MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
1577:   DMPlexGetCellType(dm, cell, &ct);
1578:   DMGetCoordinateDim(dm, &cdim);
1579:   switch (ct) {
1580:   case DM_POLYTOPE_SEGMENT:
1581:   case DM_POLYTOPE_POINT_PRISM_TENSOR:
1582:     switch (cdim) {
1583:     case 1: {
1584:       const PetscReal y  = 0.5;  /* TODO Put it in the middle of the viewport */
1585:       const PetscReal dy = 0.05; /* TODO Make it a fraction of the total length */

1587:       PetscDrawLine(draw, PetscRealPart(coords[0]), y, PetscRealPart(coords[1]), y, PETSC_DRAW_BLACK);
1588:       PetscDrawLine(draw, PetscRealPart(coords[0]), y + dy, PetscRealPart(coords[0]), y - dy, PETSC_DRAW_BLACK);
1589:       PetscDrawLine(draw, PetscRealPart(coords[1]), y + dy, PetscRealPart(coords[1]), y - dy, PETSC_DRAW_BLACK);
1590:     } break;
1591:     case 2: {
1592:       const PetscReal dx = (PetscRealPart(coords[3]) - PetscRealPart(coords[1]));
1593:       const PetscReal dy = (PetscRealPart(coords[2]) - PetscRealPart(coords[0]));
1594:       const PetscReal l  = 0.1 / PetscSqrtReal(dx * dx + dy * dy);

1596:       PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);
1597:       PetscDrawLine(draw, PetscRealPart(coords[0]) + l * dx, PetscRealPart(coords[1]) + l * dy, PetscRealPart(coords[0]) - l * dx, PetscRealPart(coords[1]) - l * dy, PETSC_DRAW_BLACK);
1598:       PetscDrawLine(draw, PetscRealPart(coords[2]) + l * dx, PetscRealPart(coords[3]) + l * dy, PetscRealPart(coords[2]) - l * dx, PetscRealPart(coords[3]) - l * dy, PETSC_DRAW_BLACK);
1599:     } break;
1600:     default:
1601:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of dimension %" PetscInt_FMT, cdim);
1602:     }
1603:     break;
1604:   case DM_POLYTOPE_TRIANGLE:
1605:     PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS - 2) + 2, PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS - 2) + 2, PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS - 2) + 2);
1606:     PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);
1607:     PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);
1608:     PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);
1609:     break;
1610:   case DM_POLYTOPE_QUADRILATERAL:
1611:     PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS - 2) + 2, PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS - 2) + 2, PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS - 2) + 2);
1612:     PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS - 2) + 2, PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS - 2) + 2, PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS - 2) + 2);
1613:     PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);
1614:     PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);
1615:     PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);
1616:     PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);
1617:     break;
1618:   case DM_POLYTOPE_FV_GHOST:
1619:     break;
1620:   default:
1621:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of type %s", DMPolytopeTypes[ct]);
1622:   }
1623:   return 0;
1624: }

1626: static PetscErrorCode DMPlexDrawCellHighOrder(DM dm, PetscDraw draw, PetscInt cell, const PetscScalar coords[], PetscInt edgeDiv, PetscReal refCoords[], PetscReal edgeCoords[])
1627: {
1628:   DMPolytopeType ct;
1629:   PetscReal      centroid[2] = {0., 0.};
1630:   PetscMPIInt    rank;
1631:   PetscInt       fillColor, v, e, d;

1633:   MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
1634:   DMPlexGetCellType(dm, cell, &ct);
1635:   fillColor = PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS - 2) + 2;
1636:   switch (ct) {
1637:   case DM_POLYTOPE_TRIANGLE: {
1638:     PetscReal refVertices[6] = {-1., -1., 1., -1., -1., 1.};

1640:     for (v = 0; v < 3; ++v) {
1641:       centroid[0] += PetscRealPart(coords[v * 2 + 0]) / 3.;
1642:       centroid[1] += PetscRealPart(coords[v * 2 + 1]) / 3.;
1643:     }
1644:     for (e = 0; e < 3; ++e) {
1645:       refCoords[0] = refVertices[e * 2 + 0];
1646:       refCoords[1] = refVertices[e * 2 + 1];
1647:       for (d = 1; d <= edgeDiv; ++d) {
1648:         refCoords[d * 2 + 0] = refCoords[0] + (refVertices[(e + 1) % 3 * 2 + 0] - refCoords[0]) * d / edgeDiv;
1649:         refCoords[d * 2 + 1] = refCoords[1] + (refVertices[(e + 1) % 3 * 2 + 1] - refCoords[1]) * d / edgeDiv;
1650:       }
1651:       DMPlexReferenceToCoordinates(dm, cell, edgeDiv + 1, refCoords, edgeCoords);
1652:       for (d = 0; d < edgeDiv; ++d) {
1653:         PetscDrawTriangle(draw, centroid[0], centroid[1], edgeCoords[d * 2 + 0], edgeCoords[d * 2 + 1], edgeCoords[(d + 1) * 2 + 0], edgeCoords[(d + 1) * 2 + 1], fillColor, fillColor, fillColor);
1654:         PetscDrawLine(draw, edgeCoords[d * 2 + 0], edgeCoords[d * 2 + 1], edgeCoords[(d + 1) * 2 + 0], edgeCoords[(d + 1) * 2 + 1], PETSC_DRAW_BLACK);
1655:       }
1656:     }
1657:   } break;
1658:   default:
1659:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of type %s", DMPolytopeTypes[ct]);
1660:   }
1661:   return 0;
1662: }

1664: static PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
1665: {
1666:   PetscDraw          draw;
1667:   DM                 cdm;
1668:   PetscSection       coordSection;
1669:   Vec                coordinates;
1670:   const PetscScalar *coords;
1671:   PetscReal          xyl[2], xyr[2], bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
1672:   PetscReal         *refCoords, *edgeCoords;
1673:   PetscBool          isnull, drawAffine = PETSC_TRUE;
1674:   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N, edgeDiv = 4;

1676:   DMGetCoordinateDim(dm, &dim);
1678:   PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_view_draw_affine", &drawAffine, NULL);
1679:   if (!drawAffine) PetscMalloc2((edgeDiv + 1) * dim, &refCoords, (edgeDiv + 1) * dim, &edgeCoords);
1680:   DMGetCoordinateDM(dm, &cdm);
1681:   DMGetLocalSection(cdm, &coordSection);
1682:   DMGetCoordinatesLocal(dm, &coordinates);
1683:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
1684:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);

1686:   PetscViewerDrawGetDraw(viewer, 0, &draw);
1687:   PetscDrawIsNull(draw, &isnull);
1688:   if (isnull) return 0;
1689:   PetscDrawSetTitle(draw, "Mesh");

1691:   VecGetLocalSize(coordinates, &N);
1692:   VecGetArrayRead(coordinates, &coords);
1693:   for (c = 0; c < N; c += dim) {
1694:     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));
1695:     bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
1696:     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c + 1]));
1697:     bound[3] = PetscMax(bound[3], PetscRealPart(coords[c + 1]));
1698:   }
1699:   VecRestoreArrayRead(coordinates, &coords);
1700:   MPIU_Allreduce(&bound[0], xyl, 2, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)dm));
1701:   MPIU_Allreduce(&bound[2], xyr, 2, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject)dm));
1702:   PetscDrawSetCoordinates(draw, xyl[0], xyl[1], xyr[0], xyr[1]);
1703:   PetscDrawClear(draw);

1705:   for (c = cStart; c < cEnd; ++c) {
1706:     PetscScalar *coords = NULL;
1707:     PetscInt     numCoords;

1709:     DMPlexVecGetClosureAtDepth_Internal(dm, coordSection, coordinates, c, 0, &numCoords, &coords);
1710:     if (drawAffine) DMPlexDrawCell(dm, draw, c, coords);
1711:     else DMPlexDrawCellHighOrder(dm, draw, c, coords, edgeDiv, refCoords, edgeCoords);
1712:     DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);
1713:   }
1714:   if (!drawAffine) PetscFree2(refCoords, edgeCoords);
1715:   PetscDrawFlush(draw);
1716:   PetscDrawPause(draw);
1717:   PetscDrawSave(draw);
1718:   return 0;
1719: }

1721: #if defined(PETSC_HAVE_EXODUSII)
1722:   #include <exodusII.h>
1723: #include <petscviewerexodusii.h>
1724: #endif

1726: PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
1727: {
1728:   PetscBool iascii, ishdf5, isvtk, isdraw, flg, isglvis, isexodus, iscgns;
1729:   char      name[PETSC_MAX_PATH_LEN];

1733:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii);
1734:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERVTK, &isvtk);
1735:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
1736:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw);
1737:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERGLVIS, &isglvis);
1738:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWEREXODUSII, &isexodus);
1739:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERCGNS, &iscgns);
1740:   if (iascii) {
1741:     PetscViewerFormat format;
1742:     PetscViewerGetFormat(viewer, &format);
1743:     if (format == PETSC_VIEWER_ASCII_GLVIS) DMPlexView_GLVis(dm, viewer);
1744:     else DMPlexView_Ascii(dm, viewer);
1745:   } else if (ishdf5) {
1746: #if defined(PETSC_HAVE_HDF5)
1747:     DMPlexView_HDF5_Internal(dm, viewer);
1748: #else
1749:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1750: #endif
1751:   } else if (isvtk) {
1752:     DMPlexVTKWriteAll((PetscObject)dm, viewer);
1753:   } else if (isdraw) {
1754:     DMPlexView_Draw(dm, viewer);
1755:   } else if (isglvis) {
1756:     DMPlexView_GLVis(dm, viewer);
1757: #if defined(PETSC_HAVE_EXODUSII)
1758:   } else if (isexodus) {
1759:     /*
1760:       exodusII requires that all sets be part of exactly one cell set.
1761:       If the dm does not have a "Cell Sets" label defined, we create one
1762:       with ID 1, containig all cells.
1763:       Note that if the Cell Sets label is defined but does not cover all cells,
1764:       we may still have a problem. This should probably be checked here or in the viewer;
1765:     */
1766:     PetscInt numCS;
1767:     DMGetLabelSize(dm, "Cell Sets", &numCS);
1768:     if (!numCS) {
1769:       PetscInt cStart, cEnd, c;
1770:       DMCreateLabel(dm, "Cell Sets");
1771:       DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
1772:       for (c = cStart; c < cEnd; ++c) DMSetLabelValue(dm, "Cell Sets", c, 1);
1773:     }
1774:     DMView_PlexExodusII(dm, viewer);
1775: #endif
1776: #if defined(PETSC_HAVE_CGNS)
1777:   } else if (iscgns) {
1778:     DMView_PlexCGNS(dm, viewer);
1779: #endif
1780:   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex writing", ((PetscObject)viewer)->type_name);
1781:   /* Optionally view the partition */
1782:   PetscOptionsHasName(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_partition_view", &flg);
1783:   if (flg) {
1784:     Vec ranks;
1785:     DMPlexCreateRankField(dm, &ranks);
1786:     VecView(ranks, viewer);
1787:     VecDestroy(&ranks);
1788:   }
1789:   /* Optionally view a label */
1790:   PetscOptionsGetString(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_label_view", name, sizeof(name), &flg);
1791:   if (flg) {
1792:     DMLabel label;
1793:     Vec     val;

1795:     DMGetLabel(dm, name, &label);
1797:     DMPlexCreateLabelField(dm, label, &val);
1798:     VecView(val, viewer);
1799:     VecDestroy(&val);
1800:   }
1801:   return 0;
1802: }

1804: /*@
1805:   DMPlexTopologyView - Saves a `DMPLEX` topology into a file

1807:   Collective on dm

1809:   Input Parameters:
1810: + dm     - The `DM` whose topology is to be saved
1811: - viewer - The `PetscViewer` to save it in

1813:   Level: advanced

1815: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMView()`, `DMPlexCoordinatesView()`, `DMPlexLabelsView()`, `DMPlexTopologyLoad()`, `PetscViewer`
1816: @*/
1817: PetscErrorCode DMPlexTopologyView(DM dm, PetscViewer viewer)
1818: {
1819:   PetscBool ishdf5;

1823:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
1824:   PetscLogEventBegin(DMPLEX_TopologyView, viewer, 0, 0, 0);
1825:   if (ishdf5) {
1826: #if defined(PETSC_HAVE_HDF5)
1827:     PetscViewerFormat format;
1828:     PetscViewerGetFormat(viewer, &format);
1829:     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
1830:       IS globalPointNumbering;

1832:       DMPlexCreatePointNumbering(dm, &globalPointNumbering);
1833:       DMPlexTopologyView_HDF5_Internal(dm, globalPointNumbering, viewer);
1834:       ISDestroy(&globalPointNumbering);
1835:     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 output.", PetscViewerFormats[format]);
1836: #else
1837:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1838: #endif
1839:   }
1840:   PetscLogEventEnd(DMPLEX_TopologyView, viewer, 0, 0, 0);
1841:   return 0;
1842: }

1844: /*@
1845:   DMPlexCoordinatesView - Saves `DMPLEX` coordinates into a file

1847:   Collective on dm

1849:   Input Parameters:
1850: + dm     - The `DM` whose coordinates are to be saved
1851: - viewer - The `PetscViewer` for saving

1853:   Level: advanced

1855: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMView()`, `DMPlexTopologyView()`, `DMPlexLabelsView()`, `DMPlexCoordinatesLoad()`, `PetscViewer`
1856: @*/
1857: PetscErrorCode DMPlexCoordinatesView(DM dm, PetscViewer viewer)
1858: {
1859:   PetscBool ishdf5;

1863:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
1864:   PetscLogEventBegin(DMPLEX_CoordinatesView, viewer, 0, 0, 0);
1865:   if (ishdf5) {
1866: #if defined(PETSC_HAVE_HDF5)
1867:     PetscViewerFormat format;
1868:     PetscViewerGetFormat(viewer, &format);
1869:     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
1870:       DMPlexCoordinatesView_HDF5_Internal(dm, viewer);
1871:     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 output.", PetscViewerFormats[format]);
1872: #else
1873:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1874: #endif
1875:   }
1876:   PetscLogEventEnd(DMPLEX_CoordinatesView, viewer, 0, 0, 0);
1877:   return 0;
1878: }

1880: /*@
1881:   DMPlexLabelsView - Saves `DMPLEX` labels into a file

1883:   Collective on dm

1885:   Input Parameters:
1886: + dm     - The `DM` whose labels are to be saved
1887: - viewer - The `PetscViewer` for saving

1889:   Level: advanced

1891: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMView()`, `DMPlexTopologyView()`, `DMPlexCoordinatesView()`, `DMPlexLabelsLoad()`, `PetscViewer`
1892: @*/
1893: PetscErrorCode DMPlexLabelsView(DM dm, PetscViewer viewer)
1894: {
1895:   PetscBool ishdf5;

1899:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
1900:   PetscLogEventBegin(DMPLEX_LabelsView, viewer, 0, 0, 0);
1901:   if (ishdf5) {
1902: #if defined(PETSC_HAVE_HDF5)
1903:     IS                globalPointNumbering;
1904:     PetscViewerFormat format;

1906:     PetscViewerGetFormat(viewer, &format);
1907:     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
1908:       DMPlexCreatePointNumbering(dm, &globalPointNumbering);
1909:       DMPlexLabelsView_HDF5_Internal(dm, globalPointNumbering, viewer);
1910:       ISDestroy(&globalPointNumbering);
1911:     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
1912: #else
1913:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1914: #endif
1915:   }
1916:   PetscLogEventEnd(DMPLEX_LabelsView, viewer, 0, 0, 0);
1917:   return 0;
1918: }

1920: /*@
1921:   DMPlexSectionView - Saves a section associated with a `DMPLEX`

1923:   Collective on dm

1925:   Input Parameters:
1926: + dm         - The `DM` that contains the topology on which the section to be saved is defined
1927: . viewer     - The `PetscViewer` for saving
1928: - sectiondm  - The `DM` that contains the section to be saved

1930:   Level: advanced

1932:   Notes:
1933:   This function is a wrapper around `PetscSectionView()`; in addition to the raw section, it saves information that associates the section points to the topology (dm) points. When the topology (dm) and the section are later loaded with `DMPlexTopologyLoad()` and `DMPlexSectionLoad()`, respectively, this information is used to match section points with topology points.

1935:   In general dm and sectiondm are two different objects, the former carrying the topology and the latter carrying the section, and have been given a topology name and a section name, respectively, with `PetscObjectSetName()`. In practice, however, they can be the same object if it carries both topology and section; in that case the name of the object is used as both the topology name and the section name.

1937: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMView()`, `DMPlexTopologyView()`, `DMPlexCoordinatesView()`, `DMPlexLabelsView()`, `DMPlexGlobalVectorView()`, `DMPlexLocalVectorView()`, `PetscSectionView()`, `DMPlexSectionLoad()`, `PetscViewer`
1938: @*/
1939: PetscErrorCode DMPlexSectionView(DM dm, PetscViewer viewer, DM sectiondm)
1940: {
1941:   PetscBool ishdf5;

1946:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
1947:   PetscLogEventBegin(DMPLEX_SectionView, viewer, 0, 0, 0);
1948:   if (ishdf5) {
1949: #if defined(PETSC_HAVE_HDF5)
1950:     DMPlexSectionView_HDF5_Internal(dm, viewer, sectiondm);
1951: #else
1952:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1953: #endif
1954:   }
1955:   PetscLogEventEnd(DMPLEX_SectionView, viewer, 0, 0, 0);
1956:   return 0;
1957: }

1959: /*@
1960:   DMPlexGlobalVectorView - Saves a global vector

1962:   Collective on dm

1964:   Input Parameters:
1965: + dm        - The `DM` that represents the topology
1966: . viewer    - The `PetscViewer` to save data with
1967: . sectiondm - The `DM` that contains the global section on which vec is defined
1968: - vec       - The global vector to be saved

1970:   Level: advanced

1972:   Notes:
1973:   In general dm and sectiondm are two different objects, the former carrying the topology and the latter carrying the section, and have been given a topology name and a section name, respectively, with `PetscObjectSetName()`. In practice, however, they can be the same object if it carries both topology and section; in that case the name of the object is used as both the topology name and the section name.

1975:   Typical calling sequence:
1976: .vb
1977:        DMCreate(PETSC_COMM_WORLD, &dm);
1978:        DMSetType(dm, DMPLEX);
1979:        PetscObjectSetName((PetscObject)dm, "topologydm_name");
1980:        DMClone(dm, &sectiondm);
1981:        PetscObjectSetName((PetscObject)sectiondm, "sectiondm_name");
1982:        PetscSectionCreate(PETSC_COMM_WORLD, &section);
1983:        DMPlexGetChart(sectiondm, &pStart, &pEnd);
1984:        PetscSectionSetChart(section, pStart, pEnd);
1985:        PetscSectionSetUp(section);
1986:        DMSetLocalSection(sectiondm, section);
1987:        PetscSectionDestroy(&section);
1988:        DMGetGlobalVector(sectiondm, &vec);
1989:        PetscObjectSetName((PetscObject)vec, "vec_name");
1990:        DMPlexTopologyView(dm, viewer);
1991:        DMPlexSectionView(dm, viewer, sectiondm);
1992:        DMPlexGlobalVectorView(dm, viewer, sectiondm, vec);
1993:        DMRestoreGlobalVector(sectiondm, &vec);
1994:        DMDestroy(&sectiondm);
1995:        DMDestroy(&dm);
1996: .ve

1998: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexTopologyView()`, `DMPlexSectionView()`, `DMPlexLocalVectorView()`, `DMPlexGlobalVectorLoad()`, `DMPlexLocalVectorLoad()`
1999: @*/
2000: PetscErrorCode DMPlexGlobalVectorView(DM dm, PetscViewer viewer, DM sectiondm, Vec vec)
2001: {
2002:   PetscBool ishdf5;

2008:   /* Check consistency */
2009:   {
2010:     PetscSection section;
2011:     PetscBool    includesConstraints;
2012:     PetscInt     m, m1;

2014:     VecGetLocalSize(vec, &m1);
2015:     DMGetGlobalSection(sectiondm, &section);
2016:     PetscSectionGetIncludesConstraints(section, &includesConstraints);
2017:     if (includesConstraints) PetscSectionGetStorageSize(section, &m);
2018:     else PetscSectionGetConstrainedStorageSize(section, &m);
2020:   }
2021:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
2022:   PetscLogEventBegin(DMPLEX_GlobalVectorView, viewer, 0, 0, 0);
2023:   if (ishdf5) {
2024: #if defined(PETSC_HAVE_HDF5)
2025:     DMPlexGlobalVectorView_HDF5_Internal(dm, viewer, sectiondm, vec);
2026: #else
2027:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2028: #endif
2029:   }
2030:   PetscLogEventEnd(DMPLEX_GlobalVectorView, viewer, 0, 0, 0);
2031:   return 0;
2032: }

2034: /*@
2035:   DMPlexLocalVectorView - Saves a local vector

2037:   Collective on dm

2039:   Input Parameters:
2040: + dm        - The `DM` that represents the topology
2041: . viewer    - The `PetscViewer` to save data with
2042: . sectiondm - The `DM` that contains the local section on which vec is defined; may be the same as dm
2043: - vec       - The local vector to be saved

2045:   Level: advanced

2047:   Note:
2048:   In general dm and sectiondm are two different objects, the former carrying the topology and the latter carrying the section, and have been given a topology name and a section name, respectively, with `PetscObjectSetName()`. In practice, however, they can be the same object if it carries both topology and section; in that case the name of the object is used as both the topology name and the section name.

2050:   Typical calling sequence:
2051: .vb
2052:        DMCreate(PETSC_COMM_WORLD, &dm);
2053:        DMSetType(dm, DMPLEX);
2054:        PetscObjectSetName((PetscObject)dm, "topologydm_name");
2055:        DMClone(dm, &sectiondm);
2056:        PetscObjectSetName((PetscObject)sectiondm, "sectiondm_name");
2057:        PetscSectionCreate(PETSC_COMM_WORLD, &section);
2058:        DMPlexGetChart(sectiondm, &pStart, &pEnd);
2059:        PetscSectionSetChart(section, pStart, pEnd);
2060:        PetscSectionSetUp(section);
2061:        DMSetLocalSection(sectiondm, section);
2062:        DMGetLocalVector(sectiondm, &vec);
2063:        PetscObjectSetName((PetscObject)vec, "vec_name");
2064:        DMPlexTopologyView(dm, viewer);
2065:        DMPlexSectionView(dm, viewer, sectiondm);
2066:        DMPlexLocalVectorView(dm, viewer, sectiondm, vec);
2067:        DMRestoreLocalVector(sectiondm, &vec);
2068:        DMDestroy(&sectiondm);
2069:        DMDestroy(&dm);
2070: .ve

2072: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexTopologyView()`, `DMPlexSectionView()`, `DMPlexGlobalVectorView()`, `DMPlexGlobalVectorLoad()`, `DMPlexLocalVectorLoad()`
2073: @*/
2074: PetscErrorCode DMPlexLocalVectorView(DM dm, PetscViewer viewer, DM sectiondm, Vec vec)
2075: {
2076:   PetscBool ishdf5;

2082:   /* Check consistency */
2083:   {
2084:     PetscSection section;
2085:     PetscBool    includesConstraints;
2086:     PetscInt     m, m1;

2088:     VecGetLocalSize(vec, &m1);
2089:     DMGetLocalSection(sectiondm, &section);
2090:     PetscSectionGetIncludesConstraints(section, &includesConstraints);
2091:     if (includesConstraints) PetscSectionGetStorageSize(section, &m);
2092:     else PetscSectionGetConstrainedStorageSize(section, &m);
2094:   }
2095:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
2096:   PetscLogEventBegin(DMPLEX_LocalVectorView, viewer, 0, 0, 0);
2097:   if (ishdf5) {
2098: #if defined(PETSC_HAVE_HDF5)
2099:     DMPlexLocalVectorView_HDF5_Internal(dm, viewer, sectiondm, vec);
2100: #else
2101:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2102: #endif
2103:   }
2104:   PetscLogEventEnd(DMPLEX_LocalVectorView, viewer, 0, 0, 0);
2105:   return 0;
2106: }

2108: PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
2109: {
2110:   PetscBool ishdf5;

2114:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
2115:   if (ishdf5) {
2116: #if defined(PETSC_HAVE_HDF5)
2117:     PetscViewerFormat format;
2118:     PetscViewerGetFormat(viewer, &format);
2119:     if (format == PETSC_VIEWER_HDF5_XDMF || format == PETSC_VIEWER_HDF5_VIZ) {
2120:       DMPlexLoad_HDF5_Xdmf_Internal(dm, viewer);
2121:     } else if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
2122:       DMPlexLoad_HDF5_Internal(dm, viewer);
2123:     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
2124:     return 0;
2125: #else
2126:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2127: #endif
2128:   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex loading", ((PetscObject)viewer)->type_name);
2129: }

2131: /*@
2132:   DMPlexTopologyLoad - Loads a topology into a `DMPLEX`

2134:   Collective on dm

2136:   Input Parameters:
2137: + dm                - The `DM` into which the topology is loaded
2138: - viewer            - The `PetscViewer` for the saved topology

2140:   Output Parameters:
2141: . globalToLocalPointSF - The `PetscSF` that pushes points in [0, N) to the associated points in the loaded plex, where N is the global number of points; NULL if unneeded

2143:   Level: advanced

2145: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMLoad()`, `DMPlexCoordinatesLoad()`, `DMPlexLabelsLoad()`, `DMView()`, `PetscViewerHDF5Open()`, `PetscViewerPushFormat()`,
2146:           `PetscViewer`, `PetscSF`
2147: @*/
2148: PetscErrorCode DMPlexTopologyLoad(DM dm, PetscViewer viewer, PetscSF *globalToLocalPointSF)
2149: {
2150:   PetscBool ishdf5;

2155:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
2156:   PetscLogEventBegin(DMPLEX_TopologyLoad, viewer, 0, 0, 0);
2157:   if (ishdf5) {
2158: #if defined(PETSC_HAVE_HDF5)
2159:     PetscViewerFormat format;
2160:     PetscViewerGetFormat(viewer, &format);
2161:     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
2162:       DMPlexTopologyLoad_HDF5_Internal(dm, viewer, globalToLocalPointSF);
2163:     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
2164: #else
2165:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2166: #endif
2167:   }
2168:   PetscLogEventEnd(DMPLEX_TopologyLoad, viewer, 0, 0, 0);
2169:   return 0;
2170: }

2172: /*@
2173:   DMPlexCoordinatesLoad - Loads coordinates into a `DMPLEX`

2175:   Collective on dm

2177:   Input Parameters:
2178: + dm     - The `DM` into which the coordinates are loaded
2179: . viewer - The `PetscViewer` for the saved coordinates
2180: - globalToLocalPointSF - The `PetscSF` returned by `DMPlexTopologyLoad()` when loading dm from viewer

2182:   Level: advanced

2184: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMLoad()`, `DMPlexTopologyLoad()`, `DMPlexLabelsLoad()`, `DMView()`, `PetscViewerHDF5Open()`, `PetscViewerPushFormat()`,
2185:           `PetscSF`, `PetscViewer`
2186: @*/
2187: PetscErrorCode DMPlexCoordinatesLoad(DM dm, PetscViewer viewer, PetscSF globalToLocalPointSF)
2188: {
2189:   PetscBool ishdf5;

2194:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
2195:   PetscLogEventBegin(DMPLEX_CoordinatesLoad, viewer, 0, 0, 0);
2196:   if (ishdf5) {
2197: #if defined(PETSC_HAVE_HDF5)
2198:     PetscViewerFormat format;
2199:     PetscViewerGetFormat(viewer, &format);
2200:     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
2201:       DMPlexCoordinatesLoad_HDF5_Internal(dm, viewer, globalToLocalPointSF);
2202:     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
2203: #else
2204:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2205: #endif
2206:   }
2207:   PetscLogEventEnd(DMPLEX_CoordinatesLoad, viewer, 0, 0, 0);
2208:   return 0;
2209: }

2211: /*@
2212:   DMPlexLabelsLoad - Loads labels into a `DMPLEX`

2214:   Collective on dm

2216:   Input Parameters:
2217: + dm     - The `DM` into which the labels are loaded
2218: . viewer - The `PetscViewer` for the saved labels
2219: - globalToLocalPointSF - The `PetscSF` returned by `DMPlexTopologyLoad()` when loading dm from viewer

2221:   Level: advanced

2223:   Note:
2224:   The `PetscSF` argument must not be NULL if the `DM` is distributed, otherwise an error occurs.

2226: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMLoad()`, `DMPlexTopologyLoad()`, `DMPlexCoordinatesLoad()`, `DMView()`, `PetscViewerHDF5Open()`, `PetscViewerPushFormat()`,
2227:           `PetscSF`, `PetscViewer`
2228: @*/
2229: PetscErrorCode DMPlexLabelsLoad(DM dm, PetscViewer viewer, PetscSF globalToLocalPointSF)
2230: {
2231:   PetscBool ishdf5;

2236:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
2237:   PetscLogEventBegin(DMPLEX_LabelsLoad, viewer, 0, 0, 0);
2238:   if (ishdf5) {
2239: #if defined(PETSC_HAVE_HDF5)
2240:     PetscViewerFormat format;

2242:     PetscViewerGetFormat(viewer, &format);
2243:     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
2244:       DMPlexLabelsLoad_HDF5_Internal(dm, viewer, globalToLocalPointSF);
2245:     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
2246: #else
2247:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2248: #endif
2249:   }
2250:   PetscLogEventEnd(DMPLEX_LabelsLoad, viewer, 0, 0, 0);
2251:   return 0;
2252: }

2254: /*@
2255:   DMPlexSectionLoad - Loads section into a `DMPLEX`

2257:   Collective on dm

2259:   Input Parameters:
2260: + dm          - The `DM` that represents the topology
2261: . viewer      - The `PetscViewer` that represents the on-disk section (sectionA)
2262: . sectiondm   - The `DM` into which the on-disk section (sectionA) is migrated
2263: - globalToLocalPointSF - The `PetscSF` returned by `DMPlexTopologyLoad(`) when loading dm from viewer

2265:   Output Parameters
2266: + globalDofSF - The SF that migrates any on-disk Vec data associated with sectionA into a global Vec associated with the sectiondm's global section (NULL if not needed)
2267: - localDofSF  - The SF that migrates any on-disk Vec data associated with sectionA into a local Vec associated with the sectiondm's local section (NULL if not needed)

2269:   Level: advanced

2271:   Notes:
2272:   This function is a wrapper around `PetscSectionLoad()`; it loads, in addition to the raw section, a list of global point numbers that associates each on-disk section point with a global point number in [0, NX), where NX is the number of topology points in dm. Noting that globalToLocalPointSF associates each topology point in dm with a global number in [0, NX), one can readily establish an association of the on-disk section points with the topology points.

2274:   In general dm and sectiondm are two different objects, the former carrying the topology and the latter carrying the section, and have been given a topology name and a section name, respectively, with `PetscObjectSetName()`. In practice, however, they can be the same object if it carries both topology and section; in that case the name of the object is used as both the topology name and the section name.

2276:   The output parameter, globalDofSF (localDofSF), can later be used with `DMPlexGlobalVectorLoad()` (`DMPlexLocalVectorLoad()`) to load on-disk vectors into global (local) vectors associated with sectiondm's global (local) section.

2278:   Example using 2 processes:
2279: .vb
2280:   NX (number of points on dm): 4
2281:   sectionA                   : the on-disk section
2282:   vecA                       : a vector associated with sectionA
2283:   sectionB                   : sectiondm's local section constructed in this function
2284:   vecB (local)               : a vector associated with sectiondm's local section
2285:   vecB (global)              : a vector associated with sectiondm's global section

2287:                                      rank 0    rank 1
2288:   vecA (global)                  : [.0 .4 .1 | .2 .3]        <- to be loaded in DMPlexGlobalVectorLoad() or DMPlexLocalVectorLoad()
2289:   sectionA->atlasOff             :       0 2 | 1             <- loaded in PetscSectionLoad()
2290:   sectionA->atlasDof             :       1 3 | 1             <- loaded in PetscSectionLoad()
2291:   sectionA's global point numbers:       0 2 | 3             <- loaded in DMPlexSectionLoad()
2292:   [0, NX)                        :       0 1 | 2 3           <- conceptual partition used in globalToLocalPointSF
2293:   sectionB's global point numbers:     0 1 3 | 3 2           <- associated with [0, NX) by globalToLocalPointSF
2294:   sectionB->atlasDof             :     1 0 1 | 1 3
2295:   sectionB->atlasOff (no perm)   :     0 1 1 | 0 1
2296:   vecB (local)                   :   [.0 .4] | [.4 .1 .2 .3] <- to be constructed by calling DMPlexLocalVectorLoad() with localDofSF
2297:   vecB (global)                  :    [.0 .4 | .1 .2 .3]     <- to be constructed by calling DMPlexGlobalVectorLoad() with globalDofSF
2298: .ve
2299:   where "|" represents a partition of loaded data, and global point 3 is assumed to be owned by rank 0.

2301: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMLoad()`, `DMPlexTopologyLoad()`, `DMPlexCoordinatesLoad()`, `DMPlexLabelsLoad()`, `DMPlexGlobalVectorLoad()`, `DMPlexLocalVectorLoad()`, `PetscSectionLoad()`, `DMPlexSectionView()`, `PetscSF`, `PetscViewer`
2302: @*/
2303: PetscErrorCode DMPlexSectionLoad(DM dm, PetscViewer viewer, DM sectiondm, PetscSF globalToLocalPointSF, PetscSF *globalDofSF, PetscSF *localDofSF)
2304: {
2305:   PetscBool ishdf5;

2313:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
2314:   PetscLogEventBegin(DMPLEX_SectionLoad, viewer, 0, 0, 0);
2315:   if (ishdf5) {
2316: #if defined(PETSC_HAVE_HDF5)
2317:     DMPlexSectionLoad_HDF5_Internal(dm, viewer, sectiondm, globalToLocalPointSF, globalDofSF, localDofSF);
2318: #else
2319:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2320: #endif
2321:   }
2322:   PetscLogEventEnd(DMPLEX_SectionLoad, viewer, 0, 0, 0);
2323:   return 0;
2324: }

2326: /*@
2327:   DMPlexGlobalVectorLoad - Loads on-disk vector data into a global vector

2329:   Collective on dm

2331:   Input Parameters:
2332: + dm        - The `DM` that represents the topology
2333: . viewer    - The `PetscViewer` that represents the on-disk vector data
2334: . sectiondm - The `DM` that contains the global section on which vec is defined
2335: . sf        - The `PetscSF` that migrates the on-disk vector data into vec
2336: - vec       - The global vector to set values of

2338:   Level: advanced

2340:   Notes:
2341:   In general dm and sectiondm are two different objects, the former carrying the topology and the latter carrying the section, and have been given a topology name and a section name, respectively, with `PetscObjectSetName()`. In practice, however, they can be the same object if it carries both topology and section; in that case the name of the object is used as both the topology name and the section name.

2343:   Typical calling sequence:
2344: .vb
2345:        DMCreate(PETSC_COMM_WORLD, &dm);
2346:        DMSetType(dm, DMPLEX);
2347:        PetscObjectSetName((PetscObject)dm, "topologydm_name");
2348:        DMPlexTopologyLoad(dm, viewer, &sfX);
2349:        DMClone(dm, &sectiondm);
2350:        PetscObjectSetName((PetscObject)sectiondm, "sectiondm_name");
2351:        DMPlexSectionLoad(dm, viewer, sectiondm, sfX, &gsf, NULL);
2352:        DMGetGlobalVector(sectiondm, &vec);
2353:        PetscObjectSetName((PetscObject)vec, "vec_name");
2354:        DMPlexGlobalVectorLoad(dm, viewer, sectiondm, gsf, vec);
2355:        DMRestoreGlobalVector(sectiondm, &vec);
2356:        PetscSFDestroy(&gsf);
2357:        PetscSFDestroy(&sfX);
2358:        DMDestroy(&sectiondm);
2359:        DMDestroy(&dm);
2360: .ve

2362: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexTopologyLoad()`, `DMPlexSectionLoad()`, `DMPlexLocalVectorLoad()`, `DMPlexGlobalVectorView()`, `DMPlexLocalVectorView()`,
2363:           `PetscSF`, `PetscViewer`
2364: @*/
2365: PetscErrorCode DMPlexGlobalVectorLoad(DM dm, PetscViewer viewer, DM sectiondm, PetscSF sf, Vec vec)
2366: {
2367:   PetscBool ishdf5;

2374:   /* Check consistency */
2375:   {
2376:     PetscSection section;
2377:     PetscBool    includesConstraints;
2378:     PetscInt     m, m1;

2380:     VecGetLocalSize(vec, &m1);
2381:     DMGetGlobalSection(sectiondm, &section);
2382:     PetscSectionGetIncludesConstraints(section, &includesConstraints);
2383:     if (includesConstraints) PetscSectionGetStorageSize(section, &m);
2384:     else PetscSectionGetConstrainedStorageSize(section, &m);
2386:   }
2387:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
2388:   PetscLogEventBegin(DMPLEX_GlobalVectorLoad, viewer, 0, 0, 0);
2389:   if (ishdf5) {
2390: #if defined(PETSC_HAVE_HDF5)
2391:     DMPlexVecLoad_HDF5_Internal(dm, viewer, sectiondm, sf, vec);
2392: #else
2393:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2394: #endif
2395:   }
2396:   PetscLogEventEnd(DMPLEX_GlobalVectorLoad, viewer, 0, 0, 0);
2397:   return 0;
2398: }

2400: /*@
2401:   DMPlexLocalVectorLoad - Loads on-disk vector data into a local vector

2403:   Collective on dm

2405:   Input Parameters:
2406: + dm        - The `DM` that represents the topology
2407: . viewer    - The `PetscViewer` that represents the on-disk vector data
2408: . sectiondm - The `DM` that contains the local section on which vec is defined
2409: . sf        - The `PetscSF` that migrates the on-disk vector data into vec
2410: - vec       - The local vector to set values of

2412:   Level: advanced

2414:   Notes:
2415:   In general dm and sectiondm are two different objects, the former carrying the topology and the latter carrying the section, and have been given a topology name and a section name, respectively, with `PetscObjectSetName()`. In practice, however, they can be the same object if it carries both topology and section; in that case the name of the object is used as both the topology name and the section name.

2417:   Typical calling sequence:
2418: .vb
2419:        DMCreate(PETSC_COMM_WORLD, &dm);
2420:        DMSetType(dm, DMPLEX);
2421:        PetscObjectSetName((PetscObject)dm, "topologydm_name");
2422:        DMPlexTopologyLoad(dm, viewer, &sfX);
2423:        DMClone(dm, &sectiondm);
2424:        PetscObjectSetName((PetscObject)sectiondm, "sectiondm_name");
2425:        DMPlexSectionLoad(dm, viewer, sectiondm, sfX, NULL, &lsf);
2426:        DMGetLocalVector(sectiondm, &vec);
2427:        PetscObjectSetName((PetscObject)vec, "vec_name");
2428:        DMPlexLocalVectorLoad(dm, viewer, sectiondm, lsf, vec);
2429:        DMRestoreLocalVector(sectiondm, &vec);
2430:        PetscSFDestroy(&lsf);
2431:        PetscSFDestroy(&sfX);
2432:        DMDestroy(&sectiondm);
2433:        DMDestroy(&dm);
2434: .ve

2436: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexTopologyLoad()`, `DMPlexSectionLoad()`, `DMPlexGlobalVectorLoad()`, `DMPlexGlobalVectorView()`, `DMPlexLocalVectorView()`,
2437:           `PetscSF`, `PetscViewer`
2438: @*/
2439: PetscErrorCode DMPlexLocalVectorLoad(DM dm, PetscViewer viewer, DM sectiondm, PetscSF sf, Vec vec)
2440: {
2441:   PetscBool ishdf5;

2448:   /* Check consistency */
2449:   {
2450:     PetscSection section;
2451:     PetscBool    includesConstraints;
2452:     PetscInt     m, m1;

2454:     VecGetLocalSize(vec, &m1);
2455:     DMGetLocalSection(sectiondm, &section);
2456:     PetscSectionGetIncludesConstraints(section, &includesConstraints);
2457:     if (includesConstraints) PetscSectionGetStorageSize(section, &m);
2458:     else PetscSectionGetConstrainedStorageSize(section, &m);
2460:   }
2461:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
2462:   PetscLogEventBegin(DMPLEX_LocalVectorLoad, viewer, 0, 0, 0);
2463:   if (ishdf5) {
2464: #if defined(PETSC_HAVE_HDF5)
2465:     DMPlexVecLoad_HDF5_Internal(dm, viewer, sectiondm, sf, vec);
2466: #else
2467:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2468: #endif
2469:   }
2470:   PetscLogEventEnd(DMPLEX_LocalVectorLoad, viewer, 0, 0, 0);
2471:   return 0;
2472: }

2474: PetscErrorCode DMDestroy_Plex(DM dm)
2475: {
2476:   DM_Plex *mesh = (DM_Plex *)dm->data;

2478:   PetscObjectComposeFunction((PetscObject)dm, "DMSetUpGLVisViewer_C", NULL);
2479:   PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBoundaryValues_C", NULL);
2480:   PetscObjectComposeFunction((PetscObject)dm, "DMCreateNeumannOverlap_C", NULL);
2481:   PetscObjectComposeFunction((PetscObject)dm, "DMInterpolateSolution_C", NULL);
2482:   PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertTimeDerviativeBoundaryValues_C", NULL);
2483:   PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetOverlap_C", NULL);
2484:   PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeGetDefault_C", NULL);
2485:   PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeSetDefault_C", NULL);
2486:   PetscObjectComposeFunction((PetscObject)dm, "MatComputeNeumannOverlap_C", NULL);
2487:   PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderGetDefault_C", NULL);
2488:   PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderSetDefault_C", NULL);
2489:   PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetOverlap_C", NULL);
2490:   PetscObjectComposeFunction((PetscObject)dm, "DMPlexSetOverlap_C", NULL);
2491:   if (--mesh->refct > 0) return 0;
2492:   PetscSectionDestroy(&mesh->coneSection);
2493:   PetscFree(mesh->cones);
2494:   PetscFree(mesh->coneOrientations);
2495:   PetscSectionDestroy(&mesh->supportSection);
2496:   PetscSectionDestroy(&mesh->subdomainSection);
2497:   PetscFree(mesh->supports);
2498:   PetscFree(mesh->facesTmp);
2499:   PetscFree(mesh->tetgenOpts);
2500:   PetscFree(mesh->triangleOpts);
2501:   PetscFree(mesh->transformType);
2502:   PetscFree(mesh->distributionName);
2503:   PetscPartitionerDestroy(&mesh->partitioner);
2504:   DMLabelDestroy(&mesh->subpointMap);
2505:   ISDestroy(&mesh->subpointIS);
2506:   ISDestroy(&mesh->globalVertexNumbers);
2507:   ISDestroy(&mesh->globalCellNumbers);
2508:   PetscSectionDestroy(&mesh->anchorSection);
2509:   ISDestroy(&mesh->anchorIS);
2510:   PetscSectionDestroy(&mesh->parentSection);
2511:   PetscFree(mesh->parents);
2512:   PetscFree(mesh->childIDs);
2513:   PetscSectionDestroy(&mesh->childSection);
2514:   PetscFree(mesh->children);
2515:   DMDestroy(&mesh->referenceTree);
2516:   PetscGridHashDestroy(&mesh->lbox);
2517:   PetscFree(mesh->neighbors);
2518:   if (mesh->metricCtx) PetscFree(mesh->metricCtx);
2519:   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
2520:   PetscFree(mesh);
2521:   return 0;
2522: }

2524: PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
2525: {
2526:   PetscSection           sectionGlobal;
2527:   PetscInt               bs = -1, mbs;
2528:   PetscInt               localSize, localStart = 0;
2529:   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
2530:   MatType                mtype;
2531:   ISLocalToGlobalMapping ltog;

2533:   MatInitializePackage();
2534:   mtype = dm->mattype;
2535:   DMGetGlobalSection(dm, &sectionGlobal);
2536:   /* PetscSectionGetStorageSize(sectionGlobal, &localSize); */
2537:   PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);
2538:   MPI_Exscan(&localSize, &localStart, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)dm));
2539:   MatCreate(PetscObjectComm((PetscObject)dm), J);
2540:   MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);
2541:   MatSetType(*J, mtype);
2542:   MatSetFromOptions(*J);
2543:   MatGetBlockSize(*J, &mbs);
2544:   if (mbs > 1) bs = mbs;
2545:   PetscStrcmp(mtype, MATSHELL, &isShell);
2546:   PetscStrcmp(mtype, MATBAIJ, &isBlock);
2547:   PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);
2548:   PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);
2549:   PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);
2550:   PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);
2551:   PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);
2552:   PetscStrcmp(mtype, MATIS, &isMatIS);
2553:   if (!isShell) {
2554:     PetscBool fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
2555:     PetscInt *dnz, *onz, *dnzu, *onzu, bsLocal[2], bsMinMax[2], *pblocks;
2556:     PetscInt  pStart, pEnd, p, dof, cdof;

2558:     DMGetLocalToGlobalMapping(dm, &ltog);

2560:     PetscCalloc1(localSize, &pblocks);
2561:     PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);
2562:     for (p = pStart; p < pEnd; ++p) {
2563:       PetscInt bdof, offset;

2565:       PetscSectionGetDof(sectionGlobal, p, &dof);
2566:       PetscSectionGetOffset(sectionGlobal, p, &offset);
2567:       PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);
2568:       for (PetscInt i = 0; i < dof - cdof; i++) pblocks[offset - localStart + i] = dof - cdof;
2569:       dof  = dof < 0 ? -(dof + 1) : dof;
2570:       bdof = cdof && (dof - cdof) ? 1 : dof;
2571:       if (dof) {
2572:         if (bs < 0) {
2573:           bs = bdof;
2574:         } else if (bs != bdof) {
2575:           bs = 1;
2576:         }
2577:       }
2578:     }
2579:     /* Must have same blocksize on all procs (some might have no points) */
2580:     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs;
2581:     bsLocal[1] = bs;
2582:     PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax);
2583:     if (bsMinMax[0] != bsMinMax[1]) bs = 1;
2584:     else bs = bsMinMax[0];
2585:     bs = PetscMax(1, bs);
2586:     MatSetLocalToGlobalMapping(*J, ltog, ltog);
2587:     if (dm->prealloc_skip) { // User will likely use MatSetPreallocationCOO(), but still set structural parameters
2588:       MatSetBlockSize(*J, bs);
2589:       MatSetUp(*J);
2590:     } else {
2591:       PetscCalloc4(localSize / bs, &dnz, localSize / bs, &onz, localSize / bs, &dnzu, localSize / bs, &onzu);
2592:       DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);
2593:       PetscFree4(dnz, onz, dnzu, onzu);
2594:     }
2595:     { // Consolidate blocks
2596:       PetscInt nblocks = 0;
2597:       for (PetscInt i = 0; i < localSize; i += PetscMax(1, pblocks[i])) {
2598:         if (pblocks[i] == 0) continue;
2599:         pblocks[nblocks++] = pblocks[i]; // nblocks always <= i
2601:       }
2602:       MatSetVariableBlockSizes(*J, nblocks, pblocks);
2603:     }
2604:     PetscFree(pblocks);
2605:   }
2606:   MatSetDM(*J, dm);
2607:   return 0;
2608: }

2610: /*@
2611:   DMPlexGetSubdomainSection - Returns the section associated with the subdomain

2613:   Not Collective

2615:   Input Parameter:
2616: . mesh - The `DMPLEX`

2618:   Output Parameters:
2619: . subsection - The subdomain section

2621:   Level: developer

2623: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `PetscSection`
2624: @*/
2625: PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
2626: {
2627:   DM_Plex *mesh = (DM_Plex *)dm->data;

2630:   if (!mesh->subdomainSection) {
2631:     PetscSection section;
2632:     PetscSF      sf;

2634:     PetscSFCreate(PETSC_COMM_SELF, &sf);
2635:     DMGetLocalSection(dm, &section);
2636:     PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_TRUE, &mesh->subdomainSection);
2637:     PetscSFDestroy(&sf);
2638:   }
2639:   *subsection = mesh->subdomainSection;
2640:   return 0;
2641: }

2643: /*@
2644:   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)

2646:   Not Collective

2648:   Input Parameter:
2649: . mesh - The `DMPLEX`

2651:   Output Parameters:
2652: + pStart - The first mesh point
2653: - pEnd   - The upper bound for mesh points

2655:   Level: beginner

2657: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSetChart()`
2658: @*/
2659: PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
2660: {
2661:   DM_Plex *mesh = (DM_Plex *)dm->data;

2664:   PetscSectionGetChart(mesh->coneSection, pStart, pEnd);
2665:   return 0;
2666: }

2668: /*@
2669:   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)

2671:   Not Collective

2673:   Input Parameters:
2674: + mesh - The `DMPLEX`
2675: . pStart - The first mesh point
2676: - pEnd   - The upper bound for mesh points

2678:   Level: beginner

2680: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetChart()`
2681: @*/
2682: PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
2683: {
2684:   DM_Plex *mesh = (DM_Plex *)dm->data;

2687:   PetscSectionSetChart(mesh->coneSection, pStart, pEnd);
2688:   PetscSectionSetChart(mesh->supportSection, pStart, pEnd);
2689:   return 0;
2690: }

2692: /*@
2693:   DMPlexGetConeSize - Return the number of in-edges for this point in the DAG

2695:   Not Collective

2697:   Input Parameters:
2698: + mesh - The `DMPLEX`
2699: - p - The point, which must lie in the chart set with `DMPlexSetChart()`

2701:   Output Parameter:
2702: . size - The cone size for point p

2704:   Level: beginner

2706: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSetConeSize()`, `DMPlexSetChart()`
2707: @*/
2708: PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
2709: {
2710:   DM_Plex *mesh = (DM_Plex *)dm->data;

2714:   PetscSectionGetDof(mesh->coneSection, p, size);
2715:   return 0;
2716: }

2718: /*@
2719:   DMPlexSetConeSize - Set the number of in-edges for this point in the DAG

2721:   Not Collective

2723:   Input Parameters:
2724: + mesh - The `DMPLEX`
2725: . p - The point, which must lie in the chart set with `DMPlexSetChart()`
2726: - size - The cone size for point p

2728:   Level: beginner

2730:   Note:
2731:   This should be called after `DMPlexSetChart()`.

2733: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetConeSize()`, `DMPlexSetChart()`
2734: @*/
2735: PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
2736: {
2737:   DM_Plex *mesh = (DM_Plex *)dm->data;

2740:   PetscSectionSetDof(mesh->coneSection, p, size);
2741:   return 0;
2742: }

2744: /*@
2745:   DMPlexAddConeSize - Add the given number of in-edges to this point in the DAG

2747:   Not Collective

2749:   Input Parameters:
2750: + mesh - The `DMPLEX`
2751: . p - The point, which must lie in the chart set with `DMPlexSetChart()`
2752: - size - The additional cone size for point p

2754:   Level: beginner

2756:   Note:
2757:   This should be called after `DMPlexSetChart()`.

2759: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSetConeSize()`, `DMPlexGetConeSize()`, `DMPlexSetChart()`
2760: @*/
2761: PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
2762: {
2763:   DM_Plex *mesh = (DM_Plex *)dm->data;
2765:   PetscSectionAddDof(mesh->coneSection, p, size);
2766:   return 0;
2767: }

2769: /*@C
2770:   DMPlexGetCone - Return the points on the in-edges for this point in the DAG

2772:   Not Collective

2774:   Input Parameters:
2775: + dm - The `DMPLEX`
2776: - p - The point, which must lie in the chart set with `DMPlexSetChart()`

2778:   Output Parameter:
2779: . cone - An array of points which are on the in-edges for point p

2781:   Level: beginner

2783:   Fortran Note:
2784:   You must also call `DMPlexRestoreCone()` after you finish using the returned array.
2785:   `DMPlexRestoreCone()` is not needed/available in C.

2787: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetConeSize()`, `DMPlexSetCone()`, `DMPlexGetConeTuple()`, `DMPlexSetChart()`, `DMPlexRestoreCone()`
2788: @*/
2789: PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
2790: {
2791:   DM_Plex *mesh = (DM_Plex *)dm->data;
2792:   PetscInt off;

2796:   PetscSectionGetOffset(mesh->coneSection, p, &off);
2797:   *cone = &mesh->cones[off];
2798:   return 0;
2799: }

2801: /*@C
2802:   DMPlexGetConeTuple - Return the points on the in-edges of several points in the DAG

2804:   Not Collective

2806:   Input Parameters:
2807: + dm - The `DMPLEX`
2808: - p - The `IS` of points, which must lie in the chart set with `DMPlexSetChart()`

2810:   Output Parameters:
2811: + pConesSection - `PetscSection` describing the layout of pCones
2812: - pCones - An array of points which are on the in-edges for the point set p

2814:   Level: intermediate

2816: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexGetConeRecursive()`, `DMPlexSetChart()`, `PetscSection`, `IS`
2817: @*/
2818: PetscErrorCode DMPlexGetConeTuple(DM dm, IS p, PetscSection *pConesSection, IS *pCones)
2819: {
2820:   PetscSection cs, newcs;
2821:   PetscInt    *cones;
2822:   PetscInt    *newarr = NULL;
2823:   PetscInt     n;

2825:   DMPlexGetCones(dm, &cones);
2826:   DMPlexGetConeSection(dm, &cs);
2827:   PetscSectionExtractDofsFromArray(cs, MPIU_INT, cones, p, &newcs, pCones ? ((void **)&newarr) : NULL);
2828:   if (pConesSection) *pConesSection = newcs;
2829:   if (pCones) {
2830:     PetscSectionGetStorageSize(newcs, &n);
2831:     ISCreateGeneral(PetscObjectComm((PetscObject)p), n, newarr, PETSC_OWN_POINTER, pCones);
2832:   }
2833:   return 0;
2834: }

2836: /*@
2837:   DMPlexGetConeRecursiveVertices - Expand each given point into its cone points and do that recursively until we end up just with vertices.

2839:   Not Collective

2841:   Input Parameters:
2842: + dm - The `DMPLEX`
2843: - points - The `IS` of points, which must lie in the chart set with `DMPlexSetChart()`

2845:   Output Parameter:
2846: . expandedPoints - An array of vertices recursively expanded from input points

2848:   Level: advanced

2850:   Notes:
2851:   Like `DMPlexGetConeRecursive()` but returns only the 0-depth IS (i.e. vertices only) and no sections.

2853:   There is no corresponding Restore function, just call `ISDestroy()` on the returned `IS` to deallocate.

2855: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexGetConeTuple()`, `DMPlexGetConeRecursive()`, `DMPlexRestoreConeRecursive()`,
2856:           `DMPlexGetDepth()`, `IS`
2857: @*/
2858: PetscErrorCode DMPlexGetConeRecursiveVertices(DM dm, IS points, IS *expandedPoints)
2859: {
2860:   IS      *expandedPointsAll;
2861:   PetscInt depth;

2866:   DMPlexGetConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);
2867:   *expandedPoints = expandedPointsAll[0];
2868:   PetscObjectReference((PetscObject)expandedPointsAll[0]);
2869:   DMPlexRestoreConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);
2870:   return 0;
2871: }

2873: /*@
2874:   DMPlexGetConeRecursive - Expand each given point into its cone points and do that recursively until we end up just with vertices (DAG points of depth 0, i.e. without cones).

2876:   Not Collective

2878:   Input Parameters:
2879: + dm - The `DMPLEX`
2880: - points - The `IS` of points, which must lie in the chart set with `DMPlexSetChart()`

2882:   Output Parameters:
2883: + depth - (optional) Size of the output arrays, equal to `DMPLEX` depth, returned by `DMPlexGetDepth()`
2884: . expandedPoints - (optional) An array of index sets with recursively expanded cones
2885: - sections - (optional) An array of sections which describe mappings from points to their cone points

2887:   Level: advanced

2889:   Notes:
2890:   Like `DMPlexGetConeTuple()` but recursive.

2892:   Array expandedPoints has size equal to depth. Each expandedPoints[d] contains DAG points with maximum depth d, recursively cone-wise expanded from the input points.
2893:   For example, for d=0 it contains only vertices, for d=1 it can contain vertices and edges, etc.

2895:   Array section has size equal to depth.  Each `PetscSection` sections[d] realizes mapping from expandedPoints[d+1] (section points) to expandedPoints[d] (section dofs) as follows:
2896:   (1) DAG points in expandedPoints[d+1] with depth d+1 to their cone points in expandedPoints[d];
2897:   (2) DAG points in expandedPoints[d+1] with depth in [0,d] to the same points in expandedPoints[d].

2899: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexGetConeTuple()`, `DMPlexRestoreConeRecursive()`, `DMPlexGetConeRecursiveVertices()`,
2900:           `DMPlexGetDepth()`, `PetscSection`, `IS`
2901: @*/
2902: PetscErrorCode DMPlexGetConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
2903: {
2904:   const PetscInt *arr0 = NULL, *cone = NULL;
2905:   PetscInt       *arr = NULL, *newarr = NULL;
2906:   PetscInt        d, depth_, i, n, newn, cn, co, start, end;
2907:   IS             *expandedPoints_;
2908:   PetscSection   *sections_;

2915:   ISGetLocalSize(points, &n);
2916:   ISGetIndices(points, &arr0);
2917:   DMPlexGetDepth(dm, &depth_);
2918:   PetscCalloc1(depth_, &expandedPoints_);
2919:   PetscCalloc1(depth_, &sections_);
2920:   arr = (PetscInt *)arr0; /* this is ok because first generation of arr is not modified */
2921:   for (d = depth_ - 1; d >= 0; d--) {
2922:     PetscSectionCreate(PETSC_COMM_SELF, &sections_[d]);
2923:     PetscSectionSetChart(sections_[d], 0, n);
2924:     for (i = 0; i < n; i++) {
2925:       DMPlexGetDepthStratum(dm, d + 1, &start, &end);
2926:       if (arr[i] >= start && arr[i] < end) {
2927:         DMPlexGetConeSize(dm, arr[i], &cn);
2928:         PetscSectionSetDof(sections_[d], i, cn);
2929:       } else {
2930:         PetscSectionSetDof(sections_[d], i, 1);
2931:       }
2932:     }
2933:     PetscSectionSetUp(sections_[d]);
2934:     PetscSectionGetStorageSize(sections_[d], &newn);
2935:     PetscMalloc1(newn, &newarr);
2936:     for (i = 0; i < n; i++) {
2937:       PetscSectionGetDof(sections_[d], i, &cn);
2938:       PetscSectionGetOffset(sections_[d], i, &co);
2939:       if (cn > 1) {
2940:         DMPlexGetCone(dm, arr[i], &cone);
2941:         PetscMemcpy(&newarr[co], cone, cn * sizeof(PetscInt));
2942:       } else {
2943:         newarr[co] = arr[i];
2944:       }
2945:     }
2946:     ISCreateGeneral(PETSC_COMM_SELF, newn, newarr, PETSC_OWN_POINTER, &expandedPoints_[d]);
2947:     arr = newarr;
2948:     n   = newn;
2949:   }
2950:   ISRestoreIndices(points, &arr0);
2951:   *depth = depth_;
2952:   if (expandedPoints) *expandedPoints = expandedPoints_;
2953:   else {
2954:     for (d = 0; d < depth_; d++) ISDestroy(&expandedPoints_[d]);
2955:     PetscFree(expandedPoints_);
2956:   }
2957:   if (sections) *sections = sections_;
2958:   else {
2959:     for (d = 0; d < depth_; d++) PetscSectionDestroy(&sections_[d]);
2960:     PetscFree(sections_);
2961:   }
2962:   return 0;
2963: }

2965: /*@
2966:   DMPlexRestoreConeRecursive - Deallocates arrays created by `DMPlexGetConeRecursive()`

2968:   Not Collective

2970:   Input Parameters:
2971: + dm - The `DMPLEX`
2972: - points - The `IS` of points, which must lie in the chart set with `DMPlexSetChart()`

2974:   Output Parameters:
2975: + depth - (optional) Size of the output arrays, equal to `DMPLEX` depth, returned by `DMPlexGetDepth()`
2976: . expandedPoints - (optional) An array of recursively expanded cones
2977: - sections - (optional) An array of sections which describe mappings from points to their cone points

2979:   Level: advanced

2981:   Note:
2982:   See `DMPlexGetConeRecursive()`

2984: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexGetConeTuple()`, `DMPlexGetConeRecursive()`, `DMPlexGetConeRecursiveVertices()`,
2985:           `DMPlexGetDepth()`, `IS`, `PetscSection`
2986: @*/
2987: PetscErrorCode DMPlexRestoreConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
2988: {
2989:   PetscInt d, depth_;

2991:   DMPlexGetDepth(dm, &depth_);
2993:   if (depth) *depth = 0;
2994:   if (expandedPoints) {
2995:     for (d = 0; d < depth_; d++) ISDestroy(&((*expandedPoints)[d]));
2996:     PetscFree(*expandedPoints);
2997:   }
2998:   if (sections) {
2999:     for (d = 0; d < depth_; d++) PetscSectionDestroy(&((*sections)[d]));
3000:     PetscFree(*sections);
3001:   }
3002:   return 0;
3003: }

3005: /*@
3006:   DMPlexSetCone - Set the points on the in-edges for this point in the DAG; that is these are the points that cover the specific point

3008:   Not Collective

3010:   Input Parameters:
3011: + mesh - The `DMPLEX`
3012: . p - The point, which must lie in the chart set with `DMPlexSetChart()`
3013: - cone - An array of points which are on the in-edges for point p

3015:   Level: beginner

3017:   Note:
3018:   This should be called after all calls to `DMPlexSetConeSize()` and `DMSetUp()`.

3020: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexSetChart()`, `DMPlexSetConeSize()`, `DMSetUp()`, `DMPlexSetSupport()`, `DMPlexSetSupportSize()`
3021: @*/
3022: PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
3023: {
3024:   DM_Plex *mesh = (DM_Plex *)dm->data;
3025:   PetscInt pStart, pEnd;
3026:   PetscInt dof, off, c;

3029:   PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);
3030:   PetscSectionGetDof(mesh->coneSection, p, &dof);
3032:   PetscSectionGetOffset(mesh->coneSection, p, &off);
3034:   for (c = 0; c < dof; ++c) {
3036:     mesh->cones[off + c] = cone[c];
3037:   }
3038:   return 0;
3039: }

3041: /*@C
3042:   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the DAG

3044:   Not Collective

3046:   Input Parameters:
3047: + mesh - The `DMPLEX`
3048: - p - The point, which must lie in the chart set with `DMPlexSetChart()`

3050:   Output Parameter:
3051: . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
3052:                     integer giving the prescription for cone traversal.

3054:   Level: beginner

3056:   Note:
3057:   The number indexes the symmetry transformations for the cell type (see manual). Orientation 0 is always
3058:   the identity transformation. Negative orientation indicates reflection so that -(o+1) is the reflection
3059:   of o, however it is not necessarily the inverse. To get the inverse, use `DMPolytopeTypeComposeOrientationInv()`
3060:   with the identity.

3062:   Fortran Note:
3063:   You must also call `DMPlexRestoreConeOrientation()` after you finish using the returned array.
3064:   `DMPlexRestoreConeOrientation()` is not needed/available in C.

3066: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPolytopeTypeComposeOrientation()`, `DMPolytopeTypeComposeOrientationInv()`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexSetCone()`, `DMPlexSetChart()`
3067: @*/
3068: PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
3069: {
3070:   DM_Plex *mesh = (DM_Plex *)dm->data;
3071:   PetscInt off;

3074:   if (PetscDefined(USE_DEBUG)) {
3075:     PetscInt dof;
3076:     PetscSectionGetDof(mesh->coneSection, p, &dof);
3078:   }
3079:   PetscSectionGetOffset(mesh->coneSection, p, &off);

3081:   *coneOrientation = &mesh->coneOrientations[off];
3082:   return 0;
3083: }

3085: /*@
3086:   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the DAG

3088:   Not Collective

3090:   Input Parameters:
3091: + mesh - The `DMPLEX`
3092: . p - The point, which must lie in the chart set with `DMPlexSetChart()`
3093: - coneOrientation - An array of orientations

3095:   Level: beginner

3097:   Notes:
3098:   This should be called after all calls to `DMPlexSetConeSize()` and `DMSetUp()`.

3100:   The meaning of coneOrientation is detailed in `DMPlexGetConeOrientation()`.

3102: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetConeOrientation()`, `DMPlexSetCone()`, `DMPlexSetChart()`, `DMPlexSetConeSize()`, `DMSetUp()`
3103: @*/
3104: PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
3105: {
3106:   DM_Plex *mesh = (DM_Plex *)dm->data;
3107:   PetscInt pStart, pEnd;
3108:   PetscInt dof, off, c;

3111:   PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);
3112:   PetscSectionGetDof(mesh->coneSection, p, &dof);
3114:   PetscSectionGetOffset(mesh->coneSection, p, &off);
3116:   for (c = 0; c < dof; ++c) {
3117:     PetscInt cdof, o = coneOrientation[c];

3119:     PetscSectionGetDof(mesh->coneSection, mesh->cones[off + c], &cdof);
3121:     mesh->coneOrientations[off + c] = o;
3122:   }
3123:   return 0;
3124: }

3126: /*@
3127:   DMPlexInsertCone - Insert a point into the in-edges for the point p in the DAG

3129:   Not Collective

3131:   Input Parameters:
3132: + mesh - The `DMPLEX`
3133: . p - The point, which must lie in the chart set with `DMPlexSetChart()`
3134: . conePos - The local index in the cone where the point should be put
3135: - conePoint - The mesh point to insert

3137:   Level: beginner

3139: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexSetChart()`, `DMPlexSetConeSize()`, `DMSetUp()`
3140: @*/
3141: PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
3142: {
3143:   DM_Plex *mesh = (DM_Plex *)dm->data;
3144:   PetscInt pStart, pEnd;
3145:   PetscInt dof, off;

3148:   PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);
3151:   PetscSectionGetDof(mesh->coneSection, p, &dof);
3152:   PetscSectionGetOffset(mesh->coneSection, p, &off);
3154:   mesh->cones[off + conePos] = conePoint;
3155:   return 0;
3156: }

3158: /*@
3159:   DMPlexInsertConeOrientation - Insert a point orientation for the in-edge for the point p in the DAG

3161:   Not Collective

3163:   Input Parameters:
3164: + mesh - The `DMPLEX`
3165: . p - The point, which must lie in the chart set with `DMPlexSetChart()`
3166: . conePos - The local index in the cone where the point should be put
3167: - coneOrientation - The point orientation to insert

3169:   Level: beginner

3171:   Note:
3172:   The meaning of coneOrientation values is detailed in `DMPlexGetConeOrientation()`.

3174: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexSetChart()`, `DMPlexSetConeSize()`, `DMSetUp()`
3175: @*/
3176: PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
3177: {
3178:   DM_Plex *mesh = (DM_Plex *)dm->data;
3179:   PetscInt pStart, pEnd;
3180:   PetscInt dof, off;

3183:   PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);
3185:   PetscSectionGetDof(mesh->coneSection, p, &dof);
3186:   PetscSectionGetOffset(mesh->coneSection, p, &off);
3188:   mesh->coneOrientations[off + conePos] = coneOrientation;
3189:   return 0;
3190: }

3192: /*@
3193:   DMPlexGetSupportSize - Return the number of out-edges for this point in the DAG

3195:   Not Collective

3197:   Input Parameters:
3198: + mesh - The `DMPLEX`
3199: - p - The point, which must lie in the chart set with `DMPlexSetChart()`

3201:   Output Parameter:
3202: . size - The support size for point p

3204:   Level: beginner

3206: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSetConeSize()`, `DMPlexSetChart()`, `DMPlexGetConeSize()`
3207: @*/
3208: PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
3209: {
3210:   DM_Plex *mesh = (DM_Plex *)dm->data;

3214:   PetscSectionGetDof(mesh->supportSection, p, size);
3215:   return 0;
3216: }

3218: /*@
3219:   DMPlexSetSupportSize - Set the number of out-edges for this point in the DAG

3221:   Not Collective

3223:   Input Parameters:
3224: + mesh - The `DMPLEX`
3225: . p - The point, which must lie in the chart set with `DMPlexSetChart()`
3226: - size - The support size for point p

3228:   Level: beginner

3230:   Note:
3231:   This should be called after DMPlexSetChart().

3233: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetSupportSize()`, `DMPlexSetChart()`
3234: @*/
3235: PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
3236: {
3237:   DM_Plex *mesh = (DM_Plex *)dm->data;

3240:   PetscSectionSetDof(mesh->supportSection, p, size);
3241:   return 0;
3242: }

3244: /*@C
3245:   DMPlexGetSupport - Return the points on the out-edges for this point in the DAG

3247:   Not Collective

3249:   Input Parameters:
3250: + mesh - The `DMPLEX`
3251: - p - The point, which must lie in the chart set with `DMPlexSetChart()`

3253:   Output Parameter:
3254: . support - An array of points which are on the out-edges for point p

3256:   Level: beginner

3258:   Fortran Note:
3259:   You must also call `DMPlexRestoreSupport()` after you finish using the returned array.
3260:   `DMPlexRestoreSupport()` is not needed/available in C.

3262: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetSupportSize()`, `DMPlexSetSupport()`, `DMPlexGetCone()`, `DMPlexSetChart()`
3263: @*/
3264: PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
3265: {
3266:   DM_Plex *mesh = (DM_Plex *)dm->data;
3267:   PetscInt off;

3271:   PetscSectionGetOffset(mesh->supportSection, p, &off);
3272:   *support = &mesh->supports[off];
3273:   return 0;
3274: }

3276: /*@
3277:   DMPlexSetSupport - Set the points on the out-edges for this point in the DAG, that is the list of points that this point covers

3279:   Not Collective

3281:   Input Parameters:
3282: + mesh - The `DMPLEX`
3283: . p - The point, which must lie in the chart set with `DMPlexSetChart()`
3284: - support - An array of points which are on the out-edges for point p

3286:   Level: beginner

3288:   Note:
3289:   This should be called after all calls to `DMPlexSetSupportSize()` and `DMSetUp()`.

3291: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexSetCone()`, `DMPlexSetConeSize()`, `DMPlexCreate()`, `DMPlexGetSupport()`, `DMPlexSetChart()`, `DMPlexSetSupportSize()`, `DMSetUp()`
3292: @*/
3293: PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
3294: {
3295:   DM_Plex *mesh = (DM_Plex *)dm->data;
3296:   PetscInt pStart, pEnd;
3297:   PetscInt dof, off, c;

3300:   PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);
3301:   PetscSectionGetDof(mesh->supportSection, p, &dof);
3303:   PetscSectionGetOffset(mesh->supportSection, p, &off);
3305:   for (c = 0; c < dof; ++c) {
3307:     mesh->supports[off + c] = support[c];
3308:   }
3309:   return 0;
3310: }

3312: /*@
3313:   DMPlexInsertSupport - Insert a point into the out-edges for the point p in the DAG

3315:   Not Collective

3317:   Input Parameters:
3318: + mesh - The `DMPLEX`
3319: . p - The point, which must lie in the chart set with `DMPlexSetChart()`
3320: . supportPos - The local index in the cone where the point should be put
3321: - supportPoint - The mesh point to insert

3323:   Level: beginner

3325: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexSetChart()`, `DMPlexSetConeSize()`, `DMSetUp()`
3326: @*/
3327: PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
3328: {
3329:   DM_Plex *mesh = (DM_Plex *)dm->data;
3330:   PetscInt pStart, pEnd;
3331:   PetscInt dof, off;

3334:   PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);
3335:   PetscSectionGetDof(mesh->supportSection, p, &dof);
3336:   PetscSectionGetOffset(mesh->supportSection, p, &off);
3340:   mesh->supports[off + supportPos] = supportPoint;
3341:   return 0;
3342: }

3344: /* Converts an orientation o in the current numbering to the previous scheme used in Plex */
3345: PetscInt DMPolytopeConvertNewOrientation_Internal(DMPolytopeType ct, PetscInt o)
3346: {
3347:   switch (ct) {
3348:   case DM_POLYTOPE_SEGMENT:
3349:     if (o == -1) return -2;
3350:     break;
3351:   case DM_POLYTOPE_TRIANGLE:
3352:     if (o == -3) return -1;
3353:     if (o == -2) return -3;
3354:     if (o == -1) return -2;
3355:     break;
3356:   case DM_POLYTOPE_QUADRILATERAL:
3357:     if (o == -4) return -2;
3358:     if (o == -3) return -1;
3359:     if (o == -2) return -4;
3360:     if (o == -1) return -3;
3361:     break;
3362:   default:
3363:     return o;
3364:   }
3365:   return o;
3366: }

3368: /* Converts an orientation o in the previous scheme used in Plex to the current numbering */
3369: PetscInt DMPolytopeConvertOldOrientation_Internal(DMPolytopeType ct, PetscInt o)
3370: {
3371:   switch (ct) {
3372:   case DM_POLYTOPE_SEGMENT:
3373:     if ((o == -2) || (o == 1)) return -1;
3374:     if (o == -1) return 0;
3375:     break;
3376:   case DM_POLYTOPE_TRIANGLE:
3377:     if (o == -3) return -2;
3378:     if (o == -2) return -1;
3379:     if (o == -1) return -3;
3380:     break;
3381:   case DM_POLYTOPE_QUADRILATERAL:
3382:     if (o == -4) return -2;
3383:     if (o == -3) return -1;
3384:     if (o == -2) return -4;
3385:     if (o == -1) return -3;
3386:     break;
3387:   default:
3388:     return o;
3389:   }
3390:   return o;
3391: }

3393: /* Takes in a mesh whose orientations are in the previous scheme and converts them all to the current numbering */
3394: PetscErrorCode DMPlexConvertOldOrientations_Internal(DM dm)
3395: {
3396:   PetscInt pStart, pEnd, p;

3398:   DMPlexGetChart(dm, &pStart, &pEnd);
3399:   for (p = pStart; p < pEnd; ++p) {
3400:     const PetscInt *cone, *ornt;
3401:     PetscInt        coneSize, c;

3403:     DMPlexGetConeSize(dm, p, &coneSize);
3404:     DMPlexGetCone(dm, p, &cone);
3405:     DMPlexGetConeOrientation(dm, p, &ornt);
3406:     for (c = 0; c < coneSize; ++c) {
3407:       DMPolytopeType ct;
3408:       const PetscInt o = ornt[c];

3410:       DMPlexGetCellType(dm, cone[c], &ct);
3411:       switch (ct) {
3412:       case DM_POLYTOPE_SEGMENT:
3413:         if ((o == -2) || (o == 1)) DMPlexInsertConeOrientation(dm, p, c, -1);
3414:         if (o == -1) DMPlexInsertConeOrientation(dm, p, c, 0);
3415:         break;
3416:       case DM_POLYTOPE_TRIANGLE:
3417:         if (o == -3) DMPlexInsertConeOrientation(dm, p, c, -2);
3418:         if (o == -2) DMPlexInsertConeOrientation(dm, p, c, -1);
3419:         if (o == -1) DMPlexInsertConeOrientation(dm, p, c, -3);
3420:         break;
3421:       case DM_POLYTOPE_QUADRILATERAL:
3422:         if (o == -4) DMPlexInsertConeOrientation(dm, p, c, -2);
3423:         if (o == -3) DMPlexInsertConeOrientation(dm, p, c, -1);
3424:         if (o == -2) DMPlexInsertConeOrientation(dm, p, c, -4);
3425:         if (o == -1) DMPlexInsertConeOrientation(dm, p, c, -3);
3426:         break;
3427:       default:
3428:         break;
3429:       }
3430:     }
3431:   }
3432:   return 0;
3433: }

3435: static PetscErrorCode DMPlexGetTransitiveClosure_Depth1_Private(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
3436: {
3437:   DMPolytopeType  ct = DM_POLYTOPE_UNKNOWN;
3438:   PetscInt       *closure;
3439:   const PetscInt *tmp = NULL, *tmpO = NULL;
3440:   PetscInt        off = 0, tmpSize, t;

3443:   if (ornt) {
3444:     DMPlexGetCellType(dm, p, &ct);
3445:     if (ct == DM_POLYTOPE_FV_GHOST || ct == DM_POLYTOPE_INTERIOR_GHOST || ct == DM_POLYTOPE_UNKNOWN) ct = DM_POLYTOPE_UNKNOWN;
3446:   }
3447:   if (*points) {
3448:     closure = *points;
3449:   } else {
3450:     PetscInt maxConeSize, maxSupportSize;
3451:     DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);
3452:     DMGetWorkArray(dm, 2 * (PetscMax(maxConeSize, maxSupportSize) + 1), MPIU_INT, &closure);
3453:   }
3454:   if (useCone) {
3455:     DMPlexGetConeSize(dm, p, &tmpSize);
3456:     DMPlexGetCone(dm, p, &tmp);
3457:     DMPlexGetConeOrientation(dm, p, &tmpO);
3458:   } else {
3459:     DMPlexGetSupportSize(dm, p, &tmpSize);
3460:     DMPlexGetSupport(dm, p, &tmp);
3461:   }
3462:   if (ct == DM_POLYTOPE_UNKNOWN) {
3463:     closure[off++] = p;
3464:     closure[off++] = 0;
3465:     for (t = 0; t < tmpSize; ++t) {
3466:       closure[off++] = tmp[t];
3467:       closure[off++] = tmpO ? tmpO[t] : 0;
3468:     }
3469:   } else {
3470:     const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, ornt);

3472:     /* We assume that cells with a valid type have faces with a valid type */
3473:     closure[off++] = p;
3474:     closure[off++] = ornt;
3475:     for (t = 0; t < tmpSize; ++t) {
3476:       DMPolytopeType ft;

3478:       DMPlexGetCellType(dm, tmp[t], &ft);
3479:       closure[off++] = tmp[arr[t]];
3480:       closure[off++] = tmpO ? DMPolytopeTypeComposeOrientation(ft, ornt, tmpO[t]) : 0;
3481:     }
3482:   }
3483:   if (numPoints) *numPoints = tmpSize + 1;
3484:   if (points) *points = closure;
3485:   return 0;
3486: }

3488: /* We need a special tensor version because we want to allow duplicate points in the endcaps for hybrid cells */
3489: static PetscErrorCode DMPlexTransitiveClosure_Tensor_Internal(DM dm, PetscInt point, DMPolytopeType ct, PetscInt o, PetscBool useCone, PetscInt *numPoints, PetscInt **points)
3490: {
3491:   const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, o);
3492:   const PetscInt *cone, *ornt;
3493:   PetscInt       *pts, *closure = NULL;
3494:   DMPolytopeType  ft;
3495:   PetscInt        maxConeSize, maxSupportSize, coneSeries, supportSeries, maxSize;
3496:   PetscInt        dim, coneSize, c, d, clSize, cl;

3499:   DMGetDimension(dm, &dim);
3500:   DMPlexGetConeSize(dm, point, &coneSize);
3501:   DMPlexGetCone(dm, point, &cone);
3502:   DMPlexGetConeOrientation(dm, point, &ornt);
3503:   DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);
3504:   coneSeries    = (maxConeSize > 1) ? ((PetscPowInt(maxConeSize, dim + 1) - 1) / (maxConeSize - 1)) : dim + 1;
3505:   supportSeries = (maxSupportSize > 1) ? ((PetscPowInt(maxSupportSize, dim + 1) - 1) / (maxSupportSize - 1)) : dim + 1;
3506:   maxSize       = PetscMax(coneSeries, supportSeries);
3507:   if (*points) {
3508:     pts = *points;
3509:   } else DMGetWorkArray(dm, 2 * maxSize, MPIU_INT, &pts);
3510:   c        = 0;
3511:   pts[c++] = point;
3512:   pts[c++] = o;
3513:   DMPlexGetCellType(dm, cone[arr[0 * 2 + 0]], &ft);
3514:   DMPlexGetTransitiveClosure_Internal(dm, cone[arr[0 * 2 + 0]], DMPolytopeTypeComposeOrientation(ft, arr[0 * 2 + 1], ornt[0]), useCone, &clSize, &closure);
3515:   for (cl = 0; cl < clSize * 2; cl += 2) {
3516:     pts[c++] = closure[cl];
3517:     pts[c++] = closure[cl + 1];
3518:   }
3519:   DMPlexGetTransitiveClosure_Internal(dm, cone[arr[1 * 2 + 0]], DMPolytopeTypeComposeOrientation(ft, arr[1 * 2 + 1], ornt[1]), useCone, &clSize, &closure);
3520:   for (cl = 0; cl < clSize * 2; cl += 2) {
3521:     pts[c++] = closure[cl];
3522:     pts[c++] = closure[cl + 1];
3523:   }
3524:   DMPlexRestoreTransitiveClosure(dm, cone[0], useCone, &clSize, &closure);
3525:   for (d = 2; d < coneSize; ++d) {
3526:     DMPlexGetCellType(dm, cone[arr[d * 2 + 0]], &ft);
3527:     pts[c++] = cone[arr[d * 2 + 0]];
3528:     pts[c++] = DMPolytopeTypeComposeOrientation(ft, arr[d * 2 + 1], ornt[d]);
3529:   }
3530:   if (dim >= 3) {
3531:     for (d = 2; d < coneSize; ++d) {
3532:       const PetscInt  fpoint = cone[arr[d * 2 + 0]];
3533:       const PetscInt *fcone, *fornt;
3534:       PetscInt        fconeSize, fc, i;

3536:       DMPlexGetCellType(dm, fpoint, &ft);
3537:       const PetscInt *farr = DMPolytopeTypeGetArrangment(ft, DMPolytopeTypeComposeOrientation(ft, arr[d * 2 + 1], ornt[d]));
3538:       DMPlexGetConeSize(dm, fpoint, &fconeSize);
3539:       DMPlexGetCone(dm, fpoint, &fcone);
3540:       DMPlexGetConeOrientation(dm, fpoint, &fornt);
3541:       for (fc = 0; fc < fconeSize; ++fc) {
3542:         const PetscInt cp = fcone[farr[fc * 2 + 0]];
3543:         const PetscInt co = farr[fc * 2 + 1];

3545:         for (i = 0; i < c; i += 2)
3546:           if (pts[i] == cp) break;
3547:         if (i == c) {
3548:           DMPlexGetCellType(dm, cp, &ft);
3549:           pts[c++] = cp;
3550:           pts[c++] = DMPolytopeTypeComposeOrientation(ft, co, fornt[farr[fc * 2 + 0]]);
3551:         }
3552:       }
3553:     }
3554:   }
3555:   *numPoints = c / 2;
3556:   *points    = pts;
3557:   return 0;
3558: }

3560: PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
3561: {
3562:   DMPolytopeType ct;
3563:   PetscInt      *closure, *fifo;
3564:   PetscInt       closureSize = 0, fifoStart = 0, fifoSize = 0;
3565:   PetscInt       maxConeSize, maxSupportSize, coneSeries, supportSeries;
3566:   PetscInt       depth, maxSize;

3569:   DMPlexGetDepth(dm, &depth);
3570:   if (depth == 1) {
3571:     DMPlexGetTransitiveClosure_Depth1_Private(dm, p, ornt, useCone, numPoints, points);
3572:     return 0;
3573:   }
3574:   DMPlexGetCellType(dm, p, &ct);
3575:   if (ct == DM_POLYTOPE_FV_GHOST || ct == DM_POLYTOPE_INTERIOR_GHOST || ct == DM_POLYTOPE_UNKNOWN) ct = DM_POLYTOPE_UNKNOWN;
3576:   if (ct == DM_POLYTOPE_SEG_PRISM_TENSOR || ct == DM_POLYTOPE_TRI_PRISM_TENSOR || ct == DM_POLYTOPE_QUAD_PRISM_TENSOR) {
3577:     DMPlexTransitiveClosure_Tensor_Internal(dm, p, ct, ornt, useCone, numPoints, points);
3578:     return 0;
3579:   }
3580:   DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);
3581:   coneSeries    = (maxConeSize > 1) ? ((PetscPowInt(maxConeSize, depth + 1) - 1) / (maxConeSize - 1)) : depth + 1;
3582:   supportSeries = (maxSupportSize > 1) ? ((PetscPowInt(maxSupportSize, depth + 1) - 1) / (maxSupportSize - 1)) : depth + 1;
3583:   maxSize       = PetscMax(coneSeries, supportSeries);
3584:   DMGetWorkArray(dm, 3 * maxSize, MPIU_INT, &fifo);
3585:   if (*points) {
3586:     closure = *points;
3587:   } else DMGetWorkArray(dm, 2 * maxSize, MPIU_INT, &closure);
3588:   closure[closureSize++] = p;
3589:   closure[closureSize++] = ornt;
3590:   fifo[fifoSize++]       = p;
3591:   fifo[fifoSize++]       = ornt;
3592:   fifo[fifoSize++]       = ct;
3593:   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
3594:   while (fifoSize - fifoStart) {
3595:     const PetscInt       q    = fifo[fifoStart++];
3596:     const PetscInt       o    = fifo[fifoStart++];
3597:     const DMPolytopeType qt   = (DMPolytopeType)fifo[fifoStart++];
3598:     const PetscInt      *qarr = DMPolytopeTypeGetArrangment(qt, o);
3599:     const PetscInt      *tmp, *tmpO;
3600:     PetscInt             tmpSize, t;

3602:     if (PetscDefined(USE_DEBUG)) {
3603:       PetscInt nO = DMPolytopeTypeGetNumArrangments(qt) / 2;
3605:     }
3606:     if (useCone) {
3607:       DMPlexGetConeSize(dm, q, &tmpSize);
3608:       DMPlexGetCone(dm, q, &tmp);
3609:       DMPlexGetConeOrientation(dm, q, &tmpO);
3610:     } else {
3611:       DMPlexGetSupportSize(dm, q, &tmpSize);
3612:       DMPlexGetSupport(dm, q, &tmp);
3613:       tmpO = NULL;
3614:     }
3615:     for (t = 0; t < tmpSize; ++t) {
3616:       const PetscInt ip = useCone && qarr ? qarr[t * 2] : t;
3617:       const PetscInt io = useCone && qarr ? qarr[t * 2 + 1] : 0;
3618:       const PetscInt cp = tmp[ip];
3619:       DMPlexGetCellType(dm, cp, &ct);
3620:       const PetscInt co = tmpO ? DMPolytopeTypeComposeOrientation(ct, io, tmpO[ip]) : 0;
3621:       PetscInt       c;

3623:       /* Check for duplicate */
3624:       for (c = 0; c < closureSize; c += 2) {
3625:         if (closure[c] == cp) break;
3626:       }
3627:       if (c == closureSize) {
3628:         closure[closureSize++] = cp;
3629:         closure[closureSize++] = co;
3630:         fifo[fifoSize++]       = cp;
3631:         fifo[fifoSize++]       = co;
3632:         fifo[fifoSize++]       = ct;
3633:       }
3634:     }
3635:   }
3636:   DMRestoreWorkArray(dm, 3 * maxSize, MPIU_INT, &fifo);
3637:   if (numPoints) *numPoints = closureSize / 2;
3638:   if (points) *points = closure;
3639:   return 0;
3640: }

3642: /*@C
3643:   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the DAG

3645:   Not Collective

3647:   Input Parameters:
3648: + dm      - The `DMPLEX`
3649: . p       - The mesh point
3650: - useCone - `PETSC_TRUE` for the closure, otherwise return the star

3652:   Input/Output Parameter:
3653: . points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...];
3654:            if NULL on input, internal storage will be returned, otherwise the provided array is used

3656:   Output Parameter:
3657: . numPoints - The number of points in the closure, so points[] is of size 2*numPoints

3659:   Level: beginner

3661:   Note:
3662:   If using internal storage (points is NULL on input), each call overwrites the last output.

3664:   Fortran Note:
3665:   The numPoints argument is not present in the Fortran binding since it is internal to the array.

3667: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexRestoreTransitiveClosure()`, `DMPlexCreate()`, `DMPlexSetCone()`, `DMPlexSetChart()`, `DMPlexGetCone()`
3668: @*/
3669: PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
3670: {
3675:   DMPlexGetTransitiveClosure_Internal(dm, p, 0, useCone, numPoints, points);
3676:   return 0;
3677: }

3679: /*@C
3680:   DMPlexRestoreTransitiveClosure - Restore the array of points on the transitive closure of the in-edges or out-edges for this point in the DAG

3682:   Not Collective

3684:   Input Parameters:
3685: + dm        - The `DMPLEX`
3686: . p         - The mesh point
3687: . useCone   - `PETSC_TRUE` for the closure, otherwise return the star
3688: . numPoints - The number of points in the closure, so points[] is of size 2*numPoints
3689: - points    - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]

3691:   Level: beginner

3693:   Note:
3694:   If not using internal storage (points is not NULL on input), this call is unnecessary

3696: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetTransitiveClosure()`, `DMPlexCreate()`, `DMPlexSetCone()`, `DMPlexSetChart()`, `DMPlexGetCone()`
3697: @*/
3698: PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
3699: {
3702:   if (numPoints) *numPoints = 0;
3703:   DMRestoreWorkArray(dm, 0, MPIU_INT, points);
3704:   return 0;
3705: }

3707: /*@
3708:   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the DAG

3710:   Not Collective

3712:   Input Parameter:
3713: . mesh - The `DMPLEX`

3715:   Output Parameters:
3716: + maxConeSize - The maximum number of in-edges
3717: - maxSupportSize - The maximum number of out-edges

3719:   Level: beginner

3721: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSetConeSize()`, `DMPlexSetChart()`
3722: @*/
3723: PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
3724: {
3725:   DM_Plex *mesh = (DM_Plex *)dm->data;

3728:   if (maxConeSize) PetscSectionGetMaxDof(mesh->coneSection, maxConeSize);
3729:   if (maxSupportSize) PetscSectionGetMaxDof(mesh->supportSection, maxSupportSize);
3730:   return 0;
3731: }

3733: PetscErrorCode DMSetUp_Plex(DM dm)
3734: {
3735:   DM_Plex *mesh = (DM_Plex *)dm->data;
3736:   PetscInt size, maxSupportSize;

3739:   PetscSectionSetUp(mesh->coneSection);
3740:   PetscSectionGetStorageSize(mesh->coneSection, &size);
3741:   PetscMalloc1(size, &mesh->cones);
3742:   PetscCalloc1(size, &mesh->coneOrientations);
3743:   PetscSectionGetMaxDof(mesh->supportSection, &maxSupportSize);
3744:   if (maxSupportSize) {
3745:     PetscSectionSetUp(mesh->supportSection);
3746:     PetscSectionGetStorageSize(mesh->supportSection, &size);
3747:     PetscMalloc1(size, &mesh->supports);
3748:   }
3749:   return 0;
3750: }

3752: PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
3753: {
3754:   if (subdm) DMClone(dm, subdm);
3755:   DMCreateSectionSubDM(dm, numFields, fields, is, subdm);
3756:   if (subdm) (*subdm)->useNatural = dm->useNatural;
3757:   if (dm->useNatural && dm->sfMigration) {
3758:     PetscSF sfNatural;

3760:     (*subdm)->sfMigration = dm->sfMigration;
3761:     PetscObjectReference((PetscObject)dm->sfMigration);
3762:     DMPlexCreateGlobalToNaturalSF(*subdm, NULL, (*subdm)->sfMigration, &sfNatural);
3763:     (*subdm)->sfNatural = sfNatural;
3764:   }
3765:   return 0;
3766: }

3768: PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm)
3769: {
3770:   PetscInt i = 0;

3772:   DMClone(dms[0], superdm);
3773:   DMCreateSectionSuperDM(dms, len, is, superdm);
3774:   (*superdm)->useNatural = PETSC_FALSE;
3775:   for (i = 0; i < len; i++) {
3776:     if (dms[i]->useNatural && dms[i]->sfMigration) {
3777:       PetscSF sfNatural;

3779:       (*superdm)->sfMigration = dms[i]->sfMigration;
3780:       PetscObjectReference((PetscObject)dms[i]->sfMigration);
3781:       (*superdm)->useNatural = PETSC_TRUE;
3782:       DMPlexCreateGlobalToNaturalSF(*superdm, NULL, (*superdm)->sfMigration, &sfNatural);
3783:       (*superdm)->sfNatural = sfNatural;
3784:       break;
3785:     }
3786:   }
3787:   return 0;
3788: }

3790: /*@
3791:   DMPlexSymmetrize - Create support (out-edge) information from cone (in-edge) information

3793:   Not Collective

3795:   Input Parameter:
3796: . mesh - The `DMPLEX`

3798:   Level: beginner

3800:   Note:
3801:   This should be called after all calls to `DMPlexSetCone()`

3803: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSetChart()`, `DMPlexSetConeSize()`, `DMPlexSetCone()`
3804: @*/
3805: PetscErrorCode DMPlexSymmetrize(DM dm)
3806: {
3807:   DM_Plex  *mesh = (DM_Plex *)dm->data;
3808:   PetscInt *offsets;
3809:   PetscInt  supportSize;
3810:   PetscInt  pStart, pEnd, p;

3814:   PetscLogEventBegin(DMPLEX_Symmetrize, dm, 0, 0, 0);
3815:   /* Calculate support sizes */
3816:   DMPlexGetChart(dm, &pStart, &pEnd);
3817:   for (p = pStart; p < pEnd; ++p) {
3818:     PetscInt dof, off, c;

3820:     PetscSectionGetDof(mesh->coneSection, p, &dof);
3821:     PetscSectionGetOffset(mesh->coneSection, p, &off);
3822:     for (c = off; c < off + dof; ++c) PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);
3823:   }
3824:   PetscSectionSetUp(mesh->supportSection);
3825:   /* Calculate supports */
3826:   PetscSectionGetStorageSize(mesh->supportSection, &supportSize);
3827:   PetscMalloc1(supportSize, &mesh->supports);
3828:   PetscCalloc1(pEnd - pStart, &offsets);
3829:   for (p = pStart; p < pEnd; ++p) {
3830:     PetscInt dof, off, c;

3832:     PetscSectionGetDof(mesh->coneSection, p, &dof);
3833:     PetscSectionGetOffset(mesh->coneSection, p, &off);
3834:     for (c = off; c < off + dof; ++c) {
3835:       const PetscInt q = mesh->cones[c];
3836:       PetscInt       offS;

3838:       PetscSectionGetOffset(mesh->supportSection, q, &offS);

3840:       mesh->supports[offS + offsets[q]] = p;
3841:       ++offsets[q];
3842:     }
3843:   }
3844:   PetscFree(offsets);
3845:   PetscLogEventEnd(DMPLEX_Symmetrize, dm, 0, 0, 0);
3846:   return 0;
3847: }

3849: static PetscErrorCode DMPlexCreateDepthStratum(DM dm, DMLabel label, PetscInt depth, PetscInt pStart, PetscInt pEnd)
3850: {
3851:   IS stratumIS;

3853:   if (pStart >= pEnd) return 0;
3854:   if (PetscDefined(USE_DEBUG)) {
3855:     PetscInt  qStart, qEnd, numLevels, level;
3856:     PetscBool overlap = PETSC_FALSE;
3857:     DMLabelGetNumValues(label, &numLevels);
3858:     for (level = 0; level < numLevels; level++) {
3859:       DMLabelGetStratumBounds(label, level, &qStart, &qEnd);
3860:       if ((pStart >= qStart && pStart < qEnd) || (pEnd > qStart && pEnd <= qEnd)) {
3861:         overlap = PETSC_TRUE;
3862:         break;
3863:       }
3864:     }
3866:   }
3867:   ISCreateStride(PETSC_COMM_SELF, pEnd - pStart, pStart, 1, &stratumIS);
3868:   DMLabelSetStratumIS(label, depth, stratumIS);
3869:   ISDestroy(&stratumIS);
3870:   return 0;
3871: }

3873: /*@
3874:   DMPlexStratify - The DAG for most topologies is a graded poset (https://en.wikipedia.org/wiki/Graded_poset), and
3875:   can be illustrated by a Hasse Diagram (https://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
3876:   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
3877:   the DAG.

3879:   Collective on dm

3881:   Input Parameter:
3882: . mesh - The `DMPLEX`

3884:   Level: beginner

3886:   Notes:
3887:   Concretely, `DMPlexStratify()` creates a new label named "depth" containing the depth in the DAG of each point. For cell-vertex
3888:   meshes, vertices are depth 0 and cells are depth 1. For fully interpolated meshes, depth 0 for vertices, 1 for edges, and so on
3889:   until cells have depth equal to the dimension of the mesh. The depth label can be accessed through `DMPlexGetDepthLabel()` or `DMPlexGetDepthStratum()`, or
3890:   manually via `DMGetLabel()`.  The height is defined implicitly by height = maxDimension - depth, and can be accessed
3891:   via `DMPlexGetHeightStratum()`.  For example, cells have height 0 and faces have height 1.

3893:   The depth of a point is calculated by executing a breadth-first search (BFS) on the DAG. This could produce surprising results
3894:   if run on a partially interpolated mesh, meaning one that had some edges and faces, but not others. For example, suppose that
3895:   we had a mesh consisting of one triangle (c0) and three vertices (v0, v1, v2), and only one edge is on the boundary so we choose
3896:   to interpolate only that one (e0), so that
3897: .vb
3898:   cone(c0) = {e0, v2}
3899:   cone(e0) = {v0, v1}
3900: .ve
3901:   If `DMPlexStratify()` is run on this mesh, it will give depths
3902: .vb
3903:    depth 0 = {v0, v1, v2}
3904:    depth 1 = {e0, c0}
3905: .ve
3906:   where the triangle has been given depth 1, instead of 2, because it is reachable from vertex v2.

3908:   `DMPlexStratify()` should be called after all calls to `DMPlexSymmetrize()`

3910: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSymmetrize()`, `DMPlexComputeCellTypes()`
3911: @*/
3912: PetscErrorCode DMPlexStratify(DM dm)
3913: {
3914:   DM_Plex *mesh = (DM_Plex *)dm->data;
3915:   DMLabel  label;
3916:   PetscInt pStart, pEnd, p;
3917:   PetscInt numRoots = 0, numLeaves = 0;

3920:   PetscLogEventBegin(DMPLEX_Stratify, dm, 0, 0, 0);

3922:   /* Create depth label */
3923:   DMPlexGetChart(dm, &pStart, &pEnd);
3924:   DMCreateLabel(dm, "depth");
3925:   DMPlexGetDepthLabel(dm, &label);

3927:   {
3928:     /* Initialize roots and count leaves */
3929:     PetscInt sMin = PETSC_MAX_INT;
3930:     PetscInt sMax = PETSC_MIN_INT;
3931:     PetscInt coneSize, supportSize;

3933:     for (p = pStart; p < pEnd; ++p) {
3934:       DMPlexGetConeSize(dm, p, &coneSize);
3935:       DMPlexGetSupportSize(dm, p, &supportSize);
3936:       if (!coneSize && supportSize) {
3937:         sMin = PetscMin(p, sMin);
3938:         sMax = PetscMax(p, sMax);
3939:         ++numRoots;
3940:       } else if (!supportSize && coneSize) {
3941:         ++numLeaves;
3942:       } else if (!supportSize && !coneSize) {
3943:         /* Isolated points */
3944:         sMin = PetscMin(p, sMin);
3945:         sMax = PetscMax(p, sMax);
3946:       }
3947:     }
3948:     DMPlexCreateDepthStratum(dm, label, 0, sMin, sMax + 1);
3949:   }

3951:   if (numRoots + numLeaves == (pEnd - pStart)) {
3952:     PetscInt sMin = PETSC_MAX_INT;
3953:     PetscInt sMax = PETSC_MIN_INT;
3954:     PetscInt coneSize, supportSize;

3956:     for (p = pStart; p < pEnd; ++p) {
3957:       DMPlexGetConeSize(dm, p, &coneSize);
3958:       DMPlexGetSupportSize(dm, p, &supportSize);
3959:       if (!supportSize && coneSize) {
3960:         sMin = PetscMin(p, sMin);
3961:         sMax = PetscMax(p, sMax);
3962:       }
3963:     }
3964:     DMPlexCreateDepthStratum(dm, label, 1, sMin, sMax + 1);
3965:   } else {
3966:     PetscInt level = 0;
3967:     PetscInt qStart, qEnd, q;

3969:     DMLabelGetStratumBounds(label, level, &qStart, &qEnd);
3970:     while (qEnd > qStart) {
3971:       PetscInt sMin = PETSC_MAX_INT;
3972:       PetscInt sMax = PETSC_MIN_INT;

3974:       for (q = qStart; q < qEnd; ++q) {
3975:         const PetscInt *support;
3976:         PetscInt        supportSize, s;

3978:         DMPlexGetSupportSize(dm, q, &supportSize);
3979:         DMPlexGetSupport(dm, q, &support);
3980:         for (s = 0; s < supportSize; ++s) {
3981:           sMin = PetscMin(support[s], sMin);
3982:           sMax = PetscMax(support[s], sMax);
3983:         }
3984:       }
3985:       DMLabelGetNumValues(label, &level);
3986:       DMPlexCreateDepthStratum(dm, label, level, sMin, sMax + 1);
3987:       DMLabelGetStratumBounds(label, level, &qStart, &qEnd);
3988:     }
3989:   }
3990:   { /* just in case there is an empty process */
3991:     PetscInt numValues, maxValues = 0, v;

3993:     DMLabelGetNumValues(label, &numValues);
3994:     MPI_Allreduce(&numValues, &maxValues, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));
3995:     for (v = numValues; v < maxValues; v++) DMLabelAddStratum(label, v);
3996:   }
3997:   PetscObjectStateGet((PetscObject)label, &mesh->depthState);
3998:   PetscLogEventEnd(DMPLEX_Stratify, dm, 0, 0, 0);
3999:   return 0;
4000: }

4002: PetscErrorCode DMPlexComputeCellType_Internal(DM dm, PetscInt p, PetscInt pdepth, DMPolytopeType *pt)
4003: {
4004:   DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
4005:   PetscInt       dim, depth, pheight, coneSize;

4008:   DMGetDimension(dm, &dim);
4009:   DMPlexGetDepth(dm, &depth);
4010:   DMPlexGetConeSize(dm, p, &coneSize);
4011:   pheight = depth - pdepth;
4012:   if (depth <= 1) {
4013:     switch (pdepth) {
4014:     case 0:
4015:       ct = DM_POLYTOPE_POINT;
4016:       break;
4017:     case 1:
4018:       switch (coneSize) {
4019:       case 2:
4020:         ct = DM_POLYTOPE_SEGMENT;
4021:         break;
4022:       case 3:
4023:         ct = DM_POLYTOPE_TRIANGLE;
4024:         break;
4025:       case 4:
4026:         switch (dim) {
4027:         case 2:
4028:           ct = DM_POLYTOPE_QUADRILATERAL;
4029:           break;
4030:         case 3:
4031:           ct = DM_POLYTOPE_TETRAHEDRON;
4032:           break;
4033:         default:
4034:           break;
4035:         }
4036:         break;
4037:       case 5:
4038:         ct = DM_POLYTOPE_PYRAMID;
4039:         break;
4040:       case 6:
4041:         ct = DM_POLYTOPE_TRI_PRISM_TENSOR;
4042:         break;
4043:       case 8:
4044:         ct = DM_POLYTOPE_HEXAHEDRON;
4045:         break;
4046:       default:
4047:         break;
4048:       }
4049:     }
4050:   } else {
4051:     if (pdepth == 0) {
4052:       ct = DM_POLYTOPE_POINT;
4053:     } else if (pheight == 0) {
4054:       switch (dim) {
4055:       case 1:
4056:         switch (coneSize) {
4057:         case 2:
4058:           ct = DM_POLYTOPE_SEGMENT;
4059:           break;
4060:         default:
4061:           break;
4062:         }
4063:         break;
4064:       case 2:
4065:         switch (coneSize) {
4066:         case 3:
4067:           ct = DM_POLYTOPE_TRIANGLE;
4068:           break;
4069:         case 4:
4070:           ct = DM_POLYTOPE_QUADRILATERAL;
4071:           break;
4072:         default:
4073:           break;
4074:         }
4075:         break;
4076:       case 3:
4077:         switch (coneSize) {
4078:         case 4:
4079:           ct = DM_POLYTOPE_TETRAHEDRON;
4080:           break;
4081:         case 5: {
4082:           const PetscInt *cone;
4083:           PetscInt        faceConeSize;

4085:           DMPlexGetCone(dm, p, &cone);
4086:           DMPlexGetConeSize(dm, cone[0], &faceConeSize);
4087:           switch (faceConeSize) {
4088:           case 3:
4089:             ct = DM_POLYTOPE_TRI_PRISM_TENSOR;
4090:             break;
4091:           case 4:
4092:             ct = DM_POLYTOPE_PYRAMID;
4093:             break;
4094:           }
4095:         } break;
4096:         case 6:
4097:           ct = DM_POLYTOPE_HEXAHEDRON;
4098:           break;
4099:         default:
4100:           break;
4101:         }
4102:         break;
4103:       default:
4104:         break;
4105:       }
4106:     } else if (pheight > 0) {
4107:       switch (coneSize) {
4108:       case 2:
4109:         ct = DM_POLYTOPE_SEGMENT;
4110:         break;
4111:       case 3:
4112:         ct = DM_POLYTOPE_TRIANGLE;
4113:         break;
4114:       case 4:
4115:         ct = DM_POLYTOPE_QUADRILATERAL;
4116:         break;
4117:       default:
4118:         break;
4119:       }
4120:     }
4121:   }
4122:   *pt = ct;
4123:   return 0;
4124: }

4126: /*@
4127:   DMPlexComputeCellTypes - Infer the polytope type of every cell using its dimension and cone size.

4129:   Collective on dm

4131:   Input Parameter:
4132: . mesh - The `DMPLEX`

4134:   Level: developer

4136:   Note:
4137:   This function is normally called automatically when a cell type is requested. It creates an
4138:   internal `DMLabel` named "celltype" which can be directly accessed using `DMGetLabel()`. A user may disable
4139:   automatic creation by creating the label manually, using `DMCreateLabel`(dm, "celltype").

4141:   `DMPlexComputeCellTypes()` should be called after all calls to `DMPlexSymmetrize()` and `DMPlexStratify()`

4143: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSymmetrize()`, `DMPlexStratify()`, `DMGetLabel()`, `DMCreateLabel()`
4144: @*/
4145: PetscErrorCode DMPlexComputeCellTypes(DM dm)
4146: {
4147:   DM_Plex *mesh;
4148:   DMLabel  ctLabel;
4149:   PetscInt pStart, pEnd, p;

4152:   mesh = (DM_Plex *)dm->data;
4153:   DMCreateLabel(dm, "celltype");
4154:   DMPlexGetCellTypeLabel(dm, &ctLabel);
4155:   DMPlexGetChart(dm, &pStart, &pEnd);
4156:   for (p = pStart; p < pEnd; ++p) {
4157:     DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
4158:     PetscInt       pdepth;

4160:     DMPlexGetPointDepth(dm, p, &pdepth);
4161:     DMPlexComputeCellType_Internal(dm, p, pdepth, &ct);
4163:     DMLabelSetValue(ctLabel, p, ct);
4164:   }
4165:   PetscObjectStateGet((PetscObject)ctLabel, &mesh->celltypeState);
4166:   PetscObjectViewFromOptions((PetscObject)ctLabel, NULL, "-dm_plex_celltypes_view");
4167:   return 0;
4168: }

4170: /*@C
4171:   DMPlexGetJoin - Get an array for the join of the set of points

4173:   Not Collective

4175:   Input Parameters:
4176: + dm - The `DMPLEX` object
4177: . numPoints - The number of input points for the join
4178: - points - The input points

4180:   Output Parameters:
4181: + numCoveredPoints - The number of points in the join
4182: - coveredPoints - The points in the join

4184:   Level: intermediate

4186:   Note:
4187:   Currently, this is restricted to a single level join

4189:   Fortran Note:
4190:   The numCoveredPoints argument is not present in the Fortran binding since it is internal to the array.

4192: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexRestoreJoin()`, `DMPlexGetMeet()`
4193: @*/
4194: PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
4195: {
4196:   DM_Plex  *mesh = (DM_Plex *)dm->data;
4197:   PetscInt *join[2];
4198:   PetscInt  joinSize, i = 0;
4199:   PetscInt  dof, off, p, c, m;
4200:   PetscInt  maxSupportSize;

4206:   PetscSectionGetMaxDof(mesh->supportSection, &maxSupportSize);
4207:   DMGetWorkArray(dm, maxSupportSize, MPIU_INT, &join[0]);
4208:   DMGetWorkArray(dm, maxSupportSize, MPIU_INT, &join[1]);
4209:   /* Copy in support of first point */
4210:   PetscSectionGetDof(mesh->supportSection, points[0], &dof);
4211:   PetscSectionGetOffset(mesh->supportSection, points[0], &off);
4212:   for (joinSize = 0; joinSize < dof; ++joinSize) join[i][joinSize] = mesh->supports[off + joinSize];
4213:   /* Check each successive support */
4214:   for (p = 1; p < numPoints; ++p) {
4215:     PetscInt newJoinSize = 0;

4217:     PetscSectionGetDof(mesh->supportSection, points[p], &dof);
4218:     PetscSectionGetOffset(mesh->supportSection, points[p], &off);
4219:     for (c = 0; c < dof; ++c) {
4220:       const PetscInt point = mesh->supports[off + c];

4222:       for (m = 0; m < joinSize; ++m) {
4223:         if (point == join[i][m]) {
4224:           join[1 - i][newJoinSize++] = point;
4225:           break;
4226:         }
4227:       }
4228:     }
4229:     joinSize = newJoinSize;
4230:     i        = 1 - i;
4231:   }
4232:   *numCoveredPoints = joinSize;
4233:   *coveredPoints    = join[i];
4234:   DMRestoreWorkArray(dm, maxSupportSize, MPIU_INT, &join[1 - i]);
4235:   return 0;
4236: }

4238: /*@C
4239:   DMPlexRestoreJoin - Restore an array for the join of the set of points

4241:   Not Collective

4243:   Input Parameters:
4244: + dm - The `DMPLEX` object
4245: . numPoints - The number of input points for the join
4246: - points - The input points

4248:   Output Parameters:
4249: + numCoveredPoints - The number of points in the join
4250: - coveredPoints - The points in the join

4252:   Level: intermediate

4254:   Fortran Note:
4255:   The numCoveredPoints argument is not present in the Fortran binding since it is internal to the array.

4257: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetJoin()`, `DMPlexGetFullJoin()`, `DMPlexGetMeet()`
4258: @*/
4259: PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
4260: {
4265:   DMRestoreWorkArray(dm, 0, MPIU_INT, (void *)coveredPoints);
4266:   if (numCoveredPoints) *numCoveredPoints = 0;
4267:   return 0;
4268: }

4270: /*@C
4271:   DMPlexGetFullJoin - Get an array for the join of the set of points

4273:   Not Collective

4275:   Input Parameters:
4276: + dm - The `DMPLEX` object
4277: . numPoints - The number of input points for the join
4278: - points - The input points

4280:   Output Parameters:
4281: + numCoveredPoints - The number of points in the join
4282: - coveredPoints - The points in the join

4284:   Level: intermediate

4286:   Fortran Note:
4287:   The numCoveredPoints argument is not present in the Fortran binding since it is internal to the array.

4289: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetJoin()`, `DMPlexRestoreJoin()`, `DMPlexGetMeet()`
4290: @*/
4291: PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
4292: {
4293:   PetscInt *offsets, **closures;
4294:   PetscInt *join[2];
4295:   PetscInt  depth = 0, maxSize, joinSize = 0, i = 0;
4296:   PetscInt  p, d, c, m, ms;


4303:   DMPlexGetDepth(dm, &depth);
4304:   PetscCalloc1(numPoints, &closures);
4305:   DMGetWorkArray(dm, numPoints * (depth + 2), MPIU_INT, &offsets);
4306:   DMPlexGetMaxSizes(dm, NULL, &ms);
4307:   maxSize = (ms > 1) ? ((PetscPowInt(ms, depth + 1) - 1) / (ms - 1)) : depth + 1;
4308:   DMGetWorkArray(dm, maxSize, MPIU_INT, &join[0]);
4309:   DMGetWorkArray(dm, maxSize, MPIU_INT, &join[1]);

4311:   for (p = 0; p < numPoints; ++p) {
4312:     PetscInt closureSize;

4314:     DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);

4316:     offsets[p * (depth + 2) + 0] = 0;
4317:     for (d = 0; d < depth + 1; ++d) {
4318:       PetscInt pStart, pEnd, i;

4320:       DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);
4321:       for (i = offsets[p * (depth + 2) + d]; i < closureSize; ++i) {
4322:         if ((pStart > closures[p][i * 2]) || (pEnd <= closures[p][i * 2])) {
4323:           offsets[p * (depth + 2) + d + 1] = i;
4324:           break;
4325:         }
4326:       }
4327:       if (i == closureSize) offsets[p * (depth + 2) + d + 1] = i;
4328:     }
4330:   }
4331:   for (d = 0; d < depth + 1; ++d) {
4332:     PetscInt dof;

4334:     /* Copy in support of first point */
4335:     dof = offsets[d + 1] - offsets[d];
4336:     for (joinSize = 0; joinSize < dof; ++joinSize) join[i][joinSize] = closures[0][(offsets[d] + joinSize) * 2];
4337:     /* Check each successive cone */
4338:     for (p = 1; p < numPoints && joinSize; ++p) {
4339:       PetscInt newJoinSize = 0;

4341:       dof = offsets[p * (depth + 2) + d + 1] - offsets[p * (depth + 2) + d];
4342:       for (c = 0; c < dof; ++c) {
4343:         const PetscInt point = closures[p][(offsets[p * (depth + 2) + d] + c) * 2];

4345:         for (m = 0; m < joinSize; ++m) {
4346:           if (point == join[i][m]) {
4347:             join[1 - i][newJoinSize++] = point;
4348:             break;
4349:           }
4350:         }
4351:       }
4352:       joinSize = newJoinSize;
4353:       i        = 1 - i;
4354:     }
4355:     if (joinSize) break;
4356:   }
4357:   *numCoveredPoints = joinSize;
4358:   *coveredPoints    = join[i];
4359:   for (p = 0; p < numPoints; ++p) DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);
4360:   PetscFree(closures);
4361:   DMRestoreWorkArray(dm, numPoints * (depth + 2), MPIU_INT, &offsets);
4362:   DMRestoreWorkArray(dm, ms, MPIU_INT, &join[1 - i]);
4363:   return 0;
4364: }

4366: /*@C
4367:   DMPlexGetMeet - Get an array for the meet of the set of points

4369:   Not Collective

4371:   Input Parameters:
4372: + dm - The `DMPLEX` object
4373: . numPoints - The number of input points for the meet
4374: - points - The input points

4376:   Output Parameters:
4377: + numCoveredPoints - The number of points in the meet
4378: - coveredPoints - The points in the meet

4380:   Level: intermediate

4382:   Note:
4383:   Currently, this is restricted to a single level meet

4385:   Fortran Notes:
4386:   The numCoveredPoints argument is not present in the Fortran binding since it is internal to the array.

4388: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexRestoreMeet()`, `DMPlexGetJoin()`
4389: @*/
4390: PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
4391: {
4392:   DM_Plex  *mesh = (DM_Plex *)dm->data;
4393:   PetscInt *meet[2];
4394:   PetscInt  meetSize, i = 0;
4395:   PetscInt  dof, off, p, c, m;
4396:   PetscInt  maxConeSize;

4402:   PetscSectionGetMaxDof(mesh->coneSection, &maxConeSize);
4403:   DMGetWorkArray(dm, maxConeSize, MPIU_INT, &meet[0]);
4404:   DMGetWorkArray(dm, maxConeSize, MPIU_INT, &meet[1]);
4405:   /* Copy in cone of first point */
4406:   PetscSectionGetDof(mesh->coneSection, points[0], &dof);
4407:   PetscSectionGetOffset(mesh->coneSection, points[0], &off);
4408:   for (meetSize = 0; meetSize < dof; ++meetSize) meet[i][meetSize] = mesh->cones[off + meetSize];
4409:   /* Check each successive cone */
4410:   for (p = 1; p < numPoints; ++p) {
4411:     PetscInt newMeetSize = 0;

4413:     PetscSectionGetDof(mesh->coneSection, points[p], &dof);
4414:     PetscSectionGetOffset(mesh->coneSection, points[p], &off);
4415:     for (c = 0; c < dof; ++c) {
4416:       const PetscInt point = mesh->cones[off + c];

4418:       for (m = 0; m < meetSize; ++m) {
4419:         if (point == meet[i][m]) {
4420:           meet[1 - i][newMeetSize++] = point;
4421:           break;
4422:         }
4423:       }
4424:     }
4425:     meetSize = newMeetSize;
4426:     i        = 1 - i;
4427:   }
4428:   *numCoveringPoints = meetSize;
4429:   *coveringPoints    = meet[i];
4430:   DMRestoreWorkArray(dm, maxConeSize, MPIU_INT, &meet[1 - i]);
4431:   return 0;
4432: }

4434: /*@C
4435:   DMPlexRestoreMeet - Restore an array for the meet of the set of points

4437:   Not Collective

4439:   Input Parameters:
4440: + dm - The `DMPLEX` object
4441: . numPoints - The number of input points for the meet
4442: - points - The input points

4444:   Output Parameters:
4445: + numCoveredPoints - The number of points in the meet
4446: - coveredPoints - The points in the meet

4448:   Level: intermediate

4450:   Fortran Note:
4451:   The numCoveredPoints argument is not present in the Fortran binding since it is internal to the array.

4453: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetMeet()`, `DMPlexGetFullMeet()`, `DMPlexGetJoin()`
4454: @*/
4455: PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
4456: {
4461:   DMRestoreWorkArray(dm, 0, MPIU_INT, (void *)coveredPoints);
4462:   if (numCoveredPoints) *numCoveredPoints = 0;
4463:   return 0;
4464: }

4466: /*@C
4467:   DMPlexGetFullMeet - Get an array for the meet of the set of points

4469:   Not Collective

4471:   Input Parameters:
4472: + dm - The `DMPLEX` object
4473: . numPoints - The number of input points for the meet
4474: - points - The input points

4476:   Output Parameters:
4477: + numCoveredPoints - The number of points in the meet
4478: - coveredPoints - The points in the meet

4480:   Level: intermediate

4482:   Fortran Note:
4483:   The numCoveredPoints argument is not present in the Fortran binding since it is internal to the array.

4485: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetMeet()`, `DMPlexRestoreMeet()`, `DMPlexGetJoin()`
4486: @*/
4487: PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
4488: {
4489:   PetscInt *offsets, **closures;
4490:   PetscInt *meet[2];
4491:   PetscInt  height = 0, maxSize, meetSize = 0, i = 0;
4492:   PetscInt  p, h, c, m, mc;


4499:   DMPlexGetDepth(dm, &height);
4500:   PetscMalloc1(numPoints, &closures);
4501:   DMGetWorkArray(dm, numPoints * (height + 2), MPIU_INT, &offsets);
4502:   DMPlexGetMaxSizes(dm, &mc, NULL);
4503:   maxSize = (mc > 1) ? ((PetscPowInt(mc, height + 1) - 1) / (mc - 1)) : height + 1;
4504:   DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[0]);
4505:   DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[1]);

4507:   for (p = 0; p < numPoints; ++p) {
4508:     PetscInt closureSize;

4510:     DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);

4512:     offsets[p * (height + 2) + 0] = 0;
4513:     for (h = 0; h < height + 1; ++h) {
4514:       PetscInt pStart, pEnd, i;

4516:       DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);
4517:       for (i = offsets[p * (height + 2) + h]; i < closureSize; ++i) {
4518:         if ((pStart > closures[p][i * 2]) || (pEnd <= closures[p][i * 2])) {
4519:           offsets[p * (height + 2) + h + 1] = i;
4520:           break;
4521:         }
4522:       }
4523:       if (i == closureSize) offsets[p * (height + 2) + h + 1] = i;
4524:     }
4526:   }
4527:   for (h = 0; h < height + 1; ++h) {
4528:     PetscInt dof;

4530:     /* Copy in cone of first point */
4531:     dof = offsets[h + 1] - offsets[h];
4532:     for (meetSize = 0; meetSize < dof; ++meetSize) meet[i][meetSize] = closures[0][(offsets[h] + meetSize) * 2];
4533:     /* Check each successive cone */
4534:     for (p = 1; p < numPoints && meetSize; ++p) {
4535:       PetscInt newMeetSize = 0;

4537:       dof = offsets[p * (height + 2) + h + 1] - offsets[p * (height + 2) + h];
4538:       for (c = 0; c < dof; ++c) {
4539:         const PetscInt point = closures[p][(offsets[p * (height + 2) + h] + c) * 2];

4541:         for (m = 0; m < meetSize; ++m) {
4542:           if (point == meet[i][m]) {
4543:             meet[1 - i][newMeetSize++] = point;
4544:             break;
4545:           }
4546:         }
4547:       }
4548:       meetSize = newMeetSize;
4549:       i        = 1 - i;
4550:     }
4551:     if (meetSize) break;
4552:   }
4553:   *numCoveredPoints = meetSize;
4554:   *coveredPoints    = meet[i];
4555:   for (p = 0; p < numPoints; ++p) DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);
4556:   PetscFree(closures);
4557:   DMRestoreWorkArray(dm, numPoints * (height + 2), MPIU_INT, &offsets);
4558:   DMRestoreWorkArray(dm, mc, MPIU_INT, &meet[1 - i]);
4559:   return 0;
4560: }

4562: /*@C
4563:   DMPlexEqual - Determine if two `DM` have the same topology

4565:   Not Collective

4567:   Input Parameters:
4568: + dmA - A `DMPLEX` object
4569: - dmB - A `DMPLEX` object

4571:   Output Parameters:
4572: . equal - `PETSC_TRUE` if the topologies are identical

4574:   Level: intermediate

4576:   Note:
4577:   We are not solving graph isomorphism, so we do not permute.

4579: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetCone()`
4580: @*/
4581: PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
4582: {
4583:   PetscInt depth, depthB, pStart, pEnd, pStartB, pEndB, p;


4589:   *equal = PETSC_FALSE;
4590:   DMPlexGetDepth(dmA, &depth);
4591:   DMPlexGetDepth(dmB, &depthB);
4592:   if (depth != depthB) return 0;
4593:   DMPlexGetChart(dmA, &pStart, &pEnd);
4594:   DMPlexGetChart(dmB, &pStartB, &pEndB);
4595:   if ((pStart != pStartB) || (pEnd != pEndB)) return 0;
4596:   for (p = pStart; p < pEnd; ++p) {
4597:     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
4598:     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;

4600:     DMPlexGetConeSize(dmA, p, &coneSize);
4601:     DMPlexGetCone(dmA, p, &cone);
4602:     DMPlexGetConeOrientation(dmA, p, &ornt);
4603:     DMPlexGetConeSize(dmB, p, &coneSizeB);
4604:     DMPlexGetCone(dmB, p, &coneB);
4605:     DMPlexGetConeOrientation(dmB, p, &orntB);
4606:     if (coneSize != coneSizeB) return 0;
4607:     for (c = 0; c < coneSize; ++c) {
4608:       if (cone[c] != coneB[c]) return 0;
4609:       if (ornt[c] != orntB[c]) return 0;
4610:     }
4611:     DMPlexGetSupportSize(dmA, p, &supportSize);
4612:     DMPlexGetSupport(dmA, p, &support);
4613:     DMPlexGetSupportSize(dmB, p, &supportSizeB);
4614:     DMPlexGetSupport(dmB, p, &supportB);
4615:     if (supportSize != supportSizeB) return 0;
4616:     for (s = 0; s < supportSize; ++s) {
4617:       if (support[s] != supportB[s]) return 0;
4618:     }
4619:   }
4620:   *equal = PETSC_TRUE;
4621:   return 0;
4622: }

4624: /*@C
4625:   DMPlexGetNumFaceVertices - Returns the number of vertices on a face

4627:   Not Collective

4629:   Input Parameters:
4630: + dm         - The `DMPLEX`
4631: . cellDim    - The cell dimension
4632: - numCorners - The number of vertices on a cell

4634:   Output Parameters:
4635: . numFaceVertices - The number of vertices on a face

4637:   Level: developer

4639:   Note:
4640:   Of course this can only work for a restricted set of symmetric shapes

4642: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetCone()`
4643: @*/
4644: PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
4645: {
4646:   MPI_Comm comm;

4648:   PetscObjectGetComm((PetscObject)dm, &comm);
4650:   switch (cellDim) {
4651:   case 0:
4652:     *numFaceVertices = 0;
4653:     break;
4654:   case 1:
4655:     *numFaceVertices = 1;
4656:     break;
4657:   case 2:
4658:     switch (numCorners) {
4659:     case 3:                 /* triangle */
4660:       *numFaceVertices = 2; /* Edge has 2 vertices */
4661:       break;
4662:     case 4:                 /* quadrilateral */
4663:       *numFaceVertices = 2; /* Edge has 2 vertices */
4664:       break;
4665:     case 6:                 /* quadratic triangle, tri and quad cohesive Lagrange cells */
4666:       *numFaceVertices = 3; /* Edge has 3 vertices */
4667:       break;
4668:     case 9:                 /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
4669:       *numFaceVertices = 3; /* Edge has 3 vertices */
4670:       break;
4671:     default:
4672:       SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %" PetscInt_FMT " for dimension %" PetscInt_FMT, numCorners, cellDim);
4673:     }
4674:     break;
4675:   case 3:
4676:     switch (numCorners) {
4677:     case 4:                 /* tetradehdron */
4678:       *numFaceVertices = 3; /* Face has 3 vertices */
4679:       break;
4680:     case 6:                 /* tet cohesive cells */
4681:       *numFaceVertices = 4; /* Face has 4 vertices */
4682:       break;
4683:     case 8:                 /* hexahedron */
4684:       *numFaceVertices = 4; /* Face has 4 vertices */
4685:       break;
4686:     case 9:                 /* tet cohesive Lagrange cells */
4687:       *numFaceVertices = 6; /* Face has 6 vertices */
4688:       break;
4689:     case 10:                /* quadratic tetrahedron */
4690:       *numFaceVertices = 6; /* Face has 6 vertices */
4691:       break;
4692:     case 12:                /* hex cohesive Lagrange cells */
4693:       *numFaceVertices = 6; /* Face has 6 vertices */
4694:       break;
4695:     case 18:                /* quadratic tet cohesive Lagrange cells */
4696:       *numFaceVertices = 6; /* Face has 6 vertices */
4697:       break;
4698:     case 27:                /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
4699:       *numFaceVertices = 9; /* Face has 9 vertices */
4700:       break;
4701:     default:
4702:       SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %" PetscInt_FMT " for dimension %" PetscInt_FMT, numCorners, cellDim);
4703:     }
4704:     break;
4705:   default:
4706:     SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %" PetscInt_FMT, cellDim);
4707:   }
4708:   return 0;
4709: }

4711: /*@
4712:   DMPlexGetDepthLabel - Get the `DMLabel` recording the depth of each point

4714:   Not Collective

4716:   Input Parameter:
4717: . dm    - The `DMPLEX` object

4719:   Output Parameter:
4720: . depthLabel - The `DMLabel` recording point depth

4722:   Level: developer

4724: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetDepth()`, `DMPlexGetHeightStratum()`, `DMPlexGetDepthStratum()`, `DMPlexGetPointDepth()`,
4725: @*/
4726: PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
4727: {
4730:   *depthLabel = dm->depthLabel;
4731:   return 0;
4732: }

4734: /*@
4735:   DMPlexGetDepth - Get the depth of the DAG representing this mesh

4737:   Not Collective

4739:   Input Parameter:
4740: . dm    - The `DMPLEX` object

4742:   Output Parameter:
4743: . depth - The number of strata (breadth first levels) in the DAG

4745:   Level: developer

4747:   Notes:
4748:   This returns maximum of point depths over all points, i.e. maximum value of the label returned by `DMPlexGetDepthLabel()`.

4750:   The point depth is described more in detail in `DMPlexGetDepthStratum()`.

4752:   An empty mesh gives -1.

4754: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetDepthLabel()`, `DMPlexGetDepthStratum()`, `DMPlexGetPointDepth()`, `DMPlexSymmetrize()`
4755: @*/
4756: PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
4757: {
4758:   DMLabel  label;
4759:   PetscInt d = 0;

4763:   DMPlexGetDepthLabel(dm, &label);
4764:   if (label) DMLabelGetNumValues(label, &d);
4765:   *depth = d - 1;
4766:   return 0;
4767: }

4769: /*@
4770:   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.

4772:   Not Collective

4774:   Input Parameters:
4775: + dm    - The `DMPLEX` object
4776: - depth - The requested depth

4778:   Output Parameters:
4779: + start - The first point at this depth
4780: - end   - One beyond the last point at this depth

4782:   Level: developer

4784:   Notes:
4785:   Depth indexing is related to topological dimension.  Depth stratum 0 contains the lowest topological dimension points,
4786:   often "vertices".  If the mesh is "interpolated" (see `DMPlexInterpolate()`), then depth stratum 1 contains the next
4787:   higher dimension, e.g., "edges".

4789: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetHeightStratum()`, `DMPlexGetDepth()`, `DMPlexGetDepthLabel()`, `DMPlexGetPointDepth()`, `DMPlexSymmetrize()`, `DMPlexInterpolate()`
4790: @*/
4791: PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt depth, PetscInt *start, PetscInt *end)
4792: {
4793:   DMLabel  label;
4794:   PetscInt pStart, pEnd;

4797:   if (start) {
4799:     *start = 0;
4800:   }
4801:   if (end) {
4803:     *end = 0;
4804:   }
4805:   DMPlexGetChart(dm, &pStart, &pEnd);
4806:   if (pStart == pEnd) return 0;
4807:   if (depth < 0) {
4808:     if (start) *start = pStart;
4809:     if (end) *end = pEnd;
4810:     return 0;
4811:   }
4812:   DMPlexGetDepthLabel(dm, &label);
4814:   DMLabelGetStratumBounds(label, depth, start, end);
4815:   return 0;
4816: }

4818: /*@
4819:   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.

4821:   Not Collective

4823:   Input Parameters:
4824: + dm     - The `DMPLEX` object
4825: - height - The requested height

4827:   Output Parameters:
4828: + start - The first point at this height
4829: - end   - One beyond the last point at this height

4831:   Level: developer

4833:   Notes:
4834:   Height indexing is related to topological codimension.  Height stratum 0 contains the highest topological dimension
4835:   points, often called "cells" or "elements".  If the mesh is "interpolated" (see `DMPlexInterpolate()`), then height
4836:   stratum 1 contains the boundary of these "cells", often called "faces" or "facets".

4838: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetDepth()`, `DMPlexGetPointHeight()`
4839: @*/
4840: PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt height, PetscInt *start, PetscInt *end)
4841: {
4842:   DMLabel  label;
4843:   PetscInt depth, pStart, pEnd;

4846:   if (start) {
4848:     *start = 0;
4849:   }
4850:   if (end) {
4852:     *end = 0;
4853:   }
4854:   DMPlexGetChart(dm, &pStart, &pEnd);
4855:   if (pStart == pEnd) return 0;
4856:   if (height < 0) {
4857:     if (start) *start = pStart;
4858:     if (end) *end = pEnd;
4859:     return 0;
4860:   }
4861:   DMPlexGetDepthLabel(dm, &label);
4863:   DMLabelGetNumValues(label, &depth);
4864:   DMLabelGetStratumBounds(label, depth - 1 - height, start, end);
4865:   return 0;
4866: }

4868: /*@
4869:   DMPlexGetPointDepth - Get the depth of a given point

4871:   Not Collective

4873:   Input Parameters:
4874: + dm    - The `DMPLEX` object
4875: - point - The point

4877:   Output Parameter:
4878: . depth - The depth of the point

4880:   Level: intermediate

4882: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellType()`, `DMPlexGetDepthLabel()`, `DMPlexGetDepth()`, `DMPlexGetPointHeight()`
4883: @*/
4884: PetscErrorCode DMPlexGetPointDepth(DM dm, PetscInt point, PetscInt *depth)
4885: {
4888:   DMLabelGetValue(dm->depthLabel, point, depth);
4889:   return 0;
4890: }

4892: /*@
4893:   DMPlexGetPointHeight - Get the height of a given point

4895:   Not Collective

4897:   Input Parameters:
4898: + dm    - The `DMPLEX` object
4899: - point - The point

4901:   Output Parameter:
4902: . height - The height of the point

4904:   Level: intermediate

4906: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellType()`, `DMPlexGetDepthLabel()`, `DMPlexGetDepth()`, `DMPlexGetPointDepth()`
4907: @*/
4908: PetscErrorCode DMPlexGetPointHeight(DM dm, PetscInt point, PetscInt *height)
4909: {
4910:   PetscInt n, pDepth;

4914:   DMLabelGetNumValues(dm->depthLabel, &n);
4915:   DMLabelGetValue(dm->depthLabel, point, &pDepth);
4916:   *height = n - 1 - pDepth; /* DAG depth is n-1 */
4917:   return 0;
4918: }

4920: /*@
4921:   DMPlexGetCellTypeLabel - Get the `DMLabel` recording the polytope type of each cell

4923:   Not Collective

4925:   Input Parameter:
4926: . dm - The `DMPLEX` object

4928:   Output Parameter:
4929: . celltypeLabel - The `DMLabel` recording cell polytope type

4931:   Level: developer

4933:   Note:
4934:   This function will trigger automatica computation of cell types. This can be disabled by calling
4935:   `DMCreateLabel`(dm, "celltype") beforehand.

4937: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellType()`, `DMPlexGetDepthLabel()`, `DMCreateLabel()`
4938: @*/
4939: PetscErrorCode DMPlexGetCellTypeLabel(DM dm, DMLabel *celltypeLabel)
4940: {
4943:   if (!dm->celltypeLabel) DMPlexComputeCellTypes(dm);
4944:   *celltypeLabel = dm->celltypeLabel;
4945:   return 0;
4946: }

4948: /*@
4949:   DMPlexGetCellType - Get the polytope type of a given cell

4951:   Not Collective

4953:   Input Parameters:
4954: + dm   - The `DMPLEX` object
4955: - cell - The cell

4957:   Output Parameter:
4958: . celltype - The polytope type of the cell

4960:   Level: intermediate

4962: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellTypeLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetDepth()`
4963: @*/
4964: PetscErrorCode DMPlexGetCellType(DM dm, PetscInt cell, DMPolytopeType *celltype)
4965: {
4966:   DMLabel  label;
4967:   PetscInt ct;

4971:   DMPlexGetCellTypeLabel(dm, &label);
4972:   DMLabelGetValue(label, cell, &ct);
4974:   *celltype = (DMPolytopeType)ct;
4975:   return 0;
4976: }

4978: /*@
4979:   DMPlexSetCellType - Set the polytope type of a given cell

4981:   Not Collective

4983:   Input Parameters:
4984: + dm   - The `DMPLEX` object
4985: . cell - The cell
4986: - celltype - The polytope type of the cell

4988:   Level: advanced

4990:   Note:
4991:   By default, cell types will be automatically computed using `DMPlexComputeCellTypes()` before this function
4992:   is executed. This function will override the computed type. However, if automatic classification will not succeed
4993:   and a user wants to manually specify all types, the classification must be disabled by calling
4994:   DMCreaateLabel(dm, "celltype") before getting or setting any cell types.

4996: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellTypeLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetDepth()`, `DMPlexComputeCellTypes()`, `DMCreateLabel()`
4997: @*/
4998: PetscErrorCode DMPlexSetCellType(DM dm, PetscInt cell, DMPolytopeType celltype)
4999: {
5000:   DMLabel label;

5003:   DMPlexGetCellTypeLabel(dm, &label);
5004:   DMLabelSetValue(label, cell, celltype);
5005:   return 0;
5006: }

5008: PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
5009: {
5010:   PetscSection section, s;
5011:   Mat          m;
5012:   PetscInt     maxHeight;

5014:   DMClone(dm, cdm);
5015:   DMPlexGetMaxProjectionHeight(dm, &maxHeight);
5016:   DMPlexSetMaxProjectionHeight(*cdm, maxHeight);
5017:   PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);
5018:   DMSetLocalSection(*cdm, section);
5019:   PetscSectionDestroy(&section);
5020:   PetscSectionCreate(PETSC_COMM_SELF, &s);
5021:   MatCreate(PETSC_COMM_SELF, &m);
5022:   DMSetDefaultConstraints(*cdm, s, m, NULL);
5023:   PetscSectionDestroy(&s);
5024:   MatDestroy(&m);

5026:   DMSetNumFields(*cdm, 1);
5027:   DMCreateDS(*cdm);
5028:   return 0;
5029: }

5031: PetscErrorCode DMCreateCoordinateField_Plex(DM dm, DMField *field)
5032: {
5033:   Vec coordsLocal, cellCoordsLocal;
5034:   DM  coordsDM, cellCoordsDM;

5036:   *field = NULL;
5037:   DMGetCoordinatesLocal(dm, &coordsLocal);
5038:   DMGetCoordinateDM(dm, &coordsDM);
5039:   DMGetCellCoordinatesLocal(dm, &cellCoordsLocal);
5040:   DMGetCellCoordinateDM(dm, &cellCoordsDM);
5041:   if (coordsLocal && coordsDM) {
5042:     if (cellCoordsLocal && cellCoordsDM) DMFieldCreateDSWithDG(coordsDM, cellCoordsDM, 0, coordsLocal, cellCoordsLocal, field);
5043:     else DMFieldCreateDS(coordsDM, 0, coordsLocal, field);
5044:   }
5045:   return 0;
5046: }

5048: /*@C
5049:   DMPlexGetConeSection - Return a section which describes the layout of cone data

5051:   Not Collective

5053:   Input Parameters:
5054: . dm        - The `DMPLEX` object

5056:   Output Parameter:
5057: . section - The `PetscSection` object

5059:   Level: developer

5061: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetSupportSection()`, `DMPlexGetCones()`, `DMPlexGetConeOrientations()`, `PetscSection`
5062: @*/
5063: PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
5064: {
5065:   DM_Plex *mesh = (DM_Plex *)dm->data;

5068:   if (section) *section = mesh->coneSection;
5069:   return 0;
5070: }

5072: /*@C
5073:   DMPlexGetSupportSection - Return a section which describes the layout of support data

5075:   Not Collective

5077:   Input Parameters:
5078: . dm        - The `DMPLEX` object

5080:   Output Parameter:
5081: . section - The `PetscSection` object

5083:   Level: developer

5085: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetConeSection()`, `PetscSection`
5086: @*/
5087: PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
5088: {
5089:   DM_Plex *mesh = (DM_Plex *)dm->data;

5092:   if (section) *section = mesh->supportSection;
5093:   return 0;
5094: }

5096: /*@C
5097:   DMPlexGetCones - Return cone data

5099:   Not Collective

5101:   Input Parameters:
5102: . dm        - The `DMPLEX` object

5104:   Output Parameter:
5105: . cones - The cone for each point

5107:   Level: developer

5109: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetConeSection()`
5110: @*/
5111: PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
5112: {
5113:   DM_Plex *mesh = (DM_Plex *)dm->data;

5116:   if (cones) *cones = mesh->cones;
5117:   return 0;
5118: }

5120: /*@C
5121:   DMPlexGetConeOrientations - Return cone orientation data

5123:   Not Collective

5125:   Input Parameters:
5126: . dm        - The `DMPLEX` object

5128:   Output Parameter:
5129: . coneOrientations - The array of cone orientations for all points

5131:   Level: developer

5133:   Notes:
5134:   The `PetscSection` returned by `DMPlexGetConeSection()` partitions coneOrientations into cone orientations of particular points as returned by `DMPlexGetConeOrientation()`.

5136:   The meaning of coneOrientations values is detailed in `DMPlexGetConeOrientation()`.

5138: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetConeSection()`, `DMPlexGetConeOrientation()`, `PetscSection`
5139: @*/
5140: PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
5141: {
5142:   DM_Plex *mesh = (DM_Plex *)dm->data;

5145:   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
5146:   return 0;
5147: }

5149: /******************************** FEM Support **********************************/

5151: /*
5152:  Returns number of components and tensor degree for the field.  For interpolated meshes, line should be a point
5153:  representing a line in the section.
5154: */
5155: static PetscErrorCode PetscSectionFieldGetTensorDegree_Private(PetscSection section, PetscInt field, PetscInt line, PetscBool vertexchart, PetscInt *Nc, PetscInt *k)
5156: {
5158:   PetscSectionGetFieldComponents(section, field, Nc);
5159:   if (line < 0) {
5160:     *k  = 0;
5161:     *Nc = 0;
5162:   } else if (vertexchart) { /* If we only have a vertex chart, we must have degree k=1 */
5163:     *k = 1;
5164:   } else { /* Assume the full interpolated mesh is in the chart; lines in particular */
5165:     /* An order k SEM disc has k-1 dofs on an edge */
5166:     PetscSectionGetFieldDof(section, line, field, k);
5167:     *k = *k / *Nc + 1;
5168:   }
5169:   return 0;
5170: }

5172: /*@

5174:   DMPlexSetClosurePermutationTensor - Create a permutation from the default (BFS) point ordering in the closure, to a
5175:   lexicographic ordering over the tensor product cell (i.e., line, quad, hex, etc.), and set this permutation in the
5176:   section provided (or the section of the DM).

5178:   Input Parameters:
5179: + dm      - The DM
5180: . point   - Either a cell (highest dim point) or an edge (dim 1 point), or PETSC_DETERMINE
5181: - section - The PetscSection to reorder, or NULL for the default section

5183:   Example:
5184:   A typical interpolated single-quad mesh might order points as
5185: .vb
5186:   [c0, v1, v2, v3, v4, e5, e6, e7, e8]

5188:   v4 -- e6 -- v3
5189:   |           |
5190:   e7    c0    e8
5191:   |           |
5192:   v1 -- e5 -- v2
5193: .ve

5195:   (There is no significance to the ordering described here.)  The default section for a Q3 quad might typically assign
5196:   dofs in the order of points, e.g.,
5197: .vb
5198:     c0 -> [0,1,2,3]
5199:     v1 -> [4]
5200:     ...
5201:     e5 -> [8, 9]
5202: .ve

5204:   which corresponds to the dofs
5205: .vb
5206:     6   10  11  7
5207:     13  2   3   15
5208:     12  0   1   14
5209:     4   8   9   5
5210: .ve

5212:   The closure in BFS ordering works through height strata (cells, edges, vertices) to produce the ordering
5213: .vb
5214:   0 1 2 3 8 9 14 15 11 10 13 12 4 5 7 6
5215: .ve

5217:   After calling DMPlexSetClosurePermutationTensor(), the closure will be ordered lexicographically,
5218: .vb
5219:    4 8 9 5 12 0 1 14 13 2 3 15 6 10 11 7
5220: .ve

5222:   Level: developer

5224:   Note:
5225:   The point is used to determine the number of dofs/field on an edge. For SEM, this is related to the polynomial
5226:   degree of the basis.

5228: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMGetLocalSection()`, `PetscSectionSetClosurePermutation()`, `DMSetGlobalSection()`
5229: @*/
5230: PetscErrorCode DMPlexSetClosurePermutationTensor(DM dm, PetscInt point, PetscSection section)
5231: {
5232:   DMLabel   label;
5233:   PetscInt  dim, depth = -1, eStart = -1, Nf;
5234:   PetscBool vertexchart;

5236:   DMGetDimension(dm, &dim);
5237:   if (dim < 1) return 0;
5238:   if (point < 0) {
5239:     PetscInt sStart, sEnd;

5241:     DMPlexGetDepthStratum(dm, 1, &sStart, &sEnd);
5242:     point = sEnd - sStart ? sStart : point;
5243:   }
5244:   DMPlexGetDepthLabel(dm, &label);
5245:   if (point >= 0) DMLabelGetValue(label, point, &depth);
5246:   if (!section) DMGetLocalSection(dm, &section);
5247:   if (depth == 1) {
5248:     eStart = point;
5249:   } else if (depth == dim) {
5250:     const PetscInt *cone;

5252:     DMPlexGetCone(dm, point, &cone);
5253:     if (dim == 2) eStart = cone[0];
5254:     else if (dim == 3) {
5255:       const PetscInt *cone2;
5256:       DMPlexGetCone(dm, cone[0], &cone2);
5257:       eStart = cone2[0];
5258:     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Point %" PetscInt_FMT " of depth %" PetscInt_FMT " cannot be used to bootstrap spectral ordering for dim %" PetscInt_FMT, point, depth, dim);
5260:   { /* Determine whether the chart covers all points or just vertices. */
5261:     PetscInt pStart, pEnd, cStart, cEnd;
5262:     DMPlexGetDepthStratum(dm, 0, &pStart, &pEnd);
5263:     PetscSectionGetChart(section, &cStart, &cEnd);
5264:     if (pStart == cStart && pEnd == cEnd) vertexchart = PETSC_TRUE;      /* Only vertices are in the chart */
5265:     else if (cStart <= point && point < cEnd) vertexchart = PETSC_FALSE; /* Some interpolated points exist in the chart */
5266:     else vertexchart = PETSC_TRUE;                                       /* Some interpolated points are not in chart; assume dofs only at cells and vertices */
5267:   }
5268:   PetscSectionGetNumFields(section, &Nf);
5269:   for (PetscInt d = 1; d <= dim; d++) {
5270:     PetscInt  k, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
5271:     PetscInt *perm;

5273:     for (f = 0; f < Nf; ++f) {
5274:       PetscSectionFieldGetTensorDegree_Private(section, f, eStart, vertexchart, &Nc, &k);
5275:       size += PetscPowInt(k + 1, d) * Nc;
5276:     }
5277:     PetscMalloc1(size, &perm);
5278:     for (f = 0; f < Nf; ++f) {
5279:       switch (d) {
5280:       case 1:
5281:         PetscSectionFieldGetTensorDegree_Private(section, f, eStart, vertexchart, &Nc, &k);
5282:         /*
5283:          Original ordering is [ edge of length k-1; vtx0; vtx1 ]
5284:          We want              [ vtx0; edge of length k-1; vtx1 ]
5285:          */
5286:         for (c = 0; c < Nc; c++, offset++) perm[offset] = (k - 1) * Nc + c + foffset;
5287:         for (i = 0; i < k - 1; i++)
5288:           for (c = 0; c < Nc; c++, offset++) perm[offset] = i * Nc + c + foffset;
5289:         for (c = 0; c < Nc; c++, offset++) perm[offset] = k * Nc + c + foffset;
5290:         foffset = offset;
5291:         break;
5292:       case 2:
5293:         /* The original quad closure is oriented clockwise, {f, e_b, e_r, e_t, e_l, v_lb, v_rb, v_tr, v_tl} */
5294:         PetscSectionFieldGetTensorDegree_Private(section, f, eStart, vertexchart, &Nc, &k);
5295:         /* The SEM order is

5297:          v_lb, {e_b}, v_rb,
5298:          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
5299:          v_lt, reverse {e_t}, v_rt
5300:          */
5301:         {
5302:           const PetscInt of   = 0;
5303:           const PetscInt oeb  = of + PetscSqr(k - 1);
5304:           const PetscInt oer  = oeb + (k - 1);
5305:           const PetscInt oet  = oer + (k - 1);
5306:           const PetscInt oel  = oet + (k - 1);
5307:           const PetscInt ovlb = oel + (k - 1);
5308:           const PetscInt ovrb = ovlb + 1;
5309:           const PetscInt ovrt = ovrb + 1;
5310:           const PetscInt ovlt = ovrt + 1;
5311:           PetscInt       o;

5313:           /* bottom */
5314:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb * Nc + c + foffset;
5315:           for (o = oeb; o < oer; ++o)
5316:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
5317:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb * Nc + c + foffset;
5318:           /* middle */
5319:           for (i = 0; i < k - 1; ++i) {
5320:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel + (k - 2) - i) * Nc + c + foffset;
5321:             for (o = of + (k - 1) * i; o < of + (k - 1) * (i + 1); ++o)
5322:               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
5323:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer + i) * Nc + c + foffset;
5324:           }
5325:           /* top */
5326:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt * Nc + c + foffset;
5327:           for (o = oel - 1; o >= oet; --o)
5328:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
5329:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt * Nc + c + foffset;
5330:           foffset = offset;
5331:         }
5332:         break;
5333:       case 3:
5334:         /* The original hex closure is

5336:          {c,
5337:          f_b, f_t, f_f, f_b, f_r, f_l,
5338:          e_bl, e_bb, e_br, e_bf,  e_tf, e_tr, e_tb, e_tl,  e_rf, e_lf, e_lb, e_rb,
5339:          v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
5340:          */
5341:         PetscSectionFieldGetTensorDegree_Private(section, f, eStart, vertexchart, &Nc, &k);
5342:         /* The SEM order is
5343:          Bottom Slice
5344:          v_blf, {e^{(k-1)-n}_bf}, v_brf,
5345:          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
5346:          v_blb, {e_bb}, v_brb,

5348:          Middle Slice (j)
5349:          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
5350:          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
5351:          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,

5353:          Top Slice
5354:          v_tlf, {e_tf}, v_trf,
5355:          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
5356:          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
5357:          */
5358:         {
5359:           const PetscInt oc    = 0;
5360:           const PetscInt ofb   = oc + PetscSqr(k - 1) * (k - 1);
5361:           const PetscInt oft   = ofb + PetscSqr(k - 1);
5362:           const PetscInt off   = oft + PetscSqr(k - 1);
5363:           const PetscInt ofk   = off + PetscSqr(k - 1);
5364:           const PetscInt ofr   = ofk + PetscSqr(k - 1);
5365:           const PetscInt ofl   = ofr + PetscSqr(k - 1);
5366:           const PetscInt oebl  = ofl + PetscSqr(k - 1);
5367:           const PetscInt oebb  = oebl + (k - 1);
5368:           const PetscInt oebr  = oebb + (k - 1);
5369:           const PetscInt oebf  = oebr + (k - 1);
5370:           const PetscInt oetf  = oebf + (k - 1);
5371:           const PetscInt oetr  = oetf + (k - 1);
5372:           const PetscInt oetb  = oetr + (k - 1);
5373:           const PetscInt oetl  = oetb + (k - 1);
5374:           const PetscInt oerf  = oetl + (k - 1);
5375:           const PetscInt oelf  = oerf + (k - 1);
5376:           const PetscInt oelb  = oelf + (k - 1);
5377:           const PetscInt oerb  = oelb + (k - 1);
5378:           const PetscInt ovblf = oerb + (k - 1);
5379:           const PetscInt ovblb = ovblf + 1;
5380:           const PetscInt ovbrb = ovblb + 1;
5381:           const PetscInt ovbrf = ovbrb + 1;
5382:           const PetscInt ovtlf = ovbrf + 1;
5383:           const PetscInt ovtrf = ovtlf + 1;
5384:           const PetscInt ovtrb = ovtrf + 1;
5385:           const PetscInt ovtlb = ovtrb + 1;
5386:           PetscInt       o, n;

5388:           /* Bottom Slice */
5389:           /*   bottom */
5390:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf * Nc + c + foffset;
5391:           for (o = oetf - 1; o >= oebf; --o)
5392:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
5393:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf * Nc + c + foffset;
5394:           /*   middle */
5395:           for (i = 0; i < k - 1; ++i) {
5396:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl + i) * Nc + c + foffset;
5397:             for (n = 0; n < k - 1; ++n) {
5398:               o = ofb + n * (k - 1) + i;
5399:               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
5400:             }
5401:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr + (k - 2) - i) * Nc + c + foffset;
5402:           }
5403:           /*   top */
5404:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb * Nc + c + foffset;
5405:           for (o = oebb; o < oebr; ++o)
5406:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
5407:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb * Nc + c + foffset;

5409:           /* Middle Slice */
5410:           for (j = 0; j < k - 1; ++j) {
5411:             /*   bottom */
5412:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf + (k - 2) - j) * Nc + c + foffset;
5413:             for (o = off + j * (k - 1); o < off + (j + 1) * (k - 1); ++o)
5414:               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
5415:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf + j) * Nc + c + foffset;
5416:             /*   middle */
5417:             for (i = 0; i < k - 1; ++i) {
5418:               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl + i * (k - 1) + j) * Nc + c + foffset;
5419:               for (n = 0; n < k - 1; ++n)
5420:                 for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oc + (j * (k - 1) + i) * (k - 1) + n) * Nc + c + foffset;
5421:               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr + j * (k - 1) + i) * Nc + c + foffset;
5422:             }
5423:             /*   top */
5424:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb + j) * Nc + c + foffset;
5425:             for (o = ofk + j * (k - 1) + (k - 2); o >= ofk + j * (k - 1); --o)
5426:               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
5427:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb + (k - 2) - j) * Nc + c + foffset;
5428:           }

5430:           /* Top Slice */
5431:           /*   bottom */
5432:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf * Nc + c + foffset;
5433:           for (o = oetf; o < oetr; ++o)
5434:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
5435:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf * Nc + c + foffset;
5436:           /*   middle */
5437:           for (i = 0; i < k - 1; ++i) {
5438:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl + (k - 2) - i) * Nc + c + foffset;
5439:             for (n = 0; n < k - 1; ++n)
5440:               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oft + i * (k - 1) + n) * Nc + c + foffset;
5441:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr + i) * Nc + c + foffset;
5442:           }
5443:           /*   top */
5444:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb * Nc + c + foffset;
5445:           for (o = oetl - 1; o >= oetb; --o)
5446:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
5447:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb * Nc + c + foffset;

5449:           foffset = offset;
5450:         }
5451:         break;
5452:       default:
5453:         SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %" PetscInt_FMT, d);
5454:       }
5455:     }
5457:     /* Check permutation */
5458:     {
5459:       PetscInt *check;

5461:       PetscMalloc1(size, &check);
5462:       for (i = 0; i < size; ++i) {
5463:         check[i] = -1;
5465:       }
5466:       for (i = 0; i < size; ++i) check[perm[i]] = i;
5468:       PetscFree(check);
5469:     }
5470:     PetscSectionSetClosurePermutation_Internal(section, (PetscObject)dm, d, size, PETSC_OWN_POINTER, perm);
5471:     if (d == dim) { // Add permutation for localized (in case this is a coordinate DM)
5472:       PetscInt *loc_perm;
5473:       PetscMalloc1(size * 2, &loc_perm);
5474:       for (PetscInt i = 0; i < size; i++) {
5475:         loc_perm[i]        = perm[i];
5476:         loc_perm[size + i] = size + perm[i];
5477:       }
5478:       PetscSectionSetClosurePermutation_Internal(section, (PetscObject)dm, d, size * 2, PETSC_OWN_POINTER, loc_perm);
5479:     }
5480:   }
5481:   return 0;
5482: }

5484: PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
5485: {
5486:   PetscDS  prob;
5487:   PetscInt depth, Nf, h;
5488:   DMLabel  label;

5491:   DMGetDS(dm, &prob);
5492:   Nf      = prob->Nf;
5493:   label   = dm->depthLabel;
5494:   *dspace = NULL;
5495:   if (field < Nf) {
5496:     PetscObject disc = prob->disc[field];

5498:     if (disc->classid == PETSCFE_CLASSID) {
5499:       PetscDualSpace dsp;

5501:       PetscFEGetDualSpace((PetscFE)disc, &dsp);
5502:       DMLabelGetNumValues(label, &depth);
5503:       DMLabelGetValue(label, point, &h);
5504:       h = depth - 1 - h;
5505:       if (h) {
5506:         PetscDualSpaceGetHeightSubspace(dsp, h, dspace);
5507:       } else {
5508:         *dspace = dsp;
5509:       }
5510:     }
5511:   }
5512:   return 0;
5513: }

5515: static inline PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
5516: {
5517:   PetscScalar       *array;
5518:   const PetscScalar *vArray;
5519:   const PetscInt    *cone, *coneO;
5520:   PetscInt           pStart, pEnd, p, numPoints, size = 0, offset = 0;

5523:   PetscSectionGetChart(section, &pStart, &pEnd);
5524:   DMPlexGetConeSize(dm, point, &numPoints);
5525:   DMPlexGetCone(dm, point, &cone);
5526:   DMPlexGetConeOrientation(dm, point, &coneO);
5527:   if (!values || !*values) {
5528:     if ((point >= pStart) && (point < pEnd)) {
5529:       PetscInt dof;

5531:       PetscSectionGetDof(section, point, &dof);
5532:       size += dof;
5533:     }
5534:     for (p = 0; p < numPoints; ++p) {
5535:       const PetscInt cp = cone[p];
5536:       PetscInt       dof;

5538:       if ((cp < pStart) || (cp >= pEnd)) continue;
5539:       PetscSectionGetDof(section, cp, &dof);
5540:       size += dof;
5541:     }
5542:     if (!values) {
5543:       if (csize) *csize = size;
5544:       return 0;
5545:     }
5546:     DMGetWorkArray(dm, size, MPIU_SCALAR, &array);
5547:   } else {
5548:     array = *values;
5549:   }
5550:   size = 0;
5551:   VecGetArrayRead(v, &vArray);
5552:   if ((point >= pStart) && (point < pEnd)) {
5553:     PetscInt           dof, off, d;
5554:     const PetscScalar *varr;

5556:     PetscSectionGetDof(section, point, &dof);
5557:     PetscSectionGetOffset(section, point, &off);
5558:     varr = &vArray[off];
5559:     for (d = 0; d < dof; ++d, ++offset) array[offset] = varr[d];
5560:     size += dof;
5561:   }
5562:   for (p = 0; p < numPoints; ++p) {
5563:     const PetscInt     cp = cone[p];
5564:     PetscInt           o  = coneO[p];
5565:     PetscInt           dof, off, d;
5566:     const PetscScalar *varr;

5568:     if ((cp < pStart) || (cp >= pEnd)) continue;
5569:     PetscSectionGetDof(section, cp, &dof);
5570:     PetscSectionGetOffset(section, cp, &off);
5571:     varr = &vArray[off];
5572:     if (o >= 0) {
5573:       for (d = 0; d < dof; ++d, ++offset) array[offset] = varr[d];
5574:     } else {
5575:       for (d = dof - 1; d >= 0; --d, ++offset) array[offset] = varr[d];
5576:     }
5577:     size += dof;
5578:   }
5579:   VecRestoreArrayRead(v, &vArray);
5580:   if (!*values) {
5581:     if (csize) *csize = size;
5582:     *values = array;
5583:   } else {
5585:     *csize = size;
5586:   }
5587:   return 0;
5588: }

5590: /* Compress out points not in the section */
5591: static inline PetscErrorCode CompressPoints_Private(PetscSection section, PetscInt *numPoints, PetscInt points[])
5592: {
5593:   const PetscInt np = *numPoints;
5594:   PetscInt       pStart, pEnd, p, q;

5596:   PetscSectionGetChart(section, &pStart, &pEnd);
5597:   for (p = 0, q = 0; p < np; ++p) {
5598:     const PetscInt r = points[p * 2];
5599:     if ((r >= pStart) && (r < pEnd)) {
5600:       points[q * 2]     = r;
5601:       points[q * 2 + 1] = points[p * 2 + 1];
5602:       ++q;
5603:     }
5604:   }
5605:   *numPoints = q;
5606:   return 0;
5607: }

5609: /* Compressed closure does not apply closure permutation */
5610: PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
5611: {
5612:   const PetscInt *cla = NULL;
5613:   PetscInt        np, *pts = NULL;

5616:   PetscSectionGetClosureIndex(section, (PetscObject)dm, clSec, clPoints);
5617:   if (*clPoints) {
5618:     PetscInt dof, off;

5620:     PetscSectionGetDof(*clSec, point, &dof);
5621:     PetscSectionGetOffset(*clSec, point, &off);
5622:     ISGetIndices(*clPoints, &cla);
5623:     np  = dof / 2;
5624:     pts = (PetscInt *)&cla[off];
5625:   } else {
5626:     DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);
5627:     CompressPoints_Private(section, &np, pts);
5628:   }
5629:   *numPoints = np;
5630:   *points    = pts;
5631:   *clp       = cla;
5632:   return 0;
5633: }

5635: PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
5636: {
5638:   if (!*clPoints) {
5639:     DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);
5640:   } else {
5641:     ISRestoreIndices(*clPoints, clp);
5642:   }
5643:   *numPoints = 0;
5644:   *points    = NULL;
5645:   *clSec     = NULL;
5646:   *clPoints  = NULL;
5647:   *clp       = NULL;
5648:   return 0;
5649: }

5651: static inline PetscErrorCode DMPlexVecGetClosure_Static(DM dm, PetscSection section, PetscInt numPoints, const PetscInt points[], const PetscInt clperm[], const PetscScalar vArray[], PetscInt *size, PetscScalar array[])
5652: {
5653:   PetscInt            offset = 0, p;
5654:   const PetscInt    **perms  = NULL;
5655:   const PetscScalar **flips  = NULL;

5658:   *size = 0;
5659:   PetscSectionGetPointSyms(section, numPoints, points, &perms, &flips);
5660:   for (p = 0; p < numPoints; p++) {
5661:     const PetscInt     point = points[2 * p];
5662:     const PetscInt    *perm  = perms ? perms[p] : NULL;
5663:     const PetscScalar *flip  = flips ? flips[p] : NULL;
5664:     PetscInt           dof, off, d;
5665:     const PetscScalar *varr;

5667:     PetscSectionGetDof(section, point, &dof);
5668:     PetscSectionGetOffset(section, point, &off);
5669:     varr = &vArray[off];
5670:     if (clperm) {
5671:       if (perm) {
5672:         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]] = varr[d];
5673:       } else {
5674:         for (d = 0; d < dof; d++) array[clperm[offset + d]] = varr[d];
5675:       }
5676:       if (flip) {
5677:         for (d = 0; d < dof; d++) array[clperm[offset + d]] *= flip[d];
5678:       }
5679:     } else {
5680:       if (perm) {
5681:         for (d = 0; d < dof; d++) array[offset + perm[d]] = varr[d];
5682:       } else {
5683:         for (d = 0; d < dof; d++) array[offset + d] = varr[d];
5684:       }
5685:       if (flip) {
5686:         for (d = 0; d < dof; d++) array[offset + d] *= flip[d];
5687:       }
5688:     }
5689:     offset += dof;
5690:   }
5691:   PetscSectionRestorePointSyms(section, numPoints, points, &perms, &flips);
5692:   *size = offset;
5693:   return 0;
5694: }

5696: static inline PetscErrorCode DMPlexVecGetClosure_Fields_Static(DM dm, PetscSection section, PetscInt numPoints, const PetscInt points[], PetscInt numFields, const PetscInt clperm[], const PetscScalar vArray[], PetscInt *size, PetscScalar array[])
5697: {
5698:   PetscInt offset = 0, f;

5701:   *size = 0;
5702:   for (f = 0; f < numFields; ++f) {
5703:     PetscInt            p;
5704:     const PetscInt    **perms = NULL;
5705:     const PetscScalar **flips = NULL;

5707:     PetscSectionGetFieldPointSyms(section, f, numPoints, points, &perms, &flips);
5708:     for (p = 0; p < numPoints; p++) {
5709:       const PetscInt     point = points[2 * p];
5710:       PetscInt           fdof, foff, b;
5711:       const PetscScalar *varr;
5712:       const PetscInt    *perm = perms ? perms[p] : NULL;
5713:       const PetscScalar *flip = flips ? flips[p] : NULL;

5715:       PetscSectionGetFieldDof(section, point, f, &fdof);
5716:       PetscSectionGetFieldOffset(section, point, f, &foff);
5717:       varr = &vArray[foff];
5718:       if (clperm) {
5719:         if (perm) {
5720:           for (b = 0; b < fdof; b++) array[clperm[offset + perm[b]]] = varr[b];
5721:         } else {
5722:           for (b = 0; b < fdof; b++) array[clperm[offset + b]] = varr[b];
5723:         }
5724:         if (flip) {
5725:           for (b = 0; b < fdof; b++) array[clperm[offset + b]] *= flip[b];
5726:         }
5727:       } else {
5728:         if (perm) {
5729:           for (b = 0; b < fdof; b++) array[offset + perm[b]] = varr[b];
5730:         } else {
5731:           for (b = 0; b < fdof; b++) array[offset + b] = varr[b];
5732:         }
5733:         if (flip) {
5734:           for (b = 0; b < fdof; b++) array[offset + b] *= flip[b];
5735:         }
5736:       }
5737:       offset += fdof;
5738:     }
5739:     PetscSectionRestoreFieldPointSyms(section, f, numPoints, points, &perms, &flips);
5740:   }
5741:   *size = offset;
5742:   return 0;
5743: }

5745: /*@C
5746:   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'

5748:   Not collective

5750:   Input Parameters:
5751: + dm - The `DM`
5752: . section - The section describing the layout in v, or NULL to use the default section
5753: . v - The local vector
5754: - point - The point in the `DM`

5756:   Input/Output Parameters:
5757: + csize  - The size of the input values array, or NULL; on output the number of values in the closure
5758: - values - An array to use for the values, or NULL to have it allocated automatically;
5759:            if the user provided NULL, it is a borrowed array and should not be freed


5762:   Level: intermediate

5764:   Notes:
5765:   `DMPlexVecGetClosure()`/`DMPlexVecRestoreClosure()` only allocates the values array if it set to NULL in the
5766:   calling function. This is because `DMPlexVecGetClosure()` is typically called in the inner loop of a `Vec` or `Mat`
5767:   assembly function, and a user may already have allocated storage for this operation.

5769:   A typical use could be
5770: .vb
5771:    values = NULL;
5772:    DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);
5773:    for (cl = 0; cl < clSize; ++cl) {
5774:      <Compute on closure>
5775:    }
5776:    DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values);
5777: .ve
5778:   or
5779: .vb
5780:    PetscMalloc1(clMaxSize, &values);
5781:    for (p = pStart; p < pEnd; ++p) {
5782:      clSize = clMaxSize;
5783:      DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);
5784:      for (cl = 0; cl < clSize; ++cl) {
5785:        <Compute on closure>
5786:      }
5787:    }
5788:    PetscFree(values);
5789: .ve

5791:   Fortran Note:
5792:   The csize argument is not present in the Fortran binding since it is internal to the array.

5794: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexVecRestoreClosure()`, `DMPlexVecSetClosure()`, `DMPlexMatSetClosure()`
5795: @*/
5796: PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
5797: {
5798:   PetscSection    clSection;
5799:   IS              clPoints;
5800:   PetscInt       *points = NULL;
5801:   const PetscInt *clp, *perm;
5802:   PetscInt        depth, numFields, numPoints, asize;

5806:   if (!section) DMGetLocalSection(dm, &section);
5809:   DMPlexGetDepth(dm, &depth);
5810:   PetscSectionGetNumFields(section, &numFields);
5811:   if (depth == 1 && numFields < 2) {
5812:     DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);
5813:     return 0;
5814:   }
5815:   /* Get points */
5816:   DMPlexGetCompressedClosure(dm, section, point, &numPoints, &points, &clSection, &clPoints, &clp);
5817:   /* Get sizes */
5818:   asize = 0;
5819:   for (PetscInt p = 0; p < numPoints * 2; p += 2) {
5820:     PetscInt dof;
5821:     PetscSectionGetDof(section, points[p], &dof);
5822:     asize += dof;
5823:   }
5824:   if (values) {
5825:     const PetscScalar *vArray;
5826:     PetscInt           size;

5828:     if (*values) {
5830:     } else DMGetWorkArray(dm, asize, MPIU_SCALAR, values);
5831:     PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject)dm, depth, asize, &perm);
5832:     VecGetArrayRead(v, &vArray);
5833:     /* Get values */
5834:     if (numFields > 0) DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, *values);
5835:     else DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, *values);
5837:     /* Cleanup array */
5838:     VecRestoreArrayRead(v, &vArray);
5839:   }
5840:   if (csize) *csize = asize;
5841:   /* Cleanup points */
5842:   DMPlexRestoreCompressedClosure(dm, section, point, &numPoints, &points, &clSection, &clPoints, &clp);
5843:   return 0;
5844: }

5846: PetscErrorCode DMPlexVecGetClosureAtDepth_Internal(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt depth, PetscInt *csize, PetscScalar *values[])
5847: {
5848:   DMLabel            depthLabel;
5849:   PetscSection       clSection;
5850:   IS                 clPoints;
5851:   PetscScalar       *array;
5852:   const PetscScalar *vArray;
5853:   PetscInt          *points = NULL;
5854:   const PetscInt    *clp, *perm = NULL;
5855:   PetscInt           mdepth, numFields, numPoints, Np = 0, p, clsize, size;

5859:   if (!section) DMGetLocalSection(dm, &section);
5862:   DMPlexGetDepth(dm, &mdepth);
5863:   DMPlexGetDepthLabel(dm, &depthLabel);
5864:   PetscSectionGetNumFields(section, &numFields);
5865:   if (mdepth == 1 && numFields < 2) {
5866:     DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);
5867:     return 0;
5868:   }
5869:   /* Get points */
5870:   DMPlexGetCompressedClosure(dm, section, point, &numPoints, &points, &clSection, &clPoints, &clp);
5871:   for (clsize = 0, p = 0; p < Np; p++) {
5872:     PetscInt dof;
5873:     PetscSectionGetDof(section, points[2 * p], &dof);
5874:     clsize += dof;
5875:   }
5876:   PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject)dm, depth, clsize, &perm);
5877:   /* Filter points */
5878:   for (p = 0; p < numPoints * 2; p += 2) {
5879:     PetscInt dep;

5881:     DMLabelGetValue(depthLabel, points[p], &dep);
5882:     if (dep != depth) continue;
5883:     points[Np * 2 + 0] = points[p];
5884:     points[Np * 2 + 1] = points[p + 1];
5885:     ++Np;
5886:   }
5887:   /* Get array */
5888:   if (!values || !*values) {
5889:     PetscInt asize = 0, dof;

5891:     for (p = 0; p < Np * 2; p += 2) {
5892:       PetscSectionGetDof(section, points[p], &dof);
5893:       asize += dof;
5894:     }
5895:     if (!values) {
5896:       DMPlexRestoreCompressedClosure(dm, section, point, &numPoints, &points, &clSection, &clPoints, &clp);
5897:       if (csize) *csize = asize;
5898:       return 0;
5899:     }
5900:     DMGetWorkArray(dm, asize, MPIU_SCALAR, &array);
5901:   } else {
5902:     array = *values;
5903:   }
5904:   VecGetArrayRead(v, &vArray);
5905:   /* Get values */
5906:   if (numFields > 0) DMPlexVecGetClosure_Fields_Static(dm, section, Np, points, numFields, perm, vArray, &size, array);
5907:   else DMPlexVecGetClosure_Static(dm, section, Np, points, perm, vArray, &size, array);
5908:   /* Cleanup points */
5909:   DMPlexRestoreCompressedClosure(dm, section, point, &numPoints, &points, &clSection, &clPoints, &clp);
5910:   /* Cleanup array */
5911:   VecRestoreArrayRead(v, &vArray);
5912:   if (!*values) {
5913:     if (csize) *csize = size;
5914:     *values = array;
5915:   } else {
5917:     *csize = size;
5918:   }
5919:   return 0;
5920: }

5922: /*@C
5923:   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'

5925:   Not collective

5927:   Input Parameters:
5928: + dm - The `DM`
5929: . section - The section describing the layout in v, or NULL to use the default section
5930: . v - The local vector
5931: . point - The point in the `DM`
5932: . csize - The number of values in the closure, or NULL
5933: - values - The array of values, which is a borrowed array and should not be freed

5935:   Level: intermediate

5937:   Note:
5938:   The array values are discarded and not copied back into v. In order to copy values back to v, use `DMPlexVecSetClosure()`

5940:   Fortran Note:
5941:   The csize argument is not present in the Fortran binding since it is internal to the array.

5943: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexVecGetClosure()`, `DMPlexVecSetClosure()`, `DMPlexMatSetClosure()`
5944: @*/
5945: PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
5946: {
5947:   PetscInt size = 0;

5949:   /* Should work without recalculating size */
5950:   DMRestoreWorkArray(dm, size, MPIU_SCALAR, (void *)values);
5951:   *values = NULL;
5952:   return 0;
5953: }

5955: static inline void add(PetscScalar *x, PetscScalar y)
5956: {
5957:   *x += y;
5958: }
5959: static inline void insert(PetscScalar *x, PetscScalar y)
5960: {
5961:   *x = y;
5962: }

5964: static inline PetscErrorCode updatePoint_private(PetscSection section, PetscInt point, PetscInt dof, void (*fuse)(PetscScalar *, PetscScalar), PetscBool setBC, const PetscInt perm[], const PetscScalar flip[], const PetscInt clperm[], const PetscScalar values[], PetscInt offset, PetscScalar array[])
5965: {
5966:   PetscInt        cdof;  /* The number of constraints on this point */
5967:   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5968:   PetscScalar    *a;
5969:   PetscInt        off, cind = 0, k;

5971:   PetscSectionGetConstraintDof(section, point, &cdof);
5972:   PetscSectionGetOffset(section, point, &off);
5973:   a = &array[off];
5974:   if (!cdof || setBC) {
5975:     if (clperm) {
5976:       if (perm) {
5977:         for (k = 0; k < dof; ++k) fuse(&a[k], values[clperm[offset + perm[k]]] * (flip ? flip[perm[k]] : 1.));
5978:       } else {
5979:         for (k = 0; k < dof; ++k) fuse(&a[k], values[clperm[offset + k]] * (flip ? flip[k] : 1.));
5980:       }
5981:     } else {
5982:       if (perm) {
5983:         for (k = 0; k < dof; ++k) fuse(&a[k], values[offset + perm[k]] * (flip ? flip[perm[k]] : 1.));
5984:       } else {
5985:         for (k = 0; k < dof; ++k) fuse(&a[k], values[offset + k] * (flip ? flip[k] : 1.));
5986:       }
5987:     }
5988:   } else {
5989:     PetscSectionGetConstraintIndices(section, point, &cdofs);
5990:     if (clperm) {
5991:       if (perm) {
5992:         for (k = 0; k < dof; ++k) {
5993:           if ((cind < cdof) && (k == cdofs[cind])) {
5994:             ++cind;
5995:             continue;
5996:           }
5997:           fuse(&a[k], values[clperm[offset + perm[k]]] * (flip ? flip[perm[k]] : 1.));
5998:         }
5999:       } else {
6000:         for (k = 0; k < dof; ++k) {
6001:           if ((cind < cdof) && (k == cdofs[cind])) {
6002:             ++cind;
6003:             continue;
6004:           }
6005:           fuse(&a[k], values[clperm[offset + k]] * (flip ? flip[k] : 1.));
6006:         }
6007:       }
6008:     } else {
6009:       if (perm) {
6010:         for (k = 0; k < dof; ++k) {
6011:           if ((cind < cdof) && (k == cdofs[cind])) {
6012:             ++cind;
6013:             continue;
6014:           }
6015:           fuse(&a[k], values[offset + perm[k]] * (flip ? flip[perm[k]] : 1.));
6016:         }
6017:       } else {
6018:         for (k = 0; k < dof; ++k) {
6019:           if ((cind < cdof) && (k == cdofs[cind])) {
6020:             ++cind;
6021:             continue;
6022:           }
6023:           fuse(&a[k], values[offset + k] * (flip ? flip[k] : 1.));
6024:         }
6025:       }
6026:     }
6027:   }
6028:   return 0;
6029: }

6031: static inline PetscErrorCode updatePointBC_private(PetscSection section, PetscInt point, PetscInt dof, void (*fuse)(PetscScalar *, PetscScalar), const PetscInt perm[], const PetscScalar flip[], const PetscInt clperm[], const PetscScalar values[], PetscInt offset, PetscScalar array[])
6032: {
6033:   PetscInt        cdof;  /* The number of constraints on this point */
6034:   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
6035:   PetscScalar    *a;
6036:   PetscInt        off, cind = 0, k;

6038:   PetscSectionGetConstraintDof(section, point, &cdof);
6039:   PetscSectionGetOffset(section, point, &off);
6040:   a = &array[off];
6041:   if (cdof) {
6042:     PetscSectionGetConstraintIndices(section, point, &cdofs);
6043:     if (clperm) {
6044:       if (perm) {
6045:         for (k = 0; k < dof; ++k) {
6046:           if ((cind < cdof) && (k == cdofs[cind])) {
6047:             fuse(&a[k], values[clperm[offset + perm[k]]] * (flip ? flip[perm[k]] : 1.));
6048:             cind++;
6049:           }
6050:         }
6051:       } else {
6052:         for (k = 0; k < dof; ++k) {
6053:           if ((cind < cdof) && (k == cdofs[cind])) {
6054:             fuse(&a[k], values[clperm[offset + k]] * (flip ? flip[k] : 1.));
6055:             cind++;
6056:           }
6057:         }
6058:       }
6059:     } else {
6060:       if (perm) {
6061:         for (k = 0; k < dof; ++k) {
6062:           if ((cind < cdof) && (k == cdofs[cind])) {
6063:             fuse(&a[k], values[offset + perm[k]] * (flip ? flip[perm[k]] : 1.));
6064:             cind++;
6065:           }
6066:         }
6067:       } else {
6068:         for (k = 0; k < dof; ++k) {
6069:           if ((cind < cdof) && (k == cdofs[cind])) {
6070:             fuse(&a[k], values[offset + k] * (flip ? flip[k] : 1.));
6071:             cind++;
6072:           }
6073:         }
6074:       }
6075:     }
6076:   }
6077:   return 0;
6078: }

6080: static inline PetscErrorCode updatePointFields_private(PetscSection section, PetscInt point, const PetscInt *perm, const PetscScalar *flip, PetscInt f, void (*fuse)(PetscScalar *, PetscScalar), PetscBool setBC, const PetscInt clperm[], const PetscScalar values[], PetscInt *offset, PetscScalar array[])
6081: {
6082:   PetscScalar    *a;
6083:   PetscInt        fdof, foff, fcdof, foffset = *offset;
6084:   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
6085:   PetscInt        cind = 0, b;

6087:   PetscSectionGetFieldDof(section, point, f, &fdof);
6088:   PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);
6089:   PetscSectionGetFieldOffset(section, point, f, &foff);
6090:   a = &array[foff];
6091:   if (!fcdof || setBC) {
6092:     if (clperm) {
6093:       if (perm) {
6094:         for (b = 0; b < fdof; b++) fuse(&a[b], values[clperm[foffset + perm[b]]] * (flip ? flip[perm[b]] : 1.));
6095:       } else {
6096:         for (b = 0; b < fdof; b++) fuse(&a[b], values[clperm[foffset + b]] * (flip ? flip[b] : 1.));
6097:       }
6098:     } else {
6099:       if (perm) {
6100:         for (b = 0; b < fdof; b++) fuse(&a[b], values[foffset + perm[b]] * (flip ? flip[perm[b]] : 1.));
6101:       } else {
6102:         for (b = 0; b < fdof; b++) fuse(&a[b], values[foffset + b] * (flip ? flip[b] : 1.));
6103:       }
6104:     }
6105:   } else {
6106:     PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);
6107:     if (clperm) {
6108:       if (perm) {
6109:         for (b = 0; b < fdof; b++) {
6110:           if ((cind < fcdof) && (b == fcdofs[cind])) {
6111:             ++cind;
6112:             continue;
6113:           }
6114:           fuse(&a[b], values[clperm[foffset + perm[b]]] * (flip ? flip[perm[b]] : 1.));
6115:         }
6116:       } else {
6117:         for (b = 0; b < fdof; b++) {
6118:           if ((cind < fcdof) && (b == fcdofs[cind])) {
6119:             ++cind;
6120:             continue;
6121:           }
6122:           fuse(&a[b], values[clperm[foffset + b]] * (flip ? flip[b] : 1.));
6123:         }
6124:       }
6125:     } else {
6126:       if (perm) {
6127:         for (b = 0; b < fdof; b++) {
6128:           if ((cind < fcdof) && (b == fcdofs[cind])) {
6129:             ++cind;
6130:             continue;
6131:           }
6132:           fuse(&a[b], values[foffset + perm[b]] * (flip ? flip[perm[b]] : 1.));
6133:         }
6134:       } else {
6135:         for (b = 0; b < fdof; b++) {
6136:           if ((cind < fcdof) && (b == fcdofs[cind])) {
6137:             ++cind;
6138:             continue;
6139:           }
6140:           fuse(&a[b], values[foffset + b] * (flip ? flip[b] : 1.));
6141:         }
6142:       }
6143:     }
6144:   }
6145:   *offset += fdof;
6146:   return 0;
6147: }

6149: static inline PetscErrorCode updatePointFieldsBC_private(PetscSection section, PetscInt point, const PetscInt perm[], const PetscScalar flip[], PetscInt f, PetscInt Ncc, const PetscInt comps[], void (*fuse)(PetscScalar *, PetscScalar), const PetscInt clperm[], const PetscScalar values[], PetscInt *offset, PetscScalar array[])
6150: {
6151:   PetscScalar    *a;
6152:   PetscInt        fdof, foff, fcdof, foffset = *offset;
6153:   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
6154:   PetscInt        Nc, cind = 0, ncind = 0, b;
6155:   PetscBool       ncSet, fcSet;

6157:   PetscSectionGetFieldComponents(section, f, &Nc);
6158:   PetscSectionGetFieldDof(section, point, f, &fdof);
6159:   PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);
6160:   PetscSectionGetFieldOffset(section, point, f, &foff);
6161:   a = &array[foff];
6162:   if (fcdof) {
6163:     /* We just override fcdof and fcdofs with Ncc and comps */
6164:     PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);
6165:     if (clperm) {
6166:       if (perm) {
6167:         if (comps) {
6168:           for (b = 0; b < fdof; b++) {
6169:             ncSet = fcSet = PETSC_FALSE;
6170:             if (b % Nc == comps[ncind]) {
6171:               ncind = (ncind + 1) % Ncc;
6172:               ncSet = PETSC_TRUE;
6173:             }
6174:             if ((cind < fcdof) && (b == fcdofs[cind])) {
6175:               ++cind;
6176:               fcSet = PETSC_TRUE;
6177:             }
6178:             if (ncSet && fcSet) fuse(&a[b], values[clperm[foffset + perm[b]]] * (flip ? flip[perm[b]] : 1.));
6179:           }
6180:         } else {
6181:           for (b = 0; b < fdof; b++) {
6182:             if ((cind < fcdof) && (b == fcdofs[cind])) {
6183:               fuse(&a[b], values[clperm[foffset + perm[b]]] * (flip ? flip[perm[b]] : 1.));
6184:               ++cind;
6185:             }
6186:           }
6187:         }
6188:       } else {
6189:         if (comps) {
6190:           for (b = 0; b < fdof; b++) {
6191:             ncSet = fcSet = PETSC_FALSE;
6192:             if (b % Nc == comps[ncind]) {
6193:               ncind = (ncind + 1) % Ncc;
6194:               ncSet = PETSC_TRUE;
6195:             }
6196:             if ((cind < fcdof) && (b == fcdofs[cind])) {
6197:               ++cind;
6198:               fcSet = PETSC_TRUE;
6199:             }
6200:             if (ncSet && fcSet) fuse(&a[b], values[clperm[foffset + b]] * (flip ? flip[b] : 1.));
6201:           }
6202:         } else {
6203:           for (b = 0; b < fdof; b++) {
6204:             if ((cind < fcdof) && (b == fcdofs[cind])) {
6205:               fuse(&a[b], values[clperm[foffset + b]] * (flip ? flip[b] : 1.));
6206:               ++cind;
6207:             }
6208:           }
6209:         }
6210:       }
6211:     } else {
6212:       if (perm) {
6213:         if (comps) {
6214:           for (b = 0; b < fdof; b++) {
6215:             ncSet = fcSet = PETSC_FALSE;
6216:             if (b % Nc == comps[ncind]) {
6217:               ncind = (ncind + 1) % Ncc;
6218:               ncSet = PETSC_TRUE;
6219:             }
6220:             if ((cind < fcdof) && (b == fcdofs[cind])) {
6221:               ++cind;
6222:               fcSet = PETSC_TRUE;
6223:             }
6224:             if (ncSet && fcSet) fuse(&a[b], values[foffset + perm[b]] * (flip ? flip[perm[b]] : 1.));
6225:           }
6226:         } else {
6227:           for (b = 0; b < fdof; b++) {
6228:             if ((cind < fcdof) && (b == fcdofs[cind])) {
6229:               fuse(&a[b], values[foffset + perm[b]] * (flip ? flip[perm[b]] : 1.));
6230:               ++cind;
6231:             }
6232:           }
6233:         }
6234:       } else {
6235:         if (comps) {
6236:           for (b = 0; b < fdof; b++) {
6237:             ncSet = fcSet = PETSC_FALSE;
6238:             if (b % Nc == comps[ncind]) {
6239:               ncind = (ncind + 1) % Ncc;
6240:               ncSet = PETSC_TRUE;
6241:             }
6242:             if ((cind < fcdof) && (b == fcdofs[cind])) {
6243:               ++cind;
6244:               fcSet = PETSC_TRUE;
6245:             }
6246:             if (ncSet && fcSet) fuse(&a[b], values[foffset + b] * (flip ? flip[b] : 1.));
6247:           }
6248:         } else {
6249:           for (b = 0; b < fdof; b++) {
6250:             if ((cind < fcdof) && (b == fcdofs[cind])) {
6251:               fuse(&a[b], values[foffset + b] * (flip ? flip[b] : 1.));
6252:               ++cind;
6253:             }
6254:           }
6255:         }
6256:       }
6257:     }
6258:   }
6259:   *offset += fdof;
6260:   return 0;
6261: }

6263: static inline PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
6264: {
6265:   PetscScalar    *array;
6266:   const PetscInt *cone, *coneO;
6267:   PetscInt        pStart, pEnd, p, numPoints, off, dof;

6270:   PetscSectionGetChart(section, &pStart, &pEnd);
6271:   DMPlexGetConeSize(dm, point, &numPoints);
6272:   DMPlexGetCone(dm, point, &cone);
6273:   DMPlexGetConeOrientation(dm, point, &coneO);
6274:   VecGetArray(v, &array);
6275:   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
6276:     const PetscInt cp = !p ? point : cone[p - 1];
6277:     const PetscInt o  = !p ? 0 : coneO[p - 1];

6279:     if ((cp < pStart) || (cp >= pEnd)) {
6280:       dof = 0;
6281:       continue;
6282:     }
6283:     PetscSectionGetDof(section, cp, &dof);
6284:     /* ADD_VALUES */
6285:     {
6286:       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
6287:       PetscScalar    *a;
6288:       PetscInt        cdof, coff, cind = 0, k;

6290:       PetscSectionGetConstraintDof(section, cp, &cdof);
6291:       PetscSectionGetOffset(section, cp, &coff);
6292:       a = &array[coff];
6293:       if (!cdof) {
6294:         if (o >= 0) {
6295:           for (k = 0; k < dof; ++k) a[k] += values[off + k];
6296:         } else {
6297:           for (k = 0; k < dof; ++k) a[k] += values[off + dof - k - 1];
6298:         }
6299:       } else {
6300:         PetscSectionGetConstraintIndices(section, cp, &cdofs);
6301:         if (o >= 0) {
6302:           for (k = 0; k < dof; ++k) {
6303:             if ((cind < cdof) && (k == cdofs[cind])) {
6304:               ++cind;
6305:               continue;
6306:             }
6307:             a[k] += values[off + k];
6308:           }
6309:         } else {
6310:           for (k = 0; k < dof; ++k) {
6311:             if ((cind < cdof) && (k == cdofs[cind])) {
6312:               ++cind;
6313:               continue;
6314:             }
6315:             a[k] += values[off + dof - k - 1];
6316:           }
6317:         }
6318:       }
6319:     }
6320:   }
6321:   VecRestoreArray(v, &array);
6322:   return 0;
6323: }

6325: /*@C
6326:   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'

6328:   Not collective

6330:   Input Parameters:
6331: + dm - The `DM`
6332: . section - The section describing the layout in v, or NULL to use the default section
6333: . v - The local vector
6334: . point - The point in the DM
6335: . values - The array of values
6336: - mode - The insert mode. One of `INSERT_ALL_VALUES`, `ADD_ALL_VALUES`, `INSERT_VALUES`, `ADD_VALUES`, `INSERT_BC_VALUES`, and `ADD_BC_VALUES`,
6337:          where `INSERT_ALL_VALUES` and `ADD_ALL_VALUES` also overwrite boundary conditions.

6339:   Level: intermediate

6341: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexVecGetClosure()`, `DMPlexMatSetClosure()`
6342: @*/
6343: PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
6344: {
6345:   PetscSection    clSection;
6346:   IS              clPoints;
6347:   PetscScalar    *array;
6348:   PetscInt       *points = NULL;
6349:   const PetscInt *clp, *clperm = NULL;
6350:   PetscInt        depth, numFields, numPoints, p, clsize;

6354:   if (!section) DMGetLocalSection(dm, &section);
6357:   DMPlexGetDepth(dm, &depth);
6358:   PetscSectionGetNumFields(section, &numFields);
6359:   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
6360:     DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);
6361:     return 0;
6362:   }
6363:   /* Get points */
6364:   DMPlexGetCompressedClosure(dm, section, point, &numPoints, &points, &clSection, &clPoints, &clp);
6365:   for (clsize = 0, p = 0; p < numPoints; p++) {
6366:     PetscInt dof;
6367:     PetscSectionGetDof(section, points[2 * p], &dof);
6368:     clsize += dof;
6369:   }
6370:   PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject)dm, depth, clsize, &clperm);
6371:   /* Get array */
6372:   VecGetArray(v, &array);
6373:   /* Get values */
6374:   if (numFields > 0) {
6375:     PetscInt offset = 0, f;
6376:     for (f = 0; f < numFields; ++f) {
6377:       const PetscInt    **perms = NULL;
6378:       const PetscScalar **flips = NULL;

6380:       PetscSectionGetFieldPointSyms(section, f, numPoints, points, &perms, &flips);
6381:       switch (mode) {
6382:       case INSERT_VALUES:
6383:         for (p = 0; p < numPoints; p++) {
6384:           const PetscInt     point = points[2 * p];
6385:           const PetscInt    *perm  = perms ? perms[p] : NULL;
6386:           const PetscScalar *flip  = flips ? flips[p] : NULL;
6387:           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
6388:         }
6389:         break;
6390:       case INSERT_ALL_VALUES:
6391:         for (p = 0; p < numPoints; p++) {
6392:           const PetscInt     point = points[2 * p];
6393:           const PetscInt    *perm  = perms ? perms[p] : NULL;
6394:           const PetscScalar *flip  = flips ? flips[p] : NULL;
6395:           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
6396:         }
6397:         break;
6398:       case INSERT_BC_VALUES:
6399:         for (p = 0; p < numPoints; p++) {
6400:           const PetscInt     point = points[2 * p];
6401:           const PetscInt    *perm  = perms ? perms[p] : NULL;
6402:           const PetscScalar *flip  = flips ? flips[p] : NULL;
6403:           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, insert, clperm, values, &offset, array);
6404:         }
6405:         break;
6406:       case ADD_VALUES:
6407:         for (p = 0; p < numPoints; p++) {
6408:           const PetscInt     point = points[2 * p];
6409:           const PetscInt    *perm  = perms ? perms[p] : NULL;
6410:           const PetscScalar *flip  = flips ? flips[p] : NULL;
6411:           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
6412:         }
6413:         break;
6414:       case ADD_ALL_VALUES:
6415:         for (p = 0; p < numPoints; p++) {
6416:           const PetscInt     point = points[2 * p];
6417:           const PetscInt    *perm  = perms ? perms[p] : NULL;
6418:           const PetscScalar *flip  = flips ? flips[p] : NULL;
6419:           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
6420:         }
6421:         break;
6422:       case ADD_BC_VALUES:
6423:         for (p = 0; p < numPoints; p++) {
6424:           const PetscInt     point = points[2 * p];
6425:           const PetscInt    *perm  = perms ? perms[p] : NULL;
6426:           const PetscScalar *flip  = flips ? flips[p] : NULL;
6427:           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, add, clperm, values, &offset, array);
6428:         }
6429:         break;
6430:       default:
6431:         SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
6432:       }
6433:       PetscSectionRestoreFieldPointSyms(section, f, numPoints, points, &perms, &flips);
6434:     }
6435:   } else {
6436:     PetscInt            dof, off;
6437:     const PetscInt    **perms = NULL;
6438:     const PetscScalar **flips = NULL;

6440:     PetscSectionGetPointSyms(section, numPoints, points, &perms, &flips);
6441:     switch (mode) {
6442:     case INSERT_VALUES:
6443:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
6444:         const PetscInt     point = points[2 * p];
6445:         const PetscInt    *perm  = perms ? perms[p] : NULL;
6446:         const PetscScalar *flip  = flips ? flips[p] : NULL;
6447:         PetscSectionGetDof(section, point, &dof);
6448:         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
6449:       }
6450:       break;
6451:     case INSERT_ALL_VALUES:
6452:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
6453:         const PetscInt     point = points[2 * p];
6454:         const PetscInt    *perm  = perms ? perms[p] : NULL;
6455:         const PetscScalar *flip  = flips ? flips[p] : NULL;
6456:         PetscSectionGetDof(section, point, &dof);
6457:         updatePoint_private(section, point, dof, insert, PETSC_TRUE, perm, flip, clperm, values, off, array);
6458:       }
6459:       break;
6460:     case INSERT_BC_VALUES:
6461:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
6462:         const PetscInt     point = points[2 * p];
6463:         const PetscInt    *perm  = perms ? perms[p] : NULL;
6464:         const PetscScalar *flip  = flips ? flips[p] : NULL;
6465:         PetscSectionGetDof(section, point, &dof);
6466:         updatePointBC_private(section, point, dof, insert, perm, flip, clperm, values, off, array);
6467:       }
6468:       break;
6469:     case ADD_VALUES:
6470:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
6471:         const PetscInt     point = points[2 * p];
6472:         const PetscInt    *perm  = perms ? perms[p] : NULL;
6473:         const PetscScalar *flip  = flips ? flips[p] : NULL;
6474:         PetscSectionGetDof(section, point, &dof);
6475:         updatePoint_private(section, point, dof, add, PETSC_FALSE, perm, flip, clperm, values, off, array);
6476:       }
6477:       break;
6478:     case ADD_ALL_VALUES:
6479:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
6480:         const PetscInt     point = points[2 * p];
6481:         const PetscInt    *perm  = perms ? perms[p] : NULL;
6482:         const PetscScalar *flip  = flips ? flips[p] : NULL;
6483:         PetscSectionGetDof(section, point, &dof);
6484:         updatePoint_private(section, point, dof, add, PETSC_TRUE, perm, flip, clperm, values, off, array);
6485:       }
6486:       break;
6487:     case ADD_BC_VALUES:
6488:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
6489:         const PetscInt     point = points[2 * p];
6490:         const PetscInt    *perm  = perms ? perms[p] : NULL;
6491:         const PetscScalar *flip  = flips ? flips[p] : NULL;
6492:         PetscSectionGetDof(section, point, &dof);
6493:         updatePointBC_private(section, point, dof, add, perm, flip, clperm, values, off, array);
6494:       }
6495:       break;
6496:     default:
6497:       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
6498:     }
6499:     PetscSectionRestorePointSyms(section, numPoints, points, &perms, &flips);
6500:   }
6501:   /* Cleanup points */
6502:   DMPlexRestoreCompressedClosure(dm, section, point, &numPoints, &points, &clSection, &clPoints, &clp);
6503:   /* Cleanup array */
6504:   VecRestoreArray(v, &array);
6505:   return 0;
6506: }

6508: /* Check whether the given point is in the label. If not, update the offset to skip this point */
6509: static inline PetscErrorCode CheckPoint_Private(DMLabel label, PetscInt labelId, PetscSection section, PetscInt point, PetscInt f, PetscInt *offset, PetscBool *contains)
6510: {
6511:   *contains = PETSC_TRUE;
6512:   if (label) {
6513:     PetscInt fdof;

6515:     DMLabelStratumHasPoint(label, labelId, point, contains);
6516:     if (!*contains) {
6517:       PetscSectionGetFieldDof(section, point, f, &fdof);
6518:       *offset += fdof;
6519:       return 0;
6520:     }
6521:   }
6522:   return 0;
6523: }

6525: /* Unlike DMPlexVecSetClosure(), this uses plex-native closure permutation, not a user-specified permutation such as DMPlexSetClosurePermutationTensor(). */
6526: PetscErrorCode DMPlexVecSetFieldClosure_Internal(DM dm, PetscSection section, Vec v, PetscBool fieldActive[], PetscInt point, PetscInt Ncc, const PetscInt comps[], DMLabel label, PetscInt labelId, const PetscScalar values[], InsertMode mode)
6527: {
6528:   PetscSection    clSection;
6529:   IS              clPoints;
6530:   PetscScalar    *array;
6531:   PetscInt       *points = NULL;
6532:   const PetscInt *clp;
6533:   PetscInt        numFields, numPoints, p;
6534:   PetscInt        offset = 0, f;

6538:   if (!section) DMGetLocalSection(dm, &section);
6541:   PetscSectionGetNumFields(section, &numFields);
6542:   /* Get points */
6543:   DMPlexGetCompressedClosure(dm, section, point, &numPoints, &points, &clSection, &clPoints, &clp);
6544:   /* Get array */
6545:   VecGetArray(v, &array);
6546:   /* Get values */
6547:   for (f = 0; f < numFields; ++f) {
6548:     const PetscInt    **perms = NULL;
6549:     const PetscScalar **flips = NULL;
6550:     PetscBool           contains;

6552:     if (!fieldActive[f]) {
6553:       for (p = 0; p < numPoints * 2; p += 2) {
6554:         PetscInt fdof;
6555:         PetscSectionGetFieldDof(section, points[p], f, &fdof);
6556:         offset += fdof;
6557:       }
6558:       continue;
6559:     }
6560:     PetscSectionGetFieldPointSyms(section, f, numPoints, points, &perms, &flips);
6561:     switch (mode) {
6562:     case INSERT_VALUES:
6563:       for (p = 0; p < numPoints; p++) {
6564:         const PetscInt     point = points[2 * p];
6565:         const PetscInt    *perm  = perms ? perms[p] : NULL;
6566:         const PetscScalar *flip  = flips ? flips[p] : NULL;
6567:         CheckPoint_Private(label, labelId, section, point, f, &offset, &contains);
6568:         if (!contains) continue;
6569:         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, NULL, values, &offset, array);
6570:       }
6571:       break;
6572:     case INSERT_ALL_VALUES:
6573:       for (p = 0; p < numPoints; p++) {
6574:         const PetscInt     point = points[2 * p];
6575:         const PetscInt    *perm  = perms ? perms[p] : NULL;
6576:         const PetscScalar *flip  = flips ? flips[p] : NULL;
6577:         CheckPoint_Private(label, labelId, section, point, f, &offset, &contains);
6578:         if (!contains) continue;
6579:         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, NULL, values, &offset, array);
6580:       }
6581:       break;
6582:     case INSERT_BC_VALUES:
6583:       for (p = 0; p < numPoints; p++) {
6584:         const PetscInt     point = points[2 * p];
6585:         const PetscInt    *perm  = perms ? perms[p] : NULL;
6586:         const PetscScalar *flip  = flips ? flips[p] : NULL;
6587:         CheckPoint_Private(label, labelId, section, point, f, &offset, &contains);
6588:         if (!contains) continue;
6589:         updatePointFieldsBC_private(section, point, perm, flip, f, Ncc, comps, insert, NULL, values, &offset, array);
6590:       }
6591:       break;
6592:     case ADD_VALUES:
6593:       for (p = 0; p < numPoints; p++) {
6594:         const PetscInt     point = points[2 * p];
6595:         const PetscInt    *perm  = perms ? perms[p] : NULL;
6596:         const PetscScalar *flip  = flips ? flips[p] : NULL;
6597:         CheckPoint_Private(label, labelId, section, point, f, &offset, &contains);
6598:         if (!contains) continue;
6599:         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, NULL, values, &offset, array);
6600:       }
6601:       break;
6602:     case ADD_ALL_VALUES:
6603:       for (p = 0; p < numPoints; p++) {
6604:         const PetscInt     point = points[2 * p];
6605:         const PetscInt    *perm  = perms ? perms[p] : NULL;
6606:         const PetscScalar *flip  = flips ? flips[p] : NULL;
6607:         CheckPoint_Private(label, labelId, section, point, f, &offset, &contains);
6608:         if (!contains) continue;
6609:         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, NULL, values, &offset, array);
6610:       }
6611:       break;
6612:     default:
6613:       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
6614:     }
6615:     PetscSectionRestoreFieldPointSyms(section, f, numPoints, points, &perms, &flips);
6616:   }
6617:   /* Cleanup points */
6618:   DMPlexRestoreCompressedClosure(dm, section, point, &numPoints, &points, &clSection, &clPoints, &clp);
6619:   /* Cleanup array */
6620:   VecRestoreArray(v, &array);
6621:   return 0;
6622: }

6624: static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
6625: {
6626:   PetscMPIInt rank;
6627:   PetscInt    i, j;

6629:   MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);
6630:   PetscViewerASCIIPrintf(viewer, "[%d]mat for point %" PetscInt_FMT "\n", rank, point);
6631:   for (i = 0; i < numRIndices; i++) PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%" PetscInt_FMT "] = %" PetscInt_FMT "\n", rank, i, rindices[i]);
6632:   for (i = 0; i < numCIndices; i++) PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%" PetscInt_FMT "] = %" PetscInt_FMT "\n", rank, i, cindices[i]);
6633:   numCIndices = numCIndices ? numCIndices : numRIndices;
6634:   if (!values) return 0;
6635:   for (i = 0; i < numRIndices; i++) {
6636:     PetscViewerASCIIPrintf(viewer, "[%d]", rank);
6637:     for (j = 0; j < numCIndices; j++) {
6638: #if defined(PETSC_USE_COMPLEX)
6639:       PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i * numCIndices + j]), (double)PetscImaginaryPart(values[i * numCIndices + j]));
6640: #else
6641:       PetscViewerASCIIPrintf(viewer, " %g", (double)values[i * numCIndices + j]);
6642: #endif
6643:     }
6644:     PetscViewerASCIIPrintf(viewer, "\n");
6645:   }
6646:   return 0;
6647: }

6649: /*
6650:   DMPlexGetIndicesPoint_Internal - Add the indices for dofs on a point to an index array

6652:   Input Parameters:
6653: + section - The section for this data layout
6654: . islocal - Is the section (and thus indices being requested) local or global?
6655: . point   - The point contributing dofs with these indices
6656: . off     - The global offset of this point
6657: . loff    - The local offset of each field
6658: . setBC   - The flag determining whether to include indices of boundary values
6659: . perm    - A permutation of the dofs on this point, or NULL
6660: - indperm - A permutation of the entire indices array, or NULL

6662:   Output Parameter:
6663: . indices - Indices for dofs on this point

6665:   Level: developer

6667:   Note: The indices could be local or global, depending on the value of 'off'.
6668: */
6669: PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscBool islocal, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], const PetscInt indperm[], PetscInt indices[])
6670: {
6671:   PetscInt        dof;   /* The number of unknowns on this point */
6672:   PetscInt        cdof;  /* The number of constraints on this point */
6673:   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
6674:   PetscInt        cind = 0, k;

6677:   PetscSectionGetDof(section, point, &dof);
6678:   PetscSectionGetConstraintDof(section, point, &cdof);
6679:   if (!cdof || setBC) {
6680:     for (k = 0; k < dof; ++k) {
6681:       const PetscInt preind = perm ? *loff + perm[k] : *loff + k;
6682:       const PetscInt ind    = indperm ? indperm[preind] : preind;

6684:       indices[ind] = off + k;
6685:     }
6686:   } else {
6687:     PetscSectionGetConstraintIndices(section, point, &cdofs);
6688:     for (k = 0; k < dof; ++k) {
6689:       const PetscInt preind = perm ? *loff + perm[k] : *loff + k;
6690:       const PetscInt ind    = indperm ? indperm[preind] : preind;

6692:       if ((cind < cdof) && (k == cdofs[cind])) {
6693:         /* Insert check for returning constrained indices */
6694:         indices[ind] = -(off + k + 1);
6695:         ++cind;
6696:       } else {
6697:         indices[ind] = off + k - (islocal ? 0 : cind);
6698:       }
6699:     }
6700:   }
6701:   *loff += dof;
6702:   return 0;
6703: }

6705: /*
6706:  DMPlexGetIndicesPointFields_Internal - gets section indices for a point in its canonical ordering.

6708:  Input Parameters:
6709: + section - a section (global or local)
6710: - islocal - PETSC_TRUE if requesting local indices (i.e., section is local); PETSC_FALSE for global
6711: . point - point within section
6712: . off - The offset of this point in the (local or global) indexed space - should match islocal and (usually) the section
6713: . foffs - array of length numFields containing the offset in canonical point ordering (the location in indices) of each field
6714: . setBC - identify constrained (boundary condition) points via involution.
6715: . perms - perms[f][permsoff][:] is a permutation of dofs within each field
6716: . permsoff - offset
6717: - indperm - index permutation

6719:  Output Parameter:
6720: . foffs - each entry is incremented by the number of (unconstrained if setBC=FALSE) dofs in that field
6721: . indices - array to hold indices (as defined by section) of each dof associated with point

6723:  Notes:
6724:  If section is local and setBC=true, there is no distinction between constrained and unconstrained dofs.
6725:  If section is local and setBC=false, the indices for constrained points are the involution -(i+1) of their position
6726:  in the local vector.

6728:  If section is global and setBC=false, the indices for constrained points are negative (and their value is not
6729:  significant).  It is invalid to call with a global section and setBC=true.

6731:  Developer Note:
6732:  The section is only used for field layout, so islocal is technically a statement about the offset (off).  At some point
6733:  in the future, global sections may have fields set, in which case we could pass the global section and obtain the
6734:  offset could be obtained from the section instead of passing it explicitly as we do now.

6736:  Example:
6737:  Suppose a point contains one field with three components, and for which the unconstrained indices are {10, 11, 12}.
6738:  When the middle component is constrained, we get the array {10, -12, 12} for (islocal=TRUE, setBC=FALSE).
6739:  Note that -12 is the involution of 11, so the user can involute negative indices to recover local indices.
6740:  The global vector does not store constrained dofs, so when this function returns global indices, say {110, -112, 111}, the value of -112 is an arbitrary flag that should not be interpreted beyond its sign.

6742:  Level: developer
6743: */
6744: PetscErrorCode DMPlexGetIndicesPointFields_Internal(PetscSection section, PetscBool islocal, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, const PetscInt indperm[], PetscInt indices[])
6745: {
6746:   PetscInt numFields, foff, f;

6749:   PetscSectionGetNumFields(section, &numFields);
6750:   for (f = 0, foff = 0; f < numFields; ++f) {
6751:     PetscInt        fdof, cfdof;
6752:     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
6753:     PetscInt        cind = 0, b;
6754:     const PetscInt *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;

6756:     PetscSectionGetFieldDof(section, point, f, &fdof);
6757:     PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);
6758:     if (!cfdof || setBC) {
6759:       for (b = 0; b < fdof; ++b) {
6760:         const PetscInt preind = perm ? foffs[f] + perm[b] : foffs[f] + b;
6761:         const PetscInt ind    = indperm ? indperm[preind] : preind;

6763:         indices[ind] = off + foff + b;
6764:       }
6765:     } else {
6766:       PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);
6767:       for (b = 0; b < fdof; ++b) {
6768:         const PetscInt preind = perm ? foffs[f] + perm[b] : foffs[f] + b;
6769:         const PetscInt ind    = indperm ? indperm[preind] : preind;

6771:         if ((cind < cfdof) && (b == fcdofs[cind])) {
6772:           indices[ind] = -(off + foff + b + 1);
6773:           ++cind;
6774:         } else {
6775:           indices[ind] = off + foff + b - (islocal ? 0 : cind);
6776:         }
6777:       }
6778:     }
6779:     foff += (setBC || islocal ? fdof : (fdof - cfdof));
6780:     foffs[f] += fdof;
6781:   }
6782:   return 0;
6783: }

6785: /*
6786:   This version believes the globalSection offsets for each field, rather than just the point offset

6788:  . foffs - The offset into 'indices' for each field, since it is segregated by field

6790:  Notes:
6791:  The semantics of this function relate to that of setBC=FALSE in DMPlexGetIndicesPointFields_Internal.
6792:  Since this function uses global indices, setBC=TRUE would be invalid, so no such argument exists.
6793: */
6794: static PetscErrorCode DMPlexGetIndicesPointFieldsSplit_Internal(PetscSection section, PetscSection globalSection, PetscInt point, PetscInt foffs[], const PetscInt ***perms, PetscInt permsoff, const PetscInt indperm[], PetscInt indices[])
6795: {
6796:   PetscInt numFields, foff, f;

6798:   PetscSectionGetNumFields(section, &numFields);
6799:   for (f = 0; f < numFields; ++f) {
6800:     PetscInt        fdof, cfdof;
6801:     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
6802:     PetscInt        cind = 0, b;
6803:     const PetscInt *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;

6805:     PetscSectionGetFieldDof(section, point, f, &fdof);
6806:     PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);
6807:     PetscSectionGetFieldOffset(globalSection, point, f, &foff);
6808:     if (!cfdof) {
6809:       for (b = 0; b < fdof; ++b) {
6810:         const PetscInt preind = perm ? foffs[f] + perm[b] : foffs[f] + b;
6811:         const PetscInt ind    = indperm ? indperm[preind] : preind;

6813:         indices[ind] = foff + b;
6814:       }
6815:     } else {
6816:       PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);
6817:       for (b = 0; b < fdof; ++b) {
6818:         const PetscInt preind = perm ? foffs[f] + perm[b] : foffs[f] + b;
6819:         const PetscInt ind    = indperm ? indperm[preind] : preind;

6821:         if ((cind < cfdof) && (b == fcdofs[cind])) {
6822:           indices[ind] = -(foff + b + 1);
6823:           ++cind;
6824:         } else {
6825:           indices[ind] = foff + b - cind;
6826:         }
6827:       }
6828:     }
6829:     foffs[f] += fdof;
6830:   }
6831:   return 0;
6832: }

6834: PetscErrorCode DMPlexAnchorsModifyMat(DM dm, PetscSection section, PetscInt numPoints, PetscInt numIndices, const PetscInt points[], const PetscInt ***perms, const PetscScalar values[], PetscInt *outNumPoints, PetscInt *outNumIndices, PetscInt *outPoints[], PetscScalar *outValues[], PetscInt offsets[], PetscBool multiplyLeft)
6835: {
6836:   Mat             cMat;
6837:   PetscSection    aSec, cSec;
6838:   IS              aIS;
6839:   PetscInt        aStart = -1, aEnd = -1;
6840:   const PetscInt *anchors;
6841:   PetscInt        numFields, f, p, q, newP = 0;
6842:   PetscInt        newNumPoints = 0, newNumIndices = 0;
6843:   PetscInt       *newPoints, *indices, *newIndices;
6844:   PetscInt        maxAnchor, maxDof;
6845:   PetscInt        newOffsets[32];
6846:   PetscInt       *pointMatOffsets[32];
6847:   PetscInt       *newPointOffsets[32];
6848:   PetscScalar    *pointMat[32];
6849:   PetscScalar    *newValues      = NULL, *tmpValues;
6850:   PetscBool       anyConstrained = PETSC_FALSE;

6854:   PetscSectionGetNumFields(section, &numFields);

6856:   DMPlexGetAnchors(dm, &aSec, &aIS);
6857:   /* if there are point-to-point constraints */
6858:   if (aSec) {
6859:     PetscArrayzero(newOffsets, 32);
6860:     ISGetIndices(aIS, &anchors);
6861:     PetscSectionGetChart(aSec, &aStart, &aEnd);
6862:     /* figure out how many points are going to be in the new element matrix
6863:      * (we allow double counting, because it's all just going to be summed
6864:      * into the global matrix anyway) */
6865:     for (p = 0; p < 2 * numPoints; p += 2) {
6866:       PetscInt b    = points[p];
6867:       PetscInt bDof = 0, bSecDof;

6869:       PetscSectionGetDof(section, b, &bSecDof);
6870:       if (!bSecDof) continue;
6871:       if (b >= aStart && b < aEnd) PetscSectionGetDof(aSec, b, &bDof);
6872:       if (bDof) {
6873:         /* this point is constrained */
6874:         /* it is going to be replaced by its anchors */
6875:         PetscInt bOff, q;

6877:         anyConstrained = PETSC_TRUE;
6878:         newNumPoints += bDof;
6879:         PetscSectionGetOffset(aSec, b, &bOff);
6880:         for (q = 0; q < bDof; q++) {
6881:           PetscInt a = anchors[bOff + q];
6882:           PetscInt aDof;

6884:           PetscSectionGetDof(section, a, &aDof);
6885:           newNumIndices += aDof;
6886:           for (f = 0; f < numFields; ++f) {
6887:             PetscInt fDof;

6889:             PetscSectionGetFieldDof(section, a, f, &fDof);
6890:             newOffsets[f + 1] += fDof;
6891:           }
6892:         }
6893:       } else {
6894:         /* this point is not constrained */
6895:         newNumPoints++;
6896:         newNumIndices += bSecDof;
6897:         for (f = 0; f < numFields; ++f) {
6898:           PetscInt fDof;

6900:           PetscSectionGetFieldDof(section, b, f, &fDof);
6901:           newOffsets[f + 1] += fDof;
6902:         }
6903:       }
6904:     }
6905:   }
6906:   if (!anyConstrained) {
6907:     if (outNumPoints) *outNumPoints = 0;
6908:     if (outNumIndices) *outNumIndices = 0;
6909:     if (outPoints) *outPoints = NULL;
6910:     if (outValues) *outValues = NULL;
6911:     if (aSec) ISRestoreIndices(aIS, &anchors);
6912:     return 0;
6913:   }

6915:   if (outNumPoints) *outNumPoints = newNumPoints;
6916:   if (outNumIndices) *outNumIndices = newNumIndices;

6918:   for (f = 0; f < numFields; ++f) newOffsets[f + 1] += newOffsets[f];

6920:   if (!outPoints && !outValues) {
6921:     if (offsets) {
6922:       for (f = 0; f <= numFields; f++) offsets[f] = newOffsets[f];
6923:     }
6924:     if (aSec) ISRestoreIndices(aIS, &anchors);
6925:     return 0;
6926:   }


6930:   DMGetDefaultConstraints(dm, &cSec, &cMat, NULL);

6932:   /* workspaces */
6933:   if (numFields) {
6934:     for (f = 0; f < numFields; f++) {
6935:       DMGetWorkArray(dm, numPoints + 1, MPIU_INT, &pointMatOffsets[f]);
6936:       DMGetWorkArray(dm, numPoints + 1, MPIU_INT, &newPointOffsets[f]);
6937:     }
6938:   } else {
6939:     DMGetWorkArray(dm, numPoints + 1, MPIU_INT, &pointMatOffsets[0]);
6940:     DMGetWorkArray(dm, numPoints, MPIU_INT, &newPointOffsets[0]);
6941:   }

6943:   /* get workspaces for the point-to-point matrices */
6944:   if (numFields) {
6945:     PetscInt totalOffset, totalMatOffset;

6947:     for (p = 0; p < numPoints; p++) {
6948:       PetscInt b    = points[2 * p];
6949:       PetscInt bDof = 0, bSecDof;

6951:       PetscSectionGetDof(section, b, &bSecDof);
6952:       if (!bSecDof) {
6953:         for (f = 0; f < numFields; f++) {
6954:           newPointOffsets[f][p + 1] = 0;
6955:           pointMatOffsets[f][p + 1] = 0;
6956:         }
6957:         continue;
6958:       }
6959:       if (b >= aStart && b < aEnd) PetscSectionGetDof(aSec, b, &bDof);
6960:       if (bDof) {
6961:         for (f = 0; f < numFields; f++) {
6962:           PetscInt fDof, q, bOff, allFDof = 0;

6964:           PetscSectionGetFieldDof(section, b, f, &fDof);
6965:           PetscSectionGetOffset(aSec, b, &bOff);
6966:           for (q = 0; q < bDof; q++) {
6967:             PetscInt a = anchors[bOff + q];
6968:             PetscInt aFDof;

6970:             PetscSectionGetFieldDof(section, a, f, &aFDof);
6971:             allFDof += aFDof;
6972:           }
6973:           newPointOffsets[f][p + 1] = allFDof;
6974:           pointMatOffsets[f][p + 1] = fDof * allFDof;
6975:         }
6976:       } else {
6977:         for (f = 0; f < numFields; f++) {
6978:           PetscInt fDof;

6980:           PetscSectionGetFieldDof(section, b, f, &fDof);
6981:           newPointOffsets[f][p + 1] = fDof;
6982:           pointMatOffsets[f][p + 1] = 0;
6983:         }
6984:       }
6985:     }
6986:     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
6987:       newPointOffsets[f][0] = totalOffset;
6988:       pointMatOffsets[f][0] = totalMatOffset;
6989:       for (p = 0; p < numPoints; p++) {
6990:         newPointOffsets[f][p + 1] += newPointOffsets[f][p];
6991:         pointMatOffsets[f][p + 1] += pointMatOffsets[f][p];
6992:       }
6993:       totalOffset    = newPointOffsets[f][numPoints];
6994:       totalMatOffset = pointMatOffsets[f][numPoints];
6995:       DMGetWorkArray(dm, pointMatOffsets[f][numPoints], MPIU_SCALAR, &pointMat[f]);
6996:     }
6997:   } else {
6998:     for (p = 0; p < numPoints; p++) {
6999:       PetscInt b    = points[2 * p];
7000:       PetscInt bDof = 0, bSecDof;

7002:       PetscSectionGetDof(section, b, &bSecDof);
7003:       if (!bSecDof) {
7004:         newPointOffsets[0][p + 1] = 0;
7005:         pointMatOffsets[0][p + 1] = 0;
7006:         continue;
7007:       }
7008:       if (b >= aStart && b < aEnd) PetscSectionGetDof(aSec, b, &bDof);
7009:       if (bDof) {
7010:         PetscInt bOff, q, allDof = 0;

7012:         PetscSectionGetOffset(aSec, b, &bOff);
7013:         for (q = 0; q < bDof; q++) {
7014:           PetscInt a = anchors[bOff + q], aDof;

7016:           PetscSectionGetDof(section, a, &aDof);
7017:           allDof += aDof;
7018:         }
7019:         newPointOffsets[0][p + 1] = allDof;
7020:         pointMatOffsets[0][p + 1] = bSecDof * allDof;
7021:       } else {
7022:         newPointOffsets[0][p + 1] = bSecDof;
7023:         pointMatOffsets[0][p + 1] = 0;
7024:       }
7025:     }
7026:     newPointOffsets[0][0] = 0;
7027:     pointMatOffsets[0][0] = 0;
7028:     for (p = 0; p < numPoints; p++) {
7029:       newPointOffsets[0][p + 1] += newPointOffsets[0][p];
7030:       pointMatOffsets[0][p + 1] += pointMatOffsets[0][p];
7031:     }
7032:     DMGetWorkArray(dm, pointMatOffsets[0][numPoints], MPIU_SCALAR, &pointMat[0]);
7033:   }

7035:   /* output arrays */
7036:   DMGetWorkArray(dm, 2 * newNumPoints, MPIU_INT, &newPoints);

7038:   /* get the point-to-point matrices; construct newPoints */
7039:   PetscSectionGetMaxDof(aSec, &maxAnchor);
7040:   PetscSectionGetMaxDof(section, &maxDof);
7041:   DMGetWorkArray(dm, maxDof, MPIU_INT, &indices);
7042:   DMGetWorkArray(dm, maxAnchor * maxDof, MPIU_INT, &newIndices);
7043:   if (numFields) {
7044:     for (p = 0, newP = 0; p < numPoints; p++) {
7045:       PetscInt b    = points[2 * p];
7046:       PetscInt o    = points[2 * p + 1];
7047:       PetscInt bDof = 0, bSecDof;

7049:       PetscSectionGetDof(section, b, &bSecDof);
7050:       if (!bSecDof) continue;
7051:       if (b >= aStart && b < aEnd) PetscSectionGetDof(aSec, b, &bDof);
7052:       if (bDof) {
7053:         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;

7055:         fStart[0] = 0;
7056:         fEnd[0]   = 0;
7057:         for (f = 0; f < numFields; f++) {
7058:           PetscInt fDof;

7060:           PetscSectionGetFieldDof(cSec, b, f, &fDof);
7061:           fStart[f + 1] = fStart[f] + fDof;
7062:           fEnd[f + 1]   = fStart[f + 1];
7063:         }
7064:         PetscSectionGetOffset(cSec, b, &bOff);
7065:         DMPlexGetIndicesPointFields_Internal(cSec, PETSC_TRUE, b, bOff, fEnd, PETSC_TRUE, perms, p, NULL, indices);

7067:         fAnchorStart[0] = 0;
7068:         fAnchorEnd[0]   = 0;
7069:         for (f = 0; f < numFields; f++) {
7070:           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];

7072:           fAnchorStart[f + 1] = fAnchorStart[f] + fDof;
7073:           fAnchorEnd[f + 1]   = fAnchorStart[f + 1];
7074:         }
7075:         PetscSectionGetOffset(aSec, b, &bOff);
7076:         for (q = 0; q < bDof; q++) {
7077:           PetscInt a = anchors[bOff + q], aOff;

7079:           /* we take the orientation of ap into account in the order that we constructed the indices above: the newly added points have no orientation */
7080:           newPoints[2 * (newP + q)]     = a;
7081:           newPoints[2 * (newP + q) + 1] = 0;
7082:           PetscSectionGetOffset(section, a, &aOff);
7083:           DMPlexGetIndicesPointFields_Internal(section, PETSC_TRUE, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, NULL, newIndices);
7084:         }
7085:         newP += bDof;

7087:         if (outValues) {
7088:           /* get the point-to-point submatrix */
7089:           for (f = 0; f < numFields; f++) MatGetValues(cMat, fEnd[f] - fStart[f], indices + fStart[f], fAnchorEnd[f] - fAnchorStart[f], newIndices + fAnchorStart[f], pointMat[f] + pointMatOffsets[f][p]);
7090:         }
7091:       } else {
7092:         newPoints[2 * newP]     = b;
7093:         newPoints[2 * newP + 1] = o;
7094:         newP++;
7095:       }
7096:     }
7097:   } else {
7098:     for (p = 0; p < numPoints; p++) {
7099:       PetscInt b    = points[2 * p];
7100:       PetscInt o    = points[2 * p + 1];
7101:       PetscInt bDof = 0, bSecDof;

7103:       PetscSectionGetDof(section, b, &bSecDof);
7104:       if (!bSecDof) continue;
7105:       if (b >= aStart && b < aEnd) PetscSectionGetDof(aSec, b, &bDof);
7106:       if (bDof) {
7107:         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;

7109:         PetscSectionGetOffset(cSec, b, &bOff);
7110:         DMPlexGetIndicesPoint_Internal(cSec, PETSC_TRUE, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, NULL, indices);

7112:         PetscSectionGetOffset(aSec, b, &bOff);
7113:         for (q = 0; q < bDof; q++) {
7114:           PetscInt a = anchors[bOff + q], aOff;

7116:           /* we take the orientation of ap into account in the order that we constructed the indices above: the newly added points have no orientation */

7118:           newPoints[2 * (newP + q)]     = a;
7119:           newPoints[2 * (newP + q) + 1] = 0;
7120:           PetscSectionGetOffset(section, a, &aOff);
7121:           DMPlexGetIndicesPoint_Internal(section, PETSC_TRUE, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, NULL, newIndices);
7122:         }
7123:         newP += bDof;

7125:         /* get the point-to-point submatrix */
7126:         if (outValues) MatGetValues(cMat, bEnd, indices, bAnchorEnd, newIndices, pointMat[0] + pointMatOffsets[0][p]);
7127:       } else {
7128:         newPoints[2 * newP]     = b;
7129:         newPoints[2 * newP + 1] = o;
7130:         newP++;
7131:       }
7132:     }
7133:   }

7135:   if (outValues) {
7136:     DMGetWorkArray(dm, newNumIndices * numIndices, MPIU_SCALAR, &tmpValues);
7137:     PetscArrayzero(tmpValues, newNumIndices * numIndices);
7138:     /* multiply constraints on the right */
7139:     if (numFields) {
7140:       for (f = 0; f < numFields; f++) {
7141:         PetscInt oldOff = offsets[f];

7143:         for (p = 0; p < numPoints; p++) {
7144:           PetscInt cStart = newPointOffsets[f][p];
7145:           PetscInt b      = points[2 * p];
7146:           PetscInt c, r, k;
7147:           PetscInt dof;

7149:           PetscSectionGetFieldDof(section, b, f, &dof);
7150:           if (!dof) continue;
7151:           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
7152:             PetscInt           nCols = newPointOffsets[f][p + 1] - cStart;
7153:             const PetscScalar *mat   = pointMat[f] + pointMatOffsets[f][p];

7155:             for (r = 0; r < numIndices; r++) {
7156:               for (c = 0; c < nCols; c++) {
7157:                 for (k = 0; k < dof; k++) tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
7158:               }
7159:             }
7160:           } else {
7161:             /* copy this column as is */
7162:             for (r = 0; r < numIndices; r++) {
7163:               for (c = 0; c < dof; c++) tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
7164:             }
7165:           }
7166:           oldOff += dof;
7167:         }
7168:       }
7169:     } else {
7170:       PetscInt oldOff = 0;
7171:       for (p = 0; p < numPoints; p++) {
7172:         PetscInt cStart = newPointOffsets[0][p];
7173:         PetscInt b      = points[2 * p];
7174:         PetscInt c, r, k;
7175:         PetscInt dof;

7177:         PetscSectionGetDof(section, b, &dof);
7178:         if (!dof) continue;
7179:         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
7180:           PetscInt           nCols = newPointOffsets[0][p + 1] - cStart;
7181:           const PetscScalar *mat   = pointMat[0] + pointMatOffsets[0][p];

7183:           for (r = 0; r < numIndices; r++) {
7184:             for (c = 0; c < nCols; c++) {
7185:               for (k = 0; k < dof; k++) tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
7186:             }
7187:           }
7188:         } else {
7189:           /* copy this column as is */
7190:           for (r = 0; r < numIndices; r++) {
7191:             for (c = 0; c < dof; c++) tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
7192:           }
7193:         }
7194:         oldOff += dof;
7195:       }
7196:     }

7198:     if (multiplyLeft) {
7199:       DMGetWorkArray(dm, newNumIndices * newNumIndices, MPIU_SCALAR, &newValues);
7200:       PetscArrayzero(newValues, newNumIndices * newNumIndices);
7201:       /* multiply constraints transpose on the left */
7202:       if (numFields) {
7203:         for (f = 0; f < numFields; f++) {
7204:           PetscInt oldOff = offsets[f];

7206:           for (p = 0; p < numPoints; p++) {
7207:             PetscInt rStart = newPointOffsets[f][p];
7208:             PetscInt b      = points[2 * p];
7209:             PetscInt c, r, k;
7210:             PetscInt dof;

7212:             PetscSectionGetFieldDof(section, b, f, &dof);
7213:             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
7214:               PetscInt                          nRows = newPointOffsets[f][p + 1] - rStart;
7215:               const PetscScalar *PETSC_RESTRICT mat   = pointMat[f] + pointMatOffsets[f][p];

7217:               for (r = 0; r < nRows; r++) {
7218:                 for (c = 0; c < newNumIndices; c++) {
7219:                   for (k = 0; k < dof; k++) newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
7220:                 }
7221:               }
7222:             } else {
7223:               /* copy this row as is */
7224:               for (r = 0; r < dof; r++) {
7225:                 for (c = 0; c < newNumIndices; c++) newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
7226:               }
7227:             }
7228:             oldOff += dof;
7229:           }
7230:         }
7231:       } else {
7232:         PetscInt oldOff = 0;

7234:         for (p = 0; p < numPoints; p++) {
7235:           PetscInt rStart = newPointOffsets[0][p];
7236:           PetscInt b      = points[2 * p];
7237:           PetscInt c, r, k;
7238:           PetscInt dof;

7240:           PetscSectionGetDof(section, b, &dof);
7241:           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
7242:             PetscInt                          nRows = newPointOffsets[0][p + 1] - rStart;
7243:             const PetscScalar *PETSC_RESTRICT mat   = pointMat[0] + pointMatOffsets[0][p];

7245:             for (r = 0; r < nRows; r++) {
7246:               for (c = 0; c < newNumIndices; c++) {
7247:                 for (k = 0; k < dof; k++) newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
7248:               }
7249:             }
7250:           } else {
7251:             /* copy this row as is */
7252:             for (r = 0; r < dof; r++) {
7253:               for (c = 0; c < newNumIndices; c++) newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
7254:             }
7255:           }
7256:           oldOff += dof;
7257:         }
7258:       }

7260:       DMRestoreWorkArray(dm, newNumIndices * numIndices, MPIU_SCALAR, &tmpValues);
7261:     } else {
7262:       newValues = tmpValues;
7263:     }
7264:   }

7266:   /* clean up */
7267:   DMRestoreWorkArray(dm, maxDof, MPIU_INT, &indices);
7268:   DMRestoreWorkArray(dm, maxAnchor * maxDof, MPIU_INT, &newIndices);

7270:   if (numFields) {
7271:     for (f = 0; f < numFields; f++) {
7272:       DMRestoreWorkArray(dm, pointMatOffsets[f][numPoints], MPIU_SCALAR, &pointMat[f]);
7273:       DMRestoreWorkArray(dm, numPoints + 1, MPIU_INT, &pointMatOffsets[f]);
7274:       DMRestoreWorkArray(dm, numPoints + 1, MPIU_INT, &newPointOffsets[f]);
7275:     }
7276:   } else {
7277:     DMRestoreWorkArray(dm, pointMatOffsets[0][numPoints], MPIU_SCALAR, &pointMat[0]);
7278:     DMRestoreWorkArray(dm, numPoints + 1, MPIU_INT, &pointMatOffsets[0]);
7279:     DMRestoreWorkArray(dm, numPoints + 1, MPIU_INT, &newPointOffsets[0]);
7280:   }
7281:   ISRestoreIndices(aIS, &anchors);

7283:   /* output */
7284:   if (outPoints) {
7285:     *outPoints = newPoints;
7286:   } else {
7287:     DMRestoreWorkArray(dm, 2 * newNumPoints, MPIU_INT, &newPoints);
7288:   }
7289:   if (outValues) *outValues = newValues;
7290:   for (f = 0; f <= numFields; f++) offsets[f] = newOffsets[f];
7291:   return 0;
7292: }

7294: /*@C
7295:   DMPlexGetClosureIndices - Gets the global dof indices associated with the closure of the given point within the provided sections.

7297:   Not collective

7299:   Input Parameters:
7300: + dm         - The `DM`
7301: . section    - The `PetscSection` describing the points (a local section)
7302: . idxSection - The `PetscSection` from which to obtain indices (may be local or global)
7303: . point      - The point defining the closure
7304: - useClPerm  - Use the closure point permutation if available

7306:   Output Parameters:
7307: + numIndices - The number of dof indices in the closure of point with the input sections
7308: . indices    - The dof indices
7309: . outOffsets - Array to write the field offsets into, or NULL
7310: - values     - The input values, which may be modified if sign flips are induced by the point symmetries, or NULL

7312:   Level: advanced

7314:   Notes:
7315:   Must call `DMPlexRestoreClosureIndices()` to free allocated memory

7317:   If idxSection is global, any constrained dofs (see `DMAddBoundary()`, for example) will get negative indices.  The value
7318:   of those indices is not significant.  If idxSection is local, the constrained dofs will yield the involution -(idx+1)
7319:   of their index in a local vector.  A caller who does not wish to distinguish those points may recover the nonnegative
7320:   indices via involution, -(-(idx+1)+1)==idx.  Local indices are provided when idxSection == section, otherwise global
7321:   indices (with the above semantics) are implied.

7323: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexRestoreClosureIndices()`, `DMPlexVecGetClosure()`, `DMPlexMatSetClosure()`, `DMGetLocalSection()`,
7324:           `PetscSection`, `DMGetGlobalSection()`
7325: @*/
7326: PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscBool useClPerm, PetscInt *numIndices, PetscInt *indices[], PetscInt outOffsets[], PetscScalar *values[])
7327: {
7328:   /* Closure ordering */
7329:   PetscSection    clSection;
7330:   IS              clPoints;
7331:   const PetscInt *clp;
7332:   PetscInt       *points;
7333:   const PetscInt *clperm = NULL;
7334:   /* Dof permutation and sign flips */
7335:   const PetscInt    **perms[32] = {NULL};
7336:   const PetscScalar **flips[32] = {NULL};
7337:   PetscScalar        *valCopy   = NULL;
7338:   /* Hanging node constraints */
7339:   PetscInt    *pointsC = NULL;
7340:   PetscScalar *valuesC = NULL;
7341:   PetscInt     NclC, NiC;

7343:   PetscInt *idx;
7344:   PetscInt  Nf, Ncl, Ni = 0, offsets[32], p, f;
7345:   PetscBool isLocal = (section == idxSection) ? PETSC_TRUE : PETSC_FALSE;

7355:   PetscSectionGetNumFields(section, &Nf);
7357:   PetscArrayzero(offsets, 32);
7358:   /* 1) Get points in closure */
7359:   DMPlexGetCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);
7360:   if (useClPerm) {
7361:     PetscInt depth, clsize;
7362:     DMPlexGetPointDepth(dm, point, &depth);
7363:     for (clsize = 0, p = 0; p < Ncl; p++) {
7364:       PetscInt dof;
7365:       PetscSectionGetDof(section, points[2 * p], &dof);
7366:       clsize += dof;
7367:     }
7368:     PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject)dm, depth, clsize, &clperm);
7369:   }
7370:   /* 2) Get number of indices on these points and field offsets from section */
7371:   for (p = 0; p < Ncl * 2; p += 2) {
7372:     PetscInt dof, fdof;

7374:     PetscSectionGetDof(section, points[p], &dof);
7375:     for (f = 0; f < Nf; ++f) {
7376:       PetscSectionGetFieldDof(section, points[p], f, &fdof);
7377:       offsets[f + 1] += fdof;
7378:     }
7379:     Ni += dof;
7380:   }
7381:   for (f = 1; f < Nf; ++f) offsets[f + 1] += offsets[f];
7383:   /* 3) Get symmetries and sign flips. Apply sign flips to values if passed in (only works for square values matrix) */
7384:   for (f = 0; f < PetscMax(1, Nf); ++f) {
7385:     if (Nf) PetscSectionGetFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);
7386:     else PetscSectionGetPointSyms(section, Ncl, points, &perms[f], &flips[f]);
7387:     /* may need to apply sign changes to the element matrix */
7388:     if (values && flips[f]) {
7389:       PetscInt foffset = offsets[f];

7391:       for (p = 0; p < Ncl; ++p) {
7392:         PetscInt           pnt  = points[2 * p], fdof;
7393:         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;

7395:         if (!Nf) PetscSectionGetDof(section, pnt, &fdof);
7396:         else PetscSectionGetFieldDof(section, pnt, f, &fdof);
7397:         if (flip) {
7398:           PetscInt i, j, k;

7400:           if (!valCopy) {
7401:             DMGetWorkArray(dm, Ni * Ni, MPIU_SCALAR, &valCopy);
7402:             for (j = 0; j < Ni * Ni; ++j) valCopy[j] = (*values)[j];
7403:             *values = valCopy;
7404:           }
7405:           for (i = 0; i < fdof; ++i) {
7406:             PetscScalar fval = flip[i];

7408:             for (k = 0; k < Ni; ++k) {
7409:               valCopy[Ni * (foffset + i) + k] *= fval;
7410:               valCopy[Ni * k + (foffset + i)] *= fval;
7411:             }
7412:           }
7413:         }
7414:         foffset += fdof;
7415:       }
7416:     }
7417:   }
7418:   /* 4) Apply hanging node constraints. Get new symmetries and replace all storage with constrained storage */
7419:   DMPlexAnchorsModifyMat(dm, section, Ncl, Ni, points, perms, values ? *values : NULL, &NclC, &NiC, &pointsC, values ? &valuesC : NULL, offsets, PETSC_TRUE);
7420:   if (NclC) {
7421:     if (valCopy) DMRestoreWorkArray(dm, Ni * Ni, MPIU_SCALAR, &valCopy);
7422:     for (f = 0; f < PetscMax(1, Nf); ++f) {
7423:       if (Nf) PetscSectionRestoreFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);
7424:       else PetscSectionRestorePointSyms(section, Ncl, points, &perms[f], &flips[f]);
7425:     }
7426:     for (f = 0; f < PetscMax(1, Nf); ++f) {
7427:       if (Nf) PetscSectionGetFieldPointSyms(section, f, NclC, pointsC, &perms[f], &flips[f]);
7428:       else PetscSectionGetPointSyms(section, NclC, pointsC, &perms[f], &flips[f]);
7429:     }
7430:     DMPlexRestoreCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);
7431:     Ncl    = NclC;
7432:     Ni     = NiC;
7433:     points = pointsC;
7434:     if (values) *values = valuesC;
7435:   }
7436:   /* 5) Calculate indices */
7437:   DMGetWorkArray(dm, Ni, MPIU_INT, &idx);
7438:   if (Nf) {
7439:     PetscInt  idxOff;
7440:     PetscBool useFieldOffsets;

7442:     if (outOffsets) {
7443:       for (f = 0; f <= Nf; f++) outOffsets[f] = offsets[f];
7444:     }
7445:     PetscSectionGetUseFieldOffsets(idxSection, &useFieldOffsets);
7446:     if (useFieldOffsets) {
7447:       for (p = 0; p < Ncl; ++p) {
7448:         const PetscInt pnt = points[p * 2];

7450:         DMPlexGetIndicesPointFieldsSplit_Internal(section, idxSection, pnt, offsets, perms, p, clperm, idx);
7451:       }
7452:     } else {
7453:       for (p = 0; p < Ncl; ++p) {
7454:         const PetscInt pnt = points[p * 2];

7456:         PetscSectionGetOffset(idxSection, pnt, &idxOff);
7457:         /* Note that we pass a local section even though we're using global offsets.  This is because global sections do
7458:          * not (at the time of this writing) have fields set. They probably should, in which case we would pass the
7459:          * global section. */
7460:         DMPlexGetIndicesPointFields_Internal(section, isLocal, pnt, idxOff < 0 ? -(idxOff + 1) : idxOff, offsets, PETSC_FALSE, perms, p, clperm, idx);
7461:       }
7462:     }
7463:   } else {
7464:     PetscInt off = 0, idxOff;

7466:     for (p = 0; p < Ncl; ++p) {
7467:       const PetscInt  pnt  = points[p * 2];
7468:       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;

7470:       PetscSectionGetOffset(idxSection, pnt, &idxOff);
7471:       /* Note that we pass a local section even though we're using global offsets.  This is because global sections do
7472:        * not (at the time of this writing) have fields set. They probably should, in which case we would pass the global section. */
7473:       DMPlexGetIndicesPoint_Internal(section, isLocal, pnt, idxOff < 0 ? -(idxOff + 1) : idxOff, &off, PETSC_FALSE, perm, clperm, idx);
7474:     }
7475:   }
7476:   /* 6) Cleanup */
7477:   for (f = 0; f < PetscMax(1, Nf); ++f) {
7478:     if (Nf) PetscSectionRestoreFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);
7479:     else PetscSectionRestorePointSyms(section, Ncl, points, &perms[f], &flips[f]);
7480:   }
7481:   if (NclC) {
7482:     DMRestoreWorkArray(dm, NclC * 2, MPIU_INT, &pointsC);
7483:   } else {
7484:     DMPlexRestoreCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);
7485:   }

7487:   if (numIndices) *numIndices = Ni;
7488:   if (indices) *indices = idx;
7489:   return 0;
7490: }

7492: /*@C
7493:   DMPlexRestoreClosureIndices - Restores the global dof indices associated with the closure of the given point within the provided sections.

7495:   Not collective

7497:   Input Parameters:
7498: + dm         - The `DM`
7499: . section    - The `PetscSection` describing the points (a local section)
7500: . idxSection - The `PetscSection` from which to obtain indices (may be local or global)
7501: . point      - The point defining the closure
7502: - useClPerm  - Use the closure point permutation if available

7504:   Output Parameters:
7505: + numIndices - The number of dof indices in the closure of point with the input sections
7506: . indices    - The dof indices
7507: . outOffsets - Array to write the field offsets into, or NULL
7508: - values     - The input values, which may be modified if sign flips are induced by the point symmetries, or NULL

7510:   Level: advanced

7512:   Notes:
7513:   If values were modified, the user is responsible for calling `DMRestoreWorkArray`(dm, 0, `MPIU_SCALAR`, &values).

7515:   If idxSection is global, any constrained dofs (see `DMAddBoundary()`, for example) will get negative indices.  The value
7516:   of those indices is not significant.  If idxSection is local, the constrained dofs will yield the involution -(idx+1)
7517:   of their index in a local vector.  A caller who does not wish to distinguish those points may recover the nonnegative
7518:   indices via involution, -(-(idx+1)+1)==idx.  Local indices are provided when idxSection == section, otherwise global
7519:   indices (with the above semantics) are implied.

7521: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetClosureIndices()`, `DMPlexVecGetClosure()`, `DMPlexMatSetClosure()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
7522: @*/
7523: PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscBool useClPerm, PetscInt *numIndices, PetscInt *indices[], PetscInt outOffsets[], PetscScalar *values[])
7524: {
7527:   DMRestoreWorkArray(dm, 0, MPIU_INT, indices);
7528:   return 0;
7529: }

7531: /*@C
7532:   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'

7534:   Not collective

7536:   Input Parameters:
7537: + dm - The `DM`
7538: . section - The section describing the layout in v, or NULL to use the default section
7539: . globalSection - The section describing the layout in v, or NULL to use the default global section
7540: . A - The matrix
7541: . point - The point in the `DM`
7542: . values - The array of values
7543: - mode - The insert mode, where `INSERT_ALL_VALUES` and `ADD_ALL_VALUES` also overwrite boundary conditions

7545:   Level: intermediate

7547: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexMatSetClosureGeneral()`, `DMPlexVecGetClosure()`, `DMPlexVecSetClosure()`
7548: @*/
7549: PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
7550: {
7551:   DM_Plex           *mesh = (DM_Plex *)dm->data;
7552:   PetscInt          *indices;
7553:   PetscInt           numIndices;
7554:   const PetscScalar *valuesOrig = values;
7555:   PetscErrorCode     ierr;

7558:   if (!section) DMGetLocalSection(dm, &section);
7560:   if (!globalSection) DMGetGlobalSection(dm, &globalSection);

7564:   DMPlexGetClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **)&values);

7566:   if (mesh->printSetValues) DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);
7567:   /* TODO: fix this code to not use error codes as handle-able exceptions! */
7568:   MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
7569:   if (ierr) {
7570:     PetscMPIInt rank;

7572:     MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);
7573:     (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);
7574:     DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);
7575:     DMPlexRestoreClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **)&values);
7576:     if (values != valuesOrig) DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values);
7577:     SETERRQ(PetscObjectComm((PetscObject)dm), ierr, "Not possible to set matrix values");
7578:   }
7579:   if (mesh->printFEM > 1) {
7580:     PetscInt i;
7581:     PetscPrintf(PETSC_COMM_SELF, "  Indices:");
7582:     for (i = 0; i < numIndices; ++i) PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, indices[i]);
7583:     PetscPrintf(PETSC_COMM_SELF, "\n");
7584:   }

7586:   DMPlexRestoreClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **)&values);
7587:   if (values != valuesOrig) DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values);
7588:   return 0;
7589: }

7591: /*@C
7592:   DMPlexMatSetClosure - Set an array of the values on the closure of 'point' using a different row and column section

7594:   Not collective

7596:   Input Parameters:
7597: + dmRow - The `DM` for the row fields
7598: . sectionRow - The section describing the layout, or NULL to use the default section in dmRow
7599: . globalSectionRow - The section describing the layout, or NULL to use the default global section in dmRow
7600: . dmCol - The `DM` for the column fields
7601: . sectionCol - The section describing the layout, or NULL to use the default section in dmCol
7602: . globalSectionCol - The section describing the layout, or NULL to use the default global section in dmCol
7603: . A - The matrix
7604: . point - The point in the `DM`
7605: . values - The array of values
7606: - mode - The insert mode, where `INSERT_ALL_VALUES` and `ADD_ALL_VALUES` also overwrite boundary conditions

7608:   Level: intermediate

7610: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexMatSetClosure()`, `DMPlexVecGetClosure()`, `DMPlexVecSetClosure()`
7611: @*/
7612: PetscErrorCode DMPlexMatSetClosureGeneral(DM dmRow, PetscSection sectionRow, PetscSection globalSectionRow, DM dmCol, PetscSection sectionCol, PetscSection globalSectionCol, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
7613: {
7614:   DM_Plex           *mesh = (DM_Plex *)dmRow->data;
7615:   PetscInt          *indicesRow, *indicesCol;
7616:   PetscInt           numIndicesRow, numIndicesCol;
7617:   const PetscScalar *valuesOrig = values;
7618:   PetscErrorCode     ierr;

7621:   if (!sectionRow) DMGetLocalSection(dmRow, &sectionRow);
7623:   if (!globalSectionRow) DMGetGlobalSection(dmRow, &globalSectionRow);
7626:   if (!sectionCol) DMGetLocalSection(dmCol, &sectionCol);
7628:   if (!globalSectionCol) DMGetGlobalSection(dmCol, &globalSectionCol);

7632:   DMPlexGetClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **)&values);
7633:   DMPlexGetClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesCol, NULL, (PetscScalar **)&values);

7635:   if (mesh->printSetValues) DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values);
7636:   /* TODO: fix this code to not use error codes as handle-able exceptions! */
7637:   MatSetValues(A, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values, mode);
7638:   if (ierr) {
7639:     PetscMPIInt rank;

7641:     MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);
7642:     (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);
7643:     DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values);
7644:     DMPlexRestoreClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **)&values);
7645:     DMPlexRestoreClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesRow, NULL, (PetscScalar **)&values);
7646:     if (values != valuesOrig) DMRestoreWorkArray(dmRow, 0, MPIU_SCALAR, &values);
7647:   }

7649:   DMPlexRestoreClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **)&values);
7650:   DMPlexRestoreClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesCol, NULL, (PetscScalar **)&values);
7651:   if (values != valuesOrig) DMRestoreWorkArray(dmRow, 0, MPIU_SCALAR, &values);
7652:   return 0;
7653: }

7655: PetscErrorCode DMPlexMatSetClosureRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
7656: {
7657:   DM_Plex        *mesh    = (DM_Plex *)dmf->data;
7658:   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
7659:   PetscInt       *cpoints = NULL;
7660:   PetscInt       *findices, *cindices;
7661:   const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
7662:   PetscInt        foffsets[32], coffsets[32];
7663:   DMPolytopeType  ct;
7664:   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
7665:   PetscErrorCode  ierr;

7669:   if (!fsection) DMGetLocalSection(dmf, &fsection);
7671:   if (!csection) DMGetLocalSection(dmc, &csection);
7673:   if (!globalFSection) DMGetGlobalSection(dmf, &globalFSection);
7675:   if (!globalCSection) DMGetGlobalSection(dmc, &globalCSection);
7678:   PetscSectionGetNumFields(fsection, &numFields);
7680:   PetscArrayzero(foffsets, 32);
7681:   PetscArrayzero(coffsets, 32);
7682:   /* Column indices */
7683:   DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);
7684:   maxFPoints = numCPoints;
7685:   /* Compress out points not in the section */
7686:   /*   TODO: Squeeze out points with 0 dof as well */
7687:   PetscSectionGetChart(csection, &pStart, &pEnd);
7688:   for (p = 0, q = 0; p < numCPoints * 2; p += 2) {
7689:     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
7690:       cpoints[q * 2]     = cpoints[p];
7691:       cpoints[q * 2 + 1] = cpoints[p + 1];
7692:       ++q;
7693:     }
7694:   }
7695:   numCPoints = q;
7696:   for (p = 0, numCIndices = 0; p < numCPoints * 2; p += 2) {
7697:     PetscInt fdof;

7699:     PetscSectionGetDof(csection, cpoints[p], &dof);
7700:     if (!dof) continue;
7701:     for (f = 0; f < numFields; ++f) {
7702:       PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);
7703:       coffsets[f + 1] += fdof;
7704:     }
7705:     numCIndices += dof;
7706:   }
7707:   for (f = 1; f < numFields; ++f) coffsets[f + 1] += coffsets[f];
7708:   /* Row indices */
7709:   DMPlexGetCellType(dmc, point, &ct);
7710:   {
7711:     DMPlexTransform tr;
7712:     DMPolytopeType *rct;
7713:     PetscInt       *rsize, *rcone, *rornt, Nt;

7715:     DMPlexTransformCreate(PETSC_COMM_SELF, &tr);
7716:     DMPlexTransformSetType(tr, DMPLEXREFINEREGULAR);
7717:     DMPlexTransformCellTransform(tr, ct, point, NULL, &Nt, &rct, &rsize, &rcone, &rornt);
7718:     numSubcells = rsize[Nt - 1];
7719:     DMPlexTransformDestroy(&tr);
7720:   }
7721:   DMGetWorkArray(dmf, maxFPoints * 2 * numSubcells, MPIU_INT, &ftotpoints);
7722:   for (r = 0, q = 0; r < numSubcells; ++r) {
7723:     /* TODO Map from coarse to fine cells */
7724:     DMPlexGetTransitiveClosure(dmf, point * numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);
7725:     /* Compress out points not in the section */
7726:     PetscSectionGetChart(fsection, &pStart, &pEnd);
7727:     for (p = 0; p < numFPoints * 2; p += 2) {
7728:       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
7729:         PetscSectionGetDof(fsection, fpoints[p], &dof);
7730:         if (!dof) continue;
7731:         for (s = 0; s < q; ++s)
7732:           if (fpoints[p] == ftotpoints[s * 2]) break;
7733:         if (s < q) continue;
7734:         ftotpoints[q * 2]     = fpoints[p];
7735:         ftotpoints[q * 2 + 1] = fpoints[p + 1];
7736:         ++q;
7737:       }
7738:     }
7739:     DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);
7740:   }
7741:   numFPoints = q;
7742:   for (p = 0, numFIndices = 0; p < numFPoints * 2; p += 2) {
7743:     PetscInt fdof;

7745:     PetscSectionGetDof(fsection, ftotpoints[p], &dof);
7746:     if (!dof) continue;
7747:     for (f = 0; f < numFields; ++f) {
7748:       PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);
7749:       foffsets[f + 1] += fdof;
7750:     }
7751:     numFIndices += dof;
7752:   }
7753:   for (f = 1; f < numFields; ++f) foffsets[f + 1] += foffsets[f];

7757:   DMGetWorkArray(dmf, numFIndices, MPIU_INT, &findices);
7758:   DMGetWorkArray(dmc, numCIndices, MPIU_INT, &cindices);
7759:   if (numFields) {
7760:     const PetscInt **permsF[32] = {NULL};
7761:     const PetscInt **permsC[32] = {NULL};

7763:     for (f = 0; f < numFields; f++) {
7764:       PetscSectionGetFieldPointSyms(fsection, f, numFPoints, ftotpoints, &permsF[f], NULL);
7765:       PetscSectionGetFieldPointSyms(csection, f, numCPoints, cpoints, &permsC[f], NULL);
7766:     }
7767:     for (p = 0; p < numFPoints; p++) {
7768:       PetscSectionGetOffset(globalFSection, ftotpoints[2 * p], &globalOff);
7769:       DMPlexGetIndicesPointFields_Internal(fsection, PETSC_FALSE, ftotpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);
7770:     }
7771:     for (p = 0; p < numCPoints; p++) {
7772:       PetscSectionGetOffset(globalCSection, cpoints[2 * p], &globalOff);
7773:       DMPlexGetIndicesPointFields_Internal(csection, PETSC_FALSE, cpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);
7774:     }
7775:     for (f = 0; f < numFields; f++) {
7776:       PetscSectionRestoreFieldPointSyms(fsection, f, numFPoints, ftotpoints, &permsF[f], NULL);
7777:       PetscSectionRestoreFieldPointSyms(csection, f, numCPoints, cpoints, &permsC[f], NULL);
7778:     }
7779:   } else {
7780:     const PetscInt **permsF = NULL;
7781:     const PetscInt **permsC = NULL;

7783:     PetscSectionGetPointSyms(fsection, numFPoints, ftotpoints, &permsF, NULL);
7784:     PetscSectionGetPointSyms(csection, numCPoints, cpoints, &permsC, NULL);
7785:     for (p = 0, off = 0; p < numFPoints; p++) {
7786:       const PetscInt *perm = permsF ? permsF[p] : NULL;

7788:       PetscSectionGetOffset(globalFSection, ftotpoints[2 * p], &globalOff);
7789:       DMPlexGetIndicesPoint_Internal(fsection, PETSC_FALSE, ftotpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);
7790:     }
7791:     for (p = 0, off = 0; p < numCPoints; p++) {
7792:       const PetscInt *perm = permsC ? permsC[p] : NULL;

7794:       PetscSectionGetOffset(globalCSection, cpoints[2 * p], &globalOff);
7795:       DMPlexGetIndicesPoint_Internal(csection, PETSC_FALSE, cpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);
7796:     }
7797:     PetscSectionRestorePointSyms(fsection, numFPoints, ftotpoints, &permsF, NULL);
7798:     PetscSectionRestorePointSyms(csection, numCPoints, cpoints, &permsC, NULL);
7799:   }
7800:   if (mesh->printSetValues) DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);
7801:   /* TODO: flips */
7802:   /* TODO: fix this code to not use error codes as handle-able exceptions! */
7803:   MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
7804:   if (ierr) {
7805:     PetscMPIInt rank;

7807:     MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);
7808:     (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);
7809:     DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);
7810:     DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);
7811:     DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);
7812:   }
7813:   DMRestoreWorkArray(dmf, numCPoints * 2 * 4, MPIU_INT, &ftotpoints);
7814:   DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);
7815:   DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);
7816:   DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);
7817:   return 0;
7818: }

7820: PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
7821: {
7822:   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
7823:   PetscInt       *cpoints = NULL;
7824:   PetscInt        foffsets[32], coffsets[32];
7825:   const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
7826:   DMPolytopeType  ct;
7827:   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;

7831:   if (!fsection) DMGetLocalSection(dmf, &fsection);
7833:   if (!csection) DMGetLocalSection(dmc, &csection);
7835:   if (!globalFSection) DMGetGlobalSection(dmf, &globalFSection);
7837:   if (!globalCSection) DMGetGlobalSection(dmc, &globalCSection);
7839:   PetscSectionGetNumFields(fsection, &numFields);
7841:   PetscArrayzero(foffsets, 32);
7842:   PetscArrayzero(coffsets, 32);
7843:   /* Column indices */
7844:   DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);
7845:   maxFPoints = numCPoints;
7846:   /* Compress out points not in the section */
7847:   /*   TODO: Squeeze out points with 0 dof as well */
7848:   PetscSectionGetChart(csection, &pStart, &pEnd);
7849:   for (p = 0, q = 0; p < numCPoints * 2; p += 2) {
7850:     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
7851:       cpoints[q * 2]     = cpoints[p];
7852:       cpoints[q * 2 + 1] = cpoints[p + 1];
7853:       ++q;
7854:     }
7855:   }
7856:   numCPoints = q;
7857:   for (p = 0, numCIndices = 0; p < numCPoints * 2; p += 2) {
7858:     PetscInt fdof;

7860:     PetscSectionGetDof(csection, cpoints[p], &dof);
7861:     if (!dof) continue;
7862:     for (f = 0; f < numFields; ++f) {
7863:       PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);
7864:       coffsets[f + 1] += fdof;
7865:     }
7866:     numCIndices += dof;
7867:   }
7868:   for (f = 1; f < numFields; ++f) coffsets[f + 1] += coffsets[f];
7869:   /* Row indices */
7870:   DMPlexGetCellType(dmc, point, &ct);
7871:   {
7872:     DMPlexTransform tr;
7873:     DMPolytopeType *rct;
7874:     PetscInt       *rsize, *rcone, *rornt, Nt;

7876:     DMPlexTransformCreate(PETSC_COMM_SELF, &tr);
7877:     DMPlexTransformSetType(tr, DMPLEXREFINEREGULAR);
7878:     DMPlexTransformCellTransform(tr, ct, point, NULL, &Nt, &rct, &rsize, &rcone, &rornt);
7879:     numSubcells = rsize[Nt - 1];
7880:     DMPlexTransformDestroy(&tr);
7881:   }
7882:   DMGetWorkArray(dmf, maxFPoints * 2 * numSubcells, MPIU_INT, &ftotpoints);
7883:   for (r = 0, q = 0; r < numSubcells; ++r) {
7884:     /* TODO Map from coarse to fine cells */
7885:     DMPlexGetTransitiveClosure(dmf, point * numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);
7886:     /* Compress out points not in the section */
7887:     PetscSectionGetChart(fsection, &pStart, &pEnd);
7888:     for (p = 0; p < numFPoints * 2; p += 2) {
7889:       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
7890:         PetscSectionGetDof(fsection, fpoints[p], &dof);
7891:         if (!dof) continue;
7892:         for (s = 0; s < q; ++s)
7893:           if (fpoints[p] == ftotpoints[s * 2]) break;
7894:         if (s < q) continue;
7895:         ftotpoints[q * 2]     = fpoints[p];
7896:         ftotpoints[q * 2 + 1] = fpoints[p + 1];
7897:         ++q;
7898:       }
7899:     }
7900:     DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);
7901:   }
7902:   numFPoints = q;
7903:   for (p = 0, numFIndices = 0; p < numFPoints * 2; p += 2) {
7904:     PetscInt fdof;

7906:     PetscSectionGetDof(fsection, ftotpoints[p], &dof);
7907:     if (!dof) continue;
7908:     for (f = 0; f < numFields; ++f) {
7909:       PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);
7910:       foffsets[f + 1] += fdof;
7911:     }
7912:     numFIndices += dof;
7913:   }
7914:   for (f = 1; f < numFields; ++f) foffsets[f + 1] += foffsets[f];

7918:   if (numFields) {
7919:     const PetscInt **permsF[32] = {NULL};
7920:     const PetscInt **permsC[32] = {NULL};

7922:     for (f = 0; f < numFields; f++) {
7923:       PetscSectionGetFieldPointSyms(fsection, f, numFPoints, ftotpoints, &permsF[f], NULL);
7924:       PetscSectionGetFieldPointSyms(csection, f, numCPoints, cpoints, &permsC[f], NULL);
7925:     }
7926:     for (p = 0; p < numFPoints; p++) {
7927:       PetscSectionGetOffset(globalFSection, ftotpoints[2 * p], &globalOff);
7928:       DMPlexGetIndicesPointFields_Internal(fsection, PETSC_FALSE, ftotpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);
7929:     }
7930:     for (p = 0; p < numCPoints; p++) {
7931:       PetscSectionGetOffset(globalCSection, cpoints[2 * p], &globalOff);
7932:       DMPlexGetIndicesPointFields_Internal(csection, PETSC_FALSE, cpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);
7933:     }
7934:     for (f = 0; f < numFields; f++) {
7935:       PetscSectionRestoreFieldPointSyms(fsection, f, numFPoints, ftotpoints, &permsF[f], NULL);
7936:       PetscSectionRestoreFieldPointSyms(csection, f, numCPoints, cpoints, &permsC[f], NULL);
7937:     }
7938:   } else {
7939:     const PetscInt **permsF = NULL;
7940:     const PetscInt **permsC = NULL;

7942:     PetscSectionGetPointSyms(fsection, numFPoints, ftotpoints, &permsF, NULL);
7943:     PetscSectionGetPointSyms(csection, numCPoints, cpoints, &permsC, NULL);
7944:     for (p = 0, off = 0; p < numFPoints; p++) {
7945:       const PetscInt *perm = permsF ? permsF[p] : NULL;

7947:       PetscSectionGetOffset(globalFSection, ftotpoints[2 * p], &globalOff);
7948:       DMPlexGetIndicesPoint_Internal(fsection, PETSC_FALSE, ftotpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);
7949:     }
7950:     for (p = 0, off = 0; p < numCPoints; p++) {
7951:       const PetscInt *perm = permsC ? permsC[p] : NULL;

7953:       PetscSectionGetOffset(globalCSection, cpoints[2 * p], &globalOff);
7954:       DMPlexGetIndicesPoint_Internal(csection, PETSC_FALSE, cpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);
7955:     }
7956:     PetscSectionRestorePointSyms(fsection, numFPoints, ftotpoints, &permsF, NULL);
7957:     PetscSectionRestorePointSyms(csection, numCPoints, cpoints, &permsC, NULL);
7958:   }
7959:   DMRestoreWorkArray(dmf, numCPoints * 2 * 4, MPIU_INT, &ftotpoints);
7960:   DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);
7961:   return 0;
7962: }

7964: /*@C
7965:   DMPlexGetVTKCellHeight - Returns the height in the DAG used to determine which points are cells (normally 0)

7967:   Input Parameter:
7968: . dm   - The `DMPLEX` object

7970:   Output Parameter:
7971: . cellHeight - The height of a cell

7973:   Level: developer

7975: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`,  `DMPlexSetVTKCellHeight()`
7976: @*/
7977: PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
7978: {
7979:   DM_Plex *mesh = (DM_Plex *)dm->data;

7983:   *cellHeight = mesh->vtkCellHeight;
7984:   return 0;
7985: }

7987: /*@C
7988:   DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)

7990:   Input Parameters:
7991: + dm   - The `DMPLEX` object
7992: - cellHeight - The height of a cell

7994:   Level: developer

7996: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetVTKCellHeight()`
7997: @*/
7998: PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
7999: {
8000:   DM_Plex *mesh = (DM_Plex *)dm->data;

8003:   mesh->vtkCellHeight = cellHeight;
8004:   return 0;
8005: }

8007: /*@
8008:   DMPlexGetGhostCellStratum - Get the range of cells which are used to enforce FV boundary conditions

8010:   Input Parameter:
8011: . dm - The `DMPLEX` object

8013:   Output Parameters:
8014: + gcStart - The first ghost cell, or NULL
8015: - gcEnd   - The upper bound on ghost cells, or NULL

8017:   Level: advanced

8019: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexConstructGhostCells()`, `DMPlexGetGhostCellStratum()`
8020: @*/
8021: PetscErrorCode DMPlexGetGhostCellStratum(DM dm, PetscInt *gcStart, PetscInt *gcEnd)
8022: {
8023:   DMLabel ctLabel;

8026:   DMPlexGetCellTypeLabel(dm, &ctLabel);
8027:   DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_FV_GHOST, gcStart, gcEnd);
8028:   // Reset label for fast lookup
8029:   DMLabelMakeAllInvalid_Internal(ctLabel);
8030:   return 0;
8031: }

8033: PetscErrorCode DMPlexCreateNumbering_Plex(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
8034: {
8035:   PetscSection section, globalSection;
8036:   PetscInt    *numbers, p;

8038:   if (PetscDefined(USE_DEBUG)) DMPlexCheckPointSF(dm, sf, PETSC_TRUE);
8039:   PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);
8040:   PetscSectionSetChart(section, pStart, pEnd);
8041:   for (p = pStart; p < pEnd; ++p) PetscSectionSetDof(section, p, 1);
8042:   PetscSectionSetUp(section);
8043:   PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);
8044:   PetscMalloc1(pEnd - pStart, &numbers);
8045:   for (p = pStart; p < pEnd; ++p) {
8046:     PetscSectionGetOffset(globalSection, p, &numbers[p - pStart]);
8047:     if (numbers[p - pStart] < 0) numbers[p - pStart] -= shift;
8048:     else numbers[p - pStart] += shift;
8049:   }
8050:   ISCreateGeneral(PetscObjectComm((PetscObject)dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);
8051:   if (globalSize) {
8052:     PetscLayout layout;
8053:     PetscSectionGetPointLayout(PetscObjectComm((PetscObject)dm), globalSection, &layout);
8054:     PetscLayoutGetSize(layout, globalSize);
8055:     PetscLayoutDestroy(&layout);
8056:   }
8057:   PetscSectionDestroy(&section);
8058:   PetscSectionDestroy(&globalSection);
8059:   return 0;
8060: }

8062: PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
8063: {
8064:   PetscInt cellHeight, cStart, cEnd;

8066:   DMPlexGetVTKCellHeight(dm, &cellHeight);
8067:   if (includeHybrid) DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);
8068:   else DMPlexGetSimplexOrBoxCells(dm, cellHeight, &cStart, &cEnd);
8069:   DMPlexCreateNumbering_Plex(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);
8070:   return 0;
8071: }

8073: /*@
8074:   DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process

8076:   Input Parameter:
8077: . dm   - The `DMPLEX` object

8079:   Output Parameter:
8080: . globalCellNumbers - Global cell numbers for all cells on this process

8082:   Level: developer

8084: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetVertexNumbering()`
8085: @*/
8086: PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
8087: {
8088:   DM_Plex *mesh = (DM_Plex *)dm->data;

8091:   if (!mesh->globalCellNumbers) DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);
8092:   *globalCellNumbers = mesh->globalCellNumbers;
8093:   return 0;
8094: }

8096: PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
8097: {
8098:   PetscInt vStart, vEnd;

8101:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
8102:   DMPlexCreateNumbering_Plex(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);
8103:   return 0;
8104: }

8106: /*@
8107:   DMPlexGetVertexNumbering - Get a global vertex numbering for all vertices on this process

8109:   Input Parameter:
8110: . dm   - The `DMPLEX` object

8112:   Output Parameter:
8113: . globalVertexNumbers - Global vertex numbers for all vertices on this process

8115:   Level: developer

8117: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellNumbering()`
8118: @*/
8119: PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
8120: {
8121:   DM_Plex *mesh = (DM_Plex *)dm->data;

8124:   if (!mesh->globalVertexNumbers) DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);
8125:   *globalVertexNumbers = mesh->globalVertexNumbers;
8126:   return 0;
8127: }

8129: /*@
8130:   DMPlexCreatePointNumbering - Create a global numbering for all points.

8132:   Collective on dm

8134:   Input Parameter:
8135: . dm   - The `DMPLEX` object

8137:   Output Parameter:
8138: . globalPointNumbers - Global numbers for all points on this process

8140:   Level: developer

8142:   Notes:
8143:   The point numbering `IS` is parallel, with local portion indexed by local points (see `DMGetLocalSection()`). The global
8144:   points are taken as stratified, with each MPI rank owning a contiguous subset of each stratum. In the IS, owned points
8145:   will have their non-negative value while points owned by different ranks will be involuted -(idx+1). As an example,
8146:   consider a parallel mesh in which the first two elements and first two vertices are owned by rank 0.

8148:   The partitioned mesh is
8149: ```
8150:  (2)--0--(3)--1--(4)    (1)--0--(2)
8151: ```
8152:   and its global numbering is
8153: ```
8154:   (3)--0--(4)--1--(5)--2--(6)
8155: ```
8156:   Then the global numbering is provided as
8157: ```
8158: [0] Number of indices in set 5
8159: [0] 0 0
8160: [0] 1 1
8161: [0] 2 3
8162: [0] 3 4
8163: [0] 4 -6
8164: [1] Number of indices in set 3
8165: [1] 0 2
8166: [1] 1 5
8167: [1] 2 6
8168: ```

8170: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellNumbering()`
8171: @*/
8172: PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
8173: {
8174:   IS        nums[4];
8175:   PetscInt  depths[4], gdepths[4], starts[4];
8176:   PetscInt  depth, d, shift = 0;
8177:   PetscBool empty = PETSC_FALSE;

8180:   DMPlexGetDepth(dm, &depth);
8181:   // For unstratified meshes use dim instead of depth
8182:   if (depth < 0) DMGetDimension(dm, &depth);
8183:   // If any stratum is empty, we must mark all empty
8184:   for (d = 0; d <= depth; ++d) {
8185:     PetscInt end;

8187:     depths[d] = depth - d;
8188:     DMPlexGetDepthStratum(dm, depths[d], &starts[d], &end);
8189:     if (!(starts[d] - end)) empty = PETSC_TRUE;
8190:   }
8191:   if (empty)
8192:     for (d = 0; d <= depth; ++d) {
8193:       depths[d] = -1;
8194:       starts[d] = -1;
8195:     }
8196:   else PetscSortIntWithArray(depth + 1, starts, depths);
8197:   MPIU_Allreduce(depths, gdepths, depth + 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));
8199:   // Note here that 'shift' is collective, so that the numbering is stratified by depth
8200:   for (d = 0; d <= depth; ++d) {
8201:     PetscInt pStart, pEnd, gsize;

8203:     DMPlexGetDepthStratum(dm, gdepths[d], &pStart, &pEnd);
8204:     DMPlexCreateNumbering_Plex(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);
8205:     shift += gsize;
8206:   }
8207:   ISConcatenate(PetscObjectComm((PetscObject)dm), depth + 1, nums, globalPointNumbers);
8208:   for (d = 0; d <= depth; ++d) ISDestroy(&nums[d]);
8209:   return 0;
8210: }

8212: /*@
8213:   DMPlexCreateRankField - Create a cell field whose value is the rank of the owner

8215:   Input Parameter:
8216: . dm - The `DMPLEX` object

8218:   Output Parameter:
8219: . ranks - The rank field

8221:   Options Database Key:
8222: . -dm_partition_view - Adds the rank field into the DM output from -dm_view using the same viewer

8224:   Level: intermediate

8226: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMView()`
8227: @*/
8228: PetscErrorCode DMPlexCreateRankField(DM dm, Vec *ranks)
8229: {
8230:   DM             rdm;
8231:   PetscFE        fe;
8232:   PetscScalar   *r;
8233:   PetscMPIInt    rank;
8234:   DMPolytopeType ct;
8235:   PetscInt       dim, cStart, cEnd, c;
8236:   PetscBool      simplex;

8241:   MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
8242:   DMClone(dm, &rdm);
8243:   DMGetDimension(rdm, &dim);
8244:   DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);
8245:   DMPlexGetCellType(dm, cStart, &ct);
8246:   simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
8247:   PetscFECreateDefault(PETSC_COMM_SELF, dim, 1, simplex, "PETSc___rank_", -1, &fe);
8248:   PetscObjectSetName((PetscObject)fe, "rank");
8249:   DMSetField(rdm, 0, NULL, (PetscObject)fe);
8250:   PetscFEDestroy(&fe);
8251:   DMCreateDS(rdm);
8252:   DMCreateGlobalVector(rdm, ranks);
8253:   PetscObjectSetName((PetscObject)*ranks, "partition");
8254:   VecGetArray(*ranks, &r);
8255:   for (c = cStart; c < cEnd; ++c) {
8256:     PetscScalar *lr;

8258:     DMPlexPointGlobalRef(rdm, c, r, &lr);
8259:     if (lr) *lr = rank;
8260:   }
8261:   VecRestoreArray(*ranks, &r);
8262:   DMDestroy(&rdm);
8263:   return 0;
8264: }

8266: /*@
8267:   DMPlexCreateLabelField - Create a cell field whose value is the label value for that cell

8269:   Input Parameters:
8270: + dm    - The DMPlex
8271: - label - The DMLabel

8273:   Output Parameter:
8274: . val - The label value field

8276:   Options Database Keys:
8277: . -dm_label_view - Adds the label value field into the DM output from -dm_view using the same viewer

8279:   Level: intermediate

8281: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMView()`
8282: @*/
8283: PetscErrorCode DMPlexCreateLabelField(DM dm, DMLabel label, Vec *val)
8284: {
8285:   DM           rdm;
8286:   PetscFE      fe;
8287:   PetscScalar *v;
8288:   PetscInt     dim, cStart, cEnd, c;

8294:   DMClone(dm, &rdm);
8295:   DMGetDimension(rdm, &dim);
8296:   PetscFECreateDefault(PetscObjectComm((PetscObject)rdm), dim, 1, PETSC_TRUE, "PETSc___label_value_", -1, &fe);
8297:   PetscObjectSetName((PetscObject)fe, "label_value");
8298:   DMSetField(rdm, 0, NULL, (PetscObject)fe);
8299:   PetscFEDestroy(&fe);
8300:   DMCreateDS(rdm);
8301:   DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);
8302:   DMCreateGlobalVector(rdm, val);
8303:   PetscObjectSetName((PetscObject)*val, "label_value");
8304:   VecGetArray(*val, &v);
8305:   for (c = cStart; c < cEnd; ++c) {
8306:     PetscScalar *lv;
8307:     PetscInt     cval;

8309:     DMPlexPointGlobalRef(rdm, c, v, &lv);
8310:     DMLabelGetValue(label, c, &cval);
8311:     *lv = cval;
8312:   }
8313:   VecRestoreArray(*val, &v);
8314:   DMDestroy(&rdm);
8315:   return 0;
8316: }

8318: /*@
8319:   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.

8321:   Input Parameter:
8322: . dm - The `DMPLEX` object

8324:   Level: developer

8326:   Notes:
8327:   This is a useful diagnostic when creating meshes programmatically.

8329:   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().

8331: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMCreate()`, `DMSetFromOptions()`
8332: @*/
8333: PetscErrorCode DMPlexCheckSymmetry(DM dm)
8334: {
8335:   PetscSection    coneSection, supportSection;
8336:   const PetscInt *cone, *support;
8337:   PetscInt        coneSize, c, supportSize, s;
8338:   PetscInt        pStart, pEnd, p, pp, csize, ssize;
8339:   PetscBool       storagecheck = PETSC_TRUE;

8342:   DMViewFromOptions(dm, NULL, "-sym_dm_view");
8343:   DMPlexGetConeSection(dm, &coneSection);
8344:   DMPlexGetSupportSection(dm, &supportSection);
8345:   /* Check that point p is found in the support of its cone points, and vice versa */
8346:   DMPlexGetChart(dm, &pStart, &pEnd);
8347:   for (p = pStart; p < pEnd; ++p) {
8348:     DMPlexGetConeSize(dm, p, &coneSize);
8349:     DMPlexGetCone(dm, p, &cone);
8350:     for (c = 0; c < coneSize; ++c) {
8351:       PetscBool dup = PETSC_FALSE;
8352:       PetscInt  d;
8353:       for (d = c - 1; d >= 0; --d) {
8354:         if (cone[c] == cone[d]) {
8355:           dup = PETSC_TRUE;
8356:           break;
8357:         }
8358:       }
8359:       DMPlexGetSupportSize(dm, cone[c], &supportSize);
8360:       DMPlexGetSupport(dm, cone[c], &support);
8361:       for (s = 0; s < supportSize; ++s) {
8362:         if (support[s] == p) break;
8363:       }
8364:       if ((s >= supportSize) || (dup && (support[s + 1] != p))) {
8365:         PetscPrintf(PETSC_COMM_SELF, "p: %" PetscInt_FMT " cone: ", p);
8366:         for (s = 0; s < coneSize; ++s) PetscPrintf(PETSC_COMM_SELF, "%" PetscInt_FMT ", ", cone[s]);
8367:         PetscPrintf(PETSC_COMM_SELF, "\n");
8368:         PetscPrintf(PETSC_COMM_SELF, "p: %" PetscInt_FMT " support: ", cone[c]);
8369:         for (s = 0; s < supportSize; ++s) PetscPrintf(PETSC_COMM_SELF, "%" PetscInt_FMT ", ", support[s]);
8370:         PetscPrintf(PETSC_COMM_SELF, "\n");
8372:         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %" PetscInt_FMT " not found in support of cone point %" PetscInt_FMT, p, cone[c]);
8373:       }
8374:     }
8375:     DMPlexGetTreeParent(dm, p, &pp, NULL);
8376:     if (p != pp) {
8377:       storagecheck = PETSC_FALSE;
8378:       continue;
8379:     }
8380:     DMPlexGetSupportSize(dm, p, &supportSize);
8381:     DMPlexGetSupport(dm, p, &support);
8382:     for (s = 0; s < supportSize; ++s) {
8383:       DMPlexGetConeSize(dm, support[s], &coneSize);
8384:       DMPlexGetCone(dm, support[s], &cone);
8385:       for (c = 0; c < coneSize; ++c) {
8386:         DMPlexGetTreeParent(dm, cone[c], &pp, NULL);
8387:         if (cone[c] != pp) {
8388:           c = 0;
8389:           break;
8390:         }
8391:         if (cone[c] == p) break;
8392:       }
8393:       if (c >= coneSize) {
8394:         PetscPrintf(PETSC_COMM_SELF, "p: %" PetscInt_FMT " support: ", p);
8395:         for (c = 0; c < supportSize; ++c) PetscPrintf(PETSC_COMM_SELF, "%" PetscInt_FMT ", ", support[c]);
8396:         PetscPrintf(PETSC_COMM_SELF, "\n");
8397:         PetscPrintf(PETSC_COMM_SELF, "p: %" PetscInt_FMT " cone: ", support[s]);
8398:         for (c = 0; c < coneSize; ++c) PetscPrintf(PETSC_COMM_SELF, "%" PetscInt_FMT ", ", cone[c]);
8399:         PetscPrintf(PETSC_COMM_SELF, "\n");
8400:         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %" PetscInt_FMT " not found in cone of support point %" PetscInt_FMT, p, support[s]);
8401:       }
8402:     }
8403:   }
8404:   if (storagecheck) {
8405:     PetscSectionGetStorageSize(coneSection, &csize);
8406:     PetscSectionGetStorageSize(supportSection, &ssize);
8408:   }
8409:   return 0;
8410: }

8412: /*
8413:   For submeshes with cohesive cells (see DMPlexConstructCohesiveCells()), we allow a special case where some of the boundary of a face (edges and vertices) are not duplicated. We call these special boundary points "unsplit", since the same edge or vertex appears in both copies of the face. These unsplit points throw off our counting, so we have to explicitly account for them here.
8414: */
8415: static PetscErrorCode DMPlexCellUnsplitVertices_Private(DM dm, PetscInt c, DMPolytopeType ct, PetscInt *unsplit)
8416: {
8417:   DMPolytopeType  cct;
8418:   PetscInt        ptpoints[4];
8419:   const PetscInt *cone, *ccone, *ptcone;
8420:   PetscInt        coneSize, cp, cconeSize, ccp, npt = 0, pt;

8422:   *unsplit = 0;
8423:   switch (ct) {
8424:   case DM_POLYTOPE_POINT_PRISM_TENSOR:
8425:     ptpoints[npt++] = c;
8426:     break;
8427:   case DM_POLYTOPE_SEG_PRISM_TENSOR:
8428:     DMPlexGetCone(dm, c, &cone);
8429:     DMPlexGetConeSize(dm, c, &coneSize);
8430:     for (cp = 0; cp < coneSize; ++cp) {
8431:       DMPlexGetCellType(dm, cone[cp], &cct);
8432:       if (cct == DM_POLYTOPE_POINT_PRISM_TENSOR) ptpoints[npt++] = cone[cp];
8433:     }
8434:     break;
8435:   case DM_POLYTOPE_TRI_PRISM_TENSOR:
8436:   case DM_POLYTOPE_QUAD_PRISM_TENSOR:
8437:     DMPlexGetCone(dm, c, &cone);
8438:     DMPlexGetConeSize(dm, c, &coneSize);
8439:     for (cp = 0; cp < coneSize; ++cp) {
8440:       DMPlexGetCone(dm, cone[cp], &ccone);
8441:       DMPlexGetConeSize(dm, cone[cp], &cconeSize);
8442:       for (ccp = 0; ccp < cconeSize; ++ccp) {
8443:         DMPlexGetCellType(dm, ccone[ccp], &cct);
8444:         if (cct == DM_POLYTOPE_POINT_PRISM_TENSOR) {
8445:           PetscInt p;
8446:           for (p = 0; p < npt; ++p)
8447:             if (ptpoints[p] == ccone[ccp]) break;
8448:           if (p == npt) ptpoints[npt++] = ccone[ccp];
8449:         }
8450:       }
8451:     }
8452:     break;
8453:   default:
8454:     break;
8455:   }
8456:   for (pt = 0; pt < npt; ++pt) {
8457:     DMPlexGetCone(dm, ptpoints[pt], &ptcone);
8458:     if (ptcone[0] == ptcone[1]) ++(*unsplit);
8459:   }
8460:   return 0;
8461: }

8463: /*@
8464:   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices

8466:   Input Parameters:
8467: + dm - The `DMPLEX` object
8468: - cellHeight - Normally 0

8470:   Level: developer

8472:   Notes:
8473:   This is a useful diagnostic when creating meshes programmatically.
8474:   Currently applicable only to homogeneous simplex or tensor meshes.

8476:   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().

8478: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMCreate()`, `DMSetFromOptions()`
8479: @*/
8480: PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscInt cellHeight)
8481: {
8482:   DMPlexInterpolatedFlag interp;
8483:   DMPolytopeType         ct;
8484:   PetscInt               vStart, vEnd, cStart, cEnd, c;

8487:   DMPlexIsInterpolated(dm, &interp);
8488:   DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);
8489:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
8490:   for (c = cStart; c < cEnd; ++c) {
8491:     PetscInt *closure = NULL;
8492:     PetscInt  coneSize, closureSize, cl, Nv = 0;

8494:     DMPlexGetCellType(dm, c, &ct);
8496:     if (ct == DM_POLYTOPE_UNKNOWN) continue;
8497:     if (interp == DMPLEX_INTERPOLATED_FULL) {
8498:       DMPlexGetConeSize(dm, c, &coneSize);
8500:     }
8501:     DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
8502:     for (cl = 0; cl < closureSize * 2; cl += 2) {
8503:       const PetscInt p = closure[cl];
8504:       if ((p >= vStart) && (p < vEnd)) ++Nv;
8505:     }
8506:     DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
8507:     /* Special Case: Tensor faces with identified vertices */
8508:     if (Nv < DMPolytopeTypeGetNumVertices(ct)) {
8509:       PetscInt unsplit;

8511:       DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);
8512:       if (Nv + unsplit == DMPolytopeTypeGetNumVertices(ct)) continue;
8513:     }
8515:   }
8516:   return 0;
8517: }

8519: /*@
8520:   DMPlexCheckFaces - Check that the faces of each cell give a vertex order this is consistent with what we expect from the cell type

8522:   Collective on dm

8524:   Input Parameters:
8525: + dm - The `DMPLEX` object
8526: - cellHeight - Normally 0

8528:   Level: developer

8530:   Notes:
8531:   This is a useful diagnostic when creating meshes programmatically.
8532:   This routine is only relevant for meshes that are fully interpolated across all ranks.
8533:   It will error out if a partially interpolated mesh is given on some rank.
8534:   It will do nothing for locally uninterpolated mesh (as there is nothing to check).

8536:   For the complete list of DMPlexCheck* functions, see `DMSetFromOptions()`.

8538: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMCreate()`, `DMPlexGetVTKCellHeight()`, `DMSetFromOptions()`
8539: @*/
8540: PetscErrorCode DMPlexCheckFaces(DM dm, PetscInt cellHeight)
8541: {
8542:   PetscInt               dim, depth, vStart, vEnd, cStart, cEnd, c, h;
8543:   DMPlexInterpolatedFlag interpEnum;

8546:   DMPlexIsInterpolatedCollective(dm, &interpEnum);
8547:   if (interpEnum == DMPLEX_INTERPOLATED_NONE) return 0;
8548:   if (interpEnum != DMPLEX_INTERPOLATED_FULL) {
8549:     PetscPrintf(PetscObjectComm((PetscObject)dm), "DMPlexCheckFaces() warning: Mesh is only partially interpolated, this is currently not supported");
8550:     return 0;
8551:   }

8553:   DMGetDimension(dm, &dim);
8554:   DMPlexGetDepth(dm, &depth);
8555:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
8556:   for (h = cellHeight; h < PetscMin(depth, dim); ++h) {
8557:     DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);
8558:     for (c = cStart; c < cEnd; ++c) {
8559:       const PetscInt       *cone, *ornt, *faceSizes, *faces;
8560:       const DMPolytopeType *faceTypes;
8561:       DMPolytopeType        ct;
8562:       PetscInt              numFaces, coneSize, f;
8563:       PetscInt             *closure = NULL, closureSize, cl, numCorners = 0, fOff = 0, unsplit;

8565:       DMPlexGetCellType(dm, c, &ct);
8566:       DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);
8567:       if (unsplit) continue;
8568:       DMPlexGetConeSize(dm, c, &coneSize);
8569:       DMPlexGetCone(dm, c, &cone);
8570:       DMPlexGetConeOrientation(dm, c, &ornt);
8571:       DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
8572:       for (cl = 0; cl < closureSize * 2; cl += 2) {
8573:         const PetscInt p = closure[cl];
8574:         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
8575:       }
8576:       DMPlexGetRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces);
8578:       for (f = 0; f < numFaces; ++f) {
8579:         DMPolytopeType fct;
8580:         PetscInt      *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;

8582:         DMPlexGetCellType(dm, cone[f], &fct);
8583:         DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);
8584:         for (cl = 0; cl < fclosureSize * 2; cl += 2) {
8585:           const PetscInt p = fclosure[cl];
8586:           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
8587:         }
8589:         for (v = 0; v < fnumCorners; ++v) {
8590:           if (fclosure[v] != faces[fOff + v]) {
8591:             PetscInt v1;

8593:             PetscPrintf(PETSC_COMM_SELF, "face closure:");
8594:             for (v1 = 0; v1 < fnumCorners; ++v1) PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, fclosure[v1]);
8595:             PetscPrintf(PETSC_COMM_SELF, "\ncell face:");
8596:             for (v1 = 0; v1 < fnumCorners; ++v1) PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, faces[fOff + v1]);
8597:             PetscPrintf(PETSC_COMM_SELF, "\n");
8598:             SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " of type %s (cone idx %" PetscInt_FMT ", ornt %" PetscInt_FMT ") of cell %" PetscInt_FMT " of type %s vertex %" PetscInt_FMT ", %" PetscInt_FMT " != %" PetscInt_FMT, cone[f], DMPolytopeTypes[fct], f, ornt[f], c, DMPolytopeTypes[ct], v, fclosure[v], faces[fOff + v]);
8599:           }
8600:         }
8601:         DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);
8602:         fOff += faceSizes[f];
8603:       }
8604:       DMPlexRestoreRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces);
8605:       DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
8606:     }
8607:   }
8608:   return 0;
8609: }

8611: /*@
8612:   DMPlexCheckGeometry - Check the geometry of mesh cells

8614:   Input Parameter:
8615: . dm - The `DMPLEX` object

8617:   Level: developer

8619:   Notes:
8620:   This is a useful diagnostic when creating meshes programmatically.

8622:   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().

8624: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMCreate()`, `DMSetFromOptions()`
8625: @*/
8626: PetscErrorCode DMPlexCheckGeometry(DM dm)
8627: {
8628:   Vec       coordinates;
8629:   PetscReal detJ, J[9], refVol = 1.0;
8630:   PetscReal vol;
8631:   PetscInt  dim, depth, dE, d, cStart, cEnd, c;

8633:   DMGetDimension(dm, &dim);
8634:   DMGetCoordinateDim(dm, &dE);
8635:   if (dim != dE) return 0;
8636:   DMPlexGetDepth(dm, &depth);
8637:   for (d = 0; d < dim; ++d) refVol *= 2.0;
8638:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
8639:   /* Make sure local coordinates are created, because that step is collective */
8640:   DMGetCoordinatesLocal(dm, &coordinates);
8641:   for (c = cStart; c < cEnd; ++c) {
8642:     DMPolytopeType ct;
8643:     PetscInt       unsplit;
8644:     PetscBool      ignoreZeroVol = PETSC_FALSE;

8646:     DMPlexGetCellType(dm, c, &ct);
8647:     switch (ct) {
8648:     case DM_POLYTOPE_SEG_PRISM_TENSOR:
8649:     case DM_POLYTOPE_TRI_PRISM_TENSOR:
8650:     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
8651:       ignoreZeroVol = PETSC_TRUE;
8652:       break;
8653:     default:
8654:       break;
8655:     }
8656:     switch (ct) {
8657:     case DM_POLYTOPE_TRI_PRISM:
8658:     case DM_POLYTOPE_TRI_PRISM_TENSOR:
8659:     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
8660:     case DM_POLYTOPE_PYRAMID:
8661:       continue;
8662:     default:
8663:       break;
8664:     }
8665:     DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);
8666:     if (unsplit) continue;
8667:     DMPlexComputeCellGeometryFEM(dm, c, NULL, NULL, J, NULL, &detJ);
8669:     PetscInfo(dm, "Cell %" PetscInt_FMT " FEM Volume %g\n", c, (double)(detJ * refVol));
8670:     /* This should work with periodicity since DG coordinates should be used */
8671:     if (depth > 1) {
8672:       DMPlexComputeCellGeometryFVM(dm, c, &vol, NULL, NULL);
8674:       PetscInfo(dm, "Cell %" PetscInt_FMT " FVM Volume %g\n", c, (double)vol);
8675:     }
8676:   }
8677:   return 0;
8678: }

8680: /*@
8681:   DMPlexCheckPointSF - Check that several necessary conditions are met for the Point SF of this plex.

8683:   Collective on dm

8685:   Input Parameters:
8686: + dm - The `DMPLEX` object
8687: . pointSF - The `PetscSF`, or NULL for `PointSF` attached to `DM`
8688: - allowExtraRoots - Flag to allow extra points not present in the `DM`

8690:   Level: developer

8692:   Notes:
8693:   This is mainly intended for debugging/testing purposes.

8695:   For the complete list of DMPlexCheck* functions, see `DMSetFromOptions()`.

8697:   Extra roots can come from priodic cuts, where additional points appear on the boundary

8699: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMGetPointSF()`, `DMSetFromOptions()`
8700: @*/
8701: PetscErrorCode DMPlexCheckPointSF(DM dm, PetscSF pointSF, PetscBool allowExtraRoots)
8702: {
8703:   PetscInt           l, nleaves, nroots, overlap;
8704:   const PetscInt    *locals;
8705:   const PetscSFNode *remotes;
8706:   PetscBool          distributed;
8707:   MPI_Comm           comm;
8708:   PetscMPIInt        rank;

8712:   else pointSF = dm->sf;
8713:   PetscObjectGetComm((PetscObject)dm, &comm);
8715:   MPI_Comm_rank(comm, &rank);
8716:   {
8717:     PetscMPIInt mpiFlag;

8719:     MPI_Comm_compare(comm, PetscObjectComm((PetscObject)pointSF), &mpiFlag);
8721:   }
8722:   PetscSFGetGraph(pointSF, &nroots, &nleaves, &locals, &remotes);
8723:   DMPlexIsDistributed(dm, &distributed);
8724:   if (!distributed) {
8726:     return 0;
8727:   }
8729:   DMPlexGetOverlap(dm, &overlap);

8731:   /* Check SF graph is compatible with DMPlex chart */
8732:   {
8733:     PetscInt pStart, pEnd, maxLeaf;

8735:     DMPlexGetChart(dm, &pStart, &pEnd);
8736:     PetscSFGetLeafRange(pointSF, NULL, &maxLeaf);
8739:   }

8741:   /* Check Point SF has no local points referenced */
8742:   for (l = 0; l < nleaves; l++) {
8743:     PetscAssert(remotes[l].rank != (PetscInt)rank, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point SF contains local point %" PetscInt_FMT " <- (%" PetscInt_FMT ",%" PetscInt_FMT ")", locals ? locals[l] : l, remotes[l].rank, remotes[l].index);
8744:   }

8746:   /* Check there are no cells in interface */
8747:   if (!overlap) {
8748:     PetscInt cellHeight, cStart, cEnd;

8750:     DMPlexGetVTKCellHeight(dm, &cellHeight);
8751:     DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);
8752:     for (l = 0; l < nleaves; ++l) {
8753:       const PetscInt point = locals ? locals[l] : l;

8756:     }
8757:   }

8759:   /* If some point is in interface, then all its cone points must be also in interface (either as leaves or roots) */
8760:   {
8761:     const PetscInt *rootdegree;

8763:     PetscSFComputeDegreeBegin(pointSF, &rootdegree);
8764:     PetscSFComputeDegreeEnd(pointSF, &rootdegree);
8765:     for (l = 0; l < nleaves; ++l) {
8766:       const PetscInt  point = locals ? locals[l] : l;
8767:       const PetscInt *cone;
8768:       PetscInt        coneSize, c, idx;

8770:       DMPlexGetConeSize(dm, point, &coneSize);
8771:       DMPlexGetCone(dm, point, &cone);
8772:       for (c = 0; c < coneSize; ++c) {
8773:         if (!rootdegree[cone[c]]) {
8774:           if (locals) {
8775:             PetscFindInt(cone[c], nleaves, locals, &idx);
8776:           } else {
8777:             idx = (cone[c] < nleaves) ? cone[c] : -1;
8778:           }
8780:         }
8781:       }
8782:     }
8783:   }
8784:   return 0;
8785: }

8787: /*@
8788:   DMPlexCheck - Perform various checks of Plex sanity

8790:   Input Parameter:
8791: . dm - The `DMPLEX` object

8793:   Level: developer

8795:   Notes:
8796:   This is a useful diagnostic when creating meshes programmatically.

8798:   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().

8800:   Currently does not include DMPlexCheckCellShape().

8802: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, DMCreate(), DMSetFromOptions()
8803: @*/
8804: PetscErrorCode DMPlexCheck(DM dm)
8805: {
8806:   PetscInt cellHeight;

8808:   DMPlexGetVTKCellHeight(dm, &cellHeight);
8809:   DMPlexCheckSymmetry(dm);
8810:   DMPlexCheckSkeleton(dm, cellHeight);
8811:   DMPlexCheckFaces(dm, cellHeight);
8812:   DMPlexCheckGeometry(dm);
8813:   DMPlexCheckPointSF(dm, NULL, PETSC_FALSE);
8814:   DMPlexCheckInterfaceCones(dm);
8815:   return 0;
8816: }

8818: typedef struct cell_stats {
8819:   PetscReal min, max, sum, squaresum;
8820:   PetscInt  count;
8821: } cell_stats_t;

8823: static void MPIAPI cell_stats_reduce(void *a, void *b, int *len, MPI_Datatype *datatype)
8824: {
8825:   PetscInt i, N = *len;

8827:   for (i = 0; i < N; i++) {
8828:     cell_stats_t *A = (cell_stats_t *)a;
8829:     cell_stats_t *B = (cell_stats_t *)b;

8831:     B->min = PetscMin(A->min, B->min);
8832:     B->max = PetscMax(A->max, B->max);
8833:     B->sum += A->sum;
8834:     B->squaresum += A->squaresum;
8835:     B->count += A->count;
8836:   }
8837: }

8839: /*@
8840:   DMPlexCheckCellShape - Checks the Jacobian of the mapping from reference to real cells and computes some minimal statistics.

8842:   Collective on dm

8844:   Input Parameters:
8845: + dm        - The `DMPLEX` object
8846: . output    - If true, statistics will be displayed on stdout
8847: - condLimit - Display all cells above this condition number, or `PETSC_DETERMINE` for no cell output

8849:   Level: developer

8851:   Notes:
8852:   This is mainly intended for debugging/testing purposes.

8854:   For the complete list of DMPlexCheck* functions, see `DMSetFromOptions()`.

8856: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexComputeOrthogonalQuality()`
8857: @*/
8858: PetscErrorCode DMPlexCheckCellShape(DM dm, PetscBool output, PetscReal condLimit)
8859: {
8860:   DM           dmCoarse;
8861:   cell_stats_t stats, globalStats;
8862:   MPI_Comm     comm = PetscObjectComm((PetscObject)dm);
8863:   PetscReal   *J, *invJ, min = 0, max = 0, mean = 0, stdev = 0;
8864:   PetscReal    limit = condLimit > 0 ? condLimit : PETSC_MAX_REAL;
8865:   PetscInt     cdim, cStart, cEnd, c, eStart, eEnd, count = 0;
8866:   PetscMPIInt  rank, size;

8869:   stats.min = PETSC_MAX_REAL;
8870:   stats.max = PETSC_MIN_REAL;
8871:   stats.sum = stats.squaresum = 0.;
8872:   stats.count                 = 0;

8874:   MPI_Comm_size(comm, &size);
8875:   MPI_Comm_rank(comm, &rank);
8876:   DMGetCoordinateDim(dm, &cdim);
8877:   PetscMalloc2(PetscSqr(cdim), &J, PetscSqr(cdim), &invJ);
8878:   DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);
8879:   DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);
8880:   for (c = cStart; c < cEnd; c++) {
8881:     PetscInt  i;
8882:     PetscReal frobJ = 0., frobInvJ = 0., cond2, cond, detJ;

8884:     DMPlexComputeCellGeometryAffineFEM(dm, c, NULL, J, invJ, &detJ);
8886:     for (i = 0; i < PetscSqr(cdim); ++i) {
8887:       frobJ += J[i] * J[i];
8888:       frobInvJ += invJ[i] * invJ[i];
8889:     }
8890:     cond2 = frobJ * frobInvJ;
8891:     cond  = PetscSqrtReal(cond2);

8893:     stats.min = PetscMin(stats.min, cond);
8894:     stats.max = PetscMax(stats.max, cond);
8895:     stats.sum += cond;
8896:     stats.squaresum += cond2;
8897:     stats.count++;
8898:     if (output && cond > limit) {
8899:       PetscSection coordSection;
8900:       Vec          coordsLocal;
8901:       PetscScalar *coords = NULL;
8902:       PetscInt     Nv, d, clSize, cl, *closure = NULL;

8904:       DMGetCoordinatesLocal(dm, &coordsLocal);
8905:       DMGetCoordinateSection(dm, &coordSection);
8906:       DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);
8907:       PetscSynchronizedPrintf(comm, "[%d] Cell %" PetscInt_FMT " cond %g\n", rank, c, (double)cond);
8908:       for (i = 0; i < Nv / cdim; ++i) {
8909:         PetscSynchronizedPrintf(comm, "  Vertex %" PetscInt_FMT ": (", i);
8910:         for (d = 0; d < cdim; ++d) {
8911:           if (d > 0) PetscSynchronizedPrintf(comm, ", ");
8912:           PetscSynchronizedPrintf(comm, "%g", (double)PetscRealPart(coords[i * cdim + d]));
8913:         }
8914:         PetscSynchronizedPrintf(comm, ")\n");
8915:       }
8916:       DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);
8917:       for (cl = 0; cl < clSize * 2; cl += 2) {
8918:         const PetscInt edge = closure[cl];

8920:         if ((edge >= eStart) && (edge < eEnd)) {
8921:           PetscReal len;

8923:           DMPlexComputeCellGeometryFVM(dm, edge, &len, NULL, NULL);
8924:           PetscSynchronizedPrintf(comm, "  Edge %" PetscInt_FMT ": length %g\n", edge, (double)len);
8925:         }
8926:       }
8927:       DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);
8928:       DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);
8929:     }
8930:   }
8931:   if (output) PetscSynchronizedFlush(comm, NULL);

8933:   if (size > 1) {
8934:     PetscMPIInt  blockLengths[2] = {4, 1};
8935:     MPI_Aint     blockOffsets[2] = {offsetof(cell_stats_t, min), offsetof(cell_stats_t, count)};
8936:     MPI_Datatype blockTypes[2]   = {MPIU_REAL, MPIU_INT}, statType;
8937:     MPI_Op       statReduce;

8939:     MPI_Type_create_struct(2, blockLengths, blockOffsets, blockTypes, &statType);
8940:     MPI_Type_commit(&statType);
8941:     MPI_Op_create(cell_stats_reduce, PETSC_TRUE, &statReduce);
8942:     MPI_Reduce(&stats, &globalStats, 1, statType, statReduce, 0, comm);
8943:     MPI_Op_free(&statReduce);
8944:     MPI_Type_free(&statType);
8945:   } else {
8946:     PetscArraycpy(&globalStats, &stats, 1);
8947:   }
8948:   if (rank == 0) {
8949:     count = globalStats.count;
8950:     min   = globalStats.min;
8951:     max   = globalStats.max;
8952:     mean  = globalStats.sum / globalStats.count;
8953:     stdev = globalStats.count > 1 ? PetscSqrtReal(PetscMax((globalStats.squaresum - globalStats.count * mean * mean) / (globalStats.count - 1), 0)) : 0.0;
8954:   }

8956:   if (output) PetscPrintf(comm, "Mesh with %" PetscInt_FMT " cells, shape condition numbers: min = %g, max = %g, mean = %g, stddev = %g\n", count, (double)min, (double)max, (double)mean, (double)stdev);
8957:   PetscFree2(J, invJ);

8959:   DMGetCoarseDM(dm, &dmCoarse);
8960:   if (dmCoarse) {
8961:     PetscBool isplex;

8963:     PetscObjectTypeCompare((PetscObject)dmCoarse, DMPLEX, &isplex);
8964:     if (isplex) DMPlexCheckCellShape(dmCoarse, output, condLimit);
8965:   }
8966:   return 0;
8967: }

8969: /*@
8970:   DMPlexComputeOrthogonalQuality - Compute cell-wise orthogonal quality mesh statistic. Optionally tags all cells with
8971:   orthogonal quality below given tolerance.

8973:   Collective on dm

8975:   Input Parameters:
8976: + dm   - The `DMPLEX` object
8977: . fv   - Optional `PetscFV` object for pre-computed cell/face centroid information
8978: - atol - [0, 1] Absolute tolerance for tagging cells.

8980:   Output Parameters:
8981: + OrthQual      - Vec containing orthogonal quality per cell
8982: - OrthQualLabel - `DMLabel` tagging cells below atol with `DM_ADAPT_REFINE`

8984:   Options Database Keys:
8985: + -dm_plex_orthogonal_quality_label_view - view OrthQualLabel if label is requested. Currently only `PETSCVIEWERASCII` is supported.
8986: - -dm_plex_orthogonal_quality_vec_view - view OrthQual vector.

8988:   Level: intermediate

8990:   Notes:
8991:   Orthogonal quality is given by the following formula:

8993:   $ \min \left[ \frac{A_i \cdot f_i}{\|A_i\| \|f_i\|} , \frac{A_i \cdot c_i}{\|A_i\| \|c_i\|} \right]$

8995:   Where A_i is the i'th face-normal vector, f_i is the vector from the cell centroid to the i'th face centroid, and c_i
8996:   is the vector from the current cells centroid to the centroid of its i'th neighbor (which shares a face with the
8997:   current cell). This computes the vector similarity between each cell face and its corresponding neighbor centroid by
8998:   calculating the cosine of the angle between these vectors.

9000:   Orthogonal quality ranges from 1 (best) to 0 (worst).

9002:   This routine is mainly useful for FVM, however is not restricted to only FVM. The `PetscFV` object is optionally used to check for
9003:   pre-computed FVM cell data, but if it is not passed in then this data will be computed.

9005:   Cells are tagged if they have an orthogonal quality less than or equal to the absolute tolerance.

9007: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCheckCellShape()`, `DMCreateLabel()`, `PetscFV`, `DMLabel`, `Vec`
9008: @*/
9009: PetscErrorCode DMPlexComputeOrthogonalQuality(DM dm, PetscFV fv, PetscReal atol, Vec *OrthQual, DMLabel *OrthQualLabel)
9010: {
9011:   PetscInt               nc, cellHeight, cStart, cEnd, cell, cellIter = 0;
9012:   PetscInt              *idx;
9013:   PetscScalar           *oqVals;
9014:   const PetscScalar     *cellGeomArr, *faceGeomArr;
9015:   PetscReal             *ci, *fi, *Ai;
9016:   MPI_Comm               comm;
9017:   Vec                    cellgeom, facegeom;
9018:   DM                     dmFace, dmCell;
9019:   IS                     glob;
9020:   ISLocalToGlobalMapping ltog;
9021:   PetscViewer            vwr;

9027:   PetscObjectGetComm((PetscObject)dm, &comm);
9028:   DMGetDimension(dm, &nc);
9030:   {
9031:     DMPlexInterpolatedFlag interpFlag;

9033:     DMPlexIsInterpolated(dm, &interpFlag);
9034:     if (interpFlag != DMPLEX_INTERPOLATED_FULL) {
9035:       PetscMPIInt rank;

9037:       MPI_Comm_rank(comm, &rank);
9038:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM must be fully interpolated, DM on rank %d is not fully interpolated", rank);
9039:     }
9040:   }
9041:   if (OrthQualLabel) {
9043:     DMCreateLabel(dm, "Orthogonal_Quality");
9044:     DMGetLabel(dm, "Orthogonal_Quality", OrthQualLabel);
9045:   } else {
9046:     *OrthQualLabel = NULL;
9047:   }
9048:   DMPlexGetVTKCellHeight(dm, &cellHeight);
9049:   DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);
9050:   DMPlexCreateCellNumbering_Internal(dm, PETSC_TRUE, &glob);
9051:   ISLocalToGlobalMappingCreateIS(glob, &ltog);
9052:   ISLocalToGlobalMappingSetType(ltog, ISLOCALTOGLOBALMAPPINGHASH);
9053:   VecCreate(comm, OrthQual);
9054:   VecSetType(*OrthQual, VECSTANDARD);
9055:   VecSetSizes(*OrthQual, cEnd - cStart, PETSC_DETERMINE);
9056:   VecSetLocalToGlobalMapping(*OrthQual, ltog);
9057:   VecSetUp(*OrthQual);
9058:   ISDestroy(&glob);
9059:   ISLocalToGlobalMappingDestroy(&ltog);
9060:   DMPlexGetDataFVM(dm, fv, &cellgeom, &facegeom, NULL);
9061:   VecGetArrayRead(cellgeom, &cellGeomArr);
9062:   VecGetArrayRead(facegeom, &faceGeomArr);
9063:   VecGetDM(cellgeom, &dmCell);
9064:   VecGetDM(facegeom, &dmFace);
9065:   PetscMalloc5(cEnd - cStart, &idx, cEnd - cStart, &oqVals, nc, &ci, nc, &fi, nc, &Ai);
9066:   for (cell = cStart; cell < cEnd; cellIter++, cell++) {
9067:     PetscInt         cellneigh, cellneighiter = 0, adjSize = PETSC_DETERMINE;
9068:     PetscInt         cellarr[2], *adj = NULL;
9069:     PetscScalar     *cArr, *fArr;
9070:     PetscReal        minvalc = 1.0, minvalf = 1.0;
9071:     PetscFVCellGeom *cg;

9073:     idx[cellIter] = cell - cStart;
9074:     cellarr[0]    = cell;
9075:     /* Make indexing into cellGeom easier */
9076:     DMPlexPointLocalRead(dmCell, cell, cellGeomArr, &cg);
9077:     DMPlexGetAdjacency_Internal(dm, cell, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &adjSize, &adj);
9078:     /* Technically 1 too big, but easier than fiddling with empty adjacency array */
9079:     PetscCalloc2(adjSize, &cArr, adjSize, &fArr);
9080:     for (cellneigh = 0; cellneigh < adjSize; cellneighiter++, cellneigh++) {
9081:       PetscInt         i;
9082:       const PetscInt   neigh  = adj[cellneigh];
9083:       PetscReal        normci = 0, normfi = 0, normai = 0;
9084:       PetscFVCellGeom *cgneigh;
9085:       PetscFVFaceGeom *fg;

9087:       /* Don't count ourselves in the neighbor list */
9088:       if (neigh == cell) continue;
9089:       DMPlexPointLocalRead(dmCell, neigh, cellGeomArr, &cgneigh);
9090:       cellarr[1] = neigh;
9091:       {
9092:         PetscInt        numcovpts;
9093:         const PetscInt *covpts;

9095:         DMPlexGetMeet(dm, 2, cellarr, &numcovpts, &covpts);
9096:         DMPlexPointLocalRead(dmFace, covpts[0], faceGeomArr, &fg);
9097:         DMPlexRestoreMeet(dm, 2, cellarr, &numcovpts, &covpts);
9098:       }

9100:       /* Compute c_i, f_i and their norms */
9101:       for (i = 0; i < nc; i++) {
9102:         ci[i] = cgneigh->centroid[i] - cg->centroid[i];
9103:         fi[i] = fg->centroid[i] - cg->centroid[i];
9104:         Ai[i] = fg->normal[i];
9105:         normci += PetscPowReal(ci[i], 2);
9106:         normfi += PetscPowReal(fi[i], 2);
9107:         normai += PetscPowReal(Ai[i], 2);
9108:       }
9109:       normci = PetscSqrtReal(normci);
9110:       normfi = PetscSqrtReal(normfi);
9111:       normai = PetscSqrtReal(normai);

9113:       /* Normalize and compute for each face-cell-normal pair */
9114:       for (i = 0; i < nc; i++) {
9115:         ci[i] = ci[i] / normci;
9116:         fi[i] = fi[i] / normfi;
9117:         Ai[i] = Ai[i] / normai;
9118:         /* PetscAbs because I don't know if normals are guaranteed to point out */
9119:         cArr[cellneighiter] += PetscAbs(Ai[i] * ci[i]);
9120:         fArr[cellneighiter] += PetscAbs(Ai[i] * fi[i]);
9121:       }
9122:       if (PetscRealPart(cArr[cellneighiter]) < minvalc) minvalc = PetscRealPart(cArr[cellneighiter]);
9123:       if (PetscRealPart(fArr[cellneighiter]) < minvalf) minvalf = PetscRealPart(fArr[cellneighiter]);
9124:     }
9125:     PetscFree(adj);
9126:     PetscFree2(cArr, fArr);
9127:     /* Defer to cell if they're equal */
9128:     oqVals[cellIter] = PetscMin(minvalf, minvalc);
9129:     if (OrthQualLabel) {
9130:       if (PetscRealPart(oqVals[cellIter]) <= atol) DMLabelSetValue(*OrthQualLabel, cell, DM_ADAPT_REFINE);
9131:     }
9132:   }
9133:   VecSetValuesLocal(*OrthQual, cEnd - cStart, idx, oqVals, INSERT_VALUES);
9134:   VecAssemblyBegin(*OrthQual);
9135:   VecAssemblyEnd(*OrthQual);
9136:   VecRestoreArrayRead(cellgeom, &cellGeomArr);
9137:   VecRestoreArrayRead(facegeom, &faceGeomArr);
9138:   PetscOptionsGetViewer(comm, NULL, NULL, "-dm_plex_orthogonal_quality_label_view", &vwr, NULL, NULL);
9139:   if (OrthQualLabel) {
9140:     if (vwr) DMLabelView(*OrthQualLabel, vwr);
9141:   }
9142:   PetscFree5(idx, oqVals, ci, fi, Ai);
9143:   PetscViewerDestroy(&vwr);
9144:   VecViewFromOptions(*OrthQual, NULL, "-dm_plex_orthogonal_quality_vec_view");
9145:   return 0;
9146: }

9148: /* this is here instead of DMGetOutputDM because output DM still has constraints in the local indices that affect
9149:  * interpolator construction */
9150: static PetscErrorCode DMGetFullDM(DM dm, DM *odm)
9151: {
9152:   PetscSection section, newSection, gsection;
9153:   PetscSF      sf;
9154:   PetscBool    hasConstraints, ghasConstraints;

9158:   DMGetLocalSection(dm, &section);
9159:   PetscSectionHasConstraints(section, &hasConstraints);
9160:   MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm));
9161:   if (!ghasConstraints) {
9162:     PetscObjectReference((PetscObject)dm);
9163:     *odm = dm;
9164:     return 0;
9165:   }
9166:   DMClone(dm, odm);
9167:   DMCopyFields(dm, *odm);
9168:   DMGetLocalSection(*odm, &newSection);
9169:   DMGetPointSF(*odm, &sf);
9170:   PetscSectionCreateGlobalSection(newSection, sf, PETSC_TRUE, PETSC_FALSE, &gsection);
9171:   DMSetGlobalSection(*odm, gsection);
9172:   PetscSectionDestroy(&gsection);
9173:   return 0;
9174: }

9176: static PetscErrorCode DMCreateAffineInterpolationCorrection_Plex(DM dmc, DM dmf, Vec *shift)
9177: {
9178:   DM        dmco, dmfo;
9179:   Mat       interpo;
9180:   Vec       rscale;
9181:   Vec       cglobalo, clocal;
9182:   Vec       fglobal, fglobalo, flocal;
9183:   PetscBool regular;

9185:   DMGetFullDM(dmc, &dmco);
9186:   DMGetFullDM(dmf, &dmfo);
9187:   DMSetCoarseDM(dmfo, dmco);
9188:   DMPlexGetRegularRefinement(dmf, &regular);
9189:   DMPlexSetRegularRefinement(dmfo, regular);
9190:   DMCreateInterpolation(dmco, dmfo, &interpo, &rscale);
9191:   DMCreateGlobalVector(dmco, &cglobalo);
9192:   DMCreateLocalVector(dmc, &clocal);
9193:   VecSet(cglobalo, 0.);
9194:   VecSet(clocal, 0.);
9195:   DMCreateGlobalVector(dmf, &fglobal);
9196:   DMCreateGlobalVector(dmfo, &fglobalo);
9197:   DMCreateLocalVector(dmf, &flocal);
9198:   VecSet(fglobal, 0.);
9199:   VecSet(fglobalo, 0.);
9200:   VecSet(flocal, 0.);
9201:   DMPlexInsertBoundaryValues(dmc, PETSC_TRUE, clocal, 0., NULL, NULL, NULL);
9202:   DMLocalToGlobalBegin(dmco, clocal, INSERT_VALUES, cglobalo);
9203:   DMLocalToGlobalEnd(dmco, clocal, INSERT_VALUES, cglobalo);
9204:   MatMult(interpo, cglobalo, fglobalo);
9205:   DMGlobalToLocalBegin(dmfo, fglobalo, INSERT_VALUES, flocal);
9206:   DMGlobalToLocalEnd(dmfo, fglobalo, INSERT_VALUES, flocal);
9207:   DMLocalToGlobalBegin(dmf, flocal, INSERT_VALUES, fglobal);
9208:   DMLocalToGlobalEnd(dmf, flocal, INSERT_VALUES, fglobal);
9209:   *shift = fglobal;
9210:   VecDestroy(&flocal);
9211:   VecDestroy(&fglobalo);
9212:   VecDestroy(&clocal);
9213:   VecDestroy(&cglobalo);
9214:   VecDestroy(&rscale);
9215:   MatDestroy(&interpo);
9216:   DMDestroy(&dmfo);
9217:   DMDestroy(&dmco);
9218:   return 0;
9219: }

9221: PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
9222: {
9223:   PetscObject shifto;
9224:   Vec         shift;

9226:   if (!interp) {
9227:     Vec rscale;

9229:     DMCreateInterpolation(coarse, fine, &interp, &rscale);
9230: