xref: /petsc/src/dm/impls/plex/plexcreate.c (revision 9318fe57260f91432aebecc154ea28ef5cb286a6)
1552f7358SJed Brown #define PETSCDM_DLL
2af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>    /*I   "petscdmplex.h"   I*/
3e8f14785SLisandro Dalcin #include <petsc/private/hashseti.h>          /*I   "petscdmplex.h"   I*/
40c312b8eSJed Brown #include <petscsf.h>
5552f7358SJed Brown 
6b09969d6SVaclav Hapla PetscLogEvent DMPLEX_CreateFromFile, DMPLEX_BuildFromCellList, DMPLEX_BuildCoordinatesFromCellList;
758cd63d5SVaclav Hapla 
8*9318fe57SMatthew G. Knepley /* External function declarations here */
9*9318fe57SMatthew G. Knepley static PetscErrorCode DMInitialize_Plex(DM dm);
10*9318fe57SMatthew G. Knepley 
11*9318fe57SMatthew G. Knepley /* Replace dm with the contents of ndm, and then destroy ndm
12*9318fe57SMatthew G. Knepley    - Share the DM_Plex structure
13*9318fe57SMatthew G. Knepley    - Share the coordinates
14*9318fe57SMatthew G. Knepley    - Share the SF
15*9318fe57SMatthew G. Knepley */
16*9318fe57SMatthew G. Knepley static PetscErrorCode DMPlexReplace_Static(DM dm, DM *ndm)
17*9318fe57SMatthew G. Knepley {
18*9318fe57SMatthew G. Knepley   PetscSF               sf;
19*9318fe57SMatthew G. Knepley   DM                    dmNew = *ndm, coordDM, coarseDM;
20*9318fe57SMatthew G. Knepley   Vec                   coords;
21*9318fe57SMatthew G. Knepley   PetscBool             isper;
22*9318fe57SMatthew G. Knepley   const PetscReal      *maxCell, *L;
23*9318fe57SMatthew G. Knepley   const DMBoundaryType *bd;
24*9318fe57SMatthew G. Knepley   PetscInt              dim, cdim;
25*9318fe57SMatthew G. Knepley   PetscErrorCode        ierr;
26*9318fe57SMatthew G. Knepley 
27*9318fe57SMatthew G. Knepley   PetscFunctionBegin;
28*9318fe57SMatthew G. Knepley   if (dm == dmNew) {
29*9318fe57SMatthew G. Knepley     ierr = DMDestroy(ndm);CHKERRQ(ierr);
30*9318fe57SMatthew G. Knepley     PetscFunctionReturn(0);
31*9318fe57SMatthew G. Knepley   }
32*9318fe57SMatthew G. Knepley   dm->setupcalled = dmNew->setupcalled;
33*9318fe57SMatthew G. Knepley   ierr = DMGetDimension(dmNew, &dim);CHKERRQ(ierr);
34*9318fe57SMatthew G. Knepley   ierr = DMSetDimension(dm, dim);CHKERRQ(ierr);
35*9318fe57SMatthew G. Knepley   ierr = DMGetCoordinateDim(dmNew, &cdim);CHKERRQ(ierr);
36*9318fe57SMatthew G. Knepley   ierr = DMSetCoordinateDim(dm, cdim);CHKERRQ(ierr);
37*9318fe57SMatthew G. Knepley   ierr = DMGetPointSF(dmNew, &sf);CHKERRQ(ierr);
38*9318fe57SMatthew G. Knepley   ierr = DMSetPointSF(dm, sf);CHKERRQ(ierr);
39*9318fe57SMatthew G. Knepley   ierr = DMGetCoordinateDM(dmNew, &coordDM);CHKERRQ(ierr);
40*9318fe57SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dmNew, &coords);CHKERRQ(ierr);
41*9318fe57SMatthew G. Knepley   ierr = DMSetCoordinateDM(dm, coordDM);CHKERRQ(ierr);
42*9318fe57SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, coords);CHKERRQ(ierr);
43*9318fe57SMatthew G. Knepley   /* Do not want to create the coordinate field if it does not already exist, so do not call DMGetCoordinateField() */
44*9318fe57SMatthew G. Knepley   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
45*9318fe57SMatthew G. Knepley   dm->coordinateField = dmNew->coordinateField;
46*9318fe57SMatthew G. Knepley   ierr = DMGetPeriodicity(dmNew, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
47*9318fe57SMatthew G. Knepley   ierr = DMSetPeriodicity(dm, isper, maxCell, L, bd);CHKERRQ(ierr);
48*9318fe57SMatthew G. Knepley   ierr = DMDestroy_Plex(dm);CHKERRQ(ierr);
49*9318fe57SMatthew G. Knepley   ierr = DMInitialize_Plex(dm);CHKERRQ(ierr);
50*9318fe57SMatthew G. Knepley   dm->data = dmNew->data;
51*9318fe57SMatthew G. Knepley   ((DM_Plex *) dmNew->data)->refct++;
52*9318fe57SMatthew G. Knepley   ierr = DMDestroyLabelLinkList_Internal(dm);CHKERRQ(ierr);
53*9318fe57SMatthew G. Knepley   ierr = DMCopyLabels(dmNew, dm, PETSC_OWN_POINTER, PETSC_TRUE);CHKERRQ(ierr);
54*9318fe57SMatthew G. Knepley   ierr = DMGetCoarseDM(dmNew,&coarseDM);CHKERRQ(ierr);
55*9318fe57SMatthew G. Knepley   ierr = DMSetCoarseDM(dm,coarseDM);CHKERRQ(ierr);
56*9318fe57SMatthew G. Knepley   ierr = DMDestroy(ndm);CHKERRQ(ierr);
57*9318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
58*9318fe57SMatthew G. Knepley }
59*9318fe57SMatthew G. Knepley 
60*9318fe57SMatthew G. Knepley /* Swap dm with the contents of dmNew
61*9318fe57SMatthew G. Knepley    - Swap the DM_Plex structure
62*9318fe57SMatthew G. Knepley    - Swap the coordinates
63*9318fe57SMatthew G. Knepley    - Swap the point PetscSF
64*9318fe57SMatthew G. Knepley */
65*9318fe57SMatthew G. Knepley static PetscErrorCode DMPlexSwap_Static(DM dmA, DM dmB)
66*9318fe57SMatthew G. Knepley {
67*9318fe57SMatthew G. Knepley   DM              coordDMA, coordDMB;
68*9318fe57SMatthew G. Knepley   Vec             coordsA,  coordsB;
69*9318fe57SMatthew G. Knepley   PetscSF         sfA,      sfB;
70*9318fe57SMatthew G. Knepley   DMField         fieldTmp;
71*9318fe57SMatthew G. Knepley   void            *tmp;
72*9318fe57SMatthew G. Knepley   DMLabelLink     listTmp;
73*9318fe57SMatthew G. Knepley   DMLabel         depthTmp;
74*9318fe57SMatthew G. Knepley   PetscInt        tmpI;
75*9318fe57SMatthew G. Knepley   PetscErrorCode  ierr;
76*9318fe57SMatthew G. Knepley 
77*9318fe57SMatthew G. Knepley   PetscFunctionBegin;
78*9318fe57SMatthew G. Knepley   if (dmA == dmB) PetscFunctionReturn(0);
79*9318fe57SMatthew G. Knepley   ierr = DMGetPointSF(dmA, &sfA);CHKERRQ(ierr);
80*9318fe57SMatthew G. Knepley   ierr = DMGetPointSF(dmB, &sfB);CHKERRQ(ierr);
81*9318fe57SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) sfA);CHKERRQ(ierr);
82*9318fe57SMatthew G. Knepley   ierr = DMSetPointSF(dmA, sfB);CHKERRQ(ierr);
83*9318fe57SMatthew G. Knepley   ierr = DMSetPointSF(dmB, sfA);CHKERRQ(ierr);
84*9318fe57SMatthew G. Knepley   ierr = PetscObjectDereference((PetscObject) sfA);CHKERRQ(ierr);
85*9318fe57SMatthew G. Knepley 
86*9318fe57SMatthew G. Knepley   ierr = DMGetCoordinateDM(dmA, &coordDMA);CHKERRQ(ierr);
87*9318fe57SMatthew G. Knepley   ierr = DMGetCoordinateDM(dmB, &coordDMB);CHKERRQ(ierr);
88*9318fe57SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) coordDMA);CHKERRQ(ierr);
89*9318fe57SMatthew G. Knepley   ierr = DMSetCoordinateDM(dmA, coordDMB);CHKERRQ(ierr);
90*9318fe57SMatthew G. Knepley   ierr = DMSetCoordinateDM(dmB, coordDMA);CHKERRQ(ierr);
91*9318fe57SMatthew G. Knepley   ierr = PetscObjectDereference((PetscObject) coordDMA);CHKERRQ(ierr);
92*9318fe57SMatthew G. Knepley 
93*9318fe57SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dmA, &coordsA);CHKERRQ(ierr);
94*9318fe57SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dmB, &coordsB);CHKERRQ(ierr);
95*9318fe57SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) coordsA);CHKERRQ(ierr);
96*9318fe57SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmA, coordsB);CHKERRQ(ierr);
97*9318fe57SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmB, coordsA);CHKERRQ(ierr);
98*9318fe57SMatthew G. Knepley   ierr = PetscObjectDereference((PetscObject) coordsA);CHKERRQ(ierr);
99*9318fe57SMatthew G. Knepley 
100*9318fe57SMatthew G. Knepley   fieldTmp             = dmA->coordinateField;
101*9318fe57SMatthew G. Knepley   dmA->coordinateField = dmB->coordinateField;
102*9318fe57SMatthew G. Knepley   dmB->coordinateField = fieldTmp;
103*9318fe57SMatthew G. Knepley   tmp       = dmA->data;
104*9318fe57SMatthew G. Knepley   dmA->data = dmB->data;
105*9318fe57SMatthew G. Knepley   dmB->data = tmp;
106*9318fe57SMatthew G. Knepley   listTmp   = dmA->labels;
107*9318fe57SMatthew G. Knepley   dmA->labels = dmB->labels;
108*9318fe57SMatthew G. Knepley   dmB->labels = listTmp;
109*9318fe57SMatthew G. Knepley   depthTmp  = dmA->depthLabel;
110*9318fe57SMatthew G. Knepley   dmA->depthLabel = dmB->depthLabel;
111*9318fe57SMatthew G. Knepley   dmB->depthLabel = depthTmp;
112*9318fe57SMatthew G. Knepley   depthTmp  = dmA->celltypeLabel;
113*9318fe57SMatthew G. Knepley   dmA->celltypeLabel = dmB->celltypeLabel;
114*9318fe57SMatthew G. Knepley   dmB->celltypeLabel = depthTmp;
115*9318fe57SMatthew G. Knepley   tmpI         = dmA->levelup;
116*9318fe57SMatthew G. Knepley   dmA->levelup = dmB->levelup;
117*9318fe57SMatthew G. Knepley   dmB->levelup = tmpI;
118*9318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
119*9318fe57SMatthew G. Knepley }
120*9318fe57SMatthew G. Knepley 
121*9318fe57SMatthew G. Knepley static PetscErrorCode DMPlexInterpolateInPlace_Internal(DM dm)
122*9318fe57SMatthew G. Knepley {
123*9318fe57SMatthew G. Knepley   DM             idm;
124*9318fe57SMatthew G. Knepley   PetscErrorCode ierr;
125*9318fe57SMatthew G. Knepley 
126*9318fe57SMatthew G. Knepley   PetscFunctionBegin;
127*9318fe57SMatthew G. Knepley   ierr = DMPlexInterpolate(dm, &idm);CHKERRQ(ierr);
128*9318fe57SMatthew G. Knepley   ierr = DMPlexCopyCoordinates(dm, idm);CHKERRQ(ierr);
129*9318fe57SMatthew G. Knepley   ierr = DMPlexReplace_Static(dm, &idm);CHKERRQ(ierr);
130*9318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
131*9318fe57SMatthew G. Knepley }
132*9318fe57SMatthew G. Knepley 
133*9318fe57SMatthew G. Knepley /*@C
134*9318fe57SMatthew G. Knepley   DMPlexCreateCoordinateSpace - Creates a finite element space for the coordinates
135*9318fe57SMatthew G. Knepley 
136*9318fe57SMatthew G. Knepley   Collective
137*9318fe57SMatthew G. Knepley 
138*9318fe57SMatthew G. Knepley   Input Parameters:
139*9318fe57SMatthew G. Knepley + DM        - The DM
140*9318fe57SMatthew G. Knepley . degree    - The degree of the finite element
141*9318fe57SMatthew G. Knepley - coordFunc - An optional function to map new points from refinement to the surface
142*9318fe57SMatthew G. Knepley 
143*9318fe57SMatthew G. Knepley   Level: advanced
144*9318fe57SMatthew G. Knepley 
145*9318fe57SMatthew G. Knepley .seealso: PetscFECreateLagrange(), DMGetCoordinateDM()
146*9318fe57SMatthew G. Knepley @*/
147*9318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateCoordinateSpace(DM dm, PetscInt degree, PetscPointFunc coordFunc)
148*9318fe57SMatthew G. Knepley {
149*9318fe57SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
150*9318fe57SMatthew G. Knepley   DM             cdm;
151*9318fe57SMatthew G. Knepley   PetscDS        cds;
152*9318fe57SMatthew G. Knepley   PetscFE        fe;
153*9318fe57SMatthew G. Knepley   PetscClassId   id;
154*9318fe57SMatthew G. Knepley   PetscErrorCode ierr;
155*9318fe57SMatthew G. Knepley 
156*9318fe57SMatthew G. Knepley   PetscFunctionBegin;
157*9318fe57SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
158*9318fe57SMatthew G. Knepley   ierr = DMGetDS(cdm, &cds);CHKERRQ(ierr);
159*9318fe57SMatthew G. Knepley   ierr = PetscDSGetDiscretization(cds, 0, (PetscObject *) &fe);CHKERRQ(ierr);
160*9318fe57SMatthew G. Knepley   ierr = PetscObjectGetClassId((PetscObject) fe, &id);CHKERRQ(ierr);
161*9318fe57SMatthew G. Knepley   if (id != PETSCFE_CLASSID) {
162*9318fe57SMatthew G. Knepley     PetscBool simplex;
163*9318fe57SMatthew G. Knepley     PetscInt  dim, dE, qorder;
164*9318fe57SMatthew G. Knepley 
165*9318fe57SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
166*9318fe57SMatthew G. Knepley     ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
167*9318fe57SMatthew G. Knepley     ierr = DMPlexIsSimplex(dm, &simplex);CHKERRQ(ierr);
168*9318fe57SMatthew G. Knepley     qorder = degree;
169*9318fe57SMatthew G. Knepley     ierr = PetscObjectOptionsBegin((PetscObject) cdm);CHKERRQ(ierr);
170*9318fe57SMatthew G. Knepley     ierr = PetscOptionsBoundedInt("-coord_dm_default_quadrature_order", "Quadrature order is one less than quadrature points per edge", "DMPlexCreateCoordinateSpace", qorder, &qorder, NULL, 0);CHKERRQ(ierr);
171*9318fe57SMatthew G. Knepley     ierr = PetscOptionsEnd();CHKERRQ(ierr);
172*9318fe57SMatthew G. Knepley     ierr = PetscFECreateLagrange(PETSC_COMM_SELF, dim, dE, simplex, degree, qorder, &fe);CHKERRQ(ierr);
173*9318fe57SMatthew G. Knepley     ierr = DMSetField(cdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
174*9318fe57SMatthew G. Knepley     ierr = DMCreateDS(cdm);CHKERRQ(ierr);
175*9318fe57SMatthew G. Knepley     ierr = DMProjectCoordinates(dm, fe);CHKERRQ(ierr);
176*9318fe57SMatthew G. Knepley     ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
177*9318fe57SMatthew G. Knepley   }
178*9318fe57SMatthew G. Knepley   mesh->coordFunc = coordFunc;
179*9318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
180*9318fe57SMatthew G. Knepley }
181*9318fe57SMatthew G. Knepley 
1821df5d5c5SMatthew G. Knepley /*@
1831df5d5c5SMatthew G. Knepley   DMPlexCreateDoublet - Creates a mesh of two cells of the specified type, optionally with later refinement.
1841df5d5c5SMatthew G. Knepley 
185d083f849SBarry Smith   Collective
1861df5d5c5SMatthew G. Knepley 
1871df5d5c5SMatthew G. Knepley   Input Parameters:
1881df5d5c5SMatthew G. Knepley + comm - The communicator for the DM object
1891df5d5c5SMatthew G. Knepley . dim - The spatial dimension
1901df5d5c5SMatthew G. Knepley . simplex - Flag for simplicial cells, otherwise they are tensor product cells
1911df5d5c5SMatthew G. Knepley . interpolate - Flag to create intermediate mesh pieces (edges, faces)
1921df5d5c5SMatthew G. Knepley - refinementLimit - A nonzero number indicates the largest admissible volume for a refined cell
1931df5d5c5SMatthew G. Knepley 
1941df5d5c5SMatthew G. Knepley   Output Parameter:
1951df5d5c5SMatthew G. Knepley . dm - The DM object
1961df5d5c5SMatthew G. Knepley 
1971df5d5c5SMatthew G. Knepley   Level: beginner
1981df5d5c5SMatthew G. Knepley 
1991df5d5c5SMatthew G. Knepley .seealso: DMSetType(), DMCreate()
2001df5d5c5SMatthew G. Knepley @*/
2010fdc7489SMatthew Knepley PetscErrorCode DMPlexCreateDoublet(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscBool interpolate, PetscReal refinementLimit, DM *newdm)
2021df5d5c5SMatthew G. Knepley {
2031df5d5c5SMatthew G. Knepley   DM             dm;
2041df5d5c5SMatthew G. Knepley   PetscMPIInt    rank;
2051df5d5c5SMatthew G. Knepley   PetscErrorCode ierr;
2061df5d5c5SMatthew G. Knepley 
2071df5d5c5SMatthew G. Knepley   PetscFunctionBegin;
2081df5d5c5SMatthew G. Knepley   ierr = DMCreate(comm, &dm);CHKERRQ(ierr);
2091df5d5c5SMatthew G. Knepley   ierr = DMSetType(dm, DMPLEX);CHKERRQ(ierr);
210c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(dm, dim);CHKERRQ(ierr);
211ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
212ce78fa2fSMatthew G. Knepley   switch (dim) {
213ce78fa2fSMatthew G. Knepley   case 2:
214ce78fa2fSMatthew G. Knepley     if (simplex) {ierr = PetscObjectSetName((PetscObject) dm, "triangular");CHKERRQ(ierr);}
215ce78fa2fSMatthew G. Knepley     else         {ierr = PetscObjectSetName((PetscObject) dm, "quadrilateral");CHKERRQ(ierr);}
216ce78fa2fSMatthew G. Knepley     break;
217ce78fa2fSMatthew G. Knepley   case 3:
218ce78fa2fSMatthew G. Knepley     if (simplex) {ierr = PetscObjectSetName((PetscObject) dm, "tetrahedral");CHKERRQ(ierr);}
219ce78fa2fSMatthew G. Knepley     else         {ierr = PetscObjectSetName((PetscObject) dm, "hexahedral");CHKERRQ(ierr);}
220ce78fa2fSMatthew G. Knepley     break;
221ce78fa2fSMatthew G. Knepley   default:
22289c010cfSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %D", dim);
223ce78fa2fSMatthew G. Knepley   }
2241df5d5c5SMatthew G. Knepley   if (rank) {
2251df5d5c5SMatthew G. Knepley     PetscInt numPoints[2] = {0, 0};
2261df5d5c5SMatthew G. Knepley     ierr = DMPlexCreateFromDAG(dm, 1, numPoints, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
2271df5d5c5SMatthew G. Knepley   } else {
2281df5d5c5SMatthew G. Knepley     switch (dim) {
2291df5d5c5SMatthew G. Knepley     case 2:
2301df5d5c5SMatthew G. Knepley       if (simplex) {
2311df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {4, 2};
2321df5d5c5SMatthew G. Knepley         PetscInt    coneSize[6]         = {3, 3, 0, 0, 0, 0};
2331df5d5c5SMatthew G. Knepley         PetscInt    cones[6]            = {2, 3, 4,  5, 4, 3};
2341df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[6] = {0, 0, 0,  0, 0, 0};
2351df5d5c5SMatthew G. Knepley         PetscScalar vertexCoords[8]     = {-0.5, 0.5, 0.0, 0.0, 0.0, 1.0, 0.5, 0.5};
2361df5d5c5SMatthew G. Knepley 
2371df5d5c5SMatthew G. Knepley         ierr = DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2381df5d5c5SMatthew G. Knepley       } else {
2391df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {6, 2};
2401df5d5c5SMatthew G. Knepley         PetscInt    coneSize[8]         = {4, 4, 0, 0, 0, 0, 0, 0};
2411df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {2, 3, 4, 5,  3, 6, 7, 4};
2421df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0,  0, 0, 0, 0};
2431df5d5c5SMatthew 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};
2441df5d5c5SMatthew G. Knepley 
2451df5d5c5SMatthew G. Knepley         ierr = DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2461df5d5c5SMatthew G. Knepley       }
2471df5d5c5SMatthew G. Knepley       break;
2481df5d5c5SMatthew G. Knepley     case 3:
2491df5d5c5SMatthew G. Knepley       if (simplex) {
2501df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {5, 2};
2511df5d5c5SMatthew G. Knepley         PetscInt    coneSize[7]         = {4, 4, 0, 0, 0, 0, 0};
2521df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {4, 3, 5, 2,  5, 3, 4, 6};
2531df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0,  0, 0, 0, 0};
2541df5d5c5SMatthew 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};
2551df5d5c5SMatthew G. Knepley 
2561df5d5c5SMatthew G. Knepley         ierr = DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2571df5d5c5SMatthew G. Knepley       } else {
2581df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]         = {12, 2};
2591df5d5c5SMatthew G. Knepley         PetscInt    coneSize[14]         = {8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
2601df5d5c5SMatthew G. Knepley         PetscInt    cones[16]            = {2, 3, 4, 5, 6, 7, 8, 9,  5, 4, 10, 11, 7, 12, 13, 8};
2611df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[16] = {0, 0, 0, 0, 0, 0, 0, 0,  0, 0,  0,  0, 0,  0,  0, 0};
2621df5d5c5SMatthew 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,
2631df5d5c5SMatthew 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,
2641df5d5c5SMatthew 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};
2651df5d5c5SMatthew G. Knepley 
2661df5d5c5SMatthew G. Knepley         ierr = DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2671df5d5c5SMatthew G. Knepley       }
2681df5d5c5SMatthew G. Knepley       break;
2691df5d5c5SMatthew G. Knepley     default:
27089c010cfSBarry Smith       SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %D", dim);
2711df5d5c5SMatthew G. Knepley     }
2721df5d5c5SMatthew G. Knepley   }
2731df5d5c5SMatthew G. Knepley   *newdm = dm;
2741df5d5c5SMatthew G. Knepley   if (refinementLimit > 0.0) {
2751df5d5c5SMatthew G. Knepley     DM rdm;
2761df5d5c5SMatthew G. Knepley     const char *name;
2771df5d5c5SMatthew G. Knepley 
2781df5d5c5SMatthew G. Knepley     ierr = DMPlexSetRefinementUniform(*newdm, PETSC_FALSE);CHKERRQ(ierr);
2791df5d5c5SMatthew G. Knepley     ierr = DMPlexSetRefinementLimit(*newdm, refinementLimit);CHKERRQ(ierr);
2801df5d5c5SMatthew G. Knepley     ierr = DMRefine(*newdm, comm, &rdm);CHKERRQ(ierr);
2811df5d5c5SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) *newdm, &name);CHKERRQ(ierr);
2821df5d5c5SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject)    rdm,  name);CHKERRQ(ierr);
2831df5d5c5SMatthew G. Knepley     ierr = DMDestroy(newdm);CHKERRQ(ierr);
2841df5d5c5SMatthew G. Knepley     *newdm = rdm;
2851df5d5c5SMatthew G. Knepley   }
2861df5d5c5SMatthew G. Knepley   if (interpolate) {
2875fd9971aSMatthew G. Knepley     DM idm;
2881df5d5c5SMatthew G. Knepley 
2891df5d5c5SMatthew G. Knepley     ierr = DMPlexInterpolate(*newdm, &idm);CHKERRQ(ierr);
2901df5d5c5SMatthew G. Knepley     ierr = DMDestroy(newdm);CHKERRQ(ierr);
2911df5d5c5SMatthew G. Knepley     *newdm = idm;
2921df5d5c5SMatthew G. Knepley   }
2931df5d5c5SMatthew G. Knepley   PetscFunctionReturn(0);
2941df5d5c5SMatthew G. Knepley }
2951df5d5c5SMatthew G. Knepley 
296*9318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
297*9318fe57SMatthew G. Knepley {
298*9318fe57SMatthew G. Knepley   const PetscInt numVertices    = 2;
299*9318fe57SMatthew G. Knepley   PetscInt       markerRight    = 1;
300*9318fe57SMatthew G. Knepley   PetscInt       markerLeft     = 1;
301*9318fe57SMatthew G. Knepley   PetscBool      markerSeparate = PETSC_FALSE;
302*9318fe57SMatthew G. Knepley   Vec            coordinates;
303*9318fe57SMatthew G. Knepley   PetscSection   coordSection;
304*9318fe57SMatthew G. Knepley   PetscScalar   *coords;
305*9318fe57SMatthew G. Knepley   PetscInt       coordSize;
306*9318fe57SMatthew G. Knepley   PetscMPIInt    rank;
307*9318fe57SMatthew G. Knepley   PetscInt       cdim = 1, v;
308*9318fe57SMatthew G. Knepley   PetscErrorCode ierr;
309552f7358SJed Brown 
310*9318fe57SMatthew G. Knepley   PetscFunctionBegin;
311*9318fe57SMatthew G. Knepley   ierr = PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);CHKERRQ(ierr);
312*9318fe57SMatthew G. Knepley   if (markerSeparate) {
313*9318fe57SMatthew G. Knepley     markerRight  = 2;
314*9318fe57SMatthew G. Knepley     markerLeft   = 1;
315*9318fe57SMatthew G. Knepley   }
316*9318fe57SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRMPI(ierr);
317*9318fe57SMatthew G. Knepley   if (!rank) {
318*9318fe57SMatthew G. Knepley     ierr = DMPlexSetChart(dm, 0, numVertices);CHKERRQ(ierr);
319*9318fe57SMatthew G. Knepley     ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
320*9318fe57SMatthew G. Knepley     ierr = DMSetLabelValue(dm, "marker", 0, markerLeft);CHKERRQ(ierr);
321*9318fe57SMatthew G. Knepley     ierr = DMSetLabelValue(dm, "marker", 1, markerRight);CHKERRQ(ierr);
322*9318fe57SMatthew G. Knepley   }
323*9318fe57SMatthew G. Knepley   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
324*9318fe57SMatthew G. Knepley   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
325*9318fe57SMatthew G. Knepley   /* Build coordinates */
326*9318fe57SMatthew G. Knepley   ierr = DMSetCoordinateDim(dm, cdim);CHKERRQ(ierr);
327*9318fe57SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
328*9318fe57SMatthew G. Knepley   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
329*9318fe57SMatthew G. Knepley   ierr = PetscSectionSetChart(coordSection, 0, numVertices);CHKERRQ(ierr);
330*9318fe57SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(coordSection, 0, cdim);CHKERRQ(ierr);
331*9318fe57SMatthew G. Knepley   for (v = 0; v < numVertices; ++v) {
332*9318fe57SMatthew G. Knepley     ierr = PetscSectionSetDof(coordSection, v, cdim);CHKERRQ(ierr);
333*9318fe57SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(coordSection, v, 0, cdim);CHKERRQ(ierr);
334*9318fe57SMatthew G. Knepley   }
335*9318fe57SMatthew G. Knepley   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
336*9318fe57SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
337*9318fe57SMatthew G. Knepley   ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
338*9318fe57SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
339*9318fe57SMatthew G. Knepley   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
340*9318fe57SMatthew G. Knepley   ierr = VecSetBlockSize(coordinates, cdim);CHKERRQ(ierr);
341*9318fe57SMatthew G. Knepley   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
342*9318fe57SMatthew G. Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
343*9318fe57SMatthew G. Knepley   coords[0] = lower[0];
344*9318fe57SMatthew G. Knepley   coords[1] = upper[0];
345*9318fe57SMatthew G. Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
346*9318fe57SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
347*9318fe57SMatthew G. Knepley   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
348*9318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
349*9318fe57SMatthew G. Knepley }
35026492d91SMatthew G. Knepley 
351*9318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
352552f7358SJed Brown {
3531df21d24SMatthew G. Knepley   const PetscInt numVertices    = (edges[0]+1)*(edges[1]+1);
3541df21d24SMatthew G. Knepley   const PetscInt numEdges       = edges[0]*(edges[1]+1) + (edges[0]+1)*edges[1];
355552f7358SJed Brown   PetscInt       markerTop      = 1;
356552f7358SJed Brown   PetscInt       markerBottom   = 1;
357552f7358SJed Brown   PetscInt       markerRight    = 1;
358552f7358SJed Brown   PetscInt       markerLeft     = 1;
359552f7358SJed Brown   PetscBool      markerSeparate = PETSC_FALSE;
360552f7358SJed Brown   Vec            coordinates;
361552f7358SJed Brown   PetscSection   coordSection;
362552f7358SJed Brown   PetscScalar    *coords;
363552f7358SJed Brown   PetscInt       coordSize;
364552f7358SJed Brown   PetscMPIInt    rank;
365552f7358SJed Brown   PetscInt       v, vx, vy;
366552f7358SJed Brown   PetscErrorCode ierr;
367552f7358SJed Brown 
368552f7358SJed Brown   PetscFunctionBegin;
369c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);CHKERRQ(ierr);
370552f7358SJed Brown   if (markerSeparate) {
3711df21d24SMatthew G. Knepley     markerTop    = 3;
3721df21d24SMatthew G. Knepley     markerBottom = 1;
3731df21d24SMatthew G. Knepley     markerRight  = 2;
3741df21d24SMatthew G. Knepley     markerLeft   = 4;
375552f7358SJed Brown   }
376ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRMPI(ierr);
377552f7358SJed Brown   if (!rank) {
378552f7358SJed Brown     PetscInt e, ex, ey;
379552f7358SJed Brown 
380552f7358SJed Brown     ierr = DMPlexSetChart(dm, 0, numEdges+numVertices);CHKERRQ(ierr);
381552f7358SJed Brown     for (e = 0; e < numEdges; ++e) {
382552f7358SJed Brown       ierr = DMPlexSetConeSize(dm, e, 2);CHKERRQ(ierr);
383552f7358SJed Brown     }
384552f7358SJed Brown     ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
385552f7358SJed Brown     for (vx = 0; vx <= edges[0]; vx++) {
386552f7358SJed Brown       for (ey = 0; ey < edges[1]; ey++) {
387552f7358SJed Brown         PetscInt edge   = vx*edges[1] + ey + edges[0]*(edges[1]+1);
388552f7358SJed Brown         PetscInt vertex = ey*(edges[0]+1) + vx + numEdges;
389da80777bSKarl Rupp         PetscInt cone[2];
390552f7358SJed Brown 
391da80777bSKarl Rupp         cone[0] = vertex; cone[1] = vertex+edges[0]+1;
392552f7358SJed Brown         ierr    = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr);
393552f7358SJed Brown         if (vx == edges[0]) {
394c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", edge,    markerRight);CHKERRQ(ierr);
395c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", cone[0], markerRight);CHKERRQ(ierr);
396552f7358SJed Brown           if (ey == edges[1]-1) {
397c58f1c22SToby Isaac             ierr = DMSetLabelValue(dm, "marker", cone[1], markerRight);CHKERRQ(ierr);
398c668006fSMatthew G. Knepley             ierr = DMSetLabelValue(dm, "Face Sets", cone[1], markerRight);CHKERRQ(ierr);
399552f7358SJed Brown           }
400552f7358SJed Brown         } else if (vx == 0) {
401c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", edge,    markerLeft);CHKERRQ(ierr);
402c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", cone[0], markerLeft);CHKERRQ(ierr);
403552f7358SJed Brown           if (ey == edges[1]-1) {
404c58f1c22SToby Isaac             ierr = DMSetLabelValue(dm, "marker", cone[1], markerLeft);CHKERRQ(ierr);
405c668006fSMatthew G. Knepley             ierr = DMSetLabelValue(dm, "Face Sets", cone[1], markerLeft);CHKERRQ(ierr);
406552f7358SJed Brown           }
407552f7358SJed Brown         }
408552f7358SJed Brown       }
409552f7358SJed Brown     }
410552f7358SJed Brown     for (vy = 0; vy <= edges[1]; vy++) {
411552f7358SJed Brown       for (ex = 0; ex < edges[0]; ex++) {
412552f7358SJed Brown         PetscInt edge   = vy*edges[0]     + ex;
413552f7358SJed Brown         PetscInt vertex = vy*(edges[0]+1) + ex + numEdges;
414da80777bSKarl Rupp         PetscInt cone[2];
415552f7358SJed Brown 
416da80777bSKarl Rupp         cone[0] = vertex; cone[1] = vertex+1;
417552f7358SJed Brown         ierr    = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr);
418552f7358SJed Brown         if (vy == edges[1]) {
419c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", edge,    markerTop);CHKERRQ(ierr);
420c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", cone[0], markerTop);CHKERRQ(ierr);
421552f7358SJed Brown           if (ex == edges[0]-1) {
422c58f1c22SToby Isaac             ierr = DMSetLabelValue(dm, "marker", cone[1], markerTop);CHKERRQ(ierr);
423c668006fSMatthew G. Knepley             ierr = DMSetLabelValue(dm, "Face Sets", cone[1], markerTop);CHKERRQ(ierr);
424552f7358SJed Brown           }
425552f7358SJed Brown         } else if (vy == 0) {
426c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", edge,    markerBottom);CHKERRQ(ierr);
427c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", cone[0], markerBottom);CHKERRQ(ierr);
428552f7358SJed Brown           if (ex == edges[0]-1) {
429c58f1c22SToby Isaac             ierr = DMSetLabelValue(dm, "marker", cone[1], markerBottom);CHKERRQ(ierr);
430c668006fSMatthew G. Knepley             ierr = DMSetLabelValue(dm, "Face Sets", cone[1], markerBottom);CHKERRQ(ierr);
431552f7358SJed Brown           }
432552f7358SJed Brown         }
433552f7358SJed Brown       }
434552f7358SJed Brown     }
435552f7358SJed Brown   }
436552f7358SJed Brown   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
437552f7358SJed Brown   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
438552f7358SJed Brown   /* Build coordinates */
4399596c6baSMatthew G. Knepley   ierr = DMSetCoordinateDim(dm, 2);CHKERRQ(ierr);
440c2166f76SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
441972bc18aSToby Isaac   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
442552f7358SJed Brown   ierr = PetscSectionSetChart(coordSection, numEdges, numEdges + numVertices);CHKERRQ(ierr);
443972bc18aSToby Isaac   ierr = PetscSectionSetFieldComponents(coordSection, 0, 2);CHKERRQ(ierr);
444552f7358SJed Brown   for (v = numEdges; v < numEdges+numVertices; ++v) {
445552f7358SJed Brown     ierr = PetscSectionSetDof(coordSection, v, 2);CHKERRQ(ierr);
446972bc18aSToby Isaac     ierr = PetscSectionSetFieldDof(coordSection, v, 0, 2);CHKERRQ(ierr);
447552f7358SJed Brown   }
448552f7358SJed Brown   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
449552f7358SJed Brown   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
4508b9ced59SLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
451da16285aSMichael Lange   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
452552f7358SJed Brown   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
4538b9ced59SLisandro Dalcin   ierr = VecSetBlockSize(coordinates, 2);CHKERRQ(ierr);
4542eb5907fSJed Brown   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
455552f7358SJed Brown   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
456552f7358SJed Brown   for (vy = 0; vy <= edges[1]; ++vy) {
457552f7358SJed Brown     for (vx = 0; vx <= edges[0]; ++vx) {
458552f7358SJed Brown       coords[(vy*(edges[0]+1)+vx)*2+0] = lower[0] + ((upper[0] - lower[0])/edges[0])*vx;
459552f7358SJed Brown       coords[(vy*(edges[0]+1)+vx)*2+1] = lower[1] + ((upper[1] - lower[1])/edges[1])*vy;
460552f7358SJed Brown     }
461552f7358SJed Brown   }
462552f7358SJed Brown   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
463552f7358SJed Brown   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
464552f7358SJed Brown   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
465552f7358SJed Brown   PetscFunctionReturn(0);
466552f7358SJed Brown }
467552f7358SJed Brown 
468*9318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt faces[])
469552f7358SJed Brown {
4709e8abbc3SMichael Lange   PetscInt       vertices[3], numVertices;
4717b59f5a9SMichael Lange   PetscInt       numFaces    = 2*faces[0]*faces[1] + 2*faces[1]*faces[2] + 2*faces[0]*faces[2];
472552f7358SJed Brown   Vec            coordinates;
473552f7358SJed Brown   PetscSection   coordSection;
474552f7358SJed Brown   PetscScalar    *coords;
475552f7358SJed Brown   PetscInt       coordSize;
476552f7358SJed Brown   PetscMPIInt    rank;
477552f7358SJed Brown   PetscInt       v, vx, vy, vz;
4787b59f5a9SMichael Lange   PetscInt       voffset, iface=0, cone[4];
479552f7358SJed Brown   PetscErrorCode ierr;
480552f7358SJed Brown 
481552f7358SJed Brown   PetscFunctionBegin;
48282f516ccSBarry 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");
483ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRMPI(ierr);
4849e8abbc3SMichael Lange   vertices[0] = faces[0]+1; vertices[1] = faces[1]+1; vertices[2] = faces[2]+1;
4859e8abbc3SMichael Lange   numVertices = vertices[0]*vertices[1]*vertices[2];
486552f7358SJed Brown   if (!rank) {
487552f7358SJed Brown     PetscInt f;
488552f7358SJed Brown 
489552f7358SJed Brown     ierr = DMPlexSetChart(dm, 0, numFaces+numVertices);CHKERRQ(ierr);
490552f7358SJed Brown     for (f = 0; f < numFaces; ++f) {
491552f7358SJed Brown       ierr = DMPlexSetConeSize(dm, f, 4);CHKERRQ(ierr);
492552f7358SJed Brown     }
493552f7358SJed Brown     ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
4947b59f5a9SMichael Lange 
4957b59f5a9SMichael Lange     /* Side 0 (Top) */
4967b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
4977b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
4987b59f5a9SMichael Lange         voffset = numFaces + vertices[0]*vertices[1]*(vertices[2]-1) + vy*vertices[0] + vx;
4997b59f5a9SMichael Lange         cone[0] = voffset; cone[1] = voffset+1; cone[2] = voffset+vertices[0]+1; cone[3] = voffset+vertices[0];
5007b59f5a9SMichael Lange         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
501c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", iface, 1);CHKERRQ(ierr);
502c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+0, 1);CHKERRQ(ierr);
503c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+1, 1);CHKERRQ(ierr);
504c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, 1);CHKERRQ(ierr);
505c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]+1, 1);CHKERRQ(ierr);
5067b59f5a9SMichael Lange         iface++;
507552f7358SJed Brown       }
508552f7358SJed Brown     }
5097b59f5a9SMichael Lange 
5107b59f5a9SMichael Lange     /* Side 1 (Bottom) */
5117b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
5127b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5137b59f5a9SMichael Lange         voffset = numFaces + vy*(faces[0]+1) + vx;
5147b59f5a9SMichael Lange         cone[0] = voffset+1; cone[1] = voffset; cone[2] = voffset+vertices[0]; cone[3] = voffset+vertices[0]+1;
5157b59f5a9SMichael Lange         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
516c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", iface, 1);CHKERRQ(ierr);
517c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+0, 1);CHKERRQ(ierr);
518c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+1, 1);CHKERRQ(ierr);
519c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, 1);CHKERRQ(ierr);
520c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]+1, 1);CHKERRQ(ierr);
5217b59f5a9SMichael Lange         iface++;
522552f7358SJed Brown       }
523552f7358SJed Brown     }
5247b59f5a9SMichael Lange 
5257b59f5a9SMichael Lange     /* Side 2 (Front) */
5267b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
5277b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5287b59f5a9SMichael Lange         voffset = numFaces + vz*vertices[0]*vertices[1] + vx;
5297b59f5a9SMichael Lange         cone[0] = voffset; cone[1] = voffset+1; cone[2] = voffset+vertices[0]*vertices[1]+1; cone[3] = voffset+vertices[0]*vertices[1];
5307b59f5a9SMichael Lange         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
531c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", iface, 1);CHKERRQ(ierr);
532c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+0, 1);CHKERRQ(ierr);
533c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+1, 1);CHKERRQ(ierr);
534c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+0, 1);CHKERRQ(ierr);
535c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+1, 1);CHKERRQ(ierr);
5367b59f5a9SMichael Lange         iface++;
537552f7358SJed Brown       }
5387b59f5a9SMichael Lange     }
5397b59f5a9SMichael Lange 
5407b59f5a9SMichael Lange     /* Side 3 (Back) */
5417b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
5427b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5437b59f5a9SMichael Lange         voffset = numFaces + vz*vertices[0]*vertices[1] + vertices[0]*(vertices[1]-1) + vx;
5447b59f5a9SMichael Lange         cone[0] = voffset+vertices[0]*vertices[1]; cone[1] = voffset+vertices[0]*vertices[1]+1;
5457b59f5a9SMichael Lange         cone[2] = voffset+1; cone[3] = voffset;
5467b59f5a9SMichael Lange         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
547c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", iface, 1);CHKERRQ(ierr);
548c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+0, 1);CHKERRQ(ierr);
549c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+1, 1);CHKERRQ(ierr);
550c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+0, 1);CHKERRQ(ierr);
551c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+1, 1);CHKERRQ(ierr);
5527b59f5a9SMichael Lange         iface++;
5537b59f5a9SMichael Lange       }
5547b59f5a9SMichael Lange     }
5557b59f5a9SMichael Lange 
5567b59f5a9SMichael Lange     /* Side 4 (Left) */
5577b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
5587b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
5597b59f5a9SMichael Lange         voffset = numFaces + vz*vertices[0]*vertices[1] + vy*vertices[0];
5607b59f5a9SMichael Lange         cone[0] = voffset; cone[1] = voffset+vertices[0]*vertices[1];
5617b59f5a9SMichael Lange         cone[2] = voffset+vertices[0]*vertices[1]+vertices[0]; cone[3] = voffset+vertices[0];
5627b59f5a9SMichael Lange         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
563c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", iface, 1);CHKERRQ(ierr);
564c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+0, 1);CHKERRQ(ierr);
565c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, 1);CHKERRQ(ierr);
566c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[1]+0, 1);CHKERRQ(ierr);
567c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+vertices[0], 1);CHKERRQ(ierr);
5687b59f5a9SMichael Lange         iface++;
5697b59f5a9SMichael Lange       }
5707b59f5a9SMichael Lange     }
5717b59f5a9SMichael Lange 
5727b59f5a9SMichael Lange     /* Side 5 (Right) */
5737b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
5747b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
575aab5bcd8SJed Brown         voffset = numFaces + vz*vertices[0]*vertices[1] + vy*vertices[0] + faces[0];
5767b59f5a9SMichael Lange         cone[0] = voffset+vertices[0]*vertices[1]; cone[1] = voffset;
5777b59f5a9SMichael Lange         cone[2] = voffset+vertices[0]; cone[3] = voffset+vertices[0]*vertices[1]+vertices[0];
5787b59f5a9SMichael Lange         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
579c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", iface, 1);CHKERRQ(ierr);
580c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+0, 1);CHKERRQ(ierr);
581c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, 1);CHKERRQ(ierr);
582c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+0, 1);CHKERRQ(ierr);
583c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+vertices[0], 1);CHKERRQ(ierr);
5847b59f5a9SMichael Lange         iface++;
5857b59f5a9SMichael Lange       }
586552f7358SJed Brown     }
587552f7358SJed Brown   }
588552f7358SJed Brown   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
589552f7358SJed Brown   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
590552f7358SJed Brown   /* Build coordinates */
5919596c6baSMatthew G. Knepley   ierr = DMSetCoordinateDim(dm, 3);CHKERRQ(ierr);
592c2166f76SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
593*9318fe57SMatthew G. Knepley   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
594552f7358SJed Brown   ierr = PetscSectionSetChart(coordSection, numFaces, numFaces + numVertices);CHKERRQ(ierr);
595*9318fe57SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(coordSection, 0, 3);CHKERRQ(ierr);
596552f7358SJed Brown   for (v = numFaces; v < numFaces+numVertices; ++v) {
597552f7358SJed Brown     ierr = PetscSectionSetDof(coordSection, v, 3);CHKERRQ(ierr);
598*9318fe57SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(coordSection, v, 0, 3);CHKERRQ(ierr);
599552f7358SJed Brown   }
600552f7358SJed Brown   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
601552f7358SJed Brown   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
6028b9ced59SLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
603552f7358SJed Brown   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
604552f7358SJed Brown   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
6058b9ced59SLisandro Dalcin   ierr = VecSetBlockSize(coordinates, 3);CHKERRQ(ierr);
6062eb5907fSJed Brown   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
607552f7358SJed Brown   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
608552f7358SJed Brown   for (vz = 0; vz <= faces[2]; ++vz) {
609552f7358SJed Brown     for (vy = 0; vy <= faces[1]; ++vy) {
610552f7358SJed Brown       for (vx = 0; vx <= faces[0]; ++vx) {
611552f7358SJed Brown         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+0] = lower[0] + ((upper[0] - lower[0])/faces[0])*vx;
612552f7358SJed Brown         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+1] = lower[1] + ((upper[1] - lower[1])/faces[1])*vy;
613552f7358SJed Brown         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+2] = lower[2] + ((upper[2] - lower[2])/faces[2])*vz;
614552f7358SJed Brown       }
615552f7358SJed Brown     }
616552f7358SJed Brown   }
617552f7358SJed Brown   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
618552f7358SJed Brown   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
619552f7358SJed Brown   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
620552f7358SJed Brown   PetscFunctionReturn(0);
621552f7358SJed Brown }
622552f7358SJed Brown 
623*9318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate)
624*9318fe57SMatthew G. Knepley {
625*9318fe57SMatthew G. Knepley   PetscErrorCode ierr;
626*9318fe57SMatthew G. Knepley 
627*9318fe57SMatthew G. Knepley   PetscFunctionBegin;
628*9318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
629*9318fe57SMatthew G. Knepley   ierr = DMSetDimension(dm, dim-1);CHKERRQ(ierr);
630*9318fe57SMatthew G. Knepley   ierr = DMSetCoordinateDim(dm, dim);CHKERRQ(ierr);
631*9318fe57SMatthew G. Knepley   switch (dim) {
632*9318fe57SMatthew G. Knepley     case 1: ierr = DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(dm, lower, upper, faces);CHKERRQ(ierr);break;
633*9318fe57SMatthew G. Knepley     case 2: ierr = DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(dm, lower, upper, faces);CHKERRQ(ierr);break;
634*9318fe57SMatthew G. Knepley     case 3: ierr = DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(dm, lower, upper, faces);CHKERRQ(ierr);break;
635*9318fe57SMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Dimension not supported: %D", dim);
636*9318fe57SMatthew G. Knepley   }
637*9318fe57SMatthew G. Knepley   if (interpolate) {ierr = DMPlexInterpolateInPlace_Internal(dm);CHKERRQ(ierr);}
638*9318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
639*9318fe57SMatthew G. Knepley }
640*9318fe57SMatthew G. Knepley 
641*9318fe57SMatthew G. Knepley /*@C
642*9318fe57SMatthew G. Knepley   DMPlexCreateBoxSurfaceMesh - Creates a mesh on the surface of the tensor product of unit intervals (box) using tensor cells (hexahedra).
643*9318fe57SMatthew G. Knepley 
644*9318fe57SMatthew G. Knepley   Collective
645*9318fe57SMatthew G. Knepley 
646*9318fe57SMatthew G. Knepley   Input Parameters:
647*9318fe57SMatthew G. Knepley + comm        - The communicator for the DM object
648*9318fe57SMatthew G. Knepley . dim         - The spatial dimension of the box, so the resulting mesh is has dimension dim-1
649*9318fe57SMatthew G. Knepley . faces       - Number of faces per dimension, or NULL for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
650*9318fe57SMatthew G. Knepley . lower       - The lower left corner, or NULL for (0, 0, 0)
651*9318fe57SMatthew G. Knepley . upper       - The upper right corner, or NULL for (1, 1, 1)
652*9318fe57SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
653*9318fe57SMatthew G. Knepley 
654*9318fe57SMatthew G. Knepley   Output Parameter:
655*9318fe57SMatthew G. Knepley . dm  - The DM object
656*9318fe57SMatthew G. Knepley 
657*9318fe57SMatthew G. Knepley   Level: beginner
658*9318fe57SMatthew G. Knepley 
659*9318fe57SMatthew G. Knepley .seealso: DMSetFromOptions(), DMPlexCreateBoxMesh(), DMPlexCreateFromFile(), DMSetType(), DMCreate()
660*9318fe57SMatthew G. Knepley @*/
661*9318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateBoxSurfaceMesh(MPI_Comm comm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate, DM *dm)
662*9318fe57SMatthew G. Knepley {
663*9318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
664*9318fe57SMatthew G. Knepley   PetscReal      low[3] = {0, 0, 0};
665*9318fe57SMatthew G. Knepley   PetscReal      upp[3] = {1, 1, 1};
666*9318fe57SMatthew G. Knepley   PetscErrorCode ierr;
667*9318fe57SMatthew G. Knepley 
668*9318fe57SMatthew G. Knepley   PetscFunctionBegin;
669*9318fe57SMatthew G. Knepley   ierr = DMCreate(comm,dm);CHKERRQ(ierr);
670*9318fe57SMatthew G. Knepley   ierr = DMSetType(*dm,DMPLEX);CHKERRQ(ierr);
671*9318fe57SMatthew G. Knepley   ierr = DMPlexCreateBoxSurfaceMesh_Internal(*dm, dim, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, interpolate);CHKERRQ(ierr);
672*9318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
673*9318fe57SMatthew G. Knepley }
674*9318fe57SMatthew G. Knepley 
675*9318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateLineMesh_Internal(DM dm,PetscInt segments,PetscReal lower,PetscReal upper,DMBoundaryType bd)
676fdbf62faSLisandro Dalcin {
677fdbf62faSLisandro Dalcin   PetscInt       i,fStart,fEnd,numCells = 0,numVerts = 0;
678fdbf62faSLisandro Dalcin   PetscInt       numPoints[2],*coneSize,*cones,*coneOrientations;
679fdbf62faSLisandro Dalcin   PetscScalar    *vertexCoords;
680fdbf62faSLisandro Dalcin   PetscReal      L,maxCell;
681fdbf62faSLisandro Dalcin   PetscBool      markerSeparate = PETSC_FALSE;
682fdbf62faSLisandro Dalcin   PetscInt       markerLeft  = 1, faceMarkerLeft  = 1;
683fdbf62faSLisandro Dalcin   PetscInt       markerRight = 1, faceMarkerRight = 2;
684fdbf62faSLisandro Dalcin   PetscBool      wrap = (bd == DM_BOUNDARY_PERIODIC || bd == DM_BOUNDARY_TWIST) ? PETSC_TRUE : PETSC_FALSE;
685fdbf62faSLisandro Dalcin   PetscMPIInt    rank;
686fdbf62faSLisandro Dalcin   PetscErrorCode ierr;
687fdbf62faSLisandro Dalcin 
688fdbf62faSLisandro Dalcin   PetscFunctionBegin;
689*9318fe57SMatthew G. Knepley   PetscValidPointer(dm,1);
690fdbf62faSLisandro Dalcin 
691*9318fe57SMatthew G. Knepley   ierr = DMSetDimension(dm,1);CHKERRQ(ierr);
692*9318fe57SMatthew G. Knepley   ierr = DMCreateLabel(dm,"marker");CHKERRQ(ierr);
693*9318fe57SMatthew G. Knepley   ierr = DMCreateLabel(dm,"Face Sets");CHKERRQ(ierr);
694fdbf62faSLisandro Dalcin 
695*9318fe57SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm),&rank);CHKERRMPI(ierr);
696fdbf62faSLisandro Dalcin   if (!rank) numCells = segments;
697fdbf62faSLisandro Dalcin   if (!rank) numVerts = segments + (wrap ? 0 : 1);
698fdbf62faSLisandro Dalcin 
699fdbf62faSLisandro Dalcin   numPoints[0] = numVerts ; numPoints[1] = numCells;
700fdbf62faSLisandro Dalcin   ierr = PetscMalloc4(numCells+numVerts,&coneSize,numCells*2,&cones,numCells+numVerts,&coneOrientations,numVerts,&vertexCoords);CHKERRQ(ierr);
701580bdb30SBarry Smith   ierr = PetscArrayzero(coneOrientations,numCells+numVerts);CHKERRQ(ierr);
702fdbf62faSLisandro Dalcin   for (i = 0; i < numCells; ++i) { coneSize[i] = 2; }
703fdbf62faSLisandro Dalcin   for (i = 0; i < numVerts; ++i) { coneSize[numCells+i] = 0; }
704fdbf62faSLisandro Dalcin   for (i = 0; i < numCells; ++i) { cones[2*i] = numCells + i%numVerts; cones[2*i+1] = numCells + (i+1)%numVerts; }
705fdbf62faSLisandro Dalcin   for (i = 0; i < numVerts; ++i) { vertexCoords[i] = lower + (upper-lower)*((PetscReal)i/(PetscReal)numCells); }
706*9318fe57SMatthew G. Knepley   ierr = DMPlexCreateFromDAG(dm,1,numPoints,coneSize,cones,coneOrientations,vertexCoords);CHKERRQ(ierr);
707fdbf62faSLisandro Dalcin   ierr = PetscFree4(coneSize,cones,coneOrientations,vertexCoords);CHKERRQ(ierr);
708fdbf62faSLisandro Dalcin 
709*9318fe57SMatthew G. Knepley   ierr = PetscOptionsGetBool(((PetscObject)dm)->options,((PetscObject)dm)->prefix,"-dm_plex_separate_marker",&markerSeparate,NULL);CHKERRQ(ierr);
710fdbf62faSLisandro Dalcin   if (markerSeparate) { markerLeft = faceMarkerLeft; markerRight = faceMarkerRight;}
711fdbf62faSLisandro Dalcin   if (!wrap && !rank) {
712*9318fe57SMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm,1,&fStart,&fEnd);CHKERRQ(ierr);
713*9318fe57SMatthew G. Knepley     ierr = DMSetLabelValue(dm,"marker",fStart,markerLeft);CHKERRQ(ierr);
714*9318fe57SMatthew G. Knepley     ierr = DMSetLabelValue(dm,"marker",fEnd-1,markerRight);CHKERRQ(ierr);
715*9318fe57SMatthew G. Knepley     ierr = DMSetLabelValue(dm,"Face Sets",fStart,faceMarkerLeft);CHKERRQ(ierr);
716*9318fe57SMatthew G. Knepley     ierr = DMSetLabelValue(dm,"Face Sets",fEnd-1,faceMarkerRight);CHKERRQ(ierr);
717fdbf62faSLisandro Dalcin   }
718fdbf62faSLisandro Dalcin   if (wrap) {
719fdbf62faSLisandro Dalcin     L       = upper - lower;
720fdbf62faSLisandro Dalcin     maxCell = (PetscReal)1.1*(L/(PetscReal)PetscMax(1,segments));
721*9318fe57SMatthew G. Knepley     ierr = DMSetPeriodicity(dm,PETSC_TRUE,&maxCell,&L,&bd);CHKERRQ(ierr);
722fdbf62faSLisandro Dalcin   }
723*9318fe57SMatthew G. Knepley   ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE);CHKERRQ(ierr);
724fdbf62faSLisandro Dalcin   PetscFunctionReturn(0);
725fdbf62faSLisandro Dalcin }
726fdbf62faSLisandro Dalcin 
727*9318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxMesh_Simplex_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate)
728d6218766SMatthew G. Knepley {
729*9318fe57SMatthew G. Knepley   DM             boundary, vol;
730768d5fceSMatthew G. Knepley   PetscInt       i;
731d6218766SMatthew G. Knepley   PetscErrorCode ierr;
732d6218766SMatthew G. Knepley 
733d6218766SMatthew G. Knepley   PetscFunctionBegin;
734*9318fe57SMatthew G. Knepley   PetscValidPointer(dm, 1);
735*9318fe57SMatthew G. Knepley   for (i = 0; i < dim; ++i) if (periodicity[i] != DM_BOUNDARY_NONE) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Periodicity is not supported for simplex meshes");
736*9318fe57SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), &boundary);CHKERRQ(ierr);
737d6218766SMatthew G. Knepley   ierr = DMSetType(boundary, DMPLEX);CHKERRQ(ierr);
738*9318fe57SMatthew G. Knepley   ierr = DMPlexCreateBoxSurfaceMesh_Internal(boundary, dim, faces, lower, upper, PETSC_FALSE);CHKERRQ(ierr);
739*9318fe57SMatthew G. Knepley   ierr = DMPlexGenerate(boundary, NULL, interpolate, &vol);CHKERRQ(ierr);
740*9318fe57SMatthew G. Knepley   ierr = DMPlexReplace_Static(dm, &vol);CHKERRQ(ierr);
741d6218766SMatthew G. Knepley   ierr = DMDestroy(&boundary);CHKERRQ(ierr);
742d6218766SMatthew G. Knepley   PetscFunctionReturn(0);
743d6218766SMatthew G. Knepley }
744d6218766SMatthew G. Knepley 
7453dfda0b1SToby Isaac static PetscErrorCode DMPlexCreateCubeMesh_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY, DMBoundaryType bdZ)
7463dfda0b1SToby Isaac {
747ed0e4b50SMatthew G. Knepley   DMLabel        cutLabel = NULL;
748f4eb4c5dSMatthew G. Knepley   PetscInt       markerTop      = 1, faceMarkerTop      = 1;
749f4eb4c5dSMatthew G. Knepley   PetscInt       markerBottom   = 1, faceMarkerBottom   = 1;
750f4eb4c5dSMatthew G. Knepley   PetscInt       markerFront    = 1, faceMarkerFront    = 1;
751f4eb4c5dSMatthew G. Knepley   PetscInt       markerBack     = 1, faceMarkerBack     = 1;
752f4eb4c5dSMatthew G. Knepley   PetscInt       markerRight    = 1, faceMarkerRight    = 1;
753f4eb4c5dSMatthew G. Knepley   PetscInt       markerLeft     = 1, faceMarkerLeft     = 1;
7543dfda0b1SToby Isaac   PetscInt       dim;
755d8211ee3SMatthew G. Knepley   PetscBool      markerSeparate = PETSC_FALSE, cutMarker = PETSC_FALSE;
7563dfda0b1SToby Isaac   PetscMPIInt    rank;
7573dfda0b1SToby Isaac   PetscErrorCode ierr;
7583dfda0b1SToby Isaac 
7593dfda0b1SToby Isaac   PetscFunctionBegin;
760f0226e14SMatthew G. Knepley   ierr = DMGetDimension(dm,&dim);CHKERRQ(ierr);
761ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRMPI(ierr);
76250ae33c3SToby Isaac   ierr = DMCreateLabel(dm,"marker");CHKERRQ(ierr);
76350ae33c3SToby Isaac   ierr = DMCreateLabel(dm,"Face Sets");CHKERRQ(ierr);
7644c67ea77SStefano Zampini   ierr = PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL);CHKERRQ(ierr);
765d8211ee3SMatthew G. Knepley   if (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ||
766d8211ee3SMatthew G. Knepley       bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ||
767d8211ee3SMatthew G. Knepley       bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST) {
7684c67ea77SStefano Zampini 
769d1c88043SMatthew G. Knepley     if (cutMarker) {ierr = DMCreateLabel(dm, "periodic_cut");CHKERRQ(ierr); ierr = DMGetLabel(dm, "periodic_cut", &cutLabel);CHKERRQ(ierr);}
770d8211ee3SMatthew G. Knepley   }
7713dfda0b1SToby Isaac   switch (dim) {
7723dfda0b1SToby Isaac   case 2:
773f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 3;
774f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
775f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 2;
776f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 4;
7773dfda0b1SToby Isaac     break;
7783dfda0b1SToby Isaac   case 3:
779f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
780f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 2;
781f4eb4c5dSMatthew G. Knepley     faceMarkerFront  = 3;
782f4eb4c5dSMatthew G. Knepley     faceMarkerBack   = 4;
783f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 5;
784f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 6;
7853dfda0b1SToby Isaac     break;
7863dfda0b1SToby Isaac   default:
78789c010cfSBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Dimension %D not supported",dim);
7883dfda0b1SToby Isaac   }
789c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);CHKERRQ(ierr);
790f4eb4c5dSMatthew G. Knepley   if (markerSeparate) {
791f4eb4c5dSMatthew G. Knepley     markerBottom = faceMarkerBottom;
792f4eb4c5dSMatthew G. Knepley     markerTop    = faceMarkerTop;
793f4eb4c5dSMatthew G. Knepley     markerFront  = faceMarkerFront;
794f4eb4c5dSMatthew G. Knepley     markerBack   = faceMarkerBack;
795f4eb4c5dSMatthew G. Knepley     markerRight  = faceMarkerRight;
796f4eb4c5dSMatthew G. Knepley     markerLeft   = faceMarkerLeft;
7973dfda0b1SToby Isaac   }
7983dfda0b1SToby Isaac   {
7993dfda0b1SToby Isaac     const PetscInt numXEdges    = !rank ? edges[0] : 0;
8003dfda0b1SToby Isaac     const PetscInt numYEdges    = !rank ? edges[1] : 0;
8013dfda0b1SToby Isaac     const PetscInt numZEdges    = !rank ? edges[2] : 0;
8023dfda0b1SToby Isaac     const PetscInt numXVertices = !rank ? (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ? edges[0] : edges[0]+1) : 0;
8033dfda0b1SToby Isaac     const PetscInt numYVertices = !rank ? (bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ? edges[1] : edges[1]+1) : 0;
80442206facSLisandro Dalcin     const PetscInt numZVertices = !rank ? (bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST ? edges[2] : edges[2]+1) : 0;
8053dfda0b1SToby Isaac     const PetscInt numCells     = numXEdges*numYEdges*numZEdges;
8063dfda0b1SToby Isaac     const PetscInt numXFaces    = numYEdges*numZEdges;
8073dfda0b1SToby Isaac     const PetscInt numYFaces    = numXEdges*numZEdges;
8083dfda0b1SToby Isaac     const PetscInt numZFaces    = numXEdges*numYEdges;
8093dfda0b1SToby Isaac     const PetscInt numTotXFaces = numXVertices*numXFaces;
8103dfda0b1SToby Isaac     const PetscInt numTotYFaces = numYVertices*numYFaces;
8113dfda0b1SToby Isaac     const PetscInt numTotZFaces = numZVertices*numZFaces;
8123dfda0b1SToby Isaac     const PetscInt numFaces     = numTotXFaces + numTotYFaces + numTotZFaces;
8133dfda0b1SToby Isaac     const PetscInt numTotXEdges = numXEdges*numYVertices*numZVertices;
8143dfda0b1SToby Isaac     const PetscInt numTotYEdges = numYEdges*numXVertices*numZVertices;
8153dfda0b1SToby Isaac     const PetscInt numTotZEdges = numZEdges*numXVertices*numYVertices;
8163dfda0b1SToby Isaac     const PetscInt numVertices  = numXVertices*numYVertices*numZVertices;
8173dfda0b1SToby Isaac     const PetscInt numEdges     = numTotXEdges + numTotYEdges + numTotZEdges;
8183dfda0b1SToby Isaac     const PetscInt firstVertex  = (dim == 2) ? numFaces : numCells;
8193dfda0b1SToby Isaac     const PetscInt firstXFace   = (dim == 2) ? 0 : numCells + numVertices;
8203dfda0b1SToby Isaac     const PetscInt firstYFace   = firstXFace + numTotXFaces;
8213dfda0b1SToby Isaac     const PetscInt firstZFace   = firstYFace + numTotYFaces;
8223dfda0b1SToby Isaac     const PetscInt firstXEdge   = numCells + numFaces + numVertices;
8233dfda0b1SToby Isaac     const PetscInt firstYEdge   = firstXEdge + numTotXEdges;
8243dfda0b1SToby Isaac     const PetscInt firstZEdge   = firstYEdge + numTotYEdges;
8253dfda0b1SToby Isaac     Vec            coordinates;
8263dfda0b1SToby Isaac     PetscSection   coordSection;
8273dfda0b1SToby Isaac     PetscScalar   *coords;
8283dfda0b1SToby Isaac     PetscInt       coordSize;
8293dfda0b1SToby Isaac     PetscInt       v, vx, vy, vz;
8303dfda0b1SToby Isaac     PetscInt       c, f, fx, fy, fz, e, ex, ey, ez;
8313dfda0b1SToby Isaac 
8323dfda0b1SToby Isaac     ierr = DMPlexSetChart(dm, 0, numCells+numFaces+numEdges+numVertices);CHKERRQ(ierr);
8333dfda0b1SToby Isaac     for (c = 0; c < numCells; c++) {
8343dfda0b1SToby Isaac       ierr = DMPlexSetConeSize(dm, c, 6);CHKERRQ(ierr);
8353dfda0b1SToby Isaac     }
8363dfda0b1SToby Isaac     for (f = firstXFace; f < firstXFace+numFaces; ++f) {
8373dfda0b1SToby Isaac       ierr = DMPlexSetConeSize(dm, f, 4);CHKERRQ(ierr);
8383dfda0b1SToby Isaac     }
8393dfda0b1SToby Isaac     for (e = firstXEdge; e < firstXEdge+numEdges; ++e) {
8403dfda0b1SToby Isaac       ierr = DMPlexSetConeSize(dm, e, 2);CHKERRQ(ierr);
8413dfda0b1SToby Isaac     }
8423dfda0b1SToby Isaac     ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
8433dfda0b1SToby Isaac     /* Build cells */
8443dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
8453dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
8463dfda0b1SToby Isaac         for (fx = 0; fx < numXEdges; ++fx) {
8473dfda0b1SToby Isaac           PetscInt cell    = (fz*numYEdges + fy)*numXEdges + fx;
8483dfda0b1SToby Isaac           PetscInt faceB   = firstZFace + (fy*numXEdges+fx)*numZVertices +   fz;
8493dfda0b1SToby Isaac           PetscInt faceT   = firstZFace + (fy*numXEdges+fx)*numZVertices + ((fz+1)%numZVertices);
8503dfda0b1SToby Isaac           PetscInt faceF   = firstYFace + (fz*numXEdges+fx)*numYVertices +   fy;
8513dfda0b1SToby Isaac           PetscInt faceK   = firstYFace + (fz*numXEdges+fx)*numYVertices + ((fy+1)%numYVertices);
8523dfda0b1SToby Isaac           PetscInt faceL   = firstXFace + (fz*numYEdges+fy)*numXVertices +   fx;
8533dfda0b1SToby Isaac           PetscInt faceR   = firstXFace + (fz*numYEdges+fy)*numXVertices + ((fx+1)%numXVertices);
8543dfda0b1SToby Isaac                             /* B,  T,  F,  K,  R,  L */
85542206facSLisandro Dalcin           PetscInt ornt[6] = {-4,  0,  0, -1,  0, -4}; /* ??? */
85642206facSLisandro Dalcin           PetscInt cone[6];
8573dfda0b1SToby Isaac 
8583dfda0b1SToby Isaac           /* no boundary twisting in 3D */
8593dfda0b1SToby Isaac           cone[0] = faceB; cone[1] = faceT; cone[2] = faceF; cone[3] = faceK; cone[4] = faceR; cone[5] = faceL;
8603dfda0b1SToby Isaac           ierr    = DMPlexSetCone(dm, cell, cone);CHKERRQ(ierr);
8613dfda0b1SToby Isaac           ierr    = DMPlexSetConeOrientation(dm, cell, ornt);CHKERRQ(ierr);
8628a5b437dSMatthew G. Knepley           if (bdX != DM_BOUNDARY_NONE && fx == numXEdges-1 && cutLabel) {ierr = DMLabelSetValue(cutLabel, cell, 2);CHKERRQ(ierr);}
8638a5b437dSMatthew G. Knepley           if (bdY != DM_BOUNDARY_NONE && fy == numYEdges-1 && cutLabel) {ierr = DMLabelSetValue(cutLabel, cell, 2);CHKERRQ(ierr);}
8648a5b437dSMatthew G. Knepley           if (bdZ != DM_BOUNDARY_NONE && fz == numZEdges-1 && cutLabel) {ierr = DMLabelSetValue(cutLabel, cell, 2);CHKERRQ(ierr);}
8653dfda0b1SToby Isaac         }
8663dfda0b1SToby Isaac       }
8673dfda0b1SToby Isaac     }
8683dfda0b1SToby Isaac     /* Build x faces */
8693dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
8703dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
8713dfda0b1SToby Isaac         for (fx = 0; fx < numXVertices; ++fx) {
8723dfda0b1SToby Isaac           PetscInt face    = firstXFace + (fz*numYEdges+fy)     *numXVertices+fx;
8733dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy                   *numXVertices+fx)*numZEdges + fz;
8743dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (((fy+1)%numYVertices)*numXVertices+fx)*numZEdges + fz;
8753dfda0b1SToby Isaac           PetscInt edgeB   = firstYEdge + (fz                   *numXVertices+fx)*numYEdges + fy;
8763dfda0b1SToby Isaac           PetscInt edgeT   = firstYEdge + (((fz+1)%numZVertices)*numXVertices+fx)*numYEdges + fy;
8773dfda0b1SToby Isaac           PetscInt ornt[4] = {0, 0, -2, -2};
8783dfda0b1SToby Isaac           PetscInt cone[4];
8793dfda0b1SToby Isaac 
8803dfda0b1SToby Isaac           if (dim == 3) {
8813dfda0b1SToby Isaac             /* markers */
8823dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
8833dfda0b1SToby Isaac               if (fx == numXVertices-1) {
884c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerRight);CHKERRQ(ierr);
885c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", face, markerRight);CHKERRQ(ierr);
8863dfda0b1SToby Isaac               }
8873dfda0b1SToby Isaac               else if (fx == 0) {
888c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerLeft);CHKERRQ(ierr);
889c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", face, markerLeft);CHKERRQ(ierr);
8903dfda0b1SToby Isaac               }
8913dfda0b1SToby Isaac             }
8923dfda0b1SToby Isaac           }
8933dfda0b1SToby Isaac           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
8943dfda0b1SToby Isaac           ierr    = DMPlexSetCone(dm, face, cone);CHKERRQ(ierr);
8953dfda0b1SToby Isaac           ierr    = DMPlexSetConeOrientation(dm, face, ornt);CHKERRQ(ierr);
8963dfda0b1SToby Isaac         }
8973dfda0b1SToby Isaac       }
8983dfda0b1SToby Isaac     }
8993dfda0b1SToby Isaac     /* Build y faces */
9003dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
90142206facSLisandro Dalcin       for (fx = 0; fx < numXEdges; ++fx) {
9023dfda0b1SToby Isaac         for (fy = 0; fy < numYVertices; ++fy) {
9033dfda0b1SToby Isaac           PetscInt face    = firstYFace + (fz*numXEdges+fx)*numYVertices + fy;
9043dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy*numXVertices+  fx)*numZEdges + fz;
9053dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (fy*numXVertices+((fx+1)%numXVertices))*numZEdges + fz;
9063dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz                   *numYVertices+fy)*numXEdges + fx;
9073dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (((fz+1)%numZVertices)*numYVertices+fy)*numXEdges + fx;
9083dfda0b1SToby Isaac           PetscInt ornt[4] = {0, 0, -2, -2};
9093dfda0b1SToby Isaac           PetscInt cone[4];
9103dfda0b1SToby Isaac 
9113dfda0b1SToby Isaac           if (dim == 3) {
9123dfda0b1SToby Isaac             /* markers */
9133dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
9143dfda0b1SToby Isaac               if (fy == numYVertices-1) {
915c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerBack);CHKERRQ(ierr);
916c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", face, markerBack);CHKERRQ(ierr);
9173dfda0b1SToby Isaac               }
9183dfda0b1SToby Isaac               else if (fy == 0) {
919c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerFront);CHKERRQ(ierr);
920c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", face, markerFront);CHKERRQ(ierr);
9213dfda0b1SToby Isaac               }
9223dfda0b1SToby Isaac             }
9233dfda0b1SToby Isaac           }
9243dfda0b1SToby Isaac           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
9253dfda0b1SToby Isaac           ierr    = DMPlexSetCone(dm, face, cone);CHKERRQ(ierr);
9263dfda0b1SToby Isaac           ierr    = DMPlexSetConeOrientation(dm, face, ornt);CHKERRQ(ierr);
9273dfda0b1SToby Isaac         }
9283dfda0b1SToby Isaac       }
9293dfda0b1SToby Isaac     }
9303dfda0b1SToby Isaac     /* Build z faces */
9313dfda0b1SToby Isaac     for (fy = 0; fy < numYEdges; ++fy) {
9323dfda0b1SToby Isaac       for (fx = 0; fx < numXEdges; ++fx) {
9333dfda0b1SToby Isaac         for (fz = 0; fz < numZVertices; fz++) {
9343dfda0b1SToby Isaac           PetscInt face    = firstZFace + (fy*numXEdges+fx)*numZVertices + fz;
9353dfda0b1SToby Isaac           PetscInt edgeL   = firstYEdge + (fz*numXVertices+  fx)*numYEdges + fy;
9363dfda0b1SToby Isaac           PetscInt edgeR   = firstYEdge + (fz*numXVertices+((fx+1)%numXVertices))*numYEdges + fy;
9373dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz*numYVertices+  fy)*numXEdges + fx;
9383dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (fz*numYVertices+((fy+1)%numYVertices))*numXEdges + fx;
9393dfda0b1SToby Isaac           PetscInt ornt[4] = {0, 0, -2, -2};
9403dfda0b1SToby Isaac           PetscInt cone[4];
9413dfda0b1SToby Isaac 
9423dfda0b1SToby Isaac           if (dim == 2) {
9433dfda0b1SToby Isaac             if (bdX == DM_BOUNDARY_TWIST && fx == numXEdges-1) {edgeR += numYEdges-1-2*fy; ornt[1] = -2;}
9443dfda0b1SToby Isaac             if (bdY == DM_BOUNDARY_TWIST && fy == numYEdges-1) {edgeT += numXEdges-1-2*fx; ornt[2] =  0;}
9458a5b437dSMatthew G. Knepley             if (bdX != DM_BOUNDARY_NONE && fx == numXEdges-1 && cutLabel) {ierr = DMLabelSetValue(cutLabel, face, 2);CHKERRQ(ierr);}
9468a5b437dSMatthew G. Knepley             if (bdY != DM_BOUNDARY_NONE && fy == numYEdges-1 && cutLabel) {ierr = DMLabelSetValue(cutLabel, face, 2);CHKERRQ(ierr);}
947d1c88043SMatthew G. Knepley           } else {
9483dfda0b1SToby Isaac             /* markers */
9493dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
9503dfda0b1SToby Isaac               if (fz == numZVertices-1) {
951c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerTop);CHKERRQ(ierr);
952c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", face, markerTop);CHKERRQ(ierr);
9533dfda0b1SToby Isaac               }
9543dfda0b1SToby Isaac               else if (fz == 0) {
955c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerBottom);CHKERRQ(ierr);
956c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", face, markerBottom);CHKERRQ(ierr);
9573dfda0b1SToby Isaac               }
9583dfda0b1SToby Isaac             }
9593dfda0b1SToby Isaac           }
9603dfda0b1SToby Isaac           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
9613dfda0b1SToby Isaac           ierr    = DMPlexSetCone(dm, face, cone);CHKERRQ(ierr);
9623dfda0b1SToby Isaac           ierr    = DMPlexSetConeOrientation(dm, face, ornt);CHKERRQ(ierr);
9633dfda0b1SToby Isaac         }
9643dfda0b1SToby Isaac       }
9653dfda0b1SToby Isaac     }
9663dfda0b1SToby Isaac     /* Build Z edges*/
9673dfda0b1SToby Isaac     for (vy = 0; vy < numYVertices; vy++) {
9683dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
9693dfda0b1SToby Isaac         for (ez = 0; ez < numZEdges; ez++) {
9703dfda0b1SToby Isaac           const PetscInt edge    = firstZEdge  + (vy*numXVertices+vx)*numZEdges + ez;
9713dfda0b1SToby Isaac           const PetscInt vertexB = firstVertex + (ez                   *numYVertices+vy)*numXVertices + vx;
9723dfda0b1SToby Isaac           const PetscInt vertexT = firstVertex + (((ez+1)%numZVertices)*numYVertices+vy)*numXVertices + vx;
9733dfda0b1SToby Isaac           PetscInt       cone[2];
9743dfda0b1SToby Isaac 
9753dfda0b1SToby Isaac           if (dim == 3) {
9763dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
9773dfda0b1SToby Isaac               if (vx == numXVertices-1) {
978c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerRight);CHKERRQ(ierr);
9793dfda0b1SToby Isaac               }
9803dfda0b1SToby Isaac               else if (vx == 0) {
981c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerLeft);CHKERRQ(ierr);
9823dfda0b1SToby Isaac               }
9833dfda0b1SToby Isaac             }
9843dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
9853dfda0b1SToby Isaac               if (vy == numYVertices-1) {
986c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerBack);CHKERRQ(ierr);
9873dfda0b1SToby Isaac               }
9883dfda0b1SToby Isaac               else if (vy == 0) {
989c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerFront);CHKERRQ(ierr);
9903dfda0b1SToby Isaac               }
9913dfda0b1SToby Isaac             }
9923dfda0b1SToby Isaac           }
9933dfda0b1SToby Isaac           cone[0] = vertexB; cone[1] = vertexT;
9943dfda0b1SToby Isaac           ierr = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr);
9953dfda0b1SToby Isaac         }
9963dfda0b1SToby Isaac       }
9973dfda0b1SToby Isaac     }
9983dfda0b1SToby Isaac     /* Build Y edges*/
9993dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
10003dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
10013dfda0b1SToby Isaac         for (ey = 0; ey < numYEdges; ey++) {
10023dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdY == DM_BOUNDARY_TWIST && ey == numYEdges-1) ? (numXVertices-vx-1) : (vz*numYVertices+((ey+1)%numYVertices))*numXVertices + vx;
10033dfda0b1SToby Isaac           const PetscInt edge    = firstYEdge  + (vz*numXVertices+vx)*numYEdges + ey;
10043dfda0b1SToby Isaac           const PetscInt vertexF = firstVertex + (vz*numYVertices+ey)*numXVertices + vx;
10053dfda0b1SToby Isaac           const PetscInt vertexK = firstVertex + nextv;
10063dfda0b1SToby Isaac           PetscInt       cone[2];
10073dfda0b1SToby Isaac 
10083dfda0b1SToby Isaac           cone[0] = vertexF; cone[1] = vertexK;
10093dfda0b1SToby Isaac           ierr = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr);
10103dfda0b1SToby Isaac           if (dim == 2) {
10113dfda0b1SToby Isaac             if ((bdX != DM_BOUNDARY_PERIODIC) && (bdX != DM_BOUNDARY_TWIST)) {
10123dfda0b1SToby Isaac               if (vx == numXVertices-1) {
1013c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", edge, faceMarkerRight);CHKERRQ(ierr);
1014c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge,    markerRight);CHKERRQ(ierr);
1015c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", cone[0], markerRight);CHKERRQ(ierr);
10163dfda0b1SToby Isaac                 if (ey == numYEdges-1) {
1017c58f1c22SToby Isaac                   ierr = DMSetLabelValue(dm, "marker", cone[1], markerRight);CHKERRQ(ierr);
10183dfda0b1SToby Isaac                 }
1019d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
1020c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", edge, faceMarkerLeft);CHKERRQ(ierr);
1021c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge,    markerLeft);CHKERRQ(ierr);
1022c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", cone[0], markerLeft);CHKERRQ(ierr);
10233dfda0b1SToby Isaac                 if (ey == numYEdges-1) {
1024c58f1c22SToby Isaac                   ierr = DMSetLabelValue(dm, "marker", cone[1], markerLeft);CHKERRQ(ierr);
10253dfda0b1SToby Isaac                 }
10263dfda0b1SToby Isaac               }
1027d8211ee3SMatthew G. Knepley             } else {
10284c67ea77SStefano Zampini               if (vx == 0 && cutLabel) {
1029d1c88043SMatthew G. Knepley                 ierr = DMLabelSetValue(cutLabel, edge,    1);CHKERRQ(ierr);
1030d1c88043SMatthew G. Knepley                 ierr = DMLabelSetValue(cutLabel, cone[0], 1);CHKERRQ(ierr);
1031d8211ee3SMatthew G. Knepley                 if (ey == numYEdges-1) {
1032d1c88043SMatthew G. Knepley                   ierr = DMLabelSetValue(cutLabel, cone[1], 1);CHKERRQ(ierr);
10333dfda0b1SToby Isaac                 }
10343dfda0b1SToby Isaac               }
1035d8211ee3SMatthew G. Knepley             }
1036d8211ee3SMatthew G. Knepley           } else {
10373dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
10383dfda0b1SToby Isaac               if (vx == numXVertices-1) {
1039c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerRight);CHKERRQ(ierr);
1040d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
1041c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerLeft);CHKERRQ(ierr);
10423dfda0b1SToby Isaac               }
10433dfda0b1SToby Isaac             }
10443dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
10453dfda0b1SToby Isaac               if (vz == numZVertices-1) {
1046c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerTop);CHKERRQ(ierr);
1047d8211ee3SMatthew G. Knepley               } else if (vz == 0) {
1048c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerBottom);CHKERRQ(ierr);
10493dfda0b1SToby Isaac               }
10503dfda0b1SToby Isaac             }
10513dfda0b1SToby Isaac           }
10523dfda0b1SToby Isaac         }
10533dfda0b1SToby Isaac       }
10543dfda0b1SToby Isaac     }
10553dfda0b1SToby Isaac     /* Build X edges*/
10563dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
10573dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; vy++) {
10583dfda0b1SToby Isaac         for (ex = 0; ex < numXEdges; ex++) {
10593dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdX == DM_BOUNDARY_TWIST && ex == numXEdges-1) ? (numYVertices-vy-1)*numXVertices : (vz*numYVertices+vy)*numXVertices + (ex+1)%numXVertices;
10603dfda0b1SToby Isaac           const PetscInt edge    = firstXEdge  + (vz*numYVertices+vy)*numXEdges + ex;
10613dfda0b1SToby Isaac           const PetscInt vertexL = firstVertex + (vz*numYVertices+vy)*numXVertices + ex;
10623dfda0b1SToby Isaac           const PetscInt vertexR = firstVertex + nextv;
10633dfda0b1SToby Isaac           PetscInt       cone[2];
10643dfda0b1SToby Isaac 
10653dfda0b1SToby Isaac           cone[0] = vertexL; cone[1] = vertexR;
10663dfda0b1SToby Isaac           ierr = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr);
10673dfda0b1SToby Isaac           if (dim == 2) {
10683dfda0b1SToby Isaac             if ((bdY != DM_BOUNDARY_PERIODIC) && (bdY != DM_BOUNDARY_TWIST)) {
10693dfda0b1SToby Isaac               if (vy == numYVertices-1) {
1070c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", edge, faceMarkerTop);CHKERRQ(ierr);
1071c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge,    markerTop);CHKERRQ(ierr);
1072c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", cone[0], markerTop);CHKERRQ(ierr);
10733dfda0b1SToby Isaac                 if (ex == numXEdges-1) {
1074c58f1c22SToby Isaac                   ierr = DMSetLabelValue(dm, "marker", cone[1], markerTop);CHKERRQ(ierr);
10753dfda0b1SToby Isaac                 }
1076d8211ee3SMatthew G. Knepley               } else if (vy == 0) {
1077c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", edge, faceMarkerBottom);CHKERRQ(ierr);
1078c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge,    markerBottom);CHKERRQ(ierr);
1079c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", cone[0], markerBottom);CHKERRQ(ierr);
10803dfda0b1SToby Isaac                 if (ex == numXEdges-1) {
1081c58f1c22SToby Isaac                   ierr = DMSetLabelValue(dm, "marker", cone[1], markerBottom);CHKERRQ(ierr);
10823dfda0b1SToby Isaac                 }
10833dfda0b1SToby Isaac               }
1084d8211ee3SMatthew G. Knepley             } else {
10854c67ea77SStefano Zampini               if (vy == 0 && cutLabel) {
1086d1c88043SMatthew G. Knepley                 ierr = DMLabelSetValue(cutLabel, edge,    1);CHKERRQ(ierr);
1087d1c88043SMatthew G. Knepley                 ierr = DMLabelSetValue(cutLabel, cone[0], 1);CHKERRQ(ierr);
1088d8211ee3SMatthew G. Knepley                 if (ex == numXEdges-1) {
1089d1c88043SMatthew G. Knepley                   ierr = DMLabelSetValue(cutLabel, cone[1], 1);CHKERRQ(ierr);
10903dfda0b1SToby Isaac                 }
10913dfda0b1SToby Isaac               }
1092d8211ee3SMatthew G. Knepley             }
1093d8211ee3SMatthew G. Knepley           } else {
10943dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
10953dfda0b1SToby Isaac               if (vy == numYVertices-1) {
1096c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerBack);CHKERRQ(ierr);
10973dfda0b1SToby Isaac               }
10983dfda0b1SToby Isaac               else if (vy == 0) {
1099c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerFront);CHKERRQ(ierr);
11003dfda0b1SToby Isaac               }
11013dfda0b1SToby Isaac             }
11023dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
11033dfda0b1SToby Isaac               if (vz == numZVertices-1) {
1104c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerTop);CHKERRQ(ierr);
11053dfda0b1SToby Isaac               }
11063dfda0b1SToby Isaac               else if (vz == 0) {
1107c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerBottom);CHKERRQ(ierr);
11083dfda0b1SToby Isaac               }
11093dfda0b1SToby Isaac             }
11103dfda0b1SToby Isaac           }
11113dfda0b1SToby Isaac         }
11123dfda0b1SToby Isaac       }
11133dfda0b1SToby Isaac     }
11143dfda0b1SToby Isaac     ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
11153dfda0b1SToby Isaac     ierr = DMPlexStratify(dm);CHKERRQ(ierr);
11163dfda0b1SToby Isaac     /* Build coordinates */
11173dfda0b1SToby Isaac     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
11183dfda0b1SToby Isaac     ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
11193dfda0b1SToby Isaac     ierr = PetscSectionSetFieldComponents(coordSection, 0, dim);CHKERRQ(ierr);
11203dfda0b1SToby Isaac     ierr = PetscSectionSetChart(coordSection, firstVertex, firstVertex+numVertices);CHKERRQ(ierr);
11213dfda0b1SToby Isaac     for (v = firstVertex; v < firstVertex+numVertices; ++v) {
11223dfda0b1SToby Isaac       ierr = PetscSectionSetDof(coordSection, v, dim);CHKERRQ(ierr);
11233dfda0b1SToby Isaac       ierr = PetscSectionSetFieldDof(coordSection, v, 0, dim);CHKERRQ(ierr);
11243dfda0b1SToby Isaac     }
11253dfda0b1SToby Isaac     ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
11263dfda0b1SToby Isaac     ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
11278b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
1128da16285aSMichael Lange     ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
11293dfda0b1SToby Isaac     ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
11308b9ced59SLisandro Dalcin     ierr = VecSetBlockSize(coordinates, dim);CHKERRQ(ierr);
11313dfda0b1SToby Isaac     ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
11323dfda0b1SToby Isaac     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
11333dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; ++vz) {
11343dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; ++vy) {
11353dfda0b1SToby Isaac         for (vx = 0; vx < numXVertices; ++vx) {
11363dfda0b1SToby Isaac           coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+0] = lower[0] + ((upper[0] - lower[0])/numXEdges)*vx;
11373dfda0b1SToby Isaac           coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+1] = lower[1] + ((upper[1] - lower[1])/numYEdges)*vy;
11383dfda0b1SToby Isaac           if (dim == 3) {
11393dfda0b1SToby Isaac             coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+2] = lower[2] + ((upper[2] - lower[2])/numZEdges)*vz;
11403dfda0b1SToby Isaac           }
11413dfda0b1SToby Isaac         }
11423dfda0b1SToby Isaac       }
11433dfda0b1SToby Isaac     }
11443dfda0b1SToby Isaac     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
11453dfda0b1SToby Isaac     ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
11463dfda0b1SToby Isaac     ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
11473dfda0b1SToby Isaac   }
11483dfda0b1SToby Isaac   PetscFunctionReturn(0);
11493dfda0b1SToby Isaac }
11503dfda0b1SToby Isaac 
1151*9318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxMesh_Tensor_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
1152a6dfd86eSKarl Rupp {
1153*9318fe57SMatthew G. Knepley   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
1154*9318fe57SMatthew G. Knepley   PetscInt       fac[3] = {0, 0, 0}, d;
1155552f7358SJed Brown   PetscErrorCode ierr;
1156552f7358SJed Brown 
1157552f7358SJed Brown   PetscFunctionBegin;
1158*9318fe57SMatthew G. Knepley   PetscValidPointer(dm, 1);
1159*9318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
1160*9318fe57SMatthew G. Knepley   ierr = DMSetDimension(dm, dim);CHKERRQ(ierr);
1161*9318fe57SMatthew G. Knepley   for (d = 0; d < dim; ++d) {fac[d] = faces[d]; bdt[d] = periodicity[d];}
1162*9318fe57SMatthew G. Knepley   ierr = DMPlexCreateCubeMesh_Internal(dm, lower, upper, fac, bdt[0], bdt[1], bdt[2]);CHKERRQ(ierr);
1163768d5fceSMatthew G. Knepley   if (periodicity[0] == DM_BOUNDARY_PERIODIC || periodicity[0] == DM_BOUNDARY_TWIST ||
1164768d5fceSMatthew G. Knepley       periodicity[1] == DM_BOUNDARY_PERIODIC || periodicity[1] == DM_BOUNDARY_TWIST ||
1165768d5fceSMatthew G. Knepley       (dim > 2 && (periodicity[2] == DM_BOUNDARY_PERIODIC || periodicity[2] == DM_BOUNDARY_TWIST))) {
1166768d5fceSMatthew G. Knepley     PetscReal L[3];
1167768d5fceSMatthew G. Knepley     PetscReal maxCell[3];
1168552f7358SJed Brown 
1169*9318fe57SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1170*9318fe57SMatthew G. Knepley       L[d]       = upper[d] - lower[d];
1171*9318fe57SMatthew G. Knepley       maxCell[d] = 1.1 * (L[d] / PetscMax(1, faces[d]));
1172768d5fceSMatthew G. Knepley     }
1173*9318fe57SMatthew G. Knepley     ierr = DMSetPeriodicity(dm, PETSC_TRUE, maxCell, L, periodicity);CHKERRQ(ierr);
1174768d5fceSMatthew G. Knepley   }
1175*9318fe57SMatthew G. Knepley   ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE);CHKERRQ(ierr);
1176*9318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
1177*9318fe57SMatthew G. Knepley }
1178*9318fe57SMatthew G. Knepley 
1179*9318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxMesh_Internal(DM dm, PetscInt dim, PetscBool simplex, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate)
1180*9318fe57SMatthew G. Knepley {
1181*9318fe57SMatthew G. Knepley   PetscErrorCode ierr;
1182*9318fe57SMatthew G. Knepley 
1183*9318fe57SMatthew G. Knepley   PetscFunctionBegin;
1184*9318fe57SMatthew G. Knepley   if (dim == 1)      {ierr = DMPlexCreateLineMesh_Internal(dm, faces[0], lower[0], upper[0], periodicity[0]);CHKERRQ(ierr);}
1185*9318fe57SMatthew G. Knepley   else if (simplex)  {ierr = DMPlexCreateBoxMesh_Simplex_Internal(dm, dim, faces, lower, upper, periodicity, interpolate);CHKERRQ(ierr);}
1186*9318fe57SMatthew G. Knepley   else               {ierr = DMPlexCreateBoxMesh_Tensor_Internal(dm, dim, faces, lower, upper, periodicity);CHKERRQ(ierr);}
1187*9318fe57SMatthew G. Knepley   if (!interpolate && dim > 1 && !simplex) {
1188768d5fceSMatthew G. Knepley     DM udm;
1189768d5fceSMatthew G. Knepley 
1190*9318fe57SMatthew G. Knepley     ierr = DMPlexUninterpolate(dm, &udm);CHKERRQ(ierr);
1191*9318fe57SMatthew G. Knepley     ierr = DMPlexCopyCoordinates(dm, udm);CHKERRQ(ierr);
1192*9318fe57SMatthew G. Knepley     ierr = DMPlexReplace_Static(dm, &udm);CHKERRQ(ierr);
1193768d5fceSMatthew G. Knepley   }
1194768d5fceSMatthew G. Knepley   PetscFunctionReturn(0);
1195c8c68bd8SToby Isaac }
1196c8c68bd8SToby Isaac 
1197768d5fceSMatthew G. Knepley /*@C
1198768d5fceSMatthew G. Knepley   DMPlexCreateBoxMesh - Creates a mesh on the tensor product of unit intervals (box) using simplices or tensor cells (hexahedra).
1199768d5fceSMatthew G. Knepley 
1200d083f849SBarry Smith   Collective
1201768d5fceSMatthew G. Knepley 
1202768d5fceSMatthew G. Knepley   Input Parameters:
1203768d5fceSMatthew G. Knepley + comm        - The communicator for the DM object
1204768d5fceSMatthew G. Knepley . dim         - The spatial dimension
1205768d5fceSMatthew G. Knepley . simplex     - PETSC_TRUE for simplices, PETSC_FALSE for tensor cells
1206fdbf62faSLisandro Dalcin . faces       - Number of faces per dimension, or NULL for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
1207768d5fceSMatthew G. Knepley . lower       - The lower left corner, or NULL for (0, 0, 0)
1208768d5fceSMatthew G. Knepley . upper       - The upper right corner, or NULL for (1, 1, 1)
1209fdbf62faSLisandro Dalcin . periodicity - The boundary type for the X,Y,Z direction, or NULL for DM_BOUNDARY_NONE
1210768d5fceSMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
1211768d5fceSMatthew G. Knepley 
1212768d5fceSMatthew G. Knepley   Output Parameter:
1213768d5fceSMatthew G. Knepley . dm  - The DM object
1214768d5fceSMatthew G. Knepley 
1215*9318fe57SMatthew G. Knepley   Note: If you want to customize this mesh using options, you just need to
1216*9318fe57SMatthew G. Knepley $  DMCreate(comm, &dm);
1217*9318fe57SMatthew G. Knepley $  DMSetType(dm, DMPLEX);
1218*9318fe57SMatthew G. Knepley $  DMSetFromOptions(dm);
1219*9318fe57SMatthew G. Knepley and use the options on the DMSetFromOptions() page.
12201367e252SJed Brown 
12211367e252SJed Brown   Here is the numbering returned for 2 faces in each direction for tensor cells:
1222768d5fceSMatthew G. Knepley $ 10---17---11---18----12
1223768d5fceSMatthew G. Knepley $  |         |         |
1224768d5fceSMatthew G. Knepley $  |         |         |
1225768d5fceSMatthew G. Knepley $ 20    2   22    3    24
1226768d5fceSMatthew G. Knepley $  |         |         |
1227768d5fceSMatthew G. Knepley $  |         |         |
1228768d5fceSMatthew G. Knepley $  7---15----8---16----9
1229768d5fceSMatthew G. Knepley $  |         |         |
1230768d5fceSMatthew G. Knepley $  |         |         |
1231768d5fceSMatthew G. Knepley $ 19    0   21    1   23
1232768d5fceSMatthew G. Knepley $  |         |         |
1233768d5fceSMatthew G. Knepley $  |         |         |
1234768d5fceSMatthew G. Knepley $  4---13----5---14----6
1235768d5fceSMatthew G. Knepley 
1236768d5fceSMatthew G. Knepley and for simplicial cells
1237768d5fceSMatthew G. Knepley 
1238768d5fceSMatthew G. Knepley $ 14----8---15----9----16
1239768d5fceSMatthew G. Knepley $  |\     5  |\      7 |
1240768d5fceSMatthew G. Knepley $  | \       | \       |
1241768d5fceSMatthew G. Knepley $ 13   2    14    3    15
1242768d5fceSMatthew G. Knepley $  | 4   \   | 6   \   |
1243768d5fceSMatthew G. Knepley $  |       \ |       \ |
1244768d5fceSMatthew G. Knepley $ 11----6---12----7----13
1245768d5fceSMatthew G. Knepley $  |\        |\        |
1246768d5fceSMatthew G. Knepley $  | \    1  | \     3 |
1247768d5fceSMatthew G. Knepley $ 10   0    11    1    12
1248768d5fceSMatthew G. Knepley $  | 0   \   | 2   \   |
1249768d5fceSMatthew G. Knepley $  |       \ |       \ |
1250768d5fceSMatthew G. Knepley $  8----4----9----5----10
1251768d5fceSMatthew G. Knepley 
1252768d5fceSMatthew G. Knepley   Level: beginner
1253768d5fceSMatthew G. Knepley 
1254*9318fe57SMatthew G. Knepley .seealso: DMSetFromOptions(), DMPlexCreateFromFile(), DMPlexCreateHexCylinderMesh(), DMSetType(), DMCreate()
1255768d5fceSMatthew G. Knepley @*/
1256768d5fceSMatthew G. Knepley PetscErrorCode DMPlexCreateBoxMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate, DM *dm)
1257552f7358SJed Brown {
1258*9318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
1259fdbf62faSLisandro Dalcin   PetscReal      low[3] = {0, 0, 0};
1260fdbf62faSLisandro Dalcin   PetscReal      upp[3] = {1, 1, 1};
1261fdbf62faSLisandro Dalcin   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
1262768d5fceSMatthew G. Knepley   PetscErrorCode ierr;
1263552f7358SJed Brown 
1264768d5fceSMatthew G. Knepley   PetscFunctionBegin;
1265*9318fe57SMatthew G. Knepley   ierr = DMCreate(comm,dm);CHKERRQ(ierr);
1266*9318fe57SMatthew G. Knepley   ierr = DMSetType(*dm,DMPLEX);CHKERRQ(ierr);
1267*9318fe57SMatthew G. Knepley   ierr = DMPlexCreateBoxMesh_Internal(*dm, dim, simplex, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt, interpolate);CHKERRQ(ierr);
1268*9318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
1269*9318fe57SMatthew G. Knepley }
1270fdbf62faSLisandro Dalcin 
1271*9318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateWedgeBoxMesh_Internal(DM dm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool orderHeight, PetscBool interpolate)
1272*9318fe57SMatthew G. Knepley {
1273*9318fe57SMatthew G. Knepley   DM             bdm, vol;
1274*9318fe57SMatthew G. Knepley   PetscReal      normal[3] = {0., 0., 1.};
1275*9318fe57SMatthew G. Knepley   PetscInt       i;
1276*9318fe57SMatthew G. Knepley   PetscErrorCode ierr;
1277*9318fe57SMatthew G. Knepley 
1278*9318fe57SMatthew G. Knepley   PetscFunctionBegin;
1279*9318fe57SMatthew G. Knepley   for (i = 0; i < 3; ++i) if (periodicity[i] != DM_BOUNDARY_NONE) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Periodicity not yet supported");
1280*9318fe57SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), &bdm);CHKERRQ(ierr);
1281*9318fe57SMatthew G. Knepley   ierr = DMSetType(bdm, DMPLEX);CHKERRQ(ierr);
1282*9318fe57SMatthew G. Knepley   ierr = DMSetDimension(bdm, 2);CHKERRQ(ierr);
1283*9318fe57SMatthew G. Knepley   ierr = DMPlexCreateBoxMesh_Simplex_Internal(bdm, 2, faces, lower, upper, periodicity, interpolate);CHKERRQ(ierr);
1284*9318fe57SMatthew G. Knepley   ierr = DMPlexExtrude(bdm, faces[2], upper[2] - lower[2], orderHeight, normal, interpolate, &vol);CHKERRQ(ierr);
1285*9318fe57SMatthew G. Knepley   ierr = DMDestroy(&bdm);CHKERRQ(ierr);
1286*9318fe57SMatthew G. Knepley   ierr = DMPlexReplace_Static(dm, &vol);CHKERRQ(ierr);
1287*9318fe57SMatthew G. Knepley   if (lower[2] != 0.0) {
1288*9318fe57SMatthew G. Knepley     Vec          v;
1289*9318fe57SMatthew G. Knepley     PetscScalar *x;
1290*9318fe57SMatthew G. Knepley     PetscInt     cDim, n;
1291*9318fe57SMatthew G. Knepley 
1292*9318fe57SMatthew G. Knepley     ierr = DMGetCoordinatesLocal(dm, &v);CHKERRQ(ierr);
1293*9318fe57SMatthew G. Knepley     ierr = VecGetBlockSize(v, &cDim);CHKERRQ(ierr);
1294*9318fe57SMatthew G. Knepley     ierr = VecGetLocalSize(v, &n);CHKERRQ(ierr);
1295*9318fe57SMatthew G. Knepley     ierr = VecGetArray(v, &x);CHKERRQ(ierr);
1296*9318fe57SMatthew G. Knepley     x   += cDim;
1297*9318fe57SMatthew G. Knepley     for (i = 0; i < n; i += cDim) x[i] += lower[2];
1298*9318fe57SMatthew G. Knepley     ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
1299*9318fe57SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(dm, v);CHKERRQ(ierr);
1300*9318fe57SMatthew G. Knepley   }
1301552f7358SJed Brown   PetscFunctionReturn(0);
1302552f7358SJed Brown }
1303552f7358SJed Brown 
130400dabe28SStefano Zampini /*@
130500dabe28SStefano Zampini   DMPlexCreateWedgeBoxMesh - Creates a 3-D mesh tesselating the (x,y) plane and extruding in the third direction using wedge cells.
130600dabe28SStefano Zampini 
1307d083f849SBarry Smith   Collective
130800dabe28SStefano Zampini 
130900dabe28SStefano Zampini   Input Parameters:
131000dabe28SStefano Zampini + comm        - The communicator for the DM object
131100dabe28SStefano Zampini . faces       - Number of faces per dimension, or NULL for (1, 1, 1)
131200dabe28SStefano Zampini . lower       - The lower left corner, or NULL for (0, 0, 0)
131300dabe28SStefano Zampini . upper       - The upper right corner, or NULL for (1, 1, 1)
131400dabe28SStefano Zampini . periodicity - The boundary type for the X,Y,Z direction, or NULL for DM_BOUNDARY_NONE
1315d0fcb9c2SMatthew G. Knepley . orderHeight - If PETSC_TRUE, orders the extruded cells in the height first. Otherwise, orders the cell on the layers first
131600dabe28SStefano Zampini - interpolate - Flag to create intermediate mesh pieces (edges, faces)
131700dabe28SStefano Zampini 
131800dabe28SStefano Zampini   Output Parameter:
131900dabe28SStefano Zampini . dm  - The DM object
132000dabe28SStefano Zampini 
132100dabe28SStefano Zampini   Level: beginner
132200dabe28SStefano Zampini 
132300dabe28SStefano Zampini .seealso: DMPlexCreateHexCylinderMesh(), DMPlexCreateWedgeCylinderMesh(), DMPlexExtrude(), DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
132400dabe28SStefano Zampini @*/
1325d0fcb9c2SMatthew G. Knepley PetscErrorCode DMPlexCreateWedgeBoxMesh(MPI_Comm comm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool orderHeight, PetscBool interpolate, DM *dm)
132600dabe28SStefano Zampini {
1327*9318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
132800dabe28SStefano Zampini   PetscReal      low[3] = {0, 0, 0};
132900dabe28SStefano Zampini   PetscReal      upp[3] = {1, 1, 1};
133000dabe28SStefano Zampini   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
133100dabe28SStefano Zampini   PetscErrorCode ierr;
133200dabe28SStefano Zampini 
133300dabe28SStefano Zampini   PetscFunctionBegin;
1334*9318fe57SMatthew G. Knepley   ierr = DMCreate(comm,dm);CHKERRQ(ierr);
1335*9318fe57SMatthew G. Knepley   ierr = DMSetType(*dm,DMPLEX);CHKERRQ(ierr);
1336*9318fe57SMatthew G. Knepley   ierr = DMPlexCreateWedgeBoxMesh_Internal(*dm, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt, orderHeight, interpolate);CHKERRQ(ierr);
133700dabe28SStefano Zampini   PetscFunctionReturn(0);
133800dabe28SStefano Zampini }
133900dabe28SStefano Zampini 
1340d0fcb9c2SMatthew G. Knepley /*@C
134100dabe28SStefano Zampini   DMPlexExtrude - Creates a (d+1)-D mesh by extruding a d-D mesh in the normal direction using prismatic cells.
134200dabe28SStefano Zampini 
134300dabe28SStefano Zampini   Collective on idm
134400dabe28SStefano Zampini 
134500dabe28SStefano Zampini   Input Parameters:
1346d0fcb9c2SMatthew G. Knepley + idm         - The mesh to be extruded
1347d0fcb9c2SMatthew G. Knepley . layers      - The number of layers, or PETSC_DETERMINE to use the default
134809c71656SMatthew G. Knepley . height      - The total height of the extrusion, or PETSC_DETERMINE to use the default
1349d0fcb9c2SMatthew G. Knepley . orderHeight - If PETSC_TRUE, orders the extruded cells in the height first. Otherwise, orders the cell on the layers first
1350d0fcb9c2SMatthew G. Knepley . extNormal   - The normal direction in which the mesh should be extruded, or NULL to extrude using the surface normal
135100dabe28SStefano Zampini - interpolate - Flag to create intermediate mesh pieces (edges, faces)
135200dabe28SStefano Zampini 
135300dabe28SStefano Zampini   Output Parameter:
135400dabe28SStefano Zampini . dm  - The DM object
135500dabe28SStefano Zampini 
1356d0fcb9c2SMatthew G. Knepley   Notes:
1357d0fcb9c2SMatthew G. Knepley   The mesh created has prismatic cells, and the vertex ordering in the cone of the cell is that of the tensor prismatic cells. Not currently supported in Fortran.
1358d0fcb9c2SMatthew G. Knepley 
1359d0fcb9c2SMatthew G. Knepley   Options Database Keys:
13602479783cSJose E. Roman +   -dm_plex_extrude_layers <k> - Sets the number of layers k
136109c71656SMatthew G. Knepley .   -dm_plex_extrude_height <h> - Sets the total height of the extrusion
136209c71656SMatthew G. Knepley .   -dm_plex_extrude_heights <h0,h1,...> - Sets the height of each layer
1363ee300463SSatish Balay .   -dm_plex_extrude_order_height - If true, order cells by height first
1364ee300463SSatish Balay -   -dm_plex_extrude_normal <n0,...,nd> - Sets the normal vector along which to extrude
136500dabe28SStefano Zampini 
136600dabe28SStefano Zampini   Level: advanced
136700dabe28SStefano Zampini 
1368412e9a14SMatthew G. Knepley .seealso: DMPlexCreateWedgeCylinderMesh(), DMPlexCreateWedgeBoxMesh(), DMSetType(), DMCreate()
136900dabe28SStefano Zampini @*/
1370d0fcb9c2SMatthew G. Knepley PetscErrorCode DMPlexExtrude(DM idm, PetscInt layers, PetscReal height, PetscBool orderHeight, const PetscReal extNormal[], PetscBool interpolate, DM* dm)
137100dabe28SStefano Zampini {
137200dabe28SStefano Zampini   PetscScalar       *coordsB;
137300dabe28SStefano Zampini   const PetscScalar *coordsA;
137409c71656SMatthew G. Knepley   PetscReal         *normals = NULL, *heights = NULL;
1375d0fcb9c2SMatthew G. Knepley   PetscReal         clNormal[3];
137600dabe28SStefano Zampini   Vec               coordinatesA, coordinatesB;
137700dabe28SStefano Zampini   PetscSection      coordSectionA, coordSectionB;
137809c71656SMatthew G. Knepley   PetscInt          dim, cDim, cDimB, c, l, v, coordSize, *newCone, nl;
137900dabe28SStefano Zampini   PetscInt          cStart, cEnd, vStart, vEnd, cellV, numCells, numVertices;
1380d0fcb9c2SMatthew G. Knepley   const char       *prefix;
138109c71656SMatthew G. Knepley   PetscBool         haveCLNormal, flg;
138200dabe28SStefano Zampini   PetscErrorCode    ierr;
138300dabe28SStefano Zampini 
138400dabe28SStefano Zampini   PetscFunctionBegin;
138500dabe28SStefano Zampini   PetscValidHeaderSpecific(idm, DM_CLASSID, 1);
138600dabe28SStefano Zampini   PetscValidLogicalCollectiveInt(idm, layers, 2);
138700dabe28SStefano Zampini   PetscValidLogicalCollectiveReal(idm, height, 3);
1388064a246eSJacob Faibussowitsch   PetscValidLogicalCollectiveBool(idm, interpolate, 6);
138900dabe28SStefano Zampini   ierr = DMGetDimension(idm, &dim);CHKERRQ(ierr);
1390d0fcb9c2SMatthew G. Knepley   ierr = DMGetCoordinateDim(idm, &cDim);CHKERRQ(ierr);
1391d0fcb9c2SMatthew G. Knepley   cDimB = cDim == dim ? cDim+1 : cDim;
139200dabe28SStefano Zampini   if (dim < 1 || dim > 3) SETERRQ1(PetscObjectComm((PetscObject)idm), PETSC_ERR_SUP, "Support for dimension %D not coded", dim);
139300dabe28SStefano Zampini 
1394d0fcb9c2SMatthew G. Knepley   ierr = PetscObjectGetOptionsPrefix((PetscObject) idm, &prefix);CHKERRQ(ierr);
1395d0fcb9c2SMatthew G. Knepley   if (layers < 0) layers = 1;
1396d0fcb9c2SMatthew G. Knepley   ierr = PetscOptionsGetInt(NULL, prefix, "-dm_plex_extrude_layers", &layers, NULL);CHKERRQ(ierr);
1397d0fcb9c2SMatthew G. Knepley   if (layers <= 0) SETERRQ1(PetscObjectComm((PetscObject) idm), PETSC_ERR_ARG_OUTOFRANGE, "Number of layers %D must be positive", layers);
1398d0fcb9c2SMatthew G. Knepley   if (height < 0.) height = 1.;
1399d0fcb9c2SMatthew G. Knepley   ierr = PetscOptionsGetReal(NULL, prefix, "-dm_plex_extrude_height", &height, NULL);CHKERRQ(ierr);
1400d0fcb9c2SMatthew G. Knepley   if (height <= 0.) SETERRQ1(PetscObjectComm((PetscObject) idm), PETSC_ERR_ARG_OUTOFRANGE, "Height of layers %g must be positive", (double) height);
140109c71656SMatthew G. Knepley   ierr = PetscMalloc1(layers, &heights);CHKERRQ(ierr);
140209c71656SMatthew G. Knepley   nl   = layers;
140309c71656SMatthew G. Knepley   ierr = PetscOptionsGetRealArray(NULL, prefix, "-dm_plex_extrude_heights", heights, &nl, &flg);CHKERRQ(ierr);
140409c71656SMatthew G. Knepley   if (flg) {
140509c71656SMatthew G. Knepley     if (!nl) SETERRQ(PetscObjectComm((PetscObject) idm), PETSC_ERR_ARG_OUTOFRANGE, "Must give at least one height for -dm_plex_extrude_heights");
140609c71656SMatthew G. Knepley     for (l = nl; l < layers; ++l) heights[l] = heights[l-1];
140709c71656SMatthew G. Knepley     for (l = 0; l < layers; ++l) if (heights[l] <= 0.) SETERRQ2(PetscObjectComm((PetscObject) idm), PETSC_ERR_ARG_OUTOFRANGE, "Height %g of layers %D must be positive", (double) heights[l], l);
140809c71656SMatthew G. Knepley   } else {
140909c71656SMatthew G. Knepley     for (l = 0; l < layers; ++l) heights[l] = height/layers;
141009c71656SMatthew G. Knepley   }
1411d0fcb9c2SMatthew G. Knepley   ierr = PetscOptionsGetBool(NULL, prefix, "-dm_plex_extrude_order_height", &orderHeight, NULL);CHKERRQ(ierr);
1412d0fcb9c2SMatthew G. Knepley   c = 3;
1413d0fcb9c2SMatthew G. Knepley   ierr = PetscOptionsGetRealArray(NULL, prefix, "-dm_plex_extrude_normal", clNormal, &c, &haveCLNormal);CHKERRQ(ierr);
1414d0fcb9c2SMatthew G. Knepley   if (haveCLNormal && c != cDimB) SETERRQ2(PetscObjectComm((PetscObject)idm), PETSC_ERR_ARG_SIZ, "Input normal has size %D != %D extruded coordinate dimension", c, cDimB);
1415d0fcb9c2SMatthew G. Knepley 
141600dabe28SStefano Zampini   ierr = DMPlexGetHeightStratum(idm, 0, &cStart, &cEnd);CHKERRQ(ierr);
141700dabe28SStefano Zampini   ierr = DMPlexGetDepthStratum(idm, 0, &vStart, &vEnd);CHKERRQ(ierr);
141800dabe28SStefano Zampini   numCells = (cEnd - cStart)*layers;
141900dabe28SStefano Zampini   numVertices = (vEnd - vStart)*(layers+1);
142000dabe28SStefano Zampini   ierr = DMCreate(PetscObjectComm((PetscObject)idm), dm);CHKERRQ(ierr);
142100dabe28SStefano Zampini   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
142200dabe28SStefano Zampini   ierr = DMSetDimension(*dm, dim+1);CHKERRQ(ierr);
142300dabe28SStefano Zampini   ierr = DMPlexSetChart(*dm, 0, numCells+numVertices);CHKERRQ(ierr);
1424412e9a14SMatthew G. Knepley   /* Must create the celltype label here so that we do not automatically try to compute the types */
1425412e9a14SMatthew G. Knepley   ierr = DMCreateLabel(*dm, "celltype");CHKERRQ(ierr);
142600dabe28SStefano Zampini   for (c = cStart, cellV = 0; c < cEnd; ++c) {
1427412e9a14SMatthew G. Knepley     DMPolytopeType ct, nct;
142800dabe28SStefano Zampini     PetscInt      *closure = NULL;
142900dabe28SStefano Zampini     PetscInt       closureSize, numCorners = 0;
143000dabe28SStefano Zampini 
1431412e9a14SMatthew G. Knepley     ierr = DMPlexGetCellType(idm, c, &ct);CHKERRQ(ierr);
1432412e9a14SMatthew G. Knepley     switch (ct) {
1433412e9a14SMatthew G. Knepley       case DM_POLYTOPE_SEGMENT:       nct = DM_POLYTOPE_SEG_PRISM_TENSOR;break;
1434412e9a14SMatthew G. Knepley       case DM_POLYTOPE_TRIANGLE:      nct = DM_POLYTOPE_TRI_PRISM_TENSOR;break;
1435412e9a14SMatthew G. Knepley       case DM_POLYTOPE_QUADRILATERAL: nct = DM_POLYTOPE_QUAD_PRISM_TENSOR;break;
1436412e9a14SMatthew G. Knepley       default: nct = DM_POLYTOPE_UNKNOWN;
1437412e9a14SMatthew G. Knepley     }
143800dabe28SStefano Zampini     ierr = DMPlexGetTransitiveClosure(idm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
143900dabe28SStefano Zampini     for (v = 0; v < closureSize*2; v += 2) if ((closure[v] >= vStart) && (closure[v] < vEnd)) numCorners++;
144000dabe28SStefano Zampini     ierr = DMPlexRestoreTransitiveClosure(idm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
144100dabe28SStefano Zampini     for (l = 0; l < layers; ++l) {
1442d0fcb9c2SMatthew G. Knepley       const PetscInt cell = orderHeight ? layers*(c - cStart) + l : l*(cEnd - cStart) + c - cStart;
1443412e9a14SMatthew G. Knepley 
1444412e9a14SMatthew G. Knepley       ierr = DMPlexSetConeSize(*dm, cell, 2*numCorners);CHKERRQ(ierr);
1445412e9a14SMatthew G. Knepley       ierr = DMPlexSetCellType(*dm, cell, nct);CHKERRQ(ierr);
144600dabe28SStefano Zampini     }
144700dabe28SStefano Zampini     cellV = PetscMax(numCorners,cellV);
144800dabe28SStefano Zampini   }
144900dabe28SStefano Zampini   ierr = DMSetUp(*dm);CHKERRQ(ierr);
145000dabe28SStefano Zampini 
1451d0fcb9c2SMatthew G. Knepley   if (dim != cDim && !(extNormal || haveCLNormal)) {ierr = PetscCalloc1(cDim*(vEnd - vStart), &normals);CHKERRQ(ierr);}
145200dabe28SStefano Zampini   ierr = PetscMalloc1(3*cellV,&newCone);CHKERRQ(ierr);
145300dabe28SStefano Zampini   for (c = cStart; c < cEnd; ++c) {
145400dabe28SStefano Zampini     PetscInt *closure = NULL;
145500dabe28SStefano Zampini     PetscInt closureSize, numCorners = 0, l;
145600dabe28SStefano Zampini     PetscReal normal[3] = {0, 0, 0};
145700dabe28SStefano Zampini 
1458d0fcb9c2SMatthew G. Knepley     if (normals) {ierr = DMPlexComputeCellGeometryFVM(idm, c, NULL, NULL, normal);CHKERRQ(ierr);}
145900dabe28SStefano Zampini     ierr = DMPlexGetTransitiveClosure(idm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
146000dabe28SStefano Zampini     for (v = 0; v < closureSize*2; v += 2) {
146100dabe28SStefano Zampini       if ((closure[v] >= vStart) && (closure[v] < vEnd)) {
146200dabe28SStefano Zampini         PetscInt d;
146300dabe28SStefano Zampini 
146400dabe28SStefano Zampini         newCone[numCorners++] = closure[v] - vStart;
146500dabe28SStefano Zampini         if (normals) {for (d = 0; d < cDim; ++d) normals[cDim*(closure[v]-vStart)+d] += normal[d];}
146600dabe28SStefano Zampini       }
146700dabe28SStefano Zampini     }
146800dabe28SStefano Zampini     ierr = DMPlexRestoreTransitiveClosure(idm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
146900dabe28SStefano Zampini     for (l = 0; l < layers; ++l) {
147000dabe28SStefano Zampini       PetscInt i;
147100dabe28SStefano Zampini 
147200dabe28SStefano Zampini       for (i = 0; i < numCorners; ++i) {
1473d0fcb9c2SMatthew G. Knepley         newCone[  numCorners + i] = orderHeight ? (layers+1)*newCone[i] + l     + numCells :     l*(vEnd - vStart) + newCone[i] + numCells;
1474d0fcb9c2SMatthew G. Knepley         newCone[2*numCorners + i] = orderHeight ? (layers+1)*newCone[i] + l + 1 + numCells : (l+1)*(vEnd - vStart) + newCone[i] + numCells;
147500dabe28SStefano Zampini       }
1476d0fcb9c2SMatthew G. Knepley       ierr = DMPlexSetCone(*dm, orderHeight ? layers*(c - cStart) + l : l*(cEnd - cStart) + c - cStart, newCone + numCorners);CHKERRQ(ierr);
147700dabe28SStefano Zampini     }
147800dabe28SStefano Zampini   }
147900dabe28SStefano Zampini   ierr = DMPlexSymmetrize(*dm);CHKERRQ(ierr);
148000dabe28SStefano Zampini   ierr = DMPlexStratify(*dm);CHKERRQ(ierr);
148100dabe28SStefano Zampini   ierr = PetscFree(newCone);CHKERRQ(ierr);
148200dabe28SStefano Zampini 
148300dabe28SStefano Zampini   ierr = DMGetCoordinateSection(*dm, &coordSectionB);CHKERRQ(ierr);
148400dabe28SStefano Zampini   ierr = PetscSectionSetNumFields(coordSectionB, 1);CHKERRQ(ierr);
148500dabe28SStefano Zampini   ierr = PetscSectionSetFieldComponents(coordSectionB, 0, cDimB);CHKERRQ(ierr);
148600dabe28SStefano Zampini   ierr = PetscSectionSetChart(coordSectionB, numCells, numCells+numVertices);CHKERRQ(ierr);
148700dabe28SStefano Zampini   for (v = numCells; v < numCells+numVertices; ++v) {
148800dabe28SStefano Zampini     ierr = PetscSectionSetDof(coordSectionB, v, cDimB);CHKERRQ(ierr);
148900dabe28SStefano Zampini     ierr = PetscSectionSetFieldDof(coordSectionB, v, 0, cDimB);CHKERRQ(ierr);
1490412e9a14SMatthew G. Knepley     ierr = DMPlexSetCellType(*dm, v, DM_POLYTOPE_POINT);CHKERRQ(ierr);
149100dabe28SStefano Zampini   }
149200dabe28SStefano Zampini   ierr = PetscSectionSetUp(coordSectionB);CHKERRQ(ierr);
149300dabe28SStefano Zampini   ierr = PetscSectionGetStorageSize(coordSectionB, &coordSize);CHKERRQ(ierr);
149400dabe28SStefano Zampini   ierr = VecCreate(PETSC_COMM_SELF, &coordinatesB);CHKERRQ(ierr);
149500dabe28SStefano Zampini   ierr = PetscObjectSetName((PetscObject) coordinatesB, "coordinates");CHKERRQ(ierr);
149600dabe28SStefano Zampini   ierr = VecSetSizes(coordinatesB, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
149700dabe28SStefano Zampini   ierr = VecSetBlockSize(coordinatesB, cDimB);CHKERRQ(ierr);
149800dabe28SStefano Zampini   ierr = VecSetType(coordinatesB,VECSTANDARD);CHKERRQ(ierr);
149900dabe28SStefano Zampini 
150000dabe28SStefano Zampini   ierr = DMGetCoordinateSection(idm, &coordSectionA);CHKERRQ(ierr);
150100dabe28SStefano Zampini   ierr = DMGetCoordinatesLocal(idm, &coordinatesA);CHKERRQ(ierr);
150200dabe28SStefano Zampini   ierr = VecGetArray(coordinatesB, &coordsB);CHKERRQ(ierr);
150300dabe28SStefano Zampini   ierr = VecGetArrayRead(coordinatesA, &coordsA);CHKERRQ(ierr);
150400dabe28SStefano Zampini   for (v = vStart; v < vEnd; ++v) {
150500dabe28SStefano Zampini     const PetscScalar *cptr;
150600dabe28SStefano Zampini     PetscReal         ones2[2] = { 0., 1.}, ones3[3] = { 0., 0., 1.};
1507d0fcb9c2SMatthew G. Knepley     PetscReal         normal[3];
150809c71656SMatthew G. Knepley     PetscReal         norm;
150900dabe28SStefano Zampini     PetscInt          offA, d, cDimA = cDim;
151000dabe28SStefano Zampini 
1511d0fcb9c2SMatthew G. Knepley     if (normals)           {for (d = 0; d < cDimB; ++d) normal[d] = normals[cDimB*(v - vStart)+d];}
1512d0fcb9c2SMatthew G. Knepley     else if (haveCLNormal) {for (d = 0; d < cDimB; ++d) normal[d] = clNormal[d];}
1513d0fcb9c2SMatthew G. Knepley     else if (extNormal)    {for (d = 0; d < cDimB; ++d) normal[d] = extNormal[d];}
1514d0fcb9c2SMatthew G. Knepley     else if (cDimB == 2)   {for (d = 0; d < cDimB; ++d) normal[d] = ones2[d];}
1515d0fcb9c2SMatthew G. Knepley     else if (cDimB == 3)   {for (d = 0; d < cDimB; ++d) normal[d] = ones3[d];}
1516d0fcb9c2SMatthew G. Knepley     else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unable to determine normal for extrusion");
151700dabe28SStefano Zampini     for (d = 0, norm = 0.0; d < cDimB; ++d) norm += normal[d]*normal[d];
151800dabe28SStefano Zampini     for (d = 0; d < cDimB; ++d) normal[d] *= 1./PetscSqrtReal(norm);
151900dabe28SStefano Zampini 
152000dabe28SStefano Zampini     ierr = PetscSectionGetOffset(coordSectionA, v, &offA);CHKERRQ(ierr);
152100dabe28SStefano Zampini     cptr = coordsA + offA;
152209c71656SMatthew G. Knepley     for (l = 0; l <= layers; ++l) {
152300dabe28SStefano Zampini       PetscInt offB, d, newV;
152400dabe28SStefano Zampini 
1525d0fcb9c2SMatthew G. Knepley       newV = orderHeight ? (layers+1)*(v -vStart) + l + numCells : (vEnd -vStart)*l + (v -vStart) + numCells;
152600dabe28SStefano Zampini       ierr = PetscSectionGetOffset(coordSectionB, newV, &offB);CHKERRQ(ierr);
152700dabe28SStefano Zampini       for (d = 0; d < cDimA; ++d) { coordsB[offB+d]  = cptr[d]; }
152809c71656SMatthew G. Knepley       for (d = 0; d < cDimB; ++d) { coordsB[offB+d] += l ? normal[d]*heights[l-1] : 0.0; }
152900dabe28SStefano Zampini       cptr    = coordsB + offB;
153000dabe28SStefano Zampini       cDimA   = cDimB;
153100dabe28SStefano Zampini     }
153200dabe28SStefano Zampini   }
153300dabe28SStefano Zampini   ierr = VecRestoreArrayRead(coordinatesA, &coordsA);CHKERRQ(ierr);
153400dabe28SStefano Zampini   ierr = VecRestoreArray(coordinatesB, &coordsB);CHKERRQ(ierr);
153500dabe28SStefano Zampini   ierr = DMSetCoordinatesLocal(*dm, coordinatesB);CHKERRQ(ierr);
153600dabe28SStefano Zampini   ierr = VecDestroy(&coordinatesB);CHKERRQ(ierr);
153700dabe28SStefano Zampini   ierr = PetscFree(normals);CHKERRQ(ierr);
153809c71656SMatthew G. Knepley   ierr = PetscFree(heights);CHKERRQ(ierr);
153900dabe28SStefano Zampini   if (interpolate) {
154000dabe28SStefano Zampini     DM idm;
154100dabe28SStefano Zampini 
154200dabe28SStefano Zampini     ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr);
154300dabe28SStefano Zampini     ierr = DMPlexCopyCoordinates(*dm, idm);CHKERRQ(ierr);
154400dabe28SStefano Zampini     ierr = DMDestroy(dm);CHKERRQ(ierr);
154500dabe28SStefano Zampini     *dm  = idm;
154600dabe28SStefano Zampini   }
154700dabe28SStefano Zampini   PetscFunctionReturn(0);
154800dabe28SStefano Zampini }
154900dabe28SStefano Zampini 
1550a9074c1eSMatthew G. Knepley /*@C
1551a9074c1eSMatthew G. Knepley   DMPlexSetOptionsPrefix - Sets the prefix used for searching for all DM options in the database.
1552a9074c1eSMatthew G. Knepley 
1553d083f849SBarry Smith   Logically Collective on dm
1554a9074c1eSMatthew G. Knepley 
1555a9074c1eSMatthew G. Knepley   Input Parameters:
1556a9074c1eSMatthew G. Knepley + dm - the DM context
1557a9074c1eSMatthew G. Knepley - prefix - the prefix to prepend to all option names
1558a9074c1eSMatthew G. Knepley 
1559a9074c1eSMatthew G. Knepley   Notes:
1560a9074c1eSMatthew G. Knepley   A hyphen (-) must NOT be given at the beginning of the prefix name.
1561a9074c1eSMatthew G. Knepley   The first character of all runtime options is AUTOMATICALLY the hyphen.
1562a9074c1eSMatthew G. Knepley 
1563a9074c1eSMatthew G. Knepley   Level: advanced
1564a9074c1eSMatthew G. Knepley 
1565a9074c1eSMatthew G. Knepley .seealso: SNESSetFromOptions()
1566a9074c1eSMatthew G. Knepley @*/
1567a9074c1eSMatthew G. Knepley PetscErrorCode DMPlexSetOptionsPrefix(DM dm, const char prefix[])
1568a9074c1eSMatthew G. Knepley {
1569a9074c1eSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
1570a9074c1eSMatthew G. Knepley   PetscErrorCode ierr;
1571a9074c1eSMatthew G. Knepley 
1572a9074c1eSMatthew G. Knepley   PetscFunctionBegin;
1573a9074c1eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1574a9074c1eSMatthew G. Knepley   ierr = PetscObjectSetOptionsPrefix((PetscObject) dm, prefix);CHKERRQ(ierr);
1575a9074c1eSMatthew G. Knepley   ierr = PetscObjectSetOptionsPrefix((PetscObject) mesh->partitioner, prefix);CHKERRQ(ierr);
1576a9074c1eSMatthew G. Knepley   PetscFunctionReturn(0);
1577a9074c1eSMatthew G. Knepley }
1578a9074c1eSMatthew G. Knepley 
1579*9318fe57SMatthew G. Knepley /* Remap geometry to cylinder
1580*9318fe57SMatthew G. Knepley      Interior square: Linear interpolation is correct
1581*9318fe57SMatthew G. Knepley      The other cells all have vertices on rays from the origin. We want to uniformly expand the spacing
1582*9318fe57SMatthew G. Knepley      such that the last vertex is on the unit circle. So the closest and farthest vertices are at distance
15830510c589SMatthew G. Knepley 
1584*9318fe57SMatthew G. Knepley        phi     = arctan(y/x)
1585*9318fe57SMatthew G. Knepley        d_close = sqrt(1/8 + 1/4 sin^2(phi))
1586*9318fe57SMatthew G. Knepley        d_far   = sqrt(1/2 + sin^2(phi))
15870510c589SMatthew G. Knepley 
1588*9318fe57SMatthew G. Knepley      so we remap them using
15890510c589SMatthew G. Knepley 
1590*9318fe57SMatthew G. Knepley        x_new = x_close + (x - x_close) (1 - d_close) / (d_far - d_close)
1591*9318fe57SMatthew G. Knepley        y_new = y_close + (y - y_close) (1 - d_close) / (d_far - d_close)
15920510c589SMatthew G. Knepley 
1593*9318fe57SMatthew G. Knepley      If pi/4 < phi < 3pi/4 or -3pi/4 < phi < -pi/4, then we switch x and y.
1594*9318fe57SMatthew G. Knepley */
1595*9318fe57SMatthew G. Knepley static void snapToCylinder(PetscInt dim, PetscInt Nf, PetscInt NfAux,
1596*9318fe57SMatthew G. Knepley                            const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
1597*9318fe57SMatthew G. Knepley                            const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
1598*9318fe57SMatthew G. Knepley                            PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
1599*9318fe57SMatthew G. Knepley {
1600*9318fe57SMatthew G. Knepley   const PetscReal dis = 1.0/PetscSqrtReal(2.0);
1601*9318fe57SMatthew G. Knepley   const PetscReal ds2 = 0.5*dis;
160222cc497dSMatthew G. Knepley 
1603*9318fe57SMatthew G. Knepley   if ((PetscAbsScalar(u[0]) <= ds2) && (PetscAbsScalar(u[1]) <= ds2)) {
1604*9318fe57SMatthew G. Knepley     f0[0] = u[0];
1605*9318fe57SMatthew G. Knepley     f0[1] = u[1];
1606*9318fe57SMatthew G. Knepley   } else {
1607*9318fe57SMatthew G. Knepley     PetscReal phi, sinp, cosp, dc, df, x, y, xc, yc;
16080510c589SMatthew G. Knepley 
1609*9318fe57SMatthew G. Knepley     x    = PetscRealPart(u[0]);
1610*9318fe57SMatthew G. Knepley     y    = PetscRealPart(u[1]);
1611*9318fe57SMatthew G. Knepley     phi  = PetscAtan2Real(y, x);
1612*9318fe57SMatthew G. Knepley     sinp = PetscSinReal(phi);
1613*9318fe57SMatthew G. Knepley     cosp = PetscCosReal(phi);
1614*9318fe57SMatthew G. Knepley     if ((PetscAbsReal(phi) > PETSC_PI/4.0) && (PetscAbsReal(phi) < 3.0*PETSC_PI/4.0)) {
1615*9318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2/sinp);
1616*9318fe57SMatthew G. Knepley       df = PetscAbsReal(dis/sinp);
1617*9318fe57SMatthew G. Knepley       xc = ds2*x/PetscAbsReal(y);
1618*9318fe57SMatthew G. Knepley       yc = ds2*PetscSignReal(y);
1619*9318fe57SMatthew G. Knepley     } else {
1620*9318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2/cosp);
1621*9318fe57SMatthew G. Knepley       df = PetscAbsReal(dis/cosp);
1622*9318fe57SMatthew G. Knepley       xc = ds2*PetscSignReal(x);
1623*9318fe57SMatthew G. Knepley       yc = ds2*y/PetscAbsReal(x);
1624*9318fe57SMatthew G. Knepley     }
1625*9318fe57SMatthew G. Knepley     f0[0] = xc + (u[0] - xc)*(1.0 - dc)/(df - dc);
1626*9318fe57SMatthew G. Knepley     f0[1] = yc + (u[1] - yc)*(1.0 - dc)/(df - dc);
1627*9318fe57SMatthew G. Knepley   }
1628*9318fe57SMatthew G. Knepley   f0[2] = u[2];
1629*9318fe57SMatthew G. Knepley }
16300510c589SMatthew G. Knepley 
1631*9318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateHexCylinderMesh_Internal(DM dm, DMBoundaryType periodicZ)
16320510c589SMatthew G. Knepley {
16330510c589SMatthew G. Knepley   const PetscInt dim = 3;
1634*9318fe57SMatthew G. Knepley   PetscInt       numCells, numVertices;
1635d8c47e87SMatthew G. Knepley   PetscMPIInt    rank;
16360510c589SMatthew G. Knepley   PetscErrorCode ierr;
16370510c589SMatthew G. Knepley 
16380510c589SMatthew G. Knepley   PetscFunctionBegin;
1639*9318fe57SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
1640*9318fe57SMatthew G. Knepley   ierr = DMSetDimension(dm, dim);CHKERRQ(ierr);
16410510c589SMatthew G. Knepley   /* Create topology */
16420510c589SMatthew G. Knepley   {
16430510c589SMatthew G. Knepley     PetscInt cone[8], c;
16440510c589SMatthew G. Knepley 
1645d8c47e87SMatthew G. Knepley     numCells    = !rank ?  5 : 0;
1646d8c47e87SMatthew G. Knepley     numVertices = !rank ? 16 : 0;
1647006a8963SMatthew G. Knepley     if (periodicZ == DM_BOUNDARY_PERIODIC) {
1648ae8bcbbbSMatthew G. Knepley       numCells   *= 3;
1649d8c47e87SMatthew G. Knepley       numVertices = !rank ? 24 : 0;
1650006a8963SMatthew G. Knepley     }
1651*9318fe57SMatthew G. Knepley     ierr = DMPlexSetChart(dm, 0, numCells+numVertices);CHKERRQ(ierr);
1652*9318fe57SMatthew G. Knepley     for (c = 0; c < numCells; c++) {ierr = DMPlexSetConeSize(dm, c, 8);CHKERRQ(ierr);}
1653*9318fe57SMatthew G. Knepley     ierr = DMSetUp(dm);CHKERRQ(ierr);
1654d8c47e87SMatthew G. Knepley     if (!rank) {
1655006a8963SMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
1656ae8bcbbbSMatthew G. Knepley         cone[0] = 15; cone[1] = 18; cone[2] = 17; cone[3] = 16;
1657ae8bcbbbSMatthew G. Knepley         cone[4] = 31; cone[5] = 32; cone[6] = 33; cone[7] = 34;
1658*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 0, cone);CHKERRQ(ierr);
1659ae8bcbbbSMatthew G. Knepley         cone[0] = 16; cone[1] = 17; cone[2] = 24; cone[3] = 23;
1660ae8bcbbbSMatthew G. Knepley         cone[4] = 32; cone[5] = 36; cone[6] = 37; cone[7] = 33; /* 22 25 26 21 */
1661*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 1, cone);CHKERRQ(ierr);
1662ae8bcbbbSMatthew G. Knepley         cone[0] = 18; cone[1] = 27; cone[2] = 24; cone[3] = 17;
1663ae8bcbbbSMatthew G. Knepley         cone[4] = 34; cone[5] = 33; cone[6] = 37; cone[7] = 38;
1664*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 2, cone);CHKERRQ(ierr);
1665ae8bcbbbSMatthew G. Knepley         cone[0] = 29; cone[1] = 27; cone[2] = 18; cone[3] = 15;
1666ae8bcbbbSMatthew G. Knepley         cone[4] = 35; cone[5] = 31; cone[6] = 34; cone[7] = 38;
1667*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 3, cone);CHKERRQ(ierr);
1668ae8bcbbbSMatthew G. Knepley         cone[0] = 29; cone[1] = 15; cone[2] = 16; cone[3] = 23;
1669ae8bcbbbSMatthew G. Knepley         cone[4] = 35; cone[5] = 36; cone[6] = 32; cone[7] = 31;
1670*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 4, cone);CHKERRQ(ierr);
1671006a8963SMatthew G. Knepley 
1672ae8bcbbbSMatthew G. Knepley         cone[0] = 31; cone[1] = 34; cone[2] = 33; cone[3] = 32;
1673ae8bcbbbSMatthew G. Knepley         cone[4] = 19; cone[5] = 22; cone[6] = 21; cone[7] = 20;
1674*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 5, cone);CHKERRQ(ierr);
1675ae8bcbbbSMatthew G. Knepley         cone[0] = 32; cone[1] = 33; cone[2] = 37; cone[3] = 36;
1676ae8bcbbbSMatthew G. Knepley         cone[4] = 22; cone[5] = 25; cone[6] = 26; cone[7] = 21;
1677*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 6, cone);CHKERRQ(ierr);
1678ae8bcbbbSMatthew G. Knepley         cone[0] = 34; cone[1] = 38; cone[2] = 37; cone[3] = 33;
1679ae8bcbbbSMatthew G. Knepley         cone[4] = 20; cone[5] = 21; cone[6] = 26; cone[7] = 28;
1680*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 7, cone);CHKERRQ(ierr);
1681ae8bcbbbSMatthew G. Knepley         cone[0] = 35; cone[1] = 38; cone[2] = 34; cone[3] = 31;
1682ae8bcbbbSMatthew G. Knepley         cone[4] = 30; cone[5] = 19; cone[6] = 20; cone[7] = 28;
1683*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 8, cone);CHKERRQ(ierr);
1684ae8bcbbbSMatthew G. Knepley         cone[0] = 35; cone[1] = 31; cone[2] = 32; cone[3] = 36;
1685ae8bcbbbSMatthew G. Knepley         cone[4] = 30; cone[5] = 25; cone[6] = 22; cone[7] = 19;
1686*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 9, cone);CHKERRQ(ierr);
1687ae8bcbbbSMatthew G. Knepley 
1688ae8bcbbbSMatthew G. Knepley         cone[0] = 19; cone[1] = 20; cone[2] = 21; cone[3] = 22;
1689ae8bcbbbSMatthew G. Knepley         cone[4] = 15; cone[5] = 16; cone[6] = 17; cone[7] = 18;
1690*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 10, cone);CHKERRQ(ierr);
1691ae8bcbbbSMatthew G. Knepley         cone[0] = 22; cone[1] = 21; cone[2] = 26; cone[3] = 25;
1692ae8bcbbbSMatthew G. Knepley         cone[4] = 16; cone[5] = 23; cone[6] = 24; cone[7] = 17;
1693*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 11, cone);CHKERRQ(ierr);
1694ae8bcbbbSMatthew G. Knepley         cone[0] = 20; cone[1] = 28; cone[2] = 26; cone[3] = 21;
1695ae8bcbbbSMatthew G. Knepley         cone[4] = 18; cone[5] = 17; cone[6] = 24; cone[7] = 27;
1696*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 12, cone);CHKERRQ(ierr);
1697ae8bcbbbSMatthew G. Knepley         cone[0] = 30; cone[1] = 28; cone[2] = 20; cone[3] = 19;
1698ae8bcbbbSMatthew G. Knepley         cone[4] = 29; cone[5] = 15; cone[6] = 18; cone[7] = 27;
1699*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 13, cone);CHKERRQ(ierr);
1700ae8bcbbbSMatthew G. Knepley         cone[0] = 30; cone[1] = 19; cone[2] = 22; cone[3] = 25;
1701ae8bcbbbSMatthew G. Knepley         cone[4] = 29; cone[5] = 23; cone[6] = 16; cone[7] = 15;
1702*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 14, cone);CHKERRQ(ierr);
1703006a8963SMatthew G. Knepley       } else {
170410c6f908SMatthew G. Knepley         cone[0] =  5; cone[1] =  8; cone[2] =  7; cone[3] =  6;
170510c6f908SMatthew G. Knepley         cone[4] =  9; cone[5] = 12; cone[6] = 11; cone[7] = 10;
1706*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 0, cone);CHKERRQ(ierr);
170710c6f908SMatthew G. Knepley         cone[0] =  6; cone[1] =  7; cone[2] = 14; cone[3] = 13;
170810c6f908SMatthew G. Knepley         cone[4] = 12; cone[5] = 15; cone[6] = 16; cone[7] = 11;
1709*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 1, cone);CHKERRQ(ierr);
171010c6f908SMatthew G. Knepley         cone[0] =  8; cone[1] = 17; cone[2] = 14; cone[3] =  7;
171110c6f908SMatthew G. Knepley         cone[4] = 10; cone[5] = 11; cone[6] = 16; cone[7] = 18;
1712*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 2, cone);CHKERRQ(ierr);
171310c6f908SMatthew G. Knepley         cone[0] = 19; cone[1] = 17; cone[2] =  8; cone[3] =  5;
171410c6f908SMatthew G. Knepley         cone[4] = 20; cone[5] =  9; cone[6] = 10; cone[7] = 18;
1715*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 3, cone);CHKERRQ(ierr);
171610c6f908SMatthew G. Knepley         cone[0] = 19; cone[1] =  5; cone[2] =  6; cone[3] = 13;
171710c6f908SMatthew G. Knepley         cone[4] = 20; cone[5] = 15; cone[6] = 12; cone[7] =  9;
1718*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 4, cone);CHKERRQ(ierr);
1719006a8963SMatthew G. Knepley       }
1720d8c47e87SMatthew G. Knepley     }
1721*9318fe57SMatthew G. Knepley     ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
1722*9318fe57SMatthew G. Knepley     ierr = DMPlexStratify(dm);CHKERRQ(ierr);
17230510c589SMatthew G. Knepley   }
1724dbc1dc17SMatthew G. Knepley   /* Create cube geometry */
17250510c589SMatthew G. Knepley   {
17260510c589SMatthew G. Knepley     Vec             coordinates;
17270510c589SMatthew G. Knepley     PetscSection    coordSection;
17280510c589SMatthew G. Knepley     PetscScalar    *coords;
17290510c589SMatthew G. Knepley     PetscInt        coordSize, v;
17300510c589SMatthew G. Knepley     const PetscReal dis = 1.0/PetscSqrtReal(2.0);
17310510c589SMatthew G. Knepley     const PetscReal ds2 = dis/2.0;
17320510c589SMatthew G. Knepley 
17330510c589SMatthew G. Knepley     /* Build coordinates */
1734*9318fe57SMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
17350510c589SMatthew G. Knepley     ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
17360510c589SMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(coordSection, 0, dim);CHKERRQ(ierr);
17370510c589SMatthew G. Knepley     ierr = PetscSectionSetChart(coordSection, numCells, numCells+numVertices);CHKERRQ(ierr);
17380510c589SMatthew G. Knepley     for (v = numCells; v < numCells+numVertices; ++v) {
17390510c589SMatthew G. Knepley       ierr = PetscSectionSetDof(coordSection, v, dim);CHKERRQ(ierr);
17400510c589SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(coordSection, v, 0, dim);CHKERRQ(ierr);
17410510c589SMatthew G. Knepley     }
17420510c589SMatthew G. Knepley     ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
17430510c589SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
17440510c589SMatthew G. Knepley     ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
17450510c589SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
17460510c589SMatthew G. Knepley     ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
17470510c589SMatthew G. Knepley     ierr = VecSetBlockSize(coordinates, dim);CHKERRQ(ierr);
17480510c589SMatthew G. Knepley     ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
17490510c589SMatthew G. Knepley     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
1750d8c47e87SMatthew G. Knepley     if (!rank) {
17510510c589SMatthew G. Knepley       coords[0*dim+0] = -ds2; coords[0*dim+1] = -ds2; coords[0*dim+2] = 0.0;
17520510c589SMatthew G. Knepley       coords[1*dim+0] =  ds2; coords[1*dim+1] = -ds2; coords[1*dim+2] = 0.0;
17530510c589SMatthew G. Knepley       coords[2*dim+0] =  ds2; coords[2*dim+1] =  ds2; coords[2*dim+2] = 0.0;
17540510c589SMatthew G. Knepley       coords[3*dim+0] = -ds2; coords[3*dim+1] =  ds2; coords[3*dim+2] = 0.0;
17550510c589SMatthew G. Knepley       coords[4*dim+0] = -ds2; coords[4*dim+1] = -ds2; coords[4*dim+2] = 1.0;
17560510c589SMatthew G. Knepley       coords[5*dim+0] = -ds2; coords[5*dim+1] =  ds2; coords[5*dim+2] = 1.0;
17570510c589SMatthew G. Knepley       coords[6*dim+0] =  ds2; coords[6*dim+1] =  ds2; coords[6*dim+2] = 1.0;
17580510c589SMatthew G. Knepley       coords[7*dim+0] =  ds2; coords[7*dim+1] = -ds2; coords[7*dim+2] = 1.0;
17590510c589SMatthew G. Knepley       coords[ 8*dim+0] =  dis; coords[ 8*dim+1] = -dis; coords[ 8*dim+2] = 0.0;
17600510c589SMatthew G. Knepley       coords[ 9*dim+0] =  dis; coords[ 9*dim+1] =  dis; coords[ 9*dim+2] = 0.0;
17610510c589SMatthew G. Knepley       coords[10*dim+0] =  dis; coords[10*dim+1] = -dis; coords[10*dim+2] = 1.0;
17620510c589SMatthew G. Knepley       coords[11*dim+0] =  dis; coords[11*dim+1] =  dis; coords[11*dim+2] = 1.0;
17630510c589SMatthew G. Knepley       coords[12*dim+0] = -dis; coords[12*dim+1] =  dis; coords[12*dim+2] = 0.0;
17640510c589SMatthew G. Knepley       coords[13*dim+0] = -dis; coords[13*dim+1] =  dis; coords[13*dim+2] = 1.0;
17650510c589SMatthew G. Knepley       coords[14*dim+0] = -dis; coords[14*dim+1] = -dis; coords[14*dim+2] = 0.0;
17660510c589SMatthew G. Knepley       coords[15*dim+0] = -dis; coords[15*dim+1] = -dis; coords[15*dim+2] = 1.0;
1767ae8bcbbbSMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
1768ae8bcbbbSMatthew G. Knepley         /* 15 31 19 */ coords[16*dim+0] = -ds2; coords[16*dim+1] = -ds2; coords[16*dim+2] = 0.5;
1769ae8bcbbbSMatthew G. Knepley         /* 16 32 22 */ coords[17*dim+0] =  ds2; coords[17*dim+1] = -ds2; coords[17*dim+2] = 0.5;
1770ae8bcbbbSMatthew G. Knepley         /* 17 33 21 */ coords[18*dim+0] =  ds2; coords[18*dim+1] =  ds2; coords[18*dim+2] = 0.5;
1771ae8bcbbbSMatthew G. Knepley         /* 18 34 20 */ coords[19*dim+0] = -ds2; coords[19*dim+1] =  ds2; coords[19*dim+2] = 0.5;
1772ae8bcbbbSMatthew G. Knepley         /* 29 35 30 */ coords[20*dim+0] = -dis; coords[20*dim+1] = -dis; coords[20*dim+2] = 0.5;
1773ae8bcbbbSMatthew G. Knepley         /* 23 36 25 */ coords[21*dim+0] =  dis; coords[21*dim+1] = -dis; coords[21*dim+2] = 0.5;
1774ae8bcbbbSMatthew G. Knepley         /* 24 37 26 */ coords[22*dim+0] =  dis; coords[22*dim+1] =  dis; coords[22*dim+2] = 0.5;
1775ae8bcbbbSMatthew G. Knepley         /* 27 38 28 */ coords[23*dim+0] = -dis; coords[23*dim+1] =  dis; coords[23*dim+2] = 0.5;
1776ae8bcbbbSMatthew G. Knepley       }
1777d8c47e87SMatthew G. Knepley     }
17780510c589SMatthew G. Knepley     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
1779*9318fe57SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
17800510c589SMatthew G. Knepley     ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
17810510c589SMatthew G. Knepley   }
1782006a8963SMatthew G. Knepley   /* Create periodicity */
1783006a8963SMatthew G. Knepley   if (periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
1784006a8963SMatthew G. Knepley     PetscReal      L[3];
1785006a8963SMatthew G. Knepley     PetscReal      maxCell[3];
1786006a8963SMatthew G. Knepley     DMBoundaryType bdType[3];
1787006a8963SMatthew G. Knepley     PetscReal      lower[3] = {0.0, 0.0, 0.0};
1788ae8bcbbbSMatthew G. Knepley     PetscReal      upper[3] = {1.0, 1.0, 1.5};
1789ae8bcbbbSMatthew G. Knepley     PetscInt       i, numZCells = 3;
1790006a8963SMatthew G. Knepley 
1791006a8963SMatthew G. Knepley     bdType[0] = DM_BOUNDARY_NONE;
1792006a8963SMatthew G. Knepley     bdType[1] = DM_BOUNDARY_NONE;
1793006a8963SMatthew G. Knepley     bdType[2] = periodicZ;
1794006a8963SMatthew G. Knepley     for (i = 0; i < dim; i++) {
1795006a8963SMatthew G. Knepley       L[i]       = upper[i] - lower[i];
1796006a8963SMatthew G. Knepley       maxCell[i] = 1.1 * (L[i] / numZCells);
1797006a8963SMatthew G. Knepley     }
1798*9318fe57SMatthew G. Knepley     ierr = DMSetPeriodicity(dm, PETSC_TRUE, maxCell, L, bdType);CHKERRQ(ierr);
1799006a8963SMatthew G. Knepley   }
1800dbc1dc17SMatthew G. Knepley   {
1801*9318fe57SMatthew G. Knepley     DM          cdm;
1802*9318fe57SMatthew G. Knepley     PetscDS     cds;
1803*9318fe57SMatthew G. Knepley     PetscScalar c[2] = {1.0, 1.0};
1804dbc1dc17SMatthew G. Knepley 
1805*9318fe57SMatthew G. Knepley     ierr = DMPlexCreateCoordinateSpace(dm, 1, snapToCylinder);CHKERRQ(ierr);
1806*9318fe57SMatthew G. Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
1807*9318fe57SMatthew G. Knepley     ierr = DMGetDS(cdm, &cds);CHKERRQ(ierr);
1808*9318fe57SMatthew G. Knepley     ierr = PetscDSSetConstants(cds, 2, c);CHKERRQ(ierr);
1809dbc1dc17SMatthew G. Knepley   }
1810*9318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
1811*9318fe57SMatthew G. Knepley   ierr = DMPlexInterpolateInPlace_Internal(dm);CHKERRQ(ierr);
18120510c589SMatthew G. Knepley   PetscFunctionReturn(0);
18130510c589SMatthew G. Knepley }
18140510c589SMatthew G. Knepley 
181524119c2aSMatthew G. Knepley /*@
1816*9318fe57SMatthew G. Knepley   DMPlexCreateHexCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using hexahedra.
181724119c2aSMatthew G. Knepley 
1818d083f849SBarry Smith   Collective
181924119c2aSMatthew G. Knepley 
182024119c2aSMatthew G. Knepley   Input Parameters:
182124119c2aSMatthew G. Knepley + comm      - The communicator for the DM object
1822*9318fe57SMatthew G. Knepley - periodicZ - The boundary type for the Z direction
182324119c2aSMatthew G. Knepley 
182424119c2aSMatthew G. Knepley   Output Parameter:
182524119c2aSMatthew G. Knepley . dm  - The DM object
182624119c2aSMatthew G. Knepley 
1827*9318fe57SMatthew G. Knepley   Note:
1828*9318fe57SMatthew G. Knepley   Here is the output numbering looking from the bottom of the cylinder:
1829*9318fe57SMatthew G. Knepley $       17-----14
1830*9318fe57SMatthew G. Knepley $        |     |
1831*9318fe57SMatthew G. Knepley $        |  2  |
1832*9318fe57SMatthew G. Knepley $        |     |
1833*9318fe57SMatthew G. Knepley $ 17-----8-----7-----14
1834*9318fe57SMatthew G. Knepley $  |     |     |     |
1835*9318fe57SMatthew G. Knepley $  |  3  |  0  |  1  |
1836*9318fe57SMatthew G. Knepley $  |     |     |     |
1837*9318fe57SMatthew G. Knepley $ 19-----5-----6-----13
1838*9318fe57SMatthew G. Knepley $        |     |
1839*9318fe57SMatthew G. Knepley $        |  4  |
1840*9318fe57SMatthew G. Knepley $        |     |
1841*9318fe57SMatthew G. Knepley $       19-----13
1842*9318fe57SMatthew G. Knepley $
1843*9318fe57SMatthew G. Knepley $ and up through the top
1844*9318fe57SMatthew G. Knepley $
1845*9318fe57SMatthew G. Knepley $       18-----16
1846*9318fe57SMatthew G. Knepley $        |     |
1847*9318fe57SMatthew G. Knepley $        |  2  |
1848*9318fe57SMatthew G. Knepley $        |     |
1849*9318fe57SMatthew G. Knepley $ 18----10----11-----16
1850*9318fe57SMatthew G. Knepley $  |     |     |     |
1851*9318fe57SMatthew G. Knepley $  |  3  |  0  |  1  |
1852*9318fe57SMatthew G. Knepley $  |     |     |     |
1853*9318fe57SMatthew G. Knepley $ 20-----9----12-----15
1854*9318fe57SMatthew G. Knepley $        |     |
1855*9318fe57SMatthew G. Knepley $        |  4  |
1856*9318fe57SMatthew G. Knepley $        |     |
1857*9318fe57SMatthew G. Knepley $       20-----15
1858*9318fe57SMatthew G. Knepley 
185924119c2aSMatthew G. Knepley   Level: beginner
186024119c2aSMatthew G. Knepley 
1861*9318fe57SMatthew G. Knepley .seealso: DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
186224119c2aSMatthew G. Knepley @*/
1863*9318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateHexCylinderMesh(MPI_Comm comm, DMBoundaryType periodicZ, DM *dm)
1864*9318fe57SMatthew G. Knepley {
1865*9318fe57SMatthew G. Knepley   PetscErrorCode ierr;
1866*9318fe57SMatthew G. Knepley 
1867*9318fe57SMatthew G. Knepley   PetscFunctionBegin;
1868*9318fe57SMatthew G. Knepley   PetscValidPointer(dm, 3);
1869*9318fe57SMatthew G. Knepley   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
1870*9318fe57SMatthew G. Knepley   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
1871*9318fe57SMatthew G. Knepley   ierr = DMPlexCreateHexCylinderMesh_Internal(*dm, periodicZ);CHKERRQ(ierr);
1872*9318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
1873*9318fe57SMatthew G. Knepley }
1874*9318fe57SMatthew G. Knepley 
1875*9318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateWedgeCylinderMesh_Internal(DM dm, PetscInt n, PetscBool interpolate)
187624119c2aSMatthew G. Knepley {
187724119c2aSMatthew G. Knepley   const PetscInt dim = 3;
1878412e9a14SMatthew G. Knepley   PetscInt       numCells, numVertices, v;
18799fe9f049SMatthew G. Knepley   PetscMPIInt    rank;
188024119c2aSMatthew G. Knepley   PetscErrorCode ierr;
188124119c2aSMatthew G. Knepley 
188224119c2aSMatthew G. Knepley   PetscFunctionBegin;
1883*9318fe57SMatthew G. Knepley   if (n < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of wedges %D cannot be negative", n);
1884*9318fe57SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
1885*9318fe57SMatthew G. Knepley   ierr = DMSetDimension(dm, dim);CHKERRQ(ierr);
1886412e9a14SMatthew G. Knepley   /* Must create the celltype label here so that we do not automatically try to compute the types */
1887*9318fe57SMatthew G. Knepley   ierr = DMCreateLabel(dm, "celltype");CHKERRQ(ierr);
188824119c2aSMatthew G. Knepley   /* Create topology */
188924119c2aSMatthew G. Knepley   {
189024119c2aSMatthew G. Knepley     PetscInt cone[6], c;
189124119c2aSMatthew G. Knepley 
18929fe9f049SMatthew G. Knepley     numCells    = !rank ?        n : 0;
18939fe9f049SMatthew G. Knepley     numVertices = !rank ?  2*(n+1) : 0;
1894*9318fe57SMatthew G. Knepley     ierr = DMPlexSetChart(dm, 0, numCells+numVertices);CHKERRQ(ierr);
1895*9318fe57SMatthew G. Knepley     for (c = 0; c < numCells; c++) {ierr = DMPlexSetConeSize(dm, c, 6);CHKERRQ(ierr);}
1896*9318fe57SMatthew G. Knepley     ierr = DMSetUp(dm);CHKERRQ(ierr);
189724119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
189824119c2aSMatthew G. Knepley       cone[0] =  c+n*1; cone[1] = (c+1)%n+n*1; cone[2] = 0+3*n;
189924119c2aSMatthew G. Knepley       cone[3] =  c+n*2; cone[4] = (c+1)%n+n*2; cone[5] = 1+3*n;
1900*9318fe57SMatthew G. Knepley       ierr = DMPlexSetCone(dm, c, cone);CHKERRQ(ierr);
1901*9318fe57SMatthew G. Knepley       ierr = DMPlexSetCellType(dm, c, DM_POLYTOPE_TRI_PRISM_TENSOR);CHKERRQ(ierr);
190224119c2aSMatthew G. Knepley     }
1903*9318fe57SMatthew G. Knepley     ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
1904*9318fe57SMatthew G. Knepley     ierr = DMPlexStratify(dm);CHKERRQ(ierr);
190524119c2aSMatthew G. Knepley   }
1906412e9a14SMatthew G. Knepley   for (v = numCells; v < numCells+numVertices; ++v) {
1907*9318fe57SMatthew G. Knepley     ierr = DMPlexSetCellType(dm, v, DM_POLYTOPE_POINT);CHKERRQ(ierr);
190824119c2aSMatthew G. Knepley   }
190924119c2aSMatthew G. Knepley   /* Create cylinder geometry */
191024119c2aSMatthew G. Knepley   {
191124119c2aSMatthew G. Knepley     Vec          coordinates;
191224119c2aSMatthew G. Knepley     PetscSection coordSection;
191324119c2aSMatthew G. Knepley     PetscScalar *coords;
1914412e9a14SMatthew G. Knepley     PetscInt     coordSize, c;
191524119c2aSMatthew G. Knepley 
191624119c2aSMatthew G. Knepley     /* Build coordinates */
1917*9318fe57SMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
191824119c2aSMatthew G. Knepley     ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
191924119c2aSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(coordSection, 0, dim);CHKERRQ(ierr);
192024119c2aSMatthew G. Knepley     ierr = PetscSectionSetChart(coordSection, numCells, numCells+numVertices);CHKERRQ(ierr);
192124119c2aSMatthew G. Knepley     for (v = numCells; v < numCells+numVertices; ++v) {
192224119c2aSMatthew G. Knepley       ierr = PetscSectionSetDof(coordSection, v, dim);CHKERRQ(ierr);
192324119c2aSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(coordSection, v, 0, dim);CHKERRQ(ierr);
192424119c2aSMatthew G. Knepley     }
192524119c2aSMatthew G. Knepley     ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
192624119c2aSMatthew G. Knepley     ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
192724119c2aSMatthew G. Knepley     ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
192824119c2aSMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
192924119c2aSMatthew G. Knepley     ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
193024119c2aSMatthew G. Knepley     ierr = VecSetBlockSize(coordinates, dim);CHKERRQ(ierr);
193124119c2aSMatthew G. Knepley     ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
193224119c2aSMatthew G. Knepley     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
193324119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
193424119c2aSMatthew G. Knepley       coords[(c+0*n)*dim+0] = PetscCosReal(2.0*c*PETSC_PI/n); coords[(c+0*n)*dim+1] = PetscSinReal(2.0*c*PETSC_PI/n); coords[(c+0*n)*dim+2] = 1.0;
193524119c2aSMatthew G. Knepley       coords[(c+1*n)*dim+0] = PetscCosReal(2.0*c*PETSC_PI/n); coords[(c+1*n)*dim+1] = PetscSinReal(2.0*c*PETSC_PI/n); coords[(c+1*n)*dim+2] = 0.0;
193624119c2aSMatthew G. Knepley     }
19379fe9f049SMatthew G. Knepley     if (!rank) {
193824119c2aSMatthew G. Knepley       coords[(2*n+0)*dim+0] = 0.0; coords[(2*n+0)*dim+1] = 0.0; coords[(2*n+0)*dim+2] = 1.0;
193924119c2aSMatthew G. Knepley       coords[(2*n+1)*dim+0] = 0.0; coords[(2*n+1)*dim+1] = 0.0; coords[(2*n+1)*dim+2] = 0.0;
19409fe9f049SMatthew G. Knepley     }
194124119c2aSMatthew G. Knepley     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
1942*9318fe57SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
194324119c2aSMatthew G. Knepley     ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
194424119c2aSMatthew G. Knepley   }
1945*9318fe57SMatthew G. Knepley   /* Interpolate */
1946*9318fe57SMatthew G. Knepley   if (interpolate) {ierr = DMPlexInterpolateInPlace_Internal(dm);CHKERRQ(ierr);}
1947*9318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
1948*9318fe57SMatthew G. Knepley }
1949*9318fe57SMatthew G. Knepley 
1950*9318fe57SMatthew G. Knepley /*@
1951*9318fe57SMatthew G. Knepley   DMPlexCreateWedgeCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using wedges.
1952*9318fe57SMatthew G. Knepley 
1953*9318fe57SMatthew G. Knepley   Collective
1954*9318fe57SMatthew G. Knepley 
1955*9318fe57SMatthew G. Knepley   Input Parameters:
1956*9318fe57SMatthew G. Knepley + comm - The communicator for the DM object
1957*9318fe57SMatthew G. Knepley . n    - The number of wedges around the origin
1958*9318fe57SMatthew G. Knepley - interpolate - Create edges and faces
1959*9318fe57SMatthew G. Knepley 
1960*9318fe57SMatthew G. Knepley   Output Parameter:
1961*9318fe57SMatthew G. Knepley . dm  - The DM object
1962*9318fe57SMatthew G. Knepley 
1963*9318fe57SMatthew G. Knepley   Level: beginner
1964*9318fe57SMatthew G. Knepley 
1965*9318fe57SMatthew G. Knepley .seealso: DMPlexCreateHexCylinderMesh(), DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
1966*9318fe57SMatthew G. Knepley @*/
1967*9318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateWedgeCylinderMesh(MPI_Comm comm, PetscInt n, PetscBool interpolate, DM *dm)
1968*9318fe57SMatthew G. Knepley {
1969*9318fe57SMatthew G. Knepley   PetscErrorCode ierr;
1970*9318fe57SMatthew G. Knepley 
1971*9318fe57SMatthew G. Knepley   PetscFunctionBegin;
1972*9318fe57SMatthew G. Knepley   PetscValidPointer(dm, 4);
1973*9318fe57SMatthew G. Knepley   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
1974*9318fe57SMatthew G. Knepley   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
1975*9318fe57SMatthew G. Knepley   ierr = DMPlexCreateWedgeCylinderMesh_Internal(*dm, n, interpolate);CHKERRQ(ierr);
197624119c2aSMatthew G. Knepley   PetscFunctionReturn(0);
197724119c2aSMatthew G. Knepley }
197824119c2aSMatthew G. Knepley 
197965a81367SMatthew G. Knepley PETSC_STATIC_INLINE PetscReal DiffNormReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
198065a81367SMatthew G. Knepley {
198165a81367SMatthew G. Knepley   PetscReal prod = 0.0;
198265a81367SMatthew G. Knepley   PetscInt  i;
198365a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += PetscSqr(x[i] - y[i]);
198465a81367SMatthew G. Knepley   return PetscSqrtReal(prod);
198565a81367SMatthew G. Knepley }
198665a81367SMatthew G. Knepley PETSC_STATIC_INLINE PetscReal DotReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
198765a81367SMatthew G. Knepley {
198865a81367SMatthew G. Knepley   PetscReal prod = 0.0;
198965a81367SMatthew G. Knepley   PetscInt  i;
199065a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += x[i]*y[i];
199165a81367SMatthew G. Knepley   return prod;
199265a81367SMatthew G. Knepley }
199365a81367SMatthew G. Knepley 
199451a74b61SMatthew G. Knepley /* The first constant is the sphere radius */
199551a74b61SMatthew G. Knepley static void snapToSphere(PetscInt dim, PetscInt Nf, PetscInt NfAux,
199651a74b61SMatthew G. Knepley                          const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
199751a74b61SMatthew G. Knepley                          const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
199851a74b61SMatthew G. Knepley                          PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
199951a74b61SMatthew G. Knepley {
200051a74b61SMatthew G. Knepley   PetscReal r = PetscRealPart(constants[0]);
200151a74b61SMatthew G. Knepley   PetscReal norm2 = 0.0, fac;
200251a74b61SMatthew G. Knepley   PetscInt  n = uOff[1] - uOff[0], d;
200351a74b61SMatthew G. Knepley 
200451a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) norm2 += PetscSqr(PetscRealPart(u[d]));
200551a74b61SMatthew G. Knepley   fac = r/PetscSqrtReal(norm2);
200651a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) f0[d] = u[d]*fac;
200751a74b61SMatthew G. Knepley }
200851a74b61SMatthew G. Knepley 
2009*9318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateSphereMesh_Internal(DM dm, PetscInt dim, PetscBool simplex, PetscReal R)
20102829fed8SMatthew G. Knepley {
201165a81367SMatthew G. Knepley   const PetscInt  embedDim = dim+1;
201265a81367SMatthew G. Knepley   PetscSection    coordSection;
201365a81367SMatthew G. Knepley   Vec             coordinates;
201465a81367SMatthew G. Knepley   PetscScalar    *coords;
201565a81367SMatthew G. Knepley   PetscReal      *coordsIn;
201665a81367SMatthew G. Knepley   PetscInt        numCells, numEdges, numVerts, firstVertex, v, firstEdge, coordSize, d, c, e;
201765a81367SMatthew G. Knepley   PetscMPIInt     rank;
201865a81367SMatthew G. Knepley   PetscErrorCode  ierr;
201965a81367SMatthew G. Knepley 
202065a81367SMatthew G. Knepley   PetscFunctionBegin;
2021*9318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveBool(dm, simplex, 3);
2022*9318fe57SMatthew G. Knepley   ierr = DMSetDimension(dm, dim);CHKERRQ(ierr);
2023*9318fe57SMatthew G. Knepley   ierr = DMSetCoordinateDim(dm, dim+1);CHKERRQ(ierr);
2024*9318fe57SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
202565a81367SMatthew G. Knepley   switch (dim) {
202665a81367SMatthew G. Knepley   case 2:
202765a81367SMatthew G. Knepley     if (simplex) {
202851a74b61SMatthew G. Knepley       const PetscReal radius    = PetscSqrtReal(1 + PETSC_PHI*PETSC_PHI)/(1.0 + PETSC_PHI);
202951a74b61SMatthew G. Knepley       const PetscReal edgeLen   = 2.0/(1.0 + PETSC_PHI) * (R/radius);
203065a81367SMatthew G. Knepley       const PetscInt  degree    = 5;
203151a74b61SMatthew G. Knepley       PetscReal       vertex[3] = {0.0, 1.0/(1.0 + PETSC_PHI), PETSC_PHI/(1.0 + PETSC_PHI)};
203265a81367SMatthew G. Knepley       PetscInt        s[3]      = {1, 1, 1};
203365a81367SMatthew G. Knepley       PetscInt        cone[3];
203465a81367SMatthew G. Knepley       PetscInt       *graph, p, i, j, k;
203565a81367SMatthew G. Knepley 
203651a74b61SMatthew G. Knepley       vertex[0] *= R/radius; vertex[1] *= R/radius; vertex[2] *= R/radius;
203765a81367SMatthew G. Knepley       numCells    = !rank ? 20 : 0;
203865a81367SMatthew G. Knepley       numVerts    = !rank ? 12 : 0;
203965a81367SMatthew G. Knepley       firstVertex = numCells;
204051a74b61SMatthew G. Knepley       /* Use icosahedron, which for a R-sphere has coordinates which are all cyclic permutations of
204165a81367SMatthew G. Knepley 
204265a81367SMatthew G. Knepley            (0, \pm 1/\phi+1, \pm \phi/\phi+1)
204365a81367SMatthew G. Knepley 
204465a81367SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
204551a74b61SMatthew G. Knepley          length is then given by 2/(1+\phi) = 2 * 0.38197 = 0.76393.
204665a81367SMatthew G. Knepley       */
204765a81367SMatthew G. Knepley       /* Construct vertices */
204865a81367SMatthew G. Knepley       ierr = PetscCalloc1(numVerts * embedDim, &coordsIn);CHKERRQ(ierr);
204945da822fSValeria Barra       if (!rank) {
205065a81367SMatthew G. Knepley         for (p = 0, i = 0; p < embedDim; ++p) {
205165a81367SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
205265a81367SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
205365a81367SMatthew G. Knepley               for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[(d+p)%embedDim]*vertex[(d+p)%embedDim];
205465a81367SMatthew G. Knepley               ++i;
205565a81367SMatthew G. Knepley             }
205665a81367SMatthew G. Knepley           }
205765a81367SMatthew G. Knepley         }
205845da822fSValeria Barra       }
205965a81367SMatthew G. Knepley       /* Construct graph */
206065a81367SMatthew G. Knepley       ierr = PetscCalloc1(numVerts * numVerts, &graph);CHKERRQ(ierr);
206165a81367SMatthew G. Knepley       for (i = 0; i < numVerts; ++i) {
206265a81367SMatthew G. Knepley         for (j = 0, k = 0; j < numVerts; ++j) {
206365a81367SMatthew G. Knepley           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i*embedDim], &coordsIn[j*embedDim]) - edgeLen) < PETSC_SMALL) {graph[i*numVerts+j] = 1; ++k;}
206465a81367SMatthew G. Knepley         }
2065*9318fe57SMatthew G. Knepley         if (k != degree) SETERRQ3(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid icosahedron, vertex %D degree %D != %D", i, k, degree);
206665a81367SMatthew G. Knepley       }
206765a81367SMatthew G. Knepley       /* Build Topology */
2068*9318fe57SMatthew G. Knepley       ierr = DMPlexSetChart(dm, 0, numCells+numVerts);CHKERRQ(ierr);
206965a81367SMatthew G. Knepley       for (c = 0; c < numCells; c++) {
2070*9318fe57SMatthew G. Knepley         ierr = DMPlexSetConeSize(dm, c, embedDim);CHKERRQ(ierr);
207165a81367SMatthew G. Knepley       }
2072*9318fe57SMatthew G. Knepley       ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
207365a81367SMatthew G. Knepley       /* Cells */
207465a81367SMatthew G. Knepley       for (i = 0, c = 0; i < numVerts; ++i) {
207565a81367SMatthew G. Knepley         for (j = 0; j < i; ++j) {
207665a81367SMatthew G. Knepley           for (k = 0; k < j; ++k) {
207765a81367SMatthew G. Knepley             if (graph[i*numVerts+j] && graph[j*numVerts+k] && graph[k*numVerts+i]) {
207865a81367SMatthew G. Knepley               cone[0] = firstVertex+i; cone[1] = firstVertex+j; cone[2] = firstVertex+k;
207965a81367SMatthew G. Knepley               /* Check orientation */
208065a81367SMatthew G. Knepley               {
208165a81367SMatthew G. Knepley                 const PetscInt epsilon[3][3][3] = {{{0, 0, 0}, {0, 0, 1}, {0, -1, 0}}, {{0, 0, -1}, {0, 0, 0}, {1, 0, 0}}, {{0, 1, 0}, {-1, 0, 0}, {0, 0, 0}}};
208265a81367SMatthew G. Knepley                 PetscReal normal[3];
208365a81367SMatthew G. Knepley                 PetscInt  e, f;
208465a81367SMatthew G. Knepley 
208565a81367SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) {
208665a81367SMatthew G. Knepley                   normal[d] = 0.0;
208765a81367SMatthew G. Knepley                   for (e = 0; e < embedDim; ++e) {
208865a81367SMatthew G. Knepley                     for (f = 0; f < embedDim; ++f) {
208965a81367SMatthew G. Knepley                       normal[d] += epsilon[d][e][f]*(coordsIn[j*embedDim+e] - coordsIn[i*embedDim+e])*(coordsIn[k*embedDim+f] - coordsIn[i*embedDim+f]);
209065a81367SMatthew G. Knepley                     }
209165a81367SMatthew G. Knepley                   }
209265a81367SMatthew G. Knepley                 }
209365a81367SMatthew G. Knepley                 if (DotReal(embedDim, normal, &coordsIn[i*embedDim]) < 0) {PetscInt tmp = cone[1]; cone[1] = cone[2]; cone[2] = tmp;}
209465a81367SMatthew G. Knepley               }
2095*9318fe57SMatthew G. Knepley               ierr = DMPlexSetCone(dm, c++, cone);CHKERRQ(ierr);
209665a81367SMatthew G. Knepley             }
209765a81367SMatthew G. Knepley           }
209865a81367SMatthew G. Knepley         }
209965a81367SMatthew G. Knepley       }
2100*9318fe57SMatthew G. Knepley       ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
2101*9318fe57SMatthew G. Knepley       ierr = DMPlexStratify(dm);CHKERRQ(ierr);
210265a81367SMatthew G. Knepley       ierr = PetscFree(graph);CHKERRQ(ierr);
210365a81367SMatthew G. Knepley     } else {
21042829fed8SMatthew G. Knepley       /*
21052829fed8SMatthew G. Knepley         12-21--13
21062829fed8SMatthew G. Knepley          |     |
21072829fed8SMatthew G. Knepley         25  4  24
21082829fed8SMatthew G. Knepley          |     |
21092829fed8SMatthew G. Knepley   12-25--9-16--8-24--13
21102829fed8SMatthew G. Knepley    |     |     |     |
21112829fed8SMatthew G. Knepley   23  5 17  0 15  3  22
21122829fed8SMatthew G. Knepley    |     |     |     |
21132829fed8SMatthew G. Knepley   10-20--6-14--7-19--11
21142829fed8SMatthew G. Knepley          |     |
21152829fed8SMatthew G. Knepley         20  1  19
21162829fed8SMatthew G. Knepley          |     |
21172829fed8SMatthew G. Knepley         10-18--11
21182829fed8SMatthew G. Knepley          |     |
21192829fed8SMatthew G. Knepley         23  2  22
21202829fed8SMatthew G. Knepley          |     |
21212829fed8SMatthew G. Knepley         12-21--13
21222829fed8SMatthew G. Knepley        */
21232829fed8SMatthew G. Knepley       PetscInt cone[4], ornt[4];
21242829fed8SMatthew G. Knepley 
212565a81367SMatthew G. Knepley       numCells    = !rank ?  6 : 0;
212665a81367SMatthew G. Knepley       numEdges    = !rank ? 12 : 0;
212765a81367SMatthew G. Knepley       numVerts    = !rank ?  8 : 0;
212865a81367SMatthew G. Knepley       firstVertex = numCells;
212965a81367SMatthew G. Knepley       firstEdge   = numCells + numVerts;
21302829fed8SMatthew G. Knepley       /* Build Topology */
2131*9318fe57SMatthew G. Knepley       ierr = DMPlexSetChart(dm, 0, numCells+numEdges+numVerts);CHKERRQ(ierr);
21322829fed8SMatthew G. Knepley       for (c = 0; c < numCells; c++) {
2133*9318fe57SMatthew G. Knepley         ierr = DMPlexSetConeSize(dm, c, 4);CHKERRQ(ierr);
21342829fed8SMatthew G. Knepley       }
21352829fed8SMatthew G. Knepley       for (e = firstEdge; e < firstEdge+numEdges; ++e) {
2136*9318fe57SMatthew G. Knepley         ierr = DMPlexSetConeSize(dm, e, 2);CHKERRQ(ierr);
21372829fed8SMatthew G. Knepley       }
2138*9318fe57SMatthew G. Knepley       ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
213945da822fSValeria Barra       if (!rank) {
21402829fed8SMatthew G. Knepley         /* Cell 0 */
21412829fed8SMatthew G. Knepley         cone[0] = 14; cone[1] = 15; cone[2] = 16; cone[3] = 17;
2142*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 0, cone);CHKERRQ(ierr);
21432829fed8SMatthew G. Knepley         ornt[0] = 0; ornt[1] = 0; ornt[2] = 0; ornt[3] = 0;
2144*9318fe57SMatthew G. Knepley         ierr = DMPlexSetConeOrientation(dm, 0, ornt);CHKERRQ(ierr);
21452829fed8SMatthew G. Knepley         /* Cell 1 */
21462829fed8SMatthew G. Knepley         cone[0] = 18; cone[1] = 19; cone[2] = 14; cone[3] = 20;
2147*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 1, cone);CHKERRQ(ierr);
21482829fed8SMatthew G. Knepley         ornt[0] = 0; ornt[1] = 0; ornt[2] = -2; ornt[3] = 0;
2149*9318fe57SMatthew G. Knepley         ierr = DMPlexSetConeOrientation(dm, 1, ornt);CHKERRQ(ierr);
21502829fed8SMatthew G. Knepley         /* Cell 2 */
21512829fed8SMatthew G. Knepley         cone[0] = 21; cone[1] = 22; cone[2] = 18; cone[3] = 23;
2152*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 2, cone);CHKERRQ(ierr);
21532829fed8SMatthew G. Knepley         ornt[0] = 0; ornt[1] = 0; ornt[2] = -2; ornt[3] = 0;
2154*9318fe57SMatthew G. Knepley         ierr = DMPlexSetConeOrientation(dm, 2, ornt);CHKERRQ(ierr);
21552829fed8SMatthew G. Knepley         /* Cell 3 */
21562829fed8SMatthew G. Knepley         cone[0] = 19; cone[1] = 22; cone[2] = 24; cone[3] = 15;
2157*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 3, cone);CHKERRQ(ierr);
21582829fed8SMatthew G. Knepley         ornt[0] = -2; ornt[1] = -2; ornt[2] = 0; ornt[3] = -2;
2159*9318fe57SMatthew G. Knepley         ierr = DMPlexSetConeOrientation(dm, 3, ornt);CHKERRQ(ierr);
21602829fed8SMatthew G. Knepley         /* Cell 4 */
21612829fed8SMatthew G. Knepley         cone[0] = 16; cone[1] = 24; cone[2] = 21; cone[3] = 25;
2162*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 4, cone);CHKERRQ(ierr);
21632829fed8SMatthew G. Knepley         ornt[0] = -2; ornt[1] = -2; ornt[2] = -2; ornt[3] = 0;
2164*9318fe57SMatthew G. Knepley         ierr = DMPlexSetConeOrientation(dm, 4, ornt);CHKERRQ(ierr);
21652829fed8SMatthew G. Knepley         /* Cell 5 */
21662829fed8SMatthew G. Knepley         cone[0] = 20; cone[1] = 17; cone[2] = 25; cone[3] = 23;
2167*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 5, cone);CHKERRQ(ierr);
21682829fed8SMatthew G. Knepley         ornt[0] = -2; ornt[1] = -2; ornt[2] = -2; ornt[3] = -2;
2169*9318fe57SMatthew G. Knepley         ierr = DMPlexSetConeOrientation(dm, 5, ornt);CHKERRQ(ierr);
21702829fed8SMatthew G. Knepley         /* Edges */
21712829fed8SMatthew G. Knepley         cone[0] =  6; cone[1] =  7;
2172*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 14, cone);CHKERRQ(ierr);
21732829fed8SMatthew G. Knepley         cone[0] =  7; cone[1] =  8;
2174*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 15, cone);CHKERRQ(ierr);
21752829fed8SMatthew G. Knepley         cone[0] =  8; cone[1] =  9;
2176*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 16, cone);CHKERRQ(ierr);
21772829fed8SMatthew G. Knepley         cone[0] =  9; cone[1] =  6;
2178*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 17, cone);CHKERRQ(ierr);
21792829fed8SMatthew G. Knepley         cone[0] = 10; cone[1] = 11;
2180*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 18, cone);CHKERRQ(ierr);
21812829fed8SMatthew G. Knepley         cone[0] = 11; cone[1] =  7;
2182*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 19, cone);CHKERRQ(ierr);
21832829fed8SMatthew G. Knepley         cone[0] =  6; cone[1] = 10;
2184*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 20, cone);CHKERRQ(ierr);
21852829fed8SMatthew G. Knepley         cone[0] = 12; cone[1] = 13;
2186*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 21, cone);CHKERRQ(ierr);
21872829fed8SMatthew G. Knepley         cone[0] = 13; cone[1] = 11;
2188*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 22, cone);CHKERRQ(ierr);
21892829fed8SMatthew G. Knepley         cone[0] = 10; cone[1] = 12;
2190*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 23, cone);CHKERRQ(ierr);
21912829fed8SMatthew G. Knepley         cone[0] = 13; cone[1] =  8;
2192*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 24, cone);CHKERRQ(ierr);
21932829fed8SMatthew G. Knepley         cone[0] = 12; cone[1] =  9;
2194*9318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 25, cone);CHKERRQ(ierr);
219545da822fSValeria Barra       }
2196*9318fe57SMatthew G. Knepley       ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
2197*9318fe57SMatthew G. Knepley       ierr = DMPlexStratify(dm);CHKERRQ(ierr);
21982829fed8SMatthew G. Knepley       /* Build coordinates */
219965a81367SMatthew G. Knepley       ierr = PetscCalloc1(numVerts * embedDim, &coordsIn);CHKERRQ(ierr);
220045da822fSValeria Barra       if (!rank) {
220151a74b61SMatthew G. Knepley         coordsIn[0*embedDim+0] = -R; coordsIn[0*embedDim+1] =  R; coordsIn[0*embedDim+2] = -R;
220251a74b61SMatthew G. Knepley         coordsIn[1*embedDim+0] =  R; coordsIn[1*embedDim+1] =  R; coordsIn[1*embedDim+2] = -R;
220351a74b61SMatthew G. Knepley         coordsIn[2*embedDim+0] =  R; coordsIn[2*embedDim+1] = -R; coordsIn[2*embedDim+2] = -R;
220451a74b61SMatthew G. Knepley         coordsIn[3*embedDim+0] = -R; coordsIn[3*embedDim+1] = -R; coordsIn[3*embedDim+2] = -R;
220551a74b61SMatthew G. Knepley         coordsIn[4*embedDim+0] = -R; coordsIn[4*embedDim+1] =  R; coordsIn[4*embedDim+2] =  R;
220651a74b61SMatthew G. Knepley         coordsIn[5*embedDim+0] =  R; coordsIn[5*embedDim+1] =  R; coordsIn[5*embedDim+2] =  R;
220751a74b61SMatthew G. Knepley         coordsIn[6*embedDim+0] = -R; coordsIn[6*embedDim+1] = -R; coordsIn[6*embedDim+2] =  R;
220851a74b61SMatthew G. Knepley         coordsIn[7*embedDim+0] =  R; coordsIn[7*embedDim+1] = -R; coordsIn[7*embedDim+2] =  R;
220965a81367SMatthew G. Knepley       }
221045da822fSValeria Barra     }
221165a81367SMatthew G. Knepley     break;
221265a81367SMatthew G. Knepley   case 3:
2213116ded15SMatthew G. Knepley     if (simplex) {
2214116ded15SMatthew G. Knepley       const PetscReal edgeLen         = 1.0/PETSC_PHI;
221551a74b61SMatthew G. Knepley       PetscReal       vertexA[4]      = {0.5, 0.5, 0.5, 0.5};
221651a74b61SMatthew G. Knepley       PetscReal       vertexB[4]      = {1.0, 0.0, 0.0, 0.0};
221751a74b61SMatthew G. Knepley       PetscReal       vertexC[4]      = {0.5, 0.5*PETSC_PHI, 0.5/PETSC_PHI, 0.0};
2218116ded15SMatthew G. Knepley       const PetscInt  degree          = 12;
2219116ded15SMatthew G. Knepley       PetscInt        s[4]            = {1, 1, 1};
2220116ded15SMatthew G. Knepley       PetscInt        evenPerm[12][4] = {{0, 1, 2, 3}, {0, 2, 3, 1}, {0, 3, 1, 2}, {1, 0, 3, 2}, {1, 2, 0, 3}, {1, 3, 2, 0},
2221116ded15SMatthew G. Knepley                                          {2, 0, 1, 3}, {2, 1, 3, 0}, {2, 3, 0, 1}, {3, 0, 2, 1}, {3, 1, 0, 2}, {3, 2, 1, 0}};
2222116ded15SMatthew G. Knepley       PetscInt        cone[4];
2223116ded15SMatthew G. Knepley       PetscInt       *graph, p, i, j, k, l;
2224116ded15SMatthew G. Knepley 
222551a74b61SMatthew G. Knepley       vertexA[0] *= R; vertexA[1] *= R; vertexA[2] *= R; vertexA[3] *= R;
222651a74b61SMatthew G. Knepley       vertexB[0] *= R; vertexB[1] *= R; vertexB[2] *= R; vertexB[3] *= R;
222751a74b61SMatthew G. Knepley       vertexC[0] *= R; vertexC[1] *= R; vertexC[2] *= R; vertexC[3] *= R;
2228116ded15SMatthew G. Knepley       numCells    = !rank ? 600 : 0;
2229116ded15SMatthew G. Knepley       numVerts    = !rank ? 120 : 0;
2230116ded15SMatthew G. Knepley       firstVertex = numCells;
2231116ded15SMatthew G. Knepley       /* Use the 600-cell, which for a unit sphere has coordinates which are
2232116ded15SMatthew G. Knepley 
2233116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm 1,    \pm 1, \pm 1)                          16
2234116ded15SMatthew G. Knepley                (\pm 1,    0,       0,      0)  all cyclic permutations   8
2235116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm phi, \pm 1/phi, 0)  all even permutations    96
2236116ded15SMatthew G. Knepley 
2237116ded15SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
22386333ae4fSvaleriabarra          length is then given by 1/\phi = 0.61803.
2239116ded15SMatthew G. Knepley 
2240116ded15SMatthew G. Knepley          http://buzzard.pugetsound.edu/sage-practice/ch03s03.html
2241116ded15SMatthew G. Knepley          http://mathworld.wolfram.com/600-Cell.html
2242116ded15SMatthew G. Knepley       */
2243116ded15SMatthew G. Knepley       /* Construct vertices */
2244116ded15SMatthew G. Knepley       ierr = PetscCalloc1(numVerts * embedDim, &coordsIn);CHKERRQ(ierr);
2245116ded15SMatthew G. Knepley       i    = 0;
224645da822fSValeria Barra       if (!rank) {
2247116ded15SMatthew G. Knepley         for (s[0] = -1; s[0] < 2; s[0] += 2) {
2248116ded15SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
2249116ded15SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
2250116ded15SMatthew G. Knepley               for (s[3] = -1; s[3] < 2; s[3] += 2) {
2251116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[d]*vertexA[d];
2252116ded15SMatthew G. Knepley                 ++i;
2253116ded15SMatthew G. Knepley               }
2254116ded15SMatthew G. Knepley             }
2255116ded15SMatthew G. Knepley           }
2256116ded15SMatthew G. Knepley         }
2257116ded15SMatthew G. Knepley         for (p = 0; p < embedDim; ++p) {
2258116ded15SMatthew G. Knepley           s[1] = s[2] = s[3] = 1;
2259116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2260116ded15SMatthew G. Knepley             for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[(d+p)%embedDim]*vertexB[(d+p)%embedDim];
2261116ded15SMatthew G. Knepley             ++i;
2262116ded15SMatthew G. Knepley           }
2263116ded15SMatthew G. Knepley         }
2264116ded15SMatthew G. Knepley         for (p = 0; p < 12; ++p) {
2265116ded15SMatthew G. Knepley           s[3] = 1;
2266116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2267116ded15SMatthew G. Knepley             for (s[1] = -1; s[1] < 2; s[1] += 2) {
2268116ded15SMatthew G. Knepley               for (s[2] = -1; s[2] < 2; s[2] += 2) {
2269116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[evenPerm[p][d]]*vertexC[evenPerm[p][d]];
2270116ded15SMatthew G. Knepley                 ++i;
2271116ded15SMatthew G. Knepley               }
2272116ded15SMatthew G. Knepley             }
2273116ded15SMatthew G. Knepley           }
2274116ded15SMatthew G. Knepley         }
227545da822fSValeria Barra       }
2276*9318fe57SMatthew G. Knepley       if (i != numVerts) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertices %D != %D", i, numVerts);
2277116ded15SMatthew G. Knepley       /* Construct graph */
2278116ded15SMatthew G. Knepley       ierr = PetscCalloc1(numVerts * numVerts, &graph);CHKERRQ(ierr);
2279116ded15SMatthew G. Knepley       for (i = 0; i < numVerts; ++i) {
2280116ded15SMatthew G. Knepley         for (j = 0, k = 0; j < numVerts; ++j) {
2281116ded15SMatthew G. Knepley           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i*embedDim], &coordsIn[j*embedDim]) - edgeLen) < PETSC_SMALL) {graph[i*numVerts+j] = 1; ++k;}
2282116ded15SMatthew G. Knepley         }
2283*9318fe57SMatthew G. Knepley         if (k != degree) SETERRQ3(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertex %D degree %D != %D", i, k, degree);
2284116ded15SMatthew G. Knepley       }
2285116ded15SMatthew G. Knepley       /* Build Topology */
2286*9318fe57SMatthew G. Knepley       ierr = DMPlexSetChart(dm, 0, numCells+numVerts);CHKERRQ(ierr);
2287116ded15SMatthew G. Knepley       for (c = 0; c < numCells; c++) {
2288*9318fe57SMatthew G. Knepley         ierr = DMPlexSetConeSize(dm, c, embedDim);CHKERRQ(ierr);
2289116ded15SMatthew G. Knepley       }
2290*9318fe57SMatthew G. Knepley       ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
2291116ded15SMatthew G. Knepley       /* Cells */
229245da822fSValeria Barra       if (!rank) {
2293116ded15SMatthew G. Knepley         for (i = 0, c = 0; i < numVerts; ++i) {
2294116ded15SMatthew G. Knepley           for (j = 0; j < i; ++j) {
2295116ded15SMatthew G. Knepley             for (k = 0; k < j; ++k) {
2296116ded15SMatthew G. Knepley               for (l = 0; l < k; ++l) {
2297116ded15SMatthew G. Knepley                 if (graph[i*numVerts+j] && graph[j*numVerts+k] && graph[k*numVerts+i] &&
2298116ded15SMatthew G. Knepley                     graph[l*numVerts+i] && graph[l*numVerts+j] && graph[l*numVerts+k]) {
2299116ded15SMatthew G. Knepley                   cone[0] = firstVertex+i; cone[1] = firstVertex+j; cone[2] = firstVertex+k; cone[3] = firstVertex+l;
2300116ded15SMatthew G. Knepley                   /* Check orientation: https://ef.gy/linear-algebra:normal-vectors-in-higher-dimensional-spaces */
2301116ded15SMatthew G. Knepley                   {
2302116ded15SMatthew G. Knepley                     const PetscInt epsilon[4][4][4][4] = {{{{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
2303116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  1}, { 0,  0, -1, 0}},
2304116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0, -1}, { 0,  0, 0,  0}, { 0,  1,  0, 0}},
2305116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  1,  0}, { 0, -1, 0,  0}, { 0,  0,  0, 0}}},
2306116ded15SMatthew G. Knepley 
2307116ded15SMatthew G. Knepley                                                           {{{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0, -1}, { 0,  0,  1, 0}},
2308116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
2309116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  1}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, {-1,  0,  0, 0}},
2310116ded15SMatthew G. Knepley                                                            {{0,  0, -1,  0}, { 0, 0,  0,  0}, { 1,  0, 0,  0}, { 0,  0,  0, 0}}},
2311116ded15SMatthew G. Knepley 
2312116ded15SMatthew G. Knepley                                                           {{{0,  0,  0,  0}, { 0, 0,  0,  1}, { 0,  0, 0,  0}, { 0, -1,  0, 0}},
2313116ded15SMatthew G. Knepley                                                            {{0,  0,  0, -1}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 1,  0,  0, 0}},
2314116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
2315116ded15SMatthew G. Knepley                                                            {{0,  1,  0,  0}, {-1, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}}},
2316116ded15SMatthew G. Knepley 
2317116ded15SMatthew G. Knepley                                                           {{{0,  0,  0,  0}, { 0, 0, -1,  0}, { 0,  1, 0,  0}, { 0,  0,  0, 0}},
2318116ded15SMatthew G. Knepley                                                            {{0,  0,  1,  0}, { 0, 0,  0,  0}, {-1,  0, 0,  0}, { 0,  0,  0, 0}},
2319116ded15SMatthew G. Knepley                                                            {{0, -1,  0,  0}, { 1, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
2320116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}}}};
2321116ded15SMatthew G. Knepley                     PetscReal normal[4];
2322116ded15SMatthew G. Knepley                     PetscInt  e, f, g;
2323116ded15SMatthew G. Knepley 
2324116ded15SMatthew G. Knepley                     for (d = 0; d < embedDim; ++d) {
2325116ded15SMatthew G. Knepley                       normal[d] = 0.0;
2326116ded15SMatthew G. Knepley                       for (e = 0; e < embedDim; ++e) {
2327116ded15SMatthew G. Knepley                         for (f = 0; f < embedDim; ++f) {
2328116ded15SMatthew G. Knepley                           for (g = 0; g < embedDim; ++g) {
2329116ded15SMatthew G. Knepley                             normal[d] += epsilon[d][e][f][g]*(coordsIn[j*embedDim+e] - coordsIn[i*embedDim+e])*(coordsIn[k*embedDim+f] - coordsIn[i*embedDim+f])*(coordsIn[l*embedDim+f] - coordsIn[i*embedDim+f]);
2330116ded15SMatthew G. Knepley                           }
2331116ded15SMatthew G. Knepley                         }
2332116ded15SMatthew G. Knepley                       }
2333116ded15SMatthew G. Knepley                     }
2334116ded15SMatthew G. Knepley                     if (DotReal(embedDim, normal, &coordsIn[i*embedDim]) < 0) {PetscInt tmp = cone[1]; cone[1] = cone[2]; cone[2] = tmp;}
2335116ded15SMatthew G. Knepley                   }
2336*9318fe57SMatthew G. Knepley                   ierr = DMPlexSetCone(dm, c++, cone);CHKERRQ(ierr);
2337116ded15SMatthew G. Knepley                 }
2338116ded15SMatthew G. Knepley               }
2339116ded15SMatthew G. Knepley             }
2340116ded15SMatthew G. Knepley           }
2341116ded15SMatthew G. Knepley         }
234245da822fSValeria Barra       }
2343*9318fe57SMatthew G. Knepley       ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
2344*9318fe57SMatthew G. Knepley       ierr = DMPlexStratify(dm);CHKERRQ(ierr);
2345116ded15SMatthew G. Knepley       ierr = PetscFree(graph);CHKERRQ(ierr);
2346116ded15SMatthew G. Knepley       break;
2347116ded15SMatthew G. Knepley     }
2348*9318fe57SMatthew G. Knepley   default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Unsupported dimension for sphere: %D", dim);
234965a81367SMatthew G. Knepley   }
235065a81367SMatthew G. Knepley   /* Create coordinates */
2351*9318fe57SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
23522829fed8SMatthew G. Knepley   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
23532829fed8SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(coordSection, 0, embedDim);CHKERRQ(ierr);
23542829fed8SMatthew G. Knepley   ierr = PetscSectionSetChart(coordSection, firstVertex, firstVertex+numVerts);CHKERRQ(ierr);
23552829fed8SMatthew G. Knepley   for (v = firstVertex; v < firstVertex+numVerts; ++v) {
23562829fed8SMatthew G. Knepley     ierr = PetscSectionSetDof(coordSection, v, embedDim);CHKERRQ(ierr);
23572829fed8SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(coordSection, v, 0, embedDim);CHKERRQ(ierr);
23582829fed8SMatthew G. Knepley   }
23592829fed8SMatthew G. Knepley   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
23602829fed8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
23612829fed8SMatthew G. Knepley   ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
23622829fed8SMatthew G. Knepley   ierr = VecSetBlockSize(coordinates, embedDim);CHKERRQ(ierr);
23632829fed8SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
23642829fed8SMatthew G. Knepley   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
23652829fed8SMatthew G. Knepley   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
23662829fed8SMatthew G. Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
236765a81367SMatthew G. Knepley   for (v = 0; v < numVerts; ++v) for (d = 0; d < embedDim; ++d) {coords[v*embedDim+d] = coordsIn[v*embedDim+d];}
23682829fed8SMatthew G. Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
2369*9318fe57SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
23702829fed8SMatthew G. Knepley   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
237165a81367SMatthew G. Knepley   ierr = PetscFree(coordsIn);CHKERRQ(ierr);
237251a74b61SMatthew G. Knepley   {
237351a74b61SMatthew G. Knepley     DM          cdm;
237451a74b61SMatthew G. Knepley     PetscDS     cds;
2375*9318fe57SMatthew G. Knepley     PetscScalar c = R;
237651a74b61SMatthew G. Knepley 
2377*9318fe57SMatthew G. Knepley     ierr = DMPlexCreateCoordinateSpace(dm, 1, snapToSphere);CHKERRQ(ierr);
2378*9318fe57SMatthew G. Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
237951a74b61SMatthew G. Knepley     ierr = DMGetDS(cdm, &cds);CHKERRQ(ierr);
2380*9318fe57SMatthew G. Knepley     ierr = PetscDSSetConstants(cds, 1, &c);CHKERRQ(ierr);
238151a74b61SMatthew G. Knepley   }
2382*9318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
2383*9318fe57SMatthew G. Knepley   if (simplex) {ierr = DMPlexInterpolateInPlace_Internal(dm);CHKERRQ(ierr);}
2384*9318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
2385*9318fe57SMatthew G. Knepley }
2386*9318fe57SMatthew G. Knepley 
2387*9318fe57SMatthew G. Knepley /*@
2388*9318fe57SMatthew G. Knepley   DMPlexCreateSphereMesh - Creates a mesh on the d-dimensional sphere, S^d.
2389*9318fe57SMatthew G. Knepley 
2390*9318fe57SMatthew G. Knepley   Collective
2391*9318fe57SMatthew G. Knepley 
2392*9318fe57SMatthew G. Knepley   Input Parameters:
2393*9318fe57SMatthew G. Knepley + comm    - The communicator for the DM object
2394*9318fe57SMatthew G. Knepley . dim     - The dimension
2395*9318fe57SMatthew G. Knepley . simplex - Use simplices, or tensor product cells
2396*9318fe57SMatthew G. Knepley - R       - The radius
2397*9318fe57SMatthew G. Knepley 
2398*9318fe57SMatthew G. Knepley   Output Parameter:
2399*9318fe57SMatthew G. Knepley . dm  - The DM object
2400*9318fe57SMatthew G. Knepley 
2401*9318fe57SMatthew G. Knepley   Level: beginner
2402*9318fe57SMatthew G. Knepley 
2403*9318fe57SMatthew G. Knepley .seealso: DMPlexCreateBallMesh(), DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
2404*9318fe57SMatthew G. Knepley @*/
2405*9318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateSphereMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscReal R, DM *dm)
2406*9318fe57SMatthew G. Knepley {
2407*9318fe57SMatthew G. Knepley   PetscErrorCode ierr;
2408*9318fe57SMatthew G. Knepley 
2409*9318fe57SMatthew G. Knepley   PetscFunctionBegin;
2410*9318fe57SMatthew G. Knepley   PetscValidPointer(dm, 5);
2411*9318fe57SMatthew G. Knepley   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
2412*9318fe57SMatthew G. Knepley   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
2413*9318fe57SMatthew G. Knepley   ierr = DMPlexCreateSphereMesh_Internal(*dm, dim, simplex, R);CHKERRQ(ierr);
2414*9318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
2415*9318fe57SMatthew G. Knepley }
2416*9318fe57SMatthew G. Knepley 
2417*9318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBallMesh_Internal(DM dm, PetscInt dim, PetscReal R)
2418*9318fe57SMatthew G. Knepley {
2419*9318fe57SMatthew G. Knepley   DM             sdm, vol;
2420*9318fe57SMatthew G. Knepley   DMLabel        bdlabel;
2421*9318fe57SMatthew G. Knepley   PetscErrorCode ierr;
2422*9318fe57SMatthew G. Knepley 
2423*9318fe57SMatthew G. Knepley   PetscFunctionBegin;
2424*9318fe57SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), &sdm);CHKERRQ(ierr);
2425*9318fe57SMatthew G. Knepley   ierr = DMSetType(sdm, DMPLEX);CHKERRQ(ierr);
2426*9318fe57SMatthew G. Knepley   ierr = PetscObjectSetOptionsPrefix((PetscObject) sdm, "bd_");CHKERRQ(ierr);
2427*9318fe57SMatthew G. Knepley   ierr = DMPlexCreateSphereMesh_Internal(sdm, dim-1, PETSC_TRUE, R);CHKERRQ(ierr);
2428*9318fe57SMatthew G. Knepley   ierr = DMSetFromOptions(sdm);CHKERRQ(ierr);
2429*9318fe57SMatthew G. Knepley   ierr = DMViewFromOptions(sdm, NULL, "-dm_view");CHKERRQ(ierr);
2430*9318fe57SMatthew G. Knepley   ierr = DMPlexGenerate(sdm, NULL, PETSC_TRUE, &vol);CHKERRQ(ierr);
2431*9318fe57SMatthew G. Knepley   ierr = DMDestroy(&sdm);CHKERRQ(ierr);
2432*9318fe57SMatthew G. Knepley   ierr = DMPlexReplace_Static(dm, &vol);CHKERRQ(ierr);
2433*9318fe57SMatthew G. Knepley   ierr = DMCreateLabel(dm, "marker");CHKERRQ(ierr);
2434*9318fe57SMatthew G. Knepley   ierr = DMGetLabel(dm, "marker", &bdlabel);CHKERRQ(ierr);
2435*9318fe57SMatthew G. Knepley   ierr = DMPlexMarkBoundaryFaces(dm, PETSC_DETERMINE, bdlabel);CHKERRQ(ierr);
2436*9318fe57SMatthew G. Knepley   ierr = DMPlexLabelComplete(dm, bdlabel);CHKERRQ(ierr);
243751a74b61SMatthew G. Knepley   PetscFunctionReturn(0);
243851a74b61SMatthew G. Knepley }
243951a74b61SMatthew G. Knepley 
244051a74b61SMatthew G. Knepley /*@
244151a74b61SMatthew G. Knepley   DMPlexCreateBallMesh - Creates a simplex mesh on the d-dimensional ball, B^d.
244251a74b61SMatthew G. Knepley 
244351a74b61SMatthew G. Knepley   Collective
244451a74b61SMatthew G. Knepley 
244551a74b61SMatthew G. Knepley   Input Parameters:
244651a74b61SMatthew G. Knepley + comm  - The communicator for the DM object
244751a74b61SMatthew G. Knepley . dim   - The dimension
244851a74b61SMatthew G. Knepley - R     - The radius
244951a74b61SMatthew G. Knepley 
245051a74b61SMatthew G. Knepley   Output Parameter:
245151a74b61SMatthew G. Knepley . dm  - The DM object
245251a74b61SMatthew G. Knepley 
245351a74b61SMatthew G. Knepley   Options Database Keys:
245451a74b61SMatthew G. Knepley - bd_dm_refine - This will refine the surface mesh preserving the sphere geometry
245551a74b61SMatthew G. Knepley 
245651a74b61SMatthew G. Knepley   Level: beginner
245751a74b61SMatthew G. Knepley 
245851a74b61SMatthew G. Knepley .seealso: DMPlexCreateSphereMesh(), DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
245951a74b61SMatthew G. Knepley @*/
246051a74b61SMatthew G. Knepley PetscErrorCode DMPlexCreateBallMesh(MPI_Comm comm, PetscInt dim, PetscReal R, DM *dm)
246151a74b61SMatthew G. Knepley {
246251a74b61SMatthew G. Knepley   PetscErrorCode ierr;
246351a74b61SMatthew G. Knepley 
246451a74b61SMatthew G. Knepley   PetscFunctionBegin;
2465*9318fe57SMatthew G. Knepley   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
2466*9318fe57SMatthew G. Knepley   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
2467*9318fe57SMatthew G. Knepley   ierr = DMPlexCreateBallMesh_Internal(*dm, dim, R);CHKERRQ(ierr);
24682829fed8SMatthew G. Knepley   PetscFunctionReturn(0);
24692829fed8SMatthew G. Knepley }
24702829fed8SMatthew G. Knepley 
2471*9318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateReferenceCell_Internal(DM rdm, DMPolytopeType ct)
24720a6ba040SMatthew G. Knepley {
24730a6ba040SMatthew G. Knepley   PetscErrorCode ierr;
24740a6ba040SMatthew G. Knepley 
24750a6ba040SMatthew G. Knepley   PetscFunctionBegin;
2476*9318fe57SMatthew G. Knepley   switch (ct) {
2477*9318fe57SMatthew G. Knepley     case DM_POLYTOPE_POINT:
2478*9318fe57SMatthew G. Knepley     {
2479*9318fe57SMatthew G. Knepley       PetscInt    numPoints[1]        = {1};
2480*9318fe57SMatthew G. Knepley       PetscInt    coneSize[1]         = {0};
2481*9318fe57SMatthew G. Knepley       PetscInt    cones[1]            = {0};
2482*9318fe57SMatthew G. Knepley       PetscInt    coneOrientations[1] = {0};
2483*9318fe57SMatthew G. Knepley       PetscScalar vertexCoords[1]     = {0.0};
2484*9318fe57SMatthew G. Knepley 
2485*9318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 0);CHKERRQ(ierr);
2486*9318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 0, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2487*9318fe57SMatthew G. Knepley     }
2488*9318fe57SMatthew G. Knepley     break;
2489*9318fe57SMatthew G. Knepley     case DM_POLYTOPE_SEGMENT:
2490*9318fe57SMatthew G. Knepley     {
2491*9318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {2, 1};
2492*9318fe57SMatthew G. Knepley       PetscInt    coneSize[3]         = {2, 0, 0};
2493*9318fe57SMatthew G. Knepley       PetscInt    cones[2]            = {1, 2};
2494*9318fe57SMatthew G. Knepley       PetscInt    coneOrientations[2] = {0, 0};
2495*9318fe57SMatthew G. Knepley       PetscScalar vertexCoords[2]     = {-1.0,  1.0};
2496*9318fe57SMatthew G. Knepley 
2497*9318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 1);CHKERRQ(ierr);
2498*9318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2499*9318fe57SMatthew G. Knepley     }
2500*9318fe57SMatthew G. Knepley     break;
2501*9318fe57SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
2502*9318fe57SMatthew G. Knepley     {
2503*9318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {3, 1};
2504*9318fe57SMatthew G. Knepley       PetscInt    coneSize[4]         = {3, 0, 0, 0};
2505*9318fe57SMatthew G. Knepley       PetscInt    cones[3]            = {1, 2, 3};
2506*9318fe57SMatthew G. Knepley       PetscInt    coneOrientations[3] = {0, 0, 0};
2507*9318fe57SMatthew G. Knepley       PetscScalar vertexCoords[6]     = {-1.0, -1.0,  1.0, -1.0,  -1.0, 1.0};
2508*9318fe57SMatthew G. Knepley 
2509*9318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 2);CHKERRQ(ierr);
2510*9318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2511*9318fe57SMatthew G. Knepley     }
2512*9318fe57SMatthew G. Knepley     break;
2513*9318fe57SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
2514*9318fe57SMatthew G. Knepley     {
2515*9318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {4, 1};
2516*9318fe57SMatthew G. Knepley       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
2517*9318fe57SMatthew G. Knepley       PetscInt    cones[4]            = {1, 2, 3, 4};
2518*9318fe57SMatthew G. Knepley       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
2519*9318fe57SMatthew G. Knepley       PetscScalar vertexCoords[8]     = {-1.0, -1.0,  1.0, -1.0,  1.0, 1.0,  -1.0, 1.0};
2520*9318fe57SMatthew G. Knepley 
2521*9318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 2);CHKERRQ(ierr);
2522*9318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2523*9318fe57SMatthew G. Knepley     }
2524*9318fe57SMatthew G. Knepley     break;
2525*9318fe57SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
2526*9318fe57SMatthew G. Knepley     {
2527*9318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {4, 1};
2528*9318fe57SMatthew G. Knepley       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
2529*9318fe57SMatthew G. Knepley       PetscInt    cones[4]            = {1, 2, 3, 4};
2530*9318fe57SMatthew G. Knepley       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
2531*9318fe57SMatthew G. Knepley       PetscScalar vertexCoords[8]     = {-1.0, -1.0,  1.0, -1.0,  -1.0, 1.0,  1.0, 1.0};
2532*9318fe57SMatthew G. Knepley 
2533*9318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 2);CHKERRQ(ierr);
2534*9318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2535*9318fe57SMatthew G. Knepley     }
2536*9318fe57SMatthew G. Knepley     break;
2537*9318fe57SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
2538*9318fe57SMatthew G. Knepley     {
2539*9318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {4, 1};
2540*9318fe57SMatthew G. Knepley       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
2541*9318fe57SMatthew G. Knepley       PetscInt    cones[4]            = {1, 3, 2, 4};
2542*9318fe57SMatthew G. Knepley       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
2543*9318fe57SMatthew G. Knepley       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};
2544*9318fe57SMatthew G. Knepley 
2545*9318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 3);CHKERRQ(ierr);
2546*9318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2547*9318fe57SMatthew G. Knepley     }
2548*9318fe57SMatthew G. Knepley     break;
2549*9318fe57SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
2550*9318fe57SMatthew G. Knepley     {
2551*9318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {8, 1};
2552*9318fe57SMatthew G. Knepley       PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
2553*9318fe57SMatthew G. Knepley       PetscInt    cones[8]            = {1, 4, 3, 2, 5, 6, 7, 8};
2554*9318fe57SMatthew G. Knepley       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
2555*9318fe57SMatthew G. Knepley       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,
2556*9318fe57SMatthew G. Knepley                                          -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  1.0, 1.0,  1.0,  -1.0, 1.0,  1.0};
2557*9318fe57SMatthew G. Knepley 
2558*9318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 3);CHKERRQ(ierr);
2559*9318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2560*9318fe57SMatthew G. Knepley     }
2561*9318fe57SMatthew G. Knepley     break;
2562*9318fe57SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM:
2563*9318fe57SMatthew G. Knepley     {
2564*9318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {6, 1};
2565*9318fe57SMatthew G. Knepley       PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
2566*9318fe57SMatthew G. Knepley       PetscInt    cones[6]            = {1, 3, 2, 4, 5, 6};
2567*9318fe57SMatthew G. Knepley       PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
2568*9318fe57SMatthew G. Knepley       PetscScalar vertexCoords[18]    = {-1.0, -1.0, -1.0,  1.0, -1.0, -1.0,  -1.0, 1.0, -1.0,
2569*9318fe57SMatthew G. Knepley                                          -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  -1.0, 1.0,  1.0};
2570*9318fe57SMatthew G. Knepley 
2571*9318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 3);CHKERRQ(ierr);
2572*9318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2573*9318fe57SMatthew G. Knepley     }
2574*9318fe57SMatthew G. Knepley     break;
2575*9318fe57SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM_TENSOR:
2576*9318fe57SMatthew G. Knepley     {
2577*9318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {6, 1};
2578*9318fe57SMatthew G. Knepley       PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
2579*9318fe57SMatthew G. Knepley       PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
2580*9318fe57SMatthew G. Knepley       PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
2581*9318fe57SMatthew G. Knepley       PetscScalar vertexCoords[18]    = {-1.0, -1.0, -1.0,  1.0, -1.0, -1.0,  -1.0, 1.0, -1.0,
2582*9318fe57SMatthew G. Knepley                                          -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  -1.0, 1.0,  1.0};
2583*9318fe57SMatthew G. Knepley 
2584*9318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 3);CHKERRQ(ierr);
2585*9318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2586*9318fe57SMatthew G. Knepley     }
2587*9318fe57SMatthew G. Knepley     break;
2588*9318fe57SMatthew G. Knepley     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
2589*9318fe57SMatthew G. Knepley     {
2590*9318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {8, 1};
2591*9318fe57SMatthew G. Knepley       PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
2592*9318fe57SMatthew G. Knepley       PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
2593*9318fe57SMatthew G. Knepley       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
2594*9318fe57SMatthew G. Knepley       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,
2595*9318fe57SMatthew G. Knepley                                          -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  1.0, 1.0,  1.0,  -1.0, 1.0,  1.0};
2596*9318fe57SMatthew G. Knepley 
2597*9318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 3);CHKERRQ(ierr);
2598*9318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2599*9318fe57SMatthew G. Knepley     }
2600*9318fe57SMatthew G. Knepley     break;
2601*9318fe57SMatthew G. Knepley     case DM_POLYTOPE_PYRAMID:
2602*9318fe57SMatthew G. Knepley     {
2603*9318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {5, 1};
2604*9318fe57SMatthew G. Knepley       PetscInt    coneSize[6]         = {5, 0, 0, 0, 0, 0};
2605*9318fe57SMatthew G. Knepley       PetscInt    cones[5]            = {1, 4, 3, 2, 5};
2606*9318fe57SMatthew G. Knepley       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
2607*9318fe57SMatthew G. Knepley       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,
2608*9318fe57SMatthew G. Knepley                                           0.0,  0.0,  1.0};
2609*9318fe57SMatthew G. Knepley 
2610*9318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 3);CHKERRQ(ierr);
2611*9318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2612*9318fe57SMatthew G. Knepley     }
2613*9318fe57SMatthew G. Knepley     break;
2614*9318fe57SMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) rdm), PETSC_ERR_ARG_WRONG, "Cannot create reference cell for cell type %s", DMPolytopeTypes[ct]);
2615*9318fe57SMatthew G. Knepley   }
2616*9318fe57SMatthew G. Knepley   {
2617*9318fe57SMatthew G. Knepley     PetscInt Nv, v;
2618*9318fe57SMatthew G. Knepley 
2619*9318fe57SMatthew G. Knepley     /* Must create the celltype label here so that we do not automatically try to compute the types */
2620*9318fe57SMatthew G. Knepley     ierr = DMCreateLabel(rdm, "celltype");CHKERRQ(ierr);
2621*9318fe57SMatthew G. Knepley     ierr = DMPlexSetCellType(rdm, 0, ct);CHKERRQ(ierr);
2622*9318fe57SMatthew G. Knepley     ierr = DMPlexGetChart(rdm, NULL, &Nv);CHKERRQ(ierr);
2623*9318fe57SMatthew G. Knepley     for (v = 1; v < Nv; ++v) {ierr = DMPlexSetCellType(rdm, v, DM_POLYTOPE_POINT);CHKERRQ(ierr);}
2624*9318fe57SMatthew G. Knepley   }
2625*9318fe57SMatthew G. Knepley   ierr = DMPlexInterpolateInPlace_Internal(rdm);CHKERRQ(ierr);
26260a6ba040SMatthew G. Knepley   PetscFunctionReturn(0);
26270a6ba040SMatthew G. Knepley }
26280a6ba040SMatthew G. Knepley 
2629*9318fe57SMatthew G. Knepley /*@
2630*9318fe57SMatthew G. Knepley   DMPlexCreateReferenceCell - Create a DMPLEX with the appropriate FEM reference cell
2631*9318fe57SMatthew G. Knepley 
2632*9318fe57SMatthew G. Knepley   Collective
2633*9318fe57SMatthew G. Knepley 
2634*9318fe57SMatthew G. Knepley   Input Parameters:
2635*9318fe57SMatthew G. Knepley + comm - The communicator
2636*9318fe57SMatthew G. Knepley - ct   - The cell type of the reference cell
2637*9318fe57SMatthew G. Knepley 
2638*9318fe57SMatthew G. Knepley   Output Parameter:
2639*9318fe57SMatthew G. Knepley . refdm - The reference cell
2640*9318fe57SMatthew G. Knepley 
2641*9318fe57SMatthew G. Knepley   Level: intermediate
2642*9318fe57SMatthew G. Knepley 
2643*9318fe57SMatthew G. Knepley .seealso: DMPlexCreateReferenceCell(), DMPlexCreateBoxMesh()
2644*9318fe57SMatthew G. Knepley @*/
2645*9318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateReferenceCell(MPI_Comm comm, DMPolytopeType ct, DM *refdm)
26460a6ba040SMatthew G. Knepley {
26470a6ba040SMatthew G. Knepley   PetscErrorCode ierr;
26480a6ba040SMatthew G. Knepley 
26490a6ba040SMatthew G. Knepley   PetscFunctionBegin;
2650*9318fe57SMatthew G. Knepley   ierr = DMCreate(comm, refdm);CHKERRQ(ierr);
2651*9318fe57SMatthew G. Knepley   ierr = DMSetType(*refdm, DMPLEX);CHKERRQ(ierr);
2652*9318fe57SMatthew G. Knepley   ierr = DMPlexCreateReferenceCell_Internal(*refdm, ct);CHKERRQ(ierr);
2653*9318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
2654*9318fe57SMatthew G. Knepley }
265579a015ccSMatthew G. Knepley 
2656*9318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoundaryLabel_Private(DM dm, const char name[])
2657*9318fe57SMatthew G. Knepley {
2658*9318fe57SMatthew G. Knepley   DM             plex;
2659*9318fe57SMatthew G. Knepley   DMLabel        label;
2660*9318fe57SMatthew G. Knepley   PetscBool      hasLabel;
2661*9318fe57SMatthew G. Knepley   PetscErrorCode ierr;
26620a6ba040SMatthew G. Knepley 
2663*9318fe57SMatthew G. Knepley   PetscFunctionBeginUser;
2664*9318fe57SMatthew G. Knepley   ierr = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
2665*9318fe57SMatthew G. Knepley   if (hasLabel) PetscFunctionReturn(0);
2666*9318fe57SMatthew G. Knepley   ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
2667*9318fe57SMatthew G. Knepley   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
2668*9318fe57SMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
2669*9318fe57SMatthew G. Knepley   ierr = DMPlexMarkBoundaryFaces(plex, 1, label);CHKERRQ(ierr);
2670*9318fe57SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
2671*9318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
2672*9318fe57SMatthew G. Knepley }
2673acdc6f61SToby Isaac 
2674*9318fe57SMatthew G. Knepley const char * const DMPlexShapes[] = {"box", "box_surface", "ball", "sphere", "cylinder", "unknown", "DMPlexShape", "DM_SHAPE_", NULL};
2675*9318fe57SMatthew G. Knepley 
2676*9318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateFromOptions_Internal(PetscOptionItems *PetscOptionsObject, DM dm)
2677*9318fe57SMatthew G. Knepley {
2678*9318fe57SMatthew G. Knepley   DMPlexShape    shape = DM_SHAPE_BOX;
2679*9318fe57SMatthew G. Knepley   DMPolytopeType cell  = DM_POLYTOPE_TRIANGLE;
2680*9318fe57SMatthew G. Knepley   PetscInt       dim   = 2;
2681*9318fe57SMatthew G. Knepley   PetscBool      simplex = PETSC_TRUE, interpolate = PETSC_TRUE, adjCone = PETSC_FALSE, adjClosure = PETSC_TRUE, refDomain = PETSC_FALSE;
2682*9318fe57SMatthew G. Knepley   PetscBool      flg, flg2, fflg, bdfflg;
2683*9318fe57SMatthew G. Knepley   MPI_Comm       comm;
2684*9318fe57SMatthew G. Knepley   char           filename[PETSC_MAX_PATH_LEN], bdFilename[PETSC_MAX_PATH_LEN];
2685*9318fe57SMatthew G. Knepley   PetscErrorCode ierr;
2686*9318fe57SMatthew G. Knepley 
2687*9318fe57SMatthew G. Knepley   PetscFunctionBegin;
2688*9318fe57SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
2689*9318fe57SMatthew G. Knepley   /* TODO Turn this into a registration interface */
2690*9318fe57SMatthew G. Knepley   ierr = PetscOptionsString("-dm_plex_filename", "File containing a mesh", "DMPlexCreateFromFile", filename, filename, sizeof(filename), &fflg);CHKERRQ(ierr);
2691*9318fe57SMatthew G. Knepley   ierr = PetscOptionsString("-dm_plex_boundary_filename", "File containing a mesh boundary", "DMPlexCreateFromFile", bdFilename, bdFilename, sizeof(bdFilename), &bdfflg);CHKERRQ(ierr);
2692*9318fe57SMatthew G. Knepley   ierr = PetscOptionsEnum("-dm_plex_cell", "Cell shape", "", DMPolytopeTypes, (PetscEnum) cell, (PetscEnum *) &cell, NULL);CHKERRQ(ierr);
2693*9318fe57SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_reference_cell_domain", "Use a reference cell domain", "", refDomain, &refDomain, NULL);CHKERRQ(ierr);
2694*9318fe57SMatthew G. Knepley   ierr = PetscOptionsEnum("-dm_plex_shape", "Shape for built-in mesh", "", DMPlexShapes, (PetscEnum) shape, (PetscEnum *) &shape, &flg);CHKERRQ(ierr);
2695*9318fe57SMatthew G. Knepley   ierr = PetscOptionsBoundedInt("-dm_plex_dim", "Topological dimension of the mesh", "DMGetDimension", dim, &dim, &flg, 0);CHKERRQ(ierr);
2696*9318fe57SMatthew G. Knepley   if ((dim < 0) || (dim > 3)) SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Dimension %D should be in [1, 3]", dim);
2697*9318fe57SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_simplex", "Mesh cell shape", "", simplex,  &simplex, &flg);CHKERRQ(ierr);
2698*9318fe57SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_interpolate", "Flag to create edges and faces automatically", "", interpolate, &interpolate, &flg);CHKERRQ(ierr);
2699*9318fe57SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_adj_cone", "Set adjacency direction", "DMSetBasicAdjacency", adjCone,  &adjCone, &flg);CHKERRQ(ierr);
2700*9318fe57SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_adj_closure", "Set adjacency size", "DMSetBasicAdjacency", adjClosure,  &adjClosure, &flg2);CHKERRQ(ierr);
2701*9318fe57SMatthew G. Knepley   if (flg || flg2) {ierr = DMSetBasicAdjacency(dm, adjCone, adjClosure);CHKERRQ(ierr);}
2702*9318fe57SMatthew G. Knepley 
2703*9318fe57SMatthew G. Knepley   if (fflg) {
2704*9318fe57SMatthew G. Knepley     DM dmnew;
2705*9318fe57SMatthew G. Knepley 
2706*9318fe57SMatthew G. Knepley     ierr = DMPlexCreateFromFile(PetscObjectComm((PetscObject) dm), filename, interpolate, &dmnew);CHKERRQ(ierr);
2707*9318fe57SMatthew G. Knepley     ierr = DMPlexReplace_Static(dm, &dmnew);CHKERRQ(ierr);
2708*9318fe57SMatthew G. Knepley   } else if (refDomain) {
2709*9318fe57SMatthew G. Knepley     ierr = DMPlexCreateReferenceCell_Internal(dm, cell);CHKERRQ(ierr);
2710*9318fe57SMatthew G. Knepley   } else if (bdfflg) {
2711*9318fe57SMatthew G. Knepley     DM bdm, dmnew;
2712*9318fe57SMatthew G. Knepley 
2713*9318fe57SMatthew G. Knepley     ierr = DMPlexCreateFromFile(PetscObjectComm((PetscObject) dm), bdFilename, interpolate, &bdm);CHKERRQ(ierr);
2714*9318fe57SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) bdm, "bd_");CHKERRQ(ierr);
2715*9318fe57SMatthew G. Knepley     ierr = DMSetFromOptions(bdm);CHKERRQ(ierr);
2716*9318fe57SMatthew G. Knepley     ierr = DMPlexGenerate(bdm, NULL, interpolate, &dmnew);CHKERRQ(ierr);
2717*9318fe57SMatthew G. Knepley     ierr = DMDestroy(&bdm);CHKERRQ(ierr);
2718*9318fe57SMatthew G. Knepley     ierr = DMPlexReplace_Static(dm, &dmnew);CHKERRQ(ierr);
2719*9318fe57SMatthew G. Knepley   } else {
2720*9318fe57SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) dm, DMPlexShapes[shape]);CHKERRQ(ierr);
2721*9318fe57SMatthew G. Knepley     switch (shape) {
2722*9318fe57SMatthew G. Knepley       case DM_SHAPE_BOX:
2723*9318fe57SMatthew G. Knepley       {
2724*9318fe57SMatthew G. Knepley         PetscInt       faces[3] = {0, 0, 0};
2725*9318fe57SMatthew G. Knepley         PetscReal      lower[3] = {0, 0, 0};
2726*9318fe57SMatthew G. Knepley         PetscReal      upper[3] = {1, 1, 1};
2727*9318fe57SMatthew G. Knepley         DMBoundaryType bdt[3]   = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
2728*9318fe57SMatthew G. Knepley         PetscInt       i, n;
2729*9318fe57SMatthew G. Knepley 
2730*9318fe57SMatthew G. Knepley         n    = dim;
2731*9318fe57SMatthew G. Knepley         for (i = 0; i < dim; ++i) faces[i] = (dim == 1 ? 1 : 4-dim);
2732*9318fe57SMatthew G. Knepley         ierr = PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg);CHKERRQ(ierr);
2733*9318fe57SMatthew G. Knepley         n    = 3;
2734*9318fe57SMatthew G. Knepley         ierr = PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg);CHKERRQ(ierr);
2735*9318fe57SMatthew G. Knepley         if (flg && (n != dim)) SETERRQ2(comm, PETSC_ERR_ARG_SIZ, "Lower box point had %D values, should have been %D", n, dim);
2736*9318fe57SMatthew G. Knepley         n    = 3;
2737*9318fe57SMatthew G. Knepley         ierr = PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg);CHKERRQ(ierr);
2738*9318fe57SMatthew G. Knepley         if (flg && (n != dim)) SETERRQ2(comm, PETSC_ERR_ARG_SIZ, "Upper box point had %D values, should have been %D", n, dim);
2739*9318fe57SMatthew G. Knepley         n    = 3;
2740*9318fe57SMatthew G. Knepley         ierr = PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *) bdt, &n, &flg);CHKERRQ(ierr);
2741*9318fe57SMatthew G. Knepley         if (flg && (n != dim)) SETERRQ2(comm, PETSC_ERR_ARG_SIZ, "Box boundary types had %D values, should have been %D", n, dim);
2742*9318fe57SMatthew G. Knepley         switch (cell) {
2743*9318fe57SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM:
2744*9318fe57SMatthew G. Knepley             ierr = DMPlexCreateWedgeBoxMesh_Internal(dm, faces, lower, upper, bdt, PETSC_FALSE, interpolate);CHKERRQ(ierr);
2745*9318fe57SMatthew G. Knepley             break;
2746*9318fe57SMatthew G. Knepley           default:
2747*9318fe57SMatthew G. Knepley             ierr = DMPlexCreateBoxMesh_Internal(dm, dim, simplex, faces, lower, upper, bdt, interpolate);CHKERRQ(ierr);
2748*9318fe57SMatthew G. Knepley             break;
2749*9318fe57SMatthew G. Knepley         }
2750*9318fe57SMatthew G. Knepley       }
2751*9318fe57SMatthew G. Knepley       break;
2752*9318fe57SMatthew G. Knepley       case DM_SHAPE_BOX_SURFACE:
2753*9318fe57SMatthew G. Knepley       {
2754*9318fe57SMatthew G. Knepley         PetscInt  faces[3] = {0, 0, 0};
2755*9318fe57SMatthew G. Knepley         PetscReal lower[3] = {0, 0, 0};
2756*9318fe57SMatthew G. Knepley         PetscReal upper[3] = {1, 1, 1};
2757*9318fe57SMatthew G. Knepley         PetscInt  i, n;
2758*9318fe57SMatthew G. Knepley 
2759*9318fe57SMatthew G. Knepley         n    = dim+1;
2760*9318fe57SMatthew G. Knepley         for (i = 0; i < dim+1; ++i) faces[i] = (dim+1 == 1 ? 1 : 4-(dim+1));
2761*9318fe57SMatthew G. Knepley         ierr = PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg);CHKERRQ(ierr);
2762*9318fe57SMatthew G. Knepley         n    = 3;
2763*9318fe57SMatthew G. Knepley         ierr = PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg);CHKERRQ(ierr);
2764*9318fe57SMatthew G. Knepley         if (flg && (n != dim+1)) SETERRQ2(comm, PETSC_ERR_ARG_SIZ, "Lower box point had %D values, should have been %D", n, dim+1);
2765*9318fe57SMatthew G. Knepley         n    = 3;
2766*9318fe57SMatthew G. Knepley         ierr = PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg);CHKERRQ(ierr);
2767*9318fe57SMatthew G. Knepley         if (flg && (n != dim+1)) SETERRQ2(comm, PETSC_ERR_ARG_SIZ, "Upper box point had %D values, should have been %D", n, dim+1);
2768*9318fe57SMatthew G. Knepley         ierr = DMPlexCreateBoxSurfaceMesh_Internal(dm, dim+1, faces, lower, upper, interpolate);CHKERRQ(ierr);
2769*9318fe57SMatthew G. Knepley       }
2770*9318fe57SMatthew G. Knepley       break;
2771*9318fe57SMatthew G. Knepley       case DM_SHAPE_SPHERE:
2772*9318fe57SMatthew G. Knepley       {
2773*9318fe57SMatthew G. Knepley         PetscReal R = 1.0;
2774*9318fe57SMatthew G. Knepley 
2775*9318fe57SMatthew G. Knepley         ierr = PetscOptionsReal("-dm_plex_sphere_radius", "Radius of the sphere", "", R,  &R, &flg);CHKERRQ(ierr);
2776*9318fe57SMatthew G. Knepley         ierr = DMPlexCreateSphereMesh_Internal(dm, dim, simplex, R);CHKERRQ(ierr);
2777*9318fe57SMatthew G. Knepley       }
2778*9318fe57SMatthew G. Knepley       break;
2779*9318fe57SMatthew G. Knepley       case DM_SHAPE_BALL:
2780*9318fe57SMatthew G. Knepley       {
2781*9318fe57SMatthew G. Knepley         PetscReal R = 1.0;
2782*9318fe57SMatthew G. Knepley 
2783*9318fe57SMatthew G. Knepley         ierr = PetscOptionsReal("-dm_plex_ball_radius", "Radius of the ball", "", R,  &R, &flg);CHKERRQ(ierr);
2784*9318fe57SMatthew G. Knepley         ierr = DMPlexCreateBallMesh_Internal(dm, dim, R);CHKERRQ(ierr);
2785*9318fe57SMatthew G. Knepley       }
2786*9318fe57SMatthew G. Knepley       break;
2787*9318fe57SMatthew G. Knepley       case DM_SHAPE_CYLINDER:
2788*9318fe57SMatthew G. Knepley       {
2789*9318fe57SMatthew G. Knepley         DMBoundaryType bdt = DM_BOUNDARY_NONE;
2790*9318fe57SMatthew G. Knepley         PetscInt       Nw  = 6;
2791*9318fe57SMatthew G. Knepley 
2792*9318fe57SMatthew G. Knepley         ierr = PetscOptionsEnum("-dm_plex_cylinder_bd", "Boundary type in the z direction", "", DMBoundaryTypes, (PetscEnum) bdt, (PetscEnum *) &bdt, NULL);CHKERRQ(ierr);
2793*9318fe57SMatthew G. Knepley         ierr = PetscOptionsInt("-dm_plex_cylinder_num wedges", "Number of wedges around the cylinder", "", Nw, &Nw, NULL);CHKERRQ(ierr);
2794*9318fe57SMatthew G. Knepley         switch (cell) {
2795*9318fe57SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM:
2796*9318fe57SMatthew G. Knepley             ierr = DMPlexCreateWedgeCylinderMesh_Internal(dm, Nw, interpolate);CHKERRQ(ierr);
2797*9318fe57SMatthew G. Knepley             break;
2798*9318fe57SMatthew G. Knepley           default:
2799*9318fe57SMatthew G. Knepley             ierr = DMPlexCreateHexCylinderMesh_Internal(dm, bdt);CHKERRQ(ierr);
2800*9318fe57SMatthew G. Knepley             break;
2801*9318fe57SMatthew G. Knepley         }
2802*9318fe57SMatthew G. Knepley       }
2803*9318fe57SMatthew G. Knepley       break;
2804*9318fe57SMatthew G. Knepley       default: SETERRQ1(comm, PETSC_ERR_SUP, "Domain shape %s is unsupported", DMPlexShapes[shape]);
2805*9318fe57SMatthew G. Knepley     }
2806*9318fe57SMatthew G. Knepley   }
2807*9318fe57SMatthew G. Knepley   ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE);CHKERRQ(ierr);
28080a6ba040SMatthew G. Knepley   PetscFunctionReturn(0);
28090a6ba040SMatthew G. Knepley }
28100a6ba040SMatthew G. Knepley 
281147920aaeSMatthew G. Knepley PetscErrorCode DMSetFromOptions_NonRefinement_Plex(PetscOptionItems *PetscOptionsObject,DM dm)
28120a6ba040SMatthew G. Knepley {
28130a6ba040SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2814c0f0dcc3SMatthew G. Knepley   PetscBool      flg;
2815*9318fe57SMatthew G. Knepley   char           bdLabel[PETSC_MAX_PATH_LEN];
28160a6ba040SMatthew G. Knepley   PetscErrorCode ierr;
28170a6ba040SMatthew G. Knepley 
28180a6ba040SMatthew G. Knepley   PetscFunctionBegin;
28190a6ba040SMatthew G. Knepley   /* Handle viewing */
28200c77aedcSMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMPlexMatSetClosure", PETSC_FALSE, &mesh->printSetValues, NULL);CHKERRQ(ierr);
28215a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-dm_plex_print_fem", "Debug output level all fem computations", "DMPlexSNESComputeResidualFEM", 0, &mesh->printFEM, NULL,0);CHKERRQ(ierr);
28220c77aedcSMatthew G. Knepley   ierr = PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMPlexSNESComputeResidualFEM", mesh->printTol, &mesh->printTol, NULL);CHKERRQ(ierr);
28235a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-dm_plex_print_l2", "Debug output level all L2 diff computations", "DMComputeL2Diff", 0, &mesh->printL2, NULL,0);CHKERRQ(ierr);
2824c0f0dcc3SMatthew G. Knepley   ierr = DMMonitorSetFromOptions(dm, "-dm_plex_monitor_throughput", "Monitor the simulation throughput", "DMPlexMonitorThroughput", DMPlexMonitorThroughput, NULL, &flg);CHKERRQ(ierr);
2825c0f0dcc3SMatthew G. Knepley   if (flg) {ierr = PetscLogDefaultBegin();CHKERRQ(ierr);}
2826*9318fe57SMatthew G. Knepley   /* Labeling */
2827*9318fe57SMatthew G. Knepley   ierr = PetscOptionsString("-dm_plex_boundary_label", "Label to mark the mesh boundary", "", bdLabel, bdLabel, sizeof(bdLabel), &flg);CHKERRQ(ierr);
2828*9318fe57SMatthew G. Knepley   if (flg) {ierr = DMPlexCreateBoundaryLabel_Private(dm, bdLabel);CHKERRQ(ierr);}
2829953fc75cSMatthew G. Knepley   /* Point Location */
28300c77aedcSMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_hash_location", "Use grid hashing for point location", "DMInterpolate", PETSC_FALSE, &mesh->useHashLocation, NULL);CHKERRQ(ierr);
28310848f4b5SMatthew G. Knepley   /* Partitioning and distribution */
283298ba2d7fSLawrence Mitchell   ierr = PetscOptionsBool("-dm_plex_partition_balance", "Attempt to evenly divide points on partition boundary between processes", "DMPlexSetPartitionBalance", PETSC_FALSE, &mesh->partitionBalance, NULL);CHKERRQ(ierr);
28332e62ab5aSMatthew G. Knepley   /* Generation and remeshing */
28340c77aedcSMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_remesh_bd", "Allow changes to the boundary on remeshing", "DMAdapt", PETSC_FALSE, &mesh->remeshBd, NULL);CHKERRQ(ierr);
2835b29cfa1cSToby Isaac   /* Projection behavior */
28365a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-dm_plex_max_projection_height", "Maxmimum mesh point height used to project locally", "DMPlexSetMaxProjectionHeight", 0, &mesh->maxProjectionHeight, NULL,0);CHKERRQ(ierr);
283764141f95SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_regular_refinement", "Use special nested projection algorithm for regular refinement", "DMPlexSetRegularRefinement", mesh->regularRefinement, &mesh->regularRefinement, NULL);CHKERRQ(ierr);
2838*9318fe57SMatthew G. Knepley   ierr = PetscOptionsEnum("-dm_plex_cell_refiner", "Strategy for cell refinment", "", DMPlexCellRefinerTypes, (PetscEnum) mesh->cellRefiner, (PetscEnum *) &mesh->cellRefiner, NULL);CHKERRQ(ierr);
2839f12cf164SMatthew G. Knepley   /* Checking structure */
2840f12cf164SMatthew G. Knepley   {
2841e902f1eaSVaclav Hapla     PetscBool   flg = PETSC_FALSE, flg2 = PETSC_FALSE, all = PETSC_FALSE;
2842f12cf164SMatthew G. Knepley 
2843e902f1eaSVaclav Hapla     ierr = PetscOptionsBool("-dm_plex_check_all", "Perform all checks", NULL, PETSC_FALSE, &all, &flg2);CHKERRQ(ierr);
2844f12cf164SMatthew G. Knepley     ierr = PetscOptionsBool("-dm_plex_check_symmetry", "Check that the adjacency information in the mesh is symmetric", "DMPlexCheckSymmetry", PETSC_FALSE, &flg, &flg2);CHKERRQ(ierr);
2845e902f1eaSVaclav Hapla     if (all || (flg && flg2)) {ierr = DMPlexCheckSymmetry(dm);CHKERRQ(ierr);}
284625c50c26SVaclav Hapla     ierr = PetscOptionsBool("-dm_plex_check_skeleton", "Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes)", "DMPlexCheckSkeleton", PETSC_FALSE, &flg, &flg2);CHKERRQ(ierr);
2847e902f1eaSVaclav Hapla     if (all || (flg && flg2)) {ierr = DMPlexCheckSkeleton(dm, 0);CHKERRQ(ierr);}
284825c50c26SVaclav Hapla     ierr = PetscOptionsBool("-dm_plex_check_faces", "Check that the faces of each cell give a vertex order this is consistent with what we expect from the cell type", "DMPlexCheckFaces", PETSC_FALSE, &flg, &flg2);CHKERRQ(ierr);
2849e902f1eaSVaclav Hapla     if (all || (flg && flg2)) {ierr = DMPlexCheckFaces(dm, 0);CHKERRQ(ierr);}
2850f12cf164SMatthew G. Knepley     ierr = PetscOptionsBool("-dm_plex_check_geometry", "Check that cells have positive volume", "DMPlexCheckGeometry", PETSC_FALSE, &flg, &flg2);CHKERRQ(ierr);
2851e902f1eaSVaclav Hapla     if (all || (flg && flg2)) {ierr = DMPlexCheckGeometry(dm);CHKERRQ(ierr);}
285275ebff7aSVaclav Hapla     ierr = PetscOptionsBool("-dm_plex_check_pointsf", "Check some necessary conditions for PointSF", "DMPlexCheckPointSF", PETSC_FALSE, &flg, &flg2);CHKERRQ(ierr);
2853e902f1eaSVaclav Hapla     if (all || (flg && flg2)) {ierr = DMPlexCheckPointSF(dm);CHKERRQ(ierr);}
285475ebff7aSVaclav Hapla     ierr = PetscOptionsBool("-dm_plex_check_interface_cones", "Check points on inter-partition interfaces have conforming order of cone points", "DMPlexCheckInterfaceCones", PETSC_FALSE, &flg, &flg2);CHKERRQ(ierr);
2855e902f1eaSVaclav Hapla     if (all || (flg && flg2)) {ierr = DMPlexCheckInterfaceCones(dm);CHKERRQ(ierr);}
2856412e9a14SMatthew G. Knepley     ierr = PetscOptionsBool("-dm_plex_check_cell_shape", "Check cell shape", "DMPlexCheckCellShape", PETSC_FALSE, &flg, &flg2);CHKERRQ(ierr);
2857412e9a14SMatthew G. Knepley     if (flg && flg2) {ierr = DMPlexCheckCellShape(dm, PETSC_TRUE, PETSC_DETERMINE);CHKERRQ(ierr);}
2858f12cf164SMatthew G. Knepley   }
2859*9318fe57SMatthew G. Knepley   {
2860*9318fe57SMatthew G. Knepley     PetscReal scale = 1.0;
28614f3833eaSMatthew G. Knepley 
2862*9318fe57SMatthew G. Knepley     ierr = PetscOptionsReal("-dm_plex_scale", "Scale factor for mesh coordinates", "DMPlexScale", scale, &scale, &flg);CHKERRQ(ierr);
2863*9318fe57SMatthew G. Knepley     if (flg) {
2864*9318fe57SMatthew G. Knepley       Vec coordinates, coordinatesLocal;
2865*9318fe57SMatthew G. Knepley 
2866*9318fe57SMatthew G. Knepley       ierr = DMGetCoordinates(dm, &coordinates);CHKERRQ(ierr);
2867*9318fe57SMatthew G. Knepley       ierr = DMGetCoordinatesLocal(dm, &coordinatesLocal);CHKERRQ(ierr);
2868*9318fe57SMatthew G. Knepley       ierr = VecScale(coordinates, scale);CHKERRQ(ierr);
2869*9318fe57SMatthew G. Knepley       ierr = VecScale(coordinatesLocal, scale);CHKERRQ(ierr);
2870*9318fe57SMatthew G. Knepley     }
2871*9318fe57SMatthew G. Knepley   }
28724f3833eaSMatthew G. Knepley   ierr = PetscPartitionerSetFromOptions(mesh->partitioner);CHKERRQ(ierr);
287368d4fef7SMatthew G. Knepley   PetscFunctionReturn(0);
287468d4fef7SMatthew G. Knepley }
287568d4fef7SMatthew G. Knepley 
287646fa42a0SMatthew G. Knepley static PetscErrorCode DMSetFromOptions_Plex(PetscOptionItems *PetscOptionsObject,DM dm)
287768d4fef7SMatthew G. Knepley {
2878*9318fe57SMatthew G. Knepley   PetscReal      volume = -1.0, extThickness = 1.0;
2879*9318fe57SMatthew G. Knepley   PetscInt       prerefine = 0, refine = 0, r, coarsen = 0, overlap = 0, extLayers = 0, dim;
2880*9318fe57SMatthew G. Knepley   PetscBool      uniformOrig, created = PETSC_FALSE, uniform = PETSC_TRUE, distribute = PETSC_FALSE, interpolate = PETSC_TRUE, extColOrder = PETSC_TRUE, isHierarchy, flg;
288168d4fef7SMatthew G. Knepley   PetscErrorCode ierr;
288268d4fef7SMatthew G. Knepley 
288368d4fef7SMatthew G. Knepley   PetscFunctionBegin;
2884064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
28851a1499c8SBarry Smith   ierr = PetscOptionsHead(PetscOptionsObject,"DMPlex Options");CHKERRQ(ierr);
2886*9318fe57SMatthew G. Knepley   /* Handle automatic creation */
2887*9318fe57SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2888*9318fe57SMatthew G. Knepley   if (dim < 0) {ierr = DMPlexCreateFromOptions_Internal(PetscOptionsObject, dm);CHKERRQ(ierr);created = PETSC_TRUE;}
28899b44eab4SMatthew G. Knepley   /* Handle DMPlex refinement before distribution */
2890250712c9SMatthew G. Knepley   ierr = DMPlexGetRefinementUniform(dm, &uniformOrig);CHKERRQ(ierr);
2891*9318fe57SMatthew G. Knepley   ierr = PetscOptionsBoundedInt("-dm_refine_pre", "The number of refinements before distribution", "DMCreate", prerefine, &prerefine, NULL,0);CHKERRQ(ierr);
2892250712c9SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_refine_uniform_pre", "Flag for uniform refinement before distribution", "DMCreate", uniform, &uniform, &flg);CHKERRQ(ierr);
2893250712c9SMatthew G. Knepley   if (flg) {ierr = DMPlexSetRefinementUniform(dm, uniform);CHKERRQ(ierr);}
2894250712c9SMatthew G. Knepley   ierr = PetscOptionsReal("-dm_refine_volume_limit_pre", "The maximum cell volume after refinement before distribution", "DMCreate", volume, &volume, &flg);CHKERRQ(ierr);
2895*9318fe57SMatthew G. Knepley   if (flg) {
2896*9318fe57SMatthew G. Knepley     ierr = DMPlexSetRefinementUniform(dm, PETSC_FALSE);CHKERRQ(ierr);
2897*9318fe57SMatthew G. Knepley     ierr = DMPlexSetRefinementLimit(dm, volume);CHKERRQ(ierr);
2898*9318fe57SMatthew G. Knepley     prerefine = PetscMax(prerefine, 1);
2899*9318fe57SMatthew G. Knepley   }
29009b44eab4SMatthew G. Knepley   for (r = 0; r < prerefine; ++r) {
29019b44eab4SMatthew G. Knepley     DM             rdm;
29029b44eab4SMatthew G. Knepley     PetscPointFunc coordFunc = ((DM_Plex*) dm->data)->coordFunc;
29039b44eab4SMatthew G. Knepley 
29049b44eab4SMatthew G. Knepley     ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
29059b44eab4SMatthew G. Knepley     ierr = DMRefine(dm, PetscObjectComm((PetscObject) dm), &rdm);CHKERRQ(ierr);
29069b44eab4SMatthew G. Knepley     /* Total hack since we do not pass in a pointer */
2907*9318fe57SMatthew G. Knepley     ierr = DMPlexReplace_Static(dm, &rdm);CHKERRQ(ierr);
29089b44eab4SMatthew G. Knepley     ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
29099b44eab4SMatthew G. Knepley     if (coordFunc) {
29109b44eab4SMatthew G. Knepley       ierr = DMPlexRemapGeometry(dm, 0.0, coordFunc);CHKERRQ(ierr);
29119b44eab4SMatthew G. Knepley       ((DM_Plex*) dm->data)->coordFunc = coordFunc;
29129b44eab4SMatthew G. Knepley     }
29139b44eab4SMatthew G. Knepley   }
2914250712c9SMatthew G. Knepley   ierr = DMPlexSetRefinementUniform(dm, uniformOrig);CHKERRQ(ierr);
2915*9318fe57SMatthew G. Knepley   /* Handle DMPlex extrusion before distribution */
2916*9318fe57SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_interpolate", "Flag to create edges and faces automatically", "", interpolate, &interpolate, NULL);CHKERRQ(ierr);
2917*9318fe57SMatthew G. Knepley   ierr = PetscOptionsBoundedInt("-dm_extrude_layers", "The number of layers to extrude", "", extLayers, &extLayers, NULL, 0);CHKERRQ(ierr);
2918*9318fe57SMatthew G. Knepley   ierr = PetscOptionsReal("-dm_extrude_thickness", "The thickness of the layer to be extruded", "", extThickness, &extThickness, NULL);CHKERRQ(ierr);
2919*9318fe57SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_extrude_column_first", "Order the cells in a vertical column first", "", extColOrder, &extColOrder, NULL);CHKERRQ(ierr);
2920*9318fe57SMatthew G. Knepley   if (extLayers) {
2921*9318fe57SMatthew G. Knepley     DM edm;
2922*9318fe57SMatthew G. Knepley 
2923*9318fe57SMatthew G. Knepley     ierr = DMPlexExtrude(dm, extLayers, extThickness, extColOrder, NULL, interpolate, &edm);CHKERRQ(ierr);
2924*9318fe57SMatthew G. Knepley     ierr = DMPlexReplace_Static(dm, &edm);CHKERRQ(ierr);
2925*9318fe57SMatthew G. Knepley   }
29269b44eab4SMatthew G. Knepley   /* Handle DMPlex distribution */
29279b44eab4SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_distribute", "Flag to redistribute a mesh among processes", "DMCreate", distribute, &distribute, NULL);CHKERRQ(ierr);
29289b44eab4SMatthew G. Knepley   ierr = PetscOptionsBoundedInt("-dm_distribute_overlap", "The size of the overlap halo", "DMCreate", overlap, &overlap, NULL, 0);CHKERRQ(ierr);
29299b44eab4SMatthew G. Knepley   if (distribute) {
29309b44eab4SMatthew G. Knepley     DM               pdm = NULL;
29319b44eab4SMatthew G. Knepley     PetscPartitioner part;
29329b44eab4SMatthew G. Knepley 
29339b44eab4SMatthew G. Knepley     ierr = DMPlexGetPartitioner(dm, &part);CHKERRQ(ierr);
29349b44eab4SMatthew G. Knepley     ierr = PetscPartitionerSetFromOptions(part);CHKERRQ(ierr);
29359b44eab4SMatthew G. Knepley     ierr = DMPlexDistribute(dm, overlap, NULL, &pdm);CHKERRQ(ierr);
29369b44eab4SMatthew G. Knepley     if (pdm) {
2937*9318fe57SMatthew G. Knepley       ierr = DMPlexReplace_Static(dm, &pdm);CHKERRQ(ierr);
29389b44eab4SMatthew G. Knepley     }
29399b44eab4SMatthew G. Knepley   }
2940*9318fe57SMatthew G. Knepley   /* Create coordinate space */
2941*9318fe57SMatthew G. Knepley   if (created) {
2942*9318fe57SMatthew G. Knepley     PetscBool coordSpace = PETSC_TRUE;
2943*9318fe57SMatthew G. Knepley     PetscInt  degree     = 1;
2944*9318fe57SMatthew G. Knepley 
2945*9318fe57SMatthew G. Knepley     ierr = PetscOptionsBool("-dm_coord_space", "Use an FEM space for coordinates", "", coordSpace, &coordSpace, NULL);CHKERRQ(ierr);
2946*9318fe57SMatthew G. Knepley     ierr = PetscOptionsInt("-dm_coord_petscspace_degree", "FEM degree for coordinate space", "", degree, &degree, NULL);CHKERRQ(ierr);
2947*9318fe57SMatthew G. Knepley     if (coordSpace) {ierr = DMPlexCreateCoordinateSpace(dm, degree, NULL);CHKERRQ(ierr);}
2948*9318fe57SMatthew G. Knepley     ierr = DMLocalizeCoordinates(dm);CHKERRQ(ierr);
2949*9318fe57SMatthew G. Knepley   }
295068d4fef7SMatthew G. Knepley   /* Handle DMPlex refinement */
29515a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-dm_refine", "The number of uniform refinements", "DMCreate", refine, &refine, NULL,0);CHKERRQ(ierr);
29525a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-dm_refine_hierarchy", "The number of uniform refinements", "DMCreate", refine, &refine, &isHierarchy,0);CHKERRQ(ierr);
2953b6a0289aSMatthew G. Knepley   if (refine) {ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE);CHKERRQ(ierr);}
295468d4fef7SMatthew G. Knepley   if (refine && isHierarchy) {
2955acdc6f61SToby Isaac     DM *dms, coarseDM;
295668d4fef7SMatthew G. Knepley 
2957acdc6f61SToby Isaac     ierr = DMGetCoarseDM(dm, &coarseDM);CHKERRQ(ierr);
2958acdc6f61SToby Isaac     ierr = PetscObjectReference((PetscObject)coarseDM);CHKERRQ(ierr);
295968d4fef7SMatthew G. Knepley     ierr = PetscMalloc1(refine,&dms);CHKERRQ(ierr);
296068d4fef7SMatthew G. Knepley     ierr = DMRefineHierarchy(dm, refine, dms);CHKERRQ(ierr);
296168d4fef7SMatthew G. Knepley     /* Total hack since we do not pass in a pointer */
296268d4fef7SMatthew G. Knepley     ierr = DMPlexSwap_Static(dm, dms[refine-1]);CHKERRQ(ierr);
296368d4fef7SMatthew G. Knepley     if (refine == 1) {
2964a8fb8f29SToby Isaac       ierr = DMSetCoarseDM(dm, dms[0]);CHKERRQ(ierr);
29650aef6b92SMatthew G. Knepley       ierr = DMPlexSetRegularRefinement(dm, PETSC_TRUE);CHKERRQ(ierr);
296668d4fef7SMatthew G. Knepley     } else {
2967a8fb8f29SToby Isaac       ierr = DMSetCoarseDM(dm, dms[refine-2]);CHKERRQ(ierr);
29680aef6b92SMatthew G. Knepley       ierr = DMPlexSetRegularRefinement(dm, PETSC_TRUE);CHKERRQ(ierr);
2969a8fb8f29SToby Isaac       ierr = DMSetCoarseDM(dms[0], dms[refine-1]);CHKERRQ(ierr);
29700aef6b92SMatthew G. Knepley       ierr = DMPlexSetRegularRefinement(dms[0], PETSC_TRUE);CHKERRQ(ierr);
297168d4fef7SMatthew G. Knepley     }
2972acdc6f61SToby Isaac     ierr = DMSetCoarseDM(dms[refine-1], coarseDM);CHKERRQ(ierr);
2973acdc6f61SToby Isaac     ierr = PetscObjectDereference((PetscObject)coarseDM);CHKERRQ(ierr);
297468d4fef7SMatthew G. Knepley     /* Free DMs */
297568d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
2976547f7119SMatthew G. Knepley       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]);CHKERRQ(ierr);
297768d4fef7SMatthew G. Knepley       ierr = DMDestroy(&dms[r]);CHKERRQ(ierr);
297868d4fef7SMatthew G. Knepley     }
297968d4fef7SMatthew G. Knepley     ierr = PetscFree(dms);CHKERRQ(ierr);
298068d4fef7SMatthew G. Knepley   } else {
298168d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
2982*9318fe57SMatthew G. Knepley       DM             rdm;
298351a74b61SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex*) dm->data)->coordFunc;
298468d4fef7SMatthew G. Knepley 
29851a1499c8SBarry Smith       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
2986*9318fe57SMatthew G. Knepley       ierr = DMRefine(dm, PetscObjectComm((PetscObject) dm), &rdm);CHKERRQ(ierr);
298768d4fef7SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
2988*9318fe57SMatthew G. Knepley       ierr = DMPlexReplace_Static(dm, &rdm);CHKERRQ(ierr);
2989547f7119SMatthew G. Knepley       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
299051a74b61SMatthew G. Knepley       if (coordFunc) {
299151a74b61SMatthew G. Knepley         ierr = DMPlexRemapGeometry(dm, 0.0, coordFunc);CHKERRQ(ierr);
299251a74b61SMatthew G. Knepley         ((DM_Plex*) dm->data)->coordFunc = coordFunc;
299351a74b61SMatthew G. Knepley       }
299468d4fef7SMatthew G. Knepley     }
299568d4fef7SMatthew G. Knepley   }
29963cf6fe12SMatthew G. Knepley   /* Handle DMPlex coarsening */
29975a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-dm_coarsen", "Coarsen the mesh", "DMCreate", coarsen, &coarsen, NULL,0);CHKERRQ(ierr);
29985a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-dm_coarsen_hierarchy", "The number of coarsenings", "DMCreate", coarsen, &coarsen, &isHierarchy,0);CHKERRQ(ierr);
2999b653a561SMatthew G. Knepley   if (coarsen && isHierarchy) {
3000b653a561SMatthew G. Knepley     DM *dms;
3001b653a561SMatthew G. Knepley 
3002b653a561SMatthew G. Knepley     ierr = PetscMalloc1(coarsen, &dms);CHKERRQ(ierr);
3003b653a561SMatthew G. Knepley     ierr = DMCoarsenHierarchy(dm, coarsen, dms);CHKERRQ(ierr);
3004b653a561SMatthew G. Knepley     /* Free DMs */
3005b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
3006547f7119SMatthew G. Knepley       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]);CHKERRQ(ierr);
3007b653a561SMatthew G. Knepley       ierr = DMDestroy(&dms[r]);CHKERRQ(ierr);
3008b653a561SMatthew G. Knepley     }
3009b653a561SMatthew G. Knepley     ierr = PetscFree(dms);CHKERRQ(ierr);
3010b653a561SMatthew G. Knepley   } else {
3011b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
3012*9318fe57SMatthew G. Knepley       DM             cdm;
3013*9318fe57SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex*) dm->data)->coordFunc;
30143cf6fe12SMatthew G. Knepley 
30153cf6fe12SMatthew G. Knepley       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
3016*9318fe57SMatthew G. Knepley       ierr = DMCoarsen(dm, PetscObjectComm((PetscObject) dm), &cdm);CHKERRQ(ierr);
30173cf6fe12SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
3018*9318fe57SMatthew G. Knepley       ierr = DMPlexReplace_Static(dm, &cdm);CHKERRQ(ierr);
3019547f7119SMatthew G. Knepley       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
3020*9318fe57SMatthew G. Knepley       if (coordFunc) {
3021*9318fe57SMatthew G. Knepley         ierr = DMPlexRemapGeometry(dm, 0.0, coordFunc);CHKERRQ(ierr);
3022*9318fe57SMatthew G. Knepley         ((DM_Plex*) dm->data)->coordFunc = coordFunc;
3023*9318fe57SMatthew G. Knepley       }
30243cf6fe12SMatthew G. Knepley     }
3025b653a561SMatthew G. Knepley   }
30263cf6fe12SMatthew G. Knepley   /* Handle */
30271a1499c8SBarry Smith   ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
30280a6ba040SMatthew G. Knepley   ierr = PetscOptionsTail();CHKERRQ(ierr);
30290a6ba040SMatthew G. Knepley   PetscFunctionReturn(0);
30300a6ba040SMatthew G. Knepley }
30310a6ba040SMatthew G. Knepley 
3032552f7358SJed Brown static PetscErrorCode DMCreateGlobalVector_Plex(DM dm,Vec *vec)
3033552f7358SJed Brown {
3034552f7358SJed Brown   PetscErrorCode ierr;
3035552f7358SJed Brown 
3036552f7358SJed Brown   PetscFunctionBegin;
3037552f7358SJed Brown   ierr = DMCreateGlobalVector_Section_Private(dm,vec);CHKERRQ(ierr);
3038552f7358SJed Brown   /* ierr = VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM);CHKERRQ(ierr); */
3039552f7358SJed Brown   ierr = VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex);CHKERRQ(ierr);
3040d930f514SMatthew G. Knepley   ierr = VecSetOperation(*vec, VECOP_VIEWNATIVE, (void (*)(void)) VecView_Plex_Native);CHKERRQ(ierr);
30412c40f234SMatthew G. Knepley   ierr = VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex);CHKERRQ(ierr);
3042d930f514SMatthew G. Knepley   ierr = VecSetOperation(*vec, VECOP_LOADNATIVE, (void (*)(void)) VecLoad_Plex_Native);CHKERRQ(ierr);
3043552f7358SJed Brown   PetscFunctionReturn(0);
3044552f7358SJed Brown }
3045552f7358SJed Brown 
3046552f7358SJed Brown static PetscErrorCode DMCreateLocalVector_Plex(DM dm,Vec *vec)
3047552f7358SJed Brown {
3048552f7358SJed Brown   PetscErrorCode ierr;
3049552f7358SJed Brown 
3050552f7358SJed Brown   PetscFunctionBegin;
3051552f7358SJed Brown   ierr = DMCreateLocalVector_Section_Private(dm,vec);CHKERRQ(ierr);
3052552f7358SJed Brown   ierr = VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex_Local);CHKERRQ(ierr);
30532c40f234SMatthew G. Knepley   ierr = VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex_Local);CHKERRQ(ierr);
3054552f7358SJed Brown   PetscFunctionReturn(0);
3055552f7358SJed Brown }
3056552f7358SJed Brown 
3057793f3fe5SMatthew G. Knepley static PetscErrorCode DMGetDimPoints_Plex(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
3058793f3fe5SMatthew G. Knepley {
3059793f3fe5SMatthew G. Knepley   PetscInt       depth, d;
3060793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
3061793f3fe5SMatthew G. Knepley 
3062793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
3063793f3fe5SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3064793f3fe5SMatthew G. Knepley   if (depth == 1) {
3065793f3fe5SMatthew G. Knepley     ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
3066793f3fe5SMatthew G. Knepley     if (dim == 0)      {ierr = DMPlexGetDepthStratum(dm, dim, pStart, pEnd);CHKERRQ(ierr);}
3067793f3fe5SMatthew G. Knepley     else if (dim == d) {ierr = DMPlexGetDepthStratum(dm, 1, pStart, pEnd);CHKERRQ(ierr);}
3068793f3fe5SMatthew G. Knepley     else               {*pStart = 0; *pEnd = 0;}
3069793f3fe5SMatthew G. Knepley   } else {
3070793f3fe5SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, dim, pStart, pEnd);CHKERRQ(ierr);
3071793f3fe5SMatthew G. Knepley   }
3072793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
3073793f3fe5SMatthew G. Knepley }
3074793f3fe5SMatthew G. Knepley 
307528d58a37SPierre Jolivet static PetscErrorCode DMGetNeighbors_Plex(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
3076502a2867SDave May {
3077502a2867SDave May   PetscSF           sf;
30780a19bb7dSprj-   PetscInt          niranks, njranks, n;
30790a19bb7dSprj-   const PetscMPIInt *iranks, *jranks;
30800a19bb7dSprj-   DM_Plex           *data = (DM_Plex*) dm->data;
30812f356facSMatthew G. Knepley   PetscErrorCode    ierr;
3082502a2867SDave May 
30832f356facSMatthew G. Knepley   PetscFunctionBegin;
3084502a2867SDave May   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
30850a19bb7dSprj-   if (!data->neighbors) {
308607a779feSPierre Jolivet     ierr = PetscSFSetUp(sf);CHKERRQ(ierr);
30870a19bb7dSprj-     ierr = PetscSFGetRootRanks(sf, &njranks, &jranks, NULL, NULL, NULL);CHKERRQ(ierr);
30880a19bb7dSprj-     ierr = PetscSFGetLeafRanks(sf, &niranks, &iranks, NULL, NULL);CHKERRQ(ierr);
30890a19bb7dSprj-     ierr = PetscMalloc1(njranks + niranks + 1, &data->neighbors);CHKERRQ(ierr);
30900a19bb7dSprj-     ierr = PetscArraycpy(data->neighbors + 1, jranks, njranks);CHKERRQ(ierr);
30910a19bb7dSprj-     ierr = PetscArraycpy(data->neighbors + njranks + 1, iranks, niranks);CHKERRQ(ierr);
30920a19bb7dSprj-     n = njranks + niranks;
30930a19bb7dSprj-     ierr = PetscSortRemoveDupsMPIInt(&n, data->neighbors + 1);CHKERRQ(ierr);
30940a19bb7dSprj-     /* The following cast should never fail: can't have more neighbors than PETSC_MPI_INT_MAX */
30950a19bb7dSprj-     ierr = PetscMPIIntCast(n, data->neighbors);CHKERRQ(ierr);
30960a19bb7dSprj-   }
30970a19bb7dSprj-   if (nranks) *nranks = data->neighbors[0];
30980a19bb7dSprj-   if (ranks) {
30990a19bb7dSprj-     if (data->neighbors[0]) *ranks = data->neighbors + 1;
31000a19bb7dSprj-     else                    *ranks = NULL;
31010a19bb7dSprj-   }
3102502a2867SDave May   PetscFunctionReturn(0);
3103502a2867SDave May }
3104502a2867SDave May 
31051eb70e55SToby Isaac PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM, DM, Mat, Vec, Vec);
31061eb70e55SToby Isaac 
310746fa42a0SMatthew G. Knepley static PetscErrorCode DMInitialize_Plex(DM dm)
3108552f7358SJed Brown {
3109713918a9SToby Isaac   PetscErrorCode ierr;
3110713918a9SToby Isaac 
3111552f7358SJed Brown   PetscFunctionBegin;
3112552f7358SJed Brown   dm->ops->view                            = DMView_Plex;
31132c40f234SMatthew G. Knepley   dm->ops->load                            = DMLoad_Plex;
3114552f7358SJed Brown   dm->ops->setfromoptions                  = DMSetFromOptions_Plex;
311538221697SMatthew G. Knepley   dm->ops->clone                           = DMClone_Plex;
3116552f7358SJed Brown   dm->ops->setup                           = DMSetUp_Plex;
31171bb6d2a8SBarry Smith   dm->ops->createlocalsection              = DMCreateLocalSection_Plex;
311866ad2231SToby Isaac   dm->ops->createdefaultconstraints        = DMCreateDefaultConstraints_Plex;
3119552f7358SJed Brown   dm->ops->createglobalvector              = DMCreateGlobalVector_Plex;
3120552f7358SJed Brown   dm->ops->createlocalvector               = DMCreateLocalVector_Plex;
3121184d77edSJed Brown   dm->ops->getlocaltoglobalmapping         = NULL;
31220298fd71SBarry Smith   dm->ops->createfieldis                   = NULL;
3123552f7358SJed Brown   dm->ops->createcoordinatedm              = DMCreateCoordinateDM_Plex;
3124f19dbd58SToby Isaac   dm->ops->createcoordinatefield           = DMCreateCoordinateField_Plex;
31250a6ba040SMatthew G. Knepley   dm->ops->getcoloring                     = NULL;
3126552f7358SJed Brown   dm->ops->creatematrix                    = DMCreateMatrix_Plex;
3127bceba477SMatthew G. Knepley   dm->ops->createinterpolation             = DMCreateInterpolation_Plex;
3128bd041c0cSMatthew G. Knepley   dm->ops->createmassmatrix                = DMCreateMassMatrix_Plex;
31295a84ad33SLisandro Dalcin   dm->ops->createinjection                 = DMCreateInjection_Plex;
3130552f7358SJed Brown   dm->ops->refine                          = DMRefine_Plex;
31310a6ba040SMatthew G. Knepley   dm->ops->coarsen                         = DMCoarsen_Plex;
31320a6ba040SMatthew G. Knepley   dm->ops->refinehierarchy                 = DMRefineHierarchy_Plex;
3133b653a561SMatthew G. Knepley   dm->ops->coarsenhierarchy                = DMCoarsenHierarchy_Plex;
31340d1cd5e0SMatthew G. Knepley   dm->ops->adaptlabel                      = DMAdaptLabel_Plex;
31350d1cd5e0SMatthew G. Knepley   dm->ops->adaptmetric                     = DMAdaptMetric_Plex;
31360298fd71SBarry Smith   dm->ops->globaltolocalbegin              = NULL;
31370298fd71SBarry Smith   dm->ops->globaltolocalend                = NULL;
31380298fd71SBarry Smith   dm->ops->localtoglobalbegin              = NULL;
31390298fd71SBarry Smith   dm->ops->localtoglobalend                = NULL;
3140552f7358SJed Brown   dm->ops->destroy                         = DMDestroy_Plex;
3141552f7358SJed Brown   dm->ops->createsubdm                     = DMCreateSubDM_Plex;
31422adcc780SMatthew G. Knepley   dm->ops->createsuperdm                   = DMCreateSuperDM_Plex;
3143793f3fe5SMatthew G. Knepley   dm->ops->getdimpoints                    = DMGetDimPoints_Plex;
3144552f7358SJed Brown   dm->ops->locatepoints                    = DMLocatePoints_Plex;
31450709b2feSToby Isaac   dm->ops->projectfunctionlocal            = DMProjectFunctionLocal_Plex;
31460709b2feSToby Isaac   dm->ops->projectfunctionlabellocal       = DMProjectFunctionLabelLocal_Plex;
3147bfc4295aSToby Isaac   dm->ops->projectfieldlocal               = DMProjectFieldLocal_Plex;
31488c6c5593SMatthew G. Knepley   dm->ops->projectfieldlabellocal          = DMProjectFieldLabelLocal_Plex;
3149ece3a9fcSMatthew G. Knepley   dm->ops->projectbdfieldlabellocal        = DMProjectBdFieldLabelLocal_Plex;
31500709b2feSToby Isaac   dm->ops->computel2diff                   = DMComputeL2Diff_Plex;
3151b698f381SToby Isaac   dm->ops->computel2gradientdiff           = DMComputeL2GradientDiff_Plex;
31522a16baeaSToby Isaac   dm->ops->computel2fielddiff              = DMComputeL2FieldDiff_Plex;
315328d58a37SPierre Jolivet   dm->ops->getneighbors                    = DMGetNeighbors_Plex;
3154f1d73a7aSMatthew G. Knepley   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C",DMPlexInsertBoundaryValues_Plex);CHKERRQ(ierr);
315556cf3b9cSMatthew G. Knepley   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertTimeDerviativeBoundaryValues_C",DMPlexInsertTimeDerivativeBoundaryValues_Plex);CHKERRQ(ierr);
31568135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",DMSetUpGLVisViewer_Plex);CHKERRQ(ierr);
315728d58a37SPierre Jolivet   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMCreateNeumannOverlap_C",DMCreateNeumannOverlap_Plex);CHKERRQ(ierr);
3158cb54e036SVaclav Hapla   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexGetOverlap_C",DMPlexGetOverlap_Plex);CHKERRQ(ierr);
31591eb70e55SToby Isaac   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMInterpolateSolution_C",DMInterpolateSolution_Plex);CHKERRQ(ierr);
3160552f7358SJed Brown   PetscFunctionReturn(0);
3161552f7358SJed Brown }
3162552f7358SJed Brown 
316346fa42a0SMatthew G. Knepley PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm)
316463a16f15SMatthew G. Knepley {
316563a16f15SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex *) dm->data;
316663a16f15SMatthew G. Knepley   PetscErrorCode ierr;
316763a16f15SMatthew G. Knepley 
316863a16f15SMatthew G. Knepley   PetscFunctionBegin;
316963a16f15SMatthew G. Knepley   mesh->refct++;
317063a16f15SMatthew G. Knepley   (*newdm)->data = mesh;
317163a16f15SMatthew G. Knepley   ierr = PetscObjectChangeTypeName((PetscObject) *newdm, DMPLEX);CHKERRQ(ierr);
317263a16f15SMatthew G. Knepley   ierr = DMInitialize_Plex(*newdm);CHKERRQ(ierr);
317363a16f15SMatthew G. Knepley   PetscFunctionReturn(0);
317463a16f15SMatthew G. Knepley }
317563a16f15SMatthew G. Knepley 
31768818961aSMatthew G Knepley /*MC
31778818961aSMatthew G Knepley   DMPLEX = "plex" - A DM object that encapsulates an unstructured mesh, or CW Complex, which can be expressed using a Hasse Diagram.
31788818961aSMatthew G Knepley                     In the local representation, Vecs contain all unknowns in the interior and shared boundary. This is
31798818961aSMatthew G Knepley                     specified by a PetscSection object. Ownership in the global representation is determined by
31808818961aSMatthew G Knepley                     ownership of the underlying DMPlex points. This is specified by another PetscSection object.
31818818961aSMatthew G Knepley 
3182e5893cccSMatthew G. Knepley   Options Database Keys:
3183250712c9SMatthew G. Knepley + -dm_refine_pre                     - Refine mesh before distribution
3184250712c9SMatthew G. Knepley + -dm_refine_uniform_pre             - Choose uniform or generator-based refinement
3185250712c9SMatthew G. Knepley + -dm_refine_volume_limit_pre        - Cell volume limit after pre-refinement using generator
3186250712c9SMatthew G. Knepley . -dm_distribute                     - Distribute mesh across processes
3187250712c9SMatthew G. Knepley . -dm_distribute_overlap             - Number of cells to overlap for distribution
3188250712c9SMatthew G. Knepley . -dm_refine                         - Refine mesh after distribution
3189250712c9SMatthew G. Knepley . -dm_plex_hash_location             - Use grid hashing for point location
3190f12cf164SMatthew G. Knepley . -dm_plex_partition_balance         - Attempt to evenly divide points on partition boundary between processes
3191f12cf164SMatthew G. Knepley . -dm_plex_remesh_bd                 - Allow changes to the boundary on remeshing
3192f12cf164SMatthew G. Knepley . -dm_plex_max_projection_height     - Maxmimum mesh point height used to project locally
3193f12cf164SMatthew G. Knepley . -dm_plex_regular_refinement        - Use special nested projection algorithm for regular refinement
3194250712c9SMatthew G. Knepley . -dm_plex_check_all                 - Perform all shecks below
3195f12cf164SMatthew G. Knepley . -dm_plex_check_symmetry            - Check that the adjacency information in the mesh is symmetric
3196f12cf164SMatthew G. Knepley . -dm_plex_check_skeleton <celltype> - Check that each cell has the correct number of vertices
3197f12cf164SMatthew G. Knepley . -dm_plex_check_faces <celltype>    - Check that the faces of each cell give a vertex order this is consistent with what we expect from the cell type
3198f12cf164SMatthew G. Knepley . -dm_plex_check_geometry            - Check that cells have positive volume
3199f12cf164SMatthew G. Knepley . -dm_view :mesh.tex:ascii_latex     - View the mesh in LaTeX/TikZ
3200e5893cccSMatthew G. Knepley . -dm_plex_view_scale <num>          - Scale the TikZ
3201e5893cccSMatthew G. Knepley - -dm_plex_print_fem <num>           - View FEM assembly information, such as element vectors and matrices
3202e5893cccSMatthew G. Knepley 
3203e5893cccSMatthew G. Knepley 
32048818961aSMatthew G Knepley   Level: intermediate
32058818961aSMatthew G Knepley 
32068818961aSMatthew G Knepley .seealso: DMType, DMPlexCreate(), DMCreate(), DMSetType()
32078818961aSMatthew G Knepley M*/
32088818961aSMatthew G Knepley 
32098cc058d9SJed Brown PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm)
3210552f7358SJed Brown {
3211552f7358SJed Brown   DM_Plex       *mesh;
3212412e9a14SMatthew G. Knepley   PetscInt       unit;
3213552f7358SJed Brown   PetscErrorCode ierr;
3214552f7358SJed Brown 
3215552f7358SJed Brown   PetscFunctionBegin;
3216552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3217b00a9115SJed Brown   ierr     = PetscNewLog(dm,&mesh);CHKERRQ(ierr);
3218552f7358SJed Brown   dm->data = mesh;
3219552f7358SJed Brown 
3220552f7358SJed Brown   mesh->refct             = 1;
322182f516ccSBarry Smith   ierr                    = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection);CHKERRQ(ierr);
3222552f7358SJed Brown   mesh->maxConeSize       = 0;
32230298fd71SBarry Smith   mesh->cones             = NULL;
32240298fd71SBarry Smith   mesh->coneOrientations  = NULL;
322582f516ccSBarry Smith   ierr                    = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection);CHKERRQ(ierr);
3226552f7358SJed Brown   mesh->maxSupportSize    = 0;
32270298fd71SBarry Smith   mesh->supports          = NULL;
3228552f7358SJed Brown   mesh->refinementUniform = PETSC_TRUE;
3229552f7358SJed Brown   mesh->refinementLimit   = -1.0;
32307d0f5628SVaclav Hapla   mesh->interpolated      = DMPLEX_INTERPOLATED_INVALID;
32317d0f5628SVaclav Hapla   mesh->interpolatedCollective = DMPLEX_INTERPOLATED_INVALID;
3232552f7358SJed Brown 
32330298fd71SBarry Smith   mesh->facesTmp = NULL;
3234552f7358SJed Brown 
3235d9deefdfSMatthew G. Knepley   mesh->tetgenOpts   = NULL;
3236d9deefdfSMatthew G. Knepley   mesh->triangleOpts = NULL;
323777623264SMatthew G. Knepley   ierr = PetscPartitionerCreate(PetscObjectComm((PetscObject)dm), &mesh->partitioner);CHKERRQ(ierr);
32382e62ab5aSMatthew G. Knepley   mesh->remeshBd     = PETSC_FALSE;
3239d9deefdfSMatthew G. Knepley 
32400298fd71SBarry Smith   mesh->subpointMap = NULL;
3241552f7358SJed Brown 
32428865f1eaSKarl Rupp   for (unit = 0; unit < NUM_PETSC_UNITS; ++unit) mesh->scale[unit] = 1.0;
3243552f7358SJed Brown 
32440aef6b92SMatthew G. Knepley   mesh->regularRefinement   = PETSC_FALSE;
3245df0420ecSMatthew G. Knepley   mesh->depthState          = -1;
3246ba2698f1SMatthew G. Knepley   mesh->celltypeState       = -1;
32470298fd71SBarry Smith   mesh->globalVertexNumbers = NULL;
32480298fd71SBarry Smith   mesh->globalCellNumbers   = NULL;
3249a68b90caSToby Isaac   mesh->anchorSection       = NULL;
3250a68b90caSToby Isaac   mesh->anchorIS            = NULL;
325141e6d900SToby Isaac   mesh->createanchors       = NULL;
3252fa73a4e1SToby Isaac   mesh->computeanchormatrix = NULL;
3253d961a43aSToby Isaac   mesh->parentSection       = NULL;
3254d961a43aSToby Isaac   mesh->parents             = NULL;
3255d961a43aSToby Isaac   mesh->childIDs            = NULL;
3256d961a43aSToby Isaac   mesh->childSection        = NULL;
3257d961a43aSToby Isaac   mesh->children            = NULL;
3258d6a7ad0dSToby Isaac   mesh->referenceTree       = NULL;
3259dcbd3bf7SToby Isaac   mesh->getchildsymmetry    = NULL;
3260552f7358SJed Brown   mesh->vtkCellHeight       = 0;
3261e228b242SToby Isaac   mesh->useAnchors          = PETSC_FALSE;
3262552f7358SJed Brown 
3263b29cfa1cSToby Isaac   mesh->maxProjectionHeight = 0;
3264b29cfa1cSToby Isaac 
32650a19bb7dSprj-   mesh->neighbors           = NULL;
32660a19bb7dSprj- 
3267552f7358SJed Brown   mesh->printSetValues = PETSC_FALSE;
3268552f7358SJed Brown   mesh->printFEM       = 0;
32696113b454SMatthew G. Knepley   mesh->printTol       = 1.0e-10;
3270552f7358SJed Brown 
3271552f7358SJed Brown   ierr = DMInitialize_Plex(dm);CHKERRQ(ierr);
3272552f7358SJed Brown   PetscFunctionReturn(0);
3273552f7358SJed Brown }
3274552f7358SJed Brown 
3275552f7358SJed Brown /*@
3276552f7358SJed Brown   DMPlexCreate - Creates a DMPlex object, which encapsulates an unstructured mesh, or CW complex, which can be expressed using a Hasse Diagram.
3277552f7358SJed Brown 
3278d083f849SBarry Smith   Collective
3279552f7358SJed Brown 
3280552f7358SJed Brown   Input Parameter:
3281552f7358SJed Brown . comm - The communicator for the DMPlex object
3282552f7358SJed Brown 
3283552f7358SJed Brown   Output Parameter:
3284552f7358SJed Brown . mesh  - The DMPlex object
3285552f7358SJed Brown 
3286552f7358SJed Brown   Level: beginner
3287552f7358SJed Brown 
3288552f7358SJed Brown @*/
3289552f7358SJed Brown PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh)
3290552f7358SJed Brown {
3291552f7358SJed Brown   PetscErrorCode ierr;
3292552f7358SJed Brown 
3293552f7358SJed Brown   PetscFunctionBegin;
3294552f7358SJed Brown   PetscValidPointer(mesh,2);
3295552f7358SJed Brown   ierr = DMCreate(comm, mesh);CHKERRQ(ierr);
3296552f7358SJed Brown   ierr = DMSetType(*mesh, DMPLEX);CHKERRQ(ierr);
3297552f7358SJed Brown   PetscFunctionReturn(0);
3298552f7358SJed Brown }
3299552f7358SJed Brown 
3300b09969d6SVaclav Hapla /*@C
3301b09969d6SVaclav Hapla   DMPlexBuildFromCellListParallel - Build distributed DMPLEX topology from a list of vertices for each cell (common mesh generator output)
3302b09969d6SVaclav Hapla 
3303b09969d6SVaclav Hapla   Input Parameters:
3304b09969d6SVaclav Hapla + dm - The DM
3305b09969d6SVaclav Hapla . numCells - The number of cells owned by this process
330625b6865aSVaclav Hapla . numVertices - The number of vertices owned by this process, or PETSC_DECIDE
330725b6865aSVaclav Hapla . NVertices - The global number of vertices, or PETSC_DECIDE
3308b09969d6SVaclav Hapla . numCorners - The number of vertices for each cell
33095e488331SVaclav Hapla - cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
3310b09969d6SVaclav Hapla 
3311b09969d6SVaclav Hapla   Output Parameter:
33128ddadb14SVaclav Hapla . vertexSF - (Optional) SF describing complete vertex ownership
3313b09969d6SVaclav Hapla 
3314b09969d6SVaclav Hapla   Notes:
3315b09969d6SVaclav Hapla   Two triangles sharing a face
3316b09969d6SVaclav Hapla $
3317b09969d6SVaclav Hapla $        2
3318b09969d6SVaclav Hapla $      / | \
3319b09969d6SVaclav Hapla $     /  |  \
3320b09969d6SVaclav Hapla $    /   |   \
3321b09969d6SVaclav Hapla $   0  0 | 1  3
3322b09969d6SVaclav Hapla $    \   |   /
3323b09969d6SVaclav Hapla $     \  |  /
3324b09969d6SVaclav Hapla $      \ | /
3325b09969d6SVaclav Hapla $        1
3326b09969d6SVaclav Hapla would have input
3327b09969d6SVaclav Hapla $  numCells = 2, numVertices = 4
3328b09969d6SVaclav Hapla $  cells = [0 1 2  1 3 2]
3329b09969d6SVaclav Hapla $
3330b09969d6SVaclav Hapla which would result in the DMPlex
3331b09969d6SVaclav Hapla $
3332b09969d6SVaclav Hapla $        4
3333b09969d6SVaclav Hapla $      / | \
3334b09969d6SVaclav Hapla $     /  |  \
3335b09969d6SVaclav Hapla $    /   |   \
3336b09969d6SVaclav Hapla $   2  0 | 1  5
3337b09969d6SVaclav Hapla $    \   |   /
3338b09969d6SVaclav Hapla $     \  |  /
3339b09969d6SVaclav Hapla $      \ | /
3340b09969d6SVaclav Hapla $        3
3341b09969d6SVaclav Hapla 
334225b6865aSVaclav Hapla   Vertices are implicitly numbered consecutively 0,...,NVertices.
334325b6865aSVaclav Hapla   Each rank owns a chunk of numVertices consecutive vertices.
334425b6865aSVaclav Hapla   If numVertices is PETSC_DECIDE, PETSc will distribute them as evenly as possible using PetscLayout.
334525b6865aSVaclav Hapla   If both NVertices and numVertices are PETSC_DECIDE, NVertices is computed by PETSc as the maximum vertex index in cells + 1.
334625b6865aSVaclav Hapla   If only NVertices is PETSC_DECIDE, it is computed as the sum of numVertices over all ranks.
334725b6865aSVaclav Hapla 
3348b09969d6SVaclav Hapla   The cell distribution is arbitrary non-overlapping, independent of the vertex distribution.
3349b09969d6SVaclav Hapla 
3350b09969d6SVaclav Hapla   Not currently supported in Fortran.
3351b09969d6SVaclav Hapla 
3352b09969d6SVaclav Hapla   Level: advanced
3353b09969d6SVaclav Hapla 
3354b09969d6SVaclav Hapla .seealso: DMPlexBuildFromCellList(), DMPlexCreateFromCellListParallelPetsc(), DMPlexBuildCoordinatesFromCellListParallel()
3355b09969d6SVaclav Hapla @*/
335625b6865aSVaclav Hapla PetscErrorCode DMPlexBuildFromCellListParallel(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscInt numCorners, const PetscInt cells[], PetscSF *vertexSF)
3357a47d0d45SMatthew G. Knepley {
33582464107aSksagiyam   PetscSF         sfPoint;
33592464107aSksagiyam   PetscLayout     layout;
33602464107aSksagiyam   PetscInt        numVerticesAdj, *verticesAdj, *cones, c, p, dim;
33619852e123SBarry Smith   PetscMPIInt     rank, size;
3362a47d0d45SMatthew G. Knepley   PetscErrorCode  ierr;
3363a47d0d45SMatthew G. Knepley 
3364a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
336525b6865aSVaclav Hapla   PetscValidLogicalCollectiveInt(dm,NVertices,4);
3366b09969d6SVaclav Hapla   ierr = PetscLogEventBegin(DMPLEX_BuildFromCellList,dm,0,0,0);CHKERRQ(ierr);
3367ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
3368ffc4695bSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size);CHKERRMPI(ierr);
33696cbf6523SVaclav Hapla   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
337025b6865aSVaclav Hapla   /* Get/check global number of vertices */
337125b6865aSVaclav Hapla   {
337225b6865aSVaclav Hapla     PetscInt NVerticesInCells, i;
337325b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
337425b6865aSVaclav Hapla 
337525b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
337625b6865aSVaclav Hapla     NVerticesInCells = PETSC_MIN_INT;
337725b6865aSVaclav Hapla     for (i=0; i<len; i++) if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
337825b6865aSVaclav Hapla     ++NVerticesInCells;
3379ffc4695bSBarry Smith     ierr = MPI_Allreduce(MPI_IN_PLACE, &NVerticesInCells, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);
338025b6865aSVaclav Hapla 
338125b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE && NVertices == PETSC_DECIDE) NVertices = NVerticesInCells;
338225b6865aSVaclav Hapla     else if (NVertices != PETSC_DECIDE && NVertices < NVerticesInCells) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Specified global number of vertices %D must be greater than or equal to the number of vertices in cells %D",NVertices,NVerticesInCells);
338325b6865aSVaclav Hapla   }
33849079aca8SVaclav Hapla   /* Count locally unique vertices */
33859079aca8SVaclav Hapla   {
33869079aca8SVaclav Hapla     PetscHSetI vhash;
33879079aca8SVaclav Hapla     PetscInt off = 0;
33889079aca8SVaclav Hapla 
3389e8f14785SLisandro Dalcin     ierr = PetscHSetICreate(&vhash);CHKERRQ(ierr);
3390a47d0d45SMatthew G. Knepley     for (c = 0; c < numCells; ++c) {
3391a47d0d45SMatthew G. Knepley       for (p = 0; p < numCorners; ++p) {
3392e8f14785SLisandro Dalcin         ierr = PetscHSetIAdd(vhash, cells[c*numCorners+p]);CHKERRQ(ierr);
3393a47d0d45SMatthew G. Knepley       }
3394a47d0d45SMatthew G. Knepley     }
3395e8f14785SLisandro Dalcin     ierr = PetscHSetIGetSize(vhash, &numVerticesAdj);CHKERRQ(ierr);
3396a47d0d45SMatthew G. Knepley     ierr = PetscMalloc1(numVerticesAdj, &verticesAdj);CHKERRQ(ierr);
3397e8f14785SLisandro Dalcin     ierr = PetscHSetIGetElems(vhash, &off, verticesAdj);CHKERRQ(ierr);
3398e8f14785SLisandro Dalcin     ierr = PetscHSetIDestroy(&vhash);CHKERRQ(ierr);
3399a47d0d45SMatthew G. Knepley     if (off != numVerticesAdj) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid number of local vertices %D should be %D", off, numVerticesAdj);
3400a47d0d45SMatthew G. Knepley   }
34019079aca8SVaclav Hapla   ierr = PetscSortInt(numVerticesAdj, verticesAdj);CHKERRQ(ierr);
3402a47d0d45SMatthew G. Knepley   /* Create cones */
3403a47d0d45SMatthew G. Knepley   ierr = DMPlexSetChart(dm, 0, numCells+numVerticesAdj);CHKERRQ(ierr);
3404a47d0d45SMatthew G. Knepley   for (c = 0; c < numCells; ++c) {ierr = DMPlexSetConeSize(dm, c, numCorners);CHKERRQ(ierr);}
3405a47d0d45SMatthew G. Knepley   ierr = DMSetUp(dm);CHKERRQ(ierr);
3406961cfab0SVaclav Hapla   ierr = DMPlexGetCones(dm,&cones);CHKERRQ(ierr);
3407a47d0d45SMatthew G. Knepley   for (c = 0; c < numCells; ++c) {
3408a47d0d45SMatthew G. Knepley     for (p = 0; p < numCorners; ++p) {
3409a47d0d45SMatthew G. Knepley       const PetscInt gv = cells[c*numCorners+p];
3410a47d0d45SMatthew G. Knepley       PetscInt       lv;
3411a47d0d45SMatthew G. Knepley 
34129079aca8SVaclav Hapla       /* Positions within verticesAdj form 0-based local vertex numbering;
34139079aca8SVaclav Hapla          we need to shift it by numCells to get correct DAG points (cells go first) */
3414a47d0d45SMatthew G. Knepley       ierr = PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv);CHKERRQ(ierr);
3415a47d0d45SMatthew G. Knepley       if (lv < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %D in local connectivity", gv);
3416961cfab0SVaclav Hapla       cones[c*numCorners+p] = lv+numCells;
3417a47d0d45SMatthew G. Knepley     }
3418a47d0d45SMatthew G. Knepley   }
34192464107aSksagiyam   /* Build point sf */
34202464107aSksagiyam   ierr = PetscLayoutCreate(PetscObjectComm((PetscObject)dm), &layout);CHKERRQ(ierr);
34212464107aSksagiyam   ierr = PetscLayoutSetSize(layout, NVertices);CHKERRQ(ierr);
34222464107aSksagiyam   ierr = PetscLayoutSetLocalSize(layout, numVertices);CHKERRQ(ierr);
34232464107aSksagiyam   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
34242464107aSksagiyam   ierr = PetscSFCreateByMatchingIndices(layout, numVerticesAdj, verticesAdj, NULL, numCells, numVerticesAdj, verticesAdj, NULL, numCells, vertexSF, &sfPoint);CHKERRQ(ierr);
34252464107aSksagiyam   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3426a47d0d45SMatthew G. Knepley   ierr = PetscFree(verticesAdj);CHKERRQ(ierr);
3427a47d0d45SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) sfPoint, "point SF");CHKERRQ(ierr);
34282464107aSksagiyam   if (dm->sf) {
34292464107aSksagiyam     const char *prefix;
34302464107aSksagiyam 
34312464107aSksagiyam     ierr = PetscObjectGetOptionsPrefix((PetscObject)dm->sf, &prefix);CHKERRQ(ierr);
34322464107aSksagiyam     ierr = PetscObjectSetOptionsPrefix((PetscObject)sfPoint, prefix);CHKERRQ(ierr);
34332464107aSksagiyam   }
34342464107aSksagiyam   ierr = DMSetPointSF(dm, sfPoint);CHKERRQ(ierr);
34352464107aSksagiyam   ierr = PetscSFDestroy(&sfPoint);CHKERRQ(ierr);
34362464107aSksagiyam   if (vertexSF) {ierr = PetscObjectSetName((PetscObject)(*vertexSF), "Vertex Ownership SF");CHKERRQ(ierr);}
3437a47d0d45SMatthew G. Knepley   /* Fill in the rest of the topology structure */
3438a47d0d45SMatthew G. Knepley   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
3439a47d0d45SMatthew G. Knepley   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
3440b09969d6SVaclav Hapla   ierr = PetscLogEventEnd(DMPLEX_BuildFromCellList,dm,0,0,0);CHKERRQ(ierr);
3441a47d0d45SMatthew G. Knepley   PetscFunctionReturn(0);
3442a47d0d45SMatthew G. Knepley }
3443a47d0d45SMatthew G. Knepley 
3444b09969d6SVaclav Hapla /*@C
3445b09969d6SVaclav Hapla   DMPlexBuildCoordinatesFromCellListParallel - Build DM coordinates from a list of coordinates for each owned vertex (common mesh generator output)
3446b09969d6SVaclav Hapla 
3447b09969d6SVaclav Hapla   Input Parameters:
3448b09969d6SVaclav Hapla + dm - The DM
3449b09969d6SVaclav Hapla . spaceDim - The spatial dimension used for coordinates
3450b09969d6SVaclav Hapla . sfVert - SF describing complete vertex ownership
3451b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
3452b09969d6SVaclav Hapla 
3453b09969d6SVaclav Hapla   Level: advanced
3454b09969d6SVaclav Hapla 
3455b09969d6SVaclav Hapla   Notes:
3456b09969d6SVaclav Hapla   Not currently supported in Fortran.
3457b09969d6SVaclav Hapla 
3458b09969d6SVaclav Hapla .seealso: DMPlexBuildCoordinatesFromCellList(), DMPlexCreateFromCellListParallelPetsc(), DMPlexBuildFromCellListParallel()
3459b09969d6SVaclav Hapla @*/
34601edcf0b2SVaclav Hapla PetscErrorCode DMPlexBuildCoordinatesFromCellListParallel(DM dm, PetscInt spaceDim, PetscSF sfVert, const PetscReal vertexCoords[])
3461a47d0d45SMatthew G. Knepley {
3462a47d0d45SMatthew G. Knepley   PetscSection   coordSection;
3463a47d0d45SMatthew G. Knepley   Vec            coordinates;
3464a47d0d45SMatthew G. Knepley   PetscScalar   *coords;
34651edcf0b2SVaclav Hapla   PetscInt       numVertices, numVerticesAdj, coordSize, v, vStart, vEnd;
3466a47d0d45SMatthew G. Knepley   PetscErrorCode ierr;
3467a47d0d45SMatthew G. Knepley 
3468a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
3469b09969d6SVaclav Hapla   ierr = PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0);CHKERRQ(ierr);
34701edcf0b2SVaclav Hapla   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
34711edcf0b2SVaclav Hapla   if (vStart < 0 || vEnd < 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
34729596c6baSMatthew G. Knepley   ierr = DMSetCoordinateDim(dm, spaceDim);CHKERRQ(ierr);
3473a47d0d45SMatthew G. Knepley   ierr = PetscSFGetGraph(sfVert, &numVertices, &numVerticesAdj, NULL, NULL);CHKERRQ(ierr);
34741edcf0b2SVaclav Hapla   if (vEnd - vStart != numVerticesAdj) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Supplied sfVert has wrong number of leaves = %D != %D = vEnd - vStart",numVerticesAdj,vEnd - vStart);
3475a47d0d45SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
3476a47d0d45SMatthew G. Knepley   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
3477a47d0d45SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(coordSection, 0, spaceDim);CHKERRQ(ierr);
34781edcf0b2SVaclav Hapla   ierr = PetscSectionSetChart(coordSection, vStart, vEnd);CHKERRQ(ierr);
34791edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
3480a47d0d45SMatthew G. Knepley     ierr = PetscSectionSetDof(coordSection, v, spaceDim);CHKERRQ(ierr);
3481a47d0d45SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(coordSection, v, 0, spaceDim);CHKERRQ(ierr);
3482a47d0d45SMatthew G. Knepley   }
3483a47d0d45SMatthew G. Knepley   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
3484a47d0d45SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
3485a47d0d45SMatthew G. Knepley   ierr = VecCreate(PetscObjectComm((PetscObject)dm), &coordinates);CHKERRQ(ierr);
3486a47d0d45SMatthew G. Knepley   ierr = VecSetBlockSize(coordinates, spaceDim);CHKERRQ(ierr);
3487a47d0d45SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
3488a47d0d45SMatthew G. Knepley   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
3489a47d0d45SMatthew G. Knepley   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
3490a47d0d45SMatthew G. Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
3491a47d0d45SMatthew G. Knepley   {
3492a47d0d45SMatthew G. Knepley     MPI_Datatype coordtype;
3493a47d0d45SMatthew G. Knepley 
3494a47d0d45SMatthew G. Knepley     /* Need a temp buffer for coords if we have complex/single */
3495ffc4695bSBarry Smith     ierr = MPI_Type_contiguous(spaceDim, MPIU_SCALAR, &coordtype);CHKERRMPI(ierr);
3496ffc4695bSBarry Smith     ierr = MPI_Type_commit(&coordtype);CHKERRMPI(ierr);
349721016a8bSBarry Smith #if defined(PETSC_USE_COMPLEX)
349821016a8bSBarry Smith     {
349921016a8bSBarry Smith     PetscScalar *svertexCoords;
350021016a8bSBarry Smith     PetscInt    i;
35013612f820SVaclav Hapla     ierr = PetscMalloc1(numVertices*spaceDim,&svertexCoords);CHKERRQ(ierr);
35023612f820SVaclav Hapla     for (i=0; i<numVertices*spaceDim; i++) svertexCoords[i] = vertexCoords[i];
3503ad227feaSJunchao Zhang     ierr = PetscSFBcastBegin(sfVert, coordtype, svertexCoords, coords,MPI_REPLACE);CHKERRQ(ierr);
3504ad227feaSJunchao Zhang     ierr = PetscSFBcastEnd(sfVert, coordtype, svertexCoords, coords,MPI_REPLACE);CHKERRQ(ierr);
350521016a8bSBarry Smith     ierr = PetscFree(svertexCoords);CHKERRQ(ierr);
350621016a8bSBarry Smith     }
350721016a8bSBarry Smith #else
3508ad227feaSJunchao Zhang     ierr = PetscSFBcastBegin(sfVert, coordtype, vertexCoords, coords,MPI_REPLACE);CHKERRQ(ierr);
3509ad227feaSJunchao Zhang     ierr = PetscSFBcastEnd(sfVert, coordtype, vertexCoords, coords,MPI_REPLACE);CHKERRQ(ierr);
351021016a8bSBarry Smith #endif
3511ffc4695bSBarry Smith     ierr = MPI_Type_free(&coordtype);CHKERRMPI(ierr);
3512a47d0d45SMatthew G. Knepley   }
3513a47d0d45SMatthew G. Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
3514a47d0d45SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
3515a47d0d45SMatthew G. Knepley   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
3516b09969d6SVaclav Hapla   ierr = PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0);CHKERRQ(ierr);
3517a47d0d45SMatthew G. Knepley   PetscFunctionReturn(0);
3518a47d0d45SMatthew G. Knepley }
3519a47d0d45SMatthew G. Knepley 
3520c3edce3dSSatish Balay /*@
3521b09969d6SVaclav Hapla   DMPlexCreateFromCellListParallelPetsc - Create distributed DMPLEX from a list of vertices for each cell (common mesh generator output)
3522a47d0d45SMatthew G. Knepley 
3523a47d0d45SMatthew G. Knepley   Input Parameters:
3524a47d0d45SMatthew G. Knepley + comm - The communicator
3525a47d0d45SMatthew G. Knepley . dim - The topological dimension of the mesh
3526a47d0d45SMatthew G. Knepley . numCells - The number of cells owned by this process
352725b6865aSVaclav Hapla . numVertices - The number of vertices owned by this process, or PETSC_DECIDE
352825b6865aSVaclav Hapla . NVertices - The global number of vertices, or PETSC_DECIDE
3529a47d0d45SMatthew G. Knepley . numCorners - The number of vertices for each cell
3530a47d0d45SMatthew G. Knepley . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
3531a47d0d45SMatthew G. Knepley . cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
3532a47d0d45SMatthew G. Knepley . spaceDim - The spatial dimension used for coordinates
3533a47d0d45SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
3534a47d0d45SMatthew G. Knepley 
3535a47d0d45SMatthew G. Knepley   Output Parameter:
353618d54ad4SMichael Lange + dm - The DM
3537b09969d6SVaclav Hapla - vertexSF - (Optional) SF describing complete vertex ownership
3538a47d0d45SMatthew G. Knepley 
3539b09969d6SVaclav Hapla   Notes:
3540b09969d6SVaclav Hapla   This function is just a convenient sequence of DMCreate(), DMSetType(), DMSetDimension(),
3541b09969d6SVaclav Hapla   DMPlexBuildFromCellListParallel(), DMPlexInterpolate(), DMPlexBuildCoordinatesFromCellListParallel()
3542a47d0d45SMatthew G. Knepley 
354325b6865aSVaclav Hapla   See DMPlexBuildFromCellListParallel() for an example and details about the topology-related parameters.
354425b6865aSVaclav Hapla   See DMPlexBuildCoordinatesFromCellListParallel() for details about the geometry-related parameters.
354525b6865aSVaclav Hapla 
3546b09969d6SVaclav Hapla   Level: intermediate
3547a47d0d45SMatthew G. Knepley 
3548b09969d6SVaclav Hapla .seealso: DMPlexCreateFromCellListPetsc(), DMPlexBuildFromCellListParallel(), DMPlexBuildCoordinatesFromCellListParallel(), DMPlexCreateFromDAG(), DMPlexCreate()
3549a47d0d45SMatthew G. Knepley @*/
355025b6865aSVaclav Hapla PetscErrorCode DMPlexCreateFromCellListParallelPetsc(MPI_Comm comm, PetscInt dim, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscInt numCorners, PetscBool interpolate, const PetscInt cells[], PetscInt spaceDim, const PetscReal vertexCoords[], PetscSF *vertexSF, DM *dm)
3551a47d0d45SMatthew G. Knepley {
3552a47d0d45SMatthew G. Knepley   PetscSF        sfVert;
3553a47d0d45SMatthew G. Knepley   PetscErrorCode ierr;
3554a47d0d45SMatthew G. Knepley 
3555a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
3556a47d0d45SMatthew G. Knepley   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
3557a47d0d45SMatthew G. Knepley   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
3558a47d0d45SMatthew G. Knepley   PetscValidLogicalCollectiveInt(*dm, dim, 2);
3559064a246eSJacob Faibussowitsch   PetscValidLogicalCollectiveInt(*dm, spaceDim, 9);
3560a47d0d45SMatthew G. Knepley   ierr = DMSetDimension(*dm, dim);CHKERRQ(ierr);
356125b6865aSVaclav Hapla   ierr = DMPlexBuildFromCellListParallel(*dm, numCells, numVertices, NVertices, numCorners, cells, &sfVert);CHKERRQ(ierr);
3562a47d0d45SMatthew G. Knepley   if (interpolate) {
35635fd9971aSMatthew G. Knepley     DM idm;
3564a47d0d45SMatthew G. Knepley 
3565a47d0d45SMatthew G. Knepley     ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr);
3566a47d0d45SMatthew G. Knepley     ierr = DMDestroy(dm);CHKERRQ(ierr);
3567a47d0d45SMatthew G. Knepley     *dm  = idm;
3568a47d0d45SMatthew G. Knepley   }
35691edcf0b2SVaclav Hapla   ierr = DMPlexBuildCoordinatesFromCellListParallel(*dm, spaceDim, sfVert, vertexCoords);CHKERRQ(ierr);
357018d54ad4SMichael Lange   if (vertexSF) *vertexSF = sfVert;
3571fba955ccSBarry Smith   else {ierr = PetscSFDestroy(&sfVert);CHKERRQ(ierr);}
3572a47d0d45SMatthew G. Knepley   PetscFunctionReturn(0);
3573a47d0d45SMatthew G. Knepley }
3574a47d0d45SMatthew G. Knepley 
3575a4a685f2SJacob Faibussowitsch 
3576a4a685f2SJacob Faibussowitsch /*@
3577a4a685f2SJacob Faibussowitsch   DMPlexCreateFromCellListParallel - Deprecated, use DMPlexCreateFromCellListParallelPetsc()
3578a4a685f2SJacob Faibussowitsch 
3579a4a685f2SJacob Faibussowitsch   Level: deprecated
3580a4a685f2SJacob Faibussowitsch 
3581a4a685f2SJacob Faibussowitsch .seealso: DMPlexCreateFromCellListParallelPetsc()
3582a4a685f2SJacob Faibussowitsch @*/
3583a4a685f2SJacob Faibussowitsch 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)
3584a4a685f2SJacob Faibussowitsch {
3585a4a685f2SJacob Faibussowitsch   PetscErrorCode ierr;
3586a4a685f2SJacob Faibussowitsch   PetscInt       i;
3587a4a685f2SJacob Faibussowitsch   PetscInt       *pintCells;
3588a4a685f2SJacob Faibussowitsch 
3589a4a685f2SJacob Faibussowitsch   PetscFunctionBegin;
3590a4a685f2SJacob Faibussowitsch   if (sizeof(int) > sizeof(PetscInt)) SETERRQ2(comm, PETSC_ERR_ARG_SIZ, "Size of int %zd greater than size of PetscInt %zd. Reconfigure PETSc --with-64-bit-indices=1", sizeof(int), sizeof(PetscInt));
3591a4a685f2SJacob Faibussowitsch   if (sizeof(int) == sizeof(PetscInt)) {
3592a4a685f2SJacob Faibussowitsch     pintCells = (PetscInt *) cells;
3593a4a685f2SJacob Faibussowitsch   } else {
3594a4a685f2SJacob Faibussowitsch     ierr = PetscMalloc1(numCells*numCorners, &pintCells);CHKERRQ(ierr);
3595a4a685f2SJacob Faibussowitsch     for (i = 0; i < numCells*numCorners; i++) {
3596a4a685f2SJacob Faibussowitsch       pintCells[i] = (PetscInt) cells[i];
3597a4a685f2SJacob Faibussowitsch     }
3598a4a685f2SJacob Faibussowitsch   }
359925b6865aSVaclav Hapla   ierr = DMPlexCreateFromCellListParallelPetsc(comm, dim, numCells, numVertices, PETSC_DECIDE, numCorners, interpolate, pintCells, spaceDim, vertexCoords, vertexSF, dm);CHKERRQ(ierr);
3600a4a685f2SJacob Faibussowitsch   if (sizeof(int) != sizeof(PetscInt)) {
3601a4a685f2SJacob Faibussowitsch     ierr = PetscFree(pintCells);CHKERRQ(ierr);
3602a4a685f2SJacob Faibussowitsch   }
3603a4a685f2SJacob Faibussowitsch   PetscFunctionReturn(0);
3604a4a685f2SJacob Faibussowitsch }
3605a4a685f2SJacob Faibussowitsch 
3606b09969d6SVaclav Hapla /*@C
3607b09969d6SVaclav Hapla   DMPlexBuildFromCellList - Build DMPLEX topology from a list of vertices for each cell (common mesh generator output)
36089298eaa6SMatthew G Knepley 
36099298eaa6SMatthew G Knepley   Input Parameters:
3610b09969d6SVaclav Hapla + dm - The DM
3611b09969d6SVaclav Hapla . numCells - The number of cells owned by this process
361225b6865aSVaclav Hapla . numVertices - The number of vertices owned by this process, or PETSC_DECIDE
36139298eaa6SMatthew G Knepley . numCorners - The number of vertices for each cell
36145e488331SVaclav Hapla - cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
36159298eaa6SMatthew G Knepley 
3616b09969d6SVaclav Hapla   Level: advanced
36179298eaa6SMatthew G Knepley 
3618b09969d6SVaclav Hapla   Notes:
3619b09969d6SVaclav Hapla   Two triangles sharing a face
36209298eaa6SMatthew G Knepley $
36219298eaa6SMatthew G Knepley $        2
36229298eaa6SMatthew G Knepley $      / | \
36239298eaa6SMatthew G Knepley $     /  |  \
36249298eaa6SMatthew G Knepley $    /   |   \
36259298eaa6SMatthew G Knepley $   0  0 | 1  3
36269298eaa6SMatthew G Knepley $    \   |   /
36279298eaa6SMatthew G Knepley $     \  |  /
36289298eaa6SMatthew G Knepley $      \ | /
36299298eaa6SMatthew G Knepley $        1
36309298eaa6SMatthew G Knepley would have input
36319298eaa6SMatthew G Knepley $  numCells = 2, numVertices = 4
36329298eaa6SMatthew G Knepley $  cells = [0 1 2  1 3 2]
36339298eaa6SMatthew G Knepley $
36349298eaa6SMatthew G Knepley which would result in the DMPlex
36359298eaa6SMatthew G Knepley $
36369298eaa6SMatthew G Knepley $        4
36379298eaa6SMatthew G Knepley $      / | \
36389298eaa6SMatthew G Knepley $     /  |  \
36399298eaa6SMatthew G Knepley $    /   |   \
36409298eaa6SMatthew G Knepley $   2  0 | 1  5
36419298eaa6SMatthew G Knepley $    \   |   /
36429298eaa6SMatthew G Knepley $     \  |  /
36439298eaa6SMatthew G Knepley $      \ | /
36449298eaa6SMatthew G Knepley $        3
36459298eaa6SMatthew G Knepley 
364625b6865aSVaclav Hapla   If numVertices is PETSC_DECIDE, it is computed by PETSc as the maximum vertex index in cells + 1.
364725b6865aSVaclav Hapla 
3648b09969d6SVaclav Hapla   Not currently supported in Fortran.
36499298eaa6SMatthew G Knepley 
3650b09969d6SVaclav Hapla .seealso: DMPlexBuildFromCellListParallel(), DMPlexBuildCoordinatesFromCellList(), DMPlexCreateFromCellListPetsc()
3651b09969d6SVaclav Hapla @*/
36525e488331SVaclav Hapla PetscErrorCode DMPlexBuildFromCellList(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const PetscInt cells[])
3653b09969d6SVaclav Hapla {
3654961cfab0SVaclav Hapla   PetscInt      *cones, c, p, dim;
3655b09969d6SVaclav Hapla   PetscErrorCode ierr;
3656b09969d6SVaclav Hapla 
3657b09969d6SVaclav Hapla   PetscFunctionBegin;
3658b09969d6SVaclav Hapla   ierr = PetscLogEventBegin(DMPLEX_BuildFromCellList,dm,0,0,0);CHKERRQ(ierr);
3659b09969d6SVaclav Hapla   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
366025b6865aSVaclav Hapla   /* Get/check global number of vertices */
366125b6865aSVaclav Hapla   {
366225b6865aSVaclav Hapla     PetscInt NVerticesInCells, i;
366325b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
366425b6865aSVaclav Hapla 
366525b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
366625b6865aSVaclav Hapla     NVerticesInCells = PETSC_MIN_INT;
366725b6865aSVaclav Hapla     for (i=0; i<len; i++) if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
366825b6865aSVaclav Hapla     ++NVerticesInCells;
366925b6865aSVaclav Hapla 
367025b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE) numVertices = NVerticesInCells;
367125b6865aSVaclav Hapla     else if (numVertices < NVerticesInCells) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Specified number of vertices %D must be greater than or equal to the number of vertices in cells %D",numVertices,NVerticesInCells);
367225b6865aSVaclav Hapla   }
3673b09969d6SVaclav Hapla   ierr = DMPlexSetChart(dm, 0, numCells+numVertices);CHKERRQ(ierr);
3674b09969d6SVaclav Hapla   for (c = 0; c < numCells; ++c) {
3675b09969d6SVaclav Hapla     ierr = DMPlexSetConeSize(dm, c, numCorners);CHKERRQ(ierr);
3676b09969d6SVaclav Hapla   }
3677b09969d6SVaclav Hapla   ierr = DMSetUp(dm);CHKERRQ(ierr);
3678961cfab0SVaclav Hapla   ierr = DMPlexGetCones(dm,&cones);CHKERRQ(ierr);
3679b09969d6SVaclav Hapla   for (c = 0; c < numCells; ++c) {
3680b09969d6SVaclav Hapla     for (p = 0; p < numCorners; ++p) {
3681961cfab0SVaclav Hapla       cones[c*numCorners+p] = cells[c*numCorners+p]+numCells;
3682b09969d6SVaclav Hapla     }
3683b09969d6SVaclav Hapla   }
3684b09969d6SVaclav Hapla   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
3685b09969d6SVaclav Hapla   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
3686b09969d6SVaclav Hapla   ierr = PetscLogEventEnd(DMPLEX_BuildFromCellList,dm,0,0,0);CHKERRQ(ierr);
3687b09969d6SVaclav Hapla   PetscFunctionReturn(0);
3688b09969d6SVaclav Hapla }
3689b09969d6SVaclav Hapla 
3690b09969d6SVaclav Hapla /*@C
3691b09969d6SVaclav Hapla   DMPlexBuildCoordinatesFromCellList - Build DM coordinates from a list of coordinates for each owned vertex (common mesh generator output)
3692b09969d6SVaclav Hapla 
3693b09969d6SVaclav Hapla   Input Parameters:
3694b09969d6SVaclav Hapla + dm - The DM
3695b09969d6SVaclav Hapla . spaceDim - The spatial dimension used for coordinates
3696b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
3697b09969d6SVaclav Hapla 
3698b09969d6SVaclav Hapla   Level: advanced
3699b09969d6SVaclav Hapla 
3700b09969d6SVaclav Hapla   Notes:
3701b09969d6SVaclav Hapla   Not currently supported in Fortran.
3702b09969d6SVaclav Hapla 
3703b09969d6SVaclav Hapla .seealso: DMPlexBuildCoordinatesFromCellListParallel(), DMPlexCreateFromCellListPetsc(), DMPlexBuildFromCellList()
3704b09969d6SVaclav Hapla @*/
37051edcf0b2SVaclav Hapla PetscErrorCode DMPlexBuildCoordinatesFromCellList(DM dm, PetscInt spaceDim, const PetscReal vertexCoords[])
3706b09969d6SVaclav Hapla {
3707b09969d6SVaclav Hapla   PetscSection   coordSection;
3708b09969d6SVaclav Hapla   Vec            coordinates;
3709b09969d6SVaclav Hapla   DM             cdm;
3710b09969d6SVaclav Hapla   PetscScalar   *coords;
37111edcf0b2SVaclav Hapla   PetscInt       v, vStart, vEnd, d;
3712b09969d6SVaclav Hapla   PetscErrorCode ierr;
3713b09969d6SVaclav Hapla 
3714b09969d6SVaclav Hapla   PetscFunctionBegin;
3715b09969d6SVaclav Hapla   ierr = PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0);CHKERRQ(ierr);
37161edcf0b2SVaclav Hapla   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
37171edcf0b2SVaclav Hapla   if (vStart < 0 || vEnd < 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
3718b09969d6SVaclav Hapla   ierr = DMSetCoordinateDim(dm, spaceDim);CHKERRQ(ierr);
3719b09969d6SVaclav Hapla   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
3720b09969d6SVaclav Hapla   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
3721b09969d6SVaclav Hapla   ierr = PetscSectionSetFieldComponents(coordSection, 0, spaceDim);CHKERRQ(ierr);
37221edcf0b2SVaclav Hapla   ierr = PetscSectionSetChart(coordSection, vStart, vEnd);CHKERRQ(ierr);
37231edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
3724b09969d6SVaclav Hapla     ierr = PetscSectionSetDof(coordSection, v, spaceDim);CHKERRQ(ierr);
3725b09969d6SVaclav Hapla     ierr = PetscSectionSetFieldDof(coordSection, v, 0, spaceDim);CHKERRQ(ierr);
3726b09969d6SVaclav Hapla   }
3727b09969d6SVaclav Hapla   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
3728b09969d6SVaclav Hapla 
3729b09969d6SVaclav Hapla   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
3730b09969d6SVaclav Hapla   ierr = DMCreateLocalVector(cdm, &coordinates);CHKERRQ(ierr);
3731b09969d6SVaclav Hapla   ierr = VecSetBlockSize(coordinates, spaceDim);CHKERRQ(ierr);
3732b09969d6SVaclav Hapla   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
373399946890SBarry Smith   ierr = VecGetArrayWrite(coordinates, &coords);CHKERRQ(ierr);
37341edcf0b2SVaclav Hapla   for (v = 0; v < vEnd-vStart; ++v) {
3735b09969d6SVaclav Hapla     for (d = 0; d < spaceDim; ++d) {
3736b09969d6SVaclav Hapla       coords[v*spaceDim+d] = vertexCoords[v*spaceDim+d];
3737b09969d6SVaclav Hapla     }
3738b09969d6SVaclav Hapla   }
373999946890SBarry Smith   ierr = VecRestoreArrayWrite(coordinates, &coords);CHKERRQ(ierr);
3740b09969d6SVaclav Hapla   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
3741b09969d6SVaclav Hapla   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
3742b09969d6SVaclav Hapla   ierr = PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0);CHKERRQ(ierr);
3743b09969d6SVaclav Hapla   PetscFunctionReturn(0);
3744b09969d6SVaclav Hapla }
3745b09969d6SVaclav Hapla 
3746b09969d6SVaclav Hapla /*@
3747b09969d6SVaclav Hapla   DMPlexCreateFromCellListPetsc - Create DMPLEX from a list of vertices for each cell (common mesh generator output)
3748b09969d6SVaclav Hapla 
3749b09969d6SVaclav Hapla   Input Parameters:
3750b09969d6SVaclav Hapla + comm - The communicator
3751b09969d6SVaclav Hapla . dim - The topological dimension of the mesh
3752b09969d6SVaclav Hapla . numCells - The number of cells
375325b6865aSVaclav Hapla . numVertices - The number of vertices owned by this process, or PETSC_DECIDE
3754b09969d6SVaclav Hapla . numCorners - The number of vertices for each cell
3755b09969d6SVaclav Hapla . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
3756b09969d6SVaclav Hapla . cells - An array of numCells*numCorners numbers, the vertices for each cell
3757b09969d6SVaclav Hapla . spaceDim - The spatial dimension used for coordinates
3758b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
3759b09969d6SVaclav Hapla 
3760b09969d6SVaclav Hapla   Output Parameter:
3761b09969d6SVaclav Hapla . dm - The DM
3762b09969d6SVaclav Hapla 
3763b09969d6SVaclav Hapla   Notes:
3764b09969d6SVaclav Hapla   This function is just a convenient sequence of DMCreate(), DMSetType(), DMSetDimension(), DMPlexBuildFromCellList(),
3765b09969d6SVaclav Hapla   DMPlexInterpolate(), DMPlexBuildCoordinatesFromCellList()
3766b09969d6SVaclav Hapla 
376725b6865aSVaclav Hapla   See DMPlexBuildFromCellList() for an example and details about the topology-related parameters.
376825b6865aSVaclav Hapla   See DMPlexBuildCoordinatesFromCellList() for details about the geometry-related parameters.
376925b6865aSVaclav Hapla 
3770b09969d6SVaclav Hapla   Level: intermediate
3771b09969d6SVaclav Hapla 
3772b09969d6SVaclav Hapla .seealso: DMPlexCreateFromCellListParallelPetsc(), DMPlexBuildFromCellList(), DMPlexBuildCoordinatesFromCellList(), DMPlexCreateFromDAG(), DMPlexCreate()
37739298eaa6SMatthew G Knepley @*/
3774a4a685f2SJacob Faibussowitsch PetscErrorCode DMPlexCreateFromCellListPetsc(MPI_Comm comm, PetscInt dim, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, PetscBool interpolate, const PetscInt cells[], PetscInt spaceDim, const PetscReal vertexCoords[], DM *dm)
37759298eaa6SMatthew G Knepley {
37769298eaa6SMatthew G Knepley   PetscErrorCode ierr;
37779298eaa6SMatthew G Knepley 
37789298eaa6SMatthew G Knepley   PetscFunctionBegin;
37795fd8819aSMatthew Knepley   if (!dim) SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "This is not appropriate for 0-dimensional meshes. Consider either creating the DM using DMPlexCreateFromDAG(), by hand, or using DMSwarm.");
37809298eaa6SMatthew G Knepley   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
37819298eaa6SMatthew G Knepley   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
3782c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(*dm, dim);CHKERRQ(ierr);
37835e488331SVaclav Hapla   ierr = DMPlexBuildFromCellList(*dm, numCells, numVertices, numCorners, cells);CHKERRQ(ierr);
37849298eaa6SMatthew G Knepley   if (interpolate) {
37855fd9971aSMatthew G. Knepley     DM idm;
37869298eaa6SMatthew G Knepley 
37879298eaa6SMatthew G Knepley     ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr);
37889298eaa6SMatthew G Knepley     ierr = DMDestroy(dm);CHKERRQ(ierr);
37899298eaa6SMatthew G Knepley     *dm  = idm;
37909298eaa6SMatthew G Knepley   }
37911edcf0b2SVaclav Hapla   ierr = DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, vertexCoords);CHKERRQ(ierr);
37929298eaa6SMatthew G Knepley   PetscFunctionReturn(0);
37939298eaa6SMatthew G Knepley }
37949298eaa6SMatthew G Knepley 
3795939f6067SMatthew G. Knepley /*@
3796a4a685f2SJacob Faibussowitsch   DMPlexCreateFromCellList - Deprecated, use DMPlexCreateFromCellListPetsc()
3797a4a685f2SJacob Faibussowitsch 
3798a4a685f2SJacob Faibussowitsch   Level: deprecated
3799a4a685f2SJacob Faibussowitsch 
3800a4a685f2SJacob Faibussowitsch .seealso: DMPlexCreateFromCellListPetsc()
3801a4a685f2SJacob Faibussowitsch @*/
3802a4a685f2SJacob Faibussowitsch 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)
3803a4a685f2SJacob Faibussowitsch {
3804a4a685f2SJacob Faibussowitsch   PetscErrorCode ierr;
3805a4a685f2SJacob Faibussowitsch   PetscInt       i;
3806a4a685f2SJacob Faibussowitsch   PetscInt       *pintCells;
3807a4a685f2SJacob Faibussowitsch   PetscReal      *prealVC;
3808a4a685f2SJacob Faibussowitsch 
3809a4a685f2SJacob Faibussowitsch   PetscFunctionBegin;
3810a4a685f2SJacob Faibussowitsch   if (sizeof(int) > sizeof(PetscInt)) SETERRQ2(comm, PETSC_ERR_ARG_SIZ, "Size of int %zd greater than size of PetscInt %zd. Reconfigure PETSc --with-64-bit-indices=1", sizeof(int), sizeof(PetscInt));
3811a4a685f2SJacob Faibussowitsch   if (sizeof(int) == sizeof(PetscInt)) {
3812a4a685f2SJacob Faibussowitsch     pintCells = (PetscInt *) cells;
3813a4a685f2SJacob Faibussowitsch   } else {
3814a4a685f2SJacob Faibussowitsch     ierr = PetscMalloc1(numCells*numCorners, &pintCells);CHKERRQ(ierr);
3815a4a685f2SJacob Faibussowitsch     for (i = 0; i < numCells*numCorners; i++) {
3816a4a685f2SJacob Faibussowitsch       pintCells[i] = (PetscInt) cells[i];
3817a4a685f2SJacob Faibussowitsch     }
3818a4a685f2SJacob Faibussowitsch   }
3819a4a685f2SJacob Faibussowitsch   if (sizeof(double) > sizeof(PetscReal)) SETERRQ2(comm, PETSC_ERR_ARG_SIZ, "Size of double %zd greater than size of PetscReal %zd. Reconfigure PETSc --with-precision=<higher precision>.", sizeof(double), sizeof(PetscReal));
3820a4a685f2SJacob Faibussowitsch   if (sizeof(double) == sizeof(PetscReal)) {
3821a4a685f2SJacob Faibussowitsch     prealVC = (PetscReal *) vertexCoords;
3822a4a685f2SJacob Faibussowitsch   } else {
3823a4a685f2SJacob Faibussowitsch     ierr = PetscMalloc1(numVertices*spaceDim, &prealVC);CHKERRQ(ierr);
3824a4a685f2SJacob Faibussowitsch     for (i = 0; i < numVertices*spaceDim; i++) {
3825a4a685f2SJacob Faibussowitsch       prealVC[i] = (PetscReal) vertexCoords[i];
3826a4a685f2SJacob Faibussowitsch     }
3827a4a685f2SJacob Faibussowitsch   }
3828a4a685f2SJacob Faibussowitsch   ierr = DMPlexCreateFromCellListPetsc(comm, dim, numCells, numVertices, numCorners, interpolate, pintCells, spaceDim, prealVC, dm);CHKERRQ(ierr);
3829a4a685f2SJacob Faibussowitsch   if (sizeof(int) != sizeof(PetscInt)) {
3830a4a685f2SJacob Faibussowitsch     ierr = PetscFree(pintCells);CHKERRQ(ierr);
3831a4a685f2SJacob Faibussowitsch   }
3832a4a685f2SJacob Faibussowitsch   if (sizeof(double) != sizeof(PetscReal)) {
3833a4a685f2SJacob Faibussowitsch     ierr = PetscFree(prealVC);CHKERRQ(ierr);
3834a4a685f2SJacob Faibussowitsch   }
3835a4a685f2SJacob Faibussowitsch   PetscFunctionReturn(0);
3836a4a685f2SJacob Faibussowitsch }
3837a4a685f2SJacob Faibussowitsch 
3838a4a685f2SJacob Faibussowitsch /*@
3839939f6067SMatthew 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
3840939f6067SMatthew G. Knepley 
3841939f6067SMatthew G. Knepley   Input Parameters:
3842c73cfb54SMatthew G. Knepley + dm - The empty DM object, usually from DMCreate() and DMSetDimension()
3843939f6067SMatthew G. Knepley . depth - The depth of the DAG
3844367003a6SStefano Zampini . numPoints - Array of size depth + 1 containing the number of points at each depth
3845939f6067SMatthew G. Knepley . coneSize - The cone size of each point
3846939f6067SMatthew G. Knepley . cones - The concatenation of the cone points for each point, the cone list must be oriented correctly for each point
3847939f6067SMatthew G. Knepley . coneOrientations - The orientation of each cone point
3848367003a6SStefano Zampini - vertexCoords - An array of numPoints[0]*spacedim numbers representing the coordinates of each vertex, with spacedim the value set via DMSetCoordinateDim()
3849939f6067SMatthew G. Knepley 
3850939f6067SMatthew G. Knepley   Output Parameter:
3851939f6067SMatthew G. Knepley . dm - The DM
3852939f6067SMatthew G. Knepley 
3853939f6067SMatthew G. Knepley   Note: Two triangles sharing a face would have input
3854939f6067SMatthew G. Knepley $  depth = 1, numPoints = [4 2], coneSize = [3 3 0 0 0 0]
3855939f6067SMatthew G. Knepley $  cones = [2 3 4  3 5 4], coneOrientations = [0 0 0  0 0 0]
3856939f6067SMatthew G. Knepley $ vertexCoords = [-1.0 0.0  0.0 -1.0  0.0 1.0  1.0 0.0]
3857939f6067SMatthew G. Knepley $
3858939f6067SMatthew G. Knepley which would result in the DMPlex
3859939f6067SMatthew G. Knepley $
3860939f6067SMatthew G. Knepley $        4
3861939f6067SMatthew G. Knepley $      / | \
3862939f6067SMatthew G. Knepley $     /  |  \
3863939f6067SMatthew G. Knepley $    /   |   \
3864939f6067SMatthew G. Knepley $   2  0 | 1  5
3865939f6067SMatthew G. Knepley $    \   |   /
3866939f6067SMatthew G. Knepley $     \  |  /
3867939f6067SMatthew G. Knepley $      \ | /
3868939f6067SMatthew G. Knepley $        3
3869939f6067SMatthew G. Knepley $
3870a4a685f2SJacob Faibussowitsch $ Notice that all points are numbered consecutively, unlike DMPlexCreateFromCellListPetsc()
3871939f6067SMatthew G. Knepley 
3872939f6067SMatthew G. Knepley   Level: advanced
3873939f6067SMatthew G. Knepley 
3874a4a685f2SJacob Faibussowitsch .seealso: DMPlexCreateFromCellListPetsc(), DMPlexCreate()
3875939f6067SMatthew G. Knepley @*/
38769298eaa6SMatthew G Knepley PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[])
38779298eaa6SMatthew G Knepley {
38789298eaa6SMatthew G Knepley   Vec            coordinates;
38799298eaa6SMatthew G Knepley   PetscSection   coordSection;
38809298eaa6SMatthew G Knepley   PetscScalar    *coords;
3881811e8653SToby Isaac   PetscInt       coordSize, firstVertex = -1, pStart = 0, pEnd = 0, p, v, dim, dimEmbed, d, off;
38829298eaa6SMatthew G Knepley   PetscErrorCode ierr;
38839298eaa6SMatthew G Knepley 
38849298eaa6SMatthew G Knepley   PetscFunctionBegin;
3885c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3886811e8653SToby Isaac   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
388789c010cfSBarry Smith   if (dimEmbed < dim) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Embedding dimension %D cannot be less than intrinsic dimension %d",dimEmbed,dim);
38889298eaa6SMatthew G Knepley   for (d = 0; d <= depth; ++d) pEnd += numPoints[d];
38899298eaa6SMatthew G Knepley   ierr = DMPlexSetChart(dm, pStart, pEnd);CHKERRQ(ierr);
38909298eaa6SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
38919298eaa6SMatthew G Knepley     ierr = DMPlexSetConeSize(dm, p, coneSize[p-pStart]);CHKERRQ(ierr);
389297e052ccSToby Isaac     if (firstVertex < 0 && !coneSize[p - pStart]) {
389397e052ccSToby Isaac       firstVertex = p - pStart;
38949298eaa6SMatthew G Knepley     }
389597e052ccSToby Isaac   }
389689c010cfSBarry Smith   if (firstVertex < 0 && numPoints[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected %D vertices but could not find any", numPoints[0]);
38979298eaa6SMatthew G Knepley   ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
38989298eaa6SMatthew G Knepley   for (p = pStart, off = 0; p < pEnd; off += coneSize[p-pStart], ++p) {
38999298eaa6SMatthew G Knepley     ierr = DMPlexSetCone(dm, p, &cones[off]);CHKERRQ(ierr);
39009298eaa6SMatthew G Knepley     ierr = DMPlexSetConeOrientation(dm, p, &coneOrientations[off]);CHKERRQ(ierr);
39019298eaa6SMatthew G Knepley   }
39029298eaa6SMatthew G Knepley   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
39039298eaa6SMatthew G Knepley   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
39049298eaa6SMatthew G Knepley   /* Build coordinates */
3905c2166f76SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
39069298eaa6SMatthew G Knepley   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
3907811e8653SToby Isaac   ierr = PetscSectionSetFieldComponents(coordSection, 0, dimEmbed);CHKERRQ(ierr);
39089298eaa6SMatthew G Knepley   ierr = PetscSectionSetChart(coordSection, firstVertex, firstVertex+numPoints[0]);CHKERRQ(ierr);
39099298eaa6SMatthew G Knepley   for (v = firstVertex; v < firstVertex+numPoints[0]; ++v) {
3910811e8653SToby Isaac     ierr = PetscSectionSetDof(coordSection, v, dimEmbed);CHKERRQ(ierr);
3911811e8653SToby Isaac     ierr = PetscSectionSetFieldDof(coordSection, v, 0, dimEmbed);CHKERRQ(ierr);
39129298eaa6SMatthew G Knepley   }
39139298eaa6SMatthew G Knepley   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
39149298eaa6SMatthew G Knepley   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
39158b9ced59SLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
39166f8cbbeeSMatthew G Knepley   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
39179298eaa6SMatthew G Knepley   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
39188b9ced59SLisandro Dalcin   ierr = VecSetBlockSize(coordinates, dimEmbed);CHKERRQ(ierr);
39192eb5907fSJed Brown   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
3920*9318fe57SMatthew G. Knepley   if (vertexCoords) {
39219298eaa6SMatthew G Knepley     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
39229298eaa6SMatthew G Knepley     for (v = 0; v < numPoints[0]; ++v) {
39239298eaa6SMatthew G Knepley       PetscInt off;
39249298eaa6SMatthew G Knepley 
39259298eaa6SMatthew G Knepley       ierr = PetscSectionGetOffset(coordSection, v+firstVertex, &off);CHKERRQ(ierr);
3926811e8653SToby Isaac       for (d = 0; d < dimEmbed; ++d) {
3927811e8653SToby Isaac         coords[off+d] = vertexCoords[v*dimEmbed+d];
39289298eaa6SMatthew G Knepley       }
39299298eaa6SMatthew G Knepley     }
3930*9318fe57SMatthew G. Knepley   }
39319298eaa6SMatthew G Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
39329298eaa6SMatthew G Knepley   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
39339298eaa6SMatthew G Knepley   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
39349298eaa6SMatthew G Knepley   PetscFunctionReturn(0);
39359298eaa6SMatthew G Knepley }
39368415267dSToby Isaac 
3937ca522641SMatthew G. Knepley /*@C
39388ca92349SMatthew G. Knepley   DMPlexCreateCellVertexFromFile - Create a DMPlex mesh from a simple cell-vertex file.
39398ca92349SMatthew G. Knepley 
39408ca92349SMatthew G. Knepley + comm        - The MPI communicator
39418ca92349SMatthew G. Knepley . filename    - Name of the .dat file
39428ca92349SMatthew G. Knepley - interpolate - Create faces and edges in the mesh
39438ca92349SMatthew G. Knepley 
39448ca92349SMatthew G. Knepley   Output Parameter:
39458ca92349SMatthew G. Knepley . dm  - The DM object representing the mesh
39468ca92349SMatthew G. Knepley 
39478ca92349SMatthew G. Knepley   Note: The format is the simplest possible:
39488ca92349SMatthew G. Knepley $ Ne
39498ca92349SMatthew G. Knepley $ v0 v1 ... vk
39508ca92349SMatthew G. Knepley $ Nv
39518ca92349SMatthew G. Knepley $ x y z marker
39528ca92349SMatthew G. Knepley 
39538ca92349SMatthew G. Knepley   Level: beginner
39548ca92349SMatthew G. Knepley 
39558ca92349SMatthew G. Knepley .seealso: DMPlexCreateFromFile(), DMPlexCreateMedFromFile(), DMPlexCreateGmsh(), DMPlexCreate()
39568ca92349SMatthew G. Knepley @*/
39578ca92349SMatthew G. Knepley PetscErrorCode DMPlexCreateCellVertexFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
39588ca92349SMatthew G. Knepley {
39598ca92349SMatthew G. Knepley   DMLabel         marker;
39608ca92349SMatthew G. Knepley   PetscViewer     viewer;
39618ca92349SMatthew G. Knepley   Vec             coordinates;
39628ca92349SMatthew G. Knepley   PetscSection    coordSection;
39638ca92349SMatthew G. Knepley   PetscScalar    *coords;
39648ca92349SMatthew G. Knepley   char            line[PETSC_MAX_PATH_LEN];
39658ca92349SMatthew G. Knepley   PetscInt        dim = 3, cdim = 3, coordSize, v, c, d;
39668ca92349SMatthew G. Knepley   PetscMPIInt     rank;
39678ca92349SMatthew G. Knepley   int             snum, Nv, Nc;
39688ca92349SMatthew G. Knepley   PetscErrorCode  ierr;
39698ca92349SMatthew G. Knepley 
39708ca92349SMatthew G. Knepley   PetscFunctionBegin;
3971ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
39728ca92349SMatthew G. Knepley   ierr = PetscViewerCreate(comm, &viewer);CHKERRQ(ierr);
39738ca92349SMatthew G. Knepley   ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
39748ca92349SMatthew G. Knepley   ierr = PetscViewerFileSetMode(viewer, FILE_MODE_READ);CHKERRQ(ierr);
39758ca92349SMatthew G. Knepley   ierr = PetscViewerFileSetName(viewer, filename);CHKERRQ(ierr);
39768ca92349SMatthew G. Knepley   if (!rank) {
39778ca92349SMatthew G. Knepley     ierr = PetscViewerRead(viewer, line, 2, NULL, PETSC_STRING);CHKERRQ(ierr);
39788ca92349SMatthew G. Knepley     snum = sscanf(line, "%d %d", &Nc, &Nv);
39798ca92349SMatthew G. Knepley     if (snum != 2) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
398025ce1634SJed Brown   } else {
398125ce1634SJed Brown     Nc = Nv = 0;
39828ca92349SMatthew G. Knepley   }
39838ca92349SMatthew G. Knepley   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
39848ca92349SMatthew G. Knepley   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
39858ca92349SMatthew G. Knepley   ierr = DMPlexSetChart(*dm, 0, Nc+Nv);CHKERRQ(ierr);
39868ca92349SMatthew G. Knepley   ierr = DMSetDimension(*dm, dim);CHKERRQ(ierr);
39878ca92349SMatthew G. Knepley   ierr = DMSetCoordinateDim(*dm, cdim);CHKERRQ(ierr);
39888ca92349SMatthew G. Knepley   /* Read topology */
39898ca92349SMatthew G. Knepley   if (!rank) {
39908ca92349SMatthew G. Knepley     PetscInt cone[8], corners = 8;
39918ca92349SMatthew G. Knepley     int      vbuf[8], v;
39928ca92349SMatthew G. Knepley 
39938ca92349SMatthew G. Knepley     for (c = 0; c < Nc; ++c) {ierr = DMPlexSetConeSize(*dm, c, corners);CHKERRQ(ierr);}
39948ca92349SMatthew G. Knepley     ierr = DMSetUp(*dm);CHKERRQ(ierr);
39958ca92349SMatthew G. Knepley     for (c = 0; c < Nc; ++c) {
39968ca92349SMatthew G. Knepley       ierr = PetscViewerRead(viewer, line, corners, NULL, PETSC_STRING);CHKERRQ(ierr);
39978ca92349SMatthew G. Knepley       snum = sscanf(line, "%d %d %d %d %d %d %d %d", &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5], &vbuf[6], &vbuf[7]);
39988ca92349SMatthew G. Knepley       if (snum != corners) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
39998ca92349SMatthew G. Knepley       for (v = 0; v < corners; ++v) cone[v] = vbuf[v] + Nc;
40008ca92349SMatthew G. Knepley       /* Hexahedra are inverted */
40018ca92349SMatthew G. Knepley       {
40028ca92349SMatthew G. Knepley         PetscInt tmp = cone[1];
40038ca92349SMatthew G. Knepley         cone[1] = cone[3];
40048ca92349SMatthew G. Knepley         cone[3] = tmp;
40058ca92349SMatthew G. Knepley       }
40068ca92349SMatthew G. Knepley       ierr = DMPlexSetCone(*dm, c, cone);CHKERRQ(ierr);
40078ca92349SMatthew G. Knepley     }
40088ca92349SMatthew G. Knepley   }
40098ca92349SMatthew G. Knepley   ierr = DMPlexSymmetrize(*dm);CHKERRQ(ierr);
40108ca92349SMatthew G. Knepley   ierr = DMPlexStratify(*dm);CHKERRQ(ierr);
40118ca92349SMatthew G. Knepley   /* Read coordinates */
40128ca92349SMatthew G. Knepley   ierr = DMGetCoordinateSection(*dm, &coordSection);CHKERRQ(ierr);
40138ca92349SMatthew G. Knepley   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
40148ca92349SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(coordSection, 0, cdim);CHKERRQ(ierr);
40158ca92349SMatthew G. Knepley   ierr = PetscSectionSetChart(coordSection, Nc, Nc + Nv);CHKERRQ(ierr);
40168ca92349SMatthew G. Knepley   for (v = Nc; v < Nc+Nv; ++v) {
40178ca92349SMatthew G. Knepley     ierr = PetscSectionSetDof(coordSection, v, cdim);CHKERRQ(ierr);
40188ca92349SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(coordSection, v, 0, cdim);CHKERRQ(ierr);
40198ca92349SMatthew G. Knepley   }
40208ca92349SMatthew G. Knepley   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
40218ca92349SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
40228ca92349SMatthew G. Knepley   ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
40238ca92349SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
40248ca92349SMatthew G. Knepley   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
40258ca92349SMatthew G. Knepley   ierr = VecSetBlockSize(coordinates, cdim);CHKERRQ(ierr);
40268ca92349SMatthew G. Knepley   ierr = VecSetType(coordinates, VECSTANDARD);CHKERRQ(ierr);
40278ca92349SMatthew G. Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
40288ca92349SMatthew G. Knepley   if (!rank) {
40298ca92349SMatthew G. Knepley     double x[3];
40308ca92349SMatthew G. Knepley     int    val;
40318ca92349SMatthew G. Knepley 
40328ca92349SMatthew G. Knepley     ierr = DMCreateLabel(*dm, "marker");CHKERRQ(ierr);
40338ca92349SMatthew G. Knepley     ierr = DMGetLabel(*dm, "marker", &marker);CHKERRQ(ierr);
40348ca92349SMatthew G. Knepley     for (v = 0; v < Nv; ++v) {
40358ca92349SMatthew G. Knepley       ierr = PetscViewerRead(viewer, line, 4, NULL, PETSC_STRING);CHKERRQ(ierr);
40368ca92349SMatthew G. Knepley       snum = sscanf(line, "%lg %lg %lg %d", &x[0], &x[1], &x[2], &val);
40378ca92349SMatthew G. Knepley       if (snum != 4) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
40388ca92349SMatthew G. Knepley       for (d = 0; d < cdim; ++d) coords[v*cdim+d] = x[d];
40398ca92349SMatthew G. Knepley       if (val) {ierr = DMLabelSetValue(marker, v+Nc, val);CHKERRQ(ierr);}
40408ca92349SMatthew G. Knepley     }
40418ca92349SMatthew G. Knepley   }
40428ca92349SMatthew G. Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
40438ca92349SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(*dm, coordinates);CHKERRQ(ierr);
40448ca92349SMatthew G. Knepley   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
40458ca92349SMatthew G. Knepley   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
40468ca92349SMatthew G. Knepley   if (interpolate) {
40478ca92349SMatthew G. Knepley     DM      idm;
40488ca92349SMatthew G. Knepley     DMLabel bdlabel;
40498ca92349SMatthew G. Knepley 
40508ca92349SMatthew G. Knepley     ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr);
40518ca92349SMatthew G. Knepley     ierr = DMDestroy(dm);CHKERRQ(ierr);
40528ca92349SMatthew G. Knepley     *dm  = idm;
40538ca92349SMatthew G. Knepley 
40548ca92349SMatthew G. Knepley     ierr = DMGetLabel(*dm, "marker", &bdlabel);CHKERRQ(ierr);
40558ca92349SMatthew G. Knepley     ierr = DMPlexMarkBoundaryFaces(*dm, PETSC_DETERMINE, bdlabel);CHKERRQ(ierr);
40568ca92349SMatthew G. Knepley     ierr = DMPlexLabelComplete(*dm, bdlabel);CHKERRQ(ierr);
40578ca92349SMatthew G. Knepley   }
40588ca92349SMatthew G. Knepley   PetscFunctionReturn(0);
40598ca92349SMatthew G. Knepley }
40608ca92349SMatthew G. Knepley 
40618ca92349SMatthew G. Knepley /*@C
4062ca522641SMatthew G. Knepley   DMPlexCreateFromFile - This takes a filename and produces a DM
4063ca522641SMatthew G. Knepley 
4064ca522641SMatthew G. Knepley   Input Parameters:
4065ca522641SMatthew G. Knepley + comm - The communicator
4066ca522641SMatthew G. Knepley . filename - A file name
4067ca522641SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
4068ca522641SMatthew G. Knepley 
4069ca522641SMatthew G. Knepley   Output Parameter:
4070ca522641SMatthew G. Knepley . dm - The DM
4071ca522641SMatthew G. Knepley 
407202ef0d99SVaclav Hapla   Options Database Keys:
407302ef0d99SVaclav Hapla . -dm_plex_create_from_hdf5_xdmf - use the PETSC_VIEWER_HDF5_XDMF format for reading HDF5
407402ef0d99SVaclav Hapla 
4075bca97951SVaclav Hapla   Use -dm_plex_create_ prefix to pass options to the internal PetscViewer, e.g.
4076bca97951SVaclav Hapla $ -dm_plex_create_viewer_hdf5_collective
4077bca97951SVaclav Hapla 
4078ca522641SMatthew G. Knepley   Level: beginner
4079ca522641SMatthew G. Knepley 
4080a4a685f2SJacob Faibussowitsch .seealso: DMPlexCreateFromDAG(), DMPlexCreateFromCellListPetsc(), DMPlexCreate()
4081ca522641SMatthew G. Knepley @*/
4082ca522641SMatthew G. Knepley PetscErrorCode DMPlexCreateFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
4083ca522641SMatthew G. Knepley {
4084ca522641SMatthew G. Knepley   const char    *extGmsh    = ".msh";
4085de78e4feSLisandro Dalcin   const char    *extGmsh2   = ".msh2";
4086de78e4feSLisandro Dalcin   const char    *extGmsh4   = ".msh4";
4087ca522641SMatthew G. Knepley   const char    *extCGNS    = ".cgns";
4088ca522641SMatthew G. Knepley   const char    *extExodus  = ".exo";
408990c68965SMatthew G. Knepley   const char    *extGenesis = ".gen";
40902f0bd6dcSMichael Lange   const char    *extFluent  = ".cas";
4091cc2f8f65SMatthew G. Knepley   const char    *extHDF5    = ".h5";
4092707dd687SMichael Lange   const char    *extMed     = ".med";
4093f2801cd6SMatthew G. Knepley   const char    *extPLY     = ".ply";
40947bee2925SMatthew Knepley   const char    *extEGADS   = ".egadslite";
40958ca92349SMatthew G. Knepley   const char    *extCV      = ".dat";
4096ca522641SMatthew G. Knepley   size_t         len;
40977bee2925SMatthew Knepley   PetscBool      isGmsh, isGmsh2, isGmsh4, isCGNS, isExodus, isGenesis, isFluent, isHDF5, isMed, isPLY, isEGADS, isCV;
4098ca522641SMatthew G. Knepley   PetscMPIInt    rank;
4099ca522641SMatthew G. Knepley   PetscErrorCode ierr;
4100ca522641SMatthew G. Knepley 
4101ca522641SMatthew G. Knepley   PetscFunctionBegin;
41025d80c0bfSVaclav Hapla   PetscValidCharPointer(filename, 2);
4103ca522641SMatthew G. Knepley   PetscValidPointer(dm, 4);
4104f1f45c63SVaclav Hapla   ierr = DMInitializePackage();CHKERRQ(ierr);
4105f1f45c63SVaclav Hapla   ierr = PetscLogEventBegin(DMPLEX_CreateFromFile,0,0,0,0);CHKERRQ(ierr);
4106ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
4107ca522641SMatthew G. Knepley   ierr = PetscStrlen(filename, &len);CHKERRQ(ierr);
4108ca522641SMatthew G. Knepley   if (!len) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Filename must be a valid path");
4109ca522641SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)], extGmsh,    4, &isGmsh);CHKERRQ(ierr);
4110de78e4feSLisandro Dalcin   ierr = PetscStrncmp(&filename[PetscMax(0,len-5)], extGmsh2,   5, &isGmsh2);CHKERRQ(ierr);
4111de78e4feSLisandro Dalcin   ierr = PetscStrncmp(&filename[PetscMax(0,len-5)], extGmsh4,   5, &isGmsh4);CHKERRQ(ierr);
4112ca522641SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-5)], extCGNS,    5, &isCGNS);CHKERRQ(ierr);
4113ca522641SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)], extExodus,  4, &isExodus);CHKERRQ(ierr);
411490c68965SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)], extGenesis, 4, &isGenesis);CHKERRQ(ierr);
41152f0bd6dcSMichael Lange   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)], extFluent,  4, &isFluent);CHKERRQ(ierr);
4116cc2f8f65SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-3)], extHDF5,    3, &isHDF5);CHKERRQ(ierr);
4117707dd687SMichael Lange   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)], extMed,     4, &isMed);CHKERRQ(ierr);
4118f2801cd6SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)], extPLY,     4, &isPLY);CHKERRQ(ierr);
41197bee2925SMatthew Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-10)], extEGADS,   9, &isEGADS);CHKERRQ(ierr);
41208ca92349SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)], extCV,      4, &isCV);CHKERRQ(ierr);
4121de78e4feSLisandro Dalcin   if (isGmsh || isGmsh2 || isGmsh4) {
41227d282ae0SMichael Lange     ierr = DMPlexCreateGmshFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
4123ca522641SMatthew G. Knepley   } else if (isCGNS) {
4124ca522641SMatthew G. Knepley     ierr = DMPlexCreateCGNSFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
412590c68965SMatthew G. Knepley   } else if (isExodus || isGenesis) {
4126ca522641SMatthew G. Knepley     ierr = DMPlexCreateExodusFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
41272f0bd6dcSMichael Lange   } else if (isFluent) {
41282f0bd6dcSMichael Lange     ierr = DMPlexCreateFluentFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
4129cc2f8f65SMatthew G. Knepley   } else if (isHDF5) {
41309c48423bSVaclav Hapla     PetscBool      load_hdf5_xdmf = PETSC_FALSE;
4131cc2f8f65SMatthew G. Knepley     PetscViewer viewer;
4132cc2f8f65SMatthew G. Knepley 
413343b242b4SVaclav Hapla     /* PETSC_VIEWER_HDF5_XDMF is used if the filename ends with .xdmf.h5, or if -dm_plex_create_from_hdf5_xdmf option is present */
413443b242b4SVaclav Hapla     ierr = PetscStrncmp(&filename[PetscMax(0,len-8)], ".xdmf",  5, &load_hdf5_xdmf);CHKERRQ(ierr);
41359c48423bSVaclav Hapla     ierr = PetscOptionsGetBool(NULL, NULL, "-dm_plex_create_from_hdf5_xdmf", &load_hdf5_xdmf, NULL);CHKERRQ(ierr);
4136cc2f8f65SMatthew G. Knepley     ierr = PetscViewerCreate(comm, &viewer);CHKERRQ(ierr);
4137cc2f8f65SMatthew G. Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERHDF5);CHKERRQ(ierr);
4138bca97951SVaclav Hapla     ierr = PetscViewerSetOptionsPrefix(viewer, "dm_plex_create_");CHKERRQ(ierr);
4139bca97951SVaclav Hapla     ierr = PetscViewerSetFromOptions(viewer);CHKERRQ(ierr);
4140cc2f8f65SMatthew G. Knepley     ierr = PetscViewerFileSetMode(viewer, FILE_MODE_READ);CHKERRQ(ierr);
4141cc2f8f65SMatthew G. Knepley     ierr = PetscViewerFileSetName(viewer, filename);CHKERRQ(ierr);
4142cc2f8f65SMatthew G. Knepley     ierr = DMCreate(comm, dm);CHKERRQ(ierr);
4143cc2f8f65SMatthew G. Knepley     ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
41449c48423bSVaclav Hapla     if (load_hdf5_xdmf) {ierr = PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_XDMF);CHKERRQ(ierr);}
4145cc2f8f65SMatthew G. Knepley     ierr = DMLoad(*dm, viewer);CHKERRQ(ierr);
41469c48423bSVaclav Hapla     if (load_hdf5_xdmf) {ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);}
4147cc2f8f65SMatthew G. Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
41485fd9971aSMatthew G. Knepley 
41495fd9971aSMatthew G. Knepley     if (interpolate) {
41505fd9971aSMatthew G. Knepley       DM idm;
41515fd9971aSMatthew G. Knepley 
41525fd9971aSMatthew G. Knepley       ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr);
41535fd9971aSMatthew G. Knepley       ierr = DMDestroy(dm);CHKERRQ(ierr);
41545fd9971aSMatthew G. Knepley       *dm  = idm;
41555fd9971aSMatthew G. Knepley     }
4156707dd687SMichael Lange   } else if (isMed) {
4157707dd687SMichael Lange     ierr = DMPlexCreateMedFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
4158f2801cd6SMatthew G. Knepley   } else if (isPLY) {
4159f2801cd6SMatthew G. Knepley     ierr = DMPlexCreatePLYFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
41607bee2925SMatthew Knepley   } else if (isEGADS) {
41617bee2925SMatthew Knepley     ierr = DMPlexCreateEGADSFromFile(comm, filename, dm);CHKERRQ(ierr);
41627bee2925SMatthew Knepley     if (!interpolate) {
41637bee2925SMatthew Knepley       DM udm;
41647bee2925SMatthew Knepley 
41657bee2925SMatthew Knepley       ierr = DMPlexUninterpolate(*dm, &udm);CHKERRQ(ierr);
41667bee2925SMatthew Knepley       ierr = DMDestroy(dm);CHKERRQ(ierr);
41677bee2925SMatthew Knepley       *dm  = udm;
41687bee2925SMatthew Knepley     }
41698ca92349SMatthew G. Knepley   } else if (isCV) {
41708ca92349SMatthew G. Knepley     ierr = DMPlexCreateCellVertexFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
4171ca522641SMatthew G. Knepley   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename);
4172f1f45c63SVaclav Hapla   ierr = PetscLogEventEnd(DMPLEX_CreateFromFile,0,0,0,0);CHKERRQ(ierr);
4173ca522641SMatthew G. Knepley   PetscFunctionReturn(0);
4174ca522641SMatthew G. Knepley }
4175