Actual source code: plex.c

petsc-master 2016-05-30
Report Typos and Errors
  1: #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
  2: #include <petsc/private/isimpl.h>
  3: #include <petsc/private/vecimpl.h>
  4: #include <petscsf.h>
  5: #include <petscds.h>

  7: /* Logging support */
  8: PetscLogEvent DMPLEX_Interpolate, PETSCPARTITIONER_Partition, DMPLEX_Distribute, DMPLEX_DistributeCones, DMPLEX_DistributeLabels, DMPLEX_DistributeSF, DMPLEX_DistributeOverlap, DMPLEX_DistributeField, DMPLEX_DistributeData, DMPLEX_Migrate, DMPLEX_GlobalToNaturalBegin, DMPLEX_GlobalToNaturalEnd, DMPLEX_NaturalToGlobalBegin, DMPLEX_NaturalToGlobalEnd, DMPLEX_Stratify, DMPLEX_Preallocate, DMPLEX_ResidualFEM, DMPLEX_JacobianFEM, DMPLEX_InterpolatorFEM, DMPLEX_InjectorFEM, DMPLEX_IntegralFEM, DMPLEX_CreateGmsh;

 10: PETSC_EXTERN PetscErrorCode VecView_MPI(Vec, PetscViewer);
 11: PETSC_EXTERN PetscErrorCode VecLoad_Default(Vec, PetscViewer);

 15: PetscErrorCode DMPlexGetFieldType_Internal(DM dm, PetscSection section, PetscInt field, PetscInt *sStart, PetscInt *sEnd, PetscViewerVTKFieldType *ft)
 16: {
 17:   PetscInt       dim, pStart, pEnd, vStart, vEnd, cStart, cEnd, cEndInterior, vdof = 0, cdof = 0;

 21:   *ft  = PETSC_VTK_POINT_FIELD;
 22:   DMGetDimension(dm, &dim);
 23:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
 24:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
 25:   DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);
 26:   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
 27:   PetscSectionGetChart(section, &pStart, &pEnd);
 28:   if (field >= 0) {
 29:     if ((vStart >= pStart) && (vStart < pEnd)) {PetscSectionGetFieldDof(section, vStart, field, &vdof);}
 30:     if ((cStart >= pStart) && (cStart < pEnd)) {PetscSectionGetFieldDof(section, cStart, field, &cdof);}
 31:   } else {
 32:     if ((vStart >= pStart) && (vStart < pEnd)) {PetscSectionGetDof(section, vStart, &vdof);}
 33:     if ((cStart >= pStart) && (cStart < pEnd)) {PetscSectionGetDof(section, cStart, &cdof);}
 34:   }
 35:   if (vdof) {
 36:     *sStart = vStart;
 37:     *sEnd   = vEnd;
 38:     if (vdof == dim) *ft = PETSC_VTK_POINT_VECTOR_FIELD;
 39:     else             *ft = PETSC_VTK_POINT_FIELD;
 40:   } else if (cdof) {
 41:     *sStart = cStart;
 42:     *sEnd   = cEnd;
 43:     if (cdof == dim) *ft = PETSC_VTK_CELL_VECTOR_FIELD;
 44:     else             *ft = PETSC_VTK_CELL_FIELD;
 45:   } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Could not classify input Vec for VTK");
 46:   return(0);
 47: }

 51: PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
 52: {
 53:   DM             dm;
 54:   PetscBool      isvtk, ishdf5, isseq;

 58:   VecGetDM(v, &dm);
 59:   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
 60:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);
 61:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);
 62:   PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);
 63:   if (isvtk || ishdf5) {
 64:     PetscInt  numFields;
 65:     PetscBool fem = PETSC_FALSE;

 67:     DMGetNumFields(dm, &numFields);
 68:     if (numFields) {
 69:       PetscObject fe;

 71:       DMGetField(dm, 0, &fe);
 72:       if (fe->classid == PETSCFE_CLASSID) fem = PETSC_TRUE;
 73:     }
 74:     if (fem) {DMPlexInsertBoundaryValues(dm, PETSC_TRUE, v, 0.0, NULL, NULL, NULL);}
 75:   }
 76:   if (isvtk) {
 77:     PetscSection            section;
 78:     PetscViewerVTKFieldType ft;
 79:     PetscInt                pStart, pEnd;

 81:     DMGetDefaultSection(dm, &section);
 82:     DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft);
 83:     PetscObjectReference((PetscObject) dm); /* viewer drops reference */
 84:     PetscObjectReference((PetscObject) v);  /* viewer drops reference */
 85:     PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, ft, (PetscObject) v);
 86:   } else if (ishdf5) {
 87: #if defined(PETSC_HAVE_HDF5)
 88:     VecView_Plex_Local_HDF5(v, viewer);
 89: #else
 90:     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
 91: #endif
 92:   } else {
 93:     if (isseq) {VecView_Seq(v, viewer);}
 94:     else       {VecView_MPI(v, viewer);}
 95:   }
 96:   return(0);
 97: }

101: PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
102: {
103:   DM             dm;
104:   PetscBool      isvtk, ishdf5, isseq;

108:   VecGetDM(v, &dm);
109:   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
110:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);
111:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);
112:   PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);
113:   if (isvtk) {
114:     Vec         locv;
115:     const char *name;

117:     DMGetLocalVector(dm, &locv);
118:     PetscObjectGetName((PetscObject) v, &name);
119:     PetscObjectSetName((PetscObject) locv, name);
120:     DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);
121:     DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);
122:     VecView_Plex_Local(locv, viewer);
123:     DMRestoreLocalVector(dm, &locv);
124:   } else if (ishdf5) {
125: #if defined(PETSC_HAVE_HDF5)
126:     VecView_Plex_HDF5(v, viewer);
127: #else
128:     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
129: #endif
130:   } else {
131:     if (isseq) {VecView_Seq(v, viewer);}
132:     else       {VecView_MPI(v, viewer);}
133:   }
134:   return(0);
135: }

139: PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
140: {
141:   DM                dm;
142:   MPI_Comm          comm;
143:   PetscViewerFormat format;
144:   Vec               v;
145:   PetscBool         isvtk, ishdf5;
146:   PetscErrorCode    ierr;

149:   VecGetDM(originalv, &dm);
150:   PetscObjectGetComm((PetscObject) originalv, &comm);
151:   if (!dm) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
152:   PetscViewerGetFormat(viewer, &format);
153:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);
154:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);
155:   if (format == PETSC_VIEWER_NATIVE) {
156:     const char *vecname;
157:     PetscInt    n, nroots;

159:     if (dm->sfNatural) {
160:       VecGetLocalSize(originalv, &n);
161:       PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);
162:       if (n == nroots) {
163:         DMGetGlobalVector(dm, &v);
164:         DMPlexGlobalToNaturalBegin(dm, originalv, v);
165:         DMPlexGlobalToNaturalEnd(dm, originalv, v);
166:         PetscObjectGetName((PetscObject) originalv, &vecname);
167:         PetscObjectSetName((PetscObject) v, vecname);
168:       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
169:     } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
170:   } else {
171:     /* we are viewing a natural DMPlex vec. */
172:     v = originalv;
173:   }
174:   if (ishdf5) {
175: #if defined(PETSC_HAVE_HDF5)
176:     VecView_Plex_HDF5_Native(v, viewer);
177: #else
178:     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
179: #endif
180:   } else if (isvtk) {
181:     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
182:   } else {
183:     PetscBool isseq;

185:     PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);
186:     if (isseq) {VecView_Seq(v, viewer);}
187:     else       {VecView_MPI(v, viewer);}
188:   }
189:   if (format == PETSC_VIEWER_NATIVE) {DMRestoreGlobalVector(dm, &v);}
190:   return(0);
191: }

195: PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
196: {
197:   DM             dm;
198:   PetscBool      ishdf5;

202:   VecGetDM(v, &dm);
203:   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
204:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);
205:   if (ishdf5) {
206:     DM          dmBC;
207:     Vec         gv;
208:     const char *name;

210:     DMGetOutputDM(dm, &dmBC);
211:     DMGetGlobalVector(dmBC, &gv);
212:     PetscObjectGetName((PetscObject) v, &name);
213:     PetscObjectSetName((PetscObject) gv, name);
214:     VecLoad_Default(gv, viewer);
215:     DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);
216:     DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);
217:     DMRestoreGlobalVector(dmBC, &gv);
218:   } else {
219:     VecLoad_Default(v, viewer);
220:   }
221:   return(0);
222: }

226: PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
227: {
228:   DM             dm;
229:   PetscBool      ishdf5;

233:   VecGetDM(v, &dm);
234:   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
235:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);
236:   if (ishdf5) {
237: #if defined(PETSC_HAVE_HDF5)
238:     VecLoad_Plex_HDF5(v, viewer);
239: #else
240:     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
241: #endif
242:   } else {
243:     VecLoad_Default(v, viewer);
244:   }
245:   return(0);
246: }

250: PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
251: {
252:   DM                dm;
253:   PetscViewerFormat format;
254:   PetscBool         ishdf5;
255:   PetscErrorCode    ierr;

258:   VecGetDM(originalv, &dm);
259:   if (!dm) SETERRQ(PetscObjectComm((PetscObject) originalv), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
260:   PetscViewerGetFormat(viewer, &format);
261:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);
262:   if (format == PETSC_VIEWER_NATIVE) {
263:     if (dm->sfNatural) {
264:       if (ishdf5) {
265: #if defined(PETSC_HAVE_HDF5)
266:         Vec         v;
267:         const char *vecname;

269:         DMGetGlobalVector(dm, &v);
270:         PetscObjectGetName((PetscObject) originalv, &vecname);
271:         PetscObjectSetName((PetscObject) v, vecname);
272:         VecLoad_Plex_HDF5_Native(v, viewer);
273:         DMPlexNaturalToGlobalBegin(dm, v, originalv);
274:         DMPlexNaturalToGlobalEnd(dm, v, originalv);
275:         DMRestoreGlobalVector(dm, &v);
276: #else
277:         SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
278: #endif
279:       } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
280:     }
281:   }
282:   return(0);
283: }

287: PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
288: {
289:   PetscSection       coordSection;
290:   Vec                coordinates;
291:   DMLabel            depthLabel;
292:   const char        *name[4];
293:   const PetscScalar *a;
294:   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;
295:   PetscErrorCode     ierr;

298:   DMGetDimension(dm, &dim);
299:   DMGetCoordinatesLocal(dm, &coordinates);
300:   DMGetCoordinateSection(dm, &coordSection);
301:   DMPlexGetDepthLabel(dm, &depthLabel);
302:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
303:   PetscSectionGetChart(coordSection, &pStart, &pEnd);
304:   VecGetArrayRead(coordinates, &a);
305:   name[0]     = "vertex";
306:   name[1]     = "edge";
307:   name[dim-1] = "face";
308:   name[dim]   = "cell";
309:   for (c = cStart; c < cEnd; ++c) {
310:     PetscInt *closure = NULL;
311:     PetscInt  closureSize, cl;

313:     PetscViewerASCIIPrintf(viewer, "Geometry for cell %D:\n", c);
314:     DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
315:     PetscViewerASCIIPushTab(viewer);
316:     for (cl = 0; cl < closureSize*2; cl += 2) {
317:       PetscInt point = closure[cl], depth, dof, off, d, p;

319:       if ((point < pStart) || (point >= pEnd)) continue;
320:       PetscSectionGetDof(coordSection, point, &dof);
321:       if (!dof) continue;
322:       DMLabelGetValue(depthLabel, point, &depth);
323:       PetscSectionGetOffset(coordSection, point, &off);
324:       PetscViewerASCIIPrintf(viewer, "%s %D coords:", name[depth], point);
325:       for (p = 0; p < dof/dim; ++p) {
326:         PetscViewerASCIIPrintf(viewer, " (");
327:         for (d = 0; d < dim; ++d) {
328:           if (d > 0) {PetscViewerASCIIPrintf(viewer, ", ");}
329:           PetscViewerASCIIPrintf(viewer, "%g", PetscRealPart(a[off+p*dim+d]));
330:         }
331:         PetscViewerASCIIPrintf(viewer, ")");
332:       }
333:       PetscViewerASCIIPrintf(viewer, "\n");
334:     }
335:     DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
336:     PetscViewerASCIIPopTab(viewer);
337:   }
338:   VecRestoreArrayRead(coordinates, &a);
339:   return(0);
340: }

344: PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
345: {
346:   DM_Plex          *mesh = (DM_Plex*) dm->data;
347:   DM                cdm;
348:   DMLabel           markers;
349:   PetscSection      coordSection;
350:   Vec               coordinates;
351:   PetscViewerFormat format;
352:   PetscErrorCode    ierr;

355:   DMGetCoordinateDM(dm, &cdm);
356:   DMGetDefaultSection(cdm, &coordSection);
357:   DMGetCoordinatesLocal(dm, &coordinates);
358:   PetscViewerGetFormat(viewer, &format);
359:   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
360:     const char *name;
361:     PetscInt    maxConeSize, maxSupportSize;
362:     PetscInt    pStart, pEnd, p;
363:     PetscMPIInt rank, size;

365:     MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
366:     MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);
367:     PetscObjectGetName((PetscObject) dm, &name);
368:     DMPlexGetChart(dm, &pStart, &pEnd);
369:     DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);
370:     PetscViewerASCIIPrintf(viewer, "Mesh '%s':\n", name);
371:     PetscViewerASCIIPrintf(viewer, "orientation is missing\n", name);
372:     PetscViewerASCIIPrintf(viewer, "cap --> base:\n", name);
373:     PetscViewerASCIIPushSynchronized(viewer);
374:     PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max sizes cone: %D support: %D\n", rank,maxConeSize, maxSupportSize);
375:     for (p = pStart; p < pEnd; ++p) {
376:       PetscInt dof, off, s;

378:       PetscSectionGetDof(mesh->supportSection, p, &dof);
379:       PetscSectionGetOffset(mesh->supportSection, p, &off);
380:       for (s = off; s < off+dof; ++s) {
381:         PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D ----> %D\n", rank, p, mesh->supports[s]);
382:       }
383:     }
384:     PetscViewerFlush(viewer);
385:     PetscViewerASCIIPrintf(viewer, "base <-- cap:\n", name);
386:     for (p = pStart; p < pEnd; ++p) {
387:       PetscInt dof, off, c;

389:       PetscSectionGetDof(mesh->coneSection, p, &dof);
390:       PetscSectionGetOffset(mesh->coneSection, p, &off);
391:       for (c = off; c < off+dof; ++c) {
392:         PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D <---- %D (%D)\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);
393:       }
394:     }
395:     PetscViewerFlush(viewer);
396:     PetscViewerASCIIPopSynchronized(viewer);
397:     PetscSectionGetChart(coordSection, &pStart, NULL);
398:     if (pStart >= 0) {PetscSectionVecView(coordSection, coordinates, viewer);}
399:     DMGetLabel(dm, "marker", &markers);
400:     DMLabelView(markers,viewer);
401:     if (size > 1) {
402:       PetscSF sf;

404:       DMGetPointSF(dm, &sf);
405:       PetscSFView(sf, viewer);
406:     }
407:     PetscViewerFlush(viewer);
408:   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
409:     const char  *name, *color;
410:     const char  *defcolors[3]  = {"gray", "orange", "green"};
411:     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
412:     PetscReal    scale         = 2.0;
413:     PetscBool    useNumbers    = PETSC_TRUE, useLabels, useColors;
414:     double       tcoords[3];
415:     PetscScalar *coords;
416:     PetscInt     numLabels, l, numColors, numLColors, dim, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p;
417:     PetscMPIInt  rank, size;
418:     char         **names, **colors, **lcolors;

420:     DMGetDimension(dm, &dim);
421:     DMPlexGetDepth(dm, &depth);
422:     DMGetNumLabels(dm, &numLabels);
423:     numLabels  = PetscMax(numLabels, 10);
424:     numColors  = 10;
425:     numLColors = 10;
426:     PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors);
427:     PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_scale", &scale, NULL);
428:     PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);
429:     PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);
430:     if (!useLabels) numLabels = 0;
431:     PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);
432:     if (!useColors) {
433:       numColors = 3;
434:       for (c = 0; c < numColors; ++c) {PetscStrallocpy(defcolors[c], &colors[c]);}
435:     }
436:     PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);
437:     if (!useColors) {
438:       numLColors = 4;
439:       for (c = 0; c < numLColors; ++c) {PetscStrallocpy(deflcolors[c], &lcolors[c]);}
440:     }
441:     MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
442:     MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);
443:     PetscObjectGetName((PetscObject) dm, &name);
444:     PetscViewerASCIIPrintf(viewer, "\
445: \\documentclass[tikz]{standalone}\n\n\
446: \\usepackage{pgflibraryshapes}\n\
447: \\usetikzlibrary{backgrounds}\n\
448: \\usetikzlibrary{arrows}\n\
449: \\begin{document}\n");
450:     if (size > 1) {
451:       PetscViewerASCIIPrintf(viewer, "%s for process ", name);
452:       for (p = 0; p < size; ++p) {
453:         if (p > 0 && p == size-1) {
454:           PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);
455:         } else if (p > 0) {
456:           PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);
457:         }
458:         PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);
459:       }
460:       PetscViewerASCIIPrintf(viewer, ".\n\n\n");
461:     }
462:     PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", 1.0);
463:     /* Plot vertices */
464:     DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
465:     VecGetArray(coordinates, &coords);
466:     PetscViewerASCIIPushSynchronized(viewer);
467:     for (v = vStart; v < vEnd; ++v) {
468:       PetscInt  off, dof, d;
469:       PetscBool isLabeled = PETSC_FALSE;

471:       PetscSectionGetDof(coordSection, v, &dof);
472:       PetscSectionGetOffset(coordSection, v, &off);
473:       PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");
474:       if (PetscUnlikely(dof > 3)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"coordSection vertex %D has dof %D > 3",v,dof);
475:       for (d = 0; d < dof; ++d) {
476:         tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
477:         tcoords[d] = PetscAbsReal(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
478:       }
479:       /* Rotate coordinates since PGF makes z point out of the page instead of up */
480:       if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
481:       for (d = 0; d < dof; ++d) {
482:         if (d > 0) {PetscViewerASCIISynchronizedPrintf(viewer, ",");}
483:         PetscViewerASCIISynchronizedPrintf(viewer, "%g", tcoords[d]);
484:       }
485:       color = colors[rank%numColors];
486:       for (l = 0; l < numLabels; ++l) {
487:         PetscInt val;
488:         DMGetLabelValue(dm, names[l], v, &val);
489:         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
490:       }
491:       if (useNumbers) {
492:         PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", v, rank, color, v);
493:       } else {
494:         PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);
495:       }
496:     }
497:     VecRestoreArray(coordinates, &coords);
498:     PetscViewerFlush(viewer);
499:     /* Plot edges */
500:     if (depth > 1) {DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);}
501:     if (dim < 3 && useNumbers) {
502:       VecGetArray(coordinates, &coords);
503:       PetscViewerASCIIPrintf(viewer, "\\path\n");
504:       for (e = eStart; e < eEnd; ++e) {
505:         const PetscInt *cone;
506:         PetscInt        coneSize, offA, offB, dof, d;

508:         DMPlexGetConeSize(dm, e, &coneSize);
509:         if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %D cone should have two vertices, not %D", e, coneSize);
510:         DMPlexGetCone(dm, e, &cone);
511:         PetscSectionGetDof(coordSection, cone[0], &dof);
512:         PetscSectionGetOffset(coordSection, cone[0], &offA);
513:         PetscSectionGetOffset(coordSection, cone[1], &offB);
514:         PetscViewerASCIISynchronizedPrintf(viewer, "(");
515:         for (d = 0; d < dof; ++d) {
516:           tcoords[d] = (double) (scale*PetscRealPart(coords[offA+d]+coords[offB+d]));
517:           tcoords[d] = PetscAbsReal(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
518:         }
519:         /* Rotate coordinates since PGF makes z point out of the page instead of up */
520:         if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
521:         for (d = 0; d < dof; ++d) {
522:           if (d > 0) {PetscViewerASCIISynchronizedPrintf(viewer, ",");}
523:           PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);
524:         }
525:         color = colors[rank%numColors];
526:         for (l = 0; l < numLabels; ++l) {
527:           PetscInt val;
528:           DMGetLabelValue(dm, names[l], v, &val);
529:           if (val >= 0) {color = lcolors[l%numLColors]; break;}
530:         }
531:         PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D} --\n", e, rank, color, e);
532:       }
533:       VecRestoreArray(coordinates, &coords);
534:       PetscViewerFlush(viewer);
535:       PetscViewerASCIIPrintf(viewer, "(0,0);\n");
536:     }
537:     /* Plot cells */
538:     if (dim == 3 || !useNumbers) {
539:       for (e = eStart; e < eEnd; ++e) {
540:         const PetscInt *cone;

542:         color = colors[rank%numColors];
543:         for (l = 0; l < numLabels; ++l) {
544:           PetscInt val;
545:           DMGetLabelValue(dm, names[l], e, &val);
546:           if (val >= 0) {color = lcolors[l%numLColors]; break;}
547:         }
548:         DMPlexGetCone(dm, e, &cone);
549:         PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d);\n", color, cone[0], rank, cone[1], rank);
550:       }
551:     } else {
552:       DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
553:       for (c = cStart; c < cEnd; ++c) {
554:         PetscInt *closure = NULL;
555:         PetscInt  closureSize, firstPoint = -1;

557:         DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
558:         PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);
559:         for (p = 0; p < closureSize*2; p += 2) {
560:           const PetscInt point = closure[p];

562:           if ((point < vStart) || (point >= vEnd)) continue;
563:           if (firstPoint >= 0) {PetscViewerASCIISynchronizedPrintf(viewer, " -- ");}
564:           PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%d)", point, rank);
565:           if (firstPoint < 0) firstPoint = point;
566:         }
567:         /* Why doesn't this work? PetscViewerASCIISynchronizedPrintf(viewer, " -- cycle;\n"); */
568:         PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%d);\n", firstPoint, rank);
569:         DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
570:       }
571:     }
572:     PetscViewerFlush(viewer);
573:     PetscViewerASCIIPopSynchronized(viewer);
574:     PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");
575:     PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);
576:     for (l = 0; l < numLabels;  ++l) {PetscFree(names[l]);}
577:     for (c = 0; c < numColors;  ++c) {PetscFree(colors[c]);}
578:     for (c = 0; c < numLColors; ++c) {PetscFree(lcolors[c]);}
579:     PetscFree3(names, colors, lcolors);
580:   } else {
581:     MPI_Comm    comm;
582:     PetscInt   *sizes, *hybsizes;
583:     PetscInt    locDepth, depth, dim, d, pMax[4];
584:     PetscInt    pStart, pEnd, p;
585:     PetscInt    numLabels, l;
586:     const char *name;
587:     PetscMPIInt size;

589:     PetscObjectGetComm((PetscObject)dm,&comm);
590:     MPI_Comm_size(comm, &size);
591:     DMGetDimension(dm, &dim);
592:     PetscObjectGetName((PetscObject) dm, &name);
593:     if (name) {PetscViewerASCIIPrintf(viewer, "%s in %D dimensions:\n", name, dim);}
594:     else      {PetscViewerASCIIPrintf(viewer, "Mesh in %D dimensions:\n", dim);}
595:     DMPlexGetDepth(dm, &locDepth);
596:     MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);
597:     DMPlexGetHybridBounds(dm, &pMax[depth], depth > 0 ? &pMax[depth-1] : NULL, &pMax[1], &pMax[0]);
598:     PetscMalloc2(size,&sizes,size,&hybsizes);
599:     if (depth == 1) {
600:       DMPlexGetDepthStratum(dm, 0, &pStart, &pEnd);
601:       pEnd = pEnd - pStart;
602:       MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);
603:       PetscViewerASCIIPrintf(viewer, "  %d-cells:", 0);
604:       for (p = 0; p < size; ++p) {PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);}
605:       PetscViewerASCIIPrintf(viewer, "\n");
606:       DMPlexGetHeightStratum(dm, 0, &pStart, &pEnd);
607:       pEnd = pEnd - pStart;
608:       MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);
609:       PetscViewerASCIIPrintf(viewer, "  %D-cells:", dim);
610:       for (p = 0; p < size; ++p) {PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);}
611:       PetscViewerASCIIPrintf(viewer, "\n");
612:     } else {
613:       for (d = 0; d <= dim; d++) {
614:         DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);
615:         pEnd    -= pStart;
616:         pMax[d] -= pStart;
617:         MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);
618:         MPI_Gather(&pMax[d], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);
619:         PetscViewerASCIIPrintf(viewer, "  %D-cells:", d);
620:         for (p = 0; p < size; ++p) {
621:           if (hybsizes[p] >= 0) {PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);}
622:           else                  {PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);}
623:         }
624:         PetscViewerASCIIPrintf(viewer, "\n");
625:       }
626:     }
627:     PetscFree2(sizes,hybsizes);
628:     DMGetNumLabels(dm, &numLabels);
629:     if (numLabels) {PetscViewerASCIIPrintf(viewer, "Labels:\n");}
630:     for (l = 0; l < numLabels; ++l) {
631:       DMLabel         label;
632:       const char     *name;
633:       IS              valueIS;
634:       const PetscInt *values;
635:       PetscInt        numValues, v;

637:       DMGetLabelName(dm, l, &name);
638:       DMGetLabel(dm, name, &label);
639:       DMLabelGetNumValues(label, &numValues);
640:       PetscViewerASCIIPrintf(viewer, "  %s: %D strata of sizes (", name, numValues);
641:       DMLabelGetValueIS(label, &valueIS);
642:       ISGetIndices(valueIS, &values);
643:       PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);
644:       for (v = 0; v < numValues; ++v) {
645:         PetscInt size;

647:         DMLabelGetStratumSize(label, values[v], &size);
648:         if (v > 0) {PetscViewerASCIIPrintf(viewer, ", ");}
649:         PetscViewerASCIIPrintf(viewer, "%D", size);
650:       }
651:       PetscViewerASCIIPrintf(viewer, ")\n");
652:       PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);
653:       ISRestoreIndices(valueIS, &values);
654:       ISDestroy(&valueIS);
655:     }
656:     DMGetCoarseDM(dm, &cdm);
657:     if (cdm) {
658:       PetscViewerASCIIPushTab(viewer);
659:       DMPlexView_Ascii(cdm, viewer);
660:       PetscViewerASCIIPopTab(viewer);
661:     }
662:   }
663:   return(0);
664: }

668: PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
669: {
670:   PetscBool      iascii, ishdf5, isvtk;

676:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);
677:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);
678:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);
679:   if (iascii) {
680:     DMPlexView_Ascii(dm, viewer);
681:   } else if (ishdf5) {
682: #if defined(PETSC_HAVE_HDF5)
683:     PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_VIZ);
684:     DMPlexView_HDF5(dm, viewer);
685:     PetscViewerPopFormat(viewer);
686: #else
687:     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
688: #endif
689:   }
690:   else if (isvtk) {
691:     DMPlexVTKWriteAll((PetscObject) dm,viewer);
692:   }
693:   return(0);
694: }

698: PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
699: {
700:   PetscBool      isbinary, ishdf5;

706:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERBINARY, &isbinary);
707:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);
708:   if (isbinary) {SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Do not yet support binary viewers");}
709:   else if (ishdf5) {
710: #if defined(PETSC_HAVE_HDF5)
711:     DMPlexLoad_HDF5(dm, viewer);
712: #else
713:     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
714: #endif
715:   }
716:   return(0);
717: }


722: PetscErrorCode DMDestroy_Plex(DM dm)
723: {
724:   DM_Plex       *mesh = (DM_Plex*) dm->data;

728:   if (--mesh->refct > 0) return(0);
729:   PetscSectionDestroy(&mesh->coneSection);
730:   PetscFree(mesh->cones);
731:   PetscFree(mesh->coneOrientations);
732:   PetscSectionDestroy(&mesh->supportSection);
733:   PetscFree(mesh->supports);
734:   PetscFree(mesh->facesTmp);
735:   PetscFree(mesh->tetgenOpts);
736:   PetscFree(mesh->triangleOpts);
737:   PetscPartitionerDestroy(&mesh->partitioner);
738:   DMLabelDestroy(&mesh->subpointMap);
739:   ISDestroy(&mesh->globalVertexNumbers);
740:   ISDestroy(&mesh->globalCellNumbers);
741:   PetscSectionDestroy(&mesh->anchorSection);
742:   ISDestroy(&mesh->anchorIS);
743:   PetscSectionDestroy(&mesh->parentSection);
744:   PetscFree(mesh->parents);
745:   PetscFree(mesh->childIDs);
746:   PetscSectionDestroy(&mesh->childSection);
747:   PetscFree(mesh->children);
748:   DMDestroy(&mesh->referenceTree);
749:   PetscGridHashDestroy(&mesh->lbox);
750:   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
751:   PetscFree(mesh);
752:   return(0);
753: }

757: PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
758: {
759:   PetscSection   sectionGlobal;
760:   PetscInt       bs = -1, mbs;
761:   PetscInt       localSize;
762:   PetscBool      isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock;
764:   MatType        mtype;
765:   ISLocalToGlobalMapping ltog;

768:   MatInitializePackage();
769:   mtype = dm->mattype;
770:   DMGetDefaultGlobalSection(dm, &sectionGlobal);
771:   /* PetscSectionGetStorageSize(sectionGlobal, &localSize); */
772:   PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);
773:   MatCreate(PetscObjectComm((PetscObject)dm), J);
774:   MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);
775:   MatSetType(*J, mtype);
776:   MatSetFromOptions(*J);
777:   MatGetBlockSize(*J, &mbs);
778:   if (mbs > 1) bs = mbs;
779:   PetscStrcmp(mtype, MATSHELL, &isShell);
780:   PetscStrcmp(mtype, MATBAIJ, &isBlock);
781:   PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);
782:   PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);
783:   PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);
784:   PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);
785:   PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);
786:   if (!isShell) {
787:     PetscBool fillMatrix = (PetscBool) !dm->prealloc_only;
788:     PetscInt *dnz, *onz, *dnzu, *onzu, bsLocal, bsMax, bsMin;
789:     PetscInt  pStart, pEnd, p, dof, cdof;

791:     PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);
792:     for (p = pStart; p < pEnd; ++p) {
793:       PetscInt bdof;

795:       PetscSectionGetDof(sectionGlobal, p, &dof);
796:       PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);
797:       dof  = dof < 0 ? -(dof+1) : dof;
798:       bdof = cdof && (dof-cdof) ? 1 : dof;
799:       if (dof) {
800:         if (bs < 0)          {bs = bdof;}
801:         else if (bs != bdof) {bs = 1; break;}
802:       }
803:     }
804:     /* Must have same blocksize on all procs (some might have no points) */
805:     bsLocal = bs;
806:     MPIU_Allreduce(&bsLocal, &bsMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));
807:     bsLocal = bs < 0 ? bsMax : bs;
808:     MPIU_Allreduce(&bsLocal, &bsMin, 1, MPIU_INT, MPI_MIN, PetscObjectComm((PetscObject)dm));
809:     if (bsMin != bsMax) {bs = 1;}
810:     else                {bs = bsMax;}
811:     PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);
812:     DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);
813:     PetscFree4(dnz, onz, dnzu, onzu);

815:     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work */
816:     DMGetLocalToGlobalMapping(dm,&ltog);
817:     MatSetLocalToGlobalMapping(*J,ltog,ltog);
818:   }
819:   return(0);
820: }

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

827:   Not collective

829:   Input Parameter:
830: . mesh - The DMPlex

832:   Output Parameters:
833: + pStart - The first mesh point
834: - pEnd   - The upper bound for mesh points

836:   Level: beginner

838: .seealso: DMPlexCreate(), DMPlexSetChart()
839: @*/
840: PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
841: {
842:   DM_Plex       *mesh = (DM_Plex*) dm->data;

847:   PetscSectionGetChart(mesh->coneSection, pStart, pEnd);
848:   return(0);
849: }

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

856:   Not collective

858:   Input Parameters:
859: + mesh - The DMPlex
860: . pStart - The first mesh point
861: - pEnd   - The upper bound for mesh points

863:   Output Parameters:

865:   Level: beginner

867: .seealso: DMPlexCreate(), DMPlexGetChart()
868: @*/
869: PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
870: {
871:   DM_Plex       *mesh = (DM_Plex*) dm->data;

876:   PetscSectionSetChart(mesh->coneSection, pStart, pEnd);
877:   PetscSectionSetChart(mesh->supportSection, pStart, pEnd);
878:   return(0);
879: }

883: /*@
884:   DMPlexGetConeSize - Return the number of in-edges for this point in the Sieve DAG

886:   Not collective

888:   Input Parameters:
889: + mesh - The DMPlex
890: - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()

892:   Output Parameter:
893: . size - The cone size for point p

895:   Level: beginner

897: .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
898: @*/
899: PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
900: {
901:   DM_Plex       *mesh = (DM_Plex*) dm->data;

907:   PetscSectionGetDof(mesh->coneSection, p, size);
908:   return(0);
909: }

913: /*@
914:   DMPlexSetConeSize - Set the number of in-edges for this point in the Sieve DAG

916:   Not collective

918:   Input Parameters:
919: + mesh - The DMPlex
920: . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
921: - size - The cone size for point p

923:   Output Parameter:

925:   Note:
926:   This should be called after DMPlexSetChart().

928:   Level: beginner

930: .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
931: @*/
932: PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
933: {
934:   DM_Plex       *mesh = (DM_Plex*) dm->data;

939:   PetscSectionSetDof(mesh->coneSection, p, size);

941:   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
942:   return(0);
943: }

947: /*@
948:   DMPlexAddConeSize - Add the given number of in-edges to this point in the Sieve DAG

950:   Not collective

952:   Input Parameters:
953: + mesh - The DMPlex
954: . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
955: - size - The additional cone size for point p

957:   Output Parameter:

959:   Note:
960:   This should be called after DMPlexSetChart().

962:   Level: beginner

964: .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
965: @*/
966: PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
967: {
968:   DM_Plex       *mesh = (DM_Plex*) dm->data;
969:   PetscInt       csize;

974:   PetscSectionAddDof(mesh->coneSection, p, size);
975:   PetscSectionGetDof(mesh->coneSection, p, &csize);

977:   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
978:   return(0);
979: }

983: /*@C
984:   DMPlexGetCone - Return the points on the in-edges for this point in the Sieve DAG

986:   Not collective

988:   Input Parameters:
989: + mesh - The DMPlex
990: - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()

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

995:   Level: beginner

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

1001:   You must also call DMPlexRestoreCone() after you finish using the returned array.

1003: .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart()
1004: @*/
1005: PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
1006: {
1007:   DM_Plex       *mesh = (DM_Plex*) dm->data;
1008:   PetscInt       off;

1014:   PetscSectionGetOffset(mesh->coneSection, p, &off);
1015:   *cone = &mesh->cones[off];
1016:   return(0);
1017: }

1021: /*@
1022:   DMPlexSetCone - Set the points on the in-edges for this point in the Sieve DAG

1024:   Not collective

1026:   Input Parameters:
1027: + mesh - The DMPlex
1028: . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1029: - cone - An array of points which are on the in-edges for point p

1031:   Output Parameter:

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

1036:   Level: beginner

1038: .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1039: @*/
1040: PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
1041: {
1042:   DM_Plex       *mesh = (DM_Plex*) dm->data;
1043:   PetscInt       pStart, pEnd;
1044:   PetscInt       dof, off, c;

1049:   PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);
1050:   PetscSectionGetDof(mesh->coneSection, p, &dof);
1052:   PetscSectionGetOffset(mesh->coneSection, p, &off);
1053:   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
1054:   for (c = 0; c < dof; ++c) {
1055:     if ((cone[c] < pStart) || (cone[c] >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone point %D is not in the valid range [%D, %D)", cone[c], pStart, pEnd);
1056:     mesh->cones[off+c] = cone[c];
1057:   }
1058:   return(0);
1059: }

1063: /*@C
1064:   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the Sieve DAG

1066:   Not collective

1068:   Input Parameters:
1069: + mesh - The DMPlex
1070: - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()

1072:   Output Parameter:
1073: . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1074:                     integer giving the prescription for cone traversal. If it is negative, the cone is
1075:                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1076:                     the index of the cone point on which to start.

1078:   Level: beginner

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

1084:   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.

1086: .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
1087: @*/
1088: PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
1089: {
1090:   DM_Plex       *mesh = (DM_Plex*) dm->data;
1091:   PetscInt       off;

1096: #if defined(PETSC_USE_DEBUG)
1097:   {
1098:     PetscInt dof;
1099:     PetscSectionGetDof(mesh->coneSection, p, &dof);
1101:   }
1102: #endif
1103:   PetscSectionGetOffset(mesh->coneSection, p, &off);

1105:   *coneOrientation = &mesh->coneOrientations[off];
1106:   return(0);
1107: }

1111: /*@
1112:   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the Sieve DAG

1114:   Not collective

1116:   Input Parameters:
1117: + mesh - The DMPlex
1118: . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1119: - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1120:                     integer giving the prescription for cone traversal. If it is negative, the cone is
1121:                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1122:                     the index of the cone point on which to start.

1124:   Output Parameter:

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

1129:   Level: beginner

1131: .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1132: @*/
1133: PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
1134: {
1135:   DM_Plex       *mesh = (DM_Plex*) dm->data;
1136:   PetscInt       pStart, pEnd;
1137:   PetscInt       dof, off, c;

1142:   PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);
1143:   PetscSectionGetDof(mesh->coneSection, p, &dof);
1145:   PetscSectionGetOffset(mesh->coneSection, p, &off);
1146:   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
1147:   for (c = 0; c < dof; ++c) {
1148:     PetscInt cdof, o = coneOrientation[c];

1150:     PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);
1151:     if (o && ((o < -(cdof+1)) || (o >= cdof))) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone orientation %D is not in the valid range [%D. %D)", o, -(cdof+1), cdof);
1152:     mesh->coneOrientations[off+c] = o;
1153:   }
1154:   return(0);
1155: }

1159: PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
1160: {
1161:   DM_Plex       *mesh = (DM_Plex*) dm->data;
1162:   PetscInt       pStart, pEnd;
1163:   PetscInt       dof, off;

1168:   PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);
1169:   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
1170:   if ((conePoint < pStart) || (conePoint >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone point %D is not in the valid range [%D, %D)", conePoint, pStart, pEnd);
1171:   PetscSectionGetDof(mesh->coneSection, p, &dof);
1172:   PetscSectionGetOffset(mesh->coneSection, p, &off);
1173:   if ((conePos < 0) || (conePos >= dof)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone position %D of point %D is not in the valid range [0, %D)", conePos, p, dof);
1174:   mesh->cones[off+conePos] = conePoint;
1175:   return(0);
1176: }

1180: PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
1181: {
1182:   DM_Plex       *mesh = (DM_Plex*) dm->data;
1183:   PetscInt       pStart, pEnd;
1184:   PetscInt       dof, off;

1189:   PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);
1190:   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
1191:   PetscSectionGetDof(mesh->coneSection, p, &dof);
1192:   PetscSectionGetOffset(mesh->coneSection, p, &off);
1193:   if ((conePos < 0) || (conePos >= dof)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone position %D of point %D is not in the valid range [0, %D)", conePos, p, dof);
1194:   mesh->coneOrientations[off+conePos] = coneOrientation;
1195:   return(0);
1196: }

1200: /*@
1201:   DMPlexGetSupportSize - Return the number of out-edges for this point in the Sieve DAG

1203:   Not collective

1205:   Input Parameters:
1206: + mesh - The DMPlex
1207: - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()

1209:   Output Parameter:
1210: . size - The support size for point p

1212:   Level: beginner

1214: .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
1215: @*/
1216: PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
1217: {
1218:   DM_Plex       *mesh = (DM_Plex*) dm->data;

1224:   PetscSectionGetDof(mesh->supportSection, p, size);
1225:   return(0);
1226: }

1230: /*@
1231:   DMPlexSetSupportSize - Set the number of out-edges for this point in the Sieve DAG

1233:   Not collective

1235:   Input Parameters:
1236: + mesh - The DMPlex
1237: . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1238: - size - The support size for point p

1240:   Output Parameter:

1242:   Note:
1243:   This should be called after DMPlexSetChart().

1245:   Level: beginner

1247: .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
1248: @*/
1249: PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
1250: {
1251:   DM_Plex       *mesh = (DM_Plex*) dm->data;

1256:   PetscSectionSetDof(mesh->supportSection, p, size);

1258:   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
1259:   return(0);
1260: }

1264: /*@C
1265:   DMPlexGetSupport - Return the points on the out-edges for this point in the Sieve DAG

1267:   Not collective

1269:   Input Parameters:
1270: + mesh - The DMPlex
1271: - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()

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

1276:   Level: beginner

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

1282:   You must also call DMPlexRestoreSupport() after you finish using the returned array.

1284: .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1285: @*/
1286: PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
1287: {
1288:   DM_Plex       *mesh = (DM_Plex*) dm->data;
1289:   PetscInt       off;

1295:   PetscSectionGetOffset(mesh->supportSection, p, &off);
1296:   *support = &mesh->supports[off];
1297:   return(0);
1298: }

1302: /*@
1303:   DMPlexSetSupport - Set the points on the out-edges for this point in the Sieve DAG

1305:   Not collective

1307:   Input Parameters:
1308: + mesh - The DMPlex
1309: . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1310: - support - An array of points which are on the in-edges for point p

1312:   Output Parameter:

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

1317:   Level: beginner

1319: .seealso: DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
1320: @*/
1321: PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
1322: {
1323:   DM_Plex       *mesh = (DM_Plex*) dm->data;
1324:   PetscInt       pStart, pEnd;
1325:   PetscInt       dof, off, c;

1330:   PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);
1331:   PetscSectionGetDof(mesh->supportSection, p, &dof);
1333:   PetscSectionGetOffset(mesh->supportSection, p, &off);
1334:   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
1335:   for (c = 0; c < dof; ++c) {
1336:     if ((support[c] < pStart) || (support[c] >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Support point %D is not in the valid range [%D, %D)", support[c], pStart, pEnd);
1337:     mesh->supports[off+c] = support[c];
1338:   }
1339:   return(0);
1340: }

1344: PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
1345: {
1346:   DM_Plex       *mesh = (DM_Plex*) dm->data;
1347:   PetscInt       pStart, pEnd;
1348:   PetscInt       dof, off;

1353:   PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);
1354:   PetscSectionGetDof(mesh->supportSection, p, &dof);
1355:   PetscSectionGetOffset(mesh->supportSection, p, &off);
1356:   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
1357:   if ((supportPoint < pStart) || (supportPoint >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Support point %D is not in the valid range [%D, %D)", supportPoint, pStart, pEnd);
1358:   if (supportPos >= dof) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Support position %D of point %D is not in the valid range [0, %D)", supportPos, p, dof);
1359:   mesh->supports[off+supportPos] = supportPoint;
1360:   return(0);
1361: }

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

1368:   Not collective

1370:   Input Parameters:
1371: + mesh - The DMPlex
1372: . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1373: . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
1374: - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used

1376:   Output Parameters:
1377: + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
1378: - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]

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

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

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

1389:   Level: beginner

1391: .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1392: @*/
1393: PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
1394: {
1395:   DM_Plex        *mesh = (DM_Plex*) dm->data;
1396:   PetscInt       *closure, *fifo;
1397:   const PetscInt *tmp = NULL, *tmpO = NULL;
1398:   PetscInt        tmpSize, t;
1399:   PetscInt        depth       = 0, maxSize;
1400:   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
1401:   PetscErrorCode  ierr;

1405:   DMPlexGetDepth(dm, &depth);
1406:   /* This is only 1-level */
1407:   if (useCone) {
1408:     DMPlexGetConeSize(dm, p, &tmpSize);
1409:     DMPlexGetCone(dm, p, &tmp);
1410:     DMPlexGetConeOrientation(dm, p, &tmpO);
1411:   } else {
1412:     DMPlexGetSupportSize(dm, p, &tmpSize);
1413:     DMPlexGetSupport(dm, p, &tmp);
1414:   }
1415:   if (depth == 1) {
1416:     if (*points) {
1417:       closure = *points;
1418:     } else {
1419:       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
1420:       DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);
1421:     }
1422:     closure[0] = p; closure[1] = 0;
1423:     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
1424:       closure[closureSize]   = tmp[t];
1425:       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
1426:     }
1427:     if (numPoints) *numPoints = closureSize/2;
1428:     if (points)    *points    = closure;
1429:     return(0);
1430:   }
1431:   {
1432:     PetscInt c, coneSeries, s,supportSeries;

1434:     c = mesh->maxConeSize;
1435:     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
1436:     s = mesh->maxSupportSize;
1437:     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
1438:     maxSize = 2*PetscMax(coneSeries,supportSeries);
1439:   }
1440:   DMGetWorkArray(dm, maxSize, PETSC_INT, &fifo);
1441:   if (*points) {
1442:     closure = *points;
1443:   } else {
1444:     DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);
1445:   }
1446:   closure[0] = p; closure[1] = 0;
1447:   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
1448:     const PetscInt cp = tmp[t];
1449:     const PetscInt co = tmpO ? tmpO[t] : 0;

1451:     closure[closureSize]   = cp;
1452:     closure[closureSize+1] = co;
1453:     fifo[fifoSize]         = cp;
1454:     fifo[fifoSize+1]       = co;
1455:   }
1456:   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
1457:   while (fifoSize - fifoStart) {
1458:     const PetscInt q   = fifo[fifoStart];
1459:     const PetscInt o   = fifo[fifoStart+1];
1460:     const PetscInt rev = o >= 0 ? 0 : 1;
1461:     const PetscInt off = rev ? -(o+1) : o;

1463:     if (useCone) {
1464:       DMPlexGetConeSize(dm, q, &tmpSize);
1465:       DMPlexGetCone(dm, q, &tmp);
1466:       DMPlexGetConeOrientation(dm, q, &tmpO);
1467:     } else {
1468:       DMPlexGetSupportSize(dm, q, &tmpSize);
1469:       DMPlexGetSupport(dm, q, &tmp);
1470:       tmpO = NULL;
1471:     }
1472:     for (t = 0; t < tmpSize; ++t) {
1473:       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
1474:       const PetscInt cp = tmp[i];
1475:       /* Must propogate orientation: When we reverse orientation, we both reverse the direction of iteration and start at the other end of the chain. */
1476:       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
1477:        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
1478:       PetscInt       co = tmpO ? tmpO[i] : 0;
1479:       PetscInt       c;

1481:       if (rev) {
1482:         PetscInt childSize, coff;
1483:         DMPlexGetConeSize(dm, cp, &childSize);
1484:         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
1485:         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
1486:       }
1487:       /* Check for duplicate */
1488:       for (c = 0; c < closureSize; c += 2) {
1489:         if (closure[c] == cp) break;
1490:       }
1491:       if (c == closureSize) {
1492:         closure[closureSize]   = cp;
1493:         closure[closureSize+1] = co;
1494:         fifo[fifoSize]         = cp;
1495:         fifo[fifoSize+1]       = co;
1496:         closureSize           += 2;
1497:         fifoSize              += 2;
1498:       }
1499:     }
1500:     fifoStart += 2;
1501:   }
1502:   if (numPoints) *numPoints = closureSize/2;
1503:   if (points)    *points    = closure;
1504:   DMRestoreWorkArray(dm, maxSize, PETSC_INT, &fifo);
1505:   return(0);
1506: }

1510: /*@C
1511:   DMPlexGetTransitiveClosure_Internal - Return the points on the transitive closure of the in-edges or out-edges for this point in the Sieve DAG with a specified initial orientation

1513:   Not collective

1515:   Input Parameters:
1516: + mesh - The DMPlex
1517: . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1518: . orientation - The orientation of the point
1519: . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
1520: - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used

1522:   Output Parameters:
1523: + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
1524: - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]

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

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

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

1535:   Level: beginner

1537: .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1538: @*/
1539: PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
1540: {
1541:   DM_Plex        *mesh = (DM_Plex*) dm->data;
1542:   PetscInt       *closure, *fifo;
1543:   const PetscInt *tmp = NULL, *tmpO = NULL;
1544:   PetscInt        tmpSize, t;
1545:   PetscInt        depth       = 0, maxSize;
1546:   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
1547:   PetscErrorCode  ierr;

1551:   DMPlexGetDepth(dm, &depth);
1552:   /* This is only 1-level */
1553:   if (useCone) {
1554:     DMPlexGetConeSize(dm, p, &tmpSize);
1555:     DMPlexGetCone(dm, p, &tmp);
1556:     DMPlexGetConeOrientation(dm, p, &tmpO);
1557:   } else {
1558:     DMPlexGetSupportSize(dm, p, &tmpSize);
1559:     DMPlexGetSupport(dm, p, &tmp);
1560:   }
1561:   if (depth == 1) {
1562:     if (*points) {
1563:       closure = *points;
1564:     } else {
1565:       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
1566:       DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);
1567:     }
1568:     closure[0] = p; closure[1] = ornt;
1569:     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
1570:       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
1571:       closure[closureSize]   = tmp[i];
1572:       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
1573:     }
1574:     if (numPoints) *numPoints = closureSize/2;
1575:     if (points)    *points    = closure;
1576:     return(0);
1577:   }
1578:   {
1579:     PetscInt c, coneSeries, s,supportSeries;

1581:     c = mesh->maxConeSize;
1582:     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
1583:     s = mesh->maxSupportSize;
1584:     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
1585:     maxSize = 2*PetscMax(coneSeries,supportSeries);
1586:   }
1587:   DMGetWorkArray(dm, maxSize, PETSC_INT, &fifo);
1588:   if (*points) {
1589:     closure = *points;
1590:   } else {
1591:     DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);
1592:   }
1593:   closure[0] = p; closure[1] = ornt;
1594:   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
1595:     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
1596:     const PetscInt cp = tmp[i];
1597:     PetscInt       co = tmpO ? tmpO[i] : 0;

1599:     if (ornt < 0) {
1600:       PetscInt childSize, coff;
1601:       DMPlexGetConeSize(dm, cp, &childSize);
1602:       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
1603:       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
1604:     }
1605:     closure[closureSize]   = cp;
1606:     closure[closureSize+1] = co;
1607:     fifo[fifoSize]         = cp;
1608:     fifo[fifoSize+1]       = co;
1609:   }
1610:   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
1611:   while (fifoSize - fifoStart) {
1612:     const PetscInt q   = fifo[fifoStart];
1613:     const PetscInt o   = fifo[fifoStart+1];
1614:     const PetscInt rev = o >= 0 ? 0 : 1;
1615:     const PetscInt off = rev ? -(o+1) : o;

1617:     if (useCone) {
1618:       DMPlexGetConeSize(dm, q, &tmpSize);
1619:       DMPlexGetCone(dm, q, &tmp);
1620:       DMPlexGetConeOrientation(dm, q, &tmpO);
1621:     } else {
1622:       DMPlexGetSupportSize(dm, q, &tmpSize);
1623:       DMPlexGetSupport(dm, q, &tmp);
1624:       tmpO = NULL;
1625:     }
1626:     for (t = 0; t < tmpSize; ++t) {
1627:       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
1628:       const PetscInt cp = tmp[i];
1629:       /* Must propogate orientation: When we reverse orientation, we both reverse the direction of iteration and start at the other end of the chain. */
1630:       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
1631:        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
1632:       PetscInt       co = tmpO ? tmpO[i] : 0;
1633:       PetscInt       c;

1635:       if (rev) {
1636:         PetscInt childSize, coff;
1637:         DMPlexGetConeSize(dm, cp, &childSize);
1638:         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
1639:         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
1640:       }
1641:       /* Check for duplicate */
1642:       for (c = 0; c < closureSize; c += 2) {
1643:         if (closure[c] == cp) break;
1644:       }
1645:       if (c == closureSize) {
1646:         closure[closureSize]   = cp;
1647:         closure[closureSize+1] = co;
1648:         fifo[fifoSize]         = cp;
1649:         fifo[fifoSize+1]       = co;
1650:         closureSize           += 2;
1651:         fifoSize              += 2;
1652:       }
1653:     }
1654:     fifoStart += 2;
1655:   }
1656:   if (numPoints) *numPoints = closureSize/2;
1657:   if (points)    *points    = closure;
1658:   DMRestoreWorkArray(dm, maxSize, PETSC_INT, &fifo);
1659:   return(0);
1660: }

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

1667:   Not collective

1669:   Input Parameters:
1670: + mesh - The DMPlex
1671: . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1672: . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
1673: . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
1674: - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit

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

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

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

1685:   Level: beginner

1687: .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1688: @*/
1689: PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
1690: {

1697:   DMRestoreWorkArray(dm, 0, PETSC_INT, points);
1698:   if (numPoints) *numPoints = 0;
1699:   return(0);
1700: }

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

1707:   Not collective

1709:   Input Parameter:
1710: . mesh - The DMPlex

1712:   Output Parameters:
1713: + maxConeSize - The maximum number of in-edges
1714: - maxSupportSize - The maximum number of out-edges

1716:   Level: beginner

1718: .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1719: @*/
1720: PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
1721: {
1722:   DM_Plex *mesh = (DM_Plex*) dm->data;

1726:   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
1727:   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
1728:   return(0);
1729: }

1733: PetscErrorCode DMSetUp_Plex(DM dm)
1734: {
1735:   DM_Plex       *mesh = (DM_Plex*) dm->data;
1736:   PetscInt       size;

1741:   PetscSectionSetUp(mesh->coneSection);
1742:   PetscSectionGetStorageSize(mesh->coneSection, &size);
1743:   PetscMalloc1(size, &mesh->cones);
1744:   PetscCalloc1(size, &mesh->coneOrientations);
1745:   if (mesh->maxSupportSize) {
1746:     PetscSectionSetUp(mesh->supportSection);
1747:     PetscSectionGetStorageSize(mesh->supportSection, &size);
1748:     PetscMalloc1(size, &mesh->supports);
1749:   }
1750:   return(0);
1751: }

1755: PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1756: {

1760:   if (subdm) {DMClone(dm, subdm);}
1761:   DMCreateSubDM_Section_Private(dm, numFields, fields, is, subdm);
1762:   return(0);
1763: }

1767: /*@
1768:   DMPlexSymmetrize - Creates support (out-edge) information from cone (in-edge) inoformation

1770:   Not collective

1772:   Input Parameter:
1773: . mesh - The DMPlex

1775:   Output Parameter:

1777:   Note:
1778:   This should be called after all calls to DMPlexSetCone()

1780:   Level: beginner

1782: .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
1783: @*/
1784: PetscErrorCode DMPlexSymmetrize(DM dm)
1785: {
1786:   DM_Plex       *mesh = (DM_Plex*) dm->data;
1787:   PetscInt      *offsets;
1788:   PetscInt       supportSize;
1789:   PetscInt       pStart, pEnd, p;

1794:   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
1795:   /* Calculate support sizes */
1796:   DMPlexGetChart(dm, &pStart, &pEnd);
1797:   for (p = pStart; p < pEnd; ++p) {
1798:     PetscInt dof, off, c;

1800:     PetscSectionGetDof(mesh->coneSection, p, &dof);
1801:     PetscSectionGetOffset(mesh->coneSection, p, &off);
1802:     for (c = off; c < off+dof; ++c) {
1803:       PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);
1804:     }
1805:   }
1806:   for (p = pStart; p < pEnd; ++p) {
1807:     PetscInt dof;

1809:     PetscSectionGetDof(mesh->supportSection, p, &dof);

1811:     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
1812:   }
1813:   PetscSectionSetUp(mesh->supportSection);
1814:   /* Calculate supports */
1815:   PetscSectionGetStorageSize(mesh->supportSection, &supportSize);
1816:   PetscMalloc1(supportSize, &mesh->supports);
1817:   PetscCalloc1(pEnd - pStart, &offsets);
1818:   for (p = pStart; p < pEnd; ++p) {
1819:     PetscInt dof, off, c;

1821:     PetscSectionGetDof(mesh->coneSection, p, &dof);
1822:     PetscSectionGetOffset(mesh->coneSection, p, &off);
1823:     for (c = off; c < off+dof; ++c) {
1824:       const PetscInt q = mesh->cones[c];
1825:       PetscInt       offS;

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

1829:       mesh->supports[offS+offsets[q]] = p;
1830:       ++offsets[q];
1831:     }
1832:   }
1833:   PetscFree(offsets);
1834:   return(0);
1835: }

1839: /*@
1840:   DMPlexStratify - The Sieve DAG for most topologies is a graded poset (http://en.wikipedia.org/wiki/Graded_poset), and
1841:   can be illustrated by Hasse Diagram (a http://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
1842:   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
1843:   the DAG.

1845:   Collective on dm

1847:   Input Parameter:
1848: . mesh - The DMPlex

1850:   Output Parameter:

1852:   Notes:
1853:   Concretely, DMPlexStratify() creates a new label named "depth" containing the dimension of each element: 0 for vertices,
1854:   1 for edges, and so on.  The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
1855:   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
1856:   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.

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

1860:   Level: beginner

1862: .seealso: DMPlexCreate(), DMPlexSymmetrize()
1863: @*/
1864: PetscErrorCode DMPlexStratify(DM dm)
1865: {
1866:   DM_Plex       *mesh = (DM_Plex*) dm->data;
1867:   DMLabel        label;
1868:   PetscInt       pStart, pEnd, p;
1869:   PetscInt       numRoots = 0, numLeaves = 0;

1874:   PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);
1875:   /* Calculate depth */
1876:   DMPlexGetChart(dm, &pStart, &pEnd);
1877:   DMCreateLabel(dm, "depth");
1878:   DMPlexGetDepthLabel(dm, &label);
1879:   /* Initialize roots and count leaves */
1880:   for (p = pStart; p < pEnd; ++p) {
1881:     PetscInt coneSize, supportSize;

1883:     DMPlexGetConeSize(dm, p, &coneSize);
1884:     DMPlexGetSupportSize(dm, p, &supportSize);
1885:     if (!coneSize && supportSize) {
1886:       ++numRoots;
1887:       DMLabelSetValue(label, p, 0);
1888:     } else if (!supportSize && coneSize) {
1889:       ++numLeaves;
1890:     } else if (!supportSize && !coneSize) {
1891:       /* Isolated points */
1892:       DMLabelSetValue(label, p, 0);
1893:     }
1894:   }
1895:   if (numRoots + numLeaves == (pEnd - pStart)) {
1896:     for (p = pStart; p < pEnd; ++p) {
1897:       PetscInt coneSize, supportSize;

1899:       DMPlexGetConeSize(dm, p, &coneSize);
1900:       DMPlexGetSupportSize(dm, p, &supportSize);
1901:       if (!supportSize && coneSize) {
1902:         DMLabelSetValue(label, p, 1);
1903:       }
1904:     }
1905:   } else {
1906:     IS       pointIS;
1907:     PetscInt numPoints = 0, level = 0;

1909:     DMLabelGetStratumIS(label, level, &pointIS);
1910:     if (pointIS) {ISGetLocalSize(pointIS, &numPoints);}
1911:     while (numPoints) {
1912:       const PetscInt *points;
1913:       const PetscInt  newLevel = level+1;

1915:       ISGetIndices(pointIS, &points);
1916:       for (p = 0; p < numPoints; ++p) {
1917:         const PetscInt  point = points[p];
1918:         const PetscInt *support;
1919:         PetscInt        supportSize, s;

1921:         DMPlexGetSupportSize(dm, point, &supportSize);
1922:         DMPlexGetSupport(dm, point, &support);
1923:         for (s = 0; s < supportSize; ++s) {
1924:           DMLabelSetValue(label, support[s], newLevel);
1925:         }
1926:       }
1927:       ISRestoreIndices(pointIS, &points);
1928:       ++level;
1929:       ISDestroy(&pointIS);
1930:       DMLabelGetStratumIS(label, level, &pointIS);
1931:       if (pointIS) {ISGetLocalSize(pointIS, &numPoints);}
1932:       else         {numPoints = 0;}
1933:     }
1934:     ISDestroy(&pointIS);
1935:   }
1936:   { /* just in case there is an empty process */
1937:     PetscInt numValues, maxValues = 0, v;

1939:     DMLabelGetNumValues(label,&numValues);
1940:     MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));
1941:     for (v = numValues; v < maxValues; v++) {
1942:       DMLabelAddStratum(label,v);
1943:     }
1944:   }

1946:   DMLabelGetState(label, &mesh->depthState);
1947:   PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);
1948:   return(0);
1949: }

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

1956:   Not Collective

1958:   Input Parameters:
1959: + dm - The DMPlex object
1960: . numPoints - The number of input points for the join
1961: - points - The input points

1963:   Output Parameters:
1964: + numCoveredPoints - The number of points in the join
1965: - coveredPoints - The points in the join

1967:   Level: intermediate

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

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

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

1977: .keywords: mesh
1978: .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
1979: @*/
1980: PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
1981: {
1982:   DM_Plex       *mesh = (DM_Plex*) dm->data;
1983:   PetscInt      *join[2];
1984:   PetscInt       joinSize, i = 0;
1985:   PetscInt       dof, off, p, c, m;

1993:   DMGetWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[0]);
1994:   DMGetWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1]);
1995:   /* Copy in support of first point */
1996:   PetscSectionGetDof(mesh->supportSection, points[0], &dof);
1997:   PetscSectionGetOffset(mesh->supportSection, points[0], &off);
1998:   for (joinSize = 0; joinSize < dof; ++joinSize) {
1999:     join[i][joinSize] = mesh->supports[off+joinSize];
2000:   }
2001:   /* Check each successive support */
2002:   for (p = 1; p < numPoints; ++p) {
2003:     PetscInt newJoinSize = 0;

2005:     PetscSectionGetDof(mesh->supportSection, points[p], &dof);
2006:     PetscSectionGetOffset(mesh->supportSection, points[p], &off);
2007:     for (c = 0; c < dof; ++c) {
2008:       const PetscInt point = mesh->supports[off+c];

2010:       for (m = 0; m < joinSize; ++m) {
2011:         if (point == join[i][m]) {
2012:           join[1-i][newJoinSize++] = point;
2013:           break;
2014:         }
2015:       }
2016:     }
2017:     joinSize = newJoinSize;
2018:     i        = 1-i;
2019:   }
2020:   *numCoveredPoints = joinSize;
2021:   *coveredPoints    = join[i];
2022:   DMRestoreWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1-i]);
2023:   return(0);
2024: }

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

2031:   Not Collective

2033:   Input Parameters:
2034: + dm - The DMPlex object
2035: . numPoints - The number of input points for the join
2036: - points - The input points

2038:   Output Parameters:
2039: + numCoveredPoints - The number of points in the join
2040: - coveredPoints - The points in the join

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

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

2048:   Level: intermediate

2050: .keywords: mesh
2051: .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
2052: @*/
2053: PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2054: {

2062:   DMRestoreWorkArray(dm, 0, PETSC_INT, (void*) coveredPoints);
2063:   if (numCoveredPoints) *numCoveredPoints = 0;
2064:   return(0);
2065: }

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

2072:   Not Collective

2074:   Input Parameters:
2075: + dm - The DMPlex object
2076: . numPoints - The number of input points for the join
2077: - points - The input points

2079:   Output Parameters:
2080: + numCoveredPoints - The number of points in the join
2081: - coveredPoints - The points in the join

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

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

2089:   Level: intermediate

2091: .keywords: mesh
2092: .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
2093: @*/
2094: PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2095: {
2096:   DM_Plex       *mesh = (DM_Plex*) dm->data;
2097:   PetscInt      *offsets, **closures;
2098:   PetscInt      *join[2];
2099:   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
2100:   PetscInt       p, d, c, m, ms;


2109:   DMPlexGetDepth(dm, &depth);
2110:   PetscCalloc1(numPoints, &closures);
2111:   DMGetWorkArray(dm, numPoints*(depth+2), PETSC_INT, &offsets);
2112:   ms      = mesh->maxSupportSize;
2113:   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
2114:   DMGetWorkArray(dm, maxSize, PETSC_INT, &join[0]);
2115:   DMGetWorkArray(dm, maxSize, PETSC_INT, &join[1]);

2117:   for (p = 0; p < numPoints; ++p) {
2118:     PetscInt closureSize;

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

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

2126:       DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);
2127:       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
2128:         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2129:           offsets[p*(depth+2)+d+1] = i;
2130:           break;
2131:         }
2132:       }
2133:       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
2134:     }
2135:     if (offsets[p*(depth+2)+depth+1] != closureSize) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Total size of closure %D should be %D", offsets[p*(depth+2)+depth+1], closureSize);
2136:   }
2137:   for (d = 0; d < depth+1; ++d) {
2138:     PetscInt dof;

2140:     /* Copy in support of first point */
2141:     dof = offsets[d+1] - offsets[d];
2142:     for (joinSize = 0; joinSize < dof; ++joinSize) {
2143:       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
2144:     }
2145:     /* Check each successive cone */
2146:     for (p = 1; p < numPoints && joinSize; ++p) {
2147:       PetscInt newJoinSize = 0;

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

2153:         for (m = 0; m < joinSize; ++m) {
2154:           if (point == join[i][m]) {
2155:             join[1-i][newJoinSize++] = point;
2156:             break;
2157:           }
2158:         }
2159:       }
2160:       joinSize = newJoinSize;
2161:       i        = 1-i;
2162:     }
2163:     if (joinSize) break;
2164:   }
2165:   *numCoveredPoints = joinSize;
2166:   *coveredPoints    = join[i];
2167:   for (p = 0; p < numPoints; ++p) {
2168:     DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);
2169:   }
2170:   PetscFree(closures);
2171:   DMRestoreWorkArray(dm, numPoints*(depth+2), PETSC_INT, &offsets);
2172:   DMRestoreWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1-i]);
2173:   return(0);
2174: }

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

2181:   Not Collective

2183:   Input Parameters:
2184: + dm - The DMPlex object
2185: . numPoints - The number of input points for the meet
2186: - points - The input points

2188:   Output Parameters:
2189: + numCoveredPoints - The number of points in the meet
2190: - coveredPoints - The points in the meet

2192:   Level: intermediate

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

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

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

2202: .keywords: mesh
2203: .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
2204: @*/
2205: PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
2206: {
2207:   DM_Plex       *mesh = (DM_Plex*) dm->data;
2208:   PetscInt      *meet[2];
2209:   PetscInt       meetSize, i = 0;
2210:   PetscInt       dof, off, p, c, m;

2218:   DMGetWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[0]);
2219:   DMGetWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1]);
2220:   /* Copy in cone of first point */
2221:   PetscSectionGetDof(mesh->coneSection, points[0], &dof);
2222:   PetscSectionGetOffset(mesh->coneSection, points[0], &off);
2223:   for (meetSize = 0; meetSize < dof; ++meetSize) {
2224:     meet[i][meetSize] = mesh->cones[off+meetSize];
2225:   }
2226:   /* Check each successive cone */
2227:   for (p = 1; p < numPoints; ++p) {
2228:     PetscInt newMeetSize = 0;

2230:     PetscSectionGetDof(mesh->coneSection, points[p], &dof);
2231:     PetscSectionGetOffset(mesh->coneSection, points[p], &off);
2232:     for (c = 0; c < dof; ++c) {
2233:       const PetscInt point = mesh->cones[off+c];

2235:       for (m = 0; m < meetSize; ++m) {
2236:         if (point == meet[i][m]) {
2237:           meet[1-i][newMeetSize++] = point;
2238:           break;
2239:         }
2240:       }
2241:     }
2242:     meetSize = newMeetSize;
2243:     i        = 1-i;
2244:   }
2245:   *numCoveringPoints = meetSize;
2246:   *coveringPoints    = meet[i];
2247:   DMRestoreWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1-i]);
2248:   return(0);
2249: }

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

2256:   Not Collective

2258:   Input Parameters:
2259: + dm - The DMPlex object
2260: . numPoints - The number of input points for the meet
2261: - points - The input points

2263:   Output Parameters:
2264: + numCoveredPoints - The number of points in the meet
2265: - coveredPoints - The points in the meet

2267:   Level: intermediate

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

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

2275: .keywords: mesh
2276: .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
2277: @*/
2278: PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2279: {

2287:   DMRestoreWorkArray(dm, 0, PETSC_INT, (void*) coveredPoints);
2288:   if (numCoveredPoints) *numCoveredPoints = 0;
2289:   return(0);
2290: }

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

2297:   Not Collective

2299:   Input Parameters:
2300: + dm - The DMPlex object
2301: . numPoints - The number of input points for the meet
2302: - points - The input points

2304:   Output Parameters:
2305: + numCoveredPoints - The number of points in the meet
2306: - coveredPoints - The points in the meet

2308:   Level: intermediate

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

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

2316: .keywords: mesh
2317: .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
2318: @*/
2319: PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2320: {
2321:   DM_Plex       *mesh = (DM_Plex*) dm->data;
2322:   PetscInt      *offsets, **closures;
2323:   PetscInt      *meet[2];
2324:   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
2325:   PetscInt       p, h, c, m, mc;


2334:   DMPlexGetDepth(dm, &height);
2335:   PetscMalloc1(numPoints, &closures);
2336:   DMGetWorkArray(dm, numPoints*(height+2), PETSC_INT, &offsets);
2337:   mc      = mesh->maxConeSize;
2338:   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
2339:   DMGetWorkArray(dm, maxSize, PETSC_INT, &meet[0]);
2340:   DMGetWorkArray(dm, maxSize, PETSC_INT, &meet[1]);

2342:   for (p = 0; p < numPoints; ++p) {
2343:     PetscInt closureSize;

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

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

2351:       DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);
2352:       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
2353:         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2354:           offsets[p*(height+2)+h+1] = i;
2355:           break;
2356:         }
2357:       }
2358:       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
2359:     }
2360:     if (offsets[p*(height+2)+height+1] != closureSize) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Total size of closure %D should be %D", offsets[p*(height+2)+height+1], closureSize);
2361:   }
2362:   for (h = 0; h < height+1; ++h) {
2363:     PetscInt dof;

2365:     /* Copy in cone of first point */
2366:     dof = offsets[h+1] - offsets[h];
2367:     for (meetSize = 0; meetSize < dof; ++meetSize) {
2368:       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
2369:     }
2370:     /* Check each successive cone */
2371:     for (p = 1; p < numPoints && meetSize; ++p) {
2372:       PetscInt newMeetSize = 0;

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

2378:         for (m = 0; m < meetSize; ++m) {
2379:           if (point == meet[i][m]) {
2380:             meet[1-i][newMeetSize++] = point;
2381:             break;
2382:           }
2383:         }
2384:       }
2385:       meetSize = newMeetSize;
2386:       i        = 1-i;
2387:     }
2388:     if (meetSize) break;
2389:   }
2390:   *numCoveredPoints = meetSize;
2391:   *coveredPoints    = meet[i];
2392:   for (p = 0; p < numPoints; ++p) {
2393:     DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);
2394:   }
2395:   PetscFree(closures);
2396:   DMRestoreWorkArray(dm, numPoints*(height+2), PETSC_INT, &offsets);
2397:   DMRestoreWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1-i]);
2398:   return(0);
2399: }

2403: /*@C
2404:   DMPlexEqual - Determine if two DMs have the same topology

2406:   Not Collective

2408:   Input Parameters:
2409: + dmA - A DMPlex object
2410: - dmB - A DMPlex object

2412:   Output Parameters:
2413: . equal - PETSC_TRUE if the topologies are identical

2415:   Level: intermediate

2417:   Notes:
2418:   We are not solving graph isomorphism, so we do not permutation.

2420: .keywords: mesh
2421: .seealso: DMPlexGetCone()
2422: @*/
2423: PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
2424: {
2425:   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;


2433:   *equal = PETSC_FALSE;
2434:   DMPlexGetDepth(dmA, &depth);
2435:   DMPlexGetDepth(dmB, &depthB);
2436:   if (depth != depthB) return(0);
2437:   DMPlexGetChart(dmA, &pStart,  &pEnd);
2438:   DMPlexGetChart(dmB, &pStartB, &pEndB);
2439:   if ((pStart != pStartB) || (pEnd != pEndB)) return(0);
2440:   for (p = pStart; p < pEnd; ++p) {
2441:     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
2442:     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;

2444:     DMPlexGetConeSize(dmA, p, &coneSize);
2445:     DMPlexGetCone(dmA, p, &cone);
2446:     DMPlexGetConeOrientation(dmA, p, &ornt);
2447:     DMPlexGetConeSize(dmB, p, &coneSizeB);
2448:     DMPlexGetCone(dmB, p, &coneB);
2449:     DMPlexGetConeOrientation(dmB, p, &orntB);
2450:     if (coneSize != coneSizeB) return(0);
2451:     for (c = 0; c < coneSize; ++c) {
2452:       if (cone[c] != coneB[c]) return(0);
2453:       if (ornt[c] != orntB[c]) return(0);
2454:     }
2455:     DMPlexGetSupportSize(dmA, p, &supportSize);
2456:     DMPlexGetSupport(dmA, p, &support);
2457:     DMPlexGetSupportSize(dmB, p, &supportSizeB);
2458:     DMPlexGetSupport(dmB, p, &supportB);
2459:     if (supportSize != supportSizeB) return(0);
2460:     for (s = 0; s < supportSize; ++s) {
2461:       if (support[s] != supportB[s]) return(0);
2462:     }
2463:   }
2464:   *equal = PETSC_TRUE;
2465:   return(0);
2466: }

2470: PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
2471: {
2472:   MPI_Comm       comm;

2476:   PetscObjectGetComm((PetscObject)dm,&comm);
2478:   switch (cellDim) {
2479:   case 0:
2480:     *numFaceVertices = 0;
2481:     break;
2482:   case 1:
2483:     *numFaceVertices = 1;
2484:     break;
2485:   case 2:
2486:     switch (numCorners) {
2487:     case 3: /* triangle */
2488:       *numFaceVertices = 2; /* Edge has 2 vertices */
2489:       break;
2490:     case 4: /* quadrilateral */
2491:       *numFaceVertices = 2; /* Edge has 2 vertices */
2492:       break;
2493:     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
2494:       *numFaceVertices = 3; /* Edge has 3 vertices */
2495:       break;
2496:     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
2497:       *numFaceVertices = 3; /* Edge has 3 vertices */
2498:       break;
2499:     default:
2500:       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
2501:     }
2502:     break;
2503:   case 3:
2504:     switch (numCorners) {
2505:     case 4: /* tetradehdron */
2506:       *numFaceVertices = 3; /* Face has 3 vertices */
2507:       break;
2508:     case 6: /* tet cohesive cells */
2509:       *numFaceVertices = 4; /* Face has 4 vertices */
2510:       break;
2511:     case 8: /* hexahedron */
2512:       *numFaceVertices = 4; /* Face has 4 vertices */
2513:       break;
2514:     case 9: /* tet cohesive Lagrange cells */
2515:       *numFaceVertices = 6; /* Face has 6 vertices */
2516:       break;
2517:     case 10: /* quadratic tetrahedron */
2518:       *numFaceVertices = 6; /* Face has 6 vertices */
2519:       break;
2520:     case 12: /* hex cohesive Lagrange cells */
2521:       *numFaceVertices = 6; /* Face has 6 vertices */
2522:       break;
2523:     case 18: /* quadratic tet cohesive Lagrange cells */
2524:       *numFaceVertices = 6; /* Face has 6 vertices */
2525:       break;
2526:     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
2527:       *numFaceVertices = 9; /* Face has 9 vertices */
2528:       break;
2529:     default:
2530:       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
2531:     }
2532:     break;
2533:   default:
2534:     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
2535:   }
2536:   return(0);
2537: }

2541: /*@
2542:   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point

2544:   Not Collective

2546:   Input Parameter:
2547: . dm    - The DMPlex object

2549:   Output Parameter:
2550: . depthLabel - The DMLabel recording point depth

2552:   Level: developer

2554: .keywords: mesh, points
2555: .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
2556: @*/
2557: PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
2558: {

2564:   if (!dm->depthLabel) {DMGetLabel(dm, "depth", &dm->depthLabel);}
2565:   *depthLabel = dm->depthLabel;
2566:   return(0);
2567: }

2571: /*@
2572:   DMPlexGetDepth - Get the depth of the DAG representing this mesh

2574:   Not Collective

2576:   Input Parameter:
2577: . dm    - The DMPlex object

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

2582:   Level: developer

2584: .keywords: mesh, points
2585: .seealso: DMPlexGetDepthLabel(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
2586: @*/
2587: PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
2588: {
2589:   DMLabel        label;
2590:   PetscInt       d = 0;

2596:   DMPlexGetDepthLabel(dm, &label);
2597:   if (label) {DMLabelGetNumValues(label, &d);}
2598:   *depth = d-1;
2599:   return(0);
2600: }

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

2607:   Not Collective

2609:   Input Parameters:
2610: + dm           - The DMPlex object
2611: - stratumValue - The requested depth

2613:   Output Parameters:
2614: + start - The first point at this depth
2615: - end   - One beyond the last point at this depth

2617:   Level: developer

2619: .keywords: mesh, points
2620: .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth()
2621: @*/
2622: PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
2623: {
2624:   DMLabel        label;
2625:   PetscInt       pStart, pEnd;

2632:   DMPlexGetChart(dm, &pStart, &pEnd);
2633:   if (pStart == pEnd) return(0);
2634:   if (stratumValue < 0) {
2635:     if (start) *start = pStart;
2636:     if (end)   *end   = pEnd;
2637:     return(0);
2638:   }
2639:   DMPlexGetDepthLabel(dm, &label);
2640:   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
2641:   DMLabelGetStratumBounds(label, stratumValue, start, end);
2642:   return(0);
2643: }

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

2650:   Not Collective

2652:   Input Parameters:
2653: + dm           - The DMPlex object
2654: - stratumValue - The requested height

2656:   Output Parameters:
2657: + start - The first point at this height
2658: - end   - One beyond the last point at this height

2660:   Level: developer

2662: .keywords: mesh, points
2663: .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth()
2664: @*/
2665: PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
2666: {
2667:   DMLabel        label;
2668:   PetscInt       depth, pStart, pEnd;

2675:   DMPlexGetChart(dm, &pStart, &pEnd);
2676:   if (pStart == pEnd) return(0);
2677:   if (stratumValue < 0) {
2678:     if (start) *start = pStart;
2679:     if (end)   *end   = pEnd;
2680:     return(0);
2681:   }
2682:   DMPlexGetDepthLabel(dm, &label);
2683:   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
2684:   DMLabelGetNumValues(label, &depth);
2685:   DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);
2686:   return(0);
2687: }

2691: /* Set the number of dof on each point and separate by fields */
2692: PetscErrorCode DMPlexCreateSectionInitial(DM dm, PetscInt dim, PetscInt numFields,const PetscInt numComp[],const PetscInt numDof[], PetscSection *section)
2693: {
2694:   PetscInt      *pMax;
2695:   PetscInt       depth, pStart = 0, pEnd = 0;
2696:   PetscInt       Nf, p, d, dep, f;
2697:   PetscBool     *isFE;

2701:   PetscMalloc1(numFields, &isFE);
2702:   DMGetNumFields(dm, &Nf);
2703:   for (f = 0; f < numFields; ++f) {
2704:     PetscObject  obj;
2705:     PetscClassId id;

2707:     isFE[f] = PETSC_FALSE;
2708:     if (f >= Nf) continue;
2709:     DMGetField(dm, f, &obj);
2710:     PetscObjectGetClassId(obj, &id);
2711:     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
2712:     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
2713:   }
2714:   PetscSectionCreate(PetscObjectComm((PetscObject)dm), section);
2715:   if (numFields > 0) {
2716:     PetscSectionSetNumFields(*section, numFields);
2717:     if (numComp) {
2718:       for (f = 0; f < numFields; ++f) {
2719:         PetscSectionSetFieldComponents(*section, f, numComp[f]);
2720:       }
2721:     }
2722:   }
2723:   DMPlexGetChart(dm, &pStart, &pEnd);
2724:   PetscSectionSetChart(*section, pStart, pEnd);
2725:   DMPlexGetDepth(dm, &depth);
2726:   PetscMalloc1(depth+1,&pMax);
2727:   DMPlexGetHybridBounds(dm, depth >= 0 ? &pMax[depth] : NULL, depth>1 ? &pMax[depth-1] : NULL, depth>2 ? &pMax[1] : NULL, &pMax[0]);
2728:   for (dep = 0; dep <= depth; ++dep) {
2729:     d    = dim == depth ? dep : (!dep ? 0 : dim);
2730:     DMPlexGetDepthStratum(dm, dep, &pStart, &pEnd);
2731:     pMax[dep] = pMax[dep] < 0 ? pEnd : pMax[dep];
2732:     for (p = pStart; p < pEnd; ++p) {
2733:       PetscInt tot = 0;

2735:       for (f = 0; f < numFields; ++f) {
2736:         if (isFE[f] && p >= pMax[dep]) continue;
2737:         PetscSectionSetFieldDof(*section, p, f, numDof[f*(dim+1)+d]);
2738:         tot += numDof[f*(dim+1)+d];
2739:       }
2740:       PetscSectionSetDof(*section, p, tot);
2741:     }
2742:   }
2743:   PetscFree(pMax);
2744:   PetscFree(isFE);
2745:   return(0);
2746: }

2750: /* Set the number of dof on each point and separate by fields
2751:    If bcComps is NULL or the IS is NULL, constrain every dof on the point
2752: */
2753: PetscErrorCode DMPlexCreateSectionBCDof(DM dm, PetscInt numBC, const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
2754: {
2755:   PetscInt       numFields;
2756:   PetscInt       bc;
2757:   PetscSection   aSec;

2761:   PetscSectionGetNumFields(section, &numFields);
2762:   for (bc = 0; bc < numBC; ++bc) {
2763:     PetscInt        field = 0;
2764:     const PetscInt *comp;
2765:     const PetscInt *idx;
2766:     PetscInt        Nc = -1, n, i;

2768:     if (numFields) field = bcField[bc];
2769:     if (bcComps && bcComps[bc]) {ISGetLocalSize(bcComps[bc], &Nc);}
2770:     if (bcComps && bcComps[bc]) {ISGetIndices(bcComps[bc], &comp);}
2771:     ISGetLocalSize(bcPoints[bc], &n);
2772:     ISGetIndices(bcPoints[bc], &idx);
2773:     for (i = 0; i < n; ++i) {
2774:       const PetscInt p = idx[i];
2775:       PetscInt       numConst;

2777:       if (numFields) {
2778:         PetscSectionGetFieldDof(section, p, field, &numConst);
2779:       } else {
2780:         PetscSectionGetDof(section, p, &numConst);
2781:       }
2782:       /* If Nc < 0, constrain every dof on the point */
2783:       if (Nc > 0) numConst = PetscMin(numConst, Nc);
2784:       if (numFields) {PetscSectionAddFieldConstraintDof(section, p, field, numConst);}
2785:       PetscSectionAddConstraintDof(section, p, numConst);
2786:     }
2787:     ISRestoreIndices(bcPoints[bc], &idx);
2788:     if (bcComps && bcComps[bc]) {ISRestoreIndices(bcComps[bc], &comp);}
2789:   }
2790:   DMPlexGetAnchors(dm, &aSec, NULL);
2791:   if (aSec) {
2792:     PetscInt aStart, aEnd, a;

2794:     PetscSectionGetChart(aSec, &aStart, &aEnd);
2795:     for (a = aStart; a < aEnd; a++) {
2796:       PetscInt dof, f;

2798:       PetscSectionGetDof(aSec, a, &dof);
2799:       if (dof) {
2800:         /* if there are point-to-point constraints, then all dofs are constrained */
2801:         PetscSectionGetDof(section, a, &dof);
2802:         PetscSectionSetConstraintDof(section, a, dof);
2803:         for (f = 0; f < numFields; f++) {
2804:           PetscSectionGetFieldDof(section, a, f, &dof);
2805:           PetscSectionSetFieldConstraintDof(section, a, f, dof);
2806:         }
2807:       }
2808:     }
2809:   }
2810:   return(0);
2811: }

2815: /* Set the constrained field indices on each point
2816:    If bcComps is NULL or the IS is NULL, constrain every dof on the point
2817: */
2818: PetscErrorCode DMPlexCreateSectionBCIndicesField(DM dm, PetscInt numBC,const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
2819: {
2820:   PetscSection   aSec;
2821:   PetscInt      *indices;
2822:   PetscInt       numFields, maxDof, pStart, pEnd, p, bc, f, d;

2826:   PetscSectionGetNumFields(section, &numFields);
2827:   if (!numFields) return(0);
2828:   /* Initialize all field indices to -1 */
2829:   PetscSectionGetChart(section, &pStart, &pEnd);
2830:   PetscSectionGetMaxDof(section, &maxDof);
2831:   PetscMalloc1(maxDof, &indices);
2832:   for (d = 0; d < maxDof; ++d) indices[d] = -1;
2833:   for (p = pStart; p < pEnd; ++p) for (f = 0; f < numFields; ++f) {PetscSectionSetFieldConstraintIndices(section, p, f, indices);}
2834:   /* Handle BC constraints */
2835:   for (bc = 0; bc < numBC; ++bc) {
2836:     const PetscInt  field = bcField[bc];
2837:     const PetscInt *comp, *idx;
2838:     PetscInt        Nc = -1, n, i;

2840:     if (bcComps && bcComps[bc]) {ISGetLocalSize(bcComps[bc], &Nc);}
2841:     if (bcComps && bcComps[bc]) {ISGetIndices(bcComps[bc], &comp);}
2842:     ISGetLocalSize(bcPoints[bc], &n);
2843:     ISGetIndices(bcPoints[bc], &idx);
2844:     for (i = 0; i < n; ++i) {
2845:       const PetscInt  p = idx[i];
2846:       const PetscInt *find;
2847:       PetscInt        fcdof, c;

2849:       PetscSectionGetFieldConstraintDof(section, p, field, &fcdof);
2850:       if (Nc < 0) {
2851:         for (d = 0; d < fcdof; ++d) indices[d] = d;
2852:       } else {
2853:         PetscSectionGetFieldConstraintIndices(section, p, field, &find);
2854:         for (d = 0; d < fcdof; ++d) {if (find[d] < 0) break; indices[d] = find[d];}
2855:         for (c = 0; c < Nc; ++c) indices[d+c] = comp[c];
2856:         PetscSortInt(d+Nc, indices);
2857:         for (c = d+Nc; c < fcdof; ++c) indices[c] = -1;
2858:       }
2859:       PetscSectionSetFieldConstraintIndices(section, p, field, indices);
2860:     }
2861:     if (bcComps && bcComps[bc]) {ISRestoreIndices(bcComps[bc], &comp);}
2862:     ISRestoreIndices(bcPoints[bc], &idx);
2863:   }
2864:   /* Handle anchors */
2865:   DMPlexGetAnchors(dm, &aSec, NULL);
2866:   if (aSec) {
2867:     PetscInt aStart, aEnd, a;

2869:     for (d = 0; d < maxDof; ++d) indices[d] = d;
2870:     PetscSectionGetChart(aSec, &aStart, &aEnd);
2871:     for (a = aStart; a < aEnd; a++) {
2872:       PetscInt dof, fdof, f;

2874:       PetscSectionGetDof(aSec, a, &dof);
2875:       if (dof) {
2876:         /* if there are point-to-point constraints, then all dofs are constrained */
2877:         for (f = 0; f < numFields; f++) {
2878:           PetscSectionGetFieldDof(section, a, f, &fdof);
2879:           PetscSectionSetFieldConstraintIndices(section, a, f, indices);
2880:         }
2881:       }
2882:     }
2883:   }
2884:   PetscFree(indices);
2885:   return(0);
2886: }

2890: /* Set the constrained indices on each point */
2891: PetscErrorCode DMPlexCreateSectionBCIndices(DM dm, PetscSection section)
2892: {
2893:   PetscInt      *indices;
2894:   PetscInt       numFields, maxDof, pStart, pEnd, p, f, d;

2898:   PetscSectionGetNumFields(section, &numFields);
2899:   PetscSectionGetMaxDof(section, &maxDof);
2900:   PetscSectionGetChart(section, &pStart, &pEnd);
2901:   PetscMalloc1(maxDof, &indices);
2902:   for (d = 0; d < maxDof; ++d) indices[d] = -1;
2903:   for (p = pStart; p < pEnd; ++p) {
2904:     PetscInt cdof, d;

2906:     PetscSectionGetConstraintDof(section, p, &cdof);
2907:     if (cdof) {
2908:       if (numFields) {
2909:         PetscInt numConst = 0, foff = 0;

2911:         for (f = 0; f < numFields; ++f) {
2912:           const PetscInt *find;
2913:           PetscInt        fcdof, fdof;

2915:           PetscSectionGetFieldDof(section, p, f, &fdof);
2916:           PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);
2917:           /* Change constraint numbering from field component to local dof number */
2918:           PetscSectionGetFieldConstraintIndices(section, p, f, &find);
2919:           for (d = 0; d < fcdof; ++d) indices[numConst+d] = find[d] + foff;
2920:           numConst += fcdof;
2921:           foff     += fdof;
2922:         }
2923:         if (cdof != numConst) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_LIB, "Total number of field constraints %D should be %D", numConst, cdof);
2924:       } else {
2925:         for (d = 0; d < cdof; ++d) indices[d] = d;
2926:       }
2927:       PetscSectionSetConstraintIndices(section, p, indices);
2928:     }
2929:   }
2930:   PetscFree(indices);
2931:   return(0);
2932: }

2936: /*@C
2937:   DMPlexCreateSection - Create a PetscSection based upon the dof layout specification provided.

2939:   Not Collective

2941:   Input Parameters:
2942: + dm        - The DMPlex object
2943: . dim       - The spatial dimension of the problem
2944: . numFields - The number of fields in the problem
2945: . numComp   - An array of size numFields that holds the number of components for each field
2946: . numDof    - An array of size numFields*(dim+1) which holds the number of dof for each field on a mesh piece of dimension d
2947: . numBC     - The number of boundary conditions
2948: . bcField   - An array of size numBC giving the field number for each boundry condition
2949: . bcComps   - [Optional] An array of size numBC giving an IS holding the field components to which each boundary condition applies
2950: . bcPoints  - An array of size numBC giving an IS holding the Plex points to which each boundary condition applies
2951: - perm      - Optional permutation of the chart, or NULL

2953:   Output Parameter:
2954: . section - The PetscSection object

2956:   Notes: numDof[f*(dim+1)+d] gives the number of dof for field f on sieve points of dimension d. For instance, numDof[1] is the
2957:   number of dof for field 0 on each edge.

2959:   The chart permutation is the same one set using PetscSectionSetPermutation()

2961:   Level: developer

2963:   Fortran Notes:
2964:   A Fortran 90 version is available as DMPlexCreateSectionF90()

2966: .keywords: mesh, elements
2967: .seealso: DMPlexCreate(), PetscSectionCreate(), PetscSectionSetPermutation()
2968: @*/
2969: PetscErrorCode DMPlexCreateSection(DM dm, PetscInt dim, PetscInt numFields,const PetscInt numComp[],const PetscInt numDof[], PetscInt numBC,const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], IS perm, PetscSection *section)
2970: {
2971:   PetscSection   aSec;

2975:   DMPlexCreateSectionInitial(dm, dim, numFields, numComp, numDof, section);
2976:   DMPlexCreateSectionBCDof(dm, numBC, bcField, bcComps, bcPoints, *section);
2977:   if (perm) {PetscSectionSetPermutation(*section, perm);}
2978:   PetscSectionSetUp(*section);
2979:   DMPlexGetAnchors(dm,&aSec,NULL);
2980:   if (numBC || aSec) {
2981:     DMPlexCreateSectionBCIndicesField(dm, numBC, bcField, bcComps, bcPoints, *section);
2982:     DMPlexCreateSectionBCIndices(dm, *section);
2983:   }
2984:   PetscSectionViewFromOptions(*section,NULL,"-section_view");
2985:   return(0);
2986: }

2990: PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
2991: {
2992:   PetscSection   section, s;
2993:   Mat            m;
2994:   PetscInt       maxHeight;

2998:   DMClone(dm, cdm);
2999:   DMPlexGetMaxProjectionHeight(dm, &maxHeight);
3000:   DMPlexSetMaxProjectionHeight(*cdm, maxHeight);
3001:   PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);
3002:   DMSetDefaultSection(*cdm, section);
3003:   PetscSectionDestroy(&section);
3004:   PetscSectionCreate(PETSC_COMM_SELF, &s);
3005:   MatCreate(PETSC_COMM_SELF, &m);
3006:   DMSetDefaultConstraints(*cdm, s, m);
3007:   PetscSectionDestroy(&s);
3008:   MatDestroy(&m);
3009:   return(0);
3010: }

3014: PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
3015: {
3016:   DM_Plex *mesh = (DM_Plex*) dm->data;

3020:   if (section) *section = mesh->coneSection;
3021:   return(0);
3022: }

3026: PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
3027: {
3028:   DM_Plex *mesh = (DM_Plex*) dm->data;

3032:   if (section) *section = mesh->supportSection;
3033:   return(0);
3034: }

3038: PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
3039: {
3040:   DM_Plex *mesh = (DM_Plex*) dm->data;

3044:   if (cones) *cones = mesh->cones;
3045:   return(0);
3046: }

3050: PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
3051: {
3052:   DM_Plex *mesh = (DM_Plex*) dm->data;

3056:   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
3057:   return(0);
3058: }

3060: /******************************** FEM Support **********************************/

3064: PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3065: {
3066:   PetscScalar    *array, *vArray;
3067:   const PetscInt *cone, *coneO;
3068:   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
3069:   PetscErrorCode  ierr;

3072:   PetscSectionGetChart(section, &pStart, &pEnd);
3073:   DMPlexGetConeSize(dm, point, &numPoints);
3074:   DMPlexGetCone(dm, point, &cone);
3075:   DMPlexGetConeOrientation(dm, point, &coneO);
3076:   if (!values || !*values) {
3077:     if ((point >= pStart) && (point < pEnd)) {
3078:       PetscInt dof;

3080:       PetscSectionGetDof(section, point, &dof);
3081:       size += dof;
3082:     }
3083:     for (p = 0; p < numPoints; ++p) {
3084:       const PetscInt cp = cone[p];
3085:       PetscInt       dof;

3087:       if ((cp < pStart) || (cp >= pEnd)) continue;
3088:       PetscSectionGetDof(section, cp, &dof);
3089:       size += dof;
3090:     }
3091:     if (!values) {
3092:       if (csize) *csize = size;
3093:       return(0);
3094:     }
3095:     DMGetWorkArray(dm, size, PETSC_SCALAR, &array);
3096:   } else {
3097:     array = *values;
3098:   }
3099:   size = 0;
3100:   VecGetArray(v, &vArray);
3101:   if ((point >= pStart) && (point < pEnd)) {
3102:     PetscInt     dof, off, d;
3103:     PetscScalar *varr;

3105:     PetscSectionGetDof(section, point, &dof);
3106:     PetscSectionGetOffset(section, point, &off);
3107:     varr = &vArray[off];
3108:     for (d = 0; d < dof; ++d, ++offset) {
3109:       array[offset] = varr[d];
3110:     }
3111:     size += dof;
3112:   }
3113:   for (p = 0; p < numPoints; ++p) {
3114:     const PetscInt cp = cone[p];
3115:     PetscInt       o  = coneO[p];
3116:     PetscInt       dof, off, d;
3117:     PetscScalar   *varr;

3119:     if ((cp < pStart) || (cp >= pEnd)) continue;
3120:     PetscSectionGetDof(section, cp, &dof);
3121:     PetscSectionGetOffset(section, cp, &off);
3122:     varr = &vArray[off];
3123:     if (o >= 0) {
3124:       for (d = 0; d < dof; ++d, ++offset) {
3125:         array[offset] = varr[d];
3126:       }
3127:     } else {
3128:       for (d = dof-1; d >= 0; --d, ++offset) {
3129:         array[offset] = varr[d];
3130:       }
3131:     }
3132:     size += dof;
3133:   }
3134:   VecRestoreArray(v, &vArray);
3135:   if (!*values) {
3136:     if (csize) *csize = size;
3137:     *values = array;
3138:   } else {
3139:     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %d < actual size %d", *csize, size);
3140:     *csize = size;
3141:   }
3142:   return(0);
3143: }

3147: PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Static(PetscSection section, PetscInt numPoints, const PetscInt points[], const PetscScalar vArray[], PetscInt *size, PetscScalar array[])
3148: {
3149:   PetscInt       offset = 0, p;

3153:   *size = 0;
3154:   for (p = 0; p < numPoints*2; p += 2) {
3155:     const PetscInt point = points[p];
3156:     const PetscInt o     = points[p+1];
3157:     PetscInt       dof, off, d;
3158:     const PetscScalar *varr;

3160:     PetscSectionGetDof(section, point, &dof);
3161:     PetscSectionGetOffset(section, point, &off);
3162:     varr = &vArray[off];
3163:     if (o >= 0) {
3164:       for (d = 0; d < dof; ++d, ++offset)    array[offset] = varr[d];
3165:     } else {
3166:       for (d = dof-1; d >= 0; --d, ++offset) array[offset] = varr[d];
3167:     }
3168:   }
3169:   *size = offset;
3170:   return(0);
3171: }

3175: PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Fields_Static(PetscSection section, PetscInt numPoints, const PetscInt points[], PetscInt numFields, const PetscScalar vArray[], PetscInt *size, PetscScalar array[])
3176: {
3177:   PetscInt       offset = 0, f;

3181:   *size = 0;
3182:   for (f = 0; f < numFields; ++f) {
3183:     PetscInt fcomp, p;

3185:     PetscSectionGetFieldComponents(section, f, &fcomp);
3186:     for (p = 0; p < numPoints*2; p += 2) {
3187:       const PetscInt point = points[p];
3188:       const PetscInt o     = points[p+1];
3189:       PetscInt       fdof, foff, d, c;
3190:       const PetscScalar *varr;

3192:       PetscSectionGetFieldDof(section, point, f, &fdof);
3193:       PetscSectionGetFieldOffset(section, point, f, &foff);
3194:       varr = &vArray[foff];
3195:       if (o >= 0) {
3196:         for (d = 0; d < fdof; ++d, ++offset) array[offset] = varr[d];
3197:       } else {
3198:         for (d = fdof/fcomp-1; d >= 0; --d) {
3199:           for (c = 0; c < fcomp; ++c, ++offset) {
3200:             array[offset] = varr[d*fcomp+c];
3201:           }
3202:         }
3203:       }
3204:     }
3205:   }
3206:   *size = offset;
3207:   return(0);
3208: }

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

3215:   Not collective

3217:   Input Parameters:
3218: + dm - The DM
3219: . section - The section describing the layout in v, or NULL to use the default section
3220: . v - The local vector
3221: - point - The sieve point in the DM

3223:   Output Parameters:
3224: + csize - The number of values in the closure, or NULL
3225: - values - The array of values, which is a borrowed array and should not be freed

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

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

3233:   Level: intermediate

3235: .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
3236: @*/
3237: PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3238: {
3239:   PetscSection    clSection;
3240:   IS              clPoints;
3241:   PetscScalar    *array, *vArray;
3242:   PetscInt       *points = NULL;
3243:   const PetscInt *clp;
3244:   PetscInt        depth, numFields, numPoints, size;
3245:   PetscErrorCode  ierr;

3249:   if (!section) {DMGetDefaultSection(dm, &section);}
3252:   DMPlexGetDepth(dm, &depth);
3253:   PetscSectionGetNumFields(section, &numFields);
3254:   if (depth == 1 && numFields < 2) {
3255:     DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);
3256:     return(0);
3257:   }
3258:   /* Get points */
3259:   PetscSectionGetClosureIndex(section, (PetscObject) dm, &clSection, &clPoints);
3260:   if (!clPoints) {
3261:     PetscInt pStart, pEnd, p, q;

3263:     PetscSectionGetChart(section, &pStart, &pEnd);
3264:     DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);
3265:     /* Compress out points not in the section */
3266:     for (p = 0, q = 0; p < numPoints*2; p += 2) {
3267:       if ((points[p] >= pStart) && (points[p] < pEnd)) {
3268:         points[q*2]   = points[p];
3269:         points[q*2+1] = points[p+1];
3270:         ++q;
3271:       }
3272:     }
3273:     numPoints = q;
3274:   } else {
3275:     PetscInt dof, off;

3277:     PetscSectionGetDof(clSection, point, &dof);
3278:     PetscSectionGetOffset(clSection, point, &off);
3279:     ISGetIndices(clPoints, &clp);
3280:     numPoints = dof/2;
3281:     points    = (PetscInt *) &clp[off];
3282:   }
3283:   /* Get array */
3284:   if (!values || !*values) {
3285:     PetscInt asize = 0, dof, p;

3287:     for (p = 0; p < numPoints*2; p += 2) {
3288:       PetscSectionGetDof(section, points[p], &dof);
3289:       asize += dof;
3290:     }
3291:     if (!values) {
3292:       if (!clPoints) {DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);}
3293:       else           {ISRestoreIndices(clPoints, &clp);}
3294:       if (csize) *csize = asize;
3295:       return(0);
3296:     }
3297:     DMGetWorkArray(dm, asize, PETSC_SCALAR, &array);
3298:   } else {
3299:     array = *values;
3300:   }
3301:   VecGetArray(v, &vArray);
3302:   /* Get values */
3303:   if (numFields > 0) {DMPlexVecGetClosure_Fields_Static(section, numPoints, points, numFields, vArray, &size, array);}
3304:   else               {DMPlexVecGetClosure_Static(section, numPoints, points, vArray, &size, array);}
3305:   /* Cleanup points */
3306:   if (!clPoints) {DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);}
3307:   else           {ISRestoreIndices(clPoints, &clp);}
3308:   /* Cleanup array */
3309:   VecRestoreArray(v, &vArray);
3310:   if (!*values) {
3311:     if (csize) *csize = size;
3312:     *values = array;
3313:   } else {
3314:     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
3315:     *csize = size;
3316:   }
3317:   return(0);
3318: }

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

3325:   Not collective

3327:   Input Parameters:
3328: + dm - The DM
3329: . section - The section describing the layout in v, or NULL to use the default section
3330: . v - The local vector
3331: . point - The sieve point in the DM
3332: . csize - The number of values in the closure, or NULL
3333: - values - The array of values, which is a borrowed array and should not be freed

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

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

3341:   Level: intermediate

3343: .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
3344: @*/
3345: PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3346: {
3347:   PetscInt       size = 0;

3351:   /* Should work without recalculating size */
3352:   DMRestoreWorkArray(dm, size, PETSC_SCALAR, (void*) values);
3353:   return(0);
3354: }

3356: PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
3357: PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}

3361: PETSC_STATIC_INLINE PetscErrorCode updatePoint_private(PetscSection section, PetscInt point, PetscInt dof, void (*fuse)(PetscScalar*, PetscScalar), PetscBool setBC, PetscInt orientation, const PetscScalar values[], PetscScalar array[])
3362: {
3363:   PetscInt        cdof;   /* The number of constraints on this point */
3364:   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
3365:   PetscScalar    *a;
3366:   PetscInt        off, cind = 0, k;
3367:   PetscErrorCode  ierr;

3370:   PetscSectionGetConstraintDof(section, point, &cdof);
3371:   PetscSectionGetOffset(section, point, &off);
3372:   a    = &array[off];
3373:   if (!cdof || setBC) {
3374:     if (orientation >= 0) {
3375:       for (k = 0; k < dof; ++k) {
3376:         fuse(&a[k], values[k]);
3377:       }
3378:     } else {
3379:       for (k = 0; k < dof; ++k) {
3380:         fuse(&a[k], values[dof-k-1]);
3381:       }
3382:     }
3383:   } else {
3384:     PetscSectionGetConstraintIndices(section, point, &cdofs);
3385:     if (orientation >= 0) {
3386:       for (k = 0; k < dof; ++k) {
3387:         if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
3388:         fuse(&a[k], values[k]);
3389:       }
3390:     } else {
3391:       for (k = 0; k < dof; ++k) {
3392:         if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
3393:         fuse(&a[k], values[dof-k-1]);
3394:       }
3395:     }
3396:   }
3397:   return(0);
3398: }

3402: PETSC_STATIC_INLINE PetscErrorCode updatePointBC_private(PetscSection section, PetscInt point, PetscInt dof, void (*fuse)(PetscScalar*, PetscScalar), PetscInt orientation, const PetscScalar values[], PetscScalar array[])
3403: {
3404:   PetscInt        cdof;   /* The number of constraints on this point */
3405:   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
3406:   PetscScalar    *a;
3407:   PetscInt        off, cind = 0, k;
3408:   PetscErrorCode  ierr;

3411:   PetscSectionGetConstraintDof(section, point, &cdof);
3412:   PetscSectionGetOffset(section, point, &off);
3413:   a    = &array[off];
3414:   if (cdof) {
3415:     PetscSectionGetConstraintIndices(section, point, &cdofs);
3416:     if (orientation >= 0) {
3417:       for (k = 0; k < dof; ++k) {
3418:         if ((cind < cdof) && (k == cdofs[cind])) {
3419:           fuse(&a[k], values[k]);
3420:           ++cind;
3421:         }
3422:       }
3423:     } else {
3424:       for (k = 0; k < dof; ++k) {
3425:         if ((cind < cdof) && (k == cdofs[cind])) {
3426:           fuse(&a[k], values[dof-k-1]);
3427:           ++cind;
3428:         }
3429:       }
3430:     }
3431:   }
3432:   return(0);
3433: }

3437: PETSC_STATIC_INLINE PetscErrorCode updatePointFields_private(PetscSection section, PetscInt point, PetscInt o, PetscInt f, PetscInt fcomp, void (*fuse)(PetscScalar*, PetscScalar), PetscBool setBC, const PetscScalar values[], PetscInt *offset, PetscScalar array[])
3438: {
3439:   PetscScalar    *a;
3440:   PetscInt        fdof, foff, fcdof, foffset = *offset;
3441:   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
3442:   PetscInt        cind = 0, k, c;
3443:   PetscErrorCode  ierr;

3446:   PetscSectionGetFieldDof(section, point, f, &fdof);
3447:   PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);
3448:   PetscSectionGetFieldOffset(section, point, f, &foff);
3449:   a    = &array[foff];
3450:   if (!fcdof || setBC) {
3451:     if (o >= 0) {
3452:       for (k = 0; k < fdof; ++k) fuse(&a[k], values[foffset+k]);
3453:     } else {
3454:       for (k = fdof/fcomp-1; k >= 0; --k) {
3455:         for (c = 0; c < fcomp; ++c) {
3456:           fuse(&a[(fdof/fcomp-1-k)*fcomp+c], values[foffset+k*fcomp+c]);
3457:         }
3458:       }
3459:     }
3460:   } else {
3461:     PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);
3462:     if (o >= 0) {
3463:       for (k = 0; k < fdof; ++k) {
3464:         if ((cind < fcdof) && (k == fcdofs[cind])) {++cind; continue;}
3465:         fuse(&a[k], values[foffset+k]);
3466:       }
3467:     } else {
3468:       for (k = fdof/fcomp-1; k >= 0; --k) {
3469:         for (c = 0; c < fcomp; ++c) {
3470:           if ((cind < fcdof) && (k*fcomp+c == fcdofs[cind])) {++cind; continue;}
3471:           fuse(&a[(fdof/fcomp-1-k)*fcomp+c], values[foffset+k*fcomp+c]);
3472:         }
3473:       }
3474:     }
3475:   }
3476:   *offset += fdof;
3477:   return(0);
3478: }

3482: PETSC_STATIC_INLINE PetscErrorCode updatePointFieldsBC_private(PetscSection section, PetscInt point, PetscInt o, PetscInt f, PetscInt fcomp, void (*fuse)(PetscScalar*, PetscScalar), const PetscScalar values[], PetscInt *offset, PetscScalar array[])
3483: {
3484:   PetscScalar    *a;
3485:   PetscInt        fdof, foff, fcdof, foffset = *offset;
3486:   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
3487:   PetscInt        cind = 0, k, c;
3488:   PetscErrorCode  ierr;

3491:   PetscSectionGetFieldDof(section, point, f, &fdof);
3492:   PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);
3493:   PetscSectionGetFieldOffset(section, point, f, &foff);
3494:   a    = &array[foff];
3495:   if (fcdof) {
3496:     PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);
3497:     if (o >= 0) {
3498:       for (k = 0; k < fdof; ++k) {
3499:         if ((cind < fcdof) && (k == fcdofs[cind])) {
3500:           fuse(&a[k], values[foffset+k]);
3501:           ++cind;
3502:         }
3503:       }
3504:     } else {
3505:       for (k = fdof/fcomp-1; k >= 0; --k) {
3506:         for (c = 0; c < fcomp; ++c) {
3507:           if ((cind < fcdof) && (k*fcomp+c == fcdofs[cind])) {
3508:             fuse(&a[(fdof/fcomp-1-k)*fcomp+c], values[foffset+k*fcomp+c]);
3509:             ++cind;
3510:           }
3511:         }
3512:       }
3513:     }
3514:   }
3515:   *offset += fdof;
3516:   return(0);
3517: }

3521: PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
3522: {
3523:   PetscScalar    *array;
3524:   const PetscInt *cone, *coneO;
3525:   PetscInt        pStart, pEnd, p, numPoints, off, dof;
3526:   PetscErrorCode  ierr;

3529:   PetscSectionGetChart(section, &pStart, &pEnd);
3530:   DMPlexGetConeSize(dm, point, &numPoints);
3531:   DMPlexGetCone(dm, point, &cone);
3532:   DMPlexGetConeOrientation(dm, point, &coneO);
3533:   VecGetArray(v, &array);
3534:   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
3535:     const PetscInt cp = !p ? point : cone[p-1];
3536:     const PetscInt o  = !p ? 0     : coneO[p-1];

3538:     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
3539:     PetscSectionGetDof(section, cp, &dof);
3540:     /* ADD_VALUES */
3541:     {
3542:       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
3543:       PetscScalar    *a;
3544:       PetscInt        cdof, coff, cind = 0, k;

3546:       PetscSectionGetConstraintDof(section, cp, &cdof);
3547:       PetscSectionGetOffset(section, cp, &coff);
3548:       a    = &array[coff];
3549:       if (!cdof) {
3550:         if (o >= 0) {
3551:           for (k = 0; k < dof; ++k) {
3552:             a[k] += values[off+k];
3553:           }
3554:         } else {
3555:           for (k = 0; k < dof; ++k) {
3556:             a[k] += values[off+dof-k-1];
3557:           }
3558:         }
3559:       } else {
3560:         PetscSectionGetConstraintIndices(section, cp, &cdofs);
3561:         if (o >= 0) {
3562:           for (k = 0; k < dof; ++k) {
3563:             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
3564:             a[k] += values[off+k];
3565:           }
3566:         } else {
3567:           for (k = 0; k < dof; ++k) {
3568:             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
3569:             a[k] += values[off+dof-k-1];
3570:           }
3571:         }
3572:       }
3573:     }
3574:   }
3575:   VecRestoreArray(v, &array);
3576:   return(0);
3577: }

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

3584:   Not collective

3586:   Input Parameters:
3587: + dm - The DM
3588: . section - The section describing the layout in v, or NULL to use the default section
3589: . v - The local vector
3590: . point - The sieve point in the DM
3591: . values - The array of values
3592: - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions

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

3597:   Level: intermediate

3599: .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
3600: @*/
3601: PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
3602: {
3603:   PetscSection    clSection;
3604:   IS              clPoints;
3605:   PetscScalar    *array;
3606:   PetscInt       *points = NULL;
3607:   const PetscInt *clp;
3608:   PetscInt        depth, numFields, numPoints, p;
3609:   PetscErrorCode  ierr;

3613:   if (!section) {DMGetDefaultSection(dm, &section);}
3616:   DMPlexGetDepth(dm, &depth);
3617:   PetscSectionGetNumFields(section, &numFields);
3618:   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
3619:     DMPlexVecSetClosure_Static(dm, section, v, point, values, mode);
3620:     return(0);
3621:   }
3622:   /* Get points */
3623:   PetscSectionGetClosureIndex(section, (PetscObject) dm, &clSection, &clPoints);
3624:   if (!clPoints) {
3625:     PetscInt pStart, pEnd, q;

3627:     PetscSectionGetChart(section, &pStart, &pEnd);
3628:     DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);
3629:     /* Compress out points not in the section */
3630:     for (p = 0, q = 0; p < numPoints*2; p += 2) {
3631:       if ((points[p] >= pStart) && (points[p] < pEnd)) {
3632:         points[q*2]   = points[p];
3633:         points[q*2+1] = points[p+1];
3634:         ++q;
3635:       }
3636:     }
3637:     numPoints = q;
3638:   } else {
3639:     PetscInt dof, off;

3641:     PetscSectionGetDof(clSection, point, &dof);
3642:     PetscSectionGetOffset(clSection, point, &off);
3643:     ISGetIndices(clPoints, &clp);
3644:     numPoints = dof/2;
3645:     points    = (PetscInt *) &clp[off];
3646:   }
3647:   /* Get array */
3648:   VecGetArray(v, &array);
3649:   /* Get values */
3650:   if (numFields > 0) {
3651:     PetscInt offset = 0, fcomp, f;
3652:     for (f = 0; f < numFields; ++f) {
3653:       PetscSectionGetFieldComponents(section, f, &fcomp);
3654:       switch (mode) {
3655:       case INSERT_VALUES:
3656:         for (p = 0; p < numPoints*2; p += 2) {
3657:           const PetscInt point = points[p];
3658:           const PetscInt o     = points[p+1];
3659:           updatePointFields_private(section, point, o, f, fcomp, insert, PETSC_FALSE, values, &offset, array);
3660:         } break;
3661:       case INSERT_ALL_VALUES:
3662:         for (p = 0; p < numPoints*2; p += 2) {
3663:           const PetscInt point = points[p];
3664:           const PetscInt o     = points[p+1];
3665:           updatePointFields_private(section, point, o, f, fcomp, insert, PETSC_TRUE, values, &offset, array);
3666:         } break;
3667:       case INSERT_BC_VALUES:
3668:         for (p = 0; p < numPoints*2; p += 2) {
3669:           const PetscInt point = points[p];
3670:           const PetscInt o     = points[p+1];
3671:           updatePointFieldsBC_private(section, point, o, f, fcomp, insert, values, &offset, array);
3672:         } break;
3673:       case ADD_VALUES:
3674:         for (p = 0; p < numPoints*2; p += 2) {
3675:           const PetscInt point = points[p];
3676:           const PetscInt o     = points[p+1];
3677:           updatePointFields_private(section, point, o, f, fcomp, add, PETSC_FALSE, values, &offset, array);
3678:         } break;
3679:       case ADD_ALL_VALUES:
3680:         for (p = 0; p < numPoints*2; p += 2) {
3681:           const PetscInt point = points[p];
3682:           const PetscInt o     = points[p+1];
3683:           updatePointFields_private(section, point, o, f, fcomp, add, PETSC_TRUE, values, &offset, array);
3684:         } break;
3685:       case ADD_BC_VALUES:
3686:         for (p = 0; p < numPoints*2; p += 2) {
3687:           const PetscInt point = points[p];
3688:           const PetscInt o     = points[p+1];
3689:           updatePointFieldsBC_private(section, point, o, f, fcomp, add, values, &offset, array);
3690:         } break;
3691:       default:
3692:         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
3693:       }
3694:     }
3695:   } else {
3696:     PetscInt dof, off;

3698:     switch (mode) {
3699:     case INSERT_VALUES:
3700:       for (p = 0, off = 0; p < numPoints*2; p += 2, off += dof) {
3701:         PetscInt o = points[p+1];
3702:         PetscSectionGetDof(section, points[p], &dof);
3703:         updatePoint_private(section, points[p], dof, insert, PETSC_FALSE, o, &values[off], array);
3704:       } break;
3705:     case INSERT_ALL_VALUES:
3706:       for (p = 0, off = 0; p < numPoints*2; p += 2, off += dof) {
3707:         PetscInt o = points[p+1];
3708:         PetscSectionGetDof(section, points[p], &dof);
3709:         updatePoint_private(section, points[p], dof, insert, PETSC_TRUE,  o, &values[off], array);
3710:       } break;
3711:     case INSERT_BC_VALUES:
3712:       for (p = 0, off = 0; p < numPoints*2; p += 2, off += dof) {
3713:         PetscInt o = points[p+1];
3714:         PetscSectionGetDof(section, points[p], &dof);
3715:         updatePointBC_private(section, points[p], dof, insert,  o, &values[off], array);
3716:       } break;
3717:     case ADD_VALUES:
3718:       for (p = 0, off = 0; p < numPoints*2; p += 2, off += dof) {
3719:         PetscInt o = points[p+1];
3720:         PetscSectionGetDof(section, points[p], &dof);
3721:         updatePoint_private(section, points[p], dof, add,    PETSC_FALSE, o, &values[off], array);
3722:       } break;
3723:     case ADD_ALL_VALUES:
3724:       for (p = 0, off = 0; p < numPoints*2; p += 2, off += dof) {
3725:         PetscInt o = points[p+1];
3726:         PetscSectionGetDof(section, points[p], &dof);
3727:         updatePoint_private(section, points[p], dof, add,    PETSC_TRUE,  o, &values[off], array);
3728:       } break;
3729:     case ADD_BC_VALUES:
3730:       for (p = 0, off = 0; p < numPoints*2; p += 2, off += dof) {
3731:         PetscInt o = points[p+1];
3732:         PetscSectionGetDof(section, points[p], &dof);
3733:         updatePointBC_private(section, points[p], dof, add,  o, &values[off], array);
3734:       } break;
3735:     default:
3736:       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
3737:     }
3738:   }
3739:   /* Cleanup points */
3740:   if (!clPoints) {DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);}
3741:   else           {ISRestoreIndices(clPoints, &clp);}
3742:   /* Cleanup array */
3743:   VecRestoreArray(v, &array);
3744:   return(0);
3745: }

3749: PetscErrorCode DMPlexVecSetFieldClosure_Internal(DM dm, PetscSection section, Vec v, PetscBool fieldActive[], PetscInt point, const PetscScalar values[], InsertMode mode)
3750: {
3751:   PetscSection    clSection;
3752:   IS              clPoints;
3753:   PetscScalar    *array;
3754:   PetscInt       *points = NULL;
3755:   const PetscInt *clp;
3756:   PetscInt        numFields, numPoints, p;
3757:   PetscInt        offset = 0, fcomp, f;
3758:   PetscErrorCode  ierr;

3762:   if (!section) {DMGetDefaultSection(dm, &section);}
3765:   PetscSectionGetNumFields(section, &numFields);
3766:   /* Get points */
3767:   PetscSectionGetClosureIndex(section, (PetscObject) dm, &clSection, &clPoints);
3768:   if (!clPoints) {
3769:     PetscInt pStart, pEnd, q;

3771:     PetscSectionGetChart(section, &pStart, &pEnd);
3772:     DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);
3773:     /* Compress out points not in the section */
3774:     for (p = 0, q = 0; p < numPoints*2; p += 2) {
3775:       if ((points[p] >= pStart) && (points[p] < pEnd)) {
3776:         points[q*2]   = points[p];
3777:         points[q*2+1] = points[p+1];
3778:         ++q;
3779:       }
3780:     }
3781:     numPoints = q;
3782:   } else {
3783:     PetscInt dof, off;

3785:     PetscSectionGetDof(clSection, point, &dof);
3786:     PetscSectionGetOffset(clSection, point, &off);
3787:     ISGetIndices(clPoints, &clp);
3788:     numPoints = dof/2;
3789:     points    = (PetscInt *) &clp[off];
3790:   }
3791:   /* Get array */
3792:   VecGetArray(v, &array);
3793:   /* Get values */
3794:   for (f = 0; f < numFields; ++f) {
3795:     PetscSectionGetFieldComponents(section, f, &fcomp);
3796:     if (!fieldActive[f]) {
3797:       for (p = 0; p < numPoints*2; p += 2) {
3798:         PetscInt fdof;
3799:         PetscSectionGetFieldDof(section, points[p], f, &fdof);
3800:         offset += fdof;
3801:       }
3802:       continue;
3803:     }
3804:     switch (mode) {
3805:     case INSERT_VALUES:
3806:       for (p = 0; p < numPoints*2; p += 2) {
3807:         const PetscInt point = points[p];
3808:         const PetscInt o     = points[p+1];
3809:         updatePointFields_private(section, point, o, f, fcomp, insert, PETSC_FALSE, values, &offset, array);
3810:       } break;
3811:     case INSERT_ALL_VALUES:
3812:       for (p = 0; p < numPoints*2; p += 2) {
3813:         const PetscInt point = points[p];
3814:         const PetscInt o     = points[p+1];
3815:         updatePointFields_private(section, point, o, f, fcomp, insert, PETSC_TRUE, values, &offset, array);
3816:         } break;
3817:     case INSERT_BC_VALUES:
3818:       for (p = 0; p < numPoints*2; p += 2) {
3819:         const PetscInt point = points[p];
3820:         const PetscInt o     = points[p+1];
3821:         updatePointFieldsBC_private(section, point, o, f, fcomp, insert, values, &offset, array);
3822:       } break;
3823:     case ADD_VALUES:
3824:       for (p = 0; p < numPoints*2; p += 2) {
3825:         const PetscInt point = points[p];
3826:         const PetscInt o     = points[p+1];
3827:         updatePointFields_private(section, point, o, f, fcomp, add, PETSC_FALSE, values, &offset, array);
3828:       } break;
3829:     case ADD_ALL_VALUES:
3830:       for (p = 0; p < numPoints*2; p += 2) {
3831:         const PetscInt point = points[p];
3832:         const PetscInt o     = points[p+1];
3833:         updatePointFields_private(section, point, o, f, fcomp, add, PETSC_TRUE, values, &offset, array);
3834:       } break;
3835:     default:
3836:       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
3837:     }
3838:   }
3839:   /* Cleanup points */
3840:   if (!clPoints) {DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);}
3841:   else           {ISRestoreIndices(clPoints, &clp);}
3842:   /* Cleanup array */
3843:   VecRestoreArray(v, &array);
3844:   return(0);
3845: }

3849: PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
3850: {
3851:   PetscMPIInt    rank;
3852:   PetscInt       i, j;

3856:   MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);
3857:   PetscViewerASCIIPrintf(viewer, "[%d]mat for sieve point %D\n", rank, point);
3858:   for (i = 0; i < numRIndices; i++) {PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);}
3859:   for (i = 0; i < numCIndices; i++) {PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);}
3860:   numCIndices = numCIndices ? numCIndices : numRIndices;
3861:   for (i = 0; i < numRIndices; i++) {
3862:     PetscViewerASCIIPrintf(viewer, "[%d]", rank);
3863:     for (j = 0; j < numCIndices; j++) {
3864: #if defined(PETSC_USE_COMPLEX)
3865:       PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));
3866: #else
3867:       PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);
3868: #endif
3869:     }
3870:     PetscViewerASCIIPrintf(viewer, "\n");
3871:   }
3872:   return(0);
3873: }

3877: /* . off - The global offset of this point */
3878: PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, PetscInt orientation, PetscInt indices[])
3879: {
3880:   PetscInt        dof;    /* The number of unknowns on this point */
3881:   PetscInt        cdof;   /* The number of constraints on this point */
3882:   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
3883:   PetscInt        cind = 0, k;
3884:   PetscErrorCode  ierr;

3887:   PetscSectionGetDof(section, point, &dof);
3888:   PetscSectionGetConstraintDof(section, point, &cdof);
3889:   if (!cdof || setBC) {
3890:     if (orientation >= 0) {
3891:       for (k = 0; k < dof; ++k) indices[*loff+k] = off+k;
3892:     } else {
3893:       for (k = 0; k < dof; ++k) indices[*loff+dof-k-1] = off+k;
3894:     }
3895:   } else {
3896:     PetscSectionGetConstraintIndices(section, point, &cdofs);
3897:     if (orientation >= 0) {
3898:       for (k = 0; k < dof; ++k) {
3899:         if ((cind < cdof) && (k == cdofs[cind])) {
3900:           /* Insert check for returning constrained indices */
3901:           indices[*loff+k] = -(off+k+1);
3902:           ++cind;
3903:         } else {
3904:           indices[*loff+k] = off+k-cind;
3905:         }
3906:       }
3907:     } else {
3908:       for (k = 0; k < dof; ++k) {
3909:         if ((cind < cdof) && (k == cdofs[cind])) {
3910:           /* Insert check for returning constrained indices */
3911:           indices[*loff+dof-k-1] = -(off+k+1);
3912:           ++cind;
3913:         } else {
3914:           indices[*loff+dof-k-1] = off+k-cind;
3915:         }
3916:       }
3917:     }
3918:   }
3919:   *loff += dof;
3920:   return(0);
3921: }

3925: /* . off - The global offset of this point */
3926: PetscErrorCode DMPlexGetIndicesPointFields_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, PetscInt orientation, PetscInt indices[])
3927: {
3928:   PetscInt       numFields, foff, f;

3932:   PetscSectionGetNumFields(section, &numFields);
3933:   for (f = 0, foff = 0; f < numFields; ++f) {
3934:     PetscInt        fdof, fcomp, cfdof;
3935:     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
3936:     PetscInt        cind = 0, k, c;

3938:     PetscSectionGetFieldComponents(section, f, &fcomp);
3939:     PetscSectionGetFieldDof(section, point, f, &fdof);
3940:     PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);
3941:     if (!cfdof || setBC) {
3942:       if (orientation >= 0) {
3943:         for (k = 0; k < fdof; ++k) indices[foffs[f]+k] = off+foff+k;
3944:       } else {
3945:         for (k = fdof/fcomp-1; k >= 0; --k) {
3946:           for (c = 0; c < fcomp; ++c) {
3947:             indices[foffs[f]+k*fcomp+c] = off+foff+(fdof/fcomp-1-k)*fcomp+c;
3948:           }
3949:         }
3950:       }
3951:     } else {
3952:       PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);
3953:       if (orientation >= 0) {
3954:         for (k = 0; k < fdof; ++k) {
3955:           if ((cind < cfdof) && (k == fcdofs[cind])) {
3956:             indices[foffs[f]+k] = -(off+foff+k+1);
3957:             ++cind;
3958:           } else {
3959:             indices[foffs[f]+k] = off+foff+k-cind;
3960:           }
3961:         }
3962:       } else {
3963:         for (k = fdof/fcomp-1; k >= 0; --k) {
3964:           for (c = 0; c < fcomp; ++c) {
3965:             if ((cind < cfdof) && ((fdof/fcomp-1-k)*fcomp+c == fcdofs[cind])) {
3966:               indices[foffs[f]+k*fcomp+c] = -(off+foff+(fdof/fcomp-1-k)*fcomp+c+1);
3967:               ++cind;
3968:             } else {
3969:               indices[foffs[f]+k*fcomp+c] = off+foff+(fdof/fcomp-1-k)*fcomp+c-cind;
3970:             }
3971:           }
3972:         }
3973:       }
3974:     }
3975:     foff     += (setBC ? fdof : (fdof - cfdof));
3976:     foffs[f] += fdof;
3977:   }
3978:   return(0);
3979: }

3983: PetscErrorCode DMPlexAnchorsModifyMat(DM dm, PetscSection section, PetscInt numPoints, PetscInt numIndices, const PetscInt points[], const PetscScalar values[], PetscInt *outNumPoints, PetscInt *outNumIndices, PetscInt *outPoints[], PetscScalar *outValues[], PetscInt offsets[], PetscBool multiplyLeft)
3984: {
3985:   Mat             cMat;
3986:   PetscSection    aSec, cSec;
3987:   IS              aIS;
3988:   PetscInt        aStart = -1, aEnd = -1;
3989:   const PetscInt  *anchors;
3990:   PetscInt        numFields, f, p, q, newP = 0;
3991:   PetscInt        newNumPoints = 0, newNumIndices = 0;
3992:   PetscInt        *newPoints, *indices, *newIndices;
3993:   PetscInt        maxAnchor, maxDof;
3994:   PetscInt        newOffsets[32];
3995:   PetscInt        *pointMatOffsets[32];
3996:   PetscInt        *newPointOffsets[32];
3997:   PetscScalar     *pointMat[32];
3998:   PetscScalar     *newValues=NULL,*tmpValues;
3999:   PetscBool       anyConstrained = PETSC_FALSE;
4000:   PetscErrorCode  ierr;

4005:   PetscSectionGetNumFields(section, &numFields);

4007:   DMPlexGetAnchors(dm,&aSec,&aIS);
4008:   /* if there are point-to-point constraints */
4009:   if (aSec) {
4010:     PetscMemzero(newOffsets, 32 * sizeof(PetscInt));
4011:     ISGetIndices(aIS,&anchors);
4012:     PetscSectionGetChart(aSec,&aStart,&aEnd);
4013:     /* figure out how many points are going to be in the new element matrix
4014:      * (we allow double counting, because it's all just going to be summed
4015:      * into the global matrix anyway) */
4016:     for (p = 0; p < 2*numPoints; p+=2) {
4017:       PetscInt b    = points[p];
4018:       PetscInt bDof = 0, bSecDof;

4020:       PetscSectionGetDof(section,b,&bSecDof);
4021:       if (!bSecDof) {
4022:         continue;
4023:       }
4024:       if (b >= aStart && b < aEnd) {
4025:         PetscSectionGetDof(aSec,b,&bDof);
4026:       }
4027:       if (bDof) {
4028:         /* this point is constrained */
4029:         /* it is going to be replaced by its anchors */
4030:         PetscInt bOff, q;

4032:         anyConstrained = PETSC_TRUE;
4033:         newNumPoints  += bDof;
4034:         PetscSectionGetOffset(aSec,b,&bOff);
4035:         for (q = 0; q < bDof; q++) {
4036:           PetscInt a = anchors[bOff + q];
4037:           PetscInt aDof;

4039:           PetscSectionGetDof(section,a,&aDof);
4040:           newNumIndices += aDof;
4041:           for (f = 0; f < numFields; ++f) {
4042:             PetscInt fDof;

4044:             PetscSectionGetFieldDof(section, a, f, &fDof);
4045:             newOffsets[f+1] += fDof;
4046:           }
4047:         }
4048:       }
4049:       else {
4050:         /* this point is not constrained */
4051:         newNumPoints++;
4052:         newNumIndices += bSecDof;
4053:         for (f = 0; f < numFields; ++f) {
4054:           PetscInt fDof;

4056:           PetscSectionGetFieldDof(section, b, f, &fDof);
4057:           newOffsets[f+1] += fDof;
4058:         }
4059:       }
4060:     }
4061:   }
4062:   if (!anyConstrained) {
4063:     if (outNumPoints)  *outNumPoints  = 0;
4064:     if (outNumIndices) *outNumIndices = 0;
4065:     if (outPoints)     *outPoints     = NULL;
4066:     if (outValues)     *outValues     = NULL;
4067:     if (aSec) {ISRestoreIndices(aIS,&anchors);}
4068:     return(0);
4069:   }

4071:   if (outNumPoints)  *outNumPoints  = newNumPoints;
4072:   if (outNumIndices) *outNumIndices = newNumIndices;

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

4076:   if (!outPoints && !outValues) {
4077:     if (offsets) {
4078:       for (f = 0; f <= numFields; f++) {
4079:         offsets[f] = newOffsets[f];
4080:       }
4081:     }
4082:     if (aSec) {ISRestoreIndices(aIS,&anchors);}
4083:     return(0);
4084:   }

4086:   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);

4088:   DMGetDefaultConstraints(dm, &cSec, &cMat);

4090:   /* workspaces */
4091:   if (numFields) {
4092:     for (f = 0; f < numFields; f++) {
4093:       DMGetWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[f]);
4094:       DMGetWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[f]);
4095:     }
4096:   }
4097:   else {
4098:     DMGetWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[0]);
4099:     DMGetWorkArray(dm,numPoints,PETSC_INT,&newPointOffsets[0]);
4100:   }

4102:   /* get workspaces for the point-to-point matrices */
4103:   if (numFields) {
4104:     PetscInt totalOffset, totalMatOffset;

4106:     for (p = 0; p < numPoints; p++) {
4107:       PetscInt b    = points[2*p];
4108:       PetscInt bDof = 0, bSecDof;

4110:       PetscSectionGetDof(section,b,&bSecDof);
4111:       if (!bSecDof) {
4112:         for (f = 0; f < numFields; f++) {
4113:           newPointOffsets[f][p + 1] = 0;
4114:           pointMatOffsets[f][p + 1] = 0;
4115:         }
4116:         continue;
4117:       }
4118:       if (b >= aStart && b < aEnd) {
4119:         PetscSectionGetDof(aSec, b, &bDof);
4120:       }
4121:       if (bDof) {
4122:         for (f = 0; f < numFields; f++) {
4123:           PetscInt fDof, q, bOff, allFDof = 0;

4125:           PetscSectionGetFieldDof(section, b, f, &fDof);
4126:           PetscSectionGetOffset(aSec, b, &bOff);
4127:           for (q = 0; q < bDof; q++) {
4128:             PetscInt a = anchors[bOff + q];
4129:             PetscInt aFDof;

4131:             PetscSectionGetFieldDof(section, a, f, &aFDof);
4132:             allFDof += aFDof;
4133:           }
4134:           newPointOffsets[f][p+1] = allFDof;
4135:           pointMatOffsets[f][p+1] = fDof * allFDof;
4136:         }
4137:       }
4138:       else {
4139:         for (f = 0; f < numFields; f++) {
4140:           PetscInt fDof;

4142:           PetscSectionGetFieldDof(section, b, f, &fDof);
4143:           newPointOffsets[f][p+1] = fDof;
4144:           pointMatOffsets[f][p+1] = 0;
4145:         }
4146:       }
4147:     }
4148:     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
4149:       newPointOffsets[f][0] = totalOffset;
4150:       pointMatOffsets[f][0] = totalMatOffset;
4151:       for (p = 0; p < numPoints; p++) {
4152:         newPointOffsets[f][p+1] += newPointOffsets[f][p];
4153:         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
4154:       }
4155:       totalOffset    = newPointOffsets[f][numPoints];
4156:       totalMatOffset = pointMatOffsets[f][numPoints];
4157:       DMGetWorkArray(dm,pointMatOffsets[f][numPoints],PETSC_SCALAR,&pointMat[f]);
4158:     }
4159:   }
4160:   else {
4161:     for (p = 0; p < numPoints; p++) {
4162:       PetscInt b    = points[2*p];
4163:       PetscInt bDof = 0, bSecDof;

4165:       PetscSectionGetDof(section,b,&bSecDof);
4166:       if (!bSecDof) {
4167:         newPointOffsets[0][p + 1] = 0;
4168:         pointMatOffsets[0][p + 1] = 0;
4169:         continue;
4170:       }
4171:       if (b >= aStart && b < aEnd) {
4172:         PetscSectionGetDof(aSec, b, &bDof);
4173:       }
4174:       if (bDof) {
4175:         PetscInt bOff, q, allDof = 0;

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

4181:           PetscSectionGetDof(section, a, &aDof);
4182:           allDof += aDof;
4183:         }
4184:         newPointOffsets[0][p+1] = allDof;
4185:         pointMatOffsets[0][p+1] = bSecDof * allDof;
4186:       }
4187:       else {
4188:         newPointOffsets[0][p+1] = bSecDof;
4189:         pointMatOffsets[0][p+1] = 0;
4190:       }
4191:     }
4192:     newPointOffsets[0][0] = 0;
4193:     pointMatOffsets[0][0] = 0;
4194:     for (p = 0; p < numPoints; p++) {
4195:       newPointOffsets[0][p+1] += newPointOffsets[0][p];
4196:       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
4197:     }
4198:     DMGetWorkArray(dm,pointMatOffsets[0][numPoints],PETSC_SCALAR,&pointMat[0]);
4199:   }

4201:   /* output arrays */
4202:   DMGetWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);

4204:   /* get the point-to-point matrices; construct newPoints */
4205:   PetscSectionGetMaxDof(aSec, &maxAnchor);
4206:   PetscSectionGetMaxDof(section, &maxDof);
4207:   DMGetWorkArray(dm,maxDof,PETSC_INT,&indices);
4208:   DMGetWorkArray(dm,maxAnchor*maxDof,PETSC_INT,&newIndices);
4209:   if (numFields) {
4210:     for (p = 0, newP = 0; p < numPoints; p++) {
4211:       PetscInt b    = points[2*p];
4212:       PetscInt o    = points[2*p+1];
4213:       PetscInt bDof = 0, bSecDof;

4215:       PetscSectionGetDof(section, b, &bSecDof);
4216:       if (!bSecDof) {
4217:         continue;
4218:       }
4219:       if (b >= aStart && b < aEnd) {
4220:         PetscSectionGetDof(aSec, b, &bDof);
4221:       }
4222:       if (bDof) {
4223:         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;

4225:         fStart[0] = 0;
4226:         fEnd[0]   = 0;
4227:         for (f = 0; f < numFields; f++) {
4228:           PetscInt fDof;

4230:           PetscSectionGetFieldDof(cSec, b, f, &fDof);
4231:           fStart[f+1] = fStart[f] + fDof;
4232:           fEnd[f+1]   = fStart[f+1];
4233:         }
4234:         PetscSectionGetOffset(cSec, b, &bOff);
4235:         DMPlexGetIndicesPointFields_Internal(cSec, b, bOff, fEnd, PETSC_TRUE, o, indices);

4237:         fAnchorStart[0] = 0;
4238:         fAnchorEnd[0]   = 0;
4239:         for (f = 0; f < numFields; f++) {
4240:           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];

4242:           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
4243:           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
4244:         }
4245:         PetscSectionGetOffset(aSec, b, &bOff);
4246:         for (q = 0; q < bDof; q++) {
4247:           PetscInt a = anchors[bOff + q], aOff;

4249:           /* we take the orientation of ap into account in the order that we constructed the indices above: the newly added points have no orientation */
4250:           newPoints[2*(newP + q)]     = a;
4251:           newPoints[2*(newP + q) + 1] = 0;
4252:           PetscSectionGetOffset(section, a, &aOff);
4253:           DMPlexGetIndicesPointFields_Internal(section, a, aOff, fAnchorEnd, PETSC_TRUE, 0, newIndices);
4254:         }
4255:         newP += bDof;

4257:         if (outValues) {
4258:           /* get the point-to-point submatrix */
4259:           for (f = 0; f < numFields; f++) {
4260:             MatGetValues(cMat,fEnd[f]-fStart[f],indices + fStart[f],fAnchorEnd[f] - fAnchorStart[f],newIndices + fAnchorStart[f],pointMat[f] + pointMatOffsets[f][p]);
4261:           }
4262:         }
4263:       }
4264:       else {
4265:         newPoints[2 * newP]     = b;
4266:         newPoints[2 * newP + 1] = o;
4267:         newP++;
4268:       }
4269:     }
4270:   } else {
4271:     for (p = 0; p < numPoints; p++) {
4272:       PetscInt b    = points[2*p];
4273:       PetscInt o    = points[2*p+1];
4274:       PetscInt bDof = 0, bSecDof;

4276:       PetscSectionGetDof(section, b, &bSecDof);
4277:       if (!bSecDof) {
4278:         continue;
4279:       }
4280:       if (b >= aStart && b < aEnd) {
4281:         PetscSectionGetDof(aSec, b, &bDof);
4282:       }
4283:       if (bDof) {
4284:         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;

4286:         PetscSectionGetOffset(cSec, b, &bOff);
4287:         DMPlexGetIndicesPoint_Internal(cSec, b, bOff, &bEnd, PETSC_TRUE, o, indices);

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

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

4295:           newPoints[2*(newP + q)]     = a;
4296:           newPoints[2*(newP + q) + 1] = 0;
4297:           PetscSectionGetOffset(section, a, &aOff);
4298:           DMPlexGetIndicesPoint_Internal(section, a, aOff, &bAnchorEnd, PETSC_TRUE, 0, newIndices);
4299:         }
4300:         newP += bDof;

4302:         /* get the point-to-point submatrix */
4303:         if (outValues) {
4304:           MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);
4305:         }
4306:       }
4307:       else {
4308:         newPoints[2 * newP]     = b;
4309:         newPoints[2 * newP + 1] = o;
4310:         newP++;
4311:       }
4312:     }
4313:   }

4315:   if (outValues) {
4316:     DMGetWorkArray(dm,newNumIndices*numIndices,PETSC_SCALAR,&tmpValues);
4317:     PetscMemzero(tmpValues,newNumIndices*numIndices*sizeof(*tmpValues));
4318:     /* multiply constraints on the right */
4319:     if (numFields) {
4320:       for (f = 0; f < numFields; f++) {
4321:         PetscInt oldOff = offsets[f];

4323:         for (p = 0; p < numPoints; p++) {
4324:           PetscInt cStart = newPointOffsets[f][p];
4325:           PetscInt b      = points[2 * p];
4326:           PetscInt c, r, k;
4327:           PetscInt dof;

4329:           PetscSectionGetFieldDof(section,b,f,&dof);
4330:           if (!dof) {
4331:             continue;
4332:           }
4333:           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
4334:             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
4335:             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];

4337:             for (r = 0; r < numIndices; r++) {
4338:               for (c = 0; c < nCols; c++) {
4339:                 for (k = 0; k < dof; k++) {
4340:                   tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
4341:                 }
4342:               }
4343:             }
4344:           }
4345:           else {
4346:             /* copy this column as is */
4347:             for (r = 0; r < numIndices; r++) {
4348:               for (c = 0; c < dof; c++) {
4349:                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
4350:               }
4351:             }
4352:           }
4353:           oldOff += dof;
4354:         }
4355:       }
4356:     }
4357:     else {
4358:       PetscInt oldOff = 0;
4359:       for (p = 0; p < numPoints; p++) {
4360:         PetscInt cStart = newPointOffsets[0][p];
4361:         PetscInt b      = points[2 * p];
4362:         PetscInt c, r, k;
4363:         PetscInt dof;

4365:         PetscSectionGetDof(section,b,&dof);
4366:         if (!dof) {
4367:           continue;
4368:         }
4369:         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
4370:           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
4371:           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];

4373:           for (r = 0; r < numIndices; r++) {
4374:             for (c = 0; c < nCols; c++) {
4375:               for (k = 0; k < dof; k++) {
4376:                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
4377:               }
4378:             }
4379:           }
4380:         }
4381:         else {
4382:           /* copy this column as is */
4383:           for (r = 0; r < numIndices; r++) {
4384:             for (c = 0; c < dof; c++) {
4385:               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
4386:             }
4387:           }
4388:         }
4389:         oldOff += dof;
4390:       }
4391:     }

4393:     if (multiplyLeft) {
4394:       DMGetWorkArray(dm,newNumIndices*newNumIndices,PETSC_SCALAR,&newValues);
4395:       PetscMemzero(newValues,newNumIndices*newNumIndices*sizeof(*newValues));
4396:       /* multiply constraints transpose on the left */
4397:       if (numFields) {
4398:         for (f = 0; f < numFields; f++) {
4399:           PetscInt oldOff = offsets[f];

4401:           for (p = 0; p < numPoints; p++) {
4402:             PetscInt rStart = newPointOffsets[f][p];
4403:             PetscInt b      = points[2 * p];
4404:             PetscInt c, r, k;
4405:             PetscInt dof;

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

4412:               for (r = 0; r < nRows; r++) {
4413:                 for (c = 0; c < newNumIndices; c++) {
4414:                   for (k = 0; k < dof; k++) {
4415:                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
4416:                   }
4417:                 }
4418:               }
4419:             }
4420:             else {
4421:               /* copy this row as is */
4422:               for (r = 0; r < dof; r++) {
4423:                 for (c = 0; c < newNumIndices; c++) {
4424:                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
4425:                 }
4426:               }
4427:             }
4428:             oldOff += dof;
4429:           }
4430:         }
4431:       }
4432:       else {
4433:         PetscInt oldOff = 0;

4435:         for (p = 0; p < numPoints; p++) {
4436:           PetscInt rStart = newPointOffsets[0][p];
4437:           PetscInt b      = points[2 * p];
4438:           PetscInt c, r, k;
4439:           PetscInt dof;

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

4446:             for (r = 0; r < nRows; r++) {
4447:               for (c = 0; c < newNumIndices; c++) {
4448:                 for (k = 0; k < dof; k++) {
4449:                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
4450:                 }
4451:               }
4452:             }
4453:           }
4454:           else {
4455:             /* copy this row as is */
4456:             for (r = 0; r < dof; r++) {
4457:               for (c = 0; c < newNumIndices; c++) {
4458:                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
4459:               }
4460:             }
4461:           }
4462:           oldOff += dof;
4463:         }
4464:       }

4466:       DMRestoreWorkArray(dm,newNumIndices*numIndices,PETSC_SCALAR,&tmpValues);
4467:     }
4468:     else {
4469:       newValues = tmpValues;
4470:     }
4471:   }

4473:   /* clean up */
4474:   DMRestoreWorkArray(dm,maxDof,PETSC_INT,&indices);
4475:   DMRestoreWorkArray(dm,maxAnchor*maxDof,PETSC_INT,&newIndices);

4477:   if (numFields) {
4478:     for (f = 0; f < numFields; f++) {
4479:       DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],PETSC_SCALAR,&pointMat[f]);
4480:       DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[f]);
4481:       DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[f]);
4482:     }
4483:   }
4484:   else {
4485:     DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],PETSC_SCALAR,&pointMat[0]);
4486:     DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[0]);
4487:     DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[0]);
4488:   }
4489:   ISRestoreIndices(aIS,&anchors);

4491:   /* output */
4492:   if (outPoints) {
4493:     *outPoints = newPoints;
4494:   }
4495:   else {
4496:     DMRestoreWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);
4497:   }
4498:   if (outValues) {
4499:     *outValues = newValues;
4500:   }
4501:   else {
4502:     DMRestoreWorkArray(dm,newNumIndices*newNumIndices,PETSC_SCALAR,&newValues);
4503:   }
4504:   for (f = 0; f <= numFields; f++) {
4505:     offsets[f] = newOffsets[f];
4506:   }
4507:   return(0);
4508: }

4512: PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices, PetscInt *outOffsets)
4513: {
4514:   PetscSection    clSection;
4515:   IS              clPoints;
4516:   const PetscInt *clp;
4517:   PetscInt       *points = NULL, *pointsNew;
4518:   PetscInt        numPoints, numPointsNew;
4519:   PetscInt        offsets[32];
4520:   PetscInt        Nf, Nind, NindNew, off, globalOff, f, p;
4521:   PetscErrorCode  ierr;

4529:   PetscSectionGetNumFields(section, &Nf);
4530:   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
4531:   PetscMemzero(offsets, 32 * sizeof(PetscInt));
4532:   /* Get points in closure */
4533:   PetscSectionGetClosureIndex(section, (PetscObject) dm, &clSection, &clPoints);
4534:   if (!clPoints) {
4535:     PetscInt pStart, pEnd, q;

4537:     DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);
4538:     /* Compress out points not in the section */
4539:     PetscSectionGetChart(section, &pStart, &pEnd);
4540:     for (p = 0, q = 0; p < numPoints*2; p += 2) {
4541:       if ((points[p] >= pStart) && (points[p] < pEnd)) {
4542:         points[q*2]   = points[p];
4543:         points[q*2+1] = points[p+1];
4544:         ++q;
4545:       }
4546:     }
4547:     numPoints = q;
4548:   } else {
4549:     PetscInt dof, off;

4551:     PetscSectionGetDof(clSection, point, &dof);
4552:     numPoints = dof/2;
4553:     PetscSectionGetOffset(clSection, point, &off);
4554:     ISGetIndices(clPoints, &clp);
4555:     points = (PetscInt *) &clp[off];
4556:   }
4557:   /* Get number of indices and indices per field */
4558:   for (p = 0, Nind = 0; p < numPoints*2; p += 2) {
4559:     PetscInt dof, fdof;

4561:     PetscSectionGetDof(section, points[p], &dof);
4562:     for (f = 0; f < Nf; ++f) {
4563:       PetscSectionGetFieldDof(section, points[p], f, &fdof);
4564:       offsets[f+1] += fdof;
4565:     }
4566:     Nind += dof;
4567:   }
4568:   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
4569:   if (Nf && offsets[Nf] != Nind) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid size for closure %d should be %d", offsets[Nf], Nind);
4570:   /* Correct for hanging node constraints */
4571:   {
4572:     DMPlexAnchorsModifyMat(dm, section, numPoints, Nind, points, NULL, &numPointsNew, &NindNew, &pointsNew, NULL, offsets, PETSC_TRUE);
4573:     if (numPointsNew) {
4574:       if (!clPoints) {DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);}
4575:       else           {ISRestoreIndices(clPoints, &clp);}
4576:       numPoints = numPointsNew;
4577:       Nind      = NindNew;
4578:       points    = pointsNew;
4579:     }
4580:   }
4581:   /* Calculate indices */
4582:   DMGetWorkArray(dm, Nind, PETSC_INT, indices);
4583:   if (Nf) {
4584:     if (outOffsets) {
4585:       PetscInt f;

4587:       for (f = 0; f <= Nf; f++) {
4588:         outOffsets[f] = offsets[f];
4589:       }
4590:     }
4591:     for (p = 0; p < numPoints*2; p += 2) {
4592:       PetscInt o = points[p+1];
4593:       PetscSectionGetOffset(globalSection, points[p], &globalOff);
4594:       DMPlexGetIndicesPointFields_Internal(section, points[p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, o, *indices);
4595:     }
4596:   } else {
4597:     for (p = 0, off = 0; p < numPoints*2; p += 2) {
4598:       PetscInt o = points[p+1];
4599:       PetscSectionGetOffset(globalSection, points[p], &globalOff);
4600:       DMPlexGetIndicesPoint_Internal(section, points[p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, o, *indices);
4601:     }
4602:   }
4603:   /* Cleanup points */
4604:   if (numPointsNew) {
4605:     DMRestoreWorkArray(dm, 2*numPointsNew, PETSC_INT, &pointsNew);
4606:   } else {
4607:     if (!clPoints) {DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);}
4608:     else           {ISRestoreIndices(clPoints, &clp);}
4609:   }
4610:   if (numIndices) *numIndices = Nind;
4611:   return(0);
4612: }

4616: PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices,PetscInt *outOffsets)
4617: {

4623:   DMRestoreWorkArray(dm, 0, PETSC_INT, indices);
4624:   return(0);
4625: }

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

4632:   Not collective

4634:   Input Parameters:
4635: + dm - The DM
4636: . section - The section describing the layout in v, or NULL to use the default section
4637: . globalSection - The section describing the layout in v, or NULL to use the default global section
4638: . A - The matrix
4639: . point - The sieve point in the DM
4640: . values - The array of values
4641: - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions

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

4646:   Level: intermediate

4648: .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure()
4649: @*/
4650: PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
4651: {
4652:   DM_Plex        *mesh   = (DM_Plex*) dm->data;
4653:   PetscSection    clSection;
4654:   IS              clPoints;
4655:   PetscInt       *points = NULL, *newPoints;
4656:   const PetscInt *clp;
4657:   PetscInt       *indices;
4658:   PetscInt        offsets[32];
4659:   PetscInt        numFields, numPoints, newNumPoints, numIndices, newNumIndices, dof, off, globalOff, pStart, pEnd, p, q, f;
4660:   PetscScalar    *newValues;
4661:   PetscErrorCode  ierr;

4665:   if (!section) {DMGetDefaultSection(dm, &section);}
4667:   if (!globalSection) {DMGetDefaultGlobalSection(dm, &globalSection);}
4670:   PetscSectionGetNumFields(section, &numFields);
4671:   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
4672:   PetscMemzero(offsets, 32 * sizeof(PetscInt));
4673:   PetscSectionGetClosureIndex(section, (PetscObject) dm, &clSection, &clPoints);
4674:   if (!clPoints) {
4675:     DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);
4676:     /* Compress out points not in the section */
4677:     PetscSectionGetChart(section, &pStart, &pEnd);
4678:     for (p = 0, q = 0; p < numPoints*2; p += 2) {
4679:       if ((points[p] >= pStart) && (points[p] < pEnd)) {
4680:         points[q*2]   = points[p];
4681:         points[q*2+1] = points[p+1];
4682:         ++q;
4683:       }
4684:     }
4685:     numPoints = q;
4686:   } else {
4687:     PetscInt dof, off;

4689:     PetscSectionGetDof(clSection, point, &dof);
4690:     numPoints = dof/2;
4691:     PetscSectionGetOffset(clSection, point, &off);
4692:     ISGetIndices(clPoints, &clp);
4693:     points = (PetscInt *) &clp[off];
4694:   }
4695:   for (p = 0, numIndices = 0; p < numPoints*2; p += 2) {
4696:     PetscInt fdof;

4698:     PetscSectionGetDof(section, points[p], &dof);
4699:     for (f = 0; f < numFields; ++f) {
4700:       PetscSectionGetFieldDof(section, points[p], f, &fdof);
4701:       offsets[f+1] += fdof;
4702:     }
4703:     numIndices += dof;
4704:   }
4705:   for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];

4707:   if (numFields && offsets[numFields] != numIndices) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %d should be %d", offsets[numFields], numIndices);
4708:   DMPlexAnchorsModifyMat(dm,section,numPoints,numIndices,points,values,&newNumPoints,&newNumIndices,&newPoints,&newValues,offsets,PETSC_TRUE);
4709:   if (newNumPoints) {
4710:     if (!clPoints) {
4711:       DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);
4712:     } else {
4713:       ISRestoreIndices(clPoints, &clp);
4714:     }
4715:     numPoints  = newNumPoints;
4716:     numIndices = newNumIndices;
4717:     points     = newPoints;
4718:     values     = newValues;
4719:   }
4720:   DMGetWorkArray(dm, numIndices, PETSC_INT, &indices);
4721:   if (numFields) {
4722:     for (p = 0; p < numPoints*2; p += 2) {
4723:       PetscInt o = points[p+1];
4724:       PetscSectionGetOffset(globalSection, points[p], &globalOff);
4725:       DMPlexGetIndicesPointFields_Internal(section, points[p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, o, indices);
4726:     }
4727:   } else {
4728:     for (p = 0, off = 0; p < numPoints*2; p += 2) {
4729:       PetscInt o = points[p+1];
4730:       PetscSectionGetOffset(globalSection, points[p], &globalOff);
4731:       DMPlexGetIndicesPoint_Internal(section, points[p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, o, indices);
4732:     }
4733:   }
4734:   if (mesh->printSetValues) {DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);}
4735:   MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
4736:   if (mesh->printFEM > 1) {
4737:     PetscInt i;
4738:     PetscPrintf(PETSC_COMM_SELF, "  Indices:");
4739:     for (i = 0; i < numIndices; ++i) {PetscPrintf(PETSC_COMM_SELF, " %d", indices[i]);}
4740:     PetscPrintf(PETSC_COMM_SELF, "\n");
4741:   }
4742:   if (ierr) {
4743:     PetscMPIInt    rank;
4744:     PetscErrorCode ierr2;

4746:     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
4747:     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
4748:     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
4749:     ierr2 = DMRestoreWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr2);
4750: 
4751:   }
4752:   if (newNumPoints) {
4753:     DMRestoreWorkArray(dm,newNumIndices*newNumIndices,PETSC_SCALAR,&newValues);
4754:     DMRestoreWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);
4755:   }
4756:   else {
4757:     if (!clPoints) {
4758:       DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);
4759:     } else {
4760:       ISRestoreIndices(clPoints, &clp);
4761:     }
4762:   }
4763:   DMRestoreWorkArray(dm, numIndices, PETSC_INT, &indices);
4764:   return(0);
4765: }

4769: PetscErrorCode DMPlexMatSetClosureRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
4770: {
4771:   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
4772:   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
4773:   PetscInt       *cpoints = NULL;
4774:   PetscInt       *findices, *cindices;
4775:   PetscInt        foffsets[32], coffsets[32];
4776:   CellRefiner     cellRefiner;
4777:   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
4778:   PetscErrorCode  ierr;

4783:   if (!fsection) {DMGetDefaultSection(dmf, &fsection);}
4785:   if (!csection) {DMGetDefaultSection(dmc, &csection);}
4787:   if (!globalFSection) {DMGetDefaultGlobalSection(dmf, &globalFSection);}
4789:   if (!globalCSection) {DMGetDefaultGlobalSection(dmc, &globalCSection);}
4792:   PetscSectionGetNumFields(fsection, &numFields);
4793:   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
4794:   PetscMemzero(foffsets, 32 * sizeof(PetscInt));
4795:   PetscMemzero(coffsets, 32 * sizeof(PetscInt));
4796:   /* Column indices */
4797:   DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);
4798:   maxFPoints = numCPoints;
4799:   /* Compress out points not in the section */
4800:   /*   TODO: Squeeze out points with 0 dof as well */
4801:   PetscSectionGetChart(csection, &pStart, &pEnd);
4802:   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
4803:     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
4804:       cpoints[q*2]   = cpoints[p];
4805:       cpoints[q*2+1] = cpoints[p+1];
4806:       ++q;
4807:     }
4808:   }
4809:   numCPoints = q;
4810:   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
4811:     PetscInt fdof;

4813:     PetscSectionGetDof(csection, cpoints[p], &dof);
4814:     if (!dof) continue;
4815:     for (f = 0; f < numFields; ++f) {
4816:       PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);
4817:       coffsets[f+1] += fdof;
4818:     }
4819:     numCIndices += dof;
4820:   }
4821:   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
4822:   /* Row indices */
4823:   DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);
4824:   CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);
4825:   DMGetWorkArray(dmf, maxFPoints*2*numSubcells, PETSC_INT, &ftotpoints);
4826:   for (r = 0, q = 0; r < numSubcells; ++r) {
4827:     /* TODO Map from coarse to fine cells */
4828:     DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);
4829:     /* Compress out points not in the section */
4830:     PetscSectionGetChart(fsection, &pStart, &pEnd);
4831:     for (p = 0; p < numFPoints*2; p += 2) {
4832:       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
4833:         PetscSectionGetDof(fsection, fpoints[p], &dof);
4834:         if (!dof) continue;
4835:         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
4836:         if (s < q) continue;
4837:         ftotpoints[q*2]   = fpoints[p];
4838:         ftotpoints[q*2+1] = fpoints[p+1];
4839:         ++q;
4840:       }
4841:     }
4842:     DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);
4843:   }
4844:   numFPoints = q;
4845:   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
4846:     PetscInt fdof;

4848:     PetscSectionGetDof(fsection, ftotpoints[p], &dof);
4849:     if (!dof) continue;
4850:     for (f = 0; f < numFields; ++f) {
4851:       PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);
4852:       foffsets[f+1] += fdof;
4853:     }
4854:     numFIndices += dof;
4855:   }
4856:   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];

4858:   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %d should be %d", foffsets[numFields], numFIndices);
4859:   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %d should be %d", coffsets[numFields], numCIndices);
4860:   DMGetWorkArray(dmf, numFIndices, PETSC_INT, &findices);
4861:   DMGetWorkArray(dmc, numCIndices, PETSC_INT, &cindices);
4862:   if (numFields) {
4863:     for (p = 0; p < numFPoints*2; p += 2) {
4864:       PetscInt o = ftotpoints[p+1];
4865:       PetscSectionGetOffset(globalFSection, ftotpoints[p], &globalOff);
4866:       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, o, findices);
4867:     }
4868:     for (p = 0; p < numCPoints*2; p += 2) {
4869:       PetscInt o = cpoints[p+1];
4870:       PetscSectionGetOffset(globalCSection, cpoints[p], &globalOff);
4871:       DMPlexGetIndicesPointFields_Internal(csection, cpoints[p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, o, cindices);
4872:     }
4873:   } else {
4874:     for (p = 0, off = 0; p < numFPoints*2; p += 2) {
4875:       PetscInt o = ftotpoints[p+1];
4876:       PetscSectionGetOffset(globalFSection, ftotpoints[p], &globalOff);
4877:       DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, o, findices);
4878:     }
4879:     for (p = 0, off = 0; p < numCPoints*2; p += 2) {
4880:       PetscInt o = cpoints[p+1];
4881:       PetscSectionGetOffset(globalCSection, cpoints[p], &globalOff);
4882:       DMPlexGetIndicesPoint_Internal(csection, cpoints[p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, o, cindices);
4883:     }
4884:   }
4885:   if (mesh->printSetValues) {DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);}
4886:   MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
4887:   if (ierr) {
4888:     PetscMPIInt    rank;
4889:     PetscErrorCode ierr2;

4891:     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
4892:     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
4893:     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
4894:     ierr2 = DMRestoreWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr2);
4895:     ierr2 = DMRestoreWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr2);
4896: 
4897:   }
4898:   DMRestoreWorkArray(dmf, numCPoints*2*4, PETSC_INT, &ftotpoints);
4899:   DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);
4900:   DMRestoreWorkArray(dmf, numFIndices, PETSC_INT, &findices);
4901:   DMRestoreWorkArray(dmc, numCIndices, PETSC_INT, &cindices);
4902:   return(0);
4903: }

4907: PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
4908: {
4909:   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
4910:   PetscInt      *cpoints = NULL;
4911:   PetscInt       foffsets[32], coffsets[32];
4912:   CellRefiner    cellRefiner;
4913:   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;

4919:   if (!fsection) {DMGetDefaultSection(dmf, &fsection);}
4921:   if (!csection) {DMGetDefaultSection(dmc, &csection);}
4923:   if (!globalFSection) {DMGetDefaultGlobalSection(dmf, &globalFSection);}
4925:   if (!globalCSection) {DMGetDefaultGlobalSection(dmc, &globalCSection);}
4927:   PetscSectionGetNumFields(fsection, &numFields);
4928:   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
4929:   PetscMemzero(foffsets, 32 * sizeof(PetscInt));
4930:   PetscMemzero(coffsets, 32 * sizeof(PetscInt));
4931:   /* Column indices */
4932:   DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);
4933:   maxFPoints = numCPoints;
4934:   /* Compress out points not in the section */
4935:   /*   TODO: Squeeze out points with 0 dof as well */
4936:   PetscSectionGetChart(csection, &pStart, &pEnd);
4937:   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
4938:     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
4939:       cpoints[q*2]   = cpoints[p];
4940:       cpoints[q*2+1] = cpoints[p+1];
4941:       ++q;
4942:     }
4943:   }
4944:   numCPoints = q;
4945:   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
4946:     PetscInt fdof;

4948:     PetscSectionGetDof(csection, cpoints[p], &dof);
4949:     if (!dof) continue;
4950:     for (f = 0; f < numFields; ++f) {
4951:       PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);
4952:       coffsets[f+1] += fdof;
4953:     }
4954:     numCIndices += dof;
4955:   }
4956:   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
4957:   /* Row indices */
4958:   DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);
4959:   CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);
4960:   DMGetWorkArray(dmf, maxFPoints*2*numSubcells, PETSC_INT, &ftotpoints);
4961:   for (r = 0, q = 0; r < numSubcells; ++r) {
4962:     /* TODO Map from coarse to fine cells */
4963:     DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);
4964:     /* Compress out points not in the section */
4965:     PetscSectionGetChart(fsection, &pStart, &pEnd);
4966:     for (p = 0; p < numFPoints*2; p += 2) {
4967:       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
4968:         PetscSectionGetDof(fsection, fpoints[p], &dof);
4969:         if (!dof) continue;
4970:         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
4971:         if (s < q) continue;
4972:         ftotpoints[q*2]   = fpoints[p];
4973:         ftotpoints[q*2+1] = fpoints[p+1];
4974:         ++q;
4975:       }
4976:     }
4977:     DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);
4978:   }
4979:   numFPoints = q;
4980:   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
4981:     PetscInt fdof;

4983:     PetscSectionGetDof(fsection, ftotpoints[p], &dof);
4984:     if (!dof) continue;
4985:     for (f = 0; f < numFields; ++f) {
4986:       PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);
4987:       foffsets[f+1] += fdof;
4988:     }
4989:     numFIndices += dof;
4990:   }
4991:   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];

4993:   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %d should be %d", foffsets[numFields], numFIndices);
4994:   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %d should be %d", coffsets[numFields], numCIndices);
4995:   if (numFields) {
4996:     for (p = 0; p < numFPoints*2; p += 2) {
4997:       PetscInt o = ftotpoints[p+1];
4998:       PetscSectionGetOffset(globalFSection, ftotpoints[p], &globalOff);
4999:       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, o, findices);
5000:     }
5001:     for (p = 0; p < numCPoints*2; p += 2) {
5002:       PetscInt o = cpoints[p+1];
5003:       PetscSectionGetOffset(globalCSection, cpoints[p], &globalOff);
5004:       DMPlexGetIndicesPointFields_Internal(csection, cpoints[p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, o, cindices);
5005:     }
5006:   } else {
5007:     for (p = 0, off = 0; p < numFPoints*2; p += 2) {
5008:       PetscInt o = ftotpoints[p+1];
5009:       PetscSectionGetOffset(globalFSection, ftotpoints[p], &globalOff);
5010:       DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, o, findices);
5011:     }
5012:     for (p = 0, off = 0; p < numCPoints*2; p += 2) {
5013:       PetscInt o = cpoints[p+1];
5014:       PetscSectionGetOffset(globalCSection, cpoints[p], &globalOff);
5015:       DMPlexGetIndicesPoint_Internal(csection, cpoints[p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, o, cindices);
5016:     }
5017:   }
5018:   DMRestoreWorkArray(dmf, numCPoints*2*4, PETSC_INT, &ftotpoints);
5019:   DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);
5020:   return(0);
5021: }

5025: /*@
5026:   DMPlexGetHybridBounds - Get the first mesh point of each dimension which is a hybrid

5028:   Input Parameter:
5029: . dm - The DMPlex object

5031:   Output Parameters:
5032: + cMax - The first hybrid cell
5033: . fMax - The first hybrid face
5034: . eMax - The first hybrid edge
5035: - vMax - The first hybrid vertex

5037:   Level: developer

5039: .seealso DMPlexCreateHybridMesh(), DMPlexSetHybridBounds()
5040: @*/
5041: PetscErrorCode DMPlexGetHybridBounds(DM dm, PetscInt *cMax, PetscInt *fMax, PetscInt *eMax, PetscInt *vMax)
5042: {
5043:   DM_Plex       *mesh = (DM_Plex*) dm->data;
5044:   PetscInt       dim;

5049:   DMGetDimension(dm, &dim);
5050:   if (cMax) *cMax = mesh->hybridPointMax[dim];
5051:   if (fMax) *fMax = mesh->hybridPointMax[dim-1];
5052:   if (eMax) *eMax = mesh->hybridPointMax[1];
5053:   if (vMax) *vMax = mesh->hybridPointMax[0];
5054:   return(0);
5055: }

5059: /*@
5060:   DMPlexSetHybridBounds - Set the first mesh point of each dimension which is a hybrid

5062:   Input Parameters:
5063: . dm   - The DMPlex object
5064: . cMax - The first hybrid cell
5065: . fMax - The first hybrid face
5066: . eMax - The first hybrid edge
5067: - vMax - The first hybrid vertex

5069:   Level: developer

5071: .seealso DMPlexCreateHybridMesh(), DMPlexGetHybridBounds()
5072: @*/
5073: PetscErrorCode DMPlexSetHybridBounds(DM dm, PetscInt cMax, PetscInt fMax, PetscInt eMax, PetscInt vMax)
5074: {
5075:   DM_Plex       *mesh = (DM_Plex*) dm->data;
5076:   PetscInt       dim;

5081:   DMGetDimension(dm, &dim);
5082:   if (cMax >= 0) mesh->hybridPointMax[dim]   = cMax;
5083:   if (fMax >= 0) mesh->hybridPointMax[dim-1] = fMax;
5084:   if (eMax >= 0) mesh->hybridPointMax[1]     = eMax;
5085:   if (vMax >= 0) mesh->hybridPointMax[0]     = vMax;
5086:   return(0);
5087: }

5091: PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
5092: {
5093:   DM_Plex *mesh = (DM_Plex*) dm->data;

5098:   *cellHeight = mesh->vtkCellHeight;
5099:   return(0);
5100: }

5104: PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
5105: {
5106:   DM_Plex *mesh = (DM_Plex*) dm->data;

5110:   mesh->vtkCellHeight = cellHeight;
5111:   return(0);
5112: }

5116: /* We can easily have a form that takes an IS instead */
5117: static PetscErrorCode DMPlexCreateNumbering_Private(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
5118: {
5119:   PetscSection   section, globalSection;
5120:   PetscInt      *numbers, p;

5124:   PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);
5125:   PetscSectionSetChart(section, pStart, pEnd);
5126:   for (p = pStart; p < pEnd; ++p) {
5127:     PetscSectionSetDof(section, p, 1);
5128:   }
5129:   PetscSectionSetUp(section);
5130:   PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);
5131:   PetscMalloc1(pEnd - pStart, &numbers);
5132:   for (p = pStart; p < pEnd; ++p) {
5133:     PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);
5134:     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
5135:     else                       numbers[p-pStart] += shift;
5136:   }
5137:   ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);
5138:   if (globalSize) {
5139:     PetscLayout layout;
5140:     PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);
5141:     PetscLayoutGetSize(layout, globalSize);
5142:     PetscLayoutDestroy(&layout);
5143:   }
5144:   PetscSectionDestroy(&section);
5145:   PetscSectionDestroy(&globalSection);
5146:   return(0);
5147: }

5151: PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
5152: {
5153:   PetscInt       cellHeight, cStart, cEnd, cMax;

5157:   DMPlexGetVTKCellHeight(dm, &cellHeight);
5158:   DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);
5159:   DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);
5160:   if (cMax >= 0 && !includeHybrid) cEnd = PetscMin(cEnd, cMax);
5161:   DMPlexCreateNumbering_Private(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);
5162:   return(0);
5163: }

5167: PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
5168: {
5169:   DM_Plex       *mesh = (DM_Plex*) dm->data;

5174:   if (!mesh->globalCellNumbers) {DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);}
5175:   *globalCellNumbers = mesh->globalCellNumbers;
5176:   return(0);
5177: }

5181: PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
5182: {
5183:   PetscInt       vStart, vEnd, vMax;

5188:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
5189:   DMPlexGetHybridBounds(dm, NULL, NULL, NULL, &vMax);
5190:   if (vMax >= 0 && !includeHybrid) vEnd = PetscMin(vEnd, vMax);
5191:   DMPlexCreateNumbering_Private(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);
5192:   return(0);
5193: }

5197: PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
5198: {
5199:   DM_Plex       *mesh = (DM_Plex*) dm->data;

5204:   if (!mesh->globalVertexNumbers) {DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);}
5205:   *globalVertexNumbers = mesh->globalVertexNumbers;
5206:   return(0);
5207: }

5211: PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
5212: {
5213:   IS             nums[4];
5214:   PetscInt       depths[4];
5215:   PetscInt       depth, d, shift = 0;

5220:   DMPlexGetDepth(dm, &depth);
5221:   /* For unstratified meshes use dim instead of depth */
5222:   if (depth < 0) {DMGetDimension(dm, &depth);}
5223:   depths[0] = depth; depths[1] = 0;
5224:   for (d = 2; d <= depth; ++d) depths[d] = depth-d+1;
5225:   for (d = 0; d <= depth; ++d) {
5226:     PetscInt pStart, pEnd, gsize;

5228:     DMPlexGetDepthStratum(dm, depths[d], &pStart, &pEnd);
5229:     DMPlexCreateNumbering_Private(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);
5230:     shift += gsize;
5231:   }
5232:   ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);
5233:   for (d = 0; d <= depth; ++d) {ISDestroy(&nums[d]);}
5234:   return(0);
5235: }

5239: /*@
5240:   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.

5242:   Input Parameters:
5243:   + dm - The DMPlex object

5245:   Note: This is a useful diagnostic when creating meshes programmatically.

5247:   Level: developer

5249: .seealso: DMCreate(), DMCheckSkeleton(), DMCheckFaces()
5250: @*/
5251: PetscErrorCode DMPlexCheckSymmetry(DM dm)
5252: {
5253:   PetscSection    coneSection, supportSection;
5254:   const PetscInt *cone, *support;
5255:   PetscInt        coneSize, c, supportSize, s;
5256:   PetscInt        pStart, pEnd, p, csize, ssize;
5257:   PetscErrorCode  ierr;

5261:   DMPlexGetConeSection(dm, &coneSection);
5262:   DMPlexGetSupportSection(dm, &supportSection);
5263:   /* Check that point p is found in the support of its cone points, and vice versa */
5264:   DMPlexGetChart(dm, &pStart, &pEnd);
5265:   for (p = pStart; p < pEnd; ++p) {
5266:     DMPlexGetConeSize(dm, p, &coneSize);
5267:     DMPlexGetCone(dm, p, &cone);
5268:     for (c = 0; c < coneSize; ++c) {
5269:       PetscBool dup = PETSC_FALSE;
5270:       PetscInt  d;
5271:       for (d = c-1; d >= 0; --d) {
5272:         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
5273:       }
5274:       DMPlexGetSupportSize(dm, cone[c], &supportSize);
5275:       DMPlexGetSupport(dm, cone[c], &support);
5276:       for (s = 0; s < supportSize; ++s) {
5277:         if (support[s] == p) break;
5278:       }
5279:       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
5280:         PetscPrintf(PETSC_COMM_SELF, "p: %d cone: ", p);
5281:         for (s = 0; s < coneSize; ++s) {
5282:           PetscPrintf(PETSC_COMM_SELF, "%d, ", cone[s]);
5283:         }
5284:         PetscPrintf(PETSC_COMM_SELF, "\n");
5285:         PetscPrintf(PETSC_COMM_SELF, "p: %d support: ", cone[c]);
5286:         for (s = 0; s < supportSize; ++s) {
5287:           PetscPrintf(PETSC_COMM_SELF, "%d, ", support[s]);
5288:         }
5289:         PetscPrintf(PETSC_COMM_SELF, "\n");
5290:         if (dup) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %d not repeatedly found in support of repeated cone point %d", p, cone[c]);
5291:         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %d not found in support of cone point %d", p, cone[c]);
5292:       }
5293:     }
5294:     DMPlexGetSupportSize(dm, p, &supportSize);
5295:     DMPlexGetSupport(dm, p, &support);
5296:     for (s = 0; s < supportSize; ++s) {
5297:       DMPlexGetConeSize(dm, support[s], &coneSize);
5298:       DMPlexGetCone(dm, support[s], &cone);
5299:       for (c = 0; c < coneSize; ++c) {
5300:         if (cone[c] == p) break;
5301:       }
5302:       if (c >= coneSize) {
5303:         PetscPrintf(PETSC_COMM_SELF, "p: %d support: ", p);
5304:         for (c = 0; c < supportSize; ++c) {
5305:           PetscPrintf(PETSC_COMM_SELF, "%d, ", support[c]);
5306:         }
5307:         PetscPrintf(PETSC_COMM_SELF, "\n");
5308:         PetscPrintf(PETSC_COMM_SELF, "p: %d cone: ", support[s]);
5309:         for (c = 0; c < coneSize; ++c) {
5310:           PetscPrintf(PETSC_COMM_SELF, "%d, ", cone[c]);
5311:         }
5312:         PetscPrintf(PETSC_COMM_SELF, "\n");
5313:         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %d not found in cone of support point %d", p, support[s]);
5314:       }
5315:     }
5316:   }
5317:   PetscSectionGetStorageSize(coneSection, &csize);
5318:   PetscSectionGetStorageSize(supportSection, &ssize);
5319:   if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %d != Total support size %d", csize, ssize);
5320:   return(0);
5321: }

5325: /*@
5326:   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices

5328:   Input Parameters:
5329: + dm - The DMPlex object
5330: . isSimplex - Are the cells simplices or tensor products
5331: - cellHeight - Normally 0

5333:   Note: This is a useful diagnostic when creating meshes programmatically.

5335:   Level: developer

5337: .seealso: DMCreate(), DMCheckSymmetry(), DMCheckFaces()
5338: @*/
5339: PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscBool isSimplex, PetscInt cellHeight)
5340: {
5341:   PetscInt       dim, numCorners, numHybridCorners, vStart, vEnd, cStart, cEnd, cMax, c;

5346:   DMGetDimension(dm, &dim);
5347:   switch (dim) {
5348:   case 1: numCorners = isSimplex ? 2 : 2; numHybridCorners = isSimplex ? 2 : 2; break;
5349:   case 2: numCorners = isSimplex ? 3 : 4; numHybridCorners = isSimplex ? 4 : 4; break;
5350:   case 3: numCorners = isSimplex ? 4 : 8; numHybridCorners = isSimplex ? 6 : 8; break;
5351:   default:
5352:     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle meshes of dimension %d", dim);
5353:   }
5354:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
5355:   DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);
5356:   DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);
5357:   cMax = cMax >= 0 ? cMax : cEnd;
5358:   for (c = cStart; c < cMax; ++c) {
5359:     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;

5361:     DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
5362:     for (cl = 0; cl < closureSize*2; cl += 2) {
5363:       const PetscInt p = closure[cl];
5364:       if ((p >= vStart) && (p < vEnd)) ++coneSize;
5365:     }
5366:     DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
5367:     if (coneSize != numCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %d has  %d vertices != %d", c, coneSize, numCorners);
5368:   }
5369:   for (c = cMax; c < cEnd; ++c) {
5370:     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;

5372:     DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
5373:     for (cl = 0; cl < closureSize*2; cl += 2) {
5374:       const PetscInt p = closure[cl];
5375:       if ((p >= vStart) && (p < vEnd)) ++coneSize;
5376:     }
5377:     DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
5378:     if (coneSize > numHybridCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Hybrid cell %d has  %d vertices > %d", c, coneSize, numHybridCorners);
5379:   }
5380:   return(0);
5381: }

5385: /*@
5386:   DMPlexCheckFaces - Check that the faces of each cell give a vertex order this is consistent with what we expect from the cell type

5388:   Input Parameters:
5389: + dm - The DMPlex object
5390: . isSimplex - Are the cells simplices or tensor products
5391: - cellHeight - Normally 0

5393:   Note: This is a useful diagnostic when creating meshes programmatically.

5395:   Level: developer

5397: .seealso: DMCreate(), DMCheckSymmetry(), DMCheckSkeleton()
5398: @*/
5399: PetscErrorCode DMPlexCheckFaces(DM dm, PetscBool isSimplex, PetscInt cellHeight)
5400: {
5401:   PetscInt       pMax[4];
5402:   PetscInt       dim, vStart, vEnd, cStart, cEnd, c, h;

5407:   DMGetDimension(dm, &dim);
5408:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
5409:   DMPlexGetHybridBounds(dm, &pMax[dim], &pMax[dim-1], &pMax[1], &pMax[0]);
5410:   for (h = cellHeight; h < dim; ++h) {
5411:     DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);
5412:     for (c = cStart; c < cEnd; ++c) {
5413:       const PetscInt *cone, *ornt, *faces;
5414:       PetscInt        numFaces, faceSize, coneSize,f;
5415:       PetscInt       *closure = NULL, closureSize, cl, numCorners = 0;

5417:       if (pMax[dim-h] >= 0 && c >= pMax[dim-h]) continue;
5418:       DMPlexGetConeSize(dm, c, &coneSize);
5419:       DMPlexGetCone(dm, c, &cone);
5420:       DMPlexGetConeOrientation(dm, c, &ornt);
5421:       DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
5422:       for (cl = 0; cl < closureSize*2; cl += 2) {
5423:         const PetscInt p = closure[cl];
5424:         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
5425:       }
5426:       DMPlexGetRawFaces_Internal(dm, dim-h, numCorners, closure, &numFaces, &faceSize, &faces);
5427:       if (coneSize != numFaces) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %d has %d faces but should have %d", c, coneSize, numFaces);
5428:       for (f = 0; f < numFaces; ++f) {
5429:         PetscInt *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;

5431:         DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);
5432:         for (cl = 0; cl < fclosureSize*2; cl += 2) {
5433:           const PetscInt p = fclosure[cl];
5434:           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
5435:         }
5436:         if (fnumCorners != faceSize) SETERRQ5(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d (%d) of cell %d has %d vertices but should have %d", cone[f], f, c, fnumCorners, faceSize);
5437:         for (v = 0; v < fnumCorners; ++v) {
5438:           if (fclosure[v] != faces[f*faceSize+v]) SETERRQ6(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %d (%d) of cell %d vertex %d, %d != %d", cone[f], f, c, v, fclosure[v], faces[f*faceSize+v]);
5439:         }
5440:         DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);
5441:       }
5442:       DMPlexRestoreFaces_Internal(dm, dim, c, &numFaces, &faceSize, &faces);
5443:       DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);
5444:     }
5445:   }
5446:   return(0);
5447: }

5451: /* Pointwise interpolation
5452:      Just code FEM for now
5453:      u^f = I u^c
5454:      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
5455:      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
5456:      I_{ij} = psi^f_i phi^c_j
5457: */
5458: PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
5459: {
5460:   PetscSection   gsc, gsf;
5461:   PetscInt       m, n;
5462:   void          *ctx;
5463:   DM             cdm;
5464:   PetscBool      regular;

5468:   DMGetDefaultGlobalSection(dmFine, &gsf);
5469:   PetscSectionGetConstrainedStorageSize(gsf, &m);
5470:   DMGetDefaultGlobalSection(dmCoarse, &gsc);
5471:   PetscSectionGetConstrainedStorageSize(gsc, &n);

5473:   MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);
5474:   MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);
5475:   MatSetType(*interpolation, dmCoarse->mattype);
5476:   DMGetApplicationContext(dmFine, &ctx);

5478:   DMGetCoarseDM(dmFine, &cdm);
5479:   DMPlexGetRegularRefinement(dmFine, &regular);
5480:   if (regular && cdm == dmCoarse) {DMPlexComputeInterpolatorNested(dmCoarse, dmFine, *interpolation, ctx);}
5481:   else                            {DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);}
5482:   MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");
5483:   /* Use naive scaling */
5484:   DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);
5485:   return(0);
5486: }

5490: PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
5491: {
5493:   VecScatter     ctx;

5496:   DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);
5497:   MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);
5498:   VecScatterDestroy(&ctx);
5499:   return(0);
5500: }

5504: PetscErrorCode DMCreateDefaultSection_Plex(DM dm)
5505: {
5506:   PetscSection   section;
5507:   IS            *bcPoints, *bcComps;
5508:   PetscBool     *isFE;
5509:   PetscInt      *bcFields, *numComp, *numDof;
5510:   PetscInt       depth, dim, numBd, numBC = 0, numFields, bd, bc = 0, f;
5511:   PetscInt       cStart, cEnd, cEndInterior;

5515:   DMGetNumFields(dm, &numFields);
5516:   if (!numFields) return(0);
5517:   /* FE and FV boundary conditions are handled slightly differently */
5518:   PetscMalloc1(numFields, &isFE);
5519:   for (f = 0; f < numFields; ++f) {
5520:     PetscObject  obj;
5521:     PetscClassId id;

5523:     DMGetField(dm, f, &obj);
5524:     PetscObjectGetClassId(obj, &id);
5525:     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
5526:     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
5527:     else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
5528:   }
5529:   /* Allocate boundary point storage for FEM boundaries */
5530:   DMPlexGetDepth(dm, &depth);
5531:   DMGetDimension(dm, &dim);
5532:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
5533:   DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);
5534:   DMGetNumBoundary(dm, &numBd);
5535:   for (bd = 0; bd < numBd; ++bd) {
5536:     PetscInt  field;
5537:     PetscBool isEssential;

5539:     DMGetBoundary(dm, bd, &isEssential, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL, NULL);
5540:     if (isFE[field] && isEssential) ++numBC;
5541:   }
5542:   /* Add ghost cell boundaries for FVM */
5543:   for (f = 0; f < numFields; ++f) if (!isFE[f] && cEndInterior >= 0) ++numBC;
5544:   PetscCalloc3(numBC,&bcFields,numBC,&bcPoints,numBC,&bcComps);
5545:   /* Constrain ghost cells for FV */
5546:   for (f = 0; f < numFields; ++f) {
5547:     PetscInt *newidx, c;

5549:     if (isFE[f] || cEndInterior < 0) continue;
5550:     PetscMalloc1(cEnd-cEndInterior,&newidx);
5551:     for (c = cEndInterior; c < cEnd; ++c) newidx[c-cEndInterior] = c;
5552:     bcFields[bc] = f;
5553:     ISCreateGeneral(PetscObjectComm((PetscObject) dm), cEnd-cEndInterior, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);
5554:   }
5555:   /* Handle FEM Dirichlet boundaries */
5556:   for (bd = 0; bd < numBd; ++bd) {
5557:     const char     *bdLabel;
5558:     DMLabel         label;
5559:     const PetscInt *comps;
5560:     const PetscInt *values;
5561:     PetscInt        bd2, field, numComps, numValues;
5562:     PetscBool       isEssential, duplicate = PETSC_FALSE;

5564:     DMGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, &numComps, &comps, NULL, &numValues, &values, NULL);
5565:     if (!isFE[field]) continue;
5566:     DMGetLabel(dm, bdLabel, &label);
5567:     /* Only want to modify label once */
5568:     for (bd2 = 0; bd2 < bd; ++bd2) {
5569:       const char *bdname;
5570:       DMGetBoundary(dm, bd2, NULL, NULL, &bdname, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
5571:       PetscStrcmp(bdname, bdLabel, &duplicate);
5572:       if (duplicate) break;
5573:     }
5574:     if (!duplicate && (isFE[field])) {
5575:       /* don't complete cells, which are just present to give orientation to the boundary */
5576:       DMPlexLabelComplete(dm, label);
5577:     }
5578:     /* Filter out cells, if you actually want to constrain cells you need to do things by hand right now */
5579:     if (isEssential) {
5580:       PetscInt       *newidx;
5581:       PetscInt        n, newn = 0, p, v;

5583:       bcFields[bc] = field;
5584:       if (numComps) {ISCreateGeneral(PetscObjectComm((PetscObject) dm), numComps, comps, PETSC_COPY_VALUES, &bcComps[bc]);}
5585:       for (v = 0; v < numValues; ++v) {
5586:         IS              tmp;
5587:         const PetscInt *idx;

5589:         DMGetStratumIS(dm, bdLabel, values[v], &tmp);
5590:         if (!tmp) continue;
5591:         ISGetLocalSize(tmp, &n);
5592:         ISGetIndices(tmp, &idx);
5593:         if (isFE[field]) {
5594:           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) ++newn;
5595:         } else {
5596:           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) ++newn;
5597:         }
5598:         ISRestoreIndices(tmp, &idx);
5599:         ISDestroy(&tmp);
5600:       }
5601:       PetscMalloc1(newn,&newidx);
5602:       newn = 0;
5603:       for (v = 0; v < numValues; ++v) {
5604:         IS              tmp;
5605:         const PetscInt *idx;

5607:         DMGetStratumIS(dm, bdLabel, values[v], &tmp);
5608:         if (!tmp) continue;
5609:         ISGetLocalSize(tmp, &n);
5610:         ISGetIndices(tmp, &idx);
5611:         if (isFE[field]) {
5612:           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) newidx[newn++] = idx[p];
5613:         } else {
5614:           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) newidx[newn++] = idx[p];
5615:         }
5616:         ISRestoreIndices(tmp, &idx);
5617:         ISDestroy(&tmp);
5618:       }
5619:       ISCreateGeneral(PetscObjectComm((PetscObject) dm), newn, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);
5620:     }
5621:   }
5622:   /* Handle discretization */
5623:   PetscCalloc2(numFields,&numComp,numFields*(dim+1),&numDof);
5624:   for (f = 0; f < numFields; ++f) {
5625:     PetscObject obj;

5627:     DMGetField(dm, f, &obj);
5628:     if (isFE[f]) {
5629:       PetscFE         fe = (PetscFE) obj;
5630:       const PetscInt *numFieldDof;
5631:       PetscInt        d;

5633:       PetscFEGetNumComponents(fe, &numComp[f]);
5634:       PetscFEGetNumDof(fe, &numFieldDof);
5635:       for (d = 0; d < dim+1; ++d) numDof[f*(dim+1)+d] = numFieldDof[d];
5636:     } else {
5637:       PetscFV fv = (PetscFV) obj;

5639:       PetscFVGetNumComponents(fv, &numComp[f]);
5640:       numDof[f*(dim+1)+dim] = numComp[f];
5641:     }
5642:   }
5643:   for (f = 0; f < numFields; ++f) {
5644:     PetscInt d;
5645:     for (d = 1; d < dim; ++d) {
5646:       if ((numDof[f*(dim+1)+d] > 0) && (depth < dim)) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Mesh must be interpolated when unknowns are specified on edges or faces.");
5647:     }
5648:   }
5649:   DMPlexCreateSection(dm, dim, numFields, numComp, numDof, numBC, bcFields, bcComps, bcPoints, NULL, &section);
5650:   for (f = 0; f < numFields; ++f) {
5651:     PetscFE     fe;
5652:     const char *name;

5654:     DMGetField(dm, f, (PetscObject *) &fe);
5655:     PetscObjectGetName((PetscObject) fe, &name);
5656:     PetscSectionSetFieldName(section, f, name);
5657:   }
5658:   DMSetDefaultSection(dm, section);
5659:   PetscSectionDestroy(&section);
5660:   for (bc = 0; bc < numBC; ++bc) {ISDestroy(&bcPoints[bc]);ISDestroy(&bcComps[bc]);}
5661:   PetscFree3(bcFields,bcPoints,bcComps);
5662:   PetscFree2(numComp,numDof);
5663:   PetscFree(isFE);
5664:   return(0);
5665: }

5669: /*@
5670:   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh

5672:   Input Parameter:
5673: . dm - The DMPlex object

5675:   Output Parameter:
5676: . regular - The flag

5678:   Level: intermediate

5680: .seealso: DMPlexSetRegularRefinement()
5681: @*/
5682: PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
5683: {
5687:   *regular = ((DM_Plex *) dm->data)->regularRefinement;
5688:   return(0);
5689: }

5693: /*@
5694:   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh

5696:   Input Parameters:
5697: + dm - The DMPlex object
5698: - regular - The flag

5700:   Level: intermediate

5702: .seealso: DMPlexGetRegularRefinement()
5703: @*/
5704: PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
5705: {
5708:   ((DM_Plex *) dm->data)->regularRefinement = regular;
5709:   return(0);
5710: }

5712: /* anchors */
5715: /*@
5716:   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
5717:   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().

5719:   not collective

5721:   Input Parameters:
5722: . dm - The DMPlex object

5724:   Output Parameters:
5725: + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
5726: - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection


5729:   Level: intermediate

5731: .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
5732: @*/
5733: PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
5734: {
5735:   DM_Plex *plex = (DM_Plex *)dm->data;

5740:   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {(*plex->createanchors)(dm);}
5741:   if (anchorSection) *anchorSection = plex->anchorSection;
5742:   if (anchorIS) *anchorIS = plex->anchorIS;
5743:   return(0);
5744: }

5748: /*@
5749:   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
5750:   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
5751:   point's degrees of freedom to be a linear combination of other points' degrees of freedom.

5753:   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
5754:   DMGetConstraints() and filling in the entries in the constraint matrix.

5756:   collective on dm

5758:   Input Parameters:
5759: + dm - The DMPlex object
5760: . 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).
5761: - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).

5763:   The reference counts of anchorSection and anchorIS are incremented.

5765:   Level: intermediate

5767: .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
5768: @*/
5769: PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
5770: {
5771:   DM_Plex        *plex = (DM_Plex *)dm->data;
5772:   PetscMPIInt    result;

5777:   if (anchorSection) {
5779:     MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);
5780:     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
5781:   }
5782:   if (anchorIS) {
5784:     MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);
5785:     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
5786:   }

5788:   PetscObjectReference((PetscObject)anchorSection);
5789:   PetscSectionDestroy(&plex->anchorSection);
5790:   plex->anchorSection = anchorSection;

5792:   PetscObjectReference((PetscObject)anchorIS);
5793:   ISDestroy(&plex->anchorIS);
5794:   plex->anchorIS = anchorIS;

5796: #if defined(PETSC_USE_DEBUG)
5797:   if (anchorIS && anchorSection) {
5798:     PetscInt size, a, pStart, pEnd;
5799:     const PetscInt *anchors;

5801:     PetscSectionGetChart(anchorSection,&pStart,&pEnd);
5802:     ISGetLocalSize(anchorIS,&size);
5803:     ISGetIndices(anchorIS,&anchors);
5804:     for (a = 0; a < size; a++) {
5805:       PetscInt p;

5807:       p = anchors[a];
5808:       if (p >= pStart && p < pEnd) {
5809:         PetscInt dof;

5811:         PetscSectionGetDof(anchorSection,p,&dof);
5812:         if (dof) {
5813:           PetscErrorCode ierr2;

5815:           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
5816:           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %d cannot be constrained and an anchor",p);
5817:         }
5818:       }
5819:     }
5820:     ISRestoreIndices(anchorIS,&anchors);
5821:   }
5822: #endif
5823:   /* reset the generic constraints */
5824:   DMSetDefaultConstraints(dm,NULL,NULL);
5825:   return(0);
5826: }

5830: static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
5831: {
5832:   PetscSection anchorSection;
5833:   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;

5838:   DMPlexGetAnchors(dm,&anchorSection,NULL);
5839:   PetscSectionCreate(PETSC_COMM_SELF,cSec);
5840:   PetscSectionGetNumFields(section,&numFields);
5841:   if (numFields) {
5842:     PetscInt f;
5843:     PetscSectionSetNumFields(*cSec,numFields);

5845:     for (f = 0; f < numFields; f++) {
5846:       PetscInt numComp;

5848:       PetscSectionGetFieldComponents(section,f,&numComp);
5849:       PetscSectionSetFieldComponents(*cSec,f,numComp);
5850:     }
5851:   }
5852:   PetscSectionGetChart(anchorSection,&pStart,&pEnd);
5853:   PetscSectionGetChart(section,&sStart,&sEnd);
5854:   pStart = PetscMax(pStart,sStart);
5855:   pEnd   = PetscMin(pEnd,sEnd);
5856:   pEnd   = PetscMax(pStart,pEnd);
5857:   PetscSectionSetChart(*cSec,pStart,pEnd);
5858:   for (p = pStart; p < pEnd; p++) {
5859:     PetscSectionGetDof(anchorSection,p,&dof);
5860:     if (dof) {
5861:       PetscSectionGetDof(section,p,&dof);
5862:       PetscSectionSetDof(*cSec,p,dof);
5863:       for (f = 0; f < numFields; f++) {
5864:         PetscSectionGetFieldDof(section,p,f,&dof);
5865:         PetscSectionSetFieldDof(*cSec,p,f,dof);
5866:       }
5867:     }
5868:   }
5869:   PetscSectionSetUp(*cSec);
5870:   return(0);
5871: }

5875: static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
5876: {
5877:   PetscSection aSec;
5878:   PetscInt pStart, pEnd, p, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
5879:   const PetscInt *anchors;
5880:   PetscInt numFields, f;
5881:   IS aIS;

5886:   PetscSectionGetStorageSize(cSec, &m);
5887:   PetscSectionGetStorageSize(section, &n);
5888:   MatCreate(PETSC_COMM_SELF,cMat);
5889:   MatSetSizes(*cMat,m,n,m,n);
5890:   MatSetType(*cMat,MATSEQAIJ);
5891:   DMPlexGetAnchors(dm,&aSec,&aIS);
5892:   ISGetIndices(aIS,&anchors);
5893:   /* cSec will be a subset of aSec and section */
5894:   PetscSectionGetChart(cSec,&pStart,&pEnd);
5895:   PetscMalloc1(m+1,&i);
5896:   i[0] = 0;
5897:   PetscSectionGetNumFields(section,&numFields);
5898:   for (p = pStart; p < pEnd; p++) {
5899:     PetscInt rDof, rOff, r;

5901:     PetscSectionGetDof(aSec,p,&rDof);
5902:     if (!rDof) continue;
5903:     PetscSectionGetOffset(aSec,p,&rOff);
5904:     if (numFields) {
5905:       for (f = 0; f < numFields; f++) {
5906:         annz = 0;
5907:         for (r = 0; r < rDof; r++) {
5908:           a = anchors[rOff + r];
5909:           PetscSectionGetFieldDof(section,a,f,&aDof);
5910:           annz += aDof;
5911:         }
5912:         PetscSectionGetFieldDof(cSec,p,f,&dof);
5913:         PetscSectionGetFieldOffset(cSec,p,f,&off);
5914:         for (q = 0; q < dof; q++) {
5915:           i[off + q + 1] = i[off + q] + annz;
5916:         }
5917:       }
5918:     }
5919:     else {
5920:       annz = 0;
5921:       for (q = 0; q < dof; q++) {
5922:         a = anchors[off + q];
5923:         PetscSectionGetDof(section,a,&aDof);
5924:         annz += aDof;
5925:       }
5926:       PetscSectionGetDof(cSec,p,&dof);
5927:       PetscSectionGetOffset(cSec,p,&off);
5928:       for (q = 0; q < dof; q++) {
5929:         i[off + q + 1] = i[off + q] + annz;
5930:       }
5931:     }
5932:   }
5933:   nnz = i[m];
5934:   PetscMalloc1(nnz,&j);
5935:   offset = 0;
5936:   for (p = pStart; p < pEnd; p++) {
5937:     if (numFields) {
5938:       for (f = 0; f < numFields; f++) {
5939:         PetscSectionGetFieldDof(cSec,p,f,&dof);
5940:         for (q = 0; q < dof; q++) {
5941:           PetscInt rDof, rOff, r;
5942:           PetscSectionGetDof(aSec,p,&rDof);
5943:           PetscSectionGetOffset(aSec,p,&rOff);
5944:           for (r = 0; r < rDof; r++) {
5945:             PetscInt s;

5947:             a = anchors[rOff + r];
5948:             PetscSectionGetFieldDof(section,a,f,&aDof);
5949:             PetscSectionGetFieldOffset(section,a,f,&aOff);
5950:             for (s = 0; s < aDof; s++) {
5951:               j[offset++] = aOff + s;
5952:             }
5953:           }
5954:         }
5955:       }
5956:     }
5957:     else {
5958:       PetscSectionGetDof(cSec,p,&dof);
5959:       for (q = 0; q < dof; q++) {
5960:         PetscInt rDof, rOff, r;
5961:         PetscSectionGetDof(aSec,p,&rDof);
5962:         PetscSectionGetOffset(aSec,p,&rOff);
5963:         for (r = 0; r < rDof; r++) {
5964:           PetscInt s;

5966:           a = anchors[rOff + r];
5967:           PetscSectionGetDof(section,a,&aDof);
5968:           PetscSectionGetOffset(section,a,&aOff);
5969:           for (s = 0; s < aDof; s++) {
5970:             j[offset++] = aOff + s;
5971:           }
5972:         }
5973:       }
5974:     }
5975:   }
5976:   MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);
5977:   PetscFree(i);
5978:   PetscFree(j);
5979:   ISRestoreIndices(aIS,&anchors);
5980:   return(0);
5981: }

5985: PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
5986: {
5987:   DM_Plex        *plex = (DM_Plex *)dm->data;
5988:   PetscSection   anchorSection, section, cSec;
5989:   Mat            cMat;

5994:   DMPlexGetAnchors(dm,&anchorSection,NULL);
5995:   if (anchorSection) {
5996:     PetscDS  ds;
5997:     PetscInt nf;

5999:     DMGetDefaultSection(dm,&section);
6000:     DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);
6001:     DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);
6002:     DMGetDS(dm,&ds);
6003:     PetscDSGetNumFields(ds,&nf);
6004:     if (nf && plex->computeanchormatrix) {(*plex->computeanchormatrix)(dm,section,cSec,cMat);}
6005:     DMSetDefaultConstraints(dm,cSec,cMat);
6006:     PetscSectionDestroy(&cSec);
6007:     MatDestroy(&cMat);
6008:   }
6009:   return(0);
6010: }