Actual source code: plexcreate.c

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

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

  9:   Collective on MPI_Comm

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

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

 22:   Level: beginner

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

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

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

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

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

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

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

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

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

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

149:   Collective on MPI_Comm

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

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

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

171:   Level: beginner

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

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

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

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

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

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

296:   Collective on MPI_Comm

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

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

307:   Level: beginner

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

326:   if ((faces[0] < 1) || (faces[1] < 1) || (faces[2] < 1)) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Must have at least 1 face per side");
327:   MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
328:   vertices[0] = faces[0]+1; vertices[1] = faces[1]+1; vertices[2] = faces[2]+1;
329:   numVertices = vertices[0]*vertices[1]*vertices[2];
330:   if (!rank) {
331:     PetscInt f;

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

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

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

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

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

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

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

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


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

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

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

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

490: static PetscErrorCode DMPlexCreateBoxMesh_Simplex_Internal(MPI_Comm comm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate, DM *dm)
491: {
492:   DM             boundary;
493:   PetscInt       i;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

952:     DMPlexUninterpolate(*dm, &udm);
953:     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;

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

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

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

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

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

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

1278:     bdType[0] = DM_BOUNDARY_NONE;
1279:     bdType[1] = DM_BOUNDARY_NONE;
1280:     bdType[2] = periodicZ;
1281:     for (i = 0; i < dim; i++) {
1282:       L[i]       = upper[i] - lower[i];
1283:       maxCell[i] = 1.1 * (L[i] / numZCells);
1284:     }
1285:     DMSetPeriodicity(*dm, PETSC_TRUE, maxCell, L, bdType);
1286:   }
1287:   /* Refine topology */
1288:   for (r = 0; r < numRefine; ++r) {
1289:     DM rdm = NULL;

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

1300:          phi     = arctan(y/x)
1301:          d_close = sqrt(1/8 + 1/4 sin^2(phi))
1302:          d_far   = sqrt(1/2 + sin^2(phi))

1304:        so we remap them using

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

1309:        If pi/4 < phi < 3pi/4 or -3pi/4 < phi < -pi/4, then we switch x and y.
1310:   */
1311:   {
1312:     Vec           coordinates;
1313:     PetscSection  coordSection;
1314:     PetscScalar  *coords;
1315:     PetscInt      vStart, vEnd, v;
1316:     const PetscReal dis = 1.0/PetscSqrtReal(2.0);
1317:     const PetscReal ds2 = 0.5*dis;

1319:     DMPlexGetDepthStratum(*dm, 0, &vStart, &vEnd);
1320:     DMGetCoordinateSection(*dm, &coordSection);
1321:     DMGetCoordinatesLocal(*dm, &coordinates);
1322:     VecGetArray(coordinates, &coords);
1323:     for (v = vStart; v < vEnd; ++v) {
1324:       PetscReal phi, sinp, cosp, dc, df, x, y, xc, yc;
1325:       PetscInt  off;

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

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

1359:   Collective on MPI_Comm

1361:   Input Parameters:
1362: + comm - The communicator for the DM object
1363: . n    - The number of wedges around the origin
1364: - interpolate - Create edges and faces

1366:   Output Parameter:
1367: . dm  - The DM object

1369:   Level: beginner

1371: .keywords: DM, create
1372: .seealso: DMPlexCreateHexCylinderMesh(), DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
1373: @*/
1374: PetscErrorCode DMPlexCreateWedgeCylinderMesh(MPI_Comm comm, PetscInt n, PetscBool interpolate, DM *dm)
1375: {
1376:   const PetscInt dim = 3;
1377:   PetscInt       numCells, numVertices;

1382:   if (n < 0) SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Number of wedges %D cannot be negative", n);
1383:   DMCreate(comm, dm);
1384:   DMSetType(*dm, DMPLEX);
1385:   DMSetDimension(*dm, dim);
1386:   /* Create topology */
1387:   {
1388:     PetscInt cone[6], c;

1390:     numCells    = n;
1391:     numVertices = 2*(n+1);
1392:     DMPlexSetChart(*dm, 0, numCells+numVertices);
1393:     for (c = 0; c < numCells; c++) {DMPlexSetConeSize(*dm, c, 6);}
1394:     DMSetUp(*dm);
1395:     for (c = 0; c < numCells; c++) {
1396:       cone[0] =  c+n*1; cone[1] = (c+1)%n+n*1; cone[2] = 0+3*n;
1397:       cone[3] =  c+n*2; cone[4] = (c+1)%n+n*2; cone[5] = 1+3*n;
1398:       DMPlexSetCone(*dm, c, cone);
1399:     }
1400:     DMPlexSymmetrize(*dm);
1401:     DMPlexStratify(*dm);
1402:   }
1403:   /* Interpolate */
1404:   if (interpolate) {
1405:     DM idm;

1407:     DMPlexInterpolate(*dm, &idm);
1408:     DMDestroy(dm);
1409:     *dm  = idm;
1410:   }
1411:   /* Create cylinder geometry */
1412:   {
1413:     Vec          coordinates;
1414:     PetscSection coordSection;
1415:     PetscScalar *coords;
1416:     PetscInt     coordSize, v, c;

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

1448: PETSC_STATIC_INLINE PetscReal DiffNormReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
1449: {
1450:   PetscReal prod = 0.0;
1451:   PetscInt  i;
1452:   for (i = 0; i < dim; ++i) prod += PetscSqr(x[i] - y[i]);
1453:   return PetscSqrtReal(prod);
1454: }
1455: PETSC_STATIC_INLINE PetscReal DotReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
1456: {
1457:   PetscReal prod = 0.0;
1458:   PetscInt  i;
1459:   for (i = 0; i < dim; ++i) prod += x[i]*y[i];
1460:   return prod;
1461: }

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

1466:   Collective on MPI_Comm

1468:   Input Parameters:
1469: . comm  - The communicator for the DM object
1470: . dim   - The dimension
1471: - simplex - Use simplices, or tensor product cells

1473:   Output Parameter:
1474: . dm  - The DM object

1476:   Level: beginner

1478: .keywords: DM, create
1479: .seealso: DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
1480: @*/
1481: PetscErrorCode DMPlexCreateSphereMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, DM *dm)
1482: {
1483:   const PetscInt  embedDim = dim+1;
1484:   PetscSection    coordSection;
1485:   Vec             coordinates;
1486:   PetscScalar    *coords;
1487:   PetscReal      *coordsIn;
1488:   PetscInt        numCells, numEdges, numVerts, firstVertex, v, firstEdge, coordSize, d, c, e;
1489:   PetscMPIInt     rank;
1490:   PetscErrorCode  ierr;

1494:   DMCreate(comm, dm);
1495:   DMSetType(*dm, DMPLEX);
1496:   DMSetDimension(*dm, dim);
1497:   DMSetCoordinateDim(*dm, dim+1);
1498:   MPI_Comm_rank(PetscObjectComm((PetscObject) *dm), &rank);
1499:   switch (dim) {
1500:   case 2:
1501:     if (simplex) {
1502:       DM              idm;
1503:       const PetscReal edgeLen     = 2.0/(1.0 + PETSC_PHI);
1504:       const PetscReal vertex[3]   = {0.0, 1.0/(1.0 + PETSC_PHI), PETSC_PHI/(1.0 + PETSC_PHI)};
1505:       const PetscInt  degree      = 5;
1506:       PetscInt        s[3]        = {1, 1, 1};
1507:       PetscInt        cone[3];
1508:       PetscInt       *graph, p, i, j, k;

1510:       numCells    = !rank ? 20 : 0;
1511:       numVerts    = !rank ? 12 : 0;
1512:       firstVertex = numCells;
1513:       /* Use icosahedron, which for a unit sphere has coordinates which are all cyclic permutations of

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

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

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

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

1698:       numCells    = !rank ? 600 : 0;
1699:       numVerts    = !rank ? 120 : 0;
1700:       firstVertex = numCells;
1701:       /* Use the 600-cell, which for a unit sphere has coordinates which are

1703:            1/2 (\pm 1, \pm 1,    \pm 1, \pm 1)                          16
1704:                (\pm 1,    0,       0,      0)  all cyclic permutations   8
1705:            1/2 (\pm 1, \pm phi, \pm 1/phi, 0)  all even permutations    96

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

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

1774:                                                         {{{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0, -1}, { 0,  0,  1, 0}},
1775:                                                          {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
1776:                                                          {{0,  0,  0,  1}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, {-1,  0,  0, 0}},
1777:                                                          {{0,  0, -1,  0}, { 0, 0,  0,  0}, { 1,  0, 0,  0}, { 0,  0,  0, 0}}},

1779:                                                         {{{0,  0,  0,  0}, { 0, 0,  0,  1}, { 0,  0, 0,  0}, { 0, -1,  0, 0}},
1780:                                                          {{0,  0,  0, -1}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 1,  0,  0, 0}},
1781:                                                          {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
1782:                                                          {{0,  1,  0,  0}, {-1, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}}},

1784:                                                         {{{0,  0,  0,  0}, { 0, 0, -1,  0}, { 0,  1, 0,  0}, { 0,  0,  0, 0}},
1785:                                                          {{0,  0,  1,  0}, { 0, 0,  0,  0}, {-1,  0, 0,  0}, { 0,  0,  0, 0}},
1786:                                                          {{0, -1,  0,  0}, { 1, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
1787:                                                          {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}}}};
1788:                   PetscReal normal[4];
1789:                   PetscInt  e, f, g;

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

1845: /* External function declarations here */
1846: extern PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling);
1847: extern PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat);
1848: extern PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mat);
1849: extern PetscErrorCode DMCreateDefaultSection_Plex(DM dm);
1850: extern PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm);
1851: extern PetscErrorCode DMCreateMatrix_Plex(DM dm,  Mat *J);
1852: extern PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm);
1853: PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm);
1854: extern PetscErrorCode DMSetUp_Plex(DM dm);
1855: extern PetscErrorCode DMDestroy_Plex(DM dm);
1856: extern PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer);
1857: extern PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer);
1858: extern PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm);
1859: static PetscErrorCode DMInitialize_Plex(DM dm);

1861: /* Replace dm with the contents of dmNew
1862:    - Share the DM_Plex structure
1863:    - Share the coordinates
1864:    - Share the SF
1865: */
1866: static PetscErrorCode DMPlexReplace_Static(DM dm, DM dmNew)
1867: {
1868:   PetscSF               sf;
1869:   DM                    coordDM, coarseDM;
1870:   Vec                   coords;
1871:   PetscBool             isper;
1872:   const PetscReal      *maxCell, *L;
1873:   const DMBoundaryType *bd;
1874:   PetscErrorCode        ierr;

1877:   DMGetPointSF(dmNew, &sf);
1878:   DMSetPointSF(dm, sf);
1879:   DMGetCoordinateDM(dmNew, &coordDM);
1880:   DMGetCoordinatesLocal(dmNew, &coords);
1881:   DMSetCoordinateDM(dm, coordDM);
1882:   DMSetCoordinatesLocal(dm, coords);
1883:   DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);
1884:   DMSetPeriodicity(dmNew, isper, maxCell, L, bd);
1885:   DMDestroy_Plex(dm);
1886:   DMInitialize_Plex(dm);
1887:   dm->data = dmNew->data;
1888:   ((DM_Plex *) dmNew->data)->refct++;
1889:   dmNew->labels->refct++;
1890:   if (!--(dm->labels->refct)) {
1891:     DMLabelLink next = dm->labels->next;

1893:     /* destroy the labels */
1894:     while (next) {
1895:       DMLabelLink tmp = next->next;

1897:       DMLabelDestroy(&next->label);
1898:       PetscFree(next);
1899:       next = tmp;
1900:     }
1901:     PetscFree(dm->labels);
1902:   }
1903:   dm->labels = dmNew->labels;
1904:   dm->depthLabel = dmNew->depthLabel;
1905:   DMGetCoarseDM(dmNew,&coarseDM);
1906:   DMSetCoarseDM(dm,coarseDM);
1907:   return(0);
1908: }

1910: /* Swap dm with the contents of dmNew
1911:    - Swap the DM_Plex structure
1912:    - Swap the coordinates
1913:    - Swap the point PetscSF
1914: */
1915: static PetscErrorCode DMPlexSwap_Static(DM dmA, DM dmB)
1916: {
1917:   DM              coordDMA, coordDMB;
1918:   Vec             coordsA,  coordsB;
1919:   PetscSF         sfA,      sfB;
1920:   void            *tmp;
1921:   DMLabelLinkList listTmp;
1922:   DMLabel         depthTmp;
1923:   PetscErrorCode  ierr;

1926:   DMGetPointSF(dmA, &sfA);
1927:   DMGetPointSF(dmB, &sfB);
1928:   PetscObjectReference((PetscObject) sfA);
1929:   DMSetPointSF(dmA, sfB);
1930:   DMSetPointSF(dmB, sfA);
1931:   PetscObjectDereference((PetscObject) sfA);

1933:   DMGetCoordinateDM(dmA, &coordDMA);
1934:   DMGetCoordinateDM(dmB, &coordDMB);
1935:   PetscObjectReference((PetscObject) coordDMA);
1936:   DMSetCoordinateDM(dmA, coordDMB);
1937:   DMSetCoordinateDM(dmB, coordDMA);
1938:   PetscObjectDereference((PetscObject) coordDMA);

1940:   DMGetCoordinatesLocal(dmA, &coordsA);
1941:   DMGetCoordinatesLocal(dmB, &coordsB);
1942:   PetscObjectReference((PetscObject) coordsA);
1943:   DMSetCoordinatesLocal(dmA, coordsB);
1944:   DMSetCoordinatesLocal(dmB, coordsA);
1945:   PetscObjectDereference((PetscObject) coordsA);

1947:   tmp       = dmA->data;
1948:   dmA->data = dmB->data;
1949:   dmB->data = tmp;
1950:   listTmp   = dmA->labels;
1951:   dmA->labels = dmB->labels;
1952:   dmB->labels = listTmp;
1953:   depthTmp  = dmA->depthLabel;
1954:   dmA->depthLabel = dmB->depthLabel;
1955:   dmB->depthLabel = depthTmp;
1956:   return(0);
1957: }

1959: PetscErrorCode DMSetFromOptions_NonRefinement_Plex(PetscOptionItems *PetscOptionsObject,DM dm)
1960: {
1961:   DM_Plex       *mesh = (DM_Plex*) dm->data;

1965:   /* Handle viewing */
1966:   PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMView", PETSC_FALSE, &mesh->printSetValues, NULL);
1967:   PetscOptionsInt("-dm_plex_print_fem", "Debug output level all fem computations", "DMView", 0, &mesh->printFEM, NULL);
1968:   PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMView", mesh->printTol, &mesh->printTol, NULL);
1969:   /* Point Location */
1970:   PetscOptionsBool("-dm_plex_hash_location", "Use grid hashing for point location", "DMView", PETSC_FALSE, &mesh->useHashLocation, NULL);
1971:   /* Generation and remeshing */
1972:   PetscOptionsBool("-dm_plex_remesh_bd", "Allow changes to the boundary on remeshing", "DMView", PETSC_FALSE, &mesh->remeshBd, NULL);
1973:   /* Projection behavior */
1974:   PetscOptionsInt("-dm_plex_max_projection_height", "Maxmimum mesh point height used to project locally", "DMPlexSetMaxProjectionHeight", 0, &mesh->maxProjectionHeight, NULL);
1975:   PetscOptionsBool("-dm_plex_regular_refinement", "Use special nested projection algorithm for regular refinement", "DMPlexSetRegularRefinement", mesh->regularRefinement, &mesh->regularRefinement, NULL);

1977:   PetscPartitionerSetFromOptions(mesh->partitioner);
1978:   return(0);
1979: }

1981: static PetscErrorCode DMSetFromOptions_Plex(PetscOptionItems *PetscOptionsObject,DM dm)
1982: {
1983:   PetscInt       refine = 0, coarsen = 0, r;
1984:   PetscBool      isHierarchy;

1989:   PetscOptionsHead(PetscOptionsObject,"DMPlex Options");
1990:   /* Handle DMPlex refinement */
1991:   PetscOptionsInt("-dm_refine", "The number of uniform refinements", "DMCreate", refine, &refine, NULL);
1992:   PetscOptionsInt("-dm_refine_hierarchy", "The number of uniform refinements", "DMCreate", refine, &refine, &isHierarchy);
1993:   if (refine) {DMPlexSetRefinementUniform(dm, PETSC_TRUE);}
1994:   if (refine && isHierarchy) {
1995:     DM *dms, coarseDM;

1997:     DMGetCoarseDM(dm, &coarseDM);
1998:     PetscObjectReference((PetscObject)coarseDM);
1999:     PetscMalloc1(refine,&dms);
2000:     DMRefineHierarchy(dm, refine, dms);
2001:     /* Total hack since we do not pass in a pointer */
2002:     DMPlexSwap_Static(dm, dms[refine-1]);
2003:     if (refine == 1) {
2004:       DMSetCoarseDM(dm, dms[0]);
2005:       DMPlexSetRegularRefinement(dm, PETSC_TRUE);
2006:     } else {
2007:       DMSetCoarseDM(dm, dms[refine-2]);
2008:       DMPlexSetRegularRefinement(dm, PETSC_TRUE);
2009:       DMSetCoarseDM(dms[0], dms[refine-1]);
2010:       DMPlexSetRegularRefinement(dms[0], PETSC_TRUE);
2011:     }
2012:     DMSetCoarseDM(dms[refine-1], coarseDM);
2013:     PetscObjectDereference((PetscObject)coarseDM);
2014:     /* Free DMs */
2015:     for (r = 0; r < refine; ++r) {
2016:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]);
2017:       DMDestroy(&dms[r]);
2018:     }
2019:     PetscFree(dms);
2020:   } else {
2021:     for (r = 0; r < refine; ++r) {
2022:       DM refinedMesh;

2024:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
2025:       DMRefine(dm, PetscObjectComm((PetscObject) dm), &refinedMesh);
2026:       /* Total hack since we do not pass in a pointer */
2027:       DMPlexReplace_Static(dm, refinedMesh);
2028:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
2029:       DMDestroy(&refinedMesh);
2030:     }
2031:   }
2032:   /* Handle DMPlex coarsening */
2033:   PetscOptionsInt("-dm_coarsen", "Coarsen the mesh", "DMCreate", coarsen, &coarsen, NULL);
2034:   PetscOptionsInt("-dm_coarsen_hierarchy", "The number of coarsenings", "DMCreate", coarsen, &coarsen, &isHierarchy);
2035:   if (coarsen && isHierarchy) {
2036:     DM *dms;

2038:     PetscMalloc1(coarsen, &dms);
2039:     DMCoarsenHierarchy(dm, coarsen, dms);
2040:     /* Free DMs */
2041:     for (r = 0; r < coarsen; ++r) {
2042:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]);
2043:       DMDestroy(&dms[r]);
2044:     }
2045:     PetscFree(dms);
2046:   } else {
2047:     for (r = 0; r < coarsen; ++r) {
2048:       DM coarseMesh;

2050:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
2051:       DMCoarsen(dm, PetscObjectComm((PetscObject) dm), &coarseMesh);
2052:       /* Total hack since we do not pass in a pointer */
2053:       DMPlexReplace_Static(dm, coarseMesh);
2054:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
2055:       DMDestroy(&coarseMesh);
2056:     }
2057:   }
2058:   /* Handle */
2059:   DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
2060:   PetscOptionsTail();
2061:   return(0);
2062: }

2064: static PetscErrorCode DMCreateGlobalVector_Plex(DM dm,Vec *vec)
2065: {

2069:   DMCreateGlobalVector_Section_Private(dm,vec);
2070:   /* VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM); */
2071:   VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex);
2072:   VecSetOperation(*vec, VECOP_VIEWNATIVE, (void (*)(void)) VecView_Plex_Native);
2073:   VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex);
2074:   VecSetOperation(*vec, VECOP_LOADNATIVE, (void (*)(void)) VecLoad_Plex_Native);
2075:   return(0);
2076: }

2078: static PetscErrorCode DMCreateLocalVector_Plex(DM dm,Vec *vec)
2079: {

2083:   DMCreateLocalVector_Section_Private(dm,vec);
2084:   VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex_Local);
2085:   VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex_Local);
2086:   return(0);
2087: }

2089: static PetscErrorCode DMGetDimPoints_Plex(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
2090: {
2091:   PetscInt       depth, d;

2095:   DMPlexGetDepth(dm, &depth);
2096:   if (depth == 1) {
2097:     DMGetDimension(dm, &d);
2098:     if (dim == 0)      {DMPlexGetDepthStratum(dm, dim, pStart, pEnd);}
2099:     else if (dim == d) {DMPlexGetDepthStratum(dm, 1, pStart, pEnd);}
2100:     else               {*pStart = 0; *pEnd = 0;}
2101:   } else {
2102:     DMPlexGetDepthStratum(dm, dim, pStart, pEnd);
2103:   }
2104:   return(0);
2105: }

2107: static PetscErrorCode DMGetNeighors_Plex(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
2108: {
2109:   PetscSF        sf;

2113:   DMGetPointSF(dm, &sf);
2114:   PetscSFGetRanks(sf, nranks, ranks, NULL, NULL, NULL);
2115:   return(0);
2116: }

2118: static PetscErrorCode DMInitialize_Plex(DM dm)
2119: {

2123:   dm->ops->view                            = DMView_Plex;
2124:   dm->ops->load                            = DMLoad_Plex;
2125:   dm->ops->setfromoptions                  = DMSetFromOptions_Plex;
2126:   dm->ops->clone                           = DMClone_Plex;
2127:   dm->ops->setup                           = DMSetUp_Plex;
2128:   dm->ops->createdefaultsection            = DMCreateDefaultSection_Plex;
2129:   dm->ops->createdefaultconstraints        = DMCreateDefaultConstraints_Plex;
2130:   dm->ops->createglobalvector              = DMCreateGlobalVector_Plex;
2131:   dm->ops->createlocalvector               = DMCreateLocalVector_Plex;
2132:   dm->ops->getlocaltoglobalmapping         = NULL;
2133:   dm->ops->createfieldis                   = NULL;
2134:   dm->ops->createcoordinatedm              = DMCreateCoordinateDM_Plex;
2135:   dm->ops->getcoloring                     = NULL;
2136:   dm->ops->creatematrix                    = DMCreateMatrix_Plex;
2137:   dm->ops->createinterpolation             = DMCreateInterpolation_Plex;
2138:   dm->ops->createmassmatrix                = DMCreateMassMatrix_Plex;
2139:   dm->ops->getaggregates                   = NULL;
2140:   dm->ops->getinjection                    = DMCreateInjection_Plex;
2141:   dm->ops->refine                          = DMRefine_Plex;
2142:   dm->ops->coarsen                         = DMCoarsen_Plex;
2143:   dm->ops->refinehierarchy                 = DMRefineHierarchy_Plex;
2144:   dm->ops->coarsenhierarchy                = DMCoarsenHierarchy_Plex;
2145:   dm->ops->adaptlabel                      = DMAdaptLabel_Plex;
2146:   dm->ops->adaptmetric                     = DMAdaptMetric_Plex;
2147:   dm->ops->globaltolocalbegin              = NULL;
2148:   dm->ops->globaltolocalend                = NULL;
2149:   dm->ops->localtoglobalbegin              = NULL;
2150:   dm->ops->localtoglobalend                = NULL;
2151:   dm->ops->destroy                         = DMDestroy_Plex;
2152:   dm->ops->createsubdm                     = DMCreateSubDM_Plex;
2153:   dm->ops->getdimpoints                    = DMGetDimPoints_Plex;
2154:   dm->ops->locatepoints                    = DMLocatePoints_Plex;
2155:   dm->ops->projectfunctionlocal            = DMProjectFunctionLocal_Plex;
2156:   dm->ops->projectfunctionlabellocal       = DMProjectFunctionLabelLocal_Plex;
2157:   dm->ops->projectfieldlocal               = DMProjectFieldLocal_Plex;
2158:   dm->ops->projectfieldlabellocal          = DMProjectFieldLabelLocal_Plex;
2159:   dm->ops->computel2diff                   = DMComputeL2Diff_Plex;
2160:   dm->ops->computel2gradientdiff           = DMComputeL2GradientDiff_Plex;
2161:   dm->ops->computel2fielddiff              = DMComputeL2FieldDiff_Plex;
2162:   dm->ops->getneighbors                    = DMGetNeighors_Plex;
2163:   PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C",DMPlexInsertBoundaryValues_Plex);
2164:   PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",DMSetUpGLVisViewer_Plex);
2165:   return(0);
2166: }

2168: PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm)
2169: {
2170:   DM_Plex        *mesh = (DM_Plex *) dm->data;

2174:   mesh->refct++;
2175:   (*newdm)->data = mesh;
2176:   PetscObjectChangeTypeName((PetscObject) *newdm, DMPLEX);
2177:   DMInitialize_Plex(*newdm);
2178:   return(0);
2179: }

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

2187:   Level: intermediate

2189: .seealso: DMType, DMPlexCreate(), DMCreate(), DMSetType()
2190: M*/

2192: PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm)
2193: {
2194:   DM_Plex        *mesh;
2195:   PetscInt       unit, d;

2200:   PetscNewLog(dm,&mesh);
2201:   dm->dim  = 0;
2202:   dm->data = mesh;

2204:   mesh->refct             = 1;
2205:   PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection);
2206:   mesh->maxConeSize       = 0;
2207:   mesh->cones             = NULL;
2208:   mesh->coneOrientations  = NULL;
2209:   PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection);
2210:   mesh->maxSupportSize    = 0;
2211:   mesh->supports          = NULL;
2212:   mesh->refinementUniform = PETSC_TRUE;
2213:   mesh->refinementLimit   = -1.0;

2215:   mesh->facesTmp = NULL;

2217:   mesh->tetgenOpts   = NULL;
2218:   mesh->triangleOpts = NULL;
2219:   PetscPartitionerCreate(PetscObjectComm((PetscObject)dm), &mesh->partitioner);
2220:   mesh->remeshBd     = PETSC_FALSE;

2222:   mesh->subpointMap = NULL;

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

2226:   mesh->regularRefinement   = PETSC_FALSE;
2227:   mesh->depthState          = -1;
2228:   mesh->globalVertexNumbers = NULL;
2229:   mesh->globalCellNumbers   = NULL;
2230:   mesh->anchorSection       = NULL;
2231:   mesh->anchorIS            = NULL;
2232:   mesh->createanchors       = NULL;
2233:   mesh->computeanchormatrix = NULL;
2234:   mesh->parentSection       = NULL;
2235:   mesh->parents             = NULL;
2236:   mesh->childIDs            = NULL;
2237:   mesh->childSection        = NULL;
2238:   mesh->children            = NULL;
2239:   mesh->referenceTree       = NULL;
2240:   mesh->getchildsymmetry    = NULL;
2241:   for (d = 0; d < 8; ++d) mesh->hybridPointMax[d] = PETSC_DETERMINE;
2242:   mesh->vtkCellHeight       = 0;
2243:   mesh->useAnchors          = PETSC_FALSE;

2245:   mesh->maxProjectionHeight = 0;

2247:   mesh->printSetValues = PETSC_FALSE;
2248:   mesh->printFEM       = 0;
2249:   mesh->printTol       = 1.0e-10;

2251:   DMInitialize_Plex(dm);
2252:   return(0);
2253: }

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

2258:   Collective on MPI_Comm

2260:   Input Parameter:
2261: . comm - The communicator for the DMPlex object

2263:   Output Parameter:
2264: . mesh  - The DMPlex object

2266:   Level: beginner

2268: .keywords: DMPlex, create
2269: @*/
2270: PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh)
2271: {

2276:   DMCreate(comm, mesh);
2277:   DMSetType(*mesh, DMPLEX);
2278:   return(0);
2279: }

2281: /*
2282:   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
2283: */
2284: static PetscErrorCode DMPlexBuildFromCellList_Parallel_Private(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const int cells[], PetscSF *sfVert)
2285: {
2286:   PetscSF         sfPoint;
2287:   PetscLayout     vLayout;
2288:   PetscHashI      vhash;
2289:   PetscSFNode    *remoteVerticesAdj, *vertexLocal, *vertexOwner, *remoteVertex;
2290:   const PetscInt *vrange;
2291:   PetscInt        numVerticesAdj, off, *verticesAdj, numVerticesGhost = 0, *localVertex, *cone, c, p, v, g;
2292:   PETSC_UNUSED PetscHashIIter ret, iter;
2293:   PetscMPIInt     rank, size;
2294:   PetscErrorCode  ierr;

2297:   MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);
2298:   MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size);
2299:   /* Partition vertices */
2300:   PetscLayoutCreate(PetscObjectComm((PetscObject) dm), &vLayout);
2301:   PetscLayoutSetLocalSize(vLayout, numVertices);
2302:   PetscLayoutSetBlockSize(vLayout, 1);
2303:   PetscLayoutSetUp(vLayout);
2304:   PetscLayoutGetRanges(vLayout, &vrange);
2305:   /* Count vertices and map them to procs */
2306:   PetscHashICreate(vhash);
2307:   for (c = 0; c < numCells; ++c) {
2308:     for (p = 0; p < numCorners; ++p) {
2309:       PetscHashIPut(vhash, cells[c*numCorners+p], ret, iter);
2310:     }
2311:   }
2312:   PetscHashISize(vhash, numVerticesAdj);
2313:   PetscMalloc1(numVerticesAdj, &verticesAdj);
2314:   off = 0; PetscHashIGetKeys(vhash, &off, verticesAdj);
2315:   if (off != numVerticesAdj) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid number of local vertices %D should be %D", off, numVerticesAdj);
2316:   PetscSortInt(numVerticesAdj, verticesAdj);
2317:   PetscMalloc1(numVerticesAdj, &remoteVerticesAdj);
2318:   for (v = 0; v < numVerticesAdj; ++v) {
2319:     const PetscInt gv = verticesAdj[v];
2320:     PetscInt       vrank;

2322:     PetscFindInt(gv, size+1, vrange, &vrank);
2323:     vrank = vrank < 0 ? -(vrank+2) : vrank;
2324:     remoteVerticesAdj[v].index = gv - vrange[vrank];
2325:     remoteVerticesAdj[v].rank  = vrank;
2326:   }
2327:   /* Create cones */
2328:   DMPlexSetChart(dm, 0, numCells+numVerticesAdj);
2329:   for (c = 0; c < numCells; ++c) {DMPlexSetConeSize(dm, c, numCorners);}
2330:   DMSetUp(dm);
2331:   DMGetWorkArray(dm, numCorners, MPIU_INT, &cone);
2332:   for (c = 0; c < numCells; ++c) {
2333:     for (p = 0; p < numCorners; ++p) {
2334:       const PetscInt gv = cells[c*numCorners+p];
2335:       PetscInt       lv;

2337:       PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv);
2338:       if (lv < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %D in local connectivity", gv);
2339:       cone[p] = lv+numCells;
2340:     }
2341:     DMPlexSetCone(dm, c, cone);
2342:   }
2343:   DMRestoreWorkArray(dm, numCorners, MPIU_INT, &cone);
2344:   /* Create SF for vertices */
2345:   PetscSFCreate(PetscObjectComm((PetscObject)dm), sfVert);
2346:   PetscObjectSetName((PetscObject) *sfVert, "Vertex Ownership SF");
2347:   PetscSFSetFromOptions(*sfVert);
2348:   PetscSFSetGraph(*sfVert, numVertices, numVerticesAdj, NULL, PETSC_OWN_POINTER, remoteVerticesAdj, PETSC_OWN_POINTER);
2349:   PetscFree(verticesAdj);
2350:   /* Build pointSF */
2351:   PetscMalloc2(numVerticesAdj, &vertexLocal, numVertices, &vertexOwner);
2352:   for (v = 0; v < numVerticesAdj; ++v) {vertexLocal[v].index = v+numCells; vertexLocal[v].rank = rank;}
2353:   for (v = 0; v < numVertices;    ++v) {vertexOwner[v].index = -1;         vertexOwner[v].rank = -1;}
2354:   PetscSFReduceBegin(*sfVert, MPIU_2INT, vertexLocal, vertexOwner, MPI_MAXLOC);
2355:   PetscSFReduceEnd(*sfVert, MPIU_2INT, vertexLocal, vertexOwner, MPI_MAXLOC);
2356:   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);
2357:   PetscSFBcastBegin(*sfVert, MPIU_2INT, vertexOwner, vertexLocal);
2358:   PetscSFBcastEnd(*sfVert, MPIU_2INT, vertexOwner, vertexLocal);
2359:   for (v = 0; v < numVerticesAdj; ++v) if (vertexLocal[v].rank != rank) ++numVerticesGhost;
2360:   PetscMalloc1(numVerticesGhost, &localVertex);
2361:   PetscMalloc1(numVerticesGhost, &remoteVertex);
2362:   for (v = 0, g = 0; v < numVerticesAdj; ++v) {
2363:     if (vertexLocal[v].rank != rank) {
2364:       localVertex[g]        = v+numCells;
2365:       remoteVertex[g].index = vertexLocal[v].index;
2366:       remoteVertex[g].rank  = vertexLocal[v].rank;
2367:       ++g;
2368:     }
2369:   }
2370:   PetscFree2(vertexLocal, vertexOwner);
2371:   if (g != numVerticesGhost) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid number of vertex ghosts %D should be %D", g, numVerticesGhost);
2372:   DMGetPointSF(dm, &sfPoint);
2373:   PetscObjectSetName((PetscObject) sfPoint, "point SF");
2374:   PetscSFSetGraph(sfPoint, numCells+numVerticesAdj, numVerticesGhost, localVertex, PETSC_OWN_POINTER, remoteVertex, PETSC_OWN_POINTER);
2375:   PetscLayoutDestroy(&vLayout);
2376:   PetscHashIDestroy(vhash);
2377:   /* Fill in the rest of the topology structure */
2378:   DMPlexSymmetrize(dm);
2379:   DMPlexStratify(dm);
2380:   return(0);
2381: }

2383: /*
2384:   This takes as input the coordinates for each owned vertex
2385: */
2386: static PetscErrorCode DMPlexBuildCoordinates_Parallel_Private(DM dm, PetscInt spaceDim, PetscInt numCells, PetscInt numV, PetscSF sfVert, const PetscReal vertexCoords[])
2387: {
2388:   PetscSection   coordSection;
2389:   Vec            coordinates;
2390:   PetscScalar   *coords;
2391:   PetscInt       numVertices, numVerticesAdj, coordSize, v;

2395:   DMSetCoordinateDim(dm, spaceDim);
2396:   PetscSFGetGraph(sfVert, &numVertices, &numVerticesAdj, NULL, NULL);
2397:   DMGetCoordinateSection(dm, &coordSection);
2398:   PetscSectionSetNumFields(coordSection, 1);
2399:   PetscSectionSetFieldComponents(coordSection, 0, spaceDim);
2400:   PetscSectionSetChart(coordSection, numCells, numCells + numVerticesAdj);
2401:   for (v = numCells; v < numCells+numVerticesAdj; ++v) {
2402:     PetscSectionSetDof(coordSection, v, spaceDim);
2403:     PetscSectionSetFieldDof(coordSection, v, 0, spaceDim);
2404:   }
2405:   PetscSectionSetUp(coordSection);
2406:   PetscSectionGetStorageSize(coordSection, &coordSize);
2407:   VecCreate(PetscObjectComm((PetscObject)dm), &coordinates);
2408:   VecSetBlockSize(coordinates, spaceDim);
2409:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
2410:   VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
2411:   VecSetType(coordinates,VECSTANDARD);
2412:   VecGetArray(coordinates, &coords);
2413:   {
2414:     MPI_Datatype coordtype;

2416:     /* Need a temp buffer for coords if we have complex/single */
2417:     MPI_Type_contiguous(spaceDim, MPIU_SCALAR, &coordtype);
2418:     MPI_Type_commit(&coordtype);
2419: #if defined(PETSC_USE_COMPLEX)
2420:     {
2421:     PetscScalar *svertexCoords;
2422:     PetscInt    i;
2423:     PetscMalloc1(numV*spaceDim,&svertexCoords);
2424:     for (i=0; i<numV*spaceDim; i++) svertexCoords[i] = vertexCoords[i];
2425:     PetscSFBcastBegin(sfVert, coordtype, svertexCoords, coords);
2426:     PetscSFBcastEnd(sfVert, coordtype, svertexCoords, coords);
2427:     PetscFree(svertexCoords);
2428:     }
2429: #else
2430:     PetscSFBcastBegin(sfVert, coordtype, vertexCoords, coords);
2431:     PetscSFBcastEnd(sfVert, coordtype, vertexCoords, coords);
2432: #endif
2433:     MPI_Type_free(&coordtype);
2434:   }
2435:   VecRestoreArray(coordinates, &coords);
2436:   DMSetCoordinatesLocal(dm, coordinates);
2437:   VecDestroy(&coordinates);
2438:   return(0);
2439: }

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

2444:   Input Parameters:
2445: + comm - The communicator
2446: . dim - The topological dimension of the mesh
2447: . numCells - The number of cells owned by this process
2448: . numVertices - The number of vertices owned by this process
2449: . numCorners - The number of vertices for each cell
2450: . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
2451: . cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
2452: . spaceDim - The spatial dimension used for coordinates
2453: - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex

2455:   Output Parameter:
2456: + dm - The DM
2457: - vertexSF - Optional, SF describing complete vertex ownership

2459:   Note: Two triangles sharing a face
2460: $
2461: $        2
2462: $      / | \
2463: $     /  |  \
2464: $    /   |   \
2465: $   0  0 | 1  3
2466: $    \   |   /
2467: $     \  |  /
2468: $      \ | /
2469: $        1
2470: would have input
2471: $  numCells = 2, numVertices = 4
2472: $  cells = [0 1 2  1 3 2]
2473: $
2474: which would result in the DMPlex
2475: $
2476: $        4
2477: $      / | \
2478: $     /  |  \
2479: $    /   |   \
2480: $   2  0 | 1  5
2481: $    \   |   /
2482: $     \  |  /
2483: $      \ | /
2484: $        3

2486:   Level: beginner

2488: .seealso: DMPlexCreateFromCellList(), DMPlexCreateFromDAG(), DMPlexCreate()
2489: @*/
2490: 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)
2491: {
2492:   PetscSF        sfVert;

2496:   DMCreate(comm, dm);
2497:   DMSetType(*dm, DMPLEX);
2500:   DMSetDimension(*dm, dim);
2501:   DMPlexBuildFromCellList_Parallel_Private(*dm, numCells, numVertices, numCorners, cells, &sfVert);
2502:   if (interpolate) {
2503:     DM idm;

2505:     DMPlexInterpolate(*dm, &idm);
2506:     DMDestroy(dm);
2507:     *dm  = idm;
2508:   }
2509:   DMPlexBuildCoordinates_Parallel_Private(*dm, spaceDim, numCells, numVertices,sfVert, vertexCoords);
2510:   if (vertexSF) *vertexSF = sfVert;
2511:   else {PetscSFDestroy(&sfVert);}
2512:   return(0);
2513: }

2515: /*
2516:   This takes as input the common mesh generator output, a list of the vertices for each cell
2517: */
2518: static PetscErrorCode DMPlexBuildFromCellList_Private(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const int cells[])
2519: {
2520:   PetscInt      *cone, c, p;

2524:   DMPlexSetChart(dm, 0, numCells+numVertices);
2525:   for (c = 0; c < numCells; ++c) {
2526:     DMPlexSetConeSize(dm, c, numCorners);
2527:   }
2528:   DMSetUp(dm);
2529:   DMGetWorkArray(dm, numCorners, MPIU_INT, &cone);
2530:   for (c = 0; c < numCells; ++c) {
2531:     for (p = 0; p < numCorners; ++p) {
2532:       cone[p] = cells[c*numCorners+p]+numCells;
2533:     }
2534:     DMPlexSetCone(dm, c, cone);
2535:   }
2536:   DMRestoreWorkArray(dm, numCorners, MPIU_INT, &cone);
2537:   DMPlexSymmetrize(dm);
2538:   DMPlexStratify(dm);
2539:   return(0);
2540: }

2542: /*
2543:   This takes as input the coordinates for each vertex
2544: */
2545: static PetscErrorCode DMPlexBuildCoordinates_Private(DM dm, PetscInt spaceDim, PetscInt numCells, PetscInt numVertices, const double vertexCoords[])
2546: {
2547:   PetscSection   coordSection;
2548:   Vec            coordinates;
2549:   DM             cdm;
2550:   PetscScalar   *coords;
2551:   PetscInt       v, d;

2555:   DMSetCoordinateDim(dm, spaceDim);
2556:   DMGetCoordinateSection(dm, &coordSection);
2557:   PetscSectionSetNumFields(coordSection, 1);
2558:   PetscSectionSetFieldComponents(coordSection, 0, spaceDim);
2559:   PetscSectionSetChart(coordSection, numCells, numCells + numVertices);
2560:   for (v = numCells; v < numCells+numVertices; ++v) {
2561:     PetscSectionSetDof(coordSection, v, spaceDim);
2562:     PetscSectionSetFieldDof(coordSection, v, 0, spaceDim);
2563:   }
2564:   PetscSectionSetUp(coordSection);

2566:   DMGetCoordinateDM(dm, &cdm);
2567:   DMCreateLocalVector(cdm, &coordinates);
2568:   VecSetBlockSize(coordinates, spaceDim);
2569:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
2570:   VecGetArray(coordinates, &coords);
2571:   for (v = 0; v < numVertices; ++v) {
2572:     for (d = 0; d < spaceDim; ++d) {
2573:       coords[v*spaceDim+d] = vertexCoords[v*spaceDim+d];
2574:     }
2575:   }
2576:   VecRestoreArray(coordinates, &coords);
2577:   DMSetCoordinatesLocal(dm, coordinates);
2578:   VecDestroy(&coordinates);
2579:   return(0);
2580: }

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

2585:   Input Parameters:
2586: + comm - The communicator
2587: . dim - The topological dimension of the mesh
2588: . numCells - The number of cells
2589: . numVertices - The number of vertices
2590: . numCorners - The number of vertices for each cell
2591: . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
2592: . cells - An array of numCells*numCorners numbers, the vertices for each cell
2593: . spaceDim - The spatial dimension used for coordinates
2594: - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex

2596:   Output Parameter:
2597: . dm - The DM

2599:   Note: Two triangles sharing a face
2600: $
2601: $        2
2602: $      / | \
2603: $     /  |  \
2604: $    /   |   \
2605: $   0  0 | 1  3
2606: $    \   |   /
2607: $     \  |  /
2608: $      \ | /
2609: $        1
2610: would have input
2611: $  numCells = 2, numVertices = 4
2612: $  cells = [0 1 2  1 3 2]
2613: $
2614: which would result in the DMPlex
2615: $
2616: $        4
2617: $      / | \
2618: $     /  |  \
2619: $    /   |   \
2620: $   2  0 | 1  5
2621: $    \   |   /
2622: $     \  |  /
2623: $      \ | /
2624: $        3

2626:   Level: beginner

2628: .seealso: DMPlexCreateFromDAG(), DMPlexCreate()
2629: @*/
2630: 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)
2631: {

2635:   DMCreate(comm, dm);
2636:   DMSetType(*dm, DMPLEX);
2637:   DMSetDimension(*dm, dim);
2638:   DMPlexBuildFromCellList_Private(*dm, numCells, numVertices, numCorners, cells);
2639:   if (interpolate) {
2640:     DM idm;

2642:     DMPlexInterpolate(*dm, &idm);
2643:     DMDestroy(dm);
2644:     *dm  = idm;
2645:   }
2646:   DMPlexBuildCoordinates_Private(*dm, spaceDim, numCells, numVertices, vertexCoords);
2647:   return(0);
2648: }

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

2653:   Input Parameters:
2654: + dm - The empty DM object, usually from DMCreate() and DMSetDimension()
2655: . depth - The depth of the DAG
2656: . numPoints - The number of points at each depth
2657: . coneSize - The cone size of each point
2658: . cones - The concatenation of the cone points for each point, the cone list must be oriented correctly for each point
2659: . coneOrientations - The orientation of each cone point
2660: - vertexCoords - An array of numVertices*dim numbers, the coordinates of each vertex

2662:   Output Parameter:
2663: . dm - The DM

2665:   Note: Two triangles sharing a face would have input
2666: $  depth = 1, numPoints = [4 2], coneSize = [3 3 0 0 0 0]
2667: $  cones = [2 3 4  3 5 4], coneOrientations = [0 0 0  0 0 0]
2668: $ vertexCoords = [-1.0 0.0  0.0 -1.0  0.0 1.0  1.0 0.0]
2669: $
2670: which would result in the DMPlex
2671: $
2672: $        4
2673: $      / | \
2674: $     /  |  \
2675: $    /   |   \
2676: $   2  0 | 1  5
2677: $    \   |   /
2678: $     \  |  /
2679: $      \ | /
2680: $        3
2681: $
2682: $ Notice that all points are numbered consecutively, unlikely DMPlexCreateFromCellList()

2684:   Level: advanced

2686: .seealso: DMPlexCreateFromCellList(), DMPlexCreate()
2687: @*/
2688: PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[])
2689: {
2690:   Vec            coordinates;
2691:   PetscSection   coordSection;
2692:   PetscScalar    *coords;
2693:   PetscInt       coordSize, firstVertex = -1, pStart = 0, pEnd = 0, p, v, dim, dimEmbed, d, off;

2697:   DMGetDimension(dm, &dim);
2698:   DMGetCoordinateDim(dm, &dimEmbed);
2699:   if (dimEmbed < dim) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Embedding dimension %d cannot be less than intrinsic dimension %d",dimEmbed,dim);
2700:   for (d = 0; d <= depth; ++d) pEnd += numPoints[d];
2701:   DMPlexSetChart(dm, pStart, pEnd);
2702:   for (p = pStart; p < pEnd; ++p) {
2703:     DMPlexSetConeSize(dm, p, coneSize[p-pStart]);
2704:     if (firstVertex < 0 && !coneSize[p - pStart]) {
2705:       firstVertex = p - pStart;
2706:     }
2707:   }
2708:   if (firstVertex < 0 && numPoints[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected %d vertices but could not find any", numPoints[0]);
2709:   DMSetUp(dm); /* Allocate space for cones */
2710:   for (p = pStart, off = 0; p < pEnd; off += coneSize[p-pStart], ++p) {
2711:     DMPlexSetCone(dm, p, &cones[off]);
2712:     DMPlexSetConeOrientation(dm, p, &coneOrientations[off]);
2713:   }
2714:   DMPlexSymmetrize(dm);
2715:   DMPlexStratify(dm);
2716:   /* Build coordinates */
2717:   DMGetCoordinateSection(dm, &coordSection);
2718:   PetscSectionSetNumFields(coordSection, 1);
2719:   PetscSectionSetFieldComponents(coordSection, 0, dimEmbed);
2720:   PetscSectionSetChart(coordSection, firstVertex, firstVertex+numPoints[0]);
2721:   for (v = firstVertex; v < firstVertex+numPoints[0]; ++v) {
2722:     PetscSectionSetDof(coordSection, v, dimEmbed);
2723:     PetscSectionSetFieldDof(coordSection, v, 0, dimEmbed);
2724:   }
2725:   PetscSectionSetUp(coordSection);
2726:   PetscSectionGetStorageSize(coordSection, &coordSize);
2727:   VecCreate(PETSC_COMM_SELF, &coordinates);
2728:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
2729:   VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
2730:   VecSetBlockSize(coordinates, dimEmbed);
2731:   VecSetType(coordinates,VECSTANDARD);
2732:   VecGetArray(coordinates, &coords);
2733:   for (v = 0; v < numPoints[0]; ++v) {
2734:     PetscInt off;

2736:     PetscSectionGetOffset(coordSection, v+firstVertex, &off);
2737:     for (d = 0; d < dimEmbed; ++d) {
2738:       coords[off+d] = vertexCoords[v*dimEmbed+d];
2739:     }
2740:   }
2741:   VecRestoreArray(coordinates, &coords);
2742:   DMSetCoordinatesLocal(dm, coordinates);
2743:   VecDestroy(&coordinates);
2744:   return(0);
2745: }

2747: /*@C
2748:   DMPlexCreateFromFile - This takes a filename and produces a DM

2750:   Input Parameters:
2751: + comm - The communicator
2752: . filename - A file name
2753: - interpolate - Flag to create intermediate mesh pieces (edges, faces)

2755:   Output Parameter:
2756: . dm - The DM

2758:   Level: beginner

2760: .seealso: DMPlexCreateFromDAG(), DMPlexCreateFromCellList(), DMPlexCreate()
2761: @*/
2762: PetscErrorCode DMPlexCreateFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
2763: {
2764:   const char    *extGmsh    = ".msh";
2765:   const char    *extCGNS    = ".cgns";
2766:   const char    *extExodus  = ".exo";
2767:   const char    *extGenesis = ".gen";
2768:   const char    *extFluent  = ".cas";
2769:   const char    *extHDF5    = ".h5";
2770:   const char    *extMed     = ".med";
2771:   const char    *extPLY     = ".ply";
2772:   size_t         len;
2773:   PetscBool      isGmsh, isCGNS, isExodus, isGenesis, isFluent, isHDF5, isMed, isPLY;
2774:   PetscMPIInt    rank;

2780:   MPI_Comm_rank(comm, &rank);
2781:   PetscStrlen(filename, &len);
2782:   if (!len) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Filename must be a valid path");
2783:   PetscStrncmp(&filename[PetscMax(0,len-4)], extGmsh,    4, &isGmsh);
2784:   PetscStrncmp(&filename[PetscMax(0,len-5)], extCGNS,    5, &isCGNS);
2785:   PetscStrncmp(&filename[PetscMax(0,len-4)], extExodus,  4, &isExodus);
2786:   PetscStrncmp(&filename[PetscMax(0,len-4)], extGenesis, 4, &isGenesis);
2787:   PetscStrncmp(&filename[PetscMax(0,len-4)], extFluent,  4, &isFluent);
2788:   PetscStrncmp(&filename[PetscMax(0,len-3)], extHDF5,    3, &isHDF5);
2789:   PetscStrncmp(&filename[PetscMax(0,len-4)], extMed,     4, &isMed);
2790:   PetscStrncmp(&filename[PetscMax(0,len-4)], extPLY,     4, &isPLY);
2791:   if (isGmsh) {
2792:     DMPlexCreateGmshFromFile(comm, filename, interpolate, dm);
2793:   } else if (isCGNS) {
2794:     DMPlexCreateCGNSFromFile(comm, filename, interpolate, dm);
2795:   } else if (isExodus || isGenesis) {
2796:     DMPlexCreateExodusFromFile(comm, filename, interpolate, dm);
2797:   } else if (isFluent) {
2798:     DMPlexCreateFluentFromFile(comm, filename, interpolate, dm);
2799:   } else if (isHDF5) {
2800:     PetscViewer viewer;

2802:     PetscViewerCreate(comm, &viewer);
2803:     PetscViewerSetType(viewer, PETSCVIEWERHDF5);
2804:     PetscViewerFileSetMode(viewer, FILE_MODE_READ);
2805:     PetscViewerFileSetName(viewer, filename);
2806:     DMCreate(comm, dm);
2807:     DMSetType(*dm, DMPLEX);
2808:     DMLoad(*dm, viewer);
2809:     PetscViewerDestroy(&viewer);

2811:     if (interpolate) {
2812:       DM idm;

2814:       DMPlexInterpolate(*dm, &idm);
2815:       DMDestroy(dm);
2816:       *dm  = idm;
2817:     }
2818:   } else if (isMed) {
2819:     DMPlexCreateMedFromFile(comm, filename, interpolate, dm);
2820:   } else if (isPLY) {
2821:     DMPlexCreatePLYFromFile(comm, filename, interpolate, dm);
2822:   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename);
2823:   return(0);
2824: }

2826: /*@
2827:   DMPlexCreateReferenceCell - Create a DMPLEX with the appropriate FEM reference cell

2829:   Collective on comm

2831:   Input Parameters:
2832: + comm    - The communicator
2833: . dim     - The spatial dimension
2834: - simplex - Flag for simplex, otherwise use a tensor-product cell

2836:   Output Parameter:
2837: . refdm - The reference cell

2839:   Level: intermediate

2841: .keywords: reference cell
2842: .seealso:
2843: @*/
2844: PetscErrorCode DMPlexCreateReferenceCell(MPI_Comm comm, PetscInt dim, PetscBool simplex, DM *refdm)
2845: {
2846:   DM             rdm;
2847:   Vec            coords;

2851:   DMCreate(comm, &rdm);
2852:   DMSetType(rdm, DMPLEX);
2853:   DMSetDimension(rdm, dim);
2854:   switch (dim) {
2855:   case 0:
2856:   {
2857:     PetscInt    numPoints[1]        = {1};
2858:     PetscInt    coneSize[1]         = {0};
2859:     PetscInt    cones[1]            = {0};
2860:     PetscInt    coneOrientations[1] = {0};
2861:     PetscScalar vertexCoords[1]     = {0.0};

2863:     DMPlexCreateFromDAG(rdm, 0, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2864:   }
2865:   break;
2866:   case 1:
2867:   {
2868:     PetscInt    numPoints[2]        = {2, 1};
2869:     PetscInt    coneSize[3]         = {2, 0, 0};
2870:     PetscInt    cones[2]            = {1, 2};
2871:     PetscInt    coneOrientations[2] = {0, 0};
2872:     PetscScalar vertexCoords[2]     = {-1.0,  1.0};

2874:     DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2875:   }
2876:   break;
2877:   case 2:
2878:     if (simplex) {
2879:       PetscInt    numPoints[2]        = {3, 1};
2880:       PetscInt    coneSize[4]         = {3, 0, 0, 0};
2881:       PetscInt    cones[3]            = {1, 2, 3};
2882:       PetscInt    coneOrientations[3] = {0, 0, 0};
2883:       PetscScalar vertexCoords[6]     = {-1.0, -1.0,  1.0, -1.0,  -1.0, 1.0};

2885:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2886:     } else {
2887:       PetscInt    numPoints[2]        = {4, 1};
2888:       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
2889:       PetscInt    cones[4]            = {1, 2, 3, 4};
2890:       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
2891:       PetscScalar vertexCoords[8]     = {-1.0, -1.0,  1.0, -1.0,  1.0, 1.0,  -1.0, 1.0};

2893:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2894:     }
2895:   break;
2896:   case 3:
2897:     if (simplex) {
2898:       PetscInt    numPoints[2]        = {4, 1};
2899:       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
2900:       PetscInt    cones[4]            = {1, 3, 2, 4};
2901:       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
2902:       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};

2904:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2905:     } else {
2906:       PetscInt    numPoints[2]        = {8, 1};
2907:       PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
2908:       PetscInt    cones[8]            = {1, 4, 3, 2, 5, 6, 7, 8};
2909:       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
2910:       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,
2911:                                          -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  1.0, 1.0,  1.0,  -1.0, 1.0,  1.0};

2913:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2914:     }
2915:   break;
2916:   default:
2917:     SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Cannot create reference cell for dimension %d", dim);
2918:   }
2919:   DMPlexInterpolate(rdm, refdm);
2920:   if (rdm->coordinateDM) {
2921:     DM           ncdm;
2922:     PetscSection cs;
2923:     PetscInt     pEnd = -1;

2925:     DMGetDefaultSection(rdm->coordinateDM, &cs);
2926:     if (cs) {PetscSectionGetChart(cs, NULL, &pEnd);}
2927:     if (pEnd >= 0) {
2928:       DMClone(rdm->coordinateDM, &ncdm);
2929:       DMSetDefaultSection(ncdm, cs);
2930:       DMSetCoordinateDM(*refdm, ncdm);
2931:       DMDestroy(&ncdm);
2932:     }
2933:   }
2934:   DMGetCoordinatesLocal(rdm, &coords);
2935:   if (coords) {
2936:     DMSetCoordinatesLocal(*refdm, coords);
2937:   } else {
2938:     DMGetCoordinates(rdm, &coords);
2939:     if (coords) {DMSetCoordinates(*refdm, coords);}
2940:   }
2941:   DMDestroy(&rdm);
2942:   return(0);
2943: }