xref: /petsc/src/dm/impls/plex/plexcreate.c (revision 50ae33c3ff0506d707994398120bbd882ce488d7)
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 = PetscSectionSetChart(coordSection, numEdges, numEdges + numVertices);CHKERRQ(ierr);
271   for (v = numEdges; v < numEdges+numVertices; ++v) {
272     ierr = PetscSectionSetDof(coordSection, v, 2);CHKERRQ(ierr);
273   }
274   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
275   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
276   ierr = VecCreate(PetscObjectComm((PetscObject)dm), &coordinates);CHKERRQ(ierr);
277   ierr = VecSetBlockSize(coordinates, 2);CHKERRQ(ierr);
278   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
279   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
280   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
281   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
282   for (vy = 0; vy <= edges[1]; ++vy) {
283     for (vx = 0; vx <= edges[0]; ++vx) {
284       coords[(vy*(edges[0]+1)+vx)*2+0] = lower[0] + ((upper[0] - lower[0])/edges[0])*vx;
285       coords[(vy*(edges[0]+1)+vx)*2+1] = lower[1] + ((upper[1] - lower[1])/edges[1])*vy;
286     }
287   }
288   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
289   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
290   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
291   PetscFunctionReturn(0);
292 }
293 
294 #undef __FUNCT__
295 #define __FUNCT__ "DMPlexCreateCubeBoundary"
296 /*@
297   DMPlexCreateCubeBoundary - Creates a 2D mesh the is the boundary of a cubic lattice.
298 
299   Collective on MPI_Comm
300 
301   Input Parameters:
302 + comm  - The communicator for the DM object
303 . lower - The lower left front corner coordinates
304 . upper - The upper right back corner coordinates
305 - edges - The number of cells in each direction
306 
307   Output Parameter:
308 . dm  - The DM object
309 
310   Level: beginner
311 
312 .keywords: DM, create
313 .seealso: DMPlexCreateBoxMesh(), DMPlexCreateSquareBoundary(), DMSetType(), DMCreate()
314 @*/
315 PetscErrorCode DMPlexCreateCubeBoundary(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt faces[])
316 {
317   PetscInt       vertices[3], numVertices;
318   PetscInt       numFaces    = 2*faces[0]*faces[1] + 2*faces[1]*faces[2] + 2*faces[0]*faces[2];
319   Vec            coordinates;
320   PetscSection   coordSection;
321   PetscScalar    *coords;
322   PetscInt       coordSize;
323   PetscMPIInt    rank;
324   PetscInt       v, vx, vy, vz;
325   PetscInt       voffset, iface=0, cone[4];
326   PetscErrorCode ierr;
327 
328   PetscFunctionBegin;
329   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");
330   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
331   vertices[0] = faces[0]+1; vertices[1] = faces[1]+1; vertices[2] = faces[2]+1;
332   numVertices = vertices[0]*vertices[1]*vertices[2];
333   if (!rank) {
334     PetscInt f;
335 
336     ierr = DMPlexSetChart(dm, 0, numFaces+numVertices);CHKERRQ(ierr);
337     for (f = 0; f < numFaces; ++f) {
338       ierr = DMPlexSetConeSize(dm, f, 4);CHKERRQ(ierr);
339     }
340     ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
341     for (v = 0; v < numFaces+numVertices; ++v) {
342       ierr = DMSetLabelValue(dm, "marker", v, 1);CHKERRQ(ierr);
343     }
344 
345     /* Side 0 (Top) */
346     for (vy = 0; vy < faces[1]; vy++) {
347       for (vx = 0; vx < faces[0]; vx++) {
348         voffset = numFaces + vertices[0]*vertices[1]*(vertices[2]-1) + vy*vertices[0] + vx;
349         cone[0] = voffset; cone[1] = voffset+1; cone[2] = voffset+vertices[0]+1; cone[3] = voffset+vertices[0];
350         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
351         iface++;
352       }
353     }
354 
355     /* Side 1 (Bottom) */
356     for (vy = 0; vy < faces[1]; vy++) {
357       for (vx = 0; vx < faces[0]; vx++) {
358         voffset = numFaces + vy*(faces[0]+1) + vx;
359         cone[0] = voffset+1; cone[1] = voffset; cone[2] = voffset+vertices[0]; cone[3] = voffset+vertices[0]+1;
360         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
361         iface++;
362       }
363     }
364 
365     /* Side 2 (Front) */
366     for (vz = 0; vz < faces[2]; vz++) {
367       for (vx = 0; vx < faces[0]; vx++) {
368         voffset = numFaces + vz*vertices[0]*vertices[1] + vx;
369         cone[0] = voffset; cone[1] = voffset+1; cone[2] = voffset+vertices[0]*vertices[1]+1; cone[3] = voffset+vertices[0]*vertices[1];
370         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
371         iface++;
372       }
373     }
374 
375     /* Side 3 (Back) */
376     for (vz = 0; vz < faces[2]; vz++) {
377       for (vx = 0; vx < faces[0]; vx++) {
378         voffset = numFaces + vz*vertices[0]*vertices[1] + vertices[0]*(vertices[1]-1) + vx;
379         cone[0] = voffset+vertices[0]*vertices[1]; cone[1] = voffset+vertices[0]*vertices[1]+1;
380         cone[2] = voffset+1; cone[3] = voffset;
381         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
382         iface++;
383       }
384     }
385 
386     /* Side 4 (Left) */
387     for (vz = 0; vz < faces[2]; vz++) {
388       for (vy = 0; vy < faces[1]; vy++) {
389         voffset = numFaces + vz*vertices[0]*vertices[1] + vy*vertices[0];
390         cone[0] = voffset; cone[1] = voffset+vertices[0]*vertices[1];
391         cone[2] = voffset+vertices[0]*vertices[1]+vertices[0]; cone[3] = voffset+vertices[0];
392         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
393         iface++;
394       }
395     }
396 
397     /* Side 5 (Right) */
398     for (vz = 0; vz < faces[2]; vz++) {
399       for (vy = 0; vy < faces[1]; vy++) {
400         voffset = numFaces + vz*vertices[0]*vertices[1] + vy*vertices[0] + vx;
401         cone[0] = voffset+vertices[0]*vertices[1]; cone[1] = voffset;
402         cone[2] = voffset+vertices[0]; cone[3] = voffset+vertices[0]*vertices[1]+vertices[0];
403         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
404         iface++;
405       }
406     }
407   }
408   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
409   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
410   /* Build coordinates */
411   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
412   ierr = PetscSectionSetChart(coordSection, numFaces, numFaces + numVertices);CHKERRQ(ierr);
413   for (v = numFaces; v < numFaces+numVertices; ++v) {
414     ierr = PetscSectionSetDof(coordSection, v, 3);CHKERRQ(ierr);
415   }
416   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
417   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
418   ierr = VecCreate(PetscObjectComm((PetscObject)dm), &coordinates);CHKERRQ(ierr);
419   ierr = VecSetBlockSize(coordinates, 3);CHKERRQ(ierr);
420   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
421   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
422   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
423   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
424   for (vz = 0; vz <= faces[2]; ++vz) {
425     for (vy = 0; vy <= faces[1]; ++vy) {
426       for (vx = 0; vx <= faces[0]; ++vx) {
427         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+0] = lower[0] + ((upper[0] - lower[0])/faces[0])*vx;
428         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+1] = lower[1] + ((upper[1] - lower[1])/faces[1])*vy;
429         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+2] = lower[2] + ((upper[2] - lower[2])/faces[2])*vz;
430       }
431     }
432   }
433   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
434   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
435   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
436   PetscFunctionReturn(0);
437 }
438 
439 #undef __FUNCT__
440 #define __FUNCT__ "DMPlexCreateCubeMesh_Internal"
441 static PetscErrorCode DMPlexCreateCubeMesh_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY, DMBoundaryType bdZ)
442 {
443   PetscInt       markerTop      = 1, faceMarkerTop      = 1;
444   PetscInt       markerBottom   = 1, faceMarkerBottom   = 1;
445   PetscInt       markerFront    = 1, faceMarkerFront    = 1;
446   PetscInt       markerBack     = 1, faceMarkerBack     = 1;
447   PetscInt       markerRight    = 1, faceMarkerRight    = 1;
448   PetscInt       markerLeft     = 1, faceMarkerLeft     = 1;
449   PetscInt       dim;
450   PetscBool      markerSeparate = PETSC_FALSE;
451   PetscMPIInt    rank;
452   PetscErrorCode ierr;
453 
454   PetscFunctionBegin;
455   ierr = DMGetDimension(dm,&dim);CHKERRQ(ierr);
456   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
457   ierr = DMCreateLabel(dm,"marker");CHKERRQ(ierr);
458   ierr = DMCreateLabel(dm,"Face Sets");CHKERRQ(ierr);
459   switch (dim) {
460   case 2:
461     faceMarkerTop    = 3;
462     faceMarkerBottom = 1;
463     faceMarkerRight  = 2;
464     faceMarkerLeft   = 4;
465     break;
466   case 3:
467     faceMarkerBottom = 1;
468     faceMarkerTop    = 2;
469     faceMarkerFront  = 3;
470     faceMarkerBack   = 4;
471     faceMarkerRight  = 5;
472     faceMarkerLeft   = 6;
473     break;
474   default:
475     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Dimension %d not supported",dim);
476     break;
477   }
478   ierr = PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);CHKERRQ(ierr);
479   if (markerSeparate) {
480     markerBottom = faceMarkerBottom;
481     markerTop    = faceMarkerTop;
482     markerFront  = faceMarkerFront;
483     markerBack   = faceMarkerBack;
484     markerRight  = faceMarkerRight;
485     markerLeft   = faceMarkerLeft;
486   }
487   {
488     const PetscInt numXEdges    = !rank ? edges[0]   : 0;
489     const PetscInt numYEdges    = !rank ? edges[1]   : 0;
490     const PetscInt numZEdges    = !rank ? edges[2]   : 0;
491     const PetscInt numXVertices = !rank ? (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ? edges[0] : edges[0]+1) : 0;
492     const PetscInt numYVertices = !rank ? (bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ? edges[1] : edges[1]+1) : 0;
493     const PetscInt numZVertices = !rank ? (bdZ == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ? edges[2] : edges[2]+1) : 0;
494     const PetscInt numCells     = numXEdges*numYEdges*numZEdges;
495     const PetscInt numXFaces    = numYEdges*numZEdges;
496     const PetscInt numYFaces    = numXEdges*numZEdges;
497     const PetscInt numZFaces    = numXEdges*numYEdges;
498     const PetscInt numTotXFaces = numXVertices*numXFaces;
499     const PetscInt numTotYFaces = numYVertices*numYFaces;
500     const PetscInt numTotZFaces = numZVertices*numZFaces;
501     const PetscInt numFaces     = numTotXFaces + numTotYFaces + numTotZFaces;
502     const PetscInt numTotXEdges = numXEdges*numYVertices*numZVertices;
503     const PetscInt numTotYEdges = numYEdges*numXVertices*numZVertices;
504     const PetscInt numTotZEdges = numZEdges*numXVertices*numYVertices;
505     const PetscInt numVertices  = numXVertices*numYVertices*numZVertices;
506     const PetscInt numEdges     = numTotXEdges + numTotYEdges + numTotZEdges;
507     const PetscInt firstVertex  = (dim == 2) ? numFaces : numCells;
508     const PetscInt firstXFace   = (dim == 2) ? 0 : numCells + numVertices;
509     const PetscInt firstYFace   = firstXFace + numTotXFaces;
510     const PetscInt firstZFace   = firstYFace + numTotYFaces;
511     const PetscInt firstXEdge   = numCells + numFaces + numVertices;
512     const PetscInt firstYEdge   = firstXEdge + numTotXEdges;
513     const PetscInt firstZEdge   = firstYEdge + numTotYEdges;
514     Vec            coordinates;
515     PetscSection   coordSection;
516     PetscScalar   *coords;
517     PetscInt       coordSize;
518     PetscInt       v, vx, vy, vz;
519     PetscInt       c, f, fx, fy, fz, e, ex, ey, ez;
520 
521     ierr = DMPlexSetChart(dm, 0, numCells+numFaces+numEdges+numVertices);CHKERRQ(ierr);
522     for (c = 0; c < numCells; c++) {
523       ierr = DMPlexSetConeSize(dm, c, 6);CHKERRQ(ierr);
524     }
525     for (f = firstXFace; f < firstXFace+numFaces; ++f) {
526       ierr = DMPlexSetConeSize(dm, f, 4);CHKERRQ(ierr);
527     }
528     for (e = firstXEdge; e < firstXEdge+numEdges; ++e) {
529       ierr = DMPlexSetConeSize(dm, e, 2);CHKERRQ(ierr);
530     }
531     ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
532     /* Build cells */
533     for (fz = 0; fz < numZEdges; ++fz) {
534       for (fy = 0; fy < numYEdges; ++fy) {
535         for (fx = 0; fx < numXEdges; ++fx) {
536           PetscInt cell    = (fz*numYEdges + fy)*numXEdges + fx;
537           PetscInt faceB   = firstZFace + (fy*numXEdges+fx)*numZVertices +   fz;
538           PetscInt faceT   = firstZFace + (fy*numXEdges+fx)*numZVertices + ((fz+1)%numZVertices);
539           PetscInt faceF   = firstYFace + (fz*numXEdges+fx)*numYVertices +   fy;
540           PetscInt faceK   = firstYFace + (fz*numXEdges+fx)*numYVertices + ((fy+1)%numYVertices);
541           PetscInt faceL   = firstXFace + (fz*numYEdges+fy)*numXVertices +   fx;
542           PetscInt faceR   = firstXFace + (fz*numYEdges+fy)*numXVertices + ((fx+1)%numXVertices);
543                             /* B,  T,  F,  K,  R,  L */
544           PetscInt ornt[8] = {-4,  0,  0, -1,  0, -4}; /* ??? */
545           PetscInt cone[8];
546 
547           /* no boundary twisting in 3D */
548           cone[0] = faceB; cone[1] = faceT; cone[2] = faceF; cone[3] = faceK; cone[4] = faceR; cone[5] = faceL;
549           ierr    = DMPlexSetCone(dm, cell, cone);CHKERRQ(ierr);
550           ierr    = DMPlexSetConeOrientation(dm, cell, ornt);CHKERRQ(ierr);
551         }
552       }
553     }
554     /* Build x faces */
555     for (fz = 0; fz < numZEdges; ++fz) {
556       for (fy = 0; fy < numYEdges; ++fy) {
557         for (fx = 0; fx < numXVertices; ++fx) {
558           PetscInt face    = firstXFace + (fz*numYEdges+fy)*numXVertices + fx;
559           PetscInt edgeL   = firstZEdge + (  fy*                 numXVertices+fx)*numZEdges + fz;
560           PetscInt edgeR   = firstZEdge + (((fy+1)%numYVertices)*numXVertices+fx)*numZEdges + fz;
561           PetscInt edgeB   = firstYEdge + (  fz*                 numXVertices+fx)*numYEdges + fy;
562           PetscInt edgeT   = firstYEdge + (((fz+1)%numZVertices)*numXVertices+fx)*numYEdges + fy;
563           PetscInt ornt[4] = {0, 0, -2, -2};
564           PetscInt cone[4];
565 
566           if (dim == 3) {
567             /* markers */
568             if (bdX != DM_BOUNDARY_PERIODIC) {
569               if (fx == numXVertices-1) {
570                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerRight);CHKERRQ(ierr);
571                 ierr = DMSetLabelValue(dm, "marker", face, markerRight);CHKERRQ(ierr);
572               }
573               else if (fx == 0) {
574                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerLeft);CHKERRQ(ierr);
575                 ierr = DMSetLabelValue(dm, "marker", face, markerLeft);CHKERRQ(ierr);
576               }
577             }
578           }
579           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
580           ierr    = DMPlexSetCone(dm, face, cone);CHKERRQ(ierr);
581           ierr    = DMPlexSetConeOrientation(dm, face, ornt);CHKERRQ(ierr);
582         }
583       }
584     }
585     /* Build y faces */
586     for (fz = 0; fz < numZEdges; ++fz) {
587       for (fx = 0; fx < numYEdges; ++fx) {
588         for (fy = 0; fy < numYVertices; ++fy) {
589           PetscInt face    = firstYFace + (fz*numXEdges+fx)*numYVertices + fy;
590           PetscInt edgeL   = firstZEdge + (fy*numXVertices+  fx                 )*numZEdges + fz;
591           PetscInt edgeR   = firstZEdge + (fy*numXVertices+((fx+1)%numXVertices))*numZEdges + fz;
592           PetscInt edgeB   = firstXEdge + (  fz                 *numYVertices+fy)*numXEdges + fx;
593           PetscInt edgeT   = firstXEdge + (((fz+1)%numZVertices)*numYVertices+fy)*numXEdges + fx;
594           PetscInt ornt[4] = {0, 0, -2, -2};
595           PetscInt cone[4];
596 
597           if (dim == 3) {
598             /* markers */
599             if (bdY != DM_BOUNDARY_PERIODIC) {
600               if (fy == numYVertices-1) {
601                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerBack);CHKERRQ(ierr);
602                 ierr = DMSetLabelValue(dm, "marker", face, markerBack);CHKERRQ(ierr);
603               }
604               else if (fy == 0) {
605                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerFront);CHKERRQ(ierr);
606                 ierr = DMSetLabelValue(dm, "marker", face, markerFront);CHKERRQ(ierr);
607               }
608             }
609           }
610           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
611           ierr    = DMPlexSetCone(dm, face, cone);CHKERRQ(ierr);
612           ierr    = DMPlexSetConeOrientation(dm, face, ornt);CHKERRQ(ierr);
613         }
614       }
615     }
616     /* Build z faces */
617     for (fy = 0; fy < numYEdges; ++fy) {
618       for (fx = 0; fx < numXEdges; ++fx) {
619         for (fz = 0; fz < numZVertices; fz++) {
620           PetscInt face    = firstZFace + (fy*numXEdges+fx)*numZVertices + fz;
621           PetscInt edgeL   = firstYEdge + (fz*numXVertices+  fx                 )*numYEdges + fy;
622           PetscInt edgeR   = firstYEdge + (fz*numXVertices+((fx+1)%numXVertices))*numYEdges + fy;
623           PetscInt edgeB   = firstXEdge + (fz*numYVertices+  fy                 )*numXEdges + fx;
624           PetscInt edgeT   = firstXEdge + (fz*numYVertices+((fy+1)%numYVertices))*numXEdges + fx;
625           PetscInt ornt[4] = {0, 0, -2, -2};
626           PetscInt cone[4];
627 
628           if (dim == 2) {
629             if (bdX == DM_BOUNDARY_TWIST && fx == numXEdges-1) {edgeR += numYEdges-1-2*fy; ornt[1] = -2;}
630             if (bdY == DM_BOUNDARY_TWIST && fy == numYEdges-1) {edgeT += numXEdges-1-2*fx; ornt[2] =  0;}
631           }
632           else {
633             /* markers */
634             if (bdZ != DM_BOUNDARY_PERIODIC) {
635               if (fz == numZVertices-1) {
636                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerTop);CHKERRQ(ierr);
637                 ierr = DMSetLabelValue(dm, "marker", face, markerTop);CHKERRQ(ierr);
638               }
639               else if (fz == 0) {
640                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerBottom);CHKERRQ(ierr);
641                 ierr = DMSetLabelValue(dm, "marker", face, markerBottom);CHKERRQ(ierr);
642               }
643             }
644           }
645           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
646           ierr    = DMPlexSetCone(dm, face, cone);CHKERRQ(ierr);
647           ierr    = DMPlexSetConeOrientation(dm, face, ornt);CHKERRQ(ierr);
648         }
649       }
650     }
651     /* Build Z edges*/
652     for (vy = 0; vy < numYVertices; vy++) {
653       for (vx = 0; vx < numXVertices; vx++) {
654         for (ez = 0; ez < numZEdges; ez++) {
655           const PetscInt edge    = firstZEdge  + (vy*numXVertices+vx)*numZEdges + ez;
656           const PetscInt vertexB = firstVertex + (  ez                 *numYVertices+vy)*numXVertices + vx;
657           const PetscInt vertexT = firstVertex + (((ez+1)%numZVertices)*numYVertices+vy)*numXVertices + vx;
658           PetscInt       cone[2];
659 
660           if (dim == 3) {
661             if (bdX != DM_BOUNDARY_PERIODIC) {
662               if (vx == numXVertices-1) {
663                 ierr = DMSetLabelValue(dm, "marker", edge, markerRight);CHKERRQ(ierr);
664               }
665               else if (vx == 0) {
666                 ierr = DMSetLabelValue(dm, "marker", edge, markerLeft);CHKERRQ(ierr);
667               }
668             }
669             if (bdY != DM_BOUNDARY_PERIODIC) {
670               if (vy == numYVertices-1) {
671                 ierr = DMSetLabelValue(dm, "marker", edge, markerBack);CHKERRQ(ierr);
672               }
673               else if (vy == 0) {
674                 ierr = DMSetLabelValue(dm, "marker", edge, markerFront);CHKERRQ(ierr);
675               }
676             }
677           }
678           cone[0] = vertexB; cone[1] = vertexT;
679           ierr = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr);
680         }
681       }
682     }
683     /* Build Y edges*/
684     for (vz = 0; vz < numZVertices; vz++) {
685       for (vx = 0; vx < numXVertices; vx++) {
686         for (ey = 0; ey < numYEdges; ey++) {
687           const PetscInt nextv   = (dim == 2 && bdY == DM_BOUNDARY_TWIST && ey == numYEdges-1) ? (numXVertices-vx-1) : (vz*numYVertices+((ey+1)%numYVertices))*numXVertices + vx;
688           const PetscInt edge    = firstYEdge  + (vz*numXVertices+vx)*numYEdges + ey;
689           const PetscInt vertexF = firstVertex + (vz*numYVertices+ey)*numXVertices + vx;
690           const PetscInt vertexK = firstVertex + nextv;
691           PetscInt       cone[2];
692 
693           cone[0] = vertexF; cone[1] = vertexK;
694           ierr = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr);
695           if (dim == 2) {
696             if ((bdX != DM_BOUNDARY_PERIODIC) && (bdX != DM_BOUNDARY_TWIST)) {
697               if (vx == numXVertices-1) {
698                 ierr = DMSetLabelValue(dm, "Face Sets", edge, faceMarkerRight);CHKERRQ(ierr);
699                 ierr = DMSetLabelValue(dm, "marker", edge,    markerRight);CHKERRQ(ierr);
700                 ierr = DMSetLabelValue(dm, "marker", cone[0], markerRight);CHKERRQ(ierr);
701                 if (ey == numYEdges-1) {
702                   ierr = DMSetLabelValue(dm, "marker", cone[1], markerRight);CHKERRQ(ierr);
703                 }
704               }
705               else if (vx == 0) {
706                 ierr = DMSetLabelValue(dm, "Face Sets", edge, faceMarkerLeft);CHKERRQ(ierr);
707                 ierr = DMSetLabelValue(dm, "marker", edge,    markerLeft);CHKERRQ(ierr);
708                 ierr = DMSetLabelValue(dm, "marker", cone[0], markerLeft);CHKERRQ(ierr);
709                 if (ey == numYEdges-1) {
710                   ierr = DMSetLabelValue(dm, "marker", cone[1], markerLeft);CHKERRQ(ierr);
711                 }
712               }
713             }
714           }
715           else {
716             if (bdX != DM_BOUNDARY_PERIODIC) {
717               if (vx == numXVertices-1) {
718                 ierr = DMSetLabelValue(dm, "marker", edge, markerRight);CHKERRQ(ierr);
719               }
720               else if (vx == 0) {
721                 ierr = DMSetLabelValue(dm, "marker", edge, markerLeft);CHKERRQ(ierr);
722               }
723             }
724             if (bdZ != DM_BOUNDARY_PERIODIC) {
725               if (vz == numZVertices-1) {
726                 ierr = DMSetLabelValue(dm, "marker", edge, markerTop);CHKERRQ(ierr);
727               }
728               else if (vz == 0) {
729                 ierr = DMSetLabelValue(dm, "marker", edge, markerBottom);CHKERRQ(ierr);
730               }
731             }
732           }
733         }
734       }
735     }
736     /* Build X edges*/
737     for (vz = 0; vz < numZVertices; vz++) {
738       for (vy = 0; vy < numYVertices; vy++) {
739         for (ex = 0; ex < numXEdges; ex++) {
740           const PetscInt nextv   = (dim == 2 && bdX == DM_BOUNDARY_TWIST && ex == numXEdges-1) ? (numYVertices-vy-1)*numXVertices : (vz*numYVertices+vy)*numXVertices + (ex+1)%numXVertices;
741           const PetscInt edge    = firstXEdge  + (vz*numYVertices+vy)*numXEdges + ex;
742           const PetscInt vertexL = firstVertex + (vz*numYVertices+vy)*numXVertices + ex;
743           const PetscInt vertexR = firstVertex + nextv;
744           PetscInt       cone[2];
745 
746           cone[0] = vertexL; cone[1] = vertexR;
747           ierr = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr);
748           if (dim == 2) {
749             if ((bdY != DM_BOUNDARY_PERIODIC) && (bdY != DM_BOUNDARY_TWIST)) {
750               if (vy == numYVertices-1) {
751                 ierr = DMSetLabelValue(dm, "Face Sets", edge, faceMarkerTop);CHKERRQ(ierr);
752                 ierr = DMSetLabelValue(dm, "marker", edge,    markerTop);CHKERRQ(ierr);
753                 ierr = DMSetLabelValue(dm, "marker", cone[0], markerTop);CHKERRQ(ierr);
754                 if (ex == numXEdges-1) {
755                   ierr = DMSetLabelValue(dm, "marker", cone[1], markerTop);CHKERRQ(ierr);
756                 }
757               }
758               else if (vy == 0) {
759                 ierr = DMSetLabelValue(dm, "Face Sets", edge, faceMarkerBottom);CHKERRQ(ierr);
760                 ierr = DMSetLabelValue(dm, "marker", edge,    markerBottom);CHKERRQ(ierr);
761                 ierr = DMSetLabelValue(dm, "marker", cone[0], markerBottom);CHKERRQ(ierr);
762                 if (ex == numXEdges-1) {
763                   ierr = DMSetLabelValue(dm, "marker", cone[1], markerBottom);CHKERRQ(ierr);
764                 }
765               }
766             }
767           }
768           else {
769             if (bdY != DM_BOUNDARY_PERIODIC) {
770               if (vy == numYVertices-1) {
771                 ierr = DMSetLabelValue(dm, "marker", edge, markerBack);CHKERRQ(ierr);
772               }
773               else if (vy == 0) {
774                 ierr = DMSetLabelValue(dm, "marker", edge, markerFront);CHKERRQ(ierr);
775               }
776             }
777             if (bdZ != DM_BOUNDARY_PERIODIC) {
778               if (vz == numZVertices-1) {
779                 ierr = DMSetLabelValue(dm, "marker", edge, markerTop);CHKERRQ(ierr);
780               }
781               else if (vz == 0) {
782                 ierr = DMSetLabelValue(dm, "marker", edge, markerBottom);CHKERRQ(ierr);
783               }
784             }
785           }
786         }
787       }
788     }
789     ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
790     ierr = DMPlexStratify(dm);CHKERRQ(ierr);
791     /* Build coordinates */
792     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
793     ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
794     ierr = PetscSectionSetFieldComponents(coordSection, 0, dim);CHKERRQ(ierr);
795     ierr = PetscSectionSetChart(coordSection, firstVertex, firstVertex+numVertices);CHKERRQ(ierr);
796     for (v = firstVertex; v < firstVertex+numVertices; ++v) {
797       ierr = PetscSectionSetDof(coordSection, v, dim);CHKERRQ(ierr);
798       ierr = PetscSectionSetFieldDof(coordSection, v, 0, dim);CHKERRQ(ierr);
799     }
800     ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
801     ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
802     ierr = VecCreate(PetscObjectComm((PetscObject)dm), &coordinates);CHKERRQ(ierr);
803     ierr = VecSetBlockSize(coordinates, dim);CHKERRQ(ierr);
804     ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
805     ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
806     ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
807     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
808     for (vz = 0; vz < numZVertices; ++vz) {
809       for (vy = 0; vy < numYVertices; ++vy) {
810         for (vx = 0; vx < numXVertices; ++vx) {
811           coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+0] = lower[0] + ((upper[0] - lower[0])/numXEdges)*vx;
812           coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+1] = lower[1] + ((upper[1] - lower[1])/numYEdges)*vy;
813           if (dim == 3) {
814             coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+2] = lower[2] + ((upper[2] - lower[2])/numZEdges)*vz;
815           }
816         }
817       }
818     }
819     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
820     ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
821     ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
822   }
823   PetscFunctionReturn(0);
824 }
825 
826 #undef __FUNCT__
827 #define __FUNCT__ "DMPlexCreateSquareMesh"
828 /*@
829   DMPlexCreateSquareMesh - Creates a 2D mesh for a square lattice.
830 
831   Collective on MPI_Comm
832 
833   Input Parameters:
834 + comm  - The communicator for the DM object
835 . lower - The lower left corner coordinates
836 . upper - The upper right corner coordinates
837 . edges - The number of cells in each direction
838 . bdX   - The boundary type for the X direction
839 - bdY   - The boundary type for the Y direction
840 
841   Output Parameter:
842 . dm  - The DM object
843 
844   Note: Here is the numbering returned for 2 cells in each direction:
845 $ 22--8-23--9--24
846 $  |     |     |
847 $ 13  2 14  3  15
848 $  |     |     |
849 $ 19--6-20--7--21
850 $  |     |     |
851 $ 10  0 11  1 12
852 $  |     |     |
853 $ 16--4-17--5--18
854 
855   Level: beginner
856 
857 .keywords: DM, create
858 .seealso: DMPlexCreateBoxMesh(), DMPlexCreateSquareBoundary(), DMPlexCreateCubeBoundary(), DMSetType(), DMCreate()
859 @*/
860 PetscErrorCode DMPlexCreateSquareMesh(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY)
861 {
862   PetscReal      lower3[3], upper3[3];
863   PetscInt       edges3[3];
864   PetscErrorCode ierr;
865 
866   PetscFunctionBegin;
867   lower3[0] = lower[0]; lower3[1] = lower[1]; lower3[2] = 0.;
868   upper3[0] = upper[0]; upper3[1] = upper[1]; upper3[2] = 0.;
869   edges3[0] = edges[0]; edges3[1] = edges[1]; edges3[2] = 0;
870   ierr = DMPlexCreateCubeMesh_Internal(dm, lower3, upper3, edges3, bdX, bdY, DM_BOUNDARY_NONE);CHKERRQ(ierr);
871   PetscFunctionReturn(0);
872 }
873 
874 #undef __FUNCT__
875 #define __FUNCT__ "DMPlexCreateBoxMesh"
876 /*@
877   DMPlexCreateBoxMesh - Creates a mesh on the tensor product of unit intervals (box) using simplices.
878 
879   Collective on MPI_Comm
880 
881   Input Parameters:
882 + comm - The communicator for the DM object
883 . dim - The spatial dimension
884 - interpolate - Flag to create intermediate mesh pieces (edges, faces)
885 
886   Output Parameter:
887 . dm  - The DM object
888 
889   Level: beginner
890 
891 .keywords: DM, create
892 .seealso: DMPlexCreateHexBoxMesh(), DMSetType(), DMCreate()
893 @*/
894 PetscErrorCode DMPlexCreateBoxMesh(MPI_Comm comm, PetscInt dim, PetscBool interpolate, DM *dm)
895 {
896   DM             boundary;
897   PetscErrorCode ierr;
898 
899   PetscFunctionBegin;
900   PetscValidPointer(dm, 4);
901   ierr = DMCreate(comm, &boundary);CHKERRQ(ierr);
902   PetscValidLogicalCollectiveInt(boundary,dim,2);
903   ierr = DMSetType(boundary, DMPLEX);CHKERRQ(ierr);
904   ierr = DMSetDimension(boundary, dim-1);CHKERRQ(ierr);
905   switch (dim) {
906   case 2:
907   {
908     PetscReal lower[2] = {0.0, 0.0};
909     PetscReal upper[2] = {1.0, 1.0};
910     PetscInt  edges[2] = {2, 2};
911 
912     ierr = DMPlexCreateSquareBoundary(boundary, lower, upper, edges);CHKERRQ(ierr);
913     break;
914   }
915   case 3:
916   {
917     PetscReal lower[3] = {0.0, 0.0, 0.0};
918     PetscReal upper[3] = {1.0, 1.0, 1.0};
919     PetscInt  faces[3] = {1, 1, 1};
920 
921     ierr = DMPlexCreateCubeBoundary(boundary, lower, upper, faces);CHKERRQ(ierr);
922     break;
923   }
924   default:
925     SETERRQ1(comm, PETSC_ERR_SUP, "Dimension not supported: %d", dim);
926   }
927   ierr = DMPlexGenerate(boundary, NULL, interpolate, dm);CHKERRQ(ierr);
928   ierr = DMDestroy(&boundary);CHKERRQ(ierr);
929   PetscFunctionReturn(0);
930 }
931 
932 #undef __FUNCT__
933 #define __FUNCT__ "DMPlexCreateHexBoxMesh"
934 /*@
935   DMPlexCreateHexBoxMesh - Creates a mesh on the tensor product of unit intervals (box) using hexahedra.
936 
937   Collective on MPI_Comm
938 
939   Input Parameters:
940 + comm  - The communicator for the DM object
941 . dim   - The spatial dimension
942 . periodicX - The boundary type for the X direction
943 . periodicY - The boundary type for the Y direction
944 . periodicZ - The boundary type for the Z direction
945 - cells - The number of cells in each direction
946 
947   Output Parameter:
948 . dm  - The DM object
949 
950   Level: beginner
951 
952 .keywords: DM, create
953 .seealso: DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
954 @*/
955 PetscErrorCode DMPlexCreateHexBoxMesh(MPI_Comm comm, PetscInt dim, const PetscInt cells[], DMBoundaryType periodicX, DMBoundaryType periodicY, DMBoundaryType periodicZ, DM *dm)
956 {
957   PetscErrorCode ierr;
958 
959   PetscFunctionBegin;
960   PetscValidPointer(dm, 4);
961   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
962   PetscValidLogicalCollectiveInt(*dm,dim,2);
963   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
964   ierr = DMSetDimension(*dm, dim);CHKERRQ(ierr);
965   switch (dim) {
966   case 2:
967   {
968     PetscReal lower[2] = {0.0, 0.0};
969     PetscReal upper[2] = {1.0, 1.0};
970 
971     ierr = DMPlexCreateSquareMesh(*dm, lower, upper, cells, periodicX, periodicY);CHKERRQ(ierr);
972     break;
973   }
974   case 3:
975   {
976     PetscReal lower[3] = {0.0, 0.0, 0.0};
977     PetscReal upper[3] = {1.0, 1.0, 1.0};
978 
979     ierr = DMPlexCreateCubeMesh_Internal(*dm, lower, upper, cells, periodicX, periodicY, periodicZ);CHKERRQ(ierr);
980     break;
981   }
982   default:
983     SETERRQ1(comm, PETSC_ERR_SUP, "Dimension not supported: %d", dim);
984   }
985   PetscFunctionReturn(0);
986 }
987 
988 /* External function declarations here */
989 extern PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling);
990 extern PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat);
991 extern PetscErrorCode DMCreateDefaultSection_Plex(DM dm);
992 extern PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm);
993 extern PetscErrorCode DMCreateMatrix_Plex(DM dm,  Mat *J);
994 extern PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm);
995 extern PetscErrorCode DMRefine_Plex(DM dm, MPI_Comm comm, DM *dmRefined);
996 extern PetscErrorCode DMCoarsen_Plex(DM dm, MPI_Comm comm, DM *dmCoarsened);
997 extern PetscErrorCode DMRefineHierarchy_Plex(DM dm, PetscInt nlevels, DM dmRefined[]);
998 extern PetscErrorCode DMCoarsenHierarchy_Plex(DM dm, PetscInt nlevels, DM dmCoarsened[]);
999 extern PetscErrorCode DMClone_Plex(DM dm, DM *newdm);
1000 extern PetscErrorCode DMSetUp_Plex(DM dm);
1001 extern PetscErrorCode DMDestroy_Plex(DM dm);
1002 extern PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer);
1003 extern PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer);
1004 extern PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm);
1005 extern PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, PetscSF cellSF);
1006 extern PetscErrorCode DMProjectFunctionLocal_Plex(DM,PetscReal,PetscErrorCode(**)(PetscInt,PetscReal,const PetscReal[],PetscInt,PetscScalar *,void *),void **,InsertMode,Vec);
1007 extern PetscErrorCode DMProjectFunctionLabelLocal_Plex(DM,PetscReal,DMLabel,PetscInt,const PetscInt[],PetscErrorCode(**)(PetscInt,PetscReal,const PetscReal[],PetscInt,PetscScalar *,void *),void **,InsertMode,Vec);
1008 extern PetscErrorCode DMProjectFieldLocal_Plex(DM,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);
1009 extern PetscErrorCode DMComputeL2Diff_Plex(DM,PetscReal,PetscErrorCode(**)(PetscInt,PetscReal,const PetscReal[],PetscInt,PetscScalar *,void *),void **,Vec,PetscReal *);
1010 extern PetscErrorCode DMComputeL2GradientDiff_Plex(DM,PetscReal,PetscErrorCode(**)(PetscInt,PetscReal,const PetscReal[], const PetscReal[],PetscInt,PetscScalar *,void *),void **,Vec,const PetscReal [],PetscReal *);
1011 extern PetscErrorCode DMComputeL2FieldDiff_Plex(DM,PetscReal,PetscErrorCode(**)(PetscInt,PetscReal,const PetscReal[],PetscInt,PetscScalar *,void *),void **,Vec,PetscReal *);
1012 
1013 #undef __FUNCT__
1014 #define __FUNCT__ "DMPlexReplace_Static"
1015 /* Replace dm with the contents of dmNew
1016    - Share the DM_Plex structure
1017    - Share the coordinates
1018    - Share the SF
1019 */
1020 static PetscErrorCode DMPlexReplace_Static(DM dm, DM dmNew)
1021 {
1022   PetscSF          sf;
1023   DM               coordDM, coarseDM;
1024   Vec              coords;
1025   const PetscReal *maxCell, *L;
1026   const DMBoundaryType *bd;
1027   PetscErrorCode   ierr;
1028 
1029   PetscFunctionBegin;
1030   ierr = DMGetPointSF(dmNew, &sf);CHKERRQ(ierr);
1031   ierr = DMSetPointSF(dm, sf);CHKERRQ(ierr);
1032   ierr = DMGetCoordinateDM(dmNew, &coordDM);CHKERRQ(ierr);
1033   ierr = DMGetCoordinatesLocal(dmNew, &coords);CHKERRQ(ierr);
1034   ierr = DMSetCoordinateDM(dm, coordDM);CHKERRQ(ierr);
1035   ierr = DMSetCoordinatesLocal(dm, coords);CHKERRQ(ierr);
1036   ierr = DMGetPeriodicity(dm, &maxCell, &L, &bd);CHKERRQ(ierr);
1037   if (L) {ierr = DMSetPeriodicity(dmNew, maxCell, L, bd);CHKERRQ(ierr);}
1038   ierr = DMDestroy_Plex(dm);CHKERRQ(ierr);
1039   dm->data = dmNew->data;
1040   ((DM_Plex *) dmNew->data)->refct++;
1041   dmNew->labels->refct++;
1042   if (!--(dm->labels->refct)) {
1043     DMLabelLink next = dm->labels->next;
1044 
1045     /* destroy the labels */
1046     while (next) {
1047       DMLabelLink tmp = next->next;
1048 
1049       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
1050       ierr = PetscFree(next);CHKERRQ(ierr);
1051       next = tmp;
1052     }
1053     ierr = PetscFree(dm->labels);CHKERRQ(ierr);
1054   }
1055   dm->labels = dmNew->labels;
1056   dm->depthLabel = dmNew->depthLabel;
1057   ierr = DMGetCoarseDM(dmNew,&coarseDM);CHKERRQ(ierr);
1058   ierr = DMSetCoarseDM(dm,coarseDM);CHKERRQ(ierr);
1059   PetscFunctionReturn(0);
1060 }
1061 
1062 #undef __FUNCT__
1063 #define __FUNCT__ "DMPlexSwap_Static"
1064 /* Swap dm with the contents of dmNew
1065    - Swap the DM_Plex structure
1066    - Swap the coordinates
1067    - Swap the point PetscSF
1068 */
1069 static PetscErrorCode DMPlexSwap_Static(DM dmA, DM dmB)
1070 {
1071   DM              coordDMA, coordDMB;
1072   Vec             coordsA,  coordsB;
1073   PetscSF         sfA,      sfB;
1074   void            *tmp;
1075   DMLabelLinkList listTmp;
1076   DMLabel         depthTmp;
1077   PetscErrorCode  ierr;
1078 
1079   PetscFunctionBegin;
1080   ierr = DMGetPointSF(dmA, &sfA);CHKERRQ(ierr);
1081   ierr = DMGetPointSF(dmB, &sfB);CHKERRQ(ierr);
1082   ierr = PetscObjectReference((PetscObject) sfA);CHKERRQ(ierr);
1083   ierr = DMSetPointSF(dmA, sfB);CHKERRQ(ierr);
1084   ierr = DMSetPointSF(dmB, sfA);CHKERRQ(ierr);
1085   ierr = PetscObjectDereference((PetscObject) sfA);CHKERRQ(ierr);
1086 
1087   ierr = DMGetCoordinateDM(dmA, &coordDMA);CHKERRQ(ierr);
1088   ierr = DMGetCoordinateDM(dmB, &coordDMB);CHKERRQ(ierr);
1089   ierr = PetscObjectReference((PetscObject) coordDMA);CHKERRQ(ierr);
1090   ierr = DMSetCoordinateDM(dmA, coordDMB);CHKERRQ(ierr);
1091   ierr = DMSetCoordinateDM(dmB, coordDMA);CHKERRQ(ierr);
1092   ierr = PetscObjectDereference((PetscObject) coordDMA);CHKERRQ(ierr);
1093 
1094   ierr = DMGetCoordinatesLocal(dmA, &coordsA);CHKERRQ(ierr);
1095   ierr = DMGetCoordinatesLocal(dmB, &coordsB);CHKERRQ(ierr);
1096   ierr = PetscObjectReference((PetscObject) coordsA);CHKERRQ(ierr);
1097   ierr = DMSetCoordinatesLocal(dmA, coordsB);CHKERRQ(ierr);
1098   ierr = DMSetCoordinatesLocal(dmB, coordsA);CHKERRQ(ierr);
1099   ierr = PetscObjectDereference((PetscObject) coordsA);CHKERRQ(ierr);
1100 
1101   tmp       = dmA->data;
1102   dmA->data = dmB->data;
1103   dmB->data = tmp;
1104   listTmp   = dmA->labels;
1105   dmA->labels = dmB->labels;
1106   dmB->labels = listTmp;
1107   depthTmp  = dmA->depthLabel;
1108   dmA->depthLabel = dmB->depthLabel;
1109   dmB->depthLabel = depthTmp;
1110   PetscFunctionReturn(0);
1111 }
1112 
1113 #undef __FUNCT__
1114 #define __FUNCT__ "DMSetFromOptions_NonRefinement_Plex"
1115 PetscErrorCode  DMSetFromOptions_NonRefinement_Plex(PetscOptionItems *PetscOptionsObject,DM dm)
1116 {
1117   DM_Plex       *mesh = (DM_Plex*) dm->data;
1118   DMBoundary     b;
1119   PetscErrorCode ierr;
1120 
1121   PetscFunctionBegin;
1122   /* Handle boundary conditions */
1123   ierr = PetscOptionsBegin(PetscObjectComm((PetscObject) dm), NULL, "Boundary condition options", "");CHKERRQ(ierr);
1124   for (b = dm->boundary->next; b; b = b->next) {
1125     char      optname[1024];
1126     PetscInt  ids[1024], len = 1024, i;
1127     PetscBool flg;
1128 
1129     ierr = PetscSNPrintf(optname, sizeof(optname), "-bc_%s", b->name);CHKERRQ(ierr);
1130     ierr = PetscMemzero(ids, sizeof(ids));CHKERRQ(ierr);
1131     ierr = PetscOptionsIntArray(optname, "List of boundary IDs", "", ids, &len, &flg);CHKERRQ(ierr);
1132     if (flg) {
1133       DMLabel label;
1134 
1135       ierr = DMGetLabel(dm, b->labelname, &label);CHKERRQ(ierr);
1136       for (i = 0; i < len; ++i) {
1137         PetscBool has;
1138 
1139         ierr = DMLabelHasValue(label, ids[i], &has);CHKERRQ(ierr);
1140         if (!has) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Boundary id %D is not present in the label %s", ids[i], b->name);
1141       }
1142       b->numids = len;
1143       ierr = PetscFree(b->ids);CHKERRQ(ierr);
1144       ierr = PetscMalloc1(len, &b->ids);CHKERRQ(ierr);
1145       ierr = PetscMemcpy(b->ids, ids, len*sizeof(PetscInt));CHKERRQ(ierr);
1146     }
1147     ierr = PetscSNPrintf(optname, sizeof(optname), "-bc_%s_comp", b->name);CHKERRQ(ierr);
1148     ierr = PetscMemzero(ids, sizeof(ids));CHKERRQ(ierr);
1149     ierr = PetscOptionsIntArray(optname, "List of boundary field components", "", ids, &len, &flg);CHKERRQ(ierr);
1150     if (flg) {
1151       b->numcomps = len;
1152       ierr = PetscFree(b->comps);CHKERRQ(ierr);
1153       ierr = PetscMalloc1(len, &b->comps);CHKERRQ(ierr);
1154       ierr = PetscMemcpy(b->comps, ids, len*sizeof(PetscInt));CHKERRQ(ierr);
1155     }
1156   }
1157   ierr = PetscOptionsEnd();CHKERRQ(ierr);
1158   /* Handle viewing */
1159   ierr = PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMView", PETSC_FALSE, &mesh->printSetValues, NULL);CHKERRQ(ierr);
1160   ierr = PetscOptionsInt("-dm_plex_print_fem", "Debug output level all fem computations", "DMView", 0, &mesh->printFEM, NULL);CHKERRQ(ierr);
1161   ierr = PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMView", mesh->printTol, &mesh->printTol, NULL);CHKERRQ(ierr);
1162   /* Point Location */
1163   ierr = PetscOptionsBool("-dm_plex_hash_location", "Use grid hashing for point location", "DMView", PETSC_FALSE, &mesh->useHashLocation, NULL);CHKERRQ(ierr);
1164   /* Projection behavior */
1165   ierr = PetscOptionsInt("-dm_plex_max_projection_height", "Maxmimum mesh point height used to project locally", "DMPlexSetMaxProjectionHeight", 0, &mesh->maxProjectionHeight, NULL);CHKERRQ(ierr);
1166   ierr = PetscOptionsBool("-dm_plex_regular_refinement", "Use special nested projection algorithm for regular refinement", "DMPlexSetRegularRefinement", mesh->regularRefinement, &mesh->regularRefinement, NULL);CHKERRQ(ierr);
1167   PetscFunctionReturn(0);
1168 }
1169 
1170 #undef __FUNCT__
1171 #define __FUNCT__ "DMSetFromOptions_Plex"
1172 PetscErrorCode  DMSetFromOptions_Plex(PetscOptionItems *PetscOptionsObject,DM dm)
1173 {
1174   PetscInt       refine = 0, coarsen = 0, r;
1175   PetscBool      isHierarchy;
1176   PetscErrorCode ierr;
1177 
1178   PetscFunctionBegin;
1179   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1180   ierr = PetscOptionsHead(PetscOptionsObject,"DMPlex Options");CHKERRQ(ierr);
1181   /* Handle DMPlex refinement */
1182   ierr = PetscOptionsInt("-dm_refine", "The number of uniform refinements", "DMCreate", refine, &refine, NULL);CHKERRQ(ierr);
1183   ierr = PetscOptionsInt("-dm_refine_hierarchy", "The number of uniform refinements", "DMCreate", refine, &refine, &isHierarchy);CHKERRQ(ierr);
1184   if (refine) {ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE);CHKERRQ(ierr);}
1185   if (refine && isHierarchy) {
1186     DM *dms, coarseDM;
1187 
1188     ierr = DMGetCoarseDM(dm, &coarseDM);CHKERRQ(ierr);
1189     ierr = PetscObjectReference((PetscObject)coarseDM);CHKERRQ(ierr);
1190     ierr = PetscMalloc1(refine,&dms);CHKERRQ(ierr);
1191     ierr = DMRefineHierarchy(dm, refine, dms);CHKERRQ(ierr);
1192     /* Total hack since we do not pass in a pointer */
1193     ierr = DMPlexSwap_Static(dm, dms[refine-1]);CHKERRQ(ierr);
1194     if (refine == 1) {
1195       ierr = DMSetCoarseDM(dm, dms[0]);CHKERRQ(ierr);
1196       ierr = DMPlexSetRegularRefinement(dm, PETSC_TRUE);CHKERRQ(ierr);
1197     } else {
1198       ierr = DMSetCoarseDM(dm, dms[refine-2]);CHKERRQ(ierr);
1199       ierr = DMPlexSetRegularRefinement(dm, PETSC_TRUE);CHKERRQ(ierr);
1200       ierr = DMSetCoarseDM(dms[0], dms[refine-1]);CHKERRQ(ierr);
1201       ierr = DMPlexSetRegularRefinement(dms[0], PETSC_TRUE);CHKERRQ(ierr);
1202     }
1203     ierr = DMSetCoarseDM(dms[refine-1], coarseDM);CHKERRQ(ierr);
1204     ierr = PetscObjectDereference((PetscObject)coarseDM);CHKERRQ(ierr);
1205     /* Free DMs */
1206     for (r = 0; r < refine; ++r) {
1207       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]);CHKERRQ(ierr);
1208       ierr = DMDestroy(&dms[r]);CHKERRQ(ierr);
1209     }
1210     ierr = PetscFree(dms);CHKERRQ(ierr);
1211   } else {
1212     for (r = 0; r < refine; ++r) {
1213       DM refinedMesh;
1214 
1215       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
1216       ierr = DMRefine(dm, PetscObjectComm((PetscObject) dm), &refinedMesh);CHKERRQ(ierr);
1217       /* Total hack since we do not pass in a pointer */
1218       ierr = DMPlexReplace_Static(dm, refinedMesh);CHKERRQ(ierr);
1219       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
1220       ierr = DMDestroy(&refinedMesh);CHKERRQ(ierr);
1221     }
1222   }
1223   /* Handle DMPlex coarsening */
1224   ierr = PetscOptionsInt("-dm_coarsen", "Coarsen the mesh", "DMCreate", coarsen, &coarsen, NULL);CHKERRQ(ierr);
1225   ierr = PetscOptionsInt("-dm_coarsen_hierarchy", "The number of coarsenings", "DMCreate", coarsen, &coarsen, &isHierarchy);CHKERRQ(ierr);
1226   if (coarsen && isHierarchy) {
1227     DM *dms;
1228 
1229     ierr = PetscMalloc1(coarsen, &dms);CHKERRQ(ierr);
1230     ierr = DMCoarsenHierarchy(dm, coarsen, dms);CHKERRQ(ierr);
1231     /* Free DMs */
1232     for (r = 0; r < coarsen; ++r) {
1233       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]);CHKERRQ(ierr);
1234       ierr = DMDestroy(&dms[r]);CHKERRQ(ierr);
1235     }
1236     ierr = PetscFree(dms);CHKERRQ(ierr);
1237   } else {
1238     for (r = 0; r < coarsen; ++r) {
1239       DM coarseMesh;
1240 
1241       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
1242       ierr = DMCoarsen(dm, PetscObjectComm((PetscObject) dm), &coarseMesh);CHKERRQ(ierr);
1243       /* Total hack since we do not pass in a pointer */
1244       ierr = DMPlexReplace_Static(dm, coarseMesh);CHKERRQ(ierr);
1245       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
1246       ierr = DMDestroy(&coarseMesh);CHKERRQ(ierr);
1247     }
1248   }
1249   /* Handle */
1250   ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
1251   ierr = PetscOptionsTail();CHKERRQ(ierr);
1252   PetscFunctionReturn(0);
1253 }
1254 
1255 #undef __FUNCT__
1256 #define __FUNCT__ "DMCreateGlobalVector_Plex"
1257 static PetscErrorCode DMCreateGlobalVector_Plex(DM dm,Vec *vec)
1258 {
1259   PetscErrorCode ierr;
1260 
1261   PetscFunctionBegin;
1262   ierr = DMCreateGlobalVector_Section_Private(dm,vec);CHKERRQ(ierr);
1263   /* ierr = VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM);CHKERRQ(ierr); */
1264   ierr = VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex);CHKERRQ(ierr);
1265   ierr = VecSetOperation(*vec, VECOP_VIEWNATIVE, (void (*)(void)) VecView_Plex_Native);CHKERRQ(ierr);
1266   ierr = VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex);CHKERRQ(ierr);
1267   ierr = VecSetOperation(*vec, VECOP_LOADNATIVE, (void (*)(void)) VecLoad_Plex_Native);CHKERRQ(ierr);
1268   PetscFunctionReturn(0);
1269 }
1270 
1271 #undef __FUNCT__
1272 #define __FUNCT__ "DMCreateLocalVector_Plex"
1273 static PetscErrorCode DMCreateLocalVector_Plex(DM dm,Vec *vec)
1274 {
1275   PetscErrorCode ierr;
1276 
1277   PetscFunctionBegin;
1278   ierr = DMCreateLocalVector_Section_Private(dm,vec);CHKERRQ(ierr);
1279   ierr = VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex_Local);CHKERRQ(ierr);
1280   ierr = VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex_Local);CHKERRQ(ierr);
1281   PetscFunctionReturn(0);
1282 }
1283 
1284 #undef __FUNCT__
1285 #define __FUNCT__ "DMGetDimPoints_Plex"
1286 static PetscErrorCode DMGetDimPoints_Plex(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
1287 {
1288   PetscInt       depth, d;
1289   PetscErrorCode ierr;
1290 
1291   PetscFunctionBegin;
1292   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1293   if (depth == 1) {
1294     ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
1295     if (dim == 0)      {ierr = DMPlexGetDepthStratum(dm, dim, pStart, pEnd);CHKERRQ(ierr);}
1296     else if (dim == d) {ierr = DMPlexGetDepthStratum(dm, 1, pStart, pEnd);CHKERRQ(ierr);}
1297     else               {*pStart = 0; *pEnd = 0;}
1298   } else {
1299     ierr = DMPlexGetDepthStratum(dm, dim, pStart, pEnd);CHKERRQ(ierr);
1300   }
1301   PetscFunctionReturn(0);
1302 }
1303 
1304 #undef __FUNCT__
1305 #define __FUNCT__ "DMInitialize_Plex"
1306 PetscErrorCode DMInitialize_Plex(DM dm)
1307 {
1308   PetscFunctionBegin;
1309   dm->ops->view                            = DMView_Plex;
1310   dm->ops->load                            = DMLoad_Plex;
1311   dm->ops->setfromoptions                  = DMSetFromOptions_Plex;
1312   dm->ops->clone                           = DMClone_Plex;
1313   dm->ops->setup                           = DMSetUp_Plex;
1314   dm->ops->createdefaultsection            = DMCreateDefaultSection_Plex;
1315   dm->ops->createdefaultconstraints        = DMCreateDefaultConstraints_Plex;
1316   dm->ops->createglobalvector              = DMCreateGlobalVector_Plex;
1317   dm->ops->createlocalvector               = DMCreateLocalVector_Plex;
1318   dm->ops->getlocaltoglobalmapping         = NULL;
1319   dm->ops->createfieldis                   = NULL;
1320   dm->ops->createcoordinatedm              = DMCreateCoordinateDM_Plex;
1321   dm->ops->getcoloring                     = NULL;
1322   dm->ops->creatematrix                    = DMCreateMatrix_Plex;
1323   dm->ops->createinterpolation             = DMCreateInterpolation_Plex;
1324   dm->ops->getaggregates                   = NULL;
1325   dm->ops->getinjection                    = DMCreateInjection_Plex;
1326   dm->ops->refine                          = DMRefine_Plex;
1327   dm->ops->coarsen                         = DMCoarsen_Plex;
1328   dm->ops->refinehierarchy                 = DMRefineHierarchy_Plex;
1329   dm->ops->coarsenhierarchy                = DMCoarsenHierarchy_Plex;
1330   dm->ops->globaltolocalbegin              = NULL;
1331   dm->ops->globaltolocalend                = NULL;
1332   dm->ops->localtoglobalbegin              = NULL;
1333   dm->ops->localtoglobalend                = NULL;
1334   dm->ops->destroy                         = DMDestroy_Plex;
1335   dm->ops->createsubdm                     = DMCreateSubDM_Plex;
1336   dm->ops->getdimpoints                    = DMGetDimPoints_Plex;
1337   dm->ops->locatepoints                    = DMLocatePoints_Plex;
1338   dm->ops->projectfunctionlocal            = DMProjectFunctionLocal_Plex;
1339   dm->ops->projectfunctionlabellocal       = DMProjectFunctionLabelLocal_Plex;
1340   dm->ops->projectfieldlocal               = DMProjectFieldLocal_Plex;
1341   dm->ops->computel2diff                   = DMComputeL2Diff_Plex;
1342   dm->ops->computel2gradientdiff           = DMComputeL2GradientDiff_Plex;
1343   dm->ops->computel2fielddiff              = DMComputeL2FieldDiff_Plex;
1344   PetscFunctionReturn(0);
1345 }
1346 
1347 #undef __FUNCT__
1348 #define __FUNCT__ "DMClone_Plex"
1349 PetscErrorCode DMClone_Plex(DM dm, DM *newdm)
1350 {
1351   DM_Plex        *mesh = (DM_Plex *) dm->data;
1352   PetscErrorCode ierr;
1353 
1354   PetscFunctionBegin;
1355   mesh->refct++;
1356   (*newdm)->data = mesh;
1357   ierr = PetscObjectChangeTypeName((PetscObject) *newdm, DMPLEX);CHKERRQ(ierr);
1358   ierr = DMInitialize_Plex(*newdm);CHKERRQ(ierr);
1359   PetscFunctionReturn(0);
1360 }
1361 
1362 /*MC
1363   DMPLEX = "plex" - A DM object that encapsulates an unstructured mesh, or CW Complex, which can be expressed using a Hasse Diagram.
1364                     In the local representation, Vecs contain all unknowns in the interior and shared boundary. This is
1365                     specified by a PetscSection object. Ownership in the global representation is determined by
1366                     ownership of the underlying DMPlex points. This is specified by another PetscSection object.
1367 
1368   Level: intermediate
1369 
1370 .seealso: DMType, DMPlexCreate(), DMCreate(), DMSetType()
1371 M*/
1372 
1373 #undef __FUNCT__
1374 #define __FUNCT__ "DMCreate_Plex"
1375 PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm)
1376 {
1377   DM_Plex        *mesh;
1378   PetscInt       unit, d;
1379   PetscErrorCode ierr;
1380 
1381   PetscFunctionBegin;
1382   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1383   ierr     = PetscNewLog(dm,&mesh);CHKERRQ(ierr);
1384   dm->dim  = 0;
1385   dm->data = mesh;
1386 
1387   mesh->refct             = 1;
1388   ierr                    = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection);CHKERRQ(ierr);
1389   mesh->maxConeSize       = 0;
1390   mesh->cones             = NULL;
1391   mesh->coneOrientations  = NULL;
1392   ierr                    = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection);CHKERRQ(ierr);
1393   mesh->maxSupportSize    = 0;
1394   mesh->supports          = NULL;
1395   mesh->refinementUniform = PETSC_TRUE;
1396   mesh->refinementLimit   = -1.0;
1397 
1398   mesh->facesTmp = NULL;
1399 
1400   mesh->tetgenOpts   = NULL;
1401   mesh->triangleOpts = NULL;
1402   ierr = PetscPartitionerCreate(PetscObjectComm((PetscObject)dm), &mesh->partitioner);CHKERRQ(ierr);
1403   ierr = PetscPartitionerSetTypeFromOptions_Internal(mesh->partitioner);CHKERRQ(ierr);
1404 
1405   mesh->subpointMap = NULL;
1406 
1407   for (unit = 0; unit < NUM_PETSC_UNITS; ++unit) mesh->scale[unit] = 1.0;
1408 
1409   mesh->regularRefinement   = PETSC_FALSE;
1410   mesh->depthState          = -1;
1411   mesh->globalVertexNumbers = NULL;
1412   mesh->globalCellNumbers   = NULL;
1413   mesh->anchorSection       = NULL;
1414   mesh->anchorIS            = NULL;
1415   mesh->createanchors       = NULL;
1416   mesh->computeanchormatrix = NULL;
1417   mesh->parentSection       = NULL;
1418   mesh->parents             = NULL;
1419   mesh->childIDs            = NULL;
1420   mesh->childSection        = NULL;
1421   mesh->children            = NULL;
1422   mesh->referenceTree       = NULL;
1423   mesh->getchildsymmetry    = NULL;
1424   for (d = 0; d < 8; ++d) mesh->hybridPointMax[d] = PETSC_DETERMINE;
1425   mesh->vtkCellHeight       = 0;
1426   mesh->useCone             = PETSC_FALSE;
1427   mesh->useClosure          = PETSC_TRUE;
1428   mesh->useAnchors          = PETSC_FALSE;
1429 
1430   mesh->maxProjectionHeight = 0;
1431 
1432   mesh->printSetValues = PETSC_FALSE;
1433   mesh->printFEM       = 0;
1434   mesh->printTol       = 1.0e-10;
1435 
1436   ierr = DMInitialize_Plex(dm);CHKERRQ(ierr);
1437   PetscFunctionReturn(0);
1438 }
1439 
1440 #undef __FUNCT__
1441 #define __FUNCT__ "DMPlexCreate"
1442 /*@
1443   DMPlexCreate - Creates a DMPlex object, which encapsulates an unstructured mesh, or CW complex, which can be expressed using a Hasse Diagram.
1444 
1445   Collective on MPI_Comm
1446 
1447   Input Parameter:
1448 . comm - The communicator for the DMPlex object
1449 
1450   Output Parameter:
1451 . mesh  - The DMPlex object
1452 
1453   Level: beginner
1454 
1455 .keywords: DMPlex, create
1456 @*/
1457 PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh)
1458 {
1459   PetscErrorCode ierr;
1460 
1461   PetscFunctionBegin;
1462   PetscValidPointer(mesh,2);
1463   ierr = DMCreate(comm, mesh);CHKERRQ(ierr);
1464   ierr = DMSetType(*mesh, DMPLEX);CHKERRQ(ierr);
1465   PetscFunctionReturn(0);
1466 }
1467 
1468 #undef __FUNCT__
1469 #define __FUNCT__ "DMPlexBuildFromCellList_Private"
1470 /*
1471   This takes as input the common mesh generator output, a list of the vertices for each cell
1472 */
1473 PetscErrorCode DMPlexBuildFromCellList_Private(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const int cells[])
1474 {
1475   PetscInt      *cone, c, p;
1476   PetscErrorCode ierr;
1477 
1478   PetscFunctionBegin;
1479   ierr = DMPlexSetChart(dm, 0, numCells+numVertices);CHKERRQ(ierr);
1480   for (c = 0; c < numCells; ++c) {
1481     ierr = DMPlexSetConeSize(dm, c, numCorners);CHKERRQ(ierr);
1482   }
1483   ierr = DMSetUp(dm);CHKERRQ(ierr);
1484   ierr = DMGetWorkArray(dm, numCorners, PETSC_INT, &cone);CHKERRQ(ierr);
1485   for (c = 0; c < numCells; ++c) {
1486     for (p = 0; p < numCorners; ++p) {
1487       cone[p] = cells[c*numCorners+p]+numCells;
1488     }
1489     ierr = DMPlexSetCone(dm, c, cone);CHKERRQ(ierr);
1490   }
1491   ierr = DMRestoreWorkArray(dm, numCorners, PETSC_INT, &cone);CHKERRQ(ierr);
1492   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
1493   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
1494   PetscFunctionReturn(0);
1495 }
1496 
1497 #undef __FUNCT__
1498 #define __FUNCT__ "DMPlexBuildCoordinates_Private"
1499 /*
1500   This takes as input the coordinates for each vertex
1501 */
1502 PetscErrorCode DMPlexBuildCoordinates_Private(DM dm, PetscInt spaceDim, PetscInt numCells, PetscInt numVertices, const double vertexCoords[])
1503 {
1504   PetscSection   coordSection;
1505   Vec            coordinates;
1506   PetscScalar   *coords;
1507   PetscInt       coordSize, v, d;
1508   PetscErrorCode ierr;
1509 
1510   PetscFunctionBegin;
1511   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1512   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
1513   ierr = PetscSectionSetFieldComponents(coordSection, 0, spaceDim);CHKERRQ(ierr);
1514   ierr = PetscSectionSetChart(coordSection, numCells, numCells + numVertices);CHKERRQ(ierr);
1515   for (v = numCells; v < numCells+numVertices; ++v) {
1516     ierr = PetscSectionSetDof(coordSection, v, spaceDim);CHKERRQ(ierr);
1517     ierr = PetscSectionSetFieldDof(coordSection, v, 0, spaceDim);CHKERRQ(ierr);
1518   }
1519   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
1520   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
1521   ierr = VecCreate(PetscObjectComm((PetscObject)dm), &coordinates);CHKERRQ(ierr);
1522   ierr = VecSetBlockSize(coordinates, spaceDim);CHKERRQ(ierr);
1523   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
1524   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
1525   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
1526   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
1527   for (v = 0; v < numVertices; ++v) {
1528     for (d = 0; d < spaceDim; ++d) {
1529       coords[v*spaceDim+d] = vertexCoords[v*spaceDim+d];
1530     }
1531   }
1532   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
1533   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
1534   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
1535   PetscFunctionReturn(0);
1536 }
1537 
1538 #undef __FUNCT__
1539 #define __FUNCT__ "DMPlexCreateFromCellList"
1540 /*@C
1541   DMPlexCreateFromCellList - This takes as input common mesh generator output, a list of the vertices for each cell, and produces a DM
1542 
1543   Input Parameters:
1544 + comm - The communicator
1545 . dim - The topological dimension of the mesh
1546 . numCells - The number of cells
1547 . numVertices - The number of vertices
1548 . numCorners - The number of vertices for each cell
1549 . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
1550 . cells - An array of numCells*numCorners numbers, the vertices for each cell
1551 . spaceDim - The spatial dimension used for coordinates
1552 - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
1553 
1554   Output Parameter:
1555 . dm - The DM
1556 
1557   Note: Two triangles sharing a face
1558 $
1559 $        2
1560 $      / | \
1561 $     /  |  \
1562 $    /   |   \
1563 $   0  0 | 1  3
1564 $    \   |   /
1565 $     \  |  /
1566 $      \ | /
1567 $        1
1568 would have input
1569 $  numCells = 2, numVertices = 4
1570 $  cells = [0 1 2  1 3 2]
1571 $
1572 which would result in the DMPlex
1573 $
1574 $        4
1575 $      / | \
1576 $     /  |  \
1577 $    /   |   \
1578 $   2  0 | 1  5
1579 $    \   |   /
1580 $     \  |  /
1581 $      \ | /
1582 $        3
1583 
1584   Level: beginner
1585 
1586 .seealso: DMPlexCreateFromDAG(), DMPlexCreate()
1587 @*/
1588 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)
1589 {
1590   PetscErrorCode ierr;
1591 
1592   PetscFunctionBegin;
1593   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
1594   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
1595   ierr = DMSetDimension(*dm, dim);CHKERRQ(ierr);
1596   ierr = DMPlexBuildFromCellList_Private(*dm, numCells, numVertices, numCorners, cells);CHKERRQ(ierr);
1597   if (interpolate) {
1598     DM idm = NULL;
1599 
1600     ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr);
1601     ierr = DMDestroy(dm);CHKERRQ(ierr);
1602     *dm  = idm;
1603   }
1604   ierr = DMPlexBuildCoordinates_Private(*dm, spaceDim, numCells, numVertices, vertexCoords);CHKERRQ(ierr);
1605   PetscFunctionReturn(0);
1606 }
1607 
1608 #undef __FUNCT__
1609 #define __FUNCT__ "DMPlexCreateFromDAG"
1610 /*@
1611   DMPlexCreateFromDAG - This takes as input the adjacency-list representation of the Directed Acyclic Graph (Hasse Diagram) encoding a mesh, and produces a DM
1612 
1613   Input Parameters:
1614 + dm - The empty DM object, usually from DMCreate() and DMSetDimension()
1615 . depth - The depth of the DAG
1616 . numPoints - The number of points at each depth
1617 . coneSize - The cone size of each point
1618 . cones - The concatenation of the cone points for each point, the cone list must be oriented correctly for each point
1619 . coneOrientations - The orientation of each cone point
1620 - vertexCoords - An array of numVertices*dim numbers, the coordinates of each vertex
1621 
1622   Output Parameter:
1623 . dm - The DM
1624 
1625   Note: Two triangles sharing a face would have input
1626 $  depth = 1, numPoints = [4 2], coneSize = [3 3 0 0 0 0]
1627 $  cones = [2 3 4  3 5 4], coneOrientations = [0 0 0  0 0 0]
1628 $ vertexCoords = [-1.0 0.0  0.0 -1.0  0.0 1.0  1.0 0.0]
1629 $
1630 which would result in the DMPlex
1631 $
1632 $        4
1633 $      / | \
1634 $     /  |  \
1635 $    /   |   \
1636 $   2  0 | 1  5
1637 $    \   |   /
1638 $     \  |  /
1639 $      \ | /
1640 $        3
1641 $
1642 $ Notice that all points are numbered consecutively, unlikely DMPlexCreateFromCellList()
1643 
1644   Level: advanced
1645 
1646 .seealso: DMPlexCreateFromCellList(), DMPlexCreate()
1647 @*/
1648 PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[])
1649 {
1650   Vec            coordinates;
1651   PetscSection   coordSection;
1652   PetscScalar    *coords;
1653   PetscInt       coordSize, firstVertex = -1, pStart = 0, pEnd = 0, p, v, dim, dimEmbed, d, off;
1654   PetscErrorCode ierr;
1655 
1656   PetscFunctionBegin;
1657   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1658   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
1659   if (dimEmbed < dim) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Embedding dimension %d cannot be less than intrinsic dimension %d",dimEmbed,dim);
1660   for (d = 0; d <= depth; ++d) pEnd += numPoints[d];
1661   ierr = DMPlexSetChart(dm, pStart, pEnd);CHKERRQ(ierr);
1662   for (p = pStart; p < pEnd; ++p) {
1663     ierr = DMPlexSetConeSize(dm, p, coneSize[p-pStart]);CHKERRQ(ierr);
1664     if (firstVertex < 0 && !coneSize[p - pStart]) {
1665       firstVertex = p - pStart;
1666     }
1667   }
1668   if (firstVertex < 0 && numPoints[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected %d vertices but could not find any", numPoints[0]);
1669   ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
1670   for (p = pStart, off = 0; p < pEnd; off += coneSize[p-pStart], ++p) {
1671     ierr = DMPlexSetCone(dm, p, &cones[off]);CHKERRQ(ierr);
1672     ierr = DMPlexSetConeOrientation(dm, p, &coneOrientations[off]);CHKERRQ(ierr);
1673   }
1674   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
1675   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
1676   /* Build coordinates */
1677   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1678   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
1679   ierr = PetscSectionSetFieldComponents(coordSection, 0, dimEmbed);CHKERRQ(ierr);
1680   ierr = PetscSectionSetChart(coordSection, firstVertex, firstVertex+numPoints[0]);CHKERRQ(ierr);
1681   for (v = firstVertex; v < firstVertex+numPoints[0]; ++v) {
1682     ierr = PetscSectionSetDof(coordSection, v, dimEmbed);CHKERRQ(ierr);
1683     ierr = PetscSectionSetFieldDof(coordSection, v, 0, dimEmbed);CHKERRQ(ierr);
1684   }
1685   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
1686   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
1687   ierr = VecCreate(PetscObjectComm((PetscObject)dm), &coordinates);CHKERRQ(ierr);
1688   ierr = VecSetBlockSize(coordinates, dimEmbed);CHKERRQ(ierr);
1689   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
1690   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
1691   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
1692   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
1693   for (v = 0; v < numPoints[0]; ++v) {
1694     PetscInt off;
1695 
1696     ierr = PetscSectionGetOffset(coordSection, v+firstVertex, &off);CHKERRQ(ierr);
1697     for (d = 0; d < dimEmbed; ++d) {
1698       coords[off+d] = vertexCoords[v*dimEmbed+d];
1699     }
1700   }
1701   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
1702   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
1703   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
1704   PetscFunctionReturn(0);
1705 }
1706 
1707 #undef __FUNCT__
1708 #define __FUNCT__ "DMPlexCreateFromFile"
1709 /*@C
1710   DMPlexCreateFromFile - This takes a filename and produces a DM
1711 
1712   Input Parameters:
1713 + comm - The communicator
1714 . filename - A file name
1715 - interpolate - Flag to create intermediate mesh pieces (edges, faces)
1716 
1717   Output Parameter:
1718 . dm - The DM
1719 
1720   Level: beginner
1721 
1722 .seealso: DMPlexCreateFromDAG(), DMPlexCreateFromCellList(), DMPlexCreate()
1723 @*/
1724 PetscErrorCode DMPlexCreateFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
1725 {
1726   const char    *extGmsh   = ".msh";
1727   const char    *extCGNS   = ".cgns";
1728   const char    *extExodus = ".exo";
1729   const char    *extFluent = ".cas";
1730   const char    *extHDF5   = ".h5";
1731   size_t         len;
1732   PetscBool      isGmsh, isCGNS, isExodus, isFluent, isHDF5;
1733   PetscMPIInt    rank;
1734   PetscErrorCode ierr;
1735 
1736   PetscFunctionBegin;
1737   PetscValidPointer(filename, 2);
1738   PetscValidPointer(dm, 4);
1739   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
1740   ierr = PetscStrlen(filename, &len);CHKERRQ(ierr);
1741   if (!len) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Filename must be a valid path");
1742   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)], extGmsh,   4, &isGmsh);CHKERRQ(ierr);
1743   ierr = PetscStrncmp(&filename[PetscMax(0,len-5)], extCGNS,   5, &isCGNS);CHKERRQ(ierr);
1744   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)], extExodus, 4, &isExodus);CHKERRQ(ierr);
1745   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)], extFluent, 4, &isFluent);CHKERRQ(ierr);
1746   ierr = PetscStrncmp(&filename[PetscMax(0,len-3)], extHDF5,   3, &isHDF5);CHKERRQ(ierr);
1747   if (isGmsh) {
1748     ierr = DMPlexCreateGmshFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
1749   } else if (isCGNS) {
1750     ierr = DMPlexCreateCGNSFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
1751   } else if (isExodus) {
1752     ierr = DMPlexCreateExodusFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
1753   } else if (isFluent) {
1754     ierr = DMPlexCreateFluentFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
1755   } else if (isHDF5) {
1756     PetscViewer viewer;
1757 
1758     ierr = PetscViewerCreate(comm, &viewer);CHKERRQ(ierr);
1759     ierr = PetscViewerSetType(viewer, PETSCVIEWERHDF5);CHKERRQ(ierr);
1760     ierr = PetscViewerFileSetMode(viewer, FILE_MODE_READ);CHKERRQ(ierr);
1761     ierr = PetscViewerFileSetName(viewer, filename);CHKERRQ(ierr);
1762     ierr = DMCreate(comm, dm);CHKERRQ(ierr);
1763     ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
1764     ierr = DMLoad(*dm, viewer);CHKERRQ(ierr);
1765     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
1766   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename);
1767   PetscFunctionReturn(0);
1768 }
1769 
1770 #undef __FUNCT__
1771 #define __FUNCT__ "DMPlexCreateReferenceCell"
1772 /*@
1773   DMPlexCreateReferenceCell - Create a DMPLEX with the appropriate FEM reference cell
1774 
1775   Collective on comm
1776 
1777   Input Parameters:
1778 + comm    - The communicator
1779 . dim     - The spatial dimension
1780 - simplex - Flag for simplex, otherwise use a tensor-product cell
1781 
1782   Output Parameter:
1783 . refdm - The reference cell
1784 
1785   Level: intermediate
1786 
1787 .keywords: reference cell
1788 .seealso:
1789 @*/
1790 PetscErrorCode DMPlexCreateReferenceCell(MPI_Comm comm, PetscInt dim, PetscBool simplex, DM *refdm)
1791 {
1792   DM             rdm;
1793   Vec            coords;
1794   PetscErrorCode ierr;
1795 
1796   PetscFunctionBeginUser;
1797   ierr = DMCreate(comm, &rdm);CHKERRQ(ierr);
1798   ierr = DMSetType(rdm, DMPLEX);CHKERRQ(ierr);
1799   ierr = DMSetDimension(rdm, dim);CHKERRQ(ierr);
1800   switch (dim) {
1801   case 0:
1802   {
1803     PetscInt    numPoints[1]        = {1};
1804     PetscInt    coneSize[1]         = {0};
1805     PetscInt    cones[1]            = {0};
1806     PetscInt    coneOrientations[1] = {0};
1807     PetscScalar vertexCoords[1]     = {0.0};
1808 
1809     ierr = DMPlexCreateFromDAG(rdm, 0, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
1810   }
1811   break;
1812   case 1:
1813   {
1814     PetscInt    numPoints[2]        = {2, 1};
1815     PetscInt    coneSize[3]         = {2, 0, 0};
1816     PetscInt    cones[2]            = {1, 2};
1817     PetscInt    coneOrientations[2] = {0, 0};
1818     PetscScalar vertexCoords[2]     = {-1.0,  1.0};
1819 
1820     ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
1821   }
1822   break;
1823   case 2:
1824     if (simplex) {
1825       PetscInt    numPoints[2]        = {3, 1};
1826       PetscInt    coneSize[4]         = {3, 0, 0, 0};
1827       PetscInt    cones[3]            = {1, 2, 3};
1828       PetscInt    coneOrientations[3] = {0, 0, 0};
1829       PetscScalar vertexCoords[6]     = {-1.0, -1.0,  1.0, -1.0,  -1.0, 1.0};
1830 
1831       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
1832     } else {
1833       PetscInt    numPoints[2]        = {4, 1};
1834       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
1835       PetscInt    cones[4]            = {1, 2, 3, 4};
1836       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
1837       PetscScalar vertexCoords[8]     = {-1.0, -1.0,  1.0, -1.0,  1.0, 1.0,  -1.0, 1.0};
1838 
1839       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
1840     }
1841   break;
1842   case 3:
1843     if (simplex) {
1844       PetscInt    numPoints[2]        = {4, 1};
1845       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
1846       PetscInt    cones[4]            = {1, 3, 2, 4};
1847       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
1848       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};
1849 
1850       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
1851     } else {
1852       PetscInt    numPoints[2]        = {8, 1};
1853       PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
1854       PetscInt    cones[8]            = {1, 4, 3, 2, 5, 6, 7, 8};
1855       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
1856       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,
1857                                          -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  1.0, 1.0,  1.0,  -1.0, 1.0,  1.0};
1858 
1859       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
1860     }
1861   break;
1862   default:
1863     SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Cannot create reference cell for dimension %d", dim);
1864   }
1865   *refdm = NULL;
1866   ierr = DMPlexInterpolate(rdm, refdm);CHKERRQ(ierr);
1867   if (rdm->coordinateDM) {
1868     DM           ncdm;
1869     PetscSection cs;
1870     PetscInt     pEnd = -1;
1871 
1872     ierr = DMGetDefaultSection(rdm->coordinateDM, &cs);CHKERRQ(ierr);
1873     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
1874     if (pEnd >= 0) {
1875       ierr = DMClone(rdm->coordinateDM, &ncdm);CHKERRQ(ierr);
1876       ierr = DMSetDefaultSection(ncdm, cs);CHKERRQ(ierr);
1877       ierr = DMSetCoordinateDM(*refdm, ncdm);CHKERRQ(ierr);
1878       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
1879     }
1880   }
1881   ierr = DMGetCoordinatesLocal(rdm, &coords);CHKERRQ(ierr);
1882   if (coords) {
1883     ierr = DMSetCoordinatesLocal(*refdm, coords);CHKERRQ(ierr);
1884   } else {
1885     ierr = DMGetCoordinates(rdm, &coords);CHKERRQ(ierr);
1886     if (coords) {ierr = DMSetCoordinates(*refdm, coords);CHKERRQ(ierr);}
1887   }
1888   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
1889   PetscFunctionReturn(0);
1890 }
1891