Actual source code: plexcreate.c

petsc-master 2019-12-09
Report Typos and Errors
  1: #define PETSCDM_DLL
  2:  #include <petsc/private/dmpleximpl.h>
  3:  #include <petsc/private/hashseti.h>
  4:  #include <petscsf.h>

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

  9:   Collective

 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: .seealso: DMSetType(), DMCreate()
 25: @*/
 26: PetscErrorCode DMPlexCreateDoublet(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscBool interpolate, PetscBool refinementUniform, PetscReal refinementLimit, DM *newdm)
 27: {
 28:   DM             dm;
 29:   PetscInt       p;
 30:   PetscMPIInt    rank;

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

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

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

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

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

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

119:     DMPlexInterpolate(*newdm, &idm);
120:     DMDestroy(newdm);
121:     *newdm = idm;
122:   }
123:   {
124:     DM refinedMesh     = NULL;
125:     DM distributedMesh = NULL;

127:     /* Distribute mesh over processes */
128:     DMPlexDistribute(*newdm, 0, NULL, &distributedMesh);
129:     if (distributedMesh) {
130:       DMDestroy(newdm);
131:       *newdm = distributedMesh;
132:     }
133:     if (refinementUniform) {
134:       DMPlexSetRefinementUniform(*newdm, refinementUniform);
135:       DMRefine(*newdm, comm, &refinedMesh);
136:       if (refinedMesh) {
137:         DMDestroy(newdm);
138:         *newdm = refinedMesh;
139:       }
140:     }
141:   }
142:   return(0);
143: }

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

148:   Collective

150:   Input Parameters:
151: + comm  - The communicator for the DM object
152: . lower - The lower left corner coordinates
153: . upper - The upper right corner coordinates
154: - edges - The number of cells in each direction

156:   Output Parameter:
157: . dm  - The DM object

159:   Note: Here is the numbering returned for 2 cells in each direction:
160: $ 18--5-17--4--16
161: $  |     |     |
162: $  6    10     3
163: $  |     |     |
164: $ 19-11-20--9--15
165: $  |     |     |
166: $  7     8     2
167: $  |     |     |
168: $ 12--0-13--1--14

170:   Level: beginner

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

192:   PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);
193:   if (markerSeparate) {
194:     markerTop    = 3;
195:     markerBottom = 1;
196:     markerRight  = 2;
197:     markerLeft   = 4;
198:   }
199:   MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
200:   if (!rank) {
201:     PetscInt e, ex, ey;

203:     DMPlexSetChart(dm, 0, numEdges+numVertices);
204:     for (e = 0; e < numEdges; ++e) {
205:       DMPlexSetConeSize(dm, e, 2);
206:     }
207:     DMSetUp(dm); /* Allocate space for cones */
208:     for (vx = 0; vx <= edges[0]; vx++) {
209:       for (ey = 0; ey < edges[1]; ey++) {
210:         PetscInt edge   = vx*edges[1] + ey + edges[0]*(edges[1]+1);
211:         PetscInt vertex = ey*(edges[0]+1) + vx + numEdges;
212:         PetscInt cone[2];

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

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

291: /*@
292:   DMPlexCreateCubeBoundary - Creates a 2D mesh that is the boundary of a cubic lattice.

294:   Collective

296:   Input Parameters:
297: + comm  - The communicator for the DM object
298: . lower - The lower left front corner coordinates
299: . upper - The upper right back corner coordinates
300: - edges - The number of cells in each direction

302:   Output Parameter:
303: . dm  - The DM object

305:   Level: beginner

307: .seealso: DMPlexCreateBoxMesh(), DMPlexCreateSquareBoundary(), DMSetType(), DMCreate()
308: @*/
309: PetscErrorCode DMPlexCreateCubeBoundary(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt faces[])
310: {
311:   PetscInt       vertices[3], numVertices;
312:   PetscInt       numFaces    = 2*faces[0]*faces[1] + 2*faces[1]*faces[2] + 2*faces[0]*faces[2];
313:   Vec            coordinates;
314:   PetscSection   coordSection;
315:   PetscScalar    *coords;
316:   PetscInt       coordSize;
317:   PetscMPIInt    rank;
318:   PetscInt       v, vx, vy, vz;
319:   PetscInt       voffset, iface=0, cone[4];

323:   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");
324:   MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
325:   vertices[0] = faces[0]+1; vertices[1] = faces[1]+1; vertices[2] = faces[2]+1;
326:   numVertices = vertices[0]*vertices[1]*vertices[2];
327:   if (!rank) {
328:     PetscInt f;

330:     DMPlexSetChart(dm, 0, numFaces+numVertices);
331:     for (f = 0; f < numFaces; ++f) {
332:       DMPlexSetConeSize(dm, f, 4);
333:     }
334:     DMSetUp(dm); /* Allocate space for cones */

336:     /* Side 0 (Top) */
337:     for (vy = 0; vy < faces[1]; vy++) {
338:       for (vx = 0; vx < faces[0]; vx++) {
339:         voffset = numFaces + vertices[0]*vertices[1]*(vertices[2]-1) + vy*vertices[0] + vx;
340:         cone[0] = voffset; cone[1] = voffset+1; cone[2] = voffset+vertices[0]+1; cone[3] = voffset+vertices[0];
341:         DMPlexSetCone(dm, iface, cone);
342:         DMSetLabelValue(dm, "marker", iface, 1);
343:         DMSetLabelValue(dm, "marker", voffset+0, 1);
344:         DMSetLabelValue(dm, "marker", voffset+1, 1);
345:         DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, 1);
346:         DMSetLabelValue(dm, "marker", voffset+vertices[0]+1, 1);
347:         iface++;
348:       }
349:     }

351:     /* Side 1 (Bottom) */
352:     for (vy = 0; vy < faces[1]; vy++) {
353:       for (vx = 0; vx < faces[0]; vx++) {
354:         voffset = numFaces + vy*(faces[0]+1) + vx;
355:         cone[0] = voffset+1; cone[1] = voffset; cone[2] = voffset+vertices[0]; cone[3] = voffset+vertices[0]+1;
356:         DMPlexSetCone(dm, iface, cone);
357:         DMSetLabelValue(dm, "marker", iface, 1);
358:         DMSetLabelValue(dm, "marker", voffset+0, 1);
359:         DMSetLabelValue(dm, "marker", voffset+1, 1);
360:         DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, 1);
361:         DMSetLabelValue(dm, "marker", voffset+vertices[0]+1, 1);
362:         iface++;
363:       }
364:     }

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

381:     /* Side 3 (Back) */
382:     for (vz = 0; vz < faces[2]; vz++) {
383:       for (vx = 0; vx < faces[0]; vx++) {
384:         voffset = numFaces + vz*vertices[0]*vertices[1] + vertices[0]*(vertices[1]-1) + vx;
385:         cone[0] = voffset+vertices[0]*vertices[1]; cone[1] = voffset+vertices[0]*vertices[1]+1;
386:         cone[2] = voffset+1; cone[3] = voffset;
387:         DMPlexSetCone(dm, iface, cone);
388:         DMSetLabelValue(dm, "marker", iface, 1);
389:         DMSetLabelValue(dm, "marker", voffset+0, 1);
390:         DMSetLabelValue(dm, "marker", voffset+1, 1);
391:         DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+0, 1);
392:         DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+1, 1);
393:         iface++;
394:       }
395:     }

397:     /* Side 4 (Left) */
398:     for (vz = 0; vz < faces[2]; vz++) {
399:       for (vy = 0; vy < faces[1]; vy++) {
400:         voffset = numFaces + vz*vertices[0]*vertices[1] + vy*vertices[0];
401:         cone[0] = voffset; cone[1] = voffset+vertices[0]*vertices[1];
402:         cone[2] = voffset+vertices[0]*vertices[1]+vertices[0]; cone[3] = voffset+vertices[0];
403:         DMPlexSetCone(dm, iface, cone);
404:         DMSetLabelValue(dm, "marker", iface, 1);
405:         DMSetLabelValue(dm, "marker", voffset+0, 1);
406:         DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, 1);
407:         DMSetLabelValue(dm, "marker", voffset+vertices[1]+0, 1);
408:         DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+vertices[0], 1);
409:         iface++;
410:       }
411:     }

413:     /* Side 5 (Right) */
414:     for (vz = 0; vz < faces[2]; vz++) {
415:       for (vy = 0; vy < faces[1]; vy++) {
416:         voffset = numFaces + vz*vertices[0]*vertices[1] + vy*vertices[0] + faces[0];
417:         cone[0] = voffset+vertices[0]*vertices[1]; cone[1] = voffset;
418:         cone[2] = voffset+vertices[0]; cone[3] = voffset+vertices[0]*vertices[1]+vertices[0];
419:         DMPlexSetCone(dm, iface, cone);
420:         DMSetLabelValue(dm, "marker", iface, 1);
421:         DMSetLabelValue(dm, "marker", voffset+0, 1);
422:         DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, 1);
423:         DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+0, 1);
424:         DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+vertices[0], 1);
425:         iface++;
426:       }
427:     }
428:   }
429:   DMPlexSymmetrize(dm);
430:   DMPlexStratify(dm);
431:   /* Build coordinates */
432:   DMSetCoordinateDim(dm, 3);
433:   DMGetCoordinateSection(dm, &coordSection);
434:   PetscSectionSetChart(coordSection, numFaces, numFaces + numVertices);
435:   for (v = numFaces; v < numFaces+numVertices; ++v) {
436:     PetscSectionSetDof(coordSection, v, 3);
437:   }
438:   PetscSectionSetUp(coordSection);
439:   PetscSectionGetStorageSize(coordSection, &coordSize);
440:   VecCreate(PETSC_COMM_SELF, &coordinates);
441:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
442:   VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
443:   VecSetBlockSize(coordinates, 3);
444:   VecSetType(coordinates,VECSTANDARD);
445:   VecGetArray(coordinates, &coords);
446:   for (vz = 0; vz <= faces[2]; ++vz) {
447:     for (vy = 0; vy <= faces[1]; ++vy) {
448:       for (vx = 0; vx <= faces[0]; ++vx) {
449:         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+0] = lower[0] + ((upper[0] - lower[0])/faces[0])*vx;
450:         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+1] = lower[1] + ((upper[1] - lower[1])/faces[1])*vy;
451:         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+2] = lower[2] + ((upper[2] - lower[2])/faces[2])*vz;
452:       }
453:     }
454:   }
455:   VecRestoreArray(coordinates, &coords);
456:   DMSetCoordinatesLocal(dm, coordinates);
457:   VecDestroy(&coordinates);
458:   return(0);
459: }

461: static PetscErrorCode DMPlexCreateLineMesh_Internal(MPI_Comm comm,PetscInt segments,PetscReal lower,PetscReal upper,DMBoundaryType bd,DM *dm)
462: {
463:   PetscInt       i,fStart,fEnd,numCells = 0,numVerts = 0;
464:   PetscInt       numPoints[2],*coneSize,*cones,*coneOrientations;
465:   PetscScalar    *vertexCoords;
466:   PetscReal      L,maxCell;
467:   PetscBool      markerSeparate = PETSC_FALSE;
468:   PetscInt       markerLeft  = 1, faceMarkerLeft  = 1;
469:   PetscInt       markerRight = 1, faceMarkerRight = 2;
470:   PetscBool      wrap = (bd == DM_BOUNDARY_PERIODIC || bd == DM_BOUNDARY_TWIST) ? PETSC_TRUE : PETSC_FALSE;
471:   PetscMPIInt    rank;


477:   DMCreate(comm,dm);
478:   DMSetType(*dm,DMPLEX);
479:   DMSetDimension(*dm,1);
480:   DMCreateLabel(*dm,"marker");
481:   DMCreateLabel(*dm,"Face Sets");

483:   MPI_Comm_rank(comm,&rank);
484:   if (!rank) numCells = segments;
485:   if (!rank) numVerts = segments + (wrap ? 0 : 1);

487:   numPoints[0] = numVerts ; numPoints[1] = numCells;
488:   PetscMalloc4(numCells+numVerts,&coneSize,numCells*2,&cones,numCells+numVerts,&coneOrientations,numVerts,&vertexCoords);
489:   PetscArrayzero(coneOrientations,numCells+numVerts);
490:   for (i = 0; i < numCells; ++i) { coneSize[i] = 2; }
491:   for (i = 0; i < numVerts; ++i) { coneSize[numCells+i] = 0; }
492:   for (i = 0; i < numCells; ++i) { cones[2*i] = numCells + i%numVerts; cones[2*i+1] = numCells + (i+1)%numVerts; }
493:   for (i = 0; i < numVerts; ++i) { vertexCoords[i] = lower + (upper-lower)*((PetscReal)i/(PetscReal)numCells); }
494:   DMPlexCreateFromDAG(*dm,1,numPoints,coneSize,cones,coneOrientations,vertexCoords);
495:   PetscFree4(coneSize,cones,coneOrientations,vertexCoords);

497:   PetscOptionsGetBool(((PetscObject)*dm)->options,((PetscObject)*dm)->prefix,"-dm_plex_separate_marker",&markerSeparate,NULL);
498:   if (markerSeparate) { markerLeft = faceMarkerLeft; markerRight = faceMarkerRight;}
499:   if (!wrap && !rank) {
500:     DMPlexGetHeightStratum(*dm,1,&fStart,&fEnd);
501:     DMSetLabelValue(*dm,"marker",fStart,markerLeft);
502:     DMSetLabelValue(*dm,"marker",fEnd-1,markerRight);
503:     DMSetLabelValue(*dm,"Face Sets",fStart,faceMarkerLeft);
504:     DMSetLabelValue(*dm,"Face Sets",fEnd-1,faceMarkerRight);
505:   }
506:   if (wrap) {
507:     L       = upper - lower;
508:     maxCell = (PetscReal)1.1*(L/(PetscReal)PetscMax(1,segments));
509:     DMSetPeriodicity(*dm,PETSC_TRUE,&maxCell,&L,&bd);
510:   }
511:   return(0);
512: }

514: static PetscErrorCode DMPlexCreateBoxMesh_Simplex_Internal(MPI_Comm comm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate, DM *dm)
515: {
516:   DM             boundary;
517:   PetscInt       i;

522:   for (i = 0; i < dim; ++i) if (periodicity[i] != DM_BOUNDARY_NONE) SETERRQ(comm, PETSC_ERR_SUP, "Periodicity is not supported for simplex meshes");
523:   DMCreate(comm, &boundary);
525:   DMSetType(boundary, DMPLEX);
526:   DMSetDimension(boundary, dim-1);
527:   DMSetCoordinateDim(boundary, dim);
528:   switch (dim) {
529:   case 2: DMPlexCreateSquareBoundary(boundary, lower, upper, faces);break;
530:   case 3: DMPlexCreateCubeBoundary(boundary, lower, upper, faces);break;
531:   default: SETERRQ1(comm, PETSC_ERR_SUP, "Dimension not supported: %d", dim);
532:   }
533:   DMPlexGenerate(boundary, NULL, interpolate, dm);
534:   DMDestroy(&boundary);
535:   return(0);
536: }

538: static PetscErrorCode DMPlexCreateCubeMesh_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY, DMBoundaryType bdZ)
539: {
540:   DMLabel        cutLabel = NULL;
541:   PetscInt       markerTop      = 1, faceMarkerTop      = 1;
542:   PetscInt       markerBottom   = 1, faceMarkerBottom   = 1;
543:   PetscInt       markerFront    = 1, faceMarkerFront    = 1;
544:   PetscInt       markerBack     = 1, faceMarkerBack     = 1;
545:   PetscInt       markerRight    = 1, faceMarkerRight    = 1;
546:   PetscInt       markerLeft     = 1, faceMarkerLeft     = 1;
547:   PetscInt       dim;
548:   PetscBool      markerSeparate = PETSC_FALSE, cutMarker = PETSC_FALSE;
549:   PetscMPIInt    rank;

553:   DMGetDimension(dm,&dim);
554:   MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
555:   DMCreateLabel(dm,"marker");
556:   DMCreateLabel(dm,"Face Sets");
557:   PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL);
558:   if (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ||
559:       bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ||
560:       bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST) {

562:     if (cutMarker) {DMCreateLabel(dm, "periodic_cut"); DMGetLabel(dm, "periodic_cut", &cutLabel);}
563:   }
564:   switch (dim) {
565:   case 2:
566:     faceMarkerTop    = 3;
567:     faceMarkerBottom = 1;
568:     faceMarkerRight  = 2;
569:     faceMarkerLeft   = 4;
570:     break;
571:   case 3:
572:     faceMarkerBottom = 1;
573:     faceMarkerTop    = 2;
574:     faceMarkerFront  = 3;
575:     faceMarkerBack   = 4;
576:     faceMarkerRight  = 5;
577:     faceMarkerLeft   = 6;
578:     break;
579:   default:
580:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Dimension %d not supported",dim);
581:     break;
582:   }
583:   PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);
584:   if (markerSeparate) {
585:     markerBottom = faceMarkerBottom;
586:     markerTop    = faceMarkerTop;
587:     markerFront  = faceMarkerFront;
588:     markerBack   = faceMarkerBack;
589:     markerRight  = faceMarkerRight;
590:     markerLeft   = faceMarkerLeft;
591:   }
592:   {
593:     const PetscInt numXEdges    = !rank ? edges[0] : 0;
594:     const PetscInt numYEdges    = !rank ? edges[1] : 0;
595:     const PetscInt numZEdges    = !rank ? edges[2] : 0;
596:     const PetscInt numXVertices = !rank ? (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ? edges[0] : edges[0]+1) : 0;
597:     const PetscInt numYVertices = !rank ? (bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ? edges[1] : edges[1]+1) : 0;
598:     const PetscInt numZVertices = !rank ? (bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST ? edges[2] : edges[2]+1) : 0;
599:     const PetscInt numCells     = numXEdges*numYEdges*numZEdges;
600:     const PetscInt numXFaces    = numYEdges*numZEdges;
601:     const PetscInt numYFaces    = numXEdges*numZEdges;
602:     const PetscInt numZFaces    = numXEdges*numYEdges;
603:     const PetscInt numTotXFaces = numXVertices*numXFaces;
604:     const PetscInt numTotYFaces = numYVertices*numYFaces;
605:     const PetscInt numTotZFaces = numZVertices*numZFaces;
606:     const PetscInt numFaces     = numTotXFaces + numTotYFaces + numTotZFaces;
607:     const PetscInt numTotXEdges = numXEdges*numYVertices*numZVertices;
608:     const PetscInt numTotYEdges = numYEdges*numXVertices*numZVertices;
609:     const PetscInt numTotZEdges = numZEdges*numXVertices*numYVertices;
610:     const PetscInt numVertices  = numXVertices*numYVertices*numZVertices;
611:     const PetscInt numEdges     = numTotXEdges + numTotYEdges + numTotZEdges;
612:     const PetscInt firstVertex  = (dim == 2) ? numFaces : numCells;
613:     const PetscInt firstXFace   = (dim == 2) ? 0 : numCells + numVertices;
614:     const PetscInt firstYFace   = firstXFace + numTotXFaces;
615:     const PetscInt firstZFace   = firstYFace + numTotYFaces;
616:     const PetscInt firstXEdge   = numCells + numFaces + numVertices;
617:     const PetscInt firstYEdge   = firstXEdge + numTotXEdges;
618:     const PetscInt firstZEdge   = firstYEdge + numTotYEdges;
619:     Vec            coordinates;
620:     PetscSection   coordSection;
621:     PetscScalar   *coords;
622:     PetscInt       coordSize;
623:     PetscInt       v, vx, vy, vz;
624:     PetscInt       c, f, fx, fy, fz, e, ex, ey, ez;

626:     DMPlexSetChart(dm, 0, numCells+numFaces+numEdges+numVertices);
627:     for (c = 0; c < numCells; c++) {
628:       DMPlexSetConeSize(dm, c, 6);
629:     }
630:     for (f = firstXFace; f < firstXFace+numFaces; ++f) {
631:       DMPlexSetConeSize(dm, f, 4);
632:     }
633:     for (e = firstXEdge; e < firstXEdge+numEdges; ++e) {
634:       DMPlexSetConeSize(dm, e, 2);
635:     }
636:     DMSetUp(dm); /* Allocate space for cones */
637:     /* Build cells */
638:     for (fz = 0; fz < numZEdges; ++fz) {
639:       for (fy = 0; fy < numYEdges; ++fy) {
640:         for (fx = 0; fx < numXEdges; ++fx) {
641:           PetscInt cell    = (fz*numYEdges + fy)*numXEdges + fx;
642:           PetscInt faceB   = firstZFace + (fy*numXEdges+fx)*numZVertices +   fz;
643:           PetscInt faceT   = firstZFace + (fy*numXEdges+fx)*numZVertices + ((fz+1)%numZVertices);
644:           PetscInt faceF   = firstYFace + (fz*numXEdges+fx)*numYVertices +   fy;
645:           PetscInt faceK   = firstYFace + (fz*numXEdges+fx)*numYVertices + ((fy+1)%numYVertices);
646:           PetscInt faceL   = firstXFace + (fz*numYEdges+fy)*numXVertices +   fx;
647:           PetscInt faceR   = firstXFace + (fz*numYEdges+fy)*numXVertices + ((fx+1)%numXVertices);
648:                             /* B,  T,  F,  K,  R,  L */
649:           PetscInt ornt[6] = {-4,  0,  0, -1,  0, -4}; /* ??? */
650:           PetscInt cone[6];

652:           /* no boundary twisting in 3D */
653:           cone[0] = faceB; cone[1] = faceT; cone[2] = faceF; cone[3] = faceK; cone[4] = faceR; cone[5] = faceL;
654:           DMPlexSetCone(dm, cell, cone);
655:           DMPlexSetConeOrientation(dm, cell, ornt);
656:           if (bdX != DM_BOUNDARY_NONE && fx == numXEdges-1 && cutLabel) {DMLabelSetValue(cutLabel, cell, 2);}
657:           if (bdY != DM_BOUNDARY_NONE && fy == numYEdges-1 && cutLabel) {DMLabelSetValue(cutLabel, cell, 2);}
658:           if (bdZ != DM_BOUNDARY_NONE && fz == numZEdges-1 && cutLabel) {DMLabelSetValue(cutLabel, cell, 2);}
659:         }
660:       }
661:     }
662:     /* Build x faces */
663:     for (fz = 0; fz < numZEdges; ++fz) {
664:       for (fy = 0; fy < numYEdges; ++fy) {
665:         for (fx = 0; fx < numXVertices; ++fx) {
666:           PetscInt face    = firstXFace + (fz*numYEdges+fy)*numXVertices + fx;
667:           PetscInt edgeL   = firstZEdge + (  fy*                 numXVertices+fx)*numZEdges + fz;
668:           PetscInt edgeR   = firstZEdge + (((fy+1)%numYVertices)*numXVertices+fx)*numZEdges + fz;
669:           PetscInt edgeB   = firstYEdge + (  fz*                 numXVertices+fx)*numYEdges + fy;
670:           PetscInt edgeT   = firstYEdge + (((fz+1)%numZVertices)*numXVertices+fx)*numYEdges + fy;
671:           PetscInt ornt[4] = {0, 0, -2, -2};
672:           PetscInt cone[4];

674:           if (dim == 3) {
675:             /* markers */
676:             if (bdX != DM_BOUNDARY_PERIODIC) {
677:               if (fx == numXVertices-1) {
678:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerRight);
679:                 DMSetLabelValue(dm, "marker", face, markerRight);
680:               }
681:               else if (fx == 0) {
682:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerLeft);
683:                 DMSetLabelValue(dm, "marker", face, markerLeft);
684:               }
685:             }
686:           }
687:           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
688:           DMPlexSetCone(dm, face, cone);
689:           DMPlexSetConeOrientation(dm, face, ornt);
690:         }
691:       }
692:     }
693:     /* Build y faces */
694:     for (fz = 0; fz < numZEdges; ++fz) {
695:       for (fx = 0; fx < numXEdges; ++fx) {
696:         for (fy = 0; fy < numYVertices; ++fy) {
697:           PetscInt face    = firstYFace + (fz*numXEdges+fx)*numYVertices + fy;
698:           PetscInt edgeL   = firstZEdge + (fy*numXVertices+  fx                 )*numZEdges + fz;
699:           PetscInt edgeR   = firstZEdge + (fy*numXVertices+((fx+1)%numXVertices))*numZEdges + fz;
700:           PetscInt edgeB   = firstXEdge + (  fz                 *numYVertices+fy)*numXEdges + fx;
701:           PetscInt edgeT   = firstXEdge + (((fz+1)%numZVertices)*numYVertices+fy)*numXEdges + fx;
702:           PetscInt ornt[4] = {0, 0, -2, -2};
703:           PetscInt cone[4];

705:           if (dim == 3) {
706:             /* markers */
707:             if (bdY != DM_BOUNDARY_PERIODIC) {
708:               if (fy == numYVertices-1) {
709:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerBack);
710:                 DMSetLabelValue(dm, "marker", face, markerBack);
711:               }
712:               else if (fy == 0) {
713:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerFront);
714:                 DMSetLabelValue(dm, "marker", face, markerFront);
715:               }
716:             }
717:           }
718:           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
719:           DMPlexSetCone(dm, face, cone);
720:           DMPlexSetConeOrientation(dm, face, ornt);
721:         }
722:       }
723:     }
724:     /* Build z faces */
725:     for (fy = 0; fy < numYEdges; ++fy) {
726:       for (fx = 0; fx < numXEdges; ++fx) {
727:         for (fz = 0; fz < numZVertices; fz++) {
728:           PetscInt face    = firstZFace + (fy*numXEdges+fx)*numZVertices + fz;
729:           PetscInt edgeL   = firstYEdge + (fz*numXVertices+  fx                 )*numYEdges + fy;
730:           PetscInt edgeR   = firstYEdge + (fz*numXVertices+((fx+1)%numXVertices))*numYEdges + fy;
731:           PetscInt edgeB   = firstXEdge + (fz*numYVertices+  fy                 )*numXEdges + fx;
732:           PetscInt edgeT   = firstXEdge + (fz*numYVertices+((fy+1)%numYVertices))*numXEdges + fx;
733:           PetscInt ornt[4] = {0, 0, -2, -2};
734:           PetscInt cone[4];

736:           if (dim == 2) {
737:             if (bdX == DM_BOUNDARY_TWIST && fx == numXEdges-1) {edgeR += numYEdges-1-2*fy; ornt[1] = -2;}
738:             if (bdY == DM_BOUNDARY_TWIST && fy == numYEdges-1) {edgeT += numXEdges-1-2*fx; ornt[2] =  0;}
739:             if (bdX != DM_BOUNDARY_NONE && fx == numXEdges-1 && cutLabel) {DMLabelSetValue(cutLabel, face, 2);}
740:             if (bdY != DM_BOUNDARY_NONE && fy == numYEdges-1 && cutLabel) {DMLabelSetValue(cutLabel, face, 2);}
741:           } else {
742:             /* markers */
743:             if (bdZ != DM_BOUNDARY_PERIODIC) {
744:               if (fz == numZVertices-1) {
745:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerTop);
746:                 DMSetLabelValue(dm, "marker", face, markerTop);
747:               }
748:               else if (fz == 0) {
749:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerBottom);
750:                 DMSetLabelValue(dm, "marker", face, markerBottom);
751:               }
752:             }
753:           }
754:           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
755:           DMPlexSetCone(dm, face, cone);
756:           DMPlexSetConeOrientation(dm, face, ornt);
757:         }
758:       }
759:     }
760:     /* Build Z edges*/
761:     for (vy = 0; vy < numYVertices; vy++) {
762:       for (vx = 0; vx < numXVertices; vx++) {
763:         for (ez = 0; ez < numZEdges; ez++) {
764:           const PetscInt edge    = firstZEdge  + (vy*numXVertices+vx)*numZEdges + ez;
765:           const PetscInt vertexB = firstVertex + (  ez                 *numYVertices+vy)*numXVertices + vx;
766:           const PetscInt vertexT = firstVertex + (((ez+1)%numZVertices)*numYVertices+vy)*numXVertices + vx;
767:           PetscInt       cone[2];

769:           if (dim == 3) {
770:             if (bdX != DM_BOUNDARY_PERIODIC) {
771:               if (vx == numXVertices-1) {
772:                 DMSetLabelValue(dm, "marker", edge, markerRight);
773:               }
774:               else if (vx == 0) {
775:                 DMSetLabelValue(dm, "marker", edge, markerLeft);
776:               }
777:             }
778:             if (bdY != DM_BOUNDARY_PERIODIC) {
779:               if (vy == numYVertices-1) {
780:                 DMSetLabelValue(dm, "marker", edge, markerBack);
781:               }
782:               else if (vy == 0) {
783:                 DMSetLabelValue(dm, "marker", edge, markerFront);
784:               }
785:             }
786:           }
787:           cone[0] = vertexB; cone[1] = vertexT;
788:           DMPlexSetCone(dm, edge, cone);
789:         }
790:       }
791:     }
792:     /* Build Y edges*/
793:     for (vz = 0; vz < numZVertices; vz++) {
794:       for (vx = 0; vx < numXVertices; vx++) {
795:         for (ey = 0; ey < numYEdges; ey++) {
796:           const PetscInt nextv   = (dim == 2 && bdY == DM_BOUNDARY_TWIST && ey == numYEdges-1) ? (numXVertices-vx-1) : (vz*numYVertices+((ey+1)%numYVertices))*numXVertices + vx;
797:           const PetscInt edge    = firstYEdge  + (vz*numXVertices+vx)*numYEdges + ey;
798:           const PetscInt vertexF = firstVertex + (vz*numYVertices+ey)*numXVertices + vx;
799:           const PetscInt vertexK = firstVertex + nextv;
800:           PetscInt       cone[2];

802:           cone[0] = vertexF; cone[1] = vertexK;
803:           DMPlexSetCone(dm, edge, cone);
804:           if (dim == 2) {
805:             if ((bdX != DM_BOUNDARY_PERIODIC) && (bdX != DM_BOUNDARY_TWIST)) {
806:               if (vx == numXVertices-1) {
807:                 DMSetLabelValue(dm, "Face Sets", edge, faceMarkerRight);
808:                 DMSetLabelValue(dm, "marker", edge,    markerRight);
809:                 DMSetLabelValue(dm, "marker", cone[0], markerRight);
810:                 if (ey == numYEdges-1) {
811:                   DMSetLabelValue(dm, "marker", cone[1], markerRight);
812:                 }
813:               } else if (vx == 0) {
814:                 DMSetLabelValue(dm, "Face Sets", edge, faceMarkerLeft);
815:                 DMSetLabelValue(dm, "marker", edge,    markerLeft);
816:                 DMSetLabelValue(dm, "marker", cone[0], markerLeft);
817:                 if (ey == numYEdges-1) {
818:                   DMSetLabelValue(dm, "marker", cone[1], markerLeft);
819:                 }
820:               }
821:             } else {
822:               if (vx == 0 && cutLabel) {
823:                 DMLabelSetValue(cutLabel, edge,    1);
824:                 DMLabelSetValue(cutLabel, cone[0], 1);
825:                 if (ey == numYEdges-1) {
826:                   DMLabelSetValue(cutLabel, cone[1], 1);
827:                 }
828:               }
829:             }
830:           } else {
831:             if (bdX != DM_BOUNDARY_PERIODIC) {
832:               if (vx == numXVertices-1) {
833:                 DMSetLabelValue(dm, "marker", edge, markerRight);
834:               } else if (vx == 0) {
835:                 DMSetLabelValue(dm, "marker", edge, markerLeft);
836:               }
837:             }
838:             if (bdZ != DM_BOUNDARY_PERIODIC) {
839:               if (vz == numZVertices-1) {
840:                 DMSetLabelValue(dm, "marker", edge, markerTop);
841:               } else if (vz == 0) {
842:                 DMSetLabelValue(dm, "marker", edge, markerBottom);
843:               }
844:             }
845:           }
846:         }
847:       }
848:     }
849:     /* Build X edges*/
850:     for (vz = 0; vz < numZVertices; vz++) {
851:       for (vy = 0; vy < numYVertices; vy++) {
852:         for (ex = 0; ex < numXEdges; ex++) {
853:           const PetscInt nextv   = (dim == 2 && bdX == DM_BOUNDARY_TWIST && ex == numXEdges-1) ? (numYVertices-vy-1)*numXVertices : (vz*numYVertices+vy)*numXVertices + (ex+1)%numXVertices;
854:           const PetscInt edge    = firstXEdge  + (vz*numYVertices+vy)*numXEdges + ex;
855:           const PetscInt vertexL = firstVertex + (vz*numYVertices+vy)*numXVertices + ex;
856:           const PetscInt vertexR = firstVertex + nextv;
857:           PetscInt       cone[2];

859:           cone[0] = vertexL; cone[1] = vertexR;
860:           DMPlexSetCone(dm, edge, cone);
861:           if (dim == 2) {
862:             if ((bdY != DM_BOUNDARY_PERIODIC) && (bdY != DM_BOUNDARY_TWIST)) {
863:               if (vy == numYVertices-1) {
864:                 DMSetLabelValue(dm, "Face Sets", edge, faceMarkerTop);
865:                 DMSetLabelValue(dm, "marker", edge,    markerTop);
866:                 DMSetLabelValue(dm, "marker", cone[0], markerTop);
867:                 if (ex == numXEdges-1) {
868:                   DMSetLabelValue(dm, "marker", cone[1], markerTop);
869:                 }
870:               } else if (vy == 0) {
871:                 DMSetLabelValue(dm, "Face Sets", edge, faceMarkerBottom);
872:                 DMSetLabelValue(dm, "marker", edge,    markerBottom);
873:                 DMSetLabelValue(dm, "marker", cone[0], markerBottom);
874:                 if (ex == numXEdges-1) {
875:                   DMSetLabelValue(dm, "marker", cone[1], markerBottom);
876:                 }
877:               }
878:             } else {
879:               if (vy == 0 && cutLabel) {
880:                 DMLabelSetValue(cutLabel, edge,    1);
881:                 DMLabelSetValue(cutLabel, cone[0], 1);
882:                 if (ex == numXEdges-1) {
883:                   DMLabelSetValue(cutLabel, cone[1], 1);
884:                 }
885:               }
886:             }
887:           } else {
888:             if (bdY != DM_BOUNDARY_PERIODIC) {
889:               if (vy == numYVertices-1) {
890:                 DMSetLabelValue(dm, "marker", edge, markerBack);
891:               }
892:               else if (vy == 0) {
893:                 DMSetLabelValue(dm, "marker", edge, markerFront);
894:               }
895:             }
896:             if (bdZ != DM_BOUNDARY_PERIODIC) {
897:               if (vz == numZVertices-1) {
898:                 DMSetLabelValue(dm, "marker", edge, markerTop);
899:               }
900:               else if (vz == 0) {
901:                 DMSetLabelValue(dm, "marker", edge, markerBottom);
902:               }
903:             }
904:           }
905:         }
906:       }
907:     }
908:     DMPlexSymmetrize(dm);
909:     DMPlexStratify(dm);
910:     /* Build coordinates */
911:     DMGetCoordinateSection(dm, &coordSection);
912:     PetscSectionSetNumFields(coordSection, 1);
913:     PetscSectionSetFieldComponents(coordSection, 0, dim);
914:     PetscSectionSetChart(coordSection, firstVertex, firstVertex+numVertices);
915:     for (v = firstVertex; v < firstVertex+numVertices; ++v) {
916:       PetscSectionSetDof(coordSection, v, dim);
917:       PetscSectionSetFieldDof(coordSection, v, 0, dim);
918:     }
919:     PetscSectionSetUp(coordSection);
920:     PetscSectionGetStorageSize(coordSection, &coordSize);
921:     VecCreate(PETSC_COMM_SELF, &coordinates);
922:     PetscObjectSetName((PetscObject) coordinates, "coordinates");
923:     VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
924:     VecSetBlockSize(coordinates, dim);
925:     VecSetType(coordinates,VECSTANDARD);
926:     VecGetArray(coordinates, &coords);
927:     for (vz = 0; vz < numZVertices; ++vz) {
928:       for (vy = 0; vy < numYVertices; ++vy) {
929:         for (vx = 0; vx < numXVertices; ++vx) {
930:           coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+0] = lower[0] + ((upper[0] - lower[0])/numXEdges)*vx;
931:           coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+1] = lower[1] + ((upper[1] - lower[1])/numYEdges)*vy;
932:           if (dim == 3) {
933:             coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+2] = lower[2] + ((upper[2] - lower[2])/numZEdges)*vz;
934:           }
935:         }
936:       }
937:     }
938:     VecRestoreArray(coordinates, &coords);
939:     DMSetCoordinatesLocal(dm, coordinates);
940:     VecDestroy(&coordinates);
941:   }
942:   return(0);
943: }

945: static PetscErrorCode DMPlexCreateBoxMesh_Tensor_Internal(MPI_Comm comm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate, DM *dm)
946: {
947:   PetscInt       i;

952:   DMCreate(comm, dm);
954:   DMSetType(*dm, DMPLEX);
955:   DMSetDimension(*dm, dim);
956:   switch (dim) {
957:   case 2: {DMPlexCreateCubeMesh_Internal(*dm, lower, upper, faces, periodicity[0], periodicity[1], DM_BOUNDARY_NONE);break;}
958:   case 3: {DMPlexCreateCubeMesh_Internal(*dm, lower, upper, faces, periodicity[0], periodicity[1], periodicity[2]);break;}
959:   default: SETERRQ1(comm, PETSC_ERR_SUP, "Dimension not supported: %d", dim);
960:   }
961:   if (periodicity[0] == DM_BOUNDARY_PERIODIC || periodicity[0] == DM_BOUNDARY_TWIST ||
962:       periodicity[1] == DM_BOUNDARY_PERIODIC || periodicity[1] == DM_BOUNDARY_TWIST ||
963:       (dim > 2 && (periodicity[2] == DM_BOUNDARY_PERIODIC || periodicity[2] == DM_BOUNDARY_TWIST))) {
964:     PetscReal L[3];
965:     PetscReal maxCell[3];

967:     for (i = 0; i < dim; i++) {
968:       L[i]       = upper[i] - lower[i];
969:       maxCell[i] = 1.1 * (L[i] / PetscMax(1,faces[i]));
970:     }
971:     DMSetPeriodicity(*dm,PETSC_TRUE,maxCell,L,periodicity);
972:   }
973:   if (!interpolate) {
974:     DM udm;

976:     DMPlexUninterpolate(*dm, &udm);
977:     DMPlexCopyCoordinates(*dm, udm);
978:     DMDestroy(dm);
979:     *dm  = udm;
980:   }
981:   return(0);
982: }

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

987:   Collective

989:   Input Parameters:
990: + comm        - The communicator for the DM object
991: . dim         - The spatial dimension
992: . simplex     - PETSC_TRUE for simplices, PETSC_FALSE for tensor cells
993: . faces       - Number of faces per dimension, or NULL for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
994: . lower       - The lower left corner, or NULL for (0, 0, 0)
995: . upper       - The upper right corner, or NULL for (1, 1, 1)
996: . periodicity - The boundary type for the X,Y,Z direction, or NULL for DM_BOUNDARY_NONE
997: - interpolate - Flag to create intermediate mesh pieces (edges, faces)

999:   Output Parameter:
1000: . dm  - The DM object

1002:   Options Database Keys:
1003: + -dm_plex_box_lower <x,y,z> - Specify lower-left-bottom coordinates for the box
1004: - -dm_plex_box_upper <x,y,z> - Specify upper-right-top coordinates for the box

1006:   Note: Here is the numbering returned for 2 faces in each direction for tensor cells:
1007: $ 10---17---11---18----12
1008: $  |         |         |
1009: $  |         |         |
1010: $ 20    2   22    3    24
1011: $  |         |         |
1012: $  |         |         |
1013: $  7---15----8---16----9
1014: $  |         |         |
1015: $  |         |         |
1016: $ 19    0   21    1   23
1017: $  |         |         |
1018: $  |         |         |
1019: $  4---13----5---14----6

1021: and for simplicial cells

1023: $ 14----8---15----9----16
1024: $  |\     5  |\      7 |
1025: $  | \       | \       |
1026: $ 13   2    14    3    15
1027: $  | 4   \   | 6   \   |
1028: $  |       \ |       \ |
1029: $ 11----6---12----7----13
1030: $  |\        |\        |
1031: $  | \    1  | \     3 |
1032: $ 10   0    11    1    12
1033: $  | 0   \   | 2   \   |
1034: $  |       \ |       \ |
1035: $  8----4----9----5----10

1037:   Level: beginner

1039: .seealso: DMPlexCreateFromFile(), DMPlexCreateHexCylinderMesh(), DMSetType(), DMCreate()
1040: @*/
1041: PetscErrorCode DMPlexCreateBoxMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate, DM *dm)
1042: {
1043:   PetscInt       fac[3] = {0, 0, 0};
1044:   PetscReal      low[3] = {0, 0, 0};
1045:   PetscReal      upp[3] = {1, 1, 1};
1046:   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
1047:   PetscInt       i, n;
1048:   PetscBool      flg;

1052:   n    = 3;
1053:   PetscOptionsGetIntArray(NULL, NULL, "-dm_plex_box_faces", fac, &n, &flg);
1054:   for (i = 0; i < dim; ++i) fac[i] = faces ? faces[i] : (flg && i < n ? fac[i] : (dim == 1 ? 1 : 4-dim));
1055:   if (lower) for (i = 0; i < dim; ++i) low[i] = lower[i];
1056:   if (upper) for (i = 0; i < dim; ++i) upp[i] = upper[i];
1057:   if (periodicity) for (i = 0; i < dim; ++i) bdt[i] = periodicity[i];
1058:   /* Allow bounds to be specified from the command line */
1059:   n    = 3;
1060:   PetscOptionsGetRealArray(NULL, NULL, "-dm_plex_box_lower", low, &n, &flg);
1061:   if (flg && (n != dim)) SETERRQ2(comm, PETSC_ERR_ARG_SIZ, "Lower box point had %D values, should have been %D", n, dim);
1062:   n    = 3;
1063:   PetscOptionsGetRealArray(NULL, NULL, "-dm_plex_box_upper", upp, &n, &flg);
1064:   if (flg && (n != dim)) SETERRQ2(comm, PETSC_ERR_ARG_SIZ, "Upper box point had %D values, should have been %D", n, dim);

1066:   if (dim == 1)      {DMPlexCreateLineMesh_Internal(comm, fac[0], low[0], upp[0], bdt[0], dm);}
1067:   else if (simplex)  {DMPlexCreateBoxMesh_Simplex_Internal(comm, dim, fac, low, upp, bdt, interpolate, dm);}
1068:   else               {DMPlexCreateBoxMesh_Tensor_Internal(comm, dim, fac, low, upp, bdt, interpolate, dm);}
1069:   return(0);
1070: }

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

1075:   Collective

1077:   Input Parameters:
1078: + comm        - The communicator for the DM object
1079: . faces       - Number of faces per dimension, or NULL for (1, 1, 1)
1080: . lower       - The lower left corner, or NULL for (0, 0, 0)
1081: . upper       - The upper right corner, or NULL for (1, 1, 1)
1082: . periodicity - The boundary type for the X,Y,Z direction, or NULL for DM_BOUNDARY_NONE
1083: . ordExt      - If PETSC_TRUE, orders the extruded cells in the height first. Otherwise, orders the cell on the layers first
1084: - interpolate - Flag to create intermediate mesh pieces (edges, faces)

1086:   Output Parameter:
1087: . dm  - The DM object

1089:   Level: beginner

1091: .seealso: DMPlexCreateHexCylinderMesh(), DMPlexCreateWedgeCylinderMesh(), DMPlexExtrude(), DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
1092: @*/
1093: PetscErrorCode DMPlexCreateWedgeBoxMesh(MPI_Comm comm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool ordExt, PetscBool interpolate, DM *dm)
1094: {
1095:   DM             bdm, botdm;
1096:   PetscInt       i;
1097:   PetscInt       fac[3] = {0, 0, 0};
1098:   PetscReal      low[3] = {0, 0, 0};
1099:   PetscReal      upp[3] = {1, 1, 1};
1100:   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};

1104:   for (i = 0; i < 3; ++i) fac[i] = faces ? (faces[i] > 0 ? faces[i] : 1) : 1;
1105:   if (lower) for (i = 0; i < 3; ++i) low[i] = lower[i];
1106:   if (upper) for (i = 0; i < 3; ++i) upp[i] = upper[i];
1107:   if (periodicity) for (i = 0; i < 3; ++i) bdt[i] = periodicity[i];
1108:   for (i = 0; i < 3; ++i) if (bdt[i] != DM_BOUNDARY_NONE) SETERRQ(comm, PETSC_ERR_SUP, "Periodicity not yet supported");

1110:   DMCreate(comm, &bdm);
1111:   DMSetType(bdm, DMPLEX);
1112:   DMSetDimension(bdm, 1);
1113:   DMSetCoordinateDim(bdm, 2);
1114:   DMPlexCreateSquareBoundary(bdm, low, upp, fac);
1115:   DMPlexGenerate(bdm, NULL, PETSC_FALSE, &botdm);
1116:   DMDestroy(&bdm);
1117:   DMPlexExtrude(botdm, fac[2], upp[2] - low[2], ordExt, interpolate, dm);
1118:   if (low[2] != 0.0) {
1119:     Vec         v;
1120:     PetscScalar *x;
1121:     PetscInt    cDim, n;

1123:     DMGetCoordinatesLocal(*dm, &v);
1124:     VecGetBlockSize(v, &cDim);
1125:     VecGetLocalSize(v, &n);
1126:     VecGetArray(v, &x);
1127:     x   += cDim;
1128:     for (i=0; i<n; i+=cDim) x[i] += low[2];
1129:     VecRestoreArray(v,&x);
1130:     DMSetCoordinatesLocal(*dm, v);
1131:   }
1132:   DMDestroy(&botdm);
1133:   return(0);
1134: }

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

1139:   Collective on idm

1141:   Input Parameters:
1142: + idm - The mesh to be extruted
1143: . layers - The number of layers
1144: . height - The height of the extruded layer
1145: . ordExt - If PETSC_TRUE, orders the extruded cells in the height first. Otherwise, orders the cell on the layers first
1146: - interpolate - Flag to create intermediate mesh pieces (edges, faces)

1148:   Output Parameter:
1149: . dm  - The DM object

1151:   Notes: The object created is an hybrid mesh, the vertex ordering in the cone of the cell is that of the prismatic cells

1153:   Level: advanced

1155: .seealso: DMPlexCreateWedgeCylinderMesh(), DMPlexCreateWedgeBoxMesh(), DMPlexSetHybridBounds(), DMSetType(), DMCreate()
1156: @*/
1157: PetscErrorCode DMPlexExtrude(DM idm, PetscInt layers, PetscReal height, PetscBool ordExt, PetscBool interpolate, DM* dm)
1158: {
1159:   PetscScalar       *coordsB;
1160:   const PetscScalar *coordsA;
1161:   PetscReal         *normals = NULL;
1162:   Vec               coordinatesA, coordinatesB;
1163:   PetscSection      coordSectionA, coordSectionB;
1164:   PetscInt          dim, cDim, cDimB, c, l, v, coordSize, *newCone;
1165:   PetscInt          cStart, cEnd, vStart, vEnd, cellV, numCells, numVertices;
1166:   PetscErrorCode    ierr;

1173:   DMGetDimension(idm, &dim);
1174:   if (dim < 1 || dim > 3) SETERRQ1(PetscObjectComm((PetscObject)idm), PETSC_ERR_SUP, "Support for dimension %D not coded", dim);

1176:   DMPlexGetHeightStratum(idm, 0, &cStart, &cEnd);
1177:   DMPlexGetDepthStratum(idm, 0, &vStart, &vEnd);
1178:   numCells = (cEnd - cStart)*layers;
1179:   numVertices = (vEnd - vStart)*(layers+1);
1180:   DMCreate(PetscObjectComm((PetscObject)idm), dm);
1181:   DMSetType(*dm, DMPLEX);
1182:   DMSetDimension(*dm, dim+1);
1183:   DMPlexSetChart(*dm, 0, numCells+numVertices);
1184:   for (c = cStart, cellV = 0; c < cEnd; ++c) {
1185:     PetscInt *closure = NULL;
1186:     PetscInt closureSize, numCorners = 0;

1188:     DMPlexGetTransitiveClosure(idm, c, PETSC_TRUE, &closureSize, &closure);
1189:     for (v = 0; v < closureSize*2; v += 2) if ((closure[v] >= vStart) && (closure[v] < vEnd)) numCorners++;
1190:     DMPlexRestoreTransitiveClosure(idm, c, PETSC_TRUE, &closureSize, &closure);
1191:     for (l = 0; l < layers; ++l) {
1192:       DMPlexSetConeSize(*dm, ordExt ? layers*(c - cStart) + l : l*(cEnd - cStart) + c - cStart, 2*numCorners);
1193:     }
1194:     cellV = PetscMax(numCorners,cellV);
1195:   }
1196:   DMPlexSetHybridBounds(*dm, 0, PETSC_DETERMINE, PETSC_DETERMINE, PETSC_DETERMINE);
1197:   DMSetUp(*dm);

1199:   DMGetCoordinateDim(idm, &cDim);
1200:   if (dim != cDim) {
1201:     PetscCalloc1(cDim*(vEnd - vStart), &normals);
1202:   }
1203:   PetscMalloc1(3*cellV,&newCone);
1204:   for (c = cStart; c < cEnd; ++c) {
1205:     PetscInt *closure = NULL;
1206:     PetscInt closureSize, numCorners = 0, l;
1207:     PetscReal normal[3] = {0, 0, 0};

1209:     if (normals) {
1210:       DMPlexComputeCellGeometryFVM(idm, c, NULL, NULL, normal);
1211:     }
1212:     DMPlexGetTransitiveClosure(idm, c, PETSC_TRUE, &closureSize, &closure);
1213:     for (v = 0; v < closureSize*2; v += 2) {
1214:       if ((closure[v] >= vStart) && (closure[v] < vEnd)) {
1215:         PetscInt d;

1217:         newCone[numCorners++] = closure[v] - vStart;
1218:         if (normals) { for (d = 0; d < cDim; ++d) normals[cDim*(closure[v]-vStart)+d] += normal[d]; }
1219:       }
1220:     }
1221:     switch (numCorners) {
1222:     case 4: /* do nothing */
1223:     case 2: /* do nothing */
1224:       break;
1225:     case 3: /* from counter-clockwise to wedge ordering */
1226:       l = newCone[1];
1227:       newCone[1] = newCone[2];
1228:       newCone[2] = l;
1229:       break;
1230:     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unsupported number of corners: %D", numCorners);
1231:     }
1232:     DMPlexRestoreTransitiveClosure(idm, c, PETSC_TRUE, &closureSize, &closure);
1233:     for (l = 0; l < layers; ++l) {
1234:       PetscInt i;

1236:       for (i = 0; i < numCorners; ++i) {
1237:         newCone[  numCorners + i] = ordExt ? (layers+1)*newCone[i] + l     + numCells :     l*(vEnd - vStart) + newCone[i] + numCells;
1238:         newCone[2*numCorners + i] = ordExt ? (layers+1)*newCone[i] + l + 1 + numCells : (l+1)*(vEnd - vStart) + newCone[i] + numCells;
1239:       }
1240:       DMPlexSetCone(*dm, ordExt ? layers*(c - cStart) + l : l*(cEnd - cStart) + c - cStart, newCone + numCorners);
1241:     }
1242:   }
1243:   DMPlexSymmetrize(*dm);
1244:   DMPlexStratify(*dm);
1245:   PetscFree(newCone);

1247:   cDimB = cDim == dim ? cDim+1 : cDim;
1248:   DMGetCoordinateSection(*dm, &coordSectionB);
1249:   PetscSectionSetNumFields(coordSectionB, 1);
1250:   PetscSectionSetFieldComponents(coordSectionB, 0, cDimB);
1251:   PetscSectionSetChart(coordSectionB, numCells, numCells+numVertices);
1252:   for (v = numCells; v < numCells+numVertices; ++v) {
1253:     PetscSectionSetDof(coordSectionB, v, cDimB);
1254:     PetscSectionSetFieldDof(coordSectionB, v, 0, cDimB);
1255:   }
1256:   PetscSectionSetUp(coordSectionB);
1257:   PetscSectionGetStorageSize(coordSectionB, &coordSize);
1258:   VecCreate(PETSC_COMM_SELF, &coordinatesB);
1259:   PetscObjectSetName((PetscObject) coordinatesB, "coordinates");
1260:   VecSetSizes(coordinatesB, coordSize, PETSC_DETERMINE);
1261:   VecSetBlockSize(coordinatesB, cDimB);
1262:   VecSetType(coordinatesB,VECSTANDARD);

1264:   DMGetCoordinateSection(idm, &coordSectionA);
1265:   DMGetCoordinatesLocal(idm, &coordinatesA);
1266:   VecGetArray(coordinatesB, &coordsB);
1267:   VecGetArrayRead(coordinatesA, &coordsA);
1268:   for (v = vStart; v < vEnd; ++v) {
1269:     const PetscScalar *cptr;
1270:     PetscReal         ones2[2] = { 0., 1.}, ones3[3] = { 0., 0., 1.};
1271:     PetscReal         *normal, norm, h = height/layers;
1272:     PetscInt          offA, d, cDimA = cDim;

1274:     normal = normals ? normals + cDimB*(v - vStart) : (cDim > 1 ? ones3 : ones2);
1275:     if (normals) {
1276:       for (d = 0, norm = 0.0; d < cDimB; ++d) norm += normal[d]*normal[d];
1277:       for (d = 0; d < cDimB; ++d) normal[d] *= 1./PetscSqrtReal(norm);
1278:     }

1280:     PetscSectionGetOffset(coordSectionA, v, &offA);
1281:     cptr = coordsA + offA;
1282:     for (l = 0; l < layers+1; ++l) {
1283:       PetscInt offB, d, newV;

1285:       newV = ordExt ? (layers+1)*(v -vStart) + l + numCells : (vEnd -vStart)*l + (v -vStart) + numCells;
1286:       PetscSectionGetOffset(coordSectionB, newV, &offB);
1287:       for (d = 0; d < cDimA; ++d) { coordsB[offB+d]  = cptr[d]; }
1288:       for (d = 0; d < cDimB; ++d) { coordsB[offB+d] += l ? normal[d]*h : 0.0; }
1289:       cptr    = coordsB + offB;
1290:       cDimA   = cDimB;
1291:     }
1292:   }
1293:   VecRestoreArrayRead(coordinatesA, &coordsA);
1294:   VecRestoreArray(coordinatesB, &coordsB);
1295:   DMSetCoordinatesLocal(*dm, coordinatesB);
1296:   VecDestroy(&coordinatesB);
1297:   PetscFree(normals);
1298:   if (interpolate) {
1299:     DM idm;

1301:     DMPlexInterpolate(*dm, &idm);
1302:     DMPlexCopyCoordinates(*dm, idm);
1303:     DMDestroy(dm);
1304:     *dm  = idm;
1305:   }
1306:   return(0);
1307: }

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

1312:   Logically Collective on dm

1314:   Input Parameters:
1315: + dm - the DM context
1316: - prefix - the prefix to prepend to all option names

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

1322:   Level: advanced

1324: .seealso: SNESSetFromOptions()
1325: @*/
1326: PetscErrorCode DMPlexSetOptionsPrefix(DM dm, const char prefix[])
1327: {
1328:   DM_Plex       *mesh = (DM_Plex *) dm->data;

1333:   PetscObjectSetOptionsPrefix((PetscObject) dm, prefix);
1334:   PetscObjectSetOptionsPrefix((PetscObject) mesh->partitioner, prefix);
1335:   return(0);
1336: }

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

1341:   Collective

1343:   Input Parameters:
1344: + comm      - The communicator for the DM object
1345: . numRefine - The number of regular refinements to the basic 5 cell structure
1346: - periodicZ - The boundary type for the Z direction

1348:   Output Parameter:
1349: . dm  - The DM object

1351:   Note: Here is the output numbering looking from the bottom of the cylinder:
1352: $       17-----14
1353: $        |     |
1354: $        |  2  |
1355: $        |     |
1356: $ 17-----8-----7-----14
1357: $  |     |     |     |
1358: $  |  3  |  0  |  1  |
1359: $  |     |     |     |
1360: $ 19-----5-----6-----13
1361: $        |     |
1362: $        |  4  |
1363: $        |     |
1364: $       19-----13
1365: $
1366: $ and up through the top
1367: $
1368: $       18-----16
1369: $        |     |
1370: $        |  2  |
1371: $        |     |
1372: $ 18----10----11-----16
1373: $  |     |     |     |
1374: $  |  3  |  0  |  1  |
1375: $  |     |     |     |
1376: $ 20-----9----12-----15
1377: $        |     |
1378: $        |  4  |
1379: $        |     |
1380: $       20-----15

1382:   Level: beginner

1384: .seealso: DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
1385: @*/
1386: PetscErrorCode DMPlexCreateHexCylinderMesh(MPI_Comm comm, PetscInt numRefine, DMBoundaryType periodicZ, DM *dm)
1387: {
1388:   const PetscInt dim = 3;
1389:   PetscInt       numCells, numVertices, r;
1390:   PetscMPIInt    rank;

1395:   MPI_Comm_rank(comm, &rank);
1396:   if (numRefine < 0) SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Number of refinements %D cannot be negative", numRefine);
1397:   DMCreate(comm, dm);
1398:   DMSetType(*dm, DMPLEX);
1399:   DMSetDimension(*dm, dim);
1400:   /* Create topology */
1401:   {
1402:     PetscInt cone[8], c;

1404:     numCells    = !rank ?  5 : 0;
1405:     numVertices = !rank ? 16 : 0;
1406:     if (periodicZ == DM_BOUNDARY_PERIODIC) {
1407:       numCells   *= 3;
1408:       numVertices = !rank ? 24 : 0;
1409:     }
1410:     DMPlexSetChart(*dm, 0, numCells+numVertices);
1411:     for (c = 0; c < numCells; c++) {DMPlexSetConeSize(*dm, c, 8);}
1412:     DMSetUp(*dm);
1413:     if (!rank) {
1414:       if (periodicZ == DM_BOUNDARY_PERIODIC) {
1415:         cone[0] = 15; cone[1] = 18; cone[2] = 17; cone[3] = 16;
1416:         cone[4] = 31; cone[5] = 32; cone[6] = 33; cone[7] = 34;
1417:         DMPlexSetCone(*dm, 0, cone);
1418:         cone[0] = 16; cone[1] = 17; cone[2] = 24; cone[3] = 23;
1419:         cone[4] = 32; cone[5] = 36; cone[6] = 37; cone[7] = 33; /* 22 25 26 21 */
1420:         DMPlexSetCone(*dm, 1, cone);
1421:         cone[0] = 18; cone[1] = 27; cone[2] = 24; cone[3] = 17;
1422:         cone[4] = 34; cone[5] = 33; cone[6] = 37; cone[7] = 38;
1423:         DMPlexSetCone(*dm, 2, cone);
1424:         cone[0] = 29; cone[1] = 27; cone[2] = 18; cone[3] = 15;
1425:         cone[4] = 35; cone[5] = 31; cone[6] = 34; cone[7] = 38;
1426:         DMPlexSetCone(*dm, 3, cone);
1427:         cone[0] = 29; cone[1] = 15; cone[2] = 16; cone[3] = 23;
1428:         cone[4] = 35; cone[5] = 36; cone[6] = 32; cone[7] = 31;
1429:         DMPlexSetCone(*dm, 4, cone);

1431:         cone[0] = 31; cone[1] = 34; cone[2] = 33; cone[3] = 32;
1432:         cone[4] = 19; cone[5] = 22; cone[6] = 21; cone[7] = 20;
1433:         DMPlexSetCone(*dm, 5, cone);
1434:         cone[0] = 32; cone[1] = 33; cone[2] = 37; cone[3] = 36;
1435:         cone[4] = 22; cone[5] = 25; cone[6] = 26; cone[7] = 21;
1436:         DMPlexSetCone(*dm, 6, cone);
1437:         cone[0] = 34; cone[1] = 38; cone[2] = 37; cone[3] = 33;
1438:         cone[4] = 20; cone[5] = 21; cone[6] = 26; cone[7] = 28;
1439:         DMPlexSetCone(*dm, 7, cone);
1440:         cone[0] = 35; cone[1] = 38; cone[2] = 34; cone[3] = 31;
1441:         cone[4] = 30; cone[5] = 19; cone[6] = 20; cone[7] = 28;
1442:         DMPlexSetCone(*dm, 8, cone);
1443:         cone[0] = 35; cone[1] = 31; cone[2] = 32; cone[3] = 36;
1444:         cone[4] = 30; cone[5] = 25; cone[6] = 22; cone[7] = 19;
1445:         DMPlexSetCone(*dm, 9, cone);

1447:         cone[0] = 19; cone[1] = 20; cone[2] = 21; cone[3] = 22;
1448:         cone[4] = 15; cone[5] = 16; cone[6] = 17; cone[7] = 18;
1449:         DMPlexSetCone(*dm, 10, cone);
1450:         cone[0] = 22; cone[1] = 21; cone[2] = 26; cone[3] = 25;
1451:         cone[4] = 16; cone[5] = 23; cone[6] = 24; cone[7] = 17;
1452:         DMPlexSetCone(*dm, 11, cone);
1453:         cone[0] = 20; cone[1] = 28; cone[2] = 26; cone[3] = 21;
1454:         cone[4] = 18; cone[5] = 17; cone[6] = 24; cone[7] = 27;
1455:         DMPlexSetCone(*dm, 12, cone);
1456:         cone[0] = 30; cone[1] = 28; cone[2] = 20; cone[3] = 19;
1457:         cone[4] = 29; cone[5] = 15; cone[6] = 18; cone[7] = 27;
1458:         DMPlexSetCone(*dm, 13, cone);
1459:         cone[0] = 30; cone[1] = 19; cone[2] = 22; cone[3] = 25;
1460:         cone[4] = 29; cone[5] = 23; cone[6] = 16; cone[7] = 15;
1461:         DMPlexSetCone(*dm, 14, cone);
1462:       } else {
1463:         cone[0] =  5; cone[1] =  8; cone[2] =  7; cone[3] =  6;
1464:         cone[4] =  9; cone[5] = 12; cone[6] = 11; cone[7] = 10;
1465:         DMPlexSetCone(*dm, 0, cone);
1466:         cone[0] =  6; cone[1] =  7; cone[2] = 14; cone[3] = 13;
1467:         cone[4] = 12; cone[5] = 15; cone[6] = 16; cone[7] = 11;
1468:         DMPlexSetCone(*dm, 1, cone);
1469:         cone[0] =  8; cone[1] = 17; cone[2] = 14; cone[3] =  7;
1470:         cone[4] = 10; cone[5] = 11; cone[6] = 16; cone[7] = 18;
1471:         DMPlexSetCone(*dm, 2, cone);
1472:         cone[0] = 19; cone[1] = 17; cone[2] =  8; cone[3] =  5;
1473:         cone[4] = 20; cone[5] =  9; cone[6] = 10; cone[7] = 18;
1474:         DMPlexSetCone(*dm, 3, cone);
1475:         cone[0] = 19; cone[1] =  5; cone[2] =  6; cone[3] = 13;
1476:         cone[4] = 20; cone[5] = 15; cone[6] = 12; cone[7] =  9;
1477:         DMPlexSetCone(*dm, 4, cone);
1478:       }
1479:     }
1480:     DMPlexSymmetrize(*dm);
1481:     DMPlexStratify(*dm);
1482:   }
1483:   /* Interpolate */
1484:   {
1485:     DM idm;

1487:     DMPlexInterpolate(*dm, &idm);
1488:     DMDestroy(dm);
1489:     *dm  = idm;
1490:   }
1491:   /* Create cube geometry */
1492:   {
1493:     Vec             coordinates;
1494:     PetscSection    coordSection;
1495:     PetscScalar    *coords;
1496:     PetscInt        coordSize, v;
1497:     const PetscReal dis = 1.0/PetscSqrtReal(2.0);
1498:     const PetscReal ds2 = dis/2.0;

1500:     /* Build coordinates */
1501:     DMGetCoordinateSection(*dm, &coordSection);
1502:     PetscSectionSetNumFields(coordSection, 1);
1503:     PetscSectionSetFieldComponents(coordSection, 0, dim);
1504:     PetscSectionSetChart(coordSection, numCells, numCells+numVertices);
1505:     for (v = numCells; v < numCells+numVertices; ++v) {
1506:       PetscSectionSetDof(coordSection, v, dim);
1507:       PetscSectionSetFieldDof(coordSection, v, 0, dim);
1508:     }
1509:     PetscSectionSetUp(coordSection);
1510:     PetscSectionGetStorageSize(coordSection, &coordSize);
1511:     VecCreate(PETSC_COMM_SELF, &coordinates);
1512:     PetscObjectSetName((PetscObject) coordinates, "coordinates");
1513:     VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
1514:     VecSetBlockSize(coordinates, dim);
1515:     VecSetType(coordinates,VECSTANDARD);
1516:     VecGetArray(coordinates, &coords);
1517:     if (!rank) {
1518:       coords[0*dim+0] = -ds2; coords[0*dim+1] = -ds2; coords[0*dim+2] = 0.0;
1519:       coords[1*dim+0] =  ds2; coords[1*dim+1] = -ds2; coords[1*dim+2] = 0.0;
1520:       coords[2*dim+0] =  ds2; coords[2*dim+1] =  ds2; coords[2*dim+2] = 0.0;
1521:       coords[3*dim+0] = -ds2; coords[3*dim+1] =  ds2; coords[3*dim+2] = 0.0;
1522:       coords[4*dim+0] = -ds2; coords[4*dim+1] = -ds2; coords[4*dim+2] = 1.0;
1523:       coords[5*dim+0] = -ds2; coords[5*dim+1] =  ds2; coords[5*dim+2] = 1.0;
1524:       coords[6*dim+0] =  ds2; coords[6*dim+1] =  ds2; coords[6*dim+2] = 1.0;
1525:       coords[7*dim+0] =  ds2; coords[7*dim+1] = -ds2; coords[7*dim+2] = 1.0;
1526:       coords[ 8*dim+0] =  dis; coords[ 8*dim+1] = -dis; coords[ 8*dim+2] = 0.0;
1527:       coords[ 9*dim+0] =  dis; coords[ 9*dim+1] =  dis; coords[ 9*dim+2] = 0.0;
1528:       coords[10*dim+0] =  dis; coords[10*dim+1] = -dis; coords[10*dim+2] = 1.0;
1529:       coords[11*dim+0] =  dis; coords[11*dim+1] =  dis; coords[11*dim+2] = 1.0;
1530:       coords[12*dim+0] = -dis; coords[12*dim+1] =  dis; coords[12*dim+2] = 0.0;
1531:       coords[13*dim+0] = -dis; coords[13*dim+1] =  dis; coords[13*dim+2] = 1.0;
1532:       coords[14*dim+0] = -dis; coords[14*dim+1] = -dis; coords[14*dim+2] = 0.0;
1533:       coords[15*dim+0] = -dis; coords[15*dim+1] = -dis; coords[15*dim+2] = 1.0;
1534:       if (periodicZ == DM_BOUNDARY_PERIODIC) {
1535:         /* 15 31 19 */ coords[16*dim+0] = -ds2; coords[16*dim+1] = -ds2; coords[16*dim+2] = 0.5;
1536:         /* 16 32 22 */ coords[17*dim+0] =  ds2; coords[17*dim+1] = -ds2; coords[17*dim+2] = 0.5;
1537:         /* 17 33 21 */ coords[18*dim+0] =  ds2; coords[18*dim+1] =  ds2; coords[18*dim+2] = 0.5;
1538:         /* 18 34 20 */ coords[19*dim+0] = -ds2; coords[19*dim+1] =  ds2; coords[19*dim+2] = 0.5;
1539:         /* 29 35 30 */ coords[20*dim+0] = -dis; coords[20*dim+1] = -dis; coords[20*dim+2] = 0.5;
1540:         /* 23 36 25 */ coords[21*dim+0] =  dis; coords[21*dim+1] = -dis; coords[21*dim+2] = 0.5;
1541:         /* 24 37 26 */ coords[22*dim+0] =  dis; coords[22*dim+1] =  dis; coords[22*dim+2] = 0.5;
1542:         /* 27 38 28 */ coords[23*dim+0] = -dis; coords[23*dim+1] =  dis; coords[23*dim+2] = 0.5;
1543:       }
1544:     }
1545:     VecRestoreArray(coordinates, &coords);
1546:     DMSetCoordinatesLocal(*dm, coordinates);
1547:     VecDestroy(&coordinates);
1548:   }
1549:   /* Create periodicity */
1550:   if (periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
1551:     PetscReal      L[3];
1552:     PetscReal      maxCell[3];
1553:     DMBoundaryType bdType[3];
1554:     PetscReal      lower[3] = {0.0, 0.0, 0.0};
1555:     PetscReal      upper[3] = {1.0, 1.0, 1.5};
1556:     PetscInt       i, numZCells = 3;

1558:     bdType[0] = DM_BOUNDARY_NONE;
1559:     bdType[1] = DM_BOUNDARY_NONE;
1560:     bdType[2] = periodicZ;
1561:     for (i = 0; i < dim; i++) {
1562:       L[i]       = upper[i] - lower[i];
1563:       maxCell[i] = 1.1 * (L[i] / numZCells);
1564:     }
1565:     DMSetPeriodicity(*dm, PETSC_TRUE, maxCell, L, bdType);
1566:   }
1567:   /* Refine topology */
1568:   for (r = 0; r < numRefine; ++r) {
1569:     DM rdm = NULL;

1571:     DMRefine(*dm, comm, &rdm);
1572:     DMDestroy(dm);
1573:     *dm  = rdm;
1574:   }
1575:   /* Remap geometry to cylinder
1576:        Interior square: Linear interpolation is correct
1577:        The other cells all have vertices on rays from the origin. We want to uniformly expand the spacing
1578:        such that the last vertex is on the unit circle. So the closest and farthest vertices are at distance

1580:          phi     = arctan(y/x)
1581:          d_close = sqrt(1/8 + 1/4 sin^2(phi))
1582:          d_far   = sqrt(1/2 + sin^2(phi))

1584:        so we remap them using

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

1589:        If pi/4 < phi < 3pi/4 or -3pi/4 < phi < -pi/4, then we switch x and y.
1590:   */
1591:   {
1592:     Vec           coordinates;
1593:     PetscSection  coordSection;
1594:     PetscScalar  *coords;
1595:     PetscInt      vStart, vEnd, v;
1596:     const PetscReal dis = 1.0/PetscSqrtReal(2.0);
1597:     const PetscReal ds2 = 0.5*dis;

1599:     DMPlexGetDepthStratum(*dm, 0, &vStart, &vEnd);
1600:     DMGetCoordinateSection(*dm, &coordSection);
1601:     DMGetCoordinatesLocal(*dm, &coordinates);
1602:     VecGetArray(coordinates, &coords);
1603:     for (v = vStart; v < vEnd; ++v) {
1604:       PetscReal phi, sinp, cosp, dc, df, x, y, xc, yc;
1605:       PetscInt  off;

1607:       PetscSectionGetOffset(coordSection, v, &off);
1608:       if ((PetscAbsScalar(coords[off+0]) <= ds2) && (PetscAbsScalar(coords[off+1]) <= ds2)) continue;
1609:       x    = PetscRealPart(coords[off]);
1610:       y    = PetscRealPart(coords[off+1]);
1611:       phi  = PetscAtan2Real(y, x);
1612:       sinp = PetscSinReal(phi);
1613:       cosp = PetscCosReal(phi);
1614:       if ((PetscAbsReal(phi) > PETSC_PI/4.0) && (PetscAbsReal(phi) < 3.0*PETSC_PI/4.0)) {
1615:         dc = PetscAbsReal(ds2/sinp);
1616:         df = PetscAbsReal(dis/sinp);
1617:         xc = ds2*x/PetscAbsReal(y);
1618:         yc = ds2*PetscSignReal(y);
1619:       } else {
1620:         dc = PetscAbsReal(ds2/cosp);
1621:         df = PetscAbsReal(dis/cosp);
1622:         xc = ds2*PetscSignReal(x);
1623:         yc = ds2*y/PetscAbsReal(x);
1624:       }
1625:       coords[off+0] = xc + (coords[off+0] - xc)*(1.0 - dc)/(df - dc);
1626:       coords[off+1] = yc + (coords[off+1] - yc)*(1.0 - dc)/(df - dc);
1627:     }
1628:     VecRestoreArray(coordinates, &coords);
1629:     if (periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
1630:       DMLocalizeCoordinates(*dm);
1631:     }
1632:   }
1633:   return(0);
1634: }

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

1639:   Collective

1641:   Input Parameters:
1642: + comm - The communicator for the DM object
1643: . n    - The number of wedges around the origin
1644: - interpolate - Create edges and faces

1646:   Output Parameter:
1647: . dm  - The DM object

1649:   Level: beginner

1651: .seealso: DMPlexCreateHexCylinderMesh(), DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
1652: @*/
1653: PetscErrorCode DMPlexCreateWedgeCylinderMesh(MPI_Comm comm, PetscInt n, PetscBool interpolate, DM *dm)
1654: {
1655:   const PetscInt dim = 3;
1656:   PetscInt       numCells, numVertices;
1657:   PetscMPIInt    rank;

1662:   MPI_Comm_rank(comm, &rank);
1663:   if (n < 0) SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Number of wedges %D cannot be negative", n);
1664:   DMCreate(comm, dm);
1665:   DMSetType(*dm, DMPLEX);
1666:   DMSetDimension(*dm, dim);
1667:   /* Create topology */
1668:   {
1669:     PetscInt cone[6], c;

1671:     numCells    = !rank ?        n : 0;
1672:     numVertices = !rank ?  2*(n+1) : 0;
1673:     DMPlexSetChart(*dm, 0, numCells+numVertices);
1674:     DMPlexSetHybridBounds(*dm, 0, PETSC_DETERMINE, PETSC_DETERMINE, PETSC_DETERMINE);
1675:     for (c = 0; c < numCells; c++) {DMPlexSetConeSize(*dm, c, 6);}
1676:     DMSetUp(*dm);
1677:     for (c = 0; c < numCells; c++) {
1678:       cone[0] =  c+n*1; cone[1] = (c+1)%n+n*1; cone[2] = 0+3*n;
1679:       cone[3] =  c+n*2; cone[4] = (c+1)%n+n*2; cone[5] = 1+3*n;
1680:       DMPlexSetCone(*dm, c, cone);
1681:     }
1682:     DMPlexSymmetrize(*dm);
1683:     DMPlexStratify(*dm);
1684:   }
1685:   /* Interpolate */
1686:   if (interpolate) {
1687:     DM idm;

1689:     DMPlexInterpolate(*dm, &idm);
1690:     DMDestroy(dm);
1691:     *dm  = idm;
1692:   }
1693:   /* Create cylinder geometry */
1694:   {
1695:     Vec          coordinates;
1696:     PetscSection coordSection;
1697:     PetscScalar *coords;
1698:     PetscInt     coordSize, v, c;

1700:     /* Build coordinates */
1701:     DMGetCoordinateSection(*dm, &coordSection);
1702:     PetscSectionSetNumFields(coordSection, 1);
1703:     PetscSectionSetFieldComponents(coordSection, 0, dim);
1704:     PetscSectionSetChart(coordSection, numCells, numCells+numVertices);
1705:     for (v = numCells; v < numCells+numVertices; ++v) {
1706:       PetscSectionSetDof(coordSection, v, dim);
1707:       PetscSectionSetFieldDof(coordSection, v, 0, dim);
1708:     }
1709:     PetscSectionSetUp(coordSection);
1710:     PetscSectionGetStorageSize(coordSection, &coordSize);
1711:     VecCreate(PETSC_COMM_SELF, &coordinates);
1712:     PetscObjectSetName((PetscObject) coordinates, "coordinates");
1713:     VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
1714:     VecSetBlockSize(coordinates, dim);
1715:     VecSetType(coordinates,VECSTANDARD);
1716:     VecGetArray(coordinates, &coords);
1717:     for (c = 0; c < numCells; c++) {
1718:       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;
1719:       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;
1720:     }
1721:     if (!rank) {
1722:       coords[(2*n+0)*dim+0] = 0.0; coords[(2*n+0)*dim+1] = 0.0; coords[(2*n+0)*dim+2] = 1.0;
1723:       coords[(2*n+1)*dim+0] = 0.0; coords[(2*n+1)*dim+1] = 0.0; coords[(2*n+1)*dim+2] = 0.0;
1724:     }
1725:     VecRestoreArray(coordinates, &coords);
1726:     DMSetCoordinatesLocal(*dm, coordinates);
1727:     VecDestroy(&coordinates);
1728:   }
1729:   return(0);
1730: }

1732: PETSC_STATIC_INLINE PetscReal DiffNormReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
1733: {
1734:   PetscReal prod = 0.0;
1735:   PetscInt  i;
1736:   for (i = 0; i < dim; ++i) prod += PetscSqr(x[i] - y[i]);
1737:   return PetscSqrtReal(prod);
1738: }
1739: PETSC_STATIC_INLINE PetscReal DotReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
1740: {
1741:   PetscReal prod = 0.0;
1742:   PetscInt  i;
1743:   for (i = 0; i < dim; ++i) prod += x[i]*y[i];
1744:   return prod;
1745: }

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

1750:   Collective

1752:   Input Parameters:
1753: + comm  - The communicator for the DM object
1754: . dim   - The dimension
1755: - simplex - Use simplices, or tensor product cells

1757:   Output Parameter:
1758: . dm  - The DM object

1760:   Level: beginner

1762: .seealso: DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
1763: @*/
1764: PetscErrorCode DMPlexCreateSphereMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, DM *dm)
1765: {
1766:   const PetscInt  embedDim = dim+1;
1767:   PetscSection    coordSection;
1768:   Vec             coordinates;
1769:   PetscScalar    *coords;
1770:   PetscReal      *coordsIn;
1771:   PetscInt        numCells, numEdges, numVerts, firstVertex, v, firstEdge, coordSize, d, c, e;
1772:   PetscMPIInt     rank;
1773:   PetscErrorCode  ierr;

1777:   DMCreate(comm, dm);
1778:   DMSetType(*dm, DMPLEX);
1779:   DMSetDimension(*dm, dim);
1780:   DMSetCoordinateDim(*dm, dim+1);
1781:   MPI_Comm_rank(PetscObjectComm((PetscObject) *dm), &rank);
1782:   switch (dim) {
1783:   case 2:
1784:     if (simplex) {
1785:       DM              idm;
1786:       const PetscReal edgeLen     = 2.0/(1.0 + PETSC_PHI);
1787:       const PetscReal vertex[3]   = {0.0, 1.0/(1.0 + PETSC_PHI), PETSC_PHI/(1.0 + PETSC_PHI)};
1788:       const PetscInt  degree      = 5;
1789:       PetscInt        s[3]        = {1, 1, 1};
1790:       PetscInt        cone[3];
1791:       PetscInt       *graph, p, i, j, k;

1793:       numCells    = !rank ? 20 : 0;
1794:       numVerts    = !rank ? 12 : 0;
1795:       firstVertex = numCells;
1796:       /* Use icosahedron, which for a unit sphere has coordinates which are all cyclic permutations of

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

1800:          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
1801:          length is then given by 2/\phi = 2 * 0.61803 = 1.23606.
1802:       */
1803:       /* Construct vertices */
1804:       PetscCalloc1(numVerts * embedDim, &coordsIn);
1805:       if (!rank) {
1806:         for (p = 0, i = 0; p < embedDim; ++p) {
1807:           for (s[1] = -1; s[1] < 2; s[1] += 2) {
1808:             for (s[2] = -1; s[2] < 2; s[2] += 2) {
1809:               for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[(d+p)%embedDim]*vertex[(d+p)%embedDim];
1810:               ++i;
1811:             }
1812:           }
1813:         }
1814:       }
1815:       /* Construct graph */
1816:       PetscCalloc1(numVerts * numVerts, &graph);
1817:       for (i = 0; i < numVerts; ++i) {
1818:         for (j = 0, k = 0; j < numVerts; ++j) {
1819:           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i*embedDim], &coordsIn[j*embedDim]) - edgeLen) < PETSC_SMALL) {graph[i*numVerts+j] = 1; ++k;}
1820:         }
1821:         if (k != degree) SETERRQ3(comm, PETSC_ERR_PLIB, "Invalid icosahedron, vertex %D degree %D != %D", i, k, degree);
1822:       }
1823:       /* Build Topology */
1824:       DMPlexSetChart(*dm, 0, numCells+numVerts);
1825:       for (c = 0; c < numCells; c++) {
1826:         DMPlexSetConeSize(*dm, c, embedDim);
1827:       }
1828:       DMSetUp(*dm); /* Allocate space for cones */
1829:       /* Cells */
1830:       for (i = 0, c = 0; i < numVerts; ++i) {
1831:         for (j = 0; j < i; ++j) {
1832:           for (k = 0; k < j; ++k) {
1833:             if (graph[i*numVerts+j] && graph[j*numVerts+k] && graph[k*numVerts+i]) {
1834:               cone[0] = firstVertex+i; cone[1] = firstVertex+j; cone[2] = firstVertex+k;
1835:               /* Check orientation */
1836:               {
1837:                 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}}};
1838:                 PetscReal normal[3];
1839:                 PetscInt  e, f;

1841:                 for (d = 0; d < embedDim; ++d) {
1842:                   normal[d] = 0.0;
1843:                   for (e = 0; e < embedDim; ++e) {
1844:                     for (f = 0; f < embedDim; ++f) {
1845:                       normal[d] += epsilon[d][e][f]*(coordsIn[j*embedDim+e] - coordsIn[i*embedDim+e])*(coordsIn[k*embedDim+f] - coordsIn[i*embedDim+f]);
1846:                     }
1847:                   }
1848:                 }
1849:                 if (DotReal(embedDim, normal, &coordsIn[i*embedDim]) < 0) {PetscInt tmp = cone[1]; cone[1] = cone[2]; cone[2] = tmp;}
1850:               }
1851:               DMPlexSetCone(*dm, c++, cone);
1852:             }
1853:           }
1854:         }
1855:       }
1856:       DMPlexSymmetrize(*dm);
1857:       DMPlexStratify(*dm);
1858:       PetscFree(graph);
1859:       /* Interpolate mesh */
1860:       DMPlexInterpolate(*dm, &idm);
1861:       DMDestroy(dm);
1862:       *dm  = idm;
1863:     } else {
1864:       /*
1865:         12-21--13
1866:          |     |
1867:         25  4  24
1868:          |     |
1869:   12-25--9-16--8-24--13
1870:    |     |     |     |
1871:   23  5 17  0 15  3  22
1872:    |     |     |     |
1873:   10-20--6-14--7-19--11
1874:          |     |
1875:         20  1  19
1876:          |     |
1877:         10-18--11
1878:          |     |
1879:         23  2  22
1880:          |     |
1881:         12-21--13
1882:        */
1883:       const PetscReal dist = 1.0/PetscSqrtReal(3.0);
1884:       PetscInt        cone[4], ornt[4];

1886:       numCells    = !rank ?  6 : 0;
1887:       numEdges    = !rank ? 12 : 0;
1888:       numVerts    = !rank ?  8 : 0;
1889:       firstVertex = numCells;
1890:       firstEdge   = numCells + numVerts;
1891:       /* Build Topology */
1892:       DMPlexSetChart(*dm, 0, numCells+numEdges+numVerts);
1893:       for (c = 0; c < numCells; c++) {
1894:         DMPlexSetConeSize(*dm, c, 4);
1895:       }
1896:       for (e = firstEdge; e < firstEdge+numEdges; ++e) {
1897:         DMPlexSetConeSize(*dm, e, 2);
1898:       }
1899:       DMSetUp(*dm); /* Allocate space for cones */
1900:       if (!rank) {
1901:         /* Cell 0 */
1902:         cone[0] = 14; cone[1] = 15; cone[2] = 16; cone[3] = 17;
1903:         DMPlexSetCone(*dm, 0, cone);
1904:         ornt[0] = 0; ornt[1] = 0; ornt[2] = 0; ornt[3] = 0;
1905:         DMPlexSetConeOrientation(*dm, 0, ornt);
1906:         /* Cell 1 */
1907:         cone[0] = 18; cone[1] = 19; cone[2] = 14; cone[3] = 20;
1908:         DMPlexSetCone(*dm, 1, cone);
1909:         ornt[0] = 0; ornt[1] = 0; ornt[2] = -2; ornt[3] = 0;
1910:         DMPlexSetConeOrientation(*dm, 1, ornt);
1911:         /* Cell 2 */
1912:         cone[0] = 21; cone[1] = 22; cone[2] = 18; cone[3] = 23;
1913:         DMPlexSetCone(*dm, 2, cone);
1914:         ornt[0] = 0; ornt[1] = 0; ornt[2] = -2; ornt[3] = 0;
1915:         DMPlexSetConeOrientation(*dm, 2, ornt);
1916:         /* Cell 3 */
1917:         cone[0] = 19; cone[1] = 22; cone[2] = 24; cone[3] = 15;
1918:         DMPlexSetCone(*dm, 3, cone);
1919:         ornt[0] = -2; ornt[1] = -2; ornt[2] = 0; ornt[3] = -2;
1920:         DMPlexSetConeOrientation(*dm, 3, ornt);
1921:         /* Cell 4 */
1922:         cone[0] = 16; cone[1] = 24; cone[2] = 21; cone[3] = 25;
1923:         DMPlexSetCone(*dm, 4, cone);
1924:         ornt[0] = -2; ornt[1] = -2; ornt[2] = -2; ornt[3] = 0;
1925:         DMPlexSetConeOrientation(*dm, 4, ornt);
1926:         /* Cell 5 */
1927:         cone[0] = 20; cone[1] = 17; cone[2] = 25; cone[3] = 23;
1928:         DMPlexSetCone(*dm, 5, cone);
1929:         ornt[0] = -2; ornt[1] = -2; ornt[2] = -2; ornt[3] = -2;
1930:         DMPlexSetConeOrientation(*dm, 5, ornt);
1931:         /* Edges */
1932:         cone[0] =  6; cone[1] =  7;
1933:         DMPlexSetCone(*dm, 14, cone);
1934:         cone[0] =  7; cone[1] =  8;
1935:         DMPlexSetCone(*dm, 15, cone);
1936:         cone[0] =  8; cone[1] =  9;
1937:         DMPlexSetCone(*dm, 16, cone);
1938:         cone[0] =  9; cone[1] =  6;
1939:         DMPlexSetCone(*dm, 17, cone);
1940:         cone[0] = 10; cone[1] = 11;
1941:         DMPlexSetCone(*dm, 18, cone);
1942:         cone[0] = 11; cone[1] =  7;
1943:         DMPlexSetCone(*dm, 19, cone);
1944:         cone[0] =  6; cone[1] = 10;
1945:         DMPlexSetCone(*dm, 20, cone);
1946:         cone[0] = 12; cone[1] = 13;
1947:         DMPlexSetCone(*dm, 21, cone);
1948:         cone[0] = 13; cone[1] = 11;
1949:         DMPlexSetCone(*dm, 22, cone);
1950:         cone[0] = 10; cone[1] = 12;
1951:         DMPlexSetCone(*dm, 23, cone);
1952:         cone[0] = 13; cone[1] =  8;
1953:         DMPlexSetCone(*dm, 24, cone);
1954:         cone[0] = 12; cone[1] =  9;
1955:         DMPlexSetCone(*dm, 25, cone);
1956:       }
1957:       DMPlexSymmetrize(*dm);
1958:       DMPlexStratify(*dm);
1959:       /* Build coordinates */
1960:       PetscCalloc1(numVerts * embedDim, &coordsIn);
1961:       if (!rank) {
1962:         coordsIn[0*embedDim+0] = -dist; coordsIn[0*embedDim+1] =  dist; coordsIn[0*embedDim+2] = -dist;
1963:         coordsIn[1*embedDim+0] =  dist; coordsIn[1*embedDim+1] =  dist; coordsIn[1*embedDim+2] = -dist;
1964:         coordsIn[2*embedDim+0] =  dist; coordsIn[2*embedDim+1] = -dist; coordsIn[2*embedDim+2] = -dist;
1965:         coordsIn[3*embedDim+0] = -dist; coordsIn[3*embedDim+1] = -dist; coordsIn[3*embedDim+2] = -dist;
1966:         coordsIn[4*embedDim+0] = -dist; coordsIn[4*embedDim+1] =  dist; coordsIn[4*embedDim+2] =  dist;
1967:         coordsIn[5*embedDim+0] =  dist; coordsIn[5*embedDim+1] =  dist; coordsIn[5*embedDim+2] =  dist;
1968:         coordsIn[6*embedDim+0] = -dist; coordsIn[6*embedDim+1] = -dist; coordsIn[6*embedDim+2] =  dist;
1969:         coordsIn[7*embedDim+0] =  dist; coordsIn[7*embedDim+1] = -dist; coordsIn[7*embedDim+2] =  dist;
1970:       }
1971:     }
1972:     break;
1973:   case 3:
1974:     if (simplex) {
1975:       DM              idm;
1976:       const PetscReal edgeLen         = 1.0/PETSC_PHI;
1977:       const PetscReal vertexA[4]      = {0.5, 0.5, 0.5, 0.5};
1978:       const PetscReal vertexB[4]      = {1.0, 0.0, 0.0, 0.0};
1979:       const PetscReal vertexC[4]      = {0.5, 0.5*PETSC_PHI, 0.5/PETSC_PHI, 0.0};
1980:       const PetscInt  degree          = 12;
1981:       PetscInt        s[4]            = {1, 1, 1};
1982:       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},
1983:                                          {2, 0, 1, 3}, {2, 1, 3, 0}, {2, 3, 0, 1}, {3, 0, 2, 1}, {3, 1, 0, 2}, {3, 2, 1, 0}};
1984:       PetscInt        cone[4];
1985:       PetscInt       *graph, p, i, j, k, l;

1987:       numCells    = !rank ? 600 : 0;
1988:       numVerts    = !rank ? 120 : 0;
1989:       firstVertex = numCells;
1990:       /* Use the 600-cell, which for a unit sphere has coordinates which are

1992:            1/2 (\pm 1, \pm 1,    \pm 1, \pm 1)                          16
1993:                (\pm 1,    0,       0,      0)  all cyclic permutations   8
1994:            1/2 (\pm 1, \pm phi, \pm 1/phi, 0)  all even permutations    96

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

1999:          http://buzzard.pugetsound.edu/sage-practice/ch03s03.html
2000:          http://mathworld.wolfram.com/600-Cell.html
2001:       */
2002:       /* Construct vertices */
2003:       PetscCalloc1(numVerts * embedDim, &coordsIn);
2004:       i    = 0;
2005:       if (!rank) {
2006:         for (s[0] = -1; s[0] < 2; s[0] += 2) {
2007:           for (s[1] = -1; s[1] < 2; s[1] += 2) {
2008:             for (s[2] = -1; s[2] < 2; s[2] += 2) {
2009:               for (s[3] = -1; s[3] < 2; s[3] += 2) {
2010:                 for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[d]*vertexA[d];
2011:                 ++i;
2012:               }
2013:             }
2014:           }
2015:         }
2016:         for (p = 0; p < embedDim; ++p) {
2017:           s[1] = s[2] = s[3] = 1;
2018:           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2019:             for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[(d+p)%embedDim]*vertexB[(d+p)%embedDim];
2020:             ++i;
2021:           }
2022:         }
2023:         for (p = 0; p < 12; ++p) {
2024:           s[3] = 1;
2025:           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2026:             for (s[1] = -1; s[1] < 2; s[1] += 2) {
2027:               for (s[2] = -1; s[2] < 2; s[2] += 2) {
2028:                 for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[evenPerm[p][d]]*vertexC[evenPerm[p][d]];
2029:                 ++i;
2030:               }
2031:             }
2032:           }
2033:         }
2034:       }
2035:       if (i != numVerts) SETERRQ2(comm, PETSC_ERR_PLIB, "Invalid 600-cell, vertices %D != %D", i, numVerts);
2036:       /* Construct graph */
2037:       PetscCalloc1(numVerts * numVerts, &graph);
2038:       for (i = 0; i < numVerts; ++i) {
2039:         for (j = 0, k = 0; j < numVerts; ++j) {
2040:           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i*embedDim], &coordsIn[j*embedDim]) - edgeLen) < PETSC_SMALL) {graph[i*numVerts+j] = 1; ++k;}
2041:         }
2042:         if (k != degree) SETERRQ3(comm, PETSC_ERR_PLIB, "Invalid 600-cell, vertex %D degree %D != %D", i, k, degree);
2043:       }
2044:       /* Build Topology */
2045:       DMPlexSetChart(*dm, 0, numCells+numVerts);
2046:       for (c = 0; c < numCells; c++) {
2047:         DMPlexSetConeSize(*dm, c, embedDim);
2048:       }
2049:       DMSetUp(*dm); /* Allocate space for cones */
2050:       /* Cells */
2051:       if (!rank) {
2052:         for (i = 0, c = 0; i < numVerts; ++i) {
2053:           for (j = 0; j < i; ++j) {
2054:             for (k = 0; k < j; ++k) {
2055:               for (l = 0; l < k; ++l) {
2056:                 if (graph[i*numVerts+j] && graph[j*numVerts+k] && graph[k*numVerts+i] &&
2057:                     graph[l*numVerts+i] && graph[l*numVerts+j] && graph[l*numVerts+k]) {
2058:                   cone[0] = firstVertex+i; cone[1] = firstVertex+j; cone[2] = firstVertex+k; cone[3] = firstVertex+l;
2059:                   /* Check orientation: https://ef.gy/linear-algebra:normal-vectors-in-higher-dimensional-spaces */
2060:                   {
2061:                     const PetscInt epsilon[4][4][4][4] = {{{{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
2062:                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  1}, { 0,  0, -1, 0}},
2063:                                                            {{0,  0,  0,  0}, { 0, 0,  0, -1}, { 0,  0, 0,  0}, { 0,  1,  0, 0}},
2064:                                                            {{0,  0,  0,  0}, { 0, 0,  1,  0}, { 0, -1, 0,  0}, { 0,  0,  0, 0}}},

2066:                                                           {{{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0, -1}, { 0,  0,  1, 0}},
2067:                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
2068:                                                            {{0,  0,  0,  1}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, {-1,  0,  0, 0}},
2069:                                                            {{0,  0, -1,  0}, { 0, 0,  0,  0}, { 1,  0, 0,  0}, { 0,  0,  0, 0}}},

2071:                                                           {{{0,  0,  0,  0}, { 0, 0,  0,  1}, { 0,  0, 0,  0}, { 0, -1,  0, 0}},
2072:                                                            {{0,  0,  0, -1}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 1,  0,  0, 0}},
2073:                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
2074:                                                            {{0,  1,  0,  0}, {-1, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}}},

2076:                                                           {{{0,  0,  0,  0}, { 0, 0, -1,  0}, { 0,  1, 0,  0}, { 0,  0,  0, 0}},
2077:                                                            {{0,  0,  1,  0}, { 0, 0,  0,  0}, {-1,  0, 0,  0}, { 0,  0,  0, 0}},
2078:                                                            {{0, -1,  0,  0}, { 1, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
2079:                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}}}};
2080:                     PetscReal normal[4];
2081:                     PetscInt  e, f, g;

2083:                     for (d = 0; d < embedDim; ++d) {
2084:                       normal[d] = 0.0;
2085:                       for (e = 0; e < embedDim; ++e) {
2086:                         for (f = 0; f < embedDim; ++f) {
2087:                           for (g = 0; g < embedDim; ++g) {
2088:                             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]);
2089:                           }
2090:                         }
2091:                       }
2092:                     }
2093:                     if (DotReal(embedDim, normal, &coordsIn[i*embedDim]) < 0) {PetscInt tmp = cone[1]; cone[1] = cone[2]; cone[2] = tmp;}
2094:                   }
2095:                   DMPlexSetCone(*dm, c++, cone);
2096:                 }
2097:               }
2098:             }
2099:           }
2100:         }
2101:       }
2102:       DMPlexSymmetrize(*dm);
2103:       DMPlexStratify(*dm);
2104:       PetscFree(graph);
2105:       /* Interpolate mesh */
2106:       DMPlexInterpolate(*dm, &idm);
2107:       DMDestroy(dm);
2108:       *dm  = idm;
2109:       break;
2110:     }
2111:   default: SETERRQ1(comm, PETSC_ERR_SUP, "Unsupported dimension for sphere: %D", dim);
2112:   }
2113:   /* Create coordinates */
2114:   DMGetCoordinateSection(*dm, &coordSection);
2115:   PetscSectionSetNumFields(coordSection, 1);
2116:   PetscSectionSetFieldComponents(coordSection, 0, embedDim);
2117:   PetscSectionSetChart(coordSection, firstVertex, firstVertex+numVerts);
2118:   for (v = firstVertex; v < firstVertex+numVerts; ++v) {
2119:     PetscSectionSetDof(coordSection, v, embedDim);
2120:     PetscSectionSetFieldDof(coordSection, v, 0, embedDim);
2121:   }
2122:   PetscSectionSetUp(coordSection);
2123:   PetscSectionGetStorageSize(coordSection, &coordSize);
2124:   VecCreate(PETSC_COMM_SELF, &coordinates);
2125:   VecSetBlockSize(coordinates, embedDim);
2126:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
2127:   VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
2128:   VecSetType(coordinates,VECSTANDARD);
2129:   VecGetArray(coordinates, &coords);
2130:   for (v = 0; v < numVerts; ++v) for (d = 0; d < embedDim; ++d) {coords[v*embedDim+d] = coordsIn[v*embedDim+d];}
2131:   VecRestoreArray(coordinates, &coords);
2132:   DMSetCoordinatesLocal(*dm, coordinates);
2133:   VecDestroy(&coordinates);
2134:   PetscFree(coordsIn);
2135:   return(0);
2136: }

2138: /* External function declarations here */
2139: extern PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling);
2140: extern PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat);
2141: extern PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mat);
2142: extern PetscErrorCode DMCreateLocalSection_Plex(DM dm);
2143: extern PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm);
2144: extern PetscErrorCode DMCreateMatrix_Plex(DM dm,  Mat *J);
2145: extern PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm);
2146: extern PetscErrorCode DMCreateCoordinateField_Plex(DM dm, DMField *field);
2147: PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm);
2148: extern PetscErrorCode DMSetUp_Plex(DM dm);
2149: extern PetscErrorCode DMDestroy_Plex(DM dm);
2150: extern PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer);
2151: extern PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer);
2152: extern PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm);
2153: extern PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm);
2154: static PetscErrorCode DMInitialize_Plex(DM dm);

2156: /* Replace dm with the contents of dmNew
2157:    - Share the DM_Plex structure
2158:    - Share the coordinates
2159:    - Share the SF
2160: */
2161: static PetscErrorCode DMPlexReplace_Static(DM dm, DM dmNew)
2162: {
2163:   PetscSF               sf;
2164:   DM                    coordDM, coarseDM;
2165:   Vec                   coords;
2166:   PetscBool             isper;
2167:   const PetscReal      *maxCell, *L;
2168:   const DMBoundaryType *bd;
2169:   PetscErrorCode        ierr;

2172:   DMGetPointSF(dmNew, &sf);
2173:   DMSetPointSF(dm, sf);
2174:   DMGetCoordinateDM(dmNew, &coordDM);
2175:   DMGetCoordinatesLocal(dmNew, &coords);
2176:   DMSetCoordinateDM(dm, coordDM);
2177:   DMSetCoordinatesLocal(dm, coords);
2178:   DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);
2179:   DMSetPeriodicity(dmNew, isper, maxCell, L, bd);
2180:   DMDestroy_Plex(dm);
2181:   DMInitialize_Plex(dm);
2182:   dm->data = dmNew->data;
2183:   ((DM_Plex *) dmNew->data)->refct++;
2184:   DMDestroyLabelLinkList_Internal(dm);
2185:   DMCopyLabels(dmNew, dm, PETSC_OWN_POINTER, PETSC_TRUE);
2186:   DMGetCoarseDM(dmNew,&coarseDM);
2187:   DMSetCoarseDM(dm,coarseDM);
2188:   return(0);
2189: }

2191: /* Swap dm with the contents of dmNew
2192:    - Swap the DM_Plex structure
2193:    - Swap the coordinates
2194:    - Swap the point PetscSF
2195: */
2196: static PetscErrorCode DMPlexSwap_Static(DM dmA, DM dmB)
2197: {
2198:   DM              coordDMA, coordDMB;
2199:   Vec             coordsA,  coordsB;
2200:   PetscSF         sfA,      sfB;
2201:   void            *tmp;
2202:   DMLabelLink     listTmp;
2203:   DMLabel         depthTmp;
2204:   PetscInt        tmpI;
2205:   PetscErrorCode  ierr;

2208:   DMGetPointSF(dmA, &sfA);
2209:   DMGetPointSF(dmB, &sfB);
2210:   PetscObjectReference((PetscObject) sfA);
2211:   DMSetPointSF(dmA, sfB);
2212:   DMSetPointSF(dmB, sfA);
2213:   PetscObjectDereference((PetscObject) sfA);

2215:   DMGetCoordinateDM(dmA, &coordDMA);
2216:   DMGetCoordinateDM(dmB, &coordDMB);
2217:   PetscObjectReference((PetscObject) coordDMA);
2218:   DMSetCoordinateDM(dmA, coordDMB);
2219:   DMSetCoordinateDM(dmB, coordDMA);
2220:   PetscObjectDereference((PetscObject) coordDMA);

2222:   DMGetCoordinatesLocal(dmA, &coordsA);
2223:   DMGetCoordinatesLocal(dmB, &coordsB);
2224:   PetscObjectReference((PetscObject) coordsA);
2225:   DMSetCoordinatesLocal(dmA, coordsB);
2226:   DMSetCoordinatesLocal(dmB, coordsA);
2227:   PetscObjectDereference((PetscObject) coordsA);

2229:   tmp       = dmA->data;
2230:   dmA->data = dmB->data;
2231:   dmB->data = tmp;
2232:   listTmp   = dmA->labels;
2233:   dmA->labels = dmB->labels;
2234:   dmB->labels = listTmp;
2235:   depthTmp  = dmA->depthLabel;
2236:   dmA->depthLabel = dmB->depthLabel;
2237:   dmB->depthLabel = depthTmp;
2238:   tmpI         = dmA->levelup;
2239:   dmA->levelup = dmB->levelup;
2240:   dmB->levelup = tmpI;
2241:   return(0);
2242: }

2244: PetscErrorCode DMSetFromOptions_NonRefinement_Plex(PetscOptionItems *PetscOptionsObject,DM dm)
2245: {
2246:   DM_Plex       *mesh = (DM_Plex*) dm->data;

2250:   /* Handle viewing */
2251:   PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMPlexMatSetClosure", PETSC_FALSE, &mesh->printSetValues, NULL);
2252:   PetscOptionsBoundedInt("-dm_plex_print_fem", "Debug output level all fem computations", "DMPlexSNESComputeResidualFEM", 0, &mesh->printFEM, NULL,0);
2253:   PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMPlexSNESComputeResidualFEM", mesh->printTol, &mesh->printTol, NULL);
2254:   PetscOptionsBoundedInt("-dm_plex_print_l2", "Debug output level all L2 diff computations", "DMComputeL2Diff", 0, &mesh->printL2, NULL,0);
2255:   /* Point Location */
2256:   PetscOptionsBool("-dm_plex_hash_location", "Use grid hashing for point location", "DMInterpolate", PETSC_FALSE, &mesh->useHashLocation, NULL);
2257:   /* Partitioning and distribution */
2258:   PetscOptionsBool("-dm_plex_partition_balance", "Attempt to evenly divide points on partition boundary between processes", "DMPlexSetPartitionBalance", PETSC_FALSE, &mesh->partitionBalance, NULL);
2259:   /* Generation and remeshing */
2260:   PetscOptionsBool("-dm_plex_remesh_bd", "Allow changes to the boundary on remeshing", "DMAdapt", PETSC_FALSE, &mesh->remeshBd, NULL);
2261:   /* Projection behavior */
2262:   PetscOptionsBoundedInt("-dm_plex_max_projection_height", "Maxmimum mesh point height used to project locally", "DMPlexSetMaxProjectionHeight", 0, &mesh->maxProjectionHeight, NULL,0);
2263:   PetscOptionsBool("-dm_plex_regular_refinement", "Use special nested projection algorithm for regular refinement", "DMPlexSetRegularRefinement", mesh->regularRefinement, &mesh->regularRefinement, NULL);
2264:   /* Checking structure */
2265:   {
2266:     PetscBool   flg = PETSC_FALSE, flg2 = PETSC_FALSE, all = PETSC_FALSE;

2268:     PetscOptionsBool("-dm_plex_check_all", "Perform all checks", NULL, PETSC_FALSE, &all, &flg2);
2269:     PetscOptionsBool("-dm_plex_check_symmetry", "Check that the adjacency information in the mesh is symmetric", "DMPlexCheckSymmetry", PETSC_FALSE, &flg, &flg2);
2270:     if (all || (flg && flg2)) {DMPlexCheckSymmetry(dm);}
2271:     PetscOptionsBool("-dm_plex_check_skeleton", "Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes)", "DMPlexCheckSkeleton", PETSC_FALSE, &flg, &flg2);
2272:     if (all || (flg && flg2)) {DMPlexCheckSkeleton(dm, 0);}
2273:     PetscOptionsBool("-dm_plex_check_faces", "Check that the faces of each cell give a vertex order this is consistent with what we expect from the cell type", "DMPlexCheckFaces", PETSC_FALSE, &flg, &flg2);
2274:     if (all || (flg && flg2)) {DMPlexCheckFaces(dm, 0);}
2275:     PetscOptionsBool("-dm_plex_check_geometry", "Check that cells have positive volume", "DMPlexCheckGeometry", PETSC_FALSE, &flg, &flg2);
2276:     if (all || (flg && flg2)) {DMPlexCheckGeometry(dm);}
2277:     PetscOptionsBool("-dm_plex_check_pointsf", "Check some necessary conditions for PointSF", "DMPlexCheckPointSF", PETSC_FALSE, &flg, &flg2);
2278:     if (all || (flg && flg2)) {DMPlexCheckPointSF(dm);}
2279:     PetscOptionsBool("-dm_plex_check_interface_cones", "Check points on inter-partition interfaces have conforming order of cone points", "DMPlexCheckInterfaceCones", PETSC_FALSE, &flg, &flg2);
2280:     if (all || (flg && flg2)) {DMPlexCheckInterfaceCones(dm);}
2281:   }

2283:   PetscPartitionerSetFromOptions(mesh->partitioner);
2284:   return(0);
2285: }

2287: static PetscErrorCode DMSetFromOptions_Plex(PetscOptionItems *PetscOptionsObject,DM dm)
2288: {
2289:   PetscInt       refine = 0, coarsen = 0, r;
2290:   PetscBool      isHierarchy;

2295:   PetscOptionsHead(PetscOptionsObject,"DMPlex Options");
2296:   /* Handle DMPlex refinement */
2297:   PetscOptionsBoundedInt("-dm_refine", "The number of uniform refinements", "DMCreate", refine, &refine, NULL,0);
2298:   PetscOptionsBoundedInt("-dm_refine_hierarchy", "The number of uniform refinements", "DMCreate", refine, &refine, &isHierarchy,0);
2299:   if (refine) {DMPlexSetRefinementUniform(dm, PETSC_TRUE);}
2300:   if (refine && isHierarchy) {
2301:     DM *dms, coarseDM;

2303:     DMGetCoarseDM(dm, &coarseDM);
2304:     PetscObjectReference((PetscObject)coarseDM);
2305:     PetscMalloc1(refine,&dms);
2306:     DMRefineHierarchy(dm, refine, dms);
2307:     /* Total hack since we do not pass in a pointer */
2308:     DMPlexSwap_Static(dm, dms[refine-1]);
2309:     if (refine == 1) {
2310:       DMSetCoarseDM(dm, dms[0]);
2311:       DMPlexSetRegularRefinement(dm, PETSC_TRUE);
2312:     } else {
2313:       DMSetCoarseDM(dm, dms[refine-2]);
2314:       DMPlexSetRegularRefinement(dm, PETSC_TRUE);
2315:       DMSetCoarseDM(dms[0], dms[refine-1]);
2316:       DMPlexSetRegularRefinement(dms[0], PETSC_TRUE);
2317:     }
2318:     DMSetCoarseDM(dms[refine-1], coarseDM);
2319:     PetscObjectDereference((PetscObject)coarseDM);
2320:     /* Free DMs */
2321:     for (r = 0; r < refine; ++r) {
2322:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]);
2323:       DMDestroy(&dms[r]);
2324:     }
2325:     PetscFree(dms);
2326:   } else {
2327:     for (r = 0; r < refine; ++r) {
2328:       DM refinedMesh;

2330:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
2331:       DMRefine(dm, PetscObjectComm((PetscObject) dm), &refinedMesh);
2332:       /* Total hack since we do not pass in a pointer */
2333:       DMPlexReplace_Static(dm, refinedMesh);
2334:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
2335:       DMDestroy(&refinedMesh);
2336:     }
2337:   }
2338:   /* Handle DMPlex coarsening */
2339:   PetscOptionsBoundedInt("-dm_coarsen", "Coarsen the mesh", "DMCreate", coarsen, &coarsen, NULL,0);
2340:   PetscOptionsBoundedInt("-dm_coarsen_hierarchy", "The number of coarsenings", "DMCreate", coarsen, &coarsen, &isHierarchy,0);
2341:   if (coarsen && isHierarchy) {
2342:     DM *dms;

2344:     PetscMalloc1(coarsen, &dms);
2345:     DMCoarsenHierarchy(dm, coarsen, dms);
2346:     /* Free DMs */
2347:     for (r = 0; r < coarsen; ++r) {
2348:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]);
2349:       DMDestroy(&dms[r]);
2350:     }
2351:     PetscFree(dms);
2352:   } else {
2353:     for (r = 0; r < coarsen; ++r) {
2354:       DM coarseMesh;

2356:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
2357:       DMCoarsen(dm, PetscObjectComm((PetscObject) dm), &coarseMesh);
2358:       /* Total hack since we do not pass in a pointer */
2359:       DMPlexReplace_Static(dm, coarseMesh);
2360:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
2361:       DMDestroy(&coarseMesh);
2362:     }
2363:   }
2364:   /* Handle */
2365:   DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
2366:   PetscOptionsTail();
2367:   return(0);
2368: }

2370: static PetscErrorCode DMCreateGlobalVector_Plex(DM dm,Vec *vec)
2371: {

2375:   DMCreateGlobalVector_Section_Private(dm,vec);
2376:   /* VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM); */
2377:   VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex);
2378:   VecSetOperation(*vec, VECOP_VIEWNATIVE, (void (*)(void)) VecView_Plex_Native);
2379:   VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex);
2380:   VecSetOperation(*vec, VECOP_LOADNATIVE, (void (*)(void)) VecLoad_Plex_Native);
2381:   return(0);
2382: }

2384: static PetscErrorCode DMCreateLocalVector_Plex(DM dm,Vec *vec)
2385: {

2389:   DMCreateLocalVector_Section_Private(dm,vec);
2390:   VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex_Local);
2391:   VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex_Local);
2392:   return(0);
2393: }

2395: static PetscErrorCode DMGetDimPoints_Plex(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
2396: {
2397:   PetscInt       depth, d;

2401:   DMPlexGetDepth(dm, &depth);
2402:   if (depth == 1) {
2403:     DMGetDimension(dm, &d);
2404:     if (dim == 0)      {DMPlexGetDepthStratum(dm, dim, pStart, pEnd);}
2405:     else if (dim == d) {DMPlexGetDepthStratum(dm, 1, pStart, pEnd);}
2406:     else               {*pStart = 0; *pEnd = 0;}
2407:   } else {
2408:     DMPlexGetDepthStratum(dm, dim, pStart, pEnd);
2409:   }
2410:   return(0);
2411: }

2413: static PetscErrorCode DMGetNeighbors_Plex(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
2414: {
2415:   PetscSF        sf;

2419:   DMGetPointSF(dm, &sf);
2420:   PetscSFGetRootRanks(sf, nranks, ranks, NULL, NULL, NULL);
2421:   return(0);
2422: }

2424: static PetscErrorCode DMInitialize_Plex(DM dm)
2425: {

2429:   dm->ops->view                            = DMView_Plex;
2430:   dm->ops->load                            = DMLoad_Plex;
2431:   dm->ops->setfromoptions                  = DMSetFromOptions_Plex;
2432:   dm->ops->clone                           = DMClone_Plex;
2433:   dm->ops->setup                           = DMSetUp_Plex;
2434:   dm->ops->createlocalsection              = DMCreateLocalSection_Plex;
2435:   dm->ops->createdefaultconstraints        = DMCreateDefaultConstraints_Plex;
2436:   dm->ops->createglobalvector              = DMCreateGlobalVector_Plex;
2437:   dm->ops->createlocalvector               = DMCreateLocalVector_Plex;
2438:   dm->ops->getlocaltoglobalmapping         = NULL;
2439:   dm->ops->createfieldis                   = NULL;
2440:   dm->ops->createcoordinatedm              = DMCreateCoordinateDM_Plex;
2441:   dm->ops->createcoordinatefield           = DMCreateCoordinateField_Plex;
2442:   dm->ops->getcoloring                     = NULL;
2443:   dm->ops->creatematrix                    = DMCreateMatrix_Plex;
2444:   dm->ops->createinterpolation             = DMCreateInterpolation_Plex;
2445:   dm->ops->createmassmatrix                = DMCreateMassMatrix_Plex;
2446:   dm->ops->createinjection                 = DMCreateInjection_Plex;
2447:   dm->ops->refine                          = DMRefine_Plex;
2448:   dm->ops->coarsen                         = DMCoarsen_Plex;
2449:   dm->ops->refinehierarchy                 = DMRefineHierarchy_Plex;
2450:   dm->ops->coarsenhierarchy                = DMCoarsenHierarchy_Plex;
2451:   dm->ops->adaptlabel                      = DMAdaptLabel_Plex;
2452:   dm->ops->adaptmetric                     = DMAdaptMetric_Plex;
2453:   dm->ops->globaltolocalbegin              = NULL;
2454:   dm->ops->globaltolocalend                = NULL;
2455:   dm->ops->localtoglobalbegin              = NULL;
2456:   dm->ops->localtoglobalend                = NULL;
2457:   dm->ops->destroy                         = DMDestroy_Plex;
2458:   dm->ops->createsubdm                     = DMCreateSubDM_Plex;
2459:   dm->ops->createsuperdm                   = DMCreateSuperDM_Plex;
2460:   dm->ops->getdimpoints                    = DMGetDimPoints_Plex;
2461:   dm->ops->locatepoints                    = DMLocatePoints_Plex;
2462:   dm->ops->projectfunctionlocal            = DMProjectFunctionLocal_Plex;
2463:   dm->ops->projectfunctionlabellocal       = DMProjectFunctionLabelLocal_Plex;
2464:   dm->ops->projectfieldlocal               = DMProjectFieldLocal_Plex;
2465:   dm->ops->projectfieldlabellocal          = DMProjectFieldLabelLocal_Plex;
2466:   dm->ops->computel2diff                   = DMComputeL2Diff_Plex;
2467:   dm->ops->computel2gradientdiff           = DMComputeL2GradientDiff_Plex;
2468:   dm->ops->computel2fielddiff              = DMComputeL2FieldDiff_Plex;
2469:   dm->ops->getneighbors                    = DMGetNeighbors_Plex;
2470:   PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C",DMPlexInsertBoundaryValues_Plex);
2471:   PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",DMSetUpGLVisViewer_Plex);
2472:   PetscObjectComposeFunction((PetscObject)dm,"DMCreateNeumannOverlap_C",DMCreateNeumannOverlap_Plex);
2473:   PetscObjectComposeFunction((PetscObject)dm,"DMPlexGetOverlap_C",DMPlexGetOverlap_Plex);
2474:   return(0);
2475: }

2477: PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm)
2478: {
2479:   DM_Plex        *mesh = (DM_Plex *) dm->data;

2483:   mesh->refct++;
2484:   (*newdm)->data = mesh;
2485:   PetscObjectChangeTypeName((PetscObject) *newdm, DMPLEX);
2486:   DMInitialize_Plex(*newdm);
2487:   return(0);
2488: }

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

2496:   Options Database Keys:
2497: + -dm_plex_hash_location             - Use grid hashing for point location
2498: . -dm_plex_partition_balance         - Attempt to evenly divide points on partition boundary between processes
2499: . -dm_plex_remesh_bd                 - Allow changes to the boundary on remeshing
2500: . -dm_plex_max_projection_height     - Maxmimum mesh point height used to project locally
2501: . -dm_plex_regular_refinement        - Use special nested projection algorithm for regular refinement
2502: . -dm_plex_check_symmetry            - Check that the adjacency information in the mesh is symmetric
2503: . -dm_plex_check_skeleton <celltype> - Check that each cell has the correct number of vertices
2504: . -dm_plex_check_faces <celltype>    - Check that the faces of each cell give a vertex order this is consistent with what we expect from the cell type
2505: . -dm_plex_check_geometry            - Check that cells have positive volume
2506: . -dm_view :mesh.tex:ascii_latex     - View the mesh in LaTeX/TikZ
2507: . -dm_plex_view_scale <num>          - Scale the TikZ
2508: - -dm_plex_print_fem <num>           - View FEM assembly information, such as element vectors and matrices


2511:   Level: intermediate

2513: .seealso: DMType, DMPlexCreate(), DMCreate(), DMSetType()
2514: M*/

2516: PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm)
2517: {
2518:   DM_Plex        *mesh;
2519:   PetscInt       unit, d;

2524:   PetscNewLog(dm,&mesh);
2525:   dm->dim  = 0;
2526:   dm->data = mesh;

2528:   mesh->refct             = 1;
2529:   PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection);
2530:   mesh->maxConeSize       = 0;
2531:   mesh->cones             = NULL;
2532:   mesh->coneOrientations  = NULL;
2533:   PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection);
2534:   mesh->maxSupportSize    = 0;
2535:   mesh->supports          = NULL;
2536:   mesh->refinementUniform = PETSC_TRUE;
2537:   mesh->refinementLimit   = -1.0;
2538:   mesh->ghostCellStart    = -1;
2539:   mesh->interpolated      = DMPLEX_INTERPOLATED_INVALID;
2540:   mesh->interpolatedCollective = DMPLEX_INTERPOLATED_INVALID;

2542:   mesh->facesTmp = NULL;

2544:   mesh->tetgenOpts   = NULL;
2545:   mesh->triangleOpts = NULL;
2546:   PetscPartitionerCreate(PetscObjectComm((PetscObject)dm), &mesh->partitioner);
2547:   mesh->remeshBd     = PETSC_FALSE;

2549:   mesh->subpointMap = NULL;

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

2553:   mesh->regularRefinement   = PETSC_FALSE;
2554:   mesh->depthState          = -1;
2555:   mesh->globalVertexNumbers = NULL;
2556:   mesh->globalCellNumbers   = NULL;
2557:   mesh->anchorSection       = NULL;
2558:   mesh->anchorIS            = NULL;
2559:   mesh->createanchors       = NULL;
2560:   mesh->computeanchormatrix = NULL;
2561:   mesh->parentSection       = NULL;
2562:   mesh->parents             = NULL;
2563:   mesh->childIDs            = NULL;
2564:   mesh->childSection        = NULL;
2565:   mesh->children            = NULL;
2566:   mesh->referenceTree       = NULL;
2567:   mesh->getchildsymmetry    = NULL;
2568:   for (d = 0; d < 8; ++d) mesh->hybridPointMax[d] = PETSC_DETERMINE;
2569:   mesh->vtkCellHeight       = 0;
2570:   mesh->useAnchors          = PETSC_FALSE;

2572:   mesh->maxProjectionHeight = 0;

2574:   mesh->printSetValues = PETSC_FALSE;
2575:   mesh->printFEM       = 0;
2576:   mesh->printTol       = 1.0e-10;

2578:   DMInitialize_Plex(dm);
2579:   return(0);
2580: }

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

2585:   Collective

2587:   Input Parameter:
2588: . comm - The communicator for the DMPlex object

2590:   Output Parameter:
2591: . mesh  - The DMPlex object

2593:   Level: beginner

2595: @*/
2596: PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh)
2597: {

2602:   DMCreate(comm, mesh);
2603:   DMSetType(*mesh, DMPLEX);
2604:   return(0);
2605: }

2607: /*
2608:   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
2609: */
2610: /* TODO: invertCells and spaceDim arguments could be added also to to DMPlexCreateFromCellListParallel(), DMPlexBuildFromCellList_Internal() and DMPlexCreateFromCellList() */
2611: PetscErrorCode DMPlexBuildFromCellList_Parallel_Internal(DM dm, PetscInt spaceDim, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const int cells[], PetscBool invertCells, PetscSF *sfVert)
2612: {
2613:   PetscSF         sfPoint;
2614:   PetscLayout     vLayout;
2615:   PetscHSetI      vhash;
2616:   PetscSFNode    *remoteVerticesAdj, *vertexLocal, *vertexOwner, *remoteVertex;
2617:   const PetscInt *vrange;
2618:   PetscInt        numVerticesAdj, off = 0, *verticesAdj, numVerticesGhost = 0, *localVertex, *cone, c, p, v, g;
2619:   PetscMPIInt     rank, size;
2620:   PetscErrorCode  ierr;

2623:   MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);
2624:   MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size);
2625:   /* Partition vertices */
2626:   PetscLayoutCreate(PetscObjectComm((PetscObject) dm), &vLayout);
2627:   PetscLayoutSetLocalSize(vLayout, numVertices);
2628:   PetscLayoutSetBlockSize(vLayout, 1);
2629:   PetscLayoutSetUp(vLayout);
2630:   PetscLayoutGetRanges(vLayout, &vrange);
2631:   /* Count vertices and map them to procs */
2632:   PetscHSetICreate(&vhash);
2633:   for (c = 0; c < numCells; ++c) {
2634:     for (p = 0; p < numCorners; ++p) {
2635:       PetscHSetIAdd(vhash, cells[c*numCorners+p]);
2636:     }
2637:   }
2638:   PetscHSetIGetSize(vhash, &numVerticesAdj);
2639:   PetscMalloc1(numVerticesAdj, &verticesAdj);
2640:   PetscHSetIGetElems(vhash, &off, verticesAdj);
2641:   PetscHSetIDestroy(&vhash);
2642:   if (off != numVerticesAdj) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid number of local vertices %D should be %D", off, numVerticesAdj);
2643:   PetscSortInt(numVerticesAdj, verticesAdj);
2644:   PetscMalloc1(numVerticesAdj, &remoteVerticesAdj);
2645:   for (v = 0; v < numVerticesAdj; ++v) {
2646:     const PetscInt gv = verticesAdj[v];
2647:     PetscInt       vrank;

2649:     PetscFindInt(gv, size+1, vrange, &vrank);
2650:     vrank = vrank < 0 ? -(vrank+2) : vrank;
2651:     remoteVerticesAdj[v].index = gv - vrange[vrank];
2652:     remoteVerticesAdj[v].rank  = vrank;
2653:   }
2654:   /* Create cones */
2655:   DMPlexSetChart(dm, 0, numCells+numVerticesAdj);
2656:   for (c = 0; c < numCells; ++c) {DMPlexSetConeSize(dm, c, numCorners);}
2657:   DMSetUp(dm);
2658:   DMGetWorkArray(dm, numCorners, MPIU_INT, &cone);
2659:   for (c = 0; c < numCells; ++c) {
2660:     for (p = 0; p < numCorners; ++p) {
2661:       const PetscInt gv = cells[c*numCorners+p];
2662:       PetscInt       lv;

2664:       PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv);
2665:       if (lv < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %D in local connectivity", gv);
2666:       cone[p] = lv+numCells;
2667:     }
2668:     if (invertCells) { DMPlexInvertCell_Internal(spaceDim, numCorners, cone); }
2669:     DMPlexSetCone(dm, c, cone);
2670:   }
2671:   DMRestoreWorkArray(dm, numCorners, MPIU_INT, &cone);
2672:   /* Create SF for vertices */
2673:   PetscSFCreate(PetscObjectComm((PetscObject)dm), sfVert);
2674:   PetscObjectSetName((PetscObject) *sfVert, "Vertex Ownership SF");
2675:   PetscSFSetFromOptions(*sfVert);
2676:   PetscSFSetGraph(*sfVert, numVertices, numVerticesAdj, NULL, PETSC_OWN_POINTER, remoteVerticesAdj, PETSC_OWN_POINTER);
2677:   PetscFree(verticesAdj);
2678:   /* Build pointSF */
2679:   PetscMalloc2(numVerticesAdj, &vertexLocal, numVertices, &vertexOwner);
2680:   for (v = 0; v < numVerticesAdj; ++v) {vertexLocal[v].index = v+numCells; vertexLocal[v].rank = rank;}
2681:   for (v = 0; v < numVertices;    ++v) {vertexOwner[v].index = -1;         vertexOwner[v].rank = -1;}
2682:   PetscSFReduceBegin(*sfVert, MPIU_2INT, vertexLocal, vertexOwner, MPI_MAXLOC);
2683:   PetscSFReduceEnd(*sfVert, MPIU_2INT, vertexLocal, vertexOwner, MPI_MAXLOC);
2684:   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);
2685:   PetscSFBcastBegin(*sfVert, MPIU_2INT, vertexOwner, vertexLocal);
2686:   PetscSFBcastEnd(*sfVert, MPIU_2INT, vertexOwner, vertexLocal);
2687:   for (v = 0; v < numVerticesAdj; ++v) if (vertexLocal[v].rank != rank) ++numVerticesGhost;
2688:   PetscMalloc1(numVerticesGhost, &localVertex);
2689:   PetscMalloc1(numVerticesGhost, &remoteVertex);
2690:   for (v = 0, g = 0; v < numVerticesAdj; ++v) {
2691:     if (vertexLocal[v].rank != rank) {
2692:       localVertex[g]        = v+numCells;
2693:       remoteVertex[g].index = vertexLocal[v].index;
2694:       remoteVertex[g].rank  = vertexLocal[v].rank;
2695:       ++g;
2696:     }
2697:   }
2698:   PetscFree2(vertexLocal, vertexOwner);
2699:   if (g != numVerticesGhost) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid number of vertex ghosts %D should be %D", g, numVerticesGhost);
2700:   DMGetPointSF(dm, &sfPoint);
2701:   PetscObjectSetName((PetscObject) sfPoint, "point SF");
2702:   PetscSFSetGraph(sfPoint, numCells+numVerticesAdj, numVerticesGhost, localVertex, PETSC_OWN_POINTER, remoteVertex, PETSC_OWN_POINTER);
2703:   PetscLayoutDestroy(&vLayout);
2704:   /* Fill in the rest of the topology structure */
2705:   DMPlexSymmetrize(dm);
2706:   DMPlexStratify(dm);
2707:   return(0);
2708: }

2710: /*
2711:   This takes as input the coordinates for each owned vertex
2712: */
2713: PetscErrorCode DMPlexBuildCoordinates_Parallel_Internal(DM dm, PetscInt spaceDim, PetscInt numCells, PetscInt numV, PetscSF sfVert, const PetscReal vertexCoords[])
2714: {
2715:   PetscSection   coordSection;
2716:   Vec            coordinates;
2717:   PetscScalar   *coords;
2718:   PetscInt       numVertices, numVerticesAdj, coordSize, v;

2722:   DMSetCoordinateDim(dm, spaceDim);
2723:   PetscSFGetGraph(sfVert, &numVertices, &numVerticesAdj, NULL, NULL);
2724:   DMGetCoordinateSection(dm, &coordSection);
2725:   PetscSectionSetNumFields(coordSection, 1);
2726:   PetscSectionSetFieldComponents(coordSection, 0, spaceDim);
2727:   PetscSectionSetChart(coordSection, numCells, numCells + numVerticesAdj);
2728:   for (v = numCells; v < numCells+numVerticesAdj; ++v) {
2729:     PetscSectionSetDof(coordSection, v, spaceDim);
2730:     PetscSectionSetFieldDof(coordSection, v, 0, spaceDim);
2731:   }
2732:   PetscSectionSetUp(coordSection);
2733:   PetscSectionGetStorageSize(coordSection, &coordSize);
2734:   VecCreate(PetscObjectComm((PetscObject)dm), &coordinates);
2735:   VecSetBlockSize(coordinates, spaceDim);
2736:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
2737:   VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
2738:   VecSetType(coordinates,VECSTANDARD);
2739:   VecGetArray(coordinates, &coords);
2740:   {
2741:     MPI_Datatype coordtype;

2743:     /* Need a temp buffer for coords if we have complex/single */
2744:     MPI_Type_contiguous(spaceDim, MPIU_SCALAR, &coordtype);
2745:     MPI_Type_commit(&coordtype);
2746: #if defined(PETSC_USE_COMPLEX)
2747:     {
2748:     PetscScalar *svertexCoords;
2749:     PetscInt    i;
2750:     PetscMalloc1(numV*spaceDim,&svertexCoords);
2751:     for (i=0; i<numV*spaceDim; i++) svertexCoords[i] = vertexCoords[i];
2752:     PetscSFBcastBegin(sfVert, coordtype, svertexCoords, coords);
2753:     PetscSFBcastEnd(sfVert, coordtype, svertexCoords, coords);
2754:     PetscFree(svertexCoords);
2755:     }
2756: #else
2757:     PetscSFBcastBegin(sfVert, coordtype, vertexCoords, coords);
2758:     PetscSFBcastEnd(sfVert, coordtype, vertexCoords, coords);
2759: #endif
2760:     MPI_Type_free(&coordtype);
2761:   }
2762:   VecRestoreArray(coordinates, &coords);
2763:   DMSetCoordinatesLocal(dm, coordinates);
2764:   VecDestroy(&coordinates);
2765:   return(0);
2766: }

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

2771:   Input Parameters:
2772: + comm - The communicator
2773: . dim - The topological dimension of the mesh
2774: . numCells - The number of cells owned by this process
2775: . numVertices - The number of vertices owned by this process
2776: . numCorners - The number of vertices for each cell
2777: . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
2778: . cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
2779: . spaceDim - The spatial dimension used for coordinates
2780: - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex

2782:   Output Parameter:
2783: + dm - The DM
2784: - vertexSF - Optional, SF describing complete vertex ownership

2786:   Note: Two triangles sharing a face
2787: $
2788: $        2
2789: $      / | \
2790: $     /  |  \
2791: $    /   |   \
2792: $   0  0 | 1  3
2793: $    \   |   /
2794: $     \  |  /
2795: $      \ | /
2796: $        1
2797: would have input
2798: $  numCells = 2, numVertices = 4
2799: $  cells = [0 1 2  1 3 2]
2800: $
2801: which would result in the DMPlex
2802: $
2803: $        4
2804: $      / | \
2805: $     /  |  \
2806: $    /   |   \
2807: $   2  0 | 1  5
2808: $    \   |   /
2809: $     \  |  /
2810: $      \ | /
2811: $        3

2813:   Level: beginner

2815: .seealso: DMPlexCreateFromCellList(), DMPlexCreateFromDAG(), DMPlexCreate()
2816: @*/
2817: 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)
2818: {
2819:   PetscSF        sfVert;

2823:   DMCreate(comm, dm);
2824:   DMSetType(*dm, DMPLEX);
2827:   DMSetDimension(*dm, dim);
2828:   DMPlexBuildFromCellList_Parallel_Internal(*dm, spaceDim, numCells, numVertices, numCorners, cells, PETSC_FALSE, &sfVert);
2829:   if (interpolate) {
2830:     DM idm;

2832:     DMPlexInterpolate(*dm, &idm);
2833:     DMDestroy(dm);
2834:     *dm  = idm;
2835:   }
2836:   DMPlexBuildCoordinates_Parallel_Internal(*dm, spaceDim, numCells, numVertices, sfVert, vertexCoords);
2837:   if (vertexSF) *vertexSF = sfVert;
2838:   else {PetscSFDestroy(&sfVert);}
2839:   return(0);
2840: }

2842: /*
2843:   This takes as input the common mesh generator output, a list of the vertices for each cell
2844: */
2845: PetscErrorCode DMPlexBuildFromCellList_Internal(DM dm, PetscInt spaceDim, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const int cells[], PetscBool invertCells)
2846: {
2847:   PetscInt      *cone, c, p;

2851:   DMPlexSetChart(dm, 0, numCells+numVertices);
2852:   for (c = 0; c < numCells; ++c) {
2853:     DMPlexSetConeSize(dm, c, numCorners);
2854:   }
2855:   DMSetUp(dm);
2856:   DMGetWorkArray(dm, numCorners, MPIU_INT, &cone);
2857:   for (c = 0; c < numCells; ++c) {
2858:     for (p = 0; p < numCorners; ++p) {
2859:       cone[p] = cells[c*numCorners+p]+numCells;
2860:     }
2861:     if (invertCells) { DMPlexInvertCell_Internal(spaceDim, numCorners, cone); }
2862:     DMPlexSetCone(dm, c, cone);
2863:   }
2864:   DMRestoreWorkArray(dm, numCorners, MPIU_INT, &cone);
2865:   DMPlexSymmetrize(dm);
2866:   DMPlexStratify(dm);
2867:   return(0);
2868: }

2870: /*
2871:   This takes as input the coordinates for each vertex
2872: */
2873: PetscErrorCode DMPlexBuildCoordinates_Internal(DM dm, PetscInt spaceDim, PetscInt numCells, PetscInt numVertices, const double vertexCoords[])
2874: {
2875:   PetscSection   coordSection;
2876:   Vec            coordinates;
2877:   DM             cdm;
2878:   PetscScalar   *coords;
2879:   PetscInt       v, d;

2883:   DMSetCoordinateDim(dm, spaceDim);
2884:   DMGetCoordinateSection(dm, &coordSection);
2885:   PetscSectionSetNumFields(coordSection, 1);
2886:   PetscSectionSetFieldComponents(coordSection, 0, spaceDim);
2887:   PetscSectionSetChart(coordSection, numCells, numCells + numVertices);
2888:   for (v = numCells; v < numCells+numVertices; ++v) {
2889:     PetscSectionSetDof(coordSection, v, spaceDim);
2890:     PetscSectionSetFieldDof(coordSection, v, 0, spaceDim);
2891:   }
2892:   PetscSectionSetUp(coordSection);

2894:   DMGetCoordinateDM(dm, &cdm);
2895:   DMCreateLocalVector(cdm, &coordinates);
2896:   VecSetBlockSize(coordinates, spaceDim);
2897:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
2898:   VecGetArray(coordinates, &coords);
2899:   for (v = 0; v < numVertices; ++v) {
2900:     for (d = 0; d < spaceDim; ++d) {
2901:       coords[v*spaceDim+d] = vertexCoords[v*spaceDim+d];
2902:     }
2903:   }
2904:   VecRestoreArray(coordinates, &coords);
2905:   DMSetCoordinatesLocal(dm, coordinates);
2906:   VecDestroy(&coordinates);
2907:   return(0);
2908: }

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

2913:   Input Parameters:
2914: + comm - The communicator
2915: . dim - The topological dimension of the mesh
2916: . numCells - The number of cells
2917: . numVertices - The number of vertices
2918: . numCorners - The number of vertices for each cell
2919: . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
2920: . cells - An array of numCells*numCorners numbers, the vertices for each cell
2921: . spaceDim - The spatial dimension used for coordinates
2922: - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex

2924:   Output Parameter:
2925: . dm - The DM

2927:   Note: Two triangles sharing a face
2928: $
2929: $        2
2930: $      / | \
2931: $     /  |  \
2932: $    /   |   \
2933: $   0  0 | 1  3
2934: $    \   |   /
2935: $     \  |  /
2936: $      \ | /
2937: $        1
2938: would have input
2939: $  numCells = 2, numVertices = 4
2940: $  cells = [0 1 2  1 3 2]
2941: $
2942: which would result in the DMPlex
2943: $
2944: $        4
2945: $      / | \
2946: $     /  |  \
2947: $    /   |   \
2948: $   2  0 | 1  5
2949: $    \   |   /
2950: $     \  |  /
2951: $      \ | /
2952: $        3

2954:   Level: beginner

2956: .seealso: DMPlexCreateFromDAG(), DMPlexCreate()
2957: @*/
2958: 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)
2959: {

2963:   DMCreate(comm, dm);
2964:   DMSetType(*dm, DMPLEX);
2965:   DMSetDimension(*dm, dim);
2966:   DMPlexBuildFromCellList_Internal(*dm, spaceDim, numCells, numVertices, numCorners, cells, PETSC_FALSE);
2967:   if (interpolate) {
2968:     DM idm;

2970:     DMPlexInterpolate(*dm, &idm);
2971:     DMDestroy(dm);
2972:     *dm  = idm;
2973:   }
2974:   DMPlexBuildCoordinates_Internal(*dm, spaceDim, numCells, numVertices, vertexCoords);
2975:   return(0);
2976: }

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

2981:   Input Parameters:
2982: + dm - The empty DM object, usually from DMCreate() and DMSetDimension()
2983: . depth - The depth of the DAG
2984: . numPoints - Array of size depth + 1 containing the number of points at each depth
2985: . coneSize - The cone size of each point
2986: . cones - The concatenation of the cone points for each point, the cone list must be oriented correctly for each point
2987: . coneOrientations - The orientation of each cone point
2988: - vertexCoords - An array of numPoints[0]*spacedim numbers representing the coordinates of each vertex, with spacedim the value set via DMSetCoordinateDim()

2990:   Output Parameter:
2991: . dm - The DM

2993:   Note: Two triangles sharing a face would have input
2994: $  depth = 1, numPoints = [4 2], coneSize = [3 3 0 0 0 0]
2995: $  cones = [2 3 4  3 5 4], coneOrientations = [0 0 0  0 0 0]
2996: $ vertexCoords = [-1.0 0.0  0.0 -1.0  0.0 1.0  1.0 0.0]
2997: $
2998: which would result in the DMPlex
2999: $
3000: $        4
3001: $      / | \
3002: $     /  |  \
3003: $    /   |   \
3004: $   2  0 | 1  5
3005: $    \   |   /
3006: $     \  |  /
3007: $      \ | /
3008: $        3
3009: $
3010: $ Notice that all points are numbered consecutively, unlikely DMPlexCreateFromCellList()

3012:   Level: advanced

3014: .seealso: DMPlexCreateFromCellList(), DMPlexCreate()
3015: @*/
3016: PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[])
3017: {
3018:   Vec            coordinates;
3019:   PetscSection   coordSection;
3020:   PetscScalar    *coords;
3021:   PetscInt       coordSize, firstVertex = -1, pStart = 0, pEnd = 0, p, v, dim, dimEmbed, d, off;

3025:   DMGetDimension(dm, &dim);
3026:   DMGetCoordinateDim(dm, &dimEmbed);
3027:   if (dimEmbed < dim) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Embedding dimension %d cannot be less than intrinsic dimension %d",dimEmbed,dim);
3028:   for (d = 0; d <= depth; ++d) pEnd += numPoints[d];
3029:   DMPlexSetChart(dm, pStart, pEnd);
3030:   for (p = pStart; p < pEnd; ++p) {
3031:     DMPlexSetConeSize(dm, p, coneSize[p-pStart]);
3032:     if (firstVertex < 0 && !coneSize[p - pStart]) {
3033:       firstVertex = p - pStart;
3034:     }
3035:   }
3036:   if (firstVertex < 0 && numPoints[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected %d vertices but could not find any", numPoints[0]);
3037:   DMSetUp(dm); /* Allocate space for cones */
3038:   for (p = pStart, off = 0; p < pEnd; off += coneSize[p-pStart], ++p) {
3039:     DMPlexSetCone(dm, p, &cones[off]);
3040:     DMPlexSetConeOrientation(dm, p, &coneOrientations[off]);
3041:   }
3042:   DMPlexSymmetrize(dm);
3043:   DMPlexStratify(dm);
3044:   /* Build coordinates */
3045:   DMGetCoordinateSection(dm, &coordSection);
3046:   PetscSectionSetNumFields(coordSection, 1);
3047:   PetscSectionSetFieldComponents(coordSection, 0, dimEmbed);
3048:   PetscSectionSetChart(coordSection, firstVertex, firstVertex+numPoints[0]);
3049:   for (v = firstVertex; v < firstVertex+numPoints[0]; ++v) {
3050:     PetscSectionSetDof(coordSection, v, dimEmbed);
3051:     PetscSectionSetFieldDof(coordSection, v, 0, dimEmbed);
3052:   }
3053:   PetscSectionSetUp(coordSection);
3054:   PetscSectionGetStorageSize(coordSection, &coordSize);
3055:   VecCreate(PETSC_COMM_SELF, &coordinates);
3056:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
3057:   VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
3058:   VecSetBlockSize(coordinates, dimEmbed);
3059:   VecSetType(coordinates,VECSTANDARD);
3060:   VecGetArray(coordinates, &coords);
3061:   for (v = 0; v < numPoints[0]; ++v) {
3062:     PetscInt off;

3064:     PetscSectionGetOffset(coordSection, v+firstVertex, &off);
3065:     for (d = 0; d < dimEmbed; ++d) {
3066:       coords[off+d] = vertexCoords[v*dimEmbed+d];
3067:     }
3068:   }
3069:   VecRestoreArray(coordinates, &coords);
3070:   DMSetCoordinatesLocal(dm, coordinates);
3071:   VecDestroy(&coordinates);
3072:   return(0);
3073: }

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

3078: + comm        - The MPI communicator
3079: . filename    - Name of the .dat file
3080: - interpolate - Create faces and edges in the mesh

3082:   Output Parameter:
3083: . dm  - The DM object representing the mesh

3085:   Note: The format is the simplest possible:
3086: $ Ne
3087: $ v0 v1 ... vk
3088: $ Nv
3089: $ x y z marker

3091:   Level: beginner

3093: .seealso: DMPlexCreateFromFile(), DMPlexCreateMedFromFile(), DMPlexCreateGmsh(), DMPlexCreate()
3094: @*/
3095: PetscErrorCode DMPlexCreateCellVertexFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
3096: {
3097:   DMLabel         marker;
3098:   PetscViewer     viewer;
3099:   Vec             coordinates;
3100:   PetscSection    coordSection;
3101:   PetscScalar    *coords;
3102:   char            line[PETSC_MAX_PATH_LEN];
3103:   PetscInt        dim = 3, cdim = 3, coordSize, v, c, d;
3104:   PetscMPIInt     rank;
3105:   int             snum, Nv, Nc;
3106:   PetscErrorCode  ierr;

3109:   MPI_Comm_rank(comm, &rank);
3110:   PetscViewerCreate(comm, &viewer);
3111:   PetscViewerSetType(viewer, PETSCVIEWERASCII);
3112:   PetscViewerFileSetMode(viewer, FILE_MODE_READ);
3113:   PetscViewerFileSetName(viewer, filename);
3114:   if (!rank) {
3115:     PetscViewerRead(viewer, line, 2, NULL, PETSC_STRING);
3116:     snum = sscanf(line, "%d %d", &Nc, &Nv);
3117:     if (snum != 2) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
3118:   } else {
3119:     Nc = Nv = 0;
3120:   }
3121:   DMCreate(comm, dm);
3122:   DMSetType(*dm, DMPLEX);
3123:   DMPlexSetChart(*dm, 0, Nc+Nv);
3124:   DMSetDimension(*dm, dim);
3125:   DMSetCoordinateDim(*dm, cdim);
3126:   /* Read topology */
3127:   if (!rank) {
3128:     PetscInt cone[8], corners = 8;
3129:     int      vbuf[8], v;

3131:     for (c = 0; c < Nc; ++c) {DMPlexSetConeSize(*dm, c, corners);}
3132:     DMSetUp(*dm);
3133:     for (c = 0; c < Nc; ++c) {
3134:       PetscViewerRead(viewer, line, corners, NULL, PETSC_STRING);
3135:       snum = sscanf(line, "%d %d %d %d %d %d %d %d", &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5], &vbuf[6], &vbuf[7]);
3136:       if (snum != corners) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
3137:       for (v = 0; v < corners; ++v) cone[v] = vbuf[v] + Nc;
3138:       /* Hexahedra are inverted */
3139:       {
3140:         PetscInt tmp = cone[1];
3141:         cone[1] = cone[3];
3142:         cone[3] = tmp;
3143:       }
3144:       DMPlexSetCone(*dm, c, cone);
3145:     }
3146:   }
3147:   DMPlexSymmetrize(*dm);
3148:   DMPlexStratify(*dm);
3149:   /* Read coordinates */
3150:   DMGetCoordinateSection(*dm, &coordSection);
3151:   PetscSectionSetNumFields(coordSection, 1);
3152:   PetscSectionSetFieldComponents(coordSection, 0, cdim);
3153:   PetscSectionSetChart(coordSection, Nc, Nc + Nv);
3154:   for (v = Nc; v < Nc+Nv; ++v) {
3155:     PetscSectionSetDof(coordSection, v, cdim);
3156:     PetscSectionSetFieldDof(coordSection, v, 0, cdim);
3157:   }
3158:   PetscSectionSetUp(coordSection);
3159:   PetscSectionGetStorageSize(coordSection, &coordSize);
3160:   VecCreate(PETSC_COMM_SELF, &coordinates);
3161:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
3162:   VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
3163:   VecSetBlockSize(coordinates, cdim);
3164:   VecSetType(coordinates, VECSTANDARD);
3165:   VecGetArray(coordinates, &coords);
3166:   if (!rank) {
3167:     double x[3];
3168:     int    val;

3170:     DMCreateLabel(*dm, "marker");
3171:     DMGetLabel(*dm, "marker", &marker);
3172:     for (v = 0; v < Nv; ++v) {
3173:       PetscViewerRead(viewer, line, 4, NULL, PETSC_STRING);
3174:       snum = sscanf(line, "%lg %lg %lg %d", &x[0], &x[1], &x[2], &val);
3175:       if (snum != 4) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
3176:       for (d = 0; d < cdim; ++d) coords[v*cdim+d] = x[d];
3177:       if (val) {DMLabelSetValue(marker, v+Nc, val);}
3178:     }
3179:   }
3180:   VecRestoreArray(coordinates, &coords);
3181:   DMSetCoordinatesLocal(*dm, coordinates);
3182:   VecDestroy(&coordinates);
3183:   PetscViewerDestroy(&viewer);
3184:   if (interpolate) {
3185:     DM      idm;
3186:     DMLabel bdlabel;

3188:     DMPlexInterpolate(*dm, &idm);
3189:     DMDestroy(dm);
3190:     *dm  = idm;

3192:     DMGetLabel(*dm, "marker", &bdlabel);
3193:     DMPlexMarkBoundaryFaces(*dm, PETSC_DETERMINE, bdlabel);
3194:     DMPlexLabelComplete(*dm, bdlabel);
3195:   }
3196:   return(0);
3197: }

3199: /*@C
3200:   DMPlexCreateFromFile - This takes a filename and produces a DM

3202:   Input Parameters:
3203: + comm - The communicator
3204: . filename - A file name
3205: - interpolate - Flag to create intermediate mesh pieces (edges, faces)

3207:   Output Parameter:
3208: . dm - The DM

3210:   Options Database Keys:
3211: . -dm_plex_create_from_hdf5_xdmf - use the PETSC_VIEWER_HDF5_XDMF format for reading HDF5

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

3216:   Level: beginner

3218: .seealso: DMPlexCreateFromDAG(), DMPlexCreateFromCellList(), DMPlexCreate()
3219: @*/
3220: PetscErrorCode DMPlexCreateFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
3221: {
3222:   const char    *extGmsh    = ".msh";
3223:   const char    *extGmsh2   = ".msh2";
3224:   const char    *extGmsh4   = ".msh4";
3225:   const char    *extCGNS    = ".cgns";
3226:   const char    *extExodus  = ".exo";
3227:   const char    *extGenesis = ".gen";
3228:   const char    *extFluent  = ".cas";
3229:   const char    *extHDF5    = ".h5";
3230:   const char    *extMed     = ".med";
3231:   const char    *extPLY     = ".ply";
3232:   const char    *extCV      = ".dat";
3233:   size_t         len;
3234:   PetscBool      isGmsh, isGmsh2, isGmsh4, isCGNS, isExodus, isGenesis, isFluent, isHDF5, isMed, isPLY, isCV;
3235:   PetscMPIInt    rank;

3241:   MPI_Comm_rank(comm, &rank);
3242:   PetscStrlen(filename, &len);
3243:   if (!len) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Filename must be a valid path");
3244:   PetscStrncmp(&filename[PetscMax(0,len-4)], extGmsh,    4, &isGmsh);
3245:   PetscStrncmp(&filename[PetscMax(0,len-5)], extGmsh2,   5, &isGmsh2);
3246:   PetscStrncmp(&filename[PetscMax(0,len-5)], extGmsh4,   5, &isGmsh4);
3247:   PetscStrncmp(&filename[PetscMax(0,len-5)], extCGNS,    5, &isCGNS);
3248:   PetscStrncmp(&filename[PetscMax(0,len-4)], extExodus,  4, &isExodus);
3249:   PetscStrncmp(&filename[PetscMax(0,len-4)], extGenesis, 4, &isGenesis);
3250:   PetscStrncmp(&filename[PetscMax(0,len-4)], extFluent,  4, &isFluent);
3251:   PetscStrncmp(&filename[PetscMax(0,len-3)], extHDF5,    3, &isHDF5);
3252:   PetscStrncmp(&filename[PetscMax(0,len-4)], extMed,     4, &isMed);
3253:   PetscStrncmp(&filename[PetscMax(0,len-4)], extPLY,     4, &isPLY);
3254:   PetscStrncmp(&filename[PetscMax(0,len-4)], extCV,      4, &isCV);
3255:   if (isGmsh || isGmsh2 || isGmsh4) {
3256:     DMPlexCreateGmshFromFile(comm, filename, interpolate, dm);
3257:   } else if (isCGNS) {
3258:     DMPlexCreateCGNSFromFile(comm, filename, interpolate, dm);
3259:   } else if (isExodus || isGenesis) {
3260:     DMPlexCreateExodusFromFile(comm, filename, interpolate, dm);
3261:   } else if (isFluent) {
3262:     DMPlexCreateFluentFromFile(comm, filename, interpolate, dm);
3263:   } else if (isHDF5) {
3264:     PetscBool      load_hdf5_xdmf = PETSC_FALSE;
3265:     PetscViewer viewer;

3267:     /* PETSC_VIEWER_HDF5_XDMF is used if the filename ends with .xdmf.h5, or if -dm_plex_create_from_hdf5_xdmf option is present */
3268:     PetscStrncmp(&filename[PetscMax(0,len-8)], ".xdmf",  5, &load_hdf5_xdmf);
3269:     PetscOptionsGetBool(NULL, NULL, "-dm_plex_create_from_hdf5_xdmf", &load_hdf5_xdmf, NULL);
3270:     PetscViewerCreate(comm, &viewer);
3271:     PetscViewerSetType(viewer, PETSCVIEWERHDF5);
3272:     PetscViewerSetOptionsPrefix(viewer, "dm_plex_create_");
3273:     PetscViewerSetFromOptions(viewer);
3274:     PetscViewerFileSetMode(viewer, FILE_MODE_READ);
3275:     PetscViewerFileSetName(viewer, filename);
3276:     DMCreate(comm, dm);
3277:     DMSetType(*dm, DMPLEX);
3278:     if (load_hdf5_xdmf) {PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_XDMF);}
3279:     DMLoad(*dm, viewer);
3280:     if (load_hdf5_xdmf) {PetscViewerPopFormat(viewer);}
3281:     PetscViewerDestroy(&viewer);

3283:     if (interpolate) {
3284:       DM idm;

3286:       DMPlexInterpolate(*dm, &idm);
3287:       DMDestroy(dm);
3288:       *dm  = idm;
3289:     }
3290:   } else if (isMed) {
3291:     DMPlexCreateMedFromFile(comm, filename, interpolate, dm);
3292:   } else if (isPLY) {
3293:     DMPlexCreatePLYFromFile(comm, filename, interpolate, dm);
3294:   } else if (isCV) {
3295:     DMPlexCreateCellVertexFromFile(comm, filename, interpolate, dm);
3296:   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename);
3297:   return(0);
3298: }

3300: /*@
3301:   DMPlexCreateReferenceCell - Create a DMPLEX with the appropriate FEM reference cell

3303:   Collective

3305:   Input Parameters:
3306: + comm    - The communicator
3307: . dim     - The spatial dimension
3308: - simplex - Flag for simplex, otherwise use a tensor-product cell

3310:   Output Parameter:
3311: . refdm - The reference cell

3313:   Level: intermediate

3315: .seealso:
3316: @*/
3317: PetscErrorCode DMPlexCreateReferenceCell(MPI_Comm comm, PetscInt dim, PetscBool simplex, DM *refdm)
3318: {
3319:   DM             rdm;
3320:   Vec            coords;

3324:   DMCreate(comm, &rdm);
3325:   DMSetType(rdm, DMPLEX);
3326:   DMSetDimension(rdm, dim);
3327:   switch (dim) {
3328:   case 0:
3329:   {
3330:     PetscInt    numPoints[1]        = {1};
3331:     PetscInt    coneSize[1]         = {0};
3332:     PetscInt    cones[1]            = {0};
3333:     PetscInt    coneOrientations[1] = {0};
3334:     PetscScalar vertexCoords[1]     = {0.0};

3336:     DMPlexCreateFromDAG(rdm, 0, numPoints, coneSize, cones, coneOrientations, vertexCoords);
3337:   }
3338:   break;
3339:   case 1:
3340:   {
3341:     PetscInt    numPoints[2]        = {2, 1};
3342:     PetscInt    coneSize[3]         = {2, 0, 0};
3343:     PetscInt    cones[2]            = {1, 2};
3344:     PetscInt    coneOrientations[2] = {0, 0};
3345:     PetscScalar vertexCoords[2]     = {-1.0,  1.0};

3347:     DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
3348:   }
3349:   break;
3350:   case 2:
3351:     if (simplex) {
3352:       PetscInt    numPoints[2]        = {3, 1};
3353:       PetscInt    coneSize[4]         = {3, 0, 0, 0};
3354:       PetscInt    cones[3]            = {1, 2, 3};
3355:       PetscInt    coneOrientations[3] = {0, 0, 0};
3356:       PetscScalar vertexCoords[6]     = {-1.0, -1.0,  1.0, -1.0,  -1.0, 1.0};

3358:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
3359:     } else {
3360:       PetscInt    numPoints[2]        = {4, 1};
3361:       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
3362:       PetscInt    cones[4]            = {1, 2, 3, 4};
3363:       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
3364:       PetscScalar vertexCoords[8]     = {-1.0, -1.0,  1.0, -1.0,  1.0, 1.0,  -1.0, 1.0};

3366:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
3367:     }
3368:   break;
3369:   case 3:
3370:     if (simplex) {
3371:       PetscInt    numPoints[2]        = {4, 1};
3372:       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
3373:       PetscInt    cones[4]            = {1, 3, 2, 4};
3374:       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
3375:       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};

3377:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
3378:     } else {
3379:       PetscInt    numPoints[2]        = {8, 1};
3380:       PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
3381:       PetscInt    cones[8]            = {1, 4, 3, 2, 5, 6, 7, 8};
3382:       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
3383:       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,
3384:                                          -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  1.0, 1.0,  1.0,  -1.0, 1.0,  1.0};

3386:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
3387:     }
3388:   break;
3389:   default:
3390:     SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Cannot create reference cell for dimension %d", dim);
3391:   }
3392:   DMPlexInterpolate(rdm, refdm);
3393:   if (rdm->coordinateDM) {
3394:     DM           ncdm;
3395:     PetscSection cs;
3396:     PetscInt     pEnd = -1;

3398:     DMGetLocalSection(rdm->coordinateDM, &cs);
3399:     if (cs) {PetscSectionGetChart(cs, NULL, &pEnd);}
3400:     if (pEnd >= 0) {
3401:       DMClone(*refdm, &ncdm);
3402:       DMCopyDisc(rdm->coordinateDM, ncdm);
3403:       DMSetLocalSection(ncdm, cs);
3404:       DMSetCoordinateDM(*refdm, ncdm);
3405:       DMDestroy(&ncdm);
3406:     }
3407:   }
3408:   DMGetCoordinatesLocal(rdm, &coords);
3409:   if (coords) {
3410:     DMSetCoordinatesLocal(*refdm, coords);
3411:   } else {
3412:     DMGetCoordinates(rdm, &coords);
3413:     if (coords) {DMSetCoordinates(*refdm, coords);}
3414:   }
3415:   DMDestroy(&rdm);
3416:   return(0);
3417: }