Actual source code: plexcreate.c

  1: #define PETSCDM_DLL
  2: #include <petsc/private/dmpleximpl.h>
  3: #include <petsc/private/hashseti.h>
  4: #include <petscsf.h>

  6: PetscLogEvent DMPLEX_CreateFromFile, DMPLEX_BuildFromCellList, DMPLEX_BuildCoordinatesFromCellList;

  8: /* External function declarations here */
  9: static PetscErrorCode DMInitialize_Plex(DM dm);

 11: /* Replace dm with the contents of ndm, and then destroy ndm
 12:    - Share the DM_Plex structure
 13:    - Share the coordinates
 14:    - Share the SF
 15: */
 16: static PetscErrorCode DMPlexReplace_Static(DM dm, DM *ndm)
 17: {
 18:   PetscSF               sf;
 19:   DM                    dmNew = *ndm, coordDM, coarseDM;
 20:   Vec                   coords;
 21:   PetscBool             isper;
 22:   const PetscReal      *maxCell, *L;
 23:   const DMBoundaryType *bd;
 24:   PetscInt              dim, cdim;
 25:   PetscErrorCode        ierr;

 28:   if (dm == dmNew) {
 29:     DMDestroy(ndm);
 30:     return(0);
 31:   }
 32:   dm->setupcalled = dmNew->setupcalled;
 33:   DMGetDimension(dmNew, &dim);
 34:   DMSetDimension(dm, dim);
 35:   DMGetCoordinateDim(dmNew, &cdim);
 36:   DMSetCoordinateDim(dm, cdim);
 37:   DMGetPointSF(dmNew, &sf);
 38:   DMSetPointSF(dm, sf);
 39:   DMGetCoordinateDM(dmNew, &coordDM);
 40:   DMGetCoordinatesLocal(dmNew, &coords);
 41:   DMSetCoordinateDM(dm, coordDM);
 42:   DMSetCoordinatesLocal(dm, coords);
 43:   /* Do not want to create the coordinate field if it does not already exist, so do not call DMGetCoordinateField() */
 44:   DMFieldDestroy(&dm->coordinateField);
 45:   dm->coordinateField = dmNew->coordinateField;
 46:   ((DM_Plex *) dmNew->data)->coordFunc = ((DM_Plex *) dm->data)->coordFunc;
 47:   DMGetPeriodicity(dmNew, &isper, &maxCell, &L, &bd);
 48:   DMSetPeriodicity(dm, isper, maxCell, L, bd);
 49:   DMDestroy_Plex(dm);
 50:   DMInitialize_Plex(dm);
 51:   dm->data = dmNew->data;
 52:   ((DM_Plex *) dmNew->data)->refct++;
 53:   DMDestroyLabelLinkList_Internal(dm);
 54:   DMCopyLabels(dmNew, dm, PETSC_OWN_POINTER, PETSC_TRUE);
 55:   DMGetCoarseDM(dmNew,&coarseDM);
 56:   DMSetCoarseDM(dm,coarseDM);
 57:   DMDestroy(ndm);
 58:   return(0);
 59: }

 61: /* Swap dm with the contents of dmNew
 62:    - Swap the DM_Plex structure
 63:    - Swap the coordinates
 64:    - Swap the point PetscSF
 65: */
 66: static PetscErrorCode DMPlexSwap_Static(DM dmA, DM dmB)
 67: {
 68:   DM              coordDMA, coordDMB;
 69:   Vec             coordsA,  coordsB;
 70:   PetscSF         sfA,      sfB;
 71:   DMField         fieldTmp;
 72:   void            *tmp;
 73:   DMLabelLink     listTmp;
 74:   DMLabel         depthTmp;
 75:   PetscInt        tmpI;
 76:   PetscErrorCode  ierr;

 79:   if (dmA == dmB) return(0);
 80:   DMGetPointSF(dmA, &sfA);
 81:   DMGetPointSF(dmB, &sfB);
 82:   PetscObjectReference((PetscObject) sfA);
 83:   DMSetPointSF(dmA, sfB);
 84:   DMSetPointSF(dmB, sfA);
 85:   PetscObjectDereference((PetscObject) sfA);

 87:   DMGetCoordinateDM(dmA, &coordDMA);
 88:   DMGetCoordinateDM(dmB, &coordDMB);
 89:   PetscObjectReference((PetscObject) coordDMA);
 90:   DMSetCoordinateDM(dmA, coordDMB);
 91:   DMSetCoordinateDM(dmB, coordDMA);
 92:   PetscObjectDereference((PetscObject) coordDMA);

 94:   DMGetCoordinatesLocal(dmA, &coordsA);
 95:   DMGetCoordinatesLocal(dmB, &coordsB);
 96:   PetscObjectReference((PetscObject) coordsA);
 97:   DMSetCoordinatesLocal(dmA, coordsB);
 98:   DMSetCoordinatesLocal(dmB, coordsA);
 99:   PetscObjectDereference((PetscObject) coordsA);

101:   fieldTmp             = dmA->coordinateField;
102:   dmA->coordinateField = dmB->coordinateField;
103:   dmB->coordinateField = fieldTmp;
104:   tmp       = dmA->data;
105:   dmA->data = dmB->data;
106:   dmB->data = tmp;
107:   listTmp   = dmA->labels;
108:   dmA->labels = dmB->labels;
109:   dmB->labels = listTmp;
110:   depthTmp  = dmA->depthLabel;
111:   dmA->depthLabel = dmB->depthLabel;
112:   dmB->depthLabel = depthTmp;
113:   depthTmp  = dmA->celltypeLabel;
114:   dmA->celltypeLabel = dmB->celltypeLabel;
115:   dmB->celltypeLabel = depthTmp;
116:   tmpI         = dmA->levelup;
117:   dmA->levelup = dmB->levelup;
118:   dmB->levelup = tmpI;
119:   return(0);
120: }

122: static PetscErrorCode DMPlexInterpolateInPlace_Internal(DM dm)
123: {
124:   DM             idm;

128:   DMPlexInterpolate(dm, &idm);
129:   DMPlexCopyCoordinates(dm, idm);
130:   DMPlexReplace_Static(dm, &idm);
131:   return(0);
132: }

134: /*@C
135:   DMPlexCreateCoordinateSpace - Creates a finite element space for the coordinates

137:   Collective

139:   Input Parameters:
140: + DM        - The DM
141: . degree    - The degree of the finite element
142: - coordFunc - An optional function to map new points from refinement to the surface

144:   Level: advanced

146: .seealso: PetscFECreateLagrange(), DMGetCoordinateDM()
147: @*/
148: PetscErrorCode DMPlexCreateCoordinateSpace(DM dm, PetscInt degree, PetscPointFunc coordFunc)
149: {
150:   DM_Plex       *mesh = (DM_Plex *) dm->data;
151:   DM             cdm;
152:   PetscDS        cds;
153:   PetscFE        fe;
154:   PetscClassId   id;

158:   DMGetCoordinateDM(dm, &cdm);
159:   DMGetDS(cdm, &cds);
160:   PetscDSGetDiscretization(cds, 0, (PetscObject *) &fe);
161:   PetscObjectGetClassId((PetscObject) fe, &id);
162:   if (id != PETSCFE_CLASSID) {
163:     PetscBool simplex;
164:     PetscInt  dim, dE, qorder;

166:     DMGetDimension(dm, &dim);
167:     DMGetCoordinateDim(dm, &dE);
168:     DMPlexIsSimplex(dm, &simplex);
169:     qorder = degree;
170:     PetscObjectOptionsBegin((PetscObject) cdm);
171:     PetscOptionsBoundedInt("-coord_dm_default_quadrature_order", "Quadrature order is one less than quadrature points per edge", "DMPlexCreateCoordinateSpace", qorder, &qorder, NULL, 0);
172:     PetscOptionsEnd();
173:     PetscFECreateLagrange(PETSC_COMM_SELF, dim, dE, simplex, degree, qorder, &fe);
174:     DMSetField(cdm, 0, NULL, (PetscObject) fe);
175:     DMCreateDS(cdm);
176:     DMProjectCoordinates(dm, fe);
177:     PetscFEDestroy(&fe);
178:   }
179:   mesh->coordFunc = coordFunc;
180:   return(0);
181: }

183: /*@
184:   DMPlexCreateDoublet - Creates a mesh of two cells of the specified type, optionally with later refinement.

186:   Collective

188:   Input Parameters:
189: + comm - The communicator for the DM object
190: . dim - The spatial dimension
191: . simplex - Flag for simplicial cells, otherwise they are tensor product cells
192: . interpolate - Flag to create intermediate mesh pieces (edges, faces)
193: - refinementLimit - A nonzero number indicates the largest admissible volume for a refined cell

195:   Output Parameter:
196: . dm - The DM object

198:   Level: beginner

200: .seealso: DMSetType(), DMCreate()
201: @*/
202: PetscErrorCode DMPlexCreateDoublet(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscBool interpolate, PetscReal refinementLimit, DM *newdm)
203: {
204:   DM             dm;
205:   PetscMPIInt    rank;

209:   DMCreate(comm, &dm);
210:   DMSetType(dm, DMPLEX);
211:   DMSetDimension(dm, dim);
212:   MPI_Comm_rank(comm, &rank);
213:   switch (dim) {
214:   case 2:
215:     if (simplex) {PetscObjectSetName((PetscObject) dm, "triangular");}
216:     else         {PetscObjectSetName((PetscObject) dm, "quadrilateral");}
217:     break;
218:   case 3:
219:     if (simplex) {PetscObjectSetName((PetscObject) dm, "tetrahedral");}
220:     else         {PetscObjectSetName((PetscObject) dm, "hexahedral");}
221:     break;
222:   default:
223:     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %D", dim);
224:   }
225:   if (rank) {
226:     PetscInt numPoints[2] = {0, 0};
227:     DMPlexCreateFromDAG(dm, 1, numPoints, NULL, NULL, NULL, NULL);
228:   } else {
229:     switch (dim) {
230:     case 2:
231:       if (simplex) {
232:         PetscInt    numPoints[2]        = {4, 2};
233:         PetscInt    coneSize[6]         = {3, 3, 0, 0, 0, 0};
234:         PetscInt    cones[6]            = {2, 3, 4,  5, 4, 3};
235:         PetscInt    coneOrientations[6] = {0, 0, 0,  0, 0, 0};
236:         PetscScalar vertexCoords[8]     = {-0.5, 0.5, 0.0, 0.0, 0.0, 1.0, 0.5, 0.5};

238:         DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
239:       } else {
240:         PetscInt    numPoints[2]        = {6, 2};
241:         PetscInt    coneSize[8]         = {4, 4, 0, 0, 0, 0, 0, 0};
242:         PetscInt    cones[8]            = {2, 3, 4, 5,  3, 6, 7, 4};
243:         PetscInt    coneOrientations[8] = {0, 0, 0, 0,  0, 0, 0, 0};
244:         PetscScalar vertexCoords[12]    = {-1.0, -0.5,  0.0, -0.5,  0.0, 0.5,  -1.0, 0.5,  1.0, -0.5,  1.0, 0.5};

246:         DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
247:       }
248:       break;
249:     case 3:
250:       if (simplex) {
251:         PetscInt    numPoints[2]        = {5, 2};
252:         PetscInt    coneSize[7]         = {4, 4, 0, 0, 0, 0, 0};
253:         PetscInt    cones[8]            = {4, 3, 5, 2,  5, 3, 4, 6};
254:         PetscInt    coneOrientations[8] = {0, 0, 0, 0,  0, 0, 0, 0};
255:         PetscScalar vertexCoords[15]    = {-1.0, 0.0, 0.0,  0.0, -1.0, 0.0,  0.0, 0.0, 1.0,  0.0, 1.0, 0.0,  1.0, 0.0, 0.0};

257:         DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
258:       } else {
259:         PetscInt    numPoints[2]         = {12, 2};
260:         PetscInt    coneSize[14]         = {8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
261:         PetscInt    cones[16]            = {2, 3, 4, 5, 6, 7, 8, 9,  5, 4, 10, 11, 7, 12, 13, 8};
262:         PetscInt    coneOrientations[16] = {0, 0, 0, 0, 0, 0, 0, 0,  0, 0,  0,  0, 0,  0,  0, 0};
263:         PetscScalar vertexCoords[36]     = {-1.0, -0.5, -0.5,  -1.0,  0.5, -0.5,  0.0,  0.5, -0.5,   0.0, -0.5, -0.5,
264:                                             -1.0, -0.5,  0.5,   0.0, -0.5,  0.5,  0.0,  0.5,  0.5,  -1.0,  0.5,  0.5,
265:                                              1.0,  0.5, -0.5,   1.0, -0.5, -0.5,  1.0, -0.5,  0.5,   1.0,  0.5,  0.5};

267:         DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
268:       }
269:       break;
270:     default:
271:       SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %D", dim);
272:     }
273:   }
274:   *newdm = dm;
275:   if (refinementLimit > 0.0) {
276:     DM rdm;
277:     const char *name;

279:     DMPlexSetRefinementUniform(*newdm, PETSC_FALSE);
280:     DMPlexSetRefinementLimit(*newdm, refinementLimit);
281:     DMRefine(*newdm, comm, &rdm);
282:     PetscObjectGetName((PetscObject) *newdm, &name);
283:     PetscObjectSetName((PetscObject)    rdm,  name);
284:     DMDestroy(newdm);
285:     *newdm = rdm;
286:   }
287:   if (interpolate) {
288:     DM idm;

290:     DMPlexInterpolate(*newdm, &idm);
291:     DMDestroy(newdm);
292:     *newdm = idm;
293:   }
294:   return(0);
295: }

297: static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
298: {
299:   const PetscInt numVertices    = 2;
300:   PetscInt       markerRight    = 1;
301:   PetscInt       markerLeft     = 1;
302:   PetscBool      markerSeparate = PETSC_FALSE;
303:   Vec            coordinates;
304:   PetscSection   coordSection;
305:   PetscScalar   *coords;
306:   PetscInt       coordSize;
307:   PetscMPIInt    rank;
308:   PetscInt       cdim = 1, v;

312:   PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);
313:   if (markerSeparate) {
314:     markerRight  = 2;
315:     markerLeft   = 1;
316:   }
317:   MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
318:   if (!rank) {
319:     DMPlexSetChart(dm, 0, numVertices);
320:     DMSetUp(dm); /* Allocate space for cones */
321:     DMSetLabelValue(dm, "marker", 0, markerLeft);
322:     DMSetLabelValue(dm, "marker", 1, markerRight);
323:   }
324:   DMPlexSymmetrize(dm);
325:   DMPlexStratify(dm);
326:   /* Build coordinates */
327:   DMSetCoordinateDim(dm, cdim);
328:   DMGetCoordinateSection(dm, &coordSection);
329:   PetscSectionSetNumFields(coordSection, 1);
330:   PetscSectionSetChart(coordSection, 0, numVertices);
331:   PetscSectionSetFieldComponents(coordSection, 0, cdim);
332:   for (v = 0; v < numVertices; ++v) {
333:     PetscSectionSetDof(coordSection, v, cdim);
334:     PetscSectionSetFieldDof(coordSection, v, 0, cdim);
335:   }
336:   PetscSectionSetUp(coordSection);
337:   PetscSectionGetStorageSize(coordSection, &coordSize);
338:   VecCreate(PETSC_COMM_SELF, &coordinates);
339:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
340:   VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
341:   VecSetBlockSize(coordinates, cdim);
342:   VecSetType(coordinates,VECSTANDARD);
343:   VecGetArray(coordinates, &coords);
344:   coords[0] = lower[0];
345:   coords[1] = upper[0];
346:   VecRestoreArray(coordinates, &coords);
347:   DMSetCoordinatesLocal(dm, coordinates);
348:   VecDestroy(&coordinates);
349:   return(0);
350: }

352: static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
353: {
354:   const PetscInt numVertices    = (edges[0]+1)*(edges[1]+1);
355:   const PetscInt numEdges       = edges[0]*(edges[1]+1) + (edges[0]+1)*edges[1];
356:   PetscInt       markerTop      = 1;
357:   PetscInt       markerBottom   = 1;
358:   PetscInt       markerRight    = 1;
359:   PetscInt       markerLeft     = 1;
360:   PetscBool      markerSeparate = PETSC_FALSE;
361:   Vec            coordinates;
362:   PetscSection   coordSection;
363:   PetscScalar    *coords;
364:   PetscInt       coordSize;
365:   PetscMPIInt    rank;
366:   PetscInt       v, vx, vy;

370:   PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);
371:   if (markerSeparate) {
372:     markerTop    = 3;
373:     markerBottom = 1;
374:     markerRight  = 2;
375:     markerLeft   = 4;
376:   }
377:   MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
378:   if (!rank) {
379:     PetscInt e, ex, ey;

381:     DMPlexSetChart(dm, 0, numEdges+numVertices);
382:     for (e = 0; e < numEdges; ++e) {
383:       DMPlexSetConeSize(dm, e, 2);
384:     }
385:     DMSetUp(dm); /* Allocate space for cones */
386:     for (vx = 0; vx <= edges[0]; vx++) {
387:       for (ey = 0; ey < edges[1]; ey++) {
388:         PetscInt edge   = vx*edges[1] + ey + edges[0]*(edges[1]+1);
389:         PetscInt vertex = ey*(edges[0]+1) + vx + numEdges;
390:         PetscInt cone[2];

392:         cone[0] = vertex; cone[1] = vertex+edges[0]+1;
393:         DMPlexSetCone(dm, edge, cone);
394:         if (vx == edges[0]) {
395:           DMSetLabelValue(dm, "marker", edge,    markerRight);
396:           DMSetLabelValue(dm, "marker", cone[0], markerRight);
397:           if (ey == edges[1]-1) {
398:             DMSetLabelValue(dm, "marker", cone[1], markerRight);
399:             DMSetLabelValue(dm, "Face Sets", cone[1], markerRight);
400:           }
401:         } else if (vx == 0) {
402:           DMSetLabelValue(dm, "marker", edge,    markerLeft);
403:           DMSetLabelValue(dm, "marker", cone[0], markerLeft);
404:           if (ey == edges[1]-1) {
405:             DMSetLabelValue(dm, "marker", cone[1], markerLeft);
406:             DMSetLabelValue(dm, "Face Sets", cone[1], markerLeft);
407:           }
408:         }
409:       }
410:     }
411:     for (vy = 0; vy <= edges[1]; vy++) {
412:       for (ex = 0; ex < edges[0]; ex++) {
413:         PetscInt edge   = vy*edges[0]     + ex;
414:         PetscInt vertex = vy*(edges[0]+1) + ex + numEdges;
415:         PetscInt cone[2];

417:         cone[0] = vertex; cone[1] = vertex+1;
418:         DMPlexSetCone(dm, edge, cone);
419:         if (vy == edges[1]) {
420:           DMSetLabelValue(dm, "marker", edge,    markerTop);
421:           DMSetLabelValue(dm, "marker", cone[0], markerTop);
422:           if (ex == edges[0]-1) {
423:             DMSetLabelValue(dm, "marker", cone[1], markerTop);
424:             DMSetLabelValue(dm, "Face Sets", cone[1], markerTop);
425:           }
426:         } else if (vy == 0) {
427:           DMSetLabelValue(dm, "marker", edge,    markerBottom);
428:           DMSetLabelValue(dm, "marker", cone[0], markerBottom);
429:           if (ex == edges[0]-1) {
430:             DMSetLabelValue(dm, "marker", cone[1], markerBottom);
431:             DMSetLabelValue(dm, "Face Sets", cone[1], markerBottom);
432:           }
433:         }
434:       }
435:     }
436:   }
437:   DMPlexSymmetrize(dm);
438:   DMPlexStratify(dm);
439:   /* Build coordinates */
440:   DMSetCoordinateDim(dm, 2);
441:   DMGetCoordinateSection(dm, &coordSection);
442:   PetscSectionSetNumFields(coordSection, 1);
443:   PetscSectionSetChart(coordSection, numEdges, numEdges + numVertices);
444:   PetscSectionSetFieldComponents(coordSection, 0, 2);
445:   for (v = numEdges; v < numEdges+numVertices; ++v) {
446:     PetscSectionSetDof(coordSection, v, 2);
447:     PetscSectionSetFieldDof(coordSection, v, 0, 2);
448:   }
449:   PetscSectionSetUp(coordSection);
450:   PetscSectionGetStorageSize(coordSection, &coordSize);
451:   VecCreate(PETSC_COMM_SELF, &coordinates);
452:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
453:   VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
454:   VecSetBlockSize(coordinates, 2);
455:   VecSetType(coordinates,VECSTANDARD);
456:   VecGetArray(coordinates, &coords);
457:   for (vy = 0; vy <= edges[1]; ++vy) {
458:     for (vx = 0; vx <= edges[0]; ++vx) {
459:       coords[(vy*(edges[0]+1)+vx)*2+0] = lower[0] + ((upper[0] - lower[0])/edges[0])*vx;
460:       coords[(vy*(edges[0]+1)+vx)*2+1] = lower[1] + ((upper[1] - lower[1])/edges[1])*vy;
461:     }
462:   }
463:   VecRestoreArray(coordinates, &coords);
464:   DMSetCoordinatesLocal(dm, coordinates);
465:   VecDestroy(&coordinates);
466:   return(0);
467: }

469: static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt faces[])
470: {
471:   PetscInt       vertices[3], numVertices;
472:   PetscInt       numFaces    = 2*faces[0]*faces[1] + 2*faces[1]*faces[2] + 2*faces[0]*faces[2];
473:   Vec            coordinates;
474:   PetscSection   coordSection;
475:   PetscScalar    *coords;
476:   PetscInt       coordSize;
477:   PetscMPIInt    rank;
478:   PetscInt       v, vx, vy, vz;
479:   PetscInt       voffset, iface=0, cone[4];

483:   if ((faces[0] < 1) || (faces[1] < 1) || (faces[2] < 1)) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Must have at least 1 face per side");
484:   MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
485:   vertices[0] = faces[0]+1; vertices[1] = faces[1]+1; vertices[2] = faces[2]+1;
486:   numVertices = vertices[0]*vertices[1]*vertices[2];
487:   if (!rank) {
488:     PetscInt f;

490:     DMPlexSetChart(dm, 0, numFaces+numVertices);
491:     for (f = 0; f < numFaces; ++f) {
492:       DMPlexSetConeSize(dm, f, 4);
493:     }
494:     DMSetUp(dm); /* Allocate space for cones */

496:     /* Side 0 (Top) */
497:     for (vy = 0; vy < faces[1]; vy++) {
498:       for (vx = 0; vx < faces[0]; vx++) {
499:         voffset = numFaces + vertices[0]*vertices[1]*(vertices[2]-1) + vy*vertices[0] + vx;
500:         cone[0] = voffset; cone[1] = voffset+1; cone[2] = voffset+vertices[0]+1; cone[3] = voffset+vertices[0];
501:         DMPlexSetCone(dm, iface, cone);
502:         DMSetLabelValue(dm, "marker", iface, 1);
503:         DMSetLabelValue(dm, "marker", voffset+0, 1);
504:         DMSetLabelValue(dm, "marker", voffset+1, 1);
505:         DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, 1);
506:         DMSetLabelValue(dm, "marker", voffset+vertices[0]+1, 1);
507:         iface++;
508:       }
509:     }

511:     /* Side 1 (Bottom) */
512:     for (vy = 0; vy < faces[1]; vy++) {
513:       for (vx = 0; vx < faces[0]; vx++) {
514:         voffset = numFaces + vy*(faces[0]+1) + vx;
515:         cone[0] = voffset+1; cone[1] = voffset; cone[2] = voffset+vertices[0]; cone[3] = voffset+vertices[0]+1;
516:         DMPlexSetCone(dm, iface, cone);
517:         DMSetLabelValue(dm, "marker", iface, 1);
518:         DMSetLabelValue(dm, "marker", voffset+0, 1);
519:         DMSetLabelValue(dm, "marker", voffset+1, 1);
520:         DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, 1);
521:         DMSetLabelValue(dm, "marker", voffset+vertices[0]+1, 1);
522:         iface++;
523:       }
524:     }

526:     /* Side 2 (Front) */
527:     for (vz = 0; vz < faces[2]; vz++) {
528:       for (vx = 0; vx < faces[0]; vx++) {
529:         voffset = numFaces + vz*vertices[0]*vertices[1] + vx;
530:         cone[0] = voffset; cone[1] = voffset+1; cone[2] = voffset+vertices[0]*vertices[1]+1; cone[3] = voffset+vertices[0]*vertices[1];
531:         DMPlexSetCone(dm, iface, cone);
532:         DMSetLabelValue(dm, "marker", iface, 1);
533:         DMSetLabelValue(dm, "marker", voffset+0, 1);
534:         DMSetLabelValue(dm, "marker", voffset+1, 1);
535:         DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+0, 1);
536:         DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+1, 1);
537:         iface++;
538:       }
539:     }

541:     /* Side 3 (Back) */
542:     for (vz = 0; vz < faces[2]; vz++) {
543:       for (vx = 0; vx < faces[0]; vx++) {
544:         voffset = numFaces + vz*vertices[0]*vertices[1] + vertices[0]*(vertices[1]-1) + vx;
545:         cone[0] = voffset+vertices[0]*vertices[1]; cone[1] = voffset+vertices[0]*vertices[1]+1;
546:         cone[2] = voffset+1; cone[3] = voffset;
547:         DMPlexSetCone(dm, iface, cone);
548:         DMSetLabelValue(dm, "marker", iface, 1);
549:         DMSetLabelValue(dm, "marker", voffset+0, 1);
550:         DMSetLabelValue(dm, "marker", voffset+1, 1);
551:         DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+0, 1);
552:         DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+1, 1);
553:         iface++;
554:       }
555:     }

557:     /* Side 4 (Left) */
558:     for (vz = 0; vz < faces[2]; vz++) {
559:       for (vy = 0; vy < faces[1]; vy++) {
560:         voffset = numFaces + vz*vertices[0]*vertices[1] + vy*vertices[0];
561:         cone[0] = voffset; cone[1] = voffset+vertices[0]*vertices[1];
562:         cone[2] = voffset+vertices[0]*vertices[1]+vertices[0]; cone[3] = voffset+vertices[0];
563:         DMPlexSetCone(dm, iface, cone);
564:         DMSetLabelValue(dm, "marker", iface, 1);
565:         DMSetLabelValue(dm, "marker", voffset+0, 1);
566:         DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, 1);
567:         DMSetLabelValue(dm, "marker", voffset+vertices[1]+0, 1);
568:         DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+vertices[0], 1);
569:         iface++;
570:       }
571:     }

573:     /* Side 5 (Right) */
574:     for (vz = 0; vz < faces[2]; vz++) {
575:       for (vy = 0; vy < faces[1]; vy++) {
576:         voffset = numFaces + vz*vertices[0]*vertices[1] + vy*vertices[0] + faces[0];
577:         cone[0] = voffset+vertices[0]*vertices[1]; cone[1] = voffset;
578:         cone[2] = voffset+vertices[0]; cone[3] = voffset+vertices[0]*vertices[1]+vertices[0];
579:         DMPlexSetCone(dm, iface, cone);
580:         DMSetLabelValue(dm, "marker", iface, 1);
581:         DMSetLabelValue(dm, "marker", voffset+0, 1);
582:         DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, 1);
583:         DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+0, 1);
584:         DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+vertices[0], 1);
585:         iface++;
586:       }
587:     }
588:   }
589:   DMPlexSymmetrize(dm);
590:   DMPlexStratify(dm);
591:   /* Build coordinates */
592:   DMSetCoordinateDim(dm, 3);
593:   DMGetCoordinateSection(dm, &coordSection);
594:   PetscSectionSetNumFields(coordSection, 1);
595:   PetscSectionSetChart(coordSection, numFaces, numFaces + numVertices);
596:   PetscSectionSetFieldComponents(coordSection, 0, 3);
597:   for (v = numFaces; v < numFaces+numVertices; ++v) {
598:     PetscSectionSetDof(coordSection, v, 3);
599:     PetscSectionSetFieldDof(coordSection, v, 0, 3);
600:   }
601:   PetscSectionSetUp(coordSection);
602:   PetscSectionGetStorageSize(coordSection, &coordSize);
603:   VecCreate(PETSC_COMM_SELF, &coordinates);
604:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
605:   VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
606:   VecSetBlockSize(coordinates, 3);
607:   VecSetType(coordinates,VECSTANDARD);
608:   VecGetArray(coordinates, &coords);
609:   for (vz = 0; vz <= faces[2]; ++vz) {
610:     for (vy = 0; vy <= faces[1]; ++vy) {
611:       for (vx = 0; vx <= faces[0]; ++vx) {
612:         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+0] = lower[0] + ((upper[0] - lower[0])/faces[0])*vx;
613:         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+1] = lower[1] + ((upper[1] - lower[1])/faces[1])*vy;
614:         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+2] = lower[2] + ((upper[2] - lower[2])/faces[2])*vz;
615:       }
616:     }
617:   }
618:   VecRestoreArray(coordinates, &coords);
619:   DMSetCoordinatesLocal(dm, coordinates);
620:   VecDestroy(&coordinates);
621:   return(0);
622: }

624: static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate)
625: {

630:   DMSetDimension(dm, dim-1);
631:   DMSetCoordinateDim(dm, dim);
632:   switch (dim) {
633:     case 1: DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(dm, lower, upper, faces);break;
634:     case 2: DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(dm, lower, upper, faces);break;
635:     case 3: DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(dm, lower, upper, faces);break;
636:     default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Dimension not supported: %D", dim);
637:   }
638:   if (interpolate) {DMPlexInterpolateInPlace_Internal(dm);}
639:   return(0);
640: }

642: /*@C
643:   DMPlexCreateBoxSurfaceMesh - Creates a mesh on the surface of the tensor product of unit intervals (box) using tensor cells (hexahedra).

645:   Collective

647:   Input Parameters:
648: + comm        - The communicator for the DM object
649: . dim         - The spatial dimension of the box, so the resulting mesh is has dimension dim-1
650: . faces       - Number of faces per dimension, or NULL for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
651: . lower       - The lower left corner, or NULL for (0, 0, 0)
652: . upper       - The upper right corner, or NULL for (1, 1, 1)
653: - interpolate - Flag to create intermediate mesh pieces (edges, faces)

655:   Output Parameter:
656: . dm  - The DM object

658:   Level: beginner

660: .seealso: DMSetFromOptions(), DMPlexCreateBoxMesh(), DMPlexCreateFromFile(), DMSetType(), DMCreate()
661: @*/
662: PetscErrorCode DMPlexCreateBoxSurfaceMesh(MPI_Comm comm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate, DM *dm)
663: {
664:   PetscInt       fac[3] = {1, 1, 1};
665:   PetscReal      low[3] = {0, 0, 0};
666:   PetscReal      upp[3] = {1, 1, 1};

670:   DMCreate(comm,dm);
671:   DMSetType(*dm,DMPLEX);
672:   DMPlexCreateBoxSurfaceMesh_Internal(*dm, dim, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, interpolate);
673:   return(0);
674: }

676: static PetscErrorCode DMPlexCreateLineMesh_Internal(DM dm,PetscInt segments,PetscReal lower,PetscReal upper,DMBoundaryType bd)
677: {
678:   PetscInt       i,fStart,fEnd,numCells = 0,numVerts = 0;
679:   PetscInt       numPoints[2],*coneSize,*cones,*coneOrientations;
680:   PetscScalar    *vertexCoords;
681:   PetscReal      L,maxCell;
682:   PetscBool      markerSeparate = PETSC_FALSE;
683:   PetscInt       markerLeft  = 1, faceMarkerLeft  = 1;
684:   PetscInt       markerRight = 1, faceMarkerRight = 2;
685:   PetscBool      wrap = (bd == DM_BOUNDARY_PERIODIC || bd == DM_BOUNDARY_TWIST) ? PETSC_TRUE : PETSC_FALSE;
686:   PetscMPIInt    rank;


692:   DMSetDimension(dm,1);
693:   DMCreateLabel(dm,"marker");
694:   DMCreateLabel(dm,"Face Sets");

696:   MPI_Comm_rank(PetscObjectComm((PetscObject) dm),&rank);
697:   if (!rank) numCells = segments;
698:   if (!rank) numVerts = segments + (wrap ? 0 : 1);

700:   numPoints[0] = numVerts ; numPoints[1] = numCells;
701:   PetscMalloc4(numCells+numVerts,&coneSize,numCells*2,&cones,numCells+numVerts,&coneOrientations,numVerts,&vertexCoords);
702:   PetscArrayzero(coneOrientations,numCells+numVerts);
703:   for (i = 0; i < numCells; ++i) { coneSize[i] = 2; }
704:   for (i = 0; i < numVerts; ++i) { coneSize[numCells+i] = 0; }
705:   for (i = 0; i < numCells; ++i) { cones[2*i] = numCells + i%numVerts; cones[2*i+1] = numCells + (i+1)%numVerts; }
706:   for (i = 0; i < numVerts; ++i) { vertexCoords[i] = lower + (upper-lower)*((PetscReal)i/(PetscReal)numCells); }
707:   DMPlexCreateFromDAG(dm,1,numPoints,coneSize,cones,coneOrientations,vertexCoords);
708:   PetscFree4(coneSize,cones,coneOrientations,vertexCoords);

710:   PetscOptionsGetBool(((PetscObject)dm)->options,((PetscObject)dm)->prefix,"-dm_plex_separate_marker",&markerSeparate,NULL);
711:   if (markerSeparate) { markerLeft = faceMarkerLeft; markerRight = faceMarkerRight;}
712:   if (!wrap && !rank) {
713:     DMPlexGetHeightStratum(dm,1,&fStart,&fEnd);
714:     DMSetLabelValue(dm,"marker",fStart,markerLeft);
715:     DMSetLabelValue(dm,"marker",fEnd-1,markerRight);
716:     DMSetLabelValue(dm,"Face Sets",fStart,faceMarkerLeft);
717:     DMSetLabelValue(dm,"Face Sets",fEnd-1,faceMarkerRight);
718:   }
719:   if (wrap) {
720:     L       = upper - lower;
721:     maxCell = (PetscReal)1.1*(L/(PetscReal)PetscMax(1,segments));
722:     DMSetPeriodicity(dm,PETSC_TRUE,&maxCell,&L,&bd);
723:   }
724:   DMPlexSetRefinementUniform(dm, PETSC_TRUE);
725:   return(0);
726: }

728: static PetscErrorCode DMPlexCreateBoxMesh_Simplex_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate)
729: {
730:   DM             boundary, vol;
731:   PetscInt       i;

736:   for (i = 0; i < dim; ++i) if (periodicity[i] != DM_BOUNDARY_NONE) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Periodicity is not supported for simplex meshes");
737:   DMCreate(PetscObjectComm((PetscObject) dm), &boundary);
738:   DMSetType(boundary, DMPLEX);
739:   DMPlexCreateBoxSurfaceMesh_Internal(boundary, dim, faces, lower, upper, PETSC_FALSE);
740:   DMPlexGenerate(boundary, NULL, interpolate, &vol);
741:   DMPlexReplace_Static(dm, &vol);
742:   DMDestroy(&boundary);
743:   return(0);
744: }

746: static PetscErrorCode DMPlexCreateCubeMesh_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY, DMBoundaryType bdZ)
747: {
748:   DMLabel        cutLabel = NULL;
749:   PetscInt       markerTop      = 1, faceMarkerTop      = 1;
750:   PetscInt       markerBottom   = 1, faceMarkerBottom   = 1;
751:   PetscInt       markerFront    = 1, faceMarkerFront    = 1;
752:   PetscInt       markerBack     = 1, faceMarkerBack     = 1;
753:   PetscInt       markerRight    = 1, faceMarkerRight    = 1;
754:   PetscInt       markerLeft     = 1, faceMarkerLeft     = 1;
755:   PetscInt       dim;
756:   PetscBool      markerSeparate = PETSC_FALSE, cutMarker = PETSC_FALSE;
757:   PetscMPIInt    rank;

761:   DMGetDimension(dm,&dim);
762:   MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
763:   DMCreateLabel(dm,"marker");
764:   DMCreateLabel(dm,"Face Sets");
765:   PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL);
766:   if (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ||
767:       bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ||
768:       bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST) {

770:     if (cutMarker) {DMCreateLabel(dm, "periodic_cut"); DMGetLabel(dm, "periodic_cut", &cutLabel);}
771:   }
772:   switch (dim) {
773:   case 2:
774:     faceMarkerTop    = 3;
775:     faceMarkerBottom = 1;
776:     faceMarkerRight  = 2;
777:     faceMarkerLeft   = 4;
778:     break;
779:   case 3:
780:     faceMarkerBottom = 1;
781:     faceMarkerTop    = 2;
782:     faceMarkerFront  = 3;
783:     faceMarkerBack   = 4;
784:     faceMarkerRight  = 5;
785:     faceMarkerLeft   = 6;
786:     break;
787:   default:
788:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Dimension %D not supported",dim);
789:   }
790:   PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);
791:   if (markerSeparate) {
792:     markerBottom = faceMarkerBottom;
793:     markerTop    = faceMarkerTop;
794:     markerFront  = faceMarkerFront;
795:     markerBack   = faceMarkerBack;
796:     markerRight  = faceMarkerRight;
797:     markerLeft   = faceMarkerLeft;
798:   }
799:   {
800:     const PetscInt numXEdges    = !rank ? edges[0] : 0;
801:     const PetscInt numYEdges    = !rank ? edges[1] : 0;
802:     const PetscInt numZEdges    = !rank ? edges[2] : 0;
803:     const PetscInt numXVertices = !rank ? (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ? edges[0] : edges[0]+1) : 0;
804:     const PetscInt numYVertices = !rank ? (bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ? edges[1] : edges[1]+1) : 0;
805:     const PetscInt numZVertices = !rank ? (bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST ? edges[2] : edges[2]+1) : 0;
806:     const PetscInt numCells     = numXEdges*numYEdges*numZEdges;
807:     const PetscInt numXFaces    = numYEdges*numZEdges;
808:     const PetscInt numYFaces    = numXEdges*numZEdges;
809:     const PetscInt numZFaces    = numXEdges*numYEdges;
810:     const PetscInt numTotXFaces = numXVertices*numXFaces;
811:     const PetscInt numTotYFaces = numYVertices*numYFaces;
812:     const PetscInt numTotZFaces = numZVertices*numZFaces;
813:     const PetscInt numFaces     = numTotXFaces + numTotYFaces + numTotZFaces;
814:     const PetscInt numTotXEdges = numXEdges*numYVertices*numZVertices;
815:     const PetscInt numTotYEdges = numYEdges*numXVertices*numZVertices;
816:     const PetscInt numTotZEdges = numZEdges*numXVertices*numYVertices;
817:     const PetscInt numVertices  = numXVertices*numYVertices*numZVertices;
818:     const PetscInt numEdges     = numTotXEdges + numTotYEdges + numTotZEdges;
819:     const PetscInt firstVertex  = (dim == 2) ? numFaces : numCells;
820:     const PetscInt firstXFace   = (dim == 2) ? 0 : numCells + numVertices;
821:     const PetscInt firstYFace   = firstXFace + numTotXFaces;
822:     const PetscInt firstZFace   = firstYFace + numTotYFaces;
823:     const PetscInt firstXEdge   = numCells + numFaces + numVertices;
824:     const PetscInt firstYEdge   = firstXEdge + numTotXEdges;
825:     const PetscInt firstZEdge   = firstYEdge + numTotYEdges;
826:     Vec            coordinates;
827:     PetscSection   coordSection;
828:     PetscScalar   *coords;
829:     PetscInt       coordSize;
830:     PetscInt       v, vx, vy, vz;
831:     PetscInt       c, f, fx, fy, fz, e, ex, ey, ez;

833:     DMPlexSetChart(dm, 0, numCells+numFaces+numEdges+numVertices);
834:     for (c = 0; c < numCells; c++) {
835:       DMPlexSetConeSize(dm, c, 6);
836:     }
837:     for (f = firstXFace; f < firstXFace+numFaces; ++f) {
838:       DMPlexSetConeSize(dm, f, 4);
839:     }
840:     for (e = firstXEdge; e < firstXEdge+numEdges; ++e) {
841:       DMPlexSetConeSize(dm, e, 2);
842:     }
843:     DMSetUp(dm); /* Allocate space for cones */
844:     /* Build cells */
845:     for (fz = 0; fz < numZEdges; ++fz) {
846:       for (fy = 0; fy < numYEdges; ++fy) {
847:         for (fx = 0; fx < numXEdges; ++fx) {
848:           PetscInt cell    = (fz*numYEdges + fy)*numXEdges + fx;
849:           PetscInt faceB   = firstZFace + (fy*numXEdges+fx)*numZVertices +   fz;
850:           PetscInt faceT   = firstZFace + (fy*numXEdges+fx)*numZVertices + ((fz+1)%numZVertices);
851:           PetscInt faceF   = firstYFace + (fz*numXEdges+fx)*numYVertices +   fy;
852:           PetscInt faceK   = firstYFace + (fz*numXEdges+fx)*numYVertices + ((fy+1)%numYVertices);
853:           PetscInt faceL   = firstXFace + (fz*numYEdges+fy)*numXVertices +   fx;
854:           PetscInt faceR   = firstXFace + (fz*numYEdges+fy)*numXVertices + ((fx+1)%numXVertices);
855:                             /* B,  T,  F,  K,  R,  L */
856:           PetscInt ornt[6] = {-4,  0,  0, -1,  0, -4}; /* ??? */
857:           PetscInt cone[6];

859:           /* no boundary twisting in 3D */
860:           cone[0] = faceB; cone[1] = faceT; cone[2] = faceF; cone[3] = faceK; cone[4] = faceR; cone[5] = faceL;
861:           DMPlexSetCone(dm, cell, cone);
862:           DMPlexSetConeOrientation(dm, cell, ornt);
863:           if (bdX != DM_BOUNDARY_NONE && fx == numXEdges-1 && cutLabel) {DMLabelSetValue(cutLabel, cell, 2);}
864:           if (bdY != DM_BOUNDARY_NONE && fy == numYEdges-1 && cutLabel) {DMLabelSetValue(cutLabel, cell, 2);}
865:           if (bdZ != DM_BOUNDARY_NONE && fz == numZEdges-1 && cutLabel) {DMLabelSetValue(cutLabel, cell, 2);}
866:         }
867:       }
868:     }
869:     /* Build x faces */
870:     for (fz = 0; fz < numZEdges; ++fz) {
871:       for (fy = 0; fy < numYEdges; ++fy) {
872:         for (fx = 0; fx < numXVertices; ++fx) {
873:           PetscInt face    = firstXFace + (fz*numYEdges+fy)     *numXVertices+fx;
874:           PetscInt edgeL   = firstZEdge + (fy                   *numXVertices+fx)*numZEdges + fz;
875:           PetscInt edgeR   = firstZEdge + (((fy+1)%numYVertices)*numXVertices+fx)*numZEdges + fz;
876:           PetscInt edgeB   = firstYEdge + (fz                   *numXVertices+fx)*numYEdges + fy;
877:           PetscInt edgeT   = firstYEdge + (((fz+1)%numZVertices)*numXVertices+fx)*numYEdges + fy;
878:           PetscInt ornt[4] = {0, 0, -2, -2};
879:           PetscInt cone[4];

881:           if (dim == 3) {
882:             /* markers */
883:             if (bdX != DM_BOUNDARY_PERIODIC) {
884:               if (fx == numXVertices-1) {
885:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerRight);
886:                 DMSetLabelValue(dm, "marker", face, markerRight);
887:               }
888:               else if (fx == 0) {
889:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerLeft);
890:                 DMSetLabelValue(dm, "marker", face, markerLeft);
891:               }
892:             }
893:           }
894:           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
895:           DMPlexSetCone(dm, face, cone);
896:           DMPlexSetConeOrientation(dm, face, ornt);
897:         }
898:       }
899:     }
900:     /* Build y faces */
901:     for (fz = 0; fz < numZEdges; ++fz) {
902:       for (fx = 0; fx < numXEdges; ++fx) {
903:         for (fy = 0; fy < numYVertices; ++fy) {
904:           PetscInt face    = firstYFace + (fz*numXEdges+fx)*numYVertices + fy;
905:           PetscInt edgeL   = firstZEdge + (fy*numXVertices+  fx)*numZEdges + fz;
906:           PetscInt edgeR   = firstZEdge + (fy*numXVertices+((fx+1)%numXVertices))*numZEdges + fz;
907:           PetscInt edgeB   = firstXEdge + (fz                   *numYVertices+fy)*numXEdges + fx;
908:           PetscInt edgeT   = firstXEdge + (((fz+1)%numZVertices)*numYVertices+fy)*numXEdges + fx;
909:           PetscInt ornt[4] = {0, 0, -2, -2};
910:           PetscInt cone[4];

912:           if (dim == 3) {
913:             /* markers */
914:             if (bdY != DM_BOUNDARY_PERIODIC) {
915:               if (fy == numYVertices-1) {
916:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerBack);
917:                 DMSetLabelValue(dm, "marker", face, markerBack);
918:               }
919:               else if (fy == 0) {
920:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerFront);
921:                 DMSetLabelValue(dm, "marker", face, markerFront);
922:               }
923:             }
924:           }
925:           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
926:           DMPlexSetCone(dm, face, cone);
927:           DMPlexSetConeOrientation(dm, face, ornt);
928:         }
929:       }
930:     }
931:     /* Build z faces */
932:     for (fy = 0; fy < numYEdges; ++fy) {
933:       for (fx = 0; fx < numXEdges; ++fx) {
934:         for (fz = 0; fz < numZVertices; fz++) {
935:           PetscInt face    = firstZFace + (fy*numXEdges+fx)*numZVertices + fz;
936:           PetscInt edgeL   = firstYEdge + (fz*numXVertices+  fx)*numYEdges + fy;
937:           PetscInt edgeR   = firstYEdge + (fz*numXVertices+((fx+1)%numXVertices))*numYEdges + fy;
938:           PetscInt edgeB   = firstXEdge + (fz*numYVertices+  fy)*numXEdges + fx;
939:           PetscInt edgeT   = firstXEdge + (fz*numYVertices+((fy+1)%numYVertices))*numXEdges + fx;
940:           PetscInt ornt[4] = {0, 0, -2, -2};
941:           PetscInt cone[4];

943:           if (dim == 2) {
944:             if (bdX == DM_BOUNDARY_TWIST && fx == numXEdges-1) {edgeR += numYEdges-1-2*fy; ornt[1] = -2;}
945:             if (bdY == DM_BOUNDARY_TWIST && fy == numYEdges-1) {edgeT += numXEdges-1-2*fx; ornt[2] =  0;}
946:             if (bdX != DM_BOUNDARY_NONE && fx == numXEdges-1 && cutLabel) {DMLabelSetValue(cutLabel, face, 2);}
947:             if (bdY != DM_BOUNDARY_NONE && fy == numYEdges-1 && cutLabel) {DMLabelSetValue(cutLabel, face, 2);}
948:           } else {
949:             /* markers */
950:             if (bdZ != DM_BOUNDARY_PERIODIC) {
951:               if (fz == numZVertices-1) {
952:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerTop);
953:                 DMSetLabelValue(dm, "marker", face, markerTop);
954:               }
955:               else if (fz == 0) {
956:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerBottom);
957:                 DMSetLabelValue(dm, "marker", face, markerBottom);
958:               }
959:             }
960:           }
961:           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
962:           DMPlexSetCone(dm, face, cone);
963:           DMPlexSetConeOrientation(dm, face, ornt);
964:         }
965:       }
966:     }
967:     /* Build Z edges*/
968:     for (vy = 0; vy < numYVertices; vy++) {
969:       for (vx = 0; vx < numXVertices; vx++) {
970:         for (ez = 0; ez < numZEdges; ez++) {
971:           const PetscInt edge    = firstZEdge  + (vy*numXVertices+vx)*numZEdges + ez;
972:           const PetscInt vertexB = firstVertex + (ez                   *numYVertices+vy)*numXVertices + vx;
973:           const PetscInt vertexT = firstVertex + (((ez+1)%numZVertices)*numYVertices+vy)*numXVertices + vx;
974:           PetscInt       cone[2];

976:           if (dim == 3) {
977:             if (bdX != DM_BOUNDARY_PERIODIC) {
978:               if (vx == numXVertices-1) {
979:                 DMSetLabelValue(dm, "marker", edge, markerRight);
980:               }
981:               else if (vx == 0) {
982:                 DMSetLabelValue(dm, "marker", edge, markerLeft);
983:               }
984:             }
985:             if (bdY != DM_BOUNDARY_PERIODIC) {
986:               if (vy == numYVertices-1) {
987:                 DMSetLabelValue(dm, "marker", edge, markerBack);
988:               }
989:               else if (vy == 0) {
990:                 DMSetLabelValue(dm, "marker", edge, markerFront);
991:               }
992:             }
993:           }
994:           cone[0] = vertexB; cone[1] = vertexT;
995:           DMPlexSetCone(dm, edge, cone);
996:         }
997:       }
998:     }
999:     /* Build Y edges*/
1000:     for (vz = 0; vz < numZVertices; vz++) {
1001:       for (vx = 0; vx < numXVertices; vx++) {
1002:         for (ey = 0; ey < numYEdges; ey++) {
1003:           const PetscInt nextv   = (dim == 2 && bdY == DM_BOUNDARY_TWIST && ey == numYEdges-1) ? (numXVertices-vx-1) : (vz*numYVertices+((ey+1)%numYVertices))*numXVertices + vx;
1004:           const PetscInt edge    = firstYEdge  + (vz*numXVertices+vx)*numYEdges + ey;
1005:           const PetscInt vertexF = firstVertex + (vz*numYVertices+ey)*numXVertices + vx;
1006:           const PetscInt vertexK = firstVertex + nextv;
1007:           PetscInt       cone[2];

1009:           cone[0] = vertexF; cone[1] = vertexK;
1010:           DMPlexSetCone(dm, edge, cone);
1011:           if (dim == 2) {
1012:             if ((bdX != DM_BOUNDARY_PERIODIC) && (bdX != DM_BOUNDARY_TWIST)) {
1013:               if (vx == numXVertices-1) {
1014:                 DMSetLabelValue(dm, "Face Sets", edge, faceMarkerRight);
1015:                 DMSetLabelValue(dm, "marker", edge,    markerRight);
1016:                 DMSetLabelValue(dm, "marker", cone[0], markerRight);
1017:                 if (ey == numYEdges-1) {
1018:                   DMSetLabelValue(dm, "marker", cone[1], markerRight);
1019:                 }
1020:               } else if (vx == 0) {
1021:                 DMSetLabelValue(dm, "Face Sets", edge, faceMarkerLeft);
1022:                 DMSetLabelValue(dm, "marker", edge,    markerLeft);
1023:                 DMSetLabelValue(dm, "marker", cone[0], markerLeft);
1024:                 if (ey == numYEdges-1) {
1025:                   DMSetLabelValue(dm, "marker", cone[1], markerLeft);
1026:                 }
1027:               }
1028:             } else {
1029:               if (vx == 0 && cutLabel) {
1030:                 DMLabelSetValue(cutLabel, edge,    1);
1031:                 DMLabelSetValue(cutLabel, cone[0], 1);
1032:                 if (ey == numYEdges-1) {
1033:                   DMLabelSetValue(cutLabel, cone[1], 1);
1034:                 }
1035:               }
1036:             }
1037:           } else {
1038:             if (bdX != DM_BOUNDARY_PERIODIC) {
1039:               if (vx == numXVertices-1) {
1040:                 DMSetLabelValue(dm, "marker", edge, markerRight);
1041:               } else if (vx == 0) {
1042:                 DMSetLabelValue(dm, "marker", edge, markerLeft);
1043:               }
1044:             }
1045:             if (bdZ != DM_BOUNDARY_PERIODIC) {
1046:               if (vz == numZVertices-1) {
1047:                 DMSetLabelValue(dm, "marker", edge, markerTop);
1048:               } else if (vz == 0) {
1049:                 DMSetLabelValue(dm, "marker", edge, markerBottom);
1050:               }
1051:             }
1052:           }
1053:         }
1054:       }
1055:     }
1056:     /* Build X edges*/
1057:     for (vz = 0; vz < numZVertices; vz++) {
1058:       for (vy = 0; vy < numYVertices; vy++) {
1059:         for (ex = 0; ex < numXEdges; ex++) {
1060:           const PetscInt nextv   = (dim == 2 && bdX == DM_BOUNDARY_TWIST && ex == numXEdges-1) ? (numYVertices-vy-1)*numXVertices : (vz*numYVertices+vy)*numXVertices + (ex+1)%numXVertices;
1061:           const PetscInt edge    = firstXEdge  + (vz*numYVertices+vy)*numXEdges + ex;
1062:           const PetscInt vertexL = firstVertex + (vz*numYVertices+vy)*numXVertices + ex;
1063:           const PetscInt vertexR = firstVertex + nextv;
1064:           PetscInt       cone[2];

1066:           cone[0] = vertexL; cone[1] = vertexR;
1067:           DMPlexSetCone(dm, edge, cone);
1068:           if (dim == 2) {
1069:             if ((bdY != DM_BOUNDARY_PERIODIC) && (bdY != DM_BOUNDARY_TWIST)) {
1070:               if (vy == numYVertices-1) {
1071:                 DMSetLabelValue(dm, "Face Sets", edge, faceMarkerTop);
1072:                 DMSetLabelValue(dm, "marker", edge,    markerTop);
1073:                 DMSetLabelValue(dm, "marker", cone[0], markerTop);
1074:                 if (ex == numXEdges-1) {
1075:                   DMSetLabelValue(dm, "marker", cone[1], markerTop);
1076:                 }
1077:               } else if (vy == 0) {
1078:                 DMSetLabelValue(dm, "Face Sets", edge, faceMarkerBottom);
1079:                 DMSetLabelValue(dm, "marker", edge,    markerBottom);
1080:                 DMSetLabelValue(dm, "marker", cone[0], markerBottom);
1081:                 if (ex == numXEdges-1) {
1082:                   DMSetLabelValue(dm, "marker", cone[1], markerBottom);
1083:                 }
1084:               }
1085:             } else {
1086:               if (vy == 0 && cutLabel) {
1087:                 DMLabelSetValue(cutLabel, edge,    1);
1088:                 DMLabelSetValue(cutLabel, cone[0], 1);
1089:                 if (ex == numXEdges-1) {
1090:                   DMLabelSetValue(cutLabel, cone[1], 1);
1091:                 }
1092:               }
1093:             }
1094:           } else {
1095:             if (bdY != DM_BOUNDARY_PERIODIC) {
1096:               if (vy == numYVertices-1) {
1097:                 DMSetLabelValue(dm, "marker", edge, markerBack);
1098:               }
1099:               else if (vy == 0) {
1100:                 DMSetLabelValue(dm, "marker", edge, markerFront);
1101:               }
1102:             }
1103:             if (bdZ != DM_BOUNDARY_PERIODIC) {
1104:               if (vz == numZVertices-1) {
1105:                 DMSetLabelValue(dm, "marker", edge, markerTop);
1106:               }
1107:               else if (vz == 0) {
1108:                 DMSetLabelValue(dm, "marker", edge, markerBottom);
1109:               }
1110:             }
1111:           }
1112:         }
1113:       }
1114:     }
1115:     DMPlexSymmetrize(dm);
1116:     DMPlexStratify(dm);
1117:     /* Build coordinates */
1118:     DMGetCoordinateSection(dm, &coordSection);
1119:     PetscSectionSetNumFields(coordSection, 1);
1120:     PetscSectionSetFieldComponents(coordSection, 0, dim);
1121:     PetscSectionSetChart(coordSection, firstVertex, firstVertex+numVertices);
1122:     for (v = firstVertex; v < firstVertex+numVertices; ++v) {
1123:       PetscSectionSetDof(coordSection, v, dim);
1124:       PetscSectionSetFieldDof(coordSection, v, 0, dim);
1125:     }
1126:     PetscSectionSetUp(coordSection);
1127:     PetscSectionGetStorageSize(coordSection, &coordSize);
1128:     VecCreate(PETSC_COMM_SELF, &coordinates);
1129:     PetscObjectSetName((PetscObject) coordinates, "coordinates");
1130:     VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
1131:     VecSetBlockSize(coordinates, dim);
1132:     VecSetType(coordinates,VECSTANDARD);
1133:     VecGetArray(coordinates, &coords);
1134:     for (vz = 0; vz < numZVertices; ++vz) {
1135:       for (vy = 0; vy < numYVertices; ++vy) {
1136:         for (vx = 0; vx < numXVertices; ++vx) {
1137:           coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+0] = lower[0] + ((upper[0] - lower[0])/numXEdges)*vx;
1138:           coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+1] = lower[1] + ((upper[1] - lower[1])/numYEdges)*vy;
1139:           if (dim == 3) {
1140:             coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+2] = lower[2] + ((upper[2] - lower[2])/numZEdges)*vz;
1141:           }
1142:         }
1143:       }
1144:     }
1145:     VecRestoreArray(coordinates, &coords);
1146:     DMSetCoordinatesLocal(dm, coordinates);
1147:     VecDestroy(&coordinates);
1148:   }
1149:   return(0);
1150: }

1152: static PetscErrorCode DMPlexCreateBoxMesh_Tensor_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
1153: {
1154:   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
1155:   PetscInt       fac[3] = {0, 0, 0}, d;

1161:   DMSetDimension(dm, dim);
1162:   for (d = 0; d < dim; ++d) {fac[d] = faces[d]; bdt[d] = periodicity[d];}
1163:   DMPlexCreateCubeMesh_Internal(dm, lower, upper, fac, bdt[0], bdt[1], bdt[2]);
1164:   if (periodicity[0] == DM_BOUNDARY_PERIODIC || periodicity[0] == DM_BOUNDARY_TWIST ||
1165:       periodicity[1] == DM_BOUNDARY_PERIODIC || periodicity[1] == DM_BOUNDARY_TWIST ||
1166:       (dim > 2 && (periodicity[2] == DM_BOUNDARY_PERIODIC || periodicity[2] == DM_BOUNDARY_TWIST))) {
1167:     PetscReal L[3];
1168:     PetscReal maxCell[3];

1170:     for (d = 0; d < dim; ++d) {
1171:       L[d]       = upper[d] - lower[d];
1172:       maxCell[d] = 1.1 * (L[d] / PetscMax(1, faces[d]));
1173:     }
1174:     DMSetPeriodicity(dm, PETSC_TRUE, maxCell, L, periodicity);
1175:   }
1176:   DMPlexSetRefinementUniform(dm, PETSC_TRUE);
1177:   return(0);
1178: }

1180: static PetscErrorCode DMPlexCreateBoxMesh_Internal(DM dm, PetscInt dim, PetscBool simplex, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate)
1181: {

1185:   if (dim == 1)      {DMPlexCreateLineMesh_Internal(dm, faces[0], lower[0], upper[0], periodicity[0]);}
1186:   else if (simplex)  {DMPlexCreateBoxMesh_Simplex_Internal(dm, dim, faces, lower, upper, periodicity, interpolate);}
1187:   else               {DMPlexCreateBoxMesh_Tensor_Internal(dm, dim, faces, lower, upper, periodicity);}
1188:   if (!interpolate && dim > 1 && !simplex) {
1189:     DM udm;

1191:     DMPlexUninterpolate(dm, &udm);
1192:     DMPlexCopyCoordinates(dm, udm);
1193:     DMPlexReplace_Static(dm, &udm);
1194:   }
1195:   return(0);
1196: }

1198: /*@C
1199:   DMPlexCreateBoxMesh - Creates a mesh on the tensor product of unit intervals (box) using simplices or tensor cells (hexahedra).

1201:   Collective

1203:   Input Parameters:
1204: + comm        - The communicator for the DM object
1205: . dim         - The spatial dimension
1206: . simplex     - PETSC_TRUE for simplices, PETSC_FALSE for tensor cells
1207: . faces       - Number of faces per dimension, or NULL for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
1208: . lower       - The lower left corner, or NULL for (0, 0, 0)
1209: . upper       - The upper right corner, or NULL for (1, 1, 1)
1210: . periodicity - The boundary type for the X,Y,Z direction, or NULL for DM_BOUNDARY_NONE
1211: - interpolate - Flag to create intermediate mesh pieces (edges, faces)

1213:   Output Parameter:
1214: . dm  - The DM object

1216:   Note: If you want to customize this mesh using options, you just need to
1217: $  DMCreate(comm, &dm);
1218: $  DMSetType(dm, DMPLEX);
1219: $  DMSetFromOptions(dm);
1220: and use the options on the DMSetFromOptions() page.

1222:   Here is the numbering returned for 2 faces in each direction for tensor cells:
1223: $ 10---17---11---18----12
1224: $  |         |         |
1225: $  |         |         |
1226: $ 20    2   22    3    24
1227: $  |         |         |
1228: $  |         |         |
1229: $  7---15----8---16----9
1230: $  |         |         |
1231: $  |         |         |
1232: $ 19    0   21    1   23
1233: $  |         |         |
1234: $  |         |         |
1235: $  4---13----5---14----6

1237: and for simplicial cells

1239: $ 14----8---15----9----16
1240: $  |\     5  |\      7 |
1241: $  | \       | \       |
1242: $ 13   2    14    3    15
1243: $  | 4   \   | 6   \   |
1244: $  |       \ |       \ |
1245: $ 11----6---12----7----13
1246: $  |\        |\        |
1247: $  | \    1  | \     3 |
1248: $ 10   0    11    1    12
1249: $  | 0   \   | 2   \   |
1250: $  |       \ |       \ |
1251: $  8----4----9----5----10

1253:   Level: beginner

1255: .seealso: DMSetFromOptions(), DMPlexCreateFromFile(), DMPlexCreateHexCylinderMesh(), DMSetType(), DMCreate()
1256: @*/
1257: PetscErrorCode DMPlexCreateBoxMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate, DM *dm)
1258: {
1259:   PetscInt       fac[3] = {1, 1, 1};
1260:   PetscReal      low[3] = {0, 0, 0};
1261:   PetscReal      upp[3] = {1, 1, 1};
1262:   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};

1266:   DMCreate(comm,dm);
1267:   DMSetType(*dm,DMPLEX);
1268:   DMPlexCreateBoxMesh_Internal(*dm, dim, simplex, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt, interpolate);
1269:   return(0);
1270: }

1272: static PetscErrorCode DMPlexCreateWedgeBoxMesh_Internal(DM dm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool orderHeight, PetscBool interpolate)
1273: {
1274:   DM             bdm, vol;
1275:   PetscReal      normal[3] = {0., 0., 1.};
1276:   PetscInt       i;

1280:   for (i = 0; i < 3; ++i) if (periodicity[i] != DM_BOUNDARY_NONE) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Periodicity not yet supported");
1281:   DMCreate(PetscObjectComm((PetscObject) dm), &bdm);
1282:   DMSetType(bdm, DMPLEX);
1283:   DMSetDimension(bdm, 2);
1284:   DMPlexCreateBoxMesh_Simplex_Internal(bdm, 2, faces, lower, upper, periodicity, interpolate);
1285:   DMPlexExtrude(bdm, faces[2], upper[2] - lower[2], orderHeight, normal, interpolate, &vol);
1286:   DMDestroy(&bdm);
1287:   DMPlexReplace_Static(dm, &vol);
1288:   if (lower[2] != 0.0) {
1289:     Vec          v;
1290:     PetscScalar *x;
1291:     PetscInt     cDim, n;

1293:     DMGetCoordinatesLocal(dm, &v);
1294:     VecGetBlockSize(v, &cDim);
1295:     VecGetLocalSize(v, &n);
1296:     VecGetArray(v, &x);
1297:     x   += cDim;
1298:     for (i = 0; i < n; i += cDim) x[i] += lower[2];
1299:     VecRestoreArray(v,&x);
1300:     DMSetCoordinatesLocal(dm, v);
1301:   }
1302:   return(0);
1303: }

1305: /*@
1306:   DMPlexCreateWedgeBoxMesh - Creates a 3-D mesh tesselating the (x,y) plane and extruding in the third direction using wedge cells.

1308:   Collective

1310:   Input Parameters:
1311: + comm        - The communicator for the DM object
1312: . faces       - Number of faces per dimension, or NULL for (1, 1, 1)
1313: . lower       - The lower left corner, or NULL for (0, 0, 0)
1314: . upper       - The upper right corner, or NULL for (1, 1, 1)
1315: . periodicity - The boundary type for the X,Y,Z direction, or NULL for DM_BOUNDARY_NONE
1316: . orderHeight - If PETSC_TRUE, orders the extruded cells in the height first. Otherwise, orders the cell on the layers first
1317: - interpolate - Flag to create intermediate mesh pieces (edges, faces)

1319:   Output Parameter:
1320: . dm  - The DM object

1322:   Level: beginner

1324: .seealso: DMPlexCreateHexCylinderMesh(), DMPlexCreateWedgeCylinderMesh(), DMPlexExtrude(), DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
1325: @*/
1326: PetscErrorCode DMPlexCreateWedgeBoxMesh(MPI_Comm comm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool orderHeight, PetscBool interpolate, DM *dm)
1327: {
1328:   PetscInt       fac[3] = {1, 1, 1};
1329:   PetscReal      low[3] = {0, 0, 0};
1330:   PetscReal      upp[3] = {1, 1, 1};
1331:   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};

1335:   DMCreate(comm,dm);
1336:   DMSetType(*dm,DMPLEX);
1337:   DMPlexCreateWedgeBoxMesh_Internal(*dm, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt, orderHeight, interpolate);
1338:   return(0);
1339: }

1341: /*@C
1342:   DMPlexExtrude - Creates a (d+1)-D mesh by extruding a d-D mesh in the normal direction using prismatic cells.

1344:   Collective on idm

1346:   Input Parameters:
1347: + idm         - The mesh to be extruded
1348: . layers      - The number of layers, or PETSC_DETERMINE to use the default
1349: . height      - The total height of the extrusion, or PETSC_DETERMINE to use the default
1350: . orderHeight - If PETSC_TRUE, orders the extruded cells in the height first. Otherwise, orders the cell on the layers first
1351: . extNormal   - The normal direction in which the mesh should be extruded, or NULL to extrude using the surface normal
1352: - interpolate - Flag to create intermediate mesh pieces (edges, faces)

1354:   Output Parameter:
1355: . dm  - The DM object

1357:   Notes:
1358:   The mesh created has prismatic cells, and the vertex ordering in the cone of the cell is that of the tensor prismatic cells. Not currently supported in Fortran.

1360:   Options Database Keys:
1361: +   -dm_plex_extrude_layers <k> - Sets the number of layers k
1362: .   -dm_plex_extrude_height <h> - Sets the total height of the extrusion
1363: .   -dm_plex_extrude_heights <h0,h1,...> - Sets the height of each layer
1364: .   -dm_plex_extrude_order_height - If true, order cells by height first
1365: -   -dm_plex_extrude_normal <n0,...,nd> - Sets the normal vector along which to extrude

1367:   Level: advanced

1369: .seealso: DMPlexCreateWedgeCylinderMesh(), DMPlexCreateWedgeBoxMesh(), DMSetType(), DMCreate()
1370: @*/
1371: PetscErrorCode DMPlexExtrude(DM idm, PetscInt layers, PetscReal height, PetscBool orderHeight, const PetscReal extNormal[], PetscBool interpolate, DM* dm)
1372: {
1373:   PetscScalar       *coordsB;
1374:   const PetscScalar *coordsA;
1375:   PetscReal         *normals = NULL, *heights = NULL;
1376:   PetscReal         clNormal[3];
1377:   Vec               coordinatesA, coordinatesB;
1378:   PetscSection      coordSectionA, coordSectionB;
1379:   PetscInt          dim, cDim, cDimB, c, l, v, coordSize, *newCone, nl;
1380:   PetscInt          cStart, cEnd, vStart, vEnd, cellV, numCells, numVertices;
1381:   const char       *prefix;
1382:   PetscBool         haveCLNormal, flg;
1383:   PetscErrorCode    ierr;

1390:   DMGetDimension(idm, &dim);
1391:   DMGetCoordinateDim(idm, &cDim);
1392:   cDimB = cDim == dim ? cDim+1 : cDim;
1393:   if (dim < 1 || dim > 3) SETERRQ1(PetscObjectComm((PetscObject)idm), PETSC_ERR_SUP, "Support for dimension %D not coded", dim);

1395:   PetscObjectGetOptionsPrefix((PetscObject) idm, &prefix);
1396:   if (layers < 0) layers = 1;
1397:   PetscOptionsGetInt(NULL, prefix, "-dm_plex_extrude_layers", &layers, NULL);
1398:   if (layers <= 0) SETERRQ1(PetscObjectComm((PetscObject) idm), PETSC_ERR_ARG_OUTOFRANGE, "Number of layers %D must be positive", layers);
1399:   if (height < 0.) height = 1.;
1400:   PetscOptionsGetReal(NULL, prefix, "-dm_plex_extrude_height", &height, NULL);
1401:   if (height <= 0.) SETERRQ1(PetscObjectComm((PetscObject) idm), PETSC_ERR_ARG_OUTOFRANGE, "Height of layers %g must be positive", (double) height);
1402:   PetscMalloc1(layers, &heights);
1403:   nl   = layers;
1404:   PetscOptionsGetRealArray(NULL, prefix, "-dm_plex_extrude_heights", heights, &nl, &flg);
1405:   if (flg) {
1406:     if (!nl) SETERRQ(PetscObjectComm((PetscObject) idm), PETSC_ERR_ARG_OUTOFRANGE, "Must give at least one height for -dm_plex_extrude_heights");
1407:     for (l = nl; l < layers; ++l) heights[l] = heights[l-1];
1408:     for (l = 0; l < layers; ++l) if (heights[l] <= 0.) SETERRQ2(PetscObjectComm((PetscObject) idm), PETSC_ERR_ARG_OUTOFRANGE, "Height %g of layers %D must be positive", (double) heights[l], l);
1409:   } else {
1410:     for (l = 0; l < layers; ++l) heights[l] = height/layers;
1411:   }
1412:   PetscOptionsGetBool(NULL, prefix, "-dm_plex_extrude_order_height", &orderHeight, NULL);
1413:   c = 3;
1414:   PetscOptionsGetRealArray(NULL, prefix, "-dm_plex_extrude_normal", clNormal, &c, &haveCLNormal);
1415:   if (haveCLNormal && c != cDimB) SETERRQ2(PetscObjectComm((PetscObject)idm), PETSC_ERR_ARG_SIZ, "Input normal has size %D != %D extruded coordinate dimension", c, cDimB);

1417:   DMPlexGetHeightStratum(idm, 0, &cStart, &cEnd);
1418:   DMPlexGetDepthStratum(idm, 0, &vStart, &vEnd);
1419:   numCells = (cEnd - cStart)*layers;
1420:   numVertices = (vEnd - vStart)*(layers+1);
1421:   DMCreate(PetscObjectComm((PetscObject)idm), dm);
1422:   DMSetType(*dm, DMPLEX);
1423:   DMSetDimension(*dm, dim+1);
1424:   DMPlexSetChart(*dm, 0, numCells+numVertices);
1425:   /* Must create the celltype label here so that we do not automatically try to compute the types */
1426:   DMCreateLabel(*dm, "celltype");
1427:   for (c = cStart, cellV = 0; c < cEnd; ++c) {
1428:     DMPolytopeType ct, nct;
1429:     PetscInt      *closure = NULL;
1430:     PetscInt       closureSize, numCorners = 0;

1432:     DMPlexGetCellType(idm, c, &ct);
1433:     switch (ct) {
1434:       case DM_POLYTOPE_SEGMENT:       nct = DM_POLYTOPE_SEG_PRISM_TENSOR;break;
1435:       case DM_POLYTOPE_TRIANGLE:      nct = DM_POLYTOPE_TRI_PRISM_TENSOR;break;
1436:       case DM_POLYTOPE_QUADRILATERAL: nct = DM_POLYTOPE_QUAD_PRISM_TENSOR;break;
1437:       default: nct = DM_POLYTOPE_UNKNOWN;
1438:     }
1439:     DMPlexGetTransitiveClosure(idm, c, PETSC_TRUE, &closureSize, &closure);
1440:     for (v = 0; v < closureSize*2; v += 2) if ((closure[v] >= vStart) && (closure[v] < vEnd)) numCorners++;
1441:     DMPlexRestoreTransitiveClosure(idm, c, PETSC_TRUE, &closureSize, &closure);
1442:     for (l = 0; l < layers; ++l) {
1443:       const PetscInt cell = orderHeight ? layers*(c - cStart) + l : l*(cEnd - cStart) + c - cStart;

1445:       DMPlexSetConeSize(*dm, cell, 2*numCorners);
1446:       DMPlexSetCellType(*dm, cell, nct);
1447:     }
1448:     cellV = PetscMax(numCorners,cellV);
1449:   }
1450:   DMSetUp(*dm);

1452:   if (dim != cDim && !(extNormal || haveCLNormal)) {PetscCalloc1(cDim*(vEnd - vStart), &normals);}
1453:   PetscMalloc1(3*cellV,&newCone);
1454:   for (c = cStart; c < cEnd; ++c) {
1455:     PetscInt *closure = NULL;
1456:     PetscInt closureSize, numCorners = 0, l;
1457:     PetscReal normal[3] = {0, 0, 0};

1459:     if (normals) {DMPlexComputeCellGeometryFVM(idm, c, NULL, NULL, normal);}
1460:     DMPlexGetTransitiveClosure(idm, c, PETSC_TRUE, &closureSize, &closure);
1461:     for (v = 0; v < closureSize*2; v += 2) {
1462:       if ((closure[v] >= vStart) && (closure[v] < vEnd)) {
1463:         PetscInt d;

1465:         newCone[numCorners++] = closure[v] - vStart;
1466:         if (normals) {for (d = 0; d < cDim; ++d) normals[cDim*(closure[v]-vStart)+d] += normal[d];}
1467:       }
1468:     }
1469:     DMPlexRestoreTransitiveClosure(idm, c, PETSC_TRUE, &closureSize, &closure);
1470:     for (l = 0; l < layers; ++l) {
1471:       PetscInt i;

1473:       for (i = 0; i < numCorners; ++i) {
1474:         newCone[  numCorners + i] = orderHeight ? (layers+1)*newCone[i] + l     + numCells :     l*(vEnd - vStart) + newCone[i] + numCells;
1475:         newCone[2*numCorners + i] = orderHeight ? (layers+1)*newCone[i] + l + 1 + numCells : (l+1)*(vEnd - vStart) + newCone[i] + numCells;
1476:       }
1477:       DMPlexSetCone(*dm, orderHeight ? layers*(c - cStart) + l : l*(cEnd - cStart) + c - cStart, newCone + numCorners);
1478:     }
1479:   }
1480:   DMPlexSymmetrize(*dm);
1481:   DMPlexStratify(*dm);
1482:   PetscFree(newCone);

1484:   DMGetCoordinateSection(*dm, &coordSectionB);
1485:   PetscSectionSetNumFields(coordSectionB, 1);
1486:   PetscSectionSetFieldComponents(coordSectionB, 0, cDimB);
1487:   PetscSectionSetChart(coordSectionB, numCells, numCells+numVertices);
1488:   for (v = numCells; v < numCells+numVertices; ++v) {
1489:     PetscSectionSetDof(coordSectionB, v, cDimB);
1490:     PetscSectionSetFieldDof(coordSectionB, v, 0, cDimB);
1491:     DMPlexSetCellType(*dm, v, DM_POLYTOPE_POINT);
1492:   }
1493:   PetscSectionSetUp(coordSectionB);
1494:   PetscSectionGetStorageSize(coordSectionB, &coordSize);
1495:   VecCreate(PETSC_COMM_SELF, &coordinatesB);
1496:   PetscObjectSetName((PetscObject) coordinatesB, "coordinates");
1497:   VecSetSizes(coordinatesB, coordSize, PETSC_DETERMINE);
1498:   VecSetBlockSize(coordinatesB, cDimB);
1499:   VecSetType(coordinatesB,VECSTANDARD);

1501:   DMGetCoordinateSection(idm, &coordSectionA);
1502:   DMGetCoordinatesLocal(idm, &coordinatesA);
1503:   VecGetArray(coordinatesB, &coordsB);
1504:   VecGetArrayRead(coordinatesA, &coordsA);
1505:   for (v = vStart; v < vEnd; ++v) {
1506:     const PetscScalar *cptr;
1507:     PetscReal         ones2[2] = { 0., 1.}, ones3[3] = { 0., 0., 1.};
1508:     PetscReal         normal[3];
1509:     PetscReal         norm;
1510:     PetscInt          offA, d, cDimA = cDim;

1512:     if (normals)           {for (d = 0; d < cDimB; ++d) normal[d] = normals[cDimB*(v - vStart)+d];}
1513:     else if (haveCLNormal) {for (d = 0; d < cDimB; ++d) normal[d] = clNormal[d];}
1514:     else if (extNormal)    {for (d = 0; d < cDimB; ++d) normal[d] = extNormal[d];}
1515:     else if (cDimB == 2)   {for (d = 0; d < cDimB; ++d) normal[d] = ones2[d];}
1516:     else if (cDimB == 3)   {for (d = 0; d < cDimB; ++d) normal[d] = ones3[d];}
1517:     else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unable to determine normal for extrusion");
1518:     for (d = 0, norm = 0.0; d < cDimB; ++d) norm += normal[d]*normal[d];
1519:     for (d = 0; d < cDimB; ++d) normal[d] *= 1./PetscSqrtReal(norm);

1521:     PetscSectionGetOffset(coordSectionA, v, &offA);
1522:     cptr = coordsA + offA;
1523:     for (l = 0; l <= layers; ++l) {
1524:       PetscInt offB, d, newV;

1526:       newV = orderHeight ? (layers+1)*(v -vStart) + l + numCells : (vEnd -vStart)*l + (v -vStart) + numCells;
1527:       PetscSectionGetOffset(coordSectionB, newV, &offB);
1528:       for (d = 0; d < cDimA; ++d) { coordsB[offB+d]  = cptr[d]; }
1529:       for (d = 0; d < cDimB; ++d) { coordsB[offB+d] += l ? normal[d]*heights[l-1] : 0.0; }
1530:       cptr    = coordsB + offB;
1531:       cDimA   = cDimB;
1532:     }
1533:   }
1534:   VecRestoreArrayRead(coordinatesA, &coordsA);
1535:   VecRestoreArray(coordinatesB, &coordsB);
1536:   DMSetCoordinatesLocal(*dm, coordinatesB);
1537:   VecDestroy(&coordinatesB);
1538:   PetscFree(normals);
1539:   PetscFree(heights);
1540:   if (interpolate) {
1541:     DM idm;

1543:     DMPlexInterpolate(*dm, &idm);
1544:     DMPlexCopyCoordinates(*dm, idm);
1545:     DMDestroy(dm);
1546:     *dm  = idm;
1547:   }
1548:   return(0);
1549: }

1551: /*@C
1552:   DMPlexSetOptionsPrefix - Sets the prefix used for searching for all DM options in the database.

1554:   Logically Collective on dm

1556:   Input Parameters:
1557: + dm - the DM context
1558: - prefix - the prefix to prepend to all option names

1560:   Notes:
1561:   A hyphen (-) must NOT be given at the beginning of the prefix name.
1562:   The first character of all runtime options is AUTOMATICALLY the hyphen.

1564:   Level: advanced

1566: .seealso: SNESSetFromOptions()
1567: @*/
1568: PetscErrorCode DMPlexSetOptionsPrefix(DM dm, const char prefix[])
1569: {
1570:   DM_Plex       *mesh = (DM_Plex *) dm->data;

1575:   PetscObjectSetOptionsPrefix((PetscObject) dm, prefix);
1576:   PetscObjectSetOptionsPrefix((PetscObject) mesh->partitioner, prefix);
1577:   return(0);
1578: }

1580: /* Remap geometry to cylinder
1581:    TODO: This only works for a single refinement, then it is broken

1583:      Interior square: Linear interpolation is correct
1584:      The other cells all have vertices on rays from the origin. We want to uniformly expand the spacing
1585:      such that the last vertex is on the unit circle. So the closest and farthest vertices are at distance

1587:        phi     = arctan(y/x)
1588:        d_close = sqrt(1/8 + 1/4 sin^2(phi))
1589:        d_far   = sqrt(1/2 + sin^2(phi))

1591:      so we remap them using

1593:        x_new = x_close + (x - x_close) (1 - d_close) / (d_far - d_close)
1594:        y_new = y_close + (y - y_close) (1 - d_close) / (d_far - d_close)

1596:      If pi/4 < phi < 3pi/4 or -3pi/4 < phi < -pi/4, then we switch x and y.
1597: */
1598: static void snapToCylinder(PetscInt dim, PetscInt Nf, PetscInt NfAux,
1599:                            const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
1600:                            const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
1601:                            PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
1602: {
1603:   const PetscReal dis = 1.0/PetscSqrtReal(2.0);
1604:   const PetscReal ds2 = 0.5*dis;

1606:   if ((PetscAbsScalar(u[0]) <= ds2) && (PetscAbsScalar(u[1]) <= ds2)) {
1607:     f0[0] = u[0];
1608:     f0[1] = u[1];
1609:   } else {
1610:     PetscReal phi, sinp, cosp, dc, df, x, y, xc, yc;

1612:     x    = PetscRealPart(u[0]);
1613:     y    = PetscRealPart(u[1]);
1614:     phi  = PetscAtan2Real(y, x);
1615:     sinp = PetscSinReal(phi);
1616:     cosp = PetscCosReal(phi);
1617:     if ((PetscAbsReal(phi) > PETSC_PI/4.0) && (PetscAbsReal(phi) < 3.0*PETSC_PI/4.0)) {
1618:       dc = PetscAbsReal(ds2/sinp);
1619:       df = PetscAbsReal(dis/sinp);
1620:       xc = ds2*x/PetscAbsReal(y);
1621:       yc = ds2*PetscSignReal(y);
1622:     } else {
1623:       dc = PetscAbsReal(ds2/cosp);
1624:       df = PetscAbsReal(dis/cosp);
1625:       xc = ds2*PetscSignReal(x);
1626:       yc = ds2*y/PetscAbsReal(x);
1627:     }
1628:     f0[0] = xc + (u[0] - xc)*(1.0 - dc)/(df - dc);
1629:     f0[1] = yc + (u[1] - yc)*(1.0 - dc)/(df - dc);
1630:   }
1631:   f0[2] = u[2];
1632: }

1634: static PetscErrorCode DMPlexCreateHexCylinderMesh_Internal(DM dm, DMBoundaryType periodicZ)
1635: {
1636:   const PetscInt dim = 3;
1637:   PetscInt       numCells, numVertices;
1638:   PetscMPIInt    rank;

1642:   MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);
1643:   DMSetDimension(dm, dim);
1644:   /* Create topology */
1645:   {
1646:     PetscInt cone[8], c;

1648:     numCells    = !rank ?  5 : 0;
1649:     numVertices = !rank ? 16 : 0;
1650:     if (periodicZ == DM_BOUNDARY_PERIODIC) {
1651:       numCells   *= 3;
1652:       numVertices = !rank ? 24 : 0;
1653:     }
1654:     DMPlexSetChart(dm, 0, numCells+numVertices);
1655:     for (c = 0; c < numCells; c++) {DMPlexSetConeSize(dm, c, 8);}
1656:     DMSetUp(dm);
1657:     if (!rank) {
1658:       if (periodicZ == DM_BOUNDARY_PERIODIC) {
1659:         cone[0] = 15; cone[1] = 18; cone[2] = 17; cone[3] = 16;
1660:         cone[4] = 31; cone[5] = 32; cone[6] = 33; cone[7] = 34;
1661:         DMPlexSetCone(dm, 0, cone);
1662:         cone[0] = 16; cone[1] = 17; cone[2] = 24; cone[3] = 23;
1663:         cone[4] = 32; cone[5] = 36; cone[6] = 37; cone[7] = 33; /* 22 25 26 21 */
1664:         DMPlexSetCone(dm, 1, cone);
1665:         cone[0] = 18; cone[1] = 27; cone[2] = 24; cone[3] = 17;
1666:         cone[4] = 34; cone[5] = 33; cone[6] = 37; cone[7] = 38;
1667:         DMPlexSetCone(dm, 2, cone);
1668:         cone[0] = 29; cone[1] = 27; cone[2] = 18; cone[3] = 15;
1669:         cone[4] = 35; cone[5] = 31; cone[6] = 34; cone[7] = 38;
1670:         DMPlexSetCone(dm, 3, cone);
1671:         cone[0] = 29; cone[1] = 15; cone[2] = 16; cone[3] = 23;
1672:         cone[4] = 35; cone[5] = 36; cone[6] = 32; cone[7] = 31;
1673:         DMPlexSetCone(dm, 4, cone);

1675:         cone[0] = 31; cone[1] = 34; cone[2] = 33; cone[3] = 32;
1676:         cone[4] = 19; cone[5] = 22; cone[6] = 21; cone[7] = 20;
1677:         DMPlexSetCone(dm, 5, cone);
1678:         cone[0] = 32; cone[1] = 33; cone[2] = 37; cone[3] = 36;
1679:         cone[4] = 22; cone[5] = 25; cone[6] = 26; cone[7] = 21;
1680:         DMPlexSetCone(dm, 6, cone);
1681:         cone[0] = 34; cone[1] = 38; cone[2] = 37; cone[3] = 33;
1682:         cone[4] = 20; cone[5] = 21; cone[6] = 26; cone[7] = 28;
1683:         DMPlexSetCone(dm, 7, cone);
1684:         cone[0] = 35; cone[1] = 38; cone[2] = 34; cone[3] = 31;
1685:         cone[4] = 30; cone[5] = 19; cone[6] = 20; cone[7] = 28;
1686:         DMPlexSetCone(dm, 8, cone);
1687:         cone[0] = 35; cone[1] = 31; cone[2] = 32; cone[3] = 36;
1688:         cone[4] = 30; cone[5] = 25; cone[6] = 22; cone[7] = 19;
1689:         DMPlexSetCone(dm, 9, cone);

1691:         cone[0] = 19; cone[1] = 20; cone[2] = 21; cone[3] = 22;
1692:         cone[4] = 15; cone[5] = 16; cone[6] = 17; cone[7] = 18;
1693:         DMPlexSetCone(dm, 10, cone);
1694:         cone[0] = 22; cone[1] = 21; cone[2] = 26; cone[3] = 25;
1695:         cone[4] = 16; cone[5] = 23; cone[6] = 24; cone[7] = 17;
1696:         DMPlexSetCone(dm, 11, cone);
1697:         cone[0] = 20; cone[1] = 28; cone[2] = 26; cone[3] = 21;
1698:         cone[4] = 18; cone[5] = 17; cone[6] = 24; cone[7] = 27;
1699:         DMPlexSetCone(dm, 12, cone);
1700:         cone[0] = 30; cone[1] = 28; cone[2] = 20; cone[3] = 19;
1701:         cone[4] = 29; cone[5] = 15; cone[6] = 18; cone[7] = 27;
1702:         DMPlexSetCone(dm, 13, cone);
1703:         cone[0] = 30; cone[1] = 19; cone[2] = 22; cone[3] = 25;
1704:         cone[4] = 29; cone[5] = 23; cone[6] = 16; cone[7] = 15;
1705:         DMPlexSetCone(dm, 14, cone);
1706:       } else {
1707:         cone[0] =  5; cone[1] =  8; cone[2] =  7; cone[3] =  6;
1708:         cone[4] =  9; cone[5] = 12; cone[6] = 11; cone[7] = 10;
1709:         DMPlexSetCone(dm, 0, cone);
1710:         cone[0] =  6; cone[1] =  7; cone[2] = 14; cone[3] = 13;
1711:         cone[4] = 12; cone[5] = 15; cone[6] = 16; cone[7] = 11;
1712:         DMPlexSetCone(dm, 1, cone);
1713:         cone[0] =  8; cone[1] = 17; cone[2] = 14; cone[3] =  7;
1714:         cone[4] = 10; cone[5] = 11; cone[6] = 16; cone[7] = 18;
1715:         DMPlexSetCone(dm, 2, cone);
1716:         cone[0] = 19; cone[1] = 17; cone[2] =  8; cone[3] =  5;
1717:         cone[4] = 20; cone[5] =  9; cone[6] = 10; cone[7] = 18;
1718:         DMPlexSetCone(dm, 3, cone);
1719:         cone[0] = 19; cone[1] =  5; cone[2] =  6; cone[3] = 13;
1720:         cone[4] = 20; cone[5] = 15; cone[6] = 12; cone[7] =  9;
1721:         DMPlexSetCone(dm, 4, cone);
1722:       }
1723:     }
1724:     DMPlexSymmetrize(dm);
1725:     DMPlexStratify(dm);
1726:   }
1727:   /* Create cube geometry */
1728:   {
1729:     Vec             coordinates;
1730:     PetscSection    coordSection;
1731:     PetscScalar    *coords;
1732:     PetscInt        coordSize, v;
1733:     const PetscReal dis = 1.0/PetscSqrtReal(2.0);
1734:     const PetscReal ds2 = dis/2.0;

1736:     /* Build coordinates */
1737:     DMGetCoordinateSection(dm, &coordSection);
1738:     PetscSectionSetNumFields(coordSection, 1);
1739:     PetscSectionSetFieldComponents(coordSection, 0, dim);
1740:     PetscSectionSetChart(coordSection, numCells, numCells+numVertices);
1741:     for (v = numCells; v < numCells+numVertices; ++v) {
1742:       PetscSectionSetDof(coordSection, v, dim);
1743:       PetscSectionSetFieldDof(coordSection, v, 0, dim);
1744:     }
1745:     PetscSectionSetUp(coordSection);
1746:     PetscSectionGetStorageSize(coordSection, &coordSize);
1747:     VecCreate(PETSC_COMM_SELF, &coordinates);
1748:     PetscObjectSetName((PetscObject) coordinates, "coordinates");
1749:     VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
1750:     VecSetBlockSize(coordinates, dim);
1751:     VecSetType(coordinates,VECSTANDARD);
1752:     VecGetArray(coordinates, &coords);
1753:     if (!rank) {
1754:       coords[0*dim+0] = -ds2; coords[0*dim+1] = -ds2; coords[0*dim+2] = 0.0;
1755:       coords[1*dim+0] =  ds2; coords[1*dim+1] = -ds2; coords[1*dim+2] = 0.0;
1756:       coords[2*dim+0] =  ds2; coords[2*dim+1] =  ds2; coords[2*dim+2] = 0.0;
1757:       coords[3*dim+0] = -ds2; coords[3*dim+1] =  ds2; coords[3*dim+2] = 0.0;
1758:       coords[4*dim+0] = -ds2; coords[4*dim+1] = -ds2; coords[4*dim+2] = 1.0;
1759:       coords[5*dim+0] = -ds2; coords[5*dim+1] =  ds2; coords[5*dim+2] = 1.0;
1760:       coords[6*dim+0] =  ds2; coords[6*dim+1] =  ds2; coords[6*dim+2] = 1.0;
1761:       coords[7*dim+0] =  ds2; coords[7*dim+1] = -ds2; coords[7*dim+2] = 1.0;
1762:       coords[ 8*dim+0] =  dis; coords[ 8*dim+1] = -dis; coords[ 8*dim+2] = 0.0;
1763:       coords[ 9*dim+0] =  dis; coords[ 9*dim+1] =  dis; coords[ 9*dim+2] = 0.0;
1764:       coords[10*dim+0] =  dis; coords[10*dim+1] = -dis; coords[10*dim+2] = 1.0;
1765:       coords[11*dim+0] =  dis; coords[11*dim+1] =  dis; coords[11*dim+2] = 1.0;
1766:       coords[12*dim+0] = -dis; coords[12*dim+1] =  dis; coords[12*dim+2] = 0.0;
1767:       coords[13*dim+0] = -dis; coords[13*dim+1] =  dis; coords[13*dim+2] = 1.0;
1768:       coords[14*dim+0] = -dis; coords[14*dim+1] = -dis; coords[14*dim+2] = 0.0;
1769:       coords[15*dim+0] = -dis; coords[15*dim+1] = -dis; coords[15*dim+2] = 1.0;
1770:       if (periodicZ == DM_BOUNDARY_PERIODIC) {
1771:         /* 15 31 19 */ coords[16*dim+0] = -ds2; coords[16*dim+1] = -ds2; coords[16*dim+2] = 0.5;
1772:         /* 16 32 22 */ coords[17*dim+0] =  ds2; coords[17*dim+1] = -ds2; coords[17*dim+2] = 0.5;
1773:         /* 17 33 21 */ coords[18*dim+0] =  ds2; coords[18*dim+1] =  ds2; coords[18*dim+2] = 0.5;
1774:         /* 18 34 20 */ coords[19*dim+0] = -ds2; coords[19*dim+1] =  ds2; coords[19*dim+2] = 0.5;
1775:         /* 29 35 30 */ coords[20*dim+0] = -dis; coords[20*dim+1] = -dis; coords[20*dim+2] = 0.5;
1776:         /* 23 36 25 */ coords[21*dim+0] =  dis; coords[21*dim+1] = -dis; coords[21*dim+2] = 0.5;
1777:         /* 24 37 26 */ coords[22*dim+0] =  dis; coords[22*dim+1] =  dis; coords[22*dim+2] = 0.5;
1778:         /* 27 38 28 */ coords[23*dim+0] = -dis; coords[23*dim+1] =  dis; coords[23*dim+2] = 0.5;
1779:       }
1780:     }
1781:     VecRestoreArray(coordinates, &coords);
1782:     DMSetCoordinatesLocal(dm, coordinates);
1783:     VecDestroy(&coordinates);
1784:   }
1785:   /* Create periodicity */
1786:   if (periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
1787:     PetscReal      L[3];
1788:     PetscReal      maxCell[3];
1789:     DMBoundaryType bdType[3];
1790:     PetscReal      lower[3] = {0.0, 0.0, 0.0};
1791:     PetscReal      upper[3] = {1.0, 1.0, 1.5};
1792:     PetscInt       i, numZCells = 3;

1794:     bdType[0] = DM_BOUNDARY_NONE;
1795:     bdType[1] = DM_BOUNDARY_NONE;
1796:     bdType[2] = periodicZ;
1797:     for (i = 0; i < dim; i++) {
1798:       L[i]       = upper[i] - lower[i];
1799:       maxCell[i] = 1.1 * (L[i] / numZCells);
1800:     }
1801:     DMSetPeriodicity(dm, PETSC_TRUE, maxCell, L, bdType);
1802:   }
1803:   {
1804:     DM          cdm;
1805:     PetscDS     cds;
1806:     PetscScalar c[2] = {1.0, 1.0};

1808:     DMPlexCreateCoordinateSpace(dm, 1, snapToCylinder);
1809:     DMGetCoordinateDM(dm, &cdm);
1810:     DMGetDS(cdm, &cds);
1811:     PetscDSSetConstants(cds, 2, c);
1812:   }
1813:   /* Wait for coordinate creation before doing in-place modification */
1814:   DMPlexInterpolateInPlace_Internal(dm);
1815:   return(0);
1816: }

1818: /*@
1819:   DMPlexCreateHexCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using hexahedra.

1821:   Collective

1823:   Input Parameters:
1824: + comm      - The communicator for the DM object
1825: - periodicZ - The boundary type for the Z direction

1827:   Output Parameter:
1828: . dm  - The DM object

1830:   Note:
1831:   Here is the output numbering looking from the bottom of the cylinder:
1832: $       17-----14
1833: $        |     |
1834: $        |  2  |
1835: $        |     |
1836: $ 17-----8-----7-----14
1837: $  |     |     |     |
1838: $  |  3  |  0  |  1  |
1839: $  |     |     |     |
1840: $ 19-----5-----6-----13
1841: $        |     |
1842: $        |  4  |
1843: $        |     |
1844: $       19-----13
1845: $
1846: $ and up through the top
1847: $
1848: $       18-----16
1849: $        |     |
1850: $        |  2  |
1851: $        |     |
1852: $ 18----10----11-----16
1853: $  |     |     |     |
1854: $  |  3  |  0  |  1  |
1855: $  |     |     |     |
1856: $ 20-----9----12-----15
1857: $        |     |
1858: $        |  4  |
1859: $        |     |
1860: $       20-----15

1862:   Level: beginner

1864: .seealso: DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
1865: @*/
1866: PetscErrorCode DMPlexCreateHexCylinderMesh(MPI_Comm comm, DMBoundaryType periodicZ, DM *dm)
1867: {

1872:   DMCreate(comm, dm);
1873:   DMSetType(*dm, DMPLEX);
1874:   DMPlexCreateHexCylinderMesh_Internal(*dm, periodicZ);
1875:   return(0);
1876: }

1878: static PetscErrorCode DMPlexCreateWedgeCylinderMesh_Internal(DM dm, PetscInt n, PetscBool interpolate)
1879: {
1880:   const PetscInt dim = 3;
1881:   PetscInt       numCells, numVertices, v;
1882:   PetscMPIInt    rank;

1886:   if (n < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of wedges %D cannot be negative", n);
1887:   MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);
1888:   DMSetDimension(dm, dim);
1889:   /* Must create the celltype label here so that we do not automatically try to compute the types */
1890:   DMCreateLabel(dm, "celltype");
1891:   /* Create topology */
1892:   {
1893:     PetscInt cone[6], c;

1895:     numCells    = !rank ?        n : 0;
1896:     numVertices = !rank ?  2*(n+1) : 0;
1897:     DMPlexSetChart(dm, 0, numCells+numVertices);
1898:     for (c = 0; c < numCells; c++) {DMPlexSetConeSize(dm, c, 6);}
1899:     DMSetUp(dm);
1900:     for (c = 0; c < numCells; c++) {
1901:       cone[0] =  c+n*1; cone[1] = (c+1)%n+n*1; cone[2] = 0+3*n;
1902:       cone[3] =  c+n*2; cone[4] = (c+1)%n+n*2; cone[5] = 1+3*n;
1903:       DMPlexSetCone(dm, c, cone);
1904:       DMPlexSetCellType(dm, c, DM_POLYTOPE_TRI_PRISM_TENSOR);
1905:     }
1906:     DMPlexSymmetrize(dm);
1907:     DMPlexStratify(dm);
1908:   }
1909:   for (v = numCells; v < numCells+numVertices; ++v) {
1910:     DMPlexSetCellType(dm, v, DM_POLYTOPE_POINT);
1911:   }
1912:   /* Create cylinder geometry */
1913:   {
1914:     Vec          coordinates;
1915:     PetscSection coordSection;
1916:     PetscScalar *coords;
1917:     PetscInt     coordSize, c;

1919:     /* Build coordinates */
1920:     DMGetCoordinateSection(dm, &coordSection);
1921:     PetscSectionSetNumFields(coordSection, 1);
1922:     PetscSectionSetFieldComponents(coordSection, 0, dim);
1923:     PetscSectionSetChart(coordSection, numCells, numCells+numVertices);
1924:     for (v = numCells; v < numCells+numVertices; ++v) {
1925:       PetscSectionSetDof(coordSection, v, dim);
1926:       PetscSectionSetFieldDof(coordSection, v, 0, dim);
1927:     }
1928:     PetscSectionSetUp(coordSection);
1929:     PetscSectionGetStorageSize(coordSection, &coordSize);
1930:     VecCreate(PETSC_COMM_SELF, &coordinates);
1931:     PetscObjectSetName((PetscObject) coordinates, "coordinates");
1932:     VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
1933:     VecSetBlockSize(coordinates, dim);
1934:     VecSetType(coordinates,VECSTANDARD);
1935:     VecGetArray(coordinates, &coords);
1936:     for (c = 0; c < numCells; c++) {
1937:       coords[(c+0*n)*dim+0] = PetscCosReal(2.0*c*PETSC_PI/n); coords[(c+0*n)*dim+1] = PetscSinReal(2.0*c*PETSC_PI/n); coords[(c+0*n)*dim+2] = 1.0;
1938:       coords[(c+1*n)*dim+0] = PetscCosReal(2.0*c*PETSC_PI/n); coords[(c+1*n)*dim+1] = PetscSinReal(2.0*c*PETSC_PI/n); coords[(c+1*n)*dim+2] = 0.0;
1939:     }
1940:     if (!rank) {
1941:       coords[(2*n+0)*dim+0] = 0.0; coords[(2*n+0)*dim+1] = 0.0; coords[(2*n+0)*dim+2] = 1.0;
1942:       coords[(2*n+1)*dim+0] = 0.0; coords[(2*n+1)*dim+1] = 0.0; coords[(2*n+1)*dim+2] = 0.0;
1943:     }
1944:     VecRestoreArray(coordinates, &coords);
1945:     DMSetCoordinatesLocal(dm, coordinates);
1946:     VecDestroy(&coordinates);
1947:   }
1948:   /* Interpolate */
1949:   if (interpolate) {DMPlexInterpolateInPlace_Internal(dm);}
1950:   return(0);
1951: }

1953: /*@
1954:   DMPlexCreateWedgeCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using wedges.

1956:   Collective

1958:   Input Parameters:
1959: + comm - The communicator for the DM object
1960: . n    - The number of wedges around the origin
1961: - interpolate - Create edges and faces

1963:   Output Parameter:
1964: . dm  - The DM object

1966:   Level: beginner

1968: .seealso: DMPlexCreateHexCylinderMesh(), DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
1969: @*/
1970: PetscErrorCode DMPlexCreateWedgeCylinderMesh(MPI_Comm comm, PetscInt n, PetscBool interpolate, DM *dm)
1971: {

1976:   DMCreate(comm, dm);
1977:   DMSetType(*dm, DMPLEX);
1978:   DMPlexCreateWedgeCylinderMesh_Internal(*dm, n, interpolate);
1979:   return(0);
1980: }

1982: PETSC_STATIC_INLINE PetscReal DiffNormReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
1983: {
1984:   PetscReal prod = 0.0;
1985:   PetscInt  i;
1986:   for (i = 0; i < dim; ++i) prod += PetscSqr(x[i] - y[i]);
1987:   return PetscSqrtReal(prod);
1988: }
1989: PETSC_STATIC_INLINE PetscReal DotReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
1990: {
1991:   PetscReal prod = 0.0;
1992:   PetscInt  i;
1993:   for (i = 0; i < dim; ++i) prod += x[i]*y[i];
1994:   return prod;
1995: }

1997: /* The first constant is the sphere radius */
1998: static void snapToSphere(PetscInt dim, PetscInt Nf, PetscInt NfAux,
1999:                          const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
2000:                          const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
2001:                          PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
2002: {
2003:   PetscReal r = PetscRealPart(constants[0]);
2004:   PetscReal norm2 = 0.0, fac;
2005:   PetscInt  n = uOff[1] - uOff[0], d;

2007:   for (d = 0; d < n; ++d) norm2 += PetscSqr(PetscRealPart(u[d]));
2008:   fac = r/PetscSqrtReal(norm2);
2009:   for (d = 0; d < n; ++d) f0[d] = u[d]*fac;
2010: }

2012: static PetscErrorCode DMPlexCreateSphereMesh_Internal(DM dm, PetscInt dim, PetscBool simplex, PetscReal R)
2013: {
2014:   const PetscInt  embedDim = dim+1;
2015:   PetscSection    coordSection;
2016:   Vec             coordinates;
2017:   PetscScalar    *coords;
2018:   PetscReal      *coordsIn;
2019:   PetscInt        numCells, numEdges, numVerts, firstVertex, v, firstEdge, coordSize, d, c, e;
2020:   PetscMPIInt     rank;
2021:   PetscErrorCode  ierr;

2025:   DMSetDimension(dm, dim);
2026:   DMSetCoordinateDim(dm, dim+1);
2027:   MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);
2028:   switch (dim) {
2029:   case 2:
2030:     if (simplex) {
2031:       const PetscReal radius    = PetscSqrtReal(1 + PETSC_PHI*PETSC_PHI)/(1.0 + PETSC_PHI);
2032:       const PetscReal edgeLen   = 2.0/(1.0 + PETSC_PHI) * (R/radius);
2033:       const PetscInt  degree    = 5;
2034:       PetscReal       vertex[3] = {0.0, 1.0/(1.0 + PETSC_PHI), PETSC_PHI/(1.0 + PETSC_PHI)};
2035:       PetscInt        s[3]      = {1, 1, 1};
2036:       PetscInt        cone[3];
2037:       PetscInt       *graph, p, i, j, k;

2039:       vertex[0] *= R/radius; vertex[1] *= R/radius; vertex[2] *= R/radius;
2040:       numCells    = !rank ? 20 : 0;
2041:       numVerts    = !rank ? 12 : 0;
2042:       firstVertex = numCells;
2043:       /* Use icosahedron, which for a R-sphere has coordinates which are all cyclic permutations of

2045:            (0, \pm 1/\phi+1, \pm \phi/\phi+1)

2047:          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
2048:          length is then given by 2/(1+\phi) = 2 * 0.38197 = 0.76393.
2049:       */
2050:       /* Construct vertices */
2051:       PetscCalloc1(numVerts * embedDim, &coordsIn);
2052:       if (!rank) {
2053:         for (p = 0, i = 0; p < embedDim; ++p) {
2054:           for (s[1] = -1; s[1] < 2; s[1] += 2) {
2055:             for (s[2] = -1; s[2] < 2; s[2] += 2) {
2056:               for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[(d+p)%embedDim]*vertex[(d+p)%embedDim];
2057:               ++i;
2058:             }
2059:           }
2060:         }
2061:       }
2062:       /* Construct graph */
2063:       PetscCalloc1(numVerts * numVerts, &graph);
2064:       for (i = 0; i < numVerts; ++i) {
2065:         for (j = 0, k = 0; j < numVerts; ++j) {
2066:           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i*embedDim], &coordsIn[j*embedDim]) - edgeLen) < PETSC_SMALL) {graph[i*numVerts+j] = 1; ++k;}
2067:         }
2068:         if (k != degree) SETERRQ3(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid icosahedron, vertex %D degree %D != %D", i, k, degree);
2069:       }
2070:       /* Build Topology */
2071:       DMPlexSetChart(dm, 0, numCells+numVerts);
2072:       for (c = 0; c < numCells; c++) {
2073:         DMPlexSetConeSize(dm, c, embedDim);
2074:       }
2075:       DMSetUp(dm); /* Allocate space for cones */
2076:       /* Cells */
2077:       for (i = 0, c = 0; i < numVerts; ++i) {
2078:         for (j = 0; j < i; ++j) {
2079:           for (k = 0; k < j; ++k) {
2080:             if (graph[i*numVerts+j] && graph[j*numVerts+k] && graph[k*numVerts+i]) {
2081:               cone[0] = firstVertex+i; cone[1] = firstVertex+j; cone[2] = firstVertex+k;
2082:               /* Check orientation */
2083:               {
2084:                 const PetscInt epsilon[3][3][3] = {{{0, 0, 0}, {0, 0, 1}, {0, -1, 0}}, {{0, 0, -1}, {0, 0, 0}, {1, 0, 0}}, {{0, 1, 0}, {-1, 0, 0}, {0, 0, 0}}};
2085:                 PetscReal normal[3];
2086:                 PetscInt  e, f;

2088:                 for (d = 0; d < embedDim; ++d) {
2089:                   normal[d] = 0.0;
2090:                   for (e = 0; e < embedDim; ++e) {
2091:                     for (f = 0; f < embedDim; ++f) {
2092:                       normal[d] += epsilon[d][e][f]*(coordsIn[j*embedDim+e] - coordsIn[i*embedDim+e])*(coordsIn[k*embedDim+f] - coordsIn[i*embedDim+f]);
2093:                     }
2094:                   }
2095:                 }
2096:                 if (DotReal(embedDim, normal, &coordsIn[i*embedDim]) < 0) {PetscInt tmp = cone[1]; cone[1] = cone[2]; cone[2] = tmp;}
2097:               }
2098:               DMPlexSetCone(dm, c++, cone);
2099:             }
2100:           }
2101:         }
2102:       }
2103:       DMPlexSymmetrize(dm);
2104:       DMPlexStratify(dm);
2105:       PetscFree(graph);
2106:     } else {
2107:       /*
2108:         12-21--13
2109:          |     |
2110:         25  4  24
2111:          |     |
2112:   12-25--9-16--8-24--13
2113:    |     |     |     |
2114:   23  5 17  0 15  3  22
2115:    |     |     |     |
2116:   10-20--6-14--7-19--11
2117:          |     |
2118:         20  1  19
2119:          |     |
2120:         10-18--11
2121:          |     |
2122:         23  2  22
2123:          |     |
2124:         12-21--13
2125:        */
2126:       PetscInt cone[4], ornt[4];

2128:       numCells    = !rank ?  6 : 0;
2129:       numEdges    = !rank ? 12 : 0;
2130:       numVerts    = !rank ?  8 : 0;
2131:       firstVertex = numCells;
2132:       firstEdge   = numCells + numVerts;
2133:       /* Build Topology */
2134:       DMPlexSetChart(dm, 0, numCells+numEdges+numVerts);
2135:       for (c = 0; c < numCells; c++) {
2136:         DMPlexSetConeSize(dm, c, 4);
2137:       }
2138:       for (e = firstEdge; e < firstEdge+numEdges; ++e) {
2139:         DMPlexSetConeSize(dm, e, 2);
2140:       }
2141:       DMSetUp(dm); /* Allocate space for cones */
2142:       if (!rank) {
2143:         /* Cell 0 */
2144:         cone[0] = 14; cone[1] = 15; cone[2] = 16; cone[3] = 17;
2145:         DMPlexSetCone(dm, 0, cone);
2146:         ornt[0] = 0; ornt[1] = 0; ornt[2] = 0; ornt[3] = 0;
2147:         DMPlexSetConeOrientation(dm, 0, ornt);
2148:         /* Cell 1 */
2149:         cone[0] = 18; cone[1] = 19; cone[2] = 14; cone[3] = 20;
2150:         DMPlexSetCone(dm, 1, cone);
2151:         ornt[0] = 0; ornt[1] = 0; ornt[2] = -2; ornt[3] = 0;
2152:         DMPlexSetConeOrientation(dm, 1, ornt);
2153:         /* Cell 2 */
2154:         cone[0] = 21; cone[1] = 22; cone[2] = 18; cone[3] = 23;
2155:         DMPlexSetCone(dm, 2, cone);
2156:         ornt[0] = 0; ornt[1] = 0; ornt[2] = -2; ornt[3] = 0;
2157:         DMPlexSetConeOrientation(dm, 2, ornt);
2158:         /* Cell 3 */
2159:         cone[0] = 19; cone[1] = 22; cone[2] = 24; cone[3] = 15;
2160:         DMPlexSetCone(dm, 3, cone);
2161:         ornt[0] = -2; ornt[1] = -2; ornt[2] = 0; ornt[3] = -2;
2162:         DMPlexSetConeOrientation(dm, 3, ornt);
2163:         /* Cell 4 */
2164:         cone[0] = 16; cone[1] = 24; cone[2] = 21; cone[3] = 25;
2165:         DMPlexSetCone(dm, 4, cone);
2166:         ornt[0] = -2; ornt[1] = -2; ornt[2] = -2; ornt[3] = 0;
2167:         DMPlexSetConeOrientation(dm, 4, ornt);
2168:         /* Cell 5 */
2169:         cone[0] = 20; cone[1] = 17; cone[2] = 25; cone[3] = 23;
2170:         DMPlexSetCone(dm, 5, cone);
2171:         ornt[0] = -2; ornt[1] = -2; ornt[2] = -2; ornt[3] = -2;
2172:         DMPlexSetConeOrientation(dm, 5, ornt);
2173:         /* Edges */
2174:         cone[0] =  6; cone[1] =  7;
2175:         DMPlexSetCone(dm, 14, cone);
2176:         cone[0] =  7; cone[1] =  8;
2177:         DMPlexSetCone(dm, 15, cone);
2178:         cone[0] =  8; cone[1] =  9;
2179:         DMPlexSetCone(dm, 16, cone);
2180:         cone[0] =  9; cone[1] =  6;
2181:         DMPlexSetCone(dm, 17, cone);
2182:         cone[0] = 10; cone[1] = 11;
2183:         DMPlexSetCone(dm, 18, cone);
2184:         cone[0] = 11; cone[1] =  7;
2185:         DMPlexSetCone(dm, 19, cone);
2186:         cone[0] =  6; cone[1] = 10;
2187:         DMPlexSetCone(dm, 20, cone);
2188:         cone[0] = 12; cone[1] = 13;
2189:         DMPlexSetCone(dm, 21, cone);
2190:         cone[0] = 13; cone[1] = 11;
2191:         DMPlexSetCone(dm, 22, cone);
2192:         cone[0] = 10; cone[1] = 12;
2193:         DMPlexSetCone(dm, 23, cone);
2194:         cone[0] = 13; cone[1] =  8;
2195:         DMPlexSetCone(dm, 24, cone);
2196:         cone[0] = 12; cone[1] =  9;
2197:         DMPlexSetCone(dm, 25, cone);
2198:       }
2199:       DMPlexSymmetrize(dm);
2200:       DMPlexStratify(dm);
2201:       /* Build coordinates */
2202:       PetscCalloc1(numVerts * embedDim, &coordsIn);
2203:       if (!rank) {
2204:         coordsIn[0*embedDim+0] = -R; coordsIn[0*embedDim+1] =  R; coordsIn[0*embedDim+2] = -R;
2205:         coordsIn[1*embedDim+0] =  R; coordsIn[1*embedDim+1] =  R; coordsIn[1*embedDim+2] = -R;
2206:         coordsIn[2*embedDim+0] =  R; coordsIn[2*embedDim+1] = -R; coordsIn[2*embedDim+2] = -R;
2207:         coordsIn[3*embedDim+0] = -R; coordsIn[3*embedDim+1] = -R; coordsIn[3*embedDim+2] = -R;
2208:         coordsIn[4*embedDim+0] = -R; coordsIn[4*embedDim+1] =  R; coordsIn[4*embedDim+2] =  R;
2209:         coordsIn[5*embedDim+0] =  R; coordsIn[5*embedDim+1] =  R; coordsIn[5*embedDim+2] =  R;
2210:         coordsIn[6*embedDim+0] = -R; coordsIn[6*embedDim+1] = -R; coordsIn[6*embedDim+2] =  R;
2211:         coordsIn[7*embedDim+0] =  R; coordsIn[7*embedDim+1] = -R; coordsIn[7*embedDim+2] =  R;
2212:       }
2213:     }
2214:     break;
2215:   case 3:
2216:     if (simplex) {
2217:       const PetscReal edgeLen         = 1.0/PETSC_PHI;
2218:       PetscReal       vertexA[4]      = {0.5, 0.5, 0.5, 0.5};
2219:       PetscReal       vertexB[4]      = {1.0, 0.0, 0.0, 0.0};
2220:       PetscReal       vertexC[4]      = {0.5, 0.5*PETSC_PHI, 0.5/PETSC_PHI, 0.0};
2221:       const PetscInt  degree          = 12;
2222:       PetscInt        s[4]            = {1, 1, 1};
2223:       PetscInt        evenPerm[12][4] = {{0, 1, 2, 3}, {0, 2, 3, 1}, {0, 3, 1, 2}, {1, 0, 3, 2}, {1, 2, 0, 3}, {1, 3, 2, 0},
2224:                                          {2, 0, 1, 3}, {2, 1, 3, 0}, {2, 3, 0, 1}, {3, 0, 2, 1}, {3, 1, 0, 2}, {3, 2, 1, 0}};
2225:       PetscInt        cone[4];
2226:       PetscInt       *graph, p, i, j, k, l;

2228:       vertexA[0] *= R; vertexA[1] *= R; vertexA[2] *= R; vertexA[3] *= R;
2229:       vertexB[0] *= R; vertexB[1] *= R; vertexB[2] *= R; vertexB[3] *= R;
2230:       vertexC[0] *= R; vertexC[1] *= R; vertexC[2] *= R; vertexC[3] *= R;
2231:       numCells    = !rank ? 600 : 0;
2232:       numVerts    = !rank ? 120 : 0;
2233:       firstVertex = numCells;
2234:       /* Use the 600-cell, which for a unit sphere has coordinates which are

2236:            1/2 (\pm 1, \pm 1,    \pm 1, \pm 1)                          16
2237:                (\pm 1,    0,       0,      0)  all cyclic permutations   8
2238:            1/2 (\pm 1, \pm phi, \pm 1/phi, 0)  all even permutations    96

2240:          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
2241:          length is then given by 1/\phi = 0.61803.

2243:          http://buzzard.pugetsound.edu/sage-practice/ch03s03.html
2244:          http://mathworld.wolfram.com/600-Cell.html
2245:       */
2246:       /* Construct vertices */
2247:       PetscCalloc1(numVerts * embedDim, &coordsIn);
2248:       i    = 0;
2249:       if (!rank) {
2250:         for (s[0] = -1; s[0] < 2; s[0] += 2) {
2251:           for (s[1] = -1; s[1] < 2; s[1] += 2) {
2252:             for (s[2] = -1; s[2] < 2; s[2] += 2) {
2253:               for (s[3] = -1; s[3] < 2; s[3] += 2) {
2254:                 for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[d]*vertexA[d];
2255:                 ++i;
2256:               }
2257:             }
2258:           }
2259:         }
2260:         for (p = 0; p < embedDim; ++p) {
2261:           s[1] = s[2] = s[3] = 1;
2262:           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2263:             for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[(d+p)%embedDim]*vertexB[(d+p)%embedDim];
2264:             ++i;
2265:           }
2266:         }
2267:         for (p = 0; p < 12; ++p) {
2268:           s[3] = 1;
2269:           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2270:             for (s[1] = -1; s[1] < 2; s[1] += 2) {
2271:               for (s[2] = -1; s[2] < 2; s[2] += 2) {
2272:                 for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[evenPerm[p][d]]*vertexC[evenPerm[p][d]];
2273:                 ++i;
2274:               }
2275:             }
2276:           }
2277:         }
2278:       }
2279:       if (i != numVerts) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertices %D != %D", i, numVerts);
2280:       /* Construct graph */
2281:       PetscCalloc1(numVerts * numVerts, &graph);
2282:       for (i = 0; i < numVerts; ++i) {
2283:         for (j = 0, k = 0; j < numVerts; ++j) {
2284:           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i*embedDim], &coordsIn[j*embedDim]) - edgeLen) < PETSC_SMALL) {graph[i*numVerts+j] = 1; ++k;}
2285:         }
2286:         if (k != degree) SETERRQ3(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertex %D degree %D != %D", i, k, degree);
2287:       }
2288:       /* Build Topology */
2289:       DMPlexSetChart(dm, 0, numCells+numVerts);
2290:       for (c = 0; c < numCells; c++) {
2291:         DMPlexSetConeSize(dm, c, embedDim);
2292:       }
2293:       DMSetUp(dm); /* Allocate space for cones */
2294:       /* Cells */
2295:       if (!rank) {
2296:         for (i = 0, c = 0; i < numVerts; ++i) {
2297:           for (j = 0; j < i; ++j) {
2298:             for (k = 0; k < j; ++k) {
2299:               for (l = 0; l < k; ++l) {
2300:                 if (graph[i*numVerts+j] && graph[j*numVerts+k] && graph[k*numVerts+i] &&
2301:                     graph[l*numVerts+i] && graph[l*numVerts+j] && graph[l*numVerts+k]) {
2302:                   cone[0] = firstVertex+i; cone[1] = firstVertex+j; cone[2] = firstVertex+k; cone[3] = firstVertex+l;
2303:                   /* Check orientation: https://ef.gy/linear-algebra:normal-vectors-in-higher-dimensional-spaces */
2304:                   {
2305:                     const PetscInt epsilon[4][4][4][4] = {{{{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
2306:                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  1}, { 0,  0, -1, 0}},
2307:                                                            {{0,  0,  0,  0}, { 0, 0,  0, -1}, { 0,  0, 0,  0}, { 0,  1,  0, 0}},
2308:                                                            {{0,  0,  0,  0}, { 0, 0,  1,  0}, { 0, -1, 0,  0}, { 0,  0,  0, 0}}},

2310:                                                           {{{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0, -1}, { 0,  0,  1, 0}},
2311:                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
2312:                                                            {{0,  0,  0,  1}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, {-1,  0,  0, 0}},
2313:                                                            {{0,  0, -1,  0}, { 0, 0,  0,  0}, { 1,  0, 0,  0}, { 0,  0,  0, 0}}},

2315:                                                           {{{0,  0,  0,  0}, { 0, 0,  0,  1}, { 0,  0, 0,  0}, { 0, -1,  0, 0}},
2316:                                                            {{0,  0,  0, -1}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 1,  0,  0, 0}},
2317:                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
2318:                                                            {{0,  1,  0,  0}, {-1, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}}},

2320:                                                           {{{0,  0,  0,  0}, { 0, 0, -1,  0}, { 0,  1, 0,  0}, { 0,  0,  0, 0}},
2321:                                                            {{0,  0,  1,  0}, { 0, 0,  0,  0}, {-1,  0, 0,  0}, { 0,  0,  0, 0}},
2322:                                                            {{0, -1,  0,  0}, { 1, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
2323:                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}}}};
2324:                     PetscReal normal[4];
2325:                     PetscInt  e, f, g;

2327:                     for (d = 0; d < embedDim; ++d) {
2328:                       normal[d] = 0.0;
2329:                       for (e = 0; e < embedDim; ++e) {
2330:                         for (f = 0; f < embedDim; ++f) {
2331:                           for (g = 0; g < embedDim; ++g) {
2332:                             normal[d] += epsilon[d][e][f][g]*(coordsIn[j*embedDim+e] - coordsIn[i*embedDim+e])*(coordsIn[k*embedDim+f] - coordsIn[i*embedDim+f])*(coordsIn[l*embedDim+f] - coordsIn[i*embedDim+f]);
2333:                           }
2334:                         }
2335:                       }
2336:                     }
2337:                     if (DotReal(embedDim, normal, &coordsIn[i*embedDim]) < 0) {PetscInt tmp = cone[1]; cone[1] = cone[2]; cone[2] = tmp;}
2338:                   }
2339:                   DMPlexSetCone(dm, c++, cone);
2340:                 }
2341:               }
2342:             }
2343:           }
2344:         }
2345:       }
2346:       DMPlexSymmetrize(dm);
2347:       DMPlexStratify(dm);
2348:       PetscFree(graph);
2349:       break;
2350:     }
2351:   default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Unsupported dimension for sphere: %D", dim);
2352:   }
2353:   /* Create coordinates */
2354:   DMGetCoordinateSection(dm, &coordSection);
2355:   PetscSectionSetNumFields(coordSection, 1);
2356:   PetscSectionSetFieldComponents(coordSection, 0, embedDim);
2357:   PetscSectionSetChart(coordSection, firstVertex, firstVertex+numVerts);
2358:   for (v = firstVertex; v < firstVertex+numVerts; ++v) {
2359:     PetscSectionSetDof(coordSection, v, embedDim);
2360:     PetscSectionSetFieldDof(coordSection, v, 0, embedDim);
2361:   }
2362:   PetscSectionSetUp(coordSection);
2363:   PetscSectionGetStorageSize(coordSection, &coordSize);
2364:   VecCreate(PETSC_COMM_SELF, &coordinates);
2365:   VecSetBlockSize(coordinates, embedDim);
2366:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
2367:   VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
2368:   VecSetType(coordinates,VECSTANDARD);
2369:   VecGetArray(coordinates, &coords);
2370:   for (v = 0; v < numVerts; ++v) for (d = 0; d < embedDim; ++d) {coords[v*embedDim+d] = coordsIn[v*embedDim+d];}
2371:   VecRestoreArray(coordinates, &coords);
2372:   DMSetCoordinatesLocal(dm, coordinates);
2373:   VecDestroy(&coordinates);
2374:   PetscFree(coordsIn);
2375:   {
2376:     DM          cdm;
2377:     PetscDS     cds;
2378:     PetscScalar c = R;

2380:     DMPlexCreateCoordinateSpace(dm, 1, snapToSphere);
2381:     DMGetCoordinateDM(dm, &cdm);
2382:     DMGetDS(cdm, &cds);
2383:     PetscDSSetConstants(cds, 1, &c);
2384:   }
2385:   /* Wait for coordinate creation before doing in-place modification */
2386:   if (simplex) {DMPlexInterpolateInPlace_Internal(dm);}
2387:   return(0);
2388: }

2390: /*@
2391:   DMPlexCreateSphereMesh - Creates a mesh on the d-dimensional sphere, S^d.

2393:   Collective

2395:   Input Parameters:
2396: + comm    - The communicator for the DM object
2397: . dim     - The dimension
2398: . simplex - Use simplices, or tensor product cells
2399: - R       - The radius

2401:   Output Parameter:
2402: . dm  - The DM object

2404:   Level: beginner

2406: .seealso: DMPlexCreateBallMesh(), DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
2407: @*/
2408: PetscErrorCode DMPlexCreateSphereMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscReal R, DM *dm)
2409: {

2414:   DMCreate(comm, dm);
2415:   DMSetType(*dm, DMPLEX);
2416:   DMPlexCreateSphereMesh_Internal(*dm, dim, simplex, R);
2417:   return(0);
2418: }

2420: static PetscErrorCode DMPlexCreateBallMesh_Internal(DM dm, PetscInt dim, PetscReal R)
2421: {
2422:   DM             sdm, vol;
2423:   DMLabel        bdlabel;

2427:   DMCreate(PetscObjectComm((PetscObject) dm), &sdm);
2428:   DMSetType(sdm, DMPLEX);
2429:   PetscObjectSetOptionsPrefix((PetscObject) sdm, "bd_");
2430:   DMPlexCreateSphereMesh_Internal(sdm, dim-1, PETSC_TRUE, R);
2431:   DMSetFromOptions(sdm);
2432:   DMViewFromOptions(sdm, NULL, "-dm_view");
2433:   DMPlexGenerate(sdm, NULL, PETSC_TRUE, &vol);
2434:   DMDestroy(&sdm);
2435:   DMPlexReplace_Static(dm, &vol);
2436:   DMCreateLabel(dm, "marker");
2437:   DMGetLabel(dm, "marker", &bdlabel);
2438:   DMPlexMarkBoundaryFaces(dm, PETSC_DETERMINE, bdlabel);
2439:   DMPlexLabelComplete(dm, bdlabel);
2440:   return(0);
2441: }

2443: /*@
2444:   DMPlexCreateBallMesh - Creates a simplex mesh on the d-dimensional ball, B^d.

2446:   Collective

2448:   Input Parameters:
2449: + comm  - The communicator for the DM object
2450: . dim   - The dimension
2451: - R     - The radius

2453:   Output Parameter:
2454: . dm  - The DM object

2456:   Options Database Keys:
2457: - bd_dm_refine - This will refine the surface mesh preserving the sphere geometry

2459:   Level: beginner

2461: .seealso: DMPlexCreateSphereMesh(), DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
2462: @*/
2463: PetscErrorCode DMPlexCreateBallMesh(MPI_Comm comm, PetscInt dim, PetscReal R, DM *dm)
2464: {

2468:   DMCreate(comm, dm);
2469:   DMSetType(*dm, DMPLEX);
2470:   DMPlexCreateBallMesh_Internal(*dm, dim, R);
2471:   return(0);
2472: }

2474: static PetscErrorCode DMPlexCreateReferenceCell_Internal(DM rdm, DMPolytopeType ct)
2475: {

2479:   switch (ct) {
2480:     case DM_POLYTOPE_POINT:
2481:     {
2482:       PetscInt    numPoints[1]        = {1};
2483:       PetscInt    coneSize[1]         = {0};
2484:       PetscInt    cones[1]            = {0};
2485:       PetscInt    coneOrientations[1] = {0};
2486:       PetscScalar vertexCoords[1]     = {0.0};

2488:       DMSetDimension(rdm, 0);
2489:       DMPlexCreateFromDAG(rdm, 0, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2490:     }
2491:     break;
2492:     case DM_POLYTOPE_SEGMENT:
2493:     {
2494:       PetscInt    numPoints[2]        = {2, 1};
2495:       PetscInt    coneSize[3]         = {2, 0, 0};
2496:       PetscInt    cones[2]            = {1, 2};
2497:       PetscInt    coneOrientations[2] = {0, 0};
2498:       PetscScalar vertexCoords[2]     = {-1.0,  1.0};

2500:       DMSetDimension(rdm, 1);
2501:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2502:     }
2503:     break;
2504:     case DM_POLYTOPE_TRIANGLE:
2505:     {
2506:       PetscInt    numPoints[2]        = {3, 1};
2507:       PetscInt    coneSize[4]         = {3, 0, 0, 0};
2508:       PetscInt    cones[3]            = {1, 2, 3};
2509:       PetscInt    coneOrientations[3] = {0, 0, 0};
2510:       PetscScalar vertexCoords[6]     = {-1.0, -1.0,  1.0, -1.0,  -1.0, 1.0};

2512:       DMSetDimension(rdm, 2);
2513:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2514:     }
2515:     break;
2516:     case DM_POLYTOPE_QUADRILATERAL:
2517:     {
2518:       PetscInt    numPoints[2]        = {4, 1};
2519:       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
2520:       PetscInt    cones[4]            = {1, 2, 3, 4};
2521:       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
2522:       PetscScalar vertexCoords[8]     = {-1.0, -1.0,  1.0, -1.0,  1.0, 1.0,  -1.0, 1.0};

2524:       DMSetDimension(rdm, 2);
2525:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2526:     }
2527:     break;
2528:     case DM_POLYTOPE_SEG_PRISM_TENSOR:
2529:     {
2530:       PetscInt    numPoints[2]        = {4, 1};
2531:       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
2532:       PetscInt    cones[4]            = {1, 2, 3, 4};
2533:       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
2534:       PetscScalar vertexCoords[8]     = {-1.0, -1.0,  1.0, -1.0,  -1.0, 1.0,  1.0, 1.0};

2536:       DMSetDimension(rdm, 2);
2537:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2538:     }
2539:     break;
2540:     case DM_POLYTOPE_TETRAHEDRON:
2541:     {
2542:       PetscInt    numPoints[2]        = {4, 1};
2543:       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
2544:       PetscInt    cones[4]            = {1, 3, 2, 4};
2545:       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
2546:       PetscScalar vertexCoords[12]    = {-1.0, -1.0, -1.0,  1.0, -1.0, -1.0,  -1.0, 1.0, -1.0,  -1.0, -1.0, 1.0};

2548:       DMSetDimension(rdm, 3);
2549:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2550:     }
2551:     break;
2552:     case DM_POLYTOPE_HEXAHEDRON:
2553:     {
2554:       PetscInt    numPoints[2]        = {8, 1};
2555:       PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
2556:       PetscInt    cones[8]            = {1, 4, 3, 2, 5, 6, 7, 8};
2557:       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
2558:       PetscScalar vertexCoords[24]    = {-1.0, -1.0, -1.0,  1.0, -1.0, -1.0,  1.0, 1.0, -1.0,  -1.0, 1.0, -1.0,
2559:                                          -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  1.0, 1.0,  1.0,  -1.0, 1.0,  1.0};

2561:       DMSetDimension(rdm, 3);
2562:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2563:     }
2564:     break;
2565:     case DM_POLYTOPE_TRI_PRISM:
2566:     {
2567:       PetscInt    numPoints[2]        = {6, 1};
2568:       PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
2569:       PetscInt    cones[6]            = {1, 3, 2, 4, 5, 6};
2570:       PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
2571:       PetscScalar vertexCoords[18]    = {-1.0, -1.0, -1.0,  1.0, -1.0, -1.0,  -1.0, 1.0, -1.0,
2572:                                          -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  -1.0, 1.0,  1.0};

2574:       DMSetDimension(rdm, 3);
2575:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2576:     }
2577:     break;
2578:     case DM_POLYTOPE_TRI_PRISM_TENSOR:
2579:     {
2580:       PetscInt    numPoints[2]        = {6, 1};
2581:       PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
2582:       PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
2583:       PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
2584:       PetscScalar vertexCoords[18]    = {-1.0, -1.0, -1.0,  1.0, -1.0, -1.0,  -1.0, 1.0, -1.0,
2585:                                          -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  -1.0, 1.0,  1.0};

2587:       DMSetDimension(rdm, 3);
2588:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2589:     }
2590:     break;
2591:     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
2592:     {
2593:       PetscInt    numPoints[2]        = {8, 1};
2594:       PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
2595:       PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
2596:       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
2597:       PetscScalar vertexCoords[24]    = {-1.0, -1.0, -1.0,  1.0, -1.0, -1.0,  1.0, 1.0, -1.0,  -1.0, 1.0, -1.0,
2598:                                          -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  1.0, 1.0,  1.0,  -1.0, 1.0,  1.0};

2600:       DMSetDimension(rdm, 3);
2601:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2602:     }
2603:     break;
2604:     case DM_POLYTOPE_PYRAMID:
2605:     {
2606:       PetscInt    numPoints[2]        = {5, 1};
2607:       PetscInt    coneSize[6]         = {5, 0, 0, 0, 0, 0};
2608:       PetscInt    cones[5]            = {1, 4, 3, 2, 5};
2609:       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
2610:       PetscScalar vertexCoords[24]    = {-1.0, -1.0, -1.0,  1.0, -1.0, -1.0,  1.0, 1.0, -1.0,  -1.0, 1.0, -1.0,
2611:                                           0.0,  0.0,  1.0};

2613:       DMSetDimension(rdm, 3);
2614:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2615:     }
2616:     break;
2617:     default: SETERRQ1(PetscObjectComm((PetscObject) rdm), PETSC_ERR_ARG_WRONG, "Cannot create reference cell for cell type %s", DMPolytopeTypes[ct]);
2618:   }
2619:   {
2620:     PetscInt Nv, v;

2622:     /* Must create the celltype label here so that we do not automatically try to compute the types */
2623:     DMCreateLabel(rdm, "celltype");
2624:     DMPlexSetCellType(rdm, 0, ct);
2625:     DMPlexGetChart(rdm, NULL, &Nv);
2626:     for (v = 1; v < Nv; ++v) {DMPlexSetCellType(rdm, v, DM_POLYTOPE_POINT);}
2627:   }
2628:   DMPlexInterpolateInPlace_Internal(rdm);
2629:   return(0);
2630: }

2632: /*@
2633:   DMPlexCreateReferenceCell - Create a DMPLEX with the appropriate FEM reference cell

2635:   Collective

2637:   Input Parameters:
2638: + comm - The communicator
2639: - ct   - The cell type of the reference cell

2641:   Output Parameter:
2642: . refdm - The reference cell

2644:   Level: intermediate

2646: .seealso: DMPlexCreateReferenceCell(), DMPlexCreateBoxMesh()
2647: @*/
2648: PetscErrorCode DMPlexCreateReferenceCell(MPI_Comm comm, DMPolytopeType ct, DM *refdm)
2649: {

2653:   DMCreate(comm, refdm);
2654:   DMSetType(*refdm, DMPLEX);
2655:   DMPlexCreateReferenceCell_Internal(*refdm, ct);
2656:   return(0);
2657: }

2659: static PetscErrorCode DMPlexCreateBoundaryLabel_Private(DM dm, const char name[])
2660: {
2661:   DM             plex;
2662:   DMLabel        label;
2663:   PetscBool      hasLabel;

2667:   DMHasLabel(dm, name, &hasLabel);
2668:   if (hasLabel) return(0);
2669:   DMCreateLabel(dm, name);
2670:   DMGetLabel(dm, name, &label);
2671:   DMConvert(dm, DMPLEX, &plex);
2672:   DMPlexMarkBoundaryFaces(plex, 1, label);
2673:   DMDestroy(&plex);
2674:   return(0);
2675: }

2677: const char * const DMPlexShapes[] = {"box", "box_surface", "ball", "sphere", "cylinder", "unknown", "DMPlexShape", "DM_SHAPE_", NULL};

2679: static PetscErrorCode DMPlexCreateFromOptions_Internal(PetscOptionItems *PetscOptionsObject, PetscBool *useCoordSpace, DM dm)
2680: {
2681:   DMPlexShape    shape = DM_SHAPE_BOX;
2682:   DMPolytopeType cell  = DM_POLYTOPE_TRIANGLE;
2683:   PetscInt       dim   = 2;
2684:   PetscBool      simplex = PETSC_TRUE, interpolate = PETSC_TRUE, adjCone = PETSC_FALSE, adjClosure = PETSC_TRUE, refDomain = PETSC_FALSE;
2685:   PetscBool      flg, flg2, fflg, bdfflg;
2686:   MPI_Comm       comm;
2687:   char           filename[PETSC_MAX_PATH_LEN], bdFilename[PETSC_MAX_PATH_LEN];

2691:   PetscObjectGetComm((PetscObject) dm, &comm);
2692:   /* TODO Turn this into a registration interface */
2693:   PetscOptionsString("-dm_plex_filename", "File containing a mesh", "DMPlexCreateFromFile", filename, filename, sizeof(filename), &fflg);
2694:   PetscOptionsString("-dm_plex_boundary_filename", "File containing a mesh boundary", "DMPlexCreateFromFile", bdFilename, bdFilename, sizeof(bdFilename), &bdfflg);
2695:   PetscOptionsEnum("-dm_plex_cell", "Cell shape", "", DMPolytopeTypes, (PetscEnum) cell, (PetscEnum *) &cell, NULL);
2696:   PetscOptionsBool("-dm_plex_reference_cell_domain", "Use a reference cell domain", "", refDomain, &refDomain, NULL);
2697:   PetscOptionsEnum("-dm_plex_shape", "Shape for built-in mesh", "", DMPlexShapes, (PetscEnum) shape, (PetscEnum *) &shape, &flg);
2698:   PetscOptionsBoundedInt("-dm_plex_dim", "Topological dimension of the mesh", "DMGetDimension", dim, &dim, &flg, 0);
2699:   if ((dim < 0) || (dim > 3)) SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Dimension %D should be in [1, 3]", dim);
2700:   PetscOptionsBool("-dm_plex_simplex", "Mesh cell shape", "", simplex,  &simplex, &flg);
2701:   PetscOptionsBool("-dm_plex_interpolate", "Flag to create edges and faces automatically", "", interpolate, &interpolate, &flg);
2702:   PetscOptionsBool("-dm_plex_adj_cone", "Set adjacency direction", "DMSetBasicAdjacency", adjCone,  &adjCone, &flg);
2703:   PetscOptionsBool("-dm_plex_adj_closure", "Set adjacency size", "DMSetBasicAdjacency", adjClosure,  &adjClosure, &flg2);
2704:   if (flg || flg2) {DMSetBasicAdjacency(dm, adjCone, adjClosure);}

2706:   switch (cell) {
2707:     case DM_POLYTOPE_POINT:
2708:     case DM_POLYTOPE_SEGMENT:
2709:     case DM_POLYTOPE_POINT_PRISM_TENSOR:
2710:     case DM_POLYTOPE_TRIANGLE:
2711:     case DM_POLYTOPE_QUADRILATERAL:
2712:     case DM_POLYTOPE_TETRAHEDRON:
2713:     case DM_POLYTOPE_HEXAHEDRON:
2714:       *useCoordSpace = PETSC_TRUE;break;
2715:     default: *useCoordSpace = PETSC_FALSE;break;
2716:   }

2718:   if (fflg) {
2719:     DM dmnew;

2721:     DMPlexCreateFromFile(PetscObjectComm((PetscObject) dm), filename, interpolate, &dmnew);
2722:     DMPlexReplace_Static(dm, &dmnew);
2723:   } else if (refDomain) {
2724:     DMPlexCreateReferenceCell_Internal(dm, cell);
2725:   } else if (bdfflg) {
2726:     DM bdm, dmnew;

2728:     DMPlexCreateFromFile(PetscObjectComm((PetscObject) dm), bdFilename, interpolate, &bdm);
2729:     PetscObjectSetOptionsPrefix((PetscObject) bdm, "bd_");
2730:     DMSetFromOptions(bdm);
2731:     DMPlexGenerate(bdm, NULL, interpolate, &dmnew);
2732:     DMDestroy(&bdm);
2733:     DMPlexReplace_Static(dm, &dmnew);
2734:   } else {
2735:     PetscObjectSetName((PetscObject) dm, DMPlexShapes[shape]);
2736:     switch (shape) {
2737:       case DM_SHAPE_BOX:
2738:       {
2739:         PetscInt       faces[3] = {0, 0, 0};
2740:         PetscReal      lower[3] = {0, 0, 0};
2741:         PetscReal      upper[3] = {1, 1, 1};
2742:         DMBoundaryType bdt[3]   = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
2743:         PetscInt       i, n;

2745:         n    = dim;
2746:         for (i = 0; i < dim; ++i) faces[i] = (dim == 1 ? 1 : 4-dim);
2747:         PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg);
2748:         n    = 3;
2749:         PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg);
2750:         if (flg && (n != dim)) SETERRQ2(comm, PETSC_ERR_ARG_SIZ, "Lower box point had %D values, should have been %D", n, dim);
2751:         n    = 3;
2752:         PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg);
2753:         if (flg && (n != dim)) SETERRQ2(comm, PETSC_ERR_ARG_SIZ, "Upper box point had %D values, should have been %D", n, dim);
2754:         n    = 3;
2755:         PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *) bdt, &n, &flg);
2756:         if (flg && (n != dim)) SETERRQ2(comm, PETSC_ERR_ARG_SIZ, "Box boundary types had %D values, should have been %D", n, dim);
2757:         switch (cell) {
2758:           case DM_POLYTOPE_TRI_PRISM_TENSOR:
2759:             DMPlexCreateWedgeBoxMesh_Internal(dm, faces, lower, upper, bdt, PETSC_FALSE, interpolate);
2760:             break;
2761:           default:
2762:             DMPlexCreateBoxMesh_Internal(dm, dim, simplex, faces, lower, upper, bdt, interpolate);
2763:             break;
2764:         }
2765:       }
2766:       break;
2767:       case DM_SHAPE_BOX_SURFACE:
2768:       {
2769:         PetscInt  faces[3] = {0, 0, 0};
2770:         PetscReal lower[3] = {0, 0, 0};
2771:         PetscReal upper[3] = {1, 1, 1};
2772:         PetscInt  i, n;

2774:         n    = dim+1;
2775:         for (i = 0; i < dim+1; ++i) faces[i] = (dim+1 == 1 ? 1 : 4-(dim+1));
2776:         PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg);
2777:         n    = 3;
2778:         PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg);
2779:         if (flg && (n != dim+1)) SETERRQ2(comm, PETSC_ERR_ARG_SIZ, "Lower box point had %D values, should have been %D", n, dim+1);
2780:         n    = 3;
2781:         PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg);
2782:         if (flg && (n != dim+1)) SETERRQ2(comm, PETSC_ERR_ARG_SIZ, "Upper box point had %D values, should have been %D", n, dim+1);
2783:         DMPlexCreateBoxSurfaceMesh_Internal(dm, dim+1, faces, lower, upper, interpolate);
2784:       }
2785:       break;
2786:       case DM_SHAPE_SPHERE:
2787:       {
2788:         PetscReal R = 1.0;

2790:         PetscOptionsReal("-dm_plex_sphere_radius", "Radius of the sphere", "", R,  &R, &flg);
2791:         DMPlexCreateSphereMesh_Internal(dm, dim, simplex, R);
2792:       }
2793:       break;
2794:       case DM_SHAPE_BALL:
2795:       {
2796:         PetscReal R = 1.0;

2798:         PetscOptionsReal("-dm_plex_ball_radius", "Radius of the ball", "", R,  &R, &flg);
2799:         DMPlexCreateBallMesh_Internal(dm, dim, R);
2800:       }
2801:       break;
2802:       case DM_SHAPE_CYLINDER:
2803:       {
2804:         DMBoundaryType bdt = DM_BOUNDARY_NONE;
2805:         PetscInt       Nw  = 6;

2807:         PetscOptionsEnum("-dm_plex_cylinder_bd", "Boundary type in the z direction", "", DMBoundaryTypes, (PetscEnum) bdt, (PetscEnum *) &bdt, NULL);
2808:         PetscOptionsInt("-dm_plex_cylinder_num_wedges", "Number of wedges around the cylinder", "", Nw, &Nw, NULL);
2809:         switch (cell) {
2810:           case DM_POLYTOPE_TRI_PRISM_TENSOR:
2811:             DMPlexCreateWedgeCylinderMesh_Internal(dm, Nw, interpolate);
2812:             break;
2813:           default:
2814:             DMPlexCreateHexCylinderMesh_Internal(dm, bdt);
2815:             break;
2816:         }
2817:       }
2818:       break;
2819:       default: SETERRQ1(comm, PETSC_ERR_SUP, "Domain shape %s is unsupported", DMPlexShapes[shape]);
2820:     }
2821:   }
2822:   DMPlexSetRefinementUniform(dm, PETSC_TRUE);
2823:   return(0);
2824: }

2826: PetscErrorCode DMSetFromOptions_NonRefinement_Plex(PetscOptionItems *PetscOptionsObject, DM dm)
2827: {
2828:   DM_Plex       *mesh = (DM_Plex*) dm->data;
2829:   PetscBool      flg;
2830:   char           bdLabel[PETSC_MAX_PATH_LEN];

2834:   /* Handle viewing */
2835:   PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMPlexMatSetClosure", PETSC_FALSE, &mesh->printSetValues, NULL);
2836:   PetscOptionsBoundedInt("-dm_plex_print_fem", "Debug output level all fem computations", "DMPlexSNESComputeResidualFEM", 0, &mesh->printFEM, NULL,0);
2837:   PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMPlexSNESComputeResidualFEM", mesh->printTol, &mesh->printTol, NULL);
2838:   PetscOptionsBoundedInt("-dm_plex_print_l2", "Debug output level all L2 diff computations", "DMComputeL2Diff", 0, &mesh->printL2, NULL,0);
2839:   DMMonitorSetFromOptions(dm, "-dm_plex_monitor_throughput", "Monitor the simulation throughput", "DMPlexMonitorThroughput", DMPlexMonitorThroughput, NULL, &flg);
2840:   if (flg) {PetscLogDefaultBegin();}
2841:   /* Labeling */
2842:   PetscOptionsString("-dm_plex_boundary_label", "Label to mark the mesh boundary", "", bdLabel, bdLabel, sizeof(bdLabel), &flg);
2843:   if (flg) {DMPlexCreateBoundaryLabel_Private(dm, bdLabel);}
2844:   /* Point Location */
2845:   PetscOptionsBool("-dm_plex_hash_location", "Use grid hashing for point location", "DMInterpolate", PETSC_FALSE, &mesh->useHashLocation, NULL);
2846:   /* Partitioning and distribution */
2847:   PetscOptionsBool("-dm_plex_partition_balance", "Attempt to evenly divide points on partition boundary between processes", "DMPlexSetPartitionBalance", PETSC_FALSE, &mesh->partitionBalance, NULL);
2848:   /* Generation and remeshing */
2849:   PetscOptionsBool("-dm_plex_remesh_bd", "Allow changes to the boundary on remeshing", "DMAdapt", PETSC_FALSE, &mesh->remeshBd, NULL);
2850:   /* Projection behavior */
2851:   PetscOptionsBoundedInt("-dm_plex_max_projection_height", "Maxmimum mesh point height used to project locally", "DMPlexSetMaxProjectionHeight", 0, &mesh->maxProjectionHeight, NULL,0);
2852:   PetscOptionsBool("-dm_plex_regular_refinement", "Use special nested projection algorithm for regular refinement", "DMPlexSetRegularRefinement", mesh->regularRefinement, &mesh->regularRefinement, NULL);
2853:   PetscOptionsEnum("-dm_plex_cell_refiner", "Strategy for cell refinment", "", DMPlexCellRefinerTypes, (PetscEnum) mesh->cellRefiner, (PetscEnum *) &mesh->cellRefiner, NULL);
2854:   /* Checking structure */
2855:   {
2856:     PetscBool   flg = PETSC_FALSE, flg2 = PETSC_FALSE, all = PETSC_FALSE;

2858:     PetscOptionsBool("-dm_plex_check_all", "Perform all checks", NULL, PETSC_FALSE, &all, &flg2);
2859:     PetscOptionsBool("-dm_plex_check_symmetry", "Check that the adjacency information in the mesh is symmetric", "DMPlexCheckSymmetry", PETSC_FALSE, &flg, &flg2);
2860:     if (all || (flg && flg2)) {DMPlexCheckSymmetry(dm);}
2861:     PetscOptionsBool("-dm_plex_check_skeleton", "Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes)", "DMPlexCheckSkeleton", PETSC_FALSE, &flg, &flg2);
2862:     if (all || (flg && flg2)) {DMPlexCheckSkeleton(dm, 0);}
2863:     PetscOptionsBool("-dm_plex_check_faces", "Check that the faces of each cell give a vertex order this is consistent with what we expect from the cell type", "DMPlexCheckFaces", PETSC_FALSE, &flg, &flg2);
2864:     if (all || (flg && flg2)) {DMPlexCheckFaces(dm, 0);}
2865:     PetscOptionsBool("-dm_plex_check_geometry", "Check that cells have positive volume", "DMPlexCheckGeometry", PETSC_FALSE, &flg, &flg2);
2866:     if (all || (flg && flg2)) {DMPlexCheckGeometry(dm);}
2867:     PetscOptionsBool("-dm_plex_check_pointsf", "Check some necessary conditions for PointSF", "DMPlexCheckPointSF", PETSC_FALSE, &flg, &flg2);
2868:     if (all || (flg && flg2)) {DMPlexCheckPointSF(dm);}
2869:     PetscOptionsBool("-dm_plex_check_interface_cones", "Check points on inter-partition interfaces have conforming order of cone points", "DMPlexCheckInterfaceCones", PETSC_FALSE, &flg, &flg2);
2870:     if (all || (flg && flg2)) {DMPlexCheckInterfaceCones(dm);}
2871:     PetscOptionsBool("-dm_plex_check_cell_shape", "Check cell shape", "DMPlexCheckCellShape", PETSC_FALSE, &flg, &flg2);
2872:     if (flg && flg2) {DMPlexCheckCellShape(dm, PETSC_TRUE, PETSC_DETERMINE);}
2873:   }
2874:   {
2875:     PetscReal scale = 1.0;

2877:     PetscOptionsReal("-dm_plex_scale", "Scale factor for mesh coordinates", "DMPlexScale", scale, &scale, &flg);
2878:     if (flg) {
2879:       Vec coordinates, coordinatesLocal;

2881:       DMGetCoordinates(dm, &coordinates);
2882:       DMGetCoordinatesLocal(dm, &coordinatesLocal);
2883:       VecScale(coordinates, scale);
2884:       VecScale(coordinatesLocal, scale);
2885:     }
2886:   }
2887:   PetscPartitionerSetFromOptions(mesh->partitioner);
2888:   return(0);
2889: }

2891: static PetscErrorCode DMSetFromOptions_Plex(PetscOptionItems *PetscOptionsObject,DM dm)
2892: {
2893:   PetscReal      volume = -1.0, extThickness = 1.0;
2894:   PetscInt       prerefine = 0, refine = 0, r, coarsen = 0, overlap = 0, extLayers = 0, dim;
2895:   PetscBool      uniformOrig, created = PETSC_FALSE, uniform = PETSC_TRUE, distribute = PETSC_FALSE, interpolate = PETSC_TRUE, extColOrder = PETSC_TRUE, coordSpace = PETSC_TRUE, remap = PETSC_TRUE, ghostCells = PETSC_FALSE, isHierarchy, flg;

2900:   PetscOptionsHead(PetscOptionsObject,"DMPlex Options");
2901:   /* Handle automatic creation */
2902:   DMGetDimension(dm, &dim);
2903:   if (dim < 0) {DMPlexCreateFromOptions_Internal(PetscOptionsObject, &coordSpace, dm);created = PETSC_TRUE;}
2904:   /* Handle DMPlex refinement before distribution */
2905:   DMPlexGetRefinementUniform(dm, &uniformOrig);
2906:   PetscOptionsBoundedInt("-dm_refine_pre", "The number of refinements before distribution", "DMCreate", prerefine, &prerefine, NULL,0);
2907:   PetscOptionsBool("-dm_refine_remap_pre", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL);
2908:   PetscOptionsBool("-dm_refine_uniform_pre", "Flag for uniform refinement before distribution", "DMCreate", uniform, &uniform, &flg);
2909:   if (flg) {DMPlexSetRefinementUniform(dm, uniform);}
2910:   PetscOptionsReal("-dm_refine_volume_limit_pre", "The maximum cell volume after refinement before distribution", "DMCreate", volume, &volume, &flg);
2911:   if (flg) {
2912:     DMPlexSetRefinementUniform(dm, PETSC_FALSE);
2913:     DMPlexSetRefinementLimit(dm, volume);
2914:     prerefine = PetscMax(prerefine, 1);
2915:   }
2916:   for (r = 0; r < prerefine; ++r) {
2917:     DM             rdm;
2918:     PetscPointFunc coordFunc = ((DM_Plex*) dm->data)->coordFunc;

2920:     DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
2921:     DMRefine(dm, PetscObjectComm((PetscObject) dm), &rdm);
2922:     DMPlexReplace_Static(dm, &rdm);
2923:     DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
2924:     if (coordFunc && remap) {
2925:       DMPlexRemapGeometry(dm, 0.0, coordFunc);
2926:       ((DM_Plex*) dm->data)->coordFunc = coordFunc;
2927:     }
2928:   }
2929:   DMPlexSetRefinementUniform(dm, uniformOrig);
2930:   /* Handle DMPlex extrusion before distribution */
2931:   PetscOptionsBool("-dm_plex_interpolate", "Flag to create edges and faces automatically", "", interpolate, &interpolate, NULL);
2932:   PetscOptionsBoundedInt("-dm_extrude_layers", "The number of layers to extrude", "", extLayers, &extLayers, NULL, 0);
2933:   PetscOptionsReal("-dm_extrude_thickness", "The thickness of the layer to be extruded", "", extThickness, &extThickness, NULL);
2934:   PetscOptionsBool("-dm_extrude_column_first", "Order the cells in a vertical column first", "", extColOrder, &extColOrder, NULL);
2935:   if (extLayers) {
2936:     DM edm;

2938:     DMPlexExtrude(dm, extLayers, extThickness, extColOrder, NULL, interpolate, &edm);
2939:     DMPlexReplace_Static(dm, &edm);
2940:   }
2941:   /* Handle DMPlex distribution */
2942:   PetscOptionsBool("-dm_distribute", "Flag to redistribute a mesh among processes", "DMCreate", distribute, &distribute, NULL);
2943:   PetscOptionsBoundedInt("-dm_distribute_overlap", "The size of the overlap halo", "DMCreate", overlap, &overlap, NULL, 0);
2944:   if (distribute) {
2945:     DM               pdm = NULL;
2946:     PetscPartitioner part;

2948:     DMPlexGetPartitioner(dm, &part);
2949:     PetscPartitionerSetFromOptions(part);
2950:     DMPlexDistribute(dm, overlap, NULL, &pdm);
2951:     if (pdm) {
2952:       DMPlexReplace_Static(dm, &pdm);
2953:     }
2954:   }
2955:   /* Create coordinate space */
2956:   if (created) {
2957:     DM_Plex  *mesh = (DM_Plex *) dm->data;
2958:     PetscInt  degree = 1;
2959:     PetscBool periodic, flg;

2961:     PetscOptionsBool("-dm_coord_space", "Use an FEM space for coordinates", "", coordSpace, &coordSpace, &flg);
2962:     PetscOptionsInt("-dm_coord_petscspace_degree", "FEM degree for coordinate space", "", degree, &degree, NULL);
2963:     if (coordSpace) {DMPlexCreateCoordinateSpace(dm, degree, mesh->coordFunc);}
2964:     if (flg && !coordSpace) {
2965:       DM           cdm;
2966:       PetscDS      cds;
2967:       PetscObject  obj;
2968:       PetscClassId id;

2970:       DMGetCoordinateDM(dm, &cdm);
2971:       DMGetDS(cdm, &cds);
2972:       PetscDSGetDiscretization(cds, 0, &obj);
2973:       PetscObjectGetClassId(obj, &id);
2974:       if (id == PETSCFE_CLASSID) {
2975:         PetscContainer dummy;

2977:         PetscContainerCreate(PETSC_COMM_SELF, &dummy);
2978:         PetscObjectSetName((PetscObject) dummy, "coordinates");
2979:         DMSetField(cdm, 0, NULL, (PetscObject) dummy);
2980:         PetscContainerDestroy(&dummy);
2981:         DMClearDS(cdm);
2982:       }
2983:       mesh->coordFunc = NULL;
2984:     }
2985:     DMLocalizeCoordinates(dm);
2986:     DMGetPeriodicity(dm, &periodic, NULL, NULL, NULL);
2987:     if (periodic) {DMSetPeriodicity(dm, PETSC_TRUE, NULL, NULL, NULL);}
2988:   }
2989:   /* Handle DMPlex refinement */
2990:   remap = PETSC_TRUE;
2991:   PetscOptionsBoundedInt("-dm_refine", "The number of uniform refinements", "DMCreate", refine, &refine, NULL,0);
2992:   PetscOptionsBool("-dm_refine_remap", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL);
2993:   PetscOptionsBoundedInt("-dm_refine_hierarchy", "The number of uniform refinements", "DMCreate", refine, &refine, &isHierarchy,0);
2994:   if (refine) {DMPlexSetRefinementUniform(dm, PETSC_TRUE);}
2995:   if (refine && isHierarchy) {
2996:     DM *dms, coarseDM;

2998:     DMGetCoarseDM(dm, &coarseDM);
2999:     PetscObjectReference((PetscObject)coarseDM);
3000:     PetscMalloc1(refine,&dms);
3001:     DMRefineHierarchy(dm, refine, dms);
3002:     /* Total hack since we do not pass in a pointer */
3003:     DMPlexSwap_Static(dm, dms[refine-1]);
3004:     if (refine == 1) {
3005:       DMSetCoarseDM(dm, dms[0]);
3006:       DMPlexSetRegularRefinement(dm, PETSC_TRUE);
3007:     } else {
3008:       DMSetCoarseDM(dm, dms[refine-2]);
3009:       DMPlexSetRegularRefinement(dm, PETSC_TRUE);
3010:       DMSetCoarseDM(dms[0], dms[refine-1]);
3011:       DMPlexSetRegularRefinement(dms[0], PETSC_TRUE);
3012:     }
3013:     DMSetCoarseDM(dms[refine-1], coarseDM);
3014:     PetscObjectDereference((PetscObject)coarseDM);
3015:     /* Free DMs */
3016:     for (r = 0; r < refine; ++r) {
3017:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]);
3018:       DMDestroy(&dms[r]);
3019:     }
3020:     PetscFree(dms);
3021:   } else {
3022:     for (r = 0; r < refine; ++r) {
3023:       DM             rdm;
3024:       PetscPointFunc coordFunc = ((DM_Plex*) dm->data)->coordFunc;

3026:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
3027:       DMRefine(dm, PetscObjectComm((PetscObject) dm), &rdm);
3028:       /* Total hack since we do not pass in a pointer */
3029:       DMPlexReplace_Static(dm, &rdm);
3030:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
3031:       if (coordFunc && remap) {
3032:         DMPlexRemapGeometry(dm, 0.0, coordFunc);
3033:         ((DM_Plex*) dm->data)->coordFunc = coordFunc;
3034:       }
3035:     }
3036:   }
3037:   /* Handle DMPlex coarsening */
3038:   PetscOptionsBoundedInt("-dm_coarsen", "Coarsen the mesh", "DMCreate", coarsen, &coarsen, NULL,0);
3039:   PetscOptionsBoundedInt("-dm_coarsen_hierarchy", "The number of coarsenings", "DMCreate", coarsen, &coarsen, &isHierarchy,0);
3040:   if (coarsen && isHierarchy) {
3041:     DM *dms;

3043:     PetscMalloc1(coarsen, &dms);
3044:     DMCoarsenHierarchy(dm, coarsen, dms);
3045:     /* Free DMs */
3046:     for (r = 0; r < coarsen; ++r) {
3047:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]);
3048:       DMDestroy(&dms[r]);
3049:     }
3050:     PetscFree(dms);
3051:   } else {
3052:     for (r = 0; r < coarsen; ++r) {
3053:       DM             cdm;
3054:       PetscPointFunc coordFunc = ((DM_Plex*) dm->data)->coordFunc;

3056:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
3057:       DMCoarsen(dm, PetscObjectComm((PetscObject) dm), &cdm);
3058:       /* Total hack since we do not pass in a pointer */
3059:       DMPlexReplace_Static(dm, &cdm);
3060:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
3061:       if (coordFunc) {
3062:         DMPlexRemapGeometry(dm, 0.0, coordFunc);
3063:         ((DM_Plex*) dm->data)->coordFunc = coordFunc;
3064:       }
3065:     }
3066:   }
3067:   /* Handle ghost cells */
3068:   PetscOptionsBool("-dm_plex_create_fv_ghost_cells", "Flag to create finite volume ghost cells on the boundary", "DMCreate", ghostCells, &ghostCells, NULL);
3069:   if (ghostCells) {
3070:     DM   gdm;
3071:     char lname[PETSC_MAX_PATH_LEN];

3073:     lname[0] = '\0';
3074:     PetscOptionsString("-dm_plex_fv_ghost_cells_label", "Label name for ghost cells boundary", "DMCreate", lname, lname, sizeof(lname), &flg);
3075:     DMPlexConstructGhostCells(dm, flg ? lname : NULL, NULL, &gdm);
3076:     DMPlexReplace_Static(dm, &gdm);
3077:   }
3078:   /* Handle */
3079:   DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
3080:   PetscOptionsTail();
3081:   return(0);
3082: }

3084: static PetscErrorCode DMCreateGlobalVector_Plex(DM dm,Vec *vec)
3085: {

3089:   DMCreateGlobalVector_Section_Private(dm,vec);
3090:   /* VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM); */
3091:   VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex);
3092:   VecSetOperation(*vec, VECOP_VIEWNATIVE, (void (*)(void)) VecView_Plex_Native);
3093:   VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex);
3094:   VecSetOperation(*vec, VECOP_LOADNATIVE, (void (*)(void)) VecLoad_Plex_Native);
3095:   return(0);
3096: }

3098: static PetscErrorCode DMCreateLocalVector_Plex(DM dm,Vec *vec)
3099: {

3103:   DMCreateLocalVector_Section_Private(dm,vec);
3104:   VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex_Local);
3105:   VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex_Local);
3106:   return(0);
3107: }

3109: static PetscErrorCode DMGetDimPoints_Plex(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
3110: {
3111:   PetscInt       depth, d;

3115:   DMPlexGetDepth(dm, &depth);
3116:   if (depth == 1) {
3117:     DMGetDimension(dm, &d);
3118:     if (dim == 0)      {DMPlexGetDepthStratum(dm, dim, pStart, pEnd);}
3119:     else if (dim == d) {DMPlexGetDepthStratum(dm, 1, pStart, pEnd);}
3120:     else               {*pStart = 0; *pEnd = 0;}
3121:   } else {
3122:     DMPlexGetDepthStratum(dm, dim, pStart, pEnd);
3123:   }
3124:   return(0);
3125: }

3127: static PetscErrorCode DMGetNeighbors_Plex(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
3128: {
3129:   PetscSF           sf;
3130:   PetscInt          niranks, njranks, n;
3131:   const PetscMPIInt *iranks, *jranks;
3132:   DM_Plex           *data = (DM_Plex*) dm->data;
3133:   PetscErrorCode    ierr;

3136:   DMGetPointSF(dm, &sf);
3137:   if (!data->neighbors) {
3138:     PetscSFSetUp(sf);
3139:     PetscSFGetRootRanks(sf, &njranks, &jranks, NULL, NULL, NULL);
3140:     PetscSFGetLeafRanks(sf, &niranks, &iranks, NULL, NULL);
3141:     PetscMalloc1(njranks + niranks + 1, &data->neighbors);
3142:     PetscArraycpy(data->neighbors + 1, jranks, njranks);
3143:     PetscArraycpy(data->neighbors + njranks + 1, iranks, niranks);
3144:     n = njranks + niranks;
3145:     PetscSortRemoveDupsMPIInt(&n, data->neighbors + 1);
3146:     /* The following cast should never fail: can't have more neighbors than PETSC_MPI_INT_MAX */
3147:     PetscMPIIntCast(n, data->neighbors);
3148:   }
3149:   if (nranks) *nranks = data->neighbors[0];
3150:   if (ranks) {
3151:     if (data->neighbors[0]) *ranks = data->neighbors + 1;
3152:     else                    *ranks = NULL;
3153:   }
3154:   return(0);
3155: }

3157: PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM, DM, Mat, Vec, Vec);

3159: static PetscErrorCode DMInitialize_Plex(DM dm)
3160: {

3164:   dm->ops->view                            = DMView_Plex;
3165:   dm->ops->load                            = DMLoad_Plex;
3166:   dm->ops->setfromoptions                  = DMSetFromOptions_Plex;
3167:   dm->ops->clone                           = DMClone_Plex;
3168:   dm->ops->setup                           = DMSetUp_Plex;
3169:   dm->ops->createlocalsection              = DMCreateLocalSection_Plex;
3170:   dm->ops->createdefaultconstraints        = DMCreateDefaultConstraints_Plex;
3171:   dm->ops->createglobalvector              = DMCreateGlobalVector_Plex;
3172:   dm->ops->createlocalvector               = DMCreateLocalVector_Plex;
3173:   dm->ops->getlocaltoglobalmapping         = NULL;
3174:   dm->ops->createfieldis                   = NULL;
3175:   dm->ops->createcoordinatedm              = DMCreateCoordinateDM_Plex;
3176:   dm->ops->createcoordinatefield           = DMCreateCoordinateField_Plex;
3177:   dm->ops->getcoloring                     = NULL;
3178:   dm->ops->creatematrix                    = DMCreateMatrix_Plex;
3179:   dm->ops->createinterpolation             = DMCreateInterpolation_Plex;
3180:   dm->ops->createmassmatrix                = DMCreateMassMatrix_Plex;
3181:   dm->ops->createinjection                 = DMCreateInjection_Plex;
3182:   dm->ops->refine                          = DMRefine_Plex;
3183:   dm->ops->coarsen                         = DMCoarsen_Plex;
3184:   dm->ops->refinehierarchy                 = DMRefineHierarchy_Plex;
3185:   dm->ops->coarsenhierarchy                = DMCoarsenHierarchy_Plex;
3186:   dm->ops->adaptlabel                      = DMAdaptLabel_Plex;
3187:   dm->ops->adaptmetric                     = DMAdaptMetric_Plex;
3188:   dm->ops->globaltolocalbegin              = NULL;
3189:   dm->ops->globaltolocalend                = NULL;
3190:   dm->ops->localtoglobalbegin              = NULL;
3191:   dm->ops->localtoglobalend                = NULL;
3192:   dm->ops->destroy                         = DMDestroy_Plex;
3193:   dm->ops->createsubdm                     = DMCreateSubDM_Plex;
3194:   dm->ops->createsuperdm                   = DMCreateSuperDM_Plex;
3195:   dm->ops->getdimpoints                    = DMGetDimPoints_Plex;
3196:   dm->ops->locatepoints                    = DMLocatePoints_Plex;
3197:   dm->ops->projectfunctionlocal            = DMProjectFunctionLocal_Plex;
3198:   dm->ops->projectfunctionlabellocal       = DMProjectFunctionLabelLocal_Plex;
3199:   dm->ops->projectfieldlocal               = DMProjectFieldLocal_Plex;
3200:   dm->ops->projectfieldlabellocal          = DMProjectFieldLabelLocal_Plex;
3201:   dm->ops->projectbdfieldlabellocal        = DMProjectBdFieldLabelLocal_Plex;
3202:   dm->ops->computel2diff                   = DMComputeL2Diff_Plex;
3203:   dm->ops->computel2gradientdiff           = DMComputeL2GradientDiff_Plex;
3204:   dm->ops->computel2fielddiff              = DMComputeL2FieldDiff_Plex;
3205:   dm->ops->getneighbors                    = DMGetNeighbors_Plex;
3206:   PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C",DMPlexInsertBoundaryValues_Plex);
3207:   PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertTimeDerviativeBoundaryValues_C",DMPlexInsertTimeDerivativeBoundaryValues_Plex);
3208:   PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",DMSetUpGLVisViewer_Plex);
3209:   PetscObjectComposeFunction((PetscObject)dm,"DMCreateNeumannOverlap_C",DMCreateNeumannOverlap_Plex);
3210:   PetscObjectComposeFunction((PetscObject)dm,"DMPlexGetOverlap_C",DMPlexGetOverlap_Plex);
3211:   PetscObjectComposeFunction((PetscObject)dm,"DMInterpolateSolution_C",DMInterpolateSolution_Plex);
3212:   return(0);
3213: }

3215: PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm)
3216: {
3217:   DM_Plex        *mesh = (DM_Plex *) dm->data;

3221:   mesh->refct++;
3222:   (*newdm)->data = mesh;
3223:   PetscObjectChangeTypeName((PetscObject) *newdm, DMPLEX);
3224:   DMInitialize_Plex(*newdm);
3225:   return(0);
3226: }

3228: /*MC
3229:   DMPLEX = "plex" - A DM object that encapsulates an unstructured mesh, or CW Complex, which can be expressed using a Hasse Diagram.
3230:                     In the local representation, Vecs contain all unknowns in the interior and shared boundary. This is
3231:                     specified by a PetscSection object. Ownership in the global representation is determined by
3232:                     ownership of the underlying DMPlex points. This is specified by another PetscSection object.

3234:   Options Database Keys:
3235: + -dm_refine_pre                     - Refine mesh before distribution
3236: + -dm_refine_uniform_pre             - Choose uniform or generator-based refinement
3237: + -dm_refine_volume_limit_pre        - Cell volume limit after pre-refinement using generator
3238: . -dm_distribute                     - Distribute mesh across processes
3239: . -dm_distribute_overlap             - Number of cells to overlap for distribution
3240: . -dm_refine                         - Refine mesh after distribution
3241: . -dm_plex_hash_location             - Use grid hashing for point location
3242: . -dm_plex_partition_balance         - Attempt to evenly divide points on partition boundary between processes
3243: . -dm_plex_remesh_bd                 - Allow changes to the boundary on remeshing
3244: . -dm_plex_max_projection_height     - Maxmimum mesh point height used to project locally
3245: . -dm_plex_regular_refinement        - Use special nested projection algorithm for regular refinement
3246: . -dm_plex_check_all                 - Perform all shecks below
3247: . -dm_plex_check_symmetry            - Check that the adjacency information in the mesh is symmetric
3248: . -dm_plex_check_skeleton <celltype> - Check that each cell has the correct number of vertices
3249: . -dm_plex_check_faces <celltype>    - Check that the faces of each cell give a vertex order this is consistent with what we expect from the cell type
3250: . -dm_plex_check_geometry            - Check that cells have positive volume
3251: . -dm_view :mesh.tex:ascii_latex     - View the mesh in LaTeX/TikZ
3252: . -dm_plex_view_scale <num>          - Scale the TikZ
3253: - -dm_plex_print_fem <num>           - View FEM assembly information, such as element vectors and matrices

3255:   Level: intermediate

3257: .seealso: DMType, DMPlexCreate(), DMCreate(), DMSetType()
3258: M*/

3260: PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm)
3261: {
3262:   DM_Plex       *mesh;
3263:   PetscInt       unit;

3268:   PetscNewLog(dm,&mesh);
3269:   dm->data = mesh;

3271:   mesh->refct             = 1;
3272:   PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection);
3273:   mesh->maxConeSize       = 0;
3274:   mesh->cones             = NULL;
3275:   mesh->coneOrientations  = NULL;
3276:   PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection);
3277:   mesh->maxSupportSize    = 0;
3278:   mesh->supports          = NULL;
3279:   mesh->refinementUniform = PETSC_TRUE;
3280:   mesh->refinementLimit   = -1.0;
3281:   mesh->interpolated      = DMPLEX_INTERPOLATED_INVALID;
3282:   mesh->interpolatedCollective = DMPLEX_INTERPOLATED_INVALID;

3284:   mesh->facesTmp = NULL;

3286:   mesh->tetgenOpts   = NULL;
3287:   mesh->triangleOpts = NULL;
3288:   PetscPartitionerCreate(PetscObjectComm((PetscObject)dm), &mesh->partitioner);
3289:   mesh->remeshBd     = PETSC_FALSE;

3291:   mesh->subpointMap = NULL;

3293:   for (unit = 0; unit < NUM_PETSC_UNITS; ++unit) mesh->scale[unit] = 1.0;

3295:   mesh->regularRefinement   = PETSC_FALSE;
3296:   mesh->depthState          = -1;
3297:   mesh->celltypeState       = -1;
3298:   mesh->globalVertexNumbers = NULL;
3299:   mesh->globalCellNumbers   = NULL;
3300:   mesh->anchorSection       = NULL;
3301:   mesh->anchorIS            = NULL;
3302:   mesh->createanchors       = NULL;
3303:   mesh->computeanchormatrix = NULL;
3304:   mesh->parentSection       = NULL;
3305:   mesh->parents             = NULL;
3306:   mesh->childIDs            = NULL;
3307:   mesh->childSection        = NULL;
3308:   mesh->children            = NULL;
3309:   mesh->referenceTree       = NULL;
3310:   mesh->getchildsymmetry    = NULL;
3311:   mesh->vtkCellHeight       = 0;
3312:   mesh->useAnchors          = PETSC_FALSE;

3314:   mesh->maxProjectionHeight = 0;

3316:   mesh->neighbors           = NULL;

3318:   mesh->printSetValues = PETSC_FALSE;
3319:   mesh->printFEM       = 0;
3320:   mesh->printTol       = 1.0e-10;

3322:   DMInitialize_Plex(dm);
3323:   return(0);
3324: }

3326: /*@
3327:   DMPlexCreate - Creates a DMPlex object, which encapsulates an unstructured mesh, or CW complex, which can be expressed using a Hasse Diagram.

3329:   Collective

3331:   Input Parameter:
3332: . comm - The communicator for the DMPlex object

3334:   Output Parameter:
3335: . mesh  - The DMPlex object

3337:   Level: beginner

3339: @*/
3340: PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh)
3341: {

3346:   DMCreate(comm, mesh);
3347:   DMSetType(*mesh, DMPLEX);
3348:   return(0);
3349: }

3351: /*@C
3352:   DMPlexBuildFromCellListParallel - Build distributed DMPLEX topology from a list of vertices for each cell (common mesh generator output)

3354:   Input Parameters:
3355: + dm - The DM
3356: . numCells - The number of cells owned by this process
3357: . numVertices - The number of vertices owned by this process, or PETSC_DECIDE
3358: . NVertices - The global number of vertices, or PETSC_DECIDE
3359: . numCorners - The number of vertices for each cell
3360: - cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell

3362:   Output Parameter:
3363: . vertexSF - (Optional) SF describing complete vertex ownership

3365:   Notes:
3366:   Two triangles sharing a face
3367: $
3368: $        2
3369: $      / | \
3370: $     /  |  \
3371: $    /   |   \
3372: $   0  0 | 1  3
3373: $    \   |   /
3374: $     \  |  /
3375: $      \ | /
3376: $        1
3377: would have input
3378: $  numCells = 2, numVertices = 4
3379: $  cells = [0 1 2  1 3 2]
3380: $
3381: which would result in the DMPlex
3382: $
3383: $        4
3384: $      / | \
3385: $     /  |  \
3386: $    /   |   \
3387: $   2  0 | 1  5
3388: $    \   |   /
3389: $     \  |  /
3390: $      \ | /
3391: $        3

3393:   Vertices are implicitly numbered consecutively 0,...,NVertices.
3394:   Each rank owns a chunk of numVertices consecutive vertices.
3395:   If numVertices is PETSC_DECIDE, PETSc will distribute them as evenly as possible using PetscLayout.
3396:   If both NVertices and numVertices are PETSC_DECIDE, NVertices is computed by PETSc as the maximum vertex index in cells + 1.
3397:   If only NVertices is PETSC_DECIDE, it is computed as the sum of numVertices over all ranks.

3399:   The cell distribution is arbitrary non-overlapping, independent of the vertex distribution.

3401:   Not currently supported in Fortran.

3403:   Level: advanced

3405: .seealso: DMPlexBuildFromCellList(), DMPlexCreateFromCellListParallelPetsc(), DMPlexBuildCoordinatesFromCellListParallel()
3406: @*/
3407: PetscErrorCode DMPlexBuildFromCellListParallel(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscInt numCorners, const PetscInt cells[], PetscSF *vertexSF)
3408: {
3409:   PetscSF         sfPoint;
3410:   PetscLayout     layout;
3411:   PetscInt        numVerticesAdj, *verticesAdj, *cones, c, p, dim;
3412:   PetscMPIInt     rank, size;
3413:   PetscErrorCode  ierr;

3417:   PetscLogEventBegin(DMPLEX_BuildFromCellList,dm,0,0,0);
3418:   MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);
3419:   MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size);
3420:   DMGetDimension(dm, &dim);
3421:   /* Get/check global number of vertices */
3422:   {
3423:     PetscInt NVerticesInCells, i;
3424:     const PetscInt len = numCells * numCorners;

3426:     /* NVerticesInCells = max(cells) + 1 */
3427:     NVerticesInCells = PETSC_MIN_INT;
3428:     for (i=0; i<len; i++) if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
3429:     ++NVerticesInCells;
3430:     MPI_Allreduce(MPI_IN_PLACE, &NVerticesInCells, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm));

3432:     if (numVertices == PETSC_DECIDE && NVertices == PETSC_DECIDE) NVertices = NVerticesInCells;
3433:     else if (NVertices != PETSC_DECIDE && NVertices < NVerticesInCells) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Specified global number of vertices %D must be greater than or equal to the number of vertices in cells %D",NVertices,NVerticesInCells);
3434:   }
3435:   /* Count locally unique vertices */
3436:   {
3437:     PetscHSetI vhash;
3438:     PetscInt off = 0;

3440:     PetscHSetICreate(&vhash);
3441:     for (c = 0; c < numCells; ++c) {
3442:       for (p = 0; p < numCorners; ++p) {
3443:         PetscHSetIAdd(vhash, cells[c*numCorners+p]);
3444:       }
3445:     }
3446:     PetscHSetIGetSize(vhash, &numVerticesAdj);
3447:     PetscMalloc1(numVerticesAdj, &verticesAdj);
3448:     PetscHSetIGetElems(vhash, &off, verticesAdj);
3449:     PetscHSetIDestroy(&vhash);
3450:     if (off != numVerticesAdj) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid number of local vertices %D should be %D", off, numVerticesAdj);
3451:   }
3452:   PetscSortInt(numVerticesAdj, verticesAdj);
3453:   /* Create cones */
3454:   DMPlexSetChart(dm, 0, numCells+numVerticesAdj);
3455:   for (c = 0; c < numCells; ++c) {DMPlexSetConeSize(dm, c, numCorners);}
3456:   DMSetUp(dm);
3457:   DMPlexGetCones(dm,&cones);
3458:   for (c = 0; c < numCells; ++c) {
3459:     for (p = 0; p < numCorners; ++p) {
3460:       const PetscInt gv = cells[c*numCorners+p];
3461:       PetscInt       lv;

3463:       /* Positions within verticesAdj form 0-based local vertex numbering;
3464:          we need to shift it by numCells to get correct DAG points (cells go first) */
3465:       PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv);
3466:       if (lv < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %D in local connectivity", gv);
3467:       cones[c*numCorners+p] = lv+numCells;
3468:     }
3469:   }
3470:   /* Build point sf */
3471:   PetscLayoutCreate(PetscObjectComm((PetscObject)dm), &layout);
3472:   PetscLayoutSetSize(layout, NVertices);
3473:   PetscLayoutSetLocalSize(layout, numVertices);
3474:   PetscLayoutSetBlockSize(layout, 1);
3475:   PetscSFCreateByMatchingIndices(layout, numVerticesAdj, verticesAdj, NULL, numCells, numVerticesAdj, verticesAdj, NULL, numCells, vertexSF, &sfPoint);
3476:   PetscLayoutDestroy(&layout);
3477:   PetscFree(verticesAdj);
3478:   PetscObjectSetName((PetscObject) sfPoint, "point SF");
3479:   if (dm->sf) {
3480:     const char *prefix;

3482:     PetscObjectGetOptionsPrefix((PetscObject)dm->sf, &prefix);
3483:     PetscObjectSetOptionsPrefix((PetscObject)sfPoint, prefix);
3484:   }
3485:   DMSetPointSF(dm, sfPoint);
3486:   PetscSFDestroy(&sfPoint);
3487:   if (vertexSF) {PetscObjectSetName((PetscObject)(*vertexSF), "Vertex Ownership SF");}
3488:   /* Fill in the rest of the topology structure */
3489:   DMPlexSymmetrize(dm);
3490:   DMPlexStratify(dm);
3491:   PetscLogEventEnd(DMPLEX_BuildFromCellList,dm,0,0,0);
3492:   return(0);
3493: }

3495: /*@C
3496:   DMPlexBuildCoordinatesFromCellListParallel - Build DM coordinates from a list of coordinates for each owned vertex (common mesh generator output)

3498:   Input Parameters:
3499: + dm - The DM
3500: . spaceDim - The spatial dimension used for coordinates
3501: . sfVert - SF describing complete vertex ownership
3502: - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex

3504:   Level: advanced

3506:   Notes:
3507:   Not currently supported in Fortran.

3509: .seealso: DMPlexBuildCoordinatesFromCellList(), DMPlexCreateFromCellListParallelPetsc(), DMPlexBuildFromCellListParallel()
3510: @*/
3511: PetscErrorCode DMPlexBuildCoordinatesFromCellListParallel(DM dm, PetscInt spaceDim, PetscSF sfVert, const PetscReal vertexCoords[])
3512: {
3513:   PetscSection   coordSection;
3514:   Vec            coordinates;
3515:   PetscScalar   *coords;
3516:   PetscInt       numVertices, numVerticesAdj, coordSize, v, vStart, vEnd;

3520:   PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0);
3521:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
3522:   if (vStart < 0 || vEnd < 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
3523:   DMSetCoordinateDim(dm, spaceDim);
3524:   PetscSFGetGraph(sfVert, &numVertices, &numVerticesAdj, NULL, NULL);
3525:   if (vEnd - vStart != numVerticesAdj) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Supplied sfVert has wrong number of leaves = %D != %D = vEnd - vStart",numVerticesAdj,vEnd - vStart);
3526:   DMGetCoordinateSection(dm, &coordSection);
3527:   PetscSectionSetNumFields(coordSection, 1);
3528:   PetscSectionSetFieldComponents(coordSection, 0, spaceDim);
3529:   PetscSectionSetChart(coordSection, vStart, vEnd);
3530:   for (v = vStart; v < vEnd; ++v) {
3531:     PetscSectionSetDof(coordSection, v, spaceDim);
3532:     PetscSectionSetFieldDof(coordSection, v, 0, spaceDim);
3533:   }
3534:   PetscSectionSetUp(coordSection);
3535:   PetscSectionGetStorageSize(coordSection, &coordSize);
3536:   VecCreate(PetscObjectComm((PetscObject)dm), &coordinates);
3537:   VecSetBlockSize(coordinates, spaceDim);
3538:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
3539:   VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
3540:   VecSetType(coordinates,VECSTANDARD);
3541:   VecGetArray(coordinates, &coords);
3542:   {
3543:     MPI_Datatype coordtype;

3545:     /* Need a temp buffer for coords if we have complex/single */
3546:     MPI_Type_contiguous(spaceDim, MPIU_SCALAR, &coordtype);
3547:     MPI_Type_commit(&coordtype);
3548: #if defined(PETSC_USE_COMPLEX)
3549:     {
3550:     PetscScalar *svertexCoords;
3551:     PetscInt    i;
3552:     PetscMalloc1(numVertices*spaceDim,&svertexCoords);
3553:     for (i=0; i<numVertices*spaceDim; i++) svertexCoords[i] = vertexCoords[i];
3554:     PetscSFBcastBegin(sfVert, coordtype, svertexCoords, coords,MPI_REPLACE);
3555:     PetscSFBcastEnd(sfVert, coordtype, svertexCoords, coords,MPI_REPLACE);
3556:     PetscFree(svertexCoords);
3557:     }
3558: #else
3559:     PetscSFBcastBegin(sfVert, coordtype, vertexCoords, coords,MPI_REPLACE);
3560:     PetscSFBcastEnd(sfVert, coordtype, vertexCoords, coords,MPI_REPLACE);
3561: #endif
3562:     MPI_Type_free(&coordtype);
3563:   }
3564:   VecRestoreArray(coordinates, &coords);
3565:   DMSetCoordinatesLocal(dm, coordinates);
3566:   VecDestroy(&coordinates);
3567:   PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0);
3568:   return(0);
3569: }

3571: /*@
3572:   DMPlexCreateFromCellListParallelPetsc - Create distributed DMPLEX from a list of vertices for each cell (common mesh generator output)

3574:   Input Parameters:
3575: + comm - The communicator
3576: . dim - The topological dimension of the mesh
3577: . numCells - The number of cells owned by this process
3578: . numVertices - The number of vertices owned by this process, or PETSC_DECIDE
3579: . NVertices - The global number of vertices, or PETSC_DECIDE
3580: . numCorners - The number of vertices for each cell
3581: . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
3582: . cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
3583: . spaceDim - The spatial dimension used for coordinates
3584: - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex

3586:   Output Parameter:
3587: + dm - The DM
3588: - vertexSF - (Optional) SF describing complete vertex ownership

3590:   Notes:
3591:   This function is just a convenient sequence of DMCreate(), DMSetType(), DMSetDimension(),
3592:   DMPlexBuildFromCellListParallel(), DMPlexInterpolate(), DMPlexBuildCoordinatesFromCellListParallel()

3594:   See DMPlexBuildFromCellListParallel() for an example and details about the topology-related parameters.
3595:   See DMPlexBuildCoordinatesFromCellListParallel() for details about the geometry-related parameters.

3597:   Level: intermediate

3599: .seealso: DMPlexCreateFromCellListPetsc(), DMPlexBuildFromCellListParallel(), DMPlexBuildCoordinatesFromCellListParallel(), DMPlexCreateFromDAG(), DMPlexCreate()
3600: @*/
3601: PetscErrorCode DMPlexCreateFromCellListParallelPetsc(MPI_Comm comm, PetscInt dim, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscInt numCorners, PetscBool interpolate, const PetscInt cells[], PetscInt spaceDim, const PetscReal vertexCoords[], PetscSF *vertexSF, DM *dm)
3602: {
3603:   PetscSF        sfVert;

3607:   DMCreate(comm, dm);
3608:   DMSetType(*dm, DMPLEX);
3611:   DMSetDimension(*dm, dim);
3612:   DMPlexBuildFromCellListParallel(*dm, numCells, numVertices, NVertices, numCorners, cells, &sfVert);
3613:   if (interpolate) {
3614:     DM idm;

3616:     DMPlexInterpolate(*dm, &idm);
3617:     DMDestroy(dm);
3618:     *dm  = idm;
3619:   }
3620:   DMPlexBuildCoordinatesFromCellListParallel(*dm, spaceDim, sfVert, vertexCoords);
3621:   if (vertexSF) *vertexSF = sfVert;
3622:   else {PetscSFDestroy(&sfVert);}
3623:   return(0);
3624: }

3626: /*@
3627:   DMPlexCreateFromCellListParallel - Deprecated, use DMPlexCreateFromCellListParallelPetsc()

3629:   Level: deprecated

3631: .seealso: DMPlexCreateFromCellListParallelPetsc()
3632: @*/
3633: PetscErrorCode DMPlexCreateFromCellListParallel(MPI_Comm comm, PetscInt dim, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, PetscBool interpolate, const int cells[], PetscInt spaceDim, const PetscReal vertexCoords[], PetscSF *vertexSF, DM *dm)
3634: {
3636:   PetscInt       i;
3637:   PetscInt       *pintCells;

3640:   if (sizeof(int) > sizeof(PetscInt)) SETERRQ2(comm, PETSC_ERR_ARG_SIZ, "Size of int %zd greater than size of PetscInt %zd. Reconfigure PETSc --with-64-bit-indices=1", sizeof(int), sizeof(PetscInt));
3641:   if (sizeof(int) == sizeof(PetscInt)) {
3642:     pintCells = (PetscInt *) cells;
3643:   } else {
3644:     PetscMalloc1(numCells*numCorners, &pintCells);
3645:     for (i = 0; i < numCells*numCorners; i++) {
3646:       pintCells[i] = (PetscInt) cells[i];
3647:     }
3648:   }
3649:   DMPlexCreateFromCellListParallelPetsc(comm, dim, numCells, numVertices, PETSC_DECIDE, numCorners, interpolate, pintCells, spaceDim, vertexCoords, vertexSF, dm);
3650:   if (sizeof(int) != sizeof(PetscInt)) {
3651:     PetscFree(pintCells);
3652:   }
3653:   return(0);
3654: }

3656: /*@C
3657:   DMPlexBuildFromCellList - Build DMPLEX topology from a list of vertices for each cell (common mesh generator output)

3659:   Input Parameters:
3660: + dm - The DM
3661: . numCells - The number of cells owned by this process
3662: . numVertices - The number of vertices owned by this process, or PETSC_DECIDE
3663: . numCorners - The number of vertices for each cell
3664: - cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell

3666:   Level: advanced

3668:   Notes:
3669:   Two triangles sharing a face
3670: $
3671: $        2
3672: $      / | \
3673: $     /  |  \
3674: $    /   |   \
3675: $   0  0 | 1  3
3676: $    \   |   /
3677: $     \  |  /
3678: $      \ | /
3679: $        1
3680: would have input
3681: $  numCells = 2, numVertices = 4
3682: $  cells = [0 1 2  1 3 2]
3683: $
3684: which would result in the DMPlex
3685: $
3686: $        4
3687: $      / | \
3688: $     /  |  \
3689: $    /   |   \
3690: $   2  0 | 1  5
3691: $    \   |   /
3692: $     \  |  /
3693: $      \ | /
3694: $        3

3696:   If numVertices is PETSC_DECIDE, it is computed by PETSc as the maximum vertex index in cells + 1.

3698:   Not currently supported in Fortran.

3700: .seealso: DMPlexBuildFromCellListParallel(), DMPlexBuildCoordinatesFromCellList(), DMPlexCreateFromCellListPetsc()
3701: @*/
3702: PetscErrorCode DMPlexBuildFromCellList(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const PetscInt cells[])
3703: {
3704:   PetscInt      *cones, c, p, dim;

3708:   PetscLogEventBegin(DMPLEX_BuildFromCellList,dm,0,0,0);
3709:   DMGetDimension(dm, &dim);
3710:   /* Get/check global number of vertices */
3711:   {
3712:     PetscInt NVerticesInCells, i;
3713:     const PetscInt len = numCells * numCorners;

3715:     /* NVerticesInCells = max(cells) + 1 */
3716:     NVerticesInCells = PETSC_MIN_INT;
3717:     for (i=0; i<len; i++) if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
3718:     ++NVerticesInCells;

3720:     if (numVertices == PETSC_DECIDE) numVertices = NVerticesInCells;
3721:     else if (numVertices < NVerticesInCells) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Specified number of vertices %D must be greater than or equal to the number of vertices in cells %D",numVertices,NVerticesInCells);
3722:   }
3723:   DMPlexSetChart(dm, 0, numCells+numVertices);
3724:   for (c = 0; c < numCells; ++c) {
3725:     DMPlexSetConeSize(dm, c, numCorners);
3726:   }
3727:   DMSetUp(dm);
3728:   DMPlexGetCones(dm,&cones);
3729:   for (c = 0; c < numCells; ++c) {
3730:     for (p = 0; p < numCorners; ++p) {
3731:       cones[c*numCorners+p] = cells[c*numCorners+p]+numCells;
3732:     }
3733:   }
3734:   DMPlexSymmetrize(dm);
3735:   DMPlexStratify(dm);
3736:   PetscLogEventEnd(DMPLEX_BuildFromCellList,dm,0,0,0);
3737:   return(0);
3738: }

3740: /*@C
3741:   DMPlexBuildCoordinatesFromCellList - Build DM coordinates from a list of coordinates for each owned vertex (common mesh generator output)

3743:   Input Parameters:
3744: + dm - The DM
3745: . spaceDim - The spatial dimension used for coordinates
3746: - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex

3748:   Level: advanced

3750:   Notes:
3751:   Not currently supported in Fortran.

3753: .seealso: DMPlexBuildCoordinatesFromCellListParallel(), DMPlexCreateFromCellListPetsc(), DMPlexBuildFromCellList()
3754: @*/
3755: PetscErrorCode DMPlexBuildCoordinatesFromCellList(DM dm, PetscInt spaceDim, const PetscReal vertexCoords[])
3756: {
3757:   PetscSection   coordSection;
3758:   Vec            coordinates;
3759:   DM             cdm;
3760:   PetscScalar   *coords;
3761:   PetscInt       v, vStart, vEnd, d;

3765:   PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0);
3766:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
3767:   if (vStart < 0 || vEnd < 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
3768:   DMSetCoordinateDim(dm, spaceDim);
3769:   DMGetCoordinateSection(dm, &coordSection);
3770:   PetscSectionSetNumFields(coordSection, 1);
3771:   PetscSectionSetFieldComponents(coordSection, 0, spaceDim);
3772:   PetscSectionSetChart(coordSection, vStart, vEnd);
3773:   for (v = vStart; v < vEnd; ++v) {
3774:     PetscSectionSetDof(coordSection, v, spaceDim);
3775:     PetscSectionSetFieldDof(coordSection, v, 0, spaceDim);
3776:   }
3777:   PetscSectionSetUp(coordSection);

3779:   DMGetCoordinateDM(dm, &cdm);
3780:   DMCreateLocalVector(cdm, &coordinates);
3781:   VecSetBlockSize(coordinates, spaceDim);
3782:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
3783:   VecGetArrayWrite(coordinates, &coords);
3784:   for (v = 0; v < vEnd-vStart; ++v) {
3785:     for (d = 0; d < spaceDim; ++d) {
3786:       coords[v*spaceDim+d] = vertexCoords[v*spaceDim+d];
3787:     }
3788:   }
3789:   VecRestoreArrayWrite(coordinates, &coords);
3790:   DMSetCoordinatesLocal(dm, coordinates);
3791:   VecDestroy(&coordinates);
3792:   PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0);
3793:   return(0);
3794: }

3796: /*@
3797:   DMPlexCreateFromCellListPetsc - Create DMPLEX from a list of vertices for each cell (common mesh generator output)

3799:   Input Parameters:
3800: + comm - The communicator
3801: . dim - The topological dimension of the mesh
3802: . numCells - The number of cells
3803: . numVertices - The number of vertices owned by this process, or PETSC_DECIDE
3804: . numCorners - The number of vertices for each cell
3805: . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
3806: . cells - An array of numCells*numCorners numbers, the vertices for each cell
3807: . spaceDim - The spatial dimension used for coordinates
3808: - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex

3810:   Output Parameter:
3811: . dm - The DM

3813:   Notes:
3814:   This function is just a convenient sequence of DMCreate(), DMSetType(), DMSetDimension(), DMPlexBuildFromCellList(),
3815:   DMPlexInterpolate(), DMPlexBuildCoordinatesFromCellList()

3817:   See DMPlexBuildFromCellList() for an example and details about the topology-related parameters.
3818:   See DMPlexBuildCoordinatesFromCellList() for details about the geometry-related parameters.

3820:   Level: intermediate

3822: .seealso: DMPlexCreateFromCellListParallelPetsc(), DMPlexBuildFromCellList(), DMPlexBuildCoordinatesFromCellList(), DMPlexCreateFromDAG(), DMPlexCreate()
3823: @*/
3824: PetscErrorCode DMPlexCreateFromCellListPetsc(MPI_Comm comm, PetscInt dim, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, PetscBool interpolate, const PetscInt cells[], PetscInt spaceDim, const PetscReal vertexCoords[], DM *dm)
3825: {

3829:   if (!dim) SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "This is not appropriate for 0-dimensional meshes. Consider either creating the DM using DMPlexCreateFromDAG(), by hand, or using DMSwarm.");
3830:   DMCreate(comm, dm);
3831:   DMSetType(*dm, DMPLEX);
3832:   DMSetDimension(*dm, dim);
3833:   DMPlexBuildFromCellList(*dm, numCells, numVertices, numCorners, cells);
3834:   if (interpolate) {
3835:     DM idm;

3837:     DMPlexInterpolate(*dm, &idm);
3838:     DMDestroy(dm);
3839:     *dm  = idm;
3840:   }
3841:   DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, vertexCoords);
3842:   return(0);
3843: }

3845: /*@
3846:   DMPlexCreateFromCellList - Deprecated, use DMPlexCreateFromCellListPetsc()

3848:   Level: deprecated

3850: .seealso: DMPlexCreateFromCellListPetsc()
3851: @*/
3852: PetscErrorCode DMPlexCreateFromCellList(MPI_Comm comm, PetscInt dim, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, PetscBool interpolate, const int cells[], PetscInt spaceDim, const double vertexCoords[], DM *dm)
3853: {
3855:   PetscInt       i;
3856:   PetscInt       *pintCells;
3857:   PetscReal      *prealVC;

3860:   if (sizeof(int) > sizeof(PetscInt)) SETERRQ2(comm, PETSC_ERR_ARG_SIZ, "Size of int %zd greater than size of PetscInt %zd. Reconfigure PETSc --with-64-bit-indices=1", sizeof(int), sizeof(PetscInt));
3861:   if (sizeof(int) == sizeof(PetscInt)) {
3862:     pintCells = (PetscInt *) cells;
3863:   } else {
3864:     PetscMalloc1(numCells*numCorners, &pintCells);
3865:     for (i = 0; i < numCells*numCorners; i++) {
3866:       pintCells[i] = (PetscInt) cells[i];
3867:     }
3868:   }
3869:   if (sizeof(double) > sizeof(PetscReal)) SETERRQ2(comm, PETSC_ERR_ARG_SIZ, "Size of double %zd greater than size of PetscReal %zd. Reconfigure PETSc --with-precision=<higher precision>.", sizeof(double), sizeof(PetscReal));
3870:   if (sizeof(double) == sizeof(PetscReal)) {
3871:     prealVC = (PetscReal *) vertexCoords;
3872:   } else {
3873:     PetscMalloc1(numVertices*spaceDim, &prealVC);
3874:     for (i = 0; i < numVertices*spaceDim; i++) {
3875:       prealVC[i] = (PetscReal) vertexCoords[i];
3876:     }
3877:   }
3878:   DMPlexCreateFromCellListPetsc(comm, dim, numCells, numVertices, numCorners, interpolate, pintCells, spaceDim, prealVC, dm);
3879:   if (sizeof(int) != sizeof(PetscInt)) {
3880:     PetscFree(pintCells);
3881:   }
3882:   if (sizeof(double) != sizeof(PetscReal)) {
3883:     PetscFree(prealVC);
3884:   }
3885:   return(0);
3886: }

3888: /*@
3889:   DMPlexCreateFromDAG - This takes as input the adjacency-list representation of the Directed Acyclic Graph (Hasse Diagram) encoding a mesh, and produces a DM

3891:   Input Parameters:
3892: + dm - The empty DM object, usually from DMCreate() and DMSetDimension()
3893: . depth - The depth of the DAG
3894: . numPoints - Array of size depth + 1 containing the number of points at each depth
3895: . coneSize - The cone size of each point
3896: . cones - The concatenation of the cone points for each point, the cone list must be oriented correctly for each point
3897: . coneOrientations - The orientation of each cone point
3898: - vertexCoords - An array of numPoints[0]*spacedim numbers representing the coordinates of each vertex, with spacedim the value set via DMSetCoordinateDim()

3900:   Output Parameter:
3901: . dm - The DM

3903:   Note: Two triangles sharing a face would have input
3904: $  depth = 1, numPoints = [4 2], coneSize = [3 3 0 0 0 0]
3905: $  cones = [2 3 4  3 5 4], coneOrientations = [0 0 0  0 0 0]
3906: $ vertexCoords = [-1.0 0.0  0.0 -1.0  0.0 1.0  1.0 0.0]
3907: $
3908: which would result in the DMPlex
3909: $
3910: $        4
3911: $      / | \
3912: $     /  |  \
3913: $    /   |   \
3914: $   2  0 | 1  5
3915: $    \   |   /
3916: $     \  |  /
3917: $      \ | /
3918: $        3
3919: $
3920: $ Notice that all points are numbered consecutively, unlike DMPlexCreateFromCellListPetsc()

3922:   Level: advanced

3924: .seealso: DMPlexCreateFromCellListPetsc(), DMPlexCreate()
3925: @*/
3926: PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[])
3927: {
3928:   Vec            coordinates;
3929:   PetscSection   coordSection;
3930:   PetscScalar    *coords;
3931:   PetscInt       coordSize, firstVertex = -1, pStart = 0, pEnd = 0, p, v, dim, dimEmbed, d, off;

3935:   DMGetDimension(dm, &dim);
3936:   DMGetCoordinateDim(dm, &dimEmbed);
3937:   if (dimEmbed < dim) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Embedding dimension %D cannot be less than intrinsic dimension %d",dimEmbed,dim);
3938:   for (d = 0; d <= depth; ++d) pEnd += numPoints[d];
3939:   DMPlexSetChart(dm, pStart, pEnd);
3940:   for (p = pStart; p < pEnd; ++p) {
3941:     DMPlexSetConeSize(dm, p, coneSize[p-pStart]);
3942:     if (firstVertex < 0 && !coneSize[p - pStart]) {
3943:       firstVertex = p - pStart;
3944:     }
3945:   }
3946:   if (firstVertex < 0 && numPoints[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected %D vertices but could not find any", numPoints[0]);
3947:   DMSetUp(dm); /* Allocate space for cones */
3948:   for (p = pStart, off = 0; p < pEnd; off += coneSize[p-pStart], ++p) {
3949:     DMPlexSetCone(dm, p, &cones[off]);
3950:     DMPlexSetConeOrientation(dm, p, &coneOrientations[off]);
3951:   }
3952:   DMPlexSymmetrize(dm);
3953:   DMPlexStratify(dm);
3954:   /* Build coordinates */
3955:   DMGetCoordinateSection(dm, &coordSection);
3956:   PetscSectionSetNumFields(coordSection, 1);
3957:   PetscSectionSetFieldComponents(coordSection, 0, dimEmbed);
3958:   PetscSectionSetChart(coordSection, firstVertex, firstVertex+numPoints[0]);
3959:   for (v = firstVertex; v < firstVertex+numPoints[0]; ++v) {
3960:     PetscSectionSetDof(coordSection, v, dimEmbed);
3961:     PetscSectionSetFieldDof(coordSection, v, 0, dimEmbed);
3962:   }
3963:   PetscSectionSetUp(coordSection);
3964:   PetscSectionGetStorageSize(coordSection, &coordSize);
3965:   VecCreate(PETSC_COMM_SELF, &coordinates);
3966:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
3967:   VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
3968:   VecSetBlockSize(coordinates, dimEmbed);
3969:   VecSetType(coordinates,VECSTANDARD);
3970:   if (vertexCoords) {
3971:     VecGetArray(coordinates, &coords);
3972:     for (v = 0; v < numPoints[0]; ++v) {
3973:       PetscInt off;

3975:       PetscSectionGetOffset(coordSection, v+firstVertex, &off);
3976:       for (d = 0; d < dimEmbed; ++d) {
3977:         coords[off+d] = vertexCoords[v*dimEmbed+d];
3978:       }
3979:     }
3980:   }
3981:   VecRestoreArray(coordinates, &coords);
3982:   DMSetCoordinatesLocal(dm, coordinates);
3983:   VecDestroy(&coordinates);
3984:   return(0);
3985: }

3987: /*@C
3988:   DMPlexCreateCellVertexFromFile - Create a DMPlex mesh from a simple cell-vertex file.

3990: + comm        - The MPI communicator
3991: . filename    - Name of the .dat file
3992: - interpolate - Create faces and edges in the mesh

3994:   Output Parameter:
3995: . dm  - The DM object representing the mesh

3997:   Note: The format is the simplest possible:
3998: $ Ne
3999: $ v0 v1 ... vk
4000: $ Nv
4001: $ x y z marker

4003:   Level: beginner

4005: .seealso: DMPlexCreateFromFile(), DMPlexCreateMedFromFile(), DMPlexCreateGmsh(), DMPlexCreate()
4006: @*/
4007: PetscErrorCode DMPlexCreateCellVertexFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
4008: {
4009:   DMLabel         marker;
4010:   PetscViewer     viewer;
4011:   Vec             coordinates;
4012:   PetscSection    coordSection;
4013:   PetscScalar    *coords;
4014:   char            line[PETSC_MAX_PATH_LEN];
4015:   PetscInt        dim = 3, cdim = 3, coordSize, v, c, d;
4016:   PetscMPIInt     rank;
4017:   int             snum, Nv, Nc;
4018:   PetscErrorCode  ierr;

4021:   MPI_Comm_rank(comm, &rank);
4022:   PetscViewerCreate(comm, &viewer);
4023:   PetscViewerSetType(viewer, PETSCVIEWERASCII);
4024:   PetscViewerFileSetMode(viewer, FILE_MODE_READ);
4025:   PetscViewerFileSetName(viewer, filename);
4026:   if (!rank) {
4027:     PetscViewerRead(viewer, line, 2, NULL, PETSC_STRING);
4028:     snum = sscanf(line, "%d %d", &Nc, &Nv);
4029:     if (snum != 2) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
4030:   } else {
4031:     Nc = Nv = 0;
4032:   }
4033:   DMCreate(comm, dm);
4034:   DMSetType(*dm, DMPLEX);
4035:   DMPlexSetChart(*dm, 0, Nc+Nv);
4036:   DMSetDimension(*dm, dim);
4037:   DMSetCoordinateDim(*dm, cdim);
4038:   /* Read topology */
4039:   if (!rank) {
4040:     PetscInt cone[8], corners = 8;
4041:     int      vbuf[8], v;

4043:     for (c = 0; c < Nc; ++c) {DMPlexSetConeSize(*dm, c, corners);}
4044:     DMSetUp(*dm);
4045:     for (c = 0; c < Nc; ++c) {
4046:       PetscViewerRead(viewer, line, corners, NULL, PETSC_STRING);
4047:       snum = sscanf(line, "%d %d %d %d %d %d %d %d", &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5], &vbuf[6], &vbuf[7]);
4048:       if (snum != corners) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
4049:       for (v = 0; v < corners; ++v) cone[v] = vbuf[v] + Nc;
4050:       /* Hexahedra are inverted */
4051:       {
4052:         PetscInt tmp = cone[1];
4053:         cone[1] = cone[3];
4054:         cone[3] = tmp;
4055:       }
4056:       DMPlexSetCone(*dm, c, cone);
4057:     }
4058:   }
4059:   DMPlexSymmetrize(*dm);
4060:   DMPlexStratify(*dm);
4061:   /* Read coordinates */
4062:   DMGetCoordinateSection(*dm, &coordSection);
4063:   PetscSectionSetNumFields(coordSection, 1);
4064:   PetscSectionSetFieldComponents(coordSection, 0, cdim);
4065:   PetscSectionSetChart(coordSection, Nc, Nc + Nv);
4066:   for (v = Nc; v < Nc+Nv; ++v) {
4067:     PetscSectionSetDof(coordSection, v, cdim);
4068:     PetscSectionSetFieldDof(coordSection, v, 0, cdim);
4069:   }
4070:   PetscSectionSetUp(coordSection);
4071:   PetscSectionGetStorageSize(coordSection, &coordSize);
4072:   VecCreate(PETSC_COMM_SELF, &coordinates);
4073:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
4074:   VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
4075:   VecSetBlockSize(coordinates, cdim);
4076:   VecSetType(coordinates, VECSTANDARD);
4077:   VecGetArray(coordinates, &coords);
4078:   if (!rank) {
4079:     double x[3];
4080:     int    val;

4082:     DMCreateLabel(*dm, "marker");
4083:     DMGetLabel(*dm, "marker", &marker);
4084:     for (v = 0; v < Nv; ++v) {
4085:       PetscViewerRead(viewer, line, 4, NULL, PETSC_STRING);
4086:       snum = sscanf(line, "%lg %lg %lg %d", &x[0], &x[1], &x[2], &val);
4087:       if (snum != 4) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
4088:       for (d = 0; d < cdim; ++d) coords[v*cdim+d] = x[d];
4089:       if (val) {DMLabelSetValue(marker, v+Nc, val);}
4090:     }
4091:   }
4092:   VecRestoreArray(coordinates, &coords);
4093:   DMSetCoordinatesLocal(*dm, coordinates);
4094:   VecDestroy(&coordinates);
4095:   PetscViewerDestroy(&viewer);
4096:   if (interpolate) {
4097:     DM      idm;
4098:     DMLabel bdlabel;

4100:     DMPlexInterpolate(*dm, &idm);
4101:     DMDestroy(dm);
4102:     *dm  = idm;

4104:     DMGetLabel(*dm, "marker", &bdlabel);
4105:     DMPlexMarkBoundaryFaces(*dm, PETSC_DETERMINE, bdlabel);
4106:     DMPlexLabelComplete(*dm, bdlabel);
4107:   }
4108:   return(0);
4109: }

4111: /*@C
4112:   DMPlexCreateFromFile - This takes a filename and produces a DM

4114:   Input Parameters:
4115: + comm - The communicator
4116: . filename - A file name
4117: - interpolate - Flag to create intermediate mesh pieces (edges, faces)

4119:   Output Parameter:
4120: . dm - The DM

4122:   Options Database Keys:
4123: . -dm_plex_create_from_hdf5_xdmf - use the PETSC_VIEWER_HDF5_XDMF format for reading HDF5

4125:   Use -dm_plex_create_ prefix to pass options to the internal PetscViewer, e.g.
4126: $ -dm_plex_create_viewer_hdf5_collective

4128:   Level: beginner

4130: .seealso: DMPlexCreateFromDAG(), DMPlexCreateFromCellListPetsc(), DMPlexCreate()
4131: @*/
4132: PetscErrorCode DMPlexCreateFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
4133: {
4134:   const char    *extGmsh    = ".msh";
4135:   const char    *extGmsh2   = ".msh2";
4136:   const char    *extGmsh4   = ".msh4";
4137:   const char    *extCGNS    = ".cgns";
4138:   const char    *extExodus  = ".exo";
4139:   const char    *extGenesis = ".gen";
4140:   const char    *extFluent  = ".cas";
4141:   const char    *extHDF5    = ".h5";
4142:   const char    *extMed     = ".med";
4143:   const char    *extPLY     = ".ply";
4144:   const char    *extEGADS   = ".egadslite";
4145:   const char    *extCV      = ".dat";
4146:   size_t         len;
4147:   PetscBool      isGmsh, isGmsh2, isGmsh4, isCGNS, isExodus, isGenesis, isFluent, isHDF5, isMed, isPLY, isEGADS, isCV;
4148:   PetscMPIInt    rank;

4154:   DMInitializePackage();
4155:   PetscLogEventBegin(DMPLEX_CreateFromFile,0,0,0,0);
4156:   MPI_Comm_rank(comm, &rank);
4157:   PetscStrlen(filename, &len);
4158:   if (!len) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Filename must be a valid path");
4159:   PetscStrncmp(&filename[PetscMax(0,len-4)], extGmsh,    4, &isGmsh);
4160:   PetscStrncmp(&filename[PetscMax(0,len-5)], extGmsh2,   5, &isGmsh2);
4161:   PetscStrncmp(&filename[PetscMax(0,len-5)], extGmsh4,   5, &isGmsh4);
4162:   PetscStrncmp(&filename[PetscMax(0,len-5)], extCGNS,    5, &isCGNS);
4163:   PetscStrncmp(&filename[PetscMax(0,len-4)], extExodus,  4, &isExodus);
4164:   PetscStrncmp(&filename[PetscMax(0,len-4)], extGenesis, 4, &isGenesis);
4165:   PetscStrncmp(&filename[PetscMax(0,len-4)], extFluent,  4, &isFluent);
4166:   PetscStrncmp(&filename[PetscMax(0,len-3)], extHDF5,    3, &isHDF5);
4167:   PetscStrncmp(&filename[PetscMax(0,len-4)], extMed,     4, &isMed);
4168:   PetscStrncmp(&filename[PetscMax(0,len-4)], extPLY,     4, &isPLY);
4169:   PetscStrncmp(&filename[PetscMax(0,len-10)], extEGADS,   9, &isEGADS);
4170:   PetscStrncmp(&filename[PetscMax(0,len-4)], extCV,      4, &isCV);
4171:   if (isGmsh || isGmsh2 || isGmsh4) {
4172:     DMPlexCreateGmshFromFile(comm, filename, interpolate, dm);
4173:   } else if (isCGNS) {
4174:     DMPlexCreateCGNSFromFile(comm, filename, interpolate, dm);
4175:   } else if (isExodus || isGenesis) {
4176:     DMPlexCreateExodusFromFile(comm, filename, interpolate, dm);
4177:   } else if (isFluent) {
4178:     DMPlexCreateFluentFromFile(comm, filename, interpolate, dm);
4179:   } else if (isHDF5) {
4180:     PetscBool      load_hdf5_xdmf = PETSC_FALSE;
4181:     PetscViewer viewer;

4183:     /* PETSC_VIEWER_HDF5_XDMF is used if the filename ends with .xdmf.h5, or if -dm_plex_create_from_hdf5_xdmf option is present */
4184:     PetscStrncmp(&filename[PetscMax(0,len-8)], ".xdmf",  5, &load_hdf5_xdmf);
4185:     PetscOptionsGetBool(NULL, NULL, "-dm_plex_create_from_hdf5_xdmf", &load_hdf5_xdmf, NULL);
4186:     PetscViewerCreate(comm, &viewer);
4187:     PetscViewerSetType(viewer, PETSCVIEWERHDF5);
4188:     PetscViewerSetOptionsPrefix(viewer, "dm_plex_create_");
4189:     PetscViewerSetFromOptions(viewer);
4190:     PetscViewerFileSetMode(viewer, FILE_MODE_READ);
4191:     PetscViewerFileSetName(viewer, filename);
4192:     DMCreate(comm, dm);
4193:     DMSetType(*dm, DMPLEX);
4194:     if (load_hdf5_xdmf) {PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_XDMF);}
4195:     DMLoad(*dm, viewer);
4196:     if (load_hdf5_xdmf) {PetscViewerPopFormat(viewer);}
4197:     PetscViewerDestroy(&viewer);

4199:     if (interpolate) {
4200:       DM idm;

4202:       DMPlexInterpolate(*dm, &idm);
4203:       DMDestroy(dm);
4204:       *dm  = idm;
4205:     }
4206:   } else if (isMed) {
4207:     DMPlexCreateMedFromFile(comm, filename, interpolate, dm);
4208:   } else if (isPLY) {
4209:     DMPlexCreatePLYFromFile(comm, filename, interpolate, dm);
4210:   } else if (isEGADS) {
4211:     DMPlexCreateEGADSFromFile(comm, filename, dm);
4212:     if (!interpolate) {
4213:       DM udm;

4215:       DMPlexUninterpolate(*dm, &udm);
4216:       DMDestroy(dm);
4217:       *dm  = udm;
4218:     }
4219:   } else if (isCV) {
4220:     DMPlexCreateCellVertexFromFile(comm, filename, interpolate, dm);
4221:   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename);
4222:   PetscLogEventEnd(DMPLEX_CreateFromFile,0,0,0,0);
4223:   return(0);
4224: }