Actual source code: plexcreate.c

petsc-3.9.4 2018-09-11
Report Typos and Errors
  1: #define PETSCDM_DLL
  2:  #include <petsc/private/dmpleximpl.h>
  3:  #include <petscdmda.h>
  4:  #include <petscsf.h>

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

  9:   Collective on MPI_Comm

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

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

 22:   Level: beginner

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

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

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

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

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

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

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

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

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

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

149:   Collective on MPI_Comm

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

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

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

171:   Level: beginner

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

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

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

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

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

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

296:   Collective on MPI_Comm

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

304:   Output Parameter:
305: . dm  - The DM object

307:   Level: beginner

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

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

333:     DMPlexSetChart(dm, 0, numFaces+numVertices);
334:     for (f = 0; f < numFaces; ++f) {
335:       DMPlexSetConeSize(dm, f, 4);
336:     }
337:     DMSetUp(dm); /* Allocate space for cones */
338:     for (v = 0; v < numFaces+numVertices; ++v) {
339:       DMSetLabelValue(dm, "marker", v, 1);
340:     }

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

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

362:     /* Side 2 (Front) */
363:     for (vz = 0; vz < faces[2]; vz++) {
364:       for (vx = 0; vx < faces[0]; vx++) {
365:         voffset = numFaces + vz*vertices[0]*vertices[1] + vx;
366:         cone[0] = voffset; cone[1] = voffset+1; cone[2] = voffset+vertices[0]*vertices[1]+1; cone[3] = voffset+vertices[0]*vertices[1];
367:         DMPlexSetCone(dm, iface, cone);
368:         iface++;
369:       }
370:     }

372:     /* Side 3 (Back) */
373:     for (vz = 0; vz < faces[2]; vz++) {
374:       for (vx = 0; vx < faces[0]; vx++) {
375:         voffset = numFaces + vz*vertices[0]*vertices[1] + vertices[0]*(vertices[1]-1) + vx;
376:         cone[0] = voffset+vertices[0]*vertices[1]; cone[1] = voffset+vertices[0]*vertices[1]+1;
377:         cone[2] = voffset+1; cone[3] = voffset;
378:         DMPlexSetCone(dm, iface, cone);
379:         iface++;
380:       }
381:     }

383:     /* Side 4 (Left) */
384:     for (vz = 0; vz < faces[2]; vz++) {
385:       for (vy = 0; vy < faces[1]; vy++) {
386:         voffset = numFaces + vz*vertices[0]*vertices[1] + vy*vertices[0];
387:         cone[0] = voffset; cone[1] = voffset+vertices[0]*vertices[1];
388:         cone[2] = voffset+vertices[0]*vertices[1]+vertices[0]; cone[3] = voffset+vertices[0];
389:         DMPlexSetCone(dm, iface, cone);
390:         iface++;
391:       }
392:     }

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

437: static PetscErrorCode DMPlexCreateLineMesh_Internal(MPI_Comm comm,PetscInt segments,PetscReal lower,PetscReal upper,DMBoundaryType bd,DM *dm)
438: {
439:   PetscInt       i,fStart,fEnd,numCells = 0,numVerts = 0;
440:   PetscInt       numPoints[2],*coneSize,*cones,*coneOrientations;
441:   PetscScalar    *vertexCoords;
442:   PetscReal      L,maxCell;
443:   PetscBool      markerSeparate = PETSC_FALSE;
444:   PetscInt       markerLeft  = 1, faceMarkerLeft  = 1;
445:   PetscInt       markerRight = 1, faceMarkerRight = 2;
446:   PetscBool      wrap = (bd == DM_BOUNDARY_PERIODIC || bd == DM_BOUNDARY_TWIST) ? PETSC_TRUE : PETSC_FALSE;
447:   PetscMPIInt    rank;


453:   DMCreate(comm,dm);
454:   DMSetType(*dm,DMPLEX);
455:   DMSetDimension(*dm,1);
456:   DMCreateLabel(*dm,"marker");
457:   DMCreateLabel(*dm,"Face Sets");

459:   MPI_Comm_rank(comm,&rank);
460:   if (!rank) numCells = segments;
461:   if (!rank) numVerts = segments + (wrap ? 0 : 1);

463:   numPoints[0] = numVerts ; numPoints[1] = numCells;
464:   PetscMalloc4(numCells+numVerts,&coneSize,numCells*2,&cones,numCells+numVerts,&coneOrientations,numVerts,&vertexCoords);
465:   PetscMemzero(coneOrientations,(numCells+numVerts)*sizeof(PetscInt));
466:   for (i = 0; i < numCells; ++i) { coneSize[i] = 2; }
467:   for (i = 0; i < numVerts; ++i) { coneSize[numCells+i] = 0; }
468:   for (i = 0; i < numCells; ++i) { cones[2*i] = numCells + i%numVerts; cones[2*i+1] = numCells + (i+1)%numVerts; }
469:   for (i = 0; i < numVerts; ++i) { vertexCoords[i] = lower + (upper-lower)*((PetscReal)i/(PetscReal)numCells); }
470:   DMPlexCreateFromDAG(*dm,1,numPoints,coneSize,cones,coneOrientations,vertexCoords);
471:   PetscFree4(coneSize,cones,coneOrientations,vertexCoords);

473:   PetscOptionsGetBool(((PetscObject)*dm)->options,((PetscObject)*dm)->prefix,"-dm_plex_separate_marker",&markerSeparate,NULL);
474:   if (markerSeparate) { markerLeft = faceMarkerLeft; markerRight = faceMarkerRight;}
475:   if (!wrap && !rank) {
476:     DMPlexGetHeightStratum(*dm,1,&fStart,&fEnd);
477:     DMSetLabelValue(*dm,"marker",fStart,markerLeft);
478:     DMSetLabelValue(*dm,"marker",fEnd-1,markerRight);
479:     DMSetLabelValue(*dm,"Face Sets",fStart,faceMarkerLeft);
480:     DMSetLabelValue(*dm,"Face Sets",fEnd-1,faceMarkerRight);
481:   }
482:   if (wrap) {
483:     L       = upper - lower;
484:     maxCell = (PetscReal)1.1*(L/(PetscReal)PetscMax(1,segments));
485:     DMSetPeriodicity(*dm,PETSC_TRUE,&maxCell,&L,&bd);
486:   }
487:   return(0);
488: }

490: 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)
491: {
492:   DM             boundary;
493:   PetscInt       i;

498:   for (i = 0; i < dim; ++i) if (periodicity[i] != DM_BOUNDARY_NONE) SETERRQ(comm, PETSC_ERR_SUP, "Periodicity is not supported for simplex meshes");
499:   DMCreate(comm, &boundary);
501:   DMSetType(boundary, DMPLEX);
502:   DMSetDimension(boundary, dim-1);
503:   DMSetCoordinateDim(boundary, dim);
504:   switch (dim) {
505:   case 2: DMPlexCreateSquareBoundary(boundary, lower, upper, faces);break;
506:   case 3: DMPlexCreateCubeBoundary(boundary, lower, upper, faces);break;
507:   default: SETERRQ1(comm, PETSC_ERR_SUP, "Dimension not supported: %d", dim);
508:   }
509:   DMPlexGenerate(boundary, NULL, interpolate, dm);
510:   DMDestroy(&boundary);
511:   return(0);
512: }

514: static PetscErrorCode DMPlexCreateCubeMesh_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY, DMBoundaryType bdZ)
515: {
516:   DMLabel        cutLabel = NULL;
517:   PetscInt       markerTop      = 1, faceMarkerTop      = 1;
518:   PetscInt       markerBottom   = 1, faceMarkerBottom   = 1;
519:   PetscInt       markerFront    = 1, faceMarkerFront    = 1;
520:   PetscInt       markerBack     = 1, faceMarkerBack     = 1;
521:   PetscInt       markerRight    = 1, faceMarkerRight    = 1;
522:   PetscInt       markerLeft     = 1, faceMarkerLeft     = 1;
523:   PetscInt       dim;
524:   PetscBool      markerSeparate = PETSC_FALSE, cutMarker = PETSC_FALSE;
525:   PetscMPIInt    rank;

529:   DMGetDimension(dm,&dim);
530:   MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
531:   DMCreateLabel(dm,"marker");
532:   DMCreateLabel(dm,"Face Sets");
533:   PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL);
534:   if (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ||
535:       bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ||
536:       bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST) {

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

602:     DMPlexSetChart(dm, 0, numCells+numFaces+numEdges+numVertices);
603:     for (c = 0; c < numCells; c++) {
604:       DMPlexSetConeSize(dm, c, 6);
605:     }
606:     for (f = firstXFace; f < firstXFace+numFaces; ++f) {
607:       DMPlexSetConeSize(dm, f, 4);
608:     }
609:     for (e = firstXEdge; e < firstXEdge+numEdges; ++e) {
610:       DMPlexSetConeSize(dm, e, 2);
611:     }
612:     DMSetUp(dm); /* Allocate space for cones */
613:     /* Build cells */
614:     for (fz = 0; fz < numZEdges; ++fz) {
615:       for (fy = 0; fy < numYEdges; ++fy) {
616:         for (fx = 0; fx < numXEdges; ++fx) {
617:           PetscInt cell    = (fz*numYEdges + fy)*numXEdges + fx;
618:           PetscInt faceB   = firstZFace + (fy*numXEdges+fx)*numZVertices +   fz;
619:           PetscInt faceT   = firstZFace + (fy*numXEdges+fx)*numZVertices + ((fz+1)%numZVertices);
620:           PetscInt faceF   = firstYFace + (fz*numXEdges+fx)*numYVertices +   fy;
621:           PetscInt faceK   = firstYFace + (fz*numXEdges+fx)*numYVertices + ((fy+1)%numYVertices);
622:           PetscInt faceL   = firstXFace + (fz*numYEdges+fy)*numXVertices +   fx;
623:           PetscInt faceR   = firstXFace + (fz*numYEdges+fy)*numXVertices + ((fx+1)%numXVertices);
624:                             /* B,  T,  F,  K,  R,  L */
625:           PetscInt ornt[6] = {-4,  0,  0, -1,  0, -4}; /* ??? */
626:           PetscInt cone[6];

628:           /* no boundary twisting in 3D */
629:           cone[0] = faceB; cone[1] = faceT; cone[2] = faceF; cone[3] = faceK; cone[4] = faceR; cone[5] = faceL;
630:           DMPlexSetCone(dm, cell, cone);
631:           DMPlexSetConeOrientation(dm, cell, ornt);
632:           if (bdX != DM_BOUNDARY_NONE && fx == numXEdges-1 && cutLabel) {DMLabelSetValue(cutLabel, cell, 2);}
633:           if (bdY != DM_BOUNDARY_NONE && fy == numYEdges-1 && cutLabel) {DMLabelSetValue(cutLabel, cell, 2);}
634:           if (bdZ != DM_BOUNDARY_NONE && fz == numZEdges-1 && cutLabel) {DMLabelSetValue(cutLabel, cell, 2);}
635:         }
636:       }
637:     }
638:     /* Build x faces */
639:     for (fz = 0; fz < numZEdges; ++fz) {
640:       for (fy = 0; fy < numYEdges; ++fy) {
641:         for (fx = 0; fx < numXVertices; ++fx) {
642:           PetscInt face    = firstXFace + (fz*numYEdges+fy)*numXVertices + fx;
643:           PetscInt edgeL   = firstZEdge + (  fy*                 numXVertices+fx)*numZEdges + fz;
644:           PetscInt edgeR   = firstZEdge + (((fy+1)%numYVertices)*numXVertices+fx)*numZEdges + fz;
645:           PetscInt edgeB   = firstYEdge + (  fz*                 numXVertices+fx)*numYEdges + fy;
646:           PetscInt edgeT   = firstYEdge + (((fz+1)%numZVertices)*numXVertices+fx)*numYEdges + fy;
647:           PetscInt ornt[4] = {0, 0, -2, -2};
648:           PetscInt cone[4];

650:           if (dim == 3) {
651:             /* markers */
652:             if (bdX != DM_BOUNDARY_PERIODIC) {
653:               if (fx == numXVertices-1) {
654:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerRight);
655:                 DMSetLabelValue(dm, "marker", face, markerRight);
656:               }
657:               else if (fx == 0) {
658:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerLeft);
659:                 DMSetLabelValue(dm, "marker", face, markerLeft);
660:               }
661:             }
662:           }
663:           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
664:           DMPlexSetCone(dm, face, cone);
665:           DMPlexSetConeOrientation(dm, face, ornt);
666:         }
667:       }
668:     }
669:     /* Build y faces */
670:     for (fz = 0; fz < numZEdges; ++fz) {
671:       for (fx = 0; fx < numXEdges; ++fx) {
672:         for (fy = 0; fy < numYVertices; ++fy) {
673:           PetscInt face    = firstYFace + (fz*numXEdges+fx)*numYVertices + fy;
674:           PetscInt edgeL   = firstZEdge + (fy*numXVertices+  fx                 )*numZEdges + fz;
675:           PetscInt edgeR   = firstZEdge + (fy*numXVertices+((fx+1)%numXVertices))*numZEdges + fz;
676:           PetscInt edgeB   = firstXEdge + (  fz                 *numYVertices+fy)*numXEdges + fx;
677:           PetscInt edgeT   = firstXEdge + (((fz+1)%numZVertices)*numYVertices+fy)*numXEdges + fx;
678:           PetscInt ornt[4] = {0, 0, -2, -2};
679:           PetscInt cone[4];

681:           if (dim == 3) {
682:             /* markers */
683:             if (bdY != DM_BOUNDARY_PERIODIC) {
684:               if (fy == numYVertices-1) {
685:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerBack);
686:                 DMSetLabelValue(dm, "marker", face, markerBack);
687:               }
688:               else if (fy == 0) {
689:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerFront);
690:                 DMSetLabelValue(dm, "marker", face, markerFront);
691:               }
692:             }
693:           }
694:           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
695:           DMPlexSetCone(dm, face, cone);
696:           DMPlexSetConeOrientation(dm, face, ornt);
697:         }
698:       }
699:     }
700:     /* Build z faces */
701:     for (fy = 0; fy < numYEdges; ++fy) {
702:       for (fx = 0; fx < numXEdges; ++fx) {
703:         for (fz = 0; fz < numZVertices; fz++) {
704:           PetscInt face    = firstZFace + (fy*numXEdges+fx)*numZVertices + fz;
705:           PetscInt edgeL   = firstYEdge + (fz*numXVertices+  fx                 )*numYEdges + fy;
706:           PetscInt edgeR   = firstYEdge + (fz*numXVertices+((fx+1)%numXVertices))*numYEdges + fy;
707:           PetscInt edgeB   = firstXEdge + (fz*numYVertices+  fy                 )*numXEdges + fx;
708:           PetscInt edgeT   = firstXEdge + (fz*numYVertices+((fy+1)%numYVertices))*numXEdges + fx;
709:           PetscInt ornt[4] = {0, 0, -2, -2};
710:           PetscInt cone[4];

712:           if (dim == 2) {
713:             if (bdX == DM_BOUNDARY_TWIST && fx == numXEdges-1) {edgeR += numYEdges-1-2*fy; ornt[1] = -2;}
714:             if (bdY == DM_BOUNDARY_TWIST && fy == numYEdges-1) {edgeT += numXEdges-1-2*fx; ornt[2] =  0;}
715:             if (bdX != DM_BOUNDARY_NONE && fx == numXEdges-1 && cutLabel) {DMLabelSetValue(cutLabel, face, 2);}
716:             if (bdY != DM_BOUNDARY_NONE && fy == numYEdges-1 && cutLabel) {DMLabelSetValue(cutLabel, face, 2);}
717:           } else {
718:             /* markers */
719:             if (bdZ != DM_BOUNDARY_PERIODIC) {
720:               if (fz == numZVertices-1) {
721:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerTop);
722:                 DMSetLabelValue(dm, "marker", face, markerTop);
723:               }
724:               else if (fz == 0) {
725:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerBottom);
726:                 DMSetLabelValue(dm, "marker", face, markerBottom);
727:               }
728:             }
729:           }
730:           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
731:           DMPlexSetCone(dm, face, cone);
732:           DMPlexSetConeOrientation(dm, face, ornt);
733:         }
734:       }
735:     }
736:     /* Build Z edges*/
737:     for (vy = 0; vy < numYVertices; vy++) {
738:       for (vx = 0; vx < numXVertices; vx++) {
739:         for (ez = 0; ez < numZEdges; ez++) {
740:           const PetscInt edge    = firstZEdge  + (vy*numXVertices+vx)*numZEdges + ez;
741:           const PetscInt vertexB = firstVertex + (  ez                 *numYVertices+vy)*numXVertices + vx;
742:           const PetscInt vertexT = firstVertex + (((ez+1)%numZVertices)*numYVertices+vy)*numXVertices + vx;
743:           PetscInt       cone[2];

745:           if (dim == 3) {
746:             if (bdX != DM_BOUNDARY_PERIODIC) {
747:               if (vx == numXVertices-1) {
748:                 DMSetLabelValue(dm, "marker", edge, markerRight);
749:               }
750:               else if (vx == 0) {
751:                 DMSetLabelValue(dm, "marker", edge, markerLeft);
752:               }
753:             }
754:             if (bdY != DM_BOUNDARY_PERIODIC) {
755:               if (vy == numYVertices-1) {
756:                 DMSetLabelValue(dm, "marker", edge, markerBack);
757:               }
758:               else if (vy == 0) {
759:                 DMSetLabelValue(dm, "marker", edge, markerFront);
760:               }
761:             }
762:           }
763:           cone[0] = vertexB; cone[1] = vertexT;
764:           DMPlexSetCone(dm, edge, cone);
765:         }
766:       }
767:     }
768:     /* Build Y edges*/
769:     for (vz = 0; vz < numZVertices; vz++) {
770:       for (vx = 0; vx < numXVertices; vx++) {
771:         for (ey = 0; ey < numYEdges; ey++) {
772:           const PetscInt nextv   = (dim == 2 && bdY == DM_BOUNDARY_TWIST && ey == numYEdges-1) ? (numXVertices-vx-1) : (vz*numYVertices+((ey+1)%numYVertices))*numXVertices + vx;
773:           const PetscInt edge    = firstYEdge  + (vz*numXVertices+vx)*numYEdges + ey;
774:           const PetscInt vertexF = firstVertex + (vz*numYVertices+ey)*numXVertices + vx;
775:           const PetscInt vertexK = firstVertex + nextv;
776:           PetscInt       cone[2];

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

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

921: 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)
922: {
923:   PetscInt       i;

928:   DMCreate(comm, dm);
930:   DMSetType(*dm, DMPLEX);
931:   DMSetDimension(*dm, dim);
932:   switch (dim) {
933:   case 2: {DMPlexCreateCubeMesh_Internal(*dm, lower, upper, faces, periodicity[0], periodicity[1], DM_BOUNDARY_NONE);break;}
934:   case 3: {DMPlexCreateCubeMesh_Internal(*dm, lower, upper, faces, periodicity[0], periodicity[1], periodicity[2]);break;}
935:   default: SETERRQ1(comm, PETSC_ERR_SUP, "Dimension not supported: %d", dim);
936:   }
937:   if (periodicity[0] == DM_BOUNDARY_PERIODIC || periodicity[0] == DM_BOUNDARY_TWIST ||
938:       periodicity[1] == DM_BOUNDARY_PERIODIC || periodicity[1] == DM_BOUNDARY_TWIST ||
939:       (dim > 2 && (periodicity[2] == DM_BOUNDARY_PERIODIC || periodicity[2] == DM_BOUNDARY_TWIST))) {
940:     PetscReal L[3];
941:     PetscReal maxCell[3];

943:     for (i = 0; i < dim; i++) {
944:       L[i]       = upper[i] - lower[i];
945:       maxCell[i] = 1.1 * (L[i] / PetscMax(1,faces[i]));
946:     }
947:     DMSetPeriodicity(*dm,PETSC_TRUE,maxCell,L,periodicity);
948:   }
949:   if (!interpolate) {
950:     DM udm;

952:     DMPlexUninterpolate(*dm, &udm);
953:     DMPlexCopyCoordinates(*dm, udm);
954:     DMDestroy(dm);
955:     *dm  = udm;
956:   }
957:   return(0);
958: }

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

963:   Collective on MPI_Comm

965:   Input Parameters:
966: + comm        - The communicator for the DM object
967: . dim         - The spatial dimension
968: . simplex     - PETSC_TRUE for simplices, PETSC_FALSE for tensor cells
969: . faces       - Number of faces per dimension, or NULL for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
970: . lower       - The lower left corner, or NULL for (0, 0, 0)
971: . upper       - The upper right corner, or NULL for (1, 1, 1)
972: . periodicity - The boundary type for the X,Y,Z direction, or NULL for DM_BOUNDARY_NONE
973: - interpolate - Flag to create intermediate mesh pieces (edges, faces)

975:   Output Parameter:
976: . dm  - The DM object

978:   Note: Here is the numbering returned for 2 faces in each direction for tensor cells:
979: $ 10---17---11---18----12
980: $  |         |         |
981: $  |         |         |
982: $ 20    2   22    3    24
983: $  |         |         |
984: $  |         |         |
985: $  7---15----8---16----9
986: $  |         |         |
987: $  |         |         |
988: $ 19    0   21    1   23
989: $  |         |         |
990: $  |         |         |
991: $  4---13----5---14----6

993: and for simplicial cells

995: $ 14----8---15----9----16
996: $  |\     5  |\      7 |
997: $  | \       | \       |
998: $ 13   2    14    3    15
999: $  | 4   \   | 6   \   |
1000: $  |       \ |       \ |
1001: $ 11----6---12----7----13
1002: $  |\        |\        |
1003: $  | \    1  | \     3 |
1004: $ 10   0    11    1    12
1005: $  | 0   \   | 2   \   |
1006: $  |       \ |       \ |
1007: $  8----4----9----5----10

1009:   Level: beginner

1011: .keywords: DM, create
1012: .seealso: DMPlexCreateFromFile(), DMPlexCreateHexCylinderMesh(), DMSetType(), DMCreate()
1013: @*/
1014: 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)
1015: {
1016:   PetscInt       i;
1017:   PetscInt       fac[3] = {0, 0, 0};
1018:   PetscReal      low[3] = {0, 0, 0};
1019:   PetscReal      upp[3] = {1, 1, 1};
1020:   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};

1024:   for (i = 0; i < dim; ++i) fac[i] = faces ? faces[i] : (dim == 1 ? 1 : 4-dim);
1025:   if (lower) for (i = 0; i < dim; ++i) low[i] = lower[i];
1026:   if (upper) for (i = 0; i < dim; ++i) upp[i] = upper[i];
1027:   if (periodicity) for (i = 0; i < dim; ++i) bdt[i] = periodicity[i];

1029:   if (dim == 1)      {DMPlexCreateLineMesh_Internal(comm, fac[0], low[0], upp[0], bdt[0], dm);}
1030:   else if (simplex)  {DMPlexCreateBoxMesh_Simplex_Internal(comm, dim, fac, low, upp, bdt, interpolate, dm);}
1031:   else               {DMPlexCreateBoxMesh_Tensor_Internal(comm, dim, fac, low, upp, bdt, interpolate, dm);}
1032:   return(0);
1033: }

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

1038:   Logically Collective on DM

1040:   Input Parameters:
1041: + dm - the DM context
1042: - prefix - the prefix to prepend to all option names

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

1048:   Level: advanced

1050: .seealso: SNESSetFromOptions()
1051: @*/
1052: PetscErrorCode DMPlexSetOptionsPrefix(DM dm, const char prefix[])
1053: {
1054:   DM_Plex       *mesh = (DM_Plex *) dm->data;

1059:   PetscObjectSetOptionsPrefix((PetscObject) dm, prefix);
1060:   PetscObjectSetOptionsPrefix((PetscObject) mesh->partitioner, prefix);
1061:   return(0);
1062: }

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

1067:   Collective on MPI_Comm

1069:   Input Parameters:
1070: + comm      - The communicator for the DM object
1071: . numRefine - The number of regular refinements to the basic 5 cell structure
1072: - periodicZ - The boundary type for the Z direction

1074:   Output Parameter:
1075: . dm  - The DM object

1077:   Note: Here is the output numbering looking from the bottom of the cylinder:
1078: $       17-----14
1079: $        |     |
1080: $        |  2  |
1081: $        |     |
1082: $ 17-----8-----7-----14
1083: $  |     |     |     |
1084: $  |  3  |  0  |  1  |
1085: $  |     |     |     |
1086: $ 19-----5-----6-----13
1087: $        |     |
1088: $        |  4  |
1089: $        |     |
1090: $       19-----13
1091: $
1092: $ and up through the top
1093: $
1094: $       18-----16
1095: $        |     |
1096: $        |  2  |
1097: $        |     |
1098: $ 18----10----11-----16
1099: $  |     |     |     |
1100: $  |  3  |  0  |  1  |
1101: $  |     |     |     |
1102: $ 20-----9----12-----15
1103: $        |     |
1104: $        |  4  |
1105: $        |     |
1106: $       20-----15

1108:   Level: beginner

1110: .keywords: DM, create
1111: .seealso: DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
1112: @*/
1113: PetscErrorCode DMPlexCreateHexCylinderMesh(MPI_Comm comm, PetscInt numRefine, DMBoundaryType periodicZ, DM *dm)
1114: {
1115:   const PetscInt dim = 3;
1116:   PetscInt       numCells, numVertices, r;
1117:   PetscMPIInt    rank;

1122:   MPI_Comm_rank(comm, &rank);
1123:   if (numRefine < 0) SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Number of refinements %D cannot be negative", numRefine);
1124:   DMCreate(comm, dm);
1125:   DMSetType(*dm, DMPLEX);
1126:   DMSetDimension(*dm, dim);
1127:   /* Create topology */
1128:   {
1129:     PetscInt cone[8], c;

1131:     numCells    = !rank ?  5 : 0;
1132:     numVertices = !rank ? 16 : 0;
1133:     if (periodicZ == DM_BOUNDARY_PERIODIC) {
1134:       numCells   *= 3;
1135:       numVertices = !rank ? 24 : 0;
1136:     }
1137:     DMPlexSetChart(*dm, 0, numCells+numVertices);
1138:     for (c = 0; c < numCells; c++) {DMPlexSetConeSize(*dm, c, 8);}
1139:     DMSetUp(*dm);
1140:     if (!rank) {
1141:       if (periodicZ == DM_BOUNDARY_PERIODIC) {
1142:         cone[0] = 15; cone[1] = 18; cone[2] = 17; cone[3] = 16;
1143:         cone[4] = 31; cone[5] = 32; cone[6] = 33; cone[7] = 34;
1144:         DMPlexSetCone(*dm, 0, cone);
1145:         cone[0] = 16; cone[1] = 17; cone[2] = 24; cone[3] = 23;
1146:         cone[4] = 32; cone[5] = 36; cone[6] = 37; cone[7] = 33; /* 22 25 26 21 */
1147:         DMPlexSetCone(*dm, 1, cone);
1148:         cone[0] = 18; cone[1] = 27; cone[2] = 24; cone[3] = 17;
1149:         cone[4] = 34; cone[5] = 33; cone[6] = 37; cone[7] = 38;
1150:         DMPlexSetCone(*dm, 2, cone);
1151:         cone[0] = 29; cone[1] = 27; cone[2] = 18; cone[3] = 15;
1152:         cone[4] = 35; cone[5] = 31; cone[6] = 34; cone[7] = 38;
1153:         DMPlexSetCone(*dm, 3, cone);
1154:         cone[0] = 29; cone[1] = 15; cone[2] = 16; cone[3] = 23;
1155:         cone[4] = 35; cone[5] = 36; cone[6] = 32; cone[7] = 31;
1156:         DMPlexSetCone(*dm, 4, cone);

1158:         cone[0] = 31; cone[1] = 34; cone[2] = 33; cone[3] = 32;
1159:         cone[4] = 19; cone[5] = 22; cone[6] = 21; cone[7] = 20;
1160:         DMPlexSetCone(*dm, 5, cone);
1161:         cone[0] = 32; cone[1] = 33; cone[2] = 37; cone[3] = 36;
1162:         cone[4] = 22; cone[5] = 25; cone[6] = 26; cone[7] = 21;
1163:         DMPlexSetCone(*dm, 6, cone);
1164:         cone[0] = 34; cone[1] = 38; cone[2] = 37; cone[3] = 33;
1165:         cone[4] = 20; cone[5] = 21; cone[6] = 26; cone[7] = 28;
1166:         DMPlexSetCone(*dm, 7, cone);
1167:         cone[0] = 35; cone[1] = 38; cone[2] = 34; cone[3] = 31;
1168:         cone[4] = 30; cone[5] = 19; cone[6] = 20; cone[7] = 28;
1169:         DMPlexSetCone(*dm, 8, cone);
1170:         cone[0] = 35; cone[1] = 31; cone[2] = 32; cone[3] = 36;
1171:         cone[4] = 30; cone[5] = 25; cone[6] = 22; cone[7] = 19;
1172:         DMPlexSetCone(*dm, 9, cone);

1174:         cone[0] = 19; cone[1] = 20; cone[2] = 21; cone[3] = 22;
1175:         cone[4] = 15; cone[5] = 16; cone[6] = 17; cone[7] = 18;
1176:         DMPlexSetCone(*dm, 10, cone);
1177:         cone[0] = 22; cone[1] = 21; cone[2] = 26; cone[3] = 25;
1178:         cone[4] = 16; cone[5] = 23; cone[6] = 24; cone[7] = 17;
1179:         DMPlexSetCone(*dm, 11, cone);
1180:         cone[0] = 20; cone[1] = 28; cone[2] = 26; cone[3] = 21;
1181:         cone[4] = 18; cone[5] = 17; cone[6] = 24; cone[7] = 27;
1182:         DMPlexSetCone(*dm, 12, cone);
1183:         cone[0] = 30; cone[1] = 28; cone[2] = 20; cone[3] = 19;
1184:         cone[4] = 29; cone[5] = 15; cone[6] = 18; cone[7] = 27;
1185:         DMPlexSetCone(*dm, 13, cone);
1186:         cone[0] = 30; cone[1] = 19; cone[2] = 22; cone[3] = 25;
1187:         cone[4] = 29; cone[5] = 23; cone[6] = 16; cone[7] = 15;
1188:         DMPlexSetCone(*dm, 14, cone);
1189:       } else {
1190:         cone[0] =  5; cone[1] =  8; cone[2] =  7; cone[3] =  6;
1191:         cone[4] =  9; cone[5] = 12; cone[6] = 11; cone[7] = 10;
1192:         DMPlexSetCone(*dm, 0, cone);
1193:         cone[0] =  6; cone[1] =  7; cone[2] = 14; cone[3] = 13;
1194:         cone[4] = 12; cone[5] = 15; cone[6] = 16; cone[7] = 11;
1195:         DMPlexSetCone(*dm, 1, cone);
1196:         cone[0] =  8; cone[1] = 17; cone[2] = 14; cone[3] =  7;
1197:         cone[4] = 10; cone[5] = 11; cone[6] = 16; cone[7] = 18;
1198:         DMPlexSetCone(*dm, 2, cone);
1199:         cone[0] = 19; cone[1] = 17; cone[2] =  8; cone[3] =  5;
1200:         cone[4] = 20; cone[5] =  9; cone[6] = 10; cone[7] = 18;
1201:         DMPlexSetCone(*dm, 3, cone);
1202:         cone[0] = 19; cone[1] =  5; cone[2] =  6; cone[3] = 13;
1203:         cone[4] = 20; cone[5] = 15; cone[6] = 12; cone[7] =  9;
1204:         DMPlexSetCone(*dm, 4, cone);
1205:       }
1206:     }
1207:     DMPlexSymmetrize(*dm);
1208:     DMPlexStratify(*dm);
1209:   }
1210:   /* Interpolate */
1211:   {
1212:     DM idm;

1214:     DMPlexInterpolate(*dm, &idm);
1215:     DMDestroy(dm);
1216:     *dm  = idm;
1217:   }
1218:   /* Create cube geometry */
1219:   {
1220:     Vec             coordinates;
1221:     PetscSection    coordSection;
1222:     PetscScalar    *coords;
1223:     PetscInt        coordSize, v;
1224:     const PetscReal dis = 1.0/PetscSqrtReal(2.0);
1225:     const PetscReal ds2 = dis/2.0;

1227:     /* Build coordinates */
1228:     DMGetCoordinateSection(*dm, &coordSection);
1229:     PetscSectionSetNumFields(coordSection, 1);
1230:     PetscSectionSetFieldComponents(coordSection, 0, dim);
1231:     PetscSectionSetChart(coordSection, numCells, numCells+numVertices);
1232:     for (v = numCells; v < numCells+numVertices; ++v) {
1233:       PetscSectionSetDof(coordSection, v, dim);
1234:       PetscSectionSetFieldDof(coordSection, v, 0, dim);
1235:     }
1236:     PetscSectionSetUp(coordSection);
1237:     PetscSectionGetStorageSize(coordSection, &coordSize);
1238:     VecCreate(PETSC_COMM_SELF, &coordinates);
1239:     PetscObjectSetName((PetscObject) coordinates, "coordinates");
1240:     VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
1241:     VecSetBlockSize(coordinates, dim);
1242:     VecSetType(coordinates,VECSTANDARD);
1243:     VecGetArray(coordinates, &coords);
1244:     if (!rank) {
1245:       coords[0*dim+0] = -ds2; coords[0*dim+1] = -ds2; coords[0*dim+2] = 0.0;
1246:       coords[1*dim+0] =  ds2; coords[1*dim+1] = -ds2; coords[1*dim+2] = 0.0;
1247:       coords[2*dim+0] =  ds2; coords[2*dim+1] =  ds2; coords[2*dim+2] = 0.0;
1248:       coords[3*dim+0] = -ds2; coords[3*dim+1] =  ds2; coords[3*dim+2] = 0.0;
1249:       coords[4*dim+0] = -ds2; coords[4*dim+1] = -ds2; coords[4*dim+2] = 1.0;
1250:       coords[5*dim+0] = -ds2; coords[5*dim+1] =  ds2; coords[5*dim+2] = 1.0;
1251:       coords[6*dim+0] =  ds2; coords[6*dim+1] =  ds2; coords[6*dim+2] = 1.0;
1252:       coords[7*dim+0] =  ds2; coords[7*dim+1] = -ds2; coords[7*dim+2] = 1.0;
1253:       coords[ 8*dim+0] =  dis; coords[ 8*dim+1] = -dis; coords[ 8*dim+2] = 0.0;
1254:       coords[ 9*dim+0] =  dis; coords[ 9*dim+1] =  dis; coords[ 9*dim+2] = 0.0;
1255:       coords[10*dim+0] =  dis; coords[10*dim+1] = -dis; coords[10*dim+2] = 1.0;
1256:       coords[11*dim+0] =  dis; coords[11*dim+1] =  dis; coords[11*dim+2] = 1.0;
1257:       coords[12*dim+0] = -dis; coords[12*dim+1] =  dis; coords[12*dim+2] = 0.0;
1258:       coords[13*dim+0] = -dis; coords[13*dim+1] =  dis; coords[13*dim+2] = 1.0;
1259:       coords[14*dim+0] = -dis; coords[14*dim+1] = -dis; coords[14*dim+2] = 0.0;
1260:       coords[15*dim+0] = -dis; coords[15*dim+1] = -dis; coords[15*dim+2] = 1.0;
1261:       if (periodicZ == DM_BOUNDARY_PERIODIC) {
1262:         /* 15 31 19 */ coords[16*dim+0] = -ds2; coords[16*dim+1] = -ds2; coords[16*dim+2] = 0.5;
1263:         /* 16 32 22 */ coords[17*dim+0] =  ds2; coords[17*dim+1] = -ds2; coords[17*dim+2] = 0.5;
1264:         /* 17 33 21 */ coords[18*dim+0] =  ds2; coords[18*dim+1] =  ds2; coords[18*dim+2] = 0.5;
1265:         /* 18 34 20 */ coords[19*dim+0] = -ds2; coords[19*dim+1] =  ds2; coords[19*dim+2] = 0.5;
1266:         /* 29 35 30 */ coords[20*dim+0] = -dis; coords[20*dim+1] = -dis; coords[20*dim+2] = 0.5;
1267:         /* 23 36 25 */ coords[21*dim+0] =  dis; coords[21*dim+1] = -dis; coords[21*dim+2] = 0.5;
1268:         /* 24 37 26 */ coords[22*dim+0] =  dis; coords[22*dim+1] =  dis; coords[22*dim+2] = 0.5;
1269:         /* 27 38 28 */ coords[23*dim+0] = -dis; coords[23*dim+1] =  dis; coords[23*dim+2] = 0.5;
1270:       }
1271:     }
1272:     VecRestoreArray(coordinates, &coords);
1273:     DMSetCoordinatesLocal(*dm, coordinates);
1274:     VecDestroy(&coordinates);
1275:   }
1276:   /* Create periodicity */
1277:   if (periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
1278:     PetscReal      L[3];
1279:     PetscReal      maxCell[3];
1280:     DMBoundaryType bdType[3];
1281:     PetscReal      lower[3] = {0.0, 0.0, 0.0};
1282:     PetscReal      upper[3] = {1.0, 1.0, 1.5};
1283:     PetscInt       i, numZCells = 3;

1285:     bdType[0] = DM_BOUNDARY_NONE;
1286:     bdType[1] = DM_BOUNDARY_NONE;
1287:     bdType[2] = periodicZ;
1288:     for (i = 0; i < dim; i++) {
1289:       L[i]       = upper[i] - lower[i];
1290:       maxCell[i] = 1.1 * (L[i] / numZCells);
1291:     }
1292:     DMSetPeriodicity(*dm, PETSC_TRUE, maxCell, L, bdType);
1293:   }
1294:   /* Refine topology */
1295:   for (r = 0; r < numRefine; ++r) {
1296:     DM rdm = NULL;

1298:     DMRefine(*dm, comm, &rdm);
1299:     DMDestroy(dm);
1300:     *dm  = rdm;
1301:   }
1302:   /* Remap geometry to cylinder
1303:        Interior square: Linear interpolation is correct
1304:        The other cells all have vertices on rays from the origin. We want to uniformly expand the spacing
1305:        such that the last vertex is on the unit circle. So the closest and farthest vertices are at distance

1307:          phi     = arctan(y/x)
1308:          d_close = sqrt(1/8 + 1/4 sin^2(phi))
1309:          d_far   = sqrt(1/2 + sin^2(phi))

1311:        so we remap them using

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

1316:        If pi/4 < phi < 3pi/4 or -3pi/4 < phi < -pi/4, then we switch x and y.
1317:   */
1318:   {
1319:     Vec           coordinates;
1320:     PetscSection  coordSection;
1321:     PetscScalar  *coords;
1322:     PetscInt      vStart, vEnd, v;
1323:     const PetscReal dis = 1.0/PetscSqrtReal(2.0);
1324:     const PetscReal ds2 = 0.5*dis;

1326:     DMPlexGetDepthStratum(*dm, 0, &vStart, &vEnd);
1327:     DMGetCoordinateSection(*dm, &coordSection);
1328:     DMGetCoordinatesLocal(*dm, &coordinates);
1329:     VecGetArray(coordinates, &coords);
1330:     for (v = vStart; v < vEnd; ++v) {
1331:       PetscReal phi, sinp, cosp, dc, df, x, y, xc, yc;
1332:       PetscInt  off;

1334:       PetscSectionGetOffset(coordSection, v, &off);
1335:       if ((PetscAbsScalar(coords[off+0]) <= ds2) && (PetscAbsScalar(coords[off+1]) <= ds2)) continue;
1336:       x    = PetscRealPart(coords[off]);
1337:       y    = PetscRealPart(coords[off+1]);
1338:       phi  = PetscAtan2Real(y, x);
1339:       sinp = PetscSinReal(phi);
1340:       cosp = PetscCosReal(phi);
1341:       if ((PetscAbsReal(phi) > PETSC_PI/4.0) && (PetscAbsReal(phi) < 3.0*PETSC_PI/4.0)) {
1342:         dc = PetscAbsReal(ds2/sinp);
1343:         df = PetscAbsReal(dis/sinp);
1344:         xc = ds2*x/PetscAbsReal(y);
1345:         yc = ds2*PetscSignReal(y);
1346:       } else {
1347:         dc = PetscAbsReal(ds2/cosp);
1348:         df = PetscAbsReal(dis/cosp);
1349:         xc = ds2*PetscSignReal(x);
1350:         yc = ds2*y/PetscAbsReal(x);
1351:       }
1352:       coords[off+0] = xc + (coords[off+0] - xc)*(1.0 - dc)/(df - dc);
1353:       coords[off+1] = yc + (coords[off+1] - yc)*(1.0 - dc)/(df - dc);
1354:     }
1355:     VecRestoreArray(coordinates, &coords);
1356:     if (periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
1357:       DMLocalizeCoordinates(*dm);
1358:     }
1359:   }
1360:   return(0);
1361: }

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

1366:   Collective on MPI_Comm

1368:   Input Parameters:
1369: + comm - The communicator for the DM object
1370: . n    - The number of wedges around the origin
1371: - interpolate - Create edges and faces

1373:   Output Parameter:
1374: . dm  - The DM object

1376:   Level: beginner

1378: .keywords: DM, create
1379: .seealso: DMPlexCreateHexCylinderMesh(), DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
1380: @*/
1381: PetscErrorCode DMPlexCreateWedgeCylinderMesh(MPI_Comm comm, PetscInt n, PetscBool interpolate, DM *dm)
1382: {
1383:   const PetscInt dim = 3;
1384:   PetscInt       numCells, numVertices;
1385:   PetscMPIInt    rank;

1390:   MPI_Comm_rank(comm, &rank);
1391:   if (n < 0) SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Number of wedges %D cannot be negative", n);
1392:   DMCreate(comm, dm);
1393:   DMSetType(*dm, DMPLEX);
1394:   DMSetDimension(*dm, dim);
1395:   /* Create topology */
1396:   {
1397:     PetscInt cone[6], c;

1399:     numCells    = !rank ?        n : 0;
1400:     numVertices = !rank ?  2*(n+1) : 0;
1401:     DMPlexSetChart(*dm, 0, numCells+numVertices);
1402:     for (c = 0; c < numCells; c++) {DMPlexSetConeSize(*dm, c, 6);}
1403:     DMSetUp(*dm);
1404:     for (c = 0; c < numCells; c++) {
1405:       cone[0] =  c+n*1; cone[1] = (c+1)%n+n*1; cone[2] = 0+3*n;
1406:       cone[3] =  c+n*2; cone[4] = (c+1)%n+n*2; cone[5] = 1+3*n;
1407:       DMPlexSetCone(*dm, c, cone);
1408:     }
1409:     DMPlexSymmetrize(*dm);
1410:     DMPlexStratify(*dm);
1411:   }
1412:   /* Interpolate */
1413:   if (interpolate) {
1414:     DM idm;

1416:     DMPlexInterpolate(*dm, &idm);
1417:     DMDestroy(dm);
1418:     *dm  = idm;
1419:   }
1420:   /* Create cylinder geometry */
1421:   {
1422:     Vec          coordinates;
1423:     PetscSection coordSection;
1424:     PetscScalar *coords;
1425:     PetscInt     coordSize, v, c;

1427:     /* Build coordinates */
1428:     DMGetCoordinateSection(*dm, &coordSection);
1429:     PetscSectionSetNumFields(coordSection, 1);
1430:     PetscSectionSetFieldComponents(coordSection, 0, dim);
1431:     PetscSectionSetChart(coordSection, numCells, numCells+numVertices);
1432:     for (v = numCells; v < numCells+numVertices; ++v) {
1433:       PetscSectionSetDof(coordSection, v, dim);
1434:       PetscSectionSetFieldDof(coordSection, v, 0, dim);
1435:     }
1436:     PetscSectionSetUp(coordSection);
1437:     PetscSectionGetStorageSize(coordSection, &coordSize);
1438:     VecCreate(PETSC_COMM_SELF, &coordinates);
1439:     PetscObjectSetName((PetscObject) coordinates, "coordinates");
1440:     VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
1441:     VecSetBlockSize(coordinates, dim);
1442:     VecSetType(coordinates,VECSTANDARD);
1443:     VecGetArray(coordinates, &coords);
1444:     for (c = 0; c < numCells; c++) {
1445:       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;
1446:       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;
1447:     }
1448:     if (!rank) {
1449:       coords[(2*n+0)*dim+0] = 0.0; coords[(2*n+0)*dim+1] = 0.0; coords[(2*n+0)*dim+2] = 1.0;
1450:       coords[(2*n+1)*dim+0] = 0.0; coords[(2*n+1)*dim+1] = 0.0; coords[(2*n+1)*dim+2] = 0.0;
1451:     }
1452:     VecRestoreArray(coordinates, &coords);
1453:     DMSetCoordinatesLocal(*dm, coordinates);
1454:     VecDestroy(&coordinates);
1455:   }
1456:   return(0);
1457: }

1459: PETSC_STATIC_INLINE PetscReal DiffNormReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
1460: {
1461:   PetscReal prod = 0.0;
1462:   PetscInt  i;
1463:   for (i = 0; i < dim; ++i) prod += PetscSqr(x[i] - y[i]);
1464:   return PetscSqrtReal(prod);
1465: }
1466: PETSC_STATIC_INLINE PetscReal DotReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
1467: {
1468:   PetscReal prod = 0.0;
1469:   PetscInt  i;
1470:   for (i = 0; i < dim; ++i) prod += x[i]*y[i];
1471:   return prod;
1472: }

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

1477:   Collective on MPI_Comm

1479:   Input Parameters:
1480: . comm  - The communicator for the DM object
1481: . dim   - The dimension
1482: - simplex - Use simplices, or tensor product cells

1484:   Output Parameter:
1485: . dm  - The DM object

1487:   Level: beginner

1489: .keywords: DM, create
1490: .seealso: DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
1491: @*/
1492: PetscErrorCode DMPlexCreateSphereMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, DM *dm)
1493: {
1494:   const PetscInt  embedDim = dim+1;
1495:   PetscSection    coordSection;
1496:   Vec             coordinates;
1497:   PetscScalar    *coords;
1498:   PetscReal      *coordsIn;
1499:   PetscInt        numCells, numEdges, numVerts, firstVertex, v, firstEdge, coordSize, d, c, e;
1500:   PetscMPIInt     rank;
1501:   PetscErrorCode  ierr;

1505:   DMCreate(comm, dm);
1506:   DMSetType(*dm, DMPLEX);
1507:   DMSetDimension(*dm, dim);
1508:   DMSetCoordinateDim(*dm, dim+1);
1509:   MPI_Comm_rank(PetscObjectComm((PetscObject) *dm), &rank);
1510:   switch (dim) {
1511:   case 2:
1512:     if (simplex) {
1513:       DM              idm;
1514:       const PetscReal edgeLen     = 2.0/(1.0 + PETSC_PHI);
1515:       const PetscReal vertex[3]   = {0.0, 1.0/(1.0 + PETSC_PHI), PETSC_PHI/(1.0 + PETSC_PHI)};
1516:       const PetscInt  degree      = 5;
1517:       PetscInt        s[3]        = {1, 1, 1};
1518:       PetscInt        cone[3];
1519:       PetscInt       *graph, p, i, j, k;

1521:       numCells    = !rank ? 20 : 0;
1522:       numVerts    = !rank ? 12 : 0;
1523:       firstVertex = numCells;
1524:       /* Use icosahedron, which for a unit sphere has coordinates which are all cyclic permutations of

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

1528:          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
1529:          length is then given by 2/\phi = 2 * 2.73606 = 5.47214.
1530:        */
1531:       /* Construct vertices */
1532:       PetscCalloc1(numVerts * embedDim, &coordsIn);
1533:       for (p = 0, i = 0; p < embedDim; ++p) {
1534:         for (s[1] = -1; s[1] < 2; s[1] += 2) {
1535:           for (s[2] = -1; s[2] < 2; s[2] += 2) {
1536:             for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[(d+p)%embedDim]*vertex[(d+p)%embedDim];
1537:             ++i;
1538:           }
1539:         }
1540:       }
1541:       /* Construct graph */
1542:       PetscCalloc1(numVerts * numVerts, &graph);
1543:       for (i = 0; i < numVerts; ++i) {
1544:         for (j = 0, k = 0; j < numVerts; ++j) {
1545:           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i*embedDim], &coordsIn[j*embedDim]) - edgeLen) < PETSC_SMALL) {graph[i*numVerts+j] = 1; ++k;}
1546:         }
1547:         if (k != degree) SETERRQ3(comm, PETSC_ERR_PLIB, "Invalid icosahedron, vertex %D degree %D != %D", i, k, degree);
1548:       }
1549:       /* Build Topology */
1550:       DMPlexSetChart(*dm, 0, numCells+numVerts);
1551:       for (c = 0; c < numCells; c++) {
1552:         DMPlexSetConeSize(*dm, c, embedDim);
1553:       }
1554:       DMSetUp(*dm); /* Allocate space for cones */
1555:       /* Cells */
1556:       for (i = 0, c = 0; i < numVerts; ++i) {
1557:         for (j = 0; j < i; ++j) {
1558:           for (k = 0; k < j; ++k) {
1559:             if (graph[i*numVerts+j] && graph[j*numVerts+k] && graph[k*numVerts+i]) {
1560:               cone[0] = firstVertex+i; cone[1] = firstVertex+j; cone[2] = firstVertex+k;
1561:               /* Check orientation */
1562:               {
1563:                 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}}};
1564:                 PetscReal normal[3];
1565:                 PetscInt  e, f;

1567:                 for (d = 0; d < embedDim; ++d) {
1568:                   normal[d] = 0.0;
1569:                   for (e = 0; e < embedDim; ++e) {
1570:                     for (f = 0; f < embedDim; ++f) {
1571:                       normal[d] += epsilon[d][e][f]*(coordsIn[j*embedDim+e] - coordsIn[i*embedDim+e])*(coordsIn[k*embedDim+f] - coordsIn[i*embedDim+f]);
1572:                     }
1573:                   }
1574:                 }
1575:                 if (DotReal(embedDim, normal, &coordsIn[i*embedDim]) < 0) {PetscInt tmp = cone[1]; cone[1] = cone[2]; cone[2] = tmp;}
1576:               }
1577:               DMPlexSetCone(*dm, c++, cone);
1578:             }
1579:           }
1580:         }
1581:       }
1582:       DMPlexSymmetrize(*dm);
1583:       DMPlexStratify(*dm);
1584:       PetscFree(graph);
1585:       /* Interpolate mesh */
1586:       DMPlexInterpolate(*dm, &idm);
1587:       DMDestroy(dm);
1588:       *dm  = idm;
1589:     } else {
1590:       /*
1591:         12-21--13
1592:          |     |
1593:         25  4  24
1594:          |     |
1595:   12-25--9-16--8-24--13
1596:    |     |     |     |
1597:   23  5 17  0 15  3  22
1598:    |     |     |     |
1599:   10-20--6-14--7-19--11
1600:          |     |
1601:         20  1  19
1602:          |     |
1603:         10-18--11
1604:          |     |
1605:         23  2  22
1606:          |     |
1607:         12-21--13
1608:        */
1609:       const PetscReal dist = 1.0/PetscSqrtReal(3.0);
1610:       PetscInt        cone[4], ornt[4];

1612:       numCells    = !rank ?  6 : 0;
1613:       numEdges    = !rank ? 12 : 0;
1614:       numVerts    = !rank ?  8 : 0;
1615:       firstVertex = numCells;
1616:       firstEdge   = numCells + numVerts;
1617:       /* Build Topology */
1618:       DMPlexSetChart(*dm, 0, numCells+numEdges+numVerts);
1619:       for (c = 0; c < numCells; c++) {
1620:         DMPlexSetConeSize(*dm, c, 4);
1621:       }
1622:       for (e = firstEdge; e < firstEdge+numEdges; ++e) {
1623:         DMPlexSetConeSize(*dm, e, 2);
1624:       }
1625:       DMSetUp(*dm); /* Allocate space for cones */
1626:       /* Cell 0 */
1627:       cone[0] = 14; cone[1] = 15; cone[2] = 16; cone[3] = 17;
1628:       DMPlexSetCone(*dm, 0, cone);
1629:       ornt[0] = 0; ornt[1] = 0; ornt[2] = 0; ornt[3] = 0;
1630:       DMPlexSetConeOrientation(*dm, 0, ornt);
1631:       /* Cell 1 */
1632:       cone[0] = 18; cone[1] = 19; cone[2] = 14; cone[3] = 20;
1633:       DMPlexSetCone(*dm, 1, cone);
1634:       ornt[0] = 0; ornt[1] = 0; ornt[2] = -2; ornt[3] = 0;
1635:       DMPlexSetConeOrientation(*dm, 1, ornt);
1636:       /* Cell 2 */
1637:       cone[0] = 21; cone[1] = 22; cone[2] = 18; cone[3] = 23;
1638:       DMPlexSetCone(*dm, 2, cone);
1639:       ornt[0] = 0; ornt[1] = 0; ornt[2] = -2; ornt[3] = 0;
1640:       DMPlexSetConeOrientation(*dm, 2, ornt);
1641:       /* Cell 3 */
1642:       cone[0] = 19; cone[1] = 22; cone[2] = 24; cone[3] = 15;
1643:       DMPlexSetCone(*dm, 3, cone);
1644:       ornt[0] = -2; ornt[1] = -2; ornt[2] = 0; ornt[3] = -2;
1645:       DMPlexSetConeOrientation(*dm, 3, ornt);
1646:       /* Cell 4 */
1647:       cone[0] = 16; cone[1] = 24; cone[2] = 21; cone[3] = 25;
1648:       DMPlexSetCone(*dm, 4, cone);
1649:       ornt[0] = -2; ornt[1] = -2; ornt[2] = -2; ornt[3] = 0;
1650:       DMPlexSetConeOrientation(*dm, 4, ornt);
1651:       /* Cell 5 */
1652:       cone[0] = 20; cone[1] = 17; cone[2] = 25; cone[3] = 23;
1653:       DMPlexSetCone(*dm, 5, cone);
1654:       ornt[0] = -2; ornt[1] = -2; ornt[2] = -2; ornt[3] = -2;
1655:       DMPlexSetConeOrientation(*dm, 5, ornt);
1656:       /* Edges */
1657:       cone[0] =  6; cone[1] =  7;
1658:       DMPlexSetCone(*dm, 14, cone);
1659:       cone[0] =  7; cone[1] =  8;
1660:       DMPlexSetCone(*dm, 15, cone);
1661:       cone[0] =  8; cone[1] =  9;
1662:       DMPlexSetCone(*dm, 16, cone);
1663:       cone[0] =  9; cone[1] =  6;
1664:       DMPlexSetCone(*dm, 17, cone);
1665:       cone[0] = 10; cone[1] = 11;
1666:       DMPlexSetCone(*dm, 18, cone);
1667:       cone[0] = 11; cone[1] =  7;
1668:       DMPlexSetCone(*dm, 19, cone);
1669:       cone[0] =  6; cone[1] = 10;
1670:       DMPlexSetCone(*dm, 20, cone);
1671:       cone[0] = 12; cone[1] = 13;
1672:       DMPlexSetCone(*dm, 21, cone);
1673:       cone[0] = 13; cone[1] = 11;
1674:       DMPlexSetCone(*dm, 22, cone);
1675:       cone[0] = 10; cone[1] = 12;
1676:       DMPlexSetCone(*dm, 23, cone);
1677:       cone[0] = 13; cone[1] =  8;
1678:       DMPlexSetCone(*dm, 24, cone);
1679:       cone[0] = 12; cone[1] =  9;
1680:       DMPlexSetCone(*dm, 25, cone);
1681:       DMPlexSymmetrize(*dm);
1682:       DMPlexStratify(*dm);
1683:       /* Build coordinates */
1684:       PetscCalloc1(numVerts * embedDim, &coordsIn);
1685:       coordsIn[0*embedDim+0] = -dist; coordsIn[0*embedDim+1] =  dist; coordsIn[0*embedDim+2] = -dist;
1686:       coordsIn[1*embedDim+0] =  dist; coordsIn[1*embedDim+1] =  dist; coordsIn[1*embedDim+2] = -dist;
1687:       coordsIn[2*embedDim+0] =  dist; coordsIn[2*embedDim+1] = -dist; coordsIn[2*embedDim+2] = -dist;
1688:       coordsIn[3*embedDim+0] = -dist; coordsIn[3*embedDim+1] = -dist; coordsIn[3*embedDim+2] = -dist;
1689:       coordsIn[4*embedDim+0] = -dist; coordsIn[4*embedDim+1] =  dist; coordsIn[4*embedDim+2] =  dist;
1690:       coordsIn[5*embedDim+0] =  dist; coordsIn[5*embedDim+1] =  dist; coordsIn[5*embedDim+2] =  dist;
1691:       coordsIn[6*embedDim+0] = -dist; coordsIn[6*embedDim+1] = -dist; coordsIn[6*embedDim+2] =  dist;
1692:       coordsIn[7*embedDim+0] =  dist; coordsIn[7*embedDim+1] = -dist; coordsIn[7*embedDim+2] =  dist;
1693:     }
1694:     break;
1695:   case 3:
1696:     if (simplex) {
1697:       DM              idm;
1698:       const PetscReal edgeLen         = 1.0/PETSC_PHI;
1699:       const PetscReal vertexA[4]      = {0.5, 0.5, 0.5, 0.5};
1700:       const PetscReal vertexB[4]      = {1.0, 0.0, 0.0, 0.0};
1701:       const PetscReal vertexC[4]      = {0.5, 0.5*PETSC_PHI, 0.5/PETSC_PHI, 0.0};
1702:       const PetscInt  degree          = 12;
1703:       PetscInt        s[4]            = {1, 1, 1};
1704:       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},
1705:                                          {2, 0, 1, 3}, {2, 1, 3, 0}, {2, 3, 0, 1}, {3, 0, 2, 1}, {3, 1, 0, 2}, {3, 2, 1, 0}};
1706:       PetscInt        cone[4];
1707:       PetscInt       *graph, p, i, j, k, l;

1709:       numCells    = !rank ? 600 : 0;
1710:       numVerts    = !rank ? 120 : 0;
1711:       firstVertex = numCells;
1712:       /* Use the 600-cell, which for a unit sphere has coordinates which are

1714:            1/2 (\pm 1, \pm 1,    \pm 1, \pm 1)                          16
1715:                (\pm 1,    0,       0,      0)  all cyclic permutations   8
1716:            1/2 (\pm 1, \pm phi, \pm 1/phi, 0)  all even permutations    96

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

1721:          http://buzzard.pugetsound.edu/sage-practice/ch03s03.html
1722:          http://mathworld.wolfram.com/600-Cell.html
1723:        */
1724:       /* Construct vertices */
1725:       PetscCalloc1(numVerts * embedDim, &coordsIn);
1726:       i    = 0;
1727:       for (s[0] = -1; s[0] < 2; s[0] += 2) {
1728:         for (s[1] = -1; s[1] < 2; s[1] += 2) {
1729:           for (s[2] = -1; s[2] < 2; s[2] += 2) {
1730:             for (s[3] = -1; s[3] < 2; s[3] += 2) {
1731:               for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[d]*vertexA[d];
1732:               ++i;
1733:             }
1734:           }
1735:         }
1736:       }
1737:       for (p = 0; p < embedDim; ++p) {
1738:         s[1] = s[2] = s[3] = 1;
1739:         for (s[0] = -1; s[0] < 2; s[0] += 2) {
1740:           for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[(d+p)%embedDim]*vertexB[(d+p)%embedDim];
1741:           ++i;
1742:         }
1743:       }
1744:       for (p = 0; p < 12; ++p) {
1745:         s[3] = 1;
1746:         for (s[0] = -1; s[0] < 2; s[0] += 2) {
1747:           for (s[1] = -1; s[1] < 2; s[1] += 2) {
1748:             for (s[2] = -1; s[2] < 2; s[2] += 2) {
1749:               for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[evenPerm[p][d]]*vertexC[evenPerm[p][d]];
1750:               ++i;
1751:             }
1752:           }
1753:         }
1754:       }
1755:       if (i != numVerts) SETERRQ2(comm, PETSC_ERR_PLIB, "Invalid 600-cell, vertices %D != %D", i, numVerts);
1756:       /* Construct graph */
1757:       PetscCalloc1(numVerts * numVerts, &graph);
1758:       for (i = 0; i < numVerts; ++i) {
1759:         for (j = 0, k = 0; j < numVerts; ++j) {
1760:           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i*embedDim], &coordsIn[j*embedDim]) - edgeLen) < PETSC_SMALL) {graph[i*numVerts+j] = 1; ++k;}
1761:         }
1762:         if (k != degree) SETERRQ3(comm, PETSC_ERR_PLIB, "Invalid 600-cell, vertex %D degree %D != %D", i, k, degree);
1763:       }
1764:       /* Build Topology */
1765:       DMPlexSetChart(*dm, 0, numCells+numVerts);
1766:       for (c = 0; c < numCells; c++) {
1767:         DMPlexSetConeSize(*dm, c, embedDim);
1768:       }
1769:       DMSetUp(*dm); /* Allocate space for cones */
1770:       /* Cells */
1771:       for (i = 0, c = 0; i < numVerts; ++i) {
1772:         for (j = 0; j < i; ++j) {
1773:           for (k = 0; k < j; ++k) {
1774:             for (l = 0; l < k; ++l) {
1775:               if (graph[i*numVerts+j] && graph[j*numVerts+k] && graph[k*numVerts+i] &&
1776:                   graph[l*numVerts+i] && graph[l*numVerts+j] && graph[l*numVerts+k]) {
1777:                 cone[0] = firstVertex+i; cone[1] = firstVertex+j; cone[2] = firstVertex+k; cone[3] = firstVertex+l;
1778:                 /* Check orientation: https://ef.gy/linear-algebra:normal-vectors-in-higher-dimensional-spaces */
1779:                 {
1780:                   const PetscInt epsilon[4][4][4][4] = {{{{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
1781:                                                          {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  1}, { 0,  0, -1, 0}},
1782:                                                          {{0,  0,  0,  0}, { 0, 0,  0, -1}, { 0,  0, 0,  0}, { 0,  1,  0, 0}},
1783:                                                          {{0,  0,  0,  0}, { 0, 0,  1,  0}, { 0, -1, 0,  0}, { 0,  0,  0, 0}}},

1785:                                                         {{{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0, -1}, { 0,  0,  1, 0}},
1786:                                                          {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
1787:                                                          {{0,  0,  0,  1}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, {-1,  0,  0, 0}},
1788:                                                          {{0,  0, -1,  0}, { 0, 0,  0,  0}, { 1,  0, 0,  0}, { 0,  0,  0, 0}}},

1790:                                                         {{{0,  0,  0,  0}, { 0, 0,  0,  1}, { 0,  0, 0,  0}, { 0, -1,  0, 0}},
1791:                                                          {{0,  0,  0, -1}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 1,  0,  0, 0}},
1792:                                                          {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
1793:                                                          {{0,  1,  0,  0}, {-1, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}}},

1795:                                                         {{{0,  0,  0,  0}, { 0, 0, -1,  0}, { 0,  1, 0,  0}, { 0,  0,  0, 0}},
1796:                                                          {{0,  0,  1,  0}, { 0, 0,  0,  0}, {-1,  0, 0,  0}, { 0,  0,  0, 0}},
1797:                                                          {{0, -1,  0,  0}, { 1, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
1798:                                                          {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}}}};
1799:                   PetscReal normal[4];
1800:                   PetscInt  e, f, g;

1802:                   for (d = 0; d < embedDim; ++d) {
1803:                     normal[d] = 0.0;
1804:                     for (e = 0; e < embedDim; ++e) {
1805:                       for (f = 0; f < embedDim; ++f) {
1806:                         for (g = 0; g < embedDim; ++g) {
1807:                           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]);
1808:                         }
1809:                       }
1810:                     }
1811:                   }
1812:                   if (DotReal(embedDim, normal, &coordsIn[i*embedDim]) < 0) {PetscInt tmp = cone[1]; cone[1] = cone[2]; cone[2] = tmp;}
1813:                 }
1814:                 DMPlexSetCone(*dm, c++, cone);
1815:               }
1816:             }
1817:           }
1818:         }
1819:       }
1820:       DMPlexSymmetrize(*dm);
1821:       DMPlexStratify(*dm);
1822:       PetscFree(graph);
1823:       /* Interpolate mesh */
1824:       DMPlexInterpolate(*dm, &idm);
1825:       DMDestroy(dm);
1826:       *dm  = idm;
1827:       break;
1828:     }
1829:   default: SETERRQ1(comm, PETSC_ERR_SUP, "Unsupported dimension for sphere: %D", dim);
1830:   }
1831:   /* Create coordinates */
1832:   DMGetCoordinateSection(*dm, &coordSection);
1833:   PetscSectionSetNumFields(coordSection, 1);
1834:   PetscSectionSetFieldComponents(coordSection, 0, embedDim);
1835:   PetscSectionSetChart(coordSection, firstVertex, firstVertex+numVerts);
1836:   for (v = firstVertex; v < firstVertex+numVerts; ++v) {
1837:     PetscSectionSetDof(coordSection, v, embedDim);
1838:     PetscSectionSetFieldDof(coordSection, v, 0, embedDim);
1839:   }
1840:   PetscSectionSetUp(coordSection);
1841:   PetscSectionGetStorageSize(coordSection, &coordSize);
1842:   VecCreate(PETSC_COMM_SELF, &coordinates);
1843:   VecSetBlockSize(coordinates, embedDim);
1844:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
1845:   VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
1846:   VecSetType(coordinates,VECSTANDARD);
1847:   VecGetArray(coordinates, &coords);
1848:   for (v = 0; v < numVerts; ++v) for (d = 0; d < embedDim; ++d) {coords[v*embedDim+d] = coordsIn[v*embedDim+d];}
1849:   VecRestoreArray(coordinates, &coords);
1850:   DMSetCoordinatesLocal(*dm, coordinates);
1851:   VecDestroy(&coordinates);
1852:   PetscFree(coordsIn);
1853:   return(0);
1854: }

1856: /* External function declarations here */
1857: extern PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling);
1858: extern PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat);
1859: extern PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mat);
1860: extern PetscErrorCode DMCreateDefaultSection_Plex(DM dm);
1861: extern PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm);
1862: extern PetscErrorCode DMCreateMatrix_Plex(DM dm,  Mat *J);
1863: extern PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm);
1864: PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm);
1865: extern PetscErrorCode DMSetUp_Plex(DM dm);
1866: extern PetscErrorCode DMDestroy_Plex(DM dm);
1867: extern PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer);
1868: extern PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer);
1869: extern PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm);
1870: extern PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm);
1871: static PetscErrorCode DMInitialize_Plex(DM dm);

1873: /* Replace dm with the contents of dmNew
1874:    - Share the DM_Plex structure
1875:    - Share the coordinates
1876:    - Share the SF
1877: */
1878: static PetscErrorCode DMPlexReplace_Static(DM dm, DM dmNew)
1879: {
1880:   PetscSF               sf;
1881:   DM                    coordDM, coarseDM;
1882:   Vec                   coords;
1883:   PetscBool             isper;
1884:   const PetscReal      *maxCell, *L;
1885:   const DMBoundaryType *bd;
1886:   PetscErrorCode        ierr;

1889:   DMGetPointSF(dmNew, &sf);
1890:   DMSetPointSF(dm, sf);
1891:   DMGetCoordinateDM(dmNew, &coordDM);
1892:   DMGetCoordinatesLocal(dmNew, &coords);
1893:   DMSetCoordinateDM(dm, coordDM);
1894:   DMSetCoordinatesLocal(dm, coords);
1895:   DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);
1896:   DMSetPeriodicity(dmNew, isper, maxCell, L, bd);
1897:   DMDestroy_Plex(dm);
1898:   DMInitialize_Plex(dm);
1899:   dm->data = dmNew->data;
1900:   ((DM_Plex *) dmNew->data)->refct++;
1901:   dmNew->labels->refct++;
1902:   if (!--(dm->labels->refct)) {
1903:     DMLabelLink next = dm->labels->next;

1905:     /* destroy the labels */
1906:     while (next) {
1907:       DMLabelLink tmp = next->next;

1909:       DMLabelDestroy(&next->label);
1910:       PetscFree(next);
1911:       next = tmp;
1912:     }
1913:     PetscFree(dm->labels);
1914:   }
1915:   dm->labels = dmNew->labels;
1916:   dm->depthLabel = dmNew->depthLabel;
1917:   DMGetCoarseDM(dmNew,&coarseDM);
1918:   DMSetCoarseDM(dm,coarseDM);
1919:   return(0);
1920: }

1922: /* Swap dm with the contents of dmNew
1923:    - Swap the DM_Plex structure
1924:    - Swap the coordinates
1925:    - Swap the point PetscSF
1926: */
1927: static PetscErrorCode DMPlexSwap_Static(DM dmA, DM dmB)
1928: {
1929:   DM              coordDMA, coordDMB;
1930:   Vec             coordsA,  coordsB;
1931:   PetscSF         sfA,      sfB;
1932:   void            *tmp;
1933:   DMLabelLinkList listTmp;
1934:   DMLabel         depthTmp;
1935:   PetscErrorCode  ierr;

1938:   DMGetPointSF(dmA, &sfA);
1939:   DMGetPointSF(dmB, &sfB);
1940:   PetscObjectReference((PetscObject) sfA);
1941:   DMSetPointSF(dmA, sfB);
1942:   DMSetPointSF(dmB, sfA);
1943:   PetscObjectDereference((PetscObject) sfA);

1945:   DMGetCoordinateDM(dmA, &coordDMA);
1946:   DMGetCoordinateDM(dmB, &coordDMB);
1947:   PetscObjectReference((PetscObject) coordDMA);
1948:   DMSetCoordinateDM(dmA, coordDMB);
1949:   DMSetCoordinateDM(dmB, coordDMA);
1950:   PetscObjectDereference((PetscObject) coordDMA);

1952:   DMGetCoordinatesLocal(dmA, &coordsA);
1953:   DMGetCoordinatesLocal(dmB, &coordsB);
1954:   PetscObjectReference((PetscObject) coordsA);
1955:   DMSetCoordinatesLocal(dmA, coordsB);
1956:   DMSetCoordinatesLocal(dmB, coordsA);
1957:   PetscObjectDereference((PetscObject) coordsA);

1959:   tmp       = dmA->data;
1960:   dmA->data = dmB->data;
1961:   dmB->data = tmp;
1962:   listTmp   = dmA->labels;
1963:   dmA->labels = dmB->labels;
1964:   dmB->labels = listTmp;
1965:   depthTmp  = dmA->depthLabel;
1966:   dmA->depthLabel = dmB->depthLabel;
1967:   dmB->depthLabel = depthTmp;
1968:   return(0);
1969: }

1971: PetscErrorCode DMSetFromOptions_NonRefinement_Plex(PetscOptionItems *PetscOptionsObject,DM dm)
1972: {
1973:   DM_Plex       *mesh = (DM_Plex*) dm->data;

1977:   /* Handle viewing */
1978:   PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMView", PETSC_FALSE, &mesh->printSetValues, NULL);
1979:   PetscOptionsInt("-dm_plex_print_fem", "Debug output level all fem computations", "DMView", 0, &mesh->printFEM, NULL);
1980:   PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMView", mesh->printTol, &mesh->printTol, NULL);
1981:   /* Point Location */
1982:   PetscOptionsBool("-dm_plex_hash_location", "Use grid hashing for point location", "DMView", PETSC_FALSE, &mesh->useHashLocation, NULL);
1983:   /* Generation and remeshing */
1984:   PetscOptionsBool("-dm_plex_remesh_bd", "Allow changes to the boundary on remeshing", "DMView", PETSC_FALSE, &mesh->remeshBd, NULL);
1985:   /* Projection behavior */
1986:   PetscOptionsInt("-dm_plex_max_projection_height", "Maxmimum mesh point height used to project locally", "DMPlexSetMaxProjectionHeight", 0, &mesh->maxProjectionHeight, NULL);
1987:   PetscOptionsBool("-dm_plex_regular_refinement", "Use special nested projection algorithm for regular refinement", "DMPlexSetRegularRefinement", mesh->regularRefinement, &mesh->regularRefinement, NULL);

1989:   PetscPartitionerSetFromOptions(mesh->partitioner);
1990:   return(0);
1991: }

1993: static PetscErrorCode DMSetFromOptions_Plex(PetscOptionItems *PetscOptionsObject,DM dm)
1994: {
1995:   PetscInt       refine = 0, coarsen = 0, r;
1996:   PetscBool      isHierarchy;

2001:   PetscOptionsHead(PetscOptionsObject,"DMPlex Options");
2002:   /* Handle DMPlex refinement */
2003:   PetscOptionsInt("-dm_refine", "The number of uniform refinements", "DMCreate", refine, &refine, NULL);
2004:   PetscOptionsInt("-dm_refine_hierarchy", "The number of uniform refinements", "DMCreate", refine, &refine, &isHierarchy);
2005:   if (refine) {DMPlexSetRefinementUniform(dm, PETSC_TRUE);}
2006:   if (refine && isHierarchy) {
2007:     DM *dms, coarseDM;

2009:     DMGetCoarseDM(dm, &coarseDM);
2010:     PetscObjectReference((PetscObject)coarseDM);
2011:     PetscMalloc1(refine,&dms);
2012:     DMRefineHierarchy(dm, refine, dms);
2013:     /* Total hack since we do not pass in a pointer */
2014:     DMPlexSwap_Static(dm, dms[refine-1]);
2015:     if (refine == 1) {
2016:       DMSetCoarseDM(dm, dms[0]);
2017:       DMPlexSetRegularRefinement(dm, PETSC_TRUE);
2018:     } else {
2019:       DMSetCoarseDM(dm, dms[refine-2]);
2020:       DMPlexSetRegularRefinement(dm, PETSC_TRUE);
2021:       DMSetCoarseDM(dms[0], dms[refine-1]);
2022:       DMPlexSetRegularRefinement(dms[0], PETSC_TRUE);
2023:     }
2024:     DMSetCoarseDM(dms[refine-1], coarseDM);
2025:     PetscObjectDereference((PetscObject)coarseDM);
2026:     /* Free DMs */
2027:     for (r = 0; r < refine; ++r) {
2028:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]);
2029:       DMDestroy(&dms[r]);
2030:     }
2031:     PetscFree(dms);
2032:   } else {
2033:     for (r = 0; r < refine; ++r) {
2034:       DM refinedMesh;

2036:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
2037:       DMRefine(dm, PetscObjectComm((PetscObject) dm), &refinedMesh);
2038:       /* Total hack since we do not pass in a pointer */
2039:       DMPlexReplace_Static(dm, refinedMesh);
2040:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
2041:       DMDestroy(&refinedMesh);
2042:     }
2043:   }
2044:   /* Handle DMPlex coarsening */
2045:   PetscOptionsInt("-dm_coarsen", "Coarsen the mesh", "DMCreate", coarsen, &coarsen, NULL);
2046:   PetscOptionsInt("-dm_coarsen_hierarchy", "The number of coarsenings", "DMCreate", coarsen, &coarsen, &isHierarchy);
2047:   if (coarsen && isHierarchy) {
2048:     DM *dms;

2050:     PetscMalloc1(coarsen, &dms);
2051:     DMCoarsenHierarchy(dm, coarsen, dms);
2052:     /* Free DMs */
2053:     for (r = 0; r < coarsen; ++r) {
2054:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]);
2055:       DMDestroy(&dms[r]);
2056:     }
2057:     PetscFree(dms);
2058:   } else {
2059:     for (r = 0; r < coarsen; ++r) {
2060:       DM coarseMesh;

2062:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
2063:       DMCoarsen(dm, PetscObjectComm((PetscObject) dm), &coarseMesh);
2064:       /* Total hack since we do not pass in a pointer */
2065:       DMPlexReplace_Static(dm, coarseMesh);
2066:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
2067:       DMDestroy(&coarseMesh);
2068:     }
2069:   }
2070:   /* Handle */
2071:   DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
2072:   PetscOptionsTail();
2073:   return(0);
2074: }

2076: static PetscErrorCode DMCreateGlobalVector_Plex(DM dm,Vec *vec)
2077: {

2081:   DMCreateGlobalVector_Section_Private(dm,vec);
2082:   /* VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM); */
2083:   VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex);
2084:   VecSetOperation(*vec, VECOP_VIEWNATIVE, (void (*)(void)) VecView_Plex_Native);
2085:   VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex);
2086:   VecSetOperation(*vec, VECOP_LOADNATIVE, (void (*)(void)) VecLoad_Plex_Native);
2087:   return(0);
2088: }

2090: static PetscErrorCode DMCreateLocalVector_Plex(DM dm,Vec *vec)
2091: {

2095:   DMCreateLocalVector_Section_Private(dm,vec);
2096:   VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex_Local);
2097:   VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex_Local);
2098:   return(0);
2099: }

2101: static PetscErrorCode DMGetDimPoints_Plex(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
2102: {
2103:   PetscInt       depth, d;

2107:   DMPlexGetDepth(dm, &depth);
2108:   if (depth == 1) {
2109:     DMGetDimension(dm, &d);
2110:     if (dim == 0)      {DMPlexGetDepthStratum(dm, dim, pStart, pEnd);}
2111:     else if (dim == d) {DMPlexGetDepthStratum(dm, 1, pStart, pEnd);}
2112:     else               {*pStart = 0; *pEnd = 0;}
2113:   } else {
2114:     DMPlexGetDepthStratum(dm, dim, pStart, pEnd);
2115:   }
2116:   return(0);
2117: }

2119: static PetscErrorCode DMGetNeighors_Plex(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
2120: {
2121:   PetscSF        sf;

2125:   DMGetPointSF(dm, &sf);
2126:   PetscSFGetRanks(sf, nranks, ranks, NULL, NULL, NULL);
2127:   return(0);
2128: }

2130: static PetscErrorCode DMHasCreateInjection_Plex(DM dm, PetscBool *flg)
2131: {
2135:   *flg = PETSC_TRUE;
2136:   return(0);
2137: }

2139: static PetscErrorCode DMInitialize_Plex(DM dm)
2140: {

2144:   dm->ops->view                            = DMView_Plex;
2145:   dm->ops->load                            = DMLoad_Plex;
2146:   dm->ops->setfromoptions                  = DMSetFromOptions_Plex;
2147:   dm->ops->clone                           = DMClone_Plex;
2148:   dm->ops->setup                           = DMSetUp_Plex;
2149:   dm->ops->createdefaultsection            = DMCreateDefaultSection_Plex;
2150:   dm->ops->createdefaultconstraints        = DMCreateDefaultConstraints_Plex;
2151:   dm->ops->createglobalvector              = DMCreateGlobalVector_Plex;
2152:   dm->ops->createlocalvector               = DMCreateLocalVector_Plex;
2153:   dm->ops->getlocaltoglobalmapping         = NULL;
2154:   dm->ops->createfieldis                   = NULL;
2155:   dm->ops->createcoordinatedm              = DMCreateCoordinateDM_Plex;
2156:   dm->ops->getcoloring                     = NULL;
2157:   dm->ops->creatematrix                    = DMCreateMatrix_Plex;
2158:   dm->ops->createinterpolation             = DMCreateInterpolation_Plex;
2159:   dm->ops->createmassmatrix                = DMCreateMassMatrix_Plex;
2160:   dm->ops->getaggregates                   = NULL;
2161:   dm->ops->getinjection                    = DMCreateInjection_Plex;
2162:   dm->ops->hascreateinjection              = DMHasCreateInjection_Plex;
2163:   dm->ops->refine                          = DMRefine_Plex;
2164:   dm->ops->coarsen                         = DMCoarsen_Plex;
2165:   dm->ops->refinehierarchy                 = DMRefineHierarchy_Plex;
2166:   dm->ops->coarsenhierarchy                = DMCoarsenHierarchy_Plex;
2167:   dm->ops->adaptlabel                      = DMAdaptLabel_Plex;
2168:   dm->ops->adaptmetric                     = DMAdaptMetric_Plex;
2169:   dm->ops->globaltolocalbegin              = NULL;
2170:   dm->ops->globaltolocalend                = NULL;
2171:   dm->ops->localtoglobalbegin              = NULL;
2172:   dm->ops->localtoglobalend                = NULL;
2173:   dm->ops->destroy                         = DMDestroy_Plex;
2174:   dm->ops->createsubdm                     = DMCreateSubDM_Plex;
2175:   dm->ops->createsuperdm                   = DMCreateSuperDM_Plex;
2176:   dm->ops->getdimpoints                    = DMGetDimPoints_Plex;
2177:   dm->ops->locatepoints                    = DMLocatePoints_Plex;
2178:   dm->ops->projectfunctionlocal            = DMProjectFunctionLocal_Plex;
2179:   dm->ops->projectfunctionlabellocal       = DMProjectFunctionLabelLocal_Plex;
2180:   dm->ops->projectfieldlocal               = DMProjectFieldLocal_Plex;
2181:   dm->ops->projectfieldlabellocal          = DMProjectFieldLabelLocal_Plex;
2182:   dm->ops->computel2diff                   = DMComputeL2Diff_Plex;
2183:   dm->ops->computel2gradientdiff           = DMComputeL2GradientDiff_Plex;
2184:   dm->ops->computel2fielddiff              = DMComputeL2FieldDiff_Plex;
2185:   dm->ops->getneighbors                    = DMGetNeighors_Plex;
2186:   PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C",DMPlexInsertBoundaryValues_Plex);
2187:   PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",DMSetUpGLVisViewer_Plex);
2188:   return(0);
2189: }

2191: PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm)
2192: {
2193:   DM_Plex        *mesh = (DM_Plex *) dm->data;

2197:   mesh->refct++;
2198:   (*newdm)->data = mesh;
2199:   PetscObjectChangeTypeName((PetscObject) *newdm, DMPLEX);
2200:   DMInitialize_Plex(*newdm);
2201:   return(0);
2202: }

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

2210:   Level: intermediate

2212: .seealso: DMType, DMPlexCreate(), DMCreate(), DMSetType()
2213: M*/

2215: PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm)
2216: {
2217:   DM_Plex        *mesh;
2218:   PetscInt       unit, d;

2223:   PetscNewLog(dm,&mesh);
2224:   dm->dim  = 0;
2225:   dm->data = mesh;

2227:   mesh->refct             = 1;
2228:   PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection);
2229:   mesh->maxConeSize       = 0;
2230:   mesh->cones             = NULL;
2231:   mesh->coneOrientations  = NULL;
2232:   PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection);
2233:   mesh->maxSupportSize    = 0;
2234:   mesh->supports          = NULL;
2235:   mesh->refinementUniform = PETSC_TRUE;
2236:   mesh->refinementLimit   = -1.0;

2238:   mesh->facesTmp = NULL;

2240:   mesh->tetgenOpts   = NULL;
2241:   mesh->triangleOpts = NULL;
2242:   PetscPartitionerCreate(PetscObjectComm((PetscObject)dm), &mesh->partitioner);
2243:   mesh->remeshBd     = PETSC_FALSE;

2245:   mesh->subpointMap = NULL;

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

2249:   mesh->regularRefinement   = PETSC_FALSE;
2250:   mesh->depthState          = -1;
2251:   mesh->globalVertexNumbers = NULL;
2252:   mesh->globalCellNumbers   = NULL;
2253:   mesh->anchorSection       = NULL;
2254:   mesh->anchorIS            = NULL;
2255:   mesh->createanchors       = NULL;
2256:   mesh->computeanchormatrix = NULL;
2257:   mesh->parentSection       = NULL;
2258:   mesh->parents             = NULL;
2259:   mesh->childIDs            = NULL;
2260:   mesh->childSection        = NULL;
2261:   mesh->children            = NULL;
2262:   mesh->referenceTree       = NULL;
2263:   mesh->getchildsymmetry    = NULL;
2264:   for (d = 0; d < 8; ++d) mesh->hybridPointMax[d] = PETSC_DETERMINE;
2265:   mesh->vtkCellHeight       = 0;
2266:   mesh->useAnchors          = PETSC_FALSE;

2268:   mesh->maxProjectionHeight = 0;

2270:   mesh->printSetValues = PETSC_FALSE;
2271:   mesh->printFEM       = 0;
2272:   mesh->printTol       = 1.0e-10;

2274:   DMInitialize_Plex(dm);
2275:   return(0);
2276: }

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

2281:   Collective on MPI_Comm

2283:   Input Parameter:
2284: . comm - The communicator for the DMPlex object

2286:   Output Parameter:
2287: . mesh  - The DMPlex object

2289:   Level: beginner

2291: .keywords: DMPlex, create
2292: @*/
2293: PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh)
2294: {

2299:   DMCreate(comm, mesh);
2300:   DMSetType(*mesh, DMPLEX);
2301:   return(0);
2302: }

2304: /*
2305:   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
2306: */
2307: static PetscErrorCode DMPlexBuildFromCellList_Parallel_Private(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const int cells[], PetscSF *sfVert)
2308: {
2309:   PetscSF         sfPoint;
2310:   PetscLayout     vLayout;
2311:   PetscHashI      vhash;
2312:   PetscSFNode    *remoteVerticesAdj, *vertexLocal, *vertexOwner, *remoteVertex;
2313:   const PetscInt *vrange;
2314:   PetscInt        numVerticesAdj, off, *verticesAdj, numVerticesGhost = 0, *localVertex, *cone, c, p, v, g;
2315:   PETSC_UNUSED PetscHashIIter ret, iter;
2316:   PetscMPIInt     rank, size;
2317:   PetscErrorCode  ierr;

2320:   MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);
2321:   MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size);
2322:   /* Partition vertices */
2323:   PetscLayoutCreate(PetscObjectComm((PetscObject) dm), &vLayout);
2324:   PetscLayoutSetLocalSize(vLayout, numVertices);
2325:   PetscLayoutSetBlockSize(vLayout, 1);
2326:   PetscLayoutSetUp(vLayout);
2327:   PetscLayoutGetRanges(vLayout, &vrange);
2328:   /* Count vertices and map them to procs */
2329:   PetscHashICreate(vhash);
2330:   for (c = 0; c < numCells; ++c) {
2331:     for (p = 0; p < numCorners; ++p) {
2332:       PetscHashIPut(vhash, cells[c*numCorners+p], ret, iter);
2333:     }
2334:   }
2335:   PetscHashISize(vhash, numVerticesAdj);
2336:   PetscMalloc1(numVerticesAdj, &verticesAdj);
2337:   off = 0; PetscHashIGetKeys(vhash, &off, verticesAdj);
2338:   if (off != numVerticesAdj) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid number of local vertices %D should be %D", off, numVerticesAdj);
2339:   PetscSortInt(numVerticesAdj, verticesAdj);
2340:   PetscMalloc1(numVerticesAdj, &remoteVerticesAdj);
2341:   for (v = 0; v < numVerticesAdj; ++v) {
2342:     const PetscInt gv = verticesAdj[v];
2343:     PetscInt       vrank;

2345:     PetscFindInt(gv, size+1, vrange, &vrank);
2346:     vrank = vrank < 0 ? -(vrank+2) : vrank;
2347:     remoteVerticesAdj[v].index = gv - vrange[vrank];
2348:     remoteVerticesAdj[v].rank  = vrank;
2349:   }
2350:   /* Create cones */
2351:   DMPlexSetChart(dm, 0, numCells+numVerticesAdj);
2352:   for (c = 0; c < numCells; ++c) {DMPlexSetConeSize(dm, c, numCorners);}
2353:   DMSetUp(dm);
2354:   DMGetWorkArray(dm, numCorners, MPIU_INT, &cone);
2355:   for (c = 0; c < numCells; ++c) {
2356:     for (p = 0; p < numCorners; ++p) {
2357:       const PetscInt gv = cells[c*numCorners+p];
2358:       PetscInt       lv;

2360:       PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv);
2361:       if (lv < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %D in local connectivity", gv);
2362:       cone[p] = lv+numCells;
2363:     }
2364:     DMPlexSetCone(dm, c, cone);
2365:   }
2366:   DMRestoreWorkArray(dm, numCorners, MPIU_INT, &cone);
2367:   /* Create SF for vertices */
2368:   PetscSFCreate(PetscObjectComm((PetscObject)dm), sfVert);
2369:   PetscObjectSetName((PetscObject) *sfVert, "Vertex Ownership SF");
2370:   PetscSFSetFromOptions(*sfVert);
2371:   PetscSFSetGraph(*sfVert, numVertices, numVerticesAdj, NULL, PETSC_OWN_POINTER, remoteVerticesAdj, PETSC_OWN_POINTER);
2372:   PetscFree(verticesAdj);
2373:   /* Build pointSF */
2374:   PetscMalloc2(numVerticesAdj, &vertexLocal, numVertices, &vertexOwner);
2375:   for (v = 0; v < numVerticesAdj; ++v) {vertexLocal[v].index = v+numCells; vertexLocal[v].rank = rank;}
2376:   for (v = 0; v < numVertices;    ++v) {vertexOwner[v].index = -1;         vertexOwner[v].rank = -1;}
2377:   PetscSFReduceBegin(*sfVert, MPIU_2INT, vertexLocal, vertexOwner, MPI_MAXLOC);
2378:   PetscSFReduceEnd(*sfVert, MPIU_2INT, vertexLocal, vertexOwner, MPI_MAXLOC);
2379:   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);
2380:   PetscSFBcastBegin(*sfVert, MPIU_2INT, vertexOwner, vertexLocal);
2381:   PetscSFBcastEnd(*sfVert, MPIU_2INT, vertexOwner, vertexLocal);
2382:   for (v = 0; v < numVerticesAdj; ++v) if (vertexLocal[v].rank != rank) ++numVerticesGhost;
2383:   PetscMalloc1(numVerticesGhost, &localVertex);
2384:   PetscMalloc1(numVerticesGhost, &remoteVertex);
2385:   for (v = 0, g = 0; v < numVerticesAdj; ++v) {
2386:     if (vertexLocal[v].rank != rank) {
2387:       localVertex[g]        = v+numCells;
2388:       remoteVertex[g].index = vertexLocal[v].index;
2389:       remoteVertex[g].rank  = vertexLocal[v].rank;
2390:       ++g;
2391:     }
2392:   }
2393:   PetscFree2(vertexLocal, vertexOwner);
2394:   if (g != numVerticesGhost) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid number of vertex ghosts %D should be %D", g, numVerticesGhost);
2395:   DMGetPointSF(dm, &sfPoint);
2396:   PetscObjectSetName((PetscObject) sfPoint, "point SF");
2397:   PetscSFSetGraph(sfPoint, numCells+numVerticesAdj, numVerticesGhost, localVertex, PETSC_OWN_POINTER, remoteVertex, PETSC_OWN_POINTER);
2398:   PetscLayoutDestroy(&vLayout);
2399:   PetscHashIDestroy(vhash);
2400:   /* Fill in the rest of the topology structure */
2401:   DMPlexSymmetrize(dm);
2402:   DMPlexStratify(dm);
2403:   return(0);
2404: }

2406: /*
2407:   This takes as input the coordinates for each owned vertex
2408: */
2409: static PetscErrorCode DMPlexBuildCoordinates_Parallel_Private(DM dm, PetscInt spaceDim, PetscInt numCells, PetscInt numV, PetscSF sfVert, const PetscReal vertexCoords[])
2410: {
2411:   PetscSection   coordSection;
2412:   Vec            coordinates;
2413:   PetscScalar   *coords;
2414:   PetscInt       numVertices, numVerticesAdj, coordSize, v;

2418:   DMSetCoordinateDim(dm, spaceDim);
2419:   PetscSFGetGraph(sfVert, &numVertices, &numVerticesAdj, NULL, NULL);
2420:   DMGetCoordinateSection(dm, &coordSection);
2421:   PetscSectionSetNumFields(coordSection, 1);
2422:   PetscSectionSetFieldComponents(coordSection, 0, spaceDim);
2423:   PetscSectionSetChart(coordSection, numCells, numCells + numVerticesAdj);
2424:   for (v = numCells; v < numCells+numVerticesAdj; ++v) {
2425:     PetscSectionSetDof(coordSection, v, spaceDim);
2426:     PetscSectionSetFieldDof(coordSection, v, 0, spaceDim);
2427:   }
2428:   PetscSectionSetUp(coordSection);
2429:   PetscSectionGetStorageSize(coordSection, &coordSize);
2430:   VecCreate(PetscObjectComm((PetscObject)dm), &coordinates);
2431:   VecSetBlockSize(coordinates, spaceDim);
2432:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
2433:   VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
2434:   VecSetType(coordinates,VECSTANDARD);
2435:   VecGetArray(coordinates, &coords);
2436:   {
2437:     MPI_Datatype coordtype;

2439:     /* Need a temp buffer for coords if we have complex/single */
2440:     MPI_Type_contiguous(spaceDim, MPIU_SCALAR, &coordtype);
2441:     MPI_Type_commit(&coordtype);
2442: #if defined(PETSC_USE_COMPLEX)
2443:     {
2444:     PetscScalar *svertexCoords;
2445:     PetscInt    i;
2446:     PetscMalloc1(numV*spaceDim,&svertexCoords);
2447:     for (i=0; i<numV*spaceDim; i++) svertexCoords[i] = vertexCoords[i];
2448:     PetscSFBcastBegin(sfVert, coordtype, svertexCoords, coords);
2449:     PetscSFBcastEnd(sfVert, coordtype, svertexCoords, coords);
2450:     PetscFree(svertexCoords);
2451:     }
2452: #else
2453:     PetscSFBcastBegin(sfVert, coordtype, vertexCoords, coords);
2454:     PetscSFBcastEnd(sfVert, coordtype, vertexCoords, coords);
2455: #endif
2456:     MPI_Type_free(&coordtype);
2457:   }
2458:   VecRestoreArray(coordinates, &coords);
2459:   DMSetCoordinatesLocal(dm, coordinates);
2460:   VecDestroy(&coordinates);
2461:   return(0);
2462: }

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

2467:   Input Parameters:
2468: + comm - The communicator
2469: . dim - The topological dimension of the mesh
2470: . numCells - The number of cells owned by this process
2471: . numVertices - The number of vertices owned by this process
2472: . numCorners - The number of vertices for each cell
2473: . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
2474: . cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
2475: . spaceDim - The spatial dimension used for coordinates
2476: - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex

2478:   Output Parameter:
2479: + dm - The DM
2480: - vertexSF - Optional, SF describing complete vertex ownership

2482:   Note: Two triangles sharing a face
2483: $
2484: $        2
2485: $      / | \
2486: $     /  |  \
2487: $    /   |   \
2488: $   0  0 | 1  3
2489: $    \   |   /
2490: $     \  |  /
2491: $      \ | /
2492: $        1
2493: would have input
2494: $  numCells = 2, numVertices = 4
2495: $  cells = [0 1 2  1 3 2]
2496: $
2497: which would result in the DMPlex
2498: $
2499: $        4
2500: $      / | \
2501: $     /  |  \
2502: $    /   |   \
2503: $   2  0 | 1  5
2504: $    \   |   /
2505: $     \  |  /
2506: $      \ | /
2507: $        3

2509:   Level: beginner

2511: .seealso: DMPlexCreateFromCellList(), DMPlexCreateFromDAG(), DMPlexCreate()
2512: @*/
2513: 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)
2514: {
2515:   PetscSF        sfVert;

2519:   DMCreate(comm, dm);
2520:   DMSetType(*dm, DMPLEX);
2523:   DMSetDimension(*dm, dim);
2524:   DMPlexBuildFromCellList_Parallel_Private(*dm, numCells, numVertices, numCorners, cells, &sfVert);
2525:   if (interpolate) {
2526:     DM idm;

2528:     DMPlexInterpolate(*dm, &idm);
2529:     DMDestroy(dm);
2530:     *dm  = idm;
2531:   }
2532:   DMPlexBuildCoordinates_Parallel_Private(*dm, spaceDim, numCells, numVertices,sfVert, vertexCoords);
2533:   if (vertexSF) *vertexSF = sfVert;
2534:   else {PetscSFDestroy(&sfVert);}
2535:   return(0);
2536: }

2538: /*
2539:   This takes as input the common mesh generator output, a list of the vertices for each cell
2540: */
2541: static PetscErrorCode DMPlexBuildFromCellList_Private(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const int cells[])
2542: {
2543:   PetscInt      *cone, c, p;

2547:   DMPlexSetChart(dm, 0, numCells+numVertices);
2548:   for (c = 0; c < numCells; ++c) {
2549:     DMPlexSetConeSize(dm, c, numCorners);
2550:   }
2551:   DMSetUp(dm);
2552:   DMGetWorkArray(dm, numCorners, MPIU_INT, &cone);
2553:   for (c = 0; c < numCells; ++c) {
2554:     for (p = 0; p < numCorners; ++p) {
2555:       cone[p] = cells[c*numCorners+p]+numCells;
2556:     }
2557:     DMPlexSetCone(dm, c, cone);
2558:   }
2559:   DMRestoreWorkArray(dm, numCorners, MPIU_INT, &cone);
2560:   DMPlexSymmetrize(dm);
2561:   DMPlexStratify(dm);
2562:   return(0);
2563: }

2565: /*
2566:   This takes as input the coordinates for each vertex
2567: */
2568: static PetscErrorCode DMPlexBuildCoordinates_Private(DM dm, PetscInt spaceDim, PetscInt numCells, PetscInt numVertices, const double vertexCoords[])
2569: {
2570:   PetscSection   coordSection;
2571:   Vec            coordinates;
2572:   DM             cdm;
2573:   PetscScalar   *coords;
2574:   PetscInt       v, d;

2578:   DMSetCoordinateDim(dm, spaceDim);
2579:   DMGetCoordinateSection(dm, &coordSection);
2580:   PetscSectionSetNumFields(coordSection, 1);
2581:   PetscSectionSetFieldComponents(coordSection, 0, spaceDim);
2582:   PetscSectionSetChart(coordSection, numCells, numCells + numVertices);
2583:   for (v = numCells; v < numCells+numVertices; ++v) {
2584:     PetscSectionSetDof(coordSection, v, spaceDim);
2585:     PetscSectionSetFieldDof(coordSection, v, 0, spaceDim);
2586:   }
2587:   PetscSectionSetUp(coordSection);

2589:   DMGetCoordinateDM(dm, &cdm);
2590:   DMCreateLocalVector(cdm, &coordinates);
2591:   VecSetBlockSize(coordinates, spaceDim);
2592:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
2593:   VecGetArray(coordinates, &coords);
2594:   for (v = 0; v < numVertices; ++v) {
2595:     for (d = 0; d < spaceDim; ++d) {
2596:       coords[v*spaceDim+d] = vertexCoords[v*spaceDim+d];
2597:     }
2598:   }
2599:   VecRestoreArray(coordinates, &coords);
2600:   DMSetCoordinatesLocal(dm, coordinates);
2601:   VecDestroy(&coordinates);
2602:   return(0);
2603: }

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

2608:   Input Parameters:
2609: + comm - The communicator
2610: . dim - The topological dimension of the mesh
2611: . numCells - The number of cells
2612: . numVertices - The number of vertices
2613: . numCorners - The number of vertices for each cell
2614: . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
2615: . cells - An array of numCells*numCorners numbers, the vertices for each cell
2616: . spaceDim - The spatial dimension used for coordinates
2617: - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex

2619:   Output Parameter:
2620: . dm - The DM

2622:   Note: Two triangles sharing a face
2623: $
2624: $        2
2625: $      / | \
2626: $     /  |  \
2627: $    /   |   \
2628: $   0  0 | 1  3
2629: $    \   |   /
2630: $     \  |  /
2631: $      \ | /
2632: $        1
2633: would have input
2634: $  numCells = 2, numVertices = 4
2635: $  cells = [0 1 2  1 3 2]
2636: $
2637: which would result in the DMPlex
2638: $
2639: $        4
2640: $      / | \
2641: $     /  |  \
2642: $    /   |   \
2643: $   2  0 | 1  5
2644: $    \   |   /
2645: $     \  |  /
2646: $      \ | /
2647: $        3

2649:   Level: beginner

2651: .seealso: DMPlexCreateFromDAG(), DMPlexCreate()
2652: @*/
2653: 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)
2654: {

2658:   DMCreate(comm, dm);
2659:   DMSetType(*dm, DMPLEX);
2660:   DMSetDimension(*dm, dim);
2661:   DMPlexBuildFromCellList_Private(*dm, numCells, numVertices, numCorners, cells);
2662:   if (interpolate) {
2663:     DM idm;

2665:     DMPlexInterpolate(*dm, &idm);
2666:     DMDestroy(dm);
2667:     *dm  = idm;
2668:   }
2669:   DMPlexBuildCoordinates_Private(*dm, spaceDim, numCells, numVertices, vertexCoords);
2670:   return(0);
2671: }

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

2676:   Input Parameters:
2677: + dm - The empty DM object, usually from DMCreate() and DMSetDimension()
2678: . depth - The depth of the DAG
2679: . numPoints - The number of points at each depth
2680: . coneSize - The cone size of each point
2681: . cones - The concatenation of the cone points for each point, the cone list must be oriented correctly for each point
2682: . coneOrientations - The orientation of each cone point
2683: - vertexCoords - An array of numVertices*dim numbers, the coordinates of each vertex

2685:   Output Parameter:
2686: . dm - The DM

2688:   Note: Two triangles sharing a face would have input
2689: $  depth = 1, numPoints = [4 2], coneSize = [3 3 0 0 0 0]
2690: $  cones = [2 3 4  3 5 4], coneOrientations = [0 0 0  0 0 0]
2691: $ vertexCoords = [-1.0 0.0  0.0 -1.0  0.0 1.0  1.0 0.0]
2692: $
2693: which would result in the DMPlex
2694: $
2695: $        4
2696: $      / | \
2697: $     /  |  \
2698: $    /   |   \
2699: $   2  0 | 1  5
2700: $    \   |   /
2701: $     \  |  /
2702: $      \ | /
2703: $        3
2704: $
2705: $ Notice that all points are numbered consecutively, unlikely DMPlexCreateFromCellList()

2707:   Level: advanced

2709: .seealso: DMPlexCreateFromCellList(), DMPlexCreate()
2710: @*/
2711: PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[])
2712: {
2713:   Vec            coordinates;
2714:   PetscSection   coordSection;
2715:   PetscScalar    *coords;
2716:   PetscInt       coordSize, firstVertex = -1, pStart = 0, pEnd = 0, p, v, dim, dimEmbed, d, off;

2720:   DMGetDimension(dm, &dim);
2721:   DMGetCoordinateDim(dm, &dimEmbed);
2722:   if (dimEmbed < dim) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Embedding dimension %d cannot be less than intrinsic dimension %d",dimEmbed,dim);
2723:   for (d = 0; d <= depth; ++d) pEnd += numPoints[d];
2724:   DMPlexSetChart(dm, pStart, pEnd);
2725:   for (p = pStart; p < pEnd; ++p) {
2726:     DMPlexSetConeSize(dm, p, coneSize[p-pStart]);
2727:     if (firstVertex < 0 && !coneSize[p - pStart]) {
2728:       firstVertex = p - pStart;
2729:     }
2730:   }
2731:   if (firstVertex < 0 && numPoints[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected %d vertices but could not find any", numPoints[0]);
2732:   DMSetUp(dm); /* Allocate space for cones */
2733:   for (p = pStart, off = 0; p < pEnd; off += coneSize[p-pStart], ++p) {
2734:     DMPlexSetCone(dm, p, &cones[off]);
2735:     DMPlexSetConeOrientation(dm, p, &coneOrientations[off]);
2736:   }
2737:   DMPlexSymmetrize(dm);
2738:   DMPlexStratify(dm);
2739:   /* Build coordinates */
2740:   DMGetCoordinateSection(dm, &coordSection);
2741:   PetscSectionSetNumFields(coordSection, 1);
2742:   PetscSectionSetFieldComponents(coordSection, 0, dimEmbed);
2743:   PetscSectionSetChart(coordSection, firstVertex, firstVertex+numPoints[0]);
2744:   for (v = firstVertex; v < firstVertex+numPoints[0]; ++v) {
2745:     PetscSectionSetDof(coordSection, v, dimEmbed);
2746:     PetscSectionSetFieldDof(coordSection, v, 0, dimEmbed);
2747:   }
2748:   PetscSectionSetUp(coordSection);
2749:   PetscSectionGetStorageSize(coordSection, &coordSize);
2750:   VecCreate(PETSC_COMM_SELF, &coordinates);
2751:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
2752:   VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
2753:   VecSetBlockSize(coordinates, dimEmbed);
2754:   VecSetType(coordinates,VECSTANDARD);
2755:   VecGetArray(coordinates, &coords);
2756:   for (v = 0; v < numPoints[0]; ++v) {
2757:     PetscInt off;

2759:     PetscSectionGetOffset(coordSection, v+firstVertex, &off);
2760:     for (d = 0; d < dimEmbed; ++d) {
2761:       coords[off+d] = vertexCoords[v*dimEmbed+d];
2762:     }
2763:   }
2764:   VecRestoreArray(coordinates, &coords);
2765:   DMSetCoordinatesLocal(dm, coordinates);
2766:   VecDestroy(&coordinates);
2767:   return(0);
2768: }

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

2773: + comm        - The MPI communicator
2774: . filename    - Name of the .dat file
2775: - interpolate - Create faces and edges in the mesh

2777:   Output Parameter:
2778: . dm  - The DM object representing the mesh

2780:   Note: The format is the simplest possible:
2781: $ Ne
2782: $ v0 v1 ... vk
2783: $ Nv
2784: $ x y z marker

2786:   Level: beginner

2788: .seealso: DMPlexCreateFromFile(), DMPlexCreateMedFromFile(), DMPlexCreateGmsh(), DMPlexCreate()
2789: @*/
2790: PetscErrorCode DMPlexCreateCellVertexFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
2791: {
2792:   DMLabel         marker;
2793:   PetscViewer     viewer;
2794:   Vec             coordinates;
2795:   PetscSection    coordSection;
2796:   PetscScalar    *coords;
2797:   char            line[PETSC_MAX_PATH_LEN];
2798:   PetscInt        dim = 3, cdim = 3, coordSize, v, c, d;
2799:   PetscMPIInt     rank;
2800:   int             snum, Nv, Nc;
2801:   PetscErrorCode  ierr;

2804:   MPI_Comm_rank(comm, &rank);
2805:   PetscViewerCreate(comm, &viewer);
2806:   PetscViewerSetType(viewer, PETSCVIEWERASCII);
2807:   PetscViewerFileSetMode(viewer, FILE_MODE_READ);
2808:   PetscViewerFileSetName(viewer, filename);
2809:   if (!rank) {
2810:     PetscViewerRead(viewer, line, 2, NULL, PETSC_STRING);
2811:     snum = sscanf(line, "%d %d", &Nc, &Nv);
2812:     if (snum != 2) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
2813:   } else {
2814:     Nc = Nv = 0;
2815:   }
2816:   DMCreate(comm, dm);
2817:   DMSetType(*dm, DMPLEX);
2818:   DMPlexSetChart(*dm, 0, Nc+Nv);
2819:   DMSetDimension(*dm, dim);
2820:   DMSetCoordinateDim(*dm, cdim);
2821:   /* Read topology */
2822:   if (!rank) {
2823:     PetscInt cone[8], corners = 8;
2824:     int      vbuf[8], v;

2826:     for (c = 0; c < Nc; ++c) {DMPlexSetConeSize(*dm, c, corners);}
2827:     DMSetUp(*dm);
2828:     for (c = 0; c < Nc; ++c) {
2829:       PetscViewerRead(viewer, line, corners, NULL, PETSC_STRING);
2830:       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]);
2831:       if (snum != corners) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
2832:       for (v = 0; v < corners; ++v) cone[v] = vbuf[v] + Nc;
2833:       /* Hexahedra are inverted */
2834:       {
2835:         PetscInt tmp = cone[1];
2836:         cone[1] = cone[3];
2837:         cone[3] = tmp;
2838:       }
2839:       DMPlexSetCone(*dm, c, cone);
2840:     }
2841:   }
2842:   DMPlexSymmetrize(*dm);
2843:   DMPlexStratify(*dm);
2844:   /* Read coordinates */
2845:   DMGetCoordinateSection(*dm, &coordSection);
2846:   PetscSectionSetNumFields(coordSection, 1);
2847:   PetscSectionSetFieldComponents(coordSection, 0, cdim);
2848:   PetscSectionSetChart(coordSection, Nc, Nc + Nv);
2849:   for (v = Nc; v < Nc+Nv; ++v) {
2850:     PetscSectionSetDof(coordSection, v, cdim);
2851:     PetscSectionSetFieldDof(coordSection, v, 0, cdim);
2852:   }
2853:   PetscSectionSetUp(coordSection);
2854:   PetscSectionGetStorageSize(coordSection, &coordSize);
2855:   VecCreate(PETSC_COMM_SELF, &coordinates);
2856:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
2857:   VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
2858:   VecSetBlockSize(coordinates, cdim);
2859:   VecSetType(coordinates, VECSTANDARD);
2860:   VecGetArray(coordinates, &coords);
2861:   if (!rank) {
2862:     double x[3];
2863:     int    val;

2865:     DMCreateLabel(*dm, "marker");
2866:     DMGetLabel(*dm, "marker", &marker);
2867:     for (v = 0; v < Nv; ++v) {
2868:       PetscViewerRead(viewer, line, 4, NULL, PETSC_STRING);
2869:       snum = sscanf(line, "%lg %lg %lg %d", &x[0], &x[1], &x[2], &val);
2870:       if (snum != 4) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
2871:       for (d = 0; d < cdim; ++d) coords[v*cdim+d] = x[d];
2872:       if (val) {DMLabelSetValue(marker, v+Nc, val);}
2873:     }
2874:   }
2875:   VecRestoreArray(coordinates, &coords);
2876:   DMSetCoordinatesLocal(*dm, coordinates);
2877:   VecDestroy(&coordinates);
2878:   PetscViewerDestroy(&viewer);
2879:   if (interpolate) {
2880:     DM      idm;
2881:     DMLabel bdlabel;

2883:     DMPlexInterpolate(*dm, &idm);
2884:     DMDestroy(dm);
2885:     *dm  = idm;

2887:     DMGetLabel(*dm, "marker", &bdlabel);
2888:     DMPlexMarkBoundaryFaces(*dm, PETSC_DETERMINE, bdlabel);
2889:     DMPlexLabelComplete(*dm, bdlabel);
2890:   }
2891:   return(0);
2892: }

2894: /*@C
2895:   DMPlexCreateFromFile - This takes a filename and produces a DM

2897:   Input Parameters:
2898: + comm - The communicator
2899: . filename - A file name
2900: - interpolate - Flag to create intermediate mesh pieces (edges, faces)

2902:   Output Parameter:
2903: . dm - The DM

2905:   Level: beginner

2907: .seealso: DMPlexCreateFromDAG(), DMPlexCreateFromCellList(), DMPlexCreate()
2908: @*/
2909: PetscErrorCode DMPlexCreateFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
2910: {
2911:   const char    *extGmsh    = ".msh";
2912:   const char    *extCGNS    = ".cgns";
2913:   const char    *extExodus  = ".exo";
2914:   const char    *extGenesis = ".gen";
2915:   const char    *extFluent  = ".cas";
2916:   const char    *extHDF5    = ".h5";
2917:   const char    *extMed     = ".med";
2918:   const char    *extPLY     = ".ply";
2919:   const char    *extCV      = ".dat";
2920:   size_t         len;
2921:   PetscBool      isGmsh, isCGNS, isExodus, isGenesis, isFluent, isHDF5, isMed, isPLY, isCV;
2922:   PetscMPIInt    rank;

2928:   MPI_Comm_rank(comm, &rank);
2929:   PetscStrlen(filename, &len);
2930:   if (!len) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Filename must be a valid path");
2931:   PetscStrncmp(&filename[PetscMax(0,len-4)], extGmsh,    4, &isGmsh);
2932:   PetscStrncmp(&filename[PetscMax(0,len-5)], extCGNS,    5, &isCGNS);
2933:   PetscStrncmp(&filename[PetscMax(0,len-4)], extExodus,  4, &isExodus);
2934:   PetscStrncmp(&filename[PetscMax(0,len-4)], extGenesis, 4, &isGenesis);
2935:   PetscStrncmp(&filename[PetscMax(0,len-4)], extFluent,  4, &isFluent);
2936:   PetscStrncmp(&filename[PetscMax(0,len-3)], extHDF5,    3, &isHDF5);
2937:   PetscStrncmp(&filename[PetscMax(0,len-4)], extMed,     4, &isMed);
2938:   PetscStrncmp(&filename[PetscMax(0,len-4)], extPLY,     4, &isPLY);
2939:   PetscStrncmp(&filename[PetscMax(0,len-4)], extCV,      4, &isCV);
2940:   if (isGmsh) {
2941:     DMPlexCreateGmshFromFile(comm, filename, interpolate, dm);
2942:   } else if (isCGNS) {
2943:     DMPlexCreateCGNSFromFile(comm, filename, interpolate, dm);
2944:   } else if (isExodus || isGenesis) {
2945:     DMPlexCreateExodusFromFile(comm, filename, interpolate, dm);
2946:   } else if (isFluent) {
2947:     DMPlexCreateFluentFromFile(comm, filename, interpolate, dm);
2948:   } else if (isHDF5) {
2949:     PetscViewer viewer;

2951:     PetscViewerCreate(comm, &viewer);
2952:     PetscViewerSetType(viewer, PETSCVIEWERHDF5);
2953:     PetscViewerFileSetMode(viewer, FILE_MODE_READ);
2954:     PetscViewerFileSetName(viewer, filename);
2955:     DMCreate(comm, dm);
2956:     DMSetType(*dm, DMPLEX);
2957:     DMLoad(*dm, viewer);
2958:     PetscViewerDestroy(&viewer);

2960:     if (interpolate) {
2961:       DM idm;

2963:       DMPlexInterpolate(*dm, &idm);
2964:       DMDestroy(dm);
2965:       *dm  = idm;
2966:     }
2967:   } else if (isMed) {
2968:     DMPlexCreateMedFromFile(comm, filename, interpolate, dm);
2969:   } else if (isPLY) {
2970:     DMPlexCreatePLYFromFile(comm, filename, interpolate, dm);
2971:   } else if (isCV) {
2972:     DMPlexCreateCellVertexFromFile(comm, filename, interpolate, dm);
2973:   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename);
2974:   return(0);
2975: }

2977: /*@
2978:   DMPlexCreateReferenceCell - Create a DMPLEX with the appropriate FEM reference cell

2980:   Collective on comm

2982:   Input Parameters:
2983: + comm    - The communicator
2984: . dim     - The spatial dimension
2985: - simplex - Flag for simplex, otherwise use a tensor-product cell

2987:   Output Parameter:
2988: . refdm - The reference cell

2990:   Level: intermediate

2992: .keywords: reference cell
2993: .seealso:
2994: @*/
2995: PetscErrorCode DMPlexCreateReferenceCell(MPI_Comm comm, PetscInt dim, PetscBool simplex, DM *refdm)
2996: {
2997:   DM             rdm;
2998:   Vec            coords;

3002:   DMCreate(comm, &rdm);
3003:   DMSetType(rdm, DMPLEX);
3004:   DMSetDimension(rdm, dim);
3005:   switch (dim) {
3006:   case 0:
3007:   {
3008:     PetscInt    numPoints[1]        = {1};
3009:     PetscInt    coneSize[1]         = {0};
3010:     PetscInt    cones[1]            = {0};
3011:     PetscInt    coneOrientations[1] = {0};
3012:     PetscScalar vertexCoords[1]     = {0.0};

3014:     DMPlexCreateFromDAG(rdm, 0, numPoints, coneSize, cones, coneOrientations, vertexCoords);
3015:   }
3016:   break;
3017:   case 1:
3018:   {
3019:     PetscInt    numPoints[2]        = {2, 1};
3020:     PetscInt    coneSize[3]         = {2, 0, 0};
3021:     PetscInt    cones[2]            = {1, 2};
3022:     PetscInt    coneOrientations[2] = {0, 0};
3023:     PetscScalar vertexCoords[2]     = {-1.0,  1.0};

3025:     DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
3026:   }
3027:   break;
3028:   case 2:
3029:     if (simplex) {
3030:       PetscInt    numPoints[2]        = {3, 1};
3031:       PetscInt    coneSize[4]         = {3, 0, 0, 0};
3032:       PetscInt    cones[3]            = {1, 2, 3};
3033:       PetscInt    coneOrientations[3] = {0, 0, 0};
3034:       PetscScalar vertexCoords[6]     = {-1.0, -1.0,  1.0, -1.0,  -1.0, 1.0};

3036:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
3037:     } else {
3038:       PetscInt    numPoints[2]        = {4, 1};
3039:       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
3040:       PetscInt    cones[4]            = {1, 2, 3, 4};
3041:       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
3042:       PetscScalar vertexCoords[8]     = {-1.0, -1.0,  1.0, -1.0,  1.0, 1.0,  -1.0, 1.0};

3044:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
3045:     }
3046:   break;
3047:   case 3:
3048:     if (simplex) {
3049:       PetscInt    numPoints[2]        = {4, 1};
3050:       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
3051:       PetscInt    cones[4]            = {1, 3, 2, 4};
3052:       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
3053:       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};

3055:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
3056:     } else {
3057:       PetscInt    numPoints[2]        = {8, 1};
3058:       PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
3059:       PetscInt    cones[8]            = {1, 4, 3, 2, 5, 6, 7, 8};
3060:       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
3061:       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,
3062:                                          -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  1.0, 1.0,  1.0,  -1.0, 1.0,  1.0};

3064:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
3065:     }
3066:   break;
3067:   default:
3068:     SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Cannot create reference cell for dimension %d", dim);
3069:   }
3070:   DMPlexInterpolate(rdm, refdm);
3071:   if (rdm->coordinateDM) {
3072:     DM           ncdm;
3073:     PetscSection cs;
3074:     PetscInt     pEnd = -1;

3076:     DMGetDefaultSection(rdm->coordinateDM, &cs);
3077:     if (cs) {PetscSectionGetChart(cs, NULL, &pEnd);}
3078:     if (pEnd >= 0) {
3079:       DMClone(rdm->coordinateDM, &ncdm);
3080:       DMSetDefaultSection(ncdm, cs);
3081:       DMSetCoordinateDM(*refdm, ncdm);
3082:       DMDestroy(&ncdm);
3083:     }
3084:   }
3085:   DMGetCoordinatesLocal(rdm, &coords);
3086:   if (coords) {
3087:     DMSetCoordinatesLocal(*refdm, coords);
3088:   } else {
3089:     DMGetCoordinates(rdm, &coords);
3090:     if (coords) {DMSetCoordinates(*refdm, coords);}
3091:   }
3092:   DMDestroy(&rdm);
3093:   return(0);
3094: }