xref: /petsc/src/dm/impls/plex/plexcreate.c (revision d35a276d8c863f60e07e6885b47dfedec29ea6f7)
1552f7358SJed Brown #define PETSCDM_DLL
2af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>    /*I   "petscdmplex.h"   I*/
3552f7358SJed Brown #include <petscdmda.h>
40c312b8eSJed Brown #include <petscsf.h>
5552f7358SJed Brown 
61df5d5c5SMatthew G. Knepley /*@
71df5d5c5SMatthew G. Knepley   DMPlexCreateDoublet - Creates a mesh of two cells of the specified type, optionally with later refinement.
81df5d5c5SMatthew G. Knepley 
91df5d5c5SMatthew G. Knepley   Collective on MPI_Comm
101df5d5c5SMatthew G. Knepley 
111df5d5c5SMatthew G. Knepley   Input Parameters:
121df5d5c5SMatthew G. Knepley + comm - The communicator for the DM object
131df5d5c5SMatthew G. Knepley . dim - The spatial dimension
141df5d5c5SMatthew G. Knepley . simplex - Flag for simplicial cells, otherwise they are tensor product cells
151df5d5c5SMatthew G. Knepley . interpolate - Flag to create intermediate mesh pieces (edges, faces)
161df5d5c5SMatthew G. Knepley . refinementUniform - Flag for uniform parallel refinement
171df5d5c5SMatthew G. Knepley - refinementLimit - A nonzero number indicates the largest admissible volume for a refined cell
181df5d5c5SMatthew G. Knepley 
191df5d5c5SMatthew G. Knepley   Output Parameter:
201df5d5c5SMatthew G. Knepley . dm  - The DM object
211df5d5c5SMatthew G. Knepley 
221df5d5c5SMatthew G. Knepley   Level: beginner
231df5d5c5SMatthew G. Knepley 
241df5d5c5SMatthew G. Knepley .keywords: DM, create
251df5d5c5SMatthew G. Knepley .seealso: DMSetType(), DMCreate()
261df5d5c5SMatthew G. Knepley @*/
271df5d5c5SMatthew G. Knepley PetscErrorCode DMPlexCreateDoublet(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscBool interpolate, PetscBool refinementUniform, PetscReal refinementLimit, DM *newdm)
281df5d5c5SMatthew G. Knepley {
291df5d5c5SMatthew G. Knepley   DM             dm;
301df5d5c5SMatthew G. Knepley   PetscInt       p;
311df5d5c5SMatthew G. Knepley   PetscMPIInt    rank;
321df5d5c5SMatthew G. Knepley   PetscErrorCode ierr;
331df5d5c5SMatthew G. Knepley 
341df5d5c5SMatthew G. Knepley   PetscFunctionBegin;
351df5d5c5SMatthew G. Knepley   ierr = DMCreate(comm, &dm);CHKERRQ(ierr);
361df5d5c5SMatthew G. Knepley   ierr = DMSetType(dm, DMPLEX);CHKERRQ(ierr);
37c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(dm, dim);CHKERRQ(ierr);
381df5d5c5SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
39ce78fa2fSMatthew G. Knepley   switch (dim) {
40ce78fa2fSMatthew G. Knepley   case 2:
41ce78fa2fSMatthew G. Knepley     if (simplex) {ierr = PetscObjectSetName((PetscObject) dm, "triangular");CHKERRQ(ierr);}
42ce78fa2fSMatthew G. Knepley     else         {ierr = PetscObjectSetName((PetscObject) dm, "quadrilateral");CHKERRQ(ierr);}
43ce78fa2fSMatthew G. Knepley     break;
44ce78fa2fSMatthew G. Knepley   case 3:
45ce78fa2fSMatthew G. Knepley     if (simplex) {ierr = PetscObjectSetName((PetscObject) dm, "tetrahedral");CHKERRQ(ierr);}
46ce78fa2fSMatthew G. Knepley     else         {ierr = PetscObjectSetName((PetscObject) dm, "hexahedral");CHKERRQ(ierr);}
47ce78fa2fSMatthew G. Knepley     break;
48ce78fa2fSMatthew G. Knepley   default:
49ce78fa2fSMatthew G. Knepley     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %d", dim);
50ce78fa2fSMatthew G. Knepley   }
511df5d5c5SMatthew G. Knepley   if (rank) {
521df5d5c5SMatthew G. Knepley     PetscInt numPoints[2] = {0, 0};
531df5d5c5SMatthew G. Knepley     ierr = DMPlexCreateFromDAG(dm, 1, numPoints, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
541df5d5c5SMatthew G. Knepley   } else {
551df5d5c5SMatthew G. Knepley     switch (dim) {
561df5d5c5SMatthew G. Knepley     case 2:
571df5d5c5SMatthew G. Knepley       if (simplex) {
581df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {4, 2};
591df5d5c5SMatthew G. Knepley         PetscInt    coneSize[6]         = {3, 3, 0, 0, 0, 0};
601df5d5c5SMatthew G. Knepley         PetscInt    cones[6]            = {2, 3, 4,  5, 4, 3};
611df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[6] = {0, 0, 0,  0, 0, 0};
621df5d5c5SMatthew G. Knepley         PetscScalar vertexCoords[8]     = {-0.5, 0.5, 0.0, 0.0, 0.0, 1.0, 0.5, 0.5};
631df5d5c5SMatthew G. Knepley         PetscInt    markerPoints[8]     = {2, 1, 3, 1, 4, 1, 5, 1};
641df5d5c5SMatthew G. Knepley 
651df5d5c5SMatthew G. Knepley         ierr = DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
66c58f1c22SToby Isaac         for (p = 0; p < 4; ++p) {ierr = DMSetLabelValue(dm, "marker", markerPoints[p*2], markerPoints[p*2+1]);CHKERRQ(ierr);}
671df5d5c5SMatthew G. Knepley       } else {
681df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {6, 2};
691df5d5c5SMatthew G. Knepley         PetscInt    coneSize[8]         = {4, 4, 0, 0, 0, 0, 0, 0};
701df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {2, 3, 4, 5,  3, 6, 7, 4};
711df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0,  0, 0, 0, 0};
721df5d5c5SMatthew G. Knepley         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};
731df5d5c5SMatthew G. Knepley 
741df5d5c5SMatthew G. Knepley         ierr = DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
751df5d5c5SMatthew G. Knepley       }
761df5d5c5SMatthew G. Knepley       break;
771df5d5c5SMatthew G. Knepley     case 3:
781df5d5c5SMatthew G. Knepley       if (simplex) {
791df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {5, 2};
801df5d5c5SMatthew G. Knepley         PetscInt    coneSize[7]         = {4, 4, 0, 0, 0, 0, 0};
811df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {4, 3, 5, 2,  5, 3, 4, 6};
821df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0,  0, 0, 0, 0};
831df5d5c5SMatthew G. Knepley         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};
841df5d5c5SMatthew G. Knepley         PetscInt    markerPoints[10]    = {2, 1, 3, 1, 4, 1, 5, 1, 6, 1};
851df5d5c5SMatthew G. Knepley 
861df5d5c5SMatthew G. Knepley         ierr = DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
87c58f1c22SToby Isaac         for (p = 0; p < 5; ++p) {ierr = DMSetLabelValue(dm, "marker", markerPoints[p*2], markerPoints[p*2+1]);CHKERRQ(ierr);}
881df5d5c5SMatthew G. Knepley       } else {
891df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]         = {12, 2};
901df5d5c5SMatthew G. Knepley         PetscInt    coneSize[14]         = {8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
911df5d5c5SMatthew G. Knepley         PetscInt    cones[16]            = {2, 3, 4, 5, 6, 7, 8, 9,  5, 4, 10, 11, 7, 12, 13, 8};
921df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[16] = {0, 0, 0, 0, 0, 0, 0, 0,  0, 0,  0,  0, 0,  0,  0, 0};
931df5d5c5SMatthew G. Knepley         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,
941df5d5c5SMatthew G. Knepley                                             -1.0, -0.5,  0.5,   0.0, -0.5,  0.5,  0.0,  0.5,  0.5,  -1.0,  0.5,  0.5,
951df5d5c5SMatthew G. Knepley                                              1.0,  0.5, -0.5,   1.0, -0.5, -0.5,  1.0, -0.5,  0.5,   1.0,  0.5,  0.5};
961df5d5c5SMatthew G. Knepley 
971df5d5c5SMatthew G. Knepley         ierr = DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
981df5d5c5SMatthew G. Knepley       }
991df5d5c5SMatthew G. Knepley       break;
1001df5d5c5SMatthew G. Knepley     default:
1011df5d5c5SMatthew G. Knepley       SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %d", dim);
1021df5d5c5SMatthew G. Knepley     }
1031df5d5c5SMatthew G. Knepley   }
1041df5d5c5SMatthew G. Knepley   *newdm = dm;
1051df5d5c5SMatthew G. Knepley   if (refinementLimit > 0.0) {
1061df5d5c5SMatthew G. Knepley     DM rdm;
1071df5d5c5SMatthew G. Knepley     const char *name;
1081df5d5c5SMatthew G. Knepley 
1091df5d5c5SMatthew G. Knepley     ierr = DMPlexSetRefinementUniform(*newdm, PETSC_FALSE);CHKERRQ(ierr);
1101df5d5c5SMatthew G. Knepley     ierr = DMPlexSetRefinementLimit(*newdm, refinementLimit);CHKERRQ(ierr);
1111df5d5c5SMatthew G. Knepley     ierr = DMRefine(*newdm, comm, &rdm);CHKERRQ(ierr);
1121df5d5c5SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) *newdm, &name);CHKERRQ(ierr);
1131df5d5c5SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject)    rdm,  name);CHKERRQ(ierr);
1141df5d5c5SMatthew G. Knepley     ierr = DMDestroy(newdm);CHKERRQ(ierr);
1151df5d5c5SMatthew G. Knepley     *newdm = rdm;
1161df5d5c5SMatthew G. Knepley   }
1171df5d5c5SMatthew G. Knepley   if (interpolate) {
118e56d480eSMatthew G. Knepley     DM idm = NULL;
1191df5d5c5SMatthew G. Knepley     const char *name;
1201df5d5c5SMatthew G. Knepley 
1211df5d5c5SMatthew G. Knepley     ierr = DMPlexInterpolate(*newdm, &idm);CHKERRQ(ierr);
1221df5d5c5SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) *newdm, &name);CHKERRQ(ierr);
1231df5d5c5SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject)    idm,  name);CHKERRQ(ierr);
1241df5d5c5SMatthew G. Knepley     ierr = DMPlexCopyCoordinates(*newdm, idm);CHKERRQ(ierr);
125c58f1c22SToby Isaac     ierr = DMCopyLabels(*newdm, idm);CHKERRQ(ierr);
1261df5d5c5SMatthew G. Knepley     ierr = DMDestroy(newdm);CHKERRQ(ierr);
1271df5d5c5SMatthew G. Knepley     *newdm = idm;
1281df5d5c5SMatthew G. Knepley   }
1291df5d5c5SMatthew G. Knepley   {
1301df5d5c5SMatthew G. Knepley     DM refinedMesh     = NULL;
1311df5d5c5SMatthew G. Knepley     DM distributedMesh = NULL;
1321df5d5c5SMatthew G. Knepley 
1331df5d5c5SMatthew G. Knepley     /* Distribute mesh over processes */
13480cf41d5SMatthew G. Knepley     ierr = DMPlexDistribute(*newdm, 0, NULL, &distributedMesh);CHKERRQ(ierr);
1351df5d5c5SMatthew G. Knepley     if (distributedMesh) {
1361df5d5c5SMatthew G. Knepley       ierr = DMDestroy(newdm);CHKERRQ(ierr);
1371df5d5c5SMatthew G. Knepley       *newdm = distributedMesh;
1381df5d5c5SMatthew G. Knepley     }
1391df5d5c5SMatthew G. Knepley     if (refinementUniform) {
1401df5d5c5SMatthew G. Knepley       ierr = DMPlexSetRefinementUniform(*newdm, refinementUniform);CHKERRQ(ierr);
1411df5d5c5SMatthew G. Knepley       ierr = DMRefine(*newdm, comm, &refinedMesh);CHKERRQ(ierr);
1421df5d5c5SMatthew G. Knepley       if (refinedMesh) {
1431df5d5c5SMatthew G. Knepley         ierr = DMDestroy(newdm);CHKERRQ(ierr);
1441df5d5c5SMatthew G. Knepley         *newdm = refinedMesh;
1451df5d5c5SMatthew G. Knepley       }
1461df5d5c5SMatthew G. Knepley     }
1471df5d5c5SMatthew G. Knepley   }
1481df5d5c5SMatthew G. Knepley   PetscFunctionReturn(0);
1491df5d5c5SMatthew G. Knepley }
1501df5d5c5SMatthew G. Knepley 
15126492d91SMatthew G. Knepley /*@
15226492d91SMatthew G. Knepley   DMPlexCreateSquareBoundary - Creates a 1D mesh the is the boundary of a square lattice.
153552f7358SJed Brown 
15426492d91SMatthew G. Knepley   Collective on MPI_Comm
15526492d91SMatthew G. Knepley 
15626492d91SMatthew G. Knepley   Input Parameters:
15726492d91SMatthew G. Knepley + comm  - The communicator for the DM object
15826492d91SMatthew G. Knepley . lower - The lower left corner coordinates
15926492d91SMatthew G. Knepley . upper - The upper right corner coordinates
16026492d91SMatthew G. Knepley - edges - The number of cells in each direction
16126492d91SMatthew G. Knepley 
16226492d91SMatthew G. Knepley   Output Parameter:
16326492d91SMatthew G. Knepley . dm  - The DM object
16426492d91SMatthew G. Knepley 
16526492d91SMatthew G. Knepley   Note: Here is the numbering returned for 2 cells in each direction:
16626492d91SMatthew G. Knepley $ 18--5-17--4--16
16726492d91SMatthew G. Knepley $  |     |     |
16826492d91SMatthew G. Knepley $  6    10     3
16926492d91SMatthew G. Knepley $  |     |     |
17026492d91SMatthew G. Knepley $ 19-11-20--9--15
17126492d91SMatthew G. Knepley $  |     |     |
17226492d91SMatthew G. Knepley $  7     8     2
17326492d91SMatthew G. Knepley $  |     |     |
17426492d91SMatthew G. Knepley $ 12--0-13--1--14
17526492d91SMatthew G. Knepley 
17626492d91SMatthew G. Knepley   Level: beginner
17726492d91SMatthew G. Knepley 
17826492d91SMatthew G. Knepley .keywords: DM, create
17926492d91SMatthew G. Knepley .seealso: DMPlexCreateBoxMesh(), DMPlexCreateCubeBoundary(), DMSetType(), DMCreate()
18026492d91SMatthew G. Knepley @*/
181552f7358SJed Brown PetscErrorCode DMPlexCreateSquareBoundary(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
182552f7358SJed Brown {
1831df21d24SMatthew G. Knepley   const PetscInt numVertices    = (edges[0]+1)*(edges[1]+1);
1841df21d24SMatthew G. Knepley   const PetscInt numEdges       = edges[0]*(edges[1]+1) + (edges[0]+1)*edges[1];
185552f7358SJed Brown   PetscInt       markerTop      = 1;
186552f7358SJed Brown   PetscInt       markerBottom   = 1;
187552f7358SJed Brown   PetscInt       markerRight    = 1;
188552f7358SJed Brown   PetscInt       markerLeft     = 1;
189552f7358SJed Brown   PetscBool      markerSeparate = PETSC_FALSE;
190552f7358SJed Brown   Vec            coordinates;
191552f7358SJed Brown   PetscSection   coordSection;
192552f7358SJed Brown   PetscScalar    *coords;
193552f7358SJed Brown   PetscInt       coordSize;
194552f7358SJed Brown   PetscMPIInt    rank;
195552f7358SJed Brown   PetscInt       v, vx, vy;
196552f7358SJed Brown   PetscErrorCode ierr;
197552f7358SJed Brown 
198552f7358SJed Brown   PetscFunctionBegin;
199c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);CHKERRQ(ierr);
200552f7358SJed Brown   if (markerSeparate) {
2011df21d24SMatthew G. Knepley     markerTop    = 3;
2021df21d24SMatthew G. Knepley     markerBottom = 1;
2031df21d24SMatthew G. Knepley     markerRight  = 2;
2041df21d24SMatthew G. Knepley     markerLeft   = 4;
205552f7358SJed Brown   }
20682f516ccSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
207552f7358SJed Brown   if (!rank) {
208552f7358SJed Brown     PetscInt e, ex, ey;
209552f7358SJed Brown 
210552f7358SJed Brown     ierr = DMPlexSetChart(dm, 0, numEdges+numVertices);CHKERRQ(ierr);
211552f7358SJed Brown     for (e = 0; e < numEdges; ++e) {
212552f7358SJed Brown       ierr = DMPlexSetConeSize(dm, e, 2);CHKERRQ(ierr);
213552f7358SJed Brown     }
214552f7358SJed Brown     ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
215552f7358SJed Brown     for (vx = 0; vx <= edges[0]; vx++) {
216552f7358SJed Brown       for (ey = 0; ey < edges[1]; ey++) {
217552f7358SJed Brown         PetscInt edge   = vx*edges[1] + ey + edges[0]*(edges[1]+1);
218552f7358SJed Brown         PetscInt vertex = ey*(edges[0]+1) + vx + numEdges;
219da80777bSKarl Rupp         PetscInt cone[2];
220552f7358SJed Brown 
221da80777bSKarl Rupp         cone[0] = vertex; cone[1] = vertex+edges[0]+1;
222552f7358SJed Brown         ierr    = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr);
223552f7358SJed Brown         if (vx == edges[0]) {
224c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", edge,    markerRight);CHKERRQ(ierr);
225c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", cone[0], markerRight);CHKERRQ(ierr);
226552f7358SJed Brown           if (ey == edges[1]-1) {
227c58f1c22SToby Isaac             ierr = DMSetLabelValue(dm, "marker", cone[1], markerRight);CHKERRQ(ierr);
228552f7358SJed Brown           }
229552f7358SJed Brown         } else if (vx == 0) {
230c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", edge,    markerLeft);CHKERRQ(ierr);
231c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", cone[0], markerLeft);CHKERRQ(ierr);
232552f7358SJed Brown           if (ey == edges[1]-1) {
233c58f1c22SToby Isaac             ierr = DMSetLabelValue(dm, "marker", cone[1], markerLeft);CHKERRQ(ierr);
234552f7358SJed Brown           }
235552f7358SJed Brown         }
236552f7358SJed Brown       }
237552f7358SJed Brown     }
238552f7358SJed Brown     for (vy = 0; vy <= edges[1]; vy++) {
239552f7358SJed Brown       for (ex = 0; ex < edges[0]; ex++) {
240552f7358SJed Brown         PetscInt edge   = vy*edges[0]     + ex;
241552f7358SJed Brown         PetscInt vertex = vy*(edges[0]+1) + ex + numEdges;
242da80777bSKarl Rupp         PetscInt cone[2];
243552f7358SJed Brown 
244da80777bSKarl Rupp         cone[0] = vertex; cone[1] = vertex+1;
245552f7358SJed Brown         ierr    = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr);
246552f7358SJed Brown         if (vy == edges[1]) {
247c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", edge,    markerTop);CHKERRQ(ierr);
248c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", cone[0], markerTop);CHKERRQ(ierr);
249552f7358SJed Brown           if (ex == edges[0]-1) {
250c58f1c22SToby Isaac             ierr = DMSetLabelValue(dm, "marker", cone[1], markerTop);CHKERRQ(ierr);
251552f7358SJed Brown           }
252552f7358SJed Brown         } else if (vy == 0) {
253c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", edge,    markerBottom);CHKERRQ(ierr);
254c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", cone[0], markerBottom);CHKERRQ(ierr);
255552f7358SJed Brown           if (ex == edges[0]-1) {
256c58f1c22SToby Isaac             ierr = DMSetLabelValue(dm, "marker", cone[1], markerBottom);CHKERRQ(ierr);
257552f7358SJed Brown           }
258552f7358SJed Brown         }
259552f7358SJed Brown       }
260552f7358SJed Brown     }
261552f7358SJed Brown   }
262552f7358SJed Brown   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
263552f7358SJed Brown   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
264552f7358SJed Brown   /* Build coordinates */
265c2166f76SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
266972bc18aSToby Isaac   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
267552f7358SJed Brown   ierr = PetscSectionSetChart(coordSection, numEdges, numEdges + numVertices);CHKERRQ(ierr);
268972bc18aSToby Isaac   ierr = PetscSectionSetFieldComponents(coordSection, 0, 2);CHKERRQ(ierr);
269552f7358SJed Brown   for (v = numEdges; v < numEdges+numVertices; ++v) {
270552f7358SJed Brown     ierr = PetscSectionSetDof(coordSection, v, 2);CHKERRQ(ierr);
271972bc18aSToby Isaac     ierr = PetscSectionSetFieldDof(coordSection, v, 0, 2);CHKERRQ(ierr);
272552f7358SJed Brown   }
273552f7358SJed Brown   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
274552f7358SJed Brown   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
2758b9ced59SLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
276da16285aSMichael Lange   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
277552f7358SJed Brown   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
2788b9ced59SLisandro Dalcin   ierr = VecSetBlockSize(coordinates, 2);CHKERRQ(ierr);
2792eb5907fSJed Brown   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
280552f7358SJed Brown   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
281552f7358SJed Brown   for (vy = 0; vy <= edges[1]; ++vy) {
282552f7358SJed Brown     for (vx = 0; vx <= edges[0]; ++vx) {
283552f7358SJed Brown       coords[(vy*(edges[0]+1)+vx)*2+0] = lower[0] + ((upper[0] - lower[0])/edges[0])*vx;
284552f7358SJed Brown       coords[(vy*(edges[0]+1)+vx)*2+1] = lower[1] + ((upper[1] - lower[1])/edges[1])*vy;
285552f7358SJed Brown     }
286552f7358SJed Brown   }
287552f7358SJed Brown   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
288552f7358SJed Brown   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
289552f7358SJed Brown   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
290552f7358SJed Brown   PetscFunctionReturn(0);
291552f7358SJed Brown }
292552f7358SJed Brown 
29326492d91SMatthew G. Knepley /*@
29426492d91SMatthew G. Knepley   DMPlexCreateCubeBoundary - Creates a 2D mesh the is the boundary of a cubic lattice.
295552f7358SJed Brown 
29626492d91SMatthew G. Knepley   Collective on MPI_Comm
29726492d91SMatthew G. Knepley 
29826492d91SMatthew G. Knepley   Input Parameters:
29926492d91SMatthew G. Knepley + comm  - The communicator for the DM object
30026492d91SMatthew G. Knepley . lower - The lower left front corner coordinates
30126492d91SMatthew G. Knepley . upper - The upper right back corner coordinates
30226492d91SMatthew G. Knepley - edges - The number of cells in each direction
30326492d91SMatthew G. Knepley 
30426492d91SMatthew G. Knepley   Output Parameter:
30526492d91SMatthew G. Knepley . dm  - The DM object
30626492d91SMatthew G. Knepley 
30726492d91SMatthew G. Knepley   Level: beginner
30826492d91SMatthew G. Knepley 
30926492d91SMatthew G. Knepley .keywords: DM, create
31026492d91SMatthew G. Knepley .seealso: DMPlexCreateBoxMesh(), DMPlexCreateSquareBoundary(), DMSetType(), DMCreate()
31126492d91SMatthew G. Knepley @*/
312552f7358SJed Brown PetscErrorCode DMPlexCreateCubeBoundary(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt faces[])
313552f7358SJed Brown {
3149e8abbc3SMichael Lange   PetscInt       vertices[3], numVertices;
3157b59f5a9SMichael Lange   PetscInt       numFaces    = 2*faces[0]*faces[1] + 2*faces[1]*faces[2] + 2*faces[0]*faces[2];
316552f7358SJed Brown   Vec            coordinates;
317552f7358SJed Brown   PetscSection   coordSection;
318552f7358SJed Brown   PetscScalar    *coords;
319552f7358SJed Brown   PetscInt       coordSize;
320552f7358SJed Brown   PetscMPIInt    rank;
321552f7358SJed Brown   PetscInt       v, vx, vy, vz;
3227b59f5a9SMichael Lange   PetscInt       voffset, iface=0, cone[4];
323552f7358SJed Brown   PetscErrorCode ierr;
324552f7358SJed Brown 
325552f7358SJed Brown   PetscFunctionBegin;
32682f516ccSBarry Smith   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");
32782f516ccSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
3289e8abbc3SMichael Lange   vertices[0] = faces[0]+1; vertices[1] = faces[1]+1; vertices[2] = faces[2]+1;
3299e8abbc3SMichael Lange   numVertices = vertices[0]*vertices[1]*vertices[2];
330552f7358SJed Brown   if (!rank) {
331552f7358SJed Brown     PetscInt f;
332552f7358SJed Brown 
333552f7358SJed Brown     ierr = DMPlexSetChart(dm, 0, numFaces+numVertices);CHKERRQ(ierr);
334552f7358SJed Brown     for (f = 0; f < numFaces; ++f) {
335552f7358SJed Brown       ierr = DMPlexSetConeSize(dm, f, 4);CHKERRQ(ierr);
336552f7358SJed Brown     }
337552f7358SJed Brown     ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
338552f7358SJed Brown     for (v = 0; v < numFaces+numVertices; ++v) {
339c58f1c22SToby Isaac       ierr = DMSetLabelValue(dm, "marker", v, 1);CHKERRQ(ierr);
340552f7358SJed Brown     }
3417b59f5a9SMichael Lange 
3427b59f5a9SMichael Lange     /* Side 0 (Top) */
3437b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
3447b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
3457b59f5a9SMichael Lange         voffset = numFaces + vertices[0]*vertices[1]*(vertices[2]-1) + vy*vertices[0] + vx;
3467b59f5a9SMichael Lange         cone[0] = voffset; cone[1] = voffset+1; cone[2] = voffset+vertices[0]+1; cone[3] = voffset+vertices[0];
3477b59f5a9SMichael Lange         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
3487b59f5a9SMichael Lange         iface++;
349552f7358SJed Brown       }
350552f7358SJed Brown     }
3517b59f5a9SMichael Lange 
3527b59f5a9SMichael Lange     /* Side 1 (Bottom) */
3537b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
3547b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
3557b59f5a9SMichael Lange         voffset = numFaces + vy*(faces[0]+1) + vx;
3567b59f5a9SMichael Lange         cone[0] = voffset+1; cone[1] = voffset; cone[2] = voffset+vertices[0]; cone[3] = voffset+vertices[0]+1;
3577b59f5a9SMichael Lange         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
3587b59f5a9SMichael Lange         iface++;
359552f7358SJed Brown       }
360552f7358SJed Brown     }
3617b59f5a9SMichael Lange 
3627b59f5a9SMichael Lange     /* Side 2 (Front) */
3637b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
3647b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
3657b59f5a9SMichael Lange         voffset = numFaces + vz*vertices[0]*vertices[1] + vx;
3667b59f5a9SMichael Lange         cone[0] = voffset; cone[1] = voffset+1; cone[2] = voffset+vertices[0]*vertices[1]+1; cone[3] = voffset+vertices[0]*vertices[1];
3677b59f5a9SMichael Lange         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
3687b59f5a9SMichael Lange         iface++;
369552f7358SJed Brown       }
3707b59f5a9SMichael Lange     }
3717b59f5a9SMichael Lange 
3727b59f5a9SMichael Lange     /* Side 3 (Back) */
3737b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
3747b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
3757b59f5a9SMichael Lange         voffset = numFaces + vz*vertices[0]*vertices[1] + vertices[0]*(vertices[1]-1) + vx;
3767b59f5a9SMichael Lange         cone[0] = voffset+vertices[0]*vertices[1]; cone[1] = voffset+vertices[0]*vertices[1]+1;
3777b59f5a9SMichael Lange         cone[2] = voffset+1; cone[3] = voffset;
3787b59f5a9SMichael Lange         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
3797b59f5a9SMichael Lange         iface++;
3807b59f5a9SMichael Lange       }
3817b59f5a9SMichael Lange     }
3827b59f5a9SMichael Lange 
3837b59f5a9SMichael Lange     /* Side 4 (Left) */
3847b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
3857b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
3867b59f5a9SMichael Lange         voffset = numFaces + vz*vertices[0]*vertices[1] + vy*vertices[0];
3877b59f5a9SMichael Lange         cone[0] = voffset; cone[1] = voffset+vertices[0]*vertices[1];
3887b59f5a9SMichael Lange         cone[2] = voffset+vertices[0]*vertices[1]+vertices[0]; cone[3] = voffset+vertices[0];
3897b59f5a9SMichael Lange         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
3907b59f5a9SMichael Lange         iface++;
3917b59f5a9SMichael Lange       }
3927b59f5a9SMichael Lange     }
3937b59f5a9SMichael Lange 
3947b59f5a9SMichael Lange     /* Side 5 (Right) */
3957b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
3967b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
3977b59f5a9SMichael Lange         voffset = numFaces + vz*vertices[0]*vertices[1] + vy*vertices[0] + vx;
3987b59f5a9SMichael Lange         cone[0] = voffset+vertices[0]*vertices[1]; cone[1] = voffset;
3997b59f5a9SMichael Lange         cone[2] = voffset+vertices[0]; cone[3] = voffset+vertices[0]*vertices[1]+vertices[0];
4007b59f5a9SMichael Lange         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
4017b59f5a9SMichael Lange         iface++;
4027b59f5a9SMichael Lange       }
403552f7358SJed Brown     }
404552f7358SJed Brown   }
405552f7358SJed Brown   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
406552f7358SJed Brown   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
407552f7358SJed Brown   /* Build coordinates */
408c2166f76SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
409552f7358SJed Brown   ierr = PetscSectionSetChart(coordSection, numFaces, numFaces + numVertices);CHKERRQ(ierr);
410552f7358SJed Brown   for (v = numFaces; v < numFaces+numVertices; ++v) {
411552f7358SJed Brown     ierr = PetscSectionSetDof(coordSection, v, 3);CHKERRQ(ierr);
412552f7358SJed Brown   }
413552f7358SJed Brown   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
414552f7358SJed Brown   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
4158b9ced59SLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
416552f7358SJed Brown   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
417552f7358SJed Brown   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
4188b9ced59SLisandro Dalcin   ierr = VecSetBlockSize(coordinates, 3);CHKERRQ(ierr);
4192eb5907fSJed Brown   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
420552f7358SJed Brown   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
421552f7358SJed Brown   for (vz = 0; vz <= faces[2]; ++vz) {
422552f7358SJed Brown     for (vy = 0; vy <= faces[1]; ++vy) {
423552f7358SJed Brown       for (vx = 0; vx <= faces[0]; ++vx) {
424552f7358SJed Brown         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+0] = lower[0] + ((upper[0] - lower[0])/faces[0])*vx;
425552f7358SJed Brown         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+1] = lower[1] + ((upper[1] - lower[1])/faces[1])*vy;
426552f7358SJed Brown         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+2] = lower[2] + ((upper[2] - lower[2])/faces[2])*vz;
427552f7358SJed Brown       }
428552f7358SJed Brown     }
429552f7358SJed Brown   }
430552f7358SJed Brown   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
431552f7358SJed Brown   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
432552f7358SJed Brown   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
433552f7358SJed Brown   PetscFunctionReturn(0);
434552f7358SJed Brown }
435552f7358SJed Brown 
436d6218766SMatthew G. Knepley /*@
437d6218766SMatthew G. Knepley   DMPlexCreateBoxMesh - Creates a mesh on the tensor product of unit intervals (box) using simplices.
438d6218766SMatthew G. Knepley 
439d6218766SMatthew G. Knepley   Collective on MPI_Comm
440d6218766SMatthew G. Knepley 
441d6218766SMatthew G. Knepley   Input Parameters:
442d6218766SMatthew G. Knepley + comm - The communicator for the DM object
443d6218766SMatthew G. Knepley . dim - The spatial dimension
444d6218766SMatthew G. Knepley . numFaces - Number of faces per dimension
445d6218766SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
446d6218766SMatthew G. Knepley 
447d6218766SMatthew G. Knepley   Output Parameter:
448d6218766SMatthew G. Knepley . dm  - The DM object
449d6218766SMatthew G. Knepley 
450d6218766SMatthew G. Knepley   Level: beginner
451d6218766SMatthew G. Knepley 
452d6218766SMatthew G. Knepley .keywords: DM, create
453d6218766SMatthew G. Knepley .seealso: DMPlexCreateHexBoxMesh(), DMSetType(), DMCreate()
454d6218766SMatthew G. Knepley @*/
455d6218766SMatthew G. Knepley PetscErrorCode DMPlexCreateBoxMesh(MPI_Comm comm, PetscInt dim, PetscInt numFaces, PetscBool interpolate, DM *dm)
456d6218766SMatthew G. Knepley {
457d6218766SMatthew G. Knepley   DM             boundary;
458d6218766SMatthew G. Knepley   PetscErrorCode ierr;
459d6218766SMatthew G. Knepley 
460d6218766SMatthew G. Knepley   PetscFunctionBegin;
461d6218766SMatthew G. Knepley   PetscValidPointer(dm, 4);
462d6218766SMatthew G. Knepley   ierr = DMCreate(comm, &boundary);CHKERRQ(ierr);
463d6218766SMatthew G. Knepley   PetscValidLogicalCollectiveInt(boundary,dim,2);
464d6218766SMatthew G. Knepley   ierr = DMSetType(boundary, DMPLEX);CHKERRQ(ierr);
465d6218766SMatthew G. Knepley   ierr = DMSetDimension(boundary, dim-1);CHKERRQ(ierr);
466d6218766SMatthew G. Knepley   ierr = DMSetCoordinateDim(boundary, dim);CHKERRQ(ierr);
467d6218766SMatthew G. Knepley   switch (dim) {
468d6218766SMatthew G. Knepley   case 2:
469d6218766SMatthew G. Knepley   {
470d6218766SMatthew G. Knepley     PetscReal lower[2] = {0.0, 0.0};
471d6218766SMatthew G. Knepley     PetscReal upper[2] = {1.0, 1.0};
472d6218766SMatthew G. Knepley     PetscInt  edges[2];
473d6218766SMatthew G. Knepley 
474d6218766SMatthew G. Knepley     edges[0] = numFaces; edges[1] = numFaces;
475d6218766SMatthew G. Knepley     ierr = DMPlexCreateSquareBoundary(boundary, lower, upper, edges);CHKERRQ(ierr);
476d6218766SMatthew G. Knepley     break;
477d6218766SMatthew G. Knepley   }
478d6218766SMatthew G. Knepley   case 3:
479d6218766SMatthew G. Knepley   {
480d6218766SMatthew G. Knepley     PetscReal lower[3] = {0.0, 0.0, 0.0};
481d6218766SMatthew G. Knepley     PetscReal upper[3] = {1.0, 1.0, 1.0};
482d6218766SMatthew G. Knepley     PetscInt  faces[3];
483d6218766SMatthew G. Knepley 
484d6218766SMatthew G. Knepley     faces[0] = numFaces; faces[1] = numFaces; faces[2] = numFaces;
485d6218766SMatthew G. Knepley     ierr = DMPlexCreateCubeBoundary(boundary, lower, upper, faces);CHKERRQ(ierr);
486d6218766SMatthew G. Knepley     break;
487d6218766SMatthew G. Knepley   }
488d6218766SMatthew G. Knepley   default:
489d6218766SMatthew G. Knepley     SETERRQ1(comm, PETSC_ERR_SUP, "Dimension not supported: %d", dim);
490d6218766SMatthew G. Knepley   }
491d6218766SMatthew G. Knepley   ierr = DMPlexGenerate(boundary, NULL, interpolate, dm);CHKERRQ(ierr);
492d6218766SMatthew G. Knepley   ierr = DMDestroy(&boundary);CHKERRQ(ierr);
493d6218766SMatthew G. Knepley   PetscFunctionReturn(0);
494d6218766SMatthew G. Knepley }
495d6218766SMatthew G. Knepley 
4963dfda0b1SToby Isaac static PetscErrorCode DMPlexCreateCubeMesh_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY, DMBoundaryType bdZ)
4973dfda0b1SToby Isaac {
498f4eb4c5dSMatthew G. Knepley   PetscInt       markerTop      = 1, faceMarkerTop      = 1;
499f4eb4c5dSMatthew G. Knepley   PetscInt       markerBottom   = 1, faceMarkerBottom   = 1;
500f4eb4c5dSMatthew G. Knepley   PetscInt       markerFront    = 1, faceMarkerFront    = 1;
501f4eb4c5dSMatthew G. Knepley   PetscInt       markerBack     = 1, faceMarkerBack     = 1;
502f4eb4c5dSMatthew G. Knepley   PetscInt       markerRight    = 1, faceMarkerRight    = 1;
503f4eb4c5dSMatthew G. Knepley   PetscInt       markerLeft     = 1, faceMarkerLeft     = 1;
5043dfda0b1SToby Isaac   PetscInt       dim;
5053dfda0b1SToby Isaac   PetscBool      markerSeparate = PETSC_FALSE;
5063dfda0b1SToby Isaac   PetscMPIInt    rank;
5073dfda0b1SToby Isaac   PetscErrorCode ierr;
5083dfda0b1SToby Isaac 
5093dfda0b1SToby Isaac   PetscFunctionBegin;
510f0226e14SMatthew G. Knepley   ierr = DMGetDimension(dm,&dim);CHKERRQ(ierr);
5113dfda0b1SToby Isaac   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
51250ae33c3SToby Isaac   ierr = DMCreateLabel(dm,"marker");CHKERRQ(ierr);
51350ae33c3SToby Isaac   ierr = DMCreateLabel(dm,"Face Sets");CHKERRQ(ierr);
5143dfda0b1SToby Isaac   switch (dim) {
5153dfda0b1SToby Isaac   case 2:
516f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 3;
517f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
518f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 2;
519f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 4;
5203dfda0b1SToby Isaac     break;
5213dfda0b1SToby Isaac   case 3:
522f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
523f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 2;
524f4eb4c5dSMatthew G. Knepley     faceMarkerFront  = 3;
525f4eb4c5dSMatthew G. Knepley     faceMarkerBack   = 4;
526f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 5;
527f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 6;
5283dfda0b1SToby Isaac     break;
5293dfda0b1SToby Isaac   default:
5303dfda0b1SToby Isaac     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Dimension %d not supported",dim);
5313dfda0b1SToby Isaac     break;
5323dfda0b1SToby Isaac   }
533c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);CHKERRQ(ierr);
534f4eb4c5dSMatthew G. Knepley   if (markerSeparate) {
535f4eb4c5dSMatthew G. Knepley     markerBottom = faceMarkerBottom;
536f4eb4c5dSMatthew G. Knepley     markerTop    = faceMarkerTop;
537f4eb4c5dSMatthew G. Knepley     markerFront  = faceMarkerFront;
538f4eb4c5dSMatthew G. Knepley     markerBack   = faceMarkerBack;
539f4eb4c5dSMatthew G. Knepley     markerRight  = faceMarkerRight;
540f4eb4c5dSMatthew G. Knepley     markerLeft   = faceMarkerLeft;
5413dfda0b1SToby Isaac   }
5423dfda0b1SToby Isaac   {
5433dfda0b1SToby Isaac     const PetscInt numXEdges    = !rank ? edges[0] : 0;
5443dfda0b1SToby Isaac     const PetscInt numYEdges    = !rank ? edges[1] : 0;
5453dfda0b1SToby Isaac     const PetscInt numZEdges    = !rank ? edges[2] : 0;
5463dfda0b1SToby Isaac     const PetscInt numXVertices = !rank ? (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ? edges[0] : edges[0]+1) : 0;
5473dfda0b1SToby Isaac     const PetscInt numYVertices = !rank ? (bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ? edges[1] : edges[1]+1) : 0;
54842206facSLisandro Dalcin     const PetscInt numZVertices = !rank ? (bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST ? edges[2] : edges[2]+1) : 0;
5493dfda0b1SToby Isaac     const PetscInt numCells     = numXEdges*numYEdges*numZEdges;
5503dfda0b1SToby Isaac     const PetscInt numXFaces    = numYEdges*numZEdges;
5513dfda0b1SToby Isaac     const PetscInt numYFaces    = numXEdges*numZEdges;
5523dfda0b1SToby Isaac     const PetscInt numZFaces    = numXEdges*numYEdges;
5533dfda0b1SToby Isaac     const PetscInt numTotXFaces = numXVertices*numXFaces;
5543dfda0b1SToby Isaac     const PetscInt numTotYFaces = numYVertices*numYFaces;
5553dfda0b1SToby Isaac     const PetscInt numTotZFaces = numZVertices*numZFaces;
5563dfda0b1SToby Isaac     const PetscInt numFaces     = numTotXFaces + numTotYFaces + numTotZFaces;
5573dfda0b1SToby Isaac     const PetscInt numTotXEdges = numXEdges*numYVertices*numZVertices;
5583dfda0b1SToby Isaac     const PetscInt numTotYEdges = numYEdges*numXVertices*numZVertices;
5593dfda0b1SToby Isaac     const PetscInt numTotZEdges = numZEdges*numXVertices*numYVertices;
5603dfda0b1SToby Isaac     const PetscInt numVertices  = numXVertices*numYVertices*numZVertices;
5613dfda0b1SToby Isaac     const PetscInt numEdges     = numTotXEdges + numTotYEdges + numTotZEdges;
5623dfda0b1SToby Isaac     const PetscInt firstVertex  = (dim == 2) ? numFaces : numCells;
5633dfda0b1SToby Isaac     const PetscInt firstXFace   = (dim == 2) ? 0 : numCells + numVertices;
5643dfda0b1SToby Isaac     const PetscInt firstYFace   = firstXFace + numTotXFaces;
5653dfda0b1SToby Isaac     const PetscInt firstZFace   = firstYFace + numTotYFaces;
5663dfda0b1SToby Isaac     const PetscInt firstXEdge   = numCells + numFaces + numVertices;
5673dfda0b1SToby Isaac     const PetscInt firstYEdge   = firstXEdge + numTotXEdges;
5683dfda0b1SToby Isaac     const PetscInt firstZEdge   = firstYEdge + numTotYEdges;
5693dfda0b1SToby Isaac     Vec            coordinates;
5703dfda0b1SToby Isaac     PetscSection   coordSection;
5713dfda0b1SToby Isaac     PetscScalar   *coords;
5723dfda0b1SToby Isaac     PetscInt       coordSize;
5733dfda0b1SToby Isaac     PetscInt       v, vx, vy, vz;
5743dfda0b1SToby Isaac     PetscInt       c, f, fx, fy, fz, e, ex, ey, ez;
5753dfda0b1SToby Isaac 
5763dfda0b1SToby Isaac     ierr = DMPlexSetChart(dm, 0, numCells+numFaces+numEdges+numVertices);CHKERRQ(ierr);
5773dfda0b1SToby Isaac     for (c = 0; c < numCells; c++) {
5783dfda0b1SToby Isaac       ierr = DMPlexSetConeSize(dm, c, 6);CHKERRQ(ierr);
5793dfda0b1SToby Isaac     }
5803dfda0b1SToby Isaac     for (f = firstXFace; f < firstXFace+numFaces; ++f) {
5813dfda0b1SToby Isaac       ierr = DMPlexSetConeSize(dm, f, 4);CHKERRQ(ierr);
5823dfda0b1SToby Isaac     }
5833dfda0b1SToby Isaac     for (e = firstXEdge; e < firstXEdge+numEdges; ++e) {
5843dfda0b1SToby Isaac       ierr = DMPlexSetConeSize(dm, e, 2);CHKERRQ(ierr);
5853dfda0b1SToby Isaac     }
5863dfda0b1SToby Isaac     ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
5873dfda0b1SToby Isaac     /* Build cells */
5883dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
5893dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
5903dfda0b1SToby Isaac         for (fx = 0; fx < numXEdges; ++fx) {
5913dfda0b1SToby Isaac           PetscInt cell    = (fz*numYEdges + fy)*numXEdges + fx;
5923dfda0b1SToby Isaac           PetscInt faceB   = firstZFace + (fy*numXEdges+fx)*numZVertices +   fz;
5933dfda0b1SToby Isaac           PetscInt faceT   = firstZFace + (fy*numXEdges+fx)*numZVertices + ((fz+1)%numZVertices);
5943dfda0b1SToby Isaac           PetscInt faceF   = firstYFace + (fz*numXEdges+fx)*numYVertices +   fy;
5953dfda0b1SToby Isaac           PetscInt faceK   = firstYFace + (fz*numXEdges+fx)*numYVertices + ((fy+1)%numYVertices);
5963dfda0b1SToby Isaac           PetscInt faceL   = firstXFace + (fz*numYEdges+fy)*numXVertices +   fx;
5973dfda0b1SToby Isaac           PetscInt faceR   = firstXFace + (fz*numYEdges+fy)*numXVertices + ((fx+1)%numXVertices);
5983dfda0b1SToby Isaac                             /* B,  T,  F,  K,  R,  L */
59942206facSLisandro Dalcin           PetscInt ornt[6] = {-4,  0,  0, -1,  0, -4}; /* ??? */
60042206facSLisandro Dalcin           PetscInt cone[6];
6013dfda0b1SToby Isaac 
6023dfda0b1SToby Isaac           /* no boundary twisting in 3D */
6033dfda0b1SToby Isaac           cone[0] = faceB; cone[1] = faceT; cone[2] = faceF; cone[3] = faceK; cone[4] = faceR; cone[5] = faceL;
6043dfda0b1SToby Isaac           ierr    = DMPlexSetCone(dm, cell, cone);CHKERRQ(ierr);
6053dfda0b1SToby Isaac           ierr    = DMPlexSetConeOrientation(dm, cell, ornt);CHKERRQ(ierr);
6063dfda0b1SToby Isaac         }
6073dfda0b1SToby Isaac       }
6083dfda0b1SToby Isaac     }
6093dfda0b1SToby Isaac     /* Build x faces */
6103dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
6113dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
6123dfda0b1SToby Isaac         for (fx = 0; fx < numXVertices; ++fx) {
6133dfda0b1SToby Isaac           PetscInt face    = firstXFace + (fz*numYEdges+fy)*numXVertices + fx;
6143dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (  fy*                 numXVertices+fx)*numZEdges + fz;
6153dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (((fy+1)%numYVertices)*numXVertices+fx)*numZEdges + fz;
6163dfda0b1SToby Isaac           PetscInt edgeB   = firstYEdge + (  fz*                 numXVertices+fx)*numYEdges + fy;
6173dfda0b1SToby Isaac           PetscInt edgeT   = firstYEdge + (((fz+1)%numZVertices)*numXVertices+fx)*numYEdges + fy;
6183dfda0b1SToby Isaac           PetscInt ornt[4] = {0, 0, -2, -2};
6193dfda0b1SToby Isaac           PetscInt cone[4];
6203dfda0b1SToby Isaac 
6213dfda0b1SToby Isaac           if (dim == 3) {
6223dfda0b1SToby Isaac             /* markers */
6233dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
6243dfda0b1SToby Isaac               if (fx == numXVertices-1) {
625c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerRight);CHKERRQ(ierr);
626c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", face, markerRight);CHKERRQ(ierr);
6273dfda0b1SToby Isaac               }
6283dfda0b1SToby Isaac               else if (fx == 0) {
629c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerLeft);CHKERRQ(ierr);
630c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", face, markerLeft);CHKERRQ(ierr);
6313dfda0b1SToby Isaac               }
6323dfda0b1SToby Isaac             }
6333dfda0b1SToby Isaac           }
6343dfda0b1SToby Isaac           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
6353dfda0b1SToby Isaac           ierr    = DMPlexSetCone(dm, face, cone);CHKERRQ(ierr);
6363dfda0b1SToby Isaac           ierr    = DMPlexSetConeOrientation(dm, face, ornt);CHKERRQ(ierr);
6373dfda0b1SToby Isaac         }
6383dfda0b1SToby Isaac       }
6393dfda0b1SToby Isaac     }
6403dfda0b1SToby Isaac     /* Build y faces */
6413dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
64242206facSLisandro Dalcin       for (fx = 0; fx < numXEdges; ++fx) {
6433dfda0b1SToby Isaac         for (fy = 0; fy < numYVertices; ++fy) {
6443dfda0b1SToby Isaac           PetscInt face    = firstYFace + (fz*numXEdges+fx)*numYVertices + fy;
6453dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy*numXVertices+  fx                 )*numZEdges + fz;
6463dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (fy*numXVertices+((fx+1)%numXVertices))*numZEdges + fz;
6473dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (  fz                 *numYVertices+fy)*numXEdges + fx;
6483dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (((fz+1)%numZVertices)*numYVertices+fy)*numXEdges + fx;
6493dfda0b1SToby Isaac           PetscInt ornt[4] = {0, 0, -2, -2};
6503dfda0b1SToby Isaac           PetscInt cone[4];
6513dfda0b1SToby Isaac 
6523dfda0b1SToby Isaac           if (dim == 3) {
6533dfda0b1SToby Isaac             /* markers */
6543dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
6553dfda0b1SToby Isaac               if (fy == numYVertices-1) {
656c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerBack);CHKERRQ(ierr);
657c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", face, markerBack);CHKERRQ(ierr);
6583dfda0b1SToby Isaac               }
6593dfda0b1SToby Isaac               else if (fy == 0) {
660c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerFront);CHKERRQ(ierr);
661c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", face, markerFront);CHKERRQ(ierr);
6623dfda0b1SToby Isaac               }
6633dfda0b1SToby Isaac             }
6643dfda0b1SToby Isaac           }
6653dfda0b1SToby Isaac           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
6663dfda0b1SToby Isaac           ierr    = DMPlexSetCone(dm, face, cone);CHKERRQ(ierr);
6673dfda0b1SToby Isaac           ierr    = DMPlexSetConeOrientation(dm, face, ornt);CHKERRQ(ierr);
6683dfda0b1SToby Isaac         }
6693dfda0b1SToby Isaac       }
6703dfda0b1SToby Isaac     }
6713dfda0b1SToby Isaac     /* Build z faces */
6723dfda0b1SToby Isaac     for (fy = 0; fy < numYEdges; ++fy) {
6733dfda0b1SToby Isaac       for (fx = 0; fx < numXEdges; ++fx) {
6743dfda0b1SToby Isaac         for (fz = 0; fz < numZVertices; fz++) {
6753dfda0b1SToby Isaac           PetscInt face    = firstZFace + (fy*numXEdges+fx)*numZVertices + fz;
6763dfda0b1SToby Isaac           PetscInt edgeL   = firstYEdge + (fz*numXVertices+  fx                 )*numYEdges + fy;
6773dfda0b1SToby Isaac           PetscInt edgeR   = firstYEdge + (fz*numXVertices+((fx+1)%numXVertices))*numYEdges + fy;
6783dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz*numYVertices+  fy                 )*numXEdges + fx;
6793dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (fz*numYVertices+((fy+1)%numYVertices))*numXEdges + fx;
6803dfda0b1SToby Isaac           PetscInt ornt[4] = {0, 0, -2, -2};
6813dfda0b1SToby Isaac           PetscInt cone[4];
6823dfda0b1SToby Isaac 
6833dfda0b1SToby Isaac           if (dim == 2) {
6843dfda0b1SToby Isaac             if (bdX == DM_BOUNDARY_TWIST && fx == numXEdges-1) {edgeR += numYEdges-1-2*fy; ornt[1] = -2;}
6853dfda0b1SToby Isaac             if (bdY == DM_BOUNDARY_TWIST && fy == numYEdges-1) {edgeT += numXEdges-1-2*fx; ornt[2] =  0;}
6863dfda0b1SToby Isaac           }
6873dfda0b1SToby Isaac           else {
6883dfda0b1SToby Isaac             /* markers */
6893dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
6903dfda0b1SToby Isaac               if (fz == numZVertices-1) {
691c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerTop);CHKERRQ(ierr);
692c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", face, markerTop);CHKERRQ(ierr);
6933dfda0b1SToby Isaac               }
6943dfda0b1SToby Isaac               else if (fz == 0) {
695c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerBottom);CHKERRQ(ierr);
696c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", face, markerBottom);CHKERRQ(ierr);
6973dfda0b1SToby Isaac               }
6983dfda0b1SToby Isaac             }
6993dfda0b1SToby Isaac           }
7003dfda0b1SToby Isaac           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
7013dfda0b1SToby Isaac           ierr    = DMPlexSetCone(dm, face, cone);CHKERRQ(ierr);
7023dfda0b1SToby Isaac           ierr    = DMPlexSetConeOrientation(dm, face, ornt);CHKERRQ(ierr);
7033dfda0b1SToby Isaac         }
7043dfda0b1SToby Isaac       }
7053dfda0b1SToby Isaac     }
7063dfda0b1SToby Isaac     /* Build Z edges*/
7073dfda0b1SToby Isaac     for (vy = 0; vy < numYVertices; vy++) {
7083dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
7093dfda0b1SToby Isaac         for (ez = 0; ez < numZEdges; ez++) {
7103dfda0b1SToby Isaac           const PetscInt edge    = firstZEdge  + (vy*numXVertices+vx)*numZEdges + ez;
7113dfda0b1SToby Isaac           const PetscInt vertexB = firstVertex + (  ez                 *numYVertices+vy)*numXVertices + vx;
7123dfda0b1SToby Isaac           const PetscInt vertexT = firstVertex + (((ez+1)%numZVertices)*numYVertices+vy)*numXVertices + vx;
7133dfda0b1SToby Isaac           PetscInt       cone[2];
7143dfda0b1SToby Isaac 
7153dfda0b1SToby Isaac           if (dim == 3) {
7163dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
7173dfda0b1SToby Isaac               if (vx == numXVertices-1) {
718c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerRight);CHKERRQ(ierr);
7193dfda0b1SToby Isaac               }
7203dfda0b1SToby Isaac               else if (vx == 0) {
721c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerLeft);CHKERRQ(ierr);
7223dfda0b1SToby Isaac               }
7233dfda0b1SToby Isaac             }
7243dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
7253dfda0b1SToby Isaac               if (vy == numYVertices-1) {
726c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerBack);CHKERRQ(ierr);
7273dfda0b1SToby Isaac               }
7283dfda0b1SToby Isaac               else if (vy == 0) {
729c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerFront);CHKERRQ(ierr);
7303dfda0b1SToby Isaac               }
7313dfda0b1SToby Isaac             }
7323dfda0b1SToby Isaac           }
7333dfda0b1SToby Isaac           cone[0] = vertexB; cone[1] = vertexT;
7343dfda0b1SToby Isaac           ierr = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr);
7353dfda0b1SToby Isaac         }
7363dfda0b1SToby Isaac       }
7373dfda0b1SToby Isaac     }
7383dfda0b1SToby Isaac     /* Build Y edges*/
7393dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
7403dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
7413dfda0b1SToby Isaac         for (ey = 0; ey < numYEdges; ey++) {
7423dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdY == DM_BOUNDARY_TWIST && ey == numYEdges-1) ? (numXVertices-vx-1) : (vz*numYVertices+((ey+1)%numYVertices))*numXVertices + vx;
7433dfda0b1SToby Isaac           const PetscInt edge    = firstYEdge  + (vz*numXVertices+vx)*numYEdges + ey;
7443dfda0b1SToby Isaac           const PetscInt vertexF = firstVertex + (vz*numYVertices+ey)*numXVertices + vx;
7453dfda0b1SToby Isaac           const PetscInt vertexK = firstVertex + nextv;
7463dfda0b1SToby Isaac           PetscInt       cone[2];
7473dfda0b1SToby Isaac 
7483dfda0b1SToby Isaac           cone[0] = vertexF; cone[1] = vertexK;
7493dfda0b1SToby Isaac           ierr = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr);
7503dfda0b1SToby Isaac           if (dim == 2) {
7513dfda0b1SToby Isaac             if ((bdX != DM_BOUNDARY_PERIODIC) && (bdX != DM_BOUNDARY_TWIST)) {
7523dfda0b1SToby Isaac               if (vx == numXVertices-1) {
753c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", edge, faceMarkerRight);CHKERRQ(ierr);
754c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge,    markerRight);CHKERRQ(ierr);
755c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", cone[0], markerRight);CHKERRQ(ierr);
7563dfda0b1SToby Isaac                 if (ey == numYEdges-1) {
757c58f1c22SToby Isaac                   ierr = DMSetLabelValue(dm, "marker", cone[1], markerRight);CHKERRQ(ierr);
7583dfda0b1SToby Isaac                 }
7593dfda0b1SToby Isaac               }
7603dfda0b1SToby Isaac               else if (vx == 0) {
761c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", edge, faceMarkerLeft);CHKERRQ(ierr);
762c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge,    markerLeft);CHKERRQ(ierr);
763c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", cone[0], markerLeft);CHKERRQ(ierr);
7643dfda0b1SToby Isaac                 if (ey == numYEdges-1) {
765c58f1c22SToby Isaac                   ierr = DMSetLabelValue(dm, "marker", cone[1], markerLeft);CHKERRQ(ierr);
7663dfda0b1SToby Isaac                 }
7673dfda0b1SToby Isaac               }
7683dfda0b1SToby Isaac             }
7693dfda0b1SToby Isaac           }
7703dfda0b1SToby Isaac           else {
7713dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
7723dfda0b1SToby Isaac               if (vx == numXVertices-1) {
773c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerRight);CHKERRQ(ierr);
7743dfda0b1SToby Isaac               }
7753dfda0b1SToby Isaac               else if (vx == 0) {
776c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerLeft);CHKERRQ(ierr);
7773dfda0b1SToby Isaac               }
7783dfda0b1SToby Isaac             }
7793dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
7803dfda0b1SToby Isaac               if (vz == numZVertices-1) {
781c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerTop);CHKERRQ(ierr);
7823dfda0b1SToby Isaac               }
7833dfda0b1SToby Isaac               else if (vz == 0) {
784c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerBottom);CHKERRQ(ierr);
7853dfda0b1SToby Isaac               }
7863dfda0b1SToby Isaac             }
7873dfda0b1SToby Isaac           }
7883dfda0b1SToby Isaac         }
7893dfda0b1SToby Isaac       }
7903dfda0b1SToby Isaac     }
7913dfda0b1SToby Isaac     /* Build X edges*/
7923dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
7933dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; vy++) {
7943dfda0b1SToby Isaac         for (ex = 0; ex < numXEdges; ex++) {
7953dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdX == DM_BOUNDARY_TWIST && ex == numXEdges-1) ? (numYVertices-vy-1)*numXVertices : (vz*numYVertices+vy)*numXVertices + (ex+1)%numXVertices;
7963dfda0b1SToby Isaac           const PetscInt edge    = firstXEdge  + (vz*numYVertices+vy)*numXEdges + ex;
7973dfda0b1SToby Isaac           const PetscInt vertexL = firstVertex + (vz*numYVertices+vy)*numXVertices + ex;
7983dfda0b1SToby Isaac           const PetscInt vertexR = firstVertex + nextv;
7993dfda0b1SToby Isaac           PetscInt       cone[2];
8003dfda0b1SToby Isaac 
8013dfda0b1SToby Isaac           cone[0] = vertexL; cone[1] = vertexR;
8023dfda0b1SToby Isaac           ierr = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr);
8033dfda0b1SToby Isaac           if (dim == 2) {
8043dfda0b1SToby Isaac             if ((bdY != DM_BOUNDARY_PERIODIC) && (bdY != DM_BOUNDARY_TWIST)) {
8053dfda0b1SToby Isaac               if (vy == numYVertices-1) {
806c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", edge, faceMarkerTop);CHKERRQ(ierr);
807c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge,    markerTop);CHKERRQ(ierr);
808c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", cone[0], markerTop);CHKERRQ(ierr);
8093dfda0b1SToby Isaac                 if (ex == numXEdges-1) {
810c58f1c22SToby Isaac                   ierr = DMSetLabelValue(dm, "marker", cone[1], markerTop);CHKERRQ(ierr);
8113dfda0b1SToby Isaac                 }
8123dfda0b1SToby Isaac               }
8133dfda0b1SToby Isaac               else if (vy == 0) {
814c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", edge, faceMarkerBottom);CHKERRQ(ierr);
815c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge,    markerBottom);CHKERRQ(ierr);
816c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", cone[0], markerBottom);CHKERRQ(ierr);
8173dfda0b1SToby Isaac                 if (ex == numXEdges-1) {
818c58f1c22SToby Isaac                   ierr = DMSetLabelValue(dm, "marker", cone[1], markerBottom);CHKERRQ(ierr);
8193dfda0b1SToby Isaac                 }
8203dfda0b1SToby Isaac               }
8213dfda0b1SToby Isaac             }
8223dfda0b1SToby Isaac           }
8233dfda0b1SToby Isaac           else {
8243dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
8253dfda0b1SToby Isaac               if (vy == numYVertices-1) {
826c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerBack);CHKERRQ(ierr);
8273dfda0b1SToby Isaac               }
8283dfda0b1SToby Isaac               else if (vy == 0) {
829c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerFront);CHKERRQ(ierr);
8303dfda0b1SToby Isaac               }
8313dfda0b1SToby Isaac             }
8323dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
8333dfda0b1SToby Isaac               if (vz == numZVertices-1) {
834c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerTop);CHKERRQ(ierr);
8353dfda0b1SToby Isaac               }
8363dfda0b1SToby Isaac               else if (vz == 0) {
837c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerBottom);CHKERRQ(ierr);
8383dfda0b1SToby Isaac               }
8393dfda0b1SToby Isaac             }
8403dfda0b1SToby Isaac           }
8413dfda0b1SToby Isaac         }
8423dfda0b1SToby Isaac       }
8433dfda0b1SToby Isaac     }
8443dfda0b1SToby Isaac     ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
8453dfda0b1SToby Isaac     ierr = DMPlexStratify(dm);CHKERRQ(ierr);
8463dfda0b1SToby Isaac     /* Build coordinates */
8473dfda0b1SToby Isaac     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
8483dfda0b1SToby Isaac     ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
8493dfda0b1SToby Isaac     ierr = PetscSectionSetFieldComponents(coordSection, 0, dim);CHKERRQ(ierr);
8503dfda0b1SToby Isaac     ierr = PetscSectionSetChart(coordSection, firstVertex, firstVertex+numVertices);CHKERRQ(ierr);
8513dfda0b1SToby Isaac     for (v = firstVertex; v < firstVertex+numVertices; ++v) {
8523dfda0b1SToby Isaac       ierr = PetscSectionSetDof(coordSection, v, dim);CHKERRQ(ierr);
8533dfda0b1SToby Isaac       ierr = PetscSectionSetFieldDof(coordSection, v, 0, dim);CHKERRQ(ierr);
8543dfda0b1SToby Isaac     }
8553dfda0b1SToby Isaac     ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
8563dfda0b1SToby Isaac     ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
8578b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
858da16285aSMichael Lange     ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
8593dfda0b1SToby Isaac     ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
8608b9ced59SLisandro Dalcin     ierr = VecSetBlockSize(coordinates, dim);CHKERRQ(ierr);
8613dfda0b1SToby Isaac     ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
8623dfda0b1SToby Isaac     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
8633dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; ++vz) {
8643dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; ++vy) {
8653dfda0b1SToby Isaac         for (vx = 0; vx < numXVertices; ++vx) {
8663dfda0b1SToby Isaac           coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+0] = lower[0] + ((upper[0] - lower[0])/numXEdges)*vx;
8673dfda0b1SToby Isaac           coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+1] = lower[1] + ((upper[1] - lower[1])/numYEdges)*vy;
8683dfda0b1SToby Isaac           if (dim == 3) {
8693dfda0b1SToby Isaac             coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+2] = lower[2] + ((upper[2] - lower[2])/numZEdges)*vz;
8703dfda0b1SToby Isaac           }
8713dfda0b1SToby Isaac         }
8723dfda0b1SToby Isaac       }
8733dfda0b1SToby Isaac     }
8743dfda0b1SToby Isaac     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
8753dfda0b1SToby Isaac     ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
8763dfda0b1SToby Isaac     ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
8773dfda0b1SToby Isaac   }
8783dfda0b1SToby Isaac   PetscFunctionReturn(0);
8793dfda0b1SToby Isaac }
8803dfda0b1SToby Isaac 
88126492d91SMatthew G. Knepley /*@
88226492d91SMatthew G. Knepley   DMPlexCreateHexBoxMesh - Creates a mesh on the tensor product of unit intervals (box) using hexahedra.
88326492d91SMatthew G. Knepley 
88426492d91SMatthew G. Knepley   Collective on MPI_Comm
88526492d91SMatthew G. Knepley 
88626492d91SMatthew G. Knepley   Input Parameters:
88726492d91SMatthew G. Knepley + comm  - The communicator for the DM object
88826492d91SMatthew G. Knepley . dim   - The spatial dimension
889fbf5b483SMatthew G. Knepley . periodicX - The boundary type for the X direction
890fbf5b483SMatthew G. Knepley . periodicY - The boundary type for the Y direction
891fbf5b483SMatthew G. Knepley . periodicZ - The boundary type for the Z direction
89226492d91SMatthew G. Knepley - cells - The number of cells in each direction
89326492d91SMatthew G. Knepley 
89426492d91SMatthew G. Knepley   Output Parameter:
89526492d91SMatthew G. Knepley . dm  - The DM object
89626492d91SMatthew G. Knepley 
897d6218766SMatthew G. Knepley   Note: Here is the numbering returned for 2 cells in each direction:
898d6218766SMatthew G. Knepley $ 22--8-23--9--24
899d6218766SMatthew G. Knepley $  |     |     |
900d6218766SMatthew G. Knepley $ 13  2 14  3  15
901d6218766SMatthew G. Knepley $  |     |     |
902d6218766SMatthew G. Knepley $ 19--6-20--7--21
903d6218766SMatthew G. Knepley $  |     |     |
904d6218766SMatthew G. Knepley $ 10  0 11  1 12
905d6218766SMatthew G. Knepley $  |     |     |
906d6218766SMatthew G. Knepley $ 16--4-17--5--18
907d6218766SMatthew G. Knepley 
90826492d91SMatthew G. Knepley   Level: beginner
90926492d91SMatthew G. Knepley 
91026492d91SMatthew G. Knepley .keywords: DM, create
91126492d91SMatthew G. Knepley .seealso: DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
91226492d91SMatthew G. Knepley @*/
913bff4a2f0SMatthew G. Knepley PetscErrorCode DMPlexCreateHexBoxMesh(MPI_Comm comm, PetscInt dim, const PetscInt cells[], DMBoundaryType periodicX, DMBoundaryType periodicY, DMBoundaryType periodicZ, DM *dm)
914a6dfd86eSKarl Rupp {
915c8c68bd8SToby Isaac   PetscInt       i;
916552f7358SJed Brown   PetscErrorCode ierr;
917552f7358SJed Brown 
918552f7358SJed Brown   PetscFunctionBegin;
919d67d2e29SLisandro Dalcin   PetscValidPointer(dm, 7);
920552f7358SJed Brown   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
921552f7358SJed Brown   PetscValidLogicalCollectiveInt(*dm,dim,2);
922552f7358SJed Brown   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
923c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(*dm, dim);CHKERRQ(ierr);
924552f7358SJed Brown   switch (dim) {
925552f7358SJed Brown   case 2:
926552f7358SJed Brown   {
927d6218766SMatthew G. Knepley     PetscReal lower[3] = {0.0, 0.0, 0.0};
928d6218766SMatthew G. Knepley     PetscReal upper[3] = {1.0, 1.0, 0.0};
929*d35a276dSMatthew G. Knepley     PetscInt  edges[3] = {cells[0], cells[1], 0};
930552f7358SJed Brown 
931*d35a276dSMatthew G. Knepley     ierr = DMPlexCreateCubeMesh_Internal(*dm, lower, upper, edges, periodicX, periodicY, DM_BOUNDARY_NONE);CHKERRQ(ierr);
932c8c68bd8SToby Isaac     if (periodicX == DM_BOUNDARY_PERIODIC || periodicX == DM_BOUNDARY_TWIST ||
933c8c68bd8SToby Isaac         periodicY == DM_BOUNDARY_PERIODIC || periodicY == DM_BOUNDARY_TWIST) {
934c8c68bd8SToby Isaac       PetscReal      L[2];
935c8c68bd8SToby Isaac       PetscReal      maxCell[2];
93670012f4bSToby Isaac       DMBoundaryType bdType[2];
937c8c68bd8SToby Isaac 
93870012f4bSToby Isaac       bdType[0] = periodicX;
93970012f4bSToby Isaac       bdType[1] = periodicY;
940c8c68bd8SToby Isaac       for (i = 0; i < dim; i++) {
941c8c68bd8SToby Isaac         L[i]       = upper[i] - lower[i];
942c8c68bd8SToby Isaac         maxCell[i] = 1.1 * (L[i] / cells[i]);
943c8c68bd8SToby Isaac       }
944c8c68bd8SToby Isaac 
945c8c68bd8SToby Isaac       ierr = DMSetPeriodicity(*dm,maxCell,L,bdType);CHKERRQ(ierr);
946c8c68bd8SToby Isaac     }
947552f7358SJed Brown     break;
948552f7358SJed Brown   }
949552f7358SJed Brown   case 3:
950552f7358SJed Brown   {
951552f7358SJed Brown     PetscReal lower[3] = {0.0, 0.0, 0.0};
952552f7358SJed Brown     PetscReal upper[3] = {1.0, 1.0, 1.0};
953552f7358SJed Brown 
9543dfda0b1SToby Isaac     ierr = DMPlexCreateCubeMesh_Internal(*dm, lower, upper, cells, periodicX, periodicY, periodicZ);CHKERRQ(ierr);
955c8c68bd8SToby Isaac     if (periodicX == DM_BOUNDARY_PERIODIC || periodicX == DM_BOUNDARY_TWIST ||
956c8c68bd8SToby Isaac         periodicY == DM_BOUNDARY_PERIODIC || periodicY == DM_BOUNDARY_TWIST ||
957c8c68bd8SToby Isaac         periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
958c8c68bd8SToby Isaac       PetscReal      L[3];
959c8c68bd8SToby Isaac       PetscReal      maxCell[3];
96035e84bc7SToby Isaac       DMBoundaryType bdType[3];
961c8c68bd8SToby Isaac 
96235e84bc7SToby Isaac       bdType[0] = periodicX;
96335e84bc7SToby Isaac       bdType[1] = periodicY;
96435e84bc7SToby Isaac       bdType[2] = periodicZ;
965c8c68bd8SToby Isaac       for (i = 0; i < dim; i++) {
966c8c68bd8SToby Isaac         L[i]       = upper[i] - lower[i];
967c8c68bd8SToby Isaac         maxCell[i] = 1.1 * (L[i] / cells[i]);
968c8c68bd8SToby Isaac       }
969c8c68bd8SToby Isaac 
970c8c68bd8SToby Isaac       ierr = DMSetPeriodicity(*dm,maxCell,L,bdType);CHKERRQ(ierr);
971c8c68bd8SToby Isaac     }
972552f7358SJed Brown     break;
973552f7358SJed Brown   }
974552f7358SJed Brown   default:
975552f7358SJed Brown     SETERRQ1(comm, PETSC_ERR_SUP, "Dimension not supported: %d", dim);
976552f7358SJed Brown   }
977552f7358SJed Brown   PetscFunctionReturn(0);
978552f7358SJed Brown }
979552f7358SJed Brown 
9800510c589SMatthew G. Knepley /*@
9810510c589SMatthew G. Knepley   DMPlexCreateHexCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using hexahedra.
9820510c589SMatthew G. Knepley 
9830510c589SMatthew G. Knepley   Collective on MPI_Comm
9840510c589SMatthew G. Knepley 
9850510c589SMatthew G. Knepley   Input Parameters:
9860510c589SMatthew G. Knepley + comm      - The communicator for the DM object
987dbc1dc17SMatthew G. Knepley . numRefine - The number of regular refinements to the basic 5 cell structure
9880510c589SMatthew G. Knepley - periodicZ - The boundary type for the Z direction
9890510c589SMatthew G. Knepley 
9900510c589SMatthew G. Knepley   Output Parameter:
9910510c589SMatthew G. Knepley . dm  - The DM object
9920510c589SMatthew G. Knepley 
9930510c589SMatthew G. Knepley   Note: Here is the output numbering looking from the bottom of the cylinder:
9940510c589SMatthew G. Knepley $       17-----14
9950510c589SMatthew G. Knepley $        |     |
9960510c589SMatthew G. Knepley $        |  2  |
9970510c589SMatthew G. Knepley $        |     |
9980510c589SMatthew G. Knepley $ 17-----8-----7-----14
9990510c589SMatthew G. Knepley $  |     |     |     |
10000510c589SMatthew G. Knepley $  |  3  |  0  |  1  |
10010510c589SMatthew G. Knepley $  |     |     |     |
10020510c589SMatthew G. Knepley $ 19-----5-----6-----13
10030510c589SMatthew G. Knepley $        |     |
10040510c589SMatthew G. Knepley $        |  4  |
10050510c589SMatthew G. Knepley $        |     |
10060510c589SMatthew G. Knepley $       19-----13
10070510c589SMatthew G. Knepley $
10080510c589SMatthew G. Knepley $ and up through the top
10090510c589SMatthew G. Knepley $
10100510c589SMatthew G. Knepley $       18-----16
10110510c589SMatthew G. Knepley $        |     |
10120510c589SMatthew G. Knepley $        |  2  |
10130510c589SMatthew G. Knepley $        |     |
10140510c589SMatthew G. Knepley $ 18----10----11-----16
10150510c589SMatthew G. Knepley $  |     |     |     |
10160510c589SMatthew G. Knepley $  |  3  |  0  |  1  |
10170510c589SMatthew G. Knepley $  |     |     |     |
10180510c589SMatthew G. Knepley $ 20-----9----12-----15
10190510c589SMatthew G. Knepley $        |     |
10200510c589SMatthew G. Knepley $        |  4  |
10210510c589SMatthew G. Knepley $        |     |
10220510c589SMatthew G. Knepley $       20-----15
10230510c589SMatthew G. Knepley 
10240510c589SMatthew G. Knepley   Level: beginner
10250510c589SMatthew G. Knepley 
10260510c589SMatthew G. Knepley .keywords: DM, create
10270510c589SMatthew G. Knepley .seealso: DMPlexCreateHexBoxMesh(), DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
10280510c589SMatthew G. Knepley @*/
1029dbc1dc17SMatthew G. Knepley PetscErrorCode DMPlexCreateHexCylinderMesh(MPI_Comm comm, PetscInt numRefine, DMBoundaryType periodicZ, DM *dm)
10300510c589SMatthew G. Knepley {
10310510c589SMatthew G. Knepley   const PetscInt dim = 3;
1032006a8963SMatthew G. Knepley   PetscInt       numCells, numVertices, r;
10330510c589SMatthew G. Knepley   PetscErrorCode ierr;
10340510c589SMatthew G. Knepley 
10350510c589SMatthew G. Knepley   PetscFunctionBegin;
10360510c589SMatthew G. Knepley   PetscValidPointer(dm, 4);
1037dbc1dc17SMatthew G. Knepley   if (numRefine < 0) SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Number of refinements %D cannot be negative", numRefine);
10380510c589SMatthew G. Knepley   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
10390510c589SMatthew G. Knepley   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
10400510c589SMatthew G. Knepley   ierr = DMSetDimension(*dm, dim);CHKERRQ(ierr);
10410510c589SMatthew G. Knepley   /* Create topology */
10420510c589SMatthew G. Knepley   {
10430510c589SMatthew G. Knepley     PetscInt cone[8], c;
10440510c589SMatthew G. Knepley 
1045006a8963SMatthew G. Knepley     numCells    = 5;
1046006a8963SMatthew G. Knepley     numVertices = 16;
1047006a8963SMatthew G. Knepley     if (periodicZ == DM_BOUNDARY_PERIODIC) {
1048006a8963SMatthew G. Knepley       numCells *= 2;
1049006a8963SMatthew G. Knepley     }
10500510c589SMatthew G. Knepley     ierr = DMPlexSetChart(*dm, 0, numCells+numVertices);CHKERRQ(ierr);
10510510c589SMatthew G. Knepley     for (c = 0; c < numCells; c++) {ierr = DMPlexSetConeSize(*dm, c, 8);CHKERRQ(ierr);}
10520510c589SMatthew G. Knepley     ierr = DMSetUp(*dm);CHKERRQ(ierr);
1053006a8963SMatthew G. Knepley     if (periodicZ == DM_BOUNDARY_PERIODIC) {
1054006a8963SMatthew G. Knepley       cone[0] = 10; cone[1] = 11; cone[2] = 12; cone[3] = 13;
1055006a8963SMatthew G. Knepley       cone[4] = 14; cone[5] = 15; cone[6] = 16; cone[7] = 17;
1056006a8963SMatthew G. Knepley       ierr = DMPlexSetCone(*dm, 0, cone);CHKERRQ(ierr);
1057006a8963SMatthew G. Knepley       cone[0] = 11; cone[1] = 18; cone[2] = 19; cone[3] = 12;
1058006a8963SMatthew G. Knepley       cone[4] = 17; cone[5] = 16; cone[6] = 21; cone[7] = 20;
1059006a8963SMatthew G. Knepley       ierr = DMPlexSetCone(*dm, 1, cone);CHKERRQ(ierr);
1060006a8963SMatthew G. Knepley       cone[0] = 13; cone[1] = 12; cone[2] = 19; cone[3] = 22;
1061006a8963SMatthew G. Knepley       cone[4] = 15; cone[5] = 23; cone[6] = 21; cone[7] = 16;
1062006a8963SMatthew G. Knepley       ierr = DMPlexSetCone(*dm, 2, cone);CHKERRQ(ierr);
1063006a8963SMatthew G. Knepley       cone[0] = 24; cone[1] = 10; cone[2] = 13; cone[3] = 22;
1064006a8963SMatthew G. Knepley       cone[4] = 25; cone[5] = 23; cone[6] = 15; cone[7] = 14;
1065006a8963SMatthew G. Knepley       ierr = DMPlexSetCone(*dm, 3, cone);CHKERRQ(ierr);
1066006a8963SMatthew G. Knepley       cone[0] = 24; cone[1] = 18; cone[2] = 11; cone[3] = 10;
1067006a8963SMatthew G. Knepley       cone[4] = 25; cone[5] = 14; cone[6] = 17; cone[7] = 20;
1068006a8963SMatthew G. Knepley       ierr = DMPlexSetCone(*dm, 4, cone);CHKERRQ(ierr);
1069006a8963SMatthew G. Knepley 
1070006a8963SMatthew G. Knepley       cone[0] = 14; cone[1] = 17; cone[2] = 16; cone[3] = 15;
1071006a8963SMatthew G. Knepley       cone[4] = 10; cone[5] = 13; cone[6] = 12; cone[7] = 11;
1072006a8963SMatthew G. Knepley       ierr = DMPlexSetCone(*dm, 5, cone);CHKERRQ(ierr);
1073006a8963SMatthew G. Knepley       cone[0] = 17; cone[1] = 20; cone[2] = 21; cone[3] = 16;
1074006a8963SMatthew G. Knepley       cone[4] = 11; cone[5] = 12; cone[6] = 19; cone[7] = 18;
1075006a8963SMatthew G. Knepley       ierr = DMPlexSetCone(*dm, 6, cone);CHKERRQ(ierr);
1076006a8963SMatthew G. Knepley       cone[0] = 15; cone[1] = 16; cone[2] = 21; cone[3] = 23;
1077006a8963SMatthew G. Knepley       cone[4] = 13; cone[5] = 22; cone[6] = 19; cone[7] = 12;
1078006a8963SMatthew G. Knepley       ierr = DMPlexSetCone(*dm, 7, cone);CHKERRQ(ierr);
1079006a8963SMatthew G. Knepley       cone[0] = 25; cone[1] = 14; cone[2] = 15; cone[3] = 23;
1080006a8963SMatthew G. Knepley       cone[4] = 24; cone[5] = 22; cone[6] = 13; cone[7] = 10;
1081006a8963SMatthew G. Knepley       ierr = DMPlexSetCone(*dm, 8, cone);CHKERRQ(ierr);
1082006a8963SMatthew G. Knepley       cone[0] = 25; cone[1] = 20; cone[2] = 17; cone[3] = 14;
1083006a8963SMatthew G. Knepley       cone[4] = 24; cone[5] = 10; cone[6] = 11; cone[7] = 18;
1084006a8963SMatthew G. Knepley       ierr = DMPlexSetCone(*dm, 9, cone);CHKERRQ(ierr);
1085006a8963SMatthew G. Knepley     } else {
10860510c589SMatthew G. Knepley       cone[0] =  5; cone[1] =  6; cone[2] =  7; cone[3] =  8;
10870510c589SMatthew G. Knepley       cone[4] =  9; cone[5] = 10; cone[6] = 11; cone[7] = 12;
10880510c589SMatthew G. Knepley       ierr = DMPlexSetCone(*dm, 0, cone);CHKERRQ(ierr);
10890510c589SMatthew G. Knepley       cone[0] =  6; cone[1] = 13; cone[2] = 14; cone[3] =  7;
10900510c589SMatthew G. Knepley       cone[4] = 12; cone[5] = 11; cone[6] = 16; cone[7] = 15;
10910510c589SMatthew G. Knepley       ierr = DMPlexSetCone(*dm, 1, cone);CHKERRQ(ierr);
10920510c589SMatthew G. Knepley       cone[0] =  8; cone[1] =  7; cone[2] = 14; cone[3] = 17;
10930510c589SMatthew G. Knepley       cone[4] = 10; cone[5] = 18; cone[6] = 16; cone[7] = 11;
10940510c589SMatthew G. Knepley       ierr = DMPlexSetCone(*dm, 2, cone);CHKERRQ(ierr);
10950510c589SMatthew G. Knepley       cone[0] = 19; cone[1] =  5; cone[2] =  8; cone[3] = 17;
10960510c589SMatthew G. Knepley       cone[4] = 20; cone[5] = 18; cone[6] = 10; cone[7] =  9;
10970510c589SMatthew G. Knepley       ierr = DMPlexSetCone(*dm, 3, cone);CHKERRQ(ierr);
10980510c589SMatthew G. Knepley       cone[0] = 19; cone[1] = 13; cone[2] =  6; cone[3] =  5;
10990510c589SMatthew G. Knepley       cone[4] = 20; cone[5] =  9; cone[6] = 12; cone[7] = 15;
11000510c589SMatthew G. Knepley       ierr = DMPlexSetCone(*dm, 4, cone);CHKERRQ(ierr);
1101006a8963SMatthew G. Knepley     }
11020510c589SMatthew G. Knepley     ierr = DMPlexSymmetrize(*dm);CHKERRQ(ierr);
11030510c589SMatthew G. Knepley     ierr = DMPlexStratify(*dm);CHKERRQ(ierr);
11040510c589SMatthew G. Knepley   }
11050510c589SMatthew G. Knepley   /* Interpolate */
11060510c589SMatthew G. Knepley   {
11070510c589SMatthew G. Knepley     DM idm = NULL;
11080510c589SMatthew G. Knepley 
11090510c589SMatthew G. Knepley     ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr);
11100510c589SMatthew G. Knepley     ierr = DMDestroy(dm);CHKERRQ(ierr);
11110510c589SMatthew G. Knepley     *dm  = idm;
11120510c589SMatthew G. Knepley   }
1113dbc1dc17SMatthew G. Knepley   /* Create cube geometry */
11140510c589SMatthew G. Knepley   {
11150510c589SMatthew G. Knepley     Vec             coordinates;
11160510c589SMatthew G. Knepley     PetscSection    coordSection;
11170510c589SMatthew G. Knepley     PetscScalar    *coords;
11180510c589SMatthew G. Knepley     PetscInt        coordSize, v;
11190510c589SMatthew G. Knepley     const PetscReal dis = 1.0/PetscSqrtReal(2.0);
11200510c589SMatthew G. Knepley     const PetscReal ds2 = dis/2.0;
11210510c589SMatthew G. Knepley 
11220510c589SMatthew G. Knepley     /* Build coordinates */
11230510c589SMatthew G. Knepley     ierr = DMGetCoordinateSection(*dm, &coordSection);CHKERRQ(ierr);
11240510c589SMatthew G. Knepley     ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
11250510c589SMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(coordSection, 0, dim);CHKERRQ(ierr);
11260510c589SMatthew G. Knepley     ierr = PetscSectionSetChart(coordSection, numCells, numCells+numVertices);CHKERRQ(ierr);
11270510c589SMatthew G. Knepley     for (v = numCells; v < numCells+numVertices; ++v) {
11280510c589SMatthew G. Knepley       ierr = PetscSectionSetDof(coordSection, v, dim);CHKERRQ(ierr);
11290510c589SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(coordSection, v, 0, dim);CHKERRQ(ierr);
11300510c589SMatthew G. Knepley     }
11310510c589SMatthew G. Knepley     ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
11320510c589SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
11330510c589SMatthew G. Knepley     ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
11340510c589SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
11350510c589SMatthew G. Knepley     ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
11360510c589SMatthew G. Knepley     ierr = VecSetBlockSize(coordinates, dim);CHKERRQ(ierr);
11370510c589SMatthew G. Knepley     ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
11380510c589SMatthew G. Knepley     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
11390510c589SMatthew G. Knepley     coords[0*dim+0] = -ds2; coords[0*dim+1] = -ds2; coords[0*dim+2] = 0.0;
11400510c589SMatthew G. Knepley     coords[1*dim+0] =  ds2; coords[1*dim+1] = -ds2; coords[1*dim+2] = 0.0;
11410510c589SMatthew G. Knepley     coords[2*dim+0] =  ds2; coords[2*dim+1] =  ds2; coords[2*dim+2] = 0.0;
11420510c589SMatthew G. Knepley     coords[3*dim+0] = -ds2; coords[3*dim+1] =  ds2; coords[3*dim+2] = 0.0;
11430510c589SMatthew G. Knepley     coords[4*dim+0] = -ds2; coords[4*dim+1] = -ds2; coords[4*dim+2] = 1.0;
11440510c589SMatthew G. Knepley     coords[5*dim+0] = -ds2; coords[5*dim+1] =  ds2; coords[5*dim+2] = 1.0;
11450510c589SMatthew G. Knepley     coords[6*dim+0] =  ds2; coords[6*dim+1] =  ds2; coords[6*dim+2] = 1.0;
11460510c589SMatthew G. Knepley     coords[7*dim+0] =  ds2; coords[7*dim+1] = -ds2; coords[7*dim+2] = 1.0;
11470510c589SMatthew G. Knepley     coords[ 8*dim+0] =  dis; coords[ 8*dim+1] = -dis; coords[ 8*dim+2] = 0.0;
11480510c589SMatthew G. Knepley     coords[ 9*dim+0] =  dis; coords[ 9*dim+1] =  dis; coords[ 9*dim+2] = 0.0;
11490510c589SMatthew G. Knepley     coords[10*dim+0] =  dis; coords[10*dim+1] = -dis; coords[10*dim+2] = 1.0;
11500510c589SMatthew G. Knepley     coords[11*dim+0] =  dis; coords[11*dim+1] =  dis; coords[11*dim+2] = 1.0;
11510510c589SMatthew G. Knepley     coords[12*dim+0] = -dis; coords[12*dim+1] =  dis; coords[12*dim+2] = 0.0;
11520510c589SMatthew G. Knepley     coords[13*dim+0] = -dis; coords[13*dim+1] =  dis; coords[13*dim+2] = 1.0;
11530510c589SMatthew G. Knepley     coords[14*dim+0] = -dis; coords[14*dim+1] = -dis; coords[14*dim+2] = 0.0;
11540510c589SMatthew G. Knepley     coords[15*dim+0] = -dis; coords[15*dim+1] = -dis; coords[15*dim+2] = 1.0;
11550510c589SMatthew G. Knepley     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
11560510c589SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*dm, coordinates);CHKERRQ(ierr);
11570510c589SMatthew G. Knepley     ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
11580510c589SMatthew G. Knepley   }
1159006a8963SMatthew G. Knepley   /* Create periodicity */
1160006a8963SMatthew G. Knepley   if (periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
1161006a8963SMatthew G. Knepley     PetscReal      L[3];
1162006a8963SMatthew G. Knepley     PetscReal      maxCell[3];
1163006a8963SMatthew G. Knepley     DMBoundaryType bdType[3];
1164006a8963SMatthew G. Knepley     PetscReal      lower[3] = {0.0, 0.0, 0.0};
1165006a8963SMatthew G. Knepley     PetscReal      upper[3] = {1.0, 1.0, 2.0};
1166006a8963SMatthew G. Knepley     PetscInt       i, numZCells = PetscPowInt(2, numRefine);
1167006a8963SMatthew G. Knepley 
1168006a8963SMatthew G. Knepley     bdType[0] = DM_BOUNDARY_NONE;
1169006a8963SMatthew G. Knepley     bdType[1] = DM_BOUNDARY_NONE;
1170006a8963SMatthew G. Knepley     bdType[2] = periodicZ;
1171006a8963SMatthew G. Knepley     for (i = 0; i < dim; i++) {
1172006a8963SMatthew G. Knepley       L[i]       = upper[i] - lower[i];
1173006a8963SMatthew G. Knepley       maxCell[i] = 1.1 * (L[i] / numZCells);
1174006a8963SMatthew G. Knepley     }
1175006a8963SMatthew G. Knepley     ierr = DMSetPeriodicity(*dm, maxCell, L, bdType);CHKERRQ(ierr);
1176006a8963SMatthew G. Knepley   }
1177dbc1dc17SMatthew G. Knepley   /* Refine topology */
1178dbc1dc17SMatthew G. Knepley   for (r = 0; r < numRefine; ++r) {
1179dbc1dc17SMatthew G. Knepley     DM rdm = NULL;
1180dbc1dc17SMatthew G. Knepley 
1181dbc1dc17SMatthew G. Knepley     ierr = DMRefine(*dm, comm, &rdm);CHKERRQ(ierr);
1182dbc1dc17SMatthew G. Knepley     ierr = DMDestroy(dm);CHKERRQ(ierr);
1183dbc1dc17SMatthew G. Knepley     *dm  = rdm;
1184dbc1dc17SMatthew G. Knepley   }
1185dbc1dc17SMatthew G. Knepley   /* Remap geometry to cylinder
1186dbc1dc17SMatthew G. Knepley        Interior square: Linear interpolation is correct
1187dbc1dc17SMatthew G. Knepley        The other cells all have vertices on rays from the origin. We want to uniformly expand the spacing
1188dbc1dc17SMatthew G. Knepley        such that the last vertex is on the unit circle. So the closest and farthest vertices are at distance
1189dbc1dc17SMatthew G. Knepley 
1190dbc1dc17SMatthew G. Knepley          phi     = arctan(y/x)
1191dbc1dc17SMatthew G. Knepley          d_close = sqrt(1/8 + 1/4 sin^2(phi))
1192dbc1dc17SMatthew G. Knepley          d_far   = sqrt(1/2 + sin^2(phi))
1193dbc1dc17SMatthew G. Knepley 
1194dbc1dc17SMatthew G. Knepley        so we remap them using
1195dbc1dc17SMatthew G. Knepley 
1196dbc1dc17SMatthew G. Knepley          x_new = x_close + (x - x_close) (1 - d_close) / (d_far - d_close)
1197dbc1dc17SMatthew G. Knepley          y_new = y_close + (y - y_close) (1 - d_close) / (d_far - d_close)
1198dbc1dc17SMatthew G. Knepley 
1199dbc1dc17SMatthew G. Knepley        If pi/4 < phi < 3pi/4 or -3pi/4 < phi < -pi/4, then we switch x and y.
1200dbc1dc17SMatthew G. Knepley   */
1201dbc1dc17SMatthew G. Knepley   {
1202dbc1dc17SMatthew G. Knepley     Vec           coordinates;
1203dbc1dc17SMatthew G. Knepley     PetscSection  coordSection;
1204dbc1dc17SMatthew G. Knepley     PetscScalar  *coords;
1205dbc1dc17SMatthew G. Knepley     PetscInt      vStart, vEnd, v;
1206dbc1dc17SMatthew G. Knepley     const PetscReal dis = 1.0/PetscSqrtReal(2.0);
1207dbc1dc17SMatthew G. Knepley     const PetscReal ds2 = 0.5*dis;
1208dbc1dc17SMatthew G. Knepley 
1209dbc1dc17SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(*dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1210dbc1dc17SMatthew G. Knepley     ierr = DMGetCoordinateSection(*dm, &coordSection);CHKERRQ(ierr);
1211dbc1dc17SMatthew G. Knepley     ierr = DMGetCoordinatesLocal(*dm, &coordinates);CHKERRQ(ierr);
1212dbc1dc17SMatthew G. Knepley     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
1213dbc1dc17SMatthew G. Knepley     for (v = vStart; v < vEnd; ++v) {
121471752167SMatthew G. Knepley       PetscReal phi, sinp, cosp, dc, df, x, y, xc, yc;
1215dbc1dc17SMatthew G. Knepley       PetscInt  off;
1216dbc1dc17SMatthew G. Knepley 
1217dbc1dc17SMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
121871752167SMatthew G. Knepley       if ((PetscAbsScalar(coords[off+0]) <= ds2) && (PetscAbsScalar(coords[off+1]) <= ds2)) continue;
121971752167SMatthew G. Knepley       x    = PetscRealPart(coords[off]);
122071752167SMatthew G. Knepley       y    = PetscRealPart(coords[off+1]);
122171752167SMatthew G. Knepley       phi  = PetscAtan2Real(y, x);
1222dbc1dc17SMatthew G. Knepley       sinp = PetscSinReal(phi);
1223dbc1dc17SMatthew G. Knepley       cosp = PetscCosReal(phi);
1224dbc1dc17SMatthew G. Knepley       if ((PetscAbsReal(phi) > PETSC_PI/4.0) && (PetscAbsReal(phi) < 3.0*PETSC_PI/4.0)) {
122571752167SMatthew G. Knepley         dc = PetscAbsReal(ds2/sinp);
122671752167SMatthew G. Knepley         df = PetscAbsReal(dis/sinp);
122771752167SMatthew G. Knepley         xc = ds2*x/PetscAbsScalar(y);
122871752167SMatthew G. Knepley         yc = ds2*PetscSignReal(y);
1229dbc1dc17SMatthew G. Knepley       } else {
123071752167SMatthew G. Knepley         dc = PetscAbsReal(ds2/cosp);
123171752167SMatthew G. Knepley         df = PetscAbsReal(dis/cosp);
123271752167SMatthew G. Knepley         xc = ds2*PetscSignReal(x);
123371752167SMatthew G. Knepley         yc = ds2*y/PetscAbsScalar(x);
1234dbc1dc17SMatthew G. Knepley       }
1235dbc1dc17SMatthew G. Knepley       coords[off+0] = xc + (coords[off+0] - xc)*(1.0 - dc)/(df - dc);
1236dbc1dc17SMatthew G. Knepley       coords[off+1] = yc + (coords[off+1] - yc)*(1.0 - dc)/(df - dc);
1237dbc1dc17SMatthew G. Knepley     }
1238dbc1dc17SMatthew G. Knepley     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
12390510c589SMatthew G. Knepley     if (periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
1240006a8963SMatthew G. Knepley       ierr = DMLocalizeCoordinates(*dm);CHKERRQ(ierr);
12410510c589SMatthew G. Knepley     }
12420510c589SMatthew G. Knepley   }
12430510c589SMatthew G. Knepley   PetscFunctionReturn(0);
12440510c589SMatthew G. Knepley }
12450510c589SMatthew G. Knepley 
1246552f7358SJed Brown /* External function declarations here */
1247552f7358SJed Brown extern PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling);
12486dbf9973SLawrence Mitchell extern PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat);
1249fd59a867SMatthew G. Knepley extern PetscErrorCode DMCreateDefaultSection_Plex(DM dm);
125066ad2231SToby Isaac extern PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm);
1251b412c318SBarry Smith extern PetscErrorCode DMCreateMatrix_Plex(DM dm,  Mat *J);
1252552f7358SJed Brown extern PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm);
1253d9879d6bSMatthew G. Knepley PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm);
1254552f7358SJed Brown extern PetscErrorCode DMSetUp_Plex(DM dm);
1255552f7358SJed Brown extern PetscErrorCode DMDestroy_Plex(DM dm);
1256552f7358SJed Brown extern PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer);
12572c40f234SMatthew G. Knepley extern PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer);
1258552f7358SJed Brown extern PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm);
1259552f7358SJed Brown 
12600a6ba040SMatthew G. Knepley /* Replace dm with the contents of dmNew
12610a6ba040SMatthew G. Knepley    - Share the DM_Plex structure
12620a6ba040SMatthew G. Knepley    - Share the coordinates
1263d7973521SMatthew G. Knepley    - Share the SF
12640a6ba040SMatthew G. Knepley */
12650a6ba040SMatthew G. Knepley static PetscErrorCode DMPlexReplace_Static(DM dm, DM dmNew)
12660a6ba040SMatthew G. Knepley {
1267d7973521SMatthew G. Knepley   PetscSF          sf;
1268acdc6f61SToby Isaac   DM               coordDM, coarseDM;
12690a6ba040SMatthew G. Knepley   Vec              coords;
127055fbe3e3SMatthew G. Knepley   const PetscReal *maxCell, *L;
12715dc8c3f7SMatthew G. Knepley   const DMBoundaryType *bd;
12720a6ba040SMatthew G. Knepley   PetscErrorCode   ierr;
12730a6ba040SMatthew G. Knepley 
12740a6ba040SMatthew G. Knepley   PetscFunctionBegin;
1275d7973521SMatthew G. Knepley   ierr = DMGetPointSF(dmNew, &sf);CHKERRQ(ierr);
1276d7973521SMatthew G. Knepley   ierr = DMSetPointSF(dm, sf);CHKERRQ(ierr);
127755fbe3e3SMatthew G. Knepley   ierr = DMGetCoordinateDM(dmNew, &coordDM);CHKERRQ(ierr);
12780a6ba040SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dmNew, &coords);CHKERRQ(ierr);
127955fbe3e3SMatthew G. Knepley   ierr = DMSetCoordinateDM(dm, coordDM);CHKERRQ(ierr);
12800a6ba040SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, coords);CHKERRQ(ierr);
12815dc8c3f7SMatthew G. Knepley   ierr = DMGetPeriodicity(dm, &maxCell, &L, &bd);CHKERRQ(ierr);
12825dc8c3f7SMatthew G. Knepley   if (L) {ierr = DMSetPeriodicity(dmNew, maxCell, L, bd);CHKERRQ(ierr);}
12830a6ba040SMatthew G. Knepley   ierr = DMDestroy_Plex(dm);CHKERRQ(ierr);
12840a6ba040SMatthew G. Knepley   dm->data = dmNew->data;
12850a6ba040SMatthew G. Knepley   ((DM_Plex *) dmNew->data)->refct++;
1286c58f1c22SToby Isaac   dmNew->labels->refct++;
1287c58f1c22SToby Isaac   if (!--(dm->labels->refct)) {
1288c58f1c22SToby Isaac     DMLabelLink next = dm->labels->next;
1289c58f1c22SToby Isaac 
1290c58f1c22SToby Isaac     /* destroy the labels */
1291c58f1c22SToby Isaac     while (next) {
1292c58f1c22SToby Isaac       DMLabelLink tmp = next->next;
1293c58f1c22SToby Isaac 
1294c58f1c22SToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
1295c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
1296c58f1c22SToby Isaac       next = tmp;
1297c58f1c22SToby Isaac     }
1298c58f1c22SToby Isaac     ierr = PetscFree(dm->labels);CHKERRQ(ierr);
1299c58f1c22SToby Isaac   }
1300c58f1c22SToby Isaac   dm->labels = dmNew->labels;
1301c58f1c22SToby Isaac   dm->depthLabel = dmNew->depthLabel;
1302acdc6f61SToby Isaac   ierr = DMGetCoarseDM(dmNew,&coarseDM);CHKERRQ(ierr);
1303acdc6f61SToby Isaac   ierr = DMSetCoarseDM(dm,coarseDM);CHKERRQ(ierr);
13040a6ba040SMatthew G. Knepley   PetscFunctionReturn(0);
13050a6ba040SMatthew G. Knepley }
13060a6ba040SMatthew G. Knepley 
13070a6ba040SMatthew G. Knepley /* Swap dm with the contents of dmNew
13080a6ba040SMatthew G. Knepley    - Swap the DM_Plex structure
13090a6ba040SMatthew G. Knepley    - Swap the coordinates
131079a015ccSMatthew G. Knepley    - Swap the point PetscSF
13110a6ba040SMatthew G. Knepley */
13120a6ba040SMatthew G. Knepley static PetscErrorCode DMPlexSwap_Static(DM dmA, DM dmB)
13130a6ba040SMatthew G. Knepley {
13140a6ba040SMatthew G. Knepley   DM              coordDMA, coordDMB;
13150a6ba040SMatthew G. Knepley   Vec             coordsA,  coordsB;
131679a015ccSMatthew G. Knepley   PetscSF         sfA,      sfB;
13170a6ba040SMatthew G. Knepley   void            *tmp;
1318c58f1c22SToby Isaac   DMLabelLinkList listTmp;
1319c58f1c22SToby Isaac   DMLabel         depthTmp;
13200a6ba040SMatthew G. Knepley   PetscErrorCode  ierr;
13210a6ba040SMatthew G. Knepley 
13220a6ba040SMatthew G. Knepley   PetscFunctionBegin;
132379a015ccSMatthew G. Knepley   ierr = DMGetPointSF(dmA, &sfA);CHKERRQ(ierr);
132479a015ccSMatthew G. Knepley   ierr = DMGetPointSF(dmB, &sfB);CHKERRQ(ierr);
132579a015ccSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) sfA);CHKERRQ(ierr);
132679a015ccSMatthew G. Knepley   ierr = DMSetPointSF(dmA, sfB);CHKERRQ(ierr);
132779a015ccSMatthew G. Knepley   ierr = DMSetPointSF(dmB, sfA);CHKERRQ(ierr);
132879a015ccSMatthew G. Knepley   ierr = PetscObjectDereference((PetscObject) sfA);CHKERRQ(ierr);
132979a015ccSMatthew G. Knepley 
13300a6ba040SMatthew G. Knepley   ierr = DMGetCoordinateDM(dmA, &coordDMA);CHKERRQ(ierr);
13310a6ba040SMatthew G. Knepley   ierr = DMGetCoordinateDM(dmB, &coordDMB);CHKERRQ(ierr);
13320a6ba040SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) coordDMA);CHKERRQ(ierr);
13330a6ba040SMatthew G. Knepley   ierr = DMSetCoordinateDM(dmA, coordDMB);CHKERRQ(ierr);
13340a6ba040SMatthew G. Knepley   ierr = DMSetCoordinateDM(dmB, coordDMA);CHKERRQ(ierr);
13350a6ba040SMatthew G. Knepley   ierr = PetscObjectDereference((PetscObject) coordDMA);CHKERRQ(ierr);
13360a6ba040SMatthew G. Knepley 
13370a6ba040SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dmA, &coordsA);CHKERRQ(ierr);
13380a6ba040SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dmB, &coordsB);CHKERRQ(ierr);
13390a6ba040SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) coordsA);CHKERRQ(ierr);
13400a6ba040SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmA, coordsB);CHKERRQ(ierr);
13410a6ba040SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmB, coordsA);CHKERRQ(ierr);
13420a6ba040SMatthew G. Knepley   ierr = PetscObjectDereference((PetscObject) coordsA);CHKERRQ(ierr);
1343acdc6f61SToby Isaac 
13440a6ba040SMatthew G. Knepley   tmp       = dmA->data;
13450a6ba040SMatthew G. Knepley   dmA->data = dmB->data;
13460a6ba040SMatthew G. Knepley   dmB->data = tmp;
1347c58f1c22SToby Isaac   listTmp   = dmA->labels;
1348c58f1c22SToby Isaac   dmA->labels = dmB->labels;
1349c58f1c22SToby Isaac   dmB->labels = listTmp;
1350c58f1c22SToby Isaac   depthTmp  = dmA->depthLabel;
1351c58f1c22SToby Isaac   dmA->depthLabel = dmB->depthLabel;
1352c58f1c22SToby Isaac   dmB->depthLabel = depthTmp;
13530a6ba040SMatthew G. Knepley   PetscFunctionReturn(0);
13540a6ba040SMatthew G. Knepley }
13550a6ba040SMatthew G. Knepley 
13564416b707SBarry Smith PetscErrorCode DMSetFromOptions_NonRefinement_Plex(PetscOptionItems *PetscOptionsObject,DM dm)
13570a6ba040SMatthew G. Knepley {
13580a6ba040SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
13590a6ba040SMatthew G. Knepley   PetscErrorCode ierr;
13600a6ba040SMatthew G. Knepley 
13610a6ba040SMatthew G. Knepley   PetscFunctionBegin;
13620a6ba040SMatthew G. Knepley   /* Handle viewing */
13630a6ba040SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMView", PETSC_FALSE, &mesh->printSetValues, NULL);CHKERRQ(ierr);
13640a6ba040SMatthew G. Knepley   ierr = PetscOptionsInt("-dm_plex_print_fem", "Debug output level all fem computations", "DMView", 0, &mesh->printFEM, NULL);CHKERRQ(ierr);
136594ae4db5SBarry Smith   ierr = PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMView", mesh->printTol, &mesh->printTol, NULL);CHKERRQ(ierr);
1366953fc75cSMatthew G. Knepley   /* Point Location */
1367953fc75cSMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_hash_location", "Use grid hashing for point location", "DMView", PETSC_FALSE, &mesh->useHashLocation, NULL);CHKERRQ(ierr);
13682e62ab5aSMatthew G. Knepley   /* Generation and remeshing */
13692e62ab5aSMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_remesh_bd", "Allow changes to the boundary on remeshing", "DMView", PETSC_FALSE, &mesh->remeshBd, NULL);CHKERRQ(ierr);
1370b29cfa1cSToby Isaac   /* Projection behavior */
1371b29cfa1cSToby Isaac   ierr = PetscOptionsInt("-dm_plex_max_projection_height", "Maxmimum mesh point height used to project locally", "DMPlexSetMaxProjectionHeight", 0, &mesh->maxProjectionHeight, NULL);CHKERRQ(ierr);
137264141f95SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_regular_refinement", "Use special nested projection algorithm for regular refinement", "DMPlexSetRegularRefinement", mesh->regularRefinement, &mesh->regularRefinement, NULL);CHKERRQ(ierr);
137368d4fef7SMatthew G. Knepley   PetscFunctionReturn(0);
137468d4fef7SMatthew G. Knepley }
137568d4fef7SMatthew G. Knepley 
137646fa42a0SMatthew G. Knepley static PetscErrorCode DMSetFromOptions_Plex(PetscOptionItems *PetscOptionsObject,DM dm)
137768d4fef7SMatthew G. Knepley {
1378b653a561SMatthew G. Knepley   PetscInt       refine = 0, coarsen = 0, r;
1379b653a561SMatthew G. Knepley   PetscBool      isHierarchy;
138068d4fef7SMatthew G. Knepley   PetscErrorCode ierr;
138168d4fef7SMatthew G. Knepley 
138268d4fef7SMatthew G. Knepley   PetscFunctionBegin;
138368d4fef7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13841a1499c8SBarry Smith   ierr = PetscOptionsHead(PetscOptionsObject,"DMPlex Options");CHKERRQ(ierr);
138568d4fef7SMatthew G. Knepley   /* Handle DMPlex refinement */
138668d4fef7SMatthew G. Knepley   ierr = PetscOptionsInt("-dm_refine", "The number of uniform refinements", "DMCreate", refine, &refine, NULL);CHKERRQ(ierr);
138768d4fef7SMatthew G. Knepley   ierr = PetscOptionsInt("-dm_refine_hierarchy", "The number of uniform refinements", "DMCreate", refine, &refine, &isHierarchy);CHKERRQ(ierr);
1388b6a0289aSMatthew G. Knepley   if (refine) {ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE);CHKERRQ(ierr);}
138968d4fef7SMatthew G. Knepley   if (refine && isHierarchy) {
1390acdc6f61SToby Isaac     DM *dms, coarseDM;
139168d4fef7SMatthew G. Knepley 
1392acdc6f61SToby Isaac     ierr = DMGetCoarseDM(dm, &coarseDM);CHKERRQ(ierr);
1393acdc6f61SToby Isaac     ierr = PetscObjectReference((PetscObject)coarseDM);CHKERRQ(ierr);
139468d4fef7SMatthew G. Knepley     ierr = PetscMalloc1(refine,&dms);CHKERRQ(ierr);
139568d4fef7SMatthew G. Knepley     ierr = DMRefineHierarchy(dm, refine, dms);CHKERRQ(ierr);
139668d4fef7SMatthew G. Knepley     /* Total hack since we do not pass in a pointer */
139768d4fef7SMatthew G. Knepley     ierr = DMPlexSwap_Static(dm, dms[refine-1]);CHKERRQ(ierr);
139868d4fef7SMatthew G. Knepley     if (refine == 1) {
1399a8fb8f29SToby Isaac       ierr = DMSetCoarseDM(dm, dms[0]);CHKERRQ(ierr);
14000aef6b92SMatthew G. Knepley       ierr = DMPlexSetRegularRefinement(dm, PETSC_TRUE);CHKERRQ(ierr);
140168d4fef7SMatthew G. Knepley     } else {
1402a8fb8f29SToby Isaac       ierr = DMSetCoarseDM(dm, dms[refine-2]);CHKERRQ(ierr);
14030aef6b92SMatthew G. Knepley       ierr = DMPlexSetRegularRefinement(dm, PETSC_TRUE);CHKERRQ(ierr);
1404a8fb8f29SToby Isaac       ierr = DMSetCoarseDM(dms[0], dms[refine-1]);CHKERRQ(ierr);
14050aef6b92SMatthew G. Knepley       ierr = DMPlexSetRegularRefinement(dms[0], PETSC_TRUE);CHKERRQ(ierr);
140668d4fef7SMatthew G. Knepley     }
1407acdc6f61SToby Isaac     ierr = DMSetCoarseDM(dms[refine-1], coarseDM);CHKERRQ(ierr);
1408acdc6f61SToby Isaac     ierr = PetscObjectDereference((PetscObject)coarseDM);CHKERRQ(ierr);
140968d4fef7SMatthew G. Knepley     /* Free DMs */
141068d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
1411547f7119SMatthew G. Knepley       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]);CHKERRQ(ierr);
141268d4fef7SMatthew G. Knepley       ierr = DMDestroy(&dms[r]);CHKERRQ(ierr);
141368d4fef7SMatthew G. Knepley     }
141468d4fef7SMatthew G. Knepley     ierr = PetscFree(dms);CHKERRQ(ierr);
141568d4fef7SMatthew G. Knepley   } else {
141668d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
141768d4fef7SMatthew G. Knepley       DM refinedMesh;
141868d4fef7SMatthew G. Knepley 
14191a1499c8SBarry Smith       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
142068d4fef7SMatthew G. Knepley       ierr = DMRefine(dm, PetscObjectComm((PetscObject) dm), &refinedMesh);CHKERRQ(ierr);
142168d4fef7SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
142268d4fef7SMatthew G. Knepley       ierr = DMPlexReplace_Static(dm, refinedMesh);CHKERRQ(ierr);
1423547f7119SMatthew G. Knepley       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
142468d4fef7SMatthew G. Knepley       ierr = DMDestroy(&refinedMesh);CHKERRQ(ierr);
142568d4fef7SMatthew G. Knepley     }
142668d4fef7SMatthew G. Knepley   }
14273cf6fe12SMatthew G. Knepley   /* Handle DMPlex coarsening */
1428b653a561SMatthew G. Knepley   ierr = PetscOptionsInt("-dm_coarsen", "Coarsen the mesh", "DMCreate", coarsen, &coarsen, NULL);CHKERRQ(ierr);
1429b653a561SMatthew G. Knepley   ierr = PetscOptionsInt("-dm_coarsen_hierarchy", "The number of coarsenings", "DMCreate", coarsen, &coarsen, &isHierarchy);CHKERRQ(ierr);
1430b653a561SMatthew G. Knepley   if (coarsen && isHierarchy) {
1431b653a561SMatthew G. Knepley     DM *dms;
1432b653a561SMatthew G. Knepley 
1433b653a561SMatthew G. Knepley     ierr = PetscMalloc1(coarsen, &dms);CHKERRQ(ierr);
1434b653a561SMatthew G. Knepley     ierr = DMCoarsenHierarchy(dm, coarsen, dms);CHKERRQ(ierr);
1435b653a561SMatthew G. Knepley     /* Free DMs */
1436b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
1437547f7119SMatthew G. Knepley       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]);CHKERRQ(ierr);
1438b653a561SMatthew G. Knepley       ierr = DMDestroy(&dms[r]);CHKERRQ(ierr);
1439b653a561SMatthew G. Knepley     }
1440b653a561SMatthew G. Knepley     ierr = PetscFree(dms);CHKERRQ(ierr);
1441b653a561SMatthew G. Knepley   } else {
1442b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
14433cf6fe12SMatthew G. Knepley       DM coarseMesh;
14443cf6fe12SMatthew G. Knepley 
14453cf6fe12SMatthew G. Knepley       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
14463cf6fe12SMatthew G. Knepley       ierr = DMCoarsen(dm, PetscObjectComm((PetscObject) dm), &coarseMesh);CHKERRQ(ierr);
14473cf6fe12SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
14483cf6fe12SMatthew G. Knepley       ierr = DMPlexReplace_Static(dm, coarseMesh);CHKERRQ(ierr);
1449547f7119SMatthew G. Knepley       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
14503cf6fe12SMatthew G. Knepley       ierr = DMDestroy(&coarseMesh);CHKERRQ(ierr);
14513cf6fe12SMatthew G. Knepley     }
1452b653a561SMatthew G. Knepley   }
14533cf6fe12SMatthew G. Knepley   /* Handle */
14541a1499c8SBarry Smith   ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
14550a6ba040SMatthew G. Knepley   ierr = PetscOptionsTail();CHKERRQ(ierr);
14560a6ba040SMatthew G. Knepley   PetscFunctionReturn(0);
14570a6ba040SMatthew G. Knepley }
14580a6ba040SMatthew G. Knepley 
1459552f7358SJed Brown static PetscErrorCode DMCreateGlobalVector_Plex(DM dm,Vec *vec)
1460552f7358SJed Brown {
1461552f7358SJed Brown   PetscErrorCode ierr;
1462552f7358SJed Brown 
1463552f7358SJed Brown   PetscFunctionBegin;
1464552f7358SJed Brown   ierr = DMCreateGlobalVector_Section_Private(dm,vec);CHKERRQ(ierr);
1465552f7358SJed Brown   /* ierr = VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM);CHKERRQ(ierr); */
1466552f7358SJed Brown   ierr = VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex);CHKERRQ(ierr);
1467d930f514SMatthew G. Knepley   ierr = VecSetOperation(*vec, VECOP_VIEWNATIVE, (void (*)(void)) VecView_Plex_Native);CHKERRQ(ierr);
14682c40f234SMatthew G. Knepley   ierr = VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex);CHKERRQ(ierr);
1469d930f514SMatthew G. Knepley   ierr = VecSetOperation(*vec, VECOP_LOADNATIVE, (void (*)(void)) VecLoad_Plex_Native);CHKERRQ(ierr);
1470552f7358SJed Brown   PetscFunctionReturn(0);
1471552f7358SJed Brown }
1472552f7358SJed Brown 
1473552f7358SJed Brown static PetscErrorCode DMCreateLocalVector_Plex(DM dm,Vec *vec)
1474552f7358SJed Brown {
1475552f7358SJed Brown   PetscErrorCode ierr;
1476552f7358SJed Brown 
1477552f7358SJed Brown   PetscFunctionBegin;
1478552f7358SJed Brown   ierr = DMCreateLocalVector_Section_Private(dm,vec);CHKERRQ(ierr);
1479552f7358SJed Brown   ierr = VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex_Local);CHKERRQ(ierr);
14802c40f234SMatthew G. Knepley   ierr = VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex_Local);CHKERRQ(ierr);
1481552f7358SJed Brown   PetscFunctionReturn(0);
1482552f7358SJed Brown }
1483552f7358SJed Brown 
1484793f3fe5SMatthew G. Knepley static PetscErrorCode DMGetDimPoints_Plex(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
1485793f3fe5SMatthew G. Knepley {
1486793f3fe5SMatthew G. Knepley   PetscInt       depth, d;
1487793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
1488793f3fe5SMatthew G. Knepley 
1489793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
1490793f3fe5SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1491793f3fe5SMatthew G. Knepley   if (depth == 1) {
1492793f3fe5SMatthew G. Knepley     ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
1493793f3fe5SMatthew G. Knepley     if (dim == 0)      {ierr = DMPlexGetDepthStratum(dm, dim, pStart, pEnd);CHKERRQ(ierr);}
1494793f3fe5SMatthew G. Knepley     else if (dim == d) {ierr = DMPlexGetDepthStratum(dm, 1, pStart, pEnd);CHKERRQ(ierr);}
1495793f3fe5SMatthew G. Knepley     else               {*pStart = 0; *pEnd = 0;}
1496793f3fe5SMatthew G. Knepley   } else {
1497793f3fe5SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, dim, pStart, pEnd);CHKERRQ(ierr);
1498793f3fe5SMatthew G. Knepley   }
1499793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
1500793f3fe5SMatthew G. Knepley }
1501793f3fe5SMatthew G. Knepley 
15022f356facSMatthew G. Knepley static PetscErrorCode DMGetNeighors_Plex(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
1503502a2867SDave May {
1504502a2867SDave May   PetscSF        sf;
15052f356facSMatthew G. Knepley   PetscErrorCode ierr;
1506502a2867SDave May 
15072f356facSMatthew G. Knepley   PetscFunctionBegin;
1508502a2867SDave May   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
1509502a2867SDave May   ierr = PetscSFGetRanks(sf, nranks, ranks, NULL, NULL, NULL);CHKERRQ(ierr);
1510502a2867SDave May   PetscFunctionReturn(0);
1511502a2867SDave May }
1512502a2867SDave May 
151346fa42a0SMatthew G. Knepley static PetscErrorCode DMInitialize_Plex(DM dm)
1514552f7358SJed Brown {
1515713918a9SToby Isaac   PetscErrorCode ierr;
1516713918a9SToby Isaac 
1517552f7358SJed Brown   PetscFunctionBegin;
1518552f7358SJed Brown   dm->ops->view                            = DMView_Plex;
15192c40f234SMatthew G. Knepley   dm->ops->load                            = DMLoad_Plex;
1520552f7358SJed Brown   dm->ops->setfromoptions                  = DMSetFromOptions_Plex;
152138221697SMatthew G. Knepley   dm->ops->clone                           = DMClone_Plex;
1522552f7358SJed Brown   dm->ops->setup                           = DMSetUp_Plex;
1523fd59a867SMatthew G. Knepley   dm->ops->createdefaultsection            = DMCreateDefaultSection_Plex;
152466ad2231SToby Isaac   dm->ops->createdefaultconstraints        = DMCreateDefaultConstraints_Plex;
1525552f7358SJed Brown   dm->ops->createglobalvector              = DMCreateGlobalVector_Plex;
1526552f7358SJed Brown   dm->ops->createlocalvector               = DMCreateLocalVector_Plex;
1527184d77edSJed Brown   dm->ops->getlocaltoglobalmapping         = NULL;
15280298fd71SBarry Smith   dm->ops->createfieldis                   = NULL;
1529552f7358SJed Brown   dm->ops->createcoordinatedm              = DMCreateCoordinateDM_Plex;
15300a6ba040SMatthew G. Knepley   dm->ops->getcoloring                     = NULL;
1531552f7358SJed Brown   dm->ops->creatematrix                    = DMCreateMatrix_Plex;
1532bceba477SMatthew G. Knepley   dm->ops->createinterpolation             = DMCreateInterpolation_Plex;
1533bceba477SMatthew G. Knepley   dm->ops->getaggregates                   = NULL;
1534bceba477SMatthew G. Knepley   dm->ops->getinjection                    = DMCreateInjection_Plex;
1535552f7358SJed Brown   dm->ops->refine                          = DMRefine_Plex;
15360a6ba040SMatthew G. Knepley   dm->ops->coarsen                         = DMCoarsen_Plex;
15370a6ba040SMatthew G. Knepley   dm->ops->refinehierarchy                 = DMRefineHierarchy_Plex;
1538b653a561SMatthew G. Knepley   dm->ops->coarsenhierarchy                = DMCoarsenHierarchy_Plex;
15390298fd71SBarry Smith   dm->ops->globaltolocalbegin              = NULL;
15400298fd71SBarry Smith   dm->ops->globaltolocalend                = NULL;
15410298fd71SBarry Smith   dm->ops->localtoglobalbegin              = NULL;
15420298fd71SBarry Smith   dm->ops->localtoglobalend                = NULL;
1543552f7358SJed Brown   dm->ops->destroy                         = DMDestroy_Plex;
1544552f7358SJed Brown   dm->ops->createsubdm                     = DMCreateSubDM_Plex;
1545793f3fe5SMatthew G. Knepley   dm->ops->getdimpoints                    = DMGetDimPoints_Plex;
1546552f7358SJed Brown   dm->ops->locatepoints                    = DMLocatePoints_Plex;
15470709b2feSToby Isaac   dm->ops->projectfunctionlocal            = DMProjectFunctionLocal_Plex;
15480709b2feSToby Isaac   dm->ops->projectfunctionlabellocal       = DMProjectFunctionLabelLocal_Plex;
1549bfc4295aSToby Isaac   dm->ops->projectfieldlocal               = DMProjectFieldLocal_Plex;
15508c6c5593SMatthew G. Knepley   dm->ops->projectfieldlabellocal          = DMProjectFieldLabelLocal_Plex;
15510709b2feSToby Isaac   dm->ops->computel2diff                   = DMComputeL2Diff_Plex;
1552b698f381SToby Isaac   dm->ops->computel2gradientdiff           = DMComputeL2GradientDiff_Plex;
15532a16baeaSToby Isaac   dm->ops->computel2fielddiff              = DMComputeL2FieldDiff_Plex;
1554502a2867SDave May   dm->ops->getneighbors                    = DMGetNeighors_Plex;
1555713918a9SToby Isaac   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMAdaptLabel_C",DMAdaptLabel_Plex);CHKERRQ(ierr);
1556552f7358SJed Brown   PetscFunctionReturn(0);
1557552f7358SJed Brown }
1558552f7358SJed Brown 
155946fa42a0SMatthew G. Knepley PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm)
156063a16f15SMatthew G. Knepley {
156163a16f15SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex *) dm->data;
156263a16f15SMatthew G. Knepley   PetscErrorCode ierr;
156363a16f15SMatthew G. Knepley 
156463a16f15SMatthew G. Knepley   PetscFunctionBegin;
156563a16f15SMatthew G. Knepley   mesh->refct++;
156663a16f15SMatthew G. Knepley   (*newdm)->data = mesh;
156763a16f15SMatthew G. Knepley   ierr = PetscObjectChangeTypeName((PetscObject) *newdm, DMPLEX);CHKERRQ(ierr);
156863a16f15SMatthew G. Knepley   ierr = DMInitialize_Plex(*newdm);CHKERRQ(ierr);
156963a16f15SMatthew G. Knepley   PetscFunctionReturn(0);
157063a16f15SMatthew G. Knepley }
157163a16f15SMatthew G. Knepley 
15728818961aSMatthew G Knepley /*MC
15738818961aSMatthew G Knepley   DMPLEX = "plex" - A DM object that encapsulates an unstructured mesh, or CW Complex, which can be expressed using a Hasse Diagram.
15748818961aSMatthew G Knepley                     In the local representation, Vecs contain all unknowns in the interior and shared boundary. This is
15758818961aSMatthew G Knepley                     specified by a PetscSection object. Ownership in the global representation is determined by
15768818961aSMatthew G Knepley                     ownership of the underlying DMPlex points. This is specified by another PetscSection object.
15778818961aSMatthew G Knepley 
15788818961aSMatthew G Knepley   Level: intermediate
15798818961aSMatthew G Knepley 
15808818961aSMatthew G Knepley .seealso: DMType, DMPlexCreate(), DMCreate(), DMSetType()
15818818961aSMatthew G Knepley M*/
15828818961aSMatthew G Knepley 
15838cc058d9SJed Brown PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm)
1584552f7358SJed Brown {
1585552f7358SJed Brown   DM_Plex        *mesh;
1586770b213bSMatthew G Knepley   PetscInt       unit, d;
1587552f7358SJed Brown   PetscErrorCode ierr;
1588552f7358SJed Brown 
1589552f7358SJed Brown   PetscFunctionBegin;
1590552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1591b00a9115SJed Brown   ierr     = PetscNewLog(dm,&mesh);CHKERRQ(ierr);
1592c73cfb54SMatthew G. Knepley   dm->dim  = 0;
1593552f7358SJed Brown   dm->data = mesh;
1594552f7358SJed Brown 
1595552f7358SJed Brown   mesh->refct             = 1;
159682f516ccSBarry Smith   ierr                    = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection);CHKERRQ(ierr);
1597552f7358SJed Brown   mesh->maxConeSize       = 0;
15980298fd71SBarry Smith   mesh->cones             = NULL;
15990298fd71SBarry Smith   mesh->coneOrientations  = NULL;
160082f516ccSBarry Smith   ierr                    = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection);CHKERRQ(ierr);
1601552f7358SJed Brown   mesh->maxSupportSize    = 0;
16020298fd71SBarry Smith   mesh->supports          = NULL;
1603552f7358SJed Brown   mesh->refinementUniform = PETSC_TRUE;
1604552f7358SJed Brown   mesh->refinementLimit   = -1.0;
1605552f7358SJed Brown 
16060298fd71SBarry Smith   mesh->facesTmp = NULL;
1607552f7358SJed Brown 
1608d9deefdfSMatthew G. Knepley   mesh->tetgenOpts   = NULL;
1609d9deefdfSMatthew G. Knepley   mesh->triangleOpts = NULL;
161077623264SMatthew G. Knepley   ierr = PetscPartitionerCreate(PetscObjectComm((PetscObject)dm), &mesh->partitioner);CHKERRQ(ierr);
161177623264SMatthew G. Knepley   ierr = PetscPartitionerSetTypeFromOptions_Internal(mesh->partitioner);CHKERRQ(ierr);
16122e62ab5aSMatthew G. Knepley   mesh->remeshBd     = PETSC_FALSE;
1613d9deefdfSMatthew G. Knepley 
16140298fd71SBarry Smith   mesh->subpointMap = NULL;
1615552f7358SJed Brown 
16168865f1eaSKarl Rupp   for (unit = 0; unit < NUM_PETSC_UNITS; ++unit) mesh->scale[unit] = 1.0;
1617552f7358SJed Brown 
16180aef6b92SMatthew G. Knepley   mesh->regularRefinement   = PETSC_FALSE;
1619df0420ecSMatthew G. Knepley   mesh->depthState          = -1;
16200298fd71SBarry Smith   mesh->globalVertexNumbers = NULL;
16210298fd71SBarry Smith   mesh->globalCellNumbers   = NULL;
1622a68b90caSToby Isaac   mesh->anchorSection       = NULL;
1623a68b90caSToby Isaac   mesh->anchorIS            = NULL;
162441e6d900SToby Isaac   mesh->createanchors       = NULL;
1625fa73a4e1SToby Isaac   mesh->computeanchormatrix = NULL;
1626d961a43aSToby Isaac   mesh->parentSection       = NULL;
1627d961a43aSToby Isaac   mesh->parents             = NULL;
1628d961a43aSToby Isaac   mesh->childIDs            = NULL;
1629d961a43aSToby Isaac   mesh->childSection        = NULL;
1630d961a43aSToby Isaac   mesh->children            = NULL;
1631d6a7ad0dSToby Isaac   mesh->referenceTree       = NULL;
1632dcbd3bf7SToby Isaac   mesh->getchildsymmetry    = NULL;
16338865f1eaSKarl Rupp   for (d = 0; d < 8; ++d) mesh->hybridPointMax[d] = PETSC_DETERMINE;
1634552f7358SJed Brown   mesh->vtkCellHeight       = 0;
163570034214SMatthew G. Knepley   mesh->useCone             = PETSC_FALSE;
163670034214SMatthew G. Knepley   mesh->useClosure          = PETSC_TRUE;
1637e228b242SToby Isaac   mesh->useAnchors          = PETSC_FALSE;
1638552f7358SJed Brown 
1639b29cfa1cSToby Isaac   mesh->maxProjectionHeight = 0;
1640b29cfa1cSToby Isaac 
1641552f7358SJed Brown   mesh->printSetValues = PETSC_FALSE;
1642552f7358SJed Brown   mesh->printFEM       = 0;
16436113b454SMatthew G. Knepley   mesh->printTol       = 1.0e-10;
1644552f7358SJed Brown 
1645552f7358SJed Brown   ierr = DMInitialize_Plex(dm);CHKERRQ(ierr);
1646552f7358SJed Brown   PetscFunctionReturn(0);
1647552f7358SJed Brown }
1648552f7358SJed Brown 
1649552f7358SJed Brown /*@
1650552f7358SJed Brown   DMPlexCreate - Creates a DMPlex object, which encapsulates an unstructured mesh, or CW complex, which can be expressed using a Hasse Diagram.
1651552f7358SJed Brown 
1652552f7358SJed Brown   Collective on MPI_Comm
1653552f7358SJed Brown 
1654552f7358SJed Brown   Input Parameter:
1655552f7358SJed Brown . comm - The communicator for the DMPlex object
1656552f7358SJed Brown 
1657552f7358SJed Brown   Output Parameter:
1658552f7358SJed Brown . mesh  - The DMPlex object
1659552f7358SJed Brown 
1660552f7358SJed Brown   Level: beginner
1661552f7358SJed Brown 
1662552f7358SJed Brown .keywords: DMPlex, create
1663552f7358SJed Brown @*/
1664552f7358SJed Brown PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh)
1665552f7358SJed Brown {
1666552f7358SJed Brown   PetscErrorCode ierr;
1667552f7358SJed Brown 
1668552f7358SJed Brown   PetscFunctionBegin;
1669552f7358SJed Brown   PetscValidPointer(mesh,2);
1670552f7358SJed Brown   ierr = DMCreate(comm, mesh);CHKERRQ(ierr);
1671552f7358SJed Brown   ierr = DMSetType(*mesh, DMPLEX);CHKERRQ(ierr);
1672552f7358SJed Brown   PetscFunctionReturn(0);
1673552f7358SJed Brown }
1674552f7358SJed Brown 
1675a47d0d45SMatthew G. Knepley /*
1676a47d0d45SMatthew G. Knepley   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
1677a47d0d45SMatthew G. Knepley */
1678a47d0d45SMatthew G. Knepley static PetscErrorCode DMPlexBuildFromCellList_Parallel_Private(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const int cells[], PetscSF *sfVert)
1679a47d0d45SMatthew G. Knepley {
1680a47d0d45SMatthew G. Knepley   PetscSF         sfPoint;
1681a47d0d45SMatthew G. Knepley   PetscLayout     vLayout;
1682a47d0d45SMatthew G. Knepley   PetscHashI      vhash;
1683a47d0d45SMatthew G. Knepley   PetscSFNode    *remoteVerticesAdj, *vertexLocal, *vertexOwner, *remoteVertex;
1684a47d0d45SMatthew G. Knepley   const PetscInt *vrange;
1685a47d0d45SMatthew G. Knepley   PetscInt        numVerticesAdj, off, *verticesAdj, numVerticesGhost = 0, *localVertex, *cone, c, p, v, g;
1686a47d0d45SMatthew G. Knepley   PETSC_UNUSED PetscHashIIter ret, iter;
16879852e123SBarry Smith   PetscMPIInt     rank, size;
1688a47d0d45SMatthew G. Knepley   PetscErrorCode  ierr;
1689a47d0d45SMatthew G. Knepley 
1690a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
1691a47d0d45SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
16929852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size);CHKERRQ(ierr);
1693a47d0d45SMatthew G. Knepley   /* Partition vertices */
1694a47d0d45SMatthew G. Knepley   ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) dm), &vLayout);CHKERRQ(ierr);
1695a47d0d45SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(vLayout, numVertices);CHKERRQ(ierr);
1696a47d0d45SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(vLayout, 1);CHKERRQ(ierr);
1697a47d0d45SMatthew G. Knepley   ierr = PetscLayoutSetUp(vLayout);CHKERRQ(ierr);
1698a47d0d45SMatthew G. Knepley   ierr = PetscLayoutGetRanges(vLayout, &vrange);CHKERRQ(ierr);
1699a47d0d45SMatthew G. Knepley   /* Count vertices and map them to procs */
1700a47d0d45SMatthew G. Knepley   PetscHashICreate(vhash);
1701a47d0d45SMatthew G. Knepley   for (c = 0; c < numCells; ++c) {
1702a47d0d45SMatthew G. Knepley     for (p = 0; p < numCorners; ++p) {
1703a47d0d45SMatthew G. Knepley       PetscHashIPut(vhash, cells[c*numCorners+p], ret, iter);
1704a47d0d45SMatthew G. Knepley     }
1705a47d0d45SMatthew G. Knepley   }
1706a47d0d45SMatthew G. Knepley   PetscHashISize(vhash, numVerticesAdj);
1707a47d0d45SMatthew G. Knepley   ierr = PetscMalloc1(numVerticesAdj, &verticesAdj);CHKERRQ(ierr);
1708324a3438SMichael Lange   off = 0; ierr = PetscHashIGetKeys(vhash, &off, verticesAdj);CHKERRQ(ierr);
1709a47d0d45SMatthew G. Knepley   if (off != numVerticesAdj) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid number of local vertices %D should be %D", off, numVerticesAdj);
17108cd1fd5cSMatthew G. Knepley   ierr = PetscSortInt(numVerticesAdj, verticesAdj);CHKERRQ(ierr);
1711a47d0d45SMatthew G. Knepley   ierr = PetscMalloc1(numVerticesAdj, &remoteVerticesAdj);CHKERRQ(ierr);
1712a47d0d45SMatthew G. Knepley   for (v = 0; v < numVerticesAdj; ++v) {
1713a47d0d45SMatthew G. Knepley     const PetscInt gv = verticesAdj[v];
1714a47d0d45SMatthew G. Knepley     PetscInt       vrank;
1715a47d0d45SMatthew G. Knepley 
17169852e123SBarry Smith     ierr = PetscFindInt(gv, size+1, vrange, &vrank);CHKERRQ(ierr);
1717a47d0d45SMatthew G. Knepley     vrank = vrank < 0 ? -(vrank+2) : vrank;
1718a47d0d45SMatthew G. Knepley     remoteVerticesAdj[v].index = gv - vrange[vrank];
1719a47d0d45SMatthew G. Knepley     remoteVerticesAdj[v].rank  = vrank;
1720a47d0d45SMatthew G. Knepley   }
1721a47d0d45SMatthew G. Knepley   /* Create cones */
1722a47d0d45SMatthew G. Knepley   ierr = DMPlexSetChart(dm, 0, numCells+numVerticesAdj);CHKERRQ(ierr);
1723a47d0d45SMatthew G. Knepley   for (c = 0; c < numCells; ++c) {ierr = DMPlexSetConeSize(dm, c, numCorners);CHKERRQ(ierr);}
1724a47d0d45SMatthew G. Knepley   ierr = DMSetUp(dm);CHKERRQ(ierr);
1725a47d0d45SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numCorners, PETSC_INT, &cone);CHKERRQ(ierr);
1726a47d0d45SMatthew G. Knepley   for (c = 0; c < numCells; ++c) {
1727a47d0d45SMatthew G. Knepley     for (p = 0; p < numCorners; ++p) {
1728a47d0d45SMatthew G. Knepley       const PetscInt gv = cells[c*numCorners+p];
1729a47d0d45SMatthew G. Knepley       PetscInt       lv;
1730a47d0d45SMatthew G. Knepley 
1731a47d0d45SMatthew G. Knepley       ierr = PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv);CHKERRQ(ierr);
1732a47d0d45SMatthew G. Knepley       if (lv < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %D in local connectivity", gv);
1733a47d0d45SMatthew G. Knepley       cone[p] = lv+numCells;
1734a47d0d45SMatthew G. Knepley     }
1735a47d0d45SMatthew G. Knepley     ierr = DMPlexSetCone(dm, c, cone);CHKERRQ(ierr);
1736a47d0d45SMatthew G. Knepley   }
1737a47d0d45SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, numCorners, PETSC_INT, &cone);CHKERRQ(ierr);
1738a47d0d45SMatthew G. Knepley   /* Create SF for vertices */
1739a47d0d45SMatthew G. Knepley   ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm), sfVert);CHKERRQ(ierr);
1740a47d0d45SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *sfVert, "Vertex Ownership SF");CHKERRQ(ierr);
1741a47d0d45SMatthew G. Knepley   ierr = PetscSFSetFromOptions(*sfVert);CHKERRQ(ierr);
1742a47d0d45SMatthew G. Knepley   ierr = PetscSFSetGraph(*sfVert, numVertices, numVerticesAdj, NULL, PETSC_OWN_POINTER, remoteVerticesAdj, PETSC_OWN_POINTER);CHKERRQ(ierr);
1743a47d0d45SMatthew G. Knepley   ierr = PetscFree(verticesAdj);CHKERRQ(ierr);
1744a47d0d45SMatthew G. Knepley   /* Build pointSF */
1745a47d0d45SMatthew G. Knepley   ierr = PetscMalloc2(numVerticesAdj, &vertexLocal, numVertices, &vertexOwner);CHKERRQ(ierr);
1746a47d0d45SMatthew G. Knepley   for (v = 0; v < numVerticesAdj; ++v) {vertexLocal[v].index = v+numCells; vertexLocal[v].rank = rank;}
1747a47d0d45SMatthew G. Knepley   for (v = 0; v < numVertices;    ++v) {vertexOwner[v].index = -1;         vertexOwner[v].rank = -1;}
1748a47d0d45SMatthew G. Knepley   ierr = PetscSFReduceBegin(*sfVert, MPI_2INT, vertexLocal, vertexOwner, MPI_MAXLOC);CHKERRQ(ierr);
1749a47d0d45SMatthew G. Knepley   ierr = PetscSFReduceEnd(*sfVert, MPI_2INT, vertexLocal, vertexOwner, MPI_MAXLOC);CHKERRQ(ierr);
1750a47d0d45SMatthew G. Knepley   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);
1751a47d0d45SMatthew G. Knepley   ierr = PetscSFBcastBegin(*sfVert, MPI_2INT, vertexOwner, vertexLocal);CHKERRQ(ierr);
1752a47d0d45SMatthew G. Knepley   ierr = PetscSFBcastEnd(*sfVert, MPI_2INT, vertexOwner, vertexLocal);CHKERRQ(ierr);
1753a47d0d45SMatthew G. Knepley   for (v = 0; v < numVerticesAdj; ++v) if (vertexLocal[v].rank != rank) ++numVerticesGhost;
1754a47d0d45SMatthew G. Knepley   ierr = PetscMalloc1(numVerticesGhost, &localVertex);CHKERRQ(ierr);
1755a47d0d45SMatthew G. Knepley   ierr = PetscMalloc1(numVerticesGhost, &remoteVertex);CHKERRQ(ierr);
1756a47d0d45SMatthew G. Knepley   for (v = 0, g = 0; v < numVerticesAdj; ++v) {
1757a47d0d45SMatthew G. Knepley     if (vertexLocal[v].rank != rank) {
1758a47d0d45SMatthew G. Knepley       localVertex[g]        = v+numCells;
1759a47d0d45SMatthew G. Knepley       remoteVertex[g].index = vertexLocal[v].index;
1760a47d0d45SMatthew G. Knepley       remoteVertex[g].rank  = vertexLocal[v].rank;
1761a47d0d45SMatthew G. Knepley       ++g;
1762a47d0d45SMatthew G. Knepley     }
1763a47d0d45SMatthew G. Knepley   }
1764a47d0d45SMatthew G. Knepley   ierr = PetscFree2(vertexLocal, vertexOwner);CHKERRQ(ierr);
1765a47d0d45SMatthew G. Knepley   if (g != numVerticesGhost) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid number of vertex ghosts %D should be %D", g, numVerticesGhost);
1766a47d0d45SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
1767a47d0d45SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) sfPoint, "point SF");CHKERRQ(ierr);
1768a47d0d45SMatthew G. Knepley   ierr = PetscSFSetGraph(sfPoint, numCells+numVerticesAdj, numVerticesGhost, localVertex, PETSC_OWN_POINTER, remoteVertex, PETSC_OWN_POINTER);CHKERRQ(ierr);
1769a47d0d45SMatthew G. Knepley   ierr = PetscLayoutDestroy(&vLayout);CHKERRQ(ierr);
1770a47d0d45SMatthew G. Knepley   PetscHashIDestroy(vhash);
1771a47d0d45SMatthew G. Knepley   /* Fill in the rest of the topology structure */
1772a47d0d45SMatthew G. Knepley   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
1773a47d0d45SMatthew G. Knepley   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
1774a47d0d45SMatthew G. Knepley   PetscFunctionReturn(0);
1775a47d0d45SMatthew G. Knepley }
1776a47d0d45SMatthew G. Knepley 
1777a47d0d45SMatthew G. Knepley /*
1778a47d0d45SMatthew G. Knepley   This takes as input the coordinates for each owned vertex
1779a47d0d45SMatthew G. Knepley */
178021016a8bSBarry Smith static PetscErrorCode DMPlexBuildCoordinates_Parallel_Private(DM dm, PetscInt spaceDim, PetscInt numCells, PetscInt numV, PetscSF sfVert, const PetscReal vertexCoords[])
1781a47d0d45SMatthew G. Knepley {
1782a47d0d45SMatthew G. Knepley   PetscSection   coordSection;
1783a47d0d45SMatthew G. Knepley   Vec            coordinates;
1784a47d0d45SMatthew G. Knepley   PetscScalar   *coords;
1785c5e08f72SMatthew G. Knepley   PetscInt       numVertices, numVerticesAdj, coordSize, v;
1786a47d0d45SMatthew G. Knepley   PetscErrorCode ierr;
1787a47d0d45SMatthew G. Knepley 
1788a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
1789a47d0d45SMatthew G. Knepley   ierr = PetscSFGetGraph(sfVert, &numVertices, &numVerticesAdj, NULL, NULL);CHKERRQ(ierr);
1790a47d0d45SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
1791a47d0d45SMatthew G. Knepley   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
1792a47d0d45SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(coordSection, 0, spaceDim);CHKERRQ(ierr);
1793a47d0d45SMatthew G. Knepley   ierr = PetscSectionSetChart(coordSection, numCells, numCells + numVerticesAdj);CHKERRQ(ierr);
1794a47d0d45SMatthew G. Knepley   for (v = numCells; v < numCells+numVerticesAdj; ++v) {
1795a47d0d45SMatthew G. Knepley     ierr = PetscSectionSetDof(coordSection, v, spaceDim);CHKERRQ(ierr);
1796a47d0d45SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(coordSection, v, 0, spaceDim);CHKERRQ(ierr);
1797a47d0d45SMatthew G. Knepley   }
1798a47d0d45SMatthew G. Knepley   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
1799a47d0d45SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
1800a47d0d45SMatthew G. Knepley   ierr = VecCreate(PetscObjectComm((PetscObject)dm), &coordinates);CHKERRQ(ierr);
1801a47d0d45SMatthew G. Knepley   ierr = VecSetBlockSize(coordinates, spaceDim);CHKERRQ(ierr);
1802a47d0d45SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
1803a47d0d45SMatthew G. Knepley   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
1804a47d0d45SMatthew G. Knepley   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
1805a47d0d45SMatthew G. Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
1806a47d0d45SMatthew G. Knepley   {
1807a47d0d45SMatthew G. Knepley     MPI_Datatype coordtype;
1808a47d0d45SMatthew G. Knepley 
1809a47d0d45SMatthew G. Knepley     /* Need a temp buffer for coords if we have complex/single */
181021016a8bSBarry Smith     ierr = MPI_Type_contiguous(spaceDim, MPIU_SCALAR, &coordtype);CHKERRQ(ierr);
1811a47d0d45SMatthew G. Knepley     ierr = MPI_Type_commit(&coordtype);CHKERRQ(ierr);
181221016a8bSBarry Smith #if defined(PETSC_USE_COMPLEX)
181321016a8bSBarry Smith     {
181421016a8bSBarry Smith     PetscScalar *svertexCoords;
181521016a8bSBarry Smith     PetscInt    i;
181621016a8bSBarry Smith     ierr = PetscMalloc1(numV*spaceDim,&svertexCoords);CHKERRQ(ierr);
181721016a8bSBarry Smith     for (i=0; i<numV*spaceDim; i++) svertexCoords[i] = vertexCoords[i];
181821016a8bSBarry Smith     ierr = PetscSFBcastBegin(sfVert, coordtype, svertexCoords, coords);CHKERRQ(ierr);
181921016a8bSBarry Smith     ierr = PetscSFBcastEnd(sfVert, coordtype, svertexCoords, coords);CHKERRQ(ierr);
182021016a8bSBarry Smith     ierr = PetscFree(svertexCoords);CHKERRQ(ierr);
182121016a8bSBarry Smith     }
182221016a8bSBarry Smith #else
1823a47d0d45SMatthew G. Knepley     ierr = PetscSFBcastBegin(sfVert, coordtype, vertexCoords, coords);CHKERRQ(ierr);
1824a47d0d45SMatthew G. Knepley     ierr = PetscSFBcastEnd(sfVert, coordtype, vertexCoords, coords);CHKERRQ(ierr);
182521016a8bSBarry Smith #endif
1826a47d0d45SMatthew G. Knepley     ierr = MPI_Type_free(&coordtype);CHKERRQ(ierr);
1827a47d0d45SMatthew G. Knepley   }
1828a47d0d45SMatthew G. Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
1829a47d0d45SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
1830a47d0d45SMatthew G. Knepley   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
1831a47d0d45SMatthew G. Knepley   PetscFunctionReturn(0);
1832a47d0d45SMatthew G. Knepley }
1833a47d0d45SMatthew G. Knepley 
1834a47d0d45SMatthew G. Knepley /*@C
1835a47d0d45SMatthew G. Knepley   DMPlexCreateFromCellListParallel - This takes as input common mesh generator output, a list of the vertices for each cell, and produces a DM
1836a47d0d45SMatthew G. Knepley 
1837a47d0d45SMatthew G. Knepley   Input Parameters:
1838a47d0d45SMatthew G. Knepley + comm - The communicator
1839a47d0d45SMatthew G. Knepley . dim - The topological dimension of the mesh
1840a47d0d45SMatthew G. Knepley . numCells - The number of cells owned by this process
1841a47d0d45SMatthew G. Knepley . numVertices - The number of vertices owned by this process
1842a47d0d45SMatthew G. Knepley . numCorners - The number of vertices for each cell
1843a47d0d45SMatthew G. Knepley . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
1844a47d0d45SMatthew G. Knepley . cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
1845a47d0d45SMatthew G. Knepley . spaceDim - The spatial dimension used for coordinates
1846a47d0d45SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
1847a47d0d45SMatthew G. Knepley 
1848a47d0d45SMatthew G. Knepley   Output Parameter:
184918d54ad4SMichael Lange + dm - The DM
185018d54ad4SMichael Lange - vertexSF - Optional, SF describing complete vertex ownership
1851a47d0d45SMatthew G. Knepley 
1852a47d0d45SMatthew G. Knepley   Note: Two triangles sharing a face
1853a47d0d45SMatthew G. Knepley $
1854a47d0d45SMatthew G. Knepley $        2
1855a47d0d45SMatthew G. Knepley $      / | \
1856a47d0d45SMatthew G. Knepley $     /  |  \
1857a47d0d45SMatthew G. Knepley $    /   |   \
1858a47d0d45SMatthew G. Knepley $   0  0 | 1  3
1859a47d0d45SMatthew G. Knepley $    \   |   /
1860a47d0d45SMatthew G. Knepley $     \  |  /
1861a47d0d45SMatthew G. Knepley $      \ | /
1862a47d0d45SMatthew G. Knepley $        1
1863a47d0d45SMatthew G. Knepley would have input
1864a47d0d45SMatthew G. Knepley $  numCells = 2, numVertices = 4
1865a47d0d45SMatthew G. Knepley $  cells = [0 1 2  1 3 2]
1866a47d0d45SMatthew G. Knepley $
1867a47d0d45SMatthew G. Knepley which would result in the DMPlex
1868a47d0d45SMatthew G. Knepley $
1869a47d0d45SMatthew G. Knepley $        4
1870a47d0d45SMatthew G. Knepley $      / | \
1871a47d0d45SMatthew G. Knepley $     /  |  \
1872a47d0d45SMatthew G. Knepley $    /   |   \
1873a47d0d45SMatthew G. Knepley $   2  0 | 1  5
1874a47d0d45SMatthew G. Knepley $    \   |   /
1875a47d0d45SMatthew G. Knepley $     \  |  /
1876a47d0d45SMatthew G. Knepley $      \ | /
1877a47d0d45SMatthew G. Knepley $        3
1878a47d0d45SMatthew G. Knepley 
1879a47d0d45SMatthew G. Knepley   Level: beginner
1880a47d0d45SMatthew G. Knepley 
1881a47d0d45SMatthew G. Knepley .seealso: DMPlexCreateFromCellList(), DMPlexCreateFromDAG(), DMPlexCreate()
1882a47d0d45SMatthew G. Knepley @*/
1883fba955ccSBarry Smith PetscErrorCode DMPlexCreateFromCellListParallel(MPI_Comm comm, PetscInt dim, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, PetscBool interpolate, const int cells[], PetscInt spaceDim, const PetscReal vertexCoords[], PetscSF *vertexSF, DM *dm)
1884a47d0d45SMatthew G. Knepley {
1885a47d0d45SMatthew G. Knepley   PetscSF        sfVert;
1886a47d0d45SMatthew G. Knepley   PetscErrorCode ierr;
1887a47d0d45SMatthew G. Knepley 
1888a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
1889a47d0d45SMatthew G. Knepley   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
1890a47d0d45SMatthew G. Knepley   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
1891a47d0d45SMatthew G. Knepley   PetscValidLogicalCollectiveInt(*dm, dim, 2);
1892a47d0d45SMatthew G. Knepley   PetscValidLogicalCollectiveInt(*dm, spaceDim, 8);
1893a47d0d45SMatthew G. Knepley   ierr = DMSetDimension(*dm, dim);CHKERRQ(ierr);
1894a47d0d45SMatthew G. Knepley   ierr = DMPlexBuildFromCellList_Parallel_Private(*dm, numCells, numVertices, numCorners, cells, &sfVert);CHKERRQ(ierr);
1895a47d0d45SMatthew G. Knepley   if (interpolate) {
1896a47d0d45SMatthew G. Knepley     DM idm = NULL;
1897a47d0d45SMatthew G. Knepley 
1898a47d0d45SMatthew G. Knepley     ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr);
1899a47d0d45SMatthew G. Knepley     ierr = DMDestroy(dm);CHKERRQ(ierr);
1900a47d0d45SMatthew G. Knepley     *dm  = idm;
1901a47d0d45SMatthew G. Knepley   }
190221016a8bSBarry Smith   ierr = DMPlexBuildCoordinates_Parallel_Private(*dm, spaceDim, numCells, numVertices,sfVert, vertexCoords);CHKERRQ(ierr);
190318d54ad4SMichael Lange   if (vertexSF) *vertexSF = sfVert;
1904fba955ccSBarry Smith   else {ierr = PetscSFDestroy(&sfVert);CHKERRQ(ierr);}
1905a47d0d45SMatthew G. Knepley   PetscFunctionReturn(0);
1906a47d0d45SMatthew G. Knepley }
1907a47d0d45SMatthew G. Knepley 
19089298eaa6SMatthew G Knepley /*
19099298eaa6SMatthew G Knepley   This takes as input the common mesh generator output, a list of the vertices for each cell
19109298eaa6SMatthew G Knepley */
19118f767406SMatthew G. Knepley static PetscErrorCode DMPlexBuildFromCellList_Private(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const int cells[])
19129298eaa6SMatthew G Knepley {
19139298eaa6SMatthew G Knepley   PetscInt      *cone, c, p;
19149298eaa6SMatthew G Knepley   PetscErrorCode ierr;
19159298eaa6SMatthew G Knepley 
19169298eaa6SMatthew G Knepley   PetscFunctionBegin;
19179298eaa6SMatthew G Knepley   ierr = DMPlexSetChart(dm, 0, numCells+numVertices);CHKERRQ(ierr);
19189298eaa6SMatthew G Knepley   for (c = 0; c < numCells; ++c) {
19199298eaa6SMatthew G Knepley     ierr = DMPlexSetConeSize(dm, c, numCorners);CHKERRQ(ierr);
19209298eaa6SMatthew G Knepley   }
19219298eaa6SMatthew G Knepley   ierr = DMSetUp(dm);CHKERRQ(ierr);
19229298eaa6SMatthew G Knepley   ierr = DMGetWorkArray(dm, numCorners, PETSC_INT, &cone);CHKERRQ(ierr);
19239298eaa6SMatthew G Knepley   for (c = 0; c < numCells; ++c) {
19249298eaa6SMatthew G Knepley     for (p = 0; p < numCorners; ++p) {
19259298eaa6SMatthew G Knepley       cone[p] = cells[c*numCorners+p]+numCells;
19269298eaa6SMatthew G Knepley     }
19279298eaa6SMatthew G Knepley     ierr = DMPlexSetCone(dm, c, cone);CHKERRQ(ierr);
19289298eaa6SMatthew G Knepley   }
19299298eaa6SMatthew G Knepley   ierr = DMRestoreWorkArray(dm, numCorners, PETSC_INT, &cone);CHKERRQ(ierr);
19309298eaa6SMatthew G Knepley   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
19319298eaa6SMatthew G Knepley   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
19329298eaa6SMatthew G Knepley   PetscFunctionReturn(0);
19339298eaa6SMatthew G Knepley }
19349298eaa6SMatthew G Knepley 
19359298eaa6SMatthew G Knepley /*
19369298eaa6SMatthew G Knepley   This takes as input the coordinates for each vertex
19379298eaa6SMatthew G Knepley */
19388f767406SMatthew G. Knepley static PetscErrorCode DMPlexBuildCoordinates_Private(DM dm, PetscInt spaceDim, PetscInt numCells, PetscInt numVertices, const double vertexCoords[])
19399298eaa6SMatthew G Knepley {
19409298eaa6SMatthew G Knepley   PetscSection   coordSection;
19419298eaa6SMatthew G Knepley   Vec            coordinates;
19428b9ced59SLisandro Dalcin   DM             cdm;
19439298eaa6SMatthew G Knepley   PetscScalar   *coords;
19448b9ced59SLisandro Dalcin   PetscInt       v, d;
19459298eaa6SMatthew G Knepley   PetscErrorCode ierr;
19469298eaa6SMatthew G Knepley 
19479298eaa6SMatthew G Knepley   PetscFunctionBegin;
1948c2166f76SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
19499298eaa6SMatthew G Knepley   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
19509298eaa6SMatthew G Knepley   ierr = PetscSectionSetFieldComponents(coordSection, 0, spaceDim);CHKERRQ(ierr);
19519298eaa6SMatthew G Knepley   ierr = PetscSectionSetChart(coordSection, numCells, numCells + numVertices);CHKERRQ(ierr);
19529298eaa6SMatthew G Knepley   for (v = numCells; v < numCells+numVertices; ++v) {
19539298eaa6SMatthew G Knepley     ierr = PetscSectionSetDof(coordSection, v, spaceDim);CHKERRQ(ierr);
19549298eaa6SMatthew G Knepley     ierr = PetscSectionSetFieldDof(coordSection, v, 0, spaceDim);CHKERRQ(ierr);
19559298eaa6SMatthew G Knepley   }
19569298eaa6SMatthew G Knepley   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
19578b9ced59SLisandro Dalcin 
19588b9ced59SLisandro Dalcin   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
19598b9ced59SLisandro Dalcin   ierr = DMCreateLocalVector(cdm, &coordinates);CHKERRQ(ierr);
19604b66c01dSMatthew G. Knepley   ierr = VecSetBlockSize(coordinates, spaceDim);CHKERRQ(ierr);
19619298eaa6SMatthew G Knepley   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
19629298eaa6SMatthew G Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
19639298eaa6SMatthew G Knepley   for (v = 0; v < numVertices; ++v) {
19649298eaa6SMatthew G Knepley     for (d = 0; d < spaceDim; ++d) {
19659298eaa6SMatthew G Knepley       coords[v*spaceDim+d] = vertexCoords[v*spaceDim+d];
19669298eaa6SMatthew G Knepley     }
19679298eaa6SMatthew G Knepley   }
19689298eaa6SMatthew G Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
19699298eaa6SMatthew G Knepley   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
19709298eaa6SMatthew G Knepley   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
19719298eaa6SMatthew G Knepley   PetscFunctionReturn(0);
19729298eaa6SMatthew G Knepley }
19739298eaa6SMatthew G Knepley 
19749298eaa6SMatthew G Knepley /*@C
19759298eaa6SMatthew G Knepley   DMPlexCreateFromCellList - This takes as input common mesh generator output, a list of the vertices for each cell, and produces a DM
19769298eaa6SMatthew G Knepley 
19779298eaa6SMatthew G Knepley   Input Parameters:
19789298eaa6SMatthew G Knepley + comm - The communicator
19799298eaa6SMatthew G Knepley . dim - The topological dimension of the mesh
19809298eaa6SMatthew G Knepley . numCells - The number of cells
19819298eaa6SMatthew G Knepley . numVertices - The number of vertices
19829298eaa6SMatthew G Knepley . numCorners - The number of vertices for each cell
19839298eaa6SMatthew G Knepley . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
19849298eaa6SMatthew G Knepley . cells - An array of numCells*numCorners numbers, the vertices for each cell
19859298eaa6SMatthew G Knepley . spaceDim - The spatial dimension used for coordinates
19869298eaa6SMatthew G Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
19879298eaa6SMatthew G Knepley 
19889298eaa6SMatthew G Knepley   Output Parameter:
19899298eaa6SMatthew G Knepley . dm - The DM
19909298eaa6SMatthew G Knepley 
19919298eaa6SMatthew G Knepley   Note: Two triangles sharing a face
19929298eaa6SMatthew G Knepley $
19939298eaa6SMatthew G Knepley $        2
19949298eaa6SMatthew G Knepley $      / | \
19959298eaa6SMatthew G Knepley $     /  |  \
19969298eaa6SMatthew G Knepley $    /   |   \
19979298eaa6SMatthew G Knepley $   0  0 | 1  3
19989298eaa6SMatthew G Knepley $    \   |   /
19999298eaa6SMatthew G Knepley $     \  |  /
20009298eaa6SMatthew G Knepley $      \ | /
20019298eaa6SMatthew G Knepley $        1
20029298eaa6SMatthew G Knepley would have input
20039298eaa6SMatthew G Knepley $  numCells = 2, numVertices = 4
20049298eaa6SMatthew G Knepley $  cells = [0 1 2  1 3 2]
20059298eaa6SMatthew G Knepley $
20069298eaa6SMatthew G Knepley which would result in the DMPlex
20079298eaa6SMatthew G Knepley $
20089298eaa6SMatthew G Knepley $        4
20099298eaa6SMatthew G Knepley $      / | \
20109298eaa6SMatthew G Knepley $     /  |  \
20119298eaa6SMatthew G Knepley $    /   |   \
20129298eaa6SMatthew G Knepley $   2  0 | 1  5
20139298eaa6SMatthew G Knepley $    \   |   /
20149298eaa6SMatthew G Knepley $     \  |  /
20159298eaa6SMatthew G Knepley $      \ | /
20169298eaa6SMatthew G Knepley $        3
20179298eaa6SMatthew G Knepley 
20189298eaa6SMatthew G Knepley   Level: beginner
20199298eaa6SMatthew G Knepley 
2020939f6067SMatthew G. Knepley .seealso: DMPlexCreateFromDAG(), DMPlexCreate()
20219298eaa6SMatthew G Knepley @*/
20229298eaa6SMatthew G Knepley 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)
20239298eaa6SMatthew G Knepley {
20249298eaa6SMatthew G Knepley   PetscErrorCode ierr;
20259298eaa6SMatthew G Knepley 
20269298eaa6SMatthew G Knepley   PetscFunctionBegin;
20279298eaa6SMatthew G Knepley   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
20289298eaa6SMatthew G Knepley   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
2029c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(*dm, dim);CHKERRQ(ierr);
20309298eaa6SMatthew G Knepley   ierr = DMPlexBuildFromCellList_Private(*dm, numCells, numVertices, numCorners, cells);CHKERRQ(ierr);
20319298eaa6SMatthew G Knepley   if (interpolate) {
2032e56d480eSMatthew G. Knepley     DM idm = NULL;
20339298eaa6SMatthew G Knepley 
20349298eaa6SMatthew G Knepley     ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr);
20359298eaa6SMatthew G Knepley     ierr = DMDestroy(dm);CHKERRQ(ierr);
20369298eaa6SMatthew G Knepley     *dm  = idm;
20379298eaa6SMatthew G Knepley   }
20389298eaa6SMatthew G Knepley   ierr = DMPlexBuildCoordinates_Private(*dm, spaceDim, numCells, numVertices, vertexCoords);CHKERRQ(ierr);
20399298eaa6SMatthew G Knepley   PetscFunctionReturn(0);
20409298eaa6SMatthew G Knepley }
20419298eaa6SMatthew G Knepley 
2042939f6067SMatthew G. Knepley /*@
2043939f6067SMatthew G. Knepley   DMPlexCreateFromDAG - This takes as input the adjacency-list representation of the Directed Acyclic Graph (Hasse Diagram) encoding a mesh, and produces a DM
2044939f6067SMatthew G. Knepley 
2045939f6067SMatthew G. Knepley   Input Parameters:
2046c73cfb54SMatthew G. Knepley + dm - The empty DM object, usually from DMCreate() and DMSetDimension()
2047939f6067SMatthew G. Knepley . depth - The depth of the DAG
2048939f6067SMatthew G. Knepley . numPoints - The number of points at each depth
2049939f6067SMatthew G. Knepley . coneSize - The cone size of each point
2050939f6067SMatthew G. Knepley . cones - The concatenation of the cone points for each point, the cone list must be oriented correctly for each point
2051939f6067SMatthew G. Knepley . coneOrientations - The orientation of each cone point
2052939f6067SMatthew G. Knepley - vertexCoords - An array of numVertices*dim numbers, the coordinates of each vertex
2053939f6067SMatthew G. Knepley 
2054939f6067SMatthew G. Knepley   Output Parameter:
2055939f6067SMatthew G. Knepley . dm - The DM
2056939f6067SMatthew G. Knepley 
2057939f6067SMatthew G. Knepley   Note: Two triangles sharing a face would have input
2058939f6067SMatthew G. Knepley $  depth = 1, numPoints = [4 2], coneSize = [3 3 0 0 0 0]
2059939f6067SMatthew G. Knepley $  cones = [2 3 4  3 5 4], coneOrientations = [0 0 0  0 0 0]
2060939f6067SMatthew G. Knepley $ vertexCoords = [-1.0 0.0  0.0 -1.0  0.0 1.0  1.0 0.0]
2061939f6067SMatthew G. Knepley $
2062939f6067SMatthew G. Knepley which would result in the DMPlex
2063939f6067SMatthew G. Knepley $
2064939f6067SMatthew G. Knepley $        4
2065939f6067SMatthew G. Knepley $      / | \
2066939f6067SMatthew G. Knepley $     /  |  \
2067939f6067SMatthew G. Knepley $    /   |   \
2068939f6067SMatthew G. Knepley $   2  0 | 1  5
2069939f6067SMatthew G. Knepley $    \   |   /
2070939f6067SMatthew G. Knepley $     \  |  /
2071939f6067SMatthew G. Knepley $      \ | /
2072939f6067SMatthew G. Knepley $        3
2073939f6067SMatthew G. Knepley $
2074939f6067SMatthew G. Knepley $ Notice that all points are numbered consecutively, unlikely DMPlexCreateFromCellList()
2075939f6067SMatthew G. Knepley 
2076939f6067SMatthew G. Knepley   Level: advanced
2077939f6067SMatthew G. Knepley 
2078939f6067SMatthew G. Knepley .seealso: DMPlexCreateFromCellList(), DMPlexCreate()
2079939f6067SMatthew G. Knepley @*/
20809298eaa6SMatthew G Knepley PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[])
20819298eaa6SMatthew G Knepley {
20829298eaa6SMatthew G Knepley   Vec            coordinates;
20839298eaa6SMatthew G Knepley   PetscSection   coordSection;
20849298eaa6SMatthew G Knepley   PetscScalar    *coords;
2085811e8653SToby Isaac   PetscInt       coordSize, firstVertex = -1, pStart = 0, pEnd = 0, p, v, dim, dimEmbed, d, off;
20869298eaa6SMatthew G Knepley   PetscErrorCode ierr;
20879298eaa6SMatthew G Knepley 
20889298eaa6SMatthew G Knepley   PetscFunctionBegin;
2089c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2090811e8653SToby Isaac   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
2091811e8653SToby Isaac   if (dimEmbed < dim) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Embedding dimension %d cannot be less than intrinsic dimension %d",dimEmbed,dim);
20929298eaa6SMatthew G Knepley   for (d = 0; d <= depth; ++d) pEnd += numPoints[d];
20939298eaa6SMatthew G Knepley   ierr = DMPlexSetChart(dm, pStart, pEnd);CHKERRQ(ierr);
20949298eaa6SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
20959298eaa6SMatthew G Knepley     ierr = DMPlexSetConeSize(dm, p, coneSize[p-pStart]);CHKERRQ(ierr);
209697e052ccSToby Isaac     if (firstVertex < 0 && !coneSize[p - pStart]) {
209797e052ccSToby Isaac       firstVertex = p - pStart;
20989298eaa6SMatthew G Knepley     }
209997e052ccSToby Isaac   }
210097e052ccSToby Isaac   if (firstVertex < 0 && numPoints[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected %d vertices but could not find any", numPoints[0]);
21019298eaa6SMatthew G Knepley   ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
21029298eaa6SMatthew G Knepley   for (p = pStart, off = 0; p < pEnd; off += coneSize[p-pStart], ++p) {
21039298eaa6SMatthew G Knepley     ierr = DMPlexSetCone(dm, p, &cones[off]);CHKERRQ(ierr);
21049298eaa6SMatthew G Knepley     ierr = DMPlexSetConeOrientation(dm, p, &coneOrientations[off]);CHKERRQ(ierr);
21059298eaa6SMatthew G Knepley   }
21069298eaa6SMatthew G Knepley   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
21079298eaa6SMatthew G Knepley   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
21089298eaa6SMatthew G Knepley   /* Build coordinates */
2109c2166f76SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
21109298eaa6SMatthew G Knepley   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
2111811e8653SToby Isaac   ierr = PetscSectionSetFieldComponents(coordSection, 0, dimEmbed);CHKERRQ(ierr);
21129298eaa6SMatthew G Knepley   ierr = PetscSectionSetChart(coordSection, firstVertex, firstVertex+numPoints[0]);CHKERRQ(ierr);
21139298eaa6SMatthew G Knepley   for (v = firstVertex; v < firstVertex+numPoints[0]; ++v) {
2114811e8653SToby Isaac     ierr = PetscSectionSetDof(coordSection, v, dimEmbed);CHKERRQ(ierr);
2115811e8653SToby Isaac     ierr = PetscSectionSetFieldDof(coordSection, v, 0, dimEmbed);CHKERRQ(ierr);
21169298eaa6SMatthew G Knepley   }
21179298eaa6SMatthew G Knepley   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
21189298eaa6SMatthew G Knepley   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
21198b9ced59SLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
21206f8cbbeeSMatthew G Knepley   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
21219298eaa6SMatthew G Knepley   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
21228b9ced59SLisandro Dalcin   ierr = VecSetBlockSize(coordinates, dimEmbed);CHKERRQ(ierr);
21232eb5907fSJed Brown   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
21249298eaa6SMatthew G Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
21259298eaa6SMatthew G Knepley   for (v = 0; v < numPoints[0]; ++v) {
21269298eaa6SMatthew G Knepley     PetscInt off;
21279298eaa6SMatthew G Knepley 
21289298eaa6SMatthew G Knepley     ierr = PetscSectionGetOffset(coordSection, v+firstVertex, &off);CHKERRQ(ierr);
2129811e8653SToby Isaac     for (d = 0; d < dimEmbed; ++d) {
2130811e8653SToby Isaac       coords[off+d] = vertexCoords[v*dimEmbed+d];
21319298eaa6SMatthew G Knepley     }
21329298eaa6SMatthew G Knepley   }
21339298eaa6SMatthew G Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
21349298eaa6SMatthew G Knepley   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
21359298eaa6SMatthew G Knepley   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
21369298eaa6SMatthew G Knepley   PetscFunctionReturn(0);
21379298eaa6SMatthew G Knepley }
21388415267dSToby Isaac 
2139ca522641SMatthew G. Knepley /*@C
2140ca522641SMatthew G. Knepley   DMPlexCreateFromFile - This takes a filename and produces a DM
2141ca522641SMatthew G. Knepley 
2142ca522641SMatthew G. Knepley   Input Parameters:
2143ca522641SMatthew G. Knepley + comm - The communicator
2144ca522641SMatthew G. Knepley . filename - A file name
2145ca522641SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
2146ca522641SMatthew G. Knepley 
2147ca522641SMatthew G. Knepley   Output Parameter:
2148ca522641SMatthew G. Knepley . dm - The DM
2149ca522641SMatthew G. Knepley 
2150ca522641SMatthew G. Knepley   Level: beginner
2151ca522641SMatthew G. Knepley 
2152ca522641SMatthew G. Knepley .seealso: DMPlexCreateFromDAG(), DMPlexCreateFromCellList(), DMPlexCreate()
2153ca522641SMatthew G. Knepley @*/
2154ca522641SMatthew G. Knepley PetscErrorCode DMPlexCreateFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
2155ca522641SMatthew G. Knepley {
215696a26029SBarry Smith   const char    *extGmsh   = ".msh";
2157ca522641SMatthew G. Knepley   const char    *extCGNS   = ".cgns";
2158ca522641SMatthew G. Knepley   const char    *extExodus = ".exo";
21592f0bd6dcSMichael Lange   const char    *extFluent = ".cas";
2160cc2f8f65SMatthew G. Knepley   const char    *extHDF5   = ".h5";
2161707dd687SMichael Lange   const char    *extMed    = ".med";
2162ca522641SMatthew G. Knepley   size_t         len;
2163707dd687SMichael Lange   PetscBool      isGmsh, isCGNS, isExodus, isFluent, isHDF5, isMed;
2164ca522641SMatthew G. Knepley   PetscMPIInt    rank;
2165ca522641SMatthew G. Knepley   PetscErrorCode ierr;
2166ca522641SMatthew G. Knepley 
2167ca522641SMatthew G. Knepley   PetscFunctionBegin;
2168ca522641SMatthew G. Knepley   PetscValidPointer(filename, 2);
2169ca522641SMatthew G. Knepley   PetscValidPointer(dm, 4);
2170ca522641SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
2171ca522641SMatthew G. Knepley   ierr = PetscStrlen(filename, &len);CHKERRQ(ierr);
2172ca522641SMatthew G. Knepley   if (!len) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Filename must be a valid path");
2173ca522641SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)], extGmsh,   4, &isGmsh);CHKERRQ(ierr);
2174ca522641SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-5)], extCGNS,   5, &isCGNS);CHKERRQ(ierr);
2175ca522641SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)], extExodus, 4, &isExodus);CHKERRQ(ierr);
21762f0bd6dcSMichael Lange   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)], extFluent, 4, &isFluent);CHKERRQ(ierr);
2177cc2f8f65SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-3)], extHDF5,   3, &isHDF5);CHKERRQ(ierr);
2178707dd687SMichael Lange   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)], extMed,    4, &isMed);CHKERRQ(ierr);
2179ca522641SMatthew G. Knepley   if (isGmsh) {
21807d282ae0SMichael Lange     ierr = DMPlexCreateGmshFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
2181ca522641SMatthew G. Knepley   } else if (isCGNS) {
2182ca522641SMatthew G. Knepley     ierr = DMPlexCreateCGNSFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
2183ca522641SMatthew G. Knepley   } else if (isExodus) {
2184ca522641SMatthew G. Knepley     ierr = DMPlexCreateExodusFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
21852f0bd6dcSMichael Lange   } else if (isFluent) {
21862f0bd6dcSMichael Lange     ierr = DMPlexCreateFluentFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
2187cc2f8f65SMatthew G. Knepley   } else if (isHDF5) {
2188cc2f8f65SMatthew G. Knepley     PetscViewer viewer;
2189cc2f8f65SMatthew G. Knepley 
2190cc2f8f65SMatthew G. Knepley     ierr = PetscViewerCreate(comm, &viewer);CHKERRQ(ierr);
2191cc2f8f65SMatthew G. Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERHDF5);CHKERRQ(ierr);
2192cc2f8f65SMatthew G. Knepley     ierr = PetscViewerFileSetMode(viewer, FILE_MODE_READ);CHKERRQ(ierr);
2193cc2f8f65SMatthew G. Knepley     ierr = PetscViewerFileSetName(viewer, filename);CHKERRQ(ierr);
2194cc2f8f65SMatthew G. Knepley     ierr = DMCreate(comm, dm);CHKERRQ(ierr);
2195cc2f8f65SMatthew G. Knepley     ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
2196cc2f8f65SMatthew G. Knepley     ierr = DMLoad(*dm, viewer);CHKERRQ(ierr);
2197cc2f8f65SMatthew G. Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
2198707dd687SMichael Lange   } else if (isMed) {
2199707dd687SMichael Lange     ierr = DMPlexCreateMedFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
2200ca522641SMatthew G. Knepley   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename);
2201ca522641SMatthew G. Knepley   PetscFunctionReturn(0);
2202ca522641SMatthew G. Knepley }
2203ca522641SMatthew G. Knepley 
22048415267dSToby Isaac /*@
22058415267dSToby Isaac   DMPlexCreateReferenceCell - Create a DMPLEX with the appropriate FEM reference cell
22068415267dSToby Isaac 
22078415267dSToby Isaac   Collective on comm
22088415267dSToby Isaac 
22098415267dSToby Isaac   Input Parameters:
22108415267dSToby Isaac + comm    - The communicator
22118415267dSToby Isaac . dim     - The spatial dimension
22128415267dSToby Isaac - simplex - Flag for simplex, otherwise use a tensor-product cell
22138415267dSToby Isaac 
22148415267dSToby Isaac   Output Parameter:
22158415267dSToby Isaac . refdm - The reference cell
22168415267dSToby Isaac 
22178415267dSToby Isaac   Level: intermediate
22188415267dSToby Isaac 
22198415267dSToby Isaac .keywords: reference cell
22208415267dSToby Isaac .seealso:
22218415267dSToby Isaac @*/
22228415267dSToby Isaac PetscErrorCode DMPlexCreateReferenceCell(MPI_Comm comm, PetscInt dim, PetscBool simplex, DM *refdm)
22238415267dSToby Isaac {
22248415267dSToby Isaac   DM             rdm;
2225cdf740e0SMatthew G. Knepley   Vec            coords;
22268415267dSToby Isaac   PetscErrorCode ierr;
22278415267dSToby Isaac 
22288415267dSToby Isaac   PetscFunctionBeginUser;
22298415267dSToby Isaac   ierr = DMCreate(comm, &rdm);CHKERRQ(ierr);
22308415267dSToby Isaac   ierr = DMSetType(rdm, DMPLEX);CHKERRQ(ierr);
22318415267dSToby Isaac   ierr = DMSetDimension(rdm, dim);CHKERRQ(ierr);
22328415267dSToby Isaac   switch (dim) {
22338415267dSToby Isaac   case 0:
22348415267dSToby Isaac   {
22358415267dSToby Isaac     PetscInt    numPoints[1]        = {1};
22368415267dSToby Isaac     PetscInt    coneSize[1]         = {0};
22378415267dSToby Isaac     PetscInt    cones[1]            = {0};
22388415267dSToby Isaac     PetscInt    coneOrientations[1] = {0};
22398415267dSToby Isaac     PetscScalar vertexCoords[1]     = {0.0};
22408415267dSToby Isaac 
22418415267dSToby Isaac     ierr = DMPlexCreateFromDAG(rdm, 0, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
22428415267dSToby Isaac   }
22438415267dSToby Isaac   break;
22448415267dSToby Isaac   case 1:
22458415267dSToby Isaac   {
22468415267dSToby Isaac     PetscInt    numPoints[2]        = {2, 1};
22478415267dSToby Isaac     PetscInt    coneSize[3]         = {2, 0, 0};
22488415267dSToby Isaac     PetscInt    cones[2]            = {1, 2};
22498415267dSToby Isaac     PetscInt    coneOrientations[2] = {0, 0};
22508415267dSToby Isaac     PetscScalar vertexCoords[2]     = {-1.0,  1.0};
22518415267dSToby Isaac 
22528415267dSToby Isaac     ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
22538415267dSToby Isaac   }
22548415267dSToby Isaac   break;
22558415267dSToby Isaac   case 2:
22568415267dSToby Isaac     if (simplex) {
22578415267dSToby Isaac       PetscInt    numPoints[2]        = {3, 1};
22588415267dSToby Isaac       PetscInt    coneSize[4]         = {3, 0, 0, 0};
22598415267dSToby Isaac       PetscInt    cones[3]            = {1, 2, 3};
22608415267dSToby Isaac       PetscInt    coneOrientations[3] = {0, 0, 0};
22618415267dSToby Isaac       PetscScalar vertexCoords[6]     = {-1.0, -1.0,  1.0, -1.0,  -1.0, 1.0};
22628415267dSToby Isaac 
22638415267dSToby Isaac       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
22648415267dSToby Isaac     } else {
22658415267dSToby Isaac       PetscInt    numPoints[2]        = {4, 1};
22668415267dSToby Isaac       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
22678415267dSToby Isaac       PetscInt    cones[4]            = {1, 2, 3, 4};
22688415267dSToby Isaac       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
22698415267dSToby Isaac       PetscScalar vertexCoords[8]     = {-1.0, -1.0,  1.0, -1.0,  1.0, 1.0,  -1.0, 1.0};
22708415267dSToby Isaac 
22718415267dSToby Isaac       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
22728415267dSToby Isaac     }
22738415267dSToby Isaac   break;
22748415267dSToby Isaac   case 3:
22758415267dSToby Isaac     if (simplex) {
22768415267dSToby Isaac       PetscInt    numPoints[2]        = {4, 1};
22778415267dSToby Isaac       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
22788415267dSToby Isaac       PetscInt    cones[4]            = {1, 3, 2, 4};
22798415267dSToby Isaac       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
22808415267dSToby Isaac       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};
22818415267dSToby Isaac 
22828415267dSToby Isaac       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
22838415267dSToby Isaac     } else {
22848415267dSToby Isaac       PetscInt    numPoints[2]        = {8, 1};
22858415267dSToby Isaac       PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
22868415267dSToby Isaac       PetscInt    cones[8]            = {1, 4, 3, 2, 5, 6, 7, 8};
22878415267dSToby Isaac       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
22888415267dSToby Isaac       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,
22898415267dSToby Isaac                                          -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  1.0, 1.0,  1.0,  -1.0, 1.0,  1.0};
22908415267dSToby Isaac 
22918415267dSToby Isaac       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
22928415267dSToby Isaac     }
22938415267dSToby Isaac   break;
22948415267dSToby Isaac   default:
22958415267dSToby Isaac     SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Cannot create reference cell for dimension %d", dim);
22968415267dSToby Isaac   }
22978415267dSToby Isaac   *refdm = NULL;
22988415267dSToby Isaac   ierr = DMPlexInterpolate(rdm, refdm);CHKERRQ(ierr);
2299cdf740e0SMatthew G. Knepley   if (rdm->coordinateDM) {
2300cdf740e0SMatthew G. Knepley     DM           ncdm;
2301cdf740e0SMatthew G. Knepley     PetscSection cs;
2302cdf740e0SMatthew G. Knepley     PetscInt     pEnd = -1;
2303cdf740e0SMatthew G. Knepley 
2304cdf740e0SMatthew G. Knepley     ierr = DMGetDefaultSection(rdm->coordinateDM, &cs);CHKERRQ(ierr);
2305cdf740e0SMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
2306cdf740e0SMatthew G. Knepley     if (pEnd >= 0) {
2307cdf740e0SMatthew G. Knepley       ierr = DMClone(rdm->coordinateDM, &ncdm);CHKERRQ(ierr);
2308cdf740e0SMatthew G. Knepley       ierr = DMSetDefaultSection(ncdm, cs);CHKERRQ(ierr);
2309a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*refdm, ncdm);CHKERRQ(ierr);
2310cdf740e0SMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
2311cdf740e0SMatthew G. Knepley     }
2312cdf740e0SMatthew G. Knepley   }
2313cdf740e0SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(rdm, &coords);CHKERRQ(ierr);
2314cdf740e0SMatthew G. Knepley   if (coords) {
2315cdf740e0SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*refdm, coords);CHKERRQ(ierr);
2316cdf740e0SMatthew G. Knepley   } else {
2317cdf740e0SMatthew G. Knepley     ierr = DMGetCoordinates(rdm, &coords);CHKERRQ(ierr);
2318cdf740e0SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*refdm, coords);CHKERRQ(ierr);}
2319cdf740e0SMatthew G. Knepley   }
23208415267dSToby Isaac   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
23218415267dSToby Isaac   PetscFunctionReturn(0);
23228415267dSToby Isaac }
2323