Actual source code: plex.c

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

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

 14: PETSC_EXTERN PetscErrorCode VecView_MPI(Vec, PetscViewer);

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

 19:   Input Parameter:
 20: . dm      - The DMPlex object

 22:   Output Parameter:
 23: . simplex - Flag checking for a simplex

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

 28:   Level: intermediate

 30: .seealso DMPlexGetSimplexOrBoxCells(), DMPlexGetCellType(), DMPlexGetHeightStratum(), DMPolytopeTypeGetNumVertices()
 31: @*/
 32: PetscErrorCode DMPlexIsSimplex(DM dm, PetscBool *simplex)
 33: {
 34:   DMPolytopeType ct;
 35:   PetscInt       cStart, cEnd;

 37:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
 38:   if (cEnd <= cStart) {*simplex = PETSC_FALSE; return 0;}
 39:   DMPlexGetCellType(dm, cStart, &ct);
 40:   *simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
 41:   return 0;
 42: }

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

 47:   Input Parameters:
 48: + dm     - The DMPlex object
 49: - height - The cell height in the Plex, 0 is the default

 51:   Output Parameters:
 52: + cStart - The first "normal" cell
 53: - cEnd   - The upper bound on "normal"" cells

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

 57:   Level: developer

 59: .seealso DMPlexConstructGhostCells(), DMPlexGetGhostCellStratum()
 60: @*/
 61: PetscErrorCode DMPlexGetSimplexOrBoxCells(DM dm, PetscInt height, PetscInt *cStart, PetscInt *cEnd)
 62: {
 63:   DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
 64:   PetscInt       cS, cE, c;

 66:   DMPlexGetHeightStratum(dm, PetscMax(height, 0), &cS, &cE);
 67:   for (c = cS; c < cE; ++c) {
 68:     DMPolytopeType cct;

 70:     DMPlexGetCellType(dm, c, &cct);
 71:     if ((PetscInt) cct < 0) break;
 72:     switch (cct) {
 73:       case DM_POLYTOPE_POINT:
 74:       case DM_POLYTOPE_SEGMENT:
 75:       case DM_POLYTOPE_TRIANGLE:
 76:       case DM_POLYTOPE_QUADRILATERAL:
 77:       case DM_POLYTOPE_TETRAHEDRON:
 78:       case DM_POLYTOPE_HEXAHEDRON:
 79:         ct = cct;
 80:         break;
 81:       default: break;
 82:     }
 83:     if (ct != DM_POLYTOPE_UNKNOWN) break;
 84:   }
 85:   if (ct != DM_POLYTOPE_UNKNOWN) {
 86:     DMLabel ctLabel;

 88:     DMPlexGetCellTypeLabel(dm, &ctLabel);
 89:     DMLabelGetStratumBounds(ctLabel, ct, &cS, &cE);
 90:   }
 91:   if (cStart) *cStart = cS;
 92:   if (cEnd)   *cEnd   = cE;
 93:   return 0;
 94: }

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

101:   *ft  = PETSC_VTK_INVALID;
102:   DMGetCoordinateDim(dm, &cdim);
103:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
104:   DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);
105:   PetscSectionGetChart(section, &pStart, &pEnd);
106:   if (field >= 0) {
107:     if ((vStart >= pStart) && (vStart < pEnd)) PetscSectionGetFieldDof(section, vStart, field, &vcdof[0]);
108:     if ((cStart >= pStart) && (cStart < pEnd)) PetscSectionGetFieldDof(section, cStart, field, &vcdof[1]);
109:   } else {
110:     if ((vStart >= pStart) && (vStart < pEnd)) PetscSectionGetDof(section, vStart, &vcdof[0]);
111:     if ((cStart >= pStart) && (cStart < pEnd)) PetscSectionGetDof(section, cStart, &vcdof[1]);
112:   }
113:   MPI_Allreduce(vcdof, globalvcdof, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));
114:   if (globalvcdof[0]) {
115:     *sStart = vStart;
116:     *sEnd   = vEnd;
117:     if (globalvcdof[0] == cdim) *ft = PETSC_VTK_POINT_VECTOR_FIELD;
118:     else                        *ft = PETSC_VTK_POINT_FIELD;
119:   } else if (globalvcdof[1]) {
120:     *sStart = cStart;
121:     *sEnd   = cEnd;
122:     if (globalvcdof[1] == cdim) *ft = PETSC_VTK_CELL_VECTOR_FIELD;
123:     else                        *ft = PETSC_VTK_CELL_FIELD;
124:   } else {
125:     if (field >= 0) {
126:       const char *fieldname;

128:       PetscSectionGetFieldName(section, field, &fieldname);
129:       PetscInfo((PetscObject) dm, "Could not classify VTK output type of section field %D \"%s\"\n", field, fieldname);
130:     } else {
131:       PetscInfo((PetscObject) dm, "Could not classify VTK output type of section\"%s\"\n");
132:     }
133:   }
134:   return 0;
135: }

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

140:   Collective on dm

142:   Input Parameters:
143: + dm - The DMPlex
144: . n  - The number of vectors
145: . u  - The array of local vectors
146: - viewer - The Draw viewer

148:   Level: advanced

150: .seealso: VecViewFromOptions(), VecView()
151: @*/
152: PetscErrorCode DMPlexVecView1D(DM dm, PetscInt n, Vec u[], PetscViewer viewer)
153: {
154:   PetscDS            ds;
155:   PetscDraw          draw = NULL;
156:   PetscDrawLG        lg;
157:   Vec                coordinates;
158:   const PetscScalar *coords, **sol;
159:   PetscReal         *vals;
160:   PetscInt          *Nc;
161:   PetscInt           Nf, f, c, Nl, l, i, vStart, vEnd, v;
162:   char             **names;

164:   DMGetDS(dm, &ds);
165:   PetscDSGetNumFields(ds, &Nf);
166:   PetscDSGetTotalComponents(ds, &Nl);
167:   PetscDSGetComponents(ds, &Nc);

169:   PetscViewerDrawGetDraw(viewer, 0, &draw);
170:   if (!draw) return 0;
171:   PetscDrawLGCreate(draw, n*Nl, &lg);

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

177:     PetscObjectGetName((PetscObject) u[i], &vname);
178:     for (f = 0; f < Nf; ++f) {
179:       PetscObject disc;
180:       const char *fname;
181:       char        tmpname[PETSC_MAX_PATH_LEN];

183:       PetscDSGetDiscretization(ds, f, &disc);
184:       /* TODO Create names for components */
185:       for (c = 0; c < Nc[f]; ++c, ++l) {
186:         PetscObjectGetName(disc, &fname);
187:         PetscStrcpy(tmpname, vname);
188:         PetscStrlcat(tmpname, ":", PETSC_MAX_PATH_LEN);
189:         PetscStrlcat(tmpname, fname, PETSC_MAX_PATH_LEN);
190:         PetscStrallocpy(tmpname, &names[l]);
191:       }
192:     }
193:   }
194:   PetscDrawLGSetLegend(lg, (const char *const *) names);
195:   /* Just add P_1 support for now */
196:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
197:   DMGetCoordinatesLocal(dm, &coordinates);
198:   VecGetArrayRead(coordinates, &coords);
199:   for (i = 0; i < n; ++i) VecGetArrayRead(u[i], &sol[i]);
200:   for (v = vStart; v < vEnd; ++v) {
201:     PetscScalar *x, *svals;

203:     DMPlexPointLocalRead(dm, v, coords, &x);
204:     for (i = 0; i < n; ++i) {
205:       DMPlexPointLocalRead(dm, v, sol[i], &svals);
206:       for (l = 0; l < Nl; ++l) vals[i*Nl + l] = PetscRealPart(svals[l]);
207:     }
208:     PetscDrawLGAddCommonPoint(lg, PetscRealPart(x[0]), vals);
209:   }
210:   VecRestoreArrayRead(coordinates, &coords);
211:   for (i = 0; i < n; ++i) VecRestoreArrayRead(u[i], &sol[i]);
212:   for (l = 0; l < n*Nl; ++l) PetscFree(names[l]);
213:   PetscFree3(sol, names, vals);

215:   PetscDrawLGDraw(lg);
216:   PetscDrawLGDestroy(&lg);
217:   return 0;
218: }

220: static PetscErrorCode VecView_Plex_Local_Draw_1D(Vec u, PetscViewer viewer)
221: {
222:   DM             dm;

224:   VecGetDM(u, &dm);
225:   DMPlexVecView1D(dm, 1, &u, viewer);
226:   return 0;
227: }

229: static PetscErrorCode VecView_Plex_Local_Draw_2D(Vec v, PetscViewer viewer)
230: {
231:   DM                 dm;
232:   PetscSection       s;
233:   PetscDraw          draw, popup;
234:   DM                 cdm;
235:   PetscSection       coordSection;
236:   Vec                coordinates;
237:   const PetscScalar *coords, *array;
238:   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
239:   PetscReal          vbound[2], time;
240:   PetscBool          flg;
241:   PetscInt           dim, Nf, f, Nc, comp, vStart, vEnd, cStart, cEnd, c, N, level, step, w = 0;
242:   const char        *name;
243:   char               title[PETSC_MAX_PATH_LEN];

245:   PetscViewerDrawGetDraw(viewer, 0, &draw);
246:   VecGetDM(v, &dm);
247:   DMGetCoordinateDim(dm, &dim);
248:   DMGetLocalSection(dm, &s);
249:   PetscSectionGetNumFields(s, &Nf);
250:   DMGetCoarsenLevel(dm, &level);
251:   DMGetCoordinateDM(dm, &cdm);
252:   DMGetLocalSection(cdm, &coordSection);
253:   DMGetCoordinatesLocal(dm, &coordinates);
254:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
255:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);

257:   PetscObjectGetName((PetscObject) v, &name);
258:   DMGetOutputSequenceNumber(dm, &step, &time);

260:   VecGetLocalSize(coordinates, &N);
261:   VecGetArrayRead(coordinates, &coords);
262:   for (c = 0; c < N; c += dim) {
263:     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
264:     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
265:   }
266:   VecRestoreArrayRead(coordinates, &coords);
267:   PetscDrawClear(draw);

269:   /* Could implement something like DMDASelectFields() */
270:   for (f = 0; f < Nf; ++f) {
271:     DM   fdm = dm;
272:     Vec  fv  = v;
273:     IS   fis;
274:     char prefix[PETSC_MAX_PATH_LEN];
275:     const char *fname;

277:     PetscSectionGetFieldComponents(s, f, &Nc);
278:     PetscSectionGetFieldName(s, f, &fname);

280:     if (v->hdr.prefix) PetscStrncpy(prefix, v->hdr.prefix,sizeof(prefix));
281:     else               {prefix[0] = '\0';}
282:     if (Nf > 1) {
283:       DMCreateSubDM(dm, 1, &f, &fis, &fdm);
284:       VecGetSubVector(v, fis, &fv);
285:       PetscStrlcat(prefix, fname,sizeof(prefix));
286:       PetscStrlcat(prefix, "_",sizeof(prefix));
287:     }
288:     for (comp = 0; comp < Nc; ++comp, ++w) {
289:       PetscInt nmax = 2;

291:       PetscViewerDrawGetDraw(viewer, w, &draw);
292:       if (Nc > 1) PetscSNPrintf(title, sizeof(title), "%s:%s_%D Step: %D Time: %.4g", name, fname, comp, step, time);
293:       else        PetscSNPrintf(title, sizeof(title), "%s:%s Step: %D Time: %.4g", name, fname, step, time);
294:       PetscDrawSetTitle(draw, title);

296:       /* TODO Get max and min only for this component */
297:       PetscOptionsGetRealArray(NULL, prefix, "-vec_view_bounds", vbound, &nmax, &flg);
298:       if (!flg) {
299:         VecMin(fv, NULL, &vbound[0]);
300:         VecMax(fv, NULL, &vbound[1]);
301:         if (vbound[1] <= vbound[0]) vbound[1] = vbound[0] + 1.0;
302:       }
303:       PetscDrawGetPopup(draw, &popup);
304:       PetscDrawScalePopup(popup, vbound[0], vbound[1]);
305:       PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);

307:       VecGetArrayRead(fv, &array);
308:       for (c = cStart; c < cEnd; ++c) {
309:         PetscScalar *coords = NULL, *a = NULL;
310:         PetscInt     numCoords, color[4] = {-1,-1,-1,-1};

312:         DMPlexPointLocalRead(fdm, c, array, &a);
313:         if (a) {
314:           color[0] = PetscDrawRealToColor(PetscRealPart(a[comp]), vbound[0], vbound[1]);
315:           color[1] = color[2] = color[3] = color[0];
316:         } else {
317:           PetscScalar *vals = NULL;
318:           PetscInt     numVals, va;

320:           DMPlexVecGetClosure(fdm, NULL, fv, c, &numVals, &vals);
322:           switch (numVals/Nc) {
323:           case 3: /* P1 Triangle */
324:           case 4: /* P1 Quadrangle */
325:             for (va = 0; va < numVals/Nc; ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va*Nc+comp]), vbound[0], vbound[1]);
326:             break;
327:           case 6: /* P2 Triangle */
328:           case 8: /* P2 Quadrangle */
329:             for (va = 0; va < numVals/(Nc*2); ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va*Nc+comp + numVals/(Nc*2)]), vbound[0], vbound[1]);
330:             break;
331:           default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of values for cell closure %D cannot be handled", numVals/Nc);
332:           }
333:           DMPlexVecRestoreClosure(fdm, NULL, fv, c, &numVals, &vals);
334:         }
335:         DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);
336:         switch (numCoords) {
337:         case 6:
338:         case 12: /* Localized triangle */
339:           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]);
340:           break;
341:         case 8:
342:         case 16: /* Localized quadrilateral */
343:           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]);
344:           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]);
345:           break;
346:         default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D coordinates", numCoords);
347:         }
348:         DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);
349:       }
350:       VecRestoreArrayRead(fv, &array);
351:       PetscDrawFlush(draw);
352:       PetscDrawPause(draw);
353:       PetscDrawSave(draw);
354:     }
355:     if (Nf > 1) {
356:       VecRestoreSubVector(v, fis, &fv);
357:       ISDestroy(&fis);
358:       DMDestroy(&fdm);
359:     }
360:   }
361:   return 0;
362: }

364: static PetscErrorCode VecView_Plex_Local_Draw(Vec v, PetscViewer viewer)
365: {
366:   DM        dm;
367:   PetscDraw draw;
368:   PetscInt  dim;
369:   PetscBool isnull;

371:   PetscViewerDrawGetDraw(viewer, 0, &draw);
372:   PetscDrawIsNull(draw, &isnull);
373:   if (isnull) return 0;

375:   VecGetDM(v, &dm);
376:   DMGetCoordinateDim(dm, &dim);
377:   switch (dim) {
378:   case 1: VecView_Plex_Local_Draw_1D(v, viewer);break;
379:   case 2: VecView_Plex_Local_Draw_2D(v, viewer);break;
380:   default: SETERRQ(PetscObjectComm((PetscObject) v), PETSC_ERR_SUP, "Cannot draw meshes of dimension %" PetscInt_FMT ". Try PETSCVIEWERGLVIS", dim);
381:   }
382:   return 0;
383: }

385: static PetscErrorCode VecView_Plex_Local_VTK(Vec v, PetscViewer viewer)
386: {
387:   DM                      dm;
388:   Vec                     locv;
389:   const char              *name;
390:   PetscSection            section;
391:   PetscInt                pStart, pEnd;
392:   PetscInt                numFields;
393:   PetscViewerVTKFieldType ft;

395:   VecGetDM(v, &dm);
396:   DMCreateLocalVector(dm, &locv); /* VTK viewer requires exclusive ownership of the vector */
397:   PetscObjectGetName((PetscObject) v, &name);
398:   PetscObjectSetName((PetscObject) locv, name);
399:   VecCopy(v, locv);
400:   DMGetLocalSection(dm, &section);
401:   PetscSectionGetNumFields(section, &numFields);
402:   if (!numFields) {
403:     DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft);
404:     PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, PETSC_DEFAULT, ft, PETSC_TRUE,(PetscObject) locv);
405:   } else {
406:     PetscInt f;

408:     for (f = 0; f < numFields; f++) {
409:       DMPlexGetFieldType_Internal(dm, section, f, &pStart, &pEnd, &ft);
410:       if (ft == PETSC_VTK_INVALID) continue;
411:       PetscObjectReference((PetscObject)locv);
412:       PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, f, ft, PETSC_TRUE,(PetscObject) locv);
413:     }
414:     VecDestroy(&locv);
415:   }
416:   return 0;
417: }

419: PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
420: {
421:   DM             dm;
422:   PetscBool      isvtk, ishdf5, isdraw, isglvis;

424:   VecGetDM(v, &dm);
426:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);
427:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);
428:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);
429:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);
430:   if (isvtk || ishdf5 || isdraw || isglvis) {
431:     PetscInt    i,numFields;
432:     PetscObject fe;
433:     PetscBool   fem = PETSC_FALSE;
434:     Vec         locv = v;
435:     const char  *name;
436:     PetscInt    step;
437:     PetscReal   time;

439:     DMGetNumFields(dm, &numFields);
440:     for (i=0; i<numFields; i++) {
441:       DMGetField(dm, i, NULL, &fe);
442:       if (fe->classid == PETSCFE_CLASSID) { fem = PETSC_TRUE; break; }
443:     }
444:     if (fem) {
445:       PetscObject isZero;

447:       DMGetLocalVector(dm, &locv);
448:       PetscObjectGetName((PetscObject) v, &name);
449:       PetscObjectSetName((PetscObject) locv, name);
450:       PetscObjectQuery((PetscObject) v, "__Vec_bc_zero__", &isZero);
451:       PetscObjectCompose((PetscObject) locv, "__Vec_bc_zero__", isZero);
452:       VecCopy(v, locv);
453:       DMGetOutputSequenceNumber(dm, NULL, &time);
454:       DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);
455:     }
456:     if (isvtk) {
457:       VecView_Plex_Local_VTK(locv, viewer);
458:     } else if (ishdf5) {
459: #if defined(PETSC_HAVE_HDF5)
460:       VecView_Plex_Local_HDF5_Internal(locv, viewer);
461: #else
462:       SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
463: #endif
464:     } else if (isdraw) {
465:       VecView_Plex_Local_Draw(locv, viewer);
466:     } else if (isglvis) {
467:       DMGetOutputSequenceNumber(dm, &step, NULL);
468:       PetscViewerGLVisSetSnapId(viewer, step);
469:       VecView_GLVis(locv, viewer);
470:     }
471:     if (fem) {
472:       PetscObjectCompose((PetscObject) locv, "__Vec_bc_zero__", NULL);
473:       DMRestoreLocalVector(dm, &locv);
474:     }
475:   } else {
476:     PetscBool isseq;

478:     PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);
479:     if (isseq) VecView_Seq(v, viewer);
480:     else       VecView_MPI(v, viewer);
481:   }
482:   return 0;
483: }

485: PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
486: {
487:   DM        dm;
488:   PetscBool isvtk, ishdf5, isdraw, isglvis, isexodusii;

490:   VecGetDM(v, &dm);
492:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,      &isvtk);
493:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,     &ishdf5);
494:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,     &isdraw);
495:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS,    &isglvis);
496:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWEREXODUSII, &isexodusii);
497:   if (isvtk || isdraw || isglvis) {
498:     Vec         locv;
499:     PetscObject isZero;
500:     const char *name;

502:     DMGetLocalVector(dm, &locv);
503:     PetscObjectGetName((PetscObject) v, &name);
504:     PetscObjectSetName((PetscObject) locv, name);
505:     DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);
506:     DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);
507:     PetscObjectQuery((PetscObject) v, "__Vec_bc_zero__", &isZero);
508:     PetscObjectCompose((PetscObject) locv, "__Vec_bc_zero__", isZero);
509:     VecView_Plex_Local(locv, viewer);
510:     PetscObjectCompose((PetscObject) locv, "__Vec_bc_zero__", NULL);
511:     DMRestoreLocalVector(dm, &locv);
512:   } else if (ishdf5) {
513: #if defined(PETSC_HAVE_HDF5)
514:     VecView_Plex_HDF5_Internal(v, viewer);
515: #else
516:     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
517: #endif
518:   } else if (isexodusii) {
519: #if defined(PETSC_HAVE_EXODUSII)
520:     VecView_PlexExodusII_Internal(v, viewer);
521: #else
522:     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "ExodusII not supported in this build.\nPlease reconfigure using --download-exodusii");
523: #endif
524:   } else {
525:     PetscBool isseq;

527:     PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);
528:     if (isseq) VecView_Seq(v, viewer);
529:     else       VecView_MPI(v, viewer);
530:   }
531:   return 0;
532: }

534: PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
535: {
536:   DM                dm;
537:   MPI_Comm          comm;
538:   PetscViewerFormat format;
539:   Vec               v;
540:   PetscBool         isvtk, ishdf5;

542:   VecGetDM(originalv, &dm);
543:   PetscObjectGetComm((PetscObject) originalv, &comm);
545:   PetscViewerGetFormat(viewer, &format);
546:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);
547:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);
548:   if (format == PETSC_VIEWER_NATIVE) {
549:     /* Natural ordering is the common case for DMDA, NATIVE means plain vector, for PLEX is the opposite */
550:     /* this need a better fix */
551:     if (dm->useNatural) {
552:       if (dm->sfNatural) {
553:         const char *vecname;
554:         PetscInt    n, nroots;

556:         VecGetLocalSize(originalv, &n);
557:         PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);
558:         if (n == nroots) {
559:           DMGetGlobalVector(dm, &v);
560:           DMPlexGlobalToNaturalBegin(dm, originalv, v);
561:           DMPlexGlobalToNaturalEnd(dm, originalv, v);
562:           PetscObjectGetName((PetscObject) originalv, &vecname);
563:           PetscObjectSetName((PetscObject) v, vecname);
564:         } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
565:       } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
566:     } else v = originalv;
567:   } else v = originalv;

569:   if (ishdf5) {
570: #if defined(PETSC_HAVE_HDF5)
571:     VecView_Plex_HDF5_Native_Internal(v, viewer);
572: #else
573:     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
574: #endif
575:   } else if (isvtk) {
576:     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
577:   } else {
578:     PetscBool isseq;

580:     PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);
581:     if (isseq) VecView_Seq(v, viewer);
582:     else       VecView_MPI(v, viewer);
583:   }
584:   if (v != originalv) DMRestoreGlobalVector(dm, &v);
585:   return 0;
586: }

588: PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
589: {
590:   DM             dm;
591:   PetscBool      ishdf5;

593:   VecGetDM(v, &dm);
595:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);
596:   if (ishdf5) {
597:     DM          dmBC;
598:     Vec         gv;
599:     const char *name;

601:     DMGetOutputDM(dm, &dmBC);
602:     DMGetGlobalVector(dmBC, &gv);
603:     PetscObjectGetName((PetscObject) v, &name);
604:     PetscObjectSetName((PetscObject) gv, name);
605:     VecLoad_Default(gv, viewer);
606:     DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);
607:     DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);
608:     DMRestoreGlobalVector(dmBC, &gv);
609:   } else {
610:     VecLoad_Default(v, viewer);
611:   }
612:   return 0;
613: }

615: PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
616: {
617:   DM             dm;
618:   PetscBool      ishdf5,isexodusii;

620:   VecGetDM(v, &dm);
622:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,     &ishdf5);
623:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWEREXODUSII, &isexodusii);
624:   if (ishdf5) {
625: #if defined(PETSC_HAVE_HDF5)
626:     VecLoad_Plex_HDF5_Internal(v, viewer);
627: #else
628:     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
629: #endif
630:   } else if (isexodusii) {
631: #if defined(PETSC_HAVE_EXODUSII)
632:     VecLoad_PlexExodusII_Internal(v, viewer);
633: #else
634:     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "ExodusII not supported in this build.\nPlease reconfigure using --download-exodusii");
635: #endif
636:   } else {
637:     VecLoad_Default(v, viewer);
638:   }
639:   return 0;
640: }

642: PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
643: {
644:   DM                dm;
645:   PetscViewerFormat format;
646:   PetscBool         ishdf5;

648:   VecGetDM(originalv, &dm);
650:   PetscViewerGetFormat(viewer, &format);
651:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);
652:   if (format == PETSC_VIEWER_NATIVE) {
653:     if (dm->useNatural) {
654:       if (dm->sfNatural) {
655:         if (ishdf5) {
656: #if defined(PETSC_HAVE_HDF5)
657:           Vec         v;
658:           const char *vecname;

660:           DMGetGlobalVector(dm, &v);
661:           PetscObjectGetName((PetscObject) originalv, &vecname);
662:           PetscObjectSetName((PetscObject) v, vecname);
663:           VecLoad_Plex_HDF5_Native_Internal(v, viewer);
664:           DMPlexNaturalToGlobalBegin(dm, v, originalv);
665:           DMPlexNaturalToGlobalEnd(dm, v, originalv);
666:           DMRestoreGlobalVector(dm, &v);
667: #else
668:           SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
669: #endif
670:         } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
671:       }
672:     } else {
673:       VecLoad_Default(originalv, viewer);
674:     }
675:   }
676:   return 0;
677: }

679: PETSC_UNUSED static PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
680: {
681:   PetscSection       coordSection;
682:   Vec                coordinates;
683:   DMLabel            depthLabel, celltypeLabel;
684:   const char        *name[4];
685:   const PetscScalar *a;
686:   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;

688:   DMGetDimension(dm, &dim);
689:   DMGetCoordinatesLocal(dm, &coordinates);
690:   DMGetCoordinateSection(dm, &coordSection);
691:   DMPlexGetDepthLabel(dm, &depthLabel);
692:   DMPlexGetCellTypeLabel(dm, &celltypeLabel);
693:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
694:   PetscSectionGetChart(coordSection, &pStart, &pEnd);
695:   VecGetArrayRead(coordinates, &a);
696:   name[0]     = "vertex";
697:   name[1]     = "edge";
698:   name[dim-1] = "face";
699:   name[dim]   = "cell";
700:   for (c = cStart; c < cEnd; ++c) {
701:     PetscInt *closure = NULL;
702:     PetscInt  closureSize, cl, ct;

704:     DMLabelGetValue(celltypeLabel, c, &ct);
705:     PetscViewerASCIIPrintf(viewer, "Geometry for cell %D polytope type %s:\n", c, DMPolytopeTypes[ct]);
706:     DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
707:     PetscViewerASCIIPushTab(viewer);
708:     for (cl = 0; cl < closureSize*2; cl += 2) {
709:       PetscInt point = closure[cl], depth, dof, off, d, p;

711:       if ((point < pStart) || (point >= pEnd)) continue;
712:       PetscSectionGetDof(coordSection, point, &dof);
713:       if (!dof) continue;
714:       DMLabelGetValue(depthLabel, point, &depth);
715:       PetscSectionGetOffset(coordSection, point, &off);
716:       PetscViewerASCIIPrintf(viewer, "%s %D coords:", name[depth], point);
717:       for (p = 0; p < dof/dim; ++p) {
718:         PetscViewerASCIIPrintf(viewer, " (");
719:         for (d = 0; d < dim; ++d) {
720:           if (d > 0) PetscViewerASCIIPrintf(viewer, ", ");
721:           PetscViewerASCIIPrintf(viewer, "%g", (double) PetscRealPart(a[off+p*dim+d]));
722:         }
723:         PetscViewerASCIIPrintf(viewer, ")");
724:       }
725:       PetscViewerASCIIPrintf(viewer, "\n");
726:     }
727:     DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
728:     PetscViewerASCIIPopTab(viewer);
729:   }
730:   VecRestoreArrayRead(coordinates, &a);
731:   return 0;
732: }

734: typedef enum {CS_CARTESIAN, CS_POLAR, CS_CYLINDRICAL, CS_SPHERICAL} CoordSystem;
735: const char *CoordSystems[] = {"cartesian", "polar", "cylindrical", "spherical", "CoordSystem", "CS_", NULL};

737: static PetscErrorCode DMPlexView_Ascii_Coordinates(PetscViewer viewer, CoordSystem cs, PetscInt dim, const PetscScalar x[])
738: {
739:   PetscInt       i;

741:   if (dim > 3) {
742:     for (i = 0; i < dim; ++i) PetscViewerASCIISynchronizedPrintf(viewer, " %g", (double) PetscRealPart(x[i]));
743:   } else {
744:     PetscReal coords[3], trcoords[3];

746:     for (i = 0; i < dim; ++i) coords[i] = PetscRealPart(x[i]);
747:     switch (cs) {
748:       case CS_CARTESIAN: for (i = 0; i < dim; ++i) trcoords[i] = coords[i];break;
749:       case CS_POLAR:
751:         trcoords[0] = PetscSqrtReal(PetscSqr(coords[0]) + PetscSqr(coords[1]));
752:         trcoords[1] = PetscAtan2Real(coords[1], coords[0]);
753:         break;
754:       case CS_CYLINDRICAL:
756:         trcoords[0] = PetscSqrtReal(PetscSqr(coords[0]) + PetscSqr(coords[1]));
757:         trcoords[1] = PetscAtan2Real(coords[1], coords[0]);
758:         trcoords[2] = coords[2];
759:         break;
760:       case CS_SPHERICAL:
762:         trcoords[0] = PetscSqrtReal(PetscSqr(coords[0]) + PetscSqr(coords[1]) + PetscSqr(coords[2]));
763:         trcoords[1] = PetscAtan2Real(PetscSqrtReal(PetscSqr(coords[0]) + PetscSqr(coords[1])), coords[2]);
764:         trcoords[2] = PetscAtan2Real(coords[1], coords[0]);
765:         break;
766:     }
767:     for (i = 0; i < dim; ++i) PetscViewerASCIISynchronizedPrintf(viewer, " %g", (double) trcoords[i]);
768:   }
769:   return 0;
770: }

772: static PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
773: {
774:   DM_Plex          *mesh = (DM_Plex*) dm->data;
775:   DM                cdm;
776:   PetscSection      coordSection;
777:   Vec               coordinates;
778:   PetscViewerFormat format;

780:   DMGetCoordinateDM(dm, &cdm);
781:   DMGetLocalSection(cdm, &coordSection);
782:   DMGetCoordinatesLocal(dm, &coordinates);
783:   PetscViewerGetFormat(viewer, &format);
784:   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
785:     const char *name;
786:     PetscInt    dim, cellHeight, maxConeSize, maxSupportSize;
787:     PetscInt    pStart, pEnd, p, numLabels, l;
788:     PetscMPIInt rank, size;

790:     MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
791:     MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);
792:     PetscObjectGetName((PetscObject) dm, &name);
793:     DMPlexGetChart(dm, &pStart, &pEnd);
794:     DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);
795:     DMGetDimension(dm, &dim);
796:     DMPlexGetVTKCellHeight(dm, &cellHeight);
797:     if (name) PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");
798:     else      PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");
799:     if (cellHeight) PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);
800:     PetscViewerASCIIPrintf(viewer, "Supports:\n", name);
801:     PetscViewerASCIIPushSynchronized(viewer);
802:     PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max support size: %D\n", rank, maxSupportSize);
803:     for (p = pStart; p < pEnd; ++p) {
804:       PetscInt dof, off, s;

806:       PetscSectionGetDof(mesh->supportSection, p, &dof);
807:       PetscSectionGetOffset(mesh->supportSection, p, &off);
808:       for (s = off; s < off+dof; ++s) {
809:         PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D ----> %D\n", rank, p, mesh->supports[s]);
810:       }
811:     }
812:     PetscViewerFlush(viewer);
813:     PetscViewerASCIIPrintf(viewer, "Cones:\n", name);
814:     PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max cone size: %D\n", rank, maxConeSize);
815:     for (p = pStart; p < pEnd; ++p) {
816:       PetscInt dof, off, c;

818:       PetscSectionGetDof(mesh->coneSection, p, &dof);
819:       PetscSectionGetOffset(mesh->coneSection, p, &off);
820:       for (c = off; c < off+dof; ++c) {
821:         PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D <---- %D (%D)\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);
822:       }
823:     }
824:     PetscViewerFlush(viewer);
825:     PetscViewerASCIIPopSynchronized(viewer);
826:     if (coordSection && coordinates) {
827:       CoordSystem        cs = CS_CARTESIAN;
828:       const PetscScalar *array;
829:       PetscInt           Nf, Nc, pStart, pEnd, p;
830:       PetscMPIInt        rank;
831:       const char        *name;

833:       PetscOptionsGetEnum(((PetscObject) viewer)->options, ((PetscObject) viewer)->prefix, "-dm_plex_view_coord_system", CoordSystems, (PetscEnum *) &cs, NULL);
834:       MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank);
835:       PetscSectionGetNumFields(coordSection, &Nf);
837:       PetscSectionGetFieldComponents(coordSection, 0, &Nc);
838:       PetscSectionGetChart(coordSection, &pStart, &pEnd);
839:       PetscObjectGetName((PetscObject) coordinates, &name);
840:       PetscViewerASCIIPrintf(viewer, "%s with %D fields\n", name, Nf);
841:       PetscViewerASCIIPrintf(viewer, "  field 0 with %D components\n", Nc);
842:       if (cs != CS_CARTESIAN) PetscViewerASCIIPrintf(viewer, "  output coordinate system: %s\n", CoordSystems[cs]);

844:       VecGetArrayRead(coordinates, &array);
845:       PetscViewerASCIIPushSynchronized(viewer);
846:       PetscViewerASCIISynchronizedPrintf(viewer, "Process %d:\n", rank);
847:       for (p = pStart; p < pEnd; ++p) {
848:         PetscInt dof, off;

850:         PetscSectionGetDof(coordSection, p, &dof);
851:         PetscSectionGetOffset(coordSection, p, &off);
852:         PetscViewerASCIISynchronizedPrintf(viewer, "  (%4D) dim %2D offset %3D", p, dof, off);
853:         DMPlexView_Ascii_Coordinates(viewer, cs, dof, &array[off]);
854:         PetscViewerASCIISynchronizedPrintf(viewer, "\n");
855:       }
856:       PetscViewerFlush(viewer);
857:       PetscViewerASCIIPopSynchronized(viewer);
858:       VecRestoreArrayRead(coordinates, &array);
859:     }
860:     DMGetNumLabels(dm, &numLabels);
861:     if (numLabels) PetscViewerASCIIPrintf(viewer, "Labels:\n");
862:     for (l = 0; l < numLabels; ++l) {
863:       DMLabel     label;
864:       PetscBool   isdepth;
865:       const char *name;

867:       DMGetLabelName(dm, l, &name);
868:       PetscStrcmp(name, "depth", &isdepth);
869:       if (isdepth) continue;
870:       DMGetLabel(dm, name, &label);
871:       DMLabelView(label, viewer);
872:     }
873:     if (size > 1) {
874:       PetscSF sf;

876:       DMGetPointSF(dm, &sf);
877:       PetscSFView(sf, viewer);
878:     }
879:     PetscViewerFlush(viewer);
880:   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
881:     const char  *name, *color;
882:     const char  *defcolors[3]  = {"gray", "orange", "green"};
883:     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
884:     char         lname[PETSC_MAX_PATH_LEN];
885:     PetscReal    scale         = 2.0;
886:     PetscReal    tikzscale     = 1.0;
887:     PetscBool    useNumbers    = PETSC_TRUE, drawNumbers[4], drawColors[4], useLabels, useColors, plotEdges, drawHasse = PETSC_FALSE;
888:     double       tcoords[3];
889:     PetscScalar *coords;
890:     PetscInt     numLabels, l, numColors, numLColors, dim, d, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p, n;
891:     PetscMPIInt  rank, size;
892:     char         **names, **colors, **lcolors;
893:     PetscBool    flg, lflg;
894:     PetscBT      wp = NULL;
895:     PetscInt     pEnd, pStart;

897:     DMGetDimension(dm, &dim);
898:     DMPlexGetDepth(dm, &depth);
899:     DMGetNumLabels(dm, &numLabels);
900:     numLabels  = PetscMax(numLabels, 10);
901:     numColors  = 10;
902:     numLColors = 10;
903:     PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors);
904:     PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_scale", &scale, NULL);
905:     PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_tikzscale", &tikzscale, NULL);
906:     PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);
907:     for (d = 0; d < 4; ++d) drawNumbers[d] = useNumbers;
908:     for (d = 0; d < 4; ++d) drawColors[d]  = PETSC_TRUE;
909:     n = 4;
910:     PetscOptionsGetBoolArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers_depth", drawNumbers, &n, &flg);
912:     PetscOptionsGetBoolArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors_depth", drawColors, &n, &flg);
914:     PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);
915:     if (!useLabels) numLabels = 0;
916:     PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);
917:     if (!useColors) {
918:       numColors = 3;
919:       for (c = 0; c < numColors; ++c) PetscStrallocpy(defcolors[c], &colors[c]);
920:     }
921:     PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);
922:     if (!useColors) {
923:       numLColors = 4;
924:       for (c = 0; c < numLColors; ++c) PetscStrallocpy(deflcolors[c], &lcolors[c]);
925:     }
926:     PetscOptionsGetString(((PetscObject) viewer)->options, ((PetscObject) viewer)->prefix, "-dm_plex_view_label_filter", lname, sizeof(lname), &lflg);
927:     plotEdges = (PetscBool)(depth > 1 && drawNumbers[1] && dim < 3);
928:     PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_edges", &plotEdges, &flg);
930:     if (depth < dim) plotEdges = PETSC_FALSE;
931:     PetscOptionsGetBool(((PetscObject) viewer)->options, ((PetscObject) viewer)->prefix, "-dm_plex_view_hasse", &drawHasse, NULL);

933:     /* filter points with labelvalue != labeldefaultvalue */
934:     DMPlexGetChart(dm, &pStart, &pEnd);
935:     DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
936:     DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);
937:     DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
938:     if (lflg) {
939:       DMLabel lbl;

941:       DMGetLabel(dm, lname, &lbl);
942:       if (lbl) {
943:         PetscInt val, defval;

945:         DMLabelGetDefaultValue(lbl, &defval);
946:         PetscBTCreate(pEnd-pStart, &wp);
947:         for (c = pStart;  c < pEnd; c++) {
948:           PetscInt *closure = NULL;
949:           PetscInt  closureSize;

951:           DMLabelGetValue(lbl, c, &val);
952:           if (val == defval) continue;

954:           DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
955:           for (p = 0; p < closureSize*2; p += 2) {
956:             PetscBTSet(wp, closure[p] - pStart);
957:           }
958:           DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
959:         }
960:       }
961:     }

963:     MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
964:     MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);
965:     PetscObjectGetName((PetscObject) dm, &name);
966:     PetscCall(PetscViewerASCIIPrintf(viewer, "\
967: \\documentclass[tikz]{standalone}\n\n\
968: \\usepackage{pgflibraryshapes}\n\
969: \\usetikzlibrary{backgrounds}\n\
970: \\usetikzlibrary{arrows}\n\
971: \\begin{document}\n"));
972:     if (size > 1) {
973:       PetscViewerASCIIPrintf(viewer, "%s for process ", name);
974:       for (p = 0; p < size; ++p) {
975:         if (p > 0 && p == size-1) {
976:           PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);
977:         } else if (p > 0) {
978:           PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);
979:         }
980:         PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);
981:       }
982:       PetscViewerASCIIPrintf(viewer, ".\n\n\n");
983:     }
984:     if (drawHasse) {
985:       PetscInt maxStratum = PetscMax(vEnd-vStart, PetscMax(eEnd-eStart, cEnd-cStart));

987:       PetscViewerASCIIPrintf(viewer, "\\newcommand{\\vStart}{%D}\n", vStart);
988:       PetscViewerASCIIPrintf(viewer, "\\newcommand{\\vEnd}{%D}\n", vEnd-1);
989:       PetscViewerASCIIPrintf(viewer, "\\newcommand{\\numVertices}{%D}\n", vEnd-vStart);
990:       PetscViewerASCIIPrintf(viewer, "\\newcommand{\\vShift}{%.2f}\n", 3 + (maxStratum-(vEnd-vStart))/2.);
991:       PetscViewerASCIIPrintf(viewer, "\\newcommand{\\eStart}{%D}\n", eStart);
992:       PetscViewerASCIIPrintf(viewer, "\\newcommand{\\eEnd}{%D}\n", eEnd-1);
993:       PetscViewerASCIIPrintf(viewer, "\\newcommand{\\eShift}{%.2f}\n", 3 + (maxStratum-(eEnd-eStart))/2.);
994:       PetscViewerASCIIPrintf(viewer, "\\newcommand{\\numEdges}{%D}\n", eEnd-eStart);
995:       PetscViewerASCIIPrintf(viewer, "\\newcommand{\\cStart}{%D}\n", cStart);
996:       PetscViewerASCIIPrintf(viewer, "\\newcommand{\\cEnd}{%D}\n", cEnd-1);
997:       PetscViewerASCIIPrintf(viewer, "\\newcommand{\\numCells}{%D}\n", cEnd-cStart);
998:       PetscViewerASCIIPrintf(viewer, "\\newcommand{\\cShift}{%.2f}\n", 3 + (maxStratum-(cEnd-cStart))/2.);
999:     }
1000:     PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", (double) tikzscale);

1002:     /* Plot vertices */
1003:     VecGetArray(coordinates, &coords);
1004:     PetscViewerASCIIPushSynchronized(viewer);
1005:     for (v = vStart; v < vEnd; ++v) {
1006:       PetscInt  off, dof, d;
1007:       PetscBool isLabeled = PETSC_FALSE;

1009:       if (wp && !PetscBTLookup(wp,v - pStart)) continue;
1010:       PetscSectionGetDof(coordSection, v, &dof);
1011:       PetscSectionGetOffset(coordSection, v, &off);
1012:       PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");
1014:       for (d = 0; d < dof; ++d) {
1015:         tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
1016:         tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
1017:       }
1018:       /* Rotate coordinates since PGF makes z point out of the page instead of up */
1019:       if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
1020:       for (d = 0; d < dof; ++d) {
1021:         if (d > 0) PetscViewerASCIISynchronizedPrintf(viewer, ",");
1022:         PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double) tcoords[d]);
1023:       }
1024:       if (drawHasse) color = colors[0%numColors];
1025:       else           color = colors[rank%numColors];
1026:       for (l = 0; l < numLabels; ++l) {
1027:         PetscInt val;
1028:         DMGetLabelValue(dm, names[l], v, &val);
1029:         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
1030:       }
1031:       if (drawNumbers[0]) {
1032:         PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", v, rank, color, v);
1033:       } else if (drawColors[0]) {
1034:         PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);
1035:       } else {
1036:         PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [] {};\n", v, rank);
1037:       }
1038:     }
1039:     VecRestoreArray(coordinates, &coords);
1040:     PetscViewerFlush(viewer);
1041:     /* Plot edges */
1042:     if (plotEdges) {
1043:       VecGetArray(coordinates, &coords);
1044:       PetscViewerASCIIPrintf(viewer, "\\path\n");
1045:       for (e = eStart; e < eEnd; ++e) {
1046:         const PetscInt *cone;
1047:         PetscInt        coneSize, offA, offB, dof, d;

1049:         if (wp && !PetscBTLookup(wp,e - pStart)) continue;
1050:         DMPlexGetConeSize(dm, e, &coneSize);
1052:         DMPlexGetCone(dm, e, &cone);
1053:         PetscSectionGetDof(coordSection, cone[0], &dof);
1054:         PetscSectionGetOffset(coordSection, cone[0], &offA);
1055:         PetscSectionGetOffset(coordSection, cone[1], &offB);
1056:         PetscViewerASCIISynchronizedPrintf(viewer, "(");
1057:         for (d = 0; d < dof; ++d) {
1058:           tcoords[d] = (double) (0.5*scale*PetscRealPart(coords[offA+d]+coords[offB+d]));
1059:           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
1060:         }
1061:         /* Rotate coordinates since PGF makes z point out of the page instead of up */
1062:         if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
1063:         for (d = 0; d < dof; ++d) {
1064:           if (d > 0) PetscViewerASCIISynchronizedPrintf(viewer, ",");
1065:           PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);
1066:         }
1067:         if (drawHasse) color = colors[1%numColors];
1068:         else           color = colors[rank%numColors];
1069:         for (l = 0; l < numLabels; ++l) {
1070:           PetscInt val;
1071:           DMGetLabelValue(dm, names[l], v, &val);
1072:           if (val >= 0) {color = lcolors[l%numLColors]; break;}
1073:         }
1074:         PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D} --\n", e, rank, color, e);
1075:       }
1076:       VecRestoreArray(coordinates, &coords);
1077:       PetscViewerFlush(viewer);
1078:       PetscViewerASCIIPrintf(viewer, "(0,0);\n");
1079:     }
1080:     /* Plot cells */
1081:     if (dim == 3 || !drawNumbers[1]) {
1082:       for (e = eStart; e < eEnd; ++e) {
1083:         const PetscInt *cone;

1085:         if (wp && !PetscBTLookup(wp,e - pStart)) continue;
1086:         color = colors[rank%numColors];
1087:         for (l = 0; l < numLabels; ++l) {
1088:           PetscInt val;
1089:           DMGetLabelValue(dm, names[l], e, &val);
1090:           if (val >= 0) {color = lcolors[l%numLColors]; break;}
1091:         }
1092:         DMPlexGetCone(dm, e, &cone);
1093:         PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d);\n", color, cone[0], rank, cone[1], rank);
1094:       }
1095:     } else {
1096:        DMPolytopeType ct;

1098:       /* Drawing a 2D polygon */
1099:       for (c = cStart; c < cEnd; ++c) {
1100:         if (wp && !PetscBTLookup(wp, c - pStart)) continue;
1101:         DMPlexGetCellType(dm, c, &ct);
1102:         if (ct == DM_POLYTOPE_SEG_PRISM_TENSOR ||
1103:             ct == DM_POLYTOPE_TRI_PRISM_TENSOR ||
1104:             ct == DM_POLYTOPE_QUAD_PRISM_TENSOR) {
1105:           const PetscInt *cone;
1106:           PetscInt        coneSize, e;

1108:           DMPlexGetCone(dm, c, &cone);
1109:           DMPlexGetConeSize(dm, c, &coneSize);
1110:           for (e = 0; e < coneSize; ++e) {
1111:             const PetscInt *econe;

1113:             DMPlexGetCone(dm, cone[e], &econe);
1114:             PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d) -- (%D_%d);\n", colors[rank%numColors], econe[0], rank, cone[e], rank, econe[1], rank);
1115:           }
1116:         } else {
1117:           PetscInt *closure = NULL;
1118:           PetscInt  closureSize, Nv = 0, v;

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

1124:             if ((point >= vStart) && (point < vEnd)) closure[Nv++] = point;
1125:           }
1126:           PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);
1127:           for (v = 0; v <= Nv; ++v) {
1128:             const PetscInt vertex = closure[v%Nv];

1130:             if (v > 0) {
1131:               if (plotEdges) {
1132:                 const PetscInt *edge;
1133:                 PetscInt        endpoints[2], ne;

1135:                 endpoints[0] = closure[v-1]; endpoints[1] = vertex;
1136:                 DMPlexGetJoin(dm, 2, endpoints, &ne, &edge);
1138:                 PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%d) -- ", edge[0], rank);
1139:                 DMPlexRestoreJoin(dm, 2, endpoints, &ne, &edge);
1140:               } else {
1141:                 PetscViewerASCIISynchronizedPrintf(viewer, " -- ");
1142:               }
1143:             }
1144:             PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%d)", vertex, rank);
1145:           }
1146:           PetscViewerASCIISynchronizedPrintf(viewer, ";\n");
1147:           DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
1148:         }
1149:       }
1150:     }
1151:     VecGetArray(coordinates, &coords);
1152:     for (c = cStart; c < cEnd; ++c) {
1153:       double    ccoords[3] = {0.0, 0.0, 0.0};
1154:       PetscBool isLabeled  = PETSC_FALSE;
1155:       PetscInt *closure    = NULL;
1156:       PetscInt  closureSize, dof, d, n = 0;

1158:       if (wp && !PetscBTLookup(wp,c - pStart)) continue;
1159:       DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
1160:       PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");
1161:       for (p = 0; p < closureSize*2; p += 2) {
1162:         const PetscInt point = closure[p];
1163:         PetscInt       off;

1165:         if ((point < vStart) || (point >= vEnd)) continue;
1166:         PetscSectionGetDof(coordSection, point, &dof);
1167:         PetscSectionGetOffset(coordSection, point, &off);
1168:         for (d = 0; d < dof; ++d) {
1169:           tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
1170:           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
1171:         }
1172:         /* Rotate coordinates since PGF makes z point out of the page instead of up */
1173:         if (dof == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
1174:         for (d = 0; d < dof; ++d) {ccoords[d] += tcoords[d];}
1175:         ++n;
1176:       }
1177:       for (d = 0; d < dof; ++d) {ccoords[d] /= n;}
1178:       DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
1179:       for (d = 0; d < dof; ++d) {
1180:         if (d > 0) PetscViewerASCIISynchronizedPrintf(viewer, ",");
1181:         PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double) ccoords[d]);
1182:       }
1183:       if (drawHasse) color = colors[depth%numColors];
1184:       else           color = colors[rank%numColors];
1185:       for (l = 0; l < numLabels; ++l) {
1186:         PetscInt val;
1187:         DMGetLabelValue(dm, names[l], c, &val);
1188:         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
1189:       }
1190:       if (drawNumbers[dim]) {
1191:         PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", c, rank, color, c);
1192:       } else if (drawColors[dim]) {
1193:         PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", c, rank, !isLabeled ? 1 : 2, color);
1194:       } else {
1195:         PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [] {};\n", c, rank);
1196:       }
1197:     }
1198:     VecRestoreArray(coordinates, &coords);
1199:     if (drawHasse) {
1200:       color = colors[depth%numColors];
1201:       PetscViewerASCIIPrintf(viewer, "%% Cells\n");
1202:       PetscViewerASCIIPrintf(viewer, "\\foreach \\c in {\\cStart,...,\\cEnd}\n");
1203:       PetscViewerASCIIPrintf(viewer, "{\n");
1204:       PetscViewerASCIIPrintf(viewer, "  \\node(\\c_%d) [draw,shape=circle,color=%s,minimum size = 6mm] at (\\cShift+\\c-\\cStart,0) {\\c};\n", rank, color);
1205:       PetscViewerASCIIPrintf(viewer, "}\n");

1207:       color = colors[1%numColors];
1208:       PetscViewerASCIIPrintf(viewer, "%% Edges\n");
1209:       PetscViewerASCIIPrintf(viewer, "\\foreach \\e in {\\eStart,...,\\eEnd}\n");
1210:       PetscViewerASCIIPrintf(viewer, "{\n");
1211:       PetscViewerASCIIPrintf(viewer, "  \\node(\\e_%d) [draw,shape=circle,color=%s,minimum size = 6mm] at (\\eShift+\\e-\\eStart,1) {\\e};\n", rank, color);
1212:       PetscViewerASCIIPrintf(viewer, "}\n");

1214:       color = colors[0%numColors];
1215:       PetscViewerASCIIPrintf(viewer, "%% Vertices\n");
1216:       PetscViewerASCIIPrintf(viewer, "\\foreach \\v in {\\vStart,...,\\vEnd}\n");
1217:       PetscViewerASCIIPrintf(viewer, "{\n");
1218:       PetscViewerASCIIPrintf(viewer, "  \\node(\\v_%d) [draw,shape=circle,color=%s,minimum size = 6mm] at (\\vShift+\\v-\\vStart,2) {\\v};\n", rank, color);
1219:       PetscViewerASCIIPrintf(viewer, "}\n");

1221:       for (p = pStart; p < pEnd; ++p) {
1222:         const PetscInt *cone;
1223:         PetscInt        coneSize, cp;

1225:         DMPlexGetCone(dm, p, &cone);
1226:         DMPlexGetConeSize(dm, p, &coneSize);
1227:         for (cp = 0; cp < coneSize; ++cp) {
1228:           PetscViewerASCIIPrintf(viewer, "\\draw[->, shorten >=1pt] (%D_%d) -- (%D_%d);\n", cone[cp], rank, p, rank);
1229:         }
1230:       }
1231:     }
1232:     PetscViewerFlush(viewer);
1233:     PetscViewerASCIIPopSynchronized(viewer);
1234:     PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");
1235:     PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);
1236:     for (l = 0; l < numLabels;  ++l) PetscFree(names[l]);
1237:     for (c = 0; c < numColors;  ++c) PetscFree(colors[c]);
1238:     for (c = 0; c < numLColors; ++c) PetscFree(lcolors[c]);
1239:     PetscFree3(names, colors, lcolors);
1240:     PetscBTDestroy(&wp);
1241:   } else if (format == PETSC_VIEWER_LOAD_BALANCE) {
1242:     Vec                    cown,acown;
1243:     VecScatter             sct;
1244:     ISLocalToGlobalMapping g2l;
1245:     IS                     gid,acis;
1246:     MPI_Comm               comm,ncomm = MPI_COMM_NULL;
1247:     MPI_Group              ggroup,ngroup;
1248:     PetscScalar            *array,nid;
1249:     const PetscInt         *idxs;
1250:     PetscInt               *idxs2,*start,*adjacency,*work;
1251:     PetscInt64             lm[3],gm[3];
1252:     PetscInt               i,c,cStart,cEnd,cum,numVertices,ect,ectn,cellHeight;
1253:     PetscMPIInt            d1,d2,rank;

1255:     PetscObjectGetComm((PetscObject)dm,&comm);
1256:     MPI_Comm_rank(comm,&rank);
1257: #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
1258:     MPI_Comm_split_type(comm,MPI_COMM_TYPE_SHARED,rank,MPI_INFO_NULL,&ncomm);
1259: #endif
1260:     if (ncomm != MPI_COMM_NULL) {
1261:       MPI_Comm_group(comm,&ggroup);
1262:       MPI_Comm_group(ncomm,&ngroup);
1263:       d1   = 0;
1264:       MPI_Group_translate_ranks(ngroup,1,&d1,ggroup,&d2);
1265:       nid  = d2;
1266:       MPI_Group_free(&ggroup);
1267:       MPI_Group_free(&ngroup);
1268:       MPI_Comm_free(&ncomm);
1269:     } else nid = 0.0;

1271:     /* Get connectivity */
1272:     DMPlexGetVTKCellHeight(dm,&cellHeight);
1273:     DMPlexCreatePartitionerGraph(dm,cellHeight,&numVertices,&start,&adjacency,&gid);

1275:     /* filter overlapped local cells */
1276:     DMPlexGetHeightStratum(dm,cellHeight,&cStart,&cEnd);
1277:     ISGetIndices(gid,&idxs);
1278:     ISGetLocalSize(gid,&cum);
1279:     PetscMalloc1(cum,&idxs2);
1280:     for (c = cStart, cum = 0; c < cEnd; c++) {
1281:       if (idxs[c-cStart] < 0) continue;
1282:       idxs2[cum++] = idxs[c-cStart];
1283:     }
1284:     ISRestoreIndices(gid,&idxs);
1286:     ISDestroy(&gid);
1287:     ISCreateGeneral(comm,numVertices,idxs2,PETSC_OWN_POINTER,&gid);

1289:     /* support for node-aware cell locality */
1290:     ISCreateGeneral(comm,start[numVertices],adjacency,PETSC_USE_POINTER,&acis);
1291:     VecCreateSeq(PETSC_COMM_SELF,start[numVertices],&acown);
1292:     VecCreateMPI(comm,numVertices,PETSC_DECIDE,&cown);
1293:     VecGetArray(cown,&array);
1294:     for (c = 0; c < numVertices; c++) array[c] = nid;
1295:     VecRestoreArray(cown,&array);
1296:     VecScatterCreate(cown,acis,acown,NULL,&sct);
1297:     VecScatterBegin(sct,cown,acown,INSERT_VALUES,SCATTER_FORWARD);
1298:     VecScatterEnd(sct,cown,acown,INSERT_VALUES,SCATTER_FORWARD);
1299:     ISDestroy(&acis);
1300:     VecScatterDestroy(&sct);
1301:     VecDestroy(&cown);

1303:     /* compute edgeCut */
1304:     for (c = 0, cum = 0; c < numVertices; c++) cum = PetscMax(cum,start[c+1]-start[c]);
1305:     PetscMalloc1(cum,&work);
1306:     ISLocalToGlobalMappingCreateIS(gid,&g2l);
1307:     ISLocalToGlobalMappingSetType(g2l,ISLOCALTOGLOBALMAPPINGHASH);
1308:     ISDestroy(&gid);
1309:     VecGetArray(acown,&array);
1310:     for (c = 0, ect = 0, ectn = 0; c < numVertices; c++) {
1311:       PetscInt totl;

1313:       totl = start[c+1]-start[c];
1314:       ISGlobalToLocalMappingApply(g2l,IS_GTOLM_MASK,totl,adjacency+start[c],NULL,work);
1315:       for (i = 0; i < totl; i++) {
1316:         if (work[i] < 0) {
1317:           ect  += 1;
1318:           ectn += (array[i + start[c]] != nid) ? 0 : 1;
1319:         }
1320:       }
1321:     }
1322:     PetscFree(work);
1323:     VecRestoreArray(acown,&array);
1324:     lm[0] = numVertices > 0 ?  numVertices : PETSC_MAX_INT;
1325:     lm[1] = -numVertices;
1326:     MPIU_Allreduce(lm,gm,2,MPIU_INT64,MPI_MIN,comm);
1327:     PetscViewerASCIIPrintf(viewer,"  Cell balance: %.2f (max %D, min %D",-((double)gm[1])/((double)gm[0]),-(PetscInt)gm[1],(PetscInt)gm[0]);
1328:     lm[0] = ect; /* edgeCut */
1329:     lm[1] = ectn; /* node-aware edgeCut */
1330:     lm[2] = numVertices > 0 ? 0 : 1; /* empty processes */
1331:     MPIU_Allreduce(lm,gm,3,MPIU_INT64,MPI_SUM,comm);
1332:     PetscViewerASCIIPrintf(viewer,", empty %D)\n",(PetscInt)gm[2]);
1333: #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
1334:     PetscViewerASCIIPrintf(viewer,"  Edge Cut: %D (on node %.3f)\n",(PetscInt)(gm[0]/2),gm[0] ? ((double)(gm[1]))/((double)gm[0]) : 1.);
1335: #else
1336:     PetscViewerASCIIPrintf(viewer,"  Edge Cut: %D (on node %.3f)\n",(PetscInt)(gm[0]/2),0.0);
1337: #endif
1338:     ISLocalToGlobalMappingDestroy(&g2l);
1339:     PetscFree(start);
1340:     PetscFree(adjacency);
1341:     VecDestroy(&acown);
1342:   } else {
1343:     const char    *name;
1344:     PetscInt      *sizes, *hybsizes, *ghostsizes;
1345:     PetscInt       locDepth, depth, cellHeight, dim, d;
1346:     PetscInt       pStart, pEnd, p, gcStart, gcEnd, gcNum;
1347:     PetscInt       numLabels, l, maxSize = 17;
1348:     DMPolytopeType ct0 = DM_POLYTOPE_UNKNOWN;
1349:     MPI_Comm       comm;
1350:     PetscMPIInt    size, rank;

1352:     PetscObjectGetComm((PetscObject) dm, &comm);
1353:     MPI_Comm_size(comm, &size);
1354:     MPI_Comm_rank(comm, &rank);
1355:     DMGetDimension(dm, &dim);
1356:     DMPlexGetVTKCellHeight(dm, &cellHeight);
1357:     PetscObjectGetName((PetscObject) dm, &name);
1358:     if (name) PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");
1359:     else      PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");
1360:     if (cellHeight) PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);
1361:     DMPlexGetDepth(dm, &locDepth);
1362:     MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);
1363:     DMPlexGetGhostCellStratum(dm, &gcStart, &gcEnd);
1364:     gcNum = gcEnd - gcStart;
1365:     if (size < maxSize) PetscCalloc3(size, &sizes, size, &hybsizes, size, &ghostsizes);
1366:     else                PetscCalloc3(3,    &sizes, 3,    &hybsizes, 3,    &ghostsizes);
1367:     for (d = 0; d <= depth; d++) {
1368:       PetscInt Nc[2] = {0, 0}, ict;

1370:       DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);
1371:       if (pStart < pEnd) DMPlexGetCellType(dm, pStart, &ct0);
1372:       ict  = ct0;
1373:       MPI_Bcast(&ict, 1, MPIU_INT, 0, comm);
1374:       ct0  = (DMPolytopeType) ict;
1375:       for (p = pStart; p < pEnd; ++p) {
1376:         DMPolytopeType ct;

1378:         DMPlexGetCellType(dm, p, &ct);
1379:         if (ct == ct0) ++Nc[0];
1380:         else           ++Nc[1];
1381:       }
1382:       if (size < maxSize) {
1383:         MPI_Gather(&Nc[0], 1, MPIU_INT, sizes,    1, MPIU_INT, 0, comm);
1384:         MPI_Gather(&Nc[1], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);
1385:         if (d == depth) MPI_Gather(&gcNum, 1, MPIU_INT, ghostsizes, 1, MPIU_INT, 0, comm);
1386:         PetscViewerASCIIPrintf(viewer, "  Number of %D-cells per rank:", (depth == 1) && d ? dim : d);
1387:         for (p = 0; p < size; ++p) {
1388:           if (rank == 0) {
1389:             PetscViewerASCIIPrintf(viewer, " %D", sizes[p]+hybsizes[p]);
1390:             if (hybsizes[p]   > 0) PetscViewerASCIIPrintf(viewer, " (%D)", hybsizes[p]);
1391:             if (ghostsizes[p] > 0) PetscViewerASCIIPrintf(viewer, " [%D]", ghostsizes[p]);
1392:           }
1393:         }
1394:       } else {
1395:         PetscInt locMinMax[2];

1397:         locMinMax[0] = Nc[0]+Nc[1]; locMinMax[1] = Nc[0]+Nc[1];
1398:         PetscGlobalMinMaxInt(comm, locMinMax, sizes);
1399:         locMinMax[0] = Nc[1]; locMinMax[1] = Nc[1];
1400:         PetscGlobalMinMaxInt(comm, locMinMax, hybsizes);
1401:         if (d == depth) {
1402:           locMinMax[0] = gcNum; locMinMax[1] = gcNum;
1403:           PetscGlobalMinMaxInt(comm, locMinMax, ghostsizes);
1404:         }
1405:         PetscViewerASCIIPrintf(viewer, "  Min/Max of %D-cells per rank:", (depth == 1) && d ? dim : d);
1406:         PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT "/%" PetscInt_FMT, sizes[0], sizes[1]);
1407:         if (hybsizes[0]   > 0) PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT "/%" PetscInt_FMT ")", hybsizes[0], hybsizes[1]);
1408:         if (ghostsizes[0] > 0) PetscViewerASCIIPrintf(viewer, " [%" PetscInt_FMT "/%" PetscInt_FMT "]", ghostsizes[0], ghostsizes[1]);
1409:       }
1410:       PetscViewerASCIIPrintf(viewer, "\n");
1411:     }
1412:     PetscFree3(sizes, hybsizes, ghostsizes);
1413:     {
1414:       const PetscReal      *maxCell;
1415:       const PetscReal      *L;
1416:       const DMBoundaryType *bd;
1417:       PetscBool             per, localized;

1419:       DMGetPeriodicity(dm, &per, &maxCell, &L, &bd);
1420:       DMGetCoordinatesLocalized(dm, &localized);
1421:       if (per) {
1422:         PetscViewerASCIIPrintf(viewer, "Periodic mesh (");
1423:         PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);
1424:         for (d = 0; d < dim; ++d) {
1425:           if (bd && d > 0) PetscViewerASCIIPrintf(viewer, ", ");
1426:           if (bd)    PetscViewerASCIIPrintf(viewer, "%s", DMBoundaryTypes[bd[d]]);
1427:         }
1428:         PetscViewerASCIIPrintf(viewer, ") coordinates %s\n", localized ? "localized" : "not localized");
1429:         PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);
1430:       }
1431:     }
1432:     DMGetNumLabels(dm, &numLabels);
1433:     if (numLabels) PetscViewerASCIIPrintf(viewer, "Labels:\n");
1434:     for (l = 0; l < numLabels; ++l) {
1435:       DMLabel         label;
1436:       const char     *name;
1437:       IS              valueIS;
1438:       const PetscInt *values;
1439:       PetscInt        numValues, v;

1441:       DMGetLabelName(dm, l, &name);
1442:       DMGetLabel(dm, name, &label);
1443:       DMLabelGetNumValues(label, &numValues);
1444:       PetscViewerASCIIPrintf(viewer, "  %s: %D strata with value/size (", name, numValues);
1445:       DMLabelGetValueIS(label, &valueIS);
1446:       ISGetIndices(valueIS, &values);
1447:       PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);
1448:       for (v = 0; v < numValues; ++v) {
1449:         PetscInt size;

1451:         DMLabelGetStratumSize(label, values[v], &size);
1452:         if (v > 0) PetscViewerASCIIPrintf(viewer, ", ");
1453:         PetscViewerASCIIPrintf(viewer, "%D (%D)", values[v], size);
1454:       }
1455:       PetscViewerASCIIPrintf(viewer, ")\n");
1456:       PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);
1457:       ISRestoreIndices(valueIS, &values);
1458:       ISDestroy(&valueIS);
1459:     }
1460:     {
1461:       char    **labelNames;
1462:       PetscInt  Nl = numLabels;
1463:       PetscBool flg;

1465:       PetscMalloc1(Nl, &labelNames);
1466:       PetscOptionsGetStringArray(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_plex_view_labels", labelNames, &Nl, &flg);
1467:       for (l = 0; l < Nl; ++l) {
1468:         DMLabel label;

1470:         DMHasLabel(dm, labelNames[l], &flg);
1471:         if (flg) {
1472:           DMGetLabel(dm, labelNames[l], &label);
1473:           DMLabelView(label, viewer);
1474:         }
1475:         PetscFree(labelNames[l]);
1476:       }
1477:       PetscFree(labelNames);
1478:     }
1479:     /* If no fields are specified, people do not want to see adjacency */
1480:     if (dm->Nf) {
1481:       PetscInt f;

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

1486:         PetscObjectGetName(dm->fields[f].disc, &name);
1487:         if (numLabels) PetscViewerASCIIPrintf(viewer, "Field %s:\n", name);
1488:         PetscViewerASCIIPushTab(viewer);
1489:         if (dm->fields[f].label) DMLabelView(dm->fields[f].label, viewer);
1490:         if (dm->fields[f].adjacency[0]) {
1491:           if (dm->fields[f].adjacency[1]) PetscViewerASCIIPrintf(viewer, "adjacency FVM++\n");
1492:           else                            PetscViewerASCIIPrintf(viewer, "adjacency FVM\n");
1493:         } else {
1494:           if (dm->fields[f].adjacency[1]) PetscViewerASCIIPrintf(viewer, "adjacency FEM\n");
1495:           else                            PetscViewerASCIIPrintf(viewer, "adjacency FUNKY\n");
1496:         }
1497:         PetscViewerASCIIPopTab(viewer);
1498:       }
1499:     }
1500:     DMGetCoarseDM(dm, &cdm);
1501:     if (cdm) {
1502:       PetscViewerASCIIPushTab(viewer);
1503:       DMPlexView_Ascii(cdm, viewer);
1504:       PetscViewerASCIIPopTab(viewer);
1505:     }
1506:   }
1507:   return 0;
1508: }

1510: static PetscErrorCode DMPlexDrawCell(DM dm, PetscDraw draw, PetscInt cell, const PetscScalar coords[])
1511: {
1512:   DMPolytopeType ct;
1513:   PetscMPIInt    rank;
1514:   PetscInt       cdim;

1516:   MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);
1517:   DMPlexGetCellType(dm, cell, &ct);
1518:   DMGetCoordinateDim(dm, &cdim);
1519:   switch (ct) {
1520:   case DM_POLYTOPE_SEGMENT:
1521:   case DM_POLYTOPE_POINT_PRISM_TENSOR:
1522:     switch (cdim) {
1523:     case 1:
1524:     {
1525:       const PetscReal y  = 0.5;  /* TODO Put it in the middle of the viewport */
1526:       const PetscReal dy = 0.05; /* TODO Make it a fraction of the total length */

1528:       PetscDrawLine(draw, PetscRealPart(coords[0]), y,    PetscRealPart(coords[1]), y,    PETSC_DRAW_BLACK);
1529:       PetscDrawLine(draw, PetscRealPart(coords[0]), y+dy, PetscRealPart(coords[0]), y-dy, PETSC_DRAW_BLACK);
1530:       PetscDrawLine(draw, PetscRealPart(coords[1]), y+dy, PetscRealPart(coords[1]), y-dy, PETSC_DRAW_BLACK);
1531:     }
1532:     break;
1533:     case 2:
1534:     {
1535:       const PetscReal dx = (PetscRealPart(coords[3]) - PetscRealPart(coords[1]));
1536:       const PetscReal dy = (PetscRealPart(coords[2]) - PetscRealPart(coords[0]));
1537:       const PetscReal l  = 0.1/PetscSqrtReal(dx*dx + dy*dy);

1539:       PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);
1540:       PetscDrawLine(draw, PetscRealPart(coords[0])+l*dx, PetscRealPart(coords[1])+l*dy, PetscRealPart(coords[0])-l*dx, PetscRealPart(coords[1])-l*dy, PETSC_DRAW_BLACK);
1541:       PetscDrawLine(draw, PetscRealPart(coords[2])+l*dx, PetscRealPart(coords[3])+l*dy, PetscRealPart(coords[2])-l*dx, PetscRealPart(coords[3])-l*dy, PETSC_DRAW_BLACK);
1542:     }
1543:     break;
1544:     default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of dimension %D", cdim);
1545:     }
1546:     break;
1547:   case DM_POLYTOPE_TRIANGLE:
1548:     PetscCall(PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1549:                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1550:                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1551:                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2));
1552:     PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);
1553:     PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);
1554:     PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);
1555:     break;
1556:   case DM_POLYTOPE_QUADRILATERAL:
1557:     PetscCall(PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1558:                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1559:                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1560:                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2));
1561:     PetscCall(PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]),
1562:                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1563:                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1564:                               PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2));
1565:     PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);
1566:     PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);
1567:     PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);
1568:     PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);
1569:     break;
1570:   default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of type %s", DMPolytopeTypes[ct]);
1571:   }
1572:   return 0;
1573: }

1575: static PetscErrorCode DMPlexDrawCellHighOrder(DM dm, PetscDraw draw, PetscInt cell, const PetscScalar coords[], PetscInt edgeDiv, PetscReal refCoords[], PetscReal edgeCoords[])
1576: {
1577:   DMPolytopeType ct;
1578:   PetscReal      centroid[2] = {0., 0.};
1579:   PetscMPIInt    rank;
1580:   PetscInt       fillColor, v, e, d;

1582:   MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);
1583:   DMPlexGetCellType(dm, cell, &ct);
1584:   fillColor = PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2;
1585:   switch (ct) {
1586:   case DM_POLYTOPE_TRIANGLE:
1587:     {
1588:       PetscReal refVertices[6] = {-1., -1., 1., -1., -1., 1.};

1590:       for (v = 0; v < 3; ++v) {centroid[0] += PetscRealPart(coords[v*2+0])/3.;centroid[1] += PetscRealPart(coords[v*2+1])/3.;}
1591:       for (e = 0; e < 3; ++e) {
1592:         refCoords[0] = refVertices[e*2+0];
1593:         refCoords[1] = refVertices[e*2+1];
1594:         for (d = 1; d <= edgeDiv; ++d) {
1595:           refCoords[d*2+0] = refCoords[0] + (refVertices[(e+1)%3 * 2 + 0] - refCoords[0])*d/edgeDiv;
1596:           refCoords[d*2+1] = refCoords[1] + (refVertices[(e+1)%3 * 2 + 1] - refCoords[1])*d/edgeDiv;
1597:         }
1598:         DMPlexReferenceToCoordinates(dm, cell, edgeDiv+1, refCoords, edgeCoords);
1599:         for (d = 0; d < edgeDiv; ++d) {
1600:           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);
1601:           PetscDrawLine(draw, edgeCoords[d*2+0], edgeCoords[d*2+1], edgeCoords[(d+1)*2+0], edgeCoords[(d+1)*2+1], PETSC_DRAW_BLACK);
1602:         }
1603:       }
1604:     }
1605:     break;
1606:   default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells of type %s", DMPolytopeTypes[ct]);
1607:   }
1608:   return 0;
1609: }

1611: static PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
1612: {
1613:   PetscDraw          draw;
1614:   DM                 cdm;
1615:   PetscSection       coordSection;
1616:   Vec                coordinates;
1617:   const PetscScalar *coords;
1618:   PetscReal          xyl[2],xyr[2],bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
1619:   PetscReal         *refCoords, *edgeCoords;
1620:   PetscBool          isnull, drawAffine = PETSC_TRUE;
1621:   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N, edgeDiv = 4;

1623:   DMGetCoordinateDim(dm, &dim);
1625:   PetscOptionsGetBool(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_view_draw_affine", &drawAffine, NULL);
1626:   if (!drawAffine) PetscMalloc2((edgeDiv+1)*dim, &refCoords, (edgeDiv+1)*dim, &edgeCoords);
1627:   DMGetCoordinateDM(dm, &cdm);
1628:   DMGetLocalSection(cdm, &coordSection);
1629:   DMGetCoordinatesLocal(dm, &coordinates);
1630:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
1631:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);

1633:   PetscViewerDrawGetDraw(viewer, 0, &draw);
1634:   PetscDrawIsNull(draw, &isnull);
1635:   if (isnull) return 0;
1636:   PetscDrawSetTitle(draw, "Mesh");

1638:   VecGetLocalSize(coordinates, &N);
1639:   VecGetArrayRead(coordinates, &coords);
1640:   for (c = 0; c < N; c += dim) {
1641:     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
1642:     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
1643:   }
1644:   VecRestoreArrayRead(coordinates, &coords);
1645:   MPIU_Allreduce(&bound[0],xyl,2,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)dm));
1646:   MPIU_Allreduce(&bound[2],xyr,2,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)dm));
1647:   PetscDrawSetCoordinates(draw, xyl[0], xyl[1], xyr[0], xyr[1]);
1648:   PetscDrawClear(draw);

1650:   for (c = cStart; c < cEnd; ++c) {
1651:     PetscScalar *coords = NULL;
1652:     PetscInt     numCoords;

1654:     DMPlexVecGetClosureAtDepth_Internal(dm, coordSection, coordinates, c, 0, &numCoords, &coords);
1655:     if (drawAffine) {
1656:       DMPlexDrawCell(dm, draw, c, coords);
1657:     } else {
1658:       DMPlexDrawCellHighOrder(dm, draw, c, coords, edgeDiv, refCoords, edgeCoords);
1659:     }
1660:     DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);
1661:   }
1662:   if (!drawAffine) PetscFree2(refCoords, edgeCoords);
1663:   PetscDrawFlush(draw);
1664:   PetscDrawPause(draw);
1665:   PetscDrawSave(draw);
1666:   return 0;
1667: }

1669: #if defined(PETSC_HAVE_EXODUSII)
1670: #include <exodusII.h>
1671: #include <petscviewerexodusii.h>
1672: #endif

1674: PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
1675: {
1676:   PetscBool      iascii, ishdf5, isvtk, isdraw, flg, isglvis, isexodus;
1677:   char           name[PETSC_MAX_PATH_LEN];

1681:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII,    &iascii);
1682:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,      &isvtk);
1683:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,     &ishdf5);
1684:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,     &isdraw);
1685:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS,    &isglvis);
1686:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWEREXODUSII, &isexodus);
1687:   if (iascii) {
1688:     PetscViewerFormat format;
1689:     PetscViewerGetFormat(viewer, &format);
1690:     if (format == PETSC_VIEWER_ASCII_GLVIS) {
1691:       DMPlexView_GLVis(dm, viewer);
1692:     } else {
1693:       DMPlexView_Ascii(dm, viewer);
1694:     }
1695:   } else if (ishdf5) {
1696: #if defined(PETSC_HAVE_HDF5)
1697:     DMPlexView_HDF5_Internal(dm, viewer);
1698: #else
1699:     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1700: #endif
1701:   } else if (isvtk) {
1702:     DMPlexVTKWriteAll((PetscObject) dm,viewer);
1703:   } else if (isdraw) {
1704:     DMPlexView_Draw(dm, viewer);
1705:   } else if (isglvis) {
1706:     DMPlexView_GLVis(dm, viewer);
1707: #if defined(PETSC_HAVE_EXODUSII)
1708:   } else if (isexodus) {
1709: /*
1710:       exodusII requires that all sets be part of exactly one cell set.
1711:       If the dm does not have a "Cell Sets" label defined, we create one
1712:       with ID 1, containig all cells.
1713:       Note that if the Cell Sets label is defined but does not cover all cells,
1714:       we may still have a problem. This should probably be checked here or in the viewer;
1715:     */
1716:     PetscInt numCS;
1717:     DMGetLabelSize(dm,"Cell Sets",&numCS);
1718:     if (!numCS) {
1719:       PetscInt cStart, cEnd, c;
1720:       DMCreateLabel(dm, "Cell Sets");
1721:       DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
1722:       for (c = cStart; c < cEnd; ++c) DMSetLabelValue(dm, "Cell Sets", c, 1);
1723:     }
1724:     DMView_PlexExodusII(dm, viewer);
1725: #endif
1726:   } else {
1727:     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex writing", ((PetscObject)viewer)->type_name);
1728:   }
1729:   /* Optionally view the partition */
1730:   PetscOptionsHasName(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_partition_view", &flg);
1731:   if (flg) {
1732:     Vec ranks;
1733:     DMPlexCreateRankField(dm, &ranks);
1734:     VecView(ranks, viewer);
1735:     VecDestroy(&ranks);
1736:   }
1737:   /* Optionally view a label */
1738:   PetscOptionsGetString(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_label_view", name, sizeof(name), &flg);
1739:   if (flg) {
1740:     DMLabel label;
1741:     Vec     val;

1743:     DMGetLabel(dm, name, &label);
1745:     DMPlexCreateLabelField(dm, label, &val);
1746:     VecView(val, viewer);
1747:     VecDestroy(&val);
1748:   }
1749:   return 0;
1750: }

1752: /*@
1753:   DMPlexTopologyView - Saves a DMPlex topology into a file

1755:   Collective on DM

1757:   Input Parameters:
1758: + dm     - The DM whose topology is to be saved
1759: - viewer - The PetscViewer for saving

1761:   Level: advanced

1763: .seealso: DMView(), DMPlexCoordinatesView(), DMPlexLabelsView(), DMPlexTopologyLoad()
1764: @*/
1765: PetscErrorCode DMPlexTopologyView(DM dm, PetscViewer viewer)
1766: {
1767:   PetscBool      ishdf5;

1771:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);
1772:   PetscLogEventBegin(DMPLEX_TopologyView,viewer,0,0,0);
1773:   if (ishdf5) {
1774: #if defined(PETSC_HAVE_HDF5)
1775:     PetscViewerFormat format;
1776:     PetscViewerGetFormat(viewer, &format);
1777:     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
1778:       IS globalPointNumbering;

1780:       DMPlexCreatePointNumbering(dm, &globalPointNumbering);
1781:       DMPlexTopologyView_HDF5_Internal(dm, globalPointNumbering, viewer);
1782:       ISDestroy(&globalPointNumbering);
1783:     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 output.", PetscViewerFormats[format]);
1784: #else
1785:     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1786: #endif
1787:   }
1788:   PetscLogEventEnd(DMPLEX_TopologyView,viewer,0,0,0);
1789:   return 0;
1790: }

1792: /*@
1793:   DMPlexCoordinatesView - Saves DMPlex coordinates into a file

1795:   Collective on DM

1797:   Input Parameters:
1798: + dm     - The DM whose coordinates are to be saved
1799: - viewer - The PetscViewer for saving

1801:   Level: advanced

1803: .seealso: DMView(), DMPlexTopologyView(), DMPlexLabelsView(), DMPlexCoordinatesLoad()
1804: @*/
1805: PetscErrorCode DMPlexCoordinatesView(DM dm, PetscViewer viewer)
1806: {
1807:   PetscBool      ishdf5;

1811:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);
1812:   PetscLogEventBegin(DMPLEX_CoordinatesView,viewer,0,0,0);
1813:   if (ishdf5) {
1814: #if defined(PETSC_HAVE_HDF5)
1815:     PetscViewerFormat format;
1816:     PetscViewerGetFormat(viewer, &format);
1817:     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
1818:       DMPlexCoordinatesView_HDF5_Internal(dm, viewer);
1819:     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
1820: #else
1821:     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1822: #endif
1823:   }
1824:   PetscLogEventEnd(DMPLEX_CoordinatesView,viewer,0,0,0);
1825:   return 0;
1826: }

1828: /*@
1829:   DMPlexLabelsView - Saves DMPlex labels into a file

1831:   Collective on DM

1833:   Input Parameters:
1834: + dm     - The DM whose labels are to be saved
1835: - viewer - The PetscViewer for saving

1837:   Level: advanced

1839: .seealso: DMView(), DMPlexTopologyView(), DMPlexCoordinatesView(), DMPlexLabelsLoad()
1840: @*/
1841: PetscErrorCode DMPlexLabelsView(DM dm, PetscViewer viewer)
1842: {
1843:   PetscBool      ishdf5;

1847:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);
1848:   PetscLogEventBegin(DMPLEX_LabelsView,viewer,0,0,0);
1849:   if (ishdf5) {
1850: #if defined(PETSC_HAVE_HDF5)
1851:     IS                globalPointNumbering;
1852:     PetscViewerFormat format;

1854:     PetscViewerGetFormat(viewer, &format);
1855:     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
1856:       DMPlexCreatePointNumbering(dm, &globalPointNumbering);
1857:       DMPlexLabelsView_HDF5_Internal(dm, globalPointNumbering, viewer);
1858:       ISDestroy(&globalPointNumbering);
1859:     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
1860: #else
1861:     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1862: #endif
1863:   }
1864:   PetscLogEventEnd(DMPLEX_LabelsView,viewer,0,0,0);
1865:   return 0;
1866: }

1868: /*@
1869:   DMPlexSectionView - Saves a section associated with a DMPlex

1871:   Collective on DM

1873:   Input Parameters:
1874: + dm         - The DM that contains the topology on which the section to be saved is defined
1875: . viewer     - The PetscViewer for saving
1876: - sectiondm  - The DM that contains the section to be saved

1878:   Level: advanced

1880:   Notes:
1881:   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.

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

1885: .seealso: DMView(), DMPlexTopologyView(), DMPlexCoordinatesView(), DMPlexLabelsView(), DMPlexGlobalVectorView(), DMPlexLocalVectorView(), PetscSectionView(), DMPlexSectionLoad()
1886: @*/
1887: PetscErrorCode DMPlexSectionView(DM dm, PetscViewer viewer, DM sectiondm)
1888: {
1889:   PetscBool      ishdf5;

1894:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);
1895:   PetscLogEventBegin(DMPLEX_SectionView,viewer,0,0,0);
1896:   if (ishdf5) {
1897: #if defined(PETSC_HAVE_HDF5)
1898:     DMPlexSectionView_HDF5_Internal(dm, viewer, sectiondm);
1899: #else
1900:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1901: #endif
1902:   }
1903:   PetscLogEventEnd(DMPLEX_SectionView,viewer,0,0,0);
1904:   return 0;
1905: }

1907: /*@
1908:   DMPlexGlobalVectorView - Saves a global vector

1910:   Collective on DM

1912:   Input Parameters:
1913: + dm        - The DM that represents the topology
1914: . viewer    - The PetscViewer to save data with
1915: . sectiondm - The DM that contains the global section on which vec is defined
1916: - vec       - The global vector to be saved

1918:   Level: advanced

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

1923:   Typical calling sequence
1924: $       DMCreate(PETSC_COMM_WORLD, &dm);
1925: $       DMSetType(dm, DMPLEX);
1926: $       PetscObjectSetName((PetscObject)dm, "topologydm_name");
1927: $       DMClone(dm, &sectiondm);
1928: $       PetscObjectSetName((PetscObject)sectiondm, "sectiondm_name");
1929: $       PetscSectionCreate(PETSC_COMM_WORLD, &section);
1930: $       DMPlexGetChart(sectiondm, &pStart, &pEnd);
1931: $       PetscSectionSetChart(section, pStart, pEnd);
1932: $       PetscSectionSetUp(section);
1933: $       DMSetLocalSection(sectiondm, section);
1934: $       PetscSectionDestroy(&section);
1935: $       DMGetGlobalVector(sectiondm, &vec);
1936: $       PetscObjectSetName((PetscObject)vec, "vec_name");
1937: $       DMPlexTopologyView(dm, viewer);
1938: $       DMPlexSectionView(dm, viewer, sectiondm);
1939: $       DMPlexGlobalVectorView(dm, viewer, sectiondm, vec);
1940: $       DMRestoreGlobalVector(sectiondm, &vec);
1941: $       DMDestroy(&sectiondm);
1942: $       DMDestroy(&dm);

1944: .seealso: DMPlexTopologyView(), DMPlexSectionView(), DMPlexLocalVectorView(), DMPlexGlobalVectorLoad(), DMPlexLocalVectorLoad()
1945: @*/
1946: PetscErrorCode DMPlexGlobalVectorView(DM dm, PetscViewer viewer, DM sectiondm, Vec vec)
1947: {
1948:   PetscBool       ishdf5;

1954:   /* Check consistency */
1955:   {
1956:     PetscSection  section;
1957:     PetscBool     includesConstraints;
1958:     PetscInt      m, m1;

1960:     VecGetLocalSize(vec, &m1);
1961:     DMGetGlobalSection(sectiondm, &section);
1962:     PetscSectionGetIncludesConstraints(section, &includesConstraints);
1963:     if (includesConstraints) PetscSectionGetStorageSize(section, &m);
1964:     else PetscSectionGetConstrainedStorageSize(section, &m);
1966:   }
1967:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
1968:   PetscLogEventBegin(DMPLEX_GlobalVectorView,viewer,0,0,0);
1969:   if (ishdf5) {
1970: #if defined(PETSC_HAVE_HDF5)
1971:     DMPlexGlobalVectorView_HDF5_Internal(dm, viewer, sectiondm, vec);
1972: #else
1973:     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1974: #endif
1975:   }
1976:   PetscLogEventEnd(DMPLEX_GlobalVectorView,viewer,0,0,0);
1977:   return 0;
1978: }

1980: /*@
1981:   DMPlexLocalVectorView - Saves a local vector

1983:   Collective on DM

1985:   Input Parameters:
1986: + dm        - The DM that represents the topology
1987: . viewer    - The PetscViewer to save data with
1988: . sectiondm - The DM that contains the local section on which vec is defined; may be the same as dm
1989: - vec       - The local vector to be saved

1991:   Level: advanced

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

1996:   Typical calling sequence
1997: $       DMCreate(PETSC_COMM_WORLD, &dm);
1998: $       DMSetType(dm, DMPLEX);
1999: $       PetscObjectSetName((PetscObject)dm, "topologydm_name");
2000: $       DMClone(dm, &sectiondm);
2001: $       PetscObjectSetName((PetscObject)sectiondm, "sectiondm_name");
2002: $       PetscSectionCreate(PETSC_COMM_WORLD, &section);
2003: $       DMPlexGetChart(sectiondm, &pStart, &pEnd);
2004: $       PetscSectionSetChart(section, pStart, pEnd);
2005: $       PetscSectionSetUp(section);
2006: $       DMSetLocalSection(sectiondm, section);
2007: $       DMGetLocalVector(sectiondm, &vec);
2008: $       PetscObjectSetName((PetscObject)vec, "vec_name");
2009: $       DMPlexTopologyView(dm, viewer);
2010: $       DMPlexSectionView(dm, viewer, sectiondm);
2011: $       DMPlexLocalVectorView(dm, viewer, sectiondm, vec);
2012: $       DMRestoreLocalVector(sectiondm, &vec);
2013: $       DMDestroy(&sectiondm);
2014: $       DMDestroy(&dm);

2016: .seealso: DMPlexTopologyView(), DMPlexSectionView(), DMPlexGlobalVectorView(), DMPlexGlobalVectorLoad(), DMPlexLocalVectorLoad()
2017: @*/
2018: PetscErrorCode DMPlexLocalVectorView(DM dm, PetscViewer viewer, DM sectiondm, Vec vec)
2019: {
2020:   PetscBool       ishdf5;

2026:   /* Check consistency */
2027:   {
2028:     PetscSection  section;
2029:     PetscBool     includesConstraints;
2030:     PetscInt      m, m1;

2032:     VecGetLocalSize(vec, &m1);
2033:     DMGetLocalSection(sectiondm, &section);
2034:     PetscSectionGetIncludesConstraints(section, &includesConstraints);
2035:     if (includesConstraints) PetscSectionGetStorageSize(section, &m);
2036:     else PetscSectionGetConstrainedStorageSize(section, &m);
2038:   }
2039:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
2040:   PetscLogEventBegin(DMPLEX_LocalVectorView,viewer,0,0,0);
2041:   if (ishdf5) {
2042: #if defined(PETSC_HAVE_HDF5)
2043:     DMPlexLocalVectorView_HDF5_Internal(dm, viewer, sectiondm, vec);
2044: #else
2045:     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2046: #endif
2047:   }
2048:   PetscLogEventEnd(DMPLEX_LocalVectorView,viewer,0,0,0);
2049:   return 0;
2050: }

2052: PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
2053: {
2054:   PetscBool      ishdf5;

2058:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);
2059:   if (ishdf5) {
2060: #if defined(PETSC_HAVE_HDF5)
2061:     PetscViewerFormat format;
2062:     PetscViewerGetFormat(viewer, &format);
2063:     if (format == PETSC_VIEWER_HDF5_XDMF || format == PETSC_VIEWER_HDF5_VIZ) {
2064:       DMPlexLoad_HDF5_Xdmf_Internal(dm, viewer);
2065:     } else if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
2066:       DMPlexLoad_HDF5_Internal(dm, viewer);
2067:     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
2068:     return 0;
2069: #else
2070:     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2071: #endif
2072:   } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex loading", ((PetscObject)viewer)->type_name);
2073: }

2075: /*@
2076:   DMPlexTopologyLoad - Loads a topology into a DMPlex

2078:   Collective on DM

2080:   Input Parameters:
2081: + dm     - The DM into which the topology is loaded
2082: - viewer - The PetscViewer for the saved topology

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

2087:   Level: advanced

2089: .seealso: DMLoad(), DMPlexCoordinatesLoad(), DMPlexLabelsLoad(), DMView(), PetscViewerHDF5Open(), PetscViewerPushFormat()
2090: @*/
2091: PetscErrorCode DMPlexTopologyLoad(DM dm, PetscViewer viewer, PetscSF *globalToLocalPointSF)
2092: {
2093:   PetscBool      ishdf5;

2098:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);
2099:   PetscLogEventBegin(DMPLEX_TopologyLoad,viewer,0,0,0);
2100:   if (ishdf5) {
2101: #if defined(PETSC_HAVE_HDF5)
2102:     PetscViewerFormat format;
2103:     PetscViewerGetFormat(viewer, &format);
2104:     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
2105:       DMPlexTopologyLoad_HDF5_Internal(dm, viewer, globalToLocalPointSF);
2106:     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
2107: #else
2108:     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2109: #endif
2110:   }
2111:   PetscLogEventEnd(DMPLEX_TopologyLoad,viewer,0,0,0);
2112:   return 0;
2113: }

2115: /*@
2116:   DMPlexCoordinatesLoad - Loads coordinates into a DMPlex

2118:   Collective on DM

2120:   Input Parameters:
2121: + dm     - The DM into which the coordinates are loaded
2122: . viewer - The PetscViewer for the saved coordinates
2123: - globalToLocalPointSF - The SF returned by DMPlexTopologyLoad() when loading dm from viewer

2125:   Level: advanced

2127: .seealso: DMLoad(), DMPlexTopologyLoad(), DMPlexLabelsLoad(), DMView(), PetscViewerHDF5Open(), PetscViewerPushFormat()
2128: @*/
2129: PetscErrorCode DMPlexCoordinatesLoad(DM dm, PetscViewer viewer, PetscSF globalToLocalPointSF)
2130: {
2131:   PetscBool      ishdf5;

2136:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);
2137:   PetscLogEventBegin(DMPLEX_CoordinatesLoad,viewer,0,0,0);
2138:   if (ishdf5) {
2139: #if defined(PETSC_HAVE_HDF5)
2140:     PetscViewerFormat format;
2141:     PetscViewerGetFormat(viewer, &format);
2142:     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
2143:       DMPlexCoordinatesLoad_HDF5_Internal(dm, viewer, globalToLocalPointSF);
2144:     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
2145: #else
2146:     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2147: #endif
2148:   }
2149:   PetscLogEventEnd(DMPLEX_CoordinatesLoad,viewer,0,0,0);
2150:   return 0;
2151: }

2153: /*@
2154:   DMPlexLabelsLoad - Loads labels into a DMPlex

2156:   Collective on DM

2158:   Input Parameters:
2159: + dm     - The DM into which the labels are loaded
2160: . viewer - The PetscViewer for the saved labels
2161: - globalToLocalPointSF - The SF returned by DMPlexTopologyLoad() when loading dm from viewer

2163:   Level: advanced

2165:   Notes:
2166:   The PetscSF argument must not be NULL if the DM is distributed, otherwise an error occurs.

2168: .seealso: DMLoad(), DMPlexTopologyLoad(), DMPlexCoordinatesLoad(), DMView(), PetscViewerHDF5Open(), PetscViewerPushFormat()
2169: @*/
2170: PetscErrorCode DMPlexLabelsLoad(DM dm, PetscViewer viewer, PetscSF globalToLocalPointSF)
2171: {
2172:   PetscBool      ishdf5;

2177:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);
2178:   PetscLogEventBegin(DMPLEX_LabelsLoad,viewer,0,0,0);
2179:   if (ishdf5) {
2180: #if defined(PETSC_HAVE_HDF5)
2181:     PetscViewerFormat format;

2183:     PetscViewerGetFormat(viewer, &format);
2184:     if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
2185:       DMPlexLabelsLoad_HDF5_Internal(dm, viewer, globalToLocalPointSF);
2186:     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
2187: #else
2188:     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2189: #endif
2190:   }
2191:   PetscLogEventEnd(DMPLEX_LabelsLoad,viewer,0,0,0);
2192:   return 0;
2193: }

2195: /*@
2196:   DMPlexSectionLoad - Loads section into a DMPlex

2198:   Collective on DM

2200:   Input Parameters:
2201: + dm          - The DM that represents the topology
2202: . viewer      - The PetscViewer that represents the on-disk section (sectionA)
2203: . sectiondm   - The DM into which the on-disk section (sectionA) is migrated
2204: - globalToLocalPointSF - The SF returned by DMPlexTopologyLoad() when loading dm from viewer

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

2210:   Level: advanced

2212:   Notes:
2213:   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.

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

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

2219:   Example using 2 processes:
2220: $  NX (number of points on dm): 4
2221: $  sectionA                   : the on-disk section
2222: $  vecA                       : a vector associated with sectionA
2223: $  sectionB                   : sectiondm's local section constructed in this function
2224: $  vecB (local)               : a vector associated with sectiondm's local section
2225: $  vecB (global)              : a vector associated with sectiondm's global section
2226: $
2227: $                                     rank 0    rank 1
2228: $  vecA (global)                  : [.0 .4 .1 | .2 .3]        <- to be loaded in DMPlexGlobalVectorLoad() or DMPlexLocalVectorLoad()
2229: $  sectionA->atlasOff             :       0 2 | 1             <- loaded in PetscSectionLoad()
2230: $  sectionA->atlasDof             :       1 3 | 1             <- loaded in PetscSectionLoad()
2231: $  sectionA's global point numbers:       0 2 | 3             <- loaded in DMPlexSectionLoad()
2232: $  [0, NX)                        :       0 1 | 2 3           <- conceptual partition used in globalToLocalPointSF
2233: $  sectionB's global point numbers:     0 1 3 | 3 2           <- associated with [0, NX) by globalToLocalPointSF
2234: $  sectionB->atlasDof             :     1 0 1 | 1 3
2235: $  sectionB->atlasOff (no perm)   :     0 1 1 | 0 1
2236: $  vecB (local)                   :   [.0 .4] | [.4 .1 .2 .3] <- to be constructed by calling DMPlexLocalVectorLoad() with localDofSF
2237: $  vecB (global)                  :    [.0 .4 | .1 .2 .3]     <- to be constructed by calling DMPlexGlobalVectorLoad() with globalDofSF
2238: $
2239: $  where "|" represents a partition of loaded data, and global point 3 is assumed to be owned by rank 0.

2241: .seealso: DMLoad(), DMPlexTopologyLoad(), DMPlexCoordinatesLoad(), DMPlexLabelsLoad(), DMPlexGlobalVectorLoad(), DMPlexLocalVectorLoad(), PetscSectionLoad(), DMPlexSectionView()
2242: @*/
2243: PetscErrorCode DMPlexSectionLoad(DM dm, PetscViewer viewer, DM sectiondm, PetscSF globalToLocalPointSF, PetscSF *globalDofSF, PetscSF *localDofSF)
2244: {
2245:   PetscBool      ishdf5;

2253:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);
2254:   PetscLogEventBegin(DMPLEX_SectionLoad,viewer,0,0,0);
2255:   if (ishdf5) {
2256: #if defined(PETSC_HAVE_HDF5)
2257:     DMPlexSectionLoad_HDF5_Internal(dm, viewer, sectiondm, globalToLocalPointSF, globalDofSF, localDofSF);
2258: #else
2259:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2260: #endif
2261:   }
2262:   PetscLogEventEnd(DMPLEX_SectionLoad,viewer,0,0,0);
2263:   return 0;
2264: }

2266: /*@
2267:   DMPlexGlobalVectorLoad - Loads on-disk vector data into a global vector

2269:   Collective on DM

2271:   Input Parameters:
2272: + dm        - The DM that represents the topology
2273: . viewer    - The PetscViewer that represents the on-disk vector data
2274: . sectiondm - The DM that contains the global section on which vec is defined
2275: . sf        - The SF that migrates the on-disk vector data into vec
2276: - vec       - The global vector to set values of

2278:   Level: advanced

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

2283:   Typical calling sequence
2284: $       DMCreate(PETSC_COMM_WORLD, &dm);
2285: $       DMSetType(dm, DMPLEX);
2286: $       PetscObjectSetName((PetscObject)dm, "topologydm_name");
2287: $       DMPlexTopologyLoad(dm, viewer, &sfX);
2288: $       DMClone(dm, &sectiondm);
2289: $       PetscObjectSetName((PetscObject)sectiondm, "sectiondm_name");
2290: $       DMPlexSectionLoad(dm, viewer, sectiondm, sfX, &gsf, NULL);
2291: $       DMGetGlobalVector(sectiondm, &vec);
2292: $       PetscObjectSetName((PetscObject)vec, "vec_name");
2293: $       DMPlexGlobalVectorLoad(dm, viewer, sectiondm, gsf, vec);
2294: $       DMRestoreGlobalVector(sectiondm, &vec);
2295: $       PetscSFDestroy(&gsf);
2296: $       PetscSFDestroy(&sfX);
2297: $       DMDestroy(&sectiondm);
2298: $       DMDestroy(&dm);

2300: .seealso: DMPlexTopologyLoad(), DMPlexSectionLoad(), DMPlexLocalVectorLoad(), DMPlexGlobalVectorView(), DMPlexLocalVectorView()
2301: @*/
2302: PetscErrorCode DMPlexGlobalVectorLoad(DM dm, PetscViewer viewer, DM sectiondm, PetscSF sf, Vec vec)
2303: {
2304:   PetscBool       ishdf5;

2311:   /* Check consistency */
2312:   {
2313:     PetscSection  section;
2314:     PetscBool     includesConstraints;
2315:     PetscInt      m, m1;

2317:     VecGetLocalSize(vec, &m1);
2318:     DMGetGlobalSection(sectiondm, &section);
2319:     PetscSectionGetIncludesConstraints(section, &includesConstraints);
2320:     if (includesConstraints) PetscSectionGetStorageSize(section, &m);
2321:     else PetscSectionGetConstrainedStorageSize(section, &m);
2323:   }
2324:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);
2325:   PetscLogEventBegin(DMPLEX_GlobalVectorLoad,viewer,0,0,0);
2326:   if (ishdf5) {
2327: #if defined(PETSC_HAVE_HDF5)
2328:     DMPlexVecLoad_HDF5_Internal(dm, viewer, sectiondm, sf, vec);
2329: #else
2330:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2331: #endif
2332:   }
2333:   PetscLogEventEnd(DMPLEX_GlobalVectorLoad,viewer,0,0,0);
2334:   return 0;
2335: }

2337: /*@
2338:   DMPlexLocalVectorLoad - Loads on-disk vector data into a local vector

2340:   Collective on DM

2342:   Input Parameters:
2343: + dm        - The DM that represents the topology
2344: . viewer    - The PetscViewer that represents the on-disk vector data
2345: . sectiondm - The DM that contains the local section on which vec is defined
2346: . sf        - The SF that migrates the on-disk vector data into vec
2347: - vec       - The local vector to set values of

2349:   Level: advanced

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

2354:   Typical calling sequence
2355: $       DMCreate(PETSC_COMM_WORLD, &dm);
2356: $       DMSetType(dm, DMPLEX);
2357: $       PetscObjectSetName((PetscObject)dm, "topologydm_name");
2358: $       DMPlexTopologyLoad(dm, viewer, &sfX);
2359: $       DMClone(dm, &sectiondm);
2360: $       PetscObjectSetName((PetscObject)sectiondm, "sectiondm_name");
2361: $       DMPlexSectionLoad(dm, viewer, sectiondm, sfX, NULL, &lsf);
2362: $       DMGetLocalVector(sectiondm, &vec);
2363: $       PetscObjectSetName((PetscObject)vec, "vec_name");
2364: $       DMPlexLocalVectorLoad(dm, viewer, sectiondm, lsf, vec);
2365: $       DMRestoreLocalVector(sectiondm, &vec);
2366: $       PetscSFDestroy(&lsf);
2367: $       PetscSFDestroy(&sfX);
2368: $       DMDestroy(&sectiondm);
2369: $       DMDestroy(&dm);

2371: .seealso: DMPlexTopologyLoad(), DMPlexSectionLoad(), DMPlexGlobalVectorLoad(), DMPlexGlobalVectorView(), DMPlexLocalVectorView()
2372: @*/
2373: PetscErrorCode DMPlexLocalVectorLoad(DM dm, PetscViewer viewer, DM sectiondm, PetscSF sf, Vec vec)
2374: {
2375:   PetscBool       ishdf5;

2382:   /* Check consistency */
2383:   {
2384:     PetscSection  section;
2385:     PetscBool     includesConstraints;
2386:     PetscInt      m, m1;

2388:     VecGetLocalSize(vec, &m1);
2389:     DMGetLocalSection(sectiondm, &section);
2390:     PetscSectionGetIncludesConstraints(section, &includesConstraints);
2391:     if (includesConstraints) PetscSectionGetStorageSize(section, &m);
2392:     else PetscSectionGetConstrainedStorageSize(section, &m);
2394:   }
2395:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);
2396:   PetscLogEventBegin(DMPLEX_LocalVectorLoad,viewer,0,0,0);
2397:   if (ishdf5) {
2398: #if defined(PETSC_HAVE_HDF5)
2399:     DMPlexVecLoad_HDF5_Internal(dm, viewer, sectiondm, sf, vec);
2400: #else
2401:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2402: #endif
2403:   }
2404:   PetscLogEventEnd(DMPLEX_LocalVectorLoad,viewer,0,0,0);
2405:   return 0;
2406: }

2408: PetscErrorCode DMDestroy_Plex(DM dm)
2409: {
2410:   DM_Plex       *mesh = (DM_Plex*) dm->data;

2412:   PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",NULL);
2413:   PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C", NULL);
2414:   PetscObjectComposeFunction((PetscObject)dm,"DMCreateNeumannOverlap_C", NULL);
2415:   PetscObjectComposeFunction((PetscObject)dm,"DMInterpolateSolution_C", NULL);
2416:   if (--mesh->refct > 0) return 0;
2417:   PetscSectionDestroy(&mesh->coneSection);
2418:   PetscFree(mesh->cones);
2419:   PetscFree(mesh->coneOrientations);
2420:   PetscSectionDestroy(&mesh->supportSection);
2421:   PetscSectionDestroy(&mesh->subdomainSection);
2422:   PetscFree(mesh->supports);
2423:   PetscFree(mesh->facesTmp);
2424:   PetscFree(mesh->tetgenOpts);
2425:   PetscFree(mesh->triangleOpts);
2426:   PetscFree(mesh->transformType);
2427:   PetscPartitionerDestroy(&mesh->partitioner);
2428:   DMLabelDestroy(&mesh->subpointMap);
2429:   ISDestroy(&mesh->subpointIS);
2430:   ISDestroy(&mesh->globalVertexNumbers);
2431:   ISDestroy(&mesh->globalCellNumbers);
2432:   PetscSectionDestroy(&mesh->anchorSection);
2433:   ISDestroy(&mesh->anchorIS);
2434:   PetscSectionDestroy(&mesh->parentSection);
2435:   PetscFree(mesh->parents);
2436:   PetscFree(mesh->childIDs);
2437:   PetscSectionDestroy(&mesh->childSection);
2438:   PetscFree(mesh->children);
2439:   DMDestroy(&mesh->referenceTree);
2440:   PetscGridHashDestroy(&mesh->lbox);
2441:   PetscFree(mesh->neighbors);
2442:   if (mesh->metricCtx) PetscFree(mesh->metricCtx);
2443:   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
2444:   PetscFree(mesh);
2445:   return 0;
2446: }

2448: PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
2449: {
2450:   PetscSection           sectionGlobal;
2451:   PetscInt               bs = -1, mbs;
2452:   PetscInt               localSize;
2453:   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
2454:   MatType                mtype;
2455:   ISLocalToGlobalMapping ltog;

2457:   MatInitializePackage();
2458:   mtype = dm->mattype;
2459:   DMGetGlobalSection(dm, &sectionGlobal);
2460:   /* PetscSectionGetStorageSize(sectionGlobal, &localSize); */
2461:   PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);
2462:   MatCreate(PetscObjectComm((PetscObject)dm), J);
2463:   MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);
2464:   MatSetType(*J, mtype);
2465:   MatSetFromOptions(*J);
2466:   MatGetBlockSize(*J, &mbs);
2467:   if (mbs > 1) bs = mbs;
2468:   PetscStrcmp(mtype, MATSHELL, &isShell);
2469:   PetscStrcmp(mtype, MATBAIJ, &isBlock);
2470:   PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);
2471:   PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);
2472:   PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);
2473:   PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);
2474:   PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);
2475:   PetscStrcmp(mtype, MATIS, &isMatIS);
2476:   if (!isShell) {
2477:     PetscBool fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
2478:     PetscInt  *dnz, *onz, *dnzu, *onzu, bsLocal[2], bsMinMax[2];
2479:     PetscInt  pStart, pEnd, p, dof, cdof;

2481:     DMGetLocalToGlobalMapping(dm,&ltog);
2482:     PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);
2483:     for (p = pStart; p < pEnd; ++p) {
2484:       PetscInt bdof;

2486:       PetscSectionGetDof(sectionGlobal, p, &dof);
2487:       PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);
2488:       dof  = dof < 0 ? -(dof+1) : dof;
2489:       bdof = cdof && (dof-cdof) ? 1 : dof;
2490:       if (dof) {
2491:         if (bs < 0)          {bs = bdof;}
2492:         else if (bs != bdof) {bs = 1; break;}
2493:       }
2494:     }
2495:     /* Must have same blocksize on all procs (some might have no points) */
2496:     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs;
2497:     bsLocal[1] = bs;
2498:     PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);
2499:     if (bsMinMax[0] != bsMinMax[1]) bs = 1;
2500:     else bs = bsMinMax[0];
2501:     bs = PetscMax(1,bs);
2502:     MatSetLocalToGlobalMapping(*J,ltog,ltog);
2503:     if (dm->prealloc_skip) { // User will likely use MatSetPreallocationCOO(), but still set structural parameters
2504:       MatSetBlockSize(*J, bs);
2505:       MatSetUp(*J);
2506:     } else {
2507:       PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);
2508:       DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);
2509:       PetscFree4(dnz, onz, dnzu, onzu);
2510:     }
2511:   }
2512:   MatSetDM(*J, dm);
2513:   return 0;
2514: }

2516: /*@
2517:   DMPlexGetSubdomainSection - Returns the section associated with the subdomain

2519:   Not collective

2521:   Input Parameter:
2522: . mesh - The DMPlex

2524:   Output Parameters:
2525: . subsection - The subdomain section

2527:   Level: developer

2529: .seealso:
2530: @*/
2531: PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
2532: {
2533:   DM_Plex       *mesh = (DM_Plex*) dm->data;

2536:   if (!mesh->subdomainSection) {
2537:     PetscSection section;
2538:     PetscSF      sf;

2540:     PetscSFCreate(PETSC_COMM_SELF,&sf);
2541:     DMGetLocalSection(dm,&section);
2542:     PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);
2543:     PetscSFDestroy(&sf);
2544:   }
2545:   *subsection = mesh->subdomainSection;
2546:   return 0;
2547: }

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

2552:   Not collective

2554:   Input Parameter:
2555: . mesh - The DMPlex

2557:   Output Parameters:
2558: + pStart - The first mesh point
2559: - pEnd   - The upper bound for mesh points

2561:   Level: beginner

2563: .seealso: DMPlexCreate(), DMPlexSetChart()
2564: @*/
2565: PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
2566: {
2567:   DM_Plex       *mesh = (DM_Plex*) dm->data;

2570:   PetscSectionGetChart(mesh->coneSection, pStart, pEnd);
2571:   return 0;
2572: }

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

2577:   Not collective

2579:   Input Parameters:
2580: + mesh - The DMPlex
2581: . pStart - The first mesh point
2582: - pEnd   - The upper bound for mesh points

2584:   Output Parameters:

2586:   Level: beginner

2588: .seealso: DMPlexCreate(), DMPlexGetChart()
2589: @*/
2590: PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
2591: {
2592:   DM_Plex       *mesh = (DM_Plex*) dm->data;

2595:   PetscSectionSetChart(mesh->coneSection, pStart, pEnd);
2596:   PetscSectionSetChart(mesh->supportSection, pStart, pEnd);
2597:   return 0;
2598: }

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

2603:   Not collective

2605:   Input Parameters:
2606: + mesh - The DMPlex
2607: - p - The point, which must lie in the chart set with DMPlexSetChart()

2609:   Output Parameter:
2610: . size - The cone size for point p

2612:   Level: beginner

2614: .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
2615: @*/
2616: PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
2617: {
2618:   DM_Plex       *mesh = (DM_Plex*) dm->data;

2622:   PetscSectionGetDof(mesh->coneSection, p, size);
2623:   return 0;
2624: }

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

2629:   Not collective

2631:   Input Parameters:
2632: + mesh - The DMPlex
2633: . p - The point, which must lie in the chart set with DMPlexSetChart()
2634: - size - The cone size for point p

2636:   Output Parameter:

2638:   Note:
2639:   This should be called after DMPlexSetChart().

2641:   Level: beginner

2643: .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
2644: @*/
2645: PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
2646: {
2647:   DM_Plex       *mesh = (DM_Plex*) dm->data;

2650:   PetscSectionSetDof(mesh->coneSection, p, size);
2651:   return 0;
2652: }

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

2657:   Not collective

2659:   Input Parameters:
2660: + mesh - The DMPlex
2661: . p - The point, which must lie in the chart set with DMPlexSetChart()
2662: - size - The additional cone size for point p

2664:   Output Parameter:

2666:   Note:
2667:   This should be called after DMPlexSetChart().

2669:   Level: beginner

2671: .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
2672: @*/
2673: PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
2674: {
2675:   DM_Plex       *mesh = (DM_Plex*) dm->data;
2677:   PetscSectionAddDof(mesh->coneSection, p, size);
2678:   return 0;
2679: }

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

2684:   Not collective

2686:   Input Parameters:
2687: + dm - The DMPlex
2688: - p - The point, which must lie in the chart set with DMPlexSetChart()

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

2693:   Level: beginner

2695:   Fortran Notes:
2696:   Since it returns an array, this routine is only available in Fortran 90, and you must
2697:   include petsc.h90 in your code.
2698:   You must also call DMPlexRestoreCone() after you finish using the returned array.
2699:   DMPlexRestoreCone() is not needed/available in C.

2701: .seealso: DMPlexGetConeSize(), DMPlexSetCone(), DMPlexGetConeTuple(), DMPlexSetChart()
2702: @*/
2703: PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
2704: {
2705:   DM_Plex       *mesh = (DM_Plex*) dm->data;
2706:   PetscInt       off;

2710:   PetscSectionGetOffset(mesh->coneSection, p, &off);
2711:   *cone = &mesh->cones[off];
2712:   return 0;
2713: }

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

2718:   Not collective

2720:   Input Parameters:
2721: + dm - The DMPlex
2722: - p - The IS of points, which must lie in the chart set with DMPlexSetChart()

2724:   Output Parameters:
2725: + pConesSection - PetscSection describing the layout of pCones
2726: - pCones - An array of points which are on the in-edges for the point set p

2728:   Level: intermediate

2730: .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeRecursive(), DMPlexSetChart()
2731: @*/
2732: PetscErrorCode DMPlexGetConeTuple(DM dm, IS p, PetscSection *pConesSection, IS *pCones)
2733: {
2734:   PetscSection        cs, newcs;
2735:   PetscInt            *cones;
2736:   PetscInt            *newarr=NULL;
2737:   PetscInt            n;

2739:   DMPlexGetCones(dm, &cones);
2740:   DMPlexGetConeSection(dm, &cs);
2741:   PetscSectionExtractDofsFromArray(cs, MPIU_INT, cones, p, &newcs, pCones ? ((void**)&newarr) : NULL);
2742:   if (pConesSection) *pConesSection = newcs;
2743:   if (pCones) {
2744:     PetscSectionGetStorageSize(newcs, &n);
2745:     ISCreateGeneral(PetscObjectComm((PetscObject)p), n, newarr, PETSC_OWN_POINTER, pCones);
2746:   }
2747:   return 0;
2748: }

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

2753:   Not collective

2755:   Input Parameters:
2756: + dm - The DMPlex
2757: - points - The IS of points, which must lie in the chart set with DMPlexSetChart()

2759:   Output Parameter:
2760: . expandedPoints - An array of vertices recursively expanded from input points

2762:   Level: advanced

2764:   Notes:
2765:   Like DMPlexGetConeRecursive but returns only the 0-depth IS (i.e. vertices only) and no sections.
2766:   There is no corresponding Restore function, just call ISDestroy() on the returned IS to deallocate.

2768: .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexGetConeRecursive(), DMPlexRestoreConeRecursive(), DMPlexGetDepth()
2769: @*/
2770: PetscErrorCode DMPlexGetConeRecursiveVertices(DM dm, IS points, IS *expandedPoints)
2771: {
2772:   IS                  *expandedPointsAll;
2773:   PetscInt            depth;

2778:   DMPlexGetConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);
2779:   *expandedPoints = expandedPointsAll[0];
2780:   PetscObjectReference((PetscObject)expandedPointsAll[0]);
2781:   DMPlexRestoreConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);
2782:   return 0;
2783: }

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

2788:   Not collective

2790:   Input Parameters:
2791: + dm - The DMPlex
2792: - points - The IS of points, which must lie in the chart set with DMPlexSetChart()

2794:   Output Parameters:
2795: + depth - (optional) Size of the output arrays, equal to DMPlex depth, returned by DMPlexGetDepth()
2796: . expandedPoints - (optional) An array of index sets with recursively expanded cones
2797: - sections - (optional) An array of sections which describe mappings from points to their cone points

2799:   Level: advanced

2801:   Notes:
2802:   Like DMPlexGetConeTuple() but recursive.

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

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

2811: .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexRestoreConeRecursive(), DMPlexGetConeRecursiveVertices(), DMPlexGetDepth()
2812: @*/
2813: PetscErrorCode DMPlexGetConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
2814: {
2815:   const PetscInt      *arr0=NULL, *cone=NULL;
2816:   PetscInt            *arr=NULL, *newarr=NULL;
2817:   PetscInt            d, depth_, i, n, newn, cn, co, start, end;
2818:   IS                  *expandedPoints_;
2819:   PetscSection        *sections_;

2826:   ISGetLocalSize(points, &n);
2827:   ISGetIndices(points, &arr0);
2828:   DMPlexGetDepth(dm, &depth_);
2829:   PetscCalloc1(depth_, &expandedPoints_);
2830:   PetscCalloc1(depth_, &sections_);
2831:   arr = (PetscInt*) arr0; /* this is ok because first generation of arr is not modified */
2832:   for (d=depth_-1; d>=0; d--) {
2833:     PetscSectionCreate(PETSC_COMM_SELF, &sections_[d]);
2834:     PetscSectionSetChart(sections_[d], 0, n);
2835:     for (i=0; i<n; i++) {
2836:       DMPlexGetDepthStratum(dm, d+1, &start, &end);
2837:       if (arr[i] >= start && arr[i] < end) {
2838:         DMPlexGetConeSize(dm, arr[i], &cn);
2839:         PetscSectionSetDof(sections_[d], i, cn);
2840:       } else {
2841:         PetscSectionSetDof(sections_[d], i, 1);
2842:       }
2843:     }
2844:     PetscSectionSetUp(sections_[d]);
2845:     PetscSectionGetStorageSize(sections_[d], &newn);
2846:     PetscMalloc1(newn, &newarr);
2847:     for (i=0; i<n; i++) {
2848:       PetscSectionGetDof(sections_[d], i, &cn);
2849:       PetscSectionGetOffset(sections_[d], i, &co);
2850:       if (cn > 1) {
2851:         DMPlexGetCone(dm, arr[i], &cone);
2852:         PetscMemcpy(&newarr[co], cone, cn*sizeof(PetscInt));
2853:       } else {
2854:         newarr[co] = arr[i];
2855:       }
2856:     }
2857:     ISCreateGeneral(PETSC_COMM_SELF, newn, newarr, PETSC_OWN_POINTER, &expandedPoints_[d]);
2858:     arr = newarr;
2859:     n = newn;
2860:   }
2861:   ISRestoreIndices(points, &arr0);
2862:   *depth = depth_;
2863:   if (expandedPoints) *expandedPoints = expandedPoints_;
2864:   else {
2865:     for (d=0; d<depth_; d++) ISDestroy(&expandedPoints_[d]);
2866:     PetscFree(expandedPoints_);
2867:   }
2868:   if (sections) *sections = sections_;
2869:   else {
2870:     for (d=0; d<depth_; d++) PetscSectionDestroy(&sections_[d]);
2871:     PetscFree(sections_);
2872:   }
2873:   return 0;
2874: }

2876: /*@
2877:   DMPlexRestoreConeRecursive - Deallocates arrays created by DMPlexGetConeRecursive

2879:   Not collective

2881:   Input Parameters:
2882: + dm - The DMPlex
2883: - points - The IS of points, which must lie in the chart set with DMPlexSetChart()

2885:   Output Parameters:
2886: + depth - (optional) Size of the output arrays, equal to DMPlex depth, returned by DMPlexGetDepth()
2887: . expandedPoints - (optional) An array of recursively expanded cones
2888: - sections - (optional) An array of sections which describe mappings from points to their cone points

2890:   Level: advanced

2892:   Notes:
2893:   See DMPlexGetConeRecursive() for details.

2895: .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexGetConeRecursive(), DMPlexGetConeRecursiveVertices(), DMPlexGetDepth()
2896: @*/
2897: PetscErrorCode DMPlexRestoreConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
2898: {
2899:   PetscInt            d, depth_;

2901:   DMPlexGetDepth(dm, &depth_);
2903:   if (depth) *depth = 0;
2904:   if (expandedPoints) {
2905:     for (d=0; d<depth_; d++) ISDestroy(&((*expandedPoints)[d]));
2906:     PetscFree(*expandedPoints);
2907:   }
2908:   if (sections)  {
2909:     for (d=0; d<depth_; d++) PetscSectionDestroy(&((*sections)[d]));
2910:     PetscFree(*sections);
2911:   }
2912:   return 0;
2913: }

2915: /*@
2916:   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

2918:   Not collective

2920:   Input Parameters:
2921: + mesh - The DMPlex
2922: . p - The point, which must lie in the chart set with DMPlexSetChart()
2923: - cone - An array of points which are on the in-edges for point p

2925:   Output Parameter:

2927:   Note:
2928:   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().

2930:   Level: beginner

2932: .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp(), DMPlexSetSupport(), DMPlexSetSupportSize()
2933: @*/
2934: PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
2935: {
2936:   DM_Plex       *mesh = (DM_Plex*) dm->data;
2937:   PetscInt       pStart, pEnd;
2938:   PetscInt       dof, off, c;

2941:   PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);
2942:   PetscSectionGetDof(mesh->coneSection, p, &dof);
2944:   PetscSectionGetOffset(mesh->coneSection, p, &off);
2946:   for (c = 0; c < dof; ++c) {
2948:     mesh->cones[off+c] = cone[c];
2949:   }
2950:   return 0;
2951: }

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

2956:   Not collective

2958:   Input Parameters:
2959: + mesh - The DMPlex
2960: - p - The point, which must lie in the chart set with DMPlexSetChart()

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

2966:   Level: beginner

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

2974:   Fortran Notes:
2975:   Since it returns an array, this routine is only available in Fortran 90, and you must
2976:   include petsc.h90 in your code.
2977:   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
2978:   DMPlexRestoreConeOrientation() is not needed/available in C.

2980: .seealso: DMPolytopeTypeComposeOrientation(), DMPolytopeTypeComposeOrientationInv(), DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
2981: @*/
2982: PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
2983: {
2984:   DM_Plex       *mesh = (DM_Plex*) dm->data;
2985:   PetscInt       off;

2988:   if (PetscDefined(USE_DEBUG)) {
2989:     PetscInt dof;
2990:     PetscSectionGetDof(mesh->coneSection, p, &dof);
2992:   }
2993:   PetscSectionGetOffset(mesh->coneSection, p, &off);

2995:   *coneOrientation = &mesh->coneOrientations[off];
2996:   return 0;
2997: }

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

3002:   Not collective

3004:   Input Parameters:
3005: + mesh - The DMPlex
3006: . p - The point, which must lie in the chart set with DMPlexSetChart()
3007: - coneOrientation - An array of orientations
3008:   Output Parameter:

3010:   Notes:
3011:   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().

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

3015:   Level: beginner

3017: .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
3018: @*/
3019: PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
3020: {
3021:   DM_Plex       *mesh = (DM_Plex*) dm->data;
3022:   PetscInt       pStart, pEnd;
3023:   PetscInt       dof, off, c;

3026:   PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);
3027:   PetscSectionGetDof(mesh->coneSection, p, &dof);
3029:   PetscSectionGetOffset(mesh->coneSection, p, &off);
3031:   for (c = 0; c < dof; ++c) {
3032:     PetscInt cdof, o = coneOrientation[c];

3034:     PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);
3036:     mesh->coneOrientations[off+c] = o;
3037:   }
3038:   return 0;
3039: }

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

3044:   Not collective

3046:   Input Parameters:
3047: + mesh - The DMPlex
3048: . p - The point, which must lie in the chart set with DMPlexSetChart()
3049: . conePos - The local index in the cone where the point should be put
3050: - conePoint - The mesh point to insert

3052:   Level: beginner

3054: .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
3055: @*/
3056: PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
3057: {
3058:   DM_Plex       *mesh = (DM_Plex*) dm->data;
3059:   PetscInt       pStart, pEnd;
3060:   PetscInt       dof, off;

3063:   PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);
3066:   PetscSectionGetDof(mesh->coneSection, p, &dof);
3067:   PetscSectionGetOffset(mesh->coneSection, p, &off);
3069:   mesh->cones[off+conePos] = conePoint;
3070:   return 0;
3071: }

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

3076:   Not collective

3078:   Input Parameters:
3079: + mesh - The DMPlex
3080: . p - The point, which must lie in the chart set with DMPlexSetChart()
3081: . conePos - The local index in the cone where the point should be put
3082: - coneOrientation - The point orientation to insert

3084:   Level: beginner

3086:   Notes:
3087:   The meaning of coneOrientation values is detailed in DMPlexGetConeOrientation().

3089: .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
3090: @*/
3091: PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
3092: {
3093:   DM_Plex       *mesh = (DM_Plex*) dm->data;
3094:   PetscInt       pStart, pEnd;
3095:   PetscInt       dof, off;

3098:   PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);
3100:   PetscSectionGetDof(mesh->coneSection, p, &dof);
3101:   PetscSectionGetOffset(mesh->coneSection, p, &off);
3103:   mesh->coneOrientations[off+conePos] = coneOrientation;
3104:   return 0;
3105: }

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

3110:   Not collective

3112:   Input Parameters:
3113: + mesh - The DMPlex
3114: - p - The point, which must lie in the chart set with DMPlexSetChart()

3116:   Output Parameter:
3117: . size - The support size for point p

3119:   Level: beginner

3121: .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
3122: @*/
3123: PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
3124: {
3125:   DM_Plex       *mesh = (DM_Plex*) dm->data;

3129:   PetscSectionGetDof(mesh->supportSection, p, size);
3130:   return 0;
3131: }

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

3136:   Not collective

3138:   Input Parameters:
3139: + mesh - The DMPlex
3140: . p - The point, which must lie in the chart set with DMPlexSetChart()
3141: - size - The support size for point p

3143:   Output Parameter:

3145:   Note:
3146:   This should be called after DMPlexSetChart().

3148:   Level: beginner

3150: .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
3151: @*/
3152: PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
3153: {
3154:   DM_Plex       *mesh = (DM_Plex*) dm->data;

3157:   PetscSectionSetDof(mesh->supportSection, p, size);
3158:   return 0;
3159: }

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

3164:   Not collective

3166:   Input Parameters:
3167: + mesh - The DMPlex
3168: - p - The point, which must lie in the chart set with DMPlexSetChart()

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

3173:   Level: beginner

3175:   Fortran Notes:
3176:   Since it returns an array, this routine is only available in Fortran 90, and you must
3177:   include petsc.h90 in your code.
3178:   You must also call DMPlexRestoreSupport() after you finish using the returned array.
3179:   DMPlexRestoreSupport() is not needed/available in C.

3181: .seealso: DMPlexGetSupportSize(), DMPlexSetSupport(), DMPlexGetCone(), DMPlexSetChart()
3182: @*/
3183: PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
3184: {
3185:   DM_Plex       *mesh = (DM_Plex*) dm->data;
3186:   PetscInt       off;

3190:   PetscSectionGetOffset(mesh->supportSection, p, &off);
3191:   *support = &mesh->supports[off];
3192:   return 0;
3193: }

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

3198:   Not collective

3200:   Input Parameters:
3201: + mesh - The DMPlex
3202: . p - The point, which must lie in the chart set with DMPlexSetChart()
3203: - support - An array of points which are on the out-edges for point p

3205:   Output Parameter:

3207:   Note:
3208:   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().

3210:   Level: beginner

3212: .seealso: DMPlexSetCone(), DMPlexSetConeSize(), DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
3213: @*/
3214: PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
3215: {
3216:   DM_Plex       *mesh = (DM_Plex*) dm->data;
3217:   PetscInt       pStart, pEnd;
3218:   PetscInt       dof, off, c;

3221:   PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);
3222:   PetscSectionGetDof(mesh->supportSection, p, &dof);
3224:   PetscSectionGetOffset(mesh->supportSection, p, &off);
3226:   for (c = 0; c < dof; ++c) {
3228:     mesh->supports[off+c] = support[c];
3229:   }
3230:   return 0;
3231: }

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

3236:   Not collective

3238:   Input Parameters:
3239: + mesh - The DMPlex
3240: . p - The point, which must lie in the chart set with DMPlexSetChart()
3241: . supportPos - The local index in the cone where the point should be put
3242: - supportPoint - The mesh point to insert

3244:   Level: beginner

3246: .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
3247: @*/
3248: PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
3249: {
3250:   DM_Plex       *mesh = (DM_Plex*) dm->data;
3251:   PetscInt       pStart, pEnd;
3252:   PetscInt       dof, off;

3255:   PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);
3256:   PetscSectionGetDof(mesh->supportSection, p, &dof);
3257:   PetscSectionGetOffset(mesh->supportSection, p, &off);
3261:   mesh->supports[off+supportPos] = supportPoint;
3262:   return 0;
3263: }

3265: /* Converts an orientation o in the current numbering to the previous scheme used in Plex */
3266: PetscInt DMPolytopeConvertNewOrientation_Internal(DMPolytopeType ct, PetscInt o)
3267: {
3268:   switch (ct) {
3269:     case DM_POLYTOPE_SEGMENT:
3270:       if (o == -1) return -2;
3271:       break;
3272:     case DM_POLYTOPE_TRIANGLE:
3273:       if (o == -3) return -1;
3274:       if (o == -2) return -3;
3275:       if (o == -1) return -2;
3276:       break;
3277:     case DM_POLYTOPE_QUADRILATERAL:
3278:       if (o == -4) return -2;
3279:       if (o == -3) return -1;
3280:       if (o == -2) return -4;
3281:       if (o == -1) return -3;
3282:       break;
3283:     default: return o;
3284:   }
3285:   return o;
3286: }

3288: /* Converts an orientation o in the previous scheme used in Plex to the current numbering */
3289: PetscInt DMPolytopeConvertOldOrientation_Internal(DMPolytopeType ct, PetscInt o)
3290: {
3291:   switch (ct) {
3292:     case DM_POLYTOPE_SEGMENT:
3293:       if ((o == -2) || (o == 1)) return -1;
3294:       if (o == -1) return 0;
3295:       break;
3296:     case DM_POLYTOPE_TRIANGLE:
3297:       if (o == -3) return -2;
3298:       if (o == -2) return -1;
3299:       if (o == -1) return -3;
3300:       break;
3301:     case DM_POLYTOPE_QUADRILATERAL:
3302:       if (o == -4) return -2;
3303:       if (o == -3) return -1;
3304:       if (o == -2) return -4;
3305:       if (o == -1) return -3;
3306:       break;
3307:     default: return o;
3308:   }
3309:   return o;
3310: }

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

3317:   DMPlexGetChart(dm, &pStart, &pEnd);
3318:   for (p = pStart; p < pEnd; ++p) {
3319:     const PetscInt *cone, *ornt;
3320:     PetscInt        coneSize, c;

3322:     DMPlexGetConeSize(dm, p, &coneSize);
3323:     DMPlexGetCone(dm, p, &cone);
3324:     DMPlexGetConeOrientation(dm, p, &ornt);
3325:     for (c = 0; c < coneSize; ++c) {
3326:       DMPolytopeType ct;
3327:       const PetscInt o = ornt[c];

3329:       DMPlexGetCellType(dm, cone[c], &ct);
3330:       switch (ct) {
3331:         case DM_POLYTOPE_SEGMENT:
3332:           if ((o == -2) || (o == 1)) DMPlexInsertConeOrientation(dm, p, c, -1);
3333:           if (o == -1) DMPlexInsertConeOrientation(dm, p, c, 0);
3334:           break;
3335:         case DM_POLYTOPE_TRIANGLE:
3336:           if (o == -3) DMPlexInsertConeOrientation(dm, p, c, -2);
3337:           if (o == -2) DMPlexInsertConeOrientation(dm, p, c, -1);
3338:           if (o == -1) DMPlexInsertConeOrientation(dm, p, c, -3);
3339:           break;
3340:         case DM_POLYTOPE_QUADRILATERAL:
3341:           if (o == -4) DMPlexInsertConeOrientation(dm, p, c, -2);
3342:           if (o == -3) DMPlexInsertConeOrientation(dm, p, c, -1);
3343:           if (o == -2) DMPlexInsertConeOrientation(dm, p, c, -4);
3344:           if (o == -1) DMPlexInsertConeOrientation(dm, p, c, -3);
3345:           break;
3346:         default: break;
3347:       }
3348:     }
3349:   }
3350:   return 0;
3351: }

3353: static PetscErrorCode DMPlexGetTransitiveClosure_Depth1_Private(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
3354: {
3355:   DMPolytopeType  ct = DM_POLYTOPE_UNKNOWN;
3356:   PetscInt       *closure;
3357:   const PetscInt *tmp = NULL, *tmpO = NULL;
3358:   PetscInt        off = 0, tmpSize, t;

3361:   if (ornt) {
3362:     DMPlexGetCellType(dm, p, &ct);
3363:     if (ct == DM_POLYTOPE_FV_GHOST || ct == DM_POLYTOPE_INTERIOR_GHOST || ct == DM_POLYTOPE_UNKNOWN) ct = DM_POLYTOPE_UNKNOWN;
3364:   }
3365:   if (*points) {
3366:     closure = *points;
3367:   } else {
3368:     PetscInt maxConeSize, maxSupportSize;
3369:     DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);
3370:     DMGetWorkArray(dm, 2*(PetscMax(maxConeSize, maxSupportSize)+1), MPIU_INT, &closure);
3371:   }
3372:   if (useCone) {
3373:     DMPlexGetConeSize(dm, p, &tmpSize);
3374:     DMPlexGetCone(dm, p, &tmp);
3375:     DMPlexGetConeOrientation(dm, p, &tmpO);
3376:   } else {
3377:     DMPlexGetSupportSize(dm, p, &tmpSize);
3378:     DMPlexGetSupport(dm, p, &tmp);
3379:   }
3380:   if (ct == DM_POLYTOPE_UNKNOWN) {
3381:     closure[off++] = p;
3382:     closure[off++] = 0;
3383:     for (t = 0; t < tmpSize; ++t) {
3384:       closure[off++] = tmp[t];
3385:       closure[off++] = tmpO ? tmpO[t] : 0;
3386:     }
3387:   } else {
3388:     const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, ornt);

3390:     /* We assume that cells with a valid type have faces with a valid type */
3391:     closure[off++] = p;
3392:     closure[off++] = ornt;
3393:     for (t = 0; t < tmpSize; ++t) {
3394:       DMPolytopeType ft;

3396:       DMPlexGetCellType(dm, tmp[t], &ft);
3397:       closure[off++] = tmp[arr[t]];
3398:       closure[off++] = tmpO ? DMPolytopeTypeComposeOrientation(ft, ornt, tmpO[t]) : 0;
3399:     }
3400:   }
3401:   if (numPoints) *numPoints = tmpSize+1;
3402:   if (points)    *points    = closure;
3403:   return 0;
3404: }

3406: /* We need a special tensor verison becasue we want to allow duplicate points in the endcaps for hybrid cells */
3407: static PetscErrorCode DMPlexTransitiveClosure_Tensor_Internal(DM dm, PetscInt point, DMPolytopeType ct, PetscInt o, PetscBool useCone, PetscInt *numPoints, PetscInt **points)
3408: {
3409:   const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, o);
3410:   const PetscInt *cone, *ornt;
3411:   PetscInt       *pts,  *closure = NULL;
3412:   DMPolytopeType  ft;
3413:   PetscInt        maxConeSize, maxSupportSize, coneSeries, supportSeries, maxSize;
3414:   PetscInt        dim, coneSize, c, d, clSize, cl;

3417:   DMGetDimension(dm, &dim);
3418:   DMPlexGetConeSize(dm, point, &coneSize);
3419:   DMPlexGetCone(dm, point, &cone);
3420:   DMPlexGetConeOrientation(dm, point, &ornt);
3421:   DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);
3422:   coneSeries    = (maxConeSize    > 1) ? ((PetscPowInt(maxConeSize,    dim+1)-1)/(maxConeSize-1))    : dim+1;
3423:   supportSeries = (maxSupportSize > 1) ? ((PetscPowInt(maxSupportSize, dim+1)-1)/(maxSupportSize-1)) : dim+1;
3424:   maxSize       = PetscMax(coneSeries, supportSeries);
3425:   if (*points) {pts  = *points;}
3426:   else         DMGetWorkArray(dm, 2*maxSize, MPIU_INT, &pts);
3427:   c    = 0;
3428:   pts[c++] = point;
3429:   pts[c++] = o;
3430:   DMPlexGetCellType(dm, cone[arr[0*2+0]], &ft);
3431:   DMPlexGetTransitiveClosure_Internal(dm, cone[arr[0*2+0]], DMPolytopeTypeComposeOrientation(ft, arr[0*2+1], ornt[0]), useCone, &clSize, &closure);
3432:   for (cl = 0; cl < clSize*2; cl += 2) {pts[c++] = closure[cl]; pts[c++] = closure[cl+1];}
3433:   DMPlexGetTransitiveClosure_Internal(dm, cone[arr[1*2+0]], DMPolytopeTypeComposeOrientation(ft, arr[1*2+1], ornt[1]), useCone, &clSize, &closure);
3434:   for (cl = 0; cl < clSize*2; cl += 2) {pts[c++] = closure[cl]; pts[c++] = closure[cl+1];}
3435:   DMPlexRestoreTransitiveClosure(dm, cone[0], useCone, &clSize, &closure);
3436:   for (d = 2; d < coneSize; ++d) {
3437:     DMPlexGetCellType(dm, cone[arr[d*2+0]], &ft);
3438:     pts[c++] = cone[arr[d*2+0]];
3439:     pts[c++] = DMPolytopeTypeComposeOrientation(ft, arr[d*2+1], ornt[d]);
3440:   }
3441:   if (dim >= 3) {
3442:     for (d = 2; d < coneSize; ++d) {
3443:       const PetscInt  fpoint = cone[arr[d*2+0]];
3444:       const PetscInt *fcone, *fornt;
3445:       PetscInt        fconeSize, fc, i;

3447:       DMPlexGetCellType(dm, fpoint, &ft);
3448:       const PetscInt *farr = DMPolytopeTypeGetArrangment(ft, DMPolytopeTypeComposeOrientation(ft, arr[d*2+1], ornt[d]));
3449:       DMPlexGetConeSize(dm, fpoint, &fconeSize);
3450:       DMPlexGetCone(dm, fpoint, &fcone);
3451:       DMPlexGetConeOrientation(dm, fpoint, &fornt);
3452:       for (fc = 0; fc < fconeSize; ++fc) {
3453:         const PetscInt cp = fcone[farr[fc*2+0]];
3454:         const PetscInt co = farr[fc*2+1];

3456:         for (i = 0; i < c; i += 2) if (pts[i] == cp) break;
3457:         if (i == c) {
3458:           DMPlexGetCellType(dm, cp, &ft);
3459:           pts[c++] = cp;
3460:           pts[c++] = DMPolytopeTypeComposeOrientation(ft, co, fornt[farr[fc*2+0]]);
3461:         }
3462:       }
3463:     }
3464:   }
3465:   *numPoints = c/2;
3466:   *points    = pts;
3467:   return 0;
3468: }

3470: PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
3471: {
3472:   DMPolytopeType ct;
3473:   PetscInt      *closure, *fifo;
3474:   PetscInt       closureSize = 0, fifoStart = 0, fifoSize = 0;
3475:   PetscInt       maxConeSize, maxSupportSize, coneSeries, supportSeries;
3476:   PetscInt       depth, maxSize;

3479:   DMPlexGetDepth(dm, &depth);
3480:   if (depth == 1) {
3481:     DMPlexGetTransitiveClosure_Depth1_Private(dm, p, ornt, useCone, numPoints, points);
3482:     return 0;
3483:   }
3484:   DMPlexGetCellType(dm, p, &ct);
3485:   if (ct == DM_POLYTOPE_FV_GHOST || ct == DM_POLYTOPE_INTERIOR_GHOST || ct == DM_POLYTOPE_UNKNOWN) ct = DM_POLYTOPE_UNKNOWN;
3486:   if (ct == DM_POLYTOPE_SEG_PRISM_TENSOR || ct == DM_POLYTOPE_TRI_PRISM_TENSOR || ct == DM_POLYTOPE_QUAD_PRISM_TENSOR) {
3487:     DMPlexTransitiveClosure_Tensor_Internal(dm, p, ct, ornt, useCone, numPoints, points);
3488:     return 0;
3489:   }
3490:   DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);
3491:   coneSeries    = (maxConeSize    > 1) ? ((PetscPowInt(maxConeSize,    depth+1)-1)/(maxConeSize-1))    : depth+1;
3492:   supportSeries = (maxSupportSize > 1) ? ((PetscPowInt(maxSupportSize, depth+1)-1)/(maxSupportSize-1)) : depth+1;
3493:   maxSize       = PetscMax(coneSeries, supportSeries);
3494:   DMGetWorkArray(dm, 3*maxSize, MPIU_INT, &fifo);
3495:   if (*points) {closure = *points;}
3496:   else         DMGetWorkArray(dm, 2*maxSize, MPIU_INT, &closure);
3497:   closure[closureSize++] = p;
3498:   closure[closureSize++] = ornt;
3499:   fifo[fifoSize++]       = p;
3500:   fifo[fifoSize++]       = ornt;
3501:   fifo[fifoSize++]       = ct;
3502:   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
3503:   while (fifoSize - fifoStart) {
3504:     const PetscInt       q    = fifo[fifoStart++];
3505:     const PetscInt       o    = fifo[fifoStart++];
3506:     const DMPolytopeType qt   = (DMPolytopeType) fifo[fifoStart++];
3507:     const PetscInt      *qarr = DMPolytopeTypeGetArrangment(qt, o);
3508:     const PetscInt      *tmp, *tmpO;
3509:     PetscInt             tmpSize, t;

3511:     if (PetscDefined(USE_DEBUG)) {
3512:       PetscInt nO = DMPolytopeTypeGetNumArrangments(qt)/2;
3514:     }
3515:     if (useCone) {
3516:       DMPlexGetConeSize(dm, q, &tmpSize);
3517:       DMPlexGetCone(dm, q, &tmp);
3518:       DMPlexGetConeOrientation(dm, q, &tmpO);
3519:     } else {
3520:       DMPlexGetSupportSize(dm, q, &tmpSize);
3521:       DMPlexGetSupport(dm, q, &tmp);
3522:       tmpO = NULL;
3523:     }
3524:     for (t = 0; t < tmpSize; ++t) {
3525:       const PetscInt ip = useCone && qarr ? qarr[t*2]   : t;
3526:       const PetscInt io = useCone && qarr ? qarr[t*2+1] : 0;
3527:       const PetscInt cp = tmp[ip];
3528:       DMPlexGetCellType(dm, cp, &ct);
3529:       const PetscInt co = tmpO ? DMPolytopeTypeComposeOrientation(ct, io, tmpO[ip]) : 0;
3530:       PetscInt       c;

3532:       /* Check for duplicate */
3533:       for (c = 0; c < closureSize; c += 2) {
3534:         if (closure[c] == cp) break;
3535:       }
3536:       if (c == closureSize) {
3537:         closure[closureSize++] = cp;
3538:         closure[closureSize++] = co;
3539:         fifo[fifoSize++]       = cp;
3540:         fifo[fifoSize++]       = co;
3541:         fifo[fifoSize++]       = ct;
3542:       }
3543:     }
3544:   }
3545:   DMRestoreWorkArray(dm, 3*maxSize, MPIU_INT, &fifo);
3546:   if (numPoints) *numPoints = closureSize/2;
3547:   if (points)    *points    = closure;
3548:   return 0;
3549: }

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

3554:   Not collective

3556:   Input Parameters:
3557: + dm      - The DMPlex
3558: . p       - The mesh point
3559: - useCone - PETSC_TRUE for the closure, otherwise return the star

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

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

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

3571:   Fortran Notes:
3572:   Since it returns an array, this routine is only available in Fortran 90, and you must include petsc.h90 in your code.

3574:   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.

3576:   Level: beginner

3578: .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
3579: @*/
3580: PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
3581: {
3586:   DMPlexGetTransitiveClosure_Internal(dm, p, 0, useCone, numPoints, points);
3587:   return 0;
3588: }

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

3593:   Not collective

3595:   Input Parameters:
3596: + dm        - The DMPlex
3597: . p         - The mesh point
3598: . useCone   - PETSC_TRUE for the closure, otherwise return the star
3599: . numPoints - The number of points in the closure, so points[] is of size 2*numPoints
3600: - points    - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]

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

3605:   Fortran Notes:
3606:   Since it returns an array, this routine is only available in Fortran 90, and you must include petsc.h90 in your code.

3608:   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.

3610:   Level: beginner

3612: .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
3613: @*/
3614: PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
3615: {
3618:   if (numPoints) *numPoints = 0;
3619:   DMRestoreWorkArray(dm, 0, MPIU_INT, points);
3620:   return 0;
3621: }

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

3626:   Not collective

3628:   Input Parameter:
3629: . mesh - The DMPlex

3631:   Output Parameters:
3632: + maxConeSize - The maximum number of in-edges
3633: - maxSupportSize - The maximum number of out-edges

3635:   Level: beginner

3637: .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
3638: @*/
3639: PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
3640: {
3641:   DM_Plex *mesh = (DM_Plex*) dm->data;

3644:   if (maxConeSize) {
3645:     PetscSectionGetMaxDof(mesh->coneSection, maxConeSize);
3646:   }
3647:   if (maxSupportSize) {
3648:     PetscSectionGetMaxDof(mesh->supportSection, maxSupportSize);
3649:   }
3650:   return 0;
3651: }

3653: PetscErrorCode DMSetUp_Plex(DM dm)
3654: {
3655:   DM_Plex       *mesh = (DM_Plex*) dm->data;
3656:   PetscInt       size, maxSupportSize;

3659:   PetscSectionSetUp(mesh->coneSection);
3660:   PetscSectionGetStorageSize(mesh->coneSection, &size);
3661:   PetscMalloc1(size, &mesh->cones);
3662:   PetscCalloc1(size, &mesh->coneOrientations);
3663:   PetscLogObjectMemory((PetscObject) dm, size*2*sizeof(PetscInt));
3664:   PetscSectionGetMaxDof(mesh->supportSection, &maxSupportSize);
3665:   if (maxSupportSize) {
3666:     PetscSectionSetUp(mesh->supportSection);
3667:     PetscSectionGetStorageSize(mesh->supportSection, &size);
3668:     PetscMalloc1(size, &mesh->supports);
3669:     PetscLogObjectMemory((PetscObject) dm, size*sizeof(PetscInt));
3670:   }
3671:   return 0;
3672: }

3674: PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
3675: {
3676:   if (subdm) DMClone(dm, subdm);
3677:   DMCreateSectionSubDM(dm, numFields, fields, is, subdm);
3678:   if (subdm) {(*subdm)->useNatural = dm->useNatural;}
3679:   if (dm->useNatural && dm->sfMigration) {
3680:     PetscSF        sfMigrationInv,sfNatural;
3681:     PetscSection   section, sectionSeq;

3683:     (*subdm)->sfMigration = dm->sfMigration;
3684:     PetscObjectReference((PetscObject) dm->sfMigration);
3685:     DMGetLocalSection((*subdm), &section);
3686:     PetscSFCreateInverseSF((*subdm)->sfMigration, &sfMigrationInv);
3687:     PetscSectionCreate(PetscObjectComm((PetscObject) (*subdm)), &sectionSeq);
3688:     PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);

3690:     DMPlexCreateGlobalToNaturalSF(*subdm, sectionSeq, (*subdm)->sfMigration, &sfNatural);
3691:     (*subdm)->sfNatural = sfNatural;
3692:     PetscSectionDestroy(&sectionSeq);
3693:     PetscSFDestroy(&sfMigrationInv);
3694:   }
3695:   return 0;
3696: }

3698: PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm)
3699: {
3700:   PetscInt       i = 0;

3702:   DMClone(dms[0], superdm);
3703:   DMCreateSectionSuperDM(dms, len, is, superdm);
3704:   (*superdm)->useNatural = PETSC_FALSE;
3705:   for (i = 0; i < len; i++) {
3706:     if (dms[i]->useNatural && dms[i]->sfMigration) {
3707:       PetscSF        sfMigrationInv,sfNatural;
3708:       PetscSection   section, sectionSeq;

3710:       (*superdm)->sfMigration = dms[i]->sfMigration;
3711:       PetscObjectReference((PetscObject) dms[i]->sfMigration);
3712:       (*superdm)->useNatural = PETSC_TRUE;
3713:       DMGetLocalSection((*superdm), &section);
3714:       PetscSFCreateInverseSF((*superdm)->sfMigration, &sfMigrationInv);
3715:       PetscSectionCreate(PetscObjectComm((PetscObject) (*superdm)), &sectionSeq);
3716:       PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);

3718:       DMPlexCreateGlobalToNaturalSF(*superdm, sectionSeq, (*superdm)->sfMigration, &sfNatural);
3719:       (*superdm)->sfNatural = sfNatural;
3720:       PetscSectionDestroy(&sectionSeq);
3721:       PetscSFDestroy(&sfMigrationInv);
3722:       break;
3723:     }
3724:   }
3725:   return 0;
3726: }

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

3731:   Not collective

3733:   Input Parameter:
3734: . mesh - The DMPlex

3736:   Output Parameter:

3738:   Note:
3739:   This should be called after all calls to DMPlexSetCone()

3741:   Level: beginner

3743: .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
3744: @*/
3745: PetscErrorCode DMPlexSymmetrize(DM dm)
3746: {
3747:   DM_Plex       *mesh = (DM_Plex*) dm->data;
3748:   PetscInt      *offsets;
3749:   PetscInt       supportSize;
3750:   PetscInt       pStart, pEnd, p;

3754:   PetscLogEventBegin(DMPLEX_Symmetrize,dm,0,0,0);
3755:   /* Calculate support sizes */
3756:   DMPlexGetChart(dm, &pStart, &pEnd);
3757:   for (p = pStart; p < pEnd; ++p) {
3758:     PetscInt dof, off, c;

3760:     PetscSectionGetDof(mesh->coneSection, p, &dof);
3761:     PetscSectionGetOffset(mesh->coneSection, p, &off);
3762:     for (c = off; c < off+dof; ++c) {
3763:       PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);
3764:     }
3765:   }
3766:   PetscSectionSetUp(mesh->supportSection);
3767:   /* Calculate supports */
3768:   PetscSectionGetStorageSize(mesh->supportSection, &supportSize);
3769:   PetscMalloc1(supportSize, &mesh->supports);
3770:   PetscCalloc1(pEnd - pStart, &offsets);
3771:   for (p = pStart; p < pEnd; ++p) {
3772:     PetscInt dof, off, c;

3774:     PetscSectionGetDof(mesh->coneSection, p, &dof);
3775:     PetscSectionGetOffset(mesh->coneSection, p, &off);
3776:     for (c = off; c < off+dof; ++c) {
3777:       const PetscInt q = mesh->cones[c];
3778:       PetscInt       offS;

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

3782:       mesh->supports[offS+offsets[q]] = p;
3783:       ++offsets[q];
3784:     }
3785:   }
3786:   PetscFree(offsets);
3787:   PetscLogEventEnd(DMPLEX_Symmetrize,dm,0,0,0);
3788:   return 0;
3789: }

3791: static PetscErrorCode DMPlexCreateDepthStratum(DM dm, DMLabel label, PetscInt depth, PetscInt pStart, PetscInt pEnd)
3792: {
3793:   IS             stratumIS;

3795:   if (pStart >= pEnd) return 0;
3796:   if (PetscDefined(USE_DEBUG)) {
3797:     PetscInt  qStart, qEnd, numLevels, level;
3798:     PetscBool overlap = PETSC_FALSE;
3799:     DMLabelGetNumValues(label, &numLevels);
3800:     for (level = 0; level < numLevels; level++) {
3801:       DMLabelGetStratumBounds(label, level, &qStart, &qEnd);
3802:       if ((pStart >= qStart && pStart < qEnd) || (pEnd > qStart && pEnd <= qEnd)) {overlap = PETSC_TRUE; break;}
3803:     }
3805:   }
3806:   ISCreateStride(PETSC_COMM_SELF, pEnd-pStart, pStart, 1, &stratumIS);
3807:   DMLabelSetStratumIS(label, depth, stratumIS);
3808:   ISDestroy(&stratumIS);
3809:   return 0;
3810: }

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

3818:   Collective on dm

3820:   Input Parameter:
3821: . mesh - The DMPlex

3823:   Output Parameter:

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

3832:   The depth of a point is calculated by executing a breadth-first search (BFS) on the DAG. This could produce surprising results
3833:   if run on a partially interpolated mesh, meaning one that had some edges and faces, but not others. For example, suppose that
3834:   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
3835:   to interpolate only that one (e0), so that
3836: $  cone(c0) = {e0, v2}
3837: $  cone(e0) = {v0, v1}
3838:   If DMPlexStratify() is run on this mesh, it will give depths
3839: $  depth 0 = {v0, v1, v2}
3840: $  depth 1 = {e0, c0}
3841:   where the triangle has been given depth 1, instead of 2, because it is reachable from vertex v2.

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

3845:   Level: beginner

3847: .seealso: DMPlexCreate(), DMPlexSymmetrize(), DMPlexComputeCellTypes()
3848: @*/
3849: PetscErrorCode DMPlexStratify(DM dm)
3850: {
3851:   DM_Plex       *mesh = (DM_Plex*) dm->data;
3852:   DMLabel        label;
3853:   PetscInt       pStart, pEnd, p;
3854:   PetscInt       numRoots = 0, numLeaves = 0;

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

3859:   /* Create depth label */
3860:   DMPlexGetChart(dm, &pStart, &pEnd);
3861:   DMCreateLabel(dm, "depth");
3862:   DMPlexGetDepthLabel(dm, &label);

3864:   {
3865:     /* Initialize roots and count leaves */
3866:     PetscInt sMin = PETSC_MAX_INT;
3867:     PetscInt sMax = PETSC_MIN_INT;
3868:     PetscInt coneSize, supportSize;

3870:     for (p = pStart; p < pEnd; ++p) {
3871:       DMPlexGetConeSize(dm, p, &coneSize);
3872:       DMPlexGetSupportSize(dm, p, &supportSize);
3873:       if (!coneSize && supportSize) {
3874:         sMin = PetscMin(p, sMin);
3875:         sMax = PetscMax(p, sMax);
3876:         ++numRoots;
3877:       } else if (!supportSize && coneSize) {
3878:         ++numLeaves;
3879:       } else if (!supportSize && !coneSize) {
3880:         /* Isolated points */
3881:         sMin = PetscMin(p, sMin);
3882:         sMax = PetscMax(p, sMax);
3883:       }
3884:     }
3885:     DMPlexCreateDepthStratum(dm, label, 0, sMin, sMax+1);
3886:   }

3888:   if (numRoots + numLeaves == (pEnd - pStart)) {
3889:     PetscInt sMin = PETSC_MAX_INT;
3890:     PetscInt sMax = PETSC_MIN_INT;
3891:     PetscInt coneSize, supportSize;

3893:     for (p = pStart; p < pEnd; ++p) {
3894:       DMPlexGetConeSize(dm, p, &coneSize);
3895:       DMPlexGetSupportSize(dm, p, &supportSize);
3896:       if (!supportSize && coneSize) {
3897:         sMin = PetscMin(p, sMin);
3898:         sMax = PetscMax(p, sMax);
3899:       }
3900:     }
3901:     DMPlexCreateDepthStratum(dm, label, 1, sMin, sMax+1);
3902:   } else {
3903:     PetscInt level = 0;
3904:     PetscInt qStart, qEnd, q;

3906:     DMLabelGetStratumBounds(label, level, &qStart, &qEnd);
3907:     while (qEnd > qStart) {
3908:       PetscInt sMin = PETSC_MAX_INT;
3909:       PetscInt sMax = PETSC_MIN_INT;

3911:       for (q = qStart; q < qEnd; ++q) {
3912:         const PetscInt *support;
3913:         PetscInt        supportSize, s;

3915:         DMPlexGetSupportSize(dm, q, &supportSize);
3916:         DMPlexGetSupport(dm, q, &support);
3917:         for (s = 0; s < supportSize; ++s) {
3918:           sMin = PetscMin(support[s], sMin);
3919:           sMax = PetscMax(support[s], sMax);
3920:         }
3921:       }
3922:       DMLabelGetNumValues(label, &level);
3923:       DMPlexCreateDepthStratum(dm, label, level, sMin, sMax+1);
3924:       DMLabelGetStratumBounds(label, level, &qStart, &qEnd);
3925:     }
3926:   }
3927:   { /* just in case there is an empty process */
3928:     PetscInt numValues, maxValues = 0, v;

3930:     DMLabelGetNumValues(label, &numValues);
3931:     MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));
3932:     for (v = numValues; v < maxValues; v++) {
3933:       DMLabelAddStratum(label, v);
3934:     }
3935:   }
3936:   PetscObjectStateGet((PetscObject) label, &mesh->depthState);
3937:   PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);
3938:   return 0;
3939: }

3941: PetscErrorCode DMPlexComputeCellType_Internal(DM dm, PetscInt p, PetscInt pdepth, DMPolytopeType *pt)
3942: {
3943:   DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
3944:   PetscInt       dim, depth, pheight, coneSize;

3947:   DMGetDimension(dm, &dim);
3948:   DMPlexGetDepth(dm, &depth);
3949:   DMPlexGetConeSize(dm, p, &coneSize);
3950:   pheight = depth - pdepth;
3951:   if (depth <= 1) {
3952:     switch (pdepth) {
3953:       case 0: ct = DM_POLYTOPE_POINT;break;
3954:       case 1:
3955:         switch (coneSize) {
3956:           case 2: ct = DM_POLYTOPE_SEGMENT;break;
3957:           case 3: ct = DM_POLYTOPE_TRIANGLE;break;
3958:           case 4:
3959:           switch (dim) {
3960:             case 2: ct = DM_POLYTOPE_QUADRILATERAL;break;
3961:             case 3: ct = DM_POLYTOPE_TETRAHEDRON;break;
3962:             default: break;
3963:           }
3964:           break;
3965:         case 5: ct = DM_POLYTOPE_PYRAMID;break;
3966:         case 6: ct = DM_POLYTOPE_TRI_PRISM_TENSOR;break;
3967:         case 8: ct = DM_POLYTOPE_HEXAHEDRON;break;
3968:         default: break;
3969:       }
3970:     }
3971:   } else {
3972:     if (pdepth == 0) {
3973:       ct = DM_POLYTOPE_POINT;
3974:     } else if (pheight == 0) {
3975:       switch (dim) {
3976:         case 1:
3977:           switch (coneSize) {
3978:             case 2: ct = DM_POLYTOPE_SEGMENT;break;
3979:             default: break;
3980:           }
3981:           break;
3982:         case 2:
3983:           switch (coneSize) {
3984:             case 3: ct = DM_POLYTOPE_TRIANGLE;break;
3985:             case 4: ct = DM_POLYTOPE_QUADRILATERAL;break;
3986:             default: break;
3987:           }
3988:           break;
3989:         case 3:
3990:           switch (coneSize) {
3991:             case 4: ct = DM_POLYTOPE_TETRAHEDRON;break;
3992:             case 5:
3993:             {
3994:               const PetscInt *cone;
3995:               PetscInt        faceConeSize;

3997:               DMPlexGetCone(dm, p, &cone);
3998:               DMPlexGetConeSize(dm, cone[0], &faceConeSize);
3999:               switch (faceConeSize) {
4000:                 case 3: ct = DM_POLYTOPE_TRI_PRISM_TENSOR;break;
4001:                 case 4: ct = DM_POLYTOPE_PYRAMID;break;
4002:               }
4003:             }
4004:             break;
4005:             case 6: ct = DM_POLYTOPE_HEXAHEDRON;break;
4006:             default: break;
4007:           }
4008:           break;
4009:         default: break;
4010:       }
4011:     } else if (pheight > 0) {
4012:       switch (coneSize) {
4013:         case 2: ct = DM_POLYTOPE_SEGMENT;break;
4014:         case 3: ct = DM_POLYTOPE_TRIANGLE;break;
4015:         case 4: ct = DM_POLYTOPE_QUADRILATERAL;break;
4016:         default: break;
4017:       }
4018:     }
4019:   }
4020:   *pt = ct;
4021:   return 0;
4022: }

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

4027:   Collective on dm

4029:   Input Parameter:
4030: . mesh - The DMPlex

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

4034:   Level: developer

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

4040: .seealso: DMPlexCreate(), DMPlexSymmetrize(), DMPlexStratify(), DMGetLabel(), DMCreateLabel()
4041: @*/
4042: PetscErrorCode DMPlexComputeCellTypes(DM dm)
4043: {
4044:   DM_Plex       *mesh;
4045:   DMLabel        ctLabel;
4046:   PetscInt       pStart, pEnd, p;

4049:   mesh = (DM_Plex *) dm->data;
4050:   DMCreateLabel(dm, "celltype");
4051:   DMPlexGetCellTypeLabel(dm, &ctLabel);
4052:   DMPlexGetChart(dm, &pStart, &pEnd);
4053:   for (p = pStart; p < pEnd; ++p) {
4054:     DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
4055:     PetscInt       pdepth;

4057:     DMPlexGetPointDepth(dm, p, &pdepth);
4058:     DMPlexComputeCellType_Internal(dm, p, pdepth, &ct);
4060:     DMLabelSetValue(ctLabel, p, ct);
4061:   }
4062:   PetscObjectStateGet((PetscObject) ctLabel, &mesh->celltypeState);
4063:   PetscObjectViewFromOptions((PetscObject) ctLabel, NULL, "-dm_plex_celltypes_view");
4064:   return 0;
4065: }

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

4070:   Not Collective

4072:   Input Parameters:
4073: + dm - The DMPlex object
4074: . numPoints - The number of input points for the join
4075: - points - The input points

4077:   Output Parameters:
4078: + numCoveredPoints - The number of points in the join
4079: - coveredPoints - The points in the join

4081:   Level: intermediate

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

4085:   Fortran Notes:
4086:   Since it returns an array, this routine is only available in Fortran 90, and you must
4087:   include petsc.h90 in your code.

4089:   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.

4091: .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
4092: @*/
4093: PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
4094: {
4095:   DM_Plex       *mesh = (DM_Plex*) dm->data;
4096:   PetscInt      *join[2];
4097:   PetscInt       joinSize, i = 0;
4098:   PetscInt       dof, off, p, c, m;
4099:   PetscInt       maxSupportSize;

4105:   PetscSectionGetMaxDof(mesh->supportSection, &maxSupportSize);
4106:   DMGetWorkArray(dm, maxSupportSize, MPIU_INT, &join[0]);
4107:   DMGetWorkArray(dm, maxSupportSize, MPIU_INT, &join[1]);
4108:   /* Copy in support of first point */
4109:   PetscSectionGetDof(mesh->supportSection, points[0], &dof);
4110:   PetscSectionGetOffset(mesh->supportSection, points[0], &off);
4111:   for (joinSize = 0; joinSize < dof; ++joinSize) {
4112:     join[i][joinSize] = mesh->supports[off+joinSize];
4113:   }
4114:   /* Check each successive support */
4115:   for (p = 1; p < numPoints; ++p) {
4116:     PetscInt newJoinSize = 0;

4118:     PetscSectionGetDof(mesh->supportSection, points[p], &dof);
4119:     PetscSectionGetOffset(mesh->supportSection, points[p], &off);
4120:     for (c = 0; c < dof; ++c) {
4121:       const PetscInt point = mesh->supports[off+c];

4123:       for (m = 0; m < joinSize; ++m) {
4124:         if (point == join[i][m]) {
4125:           join[1-i][newJoinSize++] = point;
4126:           break;
4127:         }
4128:       }
4129:     }
4130:     joinSize = newJoinSize;
4131:     i        = 1-i;
4132:   }
4133:   *numCoveredPoints = joinSize;
4134:   *coveredPoints    = join[i];
4135:   DMRestoreWorkArray(dm, maxSupportSize, MPIU_INT, &join[1-i]);
4136:   return 0;
4137: }

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

4142:   Not Collective

4144:   Input Parameters:
4145: + dm - The DMPlex object
4146: . numPoints - The number of input points for the join
4147: - points - The input points

4149:   Output Parameters:
4150: + numCoveredPoints - The number of points in the join
4151: - coveredPoints - The points in the join

4153:   Fortran Notes:
4154:   Since it returns an array, this routine is only available in Fortran 90, and you must
4155:   include petsc.h90 in your code.

4157:   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.

4159:   Level: intermediate

4161: .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
4162: @*/
4163: PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
4164: {
4169:   DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);
4170:   if (numCoveredPoints) *numCoveredPoints = 0;
4171:   return 0;
4172: }

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

4177:   Not Collective

4179:   Input Parameters:
4180: + dm - The DMPlex object
4181: . numPoints - The number of input points for the join
4182: - points - The input points

4184:   Output Parameters:
4185: + numCoveredPoints - The number of points in the join
4186: - coveredPoints - The points in the join

4188:   Fortran Notes:
4189:   Since it returns an array, this routine is only available in Fortran 90, and you must
4190:   include petsc.h90 in your code.

4192:   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.

4194:   Level: intermediate

4196: .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
4197: @*/
4198: PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
4199: {
4200:   PetscInt      *offsets, **closures;
4201:   PetscInt      *join[2];
4202:   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
4203:   PetscInt       p, d, c, m, ms;


4210:   DMPlexGetDepth(dm, &depth);
4211:   PetscCalloc1(numPoints, &closures);
4212:   DMGetWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);
4213:   DMPlexGetMaxSizes(dm, NULL, &ms);
4214:   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
4215:   DMGetWorkArray(dm, maxSize, MPIU_INT, &join[0]);
4216:   DMGetWorkArray(dm, maxSize, MPIU_INT, &join[1]);

4218:   for (p = 0; p < numPoints; ++p) {
4219:     PetscInt closureSize;

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

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

4227:       DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);
4228:       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
4229:         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
4230:           offsets[p*(depth+2)+d+1] = i;
4231:           break;
4232:         }
4233:       }
4234:       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
4235:     }
4237:   }
4238:   for (d = 0; d < depth+1; ++d) {
4239:     PetscInt dof;

4241:     /* Copy in support of first point */
4242:     dof = offsets[d+1] - offsets[d];
4243:     for (joinSize = 0; joinSize < dof; ++joinSize) {
4244:       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
4245:     }
4246:     /* Check each successive cone */
4247:     for (p = 1; p < numPoints && joinSize; ++p) {
4248:       PetscInt newJoinSize = 0;

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

4254:         for (m = 0; m < joinSize; ++m) {
4255:           if (point == join[i][m]) {
4256:             join[1-i][newJoinSize++] = point;
4257:             break;
4258:           }
4259:         }
4260:       }
4261:       joinSize = newJoinSize;
4262:       i        = 1-i;
4263:     }
4264:     if (joinSize) break;
4265:   }
4266:   *numCoveredPoints = joinSize;
4267:   *coveredPoints    = join[i];
4268:   for (p = 0; p < numPoints; ++p) {
4269:     DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);
4270:   }
4271:   PetscFree(closures);
4272:   DMRestoreWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);
4273:   DMRestoreWorkArray(dm, ms, MPIU_INT, &join[1-i]);
4274:   return 0;
4275: }

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

4280:   Not Collective

4282:   Input Parameters:
4283: + dm - The DMPlex object
4284: . numPoints - The number of input points for the meet
4285: - points - The input points

4287:   Output Parameters:
4288: + numCoveredPoints - The number of points in the meet
4289: - coveredPoints - The points in the meet

4291:   Level: intermediate

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

4295:   Fortran Notes:
4296:   Since it returns an array, this routine is only available in Fortran 90, and you must
4297:   include petsc.h90 in your code.

4299:   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.

4301: .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
4302: @*/
4303: PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
4304: {
4305:   DM_Plex       *mesh = (DM_Plex*) dm->data;
4306:   PetscInt      *meet[2];
4307:   PetscInt       meetSize, i = 0;
4308:   PetscInt       dof, off, p, c, m;
4309:   PetscInt       maxConeSize;

4315:   PetscSectionGetMaxDof(mesh->coneSection, &maxConeSize);
4316:   DMGetWorkArray(dm, maxConeSize, MPIU_INT, &meet[0]);
4317:   DMGetWorkArray(dm, maxConeSize, MPIU_INT, &meet[1]);
4318:   /* Copy in cone of first point */
4319:   PetscSectionGetDof(mesh->coneSection, points[0], &dof);
4320:   PetscSectionGetOffset(mesh->coneSection, points[0], &off);
4321:   for (meetSize = 0; meetSize < dof; ++meetSize) {
4322:     meet[i][meetSize] = mesh->cones[off+meetSize];
4323:   }
4324:   /* Check each successive cone */
4325:   for (p = 1; p < numPoints; ++p) {
4326:     PetscInt newMeetSize = 0;

4328:     PetscSectionGetDof(mesh->coneSection, points[p], &dof);
4329:     PetscSectionGetOffset(mesh->coneSection, points[p], &off);
4330:     for (c = 0; c < dof; ++c) {
4331:       const PetscInt point = mesh->cones[off+c];

4333:       for (m = 0; m < meetSize; ++m) {
4334:         if (point == meet[i][m]) {
4335:           meet[1-i][newMeetSize++] = point;
4336:           break;
4337:         }
4338:       }
4339:     }
4340:     meetSize = newMeetSize;
4341:     i        = 1-i;
4342:   }
4343:   *numCoveringPoints = meetSize;
4344:   *coveringPoints    = meet[i];
4345:   DMRestoreWorkArray(dm, maxConeSize, MPIU_INT, &meet[1-i]);
4346:   return 0;
4347: }

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

4352:   Not Collective

4354:   Input Parameters:
4355: + dm - The DMPlex object
4356: . numPoints - The number of input points for the meet
4357: - points - The input points

4359:   Output Parameters:
4360: + numCoveredPoints - The number of points in the meet
4361: - coveredPoints - The points in the meet

4363:   Level: intermediate

4365:   Fortran Notes:
4366:   Since it returns an array, this routine is only available in Fortran 90, and you must
4367:   include petsc.h90 in your code.

4369:   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.

4371: .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
4372: @*/
4373: PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
4374: {
4379:   DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);
4380:   if (numCoveredPoints) *numCoveredPoints = 0;
4381:   return 0;
4382: }

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

4387:   Not Collective

4389:   Input Parameters:
4390: + dm - The DMPlex object
4391: . numPoints - The number of input points for the meet
4392: - points - The input points

4394:   Output Parameters:
4395: + numCoveredPoints - The number of points in the meet
4396: - coveredPoints - The points in the meet

4398:   Level: intermediate

4400:   Fortran Notes:
4401:   Since it returns an array, this routine is only available in Fortran 90, and you must
4402:   include petsc.h90 in your code.

4404:   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.

4406: .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
4407: @*/
4408: PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
4409: {
4410:   PetscInt      *offsets, **closures;
4411:   PetscInt      *meet[2];
4412:   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
4413:   PetscInt       p, h, c, m, mc;


4420:   DMPlexGetDepth(dm, &height);
4421:   PetscMalloc1(numPoints, &closures);
4422:   DMGetWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);
4423:   DMPlexGetMaxSizes(dm, &mc, NULL);
4424:   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
4425:   DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[0]);
4426:   DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[1]);

4428:   for (p = 0; p < numPoints; ++p) {
4429:     PetscInt closureSize;

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

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

4437:       DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);
4438:       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
4439:         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
4440:           offsets[p*(height+2)+h+1] = i;
4441:           break;
4442:         }
4443:       }
4444:       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
4445:     }
4447:   }
4448:   for (h = 0; h < height+1; ++h) {
4449:     PetscInt dof;

4451:     /* Copy in cone of first point */
4452:     dof = offsets[h+1] - offsets[h];
4453:     for (meetSize = 0; meetSize < dof; ++meetSize) {
4454:       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
4455:     }
4456:     /* Check each successive cone */
4457:     for (p = 1; p < numPoints && meetSize; ++p) {
4458:       PetscInt newMeetSize = 0;

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

4464:         for (m = 0; m < meetSize; ++m) {
4465:           if (point == meet[i][m]) {
4466:             meet[1-i][newMeetSize++] = point;
4467:             break;
4468:           }
4469:         }
4470:       }
4471:       meetSize = newMeetSize;
4472:       i        = 1-i;
4473:     }
4474:     if (meetSize) break;
4475:   }
4476:   *numCoveredPoints = meetSize;
4477:   *coveredPoints    = meet[i];
4478:   for (p = 0; p < numPoints; ++p) {
4479:     DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);
4480:   }
4481:   PetscFree(closures);
4482:   DMRestoreWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);
4483:   DMRestoreWorkArray(dm, mc, MPIU_INT, &meet[1-i]);
4484:   return 0;
4485: }

4487: /*@C
4488:   DMPlexEqual - Determine if two DMs have the same topology

4490:   Not Collective

4492:   Input Parameters:
4493: + dmA - A DMPlex object
4494: - dmB - A DMPlex object

4496:   Output Parameters:
4497: . equal - PETSC_TRUE if the topologies are identical

4499:   Level: intermediate

4501:   Notes:
4502:   We are not solving graph isomorphism, so we do not permutation.

4504: .seealso: DMPlexGetCone()
4505: @*/
4506: PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
4507: {
4508:   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;


4514:   *equal = PETSC_FALSE;
4515:   DMPlexGetDepth(dmA, &depth);
4516:   DMPlexGetDepth(dmB, &depthB);
4517:   if (depth != depthB) return 0;
4518:   DMPlexGetChart(dmA, &pStart,  &pEnd);
4519:   DMPlexGetChart(dmB, &pStartB, &pEndB);
4520:   if ((pStart != pStartB) || (pEnd != pEndB)) return 0;
4521:   for (p = pStart; p < pEnd; ++p) {
4522:     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
4523:     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;

4525:     DMPlexGetConeSize(dmA, p, &coneSize);
4526:     DMPlexGetCone(dmA, p, &cone);
4527:     DMPlexGetConeOrientation(dmA, p, &ornt);
4528:     DMPlexGetConeSize(dmB, p, &coneSizeB);
4529:     DMPlexGetCone(dmB, p, &coneB);
4530:     DMPlexGetConeOrientation(dmB, p, &orntB);
4531:     if (coneSize != coneSizeB) return 0;
4532:     for (c = 0; c < coneSize; ++c) {
4533:       if (cone[c] != coneB[c]) return 0;
4534:       if (ornt[c] != orntB[c]) return 0;
4535:     }
4536:     DMPlexGetSupportSize(dmA, p, &supportSize);
4537:     DMPlexGetSupport(dmA, p, &support);
4538:     DMPlexGetSupportSize(dmB, p, &supportSizeB);
4539:     DMPlexGetSupport(dmB, p, &supportB);
4540:     if (supportSize != supportSizeB) return 0;
4541:     for (s = 0; s < supportSize; ++s) {
4542:       if (support[s] != supportB[s]) return 0;
4543:     }
4544:   }
4545:   *equal = PETSC_TRUE;
4546:   return 0;
4547: }

4549: /*@C
4550:   DMPlexGetNumFaceVertices - Returns the number of vertices on a face

4552:   Not Collective

4554:   Input Parameters:
4555: + dm         - The DMPlex
4556: . cellDim    - The cell dimension
4557: - numCorners - The number of vertices on a cell

4559:   Output Parameters:
4560: . numFaceVertices - The number of vertices on a face

4562:   Level: developer

4564:   Notes:
4565:   Of course this can only work for a restricted set of symmetric shapes

4567: .seealso: DMPlexGetCone()
4568: @*/
4569: PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
4570: {
4571:   MPI_Comm       comm;

4573:   PetscObjectGetComm((PetscObject)dm,&comm);
4575:   switch (cellDim) {
4576:   case 0:
4577:     *numFaceVertices = 0;
4578:     break;
4579:   case 1:
4580:     *numFaceVertices = 1;
4581:     break;
4582:   case 2:
4583:     switch (numCorners) {
4584:     case 3: /* triangle */
4585:       *numFaceVertices = 2; /* Edge has 2 vertices */
4586:       break;
4587:     case 4: /* quadrilateral */
4588:       *numFaceVertices = 2; /* Edge has 2 vertices */
4589:       break;
4590:     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
4591:       *numFaceVertices = 3; /* Edge has 3 vertices */
4592:       break;
4593:     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
4594:       *numFaceVertices = 3; /* Edge has 3 vertices */
4595:       break;
4596:     default:
4597:       SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
4598:     }
4599:     break;
4600:   case 3:
4601:     switch (numCorners) {
4602:     case 4: /* tetradehdron */
4603:       *numFaceVertices = 3; /* Face has 3 vertices */
4604:       break;
4605:     case 6: /* tet cohesive cells */
4606:       *numFaceVertices = 4; /* Face has 4 vertices */
4607:       break;
4608:     case 8: /* hexahedron */
4609:       *numFaceVertices = 4; /* Face has 4 vertices */
4610:       break;
4611:     case 9: /* tet cohesive Lagrange cells */
4612:       *numFaceVertices = 6; /* Face has 6 vertices */
4613:       break;
4614:     case 10: /* quadratic tetrahedron */
4615:       *numFaceVertices = 6; /* Face has 6 vertices */
4616:       break;
4617:     case 12: /* hex cohesive Lagrange cells */
4618:       *numFaceVertices = 6; /* Face has 6 vertices */
4619:       break;
4620:     case 18: /* quadratic tet cohesive Lagrange cells */
4621:       *numFaceVertices = 6; /* Face has 6 vertices */
4622:       break;
4623:     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
4624:       *numFaceVertices = 9; /* Face has 9 vertices */
4625:       break;
4626:     default:
4627:       SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
4628:     }
4629:     break;
4630:   default:
4631:     SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
4632:   }
4633:   return 0;
4634: }

4636: /*@
4637:   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point

4639:   Not Collective

4641:   Input Parameter:
4642: . dm    - The DMPlex object

4644:   Output Parameter:
4645: . depthLabel - The DMLabel recording point depth

4647:   Level: developer

4649: .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum(), DMPlexGetPointDepth(),
4650: @*/
4651: PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
4652: {
4655:   *depthLabel = dm->depthLabel;
4656:   return 0;
4657: }

4659: /*@
4660:   DMPlexGetDepth - Get the depth of the DAG representing this mesh

4662:   Not Collective

4664:   Input Parameter:
4665: . dm    - The DMPlex object

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

4670:   Level: developer

4672:   Notes:
4673:   This returns maximum of point depths over all points, i.e. maximum value of the label returned by DMPlexGetDepthLabel().
4674:   The point depth is described more in detail in DMPlexGetDepthStratum().
4675:   An empty mesh gives -1.

4677: .seealso: DMPlexGetDepthLabel(), DMPlexGetDepthStratum(), DMPlexGetPointDepth(), DMPlexSymmetrize()
4678: @*/
4679: PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
4680: {
4681:   DMLabel        label;
4682:   PetscInt       d = 0;

4686:   DMPlexGetDepthLabel(dm, &label);
4687:   if (label) DMLabelGetNumValues(label, &d);
4688:   *depth = d-1;
4689:   return 0;
4690: }

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

4695:   Not Collective

4697:   Input Parameters:
4698: + dm           - The DMPlex object
4699: - stratumValue - The requested depth

4701:   Output Parameters:
4702: + start - The first point at this depth
4703: - end   - One beyond the last point at this depth

4705:   Notes:
4706:   Depth indexing is related to topological dimension.  Depth stratum 0 contains the lowest topological dimension points,
4707:   often "vertices".  If the mesh is "interpolated" (see DMPlexInterpolate()), then depth stratum 1 contains the next
4708:   higher dimension, e.g., "edges".

4710:   Level: developer

4712: .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth(), DMPlexGetDepthLabel(), DMPlexGetPointDepth(), DMPlexSymmetrize(), DMPlexInterpolate()
4713: @*/
4714: PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
4715: {
4716:   DMLabel        label;
4717:   PetscInt       pStart, pEnd;

4722:   DMPlexGetChart(dm, &pStart, &pEnd);
4723:   if (pStart == pEnd) return 0;
4724:   if (stratumValue < 0) {
4725:     if (start) *start = pStart;
4726:     if (end)   *end   = pEnd;
4727:     return 0;
4728:   }
4729:   DMPlexGetDepthLabel(dm, &label);
4731:   DMLabelGetStratumBounds(label, stratumValue, start, end);
4732:   return 0;
4733: }

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

4738:   Not Collective

4740:   Input Parameters:
4741: + dm           - The DMPlex object
4742: - stratumValue - The requested height

4744:   Output Parameters:
4745: + start - The first point at this height
4746: - end   - One beyond the last point at this height

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

4753:   Level: developer

4755: .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth(), DMPlexGetPointHeight()
4756: @*/
4757: PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
4758: {
4759:   DMLabel        label;
4760:   PetscInt       depth, pStart, pEnd;

4765:   DMPlexGetChart(dm, &pStart, &pEnd);
4766:   if (pStart == pEnd) return 0;
4767:   if (stratumValue < 0) {
4768:     if (start) *start = pStart;
4769:     if (end)   *end   = pEnd;
4770:     return 0;
4771:   }
4772:   DMPlexGetDepthLabel(dm, &label);
4774:   DMLabelGetNumValues(label, &depth);
4775:   DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);
4776:   return 0;
4777: }

4779: /*@
4780:   DMPlexGetPointDepth - Get the depth of a given point

4782:   Not Collective

4784:   Input Parameters:
4785: + dm    - The DMPlex object
4786: - point - The point

4788:   Output Parameter:
4789: . depth - The depth of the point

4791:   Level: intermediate

4793: .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexGetPointHeight()
4794: @*/
4795: PetscErrorCode DMPlexGetPointDepth(DM dm, PetscInt point, PetscInt *depth)
4796: {
4799:   DMLabelGetValue(dm->depthLabel, point, depth);
4800:   return 0;
4801: }

4803: /*@
4804:   DMPlexGetPointHeight - Get the height of a given point

4806:   Not Collective

4808:   Input Parameters:
4809: + dm    - The DMPlex object
4810: - point - The point

4812:   Output Parameter:
4813: . height - The height of the point

4815:   Level: intermediate

4817: .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexGetPointDepth()
4818: @*/
4819: PetscErrorCode DMPlexGetPointHeight(DM dm, PetscInt point, PetscInt *height)
4820: {
4821:   PetscInt       n, pDepth;

4825:   DMLabelGetNumValues(dm->depthLabel, &n);
4826:   DMLabelGetValue(dm->depthLabel, point, &pDepth);
4827:   *height = n - 1 - pDepth;  /* DAG depth is n-1 */
4828:   return 0;
4829: }

4831: /*@
4832:   DMPlexGetCellTypeLabel - Get the DMLabel recording the polytope type of each cell

4834:   Not Collective

4836:   Input Parameter:
4837: . dm - The DMPlex object

4839:   Output Parameter:
4840: . celltypeLabel - The DMLabel recording cell polytope type

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

4845:   Level: developer

4847: .seealso: DMPlexGetCellType(), DMPlexGetDepthLabel(), DMCreateLabel()
4848: @*/
4849: PetscErrorCode DMPlexGetCellTypeLabel(DM dm, DMLabel *celltypeLabel)
4850: {
4853:   if (!dm->celltypeLabel) DMPlexComputeCellTypes(dm);
4854:   *celltypeLabel = dm->celltypeLabel;
4855:   return 0;
4856: }

4858: /*@
4859:   DMPlexGetCellType - Get the polytope type of a given cell

4861:   Not Collective

4863:   Input Parameters:
4864: + dm   - The DMPlex object
4865: - cell - The cell

4867:   Output Parameter:
4868: . celltype - The polytope type of the cell

4870:   Level: intermediate

4872: .seealso: DMPlexGetCellTypeLabel(), DMPlexGetDepthLabel(), DMPlexGetDepth()
4873: @*/
4874: PetscErrorCode DMPlexGetCellType(DM dm, PetscInt cell, DMPolytopeType *celltype)
4875: {
4876:   DMLabel        label;
4877:   PetscInt       ct;

4881:   DMPlexGetCellTypeLabel(dm, &label);
4882:   DMLabelGetValue(label, cell, &ct);
4884:   *celltype = (DMPolytopeType) ct;
4885:   return 0;
4886: }

4888: /*@
4889:   DMPlexSetCellType - Set the polytope type of a given cell

4891:   Not Collective

4893:   Input Parameters:
4894: + dm   - The DMPlex object
4895: . cell - The cell
4896: - celltype - The polytope type of the cell

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

4903:   Level: advanced

4905: .seealso: DMPlexGetCellTypeLabel(), DMPlexGetDepthLabel(), DMPlexGetDepth(), DMPlexComputeCellTypes(), DMCreateLabel()
4906: @*/
4907: PetscErrorCode DMPlexSetCellType(DM dm, PetscInt cell, DMPolytopeType celltype)
4908: {
4909:   DMLabel        label;

4912:   DMPlexGetCellTypeLabel(dm, &label);
4913:   DMLabelSetValue(label, cell, celltype);
4914:   return 0;
4915: }

4917: PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
4918: {
4919:   PetscSection   section, s;
4920:   Mat            m;
4921:   PetscInt       maxHeight;

4923:   DMClone(dm, cdm);
4924:   DMPlexGetMaxProjectionHeight(dm, &maxHeight);
4925:   DMPlexSetMaxProjectionHeight(*cdm, maxHeight);
4926:   PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);
4927:   DMSetLocalSection(*cdm, section);
4928:   PetscSectionDestroy(&section);
4929:   PetscSectionCreate(PETSC_COMM_SELF, &s);
4930:   MatCreate(PETSC_COMM_SELF, &m);
4931:   DMSetDefaultConstraints(*cdm, s, m, NULL);
4932:   PetscSectionDestroy(&s);
4933:   MatDestroy(&m);

4935:   DMSetNumFields(*cdm, 1);
4936:   DMCreateDS(*cdm);
4937:   return 0;
4938: }

4940: PetscErrorCode DMCreateCoordinateField_Plex(DM dm, DMField *field)
4941: {
4942:   Vec            coordsLocal;
4943:   DM             coordsDM;

4945:   *field = NULL;
4946:   DMGetCoordinatesLocal(dm,&coordsLocal);
4947:   DMGetCoordinateDM(dm,&coordsDM);
4948:   if (coordsLocal && coordsDM) {
4949:     DMFieldCreateDS(coordsDM, 0, coordsLocal, field);
4950:   }
4951:   return 0;
4952: }

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

4957:   Not Collective

4959:   Input Parameters:
4960: . dm        - The DMPlex object

4962:   Output Parameter:
4963: . section - The PetscSection object

4965:   Level: developer

4967: .seealso: DMPlexGetSupportSection(), DMPlexGetCones(), DMPlexGetConeOrientations()
4968: @*/
4969: PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
4970: {
4971:   DM_Plex *mesh = (DM_Plex*) dm->data;

4974:   if (section) *section = mesh->coneSection;
4975:   return 0;
4976: }

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

4981:   Not Collective

4983:   Input Parameters:
4984: . dm        - The DMPlex object

4986:   Output Parameter:
4987: . section - The PetscSection object

4989:   Level: developer

4991: .seealso: DMPlexGetConeSection()
4992: @*/
4993: PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
4994: {
4995:   DM_Plex *mesh = (DM_Plex*) dm->data;

4998:   if (section) *section = mesh->supportSection;
4999:   return 0;
5000: }

5002: /*@C
5003:   DMPlexGetCones - Return cone data

5005:   Not Collective

5007:   Input Parameters:
5008: . dm        - The DMPlex object

5010:   Output Parameter:
5011: . cones - The cone for each point

5013:   Level: developer

5015: .seealso: DMPlexGetConeSection()
5016: @*/
5017: PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
5018: {
5019:   DM_Plex *mesh = (DM_Plex*) dm->data;

5022:   if (cones) *cones = mesh->cones;
5023:   return 0;
5024: }

5026: /*@C
5027:   DMPlexGetConeOrientations - Return cone orientation data

5029:   Not Collective

5031:   Input Parameters:
5032: . dm        - The DMPlex object

5034:   Output Parameter:
5035: . coneOrientations - The array of cone orientations for all points

5037:   Level: developer

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

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

5044: .seealso: DMPlexGetConeSection(), DMPlexGetConeOrientation()
5045: @*/
5046: PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
5047: {
5048:   DM_Plex *mesh = (DM_Plex*) dm->data;

5051:   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
5052:   return 0;
5053: }

5055: /******************************** FEM Support **********************************/

5057: /*
5058:  Returns number of components and tensor degree for the field.  For interpolated meshes, line should be a point
5059:  representing a line in the section.
5060: */
5061: static PetscErrorCode PetscSectionFieldGetTensorDegree_Private(PetscSection section,PetscInt field,PetscInt line,PetscBool vertexchart,PetscInt *Nc,PetscInt *k)
5062: {
5064:   PetscSectionGetFieldComponents(section, field, Nc);
5065:   if (line < 0) {
5066:     *k = 0;
5067:     *Nc = 0;
5068:   } else if (vertexchart) {            /* If we only have a vertex chart, we must have degree k=1 */
5069:     *k = 1;
5070:   } else {                      /* Assume the full interpolated mesh is in the chart; lines in particular */
5071:     /* An order k SEM disc has k-1 dofs on an edge */
5072:     PetscSectionGetFieldDof(section, line, field, k);
5073:     *k = *k / *Nc + 1;
5074:   }
5075:   return 0;
5076: }

5078: /*@

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

5084:   Input Parameters:
5085: + dm      - The DM
5086: . point   - Either a cell (highest dim point) or an edge (dim 1 point), or PETSC_DETERMINE
5087: - section - The PetscSection to reorder, or NULL for the default section

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

5092:   Example:
5093:   A typical interpolated single-quad mesh might order points as
5094: .vb
5095:   [c0, v1, v2, v3, v4, e5, e6, e7, e8]

5097:   v4 -- e6 -- v3
5098:   |           |
5099:   e7    c0    e8
5100:   |           |
5101:   v1 -- e5 -- v2
5102: .ve

5104:   (There is no significance to the ordering described here.)  The default section for a Q3 quad might typically assign
5105:   dofs in the order of points, e.g.,
5106: .vb
5107:     c0 -> [0,1,2,3]
5108:     v1 -> [4]
5109:     ...
5110:     e5 -> [8, 9]
5111: .ve

5113:   which corresponds to the dofs
5114: .vb
5115:     6   10  11  7
5116:     13  2   3   15
5117:     12  0   1   14
5118:     4   8   9   5
5119: .ve

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

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

5131:   Level: developer

5133: .seealso: DMGetLocalSection(), PetscSectionSetClosurePermutation(), DMSetGlobalSection()
5134: @*/
5135: PetscErrorCode DMPlexSetClosurePermutationTensor(DM dm, PetscInt point, PetscSection section)
5136: {
5137:   DMLabel        label;
5138:   PetscInt       dim, depth = -1, eStart = -1, Nf;
5139:   PetscBool      vertexchart;

5141:   DMGetDimension(dm, &dim);
5142:   if (dim < 1) return 0;
5143:   if (point < 0) {
5144:     PetscInt sStart,sEnd;

5146:     DMPlexGetDepthStratum(dm, 1, &sStart, &sEnd);
5147:     point = sEnd-sStart ? sStart : point;
5148:   }
5149:   DMPlexGetDepthLabel(dm, &label);
5150:   if (point >= 0) DMLabelGetValue(label, point, &depth);
5151:   if (!section) DMGetLocalSection(dm, &section);
5152:   if (depth == 1) {eStart = point;}
5153:   else if  (depth == dim) {
5154:     const PetscInt *cone;

5156:     DMPlexGetCone(dm, point, &cone);
5157:     if (dim == 2) eStart = cone[0];
5158:     else if (dim == 3) {
5159:       const PetscInt *cone2;
5160:       DMPlexGetCone(dm, cone[0], &cone2);
5161:       eStart = cone2[0];
5162:     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Point %D of depth %D cannot be used to bootstrap spectral ordering for dim %D", point, depth, dim);
5164:   {                             /* Determine whether the chart covers all points or just vertices. */
5165:     PetscInt pStart,pEnd,cStart,cEnd;
5166:     DMPlexGetDepthStratum(dm,0,&pStart,&pEnd);
5167:     PetscSectionGetChart(section,&cStart,&cEnd);
5168:     if (pStart == cStart && pEnd == cEnd) vertexchart = PETSC_TRUE;      /* Only vertices are in the chart */
5169:     else if (cStart <= point && point < cEnd) vertexchart = PETSC_FALSE; /* Some interpolated points exist in the chart */
5170:     else vertexchart = PETSC_TRUE;                                       /* Some interpolated points are not in chart; assume dofs only at cells and vertices */
5171:   }
5172:   PetscSectionGetNumFields(section, &Nf);
5173:   for (PetscInt d=1; d<=dim; d++) {
5174:     PetscInt k, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
5175:     PetscInt *perm;

5177:     for (f = 0; f < Nf; ++f) {
5178:       PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);
5179:       size += PetscPowInt(k+1, d)*Nc;
5180:     }
5181:     PetscMalloc1(size, &perm);
5182:     for (f = 0; f < Nf; ++f) {
5183:       switch (d) {
5184:       case 1:
5185:         PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);
5186:         /*
5187:          Original ordering is [ edge of length k-1; vtx0; vtx1 ]
5188:          We want              [ vtx0; edge of length k-1; vtx1 ]
5189:          */
5190:         for (c=0; c<Nc; c++,offset++) perm[offset] = (k-1)*Nc + c + foffset;
5191:         for (i=0; i<k-1; i++) for (c=0; c<Nc; c++,offset++) perm[offset] = i*Nc + c + foffset;
5192:         for (c=0; c<Nc; c++,offset++) perm[offset] = k*Nc + c + foffset;
5193:         foffset = offset;
5194:         break;
5195:       case 2:
5196:         /* The original quad closure is oriented clockwise, {f, e_b, e_r, e_t, e_l, v_lb, v_rb, v_tr, v_tl} */
5197:         PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);
5198:         /* The SEM order is

5200:          v_lb, {e_b}, v_rb,
5201:          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
5202:          v_lt, reverse {e_t}, v_rt
5203:          */
5204:         {
5205:           const PetscInt of   = 0;
5206:           const PetscInt oeb  = of   + PetscSqr(k-1);
5207:           const PetscInt oer  = oeb  + (k-1);
5208:           const PetscInt oet  = oer  + (k-1);
5209:           const PetscInt oel  = oet  + (k-1);
5210:           const PetscInt ovlb = oel  + (k-1);
5211:           const PetscInt ovrb = ovlb + 1;
5212:           const PetscInt ovrt = ovrb + 1;
5213:           const PetscInt ovlt = ovrt + 1;
5214:           PetscInt       o;

5216:           /* bottom */
5217:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
5218:           for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
5219:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
5220:           /* middle */
5221:           for (i = 0; i < k-1; ++i) {
5222:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
5223:             for (o = of+(k-1)*i; o < of+(k-1)*(i+1); ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
5224:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
5225:           }
5226:           /* top */
5227:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
5228:           for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
5229:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
5230:           foffset = offset;
5231:         }
5232:         break;
5233:       case 3:
5234:         /* The original hex closure is

5236:          {c,
5237:          f_b, f_t, f_f, f_b, f_r, f_l,
5238:          e_bl, e_bb, e_br, e_bf,  e_tf, e_tr, e_tb, e_tl,  e_rf, e_lf, e_lb, e_rb,
5239:          v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
5240:          */
5241:         PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);
5242:         /* The SEM order is
5243:          Bottom Slice
5244:          v_blf, {e^{(k-1)-n}_bf}, v_brf,
5245:          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
5246:          v_blb, {e_bb}, v_brb,

5248:          Middle Slice (j)
5249:          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
5250:          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
5251:          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,

5253:          Top Slice
5254:          v_tlf, {e_tf}, v_trf,
5255:          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
5256:          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
5257:          */
5258:         {
5259:           const PetscInt oc    = 0;
5260:           const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
5261:           const PetscInt oft   = ofb   + PetscSqr(k-1);
5262:           const PetscInt off   = oft   + PetscSqr(k-1);
5263:           const PetscInt ofk   = off   + PetscSqr(k-1);
5264:           const PetscInt ofr   = ofk   + PetscSqr(k-1);
5265:           const PetscInt ofl   = ofr   + PetscSqr(k-1);
5266:           const PetscInt oebl  = ofl   + PetscSqr(k-1);
5267:           const PetscInt oebb  = oebl  + (k-1);
5268:           const PetscInt oebr  = oebb  + (k-1);
5269:           const PetscInt oebf  = oebr  + (k-1);
5270:           const PetscInt oetf  = oebf  + (k-1);
5271:           const PetscInt oetr  = oetf  + (k-1);
5272:           const PetscInt oetb  = oetr  + (k-1);
5273:           const PetscInt oetl  = oetb  + (k-1);
5274:           const PetscInt oerf  = oetl  + (k-1);
5275:           const PetscInt oelf  = oerf  + (k-1);
5276:           const PetscInt oelb  = oelf  + (k-1);
5277:           const PetscInt oerb  = oelb  + (k-1);
5278:           const PetscInt ovblf = oerb  + (k-1);
5279:           const PetscInt ovblb = ovblf + 1;
5280:           const PetscInt ovbrb = ovblb + 1;
5281:           const PetscInt ovbrf = ovbrb + 1;
5282:           const PetscInt ovtlf = ovbrf + 1;
5283:           const PetscInt ovtrf = ovtlf + 1;
5284:           const PetscInt ovtrb = ovtrf + 1;
5285:           const PetscInt ovtlb = ovtrb + 1;
5286:           PetscInt       o, n;

5288:           /* Bottom Slice */
5289:           /*   bottom */
5290:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
5291:           for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
5292:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
5293:           /*   middle */
5294:           for (i = 0; i < k-1; ++i) {
5295:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
5296:             for (n = 0; n < k-1; ++n) {o = ofb+n*(k-1)+i; for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;}
5297:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
5298:           }
5299:           /*   top */
5300:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
5301:           for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
5302:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;

5304:           /* Middle Slice */
5305:           for (j = 0; j < k-1; ++j) {
5306:             /*   bottom */
5307:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
5308:             for (o = off+j*(k-1); o < off+(j+1)*(k-1); ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
5309:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
5310:             /*   middle */
5311:             for (i = 0; i < k-1; ++i) {
5312:               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
5313:               for (n = 0; n < k-1; ++n) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oc+(j*(k-1)+i)*(k-1)+n)*Nc + c + foffset;
5314:               for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
5315:             }
5316:             /*   top */
5317:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
5318:             for (o = ofk+j*(k-1)+(k-2); o >= ofk+j*(k-1); --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
5319:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
5320:           }

5322:           /* Top Slice */
5323:           /*   bottom */
5324:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
5325:           for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
5326:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
5327:           /*   middle */
5328:           for (i = 0; i < k-1; ++i) {
5329:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
5330:             for (n = 0; n < k-1; ++n) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oft+i*(k-1)+n)*Nc + c + foffset;
5331:             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
5332:           }
5333:           /*   top */
5334:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
5335:           for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
5336:           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;

5338:           foffset = offset;
5339:         }
5340:         break;
5341:       default: SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", d);
5342:       }
5343:     }
5345:     /* Check permutation */
5346:     {
5347:       PetscInt *check;

5349:       PetscMalloc1(size, &check);
5351:       for (i = 0; i < size; ++i) check[perm[i]] = i;
5353:       PetscFree(check);
5354:     }
5355:     PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, d, size, PETSC_OWN_POINTER, perm);
5356:     if (d == dim) { // Add permutation for localized (in case this is a coordinate DM)
5357:       PetscInt *loc_perm;
5358:       PetscMalloc1(size*2, &loc_perm);
5359:       for (PetscInt i=0; i<size; i++) {
5360:         loc_perm[i] = perm[i];
5361:         loc_perm[size+i] = size + perm[i];
5362:       }
5363:       PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, d, size*2, PETSC_OWN_POINTER, loc_perm);
5364:     }
5365:   }
5366:   return 0;
5367: }

5369: PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
5370: {
5371:   PetscDS        prob;
5372:   PetscInt       depth, Nf, h;
5373:   DMLabel        label;

5376:   DMGetDS(dm, &prob);
5377:   Nf      = prob->Nf;
5378:   label   = dm->depthLabel;
5379:   *dspace = NULL;
5380:   if (field < Nf) {
5381:     PetscObject disc = prob->disc[field];

5383:     if (disc->classid == PETSCFE_CLASSID) {
5384:       PetscDualSpace dsp;

5386:       PetscFEGetDualSpace((PetscFE)disc,&dsp);
5387:       DMLabelGetNumValues(label,&depth);
5388:       DMLabelGetValue(label,point,&h);
5389:       h    = depth - 1 - h;
5390:       if (h) {
5391:         PetscDualSpaceGetHeightSubspace(dsp,h,dspace);
5392:       } else {
5393:         *dspace = dsp;
5394:       }
5395:     }
5396:   }
5397:   return 0;
5398: }

5400: static inline PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
5401: {
5402:   PetscScalar    *array, *vArray;
5403:   const PetscInt *cone, *coneO;
5404:   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;

5407:   PetscSectionGetChart(section, &pStart, &pEnd);
5408:   DMPlexGetConeSize(dm, point, &numPoints);
5409:   DMPlexGetCone(dm, point, &cone);
5410:   DMPlexGetConeOrientation(dm, point, &coneO);
5411:   if (!values || !*values) {
5412:     if ((point >= pStart) && (point < pEnd)) {
5413:       PetscInt dof;

5415:       PetscSectionGetDof(section, point, &dof);
5416:       size += dof;
5417:     }
5418:     for (p = 0; p < numPoints; ++p) {
5419:       const PetscInt cp = cone[p];
5420:       PetscInt       dof;

5422:       if ((cp < pStart) || (cp >= pEnd)) continue;
5423:       PetscSectionGetDof(section, cp, &dof);
5424:       size += dof;
5425:     }
5426:     if (!values) {
5427:       if (csize) *csize = size;
5428:       return 0;
5429:     }
5430:     DMGetWorkArray(dm, size, MPIU_SCALAR, &array);
5431:   } else {
5432:     array = *values;
5433:   }
5434:   size = 0;
5435:   VecGetArray(v, &vArray);
5436:   if ((point >= pStart) && (point < pEnd)) {
5437:     PetscInt     dof, off, d;
5438:     PetscScalar *varr;

5440:     PetscSectionGetDof(section, point, &dof);
5441:     PetscSectionGetOffset(section, point, &off);
5442:     varr = &vArray[off];
5443:     for (d = 0; d < dof; ++d, ++offset) {
5444:       array[offset] = varr[d];
5445:     }
5446:     size += dof;
5447:   }
5448:   for (p = 0; p < numPoints; ++p) {
5449:     const PetscInt cp = cone[p];
5450:     PetscInt       o  = coneO[p];
5451:     PetscInt       dof, off, d;
5452:     PetscScalar   *varr;

5454:     if ((cp < pStart) || (cp >= pEnd)) continue;
5455:     PetscSectionGetDof(section, cp, &dof);
5456:     PetscSectionGetOffset(section, cp, &off);
5457:     varr = &vArray[off];
5458:     if (o >= 0) {
5459:       for (d = 0; d < dof; ++d, ++offset) {
5460:         array[offset] = varr[d];
5461:       }
5462:     } else {
5463:       for (d = dof-1; d >= 0; --d, ++offset) {
5464:         array[offset] = varr[d];
5465:       }
5466:     }
5467:     size += dof;
5468:   }
5469:   VecRestoreArray(v, &vArray);
5470:   if (!*values) {
5471:     if (csize) *csize = size;
5472:     *values = array;
5473:   } else {
5475:     *csize = size;
5476:   }
5477:   return 0;
5478: }

5480: /* Compress out points not in the section */
5481: static inline PetscErrorCode CompressPoints_Private(PetscSection section, PetscInt *numPoints, PetscInt points[])
5482: {
5483:   const PetscInt np = *numPoints;
5484:   PetscInt       pStart, pEnd, p, q;

5486:   PetscSectionGetChart(section, &pStart, &pEnd);
5487:   for (p = 0, q = 0; p < np; ++p) {
5488:     const PetscInt r = points[p*2];
5489:     if ((r >= pStart) && (r < pEnd)) {
5490:       points[q*2]   = r;
5491:       points[q*2+1] = points[p*2+1];
5492:       ++q;
5493:     }
5494:   }
5495:   *numPoints = q;
5496:   return 0;
5497: }

5499: /* Compressed closure does not apply closure permutation */
5500: PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
5501: {
5502:   const PetscInt *cla = NULL;
5503:   PetscInt       np, *pts = NULL;

5506:   PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);
5507:   if (*clPoints) {
5508:     PetscInt dof, off;

5510:     PetscSectionGetDof(*clSec, point, &dof);
5511:     PetscSectionGetOffset(*clSec, point, &off);
5512:     ISGetIndices(*clPoints, &cla);
5513:     np   = dof/2;
5514:     pts  = (PetscInt *) &cla[off];
5515:   } else {
5516:     DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);
5517:     CompressPoints_Private(section, &np, pts);
5518:   }
5519:   *numPoints = np;
5520:   *points    = pts;
5521:   *clp       = cla;
5522:   return 0;
5523: }

5525: PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
5526: {
5528:   if (!*clPoints) {
5529:     DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);
5530:   } else {
5531:     ISRestoreIndices(*clPoints, clp);
5532:   }
5533:   *numPoints = 0;
5534:   *points    = NULL;
5535:   *clSec     = NULL;
5536:   *clPoints  = NULL;
5537:   *clp       = NULL;
5538:   return 0;
5539: }

5541: static inline PetscErrorCode DMPlexVecGetClosure_Static(DM dm, PetscSection section, PetscInt numPoints, const PetscInt points[], const PetscInt clperm[], const PetscScalar vArray[], PetscInt *size, PetscScalar array[])
5542: {
5543:   PetscInt          offset = 0, p;
5544:   const PetscInt    **perms = NULL;
5545:   const PetscScalar **flips = NULL;

5548:   *size = 0;
5549:   PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);
5550:   for (p = 0; p < numPoints; p++) {
5551:     const PetscInt    point = points[2*p];
5552:     const PetscInt    *perm = perms ? perms[p] : NULL;
5553:     const PetscScalar *flip = flips ? flips[p] : NULL;
5554:     PetscInt          dof, off, d;
5555:     const PetscScalar *varr;

5557:     PetscSectionGetDof(section, point, &dof);
5558:     PetscSectionGetOffset(section, point, &off);
5559:     varr = &vArray[off];
5560:     if (clperm) {
5561:       if (perm) {
5562:         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
5563:       } else {
5564:         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
5565:       }
5566:       if (flip) {
5567:         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
5568:       }
5569:     } else {
5570:       if (perm) {
5571:         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
5572:       } else {
5573:         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
5574:       }
5575:       if (flip) {
5576:         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
5577:       }
5578:     }
5579:     offset += dof;
5580:   }
5581:   PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);
5582:   *size = offset;
5583:   return 0;
5584: }

5586: 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[])
5587: {
5588:   PetscInt          offset = 0, f;

5591:   *size = 0;
5592:   for (f = 0; f < numFields; ++f) {
5593:     PetscInt          p;
5594:     const PetscInt    **perms = NULL;
5595:     const PetscScalar **flips = NULL;

5597:     PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);
5598:     for (p = 0; p < numPoints; p++) {
5599:       const PetscInt    point = points[2*p];
5600:       PetscInt          fdof, foff, b;
5601:       const PetscScalar *varr;
5602:       const PetscInt    *perm = perms ? perms[p] : NULL;
5603:       const PetscScalar *flip = flips ? flips[p] : NULL;

5605:       PetscSectionGetFieldDof(section, point, f, &fdof);
5606:       PetscSectionGetFieldOffset(section, point, f, &foff);
5607:       varr = &vArray[foff];
5608:       if (clperm) {
5609:         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
5610:         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
5611:         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
5612:       } else {
5613:         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
5614:         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
5615:         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
5616:       }
5617:       offset += fdof;
5618:     }
5619:     PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);
5620:   }
5621:   *size = offset;
5622:   return 0;
5623: }

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

5628:   Not collective

5630:   Input Parameters:
5631: + dm - The DM
5632: . section - The section describing the layout in v, or NULL to use the default section
5633: . v - The local vector
5634: - point - The point in the DM

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

5641: $ Note that DMPlexVecGetClosure/DMPlexVecRestoreClosure only allocates the values array if it set to NULL in the
5642: $ calling function. This is because DMPlexVecGetClosure() is typically called in the inner loop of a Vec or Mat
5643: $ assembly function, and a user may already have allocated storage for this operation.
5644: $
5645: $ A typical use could be
5646: $
5647: $  values = NULL;
5648: $  DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);
5649: $  for (cl = 0; cl < clSize; ++cl) {
5650: $    <Compute on closure>
5651: $  }
5652: $  DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values);
5653: $
5654: $ or
5655: $
5656: $  PetscMalloc1(clMaxSize, &values);
5657: $  for (p = pStart; p < pEnd; ++p) {
5658: $    clSize = clMaxSize;
5659: $    DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);
5660: $    for (cl = 0; cl < clSize; ++cl) {
5661: $      <Compute on closure>
5662: $    }
5663: $  }
5664: $  PetscFree(values);

5666:   Fortran Notes:
5667:   Since it returns an array, this routine is only available in Fortran 90, and you must
5668:   include petsc.h90 in your code.

5670:   The csize argument is not present in the Fortran 90 binding since it is internal to the array.

5672:   Level: intermediate

5674: .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
5675: @*/
5676: PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
5677: {
5678:   PetscSection       clSection;
5679:   IS                 clPoints;
5680:   PetscInt          *points = NULL;
5681:   const PetscInt    *clp, *perm;
5682:   PetscInt           depth, numFields, numPoints, asize;

5686:   if (!section) DMGetLocalSection(dm, &section);
5689:   DMPlexGetDepth(dm, &depth);
5690:   PetscSectionGetNumFields(section, &numFields);
5691:   if (depth == 1 && numFields < 2) {
5692:     DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);
5693:     return 0;
5694:   }
5695:   /* Get points */
5696:   DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
5697:   /* Get sizes */
5698:   asize = 0;
5699:   for (PetscInt p = 0; p < numPoints*2; p += 2) {
5700:     PetscInt dof;
5701:     PetscSectionGetDof(section, points[p], &dof);
5702:     asize += dof;
5703:   }
5704:   if (values) {
5705:     const PetscScalar *vArray;
5706:     PetscInt          size;

5708:     if (*values) {
5710:     } else DMGetWorkArray(dm, asize, MPIU_SCALAR, values);
5711:     PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, asize, &perm);
5712:     VecGetArrayRead(v, &vArray);
5713:     /* Get values */
5714:     if (numFields > 0) DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, *values);
5715:     else               DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, *values);
5717:     /* Cleanup array */
5718:     VecRestoreArrayRead(v, &vArray);
5719:   }
5720:   if (csize) *csize = asize;
5721:   /* Cleanup points */
5722:   DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
5723:   return 0;
5724: }

5726: PetscErrorCode DMPlexVecGetClosureAtDepth_Internal(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt depth, PetscInt *csize, PetscScalar *values[])
5727: {
5728:   DMLabel            depthLabel;
5729:   PetscSection       clSection;
5730:   IS                 clPoints;
5731:   PetscScalar       *array;
5732:   const PetscScalar *vArray;
5733:   PetscInt          *points = NULL;
5734:   const PetscInt    *clp, *perm = NULL;
5735:   PetscInt           mdepth, numFields, numPoints, Np = 0, p, clsize, size;

5739:   if (!section) DMGetLocalSection(dm, &section);
5742:   DMPlexGetDepth(dm, &mdepth);
5743:   DMPlexGetDepthLabel(dm, &depthLabel);
5744:   PetscSectionGetNumFields(section, &numFields);
5745:   if (mdepth == 1 && numFields < 2) {
5746:     DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);
5747:     return 0;
5748:   }
5749:   /* Get points */
5750:   DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
5751:   for (clsize=0,p=0; p<Np; p++) {
5752:     PetscInt dof;
5753:     PetscSectionGetDof(section, points[2*p], &dof);
5754:     clsize += dof;
5755:   }
5756:   PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, clsize, &perm);
5757:   /* Filter points */
5758:   for (p = 0; p < numPoints*2; p += 2) {
5759:     PetscInt dep;

5761:     DMLabelGetValue(depthLabel, points[p], &dep);
5762:     if (dep != depth) continue;
5763:     points[Np*2+0] = points[p];
5764:     points[Np*2+1] = points[p+1];
5765:     ++Np;
5766:   }
5767:   /* Get array */
5768:   if (!values || !*values) {
5769:     PetscInt asize = 0, dof;

5771:     for (p = 0; p < Np*2; p += 2) {
5772:       PetscSectionGetDof(section, points[p], &dof);
5773:       asize += dof;
5774:     }
5775:     if (!values) {
5776:       DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
5777:       if (csize) *csize = asize;
5778:       return 0;
5779:     }
5780:     DMGetWorkArray(dm, asize, MPIU_SCALAR, &array);
5781:   } else {
5782:     array = *values;
5783:   }
5784:   VecGetArrayRead(v, &vArray);
5785:   /* Get values */
5786:   if (numFields > 0) DMPlexVecGetClosure_Fields_Static(dm, section, Np, points, numFields, perm, vArray, &size, array);
5787:   else               DMPlexVecGetClosure_Static(dm, section, Np, points, perm, vArray, &size, array);
5788:   /* Cleanup points */
5789:   DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
5790:   /* Cleanup array */
5791:   VecRestoreArrayRead(v, &vArray);
5792:   if (!*values) {
5793:     if (csize) *csize = size;
5794:     *values = array;
5795:   } else {
5797:     *csize = size;
5798:   }
5799:   return 0;
5800: }

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

5805:   Not collective

5807:   Input Parameters:
5808: + dm - The DM
5809: . section - The section describing the layout in v, or NULL to use the default section
5810: . v - The local vector
5811: . point - The point in the DM
5812: . csize - The number of values in the closure, or NULL
5813: - values - The array of values, which is a borrowed array and should not be freed

5815:   Note that the array values are discarded and not copied back into v. In order to copy values back to v, use DMPlexVecSetClosure()

5817:   Fortran Notes:
5818:   Since it returns an array, this routine is only available in Fortran 90, and you must
5819:   include petsc.h90 in your code.

5821:   The csize argument is not present in the Fortran 90 binding since it is internal to the array.

5823:   Level: intermediate

5825: .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
5826: @*/
5827: PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
5828: {
5829:   PetscInt       size = 0;

5831:   /* Should work without recalculating size */
5832:   DMRestoreWorkArray(dm, size, MPIU_SCALAR, (void*) values);
5833:   *values = NULL;
5834:   return 0;
5835: }

5837: static inline void add   (PetscScalar *x, PetscScalar y) {*x += y;}
5838: static inline void insert(PetscScalar *x, PetscScalar y) {*x  = y;}

5840: 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[])
5841: {
5842:   PetscInt        cdof;   /* The number of constraints on this point */
5843:   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5844:   PetscScalar    *a;
5845:   PetscInt        off, cind = 0, k;

5847:   PetscSectionGetConstraintDof(section, point, &cdof);
5848:   PetscSectionGetOffset(section, point, &off);
5849:   a    = &array[off];
5850:   if (!cdof || setBC) {
5851:     if (clperm) {
5852:       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
5853:       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
5854:     } else {
5855:       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
5856:       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
5857:     }
5858:   } else {
5859:     PetscSectionGetConstraintIndices(section, point, &cdofs);
5860:     if (clperm) {
5861:       if (perm) {for (k = 0; k < dof; ++k) {
5862:           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5863:           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
5864:         }
5865:       } else {
5866:         for (k = 0; k < dof; ++k) {
5867:           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5868:           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
5869:         }
5870:       }
5871:     } else {
5872:       if (perm) {
5873:         for (k = 0; k < dof; ++k) {
5874:           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5875:           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
5876:         }
5877:       } else {
5878:         for (k = 0; k < dof; ++k) {
5879:           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
5880:           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
5881:         }
5882:       }
5883:     }
5884:   }
5885:   return 0;
5886: }

5888: 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[])
5889: {
5890:   PetscInt        cdof;   /* The number of constraints on this point */
5891:   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5892:   PetscScalar    *a;
5893:   PetscInt        off, cind = 0, k;

5895:   PetscSectionGetConstraintDof(section, point, &cdof);
5896:   PetscSectionGetOffset(section, point, &off);
5897:   a    = &array[off];
5898:   if (cdof) {
5899:     PetscSectionGetConstraintIndices(section, point, &cdofs);
5900:     if (clperm) {
5901:       if (perm) {
5902:         for (k = 0; k < dof; ++k) {
5903:           if ((cind < cdof) && (k == cdofs[cind])) {
5904:             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
5905:             cind++;
5906:           }
5907:         }
5908:       } else {
5909:         for (k = 0; k < dof; ++k) {
5910:           if ((cind < cdof) && (k == cdofs[cind])) {
5911:             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
5912:             cind++;
5913:           }
5914:         }
5915:       }
5916:     } else {
5917:       if (perm) {
5918:         for (k = 0; k < dof; ++k) {
5919:           if ((cind < cdof) && (k == cdofs[cind])) {
5920:             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
5921:             cind++;
5922:           }
5923:         }
5924:       } else {
5925:         for (k = 0; k < dof; ++k) {
5926:           if ((cind < cdof) && (k == cdofs[cind])) {
5927:             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
5928:             cind++;
5929:           }
5930:         }
5931:       }
5932:     }
5933:   }
5934:   return 0;
5935: }

5937: 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[])
5938: {
5939:   PetscScalar    *a;
5940:   PetscInt        fdof, foff, fcdof, foffset = *offset;
5941:   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
5942:   PetscInt        cind = 0, b;

5944:   PetscSectionGetFieldDof(section, point, f, &fdof);
5945:   PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);
5946:   PetscSectionGetFieldOffset(section, point, f, &foff);
5947:   a    = &array[foff];
5948:   if (!fcdof || setBC) {
5949:     if (clperm) {
5950:       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
5951:       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
5952:     } else {
5953:       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
5954:       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
5955:     }
5956:   } else {
5957:     PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);
5958:     if (clperm) {
5959:       if (perm) {
5960:         for (b = 0; b < fdof; b++) {
5961:           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
5962:           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
5963:         }
5964:       } else {
5965:         for (b = 0; b < fdof; b++) {
5966:           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
5967:           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
5968:         }
5969:       }
5970:     } else {
5971:       if (perm) {
5972:         for (b = 0; b < fdof; b++) {
5973:           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
5974:           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
5975:         }
5976:       } else {
5977:         for (b = 0; b < fdof; b++) {
5978:           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
5979:           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
5980:         }
5981:       }
5982:     }
5983:   }
5984:   *offset += fdof;
5985:   return 0;
5986: }

5988: 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[])
5989: {
5990:   PetscScalar    *a;
5991:   PetscInt        fdof, foff, fcdof, foffset = *offset;
5992:   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
5993:   PetscInt        Nc, cind = 0, ncind = 0, b;
5994:   PetscBool       ncSet, fcSet;

5996:   PetscSectionGetFieldComponents(section, f, &Nc);
5997:   PetscSectionGetFieldDof(section, point, f, &fdof);
5998:   PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);
5999:   PetscSectionGetFieldOffset(section, point, f, &foff);
6000:   a    = &array[foff];
6001:   if (fcdof) {
6002:     /* We just override fcdof and fcdofs with Ncc and comps */
6003:     PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);
6004:     if (clperm) {
6005:       if (perm) {
6006:         if (comps) {
6007:           for (b = 0; b < fdof; b++) {
6008:             ncSet = fcSet = PETSC_FALSE;
6009:             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
6010:             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
6011:             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}
6012:           }
6013:         } else {
6014:           for (b = 0; b < fdof; b++) {
6015:             if ((cind < fcdof) && (b == fcdofs[cind])) {
6016:               fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
6017:               ++cind;
6018:             }
6019:           }
6020:         }
6021:       } else {
6022:         if (comps) {
6023:           for (b = 0; b < fdof; b++) {
6024:             ncSet = fcSet = PETSC_FALSE;
6025:             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
6026:             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
6027:             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}
6028:           }
6029:         } else {
6030:           for (b = 0; b < fdof; b++) {
6031:             if ((cind < fcdof) && (b == fcdofs[cind])) {
6032:               fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
6033:               ++cind;
6034:             }
6035:           }
6036:         }
6037:       }
6038:     } else {
6039:       if (perm) {
6040:         if (comps) {
6041:           for (b = 0; b < fdof; b++) {
6042:             ncSet = fcSet = PETSC_FALSE;
6043:             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
6044:             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
6045:             if (ncSet && fcSet) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}
6046:           }
6047:         } else {
6048:           for (b = 0; b < fdof; b++) {
6049:             if ((cind < fcdof) && (b == fcdofs[cind])) {
6050:               fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
6051:               ++cind;
6052:             }
6053:           }
6054:         }
6055:       } else {
6056:         if (comps) {
6057:           for (b = 0; b < fdof; b++) {
6058:             ncSet = fcSet = PETSC_FALSE;
6059:             if (b%Nc == comps[ncind]) {ncind = (ncind+1)%Ncc; ncSet = PETSC_TRUE;}
6060:             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
6061:             if (ncSet && fcSet) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}
6062:           }
6063:         } else {
6064:           for (b = 0; b < fdof; b++) {
6065:             if ((cind < fcdof) && (b == fcdofs[cind])) {
6066:               fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
6067:               ++cind;
6068:             }
6069:           }
6070:         }
6071:       }
6072:     }
6073:   }
6074:   *offset += fdof;
6075:   return 0;
6076: }

6078: static inline PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
6079: {
6080:   PetscScalar    *array;
6081:   const PetscInt *cone, *coneO;
6082:   PetscInt        pStart, pEnd, p, numPoints, off, dof;

6085:   PetscSectionGetChart(section, &pStart, &pEnd);
6086:   DMPlexGetConeSize(dm, point, &numPoints);
6087:   DMPlexGetCone(dm, point, &cone);
6088:   DMPlexGetConeOrientation(dm, point, &coneO);
6089:   VecGetArray(v, &array);
6090:   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
6091:     const PetscInt cp = !p ? point : cone[p-1];
6092:     const PetscInt o  = !p ? 0     : coneO[p-1];

6094:     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
6095:     PetscSectionGetDof(section, cp, &dof);
6096:     /* ADD_VALUES */
6097:     {
6098:       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
6099:       PetscScalar    *a;
6100:       PetscInt        cdof, coff, cind = 0, k;

6102:       PetscSectionGetConstraintDof(section, cp, &cdof);
6103:       PetscSectionGetOffset(section, cp, &coff);
6104:       a    = &array[coff];
6105:       if (!cdof) {
6106:         if (o >= 0) {
6107:           for (k = 0; k < dof; ++k) {
6108:             a[k] += values[off+k];
6109:           }
6110:         } else {
6111:           for (k = 0; k < dof; ++k) {
6112:             a[k] += values[off+dof-k-1];
6113:           }
6114:         }
6115:       } else {
6116:         PetscSectionGetConstraintIndices(section, cp, &cdofs);
6117:         if (o >= 0) {
6118:           for (k = 0; k < dof; ++k) {
6119:             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
6120:             a[k] += values[off+k];
6121:           }
6122:         } else {
6123:           for (k = 0; k < dof; ++k) {
6124:             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
6125:             a[k] += values[off+dof-k-1];
6126:           }
6127:         }
6128:       }
6129:     }
6130:   }
6131:   VecRestoreArray(v, &array);
6132:   return 0;
6133: }

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

6138:   Not collective

6140:   Input Parameters:
6141: + dm - The DM
6142: . section - The section describing the layout in v, or NULL to use the default section
6143: . v - The local vector
6144: . point - The point in the DM
6145: . values - The array of values
6146: - mode - The insert mode. One of INSERT_ALL_VALUES, ADD_ALL_VALUES, INSERT_VALUES, ADD_VALUES, INSERT_BC_VALUES, and ADD_BC_VALUES,
6147:          where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions.

6149:   Fortran Notes:
6150:   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.

6152:   Level: intermediate

6154: .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
6155: @*/
6156: PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
6157: {
6158:   PetscSection    clSection;
6159:   IS              clPoints;
6160:   PetscScalar    *array;
6161:   PetscInt       *points = NULL;
6162:   const PetscInt *clp, *clperm = NULL;
6163:   PetscInt        depth, numFields, numPoints, p, clsize;

6167:   if (!section) DMGetLocalSection(dm, &section);
6170:   DMPlexGetDepth(dm, &depth);
6171:   PetscSectionGetNumFields(section, &numFields);
6172:   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
6173:     DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);
6174:     return 0;
6175:   }
6176:   /* Get points */
6177:   DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
6178:   for (clsize=0,p=0; p<numPoints; p++) {
6179:     PetscInt dof;
6180:     PetscSectionGetDof(section, points[2*p], &dof);
6181:     clsize += dof;
6182:   }
6183:   PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, clsize, &clperm);
6184:   /* Get array */
6185:   VecGetArray(v, &array);
6186:   /* Get values */
6187:   if (numFields > 0) {
6188:     PetscInt offset = 0, f;
6189:     for (f = 0; f < numFields; ++f) {
6190:       const PetscInt    **perms = NULL;
6191:       const PetscScalar **flips = NULL;

6193:       PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);
6194:       switch (mode) {
6195:       case INSERT_VALUES:
6196:         for (p = 0; p < numPoints; p++) {
6197:           const PetscInt    point = points[2*p];
6198:           const PetscInt    *perm = perms ? perms[p] : NULL;
6199:           const PetscScalar *flip = flips ? flips[p] : NULL;
6200:           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
6201:         } break;
6202:       case INSERT_ALL_VALUES:
6203:         for (p = 0; p < numPoints; p++) {
6204:           const PetscInt    point = points[2*p];
6205:           const PetscInt    *perm = perms ? perms[p] : NULL;
6206:           const PetscScalar *flip = flips ? flips[p] : NULL;
6207:           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
6208:         } break;
6209:       case INSERT_BC_VALUES:
6210:         for (p = 0; p < numPoints; p++) {
6211:           const PetscInt    point = points[2*p];
6212:           const PetscInt    *perm = perms ? perms[p] : NULL;
6213:           const PetscScalar *flip = flips ? flips[p] : NULL;
6214:           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, insert, clperm, values, &offset, array);
6215:         } break;
6216:       case ADD_VALUES:
6217:         for (p = 0; p < numPoints; p++) {
6218:           const PetscInt    point = points[2*p];
6219:           const PetscInt    *perm = perms ? perms[p] : NULL;
6220:           const PetscScalar *flip = flips ? flips[p] : NULL;
6221:           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
6222:         } break;
6223:       case ADD_ALL_VALUES:
6224:         for (p = 0; p < numPoints; p++) {
6225:           const PetscInt    point = points[2*p];
6226:           const PetscInt    *perm = perms ? perms[p] : NULL;
6227:           const PetscScalar *flip = flips ? flips[p] : NULL;
6228:           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
6229:         } break;
6230:       case ADD_BC_VALUES:
6231:         for (p = 0; p < numPoints; p++) {
6232:           const PetscInt    point = points[2*p];
6233:           const PetscInt    *perm = perms ? perms[p] : NULL;
6234:           const PetscScalar *flip = flips ? flips[p] : NULL;
6235:           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, add, clperm, values, &offset, array);
6236:         } break;
6237:       default:
6238:         SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
6239:       }
6240:       PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);
6241:     }
6242:   } else {
6243:     PetscInt dof, off;
6244:     const PetscInt    **perms = NULL;
6245:     const PetscScalar **flips = NULL;

6247:     PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);
6248:     switch (mode) {
6249:     case INSERT_VALUES:
6250:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
6251:         const PetscInt    point = points[2*p];
6252:         const PetscInt    *perm = perms ? perms[p] : NULL;
6253:         const PetscScalar *flip = flips ? flips[p] : NULL;
6254:         PetscSectionGetDof(section, point, &dof);
6255:         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
6256:       } break;
6257:     case INSERT_ALL_VALUES:
6258:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
6259:         const PetscInt    point = points[2*p];
6260:         const PetscInt    *perm = perms ? perms[p] : NULL;
6261:         const PetscScalar *flip = flips ? flips[p] : NULL;
6262:         PetscSectionGetDof(section, point, &dof);
6263:         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
6264:       } break;
6265:     case INSERT_BC_VALUES:
6266:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
6267:         const PetscInt    point = points[2*p];
6268:         const PetscInt    *perm = perms ? perms[p] : NULL;
6269:         const PetscScalar *flip = flips ? flips[p] : NULL;
6270:         PetscSectionGetDof(section, point, &dof);
6271:         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
6272:       } break;
6273:     case ADD_VALUES:
6274:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
6275:         const PetscInt    point = points[2*p];
6276:         const PetscInt    *perm = perms ? perms[p] : NULL;
6277:         const PetscScalar *flip = flips ? flips[p] : NULL;
6278:         PetscSectionGetDof(section, point, &dof);
6279:         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
6280:       } break;
6281:     case ADD_ALL_VALUES:
6282:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
6283:         const PetscInt    point = points[2*p];
6284:         const PetscInt    *perm = perms ? perms[p] : NULL;
6285:         const PetscScalar *flip = flips ? flips[p] : NULL;
6286:         PetscSectionGetDof(section, point, &dof);
6287:         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
6288:       } break;
6289:     case ADD_BC_VALUES:
6290:       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
6291:         const PetscInt    point = points[2*p];
6292:         const PetscInt    *perm = perms ? perms[p] : NULL;
6293:         const PetscScalar *flip = flips ? flips[p] : NULL;
6294:         PetscSectionGetDof(section, point, &dof);
6295:         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
6296:       } break;
6297:     default:
6298:       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
6299:     }
6300:     PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);
6301:   }
6302:   /* Cleanup points */
6303:   DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
6304:   /* Cleanup array */
6305:   VecRestoreArray(v, &array);
6306:   return 0;
6307: }

6309: /* Check whether the given point is in the label. If not, update the offset to skip this point */
6310: static inline PetscErrorCode CheckPoint_Private(DMLabel label, PetscInt labelId, PetscSection section, PetscInt point, PetscInt f, PetscInt *offset)
6311: {
6312:   if (label) {
6313:     PetscInt       val, fdof;

6315:     /* There is a problem with this:
6316:          Suppose we have two label values, defining surfaces, interecting along a line in 3D. When we add cells to the label, the cells that
6317:        touch both surfaces must pick a label value. Thus we miss setting values for the surface with that other value intersecting that cell.
6318:        Thus I am only going to check val != -1, not val != labelId
6319:     */
6320:     DMLabelGetValue(label, point, &val);
6321:     if (val < 0) {
6322:       PetscSectionGetFieldDof(section, point, f, &fdof);
6323:       *offset += fdof;
6324:       return 1;
6325:     }
6326:   }
6327:   return 0;
6328: }

6330: /* Unlike DMPlexVecSetClosure(), this uses plex-native closure permutation, not a user-specified permutation such as DMPlexSetClosurePermutationTensor(). */
6331: 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)
6332: {
6333:   PetscSection    clSection;
6334:   IS              clPoints;
6335:   PetscScalar    *array;
6336:   PetscInt       *points = NULL;
6337:   const PetscInt *clp;
6338:   PetscInt        numFields, numPoints, p;
6339:   PetscInt        offset = 0, f;

6343:   if (!section) DMGetLocalSection(dm, &section);
6346:   PetscSectionGetNumFields(section, &numFields);
6347:   /* Get points */
6348:   DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
6349:   /* Get array */
6350:   VecGetArray(v, &array);
6351:   /* Get values */
6352:   for (f = 0; f < numFields; ++f) {
6353:     const PetscInt    **perms = NULL;
6354:     const PetscScalar **flips = NULL;

6356:     if (!fieldActive[f]) {
6357:       for (p = 0; p < numPoints*2; p += 2) {
6358:         PetscInt fdof;
6359:         PetscSectionGetFieldDof(section, points[p], f, &fdof);
6360:         offset += fdof;
6361:       }
6362:       continue;
6363:     }
6364:     PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);
6365:     switch (mode) {
6366:     case INSERT_VALUES:
6367:       for (p = 0; p < numPoints; p++) {
6368:         const PetscInt    point = points[2*p];
6369:         const PetscInt    *perm = perms ? perms[p] : NULL;
6370:         const PetscScalar *flip = flips ? flips[p] : NULL;
6371:         if (CheckPoint_Private(label, labelId, section, point, f, &offset)) continue;
6372:         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, NULL, values, &offset, array);
6373:       } break;
6374:     case INSERT_ALL_VALUES:
6375:       for (p = 0; p < numPoints; p++) {
6376:         const PetscInt    point = points[2*p];
6377:         const PetscInt    *perm = perms ? perms[p] : NULL;
6378:         const PetscScalar *flip = flips ? flips[p] : NULL;
6379:         if (CheckPoint_Private(label, labelId, section, point, f, &offset)) continue;
6380:         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, NULL, values, &offset, array);
6381:       } break;
6382:     case INSERT_BC_VALUES:
6383:       for (p = 0; p < numPoints; p++) {
6384:         const PetscInt    point = points[2*p];
6385:         const PetscInt    *perm = perms ? perms[p] : NULL;
6386:         const PetscScalar *flip = flips ? flips[p] : NULL;
6387:         if (CheckPoint_Private(label, labelId, section, point, f, &offset)) continue;
6388:         updatePointFieldsBC_private(section, point, perm, flip, f, Ncc, comps, insert, NULL, values, &offset, array);
6389:       } break;
6390:     case ADD_VALUES:
6391:       for (p = 0; p < numPoints; p++) {
6392:         const PetscInt    point = points[2*p];
6393:         const PetscInt    *perm = perms ? perms[p] : NULL;
6394:         const PetscScalar *flip = flips ? flips[p] : NULL;
6395:         if (CheckPoint_Private(label, labelId, section, point, f, &offset)) continue;
6396:         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, NULL, values, &offset, array);
6397:       } break;
6398:     case ADD_ALL_VALUES:
6399:       for (p = 0; p < numPoints; p++) {
6400:         const PetscInt    point = points[2*p];
6401:         const PetscInt    *perm = perms ? perms[p] : NULL;
6402:         const PetscScalar *flip = flips ? flips[p] : NULL;
6403:         if (CheckPoint_Private(label, labelId, section, point, f, &offset)) continue;
6404:         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, NULL, values, &offset, array);
6405:       } break;
6406:     default:
6407:       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
6408:     }
6409:     PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);
6410:   }
6411:   /* Cleanup points */
6412:   DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);
6413:   /* Cleanup array */
6414:   VecRestoreArray(v, &array);
6415:   return 0;
6416: }

6418: static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
6419: {
6420:   PetscMPIInt    rank;
6421:   PetscInt       i, j;

6423:   MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);
6424:   PetscViewerASCIIPrintf(viewer, "[%d]mat for point %D\n", rank, point);
6425:   for (i = 0; i < numRIndices; i++) PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);
6426:   for (i = 0; i < numCIndices; i++) PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);
6427:   numCIndices = numCIndices ? numCIndices : numRIndices;
6428:   if (!values) return 0;
6429:   for (i = 0; i < numRIndices; i++) {
6430:     PetscViewerASCIIPrintf(viewer, "[%d]", rank);
6431:     for (j = 0; j < numCIndices; j++) {
6432: #if defined(PETSC_USE_COMPLEX)
6433:       PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));
6434: #else
6435:       PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);
6436: #endif
6437:     }
6438:     PetscViewerASCIIPrintf(viewer, "\n");
6439:   }
6440:   return 0;
6441: }

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

6446:   Input Parameters:
6447: + section - The section for this data layout
6448: . islocal - Is the section (and thus indices being requested) local or global?
6449: . point   - The point contributing dofs with these indices
6450: . off     - The global offset of this point
6451: . loff    - The local offset of each field
6452: . setBC   - The flag determining whether to include indices of boundary values
6453: . perm    - A permutation of the dofs on this point, or NULL
6454: - indperm - A permutation of the entire indices array, or NULL

6456:   Output Parameter:
6457: . indices - Indices for dofs on this point

6459:   Level: developer

6461:   Note: The indices could be local or global, depending on the value of 'off'.
6462: */
6463: PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscBool islocal,PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], const PetscInt indperm[], PetscInt indices[])
6464: {
6465:   PetscInt        dof;   /* The number of unknowns on this point */
6466:   PetscInt        cdof;  /* The number of constraints on this point */
6467:   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
6468:   PetscInt        cind = 0, k;

6471:   PetscSectionGetDof(section, point, &dof);
6472:   PetscSectionGetConstraintDof(section, point, &cdof);
6473:   if (!cdof || setBC) {
6474:     for (k = 0; k < dof; ++k) {
6475:       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
6476:       const PetscInt ind    = indperm ? indperm[preind] : preind;

6478:       indices[ind] = off + k;
6479:     }
6480:   } else {
6481:     PetscSectionGetConstraintIndices(section, point, &cdofs);
6482:     for (k = 0; k < dof; ++k) {
6483:       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
6484:       const PetscInt ind    = indperm ? indperm[preind] : preind;

6486:       if ((cind < cdof) && (k == cdofs[cind])) {
6487:         /* Insert check for returning constrained indices */
6488:         indices[ind] = -(off+k+1);
6489:         ++cind;
6490:       } else {
6491:         indices[ind] = off + k - (islocal ? 0 : cind);
6492:       }
6493:     }
6494:   }
6495:   *loff += dof;
6496:   return 0;
6497: }

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

6502:  Input Parameters:
6503: + section - a section (global or local)
6504: - islocal - PETSC_TRUE if requesting local indices (i.e., section is local); PETSC_FALSE for global
6505: . point - point within section
6506: . off - The offset of this point in the (local or global) indexed space - should match islocal and (usually) the section
6507: . foffs - array of length numFields containing the offset in canonical point ordering (the location in indices) of each field
6508: . setBC - identify constrained (boundary condition) points via involution.
6509: . perms - perms[f][permsoff][:] is a permutation of dofs within each field
6510: . permsoff - offset
6511: - indperm - index permutation

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

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

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

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

6530:  Example:
6531:  Suppose a point contains one field with three components, and for which the unconstrained indices are {10, 11, 12}.
6532:  When the middle component is constrained, we get the array {10, -12, 12} for (islocal=TRUE, setBC=FALSE).
6533:  Note that -12 is the involution of 11, so the user can involute negative indices to recover local indices.
6534:  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.

6536:  Level: developer
6537: */
6538: 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[])
6539: {
6540:   PetscInt       numFields, foff, f;

6543:   PetscSectionGetNumFields(section, &numFields);
6544:   for (f = 0, foff = 0; f < numFields; ++f) {
6545:     PetscInt        fdof, cfdof;
6546:     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
6547:     PetscInt        cind = 0, b;
6548:     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;

6550:     PetscSectionGetFieldDof(section, point, f, &fdof);
6551:     PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);
6552:     if (!cfdof || setBC) {
6553:       for (b = 0; b < fdof; ++b) {
6554:         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
6555:         const PetscInt ind    = indperm ? indperm[preind] : preind;

6557:         indices[ind] = off+foff+b;
6558:       }
6559:     } else {
6560:       PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);
6561:       for (b = 0; b < fdof; ++b) {
6562:         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
6563:         const PetscInt ind    = indperm ? indperm[preind] : preind;

6565:         if ((cind < cfdof) && (b == fcdofs[cind])) {
6566:           indices[ind] = -(off+foff+b+1);
6567:           ++cind;
6568:         } else {
6569:           indices[ind] = off + foff + b - (islocal ? 0 : cind);
6570:         }
6571:       }
6572:     }
6573:     foff     += (setBC || islocal ? fdof : (fdof - cfdof));
6574:     foffs[f] += fdof;
6575:   }
6576:   return 0;
6577: }

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

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

6584:  Notes:
6585:  The semantics of this function relate to that of setBC=FALSE in DMPlexGetIndicesPointFields_Internal.
6586:  Since this function uses global indices, setBC=TRUE would be invalid, so no such argument exists.
6587: */
6588: static PetscErrorCode DMPlexGetIndicesPointFieldsSplit_Internal(PetscSection section, PetscSection globalSection, PetscInt point, PetscInt foffs[], const PetscInt ***perms, PetscInt permsoff, const PetscInt indperm[], PetscInt indices[])
6589: {
6590:   PetscInt       numFields, foff, f;

6592:   PetscSectionGetNumFields(section, &numFields);
6593:   for (f = 0; f < numFields; ++f) {
6594:     PetscInt        fdof, cfdof;
6595:     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
6596:     PetscInt        cind = 0, b;
6597:     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;

6599:     PetscSectionGetFieldDof(section, point, f, &fdof);
6600:     PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);
6601:     PetscSectionGetFieldOffset(globalSection, point, f, &foff);
6602:     if (!cfdof) {
6603:       for (b = 0; b < fdof; ++b) {
6604:         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
6605:         const PetscInt ind    = indperm ? indperm[preind] : preind;

6607:         indices[ind] = foff+b;
6608:       }
6609:     } else {
6610:       PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);
6611:       for (b = 0; b < fdof; ++b) {
6612:         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
6613:         const PetscInt ind    = indperm ? indperm[preind] : preind;

6615:         if ((cind < cfdof) && (b == fcdofs[cind])) {
6616:           indices[ind] = -(foff+b+1);
6617:           ++cind;
6618:         } else {
6619:           indices[ind] = foff+b-cind;
6620:         }
6621:       }
6622:     }
6623:     foffs[f] += fdof;
6624:   }
6625:   return 0;
6626: }

6628: 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)
6629: {
6630:   Mat             cMat;
6631:   PetscSection    aSec, cSec;
6632:   IS              aIS;
6633:   PetscInt        aStart = -1, aEnd = -1;
6634:   const PetscInt  *anchors;
6635:   PetscInt        numFields, f, p, q, newP = 0;
6636:   PetscInt        newNumPoints = 0, newNumIndices = 0;
6637:   PetscInt        *newPoints, *indices, *newIndices;
6638:   PetscInt        maxAnchor, maxDof;
6639:   PetscInt        newOffsets[32];
6640:   PetscInt        *pointMatOffsets[32];
6641:   PetscInt        *newPointOffsets[32];
6642:   PetscScalar     *pointMat[32];
6643:   PetscScalar     *newValues=NULL,*tmpValues;
6644:   PetscBool       anyConstrained = PETSC_FALSE;

6648:   PetscSectionGetNumFields(section, &numFields);

6650:   DMPlexGetAnchors(dm,&aSec,&aIS);
6651:   /* if there are point-to-point constraints */
6652:   if (aSec) {
6653:     PetscArrayzero(newOffsets, 32);
6654:     ISGetIndices(aIS,&anchors);
6655:     PetscSectionGetChart(aSec,&aStart,&aEnd);
6656:     /* figure out how many points are going to be in the new element matrix
6657:      * (we allow double counting, because it's all just going to be summed
6658:      * into the global matrix anyway) */
6659:     for (p = 0; p < 2*numPoints; p+=2) {
6660:       PetscInt b    = points[p];
6661:       PetscInt bDof = 0, bSecDof;

6663:       PetscSectionGetDof(section,b,&bSecDof);
6664:       if (!bSecDof) {
6665:         continue;
6666:       }
6667:       if (b >= aStart && b < aEnd) {
6668:         PetscSectionGetDof(aSec,b,&bDof);
6669:       }
6670:       if (bDof) {
6671:         /* this point is constrained */
6672:         /* it is going to be replaced by its anchors */
6673:         PetscInt bOff, q;

6675:         anyConstrained = PETSC_TRUE;
6676:         newNumPoints  += bDof;
6677:         PetscSectionGetOffset(aSec,b,&bOff);
6678:         for (q = 0; q < bDof; q++) {
6679:           PetscInt a = anchors[bOff + q];
6680:           PetscInt aDof;

6682:           PetscSectionGetDof(section,a,&aDof);
6683:           newNumIndices += aDof;
6684:           for (f = 0; f < numFields; ++f) {
6685:             PetscInt fDof;

6687:             PetscSectionGetFieldDof(section, a, f, &fDof);
6688:             newOffsets[f+1] += fDof;
6689:           }
6690:         }
6691:       }
6692:       else {
6693:         /* this point is not constrained */
6694:         newNumPoints++;
6695:         newNumIndices += bSecDof;
6696:         for (f = 0; f < numFields; ++f) {
6697:           PetscInt fDof;

6699:           PetscSectionGetFieldDof(section, b, f, &fDof);
6700:           newOffsets[f+1] += fDof;
6701:         }
6702:       }
6703:     }
6704:   }
6705:   if (!anyConstrained) {
6706:     if (outNumPoints)  *outNumPoints  = 0;
6707:     if (outNumIndices) *outNumIndices = 0;
6708:     if (outPoints)     *outPoints     = NULL;
6709:     if (outValues)     *outValues     = NULL;
6710:     if (aSec) ISRestoreIndices(aIS,&anchors);
6711:     return 0;
6712:   }

6714:   if (outNumPoints)  *outNumPoints  = newNumPoints;
6715:   if (outNumIndices) *outNumIndices = newNumIndices;

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

6719:   if (!outPoints && !outValues) {
6720:     if (offsets) {
6721:       for (f = 0; f <= numFields; f++) {
6722:         offsets[f] = newOffsets[f];
6723:       }
6724:     }
6725:     if (aSec) ISRestoreIndices(aIS,&anchors);
6726:     return 0;
6727:   }


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

6733:   /* workspaces */
6734:   if (numFields) {
6735:     for (f = 0; f < numFields; f++) {
6736:       DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);
6737:       DMGetWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);
6738:     }
6739:   }
6740:   else {
6741:     DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);
6742:     DMGetWorkArray(dm,numPoints,MPIU_INT,&newPointOffsets[0]);
6743:   }

6745:   /* get workspaces for the point-to-point matrices */
6746:   if (numFields) {
6747:     PetscInt totalOffset, totalMatOffset;

6749:     for (p = 0; p < numPoints; p++) {
6750:       PetscInt b    = points[2*p];
6751:       PetscInt bDof = 0, bSecDof;

6753:       PetscSectionGetDof(section,b,&bSecDof);
6754:       if (!bSecDof) {
6755:         for (f = 0; f < numFields; f++) {
6756:           newPointOffsets[f][p + 1] = 0;
6757:           pointMatOffsets[f][p + 1] = 0;
6758:         }
6759:         continue;
6760:       }
6761:       if (b >= aStart && b < aEnd) {
6762:         PetscSectionGetDof(aSec, b, &bDof);
6763:       }
6764:       if (bDof) {
6765:         for (f = 0; f < numFields; f++) {
6766:           PetscInt fDof, q, bOff, allFDof = 0;

6768:           PetscSectionGetFieldDof(section, b, f, &fDof);
6769:           PetscSectionGetOffset(aSec, b, &bOff);
6770:           for (q = 0; q < bDof; q++) {
6771:             PetscInt a = anchors[bOff + q];
6772:             PetscInt aFDof;

6774:             PetscSectionGetFieldDof(section, a, f, &aFDof);
6775:             allFDof += aFDof;
6776:           }
6777:           newPointOffsets[f][p+1] = allFDof;
6778:           pointMatOffsets[f][p+1] = fDof * allFDof;
6779:         }
6780:       }
6781:       else {
6782:         for (f = 0; f < numFields; f++) {
6783:           PetscInt fDof;

6785:           PetscSectionGetFieldDof(section, b, f, &fDof);
6786:           newPointOffsets[f][p+1] = fDof;
6787:           pointMatOffsets[f][p+1] = 0;
6788:         }
6789:       }
6790:     }
6791:     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
6792:       newPointOffsets[f][0] = totalOffset;
6793:       pointMatOffsets[f][0] = totalMatOffset;
6794:       for (p = 0; p < numPoints; p++) {
6795:         newPointOffsets[f][p+1] += newPointOffsets[f][p];
6796:         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
6797:       }
6798:       totalOffset    = newPointOffsets[f][numPoints];
6799:       totalMatOffset = pointMatOffsets[f][numPoints];
6800:       DMGetWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);
6801:     }
6802:   }
6803:   else {
6804:     for (p = 0; p < numPoints; p++) {
6805:       PetscInt b    = points[2*p];
6806:       PetscInt bDof = 0, bSecDof;

6808:       PetscSectionGetDof(section,b,&bSecDof);
6809:       if (!bSecDof) {
6810:         newPointOffsets[0][p + 1] = 0;
6811:         pointMatOffsets[0][p + 1] = 0;
6812:         continue;
6813:       }
6814:       if (b >= aStart && b < aEnd) {
6815:         PetscSectionGetDof(aSec, b, &bDof);
6816:       }
6817:       if (bDof) {
6818:         PetscInt bOff, q, allDof = 0;

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

6824:           PetscSectionGetDof(section, a, &aDof);
6825:           allDof += aDof;
6826:         }
6827:         newPointOffsets[0][p+1] = allDof;
6828:         pointMatOffsets[0][p+1] = bSecDof * allDof;
6829:       }
6830:       else {
6831:         newPointOffsets[0][p+1] = bSecDof;
6832:         pointMatOffsets[0][p+1] = 0;
6833:       }
6834:     }
6835:     newPointOffsets[0][0] = 0;
6836:     pointMatOffsets[0][0] = 0;
6837:     for (p = 0; p < numPoints; p++) {
6838:       newPointOffsets[0][p+1] += newPointOffsets[0][p];
6839:       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
6840:     }
6841:     DMGetWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);
6842:   }

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

6847:   /* get the point-to-point matrices; construct newPoints */
6848:   PetscSectionGetMaxDof(aSec, &maxAnchor);
6849:   PetscSectionGetMaxDof(section, &maxDof);
6850:   DMGetWorkArray(dm,maxDof,MPIU_INT,&indices);
6851:   DMGetWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);
6852:   if (numFields) {
6853:     for (p = 0, newP = 0; p < numPoints; p++) {
6854:       PetscInt b    = points[2*p];
6855:       PetscInt o    = points[2*p+1];
6856:       PetscInt bDof = 0, bSecDof;

6858:       PetscSectionGetDof(section, b, &bSecDof);
6859:       if (!bSecDof) {
6860:         continue;
6861:       }
6862:       if (b >= aStart && b < aEnd) {
6863:         PetscSectionGetDof(aSec, b, &bDof);
6864:       }
6865:       if (bDof) {
6866:         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;

6868:         fStart[0] = 0;
6869:         fEnd[0]   = 0;
6870:         for (f = 0; f < numFields; f++) {
6871:           PetscInt fDof;

6873:           PetscSectionGetFieldDof(cSec, b, f, &fDof);
6874:           fStart[f+1] = fStart[f] + fDof;
6875:           fEnd[f+1]   = fStart[f+1];
6876:         }
6877:         PetscSectionGetOffset(cSec, b, &bOff);
6878:         DMPlexGetIndicesPointFields_Internal(cSec, PETSC_TRUE, b, bOff, fEnd, PETSC_TRUE, perms, p, NULL, indices);

6880:         fAnchorStart[0] = 0;
6881:         fAnchorEnd[0]   = 0;
6882:         for (f = 0; f < numFields; f++) {
6883:           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];

6885:           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
6886:           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
6887:         }
6888:         PetscSectionGetOffset(aSec, b, &bOff);
6889:         for (q = 0; q < bDof; q++) {
6890:           PetscInt a = anchors[bOff + q], aOff;

6892:           /* we take the orientation of ap into account in the order that we constructed the indices above: the newly added points have no orientation */
6893:           newPoints[2*(newP + q)]     = a;
6894:           newPoints[2*(newP + q) + 1] = 0;
6895:           PetscSectionGetOffset(section, a, &aOff);
6896:           DMPlexGetIndicesPointFields_Internal(section, PETSC_TRUE, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, NULL, newIndices);
6897:         }
6898:         newP += bDof;

6900:         if (outValues) {
6901:           /* get the point-to-point submatrix */
6902:           for (f = 0; f < numFields; f++) {
6903:             MatGetValues(cMat,fEnd[f]-fStart[f],indices + fStart[f],fAnchorEnd[f] - fAnchorStart[f],newIndices + fAnchorStart[f],pointMat[f] + pointMatOffsets[f][p]);
6904:           }
6905:         }
6906:       }
6907:       else {
6908:         newPoints[2 * newP]     = b;
6909:         newPoints[2 * newP + 1] = o;
6910:         newP++;
6911:       }
6912:     }
6913:   } else {
6914:     for (p = 0; p < numPoints; p++) {
6915:       PetscInt b    = points[2*p];
6916:       PetscInt o    = points[2*p+1];
6917:       PetscInt bDof = 0, bSecDof;

6919:       PetscSectionGetDof(section, b, &bSecDof);
6920:       if (!bSecDof) {
6921:         continue;
6922:       }
6923:       if (b >= aStart && b < aEnd) {
6924:         PetscSectionGetDof(aSec, b, &bDof);
6925:       }
6926:       if (bDof) {
6927:         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;

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

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

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

6938:           newPoints[2*(newP + q)]     = a;
6939:           newPoints[2*(newP + q) + 1] = 0;
6940:           PetscSectionGetOffset(section, a, &aOff);
6941:           DMPlexGetIndicesPoint_Internal(section, PETSC_TRUE, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, NULL, newIndices);
6942:         }
6943:         newP += bDof;

6945:         /* get the point-to-point submatrix */
6946:         if (outValues) {
6947:           MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);
6948:         }
6949:       }
6950:       else {
6951:         newPoints[2 * newP]     = b;
6952:         newPoints[2 * newP + 1] = o;
6953:         newP++;
6954:       }
6955:     }
6956:   }

6958:   if (outValues) {
6959:     DMGetWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);
6960:     PetscArrayzero(tmpValues,newNumIndices*numIndices);
6961:     /* multiply constraints on the right */
6962:     if (numFields) {
6963:       for (f = 0; f < numFields; f++) {
6964:         PetscInt oldOff = offsets[f];

6966:         for (p = 0; p < numPoints; p++) {
6967:           PetscInt cStart = newPointOffsets[f][p];
6968:           PetscInt b      = points[2 * p];
6969:           PetscInt c, r, k;
6970:           PetscInt dof;

6972:           PetscSectionGetFieldDof(section,b,f,&dof);
6973:           if (!dof) {
6974:             continue;
6975:           }
6976:           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
6977:             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
6978:             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];

6980:             for (r = 0; r < numIndices; r++) {
6981:               for (c = 0; c < nCols; c++) {
6982:                 for (k = 0; k < dof; k++) {
6983:                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
6984:                 }
6985:               }
6986:             }
6987:           }
6988:           else {
6989:             /* copy this column as is */
6990:             for (r = 0; r < numIndices; r++) {
6991:               for (c = 0; c < dof; c++) {
6992:                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
6993:               }
6994:             }
6995:           }
6996:           oldOff += dof;
6997:         }
6998:       }
6999:     }
7000:     else {
7001:       PetscInt oldOff = 0;
7002:       for (p = 0; p < numPoints; p++) {
7003:         PetscInt cStart = newPointOffsets[0][p];
7004:         PetscInt b      = points[2 * p];
7005:         PetscInt c, r, k;
7006:         PetscInt dof;

7008:         PetscSectionGetDof(section,b,&dof);
7009:         if (!dof) {
7010:           continue;
7011:         }
7012:         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
7013:           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
7014:           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];

7016:           for (r = 0; r < numIndices; r++) {
7017:             for (c = 0; c < nCols; c++) {
7018:               for (k = 0; k < dof; k++) {
7019:                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
7020:               }
7021:             }
7022:           }
7023:         }
7024:         else {
7025:           /* copy this column as is */
7026:           for (r = 0; r < numIndices; r++) {
7027:             for (c = 0; c < dof; c++) {
7028:               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
7029:             }
7030:           }
7031:         }
7032:         oldOff += dof;
7033:       }
7034:     }

7036:     if (multiplyLeft) {
7037:       DMGetWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);
7038:       PetscArrayzero(newValues,newNumIndices*newNumIndices);
7039:       /* multiply constraints transpose on the left */
7040:       if (numFields) {
7041:         for (f = 0; f < numFields; f++) {
7042:           PetscInt oldOff = offsets[f];

7044:           for (p = 0; p < numPoints; p++) {
7045:             PetscInt rStart = newPointOffsets[f][p];
7046:             PetscInt b      = points[2 * p];
7047:             PetscInt c, r, k;
7048:             PetscInt dof;

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

7055:               for (r = 0; r < nRows; r++) {
7056:                 for (c = 0; c < newNumIndices; c++) {
7057:                   for (k = 0; k < dof; k++) {
7058:                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
7059:                   }
7060:                 }
7061:               }
7062:             }
7063:             else {
7064:               /* copy this row as is */
7065:               for (r = 0; r < dof; r++) {
7066:                 for (c = 0; c < newNumIndices; c++) {
7067:                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
7068:                 }
7069:               }
7070:             }
7071:             oldOff += dof;
7072:           }
7073:         }
7074:       }
7075:       else {
7076:         PetscInt oldOff = 0;

7078:         for (p = 0; p < numPoints; p++) {
7079:           PetscInt rStart = newPointOffsets[0][p];
7080:           PetscInt b      = points[2 * p];
7081:           PetscInt c, r, k;
7082:           PetscInt dof;

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

7089:             for (r = 0; r < nRows; r++) {
7090:               for (c = 0; c < newNumIndices; c++) {
7091:                 for (k = 0; k < dof; k++) {
7092:                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
7093:                 }
7094:               }
7095:             }
7096:           }
7097:           else {
7098:             /* copy this row as is */
7099:             for (r = 0; r < dof; r++) {
7100:               for (c = 0; c < newNumIndices; c++) {
7101:                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
7102:               }
7103:             }
7104:           }
7105:           oldOff += dof;
7106:         }
7107:       }

7109:       DMRestoreWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);
7110:     }
7111:     else {
7112:       newValues = tmpValues;
7113:     }
7114:   }

7116:   /* clean up */
7117:   DMRestoreWorkArray(dm,maxDof,MPIU_INT,&indices);
7118:   DMRestoreWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);

7120:   if (numFields) {
7121:     for (f = 0; f < numFields; f++) {
7122:       DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);
7123:       DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);
7124:       DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);
7125:     }
7126:   }
7127:   else {
7128:     DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);
7129:     DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);
7130:     DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[0]);
7131:   }
7132:   ISRestoreIndices(aIS,&anchors);

7134:   /* output */
7135:   if (outPoints) {
7136:     *outPoints = newPoints;
7137:   }
7138:   else {
7139:     DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);
7140:   }
7141:   if (outValues) {
7142:     *outValues = newValues;
7143:   }
7144:   for (f = 0; f <= numFields; f++) {
7145:     offsets[f] = newOffsets[f];
7146:   }
7147:   return 0;
7148: }

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

7153:   Not collective

7155:   Input Parameters:
7156: + dm         - The DM
7157: . section    - The PetscSection describing the points (a local section)
7158: . idxSection - The PetscSection from which to obtain indices (may be local or global)
7159: . point      - The point defining the closure
7160: - useClPerm  - Use the closure point permutation if available

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

7168:   Notes:
7169:   Must call DMPlexRestoreClosureIndices() to free allocated memory

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

7177:   Level: advanced

7179: .seealso DMPlexRestoreClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure(), DMGetLocalSection(), DMGetGlobalSection()
7180: @*/
7181: PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscBool useClPerm,
7182:                                        PetscInt *numIndices, PetscInt *indices[], PetscInt outOffsets[], PetscScalar *values[])
7183: {
7184:   /* Closure ordering */
7185:   PetscSection        clSection;
7186:   IS                  clPoints;
7187:   const PetscInt     *clp;
7188:   PetscInt           *points;
7189:   const PetscInt     *clperm = NULL;
7190:   /* Dof permutation and sign flips */
7191:   const PetscInt    **perms[32] = {NULL};
7192:   const PetscScalar **flips[32] = {NULL};
7193:   PetscScalar        *valCopy   = NULL;
7194:   /* Hanging node constraints */
7195:   PetscInt           *pointsC = NULL;
7196:   PetscScalar        *valuesC = NULL;
7197:   PetscInt            NclC, NiC;

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

7211:   PetscSectionGetNumFields(section, &Nf);
7213:   PetscArrayzero(offsets, 32);
7214:   /* 1) Get points in closure */
7215:   DMPlexGetCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);
7216:   if (useClPerm) {
7217:     PetscInt depth, clsize;
7218:     DMPlexGetPointDepth(dm, point, &depth);
7219:     for (clsize=0,p=0; p<Ncl; p++) {
7220:       PetscInt dof;
7221:       PetscSectionGetDof(section, points[2*p], &dof);
7222:       clsize += dof;
7223:     }
7224:     PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, depth, clsize, &clperm);
7225:   }
7226:   /* 2) Get number of indices on these points and field offsets from section */
7227:   for (p = 0; p < Ncl*2; p += 2) {
7228:     PetscInt dof, fdof;

7230:     PetscSectionGetDof(section, points[p], &dof);
7231:     for (f = 0; f < Nf; ++f) {
7232:       PetscSectionGetFieldDof(section, points[p], f, &fdof);
7233:       offsets[f+1] += fdof;
7234:     }
7235:     Ni += dof;
7236:   }
7237:   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
7239:   /* 3) Get symmetries and sign flips. Apply sign flips to values if passed in (only works for square values matrix) */
7240:   for (f = 0; f < PetscMax(1, Nf); ++f) {
7241:     if (Nf) PetscSectionGetFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);
7242:     else    PetscSectionGetPointSyms(section, Ncl, points, &perms[f], &flips[f]);
7243:     /* may need to apply sign changes to the element matrix */
7244:     if (values && flips[f]) {
7245:       PetscInt foffset = offsets[f];

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

7251:         if (!Nf) PetscSectionGetDof(section, pnt, &fdof);
7252:         else     PetscSectionGetFieldDof(section, pnt, f, &fdof);
7253:         if (flip) {
7254:           PetscInt i, j, k;

7256:           if (!valCopy) {
7257:             DMGetWorkArray(dm, Ni*Ni, MPIU_SCALAR, &valCopy);
7258:             for (j = 0; j < Ni * Ni; ++j) valCopy[j] = (*values)[j];
7259:             *values = valCopy;
7260:           }
7261:           for (i = 0; i < fdof; ++i) {
7262:             PetscScalar fval = flip[i];

7264:             for (k = 0; k < Ni; ++k) {
7265:               valCopy[Ni * (foffset + i) + k] *= fval;
7266:               valCopy[Ni * k + (foffset + i)] *= fval;
7267:             }
7268:           }
7269:         }
7270:         foffset += fdof;
7271:       }
7272:     }
7273:   }
7274:   /* 4) Apply hanging node constraints. Get new symmetries and replace all storage with constrained storage */
7275:   DMPlexAnchorsModifyMat(dm, section, Ncl, Ni, points, perms, values ? *values : NULL, &NclC, &NiC, &pointsC, values ? &valuesC : NULL, offsets, PETSC_TRUE);
7276:   if (NclC) {
7277:     if (valCopy) DMRestoreWorkArray(dm, Ni*Ni, MPIU_SCALAR, &valCopy);
7278:     for (f = 0; f < PetscMax(1, Nf); ++f) {
7279:       if (Nf) PetscSectionRestoreFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);
7280:       else    PetscSectionRestorePointSyms(section, Ncl, points, &perms[f], &flips[f]);
7281:     }
7282:     for (f = 0; f < PetscMax(1, Nf); ++f) {
7283:       if (Nf) PetscSectionGetFieldPointSyms(section, f, NclC, pointsC, &perms[f], &flips[f]);
7284:       else    PetscSectionGetPointSyms(section, NclC, pointsC, &perms[f], &flips[f]);
7285:     }
7286:     DMPlexRestoreCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);
7287:     Ncl     = NclC;
7288:     Ni      = NiC;
7289:     points  = pointsC;
7290:     if (values) *values = valuesC;
7291:   }
7292:   /* 5) Calculate indices */
7293:   DMGetWorkArray(dm, Ni, MPIU_INT, &idx);
7294:   if (Nf) {
7295:     PetscInt  idxOff;
7296:     PetscBool useFieldOffsets;

7298:     if (outOffsets) {for (f = 0; f <= Nf; f++) outOffsets[f] = offsets[f];}
7299:     PetscSectionGetUseFieldOffsets(idxSection, &useFieldOffsets);
7300:     if (useFieldOffsets) {
7301:       for (p = 0; p < Ncl; ++p) {
7302:         const PetscInt pnt = points[p*2];

7304:         DMPlexGetIndicesPointFieldsSplit_Internal(section, idxSection, pnt, offsets, perms, p, clperm, idx);
7305:       }
7306:     } else {
7307:       for (p = 0; p < Ncl; ++p) {
7308:         const PetscInt pnt = points[p*2];

7310:         PetscSectionGetOffset(idxSection, pnt, &idxOff);
7311:         /* Note that we pass a local section even though we're using global offsets.  This is because global sections do
7312:          * not (at the time of this writing) have fields set. They probably should, in which case we would pass the
7313:          * global section. */
7314:         DMPlexGetIndicesPointFields_Internal(section, isLocal, pnt, idxOff < 0 ? -(idxOff+1) : idxOff, offsets, PETSC_FALSE, perms, p, clperm, idx);
7315:       }
7316:     }
7317:   } else {
7318:     PetscInt off = 0, idxOff;

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

7324:       PetscSectionGetOffset(idxSection, pnt, &idxOff);
7325:       /* Note that we pass a local section even though we're using global offsets.  This is because global sections do
7326:        * not (at the time of this writing) have fields set. They probably should, in which case we would pass the global section. */
7327:       DMPlexGetIndicesPoint_Internal(section, isLocal, pnt, idxOff < 0 ? -(idxOff+1) : idxOff, &off, PETSC_FALSE, perm, clperm, idx);
7328:     }
7329:   }
7330:   /* 6) Cleanup */
7331:   for (f = 0; f < PetscMax(1, Nf); ++f) {
7332:     if (Nf) PetscSectionRestoreFieldPointSyms(section, f, Ncl, points, &perms[f], &flips[f]);
7333:     else    PetscSectionRestorePointSyms(section, Ncl, points, &perms[f], &flips[f]);
7334:   }
7335:   if (NclC) {
7336:     DMRestoreWorkArray(dm, NclC*2, MPIU_INT, &pointsC);
7337:   } else {
7338:     DMPlexRestoreCompressedClosure(dm, section, point, &Ncl, &points, &clSection, &clPoints, &clp);
7339:   }

7341:   if (numIndices) *numIndices = Ni;
7342:   if (indices)    *indices    = idx;
7343:   return 0;
7344: }

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

7349:   Not collective

7351:   Input Parameters:
7352: + dm         - The DM
7353: . section    - The PetscSection describing the points (a local section)
7354: . idxSection - The PetscSection from which to obtain indices (may be local or global)
7355: . point      - The point defining the closure
7356: - useClPerm  - Use the closure point permutation if available

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

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

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

7373:   Level: advanced

7375: .seealso DMPlexGetClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure(), DMGetLocalSection(), DMGetGlobalSection()
7376: @*/
7377: PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection idxSection, PetscInt point, PetscBool useClPerm,
7378:                                            PetscInt *numIndices, PetscInt *indices[], PetscInt outOffsets[], PetscScalar *values[])
7379: {
7382:   DMRestoreWorkArray(dm, 0, MPIU_INT, indices);
7383:   return 0;
7384: }

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

7389:   Not collective

7391:   Input Parameters:
7392: + dm - The DM
7393: . section - The section describing the layout in v, or NULL to use the default section
7394: . globalSection - The section describing the layout in v, or NULL to use the default global section
7395: . A - The matrix
7396: . point - The point in the DM
7397: . values - The array of values
7398: - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions

7400:   Fortran Notes:
7401:   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.

7403:   Level: intermediate

7405: .seealso DMPlexMatSetClosureGeneral(), DMPlexVecGetClosure(), DMPlexVecSetClosure()
7406: @*/
7407: PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
7408: {
7409:   DM_Plex           *mesh = (DM_Plex*) dm->data;
7410:   PetscInt          *indices;
7411:   PetscInt           numIndices;
7412:   const PetscScalar *valuesOrig = values;
7413:   PetscErrorCode     ierr;

7416:   if (!section) DMGetLocalSection(dm, &section);
7418:   if (!globalSection) DMGetGlobalSection(dm, &globalSection);

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

7424:   if (mesh->printSetValues) DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);
7425:   MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
7426:   if (ierr) {
7427:     PetscMPIInt    rank;

7429:     MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);
7430:     (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);
7431:     DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);
7432:     DMPlexRestoreClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **) &values);
7433:     if (values != valuesOrig) DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values);
7434:     SETERRQ(PetscObjectComm((PetscObject)dm),ierr,"Not possible to set matrix values");
7435:   }
7436:   if (mesh->printFEM > 1) {
7437:     PetscInt i;
7438:     PetscPrintf(PETSC_COMM_SELF, "  Indices:");
7439:     for (i = 0; i < numIndices; ++i) PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);
7440:     PetscPrintf(PETSC_COMM_SELF, "\n");
7441:   }

7443:   DMPlexRestoreClosureIndices(dm, section, globalSection, point, PETSC_TRUE, &numIndices, &indices, NULL, (PetscScalar **) &values);
7444:   if (values != valuesOrig) DMRestoreWorkArray(dm, 0, MPIU_SCALAR, &values);
7445:   return 0;
7446: }

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

7451:   Not collective

7453:   Input Parameters:
7454: + dmRow - The DM for the row fields
7455: . sectionRow - The section describing the layout, or NULL to use the default section in dmRow
7456: . globalSectionRow - The section describing the layout, or NULL to use the default global section in dmRow
7457: . dmCol - The DM for the column fields
7458: . sectionCol - The section describing the layout, or NULL to use the default section in dmCol
7459: . globalSectionCol - The section describing the layout, or NULL to use the default global section in dmCol
7460: . A - The matrix
7461: . point - The point in the DMs
7462: . values - The array of values
7463: - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions

7465:   Level: intermediate

7467: .seealso DMPlexMatSetClosure(), DMPlexVecGetClosure(), DMPlexVecSetClosure()
7468: @*/
7469: PetscErrorCode DMPlexMatSetClosureGeneral(DM dmRow, PetscSection sectionRow, PetscSection globalSectionRow, DM dmCol, PetscSection sectionCol, PetscSection globalSectionCol, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
7470: {
7471:   DM_Plex           *mesh = (DM_Plex*) dmRow->data;
7472:   PetscInt          *indicesRow, *indicesCol;
7473:   PetscInt           numIndicesRow, numIndicesCol;
7474:   const PetscScalar *valuesOrig = values;
7475:   PetscErrorCode     ierr;

7478:   if (!sectionRow) DMGetLocalSection(dmRow, &sectionRow);
7480:   if (!globalSectionRow) DMGetGlobalSection(dmRow, &globalSectionRow);
7483:   if (!sectionCol) DMGetLocalSection(dmCol, &sectionCol);
7485:   if (!globalSectionCol) DMGetGlobalSection(dmCol, &globalSectionCol);

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

7492:   if (mesh->printSetValues) DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values);
7493:   MatSetValues(A, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values, mode);
7494:   if (ierr) {
7495:     PetscMPIInt    rank;

7497:     MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);
7498:     (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);
7499:     DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndicesRow, indicesRow, numIndicesCol, indicesCol, values);
7500:     DMPlexRestoreClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **) &values);
7501:     DMPlexRestoreClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesRow, NULL, (PetscScalar **) &values);
7502:     if (values != valuesOrig) DMRestoreWorkArray(dmRow, 0, MPIU_SCALAR, &values);
7503:     ierr;
7504:   }

7506:   DMPlexRestoreClosureIndices(dmRow, sectionRow, globalSectionRow, point, PETSC_TRUE, &numIndicesRow, &indicesRow, NULL, (PetscScalar **) &values);
7507:   DMPlexRestoreClosureIndices(dmCol, sectionCol, globalSectionCol, point, PETSC_TRUE, &numIndicesCol, &indicesCol, NULL, (PetscScalar **) &values);
7508:   if (values != valuesOrig) DMRestoreWorkArray(dmRow, 0, MPIU_SCALAR, &values);
7509:   return 0;
7510: }

7512: PetscErrorCode DMPlexMatSetClosureRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
7513: {
7514:   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
7515:   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
7516:   PetscInt       *cpoints = NULL;
7517:   PetscInt       *findices, *cindices;
7518:   const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
7519:   PetscInt        foffsets[32], coffsets[32];
7520:   DMPolytopeType  ct;
7521:   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
7522:   PetscErrorCode  ierr;

7526:   if (!fsection) DMGetLocalSection(dmf, &fsection);
7528:   if (!csection) DMGetLocalSection(dmc, &csection);
7530:   if (!globalFSection) DMGetGlobalSection(dmf, &globalFSection);
7532:   if (!globalCSection) DMGetGlobalSection(dmc, &globalCSection);
7535:   PetscSectionGetNumFields(fsection, &numFields);
7537:   PetscArrayzero(foffsets, 32);
7538:   PetscArrayzero(coffsets, 32);
7539:   /* Column indices */
7540:   DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);
7541:   maxFPoints = numCPoints;
7542:   /* Compress out points not in the section */
7543:   /*   TODO: Squeeze out points with 0 dof as well */
7544:   PetscSectionGetChart(csection, &pStart, &pEnd);
7545:   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
7546:     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
7547:       cpoints[q*2]   = cpoints[p];
7548:       cpoints[q*2+1] = cpoints[p+1];
7549:       ++q;
7550:     }
7551:   }
7552:   numCPoints = q;
7553:   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
7554:     PetscInt fdof;

7556:     PetscSectionGetDof(csection, cpoints[p], &dof);
7557:     if (!dof) continue;
7558:     for (f = 0; f < numFields; ++f) {
7559:       PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);
7560:       coffsets[f+1] += fdof;
7561:     }
7562:     numCIndices += dof;
7563:   }
7564:   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
7565:   /* Row indices */
7566:   DMPlexGetCellType(dmc, point, &ct);
7567:   {
7568:     DMPlexTransform tr;
7569:     DMPolytopeType *rct;
7570:     PetscInt       *rsize, *rcone, *rornt, Nt;

7572:     DMPlexTransformCreate(PETSC_COMM_SELF, &tr);
7573:     DMPlexTransformSetType(tr, DMPLEXREFINEREGULAR);
7574:     DMPlexTransformCellTransform(tr, ct, point, NULL, &Nt, &rct, &rsize, &rcone, &rornt);
7575:     numSubcells = rsize[Nt-1];
7576:     DMPlexTransformDestroy(&tr);
7577:   }
7578:   DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);
7579:   for (r = 0, q = 0; r < numSubcells; ++r) {
7580:     /* TODO Map from coarse to fine cells */
7581:     DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);
7582:     /* Compress out points not in the section */
7583:     PetscSectionGetChart(fsection, &pStart, &pEnd);
7584:     for (p = 0; p < numFPoints*2; p += 2) {
7585:       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
7586:         PetscSectionGetDof(fsection, fpoints[p], &dof);
7587:         if (!dof) continue;
7588:         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
7589:         if (s < q) continue;
7590:         ftotpoints[q*2]   = fpoints[p];
7591:         ftotpoints[q*2+1] = fpoints[p+1];
7592:         ++q;
7593:       }
7594:     }
7595:     DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);
7596:   }
7597:   numFPoints = q;
7598:   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
7599:     PetscInt fdof;

7601:     PetscSectionGetDof(fsection, ftotpoints[p], &dof);
7602:     if (!dof) continue;
7603:     for (f = 0; f < numFields; ++f) {
7604:       PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);
7605:       foffsets[f+1] += fdof;
7606:     }
7607:     numFIndices += dof;
7608:   }
7609:   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];

7613:   DMGetWorkArray(dmf, numFIndices, MPIU_INT, &findices);
7614:   DMGetWorkArray(dmc, numCIndices, MPIU_INT, &cindices);
7615:   if (numFields) {
7616:     const PetscInt **permsF[32] = {NULL};
7617:     const PetscInt **permsC[32] = {NULL};

7619:     for (f = 0; f < numFields; f++) {
7620:       PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);
7621:       PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);
7622:     }
7623:     for (p = 0; p < numFPoints; p++) {
7624:       PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);
7625:       DMPlexGetIndicesPointFields_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);
7626:     }
7627:     for (p = 0; p < numCPoints; p++) {
7628:       PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);
7629:       DMPlexGetIndicesPointFields_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);
7630:     }
7631:     for (f = 0; f < numFields; f++) {
7632:       PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);
7633:       PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);
7634:     }
7635:   } else {
7636:     const PetscInt **permsF = NULL;
7637:     const PetscInt **permsC = NULL;

7639:     PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);
7640:     PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);
7641:     for (p = 0, off = 0; p < numFPoints; p++) {
7642:       const PetscInt *perm = permsF ? permsF[p] : NULL;

7644:       PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);
7645:       DMPlexGetIndicesPoint_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);
7646:     }
7647:     for (p = 0, off = 0; p < numCPoints; p++) {
7648:       const PetscInt *perm = permsC ? permsC[p] : NULL;

7650:       PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);
7651:       DMPlexGetIndicesPoint_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);
7652:     }
7653:     PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);
7654:     PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);
7655:   }
7656:   if (mesh->printSetValues) DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);
7657:   /* TODO: flips */
7658:   MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
7659:   if (ierr) {
7660:     PetscMPIInt    rank;

7662:     MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);
7663:     (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);
7664:     DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);
7665:     DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);
7666:     DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);
7667:     ierr;
7668:   }
7669:   DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);
7670:   DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);
7671:   DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);
7672:   DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);
7673:   return 0;
7674: }

7676: PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
7677: {
7678:   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
7679:   PetscInt      *cpoints = NULL;
7680:   PetscInt       foffsets[32], coffsets[32];
7681:   const PetscInt *fclperm = NULL, *cclperm = NULL; /* Closure permutations cannot work here */
7682:   DMPolytopeType ct;
7683:   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;

7687:   if (!fsection) DMGetLocalSection(dmf, &fsection);
7689:   if (!csection) DMGetLocalSection(dmc, &csection);
7691:   if (!globalFSection) DMGetGlobalSection(dmf, &globalFSection);
7693:   if (!globalCSection) DMGetGlobalSection(dmc, &globalCSection);
7695:   PetscSectionGetNumFields(fsection, &numFields);
7697:   PetscArrayzero(foffsets, 32);
7698:   PetscArrayzero(coffsets, 32);
7699:   /* Column indices */
7700:   DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);
7701:   maxFPoints = numCPoints;
7702:   /* Compress out points not in the section */
7703:   /*   TODO: Squeeze out points with 0 dof as well */
7704:   PetscSectionGetChart(csection, &pStart, &pEnd);
7705:   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
7706:     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
7707:       cpoints[q*2]   = cpoints[p];
7708:       cpoints[q*2+1] = cpoints[p+1];
7709:       ++q;
7710:     }
7711:   }
7712:   numCPoints = q;
7713:   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
7714:     PetscInt fdof;

7716:     PetscSectionGetDof(csection, cpoints[p], &dof);
7717:     if (!dof) continue;
7718:     for (f = 0; f < numFields; ++f) {
7719:       PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);
7720:       coffsets[f+1] += fdof;
7721:     }
7722:     numCIndices += dof;
7723:   }
7724:   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
7725:   /* Row indices */
7726:   DMPlexGetCellType(dmc, point, &ct);
7727:   {
7728:     DMPlexTransform tr;
7729:     DMPolytopeType *rct;
7730:     PetscInt       *rsize, *rcone, *rornt, Nt;

7732:     DMPlexTransformCreate(PETSC_COMM_SELF, &tr);
7733:     DMPlexTransformSetType(tr, DMPLEXREFINEREGULAR);
7734:     DMPlexTransformCellTransform(tr, ct, point, NULL, &Nt, &rct, &rsize, &rcone, &rornt);
7735:     numSubcells = rsize[Nt-1];
7736:     DMPlexTransformDestroy(&tr);
7737:   }
7738:   DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);
7739:   for (r = 0, q = 0; r < numSubcells; ++r) {
7740:     /* TODO Map from coarse to fine cells */
7741:     DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);
7742:     /* Compress out points not in the section */
7743:     PetscSectionGetChart(fsection, &pStart, &pEnd);
7744:     for (p = 0; p < numFPoints*2; p += 2) {
7745:       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
7746:         PetscSectionGetDof(fsection, fpoints[p], &dof);
7747:         if (!dof) continue;
7748:         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
7749:         if (s < q) continue;
7750:         ftotpoints[q*2]   = fpoints[p];
7751:         ftotpoints[q*2+1] = fpoints[p+1];
7752:         ++q;
7753:       }
7754:     }
7755:     DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);
7756:   }
7757:   numFPoints = q;
7758:   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
7759:     PetscInt fdof;

7761:     PetscSectionGetDof(fsection, ftotpoints[p], &dof);
7762:     if (!dof) continue;
7763:     for (f = 0; f < numFields; ++f) {
7764:       PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);
7765:       foffsets[f+1] += fdof;
7766:     }
7767:     numFIndices += dof;
7768:   }
7769:   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];

7773:   if (numFields) {
7774:     const PetscInt **permsF[32] = {NULL};
7775:     const PetscInt **permsC[32] = {NULL};

7777:     for (f = 0; f < numFields; f++) {
7778:       PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);
7779:       PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);
7780:     }
7781:     for (p = 0; p < numFPoints; p++) {
7782:       PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);
7783:       DMPlexGetIndicesPointFields_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);
7784:     }
7785:     for (p = 0; p < numCPoints; p++) {
7786:       PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);
7787:       DMPlexGetIndicesPointFields_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);
7788:     }
7789:     for (f = 0; f < numFields; f++) {
7790:       PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);
7791:       PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);
7792:     }
7793:   } else {
7794:     const PetscInt **permsF = NULL;
7795:     const PetscInt **permsC = NULL;

7797:     PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);
7798:     PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);
7799:     for (p = 0, off = 0; p < numFPoints; p++) {
7800:       const PetscInt *perm = permsF ? permsF[p] : NULL;

7802:       PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);
7803:       DMPlexGetIndicesPoint_Internal(fsection, PETSC_FALSE, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);
7804:     }
7805:     for (p = 0, off = 0; p < numCPoints; p++) {
7806:       const PetscInt *perm = permsC ? permsC[p] : NULL;

7808:       PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);
7809:       DMPlexGetIndicesPoint_Internal(csection, PETSC_FALSE, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);
7810:     }
7811:     PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);
7812:     PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);
7813:   }
7814:   DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);
7815:   DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);
7816:   return 0;
7817: }

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

7822:   Input Parameter:
7823: . dm   - The DMPlex object

7825:   Output Parameter:
7826: . cellHeight - The height of a cell

7828:   Level: developer

7830: .seealso DMPlexSetVTKCellHeight()
7831: @*/
7832: PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
7833: {
7834:   DM_Plex *mesh = (DM_Plex*) dm->data;

7838:   *cellHeight = mesh->vtkCellHeight;
7839:   return 0;
7840: }

7842: /*@C
7843:   DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)

7845:   Input Parameters:
7846: + dm   - The DMPlex object
7847: - cellHeight - The height of a cell

7849:   Level: developer

7851: .seealso DMPlexGetVTKCellHeight()
7852: @*/
7853: PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
7854: {
7855:   DM_Plex *mesh = (DM_Plex*) dm->data;

7858:   mesh->vtkCellHeight = cellHeight;
7859:   return 0;
7860: }

7862: /*@
7863:   DMPlexGetGhostCellStratum - Get the range of cells which are used to enforce FV boundary conditions

7865:   Input Parameter:
7866: . dm - The DMPlex object

7868:   Output Parameters:
7869: + gcStart - The first ghost cell, or NULL
7870: - gcEnd   - The upper bound on ghost cells, or NULL

7872:   Level: advanced

7874: .seealso DMPlexConstructGhostCells(), DMPlexGetGhostCellStratum()
7875: @*/
7876: PetscErrorCode DMPlexGetGhostCellStratum(DM dm, PetscInt *gcStart, PetscInt *gcEnd)
7877: {
7878:   DMLabel        ctLabel;

7881:   DMPlexGetCellTypeLabel(dm, &ctLabel);
7882:   DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_FV_GHOST, gcStart, gcEnd);
7883:   return 0;
7884: }

7886: PetscErrorCode DMPlexCreateNumbering_Plex(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
7887: {
7888:   PetscSection   section, globalSection;
7889:   PetscInt      *numbers, p;

7891:   PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);
7892:   PetscSectionSetChart(section, pStart, pEnd);
7893:   for (p = pStart; p < pEnd; ++p) {
7894:     PetscSectionSetDof(section, p, 1);
7895:   }
7896:   PetscSectionSetUp(section);
7897:   PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);
7898:   PetscMalloc1(pEnd - pStart, &numbers);
7899:   for (p = pStart; p < pEnd; ++p) {
7900:     PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);
7901:     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
7902:     else                       numbers[p-pStart] += shift;
7903:   }
7904:   ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);
7905:   if (globalSize) {
7906:     PetscLayout layout;
7907:     PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);
7908:     PetscLayoutGetSize(layout, globalSize);
7909:     PetscLayoutDestroy(&layout);
7910:   }
7911:   PetscSectionDestroy(&section);
7912:   PetscSectionDestroy(&globalSection);
7913:   return 0;
7914: }

7916: PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
7917: {
7918:   PetscInt       cellHeight, cStart, cEnd;

7920:   DMPlexGetVTKCellHeight(dm, &cellHeight);
7921:   if (includeHybrid) DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);
7922:   else               DMPlexGetSimplexOrBoxCells(dm, cellHeight, &cStart, &cEnd);
7923:   DMPlexCreateNumbering_Plex(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);
7924:   return 0;
7925: }

7927: /*@
7928:   DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process

7930:   Input Parameter:
7931: . dm   - The DMPlex object

7933:   Output Parameter:
7934: . globalCellNumbers - Global cell numbers for all cells on this process

7936:   Level: developer

7938: .seealso DMPlexGetVertexNumbering()
7939: @*/
7940: PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
7941: {
7942:   DM_Plex       *mesh = (DM_Plex*) dm->data;

7945:   if (!mesh->globalCellNumbers) DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);
7946:   *globalCellNumbers = mesh->globalCellNumbers;
7947:   return 0;
7948: }

7950: PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
7951: {
7952:   PetscInt       vStart, vEnd;

7955:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
7956:   DMPlexCreateNumbering_Plex(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);
7957:   return 0;
7958: }

7960: /*@
7961:   DMPlexGetVertexNumbering - Get a global vertex numbering for all vertices on this process

7963:   Input Parameter:
7964: . dm   - The DMPlex object

7966:   Output Parameter:
7967: . globalVertexNumbers - Global vertex numbers for all vertices on this process

7969:   Level: developer

7971: .seealso DMPlexGetCellNumbering()
7972: @*/
7973: PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
7974: {
7975:   DM_Plex       *mesh = (DM_Plex*) dm->data;

7978:   if (!mesh->globalVertexNumbers) DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);
7979:   *globalVertexNumbers = mesh->globalVertexNumbers;
7980:   return 0;
7981: }

7983: /*@
7984:   DMPlexCreatePointNumbering - Create a global numbering for all points on this process

7986:   Input Parameter:
7987: . dm   - The DMPlex object

7989:   Output Parameter:
7990: . globalPointNumbers - Global numbers for all points on this process

7992:   Level: developer

7994: .seealso DMPlexGetCellNumbering()
7995: @*/
7996: PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
7997: {
7998:   IS             nums[4];
7999:   PetscInt       depths[4], gdepths[4], starts[4];
8000:   PetscInt       depth, d, shift = 0;

8003:   DMPlexGetDepth(dm, &depth);
8004:   /* For unstratified meshes use dim instead of depth */
8005:   if (depth < 0) DMGetDimension(dm, &depth);
8006:   for (d = 0; d <= depth; ++d) {
8007:     PetscInt end;

8009:     depths[d] = depth-d;
8010:     DMPlexGetDepthStratum(dm, depths[d], &starts[d], &end);
8011:     if (!(starts[d]-end)) { starts[d] = depths[d] = -1; }
8012:   }
8013:   PetscSortIntWithArray(depth+1, starts, depths);
8014:   MPIU_Allreduce(depths, gdepths, depth+1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm));
8015:   for (d = 0; d <= depth; ++d) {
8017:   }
8018:   for (d = 0; d <= depth; ++d) {
8019:     PetscInt pStart, pEnd, gsize;

8021:     DMPlexGetDepthStratum(dm, gdepths[d], &pStart, &pEnd);
8022:     DMPlexCreateNumbering_Plex(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);
8023:     shift += gsize;
8024:   }
8025:   ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);
8026:   for (d = 0; d <= depth; ++d) ISDestroy(&nums[d]);
8027:   return 0;
8028: }

8030: /*@
8031:   DMPlexCreateRankField - Create a cell field whose value is the rank of the owner

8033:   Input Parameter:
8034: . dm - The DMPlex object

8036:   Output Parameter:
8037: . ranks - The rank field

8039:   Options Database Keys:
8040: . -dm_partition_view - Adds the rank field into the DM output from -dm_view using the same viewer

8042:   Level: intermediate

8044: .seealso: DMView()
8045: @*/
8046: PetscErrorCode DMPlexCreateRankField(DM dm, Vec *ranks)
8047: {
8048:   DM             rdm;
8049:   PetscFE        fe;
8050:   PetscScalar   *r;
8051:   PetscMPIInt    rank;
8052:   DMPolytopeType ct;
8053:   PetscInt       dim, cStart, cEnd, c;
8054:   PetscBool      simplex;

8059:   MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);
8060:   DMClone(dm, &rdm);
8061:   DMGetDimension(rdm, &dim);
8062:   DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);
8063:   DMPlexGetCellType(dm, cStart, &ct);
8064:   simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
8065:   PetscFECreateDefault(PETSC_COMM_SELF, dim, 1, simplex, "PETSc___rank_", -1, &fe);
8066:   PetscObjectSetName((PetscObject) fe, "rank");
8067:   DMSetField(rdm, 0, NULL, (PetscObject) fe);
8068:   PetscFEDestroy(&fe);
8069:   DMCreateDS(rdm);
8070:   DMCreateGlobalVector(rdm, ranks);
8071:   PetscObjectSetName((PetscObject) *ranks, "partition");
8072:   VecGetArray(*ranks, &r);
8073:   for (c = cStart; c < cEnd; ++c) {
8074:     PetscScalar *lr;

8076:     DMPlexPointGlobalRef(rdm, c, r, &lr);
8077:     if (lr) *lr = rank;
8078:   }
8079:   VecRestoreArray(*ranks, &r);
8080:   DMDestroy(&rdm);
8081:   return 0;
8082: }

8084: /*@
8085:   DMPlexCreateLabelField - Create a cell field whose value is the label value for that cell

8087:   Input Parameters:
8088: + dm    - The DMPlex
8089: - label - The DMLabel

8091:   Output Parameter:
8092: . val - The label value field

8094:   Options Database Keys:
8095: . -dm_label_view - Adds the label value field into the DM output from -dm_view using the same viewer

8097:   Level: intermediate

8099: .seealso: DMView()
8100: @*/
8101: PetscErrorCode DMPlexCreateLabelField(DM dm, DMLabel label, Vec *val)
8102: {
8103:   DM             rdm;
8104:   PetscFE        fe;
8105:   PetscScalar   *v;
8106:   PetscInt       dim, cStart, cEnd, c;

8112:   DMClone(dm, &rdm);
8113:   DMGetDimension(rdm, &dim);
8114:   PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, "PETSc___label_value_", -1, &fe);
8115:   PetscObjectSetName((PetscObject) fe, "label_value");
8116:   DMSetField(rdm, 0, NULL, (PetscObject) fe);
8117:   PetscFEDestroy(&fe);
8118:   DMCreateDS(rdm);
8119:   DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);
8120:   DMCreateGlobalVector(rdm, val);
8121:   PetscObjectSetName((PetscObject) *val, "label_value");
8122:   VecGetArray(*val, &v);
8123:   for (c = cStart; c < cEnd; ++c) {
8124:     PetscScalar *lv;
8125:     PetscInt     cval;

8127:     DMPlexPointGlobalRef(rdm, c, v, &lv);
8128:     DMLabelGetValue(label, c, &cval);
8129:     *lv = cval;
8130:   }
8131:   VecRestoreArray(*val, &v);
8132:   DMDestroy(&rdm);
8133:   return 0;
8134: }

8136: /*@
8137:   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.

8139:   Input Parameter:
8140: . dm - The DMPlex object

8142:   Notes:
8143:   This is a useful diagnostic when creating meshes programmatically.

8145:   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().

8147:   Level: developer

8149: .seealso: DMCreate(), DMSetFromOptions()
8150: @*/
8151: PetscErrorCode DMPlexCheckSymmetry(DM dm)
8152: {
8153:   PetscSection    coneSection, supportSection;
8154:   const PetscInt *cone, *support;
8155:   PetscInt        coneSize, c, supportSize, s;
8156:   PetscInt        pStart, pEnd, p, pp, csize, ssize;
8157:   PetscBool       storagecheck = PETSC_TRUE;

8160:   DMViewFromOptions(dm, NULL, "-sym_dm_view");
8161:   DMPlexGetConeSection(dm, &coneSection);
8162:   DMPlexGetSupportSection(dm, &supportSection);
8163:   /* Check that point p is found in the support of its cone points, and vice versa */
8164:   DMPlexGetChart(dm, &pStart, &pEnd);
8165:   for (p = pStart; p < pEnd; ++p) {
8166:     DMPlexGetConeSize(dm, p, &coneSize);
8167:     DMPlexGetCone(dm, p, &cone);
8168:     for (c = 0; c < coneSize; ++c) {
8169:       PetscBool dup = PETSC_FALSE;
8170:       PetscInt  d;
8171:       for (d = c-1; d >= 0; --d) {
8172:         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
8173:       }
8174:       DMPlexGetSupportSize(dm, cone[c], &supportSize);
8175:       DMPlexGetSupport(dm, cone[c], &support);
8176:       for (s = 0; s < supportSize; ++s) {
8177:         if (support[s] == p) break;
8178:       }
8179:       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
8180:         PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);
8181:         for (s = 0; s < coneSize; ++s) {
8182:           PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);
8183:         }
8184:         PetscPrintf(PETSC_COMM_SELF, "\n");
8185:         PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);
8186:         for (s = 0; s < supportSize; ++s) {
8187:           PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);
8188:         }
8189:         PetscPrintf(PETSC_COMM_SELF, "\n");
8191:         else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
8192:       }
8193:     }
8194:     DMPlexGetTreeParent(dm, p, &pp, NULL);
8195:     if (p != pp) { storagecheck = PETSC_FALSE; continue; }
8196:     DMPlexGetSupportSize(dm, p, &supportSize);
8197:     DMPlexGetSupport(dm, p, &support);
8198:     for (s = 0; s < supportSize; ++s) {
8199:       DMPlexGetConeSize(dm, support[s], &coneSize);
8200:       DMPlexGetCone(dm, support[s], &cone);
8201:       for (c = 0; c < coneSize; ++c) {
8202:         DMPlexGetTreeParent(dm, cone[c], &pp, NULL);
8203:         if (cone[c] != pp) { c = 0; break; }
8204:         if (cone[c] == p) break;
8205:       }
8206:       if (c >= coneSize) {
8207:         PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);
8208:         for (c = 0; c < supportSize; ++c) {
8209:           PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);
8210:         }
8211:         PetscPrintf(PETSC_COMM_SELF, "\n");
8212:         PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);
8213:         for (c = 0; c < coneSize; ++c) {
8214:           PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);
8215:         }
8216:         PetscPrintf(PETSC_COMM_SELF, "\n");
8217:         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
8218:       }
8219:     }
8220:   }
8221:   if (storagecheck) {
8222:     PetscSectionGetStorageSize(coneSection, &csize);
8223:     PetscSectionGetStorageSize(supportSection, &ssize);
8225:   }
8226:   return 0;
8227: }

8229: /*
8230:   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.
8231: */
8232: static PetscErrorCode DMPlexCellUnsplitVertices_Private(DM dm, PetscInt c, DMPolytopeType ct, PetscInt *unsplit)
8233: {
8234:   DMPolytopeType  cct;
8235:   PetscInt        ptpoints[4];
8236:   const PetscInt *cone, *ccone, *ptcone;
8237:   PetscInt        coneSize, cp, cconeSize, ccp, npt = 0, pt;

8239:   *unsplit = 0;
8240:   switch (ct) {
8241:     case DM_POLYTOPE_POINT_PRISM_TENSOR:
8242:       ptpoints[npt++] = c;
8243:       break;
8244:     case DM_POLYTOPE_SEG_PRISM_TENSOR:
8245:       DMPlexGetCone(dm, c, &cone);
8246:       DMPlexGetConeSize(dm, c, &coneSize);
8247:       for (cp = 0; cp < coneSize; ++cp) {
8248:         DMPlexGetCellType(dm, cone[cp], &cct);
8249:         if (cct == DM_POLYTOPE_POINT_PRISM_TENSOR) ptpoints[npt++] = cone[cp];
8250:       }
8251:       break;
8252:     case DM_POLYTOPE_TRI_PRISM_TENSOR:
8253:     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
8254:       DMPlexGetCone(dm, c, &cone);
8255:       DMPlexGetConeSize(dm, c, &coneSize);
8256:       for (cp = 0; cp < coneSize; ++cp) {
8257:         DMPlexGetCone(dm, cone[cp], &ccone);
8258:         DMPlexGetConeSize(dm, cone[cp], &cconeSize);
8259:         for (ccp = 0; ccp < cconeSize; ++ccp) {
8260:           DMPlexGetCellType(dm, ccone[ccp], &cct);
8261:           if (cct == DM_POLYTOPE_POINT_PRISM_TENSOR) {
8262:             PetscInt p;
8263:             for (p = 0; p < npt; ++p) if (ptpoints[p] == ccone[ccp]) break;
8264:             if (p == npt) ptpoints[npt++] = ccone[ccp];
8265:           }
8266:         }
8267:       }
8268:       break;
8269:     default: break;
8270:   }
8271:   for (pt = 0; pt < npt; ++pt) {
8272:     DMPlexGetCone(dm, ptpoints[pt], &ptcone);
8273:     if (ptcone[0] == ptcone[1]) ++(*unsplit);
8274:   }
8275:   return 0;
8276: }

8278: /*@
8279:   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices

8281:   Input Parameters:
8282: + dm - The DMPlex object
8283: - cellHeight - Normally 0

8285:   Notes:
8286:   This is a useful diagnostic when creating meshes programmatically.
8287:   Currently applicable only to homogeneous simplex or tensor meshes.

8289:   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().

8291:   Level: developer

8293: .seealso: DMCreate(), DMSetFromOptions()
8294: @*/
8295: PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscInt cellHeight)
8296: {
8297:   DMPlexInterpolatedFlag interp;
8298:   DMPolytopeType         ct;
8299:   PetscInt               vStart, vEnd, cStart, cEnd, c;

8302:   DMPlexIsInterpolated(dm, &interp);
8303:   DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);
8304:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
8305:   for (c = cStart; c < cEnd; ++c) {
8306:     PetscInt *closure = NULL;
8307:     PetscInt  coneSize, closureSize, cl, Nv = 0;

8309:     DMPlexGetCellType(dm, c, &ct);
8311:     if (ct == DM_POLYTOPE_UNKNOWN) continue;
8312:     if (interp == DMPLEX_INTERPOLATED_FULL) {
8313:       DMPlexGetConeSize(dm, c, &coneSize);
8315:     }
8316:     DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
8317:     for (cl = 0; cl < closureSize*2; cl += 2) {
8318:       const PetscInt p = closure[cl];
8319:       if ((p >= vStart) && (p < vEnd)) ++Nv;
8320:     }
8321:     DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
8322:     /* Special Case: Tensor faces with identified vertices */
8323:     if (Nv < DMPolytopeTypeGetNumVertices(ct)) {
8324:       PetscInt unsplit;

8326:       DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);
8327:       if (Nv + unsplit == DMPolytopeTypeGetNumVertices(ct)) continue;
8328:     }
8330:   }
8331:   return 0;
8332: }

8334: /*@
8335:   DMPlexCheckFaces - Check that the faces of each cell give a vertex order this is consistent with what we expect from the cell type

8337:   Not Collective

8339:   Input Parameters:
8340: + dm - The DMPlex object
8341: - cellHeight - Normally 0

8343:   Notes:
8344:   This is a useful diagnostic when creating meshes programmatically.
8345:   This routine is only relevant for meshes that are fully interpolated across all ranks.
8346:   It will error out if a partially interpolated mesh is given on some rank.
8347:   It will do nothing for locally uninterpolated mesh (as there is nothing to check).

8349:   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().

8351:   Level: developer

8353: .seealso: DMCreate(), DMPlexGetVTKCellHeight(), DMSetFromOptions()
8354: @*/
8355: PetscErrorCode DMPlexCheckFaces(DM dm, PetscInt cellHeight)
8356: {
8357:   PetscInt       dim, depth, vStart, vEnd, cStart, cEnd, c, h;
8358:   DMPlexInterpolatedFlag interpEnum;

8361:   DMPlexIsInterpolated(dm, &interpEnum);
8362:   if (interpEnum == DMPLEX_INTERPOLATED_NONE) return 0;
8363:   if (interpEnum == DMPLEX_INTERPOLATED_PARTIAL) {
8364:     PetscMPIInt rank;
8365:     MPI_Comm    comm;

8367:     PetscObjectGetComm((PetscObject) dm, &comm);
8368:     MPI_Comm_rank(comm, &rank);
8369:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Mesh is only partially interpolated on rank %d, this is currently not supported", rank);
8370:   }

8372:   DMGetDimension(dm, &dim);
8373:   DMPlexGetDepth(dm, &depth);
8374:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
8375:   for (h = cellHeight; h < PetscMin(depth, dim); ++h) {
8376:     DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);
8377:     for (c = cStart; c < cEnd; ++c) {
8378:       const PetscInt      *cone, *ornt, *faceSizes, *faces;
8379:       const DMPolytopeType *faceTypes;
8380:       DMPolytopeType        ct;
8381:       PetscInt              numFaces, coneSize, f;
8382:       PetscInt             *closure = NULL, closureSize, cl, numCorners = 0, fOff = 0, unsplit;

8384:       DMPlexGetCellType(dm, c, &ct);
8385:       DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);
8386:       if (unsplit) continue;
8387:       DMPlexGetConeSize(dm, c, &coneSize);
8388:       DMPlexGetCone(dm, c, &cone);
8389:       DMPlexGetConeOrientation(dm, c, &ornt);
8390:       DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
8391:       for (cl = 0; cl < closureSize*2; cl += 2) {
8392:         const PetscInt p = closure[cl];
8393:         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
8394:       }
8395:       DMPlexGetRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces);
8397:       for (f = 0; f < numFaces; ++f) {
8398:         DMPolytopeType fct;
8399:         PetscInt       *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;

8401:         DMPlexGetCellType(dm, cone[f], &fct);
8402:         DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);
8403:         for (cl = 0; cl < fclosureSize*2; cl += 2) {
8404:           const PetscInt p = fclosure[cl];
8405:           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
8406:         }
8408:         for (v = 0; v < fnumCorners; ++v) {
8409:           if (fclosure[v] != faces[fOff+v]) {
8410:             PetscInt v1;

8412:             PetscPrintf(PETSC_COMM_SELF, "face closure:");
8413:             for (v1 = 0; v1 < fnumCorners; ++v1) PetscPrintf(PETSC_COMM_SELF, " %D", fclosure[v1]);
8414:             PetscPrintf(PETSC_COMM_SELF, "\ncell face:");
8415:             for (v1 = 0; v1 < fnumCorners; ++v1) PetscPrintf(PETSC_COMM_SELF, " %D", faces[fOff+v1]);
8416:             PetscPrintf(PETSC_COMM_SELF, "\n");
8417:             SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D of type %s (cone idx %d, ornt %D) of cell %D of type %s vertex %D, %D != %D", cone[f], DMPolytopeTypes[fct], f, ornt[f], c, DMPolytopeTypes[ct], v, fclosure[v], faces[fOff+v]);
8418:           }
8419:         }
8420:         DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);
8421:         fOff += faceSizes[f];
8422:       }
8423:       DMPlexRestoreRawFaces_Internal(dm, ct, closure, &numFaces, &faceTypes, &faceSizes, &faces);
8424:       DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
8425:     }
8426:   }
8427:   return 0;
8428: }

8430: /*@
8431:   DMPlexCheckGeometry - Check the geometry of mesh cells

8433:   Input Parameter:
8434: . dm - The DMPlex object

8436:   Notes:
8437:   This is a useful diagnostic when creating meshes programmatically.

8439:   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().

8441:   Level: developer

8443: .seealso: DMCreate(), DMSetFromOptions()
8444: @*/
8445: PetscErrorCode DMPlexCheckGeometry(DM dm)
8446: {
8447:   Vec            coordinates;
8448:   PetscReal      detJ, J[9], refVol = 1.0;
8449:   PetscReal      vol;
8450:   PetscBool      periodic;
8451:   PetscInt       dim, depth, dE, d, cStart, cEnd, c;

8453:   DMGetDimension(dm, &dim);
8454:   DMGetCoordinateDim(dm, &dE);
8455:   if (dim != dE) return 0;
8456:   DMPlexGetDepth(dm, &depth);
8457:   DMGetPeriodicity(dm, &periodic, NULL, NULL, NULL);
8458:   for (d = 0; d < dim; ++d) refVol *= 2.0;
8459:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
8460:   /* Make sure local coordinates are created, because that step is collective */
8461:   DMGetCoordinatesLocal(dm, &coordinates);
8462:   for (c = cStart; c < cEnd; ++c) {
8463:     DMPolytopeType ct;
8464:     PetscInt       unsplit;
8465:     PetscBool      ignoreZeroVol = PETSC_FALSE;

8467:     DMPlexGetCellType(dm, c, &ct);
8468:     switch (ct) {
8469:       case DM_POLYTOPE_SEG_PRISM_TENSOR:
8470:       case DM_POLYTOPE_TRI_PRISM_TENSOR:
8471:       case DM_POLYTOPE_QUAD_PRISM_TENSOR:
8472:         ignoreZeroVol = PETSC_TRUE; break;
8473:       default: break;
8474:     }
8475:     switch (ct) {
8476:       case DM_POLYTOPE_TRI_PRISM:
8477:       case DM_POLYTOPE_TRI_PRISM_TENSOR:
8478:       case DM_POLYTOPE_QUAD_PRISM_TENSOR:
8479:       case DM_POLYTOPE_PYRAMID:
8480:         continue;
8481:       default: break;
8482:     }
8483:     DMPlexCellUnsplitVertices_Private(dm, c, ct, &unsplit);
8484:     if (unsplit) continue;
8485:     DMPlexComputeCellGeometryFEM(dm, c, NULL, NULL, J, NULL, &detJ);
8487:     PetscInfo(dm, "Cell %D FEM Volume %g\n", c, (double) detJ*refVol);
8488:     if (depth > 1 && !periodic) {
8489:       DMPlexComputeCellGeometryFVM(dm, c, &vol, NULL, NULL);
8491:       PetscInfo(dm, "Cell %D FVM Volume %g\n", c, (double) vol);
8492:     }
8493:   }
8494:   return 0;
8495: }

8497: /*@
8498:   DMPlexCheckPointSF - Check that several necessary conditions are met for the point SF of this plex.

8500:   Input Parameters:
8501: . dm - The DMPlex object

8503:   Notes:
8504:   This is mainly intended for debugging/testing purposes.
8505:   It currently checks only meshes with no partition overlapping.

8507:   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().

8509:   Level: developer

8511: .seealso: DMGetPointSF(), DMSetFromOptions()
8512: @*/
8513: PetscErrorCode DMPlexCheckPointSF(DM dm)
8514: {
8515:   PetscSF         pointSF;
8516:   PetscInt        cellHeight, cStart, cEnd, l, nleaves, nroots, overlap;
8517:   const PetscInt *locals, *rootdegree;
8518:   PetscBool       distributed;

8521:   DMGetPointSF(dm, &pointSF);
8522:   DMPlexIsDistributed(dm, &distributed);
8523:   if (!distributed) return 0;
8524:   DMPlexGetOverlap(dm, &overlap);
8525:   if (overlap) {
8526:     PetscPrintf(PetscObjectComm((PetscObject)dm), "Warning: DMPlexCheckPointSF() is currently not implemented for meshes with partition overlapping");
8527:     return 0;
8528:   }
8530:   PetscSFGetGraph(pointSF, &nroots, &nleaves, &locals, NULL);
8532:   PetscSFComputeDegreeBegin(pointSF, &rootdegree);
8533:   PetscSFComputeDegreeEnd(pointSF, &rootdegree);

8535:   /* 1) check there are no faces in 2D, cells in 3D, in interface */
8536:   DMPlexGetVTKCellHeight(dm, &cellHeight);
8537:   DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);
8538:   for (l = 0; l < nleaves; ++l) {
8539:     const PetscInt point = locals[l];

8542:   }

8544:   /* 2) if some point is in interface, then all its cone points must be also in interface (either as leaves or roots) */
8545:   for (l = 0; l < nleaves; ++l) {
8546:     const PetscInt  point = locals[l];
8547:     const PetscInt *cone;
8548:     PetscInt        coneSize, c, idx;

8550:     DMPlexGetConeSize(dm, point, &coneSize);
8551:     DMPlexGetCone(dm, point, &cone);
8552:     for (c = 0; c < coneSize; ++c) {
8553:       if (!rootdegree[cone[c]]) {
8554:         PetscFindInt(cone[c], nleaves, locals, &idx);
8556:       }
8557:     }
8558:   }
8559:   return 0;
8560: }

8562: PetscErrorCode DMPlexCheckAll_Internal(DM dm, PetscInt cellHeight)
8563: {
8564:   DMPlexCheckSymmetry(dm);
8565:   DMPlexCheckSkeleton(dm, cellHeight);
8566:   DMPlexCheckFaces(dm, cellHeight);
8567:   DMPlexCheckGeometry(dm);
8568:   DMPlexCheckPointSF(dm);
8569:   DMPlexCheckInterfaceCones(dm);
8570:   return 0;
8571: }

8573: typedef struct cell_stats
8574: {
8575:   PetscReal min, max, sum, squaresum;
8576:   PetscInt  count;
8577: } cell_stats_t;

8579: static void MPIAPI cell_stats_reduce(void *a, void *b, int * len, MPI_Datatype *datatype)
8580: {
8581:   PetscInt i, N = *len;

8583:   for (i = 0; i < N; i++) {
8584:     cell_stats_t *A = (cell_stats_t *) a;
8585:     cell_stats_t *B = (cell_stats_t *) b;

8587:     B->min = PetscMin(A->min,B->min);
8588:     B->max = PetscMax(A->max,B->max);
8589:     B->sum += A->sum;
8590:     B->squaresum += A->squaresum;
8591:     B->count += A->count;
8592:   }
8593: }

8595: /*@
8596:   DMPlexCheckCellShape - Checks the Jacobian of the mapping from reference to real cells and computes some minimal statistics.

8598:   Collective on dm

8600:   Input Parameters:
8601: + dm        - The DMPlex object
8602: . output    - If true, statistics will be displayed on stdout
8603: - condLimit - Display all cells above this condition number, or PETSC_DETERMINE for no cell output

8605:   Notes:
8606:   This is mainly intended for debugging/testing purposes.

8608:   For the complete list of DMPlexCheck* functions, see DMSetFromOptions().

8610:   Level: developer

8612: .seealso: DMSetFromOptions(), DMPlexComputeOrthogonalQuality()
8613: @*/
8614: PetscErrorCode DMPlexCheckCellShape(DM dm, PetscBool output, PetscReal condLimit)
8615: {
8616:   DM             dmCoarse;
8617:   cell_stats_t   stats, globalStats;
8618:   MPI_Comm       comm = PetscObjectComm((PetscObject)dm);
8619:   PetscReal      *J, *invJ, min = 0, max = 0, mean = 0, stdev = 0;
8620:   PetscReal      limit = condLimit > 0 ? condLimit : PETSC_MAX_REAL;
8621:   PetscInt       cdim, cStart, cEnd, c, eStart, eEnd, count = 0;
8622:   PetscMPIInt    rank,size;

8625:   stats.min   = PETSC_MAX_REAL;
8626:   stats.max   = PETSC_MIN_REAL;
8627:   stats.sum   = stats.squaresum = 0.;
8628:   stats.count = 0;

8630:   MPI_Comm_size(comm, &size);
8631:   MPI_Comm_rank(comm, &rank);
8632:   DMGetCoordinateDim(dm,&cdim);
8633:   PetscMalloc2(PetscSqr(cdim), &J, PetscSqr(cdim), &invJ);
8634:   DMPlexGetSimplexOrBoxCells(dm,0,&cStart,&cEnd);
8635:   DMPlexGetDepthStratum(dm,1,&eStart,&eEnd);
8636:   for (c = cStart; c < cEnd; c++) {
8637:     PetscInt  i;
8638:     PetscReal frobJ = 0., frobInvJ = 0., cond2, cond, detJ;

8640:     DMPlexComputeCellGeometryAffineFEM(dm,c,NULL,J,invJ,&detJ);
8642:     for (i = 0; i < PetscSqr(cdim); ++i) {
8643:       frobJ    += J[i] * J[i];
8644:       frobInvJ += invJ[i] * invJ[i];
8645:     }
8646:     cond2 = frobJ * frobInvJ;
8647:     cond  = PetscSqrtReal(cond2);

8649:     stats.min        = PetscMin(stats.min,cond);
8650:     stats.max        = PetscMax(stats.max,cond);
8651:     stats.sum       += cond;
8652:     stats.squaresum += cond2;
8653:     stats.count++;
8654:     if (output && cond > limit) {
8655:       PetscSection coordSection;
8656:       Vec          coordsLocal;
8657:       PetscScalar *coords = NULL;
8658:       PetscInt     Nv, d, clSize, cl, *closure = NULL;

8660:       DMGetCoordinatesLocal(dm, &coordsLocal);
8661:       DMGetCoordinateSection(dm, &coordSection);
8662:       DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);
8663:       PetscSynchronizedPrintf(comm, "[%d] Cell %D cond %g\n", rank, c, (double) cond);
8664:       for (i = 0; i < Nv/cdim; ++i) {
8665:         PetscSynchronizedPrintf(comm, "  Vertex %D: (", i);
8666:         for (d = 0; d < cdim; ++d) {
8667:           if (d > 0) PetscSynchronizedPrintf(comm, ", ");
8668:           PetscSynchronizedPrintf(comm, "%g", (double) PetscRealPart(coords[i*cdim+d]));
8669:         }
8670:         PetscSynchronizedPrintf(comm, ")\n");
8671:       }
8672:       DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);
8673:       for (cl = 0; cl < clSize*2; cl += 2) {
8674:         const PetscInt edge = closure[cl];

8676:         if ((edge >= eStart) && (edge < eEnd)) {
8677:           PetscReal len;

8679:           DMPlexComputeCellGeometryFVM(dm, edge, &len, NULL, NULL);
8680:           PetscSynchronizedPrintf(comm, "  Edge %D: length %g\n", edge, (double) len);
8681:         }
8682:       }
8683:       DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);
8684:       DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);
8685:     }
8686:   }
8687:   if (output) PetscSynchronizedFlush(comm, NULL);

8689:   if (size > 1) {
8690:     PetscMPIInt   blockLengths[2] = {4,1};
8691:     MPI_Aint      blockOffsets[2] = {offsetof(cell_stats_t,min),offsetof(cell_stats_t,count)};
8692:     MPI_Datatype  blockTypes[2]   = {MPIU_REAL,MPIU_INT}, statType;
8693:     MPI_Op        statReduce;

8695:     MPI_Type_create_struct(2,blockLengths,blockOffsets,blockTypes,&statType);
8696:     MPI_Type_commit(&statType);
8697:     MPI_Op_create(cell_stats_reduce, PETSC_TRUE, &statReduce);
8698:     MPI_Reduce(&stats,&globalStats,1,statType,statReduce,0,comm);
8699:     MPI_Op_free(&statReduce);
8700:     MPI_Type_free(&statType);
8701:   } else {
8702:     PetscArraycpy(&globalStats,&stats,1);
8703:   }
8704:   if (rank == 0) {
8705:     count = globalStats.count;
8706:     min   = globalStats.min;
8707:     max   = globalStats.max;
8708:     mean  = globalStats.sum / globalStats.count;
8709:     stdev = globalStats.count > 1 ? PetscSqrtReal(PetscMax((globalStats.squaresum - globalStats.count * mean * mean) / (globalStats.count - 1),0)) : 0.0;
8710:   }

8712:   if (output) {
8713:     PetscPrintf(comm,"Mesh with %D cells, shape condition numbers: min = %g, max = %g, mean = %g, stddev = %g\n", count, (double) min, (double) max, (double) mean, (double) stdev);
8714:   }
8715:   PetscFree2(J,invJ);

8717:   DMGetCoarseDM(dm,&dmCoarse);
8718:   if (dmCoarse) {
8719:     PetscBool isplex;

8721:     PetscObjectTypeCompare((PetscObject)dmCoarse,DMPLEX,&isplex);
8722:     if (isplex) {
8723:       DMPlexCheckCellShape(dmCoarse,output,condLimit);
8724:     }
8725:   }
8726:   return 0;
8727: }

8729: /*@
8730:   DMPlexComputeOrthogonalQuality - Compute cell-wise orthogonal quality mesh statistic. Optionally tags all cells with
8731:   orthogonal quality below given tolerance.

8733:   Collective on dm

8735:   Input Parameters:
8736: + dm   - The DMPlex object
8737: . fv   - Optional PetscFV object for pre-computed cell/face centroid information
8738: - atol - [0, 1] Absolute tolerance for tagging cells.

8740:   Output Parameters:
8741: + OrthQual      - Vec containing orthogonal quality per cell
8742: - OrthQualLabel - DMLabel tagging cells below atol with DM_ADAPT_REFINE

8744:   Options Database Keys:
8745: + -dm_plex_orthogonal_quality_label_view - view OrthQualLabel if label is requested. Currently only PETSCVIEWERASCII is
8746: supported.
8747: - -dm_plex_orthogonal_quality_vec_view - view OrthQual vector.

8749:   Notes:
8750:   Orthogonal quality is given by the following formula:

8752:   \min \left[ \frac{A_i \cdot f_i}{\|A_i\| \|f_i\|} , \frac{A_i \cdot c_i}{\|A_i\| \|c_i\|} \right]

8754:   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
8755:   is the vector from the current cells centroid to the centroid of its i'th neighbor (which shares a face with the
8756:   current cell). This computes the vector similarity between each cell face and its corresponding neighbor centroid by
8757:   calculating the cosine of the angle between these vectors.

8759:   Orthogonal quality ranges from 1 (best) to 0 (worst).

8761:   This routine is mainly useful for FVM, however is not restricted to only FVM. The PetscFV object is optionally used to check for
8762:   pre-computed FVM cell data, but if it is not passed in then this data will be computed.

8764:   Cells are tagged if they have an orthogonal quality less than or equal to the absolute tolerance.

8766:   Level: intermediate

8768: .seealso: DMPlexCheckCellShape(), DMCreateLabel()
8769: @*/
8770: PetscErrorCode DMPlexComputeOrthogonalQuality(DM dm, PetscFV fv, PetscReal atol, Vec *OrthQual, DMLabel *OrthQualLabel)
8771: {
8772:   PetscInt                nc, cellHeight, cStart, cEnd, cell, cellIter = 0;
8773:   PetscInt                *idx;
8774:   PetscScalar             *oqVals;
8775:   const PetscScalar       *cellGeomArr, *faceGeomArr;
8776:   PetscReal               *ci, *fi, *Ai;
8777:   MPI_Comm                comm;
8778:   Vec                     cellgeom, facegeom;
8779:   DM                      dmFace, dmCell;
8780:   IS                      glob;
8781:   ISLocalToGlobalMapping  ltog;
8782:   PetscViewer             vwr;

8788:   PetscObjectGetComm((PetscObject) dm, &comm);
8789:   DMGetDimension(dm, &nc);
8791:   {
8792:     DMPlexInterpolatedFlag interpFlag;

8794:     DMPlexIsInterpolated(dm, &interpFlag);
8795:     if (interpFlag != DMPLEX_INTERPOLATED_FULL) {
8796:       PetscMPIInt rank;

8798:       MPI_Comm_rank(comm, &rank);
8799:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM must be fully interpolated, DM on rank %d is not fully interpolated", rank);
8800:     }
8801:   }
8802:   if (OrthQualLabel) {
8804:     DMCreateLabel(dm, "Orthogonal_Quality");
8805:     DMGetLabel(dm, "Orthogonal_Quality", OrthQualLabel);
8806:   } else {*OrthQualLabel = NULL;}
8807:   DMPlexGetVTKCellHeight(dm, &cellHeight);
8808:   DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);
8809:   DMPlexCreateCellNumbering_Internal(dm, PETSC_TRUE, &glob);
8810:   ISLocalToGlobalMappingCreateIS(glob, &ltog);
8811:   ISLocalToGlobalMappingSetType(ltog, ISLOCALTOGLOBALMAPPINGHASH);
8812:   VecCreate(comm, OrthQual);
8813:   VecSetType(*OrthQual, VECSTANDARD);
8814:   VecSetSizes(*OrthQual, cEnd-cStart, PETSC_DETERMINE);
8815:   VecSetLocalToGlobalMapping(*OrthQual, ltog);
8816:   VecSetUp(*OrthQual);
8817:   ISDestroy(&glob);
8818:   ISLocalToGlobalMappingDestroy(&ltog);
8819:   DMPlexGetDataFVM(dm, fv, &cellgeom, &facegeom, NULL);
8820:   VecGetArrayRead(cellgeom, &cellGeomArr);
8821:   VecGetArrayRead(facegeom, &faceGeomArr);
8822:   VecGetDM(cellgeom, &dmCell);
8823:   VecGetDM(facegeom, &dmFace);
8824:   PetscMalloc5(cEnd-cStart, &idx, cEnd-cStart, &oqVals, nc, &ci, nc, &fi, nc, &Ai);
8825:   for (cell = cStart; cell < cEnd; cellIter++,cell++) {
8826:     PetscInt           cellneigh, cellneighiter = 0, adjSize = PETSC_DETERMINE;
8827:     PetscInt           cellarr[2], *adj = NULL;
8828:     PetscScalar        *cArr, *fArr;
8829:     PetscReal          minvalc = 1.0, minvalf = 1.0;
8830:     PetscFVCellGeom    *cg;

8832:     idx[cellIter] = cell-cStart;
8833:     cellarr[0] = cell;
8834:     /* Make indexing into cellGeom easier */
8835:     DMPlexPointLocalRead(dmCell, cell, cellGeomArr, &cg);
8836:     DMPlexGetAdjacency_Internal(dm, cell, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &adjSize, &adj);
8837:     /* Technically 1 too big, but easier than fiddling with empty adjacency array */
8838:     PetscCalloc2(adjSize, &cArr, adjSize, &fArr);
8839:     for (cellneigh = 0; cellneigh < adjSize; cellneighiter++,cellneigh++) {
8840:       PetscInt         i;
8841:       const PetscInt   neigh = adj[cellneigh];
8842:       PetscReal        normci = 0, normfi = 0, normai = 0;
8843:       PetscFVCellGeom  *cgneigh;
8844:       PetscFVFaceGeom  *fg;

8846:       /* Don't count ourselves in the neighbor list */
8847:       if (neigh == cell) continue;
8848:       DMPlexPointLocalRead(dmCell, neigh, cellGeomArr, &cgneigh);
8849:       cellarr[1] = neigh;
8850:       {
8851:         PetscInt       numcovpts;
8852:         const PetscInt *covpts;

8854:         DMPlexGetMeet(dm, 2, cellarr, &numcovpts, &covpts);
8855:         DMPlexPointLocalRead(dmFace, covpts[0], faceGeomArr, &fg);
8856:         DMPlexRestoreMeet(dm, 2, cellarr, &numcovpts, &covpts);
8857:       }

8859:       /* Compute c_i, f_i and their norms */
8860:       for (i = 0; i < nc; i++) {
8861:         ci[i] = cgneigh->centroid[i] - cg->centroid[i];
8862:         fi[i] = fg->centroid[i] - cg->centroid[i];
8863:         Ai[i] = fg->normal[i];
8864:         normci += PetscPowReal(ci[i], 2);
8865:         normfi += PetscPowReal(fi[i], 2);
8866:         normai += PetscPowReal(Ai[i], 2);
8867:       }
8868:       normci = PetscSqrtReal(normci);
8869:       normfi = PetscSqrtReal(normfi);
8870:       normai = PetscSqrtReal(normai);

8872:       /* Normalize and compute for each face-cell-normal pair */
8873:       for (i = 0; i < nc; i++) {
8874:         ci[i] = ci[i]/normci;
8875:         fi[i] = fi[i]/normfi;
8876:         Ai[i] = Ai[i]/normai;
8877:         /* PetscAbs because I don't know if normals are guaranteed to point out */
8878:         cArr[cellneighiter] += PetscAbs(Ai[i]*ci[i]);
8879:         fArr[cellneighiter] += PetscAbs(Ai[i]*fi[i]);
8880:       }
8881:       if (PetscRealPart(cArr[cellneighiter]) < minvalc) {
8882:         minvalc = PetscRealPart(cArr[cellneighiter]);
8883:       }
8884:       if (PetscRealPart(fArr[cellneighiter]) < minvalf) {
8885:         minvalf = PetscRealPart(fArr[cellneighiter]);
8886:       }
8887:     }
8888:     PetscFree(adj);
8889:     PetscFree2(cArr, fArr);
8890:     /* Defer to cell if they're equal */
8891:     oqVals[cellIter] = PetscMin(minvalf, minvalc);
8892:     if (OrthQualLabel) {
8893:       if (PetscRealPart(oqVals[cellIter]) <= atol) DMLabelSetValue(*OrthQualLabel, cell, DM_ADAPT_REFINE);
8894:     }
8895:   }
8896:   VecSetValuesLocal(*OrthQual, cEnd-cStart, idx, oqVals, INSERT_VALUES);
8897:   VecAssemblyBegin(*OrthQual);
8898:   VecAssemblyEnd(*OrthQual);
8899:   VecRestoreArrayRead(cellgeom, &cellGeomArr);
8900:   VecRestoreArrayRead(facegeom, &faceGeomArr);
8901:   PetscOptionsGetViewer(comm, NULL, NULL, "-dm_plex_orthogonal_quality_label_view", &vwr, NULL, NULL);
8902:   if (OrthQualLabel) {
8903:     if (vwr) DMLabelView(*OrthQualLabel, vwr);
8904:   }
8905:   PetscFree5(idx, oqVals, ci, fi, Ai);
8906:   PetscViewerDestroy(&vwr);
8907:   VecViewFromOptions(*OrthQual, NULL, "-dm_plex_orthogonal_quality_vec_view");
8908:   return 0;
8909: }

8911: /* this is here insead of DMGetOutputDM because output DM still has constraints in the local indices that affect
8912:  * interpolator construction */
8913: static PetscErrorCode DMGetFullDM(DM dm, DM *odm)
8914: {
8915:   PetscSection   section, newSection, gsection;
8916:   PetscSF        sf;
8917:   PetscBool      hasConstraints, ghasConstraints;

8921:   DMGetLocalSection(dm, &section);
8922:   PetscSectionHasConstraints(section, &hasConstraints);
8923:   MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));
8924:   if (!ghasConstraints) {
8925:     PetscObjectReference((PetscObject)dm);
8926:     *odm = dm;
8927:     return 0;
8928:   }
8929:   DMClone(dm, odm);
8930:   DMCopyFields(dm, *odm);
8931:   DMGetLocalSection(*odm, &newSection);
8932:   DMGetPointSF(*odm, &sf);
8933:   PetscSectionCreateGlobalSection(newSection, sf, PETSC_TRUE, PETSC_FALSE, &gsection);
8934:   DMSetGlobalSection(*odm, gsection);
8935:   PetscSectionDestroy(&gsection);
8936:   return 0;
8937: }

8939: static PetscErrorCode DMCreateAffineInterpolationCorrection_Plex(DM dmc, DM dmf, Vec *shift)
8940: {
8941:   DM             dmco, dmfo;
8942:   Mat            interpo;
8943:   Vec            rscale;
8944:   Vec            cglobalo, clocal;
8945:   Vec            fglobal, fglobalo, flocal;
8946:   PetscBool      regular;

8948:   DMGetFullDM(dmc, &dmco);
8949:   DMGetFullDM(dmf, &dmfo);
8950:   DMSetCoarseDM(dmfo, dmco);
8951:   DMPlexGetRegularRefinement(dmf, &regular);
8952:   DMPlexSetRegularRefinement(dmfo, regular);
8953:   DMCreateInterpolation(dmco, dmfo, &interpo, &rscale);
8954:   DMCreateGlobalVector(dmco, &cglobalo);
8955:   DMCreateLocalVector(dmc, &clocal);
8956:   VecSet(cglobalo, 0.);
8957:   VecSet(clocal, 0.);
8958:   DMCreateGlobalVector(dmf, &fglobal);
8959:   DMCreateGlobalVector(dmfo, &fglobalo);
8960:   DMCreateLocalVector(dmf, &flocal);
8961:   VecSet(fglobal, 0.);
8962:   VecSet(fglobalo, 0.);
8963:   VecSet(flocal, 0.);
8964:   DMPlexInsertBoundaryValues(dmc, PETSC_TRUE, clocal, 0., NULL, NULL, NULL);
8965:   DMLocalToGlobalBegin(dmco, clocal, INSERT_VALUES, cglobalo);
8966:   DMLocalToGlobalEnd(dmco, clocal, INSERT_VALUES, cglobalo);
8967:   MatMult(interpo, cglobalo, fglobalo);
8968:   DMGlobalToLocalBegin(dmfo, fglobalo, INSERT_VALUES, flocal);
8969:   DMGlobalToLocalEnd(dmfo, fglobalo, INSERT_VALUES, flocal);
8970:   DMLocalToGlobalBegin(dmf, flocal, INSERT_VALUES, fglobal);
8971:   DMLocalToGlobalEnd(dmf, flocal, INSERT_VALUES, fglobal);
8972:   *shift = fglobal;
8973:   VecDestroy(&flocal);
8974:   VecDestroy(&fglobalo);
8975:   VecDestroy(&clocal);
8976:   VecDestroy(&cglobalo);
8977:   VecDestroy(&rscale);
8978:   MatDestroy(&interpo);
8979:   DMDestroy(&dmfo);
8980:   DMDestroy(&dmco);
8981:   return 0;
8982: }

8984: PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
8985: {
8986:   PetscObject    shifto;
8987:   Vec            shift;

8989:   if (!interp) {
8990:     Vec rscale;

8992:     DMCreateInterpolation(coarse, fine, &interp, &rscale);
8993:     VecDestroy(&rscale);
8994:   } else {
8995:     PetscObjectReference((PetscObject)interp);
8996:   }
8997:   PetscObjectQuery((PetscObject)interp, "_DMInterpolateSolution_Plex_Vec", &shifto);
8998:   if (!shifto) {
8999:     DMCreateAffineInterpolationCorrection_Plex(coarse, fine, &shift);
9000:     PetscObjectCompose((PetscObject)interp, "_DMInterpolateSolution_Plex_Vec", (PetscObject) shift);
9001:     shifto = (PetscObject) shift;
9002:     VecDestroy(&shift);
9003:   }
9004:   shift = (Vec) shifto;
9005:   MatInterpolate(interp, coarseSol, fineSol);
9006:   VecAXPY(fineSol, 1.0, shift);
9007:   MatDestroy(&interp);
9008:   return 0;
9009: }

9011: /* Pointwise interpolation
9012:      Just code FEM for now
9013:      u^f = I u^c
9014:      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
9015:      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
9016:      I_{ij} = psi^f_i phi^c_j
9017: */
9018: PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
9019: {
9020:   PetscSection   gsc, gsf;
9021:   PetscInt       m, n;
9022:   void          *ctx;
9023:   DM             cdm;
9024:   PetscBool      regular, ismatis, isRefined = dmCoarse->data == dmFine->data ? PETSC_FALSE : PETSC_TRUE;

9026:   DMGetGlobalSection(dmFine, &gsf);
9027:   PetscSectionGetConstrainedStorageSize(gsf, &m);
9028:   DMGetGlobalSection(dmCoarse, &gsc);
9029:   PetscSectionGetConstrainedStorageSize(gsc, &n);

9031:   PetscStrcmp(dmCoarse->mattype, MATIS, &ismatis);
9032:   MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);
9033:   MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);
9034:   MatSetType(*interpolation, ismatis ? MATAIJ : dmCoarse->mattype);
9035:   DMGetApplicationContext(dmFine, &ctx);

9037:   DMGetCoarseDM(dmFine, &cdm);
9038:   DMPlexGetRegularRefinement(dmFine, &regular);
9039:   if (!isRefined || (regular && cdm == dmCoarse)) DMPlexComputeInterpolatorNested(dmCoarse, dmFine, isRefined, *interpolation, ctx);
9040:   else                                            DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);
9041:   MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");
9042:   if (scaling) {
9043:     /* Use naive scaling */
9044:     DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);
9045:   }
9046:   return 0;
9047: }

9049: PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
9050: {
9051:   VecScatter     ctx;

9053:   DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);
9054:   MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);
9055:   VecScatterDestroy(&ctx);
9056:   return 0;
9057: }

9059: static void g0_identity_private(PetscInt dim, PetscInt Nf, PetscInt NfAux,
9060:                                 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
9061:                                 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
9062:                                 PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[])
9063: {
9064:   const PetscInt Nc = uOff[1] - uOff[0];
9065:   PetscInt       c;
9066:   for (c = 0; c < Nc; ++c) g0[c*Nc+c] = 1.0;
9067: }

9069: PetscErrorCode DMCreateMassMatrixLumped_Plex(DM dm, Vec *mass)
9070: {
9071:   DM             dmc;
9072:   PetscDS        ds;
9073:   Vec            ones, locmass;
9074:   IS             cellIS;
9075:   PetscFormKey   key;
9076:   PetscInt       depth;

9078:   DMClone(dm, &dmc);
9079:   DMCopyDisc(dm, dmc);
9080:   DMGetDS(dmc, &ds);
9081:   PetscDSSetJacobian(ds, 0, 0, g0_identity_private, NULL, NULL, NULL);
9082:   DMCreateGlobalVector(dmc, mass);
9083:   DMGetLocalVector(dmc, &ones);
9084:   DMGetLocalVector(dmc, &locmass);
9085:   DMPlexGetDepth(dmc, &depth);
9086:   DMGetStratumIS(dmc, "depth", depth, &cellIS);
9087:   VecSet(locmass, 0.0);
9088:   VecSet(ones, 1.0);
9089:   key.label = NULL;
9090:   key.value = 0;
9091:   key.field = 0;
9092:   key.part  = 0;
9093:   DMPlexComputeJacobian_Action_Internal(dmc, key, cellIS, 0.0, 0.0, ones, NULL, ones, locmass, NULL);
9094:   ISDestroy(&cellIS);
9095:   VecSet(*mass, 0.0);
9096:   DMLocalToGlobalBegin(dmc, locmass, ADD_VALUES, *mass);
9097:   DMLocalToGlobalEnd(dmc, locmass, ADD_VALUES, *mass);
9098:   DMRestoreLocalVector(dmc, &ones);
9099:   DMRestoreLocalVector(dmc, &locmass);
9100:   DMDestroy(&dmc);
9101:   return 0;
9102: }

9104: PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mass)
9105: {
9106:   PetscSection   gsc, gsf;
9107:   PetscInt       m, n;
9108:   void          *ctx;
9109:   DM             cdm;
9110:   PetscBool      regular;

9112:   if (dmFine == dmCoarse) {
9113:     DM            dmc;
9114:     PetscDS       ds;
9115:     PetscWeakForm wf;
9116:     Vec           u;
9117:     IS            cellIS;
9118:     PetscFormKey  key;
9119:     PetscInt      depth;

9121:     DMClone(dmFine, &dmc);
9122:     DMCopyDisc(dmFine, dmc);
9123:     DMGetDS(dmc, &ds);
9124:     PetscDSGetWeakForm(ds, &wf);
9125:     PetscWeakFormClear(wf);
9126:     PetscDSSetJacobian(ds, 0, 0, g0_identity_private, NULL, NULL, NULL);
9127:     DMCreateMatrix(dmc, mass);
9128:     DMGetGlobalVector(dmc, &u);
9129:     DMPlexGetDepth(dmc, &depth);
9130:     DMGetStratumIS(dmc, "depth", depth, &cellIS);
9131:     MatZeroEntries(*mass);
9132:     key.label = NULL;
9133:     key.value = 0;
9134:     key.field = 0;
9135:     key.part  = 0;
9136:     DMPlexComputeJacobian_Internal(dmc, key, cellIS, 0.0, 0.0, u, NULL, *mass, *mass, NULL);
9137:     ISDestroy(&cellIS);
9138:     DMRestoreGlobalVector(dmc, &u);
9139:     DMDestroy(&dmc);
9140:   } else {
9141:     DMGetGlobalSection(dmFine, &gsf);
9142:     PetscSectionGetConstrainedStorageSize(gsf, &m);
9143:     DMGetGlobalSection(dmCoarse, &gsc);
9144:     PetscSectionGetConstrainedStorageSize(gsc, &n);

9146:     MatCreate(PetscObjectComm((PetscObject) dmCoarse), mass);
9147:     MatSetSizes(*mass, m, n, PETSC_DETERMINE, PETSC_DETERMINE);
9148:     MatSetType(*mass, dmCoarse->mattype);
9149:     DMGetApplicationContext(dmFine, &ctx);

9151:     DMGetCoarseDM(dmFine, &cdm);
9152:     DMPlexGetRegularRefinement(dmFine, &regular);
9153:     if (regular && cdm == dmCoarse) DMPlexComputeMassMatrixNested(dmCoarse, dmFine, *mass, ctx);
9154:     else                            DMPlexComputeMassMatrixGeneral(dmCoarse, dmFine, *mass, ctx);
9155:   }
9156:   MatViewFromOptions(*mass, NULL, "-mass_mat_view");
9157:   return 0;
9158: }

9160: /*@
9161:   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh

9163:   Input Parameter:
9164: . dm - The DMPlex object

9166:   Output Parameter:
9167: . regular - The flag

9169:   Level: intermediate

9171: .seealso: DMPlexSetRegularRefinement()
9172: @*/
9173: PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
9174: {
9177:   *regular = ((DM_Plex *) dm->data)->regularRefinement;
9178:   return 0;
9179: }

9181: /*@
9182:   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh

9184:   Input Parameters:
9185: + dm - The DMPlex object
9186: - regular - The flag

9188:   Level: intermediate

9190: .seealso: DMPlexGetRegularRefinement()
9191: @*/
9192: PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
9193: {
9195:   ((DM_Plex *) dm->data)->regularRefinement = regular;
9196:   return 0;
9197: }

9199: /* anchors */
9200: /*@
9201:   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
9202:   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetDefaultConstraints().

9204:   not collective

9206:   Input Parameter:
9207: . dm - The DMPlex object

9209:   Output Parameters:
9210: + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
9211: - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection

9213:   Level: intermediate

9215: .seealso: DMPlexSetAnchors(), DMGetDefaultConstraints(), DMSetDefaultConstraints()
9216: @*/
9217: PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
9218: {
9219:   DM_Plex *plex = (DM_Plex *)dm->data;

9222:   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) (*plex->createanchors)(dm);
9223:   if (anchorSection) *anchorSection = plex->anchorSection;
9224:   if (anchorIS) *anchorIS = plex->anchorIS;
9225:   return 0;
9226: }

9228: /*@
9229:   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
9230:   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
9231:   point's degrees of freedom to be a linear combination of other points' degrees of freedom.

9233:   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
9234:   DMGetDefaultConstraints() and filling in the entries in the constraint matrix.

9236:   collective on dm

9238:   Input Parameters:
9239: + dm - The DMPlex object
9240: . anchorSection - The section that describes the mapping from constrained points to the anchor points listed in anchorIS.  Must have a local communicator (PETSC_COMM_SELF or derivative).
9241: - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).

9243:   The reference counts of anchorSection and anchorIS are incremented.

9245:   Level: intermediate

9247: .seealso: DMPlexGetAnchors(), DMGetDefaultConstraints(), DMSetDefaultConstraints()
9248: @*/
9249: PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
9250: {
9251:   DM_Plex        *plex = (DM_Plex *)dm->data;
9252:   PetscMPIInt    result;

9255:   if (anchorSection) {
9257:     MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);
9259:   }
9260:   if (anchorIS) {
9262:     MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);
9264:   }

9266:   PetscObjectReference((PetscObject)anchorSection);
9267:   PetscSectionDestroy(&plex->anchorSection);
9268:   plex->anchorSection = anchorSection;

9270:   PetscObjectReference((PetscObject)anchorIS);
9271:   ISDestroy(&plex->anchorIS);
9272:   plex->anchorIS = anchorIS;

9274:   if (PetscUnlikelyDebug(anchorIS && anchorSection)) {
9275:     PetscInt size, a, pStart, pEnd;
9276:     const PetscInt *anchors;

9278:     PetscSectionGetChart(anchorSection,&pStart,&pEnd);
9279:     ISGetLocalSize(anchorIS,&size);
9280:     ISGetIndices(anchorIS,&anchors);
9281:     for (a = 0; a < size; a++) {
9282:       PetscInt p;

9284:       p = anchors[a];
9285:       if (p >= pStart && p < pEnd) {
9286:         PetscInt dof;

9288:         PetscSectionGetDof(anchorSection,p,&dof);
9289:         if (dof) {

9291:           ISRestoreIndices(anchorIS,&anchors);
9292:           SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
9293:         }
9294:       }
9295:     }
9296:     ISRestoreIndices(anchorIS,&anchors);
9297:   }
9298:   /* reset the generic constraints */
9299:   DMSetDefaultConstraints(dm,NULL,NULL,NULL);
9300:   return 0;
9301: }

9303: static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
9304: {
9305:   PetscSection anchorSection;
9306:   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;

9309:   DMPlexGetAnchors(dm,&anchorSection,NULL);
9310:   PetscSectionCreate(PETSC_COMM_SELF,cSec);
9311:   PetscSectionGetNumFields(section,&numFields);
9312:   if (numFields) {
9313:     PetscInt f;
9314:     PetscSectionSetNumFields(*cSec,numFields);

9316:     for (f = 0; f < numFields; f++) {
9317:       PetscInt numComp;

9319:       PetscSectionGetFieldComponents(section,f,&numComp);
9320:       PetscSectionSetFieldComponents(*cSec,f,numComp);
9321:     }
9322:   }
9323:   PetscSectionGetChart(anchorSection,&pStart,&pEnd);
9324:   PetscSectionGetChart(section,&sStart,&sEnd);
9325:   pStart = PetscMax(pStart,sStart);
9326:   pEnd   = PetscMin(pEnd,sEnd);
9327:   pEnd   = PetscMax(pStart,pEnd);
9328:   PetscSectionSetChart(*cSec,pStart,pEnd);
9329:   for (p = pStart; p < pEnd; p++) {
9330:     PetscSectionGetDof(anchorSection,p,&dof);
9331:     if (dof) {
9332:       PetscSectionGetDof(section,p,&dof);
9333:       PetscSectionSetDof(*cSec,p,dof);
9334:       for (f = 0; f < numFields; f++) {
9335:         PetscSectionGetFieldDof(section,p,f,&dof);
9336:         PetscSectionSetFieldDof(*cSec,p,f,dof);
9337:       }
9338:     }
9339:   }
9340:   PetscSectionSetUp(*cSec);
9341:   PetscObjectSetName((PetscObject) *cSec, "Constraint Section");
9342:   return 0;
9343: }

9345: static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
9346: {
9347:   PetscSection   aSec;
9348:   PetscInt       pStart, pEnd, p, sStart, sEnd, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
9349:   const PetscInt *anchors;
9350:   PetscInt       numFields, f;
9351:   IS             aIS;
9352:   MatType        mtype;
9353:   PetscBool      iscuda,iskokkos;

9356:   PetscSectionGetStorageSize(cSec, &m);
9357:   PetscSectionGetStorageSize(section, &n);
9358:   MatCreate(PETSC_COMM_SELF,cMat);
9359:   MatSetSizes(*cMat,m,n,m,n);
9360:   PetscStrcmp(dm->mattype,MATSEQAIJCUSPARSE,&iscuda);
9361:   if (!iscuda) PetscStrcmp(dm->mattype,MATMPIAIJCUSPARSE,&iscuda);
9362:   PetscStrcmp(dm->mattype,MATSEQAIJKOKKOS,&iskokkos);
9363:   if (!iskokkos) PetscStrcmp(dm->mattype,MATMPIAIJKOKKOS,&iskokkos);
9364:   if (iscuda) mtype = MATSEQAIJCUSPARSE;
9365:   else if (iskokkos) mtype = MATSEQAIJKOKKOS;
9366:   else mtype = MATSEQAIJ;
9367:   MatSetType(*cMat,mtype);
9368:   DMPlexGetAnchors(dm,&aSec,&aIS);
9369:   ISGetIndices(aIS,&anchors);
9370:   /* cSec will be a subset of aSec and section */
9371:   PetscSectionGetChart(cSec,&pStart,&pEnd);
9372:   PetscSectionGetChart(section,&sStart,&sEnd);
9373:   PetscMalloc1(m+1,&i);
9374:   i[0] = 0;
9375:   PetscSectionGetNumFields(section,&numFields);
9376:   for (p = pStart; p < pEnd; p++) {
9377:     PetscInt rDof, rOff, r;

9379:     PetscSectionGetDof(aSec,p,&rDof);
9380:     if (!rDof) continue;
9381:     PetscSectionGetOffset(aSec,p,&rOff);
9382:     if (numFields) {
9383:       for (f = 0; f < numFields; f++) {
9384:         annz = 0;
9385:         for (r = 0; r < rDof; r++) {
9386:           a = anchors[rOff + r];
9387:           if (a < sStart || a >= sEnd) continue;
9388:           PetscSectionGetFieldDof(section,a,f,&aDof);
9389:           annz += aDof;
9390:         }
9391:         PetscSectionGetFieldDof(cSec,p,f,&dof);
9392:         PetscSectionGetFieldOffset(cSec,p,f,&off);
9393:         for (q = 0; q < dof; q++) {
9394:           i[off + q + 1] = i[off + q] + annz;
9395:         }
9396:       }
9397:     } else {
9398:       annz = 0;
9399:       PetscSectionGetDof(cSec,p,&dof);
9400:       for (q = 0; q < dof; q++) {
9401:         a = anchors[rOff + q];
9402:         if (a < sStart || a >= sEnd) continue;
9403:         PetscSectionGetDof(section,a,&aDof);
9404:         annz += aDof;
9405:       }
9406:       PetscSectionGetDof(cSec,p,&dof);
9407:       PetscSectionGetOffset(cSec,p,&off);
9408:       for (q = 0; q < dof; q++) {
9409:         i[off + q + 1] = i[off + q] + annz;
9410:       }
9411:     }
9412:   }
9413:   nnz = i[m];
9414:   PetscMalloc1(nnz,&j);
9415:   offset = 0;
9416:   for (p = pStart; p < pEnd; p++) {
9417:     if (numFields) {
9418:       for (f = 0; f < numFields; f++) {
9419:         PetscSectionGetFieldDof(cSec,p,f,&dof);
9420:         for (q = 0; q < dof; q++) {
9421:           PetscInt rDof, rOff, r;
9422:           PetscSectionGetDof(aSec,p,&rDof);
9423:           PetscSectionGetOffset(aSec,p,&rOff);
9424:           for (r = 0; r < rDof; r++) {
9425:             PetscInt s;

9427:             a = anchors[rOff + r];
9428:             if (a < sStart || a >= sEnd) continue;
9429:             PetscSectionGetFieldDof(section,a,f,&aDof);
9430:             PetscSectionGetFieldOffset(section,a,f,&aOff);
9431:             for (s = 0; s < aDof; s++) {
9432:               j[offset++] = aOff + s;
9433:             }
9434:           }
9435:         }
9436:       }
9437:     } else {
9438:       PetscSectionGetDof(cSec,p,&dof);
9439:       for (q = 0; q < dof; q++) {
9440:         PetscInt rDof, rOff, r;
9441:         PetscSectionGetDof(aSec,p,&rDof);
9442:         PetscSectionGetOffset(aSec,p,&rOff);
9443:         for (r = 0; r < rDof; r++) {
9444:           PetscInt s;

9446:           a = anchors[rOff + r];
9447:           if (a < sStart || a >= sEnd) continue;
9448:           PetscSectionGetDof(section,a,&aDof);
9449:           PetscSectionGetOffset(section,a,&aOff);
9450:           for (s = 0; s < aDof; s++) {
9451:             j[offset++] = aOff + s;
9452:           }
9453:         }
9454:       }
9455:     }
9456:   }
9457:   MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);
9458:   PetscFree(i);
9459:   PetscFree(j);
9460:   ISRestoreIndices(aIS,&anchors);
9461:   return 0;
9462: }

9464: PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
9465: {
9466:   DM_Plex        *plex = (DM_Plex *)dm->data;
9467:   PetscSection   anchorSection, section, cSec;
9468:   Mat            cMat;

9471:   DMPlexGetAnchors(dm,&anchorSection,NULL);
9472:   if (anchorSection) {
9473:     PetscInt Nf;

9475:     DMGetLocalSection(dm,&section);
9476:     DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);
9477:     DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);
9478:     DMGetNumFields(dm,&Nf);
9479:     if (Nf && plex->computeanchormatrix) (*plex->computeanchormatrix)(dm,section,cSec,cMat);
9480:     DMSetDefaultConstraints(dm,cSec,cMat,NULL);
9481:     PetscSectionDestroy(&cSec);
9482:     MatDestroy(&cMat);
9483:   }
9484:   return 0;
9485: }

9487: PetscErrorCode DMCreateSubDomainDM_Plex(DM dm, DMLabel label, PetscInt value, IS *is, DM *subdm)
9488: {
9489:   IS             subis;
9490:   PetscSection   section, subsection;

9492:   DMGetLocalSection(dm, &section);
9495:   /* Create subdomain */
9496:   DMPlexFilter(dm, label, value, subdm);
9497:   /* Create submodel */
9498:   DMPlexGetSubpointIS(*subdm, &subis);
9499:   PetscSectionCreateSubmeshSection(section, subis, &subsection);
9500:   DMSetLocalSection(*subdm, subsection);
9501:   PetscSectionDestroy(&subsection);
9502:   DMCopyDisc(dm, *subdm);
9503:   /* Create map from submodel to global model */
9504:   if (is) {
9505:     PetscSection    sectionGlobal, subsectionGlobal;
9506:     IS              spIS;
9507:     const PetscInt *spmap;
9508:     PetscInt       *subIndices;
9509:     PetscInt        subSize = 0, subOff = 0, pStart, pEnd, p;
9510:     PetscInt        Nf, f, bs = -1, bsLocal[2], bsMinMax[2];

9512:     DMPlexGetSubpointIS(*subdm, &spIS);
9513:     ISGetIndices(spIS, &spmap);
9514:     PetscSectionGetNumFields(section, &Nf);
9515:     DMGetGlobalSection(dm, &sectionGlobal);
9516:     DMGetGlobalSection(*subdm, &subsectionGlobal);
9517:     PetscSectionGetChart(subsection, &pStart, &pEnd);
9518:     for (p = pStart; p < pEnd; ++p) {
9519:       PetscInt gdof, pSubSize  = 0;

9521:       PetscSectionGetDof(sectionGlobal, p, &gdof);
9522:       if (gdof > 0) {
9523:         for (f = 0; f < Nf; ++f) {
9524:           PetscInt fdof, fcdof;

9526:           PetscSectionGetFieldDof(subsection, p, f, &fdof);
9527:           PetscSectionGetFieldConstraintDof(subsection, p, f, &fcdof);
9528:           pSubSize += fdof-fcdof;
9529:         }
9530:         subSize += pSubSize;
9531:         if (pSubSize) {
9532:           if (bs < 0) {
9533:             bs = pSubSize;
9534:           } else if (bs != pSubSize) {
9535:             /* Layout does not admit a pointwise block size */
9536:             bs = 1;
9537:           }
9538:         }
9539:       }
9540:     }
9541:     /* Must have same blocksize on all procs (some might have no points) */
9542:     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
9543:     PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);
9544:     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
9545:     else                            {bs = bsMinMax[0];}
9546:     PetscMalloc1(subSize, &subIndices);
9547:     for (p = pStart; p < pEnd; ++p) {
9548:       PetscInt gdof, goff;

9550:       PetscSectionGetDof(subsectionGlobal, p, &gdof);
9551:       if (gdof > 0) {
9552:         const PetscInt point = spmap[p];

9554:         PetscSectionGetOffset(sectionGlobal, point, &goff);
9555:         for (f = 0; f < Nf; ++f) {
9556:           PetscInt fdof, fcdof, fc, f2, poff = 0;

9558:           /* Can get rid of this loop by storing field information in the global section */
9559:           for (f2 = 0; f2 < f; ++f2) {
9560:             PetscSectionGetFieldDof(section, p, f2, &fdof);
9561:             PetscSectionGetFieldConstraintDof(section, p, f2, &fcdof);
9562:             poff += fdof-fcdof;
9563:           }
9564:           PetscSectionGetFieldDof(section, p, f, &fdof);
9565:           PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);
9566:           for (fc = 0; fc < fdof-fcdof; ++fc, ++subOff) {
9567:             subIndices[subOff] = goff+poff+fc;
9568:           }
9569:         }
9570:       }
9571:     }
9572:     ISRestoreIndices(spIS, &spmap);
9573:     ISCreateGeneral(PetscObjectComm((PetscObject)dm), subSize, subIndices, PETSC_OWN_POINTER, is);
9574:     if (bs > 1) {
9575:       /* We need to check that the block size does not come from non-contiguous fields */
9576:       PetscInt i, j, set = 1;
9577:       for (i = 0; i < subSize; i += bs) {
9578:         for (j = 0; j < bs; ++j) {
9579:           if (subIndices[i+j] != subIndices[i]+j) {set = 0; break;}
9580:         }
9581:       }
9582:       if (set) ISSetBlockSize(*is, bs);
9583:     }
9584:     /* Attach nullspace */
9585:     for (f = 0; f < Nf; ++f) {
9586:       (*subdm)->nullspaceConstructors[f] = dm->nullspaceConstructors[f];
9587:       if ((*subdm)->nullspaceConstructors[f]) break;
9588:     }
9589:     if (f < Nf) {
9590:       MatNullSpace nullSpace;
9591:       (*(*subdm)->nullspaceConstructors[f])(*subdm, f, f, &nullSpace);

9593:       PetscObjectCompose((PetscObject) *is, "nullspace", (PetscObject) nullSpace);
9594:       MatNullSpaceDestroy(&nullSpace);
9595:     }
9596:   }
9597:   return 0;
9598: }

9600: /*@
9601:   DMPlexMonitorThroughput - Report the cell throughput of FE integration

9603:   Input Parameter:
9604: - dm - The DM

9606:   Level: developer

9608:   Options Database Keys:
9609: . -dm_plex_monitor_throughput - Activate the monitor

9611: .seealso: DMSetFromOptions(), DMPlexCreate()
9612: @*/
9613: PetscErrorCode DMPlexMonitorThroughput(DM dm, void *dummy)
9614: {
9615: #if defined(PETSC_USE_LOG)
9616:   PetscStageLog      stageLog;
9617:   PetscLogEvent      event;
9618:   PetscLogStage      stage;
9619:   PetscEventPerfInfo eventInfo;
9620:   PetscReal          cellRate, flopRate;
9621:   PetscInt           cStart, cEnd, Nf, N;
9622:   const char        *name;
9623: #endif

9626: #if defined(PETSC_USE_LOG)
9627:   PetscObjectGetName((PetscObject) dm, &name);
9628:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
9629:   DMGetNumFields(dm, &Nf);
9630:   PetscLogGetStageLog(&stageLog);
9631:   PetscStageLogGetCurrent(stageLog, &stage);
9632:   PetscLogEventGetId("DMPlexResidualFE", &event);
9633:   PetscLogEventGetPerfInfo(stage, event, &eventInfo);
9634:   N        = (cEnd - cStart)*Nf*eventInfo.count;
9635:   flopRate = eventInfo.flops/eventInfo.time;
9636:   cellRate = N/eventInfo.time;
9637:   PetscPrintf(PetscObjectComm((PetscObject) dm), "DM (%s) FE Residual Integration: %D integrals %D reps\n  Cell rate: %.2g/s flop rate: %.2g MF/s\n", name ? name : "unknown", N, eventInfo.count, (double) cellRate, (double) (flopRate/1.e6));
9638: #else
9639:   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Plex Throughput Monitor is not supported if logging is turned off. Reconfigure using --with-log.");
9640: #endif
9641:   return 0;
9642: }