Actual source code: plexcreate.c

petsc-master 2018-05-21
Report Typos and Errors
  1: #define PETSCDM_DLL
  2:  #include <petsc/private/dmpleximpl.h>
  3:  #include <petscsf.h>

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

  8:   Collective on MPI_Comm

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

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

 21:   Level: beginner

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

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

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

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

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

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

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

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

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

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

148:   Collective on MPI_Comm

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

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

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

170:   Level: beginner

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

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

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

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

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

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

295:   Collective on MPI_Comm

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

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

306:   Level: beginner

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

962:   Collective on MPI_Comm

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

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

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

992: and for simplicial cells

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

1008:   Level: beginner

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

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

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

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

1037:   Logically Collective on DM

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

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

1047:   Level: advanced

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

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

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

1066:   Collective on MPI_Comm

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

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

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

1107:   Level: beginner

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

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

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

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

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

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

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

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

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

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

1310:        so we remap them using

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

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

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

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

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

1365:   Collective on MPI_Comm

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

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

1375:   Level: beginner

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

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

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

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

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

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

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

1476:   Collective on MPI_Comm

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

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

1486:   Level: beginner

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1855: /* External function declarations here */
1856: extern PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling);
1857: extern PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat);
1858: extern PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mat);
1859: extern PetscErrorCode DMCreateDefaultSection_Plex(DM dm);
1860: extern PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm);
1861: extern PetscErrorCode DMCreateMatrix_Plex(DM dm,  Mat *J);
1862: extern PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm);
1863: extern PetscErrorCode DMCreateCoordinateField_Plex(DM dm, DMField *field);
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->createcoordinatefield           = DMCreateCoordinateField_Plex;
2157:   dm->ops->getcoloring                     = NULL;
2158:   dm->ops->creatematrix                    = DMCreateMatrix_Plex;
2159:   dm->ops->createinterpolation             = DMCreateInterpolation_Plex;
2160:   dm->ops->createmassmatrix                = DMCreateMassMatrix_Plex;
2161:   dm->ops->getaggregates                   = NULL;
2162:   dm->ops->getinjection                    = DMCreateInjection_Plex;
2163:   dm->ops->hascreateinjection              = DMHasCreateInjection_Plex;
2164:   dm->ops->refine                          = DMRefine_Plex;
2165:   dm->ops->coarsen                         = DMCoarsen_Plex;
2166:   dm->ops->refinehierarchy                 = DMRefineHierarchy_Plex;
2167:   dm->ops->coarsenhierarchy                = DMCoarsenHierarchy_Plex;
2168:   dm->ops->adaptlabel                      = DMAdaptLabel_Plex;
2169:   dm->ops->adaptmetric                     = DMAdaptMetric_Plex;
2170:   dm->ops->globaltolocalbegin              = NULL;
2171:   dm->ops->globaltolocalend                = NULL;
2172:   dm->ops->localtoglobalbegin              = NULL;
2173:   dm->ops->localtoglobalend                = NULL;
2174:   dm->ops->destroy                         = DMDestroy_Plex;
2175:   dm->ops->createsubdm                     = DMCreateSubDM_Plex;
2176:   dm->ops->createsuperdm                   = DMCreateSuperDM_Plex;
2177:   dm->ops->getdimpoints                    = DMGetDimPoints_Plex;
2178:   dm->ops->locatepoints                    = DMLocatePoints_Plex;
2179:   dm->ops->projectfunctionlocal            = DMProjectFunctionLocal_Plex;
2180:   dm->ops->projectfunctionlabellocal       = DMProjectFunctionLabelLocal_Plex;
2181:   dm->ops->projectfieldlocal               = DMProjectFieldLocal_Plex;
2182:   dm->ops->projectfieldlabellocal          = DMProjectFieldLabelLocal_Plex;
2183:   dm->ops->computel2diff                   = DMComputeL2Diff_Plex;
2184:   dm->ops->computel2gradientdiff           = DMComputeL2GradientDiff_Plex;
2185:   dm->ops->computel2fielddiff              = DMComputeL2FieldDiff_Plex;
2186:   dm->ops->getneighbors                    = DMGetNeighors_Plex;
2187:   PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C",DMPlexInsertBoundaryValues_Plex);
2188:   PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",DMSetUpGLVisViewer_Plex);
2189:   return(0);
2190: }

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

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

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

2211:   Level: intermediate

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

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

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

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

2239:   mesh->facesTmp = NULL;

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

2246:   mesh->subpointMap = NULL;

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

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

2269:   mesh->maxProjectionHeight = 0;

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

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

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

2282:   Collective on MPI_Comm

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

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

2290:   Level: beginner

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

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

2305: /*
2306:   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
2307: */
2308: /* TODO: invertCells and spaceDim arguments could be added also to to DMPlexCreateFromCellListParallel(), DMPlexBuildFromCellList_Internal() and DMPlexCreateFromCellList() */
2309: PetscErrorCode DMPlexBuildFromCellList_Parallel_Internal(DM dm, PetscInt spaceDim, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const int cells[], PetscBool invertCells, PetscSF *sfVert)
2310: {
2311:   PetscSF         sfPoint;
2312:   PetscLayout     vLayout;
2313:   PetscHashI      vhash;
2314:   PetscSFNode    *remoteVerticesAdj, *vertexLocal, *vertexOwner, *remoteVertex;
2315:   const PetscInt *vrange;
2316:   PetscInt        numVerticesAdj, off, *verticesAdj, numVerticesGhost = 0, *localVertex, *cone, c, p, v, g;
2317:   PETSC_UNUSED PetscHashIIter ret, iter;
2318:   PetscMPIInt     rank, size;
2319:   PetscErrorCode  ierr;

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

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

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

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

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

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

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

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

2481:   Output Parameter:
2482: + dm - The DM
2483: - vertexSF - Optional, SF describing complete vertex ownership

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

2512:   Level: beginner

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

2522:   DMCreate(comm, dm);
2523:   DMSetType(*dm, DMPLEX);
2526:   DMSetDimension(*dm, dim);
2527:   DMPlexBuildFromCellList_Parallel_Internal(*dm, spaceDim, numCells, numVertices, numCorners, cells, PETSC_FALSE, &sfVert);
2528:   if (interpolate) {
2529:     DM idm;

2531:     DMPlexInterpolate(*dm, &idm);
2532:     DMDestroy(dm);
2533:     *dm  = idm;
2534:   }
2535:   DMPlexBuildCoordinates_Parallel_Internal(*dm, spaceDim, numCells, numVertices, sfVert, vertexCoords);
2536:   if (vertexSF) *vertexSF = sfVert;
2537:   else {PetscSFDestroy(&sfVert);}
2538:   return(0);
2539: }

2541: /*
2542:   This takes as input the common mesh generator output, a list of the vertices for each cell
2543: */
2544: PetscErrorCode DMPlexBuildFromCellList_Internal(DM dm, PetscInt spaceDim, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const int cells[], PetscBool invertCells)
2545: {
2546:   PetscInt      *cone, c, p;

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

2569: /*
2570:   This takes as input the coordinates for each vertex
2571: */
2572: PetscErrorCode DMPlexBuildCoordinates_Internal(DM dm, PetscInt spaceDim, PetscInt numCells, PetscInt numVertices, const double vertexCoords[])
2573: {
2574:   PetscSection   coordSection;
2575:   Vec            coordinates;
2576:   DM             cdm;
2577:   PetscScalar   *coords;
2578:   PetscInt       v, d;

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

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

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

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

2623:   Output Parameter:
2624: . dm - The DM

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

2653:   Level: beginner

2655: .seealso: DMPlexCreateFromDAG(), DMPlexCreate()
2656: @*/
2657: 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)
2658: {

2662:   DMCreate(comm, dm);
2663:   DMSetType(*dm, DMPLEX);
2664:   DMSetDimension(*dm, dim);
2665:   DMPlexBuildFromCellList_Internal(*dm, spaceDim, numCells, numVertices, numCorners, cells, PETSC_FALSE);
2666:   if (interpolate) {
2667:     DM idm;

2669:     DMPlexInterpolate(*dm, &idm);
2670:     DMDestroy(dm);
2671:     *dm  = idm;
2672:   }
2673:   DMPlexBuildCoordinates_Internal(*dm, spaceDim, numCells, numVertices, vertexCoords);
2674:   return(0);
2675: }

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

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

2689:   Output Parameter:
2690: . dm - The DM

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

2711:   Level: advanced

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

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

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

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

2777: + comm        - The MPI communicator
2778: . filename    - Name of the .dat file
2779: - interpolate - Create faces and edges in the mesh

2781:   Output Parameter:
2782: . dm  - The DM object representing the mesh

2784:   Note: The format is the simplest possible:
2785: $ Ne
2786: $ v0 v1 ... vk
2787: $ Nv
2788: $ x y z marker

2790:   Level: beginner

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

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

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

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

2887:     DMPlexInterpolate(*dm, &idm);
2888:     DMDestroy(dm);
2889:     *dm  = idm;

2891:     DMGetLabel(*dm, "marker", &bdlabel);
2892:     DMPlexMarkBoundaryFaces(*dm, PETSC_DETERMINE, bdlabel);
2893:     DMPlexLabelComplete(*dm, bdlabel);
2894:   }
2895:   return(0);
2896: }

2898: /*@C
2899:   DMPlexCreateFromFile - This takes a filename and produces a DM

2901:   Input Parameters:
2902: + comm - The communicator
2903: . filename - A file name
2904: - interpolate - Flag to create intermediate mesh pieces (edges, faces)

2906:   Output Parameter:
2907: . dm - The DM

2909:   Options Database Keys:
2910: . -dm_plex_create_from_hdf5_xdmf - use the PETSC_VIEWER_HDF5_XDMF format for reading HDF5 

2912:   Level: beginner

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

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

2959:     PetscOptionsGetBool(NULL, NULL, "-dm_plex_create_from_hdf5_xdmf", &load_hdf5_xdmf, NULL);
2960:     PetscViewerCreate(comm, &viewer);
2961:     PetscViewerSetType(viewer, PETSCVIEWERHDF5);
2962:     PetscViewerFileSetMode(viewer, FILE_MODE_READ);
2963:     PetscViewerFileSetName(viewer, filename);
2964:     DMCreate(comm, dm);
2965:     DMSetType(*dm, DMPLEX);
2966:     if (load_hdf5_xdmf) {PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_XDMF);}
2967:     DMLoad(*dm, viewer);
2968:     if (load_hdf5_xdmf) {PetscViewerPopFormat(viewer);}
2969:     PetscViewerDestroy(&viewer);

2971:     if (interpolate) {
2972:       DM idm;

2974:       DMPlexInterpolate(*dm, &idm);
2975:       DMDestroy(dm);
2976:       *dm  = idm;
2977:     }
2978:   } else if (isMed) {
2979:     DMPlexCreateMedFromFile(comm, filename, interpolate, dm);
2980:   } else if (isPLY) {
2981:     DMPlexCreatePLYFromFile(comm, filename, interpolate, dm);
2982:   } else if (isCV) {
2983:     DMPlexCreateCellVertexFromFile(comm, filename, interpolate, dm);
2984:   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename);
2985:   return(0);
2986: }

2988: /*@
2989:   DMPlexCreateReferenceCell - Create a DMPLEX with the appropriate FEM reference cell

2991:   Collective on comm

2993:   Input Parameters:
2994: + comm    - The communicator
2995: . dim     - The spatial dimension
2996: - simplex - Flag for simplex, otherwise use a tensor-product cell

2998:   Output Parameter:
2999: . refdm - The reference cell

3001:   Level: intermediate

3003: .keywords: reference cell
3004: .seealso:
3005: @*/
3006: PetscErrorCode DMPlexCreateReferenceCell(MPI_Comm comm, PetscInt dim, PetscBool simplex, DM *refdm)
3007: {
3008:   DM             rdm;
3009:   Vec            coords;

3013:   DMCreate(comm, &rdm);
3014:   DMSetType(rdm, DMPLEX);
3015:   DMSetDimension(rdm, dim);
3016:   switch (dim) {
3017:   case 0:
3018:   {
3019:     PetscInt    numPoints[1]        = {1};
3020:     PetscInt    coneSize[1]         = {0};
3021:     PetscInt    cones[1]            = {0};
3022:     PetscInt    coneOrientations[1] = {0};
3023:     PetscScalar vertexCoords[1]     = {0.0};

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

3036:     DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
3037:   }
3038:   break;
3039:   case 2:
3040:     if (simplex) {
3041:       PetscInt    numPoints[2]        = {3, 1};
3042:       PetscInt    coneSize[4]         = {3, 0, 0, 0};
3043:       PetscInt    cones[3]            = {1, 2, 3};
3044:       PetscInt    coneOrientations[3] = {0, 0, 0};
3045:       PetscScalar vertexCoords[6]     = {-1.0, -1.0,  1.0, -1.0,  -1.0, 1.0};

3047:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
3048:     } else {
3049:       PetscInt    numPoints[2]        = {4, 1};
3050:       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
3051:       PetscInt    cones[4]            = {1, 2, 3, 4};
3052:       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
3053:       PetscScalar vertexCoords[8]     = {-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:     }
3057:   break;
3058:   case 3:
3059:     if (simplex) {
3060:       PetscInt    numPoints[2]        = {4, 1};
3061:       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
3062:       PetscInt    cones[4]            = {1, 3, 2, 4};
3063:       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
3064:       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};

3066:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
3067:     } else {
3068:       PetscInt    numPoints[2]        = {8, 1};
3069:       PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
3070:       PetscInt    cones[8]            = {1, 4, 3, 2, 5, 6, 7, 8};
3071:       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
3072:       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,
3073:                                          -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  1.0, 1.0,  1.0,  -1.0, 1.0,  1.0};

3075:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
3076:     }
3077:   break;
3078:   default:
3079:     SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Cannot create reference cell for dimension %d", dim);
3080:   }
3081:   DMPlexInterpolate(rdm, refdm);
3082:   if (rdm->coordinateDM) {
3083:     DM           ncdm;
3084:     PetscSection cs;
3085:     PetscInt     pEnd = -1;

3087:     DMGetDefaultSection(rdm->coordinateDM, &cs);
3088:     if (cs) {PetscSectionGetChart(cs, NULL, &pEnd);}
3089:     if (pEnd >= 0) {
3090:       DMClone(rdm->coordinateDM, &ncdm);
3091:       DMSetDefaultSection(ncdm, cs);
3092:       DMSetCoordinateDM(*refdm, ncdm);
3093:       DMDestroy(&ncdm);
3094:     }
3095:   }
3096:   DMGetCoordinatesLocal(rdm, &coords);
3097:   if (coords) {
3098:     DMSetCoordinatesLocal(*refdm, coords);
3099:   } else {
3100:     DMGetCoordinates(rdm, &coords);
3101:     if (coords) {DMSetCoordinates(*refdm, coords);}
3102:   }
3103:   DMDestroy(&rdm);
3104:   return(0);
3105: }