Actual source code: plexcreate.c

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

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

  9:   Collective on MPI_Comm

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

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

 22:   Level: beginner

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

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

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

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

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

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

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

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

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

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

154:   Collective on MPI_Comm

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

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

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

176:   Level: beginner

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

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

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

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

244:         cone[0] = vertex; cone[1] = vertex+1;
245:         DMPlexSetCone(dm, edge, cone);
246:         if (vy == edges[1]) {
247:           DMSetLabelValue(dm, "marker", edge,    markerTop);
248:           DMSetLabelValue(dm, "marker", cone[0], markerTop);
249:           if (ex == edges[0]-1) {
250:             DMSetLabelValue(dm, "marker", cone[1], markerTop);
251:           }
252:         } else if (vy == 0) {
253:           DMSetLabelValue(dm, "marker", edge,    markerBottom);
254:           DMSetLabelValue(dm, "marker", cone[0], markerBottom);
255:           if (ex == edges[0]-1) {
256:             DMSetLabelValue(dm, "marker", cone[1], markerBottom);
257:           }
258:         }
259:       }
260:     }
261:   }
262:   DMPlexSymmetrize(dm);
263:   DMPlexStratify(dm);
264:   /* Build coordinates */
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 the 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] + vx;
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:   DMGetCoordinateSection(dm, &coordSection);
409:   PetscSectionSetChart(coordSection, numFaces, numFaces + numVertices);
410:   for (v = numFaces; v < numFaces+numVertices; ++v) {
411:     PetscSectionSetDof(coordSection, v, 3);
412:   }
413:   PetscSectionSetUp(coordSection);
414:   PetscSectionGetStorageSize(coordSection, &coordSize);
415:   VecCreate(PETSC_COMM_SELF, &coordinates);
416:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
417:   VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
418:   VecSetBlockSize(coordinates, 3);
419:   VecSetType(coordinates,VECSTANDARD);
420:   VecGetArray(coordinates, &coords);
421:   for (vz = 0; vz <= faces[2]; ++vz) {
422:     for (vy = 0; vy <= faces[1]; ++vy) {
423:       for (vx = 0; vx <= faces[0]; ++vx) {
424:         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+0] = lower[0] + ((upper[0] - lower[0])/faces[0])*vx;
425:         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+1] = lower[1] + ((upper[1] - lower[1])/faces[1])*vy;
426:         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+2] = lower[2] + ((upper[2] - lower[2])/faces[2])*vz;
427:       }
428:     }
429:   }
430:   VecRestoreArray(coordinates, &coords);
431:   DMSetCoordinatesLocal(dm, coordinates);
432:   VecDestroy(&coordinates);
433:   return(0);
434: }

436: static PetscErrorCode DMPlexCreateCubeMesh_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY, DMBoundaryType bdZ)
437: {
438:   PetscInt       markerTop      = 1, faceMarkerTop      = 1;
439:   PetscInt       markerBottom   = 1, faceMarkerBottom   = 1;
440:   PetscInt       markerFront    = 1, faceMarkerFront    = 1;
441:   PetscInt       markerBack     = 1, faceMarkerBack     = 1;
442:   PetscInt       markerRight    = 1, faceMarkerRight    = 1;
443:   PetscInt       markerLeft     = 1, faceMarkerLeft     = 1;
444:   PetscInt       dim;
445:   PetscBool      markerSeparate = PETSC_FALSE;
446:   PetscMPIInt    rank;

450:   DMGetDimension(dm,&dim);
451:   MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
452:   DMCreateLabel(dm,"marker");
453:   DMCreateLabel(dm,"Face Sets");
454:   switch (dim) {
455:   case 2:
456:     faceMarkerTop    = 3;
457:     faceMarkerBottom = 1;
458:     faceMarkerRight  = 2;
459:     faceMarkerLeft   = 4;
460:     break;
461:   case 3:
462:     faceMarkerBottom = 1;
463:     faceMarkerTop    = 2;
464:     faceMarkerFront  = 3;
465:     faceMarkerBack   = 4;
466:     faceMarkerRight  = 5;
467:     faceMarkerLeft   = 6;
468:     break;
469:   default:
470:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Dimension %d not supported",dim);
471:     break;
472:   }
473:   PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);
474:   if (markerSeparate) {
475:     markerBottom = faceMarkerBottom;
476:     markerTop    = faceMarkerTop;
477:     markerFront  = faceMarkerFront;
478:     markerBack   = faceMarkerBack;
479:     markerRight  = faceMarkerRight;
480:     markerLeft   = faceMarkerLeft;
481:   }
482:   {
483:     const PetscInt numXEdges    = !rank ? edges[0] : 0;
484:     const PetscInt numYEdges    = !rank ? edges[1] : 0;
485:     const PetscInt numZEdges    = !rank ? edges[2] : 0;
486:     const PetscInt numXVertices = !rank ? (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ? edges[0] : edges[0]+1) : 0;
487:     const PetscInt numYVertices = !rank ? (bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ? edges[1] : edges[1]+1) : 0;
488:     const PetscInt numZVertices = !rank ? (bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST ? edges[2] : edges[2]+1) : 0;
489:     const PetscInt numCells     = numXEdges*numYEdges*numZEdges;
490:     const PetscInt numXFaces    = numYEdges*numZEdges;
491:     const PetscInt numYFaces    = numXEdges*numZEdges;
492:     const PetscInt numZFaces    = numXEdges*numYEdges;
493:     const PetscInt numTotXFaces = numXVertices*numXFaces;
494:     const PetscInt numTotYFaces = numYVertices*numYFaces;
495:     const PetscInt numTotZFaces = numZVertices*numZFaces;
496:     const PetscInt numFaces     = numTotXFaces + numTotYFaces + numTotZFaces;
497:     const PetscInt numTotXEdges = numXEdges*numYVertices*numZVertices;
498:     const PetscInt numTotYEdges = numYEdges*numXVertices*numZVertices;
499:     const PetscInt numTotZEdges = numZEdges*numXVertices*numYVertices;
500:     const PetscInt numVertices  = numXVertices*numYVertices*numZVertices;
501:     const PetscInt numEdges     = numTotXEdges + numTotYEdges + numTotZEdges;
502:     const PetscInt firstVertex  = (dim == 2) ? numFaces : numCells;
503:     const PetscInt firstXFace   = (dim == 2) ? 0 : numCells + numVertices;
504:     const PetscInt firstYFace   = firstXFace + numTotXFaces;
505:     const PetscInt firstZFace   = firstYFace + numTotYFaces;
506:     const PetscInt firstXEdge   = numCells + numFaces + numVertices;
507:     const PetscInt firstYEdge   = firstXEdge + numTotXEdges;
508:     const PetscInt firstZEdge   = firstYEdge + numTotYEdges;
509:     Vec            coordinates;
510:     PetscSection   coordSection;
511:     PetscScalar   *coords;
512:     PetscInt       coordSize;
513:     PetscInt       v, vx, vy, vz;
514:     PetscInt       c, f, fx, fy, fz, e, ex, ey, ez;

516:     DMPlexSetChart(dm, 0, numCells+numFaces+numEdges+numVertices);
517:     for (c = 0; c < numCells; c++) {
518:       DMPlexSetConeSize(dm, c, 6);
519:     }
520:     for (f = firstXFace; f < firstXFace+numFaces; ++f) {
521:       DMPlexSetConeSize(dm, f, 4);
522:     }
523:     for (e = firstXEdge; e < firstXEdge+numEdges; ++e) {
524:       DMPlexSetConeSize(dm, e, 2);
525:     }
526:     DMSetUp(dm); /* Allocate space for cones */
527:     /* Build cells */
528:     for (fz = 0; fz < numZEdges; ++fz) {
529:       for (fy = 0; fy < numYEdges; ++fy) {
530:         for (fx = 0; fx < numXEdges; ++fx) {
531:           PetscInt cell    = (fz*numYEdges + fy)*numXEdges + fx;
532:           PetscInt faceB   = firstZFace + (fy*numXEdges+fx)*numZVertices +   fz;
533:           PetscInt faceT   = firstZFace + (fy*numXEdges+fx)*numZVertices + ((fz+1)%numZVertices);
534:           PetscInt faceF   = firstYFace + (fz*numXEdges+fx)*numYVertices +   fy;
535:           PetscInt faceK   = firstYFace + (fz*numXEdges+fx)*numYVertices + ((fy+1)%numYVertices);
536:           PetscInt faceL   = firstXFace + (fz*numYEdges+fy)*numXVertices +   fx;
537:           PetscInt faceR   = firstXFace + (fz*numYEdges+fy)*numXVertices + ((fx+1)%numXVertices);
538:                             /* B,  T,  F,  K,  R,  L */
539:           PetscInt ornt[6] = {-4,  0,  0, -1,  0, -4}; /* ??? */
540:           PetscInt cone[6];

542:           /* no boundary twisting in 3D */
543:           cone[0] = faceB; cone[1] = faceT; cone[2] = faceF; cone[3] = faceK; cone[4] = faceR; cone[5] = faceL;
544:           DMPlexSetCone(dm, cell, cone);
545:           DMPlexSetConeOrientation(dm, cell, ornt);
546:         }
547:       }
548:     }
549:     /* Build x faces */
550:     for (fz = 0; fz < numZEdges; ++fz) {
551:       for (fy = 0; fy < numYEdges; ++fy) {
552:         for (fx = 0; fx < numXVertices; ++fx) {
553:           PetscInt face    = firstXFace + (fz*numYEdges+fy)*numXVertices + fx;
554:           PetscInt edgeL   = firstZEdge + (  fy*                 numXVertices+fx)*numZEdges + fz;
555:           PetscInt edgeR   = firstZEdge + (((fy+1)%numYVertices)*numXVertices+fx)*numZEdges + fz;
556:           PetscInt edgeB   = firstYEdge + (  fz*                 numXVertices+fx)*numYEdges + fy;
557:           PetscInt edgeT   = firstYEdge + (((fz+1)%numZVertices)*numXVertices+fx)*numYEdges + fy;
558:           PetscInt ornt[4] = {0, 0, -2, -2};
559:           PetscInt cone[4];

561:           if (dim == 3) {
562:             /* markers */
563:             if (bdX != DM_BOUNDARY_PERIODIC) {
564:               if (fx == numXVertices-1) {
565:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerRight);
566:                 DMSetLabelValue(dm, "marker", face, markerRight);
567:               }
568:               else if (fx == 0) {
569:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerLeft);
570:                 DMSetLabelValue(dm, "marker", face, markerLeft);
571:               }
572:             }
573:           }
574:           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
575:           DMPlexSetCone(dm, face, cone);
576:           DMPlexSetConeOrientation(dm, face, ornt);
577:         }
578:       }
579:     }
580:     /* Build y faces */
581:     for (fz = 0; fz < numZEdges; ++fz) {
582:       for (fx = 0; fx < numXEdges; ++fx) {
583:         for (fy = 0; fy < numYVertices; ++fy) {
584:           PetscInt face    = firstYFace + (fz*numXEdges+fx)*numYVertices + fy;
585:           PetscInt edgeL   = firstZEdge + (fy*numXVertices+  fx                 )*numZEdges + fz;
586:           PetscInt edgeR   = firstZEdge + (fy*numXVertices+((fx+1)%numXVertices))*numZEdges + fz;
587:           PetscInt edgeB   = firstXEdge + (  fz                 *numYVertices+fy)*numXEdges + fx;
588:           PetscInt edgeT   = firstXEdge + (((fz+1)%numZVertices)*numYVertices+fy)*numXEdges + fx;
589:           PetscInt ornt[4] = {0, 0, -2, -2};
590:           PetscInt cone[4];

592:           if (dim == 3) {
593:             /* markers */
594:             if (bdY != DM_BOUNDARY_PERIODIC) {
595:               if (fy == numYVertices-1) {
596:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerBack);
597:                 DMSetLabelValue(dm, "marker", face, markerBack);
598:               }
599:               else if (fy == 0) {
600:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerFront);
601:                 DMSetLabelValue(dm, "marker", face, markerFront);
602:               }
603:             }
604:           }
605:           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
606:           DMPlexSetCone(dm, face, cone);
607:           DMPlexSetConeOrientation(dm, face, ornt);
608:         }
609:       }
610:     }
611:     /* Build z faces */
612:     for (fy = 0; fy < numYEdges; ++fy) {
613:       for (fx = 0; fx < numXEdges; ++fx) {
614:         for (fz = 0; fz < numZVertices; fz++) {
615:           PetscInt face    = firstZFace + (fy*numXEdges+fx)*numZVertices + fz;
616:           PetscInt edgeL   = firstYEdge + (fz*numXVertices+  fx                 )*numYEdges + fy;
617:           PetscInt edgeR   = firstYEdge + (fz*numXVertices+((fx+1)%numXVertices))*numYEdges + fy;
618:           PetscInt edgeB   = firstXEdge + (fz*numYVertices+  fy                 )*numXEdges + fx;
619:           PetscInt edgeT   = firstXEdge + (fz*numYVertices+((fy+1)%numYVertices))*numXEdges + fx;
620:           PetscInt ornt[4] = {0, 0, -2, -2};
621:           PetscInt cone[4];

623:           if (dim == 2) {
624:             if (bdX == DM_BOUNDARY_TWIST && fx == numXEdges-1) {edgeR += numYEdges-1-2*fy; ornt[1] = -2;}
625:             if (bdY == DM_BOUNDARY_TWIST && fy == numYEdges-1) {edgeT += numXEdges-1-2*fx; ornt[2] =  0;}
626:           }
627:           else {
628:             /* markers */
629:             if (bdZ != DM_BOUNDARY_PERIODIC) {
630:               if (fz == numZVertices-1) {
631:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerTop);
632:                 DMSetLabelValue(dm, "marker", face, markerTop);
633:               }
634:               else if (fz == 0) {
635:                 DMSetLabelValue(dm, "Face Sets", face, faceMarkerBottom);
636:                 DMSetLabelValue(dm, "marker", face, markerBottom);
637:               }
638:             }
639:           }
640:           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
641:           DMPlexSetCone(dm, face, cone);
642:           DMPlexSetConeOrientation(dm, face, ornt);
643:         }
644:       }
645:     }
646:     /* Build Z edges*/
647:     for (vy = 0; vy < numYVertices; vy++) {
648:       for (vx = 0; vx < numXVertices; vx++) {
649:         for (ez = 0; ez < numZEdges; ez++) {
650:           const PetscInt edge    = firstZEdge  + (vy*numXVertices+vx)*numZEdges + ez;
651:           const PetscInt vertexB = firstVertex + (  ez                 *numYVertices+vy)*numXVertices + vx;
652:           const PetscInt vertexT = firstVertex + (((ez+1)%numZVertices)*numYVertices+vy)*numXVertices + vx;
653:           PetscInt       cone[2];

655:           if (dim == 3) {
656:             if (bdX != DM_BOUNDARY_PERIODIC) {
657:               if (vx == numXVertices-1) {
658:                 DMSetLabelValue(dm, "marker", edge, markerRight);
659:               }
660:               else if (vx == 0) {
661:                 DMSetLabelValue(dm, "marker", edge, markerLeft);
662:               }
663:             }
664:             if (bdY != DM_BOUNDARY_PERIODIC) {
665:               if (vy == numYVertices-1) {
666:                 DMSetLabelValue(dm, "marker", edge, markerBack);
667:               }
668:               else if (vy == 0) {
669:                 DMSetLabelValue(dm, "marker", edge, markerFront);
670:               }
671:             }
672:           }
673:           cone[0] = vertexB; cone[1] = vertexT;
674:           DMPlexSetCone(dm, edge, cone);
675:         }
676:       }
677:     }
678:     /* Build Y edges*/
679:     for (vz = 0; vz < numZVertices; vz++) {
680:       for (vx = 0; vx < numXVertices; vx++) {
681:         for (ey = 0; ey < numYEdges; ey++) {
682:           const PetscInt nextv   = (dim == 2 && bdY == DM_BOUNDARY_TWIST && ey == numYEdges-1) ? (numXVertices-vx-1) : (vz*numYVertices+((ey+1)%numYVertices))*numXVertices + vx;
683:           const PetscInt edge    = firstYEdge  + (vz*numXVertices+vx)*numYEdges + ey;
684:           const PetscInt vertexF = firstVertex + (vz*numYVertices+ey)*numXVertices + vx;
685:           const PetscInt vertexK = firstVertex + nextv;
686:           PetscInt       cone[2];

688:           cone[0] = vertexF; cone[1] = vertexK;
689:           DMPlexSetCone(dm, edge, cone);
690:           if (dim == 2) {
691:             if ((bdX != DM_BOUNDARY_PERIODIC) && (bdX != DM_BOUNDARY_TWIST)) {
692:               if (vx == numXVertices-1) {
693:                 DMSetLabelValue(dm, "Face Sets", edge, faceMarkerRight);
694:                 DMSetLabelValue(dm, "marker", edge,    markerRight);
695:                 DMSetLabelValue(dm, "marker", cone[0], markerRight);
696:                 if (ey == numYEdges-1) {
697:                   DMSetLabelValue(dm, "marker", cone[1], markerRight);
698:                 }
699:               }
700:               else if (vx == 0) {
701:                 DMSetLabelValue(dm, "Face Sets", edge, faceMarkerLeft);
702:                 DMSetLabelValue(dm, "marker", edge,    markerLeft);
703:                 DMSetLabelValue(dm, "marker", cone[0], markerLeft);
704:                 if (ey == numYEdges-1) {
705:                   DMSetLabelValue(dm, "marker", cone[1], markerLeft);
706:                 }
707:               }
708:             }
709:           }
710:           else {
711:             if (bdX != DM_BOUNDARY_PERIODIC) {
712:               if (vx == numXVertices-1) {
713:                 DMSetLabelValue(dm, "marker", edge, markerRight);
714:               }
715:               else if (vx == 0) {
716:                 DMSetLabelValue(dm, "marker", edge, markerLeft);
717:               }
718:             }
719:             if (bdZ != DM_BOUNDARY_PERIODIC) {
720:               if (vz == numZVertices-1) {
721:                 DMSetLabelValue(dm, "marker", edge, markerTop);
722:               }
723:               else if (vz == 0) {
724:                 DMSetLabelValue(dm, "marker", edge, markerBottom);
725:               }
726:             }
727:           }
728:         }
729:       }
730:     }
731:     /* Build X edges*/
732:     for (vz = 0; vz < numZVertices; vz++) {
733:       for (vy = 0; vy < numYVertices; vy++) {
734:         for (ex = 0; ex < numXEdges; ex++) {
735:           const PetscInt nextv   = (dim == 2 && bdX == DM_BOUNDARY_TWIST && ex == numXEdges-1) ? (numYVertices-vy-1)*numXVertices : (vz*numYVertices+vy)*numXVertices + (ex+1)%numXVertices;
736:           const PetscInt edge    = firstXEdge  + (vz*numYVertices+vy)*numXEdges + ex;
737:           const PetscInt vertexL = firstVertex + (vz*numYVertices+vy)*numXVertices + ex;
738:           const PetscInt vertexR = firstVertex + nextv;
739:           PetscInt       cone[2];

741:           cone[0] = vertexL; cone[1] = vertexR;
742:           DMPlexSetCone(dm, edge, cone);
743:           if (dim == 2) {
744:             if ((bdY != DM_BOUNDARY_PERIODIC) && (bdY != DM_BOUNDARY_TWIST)) {
745:               if (vy == numYVertices-1) {
746:                 DMSetLabelValue(dm, "Face Sets", edge, faceMarkerTop);
747:                 DMSetLabelValue(dm, "marker", edge,    markerTop);
748:                 DMSetLabelValue(dm, "marker", cone[0], markerTop);
749:                 if (ex == numXEdges-1) {
750:                   DMSetLabelValue(dm, "marker", cone[1], markerTop);
751:                 }
752:               }
753:               else if (vy == 0) {
754:                 DMSetLabelValue(dm, "Face Sets", edge, faceMarkerBottom);
755:                 DMSetLabelValue(dm, "marker", edge,    markerBottom);
756:                 DMSetLabelValue(dm, "marker", cone[0], markerBottom);
757:                 if (ex == numXEdges-1) {
758:                   DMSetLabelValue(dm, "marker", cone[1], markerBottom);
759:                 }
760:               }
761:             }
762:           }
763:           else {
764:             if (bdY != DM_BOUNDARY_PERIODIC) {
765:               if (vy == numYVertices-1) {
766:                 DMSetLabelValue(dm, "marker", edge, markerBack);
767:               }
768:               else if (vy == 0) {
769:                 DMSetLabelValue(dm, "marker", edge, markerFront);
770:               }
771:             }
772:             if (bdZ != DM_BOUNDARY_PERIODIC) {
773:               if (vz == numZVertices-1) {
774:                 DMSetLabelValue(dm, "marker", edge, markerTop);
775:               }
776:               else if (vz == 0) {
777:                 DMSetLabelValue(dm, "marker", edge, markerBottom);
778:               }
779:             }
780:           }
781:         }
782:       }
783:     }
784:     DMPlexSymmetrize(dm);
785:     DMPlexStratify(dm);
786:     /* Build coordinates */
787:     DMGetCoordinateSection(dm, &coordSection);
788:     PetscSectionSetNumFields(coordSection, 1);
789:     PetscSectionSetFieldComponents(coordSection, 0, dim);
790:     PetscSectionSetChart(coordSection, firstVertex, firstVertex+numVertices);
791:     for (v = firstVertex; v < firstVertex+numVertices; ++v) {
792:       PetscSectionSetDof(coordSection, v, dim);
793:       PetscSectionSetFieldDof(coordSection, v, 0, dim);
794:     }
795:     PetscSectionSetUp(coordSection);
796:     PetscSectionGetStorageSize(coordSection, &coordSize);
797:     VecCreate(PETSC_COMM_SELF, &coordinates);
798:     PetscObjectSetName((PetscObject) coordinates, "coordinates");
799:     VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
800:     VecSetBlockSize(coordinates, dim);
801:     VecSetType(coordinates,VECSTANDARD);
802:     VecGetArray(coordinates, &coords);
803:     for (vz = 0; vz < numZVertices; ++vz) {
804:       for (vy = 0; vy < numYVertices; ++vy) {
805:         for (vx = 0; vx < numXVertices; ++vx) {
806:           coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+0] = lower[0] + ((upper[0] - lower[0])/numXEdges)*vx;
807:           coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+1] = lower[1] + ((upper[1] - lower[1])/numYEdges)*vy;
808:           if (dim == 3) {
809:             coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+2] = lower[2] + ((upper[2] - lower[2])/numZEdges)*vz;
810:           }
811:         }
812:       }
813:     }
814:     VecRestoreArray(coordinates, &coords);
815:     DMSetCoordinatesLocal(dm, coordinates);
816:     VecDestroy(&coordinates);
817:   }
818:   return(0);
819: }

821: /*@
822:   DMPlexCreateSquareMesh - Creates a 2D mesh for a square lattice.

824:   Collective on MPI_Comm

826:   Input Parameters:
827: + comm  - The communicator for the DM object
828: . lower - The lower left corner coordinates
829: . upper - The upper right corner coordinates
830: . edges - The number of cells in each direction
831: . bdX   - The boundary type for the X direction
832: - bdY   - The boundary type for the Y direction

834:   Output Parameter:
835: . dm  - The DM object

837:   Note: Here is the numbering returned for 2 cells in each direction:
838: $ 22--8-23--9--24
839: $  |     |     |
840: $ 13  2 14  3  15
841: $  |     |     |
842: $ 19--6-20--7--21
843: $  |     |     |
844: $ 10  0 11  1 12
845: $  |     |     |
846: $ 16--4-17--5--18

848:   Level: beginner

850: .keywords: DM, create
851: .seealso: DMPlexCreateBoxMesh(), DMPlexCreateSquareBoundary(), DMPlexCreateCubeBoundary(), DMSetType(), DMCreate()
852: @*/
853: PetscErrorCode DMPlexCreateSquareMesh(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY)
854: {
855:   PetscReal      lower3[3], upper3[3];
856:   PetscInt       edges3[3];

860:   lower3[0] = lower[0]; lower3[1] = lower[1]; lower3[2] = 0.;
861:   upper3[0] = upper[0]; upper3[1] = upper[1]; upper3[2] = 0.;
862:   edges3[0] = edges[0]; edges3[1] = edges[1]; edges3[2] = 0;
863:   DMPlexCreateCubeMesh_Internal(dm, lower3, upper3, edges3, bdX, bdY, DM_BOUNDARY_NONE);
864:   return(0);
865: }

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

870:   Collective on MPI_Comm

872:   Input Parameters:
873: + comm - The communicator for the DM object
874: . dim - The spatial dimension
875: . numFaces - Number of faces per dimension
876: - interpolate - Flag to create intermediate mesh pieces (edges, faces)

878:   Output Parameter:
879: . dm  - The DM object

881:   Level: beginner

883: .keywords: DM, create
884: .seealso: DMPlexCreateHexBoxMesh(), DMSetType(), DMCreate()
885: @*/
886: PetscErrorCode DMPlexCreateBoxMesh(MPI_Comm comm, PetscInt dim, PetscInt numFaces, PetscBool interpolate, DM *dm)
887: {
888:   DM             boundary;

893:   DMCreate(comm, &boundary);
895:   DMSetType(boundary, DMPLEX);
896:   DMSetDimension(boundary, dim-1);
897:   DMSetCoordinateDim(boundary, dim);
898:   switch (dim) {
899:   case 2:
900:   {
901:     PetscReal lower[2] = {0.0, 0.0};
902:     PetscReal upper[2] = {1.0, 1.0};
903:     PetscInt  edges[2];

905:     edges[0] = numFaces; edges[1] = numFaces;
906:     DMPlexCreateSquareBoundary(boundary, lower, upper, edges);
907:     break;
908:   }
909:   case 3:
910:   {
911:     PetscReal lower[3] = {0.0, 0.0, 0.0};
912:     PetscReal upper[3] = {1.0, 1.0, 1.0};
913:     PetscInt  faces[3];

915:     faces[0] = numFaces; faces[1] = numFaces; faces[2] = numFaces;
916:     DMPlexCreateCubeBoundary(boundary, lower, upper, faces);
917:     break;
918:   }
919:   default:
920:     SETERRQ1(comm, PETSC_ERR_SUP, "Dimension not supported: %d", dim);
921:   }
922:   DMPlexGenerate(boundary, NULL, interpolate, dm);
923:   DMDestroy(&boundary);
924:   return(0);
925: }

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

930:   Collective on MPI_Comm

932:   Input Parameters:
933: + comm  - The communicator for the DM object
934: . dim   - The spatial dimension
935: . periodicX - The boundary type for the X direction
936: . periodicY - The boundary type for the Y direction
937: . periodicZ - The boundary type for the Z direction
938: - cells - The number of cells in each direction

940:   Output Parameter:
941: . dm  - The DM object

943:   Level: beginner

945: .keywords: DM, create
946: .seealso: DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
947: @*/
948: PetscErrorCode DMPlexCreateHexBoxMesh(MPI_Comm comm, PetscInt dim, const PetscInt cells[], DMBoundaryType periodicX, DMBoundaryType periodicY, DMBoundaryType periodicZ, DM *dm)
949: {
950:   PetscInt       i;

955:   DMCreate(comm, dm);
957:   DMSetType(*dm, DMPLEX);
958:   DMSetDimension(*dm, dim);
959:   switch (dim) {
960:   case 2:
961:   {
962:     PetscReal lower[2] = {0.0, 0.0};
963:     PetscReal upper[2] = {1.0, 1.0};

965:     DMPlexCreateSquareMesh(*dm, lower, upper, cells, periodicX, periodicY);
966:     if (periodicX == DM_BOUNDARY_PERIODIC || periodicX == DM_BOUNDARY_TWIST ||
967:         periodicY == DM_BOUNDARY_PERIODIC || periodicY == DM_BOUNDARY_TWIST) {
968:       PetscReal      L[2];
969:       PetscReal      maxCell[2];
970:       DMBoundaryType bdType[2];

972:       bdType[0] = periodicX;
973:       bdType[1] = periodicY;
974:       for (i = 0; i < dim; i++) {
975:         L[i]       = upper[i] - lower[i];
976:         maxCell[i] = 1.1 * (L[i] / cells[i]);
977:       }

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

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

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

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

1014: /* External function declarations here */
1015: extern PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling);
1016: extern PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat);
1017: extern PetscErrorCode DMCreateDefaultSection_Plex(DM dm);
1018: extern PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm);
1019: extern PetscErrorCode DMCreateMatrix_Plex(DM dm,  Mat *J);
1020: extern PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm);
1021: extern PetscErrorCode DMRefine_Plex(DM dm, MPI_Comm comm, DM *dmRefined);
1022: extern PetscErrorCode DMCoarsen_Plex(DM dm, MPI_Comm comm, DM *dmCoarsened);
1023: extern PetscErrorCode DMAdaptLabel_Plex(DM dm, DMLabel adaptLabel, DM *dmRefined);
1024: extern PetscErrorCode DMRefineHierarchy_Plex(DM dm, PetscInt nlevels, DM dmRefined[]);
1025: extern PetscErrorCode DMCoarsenHierarchy_Plex(DM dm, PetscInt nlevels, DM dmCoarsened[]);
1026: extern PetscErrorCode DMClone_Plex(DM dm, DM *newdm);
1027: extern PetscErrorCode DMSetUp_Plex(DM dm);
1028: extern PetscErrorCode DMDestroy_Plex(DM dm);
1029: extern PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer);
1030: extern PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer);
1031: extern PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm);
1032: extern PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, DMPointLocationType ltype, PetscSF cellSF);
1033: extern PetscErrorCode DMProjectFunctionLocal_Plex(DM,PetscReal,PetscErrorCode(**)(PetscInt,PetscReal,const PetscReal[],PetscInt,PetscScalar *,void *),void **,InsertMode,Vec);
1034: extern PetscErrorCode DMProjectFunctionLabelLocal_Plex(DM,PetscReal,DMLabel,PetscInt,const PetscInt[],PetscErrorCode(**)(PetscInt,PetscReal,const PetscReal[],PetscInt,PetscScalar *,void *),void **,InsertMode,Vec);
1035: extern PetscErrorCode DMProjectFieldLocal_Plex(DM,PetscReal,Vec,void (**)(PetscInt,PetscInt,PetscInt,const PetscInt[],const PetscInt[],const PetscScalar[],const PetscScalar[],const PetscScalar[],const PetscInt[],const PetscInt[],const PetscScalar[],const PetscScalar[],const PetscScalar[],PetscReal,const PetscReal[],PetscScalar[]),InsertMode,Vec);
1036: extern PetscErrorCode DMProjectFieldLabelLocal_Plex(DM,PetscReal,DMLabel,PetscInt,const PetscInt[],Vec,void (**)(PetscInt,PetscInt,PetscInt,const PetscInt[],const PetscInt[],const PetscScalar[],const PetscScalar[],const PetscScalar[],const PetscInt[],const PetscInt[],const PetscScalar[],const PetscScalar[],const PetscScalar[],PetscReal,const PetscReal[],PetscScalar[]),InsertMode,Vec);
1037: extern PetscErrorCode DMComputeL2Diff_Plex(DM,PetscReal,PetscErrorCode(**)(PetscInt,PetscReal,const PetscReal[],PetscInt,PetscScalar *,void *),void **,Vec,PetscReal *);
1038: extern PetscErrorCode DMComputeL2GradientDiff_Plex(DM,PetscReal,PetscErrorCode(**)(PetscInt,PetscReal,const PetscReal[], const PetscReal[],PetscInt,PetscScalar *,void *),void **,Vec,const PetscReal [],PetscReal *);
1039: extern PetscErrorCode DMComputeL2FieldDiff_Plex(DM,PetscReal,PetscErrorCode(**)(PetscInt,PetscReal,const PetscReal[],PetscInt,PetscScalar *,void *),void **,Vec,PetscReal *);

1041: /* Replace dm with the contents of dmNew
1042:    - Share the DM_Plex structure
1043:    - Share the coordinates
1044:    - Share the SF
1045: */
1046: static PetscErrorCode DMPlexReplace_Static(DM dm, DM dmNew)
1047: {
1048:   PetscSF          sf;
1049:   DM               coordDM, coarseDM;
1050:   Vec              coords;
1051:   const PetscReal *maxCell, *L;
1052:   const DMBoundaryType *bd;
1053:   PetscErrorCode   ierr;

1056:   DMGetPointSF(dmNew, &sf);
1057:   DMSetPointSF(dm, sf);
1058:   DMGetCoordinateDM(dmNew, &coordDM);
1059:   DMGetCoordinatesLocal(dmNew, &coords);
1060:   DMSetCoordinateDM(dm, coordDM);
1061:   DMSetCoordinatesLocal(dm, coords);
1062:   DMGetPeriodicity(dm, &maxCell, &L, &bd);
1063:   if (L) {DMSetPeriodicity(dmNew, maxCell, L, bd);}
1064:   DMDestroy_Plex(dm);
1065:   dm->data = dmNew->data;
1066:   ((DM_Plex *) dmNew->data)->refct++;
1067:   dmNew->labels->refct++;
1068:   if (!--(dm->labels->refct)) {
1069:     DMLabelLink next = dm->labels->next;

1071:     /* destroy the labels */
1072:     while (next) {
1073:       DMLabelLink tmp = next->next;

1075:       DMLabelDestroy(&next->label);
1076:       PetscFree(next);
1077:       next = tmp;
1078:     }
1079:     PetscFree(dm->labels);
1080:   }
1081:   dm->labels = dmNew->labels;
1082:   dm->depthLabel = dmNew->depthLabel;
1083:   DMGetCoarseDM(dmNew,&coarseDM);
1084:   DMSetCoarseDM(dm,coarseDM);
1085:   return(0);
1086: }

1088: /* Swap dm with the contents of dmNew
1089:    - Swap the DM_Plex structure
1090:    - Swap the coordinates
1091:    - Swap the point PetscSF
1092: */
1093: static PetscErrorCode DMPlexSwap_Static(DM dmA, DM dmB)
1094: {
1095:   DM              coordDMA, coordDMB;
1096:   Vec             coordsA,  coordsB;
1097:   PetscSF         sfA,      sfB;
1098:   void            *tmp;
1099:   DMLabelLinkList listTmp;
1100:   DMLabel         depthTmp;
1101:   PetscErrorCode  ierr;

1104:   DMGetPointSF(dmA, &sfA);
1105:   DMGetPointSF(dmB, &sfB);
1106:   PetscObjectReference((PetscObject) sfA);
1107:   DMSetPointSF(dmA, sfB);
1108:   DMSetPointSF(dmB, sfA);
1109:   PetscObjectDereference((PetscObject) sfA);

1111:   DMGetCoordinateDM(dmA, &coordDMA);
1112:   DMGetCoordinateDM(dmB, &coordDMB);
1113:   PetscObjectReference((PetscObject) coordDMA);
1114:   DMSetCoordinateDM(dmA, coordDMB);
1115:   DMSetCoordinateDM(dmB, coordDMA);
1116:   PetscObjectDereference((PetscObject) coordDMA);

1118:   DMGetCoordinatesLocal(dmA, &coordsA);
1119:   DMGetCoordinatesLocal(dmB, &coordsB);
1120:   PetscObjectReference((PetscObject) coordsA);
1121:   DMSetCoordinatesLocal(dmA, coordsB);
1122:   DMSetCoordinatesLocal(dmB, coordsA);
1123:   PetscObjectDereference((PetscObject) coordsA);

1125:   tmp       = dmA->data;
1126:   dmA->data = dmB->data;
1127:   dmB->data = tmp;
1128:   listTmp   = dmA->labels;
1129:   dmA->labels = dmB->labels;
1130:   dmB->labels = listTmp;
1131:   depthTmp  = dmA->depthLabel;
1132:   dmA->depthLabel = dmB->depthLabel;
1133:   dmB->depthLabel = depthTmp;
1134:   return(0);
1135: }

1137: PetscErrorCode  DMSetFromOptions_NonRefinement_Plex(PetscOptionItems *PetscOptionsObject,DM dm)
1138: {
1139:   DM_Plex       *mesh = (DM_Plex*) dm->data;

1143:   /* Handle viewing */
1144:   PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMView", PETSC_FALSE, &mesh->printSetValues, NULL);
1145:   PetscOptionsInt("-dm_plex_print_fem", "Debug output level all fem computations", "DMView", 0, &mesh->printFEM, NULL);
1146:   PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMView", mesh->printTol, &mesh->printTol, NULL);
1147:   /* Point Location */
1148:   PetscOptionsBool("-dm_plex_hash_location", "Use grid hashing for point location", "DMView", PETSC_FALSE, &mesh->useHashLocation, NULL);
1149:   /* Generation and remeshing */
1150:   PetscOptionsBool("-dm_plex_remesh_bd", "Allow changes to the boundary on remeshing", "DMView", PETSC_FALSE, &mesh->remeshBd, NULL);
1151:   /* Projection behavior */
1152:   PetscOptionsInt("-dm_plex_max_projection_height", "Maxmimum mesh point height used to project locally", "DMPlexSetMaxProjectionHeight", 0, &mesh->maxProjectionHeight, NULL);
1153:   PetscOptionsBool("-dm_plex_regular_refinement", "Use special nested projection algorithm for regular refinement", "DMPlexSetRegularRefinement", mesh->regularRefinement, &mesh->regularRefinement, NULL);
1154:   return(0);
1155: }

1157: PetscErrorCode  DMSetFromOptions_Plex(PetscOptionItems *PetscOptionsObject,DM dm)
1158: {
1159:   PetscInt       refine = 0, coarsen = 0, r;
1160:   PetscBool      isHierarchy;

1165:   PetscOptionsHead(PetscOptionsObject,"DMPlex Options");
1166:   /* Handle DMPlex refinement */
1167:   PetscOptionsInt("-dm_refine", "The number of uniform refinements", "DMCreate", refine, &refine, NULL);
1168:   PetscOptionsInt("-dm_refine_hierarchy", "The number of uniform refinements", "DMCreate", refine, &refine, &isHierarchy);
1169:   if (refine) {DMPlexSetRefinementUniform(dm, PETSC_TRUE);}
1170:   if (refine && isHierarchy) {
1171:     DM *dms, coarseDM;

1173:     DMGetCoarseDM(dm, &coarseDM);
1174:     PetscObjectReference((PetscObject)coarseDM);
1175:     PetscMalloc1(refine,&dms);
1176:     DMRefineHierarchy(dm, refine, dms);
1177:     /* Total hack since we do not pass in a pointer */
1178:     DMPlexSwap_Static(dm, dms[refine-1]);
1179:     if (refine == 1) {
1180:       DMSetCoarseDM(dm, dms[0]);
1181:       DMPlexSetRegularRefinement(dm, PETSC_TRUE);
1182:     } else {
1183:       DMSetCoarseDM(dm, dms[refine-2]);
1184:       DMPlexSetRegularRefinement(dm, PETSC_TRUE);
1185:       DMSetCoarseDM(dms[0], dms[refine-1]);
1186:       DMPlexSetRegularRefinement(dms[0], PETSC_TRUE);
1187:     }
1188:     DMSetCoarseDM(dms[refine-1], coarseDM);
1189:     PetscObjectDereference((PetscObject)coarseDM);
1190:     /* Free DMs */
1191:     for (r = 0; r < refine; ++r) {
1192:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]);
1193:       DMDestroy(&dms[r]);
1194:     }
1195:     PetscFree(dms);
1196:   } else {
1197:     for (r = 0; r < refine; ++r) {
1198:       DM refinedMesh;

1200:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
1201:       DMRefine(dm, PetscObjectComm((PetscObject) dm), &refinedMesh);
1202:       /* Total hack since we do not pass in a pointer */
1203:       DMPlexReplace_Static(dm, refinedMesh);
1204:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
1205:       DMDestroy(&refinedMesh);
1206:     }
1207:   }
1208:   /* Handle DMPlex coarsening */
1209:   PetscOptionsInt("-dm_coarsen", "Coarsen the mesh", "DMCreate", coarsen, &coarsen, NULL);
1210:   PetscOptionsInt("-dm_coarsen_hierarchy", "The number of coarsenings", "DMCreate", coarsen, &coarsen, &isHierarchy);
1211:   if (coarsen && isHierarchy) {
1212:     DM *dms;

1214:     PetscMalloc1(coarsen, &dms);
1215:     DMCoarsenHierarchy(dm, coarsen, dms);
1216:     /* Free DMs */
1217:     for (r = 0; r < coarsen; ++r) {
1218:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]);
1219:       DMDestroy(&dms[r]);
1220:     }
1221:     PetscFree(dms);
1222:   } else {
1223:     for (r = 0; r < coarsen; ++r) {
1224:       DM coarseMesh;

1226:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
1227:       DMCoarsen(dm, PetscObjectComm((PetscObject) dm), &coarseMesh);
1228:       /* Total hack since we do not pass in a pointer */
1229:       DMPlexReplace_Static(dm, coarseMesh);
1230:       DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
1231:       DMDestroy(&coarseMesh);
1232:     }
1233:   }
1234:   /* Handle */
1235:   DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);
1236:   PetscOptionsTail();
1237:   return(0);
1238: }

1240: static PetscErrorCode DMCreateGlobalVector_Plex(DM dm,Vec *vec)
1241: {

1245:   DMCreateGlobalVector_Section_Private(dm,vec);
1246:   /* VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM); */
1247:   VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex);
1248:   VecSetOperation(*vec, VECOP_VIEWNATIVE, (void (*)(void)) VecView_Plex_Native);
1249:   VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex);
1250:   VecSetOperation(*vec, VECOP_LOADNATIVE, (void (*)(void)) VecLoad_Plex_Native);
1251:   return(0);
1252: }

1254: static PetscErrorCode DMCreateLocalVector_Plex(DM dm,Vec *vec)
1255: {

1259:   DMCreateLocalVector_Section_Private(dm,vec);
1260:   VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex_Local);
1261:   VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex_Local);
1262:   return(0);
1263: }

1265: static PetscErrorCode DMGetDimPoints_Plex(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
1266: {
1267:   PetscInt       depth, d;

1271:   DMPlexGetDepth(dm, &depth);
1272:   if (depth == 1) {
1273:     DMGetDimension(dm, &d);
1274:     if (dim == 0)      {DMPlexGetDepthStratum(dm, dim, pStart, pEnd);}
1275:     else if (dim == d) {DMPlexGetDepthStratum(dm, 1, pStart, pEnd);}
1276:     else               {*pStart = 0; *pEnd = 0;}
1277:   } else {
1278:     DMPlexGetDepthStratum(dm, dim, pStart, pEnd);
1279:   }
1280:   return(0);
1281: }

1283: static PetscErrorCode DMGetNeighors_Plex(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
1284: {
1285:   PetscSF        sf;

1289:   DMGetPointSF(dm, &sf);
1290:   PetscSFGetRanks(sf, nranks, ranks, NULL, NULL, NULL);
1291:   return(0);
1292: }

1294: PetscErrorCode DMInitialize_Plex(DM dm)
1295: {

1299:   dm->ops->view                            = DMView_Plex;
1300:   dm->ops->load                            = DMLoad_Plex;
1301:   dm->ops->setfromoptions                  = DMSetFromOptions_Plex;
1302:   dm->ops->clone                           = DMClone_Plex;
1303:   dm->ops->setup                           = DMSetUp_Plex;
1304:   dm->ops->createdefaultsection            = DMCreateDefaultSection_Plex;
1305:   dm->ops->createdefaultconstraints        = DMCreateDefaultConstraints_Plex;
1306:   dm->ops->createglobalvector              = DMCreateGlobalVector_Plex;
1307:   dm->ops->createlocalvector               = DMCreateLocalVector_Plex;
1308:   dm->ops->getlocaltoglobalmapping         = NULL;
1309:   dm->ops->createfieldis                   = NULL;
1310:   dm->ops->createcoordinatedm              = DMCreateCoordinateDM_Plex;
1311:   dm->ops->getcoloring                     = NULL;
1312:   dm->ops->creatematrix                    = DMCreateMatrix_Plex;
1313:   dm->ops->createinterpolation             = DMCreateInterpolation_Plex;
1314:   dm->ops->getaggregates                   = NULL;
1315:   dm->ops->getinjection                    = DMCreateInjection_Plex;
1316:   dm->ops->refine                          = DMRefine_Plex;
1317:   dm->ops->coarsen                         = DMCoarsen_Plex;
1318:   dm->ops->refinehierarchy                 = DMRefineHierarchy_Plex;
1319:   dm->ops->coarsenhierarchy                = DMCoarsenHierarchy_Plex;
1320:   dm->ops->globaltolocalbegin              = NULL;
1321:   dm->ops->globaltolocalend                = NULL;
1322:   dm->ops->localtoglobalbegin              = NULL;
1323:   dm->ops->localtoglobalend                = NULL;
1324:   dm->ops->destroy                         = DMDestroy_Plex;
1325:   dm->ops->createsubdm                     = DMCreateSubDM_Plex;
1326:   dm->ops->getdimpoints                    = DMGetDimPoints_Plex;
1327:   dm->ops->locatepoints                    = DMLocatePoints_Plex;
1328:   dm->ops->projectfunctionlocal            = DMProjectFunctionLocal_Plex;
1329:   dm->ops->projectfunctionlabellocal       = DMProjectFunctionLabelLocal_Plex;
1330:   dm->ops->projectfieldlocal               = DMProjectFieldLocal_Plex;
1331:   dm->ops->projectfieldlabellocal          = DMProjectFieldLabelLocal_Plex;
1332:   dm->ops->computel2diff                   = DMComputeL2Diff_Plex;
1333:   dm->ops->computel2gradientdiff           = DMComputeL2GradientDiff_Plex;
1334:   dm->ops->computel2fielddiff              = DMComputeL2FieldDiff_Plex;
1335:   dm->ops->getneighbors                    = DMGetNeighors_Plex;
1336:   PetscObjectComposeFunction((PetscObject)dm,"DMAdaptLabel_C",DMAdaptLabel_Plex);
1337:   return(0);
1338: }

1340: PetscErrorCode DMClone_Plex(DM dm, DM *newdm)
1341: {
1342:   DM_Plex        *mesh = (DM_Plex *) dm->data;

1346:   mesh->refct++;
1347:   (*newdm)->data = mesh;
1348:   PetscObjectChangeTypeName((PetscObject) *newdm, DMPLEX);
1349:   DMInitialize_Plex(*newdm);
1350:   return(0);
1351: }

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

1359:   Level: intermediate

1361: .seealso: DMType, DMPlexCreate(), DMCreate(), DMSetType()
1362: M*/

1364: PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm)
1365: {
1366:   DM_Plex        *mesh;
1367:   PetscInt       unit, d;

1372:   PetscNewLog(dm,&mesh);
1373:   dm->dim  = 0;
1374:   dm->data = mesh;

1376:   mesh->refct             = 1;
1377:   PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection);
1378:   mesh->maxConeSize       = 0;
1379:   mesh->cones             = NULL;
1380:   mesh->coneOrientations  = NULL;
1381:   PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection);
1382:   mesh->maxSupportSize    = 0;
1383:   mesh->supports          = NULL;
1384:   mesh->refinementUniform = PETSC_TRUE;
1385:   mesh->refinementLimit   = -1.0;

1387:   mesh->facesTmp = NULL;

1389:   mesh->tetgenOpts   = NULL;
1390:   mesh->triangleOpts = NULL;
1391:   PetscPartitionerCreate(PetscObjectComm((PetscObject)dm), &mesh->partitioner);
1392:   PetscPartitionerSetTypeFromOptions_Internal(mesh->partitioner);
1393:   mesh->remeshBd     = PETSC_FALSE;

1395:   mesh->subpointMap = NULL;

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

1399:   mesh->regularRefinement   = PETSC_FALSE;
1400:   mesh->depthState          = -1;
1401:   mesh->globalVertexNumbers = NULL;
1402:   mesh->globalCellNumbers   = NULL;
1403:   mesh->anchorSection       = NULL;
1404:   mesh->anchorIS            = NULL;
1405:   mesh->createanchors       = NULL;
1406:   mesh->computeanchormatrix = NULL;
1407:   mesh->parentSection       = NULL;
1408:   mesh->parents             = NULL;
1409:   mesh->childIDs            = NULL;
1410:   mesh->childSection        = NULL;
1411:   mesh->children            = NULL;
1412:   mesh->referenceTree       = NULL;
1413:   mesh->getchildsymmetry    = NULL;
1414:   for (d = 0; d < 8; ++d) mesh->hybridPointMax[d] = PETSC_DETERMINE;
1415:   mesh->vtkCellHeight       = 0;
1416:   mesh->useCone             = PETSC_FALSE;
1417:   mesh->useClosure          = PETSC_TRUE;
1418:   mesh->useAnchors          = PETSC_FALSE;

1420:   mesh->maxProjectionHeight = 0;

1422:   mesh->printSetValues = PETSC_FALSE;
1423:   mesh->printFEM       = 0;
1424:   mesh->printTol       = 1.0e-10;

1426:   DMInitialize_Plex(dm);
1427:   return(0);
1428: }

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

1433:   Collective on MPI_Comm

1435:   Input Parameter:
1436: . comm - The communicator for the DMPlex object

1438:   Output Parameter:
1439: . mesh  - The DMPlex object

1441:   Level: beginner

1443: .keywords: DMPlex, create
1444: @*/
1445: PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh)
1446: {

1451:   DMCreate(comm, mesh);
1452:   DMSetType(*mesh, DMPLEX);
1453:   return(0);
1454: }

1456: /*
1457:   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
1458: */
1459: static PetscErrorCode DMPlexBuildFromCellList_Parallel_Private(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const int cells[], PetscSF *sfVert)
1460: {
1461:   PetscSF         sfPoint;
1462:   PetscLayout     vLayout;
1463:   PetscHashI      vhash;
1464:   PetscSFNode    *remoteVerticesAdj, *vertexLocal, *vertexOwner, *remoteVertex;
1465:   const PetscInt *vrange;
1466:   PetscInt        numVerticesAdj, off, *verticesAdj, numVerticesGhost = 0, *localVertex, *cone, c, p, v, g;
1467:   PETSC_UNUSED PetscHashIIter ret, iter;
1468:   PetscMPIInt     rank, numProcs;
1469:   PetscErrorCode  ierr;

1472:   MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);
1473:   MPI_Comm_size(PetscObjectComm((PetscObject) dm), &numProcs);
1474:   /* Partition vertices */
1475:   PetscLayoutCreate(PetscObjectComm((PetscObject) dm), &vLayout);
1476:   PetscLayoutSetLocalSize(vLayout, numVertices);
1477:   PetscLayoutSetBlockSize(vLayout, 1);
1478:   PetscLayoutSetUp(vLayout);
1479:   PetscLayoutGetRanges(vLayout, &vrange);
1480:   /* Count vertices and map them to procs */
1481:   PetscHashICreate(vhash);
1482:   for (c = 0; c < numCells; ++c) {
1483:     for (p = 0; p < numCorners; ++p) {
1484:       PetscHashIPut(vhash, cells[c*numCorners+p], ret, iter);
1485:     }
1486:   }
1487:   PetscHashISize(vhash, numVerticesAdj);
1488:   PetscMalloc1(numVerticesAdj, &verticesAdj);
1489:   off = 0; PetscHashIGetKeys(vhash, &off, verticesAdj);
1490:   if (off != numVerticesAdj) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid number of local vertices %D should be %D", off, numVerticesAdj);
1491:   PetscSortInt(numVerticesAdj, verticesAdj);
1492:   PetscMalloc1(numVerticesAdj, &remoteVerticesAdj);
1493:   for (v = 0; v < numVerticesAdj; ++v) {
1494:     const PetscInt gv = verticesAdj[v];
1495:     PetscInt       vrank;

1497:     PetscFindInt(gv, numProcs+1, vrange, &vrank);
1498:     vrank = vrank < 0 ? -(vrank+2) : vrank;
1499:     remoteVerticesAdj[v].index = gv - vrange[vrank];
1500:     remoteVerticesAdj[v].rank  = vrank;
1501:   }
1502:   /* Create cones */
1503:   DMPlexSetChart(dm, 0, numCells+numVerticesAdj);
1504:   for (c = 0; c < numCells; ++c) {DMPlexSetConeSize(dm, c, numCorners);}
1505:   DMSetUp(dm);
1506:   DMGetWorkArray(dm, numCorners, PETSC_INT, &cone);
1507:   for (c = 0; c < numCells; ++c) {
1508:     for (p = 0; p < numCorners; ++p) {
1509:       const PetscInt gv = cells[c*numCorners+p];
1510:       PetscInt       lv;

1512:       PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv);
1513:       if (lv < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %D in local connectivity", gv);
1514:       cone[p] = lv+numCells;
1515:     }
1516:     DMPlexSetCone(dm, c, cone);
1517:   }
1518:   DMRestoreWorkArray(dm, numCorners, PETSC_INT, &cone);
1519:   /* Create SF for vertices */
1520:   PetscSFCreate(PetscObjectComm((PetscObject)dm), sfVert);
1521:   PetscObjectSetName((PetscObject) *sfVert, "Vertex Ownership SF");
1522:   PetscSFSetFromOptions(*sfVert);
1523:   PetscSFSetGraph(*sfVert, numVertices, numVerticesAdj, NULL, PETSC_OWN_POINTER, remoteVerticesAdj, PETSC_OWN_POINTER);
1524:   PetscFree(verticesAdj);
1525:   /* Build pointSF */
1526:   PetscMalloc2(numVerticesAdj, &vertexLocal, numVertices, &vertexOwner);
1527:   for (v = 0; v < numVerticesAdj; ++v) {vertexLocal[v].index = v+numCells; vertexLocal[v].rank = rank;}
1528:   for (v = 0; v < numVertices;    ++v) {vertexOwner[v].index = -1;         vertexOwner[v].rank = -1;}
1529:   PetscSFReduceBegin(*sfVert, MPI_2INT, vertexLocal, vertexOwner, MPI_MAXLOC);
1530:   PetscSFReduceEnd(*sfVert, MPI_2INT, vertexLocal, vertexOwner, MPI_MAXLOC);
1531:   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);
1532:   PetscSFBcastBegin(*sfVert, MPI_2INT, vertexOwner, vertexLocal);
1533:   PetscSFBcastEnd(*sfVert, MPI_2INT, vertexOwner, vertexLocal);
1534:   for (v = 0; v < numVerticesAdj; ++v) if (vertexLocal[v].rank != rank) ++numVerticesGhost;
1535:   PetscMalloc1(numVerticesGhost, &localVertex);
1536:   PetscMalloc1(numVerticesGhost, &remoteVertex);
1537:   for (v = 0, g = 0; v < numVerticesAdj; ++v) {
1538:     if (vertexLocal[v].rank != rank) {
1539:       localVertex[g]        = v+numCells;
1540:       remoteVertex[g].index = vertexLocal[v].index;
1541:       remoteVertex[g].rank  = vertexLocal[v].rank;
1542:       ++g;
1543:     }
1544:   }
1545:   PetscFree2(vertexLocal, vertexOwner);
1546:   if (g != numVerticesGhost) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid number of vertex ghosts %D should be %D", g, numVerticesGhost);
1547:   DMGetPointSF(dm, &sfPoint);
1548:   PetscObjectSetName((PetscObject) sfPoint, "point SF");
1549:   PetscSFSetGraph(sfPoint, numCells+numVerticesAdj, numVerticesGhost, localVertex, PETSC_OWN_POINTER, remoteVertex, PETSC_OWN_POINTER);
1550:   PetscLayoutDestroy(&vLayout);
1551:   PetscHashIDestroy(vhash);
1552:   /* Fill in the rest of the topology structure */
1553:   DMPlexSymmetrize(dm);
1554:   DMPlexStratify(dm);
1555:   return(0);
1556: }

1558: /*
1559:   This takes as input the coordinates for each owned vertex
1560: */
1561: static PetscErrorCode DMPlexBuildCoordinates_Parallel_Private(DM dm, PetscInt spaceDim, PetscInt numCells, PetscSF sfVert, const double vertexCoords[])
1562: {
1563:   PetscSection   coordSection;
1564:   Vec            coordinates;
1565:   PetscScalar   *coords;
1566:   PetscInt       numVertices, numVerticesAdj, coordSize, v;

1570:   PetscSFGetGraph(sfVert, &numVertices, &numVerticesAdj, NULL, NULL);
1571:   DMGetCoordinateSection(dm, &coordSection);
1572:   PetscSectionSetNumFields(coordSection, 1);
1573:   PetscSectionSetFieldComponents(coordSection, 0, spaceDim);
1574:   PetscSectionSetChart(coordSection, numCells, numCells + numVerticesAdj);
1575:   for (v = numCells; v < numCells+numVerticesAdj; ++v) {
1576:     PetscSectionSetDof(coordSection, v, spaceDim);
1577:     PetscSectionSetFieldDof(coordSection, v, 0, spaceDim);
1578:   }
1579:   PetscSectionSetUp(coordSection);
1580:   PetscSectionGetStorageSize(coordSection, &coordSize);
1581:   VecCreate(PetscObjectComm((PetscObject)dm), &coordinates);
1582:   VecSetBlockSize(coordinates, spaceDim);
1583:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
1584:   VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
1585:   VecSetType(coordinates,VECSTANDARD);
1586:   VecGetArray(coordinates, &coords);
1587:   {
1588:     MPI_Datatype coordtype;

1590:     /* Need a temp buffer for coords if we have complex/single */
1591:     MPI_Type_contiguous(spaceDim, MPI_DOUBLE, &coordtype);
1592:     MPI_Type_commit(&coordtype);
1593:     PetscSFBcastBegin(sfVert, coordtype, vertexCoords, coords);
1594:     PetscSFBcastEnd(sfVert, coordtype, vertexCoords, coords);
1595:     MPI_Type_free(&coordtype);
1596:   }
1597:   VecRestoreArray(coordinates, &coords);
1598:   DMSetCoordinatesLocal(dm, coordinates);
1599:   VecDestroy(&coordinates);
1600:   return(0);
1601: }

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

1606:   Input Parameters:
1607: + comm - The communicator
1608: . dim - The topological dimension of the mesh
1609: . numCells - The number of cells owned by this process
1610: . numVertices - The number of vertices owned by this process
1611: . numCorners - The number of vertices for each cell
1612: . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
1613: . cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
1614: . spaceDim - The spatial dimension used for coordinates
1615: - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex

1617:   Output Parameter:
1618: + dm - The DM
1619: - vertexSF - Optional, SF describing complete vertex ownership

1621:   Note: Two triangles sharing a face
1622: $
1623: $        2
1624: $      / | \
1625: $     /  |  \
1626: $    /   |   \
1627: $   0  0 | 1  3
1628: $    \   |   /
1629: $     \  |  /
1630: $      \ | /
1631: $        1
1632: would have input
1633: $  numCells = 2, numVertices = 4
1634: $  cells = [0 1 2  1 3 2]
1635: $
1636: which would result in the DMPlex
1637: $
1638: $        4
1639: $      / | \
1640: $     /  |  \
1641: $    /   |   \
1642: $   2  0 | 1  5
1643: $    \   |   /
1644: $     \  |  /
1645: $      \ | /
1646: $        3

1648:   Level: beginner

1650: .seealso: DMPlexCreateFromCellList(), DMPlexCreateFromDAG(), DMPlexCreate()
1651: @*/
1652: PetscErrorCode DMPlexCreateFromCellListParallel(MPI_Comm comm, PetscInt dim, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, PetscBool interpolate, const int cells[], PetscInt spaceDim, const double vertexCoords[], PetscSF *vertexSF, DM *dm)
1653: {
1654:   PetscSF        sfVert;

1658:   DMCreate(comm, dm);
1659:   DMSetType(*dm, DMPLEX);
1662:   DMSetDimension(*dm, dim);
1663:   DMPlexBuildFromCellList_Parallel_Private(*dm, numCells, numVertices, numCorners, cells, &sfVert);
1664:   if (interpolate) {
1665:     DM idm = NULL;

1667:     DMPlexInterpolate(*dm, &idm);
1668:     DMDestroy(dm);
1669:     *dm  = idm;
1670:   }
1671:   DMPlexBuildCoordinates_Parallel_Private(*dm, spaceDim, numCells, sfVert, vertexCoords);
1672:   if (vertexSF) *vertexSF = sfVert;
1673:   else PetscSFDestroy(&sfVert);
1674:   return(0);
1675: }

1677: /*
1678:   This takes as input the common mesh generator output, a list of the vertices for each cell
1679: */
1680: static PetscErrorCode DMPlexBuildFromCellList_Private(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const int cells[])
1681: {
1682:   PetscInt      *cone, c, p;

1686:   DMPlexSetChart(dm, 0, numCells+numVertices);
1687:   for (c = 0; c < numCells; ++c) {
1688:     DMPlexSetConeSize(dm, c, numCorners);
1689:   }
1690:   DMSetUp(dm);
1691:   DMGetWorkArray(dm, numCorners, PETSC_INT, &cone);
1692:   for (c = 0; c < numCells; ++c) {
1693:     for (p = 0; p < numCorners; ++p) {
1694:       cone[p] = cells[c*numCorners+p]+numCells;
1695:     }
1696:     DMPlexSetCone(dm, c, cone);
1697:   }
1698:   DMRestoreWorkArray(dm, numCorners, PETSC_INT, &cone);
1699:   DMPlexSymmetrize(dm);
1700:   DMPlexStratify(dm);
1701:   return(0);
1702: }

1704: /*
1705:   This takes as input the coordinates for each vertex
1706: */
1707: static PetscErrorCode DMPlexBuildCoordinates_Private(DM dm, PetscInt spaceDim, PetscInt numCells, PetscInt numVertices, const double vertexCoords[])
1708: {
1709:   PetscSection   coordSection;
1710:   Vec            coordinates;
1711:   DM             cdm;
1712:   PetscScalar   *coords;
1713:   PetscInt       v, d;

1717:   DMGetCoordinateSection(dm, &coordSection);
1718:   PetscSectionSetNumFields(coordSection, 1);
1719:   PetscSectionSetFieldComponents(coordSection, 0, spaceDim);
1720:   PetscSectionSetChart(coordSection, numCells, numCells + numVertices);
1721:   for (v = numCells; v < numCells+numVertices; ++v) {
1722:     PetscSectionSetDof(coordSection, v, spaceDim);
1723:     PetscSectionSetFieldDof(coordSection, v, 0, spaceDim);
1724:   }
1725:   PetscSectionSetUp(coordSection);

1727:   DMGetCoordinateDM(dm, &cdm);
1728:   DMCreateLocalVector(cdm, &coordinates);
1729:   VecSetBlockSize(coordinates, spaceDim);
1730:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
1731:   VecGetArray(coordinates, &coords);
1732:   for (v = 0; v < numVertices; ++v) {
1733:     for (d = 0; d < spaceDim; ++d) {
1734:       coords[v*spaceDim+d] = vertexCoords[v*spaceDim+d];
1735:     }
1736:   }
1737:   VecRestoreArray(coordinates, &coords);
1738:   DMSetCoordinatesLocal(dm, coordinates);
1739:   VecDestroy(&coordinates);
1740:   return(0);
1741: }

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

1746:   Input Parameters:
1747: + comm - The communicator
1748: . dim - The topological dimension of the mesh
1749: . numCells - The number of cells
1750: . numVertices - The number of vertices
1751: . numCorners - The number of vertices for each cell
1752: . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
1753: . cells - An array of numCells*numCorners numbers, the vertices for each cell
1754: . spaceDim - The spatial dimension used for coordinates
1755: - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex

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

1760:   Note: Two triangles sharing a face
1761: $
1762: $        2
1763: $      / | \
1764: $     /  |  \
1765: $    /   |   \
1766: $   0  0 | 1  3
1767: $    \   |   /
1768: $     \  |  /
1769: $      \ | /
1770: $        1
1771: would have input
1772: $  numCells = 2, numVertices = 4
1773: $  cells = [0 1 2  1 3 2]
1774: $
1775: which would result in the DMPlex
1776: $
1777: $        4
1778: $      / | \
1779: $     /  |  \
1780: $    /   |   \
1781: $   2  0 | 1  5
1782: $    \   |   /
1783: $     \  |  /
1784: $      \ | /
1785: $        3

1787:   Level: beginner

1789: .seealso: DMPlexCreateFromDAG(), DMPlexCreate()
1790: @*/
1791: 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)
1792: {

1796:   DMCreate(comm, dm);
1797:   DMSetType(*dm, DMPLEX);
1798:   DMSetDimension(*dm, dim);
1799:   DMPlexBuildFromCellList_Private(*dm, numCells, numVertices, numCorners, cells);
1800:   if (interpolate) {
1801:     DM idm = NULL;

1803:     DMPlexInterpolate(*dm, &idm);
1804:     DMDestroy(dm);
1805:     *dm  = idm;
1806:   }
1807:   DMPlexBuildCoordinates_Private(*dm, spaceDim, numCells, numVertices, vertexCoords);
1808:   return(0);
1809: }

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

1814:   Input Parameters:
1815: + dm - The empty DM object, usually from DMCreate() and DMSetDimension()
1816: . depth - The depth of the DAG
1817: . numPoints - The number of points at each depth
1818: . coneSize - The cone size of each point
1819: . cones - The concatenation of the cone points for each point, the cone list must be oriented correctly for each point
1820: . coneOrientations - The orientation of each cone point
1821: - vertexCoords - An array of numVertices*dim numbers, the coordinates of each vertex

1823:   Output Parameter:
1824: . dm - The DM

1826:   Note: Two triangles sharing a face would have input
1827: $  depth = 1, numPoints = [4 2], coneSize = [3 3 0 0 0 0]
1828: $  cones = [2 3 4  3 5 4], coneOrientations = [0 0 0  0 0 0]
1829: $ vertexCoords = [-1.0 0.0  0.0 -1.0  0.0 1.0  1.0 0.0]
1830: $
1831: which would result in the DMPlex
1832: $
1833: $        4
1834: $      / | \
1835: $     /  |  \
1836: $    /   |   \
1837: $   2  0 | 1  5
1838: $    \   |   /
1839: $     \  |  /
1840: $      \ | /
1841: $        3
1842: $
1843: $ Notice that all points are numbered consecutively, unlikely DMPlexCreateFromCellList()

1845:   Level: advanced

1847: .seealso: DMPlexCreateFromCellList(), DMPlexCreate()
1848: @*/
1849: PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[])
1850: {
1851:   Vec            coordinates;
1852:   PetscSection   coordSection;
1853:   PetscScalar    *coords;
1854:   PetscInt       coordSize, firstVertex = -1, pStart = 0, pEnd = 0, p, v, dim, dimEmbed, d, off;

1858:   DMGetDimension(dm, &dim);
1859:   DMGetCoordinateDim(dm, &dimEmbed);
1860:   if (dimEmbed < dim) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Embedding dimension %d cannot be less than intrinsic dimension %d",dimEmbed,dim);
1861:   for (d = 0; d <= depth; ++d) pEnd += numPoints[d];
1862:   DMPlexSetChart(dm, pStart, pEnd);
1863:   for (p = pStart; p < pEnd; ++p) {
1864:     DMPlexSetConeSize(dm, p, coneSize[p-pStart]);
1865:     if (firstVertex < 0 && !coneSize[p - pStart]) {
1866:       firstVertex = p - pStart;
1867:     }
1868:   }
1869:   if (firstVertex < 0 && numPoints[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected %d vertices but could not find any", numPoints[0]);
1870:   DMSetUp(dm); /* Allocate space for cones */
1871:   for (p = pStart, off = 0; p < pEnd; off += coneSize[p-pStart], ++p) {
1872:     DMPlexSetCone(dm, p, &cones[off]);
1873:     DMPlexSetConeOrientation(dm, p, &coneOrientations[off]);
1874:   }
1875:   DMPlexSymmetrize(dm);
1876:   DMPlexStratify(dm);
1877:   /* Build coordinates */
1878:   DMGetCoordinateSection(dm, &coordSection);
1879:   PetscSectionSetNumFields(coordSection, 1);
1880:   PetscSectionSetFieldComponents(coordSection, 0, dimEmbed);
1881:   PetscSectionSetChart(coordSection, firstVertex, firstVertex+numPoints[0]);
1882:   for (v = firstVertex; v < firstVertex+numPoints[0]; ++v) {
1883:     PetscSectionSetDof(coordSection, v, dimEmbed);
1884:     PetscSectionSetFieldDof(coordSection, v, 0, dimEmbed);
1885:   }
1886:   PetscSectionSetUp(coordSection);
1887:   PetscSectionGetStorageSize(coordSection, &coordSize);
1888:   VecCreate(PETSC_COMM_SELF, &coordinates);
1889:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
1890:   VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
1891:   VecSetBlockSize(coordinates, dimEmbed);
1892:   VecSetType(coordinates,VECSTANDARD);
1893:   VecGetArray(coordinates, &coords);
1894:   for (v = 0; v < numPoints[0]; ++v) {
1895:     PetscInt off;

1897:     PetscSectionGetOffset(coordSection, v+firstVertex, &off);
1898:     for (d = 0; d < dimEmbed; ++d) {
1899:       coords[off+d] = vertexCoords[v*dimEmbed+d];
1900:     }
1901:   }
1902:   VecRestoreArray(coordinates, &coords);
1903:   DMSetCoordinatesLocal(dm, coordinates);
1904:   VecDestroy(&coordinates);
1905:   return(0);
1906: }

1908: /*@C
1909:   DMPlexCreateFromFile - This takes a filename and produces a DM

1911:   Input Parameters:
1912: + comm - The communicator
1913: . filename - A file name
1914: - interpolate - Flag to create intermediate mesh pieces (edges, faces)

1916:   Output Parameter:
1917: . dm - The DM

1919:   Level: beginner

1921: .seealso: DMPlexCreateFromDAG(), DMPlexCreateFromCellList(), DMPlexCreate()
1922: @*/
1923: PetscErrorCode DMPlexCreateFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
1924: {
1925:   const char    *extGmsh   = ".msh";
1926:   const char    *extCGNS   = ".cgns";
1927:   const char    *extExodus = ".exo";
1928:   const char    *extFluent = ".cas";
1929:   const char    *extHDF5   = ".h5";
1930:   const char    *extMed    = ".med";
1931:   size_t         len;
1932:   PetscBool      isGmsh, isCGNS, isExodus, isFluent, isHDF5, isMed;
1933:   PetscMPIInt    rank;

1939:   MPI_Comm_rank(comm, &rank);
1940:   PetscStrlen(filename, &len);
1941:   if (!len) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Filename must be a valid path");
1942:   PetscStrncmp(&filename[PetscMax(0,len-4)], extGmsh,   4, &isGmsh);
1943:   PetscStrncmp(&filename[PetscMax(0,len-5)], extCGNS,   5, &isCGNS);
1944:   PetscStrncmp(&filename[PetscMax(0,len-4)], extExodus, 4, &isExodus);
1945:   PetscStrncmp(&filename[PetscMax(0,len-4)], extFluent, 4, &isFluent);
1946:   PetscStrncmp(&filename[PetscMax(0,len-3)], extHDF5,   3, &isHDF5);
1947:   PetscStrncmp(&filename[PetscMax(0,len-4)], extMed,    4, &isMed);
1948:   if (isGmsh) {
1949:     DMPlexCreateGmshFromFile(comm, filename, interpolate, dm);
1950:   } else if (isCGNS) {
1951:     DMPlexCreateCGNSFromFile(comm, filename, interpolate, dm);
1952:   } else if (isExodus) {
1953:     DMPlexCreateExodusFromFile(comm, filename, interpolate, dm);
1954:   } else if (isFluent) {
1955:     DMPlexCreateFluentFromFile(comm, filename, interpolate, dm);
1956:   } else if (isHDF5) {
1957:     PetscViewer viewer;

1959:     PetscViewerCreate(comm, &viewer);
1960:     PetscViewerSetType(viewer, PETSCVIEWERHDF5);
1961:     PetscViewerFileSetMode(viewer, FILE_MODE_READ);
1962:     PetscViewerFileSetName(viewer, filename);
1963:     DMCreate(comm, dm);
1964:     DMSetType(*dm, DMPLEX);
1965:     DMLoad(*dm, viewer);
1966:     PetscViewerDestroy(&viewer);
1967:   } else if (isMed) {
1968:     DMPlexCreateMedFromFile(comm, filename, interpolate, dm);
1969:   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename);
1970:   return(0);
1971: }

1973: /*@
1974:   DMPlexCreateReferenceCell - Create a DMPLEX with the appropriate FEM reference cell

1976:   Collective on comm

1978:   Input Parameters:
1979: + comm    - The communicator
1980: . dim     - The spatial dimension
1981: - simplex - Flag for simplex, otherwise use a tensor-product cell

1983:   Output Parameter:
1984: . refdm - The reference cell

1986:   Level: intermediate

1988: .keywords: reference cell
1989: .seealso:
1990: @*/
1991: PetscErrorCode DMPlexCreateReferenceCell(MPI_Comm comm, PetscInt dim, PetscBool simplex, DM *refdm)
1992: {
1993:   DM             rdm;
1994:   Vec            coords;

1998:   DMCreate(comm, &rdm);
1999:   DMSetType(rdm, DMPLEX);
2000:   DMSetDimension(rdm, dim);
2001:   switch (dim) {
2002:   case 0:
2003:   {
2004:     PetscInt    numPoints[1]        = {1};
2005:     PetscInt    coneSize[1]         = {0};
2006:     PetscInt    cones[1]            = {0};
2007:     PetscInt    coneOrientations[1] = {0};
2008:     PetscScalar vertexCoords[1]     = {0.0};

2010:     DMPlexCreateFromDAG(rdm, 0, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2011:   }
2012:   break;
2013:   case 1:
2014:   {
2015:     PetscInt    numPoints[2]        = {2, 1};
2016:     PetscInt    coneSize[3]         = {2, 0, 0};
2017:     PetscInt    cones[2]            = {1, 2};
2018:     PetscInt    coneOrientations[2] = {0, 0};
2019:     PetscScalar vertexCoords[2]     = {-1.0,  1.0};

2021:     DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2022:   }
2023:   break;
2024:   case 2:
2025:     if (simplex) {
2026:       PetscInt    numPoints[2]        = {3, 1};
2027:       PetscInt    coneSize[4]         = {3, 0, 0, 0};
2028:       PetscInt    cones[3]            = {1, 2, 3};
2029:       PetscInt    coneOrientations[3] = {0, 0, 0};
2030:       PetscScalar vertexCoords[6]     = {-1.0, -1.0,  1.0, -1.0,  -1.0, 1.0};

2032:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2033:     } else {
2034:       PetscInt    numPoints[2]        = {4, 1};
2035:       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
2036:       PetscInt    cones[4]            = {1, 2, 3, 4};
2037:       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
2038:       PetscScalar vertexCoords[8]     = {-1.0, -1.0,  1.0, -1.0,  1.0, 1.0,  -1.0, 1.0};

2040:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2041:     }
2042:   break;
2043:   case 3:
2044:     if (simplex) {
2045:       PetscInt    numPoints[2]        = {4, 1};
2046:       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
2047:       PetscInt    cones[4]            = {1, 3, 2, 4};
2048:       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
2049:       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};

2051:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2052:     } else {
2053:       PetscInt    numPoints[2]        = {8, 1};
2054:       PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
2055:       PetscInt    cones[8]            = {1, 4, 3, 2, 5, 6, 7, 8};
2056:       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
2057:       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,
2058:                                          -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  1.0, 1.0,  1.0,  -1.0, 1.0,  1.0};

2060:       DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
2061:     }
2062:   break;
2063:   default:
2064:     SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Cannot create reference cell for dimension %d", dim);
2065:   }
2066:   *refdm = NULL;
2067:   DMPlexInterpolate(rdm, refdm);
2068:   if (rdm->coordinateDM) {
2069:     DM           ncdm;
2070:     PetscSection cs;
2071:     PetscInt     pEnd = -1;

2073:     DMGetDefaultSection(rdm->coordinateDM, &cs);
2074:     if (cs) {PetscSectionGetChart(cs, NULL, &pEnd);}
2075:     if (pEnd >= 0) {
2076:       DMClone(rdm->coordinateDM, &ncdm);
2077:       DMSetDefaultSection(ncdm, cs);
2078:       DMSetCoordinateDM(*refdm, ncdm);
2079:       DMDestroy(&ncdm);
2080:     }
2081:   }
2082:   DMGetCoordinatesLocal(rdm, &coords);
2083:   if (coords) {
2084:     DMSetCoordinatesLocal(*refdm, coords);
2085:   } else {
2086:     DMGetCoordinates(rdm, &coords);
2087:     if (coords) {DMSetCoordinates(*refdm, coords);}
2088:   }
2089:   DMDestroy(&rdm);
2090:   return(0);
2091: }