xref: /petsc/src/dm/impls/plex/plexcreate.c (revision 35e84bc71b7cb464dcdea087d630b18bbaf3b962)
1 #define PETSCDM_DLL
2 #include <petsc/private/dmpleximpl.h>    /*I   "petscdmplex.h"   I*/
3 #include <petscdmda.h>
4 #include <petscsf.h>
5 
6 #undef __FUNCT__
7 #define __FUNCT__ "DMPlexCreateDoublet"
8 /*@
9   DMPlexCreateDoublet - Creates a mesh of two cells of the specified type, optionally with later refinement.
10 
11   Collective on MPI_Comm
12 
13   Input Parameters:
14 + comm - The communicator for the DM object
15 . dim - The spatial dimension
16 . simplex - Flag for simplicial cells, otherwise they are tensor product cells
17 . interpolate - Flag to create intermediate mesh pieces (edges, faces)
18 . refinementUniform - Flag for uniform parallel refinement
19 - refinementLimit - A nonzero number indicates the largest admissible volume for a refined cell
20 
21   Output Parameter:
22 . dm  - The DM object
23 
24   Level: beginner
25 
26 .keywords: DM, create
27 .seealso: DMSetType(), DMCreate()
28 @*/
29 PetscErrorCode DMPlexCreateDoublet(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscBool interpolate, PetscBool refinementUniform, PetscReal refinementLimit, DM *newdm)
30 {
31   DM             dm;
32   PetscInt       p;
33   PetscMPIInt    rank;
34   PetscErrorCode ierr;
35 
36   PetscFunctionBegin;
37   ierr = DMCreate(comm, &dm);CHKERRQ(ierr);
38   ierr = DMSetType(dm, DMPLEX);CHKERRQ(ierr);
39   ierr = DMSetDimension(dm, dim);CHKERRQ(ierr);
40   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
41   switch (dim) {
42   case 2:
43     if (simplex) {ierr = PetscObjectSetName((PetscObject) dm, "triangular");CHKERRQ(ierr);}
44     else         {ierr = PetscObjectSetName((PetscObject) dm, "quadrilateral");CHKERRQ(ierr);}
45     break;
46   case 3:
47     if (simplex) {ierr = PetscObjectSetName((PetscObject) dm, "tetrahedral");CHKERRQ(ierr);}
48     else         {ierr = PetscObjectSetName((PetscObject) dm, "hexahedral");CHKERRQ(ierr);}
49     break;
50   default:
51     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %d", dim);
52   }
53   if (rank) {
54     PetscInt numPoints[2] = {0, 0};
55     ierr = DMPlexCreateFromDAG(dm, 1, numPoints, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
56   } else {
57     switch (dim) {
58     case 2:
59       if (simplex) {
60         PetscInt    numPoints[2]        = {4, 2};
61         PetscInt    coneSize[6]         = {3, 3, 0, 0, 0, 0};
62         PetscInt    cones[6]            = {2, 3, 4,  5, 4, 3};
63         PetscInt    coneOrientations[6] = {0, 0, 0,  0, 0, 0};
64         PetscScalar vertexCoords[8]     = {-0.5, 0.5, 0.0, 0.0, 0.0, 1.0, 0.5, 0.5};
65         PetscInt    markerPoints[8]     = {2, 1, 3, 1, 4, 1, 5, 1};
66 
67         ierr = DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
68         for (p = 0; p < 4; ++p) {ierr = DMSetLabelValue(dm, "marker", markerPoints[p*2], markerPoints[p*2+1]);CHKERRQ(ierr);}
69       } else {
70         PetscInt    numPoints[2]        = {6, 2};
71         PetscInt    coneSize[8]         = {4, 4, 0, 0, 0, 0, 0, 0};
72         PetscInt    cones[8]            = {2, 3, 4, 5,  3, 6, 7, 4};
73         PetscInt    coneOrientations[8] = {0, 0, 0, 0,  0, 0, 0, 0};
74         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};
75 
76         ierr = DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
77       }
78       break;
79     case 3:
80       if (simplex) {
81         PetscInt    numPoints[2]        = {5, 2};
82         PetscInt    coneSize[7]         = {4, 4, 0, 0, 0, 0, 0};
83         PetscInt    cones[8]            = {4, 3, 5, 2,  5, 3, 4, 6};
84         PetscInt    coneOrientations[8] = {0, 0, 0, 0,  0, 0, 0, 0};
85         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};
86         PetscInt    markerPoints[10]    = {2, 1, 3, 1, 4, 1, 5, 1, 6, 1};
87 
88         ierr = DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
89         for (p = 0; p < 5; ++p) {ierr = DMSetLabelValue(dm, "marker", markerPoints[p*2], markerPoints[p*2+1]);CHKERRQ(ierr);}
90       } else {
91         PetscInt    numPoints[2]         = {12, 2};
92         PetscInt    coneSize[14]         = {8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
93         PetscInt    cones[16]            = {2, 3, 4, 5, 6, 7, 8, 9,  5, 4, 10, 11, 7, 12, 13, 8};
94         PetscInt    coneOrientations[16] = {0, 0, 0, 0, 0, 0, 0, 0,  0, 0,  0,  0, 0,  0,  0, 0};
95         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,
96                                             -1.0, -0.5,  0.5,   0.0, -0.5,  0.5,  0.0,  0.5,  0.5,  -1.0,  0.5,  0.5,
97                                              1.0,  0.5, -0.5,   1.0, -0.5, -0.5,  1.0, -0.5,  0.5,   1.0,  0.5,  0.5};
98 
99         ierr = DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
100       }
101       break;
102     default:
103       SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %d", dim);
104     }
105   }
106   *newdm = dm;
107   if (refinementLimit > 0.0) {
108     DM rdm;
109     const char *name;
110 
111     ierr = DMPlexSetRefinementUniform(*newdm, PETSC_FALSE);CHKERRQ(ierr);
112     ierr = DMPlexSetRefinementLimit(*newdm, refinementLimit);CHKERRQ(ierr);
113     ierr = DMRefine(*newdm, comm, &rdm);CHKERRQ(ierr);
114     ierr = PetscObjectGetName((PetscObject) *newdm, &name);CHKERRQ(ierr);
115     ierr = PetscObjectSetName((PetscObject)    rdm,  name);CHKERRQ(ierr);
116     ierr = DMDestroy(newdm);CHKERRQ(ierr);
117     *newdm = rdm;
118   }
119   if (interpolate) {
120     DM idm = NULL;
121     const char *name;
122 
123     ierr = DMPlexInterpolate(*newdm, &idm);CHKERRQ(ierr);
124     ierr = PetscObjectGetName((PetscObject) *newdm, &name);CHKERRQ(ierr);
125     ierr = PetscObjectSetName((PetscObject)    idm,  name);CHKERRQ(ierr);
126     ierr = DMPlexCopyCoordinates(*newdm, idm);CHKERRQ(ierr);
127     ierr = DMCopyLabels(*newdm, idm);CHKERRQ(ierr);
128     ierr = DMDestroy(newdm);CHKERRQ(ierr);
129     *newdm = idm;
130   }
131   {
132     DM refinedMesh     = NULL;
133     DM distributedMesh = NULL;
134 
135     /* Distribute mesh over processes */
136     ierr = DMPlexDistribute(*newdm, 0, NULL, &distributedMesh);CHKERRQ(ierr);
137     if (distributedMesh) {
138       ierr = DMDestroy(newdm);CHKERRQ(ierr);
139       *newdm = distributedMesh;
140     }
141     if (refinementUniform) {
142       ierr = DMPlexSetRefinementUniform(*newdm, refinementUniform);CHKERRQ(ierr);
143       ierr = DMRefine(*newdm, comm, &refinedMesh);CHKERRQ(ierr);
144       if (refinedMesh) {
145         ierr = DMDestroy(newdm);CHKERRQ(ierr);
146         *newdm = refinedMesh;
147       }
148     }
149   }
150   PetscFunctionReturn(0);
151 }
152 
153 #undef __FUNCT__
154 #define __FUNCT__ "DMPlexCreateSquareBoundary"
155 /*@
156   DMPlexCreateSquareBoundary - Creates a 1D mesh the is the boundary of a square lattice.
157 
158   Collective on MPI_Comm
159 
160   Input Parameters:
161 + comm  - The communicator for the DM object
162 . lower - The lower left corner coordinates
163 . upper - The upper right corner coordinates
164 - edges - The number of cells in each direction
165 
166   Output Parameter:
167 . dm  - The DM object
168 
169   Note: Here is the numbering returned for 2 cells in each direction:
170 $ 18--5-17--4--16
171 $  |     |     |
172 $  6    10     3
173 $  |     |     |
174 $ 19-11-20--9--15
175 $  |     |     |
176 $  7     8     2
177 $  |     |     |
178 $ 12--0-13--1--14
179 
180   Level: beginner
181 
182 .keywords: DM, create
183 .seealso: DMPlexCreateBoxMesh(), DMPlexCreateCubeBoundary(), DMSetType(), DMCreate()
184 @*/
185 PetscErrorCode DMPlexCreateSquareBoundary(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
186 {
187   const PetscInt numVertices    = (edges[0]+1)*(edges[1]+1);
188   const PetscInt numEdges       = edges[0]*(edges[1]+1) + (edges[0]+1)*edges[1];
189   PetscInt       markerTop      = 1;
190   PetscInt       markerBottom   = 1;
191   PetscInt       markerRight    = 1;
192   PetscInt       markerLeft     = 1;
193   PetscBool      markerSeparate = PETSC_FALSE;
194   Vec            coordinates;
195   PetscSection   coordSection;
196   PetscScalar    *coords;
197   PetscInt       coordSize;
198   PetscMPIInt    rank;
199   PetscInt       v, vx, vy;
200   PetscErrorCode ierr;
201 
202   PetscFunctionBegin;
203   ierr = PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);CHKERRQ(ierr);
204   if (markerSeparate) {
205     markerTop    = 3;
206     markerBottom = 1;
207     markerRight  = 2;
208     markerLeft   = 4;
209   }
210   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
211   if (!rank) {
212     PetscInt e, ex, ey;
213 
214     ierr = DMPlexSetChart(dm, 0, numEdges+numVertices);CHKERRQ(ierr);
215     for (e = 0; e < numEdges; ++e) {
216       ierr = DMPlexSetConeSize(dm, e, 2);CHKERRQ(ierr);
217     }
218     ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
219     for (vx = 0; vx <= edges[0]; vx++) {
220       for (ey = 0; ey < edges[1]; ey++) {
221         PetscInt edge   = vx*edges[1] + ey + edges[0]*(edges[1]+1);
222         PetscInt vertex = ey*(edges[0]+1) + vx + numEdges;
223         PetscInt cone[2];
224 
225         cone[0] = vertex; cone[1] = vertex+edges[0]+1;
226         ierr    = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr);
227         if (vx == edges[0]) {
228           ierr = DMSetLabelValue(dm, "marker", edge,    markerRight);CHKERRQ(ierr);
229           ierr = DMSetLabelValue(dm, "marker", cone[0], markerRight);CHKERRQ(ierr);
230           if (ey == edges[1]-1) {
231             ierr = DMSetLabelValue(dm, "marker", cone[1], markerRight);CHKERRQ(ierr);
232           }
233         } else if (vx == 0) {
234           ierr = DMSetLabelValue(dm, "marker", edge,    markerLeft);CHKERRQ(ierr);
235           ierr = DMSetLabelValue(dm, "marker", cone[0], markerLeft);CHKERRQ(ierr);
236           if (ey == edges[1]-1) {
237             ierr = DMSetLabelValue(dm, "marker", cone[1], markerLeft);CHKERRQ(ierr);
238           }
239         }
240       }
241     }
242     for (vy = 0; vy <= edges[1]; vy++) {
243       for (ex = 0; ex < edges[0]; ex++) {
244         PetscInt edge   = vy*edges[0]     + ex;
245         PetscInt vertex = vy*(edges[0]+1) + ex + numEdges;
246         PetscInt cone[2];
247 
248         cone[0] = vertex; cone[1] = vertex+1;
249         ierr    = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr);
250         if (vy == edges[1]) {
251           ierr = DMSetLabelValue(dm, "marker", edge,    markerTop);CHKERRQ(ierr);
252           ierr = DMSetLabelValue(dm, "marker", cone[0], markerTop);CHKERRQ(ierr);
253           if (ex == edges[0]-1) {
254             ierr = DMSetLabelValue(dm, "marker", cone[1], markerTop);CHKERRQ(ierr);
255           }
256         } else if (vy == 0) {
257           ierr = DMSetLabelValue(dm, "marker", edge,    markerBottom);CHKERRQ(ierr);
258           ierr = DMSetLabelValue(dm, "marker", cone[0], markerBottom);CHKERRQ(ierr);
259           if (ex == edges[0]-1) {
260             ierr = DMSetLabelValue(dm, "marker", cone[1], markerBottom);CHKERRQ(ierr);
261           }
262         }
263       }
264     }
265   }
266   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
267   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
268   /* Build coordinates */
269   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
270   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
271   ierr = PetscSectionSetChart(coordSection, numEdges, numEdges + numVertices);CHKERRQ(ierr);
272   ierr = PetscSectionSetFieldComponents(coordSection, 0, 2);CHKERRQ(ierr);
273   for (v = numEdges; v < numEdges+numVertices; ++v) {
274     ierr = PetscSectionSetDof(coordSection, v, 2);CHKERRQ(ierr);
275     ierr = PetscSectionSetFieldDof(coordSection, v, 0, 2);CHKERRQ(ierr);
276   }
277   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
278   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
279   ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
280   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
281   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
282   ierr = VecSetBlockSize(coordinates, 2);CHKERRQ(ierr);
283   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
284   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
285   for (vy = 0; vy <= edges[1]; ++vy) {
286     for (vx = 0; vx <= edges[0]; ++vx) {
287       coords[(vy*(edges[0]+1)+vx)*2+0] = lower[0] + ((upper[0] - lower[0])/edges[0])*vx;
288       coords[(vy*(edges[0]+1)+vx)*2+1] = lower[1] + ((upper[1] - lower[1])/edges[1])*vy;
289     }
290   }
291   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
292   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
293   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
294   PetscFunctionReturn(0);
295 }
296 
297 #undef __FUNCT__
298 #define __FUNCT__ "DMPlexCreateCubeBoundary"
299 /*@
300   DMPlexCreateCubeBoundary - Creates a 2D mesh the is the boundary of a cubic lattice.
301 
302   Collective on MPI_Comm
303 
304   Input Parameters:
305 + comm  - The communicator for the DM object
306 . lower - The lower left front corner coordinates
307 . upper - The upper right back corner coordinates
308 - edges - The number of cells in each direction
309 
310   Output Parameter:
311 . dm  - The DM object
312 
313   Level: beginner
314 
315 .keywords: DM, create
316 .seealso: DMPlexCreateBoxMesh(), DMPlexCreateSquareBoundary(), DMSetType(), DMCreate()
317 @*/
318 PetscErrorCode DMPlexCreateCubeBoundary(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt faces[])
319 {
320   PetscInt       vertices[3], numVertices;
321   PetscInt       numFaces    = 2*faces[0]*faces[1] + 2*faces[1]*faces[2] + 2*faces[0]*faces[2];
322   Vec            coordinates;
323   PetscSection   coordSection;
324   PetscScalar    *coords;
325   PetscInt       coordSize;
326   PetscMPIInt    rank;
327   PetscInt       v, vx, vy, vz;
328   PetscInt       voffset, iface=0, cone[4];
329   PetscErrorCode ierr;
330 
331   PetscFunctionBegin;
332   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");
333   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
334   vertices[0] = faces[0]+1; vertices[1] = faces[1]+1; vertices[2] = faces[2]+1;
335   numVertices = vertices[0]*vertices[1]*vertices[2];
336   if (!rank) {
337     PetscInt f;
338 
339     ierr = DMPlexSetChart(dm, 0, numFaces+numVertices);CHKERRQ(ierr);
340     for (f = 0; f < numFaces; ++f) {
341       ierr = DMPlexSetConeSize(dm, f, 4);CHKERRQ(ierr);
342     }
343     ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
344     for (v = 0; v < numFaces+numVertices; ++v) {
345       ierr = DMSetLabelValue(dm, "marker", v, 1);CHKERRQ(ierr);
346     }
347 
348     /* Side 0 (Top) */
349     for (vy = 0; vy < faces[1]; vy++) {
350       for (vx = 0; vx < faces[0]; vx++) {
351         voffset = numFaces + vertices[0]*vertices[1]*(vertices[2]-1) + vy*vertices[0] + vx;
352         cone[0] = voffset; cone[1] = voffset+1; cone[2] = voffset+vertices[0]+1; cone[3] = voffset+vertices[0];
353         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
354         iface++;
355       }
356     }
357 
358     /* Side 1 (Bottom) */
359     for (vy = 0; vy < faces[1]; vy++) {
360       for (vx = 0; vx < faces[0]; vx++) {
361         voffset = numFaces + vy*(faces[0]+1) + vx;
362         cone[0] = voffset+1; cone[1] = voffset; cone[2] = voffset+vertices[0]; cone[3] = voffset+vertices[0]+1;
363         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
364         iface++;
365       }
366     }
367 
368     /* Side 2 (Front) */
369     for (vz = 0; vz < faces[2]; vz++) {
370       for (vx = 0; vx < faces[0]; vx++) {
371         voffset = numFaces + vz*vertices[0]*vertices[1] + vx;
372         cone[0] = voffset; cone[1] = voffset+1; cone[2] = voffset+vertices[0]*vertices[1]+1; cone[3] = voffset+vertices[0]*vertices[1];
373         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
374         iface++;
375       }
376     }
377 
378     /* Side 3 (Back) */
379     for (vz = 0; vz < faces[2]; vz++) {
380       for (vx = 0; vx < faces[0]; vx++) {
381         voffset = numFaces + vz*vertices[0]*vertices[1] + vertices[0]*(vertices[1]-1) + vx;
382         cone[0] = voffset+vertices[0]*vertices[1]; cone[1] = voffset+vertices[0]*vertices[1]+1;
383         cone[2] = voffset+1; cone[3] = voffset;
384         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
385         iface++;
386       }
387     }
388 
389     /* Side 4 (Left) */
390     for (vz = 0; vz < faces[2]; vz++) {
391       for (vy = 0; vy < faces[1]; vy++) {
392         voffset = numFaces + vz*vertices[0]*vertices[1] + vy*vertices[0];
393         cone[0] = voffset; cone[1] = voffset+vertices[0]*vertices[1];
394         cone[2] = voffset+vertices[0]*vertices[1]+vertices[0]; cone[3] = voffset+vertices[0];
395         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
396         iface++;
397       }
398     }
399 
400     /* Side 5 (Right) */
401     for (vz = 0; vz < faces[2]; vz++) {
402       for (vy = 0; vy < faces[1]; vy++) {
403         voffset = numFaces + vz*vertices[0]*vertices[1] + vy*vertices[0] + vx;
404         cone[0] = voffset+vertices[0]*vertices[1]; cone[1] = voffset;
405         cone[2] = voffset+vertices[0]; cone[3] = voffset+vertices[0]*vertices[1]+vertices[0];
406         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
407         iface++;
408       }
409     }
410   }
411   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
412   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
413   /* Build coordinates */
414   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
415   ierr = PetscSectionSetChart(coordSection, numFaces, numFaces + numVertices);CHKERRQ(ierr);
416   for (v = numFaces; v < numFaces+numVertices; ++v) {
417     ierr = PetscSectionSetDof(coordSection, v, 3);CHKERRQ(ierr);
418   }
419   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
420   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
421   ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
422   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
423   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
424   ierr = VecSetBlockSize(coordinates, 3);CHKERRQ(ierr);
425   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
426   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
427   for (vz = 0; vz <= faces[2]; ++vz) {
428     for (vy = 0; vy <= faces[1]; ++vy) {
429       for (vx = 0; vx <= faces[0]; ++vx) {
430         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+0] = lower[0] + ((upper[0] - lower[0])/faces[0])*vx;
431         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+1] = lower[1] + ((upper[1] - lower[1])/faces[1])*vy;
432         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+2] = lower[2] + ((upper[2] - lower[2])/faces[2])*vz;
433       }
434     }
435   }
436   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
437   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
438   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
439   PetscFunctionReturn(0);
440 }
441 
442 #undef __FUNCT__
443 #define __FUNCT__ "DMPlexCreateCubeMesh_Internal"
444 static PetscErrorCode DMPlexCreateCubeMesh_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY, DMBoundaryType bdZ)
445 {
446   PetscInt       markerTop      = 1, faceMarkerTop      = 1;
447   PetscInt       markerBottom   = 1, faceMarkerBottom   = 1;
448   PetscInt       markerFront    = 1, faceMarkerFront    = 1;
449   PetscInt       markerBack     = 1, faceMarkerBack     = 1;
450   PetscInt       markerRight    = 1, faceMarkerRight    = 1;
451   PetscInt       markerLeft     = 1, faceMarkerLeft     = 1;
452   PetscInt       dim;
453   PetscBool      markerSeparate = PETSC_FALSE;
454   PetscMPIInt    rank;
455   PetscErrorCode ierr;
456 
457   PetscFunctionBegin;
458   ierr = DMGetDimension(dm,&dim);CHKERRQ(ierr);
459   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
460   ierr = DMCreateLabel(dm,"marker");CHKERRQ(ierr);
461   ierr = DMCreateLabel(dm,"Face Sets");CHKERRQ(ierr);
462   switch (dim) {
463   case 2:
464     faceMarkerTop    = 3;
465     faceMarkerBottom = 1;
466     faceMarkerRight  = 2;
467     faceMarkerLeft   = 4;
468     break;
469   case 3:
470     faceMarkerBottom = 1;
471     faceMarkerTop    = 2;
472     faceMarkerFront  = 3;
473     faceMarkerBack   = 4;
474     faceMarkerRight  = 5;
475     faceMarkerLeft   = 6;
476     break;
477   default:
478     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Dimension %d not supported",dim);
479     break;
480   }
481   ierr = PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);CHKERRQ(ierr);
482   if (markerSeparate) {
483     markerBottom = faceMarkerBottom;
484     markerTop    = faceMarkerTop;
485     markerFront  = faceMarkerFront;
486     markerBack   = faceMarkerBack;
487     markerRight  = faceMarkerRight;
488     markerLeft   = faceMarkerLeft;
489   }
490   {
491     const PetscInt numXEdges    = !rank ? edges[0] : 0;
492     const PetscInt numYEdges    = !rank ? edges[1] : 0;
493     const PetscInt numZEdges    = !rank ? edges[2] : 0;
494     const PetscInt numXVertices = !rank ? (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ? edges[0] : edges[0]+1) : 0;
495     const PetscInt numYVertices = !rank ? (bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ? edges[1] : edges[1]+1) : 0;
496     const PetscInt numZVertices = !rank ? (bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST ? edges[2] : edges[2]+1) : 0;
497     const PetscInt numCells     = numXEdges*numYEdges*numZEdges;
498     const PetscInt numXFaces    = numYEdges*numZEdges;
499     const PetscInt numYFaces    = numXEdges*numZEdges;
500     const PetscInt numZFaces    = numXEdges*numYEdges;
501     const PetscInt numTotXFaces = numXVertices*numXFaces;
502     const PetscInt numTotYFaces = numYVertices*numYFaces;
503     const PetscInt numTotZFaces = numZVertices*numZFaces;
504     const PetscInt numFaces     = numTotXFaces + numTotYFaces + numTotZFaces;
505     const PetscInt numTotXEdges = numXEdges*numYVertices*numZVertices;
506     const PetscInt numTotYEdges = numYEdges*numXVertices*numZVertices;
507     const PetscInt numTotZEdges = numZEdges*numXVertices*numYVertices;
508     const PetscInt numVertices  = numXVertices*numYVertices*numZVertices;
509     const PetscInt numEdges     = numTotXEdges + numTotYEdges + numTotZEdges;
510     const PetscInt firstVertex  = (dim == 2) ? numFaces : numCells;
511     const PetscInt firstXFace   = (dim == 2) ? 0 : numCells + numVertices;
512     const PetscInt firstYFace   = firstXFace + numTotXFaces;
513     const PetscInt firstZFace   = firstYFace + numTotYFaces;
514     const PetscInt firstXEdge   = numCells + numFaces + numVertices;
515     const PetscInt firstYEdge   = firstXEdge + numTotXEdges;
516     const PetscInt firstZEdge   = firstYEdge + numTotYEdges;
517     Vec            coordinates;
518     PetscSection   coordSection;
519     PetscScalar   *coords;
520     PetscInt       coordSize;
521     PetscInt       v, vx, vy, vz;
522     PetscInt       c, f, fx, fy, fz, e, ex, ey, ez;
523 
524     ierr = DMPlexSetChart(dm, 0, numCells+numFaces+numEdges+numVertices);CHKERRQ(ierr);
525     for (c = 0; c < numCells; c++) {
526       ierr = DMPlexSetConeSize(dm, c, 6);CHKERRQ(ierr);
527     }
528     for (f = firstXFace; f < firstXFace+numFaces; ++f) {
529       ierr = DMPlexSetConeSize(dm, f, 4);CHKERRQ(ierr);
530     }
531     for (e = firstXEdge; e < firstXEdge+numEdges; ++e) {
532       ierr = DMPlexSetConeSize(dm, e, 2);CHKERRQ(ierr);
533     }
534     ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
535     /* Build cells */
536     for (fz = 0; fz < numZEdges; ++fz) {
537       for (fy = 0; fy < numYEdges; ++fy) {
538         for (fx = 0; fx < numXEdges; ++fx) {
539           PetscInt cell    = (fz*numYEdges + fy)*numXEdges + fx;
540           PetscInt faceB   = firstZFace + (fy*numXEdges+fx)*numZVertices +   fz;
541           PetscInt faceT   = firstZFace + (fy*numXEdges+fx)*numZVertices + ((fz+1)%numZVertices);
542           PetscInt faceF   = firstYFace + (fz*numXEdges+fx)*numYVertices +   fy;
543           PetscInt faceK   = firstYFace + (fz*numXEdges+fx)*numYVertices + ((fy+1)%numYVertices);
544           PetscInt faceL   = firstXFace + (fz*numYEdges+fy)*numXVertices +   fx;
545           PetscInt faceR   = firstXFace + (fz*numYEdges+fy)*numXVertices + ((fx+1)%numXVertices);
546                             /* B,  T,  F,  K,  R,  L */
547           PetscInt ornt[6] = {-4,  0,  0, -1,  0, -4}; /* ??? */
548           PetscInt cone[6];
549 
550           /* no boundary twisting in 3D */
551           cone[0] = faceB; cone[1] = faceT; cone[2] = faceF; cone[3] = faceK; cone[4] = faceR; cone[5] = faceL;
552           ierr    = DMPlexSetCone(dm, cell, cone);CHKERRQ(ierr);
553           ierr    = DMPlexSetConeOrientation(dm, cell, ornt);CHKERRQ(ierr);
554         }
555       }
556     }
557     /* Build x faces */
558     for (fz = 0; fz < numZEdges; ++fz) {
559       for (fy = 0; fy < numYEdges; ++fy) {
560         for (fx = 0; fx < numXVertices; ++fx) {
561           PetscInt face    = firstXFace + (fz*numYEdges+fy)*numXVertices + fx;
562           PetscInt edgeL   = firstZEdge + (  fy*                 numXVertices+fx)*numZEdges + fz;
563           PetscInt edgeR   = firstZEdge + (((fy+1)%numYVertices)*numXVertices+fx)*numZEdges + fz;
564           PetscInt edgeB   = firstYEdge + (  fz*                 numXVertices+fx)*numYEdges + fy;
565           PetscInt edgeT   = firstYEdge + (((fz+1)%numZVertices)*numXVertices+fx)*numYEdges + fy;
566           PetscInt ornt[4] = {0, 0, -2, -2};
567           PetscInt cone[4];
568 
569           if (dim == 3) {
570             /* markers */
571             if (bdX != DM_BOUNDARY_PERIODIC) {
572               if (fx == numXVertices-1) {
573                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerRight);CHKERRQ(ierr);
574                 ierr = DMSetLabelValue(dm, "marker", face, markerRight);CHKERRQ(ierr);
575               }
576               else if (fx == 0) {
577                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerLeft);CHKERRQ(ierr);
578                 ierr = DMSetLabelValue(dm, "marker", face, markerLeft);CHKERRQ(ierr);
579               }
580             }
581           }
582           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
583           ierr    = DMPlexSetCone(dm, face, cone);CHKERRQ(ierr);
584           ierr    = DMPlexSetConeOrientation(dm, face, ornt);CHKERRQ(ierr);
585         }
586       }
587     }
588     /* Build y faces */
589     for (fz = 0; fz < numZEdges; ++fz) {
590       for (fx = 0; fx < numXEdges; ++fx) {
591         for (fy = 0; fy < numYVertices; ++fy) {
592           PetscInt face    = firstYFace + (fz*numXEdges+fx)*numYVertices + fy;
593           PetscInt edgeL   = firstZEdge + (fy*numXVertices+  fx                 )*numZEdges + fz;
594           PetscInt edgeR   = firstZEdge + (fy*numXVertices+((fx+1)%numXVertices))*numZEdges + fz;
595           PetscInt edgeB   = firstXEdge + (  fz                 *numYVertices+fy)*numXEdges + fx;
596           PetscInt edgeT   = firstXEdge + (((fz+1)%numZVertices)*numYVertices+fy)*numXEdges + fx;
597           PetscInt ornt[4] = {0, 0, -2, -2};
598           PetscInt cone[4];
599 
600           if (dim == 3) {
601             /* markers */
602             if (bdY != DM_BOUNDARY_PERIODIC) {
603               if (fy == numYVertices-1) {
604                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerBack);CHKERRQ(ierr);
605                 ierr = DMSetLabelValue(dm, "marker", face, markerBack);CHKERRQ(ierr);
606               }
607               else if (fy == 0) {
608                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerFront);CHKERRQ(ierr);
609                 ierr = DMSetLabelValue(dm, "marker", face, markerFront);CHKERRQ(ierr);
610               }
611             }
612           }
613           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
614           ierr    = DMPlexSetCone(dm, face, cone);CHKERRQ(ierr);
615           ierr    = DMPlexSetConeOrientation(dm, face, ornt);CHKERRQ(ierr);
616         }
617       }
618     }
619     /* Build z faces */
620     for (fy = 0; fy < numYEdges; ++fy) {
621       for (fx = 0; fx < numXEdges; ++fx) {
622         for (fz = 0; fz < numZVertices; fz++) {
623           PetscInt face    = firstZFace + (fy*numXEdges+fx)*numZVertices + fz;
624           PetscInt edgeL   = firstYEdge + (fz*numXVertices+  fx                 )*numYEdges + fy;
625           PetscInt edgeR   = firstYEdge + (fz*numXVertices+((fx+1)%numXVertices))*numYEdges + fy;
626           PetscInt edgeB   = firstXEdge + (fz*numYVertices+  fy                 )*numXEdges + fx;
627           PetscInt edgeT   = firstXEdge + (fz*numYVertices+((fy+1)%numYVertices))*numXEdges + fx;
628           PetscInt ornt[4] = {0, 0, -2, -2};
629           PetscInt cone[4];
630 
631           if (dim == 2) {
632             if (bdX == DM_BOUNDARY_TWIST && fx == numXEdges-1) {edgeR += numYEdges-1-2*fy; ornt[1] = -2;}
633             if (bdY == DM_BOUNDARY_TWIST && fy == numYEdges-1) {edgeT += numXEdges-1-2*fx; ornt[2] =  0;}
634           }
635           else {
636             /* markers */
637             if (bdZ != DM_BOUNDARY_PERIODIC) {
638               if (fz == numZVertices-1) {
639                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerTop);CHKERRQ(ierr);
640                 ierr = DMSetLabelValue(dm, "marker", face, markerTop);CHKERRQ(ierr);
641               }
642               else if (fz == 0) {
643                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerBottom);CHKERRQ(ierr);
644                 ierr = DMSetLabelValue(dm, "marker", face, markerBottom);CHKERRQ(ierr);
645               }
646             }
647           }
648           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
649           ierr    = DMPlexSetCone(dm, face, cone);CHKERRQ(ierr);
650           ierr    = DMPlexSetConeOrientation(dm, face, ornt);CHKERRQ(ierr);
651         }
652       }
653     }
654     /* Build Z edges*/
655     for (vy = 0; vy < numYVertices; vy++) {
656       for (vx = 0; vx < numXVertices; vx++) {
657         for (ez = 0; ez < numZEdges; ez++) {
658           const PetscInt edge    = firstZEdge  + (vy*numXVertices+vx)*numZEdges + ez;
659           const PetscInt vertexB = firstVertex + (  ez                 *numYVertices+vy)*numXVertices + vx;
660           const PetscInt vertexT = firstVertex + (((ez+1)%numZVertices)*numYVertices+vy)*numXVertices + vx;
661           PetscInt       cone[2];
662 
663           if (dim == 3) {
664             if (bdX != DM_BOUNDARY_PERIODIC) {
665               if (vx == numXVertices-1) {
666                 ierr = DMSetLabelValue(dm, "marker", edge, markerRight);CHKERRQ(ierr);
667               }
668               else if (vx == 0) {
669                 ierr = DMSetLabelValue(dm, "marker", edge, markerLeft);CHKERRQ(ierr);
670               }
671             }
672             if (bdY != DM_BOUNDARY_PERIODIC) {
673               if (vy == numYVertices-1) {
674                 ierr = DMSetLabelValue(dm, "marker", edge, markerBack);CHKERRQ(ierr);
675               }
676               else if (vy == 0) {
677                 ierr = DMSetLabelValue(dm, "marker", edge, markerFront);CHKERRQ(ierr);
678               }
679             }
680           }
681           cone[0] = vertexB; cone[1] = vertexT;
682           ierr = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr);
683         }
684       }
685     }
686     /* Build Y edges*/
687     for (vz = 0; vz < numZVertices; vz++) {
688       for (vx = 0; vx < numXVertices; vx++) {
689         for (ey = 0; ey < numYEdges; ey++) {
690           const PetscInt nextv   = (dim == 2 && bdY == DM_BOUNDARY_TWIST && ey == numYEdges-1) ? (numXVertices-vx-1) : (vz*numYVertices+((ey+1)%numYVertices))*numXVertices + vx;
691           const PetscInt edge    = firstYEdge  + (vz*numXVertices+vx)*numYEdges + ey;
692           const PetscInt vertexF = firstVertex + (vz*numYVertices+ey)*numXVertices + vx;
693           const PetscInt vertexK = firstVertex + nextv;
694           PetscInt       cone[2];
695 
696           cone[0] = vertexF; cone[1] = vertexK;
697           ierr = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr);
698           if (dim == 2) {
699             if ((bdX != DM_BOUNDARY_PERIODIC) && (bdX != DM_BOUNDARY_TWIST)) {
700               if (vx == numXVertices-1) {
701                 ierr = DMSetLabelValue(dm, "Face Sets", edge, faceMarkerRight);CHKERRQ(ierr);
702                 ierr = DMSetLabelValue(dm, "marker", edge,    markerRight);CHKERRQ(ierr);
703                 ierr = DMSetLabelValue(dm, "marker", cone[0], markerRight);CHKERRQ(ierr);
704                 if (ey == numYEdges-1) {
705                   ierr = DMSetLabelValue(dm, "marker", cone[1], markerRight);CHKERRQ(ierr);
706                 }
707               }
708               else if (vx == 0) {
709                 ierr = DMSetLabelValue(dm, "Face Sets", edge, faceMarkerLeft);CHKERRQ(ierr);
710                 ierr = DMSetLabelValue(dm, "marker", edge,    markerLeft);CHKERRQ(ierr);
711                 ierr = DMSetLabelValue(dm, "marker", cone[0], markerLeft);CHKERRQ(ierr);
712                 if (ey == numYEdges-1) {
713                   ierr = DMSetLabelValue(dm, "marker", cone[1], markerLeft);CHKERRQ(ierr);
714                 }
715               }
716             }
717           }
718           else {
719             if (bdX != DM_BOUNDARY_PERIODIC) {
720               if (vx == numXVertices-1) {
721                 ierr = DMSetLabelValue(dm, "marker", edge, markerRight);CHKERRQ(ierr);
722               }
723               else if (vx == 0) {
724                 ierr = DMSetLabelValue(dm, "marker", edge, markerLeft);CHKERRQ(ierr);
725               }
726             }
727             if (bdZ != DM_BOUNDARY_PERIODIC) {
728               if (vz == numZVertices-1) {
729                 ierr = DMSetLabelValue(dm, "marker", edge, markerTop);CHKERRQ(ierr);
730               }
731               else if (vz == 0) {
732                 ierr = DMSetLabelValue(dm, "marker", edge, markerBottom);CHKERRQ(ierr);
733               }
734             }
735           }
736         }
737       }
738     }
739     /* Build X edges*/
740     for (vz = 0; vz < numZVertices; vz++) {
741       for (vy = 0; vy < numYVertices; vy++) {
742         for (ex = 0; ex < numXEdges; ex++) {
743           const PetscInt nextv   = (dim == 2 && bdX == DM_BOUNDARY_TWIST && ex == numXEdges-1) ? (numYVertices-vy-1)*numXVertices : (vz*numYVertices+vy)*numXVertices + (ex+1)%numXVertices;
744           const PetscInt edge    = firstXEdge  + (vz*numYVertices+vy)*numXEdges + ex;
745           const PetscInt vertexL = firstVertex + (vz*numYVertices+vy)*numXVertices + ex;
746           const PetscInt vertexR = firstVertex + nextv;
747           PetscInt       cone[2];
748 
749           cone[0] = vertexL; cone[1] = vertexR;
750           ierr = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr);
751           if (dim == 2) {
752             if ((bdY != DM_BOUNDARY_PERIODIC) && (bdY != DM_BOUNDARY_TWIST)) {
753               if (vy == numYVertices-1) {
754                 ierr = DMSetLabelValue(dm, "Face Sets", edge, faceMarkerTop);CHKERRQ(ierr);
755                 ierr = DMSetLabelValue(dm, "marker", edge,    markerTop);CHKERRQ(ierr);
756                 ierr = DMSetLabelValue(dm, "marker", cone[0], markerTop);CHKERRQ(ierr);
757                 if (ex == numXEdges-1) {
758                   ierr = DMSetLabelValue(dm, "marker", cone[1], markerTop);CHKERRQ(ierr);
759                 }
760               }
761               else if (vy == 0) {
762                 ierr = DMSetLabelValue(dm, "Face Sets", edge, faceMarkerBottom);CHKERRQ(ierr);
763                 ierr = DMSetLabelValue(dm, "marker", edge,    markerBottom);CHKERRQ(ierr);
764                 ierr = DMSetLabelValue(dm, "marker", cone[0], markerBottom);CHKERRQ(ierr);
765                 if (ex == numXEdges-1) {
766                   ierr = DMSetLabelValue(dm, "marker", cone[1], markerBottom);CHKERRQ(ierr);
767                 }
768               }
769             }
770           }
771           else {
772             if (bdY != DM_BOUNDARY_PERIODIC) {
773               if (vy == numYVertices-1) {
774                 ierr = DMSetLabelValue(dm, "marker", edge, markerBack);CHKERRQ(ierr);
775               }
776               else if (vy == 0) {
777                 ierr = DMSetLabelValue(dm, "marker", edge, markerFront);CHKERRQ(ierr);
778               }
779             }
780             if (bdZ != DM_BOUNDARY_PERIODIC) {
781               if (vz == numZVertices-1) {
782                 ierr = DMSetLabelValue(dm, "marker", edge, markerTop);CHKERRQ(ierr);
783               }
784               else if (vz == 0) {
785                 ierr = DMSetLabelValue(dm, "marker", edge, markerBottom);CHKERRQ(ierr);
786               }
787             }
788           }
789         }
790       }
791     }
792     ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
793     ierr = DMPlexStratify(dm);CHKERRQ(ierr);
794     /* Build coordinates */
795     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
796     ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
797     ierr = PetscSectionSetFieldComponents(coordSection, 0, dim);CHKERRQ(ierr);
798     ierr = PetscSectionSetChart(coordSection, firstVertex, firstVertex+numVertices);CHKERRQ(ierr);
799     for (v = firstVertex; v < firstVertex+numVertices; ++v) {
800       ierr = PetscSectionSetDof(coordSection, v, dim);CHKERRQ(ierr);
801       ierr = PetscSectionSetFieldDof(coordSection, v, 0, dim);CHKERRQ(ierr);
802     }
803     ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
804     ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
805     ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
806     ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
807     ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
808     ierr = VecSetBlockSize(coordinates, dim);CHKERRQ(ierr);
809     ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
810     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
811     for (vz = 0; vz < numZVertices; ++vz) {
812       for (vy = 0; vy < numYVertices; ++vy) {
813         for (vx = 0; vx < numXVertices; ++vx) {
814           coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+0] = lower[0] + ((upper[0] - lower[0])/numXEdges)*vx;
815           coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+1] = lower[1] + ((upper[1] - lower[1])/numYEdges)*vy;
816           if (dim == 3) {
817             coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+2] = lower[2] + ((upper[2] - lower[2])/numZEdges)*vz;
818           }
819         }
820       }
821     }
822     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
823     ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
824     ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
825   }
826   PetscFunctionReturn(0);
827 }
828 
829 #undef __FUNCT__
830 #define __FUNCT__ "DMPlexCreateSquareMesh"
831 /*@
832   DMPlexCreateSquareMesh - Creates a 2D mesh for a square lattice.
833 
834   Collective on MPI_Comm
835 
836   Input Parameters:
837 + comm  - The communicator for the DM object
838 . lower - The lower left corner coordinates
839 . upper - The upper right corner coordinates
840 . edges - The number of cells in each direction
841 . bdX   - The boundary type for the X direction
842 - bdY   - The boundary type for the Y direction
843 
844   Output Parameter:
845 . dm  - The DM object
846 
847   Note: Here is the numbering returned for 2 cells in each direction:
848 $ 22--8-23--9--24
849 $  |     |     |
850 $ 13  2 14  3  15
851 $  |     |     |
852 $ 19--6-20--7--21
853 $  |     |     |
854 $ 10  0 11  1 12
855 $  |     |     |
856 $ 16--4-17--5--18
857 
858   Level: beginner
859 
860 .keywords: DM, create
861 .seealso: DMPlexCreateBoxMesh(), DMPlexCreateSquareBoundary(), DMPlexCreateCubeBoundary(), DMSetType(), DMCreate()
862 @*/
863 PetscErrorCode DMPlexCreateSquareMesh(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY)
864 {
865   PetscReal      lower3[3], upper3[3];
866   PetscInt       edges3[3];
867   PetscErrorCode ierr;
868 
869   PetscFunctionBegin;
870   lower3[0] = lower[0]; lower3[1] = lower[1]; lower3[2] = 0.;
871   upper3[0] = upper[0]; upper3[1] = upper[1]; upper3[2] = 0.;
872   edges3[0] = edges[0]; edges3[1] = edges[1]; edges3[2] = 0;
873   ierr = DMPlexCreateCubeMesh_Internal(dm, lower3, upper3, edges3, bdX, bdY, DM_BOUNDARY_NONE);CHKERRQ(ierr);
874   PetscFunctionReturn(0);
875 }
876 
877 #undef __FUNCT__
878 #define __FUNCT__ "DMPlexCreateBoxMesh"
879 /*@
880   DMPlexCreateBoxMesh - Creates a mesh on the tensor product of unit intervals (box) using simplices.
881 
882   Collective on MPI_Comm
883 
884   Input Parameters:
885 + comm - The communicator for the DM object
886 . dim - The spatial dimension
887 . numFaces - Number of faces per dimension
888 - interpolate - Flag to create intermediate mesh pieces (edges, faces)
889 
890   Output Parameter:
891 . dm  - The DM object
892 
893   Level: beginner
894 
895 .keywords: DM, create
896 .seealso: DMPlexCreateHexBoxMesh(), DMSetType(), DMCreate()
897 @*/
898 PetscErrorCode DMPlexCreateBoxMesh(MPI_Comm comm, PetscInt dim, PetscInt numFaces, PetscBool interpolate, DM *dm)
899 {
900   DM             boundary;
901   PetscErrorCode ierr;
902 
903   PetscFunctionBegin;
904   PetscValidPointer(dm, 4);
905   ierr = DMCreate(comm, &boundary);CHKERRQ(ierr);
906   PetscValidLogicalCollectiveInt(boundary,dim,2);
907   ierr = DMSetType(boundary, DMPLEX);CHKERRQ(ierr);
908   ierr = DMSetDimension(boundary, dim-1);CHKERRQ(ierr);
909   ierr = DMSetCoordinateDim(boundary, dim);CHKERRQ(ierr);
910   switch (dim) {
911   case 2:
912   {
913     PetscReal lower[2] = {0.0, 0.0};
914     PetscReal upper[2] = {1.0, 1.0};
915     PetscInt  edges[2];
916 
917     edges[0] = numFaces; edges[1] = numFaces;
918     ierr = DMPlexCreateSquareBoundary(boundary, lower, upper, edges);CHKERRQ(ierr);
919     break;
920   }
921   case 3:
922   {
923     PetscReal lower[3] = {0.0, 0.0, 0.0};
924     PetscReal upper[3] = {1.0, 1.0, 1.0};
925     PetscInt  faces[3];
926 
927     faces[0] = numFaces; faces[1] = numFaces; faces[2] = numFaces;
928     ierr = DMPlexCreateCubeBoundary(boundary, lower, upper, faces);CHKERRQ(ierr);
929     break;
930   }
931   default:
932     SETERRQ1(comm, PETSC_ERR_SUP, "Dimension not supported: %d", dim);
933   }
934   ierr = DMPlexGenerate(boundary, NULL, interpolate, dm);CHKERRQ(ierr);
935   ierr = DMDestroy(&boundary);CHKERRQ(ierr);
936   PetscFunctionReturn(0);
937 }
938 
939 #undef __FUNCT__
940 #define __FUNCT__ "DMPlexCreateHexBoxMesh"
941 /*@
942   DMPlexCreateHexBoxMesh - Creates a mesh on the tensor product of unit intervals (box) using hexahedra.
943 
944   Collective on MPI_Comm
945 
946   Input Parameters:
947 + comm  - The communicator for the DM object
948 . dim   - The spatial dimension
949 . periodicX - The boundary type for the X direction
950 . periodicY - The boundary type for the Y direction
951 . periodicZ - The boundary type for the Z direction
952 - cells - The number of cells in each direction
953 
954   Output Parameter:
955 . dm  - The DM object
956 
957   Level: beginner
958 
959 .keywords: DM, create
960 .seealso: DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
961 @*/
962 PetscErrorCode DMPlexCreateHexBoxMesh(MPI_Comm comm, PetscInt dim, const PetscInt cells[], DMBoundaryType periodicX, DMBoundaryType periodicY, DMBoundaryType periodicZ, DM *dm)
963 {
964   PetscInt       i;
965   PetscErrorCode ierr;
966 
967   PetscFunctionBegin;
968   PetscValidPointer(dm, 7);
969   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
970   PetscValidLogicalCollectiveInt(*dm,dim,2);
971   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
972   ierr = DMSetDimension(*dm, dim);CHKERRQ(ierr);
973   switch (dim) {
974   case 2:
975   {
976     PetscReal lower[2] = {0.0, 0.0};
977     PetscReal upper[2] = {1.0, 1.0};
978 
979     ierr = DMPlexCreateSquareMesh(*dm, lower, upper, cells, periodicX, periodicY);CHKERRQ(ierr);
980     if (periodicX == DM_BOUNDARY_PERIODIC || periodicX == DM_BOUNDARY_TWIST ||
981         periodicY == DM_BOUNDARY_PERIODIC || periodicY == DM_BOUNDARY_TWIST) {
982       PetscReal      L[2];
983       PetscReal      maxCell[2];
984       DMBoundaryType bdType[2] = {periodicX, periodicY};
985 
986       for (i = 0; i < dim; i++) {
987         L[i]       = upper[i] - lower[i];
988         maxCell[i] = 1.1 * (L[i] / cells[i]);
989       }
990 
991       ierr = DMSetPeriodicity(*dm,maxCell,L,bdType);CHKERRQ(ierr);
992     }
993     break;
994   }
995   case 3:
996   {
997     PetscReal lower[3] = {0.0, 0.0, 0.0};
998     PetscReal upper[3] = {1.0, 1.0, 1.0};
999 
1000     ierr = DMPlexCreateCubeMesh_Internal(*dm, lower, upper, cells, periodicX, periodicY, periodicZ);CHKERRQ(ierr);
1001     if (periodicX == DM_BOUNDARY_PERIODIC || periodicX == DM_BOUNDARY_TWIST ||
1002         periodicY == DM_BOUNDARY_PERIODIC || periodicY == DM_BOUNDARY_TWIST ||
1003         periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
1004       PetscReal      L[3];
1005       PetscReal      maxCell[3];
1006       DMBoundaryType bdType[3];
1007 
1008       bdType[0] = periodicX;
1009       bdType[1] = periodicY;
1010       bdType[2] = periodicZ;
1011       for (i = 0; i < dim; i++) {
1012         L[i]       = upper[i] - lower[i];
1013         maxCell[i] = 1.1 * (L[i] / cells[i]);
1014       }
1015 
1016       ierr = DMSetPeriodicity(*dm,maxCell,L,bdType);CHKERRQ(ierr);
1017     }
1018     break;
1019   }
1020   default:
1021     SETERRQ1(comm, PETSC_ERR_SUP, "Dimension not supported: %d", dim);
1022   }
1023   PetscFunctionReturn(0);
1024 }
1025 
1026 /* External function declarations here */
1027 extern PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling);
1028 extern PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat);
1029 extern PetscErrorCode DMCreateDefaultSection_Plex(DM dm);
1030 extern PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm);
1031 extern PetscErrorCode DMCreateMatrix_Plex(DM dm,  Mat *J);
1032 extern PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm);
1033 extern PetscErrorCode DMRefine_Plex(DM dm, MPI_Comm comm, DM *dmRefined);
1034 extern PetscErrorCode DMCoarsen_Plex(DM dm, MPI_Comm comm, DM *dmCoarsened);
1035 extern PetscErrorCode DMAdaptLabel_Plex(DM dm, DMLabel adaptLabel, DM *dmRefined);
1036 extern PetscErrorCode DMRefineHierarchy_Plex(DM dm, PetscInt nlevels, DM dmRefined[]);
1037 extern PetscErrorCode DMCoarsenHierarchy_Plex(DM dm, PetscInt nlevels, DM dmCoarsened[]);
1038 extern PetscErrorCode DMClone_Plex(DM dm, DM *newdm);
1039 extern PetscErrorCode DMSetUp_Plex(DM dm);
1040 extern PetscErrorCode DMDestroy_Plex(DM dm);
1041 extern PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer);
1042 extern PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer);
1043 extern PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm);
1044 extern PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, DMPointLocationType ltype, PetscSF cellSF);
1045 extern PetscErrorCode DMProjectFunctionLocal_Plex(DM,PetscReal,PetscErrorCode(**)(PetscInt,PetscReal,const PetscReal[],PetscInt,PetscScalar *,void *),void **,InsertMode,Vec);
1046 extern PetscErrorCode DMProjectFunctionLabelLocal_Plex(DM,PetscReal,DMLabel,PetscInt,const PetscInt[],PetscErrorCode(**)(PetscInt,PetscReal,const PetscReal[],PetscInt,PetscScalar *,void *),void **,InsertMode,Vec);
1047 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);
1048 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);
1049 extern PetscErrorCode DMComputeL2Diff_Plex(DM,PetscReal,PetscErrorCode(**)(PetscInt,PetscReal,const PetscReal[],PetscInt,PetscScalar *,void *),void **,Vec,PetscReal *);
1050 extern PetscErrorCode DMComputeL2GradientDiff_Plex(DM,PetscReal,PetscErrorCode(**)(PetscInt,PetscReal,const PetscReal[], const PetscReal[],PetscInt,PetscScalar *,void *),void **,Vec,const PetscReal [],PetscReal *);
1051 extern PetscErrorCode DMComputeL2FieldDiff_Plex(DM,PetscReal,PetscErrorCode(**)(PetscInt,PetscReal,const PetscReal[],PetscInt,PetscScalar *,void *),void **,Vec,PetscReal *);
1052 
1053 #undef __FUNCT__
1054 #define __FUNCT__ "DMPlexReplace_Static"
1055 /* Replace dm with the contents of dmNew
1056    - Share the DM_Plex structure
1057    - Share the coordinates
1058    - Share the SF
1059 */
1060 static PetscErrorCode DMPlexReplace_Static(DM dm, DM dmNew)
1061 {
1062   PetscSF          sf;
1063   DM               coordDM, coarseDM;
1064   Vec              coords;
1065   const PetscReal *maxCell, *L;
1066   const DMBoundaryType *bd;
1067   PetscErrorCode   ierr;
1068 
1069   PetscFunctionBegin;
1070   ierr = DMGetPointSF(dmNew, &sf);CHKERRQ(ierr);
1071   ierr = DMSetPointSF(dm, sf);CHKERRQ(ierr);
1072   ierr = DMGetCoordinateDM(dmNew, &coordDM);CHKERRQ(ierr);
1073   ierr = DMGetCoordinatesLocal(dmNew, &coords);CHKERRQ(ierr);
1074   ierr = DMSetCoordinateDM(dm, coordDM);CHKERRQ(ierr);
1075   ierr = DMSetCoordinatesLocal(dm, coords);CHKERRQ(ierr);
1076   ierr = DMGetPeriodicity(dm, &maxCell, &L, &bd);CHKERRQ(ierr);
1077   if (L) {ierr = DMSetPeriodicity(dmNew, maxCell, L, bd);CHKERRQ(ierr);}
1078   ierr = DMDestroy_Plex(dm);CHKERRQ(ierr);
1079   dm->data = dmNew->data;
1080   ((DM_Plex *) dmNew->data)->refct++;
1081   dmNew->labels->refct++;
1082   if (!--(dm->labels->refct)) {
1083     DMLabelLink next = dm->labels->next;
1084 
1085     /* destroy the labels */
1086     while (next) {
1087       DMLabelLink tmp = next->next;
1088 
1089       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
1090       ierr = PetscFree(next);CHKERRQ(ierr);
1091       next = tmp;
1092     }
1093     ierr = PetscFree(dm->labels);CHKERRQ(ierr);
1094   }
1095   dm->labels = dmNew->labels;
1096   dm->depthLabel = dmNew->depthLabel;
1097   ierr = DMGetCoarseDM(dmNew,&coarseDM);CHKERRQ(ierr);
1098   ierr = DMSetCoarseDM(dm,coarseDM);CHKERRQ(ierr);
1099   PetscFunctionReturn(0);
1100 }
1101 
1102 #undef __FUNCT__
1103 #define __FUNCT__ "DMPlexSwap_Static"
1104 /* Swap dm with the contents of dmNew
1105    - Swap the DM_Plex structure
1106    - Swap the coordinates
1107    - Swap the point PetscSF
1108 */
1109 static PetscErrorCode DMPlexSwap_Static(DM dmA, DM dmB)
1110 {
1111   DM              coordDMA, coordDMB;
1112   Vec             coordsA,  coordsB;
1113   PetscSF         sfA,      sfB;
1114   void            *tmp;
1115   DMLabelLinkList listTmp;
1116   DMLabel         depthTmp;
1117   PetscErrorCode  ierr;
1118 
1119   PetscFunctionBegin;
1120   ierr = DMGetPointSF(dmA, &sfA);CHKERRQ(ierr);
1121   ierr = DMGetPointSF(dmB, &sfB);CHKERRQ(ierr);
1122   ierr = PetscObjectReference((PetscObject) sfA);CHKERRQ(ierr);
1123   ierr = DMSetPointSF(dmA, sfB);CHKERRQ(ierr);
1124   ierr = DMSetPointSF(dmB, sfA);CHKERRQ(ierr);
1125   ierr = PetscObjectDereference((PetscObject) sfA);CHKERRQ(ierr);
1126 
1127   ierr = DMGetCoordinateDM(dmA, &coordDMA);CHKERRQ(ierr);
1128   ierr = DMGetCoordinateDM(dmB, &coordDMB);CHKERRQ(ierr);
1129   ierr = PetscObjectReference((PetscObject) coordDMA);CHKERRQ(ierr);
1130   ierr = DMSetCoordinateDM(dmA, coordDMB);CHKERRQ(ierr);
1131   ierr = DMSetCoordinateDM(dmB, coordDMA);CHKERRQ(ierr);
1132   ierr = PetscObjectDereference((PetscObject) coordDMA);CHKERRQ(ierr);
1133 
1134   ierr = DMGetCoordinatesLocal(dmA, &coordsA);CHKERRQ(ierr);
1135   ierr = DMGetCoordinatesLocal(dmB, &coordsB);CHKERRQ(ierr);
1136   ierr = PetscObjectReference((PetscObject) coordsA);CHKERRQ(ierr);
1137   ierr = DMSetCoordinatesLocal(dmA, coordsB);CHKERRQ(ierr);
1138   ierr = DMSetCoordinatesLocal(dmB, coordsA);CHKERRQ(ierr);
1139   ierr = PetscObjectDereference((PetscObject) coordsA);CHKERRQ(ierr);
1140 
1141   tmp       = dmA->data;
1142   dmA->data = dmB->data;
1143   dmB->data = tmp;
1144   listTmp   = dmA->labels;
1145   dmA->labels = dmB->labels;
1146   dmB->labels = listTmp;
1147   depthTmp  = dmA->depthLabel;
1148   dmA->depthLabel = dmB->depthLabel;
1149   dmB->depthLabel = depthTmp;
1150   PetscFunctionReturn(0);
1151 }
1152 
1153 #undef __FUNCT__
1154 #define __FUNCT__ "DMSetFromOptions_NonRefinement_Plex"
1155 PetscErrorCode  DMSetFromOptions_NonRefinement_Plex(PetscOptionItems *PetscOptionsObject,DM dm)
1156 {
1157   DM_Plex       *mesh = (DM_Plex*) dm->data;
1158   PetscErrorCode ierr;
1159 
1160   PetscFunctionBegin;
1161   /* Handle viewing */
1162   ierr = PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMView", PETSC_FALSE, &mesh->printSetValues, NULL);CHKERRQ(ierr);
1163   ierr = PetscOptionsInt("-dm_plex_print_fem", "Debug output level all fem computations", "DMView", 0, &mesh->printFEM, NULL);CHKERRQ(ierr);
1164   ierr = PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMView", mesh->printTol, &mesh->printTol, NULL);CHKERRQ(ierr);
1165   /* Point Location */
1166   ierr = PetscOptionsBool("-dm_plex_hash_location", "Use grid hashing for point location", "DMView", PETSC_FALSE, &mesh->useHashLocation, NULL);CHKERRQ(ierr);
1167   /* Generation and remeshing */
1168   ierr = PetscOptionsBool("-dm_plex_remesh_bd", "Allow changes to the boundary on remeshing", "DMView", PETSC_FALSE, &mesh->remeshBd, NULL);CHKERRQ(ierr);
1169   /* Projection behavior */
1170   ierr = PetscOptionsInt("-dm_plex_max_projection_height", "Maxmimum mesh point height used to project locally", "DMPlexSetMaxProjectionHeight", 0, &mesh->maxProjectionHeight, NULL);CHKERRQ(ierr);
1171   ierr = PetscOptionsBool("-dm_plex_regular_refinement", "Use special nested projection algorithm for regular refinement", "DMPlexSetRegularRefinement", mesh->regularRefinement, &mesh->regularRefinement, NULL);CHKERRQ(ierr);
1172   PetscFunctionReturn(0);
1173 }
1174 
1175 #undef __FUNCT__
1176 #define __FUNCT__ "DMSetFromOptions_Plex"
1177 PetscErrorCode  DMSetFromOptions_Plex(PetscOptionItems *PetscOptionsObject,DM dm)
1178 {
1179   PetscInt       refine = 0, coarsen = 0, r;
1180   PetscBool      isHierarchy;
1181   PetscErrorCode ierr;
1182 
1183   PetscFunctionBegin;
1184   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1185   ierr = PetscOptionsHead(PetscOptionsObject,"DMPlex Options");CHKERRQ(ierr);
1186   /* Handle DMPlex refinement */
1187   ierr = PetscOptionsInt("-dm_refine", "The number of uniform refinements", "DMCreate", refine, &refine, NULL);CHKERRQ(ierr);
1188   ierr = PetscOptionsInt("-dm_refine_hierarchy", "The number of uniform refinements", "DMCreate", refine, &refine, &isHierarchy);CHKERRQ(ierr);
1189   if (refine) {ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE);CHKERRQ(ierr);}
1190   if (refine && isHierarchy) {
1191     DM *dms, coarseDM;
1192 
1193     ierr = DMGetCoarseDM(dm, &coarseDM);CHKERRQ(ierr);
1194     ierr = PetscObjectReference((PetscObject)coarseDM);CHKERRQ(ierr);
1195     ierr = PetscMalloc1(refine,&dms);CHKERRQ(ierr);
1196     ierr = DMRefineHierarchy(dm, refine, dms);CHKERRQ(ierr);
1197     /* Total hack since we do not pass in a pointer */
1198     ierr = DMPlexSwap_Static(dm, dms[refine-1]);CHKERRQ(ierr);
1199     if (refine == 1) {
1200       ierr = DMSetCoarseDM(dm, dms[0]);CHKERRQ(ierr);
1201       ierr = DMPlexSetRegularRefinement(dm, PETSC_TRUE);CHKERRQ(ierr);
1202     } else {
1203       ierr = DMSetCoarseDM(dm, dms[refine-2]);CHKERRQ(ierr);
1204       ierr = DMPlexSetRegularRefinement(dm, PETSC_TRUE);CHKERRQ(ierr);
1205       ierr = DMSetCoarseDM(dms[0], dms[refine-1]);CHKERRQ(ierr);
1206       ierr = DMPlexSetRegularRefinement(dms[0], PETSC_TRUE);CHKERRQ(ierr);
1207     }
1208     ierr = DMSetCoarseDM(dms[refine-1], coarseDM);CHKERRQ(ierr);
1209     ierr = PetscObjectDereference((PetscObject)coarseDM);CHKERRQ(ierr);
1210     /* Free DMs */
1211     for (r = 0; r < refine; ++r) {
1212       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]);CHKERRQ(ierr);
1213       ierr = DMDestroy(&dms[r]);CHKERRQ(ierr);
1214     }
1215     ierr = PetscFree(dms);CHKERRQ(ierr);
1216   } else {
1217     for (r = 0; r < refine; ++r) {
1218       DM refinedMesh;
1219 
1220       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
1221       ierr = DMRefine(dm, PetscObjectComm((PetscObject) dm), &refinedMesh);CHKERRQ(ierr);
1222       /* Total hack since we do not pass in a pointer */
1223       ierr = DMPlexReplace_Static(dm, refinedMesh);CHKERRQ(ierr);
1224       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
1225       ierr = DMDestroy(&refinedMesh);CHKERRQ(ierr);
1226     }
1227   }
1228   /* Handle DMPlex coarsening */
1229   ierr = PetscOptionsInt("-dm_coarsen", "Coarsen the mesh", "DMCreate", coarsen, &coarsen, NULL);CHKERRQ(ierr);
1230   ierr = PetscOptionsInt("-dm_coarsen_hierarchy", "The number of coarsenings", "DMCreate", coarsen, &coarsen, &isHierarchy);CHKERRQ(ierr);
1231   if (coarsen && isHierarchy) {
1232     DM *dms;
1233 
1234     ierr = PetscMalloc1(coarsen, &dms);CHKERRQ(ierr);
1235     ierr = DMCoarsenHierarchy(dm, coarsen, dms);CHKERRQ(ierr);
1236     /* Free DMs */
1237     for (r = 0; r < coarsen; ++r) {
1238       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]);CHKERRQ(ierr);
1239       ierr = DMDestroy(&dms[r]);CHKERRQ(ierr);
1240     }
1241     ierr = PetscFree(dms);CHKERRQ(ierr);
1242   } else {
1243     for (r = 0; r < coarsen; ++r) {
1244       DM coarseMesh;
1245 
1246       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
1247       ierr = DMCoarsen(dm, PetscObjectComm((PetscObject) dm), &coarseMesh);CHKERRQ(ierr);
1248       /* Total hack since we do not pass in a pointer */
1249       ierr = DMPlexReplace_Static(dm, coarseMesh);CHKERRQ(ierr);
1250       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
1251       ierr = DMDestroy(&coarseMesh);CHKERRQ(ierr);
1252     }
1253   }
1254   /* Handle */
1255   ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
1256   ierr = PetscOptionsTail();CHKERRQ(ierr);
1257   PetscFunctionReturn(0);
1258 }
1259 
1260 #undef __FUNCT__
1261 #define __FUNCT__ "DMCreateGlobalVector_Plex"
1262 static PetscErrorCode DMCreateGlobalVector_Plex(DM dm,Vec *vec)
1263 {
1264   PetscErrorCode ierr;
1265 
1266   PetscFunctionBegin;
1267   ierr = DMCreateGlobalVector_Section_Private(dm,vec);CHKERRQ(ierr);
1268   /* ierr = VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM);CHKERRQ(ierr); */
1269   ierr = VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex);CHKERRQ(ierr);
1270   ierr = VecSetOperation(*vec, VECOP_VIEWNATIVE, (void (*)(void)) VecView_Plex_Native);CHKERRQ(ierr);
1271   ierr = VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex);CHKERRQ(ierr);
1272   ierr = VecSetOperation(*vec, VECOP_LOADNATIVE, (void (*)(void)) VecLoad_Plex_Native);CHKERRQ(ierr);
1273   PetscFunctionReturn(0);
1274 }
1275 
1276 #undef __FUNCT__
1277 #define __FUNCT__ "DMCreateLocalVector_Plex"
1278 static PetscErrorCode DMCreateLocalVector_Plex(DM dm,Vec *vec)
1279 {
1280   PetscErrorCode ierr;
1281 
1282   PetscFunctionBegin;
1283   ierr = DMCreateLocalVector_Section_Private(dm,vec);CHKERRQ(ierr);
1284   ierr = VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex_Local);CHKERRQ(ierr);
1285   ierr = VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex_Local);CHKERRQ(ierr);
1286   PetscFunctionReturn(0);
1287 }
1288 
1289 #undef __FUNCT__
1290 #define __FUNCT__ "DMGetDimPoints_Plex"
1291 static PetscErrorCode DMGetDimPoints_Plex(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
1292 {
1293   PetscInt       depth, d;
1294   PetscErrorCode ierr;
1295 
1296   PetscFunctionBegin;
1297   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1298   if (depth == 1) {
1299     ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
1300     if (dim == 0)      {ierr = DMPlexGetDepthStratum(dm, dim, pStart, pEnd);CHKERRQ(ierr);}
1301     else if (dim == d) {ierr = DMPlexGetDepthStratum(dm, 1, pStart, pEnd);CHKERRQ(ierr);}
1302     else               {*pStart = 0; *pEnd = 0;}
1303   } else {
1304     ierr = DMPlexGetDepthStratum(dm, dim, pStart, pEnd);CHKERRQ(ierr);
1305   }
1306   PetscFunctionReturn(0);
1307 }
1308 
1309 #undef __FUNCT__
1310 #define __FUNCT__ "DMGetNeighors_Plex"
1311 static PetscErrorCode DMGetNeighors_Plex(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
1312 {
1313   PetscSF        sf;
1314   PetscErrorCode ierr;
1315 
1316   PetscFunctionBegin;
1317   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
1318   ierr = PetscSFGetRanks(sf, nranks, ranks, NULL, NULL, NULL);CHKERRQ(ierr);
1319   PetscFunctionReturn(0);
1320 }
1321 
1322 #undef __FUNCT__
1323 #define __FUNCT__ "DMInitialize_Plex"
1324 PetscErrorCode DMInitialize_Plex(DM dm)
1325 {
1326   PetscErrorCode ierr;
1327 
1328   PetscFunctionBegin;
1329   dm->ops->view                            = DMView_Plex;
1330   dm->ops->load                            = DMLoad_Plex;
1331   dm->ops->setfromoptions                  = DMSetFromOptions_Plex;
1332   dm->ops->clone                           = DMClone_Plex;
1333   dm->ops->setup                           = DMSetUp_Plex;
1334   dm->ops->createdefaultsection            = DMCreateDefaultSection_Plex;
1335   dm->ops->createdefaultconstraints        = DMCreateDefaultConstraints_Plex;
1336   dm->ops->createglobalvector              = DMCreateGlobalVector_Plex;
1337   dm->ops->createlocalvector               = DMCreateLocalVector_Plex;
1338   dm->ops->getlocaltoglobalmapping         = NULL;
1339   dm->ops->createfieldis                   = NULL;
1340   dm->ops->createcoordinatedm              = DMCreateCoordinateDM_Plex;
1341   dm->ops->getcoloring                     = NULL;
1342   dm->ops->creatematrix                    = DMCreateMatrix_Plex;
1343   dm->ops->createinterpolation             = DMCreateInterpolation_Plex;
1344   dm->ops->getaggregates                   = NULL;
1345   dm->ops->getinjection                    = DMCreateInjection_Plex;
1346   dm->ops->refine                          = DMRefine_Plex;
1347   dm->ops->coarsen                         = DMCoarsen_Plex;
1348   dm->ops->refinehierarchy                 = DMRefineHierarchy_Plex;
1349   dm->ops->coarsenhierarchy                = DMCoarsenHierarchy_Plex;
1350   dm->ops->globaltolocalbegin              = NULL;
1351   dm->ops->globaltolocalend                = NULL;
1352   dm->ops->localtoglobalbegin              = NULL;
1353   dm->ops->localtoglobalend                = NULL;
1354   dm->ops->destroy                         = DMDestroy_Plex;
1355   dm->ops->createsubdm                     = DMCreateSubDM_Plex;
1356   dm->ops->getdimpoints                    = DMGetDimPoints_Plex;
1357   dm->ops->locatepoints                    = DMLocatePoints_Plex;
1358   dm->ops->projectfunctionlocal            = DMProjectFunctionLocal_Plex;
1359   dm->ops->projectfunctionlabellocal       = DMProjectFunctionLabelLocal_Plex;
1360   dm->ops->projectfieldlocal               = DMProjectFieldLocal_Plex;
1361   dm->ops->projectfieldlabellocal          = DMProjectFieldLabelLocal_Plex;
1362   dm->ops->computel2diff                   = DMComputeL2Diff_Plex;
1363   dm->ops->computel2gradientdiff           = DMComputeL2GradientDiff_Plex;
1364   dm->ops->computel2fielddiff              = DMComputeL2FieldDiff_Plex;
1365   dm->ops->getneighbors                    = DMGetNeighors_Plex;
1366   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMAdaptLabel_C",DMAdaptLabel_Plex);CHKERRQ(ierr);
1367   PetscFunctionReturn(0);
1368 }
1369 
1370 #undef __FUNCT__
1371 #define __FUNCT__ "DMClone_Plex"
1372 PetscErrorCode DMClone_Plex(DM dm, DM *newdm)
1373 {
1374   DM_Plex        *mesh = (DM_Plex *) dm->data;
1375   PetscErrorCode ierr;
1376 
1377   PetscFunctionBegin;
1378   mesh->refct++;
1379   (*newdm)->data = mesh;
1380   ierr = PetscObjectChangeTypeName((PetscObject) *newdm, DMPLEX);CHKERRQ(ierr);
1381   ierr = DMInitialize_Plex(*newdm);CHKERRQ(ierr);
1382   PetscFunctionReturn(0);
1383 }
1384 
1385 /*MC
1386   DMPLEX = "plex" - A DM object that encapsulates an unstructured mesh, or CW Complex, which can be expressed using a Hasse Diagram.
1387                     In the local representation, Vecs contain all unknowns in the interior and shared boundary. This is
1388                     specified by a PetscSection object. Ownership in the global representation is determined by
1389                     ownership of the underlying DMPlex points. This is specified by another PetscSection object.
1390 
1391   Level: intermediate
1392 
1393 .seealso: DMType, DMPlexCreate(), DMCreate(), DMSetType()
1394 M*/
1395 
1396 #undef __FUNCT__
1397 #define __FUNCT__ "DMCreate_Plex"
1398 PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm)
1399 {
1400   DM_Plex        *mesh;
1401   PetscInt       unit, d;
1402   PetscErrorCode ierr;
1403 
1404   PetscFunctionBegin;
1405   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1406   ierr     = PetscNewLog(dm,&mesh);CHKERRQ(ierr);
1407   dm->dim  = 0;
1408   dm->data = mesh;
1409 
1410   mesh->refct             = 1;
1411   ierr                    = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection);CHKERRQ(ierr);
1412   mesh->maxConeSize       = 0;
1413   mesh->cones             = NULL;
1414   mesh->coneOrientations  = NULL;
1415   ierr                    = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection);CHKERRQ(ierr);
1416   mesh->maxSupportSize    = 0;
1417   mesh->supports          = NULL;
1418   mesh->refinementUniform = PETSC_TRUE;
1419   mesh->refinementLimit   = -1.0;
1420 
1421   mesh->facesTmp = NULL;
1422 
1423   mesh->tetgenOpts   = NULL;
1424   mesh->triangleOpts = NULL;
1425   ierr = PetscPartitionerCreate(PetscObjectComm((PetscObject)dm), &mesh->partitioner);CHKERRQ(ierr);
1426   ierr = PetscPartitionerSetTypeFromOptions_Internal(mesh->partitioner);CHKERRQ(ierr);
1427   mesh->remeshBd     = PETSC_FALSE;
1428 
1429   mesh->subpointMap = NULL;
1430 
1431   for (unit = 0; unit < NUM_PETSC_UNITS; ++unit) mesh->scale[unit] = 1.0;
1432 
1433   mesh->regularRefinement   = PETSC_FALSE;
1434   mesh->depthState          = -1;
1435   mesh->globalVertexNumbers = NULL;
1436   mesh->globalCellNumbers   = NULL;
1437   mesh->anchorSection       = NULL;
1438   mesh->anchorIS            = NULL;
1439   mesh->createanchors       = NULL;
1440   mesh->computeanchormatrix = NULL;
1441   mesh->parentSection       = NULL;
1442   mesh->parents             = NULL;
1443   mesh->childIDs            = NULL;
1444   mesh->childSection        = NULL;
1445   mesh->children            = NULL;
1446   mesh->referenceTree       = NULL;
1447   mesh->getchildsymmetry    = NULL;
1448   for (d = 0; d < 8; ++d) mesh->hybridPointMax[d] = PETSC_DETERMINE;
1449   mesh->vtkCellHeight       = 0;
1450   mesh->useCone             = PETSC_FALSE;
1451   mesh->useClosure          = PETSC_TRUE;
1452   mesh->useAnchors          = PETSC_FALSE;
1453 
1454   mesh->maxProjectionHeight = 0;
1455 
1456   mesh->printSetValues = PETSC_FALSE;
1457   mesh->printFEM       = 0;
1458   mesh->printTol       = 1.0e-10;
1459 
1460   ierr = DMInitialize_Plex(dm);CHKERRQ(ierr);
1461   PetscFunctionReturn(0);
1462 }
1463 
1464 #undef __FUNCT__
1465 #define __FUNCT__ "DMPlexCreate"
1466 /*@
1467   DMPlexCreate - Creates a DMPlex object, which encapsulates an unstructured mesh, or CW complex, which can be expressed using a Hasse Diagram.
1468 
1469   Collective on MPI_Comm
1470 
1471   Input Parameter:
1472 . comm - The communicator for the DMPlex object
1473 
1474   Output Parameter:
1475 . mesh  - The DMPlex object
1476 
1477   Level: beginner
1478 
1479 .keywords: DMPlex, create
1480 @*/
1481 PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh)
1482 {
1483   PetscErrorCode ierr;
1484 
1485   PetscFunctionBegin;
1486   PetscValidPointer(mesh,2);
1487   ierr = DMCreate(comm, mesh);CHKERRQ(ierr);
1488   ierr = DMSetType(*mesh, DMPLEX);CHKERRQ(ierr);
1489   PetscFunctionReturn(0);
1490 }
1491 
1492 #undef __FUNCT__
1493 #define __FUNCT__ "DMPlexBuildFromCellList_Parallel_Private"
1494 /*
1495   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
1496 */
1497 static PetscErrorCode DMPlexBuildFromCellList_Parallel_Private(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const int cells[], PetscSF *sfVert)
1498 {
1499   PetscSF         sfPoint;
1500   PetscLayout     vLayout;
1501   PetscHashI      vhash;
1502   PetscSFNode    *remoteVerticesAdj, *vertexLocal, *vertexOwner, *remoteVertex;
1503   const PetscInt *vrange;
1504   PetscInt        numVerticesAdj, off, *verticesAdj, numVerticesGhost = 0, *localVertex, *cone, c, p, v, g;
1505   PETSC_UNUSED PetscHashIIter ret, iter;
1506   PetscMPIInt     rank, numProcs;
1507   PetscErrorCode  ierr;
1508 
1509   PetscFunctionBegin;
1510   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
1511   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &numProcs);CHKERRQ(ierr);
1512   /* Partition vertices */
1513   ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) dm), &vLayout);CHKERRQ(ierr);
1514   ierr = PetscLayoutSetLocalSize(vLayout, numVertices);CHKERRQ(ierr);
1515   ierr = PetscLayoutSetBlockSize(vLayout, 1);CHKERRQ(ierr);
1516   ierr = PetscLayoutSetUp(vLayout);CHKERRQ(ierr);
1517   ierr = PetscLayoutGetRanges(vLayout, &vrange);CHKERRQ(ierr);
1518   /* Count vertices and map them to procs */
1519   PetscHashICreate(vhash);
1520   for (c = 0; c < numCells; ++c) {
1521     for (p = 0; p < numCorners; ++p) {
1522       PetscHashIPut(vhash, cells[c*numCorners+p], ret, iter);
1523     }
1524   }
1525   PetscHashISize(vhash, numVerticesAdj);
1526   ierr = PetscMalloc1(numVerticesAdj, &verticesAdj);CHKERRQ(ierr);
1527   off = 0; ierr = PetscHashIGetKeys(vhash, &off, verticesAdj);CHKERRQ(ierr);
1528   if (off != numVerticesAdj) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid number of local vertices %D should be %D", off, numVerticesAdj);
1529   ierr = PetscMalloc1(numVerticesAdj, &remoteVerticesAdj);CHKERRQ(ierr);
1530   for (v = 0; v < numVerticesAdj; ++v) {
1531     const PetscInt gv = verticesAdj[v];
1532     PetscInt       vrank;
1533 
1534     ierr = PetscFindInt(gv, numProcs+1, vrange, &vrank);CHKERRQ(ierr);
1535     vrank = vrank < 0 ? -(vrank+2) : vrank;
1536     remoteVerticesAdj[v].index = gv - vrange[vrank];
1537     remoteVerticesAdj[v].rank  = vrank;
1538   }
1539   /* Create cones */
1540   ierr = DMPlexSetChart(dm, 0, numCells+numVerticesAdj);CHKERRQ(ierr);
1541   for (c = 0; c < numCells; ++c) {ierr = DMPlexSetConeSize(dm, c, numCorners);CHKERRQ(ierr);}
1542   ierr = DMSetUp(dm);CHKERRQ(ierr);
1543   ierr = DMGetWorkArray(dm, numCorners, PETSC_INT, &cone);CHKERRQ(ierr);
1544   for (c = 0; c < numCells; ++c) {
1545     for (p = 0; p < numCorners; ++p) {
1546       const PetscInt gv = cells[c*numCorners+p];
1547       PetscInt       lv;
1548 
1549       ierr = PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv);CHKERRQ(ierr);
1550       if (lv < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %D in local connectivity", gv);
1551       cone[p] = lv+numCells;
1552     }
1553     ierr = DMPlexSetCone(dm, c, cone);CHKERRQ(ierr);
1554   }
1555   ierr = DMRestoreWorkArray(dm, numCorners, PETSC_INT, &cone);CHKERRQ(ierr);
1556   /* Create SF for vertices */
1557   ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm), sfVert);CHKERRQ(ierr);
1558   ierr = PetscObjectSetName((PetscObject) *sfVert, "Vertex Ownership SF");CHKERRQ(ierr);
1559   ierr = PetscSFSetFromOptions(*sfVert);CHKERRQ(ierr);
1560   ierr = PetscSFSetGraph(*sfVert, numVertices, numVerticesAdj, NULL, PETSC_OWN_POINTER, remoteVerticesAdj, PETSC_OWN_POINTER);CHKERRQ(ierr);
1561   ierr = PetscFree(verticesAdj);CHKERRQ(ierr);
1562   /* Build pointSF */
1563   ierr = PetscMalloc2(numVerticesAdj, &vertexLocal, numVertices, &vertexOwner);CHKERRQ(ierr);
1564   for (v = 0; v < numVerticesAdj; ++v) {vertexLocal[v].index = v+numCells; vertexLocal[v].rank = rank;}
1565   for (v = 0; v < numVertices;    ++v) {vertexOwner[v].index = -1;         vertexOwner[v].rank = -1;}
1566   ierr = PetscSFReduceBegin(*sfVert, MPI_2INT, vertexLocal, vertexOwner, MPI_MAXLOC);CHKERRQ(ierr);
1567   ierr = PetscSFReduceEnd(*sfVert, MPI_2INT, vertexLocal, vertexOwner, MPI_MAXLOC);CHKERRQ(ierr);
1568   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);
1569   ierr = PetscSFBcastBegin(*sfVert, MPI_2INT, vertexOwner, vertexLocal);CHKERRQ(ierr);
1570   ierr = PetscSFBcastEnd(*sfVert, MPI_2INT, vertexOwner, vertexLocal);CHKERRQ(ierr);
1571   for (v = 0; v < numVerticesAdj; ++v) if (vertexLocal[v].rank != rank) ++numVerticesGhost;
1572   ierr = PetscMalloc1(numVerticesGhost, &localVertex);CHKERRQ(ierr);
1573   ierr = PetscMalloc1(numVerticesGhost, &remoteVertex);CHKERRQ(ierr);
1574   for (v = 0, g = 0; v < numVerticesAdj; ++v) {
1575     if (vertexLocal[v].rank != rank) {
1576       localVertex[g]        = v+numCells;
1577       remoteVertex[g].index = vertexLocal[v].index;
1578       remoteVertex[g].rank  = vertexLocal[v].rank;
1579       ++g;
1580     }
1581   }
1582   ierr = PetscFree2(vertexLocal, vertexOwner);CHKERRQ(ierr);
1583   if (g != numVerticesGhost) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid number of vertex ghosts %D should be %D", g, numVerticesGhost);
1584   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
1585   ierr = PetscObjectSetName((PetscObject) sfPoint, "point SF");CHKERRQ(ierr);
1586   ierr = PetscSFSetGraph(sfPoint, numCells+numVerticesAdj, numVerticesGhost, localVertex, PETSC_OWN_POINTER, remoteVertex, PETSC_OWN_POINTER);CHKERRQ(ierr);
1587   ierr = PetscLayoutDestroy(&vLayout);CHKERRQ(ierr);
1588   PetscHashIDestroy(vhash);
1589   /* Fill in the rest of the topology structure */
1590   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
1591   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
1592   PetscFunctionReturn(0);
1593 }
1594 
1595 #undef __FUNCT__
1596 #define __FUNCT__ "DMPlexBuildCoordinates_Parallel_Private"
1597 /*
1598   This takes as input the coordinates for each owned vertex
1599 */
1600 static PetscErrorCode DMPlexBuildCoordinates_Parallel_Private(DM dm, PetscInt spaceDim, PetscInt numCells, PetscSF sfVert, const double vertexCoords[])
1601 {
1602   PetscSection   coordSection;
1603   Vec            coordinates;
1604   PetscScalar   *coords;
1605   PetscInt       numVertices, numVerticesAdj, coordSize, v;
1606   PetscErrorCode ierr;
1607 
1608   PetscFunctionBegin;
1609   ierr = PetscSFGetGraph(sfVert, &numVertices, &numVerticesAdj, NULL, NULL);CHKERRQ(ierr);
1610   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1611   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
1612   ierr = PetscSectionSetFieldComponents(coordSection, 0, spaceDim);CHKERRQ(ierr);
1613   ierr = PetscSectionSetChart(coordSection, numCells, numCells + numVerticesAdj);CHKERRQ(ierr);
1614   for (v = numCells; v < numCells+numVerticesAdj; ++v) {
1615     ierr = PetscSectionSetDof(coordSection, v, spaceDim);CHKERRQ(ierr);
1616     ierr = PetscSectionSetFieldDof(coordSection, v, 0, spaceDim);CHKERRQ(ierr);
1617   }
1618   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
1619   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
1620   ierr = VecCreate(PetscObjectComm((PetscObject)dm), &coordinates);CHKERRQ(ierr);
1621   ierr = VecSetBlockSize(coordinates, spaceDim);CHKERRQ(ierr);
1622   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
1623   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
1624   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
1625   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
1626   {
1627     MPI_Datatype coordtype;
1628 
1629     /* Need a temp buffer for coords if we have complex/single */
1630     ierr = MPI_Type_contiguous(spaceDim, MPI_DOUBLE, &coordtype);CHKERRQ(ierr);
1631     ierr = MPI_Type_commit(&coordtype);CHKERRQ(ierr);
1632     ierr = PetscSFBcastBegin(sfVert, coordtype, vertexCoords, coords);CHKERRQ(ierr);
1633     ierr = PetscSFBcastEnd(sfVert, coordtype, vertexCoords, coords);CHKERRQ(ierr);
1634     ierr = MPI_Type_free(&coordtype);CHKERRQ(ierr);
1635   }
1636   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
1637   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
1638   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
1639   PetscFunctionReturn(0);
1640 }
1641 
1642 #undef __FUNCT__
1643 #define __FUNCT__ "DMPlexCreateFromCellListParallel"
1644 /*@C
1645   DMPlexCreateFromCellListParallel - This takes as input common mesh generator output, a list of the vertices for each cell, and produces a DM
1646 
1647   Input Parameters:
1648 + comm - The communicator
1649 . dim - The topological dimension of the mesh
1650 . numCells - The number of cells owned by this process
1651 . numVertices - The number of vertices owned by this process
1652 . numCorners - The number of vertices for each cell
1653 . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
1654 . cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
1655 . spaceDim - The spatial dimension used for coordinates
1656 - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
1657 
1658   Output Parameter:
1659 . dm - The DM
1660 
1661   Note: Two triangles sharing a face
1662 $
1663 $        2
1664 $      / | \
1665 $     /  |  \
1666 $    /   |   \
1667 $   0  0 | 1  3
1668 $    \   |   /
1669 $     \  |  /
1670 $      \ | /
1671 $        1
1672 would have input
1673 $  numCells = 2, numVertices = 4
1674 $  cells = [0 1 2  1 3 2]
1675 $
1676 which would result in the DMPlex
1677 $
1678 $        4
1679 $      / | \
1680 $     /  |  \
1681 $    /   |   \
1682 $   2  0 | 1  5
1683 $    \   |   /
1684 $     \  |  /
1685 $      \ | /
1686 $        3
1687 
1688   Level: beginner
1689 
1690 .seealso: DMPlexCreateFromCellList(), DMPlexCreateFromDAG(), DMPlexCreate()
1691 @*/
1692 PetscErrorCode DMPlexCreateFromCellListParallel(MPI_Comm comm, PetscInt dim, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, PetscBool interpolate, const int cells[], PetscInt spaceDim, const double vertexCoords[], DM *dm)
1693 {
1694   PetscSF        sfVert;
1695   PetscErrorCode ierr;
1696 
1697   PetscFunctionBegin;
1698   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
1699   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
1700   PetscValidLogicalCollectiveInt(*dm, dim, 2);
1701   PetscValidLogicalCollectiveInt(*dm, spaceDim, 8);
1702   ierr = DMSetDimension(*dm, dim);CHKERRQ(ierr);
1703   ierr = DMPlexBuildFromCellList_Parallel_Private(*dm, numCells, numVertices, numCorners, cells, &sfVert);CHKERRQ(ierr);
1704   if (interpolate) {
1705     DM idm = NULL;
1706 
1707     ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr);
1708     ierr = DMDestroy(dm);CHKERRQ(ierr);
1709     *dm  = idm;
1710   }
1711   ierr = DMPlexBuildCoordinates_Parallel_Private(*dm, spaceDim, numCells, sfVert, vertexCoords);CHKERRQ(ierr);
1712   ierr = PetscSFDestroy(&sfVert);CHKERRQ(ierr);
1713   PetscFunctionReturn(0);
1714 }
1715 
1716 #undef __FUNCT__
1717 #define __FUNCT__ "DMPlexBuildFromCellList_Private"
1718 /*
1719   This takes as input the common mesh generator output, a list of the vertices for each cell
1720 */
1721 static PetscErrorCode DMPlexBuildFromCellList_Private(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const int cells[])
1722 {
1723   PetscInt      *cone, c, p;
1724   PetscErrorCode ierr;
1725 
1726   PetscFunctionBegin;
1727   ierr = DMPlexSetChart(dm, 0, numCells+numVertices);CHKERRQ(ierr);
1728   for (c = 0; c < numCells; ++c) {
1729     ierr = DMPlexSetConeSize(dm, c, numCorners);CHKERRQ(ierr);
1730   }
1731   ierr = DMSetUp(dm);CHKERRQ(ierr);
1732   ierr = DMGetWorkArray(dm, numCorners, PETSC_INT, &cone);CHKERRQ(ierr);
1733   for (c = 0; c < numCells; ++c) {
1734     for (p = 0; p < numCorners; ++p) {
1735       cone[p] = cells[c*numCorners+p]+numCells;
1736     }
1737     ierr = DMPlexSetCone(dm, c, cone);CHKERRQ(ierr);
1738   }
1739   ierr = DMRestoreWorkArray(dm, numCorners, PETSC_INT, &cone);CHKERRQ(ierr);
1740   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
1741   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
1742   PetscFunctionReturn(0);
1743 }
1744 
1745 #undef __FUNCT__
1746 #define __FUNCT__ "DMPlexBuildCoordinates_Private"
1747 /*
1748   This takes as input the coordinates for each vertex
1749 */
1750 static PetscErrorCode DMPlexBuildCoordinates_Private(DM dm, PetscInt spaceDim, PetscInt numCells, PetscInt numVertices, const double vertexCoords[])
1751 {
1752   PetscSection   coordSection;
1753   Vec            coordinates;
1754   DM             cdm;
1755   PetscScalar   *coords;
1756   PetscInt       v, d;
1757   PetscErrorCode ierr;
1758 
1759   PetscFunctionBegin;
1760   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1761   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
1762   ierr = PetscSectionSetFieldComponents(coordSection, 0, spaceDim);CHKERRQ(ierr);
1763   ierr = PetscSectionSetChart(coordSection, numCells, numCells + numVertices);CHKERRQ(ierr);
1764   for (v = numCells; v < numCells+numVertices; ++v) {
1765     ierr = PetscSectionSetDof(coordSection, v, spaceDim);CHKERRQ(ierr);
1766     ierr = PetscSectionSetFieldDof(coordSection, v, 0, spaceDim);CHKERRQ(ierr);
1767   }
1768   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
1769 
1770   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
1771   ierr = DMCreateLocalVector(cdm, &coordinates);CHKERRQ(ierr);
1772   ierr = VecSetBlockSize(coordinates, spaceDim);CHKERRQ(ierr);
1773   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
1774   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
1775   for (v = 0; v < numVertices; ++v) {
1776     for (d = 0; d < spaceDim; ++d) {
1777       coords[v*spaceDim+d] = vertexCoords[v*spaceDim+d];
1778     }
1779   }
1780   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
1781   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
1782   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
1783   PetscFunctionReturn(0);
1784 }
1785 
1786 #undef __FUNCT__
1787 #define __FUNCT__ "DMPlexCreateFromCellList"
1788 /*@C
1789   DMPlexCreateFromCellList - This takes as input common mesh generator output, a list of the vertices for each cell, and produces a DM
1790 
1791   Input Parameters:
1792 + comm - The communicator
1793 . dim - The topological dimension of the mesh
1794 . numCells - The number of cells
1795 . numVertices - The number of vertices
1796 . numCorners - The number of vertices for each cell
1797 . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
1798 . cells - An array of numCells*numCorners numbers, the vertices for each cell
1799 . spaceDim - The spatial dimension used for coordinates
1800 - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
1801 
1802   Output Parameter:
1803 . dm - The DM
1804 
1805   Note: Two triangles sharing a face
1806 $
1807 $        2
1808 $      / | \
1809 $     /  |  \
1810 $    /   |   \
1811 $   0  0 | 1  3
1812 $    \   |   /
1813 $     \  |  /
1814 $      \ | /
1815 $        1
1816 would have input
1817 $  numCells = 2, numVertices = 4
1818 $  cells = [0 1 2  1 3 2]
1819 $
1820 which would result in the DMPlex
1821 $
1822 $        4
1823 $      / | \
1824 $     /  |  \
1825 $    /   |   \
1826 $   2  0 | 1  5
1827 $    \   |   /
1828 $     \  |  /
1829 $      \ | /
1830 $        3
1831 
1832   Level: beginner
1833 
1834 .seealso: DMPlexCreateFromDAG(), DMPlexCreate()
1835 @*/
1836 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)
1837 {
1838   PetscErrorCode ierr;
1839 
1840   PetscFunctionBegin;
1841   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
1842   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
1843   ierr = DMSetDimension(*dm, dim);CHKERRQ(ierr);
1844   ierr = DMPlexBuildFromCellList_Private(*dm, numCells, numVertices, numCorners, cells);CHKERRQ(ierr);
1845   if (interpolate) {
1846     DM idm = NULL;
1847 
1848     ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr);
1849     ierr = DMDestroy(dm);CHKERRQ(ierr);
1850     *dm  = idm;
1851   }
1852   ierr = DMPlexBuildCoordinates_Private(*dm, spaceDim, numCells, numVertices, vertexCoords);CHKERRQ(ierr);
1853   PetscFunctionReturn(0);
1854 }
1855 
1856 #undef __FUNCT__
1857 #define __FUNCT__ "DMPlexCreateFromDAG"
1858 /*@
1859   DMPlexCreateFromDAG - This takes as input the adjacency-list representation of the Directed Acyclic Graph (Hasse Diagram) encoding a mesh, and produces a DM
1860 
1861   Input Parameters:
1862 + dm - The empty DM object, usually from DMCreate() and DMSetDimension()
1863 . depth - The depth of the DAG
1864 . numPoints - The number of points at each depth
1865 . coneSize - The cone size of each point
1866 . cones - The concatenation of the cone points for each point, the cone list must be oriented correctly for each point
1867 . coneOrientations - The orientation of each cone point
1868 - vertexCoords - An array of numVertices*dim numbers, the coordinates of each vertex
1869 
1870   Output Parameter:
1871 . dm - The DM
1872 
1873   Note: Two triangles sharing a face would have input
1874 $  depth = 1, numPoints = [4 2], coneSize = [3 3 0 0 0 0]
1875 $  cones = [2 3 4  3 5 4], coneOrientations = [0 0 0  0 0 0]
1876 $ vertexCoords = [-1.0 0.0  0.0 -1.0  0.0 1.0  1.0 0.0]
1877 $
1878 which would result in the DMPlex
1879 $
1880 $        4
1881 $      / | \
1882 $     /  |  \
1883 $    /   |   \
1884 $   2  0 | 1  5
1885 $    \   |   /
1886 $     \  |  /
1887 $      \ | /
1888 $        3
1889 $
1890 $ Notice that all points are numbered consecutively, unlikely DMPlexCreateFromCellList()
1891 
1892   Level: advanced
1893 
1894 .seealso: DMPlexCreateFromCellList(), DMPlexCreate()
1895 @*/
1896 PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[])
1897 {
1898   Vec            coordinates;
1899   PetscSection   coordSection;
1900   PetscScalar    *coords;
1901   PetscInt       coordSize, firstVertex = -1, pStart = 0, pEnd = 0, p, v, dim, dimEmbed, d, off;
1902   PetscErrorCode ierr;
1903 
1904   PetscFunctionBegin;
1905   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1906   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
1907   if (dimEmbed < dim) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Embedding dimension %d cannot be less than intrinsic dimension %d",dimEmbed,dim);
1908   for (d = 0; d <= depth; ++d) pEnd += numPoints[d];
1909   ierr = DMPlexSetChart(dm, pStart, pEnd);CHKERRQ(ierr);
1910   for (p = pStart; p < pEnd; ++p) {
1911     ierr = DMPlexSetConeSize(dm, p, coneSize[p-pStart]);CHKERRQ(ierr);
1912     if (firstVertex < 0 && !coneSize[p - pStart]) {
1913       firstVertex = p - pStart;
1914     }
1915   }
1916   if (firstVertex < 0 && numPoints[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected %d vertices but could not find any", numPoints[0]);
1917   ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
1918   for (p = pStart, off = 0; p < pEnd; off += coneSize[p-pStart], ++p) {
1919     ierr = DMPlexSetCone(dm, p, &cones[off]);CHKERRQ(ierr);
1920     ierr = DMPlexSetConeOrientation(dm, p, &coneOrientations[off]);CHKERRQ(ierr);
1921   }
1922   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
1923   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
1924   /* Build coordinates */
1925   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1926   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
1927   ierr = PetscSectionSetFieldComponents(coordSection, 0, dimEmbed);CHKERRQ(ierr);
1928   ierr = PetscSectionSetChart(coordSection, firstVertex, firstVertex+numPoints[0]);CHKERRQ(ierr);
1929   for (v = firstVertex; v < firstVertex+numPoints[0]; ++v) {
1930     ierr = PetscSectionSetDof(coordSection, v, dimEmbed);CHKERRQ(ierr);
1931     ierr = PetscSectionSetFieldDof(coordSection, v, 0, dimEmbed);CHKERRQ(ierr);
1932   }
1933   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
1934   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
1935   ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
1936   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
1937   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
1938   ierr = VecSetBlockSize(coordinates, dimEmbed);CHKERRQ(ierr);
1939   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
1940   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
1941   for (v = 0; v < numPoints[0]; ++v) {
1942     PetscInt off;
1943 
1944     ierr = PetscSectionGetOffset(coordSection, v+firstVertex, &off);CHKERRQ(ierr);
1945     for (d = 0; d < dimEmbed; ++d) {
1946       coords[off+d] = vertexCoords[v*dimEmbed+d];
1947     }
1948   }
1949   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
1950   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
1951   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
1952   PetscFunctionReturn(0);
1953 }
1954 
1955 #undef __FUNCT__
1956 #define __FUNCT__ "DMPlexCreateFromFile"
1957 /*@C
1958   DMPlexCreateFromFile - This takes a filename and produces a DM
1959 
1960   Input Parameters:
1961 + comm - The communicator
1962 . filename - A file name
1963 - interpolate - Flag to create intermediate mesh pieces (edges, faces)
1964 
1965   Output Parameter:
1966 . dm - The DM
1967 
1968   Level: beginner
1969 
1970 .seealso: DMPlexCreateFromDAG(), DMPlexCreateFromCellList(), DMPlexCreate()
1971 @*/
1972 PetscErrorCode DMPlexCreateFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
1973 {
1974   const char    *extGmsh   = ".msh";
1975   const char    *extCGNS   = ".cgns";
1976   const char    *extExodus = ".exo";
1977   const char    *extFluent = ".cas";
1978   const char    *extHDF5   = ".h5";
1979   size_t         len;
1980   PetscBool      isGmsh, isCGNS, isExodus, isFluent, isHDF5;
1981   PetscMPIInt    rank;
1982   PetscErrorCode ierr;
1983 
1984   PetscFunctionBegin;
1985   PetscValidPointer(filename, 2);
1986   PetscValidPointer(dm, 4);
1987   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
1988   ierr = PetscStrlen(filename, &len);CHKERRQ(ierr);
1989   if (!len) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Filename must be a valid path");
1990   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)], extGmsh,   4, &isGmsh);CHKERRQ(ierr);
1991   ierr = PetscStrncmp(&filename[PetscMax(0,len-5)], extCGNS,   5, &isCGNS);CHKERRQ(ierr);
1992   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)], extExodus, 4, &isExodus);CHKERRQ(ierr);
1993   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)], extFluent, 4, &isFluent);CHKERRQ(ierr);
1994   ierr = PetscStrncmp(&filename[PetscMax(0,len-3)], extHDF5,   3, &isHDF5);CHKERRQ(ierr);
1995   if (isGmsh) {
1996     ierr = DMPlexCreateGmshFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
1997   } else if (isCGNS) {
1998     ierr = DMPlexCreateCGNSFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
1999   } else if (isExodus) {
2000     ierr = DMPlexCreateExodusFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
2001   } else if (isFluent) {
2002     ierr = DMPlexCreateFluentFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
2003   } else if (isHDF5) {
2004     PetscViewer viewer;
2005 
2006     ierr = PetscViewerCreate(comm, &viewer);CHKERRQ(ierr);
2007     ierr = PetscViewerSetType(viewer, PETSCVIEWERHDF5);CHKERRQ(ierr);
2008     ierr = PetscViewerFileSetMode(viewer, FILE_MODE_READ);CHKERRQ(ierr);
2009     ierr = PetscViewerFileSetName(viewer, filename);CHKERRQ(ierr);
2010     ierr = DMCreate(comm, dm);CHKERRQ(ierr);
2011     ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
2012     ierr = DMLoad(*dm, viewer);CHKERRQ(ierr);
2013     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
2014   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename);
2015   PetscFunctionReturn(0);
2016 }
2017 
2018 #undef __FUNCT__
2019 #define __FUNCT__ "DMPlexCreateReferenceCell"
2020 /*@
2021   DMPlexCreateReferenceCell - Create a DMPLEX with the appropriate FEM reference cell
2022 
2023   Collective on comm
2024 
2025   Input Parameters:
2026 + comm    - The communicator
2027 . dim     - The spatial dimension
2028 - simplex - Flag for simplex, otherwise use a tensor-product cell
2029 
2030   Output Parameter:
2031 . refdm - The reference cell
2032 
2033   Level: intermediate
2034 
2035 .keywords: reference cell
2036 .seealso:
2037 @*/
2038 PetscErrorCode DMPlexCreateReferenceCell(MPI_Comm comm, PetscInt dim, PetscBool simplex, DM *refdm)
2039 {
2040   DM             rdm;
2041   Vec            coords;
2042   PetscErrorCode ierr;
2043 
2044   PetscFunctionBeginUser;
2045   ierr = DMCreate(comm, &rdm);CHKERRQ(ierr);
2046   ierr = DMSetType(rdm, DMPLEX);CHKERRQ(ierr);
2047   ierr = DMSetDimension(rdm, dim);CHKERRQ(ierr);
2048   switch (dim) {
2049   case 0:
2050   {
2051     PetscInt    numPoints[1]        = {1};
2052     PetscInt    coneSize[1]         = {0};
2053     PetscInt    cones[1]            = {0};
2054     PetscInt    coneOrientations[1] = {0};
2055     PetscScalar vertexCoords[1]     = {0.0};
2056 
2057     ierr = DMPlexCreateFromDAG(rdm, 0, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2058   }
2059   break;
2060   case 1:
2061   {
2062     PetscInt    numPoints[2]        = {2, 1};
2063     PetscInt    coneSize[3]         = {2, 0, 0};
2064     PetscInt    cones[2]            = {1, 2};
2065     PetscInt    coneOrientations[2] = {0, 0};
2066     PetscScalar vertexCoords[2]     = {-1.0,  1.0};
2067 
2068     ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2069   }
2070   break;
2071   case 2:
2072     if (simplex) {
2073       PetscInt    numPoints[2]        = {3, 1};
2074       PetscInt    coneSize[4]         = {3, 0, 0, 0};
2075       PetscInt    cones[3]            = {1, 2, 3};
2076       PetscInt    coneOrientations[3] = {0, 0, 0};
2077       PetscScalar vertexCoords[6]     = {-1.0, -1.0,  1.0, -1.0,  -1.0, 1.0};
2078 
2079       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2080     } else {
2081       PetscInt    numPoints[2]        = {4, 1};
2082       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
2083       PetscInt    cones[4]            = {1, 2, 3, 4};
2084       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
2085       PetscScalar vertexCoords[8]     = {-1.0, -1.0,  1.0, -1.0,  1.0, 1.0,  -1.0, 1.0};
2086 
2087       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2088     }
2089   break;
2090   case 3:
2091     if (simplex) {
2092       PetscInt    numPoints[2]        = {4, 1};
2093       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
2094       PetscInt    cones[4]            = {1, 3, 2, 4};
2095       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
2096       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};
2097 
2098       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2099     } else {
2100       PetscInt    numPoints[2]        = {8, 1};
2101       PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
2102       PetscInt    cones[8]            = {1, 4, 3, 2, 5, 6, 7, 8};
2103       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
2104       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,
2105                                          -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  1.0, 1.0,  1.0,  -1.0, 1.0,  1.0};
2106 
2107       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2108     }
2109   break;
2110   default:
2111     SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Cannot create reference cell for dimension %d", dim);
2112   }
2113   *refdm = NULL;
2114   ierr = DMPlexInterpolate(rdm, refdm);CHKERRQ(ierr);
2115   if (rdm->coordinateDM) {
2116     DM           ncdm;
2117     PetscSection cs;
2118     PetscInt     pEnd = -1;
2119 
2120     ierr = DMGetDefaultSection(rdm->coordinateDM, &cs);CHKERRQ(ierr);
2121     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
2122     if (pEnd >= 0) {
2123       ierr = DMClone(rdm->coordinateDM, &ncdm);CHKERRQ(ierr);
2124       ierr = DMSetDefaultSection(ncdm, cs);CHKERRQ(ierr);
2125       ierr = DMSetCoordinateDM(*refdm, ncdm);CHKERRQ(ierr);
2126       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
2127     }
2128   }
2129   ierr = DMGetCoordinatesLocal(rdm, &coords);CHKERRQ(ierr);
2130   if (coords) {
2131     ierr = DMSetCoordinatesLocal(*refdm, coords);CHKERRQ(ierr);
2132   } else {
2133     ierr = DMGetCoordinates(rdm, &coords);CHKERRQ(ierr);
2134     if (coords) {ierr = DMSetCoordinates(*refdm, coords);CHKERRQ(ierr);}
2135   }
2136   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
2137   PetscFunctionReturn(0);
2138 }
2139