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>
 11: #include <petscblaslapack.h>

 13: /* Logging support */
 14: PetscLogEvent DMPLEX_Interpolate, DMPLEX_Partition, DMPLEX_Distribute, DMPLEX_DistributeMultistage, 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_CreateBoxSFC, 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;
 15: PetscLogEvent DMPLEX_RebalBuildGraph, DMPLEX_RebalRewriteSF, DMPLEX_RebalGatherGraph, DMPLEX_RebalPartition, DMPLEX_RebalScatterPart, DMPLEX_Generate, DMPLEX_GetLocalOffsets, DMPLEX_Uninterpolate;
 16: PetscLogEvent DMPLEX_DistributionView, DMPLEX_DistributionLoad;

 18: PetscBool  Plexcite       = PETSC_FALSE;
 19: const char PlexCitation[] = "@article{LangeMitchellKnepleyGorman2015,\n"
 20:                             "title     = {Efficient mesh management in {Firedrake} using {PETSc-DMPlex}},\n"
 21:                             "author    = {Michael Lange and Lawrence Mitchell and Matthew G. Knepley and Gerard J. Gorman},\n"
 22:                             "journal   = {SIAM Journal on Scientific Computing},\n"
 23:                             "volume    = {38},\n"
 24:                             "number    = {5},\n"
 25:                             "pages     = {S143--S155},\n"
 26:                             "eprint    = {http://arxiv.org/abs/1506.07749},\n"
 27:                             "doi       = {10.1137/15M1026092},\n"
 28:                             "year      = {2016},\n"
 29:                             "petsc_uses={DMPlex},\n}\n";

 31: PETSC_SINGLE_LIBRARY_INTERN PetscErrorCode VecView_MPI(Vec, PetscViewer);

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

 36:   Input Parameter:
 37: . dm - The `DMPLEX` object

 39:   Output Parameter:
 40: . simplex - Flag checking for a simplex

 42:   Level: intermediate

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

 48: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSimplexOrBoxCells()`, `DMPlexGetCellType()`, `DMPlexGetHeightStratum()`, `DMPolytopeTypeGetNumVertices()`
 49: @*/
 50: PetscErrorCode DMPlexIsSimplex(DM dm, PetscBool *simplex)
 51: {
 52:   DMPolytopeType ct;
 53:   PetscInt       cStart, cEnd, cHeight;

 55:   PetscFunctionBegin;
 56:   PetscCall(DMPlexGetVTKCellHeight(dm, &cHeight));
 57:   PetscCall(DMPlexGetHeightStratum(dm, cHeight, &cStart, &cEnd));
 58:   if (cEnd <= cStart) {
 59:     *simplex = PETSC_FALSE;
 60:     PetscFunctionReturn(PETSC_SUCCESS);
 61:   }
 62:   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
 63:   *simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
 64:   PetscFunctionReturn(PETSC_SUCCESS);
 65: }

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

 70:   Input Parameters:
 71: + dm     - The `DMPLEX` object
 72: - height - The cell height in the Plex, 0 is the default

 74:   Output Parameters:
 75: + cStart - The first "normal" cell, pass `NULL` if not needed
 76: - cEnd   - The upper bound on "normal" cells, pass `NULL` if not needed

 78:   Level: developer

 80:   Note:
 81:   This function requires that tensor cells are ordered last.

 83: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexConstructGhostCells()`, `DMPlexGetCellTypeStratum()`
 84: @*/
 85: PetscErrorCode DMPlexGetSimplexOrBoxCells(DM dm, PetscInt height, PeOp PetscInt *cStart, PeOp PetscInt *cEnd)
 86: {
 87:   DMLabel         ctLabel;
 88:   IS              valueIS;
 89:   const PetscInt *ctypes;
 90:   PetscBool       found = PETSC_FALSE;
 91:   PetscInt        Nct, cS = PETSC_INT_MAX, cE = 0;

 93:   PetscFunctionBegin;
 94:   PetscCall(DMPlexGetCellTypeLabel(dm, &ctLabel));
 95:   PetscCall(DMLabelGetValueIS(ctLabel, &valueIS));
 96:   PetscCall(ISGetLocalSize(valueIS, &Nct));
 97:   PetscCall(ISGetIndices(valueIS, &ctypes));
 98:   for (PetscInt t = 0; t < Nct; ++t) {
 99:     const DMPolytopeType ct = (DMPolytopeType)ctypes[t];
100:     PetscInt             ctS, ctE, ht;

102:     if (ct == DM_POLYTOPE_UNKNOWN) {
103:       // If any cells are not typed, just use all cells
104:       PetscCall(DMPlexGetHeightStratum(dm, PetscMax(height, 0), cStart, cEnd));
105:       break;
106:     }
107:     if (DMPolytopeTypeIsHybrid(ct) || ct == DM_POLYTOPE_FV_GHOST) continue;
108:     PetscCall(DMLabelGetStratumBounds(ctLabel, ct, &ctS, &ctE));
109:     if (ctS >= ctE) continue;
110:     // Check that a point has the right height
111:     PetscCall(DMPlexGetPointHeight(dm, ctS, &ht));
112:     if (ht != height) continue;
113:     cS    = PetscMin(cS, ctS);
114:     cE    = PetscMax(cE, ctE);
115:     found = PETSC_TRUE;
116:   }
117:   if (!Nct || !found) cS = cE = 0;
118:   PetscCall(ISDestroy(&valueIS));
119:   // Reset label for fast lookup
120:   PetscCall(DMLabelMakeAllInvalid_Internal(ctLabel));
121:   if (cStart) *cStart = cS;
122:   if (cEnd) *cEnd = cE;
123:   PetscFunctionReturn(PETSC_SUCCESS);
124: }

126: PetscErrorCode DMPlexGetFieldTypes_Internal(DM dm, PetscSection section, PetscInt field, PetscInt *types, PetscInt **ssStart, PetscInt **ssEnd, PetscViewerVTKFieldType **sft)
127: {
128:   PetscInt                 cdim, pStart, pEnd, vStart, vEnd, cStart, cEnd, c, depth, cellHeight, t;
129:   PetscInt                *sStart, *sEnd;
130:   PetscViewerVTKFieldType *ft;
131:   PetscInt                 globalvcdof[DM_NUM_POLYTOPES + 1];
132:   DMLabel                  depthLabel, ctLabel;

134:   PetscFunctionBegin;
135:   /* the vcdof and globalvcdof are sized to allow every polytope type and simple vertex at DM_NUM_POLYTOPES */
136:   PetscCall(PetscArrayzero(globalvcdof, DM_NUM_POLYTOPES + 1));
137:   PetscCall(DMGetCoordinateDim(dm, &cdim));
138:   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
139:   PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
140:   if (field >= 0) {
141:     if (vStart >= pStart && vStart < pEnd) PetscCall(PetscSectionGetFieldDof(section, vStart, field, &globalvcdof[DM_NUM_POLYTOPES]));
142:   } else {
143:     if (vStart >= pStart && vStart < pEnd) PetscCall(PetscSectionGetDof(section, vStart, &globalvcdof[DM_NUM_POLYTOPES]));
144:   }

146:   PetscCall(DMPlexGetVTKCellHeight(dm, &cellHeight));
147:   PetscCall(DMPlexGetDepth(dm, &depth));
148:   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
149:   PetscCall(DMPlexGetCellTypeLabel(dm, &ctLabel));
150:   for (c = 0; c < DM_NUM_POLYTOPES; ++c) {
151:     const DMPolytopeType ict = (DMPolytopeType)c;
152:     PetscInt             dep;

154:     if (ict == DM_POLYTOPE_FV_GHOST) continue;
155:     PetscCall(DMLabelGetStratumBounds(ctLabel, ict, &cStart, &cEnd));
156:     if (pStart >= 0) {
157:       PetscCall(DMLabelGetValue(depthLabel, cStart, &dep));
158:       if (dep != depth - cellHeight) continue;
159:     }
160:     if (field >= 0) {
161:       if (cStart >= pStart && cStart < pEnd) PetscCall(PetscSectionGetFieldDof(section, cStart, field, &globalvcdof[c]));
162:     } else {
163:       if (cStart >= pStart && cStart < pEnd) PetscCall(PetscSectionGetDof(section, cStart, &globalvcdof[c]));
164:     }
165:   }

167:   PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, globalvcdof, DM_NUM_POLYTOPES + 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
168:   *types = 0;

170:   for (c = 0; c < DM_NUM_POLYTOPES + 1; ++c) {
171:     if (globalvcdof[c]) ++(*types);
172:   }

174:   PetscCall(PetscMalloc3(*types, &sStart, *types, &sEnd, *types, &ft));
175:   t = 0;
176:   if (globalvcdof[DM_NUM_POLYTOPES]) {
177:     sStart[t] = vStart;
178:     sEnd[t]   = vEnd;
179:     ft[t]     = (globalvcdof[t] == cdim) ? PETSC_VTK_POINT_VECTOR_FIELD : PETSC_VTK_POINT_FIELD;
180:     ++t;
181:   }

183:   for (c = 0; c < DM_NUM_POLYTOPES; ++c) {
184:     if (globalvcdof[c]) {
185:       const DMPolytopeType ict = (DMPolytopeType)c;

187:       PetscCall(DMLabelGetStratumBounds(ctLabel, ict, &cStart, &cEnd));
188:       sStart[t] = cStart;
189:       sEnd[t]   = cEnd;
190:       ft[t]     = (globalvcdof[c] == cdim) ? PETSC_VTK_CELL_VECTOR_FIELD : PETSC_VTK_CELL_FIELD;
191:       ++t;
192:     }
193:   }

195:   if (!*types) {
196:     if (field >= 0) {
197:       const char *fieldname;

199:       PetscCall(PetscSectionGetFieldName(section, field, &fieldname));
200:       PetscCall(PetscInfo((PetscObject)dm, "Could not classify VTK output type of section field %" PetscInt_FMT " \"%s\"\n", field, fieldname));
201:     } else {
202:       PetscCall(PetscInfo((PetscObject)dm, "Could not classify VTK output type of section\n"));
203:     }
204:   }

206:   *ssStart = sStart;
207:   *ssEnd   = sEnd;
208:   *sft     = ft;
209:   PetscFunctionReturn(PETSC_SUCCESS);
210: }

212: PetscErrorCode DMPlexRestoreFieldTypes_Internal(DM dm, PetscSection section, PetscInt field, PetscInt *types, PetscInt **sStart, PetscInt **sEnd, PetscViewerVTKFieldType **ft)
213: {
214:   PetscFunctionBegin;
215:   PetscCall(PetscFree3(*sStart, *sEnd, *ft));
216:   PetscFunctionReturn(PETSC_SUCCESS);
217: }

219: PetscErrorCode DMPlexGetFieldType_Internal(DM dm, PetscSection section, PetscInt field, PetscInt *sStart, PetscInt *sEnd, PetscViewerVTKFieldType *ft)
220: {
221:   PetscInt cdim, pStart, pEnd, vStart, vEnd, cStart, cEnd;
222:   PetscInt globalvcdof[2] = {0, 0};

224:   PetscFunctionBegin;
225:   *ft = PETSC_VTK_INVALID;
226:   PetscCall(DMGetCoordinateDim(dm, &cdim));
227:   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
228:   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
229:   PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
230:   if (field >= 0) {
231:     if (vStart >= pStart && vStart < pEnd) PetscCall(PetscSectionGetFieldDof(section, vStart, field, &globalvcdof[0]));
232:     if (cStart >= pStart && cStart < pEnd) PetscCall(PetscSectionGetFieldDof(section, cStart, field, &globalvcdof[1]));
233:   } else {
234:     if (vStart >= pStart && vStart < pEnd) PetscCall(PetscSectionGetDof(section, vStart, &globalvcdof[0]));
235:     if (cStart >= pStart && cStart < pEnd) PetscCall(PetscSectionGetDof(section, cStart, &globalvcdof[1]));
236:   }
237:   PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, globalvcdof, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
238:   if (globalvcdof[0]) {
239:     *sStart = vStart;
240:     *sEnd   = vEnd;
241:     if (globalvcdof[0] == cdim) *ft = PETSC_VTK_POINT_VECTOR_FIELD;
242:     else *ft = PETSC_VTK_POINT_FIELD;
243:   } else if (globalvcdof[1]) {
244:     *sStart = cStart;
245:     *sEnd   = cEnd;
246:     if (globalvcdof[1] == cdim) *ft = PETSC_VTK_CELL_VECTOR_FIELD;
247:     else *ft = PETSC_VTK_CELL_FIELD;
248:   } else {
249:     if (field >= 0) {
250:       const char *fieldname;

252:       PetscCall(PetscSectionGetFieldName(section, field, &fieldname));
253:       PetscCall(PetscInfo(dm, "Could not classify VTK output type of section field %" PetscInt_FMT " \"%s\"\n", field, fieldname));
254:     } else {
255:       PetscCall(PetscInfo(dm, "Could not classify VTK output type of section\n"));
256:     }
257:   }
258:   PetscFunctionReturn(PETSC_SUCCESS);
259: }

261: static PetscErrorCode DMPlexVecFFT1D_Internal(DM dm, PetscBool removeDC, PetscInt n, Vec u, Vec uhat)
262: {
263:   Mat      FT;
264:   Vec      fftX, fftY;
265:   IS       fftReal;
266:   PetscInt N;

268:   PetscFunctionBegin;
269:   PetscCall(VecDuplicate(u, &uhat));
270:   PetscCall(VecGetSize(u, &N));
271:   PetscCall(MatCreateFFT(PetscObjectComm((PetscObject)dm), 1, &N, MATFFTW, &FT));
272:   PetscCall(MatCreateVecs(FT, &fftX, &fftY));
273:   PetscCall(ISCreateStride(PETSC_COMM_SELF, N, 0, 1, &fftReal));

275:   PetscCall(VecISCopy(fftX, fftReal, SCATTER_FORWARD, u));
276:   PetscCall(MatMult(FT, fftX, fftY));
277:   PetscCall(VecFilter(fftY, PETSC_SMALL));
278:   PetscCall(VecISCopy(fftY, fftReal, SCATTER_REVERSE, uhat));
279:   if (removeDC) PetscCall(VecSetValue(uhat, 0, 0., INSERT_VALUES)); // Remove DC component

281:   PetscCall(MatDestroy(&FT));
282:   PetscCall(VecDestroy(&fftX));
283:   PetscCall(VecDestroy(&fftY));
284:   PetscCall(ISDestroy(&fftReal));
285:   PetscFunctionReturn(PETSC_SUCCESS);
286: }

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

291:   Collective

293:   Input Parameters:
294: + dm     - The `DMPLEX` object
295: . n      - The number of vectors
296: . u      - The array of local vectors
297: - viewer - The `PetscViewer`

299:   Level: advanced

301: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `VecViewFromOptions()`, `VecView()`
302: @*/
303: PetscErrorCode DMPlexVecView1D(DM dm, PetscInt n, Vec u[], PetscViewer viewer)
304: {
305:   DM                  cdm;
306:   PetscDS             ds;
307:   PetscSection        s;
308:   Vec                *uhat;        // Fourier transform of each vector u[i]
309:   Vec                 coordinates; // Local vector of mesh coordinate
310:   const PetscScalar  *coords;      // Coordinate values
311:   const PetscScalar **sol;         // Arrays from each vector u[i]
312:   char              **names;       // Names for each component of each vector
313:   PetscReal          *vals;        // Values at a point for each component of each vector
314:   PetscInt           *Nc;          // Number of components for each field
315:   PetscInt            Ntc;         // Total number of components across all fields
316:   PetscInt            Nw;          // Number of plots
317:   PetscInt            cdof;        // Total number of cell dofs
318:   PetscInt            vdof;        // Total number of vertex dofs
319:   PetscInt            k;           // The Lagrange degree, and drawing mode
320:   PetscInt            Nf, vStart, vEnd, eStart, eEnd;
321:   PetscBool           separateCmp = PETSC_TRUE;  // Plot components of fields
322:   PetscBool           fft         = PETSC_FALSE; // Fourier Transform the field before plotting
323:   PetscBool           removeDC    = PETSC_FALSE; // Remove DC component before plotting

325:   PetscFunctionBegin;
326:   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_view_1d_fft", &fft, NULL));
327:   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_view_1d_components", &separateCmp, NULL));
328:   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_view_1d_remove_dc", &removeDC, NULL));
329:   if (!n) fft = PETSC_FALSE;
330:   if (fft) {
331:     PetscCall(PetscMalloc1(n, &uhat));
332:     for (PetscInt i = 0; i < n; ++i) {
333:       PetscCall(VecDuplicate(u[i], &uhat[i]));
334:       PetscCall(DMPlexVecFFT1D_Internal(dm, removeDC, 1, u[i], uhat[i]));
335:     }
336:   }
337:   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
338:   PetscCall(DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd));
339:   PetscCall(DMGetCoordinateDM(dm, &cdm));
340:   PetscCall(DMGetDS(dm, &ds));
341:   PetscCall(PetscDSGetNumFields(ds, &Nf));
342:   PetscCall(PetscDSGetTotalComponents(ds, &Ntc));
343:   PetscCall(PetscDSGetComponents(ds, &Nc));

345:   PetscCall(DMGetLocalSection(dm, &s));
346:   PetscCall(PetscSectionGetDof(s, eStart, &cdof));
347:   PetscCall(PetscSectionGetDof(s, vStart, &vdof));
348:   PetscCheck(cdof || vdof, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unsupported discretization");
349:   if (cdof && vdof) k = 2;
350:   else if (vdof) k = 1;
351:   else if (cdof) k = 0;

353:   PetscCall(PetscMalloc3(n, &sol, n * Ntc, &names, n * Ntc, &vals));
354:   for (PetscInt i = 0, l = 0; i < n; ++i) {
355:     const char *vname;

357:     PetscCall(PetscObjectGetName((PetscObject)u[i], &vname));
358:     for (PetscInt f = 0; f < Nf; ++f) {
359:       PetscObject disc;
360:       const char *fname;
361:       char        tmpname[PETSC_MAX_PATH_LEN];

363:       PetscCall(PetscDSGetDiscretization(ds, f, &disc));
364:       /* TODO Create names for components */
365:       for (PetscInt c = 0; c < Nc[f]; ++c, ++l) {
366:         PetscCall(PetscObjectGetName(disc, &fname));
367:         PetscCall(PetscStrncpy(tmpname, vname, sizeof(tmpname)));
368:         PetscCall(PetscStrlcat(tmpname, ":", sizeof(tmpname)));
369:         PetscCall(PetscStrlcat(tmpname, fname, sizeof(tmpname)));
370:         PetscCall(PetscStrallocpy(tmpname, &names[l]));
371:       }
372:     }
373:   }

375:   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
376:   PetscCall(VecGetArrayRead(coordinates, &coords));
377:   for (PetscInt i = 0; i < n; ++i) PetscCall(VecGetArrayRead(fft ? uhat[i] : u[i], &sol[i]));

379:   PetscDrawLG *lg;

381:   Nw = separateCmp ? Ntc : 1;
382:   PetscCall(PetscMalloc1(Nw, &lg));
383:   for (PetscInt w = 0; w < Nw; ++w) {
384:     PetscDraw      draw   = NULL;
385:     const PetscInt Nl     = separateCmp ? 1 : Ntc;
386:     PetscInt       vFirst = -1;
387:     PetscInt       field  = 0;
388:     PetscInt       cmp    = 0;
389:     PetscInt       tcmp   = 0;

391:     if (separateCmp) {
392:       for (PetscInt f = 0; f < Nf; ++f) {
393:         PetscInt c;

395:         for (c = 0; c < Nc[f]; ++c, ++tcmp) {
396:           if (tcmp == w) {
397:             cmp = c;
398:             break;
399:           }
400:         }
401:         if (c < Nc[f]) {
402:           field = f;
403:           break;
404:         }
405:       }
406:     }
407:     PetscCall(PetscViewerDrawGetDraw(viewer, w, &draw));
408:     if (!draw) PetscFunctionReturn(PETSC_SUCCESS);
409:     PetscCall(PetscDrawLGCreate(draw, n * Nl, &lg[w]));

411:     PetscCall(PetscDrawLGSetLegend(lg[w], (const char *const *)&names[w]));
412:     switch (k) {
413:     case 0:
414:       for (PetscInt e = eStart; e < eEnd; ++e) {
415:         PetscScalar    *xa, *xb, *svals;
416:         const PetscInt *cone;

418:         PetscCall(DMPlexGetCone(dm, e, &cone));
419:         PetscCall(DMPlexPointLocalRead(cdm, cone[0], coords, &xa));
420:         PetscCall(DMPlexPointLocalRead(cdm, cone[1], coords, &xb));
421:         for (PetscInt i = 0; i < n; ++i) {
422:           if (separateCmp) {
423:             PetscCall(DMPlexPointLocalFieldRead(dm, e, field, sol[i], &svals));
424:             vals[i] = PetscRealPart(svals[cmp]);
425:           } else {
426:             PetscCall(DMPlexPointLocalRead(dm, e, sol[i], &svals));
427:             for (PetscInt l = 0; l < Nl; ++l) vals[i * Nl + l] = PetscRealPart(svals[l]);
428:           }
429:         }
430:         PetscCall(PetscDrawLGAddCommonPoint(lg[w], 0.5 * (PetscRealPart(xa[0]) + PetscRealPart(xb[0])), vals));
431:       }
432:       break;
433:     case 1:
434:       for (PetscInt v = vStart; v < vEnd; ++v) {
435:         PetscScalar *x, *svals;

437:         PetscCall(DMPlexPointLocalRead(cdm, v, coords, &x));
438:         for (PetscInt i = 0; i < n; ++i) {
439:           if (separateCmp) {
440:             PetscCall(DMPlexPointLocalFieldRead(dm, v, field, sol[i], &svals));
441:             vals[i] = PetscRealPart(svals[cmp]);
442:           } else {
443:             PetscCall(DMPlexPointLocalRead(dm, v, sol[i], &svals));
444:             for (PetscInt l = 0; l < Nl; ++l) vals[i * Nl + l] = PetscRealPart(svals[l]);
445:           }
446:         }
447:         PetscCall(PetscDrawLGAddCommonPoint(lg[w], PetscRealPart(x[0]), vals));
448:       }
449:       break;
450:     case 2:
451:       for (PetscInt e = eStart; e < eEnd; ++e) {
452:         PetscScalar    *xa, *xb, *svals;
453:         const PetscInt *cone;

455:         PetscCall(DMPlexGetCone(dm, e, &cone));
456:         PetscCall(DMPlexPointLocalRead(cdm, cone[0], coords, &xa));
457:         PetscCall(DMPlexPointLocalRead(cdm, cone[1], coords, &xb));
458:         if (e == eStart) vFirst = cone[0];
459:         for (PetscInt i = 0; i < n; ++i) {
460:           if (separateCmp) {
461:             PetscCall(DMPlexPointLocalFieldRead(dm, cone[0], field, sol[i], &svals));
462:             vals[i] = PetscRealPart(svals[cmp]);
463:           } else {
464:             PetscCall(DMPlexPointLocalRead(dm, cone[0], sol[i], &svals));
465:             for (PetscInt l = 0; l < Nl; ++l) vals[i * Nl + l] = PetscRealPart(svals[l]);
466:           }
467:         }
468:         PetscCall(PetscDrawLGAddCommonPoint(lg[w], PetscRealPart(xa[0]), vals));
469:         if (e == eEnd - 1 && cone[1] != vFirst) {
470:           for (PetscInt i = 0; i < n; ++i) {
471:             if (separateCmp) {
472:               PetscCall(DMPlexPointLocalFieldRead(dm, e, field, sol[i], &svals));
473:               vals[i] = PetscRealPart(svals[cmp]);
474:             } else {
475:               PetscCall(DMPlexPointLocalRead(dm, e, sol[i], &svals));
476:               for (PetscInt l = 0; l < Nl; ++l) vals[i * Nl + l] = PetscRealPart(svals[l]);
477:             }
478:           }
479:           PetscCall(PetscDrawLGAddCommonPoint(lg[w], 0.5 * (PetscRealPart(xa[0]) + PetscRealPart(xb[0])), vals));
480:           for (PetscInt i = 0; i < n; ++i) {
481:             if (separateCmp) {
482:               PetscCall(DMPlexPointLocalFieldRead(dm, cone[1], field, sol[i], &svals));
483:               vals[i] = PetscRealPart(svals[cmp]);
484:             } else {
485:               PetscCall(DMPlexPointLocalRead(dm, cone[1], sol[i], &svals));
486:               for (PetscInt l = 0; l < Nl; ++l) vals[i * Nl + l] = PetscRealPart(svals[l]);
487:             }
488:           }
489:           PetscCall(PetscDrawLGAddCommonPoint(lg[w], PetscRealPart(xb[0]), vals));
490:         }
491:       }
492:       break;
493:     default:
494:       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid value of k: %" PetscInt_FMT, k);
495:     }
496:   }
497:   PetscCall(VecRestoreArrayRead(coordinates, &coords));
498:   for (PetscInt i = 0; i < n; ++i) PetscCall(VecRestoreArrayRead(fft ? uhat[i] : u[i], &sol[i]));
499:   if (fft) {
500:     for (PetscInt i = 0; i < n; ++i) PetscCall(VecDestroy(&uhat[i]));
501:     PetscCall(PetscFree(uhat));
502:   }
503:   for (PetscInt l = 0; l < n * Ntc; ++l) PetscCall(PetscFree(names[l]));
504:   PetscCall(PetscFree3(sol, names, vals));
505:   for (PetscInt w = 0; w < Nw; ++w) {
506:     PetscCall(PetscDrawLGDraw(lg[w]));
507:     PetscCall(PetscDrawLGDestroy(&lg[w]));
508:   }
509:   PetscCall(PetscFree(lg));
510:   PetscFunctionReturn(PETSC_SUCCESS);
511: }

513: static PetscErrorCode VecView_Plex_Local_Draw_1D(Vec u, PetscViewer viewer)
514: {
515:   DM dm;

517:   PetscFunctionBegin;
518:   PetscCall(VecGetDM(u, &dm));
519:   PetscCall(DMPlexVecView1D(dm, 1, &u, viewer));
520:   PetscFunctionReturn(PETSC_SUCCESS);
521: }

523: static PetscErrorCode VecView_Plex_Local_Draw_2D(Vec v, PetscViewer viewer)
524: {
525:   DM                 dm;
526:   PetscSection       s;
527:   PetscDraw          draw, popup;
528:   DM                 cdm;
529:   PetscSection       coordSection;
530:   Vec                coordinates;
531:   const PetscScalar *array;
532:   PetscReal          lbound[3], ubound[3];
533:   PetscReal          vbound[2], time;
534:   PetscBool          flg;
535:   PetscInt           dim, Nf, f, Nc, comp, vStart, vEnd, cStart, cEnd, c, N, level, step, w = 0;
536:   const char        *name;
537:   char               title[PETSC_MAX_PATH_LEN];

539:   PetscFunctionBegin;
540:   PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
541:   PetscCall(VecGetDM(v, &dm));
542:   PetscCall(DMGetCoordinateDim(dm, &dim));
543:   PetscCall(DMGetLocalSection(dm, &s));
544:   PetscCall(PetscSectionGetNumFields(s, &Nf));
545:   PetscCall(DMGetCoarsenLevel(dm, &level));
546:   PetscCall(DMGetCoordinateDM(dm, &cdm));
547:   PetscCall(DMGetLocalSection(cdm, &coordSection));
548:   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
549:   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
550:   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));

552:   PetscCall(PetscObjectGetName((PetscObject)v, &name));
553:   PetscCall(DMGetOutputSequenceNumber(dm, &step, &time));

555:   PetscCall(VecGetLocalSize(coordinates, &N));
556:   PetscCall(DMGetBoundingBox(dm, lbound, ubound));
557:   PetscCall(PetscDrawClear(draw));

559:   /* Could implement something like DMDASelectFields() */
560:   for (f = 0; f < Nf; ++f) {
561:     DM          fdm = dm;
562:     Vec         fv  = v;
563:     IS          fis;
564:     char        prefix[PETSC_MAX_PATH_LEN];
565:     const char *fname;

567:     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
568:     PetscCall(PetscSectionGetFieldName(s, f, &fname));

570:     if (v->hdr.prefix) PetscCall(PetscStrncpy(prefix, v->hdr.prefix, sizeof(prefix)));
571:     else prefix[0] = '\0';
572:     if (Nf > 1) {
573:       PetscCall(DMCreateSubDM(dm, 1, &f, &fis, &fdm));
574:       PetscCall(VecGetSubVector(v, fis, &fv));
575:       PetscCall(PetscStrlcat(prefix, fname, sizeof(prefix)));
576:       PetscCall(PetscStrlcat(prefix, "_", sizeof(prefix)));
577:     }
578:     for (comp = 0; comp < Nc; ++comp, ++w) {
579:       PetscInt nmax = 2;

581:       PetscCall(PetscViewerDrawGetDraw(viewer, w, &draw));
582:       if (Nc > 1) PetscCall(PetscSNPrintf(title, sizeof(title), "%s:%s_%" PetscInt_FMT " Step: %" PetscInt_FMT " Time: %.4g", name, fname, comp, step, (double)time));
583:       else PetscCall(PetscSNPrintf(title, sizeof(title), "%s:%s Step: %" PetscInt_FMT " Time: %.4g", name, fname, step, (double)time));
584:       PetscCall(PetscDrawSetTitle(draw, title));

586:       /* TODO Get max and min only for this component */
587:       PetscCall(PetscOptionsGetRealArray(NULL, prefix, "-vec_view_bounds", vbound, &nmax, &flg));
588:       if (!flg) {
589:         PetscCall(VecMin(fv, NULL, &vbound[0]));
590:         PetscCall(VecMax(fv, NULL, &vbound[1]));
591:         if (vbound[1] <= vbound[0]) vbound[1] = vbound[0] + 1.0;
592:       }

594:       PetscCall(PetscDrawGetPopup(draw, &popup));
595:       PetscCall(PetscDrawScalePopup(popup, vbound[0], vbound[1]));
596:       PetscCall(PetscDrawSetCoordinates(draw, lbound[0], lbound[1], ubound[0], ubound[1]));
597:       PetscCall(VecGetArrayRead(fv, &array));
598:       for (c = cStart; c < cEnd; ++c) {
599:         DMPolytopeType     ct;
600:         PetscScalar       *coords = NULL, *a = NULL;
601:         const PetscScalar *coords_arr;
602:         PetscBool          isDG;
603:         PetscInt           numCoords;
604:         int                color[4] = {-1, -1, -1, -1};

606:         PetscCall(DMPlexGetCellType(dm, c, &ct));
607:         PetscCall(DMPlexPointLocalRead(fdm, c, array, &a));
608:         if (a) {
609:           color[0] = PetscDrawRealToColor(PetscRealPart(a[comp]), vbound[0], vbound[1]);
610:           color[1] = color[2] = color[3] = color[0];
611:         } else {
612:           PetscScalar *vals = NULL;
613:           PetscInt     numVals;

615:           PetscCall(DMPlexVecGetClosure(fdm, NULL, fv, c, &numVals, &vals));
616:           if (!numVals) {
617:             PetscCall(DMPlexVecRestoreClosure(fdm, NULL, fv, c, &numVals, &vals));
618:             continue;
619:           }
620:           PetscCheck(numVals % Nc == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of components %" PetscInt_FMT " does not divide the number of values in the closure %" PetscInt_FMT, Nc, numVals);
621:           switch (numVals / Nc) {
622:           case 1: /* P1 Clamped Segment Prism */
623:           case 2: /* P1 Segment Prism, P2 Clamped Segment Prism */
624:             PetscCheck(ct == DM_POLYTOPE_SEG_PRISM_TENSOR, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell should be a tensor segment, but it is a %s", DMPolytopeTypes[ct]);
625:             for (PetscInt va = 0; va < numVals / Nc; ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va * Nc + comp]), vbound[0], vbound[1]);
626:             break;
627:           case 3: /* P1 Triangle */
628:           case 4: /* P1 Quadrangle */
629:             PetscCheck(ct == DM_POLYTOPE_TRIANGLE || ct == DM_POLYTOPE_QUADRILATERAL || ct == DM_POLYTOPE_SEG_PRISM_TENSOR, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell should be a triangle or quad, but it is a %s", DMPolytopeTypes[ct]);
630:             for (PetscInt va = 0; va < numVals / Nc; ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va * Nc + comp]), vbound[0], vbound[1]);
631:             break;
632:           case 6: /* P2 Triangle */
633:           case 8: /* P2 Quadrangle */
634:             PetscCheck(ct == DM_POLYTOPE_TRIANGLE || ct == DM_POLYTOPE_QUADRILATERAL || ct == DM_POLYTOPE_SEG_PRISM_TENSOR, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell should be a triangle or quad, but it is a %s", DMPolytopeTypes[ct]);
635:             for (PetscInt va = 0; va < numVals / (Nc * 2); ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va * Nc + comp + numVals / (Nc * 2)]), vbound[0], vbound[1]);
636:             break;
637:           default:
638:             SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of values for cell closure %" PetscInt_FMT " cannot be handled", numVals / Nc);
639:           }
640:           PetscCall(DMPlexVecRestoreClosure(fdm, NULL, fv, c, &numVals, &vals));
641:         }
642:         PetscCall(DMPlexGetCellCoordinates(dm, c, &isDG, &numCoords, &coords_arr, &coords));
643:         switch (numCoords) {
644:         case 6:
645:         case 12: /* Localized triangle */
646:           PetscCall(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]));
647:           break;
648:         case 8:
649:         case 16: /* Localized quadrilateral */
650:           if (ct == DM_POLYTOPE_SEG_PRISM_TENSOR) {
651:             PetscCall(PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscMax(color[0], color[1])));
652:           } else {
653:             PetscCall(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]));
654:             PetscCall(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]));
655:           }
656:           break;
657:         default:
658:           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %" PetscInt_FMT " coordinates", numCoords);
659:         }
660:         PetscCall(DMPlexRestoreCellCoordinates(dm, c, &isDG, &numCoords, &coords_arr, &coords));
661:       }
662:       PetscCall(VecRestoreArrayRead(fv, &array));
663:       PetscCall(PetscDrawFlush(draw));
664:       PetscCall(PetscDrawPause(draw));
665:       PetscCall(PetscDrawSave(draw));
666:     }
667:     if (Nf > 1) {
668:       PetscCall(VecRestoreSubVector(v, fis, &fv));
669:       PetscCall(ISDestroy(&fis));
670:       PetscCall(DMDestroy(&fdm));
671:     }
672:   }
673:   PetscFunctionReturn(PETSC_SUCCESS);
674: }

676: static PetscErrorCode VecView_Plex_Local_Draw(Vec v, PetscViewer viewer)
677: {
678:   DM        dm;
679:   PetscDraw draw;
680:   PetscInt  dim;
681:   PetscBool isnull;

683:   PetscFunctionBegin;
684:   PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
685:   PetscCall(PetscDrawIsNull(draw, &isnull));
686:   if (isnull) PetscFunctionReturn(PETSC_SUCCESS);

688:   PetscCall(VecGetDM(v, &dm));
689:   PetscCall(DMGetCoordinateDim(dm, &dim));
690:   switch (dim) {
691:   case 1:
692:     PetscCall(VecView_Plex_Local_Draw_1D(v, viewer));
693:     break;
694:   case 2:
695:     PetscCall(VecView_Plex_Local_Draw_2D(v, viewer));
696:     break;
697:   default:
698:     SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_SUP, "Cannot draw meshes of dimension %" PetscInt_FMT ". Try PETSCVIEWERGLVIS", dim);
699:   }
700:   PetscFunctionReturn(PETSC_SUCCESS);
701: }

703: static PetscErrorCode VecView_Plex_Local_VTK(Vec v, PetscViewer viewer)
704: {
705:   DM                      dm;
706:   Vec                     locv;
707:   const char             *name;
708:   PetscSection            section;
709:   PetscInt                pStart, pEnd;
710:   PetscInt                numFields;
711:   PetscViewerVTKFieldType ft;

713:   PetscFunctionBegin;
714:   PetscCall(VecGetDM(v, &dm));
715:   PetscCall(DMCreateLocalVector(dm, &locv)); /* VTK viewer requires exclusive ownership of the vector */
716:   PetscCall(PetscObjectGetName((PetscObject)v, &name));
717:   PetscCall(PetscObjectSetName((PetscObject)locv, name));
718:   PetscCall(VecCopy(v, locv));
719:   PetscCall(DMGetLocalSection(dm, &section));
720:   PetscCall(PetscSectionGetNumFields(section, &numFields));
721:   if (!numFields) {
722:     PetscCall(DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft));
723:     PetscCall(PetscViewerVTKAddField(viewer, (PetscObject)dm, DMPlexVTKWriteAll, PETSC_DEFAULT, ft, PETSC_TRUE, (PetscObject)locv));
724:   } else {
725:     for (PetscInt f = 0; f < numFields; f++) {
726:       PetscCall(DMPlexGetFieldType_Internal(dm, section, f, &pStart, &pEnd, &ft));
727:       if (ft == PETSC_VTK_INVALID) continue;
728:       PetscCall(PetscObjectReference((PetscObject)locv));
729:       PetscCall(PetscViewerVTKAddField(viewer, (PetscObject)dm, DMPlexVTKWriteAll, f, ft, PETSC_TRUE, (PetscObject)locv));
730:     }
731:     PetscCall(VecDestroy(&locv));
732:   }
733:   PetscFunctionReturn(PETSC_SUCCESS);
734: }

736: PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
737: {
738:   DM        dm;
739:   PetscBool isvtk, ishdf5, isdraw, isglvis, iscgns, ispython;

741:   PetscFunctionBegin;
742:   PetscCall(VecGetDM(v, &dm));
743:   PetscCheck(dm, PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
744:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERVTK, &isvtk));
745:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
746:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
747:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERGLVIS, &isglvis));
748:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERCGNS, &iscgns));
749:   PetscCall(PetscObjectHasFunction((PetscObject)viewer, "PetscViewerPythonViewObject_C", &ispython));
750:   if (isvtk || ishdf5 || isdraw || isglvis || iscgns || ispython) {
751:     PetscInt    numFields;
752:     PetscObject fe;
753:     PetscBool   fem  = PETSC_FALSE;
754:     Vec         locv = v;
755:     const char *name;
756:     PetscInt    step;
757:     PetscReal   time;

759:     PetscCall(DMGetNumFields(dm, &numFields));
760:     for (PetscInt i = 0; i < numFields; i++) {
761:       PetscCall(DMGetField(dm, i, NULL, &fe));
762:       if (fe->classid == PETSCFE_CLASSID) {
763:         fem = PETSC_TRUE;
764:         break;
765:       }
766:     }
767:     if (fem) {
768:       PetscObject isZero;

770:       PetscCall(DMGetLocalVector(dm, &locv));
771:       PetscCall(PetscObjectGetName((PetscObject)v, &name));
772:       PetscCall(PetscObjectSetName((PetscObject)locv, name));
773:       PetscCall(PetscObjectQuery((PetscObject)v, "__Vec_bc_zero__", &isZero));
774:       PetscCall(PetscObjectCompose((PetscObject)locv, "__Vec_bc_zero__", isZero));
775:       PetscCall(VecCopy(v, locv));
776:       PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
777:       PetscCall(DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL));
778:     }
779:     if (isvtk) {
780:       PetscCall(VecView_Plex_Local_VTK(locv, viewer));
781:     } else if (ishdf5) {
782: #if PetscDefined(HAVE_HDF5)
783:       PetscCall(VecView_Plex_Local_HDF5_Internal(locv, viewer));
784: #else
785:       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
786: #endif
787:     } else if (isdraw) {
788:       PetscCall(VecView_Plex_Local_Draw(locv, viewer));
789:     } else if (ispython) {
790:       PetscCall(PetscViewerPythonViewObject(viewer, (PetscObject)locv));
791:     } else if (isglvis) {
792:       PetscCall(DMGetOutputSequenceNumber(dm, &step, NULL));
793:       PetscCall(PetscViewerGLVisSetSnapId(viewer, step));
794:       PetscCall(VecView_GLVis(locv, viewer));
795:     } else if (iscgns) {
796: #if PetscDefined(HAVE_CGNS)
797:       PetscCall(VecView_Plex_Local_CGNS(locv, viewer));
798: #else
799:       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "CGNS not supported in this build.\nPlease reconfigure using --download-cgns");
800: #endif
801:     }
802:     if (fem) {
803:       PetscCall(PetscObjectCompose((PetscObject)locv, "__Vec_bc_zero__", NULL));
804:       PetscCall(DMRestoreLocalVector(dm, &locv));
805:     }
806:   } else {
807:     PetscBool isseq;

809:     PetscCall(PetscObjectTypeCompare((PetscObject)v, VECSEQ, &isseq));
810:     if (isseq) PetscCall(VecView_Seq(v, viewer));
811:     else PetscCall(VecView_MPI(v, viewer));
812:   }
813:   PetscFunctionReturn(PETSC_SUCCESS);
814: }

816: PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
817: {
818:   DM        dm;
819:   PetscBool isvtk, ishdf5, isdraw, isglvis, isexodusii, iscgns, ispython;

821:   PetscFunctionBegin;
822:   PetscCall(VecGetDM(v, &dm));
823:   PetscCheck(dm, PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
824:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERVTK, &isvtk));
825:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
826:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
827:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERGLVIS, &isglvis));
828:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERCGNS, &iscgns));
829:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWEREXODUSII, &isexodusii));
830:   PetscCall(PetscObjectHasFunction((PetscObject)viewer, "PetscViewerPythonViewObject_C", &ispython));
831:   if (isvtk || isdraw || isglvis || iscgns || ispython) {
832:     Vec         locv;
833:     PetscObject isZero;
834:     const char *name;

836:     PetscCall(DMGetLocalVector(dm, &locv));
837:     PetscCall(PetscObjectGetName((PetscObject)v, &name));
838:     PetscCall(PetscObjectSetName((PetscObject)locv, name));
839:     PetscCall(DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv));
840:     PetscCall(DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv));
841:     PetscCall(PetscObjectQuery((PetscObject)v, "__Vec_bc_zero__", &isZero));
842:     PetscCall(PetscObjectCompose((PetscObject)locv, "__Vec_bc_zero__", isZero));
843:     PetscCall(VecView_Plex_Local(locv, viewer));
844:     PetscCall(PetscObjectCompose((PetscObject)locv, "__Vec_bc_zero__", NULL));
845:     PetscCall(DMRestoreLocalVector(dm, &locv));
846:   } else if (ishdf5) {
847: #if PetscDefined(HAVE_HDF5)
848:     PetscCall(VecView_Plex_HDF5_Internal(v, viewer));
849: #else
850:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
851: #endif
852:   } else if (isexodusii) {
853: #if PetscDefined(HAVE_EXODUSII)
854:     PetscCall(VecView_PlexExodusII_Internal(v, viewer));
855: #else
856:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "ExodusII not supported in this build.\nPlease reconfigure using --download-exodusii");
857: #endif
858:   } else {
859:     PetscBool isseq;

861:     PetscCall(PetscObjectTypeCompare((PetscObject)v, VECSEQ, &isseq));
862:     if (isseq) PetscCall(VecView_Seq(v, viewer));
863:     else PetscCall(VecView_MPI(v, viewer));
864:   }
865:   PetscFunctionReturn(PETSC_SUCCESS);
866: }

868: PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
869: {
870:   DM                dm;
871:   MPI_Comm          comm;
872:   PetscViewerFormat format;
873:   Vec               v;
874:   PetscBool         isvtk, ishdf5;

876:   PetscFunctionBegin;
877:   PetscCall(VecGetDM(originalv, &dm));
878:   PetscCall(PetscObjectGetComm((PetscObject)originalv, &comm));
879:   PetscCheck(dm, comm, PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
880:   PetscCall(PetscViewerGetFormat(viewer, &format));
881:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
882:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERVTK, &isvtk));
883:   if (format == PETSC_VIEWER_NATIVE) {
884:     /* Natural ordering is the common case for DMDA, NATIVE means plain vector, for PLEX is the opposite */
885:     /* this need a better fix */
886:     if (dm->useNatural) {
887:       const char *vecname;
888:       PetscInt    n, nroots;

890:       PetscCheck(dm->sfNatural, comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
891:       PetscCall(VecGetLocalSize(originalv, &n));
892:       PetscCall(PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL));
893:       PetscCheck(n == nroots, comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
894:       PetscCall(DMPlexCreateNaturalVector(dm, &v));
895:       PetscCall(DMPlexGlobalToNaturalBegin(dm, originalv, v));
896:       PetscCall(DMPlexGlobalToNaturalEnd(dm, originalv, v));
897:       PetscCall(PetscObjectGetName((PetscObject)originalv, &vecname));
898:       PetscCall(PetscObjectSetName((PetscObject)v, vecname));
899:     } else v = originalv;
900:   } else v = originalv;

902:   if (ishdf5) {
903: #if PetscDefined(HAVE_HDF5)
904:     PetscCall(VecView_Plex_HDF5_Native_Internal(v, viewer));
905: #else
906:     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
907: #endif
908:   } else if (isvtk) {
909:     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
910:   } else {
911:     PetscBool isseq;

913:     PetscCall(PetscObjectTypeCompare((PetscObject)v, VECSEQ, &isseq));
914:     if (isseq) PetscCall(VecView_Seq(v, viewer));
915:     else PetscCall(VecView_MPI(v, viewer));
916:   }
917:   if (v != originalv) PetscCall(VecDestroy(&v));
918:   PetscFunctionReturn(PETSC_SUCCESS);
919: }

921: PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
922: {
923:   DM        dm;
924:   PetscBool ishdf5;

926:   PetscFunctionBegin;
927:   PetscCall(VecGetDM(v, &dm));
928:   PetscCheck(dm, PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
929:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
930:   if (ishdf5) {
931:     DM          dmBC;
932:     Vec         gv;
933:     const char *name;

935:     PetscCall(DMGetOutputDM(dm, &dmBC));
936:     PetscCall(DMGetGlobalVector(dmBC, &gv));
937:     PetscCall(PetscObjectGetName((PetscObject)v, &name));
938:     PetscCall(PetscObjectSetName((PetscObject)gv, name));
939:     PetscCall(VecLoad_Default(gv, viewer));
940:     PetscCall(DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v));
941:     PetscCall(DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v));
942:     PetscCall(DMRestoreGlobalVector(dmBC, &gv));
943:   } else PetscCall(VecLoad_Default(v, viewer));
944:   PetscFunctionReturn(PETSC_SUCCESS);
945: }

947: PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
948: {
949:   DM        dm;
950:   PetscBool ishdf5, isexodusii, iscgns;

952:   PetscFunctionBegin;
953:   PetscCall(VecGetDM(v, &dm));
954:   PetscCheck(dm, PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
955:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
956:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWEREXODUSII, &isexodusii));
957:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERCGNS, &iscgns));
958:   if (ishdf5) {
959: #if PetscDefined(HAVE_HDF5)
960:     PetscCall(VecLoad_Plex_HDF5_Internal(v, viewer));
961: #else
962:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
963: #endif
964:   } else if (isexodusii) {
965: #if PetscDefined(HAVE_EXODUSII)
966:     PetscCall(VecLoad_PlexExodusII_Internal(v, viewer));
967: #else
968:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "ExodusII not supported in this build.\nPlease reconfigure using --download-exodusii");
969: #endif
970:   } else if (iscgns) {
971: #if PetscDefined(HAVE_CGNS)
972:     PetscCall(VecLoad_Plex_CGNS_Internal(v, viewer));
973: #else
974:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "CGNS not supported in this build.\nPlease reconfigure using --download-cgns");
975: #endif
976:   } else PetscCall(VecLoad_Default(v, viewer));
977:   PetscFunctionReturn(PETSC_SUCCESS);
978: }

980: PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
981: {
982:   DM                dm;
983:   PetscViewerFormat format;
984:   PetscBool         ishdf5;

986:   PetscFunctionBegin;
987:   PetscCall(VecGetDM(originalv, &dm));
988:   PetscCheck(dm, PetscObjectComm((PetscObject)originalv), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
989:   PetscCall(PetscViewerGetFormat(viewer, &format));
990:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
991:   if (format == PETSC_VIEWER_NATIVE) {
992:     if (dm->useNatural) {
993:       if (dm->sfNatural) {
994:         if (ishdf5) {
995: #if PetscDefined(HAVE_HDF5)
996:           Vec         v;
997:           const char *vecname;

999:           PetscCall(DMPlexCreateNaturalVector(dm, &v));
1000:           PetscCall(PetscObjectGetName((PetscObject)originalv, &vecname));
1001:           PetscCall(PetscObjectSetName((PetscObject)v, vecname));
1002:           PetscCall(VecLoad_Plex_HDF5_Native_Internal(v, viewer));
1003:           PetscCall(DMPlexNaturalToGlobalBegin(dm, v, originalv));
1004:           PetscCall(DMPlexNaturalToGlobalEnd(dm, v, originalv));
1005:           PetscCall(VecDestroy(&v));
1006: #else
1007:           SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1008: #endif
1009:         } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
1010:       }
1011:     } else PetscCall(VecLoad_Default(originalv, viewer));
1012:   }
1013:   PetscFunctionReturn(PETSC_SUCCESS);
1014: }

1016: PETSC_UNUSED static PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
1017: {
1018:   PetscSection       coordSection;
1019:   Vec                coordinates;
1020:   DMLabel            depthLabel, celltypeLabel;
1021:   const char        *name[4];
1022:   const PetscScalar *a;
1023:   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;

1025:   PetscFunctionBegin;
1026:   PetscCall(DMGetDimension(dm, &dim));
1027:   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
1028:   PetscCall(DMGetCoordinateSection(dm, &coordSection));
1029:   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
1030:   PetscCall(DMPlexGetCellTypeLabel(dm, &celltypeLabel));
1031:   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
1032:   PetscCall(PetscSectionGetChart(coordSection, &pStart, &pEnd));
1033:   PetscCall(VecGetArrayRead(coordinates, &a));
1034:   name[0]       = "vertex";
1035:   name[1]       = "edge";
1036:   name[dim - 1] = "face";
1037:   name[dim]     = "cell";
1038:   for (c = cStart; c < cEnd; ++c) {
1039:     PetscInt *closure = NULL;
1040:     PetscInt  closureSize, cl, ct;

1042:     PetscCall(DMLabelGetValue(celltypeLabel, c, &ct));
1043:     PetscCall(PetscViewerASCIIPrintf(viewer, "Geometry for cell %" PetscInt_FMT " polytope type %s:\n", c, DMPolytopeTypes[ct]));
1044:     PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure));
1045:     PetscCall(PetscViewerASCIIPushTab(viewer));
1046:     for (cl = 0; cl < closureSize * 2; cl += 2) {
1047:       PetscInt point = closure[cl], depth, dof, off, d, p;

1049:       if (point < pStart || point >= pEnd) continue;
1050:       PetscCall(PetscSectionGetDof(coordSection, point, &dof));
1051:       if (!dof) continue;
1052:       PetscCall(DMLabelGetValue(depthLabel, point, &depth));
1053:       PetscCall(PetscSectionGetOffset(coordSection, point, &off));
1054:       PetscCall(PetscViewerASCIIPrintf(viewer, "%s %" PetscInt_FMT " coords:", name[depth], point));
1055:       for (p = 0; p < dof / dim; ++p) {
1056:         PetscCall(PetscViewerASCIIPrintf(viewer, " ("));
1057:         for (d = 0; d < dim; ++d) {
1058:           if (d > 0) PetscCall(PetscViewerASCIIPrintf(viewer, ", "));
1059:           PetscCall(PetscViewerASCIIPrintf(viewer, "%g", (double)PetscRealPart(a[off + p * dim + d])));
1060:         }
1061:         PetscCall(PetscViewerASCIIPrintf(viewer, ")"));
1062:       }
1063:       PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
1064:     }
1065:     PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure));
1066:     PetscCall(PetscViewerASCIIPopTab(viewer));
1067:   }
1068:   PetscCall(VecRestoreArrayRead(coordinates, &a));
1069:   PetscFunctionReturn(PETSC_SUCCESS);
1070: }

1072: typedef enum {
1073:   CS_CARTESIAN,
1074:   CS_POLAR,
1075:   CS_CYLINDRICAL,
1076:   CS_SPHERICAL
1077: } CoordSystem;
1078: const char *CoordSystems[] = {"cartesian", "polar", "cylindrical", "spherical", "CoordSystem", "CS_", NULL};

1080: static PetscErrorCode DMPlexView_Ascii_Coordinates(PetscViewer viewer, CoordSystem cs, PetscInt dim, const PetscScalar x[])
1081: {
1082:   PetscFunctionBegin;
1083:   if (dim > 3) {
1084:     for (PetscInt i = 0; i < dim; ++i) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " %g", (double)PetscRealPart(x[i])));
1085:   } else {
1086:     PetscReal coords[3], trcoords[3] = {0., 0., 0.};

1088:     for (PetscInt i = 0; i < dim; ++i) coords[i] = PetscRealPart(x[i]);
1089:     switch (cs) {
1090:     case CS_CARTESIAN:
1091:       for (PetscInt i = 0; i < dim; ++i) trcoords[i] = coords[i];
1092:       break;
1093:     case CS_POLAR:
1094:       PetscCheck(dim == 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Polar coordinates are for 2 dimension, not %" PetscInt_FMT, dim);
1095:       trcoords[0] = PetscSqrtReal(PetscSqr(coords[0]) + PetscSqr(coords[1]));
1096:       trcoords[1] = PetscAtan2Real(coords[1], coords[0]);
1097:       break;
1098:     case CS_CYLINDRICAL:
1099:       PetscCheck(dim == 3, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cylindrical coordinates are for 3 dimension, not %" PetscInt_FMT, dim);
1100:       trcoords[0] = PetscSqrtReal(PetscSqr(coords[0]) + PetscSqr(coords[1]));
1101:       trcoords[1] = PetscAtan2Real(coords[1], coords[0]);
1102:       trcoords[2] = coords[2];
1103:       break;
1104:     case CS_SPHERICAL:
1105:       PetscCheck(dim == 3, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Spherical coordinates are for 3 dimension, not %" PetscInt_FMT, dim);
1106:       trcoords[0] = PetscSqrtReal(PetscSqr(coords[0]) + PetscSqr(coords[1]) + PetscSqr(coords[2]));
1107:       trcoords[1] = PetscAtan2Real(PetscSqrtReal(PetscSqr(coords[0]) + PetscSqr(coords[1])), coords[2]);
1108:       trcoords[2] = PetscAtan2Real(coords[1], coords[0]);
1109:       break;
1110:     }
1111:     for (PetscInt i = 0; i < dim; ++i) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " %g", (double)trcoords[i]));
1112:   }
1113:   PetscFunctionReturn(PETSC_SUCCESS);
1114: }

1116: static PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
1117: {
1118:   DM_Plex          *mesh = (DM_Plex *)dm->data;
1119:   DM                cdm, cdmCell;
1120:   PetscSection      coordSection, coordSectionCell;
1121:   Vec               coordinates, coordinatesCell;
1122:   PetscViewerFormat format;

1124:   PetscFunctionBegin;
1125:   PetscCall(PetscViewerGetFormat(viewer, &format));
1126:   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
1127:     const char *name;
1128:     PetscInt    dim, cellHeight, maxConeSize, maxSupportSize;
1129:     PetscInt    pStart, pEnd, p, numLabels, l;
1130:     PetscMPIInt rank, size;

1132:     PetscCall(DMGetCoordinateDM(dm, &cdm));
1133:     PetscCall(DMGetCoordinateSection(dm, &coordSection));
1134:     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
1135:     PetscCall(DMGetCellCoordinateDM(dm, &cdmCell));
1136:     PetscCall(DMGetCellCoordinateSection(dm, &coordSectionCell));
1137:     PetscCall(DMGetCellCoordinatesLocal(dm, &coordinatesCell));
1138:     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
1139:     PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
1140:     PetscCall(PetscObjectGetName((PetscObject)dm, &name));
1141:     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
1142:     PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
1143:     PetscCall(DMGetDimension(dm, &dim));
1144:     PetscCall(DMPlexGetVTKCellHeight(dm, &cellHeight));
1145:     if (name) PetscCall(PetscViewerASCIIPrintf(viewer, "%s in %" PetscInt_FMT " dimension%s:\n", name, dim, dim == 1 ? "" : "s"));
1146:     else PetscCall(PetscViewerASCIIPrintf(viewer, "Mesh in %" PetscInt_FMT " dimension%s:\n", dim, dim == 1 ? "" : "s"));
1147:     if (cellHeight) PetscCall(PetscViewerASCIIPrintf(viewer, "  Cells are at height %" PetscInt_FMT "\n", cellHeight));
1148:     PetscCall(PetscViewerASCIIPrintf(viewer, "Supports:\n"));
1149:     PetscCall(PetscViewerASCIIPushSynchronized(viewer));
1150:     PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max support size: %" PetscInt_FMT "\n", rank, maxSupportSize));
1151:     for (p = pStart; p < pEnd; ++p) {
1152:       PetscInt dof, off, s;

1154:       PetscCall(PetscSectionGetDof(mesh->supportSection, p, &dof));
1155:       PetscCall(PetscSectionGetOffset(mesh->supportSection, p, &off));
1156:       for (s = off; s < off + dof; ++s) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %" PetscInt_FMT " ----> %" PetscInt_FMT "\n", rank, p, mesh->supports[s]));
1157:     }
1158:     PetscCall(PetscViewerFlush(viewer));
1159:     PetscCall(PetscViewerASCIIPrintf(viewer, "Cones:\n"));
1160:     PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max cone size: %" PetscInt_FMT "\n", rank, maxConeSize));
1161:     for (p = pStart; p < pEnd; ++p) {
1162:       PetscInt dof, off, c;

1164:       PetscCall(PetscSectionGetDof(mesh->coneSection, p, &dof));
1165:       PetscCall(PetscSectionGetOffset(mesh->coneSection, p, &off));
1166:       for (c = off; c < off + dof; ++c) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %" PetscInt_FMT " <---- %" PetscInt_FMT " (%" PetscInt_FMT ")\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]));
1167:     }
1168:     PetscCall(PetscViewerFlush(viewer));
1169:     PetscCall(PetscViewerASCIIPopSynchronized(viewer));
1170:     if (coordSection && coordinates) {
1171:       CoordSystem        cs = CS_CARTESIAN;
1172:       const PetscScalar *array, *arrayCell = NULL;
1173:       PetscInt           Nf, Nc, pvStart, pvEnd, pcStart = PETSC_INT_MAX, pcEnd = PETSC_INT_MIN, pStart, pEnd, p;
1174:       PetscMPIInt        rank;
1175:       const char        *name;

1177:       PetscCall(PetscOptionsGetEnum(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_coord_system", CoordSystems, (PetscEnum *)&cs, NULL));
1178:       PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank));
1179:       PetscCall(PetscSectionGetNumFields(coordSection, &Nf));
1180:       PetscCheck(Nf == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Coordinate section should have 1 field, not %" PetscInt_FMT, Nf);
1181:       PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &Nc));
1182:       PetscCall(PetscSectionGetChart(coordSection, &pvStart, &pvEnd));
1183:       if (coordSectionCell) PetscCall(PetscSectionGetChart(coordSectionCell, &pcStart, &pcEnd));
1184:       pStart = PetscMin(pvStart, pcStart);
1185:       pEnd   = PetscMax(pvEnd, pcEnd);
1186:       PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
1187:       PetscCall(PetscViewerASCIIPrintf(viewer, "%s with %" PetscInt_FMT " fields\n", name, Nf));
1188:       PetscCall(PetscViewerASCIIPrintf(viewer, "  field 0 with %" PetscInt_FMT " components\n", Nc));
1189:       if (cs != CS_CARTESIAN) PetscCall(PetscViewerASCIIPrintf(viewer, "  output coordinate system: %s\n", CoordSystems[cs]));

1191:       PetscCall(VecGetArrayRead(coordinates, &array));
1192:       if (coordinatesCell) PetscCall(VecGetArrayRead(coordinatesCell, &arrayCell));
1193:       PetscCall(PetscViewerASCIIPushSynchronized(viewer));
1194:       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "Process %d:\n", rank));
1195:       for (p = pStart; p < pEnd; ++p) {
1196:         PetscInt dof, off;

1198:         if (p >= pvStart && p < pvEnd) {
1199:           PetscCall(PetscSectionGetDof(coordSection, p, &dof));
1200:           PetscCall(PetscSectionGetOffset(coordSection, p, &off));
1201:           if (dof) {
1202:             PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "  (%4" PetscInt_FMT ") dof %2" PetscInt_FMT " offset %3" PetscInt_FMT, p, dof, off));
1203:             PetscCall(DMPlexView_Ascii_Coordinates(viewer, cs, dof, &array[off]));
1204:             PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n"));
1205:           }
1206:         }
1207:         if (cdmCell && p >= pcStart && p < pcEnd) {
1208:           PetscCall(PetscSectionGetDof(coordSectionCell, p, &dof));
1209:           PetscCall(PetscSectionGetOffset(coordSectionCell, p, &off));
1210:           if (dof) {
1211:             PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "  (%4" PetscInt_FMT ") dof %2" PetscInt_FMT " offset %3" PetscInt_FMT, p, dof, off));
1212:             PetscCall(DMPlexView_Ascii_Coordinates(viewer, cs, dof, &arrayCell[off]));
1213:             PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n"));
1214:           }
1215:         }
1216:       }
1217:       PetscCall(PetscViewerFlush(viewer));
1218:       PetscCall(PetscViewerASCIIPopSynchronized(viewer));
1219:       PetscCall(VecRestoreArrayRead(coordinates, &array));
1220:       if (coordinatesCell) PetscCall(VecRestoreArrayRead(coordinatesCell, &arrayCell));
1221:     }
1222:     PetscCall(DMGetNumLabels(dm, &numLabels));
1223:     if (numLabels) PetscCall(PetscViewerASCIIPrintf(viewer, "Labels:\n"));
1224:     for (l = 0; l < numLabels; ++l) {
1225:       DMLabel     label;
1226:       PetscBool   isdepth;
1227:       const char *name;

1229:       PetscCall(DMGetLabelName(dm, l, &name));
1230:       PetscCall(PetscStrcmp(name, "depth", &isdepth));
1231:       if (isdepth) continue;
1232:       PetscCall(DMGetLabel(dm, name, &label));
1233:       PetscCall(DMLabelView(label, viewer));
1234:     }
1235:     if (size > 1) {
1236:       PetscSF sf;

1238:       PetscCall(DMGetPointSF(dm, &sf));
1239:       PetscCall(PetscSFView(sf, viewer));
1240:     }
1241:     if (mesh->periodic.face_sfs)
1242:       for (PetscInt i = 0; i < mesh->periodic.num_face_sfs; i++) PetscCall(PetscSFView(mesh->periodic.face_sfs[i], viewer));
1243:     PetscCall(PetscViewerFlush(viewer));
1244:   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
1245:     const char  *name, *color;
1246:     const char  *defcolors[3]  = {"gray", "orange", "green"};
1247:     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
1248:     char         lname[PETSC_MAX_PATH_LEN];
1249:     PetscReal    scale      = 2.0;
1250:     PetscReal    tikzscale  = 1.0;
1251:     PetscBool    useNumbers = PETSC_TRUE, drawNumbers[4], drawColors[4], useLabels, useColors, plotEdges, drawHasse = PETSC_FALSE;
1252:     double       tcoords[3];
1253:     PetscScalar *coords;
1254:     PetscInt     numLabels, l, numColors, numLColors, dim, d, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, fStart = 0, fEnd = 0, e, p, n;
1255:     PetscMPIInt  rank, size;
1256:     char       **names, **colors, **lcolors;
1257:     PetscBool    flg, lflg;
1258:     PetscBT      wp = NULL;
1259:     PetscInt     pEnd, pStart;

1261:     PetscCall(DMGetCoordinateDM(dm, &cdm));
1262:     PetscCall(DMGetCoordinateSection(dm, &coordSection));
1263:     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
1264:     PetscCall(DMGetCellCoordinateDM(dm, &cdmCell));
1265:     PetscCall(DMGetCellCoordinateSection(dm, &coordSectionCell));
1266:     PetscCall(DMGetCellCoordinatesLocal(dm, &coordinatesCell));
1267:     PetscCall(DMGetDimension(dm, &dim));
1268:     PetscCall(DMPlexGetDepth(dm, &depth));
1269:     PetscCall(DMGetNumLabels(dm, &numLabels));
1270:     numLabels  = PetscMax(numLabels, 10);
1271:     numColors  = 10;
1272:     numLColors = 10;
1273:     PetscCall(PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors));
1274:     PetscCall(PetscOptionsGetReal(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_scale", &scale, NULL));
1275:     PetscCall(PetscOptionsGetReal(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_tikzscale", &tikzscale, NULL));
1276:     PetscCall(PetscOptionsGetBool(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL));
1277:     for (d = 0; d < 4; ++d) drawNumbers[d] = useNumbers;
1278:     for (d = 0; d < 4; ++d) drawColors[d] = PETSC_TRUE;
1279:     n = 4;
1280:     PetscCall(PetscOptionsGetBoolArray(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_numbers_depth", drawNumbers, &n, &flg));
1281:     PetscCheck(!flg || n == dim + 1, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_SIZ, "Number of flags %" PetscInt_FMT " != %" PetscInt_FMT " dim+1", n, dim + 1);
1282:     n = 4;
1283:     PetscCall(PetscOptionsGetBoolArray(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_colors_depth", drawColors, &n, &flg));
1284:     PetscCheck(!flg || n == dim + 1, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_SIZ, "Number of flags %" PetscInt_FMT " != %" PetscInt_FMT " dim+1", n, dim + 1);
1285:     PetscCall(PetscOptionsGetStringArray(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels));
1286:     if (!useLabels) numLabels = 0;
1287:     PetscCall(PetscOptionsGetStringArray(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors));
1288:     if (!useColors) {
1289:       numColors = 3;
1290:       for (c = 0; c < numColors; ++c) PetscCall(PetscStrallocpy(defcolors[c], &colors[c]));
1291:     }
1292:     PetscCall(PetscOptionsGetStringArray(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors));
1293:     if (!useColors) {
1294:       numLColors = 4;
1295:       for (c = 0; c < numLColors; ++c) PetscCall(PetscStrallocpy(deflcolors[c], &lcolors[c]));
1296:     }
1297:     PetscCall(PetscOptionsGetString(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_label_filter", lname, sizeof(lname), &lflg));
1298:     plotEdges = (PetscBool)(depth > 1 && drawNumbers[1] && dim < 3);
1299:     PetscCall(PetscOptionsGetBool(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_edges", &plotEdges, &flg));
1300:     PetscCheck(!flg || !plotEdges || depth >= dim, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Mesh must be interpolated");
1301:     if (depth < dim) plotEdges = PETSC_FALSE;
1302:     PetscCall(PetscOptionsGetBool(((PetscObject)viewer)->options, ((PetscObject)viewer)->prefix, "-dm_plex_view_hasse", &drawHasse, NULL));

1304:     /* filter points with labelvalue != labeldefaultvalue */
1305:     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
1306:     PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
1307:     PetscCall(DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd));
1308:     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
1309:     PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
1310:     if (lflg) {
1311:       DMLabel lbl;

1313:       PetscCall(DMGetLabel(dm, lname, &lbl));
1314:       if (lbl) {
1315:         PetscInt val, defval;

1317:         PetscCall(DMLabelGetDefaultValue(lbl, &defval));
1318:         PetscCall(PetscBTCreate(pEnd - pStart, &wp));
1319:         for (c = pStart; c < pEnd; c++) {
1320:           PetscInt *closure = NULL;
1321:           PetscInt  closureSize;

1323:           PetscCall(DMLabelGetValue(lbl, c, &val));
1324:           if (val == defval) continue;

1326:           PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure));
1327:           for (p = 0; p < closureSize * 2; p += 2) PetscCall(PetscBTSet(wp, closure[p] - pStart));
1328:           PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure));
1329:         }
1330:       }
1331:     }

1333:     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
1334:     PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
1335:     PetscCall(PetscObjectGetName((PetscObject)dm, &name));
1336:     PetscCall(PetscViewerASCIIPrintf(viewer, "\
1337: \\documentclass[tikz]{standalone}\n\n\
1338: \\usepackage{pgflibraryshapes}\n\
1339: \\usetikzlibrary{backgrounds}\n\
1340: \\usetikzlibrary{arrows}\n\
1341: \\begin{document}\n"));
1342:     if (size > 1) {
1343:       PetscCall(PetscViewerASCIIPrintf(viewer, "%s for process ", name));
1344:       for (p = 0; p < size; ++p) {
1345:         if (p) PetscCall(PetscViewerASCIIPrintf(viewer, (p == size - 1) ? ", and " : ", "));
1346:         PetscCall(PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%" PetscInt_FMT "}", colors[p % numColors], p));
1347:       }
1348:       PetscCall(PetscViewerASCIIPrintf(viewer, ".\n\n\n"));
1349:     }
1350:     if (drawHasse) {
1351:       PetscInt maxStratum = PetscMax(vEnd - vStart, PetscMax(eEnd - eStart, PetscMax(fEnd - fStart, cEnd - cStart)));

1353:       PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\vStart}{%" PetscInt_FMT "}\n", vStart));
1354:       PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\vEnd}{%" PetscInt_FMT "}\n", vEnd - 1));
1355:       PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\numVertices}{%" PetscInt_FMT "}\n", vEnd - vStart));
1356:       PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\vShift}{%.2f}\n", 3 + (maxStratum - (vEnd - vStart)) / 2.));
1357:       PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\eStart}{%" PetscInt_FMT "}\n", eStart));
1358:       PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\eEnd}{%" PetscInt_FMT "}\n", eEnd - 1));
1359:       PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\eShift}{%.2f}\n", 3 + (maxStratum - (eEnd - eStart)) / 2.));
1360:       PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\numEdges}{%" PetscInt_FMT "}\n", eEnd - eStart));
1361:       PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\fStart}{%" PetscInt_FMT "}\n", fStart));
1362:       PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\fEnd}{%" PetscInt_FMT "}\n", fEnd - 1));
1363:       PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\fShift}{%.2f}\n", 3 + (maxStratum - (fEnd - fStart)) / 2.));
1364:       PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\numFaces}{%" PetscInt_FMT "}\n", fEnd - fStart));
1365:       PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\cStart}{%" PetscInt_FMT "}\n", cStart));
1366:       PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\cEnd}{%" PetscInt_FMT "}\n", cEnd - 1));
1367:       PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\numCells}{%" PetscInt_FMT "}\n", cEnd - cStart));
1368:       PetscCall(PetscViewerASCIIPrintf(viewer, "\\newcommand{\\cShift}{%.2f}\n", 3 + (maxStratum - (cEnd - cStart)) / 2.));
1369:     }
1370:     PetscCall(PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", (double)tikzscale));

1372:     /* Plot vertices */
1373:     PetscCall(VecGetArray(coordinates, &coords));
1374:     PetscCall(PetscViewerASCIIPushSynchronized(viewer));
1375:     for (v = vStart; v < vEnd; ++v) {
1376:       PetscInt  off, dof, d;
1377:       PetscBool isLabeled = PETSC_FALSE;

1379:       if (wp && !PetscBTLookup(wp, v - pStart)) continue;
1380:       PetscCall(PetscSectionGetDof(coordSection, v, &dof));
1381:       PetscCall(PetscSectionGetOffset(coordSection, v, &off));
1382:       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\\path ("));
1383:       PetscCheck(dof <= 3, PETSC_COMM_SELF, PETSC_ERR_PLIB, "coordSection vertex %" PetscInt_FMT " has dof %" PetscInt_FMT " > 3", v, dof);
1384:       for (d = 0; d < dof; ++d) {
1385:         tcoords[d] = (double)(scale * PetscRealPart(coords[off + d]));
1386:         tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
1387:       }
1388:       /* Rotate coordinates since PGF makes z point out of the page instead of up */
1389:       if (dim == 3) {
1390:         PetscReal tmp = tcoords[1];
1391:         tcoords[1]    = tcoords[2];
1392:         tcoords[2]    = -tmp;
1393:       }
1394:       for (d = 0; d < dof; ++d) {
1395:         if (d > 0) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ","));
1396:         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%g", tcoords[d]));
1397:       }
1398:       if (drawHasse) color = colors[0 % numColors];
1399:       else color = colors[rank % numColors];
1400:       for (l = 0; l < numLabels; ++l) {
1401:         PetscInt val;
1402:         PetscCall(DMGetLabelValue(dm, names[l], v, &val));
1403:         if (val >= 0) {
1404:           color     = lcolors[l % numLColors];
1405:           isLabeled = PETSC_TRUE;
1406:           break;
1407:         }
1408:       }
1409:       if (drawNumbers[0]) {
1410:         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ") node(%" PetscInt_FMT "_%d) [draw,shape=circle,color=%s] {%" PetscInt_FMT "};\n", v, rank, color, v));
1411:       } else if (drawColors[0]) {
1412:         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ") node(%" PetscInt_FMT "_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color));
1413:       } else PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ") node(%" PetscInt_FMT "_%d) [] {};\n", v, rank));
1414:     }
1415:     PetscCall(VecRestoreArray(coordinates, &coords));
1416:     PetscCall(PetscViewerFlush(viewer));
1417:     /* Plot edges */
1418:     if (plotEdges) {
1419:       PetscCall(VecGetArray(coordinates, &coords));
1420:       PetscCall(PetscViewerASCIIPrintf(viewer, "\\path\n"));
1421:       for (e = eStart; e < eEnd; ++e) {
1422:         const PetscInt *cone;
1423:         PetscInt        coneSize, offA, offB, dof, d;

1425:         if (wp && !PetscBTLookup(wp, e - pStart)) continue;
1426:         PetscCall(DMPlexGetConeSize(dm, e, &coneSize));
1427:         PetscCheck(coneSize == 2, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " cone should have two vertices, not %" PetscInt_FMT, e, coneSize);
1428:         PetscCall(DMPlexGetCone(dm, e, &cone));
1429:         PetscCall(PetscSectionGetDof(coordSection, cone[0], &dof));
1430:         PetscCall(PetscSectionGetOffset(coordSection, cone[0], &offA));
1431:         PetscCall(PetscSectionGetOffset(coordSection, cone[1], &offB));
1432:         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "("));
1433:         for (d = 0; d < dof; ++d) {
1434:           tcoords[d] = (double)(scale * PetscRealPart(coords[offA + d] + coords[offB + d]) / 2);
1435:           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
1436:         }
1437:         /* Rotate coordinates since PGF makes z point out of the page instead of up */
1438:         if (dim == 3) {
1439:           PetscReal tmp = tcoords[1];
1440:           tcoords[1]    = tcoords[2];
1441:           tcoords[2]    = -tmp;
1442:         }
1443:         for (d = 0; d < dof; ++d) {
1444:           if (d > 0) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ","));
1445:           PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%g", tcoords[d]));
1446:         }
1447:         if (drawHasse) color = colors[1 % numColors];
1448:         else color = colors[rank % numColors];
1449:         for (l = 0; l < numLabels; ++l) {
1450:           PetscInt val;
1451:           PetscCall(DMGetLabelValue(dm, names[l], e, &val));
1452:           if (val >= 0) {
1453:             color = lcolors[l % numLColors];
1454:             break;
1455:           }
1456:         }
1457:         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ") node(%" PetscInt_FMT "_%d) [draw,shape=circle,color=%s] {%" PetscInt_FMT "} --\n", e, rank, color, e));
1458:       }
1459:       PetscCall(VecRestoreArray(coordinates, &coords));
1460:       PetscCall(PetscViewerFlush(viewer));
1461:       PetscCall(PetscViewerASCIIPrintf(viewer, "(0,0);\n"));
1462:     }
1463:     /* Plot cells */
1464:     if (dim == 3 || !drawNumbers[1]) {
1465:       for (e = eStart; e < eEnd; ++e) {
1466:         const PetscInt *cone;

1468:         if (wp && !PetscBTLookup(wp, e - pStart)) continue;
1469:         color = colors[rank % numColors];
1470:         for (l = 0; l < numLabels; ++l) {
1471:           PetscInt val;
1472:           PetscCall(DMGetLabelValue(dm, names[l], e, &val));
1473:           if (val >= 0) {
1474:             color = lcolors[l % numLColors];
1475:             break;
1476:           }
1477:         }
1478:         PetscCall(DMPlexGetCone(dm, e, &cone));
1479:         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%" PetscInt_FMT "_%d) -- (%" PetscInt_FMT "_%d);\n", color, cone[0], rank, cone[1], rank));
1480:       }
1481:     } else {
1482:       DMPolytopeType ct;

1484:       /* Drawing a 2D polygon */
1485:       for (c = cStart; c < cEnd; ++c) {
1486:         if (wp && !PetscBTLookup(wp, c - pStart)) continue;
1487:         PetscCall(DMPlexGetCellType(dm, c, &ct));
1488:         if (DMPolytopeTypeIsHybrid(ct)) {
1489:           const PetscInt *cone;
1490:           PetscInt        coneSize;

1492:           PetscCall(DMPlexGetCone(dm, c, &cone));
1493:           PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
1494:           for (PetscInt e = 0; e < coneSize; ++e) {
1495:             const PetscInt *econe;

1497:             PetscCall(DMPlexGetCone(dm, cone[e], &econe));
1498:             PetscCall(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));
1499:           }
1500:         } else {
1501:           PetscInt *closure = NULL;
1502:           PetscInt  closureSize, Nv = 0, v;

1504:           PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure));
1505:           for (p = 0; p < closureSize * 2; p += 2) {
1506:             const PetscInt point = closure[p];

1508:             if (point >= vStart && point < vEnd) closure[Nv++] = point;
1509:           }
1510:           PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank % numColors]));
1511:           for (v = 0; v <= Nv; ++v) {
1512:             const PetscInt vertex = closure[v % Nv];

1514:             if (v > 0) {
1515:               if (plotEdges) {
1516:                 const PetscInt *edge;
1517:                 PetscInt        endpoints[2], ne;

1519:                 endpoints[0] = closure[v - 1];
1520:                 endpoints[1] = vertex;
1521:                 PetscCall(DMPlexGetJoin(dm, 2, endpoints, &ne, &edge));
1522:                 PetscCheck(ne == 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find edge for vertices %" PetscInt_FMT ", %" PetscInt_FMT, endpoints[0], endpoints[1]);
1523:                 PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " -- (%" PetscInt_FMT "_%d) -- ", edge[0], rank));
1524:                 PetscCall(DMPlexRestoreJoin(dm, 2, endpoints, &ne, &edge));
1525:               } else PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, " -- "));
1526:             }
1527:             PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "(%" PetscInt_FMT "_%d)", vertex, rank));
1528:           }
1529:           PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ";\n"));
1530:           PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure));
1531:         }
1532:       }
1533:     }
1534:     for (c = cStart; c < cEnd; ++c) {
1535:       double             ccoords[3] = {0.0, 0.0, 0.0};
1536:       PetscBool          isLabeled  = PETSC_FALSE;
1537:       PetscScalar       *cellCoords = NULL;
1538:       const PetscScalar *array;
1539:       PetscInt           numCoords, cdim, d;
1540:       PetscBool          isDG;

1542:       if (wp && !PetscBTLookup(wp, c - pStart)) continue;
1543:       PetscCall(DMGetCoordinateDim(dm, &cdim));
1544:       PetscCall(DMPlexGetCellCoordinates(dm, c, &isDG, &numCoords, &array, &cellCoords));
1545:       PetscCheck(!(numCoords % cdim), PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "coordinate dim %" PetscInt_FMT " does not divide numCoords %" PetscInt_FMT, cdim, numCoords);
1546:       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\\path ("));
1547:       for (p = 0; p < numCoords / cdim; ++p) {
1548:         for (d = 0; d < cdim; ++d) {
1549:           tcoords[d] = (double)(scale * PetscRealPart(cellCoords[p * cdim + d]));
1550:           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
1551:         }
1552:         /* Rotate coordinates since PGF makes z point out of the page instead of up */
1553:         if (cdim == 3) {
1554:           PetscReal tmp = tcoords[1];
1555:           tcoords[1]    = tcoords[2];
1556:           tcoords[2]    = -tmp;
1557:         }
1558:         for (d = 0; d < dim; ++d) ccoords[d] += tcoords[d];
1559:       }
1560:       for (d = 0; d < cdim; ++d) ccoords[d] /= (numCoords / cdim);
1561:       PetscCall(DMPlexRestoreCellCoordinates(dm, c, &isDG, &numCoords, &array, &cellCoords));
1562:       for (d = 0; d < cdim; ++d) {
1563:         if (d > 0) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ","));
1564:         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%g", ccoords[d]));
1565:       }
1566:       if (drawHasse) color = colors[depth % numColors];
1567:       else color = colors[rank % numColors];
1568:       for (l = 0; l < numLabels; ++l) {
1569:         PetscInt val;
1570:         PetscCall(DMGetLabelValue(dm, names[l], c, &val));
1571:         if (val >= 0) {
1572:           color     = lcolors[l % numLColors];
1573:           isLabeled = PETSC_TRUE;
1574:           break;
1575:         }
1576:       }
1577:       if (drawNumbers[dim]) {
1578:         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ") node(%" PetscInt_FMT "_%d) [draw,shape=circle,color=%s] {%" PetscInt_FMT "};\n", c, rank, color, c));
1579:       } else if (drawColors[dim]) {
1580:         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ") node(%" PetscInt_FMT "_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", c, rank, !isLabeled ? 1 : 2, color));
1581:       } else PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ") node(%" PetscInt_FMT "_%d) [] {};\n", c, rank));
1582:     }
1583:     if (drawHasse) {
1584:       int height = 0;

1586:       color = colors[depth % numColors];
1587:       PetscCall(PetscViewerASCIIPrintf(viewer, "%% Cells\n"));
1588:       PetscCall(PetscViewerASCIIPrintf(viewer, "\\foreach \\c in {\\cStart,...,\\cEnd}\n"));
1589:       PetscCall(PetscViewerASCIIPrintf(viewer, "{\n"));
1590:       PetscCall(PetscViewerASCIIPrintf(viewer, "  \\node(\\c_%d) [draw,shape=circle,color=%s,minimum size = 6mm] at (\\cShift+\\c-\\cStart,%d) {\\c};\n", rank, color, height++));
1591:       PetscCall(PetscViewerASCIIPrintf(viewer, "}\n"));

1593:       if (depth > 2) {
1594:         color = colors[1 % numColors];
1595:         PetscCall(PetscViewerASCIIPrintf(viewer, "%% Faces\n"));
1596:         PetscCall(PetscViewerASCIIPrintf(viewer, "\\foreach \\f in {\\fStart,...,\\fEnd}\n"));
1597:         PetscCall(PetscViewerASCIIPrintf(viewer, "{\n"));
1598:         PetscCall(PetscViewerASCIIPrintf(viewer, "  \\node(\\f_%d) [draw,shape=circle,color=%s,minimum size = 6mm] at (\\fShift+\\f-\\fStart,%d) {\\f};\n", rank, color, height++));
1599:         PetscCall(PetscViewerASCIIPrintf(viewer, "}\n"));
1600:       }

1602:       color = colors[1 % numColors];
1603:       PetscCall(PetscViewerASCIIPrintf(viewer, "%% Edges\n"));
1604:       PetscCall(PetscViewerASCIIPrintf(viewer, "\\foreach \\e in {\\eStart,...,\\eEnd}\n"));
1605:       PetscCall(PetscViewerASCIIPrintf(viewer, "{\n"));
1606:       PetscCall(PetscViewerASCIIPrintf(viewer, "  \\node(\\e_%d) [draw,shape=circle,color=%s,minimum size = 6mm] at (\\eShift+\\e-\\eStart,%d) {\\e};\n", rank, color, height++));
1607:       PetscCall(PetscViewerASCIIPrintf(viewer, "}\n"));

1609:       color = colors[0 % numColors];
1610:       PetscCall(PetscViewerASCIIPrintf(viewer, "%% Vertices\n"));
1611:       PetscCall(PetscViewerASCIIPrintf(viewer, "\\foreach \\v in {\\vStart,...,\\vEnd}\n"));
1612:       PetscCall(PetscViewerASCIIPrintf(viewer, "{\n"));
1613:       PetscCall(PetscViewerASCIIPrintf(viewer, "  \\node(\\v_%d) [draw,shape=circle,color=%s,minimum size = 6mm] at (\\vShift+\\v-\\vStart,%d) {\\v};\n", rank, color, height++));
1614:       PetscCall(PetscViewerASCIIPrintf(viewer, "}\n"));

1616:       for (p = pStart; p < pEnd; ++p) {
1617:         const PetscInt *cone;
1618:         PetscInt        coneSize;

1620:         PetscCall(DMPlexGetCone(dm, p, &cone));
1621:         PetscCall(DMPlexGetConeSize(dm, p, &coneSize));
1622:         for (PetscInt cp = 0; cp < coneSize; ++cp) PetscCall(PetscViewerASCIIPrintf(viewer, "\\draw[->, shorten >=1pt] (%" PetscInt_FMT "_%d) -- (%" PetscInt_FMT "_%d);\n", cone[cp], rank, p, rank));
1623:       }
1624:     }
1625:     PetscCall(PetscViewerFlush(viewer));
1626:     PetscCall(PetscViewerASCIIPopSynchronized(viewer));
1627:     PetscCall(PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n"));
1628:     PetscCall(PetscViewerASCIIPrintf(viewer, "\\end{document}\n"));
1629:     for (l = 0; l < numLabels; ++l) PetscCall(PetscFree(names[l]));
1630:     for (c = 0; c < numColors; ++c) PetscCall(PetscFree(colors[c]));
1631:     for (c = 0; c < numLColors; ++c) PetscCall(PetscFree(lcolors[c]));
1632:     PetscCall(PetscFree3(names, colors, lcolors));
1633:     PetscCall(PetscBTDestroy(&wp));
1634:   } else if (format == PETSC_VIEWER_LOAD_BALANCE) {
1635:     Vec                    cown, acown;
1636:     VecScatter             sct;
1637:     ISLocalToGlobalMapping g2l;
1638:     IS                     gid, acis;
1639:     MPI_Comm               comm, ncomm = MPI_COMM_NULL;
1640:     MPI_Group              ggroup, ngroup;
1641:     PetscScalar           *array, nid;
1642:     const PetscInt        *idxs;
1643:     PetscInt              *idxs2, *start, *adjacency, *work;
1644:     PetscInt64             gm[3];
1645:     PetscInt               i, c, cStart, cEnd, cum, numVertices, ect, ectn, cellHeight;
1646:     PetscMPIInt            d1, d2, rank;

1648:     PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
1649:     PetscCallMPI(MPI_Comm_rank(comm, &rank));
1650: #if PetscDefined(HAVE_MPI_PROCESS_SHARED_MEMORY)
1651:     PetscCallMPI(MPI_Comm_split_type(comm, MPI_COMM_TYPE_SHARED, rank, MPI_INFO_NULL, &ncomm));
1652: #endif
1653:     if (ncomm != MPI_COMM_NULL) {
1654:       PetscCallMPI(MPI_Comm_group(comm, &ggroup));
1655:       PetscCallMPI(MPI_Comm_group(ncomm, &ngroup));
1656:       d1 = 0;
1657:       PetscCallMPI(MPI_Group_translate_ranks(ngroup, 1, &d1, ggroup, &d2));
1658:       nid = d2;
1659:       PetscCallMPI(MPI_Group_free(&ggroup));
1660:       PetscCallMPI(MPI_Group_free(&ngroup));
1661:       PetscCallMPI(MPI_Comm_free(&ncomm));
1662:     } else nid = 0.0;

1664:     /* Get connectivity */
1665:     PetscCall(DMPlexGetVTKCellHeight(dm, &cellHeight));
1666:     PetscCall(DMPlexCreatePartitionerGraph(dm, cellHeight, &numVertices, &start, &adjacency, &gid));

1668:     /* filter overlapped local cells */
1669:     PetscCall(DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd));
1670:     PetscCall(ISGetIndices(gid, &idxs));
1671:     PetscCall(ISGetLocalSize(gid, &cum));
1672:     PetscCall(PetscMalloc1(cum, &idxs2));
1673:     for (c = cStart, cum = 0; c < cEnd; c++) {
1674:       if (idxs[c - cStart] < 0) continue;
1675:       idxs2[cum++] = idxs[c - cStart];
1676:     }
1677:     PetscCall(ISRestoreIndices(gid, &idxs));
1678:     PetscCheck(numVertices == cum, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unexpected %" PetscInt_FMT " != %" PetscInt_FMT, numVertices, cum);
1679:     PetscCall(ISDestroy(&gid));
1680:     PetscCall(ISCreateGeneral(comm, numVertices, idxs2, PETSC_OWN_POINTER, &gid));

1682:     /* support for node-aware cell locality */
1683:     PetscCall(ISCreateGeneral(comm, start[numVertices], adjacency, PETSC_USE_POINTER, &acis));
1684:     PetscCall(VecCreateSeq(PETSC_COMM_SELF, start[numVertices], &acown));
1685:     PetscCall(VecCreateMPI(comm, numVertices, PETSC_DECIDE, &cown));
1686:     PetscCall(VecGetArray(cown, &array));
1687:     for (c = 0; c < numVertices; c++) array[c] = nid;
1688:     PetscCall(VecRestoreArray(cown, &array));
1689:     PetscCall(VecScatterCreate(cown, acis, acown, NULL, &sct));
1690:     PetscCall(VecScatterBegin(sct, cown, acown, INSERT_VALUES, SCATTER_FORWARD));
1691:     PetscCall(VecScatterEnd(sct, cown, acown, INSERT_VALUES, SCATTER_FORWARD));
1692:     PetscCall(ISDestroy(&acis));
1693:     PetscCall(VecScatterDestroy(&sct));
1694:     PetscCall(VecDestroy(&cown));

1696:     /* compute edgeCut */
1697:     for (c = 0, cum = 0; c < numVertices; c++) cum = PetscMax(cum, start[c + 1] - start[c]);
1698:     PetscCall(PetscMalloc1(cum, &work));
1699:     PetscCall(ISLocalToGlobalMappingCreateIS(gid, &g2l));
1700:     PetscCall(ISLocalToGlobalMappingSetType(g2l, ISLOCALTOGLOBALMAPPINGHASH));
1701:     PetscCall(ISDestroy(&gid));
1702:     PetscCall(VecGetArray(acown, &array));
1703:     for (c = 0, ect = 0, ectn = 0; c < numVertices; c++) {
1704:       PetscInt totl;

1706:       totl = start[c + 1] - start[c];
1707:       PetscCall(ISGlobalToLocalMappingApply(g2l, IS_GTOLM_MASK, totl, adjacency + start[c], NULL, work));
1708:       for (i = 0; i < totl; i++) {
1709:         if (work[i] < 0) {
1710:           ect += 1;
1711:           ectn += (array[i + start[c]] != nid) ? 0 : 1;
1712:         }
1713:       }
1714:     }
1715:     PetscCall(PetscFree(work));
1716:     PetscCall(VecRestoreArray(acown, &array));
1717:     gm[0] = numVertices > 0 ? numVertices : PETSC_INT_MAX;
1718:     gm[1] = -numVertices;
1719:     PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, gm, 2, MPIU_INT64, MPI_MIN, comm));
1720:     PetscCall(PetscViewerASCIIPrintf(viewer, "  Cell balance: %.2f (max %" PetscInt64_FMT ", min %" PetscInt64_FMT, -((double)gm[1]) / ((double)gm[0]), -gm[1], gm[0]));
1721:     gm[0] = ect;                     /* edgeCut */
1722:     gm[1] = ectn;                    /* node-aware edgeCut */
1723:     gm[2] = numVertices > 0 ? 0 : 1; /* empty processes */
1724:     PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, gm, 3, MPIU_INT64, MPI_SUM, comm));
1725:     PetscCall(PetscViewerASCIIPrintf(viewer, ", empty %" PetscInt64_FMT ")\n", gm[2]));
1726: #if PetscDefined(HAVE_MPI_PROCESS_SHARED_MEMORY)
1727:     PetscCall(PetscViewerASCIIPrintf(viewer, "  Edge Cut: %" PetscInt64_FMT " (on node %.3f)\n", gm[0] / 2, gm[0] ? ((double)gm[1]) / ((double)gm[0]) : 1.));
1728: #else
1729:     PetscCall(PetscViewerASCIIPrintf(viewer, "  Edge Cut: %" PetscInt64_FMT " (on node %.3f)\n", gm[0] / 2, 0.0));
1730: #endif
1731:     PetscCall(ISLocalToGlobalMappingDestroy(&g2l));
1732:     PetscCall(PetscFree(start));
1733:     PetscCall(PetscFree(adjacency));
1734:     PetscCall(VecDestroy(&acown));
1735:   } else {
1736:     const char    *name;
1737:     PetscInt      *sizes, *hybsizes, *ghostsizes;
1738:     PetscInt       depth, cellHeight, dim, d;
1739:     PetscInt       pStart, pEnd, p, gcStart, gcEnd, gcNum;
1740:     PetscInt       numLabels, l, maxSize = 17;
1741:     DMPolytopeType ct0 = DM_POLYTOPE_UNKNOWN;
1742:     MPI_Comm       comm;
1743:     PetscMPIInt    size, rank;

1745:     PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
1746:     PetscCallMPI(MPI_Comm_size(comm, &size));
1747:     PetscCallMPI(MPI_Comm_rank(comm, &rank));
1748:     PetscCall(DMGetDimension(dm, &dim));
1749:     PetscCall(DMPlexGetVTKCellHeight(dm, &cellHeight));
1750:     PetscCall(PetscObjectGetName((PetscObject)dm, &name));
1751:     if (name) PetscCall(PetscViewerASCIIPrintf(viewer, "%s in %" PetscInt_FMT " dimension%s:\n", name, dim, dim == 1 ? "" : "s"));
1752:     else PetscCall(PetscViewerASCIIPrintf(viewer, "Mesh in %" PetscInt_FMT " dimension%s:\n", dim, dim == 1 ? "" : "s"));
1753:     if (cellHeight) PetscCall(PetscViewerASCIIPrintf(viewer, "  Cells are at height %" PetscInt_FMT "\n", cellHeight));
1754:     PetscCall(DMPlexGetDepth(dm, &depth));
1755:     PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &depth, 1, MPIU_INT, MPI_MAX, comm));
1756:     PetscCall(DMPlexGetCellTypeStratum(dm, DM_POLYTOPE_FV_GHOST, &gcStart, &gcEnd));
1757:     gcNum = gcEnd - gcStart;
1758:     if (size < maxSize) PetscCall(PetscCalloc3(size, &sizes, size, &hybsizes, size, &ghostsizes));
1759:     else PetscCall(PetscCalloc3(3, &sizes, 3, &hybsizes, 3, &ghostsizes));
1760:     for (d = 0; d <= depth; d++) {
1761:       PetscInt Nc[2] = {0, 0}, ict;

1763:       PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd));
1764:       if (pStart < pEnd) PetscCall(DMPlexGetCellType(dm, pStart, &ct0));
1765:       ict = ct0;
1766:       PetscCallMPI(MPI_Bcast(&ict, 1, MPIU_INT, 0, comm));
1767:       ct0 = (DMPolytopeType)ict;
1768:       for (p = pStart; p < pEnd; ++p) {
1769:         DMPolytopeType ct;

1771:         PetscCall(DMPlexGetCellType(dm, p, &ct));
1772:         if (ct == ct0) ++Nc[0];
1773:         else ++Nc[1];
1774:       }
1775:       if (size < maxSize) {
1776:         PetscCallMPI(MPI_Gather(&Nc[0], 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm));
1777:         PetscCallMPI(MPI_Gather(&Nc[1], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm));
1778:         if (d == depth) PetscCallMPI(MPI_Gather(&gcNum, 1, MPIU_INT, ghostsizes, 1, MPIU_INT, 0, comm));
1779:         PetscCall(PetscViewerASCIIPrintf(viewer, "  Number of %" PetscInt_FMT "-cells per rank:", (depth == 1) && d ? dim : d));
1780:         for (p = 0; p < size; ++p) {
1781:           if (rank == 0) {
1782:             PetscCall(PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT, sizes[p] + hybsizes[p]));
1783:             if (hybsizes[p] > 0) PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ")", hybsizes[p]));
1784:             if (ghostsizes[p] > 0) PetscCall(PetscViewerASCIIPrintf(viewer, " [%" PetscInt_FMT "]", ghostsizes[p]));
1785:           }
1786:         }
1787:       } else {
1788:         sizes[0] = Nc[0] + Nc[1];
1789:         sizes[1] = Nc[0] + Nc[1];
1790:         PetscCall(PetscGlobalMinMaxInt(comm, sizes, sizes));
1791:         hybsizes[0] = Nc[1];
1792:         hybsizes[1] = Nc[1];
1793:         PetscCall(PetscGlobalMinMaxInt(comm, hybsizes, hybsizes));
1794:         if (d == depth) {
1795:           ghostsizes[0] = gcNum;
1796:           ghostsizes[1] = gcNum;
1797:           PetscCall(PetscGlobalMinMaxInt(comm, ghostsizes, ghostsizes));
1798:         }
1799:         PetscCall(PetscViewerASCIIPrintf(viewer, "  Min/Max of %" PetscInt_FMT "-cells per rank:", (depth == 1) && d ? dim : d));
1800:         PetscCall(PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT "/%" PetscInt_FMT, sizes[0], sizes[1]));
1801:         if (hybsizes[0] > 0) PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT "/%" PetscInt_FMT ")", hybsizes[0], hybsizes[1]));
1802:         if (ghostsizes[0] > 0) PetscCall(PetscViewerASCIIPrintf(viewer, " [%" PetscInt_FMT "/%" PetscInt_FMT "]", ghostsizes[0], ghostsizes[1]));
1803:       }
1804:       PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
1805:     }
1806:     PetscCall(PetscFree3(sizes, hybsizes, ghostsizes));
1807:     {
1808:       DM               cdm;
1809:       const PetscReal *maxCell;
1810:       const PetscReal *L;
1811:       PetscBool        localized;

1813:       PetscCall(DMGetCoordinateDM(dm, &cdm));
1814:       PetscCall(DMViewDSFromOptions_Internal(cdm, "-plex_view_ds"));
1815:       PetscCall(DMViewSectionFromOptions_Internal(cdm, "-plex_view_section"));
1816:       PetscCall(DMGetPeriodicity(dm, &maxCell, NULL, &L));
1817:       PetscCall(DMGetCoordinatesLocalized(dm, &localized));
1818:       if (L || localized) {
1819:         PetscCall(PetscViewerASCIIPrintf(viewer, "Periodic mesh"));
1820:         PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
1821:         if (L) {
1822:           PetscCall(PetscViewerASCIIPrintf(viewer, " ("));
1823:           for (d = 0; d < dim; ++d) {
1824:             if (d > 0) PetscCall(PetscViewerASCIIPrintf(viewer, ", "));
1825:             PetscCall(PetscViewerASCIIPrintf(viewer, "%s", L[d] > 0.0 ? "PERIODIC" : "NONE"));
1826:           }
1827:           PetscCall(PetscViewerASCIIPrintf(viewer, ")"));
1828:         }
1829:         PetscCall(PetscViewerASCIIPrintf(viewer, " coordinates %s\n", localized ? "localized" : "not localized"));
1830:         PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
1831:       }
1832:     }
1833:     PetscCall(DMGetNumLabels(dm, &numLabels));
1834:     if (numLabels) PetscCall(PetscViewerASCIIPrintf(viewer, "Labels:\n"));
1835:     for (l = 0; l < numLabels; ++l) {
1836:       DMLabel     label;
1837:       const char *name;
1838:       PetscInt   *values;
1839:       PetscInt    numValues;

1841:       PetscCall(DMGetLabelName(dm, l, &name));
1842:       PetscCall(DMGetLabel(dm, name, &label));
1843:       PetscCall(DMLabelGetNumValues(label, &numValues));
1844:       PetscCall(PetscViewerASCIIPrintf(viewer, "  %s: %" PetscInt_FMT " strata with value/size (", name, numValues));

1846:       { // Extract array of DMLabel values so it can be sorted
1847:         IS              is_values;
1848:         const PetscInt *is_values_local = NULL;

1850:         PetscCall(DMLabelGetValueIS(label, &is_values));
1851:         PetscCall(ISGetIndices(is_values, &is_values_local));
1852:         PetscCall(PetscMalloc1(numValues, &values));
1853:         PetscCall(PetscArraycpy(values, is_values_local, numValues));
1854:         PetscCall(PetscSortInt(numValues, values));
1855:         PetscCall(ISRestoreIndices(is_values, &is_values_local));
1856:         PetscCall(ISDestroy(&is_values));
1857:       }
1858:       PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
1859:       for (PetscInt v = 0; v < numValues; ++v) {
1860:         PetscInt size;

1862:         PetscCall(DMLabelGetStratumSize(label, values[v], &size));
1863:         if (v > 0) PetscCall(PetscViewerASCIIPrintf(viewer, ", "));
1864:         PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " (%" PetscInt_FMT ")", values[v], size));
1865:       }
1866:       PetscCall(PetscViewerASCIIPrintf(viewer, ")\n"));
1867:       PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
1868:       PetscCall(PetscFree(values));
1869:     }
1870:     {
1871:       char    **labelNames;
1872:       PetscInt  Nl = numLabels;
1873:       PetscBool flg;

1875:       PetscCall(PetscMalloc1(Nl, &labelNames));
1876:       PetscCall(PetscOptionsGetStringArray(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_view_labels", labelNames, &Nl, &flg));
1877:       for (l = 0; l < Nl; ++l) {
1878:         DMLabel label;

1880:         PetscCall(DMHasLabel(dm, labelNames[l], &flg));
1881:         if (flg) {
1882:           PetscCall(DMGetLabel(dm, labelNames[l], &label));
1883:           PetscCall(DMLabelView(label, viewer));
1884:         }
1885:         PetscCall(PetscFree(labelNames[l]));
1886:       }
1887:       PetscCall(PetscFree(labelNames));
1888:     }
1889:     /* If no fields are specified, people do not want to see adjacency */
1890:     if (dm->Nf) {
1891:       for (PetscInt f = 0; f < dm->Nf; ++f) {
1892:         const char *name;

1894:         PetscCall(PetscObjectGetName(dm->fields[f].disc, &name));
1895:         if (numLabels) PetscCall(PetscViewerASCIIPrintf(viewer, "Field %s:\n", name));
1896:         PetscCall(PetscViewerASCIIPushTab(viewer));
1897:         if (dm->fields[f].label) PetscCall(DMLabelView(dm->fields[f].label, viewer));
1898:         if (dm->fields[f].adjacency[0]) {
1899:           if (dm->fields[f].adjacency[1]) PetscCall(PetscViewerASCIIPrintf(viewer, "adjacency FVM++\n"));
1900:           else PetscCall(PetscViewerASCIIPrintf(viewer, "adjacency FVM\n"));
1901:         } else {
1902:           if (dm->fields[f].adjacency[1]) PetscCall(PetscViewerASCIIPrintf(viewer, "adjacency FEM\n"));
1903:           else PetscCall(PetscViewerASCIIPrintf(viewer, "adjacency FUNKY\n"));
1904:         }
1905:         PetscCall(PetscViewerASCIIPopTab(viewer));
1906:       }
1907:     }
1908:     DMPlexTransform tr;

1910:     PetscCall(DMPlexGetTransform(dm, &tr));
1911:     if (tr) {
1912:       PetscCall(PetscViewerASCIIPushTab(viewer));
1913:       PetscCall(PetscViewerASCIIPrintf(viewer, "Created using transform:\n"));
1914:       PetscCall(DMPlexTransformView(tr, viewer));
1915:       PetscCall(PetscViewerASCIIPopTab(viewer));
1916:     }
1917:     PetscCall(DMGetCoarseDM(dm, &cdm));
1918:     if (cdm) {
1919:       PetscCall(PetscViewerASCIIPushTab(viewer));
1920:       PetscCall(PetscViewerASCIIPrintf(viewer, "Defined by transform from:\n"));
1921:       PetscCall(DMPlexView_Ascii(cdm, viewer));
1922:       PetscCall(PetscViewerASCIIPopTab(viewer));
1923:     }
1924:   }
1925:   PetscFunctionReturn(PETSC_SUCCESS);
1926: }

1928: /*@
1929:   DMPlexDrawCell - Draw the given cell on the `PetscDraw` object.

1931:   Not collective

1933:   Input Parameters:
1934: + dm     - The `DMPLEX` object
1935: . draw   - The `PetscDraw` object
1936: . lC     - The line color, or `PETSC_DETERMINE` to use the default
1937: . cC     - The cell color, or `PETSC_DETERMINE` to use the default
1938: . cell   - The cell to draw
1939: - coords - The vertex coordinates for the cell

1941:   Level: developer

1943: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMView()`
1944: @*/
1945: PetscErrorCode DMPlexDrawCell(DM dm, PetscDraw draw, PetscInt lC, PetscInt cC, PetscInt cell, const PetscScalar coords[])
1946: {
1947:   DMPolytopeType ct;
1948:   PetscMPIInt    rank;
1949:   PetscInt       cdim;
1950:   int            lineColor, cellColor;

1952:   PetscFunctionBegin;
1955:   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
1956:   PetscCall(DMPlexGetCellType(dm, cell, &ct));
1957:   PetscCall(DMGetCoordinateDim(dm, &cdim));
1958:   lineColor = (int)(lC == PETSC_DETERMINE ? PETSC_DRAW_BLACK : lC);
1959:   cellColor = (int)(cC == PETSC_DETERMINE ? PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS - 2) + 2 : cC);
1960:   switch (ct) {
1961:   case DM_POLYTOPE_SEGMENT:
1962:   case DM_POLYTOPE_POINT_PRISM_TENSOR:
1963:     switch (cdim) {
1964:     case 1: {
1965:       const PetscReal y  = 0.5;  /* TODO Put it in the middle of the viewport */
1966:       const PetscReal dy = 0.05; /* TODO Make it a fraction of the total length */

1968:       PetscCall(PetscDrawLine(draw, PetscRealPart(coords[0]), y, PetscRealPart(coords[1]), y, lineColor));
1969:       PetscCall(PetscDrawLine(draw, PetscRealPart(coords[0]), y + dy, PetscRealPart(coords[0]), y - dy, lineColor));
1970:       PetscCall(PetscDrawLine(draw, PetscRealPart(coords[1]), y + dy, PetscRealPart(coords[1]), y - dy, lineColor));
1971:     } break;
1972:     case 2: {
1973:       const PetscReal dx = (PetscRealPart(coords[3]) - PetscRealPart(coords[1]));
1974:       const PetscReal dy = (PetscRealPart(coords[2]) - PetscRealPart(coords[0]));
1975:       const PetscReal l  = 0.1 / PetscSqrtReal(dx * dx + dy * dy);

1977:       PetscCall(PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), lineColor));
1978:       PetscCall(PetscDrawLine(draw, PetscRealPart(coords[0]) + l * dx, PetscRealPart(coords[1]) + l * dy, PetscRealPart(coords[0]) - l * dx, PetscRealPart(coords[1]) - l * dy, lineColor));
1979:       PetscCall(PetscDrawLine(draw, PetscRealPart(coords[2]) + l * dx, PetscRealPart(coords[3]) + l * dy, PetscRealPart(coords[2]) - l * dx, PetscRealPart(coords[3]) - l * dy, lineColor));
1980:     } break;
1981:     default:
1982:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of dimension %" PetscInt_FMT, cdim);
1983:     }
1984:     break;
1985:   case DM_POLYTOPE_TRIANGLE:
1986:     if (cellColor >= 0) PetscCall(PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), cellColor, cellColor, cellColor));
1987:     PetscCall(PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), lineColor));
1988:     PetscCall(PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), lineColor));
1989:     PetscCall(PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), lineColor));
1990:     break;
1991:   case DM_POLYTOPE_QUADRILATERAL:
1992:     if (cellColor >= 0) {
1993:       PetscCall(PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), cellColor, cellColor, cellColor));
1994:       PetscCall(PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), cellColor, cellColor, cellColor));
1995:     }
1996:     PetscCall(PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), lineColor));
1997:     PetscCall(PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), lineColor));
1998:     PetscCall(PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), lineColor));
1999:     PetscCall(PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), lineColor));
2000:     break;
2001:   case DM_POLYTOPE_SEG_PRISM_TENSOR:
2002:     if (cellColor >= 0) {
2003:       PetscCall(PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), cellColor, cellColor, cellColor));
2004:       PetscCall(PetscDrawTriangle(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), cellColor, cellColor, cellColor));
2005:     }
2006:     PetscCall(PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), lineColor));
2007:     PetscCall(PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), lineColor));
2008:     PetscCall(PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), lineColor));
2009:     PetscCall(PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), lineColor));
2010:     break;
2011:   case DM_POLYTOPE_FV_GHOST:
2012:     break;
2013:   default:
2014:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of type %s", DMPolytopeTypes[ct]);
2015:   }
2016:   PetscFunctionReturn(PETSC_SUCCESS);
2017: }

2019: static PetscErrorCode DrawPolygon_Private(DM dm, PetscDraw draw, PetscInt cell, PetscInt Nv, const PetscReal refVertices[], const PetscScalar coords[], PetscInt edgeDiv, PetscReal refCoords[], PetscReal edgeCoords[])
2020: {
2021:   PetscReal   centroid[2] = {0., 0.};
2022:   PetscMPIInt rank;
2023:   PetscMPIInt fillColor;

2025:   PetscFunctionBegin;
2026:   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
2027:   fillColor = PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS - 2) + 2;
2028:   for (PetscInt v = 0; v < Nv; ++v) {
2029:     centroid[0] += PetscRealPart(coords[v * 2 + 0]) / Nv;
2030:     centroid[1] += PetscRealPart(coords[v * 2 + 1]) / Nv;
2031:   }
2032:   for (PetscInt e = 0; e < Nv; ++e) {
2033:     refCoords[0] = refVertices[e * 2 + 0];
2034:     refCoords[1] = refVertices[e * 2 + 1];
2035:     for (PetscInt d = 1; d <= edgeDiv; ++d) {
2036:       refCoords[d * 2 + 0] = refCoords[0] + (refVertices[(e + 1) % Nv * 2 + 0] - refCoords[0]) * d / edgeDiv;
2037:       refCoords[d * 2 + 1] = refCoords[1] + (refVertices[(e + 1) % Nv * 2 + 1] - refCoords[1]) * d / edgeDiv;
2038:     }
2039:     PetscCall(DMPlexReferenceToCoordinates(dm, cell, edgeDiv + 1, refCoords, edgeCoords));
2040:     for (PetscInt d = 0; d < edgeDiv; ++d) {
2041:       PetscCall(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));
2042:       PetscCall(PetscDrawLine(draw, edgeCoords[d * 2 + 0], edgeCoords[d * 2 + 1], edgeCoords[(d + 1) * 2 + 0], edgeCoords[(d + 1) * 2 + 1], PETSC_DRAW_BLACK));
2043:     }
2044:   }
2045:   PetscFunctionReturn(PETSC_SUCCESS);
2046: }

2048: static PetscErrorCode DMPlexDrawCellHighOrder(DM dm, PetscDraw draw, PetscInt cell, const PetscScalar coords[], PetscInt edgeDiv, PetscReal refCoords[], PetscReal edgeCoords[])
2049: {
2050:   DMPolytopeType ct;

2052:   PetscFunctionBegin;
2053:   PetscCall(DMPlexGetCellType(dm, cell, &ct));
2054:   switch (ct) {
2055:   case DM_POLYTOPE_TRIANGLE: {
2056:     PetscReal refVertices[6] = {-1., -1., 1., -1., -1., 1.};

2058:     PetscCall(DrawPolygon_Private(dm, draw, cell, 3, refVertices, coords, edgeDiv, refCoords, edgeCoords));
2059:   } break;
2060:   case DM_POLYTOPE_QUADRILATERAL: {
2061:     PetscReal refVertices[8] = {-1., -1., 1., -1., 1., 1., -1., 1.};

2063:     PetscCall(DrawPolygon_Private(dm, draw, cell, 4, refVertices, coords, edgeDiv, refCoords, edgeCoords));
2064:   } break;
2065:   default:
2066:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of type %s", DMPolytopeTypes[ct]);
2067:   }
2068:   PetscFunctionReturn(PETSC_SUCCESS);
2069: }

2071: static PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
2072: {
2073:   PetscDraw    draw;
2074:   DM           cdm;
2075:   PetscSection coordSection;
2076:   Vec          coordinates;
2077:   PetscReal    xyl[3], xyr[3];
2078:   PetscReal   *refCoords, *edgeCoords;
2079:   PetscBool    isnull, drawAffine;
2080:   PetscInt     dim, vStart, vEnd, cStart, cEnd, c, cDegree, edgeDiv, lineColor = PETSC_DETERMINE, cellColor = PETSC_DETERMINE;

2082:   PetscFunctionBegin;
2083:   PetscCall(DMGetCoordinateDim(dm, &dim));
2084:   PetscCheck(dim <= 2, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %" PetscInt_FMT, dim);
2085:   PetscCall(DMGetCoordinateDegree_Internal(dm, &cDegree));
2086:   drawAffine = cDegree > 1 ? PETSC_FALSE : PETSC_TRUE;
2087:   edgeDiv    = cDegree + 1;
2088:   PetscCall(PetscOptionsGetInt(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_view_draw_line_color", &lineColor, NULL));
2089:   PetscCall(PetscOptionsGetInt(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_view_draw_cell_color", &cellColor, NULL));
2090:   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_view_draw_affine", &drawAffine, NULL));
2091:   if (!drawAffine) PetscCall(PetscMalloc2((edgeDiv + 1) * dim, &refCoords, (edgeDiv + 1) * dim, &edgeCoords));
2092:   PetscCall(DMGetCoordinateDM(dm, &cdm));
2093:   PetscCall(DMGetLocalSection(cdm, &coordSection));
2094:   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
2095:   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
2096:   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));

2098:   PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
2099:   PetscCall(PetscDrawIsNull(draw, &isnull));
2100:   if (isnull) PetscFunctionReturn(PETSC_SUCCESS);
2101:   PetscCall(PetscDrawSetTitle(draw, "Mesh"));

2103:   PetscCall(DMGetBoundingBox(dm, xyl, xyr));
2104:   PetscCall(PetscDrawSetCoordinates(draw, xyl[0], xyl[1], xyr[0], xyr[1]));
2105:   PetscCall(PetscDrawClear(draw));

2107:   for (c = cStart; c < cEnd; ++c) {
2108:     PetscScalar       *coords = NULL;
2109:     const PetscScalar *coords_arr;
2110:     PetscInt           numCoords;
2111:     PetscBool          isDG;

2113:     PetscCall(DMPlexGetCellCoordinates(dm, c, &isDG, &numCoords, &coords_arr, &coords));
2114:     if (drawAffine) PetscCall(DMPlexDrawCell(dm, draw, lineColor, cellColor, c, coords));
2115:     else PetscCall(DMPlexDrawCellHighOrder(dm, draw, c, coords, edgeDiv, refCoords, edgeCoords));
2116:     PetscCall(DMPlexRestoreCellCoordinates(dm, c, &isDG, &numCoords, &coords_arr, &coords));
2117:   }
2118:   if (!drawAffine) PetscCall(PetscFree2(refCoords, edgeCoords));
2119:   PetscCall(PetscDrawFlush(draw));
2120:   PetscCall(PetscDrawPause(draw));
2121:   PetscCall(PetscDrawSave(draw));
2122:   PetscFunctionReturn(PETSC_SUCCESS);
2123: }

2125: static PetscErrorCode DMPlexCreateHighOrderSurrogate_Internal(DM dm, DM *hdm)
2126: {
2127:   DM           odm = dm, rdm = dm, cdm;
2128:   PetscFE      fe;
2129:   PetscSpace   sp;
2130:   PetscClassId id;
2131:   PetscInt     degree;
2132:   PetscBool    hoView = PETSC_TRUE;

2134:   PetscFunctionBegin;
2135:   PetscObjectOptionsBegin((PetscObject)dm);
2136:   PetscCall(PetscOptionsBool("-dm_plex_high_order_view", "Subsample to view meshes with high order coordinates", "DMPlexCreateHighOrderSurrogate_Internal", hoView, &hoView, NULL));
2137:   PetscOptionsEnd();
2138:   PetscCall(PetscObjectReference((PetscObject)dm));
2139:   *hdm = dm;
2140:   if (!hoView) PetscFunctionReturn(PETSC_SUCCESS);
2141:   PetscCall(DMGetCoordinateDM(dm, &cdm));
2142:   PetscCall(DMGetField(cdm, 0, NULL, (PetscObject *)&fe));
2143:   PetscCall(PetscObjectGetClassId((PetscObject)fe, &id));
2144:   if (id != PETSCFE_CLASSID) PetscFunctionReturn(PETSC_SUCCESS);
2145:   PetscCall(PetscFEGetBasisSpace(fe, &sp));
2146:   PetscCall(PetscSpaceGetDegree(sp, &degree, NULL));
2147:   for (PetscInt r = 0, rd = PetscCeilReal(((PetscReal)degree) / 2.); r < (PetscInt)PetscCeilReal(PetscLog2Real(degree)); ++r, rd = PetscCeilReal(((PetscReal)rd) / 2.)) {
2148:     DM  cdm, rcdm;
2149:     Mat In;
2150:     Vec cl, rcl;

2152:     PetscCall(DMRefine(odm, PetscObjectComm((PetscObject)odm), &rdm));
2153:     PetscCall(DMPlexCreateCoordinateSpace(rdm, rd, PETSC_FALSE, PETSC_FALSE));
2154:     PetscCall(PetscObjectSetName((PetscObject)rdm, "Refined Mesh with Linear Coordinates"));
2155:     PetscCall(DMGetCoordinateDM(odm, &cdm));
2156:     PetscCall(DMGetCoordinateDM(rdm, &rcdm));
2157:     PetscCall(DMGetCoordinatesLocal(odm, &cl));
2158:     PetscCall(DMGetCoordinatesLocal(rdm, &rcl));
2159:     PetscCall(DMSetCoarseDM(rcdm, cdm));
2160:     PetscCall(DMCreateInterpolation(cdm, rcdm, &In, NULL));
2161:     PetscCall(MatMult(In, cl, rcl));
2162:     PetscCall(MatDestroy(&In));
2163:     PetscCall(DMSetCoordinatesLocal(rdm, rcl));
2164:     PetscCall(DMDestroy(&odm));
2165:     odm = rdm;
2166:   }
2167:   *hdm = rdm;
2168:   PetscFunctionReturn(PETSC_SUCCESS);
2169: }

2171: #if PetscDefined(HAVE_EXODUSII)
2172:   #include <exodusII.h>
2173: #endif

2175: PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
2176: {
2177:   PetscBool isascii, ishdf5, isvtk, isdraw, flg, isglvis, isexodus, iscgns, ispython;
2178:   char      name[PETSC_MAX_PATH_LEN];

2180:   PetscFunctionBegin;
2183:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
2184:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERVTK, &isvtk));
2185:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2186:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
2187:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERGLVIS, &isglvis));
2188:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWEREXODUSII, &isexodus));
2189:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERCGNS, &iscgns));
2190:   PetscCall(PetscObjectHasFunction((PetscObject)viewer, "PetscViewerPythonViewObject_C", &ispython));
2191:   if (isascii) {
2192:     PetscViewerFormat format;
2193:     PetscCall(PetscViewerGetFormat(viewer, &format));
2194:     if (format == PETSC_VIEWER_ASCII_GLVIS) PetscCall(DMPlexView_GLVis(dm, viewer));
2195:     else PetscCall(DMPlexView_Ascii(dm, viewer));
2196:   } else if (ishdf5) {
2197: #if PetscDefined(HAVE_HDF5)
2198:     PetscCall(DMPlexView_HDF5_Internal(dm, viewer));
2199: #else
2200:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2201: #endif
2202:   } else if (isvtk) {
2203:     PetscCall(DMPlexVTKWriteAll((PetscObject)dm, viewer));
2204:   } else if (isdraw) {
2205:     DM hdm;

2207:     PetscCall(DMPlexCreateHighOrderSurrogate_Internal(dm, &hdm));
2208:     PetscCall(DMPlexView_Draw(hdm, viewer));
2209:     PetscCall(DMDestroy(&hdm));
2210:   } else if (isglvis) {
2211:     PetscCall(DMPlexView_GLVis(dm, viewer));
2212: #if PetscDefined(HAVE_EXODUSII)
2213:   } else if (isexodus) {
2214:     /*
2215:       ExodusII requires that all sets be part of exactly one cell set.
2216:       If the dm does not have a "Cell Sets" label defined, we create one
2217:       with ID 1, containing all cells.
2218:       Note that if the Cell Sets label is defined but does not cover all cells,
2219:       we may still have a problem. This should probably be checked here or in the viewer;
2220:     */
2221:     PetscInt numCS;
2222:     PetscCall(DMGetLabelSize(dm, "Cell Sets", &numCS));
2223:     if (!numCS) {
2224:       PetscInt cStart, cEnd, c;
2225:       PetscCall(DMCreateLabel(dm, "Cell Sets"));
2226:       PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
2227:       for (c = cStart; c < cEnd; ++c) PetscCall(DMSetLabelValue(dm, "Cell Sets", c, 1));
2228:     }
2229:     PetscCall(DMView_PlexExodusII(dm, viewer));
2230: #endif
2231: #if PetscDefined(HAVE_CGNS)
2232:   } else if (iscgns) {
2233:     PetscCall(DMView_PlexCGNS(dm, viewer));
2234: #endif
2235:   } else if (ispython) {
2236:     PetscCall(PetscViewerPythonViewObject(viewer, (PetscObject)dm));
2237:   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex writing", ((PetscObject)viewer)->type_name);
2238:   /* Optionally view the partition */
2239:   PetscCall(PetscOptionsHasName(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_partition_view", &flg));
2240:   if (flg) {
2241:     Vec ranks;
2242:     PetscCall(DMPlexCreateRankField(dm, &ranks));
2243:     PetscCall(VecView(ranks, viewer));
2244:     PetscCall(VecDestroy(&ranks));
2245:   }
2246:   /* Optionally view a label */
2247:   PetscCall(PetscOptionsGetString(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_label_view", name, sizeof(name), &flg));
2248:   if (flg) {
2249:     DMLabel label;
2250:     Vec     val;

2252:     PetscCall(DMGetLabel(dm, name, &label));
2253:     PetscCheck(label, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Label %s provided to -dm_label_view does not exist in this DM", name);
2254:     PetscCall(DMPlexCreateLabelField(dm, label, &val));
2255:     PetscCall(VecView(val, viewer));
2256:     PetscCall(VecDestroy(&val));
2257:   }
2258:   PetscFunctionReturn(PETSC_SUCCESS);
2259: }

2261: /*@
2262:   DMPlexTopologyView - Saves a `DMPLEX` topology into a file

2264:   Collective

2266:   Input Parameters:
2267: + dm     - The `DM` whose topology is to be saved
2268: - viewer - The `PetscViewer` to save it in

2270:   Level: advanced

2272: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMView()`, `DMPlexCoordinatesView()`, `DMPlexLabelsView()`, `DMPlexTopologyLoad()`, `PetscViewer`
2273: @*/
2274: PetscErrorCode DMPlexTopologyView(DM dm, PetscViewer viewer)
2275: {
2276:   PetscBool ishdf5;

2278:   PetscFunctionBegin;
2281:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2282:   PetscCall(PetscLogEventBegin(DMPLEX_TopologyView, viewer, 0, 0, 0));
2283:   if (ishdf5) {
2284: #if PetscDefined(HAVE_HDF5)
2285:     IS                globalPointNumbering;
2286:     PetscViewerFormat format;

2288:     PetscCall(PetscViewerGetFormat(viewer, &format));
2289:     PetscCheck(format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 output.", PetscViewerFormats[format]);
2290:     PetscCall(DMPlexCreatePointNumbering(dm, &globalPointNumbering));
2291:     PetscCall(DMPlexTopologyView_HDF5_Internal(dm, globalPointNumbering, viewer));
2292:     PetscCall(ISDestroy(&globalPointNumbering));
2293: #else
2294:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2295: #endif
2296:   }
2297:   PetscCall(PetscLogEventEnd(DMPLEX_TopologyView, viewer, 0, 0, 0));
2298:   PetscFunctionReturn(PETSC_SUCCESS);
2299: }

2301: /*@
2302:   DMPlexCoordinatesView - Saves `DMPLEX` coordinates into a file

2304:   Collective

2306:   Input Parameters:
2307: + dm     - The `DM` whose coordinates are to be saved
2308: - viewer - The `PetscViewer` for saving

2310:   Level: advanced

2312: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMView()`, `DMPlexTopologyView()`, `DMPlexLabelsView()`, `DMPlexCoordinatesLoad()`, `PetscViewer`
2313: @*/
2314: PetscErrorCode DMPlexCoordinatesView(DM dm, PetscViewer viewer)
2315: {
2316:   PetscBool ishdf5;

2318:   PetscFunctionBegin;
2321:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2322:   PetscCall(PetscLogEventBegin(DMPLEX_CoordinatesView, viewer, 0, 0, 0));
2323:   if (ishdf5) {
2324: #if PetscDefined(HAVE_HDF5)
2325:     PetscViewerFormat format;

2327:     PetscCall(PetscViewerGetFormat(viewer, &format));
2328:     PetscCheck(format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 output.", PetscViewerFormats[format]);
2329:     PetscCall(DMPlexCoordinatesView_HDF5_Internal(dm, viewer));
2330: #else
2331:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2332: #endif
2333:   }
2334:   PetscCall(PetscLogEventEnd(DMPLEX_CoordinatesView, viewer, 0, 0, 0));
2335:   PetscFunctionReturn(PETSC_SUCCESS);
2336: }

2338: /*@
2339:   DMPlexLabelsView - Saves `DMPLEX` labels into a file

2341:   Collective

2343:   Input Parameters:
2344: + dm     - The `DM` whose labels are to be saved
2345: - viewer - The `PetscViewer` for saving

2347:   Level: advanced

2349: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMView()`, `DMPlexTopologyView()`, `DMPlexCoordinatesView()`, `DMPlexLabelsLoad()`, `PetscViewer`
2350: @*/
2351: PetscErrorCode DMPlexLabelsView(DM dm, PetscViewer viewer)
2352: {
2353:   PetscBool ishdf5;

2355:   PetscFunctionBegin;
2358:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2359:   PetscCall(PetscLogEventBegin(DMPLEX_LabelsView, viewer, 0, 0, 0));
2360:   if (ishdf5) {
2361: #if PetscDefined(HAVE_HDF5)
2362:     IS                globalPointNumbering;
2363:     PetscViewerFormat format;

2365:     PetscCall(PetscViewerGetFormat(viewer, &format));
2366:     PetscCheck(format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
2367:     PetscCall(DMPlexCreatePointNumbering(dm, &globalPointNumbering));
2368:     PetscCall(DMPlexLabelsView_HDF5_Internal(dm, globalPointNumbering, viewer));
2369:     PetscCall(ISDestroy(&globalPointNumbering));
2370: #else
2371:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2372: #endif
2373:   }
2374:   PetscCall(PetscLogEventEnd(DMPLEX_LabelsView, viewer, 0, 0, 0));
2375:   PetscFunctionReturn(PETSC_SUCCESS);
2376: }

2378: /*@
2379:   DMPlexSectionView - Saves a section associated with a `DMPLEX`

2381:   Collective

2383:   Input Parameters:
2384: + dm        - The `DM` that contains the topology on which the section to be saved is defined
2385: . viewer    - The `PetscViewer` for saving
2386: - sectiondm - The `DM` that contains the section to be saved, can be `NULL`

2388:   Level: advanced

2390:   Notes:
2391:   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.

2393:   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 (or in case `sectiondm` is `NULL`) 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.

2395: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMView()`, `DMPlexTopologyView()`, `DMPlexCoordinatesView()`, `DMPlexLabelsView()`, `DMPlexGlobalVectorView()`, `DMPlexLocalVectorView()`, `PetscSectionView()`, `DMPlexSectionLoad()`, `PetscViewer`
2396: @*/
2397: PetscErrorCode DMPlexSectionView(DM dm, PetscViewer viewer, DM sectiondm)
2398: {
2399:   PetscBool ishdf5;

2401:   PetscFunctionBegin;
2404:   if (!sectiondm) sectiondm = dm;
2406:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2407:   PetscCall(PetscLogEventBegin(DMPLEX_SectionView, viewer, 0, 0, 0));
2408:   if (ishdf5) {
2409: #if PetscDefined(HAVE_HDF5)
2410:     PetscCall(DMPlexSectionView_HDF5_Internal(dm, viewer, sectiondm));
2411: #else
2412:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2413: #endif
2414:   }
2415:   PetscCall(PetscLogEventEnd(DMPLEX_SectionView, viewer, 0, 0, 0));
2416:   PetscFunctionReturn(PETSC_SUCCESS);
2417: }

2419: /*@
2420:   DMPlexGlobalVectorView - Saves a global vector

2422:   Collective

2424:   Input Parameters:
2425: + dm        - The `DM` that represents the topology
2426: . viewer    - The `PetscViewer` to save data with
2427: . sectiondm - The `DM` that contains the global section on which vec is defined, can be `NULL`
2428: - vec       - The global vector to be saved

2430:   Level: advanced

2432:   Notes:
2433:   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 (or in case `sectiondm` is `NULL`) 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.

2435:   Calling sequence:
2436: .vb
2437:        DMCreate(PETSC_COMM_WORLD, &dm);
2438:        DMSetType(dm, DMPLEX);
2439:        PetscObjectSetName((PetscObject)dm, "topologydm_name");
2440:        DMClone(dm, &sectiondm);
2441:        PetscObjectSetName((PetscObject)sectiondm, "sectiondm_name");
2442:        PetscSectionCreate(PETSC_COMM_WORLD, &section);
2443:        DMPlexGetChart(sectiondm, &pStart, &pEnd);
2444:        PetscSectionSetChart(section, pStart, pEnd);
2445:        PetscSectionSetUp(section);
2446:        DMSetLocalSection(sectiondm, section);
2447:        PetscSectionDestroy(&section);
2448:        DMGetGlobalVector(sectiondm, &vec);
2449:        PetscObjectSetName((PetscObject)vec, "vec_name");
2450:        DMPlexTopologyView(dm, viewer);
2451:        DMPlexSectionView(dm, viewer, sectiondm);
2452:        DMPlexGlobalVectorView(dm, viewer, sectiondm, vec);
2453:        DMRestoreGlobalVector(sectiondm, &vec);
2454:        DMDestroy(&sectiondm);
2455:        DMDestroy(&dm);
2456: .ve

2458: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTopologyView()`, `DMPlexSectionView()`, `DMPlexLocalVectorView()`, `DMPlexGlobalVectorLoad()`, `DMPlexLocalVectorLoad()`
2459: @*/
2460: PetscErrorCode DMPlexGlobalVectorView(DM dm, PetscViewer viewer, DM sectiondm, Vec vec)
2461: {
2462:   PetscBool ishdf5;

2464:   PetscFunctionBegin;
2467:   if (!sectiondm) sectiondm = dm;
2470:   /* Check consistency */
2471:   {
2472:     PetscSection section;
2473:     PetscBool    includesConstraints;
2474:     PetscInt     m, m1;

2476:     PetscCall(VecGetLocalSize(vec, &m1));
2477:     PetscCall(DMGetGlobalSection(sectiondm, &section));
2478:     PetscCall(PetscSectionGetIncludesConstraints(section, &includesConstraints));
2479:     if (includesConstraints) PetscCall(PetscSectionGetStorageSize(section, &m));
2480:     else PetscCall(PetscSectionGetConstrainedStorageSize(section, &m));
2481:     PetscCheck(m1 == m, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Global vector size (%" PetscInt_FMT ") != global section storage size (%" PetscInt_FMT ")", m1, m);
2482:   }
2483:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2484:   PetscCall(PetscLogEventBegin(DMPLEX_GlobalVectorView, viewer, 0, 0, 0));
2485:   if (ishdf5) {
2486: #if PetscDefined(HAVE_HDF5)
2487:     PetscCall(DMPlexGlobalVectorView_HDF5_Internal(dm, viewer, sectiondm, vec));
2488: #else
2489:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2490: #endif
2491:   }
2492:   PetscCall(PetscLogEventEnd(DMPLEX_GlobalVectorView, viewer, 0, 0, 0));
2493:   PetscFunctionReturn(PETSC_SUCCESS);
2494: }

2496: /*@
2497:   DMPlexLocalVectorView - Saves a local vector

2499:   Collective

2501:   Input Parameters:
2502: + dm        - The `DM` that represents the topology
2503: . viewer    - The `PetscViewer` to save data with
2504: . sectiondm - The `DM` that contains the local section on which `vec` is defined, can be `NULL`
2505: - vec       - The local vector to be saved

2507:   Level: advanced

2509:   Note:
2510:   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 (or in case `sectiondm` is `NULL`) 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.

2512:   Calling sequence:
2513: .vb
2514:        DMCreate(PETSC_COMM_WORLD, &dm);
2515:        DMSetType(dm, DMPLEX);
2516:        PetscObjectSetName((PetscObject)dm, "topologydm_name");
2517:        DMClone(dm, &sectiondm);
2518:        PetscObjectSetName((PetscObject)sectiondm, "sectiondm_name");
2519:        PetscSectionCreate(PETSC_COMM_WORLD, &section);
2520:        DMPlexGetChart(sectiondm, &pStart, &pEnd);
2521:        PetscSectionSetChart(section, pStart, pEnd);
2522:        PetscSectionSetUp(section);
2523:        DMSetLocalSection(sectiondm, section);
2524:        DMGetLocalVector(sectiondm, &vec);
2525:        PetscObjectSetName((PetscObject)vec, "vec_name");
2526:        DMPlexTopologyView(dm, viewer);
2527:        DMPlexSectionView(dm, viewer, sectiondm);
2528:        DMPlexLocalVectorView(dm, viewer, sectiondm, vec);
2529:        DMRestoreLocalVector(sectiondm, &vec);
2530:        DMDestroy(&sectiondm);
2531:        DMDestroy(&dm);
2532: .ve

2534: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTopologyView()`, `DMPlexSectionView()`, `DMPlexGlobalVectorView()`, `DMPlexGlobalVectorLoad()`, `DMPlexLocalVectorLoad()`
2535: @*/
2536: PetscErrorCode DMPlexLocalVectorView(DM dm, PetscViewer viewer, DM sectiondm, Vec vec)
2537: {
2538:   PetscBool ishdf5;

2540:   PetscFunctionBegin;
2543:   if (!sectiondm) sectiondm = dm;
2546:   /* Check consistency */
2547:   {
2548:     PetscSection section;
2549:     PetscBool    includesConstraints;
2550:     PetscInt     m, m1;

2552:     PetscCall(VecGetLocalSize(vec, &m1));
2553:     PetscCall(DMGetLocalSection(sectiondm, &section));
2554:     PetscCall(PetscSectionGetIncludesConstraints(section, &includesConstraints));
2555:     if (includesConstraints) PetscCall(PetscSectionGetStorageSize(section, &m));
2556:     else PetscCall(PetscSectionGetConstrainedStorageSize(section, &m));
2557:     PetscCheck(m1 == m, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Local vector size (%" PetscInt_FMT ") != local section storage size (%" PetscInt_FMT ")", m1, m);
2558:   }
2559:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2560:   PetscCall(PetscLogEventBegin(DMPLEX_LocalVectorView, viewer, 0, 0, 0));
2561:   if (ishdf5) {
2562: #if PetscDefined(HAVE_HDF5)
2563:     PetscCall(DMPlexLocalVectorView_HDF5_Internal(dm, viewer, sectiondm, vec));
2564: #else
2565:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2566: #endif
2567:   }
2568:   PetscCall(PetscLogEventEnd(DMPLEX_LocalVectorView, viewer, 0, 0, 0));
2569:   PetscFunctionReturn(PETSC_SUCCESS);
2570: }

2572: PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
2573: {
2574:   PetscBool ishdf5;

2576:   PetscFunctionBegin;
2579:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2580:   if (ishdf5) {
2581: #if PetscDefined(HAVE_HDF5)
2582:     PetscViewerFormat format;
2583:     PetscCall(PetscViewerGetFormat(viewer, &format));
2584:     if (format == PETSC_VIEWER_HDF5_XDMF || format == PETSC_VIEWER_HDF5_VIZ) {
2585:       PetscCall(DMPlexLoad_HDF5_Xdmf_Internal(dm, viewer));
2586:     } else if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
2587:       PetscCall(DMPlexLoad_HDF5_Internal(dm, viewer));
2588:     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
2589:     PetscFunctionReturn(PETSC_SUCCESS);
2590: #else
2591:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2592: #endif
2593:   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex loading", ((PetscObject)viewer)->type_name);
2594: }

2596: /*@
2597:   DMPlexTopologyLoad - Loads a topology into a `DMPLEX`

2599:   Collective

2601:   Input Parameters:
2602: + dm     - The `DM` into which the topology is loaded
2603: - viewer - The `PetscViewer` for the saved topology

2605:   Output Parameter:
2606: . globalToLocalPointSF - The `PetscSF` that pushes points in [0, N) to the associated points in the loaded `DMPLEX`, where N is the global number of points;
2607:   `NULL` if unneeded

2609:   Level: advanced

2611: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMLoad()`, `DMPlexCoordinatesLoad()`, `DMPlexLabelsLoad()`, `DMView()`, `PetscViewerHDF5Open()`, `PetscViewerPushFormat()`,
2612:           `PetscViewer`, `PetscSF`
2613: @*/
2614: PetscErrorCode DMPlexTopologyLoad(DM dm, PetscViewer viewer, PetscSF *globalToLocalPointSF)
2615: {
2616:   PetscBool ishdf5;

2618:   PetscFunctionBegin;
2621:   if (globalToLocalPointSF) PetscAssertPointer(globalToLocalPointSF, 3);
2622:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2623:   PetscCall(PetscLogEventBegin(DMPLEX_TopologyLoad, viewer, 0, 0, 0));
2624:   if (ishdf5) {
2625: #if PetscDefined(HAVE_HDF5)
2626:     PetscViewerFormat format;

2628:     PetscCall(PetscViewerGetFormat(viewer, &format));
2629:     PetscCheck(format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
2630:     PetscCall(DMPlexTopologyLoad_HDF5_Internal(dm, viewer, globalToLocalPointSF));
2631: #else
2632:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2633: #endif
2634:   }
2635:   PetscCall(PetscLogEventEnd(DMPLEX_TopologyLoad, viewer, 0, 0, 0));
2636:   PetscFunctionReturn(PETSC_SUCCESS);
2637: }

2639: /*@
2640:   DMPlexCoordinatesLoad - Loads coordinates into a `DMPLEX`

2642:   Collective

2644:   Input Parameters:
2645: + dm                   - The `DM` into which the coordinates are loaded
2646: . viewer               - The `PetscViewer` for the saved coordinates
2647: - globalToLocalPointSF - The `PetscSF` returned by `DMPlexTopologyLoad()` when loading dm from viewer

2649:   Level: advanced

2651: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMLoad()`, `DMPlexTopologyLoad()`, `DMPlexLabelsLoad()`, `DMView()`, `PetscViewerHDF5Open()`, `PetscViewerPushFormat()`,
2652:           `PetscSF`, `PetscViewer`
2653: @*/
2654: PetscErrorCode DMPlexCoordinatesLoad(DM dm, PetscViewer viewer, PetscSF globalToLocalPointSF)
2655: {
2656:   PetscBool ishdf5;

2658:   PetscFunctionBegin;
2662:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2663:   PetscCall(PetscLogEventBegin(DMPLEX_CoordinatesLoad, viewer, 0, 0, 0));
2664:   if (ishdf5) {
2665: #if PetscDefined(HAVE_HDF5)
2666:     PetscViewerFormat format;

2668:     PetscCall(PetscViewerGetFormat(viewer, &format));
2669:     PetscCheck(format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
2670:     PetscCall(DMPlexCoordinatesLoad_HDF5_Internal(dm, viewer, globalToLocalPointSF));
2671: #else
2672:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2673: #endif
2674:   }
2675:   PetscCall(PetscLogEventEnd(DMPLEX_CoordinatesLoad, viewer, 0, 0, 0));
2676:   PetscFunctionReturn(PETSC_SUCCESS);
2677: }

2679: /*@
2680:   DMPlexLabelsLoad - Loads labels into a `DMPLEX`

2682:   Collective

2684:   Input Parameters:
2685: + dm                   - The `DM` into which the labels are loaded
2686: . viewer               - The `PetscViewer` for the saved labels
2687: - globalToLocalPointSF - The `PetscSF` returned by `DMPlexTopologyLoad()` when loading `dm` from viewer

2689:   Level: advanced

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

2694: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMLoad()`, `DMPlexTopologyLoad()`, `DMPlexCoordinatesLoad()`, `DMView()`, `PetscViewerHDF5Open()`, `PetscViewerPushFormat()`,
2695:           `PetscSF`, `PetscViewer`
2696: @*/
2697: PetscErrorCode DMPlexLabelsLoad(DM dm, PetscViewer viewer, PetscSF globalToLocalPointSF)
2698: {
2699:   PetscBool ishdf5;

2701:   PetscFunctionBegin;
2705:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2706:   PetscCall(PetscLogEventBegin(DMPLEX_LabelsLoad, viewer, 0, 0, 0));
2707:   if (ishdf5) {
2708: #if PetscDefined(HAVE_HDF5)
2709:     PetscViewerFormat format;

2711:     PetscCall(PetscViewerGetFormat(viewer, &format));
2712:     PetscCheck(format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
2713:     PetscCall(DMPlexLabelsLoad_HDF5_Internal(dm, viewer, globalToLocalPointSF));
2714: #else
2715:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2716: #endif
2717:   }
2718:   PetscCall(PetscLogEventEnd(DMPLEX_LabelsLoad, viewer, 0, 0, 0));
2719:   PetscFunctionReturn(PETSC_SUCCESS);
2720: }

2722: /*@
2723:   DMPlexSectionLoad - Loads section into a `DMPLEX`

2725:   Collective

2727:   Input Parameters:
2728: + dm                   - The `DM` that represents the topology
2729: . viewer               - The `PetscViewer` that represents the on-disk section (sectionA)
2730: . sectiondm            - The `DM` into which the on-disk section (sectionA) is migrated, can be `NULL`
2731: - globalToLocalPointSF - The `PetscSF` returned by `DMPlexTopologyLoad(`) when loading dm from viewer

2733:   Output Parameters:
2734: + globalDofSF - The `PetscSF` 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)
2735: - localDofSF  - The `PetscSF` 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)

2737:   Level: advanced

2739:   Notes:
2740:   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.

2742:   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 (or in case `sectiondm` is `NULL`) 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.

2744:   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.

2746:   Example using 2 processes:
2747: .vb
2748:   NX (number of points on dm): 4
2749:   sectionA                   : the on-disk section
2750:   vecA                       : a vector associated with sectionA
2751:   sectionB                   : sectiondm's local section constructed in this function
2752:   vecB (local)               : a vector associated with sectiondm's local section
2753:   vecB (global)              : a vector associated with sectiondm's global section

2755:                                      rank 0    rank 1
2756:   vecA (global)                  : [.0 .4 .1 | .2 .3]        <- to be loaded in DMPlexGlobalVectorLoad() or DMPlexLocalVectorLoad()
2757:   sectionA->atlasOff             :       0 2 | 1             <- loaded in PetscSectionLoad()
2758:   sectionA->atlasDof             :       1 3 | 1             <- loaded in PetscSectionLoad()
2759:   sectionA's global point numbers:       0 2 | 3             <- loaded in DMPlexSectionLoad()
2760:   [0, NX)                        :       0 1 | 2 3           <- conceptual partition used in globalToLocalPointSF
2761:   sectionB's global point numbers:     0 1 3 | 3 2           <- associated with [0, NX) by globalToLocalPointSF
2762:   sectionB->atlasDof             :     1 0 1 | 1 3
2763:   sectionB->atlasOff (no perm)   :     0 1 1 | 0 1
2764:   vecB (local)                   :   [.0 .4] | [.4 .1 .2 .3] <- to be constructed by calling DMPlexLocalVectorLoad() with localDofSF
2765:   vecB (global)                  :    [.0 .4 | .1 .2 .3]     <- to be constructed by calling DMPlexGlobalVectorLoad() with globalDofSF
2766: .ve
2767:   where "|" represents a partition of loaded data, and global point 3 is assumed to be owned by rank 0.

2769: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMLoad()`, `DMPlexTopologyLoad()`, `DMPlexCoordinatesLoad()`, `DMPlexLabelsLoad()`, `DMPlexGlobalVectorLoad()`, `DMPlexLocalVectorLoad()`, `PetscSectionLoad()`, `DMPlexSectionView()`, `PetscSF`, `PetscViewer`
2770: @*/
2771: PetscErrorCode DMPlexSectionLoad(DM dm, PetscViewer viewer, PeOp DM sectiondm, PetscSF globalToLocalPointSF, PeOp PetscSF *globalDofSF, PeOp PetscSF *localDofSF)
2772: {
2773:   PetscBool ishdf5;

2775:   PetscFunctionBegin;
2778:   if (!sectiondm) sectiondm = dm;
2781:   if (globalDofSF) PetscAssertPointer(globalDofSF, 5);
2782:   if (localDofSF) PetscAssertPointer(localDofSF, 6);
2783:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2784:   PetscCall(PetscLogEventBegin(DMPLEX_SectionLoad, viewer, 0, 0, 0));
2785:   if (ishdf5) {
2786: #if PetscDefined(HAVE_HDF5)
2787:     PetscCall(DMPlexSectionLoad_HDF5_Internal(dm, viewer, sectiondm, globalToLocalPointSF, globalDofSF, localDofSF));
2788: #else
2789:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2790: #endif
2791:   }
2792:   PetscCall(PetscLogEventEnd(DMPLEX_SectionLoad, viewer, 0, 0, 0));
2793:   PetscFunctionReturn(PETSC_SUCCESS);
2794: }

2796: /*@
2797:   DMPlexGlobalVectorLoad - Loads on-disk vector data into a global vector

2799:   Collective

2801:   Input Parameters:
2802: + dm        - The `DM` that represents the topology
2803: . viewer    - The `PetscViewer` that represents the on-disk vector data
2804: . sectiondm - The `DM` that contains the global section on which vec is defined, can be `NULL`
2805: . sf        - The `PetscSF` that migrates the on-disk vector data into vec
2806: - vec       - The global vector to set values of

2808:   Level: advanced

2810:   Notes:
2811:   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 (or in case `sectiondm` is `NULL`) 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.

2813:   Calling sequence:
2814: .vb
2815:        DMCreate(PETSC_COMM_WORLD, &dm);
2816:        DMSetType(dm, DMPLEX);
2817:        PetscObjectSetName((PetscObject)dm, "topologydm_name");
2818:        DMPlexTopologyLoad(dm, viewer, &sfX);
2819:        DMClone(dm, &sectiondm);
2820:        PetscObjectSetName((PetscObject)sectiondm, "sectiondm_name");
2821:        DMPlexSectionLoad(dm, viewer, sectiondm, sfX, &gsf, NULL);
2822:        DMGetGlobalVector(sectiondm, &vec);
2823:        PetscObjectSetName((PetscObject)vec, "vec_name");
2824:        DMPlexGlobalVectorLoad(dm, viewer, sectiondm, gsf, vec);
2825:        DMRestoreGlobalVector(sectiondm, &vec);
2826:        PetscSFDestroy(&gsf);
2827:        PetscSFDestroy(&sfX);
2828:        DMDestroy(&sectiondm);
2829:        DMDestroy(&dm);
2830: .ve

2832: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTopologyLoad()`, `DMPlexSectionLoad()`, `DMPlexLocalVectorLoad()`, `DMPlexGlobalVectorView()`, `DMPlexLocalVectorView()`,
2833:           `PetscSF`, `PetscViewer`
2834: @*/
2835: PetscErrorCode DMPlexGlobalVectorLoad(DM dm, PetscViewer viewer, DM sectiondm, PetscSF sf, Vec vec)
2836: {
2837:   PetscBool ishdf5;

2839:   PetscFunctionBegin;
2842:   if (!sectiondm) sectiondm = dm;
2846:   /* Check consistency */
2847:   {
2848:     PetscSection section;
2849:     PetscBool    includesConstraints;
2850:     PetscInt     m, m1;

2852:     PetscCall(VecGetLocalSize(vec, &m1));
2853:     PetscCall(DMGetGlobalSection(sectiondm, &section));
2854:     PetscCall(PetscSectionGetIncludesConstraints(section, &includesConstraints));
2855:     if (includesConstraints) PetscCall(PetscSectionGetStorageSize(section, &m));
2856:     else PetscCall(PetscSectionGetConstrainedStorageSize(section, &m));
2857:     PetscCheck(m1 == m, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Global vector size (%" PetscInt_FMT ") != global section storage size (%" PetscInt_FMT ")", m1, m);
2858:   }
2859:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2860:   PetscCall(PetscLogEventBegin(DMPLEX_GlobalVectorLoad, viewer, 0, 0, 0));
2861:   if (ishdf5) {
2862: #if PetscDefined(HAVE_HDF5)
2863:     PetscCall(DMPlexVecLoad_HDF5_Internal(dm, viewer, sectiondm, sf, vec));
2864: #else
2865:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2866: #endif
2867:   }
2868:   PetscCall(PetscLogEventEnd(DMPLEX_GlobalVectorLoad, viewer, 0, 0, 0));
2869:   PetscFunctionReturn(PETSC_SUCCESS);
2870: }

2872: /*@
2873:   DMPlexLocalVectorLoad - Loads on-disk vector data into a local vector

2875:   Collective

2877:   Input Parameters:
2878: + dm        - The `DM` that represents the topology
2879: . viewer    - The `PetscViewer` that represents the on-disk vector data
2880: . sectiondm - The `DM` that contains the local section on which vec is defined, can be `NULL`
2881: . sf        - The `PetscSF` that migrates the on-disk vector data into vec
2882: - vec       - The local vector to set values of

2884:   Level: advanced

2886:   Notes:
2887:   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 (or in case `sectiondm` is `NULL`) 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.

2889:   Calling sequence:
2890: .vb
2891:        DMCreate(PETSC_COMM_WORLD, &dm);
2892:        DMSetType(dm, DMPLEX);
2893:        PetscObjectSetName((PetscObject)dm, "topologydm_name");
2894:        DMPlexTopologyLoad(dm, viewer, &sfX);
2895:        DMClone(dm, &sectiondm);
2896:        PetscObjectSetName((PetscObject)sectiondm, "sectiondm_name");
2897:        DMPlexSectionLoad(dm, viewer, sectiondm, sfX, NULL, &lsf);
2898:        DMGetLocalVector(sectiondm, &vec);
2899:        PetscObjectSetName((PetscObject)vec, "vec_name");
2900:        DMPlexLocalVectorLoad(dm, viewer, sectiondm, lsf, vec);
2901:        DMRestoreLocalVector(sectiondm, &vec);
2902:        PetscSFDestroy(&lsf);
2903:        PetscSFDestroy(&sfX);
2904:        DMDestroy(&sectiondm);
2905:        DMDestroy(&dm);
2906: .ve

2908: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTopologyLoad()`, `DMPlexSectionLoad()`, `DMPlexGlobalVectorLoad()`, `DMPlexGlobalVectorView()`, `DMPlexLocalVectorView()`,
2909:           `PetscSF`, `PetscViewer`
2910: @*/
2911: PetscErrorCode DMPlexLocalVectorLoad(DM dm, PetscViewer viewer, DM sectiondm, PetscSF sf, Vec vec)
2912: {
2913:   PetscBool ishdf5;

2915:   PetscFunctionBegin;
2918:   if (!sectiondm) sectiondm = dm;
2922:   /* Check consistency */
2923:   {
2924:     PetscSection section;
2925:     PetscBool    includesConstraints;
2926:     PetscInt     m, m1;

2928:     PetscCall(VecGetLocalSize(vec, &m1));
2929:     PetscCall(DMGetLocalSection(sectiondm, &section));
2930:     PetscCall(PetscSectionGetIncludesConstraints(section, &includesConstraints));
2931:     if (includesConstraints) PetscCall(PetscSectionGetStorageSize(section, &m));
2932:     else PetscCall(PetscSectionGetConstrainedStorageSize(section, &m));
2933:     PetscCheck(m1 == m, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Local vector size (%" PetscInt_FMT ") != local section storage size (%" PetscInt_FMT ")", m1, m);
2934:   }
2935:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
2936:   PetscCall(PetscLogEventBegin(DMPLEX_LocalVectorLoad, viewer, 0, 0, 0));
2937:   if (ishdf5) {
2938: #if PetscDefined(HAVE_HDF5)
2939:     PetscCall(DMPlexVecLoad_HDF5_Internal(dm, viewer, sectiondm, sf, vec));
2940: #else
2941:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2942: #endif
2943:   }
2944:   PetscCall(PetscLogEventEnd(DMPLEX_LocalVectorLoad, viewer, 0, 0, 0));
2945:   PetscFunctionReturn(PETSC_SUCCESS);
2946: }

2948: PetscErrorCode DMDestroy_Plex(DM dm)
2949: {
2950:   DM_Plex *mesh = (DM_Plex *)dm->data;

2952:   PetscFunctionBegin;
2953:   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMSetUpGLVisViewer_C", NULL));
2954:   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBoundaryValues_C", NULL));
2955:   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertTimeDerivativeBoundaryValues_C", NULL));
2956:   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBounds_C", NULL));
2957:   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMCreateNeumannOverlap_C", NULL));
2958:   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMInterpolateSolution_C", NULL));
2959:   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetOverlap_C", NULL));
2960:   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeGetDefault_C", NULL));
2961:   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeSetDefault_C", NULL));
2962:   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "MatComputeNeumannOverlap_C", NULL));
2963:   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderGetDefault_C", NULL));
2964:   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderSetDefault_C", NULL));
2965:   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionGetDefault_C", NULL));
2966:   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionSetDefault_C", NULL));
2967:   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionGetType_C", NULL));
2968:   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionSetType_C", NULL));
2969:   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetOverlap_C", NULL));
2970:   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexSetOverlap_C", NULL));
2971:   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetUseCeed_C", NULL));
2972:   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexSetUseCeed_C", NULL));
2973:   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMGetIsoperiodicPointSF_C", NULL));
2974:   if (--mesh->refct > 0) PetscFunctionReturn(PETSC_SUCCESS);
2975:   PetscCall(PetscSectionDestroy(&mesh->coneSection));
2976:   PetscCall(PetscFree(mesh->cones));
2977:   PetscCall(PetscFree(mesh->coneOrientations));
2978:   PetscCall(PetscSectionDestroy(&mesh->supportSection));
2979:   PetscCall(PetscSectionDestroy(&mesh->subdomainSection));
2980:   PetscCall(PetscFree(mesh->supports));
2981:   PetscCall(PetscFree(mesh->cellTypes));
2982:   PetscCall(DMPlexTransformDestroy(&mesh->tr));
2983:   PetscCall(PetscFree(mesh->tetgenOpts));
2984:   PetscCall(PetscFree(mesh->triangleOpts));
2985:   PetscCall(PetscFree(mesh->transformType));
2986:   PetscCall(PetscFree(mesh->distributionName));
2987:   PetscCall(PetscPartitionerDestroy(&mesh->partitioner));
2988:   PetscCall(DMLabelDestroy(&mesh->subpointMap));
2989:   PetscCall(ISDestroy(&mesh->subpointIS));
2990:   PetscCall(ISDestroy(&mesh->globalVertexNumbers));
2991:   PetscCall(ISDestroy(&mesh->globalCellNumbers));
2992:   if (mesh->periodic.face_sfs) {
2993:     for (PetscInt i = 0; i < mesh->periodic.num_face_sfs; i++) PetscCall(PetscSFDestroy(&mesh->periodic.face_sfs[i]));
2994:     PetscCall(PetscFree(mesh->periodic.face_sfs));
2995:   }
2996:   PetscCall(PetscSFDestroy(&mesh->periodic.composed_sf));
2997:   if (mesh->periodic.periodic_points) {
2998:     for (PetscInt i = 0; i < mesh->periodic.num_face_sfs; i++) PetscCall(ISDestroy(&mesh->periodic.periodic_points[i]));
2999:     PetscCall(PetscFree(mesh->periodic.periodic_points));
3000:   }
3001:   PetscCall(PetscFree(mesh->periodic.transform));
3002:   PetscCall(PetscSectionDestroy(&mesh->anchorSection));
3003:   PetscCall(ISDestroy(&mesh->anchorIS));
3004:   PetscCall(PetscSectionDestroy(&mesh->parentSection));
3005:   PetscCall(PetscFree(mesh->parents));
3006:   PetscCall(PetscFree(mesh->childIDs));
3007:   PetscCall(PetscSectionDestroy(&mesh->childSection));
3008:   PetscCall(PetscFree(mesh->children));
3009:   PetscCall(DMDestroy(&mesh->referenceTree));
3010:   PetscCall(PetscGridHashDestroy(&mesh->lbox));
3011:   PetscCall(PetscFree(mesh->neighbors));
3012:   PetscCall(PetscFree(mesh->metricCtx));
3013:   if (mesh->nonempty_comm != MPI_COMM_NULL && mesh->nonempty_comm != MPI_COMM_SELF) PetscCallMPI(MPI_Comm_free(&mesh->nonempty_comm));
3014:   PetscCall(DMPlexTransformDestroy(&mesh->transform));
3015:   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
3016:   PetscCall(PetscFree(mesh));
3017:   PetscFunctionReturn(PETSC_SUCCESS);
3018: }

3020: PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
3021: {
3022:   PetscSection           sectionGlobal, sectionLocal;
3023:   PetscInt               bs = -1, mbs;
3024:   PetscInt               localSize, localStart = 0;
3025:   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
3026:   MatType                mtype;
3027:   ISLocalToGlobalMapping ltog;

3029:   PetscFunctionBegin;
3030:   PetscCall(MatInitializePackage());
3031:   mtype = dm->mattype;
3032:   PetscCall(DMGetLocalSection(dm, &sectionLocal));
3033:   PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
3034:   /* PetscCall(PetscSectionGetStorageSize(sectionGlobal, &localSize)); */
3035:   PetscCall(PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize));
3036:   PetscCallMPI(MPI_Exscan(&localSize, &localStart, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)dm)));
3037:   PetscCall(MatCreate(PetscObjectComm((PetscObject)dm), J));
3038:   PetscCall(MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE));
3039:   PetscCall(MatSetType(*J, mtype));
3040:   PetscCall(MatSetFromOptions(*J));
3041:   PetscCall(MatGetBlockSize(*J, &mbs));
3042:   if (mbs > 1) bs = mbs;
3043:   PetscCall(PetscStrcmp(mtype, MATSHELL, &isShell));
3044:   PetscCall(PetscStrcmp(mtype, MATBAIJ, &isBlock));
3045:   PetscCall(PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock));
3046:   PetscCall(PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock));
3047:   PetscCall(PetscStrcmp(mtype, MATSBAIJ, &isSymBlock));
3048:   PetscCall(PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock));
3049:   PetscCall(PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock));
3050:   PetscCall(PetscStrcmp(mtype, MATIS, &isMatIS));
3051:   if (!isShell) {
3052:     // There are three states with pblocks, since block starts can have no dofs:
3053:     // UNKNOWN) New Block:   An open block has been signalled by pblocks[p] == 1
3054:     // TRUE)    Block Start: The first entry in a block has been added
3055:     // FALSE)   Block Add:   An additional block entry has been added, since pblocks[p] == 0
3056:     PetscBT         blst;
3057:     PetscBool3      bstate     = PETSC_BOOL3_UNKNOWN;
3058:     PetscBool       fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
3059:     const PetscInt *perm       = NULL;
3060:     PetscInt       *dnz, *onz, *dnzu, *onzu, bsLocal[2], bsMinMax[2], *pblocks;
3061:     PetscInt        pStart, pEnd, dof, cdof, num_fields;

3063:     PetscCall(DMGetLocalToGlobalMapping(dm, &ltog));
3064:     PetscCall(PetscSectionGetBlockStarts(sectionLocal, &blst));
3065:     if (sectionLocal->perm) PetscCall(ISGetIndices(sectionLocal->perm, &perm));

3067:     PetscCall(PetscCalloc1(localSize, &pblocks));
3068:     PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
3069:     PetscCall(PetscSectionGetNumFields(sectionGlobal, &num_fields));
3070:     // We need to process in the permuted order to get block sizes right
3071:     for (PetscInt point = pStart; point < pEnd; ++point) {
3072:       const PetscInt p = perm ? perm[point] : point;

3074:       switch (dm->blocking_type) {
3075:       case DM_BLOCKING_TOPOLOGICAL_POINT: { // One block per topological point
3076:         PetscInt bdof, offset;

3078:         PetscCall(PetscSectionGetDof(sectionGlobal, p, &dof));
3079:         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &offset));
3080:         PetscCall(PetscSectionGetConstraintDof(sectionGlobal, p, &cdof));
3081:         if (blst && PetscBTLookup(blst, p)) bstate = PETSC_BOOL3_UNKNOWN;
3082:         if (dof > 0) {
3083:           // State change
3084:           if (bstate == PETSC_BOOL3_UNKNOWN) bstate = PETSC_BOOL3_TRUE;
3085:           else if (bstate == PETSC_BOOL3_TRUE && blst && !PetscBTLookup(blst, p)) bstate = PETSC_BOOL3_FALSE;

3087:           for (PetscInt i = 0; i < dof - cdof; ++i) pblocks[offset - localStart + i] = dof - cdof;
3088:           // Signal block concatenation
3089:           if (bstate == PETSC_BOOL3_FALSE && dof - cdof) pblocks[offset - localStart] = -(dof - cdof);
3090:         }
3091:         dof  = dof < 0 ? -(dof + 1) : dof;
3092:         bdof = cdof && (dof - cdof) ? 1 : dof;
3093:         if (dof) {
3094:           if (bs < 0) {
3095:             bs = bdof;
3096:           } else if (bs != bdof) {
3097:             bs = 1;
3098:           }
3099:         }
3100:       } break;
3101:       case DM_BLOCKING_FIELD_NODE: {
3102:         for (PetscInt field = 0; field < num_fields; field++) {
3103:           PetscInt num_comp, bdof, offset;
3104:           PetscCall(PetscSectionGetFieldComponents(sectionGlobal, field, &num_comp));
3105:           PetscCall(PetscSectionGetFieldDof(sectionGlobal, p, field, &dof));
3106:           if (dof < 0) continue;
3107:           PetscCall(PetscSectionGetFieldOffset(sectionGlobal, p, field, &offset));
3108:           PetscCall(PetscSectionGetFieldConstraintDof(sectionGlobal, p, field, &cdof));
3109:           PetscAssert(dof % num_comp == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Point %" PetscInt_FMT " field %" PetscInt_FMT " has %" PetscInt_FMT " dof, not divisible by %" PetscInt_FMT " component ", p, field, dof, num_comp);
3110:           PetscInt num_nodes = dof / num_comp;
3111:           for (PetscInt i = 0; i < dof - cdof; i++) pblocks[offset - localStart + i] = (dof - cdof) / num_nodes;
3112:           // Handle possibly constant block size (unlikely)
3113:           bdof = cdof && (dof - cdof) ? 1 : dof;
3114:           if (dof) {
3115:             if (bs < 0) {
3116:               bs = bdof;
3117:             } else if (bs != bdof) {
3118:               bs = 1;
3119:             }
3120:           }
3121:         }
3122:       } break;
3123:       }
3124:     }
3125:     if (sectionLocal->perm) PetscCall(ISRestoreIndices(sectionLocal->perm, &perm));
3126:     /* Must have same blocksize on all procs (some might have no points) */
3127:     bsLocal[0] = bs < 0 ? PETSC_INT_MAX : bs;
3128:     bsLocal[1] = bs;
3129:     PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax));
3130:     if (bsMinMax[0] != bsMinMax[1]) bs = 1;
3131:     else bs = bsMinMax[0];
3132:     bs = PetscMax(1, bs);
3133:     PetscCall(MatSetLocalToGlobalMapping(*J, ltog, ltog));
3134:     if (dm->prealloc_skip) { // User will likely use MatSetPreallocationCOO(), but still set structural parameters
3135:       PetscCall(MatSetBlockSize(*J, bs));
3136:       PetscCall(MatSetUp(*J));
3137:     } else {
3138:       PetscCall(PetscCalloc4(localSize / bs, &dnz, localSize / bs, &onz, localSize / bs, &dnzu, localSize / bs, &onzu));
3139:       PetscCall(DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix));
3140:       PetscCall(PetscFree4(dnz, onz, dnzu, onzu));
3141:     }
3142:     if (pblocks) { // Consolidate blocks
3143:       PetscInt nblocks = 0;
3144:       pblocks[0]       = PetscAbs(pblocks[0]);
3145:       for (PetscInt i = 0; i < localSize; i += PetscMax(1, pblocks[i])) {
3146:         if (pblocks[i] == 0) continue;
3147:         // Negative block size indicates the blocks should be concatenated
3148:         if (pblocks[i] < 0) {
3149:           pblocks[i] = -pblocks[i];
3150:           pblocks[nblocks - 1] += pblocks[i];
3151:         } else {
3152:           pblocks[nblocks++] = pblocks[i]; // nblocks always <= i
3153:         }
3154:         for (PetscInt j = 1; j < pblocks[i]; j++)
3155:           PetscCheck(pblocks[i + j] == pblocks[i], PETSC_COMM_SELF, PETSC_ERR_PLIB, "Block of size %" PetscInt_FMT " at %" PetscInt_FMT " mismatches entry %" PetscInt_FMT " at %" PetscInt_FMT, pblocks[i], i, pblocks[i + j], i + j);
3156:       }
3157:       PetscCall(MatSetVariableBlockSizes(*J, nblocks, pblocks));
3158:     }
3159:     PetscCall(PetscFree(pblocks));
3160:   }
3161:   PetscCall(MatSetDM(*J, dm));
3162:   PetscFunctionReturn(PETSC_SUCCESS);
3163: }

3165: /*@
3166:   DMPlexGetSubdomainSection - Returns the section associated with the subdomain

3168:   Not Collective

3170:   Input Parameter:
3171: . dm - The `DMPLEX`

3173:   Output Parameter:
3174: . subsection - The subdomain section

3176:   Level: developer

3178: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `PetscSection`
3179: @*/
3180: PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
3181: {
3182:   DM_Plex *mesh = (DM_Plex *)dm->data;

3184:   PetscFunctionBegin;
3186:   if (!mesh->subdomainSection) {
3187:     PetscSection section;
3188:     PetscSF      sf;

3190:     PetscCall(PetscSFCreate(PETSC_COMM_SELF, &sf));
3191:     PetscCall(DMGetLocalSection(dm, &section));
3192:     PetscCall(PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, PETSC_TRUE, &mesh->subdomainSection));
3193:     PetscCall(PetscSFDestroy(&sf));
3194:   }
3195:   *subsection = mesh->subdomainSection;
3196:   PetscFunctionReturn(PETSC_SUCCESS);
3197: }

3199: /*@
3200:   DMPlexGetChart - Return the interval for all mesh points [`pStart`, `pEnd`)

3202:   Not Collective

3204:   Input Parameter:
3205: . dm - The `DMPLEX`

3207:   Output Parameters:
3208: + pStart - The first mesh point
3209: - pEnd   - The upper bound for mesh points

3211:   Level: beginner

3213: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSetChart()`
3214: @*/
3215: PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
3216: {
3217:   DM_Plex *mesh = (DM_Plex *)dm->data;

3219:   PetscFunctionBegin;
3221:   if (mesh->tr) PetscCall(DMPlexTransformGetChart(mesh->tr, pStart, pEnd));
3222:   else PetscCall(PetscSectionGetChart(mesh->coneSection, pStart, pEnd));
3223:   PetscFunctionReturn(PETSC_SUCCESS);
3224: }

3226: /*@
3227:   DMPlexSetChart - Set the interval for all mesh points [`pStart`, `pEnd`)

3229:   Not Collective

3231:   Input Parameters:
3232: + dm     - The `DMPLEX`
3233: . pStart - The first mesh point
3234: - pEnd   - The upper bound for mesh points

3236:   Level: beginner

3238: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetChart()`
3239: @*/
3240: PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
3241: {
3242:   DM_Plex *mesh = (DM_Plex *)dm->data;

3244:   PetscFunctionBegin;
3246:   PetscCall(PetscSectionSetChart(mesh->coneSection, pStart, pEnd));
3247:   PetscCall(PetscSectionSetChart(mesh->supportSection, pStart, pEnd));
3248:   PetscCall(PetscFree(mesh->cellTypes));
3249:   PetscFunctionReturn(PETSC_SUCCESS);
3250: }

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

3255:   Not Collective

3257:   Input Parameters:
3258: + dm - The `DMPLEX`
3259: - p  - The point, which must lie in the chart set with `DMPlexSetChart()`

3261:   Output Parameter:
3262: . size - The cone size for point `p`

3264:   Level: beginner

3266: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSetConeSize()`, `DMPlexSetChart()`
3267: @*/
3268: PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
3269: {
3270:   DM_Plex *mesh = (DM_Plex *)dm->data;

3272:   PetscFunctionBegin;
3274:   PetscAssertPointer(size, 3);
3275:   if (mesh->tr) PetscCall(DMPlexTransformGetConeSize(mesh->tr, p, size));
3276:   else PetscCall(PetscSectionGetDof(mesh->coneSection, p, size));
3277:   PetscFunctionReturn(PETSC_SUCCESS);
3278: }

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

3283:   Not Collective

3285:   Input Parameters:
3286: + dm   - The `DMPLEX`
3287: . p    - The point, which must lie in the chart set with `DMPlexSetChart()`
3288: - size - The cone size for point `p`

3290:   Level: beginner

3292:   Note:
3293:   This should be called after `DMPlexSetChart()`.

3295: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexSetCone()`, `DMPlexCreate()`, `DMPlexGetConeSize()`, `DMPlexSetChart()`
3296: @*/
3297: PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
3298: {
3299:   DM_Plex *mesh = (DM_Plex *)dm->data;

3301:   PetscFunctionBegin;
3303:   PetscCheck(!mesh->tr, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Cannot call DMPlexSetConeSize() on a mesh with a transform defined.");
3304:   PetscCall(PetscSectionSetDof(mesh->coneSection, p, size));
3305:   PetscFunctionReturn(PETSC_SUCCESS);
3306: }

3308: /*@
3309:   DMPlexGetCone - Return the points on the in-edges for this point in the DAG

3311:   Not Collective

3313:   Input Parameters:
3314: + dm - The `DMPLEX`
3315: - p  - The point, which must lie in the chart set with `DMPlexSetChart()`

3317:   Output Parameter:
3318: . cone - An array of points which are on the in-edges for point `p`, the length of `cone` is the result of `DMPlexGetConeSize()`

3320:   Level: beginner

3322:   Fortran Notes:
3323:   `cone` must be declared with
3324: .vb
3325:   PetscInt, pointer :: cone(:)
3326: .ve

3328:   You must call `DMPlexRestoreCone()` after you finish using the array.
3329:   `DMPlexRestoreCone()` is not needed/available in C.

3331: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetConeSize()`, `DMPlexSetCone()`, `DMPlexGetConeTuple()`, `DMPlexSetChart()`, `DMPlexRestoreCone()`
3332: @*/
3333: PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
3334: {
3335:   DM_Plex *mesh = (DM_Plex *)dm->data;
3336:   PetscInt off;

3338:   PetscFunctionBegin;
3340:   PetscAssertPointer(cone, 3);
3341:   PetscCall(PetscSectionGetOffset(mesh->coneSection, p, &off));
3342:   *cone = PetscSafePointerPlusOffset(mesh->cones, off);
3343:   PetscFunctionReturn(PETSC_SUCCESS);
3344: }

3346: /*@
3347:   DMPlexGetConeTuple - Return the points on the in-edges of several points in the DAG

3349:   Not Collective

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

3355:   Output Parameters:
3356: + pConesSection - `PetscSection` describing the layout of `pCones`
3357: - pCones        - An `IS` containing the points which are on the in-edges for the point set `p`

3359:   Level: intermediate

3361: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexGetConeRecursive()`, `DMPlexSetChart()`, `PetscSection`, `IS`
3362: @*/
3363: PetscErrorCode DMPlexGetConeTuple(DM dm, IS p, PeOp PetscSection *pConesSection, PeOp IS *pCones)
3364: {
3365:   PetscSection cs, newcs;
3366:   PetscInt    *cones;
3367:   PetscInt    *newarr = NULL;
3368:   PetscInt     n;

3370:   PetscFunctionBegin;
3371:   PetscCall(DMPlexGetCones(dm, &cones));
3372:   PetscCall(DMPlexGetConeSection(dm, &cs));
3373:   PetscCall(PetscSectionExtractDofsFromArray(cs, MPIU_INT, cones, p, &newcs, pCones ? ((void **)&newarr) : NULL));
3374:   if (pConesSection) *pConesSection = newcs;
3375:   if (pCones) {
3376:     PetscCall(PetscSectionGetStorageSize(newcs, &n));
3377:     PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)p), n, newarr, PETSC_OWN_POINTER, pCones));
3378:   }
3379:   PetscFunctionReturn(PETSC_SUCCESS);
3380: }

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

3385:   Not Collective

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

3391:   Output Parameter:
3392: . expandedPoints - An `IS` containing the of vertices recursively expanded from input points

3394:   Level: advanced

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

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

3401: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexGetConeTuple()`, `DMPlexGetConeRecursive()`, `DMPlexRestoreConeRecursive()`,
3402:           `DMPlexGetDepth()`, `IS`
3403: @*/
3404: PetscErrorCode DMPlexGetConeRecursiveVertices(DM dm, IS points, IS *expandedPoints)
3405: {
3406:   IS      *expandedPointsAll;
3407:   PetscInt depth;

3409:   PetscFunctionBegin;
3412:   PetscAssertPointer(expandedPoints, 3);
3413:   PetscCall(DMPlexGetConeRecursive(dm, points, &depth, &expandedPointsAll, NULL));
3414:   *expandedPoints = expandedPointsAll[0];
3415:   PetscCall(PetscObjectReference((PetscObject)expandedPointsAll[0]));
3416:   PetscCall(DMPlexRestoreConeRecursive(dm, points, &depth, &expandedPointsAll, NULL));
3417:   PetscFunctionReturn(PETSC_SUCCESS);
3418: }

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

3424:   Not Collective

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

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

3435:   Level: advanced

3437:   Notes:
3438:   Like `DMPlexGetConeTuple()` but recursive.

3440:   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.
3441:   For example, for d=0 it contains only vertices, for d=1 it can contain vertices and edges, etc.

3443:   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\:
3444:   (1) DAG points in `expandedPoints`[d+1] with `depth` d+1 to their cone points in `expandedPoints`[d];
3445:   (2) DAG points in `expandedPoints`[d+1] with `depth` in [0,d] to the same points in `expandedPoints`[d].

3447: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexGetConeTuple()`, `DMPlexRestoreConeRecursive()`, `DMPlexGetConeRecursiveVertices()`,
3448:           `DMPlexGetDepth()`, `PetscSection`, `IS`
3449: @*/
3450: PetscErrorCode DMPlexGetConeRecursive(DM dm, IS points, PeOp PetscInt *depth, PeOp IS *expandedPoints[], PeOp PetscSection *sections[])
3451: {
3452:   const PetscInt *arr0 = NULL, *cone = NULL;
3453:   PetscInt       *arr = NULL, *newarr = NULL;
3454:   PetscInt        d, depth_, i, n, newn, cn, co, start, end;
3455:   IS             *expandedPoints_;
3456:   PetscSection   *sections_;

3458:   PetscFunctionBegin;
3461:   if (depth) PetscAssertPointer(depth, 3);
3462:   if (expandedPoints) PetscAssertPointer(expandedPoints, 4);
3463:   if (sections) PetscAssertPointer(sections, 5);
3464:   PetscCall(ISGetLocalSize(points, &n));
3465:   PetscCall(ISGetIndices(points, &arr0));
3466:   PetscCall(DMPlexGetDepth(dm, &depth_));
3467:   PetscCall(PetscCalloc1(depth_, &expandedPoints_));
3468:   PetscCall(PetscCalloc1(depth_, &sections_));
3469:   arr = (PetscInt *)arr0; /* this is ok because first generation of arr is not modified */
3470:   for (d = depth_ - 1; d >= 0; d--) {
3471:     PetscCall(PetscSectionCreate(PETSC_COMM_SELF, &sections_[d]));
3472:     PetscCall(PetscSectionSetChart(sections_[d], 0, n));
3473:     for (i = 0; i < n; i++) {
3474:       PetscCall(DMPlexGetDepthStratum(dm, d + 1, &start, &end));
3475:       if (arr[i] >= start && arr[i] < end) {
3476:         PetscCall(DMPlexGetConeSize(dm, arr[i], &cn));
3477:         PetscCall(PetscSectionSetDof(sections_[d], i, cn));
3478:       } else {
3479:         PetscCall(PetscSectionSetDof(sections_[d], i, 1));
3480:       }
3481:     }
3482:     PetscCall(PetscSectionSetUp(sections_[d]));
3483:     PetscCall(PetscSectionGetStorageSize(sections_[d], &newn));
3484:     PetscCall(PetscMalloc1(newn, &newarr));
3485:     for (i = 0; i < n; i++) {
3486:       PetscCall(PetscSectionGetDof(sections_[d], i, &cn));
3487:       PetscCall(PetscSectionGetOffset(sections_[d], i, &co));
3488:       if (cn > 1) {
3489:         PetscCall(DMPlexGetCone(dm, arr[i], &cone));
3490:         PetscCall(PetscArraycpy(&newarr[co], cone, cn));
3491:       } else {
3492:         newarr[co] = arr[i];
3493:       }
3494:     }
3495:     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, newn, newarr, PETSC_OWN_POINTER, &expandedPoints_[d]));
3496:     arr = newarr;
3497:     n   = newn;
3498:   }
3499:   PetscCall(ISRestoreIndices(points, &arr0));
3500:   *depth = depth_;
3501:   if (expandedPoints) *expandedPoints = expandedPoints_;
3502:   else {
3503:     for (d = 0; d < depth_; d++) PetscCall(ISDestroy(&expandedPoints_[d]));
3504:     PetscCall(PetscFree(expandedPoints_));
3505:   }
3506:   if (sections) *sections = sections_;
3507:   else {
3508:     for (d = 0; d < depth_; d++) PetscCall(PetscSectionDestroy(&sections_[d]));
3509:     PetscCall(PetscFree(sections_));
3510:   }
3511:   PetscFunctionReturn(PETSC_SUCCESS);
3512: }

3514: /*@
3515:   DMPlexRestoreConeRecursive - Deallocates arrays created by `DMPlexGetConeRecursive()`

3517:   Not Collective

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

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

3528:   Level: advanced

3530:   Note:
3531:   See `DMPlexGetConeRecursive()`

3533: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexGetConeTuple()`, `DMPlexGetConeRecursive()`, `DMPlexGetConeRecursiveVertices()`,
3534:           `DMPlexGetDepth()`, `IS`, `PetscSection`
3535: @*/
3536: PetscErrorCode DMPlexRestoreConeRecursive(DM dm, IS points, PeOp PetscInt *depth, PeOp IS *expandedPoints[], PeOp PetscSection *sections[])
3537: {
3538:   PetscInt depth_;

3540:   PetscFunctionBegin;
3541:   PetscCall(DMPlexGetDepth(dm, &depth_));
3542:   PetscCheck(!depth || *depth == depth_, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "depth changed since last call to DMPlexGetConeRecursive");
3543:   if (depth) *depth = 0;
3544:   if (expandedPoints) {
3545:     for (PetscInt d = 0; d < depth_; d++) PetscCall(ISDestroy(&(*expandedPoints)[d]));
3546:     PetscCall(PetscFree(*expandedPoints));
3547:   }
3548:   if (sections) {
3549:     for (PetscInt d = 0; d < depth_; d++) PetscCall(PetscSectionDestroy(&(*sections)[d]));
3550:     PetscCall(PetscFree(*sections));
3551:   }
3552:   PetscFunctionReturn(PETSC_SUCCESS);
3553: }

3555: /*@
3556:   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

3558:   Not Collective

3560:   Input Parameters:
3561: + dm   - The `DMPLEX`
3562: . p    - The point, which must lie in the chart set with `DMPlexSetChart()`
3563: - cone - An array of points which are on the in-edges for point `p`, its length must have been previously provided with `DMPlexSetConeSize()`

3565:   Level: beginner

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

3570: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexSetChart()`, `DMPlexSetConeSize()`, `DMSetUp()`, `DMPlexSetSupport()`, `DMPlexSetSupportSize()`
3571: @*/
3572: PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
3573: {
3574:   DM_Plex *mesh = (DM_Plex *)dm->data;
3575:   PetscInt dof, off, c;

3577:   PetscFunctionBegin;
3579:   PetscCall(PetscSectionGetDof(mesh->coneSection, p, &dof));
3580:   if (dof) PetscAssertPointer(cone, 3);
3581:   PetscCall(PetscSectionGetOffset(mesh->coneSection, p, &off));
3582:   if (PetscDefined(USE_DEBUG)) {
3583:     PetscInt pStart, pEnd;
3584:     PetscCall(PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd));
3585:     PetscCheck(!(p < pStart) && !(p >= pEnd), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %" PetscInt_FMT " is not in the valid range [%" PetscInt_FMT ", %" PetscInt_FMT ")", p, pStart, pEnd);
3586:     for (c = 0; c < dof; ++c) {
3587:       PetscCheck(!(cone[c] < pStart) && !(cone[c] >= pEnd), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone point %" PetscInt_FMT " is not in the valid range [%" PetscInt_FMT ", %" PetscInt_FMT ")", cone[c], pStart, pEnd);
3588:       mesh->cones[off + c] = cone[c];
3589:     }
3590:   } else {
3591:     for (c = 0; c < dof; ++c) mesh->cones[off + c] = cone[c];
3592:   }
3593:   PetscFunctionReturn(PETSC_SUCCESS);
3594: }

3596: /*@
3597:   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the DAG

3599:   Not Collective

3601:   Input Parameters:
3602: + dm - The `DMPLEX`
3603: - p  - The point, which must lie in the chart set with `DMPlexSetChart()`

3605:   Output Parameter:
3606: . coneOrientation - An array of orientations which are on the in-edges for point `p`. An orientation is an
3607:                     integer giving the prescription for cone traversal. Its length is given by the result of `DMPlexSetConeSize()`

3609:   Level: beginner

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

3617:   Fortran Notes:
3618:   You must call `DMPlexRestoreConeOrientation()` after you finish using the returned array.
3619:   `DMPlexRestoreConeOrientation()` is not needed/available in C.

3621: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexSetConeSize()`, `DMPolytopeTypeComposeOrientation()`, `DMPolytopeTypeComposeOrientationInv()`,
3622:           `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexSetCone()`, `DMPlexSetChart()`
3623: @*/
3624: PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
3625: {
3626:   DM_Plex *mesh = (DM_Plex *)dm->data;
3627:   PetscInt off;

3629:   PetscFunctionBegin;
3631:   if (PetscDefined(USE_DEBUG)) {
3632:     PetscInt dof;
3633:     PetscCall(PetscSectionGetDof(mesh->coneSection, p, &dof));
3634:     if (dof) PetscAssertPointer(coneOrientation, 3);
3635:   }
3636:   PetscCall(PetscSectionGetOffset(mesh->coneSection, p, &off));

3638:   *coneOrientation = &mesh->coneOrientations[off];
3639:   PetscFunctionReturn(PETSC_SUCCESS);
3640: }

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

3645:   Not Collective

3647:   Input Parameters:
3648: + dm              - The `DMPLEX`
3649: . p               - The point, which must lie in the chart set with `DMPlexSetChart()`
3650: - coneOrientation - An array of orientations. Its length is given by the result of `DMPlexSetConeSize()`

3652:   Level: beginner

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

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

3659: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetConeOrientation()`, `DMPlexSetCone()`, `DMPlexSetChart()`, `DMPlexSetConeSize()`, `DMSetUp()`
3660: @*/
3661: PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
3662: {
3663:   DM_Plex *mesh = (DM_Plex *)dm->data;
3664:   PetscInt pStart, pEnd;
3665:   PetscInt dof, off, c;

3667:   PetscFunctionBegin;
3669:   PetscCall(PetscSectionGetDof(mesh->coneSection, p, &dof));
3670:   if (dof) PetscAssertPointer(coneOrientation, 3);
3671:   PetscCall(PetscSectionGetOffset(mesh->coneSection, p, &off));
3672:   if (PetscDefined(USE_DEBUG)) {
3673:     PetscCall(PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd));
3674:     PetscCheck(!(p < pStart) && !(p >= pEnd), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %" PetscInt_FMT " is not in the valid range [%" PetscInt_FMT ", %" PetscInt_FMT ")", p, pStart, pEnd);
3675:     for (c = 0; c < dof; ++c) {
3676:       PetscInt cdof, o = coneOrientation[c];

3678:       PetscCall(PetscSectionGetDof(mesh->coneSection, mesh->cones[off + c], &cdof));
3679:       PetscCheck(!o || (o >= -(cdof + 1) && o < cdof), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone orientation %" PetscInt_FMT " is not in the valid range [%" PetscInt_FMT ". %" PetscInt_FMT ")", o, -(cdof + 1), cdof);
3680:       mesh->coneOrientations[off + c] = o;
3681:     }
3682:   } else {
3683:     for (c = 0; c < dof; ++c) mesh->coneOrientations[off + c] = coneOrientation[c];
3684:   }
3685:   PetscFunctionReturn(PETSC_SUCCESS);
3686: }

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

3691:   Not Collective

3693:   Input Parameters:
3694: + dm        - The `DMPLEX`
3695: . p         - The point, which must lie in the chart set with `DMPlexSetChart()`
3696: . conePos   - The local index in the cone where the point should be put
3697: - conePoint - The mesh point to insert

3699:   Level: beginner

3701: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexSetChart()`, `DMPlexSetConeSize()`, `DMSetUp()`
3702: @*/
3703: PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
3704: {
3705:   DM_Plex *mesh = (DM_Plex *)dm->data;
3706:   PetscInt pStart, pEnd;
3707:   PetscInt dof, off;

3709:   PetscFunctionBegin;
3711:   if (PetscDefined(USE_DEBUG)) {
3712:     PetscCall(PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd));
3713:     PetscCheck(!(p < pStart) && !(p >= pEnd), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %" PetscInt_FMT " is not in the valid range [%" PetscInt_FMT ", %" PetscInt_FMT ")", p, pStart, pEnd);
3714:     PetscCheck(!(conePoint < pStart) && !(conePoint >= pEnd), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone point %" PetscInt_FMT " is not in the valid range [%" PetscInt_FMT ", %" PetscInt_FMT ")", conePoint, pStart, pEnd);
3715:     PetscCall(PetscSectionGetDof(mesh->coneSection, p, &dof));
3716:     PetscCheck(!(conePos < 0) && !(conePos >= dof), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone position %" PetscInt_FMT " of point %" PetscInt_FMT " is not in the valid range [0, %" PetscInt_FMT ")", conePos, p, dof);
3717:   }
3718:   PetscCall(PetscSectionGetOffset(mesh->coneSection, p, &off));
3719:   mesh->cones[off + conePos] = conePoint;
3720:   PetscFunctionReturn(PETSC_SUCCESS);
3721: }

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

3726:   Not Collective

3728:   Input Parameters:
3729: + dm              - The `DMPLEX`
3730: . p               - The point, which must lie in the chart set with `DMPlexSetChart()`
3731: . conePos         - The local index in the cone where the point should be put
3732: - coneOrientation - The point orientation to insert

3734:   Level: beginner

3736:   Note:
3737:   The meaning of coneOrientation values is detailed in `DMPlexGetConeOrientation()`.

3739: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexSetChart()`, `DMPlexSetConeSize()`, `DMSetUp()`
3740: @*/
3741: PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
3742: {
3743:   DM_Plex *mesh = (DM_Plex *)dm->data;
3744:   PetscInt pStart, pEnd;
3745:   PetscInt dof, off;

3747:   PetscFunctionBegin;
3749:   if (PetscDefined(USE_DEBUG)) {
3750:     PetscCall(PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd));
3751:     PetscCheck(!(p < pStart) && !(p >= pEnd), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %" PetscInt_FMT " is not in the valid range [%" PetscInt_FMT ", %" PetscInt_FMT ")", p, pStart, pEnd);
3752:     PetscCall(PetscSectionGetDof(mesh->coneSection, p, &dof));
3753:     PetscCheck(!(conePos < 0) && !(conePos >= dof), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone position %" PetscInt_FMT " of point %" PetscInt_FMT " is not in the valid range [0, %" PetscInt_FMT ")", conePos, p, dof);
3754:   }
3755:   PetscCall(PetscSectionGetOffset(mesh->coneSection, p, &off));
3756:   mesh->coneOrientations[off + conePos] = coneOrientation;
3757:   PetscFunctionReturn(PETSC_SUCCESS);
3758: }

3760: /*@
3761:   DMPlexGetOrientedCone - Return the points and orientations on the in-edges for this point in the DAG

3763:   Not collective

3765:   Input Parameters:
3766: + dm - The DMPlex
3767: - p  - The point, which must lie in the chart set with DMPlexSetChart()

3769:   Output Parameters:
3770: + cone - An array of points which are on the in-edges for point `p`
3771: - ornt - An array of orientations which are on the in-edges for point `p`. An orientation is an
3772:          integer giving the prescription for cone traversal.

3774:   Level: beginner

3776:   Notes:
3777:   The number indexes the symmetry transformations for the cell type (see manual). Orientation 0 is always
3778:   the identity transformation. Negative orientation indicates reflection so that -(o+1) is the reflection
3779:   of o, however it is not necessarily the inverse. To get the inverse, use `DMPolytopeTypeComposeOrientationInv()`
3780:   with the identity.

3782:   You must also call `DMPlexRestoreOrientedCone()` after you finish using the returned array.

3784:   Fortran Notes:
3785:   `cone` and `ornt` must be declared with
3786: .vb
3787:   PetscInt, pointer :: cone(:)
3788:   PetscInt, pointer :: ornt(:)
3789: .ve

3791: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexRestoreOrientedCone()`, `DMPlexGetConeSize()`, `DMPlexGetCone()`, `DMPlexGetChart()`
3792: @*/
3793: PetscErrorCode DMPlexGetOrientedCone(DM dm, PetscInt p, PeOp const PetscInt *cone[], PeOp const PetscInt *ornt[])
3794: {
3795:   DM_Plex *mesh = (DM_Plex *)dm->data;

3797:   PetscFunctionBegin;
3799:   if (mesh->tr) {
3800:     PetscCall(DMPlexTransformGetCone(mesh->tr, p, cone, ornt));
3801:   } else {
3802:     PetscInt off;
3803:     if (PetscDefined(USE_DEBUG)) {
3804:       PetscInt dof;
3805:       PetscCall(PetscSectionGetDof(mesh->coneSection, p, &dof));
3806:       if (dof) {
3807:         if (cone) PetscAssertPointer(cone, 3);
3808:         if (ornt) PetscAssertPointer(ornt, 4);
3809:       }
3810:     }
3811:     PetscCall(PetscSectionGetOffset(mesh->coneSection, p, &off));
3812:     if (cone) *cone = PetscSafePointerPlusOffset(mesh->cones, off);
3813:     if (ornt) *ornt = PetscSafePointerPlusOffset(mesh->coneOrientations, off);
3814:   }
3815:   PetscFunctionReturn(PETSC_SUCCESS);
3816: }

3818: /*@
3819:   DMPlexRestoreOrientedCone - Restore the points and orientations on the in-edges for this point in the DAG obtained with `DMPlexGetOrientedCone()`

3821:   Not Collective

3823:   Input Parameters:
3824: + dm   - The DMPlex
3825: . p    - The point, which must lie in the chart set with `DMPlexSetChart()`
3826: . cone - An array of points which are on the in-edges for point p
3827: - ornt - An array of orientations which are on the in-edges for point `p`. An orientation is an
3828:          integer giving the prescription for cone traversal.

3830:   Level: beginner

3832: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetOrientedCone()`, `DMPlexGetConeSize()`, `DMPlexGetCone()`, `DMPlexGetChart()`
3833: @*/
3834: PetscErrorCode DMPlexRestoreOrientedCone(DM dm, PetscInt p, const PetscInt *cone[], const PetscInt *ornt[])
3835: {
3836:   DM_Plex *mesh = (DM_Plex *)dm->data;

3838:   PetscFunctionBegin;
3840:   if (mesh->tr) PetscCall(DMPlexTransformRestoreCone(mesh->tr, p, cone, ornt));
3841:   PetscFunctionReturn(PETSC_SUCCESS);
3842: }

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

3847:   Not Collective

3849:   Input Parameters:
3850: + dm - The `DMPLEX`
3851: - p  - The point, which must lie in the chart set with `DMPlexSetChart()`

3853:   Output Parameter:
3854: . size - The support size for point `p`

3856:   Level: beginner

3858: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSetConeSize()`, `DMPlexSetChart()`, `DMPlexGetConeSize()`
3859: @*/
3860: PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
3861: {
3862:   DM_Plex *mesh = (DM_Plex *)dm->data;

3864:   PetscFunctionBegin;
3866:   PetscAssertPointer(size, 3);
3867:   PetscCall(PetscSectionGetDof(mesh->supportSection, p, size));
3868:   PetscFunctionReturn(PETSC_SUCCESS);
3869: }

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

3874:   Not Collective

3876:   Input Parameters:
3877: + dm   - The `DMPLEX`
3878: . p    - The point, which must lie in the chart set with `DMPlexSetChart()`
3879: - size - The support size for point `p`

3881:   Level: beginner

3883:   Note:
3884:   This should be called after `DMPlexSetChart()`.

3886: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetSupportSize()`, `DMPlexSetChart()`
3887: @*/
3888: PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
3889: {
3890:   DM_Plex *mesh = (DM_Plex *)dm->data;

3892:   PetscFunctionBegin;
3894:   PetscCall(PetscSectionSetDof(mesh->supportSection, p, size));
3895:   PetscFunctionReturn(PETSC_SUCCESS);
3896: }

3898: /*@
3899:   DMPlexGetSupport - Return the points on the out-edges for this point in the DAG

3901:   Not Collective

3903:   Input Parameters:
3904: + dm - The `DMPLEX`
3905: - p  - The point, which must lie in the chart set with `DMPlexSetChart()`

3907:   Output Parameter:
3908: . support - An array of points which are on the out-edges for point `p`, its length is that obtained from `DMPlexGetSupportSize()`

3910:   Level: beginner

3912:   Fortran Notes:
3913:   `support` must be declared with
3914: .vb
3915:   PetscInt, pointer :: support(:)
3916: .ve

3918:   You must also call `DMPlexRestoreSupport()` after you finish using the returned array.
3919:   `DMPlexRestoreSupport()` is not needed/available in C.

3921: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSupportSize()`, `DMPlexSetSupport()`, `DMPlexGetCone()`, `DMPlexSetChart()`
3922: @*/
3923: PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
3924: {
3925:   DM_Plex *mesh = (DM_Plex *)dm->data;
3926:   PetscInt off;

3928:   PetscFunctionBegin;
3930:   PetscAssertPointer(support, 3);
3931:   PetscCall(PetscSectionGetOffset(mesh->supportSection, p, &off));
3932:   *support = PetscSafePointerPlusOffset(mesh->supports, off);
3933:   PetscFunctionReturn(PETSC_SUCCESS);
3934: }

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

3939:   Not Collective

3941:   Input Parameters:
3942: + dm      - The `DMPLEX`
3943: . p       - The point, which must lie in the chart set with `DMPlexSetChart()`
3944: - support - An array of points which are on the out-edges for point `p`, its length is that obtained from `DMPlexGetSupportSize()`

3946:   Level: beginner

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

3951: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexSetCone()`, `DMPlexSetConeSize()`, `DMPlexCreate()`, `DMPlexGetSupport()`, `DMPlexSetChart()`, `DMPlexSetSupportSize()`, `DMSetUp()`
3952: @*/
3953: PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
3954: {
3955:   DM_Plex *mesh = (DM_Plex *)dm->data;
3956:   PetscInt pStart, pEnd;
3957:   PetscInt dof, off, c;

3959:   PetscFunctionBegin;
3961:   PetscCall(PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd));
3962:   PetscCall(PetscSectionGetDof(mesh->supportSection, p, &dof));
3963:   if (dof) PetscAssertPointer(support, 3);
3964:   PetscCall(PetscSectionGetOffset(mesh->supportSection, p, &off));
3965:   PetscCheck(!(p < pStart) && !(p >= pEnd), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %" PetscInt_FMT " is not in the valid range [%" PetscInt_FMT ", %" PetscInt_FMT ")", p, pStart, pEnd);
3966:   for (c = 0; c < dof; ++c) {
3967:     PetscCheck(!(support[c] < pStart) && !(support[c] >= pEnd), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Support point %" PetscInt_FMT " is not in the valid range [%" PetscInt_FMT ", %" PetscInt_FMT ")", support[c], pStart, pEnd);
3968:     mesh->supports[off + c] = support[c];
3969:   }
3970:   PetscFunctionReturn(PETSC_SUCCESS);
3971: }

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

3976:   Not Collective

3978:   Input Parameters:
3979: + dm           - The `DMPLEX`
3980: . p            - The point, which must lie in the chart set with `DMPlexSetChart()`
3981: . supportPos   - The local index in the cone where the point should be put
3982: - supportPoint - The mesh point to insert

3984:   Level: beginner

3986: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexGetCone()`, `DMPlexSetChart()`, `DMPlexSetConeSize()`, `DMSetUp()`
3987: @*/
3988: PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
3989: {
3990:   DM_Plex *mesh = (DM_Plex *)dm->data;
3991:   PetscInt pStart, pEnd;
3992:   PetscInt dof, off;

3994:   PetscFunctionBegin;
3996:   PetscCall(PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd));
3997:   PetscCall(PetscSectionGetDof(mesh->supportSection, p, &dof));
3998:   PetscCall(PetscSectionGetOffset(mesh->supportSection, p, &off));
3999:   PetscCheck(!(p < pStart) && !(p >= pEnd), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %" PetscInt_FMT " is not in the valid range [%" PetscInt_FMT ", %" PetscInt_FMT ")", p, pStart, pEnd);
4000:   PetscCheck(!(supportPoint < pStart) && !(supportPoint >= pEnd), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Support point %" PetscInt_FMT " is not in the valid range [%" PetscInt_FMT ", %" PetscInt_FMT ")", supportPoint, pStart, pEnd);
4001:   PetscCheck(supportPos < dof, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Support position %" PetscInt_FMT " of point %" PetscInt_FMT " is not in the valid range [0, %" PetscInt_FMT ")", supportPos, p, dof);
4002:   mesh->supports[off + supportPos] = supportPoint;
4003:   PetscFunctionReturn(PETSC_SUCCESS);
4004: }

4006: /* Converts an orientation o in the current numbering to the previous scheme used in Plex */
4007: PetscInt DMPolytopeConvertNewOrientation_Internal(DMPolytopeType ct, PetscInt o)
4008: {
4009:   switch (ct) {
4010:   case DM_POLYTOPE_SEGMENT:
4011:     if (o == -1) return -2;
4012:     break;
4013:   case DM_POLYTOPE_TRIANGLE:
4014:     if (o == -3) return -1;
4015:     if (o == -2) return -3;
4016:     if (o == -1) return -2;
4017:     break;
4018:   case DM_POLYTOPE_QUADRILATERAL:
4019:     if (o == -4) return -2;
4020:     if (o == -3) return -1;
4021:     if (o == -2) return -4;
4022:     if (o == -1) return -3;
4023:     break;
4024:   default:
4025:     return o;
4026:   }
4027:   return o;
4028: }

4030: /* Converts an orientation o in the previous scheme used in Plex to the current numbering */
4031: PetscInt DMPolytopeConvertOldOrientation_Internal(DMPolytopeType ct, PetscInt o)
4032: {
4033:   switch (ct) {
4034:   case DM_POLYTOPE_SEGMENT:
4035:     if (o == -2 || o == 1) return -1;
4036:     if (o == -1) return 0;
4037:     break;
4038:   case DM_POLYTOPE_TRIANGLE:
4039:     if (o == -3) return -2;
4040:     if (o == -2) return -1;
4041:     if (o == -1) return -3;
4042:     break;
4043:   case DM_POLYTOPE_QUADRILATERAL:
4044:     if (o == -4) return -2;
4045:     if (o == -3) return -1;
4046:     if (o == -2) return -4;
4047:     if (o == -1) return -3;
4048:     break;
4049:   default:
4050:     return o;
4051:   }
4052:   return o;
4053: }

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

4060:   PetscFunctionBegin;
4061:   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
4062:   for (p = pStart; p < pEnd; ++p) {
4063:     const PetscInt *cone, *ornt;
4064:     PetscInt        coneSize, c;

4066:     PetscCall(DMPlexGetConeSize(dm, p, &coneSize));
4067:     PetscCall(DMPlexGetCone(dm, p, &cone));
4068:     PetscCall(DMPlexGetConeOrientation(dm, p, &ornt));
4069:     for (c = 0; c < coneSize; ++c) {
4070:       DMPolytopeType ct;
4071:       const PetscInt o = ornt[c];

4073:       PetscCall(DMPlexGetCellType(dm, cone[c], &ct));
4074:       switch (ct) {
4075:       case DM_POLYTOPE_SEGMENT:
4076:         if (o == -2 || o == 1) PetscCall(DMPlexInsertConeOrientation(dm, p, c, -1));
4077:         if (o == -1) PetscCall(DMPlexInsertConeOrientation(dm, p, c, 0));
4078:         break;
4079:       case DM_POLYTOPE_TRIANGLE:
4080:         if (o == -3) PetscCall(DMPlexInsertConeOrientation(dm, p, c, -2));
4081:         if (o == -2) PetscCall(DMPlexInsertConeOrientation(dm, p, c, -1));
4082:         if (o == -1) PetscCall(DMPlexInsertConeOrientation(dm, p, c, -3));
4083:         break;
4084:       case DM_POLYTOPE_QUADRILATERAL:
4085:         if (o == -4) PetscCall(DMPlexInsertConeOrientation(dm, p, c, -2));
4086:         if (o == -3) PetscCall(DMPlexInsertConeOrientation(dm, p, c, -1));
4087:         if (o == -2) PetscCall(DMPlexInsertConeOrientation(dm, p, c, -4));
4088:         if (o == -1) PetscCall(DMPlexInsertConeOrientation(dm, p, c, -3));
4089:         break;
4090:       default:
4091:         break;
4092:       }
4093:     }
4094:   }
4095:   PetscFunctionReturn(PETSC_SUCCESS);
4096: }

4098: static inline PetscErrorCode DMPlexGetTransitiveClosure_Hot_Private(DM dm, PetscInt p, PetscBool useCone, PetscInt *size, const PetscInt *arr[], const PetscInt *ornt[])
4099: {
4100:   PetscFunctionBeginHot;
4101:   if (PetscDefined(USE_DEBUG) || ((DM_Plex *)dm->data)->tr) {
4102:     if (useCone) {
4103:       PetscCall(DMPlexGetConeSize(dm, p, size));
4104:       PetscCall(DMPlexGetOrientedCone(dm, p, arr, ornt));
4105:     } else {
4106:       PetscCall(DMPlexGetSupportSize(dm, p, size));
4107:       PetscCall(DMPlexGetSupport(dm, p, arr));
4108:     }
4109:   } else {
4110:     DM_Plex *mesh = (DM_Plex *)dm->data;

4112:     if (useCone) {
4113:       const PetscSection s   = mesh->coneSection;
4114:       const PetscInt     ps  = p - s->pStart;
4115:       const PetscInt     off = s->atlasOff[ps];

4117:       *size = s->atlasDof[ps];
4118:       *arr  = mesh->cones + off;
4119:       *ornt = mesh->coneOrientations + off;
4120:     } else {
4121:       const PetscSection s   = mesh->supportSection;
4122:       const PetscInt     ps  = p - s->pStart;
4123:       const PetscInt     off = s->atlasOff[ps];

4125:       *size = s->atlasDof[ps];
4126:       *arr  = mesh->supports + off;
4127:     }
4128:   }
4129:   PetscFunctionReturn(PETSC_SUCCESS);
4130: }

4132: static inline PetscErrorCode DMPlexRestoreTransitiveClosure_Hot_Private(DM dm, PetscInt p, PetscBool useCone, PetscInt *size, const PetscInt *arr[], const PetscInt *ornt[])
4133: {
4134:   PetscFunctionBeginHot;
4135:   if (PetscDefined(USE_DEBUG) || ((DM_Plex *)dm->data)->tr) {
4136:     if (useCone) PetscCall(DMPlexRestoreOrientedCone(dm, p, arr, ornt));
4137:   }
4138:   PetscFunctionReturn(PETSC_SUCCESS);
4139: }

4141: static PetscErrorCode DMPlexGetTransitiveClosure_Depth1_Private(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
4142: {
4143:   DMPolytopeType  ct = DM_POLYTOPE_UNKNOWN;
4144:   PetscInt       *closure;
4145:   const PetscInt *tmp = NULL, *tmpO = NULL;
4146:   PetscInt        off = 0, tmpSize, t;

4148:   PetscFunctionBeginHot;
4149:   if (ornt) {
4150:     PetscCall(DMPlexGetCellType(dm, p, &ct));
4151:     if (ct == DM_POLYTOPE_FV_GHOST || ct == DM_POLYTOPE_INTERIOR_GHOST || ct == DM_POLYTOPE_UNKNOWN || ct == DM_POLYTOPE_UNKNOWN_CELL || ct == DM_POLYTOPE_UNKNOWN_FACE) ct = DM_POLYTOPE_UNKNOWN;
4152:   }
4153:   if (*points) {
4154:     closure = *points;
4155:   } else {
4156:     PetscInt maxConeSize, maxSupportSize;
4157:     PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
4158:     PetscCall(DMGetWorkArray(dm, 2 * (PetscMax(maxConeSize, maxSupportSize) + 1), MPIU_INT, &closure));
4159:   }
4160:   PetscCall(DMPlexGetTransitiveClosure_Hot_Private(dm, p, useCone, &tmpSize, &tmp, &tmpO));
4161:   if (ct == DM_POLYTOPE_UNKNOWN) {
4162:     closure[off++] = p;
4163:     closure[off++] = 0;
4164:     for (t = 0; t < tmpSize; ++t) {
4165:       closure[off++] = tmp[t];
4166:       closure[off++] = tmpO ? tmpO[t] : 0;
4167:     }
4168:   } else {
4169:     const PetscInt *arr = DMPolytopeTypeGetArrangement(ct, ornt);

4171:     /* We assume that cells with a valid type have faces with a valid type */
4172:     closure[off++] = p;
4173:     closure[off++] = ornt;
4174:     for (t = 0; t < tmpSize; ++t) {
4175:       DMPolytopeType ft;

4177:       PetscCall(DMPlexGetCellType(dm, tmp[t], &ft));
4178:       closure[off++] = tmp[arr[t]];
4179:       closure[off++] = tmpO ? DMPolytopeTypeComposeOrientation(ft, ornt, tmpO[t]) : 0;
4180:     }
4181:   }
4182:   PetscCall(DMPlexRestoreTransitiveClosure_Hot_Private(dm, p, useCone, &tmpSize, &tmp, &tmpO));
4183:   if (numPoints) *numPoints = tmpSize + 1;
4184:   if (points) *points = closure;
4185:   PetscFunctionReturn(PETSC_SUCCESS);
4186: }

4188: /* We need a special tensor version because we want to allow duplicate points in the endcaps for hybrid cells */
4189: static PetscErrorCode DMPlexTransitiveClosure_Tensor_Internal(DM dm, PetscInt point, DMPolytopeType ct, PetscInt o, PetscBool useCone, PetscInt *numPoints, PetscInt **points)
4190: {
4191:   const PetscInt *arr = DMPolytopeTypeGetArrangement(ct, o);
4192:   const PetscInt *cone, *ornt;
4193:   PetscInt       *pts, *closure = NULL;
4194:   DMPolytopeType  ft;
4195:   PetscInt        maxConeSize, maxSupportSize, coneSeries, supportSeries, maxSize;
4196:   PetscInt        dim, coneSize, c, d, clSize, cl;

4198:   PetscFunctionBeginHot;
4199:   PetscCall(DMGetDimension(dm, &dim));
4200:   PetscCall(DMPlexGetTransitiveClosure_Hot_Private(dm, point, PETSC_TRUE, &coneSize, &cone, &ornt));
4201:   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
4202:   coneSeries    = (maxConeSize > 1) ? ((PetscPowInt(maxConeSize, dim + 1) - 1) / (maxConeSize - 1)) : dim + 1;
4203:   supportSeries = (maxSupportSize > 1) ? ((PetscPowInt(maxSupportSize, dim + 1) - 1) / (maxSupportSize - 1)) : dim + 1;
4204:   maxSize       = PetscMax(coneSeries, supportSeries);
4205:   if (*points) {
4206:     pts = *points;
4207:   } else PetscCall(DMGetWorkArray(dm, 2 * maxSize, MPIU_INT, &pts));
4208:   c        = 0;
4209:   pts[c++] = point;
4210:   pts[c++] = o;
4211:   PetscCall(DMPlexGetCellType(dm, cone[arr[0 * 2 + 0]], &ft));
4212:   PetscCall(DMPlexGetTransitiveClosure_Internal(dm, cone[arr[0 * 2 + 0]], DMPolytopeTypeComposeOrientation(ft, arr[0 * 2 + 1], ornt[0]), useCone, &clSize, &closure));
4213:   for (cl = 0; cl < clSize * 2; cl += 2) {
4214:     pts[c++] = closure[cl];
4215:     pts[c++] = closure[cl + 1];
4216:   }
4217:   PetscCall(DMPlexGetTransitiveClosure_Internal(dm, cone[arr[1 * 2 + 0]], DMPolytopeTypeComposeOrientation(ft, arr[1 * 2 + 1], ornt[1]), useCone, &clSize, &closure));
4218:   for (cl = 0; cl < clSize * 2; cl += 2) {
4219:     pts[c++] = closure[cl];
4220:     pts[c++] = closure[cl + 1];
4221:   }
4222:   PetscCall(DMPlexRestoreTransitiveClosure(dm, cone[0], useCone, &clSize, &closure));
4223:   for (d = 2; d < coneSize; ++d) {
4224:     PetscCall(DMPlexGetCellType(dm, cone[arr[d * 2 + 0]], &ft));
4225:     pts[c++] = cone[arr[d * 2 + 0]];
4226:     pts[c++] = DMPolytopeTypeComposeOrientation(ft, arr[d * 2 + 1], ornt[d]);
4227:   }
4228:   PetscCall(DMPlexRestoreTransitiveClosure_Hot_Private(dm, point, PETSC_TRUE, &coneSize, &cone, &ornt));
4229:   if (dim >= 3) {
4230:     for (d = 2; d < coneSize; ++d) {
4231:       const PetscInt  fpoint = cone[arr[d * 2 + 0]];
4232:       const PetscInt *fcone, *fornt;
4233:       PetscInt        fconeSize, fc, i;

4235:       PetscCall(DMPlexGetCellType(dm, fpoint, &ft));
4236:       const PetscInt *farr = DMPolytopeTypeGetArrangement(ft, DMPolytopeTypeComposeOrientation(ft, arr[d * 2 + 1], ornt[d]));
4237:       PetscCall(DMPlexGetTransitiveClosure_Hot_Private(dm, fpoint, PETSC_TRUE, &fconeSize, &fcone, &fornt));
4238:       for (fc = 0; fc < fconeSize; ++fc) {
4239:         const PetscInt cp = fcone[farr[fc * 2 + 0]];
4240:         const PetscInt co = farr[fc * 2 + 1];

4242:         for (i = 0; i < c; i += 2)
4243:           if (pts[i] == cp) break;
4244:         if (i == c) {
4245:           PetscCall(DMPlexGetCellType(dm, cp, &ft));
4246:           pts[c++] = cp;
4247:           pts[c++] = DMPolytopeTypeComposeOrientation(ft, co, fornt[farr[fc * 2 + 0]]);
4248:         }
4249:       }
4250:       PetscCall(DMPlexRestoreTransitiveClosure_Hot_Private(dm, fpoint, PETSC_TRUE, &fconeSize, &fcone, &fornt));
4251:     }
4252:   }
4253:   *numPoints = c / 2;
4254:   *points    = pts;
4255:   PetscFunctionReturn(PETSC_SUCCESS);
4256: }

4258: PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
4259: {
4260:   DMPolytopeType ct;
4261:   PetscInt      *closure, *fifo;
4262:   PetscInt       closureSize = 0, fifoStart = 0, fifoSize = 0;
4263:   PetscInt       maxConeSize, maxSupportSize, coneSeries, supportSeries;
4264:   PetscInt       depth, maxSize;

4266:   PetscFunctionBeginHot;
4267:   PetscCall(DMPlexGetDepth(dm, &depth));
4268:   if (depth == 1) {
4269:     PetscCall(DMPlexGetTransitiveClosure_Depth1_Private(dm, p, ornt, useCone, numPoints, points));
4270:     PetscFunctionReturn(PETSC_SUCCESS);
4271:   }
4272:   PetscCall(DMPlexGetCellType(dm, p, &ct));
4273:   if (ct == DM_POLYTOPE_FV_GHOST || ct == DM_POLYTOPE_INTERIOR_GHOST || ct == DM_POLYTOPE_UNKNOWN || ct == DM_POLYTOPE_UNKNOWN_CELL || ct == DM_POLYTOPE_UNKNOWN_FACE) ct = DM_POLYTOPE_UNKNOWN;
4274:   if (DMPolytopeTypeIsHybrid(ct) && ct != DM_POLYTOPE_POINT_PRISM_TENSOR) {
4275:     PetscCall(DMPlexTransitiveClosure_Tensor_Internal(dm, p, ct, ornt, useCone, numPoints, points));
4276:     PetscFunctionReturn(PETSC_SUCCESS);
4277:   }
4278:   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
4279:   coneSeries    = (maxConeSize > 1) ? ((PetscPowInt(maxConeSize, depth + 1) - 1) / (maxConeSize - 1)) : depth + 1;
4280:   supportSeries = (maxSupportSize > 1) ? ((PetscPowInt(maxSupportSize, depth + 1) - 1) / (maxSupportSize - 1)) : depth + 1;
4281:   maxSize       = PetscMax(coneSeries, supportSeries);
4282:   PetscCall(DMGetWorkArray(dm, 3 * maxSize, MPIU_INT, &fifo));
4283:   if (*points) {
4284:     closure = *points;
4285:   } else PetscCall(DMGetWorkArray(dm, 2 * maxSize, MPIU_INT, &closure));
4286:   closure[closureSize++] = p;
4287:   closure[closureSize++] = ornt;
4288:   fifo[fifoSize++]       = p;
4289:   fifo[fifoSize++]       = ornt;
4290:   fifo[fifoSize++]       = ct;
4291:   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
4292:   while (fifoSize - fifoStart) {
4293:     const PetscInt       q    = fifo[fifoStart++];
4294:     const PetscInt       o    = fifo[fifoStart++];
4295:     const DMPolytopeType qt   = (DMPolytopeType)fifo[fifoStart++];
4296:     const PetscInt      *qarr = DMPolytopeTypeGetArrangement(qt, o);
4297:     const PetscInt      *tmp, *tmpO = NULL;
4298:     PetscInt             tmpSize;

4300:     if (PetscDefined(USE_DEBUG)) {
4301:       PetscInt nO = DMPolytopeTypeGetNumArrangements(qt) / 2;
4302:       PetscCheck(!o || !(o >= nO || o < -nO), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid orientation %" PetscInt_FMT " not in [%" PetscInt_FMT ",%" PetscInt_FMT ") for %s %" PetscInt_FMT, o, -nO, nO, DMPolytopeTypes[qt], q);
4303:     }
4304:     PetscCall(DMPlexGetTransitiveClosure_Hot_Private(dm, q, useCone, &tmpSize, &tmp, &tmpO));
4305:     for (PetscInt t = 0; t < tmpSize; ++t) {
4306:       const PetscInt ip = useCone && qarr ? qarr[t * 2] : t;
4307:       const PetscInt io = useCone && qarr ? qarr[t * 2 + 1] : 0;
4308:       const PetscInt cp = tmp[ip];
4309:       PetscCall(DMPlexGetCellType(dm, cp, &ct));
4310:       const PetscInt co = tmpO ? DMPolytopeTypeComposeOrientation(ct, io, tmpO[ip]) : 0;
4311:       PetscInt       c;

4313:       /* Check for duplicate */
4314:       for (c = 0; c < closureSize; c += 2) {
4315:         if (closure[c] == cp) break;
4316:       }
4317:       if (c == closureSize) {
4318:         closure[closureSize++] = cp;
4319:         closure[closureSize++] = co;
4320:         fifo[fifoSize++]       = cp;
4321:         fifo[fifoSize++]       = co;
4322:         fifo[fifoSize++]       = ct;
4323:       }
4324:     }
4325:     PetscCall(DMPlexRestoreTransitiveClosure_Hot_Private(dm, q, useCone, &tmpSize, &tmp, &tmpO));
4326:   }
4327:   PetscCall(DMRestoreWorkArray(dm, 3 * maxSize, MPIU_INT, &fifo));
4328:   if (numPoints) *numPoints = closureSize / 2;
4329:   if (points) *points = closure;
4330:   PetscFunctionReturn(PETSC_SUCCESS);
4331: }

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

4336:   Not Collective

4338:   Input Parameters:
4339: + dm      - The `DMPLEX`
4340: . p       - The mesh point
4341: - useCone - `PETSC_TRUE` for the closure, otherwise return the support

4343:   Input/Output Parameter:
4344: . points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...];
4345:            if *points is `NULL` on input, internal storage will be returned, use `DMPlexRestoreTransitiveClosure()`,
4346:            otherwise the provided array is used to hold the values

4348:   Output Parameter:
4349: . numPoints - The number of points in the closure, so `points` is of size 2*`numPoints`

4351:   Level: beginner

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

4356:   Fortran Notes:
4357:   `points` must be declared with
4358: .vb
4359:   PetscInt, pointer :: points(:)
4360: .ve
4361:   and is always allocated by the function.

4363:   Pass `PETSC_NULL_INTEGER` for `numPoints` if it is not needed

4365: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexRestoreTransitiveClosure()`, `DMPlexCreate()`, `DMPlexSetCone()`, `DMPlexSetChart()`, `DMPlexGetCone()`
4366: @*/
4367: PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
4368: {
4369:   PetscFunctionBeginHot;
4371:   if (numPoints) PetscAssertPointer(numPoints, 4);
4372:   if (points) PetscAssertPointer(points, 5);
4373:   if (PetscDefined(USE_DEBUG)) {
4374:     PetscInt pStart, pEnd;
4375:     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
4376:     PetscCheck(p >= pStart && p < pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %" PetscInt_FMT " is not in [%" PetscInt_FMT ", %" PetscInt_FMT ")", p, pStart, pEnd);
4377:   }
4378:   PetscCall(DMPlexGetTransitiveClosure_Internal(dm, p, 0, useCone, numPoints, points));
4379:   PetscFunctionReturn(PETSC_SUCCESS);
4380: }

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

4385:   Not Collective

4387:   Input Parameters:
4388: + dm        - The `DMPLEX`
4389: . p         - The mesh point
4390: . useCone   - `PETSC_TRUE` for the closure, otherwise return the star
4391: . numPoints - The number of points in the closure, so points[] is of size 2*`numPoints`
4392: - points    - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]

4394:   Level: beginner

4396:   Note:
4397:   If not using internal storage (points is not `NULL` on input), this call is unnecessary

4399: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetTransitiveClosure()`, `DMPlexCreate()`, `DMPlexSetCone()`, `DMPlexSetChart()`, `DMPlexGetCone()`
4400: @*/
4401: PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
4402: {
4403:   PetscFunctionBeginHot;
4405:   if (numPoints) *numPoints = 0;
4406:   PetscCall(DMRestoreWorkArray(dm, 0, MPIU_INT, points));
4407:   PetscFunctionReturn(PETSC_SUCCESS);
4408: }

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

4413:   Not Collective

4415:   Input Parameter:
4416: . dm - The `DMPLEX`

4418:   Output Parameters:
4419: + maxConeSize    - The maximum number of in-edges
4420: - maxSupportSize - The maximum number of out-edges

4422:   Level: beginner

4424: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSetConeSize()`, `DMPlexSetChart()`
4425: @*/
4426: PetscErrorCode DMPlexGetMaxSizes(DM dm, PeOp PetscInt *maxConeSize, PeOp PetscInt *maxSupportSize)
4427: {
4428:   DM_Plex *mesh = (DM_Plex *)dm->data;

4430:   PetscFunctionBegin;
4432:   if (maxConeSize) PetscCall(PetscSectionGetMaxDof(mesh->coneSection, maxConeSize));
4433:   if (maxSupportSize) PetscCall(PetscSectionGetMaxDof(mesh->supportSection, maxSupportSize));
4434:   PetscFunctionReturn(PETSC_SUCCESS);
4435: }

4437: PetscErrorCode DMSetUp_Plex(DM dm)
4438: {
4439:   DM_Plex *mesh = (DM_Plex *)dm->data;
4440:   PetscInt size, maxSupportSize;

4442:   PetscFunctionBegin;
4444:   PetscCall(PetscSectionSetUp(mesh->coneSection));
4445:   PetscCall(PetscSectionGetStorageSize(mesh->coneSection, &size));
4446:   PetscCall(PetscMalloc1(size, &mesh->cones));
4447:   PetscCall(PetscCalloc1(size, &mesh->coneOrientations));
4448:   PetscCall(PetscSectionGetMaxDof(mesh->supportSection, &maxSupportSize));
4449:   if (maxSupportSize) {
4450:     PetscCall(PetscSectionSetUp(mesh->supportSection));
4451:     PetscCall(PetscSectionGetStorageSize(mesh->supportSection, &size));
4452:     PetscCall(PetscMalloc1(size, &mesh->supports));
4453:   }
4454:   PetscFunctionReturn(PETSC_SUCCESS);
4455: }

4457: PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
4458: {
4459:   PetscFunctionBegin;
4460:   if (subdm) PetscCall(DMClone(dm, subdm));
4461:   PetscCall(DMCreateSectionSubDM(dm, numFields, fields, NULL, NULL, is, subdm));
4462:   if (subdm) (*subdm)->useNatural = dm->useNatural;
4463:   if (dm->useNatural && dm->sfMigration) {
4464:     PetscSF sfNatural;

4466:     (*subdm)->sfMigration = dm->sfMigration;
4467:     PetscCall(PetscObjectReference((PetscObject)dm->sfMigration));
4468:     PetscCall(DMPlexCreateGlobalToNaturalSF(*subdm, NULL, (*subdm)->sfMigration, &sfNatural));
4469:     (*subdm)->sfNatural = sfNatural;
4470:   }
4471:   PetscFunctionReturn(PETSC_SUCCESS);
4472: }

4474: PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm)
4475: {
4476:   PetscInt i = 0;

4478:   PetscFunctionBegin;
4479:   PetscCall(DMClone(dms[0], superdm));
4480:   PetscCall(DMCreateSectionSuperDM(dms, len, is, superdm));
4481:   (*superdm)->useNatural = PETSC_FALSE;
4482:   for (i = 0; i < len; i++) {
4483:     if (dms[i]->useNatural && dms[i]->sfMigration) {
4484:       PetscSF sfNatural;

4486:       (*superdm)->sfMigration = dms[i]->sfMigration;
4487:       PetscCall(PetscObjectReference((PetscObject)dms[i]->sfMigration));
4488:       (*superdm)->useNatural = PETSC_TRUE;
4489:       PetscCall(DMPlexCreateGlobalToNaturalSF(*superdm, NULL, (*superdm)->sfMigration, &sfNatural));
4490:       (*superdm)->sfNatural = sfNatural;
4491:       break;
4492:     }
4493:   }
4494:   PetscFunctionReturn(PETSC_SUCCESS);
4495: }

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

4500:   Not Collective

4502:   Input Parameter:
4503: . dm - The `DMPLEX`

4505:   Level: beginner

4507:   Note:
4508:   This should be called after all calls to `DMPlexSetCone()`

4510: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSetChart()`, `DMPlexSetConeSize()`, `DMPlexSetCone()`
4511: @*/
4512: PetscErrorCode DMPlexSymmetrize(DM dm)
4513: {
4514:   DM_Plex  *mesh = (DM_Plex *)dm->data;
4515:   PetscInt *offsets;
4516:   PetscInt  supportSize;
4517:   PetscInt  pStart, pEnd, p;

4519:   PetscFunctionBegin;
4521:   PetscCheck(!mesh->supports, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
4522:   PetscCall(PetscLogEventBegin(DMPLEX_Symmetrize, dm, 0, 0, 0));
4523:   /* Calculate support sizes */
4524:   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
4525:   for (p = pStart; p < pEnd; ++p) {
4526:     PetscInt dof, off, c;

4528:     PetscCall(PetscSectionGetDof(mesh->coneSection, p, &dof));
4529:     PetscCall(PetscSectionGetOffset(mesh->coneSection, p, &off));
4530:     for (c = off; c < off + dof; ++c) PetscCall(PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1));
4531:   }
4532:   PetscCall(PetscSectionSetUp(mesh->supportSection));
4533:   /* Calculate supports */
4534:   PetscCall(PetscSectionGetStorageSize(mesh->supportSection, &supportSize));
4535:   PetscCall(PetscMalloc1(supportSize, &mesh->supports));
4536:   PetscCall(PetscCalloc1(pEnd - pStart, &offsets));
4537:   for (p = pStart; p < pEnd; ++p) {
4538:     PetscInt dof, off, c;

4540:     PetscCall(PetscSectionGetDof(mesh->coneSection, p, &dof));
4541:     PetscCall(PetscSectionGetOffset(mesh->coneSection, p, &off));
4542:     for (c = off; c < off + dof; ++c) {
4543:       const PetscInt q = mesh->cones[c];
4544:       PetscInt       offS;

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

4548:       mesh->supports[offS + offsets[q]] = p;
4549:       ++offsets[q];
4550:     }
4551:   }
4552:   PetscCall(PetscFree(offsets));
4553:   PetscCall(PetscLogEventEnd(DMPLEX_Symmetrize, dm, 0, 0, 0));
4554:   PetscFunctionReturn(PETSC_SUCCESS);
4555: }

4557: static PetscErrorCode DMPlexCreateDepthStratum(DM dm, DMLabel label, PetscInt depth, PetscInt pStart, PetscInt pEnd)
4558: {
4559:   IS stratumIS;

4561:   PetscFunctionBegin;
4562:   if (pStart >= pEnd) PetscFunctionReturn(PETSC_SUCCESS);
4563:   if (PetscDefined(USE_DEBUG)) {
4564:     PetscInt  qStart, qEnd, numLevels, level;
4565:     PetscBool overlap = PETSC_FALSE;
4566:     PetscCall(DMLabelGetNumValues(label, &numLevels));
4567:     for (level = 0; level < numLevels; level++) {
4568:       PetscCall(DMLabelGetStratumBounds(label, level, &qStart, &qEnd));
4569:       if ((pStart >= qStart && pStart < qEnd) || (pEnd > qStart && pEnd <= qEnd)) {
4570:         overlap = PETSC_TRUE;
4571:         break;
4572:       }
4573:     }
4574:     PetscCheck(!overlap, PETSC_COMM_SELF, PETSC_ERR_PLIB, "New depth %" PetscInt_FMT " range [%" PetscInt_FMT ",%" PetscInt_FMT ") overlaps with depth %" PetscInt_FMT " range [%" PetscInt_FMT ",%" PetscInt_FMT ")", depth, pStart, pEnd, level, qStart, qEnd);
4575:   }
4576:   PetscCall(ISCreateStride(PETSC_COMM_SELF, pEnd - pStart, pStart, 1, &stratumIS));
4577:   PetscCall(DMLabelSetStratumIS(label, depth, stratumIS));
4578:   PetscCall(ISDestroy(&stratumIS));
4579:   PetscFunctionReturn(PETSC_SUCCESS);
4580: }

4582: static PetscErrorCode DMPlexStratify_CellType_Private(DM dm, DMLabel label)
4583: {
4584:   PetscInt *pMin, *pMax;
4585:   PetscInt  pStart, pEnd;
4586:   PetscInt  dmin = PETSC_INT_MAX, dmax = PETSC_INT_MIN;

4588:   PetscFunctionBegin;
4589:   {
4590:     DMLabel label2;

4592:     PetscCall(DMPlexGetCellTypeLabel(dm, &label2));
4593:     PetscCall(PetscObjectViewFromOptions((PetscObject)label2, NULL, "-ct_view"));
4594:   }
4595:   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
4596:   for (PetscInt p = pStart; p < pEnd; ++p) {
4597:     DMPolytopeType ct;

4599:     PetscCall(DMPlexGetCellType(dm, p, &ct));
4600:     dmin = PetscMin(DMPolytopeTypeGetDim(ct), dmin);
4601:     dmax = PetscMax(DMPolytopeTypeGetDim(ct), dmax);
4602:   }
4603:   PetscCall(PetscMalloc2(dmax + 1, &pMin, dmax + 1, &pMax));
4604:   for (PetscInt d = dmin; d <= dmax; ++d) {
4605:     pMin[d] = PETSC_INT_MAX;
4606:     pMax[d] = PETSC_INT_MIN;
4607:   }
4608:   for (PetscInt p = pStart; p < pEnd; ++p) {
4609:     DMPolytopeType ct;
4610:     PetscInt       d;

4612:     PetscCall(DMPlexGetCellType(dm, p, &ct));
4613:     d       = DMPolytopeTypeGetDim(ct);
4614:     pMin[d] = PetscMin(p, pMin[d]);
4615:     pMax[d] = PetscMax(p, pMax[d]);
4616:   }
4617:   for (PetscInt d = dmin; d <= dmax; ++d) {
4618:     if (pMin[d] > pMax[d]) continue;
4619:     PetscCall(DMPlexCreateDepthStratum(dm, label, d, pMin[d], pMax[d] + 1));
4620:   }
4621:   PetscCall(PetscFree2(pMin, pMax));
4622:   PetscFunctionReturn(PETSC_SUCCESS);
4623: }

4625: static PetscErrorCode DMPlexStratify_Topological_Private(DM dm, DMLabel label)
4626: {
4627:   PetscInt dim, pStart, pEnd;
4628:   PetscInt numRoots = 0, numLeaves = 0;

4630:   PetscFunctionBegin;
4631:   PetscCall(DMGetDimension(dm, &dim));
4632:   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
4633:   {
4634:     /* Initialize roots and count leaves */
4635:     PetscInt sMin = PETSC_INT_MAX;
4636:     PetscInt sMax = PETSC_INT_MIN;
4637:     PetscInt coneSize, supportSize;

4639:     for (PetscInt p = pStart; p < pEnd; ++p) {
4640:       PetscCall(DMPlexGetConeSize(dm, p, &coneSize));
4641:       PetscCall(DMPlexGetSupportSize(dm, p, &supportSize));
4642:       if (!coneSize && supportSize) {
4643:         sMin = PetscMin(p, sMin);
4644:         sMax = PetscMax(p, sMax);
4645:         ++numRoots;
4646:       } else if (!supportSize && coneSize) {
4647:         ++numLeaves;
4648:       } else if (!supportSize && !coneSize) {
4649:         /* Isolated points */
4650:         sMin = PetscMin(p, sMin);
4651:         sMax = PetscMax(p, sMax);
4652:       }
4653:     }
4654:     PetscCall(DMPlexCreateDepthStratum(dm, label, 0, sMin, sMax + 1));
4655:   }

4657:   if (numRoots + numLeaves == (pEnd - pStart)) {
4658:     PetscInt sMin = PETSC_INT_MAX;
4659:     PetscInt sMax = PETSC_INT_MIN;
4660:     PetscInt coneSize, supportSize;

4662:     for (PetscInt p = pStart; p < pEnd; ++p) {
4663:       PetscCall(DMPlexGetConeSize(dm, p, &coneSize));
4664:       PetscCall(DMPlexGetSupportSize(dm, p, &supportSize));
4665:       if (!supportSize && coneSize) {
4666:         sMin = PetscMin(p, sMin);
4667:         sMax = PetscMax(p, sMax);
4668:       }
4669:     }
4670:     PetscCall(DMPlexCreateDepthStratum(dm, label, 1, sMin, sMax + 1));
4671:   } else {
4672:     PetscInt  level = 0;
4673:     PetscInt  qStart, qEnd;
4674:     PetscInt *bounds;

4676:     PetscCall(PetscMalloc1((dim + 3) * 2, &bounds));
4677:     PetscCall(DMLabelGetStratumBounds(label, level, &qStart, &qEnd));
4678:     bounds[level * 2 + 0] = qStart;
4679:     bounds[level * 2 + 1] = qEnd;
4680:     while (qEnd > qStart) {
4681:       PetscInt sMin = PETSC_INT_MAX;
4682:       PetscInt sMax = PETSC_INT_MIN;

4684:       for (PetscInt q = qStart; q < qEnd; ++q) {
4685:         const PetscInt *support;
4686:         PetscInt        supportSize;

4688:         PetscCall(DMPlexGetSupportSize(dm, q, &supportSize));
4689:         PetscCall(DMPlexGetSupport(dm, q, &support));
4690:         for (PetscInt s = 0; s < supportSize; ++s) {
4691:           sMin = PetscMin(support[s], sMin);
4692:           sMax = PetscMax(support[s], sMax);
4693:         }
4694:       }
4695:       PetscCall(DMLabelGetNumValues(label, &level));
4696:       PetscCall(DMPlexCreateDepthStratum(dm, label, level, sMin, sMax + 1));
4697:       PetscCall(DMLabelGetStratumBounds(label, level, &qStart, &qEnd));
4698:       bounds[level * 2 + 0] = qStart;
4699:       bounds[level * 2 + 1] = qEnd;
4700:       for (PetscInt l = 0; l < level; ++l) {
4701:         PetscBool intersect = PETSC_FALSE;

4703:         if (bounds[level * 2 + 0] <= bounds[l * 2 + 0]) {
4704:           if (bounds[level * 2 + 1] > bounds[l * 2 + 0]) intersect = PETSC_TRUE;
4705:         } else {
4706:           if (bounds[l * 2 + 1] > bounds[level * 2 + 0]) intersect = PETSC_TRUE;
4707:         }
4708:         if (intersect) {
4709:           PetscCall(PetscPrintf(PETSC_COMM_SELF, "[%d] numRoots %" PetscInt_FMT " numLeaves %" PetscInt_FMT "\n", PetscGlobalRank, numRoots, numLeaves));
4710:           for (PetscInt m = 0; m < level; ++m) {
4711:             PetscCall(PetscPrintf(PETSC_COMM_SELF, "[%d]   Level %" PetscInt_FMT " [%" PetscInt_FMT ", %" PetscInt_FMT ")\n", PetscGlobalRank, m, bounds[m * 2 + 0], bounds[m * 2 + 1]));
4712:           }
4713:         }
4714:         PetscCheck(!intersect, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Depth %" PetscInt_FMT " [%" PetscInt_FMT ", %" PetscInt_FMT ") intersects depth %" PetscInt_FMT " [%" PetscInt_FMT ", %" PetscInt_FMT ")", level, bounds[level * 2 + 0], bounds[level * 2 + 1], l, bounds[l * 2 + 0], bounds[l * 2 + 1]);
4715:       }
4716:       PetscCheck(level <= dim + 1 || sMax < sMin, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Mesh of dimension %" PetscInt_FMT " trying to create depth %" PetscInt_FMT " [%" PetscInt_FMT ", %" PetscInt_FMT ") with chart [%" PetscInt_FMT ", %" PetscInt_FMT ")", dim, level, sMin, sMax + 1, pStart, pEnd);
4717:     }
4718:     PetscCall(PetscFree(bounds));
4719:   }
4720:   PetscFunctionReturn(PETSC_SUCCESS);
4721: }

4723: /*@
4724:   DMPlexStratify - Computes the strata for all points in the `DMPLEX`

4726:   Collective

4728:   Input Parameter:
4729: . dm - The `DMPLEX`

4731:   Level: beginner

4733:   Notes:
4734:   The strata group all points of the same grade, and this function calculates the strata. This
4735:   grade can be seen as the height (or depth) of the point in the DAG.

4737:   The DAG for most topologies is a graded poset (https://en.wikipedia.org/wiki/Graded_poset), and
4738:   can be illustrated by a Hasse Diagram (https://en.wikipedia.org/wiki/Hasse_diagram).
4739:   Concretely, `DMPlexStratify()` creates a new label named "depth" containing the depth in the DAG of each point. For cell-vertex
4740:   meshes, vertices are depth 0 and cells are depth 1. For fully interpolated meshes, depth 0 for vertices, 1 for edges, and so on
4741:   until cells have depth equal to the dimension of the mesh. The depth label can be accessed through `DMPlexGetDepthLabel()` or `DMPlexGetDepthStratum()`, or
4742:   manually via `DMGetLabel()`.  The height is defined implicitly by height = maxDimension - depth, and can be accessed
4743:   via `DMPlexGetHeightStratum()`.  For example, cells have height 0 and faces have height 1.

4745:   The depth of a point is calculated by executing a breadth-first search (BFS) on the DAG. This could produce surprising results
4746:   if run on a partially interpolated mesh, meaning one that had some edges and faces, but not others. For example, suppose that
4747:   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
4748:   to interpolate only that one (e0), so that
4749: .vb
4750:   cone(c0) = {e0, v2}
4751:   cone(e0) = {v0, v1}
4752: .ve
4753:   If `DMPlexStratify()` is run on this mesh, it will give depths
4754: .vb
4755:    depth 0 = {v0, v1, v2}
4756:    depth 1 = {e0, c0}
4757: .ve
4758:   where the triangle has been given depth 1, instead of 2, because it is reachable from vertex v2.

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

4762: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSymmetrize()`, `DMPlexComputeCellTypes()`
4763: @*/
4764: PetscErrorCode DMPlexStratify(DM dm)
4765: {
4766:   DM_Plex  *mesh = (DM_Plex *)dm->data;
4767:   DMLabel   label;
4768:   PetscBool flg = PETSC_FALSE;

4770:   PetscFunctionBegin;
4772:   PetscCall(PetscLogEventBegin(DMPLEX_Stratify, dm, 0, 0, 0));

4774:   // Create depth label
4775:   PetscCall(DMRemoveLabel(dm, "depth", NULL));
4776:   PetscCall(DMCreateLabel(dm, "depth"));
4777:   PetscCall(DMPlexGetDepthLabel(dm, &label));

4779:   PetscCall(PetscOptionsGetBool(NULL, dm->hdr.prefix, "-dm_plex_stratify_celltype", &flg, NULL));
4780:   if (flg) PetscCall(DMPlexStratify_CellType_Private(dm, label));
4781:   else PetscCall(DMPlexStratify_Topological_Private(dm, label));

4783:   { /* just in case there is an empty process */
4784:     PetscInt numValues, maxValues = 0, v;

4786:     PetscCall(DMLabelGetNumValues(label, &numValues));
4787:     PetscCallMPI(MPIU_Allreduce(&numValues, &maxValues, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
4788:     for (v = numValues; v < maxValues; v++) PetscCall(DMLabelAddStratum(label, v));
4789:   }
4790:   PetscCall(PetscObjectStateGet((PetscObject)label, &mesh->depthState));
4791:   PetscCall(PetscLogEventEnd(DMPLEX_Stratify, dm, 0, 0, 0));
4792:   PetscFunctionReturn(PETSC_SUCCESS);
4793: }

4795: PetscErrorCode DMPlexComputeCellType_Internal(DM dm, PetscInt p, PetscInt pdepth, DMPolytopeType *pt)
4796: {
4797:   DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
4798:   PetscInt       dim, depth, pheight, coneSize;
4799:   PetscBool      preferTensor;

4801:   PetscFunctionBeginHot;
4802:   PetscCall(DMGetDimension(dm, &dim));
4803:   PetscCall(DMPlexGetDepth(dm, &depth));
4804:   PetscCall(DMPlexGetConeSize(dm, p, &coneSize));
4805:   PetscCall(DMPlexGetInterpolatePreferTensor(dm, &preferTensor));
4806:   pheight = depth - pdepth;
4807:   if (depth <= 1) {
4808:     switch (pdepth) {
4809:     case 0:
4810:       ct = DM_POLYTOPE_POINT;
4811:       break;
4812:     case 1:
4813:       switch (coneSize) {
4814:       case 2:
4815:         ct = DM_POLYTOPE_SEGMENT;
4816:         break;
4817:       case 3:
4818:         ct = DM_POLYTOPE_TRIANGLE;
4819:         break;
4820:       case 4:
4821:         switch (dim) {
4822:         case 2:
4823:           ct = DM_POLYTOPE_QUADRILATERAL;
4824:           break;
4825:         case 3:
4826:           ct = DM_POLYTOPE_TETRAHEDRON;
4827:           break;
4828:         default:
4829:           break;
4830:         }
4831:         break;
4832:       case 5:
4833:         ct = DM_POLYTOPE_PYRAMID;
4834:         break;
4835:       case 6:
4836:         ct = preferTensor ? DM_POLYTOPE_TRI_PRISM_TENSOR : DM_POLYTOPE_TRI_PRISM;
4837:         break;
4838:       case 8:
4839:         ct = DM_POLYTOPE_HEXAHEDRON;
4840:         break;
4841:       default:
4842:         break;
4843:       }
4844:     }
4845:   } else {
4846:     if (pdepth == 0) {
4847:       ct = DM_POLYTOPE_POINT;
4848:     } else if (pheight == 0) {
4849:       switch (dim) {
4850:       case 1:
4851:         switch (coneSize) {
4852:         case 2:
4853:           ct = DM_POLYTOPE_SEGMENT;
4854:           break;
4855:         default:
4856:           break;
4857:         }
4858:         break;
4859:       case 2:
4860:         switch (coneSize) {
4861:         case 3:
4862:           ct = DM_POLYTOPE_TRIANGLE;
4863:           break;
4864:         case 4:
4865:           ct = DM_POLYTOPE_QUADRILATERAL;
4866:           break;
4867:         default:
4868:           break;
4869:         }
4870:         break;
4871:       case 3:
4872:         switch (coneSize) {
4873:         case 4:
4874:           ct = DM_POLYTOPE_TETRAHEDRON;
4875:           break;
4876:         case 5: {
4877:           const PetscInt *cone;
4878:           PetscInt        faceConeSize;

4880:           PetscCall(DMPlexGetCone(dm, p, &cone));
4881:           PetscCall(DMPlexGetConeSize(dm, cone[0], &faceConeSize));
4882:           switch (faceConeSize) {
4883:           case 3:
4884:             ct = preferTensor ? DM_POLYTOPE_TRI_PRISM_TENSOR : DM_POLYTOPE_TRI_PRISM;
4885:             break;
4886:           case 4:
4887:             ct = DM_POLYTOPE_PYRAMID;
4888:             break;
4889:           }
4890:         } break;
4891:         case 6:
4892:           ct = DM_POLYTOPE_HEXAHEDRON;
4893:           break;
4894:         default:
4895:           break;
4896:         }
4897:         break;
4898:       default:
4899:         break;
4900:       }
4901:     } else if (pheight > 0) {
4902:       switch (coneSize) {
4903:       case 2:
4904:         ct = DM_POLYTOPE_SEGMENT;
4905:         break;
4906:       case 3:
4907:         ct = DM_POLYTOPE_TRIANGLE;
4908:         break;
4909:       case 4:
4910:         ct = DM_POLYTOPE_QUADRILATERAL;
4911:         break;
4912:       default:
4913:         break;
4914:       }
4915:     }
4916:   }
4917:   *pt = ct;
4918:   PetscFunctionReturn(PETSC_SUCCESS);
4919: }

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

4924:   Collective

4926:   Input Parameter:
4927: . dm - The `DMPLEX`

4929:   Level: developer

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

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

4938: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreate()`, `DMPlexSymmetrize()`, `DMPlexStratify()`, `DMGetLabel()`, `DMCreateLabel()`
4939: @*/
4940: PetscErrorCode DMPlexComputeCellTypes(DM dm)
4941: {
4942:   DM_Plex *mesh;
4943:   DMLabel  ctLabel;
4944:   PetscInt pStart, pEnd, p;

4946:   PetscFunctionBegin;
4948:   mesh = (DM_Plex *)dm->data;
4949:   PetscCall(DMCreateLabel(dm, "celltype"));
4950:   PetscCall(DMPlexGetCellTypeLabel(dm, &ctLabel));
4951:   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
4952:   PetscCall(PetscFree(mesh->cellTypes));
4953:   PetscCall(PetscMalloc1(pEnd - pStart, &mesh->cellTypes));
4954:   for (p = pStart; p < pEnd; ++p) {
4955:     DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
4956:     PetscInt       pdepth;

4958:     PetscCall(DMPlexGetPointDepth(dm, p, &pdepth));
4959:     PetscCall(DMPlexComputeCellType_Internal(dm, p, pdepth, &ct));
4960:     PetscCheck(ct != DM_POLYTOPE_UNKNOWN && ct != DM_POLYTOPE_UNKNOWN_CELL && ct != DM_POLYTOPE_UNKNOWN_FACE, PETSC_COMM_SELF, PETSC_ERR_SUP, "Point %" PetscInt_FMT " has invalid celltype (%s)", p, DMPolytopeTypes[ct]);
4961:     PetscCall(DMLabelSetValue(ctLabel, p, ct));
4962:     mesh->cellTypes[p - pStart].value_as_uint8 = (uint8_t)ct;
4963:   }
4964:   PetscCall(PetscObjectStateGet((PetscObject)ctLabel, &mesh->celltypeState));
4965:   PetscCall(PetscObjectViewFromOptions((PetscObject)ctLabel, NULL, "-dm_plex_celltypes_view"));
4966:   PetscFunctionReturn(PETSC_SUCCESS);
4967: }

4969: /*@
4970:   DMPlexGetJoin - Get an array for the join of the set of points

4972:   A "join" is the intersection of the support of each of the input points.
4973:   A "full join" is the intersection of the transitive support of each of the input points.

4975:   Not Collective

4977:   Input Parameters:
4978: + dm        - The `DMPLEX` object
4979: . numPoints - The number of input points for the join
4980: - points    - The input points

4982:   Output Parameters:
4983: + numCoveredPoints - The number of points in the join
4984: - coveredPoints    - The points in the join

4986:   Level: intermediate

4988:   Note:
4989:   Currently, this is restricted to a single level join

4991:   Fortran Notes:
4992:   `converedPoints` must be declared with
4993: .vb
4994:   PetscInt, pointer :: coveredPints(:)
4995: .ve

4997: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexRestoreJoin()`, `DMPlexGetMeet()`, `DMPlexGetFullJoin()`
4998: @*/
4999: PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt *coveredPoints[])
5000: {
5001:   DM_Plex  *mesh = (DM_Plex *)dm->data;
5002:   PetscInt *join[2];
5003:   PetscInt  joinSize, i = 0;
5004:   PetscInt  dof, off, p, c, m;
5005:   PetscInt  maxSupportSize;

5007:   PetscFunctionBegin;
5009:   PetscAssertPointer(points, 3);
5010:   PetscAssertPointer(numCoveredPoints, 4);
5011:   PetscAssertPointer(coveredPoints, 5);
5012:   PetscCall(PetscSectionGetMaxDof(mesh->supportSection, &maxSupportSize));
5013:   PetscCall(DMGetWorkArray(dm, maxSupportSize, MPIU_INT, &join[0]));
5014:   PetscCall(DMGetWorkArray(dm, maxSupportSize, MPIU_INT, &join[1]));
5015:   /* Copy in support of first point */
5016:   PetscCall(PetscSectionGetDof(mesh->supportSection, points[0], &dof));
5017:   PetscCall(PetscSectionGetOffset(mesh->supportSection, points[0], &off));
5018:   for (joinSize = 0; joinSize < dof; ++joinSize) join[i][joinSize] = mesh->supports[off + joinSize];
5019:   /* Check each successive support */
5020:   for (p = 1; p < numPoints; ++p) {
5021:     PetscInt newJoinSize = 0;

5023:     PetscCall(PetscSectionGetDof(mesh->supportSection, points[p], &dof));
5024:     PetscCall(PetscSectionGetOffset(mesh->supportSection, points[p], &off));
5025:     for (c = 0; c < dof; ++c) {
5026:       const PetscInt point = mesh->supports[off + c];

5028:       for (m = 0; m < joinSize; ++m) {
5029:         if (point == join[i][m]) {
5030:           join[1 - i][newJoinSize++] = point;
5031:           break;
5032:         }
5033:       }
5034:     }
5035:     joinSize = newJoinSize;
5036:     i        = 1 - i;
5037:   }
5038:   *numCoveredPoints = joinSize;
5039:   *coveredPoints    = join[i];
5040:   PetscCall(DMRestoreWorkArray(dm, maxSupportSize, MPIU_INT, &join[1 - i]));
5041:   PetscFunctionReturn(PETSC_SUCCESS);
5042: }

5044: /*@
5045:   DMPlexRestoreJoin - Restore an array for the join of the set of points obtained with `DMPlexGetJoin()`

5047:   Not Collective

5049:   Input Parameters:
5050: + dm        - The `DMPLEX` object
5051: . numPoints - The number of input points for the join
5052: - points    - The input points

5054:   Output Parameters:
5055: + numCoveredPoints - The number of points in the join
5056: - coveredPoints    - The points in the join

5058:   Level: intermediate

5060:   Fortran Notes:
5061:   `converedPoints` must be declared with
5062: .vb
5063:   PetscInt, pointer :: coveredPoints(:)
5064: .ve

5066:   Pass `PETSC_NULL_INTEGER` for `numCoveredPoints` if it is not needed

5068: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetJoin()`, `DMPlexGetFullJoin()`, `DMPlexGetMeet()`
5069: @*/
5070: PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt *coveredPoints[])
5071: {
5072:   PetscFunctionBegin;
5074:   if (points) PetscAssertPointer(points, 3);
5075:   if (numCoveredPoints) PetscAssertPointer(numCoveredPoints, 4);
5076:   PetscAssertPointer(coveredPoints, 5);
5077:   PetscCall(DMRestoreWorkArray(dm, 0, MPIU_INT, (void *)coveredPoints));
5078:   if (numCoveredPoints) *numCoveredPoints = 0;
5079:   PetscFunctionReturn(PETSC_SUCCESS);
5080: }

5082: /*@
5083:   DMPlexGetFullJoin - Get an array for the full join of the set of points

5085:   A "join" is the intersection of the support of each of the input points.
5086:   A "full join" is the intersection of the transitive support of each of the input points.

5088:   Not Collective

5090:   Input Parameters:
5091: + dm        - The `DMPLEX` object
5092: . numPoints - The number of input points for the join
5093: - points    - The input points, its length is `numPoints`

5095:   Output Parameters:
5096: + numCoveredPoints - The number of points in the join
5097: - coveredPoints    - The points in the join, its length is `numCoveredPoints`

5099:   Level: intermediate

5101:   Fortran Notes:
5102: .vb
5103:   PetscInt, pointer :: coveredPints(:)
5104: .ve

5106: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetJoin()`, `DMPlexRestoreJoin()`, `DMPlexGetMeet()`
5107: @*/
5108: PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt *coveredPoints[])
5109: {
5110:   PetscInt *offsets, **closures;
5111:   PetscInt *join[2];
5112:   PetscInt  depth = 0, maxSize, joinSize = 0, i = 0;
5113:   PetscInt  p, d, c, m, ms;

5115:   PetscFunctionBegin;
5117:   PetscAssertPointer(points, 3);
5118:   PetscAssertPointer(numCoveredPoints, 4);
5119:   PetscAssertPointer(coveredPoints, 5);

5121:   PetscCall(DMPlexGetDepth(dm, &depth));
5122:   PetscCall(PetscCalloc1(numPoints, &closures));
5123:   PetscCall(DMGetWorkArray(dm, numPoints * (depth + 2), MPIU_INT, &offsets));
5124:   PetscCall(DMPlexGetMaxSizes(dm, NULL, &ms));
5125:   maxSize = (ms > 1) ? ((PetscPowInt(ms, depth + 1) - 1) / (ms - 1)) : depth + 1;
5126:   PetscCall(DMGetWorkArray(dm, maxSize, MPIU_INT, &join[0]));
5127:   PetscCall(DMGetWorkArray(dm, maxSize, MPIU_INT, &join[1]));

5129:   for (p = 0; p < numPoints; ++p) {
5130:     PetscInt closureSize;

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

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

5138:       PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd));
5139:       for (i = offsets[p * (depth + 2) + d]; i < closureSize; ++i) {
5140:         if (pStart > closures[p][i * 2] || pEnd <= closures[p][i * 2]) {
5141:           offsets[p * (depth + 2) + d + 1] = i;
5142:           break;
5143:         }
5144:       }
5145:       if (i == closureSize) offsets[p * (depth + 2) + d + 1] = i;
5146:     }
5147:     PetscCheck(offsets[p * (depth + 2) + depth + 1] == closureSize, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Total size of closure %" PetscInt_FMT " should be %" PetscInt_FMT, offsets[p * (depth + 2) + depth + 1], closureSize);
5148:   }
5149:   for (d = 0; d < depth + 1; ++d) {
5150:     PetscInt dof;

5152:     /* Copy in support of first point */
5153:     dof = offsets[d + 1] - offsets[d];
5154:     for (joinSize = 0; joinSize < dof; ++joinSize) join[i][joinSize] = closures[0][(offsets[d] + joinSize) * 2];
5155:     /* Check each successive cone */
5156:     for (p = 1; p < numPoints && joinSize; ++p) {
5157:       PetscInt newJoinSize = 0;

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

5163:         for (m = 0; m < joinSize; ++m) {
5164:           if (point == join[i][m]) {
5165:             join[1 - i][newJoinSize++] = point;
5166:             break;
5167:           }
5168:         }
5169:       }
5170:       joinSize = newJoinSize;
5171:       i        = 1 - i;
5172:     }
5173:     if (joinSize) break;
5174:   }
5175:   *numCoveredPoints = joinSize;
5176:   *coveredPoints    = join[i];
5177:   for (p = 0; p < numPoints; ++p) PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]));
5178:   PetscCall(PetscFree(closures));
5179:   PetscCall(DMRestoreWorkArray(dm, numPoints * (depth + 2), MPIU_INT, &offsets));
5180:   PetscCall(DMRestoreWorkArray(dm, ms, MPIU_INT, &join[1 - i]));
5181:   PetscFunctionReturn(PETSC_SUCCESS);
5182: }

5184: /*@
5185:   DMPlexGetMeet - Get an array for the meet of the set of points

5187:   A "meet" is the intersection of the cone of each of the input points.
5188:   A "full meet" is the intersection of the transitive cone of each of the input points.

5190:   Not Collective

5192:   Input Parameters:
5193: + dm        - The `DMPLEX` object
5194: . numPoints - The number of input points for the meet
5195: - points    - The input points, of length `numPoints`

5197:   Output Parameters:
5198: + numCoveringPoints - The number of points in the meet
5199: - coveringPoints    - The points in the meet, of length `numCoveringPoints`

5201:   Level: intermediate

5203:   Note:
5204:   Currently, this is restricted to a single level meet

5206:   Fortran Note:
5207:   `coveringPoints` must be declared with
5208: .vb
5209:   PetscInt, pointer :: coveringPoints(:)
5210: .ve

5212: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexRestoreMeet()`, `DMPlexGetJoin()`, `DMPlexGetFullMeet()`
5213: @*/
5214: PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt *coveringPoints[])
5215: {
5216:   DM_Plex  *mesh = (DM_Plex *)dm->data;
5217:   PetscInt *meet[2];
5218:   PetscInt  meetSize, i = 0;
5219:   PetscInt  dof, off, p, c, m;
5220:   PetscInt  maxConeSize;

5222:   PetscFunctionBegin;
5224:   PetscAssertPointer(points, 3);
5225:   PetscAssertPointer(numCoveringPoints, 4);
5226:   PetscAssertPointer(coveringPoints, 5);
5227:   PetscCall(PetscSectionGetMaxDof(mesh->coneSection, &maxConeSize));
5228:   PetscCall(DMGetWorkArray(dm, maxConeSize, MPIU_INT, &meet[0]));
5229:   PetscCall(DMGetWorkArray(dm, maxConeSize, MPIU_INT, &meet[1]));
5230:   /* Copy in cone of first point */
5231:   PetscCall(PetscSectionGetDof(mesh->coneSection, points[0], &dof));
5232:   PetscCall(PetscSectionGetOffset(mesh->coneSection, points[0], &off));
5233:   for (meetSize = 0; meetSize < dof; ++meetSize) meet[i][meetSize] = mesh->cones[off + meetSize];
5234:   /* Check each successive cone */
5235:   for (p = 1; p < numPoints; ++p) {
5236:     PetscInt newMeetSize = 0;

5238:     PetscCall(PetscSectionGetDof(mesh->coneSection, points[p], &dof));
5239:     PetscCall(PetscSectionGetOffset(mesh->coneSection, points[p], &off));
5240:     for (c = 0; c < dof; ++c) {
5241:       const PetscInt point = mesh->cones[off + c];

5243:       for (m = 0; m < meetSize; ++m) {
5244:         if (point == meet[i][m]) {
5245:           meet[1 - i][newMeetSize++] = point;
5246:           break;
5247:         }
5248:       }
5249:     }
5250:     meetSize = newMeetSize;
5251:     i        = 1 - i;
5252:   }
5253:   *numCoveringPoints = meetSize;
5254:   *coveringPoints    = meet[i];
5255:   PetscCall(DMRestoreWorkArray(dm, maxConeSize, MPIU_INT, &meet[1 - i]));
5256:   PetscFunctionReturn(PETSC_SUCCESS);
5257: }

5259: /*@
5260:   DMPlexRestoreMeet - Restore an array for the meet of the set of points obtained with `DMPlexGetMeet()`

5262:   Not Collective

5264:   Input Parameters:
5265: + dm        - The `DMPLEX` object
5266: . numPoints - The number of input points for the meet
5267: - points    - The input points

5269:   Output Parameters:
5270: + numCoveredPoints - The number of points in the meet
5271: - coveredPoints    - The points in the meet

5273:   Level: intermediate

5275: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetMeet()`, `DMPlexGetFullMeet()`, `DMPlexGetJoin()`
5276: @*/
5277: PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt *coveredPoints[])
5278: {
5279:   PetscFunctionBegin;
5281:   if (points) PetscAssertPointer(points, 3);
5282:   if (numCoveredPoints) PetscAssertPointer(numCoveredPoints, 4);
5283:   PetscAssertPointer(coveredPoints, 5);
5284:   PetscCall(DMRestoreWorkArray(dm, 0, MPIU_INT, (void *)coveredPoints));
5285:   if (numCoveredPoints) *numCoveredPoints = 0;
5286:   PetscFunctionReturn(PETSC_SUCCESS);
5287: }

5289: /*@
5290:   DMPlexGetFullMeet - Get an array for the full meet of the set of points

5292:   A "meet" is the intersection of the cone of each of the input points.
5293:   A "full meet" is the intersection of the transitive cone of each of the input points.

5295:   Not Collective

5297:   Input Parameters:
5298: + dm        - The `DMPLEX` object
5299: . numPoints - The number of input points for the meet
5300: - points    - The input points, of length  `numPoints`

5302:   Output Parameters:
5303: + numCoveredPoints - The number of points in the meet
5304: - coveredPoints    - The points in the meet, of length  `numCoveredPoints`

5306:   Level: intermediate

5308:   Fortran Notes:
5309:   `coveredPoints` must be declared with
5310: .vb
5311:   PetscInt, pointer :: coveredPoints(:)
5312: .ve

5314: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetMeet()`, `DMPlexRestoreMeet()`, `DMPlexGetJoin()`
5315: @*/
5316: PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt *coveredPoints[])
5317: {
5318:   PetscInt *offsets, **closures;
5319:   PetscInt *meet[2];
5320:   PetscInt  height = 0, maxSize, meetSize = 0, i = 0;
5321:   PetscInt  p, h, c, m, mc;

5323:   PetscFunctionBegin;
5325:   PetscAssertPointer(points, 3);
5326:   PetscAssertPointer(numCoveredPoints, 4);
5327:   PetscAssertPointer(coveredPoints, 5);

5329:   PetscCall(DMPlexGetDepth(dm, &height));
5330:   PetscCall(PetscMalloc1(numPoints, &closures));
5331:   PetscCall(DMGetWorkArray(dm, numPoints * (height + 2), MPIU_INT, &offsets));
5332:   PetscCall(DMPlexGetMaxSizes(dm, &mc, NULL));
5333:   maxSize = (mc > 1) ? ((PetscPowInt(mc, height + 1) - 1) / (mc - 1)) : height + 1;
5334:   PetscCall(DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[0]));
5335:   PetscCall(DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[1]));

5337:   for (p = 0; p < numPoints; ++p) {
5338:     PetscInt closureSize;

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

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

5346:       PetscCall(DMPlexGetHeightStratum(dm, h, &pStart, &pEnd));
5347:       for (i = offsets[p * (height + 2) + h]; i < closureSize; ++i) {
5348:         if (pStart > closures[p][i * 2] || pEnd <= closures[p][i * 2]) {
5349:           offsets[p * (height + 2) + h + 1] = i;
5350:           break;
5351:         }
5352:       }
5353:       if (i == closureSize) offsets[p * (height + 2) + h + 1] = i;
5354:     }
5355:     PetscCheck(offsets[p * (height + 2) + height + 1] == closureSize, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Total size of closure %" PetscInt_FMT " should be %" PetscInt_FMT, offsets[p * (height + 2) + height + 1], closureSize);
5356:   }
5357:   for (h = 0; h < height + 1; ++h) {
5358:     PetscInt dof;

5360:     /* Copy in cone of first point */
5361:     dof = offsets[h + 1] - offsets[h];
5362:     for (meetSize = 0; meetSize < dof; ++meetSize) meet[i][meetSize] = closures[0][(offsets[h] + meetSize) * 2];
5363:     /* Check each successive cone */
5364:     for (p = 1; p < numPoints && meetSize; ++p) {
5365:       PetscInt newMeetSize = 0;

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

5371:         for (m = 0; m < meetSize; ++m) {
5372:           if (point == meet[i][m]) {
5373:             meet[1 - i][newMeetSize++] = point;
5374:             break;
5375:           }
5376:         }
5377:       }
5378:       meetSize = newMeetSize;
5379:       i        = 1 - i;
5380:     }
5381:     if (meetSize) break;
5382:   }
5383:   *numCoveredPoints = meetSize;
5384:   *coveredPoints    = meet[i];
5385:   for (p = 0; p < numPoints; ++p) PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]));
5386:   PetscCall(PetscFree(closures));
5387:   PetscCall(DMRestoreWorkArray(dm, numPoints * (height + 2), MPIU_INT, &offsets));
5388:   PetscCall(DMRestoreWorkArray(dm, mc, MPIU_INT, &meet[1 - i]));
5389:   PetscFunctionReturn(PETSC_SUCCESS);
5390: }

5392: /*@
5393:   DMPlexEqual - Determine if two `DM` have the same topology

5395:   Not Collective

5397:   Input Parameters:
5398: + dmA - A `DMPLEX` object
5399: - dmB - A `DMPLEX` object

5401:   Output Parameter:
5402: . equal - `PETSC_TRUE` if the topologies are identical

5404:   Level: intermediate

5406:   Note:
5407:   We are not solving graph isomorphism, so we do not permute.

5409: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCone()`
5410: @*/
5411: PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
5412: {
5413:   PetscInt depth, depthB, pStart, pEnd, pStartB, pEndB, p;

5415:   PetscFunctionBegin;
5418:   PetscAssertPointer(equal, 3);

5420:   *equal = PETSC_FALSE;
5421:   PetscCall(DMPlexGetDepth(dmA, &depth));
5422:   PetscCall(DMPlexGetDepth(dmB, &depthB));
5423:   if (depth != depthB) PetscFunctionReturn(PETSC_SUCCESS);
5424:   PetscCall(DMPlexGetChart(dmA, &pStart, &pEnd));
5425:   PetscCall(DMPlexGetChart(dmB, &pStartB, &pEndB));
5426:   if (pStart != pStartB || pEnd != pEndB) PetscFunctionReturn(PETSC_SUCCESS);
5427:   for (p = pStart; p < pEnd; ++p) {
5428:     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
5429:     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;

5431:     PetscCall(DMPlexGetConeSize(dmA, p, &coneSize));
5432:     PetscCall(DMPlexGetCone(dmA, p, &cone));
5433:     PetscCall(DMPlexGetConeOrientation(dmA, p, &ornt));
5434:     PetscCall(DMPlexGetConeSize(dmB, p, &coneSizeB));
5435:     PetscCall(DMPlexGetCone(dmB, p, &coneB));
5436:     PetscCall(DMPlexGetConeOrientation(dmB, p, &orntB));
5437:     if (coneSize != coneSizeB) PetscFunctionReturn(PETSC_SUCCESS);
5438:     for (c = 0; c < coneSize; ++c) {
5439:       if (cone[c] != coneB[c]) PetscFunctionReturn(PETSC_SUCCESS);
5440:       if (ornt[c] != orntB[c]) PetscFunctionReturn(PETSC_SUCCESS);
5441:     }
5442:     PetscCall(DMPlexGetSupportSize(dmA, p, &supportSize));
5443:     PetscCall(DMPlexGetSupport(dmA, p, &support));
5444:     PetscCall(DMPlexGetSupportSize(dmB, p, &supportSizeB));
5445:     PetscCall(DMPlexGetSupport(dmB, p, &supportB));
5446:     if (supportSize != supportSizeB) PetscFunctionReturn(PETSC_SUCCESS);
5447:     for (s = 0; s < supportSize; ++s) {
5448:       if (support[s] != supportB[s]) PetscFunctionReturn(PETSC_SUCCESS);
5449:     }
5450:   }
5451:   *equal = PETSC_TRUE;
5452:   PetscFunctionReturn(PETSC_SUCCESS);
5453: }

5455: /*@
5456:   DMPlexGetNumFaceVertices - Returns the number of vertices on a face

5458:   Not Collective

5460:   Input Parameters:
5461: + dm         - The `DMPLEX`
5462: . cellDim    - The cell dimension
5463: - numCorners - The number of vertices on a cell

5465:   Output Parameter:
5466: . numFaceVertices - The number of vertices on a face

5468:   Level: developer

5470:   Note:
5471:   Of course this can only work for a restricted set of symmetric shapes

5473: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCone()`
5474: @*/
5475: PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
5476: {
5477:   MPI_Comm comm;

5479:   PetscFunctionBegin;
5480:   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5481:   PetscAssertPointer(numFaceVertices, 4);
5482:   switch (cellDim) {
5483:   case 0:
5484:     *numFaceVertices = 0;
5485:     break;
5486:   case 1:
5487:     *numFaceVertices = 1;
5488:     break;
5489:   case 2:
5490:     switch (numCorners) {
5491:     case 3:                 /* triangle */
5492:       *numFaceVertices = 2; /* Edge has 2 vertices */
5493:       break;
5494:     case 4:                 /* quadrilateral */
5495:       *numFaceVertices = 2; /* Edge has 2 vertices */
5496:       break;
5497:     case 6:                 /* quadratic triangle, tri and quad cohesive Lagrange cells */
5498:       *numFaceVertices = 3; /* Edge has 3 vertices */
5499:       break;
5500:     case 9:                 /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
5501:       *numFaceVertices = 3; /* Edge has 3 vertices */
5502:       break;
5503:     default:
5504:       SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %" PetscInt_FMT " for dimension %" PetscInt_FMT, numCorners, cellDim);
5505:     }
5506:     break;
5507:   case 3:
5508:     switch (numCorners) {
5509:     case 4:                 /* tetradehdron */
5510:       *numFaceVertices = 3; /* Face has 3 vertices */
5511:       break;
5512:     case 6:                 /* tet cohesive cells */
5513:       *numFaceVertices = 4; /* Face has 4 vertices */
5514:       break;
5515:     case 8:                 /* hexahedron */
5516:       *numFaceVertices = 4; /* Face has 4 vertices */
5517:       break;
5518:     case 9:                 /* tet cohesive Lagrange cells */
5519:       *numFaceVertices = 6; /* Face has 6 vertices */
5520:       break;
5521:     case 10:                /* quadratic tetrahedron */
5522:       *numFaceVertices = 6; /* Face has 6 vertices */
5523:       break;
5524:     case 12:                /* hex cohesive Lagrange cells */
5525:       *numFaceVertices = 6; /* Face has 6 vertices */
5526:       break;
5527:     case 18:                /* quadratic tet cohesive Lagrange cells */
5528:       *numFaceVertices = 6; /* Face has 6 vertices */
5529:       break;
5530:     case 27:                /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
5531:       *numFaceVertices = 9; /* Face has 9 vertices */
5532:       break;
5533:     default:
5534:       SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %" PetscInt_FMT " for dimension %" PetscInt_FMT, numCorners, cellDim);
5535:     }
5536:     break;
5537:   default:
5538:     SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %" PetscInt_FMT, cellDim);
5539:   }
5540:   PetscFunctionReturn(PETSC_SUCCESS);
5541: }

5543: /*@
5544:   DMPlexGetDepthLabel - Get the `DMLabel` recording the depth of each point

5546:   Not Collective

5548:   Input Parameter:
5549: . dm - The `DMPLEX` object

5551:   Output Parameter:
5552: . depthLabel - The `DMLabel` recording point depth

5554:   Level: developer

5556: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetDepth()`, `DMPlexGetHeightStratum()`, `DMPlexGetDepthStratum()`, `DMPlexGetPointDepth()`
5557: @*/
5558: PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
5559: {
5560:   PetscFunctionBegin;
5562:   PetscAssertPointer(depthLabel, 2);
5563:   *depthLabel = dm->depthLabel;
5564:   PetscFunctionReturn(PETSC_SUCCESS);
5565: }

5567: /*@
5568:   DMPlexGetDepth - Get the depth of the DAG representing this mesh

5570:   Not Collective

5572:   Input Parameter:
5573: . dm - The `DMPLEX` object

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

5578:   Level: developer

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

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

5585:   An empty mesh gives -1.

5587: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetDepthLabel()`, `DMPlexGetDepthStratum()`, `DMPlexGetPointDepth()`, `DMPlexSymmetrize()`
5588: @*/
5589: PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
5590: {
5591:   DM_Plex *mesh = (DM_Plex *)dm->data;
5592:   DMLabel  label;
5593:   PetscInt d = -1;

5595:   PetscFunctionBegin;
5597:   PetscAssertPointer(depth, 2);
5598:   if (mesh->tr) {
5599:     PetscCall(DMPlexTransformGetDepth(mesh->tr, depth));
5600:   } else {
5601:     PetscCall(DMPlexGetDepthLabel(dm, &label));
5602:     // Allow missing depths
5603:     if (label) PetscCall(DMLabelGetValueBounds(label, NULL, &d));
5604:     *depth = d;
5605:   }
5606:   PetscFunctionReturn(PETSC_SUCCESS);
5607: }

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

5612:   Not Collective

5614:   Input Parameters:
5615: + dm    - The `DMPLEX` object
5616: - depth - The requested depth

5618:   Output Parameters:
5619: + start - The first point at this `depth`
5620: - end   - One beyond the last point at this `depth`

5622:   Level: developer

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

5629: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetHeightStratum()`, `DMPlexGetCellTypeStratum()`, `DMPlexGetDepth()`, `DMPlexGetDepthLabel()`, `DMPlexGetPointDepth()`, `DMPlexSymmetrize()`, `DMPlexInterpolate()`
5630: @*/
5631: PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt depth, PeOp PetscInt *start, PeOp PetscInt *end)
5632: {
5633:   DM_Plex *mesh = (DM_Plex *)dm->data;
5634:   DMLabel  label;
5635:   PetscInt pStart, pEnd;

5637:   PetscFunctionBegin;
5639:   if (start) {
5640:     PetscAssertPointer(start, 3);
5641:     *start = 0;
5642:   }
5643:   if (end) {
5644:     PetscAssertPointer(end, 4);
5645:     *end = 0;
5646:   }
5647:   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
5648:   if (pStart == pEnd) PetscFunctionReturn(PETSC_SUCCESS);
5649:   if (depth < 0) {
5650:     if (start) *start = pStart;
5651:     if (end) *end = pEnd;
5652:     PetscFunctionReturn(PETSC_SUCCESS);
5653:   }
5654:   if (mesh->tr) {
5655:     PetscCall(DMPlexTransformGetDepthStratum(mesh->tr, depth, start, end));
5656:   } else {
5657:     PetscCall(DMPlexGetDepthLabel(dm, &label));
5658:     PetscCheck(label, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
5659:     PetscCall(DMLabelGetStratumBounds(label, depth, start, end));
5660:   }
5661:   PetscFunctionReturn(PETSC_SUCCESS);
5662: }

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

5667:   Not Collective

5669:   Input Parameters:
5670: + dm     - The `DMPLEX` object
5671: - height - The requested height

5673:   Output Parameters:
5674: + start - The first point at this `height`
5675: - end   - One beyond the last point at this `height`

5677:   Level: developer

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

5684: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetCellTypeStratum()`, `DMPlexGetDepth()`, `DMPlexGetPointHeight()`
5685: @*/
5686: PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt height, PeOp PetscInt *start, PeOp PetscInt *end)
5687: {
5688:   DMLabel  label;
5689:   PetscInt depth, pStart, pEnd;

5691:   PetscFunctionBegin;
5693:   if (start) {
5694:     PetscAssertPointer(start, 3);
5695:     *start = 0;
5696:   }
5697:   if (end) {
5698:     PetscAssertPointer(end, 4);
5699:     *end = 0;
5700:   }
5701:   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
5702:   if (pStart == pEnd) PetscFunctionReturn(PETSC_SUCCESS);
5703:   if (height < 0) {
5704:     if (start) *start = pStart;
5705:     if (end) *end = pEnd;
5706:     PetscFunctionReturn(PETSC_SUCCESS);
5707:   }
5708:   PetscCall(DMPlexGetDepthLabel(dm, &label));
5709:   if (label) PetscCall(DMLabelGetNumValues(label, &depth));
5710:   else PetscCall(DMGetDimension(dm, &depth));
5711:   PetscCheck(depth >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Depth not yet computed");
5712:   PetscCall(DMPlexGetDepthStratum(dm, depth - 1 - height, start, end));
5713:   PetscFunctionReturn(PETSC_SUCCESS);
5714: }

5716: /*@
5717:   DMPlexGetPointDepth - Get the `depth` of a given point

5719:   Not Collective

5721:   Input Parameters:
5722: + dm    - The `DMPLEX` object
5723: - point - The point

5725:   Output Parameter:
5726: . depth - The depth of the `point`

5728:   Level: intermediate

5730: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellType()`, `DMPlexGetDepthLabel()`, `DMPlexGetDepth()`, `DMPlexGetPointHeight()`
5731: @*/
5732: PetscErrorCode DMPlexGetPointDepth(DM dm, PetscInt point, PetscInt *depth)
5733: {
5734:   PetscFunctionBegin;
5736:   PetscAssertPointer(depth, 3);
5737:   PetscCall(DMLabelGetValue(dm->depthLabel, point, depth));
5738:   PetscFunctionReturn(PETSC_SUCCESS);
5739: }

5741: /*@
5742:   DMPlexGetPointHeight - Get the `height` of a given point

5744:   Not Collective

5746:   Input Parameters:
5747: + dm    - The `DMPLEX` object
5748: - point - The point

5750:   Output Parameter:
5751: . height - The height of the `point`

5753:   Level: intermediate

5755: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellType()`, `DMPlexGetDepthLabel()`, `DMPlexGetDepth()`, `DMPlexGetPointDepth()`
5756: @*/
5757: PetscErrorCode DMPlexGetPointHeight(DM dm, PetscInt point, PetscInt *height)
5758: {
5759:   PetscInt n, pDepth;

5761:   PetscFunctionBegin;
5763:   PetscAssertPointer(height, 3);
5764:   PetscCall(DMLabelGetNumValues(dm->depthLabel, &n));
5765:   PetscCall(DMLabelGetValue(dm->depthLabel, point, &pDepth));
5766:   *height = n - 1 - pDepth; /* DAG depth is n-1 */
5767:   PetscFunctionReturn(PETSC_SUCCESS);
5768: }

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

5773:   Not Collective

5775:   Input Parameter:
5776: . dm - The `DMPLEX` object

5778:   Output Parameter:
5779: . celltypeLabel - The `DMLabel` recording cell polytope type

5781:   Level: developer

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

5787: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellType()`, `DMPlexGetDepthLabel()`, `DMCreateLabel()`
5788: @*/
5789: PetscErrorCode DMPlexGetCellTypeLabel(DM dm, DMLabel *celltypeLabel)
5790: {
5791:   PetscFunctionBegin;
5793:   PetscAssertPointer(celltypeLabel, 2);
5794:   if (!dm->celltypeLabel) PetscCall(DMPlexComputeCellTypes(dm));
5795:   *celltypeLabel = dm->celltypeLabel;
5796:   PetscFunctionReturn(PETSC_SUCCESS);
5797: }

5799: /*@
5800:   DMPlexGetCellType - Get the polytope type of a given cell

5802:   Not Collective

5804:   Input Parameters:
5805: + dm   - The `DMPLEX` object
5806: - cell - The cell

5808:   Output Parameter:
5809: . celltype - The polytope type of the cell

5811:   Level: intermediate

5813: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPolytopeType`, `DMPlexGetCellTypeLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetDepth()`
5814: @*/
5815: PetscErrorCode DMPlexGetCellType(DM dm, PetscInt cell, DMPolytopeType *celltype)
5816: {
5817:   DM_Plex *mesh = (DM_Plex *)dm->data;
5818:   DMLabel  label;
5819:   PetscInt ct;

5821:   PetscFunctionBegin;
5823:   PetscAssertPointer(celltype, 3);
5824:   if (mesh->tr) {
5825:     PetscCall(DMPlexTransformGetCellType(mesh->tr, cell, celltype));
5826:   } else {
5827:     PetscInt pStart, pEnd;

5829:     PetscCall(PetscSectionGetChart(mesh->coneSection, &pStart, NULL));
5830:     if (!mesh->cellTypes) { /* XXX remove? optimize? */
5831:       PetscCall(PetscSectionGetChart(mesh->coneSection, NULL, &pEnd));
5832:       if (pEnd <= pStart) {
5833:         *celltype = DM_POLYTOPE_UNKNOWN;
5834:         PetscFunctionReturn(PETSC_SUCCESS);
5835:       }
5836:       PetscCall(PetscMalloc1(pEnd - pStart, &mesh->cellTypes));
5837:       PetscCall(DMPlexGetCellTypeLabel(dm, &label));
5838:       for (PetscInt p = pStart; p < pEnd; p++) {
5839:         PetscCall(DMLabelGetValue(label, p, &ct));
5840:         mesh->cellTypes[p - pStart].value_as_uint8 = (uint8_t)ct;
5841:       }
5842:     }
5843:     *celltype = (DMPolytopeType)mesh->cellTypes[cell - pStart].value_as_uint8;
5844:     if (PetscDefined(USE_DEBUG)) {
5845:       PetscCall(DMPlexGetCellTypeLabel(dm, &label));
5846:       PetscCall(DMLabelGetValue(label, cell, &ct));
5847:       PetscCheck(ct >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cell %" PetscInt_FMT " has not been assigned a cell type", cell);
5848:       PetscCheck(ct == (PetscInt)*celltype, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid cellType for %" PetscInt_FMT ": %d != %" PetscInt_FMT, cell, (int)*celltype, ct);
5849:     }
5850:   }
5851:   PetscFunctionReturn(PETSC_SUCCESS);
5852: }

5854: /*@
5855:   DMPlexSetCellType - Set the polytope type of a given cell

5857:   Not Collective

5859:   Input Parameters:
5860: + dm       - The `DMPLEX` object
5861: . cell     - The cell
5862: - celltype - The polytope type of the cell

5864:   Level: advanced

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

5872: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellTypeLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetDepth()`, `DMPlexComputeCellTypes()`, `DMCreateLabel()`
5873: @*/
5874: PetscErrorCode DMPlexSetCellType(DM dm, PetscInt cell, DMPolytopeType celltype)
5875: {
5876:   DM_Plex *mesh = (DM_Plex *)dm->data;
5877:   DMLabel  label;
5878:   PetscInt pStart, pEnd;

5880:   PetscFunctionBegin;
5882:   PetscCall(PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd));
5883:   PetscCall(DMPlexGetCellTypeLabel(dm, &label));
5884:   PetscCall(DMLabelSetValue(label, cell, celltype));
5885:   if (!mesh->cellTypes) PetscCall(PetscMalloc1(pEnd - pStart, &mesh->cellTypes));
5886:   mesh->cellTypes[cell - pStart].value_as_uint8 = (uint8_t)celltype;
5887:   PetscFunctionReturn(PETSC_SUCCESS);
5888: }

5890: PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
5891: {
5892:   PetscSection section;
5893:   PetscInt     maxHeight;
5894:   const char  *prefix;

5896:   PetscFunctionBegin;
5897:   PetscCall(DMClone(dm, cdm));
5898:   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &prefix));
5899:   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)*cdm, prefix));
5900:   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)*cdm, "cdm_"));
5901:   PetscCall(DMPlexGetMaxProjectionHeight(dm, &maxHeight));
5902:   PetscCall(DMPlexSetMaxProjectionHeight(*cdm, maxHeight));
5903:   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section));
5904:   PetscCall(DMSetLocalSection(*cdm, section));
5905:   PetscCall(PetscSectionDestroy(&section));

5907:   PetscCall(DMSetNumFields(*cdm, 1));
5908:   PetscCall(DMCreateDS(*cdm));
5909:   (*cdm)->cloneOpts = PETSC_TRUE;
5910:   if (dm->setfromoptionscalled) PetscCall(DMSetFromOptions(*cdm));
5911:   PetscFunctionReturn(PETSC_SUCCESS);
5912: }

5914: PetscErrorCode DMCreateCellCoordinateDM_Plex(DM dm, DM *cdm)
5915: {
5916:   DM           cgcdm;
5917:   PetscSection section;
5918:   const char  *prefix;

5920:   PetscFunctionBegin;
5921:   PetscCall(DMGetCoordinateDM(dm, &cgcdm));
5922:   PetscCall(DMClone(cgcdm, cdm));
5923:   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &prefix));
5924:   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)*cdm, prefix));
5925:   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)*cdm, "cellcdm_"));
5926:   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section));
5927:   PetscCall(DMSetLocalSection(*cdm, section));
5928:   PetscCall(PetscSectionDestroy(&section));
5929:   PetscCall(DMSetNumFields(*cdm, 1));
5930:   PetscCall(DMCreateDS(*cdm));
5931:   (*cdm)->cloneOpts = PETSC_TRUE;
5932:   if (dm->setfromoptionscalled) PetscCall(DMSetFromOptions(*cdm));
5933:   PetscFunctionReturn(PETSC_SUCCESS);
5934: }

5936: PetscErrorCode DMCreateCoordinateField_Plex(DM dm, DMField *field)
5937: {
5938:   Vec coordsLocal, cellCoordsLocal;
5939:   DM  coordsDM, cellCoordsDM;

5941:   PetscFunctionBegin;
5942:   *field = NULL;
5943:   PetscCall(DMGetCoordinatesLocal(dm, &coordsLocal));
5944:   PetscCall(DMGetCoordinateDM(dm, &coordsDM));
5945:   PetscCall(DMGetCellCoordinatesLocal(dm, &cellCoordsLocal));
5946:   PetscCall(DMGetCellCoordinateDM(dm, &cellCoordsDM));
5947:   if (coordsLocal && coordsDM) {
5948:     if (cellCoordsLocal && cellCoordsDM) PetscCall(DMFieldCreateDSWithDG(coordsDM, cellCoordsDM, 0, coordsLocal, cellCoordsLocal, field));
5949:     else PetscCall(DMFieldCreateDS(coordsDM, 0, coordsLocal, field));
5950:   }
5951:   PetscFunctionReturn(PETSC_SUCCESS);
5952: }

5954: /*@
5955:   DMPlexGetConeSection - Return a section which describes the layout of cone data

5957:   Not Collective

5959:   Input Parameter:
5960: . dm - The `DMPLEX` object

5962:   Output Parameter:
5963: . section - The `PetscSection` object

5965:   Level: developer

5967: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSupportSection()`, `DMPlexGetCones()`, `DMPlexGetConeOrientations()`, `PetscSection`
5968: @*/
5969: PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
5970: {
5971:   DM_Plex *mesh = (DM_Plex *)dm->data;

5973:   PetscFunctionBegin;
5975:   if (section) *section = mesh->coneSection;
5976:   PetscFunctionReturn(PETSC_SUCCESS);
5977: }

5979: /*@
5980:   DMPlexGetSupportSection - Return a section which describes the layout of support data

5982:   Not Collective

5984:   Input Parameter:
5985: . dm - The `DMPLEX` object

5987:   Output Parameter:
5988: . section - The `PetscSection` object

5990:   Level: developer

5992: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetConeSection()`, `PetscSection`
5993: @*/
5994: PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
5995: {
5996:   DM_Plex *mesh = (DM_Plex *)dm->data;

5998:   PetscFunctionBegin;
6000:   if (section) *section = mesh->supportSection;
6001:   PetscFunctionReturn(PETSC_SUCCESS);
6002: }

6004: /*@
6005:   DMPlexGetCones - Return cone data

6007:   Not Collective

6009:   Input Parameter:
6010: . dm - The `DMPLEX` object

6012:   Output Parameter:
6013: . cones - The cone for each point

6015:   Level: developer

6017: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetConeSection()`
6018: @*/
6019: PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
6020: {
6021:   DM_Plex *mesh = (DM_Plex *)dm->data;

6023:   PetscFunctionBegin;
6025:   if (cones) *cones = mesh->cones;
6026:   PetscFunctionReturn(PETSC_SUCCESS);
6027: }

6029: /*@
6030:   DMPlexGetConeOrientations - Return cone orientation data

6032:   Not Collective

6034:   Input Parameter:
6035: . dm - The `DMPLEX` object

6037:   Output Parameter:
6038: . coneOrientations - The array of cone orientations for all points

6040:   Level: developer

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

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

6048: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetConeSection()`, `DMPlexGetConeOrientation()`, `PetscSection`
6049: @*/
6050: PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
6051: {
6052:   DM_Plex *mesh = (DM_Plex *)dm->data;

6054:   PetscFunctionBegin;
6056:   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
6057:   PetscFunctionReturn(PETSC_SUCCESS);
6058: }

6060: /* FEM Support */

6062: PetscErrorCode DMPlexGetAllCells_Internal(DM plex, IS *cellIS)
6063: {
6064:   PetscInt depth;

6066:   PetscFunctionBegin;
6067:   PetscCall(DMPlexGetDepth(plex, &depth));
6068:   PetscCall(DMGetStratumIS(plex, "dim", depth, cellIS));
6069:   if (!*cellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, cellIS));
6070:   PetscFunctionReturn(PETSC_SUCCESS);
6071: }

6073: PetscErrorCode DMPlexGetAllFaces_Internal(DM plex, IS *faceIS)
6074: {
6075:   PetscInt depth;

6077:   PetscFunctionBegin;
6078:   PetscCall(DMPlexGetDepth(plex, &depth));
6079:   PetscCall(DMGetStratumIS(plex, "dim", depth - 1, faceIS));
6080:   if (!*faceIS) PetscCall(DMGetStratumIS(plex, "depth", depth - 1, faceIS));
6081:   PetscFunctionReturn(PETSC_SUCCESS);
6082: }

6084: /*
6085:  Returns number of components and tensor degree for the field.  For interpolated meshes, line should be a point
6086:  representing a line in the section.
6087: */
6088: static PetscErrorCode PetscSectionFieldGetTensorDegree_Private(DM dm, PetscSection section, PetscInt field, PetscInt line, PetscInt *Nc, PetscInt *k, PetscBool *continuous, PetscBool *tensor)
6089: {
6090:   PetscObject  obj;
6091:   PetscClassId id;
6092:   PetscFE      fe = NULL;

6094:   PetscFunctionBeginHot;
6095:   PetscCall(PetscSectionGetFieldComponents(section, field, Nc));
6096:   PetscCall(DMGetField(dm, field, NULL, &obj));
6097:   PetscCall(PetscObjectGetClassId(obj, &id));
6098:   if (id == PETSCFE_CLASSID) fe = (PetscFE)obj;

6100:   if (!fe) {
6101:     /* Assume the full interpolated mesh is in the chart; lines in particular */
6102:     /* An order k SEM disc has k-1 dofs on an edge */
6103:     PetscCall(PetscSectionGetFieldDof(section, line, field, k));
6104:     *k = *k / *Nc + 1;
6105:   } else {
6106:     PetscInt       dual_space_size, dim;
6107:     PetscDualSpace dsp;

6109:     PetscCall(DMGetDimension(dm, &dim));
6110:     PetscCall(PetscFEGetDualSpace(fe, &dsp));
6111:     PetscCall(PetscDualSpaceGetDimension(dsp, &dual_space_size));
6112:     PetscCall(PetscDualSpaceLagrangeGetContinuity(dsp, continuous));
6113:     PetscCall(PetscDualSpaceLagrangeGetTensor(dsp, tensor));
6114:     if (*tensor) {
6115:       *k = (PetscInt)PetscCeilReal(PetscPowReal(dual_space_size / *Nc, 1.0 / dim)) - 1;
6116:     } else {
6117:       switch (dim) {
6118:       case 1:
6119:         *k = (dual_space_size / *Nc) - 1;
6120:         break;
6121:       case 2:
6122:         // N = (k + 1) (k + 2) / 2, k^2 + 3 k - 2 (N - 1) = 0, k = (sqrt(8 N + 1) - 3) / 2
6123:         *k = (PetscInt)PetscCeilReal((PetscSqrtReal(8 * dual_space_size / *Nc + 1) - 3) / 2);
6124:         break;
6125:       case 3: {
6126:         // N = (k + 1) (k + 2) (k + 3) / 6, k = (sqrt(3) sqrt(243 N^2 - 1) + 27 N)^(1/3)/3^(2/3) + 1/(3^(1/3) (sqrt(3) sqrt(243 N^2 - 1) + 27 N)^(1/3)) - 2
6127:         PetscInt N = dual_space_size / *Nc;
6128:         *k         = (PetscInt)PetscCeilReal(PetscPowReal((PetscSqrtReal(3 * (243 * N * N - 1)) + 27 * N) / 9, 1.0 / 3.0) + 1 / PetscPowReal(3 * (PetscSqrtReal(3 * (243 * N * N - 1)) + 27 * N), 1.0 / 3.0)) - 2;
6129:       } break;
6130:       default:
6131:         *k = -1;
6132:       }
6133:     }
6134:   }
6135:   PetscFunctionReturn(PETSC_SUCCESS);
6136: }

6138: static PetscErrorCode GetFieldSize_Private(PetscInt dim, PetscInt k, PetscBool tensor, PetscInt *dof)
6139: {
6140:   PetscFunctionBeginHot;
6141:   if (tensor) {
6142:     *dof = PetscPowInt(k + 1, dim);
6143:   } else {
6144:     switch (dim) {
6145:     case 1:
6146:       *dof = k + 1;
6147:       break;
6148:     case 2:
6149:       *dof = ((k + 1) * (k + 2)) / 2;
6150:       break;
6151:     case 3:
6152:       *dof = ((k + 1) * (k + 2) * (k + 3)) / 6;
6153:       break;
6154:     default:
6155:       *dof = 0;
6156:     }
6157:   }
6158:   PetscFunctionReturn(PETSC_SUCCESS);
6159: }

6161: /*@
6162:   DMPlexSetClosurePermutationTensor - Create a permutation from the default (BFS) point ordering in the closure, to a
6163:   lexicographic ordering over the tensor product cell (i.e., line, quad, hex, etc.), and set this permutation in the
6164:   section provided (or the section of the `DM`).

6166:   Not Collective

6168:   Input Parameters:
6169: + dm      - The `DM`
6170: . point   - Either a cell (highest dim point) or an edge (dim 1 point), or `PETSC_DETERMINE`
6171: - section - The `PetscSection` to reorder, or `NULL` for the default section

6173:   Example:
6174:   A typical interpolated single-quad mesh might order points as
6175: .vb
6176:   [c0, v1, v2, v3, v4, e5, e6, e7, e8]

6178:   v4 -- e6 -- v3
6179:   |           |
6180:   e7    c0    e8
6181:   |           |
6182:   v1 -- e5 -- v2
6183: .ve

6185:   (There is no significance to the ordering described here.)  The default section for a Q3 quad might typically assign
6186:   dofs in the order of points, e.g.,
6187: .vb
6188:     c0 -> [0,1,2,3]
6189:     v1 -> [4]
6190:     ...
6191:     e5 -> [8, 9]
6192: .ve

6194:   which corresponds to the dofs
6195: .vb
6196:     6   10  11  7
6197:     13  2   3   15
6198:     12  0   1   14
6199:     4   8   9   5
6200: .ve

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

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

6212:   Level: developer

6214:   Notes:
6215:   The point is used to determine the number of dofs/field on an edge. For SEM, this is related to the polynomial
6216:   degree of the basis.

6218:   This is required to run with libCEED.

6220: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMGetLocalSection()`, `PetscSectionSetClosurePermutation()`, `DMPlexSetClosurePermutationLexicographic()`, `DMSetGlobalSection()`
6221: @*/
6222: PetscErrorCode DMPlexSetClosurePermutationTensor(DM dm, PetscInt point, PetscSection section)
6223: {
6224:   DMLabel   label;
6225:   PetscInt  dim, depth = -1, eStart = -1, Nf;
6226:   PetscBool continuous = PETSC_TRUE, tensor = PETSC_TRUE;

6228:   PetscFunctionBegin;
6231:   PetscCall(DMGetDimension(dm, &dim));
6232:   if (dim < 1) PetscFunctionReturn(PETSC_SUCCESS);
6233:   if (point < 0) {
6234:     PetscInt sStart, sEnd;

6236:     PetscCall(DMPlexGetDepthStratum(dm, 1, &sStart, &sEnd));
6237:     point = sEnd - sStart ? sStart : point;
6238:   }
6239:   PetscCall(DMPlexGetDepthLabel(dm, &label));
6240:   if (point >= 0) PetscCall(DMLabelGetValue(label, point, &depth));
6241:   if (!section) PetscCall(DMGetLocalSection(dm, &section));
6242:   if (depth == 1) {
6243:     eStart = point;
6244:   } else if (depth == dim) {
6245:     const PetscInt *cone;

6247:     PetscCall(DMPlexGetCone(dm, point, &cone));
6248:     if (dim == 2) eStart = cone[0];
6249:     else if (dim == 3) {
6250:       const PetscInt *cone2;
6251:       PetscCall(DMPlexGetCone(dm, cone[0], &cone2));
6252:       eStart = cone2[0];
6253:     } 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);
6254:   } else PetscCheck(depth < 0, 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);

6256:   PetscCall(PetscSectionGetNumFields(section, &Nf));
6257:   for (PetscInt d = 1; d <= dim; d++) {
6258:     PetscInt  k, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
6259:     PetscInt *perm;

6261:     for (f = 0; f < Nf; ++f) {
6262:       PetscInt dof;

6264:       PetscCall(PetscSectionFieldGetTensorDegree_Private(dm, section, f, eStart, &Nc, &k, &continuous, &tensor));
6265:       PetscCheck(dim == 1 || tensor || !continuous, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Continuous field %" PetscInt_FMT " must have a tensor product discretization", f);
6266:       if (!continuous && d < dim) continue;
6267:       PetscCall(GetFieldSize_Private(d, k, tensor, &dof));
6268:       size += dof * Nc;
6269:     }
6270:     PetscCall(PetscMalloc1(size, &perm));
6271:     for (f = 0; f < Nf; ++f) {
6272:       switch (d) {
6273:       case 1:
6274:         PetscCall(PetscSectionFieldGetTensorDegree_Private(dm, section, f, eStart, &Nc, &k, &continuous, &tensor));
6275:         if (!continuous && d < dim) continue;
6276:         /*
6277:          Original ordering is [ edge of length k-1; vtx0; vtx1 ]
6278:          We want              [ vtx0; edge of length k-1; vtx1 ]
6279:          */
6280:         if (continuous) {
6281:           for (c = 0; c < Nc; c++, offset++) perm[offset] = (k - 1) * Nc + c + foffset;
6282:           for (i = 0; i < k - 1; i++)
6283:             for (c = 0; c < Nc; c++, offset++) perm[offset] = i * Nc + c + foffset;
6284:           for (c = 0; c < Nc; c++, offset++) perm[offset] = k * Nc + c + foffset;
6285:           foffset = offset;
6286:         } else {
6287:           PetscInt dof;

6289:           PetscCall(GetFieldSize_Private(d, k, tensor, &dof));
6290:           for (i = 0; i < dof * Nc; ++i, ++offset) perm[offset] = i + foffset;
6291:           foffset = offset;
6292:         }
6293:         break;
6294:       case 2:
6295:         /* The original quad closure is oriented clockwise, {f, e_b, e_r, e_t, e_l, v_lb, v_rb, v_tr, v_tl} */
6296:         PetscCall(PetscSectionFieldGetTensorDegree_Private(dm, section, f, eStart, &Nc, &k, &continuous, &tensor));
6297:         if (!continuous && d < dim) continue;
6298:         /* The SEM order is

6300:          v_lb, {e_b}, v_rb,
6301:          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
6302:          v_lt, reverse {e_t}, v_rt
6303:          */
6304:         if (continuous) {
6305:           const PetscInt of   = 0;
6306:           const PetscInt oeb  = of + PetscSqr(k - 1);
6307:           const PetscInt oer  = oeb + (k - 1);
6308:           const PetscInt oet  = oer + (k - 1);
6309:           const PetscInt oel  = oet + (k - 1);
6310:           const PetscInt ovlb = oel + (k - 1);
6311:           const PetscInt ovrb = ovlb + 1;
6312:           const PetscInt ovrt = ovrb + 1;
6313:           const PetscInt ovlt = ovrt + 1;
6314:           PetscInt       o;

6316:           /* bottom */
6317:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb * Nc + c + foffset;
6318:           for (o = oeb; o < oer; ++o)
6319:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6320:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb * Nc + c + foffset;
6321:           /* middle */
6322:           for (i = 0; i < k - 1; ++i) {
6323:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel + (k - 2) - i) * Nc + c + foffset;
6324:             for (o = of + (k - 1) * i; o < of + (k - 1) * (i + 1); ++o)
6325:               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6326:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer + i) * Nc + c + foffset;
6327:           }
6328:           /* top */
6329:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt * Nc + c + foffset;
6330:           for (o = oel - 1; o >= oet; --o)
6331:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6332:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt * Nc + c + foffset;
6333:           foffset = offset;
6334:         } else {
6335:           PetscInt dof;

6337:           PetscCall(GetFieldSize_Private(d, k, tensor, &dof));
6338:           for (i = 0; i < dof * Nc; ++i, ++offset) perm[offset] = i + foffset;
6339:           foffset = offset;
6340:         }
6341:         break;
6342:       case 3:
6343:         /* The original hex closure is

6345:          {c,
6346:          f_b, f_t, f_f, f_b, f_r, f_l,
6347:          e_bl, e_bb, e_br, e_bf,  e_tf, e_tr, e_tb, e_tl,  e_rf, e_lf, e_lb, e_rb,
6348:          v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
6349:          */
6350:         PetscCall(PetscSectionFieldGetTensorDegree_Private(dm, section, f, eStart, &Nc, &k, &continuous, &tensor));
6351:         if (!continuous && d < dim) continue;
6352:         /* The SEM order is
6353:          Bottom Slice
6354:          v_blf, {e^{(k-1)-n}_bf}, v_brf,
6355:          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
6356:          v_blb, {e_bb}, v_brb,

6358:          Middle Slice (j)
6359:          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
6360:          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
6361:          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,

6363:          Top Slice
6364:          v_tlf, {e_tf}, v_trf,
6365:          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
6366:          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
6367:          */
6368:         if (continuous) {
6369:           const PetscInt oc    = 0;
6370:           const PetscInt ofb   = oc + PetscSqr(k - 1) * (k - 1);
6371:           const PetscInt oft   = ofb + PetscSqr(k - 1);
6372:           const PetscInt off   = oft + PetscSqr(k - 1);
6373:           const PetscInt ofk   = off + PetscSqr(k - 1);
6374:           const PetscInt ofr   = ofk + PetscSqr(k - 1);
6375:           const PetscInt ofl   = ofr + PetscSqr(k - 1);
6376:           const PetscInt oebl  = ofl + PetscSqr(k - 1);
6377:           const PetscInt oebb  = oebl + (k - 1);
6378:           const PetscInt oebr  = oebb + (k - 1);
6379:           const PetscInt oebf  = oebr + (k - 1);
6380:           const PetscInt oetf  = oebf + (k - 1);
6381:           const PetscInt oetr  = oetf + (k - 1);
6382:           const PetscInt oetb  = oetr + (k - 1);
6383:           const PetscInt oetl  = oetb + (k - 1);
6384:           const PetscInt oerf  = oetl + (k - 1);
6385:           const PetscInt oelf  = oerf + (k - 1);
6386:           const PetscInt oelb  = oelf + (k - 1);
6387:           const PetscInt oerb  = oelb + (k - 1);
6388:           const PetscInt ovblf = oerb + (k - 1);
6389:           const PetscInt ovblb = ovblf + 1;
6390:           const PetscInt ovbrb = ovblb + 1;
6391:           const PetscInt ovbrf = ovbrb + 1;
6392:           const PetscInt ovtlf = ovbrf + 1;
6393:           const PetscInt ovtrf = ovtlf + 1;
6394:           const PetscInt ovtrb = ovtrf + 1;
6395:           const PetscInt ovtlb = ovtrb + 1;
6396:           PetscInt       o, n;

6398:           /* Bottom Slice */
6399:           /*   bottom */
6400:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf * Nc + c + foffset;
6401:           for (o = oetf - 1; o >= oebf; --o)
6402:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6403:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf * Nc + c + foffset;
6404:           /*   middle */
6405:           for (i = 0; i < k - 1; ++i) {
6406:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl + i) * Nc + c + foffset;
6407:             for (n = 0; n < k - 1; ++n) {
6408:               o = ofb + n * (k - 1) + i;
6409:               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6410:             }
6411:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr + (k - 2) - i) * Nc + c + foffset;
6412:           }
6413:           /*   top */
6414:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb * Nc + c + foffset;
6415:           for (o = oebb; o < oebr; ++o)
6416:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6417:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb * Nc + c + foffset;

6419:           /* Middle Slice */
6420:           for (j = 0; j < k - 1; ++j) {
6421:             /*   bottom */
6422:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf + (k - 2) - j) * Nc + c + foffset;
6423:             for (o = off + j * (k - 1); o < off + (j + 1) * (k - 1); ++o)
6424:               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6425:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf + j) * Nc + c + foffset;
6426:             /*   middle */
6427:             for (i = 0; i < k - 1; ++i) {
6428:               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl + i * (k - 1) + j) * Nc + c + foffset;
6429:               for (n = 0; n < k - 1; ++n)
6430:                 for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oc + (j * (k - 1) + i) * (k - 1) + n) * Nc + c + foffset;
6431:               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr + j * (k - 1) + i) * Nc + c + foffset;
6432:             }
6433:             /*   top */
6434:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb + j) * Nc + c + foffset;
6435:             for (o = ofk + j * (k - 1) + (k - 2); o >= ofk + j * (k - 1); --o)
6436:               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6437:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb + (k - 2) - j) * Nc + c + foffset;
6438:           }

6440:           /* Top Slice */
6441:           /*   bottom */
6442:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf * Nc + c + foffset;
6443:           for (o = oetf; o < oetr; ++o)
6444:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6445:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf * Nc + c + foffset;
6446:           /*   middle */
6447:           for (i = 0; i < k - 1; ++i) {
6448:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl + (k - 2) - i) * Nc + c + foffset;
6449:             for (n = 0; n < k - 1; ++n)
6450:               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oft + i * (k - 1) + n) * Nc + c + foffset;
6451:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr + i) * Nc + c + foffset;
6452:           }
6453:           /*   top */
6454:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb * Nc + c + foffset;
6455:           for (o = oetl - 1; o >= oetb; --o)
6456:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6457:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb * Nc + c + foffset;

6459:           foffset = offset;
6460:         } else {
6461:           PetscInt dof;

6463:           PetscCall(GetFieldSize_Private(d, k, tensor, &dof));
6464:           for (i = 0; i < dof * Nc; ++i, ++offset) perm[offset] = i + foffset;
6465:           foffset = offset;
6466:         }
6467:         break;
6468:       default:
6469:         SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %" PetscInt_FMT, d);
6470:       }
6471:     }
6472:     PetscCheck(offset == size, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Number of permutation entries %" PetscInt_FMT " != %" PetscInt_FMT, offset, size);
6473:     /* Check permutation */
6474:     {
6475:       PetscInt *check;

6477:       PetscCall(PetscMalloc1(size, &check));
6478:       for (i = 0; i < size; ++i) {
6479:         check[i] = -1;
6480:         PetscCheck(perm[i] >= 0 && perm[i] < size, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid permutation index p[%" PetscInt_FMT "] = %" PetscInt_FMT, i, perm[i]);
6481:       }
6482:       for (i = 0; i < size; ++i) check[perm[i]] = i;
6483:       for (i = 0; i < size; ++i) PetscCheck(check[i] >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Missing permutation index %" PetscInt_FMT, i);
6484:       PetscCall(PetscFree(check));
6485:     }
6486:     PetscCall(PetscSectionSetClosurePermutation_Internal(section, (PetscObject)dm, d, size, PETSC_OWN_POINTER, perm));
6487:     if (d == dim) { // Add permutation for localized (in case this is a coordinate DM)
6488:       PetscInt *loc_perm;
6489:       PetscCall(PetscMalloc1(size * 2, &loc_perm));
6490:       for (PetscInt i = 0; i < size; i++) {
6491:         loc_perm[i]        = perm[i];
6492:         loc_perm[size + i] = size + perm[i];
6493:       }
6494:       PetscCall(PetscSectionSetClosurePermutation_Internal(section, (PetscObject)dm, d, size * 2, PETSC_OWN_POINTER, loc_perm));
6495:     }
6496:   }
6497:   PetscFunctionReturn(PETSC_SUCCESS);
6498: }

6500: /*@
6501:   DMPlexSetClosurePermutationLexicographic - Create a permutation from the default (BFS) point ordering in the closure, to a
6502:   lexicographic ordering over the simplex (i.e., line, tri, tet, etc.), and set this permutation in the
6503:   section provided (or the section of the `DM`).

6505:   Not Collective

6507:   Input Parameters:
6508: + dm      - The `DM`
6509: . point   - Either a cell (highest dim point) or an edge (dim 1 point), or `PETSC_DETERMINE`
6510: - section - The `PetscSection` to reorder, or `NULL` for the default section

6512:   Example:
6513:   A typical interpolated single-tri mesh might order points as
6514: .vb
6515:   [c0, v1, v2, v3, e4, e5, e6]

6517:   v3
6518:   |  \
6519:   |    \
6520:   e6    e5
6521:   |  c0   \
6522:   |         \
6523:   v1 -- e4 -- v2
6524: .ve

6526:   (There is no significance to the ordering described here.)  The default section for a P3 tri might typically assign
6527:   dofs in the order of points, e.g.,
6528: .vb
6529:     c0 -> [0]
6530:     v1 -> [1]
6531:     ...
6532:     e4 -> [4, 5]
6533: .ve

6535:   which corresponds to the dofs
6536: .vb
6537:     3
6538:     8  7
6539:     9  0   6
6540:     1  4   5   2
6541: .ve

6543:   The closure in BFS ordering works through height strata (cells, edges, vertices) to produce the ordering
6544: .vb
6545:   0 4 5 6 7 8 9 1 2 3
6546: .ve

6548:   After calling DMPlexSetClosurePermutationLexicographic(), the closure will be ordered lexicographically,
6549: .vb
6550:    1 4 5 2 9 0 6 8 7 3
6551: .ve

6553:   Level: developer

6555:   Notes:
6556:   The point is used to determine the number of dofs/field on an edge. For SEM, this is related to the polynomial
6557:   degree of the basis.

6559:   The lexicographic order starts along the left edge, not the front, to match codes like GMsh with a bottom face oriented into the volume.

6561: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMGetLocalSection()`, `PetscSectionSetClosurePermutation()`, `DMPlexSetClosurePermutationTensor()`, `DMSetGlobalSection()`
6562: @*/
6563: PetscErrorCode DMPlexSetClosurePermutationLexicographic(DM dm, PetscInt point, PetscSection section)
6564: {
6565:   DMLabel   label;
6566:   PetscInt  dim, depth = -1, eStart = -1, Nf;
6567:   PetscBool continuous = PETSC_TRUE, tensor = PETSC_TRUE;

6569:   PetscFunctionBegin;
6572:   // TODO: This needs to be tested for P4-6 using Plex ex3 with a linear field to check the permutations
6573:   PetscCall(DMGetDimension(dm, &dim));
6574:   if (dim < 1) PetscFunctionReturn(PETSC_SUCCESS);
6575:   if (point < 0) {
6576:     PetscInt sStart, sEnd;

6578:     PetscCall(DMPlexGetDepthStratum(dm, 1, &sStart, &sEnd));
6579:     point = sEnd - sStart ? sStart : point;
6580:   }
6581:   PetscCall(DMPlexGetDepthLabel(dm, &label));
6582:   if (point >= 0) PetscCall(DMLabelGetValue(label, point, &depth));
6583:   if (!section) PetscCall(DMGetLocalSection(dm, &section));
6584:   if (depth == 1) {
6585:     eStart = point;
6586:   } else if (depth == dim) {
6587:     const PetscInt *cone;

6589:     PetscCall(DMPlexGetCone(dm, point, &cone));
6590:     if (dim == 2) eStart = cone[0];
6591:     else if (dim == 3) {
6592:       const PetscInt *cone2;
6593:       PetscCall(DMPlexGetCone(dm, cone[0], &cone2));
6594:       eStart = cone2[0];
6595:     } 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);
6596:   } else PetscCheck(depth < 0, 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);

6598:   PetscCall(PetscSectionGetNumFields(section, &Nf));
6599:   for (PetscInt d = 1; d <= dim; d++) {
6600:     PetscInt  k, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
6601:     PetscInt *perm;

6603:     for (f = 0; f < Nf; ++f) {
6604:       PetscInt dof;

6606:       PetscCall(PetscSectionFieldGetTensorDegree_Private(dm, section, f, eStart, &Nc, &k, &continuous, &tensor));
6607:       PetscCheck(dim == 1 || !tensor, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Field %" PetscInt_FMT " should not have a tensor product discretization", f);
6608:       if (!continuous && d < dim) continue;
6609:       PetscCall(GetFieldSize_Private(d, k, tensor, &dof));
6610:       size += dof * Nc;
6611:     }
6612:     PetscCall(PetscMalloc1(size, &perm));
6613:     for (f = 0; f < Nf; ++f) {
6614:       switch (d) {
6615:       case 1:
6616:         PetscCall(PetscSectionFieldGetTensorDegree_Private(dm, section, f, eStart, &Nc, &k, &continuous, &tensor));
6617:         if (!continuous && d < dim) continue;
6618:         /*
6619:          Original ordering is [ edge of length k-1; vtx0; vtx1 ]
6620:          We want              [ vtx0; edge of length k-1; vtx1 ]
6621:          */
6622:         if (continuous) {
6623:           for (c = 0; c < Nc; c++, offset++) perm[offset] = (k - 1) * Nc + c + foffset;
6624:           for (i = 0; i < k - 1; i++)
6625:             for (c = 0; c < Nc; c++, offset++) perm[offset] = i * Nc + c + foffset;
6626:           for (c = 0; c < Nc; c++, offset++) perm[offset] = k * Nc + c + foffset;
6627:           foffset = offset;
6628:         } else {
6629:           PetscInt dof;

6631:           PetscCall(GetFieldSize_Private(d, k, tensor, &dof));
6632:           for (i = 0; i < dof * Nc; ++i, ++offset) perm[offset] = i + foffset;
6633:           foffset = offset;
6634:         }
6635:         break;
6636:       case 2:
6637:         /* The original tri closure is oriented clockwise, {f, e_b, e_r, e_l, v_lb, v_rb, v_lt} */
6638:         PetscCall(PetscSectionFieldGetTensorDegree_Private(dm, section, f, eStart, &Nc, &k, &continuous, &tensor));
6639:         if (!continuous && d < dim) continue;
6640:         /* The SEM order is

6642:          v_lb, {e_b}, v_rb,
6643:          e^{(k-1)-i}_l, {f^{i*(k-1-i)}}, e^i_r,
6644:          v_lt
6645:          */
6646:         if (continuous) {
6647:           const PetscInt of   = 0;
6648:           const PetscInt oeb  = of + (k - 2) * (k - 1) / 2;
6649:           const PetscInt oer  = oeb + (k - 1);
6650:           const PetscInt oel  = oer + (k - 1);
6651:           const PetscInt ovlb = oel + (k - 1);
6652:           const PetscInt ovrb = ovlb + 1;
6653:           const PetscInt ovlt = ovrb + 1;
6654:           PetscInt       o;

6656:           /* bottom */
6657:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb * Nc + c + foffset;
6658:           for (o = oeb; o < oer; ++o)
6659:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6660:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb * Nc + c + foffset;
6661:           /* middle */
6662:           for (i = 0; i < k - 1; ++i) {
6663:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel + (k - 2) - i) * Nc + c + foffset;
6664:             // (k - 2) (k - 1) / 2 - (k - 2 - i) (k - 1 - i) / 2 = i (2 k - 3 - i) / 2
6665:             for (o = of + i * (2 * k - 3 - i) / 2; o < of + (i + 1) * (2 * k - 4 - i) / 2; ++o)
6666:               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6667:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer + i) * Nc + c + foffset;
6668:           }
6669:           /* top */
6670:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt * Nc + c + foffset;
6671:           foffset = offset;
6672:         } else {
6673:           PetscInt dof;

6675:           PetscCall(GetFieldSize_Private(d, k, tensor, &dof));
6676:           for (i = 0; i < dof * Nc; ++i, ++offset) perm[offset] = i + foffset;
6677:           foffset = offset;
6678:         }
6679:         break;
6680:       case 3:
6681:         /* The original tet closure is

6683:          {c,
6684:          f_b, f_l, f_f, f_r,
6685:          e_bl, e_br, e_bf,  e_lf, e_rf, e_rb,
6686:          v_blf, v_blb, v_brf, v_tlf}
6687:          */
6688:         PetscCall(PetscSectionFieldGetTensorDegree_Private(dm, section, f, eStart, &Nc, &k, &continuous, &tensor));
6689:         if (!continuous && d < dim) continue;
6690:         /* The SEM order (starting on the left edge since GMsh flips the bottom face) is
6691:          Bottom Slice
6692:          v_blb, {e^{-n}_bl}, v_blf,
6693:          e^{i}_br, f^{i,-n}_b, e^{-i}_bf,
6694:          v_brf,

6696:          Middle Slice (j)
6697:          e^{-j}_rb, {f^{-n,j}_l}, e^{j}_lf,
6698:          f^{-n,j}_r, {c^{j,-n,i}}, f^{j,i}_f,
6699:          e^{j}_rf,

6701:          Top Slice
6702:          v_tlf,
6703:          */
6704:         if (continuous) {
6705:           const PetscInt oc    = 0;
6706:           const PetscInt ofb   = oc + (k - 3) * (k - 2) * (k - 1) / 6;
6707:           const PetscInt ofl   = ofb + (k - 2) * (k - 1) / 2;
6708:           const PetscInt off   = ofl + (k - 2) * (k - 1) / 2;
6709:           const PetscInt ofr   = off + (k - 2) * (k - 1) / 2;
6710:           const PetscInt oebl  = ofr + (k - 2) * (k - 1) / 2;
6711:           const PetscInt oebr  = oebl + (k - 1);
6712:           const PetscInt oebf  = oebr + (k - 1);
6713:           const PetscInt oelf  = oebf + (k - 1);
6714:           const PetscInt oerb  = oelf + (k - 1);
6715:           const PetscInt oerf  = oerb + (k - 1);
6716:           const PetscInt ovblf = oerf + (k - 1);
6717:           const PetscInt ovblb = ovblf + 1;
6718:           const PetscInt ovbrf = ovblb + 1;
6719:           const PetscInt ovtlf = ovbrf + 1;
6720:           PetscInt       o;

6722:           /* Bottom Slice */
6723:           /*   bottom */
6724:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb * Nc + c + foffset;
6725:           for (o = oebr - 1; o >= oebl; --o)
6726:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6727:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf * Nc + c + foffset;
6728:           /*   middle */
6729:           for (i = 0; i < k - 1; ++i) {
6730:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr + i) * Nc + c + foffset;
6731:             for (PetscInt n = 0; n < k - 2 - i; ++n) {
6732:               // (k - 2) (k - 1) / 2 - (k - 2 - i) (k - 1 - i) / 2 = i (2 k - 3 - i) / 2
6733:               o = ofb + i * (2 * k - 3 - i) / 2 + (k - 2 - i - 1 - n);
6734:               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6735:             }
6736:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebf + (k - 2) - i) * Nc + c + foffset;
6737:           }
6738:           /*   top */
6739:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf * Nc + c + foffset;

6741:           /* Middle Slice */
6742:           for (j = 0; j < k - 1; ++j) {
6743:             /*   bottom */
6744:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb + k - 2 - j) * Nc + c + foffset;
6745:             for (PetscInt n = k - 3 - j; n >= 0; --n) {
6746:               // (k - 2) (k - 1) / 2 - (k - 2 - i) (k - 1 - i) / 2 = i (2 k - 3 - i) / 2
6747:               o = ofl + n * (2 * k - 3 - n) / 2 + j;
6748:               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o * Nc + c + foffset;
6749:             }
6750:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf + j) * Nc + c + foffset;
6751:             /*   middle */
6752:             for (i = 0; i < k - 2 - j; ++i) {
6753:               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr + i * (2 * k - 1 - i) / 2 + j) * Nc + c + foffset;
6754:               for (PetscInt n = k - 4 - j - i; n >= 0; --n) {
6755:                 // (k - 2) (k - 1) k / 6 - (k - 2 - j) (k - 1 - j) (k - j) / 6 = j (j^2 - 1 - 3 (k - 1) (j + k - 1)) / 6
6756:                 for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oc + j * (j * j - 1 - 3 * (k - 1) * (j + k - 1)) / 6 + n * (2 * k - 3 - n) + i) * Nc + c + foffset;
6757:               }
6758:               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (off + j * (2 * k - 3 - j) / 2 + i) * Nc + c + foffset;
6759:             }
6760:             /*   top */
6761:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf + j) * Nc + c + foffset;
6762:           }

6764:           /* Top Slice */
6765:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf * Nc + c + foffset;

6767:           foffset = offset;
6768:         } else {
6769:           PetscInt dof;

6771:           PetscCall(GetFieldSize_Private(d, k, tensor, &dof));
6772:           for (i = 0; i < dof * Nc; ++i, ++offset) perm[offset] = i + foffset;
6773:           foffset = offset;
6774:         }
6775:         break;
6776:       default:
6777:         SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %" PetscInt_FMT, d);
6778:       }
6779:     }
6780:     PetscCheck(offset == size, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Number of permutation entries %" PetscInt_FMT " != %" PetscInt_FMT, offset, size);
6781:     /* Check permutation */
6782:     {
6783:       PetscInt *check;

6785:       PetscCall(PetscMalloc1(size, &check));
6786:       for (i = 0; i < size; ++i) {
6787:         check[i] = -1;
6788:         PetscCheck(perm[i] >= 0 && perm[i] < size, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid permutation index p[%" PetscInt_FMT "] = %" PetscInt_FMT, i, perm[i]);
6789:       }
6790:       for (i = 0; i < size; ++i) check[perm[i]] = i;
6791:       for (i = 0; i < size; ++i) PetscCheck(check[i] >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Missing permutation index %" PetscInt_FMT " out of %" PetscInt_FMT, i, size);
6792:       PetscCall(PetscFree(check));
6793:     }
6794:     PetscCall(PetscSectionSetClosurePermutation_Internal(section, (PetscObject)dm, d, size, PETSC_OWN_POINTER, perm));
6795:     if (d == dim) { // Add permutation for localized (in case this is a coordinate DM)
6796:       PetscInt *loc_perm;
6797:       PetscCall(PetscMalloc1(size * 2, &loc_perm));
6798:       for (PetscInt i = 0; i < size; i++) {
6799:         loc_perm[i]        = perm[i];
6800:         loc_perm[size + i] = size + perm[i];
6801:       }
6802:       PetscCall(PetscSectionSetClosurePermutation_Internal(section, (PetscObject)dm, d, size * 2, PETSC_OWN_POINTER, loc_perm));
6803:     }
6804:   }
6805:   PetscFunctionReturn(PETSC_SUCCESS);
6806: }

6808: PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
6809: {
6810:   PetscDS  prob;
6811:   PetscInt depth, Nf, h;
6812:   DMLabel  label;

6814:   PetscFunctionBeginHot;
6815:   PetscCall(DMGetDS(dm, &prob));
6816:   Nf      = prob->Nf;
6817:   label   = dm->depthLabel;
6818:   *dspace = NULL;
6819:   if (field < Nf) {
6820:     PetscObject disc = prob->disc[field];

6822:     if (disc->classid == PETSCFE_CLASSID) {
6823:       PetscDualSpace dsp;

6825:       PetscCall(PetscFEGetDualSpace((PetscFE)disc, &dsp));
6826:       PetscCall(DMLabelGetNumValues(label, &depth));
6827:       PetscCall(DMLabelGetValue(label, point, &h));
6828:       h = depth - 1 - h;
6829:       if (h) PetscCall(PetscDualSpaceGetHeightSubspace(dsp, h, dspace));
6830:       else *dspace = dsp;
6831:     }
6832:   }
6833:   PetscFunctionReturn(PETSC_SUCCESS);
6834: }

6836: static inline PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
6837: {
6838:   PetscScalar       *array;
6839:   const PetscScalar *vArray;
6840:   const PetscInt    *cone, *coneO;
6841:   PetscInt           pStart, pEnd, p, numPoints, size = 0, offset = 0;

6843:   PetscFunctionBeginHot;
6844:   PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
6845:   PetscCall(DMPlexGetConeSize(dm, point, &numPoints));
6846:   PetscCall(DMPlexGetCone(dm, point, &cone));
6847:   PetscCall(DMPlexGetConeOrientation(dm, point, &coneO));
6848:   if (!values || !*values) {
6849:     if (point >= pStart && point < pEnd) {
6850:       PetscInt dof;

6852:       PetscCall(PetscSectionGetDof(section, point, &dof));
6853:       size += dof;
6854:     }
6855:     for (p = 0; p < numPoints; ++p) {
6856:       const PetscInt cp = cone[p];
6857:       PetscInt       dof;

6859:       if (cp < pStart || cp >= pEnd) continue;
6860:       PetscCall(PetscSectionGetDof(section, cp, &dof));
6861:       size += dof;
6862:     }
6863:     if (!values) {
6864:       if (csize) *csize = size;
6865:       PetscFunctionReturn(PETSC_SUCCESS);
6866:     }
6867:     PetscCall(DMGetWorkArray(dm, size, MPIU_SCALAR, &array));
6868:   } else {
6869:     array = *values;
6870:   }
6871:   size = 0;
6872:   PetscCall(VecGetArrayRead(v, &vArray));
6873:   if (point >= pStart && point < pEnd) {
6874:     PetscInt           dof, off, d;
6875:     const PetscScalar *varr;

6877:     PetscCall(PetscSectionGetDof(section, point, &dof));
6878:     PetscCall(PetscSectionGetOffset(section, point, &off));
6879:     varr = PetscSafePointerPlusOffset(vArray, off);
6880:     for (d = 0; d < dof; ++d, ++offset) array[offset] = varr[d];
6881:     size += dof;
6882:   }
6883:   for (p = 0; p < numPoints; ++p) {
6884:     const PetscInt     cp = cone[p];
6885:     PetscInt           o  = coneO[p];
6886:     PetscInt           dof, off, d;
6887:     const PetscScalar *varr;

6889:     if (cp < pStart || cp >= pEnd) continue;
6890:     PetscCall(PetscSectionGetDof(section, cp, &dof));
6891:     PetscCall(PetscSectionGetOffset(section, cp, &off));
6892:     varr = PetscSafePointerPlusOffset(vArray, off);
6893:     if (o >= 0) {
6894:       for (d = 0; d < dof; ++d, ++offset) array[offset] = varr[d];
6895:     } else {
6896:       for (d = dof - 1; d >= 0; --d, ++offset) array[offset] = varr[d];
6897:     }
6898:     size += dof;
6899:   }
6900:   PetscCall(VecRestoreArrayRead(v, &vArray));
6901:   if (!*values) {
6902:     if (csize) *csize = size;
6903:     *values = array;
6904:   } else {
6905:     PetscCheck(size <= *csize, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %" PetscInt_FMT " < actual size %" PetscInt_FMT, *csize, size);
6906:     *csize = size;
6907:   }
6908:   PetscFunctionReturn(PETSC_SUCCESS);
6909: }

6911: /* Compress out points not in the section */
6912: static inline PetscErrorCode CompressPoints_Private(PetscSection section, PetscInt *numPoints, PetscInt points[])
6913: {
6914:   const PetscInt np = *numPoints;
6915:   PetscInt       pStart, pEnd, p, q;

6917:   PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
6918:   for (p = 0, q = 0; p < np; ++p) {
6919:     const PetscInt r = points[p * 2];
6920:     if (r >= pStart && r < pEnd) {
6921:       points[q * 2]     = r;
6922:       points[q * 2 + 1] = points[p * 2 + 1];
6923:       ++q;
6924:     }
6925:   }
6926:   *numPoints = q;
6927:   return PETSC_SUCCESS;
6928: }

6930: /*@
6931:   DMPlexGetCompressedClosure - Return the transitive closure of a point, restricted to points with dof in the given section

6933:   Not Collective

6935:   Input Parameters:
6936: + dm      - The `DMPLEX`
6937: . section - The `PetscSection` used to filter closure points
6938: . point   - The mesh point
6939: - ornt    - The orientation of `point`; when zero the cached closure index (if any) is used directly

6941:   Output Parameters:
6942: + numPoints - Number of closure points that participate in `section`
6943: . points    - Array of `(point, orientation)` pairs; either the cached closure or a work array from `DMPlexGetTransitiveClosure_Internal()`
6944: . clSec     - The section describing the closure index, or `NULL` if none is cached
6945: . clPoints  - The `IS` holding the closure indices, or `NULL` if none is cached
6946: - clp       - Raw pointer into the closure-index `IS` when it is used, or `NULL` otherwise

6948:   Level: developer

6950:   Note:
6951:   This routine does not apply the closure permutation. Release the outputs with `DMPlexRestoreCompressedClosure()`.

6953: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `PetscSection`, `DMPlexRestoreCompressedClosure()`, `DMPlexGetTransitiveClosure()`, `PetscSectionGetClosureIndex()`
6954: @*/
6955: /* Compressed closure does not apply closure permutation */
6956: PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt ornt, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
6957: {
6958:   const PetscInt *cla = NULL;
6959:   PetscInt        np, *pts = NULL;

6961:   PetscFunctionBeginHot;
6962:   PetscCall(PetscSectionGetClosureIndex(section, (PetscObject)dm, clSec, clPoints));
6963:   if (!ornt && *clPoints) {
6964:     PetscInt dof, off;

6966:     PetscCall(PetscSectionGetDof(*clSec, point, &dof));
6967:     PetscCall(PetscSectionGetOffset(*clSec, point, &off));
6968:     PetscCall(ISGetIndices(*clPoints, &cla));
6969:     np  = dof / 2;
6970:     pts = PetscSafePointerPlusOffset((PetscInt *)cla, off);
6971:   } else {
6972:     PetscCall(DMPlexGetTransitiveClosure_Internal(dm, point, ornt, PETSC_TRUE, &np, &pts));
6973:     PetscCall(CompressPoints_Private(section, &np, pts));
6974:   }
6975:   *numPoints = np;
6976:   *points    = pts;
6977:   *clp       = cla;
6978:   PetscFunctionReturn(PETSC_SUCCESS);
6979: }

6981: /*@
6982:   DMPlexRestoreCompressedClosure - Release the arrays returned by `DMPlexGetCompressedClosure()`

6984:   Not Collective

6986:   Input Parameters:
6987: + dm        - The `DMPLEX`
6988: . section   - The `PetscSection` passed to `DMPlexGetCompressedClosure()`
6989: . point     - The mesh point
6990: . numPoints - The number of closure points
6991: . points    - The closure array
6992: . clSec     - The cached closure section
6993: . clPoints  - The cached closure `IS`
6994: - clp       - The raw pointer into the closure index

6996:   Level: developer

6998: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `PetscSection`, `DMPlexGetCompressedClosure()`, `DMPlexRestoreTransitiveClosure()`
6999: @*/
7000: PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
7001: {
7002:   PetscFunctionBeginHot;
7003:   if (!*clPoints) {
7004:     PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points));
7005:   } else {
7006:     PetscCall(ISRestoreIndices(*clPoints, clp));
7007:   }
7008:   *numPoints = 0;
7009:   *points    = NULL;
7010:   *clSec     = NULL;
7011:   *clPoints  = NULL;
7012:   *clp       = NULL;
7013:   PetscFunctionReturn(PETSC_SUCCESS);
7014: }

7016: static inline PetscErrorCode DMPlexVecGetClosure_Static(DM dm, PetscSection section, PetscInt numPoints, const PetscInt points[], const PetscInt clperm[], const PetscScalar vArray[], PetscInt *size, PetscScalar array[])
7017: {
7018:   PetscInt            offset = 0, p;
7019:   const PetscInt    **perms  = NULL;
7020:   const PetscScalar **flips  = NULL;

7022:   PetscFunctionBeginHot;
7023:   *size = 0;
7024:   PetscCall(PetscSectionGetPointSyms(section, numPoints, points, &perms, &flips));
7025:   for (p = 0; p < numPoints; p++) {
7026:     const PetscInt     point = points[2 * p];
7027:     const PetscInt    *perm  = perms ? perms[p] : NULL;
7028:     const PetscScalar *flip  = flips ? flips[p] : NULL;
7029:     PetscInt           dof, off, d;
7030:     const PetscScalar *varr;

7032:     PetscCall(PetscSectionGetDof(section, point, &dof));
7033:     PetscCall(PetscSectionGetOffset(section, point, &off));
7034:     varr = PetscSafePointerPlusOffset(vArray, off);
7035:     if (clperm) {
7036:       if (perm) {
7037:         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]] = varr[d];
7038:       } else {
7039:         for (d = 0; d < dof; d++) array[clperm[offset + d]] = varr[d];
7040:       }
7041:       if (flip) {
7042:         for (d = 0; d < dof; d++) array[clperm[offset + d]] *= flip[d];
7043:       }
7044:     } else {
7045:       if (perm) {
7046:         for (d = 0; d < dof; d++) array[offset + perm[d]] = varr[d];
7047:       } else {
7048:         for (d = 0; d < dof; d++) array[offset + d] = varr[d];
7049:       }
7050:       if (flip) {
7051:         for (d = 0; d < dof; d++) array[offset + d] *= flip[d];
7052:       }
7053:     }
7054:     offset += dof;
7055:   }
7056:   PetscCall(PetscSectionRestorePointSyms(section, numPoints, points, &perms, &flips));
7057:   *size = offset;
7058:   PetscFunctionReturn(PETSC_SUCCESS);
7059: }

7061: 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[])
7062: {
7063:   PetscInt offset = 0, f;

7065:   PetscFunctionBeginHot;
7066:   *size = 0;
7067:   for (f = 0; f < numFields; ++f) {
7068:     PetscInt            p;
7069:     const PetscInt    **perms = NULL;
7070:     const PetscScalar **flips = NULL;

7072:     PetscCall(PetscSectionGetFieldPointSyms(section, f, numPoints, points, &perms, &flips));
7073:     for (p = 0; p < numPoints; p++) {
7074:       const PetscInt     point = points[2 * p];
7075:       PetscInt           fdof, foff, b;
7076:       const PetscScalar *varr;
7077:       const PetscInt    *perm = perms ? perms[p] : NULL;
7078:       const PetscScalar *flip = flips ? flips[p] : NULL;

7080:       PetscCall(PetscSectionGetFieldDof(section, point, f, &fdof));
7081:       PetscCall(PetscSectionGetFieldOffset(section, point, f, &foff));
7082:       varr = &vArray[foff];
7083:       if (clperm) {
7084:         if (perm) {
7085:           for (b = 0; b < fdof; b++) array[clperm[offset + perm[b]]] = varr[b];
7086:         } else {
7087:           for (b = 0; b < fdof; b++) array[clperm[offset + b]] = varr[b];
7088:         }
7089:         if (flip) {
7090:           for (b = 0; b < fdof; b++) array[clperm[offset + b]] *= flip[b];
7091:         }
7092:       } else {
7093:         if (perm) {
7094:           for (b = 0; b < fdof; b++) array[offset + perm[b]] = varr[b];
7095:         } else {
7096:           for (b = 0; b < fdof; b++) array[offset + b] = varr[b];
7097:         }
7098:         if (flip) {
7099:           for (b = 0; b < fdof; b++) array[offset + b] *= flip[b];
7100:         }
7101:       }
7102:       offset += fdof;
7103:     }
7104:     PetscCall(PetscSectionRestoreFieldPointSyms(section, f, numPoints, points, &perms, &flips));
7105:   }
7106:   *size = offset;
7107:   PetscFunctionReturn(PETSC_SUCCESS);
7108: }

7110: /*@
7111:   DMPlexVecGetOrientedClosure - Get an array of the values on the closure of `point` with a given orientation, optionally applying the closure permutation.

7113:   Not collective

7115:   Input Parameters:
7116: + dm        - The `DM`
7117: . section   - The section describing the layout in `v`, or `NULL` to use the default section
7118: . useClPerm - Flag for whether the provided closure permutation should be applied to the values
7119: . v         - The local vector
7120: . point     - The point in the `DM`
7121: - ornt      - The orientation of the cell, an integer giving the prescription for cone traversal. Typically, this will be 0.

7123:   Input/Output Parameters:
7124: + csize  - The size of the input values array, or `NULL`; on output the number of values in the closure
7125: - values - An array to use for the values, or *values = `NULL` to have it allocated automatically;
7126:            if the user provided `NULL`, it is a borrowed array and should not be freed, use  `DMPlexVecRestoreClosure()` to return it

7128:   Level: advanced

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

7135:   Fortran Notes:
7136:   The `csize` argument is present in the Fortran binding. Since the Fortran `values` array contains its length information this argument may not be needed.
7137:   In that case one may pass `PETSC_NULL_INTEGER` for `csize`.

7139:   `values` must be declared with
7140: .vb
7141:   PetscScalar,dimension(:),pointer   :: values
7142: .ve
7143:   and it will be allocated internally by PETSc to hold the values returned

7145: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexVecGetClosure()`, `DMPlexGetCellCoordinates()`, `DMPlexVecRestoreClosure()`, `DMPlexVecSetClosure()`
7146: @*/
7147: PetscErrorCode DMPlexVecGetOrientedClosure(DM dm, PetscSection section, PetscBool useClPerm, Vec v, PetscInt point, PetscInt ornt, PetscInt *csize, PetscScalar *values[])
7148: {
7149:   PetscSection    clSection;
7150:   IS              clPoints;
7151:   PetscInt       *points = NULL;
7152:   const PetscInt *clp, *perm = NULL;
7153:   PetscInt        depth, numFields, numPoints, asize;

7155:   PetscFunctionBeginHot;
7157:   if (!section) PetscCall(DMGetLocalSection(dm, &section));
7160:   PetscCall(DMPlexGetDepth(dm, &depth));
7161:   PetscCall(PetscSectionGetNumFields(section, &numFields));
7162:   if (depth == 1 && numFields < 2) {
7163:     PetscCall(DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values));
7164:     PetscFunctionReturn(PETSC_SUCCESS);
7165:   }
7166:   /* Get points */
7167:   PetscCall(DMPlexGetCompressedClosure(dm, section, point, ornt, &numPoints, &points, &clSection, &clPoints, &clp));
7168:   /* Get sizes */
7169:   asize = 0;
7170:   for (PetscInt p = 0; p < numPoints * 2; p += 2) {
7171:     PetscInt dof;
7172:     PetscCall(PetscSectionGetDof(section, points[p], &dof));
7173:     asize += dof;
7174:   }
7175:   if (values) {
7176:     const PetscScalar *vArray;
7177:     PetscInt           size;

7179:     if (*values) {
7180:       PetscCheck(*csize >= asize, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Provided array size %" PetscInt_FMT " not sufficient to hold closure size %" PetscInt_FMT, *csize, asize);
7181:     } else PetscCall(DMGetWorkArray(dm, asize, MPIU_SCALAR, values));
7182:     if (useClPerm) PetscCall(PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject)dm, depth, asize, &perm));
7183:     PetscCall(VecGetArrayRead(v, &vArray));
7184:     /* Get values */
7185:     if (numFields > 0) PetscCall(DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, *values));
7186:     else PetscCall(DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, *values));
7187:     PetscCheck(asize == size, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Section size %" PetscInt_FMT " does not match Vec closure size %" PetscInt_FMT, asize, size);
7188:     /* Cleanup array */
7189:     PetscCall(VecRestoreArrayRead(v, &vArray));
7190:   }
7191:   if (csize) *csize = asize;
7192:   /* Cleanup points */
7193:   PetscCall(DMPlexRestoreCompressedClosure(dm, section, point, &numPoints, &points, &clSection, &clPoints, &clp));
7194:   PetscFunctionReturn(PETSC_SUCCESS);
7195: }

7197: /*@
7198:   DMPlexVecGetClosure - Get an array of the values on the closure of `point`

7200:   Not collective

7202:   Input Parameters:
7203: + dm      - The `DM`
7204: . section - The section describing the layout in `v`, or `NULL` to use the default section
7205: . v       - The local vector
7206: - point   - The point in the `DM`

7208:   Input/Output Parameters:
7209: + csize  - The size of the input values array, or `NULL`; on output the number of values in the closure
7210: - values - An array to use for the values, or *values = `NULL` to have it allocated automatically;
7211:            if the user provided `NULL`, it is a borrowed array and should not be freed, use  `DMPlexVecRestoreClosure()` to return it

7213:   Level: intermediate

7215:   Notes:
7216:   This is used for getting the all values in a `Vec` in the closure of a mesh point.
7217:   To get only the values in the closure of a mesh point at a specific depth (for example, at mesh vertices), use `DMPlexVecGetClosureAtDepth()`.

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

7223:   A typical use could be
7224: .vb
7225:    values = NULL;
7226:    PetscCall(DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values));
7227:    for (cl = 0; cl < clSize; ++cl) {
7228:      <Compute on closure>
7229:    }
7230:    PetscCall(DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values));
7231: .ve
7232:   or
7233: .vb
7234:    PetscMalloc1(clMaxSize, &values);
7235:    for (p = pStart; p < pEnd; ++p) {
7236:      clSize = clMaxSize;
7237:      PetscCall(DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values));
7238:      for (cl = 0; cl < clSize; ++cl) {
7239:        <Compute on closure>
7240:      }
7241:    }
7242:    PetscFree(values);
7243: .ve

7245:   Fortran Notes:
7246:   The `csize` argument is present in the Fortran binding. Since the Fortran `values` array contains its length information this argument may not be needed.
7247:   In that case one may pass `PETSC_NULL_INTEGER` for `csize`.

7249:   `values` must be declared with
7250: .vb
7251:   PetscScalar,dimension(:),pointer   :: values
7252: .ve
7253:   and it will be allocated internally by PETSc to hold the values returned

7255: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexVecGetClosureAtDepth()`, `DMPlexVecRestoreClosure()`, `DMPlexVecSetClosure()`, `DMPlexMatSetClosure()`
7256: @*/
7257: PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
7258: {
7259:   PetscFunctionBeginHot;
7260:   PetscCall(DMPlexVecGetOrientedClosure(dm, section, PETSC_TRUE, v, point, 0, csize, values));
7261:   PetscFunctionReturn(PETSC_SUCCESS);
7262: }

7264: /*@
7265:   DMPlexVecGetClosureAtDepth - Get an array of the values on the closure of `point` that are at a specific depth

7267:   Not collective

7269:   Input Parameters:
7270: + dm      - The `DM`
7271: . section - The section describing the layout in `v`, or `NULL` to use the default section
7272: . v       - The local vector
7273: . depth   - The depth of mesh points that should be returned
7274: - point   - The point in the `DM`

7276:   Input/Output Parameters:
7277: + csize  - The size of the input values array, or `NULL`; on output the number of values in the closure
7278: - values - An array to use for the values, or *values = `NULL` to have it allocated automatically;
7279:            if the user provided `NULL`, it is a borrowed array and should not be freed, use  `DMPlexVecRestoreClosure()` to return it

7281:   Level: intermediate

7283:   Notes:
7284:   This is used for getting the values in a `Vec` associated with specific mesh points.
7285:   For example, to get only the values at mesh vertices, pass `depth=0`. To get all the values in the closure of a mesh point, use `DMPlexVecGetClosure()`.

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

7291:   A typical use could be
7292: .vb
7293:    values = NULL;
7294:    PetscCall(DMPlexVecGetClosureAtDepth(dm, NULL, v, p, depth, &clSize, &values));
7295:    for (cl = 0; cl < clSize; ++cl) {
7296:      <Compute on closure>
7297:    }
7298:    PetscCall(DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values));
7299: .ve
7300:   or
7301: .vb
7302:    PetscMalloc1(clMaxSize, &values);
7303:    for (p = pStart; p < pEnd; ++p) {
7304:      clSize = clMaxSize;
7305:      PetscCall(DMPlexVecGetClosureAtDepth(dm, NULL, v, p, depth, &clSize, &values));
7306:      for (cl = 0; cl < clSize; ++cl) {
7307:        <Compute on closure>
7308:      }
7309:    }
7310:    PetscFree(values);
7311: .ve

7313:   Fortran Notes:
7314:   The `csize` argument is present in the Fortran binding. Since the Fortran `values` array contains its length information this argument may not be needed.
7315:   In that case one may pass `PETSC_NULL_INTEGER` for `csize`.

7317:   `values` must be declared with
7318: .vb
7319:   PetscScalar,dimension(:),pointer   :: values
7320: .ve
7321:   and it will be allocated internally by PETSc to hold the values returned

7323: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexVecGetClosure()`, `DMPlexVecRestoreClosure()`, `DMPlexVecSetClosure()`, `DMPlexMatSetClosure()`
7324: @*/
7325: PetscErrorCode DMPlexVecGetClosureAtDepth(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt depth, PetscInt *csize, PetscScalar *values[])
7326: {
7327:   DMLabel            depthLabel;
7328:   PetscSection       clSection;
7329:   IS                 clPoints;
7330:   PetscScalar       *array;
7331:   const PetscScalar *vArray;
7332:   PetscInt          *points = NULL;
7333:   const PetscInt    *clp, *perm = NULL;
7334:   PetscInt           mdepth, numFields, numPoints, Np = 0, p, clsize, size;

7336:   PetscFunctionBeginHot;
7338:   if (!section) PetscCall(DMGetLocalSection(dm, &section));
7341:   PetscCall(DMPlexGetDepth(dm, &mdepth));
7342:   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
7343:   PetscCall(PetscSectionGetNumFields(section, &numFields));
7344:   if (mdepth == 1 && numFields < 2) {
7345:     PetscCall(DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values));
7346:     PetscFunctionReturn(PETSC_SUCCESS);
7347:   }
7348:   /* Get points */
7349:   PetscCall(DMPlexGetCompressedClosure(dm, section, point, 0, &numPoints, &points, &clSection, &clPoints, &clp));
7350:   for (clsize = 0, p = 0; p < Np; p++) {
7351:     PetscInt dof;
7352:     PetscCall(PetscSectionGetDof(section, points[2 * p], &dof));
7353:     clsize += dof;
7354:   }
7355:   PetscCall(PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject)dm, depth, clsize, &perm));
7356:   /* Filter points */
7357:   for (p = 0; p < numPoints * 2; p += 2) {
7358:     PetscInt dep;

7360:     PetscCall(DMLabelGetValue(depthLabel, points[p], &dep));
7361:     if (dep != depth) continue;
7362:     points[Np * 2 + 0] = points[p];
7363:     points[Np * 2 + 1] = points[p + 1];
7364:     ++Np;
7365:   }
7366:   /* Get array */
7367:   if (!values || !*values) {
7368:     PetscInt asize = 0, dof;

7370:     for (p = 0; p < Np * 2; p += 2) {
7371:       PetscCall(PetscSectionGetDof(section, points[p], &dof));
7372:       asize += dof;
7373:     }
7374:     if (!values) {
7375:       PetscCall(DMPlexRestoreCompressedClosure(dm, section, point, &numPoints, &points, &clSection, &clPoints, &clp));
7376:       if (csize) *csize = asize;
7377:       PetscFunctionReturn(PETSC_SUCCESS);
7378:     }
7379:     PetscCall(DMGetWorkArray(dm, asize, MPIU_SCALAR, &array));
7380:   } else {
7381:     array = *values;
7382:   }
7383:   PetscCall(VecGetArrayRead(v, &vArray));
7384:   /* Get values */
7385:   if (numFields > 0) PetscCall(DMPlexVecGetClosure_Fields_Static(dm, section, Np, points, numFields, perm, vArray, &size, array));
7386:   else PetscCall(DMPlexVecGetClosure_Static(dm, section, Np, points, perm, vArray, &size, array));
7387:   /* Cleanup points */
7388:   PetscCall(DMPlexRestoreCompressedClosure(dm, section, point, &numPoints, &points, &clSection, &clPoints, &clp));
7389:   /* Cleanup array */
7390:   PetscCall(VecRestoreArrayRead(v, &vArray));
7391:   if (!*values) {
7392:     if (csize) *csize = size;
7393:     *values = array;
7394:   } else {
7395:     PetscCheck(size <= *csize, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %" PetscInt_FMT " < actual size %" PetscInt_FMT, *csize, size);
7396:     *csize = size;
7397:   }
7398:   PetscFunctionReturn(PETSC_SUCCESS);
7399: }

7401: /*@
7402:   DMPlexVecRestoreClosure - Restore the array of the values on the closure of `point` obtained with `DMPlexVecGetClosure()`

7404:   Not collective

7406:   Input Parameters:
7407: + dm      - The `DM`
7408: . section - The section describing the layout in `v`, or `NULL` to use the default section
7409: . v       - The local vector
7410: . point   - The point in the `DM`
7411: . csize   - The number of values in the closure, or `NULL`
7412: - values  - The array of values

7414:   Level: intermediate

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

7419:   Fortran Note:
7420:   The `csize` argument is present in the Fortran binding. Since the Fortran `values` array contains its length information this argument may not be needed.
7421:   In that case one may pass `PETSC_NULL_INTEGER` for `csize`.

7423: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexVecGetClosure()`, `DMPlexVecSetClosure()`, `DMPlexMatSetClosure()`
7424: @*/
7425: PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
7426: {
7427:   PetscInt size = 0;

7429:   PetscFunctionBegin;
7430:   /* Should work without recalculating size */
7431:   PetscCall(DMRestoreWorkArray(dm, size, MPIU_SCALAR, (void *)values));
7432:   *values = NULL;
7433:   PetscFunctionReturn(PETSC_SUCCESS);
7434: }

7436: static inline void add(PetscScalar *x, PetscScalar y)
7437: {
7438:   *x += y;
7439: }
7440: static inline void insert(PetscScalar *x, PetscScalar y)
7441: {
7442:   *x = y;
7443: }

7445: 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[])
7446: {
7447:   PetscInt        cdof;  /* The number of constraints on this point */
7448:   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
7449:   PetscScalar    *a;
7450:   PetscInt        off, cind = 0, k;

7452:   PetscFunctionBegin;
7453:   PetscCall(PetscSectionGetConstraintDof(section, point, &cdof));
7454:   PetscCall(PetscSectionGetOffset(section, point, &off));
7455:   a = &array[off];
7456:   if (!cdof || setBC) {
7457:     if (clperm) {
7458:       if (perm) {
7459:         for (k = 0; k < dof; ++k) fuse(&a[k], values[clperm[offset + perm[k]]] * (flip ? flip[perm[k]] : 1.));
7460:       } else {
7461:         for (k = 0; k < dof; ++k) fuse(&a[k], values[clperm[offset + k]] * (flip ? flip[k] : 1.));
7462:       }
7463:     } else {
7464:       if (perm) {
7465:         for (k = 0; k < dof; ++k) fuse(&a[k], values[offset + perm[k]] * (flip ? flip[perm[k]] : 1.));
7466:       } else {
7467:         for (k = 0; k < dof; ++k) fuse(&a[k], values[offset + k] * (flip ? flip[k] : 1.));
7468:       }
7469:     }
7470:   } else {
7471:     PetscCall(PetscSectionGetConstraintIndices(section, point, &cdofs));
7472:     if (clperm) {
7473:       if (perm) {
7474:         for (k = 0; k < dof; ++k) {
7475:           if (cind < cdof && k == cdofs[cind]) {
7476:             ++cind;
7477:             continue;
7478:           }
7479:           fuse(&a[k], values[clperm[offset + perm[k]]] * (flip ? flip[perm[k]] : 1.));
7480:         }
7481:       } else {
7482:         for (k = 0; k < dof; ++k) {
7483:           if (cind < cdof && k == cdofs[cind]) {
7484:             ++cind;
7485:             continue;
7486:           }
7487:           fuse(&a[k], values[clperm[offset + k]] * (flip ? flip[k] : 1.));
7488:         }
7489:       }
7490:     } else {
7491:       if (perm) {
7492:         for (k = 0; k < dof; ++k) {
7493:           if (cind < cdof && k == cdofs[cind]) {
7494:             ++cind;
7495:             continue;
7496:           }
7497:           fuse(&a[k], values[offset + perm[k]] * (flip ? flip[perm[k]] : 1.));
7498:         }
7499:       } else {
7500:         for (k = 0; k < dof; ++k) {
7501:           if (cind < cdof && k == cdofs[cind]) {
7502:             ++cind;
7503:             continue;
7504:           }
7505:           fuse(&a[k], values[offset + k] * (flip ? flip[k] : 1.));
7506:         }
7507:       }
7508:     }
7509:   }
7510:   PetscFunctionReturn(PETSC_SUCCESS);
7511: }

7513: 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[])
7514: {
7515:   PetscInt        cdof;  /* The number of constraints on this point */
7516:   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
7517:   PetscScalar    *a;
7518:   PetscInt        off, cind = 0, k;

7520:   PetscFunctionBegin;
7521:   PetscCall(PetscSectionGetConstraintDof(section, point, &cdof));
7522:   PetscCall(PetscSectionGetOffset(section, point, &off));
7523:   a = &array[off];
7524:   if (cdof) {
7525:     PetscCall(PetscSectionGetConstraintIndices(section, point, &cdofs));
7526:     if (clperm) {
7527:       if (perm) {
7528:         for (k = 0; k < dof; ++k) {
7529:           if (cind < cdof && k == cdofs[cind]) {
7530:             fuse(&a[k], values[clperm[offset + perm[k]]] * (flip ? flip[perm[k]] : 1.));
7531:             cind++;
7532:           }
7533:         }
7534:       } else {
7535:         for (k = 0; k < dof; ++k) {
7536:           if (cind < cdof && k == cdofs[cind]) {
7537:             fuse(&a[k], values[clperm[offset + k]] * (flip ? flip[k] : 1.));
7538:             cind++;
7539:           }
7540:         }
7541:       }
7542:     } else {
7543:       if (perm) {
7544:         for (k = 0; k < dof; ++k) {
7545:           if (cind < cdof && k == cdofs[cind]) {
7546:             fuse(&a[k], values[offset + perm[k]] * (flip ? flip[perm[k]] : 1.));
7547:             cind++;
7548:           }
7549:         }
7550:       } else {
7551:         for (k = 0; k < dof; ++k) {
7552:           if (cind < cdof && k == cdofs[cind]) {
7553:             fuse(&a[k], values[offset + k] * (flip ? flip[k] : 1.));
7554:             cind++;
7555:           }
7556:         }
7557:       }
7558:     }
7559:   }
7560:   PetscFunctionReturn(PETSC_SUCCESS);
7561: }

7563: 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[])
7564: {
7565:   PetscScalar    *a;
7566:   PetscInt        fdof, foff, fcdof, foffset = *offset;
7567:   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
7568:   PetscInt        cind = 0, b;

7570:   PetscFunctionBegin;
7571:   PetscCall(PetscSectionGetFieldDof(section, point, f, &fdof));
7572:   PetscCall(PetscSectionGetFieldConstraintDof(section, point, f, &fcdof));
7573:   PetscCall(PetscSectionGetFieldOffset(section, point, f, &foff));
7574:   a = &array[foff];
7575:   if (!fcdof || setBC) {
7576:     if (clperm) {
7577:       if (perm) {
7578:         for (b = 0; b < fdof; b++) fuse(&a[b], values[clperm[foffset + perm[b]]] * (flip ? flip[perm[b]] : 1.));
7579:       } else {
7580:         for (b = 0; b < fdof; b++) fuse(&a[b], values[clperm[foffset + b]] * (flip ? flip[b] : 1.));
7581:       }
7582:     } else {
7583:       if (perm) {
7584:         for (b = 0; b < fdof; b++) fuse(&a[b], values[foffset + perm[b]] * (flip ? flip[perm[b]] : 1.));
7585:       } else {
7586:         for (b = 0; b < fdof; b++) fuse(&a[b], values[foffset + b] * (flip ? flip[b] : 1.));
7587:       }
7588:     }
7589:   } else {
7590:     PetscCall(PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs));
7591:     if (clperm) {
7592:       if (perm) {
7593:         for (b = 0; b < fdof; b++) {
7594:           if (cind < fcdof && b == fcdofs[cind]) {
7595:             ++cind;
7596:             continue;
7597:           }
7598:           fuse(&a[b], values[clperm[foffset + perm[b]]] * (flip ? flip[perm[b]] : 1.));
7599:         }
7600:       } else {
7601:         for (b = 0; b < fdof; b++) {
7602:           if (cind < fcdof && b == fcdofs[cind]) {
7603:             ++cind;
7604:             continue;
7605:           }
7606:           fuse(&a[b], values[clperm[foffset + b]] * (flip ? flip[b] : 1.));
7607:         }
7608:       }
7609:     } else {
7610:       if (perm) {
7611:         for (b = 0; b < fdof; b++) {
7612:           if (cind < fcdof && b == fcdofs[cind]) {
7613:             ++cind;
7614:             continue;
7615:           }
7616:           fuse(&a[b], values[foffset + perm[b]] * (flip ? flip[perm[b]] : 1.));
7617:         }
7618:       } else {
7619:         for (b = 0; b < fdof; b++) {
7620:           if (cind < fcdof && b == fcdofs[cind]) {
7621:             ++cind;
7622:             continue;
7623:           }
7624:           fuse(&a[b], values[foffset + b] * (flip ? flip[b] : 1.));
7625:         }
7626:       }
7627:     }
7628:   }
7629:   *offset += fdof;
7630:   PetscFunctionReturn(PETSC_SUCCESS);
7631: }

7633: 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[])
7634: {
7635:   PetscScalar    *a;
7636:   PetscInt        fdof, foff, fcdof, foffset = *offset;
7637:   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
7638:   PetscInt        Nc, cind = 0, ncind = 0, b;
7639:   PetscBool       ncSet, fcSet;

7641:   PetscFunctionBegin;
7642:   PetscCall(PetscSectionGetFieldComponents(section, f, &Nc));
7643:   PetscCall(PetscSectionGetFieldDof(section, point, f, &fdof));
7644:   PetscCall(PetscSectionGetFieldConstraintDof(section, point, f, &fcdof));
7645:   PetscCall(PetscSectionGetFieldOffset(section, point, f, &foff));
7646:   a = &array[foff];
7647:   if (fcdof) {
7648:     /* We just override fcdof and fcdofs with Ncc and comps */
7649:     PetscCall(PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs));
7650:     if (clperm) {
7651:       if (perm) {
7652:         if (comps) {
7653:           for (b = 0; b < fdof; b++) {
7654:             ncSet = fcSet = PETSC_FALSE;
7655:             if (b % Nc == comps[ncind]) {
7656:               ncind = (ncind + 1) % Ncc;
7657:               ncSet = PETSC_TRUE;
7658:             }
7659:             if (cind < fcdof && b == fcdofs[cind]) {
7660:               ++cind;
7661:               fcSet = PETSC_TRUE;
7662:             }
7663:             if (ncSet && fcSet) fuse(&a[b], values[clperm[foffset + perm[b]]] * (flip ? flip[perm[b]] : 1.));
7664:           }
7665:         } else {
7666:           for (b = 0; b < fdof; b++) {
7667:             if (cind < fcdof && b == fcdofs[cind]) {
7668:               fuse(&a[b], values[clperm[foffset + perm[b]]] * (flip ? flip[perm[b]] : 1.));
7669:               ++cind;
7670:             }
7671:           }
7672:         }
7673:       } else {
7674:         if (comps) {
7675:           for (b = 0; b < fdof; b++) {
7676:             ncSet = fcSet = PETSC_FALSE;
7677:             if (b % Nc == comps[ncind]) {
7678:               ncind = (ncind + 1) % Ncc;
7679:               ncSet = PETSC_TRUE;
7680:             }
7681:             if (cind < fcdof && b == fcdofs[cind]) {
7682:               ++cind;
7683:               fcSet = PETSC_TRUE;
7684:             }
7685:             if (ncSet && fcSet) fuse(&a[b], values[clperm[foffset + b]] * (flip ? flip[b] : 1.));
7686:           }
7687:         } else {
7688:           for (b = 0; b < fdof; b++) {
7689:             if (cind < fcdof && b == fcdofs[cind]) {
7690:               fuse(&a[b], values[clperm[foffset + b]] * (flip ? flip[b] : 1.));
7691:               ++cind;
7692:             }
7693:           }
7694:         }
7695:       }
7696:     } else {
7697:       if (perm) {
7698:         if (comps) {
7699:           for (b = 0; b < fdof; b++) {
7700:             ncSet = fcSet = PETSC_FALSE;
7701:             if (b % Nc == comps[ncind]) {
7702:               ncind = (ncind + 1) % Ncc;
7703:               ncSet = PETSC_TRUE;
7704:             }
7705:             if (cind < fcdof && b == fcdofs[cind]) {
7706:               ++cind;
7707:               fcSet = PETSC_TRUE;
7708:             }
7709:             if (ncSet && fcSet) fuse(&a[b], values[foffset + perm[b]] * (flip ? flip[perm[b]] : 1.));
7710:           }
7711:         } else {
7712:           for (b = 0; b < fdof; b++) {
7713:             if (cind < fcdof && b == fcdofs[cind]) {
7714:               fuse(&a[b], values[foffset + perm[b]] * (flip ? flip[perm[b]] : 1.));
7715:               ++cind;
7716:             }
7717:           }
7718:         }
7719:       } else {
7720:         if (comps) {
7721:           for (b = 0; b < fdof; b++) {
7722:             ncSet = fcSet = PETSC_FALSE;
7723:             if (b % Nc == comps[ncind]) {
7724:               ncind = (ncind + 1) % Ncc;
7725:               ncSet = PETSC_TRUE;
7726:             }
7727:             if (cind < fcdof && b == fcdofs[cind]) {
7728:               ++cind;
7729:               fcSet = PETSC_TRUE;
7730:             }
7731:             if (ncSet && fcSet) fuse(&a[b], values[foffset + b] * (flip ? flip[b] : 1.));
7732:           }
7733:         } else {
7734:           for (b = 0; b < fdof; b++) {
7735:             if (cind < fcdof && b == fcdofs[cind]) {
7736:               fuse(&a[b], values[foffset + b] * (flip ? flip[b] : 1.));
7737:               ++cind;
7738:             }
7739:           }
7740:         }
7741:       }
7742:     }
7743:   }
7744:   *offset += fdof;
7745:   PetscFunctionReturn(PETSC_SUCCESS);
7746: }

7748: static inline PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
7749: {
7750:   PetscScalar    *array;
7751:   const PetscInt *cone, *coneO;
7752:   PetscInt        pStart, pEnd, p, numPoints, off, dof;

7754:   PetscFunctionBeginHot;
7755:   PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
7756:   PetscCall(DMPlexGetConeSize(dm, point, &numPoints));
7757:   PetscCall(DMPlexGetCone(dm, point, &cone));
7758:   PetscCall(DMPlexGetConeOrientation(dm, point, &coneO));
7759:   PetscCall(VecGetArray(v, &array));
7760:   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
7761:     const PetscInt cp = !p ? point : cone[p - 1];
7762:     const PetscInt o  = !p ? 0 : coneO[p - 1];

7764:     if (cp < pStart || cp >= pEnd) {
7765:       dof = 0;
7766:       continue;
7767:     }
7768:     PetscCall(PetscSectionGetDof(section, cp, &dof));
7769:     /* ADD_VALUES */
7770:     {
7771:       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
7772:       PetscScalar    *a;
7773:       PetscInt        cdof, coff, cind = 0, k;

7775:       PetscCall(PetscSectionGetConstraintDof(section, cp, &cdof));
7776:       PetscCall(PetscSectionGetOffset(section, cp, &coff));
7777:       a = &array[coff];
7778:       if (!cdof) {
7779:         if (o >= 0) {
7780:           for (k = 0; k < dof; ++k) a[k] += values[off + k];
7781:         } else {
7782:           for (k = 0; k < dof; ++k) a[k] += values[off + dof - k - 1];
7783:         }
7784:       } else {
7785:         PetscCall(PetscSectionGetConstraintIndices(section, cp, &cdofs));
7786:         if (o >= 0) {
7787:           for (k = 0; k < dof; ++k) {
7788:             if (cind < cdof && k == cdofs[cind]) {
7789:               ++cind;
7790:               continue;
7791:             }
7792:             a[k] += values[off + k];
7793:           }
7794:         } else {
7795:           for (k = 0; k < dof; ++k) {
7796:             if (cind < cdof && k == cdofs[cind]) {
7797:               ++cind;
7798:               continue;
7799:             }
7800:             a[k] += values[off + dof - k - 1];
7801:           }
7802:         }
7803:       }
7804:     }
7805:   }
7806:   PetscCall(VecRestoreArray(v, &array));
7807:   PetscFunctionReturn(PETSC_SUCCESS);
7808: }

7810: /*@
7811:   DMPlexVecSetClosure - Set an array of the values on the closure of `point`

7813:   Not collective

7815:   Input Parameters:
7816: + dm      - The `DM`
7817: . section - The section describing the layout in `v`, or `NULL` to use the default section
7818: . v       - The local vector
7819: . point   - The point in the `DM`
7820: . values  - The array of values
7821: - mode    - The insert mode. One of `INSERT_ALL_VALUES`, `ADD_ALL_VALUES`, `INSERT_VALUES`, `ADD_VALUES`, `INSERT_BC_VALUES`, and `ADD_BC_VALUES`,
7822:             where `INSERT_ALL_VALUES` and `ADD_ALL_VALUES` also overwrite boundary conditions.

7824:   Level: intermediate

7826:   Note:
7827:   Usually the input arrays were obtained with `DMPlexVecGetClosure()`

7829: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexVecGetClosure()`, `DMPlexMatSetClosure()`
7830: @*/
7831: PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
7832: {
7833:   PetscSection    clSection;
7834:   IS              clPoints;
7835:   PetscScalar    *array;
7836:   PetscInt       *points = NULL;
7837:   const PetscInt *clp, *clperm = NULL;
7838:   PetscInt        depth, numFields, numPoints, p, clsize;

7840:   PetscFunctionBeginHot;
7842:   if (!section) PetscCall(DMGetLocalSection(dm, &section));
7845:   PetscCall(DMPlexGetDepth(dm, &depth));
7846:   PetscCall(PetscSectionGetNumFields(section, &numFields));
7847:   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
7848:     PetscCall(DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode));
7849:     PetscFunctionReturn(PETSC_SUCCESS);
7850:   }
7851:   /* Get points */
7852:   PetscCall(DMPlexGetCompressedClosure(dm, section, point, 0, &numPoints, &points, &clSection, &clPoints, &clp));
7853:   for (clsize = 0, p = 0; p < numPoints; p++) {
7854:     PetscInt dof;
7855:     PetscCall(PetscSectionGetDof(section, points[2 * p], &dof));
7856:     clsize += dof;
7857:   }
7858:   PetscCall(PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject)dm, depth, clsize, &clperm));
7859:   /* Get array */
7860:   PetscCall(VecGetArray(v, &array));
7861:   /* Get values */
7862:   if (numFields > 0) {
7863:     PetscInt offset = 0, f;
7864:     for (f = 0; f < numFields; ++f) {
7865:       const PetscInt    **perms = NULL;
7866:       const PetscScalar **flips = NULL;

7868:       PetscCall(PetscSectionGetFieldPointSyms(section, f, numPoints, points, &perms, &flips));
7869:       switch (mode) {
7870:       case INSERT_VALUES:
7871:         for (p = 0; p < numPoints; p++) {
7872:           const PetscInt     point = points[2 * p];
7873:           const PetscInt    *perm  = perms ? perms[p] : NULL;
7874:           const PetscScalar *flip  = flips ? flips[p] : NULL;
7875:           PetscCall(updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array));
7876:         }
7877:         break;
7878:       case INSERT_ALL_VALUES:
7879:         for (p = 0; p < numPoints; p++) {
7880:           const PetscInt     point = points[2 * p];
7881:           const PetscInt    *perm  = perms ? perms[p] : NULL;
7882:           const PetscScalar *flip  = flips ? flips[p] : NULL;
7883:           PetscCall(updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array));
7884:         }
7885:         break;
7886:       case INSERT_BC_VALUES:
7887:         for (p = 0; p < numPoints; p++) {
7888:           const PetscInt     point = points[2 * p];
7889:           const PetscInt    *perm  = perms ? perms[p] : NULL;
7890:           const PetscScalar *flip  = flips ? flips[p] : NULL;
7891:           PetscCall(updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, insert, clperm, values, &offset, array));
7892:         }
7893:         break;
7894:       case ADD_VALUES:
7895:         for (p = 0; p < numPoints; p++) {
7896:           const PetscInt     point = points[2 * p];
7897:           const PetscInt    *perm  = perms ? perms[p] : NULL;
7898:           const PetscScalar *flip  = flips ? flips[p] : NULL;
7899:           PetscCall(updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array));
7900:         }
7901:         break;
7902:       case ADD_ALL_VALUES:
7903:         for (p = 0; p < numPoints; p++) {
7904:           const PetscInt     point = points[2 * p];
7905:           const PetscInt    *perm  = perms ? perms[p] : NULL;
7906:           const PetscScalar *flip  = flips ? flips[p] : NULL;
7907:           PetscCall(updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array));
7908:         }
7909:         break;
7910:       case ADD_BC_VALUES:
7911:         for (p = 0; p < numPoints; p++) {
7912:           const PetscInt     point = points[2 * p];
7913:           const PetscInt    *perm  = perms ? perms[p] : NULL;
7914:           const PetscScalar *flip  = flips ? flips[p] : NULL;
7915:           PetscCall(updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, add, clperm, values, &offset, array));
7916:         }
7917:         break;
7918:       default:
7919:         SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
7920:       }
7921:       PetscCall(PetscSectionRestoreFieldPointSyms(section, f, numPoints, points, &perms, &flips));
7922:     }
7923:   } else {
7924:     PetscInt            dof, off;
7925:     const PetscInt    **perms = NULL;
7926:     const PetscScalar **flips = NULL;

7928:     PetscCall(PetscSectionGetPointSyms(section, numPoints, points, &perms, &flips));
7929:     switch (mode) {
7930:     case INSERT_VALUES:
7931:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
7932:         const PetscInt     point = points[2 * p];
7933:         const PetscInt    *perm  = perms ? perms[p] : NULL;
7934:         const PetscScalar *flip  = flips ? flips[p] : NULL;
7935:         PetscCall(PetscSectionGetDof(section, point, &dof));
7936:         PetscCall(updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array));
7937:       }
7938:       break;
7939:     case INSERT_ALL_VALUES:
7940:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
7941:         const PetscInt     point = points[2 * p];
7942:         const PetscInt    *perm  = perms ? perms[p] : NULL;
7943:         const PetscScalar *flip  = flips ? flips[p] : NULL;
7944:         PetscCall(PetscSectionGetDof(section, point, &dof));
7945:         PetscCall(updatePoint_private(section, point, dof, insert, PETSC_TRUE, perm, flip, clperm, values, off, array));
7946:       }
7947:       break;
7948:     case INSERT_BC_VALUES:
7949:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
7950:         const PetscInt     point = points[2 * p];
7951:         const PetscInt    *perm  = perms ? perms[p] : NULL;
7952:         const PetscScalar *flip  = flips ? flips[p] : NULL;
7953:         PetscCall(PetscSectionGetDof(section, point, &dof));
7954:         PetscCall(updatePointBC_private(section, point, dof, insert, perm, flip, clperm, values, off, array));
7955:       }
7956:       break;
7957:     case ADD_VALUES:
7958:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
7959:         const PetscInt     point = points[2 * p];
7960:         const PetscInt    *perm  = perms ? perms[p] : NULL;
7961:         const PetscScalar *flip  = flips ? flips[p] : NULL;
7962:         PetscCall(PetscSectionGetDof(section, point, &dof));
7963:         PetscCall(updatePoint_private(section, point, dof, add, PETSC_FALSE, perm, flip, clperm, values, off, array));
7964:       }
7965:       break;
7966:     case ADD_ALL_VALUES:
7967:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
7968:         const PetscInt     point = points[2 * p];
7969:         const PetscInt    *perm  = perms ? perms[p] : NULL;
7970:         const PetscScalar *flip  = flips ? flips[p] : NULL;
7971:         PetscCall(PetscSectionGetDof(section, point, &dof));
7972:         PetscCall(updatePoint_private(section, point, dof, add, PETSC_TRUE, perm, flip, clperm, values, off, array));
7973:       }
7974:       break;
7975:     case ADD_BC_VALUES:
7976:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
7977:         const PetscInt     point = points[2 * p];
7978:         const PetscInt    *perm  = perms ? perms[p] : NULL;
7979:         const PetscScalar *flip  = flips ? flips[p] : NULL;
7980:         PetscCall(PetscSectionGetDof(section, point, &dof));
7981:         PetscCall(updatePointBC_private(section, point, dof, add, perm, flip, clperm, values, off, array));
7982:       }
7983:       break;
7984:     default:
7985:       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
7986:     }
7987:     PetscCall(PetscSectionRestorePointSyms(section, numPoints, points, &perms, &flips));
7988:   }
7989:   /* Cleanup points */
7990:   PetscCall(DMPlexRestoreCompressedClosure(dm, section, point, &numPoints, &points, &clSection, &clPoints, &clp));
7991:   /* Cleanup array */
7992:   PetscCall(VecRestoreArray(v, &array));
7993:   PetscFunctionReturn(PETSC_SUCCESS);
7994: }

7996: /* Check whether the given point is in the label. If not, update the offset to skip this point */
7997: static inline PetscErrorCode CheckPoint_Private(DMLabel label, PetscInt labelId, PetscSection section, PetscInt point, PetscInt f, PetscInt *offset, PetscBool *contains)
7998: {
7999:   PetscFunctionBegin;
8000:   *contains = PETSC_TRUE;
8001:   if (label) {
8002:     PetscInt fdof;

8004:     PetscCall(DMLabelStratumHasPoint(label, labelId, point, contains));
8005:     if (!*contains) {
8006:       PetscCall(PetscSectionGetFieldDof(section, point, f, &fdof));
8007:       *offset += fdof;
8008:       PetscFunctionReturn(PETSC_SUCCESS);
8009:     }
8010:   }
8011:   PetscFunctionReturn(PETSC_SUCCESS);
8012: }

8014: /* Unlike DMPlexVecSetClosure(), this uses plex-native closure permutation, not a user-specified permutation such as DMPlexSetClosurePermutationTensor(). */
8015: 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)
8016: {
8017:   PetscSection    clSection;
8018:   IS              clPoints;
8019:   PetscScalar    *array;
8020:   PetscInt       *points = NULL;
8021:   const PetscInt *clp;
8022:   PetscInt        numFields, numPoints, p;
8023:   PetscInt        offset = 0, f;

8025:   PetscFunctionBeginHot;
8027:   if (!section) PetscCall(DMGetLocalSection(dm, &section));
8030:   PetscCall(PetscSectionGetNumFields(section, &numFields));
8031:   /* Get points */
8032:   PetscCall(DMPlexGetCompressedClosure(dm, section, point, 0, &numPoints, &points, &clSection, &clPoints, &clp));
8033:   /* Get array */
8034:   PetscCall(VecGetArray(v, &array));
8035:   /* Get values */
8036:   for (f = 0; f < numFields; ++f) {
8037:     const PetscInt    **perms = NULL;
8038:     const PetscScalar **flips = NULL;
8039:     PetscBool           contains;

8041:     if (!fieldActive[f]) {
8042:       for (p = 0; p < numPoints * 2; p += 2) {
8043:         PetscInt fdof;
8044:         PetscCall(PetscSectionGetFieldDof(section, points[p], f, &fdof));
8045:         offset += fdof;
8046:       }
8047:       continue;
8048:     }
8049:     PetscCall(PetscSectionGetFieldPointSyms(section, f, numPoints, points, &perms, &flips));
8050:     switch (mode) {
8051:     case INSERT_VALUES:
8052:       for (p = 0; p < numPoints; p++) {
8053:         const PetscInt     point = points[2 * p];
8054:         const PetscInt    *perm  = perms ? perms[p] : NULL;
8055:         const PetscScalar *flip  = flips ? flips[p] : NULL;
8056:         PetscCall(CheckPoint_Private(label, labelId, section, point, f, &offset, &contains));
8057:         if (!contains) continue;
8058:         PetscCall(updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, NULL, values, &offset, array));
8059:       }
8060:       break;
8061:     case INSERT_ALL_VALUES:
8062:       for (p = 0; p < numPoints; p++) {
8063:         const PetscInt     point = points[2 * p];
8064:         const PetscInt    *perm  = perms ? perms[p] : NULL;
8065:         const PetscScalar *flip  = flips ? flips[p] : NULL;
8066:         PetscCall(CheckPoint_Private(label, labelId, section, point, f, &offset, &contains));
8067:         if (!contains) continue;
8068:         PetscCall(updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, NULL, values, &offset, array));
8069:       }
8070:       break;
8071:     case INSERT_BC_VALUES:
8072:       for (p = 0; p < numPoints; p++) {
8073:         const PetscInt     point = points[2 * p];
8074:         const PetscInt    *perm  = perms ? perms[p] : NULL;
8075:         const PetscScalar *flip  = flips ? flips[p] : NULL;
8076:         PetscCall(CheckPoint_Private(label, labelId, section, point, f, &offset, &contains));
8077:         if (!contains) continue;
8078:         PetscCall(updatePointFieldsBC_private(section, point, perm, flip, f, Ncc, comps, insert, NULL, values, &offset, array));
8079:       }
8080:       break;
8081:     case ADD_VALUES:
8082:       for (p = 0; p < numPoints; p++) {
8083:         const PetscInt     point = points[2 * p];
8084:         const PetscInt    *perm  = perms ? perms[p] : NULL;
8085:         const PetscScalar *flip  = flips ? flips[p] : NULL;
8086:         PetscCall(CheckPoint_Private(label, labelId, section, point, f, &offset, &contains));
8087:         if (!contains) continue;
8088:         PetscCall(updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, NULL, values, &offset, array));
8089:       }
8090:       break;
8091:     case ADD_ALL_VALUES:
8092:       for (p = 0; p < numPoints; p++) {
8093:         const PetscInt     point = points[2 * p];
8094:         const PetscInt    *perm  = perms ? perms[p] : NULL;
8095:         const PetscScalar *flip  = flips ? flips[p] : NULL;
8096:         PetscCall(CheckPoint_Private(label, labelId, section, point, f, &offset, &contains));
8097:         if (!contains) continue;
8098:         PetscCall(updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, NULL, values, &offset, array));
8099:       }
8100:       break;
8101:     default:
8102:       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
8103:     }
8104:     PetscCall(PetscSectionRestoreFieldPointSyms(section, f, numPoints, points, &perms, &flips));
8105:   }
8106:   /* Cleanup points */
8107:   PetscCall(DMPlexRestoreCompressedClosure(dm, section, point, &numPoints, &points, &clSection, &clPoints, &clp));
8108:   /* Cleanup array */
8109:   PetscCall(VecRestoreArray(v, &array));
8110:   PetscFunctionReturn(PETSC_SUCCESS);
8111: }

8113: static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
8114: {
8115:   PetscMPIInt rank;

8117:   PetscFunctionBegin;
8118:   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank));
8119:   PetscCall(PetscViewerASCIIPrintf(viewer, "[%d]mat for point %" PetscInt_FMT "\n", rank, point));
8120:   for (PetscInt i = 0; i < numRIndices; i++) PetscCall(PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%" PetscInt_FMT "] = %" PetscInt_FMT "\n", rank, i, rindices[i]));
8121:   for (PetscInt i = 0; i < numCIndices; i++) PetscCall(PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%" PetscInt_FMT "] = %" PetscInt_FMT "\n", rank, i, cindices[i]));
8122:   numCIndices = numCIndices ? numCIndices : numRIndices;
8123:   if (!values) PetscFunctionReturn(PETSC_SUCCESS);
8124:   for (PetscInt i = 0; i < numRIndices; i++) {
8125:     PetscCall(PetscViewerASCIIPrintf(viewer, "[%d]", rank));
8126:     for (PetscInt j = 0; j < numCIndices; j++) {
8127: #if PetscDefined(USE_COMPLEX)
8128:       PetscCall(PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i * numCIndices + j]), (double)PetscImaginaryPart(values[i * numCIndices + j])));
8129: #else
8130:       PetscCall(PetscViewerASCIIPrintf(viewer, " %g", (double)values[i * numCIndices + j]));
8131: #endif
8132:     }
8133:     PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
8134:   }
8135:   PetscFunctionReturn(PETSC_SUCCESS);
8136: }

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

8141:   Input Parameters:
8142: + section - The section for this data layout
8143: . islocal - Is the section (and thus indices being requested) local or global?
8144: . point   - The point contributing dofs with these indices
8145: . off     - The global offset of this point
8146: . loff    - The local offset of each field
8147: . setBC   - The flag determining whether to include indices of boundary values
8148: . perm    - A permutation of the dofs on this point, or NULL
8149: - indperm - A permutation of the entire indices array, or NULL

8151:   Output Parameter:
8152: . indices - Indices for dofs on this point

8154:   Level: developer

8156:   Note: The indices could be local or global, depending on the value of `off`.
8157: */
8158: PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscBool islocal, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], const PetscInt indperm[], PetscInt indices[])
8159: {
8160:   PetscInt        dof;   /* The number of unknowns on this point */
8161:   PetscInt        cdof;  /* The number of constraints on this point */
8162:   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
8163:   PetscInt        cind = 0, k;

8165:   PetscFunctionBegin;
8166:   PetscCheck(islocal || !setBC, PetscObjectComm((PetscObject)section), PETSC_ERR_ARG_INCOMP, "setBC incompatible with global indices; use a local section or disable setBC");
8167:   PetscCall(PetscSectionGetDof(section, point, &dof));
8168:   PetscCall(PetscSectionGetConstraintDof(section, point, &cdof));
8169:   if (!cdof || setBC) {
8170:     for (k = 0; k < dof; ++k) {
8171:       const PetscInt preind = perm ? *loff + perm[k] : *loff + k;
8172:       const PetscInt ind    = indperm ? indperm[preind] : preind;

8174:       indices[ind] = off + k;
8175:     }
8176:   } else {
8177:     PetscCall(PetscSectionGetConstraintIndices(section, point, &cdofs));
8178:     for (k = 0; k < dof; ++k) {
8179:       const PetscInt preind = perm ? *loff + perm[k] : *loff + k;
8180:       const PetscInt ind    = indperm ? indperm[preind] : preind;

8182:       if (cind < cdof && k == cdofs[cind]) {
8183:         /* Insert check for returning constrained indices */
8184:         indices[ind] = -(off + k + 1);
8185:         ++cind;
8186:       } else {
8187:         indices[ind] = off + k - (islocal ? 0 : cind);
8188:       }
8189:     }
8190:   }
8191:   *loff += dof;
8192:   PetscFunctionReturn(PETSC_SUCCESS);
8193: }

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

8198:  Input Parameters:
8199: + section - a section (global or local)
8200: - islocal - `PETSC_TRUE` if requesting local indices (i.e., section is local); `PETSC_FALSE` for global
8201: . point - point within section
8202: . off - The offset of this point in the (local or global) indexed space - should match islocal and (usually) the section
8203: . foffs - array of length numFields containing the offset in canonical point ordering (the location in indices) of each field
8204: . setBC - identify constrained (boundary condition) points via involution.
8205: . perms - perms[f][permsoff][:] is a permutation of dofs within each field
8206: . permsoff - offset
8207: - indperm - index permutation

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

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

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

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

8226:  Example:
8227:  Suppose a point contains one field with three components, and for which the unconstrained indices are {10, 11, 12}.
8228:  When the middle component is constrained, we get the array {10, -12, 12} for (islocal=TRUE, setBC=FALSE).
8229:  Note that -12 is the involution of 11, so the user can involute negative indices to recover local indices.
8230:  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.

8232:  Level: developer
8233: */
8234: 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[])
8235: {
8236:   PetscInt numFields, foff, f;

8238:   PetscFunctionBegin;
8239:   PetscCheck(islocal || !setBC, PetscObjectComm((PetscObject)section), PETSC_ERR_ARG_INCOMP, "setBC incompatible with global indices; use a local section or disable setBC");
8240:   PetscCall(PetscSectionGetNumFields(section, &numFields));
8241:   for (f = 0, foff = 0; f < numFields; ++f) {
8242:     PetscInt        fdof, cfdof;
8243:     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
8244:     PetscInt        cind = 0, b;
8245:     const PetscInt *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;

8247:     PetscCall(PetscSectionGetFieldDof(section, point, f, &fdof));
8248:     PetscCall(PetscSectionGetFieldConstraintDof(section, point, f, &cfdof));
8249:     if (!cfdof || setBC) {
8250:       for (b = 0; b < fdof; ++b) {
8251:         const PetscInt preind = perm ? foffs[f] + perm[b] : foffs[f] + b;
8252:         const PetscInt ind    = indperm ? indperm[preind] : preind;

8254:         indices[ind] = off + foff + b;
8255:       }
8256:     } else {
8257:       PetscCall(PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs));
8258:       for (b = 0; b < fdof; ++b) {
8259:         const PetscInt preind = perm ? foffs[f] + perm[b] : foffs[f] + b;
8260:         const PetscInt ind    = indperm ? indperm[preind] : preind;

8262:         if (cind < cfdof && b == fcdofs[cind]) {
8263:           indices[ind] = -(off + foff + b + 1);
8264:           ++cind;
8265:         } else {
8266:           indices[ind] = off + foff + b - (islocal ? 0 : cind);
8267:         }
8268:       }
8269:     }
8270:     foff += (setBC || islocal ? fdof : (fdof - cfdof));
8271:     foffs[f] += fdof;
8272:   }
8273:   PetscFunctionReturn(PETSC_SUCCESS);
8274: }

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

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

8281:  Notes:
8282:  The semantics of this function relate to that of setBC=FALSE in DMPlexGetIndicesPointFields_Internal.
8283:  Since this function uses global indices, setBC=TRUE would be invalid, so no such argument exists.
8284: */
8285: static PetscErrorCode DMPlexGetIndicesPointFieldsSplit_Internal(PetscSection section, PetscSection globalSection, PetscInt point, PetscInt foffs[], const PetscInt ***perms, PetscInt permsoff, const PetscInt indperm[], PetscInt indices[])
8286: {
8287:   PetscInt numFields, foff, f;

8289:   PetscFunctionBegin;
8290:   PetscCall(PetscSectionGetNumFields(section, &numFields));
8291:   for (f = 0; f < numFields; ++f) {
8292:     PetscInt        fdof, cfdof;
8293:     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
8294:     PetscInt        cind = 0, b;
8295:     const PetscInt *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;

8297:     PetscCall(PetscSectionGetFieldDof(section, point, f, &fdof));
8298:     PetscCall(PetscSectionGetFieldConstraintDof(section, point, f, &cfdof));
8299:     PetscCall(PetscSectionGetFieldOffset(globalSection, point, f, &foff));
8300:     if (!cfdof) {
8301:       for (b = 0; b < fdof; ++b) {
8302:         const PetscInt preind = perm ? foffs[f] + perm[b] : foffs[f] + b;
8303:         const PetscInt ind    = indperm ? indperm[preind] : preind;

8305:         indices[ind] = foff + b;
8306:       }
8307:     } else {
8308:       PetscCall(PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs));
8309:       for (b = 0; b < fdof; ++b) {
8310:         const PetscInt preind = perm ? foffs[f] + perm[b] : foffs[f] + b;
8311:         const PetscInt ind    = indperm ? indperm[preind] : preind;

8313:         if (cind < cfdof && b == fcdofs[cind]) {
8314:           indices[ind] = -(foff + b + 1);
8315:           ++cind;
8316:         } else {
8317:           indices[ind] = foff + b - cind;
8318:         }
8319:       }
8320:     }
8321:     foffs[f] += fdof;
8322:   }
8323:   PetscFunctionReturn(PETSC_SUCCESS);
8324: }

8326: static PetscErrorCode DMPlexAnchorsGetSubMatIndices(PetscInt nPoints, const PetscInt pnts[], PetscSection section, PetscSection cSec, PetscInt tmpIndices[], PetscInt fieldOffsets[], PetscInt indices[], const PetscInt ***perms)
8327: {
8328:   PetscInt numFields, sStart, sEnd, cStart, cEnd;

8330:   PetscFunctionBegin;
8331:   PetscCall(PetscSectionGetNumFields(section, &numFields));
8332:   PetscCall(PetscSectionGetChart(section, &sStart, &sEnd));
8333:   PetscCall(PetscSectionGetChart(cSec, &cStart, &cEnd));
8334:   for (PetscInt p = 0; p < nPoints; p++) {
8335:     PetscInt     b       = pnts[2 * p];
8336:     PetscInt     bSecDof = 0, bOff;
8337:     PetscInt     cSecDof = 0;
8338:     PetscSection indices_section;

8340:     if (b >= sStart && b < sEnd) PetscCall(PetscSectionGetDof(section, b, &bSecDof));
8341:     if (!bSecDof) continue;
8342:     if (b >= cStart && b < cEnd) PetscCall(PetscSectionGetDof(cSec, b, &cSecDof));
8343:     indices_section = cSecDof > 0 ? cSec : section;
8344:     if (numFields) {
8345:       PetscInt fStart[32], fEnd[32];

8347:       fStart[0] = 0;
8348:       fEnd[0]   = 0;
8349:       for (PetscInt f = 0; f < numFields; f++) {
8350:         PetscInt fDof = 0;

8352:         PetscCall(PetscSectionGetFieldDof(indices_section, b, f, &fDof));
8353:         fStart[f + 1] = fStart[f] + fDof;
8354:         fEnd[f + 1]   = fStart[f + 1];
8355:       }
8356:       PetscCall(PetscSectionGetOffset(indices_section, b, &bOff));
8357:       // only apply permutations on one side
8358:       PetscCall(DMPlexGetIndicesPointFields_Internal(indices_section, PETSC_TRUE, b, bOff, fEnd, PETSC_TRUE, perms, perms ? p : -1, NULL, tmpIndices));
8359:       for (PetscInt f = 0; f < numFields; f++) {
8360:         for (PetscInt i = fStart[f]; i < fEnd[f]; i++) indices[fieldOffsets[f]++] = (cSecDof > 0) ? tmpIndices[i] : -(tmpIndices[i] + 1);
8361:       }
8362:     } else {
8363:       PetscInt bEnd = 0;

8365:       PetscCall(PetscSectionGetOffset(indices_section, b, &bOff));
8366:       PetscCall(DMPlexGetIndicesPoint_Internal(indices_section, PETSC_TRUE, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, NULL, tmpIndices));

8368:       for (PetscInt i = 0; i < bEnd; i++) indices[fieldOffsets[0]++] = (cSecDof > 0) ? tmpIndices[i] : -(tmpIndices[i] + 1);
8369:     }
8370:   }
8371:   PetscFunctionReturn(PETSC_SUCCESS);
8372: }

8374: PETSC_INTERN PetscErrorCode DMPlexAnchorsGetSubMatModification(DM dm, PetscSection section, PetscInt numPoints, PetscInt numIndices, const PetscInt points[], const PetscInt ***perms, PetscInt *outNumPoints, PetscInt *outNumIndices, PetscInt *outPoints[], PetscInt offsets[], PetscScalar *outMat[])
8375: {
8376:   Mat             cMat;
8377:   PetscSection    aSec, cSec;
8378:   IS              aIS;
8379:   PetscInt        aStart = -1, aEnd = -1;
8380:   PetscInt        sStart = -1, sEnd = -1;
8381:   PetscInt        cStart = -1, cEnd = -1;
8382:   const PetscInt *anchors;
8383:   PetscInt        numFields, p;
8384:   PetscInt        newNumPoints = 0, newNumIndices = 0;
8385:   PetscInt       *newPoints, *indices, *newIndices, *tmpIndices, *tmpNewIndices;
8386:   PetscInt        oldOffsets[32];
8387:   PetscInt        newOffsets[32];
8388:   PetscInt        oldOffsetsCopy[32];
8389:   PetscInt        newOffsetsCopy[32];
8390:   PetscScalar    *modMat         = NULL;
8391:   PetscBool       anyConstrained = PETSC_FALSE;

8393:   PetscFunctionBegin;
8396:   PetscCall(PetscSectionGetNumFields(section, &numFields));

8398:   PetscCall(DMPlexGetAnchors(dm, &aSec, &aIS));
8399:   /* if there are point-to-point constraints */
8400:   if (aSec) {
8401:     PetscCall(PetscArrayzero(newOffsets, 32));
8402:     PetscCall(PetscArrayzero(oldOffsets, 32));
8403:     PetscCall(ISGetIndices(aIS, &anchors));
8404:     PetscCall(PetscSectionGetChart(aSec, &aStart, &aEnd));
8405:     PetscCall(PetscSectionGetChart(section, &sStart, &sEnd));
8406:     /* figure out how many points are going to be in the new element matrix
8407:      * (we allow double counting, because it's all just going to be summed
8408:      * into the global matrix anyway) */
8409:     for (p = 0; p < 2 * numPoints; p += 2) {
8410:       PetscInt b    = points[p];
8411:       PetscInt bDof = 0, bSecDof = 0;

8413:       if (b >= sStart && b < sEnd) PetscCall(PetscSectionGetDof(section, b, &bSecDof));
8414:       if (!bSecDof) continue;

8416:       for (PetscInt f = 0; f < numFields; f++) {
8417:         PetscInt fDof = 0;

8419:         PetscCall(PetscSectionGetFieldDof(section, b, f, &fDof));
8420:         oldOffsets[f + 1] += fDof;
8421:       }
8422:       if (b >= aStart && b < aEnd) PetscCall(PetscSectionGetDof(aSec, b, &bDof));
8423:       if (bDof) {
8424:         /* this point is constrained */
8425:         /* it is going to be replaced by its anchors */
8426:         PetscInt bOff;

8428:         PetscCall(PetscSectionGetOffset(aSec, b, &bOff));
8429:         for (PetscInt q = 0; q < bDof; q++) {
8430:           PetscInt a    = anchors[bOff + q];
8431:           PetscInt aDof = 0;

8433:           if (a >= sStart && a < sEnd) PetscCall(PetscSectionGetDof(section, a, &aDof));
8434:           if (aDof) {
8435:             anyConstrained = PETSC_TRUE;
8436:             newNumPoints += 1;
8437:           }
8438:           newNumIndices += aDof;
8439:           for (PetscInt f = 0; f < numFields; ++f) {
8440:             PetscInt fDof = 0;

8442:             if (a >= sStart && a < sEnd) PetscCall(PetscSectionGetFieldDof(section, a, f, &fDof));
8443:             newOffsets[f + 1] += fDof;
8444:           }
8445:         }
8446:       } else {
8447:         /* this point is not constrained */
8448:         newNumPoints++;
8449:         newNumIndices += bSecDof;
8450:         for (PetscInt f = 0; f < numFields; ++f) {
8451:           PetscInt fDof;

8453:           PetscCall(PetscSectionGetFieldDof(section, b, f, &fDof));
8454:           newOffsets[f + 1] += fDof;
8455:         }
8456:       }
8457:     }
8458:   }
8459:   if (!anyConstrained) {
8460:     if (outNumPoints) *outNumPoints = 0;
8461:     if (outNumIndices) *outNumIndices = 0;
8462:     if (outPoints) *outPoints = NULL;
8463:     if (outMat) *outMat = NULL;
8464:     if (aSec) PetscCall(ISRestoreIndices(aIS, &anchors));
8465:     PetscFunctionReturn(PETSC_SUCCESS);
8466:   }

8468:   if (outNumPoints) *outNumPoints = newNumPoints;
8469:   if (outNumIndices) *outNumIndices = newNumIndices;

8471:   for (PetscInt f = 0; f < numFields; ++f) newOffsets[f + 1] += newOffsets[f];
8472:   for (PetscInt f = 0; f < numFields; ++f) oldOffsets[f + 1] += oldOffsets[f];

8474:   if (!outPoints && !outMat) {
8475:     if (offsets) {
8476:       for (PetscInt f = 0; f <= numFields; f++) offsets[f] = newOffsets[f];
8477:     }
8478:     if (aSec) PetscCall(ISRestoreIndices(aIS, &anchors));
8479:     PetscFunctionReturn(PETSC_SUCCESS);
8480:   }

8482:   PetscCheck(!numFields || newOffsets[numFields] == newNumIndices, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %" PetscInt_FMT " should be %" PetscInt_FMT, newOffsets[numFields], newNumIndices);
8483:   PetscCheck(!numFields || oldOffsets[numFields] == numIndices, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %" PetscInt_FMT " should be %" PetscInt_FMT, oldOffsets[numFields], numIndices);

8485:   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, NULL));
8486:   PetscCall(PetscSectionGetChart(cSec, &cStart, &cEnd));

8488:   /* output arrays */
8489:   PetscCall(DMGetWorkArray(dm, 2 * newNumPoints, MPIU_INT, &newPoints));
8490:   PetscCall(PetscArrayzero(newPoints, 2 * newNumPoints));

8492:   // get the new Points
8493:   for (PetscInt p = 0, newP = 0; p < numPoints; p++) {
8494:     PetscInt b    = points[2 * p];
8495:     PetscInt bDof = 0, bSecDof = 0, bOff;

8497:     if (b >= sStart && b < sEnd) PetscCall(PetscSectionGetDof(section, b, &bSecDof));
8498:     if (!bSecDof) continue;
8499:     if (b >= aStart && b < aEnd) PetscCall(PetscSectionGetDof(aSec, b, &bDof));
8500:     if (bDof) {
8501:       PetscCall(PetscSectionGetOffset(aSec, b, &bOff));
8502:       for (PetscInt q = 0; q < bDof; q++) {
8503:         PetscInt a = anchors[bOff + q], aDof = 0;

8505:         if (a >= sStart && a < sEnd) PetscCall(PetscSectionGetDof(section, a, &aDof));
8506:         if (aDof) {
8507:           newPoints[2 * newP]     = a;
8508:           newPoints[2 * newP + 1] = 0; // orientations are accounted for in constructing the matrix, newly added points are in default orientation
8509:           newP++;
8510:         }
8511:       }
8512:     } else {
8513:       newPoints[2 * newP]     = b;
8514:       newPoints[2 * newP + 1] = points[2 * p + 1];
8515:       newP++;
8516:     }
8517:   }

8519:   if (outMat) {
8520:     PetscScalar *tmpMat;
8521:     PetscCall(PetscArraycpy(oldOffsetsCopy, oldOffsets, 32));
8522:     PetscCall(PetscArraycpy(newOffsetsCopy, newOffsets, 32));

8524:     PetscCall(DMGetWorkArray(dm, numIndices, MPIU_INT, &indices));
8525:     PetscCall(DMGetWorkArray(dm, numIndices, MPIU_INT, &tmpIndices));
8526:     PetscCall(DMGetWorkArray(dm, newNumIndices, MPIU_INT, &newIndices));
8527:     PetscCall(DMGetWorkArray(dm, newNumIndices, MPIU_INT, &tmpNewIndices));

8529:     for (PetscInt i = 0; i < numIndices; i++) indices[i] = -1;
8530:     for (PetscInt i = 0; i < newNumIndices; i++) newIndices[i] = -1;

8532:     PetscCall(DMPlexAnchorsGetSubMatIndices(numPoints, points, section, cSec, tmpIndices, oldOffsetsCopy, indices, perms));
8533:     PetscCall(DMPlexAnchorsGetSubMatIndices(newNumPoints, newPoints, section, section, tmpNewIndices, newOffsetsCopy, newIndices, NULL));

8535:     PetscCall(DMGetWorkArray(dm, numIndices * newNumIndices, MPIU_SCALAR, &modMat));
8536:     PetscCall(DMGetWorkArray(dm, numIndices * newNumIndices, MPIU_SCALAR, &tmpMat));
8537:     PetscCall(PetscArrayzero(modMat, newNumIndices * numIndices));
8538:     // for each field, insert the anchor modification into modMat
8539:     for (PetscInt f = 0; f < PetscMax(1, numFields); f++) {
8540:       PetscInt fStart    = oldOffsets[f];
8541:       PetscInt fNewStart = newOffsets[f];
8542:       for (PetscInt p = 0, o = fStart, oNew = fNewStart; p < numPoints; p++) {
8543:         PetscInt b    = points[2 * p];
8544:         PetscInt bDof = 0, bSecDof = 0, bOff;

8546:         if (b >= sStart && b < sEnd) {
8547:           if (numFields) PetscCall(PetscSectionGetFieldDof(section, b, f, &bSecDof));
8548:           else PetscCall(PetscSectionGetDof(section, b, &bSecDof));
8549:         }
8550:         if (!bSecDof) continue;
8551:         if (b >= aStart && b < aEnd) PetscCall(PetscSectionGetDof(aSec, b, &bDof));
8552:         if (bDof) {
8553:           PetscCall(PetscSectionGetOffset(aSec, b, &bOff));
8554:           for (PetscInt q = 0; q < bDof; q++) {
8555:             PetscInt a = anchors[bOff + q], aDof = 0;

8557:             if (a >= sStart && a < sEnd) {
8558:               if (numFields) PetscCall(PetscSectionGetFieldDof(section, a, f, &aDof));
8559:               else PetscCall(PetscSectionGetDof(section, a, &aDof));
8560:             }
8561:             if (aDof) {
8562:               PetscCall(MatGetValues(cMat, bSecDof, &indices[o], aDof, &newIndices[oNew], tmpMat));
8563:               for (PetscInt d = 0; d < bSecDof; d++) {
8564:                 for (PetscInt e = 0; e < aDof; e++) modMat[(o + d) * newNumIndices + oNew + e] = tmpMat[d * aDof + e];
8565:               }
8566:             }
8567:             oNew += aDof;
8568:           }
8569:         } else {
8570:           // Insert the identity matrix in this block
8571:           for (PetscInt d = 0; d < bSecDof; d++) modMat[(o + d) * newNumIndices + oNew + d] = 1;
8572:           oNew += bSecDof;
8573:         }
8574:         o += bSecDof;
8575:       }
8576:     }

8578:     *outMat = modMat;

8580:     PetscCall(DMRestoreWorkArray(dm, numIndices * newNumIndices, MPIU_SCALAR, &tmpMat));
8581:     PetscCall(DMRestoreWorkArray(dm, newNumIndices, MPIU_INT, &tmpNewIndices));
8582:     PetscCall(DMRestoreWorkArray(dm, newNumIndices, MPIU_INT, &newIndices));
8583:     PetscCall(DMRestoreWorkArray(dm, numIndices, MPIU_INT, &tmpIndices));
8584:     PetscCall(DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices));
8585:   }
8586:   PetscCall(ISRestoreIndices(aIS, &anchors));

8588:   /* output */
8589:   if (outPoints) {
8590:     *outPoints = newPoints;
8591:   } else {
8592:     PetscCall(DMRestoreWorkArray(dm, 2 * newNumPoints, MPIU_INT, &newPoints));
8593:   }
8594:   for (PetscInt f = 0; f <= numFields; f++) offsets[f] = newOffsets[f];
8595:   PetscFunctionReturn(PETSC_SUCCESS);
8596: }

8598: PETSC_INTERN PetscErrorCode DMPlexAnchorsModifyMat_Internal(DM dm, PetscSection section, PetscInt numPoints, PetscInt numIndices, const PetscInt points[], const PetscInt ***perms, PetscInt numRows, PetscInt numCols, const PetscScalar values[], PetscInt *outNumPoints, PetscInt *outNumIndices, PetscInt *outPoints[], PetscScalar *outValues[], PetscInt offsets[], PetscBool multiplyRight, PetscBool multiplyLeft)
8599: {
8600:   PetscScalar *modMat        = NULL;
8601:   PetscInt     newNumIndices = -1;

8603:   PetscFunctionBegin;
8604:   /* If M is the matrix represented by values, get the matrix C such that we will add M * C (or, if multiplyLeft, C^T * M * C) into the global matrix.
8605:      modMat is that matrix C */
8606:   PetscCall(DMPlexAnchorsGetSubMatModification(dm, section, numPoints, numIndices, points, perms, outNumPoints, &newNumIndices, outPoints, offsets, outValues ? &modMat : NULL));
8607:   if (outNumIndices) *outNumIndices = newNumIndices;
8608:   if (modMat) {
8609:     const PetscScalar *newValues = values;

8611:     if (multiplyRight) {
8612:       PetscScalar *newNewValues = NULL;
8613:       PetscBLASInt M, N, K;
8614:       PetscScalar  a = 1.0, b = 0.0;

8616:       PetscCheck(numCols == numIndices, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_SIZ, "values matrix has the wrong number of columns: %" PetscInt_FMT ", expected %" PetscInt_FMT, numCols, numIndices);

8618:       PetscCall(PetscBLASIntCast(newNumIndices, &M));
8619:       PetscCall(PetscBLASIntCast(numRows, &N));
8620:       PetscCall(PetscBLASIntCast(numIndices, &K));
8621:       PetscCall(DMGetWorkArray(dm, numRows * newNumIndices, MPIU_SCALAR, &newNewValues));
8622:       // row-major to column-major conversion, right multiplication becomes left multiplication
8623:       PetscCallBLAS("BLASgemm", BLASgemm_("N", "N", &M, &N, &K, &a, modMat, &M, newValues, &K, &b, newNewValues, &M));
8624:       numCols   = newNumIndices;
8625:       newValues = newNewValues;
8626:     }

8628:     if (multiplyLeft) {
8629:       PetscScalar *newNewValues = NULL;
8630:       PetscBLASInt M, N, K;
8631:       PetscScalar  a = 1.0, b = 0.0;

8633:       PetscCheck(numRows == numIndices, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_SIZ, "values matrix has the wrong number of rows: %" PetscInt_FMT ", expected %" PetscInt_FMT, numRows, numIndices);

8635:       PetscCall(PetscBLASIntCast(numCols, &M));
8636:       PetscCall(PetscBLASIntCast(newNumIndices, &N));
8637:       PetscCall(PetscBLASIntCast(numIndices, &K));
8638:       PetscCall(DMGetWorkArray(dm, newNumIndices * numCols, MPIU_SCALAR, &newNewValues));
8639:       // row-major to column-major conversion, left multiplication becomes right multiplication
8640:       PetscCallBLAS("BLASgemm", BLASgemm_("N", "T", &M, &N, &K, &a, newValues, &M, modMat, &N, &b, newNewValues, &M));
8641:       if (newValues != values) PetscCall(DMRestoreWorkArray(dm, numIndices * newNumIndices, MPIU_SCALAR, &newValues));
8642:       newValues = newNewValues;
8643:     }
8644:     *outValues = (PetscScalar *)newValues;
8645:     PetscCall(DMRestoreWorkArray(dm, numIndices * newNumIndices, MPIU_SCALAR, &modMat));
8646:   }
8647:   PetscFunctionReturn(PETSC_SUCCESS);
8648: }

8650: PETSC_INTERN 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)
8651: {
8652:   PetscFunctionBegin;
8653:   PetscCall(DMPlexAnchorsModifyMat_Internal(dm, section, numPoints, numIndices, points, perms, numIndices, numIndices, values, outNumPoints, outNumIndices, outPoints, outValues, offsets, PETSC_TRUE, multiplyLeft));
8654:   PetscFunctionReturn(PETSC_SUCCESS);
8655: }

8657: static PetscErrorCode DMPlexGetClosureIndicesSize_Internal(DM dm, PetscSection section, PetscInt point, PetscInt *closureSize)
8658: {
8659:   /* Closure ordering */
8660:   PetscSection    clSection;
8661:   IS              clPoints;
8662:   const PetscInt *clp;
8663:   PetscInt       *points;
8664:   PetscInt        Ncl, Ni = 0;

8666:   PetscFunctionBeginHot;
8667:   PetscCall(DMPlexGetCompressedClosure(dm, section, point, 0, &Ncl, &points, &clSection, &clPoints, &clp));
8668:   for (PetscInt p = 0; p < Ncl * 2; p += 2) {
8669:     PetscInt dof;

8671:     PetscCall(PetscSectionGetDof(section, points[p], &dof));
8672:     Ni += dof;
8673:   }
8674:   PetscCall(DMPlexRestoreCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp));
8675:   *closureSize = Ni;
8676:   PetscFunctionReturn(PETSC_SUCCESS);
8677: }

8679: static PetscErrorCode DMPlexGetClosureIndices_Internal(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscBool useClPerm, PetscInt *numRows, PetscInt *numCols, PetscInt *indices[], PetscInt outOffsets[], PetscScalar *values[], PetscBool multiplyRight, PetscBool multiplyLeft)
8680: {
8681:   /* Closure ordering */
8682:   PetscSection    clSection;
8683:   IS              clPoints;
8684:   const PetscInt *clp;
8685:   PetscInt       *points;
8686:   const PetscInt *clperm = NULL;
8687:   /* Dof permutation and sign flips */
8688:   const PetscInt    **perms[32] = {NULL};
8689:   const PetscScalar **flips[32] = {NULL};
8690:   PetscScalar        *valCopy   = NULL;
8691:   /* Hanging node constraints */
8692:   PetscInt    *pointsC = NULL;
8693:   PetscScalar *valuesC = NULL;
8694:   PetscInt     NclC, NiC;

8696:   PetscInt *idx;
8697:   PetscInt  Nf, Ncl, Ni = 0, offsets[32], p, f;
8698:   PetscBool isLocal = (section == idxSection) ? PETSC_TRUE : PETSC_FALSE;
8699:   PetscInt  idxStart, idxEnd;
8700:   PetscInt  nRows, nCols;

8702:   PetscFunctionBeginHot;
8706:   PetscAssertPointer(numRows, 6);
8707:   PetscAssertPointer(numCols, 7);
8708:   if (indices) PetscAssertPointer(indices, 8);
8709:   if (outOffsets) PetscAssertPointer(outOffsets, 9);
8710:   if (values) PetscAssertPointer(values, 10);
8711:   PetscCall(PetscSectionGetNumFields(section, &Nf));
8712:   PetscCheck(Nf <= 31, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %" PetscInt_FMT " limited to 31", Nf);
8713:   PetscCall(PetscArrayzero(offsets, 32));
8714:   /* 1) Get points in closure */
8715:   PetscCall(DMPlexGetCompressedClosure(dm, section, point, 0, &Ncl, &points, &clSection, &clPoints, &clp));
8716:   if (useClPerm) {
8717:     PetscInt depth, clsize;
8718:     PetscCall(DMPlexGetPointDepth(dm, point, &depth));
8719:     for (clsize = 0, p = 0; p < Ncl; p++) {
8720:       PetscInt dof;
8721:       PetscCall(PetscSectionGetDof(section, points[2 * p], &dof));
8722:       clsize += dof;
8723:     }
8724:     PetscCall(PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject)dm, depth, clsize, &clperm));
8725:   }
8726:   /* 2) Get number of indices on these points and field offsets from section */
8727:   for (p = 0; p < Ncl * 2; p += 2) {
8728:     PetscInt dof, fdof;

8730:     PetscCall(PetscSectionGetDof(section, points[p], &dof));
8731:     for (f = 0; f < Nf; ++f) {
8732:       PetscCall(PetscSectionGetFieldDof(section, points[p], f, &fdof));
8733:       offsets[f + 1] += fdof;
8734:     }
8735:     Ni += dof;
8736:   }
8737:   if (*numRows == -1) *numRows = Ni;
8738:   if (*numCols == -1) *numCols = Ni;
8739:   nRows = *numRows;
8740:   nCols = *numCols;
8741:   for (f = 1; f < Nf; ++f) offsets[f + 1] += offsets[f];
8742:   PetscCheck(!Nf || offsets[Nf] == Ni, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %" PetscInt_FMT " should be %" PetscInt_FMT, offsets[Nf], Ni);
8743:   /* 3) Get symmetries and sign flips. Apply sign flips to values if passed in (only works for square values matrix) */
8744:   if (multiplyRight) PetscCheck(nCols == Ni, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_SIZ, "Expected %" PetscInt_FMT " columns, got %" PetscInt_FMT, Ni, nCols);
8745:   if (multiplyLeft) PetscCheck(nRows == Ni, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_SIZ, "Expected %" PetscInt_FMT " rows, got %" PetscInt_FMT, Ni, nRows);
8746:   for (f = 0; f < PetscMax(1, Nf); ++f) {
8747:     if (Nf) PetscCall(PetscSectionGetFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]));
8748:     else PetscCall(PetscSectionGetPointSyms(section, Ncl, points, &perms[f], &flips[f]));
8749:     /* may need to apply sign changes to the element matrix */
8750:     if (values && flips[f]) {
8751:       PetscInt foffset = offsets[f];

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

8757:         if (!Nf) PetscCall(PetscSectionGetDof(section, pnt, &fdof));
8758:         else PetscCall(PetscSectionGetFieldDof(section, pnt, f, &fdof));
8759:         if (flip) {
8760:           PetscInt i, j, k;

8762:           if (!valCopy) {
8763:             PetscCall(DMGetWorkArray(dm, Ni * Ni, MPIU_SCALAR, &valCopy));
8764:             for (j = 0; j < Ni * Ni; ++j) valCopy[j] = (*values)[j];
8765:             *values = valCopy;
8766:           }
8767:           for (i = 0; i < fdof; ++i) {
8768:             PetscScalar fval = flip[i];

8770:             if (multiplyRight) {
8771:               for (k = 0; k < nRows; ++k) valCopy[Ni * k + (foffset + i)] *= fval;
8772:             }
8773:             if (multiplyLeft) {
8774:               for (k = 0; k < nCols; ++k) valCopy[nCols * (foffset + i) + k] *= fval;
8775:             }
8776:           }
8777:         }
8778:         foffset += fdof;
8779:       }
8780:     }
8781:   }
8782:   /* 4) Apply hanging node constraints. Get new symmetries and replace all storage with constrained storage */
8783:   PetscCall(DMPlexAnchorsModifyMat_Internal(dm, section, Ncl, Ni, points, perms, nRows, nCols, values ? *values : NULL, &NclC, &NiC, &pointsC, values ? &valuesC : NULL, offsets, multiplyRight, multiplyLeft));
8784:   if (NclC) {
8785:     if (multiplyRight) *numCols = NiC;
8786:     if (multiplyLeft) *numRows = NiC;
8787:     if (valCopy) PetscCall(DMRestoreWorkArray(dm, Ni * Ni, MPIU_SCALAR, &valCopy));
8788:     for (f = 0; f < PetscMax(1, Nf); ++f) {
8789:       if (Nf) PetscCall(PetscSectionRestoreFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]));
8790:       else PetscCall(PetscSectionRestorePointSyms(section, Ncl, points, &perms[f], &flips[f]));
8791:     }
8792:     for (f = 0; f < PetscMax(1, Nf); ++f) {
8793:       if (Nf) PetscCall(PetscSectionGetFieldPointSyms(section, f, NclC, pointsC, &perms[f], &flips[f]));
8794:       else PetscCall(PetscSectionGetPointSyms(section, NclC, pointsC, &perms[f], &flips[f]));
8795:     }
8796:     PetscCall(DMPlexRestoreCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp));
8797:     Ncl    = NclC;
8798:     Ni     = NiC;
8799:     points = pointsC;
8800:     if (values) *values = valuesC;
8801:   }
8802:   /* 5) Calculate indices */
8803:   PetscCall(DMGetWorkArray(dm, Ni, MPIU_INT, &idx));
8804:   PetscCall(PetscSectionGetChart(idxSection, &idxStart, &idxEnd));
8805:   if (Nf) {
8806:     PetscInt  idxOff;
8807:     PetscBool useFieldOffsets;

8809:     if (outOffsets) {
8810:       for (f = 0; f <= Nf; f++) outOffsets[f] = offsets[f];
8811:     }
8812:     PetscCall(PetscSectionGetUseFieldOffsets(idxSection, &useFieldOffsets));
8813:     if (useFieldOffsets) {
8814:       for (p = 0; p < Ncl; ++p) {
8815:         const PetscInt pnt = points[p * 2];

8817:         PetscCall(DMPlexGetIndicesPointFieldsSplit_Internal(section, idxSection, pnt, offsets, perms, p, clperm, idx));
8818:       }
8819:     } else {
8820:       for (p = 0; p < Ncl; ++p) {
8821:         const PetscInt pnt = points[p * 2];

8823:         if (pnt < idxStart || pnt >= idxEnd) continue;
8824:         PetscCall(PetscSectionGetOffset(idxSection, pnt, &idxOff));
8825:         /* Note that we pass a local section even though we're using global offsets.  This is because global sections do
8826:          * not (at the time of this writing) have fields set. They probably should, in which case we would pass the
8827:          * global section. */
8828:         PetscCall(DMPlexGetIndicesPointFields_Internal(section, isLocal, pnt, idxOff < 0 ? -(idxOff + 1) : idxOff, offsets, PETSC_FALSE, perms, p, clperm, idx));
8829:       }
8830:     }
8831:   } else {
8832:     PetscInt off = 0, idxOff;

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

8838:       if (pnt < idxStart || pnt >= idxEnd) continue;
8839:       PetscCall(PetscSectionGetOffset(idxSection, pnt, &idxOff));
8840:       /* Note that we pass a local section even though we're using global offsets.  This is because global sections do
8841:        * not (at the time of this writing) have fields set. They probably should, in which case we would pass the global section. */
8842:       PetscCall(DMPlexGetIndicesPoint_Internal(section, isLocal, pnt, idxOff < 0 ? -(idxOff + 1) : idxOff, &off, PETSC_FALSE, perm, clperm, idx));
8843:     }
8844:   }
8845:   /* 6) Cleanup */
8846:   for (f = 0; f < PetscMax(1, Nf); ++f) {
8847:     if (Nf) PetscCall(PetscSectionRestoreFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]));
8848:     else PetscCall(PetscSectionRestorePointSyms(section, Ncl, points, &perms[f], &flips[f]));
8849:   }
8850:   if (NclC) {
8851:     PetscCall(DMRestoreWorkArray(dm, NclC * 2, MPIU_INT, &pointsC));
8852:   } else {
8853:     PetscCall(DMPlexRestoreCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp));
8854:   }

8856:   if (indices) *indices = idx;
8857:   PetscFunctionReturn(PETSC_SUCCESS);
8858: }

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

8863:   Not collective

8865:   Input Parameters:
8866: + dm         - The `DM`
8867: . section    - The `PetscSection` describing the points (a local section)
8868: . idxSection - The `PetscSection` from which to obtain indices (may be local or global)
8869: . point      - The point defining the closure
8870: - useClPerm  - Use the closure point permutation if available

8872:   Output Parameters:
8873: + numIndices - The number of dof indices in the closure of point with the input sections
8874: . indices    - The dof indices
8875: . outOffsets - Array, of length the number of fields plus 1, to write the field offsets into, or `NULL`
8876: - values     - The input values, which may be modified if sign flips are induced by the point symmetries, or `NULL`

8878:   Level: advanced

8880:   Notes:
8881:   Call `DMPlexRestoreClosureIndices()` to free allocated memory

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

8889: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexRestoreClosureIndices()`, `DMPlexVecGetClosure()`, `DMPlexMatSetClosure()`, `DMGetLocalSection()`,
8890:           `PetscSection`, `DMGetGlobalSection()`
8891: @*/
8892: PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscBool useClPerm, PetscInt *numIndices, PetscInt *indices[], PeOp PetscInt outOffsets[], PeOp PetscScalar *values[])
8893: {
8894:   PetscInt numRows = -1, numCols = -1;

8896:   PetscFunctionBeginHot;
8897:   PetscCall(DMPlexGetClosureIndices_Internal(dm, section, idxSection, point, useClPerm, &numRows, &numCols, indices, outOffsets, values, PETSC_TRUE, PETSC_TRUE));
8898:   PetscCheck(numRows == numCols, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Symmetric matrix transformation produces rectangular dimensions (%" PetscInt_FMT ", %" PetscInt_FMT ")", numRows, numCols);
8899:   *numIndices = numRows;
8900:   PetscFunctionReturn(PETSC_SUCCESS);
8901: }

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

8906:   Not collective

8908:   Input Parameters:
8909: + dm         - The `DM`
8910: . section    - The `PetscSection` describing the points (a local section)
8911: . idxSection - The `PetscSection` from which to obtain indices (may be local or global)
8912: . point      - The point defining the closure
8913: - useClPerm  - Use the closure point permutation if available

8915:   Output Parameters:
8916: + numIndices - The number of dof indices in the closure of point with the input sections
8917: . indices    - The dof indices
8918: . outOffsets - Array to write the field offsets into, or `NULL`
8919: - values     - The input values, which may be modified if sign flips are induced by the point symmetries, or `NULL`

8921:   Level: advanced

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

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

8932: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetClosureIndices()`, `DMPlexVecGetClosure()`, `DMPlexMatSetClosure()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
8933: @*/
8934: PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscBool useClPerm, PetscInt *numIndices, PetscInt *indices[], PeOp PetscInt outOffsets[], PeOp PetscScalar *values[])
8935: {
8936:   PetscFunctionBegin;
8938:   PetscAssertPointer(indices, 7);
8939:   PetscCall(DMRestoreWorkArray(dm, 0, MPIU_INT, indices));
8940:   PetscFunctionReturn(PETSC_SUCCESS);
8941: }

8943: PetscErrorCode DMPlexMatSetClosure_Internal(DM dm, PetscSection section, PetscSection globalSection, PetscBool useClPerm, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
8944: {
8945:   DM_Plex           *mesh = (DM_Plex *)dm->data;
8946:   PetscInt          *indices;
8947:   PetscInt           numIndices;
8948:   const PetscScalar *valuesOrig = values;
8949:   PetscErrorCode     ierr;

8951:   PetscFunctionBegin;
8953:   if (!section) PetscCall(DMGetLocalSection(dm, &section));
8955:   if (!globalSection) PetscCall(DMGetGlobalSection(dm, &globalSection));

8959:   PetscCall(DMPlexGetClosureIndices(dm, section, globalSection, point, useClPerm, &numIndices, &indices, NULL, (PetscScalar **)&values));

8961:   if (mesh->printSetValues) PetscCall(DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values));
8962:   /* TODO: fix this code to not use error codes as handle-able exceptions! */
8963:   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
8964:   if (ierr) {
8965:     PetscMPIInt rank;

8967:     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank));
8968:     PetscCall((*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank));
8969:     PetscCall(DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values));
8970:     PetscCall(DMPlexRestoreClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **)&values));
8971:     if (values != valuesOrig) PetscCall(DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values));
8972:     SETERRQ(PetscObjectComm((PetscObject)dm), ierr, "Not possible to set matrix values");
8973:   }
8974:   if (mesh->printFEM > 1) {
8975:     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Indices:"));
8976:     for (PetscInt i = 0; i < numIndices; ++i) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, indices[i]));
8977:     PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
8978:   }

8980:   PetscCall(DMPlexRestoreClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **)&values));
8981:   if (values != valuesOrig) PetscCall(DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values));
8982:   PetscFunctionReturn(PETSC_SUCCESS);
8983: }

8985: /*@
8986:   DMPlexMatSetClosure - Set an array of the values on the closure of `point`

8988:   Not collective

8990:   Input Parameters:
8991: + dm            - The `DM`
8992: . section       - The section describing the layout in `v`, or `NULL` to use the default section
8993: . globalSection - The section describing the layout in `v`, or `NULL` to use the default global section
8994: . A             - The matrix
8995: . point         - The point in the `DM`
8996: . values        - The array of values
8997: - mode          - The insert mode, where `INSERT_ALL_VALUES` and `ADD_ALL_VALUES` also overwrite boundary conditions

8999:   Level: intermediate

9001: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexMatSetClosureGeneral()`, `DMPlexVecGetClosure()`, `DMPlexVecSetClosure()`
9002: @*/
9003: PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
9004: {
9005:   PetscFunctionBegin;
9006:   PetscCall(DMPlexMatSetClosure_Internal(dm, section, globalSection, PETSC_TRUE, A, point, values, mode));
9007:   PetscFunctionReturn(PETSC_SUCCESS);
9008: }

9010: /*@
9011:   DMPlexMatSetClosureGeneral - Set an array of the values on the closure of `point` using a different row and column section

9013:   Not collective

9015:   Input Parameters:
9016: + dmRow            - The `DM` for the row fields
9017: . sectionRow       - The section describing the layout, or `NULL` to use the default section in `dmRow`
9018: . useRowPerm       - The flag to use the closure permutation of the `dmRow` if available
9019: . globalSectionRow - The section describing the layout, or `NULL` to use the default global section in `dmRow`
9020: . dmCol            - The `DM` for the column fields
9021: . sectionCol       - The section describing the layout, or `NULL` to use the default section in `dmCol`
9022: . useColPerm       - The flag to use the closure permutation of the `dmCol` if available
9023: . globalSectionCol - The section describing the layout, or `NULL` to use the default global section in `dmCol`
9024: . A                - The matrix
9025: . point            - The point in the `DM`
9026: . values           - The array of values
9027: - mode             - The insert mode, where `INSERT_ALL_VALUES` and `ADD_ALL_VALUES` also overwrite boundary conditions

9029:   Level: intermediate

9031: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexMatSetClosure()`, `DMPlexVecGetClosure()`, `DMPlexVecSetClosure()`
9032: @*/
9033: PetscErrorCode DMPlexMatSetClosureGeneral(DM dmRow, PetscSection sectionRow, PetscSection globalSectionRow, PetscBool useRowPerm, DM dmCol, PetscSection sectionCol, PetscSection globalSectionCol, PetscBool useColPerm, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
9034: {
9035:   DM_Plex           *mesh = (DM_Plex *)dmRow->data;
9036:   PetscInt          *indicesRow, *indicesCol;
9037:   PetscInt           numIndicesRow = -1, numIndicesCol = -1;
9038:   const PetscScalar *valuesV0 = values, *valuesV1, *valuesV2;

9040:   PetscErrorCode ierr;

9042:   PetscFunctionBegin;
9044:   if (!sectionRow) PetscCall(DMGetLocalSection(dmRow, &sectionRow));
9046:   if (!globalSectionRow) PetscCall(DMGetGlobalSection(dmRow, &globalSectionRow));
9049:   if (!sectionCol) PetscCall(DMGetLocalSection(dmCol, &sectionCol));
9051:   if (!globalSectionCol) PetscCall(DMGetGlobalSection(dmCol, &globalSectionCol));

9055:   PetscCall(DMPlexGetClosureIndicesSize_Internal(dmRow, sectionRow, point, &numIndicesRow));
9056:   PetscCall(DMPlexGetClosureIndicesSize_Internal(dmCol, sectionCol, point, &numIndicesCol));
9057:   valuesV1 = valuesV0;
9058:   PetscCall(DMPlexGetClosureIndices_Internal(dmRow, sectionRow, globalSectionRow, point, useRowPerm, &numIndicesRow, &numIndicesCol, &indicesRow, NULL, (PetscScalar **)&valuesV1, PETSC_FALSE, PETSC_TRUE));
9059:   valuesV2 = valuesV1;
9060:   PetscCall(DMPlexGetClosureIndices_Internal(dmCol, sectionCol, globalSectionCol, point, useColPerm, &numIndicesRow, &numIndicesCol, &indicesCol, NULL, (PetscScalar **)&valuesV2, PETSC_TRUE, PETSC_FALSE));

9062:   if (mesh->printSetValues) PetscCall(DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndicesRow, indicesRow, numIndicesCol, indicesCol, valuesV2));
9063:   /* TODO: fix this code to not use error codes as handle-able exceptions! */
9064:   ierr = MatSetValues(A, numIndicesRow, indicesRow, numIndicesCol, indicesCol, valuesV2, mode);
9065:   if (ierr) {
9066:     PetscMPIInt rank;

9068:     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank));
9069:     PetscCall((*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank));
9070:     PetscCall(DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values));
9071:     PetscCall(DMPlexRestoreClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesRow, NULL, (PetscScalar **)&valuesV2));
9072:     PetscCall(DMPlexRestoreClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **)&valuesV1));
9073:     if (valuesV2 != valuesV1) PetscCall(DMRestoreWorkArray(dmCol, 0, MPIU_SCALAR, &valuesV2));
9074:     if (valuesV1 != valuesV0) PetscCall(DMRestoreWorkArray(dmRow, 0, MPIU_SCALAR, &valuesV1));
9075:   }

9077:   PetscCall(DMPlexRestoreClosureIndices(dmCol, sectionCol, globalSectionCol, point, useColPerm, &numIndicesCol, &indicesCol, NULL, (PetscScalar **)&valuesV2));
9078:   PetscCall(DMPlexRestoreClosureIndices(dmRow, sectionRow, globalSectionRow, point, useRowPerm, &numIndicesRow, &indicesRow, NULL, (PetscScalar **)&valuesV1));
9079:   if (valuesV2 != valuesV1) PetscCall(DMRestoreWorkArray(dmCol, 0, MPIU_SCALAR, &valuesV2));
9080:   if (valuesV1 != valuesV0) PetscCall(DMRestoreWorkArray(dmRow, 0, MPIU_SCALAR, &valuesV1));
9081:   PetscFunctionReturn(PETSC_SUCCESS);
9082: }

9084: /*@
9085:   DMPlexMatSetClosureRefined - Insert values into `A` for the closure of a coarse-cell point, using indices from the associated refined subcells

9087:   Not Collective

9089:   Input Parameters:
9090: + dmf            - The fine `DMPLEX`
9091: . fsection       - The fine local `PetscSection`, or `NULL` to use `DMGetLocalSection(dmf, ...)`
9092: . globalFSection - The fine global `PetscSection`, or `NULL` to use `DMGetGlobalSection(dmf, ...)`
9093: . dmc            - The coarse `DMPLEX`
9094: . csection       - The coarse local `PetscSection`, or `NULL` to use `DMGetLocalSection(dmc, ...)`
9095: . globalCSection - The coarse global `PetscSection`, or `NULL` to use `DMGetGlobalSection(dmc, ...)`
9096: . A              - The matrix
9097: . point          - The coarse-mesh point whose refined closure is inserted
9098: . values         - The values to insert (row block of size equal to the fine-closure dof count, column block of size equal to the coarse-closure dof count)
9099: - mode           - The `InsertMode` (`ADD_VALUES` or `INSERT_VALUES`)

9101:   Level: developer

9103:   Note:
9104:   This helper mirrors `DMPlexMatSetClosure()` but produces row indices from the fine subcells associated
9105:   with the coarse `point` under regular refinement. It is used when assembling operators that couple a
9106:   coarse cell to its uniform refinement.

9108: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexMatSetClosure()`, `DMPlexMatGetClosureIndicesRefined()`, `DMPlexTransformCellTransform()`, `MatSetValues()`
9109: @*/
9110: PetscErrorCode DMPlexMatSetClosureRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
9111: {
9112:   DM_Plex        *mesh    = (DM_Plex *)dmf->data;
9113:   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
9114:   PetscInt       *cpoints = NULL;
9115:   PetscInt       *findices, *cindices;
9116:   const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
9117:   PetscInt        foffsets[32], coffsets[32];
9118:   DMPolytopeType  ct;
9119:   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
9120:   PetscErrorCode  ierr;

9122:   PetscFunctionBegin;
9125:   if (!fsection) PetscCall(DMGetLocalSection(dmf, &fsection));
9127:   if (!csection) PetscCall(DMGetLocalSection(dmc, &csection));
9129:   if (!globalFSection) PetscCall(DMGetGlobalSection(dmf, &globalFSection));
9131:   if (!globalCSection) PetscCall(DMGetGlobalSection(dmc, &globalCSection));
9134:   PetscCall(PetscSectionGetNumFields(fsection, &numFields));
9135:   PetscCheck(numFields <= 31, PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %" PetscInt_FMT " limited to 31", numFields);
9136:   PetscCall(PetscArrayzero(foffsets, 32));
9137:   PetscCall(PetscArrayzero(coffsets, 32));
9138:   /* Column indices */
9139:   PetscCall(DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints));
9140:   maxFPoints = numCPoints;
9141:   /* Compress out points not in the section */
9142:   /*   TODO: Squeeze out points with 0 dof as well */
9143:   PetscCall(PetscSectionGetChart(csection, &pStart, &pEnd));
9144:   for (p = 0, q = 0; p < numCPoints * 2; p += 2) {
9145:     if (cpoints[p] >= pStart && cpoints[p] < pEnd) {
9146:       cpoints[q * 2]     = cpoints[p];
9147:       cpoints[q * 2 + 1] = cpoints[p + 1];
9148:       ++q;
9149:     }
9150:   }
9151:   numCPoints = q;
9152:   for (p = 0, numCIndices = 0; p < numCPoints * 2; p += 2) {
9153:     PetscInt fdof;

9155:     PetscCall(PetscSectionGetDof(csection, cpoints[p], &dof));
9156:     if (!dof) continue;
9157:     for (f = 0; f < numFields; ++f) {
9158:       PetscCall(PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof));
9159:       coffsets[f + 1] += fdof;
9160:     }
9161:     numCIndices += dof;
9162:   }
9163:   for (f = 1; f < numFields; ++f) coffsets[f + 1] += coffsets[f];
9164:   /* Row indices */
9165:   PetscCall(DMPlexGetCellType(dmc, point, &ct));
9166:   {
9167:     DMPlexTransform tr;
9168:     DMPolytopeType *rct;
9169:     PetscInt       *rsize, *rcone, *rornt, Nt;

9171:     PetscCall(DMPlexTransformCreate(PETSC_COMM_SELF, &tr));
9172:     PetscCall(DMPlexTransformSetType(tr, DMPLEXREFINEREGULAR));
9173:     PetscCall(DMPlexTransformCellTransform(tr, ct, point, NULL, &Nt, &rct, &rsize, &rcone, &rornt));
9174:     numSubcells = rsize[Nt - 1];
9175:     PetscCall(DMPlexTransformDestroy(&tr));
9176:   }
9177:   PetscCall(DMGetWorkArray(dmf, maxFPoints * 2 * numSubcells, MPIU_INT, &ftotpoints));
9178:   for (r = 0, q = 0; r < numSubcells; ++r) {
9179:     /* TODO Map from coarse to fine cells */
9180:     PetscCall(DMPlexGetTransitiveClosure(dmf, point * numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints));
9181:     /* Compress out points not in the section */
9182:     PetscCall(PetscSectionGetChart(fsection, &pStart, &pEnd));
9183:     for (p = 0; p < numFPoints * 2; p += 2) {
9184:       if (fpoints[p] >= pStart && fpoints[p] < pEnd) {
9185:         PetscCall(PetscSectionGetDof(fsection, fpoints[p], &dof));
9186:         if (!dof) continue;
9187:         for (s = 0; s < q; ++s)
9188:           if (fpoints[p] == ftotpoints[s * 2]) break;
9189:         if (s < q) continue;
9190:         ftotpoints[q * 2]     = fpoints[p];
9191:         ftotpoints[q * 2 + 1] = fpoints[p + 1];
9192:         ++q;
9193:       }
9194:     }
9195:     PetscCall(DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints));
9196:   }
9197:   numFPoints = q;
9198:   for (p = 0, numFIndices = 0; p < numFPoints * 2; p += 2) {
9199:     PetscInt fdof;

9201:     PetscCall(PetscSectionGetDof(fsection, ftotpoints[p], &dof));
9202:     if (!dof) continue;
9203:     for (f = 0; f < numFields; ++f) {
9204:       PetscCall(PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof));
9205:       foffsets[f + 1] += fdof;
9206:     }
9207:     numFIndices += dof;
9208:   }
9209:   for (f = 1; f < numFields; ++f) foffsets[f + 1] += foffsets[f];

9211:   PetscCheck(!numFields || foffsets[numFields] == numFIndices, PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %" PetscInt_FMT " should be %" PetscInt_FMT, foffsets[numFields], numFIndices);
9212:   PetscCheck(!numFields || coffsets[numFields] == numCIndices, PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %" PetscInt_FMT " should be %" PetscInt_FMT, coffsets[numFields], numCIndices);
9213:   PetscCall(DMGetWorkArray(dmf, numFIndices, MPIU_INT, &findices));
9214:   PetscCall(DMGetWorkArray(dmc, numCIndices, MPIU_INT, &cindices));
9215:   if (numFields) {
9216:     const PetscInt **permsF[32] = {NULL};
9217:     const PetscInt **permsC[32] = {NULL};

9219:     for (f = 0; f < numFields; f++) {
9220:       PetscCall(PetscSectionGetFieldPointSyms(fsection, f, numFPoints, ftotpoints, &permsF[f], NULL));
9221:       PetscCall(PetscSectionGetFieldPointSyms(csection, f, numCPoints, cpoints, &permsC[f], NULL));
9222:     }
9223:     for (p = 0; p < numFPoints; p++) {
9224:       PetscCall(PetscSectionGetOffset(globalFSection, ftotpoints[2 * p], &globalOff));
9225:       PetscCall(DMPlexGetIndicesPointFields_Internal(fsection, PETSC_FALSE, ftotpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices));
9226:     }
9227:     for (p = 0; p < numCPoints; p++) {
9228:       PetscCall(PetscSectionGetOffset(globalCSection, cpoints[2 * p], &globalOff));
9229:       PetscCall(DMPlexGetIndicesPointFields_Internal(csection, PETSC_FALSE, cpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices));
9230:     }
9231:     for (f = 0; f < numFields; f++) {
9232:       PetscCall(PetscSectionRestoreFieldPointSyms(fsection, f, numFPoints, ftotpoints, &permsF[f], NULL));
9233:       PetscCall(PetscSectionRestoreFieldPointSyms(csection, f, numCPoints, cpoints, &permsC[f], NULL));
9234:     }
9235:   } else {
9236:     const PetscInt **permsF = NULL;
9237:     const PetscInt **permsC = NULL;

9239:     PetscCall(PetscSectionGetPointSyms(fsection, numFPoints, ftotpoints, &permsF, NULL));
9240:     PetscCall(PetscSectionGetPointSyms(csection, numCPoints, cpoints, &permsC, NULL));
9241:     for (p = 0, off = 0; p < numFPoints; p++) {
9242:       const PetscInt *perm = permsF ? permsF[p] : NULL;

9244:       PetscCall(PetscSectionGetOffset(globalFSection, ftotpoints[2 * p], &globalOff));
9245:       PetscCall(DMPlexGetIndicesPoint_Internal(fsection, PETSC_FALSE, ftotpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices));
9246:     }
9247:     for (p = 0, off = 0; p < numCPoints; p++) {
9248:       const PetscInt *perm = permsC ? permsC[p] : NULL;

9250:       PetscCall(PetscSectionGetOffset(globalCSection, cpoints[2 * p], &globalOff));
9251:       PetscCall(DMPlexGetIndicesPoint_Internal(csection, PETSC_FALSE, cpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices));
9252:     }
9253:     PetscCall(PetscSectionRestorePointSyms(fsection, numFPoints, ftotpoints, &permsF, NULL));
9254:     PetscCall(PetscSectionRestorePointSyms(csection, numCPoints, cpoints, &permsC, NULL));
9255:   }
9256:   if (mesh->printSetValues) PetscCall(DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values));
9257:   /* TODO: flips */
9258:   /* TODO: fix this code to not use error codes as handle-able exceptions! */
9259:   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
9260:   if (ierr) {
9261:     PetscMPIInt rank;

9263:     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank));
9264:     PetscCall((*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank));
9265:     PetscCall(DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values));
9266:     PetscCall(DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices));
9267:     PetscCall(DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices));
9268:   }
9269:   PetscCall(DMRestoreWorkArray(dmf, numCPoints * 2 * 4, MPIU_INT, &ftotpoints));
9270:   PetscCall(DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints));
9271:   PetscCall(DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices));
9272:   PetscCall(DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices));
9273:   PetscFunctionReturn(PETSC_SUCCESS);
9274: }

9276: /*@
9277:   DMPlexMatGetClosureIndicesRefined - Compute the fine-row and coarse-column global indices associated with the refined closure of a coarse point

9279:   Not Collective

9281:   Input Parameters:
9282: + dmf            - The fine `DMPLEX`
9283: . fsection       - The fine local `PetscSection`, or `NULL` to use `DMGetLocalSection(dmf, ...)`
9284: . globalFSection - The fine global `PetscSection`, or `NULL` to use `DMGetGlobalSection(dmf, ...)`
9285: . dmc            - The coarse `DMPLEX`
9286: . csection       - The coarse local `PetscSection`, or `NULL` to use `DMGetLocalSection(dmc, ...)`
9287: . globalCSection - The coarse global `PetscSection`, or `NULL` to use `DMGetGlobalSection(dmc, ...)`
9288: - point          - The coarse-mesh point

9290:   Output Parameters:
9291: + cindices - Global column indices for the coarse closure (caller-provided buffer of the correct size)
9292: - findices - Global row indices for the fine closure across all subcells produced by uniformly refining `point`

9294:   Level: developer

9296:   Note:
9297:   This companion to `DMPlexMatSetClosureRefined()` returns the index sets without inserting values,
9298:   allowing callers to build sparsity patterns or perform their own `MatSetValues()` calls.

9300: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexMatSetClosureRefined()`, `DMPlexGetClosureIndices()`, `DMPlexTransformCellTransform()`
9301: @*/
9302: PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
9303: {
9304:   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
9305:   PetscInt       *cpoints      = NULL;
9306:   PetscInt        foffsets[32] = {0}, coffsets[32] = {0};
9307:   const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
9308:   DMPolytopeType  ct;
9309:   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;

9311:   PetscFunctionBegin;
9314:   if (!fsection) PetscCall(DMGetLocalSection(dmf, &fsection));
9316:   if (!csection) PetscCall(DMGetLocalSection(dmc, &csection));
9318:   if (!globalFSection) PetscCall(DMGetGlobalSection(dmf, &globalFSection));
9320:   if (!globalCSection) PetscCall(DMGetGlobalSection(dmc, &globalCSection));
9322:   PetscCall(PetscSectionGetNumFields(fsection, &numFields));
9323:   PetscCheck(numFields <= 31, PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %" PetscInt_FMT " limited to 31", numFields);
9324:   /* Column indices */
9325:   PetscCall(DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints));
9326:   maxFPoints = numCPoints;
9327:   /* Compress out points not in the section */
9328:   /*   TODO: Squeeze out points with 0 dof as well */
9329:   PetscCall(PetscSectionGetChart(csection, &pStart, &pEnd));
9330:   for (p = 0, q = 0; p < numCPoints * 2; p += 2) {
9331:     if (cpoints[p] >= pStart && cpoints[p] < pEnd) {
9332:       cpoints[q * 2]     = cpoints[p];
9333:       cpoints[q * 2 + 1] = cpoints[p + 1];
9334:       ++q;
9335:     }
9336:   }
9337:   numCPoints = q;
9338:   for (p = 0, numCIndices = 0; p < numCPoints * 2; p += 2) {
9339:     PetscInt fdof;

9341:     PetscCall(PetscSectionGetDof(csection, cpoints[p], &dof));
9342:     if (!dof) continue;
9343:     for (f = 0; f < numFields; ++f) {
9344:       PetscCall(PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof));
9345:       coffsets[f + 1] += fdof;
9346:     }
9347:     numCIndices += dof;
9348:   }
9349:   for (f = 1; f < numFields; ++f) coffsets[f + 1] += coffsets[f];
9350:   /* Row indices */
9351:   PetscCall(DMPlexGetCellType(dmc, point, &ct));
9352:   {
9353:     DMPlexTransform tr;
9354:     DMPolytopeType *rct;
9355:     PetscInt       *rsize, *rcone, *rornt, Nt;

9357:     PetscCall(DMPlexTransformCreate(PETSC_COMM_SELF, &tr));
9358:     PetscCall(DMPlexTransformSetType(tr, DMPLEXREFINEREGULAR));
9359:     PetscCall(DMPlexTransformCellTransform(tr, ct, point, NULL, &Nt, &rct, &rsize, &rcone, &rornt));
9360:     numSubcells = rsize[Nt - 1];
9361:     PetscCall(DMPlexTransformDestroy(&tr));
9362:   }
9363:   PetscCall(DMGetWorkArray(dmf, maxFPoints * 2 * numSubcells, MPIU_INT, &ftotpoints));
9364:   for (r = 0, q = 0; r < numSubcells; ++r) {
9365:     /* TODO Map from coarse to fine cells */
9366:     PetscCall(DMPlexGetTransitiveClosure(dmf, point * numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints));
9367:     /* Compress out points not in the section */
9368:     PetscCall(PetscSectionGetChart(fsection, &pStart, &pEnd));
9369:     for (p = 0; p < numFPoints * 2; p += 2) {
9370:       if (fpoints[p] >= pStart && fpoints[p] < pEnd) {
9371:         PetscCall(PetscSectionGetDof(fsection, fpoints[p], &dof));
9372:         if (!dof) continue;
9373:         for (s = 0; s < q; ++s)
9374:           if (fpoints[p] == ftotpoints[s * 2]) break;
9375:         if (s < q) continue;
9376:         ftotpoints[q * 2]     = fpoints[p];
9377:         ftotpoints[q * 2 + 1] = fpoints[p + 1];
9378:         ++q;
9379:       }
9380:     }
9381:     PetscCall(DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints));
9382:   }
9383:   numFPoints = q;
9384:   for (p = 0, numFIndices = 0; p < numFPoints * 2; p += 2) {
9385:     PetscInt fdof;

9387:     PetscCall(PetscSectionGetDof(fsection, ftotpoints[p], &dof));
9388:     if (!dof) continue;
9389:     for (f = 0; f < numFields; ++f) {
9390:       PetscCall(PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof));
9391:       foffsets[f + 1] += fdof;
9392:     }
9393:     numFIndices += dof;
9394:   }
9395:   for (f = 1; f < numFields; ++f) foffsets[f + 1] += foffsets[f];

9397:   PetscCheck(!numFields || foffsets[numFields] == numFIndices, PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %" PetscInt_FMT " should be %" PetscInt_FMT, foffsets[numFields], numFIndices);
9398:   PetscCheck(!numFields || coffsets[numFields] == numCIndices, PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %" PetscInt_FMT " should be %" PetscInt_FMT, coffsets[numFields], numCIndices);
9399:   if (numFields) {
9400:     const PetscInt **permsF[32] = {NULL};
9401:     const PetscInt **permsC[32] = {NULL};

9403:     for (f = 0; f < numFields; f++) {
9404:       PetscCall(PetscSectionGetFieldPointSyms(fsection, f, numFPoints, ftotpoints, &permsF[f], NULL));
9405:       PetscCall(PetscSectionGetFieldPointSyms(csection, f, numCPoints, cpoints, &permsC[f], NULL));
9406:     }
9407:     for (p = 0; p < numFPoints; p++) {
9408:       PetscCall(PetscSectionGetOffset(globalFSection, ftotpoints[2 * p], &globalOff));
9409:       PetscCall(DMPlexGetIndicesPointFields_Internal(fsection, PETSC_FALSE, ftotpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices));
9410:     }
9411:     for (p = 0; p < numCPoints; p++) {
9412:       PetscCall(PetscSectionGetOffset(globalCSection, cpoints[2 * p], &globalOff));
9413:       PetscCall(DMPlexGetIndicesPointFields_Internal(csection, PETSC_FALSE, cpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices));
9414:     }
9415:     for (f = 0; f < numFields; f++) {
9416:       PetscCall(PetscSectionRestoreFieldPointSyms(fsection, f, numFPoints, ftotpoints, &permsF[f], NULL));
9417:       PetscCall(PetscSectionRestoreFieldPointSyms(csection, f, numCPoints, cpoints, &permsC[f], NULL));
9418:     }
9419:   } else {
9420:     const PetscInt **permsF = NULL;
9421:     const PetscInt **permsC = NULL;

9423:     PetscCall(PetscSectionGetPointSyms(fsection, numFPoints, ftotpoints, &permsF, NULL));
9424:     PetscCall(PetscSectionGetPointSyms(csection, numCPoints, cpoints, &permsC, NULL));
9425:     for (p = 0, off = 0; p < numFPoints; p++) {
9426:       const PetscInt *perm = permsF ? permsF[p] : NULL;

9428:       PetscCall(PetscSectionGetOffset(globalFSection, ftotpoints[2 * p], &globalOff));
9429:       PetscCall(DMPlexGetIndicesPoint_Internal(fsection, PETSC_FALSE, ftotpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices));
9430:     }
9431:     for (p = 0, off = 0; p < numCPoints; p++) {
9432:       const PetscInt *perm = permsC ? permsC[p] : NULL;

9434:       PetscCall(PetscSectionGetOffset(globalCSection, cpoints[2 * p], &globalOff));
9435:       PetscCall(DMPlexGetIndicesPoint_Internal(csection, PETSC_FALSE, cpoints[2 * p], globalOff < 0 ? -(globalOff + 1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices));
9436:     }
9437:     PetscCall(PetscSectionRestorePointSyms(fsection, numFPoints, ftotpoints, &permsF, NULL));
9438:     PetscCall(PetscSectionRestorePointSyms(csection, numCPoints, cpoints, &permsC, NULL));
9439:   }
9440:   PetscCall(DMRestoreWorkArray(dmf, numCPoints * 2 * 4, MPIU_INT, &ftotpoints));
9441:   PetscCall(DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints));
9442:   PetscFunctionReturn(PETSC_SUCCESS);
9443: }

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

9448:   Input Parameter:
9449: . dm - The `DMPLEX` object

9451:   Output Parameter:
9452: . cellHeight - The height of a cell

9454:   Level: developer

9456: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexSetVTKCellHeight()`
9457: @*/
9458: PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
9459: {
9460:   DM_Plex *mesh = (DM_Plex *)dm->data;

9462:   PetscFunctionBegin;
9464:   PetscAssertPointer(cellHeight, 2);
9465:   *cellHeight = mesh->vtkCellHeight;
9466:   PetscFunctionReturn(PETSC_SUCCESS);
9467: }

9469: /*@
9470:   DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)

9472:   Input Parameters:
9473: + dm         - The `DMPLEX` object
9474: - cellHeight - The height of a cell

9476:   Level: developer

9478: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetVTKCellHeight()`
9479: @*/
9480: PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
9481: {
9482:   DM_Plex *mesh = (DM_Plex *)dm->data;

9484:   PetscFunctionBegin;
9486:   mesh->vtkCellHeight = cellHeight;
9487:   PetscFunctionReturn(PETSC_SUCCESS);
9488: }

9490: /*@
9491:   DMPlexGetCellTypeStratum - Get the range of cells of a given celltype

9493:   Input Parameters:
9494: + dm - The `DMPLEX` object
9495: - ct - The `DMPolytopeType` of the cell

9497:   Output Parameters:
9498: + start - The first cell of this type, or `NULL`
9499: - end   - The upper bound on this celltype, or `NULL`

9501:   Level: advanced

9503: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexConstructGhostCells()`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
9504: @*/
9505: PetscErrorCode DMPlexGetCellTypeStratum(DM dm, DMPolytopeType ct, PeOp PetscInt *start, PeOp PetscInt *end)
9506: {
9507:   DM_Plex *mesh = (DM_Plex *)dm->data;
9508:   DMLabel  label;
9509:   PetscInt pStart, pEnd;

9511:   PetscFunctionBegin;
9513:   if (start) {
9514:     PetscAssertPointer(start, 3);
9515:     *start = 0;
9516:   }
9517:   if (end) {
9518:     PetscAssertPointer(end, 4);
9519:     *end = 0;
9520:   }
9521:   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
9522:   if (pStart == pEnd) PetscFunctionReturn(PETSC_SUCCESS);
9523:   if (mesh->tr) {
9524:     PetscCall(DMPlexTransformGetCellTypeStratum(mesh->tr, ct, start, end));
9525:   } else {
9526:     PetscCall(DMPlexGetCellTypeLabel(dm, &label));
9527:     PetscCheck(label, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "No label named celltype was found");
9528:     PetscCall(DMLabelGetStratumBounds(label, ct, start, end));
9529:   }
9530:   PetscFunctionReturn(PETSC_SUCCESS);
9531: }

9533: /*@
9534:   DMPlexGetDepthStratumGlobalSize - Get the global size for a given depth stratum

9536:   Input Parameters:
9537: + dm    - The `DMPLEX` object
9538: - depth - The depth for the given point stratum

9540:   Output Parameter:
9541: . gsize - The global number of points in the stratum

9543:   Level: advanced

9545: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellNumbering()`, `DMPlexGetVertexNumbering()`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
9546: @*/
9547: PetscErrorCode DMPlexGetDepthStratumGlobalSize(DM dm, PetscInt depth, PetscInt *gsize)
9548: {
9549:   PetscSF         sf;
9550:   const PetscInt *leaves;
9551:   PetscInt        Nl, loc, start, end;

9553:   PetscFunctionBegin;
9554:   *gsize = 0;
9555:   PetscCall(DMGetPointSF(dm, &sf));
9556:   PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL));
9557:   PetscCall(DMPlexGetDepthStratum(dm, depth, &start, &end));
9558:   for (PetscInt p = start; p < end; ++p) {
9559:     PetscCall(PetscFindInt(p, Nl, leaves, &loc));
9560:     if (loc < 0) ++(*gsize);
9561:   }
9562:   PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, gsize, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)dm)));
9563:   PetscFunctionReturn(PETSC_SUCCESS);
9564: }

9566: PetscErrorCode DMPlexCreateNumbering_Plex(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
9567: {
9568:   PetscSection section, globalSection;
9569:   PetscInt    *numbers, p;

9571:   PetscFunctionBegin;
9572:   if (PetscDefined(USE_DEBUG)) PetscCall(DMPlexCheckPointSF(dm, sf, PETSC_TRUE));
9573:   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section));
9574:   PetscCall(PetscSectionSetChart(section, pStart, pEnd));
9575:   for (p = pStart; p < pEnd; ++p) PetscCall(PetscSectionSetDof(section, p, 1));
9576:   PetscCall(PetscSectionSetUp(section));
9577:   PetscCall(PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &globalSection));
9578:   PetscCall(PetscMalloc1(pEnd - pStart, &numbers));
9579:   for (p = pStart; p < pEnd; ++p) {
9580:     PetscCall(PetscSectionGetOffset(globalSection, p, &numbers[p - pStart]));
9581:     if (numbers[p - pStart] < 0) numbers[p - pStart] -= shift;
9582:     else numbers[p - pStart] += shift;
9583:   }
9584:   PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering));
9585:   if (globalSize) {
9586:     PetscLayout layout;
9587:     PetscCall(PetscSectionGetPointLayout(PetscObjectComm((PetscObject)dm), globalSection, &layout));
9588:     PetscCall(PetscLayoutGetSize(layout, globalSize));
9589:     PetscCall(PetscLayoutDestroy(&layout));
9590:   }
9591:   PetscCall(PetscSectionDestroy(&section));
9592:   PetscCall(PetscSectionDestroy(&globalSection));
9593:   PetscFunctionReturn(PETSC_SUCCESS);
9594: }

9596: /*@
9597:   DMPlexCreateCellNumbering - Get a global cell numbering for all cells on this process

9599:   Input Parameters:
9600: + dm         - The `DMPLEX` object
9601: - includeAll - Whether to include all cells, or just the simplex and box cells

9603:   Output Parameter:
9604: . globalCellNumbers - Global cell numbers for all cells on this process

9606:   Level: developer

9608: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellNumbering()`, `DMPlexGetVertexNumbering()`
9609: @*/
9610: PetscErrorCode DMPlexCreateCellNumbering(DM dm, PetscBool includeAll, IS *globalCellNumbers)
9611: {
9612:   PetscInt cellHeight, cStart, cEnd;

9614:   PetscFunctionBegin;
9615:   PetscCall(DMPlexGetVTKCellHeight(dm, &cellHeight));
9616:   if (includeAll) PetscCall(DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd));
9617:   else PetscCall(DMPlexGetSimplexOrBoxCells(dm, cellHeight, &cStart, &cEnd));
9618:   PetscCall(DMPlexCreateNumbering_Plex(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers));
9619:   PetscFunctionReturn(PETSC_SUCCESS);
9620: }

9622: /*@
9623:   DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process

9625:   Input Parameter:
9626: . dm - The `DMPLEX` object

9628:   Output Parameter:
9629: . globalCellNumbers - Global cell numbers for all cells on this process

9631:   Level: developer

9633: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateCellNumbering()`, `DMPlexGetVertexNumbering()`
9634: @*/
9635: PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
9636: {
9637:   DM_Plex *mesh = (DM_Plex *)dm->data;

9639:   PetscFunctionBegin;
9641:   if (!mesh->globalCellNumbers) PetscCall(DMPlexCreateCellNumbering(dm, PETSC_FALSE, &mesh->globalCellNumbers));
9642:   *globalCellNumbers = mesh->globalCellNumbers;
9643:   PetscFunctionReturn(PETSC_SUCCESS);
9644: }

9646: PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
9647: {
9648:   PetscInt vStart, vEnd;

9650:   PetscFunctionBegin;
9652:   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
9653:   PetscCall(DMPlexCreateNumbering_Plex(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers));
9654:   PetscFunctionReturn(PETSC_SUCCESS);
9655: }

9657: /*@
9658:   DMPlexGetVertexNumbering - Get a global vertex numbering for all vertices on this process

9660:   Input Parameter:
9661: . dm - The `DMPLEX` object

9663:   Output Parameter:
9664: . globalVertexNumbers - Global vertex numbers for all vertices on this process

9666:   Level: developer

9668: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellNumbering()`
9669: @*/
9670: PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
9671: {
9672:   DM_Plex *mesh = (DM_Plex *)dm->data;

9674:   PetscFunctionBegin;
9676:   if (!mesh->globalVertexNumbers) PetscCall(DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers));
9677:   *globalVertexNumbers = mesh->globalVertexNumbers;
9678:   PetscFunctionReturn(PETSC_SUCCESS);
9679: }

9681: /*@
9682:   DMPlexCreatePointNumbering - Create a global numbering for all points.

9684:   Collective

9686:   Input Parameter:
9687: . dm - The `DMPLEX` object

9689:   Output Parameter:
9690: . globalPointNumbers - Global numbers for all points on this process

9692:   Level: developer

9694:   Notes:
9695:   The point numbering `IS` is parallel, with local portion indexed by local points (see `DMGetLocalSection()`). The global
9696:   points are taken as stratified, with each MPI rank owning a contiguous subset of each stratum. In the IS, owned points
9697:   will have their non-negative value while points owned by different ranks will be involuted -(idx+1). As an example,
9698:   consider a parallel mesh in which the first two elements and first two vertices are owned by rank 0.

9700:   The partitioned mesh is
9701:   ```
9702:   (2)--0--(3)--1--(4)    (1)--0--(2)
9703:   ```
9704:   and its global numbering is
9705:   ```
9706:   (3)--0--(4)--1--(5)--2--(6)
9707:   ```
9708:   Then the global numbering is provided as
9709:   ```
9710:   [0] Number of indices in set 5
9711:   [0] 0 0
9712:   [0] 1 1
9713:   [0] 2 3
9714:   [0] 3 4
9715:   [0] 4 -6
9716:   [1] Number of indices in set 3
9717:   [1] 0 2
9718:   [1] 1 5
9719:   [1] 2 6
9720:   ```

9722: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellNumbering()`
9723: @*/
9724: PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
9725: {
9726:   IS        nums[4];
9727:   PetscInt  depths[4], gdepths[4], starts[4];
9728:   PetscInt  depth, d, shift = 0;
9729:   PetscBool empty = PETSC_FALSE;

9731:   PetscFunctionBegin;
9733:   PetscCall(DMPlexGetDepth(dm, &depth));
9734:   // For unstratified meshes use dim instead of depth
9735:   if (depth < 0) PetscCall(DMGetDimension(dm, &depth));
9736:   // If any stratum is empty, we must mark all empty
9737:   for (d = 0; d <= depth; ++d) {
9738:     PetscInt end;

9740:     depths[d] = depth - d;
9741:     PetscCall(DMPlexGetDepthStratum(dm, depths[d], &starts[d], &end));
9742:     if (!(starts[d] - end)) empty = PETSC_TRUE;
9743:   }
9744:   if (empty)
9745:     for (d = 0; d <= depth; ++d) {
9746:       depths[d] = -1;
9747:       starts[d] = -1;
9748:     }
9749:   else PetscCall(PetscSortIntWithArray(depth + 1, starts, depths));
9750:   PetscCallMPI(MPIU_Allreduce(depths, gdepths, depth + 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
9751:   for (d = 0; d <= depth; ++d) PetscCheck(starts[d] < 0 || depths[d] == gdepths[d], PETSC_COMM_SELF, PETSC_ERR_PLIB, "Expected depth %" PetscInt_FMT ", found %" PetscInt_FMT, depths[d], gdepths[d]);
9752:   // Note here that 'shift' is collective, so that the numbering is stratified by depth
9753:   for (d = 0; d <= depth; ++d) {
9754:     PetscInt pStart, pEnd, gsize;

9756:     PetscCall(DMPlexGetDepthStratum(dm, gdepths[d], &pStart, &pEnd));
9757:     PetscCall(DMPlexCreateNumbering_Plex(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]));
9758:     shift += gsize;
9759:   }
9760:   PetscCall(ISConcatenate(PETSC_COMM_SELF, depth + 1, nums, globalPointNumbers));
9761:   for (d = 0; d <= depth; ++d) PetscCall(ISDestroy(&nums[d]));
9762:   PetscFunctionReturn(PETSC_SUCCESS);
9763: }

9765: /*@
9766:   DMPlexCreateEdgeNumbering - Create a global numbering for edges.

9768:   Collective

9770:   Input Parameter:
9771: . dm - The `DMPLEX` object

9773:   Output Parameter:
9774: . globalEdgeNumbers - Global numbers for all edges on this process

9776:   Level: developer

9778:   Notes:
9779:   The point numbering `IS` is parallel, with local portion indexed by local points (see `DMGetLocalSection()`). In the IS, owned edges will have their non-negative value while edges owned by different ranks will be involuted -(idx+1).

9781: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCellNumbering()`, `DMPlexGetVertexNumbering()`, `DMPlexCreatePointNumbering()`
9782: @*/
9783: PetscErrorCode DMPlexCreateEdgeNumbering(DM dm, IS *globalEdgeNumbers)
9784: {
9785:   PetscSF  sf;
9786:   PetscInt eStart, eEnd;

9788:   PetscFunctionBegin;
9790:   PetscCall(DMGetPointSF(dm, &sf));
9791:   PetscCall(DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd));
9792:   PetscCall(DMPlexCreateNumbering_Plex(dm, eStart, eEnd, 0, NULL, sf, globalEdgeNumbers));
9793:   PetscFunctionReturn(PETSC_SUCCESS);
9794: }

9796: /*@
9797:   DMPlexCreateRankField - Create a cell field whose value is the rank of the owner

9799:   Input Parameter:
9800: . dm - The `DMPLEX` object

9802:   Output Parameter:
9803: . ranks - The rank field

9805:   Options Database Key:
9806: . -dm_partition_view - Adds the rank field into the `DM` output from `-dm_view` using the same viewer

9808:   Level: intermediate

9810: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMView()`
9811: @*/
9812: PetscErrorCode DMPlexCreateRankField(DM dm, Vec *ranks)
9813: {
9814:   DM             rdm;
9815:   PetscFE        fe;
9816:   PetscScalar   *r;
9817:   PetscMPIInt    rank;
9818:   DMPolytopeType ct;
9819:   PetscInt       dim, cStart, cEnd, c;

9821:   PetscFunctionBeginUser;
9823:   PetscAssertPointer(ranks, 2);
9824:   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
9825:   PetscCall(DMClone(dm, &rdm));
9826:   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)rdm, "PETSc___rank_"));
9827:   PetscCall(DMGetDimension(rdm, &dim));
9828:   PetscCall(DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd));
9829:   if (cEnd > cStart) PetscCall(DMPlexGetCellType(rdm, cStart, &ct));
9830:   else {
9831:     switch (dim) {
9832:     case 0:
9833:       ct = DM_POLYTOPE_POINT;
9834:       break;
9835:     case 1:
9836:       ct = DM_POLYTOPE_SEGMENT;
9837:       break;
9838:     case 2:
9839:       ct = DM_POLYTOPE_TRIANGLE;
9840:       break;
9841:     case 3:
9842:       ct = DM_POLYTOPE_TETRAHEDRON;
9843:       break;
9844:     default:
9845:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No default cell type for dimension %" PetscInt_FMT, dim);
9846:     }
9847:   }
9848:   PetscCall(PetscFECreateLagrangeByCell(PETSC_COMM_SELF, dim, 1, ct, 0, -1, &fe));
9849:   PetscCall(PetscObjectSetName((PetscObject)fe, "rank"));
9850:   PetscCall(DMSetField(rdm, 0, NULL, (PetscObject)fe));
9851:   PetscCall(PetscFEDestroy(&fe));
9852:   PetscCall(DMCreateDS(rdm));
9853:   PetscCall(DMViewFromOptions(rdm, NULL, "-dm_view"));
9854:   PetscCall(DMCreateGlobalVector(rdm, ranks));
9855:   PetscCall(PetscObjectSetName((PetscObject)*ranks, "partition"));
9856:   PetscCall(VecGetArray(*ranks, &r));
9857:   if (r) {
9858:     for (c = cStart; c < cEnd; ++c) {
9859:       PetscScalar *lr;

9861:       PetscCall(DMPlexPointGlobalRef(rdm, c, r, &lr));
9862:       if (lr) *lr = rank;
9863:     }
9864:   }
9865:   PetscCall(VecRestoreArray(*ranks, &r));
9866:   PetscCall(DMDestroy(&rdm));
9867:   PetscFunctionReturn(PETSC_SUCCESS);
9868: }

9870: /*@
9871:   DMPlexCreateLabelField - Create a field whose value is the label value for that point

9873:   Input Parameters:
9874: + dm    - The `DMPLEX`
9875: - label - The `DMLabel`

9877:   Output Parameter:
9878: . val - The label value field

9880:   Options Database Key:
9881: . -dm_label_view - Adds the label value field into the `DM` output from `-dm_view` using the same viewer

9883:   Level: intermediate

9885: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMView()`
9886: @*/
9887: PetscErrorCode DMPlexCreateLabelField(DM dm, DMLabel label, Vec *val)
9888: {
9889:   DM             rdm, plex;
9890:   Vec            lval;
9891:   PetscSection   section;
9892:   PetscFE        fe;
9893:   PetscScalar   *v;
9894:   PetscInt       dim, pStart, pEnd, p, cStart;
9895:   DMPolytopeType ct;
9896:   char           name[PETSC_MAX_PATH_LEN];
9897:   const char    *lname, *prefix;

9899:   PetscFunctionBeginUser;
9901:   PetscAssertPointer(label, 2);
9902:   PetscAssertPointer(val, 3);
9903:   PetscCall(DMClone(dm, &rdm));
9904:   PetscCall(DMConvert(rdm, DMPLEX, &plex));
9905:   PetscCall(DMPlexGetHeightStratum(plex, 0, &cStart, NULL));
9906:   PetscCall(DMPlexGetCellType(plex, cStart, &ct));
9907:   PetscCall(DMDestroy(&plex));
9908:   PetscCall(DMGetDimension(rdm, &dim));
9909:   PetscCall(DMGetOptionsPrefix(dm, &prefix));
9910:   PetscCall(PetscObjectGetName((PetscObject)label, &lname));
9911:   PetscCall(PetscSNPrintf(name, sizeof(name), "%s%s_", prefix ? prefix : "", lname));
9912:   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, 1, ct, name, -1, &fe));
9913:   PetscCall(PetscObjectSetName((PetscObject)fe, ""));
9914:   PetscCall(DMSetField(rdm, 0, NULL, (PetscObject)fe));
9915:   PetscCall(PetscFEDestroy(&fe));
9916:   PetscCall(DMCreateDS(rdm));
9917:   PetscCall(DMCreateGlobalVector(rdm, val));
9918:   PetscCall(DMCreateLocalVector(rdm, &lval));
9919:   PetscCall(PetscObjectSetName((PetscObject)*val, lname));
9920:   PetscCall(DMGetLocalSection(rdm, &section));
9921:   PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
9922:   PetscCall(VecGetArray(lval, &v));
9923:   for (p = pStart; p < pEnd; ++p) {
9924:     PetscInt cval, dof, off;

9926:     PetscCall(PetscSectionGetDof(section, p, &dof));
9927:     if (!dof) continue;
9928:     PetscCall(DMLabelGetValue(label, p, &cval));
9929:     PetscCall(PetscSectionGetOffset(section, p, &off));
9930:     for (PetscInt d = 0; d < dof; d++) v[off + d] = cval;
9931:   }
9932:   PetscCall(VecRestoreArray(lval, &v));
9933:   PetscCall(DMLocalToGlobal(rdm, lval, INSERT_VALUES, *val));
9934:   PetscCall(VecDestroy(&lval));
9935:   PetscCall(DMDestroy(&rdm));
9936:   PetscFunctionReturn(PETSC_SUCCESS);
9937: }

9939: /*@
9940:   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.

9942:   Input Parameter:
9943: . dm - The `DMPLEX` object

9945:   Level: developer

9947:   Notes:
9948:   This is a useful diagnostic when creating meshes programmatically.

9950:   For the complete list of DMPlexCheck* functions, see `DMSetFromOptions()`.

9952: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMCreate()`, `DMSetFromOptions()`
9953: @*/
9954: PetscErrorCode DMPlexCheckSymmetry(DM dm)
9955: {
9956:   PetscSection    coneSection, supportSection;
9957:   const PetscInt *cone, *support;
9958:   PetscInt        coneSize, c, supportSize, s;
9959:   PetscInt        pStart, pEnd, p, pp, csize, ssize;
9960:   PetscBool       storagecheck = PETSC_TRUE;

9962:   PetscFunctionBegin;
9964:   PetscCall(DMViewFromOptions(dm, NULL, "-sym_dm_view"));
9965:   PetscCall(DMPlexGetConeSection(dm, &coneSection));
9966:   PetscCall(DMPlexGetSupportSection(dm, &supportSection));
9967:   /* Check that point p is found in the support of its cone points, and vice versa */
9968:   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
9969:   for (p = pStart; p < pEnd; ++p) {
9970:     PetscCall(DMPlexGetConeSize(dm, p, &coneSize));
9971:     PetscCall(DMPlexGetCone(dm, p, &cone));
9972:     for (c = 0; c < coneSize; ++c) {
9973:       PetscBool dup = PETSC_FALSE;
9974:       for (PetscInt d = c - 1; d >= 0; --d) {
9975:         if (cone[c] == cone[d]) {
9976:           dup = PETSC_TRUE;
9977:           break;
9978:         }
9979:       }
9980:       PetscCall(DMPlexGetSupportSize(dm, cone[c], &supportSize));
9981:       PetscCall(DMPlexGetSupport(dm, cone[c], &support));
9982:       for (s = 0; s < supportSize; ++s) {
9983:         if (support[s] == p) break;
9984:       }
9985:       if (s >= supportSize || (dup && support[s + 1] != p)) {
9986:         PetscCall(PetscPrintf(PETSC_COMM_SELF, "p: %" PetscInt_FMT " cone: ", p));
9987:         for (s = 0; s < coneSize; ++s) PetscCall(PetscPrintf(PETSC_COMM_SELF, "%" PetscInt_FMT ", ", cone[s]));
9988:         PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
9989:         PetscCall(PetscPrintf(PETSC_COMM_SELF, "p: %" PetscInt_FMT " support: ", cone[c]));
9990:         for (s = 0; s < supportSize; ++s) PetscCall(PetscPrintf(PETSC_COMM_SELF, "%" PetscInt_FMT ", ", support[s]));
9991:         PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
9992:         PetscCheck(!dup, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %" PetscInt_FMT " not repeatedly found in support of repeated cone point %" PetscInt_FMT, p, cone[c]);
9993:         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %" PetscInt_FMT " not found in support of cone point %" PetscInt_FMT, p, cone[c]);
9994:       }
9995:     }
9996:     PetscCall(DMPlexGetTreeParent(dm, p, &pp, NULL));
9997:     if (p != pp) {
9998:       storagecheck = PETSC_FALSE;
9999:       continue;
10000:     }
10001:     PetscCall(DMPlexGetSupportSize(dm, p, &supportSize));
10002:     PetscCall(DMPlexGetSupport(dm, p, &support));
10003:     for (s = 0; s < supportSize; ++s) {
10004:       PetscCall(DMPlexGetConeSize(dm, support[s], &coneSize));
10005:       PetscCall(DMPlexGetCone(dm, support[s], &cone));
10006:       for (c = 0; c < coneSize; ++c) {
10007:         PetscCall(DMPlexGetTreeParent(dm, cone[c], &pp, NULL));
10008:         if (cone[c] != pp) {
10009:           c = 0;
10010:           break;
10011:         }
10012:         if (cone[c] == p) break;
10013:       }
10014:       if (c >= coneSize) {
10015:         PetscCall(PetscPrintf(PETSC_COMM_SELF, "p: %" PetscInt_FMT " support: ", p));
10016:         for (c = 0; c < supportSize; ++c) PetscCall(PetscPrintf(PETSC_COMM_SELF, "%" PetscInt_FMT ", ", support[c]));
10017:         PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
10018:         PetscCall(PetscPrintf(PETSC_COMM_SELF, "p: %" PetscInt_FMT " cone: ", support[s]));
10019:         for (c = 0; c < coneSize; ++c) PetscCall(PetscPrintf(PETSC_COMM_SELF, "%" PetscInt_FMT ", ", cone[c]));
10020:         PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
10021:         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %" PetscInt_FMT " not found in cone of support point %" PetscInt_FMT, p, support[s]);
10022:       }
10023:     }
10024:   }
10025:   if (storagecheck) {
10026:     PetscCall(PetscSectionGetStorageSize(coneSection, &csize));
10027:     PetscCall(PetscSectionGetStorageSize(supportSection, &ssize));
10028:     PetscCheck(csize == ssize, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %" PetscInt_FMT " != Total support size %" PetscInt_FMT, csize, ssize);
10029:   }
10030:   PetscFunctionReturn(PETSC_SUCCESS);
10031: }

10033: /*
10034:   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.
10035: */
10036: PetscErrorCode DMPlexCellUnsplitVertices_Internal(DM dm, PetscInt c, DMPolytopeType ct, PetscInt *unsplit)
10037: {
10038:   DMPolytopeType  cct;
10039:   PetscInt        ptpoints[4];
10040:   const PetscInt *cone, *ccone, *ptcone;
10041:   PetscInt        coneSize, cp, cconeSize, ccp, npt = 0, pt;

10043:   PetscFunctionBegin;
10044:   *unsplit = 0;
10045:   switch (ct) {
10046:   case DM_POLYTOPE_POINT_PRISM_TENSOR:
10047:     ptpoints[npt++] = c;
10048:     break;
10049:   case DM_POLYTOPE_SEG_PRISM_TENSOR:
10050:     PetscCall(DMPlexGetCone(dm, c, &cone));
10051:     PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
10052:     for (cp = 0; cp < coneSize; ++cp) {
10053:       PetscCall(DMPlexGetCellType(dm, cone[cp], &cct));
10054:       if (cct == DM_POLYTOPE_POINT_PRISM_TENSOR) ptpoints[npt++] = cone[cp];
10055:     }
10056:     break;
10057:   case DM_POLYTOPE_TRI_PRISM_TENSOR:
10058:   case DM_POLYTOPE_QUAD_PRISM_TENSOR:
10059:     PetscCall(DMPlexGetCone(dm, c, &cone));
10060:     PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
10061:     for (cp = 0; cp < coneSize; ++cp) {
10062:       PetscCall(DMPlexGetCone(dm, cone[cp], &ccone));
10063:       PetscCall(DMPlexGetConeSize(dm, cone[cp], &cconeSize));
10064:       for (ccp = 0; ccp < cconeSize; ++ccp) {
10065:         PetscCall(DMPlexGetCellType(dm, ccone[ccp], &cct));
10066:         if (cct == DM_POLYTOPE_POINT_PRISM_TENSOR) {
10067:           PetscInt p;
10068:           for (p = 0; p < npt; ++p)
10069:             if (ptpoints[p] == ccone[ccp]) break;
10070:           if (p == npt) ptpoints[npt++] = ccone[ccp];
10071:         }
10072:       }
10073:     }
10074:     break;
10075:   default:
10076:     break;
10077:   }
10078:   for (pt = 0; pt < npt; ++pt) {
10079:     PetscCall(DMPlexGetCone(dm, ptpoints[pt], &ptcone));
10080:     if (ptcone[0] == ptcone[1]) ++(*unsplit);
10081:   }
10082:   PetscFunctionReturn(PETSC_SUCCESS);
10083: }

10085: /*@
10086:   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices

10088:   Input Parameters:
10089: + dm         - The `DMPLEX` object
10090: - cellHeight - Normally 0

10092:   Level: developer

10094:   Notes:
10095:   This is a useful diagnostic when creating meshes programmatically.
10096:   Currently applicable only to homogeneous simplex or tensor meshes.

10098:   For the complete list of DMPlexCheck* functions, see `DMSetFromOptions()`.

10100: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMCreate()`, `DMSetFromOptions()`
10101: @*/
10102: PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscInt cellHeight)
10103: {
10104:   DMPlexInterpolatedFlag interp;
10105:   DMPolytopeType         ct;
10106:   PetscInt               vStart, vEnd, cStart, cEnd, c;

10108:   PetscFunctionBegin;
10110:   PetscCall(DMPlexIsInterpolated(dm, &interp));
10111:   PetscCall(DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd));
10112:   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
10113:   for (c = cStart; c < cEnd; ++c) {
10114:     PetscInt *closure = NULL;
10115:     PetscInt  coneSize, closureSize, cl, Nv = 0;

10117:     PetscCall(DMPlexGetCellType(dm, c, &ct));
10118:     if (ct == DM_POLYTOPE_UNKNOWN) continue;
10119:     if (interp == DMPLEX_INTERPOLATED_FULL) {
10120:       PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
10121:       PetscCheck(coneSize == DMPolytopeTypeGetConeSize(ct), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %" PetscInt_FMT " of type %s has cone size %" PetscInt_FMT " != %" PetscInt_FMT, c, DMPolytopeTypes[ct], coneSize, DMPolytopeTypeGetConeSize(ct));
10122:     }
10123:     PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure));
10124:     for (cl = 0; cl < closureSize * 2; cl += 2) {
10125:       const PetscInt p = closure[cl];
10126:       if (p >= vStart && p < vEnd) ++Nv;
10127:     }
10128:     PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure));
10129:     /* Special Case: Tensor faces with identified vertices */
10130:     if (Nv < DMPolytopeTypeGetNumVertices(ct)) {
10131:       PetscInt unsplit;

10133:       PetscCall(DMPlexCellUnsplitVertices_Internal(dm, c, ct, &unsplit));
10134:       if (Nv + unsplit == DMPolytopeTypeGetNumVertices(ct)) continue;
10135:     }
10136:     PetscCheck(Nv == DMPolytopeTypeGetNumVertices(ct), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %" PetscInt_FMT " of type %s has %" PetscInt_FMT " vertices != %" PetscInt_FMT, c, DMPolytopeTypes[ct], Nv, DMPolytopeTypeGetNumVertices(ct));
10137:   }
10138:   PetscFunctionReturn(PETSC_SUCCESS);
10139: }

10141: /*@
10142:   DMPlexCheckFaces - Check that the faces of each cell give a vertex order this is consistent with what we expect from the cell type

10144:   Collective

10146:   Input Parameters:
10147: + dm         - The `DMPLEX` object
10148: - cellHeight - Normally 0

10150:   Level: developer

10152:   Notes:
10153:   This is a useful diagnostic when creating meshes programmatically.
10154:   This routine is only relevant for meshes that are fully interpolated across all ranks.
10155:   It will error out if a partially interpolated mesh is given on some rank.
10156:   It will do nothing for locally uninterpolated mesh (as there is nothing to check).

10158:   For the complete list of DMPlexCheck* functions, see `DMSetFromOptions()`.

10160: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMCreate()`, `DMPlexGetVTKCellHeight()`, `DMSetFromOptions()`
10161: @*/
10162: PetscErrorCode DMPlexCheckFaces(DM dm, PetscInt cellHeight)
10163: {
10164:   PetscInt               dim, depth, vStart, vEnd, cStart, cEnd, c, h;
10165:   DMPlexInterpolatedFlag interpEnum;

10167:   PetscFunctionBegin;
10169:   PetscCall(DMPlexIsInterpolatedCollective(dm, &interpEnum));
10170:   if (interpEnum == DMPLEX_INTERPOLATED_NONE) PetscFunctionReturn(PETSC_SUCCESS);
10171:   if (interpEnum != DMPLEX_INTERPOLATED_FULL) {
10172:     PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "DMPlexCheckFaces() warning: Mesh is only partially interpolated, this is currently not supported"));
10173:     PetscFunctionReturn(PETSC_SUCCESS);
10174:   }

10176:   PetscCall(DMGetDimension(dm, &dim));
10177:   PetscCall(DMPlexGetDepth(dm, &depth));
10178:   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
10179:   for (h = cellHeight; h < PetscMin(depth, dim); ++h) {
10180:     PetscCall(DMPlexGetHeightStratum(dm, h, &cStart, &cEnd));
10181:     for (c = cStart; c < cEnd; ++c) {
10182:       const PetscInt       *cone, *ornt, *faceSizes, *faces;
10183:       const DMPolytopeType *faceTypes;
10184:       DMPolytopeType        ct;
10185:       PetscInt              numFaces, coneSize, f;
10186:       PetscInt             *closure = NULL, closureSize, cl, numCorners = 0, fOff = 0, unsplit;

10188:       PetscCall(DMPlexGetCellType(dm, c, &ct));
10189:       PetscCall(DMPlexCellUnsplitVertices_Internal(dm, c, ct, &unsplit));
10190:       if (unsplit) continue;
10191:       PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
10192:       PetscCall(DMPlexGetCone(dm, c, &cone));
10193:       PetscCall(DMPlexGetConeOrientation(dm, c, &ornt));
10194:       PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure));
10195:       for (cl = 0; cl < closureSize * 2; cl += 2) {
10196:         const PetscInt p = closure[cl];
10197:         if (p >= vStart && p < vEnd) closure[numCorners++] = p;
10198:       }
10199:       PetscCall(DMPlexGetRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces));
10200:       PetscCheck(coneSize == numFaces, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %" PetscInt_FMT " of type %s has %" PetscInt_FMT " faces but should have %" PetscInt_FMT, c, DMPolytopeTypes[ct], coneSize, numFaces);
10201:       for (f = 0; f < numFaces; ++f) {
10202:         DMPolytopeType fct;
10203:         PetscInt      *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;

10205:         PetscCall(DMPlexGetCellType(dm, cone[f], &fct));
10206:         PetscCall(DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure));
10207:         for (cl = 0; cl < fclosureSize * 2; cl += 2) {
10208:           const PetscInt p = fclosure[cl];
10209:           if (p >= vStart && p < vEnd) fclosure[fnumCorners++] = p;
10210:         }
10211:         PetscCheck(fnumCorners == faceSizes[f], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " of type %s (cone idx %" PetscInt_FMT ") of cell %" PetscInt_FMT " of type %s has %" PetscInt_FMT " vertices but should have %" PetscInt_FMT, cone[f], DMPolytopeTypes[fct], f, c, DMPolytopeTypes[ct], fnumCorners, faceSizes[f]);
10212:         for (v = 0; v < fnumCorners; ++v) {
10213:           if (fclosure[v] != faces[fOff + v]) {
10214:             PetscCall(PetscPrintf(PETSC_COMM_SELF, "face closure:"));
10215:             for (PetscInt v1 = 0; v1 < fnumCorners; ++v1) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, fclosure[v1]));
10216:             PetscCall(PetscPrintf(PETSC_COMM_SELF, "\ncell face:"));
10217:             for (PetscInt v1 = 0; v1 < fnumCorners; ++v1) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, faces[fOff + v1]));
10218:             PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
10219:             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]);
10220:           }
10221:         }
10222:         PetscCall(DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure));
10223:         fOff += faceSizes[f];
10224:       }
10225:       PetscCall(DMPlexRestoreRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces));
10226:       PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure));
10227:     }
10228:   }
10229:   PetscFunctionReturn(PETSC_SUCCESS);
10230: }

10232: /*@
10233:   DMPlexCheckGeometry - Check the geometry of mesh cells

10235:   Input Parameter:
10236: . dm - The `DMPLEX` object

10238:   Level: developer

10240:   Notes:
10241:   This is a useful diagnostic when creating meshes programmatically.

10243:   For the complete list of DMPlexCheck* functions, see `DMSetFromOptions()`.

10245: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMCreate()`, `DMSetFromOptions()`
10246: @*/
10247: PetscErrorCode DMPlexCheckGeometry(DM dm)
10248: {
10249:   Vec       coordinates;
10250:   PetscReal detJ, J[9], refVol = 1.0;
10251:   PetscReal vol;
10252:   PetscInt  dim, depth, dE, d, cStart, cEnd, c;

10254:   PetscFunctionBegin;
10255:   PetscCall(DMGetDimension(dm, &dim));
10256:   PetscCall(DMGetCoordinateDim(dm, &dE));
10257:   if (dim != dE) PetscFunctionReturn(PETSC_SUCCESS);
10258:   PetscCall(DMPlexGetDepth(dm, &depth));
10259:   for (d = 0; d < dim; ++d) refVol *= 2.0;
10260:   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
10261:   /* Make sure local coordinates are created, because that step is collective */
10262:   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
10263:   if (!coordinates) PetscFunctionReturn(PETSC_SUCCESS);
10264:   for (c = cStart; c < cEnd; ++c) {
10265:     DMPolytopeType ct;
10266:     PetscInt       unsplit;
10267:     PetscBool      ignoreZeroVol = PETSC_FALSE;

10269:     PetscCall(DMPlexGetCellType(dm, c, &ct));
10270:     switch (ct) {
10271:     case DM_POLYTOPE_SEG_PRISM_TENSOR:
10272:     case DM_POLYTOPE_TRI_PRISM_TENSOR:
10273:     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
10274:       ignoreZeroVol = PETSC_TRUE;
10275:       break;
10276:     default:
10277:       break;
10278:     }
10279:     switch (ct) {
10280:     case DM_POLYTOPE_TRI_PRISM:
10281:     case DM_POLYTOPE_TRI_PRISM_TENSOR:
10282:     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
10283:     case DM_POLYTOPE_PYRAMID:
10284:       continue;
10285:     default:
10286:       break;
10287:     }
10288:     PetscCall(DMPlexCellUnsplitVertices_Internal(dm, c, ct, &unsplit));
10289:     if (unsplit) continue;
10290:     PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, NULL, J, NULL, &detJ));
10291:     PetscCheck(detJ >= -PETSC_SMALL && (detJ > 0.0 || ignoreZeroVol), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %" PetscInt_FMT " of type %s is inverted, |J| = %g", c, DMPolytopeTypes[ct], (double)detJ);
10292:     PetscCall(PetscInfo(dm, "Cell %" PetscInt_FMT " FEM Volume %g\n", c, (double)(detJ * refVol)));
10293:     /* This should work with periodicity since DG coordinates should be used */
10294:     if (depth > 1) {
10295:       PetscCall(DMPlexComputeCellGeometryFVM(dm, c, &vol, NULL, NULL));
10296:       PetscCheck(vol >= -PETSC_SMALL && (vol > 0.0 || ignoreZeroVol), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %" PetscInt_FMT " of type %s is inverted, vol = %g", c, DMPolytopeTypes[ct], (double)vol);
10297:       PetscCall(PetscInfo(dm, "Cell %" PetscInt_FMT " FVM Volume %g\n", c, (double)vol));
10298:     }
10299:   }
10300:   PetscFunctionReturn(PETSC_SUCCESS);
10301: }

10303: /*@
10304:   DMPlexCheckPointSF - Check that several necessary conditions are met for the point `PetscSF` of this plex.

10306:   Collective

10308:   Input Parameters:
10309: + dm              - The `DMPLEX` object
10310: . pointSF         - The `PetscSF`, or `NULL` for `PointSF` attached to `DM`
10311: - allowExtraRoots - Flag to allow extra points not present in the `DM`

10313:   Level: developer

10315:   Notes:
10316:   This is mainly intended for debugging/testing purposes.

10318:   For the complete list of DMPlexCheck* functions, see `DMSetFromOptions()`.

10320:   Extra roots can come from periodic cuts, where additional points appear on the boundary

10322: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMGetPointSF()`, `DMSetFromOptions()`
10323: @*/
10324: PetscErrorCode DMPlexCheckPointSF(DM dm, PetscSF pointSF, PetscBool allowExtraRoots)
10325: {
10326:   PetscInt           l, nleaves, nroots, overlap;
10327:   const PetscInt    *locals;
10328:   const PetscSFNode *remotes;
10329:   PetscBool          distributed;
10330:   MPI_Comm           comm;
10331:   PetscMPIInt        rank;

10333:   PetscFunctionBegin;
10336:   else pointSF = dm->sf;
10337:   PetscCall(DMViewFromOptions(dm, NULL, "-dm_plex_point_sf_view"));
10338:   PetscCall(PetscSFViewFromOptions(pointSF, NULL, "-dm_plex_point_sf_view"));
10339:   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
10340:   PetscCheck(pointSF, comm, PETSC_ERR_ARG_WRONGSTATE, "DMPlex must have Point SF attached");
10341:   PetscCallMPI(MPI_Comm_rank(comm, &rank));
10342:   {
10343:     PetscMPIInt mpiFlag;

10345:     PetscCallMPI(MPI_Comm_compare(comm, PetscObjectComm((PetscObject)pointSF), &mpiFlag));
10346:     PetscCheck(mpiFlag == MPI_CONGRUENT || mpiFlag == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "DM and Point SF have different communicators (flag %d)", mpiFlag);
10347:   }
10348:   PetscCall(PetscSFGetGraph(pointSF, &nroots, &nleaves, &locals, &remotes));
10349:   PetscCall(DMPlexIsDistributed(dm, &distributed));
10350:   if (!distributed) {
10351:     PetscCheck(nroots < 0 || nleaves == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Undistributed DMPlex cannot have non-empty PointSF (has %" PetscInt_FMT " roots, %" PetscInt_FMT " leaves)", nroots, nleaves);
10352:     PetscFunctionReturn(PETSC_SUCCESS);
10353:   }
10354:   PetscCheck(nroots >= 0, comm, PETSC_ERR_ARG_WRONGSTATE, "This DMPlex is distributed but its PointSF has no graph set (has %" PetscInt_FMT " roots, %" PetscInt_FMT " leaves)", nroots, nleaves);
10355:   PetscCall(DMPlexGetOverlap(dm, &overlap));

10357:   /* Check SF graph is compatible with DMPlex chart */
10358:   {
10359:     PetscInt pStart, pEnd, maxLeaf;

10361:     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
10362:     PetscCall(PetscSFGetLeafRange(pointSF, NULL, &maxLeaf));
10363:     PetscCheck(allowExtraRoots || pEnd - pStart == nroots, PETSC_COMM_SELF, PETSC_ERR_PLIB, "pEnd - pStart = %" PetscInt_FMT " != nroots = %" PetscInt_FMT, pEnd - pStart, nroots);
10364:     PetscCheck(maxLeaf < pEnd, PETSC_COMM_SELF, PETSC_ERR_PLIB, "maxLeaf = %" PetscInt_FMT " >= pEnd = %" PetscInt_FMT, maxLeaf, pEnd);
10365:   }

10367:   /* Check there are no cells in interface */
10368:   if (!overlap) {
10369:     PetscInt cellHeight, cStart, cEnd;

10371:     PetscCall(DMPlexGetVTKCellHeight(dm, &cellHeight));
10372:     PetscCall(DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd));
10373:     for (l = 0; l < nleaves; ++l) {
10374:       const PetscInt point = locals ? locals[l] : l;

10376:       PetscCheck(point < cStart || point >= cEnd, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point SF contains %" PetscInt_FMT " which is a cell", point);
10377:     }
10378:   }

10380:   /* If some point is in interface, then all its cone points must be also in interface (either as leaves or roots) */
10381:   {
10382:     const PetscInt *rootdegree;

10384:     PetscCall(PetscSFComputeDegreeBegin(pointSF, &rootdegree));
10385:     PetscCall(PetscSFComputeDegreeEnd(pointSF, &rootdegree));
10386:     for (l = 0; l < nleaves; ++l) {
10387:       const PetscInt  point = locals ? locals[l] : l;
10388:       const PetscInt *cone;
10389:       PetscInt        coneSize, c, idx;

10391:       PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
10392:       PetscCall(DMPlexGetCone(dm, point, &cone));
10393:       for (c = 0; c < coneSize; ++c) {
10394:         if (!rootdegree[cone[c]]) {
10395:           if (locals) PetscCall(PetscFindInt(cone[c], nleaves, locals, &idx));
10396:           else idx = (cone[c] < nleaves) ? cone[c] : -1;
10397:           PetscCheck(idx >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point SF contains %" PetscInt_FMT " but not %" PetscInt_FMT " from its cone", point, cone[c]);
10398:         }
10399:       }
10400:     }
10401:   }

10403:   // Depths of leaves should match depths of root
10404:   //   Does not work for geometrically non-conforming meshes
10405:   if (!((DM_Plex *)dm->data)->parentSection) {
10406:     PetscInt   *starts, *gstarts, *depths;
10407:     PetscInt    depth;
10408:     PetscMPIInt size;
10409:     PetscBool   skip = PETSC_FALSE;

10411:     PetscCallMPI(MPI_Comm_size(comm, &size));
10412:     PetscCall(DMPlexGetDepth(dm, &depth));
10413:     PetscCall(PetscMalloc3(depth + 2, &starts, size * (depth + 2), &gstarts, depth + 2, &depths));
10414:     depths[0] = depth;
10415:     depths[1] = 0;
10416:     for (PetscInt d = 2; d <= depth; ++d) depths[d] = depth + 1 - d;
10417:     depths[depth + 1] = depth + 1;
10418:     for (PetscInt d = 0; d <= depth; ++d) {
10419:       PetscCall(DMPlexGetDepthStratum(dm, d, &starts[d], NULL));
10420:     }
10421:     // This is necessary because some strata might be missing
10422:     PetscCall(DMPlexGetChart(dm, NULL, &starts[depth + 1]));
10423:     PetscCallMPI(MPI_Allgather(starts, (int)(depth + 2), MPIU_INT, gstarts, (int)(depth + 2), MPIU_INT, comm));
10424:     // Check is invalid with empty strata
10425:     for (PetscInt p = 0; p < size * (depth + 2); ++p)
10426:       if (gstarts[p] < 0) skip = PETSC_TRUE;
10427:     for (l = skip ? nleaves : 0; l < nleaves; ++l) {
10428:       const PetscInt point  = locals ? locals[l] : l;
10429:       const PetscInt rpoint = remotes[l].index;
10430:       const PetscInt rrank  = remotes[l].rank;
10431:       PetscInt       pdepth, rdepth = -1;

10433:       PetscCall(DMPlexGetPointDepth(dm, point, &pdepth));
10434:       for (PetscInt d = 0; d <= depth; ++d) {
10435:         if (gstarts[rrank * (depth + 2) + depths[d]] <= rpoint && rpoint < gstarts[rrank * (depth + 2) + depths[d + 1]]) {
10436:           rdepth = depths[d];
10437:           break;
10438:         }
10439:       }
10440:       PetscCheck(rdepth != -1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Leaf %" PetscInt_FMT " (%" PetscInt_FMT ") was not found on remote rank %" PetscInt_FMT, point, rpoint, rrank);
10441:       PetscCheck(pdepth == rdepth, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Leaf %" PetscInt_FMT " has depth %" PetscInt_FMT " but remote (%" PetscInt_FMT ", %" PetscInt_FMT ") depth is %" PetscInt_FMT, point, pdepth, rpoint, rrank, rdepth);
10442:     }
10443:     PetscCall(PetscFree3(starts, gstarts, depths));
10444:   }
10445:   PetscFunctionReturn(PETSC_SUCCESS);
10446: }

10448: /*@
10449:   DMPlexCheckOrphanVertices - Check that no vertices are disconnected from the mesh, unless the mesh only consists of disconnected vertices.

10451:   Collective

10453:   Input Parameter:
10454: . dm - The `DMPLEX` object

10456:   Level: developer

10458:   Notes:
10459:   This is mainly intended for debugging/testing purposes.

10461:   Other cell types which are disconnected would be caught by the symmetry and face checks.

10463:   For the complete list of DMPlexCheck* functions, see `DMSetFromOptions()`.

10465: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCheck()`, `DMSetFromOptions()`
10466: @*/
10467: PetscErrorCode DMPlexCheckOrphanVertices(DM dm)
10468: {
10469:   PetscInt pStart, pEnd, vStart, vEnd;

10471:   PetscFunctionBegin;
10472:   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
10473:   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
10474:   if (pStart == vStart && pEnd == vEnd) PetscFunctionReturn(PETSC_SUCCESS);
10475:   for (PetscInt v = vStart; v < vEnd; ++v) {
10476:     PetscInt suppSize;

10478:     PetscCall(DMPlexGetSupportSize(dm, v, &suppSize));
10479:     PetscCheck(suppSize, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Vertex %" PetscInt_FMT " is disconnected from the mesh", v);
10480:   }
10481:   PetscFunctionReturn(PETSC_SUCCESS);
10482: }

10484: /*@
10485:   DMPlexCheckTransform - If the mesh was produced by a transform, run the transform verification check on it

10487:   Collective

10489:   Input Parameter:
10490: . dm - The `DMPLEX` object

10492:   Level: developer

10494:   Notes:
10495:   This is mainly intended for debugging/testing purposes.

10497:   For the complete list of DMPlexCheck* functions, see `DMSetFromOptions()`.

10499: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCheck()`, `DMSetFromOptions()`
10500: @*/
10501: PetscErrorCode DMPlexCheckTransform(DM dm)
10502: {
10503:   DMPlexTransform tr;

10505:   PetscFunctionBegin;
10506:   PetscCall(DMPlexGetTransform(dm, &tr));
10507:   if (tr) PetscCall(DMPlexTransformCheck(tr, dm));
10508:   PetscFunctionReturn(PETSC_SUCCESS);
10509: }

10511: /*@
10512:   DMPlexCheck - Perform various checks of `DMPLEX` sanity

10514:   Collective

10516:   Input Parameter:
10517: . dm - The `DMPLEX` object

10519:   Level: developer

10521:   Notes:
10522:   This is a useful diagnostic when creating meshes programmatically.

10524:   For the complete list of DMPlexCheck* functions, see `DMSetFromOptions()`.

10526:   Currently does not include `DMPlexCheckCellShape()`.

10528: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMCreate()`, `DMSetFromOptions()`
10529: @*/
10530: PetscErrorCode DMPlexCheck(DM dm)
10531: {
10532:   PetscInt cellHeight;

10534:   PetscFunctionBegin;
10535:   PetscCall(DMPlexGetVTKCellHeight(dm, &cellHeight));
10536:   PetscCall(DMPlexCheckSymmetry(dm));
10537:   PetscCall(DMPlexCheckSkeleton(dm, cellHeight));
10538:   PetscCall(DMPlexCheckFaces(dm, cellHeight));
10539:   PetscCall(DMPlexCheckGeometry(dm));
10540:   PetscCall(DMPlexCheckPointSF(dm, NULL, PETSC_FALSE));
10541:   PetscCall(DMPlexCheckInterfaceCones(dm));
10542:   PetscCall(DMPlexCheckOrphanVertices(dm));
10543:   PetscCall(DMPlexCheckTransform(dm));
10544:   PetscFunctionReturn(PETSC_SUCCESS);
10545: }

10547: typedef struct cell_stats {
10548:   PetscReal min, max, sum, squaresum;
10549:   PetscInt  count;
10550: } cell_stats_t;

10552: static void MPIAPI cell_stats_reduce(void *a, void *b, int *len, MPI_Datatype *datatype)
10553: {
10554:   PetscInt i, N = *len;

10556:   for (i = 0; i < N; i++) {
10557:     cell_stats_t *A = (cell_stats_t *)a;
10558:     cell_stats_t *B = (cell_stats_t *)b;

10560:     B->min = PetscMin(A->min, B->min);
10561:     B->max = PetscMax(A->max, B->max);
10562:     B->sum += A->sum;
10563:     B->squaresum += A->squaresum;
10564:     B->count += A->count;
10565:   }
10566: }

10568: /*@
10569:   DMPlexCheckCellShape - Checks the Jacobian of the mapping from reference to real cells and computes some minimal statistics.

10571:   Collective

10573:   Input Parameters:
10574: + dm        - The `DMPLEX` object
10575: . output    - If true, statistics will be displayed on `stdout`
10576: - condLimit - Display all cells above this condition number, or `PETSC_DETERMINE` for no cell output

10578:   Level: developer

10580:   Notes:
10581:   The condition number $\kappa_c$ of a cell $c$ is given by
10582:   ```{math}
10583:   \kappa_c = \left\lVert J_c \right\rVert \left\lVert J^{-1}_c \right\rVert
10584:   ```
10585:   where $J_c$ is the Jacobian of the mapping from the reference cell to cell $c$.

10587:   This is mainly intended for debugging/testing purposes.

10589:   For the complete list of DMPlexCheck* functions, see `DMSetFromOptions()`.

10591: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexComputeOrthogonalQuality()`
10592: @*/
10593: PetscErrorCode DMPlexCheckCellShape(DM dm, PetscBool output, PetscReal condLimit)
10594: {
10595:   DM           dmCoarse;
10596:   cell_stats_t stats, globalStats;
10597:   MPI_Comm     comm = PetscObjectComm((PetscObject)dm);
10598:   PetscReal   *J, *invJ, min = 0, max = 0, mean = 0, stdev = 0;
10599:   PetscReal    limit = condLimit > 0 ? condLimit : PETSC_MAX_REAL;
10600:   PetscInt     cdim, cStart, cEnd, c, eStart, eEnd, count = 0;
10601:   PetscMPIInt  rank, size;

10603:   PetscFunctionBegin;
10605:   stats.min = PETSC_MAX_REAL;
10606:   stats.max = PETSC_MIN_REAL;
10607:   stats.sum = stats.squaresum = 0.;
10608:   stats.count                 = 0;

10610:   PetscCallMPI(MPI_Comm_size(comm, &size));
10611:   PetscCallMPI(MPI_Comm_rank(comm, &rank));
10612:   PetscCall(DMGetCoordinateDim(dm, &cdim));
10613:   PetscCall(DMGetCoordinatesLocalSetUp(dm));
10614:   PetscCall(PetscMalloc2(PetscSqr(cdim), &J, PetscSqr(cdim), &invJ));
10615:   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
10616:   PetscCall(DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd));
10617:   for (c = cStart; c < cEnd; c++) {
10618:     PetscReal frobJ = 0., frobInvJ = 0., cond2, cond, detJ;

10620:     PetscCall(DMPlexComputeCellGeometryAffineFEM(dm, c, NULL, J, invJ, &detJ));
10621:     PetscCheck(detJ >= 0.0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %" PetscInt_FMT " is inverted", c);
10622:     for (PetscInt i = 0; i < PetscSqr(cdim); ++i) {
10623:       frobJ += J[i] * J[i];
10624:       frobInvJ += invJ[i] * invJ[i];
10625:     }
10626:     cond2 = frobJ * frobInvJ;
10627:     cond  = PetscSqrtReal(cond2);

10629:     stats.min = PetscMin(stats.min, cond);
10630:     stats.max = PetscMax(stats.max, cond);
10631:     stats.sum += cond;
10632:     stats.squaresum += cond2;
10633:     stats.count++;
10634:     if (output && cond > limit) {
10635:       PetscSection coordSection;
10636:       Vec          coordsLocal;
10637:       PetscScalar *coords = NULL;
10638:       PetscInt     Nv, d, clSize, cl, *closure = NULL;

10640:       PetscCall(DMGetCoordinatesLocal(dm, &coordsLocal));
10641:       PetscCall(DMGetCoordinateSection(dm, &coordSection));
10642:       PetscCall(DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &Nv, &coords));
10643:       PetscCall(PetscSynchronizedPrintf(comm, "[%d] Cell %" PetscInt_FMT " cond %g\n", rank, c, (double)cond));
10644:       for (PetscInt i = 0; i < Nv / cdim; ++i) {
10645:         PetscCall(PetscSynchronizedPrintf(comm, "  Vertex %" PetscInt_FMT ": (", i));
10646:         for (d = 0; d < cdim; ++d) {
10647:           if (d > 0) PetscCall(PetscSynchronizedPrintf(comm, ", "));
10648:           PetscCall(PetscSynchronizedPrintf(comm, "%g", (double)PetscRealPart(coords[i * cdim + d])));
10649:         }
10650:         PetscCall(PetscSynchronizedPrintf(comm, ")\n"));
10651:       }
10652:       PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure));
10653:       for (cl = 0; cl < clSize * 2; cl += 2) {
10654:         const PetscInt edge = closure[cl];

10656:         if (edge >= eStart && edge < eEnd) {
10657:           PetscReal len;

10659:           PetscCall(DMPlexComputeCellGeometryFVM(dm, edge, &len, NULL, NULL));
10660:           PetscCall(PetscSynchronizedPrintf(comm, "  Edge %" PetscInt_FMT ": length %g\n", edge, (double)len));
10661:         }
10662:       }
10663:       PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure));
10664:       PetscCall(DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, &Nv, &coords));
10665:     }
10666:   }
10667:   if (output) PetscCall(PetscSynchronizedFlush(comm, NULL));

10669:   if (size > 1) {
10670:     PetscMPIInt  blockLengths[2] = {4, 1};
10671:     MPI_Aint     blockOffsets[2] = {offsetof(cell_stats_t, min), offsetof(cell_stats_t, count)};
10672:     MPI_Datatype blockTypes[2]   = {MPIU_REAL, MPIU_INT}, statType;
10673:     MPI_Op       statReduce;

10675:     PetscCallMPI(MPI_Type_create_struct(2, blockLengths, blockOffsets, blockTypes, &statType));
10676:     PetscCallMPI(MPI_Type_commit(&statType));
10677:     PetscCallMPI(MPI_Op_create(cell_stats_reduce, PETSC_TRUE, &statReduce));
10678:     PetscCallMPI(MPI_Reduce(&stats, &globalStats, 1, statType, statReduce, 0, comm));
10679:     PetscCallMPI(MPI_Op_free(&statReduce));
10680:     PetscCallMPI(MPI_Type_free(&statType));
10681:   } else {
10682:     PetscCall(PetscArraycpy(&globalStats, &stats, 1));
10683:   }
10684:   if (rank == 0) {
10685:     count = globalStats.count;
10686:     min   = globalStats.min;
10687:     max   = globalStats.max;
10688:     mean  = globalStats.sum / globalStats.count;
10689:     stdev = globalStats.count > 1 ? PetscSqrtReal(PetscMax((globalStats.squaresum - globalStats.count * mean * mean) / (globalStats.count - 1), 0)) : 0.0;
10690:   }

10692:   if (output) PetscCall(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));
10693:   PetscCall(PetscFree2(J, invJ));

10695:   PetscCall(DMGetCoarseDM(dm, &dmCoarse));
10696:   if (dmCoarse) {
10697:     PetscBool isplex;

10699:     PetscCall(PetscObjectTypeCompare((PetscObject)dmCoarse, DMPLEX, &isplex));
10700:     if (isplex) PetscCall(DMPlexCheckCellShape(dmCoarse, output, condLimit));
10701:   }
10702:   PetscFunctionReturn(PETSC_SUCCESS);
10703: }

10705: /*@
10706:   DMPlexComputeOrthogonalQuality - Compute cell-wise orthogonal quality mesh statistic. Optionally tags all cells with
10707:   orthogonal quality below given tolerance.

10709:   Collective

10711:   Input Parameters:
10712: + dm   - The `DMPLEX` object
10713: . fv   - Optional `PetscFV` object for pre-computed cell/face centroid information
10714: - atol - [0, 1] Absolute tolerance for tagging cells.

10716:   Output Parameters:
10717: + OrthQual      - `Vec` containing orthogonal quality per cell
10718: - OrthQualLabel - `DMLabel` tagging cells below atol with `DM_ADAPT_REFINE`

10720:   Options Database Keys:
10721: + -dm_plex_orthogonal_quality_label_view - view OrthQualLabel if label is requested. Currently only `PETSCVIEWERASCII` is supported.
10722: - -dm_plex_orthogonal_quality_vec_view   - view OrthQual vector.

10724:   Level: intermediate

10726:   Notes:
10727:   Orthogonal quality is given by the following formula\:

10729:   $ \min \left[ \frac{A_i \cdot f_i}{\|A_i\| \|f_i\|} , \frac{A_i \cdot c_i}{\|A_i\| \|c_i\|} \right]$

10731:   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
10732:   is the vector from the current cells centroid to the centroid of its i'th neighbor (which shares a face with the
10733:   current cell). This computes the vector similarity between each cell face and its corresponding neighbor centroid by
10734:   calculating the cosine of the angle between these vectors.

10736:   Orthogonal quality ranges from 1 (best) to 0 (worst).

10738:   This routine is mainly useful for FVM, however is not restricted to only FVM. The `PetscFV` object is optionally used to check for
10739:   pre-computed FVM cell data, but if it is not passed in then this data will be computed.

10741:   Cells are tagged if they have an orthogonal quality less than or equal to the absolute tolerance.

10743: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCheckCellShape()`, `DMCreateLabel()`, `PetscFV`, `DMLabel`, `Vec`
10744: @*/
10745: PetscErrorCode DMPlexComputeOrthogonalQuality(DM dm, PeOp PetscFV fv, PetscReal atol, Vec *OrthQual, DMLabel *OrthQualLabel)
10746: {
10747:   PetscInt               nc, cellHeight, cStart, cEnd, cell, cellIter = 0;
10748:   PetscInt              *idx;
10749:   PetscScalar           *oqVals;
10750:   const PetscScalar     *cellGeomArr, *faceGeomArr;
10751:   PetscReal             *ci, *fi, *Ai;
10752:   MPI_Comm               comm;
10753:   Vec                    cellgeom, facegeom;
10754:   DM                     dmFace, dmCell;
10755:   IS                     glob;
10756:   ISLocalToGlobalMapping ltog;
10757:   PetscViewer            vwr;

10759:   PetscFunctionBegin;
10762:   PetscAssertPointer(OrthQual, 4);
10763:   PetscCheck(atol >= 0.0 && atol <= 1.0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Absolute tolerance %g not in [0,1]", (double)atol);
10764:   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
10765:   PetscCall(DMGetDimension(dm, &nc));
10766:   PetscCheck(nc >= 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM must have dimension >= 2 (current %" PetscInt_FMT ")", nc);
10767:   {
10768:     DMPlexInterpolatedFlag interpFlag;

10770:     PetscCall(DMPlexIsInterpolated(dm, &interpFlag));
10771:     if (interpFlag != DMPLEX_INTERPOLATED_FULL) {
10772:       PetscMPIInt rank;

10774:       PetscCallMPI(MPI_Comm_rank(comm, &rank));
10775:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM must be fully interpolated, DM on rank %d is not fully interpolated", rank);
10776:     }
10777:   }
10778:   if (OrthQualLabel) {
10779:     PetscAssertPointer(OrthQualLabel, 5);
10780:     PetscCall(DMCreateLabel(dm, "Orthogonal_Quality"));
10781:     PetscCall(DMGetLabel(dm, "Orthogonal_Quality", OrthQualLabel));
10782:   } else {
10783:     *OrthQualLabel = NULL;
10784:   }
10785:   PetscCall(DMPlexGetVTKCellHeight(dm, &cellHeight));
10786:   PetscCall(DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd));
10787:   PetscCall(DMPlexCreateCellNumbering(dm, PETSC_TRUE, &glob));
10788:   PetscCall(ISLocalToGlobalMappingCreateIS(glob, &ltog));
10789:   PetscCall(ISLocalToGlobalMappingSetType(ltog, ISLOCALTOGLOBALMAPPINGHASH));
10790:   PetscCall(VecCreate(comm, OrthQual));
10791:   PetscCall(VecSetType(*OrthQual, VECSTANDARD));
10792:   PetscCall(VecSetSizes(*OrthQual, cEnd - cStart, PETSC_DETERMINE));
10793:   PetscCall(VecSetLocalToGlobalMapping(*OrthQual, ltog));
10794:   PetscCall(VecSetUp(*OrthQual));
10795:   PetscCall(ISDestroy(&glob));
10796:   PetscCall(ISLocalToGlobalMappingDestroy(&ltog));
10797:   PetscCall(DMPlexGetDataFVM(dm, fv, &cellgeom, &facegeom, NULL));
10798:   PetscCall(VecGetArrayRead(cellgeom, &cellGeomArr));
10799:   PetscCall(VecGetArrayRead(facegeom, &faceGeomArr));
10800:   PetscCall(VecGetDM(cellgeom, &dmCell));
10801:   PetscCall(VecGetDM(facegeom, &dmFace));
10802:   PetscCall(PetscMalloc5(cEnd - cStart, &idx, cEnd - cStart, &oqVals, nc, &ci, nc, &fi, nc, &Ai));
10803:   for (cell = cStart; cell < cEnd; cellIter++, cell++) {
10804:     PetscInt         cellneigh, cellneighiter = 0, adjSize = PETSC_DETERMINE;
10805:     PetscInt         cellarr[2], *adj = NULL;
10806:     PetscScalar     *cArr, *fArr;
10807:     PetscReal        minvalc = 1.0, minvalf = 1.0;
10808:     PetscFVCellGeom *cg;

10810:     idx[cellIter] = cell - cStart;
10811:     cellarr[0]    = cell;
10812:     /* Make indexing into cellGeom easier */
10813:     PetscCall(DMPlexPointLocalRead(dmCell, cell, cellGeomArr, &cg));
10814:     PetscCall(DMPlexGetAdjacency_Internal(dm, cell, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &adjSize, &adj));
10815:     /* Technically 1 too big, but easier than fiddling with empty adjacency array */
10816:     PetscCall(PetscCalloc2(adjSize, &cArr, adjSize, &fArr));
10817:     for (cellneigh = 0; cellneigh < adjSize; cellneighiter++, cellneigh++) {
10818:       const PetscInt   neigh  = adj[cellneigh];
10819:       PetscReal        normci = 0, normfi = 0, normai = 0;
10820:       PetscFVCellGeom *cgneigh;
10821:       PetscFVFaceGeom *fg;

10823:       /* Don't count ourselves in the neighbor list */
10824:       if (neigh == cell) continue;
10825:       PetscCall(DMPlexPointLocalRead(dmCell, neigh, cellGeomArr, &cgneigh));
10826:       cellarr[1] = neigh;
10827:       {
10828:         PetscInt        numcovpts;
10829:         const PetscInt *covpts;

10831:         PetscCall(DMPlexGetMeet(dm, 2, cellarr, &numcovpts, &covpts));
10832:         PetscCall(DMPlexPointLocalRead(dmFace, covpts[0], faceGeomArr, &fg));
10833:         PetscCall(DMPlexRestoreMeet(dm, 2, cellarr, &numcovpts, &covpts));
10834:       }

10836:       /* Compute c_i, f_i and their norms */
10837:       for (PetscInt i = 0; i < nc; i++) {
10838:         ci[i] = cgneigh->centroid[i] - cg->centroid[i];
10839:         fi[i] = fg->centroid[i] - cg->centroid[i];
10840:         Ai[i] = fg->normal[i];
10841:         normci += PetscPowReal(ci[i], 2);
10842:         normfi += PetscPowReal(fi[i], 2);
10843:         normai += PetscPowReal(Ai[i], 2);
10844:       }
10845:       normci = PetscSqrtReal(normci);
10846:       normfi = PetscSqrtReal(normfi);
10847:       normai = PetscSqrtReal(normai);

10849:       /* Normalize and compute for each face-cell-normal pair */
10850:       for (PetscInt i = 0; i < nc; i++) {
10851:         ci[i] = ci[i] / normci;
10852:         fi[i] = fi[i] / normfi;
10853:         Ai[i] = Ai[i] / normai;
10854:         /* PetscAbs because I don't know if normals are guaranteed to point out */
10855:         cArr[cellneighiter] += PetscAbs(Ai[i] * ci[i]);
10856:         fArr[cellneighiter] += PetscAbs(Ai[i] * fi[i]);
10857:       }
10858:       if (PetscRealPart(cArr[cellneighiter]) < minvalc) minvalc = PetscRealPart(cArr[cellneighiter]);
10859:       if (PetscRealPart(fArr[cellneighiter]) < minvalf) minvalf = PetscRealPart(fArr[cellneighiter]);
10860:     }
10861:     PetscCall(PetscFree(adj));
10862:     PetscCall(PetscFree2(cArr, fArr));
10863:     /* Defer to cell if they're equal */
10864:     oqVals[cellIter] = PetscMin(minvalf, minvalc);
10865:     if (OrthQualLabel) {
10866:       if (PetscRealPart(oqVals[cellIter]) <= atol) PetscCall(DMLabelSetValue(*OrthQualLabel, cell, DM_ADAPT_REFINE));
10867:     }
10868:   }
10869:   PetscCall(VecSetValuesLocal(*OrthQual, cEnd - cStart, idx, oqVals, INSERT_VALUES));
10870:   PetscCall(VecAssemblyBegin(*OrthQual));
10871:   PetscCall(VecAssemblyEnd(*OrthQual));
10872:   PetscCall(VecRestoreArrayRead(cellgeom, &cellGeomArr));
10873:   PetscCall(VecRestoreArrayRead(facegeom, &faceGeomArr));
10874:   PetscCall(PetscOptionsCreateViewer(comm, NULL, NULL, "-dm_plex_orthogonal_quality_label_view", &vwr, NULL, NULL));
10875:   if (OrthQualLabel) {
10876:     if (vwr) PetscCall(DMLabelView(*OrthQualLabel, vwr));
10877:   }
10878:   PetscCall(PetscFree5(idx, oqVals, ci, fi, Ai));
10879:   PetscCall(PetscViewerDestroy(&vwr));
10880:   PetscCall(VecViewFromOptions(*OrthQual, NULL, "-dm_plex_orthogonal_quality_vec_view"));
10881:   PetscFunctionReturn(PETSC_SUCCESS);
10882: }

10884: /* this is here instead of DMGetOutputDM because output DM still has constraints in the local indices that affect
10885:  * interpolator construction */
10886: static PetscErrorCode DMGetFullDM(DM dm, DM *odm)
10887: {
10888:   PetscSection section, newSection, gsection;
10889:   PetscSF      sf;
10890:   PetscBool    hasConstraints;

10892:   PetscFunctionBegin;
10894:   PetscAssertPointer(odm, 2);
10895:   PetscCall(DMGetLocalSection(dm, &section));
10896:   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
10897:   PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &hasConstraints, 1, MPI_C_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
10898:   if (!hasConstraints) {
10899:     PetscCall(PetscObjectReference((PetscObject)dm));
10900:     *odm = dm;
10901:     PetscFunctionReturn(PETSC_SUCCESS);
10902:   }
10903:   PetscCall(DMClone(dm, odm));
10904:   PetscCall(DMCopyFields(dm, PETSC_DETERMINE, PETSC_DETERMINE, *odm));
10905:   PetscCall(DMGetLocalSection(*odm, &newSection));
10906:   PetscCall(DMGetPointSF(*odm, &sf));
10907:   PetscCall(PetscSectionCreateGlobalSection(newSection, sf, PETSC_TRUE, PETSC_TRUE, PETSC_FALSE, &gsection));
10908:   PetscCall(DMSetGlobalSection(*odm, gsection));
10909:   PetscCall(PetscSectionDestroy(&gsection));
10910:   PetscFunctionReturn(PETSC_SUCCESS);
10911: }

10913: static PetscErrorCode DMCreateAffineInterpolationCorrection_Plex(DM dmc, DM dmf, Vec *shift)
10914: {
10915:   DM        dmco, dmfo;
10916:   Mat       interpo;
10917:   Vec       rscale;
10918:   Vec       cglobalo, clocal;
10919:   Vec       fglobal, fglobalo, flocal;
10920:   PetscBool regular;

10922:   PetscFunctionBegin;
10923:   PetscCall(DMGetFullDM(dmc, &dmco));
10924:   PetscCall(DMGetFullDM(dmf, &dmfo));
10925:   PetscCall(DMSetCoarseDM(dmfo, dmco));
10926:   PetscCall(DMPlexGetRegularRefinement(dmf, &regular));
10927:   PetscCall(DMPlexSetRegularRefinement(dmfo, regular));
10928:   PetscCall(DMCreateInterpolation(dmco, dmfo, &interpo, &rscale));
10929:   PetscCall(DMCreateGlobalVector(dmco, &cglobalo));
10930:   PetscCall(DMCreateLocalVector(dmc, &clocal));
10931:   PetscCall(DMCreateGlobalVector(dmf, &fglobal));
10932:   PetscCall(DMCreateGlobalVector(dmfo, &fglobalo));
10933:   PetscCall(DMCreateLocalVector(dmf, &flocal));
10934:   PetscCall(DMPlexInsertBoundaryValues(dmc, PETSC_TRUE, clocal, 0., NULL, NULL, NULL));
10935:   PetscCall(DMLocalToGlobalBegin(dmco, clocal, INSERT_VALUES, cglobalo));
10936:   PetscCall(DMLocalToGlobalEnd(dmco, clocal, INSERT_VALUES, cglobalo));
10937:   PetscCall(MatMult(interpo, cglobalo, fglobalo));
10938:   PetscCall(DMGlobalToLocalBegin(dmfo, fglobalo, INSERT_VALUES, flocal));
10939:   PetscCall(DMGlobalToLocalEnd(dmfo, fglobalo, INSERT_VALUES, flocal));
10940:   PetscCall(DMLocalToGlobalBegin(dmf, flocal, INSERT_VALUES, fglobal));
10941:   PetscCall(DMLocalToGlobalEnd(dmf, flocal, INSERT_VALUES, fglobal));
10942:   *shift = fglobal;
10943:   PetscCall(VecDestroy(&flocal));
10944:   PetscCall(VecDestroy(&fglobalo));
10945:   PetscCall(VecDestroy(&clocal));
10946:   PetscCall(VecDestroy(&cglobalo));
10947:   PetscCall(VecDestroy(&rscale));
10948:   PetscCall(MatDestroy(&interpo));
10949:   PetscCall(DMDestroy(&dmfo));
10950:   PetscCall(DMDestroy(&dmco));
10951:   PetscFunctionReturn(PETSC_SUCCESS);
10952: }

10954: PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
10955: {
10956:   PetscObject shifto;
10957:   Vec         shift;

10959:   PetscFunctionBegin;
10960:   if (!interp) {
10961:     Vec rscale;

10963:     PetscCall(DMCreateInterpolation(coarse, fine, &interp, &rscale));
10964:     PetscCall(VecDestroy(&rscale));
10965:   } else {
10966:     PetscCall(PetscObjectReference((PetscObject)interp));
10967:   }
10968:   PetscCall(PetscObjectQuery((PetscObject)interp, "_DMInterpolateSolution_Plex_Vec", &shifto));
10969:   if (!shifto) {
10970:     PetscCall(DMCreateAffineInterpolationCorrection_Plex(coarse, fine, &shift));
10971:     PetscCall(PetscObjectCompose((PetscObject)interp, "_DMInterpolateSolution_Plex_Vec", (PetscObject)shift));
10972:     shifto = (PetscObject)shift;
10973:     PetscCall(VecDestroy(&shift));
10974:   }
10975:   shift = (Vec)shifto;
10976:   PetscCall(MatInterpolate(interp, coarseSol, fineSol));
10977:   PetscCall(VecAXPY(fineSol, 1.0, shift));
10978:   PetscCall(MatDestroy(&interp));
10979:   PetscFunctionReturn(PETSC_SUCCESS);
10980: }

10982: /* Pointwise interpolation
10983:      Just code FEM for now
10984:      u^f = I u^c
10985:      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
10986:      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
10987:      I_{ij} = psi^f_i phi^c_j
10988: */
10989: PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
10990: {
10991:   PetscSection gsc, gsf;
10992:   PetscInt     m, n;
10993:   void        *ctx;
10994:   DM           cdm;
10995:   PetscBool    regular, ismatis, isRefined = dmCoarse->data == dmFine->data ? PETSC_FALSE : PETSC_TRUE;

10997:   PetscFunctionBegin;
10998:   PetscCall(DMGetGlobalSection(dmFine, &gsf));
10999:   PetscCall(PetscSectionGetConstrainedStorageSize(gsf, &m));
11000:   PetscCall(DMGetGlobalSection(dmCoarse, &gsc));
11001:   PetscCall(PetscSectionGetConstrainedStorageSize(gsc, &n));

11003:   PetscCall(PetscStrcmp(dmCoarse->mattype, MATIS, &ismatis));
11004:   PetscCall(MatCreate(PetscObjectComm((PetscObject)dmCoarse), interpolation));
11005:   PetscCall(MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE));
11006:   PetscCall(MatSetType(*interpolation, ismatis ? MATAIJ : dmCoarse->mattype));
11007:   PetscCall(DMGetApplicationContext(dmFine, &ctx));

11009:   PetscCall(DMGetCoarseDM(dmFine, &cdm));
11010:   PetscCall(DMPlexGetRegularRefinement(dmFine, &regular));
11011:   if (!isRefined || (regular && cdm == dmCoarse)) PetscCall(DMPlexComputeInterpolatorNested(dmCoarse, dmFine, isRefined, *interpolation, ctx));
11012:   else PetscCall(DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx));
11013:   PetscCall(MatViewFromOptions(*interpolation, NULL, "-interp_mat_view"));
11014:   if (scaling) {
11015:     /* Use naive scaling */
11016:     PetscCall(DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling));
11017:   }
11018:   PetscFunctionReturn(PETSC_SUCCESS);
11019: }

11021: PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
11022: {
11023:   VecScatter ctx;

11025:   PetscFunctionBegin;
11026:   PetscCall(DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL));
11027:   PetscCall(MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat));
11028:   PetscCall(VecScatterDestroy(&ctx));
11029:   PetscFunctionReturn(PETSC_SUCCESS);
11030: }

11032: static void g0_identity_private(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[])
11033: {
11034:   const PetscInt f  = (PetscInt)PetscRealPart(constants[numConstants]);
11035:   const PetscInt Nc = uOff[f + 1] - uOff[f];
11036:   for (PetscInt c = 0; c < Nc; ++c) g0[c * Nc + c] = 1.0;
11037: }

11039: // The assumption here is that the test field is a vector and the basis field is a scalar (so we need the gradient)
11040: static void g1_identity_private(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g1[])
11041: {
11042:   for (PetscInt c = 0; c < dim; ++c) g1[c * dim + c] = 1.0;
11043: }

11045: PetscErrorCode DMCreateMassMatrixLumped_Plex(DM dm, Vec *lmass, Vec *mass)
11046: {
11047:   DM           dmc;
11048:   PetscDS      ds;
11049:   Vec          ones, locmass;
11050:   IS           cellIS;
11051:   PetscFormKey key;
11052:   PetscInt     depth;

11054:   PetscFunctionBegin;
11055:   PetscCall(DMClone(dm, &dmc));
11056:   PetscCall(DMCopyDisc(dm, dmc));
11057:   PetscCall(DMGetDS(dmc, &ds));
11058:   for (PetscInt f = 0; f < dmc->Nf; ++f) PetscCall(PetscDSSetJacobian(ds, f, f, g0_identity_private, NULL, NULL, NULL));
11059:   if (mass) PetscCall(DMCreateGlobalVector(dm, mass));
11060:   if (lmass) PetscCall(DMCreateLocalVector(dm, &locmass));
11061:   else PetscCall(DMGetLocalVector(dm, &locmass));
11062:   PetscCall(DMGetLocalVector(dm, &ones));
11063:   PetscCall(DMPlexGetDepth(dm, &depth));
11064:   PetscCall(DMGetStratumIS(dm, "depth", depth, &cellIS));
11065:   PetscCall(VecSet(locmass, 0.0));
11066:   PetscCall(VecSet(ones, 1.0));
11067:   key.label = NULL;
11068:   key.value = 0;
11069:   key.field = 0;
11070:   key.part  = 0;
11071:   PetscCall(DMPlexComputeJacobianActionByKey(dmc, key, cellIS, 0.0, 0.0, ones, NULL, ones, locmass, NULL));
11072:   PetscCall(ISDestroy(&cellIS));
11073:   if (mass) {
11074:     PetscCall(DMLocalToGlobalBegin(dm, locmass, ADD_VALUES, *mass));
11075:     PetscCall(DMLocalToGlobalEnd(dm, locmass, ADD_VALUES, *mass));
11076:   }
11077:   PetscCall(DMRestoreLocalVector(dm, &ones));
11078:   if (lmass) *lmass = locmass;
11079:   else PetscCall(DMRestoreLocalVector(dm, &locmass));
11080:   PetscCall(DMDestroy(&dmc));
11081:   PetscFunctionReturn(PETSC_SUCCESS);
11082: }

11084: PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mass)
11085: {
11086:   PetscSection gsc, gsf;
11087:   PetscInt     m, n;
11088:   void        *ctx;
11089:   DM           cdm;
11090:   PetscBool    regular;

11092:   PetscFunctionBegin;
11093:   if (dmFine == dmCoarse) {
11094:     DM            dmc;
11095:     PetscDS       ds;
11096:     PetscWeakForm wf;
11097:     Vec           u;
11098:     IS            cellIS;
11099:     PetscFormKey  key;
11100:     PetscInt      depth;

11102:     PetscCall(DMClone(dmFine, &dmc));
11103:     PetscCall(DMCopyDisc(dmFine, dmc));
11104:     PetscCall(DMGetDS(dmc, &ds));
11105:     PetscCall(PetscDSGetWeakForm(ds, &wf));
11106:     PetscCall(PetscWeakFormClear(wf));
11107:     for (PetscInt f = 0; f < dmc->Nf; ++f) PetscCall(PetscDSSetJacobian(ds, f, f, g0_identity_private, NULL, NULL, NULL));
11108:     PetscCall(DMCreateMatrix(dmc, mass));
11109:     PetscCall(DMGetLocalVector(dmc, &u));
11110:     PetscCall(DMPlexGetDepth(dmc, &depth));
11111:     PetscCall(DMGetStratumIS(dmc, "depth", depth, &cellIS));
11112:     PetscCall(MatZeroEntries(*mass));
11113:     key.label = NULL;
11114:     key.value = 0;
11115:     key.field = 0;
11116:     key.part  = 0;
11117:     PetscCall(DMPlexComputeJacobianByKey(dmc, key, cellIS, 0.0, 0.0, u, NULL, *mass, *mass, NULL));
11118:     PetscCall(ISDestroy(&cellIS));
11119:     PetscCall(DMRestoreLocalVector(dmc, &u));
11120:     PetscCall(DMDestroy(&dmc));
11121:   } else {
11122:     PetscCall(DMGetGlobalSection(dmFine, &gsf));
11123:     PetscCall(PetscSectionGetConstrainedStorageSize(gsf, &m));
11124:     PetscCall(DMGetGlobalSection(dmCoarse, &gsc));
11125:     PetscCall(PetscSectionGetConstrainedStorageSize(gsc, &n));

11127:     PetscCall(MatCreate(PetscObjectComm((PetscObject)dmCoarse), mass));
11128:     PetscCall(MatSetSizes(*mass, m, n, PETSC_DETERMINE, PETSC_DETERMINE));
11129:     PetscCall(MatSetType(*mass, dmCoarse->mattype));
11130:     PetscCall(DMGetApplicationContext(dmFine, &ctx));

11132:     PetscCall(DMGetCoarseDM(dmFine, &cdm));
11133:     PetscCall(DMPlexGetRegularRefinement(dmFine, &regular));
11134:     if (regular && cdm == dmCoarse) PetscCall(DMPlexComputeMassMatrixNested(dmCoarse, dmFine, *mass, ctx));
11135:     else PetscCall(DMPlexComputeMassMatrixGeneral(dmCoarse, dmFine, *mass, ctx));
11136:   }
11137:   PetscCall(MatViewFromOptions(*mass, NULL, "-mass_mat_view"));
11138:   PetscFunctionReturn(PETSC_SUCCESS);
11139: }

11141: PetscErrorCode DMCreateGradientMatrix_Plex(DM dmc, DM dmr, Mat *derv)
11142: {
11143:   PetscSection gsc, gsf;
11144:   PetscInt     m, n;
11145:   void        *ctx;

11147:   PetscFunctionBegin;
11148:   PetscCall(DMGetGlobalSection(dmr, &gsf));
11149:   PetscCall(PetscSectionGetConstrainedStorageSize(gsf, &m));
11150:   PetscCall(DMGetGlobalSection(dmc, &gsc));
11151:   PetscCall(PetscSectionGetConstrainedStorageSize(gsc, &n));

11153:   PetscCall(MatCreate(PetscObjectComm((PetscObject)dmc), derv));
11154:   PetscCall(PetscObjectSetName((PetscObject)*derv, "Plex Derivative Matrix"));
11155:   PetscCall(MatSetSizes(*derv, m, n, PETSC_DETERMINE, PETSC_DETERMINE));
11156:   PetscCall(MatSetType(*derv, dmc->mattype));

11158:   PetscCall(DMGetApplicationContext(dmr, &ctx));
11159:   {
11160:     DM            ndmr;
11161:     PetscDS       ds;
11162:     PetscWeakForm wf;
11163:     Vec           u;
11164:     IS            cellIS;
11165:     PetscFormKey  key;
11166:     PetscInt      depth, Nf;

11168:     PetscCall(DMClone(dmr, &ndmr));
11169:     PetscCall(DMCopyDisc(dmr, ndmr));
11170:     PetscCall(DMGetDS(ndmr, &ds));
11171:     PetscCall(PetscDSGetWeakForm(ds, &wf));
11172:     PetscCall(PetscWeakFormClear(wf));
11173:     PetscCall(PetscDSGetNumFields(ds, &Nf));
11174:     for (PetscInt f = 0; f < Nf; ++f) PetscCall(PetscDSSetJacobian(ds, f, f, NULL, g1_identity_private, NULL, NULL));
11175:     PetscCall(DMGetLocalVector(ndmr, &u));
11176:     PetscCall(DMPlexGetDepth(ndmr, &depth));
11177:     PetscCall(DMGetStratumIS(ndmr, "depth", depth, &cellIS));
11178:     PetscCall(MatZeroEntries(*derv));
11179:     key.label = NULL;
11180:     key.value = 0;
11181:     key.field = 0;
11182:     key.part  = 0;
11183:     PetscCall(DMPlexComputeJacobianByKeyGeneral(ndmr, dmc, key, cellIS, 0.0, 0.0, u, NULL, *derv, *derv, NULL));
11184:     PetscCall(ISDestroy(&cellIS));
11185:     PetscCall(DMRestoreLocalVector(ndmr, &u));
11186:     PetscCall(DMDestroy(&ndmr));
11187:   }
11188:   PetscCall(MatViewFromOptions(*derv, NULL, "-gradient_mat_view"));
11189:   PetscFunctionReturn(PETSC_SUCCESS);
11190: }

11192: /*@
11193:   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh

11195:   Input Parameter:
11196: . dm - The `DMPLEX` object

11198:   Output Parameter:
11199: . regular - The flag

11201:   Level: intermediate

11203: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexSetRegularRefinement()`
11204: @*/
11205: PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
11206: {
11207:   PetscFunctionBegin;
11209:   PetscAssertPointer(regular, 2);
11210:   *regular = ((DM_Plex *)dm->data)->regularRefinement;
11211:   PetscFunctionReturn(PETSC_SUCCESS);
11212: }

11214: /*@
11215:   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh

11217:   Input Parameters:
11218: + dm      - The `DMPLEX` object
11219: - regular - The flag

11221:   Level: intermediate

11223: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetRegularRefinement()`
11224: @*/
11225: PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
11226: {
11227:   PetscFunctionBegin;
11229:   ((DM_Plex *)dm->data)->regularRefinement = regular;
11230:   PetscFunctionReturn(PETSC_SUCCESS);
11231: }

11233: /*@
11234:   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
11235:   call DMPlexGetAnchors() directly: if there are anchors, then `DMPlexGetAnchors()` is called during `DMGetDefaultConstraints()`.

11237:   Not Collective

11239:   Input Parameter:
11240: . dm - The `DMPLEX` object

11242:   Output Parameters:
11243: + anchorSection - If not `NULL`, set to the section describing which points anchor the constrained points.
11244: - anchorIS      - If not `NULL`, set to the list of anchors indexed by `anchorSection`

11246:   Level: intermediate

11248: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexSetAnchors()`, `DMGetDefaultConstraints()`, `DMSetDefaultConstraints()`, `IS`, `PetscSection`
11249: @*/
11250: PetscErrorCode DMPlexGetAnchors(DM dm, PeOp PetscSection *anchorSection, PeOp IS *anchorIS)
11251: {
11252:   DM_Plex *plex = (DM_Plex *)dm->data;

11254:   PetscFunctionBegin;
11256:   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) PetscCall((*plex->createanchors)(dm));
11257:   if (anchorSection) *anchorSection = plex->anchorSection;
11258:   if (anchorIS) *anchorIS = plex->anchorIS;
11259:   PetscFunctionReturn(PETSC_SUCCESS);
11260: }

11262: /*@
11263:   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.

11265:   Collective

11267:   Input Parameters:
11268: + dm            - The `DMPLEX` object
11269: . anchorSection - The section that describes the mapping from constrained points to the anchor points listed in anchorIS.
11270:                   Must have a local communicator (`PETSC_COMM_SELF` or derivative).
11271: - anchorIS      - The list of all anchor points.  Must have a local communicator (`PETSC_COMM_SELF` or derivative).

11273:   Level: intermediate

11275:   Notes:
11276:   Unlike boundary conditions, when a point's degrees of freedom in a section are constrained to
11277:   an outside value, the anchor constraints set a point's degrees of freedom to be a linear
11278:   combination of other points' degrees of freedom.

11280:   After specifying the layout of constraints with `DMPlexSetAnchors()`, one specifies the constraints by calling
11281:   `DMGetDefaultConstraints()` and filling in the entries in the constraint matrix.

11283:   The reference counts of `anchorSection` and `anchorIS` are incremented.

11285: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetAnchors()`, `DMGetDefaultConstraints()`, `DMSetDefaultConstraints()`
11286: @*/
11287: PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
11288: {
11289:   DM_Plex    *plex = (DM_Plex *)dm->data;
11290:   PetscMPIInt result;

11292:   PetscFunctionBegin;
11294:   if (anchorSection) {
11296:     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)anchorSection), &result));
11297:     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "anchor section must have local communicator");
11298:   }
11299:   if (anchorIS) {
11301:     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)anchorIS), &result));
11302:     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "anchor IS must have local communicator");
11303:   }

11305:   PetscCall(PetscObjectReference((PetscObject)anchorSection));
11306:   PetscCall(PetscSectionDestroy(&plex->anchorSection));
11307:   plex->anchorSection = anchorSection;

11309:   PetscCall(PetscObjectReference((PetscObject)anchorIS));
11310:   PetscCall(ISDestroy(&plex->anchorIS));
11311:   plex->anchorIS = anchorIS;

11313:   if (PetscUnlikelyDebug(anchorIS && anchorSection)) {
11314:     PetscInt        size, a, pStart, pEnd;
11315:     const PetscInt *anchors;

11317:     PetscCall(PetscSectionGetChart(anchorSection, &pStart, &pEnd));
11318:     PetscCall(ISGetLocalSize(anchorIS, &size));
11319:     PetscCall(ISGetIndices(anchorIS, &anchors));
11320:     for (a = 0; a < size; a++) {
11321:       PetscInt p;

11323:       p = anchors[a];
11324:       if (p >= pStart && p < pEnd) {
11325:         PetscInt dof;

11327:         PetscCall(PetscSectionGetDof(anchorSection, p, &dof));
11328:         if (dof) {
11329:           PetscCall(ISRestoreIndices(anchorIS, &anchors));
11330:           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Point %" PetscInt_FMT " cannot be constrained and an anchor", p);
11331:         }
11332:       }
11333:     }
11334:     PetscCall(ISRestoreIndices(anchorIS, &anchors));
11335:   }
11336:   /* reset the generic constraints */
11337:   PetscCall(DMSetDefaultConstraints(dm, NULL, NULL, NULL));
11338:   PetscFunctionReturn(PETSC_SUCCESS);
11339: }

11341: static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
11342: {
11343:   PetscSection anchorSection;
11344:   PetscInt     pStart, pEnd, sStart, sEnd, p, dof, numFields, f;

11346:   PetscFunctionBegin;
11348:   PetscCall(DMPlexGetAnchors(dm, &anchorSection, NULL));
11349:   PetscCall(PetscSectionCreate(PETSC_COMM_SELF, cSec));
11350:   PetscCall(PetscSectionGetNumFields(section, &numFields));
11351:   if (numFields) {
11352:     PetscInt f;
11353:     PetscCall(PetscSectionSetNumFields(*cSec, numFields));

11355:     for (f = 0; f < numFields; f++) {
11356:       PetscInt numComp;

11358:       PetscCall(PetscSectionGetFieldComponents(section, f, &numComp));
11359:       PetscCall(PetscSectionSetFieldComponents(*cSec, f, numComp));
11360:     }
11361:   }
11362:   PetscCall(PetscSectionGetChart(anchorSection, &pStart, &pEnd));
11363:   PetscCall(PetscSectionGetChart(section, &sStart, &sEnd));
11364:   pStart = PetscMax(pStart, sStart);
11365:   pEnd   = PetscMin(pEnd, sEnd);
11366:   pEnd   = PetscMax(pStart, pEnd);
11367:   PetscCall(PetscSectionSetChart(*cSec, pStart, pEnd));
11368:   for (p = pStart; p < pEnd; p++) {
11369:     PetscCall(PetscSectionGetDof(anchorSection, p, &dof));
11370:     if (dof) {
11371:       PetscCall(PetscSectionGetDof(section, p, &dof));
11372:       PetscCall(PetscSectionSetDof(*cSec, p, dof));
11373:       for (f = 0; f < numFields; f++) {
11374:         PetscCall(PetscSectionGetFieldDof(section, p, f, &dof));
11375:         PetscCall(PetscSectionSetFieldDof(*cSec, p, f, dof));
11376:       }
11377:     }
11378:   }
11379:   PetscCall(PetscSectionSetUp(*cSec));
11380:   PetscCall(PetscObjectSetName((PetscObject)*cSec, "Constraint Section"));
11381:   PetscFunctionReturn(PETSC_SUCCESS);
11382: }

11384: static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
11385: {
11386:   PetscSection    aSec;
11387:   PetscInt        pStart, pEnd, p, sStart, sEnd, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
11388:   const PetscInt *anchors;
11389:   PetscInt        numFields, f;
11390:   IS              aIS;
11391:   MatType         mtype;
11392:   PetscBool       iscuda, iskokkos;

11394:   PetscFunctionBegin;
11396:   PetscCall(PetscSectionGetStorageSize(cSec, &m));
11397:   PetscCall(PetscSectionGetStorageSize(section, &n));
11398:   PetscCall(MatCreate(PETSC_COMM_SELF, cMat));
11399:   PetscCall(MatSetSizes(*cMat, m, n, m, n));
11400:   PetscCall(PetscStrcmp(dm->mattype, MATSEQAIJCUSPARSE, &iscuda));
11401:   if (!iscuda) PetscCall(PetscStrcmp(dm->mattype, MATMPIAIJCUSPARSE, &iscuda));
11402:   PetscCall(PetscStrcmp(dm->mattype, MATSEQAIJKOKKOS, &iskokkos));
11403:   if (!iskokkos) PetscCall(PetscStrcmp(dm->mattype, MATMPIAIJKOKKOS, &iskokkos));
11404:   if (iscuda) mtype = MATSEQAIJCUSPARSE;
11405:   else if (iskokkos) mtype = MATSEQAIJKOKKOS;
11406:   else mtype = MATSEQAIJ;
11407:   PetscCall(MatSetType(*cMat, mtype));
11408:   PetscCall(DMPlexGetAnchors(dm, &aSec, &aIS));
11409:   PetscCall(ISGetIndices(aIS, &anchors));
11410:   /* cSec will be a subset of aSec and section */
11411:   PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
11412:   PetscCall(PetscSectionGetChart(section, &sStart, &sEnd));
11413:   PetscCall(PetscMalloc1(m + 1, &i));
11414:   i[0] = 0;
11415:   PetscCall(PetscSectionGetNumFields(section, &numFields));
11416:   for (p = pStart; p < pEnd; p++) {
11417:     PetscInt rDof, rOff, r;

11419:     PetscCall(PetscSectionGetDof(aSec, p, &rDof));
11420:     if (!rDof) continue;
11421:     PetscCall(PetscSectionGetOffset(aSec, p, &rOff));
11422:     if (numFields) {
11423:       for (f = 0; f < numFields; f++) {
11424:         annz = 0;
11425:         for (r = 0; r < rDof; r++) {
11426:           a = anchors[rOff + r];
11427:           if (a < sStart || a >= sEnd) continue;
11428:           PetscCall(PetscSectionGetFieldDof(section, a, f, &aDof));
11429:           annz += aDof;
11430:         }
11431:         PetscCall(PetscSectionGetFieldDof(cSec, p, f, &dof));
11432:         PetscCall(PetscSectionGetFieldOffset(cSec, p, f, &off));
11433:         for (q = 0; q < dof; q++) i[off + q + 1] = i[off + q] + annz;
11434:       }
11435:     } else {
11436:       annz = 0;
11437:       PetscCall(PetscSectionGetDof(cSec, p, &dof));
11438:       for (q = 0; q < dof; q++) {
11439:         a = anchors[rOff + q];
11440:         if (a < sStart || a >= sEnd) continue;
11441:         PetscCall(PetscSectionGetDof(section, a, &aDof));
11442:         annz += aDof;
11443:       }
11444:       PetscCall(PetscSectionGetDof(cSec, p, &dof));
11445:       PetscCall(PetscSectionGetOffset(cSec, p, &off));
11446:       for (q = 0; q < dof; q++) i[off + q + 1] = i[off + q] + annz;
11447:     }
11448:   }
11449:   nnz = i[m];
11450:   PetscCall(PetscMalloc1(nnz, &j));
11451:   offset = 0;
11452:   for (p = pStart; p < pEnd; p++) {
11453:     if (numFields) {
11454:       for (f = 0; f < numFields; f++) {
11455:         PetscCall(PetscSectionGetFieldDof(cSec, p, f, &dof));
11456:         for (q = 0; q < dof; q++) {
11457:           PetscInt rDof, rOff, r;
11458:           PetscCall(PetscSectionGetDof(aSec, p, &rDof));
11459:           PetscCall(PetscSectionGetOffset(aSec, p, &rOff));
11460:           for (r = 0; r < rDof; r++) {
11461:             a = anchors[rOff + r];
11462:             if (a < sStart || a >= sEnd) continue;
11463:             PetscCall(PetscSectionGetFieldDof(section, a, f, &aDof));
11464:             PetscCall(PetscSectionGetFieldOffset(section, a, f, &aOff));
11465:             for (PetscInt s = 0; s < aDof; s++) j[offset++] = aOff + s;
11466:           }
11467:         }
11468:       }
11469:     } else {
11470:       PetscCall(PetscSectionGetDof(cSec, p, &dof));
11471:       for (q = 0; q < dof; q++) {
11472:         PetscInt rDof, rOff, r;
11473:         PetscCall(PetscSectionGetDof(aSec, p, &rDof));
11474:         PetscCall(PetscSectionGetOffset(aSec, p, &rOff));
11475:         for (r = 0; r < rDof; r++) {
11476:           a = anchors[rOff + r];
11477:           if (a < sStart || a >= sEnd) continue;
11478:           PetscCall(PetscSectionGetDof(section, a, &aDof));
11479:           PetscCall(PetscSectionGetOffset(section, a, &aOff));
11480:           for (PetscInt s = 0; s < aDof; s++) j[offset++] = aOff + s;
11481:         }
11482:       }
11483:     }
11484:   }
11485:   PetscCall(MatSeqAIJSetPreallocationCSR(*cMat, i, j, NULL));
11486:   PetscCall(PetscFree(i));
11487:   PetscCall(PetscFree(j));
11488:   PetscCall(ISRestoreIndices(aIS, &anchors));
11489:   PetscFunctionReturn(PETSC_SUCCESS);
11490: }

11492: PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
11493: {
11494:   DM_Plex     *plex = (DM_Plex *)dm->data;
11495:   PetscSection anchorSection, section, cSec;
11496:   Mat          cMat;

11498:   PetscFunctionBegin;
11500:   PetscCall(DMPlexGetAnchors(dm, &anchorSection, NULL));
11501:   if (anchorSection) {
11502:     PetscInt Nf;

11504:     PetscCall(DMGetLocalSection(dm, &section));
11505:     PetscCall(DMPlexCreateConstraintSection_Anchors(dm, section, &cSec));
11506:     PetscCall(DMPlexCreateConstraintMatrix_Anchors(dm, section, cSec, &cMat));
11507:     PetscCall(DMGetNumFields(dm, &Nf));
11508:     if (Nf && plex->computeanchormatrix) PetscCall((*plex->computeanchormatrix)(dm, section, cSec, cMat));
11509:     PetscCall(DMSetDefaultConstraints(dm, cSec, cMat, NULL));
11510:     PetscCall(PetscSectionDestroy(&cSec));
11511:     PetscCall(MatDestroy(&cMat));
11512:   }
11513:   PetscFunctionReturn(PETSC_SUCCESS);
11514: }

11516: PetscErrorCode DMCreateSubDomainDM_Plex(DM dm, DMLabel label, PetscInt value, IS *is, DM *subdm)
11517: {
11518:   IS           subis;
11519:   PetscSection section, subsection;

11521:   PetscFunctionBegin;
11522:   PetscCall(DMGetLocalSection(dm, &section));
11523:   PetscCheck(section, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Must set default section for DM before splitting subdomain");
11524:   PetscCheck(subdm, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Must set output subDM for splitting subdomain");
11525:   /* Create subdomain */
11526:   PetscCall(DMPlexFilter(dm, label, value, PETSC_FALSE, PETSC_FALSE, PetscObjectComm((PetscObject)dm), NULL, subdm));
11527:   /* Create submodel */
11528:   PetscCall(DMPlexGetSubpointIS(*subdm, &subis));
11529:   PetscCall(PetscSectionCreateSubmeshSection(section, subis, &subsection));
11530:   PetscCall(DMSetLocalSection(*subdm, subsection));
11531:   PetscCall(PetscSectionDestroy(&subsection));
11532:   PetscCall(DMCopyDisc(dm, *subdm));
11533:   /* Create map from submodel to global model */
11534:   if (is) {
11535:     PetscSection    sectionGlobal, subsectionGlobal;
11536:     IS              spIS;
11537:     const PetscInt *spmap;
11538:     PetscInt       *subIndices;
11539:     PetscInt        subSize = 0, subOff = 0, pStart, pEnd, p;
11540:     PetscInt        Nf, f, bs = -1, bsLocal[2], bsMinMax[2];

11542:     PetscCall(DMPlexGetSubpointIS(*subdm, &spIS));
11543:     PetscCall(ISGetIndices(spIS, &spmap));
11544:     PetscCall(PetscSectionGetNumFields(section, &Nf));
11545:     PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
11546:     PetscCall(DMGetGlobalSection(*subdm, &subsectionGlobal));
11547:     PetscCall(PetscSectionGetChart(subsection, &pStart, &pEnd));
11548:     for (p = pStart; p < pEnd; ++p) {
11549:       PetscInt gdof, pSubSize = 0;

11551:       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
11552:       if (gdof > 0) {
11553:         for (f = 0; f < Nf; ++f) {
11554:           PetscInt fdof, fcdof;

11556:           PetscCall(PetscSectionGetFieldDof(subsection, p, f, &fdof));
11557:           PetscCall(PetscSectionGetFieldConstraintDof(subsection, p, f, &fcdof));
11558:           pSubSize += fdof - fcdof;
11559:         }
11560:         subSize += pSubSize;
11561:         if (pSubSize) {
11562:           if (bs < 0) {
11563:             bs = pSubSize;
11564:           } else if (bs != pSubSize) {
11565:             /* Layout does not admit a pointwise block size */
11566:             bs = 1;
11567:           }
11568:         }
11569:       }
11570:     }
11571:     /* Must have same blocksize on all procs (some might have no points) */
11572:     bsLocal[0] = bs < 0 ? PETSC_INT_MAX : bs;
11573:     bsLocal[1] = bs;
11574:     PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax));
11575:     if (bsMinMax[0] != bsMinMax[1]) bs = 1;
11576:     else bs = bsMinMax[0];
11577:     PetscCall(PetscMalloc1(subSize, &subIndices));
11578:     for (p = pStart; p < pEnd; ++p) {
11579:       PetscInt gdof, goff;

11581:       PetscCall(PetscSectionGetDof(subsectionGlobal, p, &gdof));
11582:       if (gdof > 0) {
11583:         const PetscInt point = spmap[p];

11585:         PetscCall(PetscSectionGetOffset(sectionGlobal, point, &goff));
11586:         for (f = 0; f < Nf; ++f) {
11587:           PetscInt fdof, fcdof, fc, f2, poff = 0;

11589:           /* Can get rid of this loop by storing field information in the global section */
11590:           for (f2 = 0; f2 < f; ++f2) {
11591:             PetscCall(PetscSectionGetFieldDof(section, p, f2, &fdof));
11592:             PetscCall(PetscSectionGetFieldConstraintDof(section, p, f2, &fcdof));
11593:             poff += fdof - fcdof;
11594:           }
11595:           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
11596:           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
11597:           for (fc = 0; fc < fdof - fcdof; ++fc, ++subOff) subIndices[subOff] = goff + poff + fc;
11598:         }
11599:       }
11600:     }
11601:     PetscCall(ISRestoreIndices(spIS, &spmap));
11602:     PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), subSize, subIndices, PETSC_OWN_POINTER, is));
11603:     if (bs > 1) {
11604:       /* We need to check that the block size does not come from non-contiguous fields */
11605:       PetscInt i, j, set = 1;
11606:       for (i = 0; i < subSize; i += bs) {
11607:         for (j = 0; j < bs; ++j) {
11608:           if (subIndices[i + j] != subIndices[i] + j) {
11609:             set = 0;
11610:             break;
11611:           }
11612:         }
11613:       }
11614:       if (set) PetscCall(ISSetBlockSize(*is, bs));
11615:     }
11616:     // Attach nullspace
11617:     if (dm->nullspaceConstructors) {
11618:       for (f = 0; f < Nf; ++f) {
11619:         (*subdm)->nullspaceConstructors[f] = dm->nullspaceConstructors[f];
11620:         if ((*subdm)->nullspaceConstructors[f]) break;
11621:       }
11622:       if (f < Nf) {
11623:         MatNullSpace nullSpace;
11624:         PetscCall((*(*subdm)->nullspaceConstructors[f])(*subdm, f, f, &nullSpace));

11626:         PetscCall(PetscObjectCompose((PetscObject)*is, "nullspace", (PetscObject)nullSpace));
11627:         PetscCall(MatNullSpaceDestroy(&nullSpace));
11628:       }
11629:     }
11630:   }
11631:   PetscFunctionReturn(PETSC_SUCCESS);
11632: }

11634: /*@
11635:   DMPlexMonitorThroughput - Report the cell throughput of FE integration

11637:   Input Parameters:
11638: + dm     - The `DM`
11639: - unused - unused argument

11641:   Options Database Key:
11642: . -dm_plex_monitor_throughput - Activate the monitor

11644:   Level: developer

11646: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexCreate()`
11647: @*/
11648: PetscErrorCode DMPlexMonitorThroughput(DM dm, void *unused)
11649: {
11650:   PetscLogHandler default_handler;

11652:   PetscFunctionBegin;
11654:   PetscCall(PetscLogGetDefaultHandler(&default_handler));
11655:   if (default_handler) {
11656:     PetscLogEvent      event;
11657:     PetscEventPerfInfo eventInfo;
11658:     PetscLogDouble     cellRate, flopRate;
11659:     PetscInt           cStart, cEnd, Nf, N;
11660:     const char        *name;

11662:     PetscCall(PetscObjectGetName((PetscObject)dm, &name));
11663:     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
11664:     PetscCall(DMGetNumFields(dm, &Nf));
11665:     PetscCall(PetscLogEventGetId("DMPlexResidualFE", &event));
11666:     PetscCall(PetscLogEventGetPerfInfo(PETSC_DEFAULT, event, &eventInfo));
11667:     N        = (cEnd - cStart) * Nf * eventInfo.count;
11668:     flopRate = eventInfo.flops / eventInfo.time;
11669:     cellRate = N / eventInfo.time;
11670:     PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "DM (%s) FE Residual Integration: %" PetscInt_FMT " integrals %d reps\n  Cell rate: %.2g/s flop rate: %.2g MF/s\n", name ? name : "unknown", N, eventInfo.count, cellRate, flopRate / 1.e6));
11671:   } else {
11672:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Plex Throughput Monitor is not supported if logging is turned off or the default log handler is not running. Reconfigure using --with-log and run with -log_view.");
11673:   }
11674:   PetscFunctionReturn(PETSC_SUCCESS);
11675: }

11677: static inline PetscInt DMPlex_GlobalID(PetscInt point)
11678: {
11679:   return point >= 0 ? point : -(point + 1);
11680: }

11682: /* Return the unsorted radius neighborhood of p, including p; the caller owns *adj. */
11683: static PetscErrorCode DMPlexGetAdjacencyRadius_Private(DM dm, PetscInt p, PetscInt radius, PetscInt *nadj, PetscInt *adj[])
11684: {
11685:   PetscHSetI ht   = NULL;
11686:   PetscInt  *pts  = NULL;
11687:   PetscInt   npts = 0, index = 0;

11689:   PetscFunctionBegin;
11690:   if (radius < 2) {
11691:     PetscCall(DMPlexGetAdjacency(dm, p, nadj, adj));
11692:     PetscFunctionReturn(PETSC_SUCCESS);
11693:   }
11694:   PetscCall(PetscHSetICreate(&ht));
11695:   PetscCall(PetscHSetIAdd(ht, p));
11696:   for (PetscInt r = 0; r < radius; ++r) {
11697:     /* Expand only the points present at the start of this iteration. */
11698:     PetscCall(PetscHSetIGetSize(ht, &npts));
11699:     PetscCall(PetscMalloc1(npts, &pts));
11700:     index = 0;
11701:     PetscCall(PetscHSetIGetElems(ht, &index, pts));
11702:     for (PetscInt k = 0; k < npts; ++k) {
11703:       PetscInt  n = PETSC_DETERMINE;
11704:       PetscInt *a = NULL;

11706:       PetscCall(DMPlexGetAdjacency(dm, pts[k], &n, &a));
11707:       for (PetscInt s = 0; s < n; ++s) PetscCall(PetscHSetIAdd(ht, a[s]));
11708:       PetscCall(PetscFree(a));
11709:     }
11710:     PetscCall(PetscFree(pts));
11711:   }
11712:   PetscCall(PetscHSetIGetSize(ht, nadj));
11713:   PetscCall(PetscMalloc1(*nadj, adj));
11714:   index = 0;
11715:   PetscCall(PetscHSetIGetElems(ht, &index, *adj));
11716:   PetscCall(PetscHSetIDestroy(&ht));
11717:   PetscFunctionReturn(PETSC_SUCCESS);
11718: }

11720: /*
11721:   Mark selected points in [pStart, pEnd). In local mode, owned points are numbered in increasing
11722:   point order and ghost points are marked PETSC_INT_MIN. In global mode, selected points carry
11723:   their global indices. Unselected points are marked PETSC_INT_MIN.
11724: */
11725: static PetscErrorCode DMPlexCreateSubsetNumbering_Private(DM dm, PetscInt pStart, PetscInt pEnd, DMLabel label, PetscInt value, PetscBool local, PetscInt *numOwned, PetscInt *numbering[], IS *points)
11726: {
11727:   PetscSection    section, globalSection = NULL;
11728:   const PetscInt *leaves = NULL;
11729:   PetscInt       *nums, *pts;
11730:   PetscInt        n = 0, nleaves = 0;

11732:   PetscFunctionBegin;
11733:   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section));
11734:   PetscCall(PetscSectionSetChart(section, pStart, pEnd));
11735:   for (PetscInt p = pStart; p < pEnd; ++p) {
11736:     PetscBool selected = PETSC_TRUE;

11738:     if (label != NULL) PetscCall(DMLabelStratumHasPoint(label, value, p, &selected));
11739:     if (selected == PETSC_TRUE) PetscCall(PetscSectionSetDof(section, p, 1));
11740:   }
11741:   PetscCall(PetscSectionSetUp(section));
11742:   if (local == PETSC_TRUE) {
11743:     PetscCall(PetscSFGetGraph(dm->sf, NULL, &nleaves, &leaves, NULL));
11744:     nleaves = PetscMax(0, nleaves);
11745:   } else PetscCall(PetscSectionCreateGlobalSection(section, dm->sf, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &globalSection));
11746:   PetscCall(PetscMalloc1(pEnd - pStart, &nums));
11747:   PetscCall(PetscMalloc1(pEnd - pStart, &pts));
11748:   for (PetscInt p = pStart; p < pEnd; ++p) {
11749:     PetscInt dof;

11751:     nums[p - pStart] = PETSC_INT_MIN;
11752:     PetscCall(PetscSectionGetDof(section, p, &dof));
11753:     if (!dof) continue;
11754:     if (local == PETSC_TRUE) {
11755:       PetscInt loc;

11757:       if (leaves != NULL) PetscCall(PetscFindInt(p, nleaves, leaves, &loc));
11758:       else loc = (p >= 0 && p < nleaves) ? p : -1;
11759:       if (loc < 0) {
11760:         nums[p - pStart] = n;
11761:         pts[n++]         = p;
11762:       }
11763:     } else {
11764:       PetscInt off;

11766:       PetscCall(PetscSectionGetOffset(globalSection, p, &off));
11767:       if (off >= 0) {
11768:         nums[p - pStart] = off;
11769:         pts[n++]         = p;
11770:       } else nums[p - pStart] = DMPlex_GlobalID(off);
11771:     }
11772:   }
11773:   PetscCall(PetscSectionDestroy(&section));
11774:   PetscCall(PetscSectionDestroy(&globalSection));
11775:   if (numOwned != NULL) *numOwned = n;
11776:   if (points != NULL) PetscCall(ISCreateGeneral(PETSC_COMM_SELF, n, pts, PETSC_COPY_VALUES, points));
11777:   PetscCall(PetscFree(pts));
11778:   if (numbering != NULL) *numbering = nums;
11779:   else PetscCall(PetscFree(nums));
11780:   PetscFunctionReturn(PETSC_SUCCESS);
11781: }

11783: /*
11784:   Build the graph Laplacian L = D - A for points at depth, optionally restricted by label. Points
11785:   are connected when one lies in the other's radius neighborhood. oPoints returns the owned
11786:   selected points in row order. In local mode, each process gets the graph its own points induce,
11787:   on PETSC_COMM_SELF.
11788: */
11789: static PetscErrorCode DMPlexCreateGraphLaplacian_Private(DM dm, PetscInt depth, PetscInt radius, DMLabel label, PetscInt value, PetscBool local, Mat *oL, IS *oPoints)
11790: {
11791:   Mat             L, preall;
11792:   Vec             x, y;
11793:   IS              points;
11794:   MPI_Comm        comm;
11795:   const PetscInt *pts;
11796:   PetscInt       *numbering   = NULL, *i, *j, *numDof;
11797:   PetscInt        numVertices = 0, numEdges = 0, shift, maxnnzrow, dim, numFields, iptr = 0;
11798:   PetscInt        pStart, pEnd;
11799:   PetscScalar    *vals;
11800:   PetscSection    s;

11802:   PetscFunctionBeginUser;
11803:   comm = local == PETSC_TRUE ? PETSC_COMM_SELF : PetscObjectComm((PetscObject)dm);
11804:   PetscCall(DMGetDimension(dm, &dim));
11805:   {
11806:     PetscCall(DMPlexGetDepthStratum(dm, depth, &pStart, &pEnd));
11807:     PetscCall(DMPlexCreateSubsetNumbering_Private(dm, pStart, pEnd, label, value, local, &numVertices, &numbering, &points));
11808:     PetscCall(ISGetIndices(points, &pts));
11809:     /* Keep edges only when both endpoints are selected. */
11810:     for (PetscInt v = 0; v < numVertices; v++) {
11811:       PetscInt  nadj = PETSC_DETERMINE;
11812:       PetscInt *adj  = NULL;

11814:       PetscCall(DMPlexGetAdjacencyRadius_Private(dm, pts[v], radius, &nadj, &adj));
11815:       for (PetscInt a = 0; a < nadj; a++)
11816:         if (adj[a] != pts[v] && pStart <= adj[a] && adj[a] < pEnd && numbering[adj[a] - pStart] != PETSC_INT_MIN) numEdges++;
11817:       PetscCall(PetscFree(adj));
11818:     }
11819:     PetscCall(PetscMalloc1(numVertices + 1, &i));
11820:     PetscCall(PetscMalloc1(numEdges, &j));
11821:     i[0] = iptr;
11822:     for (PetscInt v = 0; v < numVertices; v++) {
11823:       PetscInt  nadj = PETSC_DETERMINE;
11824:       PetscInt *adj  = NULL;

11826:       PetscCall(DMPlexGetAdjacencyRadius_Private(dm, pts[v], radius, &nadj, &adj));
11827:       for (PetscInt a = 0; a < nadj; a++)
11828:         if (adj[a] != pts[v] && pStart <= adj[a] && adj[a] < pEnd && numbering[adj[a] - pStart] != PETSC_INT_MIN) j[iptr++] = numbering[adj[a] - pStart];
11829:       PetscCall(PetscFree(adj));
11830:       i[v + 1] = iptr;
11831:       /* Sort adjacencies (not strictly necessary) */
11832:       if (i[v + 1] > i[v]) PetscCall(PetscSortInt(i[v + 1] - i[v], &j[i[v]]));
11833:     }
11834:     PetscCall(ISRestoreIndices(points, &pts));
11835:     PetscCall(PetscFree(numbering));
11836:   }
11837:   /* First create a matrix object */
11838:   PetscCall(MatCreate(comm, &L));
11839:   PetscCall(MatSetSizes(L, numVertices, numVertices, PETSC_DECIDE, PETSC_DECIDE));
11840:   PetscCall(MatSetOptionsPrefix(L, "dm_plex_laplacian_"));
11841:   PetscCall(MatSetFromOptions(L));
11842:   /* Preallocation */
11843:   PetscCall(MatCreate(comm, &preall));
11844:   PetscCall(MatSetSizes(preall, numVertices, numVertices, PETSC_DECIDE, PETSC_DECIDE));
11845:   PetscCall(MatSetType(preall, MATPREALLOCATOR));
11846:   PetscCall(MatSetUp(preall));
11847:   PetscCall(MatGetOwnershipRange(preall, &shift, NULL));
11848:   maxnnzrow = 0;
11849:   for (PetscInt k = 0; k < numVertices; k++) {
11850:     PetscInt  nnzrow = i[k + 1] - i[k];
11851:     PetscInt  row    = shift + k;
11852:     PetscInt *col    = PetscSafePointerPlusOffset(j, i[k]);
11853:     maxnnzrow        = PetscMax(maxnnzrow, nnzrow);
11854:     /* Add adjacency connection */
11855:     PetscCall(MatSetValues(preall, 1, &row, nnzrow, col, NULL, INSERT_VALUES));
11856:     /* The graph CSR does not represent self-to-self connections, we need them
11857:        for the graph laplacian */
11858:     PetscCall(MatSetValues(preall, 1, &row, 1, &row, NULL, INSERT_VALUES));
11859:   }
11860:   PetscCall(MatAssemblyBegin(preall, MAT_FINAL_ASSEMBLY));
11861:   PetscCall(MatAssemblyEnd(preall, MAT_FINAL_ASSEMBLY));
11862:   /* Preallocate the graph laplacian matrix */
11863:   PetscCall(MatPreallocatorPreallocate(preall, PETSC_TRUE, L));
11864:   PetscCall(MatDestroy(&preall));
11865:   /* Set values. We first set all values to -1.0 to obtain -A,
11866:      and then use matrix API to modify for our needs and add the diagonal D matrix */
11867:   PetscCall(PetscMalloc1(maxnnzrow, &vals));
11868:   for (PetscInt k = 0; k < maxnnzrow; k++) vals[k] = -1.0;
11869:   for (PetscInt k = 0; k < numVertices; k++) {
11870:     PetscInt  nnzrow = i[k + 1] - i[k];
11871:     PetscInt  row    = shift + k;
11872:     PetscInt *col    = PetscSafePointerPlusOffset(j, i[k]);
11873:     PetscCall(MatSetValues(L, 1, &row, nnzrow, col, vals, INSERT_VALUES));
11874:   }
11875:   PetscCall(MatAssemblyBegin(L, MAT_FINAL_ASSEMBLY));
11876:   PetscCall(MatAssemblyEnd(L, MAT_FINAL_ASSEMBLY));
11877:   /* Add D. Here we use the fact that D = rowsum(A) */
11878:   PetscCall(MatCreateVecs(L, &x, &y));
11879:   PetscCall(VecSet(x, -1.0));
11880:   PetscCall(MatMult(L, x, y));
11881:   PetscCall(MatDiagonalSet(L, y, INSERT_VALUES));
11882:   PetscCall(MatAssemblyBegin(L, MAT_FINAL_ASSEMBLY));
11883:   PetscCall(MatAssemblyEnd(L, MAT_FINAL_ASSEMBLY));
11884:   PetscCall(VecDestroy(&x));
11885:   PetscCall(VecDestroy(&y));
11886:   /* Clean up */
11887:   PetscCall(PetscFree(vals));
11888:   PetscCall(PetscFree(i));
11889:   PetscCall(PetscFree(j));
11890:   /* Allow command line view via -laplacian_view */
11891:   PetscCall(MatViewFromOptions(L, NULL, "-view"));
11892:   /* The matrix layout matches the DM only for the full, global stratum. */
11893:   if (label == NULL && local == PETSC_FALSE) {
11894:     PetscCall(DMClone(dm, &dm));
11895:     numFields = 1;
11896:     PetscCall(DMSetNumFields(dm, numFields));
11897:     PetscCall(PetscCalloc1(dim + 1, &numDof));
11898:     numDof[depth] = 1;
11899:     PetscCall(DMPlexCreateSection(dm, NULL, &numFields, numDof, 0, NULL, NULL, NULL, NULL, &s));
11900:     PetscCall(DMSetLocalSection(dm, s));
11901:     PetscCall(PetscSectionDestroy(&s));
11902:     PetscCall(PetscFree(numDof));
11903:     PetscCall(MatSetDM(L, dm));
11904:     PetscCall(DMDestroy(&dm));
11905:   }
11906:   *oL = L;
11907:   if (oPoints != NULL) *oPoints = points;
11908:   else PetscCall(ISDestroy(&points));
11909:   PetscFunctionReturn(PETSC_SUCCESS);
11910: }

11912: /*@
11913:   DMPlexCreateColoring - Gets coloring of the connectivity graph of the `DMPlex` points at a given depth.

11915:   Collective

11917:   Input Parameters:
11918: + dm       - the `DMPlex` object
11919: . depth    - the dimension of the entities in the connectivity graph.
11920: - distance - how far through the mesh a point reaches, in applications of the adjacency (1 for a star, 2 for its closure).

11922:   Output Parameter:
11923: . coloring - the coloring

11925:   Level: developer

11927:   Notes:
11928:   Unlike `DMCreateColoring`, the graph used for the coloring does not represent the operator matrix associated with the discretization of a PDE on the `DM`.
11929:   Here the coloring is computed from the connectivity graph of the mesh entities.

11931:   Coloring of matrices can also be computed directly from the sparse matrix nonzero structure via the `MatColoring` object or from the mesh from which the
11932:   matrix comes from (what this function provides). In general using the mesh produces a more optimal coloring (fewer colors).

11934:   Mesh colorings are useful for additive and multiplicative Schwarz methods.
11935:   In particular, they mitigate overhead costs associated with setting up individual KSPs and PCs on many subdomains per process.
11936:   A coloring with `distance=1` groups non-overlapping star patches into multi-patch subdomains, and one with `distance=2`
11937:   groups non-overlapping Vanka patches, whose reach is the closure of a star. Either works at any `depth`.

11939:   This colors the whole stratum. Use `DMPlexCreateColoringLabel()` to color a subset of it, and see that routine for
11940:   the options controlling the ordering and hence the number of colors.

11942: .seealso: [](ch_unstructured), `DMPlex`, `ISColoring`, `MatColoring`, `DMCreateColoring()`, `DMPlexCreateColoringLabel()`
11943: @*/
11944: PetscErrorCode DMPlexCreateColoring(DM dm, PetscInt depth, PetscInt distance, ISColoring *coloring)
11945: {
11946:   PetscFunctionBegin;
11947:   PetscCall(DMPlexCreateColoringLabel(dm, depth, distance, NULL, 0, coloring));
11948:   PetscFunctionReturn(PETSC_SUCCESS);
11949: }

11951: /*@
11952:   DMPlexCreateColoringLabel - Gets coloring of the connectivity graph of the `DMPlex` points at a given depth that are marked by a `DMLabel`.

11954:   Collective

11956:   Input Parameters:
11957: + dm       - the `DMPlex` object
11958: . depth    - the dimension of the entities in the connectivity graph.
11959: . distance - how far through the mesh a point reaches, in applications of the adjacency (1 for a star, 2 for its closure).
11960: . label    - the `DMLabel` selecting the points to color, or `NULL` to color the whole stratum
11961: - value    - the stratum value of `label` selecting the points, ignored when `label` is `NULL`

11963:   Output Parameter:
11964: . coloring - the coloring, in `DMPlex` point numbers

11966:   Options Database Keys:
11967: + -dm_plex_coloring_local                                           - color the points each process owns by themselves, without communicating
11968: . -dm_plex_coloring_ordering_type name                              - order the points with `MatGetOrdering()` before coloring them
11969: . -dm_plex_coloring_mat_coloring_type name                          - the `MatColoringType` used to color the connectivity graph
11970: - -dm_plex_coloring_mat_coloring_weight_type (RANDOM|LEXICAL|LF|SL) - the vertex weighting, which sets the order in which points are colored

11972:   Level: developer

11974:   Notes:
11975:   The graph is the subgraph induced by the selected points: two selected points are connected exactly when one lies in
11976:   the other's neighborhood, the points that `distance` applications of the adjacency reach. Restricting the graph this
11977:   way, rather than coloring the whole stratum and discarding the unselected points afterwards, both costs work
11978:   proportional to the selected set and uses fewer colors, since points whose neighbors are all unselected become
11979:   isolated and can share a color.

11981:   Points of one color lie outside one another's neighborhoods, which is exactly the statement that the patches of that
11982:   reach are disjoint: with `distance` one their stars share no cell, and with `distance` two the closures of those
11983:   stars, which is what a Vanka patch spans, share no point. Counting hops through the mesh rather than through the
11984:   graph is what makes this hold at every depth. At `depth` zero the two agree, but the cell stratum induces no edges
11985:   at all, since the finite-element adjacency of a cell is its own closure, and only a neighborhood reaching past that
11986:   cell separates one cell patch from the next.

11988:   The adjacency is the one configured on `dm` by `DMSetBasicAdjacency()`. For grouping patches, that must be the
11989:   finite-element adjacency (`useCone` `PETSC_FALSE`, `useClosure` `PETSC_TRUE`), for which two vertices are adjacent
11990:   exactly when they share a cell. In parallel the neighborhood of an owned point must be complete on its process, so
11991:   the mesh overlap has to be at least `distance`.

11993:   By default the graph spans the whole mesh, so a point is colored against its neighbors on other processes and the
11994:   coloring is the same one a serial run would produce. With `-dm_plex_coloring_local` each process instead colors the
11995:   graph its own points induce, on `PETSC_COMM_SELF`, needing no communication and at most as many colors, but the
11996:   resulting colors are only meaningful process by process and their number varies between processes. That suits a
11997:   caller that consumes each process's colors on their own, such as `PCPATCH`, which only builds patches around the
11998:   points a process owns, and never one that treats a color as a global object.

12000:   `MATCOLORINGGREEDY` colors the points in order of decreasing weight, so the ordering determines the number of
12001:   colors. This routine defaults to `MAT_COLORING_WEIGHT_LEXICAL`, which sweeps in the point numbering and yields the
12002:   optimal four colors on a structured grid, where the `MatColoring` default of random weights uses seven. Use
12003:   `-dm_plex_coloring_ordering_type` to sweep in a different order when the point numbering has no locality. A
12004:   bandwidth-reducing ordering is not what serves a coloring: `MATORDERINGRCM` is a wavefront, and sweeping a
12005:   structured grid diagonally costs it six colors rather than four. `MatGetOrdering()` reaches the graph through
12006:   `MatGetRowIJ()`, which is not supported for parallel matrices, so requesting an ordering on more than one process
12007:   raises an error.

12009: .seealso: [](ch_unstructured), `DMPlex`, `ISColoring`, `MatColoring`, `DMCreateColoring()`, `DMPlexCreateColoring()`, `DMSetBasicAdjacency()`, `MatGetOrdering()`
12010: @*/
12011: PetscErrorCode DMPlexCreateColoringLabel(DM dm, PetscInt depth, PetscInt distance, DMLabel label, PetscInt value, ISColoring *coloring)
12012: {
12013:   Mat             L        = NULL;
12014:   MatColoring     mc       = NULL;
12015:   IS             *iscolors = NULL;
12016:   IS              points;
12017:   const PetscInt *pts;
12018:   PetscInt       *idx;
12019:   PetscInt        rowStart = 0, numVertices = 0, ncolors = 0;
12020:   char            ordering[PETSC_MAX_PATH_LEN];
12021:   PetscBool       local = PETSC_FALSE, flg;

12023:   PetscFunctionBegin;
12026:   PetscAssertPointer(coloring, 6);
12027:   PetscOptionsBegin(PetscObjectComm((PetscObject)dm), "dm_plex_coloring_", "DMPlex point coloring options", "DMPlex");
12028:   PetscCall(PetscOptionsBool("-local", "Color the points each process owns by themselves, without communicating", "DMPlexCreateColoringLabel", local, &local, NULL));
12029:   PetscCall(PetscOptionsFList("-ordering_type", "Reorder the points with MatGetOrdering() before coloring them", "MatGetOrdering", MatOrderingList, NULL, ordering, sizeof(ordering), &flg));
12030:   PetscOptionsEnd();
12031:   PetscCall(DMPlexCreateGraphLaplacian_Private(dm, depth, distance, label, value, local, &L, &points));
12032:   PetscCall(MatGetOwnershipRange(L, &rowStart, NULL));
12033:   PetscCall(ISGetLocalSize(points, &numVertices));
12034:   PetscCall(MatColoringCreate(L, &mc));
12035:   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)mc, "dm_plex_coloring_"));
12036:   PetscCall(MatColoringSetType(mc, MATCOLORINGGREEDY));
12037:   /* The mesh distance is encoded in the graph; color its one-hop overlap graph. */
12038:   PetscCall(MatColoringSetDistance(mc, 1));
12039:   PetscCall(MatColoringSetWeightType(mc, MAT_COLORING_WEIGHT_LEXICAL));
12040:   PetscCall(MatColoringSetFromOptions(mc));
12041:   if (flg == PETSC_TRUE) {
12042:     IS              rperm, cperm;
12043:     const PetscInt *perm;
12044:     PetscReal      *wts;
12045:     PetscInt        n;
12046:     PetscMPIInt     size;

12048:     PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
12049:     PetscCheck(local == PETSC_TRUE || size == 1, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Reordering the points before coloring them needs -dm_plex_coloring_local in parallel, because MatGetOrdering() reaches the graph through MatGetRowIJ()");
12050:     PetscCall(MatGetOrdering(L, ordering, &rperm, &cperm));
12051:     PetscCall(ISGetLocalSize(rperm, &n));
12052:     PetscCheck(n == numVertices, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_SIZ, "Ordering %s returned %" PetscInt_FMT " indices, but the graph has %" PetscInt_FMT " local points", ordering, n, numVertices);
12053:     PetscCall(ISGetIndices(rperm, &perm));
12054:     PetscCall(PetscMalloc1(n, &wts));
12055:     /* Greedy coloring visits points in decreasing weight order. */
12056:     for (PetscInt k = 0; k < n; k++) wts[perm[k]] = (PetscReal)(n - k);
12057:     PetscCall(ISRestoreIndices(rperm, &perm));
12058:     PetscCall(MatColoringSetWeights(mc, wts, NULL));
12059:     PetscCall(PetscFree(wts));
12060:     PetscCall(ISDestroy(&rperm));
12061:     PetscCall(ISDestroy(&cperm));
12062:   }
12063:   PetscCall(MatColoringApply(mc, coloring));
12064:   PetscCall(MatColoringDestroy(&mc));
12065:   PetscCall(MatDestroy(&L));
12066:   /* Convert graph-row indices back to DMPlex point numbers. */
12067:   PetscCall(ISGetIndices(points, &pts));
12068:   PetscCall(PetscMalloc1(numVertices, &idx));
12069:   PetscCall(ISColoringGetIS(*coloring, PETSC_USE_POINTER, &ncolors, &iscolors));
12070:   for (PetscInt c = 0; c < ncolors; c++) {
12071:     const PetscInt *rows;
12072:     PetscInt        n;

12074:     PetscCall(ISGetLocalSize(iscolors[c], &n));
12075:     PetscCall(ISGetIndices(iscolors[c], &rows));
12076:     for (PetscInt k = 0; k < n; k++) idx[k] = pts[rows[k] - rowStart];
12077:     PetscCall(ISRestoreIndices(iscolors[c], &rows));
12078:     PetscCall(ISGeneralSetIndices(iscolors[c], n, idx, PETSC_COPY_VALUES));
12079:   }
12080:   PetscCall(ISColoringRestoreIS(*coloring, PETSC_USE_POINTER, &iscolors));
12081:   PetscCall(PetscFree(idx));
12082:   PetscCall(ISRestoreIndices(points, &pts));
12083:   PetscCall(ISDestroy(&points));
12084:   PetscFunctionReturn(PETSC_SUCCESS);
12085: }