Actual source code: plexcreate.c

petsc-master 2017-08-16
Report Typos and Errors
  1: #define PETSCDM_DLL
  2:  #include <petsc/private/dmpleximpl.h>
  3:  #include <petscdmda.h>
  4:  #include <petscsf.h>

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

  9:   Collective on MPI_Comm

 11:   Input Parameters:
 12: + comm - The communicator for the DM object
 13: . dim - The spatial dimension
 14: . simplex - Flag for simplicial cells, otherwise they are tensor product cells
 15: . interpolate - Flag to create intermediate mesh pieces (edges, faces)
 16: . refinementUniform - Flag for uniform parallel refinement
 17: - refinementLimit - A nonzero number indicates the largest admissible volume for a refined cell

 19:   Output Parameter:
 20: . dm  - The DM object

 22:   Level: beginner

 24: .keywords: DM, create
 25: .seealso: DMSetType(), DMCreate()
 26: @*/
 27: PetscErrorCode DMPlexCreateDoublet(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscBool interpolate, PetscBool refinementUniform, PetscReal refinementLimit, DM *newdm)
 28: {
 29:   DM             dm;
 30:   PetscInt       p;
 31:   PetscMPIInt    rank;

 35:   DMCreate(comm, &dm);
 36:   DMSetType(dm, DMPLEX);
 37:   DMSetDimension(dm, dim);
 38:   MPI_Comm_rank(comm, &rank);
 39:   switch (dim) {
 40:   case 2:
 41:     if (simplex) {PetscObjectSetName((PetscObject) dm, "triangular");}
 42:     else         {PetscObjectSetName((PetscObject) dm, "quadrilateral");}
 43:     break;
 44:   case 3:
 45:     if (simplex) {PetscObjectSetName((PetscObject) dm, "tetrahedral");}
 46:     else         {PetscObjectSetName((PetscObject) dm, "hexahedral");}
 47:     break;
 48:   default:
 49:     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %d", dim);
 50:   }
 51:   if (rank) {
 52:     PetscInt numPoints[2] = {0, 0};
 53:     DMPlexCreateFromDAG(dm, 1, numPoints, NULL, NULL, NULL, NULL);
 54:   } else {
 55:     switch (dim) {
 56:     case 2:
 57:       if (simplex) {
 58:         PetscInt    numPoints[2]        = {4, 2};
 59:         PetscInt    coneSize[6]         = {3, 3, 0, 0, 0, 0};
 60:         PetscInt    cones[6]            = {2, 3, 4,  5, 4, 3};
 61:         PetscInt    coneOrientations[6] = {0, 0, 0,  0, 0, 0};
 62:         PetscScalar vertexCoords[8]     = {-0.5, 0.5, 0.0, 0.0, 0.0, 1.0, 0.5, 0.5};
 63:         PetscInt    markerPoints[8]     = {2, 1, 3, 1, 4, 1, 5, 1};

 65:         DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
 66:         for (p = 0; p < 4; ++p) {DMSetLabelValue(dm, "marker", markerPoints[p*2], markerPoints[p*2+1]);}
 67:       } else {
 68:         PetscInt    numPoints[2]        = {6, 2};
 69:         PetscInt    coneSize[8]         = {4, 4, 0, 0, 0, 0, 0, 0};
 70:         PetscInt    cones[8]            = {2, 3, 4, 5,  3, 6, 7, 4};
 71:         PetscInt    coneOrientations[8] = {0, 0, 0, 0,  0, 0, 0, 0};
 72:         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};

 74:         DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
 75:       }
 76:       break;
 77:     case 3:
 78:       if (simplex) {
 79:         PetscInt    numPoints[2]        = {5, 2};
 80:         PetscInt    coneSize[7]         = {4, 4, 0, 0, 0, 0, 0};
 81:         PetscInt    cones[8]            = {4, 3, 5, 2,  5, 3, 4, 6};
 82:         PetscInt    coneOrientations[8] = {0, 0, 0, 0,  0, 0, 0, 0};
 83:         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};
 84:         PetscInt    markerPoints[10]    = {2, 1, 3, 1, 4, 1, 5, 1, 6, 1};

 86:         DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
 87:         for (p = 0; p < 5; ++p) {DMSetLabelValue(dm, "marker", markerPoints[p*2], markerPoints[p*2+1]);}
 88:       } else {
 89:         PetscInt    numPoints[2]         = {12, 2};
 90:         PetscInt    coneSize[14]         = {8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
 91:         PetscInt    cones[16]            = {2, 3, 4, 5, 6, 7, 8, 9,  5, 4, 10, 11, 7, 12, 13, 8};
 92:         PetscInt    coneOrientations[16] = {0, 0, 0, 0, 0, 0, 0, 0,  0, 0,  0,  0, 0,  0,  0, 0};
 93:         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,
 94:                                             -1.0, -0.5,  0.5,   0.0, -0.5,  0.5,  0.0,  0.5,  0.5,  -1.0,  0.5,  0.5,
 95:                                              1.0,  0.5, -0.5,   1.0, -0.5, -0.5,  1.0, -0.5,  0.5,   1.0,  0.5,  0.5};

 97:         DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
 98:       }
 99:       break;
100:     default:
101:       SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %d", dim);
102:     }
103:   }
104:   *newdm = dm;
105:   if (refinementLimit > 0.0) {
106:     DM rdm;
107:     const char *name;

109:     DMPlexSetRefinementUniform(*newdm, PETSC_FALSE);
110:     DMPlexSetRefinementLimit(*newdm, refinementLimit);
111:     DMRefine(*newdm, comm, &rdm);
112:     PetscObjectGetName((PetscObject) *newdm, &name);
113:     PetscObjectSetName((PetscObject)    rdm,  name);
114:     DMDestroy(newdm);
115:     *newdm = rdm;
116:   }
117:   if (interpolate) {
118:     DM idm = NULL;
119:     const char *name;

121:     DMPlexInterpolate(*newdm, &idm);
122:     PetscObjectGetName((PetscObject) *newdm, &name);
123:     PetscObjectSetName((PetscObject)    idm,  name);
124:     DMPlexCopyCoordinates(*newdm, idm);
125:     DMCopyLabels(*newdm, idm);
126:     DMDestroy(newdm);
127:     *newdm = idm;
128:   }
129:   {
130:     DM refinedMesh     = NULL;
131:     DM distributedMesh = NULL;

133:     /* Distribute mesh over processes */
134:     DMPlexDistribute(*newdm, 0, NULL, &distributedMesh);
135:     if (distributedMesh) {
136:       DMDestroy(newdm);
137:       *newdm = distributedMesh;
138:     }
139:     if (refinementUniform) {
140:       DMPlexSetRefinementUniform(*newdm, refinementUniform);
141:       DMRefine(*newdm, comm, &refinedMesh);
142:       if (refinedMesh) {
143:         DMDestroy(newdm);
144:         *newdm = refinedMesh;
145:       }
146:     }
147:   }
148:   return(0);
149: }

151: /*@
152:   DMPlexCreateSquareBoundary - Creates a 1D mesh the is the boundary of a square lattice.

154:   Collective on MPI_Comm

156:   Input Parameters:
157: + comm  - The communicator for the DM object
158: . lower - The lower left corner coordinates
159: . upper - The upper right corner coordinates
160: - edges - The number of cells in each direction

162:   Output Parameter:
163: . dm  - The DM object

165:   Note: Here is the numbering returned for 2 cells in each direction:
166: $ 18--5-17--4--16
167: $  |     |     |
168: $  6    10     3
169: $  |     |     |
170: $ 19-11-20--9--15
171: $  |     |     |
172: $  7     8     2
173: $  |     |     |
174: $ 12--0-13--1--14

176:   Level: beginner

178: .keywords: DM, create
179: .seealso: DMPlexCreateBoxMesh(), DMPlexCreateCubeBoundary(), DMSetType(), DMCreate()
180: @*/
181: PetscErrorCode DMPlexCreateSquareBoundary(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
182: {
183:   const PetscInt numVertices    = (edges[0]+1)*(edges[1]+1);
184:   const PetscInt numEdges       = edges[0]*(edges[1]+1) + (edges[0]+1)*edges[1];
185:   PetscInt       markerTop      = 1;
186:   PetscInt       markerBottom   = 1;
187:   PetscInt       markerRight    = 1;
188:   PetscInt       markerLeft     = 1;
189:   PetscBool      markerSeparate = PETSC_FALSE;
190:   Vec            coordinates;
191:   PetscSection   coordSection;
192:   PetscScalar    *coords;
193:   PetscInt       coordSize;
194:   PetscMPIInt    rank;
195:   PetscInt       v, vx, vy;

199:   PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);
200:   if (markerSeparate) {
201:     markerTop    = 3;
202:     markerBottom = 1;
203:     markerRight  = 2;
204:     markerLeft   = 4;
205:   }
206:   MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
207:   if (!rank) {
208:     PetscInt e, ex, ey;

210:     DMPlexSetChart(dm, 0, numEdges+numVertices);
211:     for (e = 0; e < numEdges; ++e) {
212:       DMPlexSetConeSize(dm, e, 2);
213:     }
214:     DMSetUp(dm); /* Allocate space for cones */
215:     for (vx = 0; vx <= edges[0]; vx++) {
216:       for (ey = 0; ey < edges[1]; ey++) {
217:         PetscInt edge   = vx*edges[1] + ey + edges[0]*(edges[1]+1);
218:         PetscInt vertex = ey*(edges[0]+1) + vx + numEdges;
219:         PetscInt cone[2];

221:         cone[0] = vertex; cone[1] = vertex+edges[0]+1;
222:         DMPlexSetCone(dm, edge, cone);
223:         if (vx == edges[0]) {
224:           DMSetLabelValue(dm, "marker", edge,    markerRight);
225:           DMSetLabelValue(dm, "marker", cone[0], markerRight);
226:           if (ey == edges[1]-1) {
227:             DMSetLabelValue(dm, "marker", cone[1], markerRight);
228:             DMSetLabelValue(dm, "Face Sets", cone[1], markerRight);
229:           }
230:         } else if (vx == 0) {
231:           DMSetLabelValue(dm, "marker", edge,    markerLeft);
232:           DMSetLabelValue(dm, "marker", cone[0], markerLeft);
233:           if (ey == edges[1]-1) {
234:             DMSetLabelValue(dm, "marker", cone[1], markerLeft);
235:             DMSetLabelValue(dm, "Face Sets", cone[1], markerLeft);
236:           }
237:         }
238:       }
239:     }
240:     for (vy = 0; vy <= edges[1]; vy++) {
241:       for (ex = 0; ex < edges[0]; ex++) {
242:         PetscInt edge   = vy*edges[0]     + ex;
243:         PetscInt vertex = vy*(edges[0]+1) + ex + numEdges;
244:         PetscInt cone[2];

246:         cone[0] = vertex; cone[1] = vertex+1;
247:         DMPlexSetCone(dm, edge, cone);
248:         if (vy == edges[1]) {
249:           DMSetLabelValue(dm, "marker", edge,    markerTop);
250:           DMSetLabelValue(dm, "marker", cone[0], markerTop);
251:           if (ex == edges[0]-1) {
252:             DMSetLabelValue(dm, "marker", cone[1], markerTop);
253:             DMSetLabelValue(dm, "Face Sets", cone[1], markerTop);
254:           }
255:         } else if (vy == 0) {
256:           DMSetLabelValue(dm, "marker", edge,    markerBottom);
257:           DMSetLabelValue(dm, "marker", cone[0], markerBottom);
258:           if (ex == edges[0]-1) {
259:             DMSetLabelValue(dm, "marker", cone[1], markerBottom);
260:             DMSetLabelValue(dm, "Face Sets", cone[1], markerBottom);
261:           }
262:         }
263:       }
264:     }
265:   }
266:   DMPlexSymmetrize(dm);
267:   DMPlexStratify(dm);
268:   /* Build coordinates */
269:   DMSetCoordinateDim(dm, 2);
270:   DMGetCoordinateSection(dm, &coordSection);
271:   PetscSectionSetNumFields(coordSection, 1);
272:   PetscSectionSetChart(coordSection, numEdges, numEdges + numVertices);
273:   PetscSectionSetFieldComponents(coordSection, 0, 2);
274:   for (v = numEdges; v < numEdges+numVertices; ++v) {
275:     PetscSectionSetDof(coordSection, v, 2);
276:     PetscSectionSetFieldDof(coordSection, v, 0, 2);
277:   }
278:   PetscSectionSetUp(coordSection);
279:   PetscSectionGetStorageSize(coordSection, &coordSize);
280:   VecCreate(PETSC_COMM_SELF, &coordinates);
281:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
282:   VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
283:   VecSetBlockSize(coordinates, 2);
284:   VecSetType(coordinates,VECSTANDARD);
285:   VecGetArray(coordinates, &coords);
286:   for (vy = 0; vy <= edges[1]; ++vy) {
287:     for (vx = 0; vx <= edges[0]; ++vx) {
288:       coords[(vy*(edges[0]+1)+vx)*2+0] = lower[0] + ((upper[0] - lower[0])/edges[0])*vx;
289:       coords[(vy*(edges[0]+1)+vx)*2+1] = lower[1] + ((upper[1] - lower[1])/edges[1])*vy;
290:     }
291:   }
292:   VecRestoreArray(coordinates, &coords);
293:   DMSetCoordinatesLocal(dm, coordinates);
294:   VecDestroy(&coordinates);
295:   return(0);
296: }

298: /*@
299:   DMPlexCreateCubeBoundary - Creates a 2D mesh the is the boundary of a cubic lattice.

301:   Collective on MPI_Comm

303:   Input Parameters:
304: + comm  - The communicator for the DM object
305: . lower - The lower left front corner coordinates
306: . upper - The upper right back corner coordinates
307: - edges - The number of cells in each direction

309:   Output Parameter:
310: . dm  - The DM object

312:   Level: beginner

314: .keywords: DM, create
315: .seealso: DMPlexCreateBoxMesh(), DMPlexCreateSquareBoundary(), DMSetType(), DMCreate()
316: @*/
317: PetscErrorCode DMPlexCreateCubeBoundary(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt faces[])
318: {
319:   PetscInt       vertices[3], numVertices;
320:   PetscInt       numFaces    = 2*faces[0]*faces[1] + 2*faces[1]*faces[2] + 2*faces[0]*faces[2];
321:   Vec            coordinates;
322:   PetscSection   coordSection;
323:   PetscScalar    *coords;
324:   PetscInt       coordSize;
325:   PetscMPIInt    rank;
326:   PetscInt       v, vx, vy, vz;
327:   PetscInt       voffset, iface=0, cone[4];

331:   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");
332:   MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
333:   vertices[0] = faces[0]+1; vertices[1] = faces[1]+1; vertices[2] = faces[2]+1;
334:   numVertices = vertices[0]*vertices[1]*vertices[2];
335:   if (!rank) {
336:     PetscInt f;

338:     DMPlexSetChart(dm, 0, numFaces+numVertices);
339:     for (f = 0; f < numFaces; ++f) {
340:       DMPlexSetConeSize(dm, f, 4);
341:     }
342:     DMSetUp(dm); /* Allocate space for cones */
343:     for (v = 0; v < numFaces+numVertices; ++v) {
344:       DMSetLabelValue(dm, "marker", v, 1);
345:     }

347:     /* Side 0 (Top) */
348:     for (vy = 0; vy < faces[1]; vy++) {
349:       for (vx = 0; vx < faces[0]; vx++) {
350:         voffset = numFaces + vertices[0]*vertices[1]*(vertices[2]-1) + vy*vertices[0] + vx;
351:         cone[0] = voffset; cone[1] = voffset+1; cone[2] = voffset+vertices[0]+1; cone[3] = voffset+vertices[0];
352:         DMPlexSetCone(dm, iface, cone);
353:         iface++;
354:       }
355:     }

357:     /* Side 1 (Bottom) */
358:     for (vy = 0; vy < faces[1]; vy++) {
359:       for (vx = 0; vx < faces[0]; vx++) {
360:         voffset = numFaces + vy*(faces[0]+1) + vx;
361:         cone[0] = voffset+1; cone[1] = voffset; cone[2] = voffset+vertices[0]; cone[3] = voffset+vertices[0]+1;
362:         DMPlexSetCone(dm, iface, cone);
363:         iface++;
364:       }
365:     }

367:     /* Side 2 (Front) */
368:     for (vz = 0; vz < faces[2]; vz++) {
369:       for (vx = 0; vx < faces[0]; vx++) {
370:         voffset = numFaces + vz*vertices[0]*vertices[1] + vx;
371:         cone[0] = voffset; cone[1] = voffset+1; cone[2] = voffset+vertices[0]*vertices[1]+1; cone[3] = voffset+vertices[0]*vertices[1];
372:         DMPlexSetCone(dm, iface, cone);
373:         iface++;
374:       }
375:     }

377:     /* Side 3 (Back) */
378:     for (vz = 0; vz < faces[2]; vz++) {
379:       for (vx = 0; vx < faces[0]; vx++) {
380:         voffset = numFaces + vz*vertices[0]*vertices[1] + vertices[0]*(vertices[1]-1) + vx;
381:         cone[0] = voffset+vertices[0]*vertices[1]; cone[1] = voffset+vertices[0]*vertices[1]+1;
382:         cone[2] = voffset+1; cone[3] = voffset;
383:         DMPlexSetCone(dm, iface, cone);
384:         iface++;
385:       }
386:     }

388:     /* Side 4 (Left) */
389:     for (vz = 0; vz < faces[2]; vz++) {
390:       for (vy = 0; vy < faces[1]; vy++) {
391:         voffset = numFaces + vz*vertices[0]*vertices[1] + vy*vertices[0];
392:         cone[0] = voffset; cone[1] = voffset+vertices[0]*vertices[1];
393:         cone[2] = voffset+vertices[0]*vertices[1]+vertices[0]; cone[3] = voffset+vertices[0];
394:         DMPlexSetCone(dm, iface, cone);
395:         iface++;
396:       }
397:     }

399:     /* Side 5 (Right) */
400:     for (vz = 0; vz < faces[2]; vz++) {
401:       for (vy = 0; vy < faces[1]; vy++) {
402:         voffset = numFaces + vz*vertices[0]*vertices[1] + vy*vertices[0] + faces[0];
403:         cone[0] = voffset+vertices[0]*vertices[1]; cone[1] = voffset;
404:         cone[2] = voffset+vertices[0]; cone[3] = voffset+vertices[0]*vertices[1]+vertices[0];
405:         DMPlexSetCone(dm, iface, cone);
406:         iface++;
407:       }
408:     }
409:   }
410:   DMPlexSymmetrize(dm);
411:   DMPlexStratify(dm);
412:   /* Build coordinates */
413:   DMSetCoordinateDim(dm, 3);
414:   DMGetCoordinateSection(dm, &coordSection);
415:   PetscSectionSetChart(coordSection, numFaces, numFaces + numVertices);
416:   for (v = numFaces; v < numFaces+numVertices; ++v) {
417:     PetscSectionSetDof(coordSection, v, 3);
418:   }
419:   PetscSectionSetUp(coordSection);
420:   PetscSectionGetStorageSize(coordSection, &coordSize);
421:   VecCreate(PETSC_COMM_SELF, &coordinates);
422:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
423:   VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
424:   VecSetBlockSize(coordinates, 3);
425:   VecSetType(coordinates,VECSTANDARD);
426:   VecGetArray(coordinates, &coords);
427:   for (vz = 0; vz <= faces[2]; ++vz) {
428:     for (vy = 0; vy <= faces[1]; ++vy) {
429:       for (vx = 0; vx <= faces[0]; ++vx) {
430:         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+0] = lower[0] + ((upper[0] - lower[0])/faces[0])*vx;
431:         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+1] = lower[1] + ((upper[1] - lower[1])/faces[1])*vy;
432:         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+2] = lower[2] + ((upper[2] - lower[2])/faces[2])*vz;
433:       }
434:     }
435:   }
436:   VecRestoreArray(coordinates, &coords);
437:   DMSetCoordinatesLocal(dm, coordinates);
438:   VecDestroy(&coordinates);
439:   return(0);
440: }

442: /*@
443:   DMPlexCreateBoxMesh - Creates a mesh on the tensor product of unit intervals (box) using simplices.

445:   Collective on MPI_Comm

447:   Input Parameters:
448: + comm - The communicator for the DM object
449: . dim - The spatial dimension
450: . numFaces - Number of faces per dimension
451: - interpolate - Flag to create intermediate mesh pieces (edges, faces)

453:   Output Parameter:
454: . dm  - The DM object

456:   Level: beginner

458: .keywords: DM, create
459: .seealso: DMPlexCreateHexBoxMesh(), DMSetType(), DMCreate()
460: @*/
461: PetscErrorCode DMPlexCreateBoxMesh(MPI_Comm comm, PetscInt dim, PetscInt numFaces, PetscBool interpolate, DM *dm)
462: {
463:   DM             boundary;

468:   DMCreate(comm, &boundary);
470:   DMSetType(boundary, DMPLEX);
471:   DMSetDimension(boundary, dim-1);
472:   DMSetCoordinateDim(boundary, dim);
473:   switch (dim) {
474:   case 2:
475:   {
476:     PetscReal lower[2] = {0.0, 0.0};
477:     PetscReal upper[2] = {1.0, 1.0};
478:     PetscInt  edges[2];

480:     edges[0] = numFaces; edges[1] = numFaces;
481:     DMPlexCreateSquareBoundary(boundary, lower, upper, edges);
482:     break;
483:   }
484:   case 3:
485:   {
486:     PetscReal lower[3] = {0.0, 0.0, 0.0};
487:     PetscReal upper[3] = {1.0, 1.0, 1.0};
488:     PetscInt  faces[3];

490:     faces[0] = numFaces; faces[1] = numFaces; faces[2] = numFaces;
491:     DMPlexCreateCubeBoundary(boundary, lower, upper, faces);
492:     break;
493:   }
494:   default:
495:     SETERRQ1(comm, PETSC_ERR_SUP, "Dimension not supported: %d", dim);
496:   }
497:   DMPlexGenerate(boundary, NULL, interpolate, dm);
498:   DMDestroy(&boundary);
499:   return(0);
500: }

502: static PetscErrorCode DMPlexCreateCubeMesh_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY, DMBoundaryType bdZ)
503: {
504:   DMLabel        cutLabel = NULL;
505:   PetscInt       markerTop      = 1, faceMarkerTop      = 1;
506:   PetscInt       markerBottom   = 1, faceMarkerBottom   = 1;
507:   PetscInt       markerFront    = 1, faceMarkerFront    = 1;
508:   PetscInt       markerBack     = 1, faceMarkerBack     = 1;
509:   PetscInt       markerRight    = 1, faceMarkerRight    = 1;
510:   PetscInt       markerLeft     = 1, faceMarkerLeft     = 1;
511:   PetscInt       dim;
512:   PetscBool      markerSeparate = PETSC_FALSE, cutMarker = PETSC_FALSE;
513:   PetscMPIInt    rank;

517:   DMGetDimension(dm,&dim);
518:   MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
519:   DMCreateLabel(dm,"marker");
520:   DMCreateLabel(dm,"Face Sets");
521:   if (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ||
522:       bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ||
523:       bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST) {
524:     PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL);
525:     if (cutMarker) {DMCreateLabel(dm, "periodic_cut"); DMGetLabel(dm, "periodic_cut", &cutLabel);}
526:   }
527:   switch (dim) {
528:   case 2:
529:     faceMarkerTop    = 3;
530:     faceMarkerBottom = 1;
531:     faceMarkerRight  = 2;
532:     faceMarkerLeft   = 4;
533:     break;
534:   case 3:
535:     faceMarkerBottom = 1;
536:     faceMarkerTop    = 2;
537:     faceMarkerFront  = 3;
538:     faceMarkerBack   = 4;
539:     faceMarkerRight  = 5;
540:     faceMarkerLeft   = 6;
541:     break;
542:   default:
543:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Dimension %d not supported",dim);
544:     break;
545:   }
546:   PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);
547:   if (markerSeparate) {
548:     markerBottom = faceMarkerBottom;
549:     markerTop    = faceMarkerTop;
550:     markerFront  = faceMarkerFront;
551:     markerBack   = faceMarkerBack;
552:     markerRight  = faceMarkerRight;
553:     markerLeft   = faceMarkerLeft;
554:   }
555:   {
556:     const PetscInt numXEdges    = !rank ? edges[0] : 0;
557:     const PetscInt numYEdges    = !rank ? edges[1] : 0;
558:     const PetscInt numZEdges    = !rank ? edges[2] : 0;
559:     const PetscInt numXVertices = !rank ? (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ? edges[0] : edges[0]+1) : 0;
560:     const PetscInt numYVertices = !rank ? (bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ? edges[1] : edges[1]+1) : 0;
561:     const PetscInt numZVertices = !rank ? (bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST ? edges[2] : edges[2]+1) : 0;
562:     const PetscInt numCells     = numXEdges*numYEdges*numZEdges;
563:     const PetscInt numXFaces    = numYEdges*numZEdges;
564:     const PetscInt numYFaces    = numXEdges*numZEdges;
565:     const PetscInt numZFaces    = numXEdges*numYEdges;
566:     const PetscInt numTotXFaces = numXVertices*numXFaces;
567:     const PetscInt numTotYFaces = numYVertices*numYFaces;
568:     const PetscInt numTotZFaces = numZVertices*numZFaces;
569:     const PetscInt numFaces     = numTotXFaces + numTotYFaces + numTotZFaces;
570:     const PetscInt numTotXEdges = numXEdges*numYVertices*numZVertices;
571:     const PetscInt numTotYEdges = numYEdges*numXVertices*numZVertices;
572:     const PetscInt numTotZEdges = numZEdges*numXVertices*numYVertices;
573:     const PetscInt numVertices  = numXVertices*numYVertices*numZVertices;
574:     const PetscInt numEdges     = numTotXEdges + numTotYEdges + numTotZEdges;
575:     const PetscInt firstVertex  = (dim == 2) ? numFaces : numCells;
576:     const PetscInt firstXFace   = (dim == 2) ? 0 : numCells + numVertices;
577:     const PetscInt firstYFace   = firstXFace + numTotXFaces;
578:     const PetscInt firstZFace   = firstYFace + numTotYFaces;
579:     const PetscInt firstXEdge   = numCells + numFaces + numVertices;
580:     const PetscInt firstYEdge   = firstXEdge + numTotXEdges;
581:     const PetscInt firstZEdge   = firstYEdge + numTotYEdges;
582:     Vec            coordinates;
583:     PetscSection   coordSection;
584:     PetscScalar   *coords;
585:     PetscInt       coordSize;
586:     PetscInt       v, vx, vy, vz;
587:     PetscInt       c, f, fx, fy, fz, e, ex, ey, ez;

589:     DMPlexSetChart(dm, 0, numCells+numFaces+numEdges+numVertices);
590:     for (c = 0; c < numCells; c++) {
591:       DMPlexSetConeSize(dm, c, 6);
592:     }
593:     for (f = firstXFace; f < firstXFace+numFaces; ++f) {
594:       DMPlexSetConeSize(dm, f, 4);
595:     }
596:     for (e = firstXEdge; e < firstXEdge+numEdges; ++e) {
597:       DMPlexSetConeSize(dm, e, 2);
598:     }
599:     DMSetUp(dm); /* Allocate space for cones */
600:     /* Build cells */
601:     for (fz = 0; fz < numZEdges; ++fz) {
602:       for (fy = 0; fy < numYEdges; ++fy) {
603:         for (fx = 0; fx < numXEdges; ++fx) {
604:           PetscInt cell    = (fz*numYEdges + fy)*numXEdges + fx;
605:           PetscInt faceB   = firstZFace + (fy*numXEdges+fx)*numZVertices +   fz;
606:           PetscInt faceT   = firstZFace + (fy*numXEdges+fx)*numZVertices + ((fz+1)%numZVertices);
607:           PetscInt faceF   = firstYFace + (fz*numXEdges+fx)*numYVertices +   fy;
608:           PetscInt faceK   = firstYFace + (fz*numXEdges+fx)*numYVertices + ((fy+1)%numYVertices);
609:           PetscInt faceL   = firstXFace + (fz*numYEdges+fy)*numXVertices +   fx;
610:           PetscInt faceR   = firstXFace + (fz*numYEdges+fy)*numXVertices + ((fx+1)%numXVertices);
611:                             /* B,  T,  F,  K,  R,  L */
612:           PetscInt ornt[6] = {-4,  0,  0, -1,  0, -4}; /* ??? */
613:           PetscInt cone[6];

615:           /* no boundary twisting in 3D */
616:           cone[0] = faceB; cone[1] = faceT; cone[2] = faceF; cone[3] = faceK; cone[4] = faceR; cone[5] = faceL;
617:           DMPlexSetCone(dm, cell, cone);
618:           DMPlexSetConeOrientation(dm, cell, ornt);
619:           if (bdX != DM_BOUNDARY_NONE && fx == numXEdges-1 && cutLabel) {DMLabelSetValue(cutLabel, cell, 2);}
620:           if (bdY != DM_BOUNDARY_NONE && fy == numYEdges-1 && cutLabel) {DMLabelSetValue(cutLabel, cell, 2);}
621:           if (bdZ != DM_BOUNDARY_NONE && fz == numZEdges-1 && cutLabel) {DMLabelSetValue(cutLabel, cell, 2);}
622:         }
623:       }
624:     }
625:     /* Build x faces */
626:     for (fz = 0; fz < numZEdges; ++fz) {
627:       for (fy = 0; fy < numYEdges; ++fy) {
628:         for (fx = 0; fx < numXVertices; ++fx) {
629:           PetscInt face    = firstXFace + (fz*numYEdges+fy)*numXVertices + fx;
630:           PetscInt edgeL   = firstZEdge + (  fy*                 numXVertices+fx)*numZEdges + fz;
631:           PetscInt edgeR   = firstZEdge + (((fy+1)%numYVertices)*numXVertices+fx)*numZEdges + fz;
632:           PetscInt edgeB   = firstYEdge + (  fz*                 numXVertices+fx)*numYEdges + fy;
633:           PetscInt edgeT   = firstYEdge + (((fz+1)%numZVertices)*numXVertices+fx)*numYEdges + fy;
634:           PetscInt ornt[4] = {0, 0, -2, -2};
635:           PetscInt cone[4];

637:           if (dim == 3) {
638:             /* markers */
639:             if (bdX != DM_BOUNDARY_PERIODIC) {
640:               if (fx == numXVertices-1) {
641:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerRight);
642:                 DMSetLabelValue(dm, "marker", face, markerRight);
643:               }
644:               else if (fx == 0) {
645:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerLeft);
646:                 DMSetLabelValue(dm, "marker", face, markerLeft);
647:               }
648:             }
649:           }
650:           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
651:           DMPlexSetCone(dm, face, cone);
652:           DMPlexSetConeOrientation(dm, face, ornt);
653:         }
654:       }
655:     }
656:     /* Build y faces */
657:     for (fz = 0; fz < numZEdges; ++fz) {
658:       for (fx = 0; fx < numXEdges; ++fx) {
659:         for (fy = 0; fy < numYVertices; ++fy) {
660:           PetscInt face    = firstYFace + (fz*numXEdges+fx)*numYVertices + fy;
661:           PetscInt edgeL   = firstZEdge + (fy*numXVertices+  fx                 )*numZEdges + fz;
662:           PetscInt edgeR   = firstZEdge + (fy*numXVertices+((fx+1)%numXVertices))*numZEdges + fz;
663:           PetscInt edgeB   = firstXEdge + (  fz                 *numYVertices+fy)*numXEdges + fx;
664:           PetscInt edgeT   = firstXEdge + (((fz+1)%numZVertices)*numYVertices+fy)*numXEdges + fx;
665:           PetscInt ornt[4] = {0, 0, -2, -2};
666:           PetscInt cone[4];

668:           if (dim == 3) {
669:             /* markers */
670:             if (bdY != DM_BOUNDARY_PERIODIC) {
671:               if (fy == numYVertices-1) {
672:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerBack);
673:                 DMSetLabelValue(dm, "marker", face, markerBack);
674:               }
675:               else if (fy == 0) {
676:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerFront);
677:                 DMSetLabelValue(dm, "marker", face, markerFront);
678:               }
679:             }
680:           }
681:           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
682:           DMPlexSetCone(dm, face, cone);
683:           DMPlexSetConeOrientation(dm, face, ornt);
684:         }
685:       }
686:     }
687:     /* Build z faces */
688:     for (fy = 0; fy < numYEdges; ++fy) {
689:       for (fx = 0; fx < numXEdges; ++fx) {
690:         for (fz = 0; fz < numZVertices; fz++) {
691:           PetscInt face    = firstZFace + (fy*numXEdges+fx)*numZVertices + fz;
692:           PetscInt edgeL   = firstYEdge + (fz*numXVertices+  fx                 )*numYEdges + fy;
693:           PetscInt edgeR   = firstYEdge + (fz*numXVertices+((fx+1)%numXVertices))*numYEdges + fy;
694:           PetscInt edgeB   = firstXEdge + (fz*numYVertices+  fy                 )*numXEdges + fx;
695:           PetscInt edgeT   = firstXEdge + (fz*numYVertices+((fy+1)%numYVertices))*numXEdges + fx;
696:           PetscInt ornt[4] = {0, 0, -2, -2};
697:           PetscInt cone[4];

699:           if (dim == 2) {
700:             if (bdX == DM_BOUNDARY_TWIST && fx == numXEdges-1) {edgeR += numYEdges-1-2*fy; ornt[1] = -2;}
701:             if (bdY == DM_BOUNDARY_TWIST && fy == numYEdges-1) {edgeT += numXEdges-1-2*fx; ornt[2] =  0;}
702:             if (bdX != DM_BOUNDARY_NONE && fx == numXEdges-1 && cutLabel) {DMLabelSetValue(cutLabel, face, 2);}
703:             if (bdY != DM_BOUNDARY_NONE && fy == numYEdges-1 && cutLabel) {DMLabelSetValue(cutLabel, face, 2);}
704:           } else {
705:             /* markers */
706:             if (bdZ != DM_BOUNDARY_PERIODIC) {
707:               if (fz == numZVertices-1) {
708:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerTop);
709:                 DMSetLabelValue(dm, "marker", face, markerTop);
710:               }
711:               else if (fz == 0) {
712:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerBottom);
713:                 DMSetLabelValue(dm, "marker", face, markerBottom);
714:               }
715:             }
716:           }
717:           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
718:           DMPlexSetCone(dm, face, cone);
719:           DMPlexSetConeOrientation(dm, face, ornt);
720:         }
721:       }
722:     }
723:     /* Build Z edges*/
724:     for (vy = 0; vy < numYVertices; vy++) {
725:       for (vx = 0; vx < numXVertices; vx++) {
726:         for (ez = 0; ez < numZEdges; ez++) {
727:           const PetscInt edge    = firstZEdge  + (vy*numXVertices+vx)*numZEdges + ez;
728:           const PetscInt vertexB = firstVertex + (  ez                 *numYVertices+vy)*numXVertices + vx;
729:           const PetscInt vertexT = firstVertex + (((ez+1)%numZVertices)*numYVertices+vy)*numXVertices + vx;
730:           PetscInt       cone[2];

732:           if (dim == 3) {
733:             if (bdX != DM_BOUNDARY_PERIODIC) {
734:               if (vx == numXVertices-1) {
735:                 DMSetLabelValue(dm, "marker", edge, markerRight);
736:               }
737:               else if (vx == 0) {
738:                 DMSetLabelValue(dm, "marker", edge, markerLeft);
739:               }
740:             }
741:             if (bdY != DM_BOUNDARY_PERIODIC) {
742:               if (vy == numYVertices-1) {
743:                 DMSetLabelValue(dm, "marker", edge, markerBack);
744:               }
745:               else if (vy == 0) {
746:                 DMSetLabelValue(dm, "marker", edge, markerFront);
747:               }
748:             }
749:           }
750:           cone[0] = vertexB; cone[1] = vertexT;
751:           DMPlexSetCone(dm, edge, cone);
752:         }
753:       }
754:     }
755:     /* Build Y edges*/
756:     for (vz = 0; vz < numZVertices; vz++) {
757:       for (vx = 0; vx < numXVertices; vx++) {
758:         for (ey = 0; ey < numYEdges; ey++) {
759:           const PetscInt nextv   = (dim == 2 && bdY == DM_BOUNDARY_TWIST && ey == numYEdges-1) ? (numXVertices-vx-1) : (vz*numYVertices+((ey+1)%numYVertices))*numXVertices + vx;
760:           const PetscInt edge    = firstYEdge  + (vz*numXVertices+vx)*numYEdges + ey;
761:           const PetscInt vertexF = firstVertex + (vz*numYVertices+ey)*numXVertices + vx;
762:           const PetscInt vertexK = firstVertex + nextv;
763:           PetscInt       cone[2];

765:           cone[0] = vertexF; cone[1] = vertexK;
766:           DMPlexSetCone(dm, edge, cone);
767:           if (dim == 2) {
768:             if ((bdX != DM_BOUNDARY_PERIODIC) && (bdX != DM_BOUNDARY_TWIST)) {
769:               if (vx == numXVertices-1) {
770:                 DMSetLabelValue(dm, "Face Sets", edge, faceMarkerRight);
771:                 DMSetLabelValue(dm, "marker", edge,    markerRight);
772:                 DMSetLabelValue(dm, "marker", cone[0], markerRight);
773:                 if (ey == numYEdges-1) {
774:                   DMSetLabelValue(dm, "marker", cone[1], markerRight);
775:                 }
776:               } else if (vx == 0) {
777:                 DMSetLabelValue(dm, "Face Sets", edge, faceMarkerLeft);
778:                 DMSetLabelValue(dm, "marker", edge,    markerLeft);
779:                 DMSetLabelValue(dm, "marker", cone[0], markerLeft);
780:                 if (ey == numYEdges-1) {
781:                   DMSetLabelValue(dm, "marker", cone[1], markerLeft);
782:                 }
783:               }
784:             } else {
785:               if (vx == 0 && cutMarker) {
786:                 DMLabelSetValue(cutLabel, edge,    1);
787:                 DMLabelSetValue(cutLabel, cone[0], 1);
788:                 if (ey == numYEdges-1) {
789:                   DMLabelSetValue(cutLabel, cone[1], 1);
790:                 }
791:               }
792:             }
793:           } else {
794:             if (bdX != DM_BOUNDARY_PERIODIC) {
795:               if (vx == numXVertices-1) {
796:                 DMSetLabelValue(dm, "marker", edge, markerRight);
797:               } else if (vx == 0) {
798:                 DMSetLabelValue(dm, "marker", edge, markerLeft);
799:               }
800:             }
801:             if (bdZ != DM_BOUNDARY_PERIODIC) {
802:               if (vz == numZVertices-1) {
803:                 DMSetLabelValue(dm, "marker", edge, markerTop);
804:               } else if (vz == 0) {
805:                 DMSetLabelValue(dm, "marker", edge, markerBottom);
806:               }
807:             }
808:           }
809:         }
810:       }
811:     }
812:     /* Build X edges*/
813:     for (vz = 0; vz < numZVertices; vz++) {
814:       for (vy = 0; vy < numYVertices; vy++) {
815:         for (ex = 0; ex < numXEdges; ex++) {
816:           const PetscInt nextv   = (dim == 2 && bdX == DM_BOUNDARY_TWIST && ex == numXEdges-1) ? (numYVertices-vy-1)*numXVertices : (vz*numYVertices+vy)*numXVertices + (ex+1)%numXVertices;
817:           const PetscInt edge    = firstXEdge  + (vz*numYVertices+vy)*numXEdges + ex;
818:           const PetscInt vertexL = firstVertex + (vz*numYVertices+vy)*numXVertices + ex;
819:           const PetscInt vertexR = firstVertex + nextv;
820:           PetscInt       cone[2];

822:           cone[0] = vertexL; cone[1] = vertexR;
823:           DMPlexSetCone(dm, edge, cone);
824:           if (dim == 2) {
825:             if ((bdY != DM_BOUNDARY_PERIODIC) && (bdY != DM_BOUNDARY_TWIST)) {
826:               if (vy == numYVertices-1) {
827:                 DMSetLabelValue(dm, "Face Sets", edge, faceMarkerTop);
828:                 DMSetLabelValue(dm, "marker", edge,    markerTop);
829:                 DMSetLabelValue(dm, "marker", cone[0], markerTop);
830:                 if (ex == numXEdges-1) {
831:                   DMSetLabelValue(dm, "marker", cone[1], markerTop);
832:                 }
833:               } else if (vy == 0) {
834:                 DMSetLabelValue(dm, "Face Sets", edge, faceMarkerBottom);
835:                 DMSetLabelValue(dm, "marker", edge,    markerBottom);
836:                 DMSetLabelValue(dm, "marker", cone[0], markerBottom);
837:                 if (ex == numXEdges-1) {
838:                   DMSetLabelValue(dm, "marker", cone[1], markerBottom);
839:                 }
840:               }
841:             } else {
842:               if (vy == 0 && cutMarker) {
843:                 DMLabelSetValue(cutLabel, edge,    1);
844:                 DMLabelSetValue(cutLabel, cone[0], 1);
845:                 if (ex == numXEdges-1) {
846:                   DMLabelSetValue(cutLabel, cone[1], 1);
847:                 }
848:               }
849:             }
850:           } else {
851:             if (bdY != DM_BOUNDARY_PERIODIC) {
852:               if (vy == numYVertices-1) {
853:                 DMSetLabelValue(dm, "marker", edge, markerBack);
854:               }
855:               else if (vy == 0) {
856:                 DMSetLabelValue(dm, "marker", edge, markerFront);
857:               }
858:             }
859:             if (bdZ != DM_BOUNDARY_PERIODIC) {
860:               if (vz == numZVertices-1) {
861:                 DMSetLabelValue(dm, "marker", edge, markerTop);
862:               }
863:               else if (vz == 0) {
864:                 DMSetLabelValue(dm, "marker", edge, markerBottom);
865:               }
866:             }
867:           }
868:         }
869:       }
870:     }
871:     DMPlexSymmetrize(dm);
872:     DMPlexStratify(dm);
873:     /* Build coordinates */
874:     DMGetCoordinateSection(dm, &coordSection);
875:     PetscSectionSetNumFields(coordSection, 1);
876:     PetscSectionSetFieldComponents(coordSection, 0, dim);
877:     PetscSectionSetChart(coordSection, firstVertex, firstVertex+numVertices);
878:     for (v = firstVertex; v < firstVertex+numVertices; ++v) {
879:       PetscSectionSetDof(coordSection, v, dim);
880:       PetscSectionSetFieldDof(coordSection, v, 0, dim);
881:     }
882:     PetscSectionSetUp(coordSection);
883:     PetscSectionGetStorageSize(coordSection, &coordSize);
884:     VecCreate(PETSC_COMM_SELF, &coordinates);
885:     PetscObjectSetName((PetscObject) coordinates, "coordinates");
886:     VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
887:     VecSetBlockSize(coordinates, dim);
888:     VecSetType(coordinates,VECSTANDARD);
889:     VecGetArray(coordinates, &coords);
890:     for (vz = 0; vz < numZVertices; ++vz) {
891:       for (vy = 0; vy < numYVertices; ++vy) {
892:         for (vx = 0; vx < numXVertices; ++vx) {
893:           coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+0] = lower[0] + ((upper[0] - lower[0])/numXEdges)*vx;
894:           coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+1] = lower[1] + ((upper[1] - lower[1])/numYEdges)*vy;
895:           if (dim == 3) {
896:             coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+2] = lower[2] + ((upper[2] - lower[2])/numZEdges)*vz;
897:           }
898:         }
899:       }
900:     }
901:     VecRestoreArray(coordinates, &coords);
902:     DMSetCoordinatesLocal(dm, coordinates);
903:     VecDestroy(&coordinates);
904:   }
905:   return(0);
906: }

908: /*@
909:   DMPlexCreateHexBoxMesh - Creates a mesh on the tensor product of unit intervals (box) using hexahedra.

911:   Collective on MPI_Comm

913:   Input Parameters:
914: + comm  - The communicator for the DM object
915: . dim   - The spatial dimension
916: . periodicX - The boundary type for the X direction
917: . periodicY - The boundary type for the Y direction
918: . periodicZ - The boundary type for the Z direction
919: - cells - The number of cells in each direction

921:   Output Parameter:
922: . dm  - The DM object

924:   Note: Here is the numbering returned for 2 cells in each direction:
925: $ 22--8-23--9--24
926: $  |     |     |
927: $ 13  2 14  3  15
928: $  |     |     |
929: $ 19--6-20--7--21
930: $  |     |     |
931: $ 10  0 11  1 12
932: $  |     |     |
933: $ 16--4-17--5--18

935:   Level: beginner

937: .keywords: DM, create
938: .seealso: DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
939: @*/
940: PetscErrorCode DMPlexCreateHexBoxMesh(MPI_Comm comm, PetscInt dim, const PetscInt cells[], DMBoundaryType periodicX, DMBoundaryType periodicY, DMBoundaryType periodicZ, DM *dm)
941: {
942:   PetscInt       i;

947:   DMCreate(comm, dm);
949:   DMSetType(*dm, DMPLEX);
950:   DMSetDimension(*dm, dim);
951:   switch (dim) {
952:   case 2:
953:   {
954:     PetscReal lower[3] = {0.0, 0.0, 0.0};
955:     PetscReal upper[3] = {1.0, 1.0, 0.0};
956:     PetscInt  edges[3];

958:     edges[0] = cells[0];
959:     edges[1] = cells[1];
960:     edges[2] = 0;

962:     DMPlexCreateCubeMesh_Internal(*dm, lower, upper, edges, periodicX, periodicY, DM_BOUNDARY_NONE);
963:     if (periodicX == DM_BOUNDARY_PERIODIC || periodicX == DM_BOUNDARY_TWIST ||
964:         periodicY == DM_BOUNDARY_PERIODIC || periodicY == DM_BOUNDARY_TWIST) {
965:       PetscReal      L[2];
966:       PetscReal      maxCell[2];
967:       DMBoundaryType bdType[2];

969:       bdType[0] = periodicX;
970:       bdType[1] = periodicY;
971:       for (i = 0; i < dim; i++) {
972:         L[i]       = upper[i] - lower[i];
973:         maxCell[i] = 1.1 * (L[i] / PetscMax(1,cells[i]));
974:       }

976:       DMSetPeriodicity(*dm,PETSC_TRUE,maxCell,L,bdType);
977:     }
978:     break;
979:   }
980:   case 3:
981:   {
982:     PetscReal lower[3] = {0.0, 0.0, 0.0};
983:     PetscReal upper[3] = {1.0, 1.0, 1.0};

985:     DMPlexCreateCubeMesh_Internal(*dm, lower, upper, cells, periodicX, periodicY, periodicZ);
986:     if (periodicX == DM_BOUNDARY_PERIODIC || periodicX == DM_BOUNDARY_TWIST ||
987:         periodicY == DM_BOUNDARY_PERIODIC || periodicY == DM_BOUNDARY_TWIST ||
988:         periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
989:       PetscReal      L[3];
990:       PetscReal      maxCell[3];
991:       DMBoundaryType bdType[3];

993:       bdType[0] = periodicX;
994:       bdType[1] = periodicY;
995:       bdType[2] = periodicZ;
996:       for (i = 0; i < dim; i++) {
997:         L[i]       = upper[i] - lower[i];
998:         maxCell[i] = 1.1 * (L[i] / cells[i]);
999:       }

1001:       DMSetPeriodicity(*dm,PETSC_TRUE,maxCell,L,bdType);
1002:     }
1003:     break;
1004:   }
1005:   default:
1006:     SETERRQ1(comm, PETSC_ERR_SUP, "Dimension not supported: %d", dim);
1007:   }
1008:   return(0);
1009: }

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

1014:   Logically Collective on DM

1016:   Input Parameters:
1017: + dm - the DM context
1018: - prefix - the prefix to prepend to all option names

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

1024:   Level: advanced

1026: .seealso: SNESSetFromOptions()
1027: @*/
1028: PetscErrorCode DMPlexSetOptionsPrefix(DM dm, const char prefix[])
1029: {
1030:   DM_Plex       *mesh = (DM_Plex *) dm->data;

1035:   PetscObjectSetOptionsPrefix((PetscObject) dm, prefix);
1036:   PetscObjectSetOptionsPrefix((PetscObject) mesh->partitioner, prefix);
1037:   return(0);
1038: }

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

1043:   Collective on MPI_Comm

1045:   Input Parameters:
1046: + comm      - The communicator for the DM object
1047: . numRefine - The number of regular refinements to the basic 5 cell structure
1048: - periodicZ - The boundary type for the Z direction

1050:   Output Parameter:
1051: . dm  - The DM object

1053:   Note: Here is the output numbering looking from the bottom of the cylinder:
1054: $       17-----14
1055: $        |     |
1056: $        |  2  |
1057: $        |     |
1058: $ 17-----8-----7-----14
1059: $  |     |     |     |
1060: $  |  3  |  0  |  1  |
1061: $  |     |     |     |
1062: $ 19-----5-----6-----13
1063: $        |     |
1064: $        |  4  |
1065: $        |     |
1066: $       19-----13
1067: $
1068: $ and up through the top
1069: $
1070: $       18-----16
1071: $        |     |
1072: $        |  2  |
1073: $        |     |
1074: $ 18----10----11-----16
1075: $  |     |     |     |
1076: $  |  3  |  0  |  1  |
1077: $  |     |     |     |
1078: $ 20-----9----12-----15
1079: $        |     |
1080: $        |  4  |
1081: $        |     |
1082: $       20-----15

1084:   Level: beginner

1086: .keywords: DM, create
1087: .seealso: DMPlexCreateHexBoxMesh(), DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
1088: @*/
1089: PetscErrorCode DMPlexCreateHexCylinderMesh(MPI_Comm comm, PetscInt numRefine, DMBoundaryType periodicZ, DM *dm)
1090: {
1091:   const PetscInt dim = 3;
1092:   PetscInt       numCells, numVertices, r;

1097:   if (numRefine < 0) SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Number of refinements %D cannot be negative", numRefine);
1098:   DMCreate(comm, dm);
1099:   DMSetType(*dm, DMPLEX);
1100:   DMSetDimension(*dm, dim);
1101:   /* Create topology */
1102:   {
1103:     PetscInt cone[8], c;

1105:     numCells    = 5;
1106:     numVertices = 16;
1107:     if (periodicZ == DM_BOUNDARY_PERIODIC) {
1108:       numCells   *= 3;
1109:       numVertices = 24;
1110:     }
1111:     DMPlexSetChart(*dm, 0, numCells+numVertices);
1112:     for (c = 0; c < numCells; c++) {DMPlexSetConeSize(*dm, c, 8);}
1113:     DMSetUp(*dm);
1114:     if (periodicZ == DM_BOUNDARY_PERIODIC) {
1115:       cone[0] = 15; cone[1] = 18; cone[2] = 17; cone[3] = 16;
1116:       cone[4] = 31; cone[5] = 32; cone[6] = 33; cone[7] = 34;
1117:       DMPlexSetCone(*dm, 0, cone);
1118:       cone[0] = 16; cone[1] = 17; cone[2] = 24; cone[3] = 23;
1119:       cone[4] = 32; cone[5] = 36; cone[6] = 37; cone[7] = 33; /* 22 25 26 21 */
1120:       DMPlexSetCone(*dm, 1, cone);
1121:       cone[0] = 18; cone[1] = 27; cone[2] = 24; cone[3] = 17;
1122:       cone[4] = 34; cone[5] = 33; cone[6] = 37; cone[7] = 38;
1123:       DMPlexSetCone(*dm, 2, cone);
1124:       cone[0] = 29; cone[1] = 27; cone[2] = 18; cone[3] = 15;
1125:       cone[4] = 35; cone[5] = 31; cone[6] = 34; cone[7] = 38;
1126:       DMPlexSetCone(*dm, 3, cone);
1127:       cone[0] = 29; cone[1] = 15; cone[2] = 16; cone[3] = 23;
1128:       cone[4] = 35; cone[5] = 36; cone[6] = 32; cone[7] = 31;
1129:       DMPlexSetCone(*dm, 4, cone);

1131:       cone[0] = 31; cone[1] = 34; cone[2] = 33; cone[3] = 32;
1132:       cone[4] = 19; cone[5] = 22; cone[6] = 21; cone[7] = 20;
1133:       DMPlexSetCone(*dm, 5, cone);
1134:       cone[0] = 32; cone[1] = 33; cone[2] = 37; cone[3] = 36;
1135:       cone[4] = 22; cone[5] = 25; cone[6] = 26; cone[7] = 21;
1136:       DMPlexSetCone(*dm, 6, cone);
1137:       cone[0] = 34; cone[1] = 38; cone[2] = 37; cone[3] = 33;
1138:       cone[4] = 20; cone[5] = 21; cone[6] = 26; cone[7] = 28;
1139:       DMPlexSetCone(*dm, 7, cone);
1140:       cone[0] = 35; cone[1] = 38; cone[2] = 34; cone[3] = 31;
1141:       cone[4] = 30; cone[5] = 19; cone[6] = 20; cone[7] = 28;
1142:       DMPlexSetCone(*dm, 8, cone);
1143:       cone[0] = 35; cone[1] = 31; cone[2] = 32; cone[3] = 36;
1144:       cone[4] = 30; cone[5] = 25; cone[6] = 22; cone[7] = 19;
1145:       DMPlexSetCone(*dm, 9, cone);

1147:       cone[0] = 19; cone[1] = 20; cone[2] = 21; cone[3] = 22;
1148:       cone[4] = 15; cone[5] = 16; cone[6] = 17; cone[7] = 18;
1149:       DMPlexSetCone(*dm, 10, cone);
1150:       cone[0] = 22; cone[1] = 21; cone[2] = 26; cone[3] = 25;
1151:       cone[4] = 16; cone[5] = 23; cone[6] = 24; cone[7] = 17;
1152:       DMPlexSetCone(*dm, 11, cone);
1153:       cone[0] = 20; cone[1] = 28; cone[2] = 26; cone[3] = 21;
1154:       cone[4] = 18; cone[5] = 17; cone[6] = 24; cone[7] = 27;
1155:       DMPlexSetCone(*dm, 12, cone);
1156:       cone[0] = 30; cone[1] = 28; cone[2] = 20; cone[3] = 19;
1157:       cone[4] = 29; cone[5] = 15; cone[6] = 18; cone[7] = 27;
1158:       DMPlexSetCone(*dm, 13, cone);
1159:       cone[0] = 30; cone[1] = 19; cone[2] = 22; cone[3] = 25;
1160:       cone[4] = 29; cone[5] = 23; cone[6] = 16; cone[7] = 15;
1161:       DMPlexSetCone(*dm, 14, cone);
1162:     } else {
1163:       cone[0] =  5; cone[1] =  8; cone[2] =  7; cone[3] =  6;
1164:       cone[4] =  9; cone[5] = 12; cone[6] = 11; cone[7] = 10;
1165:       DMPlexSetCone(*dm, 0, cone);
1166:       cone[0] =  6; cone[1] =  7; cone[2] = 14; cone[3] = 13;
1167:       cone[4] = 12; cone[5] = 15; cone[6] = 16; cone[7] = 11;
1168:       DMPlexSetCone(*dm, 1, cone);
1169:       cone[0] =  8; cone[1] = 17; cone[2] = 14; cone[3] =  7;
1170:       cone[4] = 10; cone[5] = 11; cone[6] = 16; cone[7] = 18;
1171:       DMPlexSetCone(*dm, 2, cone);
1172:       cone[0] = 19; cone[1] = 17; cone[2] =  8; cone[3] =  5;
1173:       cone[4] = 20; cone[5] =  9; cone[6] = 10; cone[7] = 18;
1174:       DMPlexSetCone(*dm, 3, cone);
1175:       cone[0] = 19; cone[1] =  5; cone[2] =  6; cone[3] = 13;
1176:       cone[4] = 20; cone[5] = 15; cone[6] = 12; cone[7] =  9;
1177:       DMPlexSetCone(*dm, 4, cone);
1178:     }
1179:     DMPlexSymmetrize(*dm);
1180:     DMPlexStratify(*dm);
1181:   }
1182:   /* Interpolate */
1183:   {
1184:     DM idm = NULL;

1186:     DMPlexInterpolate(*dm, &idm);
1187:     DMDestroy(dm);
1188:     *dm  = idm;
1189:   }
1190:   /* Create cube geometry */
1191:   {
1192:     Vec             coordinates;
1193:     PetscSection    coordSection;
1194:     PetscScalar    *coords;
1195:     PetscInt        coordSize, v;
1196:     const PetscReal dis = 1.0/PetscSqrtReal(2.0);
1197:     const PetscReal ds2 = dis/2.0;

1199:     /* Build coordinates */
1200:     DMGetCoordinateSection(*dm, &coordSection);
1201:     PetscSectionSetNumFields(coordSection, 1);
1202:     PetscSectionSetFieldComponents(coordSection, 0, dim);
1203:     PetscSectionSetChart(coordSection, numCells, numCells+numVertices);
1204:     for (v = numCells; v < numCells+numVertices; ++v) {
1205:       PetscSectionSetDof(coordSection, v, dim);
1206:       PetscSectionSetFieldDof(coordSection, v, 0, dim);
1207:     }
1208:     PetscSectionSetUp(coordSection);
1209:     PetscSectionGetStorageSize(coordSection, &coordSize);
1210:     VecCreate(PETSC_COMM_SELF, &coordinates);
1211:     PetscObjectSetName((PetscObject) coordinates, "coordinates");
1212:     VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
1213:     VecSetBlockSize(coordinates, dim);
1214:     VecSetType(coordinates,VECSTANDARD);
1215:     VecGetArray(coordinates, &coords);
1216:     coords[0*dim+0] = -ds2; coords[0*dim+1] = -ds2; coords[0*dim+2] = 0.0;
1217:     coords[1*dim+0] =  ds2; coords[1*dim+1] = -ds2; coords[1*dim+2] = 0.0;
1218:     coords[2*dim+0] =  ds2; coords[2*dim+1] =  ds2; coords[2*dim+2] = 0.0;
1219:     coords[3*dim+0] = -ds2; coords[3*dim+1] =  ds2; coords[3*dim+2] = 0.0;
1220:     coords[4*dim+0] = -ds2; coords[4*dim+1] = -ds2; coords[4*dim+2] = 1.0;
1221:     coords[5*dim+0] = -ds2; coords[5*dim+1] =  ds2; coords[5*dim+2] = 1.0;
1222:     coords[6*dim+0] =  ds2; coords[6*dim+1] =  ds2; coords[6*dim+2] = 1.0;
1223:     coords[7*dim+0] =  ds2; coords[7*dim+1] = -ds2; coords[7*dim+2] = 1.0;
1224:     coords[ 8*dim+0] =  dis; coords[ 8*dim+1] = -dis; coords[ 8*dim+2] = 0.0;
1225:     coords[ 9*dim+0] =  dis; coords[ 9*dim+1] =  dis; coords[ 9*dim+2] = 0.0;
1226:     coords[10*dim+0] =  dis; coords[10*dim+1] = -dis; coords[10*dim+2] = 1.0;
1227:     coords[11*dim+0] =  dis; coords[11*dim+1] =  dis; coords[11*dim+2] = 1.0;
1228:     coords[12*dim+0] = -dis; coords[12*dim+1] =  dis; coords[12*dim+2] = 0.0;
1229:     coords[13*dim+0] = -dis; coords[13*dim+1] =  dis; coords[13*dim+2] = 1.0;
1230:     coords[14*dim+0] = -dis; coords[14*dim+1] = -dis; coords[14*dim+2] = 0.0;
1231:     coords[15*dim+0] = -dis; coords[15*dim+1] = -dis; coords[15*dim+2] = 1.0;
1232:     if (periodicZ == DM_BOUNDARY_PERIODIC) {
1233:       /* 15 31 19 */ coords[16*dim+0] = -ds2; coords[16*dim+1] = -ds2; coords[16*dim+2] = 0.5;
1234:       /* 16 32 22 */ coords[17*dim+0] =  ds2; coords[17*dim+1] = -ds2; coords[17*dim+2] = 0.5;
1235:       /* 17 33 21 */ coords[18*dim+0] =  ds2; coords[18*dim+1] =  ds2; coords[18*dim+2] = 0.5;
1236:       /* 18 34 20 */ coords[19*dim+0] = -ds2; coords[19*dim+1] =  ds2; coords[19*dim+2] = 0.5;
1237:       /* 29 35 30 */ coords[20*dim+0] = -dis; coords[20*dim+1] = -dis; coords[20*dim+2] = 0.5;
1238:       /* 23 36 25 */ coords[21*dim+0] =  dis; coords[21*dim+1] = -dis; coords[21*dim+2] = 0.5;
1239:       /* 24 37 26 */ coords[22*dim+0] =  dis; coords[22*dim+1] =  dis; coords[22*dim+2] = 0.5;
1240:       /* 27 38 28 */ coords[23*dim+0] = -dis; coords[23*dim+1] =  dis; coords[23*dim+2] = 0.5;
1241:     }
1242:     VecRestoreArray(coordinates, &coords);
1243:     DMSetCoordinatesLocal(*dm, coordinates);
1244:     VecDestroy(&coordinates);
1245:   }
1246:   /* Create periodicity */
1247:   if (periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
1248:     PetscReal      L[3];
1249:     PetscReal      maxCell[3];
1250:     DMBoundaryType bdType[3];
1251:     PetscReal      lower[3] = {0.0, 0.0, 0.0};
1252:     PetscReal      upper[3] = {1.0, 1.0, 1.5};
1253:     PetscInt       i, numZCells = 3;

1255:     bdType[0] = DM_BOUNDARY_NONE;
1256:     bdType[1] = DM_BOUNDARY_NONE;
1257:     bdType[2] = periodicZ;
1258:     for (i = 0; i < dim; i++) {
1259:       L[i]       = upper[i] - lower[i];
1260:       maxCell[i] = 1.1 * (L[i] / numZCells);
1261:     }
1262:     DMSetPeriodicity(*dm, PETSC_TRUE, maxCell, L, bdType);
1263:   }
1264:   /* Refine topology */
1265:   for (r = 0; r < numRefine; ++r) {
1266:     DM rdm = NULL;

1268:     DMRefine(*dm, comm, &rdm);
1269:     DMDestroy(dm);
1270:     *dm  = rdm;
1271:   }
1272:   /* Remap geometry to cylinder
1273:        Interior square: Linear interpolation is correct
1274:        The other cells all have vertices on rays from the origin. We want to uniformly expand the spacing
1275:        such that the last vertex is on the unit circle. So the closest and farthest vertices are at distance

1277:          phi     = arctan(y/x)
1278:          d_close = sqrt(1/8 + 1/4 sin^2(phi))
1279:          d_far   = sqrt(1/2 + sin^2(phi))

1281:        so we remap them using

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

1286:        If pi/4 < phi < 3pi/4 or -3pi/4 < phi < -pi/4, then we switch x and y.
1287:   */
1288:   {
1289:     Vec           coordinates;
1290:     PetscSection  coordSection;
1291:     PetscScalar  *coords;
1292:     PetscInt      vStart, vEnd, v;
1293:     const PetscReal dis = 1.0/PetscSqrtReal(2.0);
1294:     const PetscReal ds2 = 0.5*dis;

1296:     DMPlexGetDepthStratum(*dm, 0, &vStart, &vEnd);
1297:     DMGetCoordinateSection(*dm, &coordSection);
1298:     DMGetCoordinatesLocal(*dm, &coordinates);
1299:     VecGetArray(coordinates, &coords);
1300:     for (v = vStart; v < vEnd; ++v) {
1301:       PetscReal phi, sinp, cosp, dc, df, x, y, xc, yc;
1302:       PetscInt  off;

1304:       PetscSectionGetOffset(coordSection, v, &off);
1305:       if ((PetscAbsScalar(coords[off+0]) <= ds2) && (PetscAbsScalar(coords[off+1]) <= ds2)) continue;
1306:       x    = PetscRealPart(coords[off]);
1307:       y    = PetscRealPart(coords[off+1]);
1308:       phi  = PetscAtan2Real(y, x);
1309:       sinp = PetscSinReal(phi);
1310:       cosp = PetscCosReal(phi);
1311:       if ((PetscAbsReal(phi) > PETSC_PI/4.0) && (PetscAbsReal(phi) < 3.0*PETSC_PI/4.0)) {
1312:         dc = PetscAbsReal(ds2/sinp);
1313:         df = PetscAbsReal(dis/sinp);
1314:         xc = ds2*x/PetscAbsReal(y);
1315:         yc = ds2*PetscSignReal(y);
1316:       } else {
1317:         dc = PetscAbsReal(ds2/cosp);
1318:         df = PetscAbsReal(dis/cosp);
1319:         xc = ds2*PetscSignReal(x);
1320:         yc = ds2*y/PetscAbsReal(x);
1321:       }
1322:       coords[off+0] = xc + (coords[off+0] - xc)*(1.0 - dc)/(df - dc);
1323:       coords[off+1] = yc + (coords[off+1] - yc)*(1.0 - dc)/(df - dc);
1324:     }
1325:     VecRestoreArray(coordinates, &coords);
1326:     if (periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
1327:       DMLocalizeCoordinates(*dm);
1328:     }
1329:   }
1330:   return(0);
1331: }

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

1336:   Collective on MPI_Comm

1338:   Input Parameters:
1339: + comm - The communicator for the DM object
1340: . n    - The number of wedges around the origin
1341: - interpolate - Create edges and faces

1343:   Output Parameter:
1344: . dm  - The DM object

1346:   Level: beginner

1348: .keywords: DM, create
1349: .seealso: DMPlexCreateHexCylinderMesh(), DMPlexCreateHexBoxMesh(), DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
1350: @*/
1351: PetscErrorCode DMPlexCreateWedgeCylinderMesh(MPI_Comm comm, PetscInt n, PetscBool interpolate, DM *dm)
1352: {
1353:   const PetscInt dim = 3;
1354:   PetscInt       numCells, numVertices;

1359:   if (n < 0) SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Number of wedges %D cannot be negative", n);
1360:   DMCreate(comm, dm);
1361:   DMSetType(*dm, DMPLEX);
1362:   DMSetDimension(*dm, dim);
1363:   /* Create topology */
1364:   {
1365:     PetscInt cone[6], c;

1367:     numCells    = n;
1368:     numVertices = 2*(n+1);
1369:     DMPlexSetChart(*dm, 0, numCells+numVertices);
1370:     for (c = 0; c < numCells; c++) {DMPlexSetConeSize(*dm, c, 6);}
1371:     DMSetUp(*dm);
1372:     for (c = 0; c < numCells; c++) {
1373:       cone[0] =  c+n*1; cone[1] = (c+1)%n+n*1; cone[2] = 0+3*n;
1374:       cone[3] =  c+n*2; cone[4] = (c+1)%n+n*2; cone[5] = 1+3*n;
1375:       DMPlexSetCone(*dm, c, cone);
1376:     }
1377:     DMPlexSymmetrize(*dm);
1378:     DMPlexStratify(*dm);
1379:   }
1380:   /* Interpolate */
1381:   if (interpolate) {
1382:     DM idm = NULL;

1384:     DMPlexInterpolate(*dm, &idm);
1385:     DMDestroy(dm);
1386:     *dm  = idm;
1387:   }
1388:   /* Create cylinder geometry */
1389:   {
1390:     Vec          coordinates;
1391:     PetscSection coordSection;
1392:     PetscScalar *coords;
1393:     PetscInt     coordSize, v, c;

1395:     /* Build coordinates */
1396:     DMGetCoordinateSection(*dm, &coordSection);
1397:     PetscSectionSetNumFields(coordSection, 1);
1398:     PetscSectionSetFieldComponents(coordSection, 0, dim);
1399:     PetscSectionSetChart(coordSection, numCells, numCells+numVertices);
1400:     for (v = numCells; v < numCells+numVertices; ++v) {
1401:       PetscSectionSetDof(coordSection, v, dim);
1402:       PetscSectionSetFieldDof(coordSection, v, 0, dim);
1403:     }
1404:     PetscSectionSetUp(coordSection);
1405:     PetscSectionGetStorageSize(coordSection, &coordSize);
1406:     VecCreate(PETSC_COMM_SELF, &coordinates);
1407:     PetscObjectSetName((PetscObject) coordinates, "coordinates");
1408:     VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
1409:     VecSetBlockSize(coordinates, dim);
1410:     VecSetType(coordinates,VECSTANDARD);
1411:     VecGetArray(coordinates, &coords);
1412:     for (c = 0; c < numCells; c++) {
1413:       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;
1414:       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;
1415:     }
1416:     coords[(2*n+0)*dim+0] = 0.0; coords[(2*n+0)*dim+1] = 0.0; coords[(2*n+0)*dim+2] = 1.0;
1417:     coords[(2*n+1)*dim+0] = 0.0; coords[(2*n+1)*dim+1] = 0.0; coords[(2*n+1)*dim+2] = 0.0;
1418:     VecRestoreArray(coordinates, &coords);
1419:     DMSetCoordinatesLocal(*dm, coordinates);
1420:     VecDestroy(&coordinates);
1421:   }
1422:   return(0);
1423: }

1425: PETSC_STATIC_INLINE PetscReal DiffNormReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
1426: {
1427:   PetscReal prod = 0.0;
1428:   PetscInt  i;
1429:   for (i = 0; i < dim; ++i) prod += PetscSqr(x[i] - y[i]);
1430:   return PetscSqrtReal(prod);
1431: }
1432: PETSC_STATIC_INLINE PetscReal DotReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
1433: {
1434:   PetscReal prod = 0.0;
1435:   PetscInt  i;
1436:   for (i = 0; i < dim; ++i) prod += x[i]*y[i];
1437:   return prod;
1438: }

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

1445:   Collective on MPI_Comm

1447:   Input Parameters:
1448: . comm  - The communicator for the DM object
1449: . dim   - The dimension
1450: - simplex - Use simplices, or tensor product cells

1452:   Output Parameter:
1453: . dm  - The DM object

1455:   Level: beginner

1457: .keywords: DM, create
1458: .seealso: DMPlexCreateBoxMesh(), DMPlexCreateHexBoxMesh(), DMSetType(), DMCreate()
1459: @*/
1460: PetscErrorCode DMPlexCreateSphereMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, DM *dm)
1461: {
1462:   const PetscInt  embedDim = dim+1;
1463:   PetscSection    coordSection;
1464:   Vec             coordinates;
1465:   PetscScalar    *coords;
1466:   PetscReal      *coordsIn;
1467:   PetscInt        numCells, numEdges, numVerts, firstVertex, v, firstEdge, coordSize, d, c, e;
1468:   PetscMPIInt     rank;
1469:   PetscErrorCode  ierr;

1473:   DMCreate(comm, dm);
1474:   DMSetType(*dm, DMPLEX);
1475:   DMSetDimension(*dm, dim);
1476:   DMSetCoordinateDim(*dm, dim+1);
1477:   MPI_Comm_rank(PetscObjectComm((PetscObject) *dm), &rank);
1478:   switch (dim) {
1479:   case 2:
1480:     if (simplex) {
1481:       DM              idm         = NULL;
1482:       const PetscReal edgeLen     = 2.0/(1.0 + PETSC_PHI);
1483:       const PetscReal vertex[3]   = {0.0, 1.0/(1.0 + PETSC_PHI), PETSC_PHI/(1.0 + PETSC_PHI)};
1484:       const PetscInt  degree      = 5;
1485:       PetscInt        s[3]        = {1, 1, 1};
1486:       PetscInt        cone[3];
1487:       PetscInt       *graph, p, i, j, k;

1489:       numCells    = !rank ? 20 : 0;
1490:       numVerts    = !rank ? 12 : 0;
1491:       firstVertex = numCells;
1492:       /* Use icosahedron, which for a unit sphere has coordinates which are all cyclic permutations of

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

1496:          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
1497:          length is then given by 2/\phi = 2 * 2.73606 = 5.47214.
1498:        */
1499:       /* Construct vertices */
1500:       PetscCalloc1(numVerts * embedDim, &coordsIn);
1501:       for (p = 0, i = 0; p < embedDim; ++p) {
1502:         for (s[1] = -1; s[1] < 2; s[1] += 2) {
1503:           for (s[2] = -1; s[2] < 2; s[2] += 2) {
1504:             for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[(d+p)%embedDim]*vertex[(d+p)%embedDim];
1505:             ++i;
1506:           }
1507:         }
1508:       }
1509:       /* Construct graph */
1510:       PetscCalloc1(numVerts * numVerts, &graph);
1511:       for (i = 0; i < numVerts; ++i) {
1512:         for (j = 0, k = 0; j < numVerts; ++j) {
1513:           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i*embedDim], &coordsIn[j*embedDim]) - edgeLen) < PETSC_SMALL) {graph[i*numVerts+j] = 1; ++k;}
1514:         }
1515:         if (k != degree) SETERRQ3(comm, PETSC_ERR_PLIB, "Invalid icosahedron, vertex %D degree %D != %D", i, k, degree);
1516:       }
1517:       /* Build Topology */
1518:       DMPlexSetChart(*dm, 0, numCells+numVerts);
1519:       for (c = 0; c < numCells; c++) {
1520:         DMPlexSetConeSize(*dm, c, embedDim);
1521:       }
1522:       DMSetUp(*dm); /* Allocate space for cones */
1523:       /* Cells */
1524:       for (i = 0, c = 0; i < numVerts; ++i) {
1525:         for (j = 0; j < i; ++j) {
1526:           for (k = 0; k < j; ++k) {
1527:             if (graph[i*numVerts+j] && graph[j*numVerts+k] && graph[k*numVerts+i]) {
1528:               cone[0] = firstVertex+i; cone[1] = firstVertex+j; cone[2] = firstVertex+k;
1529:               /* Check orientation */
1530:               {
1531:                 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}}};
1532:                 PetscReal normal[3];
1533:                 PetscInt  e, f;

1535:                 for (d = 0; d < embedDim; ++d) {
1536:                   normal[d] = 0.0;
1537:                   for (e = 0; e < embedDim; ++e) {
1538:                     for (f = 0; f < embedDim; ++f) {
1539:                       normal[d] += epsilon[d][e][f]*(coordsIn[j*embedDim+e] - coordsIn[i*embedDim+e])*(coordsIn[k*embedDim+f] - coordsIn[i*embedDim+f]);
1540:                     }
1541:                   }
1542:                 }
1543:                 if (DotReal(embedDim, normal, &coordsIn[i*embedDim]) < 0) {PetscInt tmp = cone[1]; cone[1] = cone[2]; cone[2] = tmp;}
1544:               }
1545:               DMPlexSetCone(*dm, c++, cone);
1546:             }
1547:           }
1548:         }
1549:       }
1550:       DMPlexSymmetrize(*dm);
1551:       DMPlexStratify(*dm);
1552:       PetscFree(graph);
1553:       /* Interpolate mesh */
1554:       DMPlexInterpolate(*dm, &idm);
1555:       DMDestroy(dm);
1556:       *dm  = idm;
1557:     } else {
1558:       /*
1559:         12-21--13
1560:          |     |
1561:         25  4  24
1562:          |     |
1563:   12-25--9-16--8-24--13
1564:    |     |     |     |
1565:   23  5 17  0 15  3  22
1566:    |     |     |     |
1567:   10-20--6-14--7-19--11
1568:          |     |
1569:         20  1  19
1570:          |     |
1571:         10-18--11
1572:          |     |
1573:         23  2  22
1574:          |     |
1575:         12-21--13
1576:        */
1577:       const PetscReal dist = 1.0/PetscSqrtReal(3.0);
1578:       PetscInt        cone[4], ornt[4];

1580:       numCells    = !rank ?  6 : 0;
1581:       numEdges    = !rank ? 12 : 0;
1582:       numVerts    = !rank ?  8 : 0;
1583:       firstVertex = numCells;
1584:       firstEdge   = numCells + numVerts;
1585:       /* Build Topology */
1586:       DMPlexSetChart(*dm, 0, numCells+numEdges+numVerts);
1587:       for (c = 0; c < numCells; c++) {
1588:         DMPlexSetConeSize(*dm, c, 4);
1589:       }
1590:       for (e = firstEdge; e < firstEdge+numEdges; ++e) {
1591:         DMPlexSetConeSize(*dm, e, 2);
1592:       }
1593:       DMSetUp(*dm); /* Allocate space for cones */
1594:       /* Cell 0 */
1595:       cone[0] = 14; cone[1] = 15; cone[2] = 16; cone[3] = 17;
1596:       DMPlexSetCone(*dm, 0, cone);
1597:       ornt[0] = 0; ornt[1] = 0; ornt[2] = 0; ornt[3] = 0;
1598:       DMPlexSetConeOrientation(*dm, 0, ornt);
1599:       /* Cell 1 */
1600:       cone[0] = 18; cone[1] = 19; cone[2] = 14; cone[3] = 20;
1601:       DMPlexSetCone(*dm, 1, cone);
1602:       ornt[0] = 0; ornt[1] = 0; ornt[2] = -2; ornt[3] = 0;
1603:       DMPlexSetConeOrientation(*dm, 1, ornt);
1604:       /* Cell 2 */
1605:       cone[0] = 21; cone[1] = 22; cone[2] = 18; cone[3] = 23;
1606:       DMPlexSetCone(*dm, 2, cone);
1607:       ornt[0] = 0; ornt[1] = 0; ornt[2] = -2; ornt[3] = 0;
1608:       DMPlexSetConeOrientation(*dm, 2, ornt);
1609:       /* Cell 3 */
1610:       cone[0] = 19; cone[1] = 22; cone[2] = 24; cone[3] = 15;
1611:       DMPlexSetCone(*dm, 3, cone);
1612:       ornt[0] = -2; ornt[1] = -2; ornt[2] = 0; ornt[3] = -2;
1613:       DMPlexSetConeOrientation(*dm, 3, ornt);
1614:       /* Cell 4 */
1615:       cone[0] = 16; cone[1] = 24; cone[2] = 21; cone[3] = 25;
1616:       DMPlexSetCone(*dm, 4, cone);
1617:       ornt[0] = -2; ornt[1] = -2; ornt[2] = -2; ornt[3] = 0;
1618:       DMPlexSetConeOrientation(*dm, 4, ornt);
1619:       /* Cell 5 */
1620:       cone[0] = 20; cone[1] = 17; cone[2] = 25; cone[3] = 23;
1621:       DMPlexSetCone(*dm, 5, cone);
1622:       ornt[0] = -2; ornt[1] = -2; ornt[2] = -2; ornt[3] = -2;
1623:       DMPlexSetConeOrientation(*dm, 5, ornt);
1624:       /* Edges */
1625:       cone[0] =  6; cone[1] =  7;
1626:       DMPlexSetCone(*dm, 14, cone);
1627:       cone[0] =  7; cone[1] =  8;
1628:       DMPlexSetCone(*dm, 15, cone);
1629:       cone[0] =  8; cone[1] =  9;
1630:       DMPlexSetCone(*dm, 16, cone);
1631:       cone[0] =  9; cone[1] =  6;
1632:       DMPlexSetCone(*dm, 17, cone);
1633:       cone[0] = 10; cone[1] = 11;
1634:       DMPlexSetCone(*dm, 18, cone);
1635:       cone[0] = 11; cone[1] =  7;
1636:       DMPlexSetCone(*dm, 19, cone);
1637:       cone[0] =  6; cone[1] = 10;
1638:       DMPlexSetCone(*dm, 20, cone);
1639:       cone[0] = 12; cone[1] = 13;
1640:       DMPlexSetCone(*dm, 21, cone);
1641:       cone[0] = 13; cone[1] = 11;
1642:       DMPlexSetCone(*dm, 22, cone);
1643:       cone[0] = 10; cone[1] = 12;
1644:       DMPlexSetCone(*dm, 23, cone);
1645:       cone[0] = 13; cone[1] =  8;
1646:       DMPlexSetCone(*dm, 24, cone);
1647:       cone[0] = 12; cone[1] =  9;
1648:       DMPlexSetCone(*dm, 25, cone);
1649:       DMPlexSymmetrize(*dm);
1650:       DMPlexStratify(*dm);
1651:       /* Build coordinates */
1652:       PetscCalloc1(numVerts * embedDim, &coordsIn);
1653:       coordsIn[0*embedDim+0] = -dist; coordsIn[0*embedDim+1] =  dist; coordsIn[0*embedDim+2] = -dist;
1654:       coordsIn[1*embedDim+0] =  dist; coordsIn[1*embedDim+1] =  dist; coordsIn[1*embedDim+2] = -dist;
1655:       coordsIn[2*embedDim+0] =  dist; coordsIn[2*embedDim+1] = -dist; coordsIn[2*embedDim+2] = -dist;
1656:       coordsIn[3*embedDim+0] = -dist; coordsIn[3*embedDim+1] = -dist; coordsIn[3*embedDim+2] = -dist;
1657:       coordsIn[4*embedDim+0] = -dist; coordsIn[4*embedDim+1] =  dist; coordsIn[4*embedDim+2] =  dist;
1658:       coordsIn[5*embedDim+0] =  dist; coordsIn[5*embedDim+1] =  dist; coordsIn[5*embedDim+2] =  dist;
1659:       coordsIn[6*embedDim+0] = -dist; coordsIn[6*embedDim+1] = -dist; coordsIn[6*embedDim+2] =  dist;
1660:       coordsIn[7*embedDim+0] =  dist; coordsIn[7*embedDim+1] = -dist; coordsIn[7*embedDim+2] =  dist;
1661:     }
1662:     break;
1663:   case 3:
1664:     if (simplex) {
1665:       DM              idm             = NULL;
1666:       const PetscReal edgeLen         = 1.0/PETSC_PHI;
1667:       const PetscReal vertexA[4]      = {0.5, 0.5, 0.5, 0.5};
1668:       const PetscReal vertexB[4]      = {1.0, 0.0, 0.0, 0.0};
1669:       const PetscReal vertexC[4]      = {0.5, 0.5*PETSC_PHI, 0.5/PETSC_PHI, 0.0};
1670:       const PetscInt  degree          = 12;
1671:       PetscInt        s[4]            = {1, 1, 1};
1672:       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},
1673:                                          {2, 0, 1, 3}, {2, 1, 3, 0}, {2, 3, 0, 1}, {3, 0, 2, 1}, {3, 1, 0, 2}, {3, 2, 1, 0}};
1674:       PetscInt        cone[4];
1675:       PetscInt       *graph, p, i, j, k, l;

1677:       numCells    = !rank ? 600 : 0;
1678:       numVerts    = !rank ? 120 : 0;
1679:       firstVertex = numCells;
1680:       /* Use the 600-cell, which for a unit sphere has coordinates which are

1682:            1/2 (\pm 1, \pm 1,    \pm 1, \pm 1)                          16
1683:                (\pm 1,    0,       0,      0)  all cyclic permutations   8
1684:            1/2 (\pm 1, \pm phi, \pm 1/phi, 0)  all even permutations    96

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

1689:          http://buzzard.pugetsound.edu/sage-practice/ch03s03.html
1690:          http://mathworld.wolfram.com/600-Cell.html
1691:        */
1692:       /* Construct vertices */
1693:       PetscCalloc1(numVerts * embedDim, &coordsIn);
1694:       i    = 0;
1695:       for (s[0] = -1; s[0] < 2; s[0] += 2) {
1696:         for (s[1] = -1; s[1] < 2; s[1] += 2) {
1697:           for (s[2] = -1; s[2] < 2; s[2] += 2) {
1698:             for (s[3] = -1; s[3] < 2; s[3] += 2) {
1699:               for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[d]*vertexA[d];
1700:               ++i;
1701:             }
1702:           }
1703:         }
1704:       }
1705:       for (p = 0; p < embedDim; ++p) {
1706:         s[1] = s[2] = s[3] = 1;
1707:         for (s[0] = -1; s[0] < 2; s[0] += 2) {
1708:           for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[(d+p)%embedDim]*vertexB[(d+p)%embedDim];
1709:           ++i;
1710:         }
1711:       }
1712:       for (p = 0; p < 12; ++p) {
1713:         s[3] = 1;
1714:         for (s[0] = -1; s[0] < 2; s[0] += 2) {
1715:           for (s[1] = -1; s[1] < 2; s[1] += 2) {
1716:             for (s[2] = -1; s[2] < 2; s[2] += 2) {
1717:               for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[evenPerm[p][d]]*vertexC[evenPerm[p][d]];
1718:               ++i;
1719:             }
1720:           }
1721:         }
1722:       }
1723:       if (i != numVerts) SETERRQ2(comm, PETSC_ERR_PLIB, "Invalid 600-cell, vertices %D != %D", i, numVerts);
1724:       /* Construct graph */
1725:       PetscCalloc1(numVerts * numVerts, &graph);
1726:       for (i = 0; i < numVerts; ++i) {
1727:         for (j = 0, k = 0; j < numVerts; ++j) {
1728:           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i*embedDim], &coordsIn[j*embedDim]) - edgeLen) < PETSC_SMALL) {graph[i*numVerts+j] = 1; ++k;}
1729:         }
1730:         if (k != degree) SETERRQ3(comm, PETSC_ERR_PLIB, "Invalid 600-cell, vertex %D degree %D != %D", i, k, degree);
1731:       }
1732:       /* Build Topology */
1733:       DMPlexSetChart(*dm, 0, numCells+numVerts);
1734:       for (c = 0; c < numCells; c++) {
1735:         DMPlexSetConeSize(*dm, c, embedDim);
1736:       }
1737:       DMSetUp(*dm); /* Allocate space for cones */
1738:       /* Cells */
1739:       for (i = 0, c = 0; i < numVerts; ++i) {
1740:         for (j = 0; j < i; ++j) {
1741:           for (k = 0; k < j; ++k) {
1742:             for (l = 0; l < k; ++l) {
1743:               if (graph[i*numVerts+j] && graph[j*numVerts+k] && graph[k*numVerts+i] &&
1744:                   graph[l*numVerts+i] && graph[l*numVerts+j] && graph[l*numVerts+k]) {
1745:                 cone[0] = firstVertex+i; cone[1] = firstVertex+j; cone[2] = firstVertex+k; cone[3] = firstVertex+l;
1746:                 /* Check orientation: https://ef.gy/linear-algebra:normal-vectors-in-higher-dimensional-spaces */
1747:                 {
1748:                   const PetscInt epsilon[4][4][4][4] = {{{{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
1749:                                                          {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  1}, { 0,  0, -1, 0}},
1750:                                                          {{0,  0,  0,  0}, { 0, 0,  0, -1}, { 0,  0, 0,  0}, { 0,  1,  0, 0}},
1751:                                                          {{0,  0,  0,  0}, { 0, 0,  1,  0}, { 0, -1, 0,  0}, { 0,  0,  0, 0}}},

1753:                                                         {{{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0, -1}, { 0,  0,  1, 0}},
1754:                                                          {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
1755:                                                          {{0,  0,  0,  1}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, {-1,  0,  0, 0}},
1756:                                                          {{0,  0, -1,  0}, { 0, 0,  0,  0}, { 1,  0, 0,  0}, { 0,  0,  0, 0}}},

1758:                                                         {{{0,  0,  0,  0}, { 0, 0,  0,  1}, { 0,  0, 0,  0}, { 0, -1,  0, 0}},
1759:                                                          {{0,  0,  0, -1}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 1,  0,  0, 0}},
1760:                                                          {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
1761:                                                          {{0,  1,  0,  0}, {-1, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}}},

1763:                                                         {{{0,  0,  0,  0}, { 0, 0, -1,  0}, { 0,  1, 0,  0}, { 0,  0,  0, 0}},
1764:                                                          {{0,  0,  1,  0}, { 0, 0,  0,  0}, {-1,  0, 0,  0}, { 0,  0,  0, 0}},
1765:                                                          {{0, -1,  0,  0}, { 1, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
1766:                                                          {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}}}};
1767:                   PetscReal normal[4];
1768:                   PetscInt  e, f, g;

1770:                   for (d = 0; d < embedDim; ++d) {
1771:                     normal[d] = 0.0;
1772:                     for (e = 0; e < embedDim; ++e) {
1773:                       for (f = 0; f < embedDim; ++f) {
1774:                         for (g = 0; g < embedDim; ++g) {
1775:                           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]);
1776:                         }
1777:                       }
1778:                     }
1779:                   }
1780:                   if (DotReal(embedDim, normal, &coordsIn[i*embedDim]) < 0) {PetscInt tmp = cone[1]; cone[1] = cone[2]; cone[2] = tmp;}
1781:                 }
1782:                 DMPlexSetCone(*dm, c++, cone);
1783:               }
1784:             }
1785:           }
1786:         }
1787:       }
1788:       DMPlexSymmetrize(*dm);
1789:       DMPlexStratify(*dm);
1790:       PetscFree(graph);
1791:       /* Interpolate mesh */
1792:       DMPlexInterpolate(*dm, &idm);
1793:       DMDestroy(dm);
1794:       *dm  = idm;
1795:       break;
1796:     }
1797:   default: SETERRQ1(comm, PETSC_ERR_SUP, "Unsupported dimension for sphere: %D", dim);
1798:   }
1799:   /* Create coordinates */
1800:   DMGetCoordinateSection(*dm, &coordSection);
1801:   PetscSectionSetNumFields(coordSection, 1);
1802:   PetscSectionSetFieldComponents(coordSection, 0, embedDim);
1803:   PetscSectionSetChart(coordSection, firstVertex, firstVertex+numVerts);
1804:   for (v = firstVertex; v < firstVertex+numVerts; ++v) {
1805:     PetscSectionSetDof(coordSection, v, embedDim);
1806:     PetscSectionSetFieldDof(coordSection, v, 0, embedDim);
1807:   }
1808:   PetscSectionSetUp(coordSection);
1809:   PetscSectionGetStorageSize(coordSection, &coordSize);
1810:   VecCreate(PETSC_COMM_SELF, &coordinates);
1811:   VecSetBlockSize(coordinates, embedDim);
1812:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
1813:   VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
1814:   VecSetType(coordinates,VECSTANDARD);
1815:   VecGetArray(coordinates, &coords);
1816:   for (v = 0; v < numVerts; ++v) for (d = 0; d < embedDim; ++d) {coords[v*embedDim+d] = coordsIn[v*embedDim+d];}
1817:   VecRestoreArray(coordinates, &coords);
1818:   DMSetCoordinatesLocal(*dm, coordinates);
1819:   VecDestroy(&coordinates);
1820:   PetscFree(coordsIn);
1821:   return(0);
1822: }

1824: /* External function declarations here */
1825: extern PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling);
1826: extern PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat);
1827: extern PetscErrorCode DMCreateDefaultSection_Plex(DM dm);
1828: extern PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm);
1829: extern PetscErrorCode DMCreateMatrix_Plex(DM dm,  Mat *J);
1830: extern PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm);
1831: PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm);
1832: extern PetscErrorCode DMSetUp_Plex(DM dm);
1833: extern PetscErrorCode DMDestroy_Plex(DM dm);
1834: extern PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer);
1835: extern PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer);
1836: extern PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm);
1837: static PetscErrorCode DMInitialize_Plex(DM dm);

1839: /* Replace dm with the contents of dmNew
1840:    - Share the DM_Plex structure
1841:    - Share the coordinates
1842:    - Share the SF
1843: */
1844: static PetscErrorCode DMPlexReplace_Static(DM dm, DM dmNew)
1845: {
1846:   PetscSF               sf;
1847:   DM                    coordDM, coarseDM;
1848:   Vec                   coords;
1849:   PetscBool             isper;
1850:   const PetscReal      *maxCell, *L;
1851:   const DMBoundaryType *bd;
1852:   PetscErrorCode        ierr;

1855:   DMGetPointSF(dmNew, &sf);
1856:   DMSetPointSF(dm, sf);
1857:   DMGetCoordinateDM(dmNew, &coordDM);
1858:   DMGetCoordinatesLocal(dmNew, &coords);
1859:   DMSetCoordinateDM(dm, coordDM);
1860:   DMSetCoordinatesLocal(dm, coords);
1861:   DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);
1862:   DMSetPeriodicity(dmNew, isper, maxCell, L, bd);
1863:   DMDestroy_Plex(dm);
1864:   DMInitialize_Plex(dm);
1865:   dm->data = dmNew->data;
1866:   ((DM_Plex *) dmNew->data)->refct++;
1867:   dmNew->labels->refct++;
1868:   if (!--(dm->labels->refct)) {
1869:     DMLabelLink next = dm->labels->next;

1871:     /* destroy the labels */
1872:     while (next) {
1873:       DMLabelLink tmp = next->next;

1875:       DMLabelDestroy(&next->label);
1876:       PetscFree(next);
1877:       next = tmp;
1878:     }
1879:     PetscFree(dm->labels);
1880:   }
1881:   dm->labels = dmNew->labels;
1882:   dm->depthLabel = dmNew->depthLabel;
1883:   DMGetCoarseDM(dmNew,&coarseDM);
1884:   DMSetCoarseDM(dm,coarseDM);
1885:   return(0);
1886: }

1888: /* Swap dm with the contents of dmNew
1889:    - Swap the DM_Plex structure
1890:    - Swap the coordinates
1891:    - Swap the point PetscSF
1892: */
1893: static PetscErrorCode DMPlexSwap_Static(DM dmA, DM dmB)
1894: {
1895:   DM              coordDMA, coordDMB;
1896:   Vec             coordsA,  coordsB;
1897:   PetscSF         sfA,      sfB;
1898:   void            *tmp;
1899:   DMLabelLinkList listTmp;
1900:   DMLabel         depthTmp;
1901:   PetscErrorCode  ierr;

1904:   DMGetPointSF(dmA, &sfA);
1905:   DMGetPointSF(dmB, &sfB);
1906:   PetscObjectReference((PetscObject) sfA);
1907:   DMSetPointSF(dmA, sfB);
1908:   DMSetPointSF(dmB, sfA);
1909:   PetscObjectDereference((PetscObject) sfA);

1911:   DMGetCoordinateDM(dmA, &coordDMA);
1912:   DMGetCoordinateDM(dmB, &coordDMB);
1913:   PetscObjectReference((PetscObject) coordDMA);
1914:   DMSetCoordinateDM(dmA, coordDMB);
1915:   DMSetCoordinateDM(dmB, coordDMA);
1916:   PetscObjectDereference((PetscObject) coordDMA);

1918:   DMGetCoordinatesLocal(dmA, &coordsA);
1919:   DMGetCoordinatesLocal(dmB, &coordsB);
1920:   PetscObjectReference((PetscObject) coordsA);
1921:   DMSetCoordinatesLocal(dmA, coordsB);
1922:   DMSetCoordinatesLocal(dmB, coordsA);
1923:   PetscObjectDereference((PetscObject) coordsA);

1925:   tmp       = dmA->data;
1926:   dmA->data = dmB->data;
1927:   dmB->data = tmp;
1928:   listTmp   = dmA->labels;
1929:   dmA->labels = dmB->labels;
1930:   dmB->labels = listTmp;
1931:   depthTmp  = dmA->depthLabel;
1932:   dmA->depthLabel = dmB->depthLabel;
1933:   dmB->depthLabel = depthTmp;
1934:   return(0);
1935: }

1937: PetscErrorCode DMSetFromOptions_NonRefinement_Plex(PetscOptionItems *PetscOptionsObject,DM dm)
1938: {
1939:   DM_Plex       *mesh = (DM_Plex*) dm->data;

1943:   /* Handle viewing */
1944:   PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMView", PETSC_FALSE, &mesh->printSetValues, NULL);
1945:   PetscOptionsInt("-dm_plex_print_fem", "Debug output level all fem computations", "DMView", 0, &mesh->printFEM, NULL);
1946:   PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMView", mesh->printTol, &mesh->printTol, NULL);
1947:   /* Point Location */
1948:   PetscOptionsBool("-dm_plex_hash_location", "Use grid hashing for point location", "DMView", PETSC_FALSE, &mesh->useHashLocation, NULL);
1949:   /* Generation and remeshing */
1950:   PetscOptionsBool("-dm_plex_remesh_bd", "Allow changes to the boundary on remeshing", "DMView", PETSC_FALSE, &mesh->remeshBd, NULL);
1951:   /* Projection behavior */
1952:   PetscOptionsInt("-dm_plex_max_projection_height", "Maxmimum mesh point height used to project locally", "DMPlexSetMaxProjectionHeight", 0, &mesh->maxProjectionHeight, NULL);
1953:   PetscOptionsBool("-dm_plex_regular_refinement", "Use special nested projection algorithm for regular refinement", "DMPlexSetRegularRefinement", mesh->regularRefinement, &mesh->regularRefinement, NULL);

1955:   PetscPartitionerSetFromOptions(mesh->partitioner);
1956:   return(0);
1957: }

1959: static PetscErrorCode DMSetFromOptions_Plex(PetscOptionItems *PetscOptionsObject,DM dm)
1960: {
1961:   PetscInt       refine = 0, coarsen = 0, r;
1962:   PetscBool      isHierarchy;

1967:   PetscOptionsHead(PetscOptionsObject,"DMPlex Options");
1968:   /* Handle DMPlex refinement */
1969:   PetscOptionsInt("-dm_refine", "The number of uniform refinements", "DMCreate", refine, &refine, NULL);
1970:   PetscOptionsInt("-dm_refine_hierarchy", "The number of uniform refinements", "DMCreate", refine, &refine, &isHierarchy);
1971:   if (refine) {DMPlexSetRefinementUniform(dm, PETSC_TRUE);}
1972:   if (refine && isHierarchy) {
1973:     DM *dms, coarseDM;

1975:     DMGetCoarseDM(dm, &coarseDM);
1976:     PetscObjectReference((PetscObject)coarseDM);
1977:     PetscMalloc1(refine,&dms);
1978:     DMRefineHierarchy(dm, refine, dms);
1979:     /* Total hack since we do not pass in a pointer */
1980:     DMPlexSwap_Static(dm, dms[refine-1]);
1981:     if (refine == 1) {
1982:       DMSetCoarseDM(dm, dms[0]);
1983:       DMPlexSetRegularRefinement(dm, PETSC_TRUE);
1984:     } else {
1985:       DMSetCoarseDM(dm, dms[refine-2]);
1986:       DMPlexSetRegularRefinement(dm, PETSC_TRUE);
1987:       DMSetCoarseDM(dms[0], dms[refine-1]);
1988:       DMPlexSetRegularRefinement(dms[0], PETSC_TRUE);
1989:     }
1990:     DMSetCoarseDM(dms[refine-1], coarseDM);
1991:     PetscObjectDereference((PetscObject)coarseDM);
1992:     /* Free DMs */
1993:     for (r = 0; r < refine; ++r) {
1994:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]);
1995:       DMDestroy(&dms[r]);
1996:     }
1997:     PetscFree(dms);
1998:   } else {
1999:     for (r = 0; r < refine; ++r) {
2000:       DM refinedMesh;

2002:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
2003:       DMRefine(dm, PetscObjectComm((PetscObject) dm), &refinedMesh);
2004:       /* Total hack since we do not pass in a pointer */
2005:       DMPlexReplace_Static(dm, refinedMesh);
2006:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
2007:       DMDestroy(&refinedMesh);
2008:     }
2009:   }
2010:   /* Handle DMPlex coarsening */
2011:   PetscOptionsInt("-dm_coarsen", "Coarsen the mesh", "DMCreate", coarsen, &coarsen, NULL);
2012:   PetscOptionsInt("-dm_coarsen_hierarchy", "The number of coarsenings", "DMCreate", coarsen, &coarsen, &isHierarchy);
2013:   if (coarsen && isHierarchy) {
2014:     DM *dms;

2016:     PetscMalloc1(coarsen, &dms);
2017:     DMCoarsenHierarchy(dm, coarsen, dms);
2018:     /* Free DMs */
2019:     for (r = 0; r < coarsen; ++r) {
2020:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]);
2021:       DMDestroy(&dms[r]);
2022:     }
2023:     PetscFree(dms);
2024:   } else {
2025:     for (r = 0; r < coarsen; ++r) {
2026:       DM coarseMesh;

2028:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
2029:       DMCoarsen(dm, PetscObjectComm((PetscObject) dm), &coarseMesh);
2030:       /* Total hack since we do not pass in a pointer */
2031:       DMPlexReplace_Static(dm, coarseMesh);
2032:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
2033:       DMDestroy(&coarseMesh);
2034:     }
2035:   }
2036:   /* Handle */
2037:   DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
2038:   PetscOptionsTail();
2039:   return(0);
2040: }

2042: static PetscErrorCode DMCreateGlobalVector_Plex(DM dm,Vec *vec)
2043: {

2047:   DMCreateGlobalVector_Section_Private(dm,vec);
2048:   /* VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM); */
2049:   VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex);
2050:   VecSetOperation(*vec, VECOP_VIEWNATIVE, (void (*)(void)) VecView_Plex_Native);
2051:   VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex);
2052:   VecSetOperation(*vec, VECOP_LOADNATIVE, (void (*)(void)) VecLoad_Plex_Native);
2053:   return(0);
2054: }

2056: static PetscErrorCode DMCreateLocalVector_Plex(DM dm,Vec *vec)
2057: {

2061:   DMCreateLocalVector_Section_Private(dm,vec);
2062:   VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex_Local);
2063:   VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex_Local);
2064:   return(0);
2065: }

2067: static PetscErrorCode DMGetDimPoints_Plex(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
2068: {
2069:   PetscInt       depth, d;

2073:   DMPlexGetDepth(dm, &depth);
2074:   if (depth == 1) {
2075:     DMGetDimension(dm, &d);
2076:     if (dim == 0)      {DMPlexGetDepthStratum(dm, dim, pStart, pEnd);}
2077:     else if (dim == d) {DMPlexGetDepthStratum(dm, 1, pStart, pEnd);}
2078:     else               {*pStart = 0; *pEnd = 0;}
2079:   } else {
2080:     DMPlexGetDepthStratum(dm, dim, pStart, pEnd);
2081:   }
2082:   return(0);
2083: }

2085: static PetscErrorCode DMGetNeighors_Plex(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
2086: {
2087:   PetscSF        sf;

2091:   DMGetPointSF(dm, &sf);
2092:   PetscSFGetRanks(sf, nranks, ranks, NULL, NULL, NULL);
2093:   return(0);
2094: }

2096: static PetscErrorCode DMInitialize_Plex(DM dm)
2097: {

2101:   dm->ops->view                            = DMView_Plex;
2102:   dm->ops->load                            = DMLoad_Plex;
2103:   dm->ops->setfromoptions                  = DMSetFromOptions_Plex;
2104:   dm->ops->clone                           = DMClone_Plex;
2105:   dm->ops->setup                           = DMSetUp_Plex;
2106:   dm->ops->createdefaultsection            = DMCreateDefaultSection_Plex;
2107:   dm->ops->createdefaultconstraints        = DMCreateDefaultConstraints_Plex;
2108:   dm->ops->createglobalvector              = DMCreateGlobalVector_Plex;
2109:   dm->ops->createlocalvector               = DMCreateLocalVector_Plex;
2110:   dm->ops->getlocaltoglobalmapping         = NULL;
2111:   dm->ops->createfieldis                   = NULL;
2112:   dm->ops->createcoordinatedm              = DMCreateCoordinateDM_Plex;
2113:   dm->ops->getcoloring                     = NULL;
2114:   dm->ops->creatematrix                    = DMCreateMatrix_Plex;
2115:   dm->ops->createinterpolation             = DMCreateInterpolation_Plex;
2116:   dm->ops->getaggregates                   = NULL;
2117:   dm->ops->getinjection                    = DMCreateInjection_Plex;
2118:   dm->ops->refine                          = DMRefine_Plex;
2119:   dm->ops->coarsen                         = DMCoarsen_Plex;
2120:   dm->ops->refinehierarchy                 = DMRefineHierarchy_Plex;
2121:   dm->ops->coarsenhierarchy                = DMCoarsenHierarchy_Plex;
2122:   dm->ops->adaptlabel                      = DMAdaptLabel_Plex;
2123:   dm->ops->adaptmetric                     = DMAdaptMetric_Plex;
2124:   dm->ops->globaltolocalbegin              = NULL;
2125:   dm->ops->globaltolocalend                = NULL;
2126:   dm->ops->localtoglobalbegin              = NULL;
2127:   dm->ops->localtoglobalend                = NULL;
2128:   dm->ops->destroy                         = DMDestroy_Plex;
2129:   dm->ops->createsubdm                     = DMCreateSubDM_Plex;
2130:   dm->ops->getdimpoints                    = DMGetDimPoints_Plex;
2131:   dm->ops->locatepoints                    = DMLocatePoints_Plex;
2132:   dm->ops->projectfunctionlocal            = DMProjectFunctionLocal_Plex;
2133:   dm->ops->projectfunctionlabellocal       = DMProjectFunctionLabelLocal_Plex;
2134:   dm->ops->projectfieldlocal               = DMProjectFieldLocal_Plex;
2135:   dm->ops->projectfieldlabellocal          = DMProjectFieldLabelLocal_Plex;
2136:   dm->ops->computel2diff                   = DMComputeL2Diff_Plex;
2137:   dm->ops->computel2gradientdiff           = DMComputeL2GradientDiff_Plex;
2138:   dm->ops->computel2fielddiff              = DMComputeL2FieldDiff_Plex;
2139:   dm->ops->getneighbors                    = DMGetNeighors_Plex;
2140:   PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C",DMPlexInsertBoundaryValues_Plex);
2141:   PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",DMSetUpGLVisViewer_Plex);
2142:   return(0);
2143: }

2145: PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm)
2146: {
2147:   DM_Plex        *mesh = (DM_Plex *) dm->data;

2151:   mesh->refct++;
2152:   (*newdm)->data = mesh;
2153:   PetscObjectChangeTypeName((PetscObject) *newdm, DMPLEX);
2154:   DMInitialize_Plex(*newdm);
2155:   return(0);
2156: }

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

2164:   Level: intermediate

2166: .seealso: DMType, DMPlexCreate(), DMCreate(), DMSetType()
2167: M*/

2169: PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm)
2170: {
2171:   DM_Plex        *mesh;
2172:   PetscInt       unit, d;

2177:   PetscNewLog(dm,&mesh);
2178:   dm->dim  = 0;
2179:   dm->data = mesh;

2181:   mesh->refct             = 1;
2182:   PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection);
2183:   mesh->maxConeSize       = 0;
2184:   mesh->cones             = NULL;
2185:   mesh->coneOrientations  = NULL;
2186:   PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection);
2187:   mesh->maxSupportSize    = 0;
2188:   mesh->supports          = NULL;
2189:   mesh->refinementUniform = PETSC_TRUE;
2190:   mesh->refinementLimit   = -1.0;

2192:   mesh->facesTmp = NULL;

2194:   mesh->tetgenOpts   = NULL;
2195:   mesh->triangleOpts = NULL;
2196:   PetscPartitionerCreate(PetscObjectComm((PetscObject)dm), &mesh->partitioner);
2197:   mesh->remeshBd     = PETSC_FALSE;

2199:   mesh->subpointMap = NULL;

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

2203:   mesh->regularRefinement   = PETSC_FALSE;
2204:   mesh->depthState          = -1;
2205:   mesh->globalVertexNumbers = NULL;
2206:   mesh->globalCellNumbers   = NULL;
2207:   mesh->anchorSection       = NULL;
2208:   mesh->anchorIS            = NULL;
2209:   mesh->createanchors       = NULL;
2210:   mesh->computeanchormatrix = NULL;
2211:   mesh->parentSection       = NULL;
2212:   mesh->parents             = NULL;
2213:   mesh->childIDs            = NULL;
2214:   mesh->childSection        = NULL;
2215:   mesh->children            = NULL;
2216:   mesh->referenceTree       = NULL;
2217:   mesh->getchildsymmetry    = NULL;
2218:   for (d = 0; d < 8; ++d) mesh->hybridPointMax[d] = PETSC_DETERMINE;
2219:   mesh->vtkCellHeight       = 0;
2220:   mesh->useCone             = PETSC_FALSE;
2221:   mesh->useClosure          = PETSC_TRUE;
2222:   mesh->useAnchors          = PETSC_FALSE;

2224:   mesh->maxProjectionHeight = 0;

2226:   mesh->printSetValues = PETSC_FALSE;
2227:   mesh->printFEM       = 0;
2228:   mesh->printTol       = 1.0e-10;

2230:   DMInitialize_Plex(dm);
2231:   return(0);
2232: }

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

2237:   Collective on MPI_Comm

2239:   Input Parameter:
2240: . comm - The communicator for the DMPlex object

2242:   Output Parameter:
2243: . mesh  - The DMPlex object

2245:   Level: beginner

2247: .keywords: DMPlex, create
2248: @*/
2249: PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh)
2250: {

2255:   DMCreate(comm, mesh);
2256:   DMSetType(*mesh, DMPLEX);
2257:   return(0);
2258: }

2260: /*
2261:   This takes as input the common mesh generator output, a list of the vertices for each cell, but vertex numbers are global and an SF is built for them
2262: */
2263: static PetscErrorCode DMPlexBuildFromCellList_Parallel_Private(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const int cells[], PetscSF *sfVert)
2264: {
2265:   PetscSF         sfPoint;
2266:   PetscLayout     vLayout;
2267:   PetscHashI      vhash;
2268:   PetscSFNode    *remoteVerticesAdj, *vertexLocal, *vertexOwner, *remoteVertex;
2269:   const PetscInt *vrange;
2270:   PetscInt        numVerticesAdj, off, *verticesAdj, numVerticesGhost = 0, *localVertex, *cone, c, p, v, g;
2271:   PETSC_UNUSED PetscHashIIter ret, iter;
2272:   PetscMPIInt     rank, size;
2273:   PetscErrorCode  ierr;

2276:   MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);
2277:   MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size);
2278:   /* Partition vertices */
2279:   PetscLayoutCreate(PetscObjectComm((PetscObject) dm), &vLayout);
2280:   PetscLayoutSetLocalSize(vLayout, numVertices);
2281:   PetscLayoutSetBlockSize(vLayout, 1);
2282:   PetscLayoutSetUp(vLayout);
2283:   PetscLayoutGetRanges(vLayout, &vrange);
2284:   /* Count vertices and map them to procs */
2285:   PetscHashICreate(vhash);
2286:   for (c = 0; c < numCells; ++c) {
2287:     for (p = 0; p < numCorners; ++p) {
2288:       PetscHashIPut(vhash, cells[c*numCorners+p], ret, iter);
2289:     }
2290:   }
2291:   PetscHashISize(vhash, numVerticesAdj);
2292:   PetscMalloc1(numVerticesAdj, &verticesAdj);
2293:   off = 0; PetscHashIGetKeys(vhash, &off, verticesAdj);
2294:   if (off != numVerticesAdj) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid number of local vertices %D should be %D", off, numVerticesAdj);
2295:   PetscSortInt(numVerticesAdj, verticesAdj);
2296:   PetscMalloc1(numVerticesAdj, &remoteVerticesAdj);
2297:   for (v = 0; v < numVerticesAdj; ++v) {
2298:     const PetscInt gv = verticesAdj[v];
2299:     PetscInt       vrank;

2301:     PetscFindInt(gv, size+1, vrange, &vrank);
2302:     vrank = vrank < 0 ? -(vrank+2) : vrank;
2303:     remoteVerticesAdj[v].index = gv - vrange[vrank];
2304:     remoteVerticesAdj[v].rank  = vrank;
2305:   }
2306:   /* Create cones */
2307:   DMPlexSetChart(dm, 0, numCells+numVerticesAdj);
2308:   for (c = 0; c < numCells; ++c) {DMPlexSetConeSize(dm, c, numCorners);}
2309:   DMSetUp(dm);
2310:   DMGetWorkArray(dm, numCorners, PETSC_INT, &cone);
2311:   for (c = 0; c < numCells; ++c) {
2312:     for (p = 0; p < numCorners; ++p) {
2313:       const PetscInt gv = cells[c*numCorners+p];
2314:       PetscInt       lv;

2316:       PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv);
2317:       if (lv < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %D in local connectivity", gv);
2318:       cone[p] = lv+numCells;
2319:     }
2320:     DMPlexSetCone(dm, c, cone);
2321:   }
2322:   DMRestoreWorkArray(dm, numCorners, PETSC_INT, &cone);
2323:   /* Create SF for vertices */
2324:   PetscSFCreate(PetscObjectComm((PetscObject)dm), sfVert);
2325:   PetscObjectSetName((PetscObject) *sfVert, "Vertex Ownership SF");
2326:   PetscSFSetFromOptions(*sfVert);
2327:   PetscSFSetGraph(*sfVert, numVertices, numVerticesAdj, NULL, PETSC_OWN_POINTER, remoteVerticesAdj, PETSC_OWN_POINTER);
2328:   PetscFree(verticesAdj);
2329:   /* Build pointSF */
2330:   PetscMalloc2(numVerticesAdj, &vertexLocal, numVertices, &vertexOwner);
2331:   for (v = 0; v < numVerticesAdj; ++v) {vertexLocal[v].index = v+numCells; vertexLocal[v].rank = rank;}
2332:   for (v = 0; v < numVertices;    ++v) {vertexOwner[v].index = -1;         vertexOwner[v].rank = -1;}
2333:   PetscSFReduceBegin(*sfVert, MPIU_2INT, vertexLocal, vertexOwner, MPI_MAXLOC);
2334:   PetscSFReduceEnd(*sfVert, MPIU_2INT, vertexLocal, vertexOwner, MPI_MAXLOC);
2335:   for (v = 0; v < numVertices;    ++v) if (vertexOwner[v].rank < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Global vertex %d on rank %d was unclaimed", v + vrange[rank], rank);
2336:   PetscSFBcastBegin(*sfVert, MPIU_2INT, vertexOwner, vertexLocal);
2337:   PetscSFBcastEnd(*sfVert, MPIU_2INT, vertexOwner, vertexLocal);
2338:   for (v = 0; v < numVerticesAdj; ++v) if (vertexLocal[v].rank != rank) ++numVerticesGhost;
2339:   PetscMalloc1(numVerticesGhost, &localVertex);
2340:   PetscMalloc1(numVerticesGhost, &remoteVertex);
2341:   for (v = 0, g = 0; v < numVerticesAdj; ++v) {
2342:     if (vertexLocal[v].rank != rank) {
2343:       localVertex[g]        = v+numCells;
2344:       remoteVertex[g].index = vertexLocal[v].index;
2345:       remoteVertex[g].rank  = vertexLocal[v].rank;
2346:       ++g;
2347:     }
2348:   }
2349:   PetscFree2(vertexLocal, vertexOwner);
2350:   if (g != numVerticesGhost) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid number of vertex ghosts %D should be %D", g, numVerticesGhost);
2351:   DMGetPointSF(dm, &sfPoint);
2352:   PetscObjectSetName((PetscObject) sfPoint, "point SF");
2353:   PetscSFSetGraph(sfPoint, numCells+numVerticesAdj, numVerticesGhost, localVertex, PETSC_OWN_POINTER, remoteVertex, PETSC_OWN_POINTER);
2354:   PetscLayoutDestroy(&vLayout);
2355:   PetscHashIDestroy(vhash);
2356:   /* Fill in the rest of the topology structure */
2357:   DMPlexSymmetrize(dm);
2358:   DMPlexStratify(dm);
2359:   return(0);
2360: }

2362: /*
2363:   This takes as input the coordinates for each owned vertex
2364: */
2365: static PetscErrorCode DMPlexBuildCoordinates_Parallel_Private(DM dm, PetscInt spaceDim, PetscInt numCells, PetscInt numV, PetscSF sfVert, const PetscReal vertexCoords[])
2366: {
2367:   PetscSection   coordSection;
2368:   Vec            coordinates;
2369:   PetscScalar   *coords;
2370:   PetscInt       numVertices, numVerticesAdj, coordSize, v;

2374:   DMSetCoordinateDim(dm, spaceDim);
2375:   PetscSFGetGraph(sfVert, &numVertices, &numVerticesAdj, NULL, NULL);
2376:   DMGetCoordinateSection(dm, &coordSection);
2377:   PetscSectionSetNumFields(coordSection, 1);
2378:   PetscSectionSetFieldComponents(coordSection, 0, spaceDim);
2379:   PetscSectionSetChart(coordSection, numCells, numCells + numVerticesAdj);
2380:   for (v = numCells; v < numCells+numVerticesAdj; ++v) {
2381:     PetscSectionSetDof(coordSection, v, spaceDim);
2382:     PetscSectionSetFieldDof(coordSection, v, 0, spaceDim);
2383:   }
2384:   PetscSectionSetUp(coordSection);
2385:   PetscSectionGetStorageSize(coordSection, &coordSize);
2386:   VecCreate(PetscObjectComm((PetscObject)dm), &coordinates);
2387:   VecSetBlockSize(coordinates, spaceDim);
2388:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
2389:   VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
2390:   VecSetType(coordinates,VECSTANDARD);
2391:   VecGetArray(coordinates, &coords);
2392:   {
2393:     MPI_Datatype coordtype;

2395:     /* Need a temp buffer for coords if we have complex/single */
2396:     MPI_Type_contiguous(spaceDim, MPIU_SCALAR, &coordtype);
2397:     MPI_Type_commit(&coordtype);
2398: #if defined(PETSC_USE_COMPLEX)
2399:     {
2400:     PetscScalar *svertexCoords;
2401:     PetscInt    i;
2402:     PetscMalloc1(numV*spaceDim,&svertexCoords);
2403:     for (i=0; i<numV*spaceDim; i++) svertexCoords[i] = vertexCoords[i];
2404:     PetscSFBcastBegin(sfVert, coordtype, svertexCoords, coords);
2405:     PetscSFBcastEnd(sfVert, coordtype, svertexCoords, coords);
2406:     PetscFree(svertexCoords);
2407:     }
2408: #else
2409:     PetscSFBcastBegin(sfVert, coordtype, vertexCoords, coords);
2410:     PetscSFBcastEnd(sfVert, coordtype, vertexCoords, coords);
2411: #endif
2412:     MPI_Type_free(&coordtype);
2413:   }
2414:   VecRestoreArray(coordinates, &coords);
2415:   DMSetCoordinatesLocal(dm, coordinates);
2416:   VecDestroy(&coordinates);
2417:   return(0);
2418: }

2420: /*@C
2421:   DMPlexCreateFromCellListParallel - This takes as input common mesh generator output, a list of the vertices for each cell, and produces a DM

2423:   Input Parameters:
2424: + comm - The communicator
2425: . dim - The topological dimension of the mesh
2426: . numCells - The number of cells owned by this process
2427: . numVertices - The number of vertices owned by this process
2428: . numCorners - The number of vertices for each cell
2429: . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
2430: . cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
2431: . spaceDim - The spatial dimension used for coordinates
2432: - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex

2434:   Output Parameter:
2435: + dm - The DM
2436: - vertexSF - Optional, SF describing complete vertex ownership

2438:   Note: Two triangles sharing a face
2439: $
2440: $        2
2441: $      / | \
2442: $     /  |  \
2443: $    /   |   \
2444: $   0  0 | 1  3
2445: $    \   |   /
2446: $     \  |  /
2447: $      \ | /
2448: $        1
2449: would have input
2450: $  numCells = 2, numVertices = 4
2451: $  cells = [0 1 2  1 3 2]
2452: $
2453: which would result in the DMPlex
2454: $
2455: $        4
2456: $      / | \
2457: $     /  |  \
2458: $    /   |   \
2459: $   2  0 | 1  5
2460: $    \   |   /
2461: $     \  |  /
2462: $      \ | /
2463: $        3

2465:   Level: beginner

2467: .seealso: DMPlexCreateFromCellList(), DMPlexCreateFromDAG(), DMPlexCreate()
2468: @*/
2469: 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)
2470: {
2471:   PetscSF        sfVert;

2475:   DMCreate(comm, dm);
2476:   DMSetType(*dm, DMPLEX);
2479:   DMSetDimension(*dm, dim);
2480:   DMPlexBuildFromCellList_Parallel_Private(*dm, numCells, numVertices, numCorners, cells, &sfVert);
2481:   if (interpolate) {
2482:     DM idm = NULL;

2484:     DMPlexInterpolate(*dm, &idm);
2485:     DMDestroy(dm);
2486:     *dm  = idm;
2487:   }
2488:   DMPlexBuildCoordinates_Parallel_Private(*dm, spaceDim, numCells, numVertices,sfVert, vertexCoords);
2489:   if (vertexSF) *vertexSF = sfVert;
2490:   else {PetscSFDestroy(&sfVert);}
2491:   return(0);
2492: }

2494: /*
2495:   This takes as input the common mesh generator output, a list of the vertices for each cell
2496: */
2497: static PetscErrorCode DMPlexBuildFromCellList_Private(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const int cells[])
2498: {
2499:   PetscInt      *cone, c, p;

2503:   DMPlexSetChart(dm, 0, numCells+numVertices);
2504:   for (c = 0; c < numCells; ++c) {
2505:     DMPlexSetConeSize(dm, c, numCorners);
2506:   }
2507:   DMSetUp(dm);
2508:   DMGetWorkArray(dm, numCorners, PETSC_INT, &cone);
2509:   for (c = 0; c < numCells; ++c) {
2510:     for (p = 0; p < numCorners; ++p) {
2511:       cone[p] = cells[c*numCorners+p]+numCells;
2512:     }
2513:     DMPlexSetCone(dm, c, cone);
2514:   }
2515:   DMRestoreWorkArray(dm, numCorners, PETSC_INT, &cone);
2516:   DMPlexSymmetrize(dm);
2517:   DMPlexStratify(dm);
2518:   return(0);
2519: }

2521: /*
2522:   This takes as input the coordinates for each vertex
2523: */
2524: static PetscErrorCode DMPlexBuildCoordinates_Private(DM dm, PetscInt spaceDim, PetscInt numCells, PetscInt numVertices, const double vertexCoords[])
2525: {
2526:   PetscSection   coordSection;
2527:   Vec            coordinates;
2528:   DM             cdm;
2529:   PetscScalar   *coords;
2530:   PetscInt       v, d;

2534:   DMSetCoordinateDim(dm, spaceDim);
2535:   DMGetCoordinateSection(dm, &coordSection);
2536:   PetscSectionSetNumFields(coordSection, 1);
2537:   PetscSectionSetFieldComponents(coordSection, 0, spaceDim);
2538:   PetscSectionSetChart(coordSection, numCells, numCells + numVertices);
2539:   for (v = numCells; v < numCells+numVertices; ++v) {
2540:     PetscSectionSetDof(coordSection, v, spaceDim);
2541:     PetscSectionSetFieldDof(coordSection, v, 0, spaceDim);
2542:   }
2543:   PetscSectionSetUp(coordSection);

2545:   DMGetCoordinateDM(dm, &cdm);
2546:   DMCreateLocalVector(cdm, &coordinates);
2547:   VecSetBlockSize(coordinates, spaceDim);
2548:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
2549:   VecGetArray(coordinates, &coords);
2550:   for (v = 0; v < numVertices; ++v) {
2551:     for (d = 0; d < spaceDim; ++d) {
2552:       coords[v*spaceDim+d] = vertexCoords[v*spaceDim+d];
2553:     }
2554:   }
2555:   VecRestoreArray(coordinates, &coords);
2556:   DMSetCoordinatesLocal(dm, coordinates);
2557:   VecDestroy(&coordinates);
2558:   return(0);
2559: }

2561: /*@C
2562:   DMPlexCreateFromCellList - This takes as input common mesh generator output, a list of the vertices for each cell, and produces a DM

2564:   Input Parameters:
2565: + comm - The communicator
2566: . dim - The topological dimension of the mesh
2567: . numCells - The number of cells
2568: . numVertices - The number of vertices
2569: . numCorners - The number of vertices for each cell
2570: . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
2571: . cells - An array of numCells*numCorners numbers, the vertices for each cell
2572: . spaceDim - The spatial dimension used for coordinates
2573: - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex

2575:   Output Parameter:
2576: . dm - The DM

2578:   Note: Two triangles sharing a face
2579: $
2580: $        2
2581: $      / | \
2582: $     /  |  \
2583: $    /   |   \
2584: $   0  0 | 1  3
2585: $    \   |   /
2586: $     \  |  /
2587: $      \ | /
2588: $        1
2589: would have input
2590: $  numCells = 2, numVertices = 4
2591: $  cells = [0 1 2  1 3 2]
2592: $
2593: which would result in the DMPlex
2594: $
2595: $        4
2596: $      / | \
2597: $     /  |  \
2598: $    /   |   \
2599: $   2  0 | 1  5
2600: $    \   |   /
2601: $     \  |  /
2602: $      \ | /
2603: $        3

2605:   Level: beginner

2607: .seealso: DMPlexCreateFromDAG(), DMPlexCreate()
2608: @*/
2609: 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)
2610: {

2614:   DMCreate(comm, dm);
2615:   DMSetType(*dm, DMPLEX);
2616:   DMSetDimension(*dm, dim);
2617:   DMPlexBuildFromCellList_Private(*dm, numCells, numVertices, numCorners, cells);
2618:   if (interpolate) {
2619:     DM idm = NULL;

2621:     DMPlexInterpolate(*dm, &idm);
2622:     DMDestroy(dm);
2623:     *dm  = idm;
2624:   }
2625:   DMPlexBuildCoordinates_Private(*dm, spaceDim, numCells, numVertices, vertexCoords);
2626:   return(0);
2627: }

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

2632:   Input Parameters:
2633: + dm - The empty DM object, usually from DMCreate() and DMSetDimension()
2634: . depth - The depth of the DAG
2635: . numPoints - The number of points at each depth
2636: . coneSize - The cone size of each point
2637: . cones - The concatenation of the cone points for each point, the cone list must be oriented correctly for each point
2638: . coneOrientations - The orientation of each cone point
2639: - vertexCoords - An array of numVertices*dim numbers, the coordinates of each vertex

2641:   Output Parameter:
2642: . dm - The DM

2644:   Note: Two triangles sharing a face would have input
2645: $  depth = 1, numPoints = [4 2], coneSize = [3 3 0 0 0 0]
2646: $  cones = [2 3 4  3 5 4], coneOrientations = [0 0 0  0 0 0]
2647: $ vertexCoords = [-1.0 0.0  0.0 -1.0  0.0 1.0  1.0 0.0]
2648: $
2649: which would result in the DMPlex
2650: $
2651: $        4
2652: $      / | \
2653: $     /  |  \
2654: $    /   |   \
2655: $   2  0 | 1  5
2656: $    \   |   /
2657: $     \  |  /
2658: $      \ | /
2659: $        3
2660: $
2661: $ Notice that all points are numbered consecutively, unlikely DMPlexCreateFromCellList()

2663:   Level: advanced

2665: .seealso: DMPlexCreateFromCellList(), DMPlexCreate()
2666: @*/
2667: PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[])
2668: {
2669:   Vec            coordinates;
2670:   PetscSection   coordSection;
2671:   PetscScalar    *coords;
2672:   PetscInt       coordSize, firstVertex = -1, pStart = 0, pEnd = 0, p, v, dim, dimEmbed, d, off;

2676:   DMGetDimension(dm, &dim);
2677:   DMGetCoordinateDim(dm, &dimEmbed);
2678:   if (dimEmbed < dim) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Embedding dimension %d cannot be less than intrinsic dimension %d",dimEmbed,dim);
2679:   for (d = 0; d <= depth; ++d) pEnd += numPoints[d];
2680:   DMPlexSetChart(dm, pStart, pEnd);
2681:   for (p = pStart; p < pEnd; ++p) {
2682:     DMPlexSetConeSize(dm, p, coneSize[p-pStart]);
2683:     if (firstVertex < 0 && !coneSize[p - pStart]) {
2684:       firstVertex = p - pStart;
2685:     }
2686:   }
2687:   if (firstVertex < 0 && numPoints[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected %d vertices but could not find any", numPoints[0]);
2688:   DMSetUp(dm); /* Allocate space for cones */
2689:   for (p = pStart, off = 0; p < pEnd; off += coneSize[p-pStart], ++p) {
2690:     DMPlexSetCone(dm, p, &cones[off]);
2691:     DMPlexSetConeOrientation(dm, p, &coneOrientations[off]);
2692:   }
2693:   DMPlexSymmetrize(dm);
2694:   DMPlexStratify(dm);
2695:   /* Build coordinates */
2696:   DMGetCoordinateSection(dm, &coordSection);
2697:   PetscSectionSetNumFields(coordSection, 1);
2698:   PetscSectionSetFieldComponents(coordSection, 0, dimEmbed);
2699:   PetscSectionSetChart(coordSection, firstVertex, firstVertex+numPoints[0]);
2700:   for (v = firstVertex; v < firstVertex+numPoints[0]; ++v) {
2701:     PetscSectionSetDof(coordSection, v, dimEmbed);
2702:     PetscSectionSetFieldDof(coordSection, v, 0, dimEmbed);
2703:   }
2704:   PetscSectionSetUp(coordSection);
2705:   PetscSectionGetStorageSize(coordSection, &coordSize);
2706:   VecCreate(PETSC_COMM_SELF, &coordinates);
2707:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
2708:   VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
2709:   VecSetBlockSize(coordinates, dimEmbed);
2710:   VecSetType(coordinates,VECSTANDARD);
2711:   VecGetArray(coordinates, &coords);
2712:   for (v = 0; v < numPoints[0]; ++v) {
2713:     PetscInt off;

2715:     PetscSectionGetOffset(coordSection, v+firstVertex, &off);
2716:     for (d = 0; d < dimEmbed; ++d) {
2717:       coords[off+d] = vertexCoords[v*dimEmbed+d];
2718:     }
2719:   }
2720:   VecRestoreArray(coordinates, &coords);
2721:   DMSetCoordinatesLocal(dm, coordinates);
2722:   VecDestroy(&coordinates);
2723:   return(0);
2724: }

2726: /*@C
2727:   DMPlexCreateFromFile - This takes a filename and produces a DM

2729:   Input Parameters:
2730: + comm - The communicator
2731: . filename - A file name
2732: - interpolate - Flag to create intermediate mesh pieces (edges, faces)

2734:   Output Parameter:
2735: . dm - The DM

2737:   Level: beginner

2739: .seealso: DMPlexCreateFromDAG(), DMPlexCreateFromCellList(), DMPlexCreate()
2740: @*/
2741: PetscErrorCode DMPlexCreateFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
2742: {
2743:   const char    *extGmsh    = ".msh";
2744:   const char    *extCGNS    = ".cgns";
2745:   const char    *extExodus  = ".exo";
2746:   const char    *extGenesis = ".gen";
2747:   const char    *extFluent  = ".cas";
2748:   const char    *extHDF5    = ".h5";
2749:   const char    *extMed     = ".med";
2750:   const char    *extPLY     = ".ply";
2751:   size_t         len;
2752:   PetscBool      isGmsh, isCGNS, isExodus, isGenesis, isFluent, isHDF5, isMed, isPLY;
2753:   PetscMPIInt    rank;

2759:   MPI_Comm_rank(comm, &rank);
2760:   PetscStrlen(filename, &len);
2761:   if (!len) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Filename must be a valid path");
2762:   PetscStrncmp(&filename[PetscMax(0,len-4)], extGmsh,    4, &isGmsh);
2763:   PetscStrncmp(&filename[PetscMax(0,len-5)], extCGNS,    5, &isCGNS);
2764:   PetscStrncmp(&filename[PetscMax(0,len-4)], extExodus,  4, &isExodus);
2765:   PetscStrncmp(&filename[PetscMax(0,len-4)], extGenesis, 4, &isGenesis);
2766:   PetscStrncmp(&filename[PetscMax(0,len-4)], extFluent,  4, &isFluent);
2767:   PetscStrncmp(&filename[PetscMax(0,len-3)], extHDF5,    3, &isHDF5);
2768:   PetscStrncmp(&filename[PetscMax(0,len-4)], extMed,     4, &isMed);
2769:   PetscStrncmp(&filename[PetscMax(0,len-4)], extPLY,     4, &isPLY);
2770:   if (isGmsh) {
2771:     DMPlexCreateGmshFromFile(comm, filename, interpolate, dm);
2772:   } else if (isCGNS) {
2773:     DMPlexCreateCGNSFromFile(comm, filename, interpolate, dm);
2774:   } else if (isExodus || isGenesis) {
2775:     DMPlexCreateExodusFromFile(comm, filename, interpolate, dm);
2776:   } else if (isFluent) {
2777:     DMPlexCreateFluentFromFile(comm, filename, interpolate, dm);
2778:   } else if (isHDF5) {
2779:     PetscViewer viewer;

2781:     PetscViewerCreate(comm, &viewer);
2782:     PetscViewerSetType(viewer, PETSCVIEWERHDF5);
2783:     PetscViewerFileSetMode(viewer, FILE_MODE_READ);
2784:     PetscViewerFileSetName(viewer, filename);
2785:     DMCreate(comm, dm);
2786:     DMSetType(*dm, DMPLEX);
2787:     DMLoad(*dm, viewer);
2788:     PetscViewerDestroy(&viewer);
2789:   } else if (isMed) {
2790:     DMPlexCreateMedFromFile(comm, filename, interpolate, dm);
2791:   } else if (isPLY) {
2792:     DMPlexCreatePLYFromFile(comm, filename, interpolate, dm);
2793:   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename);
2794:   return(0);
2795: }

2797: /*@
2798:   DMPlexCreateReferenceCell - Create a DMPLEX with the appropriate FEM reference cell

2800:   Collective on comm

2802:   Input Parameters:
2803: + comm    - The communicator
2804: . dim     - The spatial dimension
2805: - simplex - Flag for simplex, otherwise use a tensor-product cell

2807:   Output Parameter:
2808: . refdm - The reference cell

2810:   Level: intermediate

2812: .keywords: reference cell
2813: .seealso:
2814: @*/
2815: PetscErrorCode DMPlexCreateReferenceCell(MPI_Comm comm, PetscInt dim, PetscBool simplex, DM *refdm)
2816: {
2817:   DM             rdm;
2818:   Vec            coords;

2822:   DMCreate(comm, &rdm);
2823:   DMSetType(rdm, DMPLEX);
2824:   DMSetDimension(rdm, dim);
2825:   switch (dim) {
2826:   case 0:
2827:   {
2828:     PetscInt    numPoints[1]        = {1};
2829:     PetscInt    coneSize[1]         = {0};
2830:     PetscInt    cones[1]            = {0};
2831:     PetscInt    coneOrientations[1] = {0};
2832:     PetscScalar vertexCoords[1]     = {0.0};

2834:     DMPlexCreateFromDAG(rdm, 0, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2835:   }
2836:   break;
2837:   case 1:
2838:   {
2839:     PetscInt    numPoints[2]        = {2, 1};
2840:     PetscInt    coneSize[3]         = {2, 0, 0};
2841:     PetscInt    cones[2]            = {1, 2};
2842:     PetscInt    coneOrientations[2] = {0, 0};
2843:     PetscScalar vertexCoords[2]     = {-1.0,  1.0};

2845:     DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2846:   }
2847:   break;
2848:   case 2:
2849:     if (simplex) {
2850:       PetscInt    numPoints[2]        = {3, 1};
2851:       PetscInt    coneSize[4]         = {3, 0, 0, 0};
2852:       PetscInt    cones[3]            = {1, 2, 3};
2853:       PetscInt    coneOrientations[3] = {0, 0, 0};
2854:       PetscScalar vertexCoords[6]     = {-1.0, -1.0,  1.0, -1.0,  -1.0, 1.0};

2856:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2857:     } else {
2858:       PetscInt    numPoints[2]        = {4, 1};
2859:       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
2860:       PetscInt    cones[4]            = {1, 2, 3, 4};
2861:       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
2862:       PetscScalar vertexCoords[8]     = {-1.0, -1.0,  1.0, -1.0,  1.0, 1.0,  -1.0, 1.0};

2864:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2865:     }
2866:   break;
2867:   case 3:
2868:     if (simplex) {
2869:       PetscInt    numPoints[2]        = {4, 1};
2870:       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
2871:       PetscInt    cones[4]            = {1, 3, 2, 4};
2872:       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
2873:       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};

2875:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2876:     } else {
2877:       PetscInt    numPoints[2]        = {8, 1};
2878:       PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
2879:       PetscInt    cones[8]            = {1, 4, 3, 2, 5, 6, 7, 8};
2880:       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
2881:       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,
2882:                                          -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  1.0, 1.0,  1.0,  -1.0, 1.0,  1.0};

2884:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2885:     }
2886:   break;
2887:   default:
2888:     SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Cannot create reference cell for dimension %d", dim);
2889:   }
2890:   *refdm = NULL;
2891:   DMPlexInterpolate(rdm, refdm);
2892:   if (rdm->coordinateDM) {
2893:     DM           ncdm;
2894:     PetscSection cs;
2895:     PetscInt     pEnd = -1;

2897:     DMGetDefaultSection(rdm->coordinateDM, &cs);
2898:     if (cs) {PetscSectionGetChart(cs, NULL, &pEnd);}
2899:     if (pEnd >= 0) {
2900:       DMClone(rdm->coordinateDM, &ncdm);
2901:       DMSetDefaultSection(ncdm, cs);
2902:       DMSetCoordinateDM(*refdm, ncdm);
2903:       DMDestroy(&ncdm);
2904:     }
2905:   }
2906:   DMGetCoordinatesLocal(rdm, &coords);
2907:   if (coords) {
2908:     DMSetCoordinatesLocal(*refdm, coords);
2909:   } else {
2910:     DMGetCoordinates(rdm, &coords);
2911:     if (coords) {DMSetCoordinates(*refdm, coords);}
2912:   }
2913:   DMDestroy(&rdm);
2914:   return(0);
2915: }