xref: /petsc/src/dm/impls/plex/plexcreate.c (revision f8d5e32088cbd42f3dd103c6c1003f6eacc3c7e1)
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 
89318fe57SMatthew G. Knepley /* External function declarations here */
99318fe57SMatthew G. Knepley static PetscErrorCode DMInitialize_Plex(DM dm);
109318fe57SMatthew G. Knepley 
119318fe57SMatthew G. Knepley /* Replace dm with the contents of ndm, and then destroy ndm
129318fe57SMatthew G. Knepley    - Share the DM_Plex structure
139318fe57SMatthew G. Knepley    - Share the coordinates
149318fe57SMatthew G. Knepley    - Share the SF
159318fe57SMatthew G. Knepley */
169318fe57SMatthew G. Knepley static PetscErrorCode DMPlexReplace_Static(DM dm, DM *ndm)
179318fe57SMatthew G. Knepley {
189318fe57SMatthew G. Knepley   PetscSF               sf;
199318fe57SMatthew G. Knepley   DM                    dmNew = *ndm, coordDM, coarseDM;
209318fe57SMatthew G. Knepley   Vec                   coords;
219318fe57SMatthew G. Knepley   PetscBool             isper;
229318fe57SMatthew G. Knepley   const PetscReal      *maxCell, *L;
239318fe57SMatthew G. Knepley   const DMBoundaryType *bd;
249318fe57SMatthew G. Knepley   PetscInt              dim, cdim;
259318fe57SMatthew G. Knepley   PetscErrorCode        ierr;
269318fe57SMatthew G. Knepley 
279318fe57SMatthew G. Knepley   PetscFunctionBegin;
289318fe57SMatthew G. Knepley   if (dm == dmNew) {
299318fe57SMatthew G. Knepley     ierr = DMDestroy(ndm);CHKERRQ(ierr);
309318fe57SMatthew G. Knepley     PetscFunctionReturn(0);
319318fe57SMatthew G. Knepley   }
329318fe57SMatthew G. Knepley   dm->setupcalled = dmNew->setupcalled;
339318fe57SMatthew G. Knepley   ierr = DMGetDimension(dmNew, &dim);CHKERRQ(ierr);
349318fe57SMatthew G. Knepley   ierr = DMSetDimension(dm, dim);CHKERRQ(ierr);
359318fe57SMatthew G. Knepley   ierr = DMGetCoordinateDim(dmNew, &cdim);CHKERRQ(ierr);
369318fe57SMatthew G. Knepley   ierr = DMSetCoordinateDim(dm, cdim);CHKERRQ(ierr);
379318fe57SMatthew G. Knepley   ierr = DMGetPointSF(dmNew, &sf);CHKERRQ(ierr);
389318fe57SMatthew G. Knepley   ierr = DMSetPointSF(dm, sf);CHKERRQ(ierr);
399318fe57SMatthew G. Knepley   ierr = DMGetCoordinateDM(dmNew, &coordDM);CHKERRQ(ierr);
409318fe57SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dmNew, &coords);CHKERRQ(ierr);
419318fe57SMatthew G. Knepley   ierr = DMSetCoordinateDM(dm, coordDM);CHKERRQ(ierr);
429318fe57SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, coords);CHKERRQ(ierr);
439318fe57SMatthew G. Knepley   /* Do not want to create the coordinate field if it does not already exist, so do not call DMGetCoordinateField() */
449318fe57SMatthew G. Knepley   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
459318fe57SMatthew G. Knepley   dm->coordinateField = dmNew->coordinateField;
4661a622f3SMatthew G. Knepley   ((DM_Plex *) dmNew->data)->coordFunc = ((DM_Plex *) dm->data)->coordFunc;
479318fe57SMatthew G. Knepley   ierr = DMGetPeriodicity(dmNew, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
489318fe57SMatthew G. Knepley   ierr = DMSetPeriodicity(dm, isper, maxCell, L, bd);CHKERRQ(ierr);
499318fe57SMatthew G. Knepley   ierr = DMDestroy_Plex(dm);CHKERRQ(ierr);
509318fe57SMatthew G. Knepley   ierr = DMInitialize_Plex(dm);CHKERRQ(ierr);
519318fe57SMatthew G. Knepley   dm->data = dmNew->data;
529318fe57SMatthew G. Knepley   ((DM_Plex *) dmNew->data)->refct++;
539318fe57SMatthew G. Knepley   ierr = DMDestroyLabelLinkList_Internal(dm);CHKERRQ(ierr);
542cbb9b06SVaclav Hapla   ierr = DMCopyLabels(dmNew, dm, PETSC_OWN_POINTER, PETSC_TRUE, DM_COPY_LABELS_FAIL);CHKERRQ(ierr);
559318fe57SMatthew G. Knepley   ierr = DMGetCoarseDM(dmNew,&coarseDM);CHKERRQ(ierr);
569318fe57SMatthew G. Knepley   ierr = DMSetCoarseDM(dm,coarseDM);CHKERRQ(ierr);
579318fe57SMatthew G. Knepley   ierr = DMDestroy(ndm);CHKERRQ(ierr);
589318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
599318fe57SMatthew G. Knepley }
609318fe57SMatthew G. Knepley 
619318fe57SMatthew G. Knepley /* Swap dm with the contents of dmNew
629318fe57SMatthew G. Knepley    - Swap the DM_Plex structure
639318fe57SMatthew G. Knepley    - Swap the coordinates
649318fe57SMatthew G. Knepley    - Swap the point PetscSF
659318fe57SMatthew G. Knepley */
669318fe57SMatthew G. Knepley static PetscErrorCode DMPlexSwap_Static(DM dmA, DM dmB)
679318fe57SMatthew G. Knepley {
689318fe57SMatthew G. Knepley   DM              coordDMA, coordDMB;
699318fe57SMatthew G. Knepley   Vec             coordsA,  coordsB;
709318fe57SMatthew G. Knepley   PetscSF         sfA,      sfB;
719318fe57SMatthew G. Knepley   DMField         fieldTmp;
729318fe57SMatthew G. Knepley   void            *tmp;
739318fe57SMatthew G. Knepley   DMLabelLink     listTmp;
749318fe57SMatthew G. Knepley   DMLabel         depthTmp;
759318fe57SMatthew G. Knepley   PetscInt        tmpI;
769318fe57SMatthew G. Knepley   PetscErrorCode  ierr;
779318fe57SMatthew G. Knepley 
789318fe57SMatthew G. Knepley   PetscFunctionBegin;
799318fe57SMatthew G. Knepley   if (dmA == dmB) PetscFunctionReturn(0);
809318fe57SMatthew G. Knepley   ierr = DMGetPointSF(dmA, &sfA);CHKERRQ(ierr);
819318fe57SMatthew G. Knepley   ierr = DMGetPointSF(dmB, &sfB);CHKERRQ(ierr);
829318fe57SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) sfA);CHKERRQ(ierr);
839318fe57SMatthew G. Knepley   ierr = DMSetPointSF(dmA, sfB);CHKERRQ(ierr);
849318fe57SMatthew G. Knepley   ierr = DMSetPointSF(dmB, sfA);CHKERRQ(ierr);
859318fe57SMatthew G. Knepley   ierr = PetscObjectDereference((PetscObject) sfA);CHKERRQ(ierr);
869318fe57SMatthew G. Knepley 
879318fe57SMatthew G. Knepley   ierr = DMGetCoordinateDM(dmA, &coordDMA);CHKERRQ(ierr);
889318fe57SMatthew G. Knepley   ierr = DMGetCoordinateDM(dmB, &coordDMB);CHKERRQ(ierr);
899318fe57SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) coordDMA);CHKERRQ(ierr);
909318fe57SMatthew G. Knepley   ierr = DMSetCoordinateDM(dmA, coordDMB);CHKERRQ(ierr);
919318fe57SMatthew G. Knepley   ierr = DMSetCoordinateDM(dmB, coordDMA);CHKERRQ(ierr);
929318fe57SMatthew G. Knepley   ierr = PetscObjectDereference((PetscObject) coordDMA);CHKERRQ(ierr);
939318fe57SMatthew G. Knepley 
949318fe57SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dmA, &coordsA);CHKERRQ(ierr);
959318fe57SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dmB, &coordsB);CHKERRQ(ierr);
969318fe57SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) coordsA);CHKERRQ(ierr);
979318fe57SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmA, coordsB);CHKERRQ(ierr);
989318fe57SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmB, coordsA);CHKERRQ(ierr);
999318fe57SMatthew G. Knepley   ierr = PetscObjectDereference((PetscObject) coordsA);CHKERRQ(ierr);
1009318fe57SMatthew G. Knepley 
1019318fe57SMatthew G. Knepley   fieldTmp             = dmA->coordinateField;
1029318fe57SMatthew G. Knepley   dmA->coordinateField = dmB->coordinateField;
1039318fe57SMatthew G. Knepley   dmB->coordinateField = fieldTmp;
1049318fe57SMatthew G. Knepley   tmp       = dmA->data;
1059318fe57SMatthew G. Knepley   dmA->data = dmB->data;
1069318fe57SMatthew G. Knepley   dmB->data = tmp;
1079318fe57SMatthew G. Knepley   listTmp   = dmA->labels;
1089318fe57SMatthew G. Knepley   dmA->labels = dmB->labels;
1099318fe57SMatthew G. Knepley   dmB->labels = listTmp;
1109318fe57SMatthew G. Knepley   depthTmp  = dmA->depthLabel;
1119318fe57SMatthew G. Knepley   dmA->depthLabel = dmB->depthLabel;
1129318fe57SMatthew G. Knepley   dmB->depthLabel = depthTmp;
1139318fe57SMatthew G. Knepley   depthTmp  = dmA->celltypeLabel;
1149318fe57SMatthew G. Knepley   dmA->celltypeLabel = dmB->celltypeLabel;
1159318fe57SMatthew G. Knepley   dmB->celltypeLabel = depthTmp;
1169318fe57SMatthew G. Knepley   tmpI         = dmA->levelup;
1179318fe57SMatthew G. Knepley   dmA->levelup = dmB->levelup;
1189318fe57SMatthew G. Knepley   dmB->levelup = tmpI;
1199318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
1209318fe57SMatthew G. Knepley }
1219318fe57SMatthew G. Knepley 
1229318fe57SMatthew G. Knepley static PetscErrorCode DMPlexInterpolateInPlace_Internal(DM dm)
1239318fe57SMatthew G. Knepley {
1249318fe57SMatthew G. Knepley   DM             idm;
1259318fe57SMatthew G. Knepley   PetscErrorCode ierr;
1269318fe57SMatthew G. Knepley 
1279318fe57SMatthew G. Knepley   PetscFunctionBegin;
1289318fe57SMatthew G. Knepley   ierr = DMPlexInterpolate(dm, &idm);CHKERRQ(ierr);
1299318fe57SMatthew G. Knepley   ierr = DMPlexCopyCoordinates(dm, idm);CHKERRQ(ierr);
1309318fe57SMatthew G. Knepley   ierr = DMPlexReplace_Static(dm, &idm);CHKERRQ(ierr);
1319318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
1329318fe57SMatthew G. Knepley }
1339318fe57SMatthew G. Knepley 
1349318fe57SMatthew G. Knepley /*@C
1359318fe57SMatthew G. Knepley   DMPlexCreateCoordinateSpace - Creates a finite element space for the coordinates
1369318fe57SMatthew G. Knepley 
1379318fe57SMatthew G. Knepley   Collective
1389318fe57SMatthew G. Knepley 
1399318fe57SMatthew G. Knepley   Input Parameters:
1409318fe57SMatthew G. Knepley + DM        - The DM
1419318fe57SMatthew G. Knepley . degree    - The degree of the finite element
1429318fe57SMatthew G. Knepley - coordFunc - An optional function to map new points from refinement to the surface
1439318fe57SMatthew G. Knepley 
1449318fe57SMatthew G. Knepley   Level: advanced
1459318fe57SMatthew G. Knepley 
1469318fe57SMatthew G. Knepley .seealso: PetscFECreateLagrange(), DMGetCoordinateDM()
1479318fe57SMatthew G. Knepley @*/
1489318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateCoordinateSpace(DM dm, PetscInt degree, PetscPointFunc coordFunc)
1499318fe57SMatthew G. Knepley {
1509318fe57SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
1519318fe57SMatthew G. Knepley   DM             cdm;
1529318fe57SMatthew G. Knepley   PetscDS        cds;
1539318fe57SMatthew G. Knepley   PetscFE        fe;
1549318fe57SMatthew G. Knepley   PetscClassId   id;
1559318fe57SMatthew G. Knepley   PetscErrorCode ierr;
1569318fe57SMatthew G. Knepley 
1579318fe57SMatthew G. Knepley   PetscFunctionBegin;
1589318fe57SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
1599318fe57SMatthew G. Knepley   ierr = DMGetDS(cdm, &cds);CHKERRQ(ierr);
1609318fe57SMatthew G. Knepley   ierr = PetscDSGetDiscretization(cds, 0, (PetscObject *) &fe);CHKERRQ(ierr);
1619318fe57SMatthew G. Knepley   ierr = PetscObjectGetClassId((PetscObject) fe, &id);CHKERRQ(ierr);
1629318fe57SMatthew G. Knepley   if (id != PETSCFE_CLASSID) {
1639318fe57SMatthew G. Knepley     PetscBool simplex;
1649318fe57SMatthew G. Knepley     PetscInt  dim, dE, qorder;
1659318fe57SMatthew G. Knepley 
1669318fe57SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1679318fe57SMatthew G. Knepley     ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
1689318fe57SMatthew G. Knepley     ierr = DMPlexIsSimplex(dm, &simplex);CHKERRQ(ierr);
1699318fe57SMatthew G. Knepley     qorder = degree;
1709318fe57SMatthew G. Knepley     ierr = PetscObjectOptionsBegin((PetscObject) cdm);CHKERRQ(ierr);
1719318fe57SMatthew 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);
1729318fe57SMatthew G. Knepley     ierr = PetscOptionsEnd();CHKERRQ(ierr);
1739318fe57SMatthew G. Knepley     ierr = PetscFECreateLagrange(PETSC_COMM_SELF, dim, dE, simplex, degree, qorder, &fe);CHKERRQ(ierr);
1749318fe57SMatthew G. Knepley     ierr = DMSetField(cdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
1759318fe57SMatthew G. Knepley     ierr = DMCreateDS(cdm);CHKERRQ(ierr);
1769318fe57SMatthew G. Knepley     ierr = DMProjectCoordinates(dm, fe);CHKERRQ(ierr);
1779318fe57SMatthew G. Knepley     ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
1789318fe57SMatthew G. Knepley   }
1799318fe57SMatthew G. Knepley   mesh->coordFunc = coordFunc;
1809318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
1819318fe57SMatthew G. Knepley }
1829318fe57SMatthew G. Knepley 
1831df5d5c5SMatthew G. Knepley /*@
1841df5d5c5SMatthew G. Knepley   DMPlexCreateDoublet - Creates a mesh of two cells of the specified type, optionally with later refinement.
1851df5d5c5SMatthew G. Knepley 
186d083f849SBarry Smith   Collective
1871df5d5c5SMatthew G. Knepley 
1881df5d5c5SMatthew G. Knepley   Input Parameters:
1891df5d5c5SMatthew G. Knepley + comm - The communicator for the DM object
1901df5d5c5SMatthew G. Knepley . dim - The spatial dimension
1911df5d5c5SMatthew G. Knepley . simplex - Flag for simplicial cells, otherwise they are tensor product cells
1921df5d5c5SMatthew G. Knepley . interpolate - Flag to create intermediate mesh pieces (edges, faces)
1931df5d5c5SMatthew G. Knepley - refinementLimit - A nonzero number indicates the largest admissible volume for a refined cell
1941df5d5c5SMatthew G. Knepley 
1951df5d5c5SMatthew G. Knepley   Output Parameter:
1961df5d5c5SMatthew G. Knepley . dm - The DM object
1971df5d5c5SMatthew G. Knepley 
1981df5d5c5SMatthew G. Knepley   Level: beginner
1991df5d5c5SMatthew G. Knepley 
2001df5d5c5SMatthew G. Knepley .seealso: DMSetType(), DMCreate()
2011df5d5c5SMatthew G. Knepley @*/
2020fdc7489SMatthew Knepley PetscErrorCode DMPlexCreateDoublet(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscBool interpolate, PetscReal refinementLimit, DM *newdm)
2031df5d5c5SMatthew G. Knepley {
2041df5d5c5SMatthew G. Knepley   DM             dm;
2051df5d5c5SMatthew G. Knepley   PetscMPIInt    rank;
2061df5d5c5SMatthew G. Knepley   PetscErrorCode ierr;
2071df5d5c5SMatthew G. Knepley 
2081df5d5c5SMatthew G. Knepley   PetscFunctionBegin;
2091df5d5c5SMatthew G. Knepley   ierr = DMCreate(comm, &dm);CHKERRQ(ierr);
2101df5d5c5SMatthew G. Knepley   ierr = DMSetType(dm, DMPLEX);CHKERRQ(ierr);
211c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(dm, dim);CHKERRQ(ierr);
212ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
213ce78fa2fSMatthew G. Knepley   switch (dim) {
214ce78fa2fSMatthew G. Knepley   case 2:
215ce78fa2fSMatthew G. Knepley     if (simplex) {ierr = PetscObjectSetName((PetscObject) dm, "triangular");CHKERRQ(ierr);}
216ce78fa2fSMatthew G. Knepley     else         {ierr = PetscObjectSetName((PetscObject) dm, "quadrilateral");CHKERRQ(ierr);}
217ce78fa2fSMatthew G. Knepley     break;
218ce78fa2fSMatthew G. Knepley   case 3:
219ce78fa2fSMatthew G. Knepley     if (simplex) {ierr = PetscObjectSetName((PetscObject) dm, "tetrahedral");CHKERRQ(ierr);}
220ce78fa2fSMatthew G. Knepley     else         {ierr = PetscObjectSetName((PetscObject) dm, "hexahedral");CHKERRQ(ierr);}
221ce78fa2fSMatthew G. Knepley     break;
222ce78fa2fSMatthew G. Knepley   default:
22389c010cfSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %D", dim);
224ce78fa2fSMatthew G. Knepley   }
2251df5d5c5SMatthew G. Knepley   if (rank) {
2261df5d5c5SMatthew G. Knepley     PetscInt numPoints[2] = {0, 0};
2271df5d5c5SMatthew G. Knepley     ierr = DMPlexCreateFromDAG(dm, 1, numPoints, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
2281df5d5c5SMatthew G. Knepley   } else {
2291df5d5c5SMatthew G. Knepley     switch (dim) {
2301df5d5c5SMatthew G. Knepley     case 2:
2311df5d5c5SMatthew G. Knepley       if (simplex) {
2321df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {4, 2};
2331df5d5c5SMatthew G. Knepley         PetscInt    coneSize[6]         = {3, 3, 0, 0, 0, 0};
2341df5d5c5SMatthew G. Knepley         PetscInt    cones[6]            = {2, 3, 4,  5, 4, 3};
2351df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[6] = {0, 0, 0,  0, 0, 0};
2361df5d5c5SMatthew G. Knepley         PetscScalar vertexCoords[8]     = {-0.5, 0.5, 0.0, 0.0, 0.0, 1.0, 0.5, 0.5};
2371df5d5c5SMatthew G. Knepley 
2381df5d5c5SMatthew G. Knepley         ierr = DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2391df5d5c5SMatthew G. Knepley       } else {
2401df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {6, 2};
2411df5d5c5SMatthew G. Knepley         PetscInt    coneSize[8]         = {4, 4, 0, 0, 0, 0, 0, 0};
2421df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {2, 3, 4, 5,  3, 6, 7, 4};
2431df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0,  0, 0, 0, 0};
2441df5d5c5SMatthew 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};
2451df5d5c5SMatthew G. Knepley 
2461df5d5c5SMatthew G. Knepley         ierr = DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2471df5d5c5SMatthew G. Knepley       }
2481df5d5c5SMatthew G. Knepley       break;
2491df5d5c5SMatthew G. Knepley     case 3:
2501df5d5c5SMatthew G. Knepley       if (simplex) {
2511df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {5, 2};
2521df5d5c5SMatthew G. Knepley         PetscInt    coneSize[7]         = {4, 4, 0, 0, 0, 0, 0};
2531df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {4, 3, 5, 2,  5, 3, 4, 6};
2541df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0,  0, 0, 0, 0};
2551df5d5c5SMatthew 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};
2561df5d5c5SMatthew G. Knepley 
2571df5d5c5SMatthew G. Knepley         ierr = DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2581df5d5c5SMatthew G. Knepley       } else {
2591df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]         = {12, 2};
2601df5d5c5SMatthew G. Knepley         PetscInt    coneSize[14]         = {8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
2611df5d5c5SMatthew G. Knepley         PetscInt    cones[16]            = {2, 3, 4, 5, 6, 7, 8, 9,  5, 4, 10, 11, 7, 12, 13, 8};
2621df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[16] = {0, 0, 0, 0, 0, 0, 0, 0,  0, 0,  0,  0, 0,  0,  0, 0};
2631df5d5c5SMatthew 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,
2641df5d5c5SMatthew 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,
2651df5d5c5SMatthew 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};
2661df5d5c5SMatthew G. Knepley 
2671df5d5c5SMatthew G. Knepley         ierr = DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2681df5d5c5SMatthew G. Knepley       }
2691df5d5c5SMatthew G. Knepley       break;
2701df5d5c5SMatthew G. Knepley     default:
27189c010cfSBarry Smith       SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %D", dim);
2721df5d5c5SMatthew G. Knepley     }
2731df5d5c5SMatthew G. Knepley   }
2741df5d5c5SMatthew G. Knepley   *newdm = dm;
2751df5d5c5SMatthew G. Knepley   if (refinementLimit > 0.0) {
2761df5d5c5SMatthew G. Knepley     DM rdm;
2771df5d5c5SMatthew G. Knepley     const char *name;
2781df5d5c5SMatthew G. Knepley 
2791df5d5c5SMatthew G. Knepley     ierr = DMPlexSetRefinementUniform(*newdm, PETSC_FALSE);CHKERRQ(ierr);
2801df5d5c5SMatthew G. Knepley     ierr = DMPlexSetRefinementLimit(*newdm, refinementLimit);CHKERRQ(ierr);
2811df5d5c5SMatthew G. Knepley     ierr = DMRefine(*newdm, comm, &rdm);CHKERRQ(ierr);
2821df5d5c5SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) *newdm, &name);CHKERRQ(ierr);
2831df5d5c5SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject)    rdm,  name);CHKERRQ(ierr);
2841df5d5c5SMatthew G. Knepley     ierr = DMDestroy(newdm);CHKERRQ(ierr);
2851df5d5c5SMatthew G. Knepley     *newdm = rdm;
2861df5d5c5SMatthew G. Knepley   }
2871df5d5c5SMatthew G. Knepley   if (interpolate) {
2885fd9971aSMatthew G. Knepley     DM idm;
2891df5d5c5SMatthew G. Knepley 
2901df5d5c5SMatthew G. Knepley     ierr = DMPlexInterpolate(*newdm, &idm);CHKERRQ(ierr);
2911df5d5c5SMatthew G. Knepley     ierr = DMDestroy(newdm);CHKERRQ(ierr);
2921df5d5c5SMatthew G. Knepley     *newdm = idm;
2931df5d5c5SMatthew G. Knepley   }
2941df5d5c5SMatthew G. Knepley   PetscFunctionReturn(0);
2951df5d5c5SMatthew G. Knepley }
2961df5d5c5SMatthew G. Knepley 
2979318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
2989318fe57SMatthew G. Knepley {
2999318fe57SMatthew G. Knepley   const PetscInt numVertices    = 2;
3009318fe57SMatthew G. Knepley   PetscInt       markerRight    = 1;
3019318fe57SMatthew G. Knepley   PetscInt       markerLeft     = 1;
3029318fe57SMatthew G. Knepley   PetscBool      markerSeparate = PETSC_FALSE;
3039318fe57SMatthew G. Knepley   Vec            coordinates;
3049318fe57SMatthew G. Knepley   PetscSection   coordSection;
3059318fe57SMatthew G. Knepley   PetscScalar   *coords;
3069318fe57SMatthew G. Knepley   PetscInt       coordSize;
3079318fe57SMatthew G. Knepley   PetscMPIInt    rank;
3089318fe57SMatthew G. Knepley   PetscInt       cdim = 1, v;
3099318fe57SMatthew G. Knepley   PetscErrorCode ierr;
310552f7358SJed Brown 
3119318fe57SMatthew G. Knepley   PetscFunctionBegin;
3129318fe57SMatthew G. Knepley   ierr = PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);CHKERRQ(ierr);
3139318fe57SMatthew G. Knepley   if (markerSeparate) {
3149318fe57SMatthew G. Knepley     markerRight  = 2;
3159318fe57SMatthew G. Knepley     markerLeft   = 1;
3169318fe57SMatthew G. Knepley   }
3179318fe57SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRMPI(ierr);
3189318fe57SMatthew G. Knepley   if (!rank) {
3199318fe57SMatthew G. Knepley     ierr = DMPlexSetChart(dm, 0, numVertices);CHKERRQ(ierr);
3209318fe57SMatthew G. Knepley     ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
3219318fe57SMatthew G. Knepley     ierr = DMSetLabelValue(dm, "marker", 0, markerLeft);CHKERRQ(ierr);
3229318fe57SMatthew G. Knepley     ierr = DMSetLabelValue(dm, "marker", 1, markerRight);CHKERRQ(ierr);
3239318fe57SMatthew G. Knepley   }
3249318fe57SMatthew G. Knepley   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
3259318fe57SMatthew G. Knepley   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
3269318fe57SMatthew G. Knepley   /* Build coordinates */
3279318fe57SMatthew G. Knepley   ierr = DMSetCoordinateDim(dm, cdim);CHKERRQ(ierr);
3289318fe57SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
3299318fe57SMatthew G. Knepley   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
3309318fe57SMatthew G. Knepley   ierr = PetscSectionSetChart(coordSection, 0, numVertices);CHKERRQ(ierr);
3319318fe57SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(coordSection, 0, cdim);CHKERRQ(ierr);
3329318fe57SMatthew G. Knepley   for (v = 0; v < numVertices; ++v) {
3339318fe57SMatthew G. Knepley     ierr = PetscSectionSetDof(coordSection, v, cdim);CHKERRQ(ierr);
3349318fe57SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(coordSection, v, 0, cdim);CHKERRQ(ierr);
3359318fe57SMatthew G. Knepley   }
3369318fe57SMatthew G. Knepley   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
3379318fe57SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
3389318fe57SMatthew G. Knepley   ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
3399318fe57SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
3409318fe57SMatthew G. Knepley   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
3419318fe57SMatthew G. Knepley   ierr = VecSetBlockSize(coordinates, cdim);CHKERRQ(ierr);
3429318fe57SMatthew G. Knepley   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
3439318fe57SMatthew G. Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
3449318fe57SMatthew G. Knepley   coords[0] = lower[0];
3459318fe57SMatthew G. Knepley   coords[1] = upper[0];
3469318fe57SMatthew G. Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
3479318fe57SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
3489318fe57SMatthew G. Knepley   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
3499318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
3509318fe57SMatthew G. Knepley }
35126492d91SMatthew G. Knepley 
3529318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
353552f7358SJed Brown {
3541df21d24SMatthew G. Knepley   const PetscInt numVertices    = (edges[0]+1)*(edges[1]+1);
3551df21d24SMatthew G. Knepley   const PetscInt numEdges       = edges[0]*(edges[1]+1) + (edges[0]+1)*edges[1];
356552f7358SJed Brown   PetscInt       markerTop      = 1;
357552f7358SJed Brown   PetscInt       markerBottom   = 1;
358552f7358SJed Brown   PetscInt       markerRight    = 1;
359552f7358SJed Brown   PetscInt       markerLeft     = 1;
360552f7358SJed Brown   PetscBool      markerSeparate = PETSC_FALSE;
361552f7358SJed Brown   Vec            coordinates;
362552f7358SJed Brown   PetscSection   coordSection;
363552f7358SJed Brown   PetscScalar    *coords;
364552f7358SJed Brown   PetscInt       coordSize;
365552f7358SJed Brown   PetscMPIInt    rank;
366552f7358SJed Brown   PetscInt       v, vx, vy;
367552f7358SJed Brown   PetscErrorCode ierr;
368552f7358SJed Brown 
369552f7358SJed Brown   PetscFunctionBegin;
370c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);CHKERRQ(ierr);
371552f7358SJed Brown   if (markerSeparate) {
3721df21d24SMatthew G. Knepley     markerTop    = 3;
3731df21d24SMatthew G. Knepley     markerBottom = 1;
3741df21d24SMatthew G. Knepley     markerRight  = 2;
3751df21d24SMatthew G. Knepley     markerLeft   = 4;
376552f7358SJed Brown   }
377ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRMPI(ierr);
378dd400576SPatrick Sanan   if (rank == 0) {
379552f7358SJed Brown     PetscInt e, ex, ey;
380552f7358SJed Brown 
381552f7358SJed Brown     ierr = DMPlexSetChart(dm, 0, numEdges+numVertices);CHKERRQ(ierr);
382552f7358SJed Brown     for (e = 0; e < numEdges; ++e) {
383552f7358SJed Brown       ierr = DMPlexSetConeSize(dm, e, 2);CHKERRQ(ierr);
384552f7358SJed Brown     }
385552f7358SJed Brown     ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
386552f7358SJed Brown     for (vx = 0; vx <= edges[0]; vx++) {
387552f7358SJed Brown       for (ey = 0; ey < edges[1]; ey++) {
388552f7358SJed Brown         PetscInt edge   = vx*edges[1] + ey + edges[0]*(edges[1]+1);
389552f7358SJed Brown         PetscInt vertex = ey*(edges[0]+1) + vx + numEdges;
390da80777bSKarl Rupp         PetscInt cone[2];
391552f7358SJed Brown 
392da80777bSKarl Rupp         cone[0] = vertex; cone[1] = vertex+edges[0]+1;
393552f7358SJed Brown         ierr    = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr);
394552f7358SJed Brown         if (vx == edges[0]) {
395c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", edge,    markerRight);CHKERRQ(ierr);
396c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", cone[0], markerRight);CHKERRQ(ierr);
397552f7358SJed Brown           if (ey == edges[1]-1) {
398c58f1c22SToby Isaac             ierr = DMSetLabelValue(dm, "marker", cone[1], markerRight);CHKERRQ(ierr);
399c668006fSMatthew G. Knepley             ierr = DMSetLabelValue(dm, "Face Sets", cone[1], markerRight);CHKERRQ(ierr);
400552f7358SJed Brown           }
401552f7358SJed Brown         } else if (vx == 0) {
402c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", edge,    markerLeft);CHKERRQ(ierr);
403c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", cone[0], markerLeft);CHKERRQ(ierr);
404552f7358SJed Brown           if (ey == edges[1]-1) {
405c58f1c22SToby Isaac             ierr = DMSetLabelValue(dm, "marker", cone[1], markerLeft);CHKERRQ(ierr);
406c668006fSMatthew G. Knepley             ierr = DMSetLabelValue(dm, "Face Sets", cone[1], markerLeft);CHKERRQ(ierr);
407552f7358SJed Brown           }
408552f7358SJed Brown         }
409552f7358SJed Brown       }
410552f7358SJed Brown     }
411552f7358SJed Brown     for (vy = 0; vy <= edges[1]; vy++) {
412552f7358SJed Brown       for (ex = 0; ex < edges[0]; ex++) {
413552f7358SJed Brown         PetscInt edge   = vy*edges[0]     + ex;
414552f7358SJed Brown         PetscInt vertex = vy*(edges[0]+1) + ex + numEdges;
415da80777bSKarl Rupp         PetscInt cone[2];
416552f7358SJed Brown 
417da80777bSKarl Rupp         cone[0] = vertex; cone[1] = vertex+1;
418552f7358SJed Brown         ierr    = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr);
419552f7358SJed Brown         if (vy == edges[1]) {
420c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", edge,    markerTop);CHKERRQ(ierr);
421c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", cone[0], markerTop);CHKERRQ(ierr);
422552f7358SJed Brown           if (ex == edges[0]-1) {
423c58f1c22SToby Isaac             ierr = DMSetLabelValue(dm, "marker", cone[1], markerTop);CHKERRQ(ierr);
424c668006fSMatthew G. Knepley             ierr = DMSetLabelValue(dm, "Face Sets", cone[1], markerTop);CHKERRQ(ierr);
425552f7358SJed Brown           }
426552f7358SJed Brown         } else if (vy == 0) {
427c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", edge,    markerBottom);CHKERRQ(ierr);
428c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", cone[0], markerBottom);CHKERRQ(ierr);
429552f7358SJed Brown           if (ex == edges[0]-1) {
430c58f1c22SToby Isaac             ierr = DMSetLabelValue(dm, "marker", cone[1], markerBottom);CHKERRQ(ierr);
431c668006fSMatthew G. Knepley             ierr = DMSetLabelValue(dm, "Face Sets", cone[1], markerBottom);CHKERRQ(ierr);
432552f7358SJed Brown           }
433552f7358SJed Brown         }
434552f7358SJed Brown       }
435552f7358SJed Brown     }
436552f7358SJed Brown   }
437552f7358SJed Brown   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
438552f7358SJed Brown   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
439552f7358SJed Brown   /* Build coordinates */
4409596c6baSMatthew G. Knepley   ierr = DMSetCoordinateDim(dm, 2);CHKERRQ(ierr);
441c2166f76SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
442972bc18aSToby Isaac   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
443552f7358SJed Brown   ierr = PetscSectionSetChart(coordSection, numEdges, numEdges + numVertices);CHKERRQ(ierr);
444972bc18aSToby Isaac   ierr = PetscSectionSetFieldComponents(coordSection, 0, 2);CHKERRQ(ierr);
445552f7358SJed Brown   for (v = numEdges; v < numEdges+numVertices; ++v) {
446552f7358SJed Brown     ierr = PetscSectionSetDof(coordSection, v, 2);CHKERRQ(ierr);
447972bc18aSToby Isaac     ierr = PetscSectionSetFieldDof(coordSection, v, 0, 2);CHKERRQ(ierr);
448552f7358SJed Brown   }
449552f7358SJed Brown   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
450552f7358SJed Brown   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
4518b9ced59SLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
452da16285aSMichael Lange   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
453552f7358SJed Brown   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
4548b9ced59SLisandro Dalcin   ierr = VecSetBlockSize(coordinates, 2);CHKERRQ(ierr);
4552eb5907fSJed Brown   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
456552f7358SJed Brown   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
457552f7358SJed Brown   for (vy = 0; vy <= edges[1]; ++vy) {
458552f7358SJed Brown     for (vx = 0; vx <= edges[0]; ++vx) {
459552f7358SJed Brown       coords[(vy*(edges[0]+1)+vx)*2+0] = lower[0] + ((upper[0] - lower[0])/edges[0])*vx;
460552f7358SJed Brown       coords[(vy*(edges[0]+1)+vx)*2+1] = lower[1] + ((upper[1] - lower[1])/edges[1])*vy;
461552f7358SJed Brown     }
462552f7358SJed Brown   }
463552f7358SJed Brown   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
464552f7358SJed Brown   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
465552f7358SJed Brown   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
466552f7358SJed Brown   PetscFunctionReturn(0);
467552f7358SJed Brown }
468552f7358SJed Brown 
4699318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt faces[])
470552f7358SJed Brown {
4719e8abbc3SMichael Lange   PetscInt       vertices[3], numVertices;
4727b59f5a9SMichael Lange   PetscInt       numFaces    = 2*faces[0]*faces[1] + 2*faces[1]*faces[2] + 2*faces[0]*faces[2];
473552f7358SJed Brown   Vec            coordinates;
474552f7358SJed Brown   PetscSection   coordSection;
475552f7358SJed Brown   PetscScalar    *coords;
476552f7358SJed Brown   PetscInt       coordSize;
477552f7358SJed Brown   PetscMPIInt    rank;
478552f7358SJed Brown   PetscInt       v, vx, vy, vz;
4797b59f5a9SMichael Lange   PetscInt       voffset, iface=0, cone[4];
480552f7358SJed Brown   PetscErrorCode ierr;
481552f7358SJed Brown 
482552f7358SJed Brown   PetscFunctionBegin;
48382f516ccSBarry 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");
484ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRMPI(ierr);
4859e8abbc3SMichael Lange   vertices[0] = faces[0]+1; vertices[1] = faces[1]+1; vertices[2] = faces[2]+1;
4869e8abbc3SMichael Lange   numVertices = vertices[0]*vertices[1]*vertices[2];
487dd400576SPatrick Sanan   if (rank == 0) {
488552f7358SJed Brown     PetscInt f;
489552f7358SJed Brown 
490552f7358SJed Brown     ierr = DMPlexSetChart(dm, 0, numFaces+numVertices);CHKERRQ(ierr);
491552f7358SJed Brown     for (f = 0; f < numFaces; ++f) {
492552f7358SJed Brown       ierr = DMPlexSetConeSize(dm, f, 4);CHKERRQ(ierr);
493552f7358SJed Brown     }
494552f7358SJed Brown     ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
4957b59f5a9SMichael Lange 
4967b59f5a9SMichael Lange     /* Side 0 (Top) */
4977b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
4987b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
4997b59f5a9SMichael Lange         voffset = numFaces + vertices[0]*vertices[1]*(vertices[2]-1) + vy*vertices[0] + vx;
5007b59f5a9SMichael Lange         cone[0] = voffset; cone[1] = voffset+1; cone[2] = voffset+vertices[0]+1; cone[3] = voffset+vertices[0];
5017b59f5a9SMichael Lange         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
502c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", iface, 1);CHKERRQ(ierr);
503c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+0, 1);CHKERRQ(ierr);
504c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+1, 1);CHKERRQ(ierr);
505c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, 1);CHKERRQ(ierr);
506c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]+1, 1);CHKERRQ(ierr);
5077b59f5a9SMichael Lange         iface++;
508552f7358SJed Brown       }
509552f7358SJed Brown     }
5107b59f5a9SMichael Lange 
5117b59f5a9SMichael Lange     /* Side 1 (Bottom) */
5127b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
5137b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5147b59f5a9SMichael Lange         voffset = numFaces + vy*(faces[0]+1) + vx;
5157b59f5a9SMichael Lange         cone[0] = voffset+1; cone[1] = voffset; cone[2] = voffset+vertices[0]; cone[3] = voffset+vertices[0]+1;
5167b59f5a9SMichael Lange         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
517c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", iface, 1);CHKERRQ(ierr);
518c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+0, 1);CHKERRQ(ierr);
519c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+1, 1);CHKERRQ(ierr);
520c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, 1);CHKERRQ(ierr);
521c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]+1, 1);CHKERRQ(ierr);
5227b59f5a9SMichael Lange         iface++;
523552f7358SJed Brown       }
524552f7358SJed Brown     }
5257b59f5a9SMichael Lange 
5267b59f5a9SMichael Lange     /* Side 2 (Front) */
5277b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
5287b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5297b59f5a9SMichael Lange         voffset = numFaces + vz*vertices[0]*vertices[1] + vx;
5307b59f5a9SMichael Lange         cone[0] = voffset; cone[1] = voffset+1; cone[2] = voffset+vertices[0]*vertices[1]+1; cone[3] = voffset+vertices[0]*vertices[1];
5317b59f5a9SMichael Lange         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
532c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", iface, 1);CHKERRQ(ierr);
533c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+0, 1);CHKERRQ(ierr);
534c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+1, 1);CHKERRQ(ierr);
535c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+0, 1);CHKERRQ(ierr);
536c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+1, 1);CHKERRQ(ierr);
5377b59f5a9SMichael Lange         iface++;
538552f7358SJed Brown       }
5397b59f5a9SMichael Lange     }
5407b59f5a9SMichael Lange 
5417b59f5a9SMichael Lange     /* Side 3 (Back) */
5427b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
5437b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5447b59f5a9SMichael Lange         voffset = numFaces + vz*vertices[0]*vertices[1] + vertices[0]*(vertices[1]-1) + vx;
5457b59f5a9SMichael Lange         cone[0] = voffset+vertices[0]*vertices[1]; cone[1] = voffset+vertices[0]*vertices[1]+1;
5467b59f5a9SMichael Lange         cone[2] = voffset+1; cone[3] = voffset;
5477b59f5a9SMichael Lange         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
548c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", iface, 1);CHKERRQ(ierr);
549c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+0, 1);CHKERRQ(ierr);
550c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+1, 1);CHKERRQ(ierr);
551c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+0, 1);CHKERRQ(ierr);
552c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+1, 1);CHKERRQ(ierr);
5537b59f5a9SMichael Lange         iface++;
5547b59f5a9SMichael Lange       }
5557b59f5a9SMichael Lange     }
5567b59f5a9SMichael Lange 
5577b59f5a9SMichael Lange     /* Side 4 (Left) */
5587b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
5597b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
5607b59f5a9SMichael Lange         voffset = numFaces + vz*vertices[0]*vertices[1] + vy*vertices[0];
5617b59f5a9SMichael Lange         cone[0] = voffset; cone[1] = voffset+vertices[0]*vertices[1];
5627b59f5a9SMichael Lange         cone[2] = voffset+vertices[0]*vertices[1]+vertices[0]; cone[3] = voffset+vertices[0];
5637b59f5a9SMichael Lange         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
564c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", iface, 1);CHKERRQ(ierr);
565c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+0, 1);CHKERRQ(ierr);
566c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, 1);CHKERRQ(ierr);
567c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[1]+0, 1);CHKERRQ(ierr);
568c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+vertices[0], 1);CHKERRQ(ierr);
5697b59f5a9SMichael Lange         iface++;
5707b59f5a9SMichael Lange       }
5717b59f5a9SMichael Lange     }
5727b59f5a9SMichael Lange 
5737b59f5a9SMichael Lange     /* Side 5 (Right) */
5747b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
5757b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
576aab5bcd8SJed Brown         voffset = numFaces + vz*vertices[0]*vertices[1] + vy*vertices[0] + faces[0];
5777b59f5a9SMichael Lange         cone[0] = voffset+vertices[0]*vertices[1]; cone[1] = voffset;
5787b59f5a9SMichael Lange         cone[2] = voffset+vertices[0]; cone[3] = voffset+vertices[0]*vertices[1]+vertices[0];
5797b59f5a9SMichael Lange         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
580c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", iface, 1);CHKERRQ(ierr);
581c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+0, 1);CHKERRQ(ierr);
582c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, 1);CHKERRQ(ierr);
583c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+0, 1);CHKERRQ(ierr);
584c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+vertices[0], 1);CHKERRQ(ierr);
5857b59f5a9SMichael Lange         iface++;
5867b59f5a9SMichael Lange       }
587552f7358SJed Brown     }
588552f7358SJed Brown   }
589552f7358SJed Brown   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
590552f7358SJed Brown   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
591552f7358SJed Brown   /* Build coordinates */
5929596c6baSMatthew G. Knepley   ierr = DMSetCoordinateDim(dm, 3);CHKERRQ(ierr);
593c2166f76SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
5949318fe57SMatthew G. Knepley   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
595552f7358SJed Brown   ierr = PetscSectionSetChart(coordSection, numFaces, numFaces + numVertices);CHKERRQ(ierr);
5969318fe57SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(coordSection, 0, 3);CHKERRQ(ierr);
597552f7358SJed Brown   for (v = numFaces; v < numFaces+numVertices; ++v) {
598552f7358SJed Brown     ierr = PetscSectionSetDof(coordSection, v, 3);CHKERRQ(ierr);
5999318fe57SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(coordSection, v, 0, 3);CHKERRQ(ierr);
600552f7358SJed Brown   }
601552f7358SJed Brown   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
602552f7358SJed Brown   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
6038b9ced59SLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
604552f7358SJed Brown   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
605552f7358SJed Brown   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
6068b9ced59SLisandro Dalcin   ierr = VecSetBlockSize(coordinates, 3);CHKERRQ(ierr);
6072eb5907fSJed Brown   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
608552f7358SJed Brown   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
609552f7358SJed Brown   for (vz = 0; vz <= faces[2]; ++vz) {
610552f7358SJed Brown     for (vy = 0; vy <= faces[1]; ++vy) {
611552f7358SJed Brown       for (vx = 0; vx <= faces[0]; ++vx) {
612552f7358SJed Brown         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+0] = lower[0] + ((upper[0] - lower[0])/faces[0])*vx;
613552f7358SJed Brown         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+1] = lower[1] + ((upper[1] - lower[1])/faces[1])*vy;
614552f7358SJed Brown         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+2] = lower[2] + ((upper[2] - lower[2])/faces[2])*vz;
615552f7358SJed Brown       }
616552f7358SJed Brown     }
617552f7358SJed Brown   }
618552f7358SJed Brown   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
619552f7358SJed Brown   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
620552f7358SJed Brown   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
621552f7358SJed Brown   PetscFunctionReturn(0);
622552f7358SJed Brown }
623552f7358SJed Brown 
6249318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate)
6259318fe57SMatthew G. Knepley {
6269318fe57SMatthew G. Knepley   PetscErrorCode ierr;
6279318fe57SMatthew G. Knepley 
6289318fe57SMatthew G. Knepley   PetscFunctionBegin;
6299318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6309318fe57SMatthew G. Knepley   ierr = DMSetDimension(dm, dim-1);CHKERRQ(ierr);
6319318fe57SMatthew G. Knepley   ierr = DMSetCoordinateDim(dm, dim);CHKERRQ(ierr);
6329318fe57SMatthew G. Knepley   switch (dim) {
6339318fe57SMatthew G. Knepley     case 1: ierr = DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(dm, lower, upper, faces);CHKERRQ(ierr);break;
6349318fe57SMatthew G. Knepley     case 2: ierr = DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(dm, lower, upper, faces);CHKERRQ(ierr);break;
6359318fe57SMatthew G. Knepley     case 3: ierr = DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(dm, lower, upper, faces);CHKERRQ(ierr);break;
6369318fe57SMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Dimension not supported: %D", dim);
6379318fe57SMatthew G. Knepley   }
6389318fe57SMatthew G. Knepley   if (interpolate) {ierr = DMPlexInterpolateInPlace_Internal(dm);CHKERRQ(ierr);}
6399318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
6409318fe57SMatthew G. Knepley }
6419318fe57SMatthew G. Knepley 
6429318fe57SMatthew G. Knepley /*@C
6439318fe57SMatthew G. Knepley   DMPlexCreateBoxSurfaceMesh - Creates a mesh on the surface of the tensor product of unit intervals (box) using tensor cells (hexahedra).
6449318fe57SMatthew G. Knepley 
6459318fe57SMatthew G. Knepley   Collective
6469318fe57SMatthew G. Knepley 
6479318fe57SMatthew G. Knepley   Input Parameters:
6489318fe57SMatthew G. Knepley + comm        - The communicator for the DM object
6499318fe57SMatthew G. Knepley . dim         - The spatial dimension of the box, so the resulting mesh is has dimension dim-1
6509318fe57SMatthew 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
6519318fe57SMatthew G. Knepley . lower       - The lower left corner, or NULL for (0, 0, 0)
6529318fe57SMatthew G. Knepley . upper       - The upper right corner, or NULL for (1, 1, 1)
6539318fe57SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
6549318fe57SMatthew G. Knepley 
6559318fe57SMatthew G. Knepley   Output Parameter:
6569318fe57SMatthew G. Knepley . dm  - The DM object
6579318fe57SMatthew G. Knepley 
6589318fe57SMatthew G. Knepley   Level: beginner
6599318fe57SMatthew G. Knepley 
6609318fe57SMatthew G. Knepley .seealso: DMSetFromOptions(), DMPlexCreateBoxMesh(), DMPlexCreateFromFile(), DMSetType(), DMCreate()
6619318fe57SMatthew G. Knepley @*/
6629318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateBoxSurfaceMesh(MPI_Comm comm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate, DM *dm)
6639318fe57SMatthew G. Knepley {
6649318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
6659318fe57SMatthew G. Knepley   PetscReal      low[3] = {0, 0, 0};
6669318fe57SMatthew G. Knepley   PetscReal      upp[3] = {1, 1, 1};
6679318fe57SMatthew G. Knepley   PetscErrorCode ierr;
6689318fe57SMatthew G. Knepley 
6699318fe57SMatthew G. Knepley   PetscFunctionBegin;
6709318fe57SMatthew G. Knepley   ierr = DMCreate(comm,dm);CHKERRQ(ierr);
6719318fe57SMatthew G. Knepley   ierr = DMSetType(*dm,DMPLEX);CHKERRQ(ierr);
6729318fe57SMatthew G. Knepley   ierr = DMPlexCreateBoxSurfaceMesh_Internal(*dm, dim, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, interpolate);CHKERRQ(ierr);
6739318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
6749318fe57SMatthew G. Knepley }
6759318fe57SMatthew G. Knepley 
6769318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateLineMesh_Internal(DM dm,PetscInt segments,PetscReal lower,PetscReal upper,DMBoundaryType bd)
677fdbf62faSLisandro Dalcin {
678fdbf62faSLisandro Dalcin   PetscInt       i,fStart,fEnd,numCells = 0,numVerts = 0;
679fdbf62faSLisandro Dalcin   PetscInt       numPoints[2],*coneSize,*cones,*coneOrientations;
680fdbf62faSLisandro Dalcin   PetscScalar    *vertexCoords;
681fdbf62faSLisandro Dalcin   PetscReal      L,maxCell;
682fdbf62faSLisandro Dalcin   PetscBool      markerSeparate = PETSC_FALSE;
683fdbf62faSLisandro Dalcin   PetscInt       markerLeft  = 1, faceMarkerLeft  = 1;
684fdbf62faSLisandro Dalcin   PetscInt       markerRight = 1, faceMarkerRight = 2;
685fdbf62faSLisandro Dalcin   PetscBool      wrap = (bd == DM_BOUNDARY_PERIODIC || bd == DM_BOUNDARY_TWIST) ? PETSC_TRUE : PETSC_FALSE;
686fdbf62faSLisandro Dalcin   PetscMPIInt    rank;
687fdbf62faSLisandro Dalcin   PetscErrorCode ierr;
688fdbf62faSLisandro Dalcin 
689fdbf62faSLisandro Dalcin   PetscFunctionBegin;
6909318fe57SMatthew G. Knepley   PetscValidPointer(dm,1);
691fdbf62faSLisandro Dalcin 
6929318fe57SMatthew G. Knepley   ierr = DMSetDimension(dm,1);CHKERRQ(ierr);
6939318fe57SMatthew G. Knepley   ierr = DMCreateLabel(dm,"marker");CHKERRQ(ierr);
6949318fe57SMatthew G. Knepley   ierr = DMCreateLabel(dm,"Face Sets");CHKERRQ(ierr);
695fdbf62faSLisandro Dalcin 
6969318fe57SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm),&rank);CHKERRMPI(ierr);
697dd400576SPatrick Sanan   if (rank == 0) numCells = segments;
698dd400576SPatrick Sanan   if (rank == 0) numVerts = segments + (wrap ? 0 : 1);
699fdbf62faSLisandro Dalcin 
700fdbf62faSLisandro Dalcin   numPoints[0] = numVerts ; numPoints[1] = numCells;
701fdbf62faSLisandro Dalcin   ierr = PetscMalloc4(numCells+numVerts,&coneSize,numCells*2,&cones,numCells+numVerts,&coneOrientations,numVerts,&vertexCoords);CHKERRQ(ierr);
702580bdb30SBarry Smith   ierr = PetscArrayzero(coneOrientations,numCells+numVerts);CHKERRQ(ierr);
703fdbf62faSLisandro Dalcin   for (i = 0; i < numCells; ++i) { coneSize[i] = 2; }
704fdbf62faSLisandro Dalcin   for (i = 0; i < numVerts; ++i) { coneSize[numCells+i] = 0; }
705fdbf62faSLisandro Dalcin   for (i = 0; i < numCells; ++i) { cones[2*i] = numCells + i%numVerts; cones[2*i+1] = numCells + (i+1)%numVerts; }
706fdbf62faSLisandro Dalcin   for (i = 0; i < numVerts; ++i) { vertexCoords[i] = lower + (upper-lower)*((PetscReal)i/(PetscReal)numCells); }
7079318fe57SMatthew G. Knepley   ierr = DMPlexCreateFromDAG(dm,1,numPoints,coneSize,cones,coneOrientations,vertexCoords);CHKERRQ(ierr);
708fdbf62faSLisandro Dalcin   ierr = PetscFree4(coneSize,cones,coneOrientations,vertexCoords);CHKERRQ(ierr);
709fdbf62faSLisandro Dalcin 
7109318fe57SMatthew G. Knepley   ierr = PetscOptionsGetBool(((PetscObject)dm)->options,((PetscObject)dm)->prefix,"-dm_plex_separate_marker",&markerSeparate,NULL);CHKERRQ(ierr);
711fdbf62faSLisandro Dalcin   if (markerSeparate) { markerLeft = faceMarkerLeft; markerRight = faceMarkerRight;}
712dd400576SPatrick Sanan   if (!wrap && rank == 0) {
7139318fe57SMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm,1,&fStart,&fEnd);CHKERRQ(ierr);
7149318fe57SMatthew G. Knepley     ierr = DMSetLabelValue(dm,"marker",fStart,markerLeft);CHKERRQ(ierr);
7159318fe57SMatthew G. Knepley     ierr = DMSetLabelValue(dm,"marker",fEnd-1,markerRight);CHKERRQ(ierr);
7169318fe57SMatthew G. Knepley     ierr = DMSetLabelValue(dm,"Face Sets",fStart,faceMarkerLeft);CHKERRQ(ierr);
7179318fe57SMatthew G. Knepley     ierr = DMSetLabelValue(dm,"Face Sets",fEnd-1,faceMarkerRight);CHKERRQ(ierr);
718fdbf62faSLisandro Dalcin   }
719fdbf62faSLisandro Dalcin   if (wrap) {
720fdbf62faSLisandro Dalcin     L       = upper - lower;
721fdbf62faSLisandro Dalcin     maxCell = (PetscReal)1.1*(L/(PetscReal)PetscMax(1,segments));
7229318fe57SMatthew G. Knepley     ierr = DMSetPeriodicity(dm,PETSC_TRUE,&maxCell,&L,&bd);CHKERRQ(ierr);
723fdbf62faSLisandro Dalcin   }
7249318fe57SMatthew G. Knepley   ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE);CHKERRQ(ierr);
725fdbf62faSLisandro Dalcin   PetscFunctionReturn(0);
726fdbf62faSLisandro Dalcin }
727fdbf62faSLisandro Dalcin 
7289318fe57SMatthew 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)
729d6218766SMatthew G. Knepley {
7309318fe57SMatthew G. Knepley   DM             boundary, vol;
731768d5fceSMatthew G. Knepley   PetscInt       i;
732d6218766SMatthew G. Knepley   PetscErrorCode ierr;
733d6218766SMatthew G. Knepley 
734d6218766SMatthew G. Knepley   PetscFunctionBegin;
7359318fe57SMatthew G. Knepley   PetscValidPointer(dm, 1);
7369318fe57SMatthew 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");
7379318fe57SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), &boundary);CHKERRQ(ierr);
738d6218766SMatthew G. Knepley   ierr = DMSetType(boundary, DMPLEX);CHKERRQ(ierr);
7399318fe57SMatthew G. Knepley   ierr = DMPlexCreateBoxSurfaceMesh_Internal(boundary, dim, faces, lower, upper, PETSC_FALSE);CHKERRQ(ierr);
7409318fe57SMatthew G. Knepley   ierr = DMPlexGenerate(boundary, NULL, interpolate, &vol);CHKERRQ(ierr);
7419318fe57SMatthew G. Knepley   ierr = DMPlexReplace_Static(dm, &vol);CHKERRQ(ierr);
742d6218766SMatthew G. Knepley   ierr = DMDestroy(&boundary);CHKERRQ(ierr);
743d6218766SMatthew G. Knepley   PetscFunctionReturn(0);
744d6218766SMatthew G. Knepley }
745d6218766SMatthew G. Knepley 
7463dfda0b1SToby Isaac static PetscErrorCode DMPlexCreateCubeMesh_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY, DMBoundaryType bdZ)
7473dfda0b1SToby Isaac {
748ed0e4b50SMatthew G. Knepley   DMLabel        cutLabel = NULL;
749f4eb4c5dSMatthew G. Knepley   PetscInt       markerTop      = 1, faceMarkerTop      = 1;
750f4eb4c5dSMatthew G. Knepley   PetscInt       markerBottom   = 1, faceMarkerBottom   = 1;
751f4eb4c5dSMatthew G. Knepley   PetscInt       markerFront    = 1, faceMarkerFront    = 1;
752f4eb4c5dSMatthew G. Knepley   PetscInt       markerBack     = 1, faceMarkerBack     = 1;
753f4eb4c5dSMatthew G. Knepley   PetscInt       markerRight    = 1, faceMarkerRight    = 1;
754f4eb4c5dSMatthew G. Knepley   PetscInt       markerLeft     = 1, faceMarkerLeft     = 1;
7553dfda0b1SToby Isaac   PetscInt       dim;
756d8211ee3SMatthew G. Knepley   PetscBool      markerSeparate = PETSC_FALSE, cutMarker = PETSC_FALSE;
7573dfda0b1SToby Isaac   PetscMPIInt    rank;
7583dfda0b1SToby Isaac   PetscErrorCode ierr;
7593dfda0b1SToby Isaac 
7603dfda0b1SToby Isaac   PetscFunctionBegin;
761f0226e14SMatthew G. Knepley   ierr = DMGetDimension(dm,&dim);CHKERRQ(ierr);
762ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRMPI(ierr);
76350ae33c3SToby Isaac   ierr = DMCreateLabel(dm,"marker");CHKERRQ(ierr);
76450ae33c3SToby Isaac   ierr = DMCreateLabel(dm,"Face Sets");CHKERRQ(ierr);
7654c67ea77SStefano Zampini   ierr = PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL);CHKERRQ(ierr);
766d8211ee3SMatthew G. Knepley   if (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ||
767d8211ee3SMatthew G. Knepley       bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ||
768d8211ee3SMatthew G. Knepley       bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST) {
7694c67ea77SStefano Zampini 
770d1c88043SMatthew G. Knepley     if (cutMarker) {ierr = DMCreateLabel(dm, "periodic_cut");CHKERRQ(ierr); ierr = DMGetLabel(dm, "periodic_cut", &cutLabel);CHKERRQ(ierr);}
771d8211ee3SMatthew G. Knepley   }
7723dfda0b1SToby Isaac   switch (dim) {
7733dfda0b1SToby Isaac   case 2:
774f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 3;
775f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
776f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 2;
777f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 4;
7783dfda0b1SToby Isaac     break;
7793dfda0b1SToby Isaac   case 3:
780f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
781f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 2;
782f4eb4c5dSMatthew G. Knepley     faceMarkerFront  = 3;
783f4eb4c5dSMatthew G. Knepley     faceMarkerBack   = 4;
784f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 5;
785f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 6;
7863dfda0b1SToby Isaac     break;
7873dfda0b1SToby Isaac   default:
78889c010cfSBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Dimension %D not supported",dim);
7893dfda0b1SToby Isaac   }
790c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);CHKERRQ(ierr);
791f4eb4c5dSMatthew G. Knepley   if (markerSeparate) {
792f4eb4c5dSMatthew G. Knepley     markerBottom = faceMarkerBottom;
793f4eb4c5dSMatthew G. Knepley     markerTop    = faceMarkerTop;
794f4eb4c5dSMatthew G. Knepley     markerFront  = faceMarkerFront;
795f4eb4c5dSMatthew G. Knepley     markerBack   = faceMarkerBack;
796f4eb4c5dSMatthew G. Knepley     markerRight  = faceMarkerRight;
797f4eb4c5dSMatthew G. Knepley     markerLeft   = faceMarkerLeft;
7983dfda0b1SToby Isaac   }
7993dfda0b1SToby Isaac   {
800dd400576SPatrick Sanan     const PetscInt numXEdges    = rank == 0 ? edges[0] : 0;
801dd400576SPatrick Sanan     const PetscInt numYEdges    = rank == 0 ? edges[1] : 0;
802dd400576SPatrick Sanan     const PetscInt numZEdges    = rank == 0 ? edges[2] : 0;
803dd400576SPatrick Sanan     const PetscInt numXVertices = rank == 0 ? (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ? edges[0] : edges[0]+1) : 0;
804dd400576SPatrick Sanan     const PetscInt numYVertices = rank == 0 ? (bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ? edges[1] : edges[1]+1) : 0;
805dd400576SPatrick Sanan     const PetscInt numZVertices = rank == 0 ? (bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST ? edges[2] : edges[2]+1) : 0;
8063dfda0b1SToby Isaac     const PetscInt numCells     = numXEdges*numYEdges*numZEdges;
8073dfda0b1SToby Isaac     const PetscInt numXFaces    = numYEdges*numZEdges;
8083dfda0b1SToby Isaac     const PetscInt numYFaces    = numXEdges*numZEdges;
8093dfda0b1SToby Isaac     const PetscInt numZFaces    = numXEdges*numYEdges;
8103dfda0b1SToby Isaac     const PetscInt numTotXFaces = numXVertices*numXFaces;
8113dfda0b1SToby Isaac     const PetscInt numTotYFaces = numYVertices*numYFaces;
8123dfda0b1SToby Isaac     const PetscInt numTotZFaces = numZVertices*numZFaces;
8133dfda0b1SToby Isaac     const PetscInt numFaces     = numTotXFaces + numTotYFaces + numTotZFaces;
8143dfda0b1SToby Isaac     const PetscInt numTotXEdges = numXEdges*numYVertices*numZVertices;
8153dfda0b1SToby Isaac     const PetscInt numTotYEdges = numYEdges*numXVertices*numZVertices;
8163dfda0b1SToby Isaac     const PetscInt numTotZEdges = numZEdges*numXVertices*numYVertices;
8173dfda0b1SToby Isaac     const PetscInt numVertices  = numXVertices*numYVertices*numZVertices;
8183dfda0b1SToby Isaac     const PetscInt numEdges     = numTotXEdges + numTotYEdges + numTotZEdges;
8193dfda0b1SToby Isaac     const PetscInt firstVertex  = (dim == 2) ? numFaces : numCells;
8203dfda0b1SToby Isaac     const PetscInt firstXFace   = (dim == 2) ? 0 : numCells + numVertices;
8213dfda0b1SToby Isaac     const PetscInt firstYFace   = firstXFace + numTotXFaces;
8223dfda0b1SToby Isaac     const PetscInt firstZFace   = firstYFace + numTotYFaces;
8233dfda0b1SToby Isaac     const PetscInt firstXEdge   = numCells + numFaces + numVertices;
8243dfda0b1SToby Isaac     const PetscInt firstYEdge   = firstXEdge + numTotXEdges;
8253dfda0b1SToby Isaac     const PetscInt firstZEdge   = firstYEdge + numTotYEdges;
8263dfda0b1SToby Isaac     Vec            coordinates;
8273dfda0b1SToby Isaac     PetscSection   coordSection;
8283dfda0b1SToby Isaac     PetscScalar   *coords;
8293dfda0b1SToby Isaac     PetscInt       coordSize;
8303dfda0b1SToby Isaac     PetscInt       v, vx, vy, vz;
8313dfda0b1SToby Isaac     PetscInt       c, f, fx, fy, fz, e, ex, ey, ez;
8323dfda0b1SToby Isaac 
8333dfda0b1SToby Isaac     ierr = DMPlexSetChart(dm, 0, numCells+numFaces+numEdges+numVertices);CHKERRQ(ierr);
8343dfda0b1SToby Isaac     for (c = 0; c < numCells; c++) {
8353dfda0b1SToby Isaac       ierr = DMPlexSetConeSize(dm, c, 6);CHKERRQ(ierr);
8363dfda0b1SToby Isaac     }
8373dfda0b1SToby Isaac     for (f = firstXFace; f < firstXFace+numFaces; ++f) {
8383dfda0b1SToby Isaac       ierr = DMPlexSetConeSize(dm, f, 4);CHKERRQ(ierr);
8393dfda0b1SToby Isaac     }
8403dfda0b1SToby Isaac     for (e = firstXEdge; e < firstXEdge+numEdges; ++e) {
8413dfda0b1SToby Isaac       ierr = DMPlexSetConeSize(dm, e, 2);CHKERRQ(ierr);
8423dfda0b1SToby Isaac     }
8433dfda0b1SToby Isaac     ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
8443dfda0b1SToby Isaac     /* Build cells */
8453dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
8463dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
8473dfda0b1SToby Isaac         for (fx = 0; fx < numXEdges; ++fx) {
8483dfda0b1SToby Isaac           PetscInt cell    = (fz*numYEdges + fy)*numXEdges + fx;
8493dfda0b1SToby Isaac           PetscInt faceB   = firstZFace + (fy*numXEdges+fx)*numZVertices +   fz;
8503dfda0b1SToby Isaac           PetscInt faceT   = firstZFace + (fy*numXEdges+fx)*numZVertices + ((fz+1)%numZVertices);
8513dfda0b1SToby Isaac           PetscInt faceF   = firstYFace + (fz*numXEdges+fx)*numYVertices +   fy;
8523dfda0b1SToby Isaac           PetscInt faceK   = firstYFace + (fz*numXEdges+fx)*numYVertices + ((fy+1)%numYVertices);
8533dfda0b1SToby Isaac           PetscInt faceL   = firstXFace + (fz*numYEdges+fy)*numXVertices +   fx;
8543dfda0b1SToby Isaac           PetscInt faceR   = firstXFace + (fz*numYEdges+fy)*numXVertices + ((fx+1)%numXVertices);
8553dfda0b1SToby Isaac                             /* B,  T,  F,  K,  R,  L */
856b5a892a1SMatthew G. Knepley           PetscInt ornt[6] = {-2,  0,  0, -3,  0, -2}; /* ??? */
85742206facSLisandro Dalcin           PetscInt cone[6];
8583dfda0b1SToby Isaac 
8593dfda0b1SToby Isaac           /* no boundary twisting in 3D */
8603dfda0b1SToby Isaac           cone[0] = faceB; cone[1] = faceT; cone[2] = faceF; cone[3] = faceK; cone[4] = faceR; cone[5] = faceL;
8613dfda0b1SToby Isaac           ierr    = DMPlexSetCone(dm, cell, cone);CHKERRQ(ierr);
8623dfda0b1SToby Isaac           ierr    = DMPlexSetConeOrientation(dm, cell, ornt);CHKERRQ(ierr);
8638a5b437dSMatthew G. Knepley           if (bdX != DM_BOUNDARY_NONE && fx == numXEdges-1 && cutLabel) {ierr = DMLabelSetValue(cutLabel, cell, 2);CHKERRQ(ierr);}
8648a5b437dSMatthew G. Knepley           if (bdY != DM_BOUNDARY_NONE && fy == numYEdges-1 && cutLabel) {ierr = DMLabelSetValue(cutLabel, cell, 2);CHKERRQ(ierr);}
8658a5b437dSMatthew G. Knepley           if (bdZ != DM_BOUNDARY_NONE && fz == numZEdges-1 && cutLabel) {ierr = DMLabelSetValue(cutLabel, cell, 2);CHKERRQ(ierr);}
8663dfda0b1SToby Isaac         }
8673dfda0b1SToby Isaac       }
8683dfda0b1SToby Isaac     }
8693dfda0b1SToby Isaac     /* Build x faces */
8703dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
8713dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
8723dfda0b1SToby Isaac         for (fx = 0; fx < numXVertices; ++fx) {
8733dfda0b1SToby Isaac           PetscInt face    = firstXFace + (fz*numYEdges+fy)     *numXVertices+fx;
8743dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy                   *numXVertices+fx)*numZEdges + fz;
8753dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (((fy+1)%numYVertices)*numXVertices+fx)*numZEdges + fz;
8763dfda0b1SToby Isaac           PetscInt edgeB   = firstYEdge + (fz                   *numXVertices+fx)*numYEdges + fy;
8773dfda0b1SToby Isaac           PetscInt edgeT   = firstYEdge + (((fz+1)%numZVertices)*numXVertices+fx)*numYEdges + fy;
878b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
8793dfda0b1SToby Isaac           PetscInt cone[4];
8803dfda0b1SToby Isaac 
8813dfda0b1SToby Isaac           if (dim == 3) {
8823dfda0b1SToby Isaac             /* markers */
8833dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
8843dfda0b1SToby Isaac               if (fx == numXVertices-1) {
885c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerRight);CHKERRQ(ierr);
886c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", face, markerRight);CHKERRQ(ierr);
8873dfda0b1SToby Isaac               }
8883dfda0b1SToby Isaac               else if (fx == 0) {
889c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerLeft);CHKERRQ(ierr);
890c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", face, markerLeft);CHKERRQ(ierr);
8913dfda0b1SToby Isaac               }
8923dfda0b1SToby Isaac             }
8933dfda0b1SToby Isaac           }
8943dfda0b1SToby Isaac           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
8953dfda0b1SToby Isaac           ierr    = DMPlexSetCone(dm, face, cone);CHKERRQ(ierr);
8963dfda0b1SToby Isaac           ierr    = DMPlexSetConeOrientation(dm, face, ornt);CHKERRQ(ierr);
8973dfda0b1SToby Isaac         }
8983dfda0b1SToby Isaac       }
8993dfda0b1SToby Isaac     }
9003dfda0b1SToby Isaac     /* Build y faces */
9013dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
90242206facSLisandro Dalcin       for (fx = 0; fx < numXEdges; ++fx) {
9033dfda0b1SToby Isaac         for (fy = 0; fy < numYVertices; ++fy) {
9043dfda0b1SToby Isaac           PetscInt face    = firstYFace + (fz*numXEdges+fx)*numYVertices + fy;
9053dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy*numXVertices+  fx)*numZEdges + fz;
9063dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (fy*numXVertices+((fx+1)%numXVertices))*numZEdges + fz;
9073dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz                   *numYVertices+fy)*numXEdges + fx;
9083dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (((fz+1)%numZVertices)*numYVertices+fy)*numXEdges + fx;
909b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
9103dfda0b1SToby Isaac           PetscInt cone[4];
9113dfda0b1SToby Isaac 
9123dfda0b1SToby Isaac           if (dim == 3) {
9133dfda0b1SToby Isaac             /* markers */
9143dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
9153dfda0b1SToby Isaac               if (fy == numYVertices-1) {
916c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerBack);CHKERRQ(ierr);
917c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", face, markerBack);CHKERRQ(ierr);
9183dfda0b1SToby Isaac               }
9193dfda0b1SToby Isaac               else if (fy == 0) {
920c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerFront);CHKERRQ(ierr);
921c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", face, markerFront);CHKERRQ(ierr);
9223dfda0b1SToby Isaac               }
9233dfda0b1SToby Isaac             }
9243dfda0b1SToby Isaac           }
9253dfda0b1SToby Isaac           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
9263dfda0b1SToby Isaac           ierr    = DMPlexSetCone(dm, face, cone);CHKERRQ(ierr);
9273dfda0b1SToby Isaac           ierr    = DMPlexSetConeOrientation(dm, face, ornt);CHKERRQ(ierr);
9283dfda0b1SToby Isaac         }
9293dfda0b1SToby Isaac       }
9303dfda0b1SToby Isaac     }
9313dfda0b1SToby Isaac     /* Build z faces */
9323dfda0b1SToby Isaac     for (fy = 0; fy < numYEdges; ++fy) {
9333dfda0b1SToby Isaac       for (fx = 0; fx < numXEdges; ++fx) {
9343dfda0b1SToby Isaac         for (fz = 0; fz < numZVertices; fz++) {
9353dfda0b1SToby Isaac           PetscInt face    = firstZFace + (fy*numXEdges+fx)*numZVertices + fz;
9363dfda0b1SToby Isaac           PetscInt edgeL   = firstYEdge + (fz*numXVertices+  fx)*numYEdges + fy;
9373dfda0b1SToby Isaac           PetscInt edgeR   = firstYEdge + (fz*numXVertices+((fx+1)%numXVertices))*numYEdges + fy;
9383dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz*numYVertices+  fy)*numXEdges + fx;
9393dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (fz*numYVertices+((fy+1)%numYVertices))*numXEdges + fx;
940b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
9413dfda0b1SToby Isaac           PetscInt cone[4];
9423dfda0b1SToby Isaac 
9433dfda0b1SToby Isaac           if (dim == 2) {
944b5a892a1SMatthew G. Knepley             if (bdX == DM_BOUNDARY_TWIST && fx == numXEdges-1) {edgeR += numYEdges-1-2*fy; ornt[1] = -1;}
9453dfda0b1SToby Isaac             if (bdY == DM_BOUNDARY_TWIST && fy == numYEdges-1) {edgeT += numXEdges-1-2*fx; ornt[2] =  0;}
9468a5b437dSMatthew G. Knepley             if (bdX != DM_BOUNDARY_NONE && fx == numXEdges-1 && cutLabel) {ierr = DMLabelSetValue(cutLabel, face, 2);CHKERRQ(ierr);}
9478a5b437dSMatthew G. Knepley             if (bdY != DM_BOUNDARY_NONE && fy == numYEdges-1 && cutLabel) {ierr = DMLabelSetValue(cutLabel, face, 2);CHKERRQ(ierr);}
948d1c88043SMatthew G. Knepley           } else {
9493dfda0b1SToby Isaac             /* markers */
9503dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
9513dfda0b1SToby Isaac               if (fz == numZVertices-1) {
952c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerTop);CHKERRQ(ierr);
953c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", face, markerTop);CHKERRQ(ierr);
9543dfda0b1SToby Isaac               }
9553dfda0b1SToby Isaac               else if (fz == 0) {
956c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerBottom);CHKERRQ(ierr);
957c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", face, markerBottom);CHKERRQ(ierr);
9583dfda0b1SToby Isaac               }
9593dfda0b1SToby Isaac             }
9603dfda0b1SToby Isaac           }
9613dfda0b1SToby Isaac           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
9623dfda0b1SToby Isaac           ierr    = DMPlexSetCone(dm, face, cone);CHKERRQ(ierr);
9633dfda0b1SToby Isaac           ierr    = DMPlexSetConeOrientation(dm, face, ornt);CHKERRQ(ierr);
9643dfda0b1SToby Isaac         }
9653dfda0b1SToby Isaac       }
9663dfda0b1SToby Isaac     }
9673dfda0b1SToby Isaac     /* Build Z edges*/
9683dfda0b1SToby Isaac     for (vy = 0; vy < numYVertices; vy++) {
9693dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
9703dfda0b1SToby Isaac         for (ez = 0; ez < numZEdges; ez++) {
9713dfda0b1SToby Isaac           const PetscInt edge    = firstZEdge  + (vy*numXVertices+vx)*numZEdges + ez;
9723dfda0b1SToby Isaac           const PetscInt vertexB = firstVertex + (ez                   *numYVertices+vy)*numXVertices + vx;
9733dfda0b1SToby Isaac           const PetscInt vertexT = firstVertex + (((ez+1)%numZVertices)*numYVertices+vy)*numXVertices + vx;
9743dfda0b1SToby Isaac           PetscInt       cone[2];
9753dfda0b1SToby Isaac 
9763dfda0b1SToby Isaac           if (dim == 3) {
9773dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
9783dfda0b1SToby Isaac               if (vx == numXVertices-1) {
979c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerRight);CHKERRQ(ierr);
9803dfda0b1SToby Isaac               }
9813dfda0b1SToby Isaac               else if (vx == 0) {
982c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerLeft);CHKERRQ(ierr);
9833dfda0b1SToby Isaac               }
9843dfda0b1SToby Isaac             }
9853dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
9863dfda0b1SToby Isaac               if (vy == numYVertices-1) {
987c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerBack);CHKERRQ(ierr);
9883dfda0b1SToby Isaac               }
9893dfda0b1SToby Isaac               else if (vy == 0) {
990c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerFront);CHKERRQ(ierr);
9913dfda0b1SToby Isaac               }
9923dfda0b1SToby Isaac             }
9933dfda0b1SToby Isaac           }
9943dfda0b1SToby Isaac           cone[0] = vertexB; cone[1] = vertexT;
9953dfda0b1SToby Isaac           ierr = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr);
9963dfda0b1SToby Isaac         }
9973dfda0b1SToby Isaac       }
9983dfda0b1SToby Isaac     }
9993dfda0b1SToby Isaac     /* Build Y edges*/
10003dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
10013dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
10023dfda0b1SToby Isaac         for (ey = 0; ey < numYEdges; ey++) {
10033dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdY == DM_BOUNDARY_TWIST && ey == numYEdges-1) ? (numXVertices-vx-1) : (vz*numYVertices+((ey+1)%numYVertices))*numXVertices + vx;
10043dfda0b1SToby Isaac           const PetscInt edge    = firstYEdge  + (vz*numXVertices+vx)*numYEdges + ey;
10053dfda0b1SToby Isaac           const PetscInt vertexF = firstVertex + (vz*numYVertices+ey)*numXVertices + vx;
10063dfda0b1SToby Isaac           const PetscInt vertexK = firstVertex + nextv;
10073dfda0b1SToby Isaac           PetscInt       cone[2];
10083dfda0b1SToby Isaac 
10093dfda0b1SToby Isaac           cone[0] = vertexF; cone[1] = vertexK;
10103dfda0b1SToby Isaac           ierr = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr);
10113dfda0b1SToby Isaac           if (dim == 2) {
10123dfda0b1SToby Isaac             if ((bdX != DM_BOUNDARY_PERIODIC) && (bdX != DM_BOUNDARY_TWIST)) {
10133dfda0b1SToby Isaac               if (vx == numXVertices-1) {
1014c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", edge, faceMarkerRight);CHKERRQ(ierr);
1015c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge,    markerRight);CHKERRQ(ierr);
1016c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", cone[0], markerRight);CHKERRQ(ierr);
10173dfda0b1SToby Isaac                 if (ey == numYEdges-1) {
1018c58f1c22SToby Isaac                   ierr = DMSetLabelValue(dm, "marker", cone[1], markerRight);CHKERRQ(ierr);
10193dfda0b1SToby Isaac                 }
1020d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
1021c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", edge, faceMarkerLeft);CHKERRQ(ierr);
1022c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge,    markerLeft);CHKERRQ(ierr);
1023c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", cone[0], markerLeft);CHKERRQ(ierr);
10243dfda0b1SToby Isaac                 if (ey == numYEdges-1) {
1025c58f1c22SToby Isaac                   ierr = DMSetLabelValue(dm, "marker", cone[1], markerLeft);CHKERRQ(ierr);
10263dfda0b1SToby Isaac                 }
10273dfda0b1SToby Isaac               }
1028d8211ee3SMatthew G. Knepley             } else {
10294c67ea77SStefano Zampini               if (vx == 0 && cutLabel) {
1030d1c88043SMatthew G. Knepley                 ierr = DMLabelSetValue(cutLabel, edge,    1);CHKERRQ(ierr);
1031d1c88043SMatthew G. Knepley                 ierr = DMLabelSetValue(cutLabel, cone[0], 1);CHKERRQ(ierr);
1032d8211ee3SMatthew G. Knepley                 if (ey == numYEdges-1) {
1033d1c88043SMatthew G. Knepley                   ierr = DMLabelSetValue(cutLabel, cone[1], 1);CHKERRQ(ierr);
10343dfda0b1SToby Isaac                 }
10353dfda0b1SToby Isaac               }
1036d8211ee3SMatthew G. Knepley             }
1037d8211ee3SMatthew G. Knepley           } else {
10383dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
10393dfda0b1SToby Isaac               if (vx == numXVertices-1) {
1040c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerRight);CHKERRQ(ierr);
1041d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
1042c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerLeft);CHKERRQ(ierr);
10433dfda0b1SToby Isaac               }
10443dfda0b1SToby Isaac             }
10453dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
10463dfda0b1SToby Isaac               if (vz == numZVertices-1) {
1047c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerTop);CHKERRQ(ierr);
1048d8211ee3SMatthew G. Knepley               } else if (vz == 0) {
1049c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerBottom);CHKERRQ(ierr);
10503dfda0b1SToby Isaac               }
10513dfda0b1SToby Isaac             }
10523dfda0b1SToby Isaac           }
10533dfda0b1SToby Isaac         }
10543dfda0b1SToby Isaac       }
10553dfda0b1SToby Isaac     }
10563dfda0b1SToby Isaac     /* Build X edges*/
10573dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
10583dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; vy++) {
10593dfda0b1SToby Isaac         for (ex = 0; ex < numXEdges; ex++) {
10603dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdX == DM_BOUNDARY_TWIST && ex == numXEdges-1) ? (numYVertices-vy-1)*numXVertices : (vz*numYVertices+vy)*numXVertices + (ex+1)%numXVertices;
10613dfda0b1SToby Isaac           const PetscInt edge    = firstXEdge  + (vz*numYVertices+vy)*numXEdges + ex;
10623dfda0b1SToby Isaac           const PetscInt vertexL = firstVertex + (vz*numYVertices+vy)*numXVertices + ex;
10633dfda0b1SToby Isaac           const PetscInt vertexR = firstVertex + nextv;
10643dfda0b1SToby Isaac           PetscInt       cone[2];
10653dfda0b1SToby Isaac 
10663dfda0b1SToby Isaac           cone[0] = vertexL; cone[1] = vertexR;
10673dfda0b1SToby Isaac           ierr = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr);
10683dfda0b1SToby Isaac           if (dim == 2) {
10693dfda0b1SToby Isaac             if ((bdY != DM_BOUNDARY_PERIODIC) && (bdY != DM_BOUNDARY_TWIST)) {
10703dfda0b1SToby Isaac               if (vy == numYVertices-1) {
1071c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", edge, faceMarkerTop);CHKERRQ(ierr);
1072c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge,    markerTop);CHKERRQ(ierr);
1073c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", cone[0], markerTop);CHKERRQ(ierr);
10743dfda0b1SToby Isaac                 if (ex == numXEdges-1) {
1075c58f1c22SToby Isaac                   ierr = DMSetLabelValue(dm, "marker", cone[1], markerTop);CHKERRQ(ierr);
10763dfda0b1SToby Isaac                 }
1077d8211ee3SMatthew G. Knepley               } else if (vy == 0) {
1078c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", edge, faceMarkerBottom);CHKERRQ(ierr);
1079c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge,    markerBottom);CHKERRQ(ierr);
1080c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", cone[0], markerBottom);CHKERRQ(ierr);
10813dfda0b1SToby Isaac                 if (ex == numXEdges-1) {
1082c58f1c22SToby Isaac                   ierr = DMSetLabelValue(dm, "marker", cone[1], markerBottom);CHKERRQ(ierr);
10833dfda0b1SToby Isaac                 }
10843dfda0b1SToby Isaac               }
1085d8211ee3SMatthew G. Knepley             } else {
10864c67ea77SStefano Zampini               if (vy == 0 && cutLabel) {
1087d1c88043SMatthew G. Knepley                 ierr = DMLabelSetValue(cutLabel, edge,    1);CHKERRQ(ierr);
1088d1c88043SMatthew G. Knepley                 ierr = DMLabelSetValue(cutLabel, cone[0], 1);CHKERRQ(ierr);
1089d8211ee3SMatthew G. Knepley                 if (ex == numXEdges-1) {
1090d1c88043SMatthew G. Knepley                   ierr = DMLabelSetValue(cutLabel, cone[1], 1);CHKERRQ(ierr);
10913dfda0b1SToby Isaac                 }
10923dfda0b1SToby Isaac               }
1093d8211ee3SMatthew G. Knepley             }
1094d8211ee3SMatthew G. Knepley           } else {
10953dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
10963dfda0b1SToby Isaac               if (vy == numYVertices-1) {
1097c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerBack);CHKERRQ(ierr);
10983dfda0b1SToby Isaac               }
10993dfda0b1SToby Isaac               else if (vy == 0) {
1100c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerFront);CHKERRQ(ierr);
11013dfda0b1SToby Isaac               }
11023dfda0b1SToby Isaac             }
11033dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
11043dfda0b1SToby Isaac               if (vz == numZVertices-1) {
1105c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerTop);CHKERRQ(ierr);
11063dfda0b1SToby Isaac               }
11073dfda0b1SToby Isaac               else if (vz == 0) {
1108c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerBottom);CHKERRQ(ierr);
11093dfda0b1SToby Isaac               }
11103dfda0b1SToby Isaac             }
11113dfda0b1SToby Isaac           }
11123dfda0b1SToby Isaac         }
11133dfda0b1SToby Isaac       }
11143dfda0b1SToby Isaac     }
11153dfda0b1SToby Isaac     ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
11163dfda0b1SToby Isaac     ierr = DMPlexStratify(dm);CHKERRQ(ierr);
11173dfda0b1SToby Isaac     /* Build coordinates */
11183dfda0b1SToby Isaac     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
11193dfda0b1SToby Isaac     ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
11203dfda0b1SToby Isaac     ierr = PetscSectionSetFieldComponents(coordSection, 0, dim);CHKERRQ(ierr);
11213dfda0b1SToby Isaac     ierr = PetscSectionSetChart(coordSection, firstVertex, firstVertex+numVertices);CHKERRQ(ierr);
11223dfda0b1SToby Isaac     for (v = firstVertex; v < firstVertex+numVertices; ++v) {
11233dfda0b1SToby Isaac       ierr = PetscSectionSetDof(coordSection, v, dim);CHKERRQ(ierr);
11243dfda0b1SToby Isaac       ierr = PetscSectionSetFieldDof(coordSection, v, 0, dim);CHKERRQ(ierr);
11253dfda0b1SToby Isaac     }
11263dfda0b1SToby Isaac     ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
11273dfda0b1SToby Isaac     ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
11288b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
1129da16285aSMichael Lange     ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
11303dfda0b1SToby Isaac     ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
11318b9ced59SLisandro Dalcin     ierr = VecSetBlockSize(coordinates, dim);CHKERRQ(ierr);
11323dfda0b1SToby Isaac     ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
11333dfda0b1SToby Isaac     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
11343dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; ++vz) {
11353dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; ++vy) {
11363dfda0b1SToby Isaac         for (vx = 0; vx < numXVertices; ++vx) {
11373dfda0b1SToby Isaac           coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+0] = lower[0] + ((upper[0] - lower[0])/numXEdges)*vx;
11383dfda0b1SToby Isaac           coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+1] = lower[1] + ((upper[1] - lower[1])/numYEdges)*vy;
11393dfda0b1SToby Isaac           if (dim == 3) {
11403dfda0b1SToby Isaac             coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+2] = lower[2] + ((upper[2] - lower[2])/numZEdges)*vz;
11413dfda0b1SToby Isaac           }
11423dfda0b1SToby Isaac         }
11433dfda0b1SToby Isaac       }
11443dfda0b1SToby Isaac     }
11453dfda0b1SToby Isaac     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
11463dfda0b1SToby Isaac     ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
11473dfda0b1SToby Isaac     ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
11483dfda0b1SToby Isaac   }
11493dfda0b1SToby Isaac   PetscFunctionReturn(0);
11503dfda0b1SToby Isaac }
11513dfda0b1SToby Isaac 
11529318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxMesh_Tensor_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
1153a6dfd86eSKarl Rupp {
11549318fe57SMatthew G. Knepley   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
11559318fe57SMatthew G. Knepley   PetscInt       fac[3] = {0, 0, 0}, d;
1156552f7358SJed Brown   PetscErrorCode ierr;
1157552f7358SJed Brown 
1158552f7358SJed Brown   PetscFunctionBegin;
11599318fe57SMatthew G. Knepley   PetscValidPointer(dm, 1);
11609318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
11619318fe57SMatthew G. Knepley   ierr = DMSetDimension(dm, dim);CHKERRQ(ierr);
11629318fe57SMatthew G. Knepley   for (d = 0; d < dim; ++d) {fac[d] = faces[d]; bdt[d] = periodicity[d];}
11639318fe57SMatthew G. Knepley   ierr = DMPlexCreateCubeMesh_Internal(dm, lower, upper, fac, bdt[0], bdt[1], bdt[2]);CHKERRQ(ierr);
1164768d5fceSMatthew G. Knepley   if (periodicity[0] == DM_BOUNDARY_PERIODIC || periodicity[0] == DM_BOUNDARY_TWIST ||
1165768d5fceSMatthew G. Knepley       periodicity[1] == DM_BOUNDARY_PERIODIC || periodicity[1] == DM_BOUNDARY_TWIST ||
1166768d5fceSMatthew G. Knepley       (dim > 2 && (periodicity[2] == DM_BOUNDARY_PERIODIC || periodicity[2] == DM_BOUNDARY_TWIST))) {
1167768d5fceSMatthew G. Knepley     PetscReal L[3];
1168768d5fceSMatthew G. Knepley     PetscReal maxCell[3];
1169552f7358SJed Brown 
11709318fe57SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
11719318fe57SMatthew G. Knepley       L[d]       = upper[d] - lower[d];
11729318fe57SMatthew G. Knepley       maxCell[d] = 1.1 * (L[d] / PetscMax(1, faces[d]));
1173768d5fceSMatthew G. Knepley     }
11749318fe57SMatthew G. Knepley     ierr = DMSetPeriodicity(dm, PETSC_TRUE, maxCell, L, periodicity);CHKERRQ(ierr);
1175768d5fceSMatthew G. Knepley   }
11769318fe57SMatthew G. Knepley   ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE);CHKERRQ(ierr);
11779318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
11789318fe57SMatthew G. Knepley }
11799318fe57SMatthew G. Knepley 
11809318fe57SMatthew 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)
11819318fe57SMatthew G. Knepley {
11829318fe57SMatthew G. Knepley   PetscErrorCode ierr;
11839318fe57SMatthew G. Knepley 
11849318fe57SMatthew G. Knepley   PetscFunctionBegin;
11859318fe57SMatthew G. Knepley   if (dim == 1)      {ierr = DMPlexCreateLineMesh_Internal(dm, faces[0], lower[0], upper[0], periodicity[0]);CHKERRQ(ierr);}
11869318fe57SMatthew G. Knepley   else if (simplex)  {ierr = DMPlexCreateBoxMesh_Simplex_Internal(dm, dim, faces, lower, upper, periodicity, interpolate);CHKERRQ(ierr);}
11879318fe57SMatthew G. Knepley   else               {ierr = DMPlexCreateBoxMesh_Tensor_Internal(dm, dim, faces, lower, upper, periodicity);CHKERRQ(ierr);}
11889318fe57SMatthew G. Knepley   if (!interpolate && dim > 1 && !simplex) {
1189768d5fceSMatthew G. Knepley     DM udm;
1190768d5fceSMatthew G. Knepley 
11919318fe57SMatthew G. Knepley     ierr = DMPlexUninterpolate(dm, &udm);CHKERRQ(ierr);
11929318fe57SMatthew G. Knepley     ierr = DMPlexCopyCoordinates(dm, udm);CHKERRQ(ierr);
11939318fe57SMatthew G. Knepley     ierr = DMPlexReplace_Static(dm, &udm);CHKERRQ(ierr);
1194768d5fceSMatthew G. Knepley   }
1195768d5fceSMatthew G. Knepley   PetscFunctionReturn(0);
1196c8c68bd8SToby Isaac }
1197c8c68bd8SToby Isaac 
1198768d5fceSMatthew G. Knepley /*@C
1199768d5fceSMatthew G. Knepley   DMPlexCreateBoxMesh - Creates a mesh on the tensor product of unit intervals (box) using simplices or tensor cells (hexahedra).
1200768d5fceSMatthew G. Knepley 
1201d083f849SBarry Smith   Collective
1202768d5fceSMatthew G. Knepley 
1203768d5fceSMatthew G. Knepley   Input Parameters:
1204768d5fceSMatthew G. Knepley + comm        - The communicator for the DM object
1205768d5fceSMatthew G. Knepley . dim         - The spatial dimension
1206768d5fceSMatthew G. Knepley . simplex     - PETSC_TRUE for simplices, PETSC_FALSE for tensor cells
1207fdbf62faSLisandro Dalcin . faces       - Number of faces per dimension, or NULL for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
1208768d5fceSMatthew G. Knepley . lower       - The lower left corner, or NULL for (0, 0, 0)
1209768d5fceSMatthew G. Knepley . upper       - The upper right corner, or NULL for (1, 1, 1)
1210fdbf62faSLisandro Dalcin . periodicity - The boundary type for the X,Y,Z direction, or NULL for DM_BOUNDARY_NONE
1211768d5fceSMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
1212768d5fceSMatthew G. Knepley 
1213768d5fceSMatthew G. Knepley   Output Parameter:
1214768d5fceSMatthew G. Knepley . dm  - The DM object
1215768d5fceSMatthew G. Knepley 
12169318fe57SMatthew G. Knepley   Note: If you want to customize this mesh using options, you just need to
12179318fe57SMatthew G. Knepley $  DMCreate(comm, &dm);
12189318fe57SMatthew G. Knepley $  DMSetType(dm, DMPLEX);
12199318fe57SMatthew G. Knepley $  DMSetFromOptions(dm);
12209318fe57SMatthew G. Knepley and use the options on the DMSetFromOptions() page.
12211367e252SJed Brown 
12221367e252SJed Brown   Here is the numbering returned for 2 faces in each direction for tensor cells:
1223768d5fceSMatthew G. Knepley $ 10---17---11---18----12
1224768d5fceSMatthew G. Knepley $  |         |         |
1225768d5fceSMatthew G. Knepley $  |         |         |
1226768d5fceSMatthew G. Knepley $ 20    2   22    3    24
1227768d5fceSMatthew G. Knepley $  |         |         |
1228768d5fceSMatthew G. Knepley $  |         |         |
1229768d5fceSMatthew G. Knepley $  7---15----8---16----9
1230768d5fceSMatthew G. Knepley $  |         |         |
1231768d5fceSMatthew G. Knepley $  |         |         |
1232768d5fceSMatthew G. Knepley $ 19    0   21    1   23
1233768d5fceSMatthew G. Knepley $  |         |         |
1234768d5fceSMatthew G. Knepley $  |         |         |
1235768d5fceSMatthew G. Knepley $  4---13----5---14----6
1236768d5fceSMatthew G. Knepley 
1237768d5fceSMatthew G. Knepley and for simplicial cells
1238768d5fceSMatthew G. Knepley 
1239768d5fceSMatthew G. Knepley $ 14----8---15----9----16
1240768d5fceSMatthew G. Knepley $  |\     5  |\      7 |
1241768d5fceSMatthew G. Knepley $  | \       | \       |
1242768d5fceSMatthew G. Knepley $ 13   2    14    3    15
1243768d5fceSMatthew G. Knepley $  | 4   \   | 6   \   |
1244768d5fceSMatthew G. Knepley $  |       \ |       \ |
1245768d5fceSMatthew G. Knepley $ 11----6---12----7----13
1246768d5fceSMatthew G. Knepley $  |\        |\        |
1247768d5fceSMatthew G. Knepley $  | \    1  | \     3 |
1248768d5fceSMatthew G. Knepley $ 10   0    11    1    12
1249768d5fceSMatthew G. Knepley $  | 0   \   | 2   \   |
1250768d5fceSMatthew G. Knepley $  |       \ |       \ |
1251768d5fceSMatthew G. Knepley $  8----4----9----5----10
1252768d5fceSMatthew G. Knepley 
1253768d5fceSMatthew G. Knepley   Level: beginner
1254768d5fceSMatthew G. Knepley 
12559318fe57SMatthew G. Knepley .seealso: DMSetFromOptions(), DMPlexCreateFromFile(), DMPlexCreateHexCylinderMesh(), DMSetType(), DMCreate()
1256768d5fceSMatthew G. Knepley @*/
1257768d5fceSMatthew 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)
1258552f7358SJed Brown {
12599318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
1260fdbf62faSLisandro Dalcin   PetscReal      low[3] = {0, 0, 0};
1261fdbf62faSLisandro Dalcin   PetscReal      upp[3] = {1, 1, 1};
1262fdbf62faSLisandro Dalcin   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
1263768d5fceSMatthew G. Knepley   PetscErrorCode ierr;
1264552f7358SJed Brown 
1265768d5fceSMatthew G. Knepley   PetscFunctionBegin;
12669318fe57SMatthew G. Knepley   ierr = DMCreate(comm,dm);CHKERRQ(ierr);
12679318fe57SMatthew G. Knepley   ierr = DMSetType(*dm,DMPLEX);CHKERRQ(ierr);
12689318fe57SMatthew G. Knepley   ierr = DMPlexCreateBoxMesh_Internal(*dm, dim, simplex, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt, interpolate);CHKERRQ(ierr);
12699318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
12709318fe57SMatthew G. Knepley }
1271fdbf62faSLisandro Dalcin 
1272d410b0cfSMatthew G. Knepley static PetscErrorCode DMPlexCreateWedgeBoxMesh_Internal(DM dm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
12739318fe57SMatthew G. Knepley {
12749318fe57SMatthew G. Knepley   DM             bdm, vol;
12759318fe57SMatthew G. Knepley   PetscInt       i;
12769318fe57SMatthew G. Knepley   PetscErrorCode ierr;
12779318fe57SMatthew G. Knepley 
12789318fe57SMatthew G. Knepley   PetscFunctionBegin;
12799318fe57SMatthew 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");
12809318fe57SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), &bdm);CHKERRQ(ierr);
12819318fe57SMatthew G. Knepley   ierr = DMSetType(bdm, DMPLEX);CHKERRQ(ierr);
12829318fe57SMatthew G. Knepley   ierr = DMSetDimension(bdm, 2);CHKERRQ(ierr);
1283d410b0cfSMatthew G. Knepley   ierr = DMPlexCreateBoxMesh_Simplex_Internal(bdm, 2, faces, lower, upper, periodicity, PETSC_TRUE);CHKERRQ(ierr);
1284d410b0cfSMatthew G. Knepley   ierr = DMPlexExtrude(bdm, faces[2], upper[2] - lower[2], PETSC_TRUE, PETSC_FALSE, NULL, NULL, &vol);CHKERRQ(ierr);
12859318fe57SMatthew G. Knepley   ierr = DMDestroy(&bdm);CHKERRQ(ierr);
12869318fe57SMatthew G. Knepley   ierr = DMPlexReplace_Static(dm, &vol);CHKERRQ(ierr);
12879318fe57SMatthew G. Knepley   if (lower[2] != 0.0) {
12889318fe57SMatthew G. Knepley     Vec          v;
12899318fe57SMatthew G. Knepley     PetscScalar *x;
12909318fe57SMatthew G. Knepley     PetscInt     cDim, n;
12919318fe57SMatthew G. Knepley 
12929318fe57SMatthew G. Knepley     ierr = DMGetCoordinatesLocal(dm, &v);CHKERRQ(ierr);
12939318fe57SMatthew G. Knepley     ierr = VecGetBlockSize(v, &cDim);CHKERRQ(ierr);
12949318fe57SMatthew G. Knepley     ierr = VecGetLocalSize(v, &n);CHKERRQ(ierr);
12959318fe57SMatthew G. Knepley     ierr = VecGetArray(v, &x);CHKERRQ(ierr);
12969318fe57SMatthew G. Knepley     x   += cDim;
12979318fe57SMatthew G. Knepley     for (i = 0; i < n; i += cDim) x[i] += lower[2];
12989318fe57SMatthew G. Knepley     ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
12999318fe57SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(dm, v);CHKERRQ(ierr);
13009318fe57SMatthew 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 
1323d410b0cfSMatthew G. Knepley .seealso: DMPlexCreateHexCylinderMesh(), DMPlexCreateWedgeCylinderMesh(), DMExtrude(), 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 {
13279318fe57SMatthew 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;
13349318fe57SMatthew G. Knepley   ierr = DMCreate(comm,dm);CHKERRQ(ierr);
13359318fe57SMatthew G. Knepley   ierr = DMSetType(*dm,DMPLEX);CHKERRQ(ierr);
1336d410b0cfSMatthew G. Knepley   ierr = DMPlexCreateWedgeBoxMesh_Internal(*dm, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt);CHKERRQ(ierr);
1337d410b0cfSMatthew G. Knepley   if (!interpolate) {
1338d410b0cfSMatthew G. Knepley     DM udm;
133900dabe28SStefano Zampini 
1340d410b0cfSMatthew G. Knepley     ierr = DMPlexUninterpolate(*dm, &udm);CHKERRQ(ierr);
1341d410b0cfSMatthew G. Knepley     ierr = DMPlexReplace_Static(*dm, &udm);CHKERRQ(ierr);
134200dabe28SStefano Zampini   }
134300dabe28SStefano Zampini   PetscFunctionReturn(0);
134400dabe28SStefano Zampini }
134500dabe28SStefano Zampini 
1346a9074c1eSMatthew G. Knepley /*@C
1347a9074c1eSMatthew G. Knepley   DMPlexSetOptionsPrefix - Sets the prefix used for searching for all DM options in the database.
1348a9074c1eSMatthew G. Knepley 
1349d083f849SBarry Smith   Logically Collective on dm
1350a9074c1eSMatthew G. Knepley 
1351a9074c1eSMatthew G. Knepley   Input Parameters:
1352a9074c1eSMatthew G. Knepley + dm - the DM context
1353a9074c1eSMatthew G. Knepley - prefix - the prefix to prepend to all option names
1354a9074c1eSMatthew G. Knepley 
1355a9074c1eSMatthew G. Knepley   Notes:
1356a9074c1eSMatthew G. Knepley   A hyphen (-) must NOT be given at the beginning of the prefix name.
1357a9074c1eSMatthew G. Knepley   The first character of all runtime options is AUTOMATICALLY the hyphen.
1358a9074c1eSMatthew G. Knepley 
1359a9074c1eSMatthew G. Knepley   Level: advanced
1360a9074c1eSMatthew G. Knepley 
1361a9074c1eSMatthew G. Knepley .seealso: SNESSetFromOptions()
1362a9074c1eSMatthew G. Knepley @*/
1363a9074c1eSMatthew G. Knepley PetscErrorCode DMPlexSetOptionsPrefix(DM dm, const char prefix[])
1364a9074c1eSMatthew G. Knepley {
1365a9074c1eSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
1366a9074c1eSMatthew G. Knepley   PetscErrorCode ierr;
1367a9074c1eSMatthew G. Knepley 
1368a9074c1eSMatthew G. Knepley   PetscFunctionBegin;
1369a9074c1eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1370a9074c1eSMatthew G. Knepley   ierr = PetscObjectSetOptionsPrefix((PetscObject) dm, prefix);CHKERRQ(ierr);
1371a9074c1eSMatthew G. Knepley   ierr = PetscObjectSetOptionsPrefix((PetscObject) mesh->partitioner, prefix);CHKERRQ(ierr);
1372a9074c1eSMatthew G. Knepley   PetscFunctionReturn(0);
1373a9074c1eSMatthew G. Knepley }
1374a9074c1eSMatthew G. Knepley 
13759318fe57SMatthew G. Knepley /* Remap geometry to cylinder
137661a622f3SMatthew G. Knepley    TODO: This only works for a single refinement, then it is broken
137761a622f3SMatthew G. Knepley 
13789318fe57SMatthew G. Knepley      Interior square: Linear interpolation is correct
13799318fe57SMatthew G. Knepley      The other cells all have vertices on rays from the origin. We want to uniformly expand the spacing
13809318fe57SMatthew G. Knepley      such that the last vertex is on the unit circle. So the closest and farthest vertices are at distance
13810510c589SMatthew G. Knepley 
13829318fe57SMatthew G. Knepley        phi     = arctan(y/x)
13839318fe57SMatthew G. Knepley        d_close = sqrt(1/8 + 1/4 sin^2(phi))
13849318fe57SMatthew G. Knepley        d_far   = sqrt(1/2 + sin^2(phi))
13850510c589SMatthew G. Knepley 
13869318fe57SMatthew G. Knepley      so we remap them using
13870510c589SMatthew G. Knepley 
13889318fe57SMatthew G. Knepley        x_new = x_close + (x - x_close) (1 - d_close) / (d_far - d_close)
13899318fe57SMatthew G. Knepley        y_new = y_close + (y - y_close) (1 - d_close) / (d_far - d_close)
13900510c589SMatthew G. Knepley 
13919318fe57SMatthew G. Knepley      If pi/4 < phi < 3pi/4 or -3pi/4 < phi < -pi/4, then we switch x and y.
13929318fe57SMatthew G. Knepley */
13939318fe57SMatthew G. Knepley static void snapToCylinder(PetscInt dim, PetscInt Nf, PetscInt NfAux,
13949318fe57SMatthew G. Knepley                            const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
13959318fe57SMatthew G. Knepley                            const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
13969318fe57SMatthew G. Knepley                            PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
13979318fe57SMatthew G. Knepley {
13989318fe57SMatthew G. Knepley   const PetscReal dis = 1.0/PetscSqrtReal(2.0);
13999318fe57SMatthew G. Knepley   const PetscReal ds2 = 0.5*dis;
140022cc497dSMatthew G. Knepley 
14019318fe57SMatthew G. Knepley   if ((PetscAbsScalar(u[0]) <= ds2) && (PetscAbsScalar(u[1]) <= ds2)) {
14029318fe57SMatthew G. Knepley     f0[0] = u[0];
14039318fe57SMatthew G. Knepley     f0[1] = u[1];
14049318fe57SMatthew G. Knepley   } else {
14059318fe57SMatthew G. Knepley     PetscReal phi, sinp, cosp, dc, df, x, y, xc, yc;
14060510c589SMatthew G. Knepley 
14079318fe57SMatthew G. Knepley     x    = PetscRealPart(u[0]);
14089318fe57SMatthew G. Knepley     y    = PetscRealPart(u[1]);
14099318fe57SMatthew G. Knepley     phi  = PetscAtan2Real(y, x);
14109318fe57SMatthew G. Knepley     sinp = PetscSinReal(phi);
14119318fe57SMatthew G. Knepley     cosp = PetscCosReal(phi);
14129318fe57SMatthew G. Knepley     if ((PetscAbsReal(phi) > PETSC_PI/4.0) && (PetscAbsReal(phi) < 3.0*PETSC_PI/4.0)) {
14139318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2/sinp);
14149318fe57SMatthew G. Knepley       df = PetscAbsReal(dis/sinp);
14159318fe57SMatthew G. Knepley       xc = ds2*x/PetscAbsReal(y);
14169318fe57SMatthew G. Knepley       yc = ds2*PetscSignReal(y);
14179318fe57SMatthew G. Knepley     } else {
14189318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2/cosp);
14199318fe57SMatthew G. Knepley       df = PetscAbsReal(dis/cosp);
14209318fe57SMatthew G. Knepley       xc = ds2*PetscSignReal(x);
14219318fe57SMatthew G. Knepley       yc = ds2*y/PetscAbsReal(x);
14229318fe57SMatthew G. Knepley     }
14239318fe57SMatthew G. Knepley     f0[0] = xc + (u[0] - xc)*(1.0 - dc)/(df - dc);
14249318fe57SMatthew G. Knepley     f0[1] = yc + (u[1] - yc)*(1.0 - dc)/(df - dc);
14259318fe57SMatthew G. Knepley   }
14269318fe57SMatthew G. Knepley   f0[2] = u[2];
14279318fe57SMatthew G. Knepley }
14280510c589SMatthew G. Knepley 
14299318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateHexCylinderMesh_Internal(DM dm, DMBoundaryType periodicZ)
14300510c589SMatthew G. Knepley {
14310510c589SMatthew G. Knepley   const PetscInt dim = 3;
14329318fe57SMatthew G. Knepley   PetscInt       numCells, numVertices;
1433d8c47e87SMatthew G. Knepley   PetscMPIInt    rank;
14340510c589SMatthew G. Knepley   PetscErrorCode ierr;
14350510c589SMatthew G. Knepley 
14360510c589SMatthew G. Knepley   PetscFunctionBegin;
14379318fe57SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
14389318fe57SMatthew G. Knepley   ierr = DMSetDimension(dm, dim);CHKERRQ(ierr);
14390510c589SMatthew G. Knepley   /* Create topology */
14400510c589SMatthew G. Knepley   {
14410510c589SMatthew G. Knepley     PetscInt cone[8], c;
14420510c589SMatthew G. Knepley 
1443dd400576SPatrick Sanan     numCells    = rank == 0 ?  5 : 0;
1444dd400576SPatrick Sanan     numVertices = rank == 0 ? 16 : 0;
1445006a8963SMatthew G. Knepley     if (periodicZ == DM_BOUNDARY_PERIODIC) {
1446ae8bcbbbSMatthew G. Knepley       numCells   *= 3;
1447dd400576SPatrick Sanan       numVertices = rank == 0 ? 24 : 0;
1448006a8963SMatthew G. Knepley     }
14499318fe57SMatthew G. Knepley     ierr = DMPlexSetChart(dm, 0, numCells+numVertices);CHKERRQ(ierr);
14509318fe57SMatthew G. Knepley     for (c = 0; c < numCells; c++) {ierr = DMPlexSetConeSize(dm, c, 8);CHKERRQ(ierr);}
14519318fe57SMatthew G. Knepley     ierr = DMSetUp(dm);CHKERRQ(ierr);
1452dd400576SPatrick Sanan     if (rank == 0) {
1453006a8963SMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
1454ae8bcbbbSMatthew G. Knepley         cone[0] = 15; cone[1] = 18; cone[2] = 17; cone[3] = 16;
1455ae8bcbbbSMatthew G. Knepley         cone[4] = 31; cone[5] = 32; cone[6] = 33; cone[7] = 34;
14569318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 0, cone);CHKERRQ(ierr);
1457ae8bcbbbSMatthew G. Knepley         cone[0] = 16; cone[1] = 17; cone[2] = 24; cone[3] = 23;
1458ae8bcbbbSMatthew G. Knepley         cone[4] = 32; cone[5] = 36; cone[6] = 37; cone[7] = 33; /* 22 25 26 21 */
14599318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 1, cone);CHKERRQ(ierr);
1460ae8bcbbbSMatthew G. Knepley         cone[0] = 18; cone[1] = 27; cone[2] = 24; cone[3] = 17;
1461ae8bcbbbSMatthew G. Knepley         cone[4] = 34; cone[5] = 33; cone[6] = 37; cone[7] = 38;
14629318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 2, cone);CHKERRQ(ierr);
1463ae8bcbbbSMatthew G. Knepley         cone[0] = 29; cone[1] = 27; cone[2] = 18; cone[3] = 15;
1464ae8bcbbbSMatthew G. Knepley         cone[4] = 35; cone[5] = 31; cone[6] = 34; cone[7] = 38;
14659318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 3, cone);CHKERRQ(ierr);
1466ae8bcbbbSMatthew G. Knepley         cone[0] = 29; cone[1] = 15; cone[2] = 16; cone[3] = 23;
1467ae8bcbbbSMatthew G. Knepley         cone[4] = 35; cone[5] = 36; cone[6] = 32; cone[7] = 31;
14689318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 4, cone);CHKERRQ(ierr);
1469006a8963SMatthew G. Knepley 
1470ae8bcbbbSMatthew G. Knepley         cone[0] = 31; cone[1] = 34; cone[2] = 33; cone[3] = 32;
1471ae8bcbbbSMatthew G. Knepley         cone[4] = 19; cone[5] = 22; cone[6] = 21; cone[7] = 20;
14729318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 5, cone);CHKERRQ(ierr);
1473ae8bcbbbSMatthew G. Knepley         cone[0] = 32; cone[1] = 33; cone[2] = 37; cone[3] = 36;
1474ae8bcbbbSMatthew G. Knepley         cone[4] = 22; cone[5] = 25; cone[6] = 26; cone[7] = 21;
14759318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 6, cone);CHKERRQ(ierr);
1476ae8bcbbbSMatthew G. Knepley         cone[0] = 34; cone[1] = 38; cone[2] = 37; cone[3] = 33;
1477ae8bcbbbSMatthew G. Knepley         cone[4] = 20; cone[5] = 21; cone[6] = 26; cone[7] = 28;
14789318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 7, cone);CHKERRQ(ierr);
1479ae8bcbbbSMatthew G. Knepley         cone[0] = 35; cone[1] = 38; cone[2] = 34; cone[3] = 31;
1480ae8bcbbbSMatthew G. Knepley         cone[4] = 30; cone[5] = 19; cone[6] = 20; cone[7] = 28;
14819318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 8, cone);CHKERRQ(ierr);
1482ae8bcbbbSMatthew G. Knepley         cone[0] = 35; cone[1] = 31; cone[2] = 32; cone[3] = 36;
1483ae8bcbbbSMatthew G. Knepley         cone[4] = 30; cone[5] = 25; cone[6] = 22; cone[7] = 19;
14849318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 9, cone);CHKERRQ(ierr);
1485ae8bcbbbSMatthew G. Knepley 
1486ae8bcbbbSMatthew G. Knepley         cone[0] = 19; cone[1] = 20; cone[2] = 21; cone[3] = 22;
1487ae8bcbbbSMatthew G. Knepley         cone[4] = 15; cone[5] = 16; cone[6] = 17; cone[7] = 18;
14889318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 10, cone);CHKERRQ(ierr);
1489ae8bcbbbSMatthew G. Knepley         cone[0] = 22; cone[1] = 21; cone[2] = 26; cone[3] = 25;
1490ae8bcbbbSMatthew G. Knepley         cone[4] = 16; cone[5] = 23; cone[6] = 24; cone[7] = 17;
14919318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 11, cone);CHKERRQ(ierr);
1492ae8bcbbbSMatthew G. Knepley         cone[0] = 20; cone[1] = 28; cone[2] = 26; cone[3] = 21;
1493ae8bcbbbSMatthew G. Knepley         cone[4] = 18; cone[5] = 17; cone[6] = 24; cone[7] = 27;
14949318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 12, cone);CHKERRQ(ierr);
1495ae8bcbbbSMatthew G. Knepley         cone[0] = 30; cone[1] = 28; cone[2] = 20; cone[3] = 19;
1496ae8bcbbbSMatthew G. Knepley         cone[4] = 29; cone[5] = 15; cone[6] = 18; cone[7] = 27;
14979318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 13, cone);CHKERRQ(ierr);
1498ae8bcbbbSMatthew G. Knepley         cone[0] = 30; cone[1] = 19; cone[2] = 22; cone[3] = 25;
1499ae8bcbbbSMatthew G. Knepley         cone[4] = 29; cone[5] = 23; cone[6] = 16; cone[7] = 15;
15009318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 14, cone);CHKERRQ(ierr);
1501006a8963SMatthew G. Knepley       } else {
150210c6f908SMatthew G. Knepley         cone[0] =  5; cone[1] =  8; cone[2] =  7; cone[3] =  6;
150310c6f908SMatthew G. Knepley         cone[4] =  9; cone[5] = 12; cone[6] = 11; cone[7] = 10;
15049318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 0, cone);CHKERRQ(ierr);
150510c6f908SMatthew G. Knepley         cone[0] =  6; cone[1] =  7; cone[2] = 14; cone[3] = 13;
150610c6f908SMatthew G. Knepley         cone[4] = 12; cone[5] = 15; cone[6] = 16; cone[7] = 11;
15079318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 1, cone);CHKERRQ(ierr);
150810c6f908SMatthew G. Knepley         cone[0] =  8; cone[1] = 17; cone[2] = 14; cone[3] =  7;
150910c6f908SMatthew G. Knepley         cone[4] = 10; cone[5] = 11; cone[6] = 16; cone[7] = 18;
15109318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 2, cone);CHKERRQ(ierr);
151110c6f908SMatthew G. Knepley         cone[0] = 19; cone[1] = 17; cone[2] =  8; cone[3] =  5;
151210c6f908SMatthew G. Knepley         cone[4] = 20; cone[5] =  9; cone[6] = 10; cone[7] = 18;
15139318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 3, cone);CHKERRQ(ierr);
151410c6f908SMatthew G. Knepley         cone[0] = 19; cone[1] =  5; cone[2] =  6; cone[3] = 13;
151510c6f908SMatthew G. Knepley         cone[4] = 20; cone[5] = 15; cone[6] = 12; cone[7] =  9;
15169318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 4, cone);CHKERRQ(ierr);
1517006a8963SMatthew G. Knepley       }
1518d8c47e87SMatthew G. Knepley     }
15199318fe57SMatthew G. Knepley     ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
15209318fe57SMatthew G. Knepley     ierr = DMPlexStratify(dm);CHKERRQ(ierr);
15210510c589SMatthew G. Knepley   }
1522dbc1dc17SMatthew G. Knepley   /* Create cube geometry */
15230510c589SMatthew G. Knepley   {
15240510c589SMatthew G. Knepley     Vec             coordinates;
15250510c589SMatthew G. Knepley     PetscSection    coordSection;
15260510c589SMatthew G. Knepley     PetscScalar    *coords;
15270510c589SMatthew G. Knepley     PetscInt        coordSize, v;
15280510c589SMatthew G. Knepley     const PetscReal dis = 1.0/PetscSqrtReal(2.0);
15290510c589SMatthew G. Knepley     const PetscReal ds2 = dis/2.0;
15300510c589SMatthew G. Knepley 
15310510c589SMatthew G. Knepley     /* Build coordinates */
15329318fe57SMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
15330510c589SMatthew G. Knepley     ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
15340510c589SMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(coordSection, 0, dim);CHKERRQ(ierr);
15350510c589SMatthew G. Knepley     ierr = PetscSectionSetChart(coordSection, numCells, numCells+numVertices);CHKERRQ(ierr);
15360510c589SMatthew G. Knepley     for (v = numCells; v < numCells+numVertices; ++v) {
15370510c589SMatthew G. Knepley       ierr = PetscSectionSetDof(coordSection, v, dim);CHKERRQ(ierr);
15380510c589SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(coordSection, v, 0, dim);CHKERRQ(ierr);
15390510c589SMatthew G. Knepley     }
15400510c589SMatthew G. Knepley     ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
15410510c589SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
15420510c589SMatthew G. Knepley     ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
15430510c589SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
15440510c589SMatthew G. Knepley     ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
15450510c589SMatthew G. Knepley     ierr = VecSetBlockSize(coordinates, dim);CHKERRQ(ierr);
15460510c589SMatthew G. Knepley     ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
15470510c589SMatthew G. Knepley     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
1548dd400576SPatrick Sanan     if (rank == 0) {
15490510c589SMatthew G. Knepley       coords[0*dim+0] = -ds2; coords[0*dim+1] = -ds2; coords[0*dim+2] = 0.0;
15500510c589SMatthew G. Knepley       coords[1*dim+0] =  ds2; coords[1*dim+1] = -ds2; coords[1*dim+2] = 0.0;
15510510c589SMatthew G. Knepley       coords[2*dim+0] =  ds2; coords[2*dim+1] =  ds2; coords[2*dim+2] = 0.0;
15520510c589SMatthew G. Knepley       coords[3*dim+0] = -ds2; coords[3*dim+1] =  ds2; coords[3*dim+2] = 0.0;
15530510c589SMatthew G. Knepley       coords[4*dim+0] = -ds2; coords[4*dim+1] = -ds2; coords[4*dim+2] = 1.0;
15540510c589SMatthew G. Knepley       coords[5*dim+0] = -ds2; coords[5*dim+1] =  ds2; coords[5*dim+2] = 1.0;
15550510c589SMatthew G. Knepley       coords[6*dim+0] =  ds2; coords[6*dim+1] =  ds2; coords[6*dim+2] = 1.0;
15560510c589SMatthew G. Knepley       coords[7*dim+0] =  ds2; coords[7*dim+1] = -ds2; coords[7*dim+2] = 1.0;
15570510c589SMatthew G. Knepley       coords[ 8*dim+0] =  dis; coords[ 8*dim+1] = -dis; coords[ 8*dim+2] = 0.0;
15580510c589SMatthew G. Knepley       coords[ 9*dim+0] =  dis; coords[ 9*dim+1] =  dis; coords[ 9*dim+2] = 0.0;
15590510c589SMatthew G. Knepley       coords[10*dim+0] =  dis; coords[10*dim+1] = -dis; coords[10*dim+2] = 1.0;
15600510c589SMatthew G. Knepley       coords[11*dim+0] =  dis; coords[11*dim+1] =  dis; coords[11*dim+2] = 1.0;
15610510c589SMatthew G. Knepley       coords[12*dim+0] = -dis; coords[12*dim+1] =  dis; coords[12*dim+2] = 0.0;
15620510c589SMatthew G. Knepley       coords[13*dim+0] = -dis; coords[13*dim+1] =  dis; coords[13*dim+2] = 1.0;
15630510c589SMatthew G. Knepley       coords[14*dim+0] = -dis; coords[14*dim+1] = -dis; coords[14*dim+2] = 0.0;
15640510c589SMatthew G. Knepley       coords[15*dim+0] = -dis; coords[15*dim+1] = -dis; coords[15*dim+2] = 1.0;
1565ae8bcbbbSMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
1566ae8bcbbbSMatthew G. Knepley         /* 15 31 19 */ coords[16*dim+0] = -ds2; coords[16*dim+1] = -ds2; coords[16*dim+2] = 0.5;
1567ae8bcbbbSMatthew G. Knepley         /* 16 32 22 */ coords[17*dim+0] =  ds2; coords[17*dim+1] = -ds2; coords[17*dim+2] = 0.5;
1568ae8bcbbbSMatthew G. Knepley         /* 17 33 21 */ coords[18*dim+0] =  ds2; coords[18*dim+1] =  ds2; coords[18*dim+2] = 0.5;
1569ae8bcbbbSMatthew G. Knepley         /* 18 34 20 */ coords[19*dim+0] = -ds2; coords[19*dim+1] =  ds2; coords[19*dim+2] = 0.5;
1570ae8bcbbbSMatthew G. Knepley         /* 29 35 30 */ coords[20*dim+0] = -dis; coords[20*dim+1] = -dis; coords[20*dim+2] = 0.5;
1571ae8bcbbbSMatthew G. Knepley         /* 23 36 25 */ coords[21*dim+0] =  dis; coords[21*dim+1] = -dis; coords[21*dim+2] = 0.5;
1572ae8bcbbbSMatthew G. Knepley         /* 24 37 26 */ coords[22*dim+0] =  dis; coords[22*dim+1] =  dis; coords[22*dim+2] = 0.5;
1573ae8bcbbbSMatthew G. Knepley         /* 27 38 28 */ coords[23*dim+0] = -dis; coords[23*dim+1] =  dis; coords[23*dim+2] = 0.5;
1574ae8bcbbbSMatthew G. Knepley       }
1575d8c47e87SMatthew G. Knepley     }
15760510c589SMatthew G. Knepley     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
15779318fe57SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
15780510c589SMatthew G. Knepley     ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
15790510c589SMatthew G. Knepley   }
1580006a8963SMatthew G. Knepley   /* Create periodicity */
1581006a8963SMatthew G. Knepley   if (periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
1582006a8963SMatthew G. Knepley     PetscReal      L[3];
1583006a8963SMatthew G. Knepley     PetscReal      maxCell[3];
1584006a8963SMatthew G. Knepley     DMBoundaryType bdType[3];
1585006a8963SMatthew G. Knepley     PetscReal      lower[3] = {0.0, 0.0, 0.0};
1586ae8bcbbbSMatthew G. Knepley     PetscReal      upper[3] = {1.0, 1.0, 1.5};
1587ae8bcbbbSMatthew G. Knepley     PetscInt       i, numZCells = 3;
1588006a8963SMatthew G. Knepley 
1589006a8963SMatthew G. Knepley     bdType[0] = DM_BOUNDARY_NONE;
1590006a8963SMatthew G. Knepley     bdType[1] = DM_BOUNDARY_NONE;
1591006a8963SMatthew G. Knepley     bdType[2] = periodicZ;
1592006a8963SMatthew G. Knepley     for (i = 0; i < dim; i++) {
1593006a8963SMatthew G. Knepley       L[i]       = upper[i] - lower[i];
1594006a8963SMatthew G. Knepley       maxCell[i] = 1.1 * (L[i] / numZCells);
1595006a8963SMatthew G. Knepley     }
15969318fe57SMatthew G. Knepley     ierr = DMSetPeriodicity(dm, PETSC_TRUE, maxCell, L, bdType);CHKERRQ(ierr);
1597006a8963SMatthew G. Knepley   }
1598dbc1dc17SMatthew G. Knepley   {
15999318fe57SMatthew G. Knepley     DM          cdm;
16009318fe57SMatthew G. Knepley     PetscDS     cds;
16019318fe57SMatthew G. Knepley     PetscScalar c[2] = {1.0, 1.0};
1602dbc1dc17SMatthew G. Knepley 
16039318fe57SMatthew G. Knepley     ierr = DMPlexCreateCoordinateSpace(dm, 1, snapToCylinder);CHKERRQ(ierr);
16049318fe57SMatthew G. Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
16059318fe57SMatthew G. Knepley     ierr = DMGetDS(cdm, &cds);CHKERRQ(ierr);
16069318fe57SMatthew G. Knepley     ierr = PetscDSSetConstants(cds, 2, c);CHKERRQ(ierr);
1607dbc1dc17SMatthew G. Knepley   }
16089318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
16099318fe57SMatthew G. Knepley   ierr = DMPlexInterpolateInPlace_Internal(dm);CHKERRQ(ierr);
16100510c589SMatthew G. Knepley   PetscFunctionReturn(0);
16110510c589SMatthew G. Knepley }
16120510c589SMatthew G. Knepley 
161324119c2aSMatthew G. Knepley /*@
16149318fe57SMatthew G. Knepley   DMPlexCreateHexCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using hexahedra.
161524119c2aSMatthew G. Knepley 
1616d083f849SBarry Smith   Collective
161724119c2aSMatthew G. Knepley 
161824119c2aSMatthew G. Knepley   Input Parameters:
161924119c2aSMatthew G. Knepley + comm      - The communicator for the DM object
16209318fe57SMatthew G. Knepley - periodicZ - The boundary type for the Z direction
162124119c2aSMatthew G. Knepley 
162224119c2aSMatthew G. Knepley   Output Parameter:
162324119c2aSMatthew G. Knepley . dm  - The DM object
162424119c2aSMatthew G. Knepley 
16259318fe57SMatthew G. Knepley   Note:
16269318fe57SMatthew G. Knepley   Here is the output numbering looking from the bottom of the cylinder:
16279318fe57SMatthew G. Knepley $       17-----14
16289318fe57SMatthew G. Knepley $        |     |
16299318fe57SMatthew G. Knepley $        |  2  |
16309318fe57SMatthew G. Knepley $        |     |
16319318fe57SMatthew G. Knepley $ 17-----8-----7-----14
16329318fe57SMatthew G. Knepley $  |     |     |     |
16339318fe57SMatthew G. Knepley $  |  3  |  0  |  1  |
16349318fe57SMatthew G. Knepley $  |     |     |     |
16359318fe57SMatthew G. Knepley $ 19-----5-----6-----13
16369318fe57SMatthew G. Knepley $        |     |
16379318fe57SMatthew G. Knepley $        |  4  |
16389318fe57SMatthew G. Knepley $        |     |
16399318fe57SMatthew G. Knepley $       19-----13
16409318fe57SMatthew G. Knepley $
16419318fe57SMatthew G. Knepley $ and up through the top
16429318fe57SMatthew G. Knepley $
16439318fe57SMatthew G. Knepley $       18-----16
16449318fe57SMatthew G. Knepley $        |     |
16459318fe57SMatthew G. Knepley $        |  2  |
16469318fe57SMatthew G. Knepley $        |     |
16479318fe57SMatthew G. Knepley $ 18----10----11-----16
16489318fe57SMatthew G. Knepley $  |     |     |     |
16499318fe57SMatthew G. Knepley $  |  3  |  0  |  1  |
16509318fe57SMatthew G. Knepley $  |     |     |     |
16519318fe57SMatthew G. Knepley $ 20-----9----12-----15
16529318fe57SMatthew G. Knepley $        |     |
16539318fe57SMatthew G. Knepley $        |  4  |
16549318fe57SMatthew G. Knepley $        |     |
16559318fe57SMatthew G. Knepley $       20-----15
16569318fe57SMatthew G. Knepley 
165724119c2aSMatthew G. Knepley   Level: beginner
165824119c2aSMatthew G. Knepley 
16599318fe57SMatthew G. Knepley .seealso: DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
166024119c2aSMatthew G. Knepley @*/
16619318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateHexCylinderMesh(MPI_Comm comm, DMBoundaryType periodicZ, DM *dm)
16629318fe57SMatthew G. Knepley {
16639318fe57SMatthew G. Knepley   PetscErrorCode ierr;
16649318fe57SMatthew G. Knepley 
16659318fe57SMatthew G. Knepley   PetscFunctionBegin;
16669318fe57SMatthew G. Knepley   PetscValidPointer(dm, 3);
16679318fe57SMatthew G. Knepley   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
16689318fe57SMatthew G. Knepley   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
16699318fe57SMatthew G. Knepley   ierr = DMPlexCreateHexCylinderMesh_Internal(*dm, periodicZ);CHKERRQ(ierr);
16709318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
16719318fe57SMatthew G. Knepley }
16729318fe57SMatthew G. Knepley 
16739318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateWedgeCylinderMesh_Internal(DM dm, PetscInt n, PetscBool interpolate)
167424119c2aSMatthew G. Knepley {
167524119c2aSMatthew G. Knepley   const PetscInt dim = 3;
1676412e9a14SMatthew G. Knepley   PetscInt       numCells, numVertices, v;
16779fe9f049SMatthew G. Knepley   PetscMPIInt    rank;
167824119c2aSMatthew G. Knepley   PetscErrorCode ierr;
167924119c2aSMatthew G. Knepley 
168024119c2aSMatthew G. Knepley   PetscFunctionBegin;
16819318fe57SMatthew G. Knepley   if (n < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of wedges %D cannot be negative", n);
16829318fe57SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
16839318fe57SMatthew G. Knepley   ierr = DMSetDimension(dm, dim);CHKERRQ(ierr);
1684412e9a14SMatthew G. Knepley   /* Must create the celltype label here so that we do not automatically try to compute the types */
16859318fe57SMatthew G. Knepley   ierr = DMCreateLabel(dm, "celltype");CHKERRQ(ierr);
168624119c2aSMatthew G. Knepley   /* Create topology */
168724119c2aSMatthew G. Knepley   {
168824119c2aSMatthew G. Knepley     PetscInt cone[6], c;
168924119c2aSMatthew G. Knepley 
1690dd400576SPatrick Sanan     numCells    = rank == 0 ?        n : 0;
1691dd400576SPatrick Sanan     numVertices = rank == 0 ?  2*(n+1) : 0;
16929318fe57SMatthew G. Knepley     ierr = DMPlexSetChart(dm, 0, numCells+numVertices);CHKERRQ(ierr);
16939318fe57SMatthew G. Knepley     for (c = 0; c < numCells; c++) {ierr = DMPlexSetConeSize(dm, c, 6);CHKERRQ(ierr);}
16949318fe57SMatthew G. Knepley     ierr = DMSetUp(dm);CHKERRQ(ierr);
169524119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
169624119c2aSMatthew G. Knepley       cone[0] =  c+n*1; cone[1] = (c+1)%n+n*1; cone[2] = 0+3*n;
169724119c2aSMatthew G. Knepley       cone[3] =  c+n*2; cone[4] = (c+1)%n+n*2; cone[5] = 1+3*n;
16989318fe57SMatthew G. Knepley       ierr = DMPlexSetCone(dm, c, cone);CHKERRQ(ierr);
16999318fe57SMatthew G. Knepley       ierr = DMPlexSetCellType(dm, c, DM_POLYTOPE_TRI_PRISM_TENSOR);CHKERRQ(ierr);
170024119c2aSMatthew G. Knepley     }
17019318fe57SMatthew G. Knepley     ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
17029318fe57SMatthew G. Knepley     ierr = DMPlexStratify(dm);CHKERRQ(ierr);
170324119c2aSMatthew G. Knepley   }
1704412e9a14SMatthew G. Knepley   for (v = numCells; v < numCells+numVertices; ++v) {
17059318fe57SMatthew G. Knepley     ierr = DMPlexSetCellType(dm, v, DM_POLYTOPE_POINT);CHKERRQ(ierr);
170624119c2aSMatthew G. Knepley   }
170724119c2aSMatthew G. Knepley   /* Create cylinder geometry */
170824119c2aSMatthew G. Knepley   {
170924119c2aSMatthew G. Knepley     Vec          coordinates;
171024119c2aSMatthew G. Knepley     PetscSection coordSection;
171124119c2aSMatthew G. Knepley     PetscScalar *coords;
1712412e9a14SMatthew G. Knepley     PetscInt     coordSize, c;
171324119c2aSMatthew G. Knepley 
171424119c2aSMatthew G. Knepley     /* Build coordinates */
17159318fe57SMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
171624119c2aSMatthew G. Knepley     ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
171724119c2aSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(coordSection, 0, dim);CHKERRQ(ierr);
171824119c2aSMatthew G. Knepley     ierr = PetscSectionSetChart(coordSection, numCells, numCells+numVertices);CHKERRQ(ierr);
171924119c2aSMatthew G. Knepley     for (v = numCells; v < numCells+numVertices; ++v) {
172024119c2aSMatthew G. Knepley       ierr = PetscSectionSetDof(coordSection, v, dim);CHKERRQ(ierr);
172124119c2aSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(coordSection, v, 0, dim);CHKERRQ(ierr);
172224119c2aSMatthew G. Knepley     }
172324119c2aSMatthew G. Knepley     ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
172424119c2aSMatthew G. Knepley     ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
172524119c2aSMatthew G. Knepley     ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
172624119c2aSMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
172724119c2aSMatthew G. Knepley     ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
172824119c2aSMatthew G. Knepley     ierr = VecSetBlockSize(coordinates, dim);CHKERRQ(ierr);
172924119c2aSMatthew G. Knepley     ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
173024119c2aSMatthew G. Knepley     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
173124119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
173224119c2aSMatthew 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;
173324119c2aSMatthew 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;
173424119c2aSMatthew G. Knepley     }
1735dd400576SPatrick Sanan     if (rank == 0) {
173624119c2aSMatthew 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;
173724119c2aSMatthew 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;
17389fe9f049SMatthew G. Knepley     }
173924119c2aSMatthew G. Knepley     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
17409318fe57SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
174124119c2aSMatthew G. Knepley     ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
174224119c2aSMatthew G. Knepley   }
17439318fe57SMatthew G. Knepley   /* Interpolate */
17449318fe57SMatthew G. Knepley   if (interpolate) {ierr = DMPlexInterpolateInPlace_Internal(dm);CHKERRQ(ierr);}
17459318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
17469318fe57SMatthew G. Knepley }
17479318fe57SMatthew G. Knepley 
17489318fe57SMatthew G. Knepley /*@
17499318fe57SMatthew G. Knepley   DMPlexCreateWedgeCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using wedges.
17509318fe57SMatthew G. Knepley 
17519318fe57SMatthew G. Knepley   Collective
17529318fe57SMatthew G. Knepley 
17539318fe57SMatthew G. Knepley   Input Parameters:
17549318fe57SMatthew G. Knepley + comm - The communicator for the DM object
17559318fe57SMatthew G. Knepley . n    - The number of wedges around the origin
17569318fe57SMatthew G. Knepley - interpolate - Create edges and faces
17579318fe57SMatthew G. Knepley 
17589318fe57SMatthew G. Knepley   Output Parameter:
17599318fe57SMatthew G. Knepley . dm  - The DM object
17609318fe57SMatthew G. Knepley 
17619318fe57SMatthew G. Knepley   Level: beginner
17629318fe57SMatthew G. Knepley 
17639318fe57SMatthew G. Knepley .seealso: DMPlexCreateHexCylinderMesh(), DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
17649318fe57SMatthew G. Knepley @*/
17659318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateWedgeCylinderMesh(MPI_Comm comm, PetscInt n, PetscBool interpolate, DM *dm)
17669318fe57SMatthew G. Knepley {
17679318fe57SMatthew G. Knepley   PetscErrorCode ierr;
17689318fe57SMatthew G. Knepley 
17699318fe57SMatthew G. Knepley   PetscFunctionBegin;
17709318fe57SMatthew G. Knepley   PetscValidPointer(dm, 4);
17719318fe57SMatthew G. Knepley   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
17729318fe57SMatthew G. Knepley   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
17739318fe57SMatthew G. Knepley   ierr = DMPlexCreateWedgeCylinderMesh_Internal(*dm, n, interpolate);CHKERRQ(ierr);
177424119c2aSMatthew G. Knepley   PetscFunctionReturn(0);
177524119c2aSMatthew G. Knepley }
177624119c2aSMatthew G. Knepley 
177765a81367SMatthew G. Knepley PETSC_STATIC_INLINE PetscReal DiffNormReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
177865a81367SMatthew G. Knepley {
177965a81367SMatthew G. Knepley   PetscReal prod = 0.0;
178065a81367SMatthew G. Knepley   PetscInt  i;
178165a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += PetscSqr(x[i] - y[i]);
178265a81367SMatthew G. Knepley   return PetscSqrtReal(prod);
178365a81367SMatthew G. Knepley }
178465a81367SMatthew G. Knepley PETSC_STATIC_INLINE PetscReal DotReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
178565a81367SMatthew G. Knepley {
178665a81367SMatthew G. Knepley   PetscReal prod = 0.0;
178765a81367SMatthew G. Knepley   PetscInt  i;
178865a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += x[i]*y[i];
178965a81367SMatthew G. Knepley   return prod;
179065a81367SMatthew G. Knepley }
179165a81367SMatthew G. Knepley 
179251a74b61SMatthew G. Knepley /* The first constant is the sphere radius */
179351a74b61SMatthew G. Knepley static void snapToSphere(PetscInt dim, PetscInt Nf, PetscInt NfAux,
179451a74b61SMatthew G. Knepley                          const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
179551a74b61SMatthew G. Knepley                          const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
179651a74b61SMatthew G. Knepley                          PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
179751a74b61SMatthew G. Knepley {
179851a74b61SMatthew G. Knepley   PetscReal r = PetscRealPart(constants[0]);
179951a74b61SMatthew G. Knepley   PetscReal norm2 = 0.0, fac;
180051a74b61SMatthew G. Knepley   PetscInt  n = uOff[1] - uOff[0], d;
180151a74b61SMatthew G. Knepley 
180251a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) norm2 += PetscSqr(PetscRealPart(u[d]));
180351a74b61SMatthew G. Knepley   fac = r/PetscSqrtReal(norm2);
180451a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) f0[d] = u[d]*fac;
180551a74b61SMatthew G. Knepley }
180651a74b61SMatthew G. Knepley 
18079318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateSphereMesh_Internal(DM dm, PetscInt dim, PetscBool simplex, PetscReal R)
18082829fed8SMatthew G. Knepley {
180965a81367SMatthew G. Knepley   const PetscInt  embedDim = dim+1;
181065a81367SMatthew G. Knepley   PetscSection    coordSection;
181165a81367SMatthew G. Knepley   Vec             coordinates;
181265a81367SMatthew G. Knepley   PetscScalar    *coords;
181365a81367SMatthew G. Knepley   PetscReal      *coordsIn;
181465a81367SMatthew G. Knepley   PetscInt        numCells, numEdges, numVerts, firstVertex, v, firstEdge, coordSize, d, c, e;
181565a81367SMatthew G. Knepley   PetscMPIInt     rank;
181665a81367SMatthew G. Knepley   PetscErrorCode  ierr;
181765a81367SMatthew G. Knepley 
181865a81367SMatthew G. Knepley   PetscFunctionBegin;
18199318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveBool(dm, simplex, 3);
18209318fe57SMatthew G. Knepley   ierr = DMSetDimension(dm, dim);CHKERRQ(ierr);
18219318fe57SMatthew G. Knepley   ierr = DMSetCoordinateDim(dm, dim+1);CHKERRQ(ierr);
18229318fe57SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
182365a81367SMatthew G. Knepley   switch (dim) {
182465a81367SMatthew G. Knepley   case 2:
182565a81367SMatthew G. Knepley     if (simplex) {
182651a74b61SMatthew G. Knepley       const PetscReal radius    = PetscSqrtReal(1 + PETSC_PHI*PETSC_PHI)/(1.0 + PETSC_PHI);
182751a74b61SMatthew G. Knepley       const PetscReal edgeLen   = 2.0/(1.0 + PETSC_PHI) * (R/radius);
182865a81367SMatthew G. Knepley       const PetscInt  degree    = 5;
182951a74b61SMatthew G. Knepley       PetscReal       vertex[3] = {0.0, 1.0/(1.0 + PETSC_PHI), PETSC_PHI/(1.0 + PETSC_PHI)};
183065a81367SMatthew G. Knepley       PetscInt        s[3]      = {1, 1, 1};
183165a81367SMatthew G. Knepley       PetscInt        cone[3];
183265a81367SMatthew G. Knepley       PetscInt       *graph, p, i, j, k;
183365a81367SMatthew G. Knepley 
183451a74b61SMatthew G. Knepley       vertex[0] *= R/radius; vertex[1] *= R/radius; vertex[2] *= R/radius;
1835dd400576SPatrick Sanan       numCells    = rank == 0 ? 20 : 0;
1836dd400576SPatrick Sanan       numVerts    = rank == 0 ? 12 : 0;
183765a81367SMatthew G. Knepley       firstVertex = numCells;
183851a74b61SMatthew G. Knepley       /* Use icosahedron, which for a R-sphere has coordinates which are all cyclic permutations of
183965a81367SMatthew G. Knepley 
184065a81367SMatthew G. Knepley            (0, \pm 1/\phi+1, \pm \phi/\phi+1)
184165a81367SMatthew G. Knepley 
184265a81367SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
184351a74b61SMatthew G. Knepley          length is then given by 2/(1+\phi) = 2 * 0.38197 = 0.76393.
184465a81367SMatthew G. Knepley       */
184565a81367SMatthew G. Knepley       /* Construct vertices */
184665a81367SMatthew G. Knepley       ierr = PetscCalloc1(numVerts * embedDim, &coordsIn);CHKERRQ(ierr);
1847dd400576SPatrick Sanan       if (rank == 0) {
184865a81367SMatthew G. Knepley         for (p = 0, i = 0; p < embedDim; ++p) {
184965a81367SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
185065a81367SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
185165a81367SMatthew G. Knepley               for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[(d+p)%embedDim]*vertex[(d+p)%embedDim];
185265a81367SMatthew G. Knepley               ++i;
185365a81367SMatthew G. Knepley             }
185465a81367SMatthew G. Knepley           }
185565a81367SMatthew G. Knepley         }
185645da822fSValeria Barra       }
185765a81367SMatthew G. Knepley       /* Construct graph */
185865a81367SMatthew G. Knepley       ierr = PetscCalloc1(numVerts * numVerts, &graph);CHKERRQ(ierr);
185965a81367SMatthew G. Knepley       for (i = 0; i < numVerts; ++i) {
186065a81367SMatthew G. Knepley         for (j = 0, k = 0; j < numVerts; ++j) {
186165a81367SMatthew G. Knepley           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i*embedDim], &coordsIn[j*embedDim]) - edgeLen) < PETSC_SMALL) {graph[i*numVerts+j] = 1; ++k;}
186265a81367SMatthew G. Knepley         }
18639318fe57SMatthew G. Knepley         if (k != degree) SETERRQ3(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid icosahedron, vertex %D degree %D != %D", i, k, degree);
186465a81367SMatthew G. Knepley       }
186565a81367SMatthew G. Knepley       /* Build Topology */
18669318fe57SMatthew G. Knepley       ierr = DMPlexSetChart(dm, 0, numCells+numVerts);CHKERRQ(ierr);
186765a81367SMatthew G. Knepley       for (c = 0; c < numCells; c++) {
18689318fe57SMatthew G. Knepley         ierr = DMPlexSetConeSize(dm, c, embedDim);CHKERRQ(ierr);
186965a81367SMatthew G. Knepley       }
18709318fe57SMatthew G. Knepley       ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
187165a81367SMatthew G. Knepley       /* Cells */
187265a81367SMatthew G. Knepley       for (i = 0, c = 0; i < numVerts; ++i) {
187365a81367SMatthew G. Knepley         for (j = 0; j < i; ++j) {
187465a81367SMatthew G. Knepley           for (k = 0; k < j; ++k) {
187565a81367SMatthew G. Knepley             if (graph[i*numVerts+j] && graph[j*numVerts+k] && graph[k*numVerts+i]) {
187665a81367SMatthew G. Knepley               cone[0] = firstVertex+i; cone[1] = firstVertex+j; cone[2] = firstVertex+k;
187765a81367SMatthew G. Knepley               /* Check orientation */
187865a81367SMatthew G. Knepley               {
187965a81367SMatthew 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}}};
188065a81367SMatthew G. Knepley                 PetscReal normal[3];
188165a81367SMatthew G. Knepley                 PetscInt  e, f;
188265a81367SMatthew G. Knepley 
188365a81367SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) {
188465a81367SMatthew G. Knepley                   normal[d] = 0.0;
188565a81367SMatthew G. Knepley                   for (e = 0; e < embedDim; ++e) {
188665a81367SMatthew G. Knepley                     for (f = 0; f < embedDim; ++f) {
188765a81367SMatthew G. Knepley                       normal[d] += epsilon[d][e][f]*(coordsIn[j*embedDim+e] - coordsIn[i*embedDim+e])*(coordsIn[k*embedDim+f] - coordsIn[i*embedDim+f]);
188865a81367SMatthew G. Knepley                     }
188965a81367SMatthew G. Knepley                   }
189065a81367SMatthew G. Knepley                 }
189165a81367SMatthew G. Knepley                 if (DotReal(embedDim, normal, &coordsIn[i*embedDim]) < 0) {PetscInt tmp = cone[1]; cone[1] = cone[2]; cone[2] = tmp;}
189265a81367SMatthew G. Knepley               }
18939318fe57SMatthew G. Knepley               ierr = DMPlexSetCone(dm, c++, cone);CHKERRQ(ierr);
189465a81367SMatthew G. Knepley             }
189565a81367SMatthew G. Knepley           }
189665a81367SMatthew G. Knepley         }
189765a81367SMatthew G. Knepley       }
18989318fe57SMatthew G. Knepley       ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
18999318fe57SMatthew G. Knepley       ierr = DMPlexStratify(dm);CHKERRQ(ierr);
190065a81367SMatthew G. Knepley       ierr = PetscFree(graph);CHKERRQ(ierr);
190165a81367SMatthew G. Knepley     } else {
19022829fed8SMatthew G. Knepley       /*
19032829fed8SMatthew G. Knepley         12-21--13
19042829fed8SMatthew G. Knepley          |     |
19052829fed8SMatthew G. Knepley         25  4  24
19062829fed8SMatthew G. Knepley          |     |
19072829fed8SMatthew G. Knepley   12-25--9-16--8-24--13
19082829fed8SMatthew G. Knepley    |     |     |     |
19092829fed8SMatthew G. Knepley   23  5 17  0 15  3  22
19102829fed8SMatthew G. Knepley    |     |     |     |
19112829fed8SMatthew G. Knepley   10-20--6-14--7-19--11
19122829fed8SMatthew G. Knepley          |     |
19132829fed8SMatthew G. Knepley         20  1  19
19142829fed8SMatthew G. Knepley          |     |
19152829fed8SMatthew G. Knepley         10-18--11
19162829fed8SMatthew G. Knepley          |     |
19172829fed8SMatthew G. Knepley         23  2  22
19182829fed8SMatthew G. Knepley          |     |
19192829fed8SMatthew G. Knepley         12-21--13
19202829fed8SMatthew G. Knepley        */
19212829fed8SMatthew G. Knepley       PetscInt cone[4], ornt[4];
19222829fed8SMatthew G. Knepley 
1923dd400576SPatrick Sanan       numCells    = rank == 0 ?  6 : 0;
1924dd400576SPatrick Sanan       numEdges    = rank == 0 ? 12 : 0;
1925dd400576SPatrick Sanan       numVerts    = rank == 0 ?  8 : 0;
192665a81367SMatthew G. Knepley       firstVertex = numCells;
192765a81367SMatthew G. Knepley       firstEdge   = numCells + numVerts;
19282829fed8SMatthew G. Knepley       /* Build Topology */
19299318fe57SMatthew G. Knepley       ierr = DMPlexSetChart(dm, 0, numCells+numEdges+numVerts);CHKERRQ(ierr);
19302829fed8SMatthew G. Knepley       for (c = 0; c < numCells; c++) {
19319318fe57SMatthew G. Knepley         ierr = DMPlexSetConeSize(dm, c, 4);CHKERRQ(ierr);
19322829fed8SMatthew G. Knepley       }
19332829fed8SMatthew G. Knepley       for (e = firstEdge; e < firstEdge+numEdges; ++e) {
19349318fe57SMatthew G. Knepley         ierr = DMPlexSetConeSize(dm, e, 2);CHKERRQ(ierr);
19352829fed8SMatthew G. Knepley       }
19369318fe57SMatthew G. Knepley       ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
1937dd400576SPatrick Sanan       if (rank == 0) {
19382829fed8SMatthew G. Knepley         /* Cell 0 */
19392829fed8SMatthew G. Knepley         cone[0] = 14; cone[1] = 15; cone[2] = 16; cone[3] = 17;
19409318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 0, cone);CHKERRQ(ierr);
19412829fed8SMatthew G. Knepley         ornt[0] = 0; ornt[1] = 0; ornt[2] = 0; ornt[3] = 0;
19429318fe57SMatthew G. Knepley         ierr = DMPlexSetConeOrientation(dm, 0, ornt);CHKERRQ(ierr);
19432829fed8SMatthew G. Knepley         /* Cell 1 */
19442829fed8SMatthew G. Knepley         cone[0] = 18; cone[1] = 19; cone[2] = 14; cone[3] = 20;
19459318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 1, cone);CHKERRQ(ierr);
1946b5a892a1SMatthew G. Knepley         ornt[0] = 0; ornt[1] = 0; ornt[2] = -1; ornt[3] = 0;
19479318fe57SMatthew G. Knepley         ierr = DMPlexSetConeOrientation(dm, 1, ornt);CHKERRQ(ierr);
19482829fed8SMatthew G. Knepley         /* Cell 2 */
19492829fed8SMatthew G. Knepley         cone[0] = 21; cone[1] = 22; cone[2] = 18; cone[3] = 23;
19509318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 2, cone);CHKERRQ(ierr);
1951b5a892a1SMatthew G. Knepley         ornt[0] = 0; ornt[1] = 0; ornt[2] = -1; ornt[3] = 0;
19529318fe57SMatthew G. Knepley         ierr = DMPlexSetConeOrientation(dm, 2, ornt);CHKERRQ(ierr);
19532829fed8SMatthew G. Knepley         /* Cell 3 */
19542829fed8SMatthew G. Knepley         cone[0] = 19; cone[1] = 22; cone[2] = 24; cone[3] = 15;
19559318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 3, cone);CHKERRQ(ierr);
1956b5a892a1SMatthew G. Knepley         ornt[0] = -1; ornt[1] = -1; ornt[2] = 0; ornt[3] = -1;
19579318fe57SMatthew G. Knepley         ierr = DMPlexSetConeOrientation(dm, 3, ornt);CHKERRQ(ierr);
19582829fed8SMatthew G. Knepley         /* Cell 4 */
19592829fed8SMatthew G. Knepley         cone[0] = 16; cone[1] = 24; cone[2] = 21; cone[3] = 25;
19609318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 4, cone);CHKERRQ(ierr);
1961b5a892a1SMatthew G. Knepley         ornt[0] = -1; ornt[1] = -1; ornt[2] = -1; ornt[3] = 0;
19629318fe57SMatthew G. Knepley         ierr = DMPlexSetConeOrientation(dm, 4, ornt);CHKERRQ(ierr);
19632829fed8SMatthew G. Knepley         /* Cell 5 */
19642829fed8SMatthew G. Knepley         cone[0] = 20; cone[1] = 17; cone[2] = 25; cone[3] = 23;
19659318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 5, cone);CHKERRQ(ierr);
1966b5a892a1SMatthew G. Knepley         ornt[0] = -1; ornt[1] = -1; ornt[2] = -1; ornt[3] = -1;
19679318fe57SMatthew G. Knepley         ierr = DMPlexSetConeOrientation(dm, 5, ornt);CHKERRQ(ierr);
19682829fed8SMatthew G. Knepley         /* Edges */
19692829fed8SMatthew G. Knepley         cone[0] =  6; cone[1] =  7;
19709318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 14, cone);CHKERRQ(ierr);
19712829fed8SMatthew G. Knepley         cone[0] =  7; cone[1] =  8;
19729318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 15, cone);CHKERRQ(ierr);
19732829fed8SMatthew G. Knepley         cone[0] =  8; cone[1] =  9;
19749318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 16, cone);CHKERRQ(ierr);
19752829fed8SMatthew G. Knepley         cone[0] =  9; cone[1] =  6;
19769318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 17, cone);CHKERRQ(ierr);
19772829fed8SMatthew G. Knepley         cone[0] = 10; cone[1] = 11;
19789318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 18, cone);CHKERRQ(ierr);
19792829fed8SMatthew G. Knepley         cone[0] = 11; cone[1] =  7;
19809318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 19, cone);CHKERRQ(ierr);
19812829fed8SMatthew G. Knepley         cone[0] =  6; cone[1] = 10;
19829318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 20, cone);CHKERRQ(ierr);
19832829fed8SMatthew G. Knepley         cone[0] = 12; cone[1] = 13;
19849318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 21, cone);CHKERRQ(ierr);
19852829fed8SMatthew G. Knepley         cone[0] = 13; cone[1] = 11;
19869318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 22, cone);CHKERRQ(ierr);
19872829fed8SMatthew G. Knepley         cone[0] = 10; cone[1] = 12;
19889318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 23, cone);CHKERRQ(ierr);
19892829fed8SMatthew G. Knepley         cone[0] = 13; cone[1] =  8;
19909318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 24, cone);CHKERRQ(ierr);
19912829fed8SMatthew G. Knepley         cone[0] = 12; cone[1] =  9;
19929318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 25, cone);CHKERRQ(ierr);
199345da822fSValeria Barra       }
19949318fe57SMatthew G. Knepley       ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
19959318fe57SMatthew G. Knepley       ierr = DMPlexStratify(dm);CHKERRQ(ierr);
19962829fed8SMatthew G. Knepley       /* Build coordinates */
199765a81367SMatthew G. Knepley       ierr = PetscCalloc1(numVerts * embedDim, &coordsIn);CHKERRQ(ierr);
1998dd400576SPatrick Sanan       if (rank == 0) {
199951a74b61SMatthew G. Knepley         coordsIn[0*embedDim+0] = -R; coordsIn[0*embedDim+1] =  R; coordsIn[0*embedDim+2] = -R;
200051a74b61SMatthew G. Knepley         coordsIn[1*embedDim+0] =  R; coordsIn[1*embedDim+1] =  R; coordsIn[1*embedDim+2] = -R;
200151a74b61SMatthew G. Knepley         coordsIn[2*embedDim+0] =  R; coordsIn[2*embedDim+1] = -R; coordsIn[2*embedDim+2] = -R;
200251a74b61SMatthew G. Knepley         coordsIn[3*embedDim+0] = -R; coordsIn[3*embedDim+1] = -R; coordsIn[3*embedDim+2] = -R;
200351a74b61SMatthew G. Knepley         coordsIn[4*embedDim+0] = -R; coordsIn[4*embedDim+1] =  R; coordsIn[4*embedDim+2] =  R;
200451a74b61SMatthew G. Knepley         coordsIn[5*embedDim+0] =  R; coordsIn[5*embedDim+1] =  R; coordsIn[5*embedDim+2] =  R;
200551a74b61SMatthew G. Knepley         coordsIn[6*embedDim+0] = -R; coordsIn[6*embedDim+1] = -R; coordsIn[6*embedDim+2] =  R;
200651a74b61SMatthew G. Knepley         coordsIn[7*embedDim+0] =  R; coordsIn[7*embedDim+1] = -R; coordsIn[7*embedDim+2] =  R;
200765a81367SMatthew G. Knepley       }
200845da822fSValeria Barra     }
200965a81367SMatthew G. Knepley     break;
201065a81367SMatthew G. Knepley   case 3:
2011116ded15SMatthew G. Knepley     if (simplex) {
2012116ded15SMatthew G. Knepley       const PetscReal edgeLen         = 1.0/PETSC_PHI;
201351a74b61SMatthew G. Knepley       PetscReal       vertexA[4]      = {0.5, 0.5, 0.5, 0.5};
201451a74b61SMatthew G. Knepley       PetscReal       vertexB[4]      = {1.0, 0.0, 0.0, 0.0};
201551a74b61SMatthew G. Knepley       PetscReal       vertexC[4]      = {0.5, 0.5*PETSC_PHI, 0.5/PETSC_PHI, 0.0};
2016116ded15SMatthew G. Knepley       const PetscInt  degree          = 12;
2017116ded15SMatthew G. Knepley       PetscInt        s[4]            = {1, 1, 1};
2018116ded15SMatthew 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},
2019116ded15SMatthew 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}};
2020116ded15SMatthew G. Knepley       PetscInt        cone[4];
2021116ded15SMatthew G. Knepley       PetscInt       *graph, p, i, j, k, l;
2022116ded15SMatthew G. Knepley 
202351a74b61SMatthew G. Knepley       vertexA[0] *= R; vertexA[1] *= R; vertexA[2] *= R; vertexA[3] *= R;
202451a74b61SMatthew G. Knepley       vertexB[0] *= R; vertexB[1] *= R; vertexB[2] *= R; vertexB[3] *= R;
202551a74b61SMatthew G. Knepley       vertexC[0] *= R; vertexC[1] *= R; vertexC[2] *= R; vertexC[3] *= R;
2026dd400576SPatrick Sanan       numCells    = rank == 0 ? 600 : 0;
2027dd400576SPatrick Sanan       numVerts    = rank == 0 ? 120 : 0;
2028116ded15SMatthew G. Knepley       firstVertex = numCells;
2029116ded15SMatthew G. Knepley       /* Use the 600-cell, which for a unit sphere has coordinates which are
2030116ded15SMatthew G. Knepley 
2031116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm 1,    \pm 1, \pm 1)                          16
2032116ded15SMatthew G. Knepley                (\pm 1,    0,       0,      0)  all cyclic permutations   8
2033116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm phi, \pm 1/phi, 0)  all even permutations    96
2034116ded15SMatthew G. Knepley 
2035116ded15SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
20366333ae4fSvaleriabarra          length is then given by 1/\phi = 0.61803.
2037116ded15SMatthew G. Knepley 
2038116ded15SMatthew G. Knepley          http://buzzard.pugetsound.edu/sage-practice/ch03s03.html
2039116ded15SMatthew G. Knepley          http://mathworld.wolfram.com/600-Cell.html
2040116ded15SMatthew G. Knepley       */
2041116ded15SMatthew G. Knepley       /* Construct vertices */
2042116ded15SMatthew G. Knepley       ierr = PetscCalloc1(numVerts * embedDim, &coordsIn);CHKERRQ(ierr);
2043116ded15SMatthew G. Knepley       i    = 0;
2044dd400576SPatrick Sanan       if (rank == 0) {
2045116ded15SMatthew G. Knepley         for (s[0] = -1; s[0] < 2; s[0] += 2) {
2046116ded15SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
2047116ded15SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
2048116ded15SMatthew G. Knepley               for (s[3] = -1; s[3] < 2; s[3] += 2) {
2049116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[d]*vertexA[d];
2050116ded15SMatthew G. Knepley                 ++i;
2051116ded15SMatthew G. Knepley               }
2052116ded15SMatthew G. Knepley             }
2053116ded15SMatthew G. Knepley           }
2054116ded15SMatthew G. Knepley         }
2055116ded15SMatthew G. Knepley         for (p = 0; p < embedDim; ++p) {
2056116ded15SMatthew G. Knepley           s[1] = s[2] = s[3] = 1;
2057116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2058116ded15SMatthew G. Knepley             for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[(d+p)%embedDim]*vertexB[(d+p)%embedDim];
2059116ded15SMatthew G. Knepley             ++i;
2060116ded15SMatthew G. Knepley           }
2061116ded15SMatthew G. Knepley         }
2062116ded15SMatthew G. Knepley         for (p = 0; p < 12; ++p) {
2063116ded15SMatthew G. Knepley           s[3] = 1;
2064116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2065116ded15SMatthew G. Knepley             for (s[1] = -1; s[1] < 2; s[1] += 2) {
2066116ded15SMatthew G. Knepley               for (s[2] = -1; s[2] < 2; s[2] += 2) {
2067116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[evenPerm[p][d]]*vertexC[evenPerm[p][d]];
2068116ded15SMatthew G. Knepley                 ++i;
2069116ded15SMatthew G. Knepley               }
2070116ded15SMatthew G. Knepley             }
2071116ded15SMatthew G. Knepley           }
2072116ded15SMatthew G. Knepley         }
207345da822fSValeria Barra       }
20749318fe57SMatthew G. Knepley       if (i != numVerts) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertices %D != %D", i, numVerts);
2075116ded15SMatthew G. Knepley       /* Construct graph */
2076116ded15SMatthew G. Knepley       ierr = PetscCalloc1(numVerts * numVerts, &graph);CHKERRQ(ierr);
2077116ded15SMatthew G. Knepley       for (i = 0; i < numVerts; ++i) {
2078116ded15SMatthew G. Knepley         for (j = 0, k = 0; j < numVerts; ++j) {
2079116ded15SMatthew G. Knepley           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i*embedDim], &coordsIn[j*embedDim]) - edgeLen) < PETSC_SMALL) {graph[i*numVerts+j] = 1; ++k;}
2080116ded15SMatthew G. Knepley         }
20819318fe57SMatthew G. Knepley         if (k != degree) SETERRQ3(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertex %D degree %D != %D", i, k, degree);
2082116ded15SMatthew G. Knepley       }
2083116ded15SMatthew G. Knepley       /* Build Topology */
20849318fe57SMatthew G. Knepley       ierr = DMPlexSetChart(dm, 0, numCells+numVerts);CHKERRQ(ierr);
2085116ded15SMatthew G. Knepley       for (c = 0; c < numCells; c++) {
20869318fe57SMatthew G. Knepley         ierr = DMPlexSetConeSize(dm, c, embedDim);CHKERRQ(ierr);
2087116ded15SMatthew G. Knepley       }
20889318fe57SMatthew G. Knepley       ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
2089116ded15SMatthew G. Knepley       /* Cells */
2090dd400576SPatrick Sanan       if (rank == 0) {
2091116ded15SMatthew G. Knepley         for (i = 0, c = 0; i < numVerts; ++i) {
2092116ded15SMatthew G. Knepley           for (j = 0; j < i; ++j) {
2093116ded15SMatthew G. Knepley             for (k = 0; k < j; ++k) {
2094116ded15SMatthew G. Knepley               for (l = 0; l < k; ++l) {
2095116ded15SMatthew G. Knepley                 if (graph[i*numVerts+j] && graph[j*numVerts+k] && graph[k*numVerts+i] &&
2096116ded15SMatthew G. Knepley                     graph[l*numVerts+i] && graph[l*numVerts+j] && graph[l*numVerts+k]) {
2097116ded15SMatthew G. Knepley                   cone[0] = firstVertex+i; cone[1] = firstVertex+j; cone[2] = firstVertex+k; cone[3] = firstVertex+l;
2098116ded15SMatthew G. Knepley                   /* Check orientation: https://ef.gy/linear-algebra:normal-vectors-in-higher-dimensional-spaces */
2099116ded15SMatthew G. Knepley                   {
2100116ded15SMatthew 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}},
2101116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  1}, { 0,  0, -1, 0}},
2102116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0, -1}, { 0,  0, 0,  0}, { 0,  1,  0, 0}},
2103116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  1,  0}, { 0, -1, 0,  0}, { 0,  0,  0, 0}}},
2104116ded15SMatthew G. Knepley 
2105116ded15SMatthew G. Knepley                                                           {{{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0, -1}, { 0,  0,  1, 0}},
2106116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
2107116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  1}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, {-1,  0,  0, 0}},
2108116ded15SMatthew G. Knepley                                                            {{0,  0, -1,  0}, { 0, 0,  0,  0}, { 1,  0, 0,  0}, { 0,  0,  0, 0}}},
2109116ded15SMatthew G. Knepley 
2110116ded15SMatthew G. Knepley                                                           {{{0,  0,  0,  0}, { 0, 0,  0,  1}, { 0,  0, 0,  0}, { 0, -1,  0, 0}},
2111116ded15SMatthew G. Knepley                                                            {{0,  0,  0, -1}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 1,  0,  0, 0}},
2112116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
2113116ded15SMatthew G. Knepley                                                            {{0,  1,  0,  0}, {-1, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}}},
2114116ded15SMatthew G. Knepley 
2115116ded15SMatthew G. Knepley                                                           {{{0,  0,  0,  0}, { 0, 0, -1,  0}, { 0,  1, 0,  0}, { 0,  0,  0, 0}},
2116116ded15SMatthew G. Knepley                                                            {{0,  0,  1,  0}, { 0, 0,  0,  0}, {-1,  0, 0,  0}, { 0,  0,  0, 0}},
2117116ded15SMatthew G. Knepley                                                            {{0, -1,  0,  0}, { 1, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
2118116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}}}};
2119116ded15SMatthew G. Knepley                     PetscReal normal[4];
2120116ded15SMatthew G. Knepley                     PetscInt  e, f, g;
2121116ded15SMatthew G. Knepley 
2122116ded15SMatthew G. Knepley                     for (d = 0; d < embedDim; ++d) {
2123116ded15SMatthew G. Knepley                       normal[d] = 0.0;
2124116ded15SMatthew G. Knepley                       for (e = 0; e < embedDim; ++e) {
2125116ded15SMatthew G. Knepley                         for (f = 0; f < embedDim; ++f) {
2126116ded15SMatthew G. Knepley                           for (g = 0; g < embedDim; ++g) {
2127116ded15SMatthew 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]);
2128116ded15SMatthew G. Knepley                           }
2129116ded15SMatthew G. Knepley                         }
2130116ded15SMatthew G. Knepley                       }
2131116ded15SMatthew G. Knepley                     }
2132116ded15SMatthew G. Knepley                     if (DotReal(embedDim, normal, &coordsIn[i*embedDim]) < 0) {PetscInt tmp = cone[1]; cone[1] = cone[2]; cone[2] = tmp;}
2133116ded15SMatthew G. Knepley                   }
21349318fe57SMatthew G. Knepley                   ierr = DMPlexSetCone(dm, c++, cone);CHKERRQ(ierr);
2135116ded15SMatthew G. Knepley                 }
2136116ded15SMatthew G. Knepley               }
2137116ded15SMatthew G. Knepley             }
2138116ded15SMatthew G. Knepley           }
2139116ded15SMatthew G. Knepley         }
214045da822fSValeria Barra       }
21419318fe57SMatthew G. Knepley       ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
21429318fe57SMatthew G. Knepley       ierr = DMPlexStratify(dm);CHKERRQ(ierr);
2143116ded15SMatthew G. Knepley       ierr = PetscFree(graph);CHKERRQ(ierr);
2144116ded15SMatthew G. Knepley       break;
2145116ded15SMatthew G. Knepley     }
21469318fe57SMatthew G. Knepley   default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Unsupported dimension for sphere: %D", dim);
214765a81367SMatthew G. Knepley   }
214865a81367SMatthew G. Knepley   /* Create coordinates */
21499318fe57SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
21502829fed8SMatthew G. Knepley   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
21512829fed8SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(coordSection, 0, embedDim);CHKERRQ(ierr);
21522829fed8SMatthew G. Knepley   ierr = PetscSectionSetChart(coordSection, firstVertex, firstVertex+numVerts);CHKERRQ(ierr);
21532829fed8SMatthew G. Knepley   for (v = firstVertex; v < firstVertex+numVerts; ++v) {
21542829fed8SMatthew G. Knepley     ierr = PetscSectionSetDof(coordSection, v, embedDim);CHKERRQ(ierr);
21552829fed8SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(coordSection, v, 0, embedDim);CHKERRQ(ierr);
21562829fed8SMatthew G. Knepley   }
21572829fed8SMatthew G. Knepley   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
21582829fed8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
21592829fed8SMatthew G. Knepley   ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
21602829fed8SMatthew G. Knepley   ierr = VecSetBlockSize(coordinates, embedDim);CHKERRQ(ierr);
21612829fed8SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
21622829fed8SMatthew G. Knepley   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
21632829fed8SMatthew G. Knepley   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
21642829fed8SMatthew G. Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
216565a81367SMatthew G. Knepley   for (v = 0; v < numVerts; ++v) for (d = 0; d < embedDim; ++d) {coords[v*embedDim+d] = coordsIn[v*embedDim+d];}
21662829fed8SMatthew G. Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
21679318fe57SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
21682829fed8SMatthew G. Knepley   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
216965a81367SMatthew G. Knepley   ierr = PetscFree(coordsIn);CHKERRQ(ierr);
217051a74b61SMatthew G. Knepley   {
217151a74b61SMatthew G. Knepley     DM          cdm;
217251a74b61SMatthew G. Knepley     PetscDS     cds;
21739318fe57SMatthew G. Knepley     PetscScalar c = R;
217451a74b61SMatthew G. Knepley 
21759318fe57SMatthew G. Knepley     ierr = DMPlexCreateCoordinateSpace(dm, 1, snapToSphere);CHKERRQ(ierr);
21769318fe57SMatthew G. Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
217751a74b61SMatthew G. Knepley     ierr = DMGetDS(cdm, &cds);CHKERRQ(ierr);
21789318fe57SMatthew G. Knepley     ierr = PetscDSSetConstants(cds, 1, &c);CHKERRQ(ierr);
217951a74b61SMatthew G. Knepley   }
21809318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
21819318fe57SMatthew G. Knepley   if (simplex) {ierr = DMPlexInterpolateInPlace_Internal(dm);CHKERRQ(ierr);}
21829318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
21839318fe57SMatthew G. Knepley }
21849318fe57SMatthew G. Knepley 
21859318fe57SMatthew G. Knepley /*@
21869318fe57SMatthew G. Knepley   DMPlexCreateSphereMesh - Creates a mesh on the d-dimensional sphere, S^d.
21879318fe57SMatthew G. Knepley 
21889318fe57SMatthew G. Knepley   Collective
21899318fe57SMatthew G. Knepley 
21909318fe57SMatthew G. Knepley   Input Parameters:
21919318fe57SMatthew G. Knepley + comm    - The communicator for the DM object
21929318fe57SMatthew G. Knepley . dim     - The dimension
21939318fe57SMatthew G. Knepley . simplex - Use simplices, or tensor product cells
21949318fe57SMatthew G. Knepley - R       - The radius
21959318fe57SMatthew G. Knepley 
21969318fe57SMatthew G. Knepley   Output Parameter:
21979318fe57SMatthew G. Knepley . dm  - The DM object
21989318fe57SMatthew G. Knepley 
21999318fe57SMatthew G. Knepley   Level: beginner
22009318fe57SMatthew G. Knepley 
22019318fe57SMatthew G. Knepley .seealso: DMPlexCreateBallMesh(), DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
22029318fe57SMatthew G. Knepley @*/
22039318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateSphereMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscReal R, DM *dm)
22049318fe57SMatthew G. Knepley {
22059318fe57SMatthew G. Knepley   PetscErrorCode ierr;
22069318fe57SMatthew G. Knepley 
22079318fe57SMatthew G. Knepley   PetscFunctionBegin;
22089318fe57SMatthew G. Knepley   PetscValidPointer(dm, 5);
22099318fe57SMatthew G. Knepley   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
22109318fe57SMatthew G. Knepley   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
22119318fe57SMatthew G. Knepley   ierr = DMPlexCreateSphereMesh_Internal(*dm, dim, simplex, R);CHKERRQ(ierr);
22129318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
22139318fe57SMatthew G. Knepley }
22149318fe57SMatthew G. Knepley 
22159318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBallMesh_Internal(DM dm, PetscInt dim, PetscReal R)
22169318fe57SMatthew G. Knepley {
22179318fe57SMatthew G. Knepley   DM             sdm, vol;
22189318fe57SMatthew G. Knepley   DMLabel        bdlabel;
22199318fe57SMatthew G. Knepley   PetscErrorCode ierr;
22209318fe57SMatthew G. Knepley 
22219318fe57SMatthew G. Knepley   PetscFunctionBegin;
22229318fe57SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), &sdm);CHKERRQ(ierr);
22239318fe57SMatthew G. Knepley   ierr = DMSetType(sdm, DMPLEX);CHKERRQ(ierr);
22249318fe57SMatthew G. Knepley   ierr = PetscObjectSetOptionsPrefix((PetscObject) sdm, "bd_");CHKERRQ(ierr);
22259318fe57SMatthew G. Knepley   ierr = DMPlexCreateSphereMesh_Internal(sdm, dim-1, PETSC_TRUE, R);CHKERRQ(ierr);
22269318fe57SMatthew G. Knepley   ierr = DMSetFromOptions(sdm);CHKERRQ(ierr);
22279318fe57SMatthew G. Knepley   ierr = DMViewFromOptions(sdm, NULL, "-dm_view");CHKERRQ(ierr);
22289318fe57SMatthew G. Knepley   ierr = DMPlexGenerate(sdm, NULL, PETSC_TRUE, &vol);CHKERRQ(ierr);
22299318fe57SMatthew G. Knepley   ierr = DMDestroy(&sdm);CHKERRQ(ierr);
22309318fe57SMatthew G. Knepley   ierr = DMPlexReplace_Static(dm, &vol);CHKERRQ(ierr);
22319318fe57SMatthew G. Knepley   ierr = DMCreateLabel(dm, "marker");CHKERRQ(ierr);
22329318fe57SMatthew G. Knepley   ierr = DMGetLabel(dm, "marker", &bdlabel);CHKERRQ(ierr);
22339318fe57SMatthew G. Knepley   ierr = DMPlexMarkBoundaryFaces(dm, PETSC_DETERMINE, bdlabel);CHKERRQ(ierr);
22349318fe57SMatthew G. Knepley   ierr = DMPlexLabelComplete(dm, bdlabel);CHKERRQ(ierr);
223551a74b61SMatthew G. Knepley   PetscFunctionReturn(0);
223651a74b61SMatthew G. Knepley }
223751a74b61SMatthew G. Knepley 
223851a74b61SMatthew G. Knepley /*@
223951a74b61SMatthew G. Knepley   DMPlexCreateBallMesh - Creates a simplex mesh on the d-dimensional ball, B^d.
224051a74b61SMatthew G. Knepley 
224151a74b61SMatthew G. Knepley   Collective
224251a74b61SMatthew G. Knepley 
224351a74b61SMatthew G. Knepley   Input Parameters:
224451a74b61SMatthew G. Knepley + comm  - The communicator for the DM object
224551a74b61SMatthew G. Knepley . dim   - The dimension
224651a74b61SMatthew G. Knepley - R     - The radius
224751a74b61SMatthew G. Knepley 
224851a74b61SMatthew G. Knepley   Output Parameter:
224951a74b61SMatthew G. Knepley . dm  - The DM object
225051a74b61SMatthew G. Knepley 
225151a74b61SMatthew G. Knepley   Options Database Keys:
225251a74b61SMatthew G. Knepley - bd_dm_refine - This will refine the surface mesh preserving the sphere geometry
225351a74b61SMatthew G. Knepley 
225451a74b61SMatthew G. Knepley   Level: beginner
225551a74b61SMatthew G. Knepley 
225651a74b61SMatthew G. Knepley .seealso: DMPlexCreateSphereMesh(), DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
225751a74b61SMatthew G. Knepley @*/
225851a74b61SMatthew G. Knepley PetscErrorCode DMPlexCreateBallMesh(MPI_Comm comm, PetscInt dim, PetscReal R, DM *dm)
225951a74b61SMatthew G. Knepley {
226051a74b61SMatthew G. Knepley   PetscErrorCode ierr;
226151a74b61SMatthew G. Knepley 
226251a74b61SMatthew G. Knepley   PetscFunctionBegin;
22639318fe57SMatthew G. Knepley   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
22649318fe57SMatthew G. Knepley   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
22659318fe57SMatthew G. Knepley   ierr = DMPlexCreateBallMesh_Internal(*dm, dim, R);CHKERRQ(ierr);
22662829fed8SMatthew G. Knepley   PetscFunctionReturn(0);
22672829fed8SMatthew G. Knepley }
22682829fed8SMatthew G. Knepley 
22699318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateReferenceCell_Internal(DM rdm, DMPolytopeType ct)
22700a6ba040SMatthew G. Knepley {
22710a6ba040SMatthew G. Knepley   PetscErrorCode ierr;
22720a6ba040SMatthew G. Knepley 
22730a6ba040SMatthew G. Knepley   PetscFunctionBegin;
22749318fe57SMatthew G. Knepley   switch (ct) {
22759318fe57SMatthew G. Knepley     case DM_POLYTOPE_POINT:
22769318fe57SMatthew G. Knepley     {
22779318fe57SMatthew G. Knepley       PetscInt    numPoints[1]        = {1};
22789318fe57SMatthew G. Knepley       PetscInt    coneSize[1]         = {0};
22799318fe57SMatthew G. Knepley       PetscInt    cones[1]            = {0};
22809318fe57SMatthew G. Knepley       PetscInt    coneOrientations[1] = {0};
22819318fe57SMatthew G. Knepley       PetscScalar vertexCoords[1]     = {0.0};
22829318fe57SMatthew G. Knepley 
22839318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 0);CHKERRQ(ierr);
22849318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 0, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
22859318fe57SMatthew G. Knepley     }
22869318fe57SMatthew G. Knepley     break;
22879318fe57SMatthew G. Knepley     case DM_POLYTOPE_SEGMENT:
22889318fe57SMatthew G. Knepley     {
22899318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {2, 1};
22909318fe57SMatthew G. Knepley       PetscInt    coneSize[3]         = {2, 0, 0};
22919318fe57SMatthew G. Knepley       PetscInt    cones[2]            = {1, 2};
22929318fe57SMatthew G. Knepley       PetscInt    coneOrientations[2] = {0, 0};
22939318fe57SMatthew G. Knepley       PetscScalar vertexCoords[2]     = {-1.0,  1.0};
22949318fe57SMatthew G. Knepley 
22959318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 1);CHKERRQ(ierr);
22969318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
22979318fe57SMatthew G. Knepley     }
22989318fe57SMatthew G. Knepley     break;
2299b5a892a1SMatthew G. Knepley     case DM_POLYTOPE_POINT_PRISM_TENSOR:
2300b5a892a1SMatthew G. Knepley     {
2301b5a892a1SMatthew G. Knepley       PetscInt    numPoints[2]        = {2, 1};
2302b5a892a1SMatthew G. Knepley       PetscInt    coneSize[3]         = {2, 0, 0};
2303b5a892a1SMatthew G. Knepley       PetscInt    cones[2]            = {1, 2};
2304b5a892a1SMatthew G. Knepley       PetscInt    coneOrientations[2] = {0, 0};
2305b5a892a1SMatthew G. Knepley       PetscScalar vertexCoords[2]     = {-1.0,  1.0};
2306b5a892a1SMatthew G. Knepley 
2307b5a892a1SMatthew G. Knepley       ierr = DMSetDimension(rdm, 1);CHKERRQ(ierr);
2308b5a892a1SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2309b5a892a1SMatthew G. Knepley     }
2310b5a892a1SMatthew G. Knepley     break;
23119318fe57SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
23129318fe57SMatthew G. Knepley     {
23139318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {3, 1};
23149318fe57SMatthew G. Knepley       PetscInt    coneSize[4]         = {3, 0, 0, 0};
23159318fe57SMatthew G. Knepley       PetscInt    cones[3]            = {1, 2, 3};
23169318fe57SMatthew G. Knepley       PetscInt    coneOrientations[3] = {0, 0, 0};
23179318fe57SMatthew G. Knepley       PetscScalar vertexCoords[6]     = {-1.0, -1.0,  1.0, -1.0,  -1.0, 1.0};
23189318fe57SMatthew G. Knepley 
23199318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 2);CHKERRQ(ierr);
23209318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
23219318fe57SMatthew G. Knepley     }
23229318fe57SMatthew G. Knepley     break;
23239318fe57SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
23249318fe57SMatthew G. Knepley     {
23259318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {4, 1};
23269318fe57SMatthew G. Knepley       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
23279318fe57SMatthew G. Knepley       PetscInt    cones[4]            = {1, 2, 3, 4};
23289318fe57SMatthew G. Knepley       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
23299318fe57SMatthew G. Knepley       PetscScalar vertexCoords[8]     = {-1.0, -1.0,  1.0, -1.0,  1.0, 1.0,  -1.0, 1.0};
23309318fe57SMatthew G. Knepley 
23319318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 2);CHKERRQ(ierr);
23329318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
23339318fe57SMatthew G. Knepley     }
23349318fe57SMatthew G. Knepley     break;
23359318fe57SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
23369318fe57SMatthew G. Knepley     {
23379318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {4, 1};
23389318fe57SMatthew G. Knepley       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
23399318fe57SMatthew G. Knepley       PetscInt    cones[4]            = {1, 2, 3, 4};
23409318fe57SMatthew G. Knepley       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
23419318fe57SMatthew G. Knepley       PetscScalar vertexCoords[8]     = {-1.0, -1.0,  1.0, -1.0,  -1.0, 1.0,  1.0, 1.0};
23429318fe57SMatthew G. Knepley 
23439318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 2);CHKERRQ(ierr);
23449318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
23459318fe57SMatthew G. Knepley     }
23469318fe57SMatthew G. Knepley     break;
23479318fe57SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
23489318fe57SMatthew G. Knepley     {
23499318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {4, 1};
23509318fe57SMatthew G. Knepley       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
2351f0edb160SMatthew G. Knepley       PetscInt    cones[4]            = {1, 2, 3, 4};
23529318fe57SMatthew G. Knepley       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
2353f0edb160SMatthew 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};
23549318fe57SMatthew G. Knepley 
23559318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 3);CHKERRQ(ierr);
23569318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
23579318fe57SMatthew G. Knepley     }
23589318fe57SMatthew G. Knepley     break;
23599318fe57SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
23609318fe57SMatthew G. Knepley     {
23619318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {8, 1};
23629318fe57SMatthew G. Knepley       PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
2363f0edb160SMatthew G. Knepley       PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
23649318fe57SMatthew G. Knepley       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
2365f0edb160SMatthew 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,
23669318fe57SMatthew 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};
23679318fe57SMatthew G. Knepley 
23689318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 3);CHKERRQ(ierr);
23699318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
23709318fe57SMatthew G. Knepley     }
23719318fe57SMatthew G. Knepley     break;
23729318fe57SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM:
23739318fe57SMatthew G. Knepley     {
23749318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {6, 1};
23759318fe57SMatthew G. Knepley       PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
2376f0edb160SMatthew G. Knepley       PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
23779318fe57SMatthew G. Knepley       PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
2378f0edb160SMatthew G. Knepley       PetscScalar vertexCoords[18]    = {-1.0, -1.0, -1.0, -1.0,  1.0, -1.0,   1.0, -1.0, -1.0,
23799318fe57SMatthew G. Knepley                                          -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  -1.0,  1.0,  1.0};
23809318fe57SMatthew G. Knepley 
23819318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 3);CHKERRQ(ierr);
23829318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
23839318fe57SMatthew G. Knepley     }
23849318fe57SMatthew G. Knepley     break;
23859318fe57SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM_TENSOR:
23869318fe57SMatthew G. Knepley     {
23879318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {6, 1};
23889318fe57SMatthew G. Knepley       PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
23899318fe57SMatthew G. Knepley       PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
23909318fe57SMatthew G. Knepley       PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
23919318fe57SMatthew G. Knepley       PetscScalar vertexCoords[18]    = {-1.0, -1.0, -1.0,  1.0, -1.0, -1.0,  -1.0, 1.0, -1.0,
23929318fe57SMatthew G. Knepley                                          -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  -1.0, 1.0,  1.0};
23939318fe57SMatthew G. Knepley 
23949318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 3);CHKERRQ(ierr);
23959318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
23969318fe57SMatthew G. Knepley     }
23979318fe57SMatthew G. Knepley     break;
23989318fe57SMatthew G. Knepley     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
23999318fe57SMatthew G. Knepley     {
24009318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {8, 1};
24019318fe57SMatthew G. Knepley       PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
24029318fe57SMatthew G. Knepley       PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
24039318fe57SMatthew G. Knepley       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
24049318fe57SMatthew 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,
24059318fe57SMatthew 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};
24069318fe57SMatthew G. Knepley 
24079318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 3);CHKERRQ(ierr);
24089318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
24099318fe57SMatthew G. Knepley     }
24109318fe57SMatthew G. Knepley     break;
24119318fe57SMatthew G. Knepley     case DM_POLYTOPE_PYRAMID:
24129318fe57SMatthew G. Knepley     {
24139318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {5, 1};
24149318fe57SMatthew G. Knepley       PetscInt    coneSize[6]         = {5, 0, 0, 0, 0, 0};
2415f0edb160SMatthew G. Knepley       PetscInt    cones[5]            = {1, 2, 3, 4, 5};
24169318fe57SMatthew G. Knepley       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
2417f0edb160SMatthew 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,
24189318fe57SMatthew G. Knepley                                           0.0,  0.0,  1.0};
24199318fe57SMatthew G. Knepley 
24209318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 3);CHKERRQ(ierr);
24219318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
24229318fe57SMatthew G. Knepley     }
24239318fe57SMatthew G. Knepley     break;
24249318fe57SMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) rdm), PETSC_ERR_ARG_WRONG, "Cannot create reference cell for cell type %s", DMPolytopeTypes[ct]);
24259318fe57SMatthew G. Knepley   }
24269318fe57SMatthew G. Knepley   {
24279318fe57SMatthew G. Knepley     PetscInt Nv, v;
24289318fe57SMatthew G. Knepley 
24299318fe57SMatthew G. Knepley     /* Must create the celltype label here so that we do not automatically try to compute the types */
24309318fe57SMatthew G. Knepley     ierr = DMCreateLabel(rdm, "celltype");CHKERRQ(ierr);
24319318fe57SMatthew G. Knepley     ierr = DMPlexSetCellType(rdm, 0, ct);CHKERRQ(ierr);
24329318fe57SMatthew G. Knepley     ierr = DMPlexGetChart(rdm, NULL, &Nv);CHKERRQ(ierr);
24339318fe57SMatthew G. Knepley     for (v = 1; v < Nv; ++v) {ierr = DMPlexSetCellType(rdm, v, DM_POLYTOPE_POINT);CHKERRQ(ierr);}
24349318fe57SMatthew G. Knepley   }
24359318fe57SMatthew G. Knepley   ierr = DMPlexInterpolateInPlace_Internal(rdm);CHKERRQ(ierr);
2436b7f6ffafSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) rdm, DMPolytopeTypes[ct]);CHKERRQ(ierr);
24370a6ba040SMatthew G. Knepley   PetscFunctionReturn(0);
24380a6ba040SMatthew G. Knepley }
24390a6ba040SMatthew G. Knepley 
24409318fe57SMatthew G. Knepley /*@
24419318fe57SMatthew G. Knepley   DMPlexCreateReferenceCell - Create a DMPLEX with the appropriate FEM reference cell
24429318fe57SMatthew G. Knepley 
24439318fe57SMatthew G. Knepley   Collective
24449318fe57SMatthew G. Knepley 
24459318fe57SMatthew G. Knepley   Input Parameters:
24469318fe57SMatthew G. Knepley + comm - The communicator
24479318fe57SMatthew G. Knepley - ct   - The cell type of the reference cell
24489318fe57SMatthew G. Knepley 
24499318fe57SMatthew G. Knepley   Output Parameter:
24509318fe57SMatthew G. Knepley . refdm - The reference cell
24519318fe57SMatthew G. Knepley 
24529318fe57SMatthew G. Knepley   Level: intermediate
24539318fe57SMatthew G. Knepley 
24549318fe57SMatthew G. Knepley .seealso: DMPlexCreateReferenceCell(), DMPlexCreateBoxMesh()
24559318fe57SMatthew G. Knepley @*/
24569318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateReferenceCell(MPI_Comm comm, DMPolytopeType ct, DM *refdm)
24570a6ba040SMatthew G. Knepley {
24580a6ba040SMatthew G. Knepley   PetscErrorCode ierr;
24590a6ba040SMatthew G. Knepley 
24600a6ba040SMatthew G. Knepley   PetscFunctionBegin;
24619318fe57SMatthew G. Knepley   ierr = DMCreate(comm, refdm);CHKERRQ(ierr);
24629318fe57SMatthew G. Knepley   ierr = DMSetType(*refdm, DMPLEX);CHKERRQ(ierr);
24639318fe57SMatthew G. Knepley   ierr = DMPlexCreateReferenceCell_Internal(*refdm, ct);CHKERRQ(ierr);
24649318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
24659318fe57SMatthew G. Knepley }
246679a015ccSMatthew G. Knepley 
24679318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoundaryLabel_Private(DM dm, const char name[])
24689318fe57SMatthew G. Knepley {
24699318fe57SMatthew G. Knepley   DM             plex;
24709318fe57SMatthew G. Knepley   DMLabel        label;
24719318fe57SMatthew G. Knepley   PetscBool      hasLabel;
24729318fe57SMatthew G. Knepley   PetscErrorCode ierr;
24730a6ba040SMatthew G. Knepley 
24749318fe57SMatthew G. Knepley   PetscFunctionBeginUser;
24759318fe57SMatthew G. Knepley   ierr = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
24769318fe57SMatthew G. Knepley   if (hasLabel) PetscFunctionReturn(0);
24779318fe57SMatthew G. Knepley   ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
24789318fe57SMatthew G. Knepley   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
24799318fe57SMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
24809318fe57SMatthew G. Knepley   ierr = DMPlexMarkBoundaryFaces(plex, 1, label);CHKERRQ(ierr);
24819318fe57SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
24829318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
24839318fe57SMatthew G. Knepley }
2484acdc6f61SToby Isaac 
24859318fe57SMatthew G. Knepley const char * const DMPlexShapes[] = {"box", "box_surface", "ball", "sphere", "cylinder", "unknown", "DMPlexShape", "DM_SHAPE_", NULL};
24869318fe57SMatthew G. Knepley 
248761a622f3SMatthew G. Knepley static PetscErrorCode DMPlexCreateFromOptions_Internal(PetscOptionItems *PetscOptionsObject, PetscBool *useCoordSpace, DM dm)
24889318fe57SMatthew G. Knepley {
24899318fe57SMatthew G. Knepley   DMPlexShape    shape = DM_SHAPE_BOX;
24909318fe57SMatthew G. Knepley   DMPolytopeType cell  = DM_POLYTOPE_TRIANGLE;
24919318fe57SMatthew G. Knepley   PetscInt       dim   = 2;
24929318fe57SMatthew G. Knepley   PetscBool      simplex = PETSC_TRUE, interpolate = PETSC_TRUE, adjCone = PETSC_FALSE, adjClosure = PETSC_TRUE, refDomain = PETSC_FALSE;
24939318fe57SMatthew G. Knepley   PetscBool      flg, flg2, fflg, bdfflg;
24949318fe57SMatthew G. Knepley   MPI_Comm       comm;
24959318fe57SMatthew G. Knepley   char           filename[PETSC_MAX_PATH_LEN], bdFilename[PETSC_MAX_PATH_LEN];
24969318fe57SMatthew G. Knepley   PetscErrorCode ierr;
24979318fe57SMatthew G. Knepley 
24989318fe57SMatthew G. Knepley   PetscFunctionBegin;
24999318fe57SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
25009318fe57SMatthew G. Knepley   /* TODO Turn this into a registration interface */
25019318fe57SMatthew G. Knepley   ierr = PetscOptionsString("-dm_plex_filename", "File containing a mesh", "DMPlexCreateFromFile", filename, filename, sizeof(filename), &fflg);CHKERRQ(ierr);
25029318fe57SMatthew G. Knepley   ierr = PetscOptionsString("-dm_plex_boundary_filename", "File containing a mesh boundary", "DMPlexCreateFromFile", bdFilename, bdFilename, sizeof(bdFilename), &bdfflg);CHKERRQ(ierr);
25039318fe57SMatthew G. Knepley   ierr = PetscOptionsEnum("-dm_plex_cell", "Cell shape", "", DMPolytopeTypes, (PetscEnum) cell, (PetscEnum *) &cell, NULL);CHKERRQ(ierr);
25049318fe57SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_reference_cell_domain", "Use a reference cell domain", "", refDomain, &refDomain, NULL);CHKERRQ(ierr);
25059318fe57SMatthew G. Knepley   ierr = PetscOptionsEnum("-dm_plex_shape", "Shape for built-in mesh", "", DMPlexShapes, (PetscEnum) shape, (PetscEnum *) &shape, &flg);CHKERRQ(ierr);
25069318fe57SMatthew G. Knepley   ierr = PetscOptionsBoundedInt("-dm_plex_dim", "Topological dimension of the mesh", "DMGetDimension", dim, &dim, &flg, 0);CHKERRQ(ierr);
25079318fe57SMatthew G. Knepley   if ((dim < 0) || (dim > 3)) SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Dimension %D should be in [1, 3]", dim);
25089318fe57SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_simplex", "Mesh cell shape", "", simplex,  &simplex, &flg);CHKERRQ(ierr);
25099318fe57SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_interpolate", "Flag to create edges and faces automatically", "", interpolate, &interpolate, &flg);CHKERRQ(ierr);
25109318fe57SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_adj_cone", "Set adjacency direction", "DMSetBasicAdjacency", adjCone,  &adjCone, &flg);CHKERRQ(ierr);
25119318fe57SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_adj_closure", "Set adjacency size", "DMSetBasicAdjacency", adjClosure,  &adjClosure, &flg2);CHKERRQ(ierr);
25129318fe57SMatthew G. Knepley   if (flg || flg2) {ierr = DMSetBasicAdjacency(dm, adjCone, adjClosure);CHKERRQ(ierr);}
25139318fe57SMatthew G. Knepley 
251461a622f3SMatthew G. Knepley   switch (cell) {
251561a622f3SMatthew G. Knepley     case DM_POLYTOPE_POINT:
251661a622f3SMatthew G. Knepley     case DM_POLYTOPE_SEGMENT:
251761a622f3SMatthew G. Knepley     case DM_POLYTOPE_POINT_PRISM_TENSOR:
251861a622f3SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
251961a622f3SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
252061a622f3SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
252161a622f3SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
252261a622f3SMatthew G. Knepley       *useCoordSpace = PETSC_TRUE;break;
252361a622f3SMatthew G. Knepley     default: *useCoordSpace = PETSC_FALSE;break;
252461a622f3SMatthew G. Knepley   }
252561a622f3SMatthew G. Knepley 
25269318fe57SMatthew G. Knepley   if (fflg) {
25279318fe57SMatthew G. Knepley     DM dmnew;
25289318fe57SMatthew G. Knepley 
25299318fe57SMatthew G. Knepley     ierr = DMPlexCreateFromFile(PetscObjectComm((PetscObject) dm), filename, interpolate, &dmnew);CHKERRQ(ierr);
25309318fe57SMatthew G. Knepley     ierr = DMPlexReplace_Static(dm, &dmnew);CHKERRQ(ierr);
25319318fe57SMatthew G. Knepley   } else if (refDomain) {
25329318fe57SMatthew G. Knepley     ierr = DMPlexCreateReferenceCell_Internal(dm, cell);CHKERRQ(ierr);
25339318fe57SMatthew G. Knepley   } else if (bdfflg) {
25349318fe57SMatthew G. Knepley     DM bdm, dmnew;
25359318fe57SMatthew G. Knepley 
25369318fe57SMatthew G. Knepley     ierr = DMPlexCreateFromFile(PetscObjectComm((PetscObject) dm), bdFilename, interpolate, &bdm);CHKERRQ(ierr);
25379318fe57SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) bdm, "bd_");CHKERRQ(ierr);
25389318fe57SMatthew G. Knepley     ierr = DMSetFromOptions(bdm);CHKERRQ(ierr);
25399318fe57SMatthew G. Knepley     ierr = DMPlexGenerate(bdm, NULL, interpolate, &dmnew);CHKERRQ(ierr);
25409318fe57SMatthew G. Knepley     ierr = DMDestroy(&bdm);CHKERRQ(ierr);
25419318fe57SMatthew G. Knepley     ierr = DMPlexReplace_Static(dm, &dmnew);CHKERRQ(ierr);
25429318fe57SMatthew G. Knepley   } else {
25439318fe57SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) dm, DMPlexShapes[shape]);CHKERRQ(ierr);
25449318fe57SMatthew G. Knepley     switch (shape) {
25459318fe57SMatthew G. Knepley       case DM_SHAPE_BOX:
25469318fe57SMatthew G. Knepley       {
25479318fe57SMatthew G. Knepley         PetscInt       faces[3] = {0, 0, 0};
25489318fe57SMatthew G. Knepley         PetscReal      lower[3] = {0, 0, 0};
25499318fe57SMatthew G. Knepley         PetscReal      upper[3] = {1, 1, 1};
25509318fe57SMatthew G. Knepley         DMBoundaryType bdt[3]   = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
25519318fe57SMatthew G. Knepley         PetscInt       i, n;
25529318fe57SMatthew G. Knepley 
25539318fe57SMatthew G. Knepley         n    = dim;
25549318fe57SMatthew G. Knepley         for (i = 0; i < dim; ++i) faces[i] = (dim == 1 ? 1 : 4-dim);
25559318fe57SMatthew G. Knepley         ierr = PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg);CHKERRQ(ierr);
25569318fe57SMatthew G. Knepley         n    = 3;
25579318fe57SMatthew G. Knepley         ierr = PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg);CHKERRQ(ierr);
25589318fe57SMatthew G. Knepley         if (flg && (n != dim)) SETERRQ2(comm, PETSC_ERR_ARG_SIZ, "Lower box point had %D values, should have been %D", n, dim);
25599318fe57SMatthew G. Knepley         n    = 3;
25609318fe57SMatthew G. Knepley         ierr = PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg);CHKERRQ(ierr);
25619318fe57SMatthew G. Knepley         if (flg && (n != dim)) SETERRQ2(comm, PETSC_ERR_ARG_SIZ, "Upper box point had %D values, should have been %D", n, dim);
25629318fe57SMatthew G. Knepley         n    = 3;
25639318fe57SMatthew G. Knepley         ierr = PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *) bdt, &n, &flg);CHKERRQ(ierr);
25649318fe57SMatthew G. Knepley         if (flg && (n != dim)) SETERRQ2(comm, PETSC_ERR_ARG_SIZ, "Box boundary types had %D values, should have been %D", n, dim);
25659318fe57SMatthew G. Knepley         switch (cell) {
256661a622f3SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
2567d410b0cfSMatthew G. Knepley             ierr = DMPlexCreateWedgeBoxMesh_Internal(dm, faces, lower, upper, bdt);CHKERRQ(ierr);
2568d410b0cfSMatthew G. Knepley             if (!interpolate) {
2569d410b0cfSMatthew G. Knepley               DM udm;
2570d410b0cfSMatthew G. Knepley 
2571d410b0cfSMatthew G. Knepley               ierr = DMPlexUninterpolate(dm, &udm);CHKERRQ(ierr);
2572d410b0cfSMatthew G. Knepley               ierr = DMPlexReplace_Static(dm, &udm);CHKERRQ(ierr);
2573d410b0cfSMatthew G. Knepley             }
25749318fe57SMatthew G. Knepley             break;
25759318fe57SMatthew G. Knepley           default:
25769318fe57SMatthew G. Knepley             ierr = DMPlexCreateBoxMesh_Internal(dm, dim, simplex, faces, lower, upper, bdt, interpolate);CHKERRQ(ierr);
25779318fe57SMatthew G. Knepley             break;
25789318fe57SMatthew G. Knepley         }
25799318fe57SMatthew G. Knepley       }
25809318fe57SMatthew G. Knepley       break;
25819318fe57SMatthew G. Knepley       case DM_SHAPE_BOX_SURFACE:
25829318fe57SMatthew G. Knepley       {
25839318fe57SMatthew G. Knepley         PetscInt  faces[3] = {0, 0, 0};
25849318fe57SMatthew G. Knepley         PetscReal lower[3] = {0, 0, 0};
25859318fe57SMatthew G. Knepley         PetscReal upper[3] = {1, 1, 1};
25869318fe57SMatthew G. Knepley         PetscInt  i, n;
25879318fe57SMatthew G. Knepley 
25889318fe57SMatthew G. Knepley         n    = dim+1;
25899318fe57SMatthew G. Knepley         for (i = 0; i < dim+1; ++i) faces[i] = (dim+1 == 1 ? 1 : 4-(dim+1));
25909318fe57SMatthew G. Knepley         ierr = PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg);CHKERRQ(ierr);
25919318fe57SMatthew G. Knepley         n    = 3;
25929318fe57SMatthew G. Knepley         ierr = PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg);CHKERRQ(ierr);
25939318fe57SMatthew 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);
25949318fe57SMatthew G. Knepley         n    = 3;
25959318fe57SMatthew G. Knepley         ierr = PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg);CHKERRQ(ierr);
25969318fe57SMatthew 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);
25979318fe57SMatthew G. Knepley         ierr = DMPlexCreateBoxSurfaceMesh_Internal(dm, dim+1, faces, lower, upper, interpolate);CHKERRQ(ierr);
25989318fe57SMatthew G. Knepley       }
25999318fe57SMatthew G. Knepley       break;
26009318fe57SMatthew G. Knepley       case DM_SHAPE_SPHERE:
26019318fe57SMatthew G. Knepley       {
26029318fe57SMatthew G. Knepley         PetscReal R = 1.0;
26039318fe57SMatthew G. Knepley 
26049318fe57SMatthew G. Knepley         ierr = PetscOptionsReal("-dm_plex_sphere_radius", "Radius of the sphere", "", R,  &R, &flg);CHKERRQ(ierr);
26059318fe57SMatthew G. Knepley         ierr = DMPlexCreateSphereMesh_Internal(dm, dim, simplex, R);CHKERRQ(ierr);
26069318fe57SMatthew G. Knepley       }
26079318fe57SMatthew G. Knepley       break;
26089318fe57SMatthew G. Knepley       case DM_SHAPE_BALL:
26099318fe57SMatthew G. Knepley       {
26109318fe57SMatthew G. Knepley         PetscReal R = 1.0;
26119318fe57SMatthew G. Knepley 
26129318fe57SMatthew G. Knepley         ierr = PetscOptionsReal("-dm_plex_ball_radius", "Radius of the ball", "", R,  &R, &flg);CHKERRQ(ierr);
26139318fe57SMatthew G. Knepley         ierr = DMPlexCreateBallMesh_Internal(dm, dim, R);CHKERRQ(ierr);
26149318fe57SMatthew G. Knepley       }
26159318fe57SMatthew G. Knepley       break;
26169318fe57SMatthew G. Knepley       case DM_SHAPE_CYLINDER:
26179318fe57SMatthew G. Knepley       {
26189318fe57SMatthew G. Knepley         DMBoundaryType bdt = DM_BOUNDARY_NONE;
26199318fe57SMatthew G. Knepley         PetscInt       Nw  = 6;
26209318fe57SMatthew G. Knepley 
26219318fe57SMatthew G. Knepley         ierr = PetscOptionsEnum("-dm_plex_cylinder_bd", "Boundary type in the z direction", "", DMBoundaryTypes, (PetscEnum) bdt, (PetscEnum *) &bdt, NULL);CHKERRQ(ierr);
262261a622f3SMatthew G. Knepley         ierr = PetscOptionsInt("-dm_plex_cylinder_num_wedges", "Number of wedges around the cylinder", "", Nw, &Nw, NULL);CHKERRQ(ierr);
26239318fe57SMatthew G. Knepley         switch (cell) {
262461a622f3SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
26259318fe57SMatthew G. Knepley             ierr = DMPlexCreateWedgeCylinderMesh_Internal(dm, Nw, interpolate);CHKERRQ(ierr);
26269318fe57SMatthew G. Knepley             break;
26279318fe57SMatthew G. Knepley           default:
26289318fe57SMatthew G. Knepley             ierr = DMPlexCreateHexCylinderMesh_Internal(dm, bdt);CHKERRQ(ierr);
26299318fe57SMatthew G. Knepley             break;
26309318fe57SMatthew G. Knepley         }
26319318fe57SMatthew G. Knepley       }
26329318fe57SMatthew G. Knepley       break;
26339318fe57SMatthew G. Knepley       default: SETERRQ1(comm, PETSC_ERR_SUP, "Domain shape %s is unsupported", DMPlexShapes[shape]);
26349318fe57SMatthew G. Knepley     }
26359318fe57SMatthew G. Knepley   }
26369318fe57SMatthew G. Knepley   ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE);CHKERRQ(ierr);
26370a6ba040SMatthew G. Knepley   PetscFunctionReturn(0);
26380a6ba040SMatthew G. Knepley }
26390a6ba040SMatthew G. Knepley 
264047920aaeSMatthew G. Knepley PetscErrorCode DMSetFromOptions_NonRefinement_Plex(PetscOptionItems *PetscOptionsObject, DM dm)
26410a6ba040SMatthew G. Knepley {
26420a6ba040SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2643c0f0dcc3SMatthew G. Knepley   PetscBool      flg;
26449318fe57SMatthew G. Knepley   char           bdLabel[PETSC_MAX_PATH_LEN];
26450a6ba040SMatthew G. Knepley   PetscErrorCode ierr;
26460a6ba040SMatthew G. Knepley 
26470a6ba040SMatthew G. Knepley   PetscFunctionBegin;
26480a6ba040SMatthew G. Knepley   /* Handle viewing */
26490c77aedcSMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMPlexMatSetClosure", PETSC_FALSE, &mesh->printSetValues, NULL);CHKERRQ(ierr);
26505a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-dm_plex_print_fem", "Debug output level all fem computations", "DMPlexSNESComputeResidualFEM", 0, &mesh->printFEM, NULL,0);CHKERRQ(ierr);
26510c77aedcSMatthew G. Knepley   ierr = PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMPlexSNESComputeResidualFEM", mesh->printTol, &mesh->printTol, NULL);CHKERRQ(ierr);
26525a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-dm_plex_print_l2", "Debug output level all L2 diff computations", "DMComputeL2Diff", 0, &mesh->printL2, NULL,0);CHKERRQ(ierr);
2653c0f0dcc3SMatthew G. Knepley   ierr = DMMonitorSetFromOptions(dm, "-dm_plex_monitor_throughput", "Monitor the simulation throughput", "DMPlexMonitorThroughput", DMPlexMonitorThroughput, NULL, &flg);CHKERRQ(ierr);
2654c0f0dcc3SMatthew G. Knepley   if (flg) {ierr = PetscLogDefaultBegin();CHKERRQ(ierr);}
26559318fe57SMatthew G. Knepley   /* Labeling */
26569318fe57SMatthew G. Knepley   ierr = PetscOptionsString("-dm_plex_boundary_label", "Label to mark the mesh boundary", "", bdLabel, bdLabel, sizeof(bdLabel), &flg);CHKERRQ(ierr);
26579318fe57SMatthew G. Knepley   if (flg) {ierr = DMPlexCreateBoundaryLabel_Private(dm, bdLabel);CHKERRQ(ierr);}
2658953fc75cSMatthew G. Knepley   /* Point Location */
26590c77aedcSMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_hash_location", "Use grid hashing for point location", "DMInterpolate", PETSC_FALSE, &mesh->useHashLocation, NULL);CHKERRQ(ierr);
26600848f4b5SMatthew G. Knepley   /* Partitioning and distribution */
266198ba2d7fSLawrence 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);
26622e62ab5aSMatthew G. Knepley   /* Generation and remeshing */
26630c77aedcSMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_remesh_bd", "Allow changes to the boundary on remeshing", "DMAdapt", PETSC_FALSE, &mesh->remeshBd, NULL);CHKERRQ(ierr);
2664b29cfa1cSToby Isaac   /* Projection behavior */
26655a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-dm_plex_max_projection_height", "Maxmimum mesh point height used to project locally", "DMPlexSetMaxProjectionHeight", 0, &mesh->maxProjectionHeight, NULL,0);CHKERRQ(ierr);
266664141f95SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_regular_refinement", "Use special nested projection algorithm for regular refinement", "DMPlexSetRegularRefinement", mesh->regularRefinement, &mesh->regularRefinement, NULL);CHKERRQ(ierr);
2667f12cf164SMatthew G. Knepley   /* Checking structure */
2668f12cf164SMatthew G. Knepley   {
2669e902f1eaSVaclav Hapla     PetscBool   flg = PETSC_FALSE, flg2 = PETSC_FALSE, all = PETSC_FALSE;
2670f12cf164SMatthew G. Knepley 
2671e902f1eaSVaclav Hapla     ierr = PetscOptionsBool("-dm_plex_check_all", "Perform all checks", NULL, PETSC_FALSE, &all, &flg2);CHKERRQ(ierr);
2672f12cf164SMatthew 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);
2673e902f1eaSVaclav Hapla     if (all || (flg && flg2)) {ierr = DMPlexCheckSymmetry(dm);CHKERRQ(ierr);}
267425c50c26SVaclav 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);
2675e902f1eaSVaclav Hapla     if (all || (flg && flg2)) {ierr = DMPlexCheckSkeleton(dm, 0);CHKERRQ(ierr);}
267625c50c26SVaclav 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);
2677e902f1eaSVaclav Hapla     if (all || (flg && flg2)) {ierr = DMPlexCheckFaces(dm, 0);CHKERRQ(ierr);}
2678f12cf164SMatthew G. Knepley     ierr = PetscOptionsBool("-dm_plex_check_geometry", "Check that cells have positive volume", "DMPlexCheckGeometry", PETSC_FALSE, &flg, &flg2);CHKERRQ(ierr);
2679e902f1eaSVaclav Hapla     if (all || (flg && flg2)) {ierr = DMPlexCheckGeometry(dm);CHKERRQ(ierr);}
268075ebff7aSVaclav Hapla     ierr = PetscOptionsBool("-dm_plex_check_pointsf", "Check some necessary conditions for PointSF", "DMPlexCheckPointSF", PETSC_FALSE, &flg, &flg2);CHKERRQ(ierr);
2681e902f1eaSVaclav Hapla     if (all || (flg && flg2)) {ierr = DMPlexCheckPointSF(dm);CHKERRQ(ierr);}
268275ebff7aSVaclav 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);
2683e902f1eaSVaclav Hapla     if (all || (flg && flg2)) {ierr = DMPlexCheckInterfaceCones(dm);CHKERRQ(ierr);}
2684412e9a14SMatthew G. Knepley     ierr = PetscOptionsBool("-dm_plex_check_cell_shape", "Check cell shape", "DMPlexCheckCellShape", PETSC_FALSE, &flg, &flg2);CHKERRQ(ierr);
2685412e9a14SMatthew G. Knepley     if (flg && flg2) {ierr = DMPlexCheckCellShape(dm, PETSC_TRUE, PETSC_DETERMINE);CHKERRQ(ierr);}
2686f12cf164SMatthew G. Knepley   }
26879318fe57SMatthew G. Knepley   {
26889318fe57SMatthew G. Knepley     PetscReal scale = 1.0;
26894f3833eaSMatthew G. Knepley 
26909318fe57SMatthew G. Knepley     ierr = PetscOptionsReal("-dm_plex_scale", "Scale factor for mesh coordinates", "DMPlexScale", scale, &scale, &flg);CHKERRQ(ierr);
26919318fe57SMatthew G. Knepley     if (flg) {
26929318fe57SMatthew G. Knepley       Vec coordinates, coordinatesLocal;
26939318fe57SMatthew G. Knepley 
26949318fe57SMatthew G. Knepley       ierr = DMGetCoordinates(dm, &coordinates);CHKERRQ(ierr);
26959318fe57SMatthew G. Knepley       ierr = DMGetCoordinatesLocal(dm, &coordinatesLocal);CHKERRQ(ierr);
26969318fe57SMatthew G. Knepley       ierr = VecScale(coordinates, scale);CHKERRQ(ierr);
26979318fe57SMatthew G. Knepley       ierr = VecScale(coordinatesLocal, scale);CHKERRQ(ierr);
26989318fe57SMatthew G. Knepley     }
26999318fe57SMatthew G. Knepley   }
27004f3833eaSMatthew G. Knepley   ierr = PetscPartitionerSetFromOptions(mesh->partitioner);CHKERRQ(ierr);
270168d4fef7SMatthew G. Knepley   PetscFunctionReturn(0);
270268d4fef7SMatthew G. Knepley }
270368d4fef7SMatthew G. Knepley 
270446fa42a0SMatthew G. Knepley static PetscErrorCode DMSetFromOptions_Plex(PetscOptionItems *PetscOptionsObject,DM dm)
270568d4fef7SMatthew G. Knepley {
2706d410b0cfSMatthew G. Knepley   PetscReal      volume = -1.0;
27079318fe57SMatthew G. Knepley   PetscInt       prerefine = 0, refine = 0, r, coarsen = 0, overlap = 0, extLayers = 0, dim;
2708d410b0cfSMatthew G. Knepley   PetscBool      uniformOrig, created = PETSC_FALSE, uniform = PETSC_TRUE, distribute = PETSC_FALSE, interpolate = PETSC_TRUE, coordSpace = PETSC_TRUE, remap = PETSC_TRUE, ghostCells = PETSC_FALSE, isHierarchy, ignoreModel = PETSC_FALSE, flg;
270968d4fef7SMatthew G. Knepley   PetscErrorCode ierr;
271068d4fef7SMatthew G. Knepley 
271168d4fef7SMatthew G. Knepley   PetscFunctionBegin;
2712064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
27131a1499c8SBarry Smith   ierr = PetscOptionsHead(PetscOptionsObject,"DMPlex Options");CHKERRQ(ierr);
27149318fe57SMatthew G. Knepley   /* Handle automatic creation */
27159318fe57SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
271661a622f3SMatthew G. Knepley   if (dim < 0) {ierr = DMPlexCreateFromOptions_Internal(PetscOptionsObject, &coordSpace, dm);CHKERRQ(ierr);created = PETSC_TRUE;}
2717d89e6e46SMatthew G. Knepley   /* Handle interpolation before distribution */
2718d89e6e46SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_interpolate_pre", "Flag to interpolate mesh before distribution", "", interpolate, &interpolate, &flg);CHKERRQ(ierr);
2719d89e6e46SMatthew G. Knepley   if (flg) {
2720d89e6e46SMatthew G. Knepley     DMPlexInterpolatedFlag interpolated;
2721d89e6e46SMatthew G. Knepley 
2722d89e6e46SMatthew G. Knepley     ierr = DMPlexIsInterpolated(dm, &interpolated);CHKERRQ(ierr);
2723d89e6e46SMatthew G. Knepley     if (interpolated == DMPLEX_INTERPOLATED_FULL && !interpolate) {
2724d89e6e46SMatthew G. Knepley       DM udm;
2725d89e6e46SMatthew G. Knepley 
2726d89e6e46SMatthew G. Knepley       ierr = DMPlexUninterpolate(dm, &udm);CHKERRQ(ierr);
2727d89e6e46SMatthew G. Knepley       ierr = DMPlexReplace_Static(dm, &udm);CHKERRQ(ierr);
2728d89e6e46SMatthew G. Knepley     } else if (interpolated != DMPLEX_INTERPOLATED_FULL && interpolate) {
2729d89e6e46SMatthew G. Knepley       DM idm;
2730d89e6e46SMatthew G. Knepley 
2731d89e6e46SMatthew G. Knepley       ierr = DMPlexInterpolate(dm, &idm);CHKERRQ(ierr);
2732d89e6e46SMatthew G. Knepley       ierr = DMPlexReplace_Static(dm, &idm);CHKERRQ(ierr);
2733d89e6e46SMatthew G. Knepley     }
2734d89e6e46SMatthew G. Knepley   }
27359b44eab4SMatthew G. Knepley   /* Handle DMPlex refinement before distribution */
2736c1cad2e7SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_refine_ignore_model", "Flag to ignore the geometry model when refining", "DMCreate", ignoreModel, &ignoreModel, &flg);CHKERRQ(ierr);
2737c1cad2e7SMatthew G. Knepley   if (flg) {((DM_Plex *) dm->data)->ignoreModel = ignoreModel;}
2738250712c9SMatthew G. Knepley   ierr = DMPlexGetRefinementUniform(dm, &uniformOrig);CHKERRQ(ierr);
27399318fe57SMatthew G. Knepley   ierr = PetscOptionsBoundedInt("-dm_refine_pre", "The number of refinements before distribution", "DMCreate", prerefine, &prerefine, NULL,0);CHKERRQ(ierr);
274061a622f3SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_refine_remap_pre", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL);CHKERRQ(ierr);
2741250712c9SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_refine_uniform_pre", "Flag for uniform refinement before distribution", "DMCreate", uniform, &uniform, &flg);CHKERRQ(ierr);
2742250712c9SMatthew G. Knepley   if (flg) {ierr = DMPlexSetRefinementUniform(dm, uniform);CHKERRQ(ierr);}
2743250712c9SMatthew G. Knepley   ierr = PetscOptionsReal("-dm_refine_volume_limit_pre", "The maximum cell volume after refinement before distribution", "DMCreate", volume, &volume, &flg);CHKERRQ(ierr);
27449318fe57SMatthew G. Knepley   if (flg) {
27459318fe57SMatthew G. Knepley     ierr = DMPlexSetRefinementUniform(dm, PETSC_FALSE);CHKERRQ(ierr);
27469318fe57SMatthew G. Knepley     ierr = DMPlexSetRefinementLimit(dm, volume);CHKERRQ(ierr);
27479318fe57SMatthew G. Knepley     prerefine = PetscMax(prerefine, 1);
27489318fe57SMatthew G. Knepley   }
27499b44eab4SMatthew G. Knepley   for (r = 0; r < prerefine; ++r) {
27509b44eab4SMatthew G. Knepley     DM             rdm;
27519b44eab4SMatthew G. Knepley     PetscPointFunc coordFunc = ((DM_Plex*) dm->data)->coordFunc;
27529b44eab4SMatthew G. Knepley 
27539b44eab4SMatthew G. Knepley     ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
27549b44eab4SMatthew G. Knepley     ierr = DMRefine(dm, PetscObjectComm((PetscObject) dm), &rdm);CHKERRQ(ierr);
27559318fe57SMatthew G. Knepley     ierr = DMPlexReplace_Static(dm, &rdm);CHKERRQ(ierr);
27569b44eab4SMatthew G. Knepley     ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
275761a622f3SMatthew G. Knepley     if (coordFunc && remap) {
27589b44eab4SMatthew G. Knepley       ierr = DMPlexRemapGeometry(dm, 0.0, coordFunc);CHKERRQ(ierr);
27599b44eab4SMatthew G. Knepley       ((DM_Plex*) dm->data)->coordFunc = coordFunc;
27609b44eab4SMatthew G. Knepley     }
27619b44eab4SMatthew G. Knepley   }
2762250712c9SMatthew G. Knepley   ierr = DMPlexSetRefinementUniform(dm, uniformOrig);CHKERRQ(ierr);
27639318fe57SMatthew G. Knepley   /* Handle DMPlex extrusion before distribution */
2764d410b0cfSMatthew G. Knepley   ierr = PetscOptionsBoundedInt("-dm_extrude", "The number of layers to extrude", "", extLayers, &extLayers, NULL, 0);CHKERRQ(ierr);
27659318fe57SMatthew G. Knepley   if (extLayers) {
27669318fe57SMatthew G. Knepley     DM edm;
27679318fe57SMatthew G. Knepley 
2768d410b0cfSMatthew G. Knepley     ierr = DMExtrude(dm, extLayers, &edm);CHKERRQ(ierr);
27699318fe57SMatthew G. Knepley     ierr = DMPlexReplace_Static(dm, &edm);CHKERRQ(ierr);
2770d410b0cfSMatthew G. Knepley     ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
2771d410b0cfSMatthew G. Knepley     extLayers = 0;
27729318fe57SMatthew G. Knepley   }
2773d410b0cfSMatthew G. Knepley   /* TODO Old-style extrusion which can be removed */
2774d410b0cfSMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_interpolate", "Flag to create edges and faces automatically", "", interpolate, &interpolate, NULL);CHKERRQ(ierr);
27759b44eab4SMatthew G. Knepley   /* Handle DMPlex distribution */
27769b44eab4SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_distribute", "Flag to redistribute a mesh among processes", "DMCreate", distribute, &distribute, NULL);CHKERRQ(ierr);
27779b44eab4SMatthew G. Knepley   ierr = PetscOptionsBoundedInt("-dm_distribute_overlap", "The size of the overlap halo", "DMCreate", overlap, &overlap, NULL, 0);CHKERRQ(ierr);
27789b44eab4SMatthew G. Knepley   if (distribute) {
27799b44eab4SMatthew G. Knepley     DM               pdm = NULL;
27809b44eab4SMatthew G. Knepley     PetscPartitioner part;
27819b44eab4SMatthew G. Knepley 
27829b44eab4SMatthew G. Knepley     ierr = DMPlexGetPartitioner(dm, &part);CHKERRQ(ierr);
27839b44eab4SMatthew G. Knepley     ierr = PetscPartitionerSetFromOptions(part);CHKERRQ(ierr);
27849b44eab4SMatthew G. Knepley     ierr = DMPlexDistribute(dm, overlap, NULL, &pdm);CHKERRQ(ierr);
27859b44eab4SMatthew G. Knepley     if (pdm) {
27869318fe57SMatthew G. Knepley       ierr = DMPlexReplace_Static(dm, &pdm);CHKERRQ(ierr);
27879b44eab4SMatthew G. Knepley     }
27889b44eab4SMatthew G. Knepley   }
27899318fe57SMatthew G. Knepley   /* Create coordinate space */
27909318fe57SMatthew G. Knepley   if (created) {
279161a622f3SMatthew G. Knepley     DM_Plex  *mesh = (DM_Plex *) dm->data;
27929318fe57SMatthew G. Knepley     PetscInt  degree = 1;
279361a622f3SMatthew G. Knepley     PetscBool periodic, flg;
27949318fe57SMatthew G. Knepley 
279561a622f3SMatthew G. Knepley     ierr = PetscOptionsBool("-dm_coord_space", "Use an FEM space for coordinates", "", coordSpace, &coordSpace, &flg);CHKERRQ(ierr);
27969318fe57SMatthew G. Knepley     ierr = PetscOptionsInt("-dm_coord_petscspace_degree", "FEM degree for coordinate space", "", degree, &degree, NULL);CHKERRQ(ierr);
279761a622f3SMatthew G. Knepley     if (coordSpace) {ierr = DMPlexCreateCoordinateSpace(dm, degree, mesh->coordFunc);CHKERRQ(ierr);}
279861a622f3SMatthew G. Knepley     if (flg && !coordSpace) {
279961a622f3SMatthew G. Knepley       DM           cdm;
280061a622f3SMatthew G. Knepley       PetscDS      cds;
280161a622f3SMatthew G. Knepley       PetscObject  obj;
280261a622f3SMatthew G. Knepley       PetscClassId id;
280361a622f3SMatthew G. Knepley 
280461a622f3SMatthew G. Knepley       ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
280561a622f3SMatthew G. Knepley       ierr = DMGetDS(cdm, &cds);CHKERRQ(ierr);
280661a622f3SMatthew G. Knepley       ierr = PetscDSGetDiscretization(cds, 0, &obj);CHKERRQ(ierr);
280761a622f3SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
280861a622f3SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
280961a622f3SMatthew G. Knepley         PetscContainer dummy;
281061a622f3SMatthew G. Knepley 
281161a622f3SMatthew G. Knepley         ierr = PetscContainerCreate(PETSC_COMM_SELF, &dummy);CHKERRQ(ierr);
281261a622f3SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) dummy, "coordinates");CHKERRQ(ierr);
281361a622f3SMatthew G. Knepley         ierr = DMSetField(cdm, 0, NULL, (PetscObject) dummy);CHKERRQ(ierr);
281461a622f3SMatthew G. Knepley         ierr = PetscContainerDestroy(&dummy);CHKERRQ(ierr);
281561a622f3SMatthew G. Knepley         ierr = DMClearDS(cdm);CHKERRQ(ierr);
281661a622f3SMatthew G. Knepley       }
281761a622f3SMatthew G. Knepley       mesh->coordFunc = NULL;
281861a622f3SMatthew G. Knepley     }
28199318fe57SMatthew G. Knepley     ierr = DMLocalizeCoordinates(dm);CHKERRQ(ierr);
282061a622f3SMatthew G. Knepley     ierr = DMGetPeriodicity(dm, &periodic, NULL, NULL, NULL);CHKERRQ(ierr);
282161a622f3SMatthew G. Knepley     if (periodic) {ierr = DMSetPeriodicity(dm, PETSC_TRUE, NULL, NULL, NULL);CHKERRQ(ierr);}
28229318fe57SMatthew G. Knepley   }
282368d4fef7SMatthew G. Knepley   /* Handle DMPlex refinement */
282461a622f3SMatthew G. Knepley   remap = PETSC_TRUE;
28255a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-dm_refine", "The number of uniform refinements", "DMCreate", refine, &refine, NULL,0);CHKERRQ(ierr);
282661a622f3SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_refine_remap", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL);CHKERRQ(ierr);
28275a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-dm_refine_hierarchy", "The number of uniform refinements", "DMCreate", refine, &refine, &isHierarchy,0);CHKERRQ(ierr);
2828b6a0289aSMatthew G. Knepley   if (refine) {ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE);CHKERRQ(ierr);}
282968d4fef7SMatthew G. Knepley   if (refine && isHierarchy) {
2830acdc6f61SToby Isaac     DM *dms, coarseDM;
283168d4fef7SMatthew G. Knepley 
2832acdc6f61SToby Isaac     ierr = DMGetCoarseDM(dm, &coarseDM);CHKERRQ(ierr);
2833acdc6f61SToby Isaac     ierr = PetscObjectReference((PetscObject)coarseDM);CHKERRQ(ierr);
283468d4fef7SMatthew G. Knepley     ierr = PetscMalloc1(refine,&dms);CHKERRQ(ierr);
283568d4fef7SMatthew G. Knepley     ierr = DMRefineHierarchy(dm, refine, dms);CHKERRQ(ierr);
283668d4fef7SMatthew G. Knepley     /* Total hack since we do not pass in a pointer */
283768d4fef7SMatthew G. Knepley     ierr = DMPlexSwap_Static(dm, dms[refine-1]);CHKERRQ(ierr);
283868d4fef7SMatthew G. Knepley     if (refine == 1) {
2839a8fb8f29SToby Isaac       ierr = DMSetCoarseDM(dm, dms[0]);CHKERRQ(ierr);
28400aef6b92SMatthew G. Knepley       ierr = DMPlexSetRegularRefinement(dm, PETSC_TRUE);CHKERRQ(ierr);
284168d4fef7SMatthew G. Knepley     } else {
2842a8fb8f29SToby Isaac       ierr = DMSetCoarseDM(dm, dms[refine-2]);CHKERRQ(ierr);
28430aef6b92SMatthew G. Knepley       ierr = DMPlexSetRegularRefinement(dm, PETSC_TRUE);CHKERRQ(ierr);
2844a8fb8f29SToby Isaac       ierr = DMSetCoarseDM(dms[0], dms[refine-1]);CHKERRQ(ierr);
28450aef6b92SMatthew G. Knepley       ierr = DMPlexSetRegularRefinement(dms[0], PETSC_TRUE);CHKERRQ(ierr);
284668d4fef7SMatthew G. Knepley     }
2847acdc6f61SToby Isaac     ierr = DMSetCoarseDM(dms[refine-1], coarseDM);CHKERRQ(ierr);
2848acdc6f61SToby Isaac     ierr = PetscObjectDereference((PetscObject)coarseDM);CHKERRQ(ierr);
284968d4fef7SMatthew G. Knepley     /* Free DMs */
285068d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
2851547f7119SMatthew G. Knepley       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]);CHKERRQ(ierr);
285268d4fef7SMatthew G. Knepley       ierr = DMDestroy(&dms[r]);CHKERRQ(ierr);
285368d4fef7SMatthew G. Knepley     }
285468d4fef7SMatthew G. Knepley     ierr = PetscFree(dms);CHKERRQ(ierr);
285568d4fef7SMatthew G. Knepley   } else {
285668d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
28579318fe57SMatthew G. Knepley       DM             rdm;
285851a74b61SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex*) dm->data)->coordFunc;
285968d4fef7SMatthew G. Knepley 
28601a1499c8SBarry Smith       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
28619318fe57SMatthew G. Knepley       ierr = DMRefine(dm, PetscObjectComm((PetscObject) dm), &rdm);CHKERRQ(ierr);
286268d4fef7SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
28639318fe57SMatthew G. Knepley       ierr = DMPlexReplace_Static(dm, &rdm);CHKERRQ(ierr);
2864547f7119SMatthew G. Knepley       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
286561a622f3SMatthew G. Knepley       if (coordFunc && remap) {
286651a74b61SMatthew G. Knepley         ierr = DMPlexRemapGeometry(dm, 0.0, coordFunc);CHKERRQ(ierr);
286751a74b61SMatthew G. Knepley         ((DM_Plex*) dm->data)->coordFunc = coordFunc;
286851a74b61SMatthew G. Knepley       }
286968d4fef7SMatthew G. Knepley     }
287068d4fef7SMatthew G. Knepley   }
28713cf6fe12SMatthew G. Knepley   /* Handle DMPlex coarsening */
28725a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-dm_coarsen", "Coarsen the mesh", "DMCreate", coarsen, &coarsen, NULL,0);CHKERRQ(ierr);
28735a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-dm_coarsen_hierarchy", "The number of coarsenings", "DMCreate", coarsen, &coarsen, &isHierarchy,0);CHKERRQ(ierr);
2874b653a561SMatthew G. Knepley   if (coarsen && isHierarchy) {
2875b653a561SMatthew G. Knepley     DM *dms;
2876b653a561SMatthew G. Knepley 
2877b653a561SMatthew G. Knepley     ierr = PetscMalloc1(coarsen, &dms);CHKERRQ(ierr);
2878b653a561SMatthew G. Knepley     ierr = DMCoarsenHierarchy(dm, coarsen, dms);CHKERRQ(ierr);
2879b653a561SMatthew G. Knepley     /* Free DMs */
2880b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
2881547f7119SMatthew G. Knepley       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]);CHKERRQ(ierr);
2882b653a561SMatthew G. Knepley       ierr = DMDestroy(&dms[r]);CHKERRQ(ierr);
2883b653a561SMatthew G. Knepley     }
2884b653a561SMatthew G. Knepley     ierr = PetscFree(dms);CHKERRQ(ierr);
2885b653a561SMatthew G. Knepley   } else {
2886b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
28879318fe57SMatthew G. Knepley       DM             cdm;
28889318fe57SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex*) dm->data)->coordFunc;
28893cf6fe12SMatthew G. Knepley 
28903cf6fe12SMatthew G. Knepley       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
28919318fe57SMatthew G. Knepley       ierr = DMCoarsen(dm, PetscObjectComm((PetscObject) dm), &cdm);CHKERRQ(ierr);
28923cf6fe12SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
28939318fe57SMatthew G. Knepley       ierr = DMPlexReplace_Static(dm, &cdm);CHKERRQ(ierr);
2894547f7119SMatthew G. Knepley       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
28959318fe57SMatthew G. Knepley       if (coordFunc) {
28969318fe57SMatthew G. Knepley         ierr = DMPlexRemapGeometry(dm, 0.0, coordFunc);CHKERRQ(ierr);
28979318fe57SMatthew G. Knepley         ((DM_Plex*) dm->data)->coordFunc = coordFunc;
28989318fe57SMatthew G. Knepley       }
28993cf6fe12SMatthew G. Knepley     }
2900b653a561SMatthew G. Knepley   }
2901909dfd52SMatthew G. Knepley   /* Handle ghost cells */
2902909dfd52SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_create_fv_ghost_cells", "Flag to create finite volume ghost cells on the boundary", "DMCreate", ghostCells, &ghostCells, NULL);CHKERRQ(ierr);
2903909dfd52SMatthew G. Knepley   if (ghostCells) {
2904909dfd52SMatthew G. Knepley     DM   gdm;
2905909dfd52SMatthew G. Knepley     char lname[PETSC_MAX_PATH_LEN];
2906909dfd52SMatthew G. Knepley 
2907909dfd52SMatthew G. Knepley     lname[0] = '\0';
2908909dfd52SMatthew G. Knepley     ierr = PetscOptionsString("-dm_plex_fv_ghost_cells_label", "Label name for ghost cells boundary", "DMCreate", lname, lname, sizeof(lname), &flg);CHKERRQ(ierr);
2909909dfd52SMatthew G. Knepley     ierr = DMPlexConstructGhostCells(dm, flg ? lname : NULL, NULL, &gdm);CHKERRQ(ierr);
2910909dfd52SMatthew G. Knepley     ierr = DMPlexReplace_Static(dm, &gdm);CHKERRQ(ierr);
2911909dfd52SMatthew G. Knepley   }
29123cf6fe12SMatthew G. Knepley   /* Handle */
29131a1499c8SBarry Smith   ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
29140a6ba040SMatthew G. Knepley   ierr = PetscOptionsTail();CHKERRQ(ierr);
29150a6ba040SMatthew G. Knepley   PetscFunctionReturn(0);
29160a6ba040SMatthew G. Knepley }
29170a6ba040SMatthew G. Knepley 
2918552f7358SJed Brown static PetscErrorCode DMCreateGlobalVector_Plex(DM dm,Vec *vec)
2919552f7358SJed Brown {
2920552f7358SJed Brown   PetscErrorCode ierr;
2921552f7358SJed Brown 
2922552f7358SJed Brown   PetscFunctionBegin;
2923552f7358SJed Brown   ierr = DMCreateGlobalVector_Section_Private(dm,vec);CHKERRQ(ierr);
2924552f7358SJed Brown   /* ierr = VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM);CHKERRQ(ierr); */
2925552f7358SJed Brown   ierr = VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex);CHKERRQ(ierr);
2926d930f514SMatthew G. Knepley   ierr = VecSetOperation(*vec, VECOP_VIEWNATIVE, (void (*)(void)) VecView_Plex_Native);CHKERRQ(ierr);
29272c40f234SMatthew G. Knepley   ierr = VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex);CHKERRQ(ierr);
2928d930f514SMatthew G. Knepley   ierr = VecSetOperation(*vec, VECOP_LOADNATIVE, (void (*)(void)) VecLoad_Plex_Native);CHKERRQ(ierr);
2929552f7358SJed Brown   PetscFunctionReturn(0);
2930552f7358SJed Brown }
2931552f7358SJed Brown 
2932552f7358SJed Brown static PetscErrorCode DMCreateLocalVector_Plex(DM dm,Vec *vec)
2933552f7358SJed Brown {
2934552f7358SJed Brown   PetscErrorCode ierr;
2935552f7358SJed Brown 
2936552f7358SJed Brown   PetscFunctionBegin;
2937552f7358SJed Brown   ierr = DMCreateLocalVector_Section_Private(dm,vec);CHKERRQ(ierr);
2938552f7358SJed Brown   ierr = VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex_Local);CHKERRQ(ierr);
29392c40f234SMatthew G. Knepley   ierr = VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex_Local);CHKERRQ(ierr);
2940552f7358SJed Brown   PetscFunctionReturn(0);
2941552f7358SJed Brown }
2942552f7358SJed Brown 
2943793f3fe5SMatthew G. Knepley static PetscErrorCode DMGetDimPoints_Plex(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
2944793f3fe5SMatthew G. Knepley {
2945793f3fe5SMatthew G. Knepley   PetscInt       depth, d;
2946793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
2947793f3fe5SMatthew G. Knepley 
2948793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
2949793f3fe5SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2950793f3fe5SMatthew G. Knepley   if (depth == 1) {
2951793f3fe5SMatthew G. Knepley     ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
2952793f3fe5SMatthew G. Knepley     if (dim == 0)      {ierr = DMPlexGetDepthStratum(dm, dim, pStart, pEnd);CHKERRQ(ierr);}
2953793f3fe5SMatthew G. Knepley     else if (dim == d) {ierr = DMPlexGetDepthStratum(dm, 1, pStart, pEnd);CHKERRQ(ierr);}
2954793f3fe5SMatthew G. Knepley     else               {*pStart = 0; *pEnd = 0;}
2955793f3fe5SMatthew G. Knepley   } else {
2956793f3fe5SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, dim, pStart, pEnd);CHKERRQ(ierr);
2957793f3fe5SMatthew G. Knepley   }
2958793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
2959793f3fe5SMatthew G. Knepley }
2960793f3fe5SMatthew G. Knepley 
296128d58a37SPierre Jolivet static PetscErrorCode DMGetNeighbors_Plex(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
2962502a2867SDave May {
2963502a2867SDave May   PetscSF           sf;
29640a19bb7dSprj-   PetscInt          niranks, njranks, n;
29650a19bb7dSprj-   const PetscMPIInt *iranks, *jranks;
29660a19bb7dSprj-   DM_Plex           *data = (DM_Plex*) dm->data;
29672f356facSMatthew G. Knepley   PetscErrorCode    ierr;
2968502a2867SDave May 
29692f356facSMatthew G. Knepley   PetscFunctionBegin;
2970502a2867SDave May   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
29710a19bb7dSprj-   if (!data->neighbors) {
297207a779feSPierre Jolivet     ierr = PetscSFSetUp(sf);CHKERRQ(ierr);
29730a19bb7dSprj-     ierr = PetscSFGetRootRanks(sf, &njranks, &jranks, NULL, NULL, NULL);CHKERRQ(ierr);
29740a19bb7dSprj-     ierr = PetscSFGetLeafRanks(sf, &niranks, &iranks, NULL, NULL);CHKERRQ(ierr);
29750a19bb7dSprj-     ierr = PetscMalloc1(njranks + niranks + 1, &data->neighbors);CHKERRQ(ierr);
29760a19bb7dSprj-     ierr = PetscArraycpy(data->neighbors + 1, jranks, njranks);CHKERRQ(ierr);
29770a19bb7dSprj-     ierr = PetscArraycpy(data->neighbors + njranks + 1, iranks, niranks);CHKERRQ(ierr);
29780a19bb7dSprj-     n = njranks + niranks;
29790a19bb7dSprj-     ierr = PetscSortRemoveDupsMPIInt(&n, data->neighbors + 1);CHKERRQ(ierr);
29800a19bb7dSprj-     /* The following cast should never fail: can't have more neighbors than PETSC_MPI_INT_MAX */
29810a19bb7dSprj-     ierr = PetscMPIIntCast(n, data->neighbors);CHKERRQ(ierr);
29820a19bb7dSprj-   }
29830a19bb7dSprj-   if (nranks) *nranks = data->neighbors[0];
29840a19bb7dSprj-   if (ranks) {
29850a19bb7dSprj-     if (data->neighbors[0]) *ranks = data->neighbors + 1;
29860a19bb7dSprj-     else                    *ranks = NULL;
29870a19bb7dSprj-   }
2988502a2867SDave May   PetscFunctionReturn(0);
2989502a2867SDave May }
2990502a2867SDave May 
29911eb70e55SToby Isaac PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM, DM, Mat, Vec, Vec);
29921eb70e55SToby Isaac 
299346fa42a0SMatthew G. Knepley static PetscErrorCode DMInitialize_Plex(DM dm)
2994552f7358SJed Brown {
2995713918a9SToby Isaac   PetscErrorCode ierr;
2996713918a9SToby Isaac 
2997552f7358SJed Brown   PetscFunctionBegin;
2998552f7358SJed Brown   dm->ops->view                            = DMView_Plex;
29992c40f234SMatthew G. Knepley   dm->ops->load                            = DMLoad_Plex;
3000552f7358SJed Brown   dm->ops->setfromoptions                  = DMSetFromOptions_Plex;
300138221697SMatthew G. Knepley   dm->ops->clone                           = DMClone_Plex;
3002552f7358SJed Brown   dm->ops->setup                           = DMSetUp_Plex;
30031bb6d2a8SBarry Smith   dm->ops->createlocalsection              = DMCreateLocalSection_Plex;
300466ad2231SToby Isaac   dm->ops->createdefaultconstraints        = DMCreateDefaultConstraints_Plex;
3005552f7358SJed Brown   dm->ops->createglobalvector              = DMCreateGlobalVector_Plex;
3006552f7358SJed Brown   dm->ops->createlocalvector               = DMCreateLocalVector_Plex;
3007184d77edSJed Brown   dm->ops->getlocaltoglobalmapping         = NULL;
30080298fd71SBarry Smith   dm->ops->createfieldis                   = NULL;
3009552f7358SJed Brown   dm->ops->createcoordinatedm              = DMCreateCoordinateDM_Plex;
3010f19dbd58SToby Isaac   dm->ops->createcoordinatefield           = DMCreateCoordinateField_Plex;
30110a6ba040SMatthew G. Knepley   dm->ops->getcoloring                     = NULL;
3012552f7358SJed Brown   dm->ops->creatematrix                    = DMCreateMatrix_Plex;
3013bceba477SMatthew G. Knepley   dm->ops->createinterpolation             = DMCreateInterpolation_Plex;
3014bd041c0cSMatthew G. Knepley   dm->ops->createmassmatrix                = DMCreateMassMatrix_Plex;
30155a84ad33SLisandro Dalcin   dm->ops->createinjection                 = DMCreateInjection_Plex;
3016552f7358SJed Brown   dm->ops->refine                          = DMRefine_Plex;
30170a6ba040SMatthew G. Knepley   dm->ops->coarsen                         = DMCoarsen_Plex;
30180a6ba040SMatthew G. Knepley   dm->ops->refinehierarchy                 = DMRefineHierarchy_Plex;
3019b653a561SMatthew G. Knepley   dm->ops->coarsenhierarchy                = DMCoarsenHierarchy_Plex;
30200d1cd5e0SMatthew G. Knepley   dm->ops->adaptlabel                      = DMAdaptLabel_Plex;
30210d1cd5e0SMatthew G. Knepley   dm->ops->adaptmetric                     = DMAdaptMetric_Plex;
3022d410b0cfSMatthew G. Knepley   dm->ops->extrude                         = DMExtrude_Plex;
30230298fd71SBarry Smith   dm->ops->globaltolocalbegin              = NULL;
30240298fd71SBarry Smith   dm->ops->globaltolocalend                = NULL;
30250298fd71SBarry Smith   dm->ops->localtoglobalbegin              = NULL;
30260298fd71SBarry Smith   dm->ops->localtoglobalend                = NULL;
3027552f7358SJed Brown   dm->ops->destroy                         = DMDestroy_Plex;
3028552f7358SJed Brown   dm->ops->createsubdm                     = DMCreateSubDM_Plex;
30292adcc780SMatthew G. Knepley   dm->ops->createsuperdm                   = DMCreateSuperDM_Plex;
3030793f3fe5SMatthew G. Knepley   dm->ops->getdimpoints                    = DMGetDimPoints_Plex;
3031552f7358SJed Brown   dm->ops->locatepoints                    = DMLocatePoints_Plex;
30320709b2feSToby Isaac   dm->ops->projectfunctionlocal            = DMProjectFunctionLocal_Plex;
30330709b2feSToby Isaac   dm->ops->projectfunctionlabellocal       = DMProjectFunctionLabelLocal_Plex;
3034bfc4295aSToby Isaac   dm->ops->projectfieldlocal               = DMProjectFieldLocal_Plex;
30358c6c5593SMatthew G. Knepley   dm->ops->projectfieldlabellocal          = DMProjectFieldLabelLocal_Plex;
3036ece3a9fcSMatthew G. Knepley   dm->ops->projectbdfieldlabellocal        = DMProjectBdFieldLabelLocal_Plex;
30370709b2feSToby Isaac   dm->ops->computel2diff                   = DMComputeL2Diff_Plex;
3038b698f381SToby Isaac   dm->ops->computel2gradientdiff           = DMComputeL2GradientDiff_Plex;
30392a16baeaSToby Isaac   dm->ops->computel2fielddiff              = DMComputeL2FieldDiff_Plex;
304028d58a37SPierre Jolivet   dm->ops->getneighbors                    = DMGetNeighbors_Plex;
3041f1d73a7aSMatthew G. Knepley   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C",DMPlexInsertBoundaryValues_Plex);CHKERRQ(ierr);
304256cf3b9cSMatthew G. Knepley   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertTimeDerviativeBoundaryValues_C",DMPlexInsertTimeDerivativeBoundaryValues_Plex);CHKERRQ(ierr);
30438135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",DMSetUpGLVisViewer_Plex);CHKERRQ(ierr);
304428d58a37SPierre Jolivet   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMCreateNeumannOverlap_C",DMCreateNeumannOverlap_Plex);CHKERRQ(ierr);
3045cb54e036SVaclav Hapla   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexGetOverlap_C",DMPlexGetOverlap_Plex);CHKERRQ(ierr);
30461eb70e55SToby Isaac   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMInterpolateSolution_C",DMInterpolateSolution_Plex);CHKERRQ(ierr);
3047552f7358SJed Brown   PetscFunctionReturn(0);
3048552f7358SJed Brown }
3049552f7358SJed Brown 
305046fa42a0SMatthew G. Knepley PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm)
305163a16f15SMatthew G. Knepley {
305263a16f15SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex *) dm->data;
305363a16f15SMatthew G. Knepley   PetscErrorCode ierr;
305463a16f15SMatthew G. Knepley 
305563a16f15SMatthew G. Knepley   PetscFunctionBegin;
305663a16f15SMatthew G. Knepley   mesh->refct++;
305763a16f15SMatthew G. Knepley   (*newdm)->data = mesh;
305863a16f15SMatthew G. Knepley   ierr = PetscObjectChangeTypeName((PetscObject) *newdm, DMPLEX);CHKERRQ(ierr);
305963a16f15SMatthew G. Knepley   ierr = DMInitialize_Plex(*newdm);CHKERRQ(ierr);
306063a16f15SMatthew G. Knepley   PetscFunctionReturn(0);
306163a16f15SMatthew G. Knepley }
306263a16f15SMatthew G. Knepley 
30638818961aSMatthew G Knepley /*MC
30648818961aSMatthew G Knepley   DMPLEX = "plex" - A DM object that encapsulates an unstructured mesh, or CW Complex, which can be expressed using a Hasse Diagram.
30658818961aSMatthew G Knepley                     In the local representation, Vecs contain all unknowns in the interior and shared boundary. This is
30668818961aSMatthew G Knepley                     specified by a PetscSection object. Ownership in the global representation is determined by
30678818961aSMatthew G Knepley                     ownership of the underlying DMPlex points. This is specified by another PetscSection object.
30688818961aSMatthew G Knepley 
3069e5893cccSMatthew G. Knepley   Options Database Keys:
3070250712c9SMatthew G. Knepley + -dm_refine_pre                     - Refine mesh before distribution
3071250712c9SMatthew G. Knepley + -dm_refine_uniform_pre             - Choose uniform or generator-based refinement
3072250712c9SMatthew G. Knepley + -dm_refine_volume_limit_pre        - Cell volume limit after pre-refinement using generator
3073250712c9SMatthew G. Knepley . -dm_distribute                     - Distribute mesh across processes
3074250712c9SMatthew G. Knepley . -dm_distribute_overlap             - Number of cells to overlap for distribution
3075250712c9SMatthew G. Knepley . -dm_refine                         - Refine mesh after distribution
3076250712c9SMatthew G. Knepley . -dm_plex_hash_location             - Use grid hashing for point location
3077ddce0771SMatthew G. Knepley . -dm_plex_hash_box_faces <n,m,p>    - The number of divisions in each direction of the grid hash
3078f12cf164SMatthew G. Knepley . -dm_plex_partition_balance         - Attempt to evenly divide points on partition boundary between processes
3079f12cf164SMatthew G. Knepley . -dm_plex_remesh_bd                 - Allow changes to the boundary on remeshing
3080f12cf164SMatthew G. Knepley . -dm_plex_max_projection_height     - Maxmimum mesh point height used to project locally
3081f12cf164SMatthew G. Knepley . -dm_plex_regular_refinement        - Use special nested projection algorithm for regular refinement
3082250712c9SMatthew G. Knepley . -dm_plex_check_all                 - Perform all shecks below
3083f12cf164SMatthew G. Knepley . -dm_plex_check_symmetry            - Check that the adjacency information in the mesh is symmetric
3084f12cf164SMatthew G. Knepley . -dm_plex_check_skeleton <celltype> - Check that each cell has the correct number of vertices
3085f12cf164SMatthew 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
3086f12cf164SMatthew G. Knepley . -dm_plex_check_geometry            - Check that cells have positive volume
3087f12cf164SMatthew G. Knepley . -dm_view :mesh.tex:ascii_latex     - View the mesh in LaTeX/TikZ
3088e5893cccSMatthew G. Knepley . -dm_plex_view_scale <num>          - Scale the TikZ
3089e5893cccSMatthew G. Knepley - -dm_plex_print_fem <num>           - View FEM assembly information, such as element vectors and matrices
3090e5893cccSMatthew G. Knepley 
30918818961aSMatthew G Knepley   Level: intermediate
30928818961aSMatthew G Knepley 
30938818961aSMatthew G Knepley .seealso: DMType, DMPlexCreate(), DMCreate(), DMSetType()
30948818961aSMatthew G Knepley M*/
30958818961aSMatthew G Knepley 
30968cc058d9SJed Brown PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm)
3097552f7358SJed Brown {
3098552f7358SJed Brown   DM_Plex       *mesh;
3099412e9a14SMatthew G. Knepley   PetscInt       unit;
3100552f7358SJed Brown   PetscErrorCode ierr;
3101552f7358SJed Brown 
3102552f7358SJed Brown   PetscFunctionBegin;
3103552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3104b00a9115SJed Brown   ierr     = PetscNewLog(dm,&mesh);CHKERRQ(ierr);
3105552f7358SJed Brown   dm->data = mesh;
3106552f7358SJed Brown 
3107552f7358SJed Brown   mesh->refct             = 1;
310882f516ccSBarry Smith   ierr                    = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection);CHKERRQ(ierr);
3109552f7358SJed Brown   mesh->maxConeSize       = 0;
31100298fd71SBarry Smith   mesh->cones             = NULL;
31110298fd71SBarry Smith   mesh->coneOrientations  = NULL;
311282f516ccSBarry Smith   ierr                    = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection);CHKERRQ(ierr);
3113552f7358SJed Brown   mesh->maxSupportSize    = 0;
31140298fd71SBarry Smith   mesh->supports          = NULL;
3115552f7358SJed Brown   mesh->refinementUniform = PETSC_TRUE;
3116552f7358SJed Brown   mesh->refinementLimit   = -1.0;
31177d0f5628SVaclav Hapla   mesh->interpolated      = DMPLEX_INTERPOLATED_INVALID;
31187d0f5628SVaclav Hapla   mesh->interpolatedCollective = DMPLEX_INTERPOLATED_INVALID;
3119552f7358SJed Brown 
31200298fd71SBarry Smith   mesh->facesTmp = NULL;
3121552f7358SJed Brown 
3122d9deefdfSMatthew G. Knepley   mesh->tetgenOpts   = NULL;
3123d9deefdfSMatthew G. Knepley   mesh->triangleOpts = NULL;
312477623264SMatthew G. Knepley   ierr = PetscPartitionerCreate(PetscObjectComm((PetscObject)dm), &mesh->partitioner);CHKERRQ(ierr);
31252e62ab5aSMatthew G. Knepley   mesh->remeshBd     = PETSC_FALSE;
3126d9deefdfSMatthew G. Knepley 
31270298fd71SBarry Smith   mesh->subpointMap = NULL;
3128552f7358SJed Brown 
31298865f1eaSKarl Rupp   for (unit = 0; unit < NUM_PETSC_UNITS; ++unit) mesh->scale[unit] = 1.0;
3130552f7358SJed Brown 
31310aef6b92SMatthew G. Knepley   mesh->regularRefinement   = PETSC_FALSE;
3132df0420ecSMatthew G. Knepley   mesh->depthState          = -1;
3133ba2698f1SMatthew G. Knepley   mesh->celltypeState       = -1;
31340298fd71SBarry Smith   mesh->globalVertexNumbers = NULL;
31350298fd71SBarry Smith   mesh->globalCellNumbers   = NULL;
3136a68b90caSToby Isaac   mesh->anchorSection       = NULL;
3137a68b90caSToby Isaac   mesh->anchorIS            = NULL;
313841e6d900SToby Isaac   mesh->createanchors       = NULL;
3139fa73a4e1SToby Isaac   mesh->computeanchormatrix = NULL;
3140d961a43aSToby Isaac   mesh->parentSection       = NULL;
3141d961a43aSToby Isaac   mesh->parents             = NULL;
3142d961a43aSToby Isaac   mesh->childIDs            = NULL;
3143d961a43aSToby Isaac   mesh->childSection        = NULL;
3144d961a43aSToby Isaac   mesh->children            = NULL;
3145d6a7ad0dSToby Isaac   mesh->referenceTree       = NULL;
3146dcbd3bf7SToby Isaac   mesh->getchildsymmetry    = NULL;
3147552f7358SJed Brown   mesh->vtkCellHeight       = 0;
3148e228b242SToby Isaac   mesh->useAnchors          = PETSC_FALSE;
3149552f7358SJed Brown 
3150b29cfa1cSToby Isaac   mesh->maxProjectionHeight = 0;
3151b29cfa1cSToby Isaac 
31520a19bb7dSprj-   mesh->neighbors           = NULL;
31530a19bb7dSprj- 
3154552f7358SJed Brown   mesh->printSetValues = PETSC_FALSE;
3155552f7358SJed Brown   mesh->printFEM       = 0;
31566113b454SMatthew G. Knepley   mesh->printTol       = 1.0e-10;
3157552f7358SJed Brown 
3158552f7358SJed Brown   ierr = DMInitialize_Plex(dm);CHKERRQ(ierr);
3159552f7358SJed Brown   PetscFunctionReturn(0);
3160552f7358SJed Brown }
3161552f7358SJed Brown 
3162552f7358SJed Brown /*@
3163552f7358SJed Brown   DMPlexCreate - Creates a DMPlex object, which encapsulates an unstructured mesh, or CW complex, which can be expressed using a Hasse Diagram.
3164552f7358SJed Brown 
3165d083f849SBarry Smith   Collective
3166552f7358SJed Brown 
3167552f7358SJed Brown   Input Parameter:
3168552f7358SJed Brown . comm - The communicator for the DMPlex object
3169552f7358SJed Brown 
3170552f7358SJed Brown   Output Parameter:
3171552f7358SJed Brown . mesh  - The DMPlex object
3172552f7358SJed Brown 
3173552f7358SJed Brown   Level: beginner
3174552f7358SJed Brown 
3175552f7358SJed Brown @*/
3176552f7358SJed Brown PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh)
3177552f7358SJed Brown {
3178552f7358SJed Brown   PetscErrorCode ierr;
3179552f7358SJed Brown 
3180552f7358SJed Brown   PetscFunctionBegin;
3181552f7358SJed Brown   PetscValidPointer(mesh,2);
3182552f7358SJed Brown   ierr = DMCreate(comm, mesh);CHKERRQ(ierr);
3183552f7358SJed Brown   ierr = DMSetType(*mesh, DMPLEX);CHKERRQ(ierr);
3184552f7358SJed Brown   PetscFunctionReturn(0);
3185552f7358SJed Brown }
3186552f7358SJed Brown 
3187b09969d6SVaclav Hapla /*@C
3188b09969d6SVaclav Hapla   DMPlexBuildFromCellListParallel - Build distributed DMPLEX topology from a list of vertices for each cell (common mesh generator output)
3189b09969d6SVaclav Hapla 
3190b09969d6SVaclav Hapla   Input Parameters:
3191b09969d6SVaclav Hapla + dm - The DM
3192b09969d6SVaclav Hapla . numCells - The number of cells owned by this process
319325b6865aSVaclav Hapla . numVertices - The number of vertices owned by this process, or PETSC_DECIDE
319425b6865aSVaclav Hapla . NVertices - The global number of vertices, or PETSC_DECIDE
3195b09969d6SVaclav Hapla . numCorners - The number of vertices for each cell
31965e488331SVaclav Hapla - cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
3197b09969d6SVaclav Hapla 
3198b09969d6SVaclav Hapla   Output Parameter:
31998ddadb14SVaclav Hapla . vertexSF - (Optional) SF describing complete vertex ownership
3200b09969d6SVaclav Hapla 
3201b09969d6SVaclav Hapla   Notes:
3202b09969d6SVaclav Hapla   Two triangles sharing a face
3203b09969d6SVaclav Hapla $
3204b09969d6SVaclav Hapla $        2
3205b09969d6SVaclav Hapla $      / | \
3206b09969d6SVaclav Hapla $     /  |  \
3207b09969d6SVaclav Hapla $    /   |   \
3208b09969d6SVaclav Hapla $   0  0 | 1  3
3209b09969d6SVaclav Hapla $    \   |   /
3210b09969d6SVaclav Hapla $     \  |  /
3211b09969d6SVaclav Hapla $      \ | /
3212b09969d6SVaclav Hapla $        1
3213b09969d6SVaclav Hapla would have input
3214b09969d6SVaclav Hapla $  numCells = 2, numVertices = 4
3215b09969d6SVaclav Hapla $  cells = [0 1 2  1 3 2]
3216b09969d6SVaclav Hapla $
3217b09969d6SVaclav Hapla which would result in the DMPlex
3218b09969d6SVaclav Hapla $
3219b09969d6SVaclav Hapla $        4
3220b09969d6SVaclav Hapla $      / | \
3221b09969d6SVaclav Hapla $     /  |  \
3222b09969d6SVaclav Hapla $    /   |   \
3223b09969d6SVaclav Hapla $   2  0 | 1  5
3224b09969d6SVaclav Hapla $    \   |   /
3225b09969d6SVaclav Hapla $     \  |  /
3226b09969d6SVaclav Hapla $      \ | /
3227b09969d6SVaclav Hapla $        3
3228b09969d6SVaclav Hapla 
322925b6865aSVaclav Hapla   Vertices are implicitly numbered consecutively 0,...,NVertices.
323025b6865aSVaclav Hapla   Each rank owns a chunk of numVertices consecutive vertices.
323125b6865aSVaclav Hapla   If numVertices is PETSC_DECIDE, PETSc will distribute them as evenly as possible using PetscLayout.
323225b6865aSVaclav Hapla   If both NVertices and numVertices are PETSC_DECIDE, NVertices is computed by PETSc as the maximum vertex index in cells + 1.
323325b6865aSVaclav Hapla   If only NVertices is PETSC_DECIDE, it is computed as the sum of numVertices over all ranks.
323425b6865aSVaclav Hapla 
3235b09969d6SVaclav Hapla   The cell distribution is arbitrary non-overlapping, independent of the vertex distribution.
3236b09969d6SVaclav Hapla 
3237b09969d6SVaclav Hapla   Not currently supported in Fortran.
3238b09969d6SVaclav Hapla 
3239b09969d6SVaclav Hapla   Level: advanced
3240b09969d6SVaclav Hapla 
3241b09969d6SVaclav Hapla .seealso: DMPlexBuildFromCellList(), DMPlexCreateFromCellListParallelPetsc(), DMPlexBuildCoordinatesFromCellListParallel()
3242b09969d6SVaclav Hapla @*/
324325b6865aSVaclav Hapla PetscErrorCode DMPlexBuildFromCellListParallel(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscInt numCorners, const PetscInt cells[], PetscSF *vertexSF)
3244a47d0d45SMatthew G. Knepley {
32452464107aSksagiyam   PetscSF         sfPoint;
32462464107aSksagiyam   PetscLayout     layout;
32472464107aSksagiyam   PetscInt        numVerticesAdj, *verticesAdj, *cones, c, p, dim;
32489852e123SBarry Smith   PetscMPIInt     rank, size;
3249a47d0d45SMatthew G. Knepley   PetscErrorCode  ierr;
3250a47d0d45SMatthew G. Knepley 
3251a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
325225b6865aSVaclav Hapla   PetscValidLogicalCollectiveInt(dm,NVertices,4);
3253b09969d6SVaclav Hapla   ierr = PetscLogEventBegin(DMPLEX_BuildFromCellList,dm,0,0,0);CHKERRQ(ierr);
3254ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
3255ffc4695bSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size);CHKERRMPI(ierr);
32566cbf6523SVaclav Hapla   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
325725b6865aSVaclav Hapla   /* Get/check global number of vertices */
325825b6865aSVaclav Hapla   {
325925b6865aSVaclav Hapla     PetscInt NVerticesInCells, i;
326025b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
326125b6865aSVaclav Hapla 
326225b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
326325b6865aSVaclav Hapla     NVerticesInCells = PETSC_MIN_INT;
326425b6865aSVaclav Hapla     for (i=0; i<len; i++) if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
326525b6865aSVaclav Hapla     ++NVerticesInCells;
3266ffc4695bSBarry Smith     ierr = MPI_Allreduce(MPI_IN_PLACE, &NVerticesInCells, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);
326725b6865aSVaclav Hapla 
326825b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE && NVertices == PETSC_DECIDE) NVertices = NVerticesInCells;
326925b6865aSVaclav 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);
327025b6865aSVaclav Hapla   }
32719079aca8SVaclav Hapla   /* Count locally unique vertices */
32729079aca8SVaclav Hapla   {
32739079aca8SVaclav Hapla     PetscHSetI vhash;
32749079aca8SVaclav Hapla     PetscInt off = 0;
32759079aca8SVaclav Hapla 
3276e8f14785SLisandro Dalcin     ierr = PetscHSetICreate(&vhash);CHKERRQ(ierr);
3277a47d0d45SMatthew G. Knepley     for (c = 0; c < numCells; ++c) {
3278a47d0d45SMatthew G. Knepley       for (p = 0; p < numCorners; ++p) {
3279e8f14785SLisandro Dalcin         ierr = PetscHSetIAdd(vhash, cells[c*numCorners+p]);CHKERRQ(ierr);
3280a47d0d45SMatthew G. Knepley       }
3281a47d0d45SMatthew G. Knepley     }
3282e8f14785SLisandro Dalcin     ierr = PetscHSetIGetSize(vhash, &numVerticesAdj);CHKERRQ(ierr);
3283a47d0d45SMatthew G. Knepley     ierr = PetscMalloc1(numVerticesAdj, &verticesAdj);CHKERRQ(ierr);
3284e8f14785SLisandro Dalcin     ierr = PetscHSetIGetElems(vhash, &off, verticesAdj);CHKERRQ(ierr);
3285e8f14785SLisandro Dalcin     ierr = PetscHSetIDestroy(&vhash);CHKERRQ(ierr);
3286a47d0d45SMatthew G. Knepley     if (off != numVerticesAdj) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid number of local vertices %D should be %D", off, numVerticesAdj);
3287a47d0d45SMatthew G. Knepley   }
32889079aca8SVaclav Hapla   ierr = PetscSortInt(numVerticesAdj, verticesAdj);CHKERRQ(ierr);
3289a47d0d45SMatthew G. Knepley   /* Create cones */
3290a47d0d45SMatthew G. Knepley   ierr = DMPlexSetChart(dm, 0, numCells+numVerticesAdj);CHKERRQ(ierr);
3291a47d0d45SMatthew G. Knepley   for (c = 0; c < numCells; ++c) {ierr = DMPlexSetConeSize(dm, c, numCorners);CHKERRQ(ierr);}
3292a47d0d45SMatthew G. Knepley   ierr = DMSetUp(dm);CHKERRQ(ierr);
3293961cfab0SVaclav Hapla   ierr = DMPlexGetCones(dm,&cones);CHKERRQ(ierr);
3294a47d0d45SMatthew G. Knepley   for (c = 0; c < numCells; ++c) {
3295a47d0d45SMatthew G. Knepley     for (p = 0; p < numCorners; ++p) {
3296a47d0d45SMatthew G. Knepley       const PetscInt gv = cells[c*numCorners+p];
3297a47d0d45SMatthew G. Knepley       PetscInt       lv;
3298a47d0d45SMatthew G. Knepley 
32999079aca8SVaclav Hapla       /* Positions within verticesAdj form 0-based local vertex numbering;
33009079aca8SVaclav Hapla          we need to shift it by numCells to get correct DAG points (cells go first) */
3301a47d0d45SMatthew G. Knepley       ierr = PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv);CHKERRQ(ierr);
3302a47d0d45SMatthew G. Knepley       if (lv < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %D in local connectivity", gv);
3303961cfab0SVaclav Hapla       cones[c*numCorners+p] = lv+numCells;
3304a47d0d45SMatthew G. Knepley     }
3305a47d0d45SMatthew G. Knepley   }
33062464107aSksagiyam   /* Build point sf */
33072464107aSksagiyam   ierr = PetscLayoutCreate(PetscObjectComm((PetscObject)dm), &layout);CHKERRQ(ierr);
33082464107aSksagiyam   ierr = PetscLayoutSetSize(layout, NVertices);CHKERRQ(ierr);
33092464107aSksagiyam   ierr = PetscLayoutSetLocalSize(layout, numVertices);CHKERRQ(ierr);
33102464107aSksagiyam   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
33112464107aSksagiyam   ierr = PetscSFCreateByMatchingIndices(layout, numVerticesAdj, verticesAdj, NULL, numCells, numVerticesAdj, verticesAdj, NULL, numCells, vertexSF, &sfPoint);CHKERRQ(ierr);
33122464107aSksagiyam   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3313a47d0d45SMatthew G. Knepley   ierr = PetscFree(verticesAdj);CHKERRQ(ierr);
3314a47d0d45SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) sfPoint, "point SF");CHKERRQ(ierr);
33152464107aSksagiyam   if (dm->sf) {
33162464107aSksagiyam     const char *prefix;
33172464107aSksagiyam 
33182464107aSksagiyam     ierr = PetscObjectGetOptionsPrefix((PetscObject)dm->sf, &prefix);CHKERRQ(ierr);
33192464107aSksagiyam     ierr = PetscObjectSetOptionsPrefix((PetscObject)sfPoint, prefix);CHKERRQ(ierr);
33202464107aSksagiyam   }
33212464107aSksagiyam   ierr = DMSetPointSF(dm, sfPoint);CHKERRQ(ierr);
33222464107aSksagiyam   ierr = PetscSFDestroy(&sfPoint);CHKERRQ(ierr);
33232464107aSksagiyam   if (vertexSF) {ierr = PetscObjectSetName((PetscObject)(*vertexSF), "Vertex Ownership SF");CHKERRQ(ierr);}
3324a47d0d45SMatthew G. Knepley   /* Fill in the rest of the topology structure */
3325a47d0d45SMatthew G. Knepley   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
3326a47d0d45SMatthew G. Knepley   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
3327b09969d6SVaclav Hapla   ierr = PetscLogEventEnd(DMPLEX_BuildFromCellList,dm,0,0,0);CHKERRQ(ierr);
3328a47d0d45SMatthew G. Knepley   PetscFunctionReturn(0);
3329a47d0d45SMatthew G. Knepley }
3330a47d0d45SMatthew G. Knepley 
3331b09969d6SVaclav Hapla /*@C
3332b09969d6SVaclav Hapla   DMPlexBuildCoordinatesFromCellListParallel - Build DM coordinates from a list of coordinates for each owned vertex (common mesh generator output)
3333b09969d6SVaclav Hapla 
3334b09969d6SVaclav Hapla   Input Parameters:
3335b09969d6SVaclav Hapla + dm - The DM
3336b09969d6SVaclav Hapla . spaceDim - The spatial dimension used for coordinates
3337b09969d6SVaclav Hapla . sfVert - SF describing complete vertex ownership
3338b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
3339b09969d6SVaclav Hapla 
3340b09969d6SVaclav Hapla   Level: advanced
3341b09969d6SVaclav Hapla 
3342b09969d6SVaclav Hapla   Notes:
3343b09969d6SVaclav Hapla   Not currently supported in Fortran.
3344b09969d6SVaclav Hapla 
3345b09969d6SVaclav Hapla .seealso: DMPlexBuildCoordinatesFromCellList(), DMPlexCreateFromCellListParallelPetsc(), DMPlexBuildFromCellListParallel()
3346b09969d6SVaclav Hapla @*/
33471edcf0b2SVaclav Hapla PetscErrorCode DMPlexBuildCoordinatesFromCellListParallel(DM dm, PetscInt spaceDim, PetscSF sfVert, const PetscReal vertexCoords[])
3348a47d0d45SMatthew G. Knepley {
3349a47d0d45SMatthew G. Knepley   PetscSection   coordSection;
3350a47d0d45SMatthew G. Knepley   Vec            coordinates;
3351a47d0d45SMatthew G. Knepley   PetscScalar   *coords;
33521edcf0b2SVaclav Hapla   PetscInt       numVertices, numVerticesAdj, coordSize, v, vStart, vEnd;
3353a47d0d45SMatthew G. Knepley   PetscErrorCode ierr;
3354a47d0d45SMatthew G. Knepley 
3355a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
3356b09969d6SVaclav Hapla   ierr = PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0);CHKERRQ(ierr);
33571edcf0b2SVaclav Hapla   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
33581edcf0b2SVaclav 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.");
33599596c6baSMatthew G. Knepley   ierr = DMSetCoordinateDim(dm, spaceDim);CHKERRQ(ierr);
3360a47d0d45SMatthew G. Knepley   ierr = PetscSFGetGraph(sfVert, &numVertices, &numVerticesAdj, NULL, NULL);CHKERRQ(ierr);
33611edcf0b2SVaclav 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);
3362a47d0d45SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
3363a47d0d45SMatthew G. Knepley   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
3364a47d0d45SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(coordSection, 0, spaceDim);CHKERRQ(ierr);
33651edcf0b2SVaclav Hapla   ierr = PetscSectionSetChart(coordSection, vStart, vEnd);CHKERRQ(ierr);
33661edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
3367a47d0d45SMatthew G. Knepley     ierr = PetscSectionSetDof(coordSection, v, spaceDim);CHKERRQ(ierr);
3368a47d0d45SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(coordSection, v, 0, spaceDim);CHKERRQ(ierr);
3369a47d0d45SMatthew G. Knepley   }
3370a47d0d45SMatthew G. Knepley   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
3371a47d0d45SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
3372a47d0d45SMatthew G. Knepley   ierr = VecCreate(PetscObjectComm((PetscObject)dm), &coordinates);CHKERRQ(ierr);
3373a47d0d45SMatthew G. Knepley   ierr = VecSetBlockSize(coordinates, spaceDim);CHKERRQ(ierr);
3374a47d0d45SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
3375a47d0d45SMatthew G. Knepley   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
3376a47d0d45SMatthew G. Knepley   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
3377a47d0d45SMatthew G. Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
3378a47d0d45SMatthew G. Knepley   {
3379a47d0d45SMatthew G. Knepley     MPI_Datatype coordtype;
3380a47d0d45SMatthew G. Knepley 
3381a47d0d45SMatthew G. Knepley     /* Need a temp buffer for coords if we have complex/single */
3382ffc4695bSBarry Smith     ierr = MPI_Type_contiguous(spaceDim, MPIU_SCALAR, &coordtype);CHKERRMPI(ierr);
3383ffc4695bSBarry Smith     ierr = MPI_Type_commit(&coordtype);CHKERRMPI(ierr);
338421016a8bSBarry Smith #if defined(PETSC_USE_COMPLEX)
338521016a8bSBarry Smith     {
338621016a8bSBarry Smith     PetscScalar *svertexCoords;
338721016a8bSBarry Smith     PetscInt    i;
33883612f820SVaclav Hapla     ierr = PetscMalloc1(numVertices*spaceDim,&svertexCoords);CHKERRQ(ierr);
33893612f820SVaclav Hapla     for (i=0; i<numVertices*spaceDim; i++) svertexCoords[i] = vertexCoords[i];
3390ad227feaSJunchao Zhang     ierr = PetscSFBcastBegin(sfVert, coordtype, svertexCoords, coords,MPI_REPLACE);CHKERRQ(ierr);
3391ad227feaSJunchao Zhang     ierr = PetscSFBcastEnd(sfVert, coordtype, svertexCoords, coords,MPI_REPLACE);CHKERRQ(ierr);
339221016a8bSBarry Smith     ierr = PetscFree(svertexCoords);CHKERRQ(ierr);
339321016a8bSBarry Smith     }
339421016a8bSBarry Smith #else
3395ad227feaSJunchao Zhang     ierr = PetscSFBcastBegin(sfVert, coordtype, vertexCoords, coords,MPI_REPLACE);CHKERRQ(ierr);
3396ad227feaSJunchao Zhang     ierr = PetscSFBcastEnd(sfVert, coordtype, vertexCoords, coords,MPI_REPLACE);CHKERRQ(ierr);
339721016a8bSBarry Smith #endif
3398ffc4695bSBarry Smith     ierr = MPI_Type_free(&coordtype);CHKERRMPI(ierr);
3399a47d0d45SMatthew G. Knepley   }
3400a47d0d45SMatthew G. Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
3401a47d0d45SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
3402a47d0d45SMatthew G. Knepley   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
3403b09969d6SVaclav Hapla   ierr = PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0);CHKERRQ(ierr);
3404a47d0d45SMatthew G. Knepley   PetscFunctionReturn(0);
3405a47d0d45SMatthew G. Knepley }
3406a47d0d45SMatthew G. Knepley 
3407c3edce3dSSatish Balay /*@
3408b09969d6SVaclav Hapla   DMPlexCreateFromCellListParallelPetsc - Create distributed DMPLEX from a list of vertices for each cell (common mesh generator output)
3409a47d0d45SMatthew G. Knepley 
3410a47d0d45SMatthew G. Knepley   Input Parameters:
3411a47d0d45SMatthew G. Knepley + comm - The communicator
3412a47d0d45SMatthew G. Knepley . dim - The topological dimension of the mesh
3413a47d0d45SMatthew G. Knepley . numCells - The number of cells owned by this process
341425b6865aSVaclav Hapla . numVertices - The number of vertices owned by this process, or PETSC_DECIDE
341525b6865aSVaclav Hapla . NVertices - The global number of vertices, or PETSC_DECIDE
3416a47d0d45SMatthew G. Knepley . numCorners - The number of vertices for each cell
3417a47d0d45SMatthew G. Knepley . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
3418a47d0d45SMatthew G. Knepley . cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
3419a47d0d45SMatthew G. Knepley . spaceDim - The spatial dimension used for coordinates
3420a47d0d45SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
3421a47d0d45SMatthew G. Knepley 
3422d8d19677SJose E. Roman   Output Parameters:
342318d54ad4SMichael Lange + dm - The DM
3424b09969d6SVaclav Hapla - vertexSF - (Optional) SF describing complete vertex ownership
3425a47d0d45SMatthew G. Knepley 
3426b09969d6SVaclav Hapla   Notes:
3427b09969d6SVaclav Hapla   This function is just a convenient sequence of DMCreate(), DMSetType(), DMSetDimension(),
3428b09969d6SVaclav Hapla   DMPlexBuildFromCellListParallel(), DMPlexInterpolate(), DMPlexBuildCoordinatesFromCellListParallel()
3429a47d0d45SMatthew G. Knepley 
343025b6865aSVaclav Hapla   See DMPlexBuildFromCellListParallel() for an example and details about the topology-related parameters.
343125b6865aSVaclav Hapla   See DMPlexBuildCoordinatesFromCellListParallel() for details about the geometry-related parameters.
343225b6865aSVaclav Hapla 
3433b09969d6SVaclav Hapla   Level: intermediate
3434a47d0d45SMatthew G. Knepley 
3435b09969d6SVaclav Hapla .seealso: DMPlexCreateFromCellListPetsc(), DMPlexBuildFromCellListParallel(), DMPlexBuildCoordinatesFromCellListParallel(), DMPlexCreateFromDAG(), DMPlexCreate()
3436a47d0d45SMatthew G. Knepley @*/
343725b6865aSVaclav 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)
3438a47d0d45SMatthew G. Knepley {
3439a47d0d45SMatthew G. Knepley   PetscSF        sfVert;
3440a47d0d45SMatthew G. Knepley   PetscErrorCode ierr;
3441a47d0d45SMatthew G. Knepley 
3442a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
3443a47d0d45SMatthew G. Knepley   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
3444a47d0d45SMatthew G. Knepley   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
3445a47d0d45SMatthew G. Knepley   PetscValidLogicalCollectiveInt(*dm, dim, 2);
3446064a246eSJacob Faibussowitsch   PetscValidLogicalCollectiveInt(*dm, spaceDim, 9);
3447a47d0d45SMatthew G. Knepley   ierr = DMSetDimension(*dm, dim);CHKERRQ(ierr);
344825b6865aSVaclav Hapla   ierr = DMPlexBuildFromCellListParallel(*dm, numCells, numVertices, NVertices, numCorners, cells, &sfVert);CHKERRQ(ierr);
3449a47d0d45SMatthew G. Knepley   if (interpolate) {
34505fd9971aSMatthew G. Knepley     DM idm;
3451a47d0d45SMatthew G. Knepley 
3452a47d0d45SMatthew G. Knepley     ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr);
3453a47d0d45SMatthew G. Knepley     ierr = DMDestroy(dm);CHKERRQ(ierr);
3454a47d0d45SMatthew G. Knepley     *dm  = idm;
3455a47d0d45SMatthew G. Knepley   }
34561edcf0b2SVaclav Hapla   ierr = DMPlexBuildCoordinatesFromCellListParallel(*dm, spaceDim, sfVert, vertexCoords);CHKERRQ(ierr);
345718d54ad4SMichael Lange   if (vertexSF) *vertexSF = sfVert;
3458fba955ccSBarry Smith   else {ierr = PetscSFDestroy(&sfVert);CHKERRQ(ierr);}
3459a47d0d45SMatthew G. Knepley   PetscFunctionReturn(0);
3460a47d0d45SMatthew G. Knepley }
3461a47d0d45SMatthew G. Knepley 
3462a4a685f2SJacob Faibussowitsch /*@
3463a4a685f2SJacob Faibussowitsch   DMPlexCreateFromCellListParallel - Deprecated, use DMPlexCreateFromCellListParallelPetsc()
3464a4a685f2SJacob Faibussowitsch 
3465a4a685f2SJacob Faibussowitsch   Level: deprecated
3466a4a685f2SJacob Faibussowitsch 
3467a4a685f2SJacob Faibussowitsch .seealso: DMPlexCreateFromCellListParallelPetsc()
3468a4a685f2SJacob Faibussowitsch @*/
3469a4a685f2SJacob 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)
3470a4a685f2SJacob Faibussowitsch {
3471a4a685f2SJacob Faibussowitsch   PetscErrorCode ierr;
3472a4a685f2SJacob Faibussowitsch   PetscInt       i;
3473a4a685f2SJacob Faibussowitsch   PetscInt       *pintCells;
3474a4a685f2SJacob Faibussowitsch 
3475a4a685f2SJacob Faibussowitsch   PetscFunctionBegin;
3476a4a685f2SJacob 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));
3477a4a685f2SJacob Faibussowitsch   if (sizeof(int) == sizeof(PetscInt)) {
3478a4a685f2SJacob Faibussowitsch     pintCells = (PetscInt *) cells;
3479a4a685f2SJacob Faibussowitsch   } else {
3480a4a685f2SJacob Faibussowitsch     ierr = PetscMalloc1(numCells*numCorners, &pintCells);CHKERRQ(ierr);
3481a4a685f2SJacob Faibussowitsch     for (i = 0; i < numCells*numCorners; i++) {
3482a4a685f2SJacob Faibussowitsch       pintCells[i] = (PetscInt) cells[i];
3483a4a685f2SJacob Faibussowitsch     }
3484a4a685f2SJacob Faibussowitsch   }
348525b6865aSVaclav Hapla   ierr = DMPlexCreateFromCellListParallelPetsc(comm, dim, numCells, numVertices, PETSC_DECIDE, numCorners, interpolate, pintCells, spaceDim, vertexCoords, vertexSF, dm);CHKERRQ(ierr);
3486a4a685f2SJacob Faibussowitsch   if (sizeof(int) != sizeof(PetscInt)) {
3487a4a685f2SJacob Faibussowitsch     ierr = PetscFree(pintCells);CHKERRQ(ierr);
3488a4a685f2SJacob Faibussowitsch   }
3489a4a685f2SJacob Faibussowitsch   PetscFunctionReturn(0);
3490a4a685f2SJacob Faibussowitsch }
3491a4a685f2SJacob Faibussowitsch 
3492b09969d6SVaclav Hapla /*@C
3493b09969d6SVaclav Hapla   DMPlexBuildFromCellList - Build DMPLEX topology from a list of vertices for each cell (common mesh generator output)
34949298eaa6SMatthew G Knepley 
34959298eaa6SMatthew G Knepley   Input Parameters:
3496b09969d6SVaclav Hapla + dm - The DM
3497b09969d6SVaclav Hapla . numCells - The number of cells owned by this process
349825b6865aSVaclav Hapla . numVertices - The number of vertices owned by this process, or PETSC_DECIDE
34999298eaa6SMatthew G Knepley . numCorners - The number of vertices for each cell
35005e488331SVaclav Hapla - cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
35019298eaa6SMatthew G Knepley 
3502b09969d6SVaclav Hapla   Level: advanced
35039298eaa6SMatthew G Knepley 
3504b09969d6SVaclav Hapla   Notes:
3505b09969d6SVaclav Hapla   Two triangles sharing a face
35069298eaa6SMatthew G Knepley $
35079298eaa6SMatthew G Knepley $        2
35089298eaa6SMatthew G Knepley $      / | \
35099298eaa6SMatthew G Knepley $     /  |  \
35109298eaa6SMatthew G Knepley $    /   |   \
35119298eaa6SMatthew G Knepley $   0  0 | 1  3
35129298eaa6SMatthew G Knepley $    \   |   /
35139298eaa6SMatthew G Knepley $     \  |  /
35149298eaa6SMatthew G Knepley $      \ | /
35159298eaa6SMatthew G Knepley $        1
35169298eaa6SMatthew G Knepley would have input
35179298eaa6SMatthew G Knepley $  numCells = 2, numVertices = 4
35189298eaa6SMatthew G Knepley $  cells = [0 1 2  1 3 2]
35199298eaa6SMatthew G Knepley $
35209298eaa6SMatthew G Knepley which would result in the DMPlex
35219298eaa6SMatthew G Knepley $
35229298eaa6SMatthew G Knepley $        4
35239298eaa6SMatthew G Knepley $      / | \
35249298eaa6SMatthew G Knepley $     /  |  \
35259298eaa6SMatthew G Knepley $    /   |   \
35269298eaa6SMatthew G Knepley $   2  0 | 1  5
35279298eaa6SMatthew G Knepley $    \   |   /
35289298eaa6SMatthew G Knepley $     \  |  /
35299298eaa6SMatthew G Knepley $      \ | /
35309298eaa6SMatthew G Knepley $        3
35319298eaa6SMatthew G Knepley 
353225b6865aSVaclav Hapla   If numVertices is PETSC_DECIDE, it is computed by PETSc as the maximum vertex index in cells + 1.
353325b6865aSVaclav Hapla 
3534b09969d6SVaclav Hapla   Not currently supported in Fortran.
35359298eaa6SMatthew G Knepley 
3536b09969d6SVaclav Hapla .seealso: DMPlexBuildFromCellListParallel(), DMPlexBuildCoordinatesFromCellList(), DMPlexCreateFromCellListPetsc()
3537b09969d6SVaclav Hapla @*/
35385e488331SVaclav Hapla PetscErrorCode DMPlexBuildFromCellList(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const PetscInt cells[])
3539b09969d6SVaclav Hapla {
3540961cfab0SVaclav Hapla   PetscInt      *cones, c, p, dim;
3541b09969d6SVaclav Hapla   PetscErrorCode ierr;
3542b09969d6SVaclav Hapla 
3543b09969d6SVaclav Hapla   PetscFunctionBegin;
3544b09969d6SVaclav Hapla   ierr = PetscLogEventBegin(DMPLEX_BuildFromCellList,dm,0,0,0);CHKERRQ(ierr);
3545b09969d6SVaclav Hapla   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
354625b6865aSVaclav Hapla   /* Get/check global number of vertices */
354725b6865aSVaclav Hapla   {
354825b6865aSVaclav Hapla     PetscInt NVerticesInCells, i;
354925b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
355025b6865aSVaclav Hapla 
355125b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
355225b6865aSVaclav Hapla     NVerticesInCells = PETSC_MIN_INT;
355325b6865aSVaclav Hapla     for (i=0; i<len; i++) if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
355425b6865aSVaclav Hapla     ++NVerticesInCells;
355525b6865aSVaclav Hapla 
355625b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE) numVertices = NVerticesInCells;
355725b6865aSVaclav 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);
355825b6865aSVaclav Hapla   }
3559b09969d6SVaclav Hapla   ierr = DMPlexSetChart(dm, 0, numCells+numVertices);CHKERRQ(ierr);
3560b09969d6SVaclav Hapla   for (c = 0; c < numCells; ++c) {
3561b09969d6SVaclav Hapla     ierr = DMPlexSetConeSize(dm, c, numCorners);CHKERRQ(ierr);
3562b09969d6SVaclav Hapla   }
3563b09969d6SVaclav Hapla   ierr = DMSetUp(dm);CHKERRQ(ierr);
3564961cfab0SVaclav Hapla   ierr = DMPlexGetCones(dm,&cones);CHKERRQ(ierr);
3565b09969d6SVaclav Hapla   for (c = 0; c < numCells; ++c) {
3566b09969d6SVaclav Hapla     for (p = 0; p < numCorners; ++p) {
3567961cfab0SVaclav Hapla       cones[c*numCorners+p] = cells[c*numCorners+p]+numCells;
3568b09969d6SVaclav Hapla     }
3569b09969d6SVaclav Hapla   }
3570b09969d6SVaclav Hapla   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
3571b09969d6SVaclav Hapla   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
3572b09969d6SVaclav Hapla   ierr = PetscLogEventEnd(DMPLEX_BuildFromCellList,dm,0,0,0);CHKERRQ(ierr);
3573b09969d6SVaclav Hapla   PetscFunctionReturn(0);
3574b09969d6SVaclav Hapla }
3575b09969d6SVaclav Hapla 
3576b09969d6SVaclav Hapla /*@C
3577b09969d6SVaclav Hapla   DMPlexBuildCoordinatesFromCellList - Build DM coordinates from a list of coordinates for each owned vertex (common mesh generator output)
3578b09969d6SVaclav Hapla 
3579b09969d6SVaclav Hapla   Input Parameters:
3580b09969d6SVaclav Hapla + dm - The DM
3581b09969d6SVaclav Hapla . spaceDim - The spatial dimension used for coordinates
3582b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
3583b09969d6SVaclav Hapla 
3584b09969d6SVaclav Hapla   Level: advanced
3585b09969d6SVaclav Hapla 
3586b09969d6SVaclav Hapla   Notes:
3587b09969d6SVaclav Hapla   Not currently supported in Fortran.
3588b09969d6SVaclav Hapla 
3589b09969d6SVaclav Hapla .seealso: DMPlexBuildCoordinatesFromCellListParallel(), DMPlexCreateFromCellListPetsc(), DMPlexBuildFromCellList()
3590b09969d6SVaclav Hapla @*/
35911edcf0b2SVaclav Hapla PetscErrorCode DMPlexBuildCoordinatesFromCellList(DM dm, PetscInt spaceDim, const PetscReal vertexCoords[])
3592b09969d6SVaclav Hapla {
3593b09969d6SVaclav Hapla   PetscSection   coordSection;
3594b09969d6SVaclav Hapla   Vec            coordinates;
3595b09969d6SVaclav Hapla   DM             cdm;
3596b09969d6SVaclav Hapla   PetscScalar   *coords;
35971edcf0b2SVaclav Hapla   PetscInt       v, vStart, vEnd, d;
3598b09969d6SVaclav Hapla   PetscErrorCode ierr;
3599b09969d6SVaclav Hapla 
3600b09969d6SVaclav Hapla   PetscFunctionBegin;
3601b09969d6SVaclav Hapla   ierr = PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0);CHKERRQ(ierr);
36021edcf0b2SVaclav Hapla   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
36031edcf0b2SVaclav 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.");
3604b09969d6SVaclav Hapla   ierr = DMSetCoordinateDim(dm, spaceDim);CHKERRQ(ierr);
3605b09969d6SVaclav Hapla   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
3606b09969d6SVaclav Hapla   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
3607b09969d6SVaclav Hapla   ierr = PetscSectionSetFieldComponents(coordSection, 0, spaceDim);CHKERRQ(ierr);
36081edcf0b2SVaclav Hapla   ierr = PetscSectionSetChart(coordSection, vStart, vEnd);CHKERRQ(ierr);
36091edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
3610b09969d6SVaclav Hapla     ierr = PetscSectionSetDof(coordSection, v, spaceDim);CHKERRQ(ierr);
3611b09969d6SVaclav Hapla     ierr = PetscSectionSetFieldDof(coordSection, v, 0, spaceDim);CHKERRQ(ierr);
3612b09969d6SVaclav Hapla   }
3613b09969d6SVaclav Hapla   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
3614b09969d6SVaclav Hapla 
3615b09969d6SVaclav Hapla   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
3616b09969d6SVaclav Hapla   ierr = DMCreateLocalVector(cdm, &coordinates);CHKERRQ(ierr);
3617b09969d6SVaclav Hapla   ierr = VecSetBlockSize(coordinates, spaceDim);CHKERRQ(ierr);
3618b09969d6SVaclav Hapla   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
361999946890SBarry Smith   ierr = VecGetArrayWrite(coordinates, &coords);CHKERRQ(ierr);
36201edcf0b2SVaclav Hapla   for (v = 0; v < vEnd-vStart; ++v) {
3621b09969d6SVaclav Hapla     for (d = 0; d < spaceDim; ++d) {
3622b09969d6SVaclav Hapla       coords[v*spaceDim+d] = vertexCoords[v*spaceDim+d];
3623b09969d6SVaclav Hapla     }
3624b09969d6SVaclav Hapla   }
362599946890SBarry Smith   ierr = VecRestoreArrayWrite(coordinates, &coords);CHKERRQ(ierr);
3626b09969d6SVaclav Hapla   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
3627b09969d6SVaclav Hapla   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
3628b09969d6SVaclav Hapla   ierr = PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0);CHKERRQ(ierr);
3629b09969d6SVaclav Hapla   PetscFunctionReturn(0);
3630b09969d6SVaclav Hapla }
3631b09969d6SVaclav Hapla 
3632b09969d6SVaclav Hapla /*@
36333df08285SMatthew G. Knepley   DMPlexCreateFromCellListPetsc - Create DMPLEX from a list of vertices for each cell (common mesh generator output), but only process 0 takes in the input
36343df08285SMatthew G. Knepley 
36353df08285SMatthew G. Knepley   Collective on comm
3636b09969d6SVaclav Hapla 
3637b09969d6SVaclav Hapla   Input Parameters:
3638b09969d6SVaclav Hapla + comm - The communicator
3639b09969d6SVaclav Hapla . dim - The topological dimension of the mesh
36403df08285SMatthew G. Knepley . numCells - The number of cells, only on process 0
36413df08285SMatthew G. Knepley . numVertices - The number of vertices owned by this process, or PETSC_DECIDE, only on process 0
36423df08285SMatthew G. Knepley . numCorners - The number of vertices for each cell, only on process 0
3643b09969d6SVaclav Hapla . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
36443df08285SMatthew G. Knepley . cells - An array of numCells*numCorners numbers, the vertices for each cell, only on process 0
3645b09969d6SVaclav Hapla . spaceDim - The spatial dimension used for coordinates
36463df08285SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex, only on process 0
3647b09969d6SVaclav Hapla 
3648b09969d6SVaclav Hapla   Output Parameter:
36493df08285SMatthew G. Knepley . dm - The DM, which only has points on process 0
3650b09969d6SVaclav Hapla 
3651b09969d6SVaclav Hapla   Notes:
3652b09969d6SVaclav Hapla   This function is just a convenient sequence of DMCreate(), DMSetType(), DMSetDimension(), DMPlexBuildFromCellList(),
3653b09969d6SVaclav Hapla   DMPlexInterpolate(), DMPlexBuildCoordinatesFromCellList()
3654b09969d6SVaclav Hapla 
365525b6865aSVaclav Hapla   See DMPlexBuildFromCellList() for an example and details about the topology-related parameters.
365625b6865aSVaclav Hapla   See DMPlexBuildCoordinatesFromCellList() for details about the geometry-related parameters.
36573df08285SMatthew G. Knepley   See DMPlexCreateFromCellListParallelPetsc() for parallel input
365825b6865aSVaclav Hapla 
3659b09969d6SVaclav Hapla   Level: intermediate
3660b09969d6SVaclav Hapla 
3661b09969d6SVaclav Hapla .seealso: DMPlexCreateFromCellListParallelPetsc(), DMPlexBuildFromCellList(), DMPlexBuildCoordinatesFromCellList(), DMPlexCreateFromDAG(), DMPlexCreate()
36629298eaa6SMatthew G Knepley @*/
3663a4a685f2SJacob 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)
36649298eaa6SMatthew G Knepley {
36653df08285SMatthew G. Knepley   PetscMPIInt    rank;
36669298eaa6SMatthew G Knepley   PetscErrorCode ierr;
36679298eaa6SMatthew G Knepley 
36689298eaa6SMatthew G Knepley   PetscFunctionBegin;
36695fd8819aSMatthew 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.");
36703df08285SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
36719298eaa6SMatthew G Knepley   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
36729298eaa6SMatthew G Knepley   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
3673c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(*dm, dim);CHKERRQ(ierr);
36743df08285SMatthew G. Knepley   if (!rank) {ierr = DMPlexBuildFromCellList(*dm, numCells, numVertices, numCorners, cells);CHKERRQ(ierr);}
36753df08285SMatthew G. Knepley   else       {ierr = DMPlexBuildFromCellList(*dm, 0, 0, 0, NULL);CHKERRQ(ierr);}
36769298eaa6SMatthew G Knepley   if (interpolate) {
36775fd9971aSMatthew G. Knepley     DM idm;
36789298eaa6SMatthew G Knepley 
36799298eaa6SMatthew G Knepley     ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr);
36809298eaa6SMatthew G Knepley     ierr = DMDestroy(dm);CHKERRQ(ierr);
36819298eaa6SMatthew G Knepley     *dm  = idm;
36829298eaa6SMatthew G Knepley   }
36833df08285SMatthew G. Knepley   if (!rank) {ierr = DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, vertexCoords);CHKERRQ(ierr);}
36843df08285SMatthew G. Knepley   else       {ierr = DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, NULL);CHKERRQ(ierr);}
36859298eaa6SMatthew G Knepley   PetscFunctionReturn(0);
36869298eaa6SMatthew G Knepley }
36879298eaa6SMatthew G Knepley 
3688939f6067SMatthew G. Knepley /*@
3689a4a685f2SJacob Faibussowitsch   DMPlexCreateFromCellList - Deprecated, use DMPlexCreateFromCellListPetsc()
3690a4a685f2SJacob Faibussowitsch 
3691a4a685f2SJacob Faibussowitsch   Level: deprecated
3692a4a685f2SJacob Faibussowitsch 
3693a4a685f2SJacob Faibussowitsch .seealso: DMPlexCreateFromCellListPetsc()
3694a4a685f2SJacob Faibussowitsch @*/
3695a4a685f2SJacob 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)
3696a4a685f2SJacob Faibussowitsch {
3697a4a685f2SJacob Faibussowitsch   PetscErrorCode ierr;
3698a4a685f2SJacob Faibussowitsch   PetscInt       i;
3699a4a685f2SJacob Faibussowitsch   PetscInt       *pintCells;
3700a4a685f2SJacob Faibussowitsch   PetscReal      *prealVC;
3701a4a685f2SJacob Faibussowitsch 
3702a4a685f2SJacob Faibussowitsch   PetscFunctionBegin;
3703a4a685f2SJacob 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));
3704a4a685f2SJacob Faibussowitsch   if (sizeof(int) == sizeof(PetscInt)) {
3705a4a685f2SJacob Faibussowitsch     pintCells = (PetscInt *) cells;
3706a4a685f2SJacob Faibussowitsch   } else {
3707a4a685f2SJacob Faibussowitsch     ierr = PetscMalloc1(numCells*numCorners, &pintCells);CHKERRQ(ierr);
3708a4a685f2SJacob Faibussowitsch     for (i = 0; i < numCells*numCorners; i++) {
3709a4a685f2SJacob Faibussowitsch       pintCells[i] = (PetscInt) cells[i];
3710a4a685f2SJacob Faibussowitsch     }
3711a4a685f2SJacob Faibussowitsch   }
3712a4a685f2SJacob 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));
3713a4a685f2SJacob Faibussowitsch   if (sizeof(double) == sizeof(PetscReal)) {
3714a4a685f2SJacob Faibussowitsch     prealVC = (PetscReal *) vertexCoords;
3715a4a685f2SJacob Faibussowitsch   } else {
3716a4a685f2SJacob Faibussowitsch     ierr = PetscMalloc1(numVertices*spaceDim, &prealVC);CHKERRQ(ierr);
3717a4a685f2SJacob Faibussowitsch     for (i = 0; i < numVertices*spaceDim; i++) {
3718a4a685f2SJacob Faibussowitsch       prealVC[i] = (PetscReal) vertexCoords[i];
3719a4a685f2SJacob Faibussowitsch     }
3720a4a685f2SJacob Faibussowitsch   }
3721a4a685f2SJacob Faibussowitsch   ierr = DMPlexCreateFromCellListPetsc(comm, dim, numCells, numVertices, numCorners, interpolate, pintCells, spaceDim, prealVC, dm);CHKERRQ(ierr);
3722a4a685f2SJacob Faibussowitsch   if (sizeof(int) != sizeof(PetscInt)) {
3723a4a685f2SJacob Faibussowitsch     ierr = PetscFree(pintCells);CHKERRQ(ierr);
3724a4a685f2SJacob Faibussowitsch   }
3725a4a685f2SJacob Faibussowitsch   if (sizeof(double) != sizeof(PetscReal)) {
3726a4a685f2SJacob Faibussowitsch     ierr = PetscFree(prealVC);CHKERRQ(ierr);
3727a4a685f2SJacob Faibussowitsch   }
3728a4a685f2SJacob Faibussowitsch   PetscFunctionReturn(0);
3729a4a685f2SJacob Faibussowitsch }
3730a4a685f2SJacob Faibussowitsch 
3731a4a685f2SJacob Faibussowitsch /*@
3732939f6067SMatthew 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
3733939f6067SMatthew G. Knepley 
3734939f6067SMatthew G. Knepley   Input Parameters:
3735c73cfb54SMatthew G. Knepley + dm - The empty DM object, usually from DMCreate() and DMSetDimension()
3736939f6067SMatthew G. Knepley . depth - The depth of the DAG
3737367003a6SStefano Zampini . numPoints - Array of size depth + 1 containing the number of points at each depth
3738939f6067SMatthew G. Knepley . coneSize - The cone size of each point
3739939f6067SMatthew G. Knepley . cones - The concatenation of the cone points for each point, the cone list must be oriented correctly for each point
3740939f6067SMatthew G. Knepley . coneOrientations - The orientation of each cone point
3741367003a6SStefano Zampini - vertexCoords - An array of numPoints[0]*spacedim numbers representing the coordinates of each vertex, with spacedim the value set via DMSetCoordinateDim()
3742939f6067SMatthew G. Knepley 
3743939f6067SMatthew G. Knepley   Output Parameter:
3744939f6067SMatthew G. Knepley . dm - The DM
3745939f6067SMatthew G. Knepley 
3746939f6067SMatthew G. Knepley   Note: Two triangles sharing a face would have input
3747939f6067SMatthew G. Knepley $  depth = 1, numPoints = [4 2], coneSize = [3 3 0 0 0 0]
3748939f6067SMatthew G. Knepley $  cones = [2 3 4  3 5 4], coneOrientations = [0 0 0  0 0 0]
3749939f6067SMatthew G. Knepley $ vertexCoords = [-1.0 0.0  0.0 -1.0  0.0 1.0  1.0 0.0]
3750939f6067SMatthew G. Knepley $
3751939f6067SMatthew G. Knepley which would result in the DMPlex
3752939f6067SMatthew G. Knepley $
3753939f6067SMatthew G. Knepley $        4
3754939f6067SMatthew G. Knepley $      / | \
3755939f6067SMatthew G. Knepley $     /  |  \
3756939f6067SMatthew G. Knepley $    /   |   \
3757939f6067SMatthew G. Knepley $   2  0 | 1  5
3758939f6067SMatthew G. Knepley $    \   |   /
3759939f6067SMatthew G. Knepley $     \  |  /
3760939f6067SMatthew G. Knepley $      \ | /
3761939f6067SMatthew G. Knepley $        3
3762939f6067SMatthew G. Knepley $
3763a4a685f2SJacob Faibussowitsch $ Notice that all points are numbered consecutively, unlike DMPlexCreateFromCellListPetsc()
3764939f6067SMatthew G. Knepley 
3765939f6067SMatthew G. Knepley   Level: advanced
3766939f6067SMatthew G. Knepley 
3767a4a685f2SJacob Faibussowitsch .seealso: DMPlexCreateFromCellListPetsc(), DMPlexCreate()
3768939f6067SMatthew G. Knepley @*/
37699298eaa6SMatthew G Knepley PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[])
37709298eaa6SMatthew G Knepley {
37719298eaa6SMatthew G Knepley   Vec            coordinates;
37729298eaa6SMatthew G Knepley   PetscSection   coordSection;
37739298eaa6SMatthew G Knepley   PetscScalar    *coords;
3774811e8653SToby Isaac   PetscInt       coordSize, firstVertex = -1, pStart = 0, pEnd = 0, p, v, dim, dimEmbed, d, off;
37759298eaa6SMatthew G Knepley   PetscErrorCode ierr;
37769298eaa6SMatthew G Knepley 
37779298eaa6SMatthew G Knepley   PetscFunctionBegin;
3778c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3779811e8653SToby Isaac   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
378089c010cfSBarry Smith   if (dimEmbed < dim) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Embedding dimension %D cannot be less than intrinsic dimension %d",dimEmbed,dim);
37819298eaa6SMatthew G Knepley   for (d = 0; d <= depth; ++d) pEnd += numPoints[d];
37829298eaa6SMatthew G Knepley   ierr = DMPlexSetChart(dm, pStart, pEnd);CHKERRQ(ierr);
37839298eaa6SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
37849298eaa6SMatthew G Knepley     ierr = DMPlexSetConeSize(dm, p, coneSize[p-pStart]);CHKERRQ(ierr);
378597e052ccSToby Isaac     if (firstVertex < 0 && !coneSize[p - pStart]) {
378697e052ccSToby Isaac       firstVertex = p - pStart;
37879298eaa6SMatthew G Knepley     }
378897e052ccSToby Isaac   }
378989c010cfSBarry Smith   if (firstVertex < 0 && numPoints[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected %D vertices but could not find any", numPoints[0]);
37909298eaa6SMatthew G Knepley   ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
37919298eaa6SMatthew G Knepley   for (p = pStart, off = 0; p < pEnd; off += coneSize[p-pStart], ++p) {
37929298eaa6SMatthew G Knepley     ierr = DMPlexSetCone(dm, p, &cones[off]);CHKERRQ(ierr);
37939298eaa6SMatthew G Knepley     ierr = DMPlexSetConeOrientation(dm, p, &coneOrientations[off]);CHKERRQ(ierr);
37949298eaa6SMatthew G Knepley   }
37959298eaa6SMatthew G Knepley   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
37969298eaa6SMatthew G Knepley   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
37979298eaa6SMatthew G Knepley   /* Build coordinates */
3798c2166f76SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
37999298eaa6SMatthew G Knepley   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
3800811e8653SToby Isaac   ierr = PetscSectionSetFieldComponents(coordSection, 0, dimEmbed);CHKERRQ(ierr);
38019298eaa6SMatthew G Knepley   ierr = PetscSectionSetChart(coordSection, firstVertex, firstVertex+numPoints[0]);CHKERRQ(ierr);
38029298eaa6SMatthew G Knepley   for (v = firstVertex; v < firstVertex+numPoints[0]; ++v) {
3803811e8653SToby Isaac     ierr = PetscSectionSetDof(coordSection, v, dimEmbed);CHKERRQ(ierr);
3804811e8653SToby Isaac     ierr = PetscSectionSetFieldDof(coordSection, v, 0, dimEmbed);CHKERRQ(ierr);
38059298eaa6SMatthew G Knepley   }
38069298eaa6SMatthew G Knepley   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
38079298eaa6SMatthew G Knepley   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
38088b9ced59SLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
38096f8cbbeeSMatthew G Knepley   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
38109298eaa6SMatthew G Knepley   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
38118b9ced59SLisandro Dalcin   ierr = VecSetBlockSize(coordinates, dimEmbed);CHKERRQ(ierr);
38122eb5907fSJed Brown   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
38139318fe57SMatthew G. Knepley   if (vertexCoords) {
38149298eaa6SMatthew G Knepley     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
38159298eaa6SMatthew G Knepley     for (v = 0; v < numPoints[0]; ++v) {
38169298eaa6SMatthew G Knepley       PetscInt off;
38179298eaa6SMatthew G Knepley 
38189298eaa6SMatthew G Knepley       ierr = PetscSectionGetOffset(coordSection, v+firstVertex, &off);CHKERRQ(ierr);
3819811e8653SToby Isaac       for (d = 0; d < dimEmbed; ++d) {
3820811e8653SToby Isaac         coords[off+d] = vertexCoords[v*dimEmbed+d];
38219298eaa6SMatthew G Knepley       }
38229298eaa6SMatthew G Knepley     }
38239318fe57SMatthew G. Knepley   }
38249298eaa6SMatthew G Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
38259298eaa6SMatthew G Knepley   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
38269298eaa6SMatthew G Knepley   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
38279298eaa6SMatthew G Knepley   PetscFunctionReturn(0);
38289298eaa6SMatthew G Knepley }
38298415267dSToby Isaac 
3830ca522641SMatthew G. Knepley /*@C
38318ca92349SMatthew G. Knepley   DMPlexCreateCellVertexFromFile - Create a DMPlex mesh from a simple cell-vertex file.
38328ca92349SMatthew G. Knepley 
38338ca92349SMatthew G. Knepley + comm        - The MPI communicator
38348ca92349SMatthew G. Knepley . filename    - Name of the .dat file
38358ca92349SMatthew G. Knepley - interpolate - Create faces and edges in the mesh
38368ca92349SMatthew G. Knepley 
38378ca92349SMatthew G. Knepley   Output Parameter:
38388ca92349SMatthew G. Knepley . dm  - The DM object representing the mesh
38398ca92349SMatthew G. Knepley 
38408ca92349SMatthew G. Knepley   Note: The format is the simplest possible:
38418ca92349SMatthew G. Knepley $ Ne
38428ca92349SMatthew G. Knepley $ v0 v1 ... vk
38438ca92349SMatthew G. Knepley $ Nv
38448ca92349SMatthew G. Knepley $ x y z marker
38458ca92349SMatthew G. Knepley 
38468ca92349SMatthew G. Knepley   Level: beginner
38478ca92349SMatthew G. Knepley 
38488ca92349SMatthew G. Knepley .seealso: DMPlexCreateFromFile(), DMPlexCreateMedFromFile(), DMPlexCreateGmsh(), DMPlexCreate()
38498ca92349SMatthew G. Knepley @*/
38508ca92349SMatthew G. Knepley PetscErrorCode DMPlexCreateCellVertexFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
38518ca92349SMatthew G. Knepley {
38528ca92349SMatthew G. Knepley   DMLabel         marker;
38538ca92349SMatthew G. Knepley   PetscViewer     viewer;
38548ca92349SMatthew G. Knepley   Vec             coordinates;
38558ca92349SMatthew G. Knepley   PetscSection    coordSection;
38568ca92349SMatthew G. Knepley   PetscScalar    *coords;
38578ca92349SMatthew G. Knepley   char            line[PETSC_MAX_PATH_LEN];
38588ca92349SMatthew G. Knepley   PetscInt        dim = 3, cdim = 3, coordSize, v, c, d;
38598ca92349SMatthew G. Knepley   PetscMPIInt     rank;
3860*f8d5e320SMatthew G. Knepley   int             snum, Nv, Nc, Ncn, Nl;
38618ca92349SMatthew G. Knepley   PetscErrorCode  ierr;
38628ca92349SMatthew G. Knepley 
38638ca92349SMatthew G. Knepley   PetscFunctionBegin;
3864ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
38658ca92349SMatthew G. Knepley   ierr = PetscViewerCreate(comm, &viewer);CHKERRQ(ierr);
38668ca92349SMatthew G. Knepley   ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
38678ca92349SMatthew G. Knepley   ierr = PetscViewerFileSetMode(viewer, FILE_MODE_READ);CHKERRQ(ierr);
38688ca92349SMatthew G. Knepley   ierr = PetscViewerFileSetName(viewer, filename);CHKERRQ(ierr);
3869dd400576SPatrick Sanan   if (rank == 0) {
3870*f8d5e320SMatthew G. Knepley     ierr = PetscViewerRead(viewer, line, 4, NULL, PETSC_STRING);CHKERRQ(ierr);
3871*f8d5e320SMatthew G. Knepley     snum = sscanf(line, "%d %d %d %d", &Nc, &Nv, &Ncn, &Nl);
3872*f8d5e320SMatthew G. Knepley     if (snum != 4) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
387325ce1634SJed Brown   } else {
3874*f8d5e320SMatthew G. Knepley     Nc = Nv = Ncn = Nl = 0;
38758ca92349SMatthew G. Knepley   }
38768ca92349SMatthew G. Knepley   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
38778ca92349SMatthew G. Knepley   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
38788ca92349SMatthew G. Knepley   ierr = DMPlexSetChart(*dm, 0, Nc+Nv);CHKERRQ(ierr);
38798ca92349SMatthew G. Knepley   ierr = DMSetDimension(*dm, dim);CHKERRQ(ierr);
38808ca92349SMatthew G. Knepley   ierr = DMSetCoordinateDim(*dm, cdim);CHKERRQ(ierr);
38818ca92349SMatthew G. Knepley   /* Read topology */
3882dd400576SPatrick Sanan   if (rank == 0) {
3883*f8d5e320SMatthew G. Knepley     char     format[PETSC_MAX_PATH_LEN];
3884*f8d5e320SMatthew G. Knepley     PetscInt cone[8];
38858ca92349SMatthew G. Knepley     int      vbuf[8], v;
38868ca92349SMatthew G. Knepley 
3887*f8d5e320SMatthew G. Knepley     for (c = 0; c < Ncn; ++c) {format[c*3+0] = '%'; format[c*3+1] = 'd'; format[c*3+2] = ' ';}
3888*f8d5e320SMatthew G. Knepley     format[Ncn*3-1] = '\0';
3889*f8d5e320SMatthew G. Knepley     for (c = 0; c < Nc; ++c) {ierr = DMPlexSetConeSize(*dm, c, Ncn);CHKERRQ(ierr);}
38908ca92349SMatthew G. Knepley     ierr = DMSetUp(*dm);CHKERRQ(ierr);
38918ca92349SMatthew G. Knepley     for (c = 0; c < Nc; ++c) {
3892*f8d5e320SMatthew G. Knepley       ierr = PetscViewerRead(viewer, line, Ncn, NULL, PETSC_STRING);CHKERRQ(ierr);
3893*f8d5e320SMatthew G. Knepley       switch (Ncn) {
3894*f8d5e320SMatthew G. Knepley         case 2: snum = sscanf(line, format, &vbuf[0], &vbuf[1]);break;
3895*f8d5e320SMatthew G. Knepley         case 3: snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2]);break;
3896*f8d5e320SMatthew G. Knepley         case 4: snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3]);break;
3897*f8d5e320SMatthew G. Knepley         case 6: snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5]);break;
3898*f8d5e320SMatthew G. Knepley         case 8: snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5], &vbuf[6], &vbuf[7]);break;
3899*f8d5e320SMatthew G. Knepley         default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No cell shape with %D vertices", Ncn);
3900*f8d5e320SMatthew G. Knepley       }
3901*f8d5e320SMatthew G. Knepley       if (snum != Ncn) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
3902*f8d5e320SMatthew G. Knepley       for (v = 0; v < Ncn; ++v) cone[v] = vbuf[v] + Nc;
39038ca92349SMatthew G. Knepley       /* Hexahedra are inverted */
3904*f8d5e320SMatthew G. Knepley       if (Ncn == 8) {
39058ca92349SMatthew G. Knepley         PetscInt tmp = cone[1];
39068ca92349SMatthew G. Knepley         cone[1] = cone[3];
39078ca92349SMatthew G. Knepley         cone[3] = tmp;
39088ca92349SMatthew G. Knepley       }
39098ca92349SMatthew G. Knepley       ierr = DMPlexSetCone(*dm, c, cone);CHKERRQ(ierr);
39108ca92349SMatthew G. Knepley     }
39118ca92349SMatthew G. Knepley   }
39128ca92349SMatthew G. Knepley   ierr = DMPlexSymmetrize(*dm);CHKERRQ(ierr);
39138ca92349SMatthew G. Knepley   ierr = DMPlexStratify(*dm);CHKERRQ(ierr);
39148ca92349SMatthew G. Knepley   /* Read coordinates */
39158ca92349SMatthew G. Knepley   ierr = DMGetCoordinateSection(*dm, &coordSection);CHKERRQ(ierr);
39168ca92349SMatthew G. Knepley   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
39178ca92349SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(coordSection, 0, cdim);CHKERRQ(ierr);
39188ca92349SMatthew G. Knepley   ierr = PetscSectionSetChart(coordSection, Nc, Nc + Nv);CHKERRQ(ierr);
39198ca92349SMatthew G. Knepley   for (v = Nc; v < Nc+Nv; ++v) {
39208ca92349SMatthew G. Knepley     ierr = PetscSectionSetDof(coordSection, v, cdim);CHKERRQ(ierr);
39218ca92349SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(coordSection, v, 0, cdim);CHKERRQ(ierr);
39228ca92349SMatthew G. Knepley   }
39238ca92349SMatthew G. Knepley   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
39248ca92349SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
39258ca92349SMatthew G. Knepley   ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
39268ca92349SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
39278ca92349SMatthew G. Knepley   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
39288ca92349SMatthew G. Knepley   ierr = VecSetBlockSize(coordinates, cdim);CHKERRQ(ierr);
39298ca92349SMatthew G. Knepley   ierr = VecSetType(coordinates, VECSTANDARD);CHKERRQ(ierr);
39308ca92349SMatthew G. Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
3931dd400576SPatrick Sanan   if (rank == 0) {
3932*f8d5e320SMatthew G. Knepley     char   format[PETSC_MAX_PATH_LEN];
39338ca92349SMatthew G. Knepley     double x[3];
3934*f8d5e320SMatthew G. Knepley     int    l, val[3];
39358ca92349SMatthew G. Knepley 
3936*f8d5e320SMatthew G. Knepley     if (Nl) {
3937*f8d5e320SMatthew G. Knepley       for (l = 0; l < Nl; ++l) {format[l*3+0] = '%'; format[l*3+1] = 'd'; format[l*3+2] = ' ';}
3938*f8d5e320SMatthew G. Knepley       format[Nl*3-1] = '\0';
39398ca92349SMatthew G. Knepley       ierr = DMCreateLabel(*dm, "marker");CHKERRQ(ierr);
39408ca92349SMatthew G. Knepley       ierr = DMGetLabel(*dm, "marker", &marker);CHKERRQ(ierr);
3941*f8d5e320SMatthew G. Knepley     }
39428ca92349SMatthew G. Knepley     for (v = 0; v < Nv; ++v) {
3943*f8d5e320SMatthew G. Knepley       ierr = PetscViewerRead(viewer, line, 3+Nl, NULL, PETSC_STRING);CHKERRQ(ierr);
3944*f8d5e320SMatthew G. Knepley       snum = sscanf(line, "%lg %lg %lg", &x[0], &x[1], &x[2]);
3945*f8d5e320SMatthew G. Knepley       if (snum != 3) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
3946*f8d5e320SMatthew G. Knepley       switch (Nl) {
3947*f8d5e320SMatthew G. Knepley         case 0: snum = 0;break;
3948*f8d5e320SMatthew G. Knepley         case 1: snum = sscanf(line, format, &val[0]);break;
3949*f8d5e320SMatthew G. Knepley         case 2: snum = sscanf(line, format, &val[0], &val[1]);break;
3950*f8d5e320SMatthew G. Knepley         case 3: snum = sscanf(line, format, &val[0], &val[1], &val[2]);break;
3951*f8d5e320SMatthew G. Knepley         default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Request support for %D labels", Nl);
3952*f8d5e320SMatthew G. Knepley       }
3953*f8d5e320SMatthew G. Knepley       if (snum != Nl) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
39548ca92349SMatthew G. Knepley       for (d = 0; d < cdim; ++d) coords[v*cdim+d] = x[d];
3955*f8d5e320SMatthew G. Knepley       for (l = 0; l < Nl; ++l) {ierr = DMLabelSetValue(marker, v+Nc, val[l]);CHKERRQ(ierr);}
39568ca92349SMatthew G. Knepley     }
39578ca92349SMatthew G. Knepley   }
39588ca92349SMatthew G. Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
39598ca92349SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(*dm, coordinates);CHKERRQ(ierr);
39608ca92349SMatthew G. Knepley   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
39618ca92349SMatthew G. Knepley   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
39628ca92349SMatthew G. Knepley   if (interpolate) {
39638ca92349SMatthew G. Knepley     DM      idm;
39648ca92349SMatthew G. Knepley     DMLabel bdlabel;
39658ca92349SMatthew G. Knepley 
39668ca92349SMatthew G. Knepley     ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr);
39678ca92349SMatthew G. Knepley     ierr = DMDestroy(dm);CHKERRQ(ierr);
39688ca92349SMatthew G. Knepley     *dm  = idm;
39698ca92349SMatthew G. Knepley 
3970*f8d5e320SMatthew G. Knepley     if (!Nl) {
3971*f8d5e320SMatthew G. Knepley       ierr = DMCreateLabel(*dm, "marker");CHKERRQ(ierr);
39728ca92349SMatthew G. Knepley       ierr = DMGetLabel(*dm, "marker", &bdlabel);CHKERRQ(ierr);
39738ca92349SMatthew G. Knepley       ierr = DMPlexMarkBoundaryFaces(*dm, PETSC_DETERMINE, bdlabel);CHKERRQ(ierr);
39748ca92349SMatthew G. Knepley       ierr = DMPlexLabelComplete(*dm, bdlabel);CHKERRQ(ierr);
39758ca92349SMatthew G. Knepley     }
3976*f8d5e320SMatthew G. Knepley   }
39778ca92349SMatthew G. Knepley   PetscFunctionReturn(0);
39788ca92349SMatthew G. Knepley }
39798ca92349SMatthew G. Knepley 
39808ca92349SMatthew G. Knepley /*@C
3981ca522641SMatthew G. Knepley   DMPlexCreateFromFile - This takes a filename and produces a DM
3982ca522641SMatthew G. Knepley 
3983ca522641SMatthew G. Knepley   Input Parameters:
3984ca522641SMatthew G. Knepley + comm - The communicator
3985ca522641SMatthew G. Knepley . filename - A file name
3986ca522641SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
3987ca522641SMatthew G. Knepley 
3988ca522641SMatthew G. Knepley   Output Parameter:
3989ca522641SMatthew G. Knepley . dm - The DM
3990ca522641SMatthew G. Knepley 
399102ef0d99SVaclav Hapla   Options Database Keys:
399202ef0d99SVaclav Hapla . -dm_plex_create_from_hdf5_xdmf - use the PETSC_VIEWER_HDF5_XDMF format for reading HDF5
399302ef0d99SVaclav Hapla 
3994bca97951SVaclav Hapla   Use -dm_plex_create_ prefix to pass options to the internal PetscViewer, e.g.
3995bca97951SVaclav Hapla $ -dm_plex_create_viewer_hdf5_collective
3996bca97951SVaclav Hapla 
3997ca522641SMatthew G. Knepley   Level: beginner
3998ca522641SMatthew G. Knepley 
3999a4a685f2SJacob Faibussowitsch .seealso: DMPlexCreateFromDAG(), DMPlexCreateFromCellListPetsc(), DMPlexCreate()
4000ca522641SMatthew G. Knepley @*/
4001ca522641SMatthew G. Knepley PetscErrorCode DMPlexCreateFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
4002ca522641SMatthew G. Knepley {
4003ca522641SMatthew G. Knepley   const char    *extGmsh      = ".msh";
4004de78e4feSLisandro Dalcin   const char    *extGmsh2     = ".msh2";
4005de78e4feSLisandro Dalcin   const char    *extGmsh4     = ".msh4";
4006ca522641SMatthew G. Knepley   const char    *extCGNS      = ".cgns";
4007ca522641SMatthew G. Knepley   const char    *extExodus    = ".exo";
400890c68965SMatthew G. Knepley   const char    *extGenesis   = ".gen";
40092f0bd6dcSMichael Lange   const char    *extFluent    = ".cas";
4010cc2f8f65SMatthew G. Knepley   const char    *extHDF5      = ".h5";
4011707dd687SMichael Lange   const char    *extMed       = ".med";
4012f2801cd6SMatthew G. Knepley   const char    *extPLY       = ".ply";
4013c1cad2e7SMatthew G. Knepley   const char    *extEGADSLite = ".egadslite";
4014c1cad2e7SMatthew G. Knepley   const char    *extEGADS     = ".egads";
4015c1cad2e7SMatthew G. Knepley   const char    *extIGES      = ".igs";
4016c1cad2e7SMatthew G. Knepley   const char    *extSTEP      = ".stp";
40178ca92349SMatthew G. Knepley   const char    *extCV        = ".dat";
4018ca522641SMatthew G. Knepley   size_t         len;
4019c1cad2e7SMatthew G. Knepley   PetscBool      isGmsh, isGmsh2, isGmsh4, isCGNS, isExodus, isGenesis, isFluent, isHDF5, isMed, isPLY, isEGADSLite, isEGADS, isIGES, isSTEP, isCV;
4020ca522641SMatthew G. Knepley   PetscMPIInt    rank;
4021ca522641SMatthew G. Knepley   PetscErrorCode ierr;
4022ca522641SMatthew G. Knepley 
4023ca522641SMatthew G. Knepley   PetscFunctionBegin;
40245d80c0bfSVaclav Hapla   PetscValidCharPointer(filename, 2);
4025ca522641SMatthew G. Knepley   PetscValidPointer(dm, 4);
4026f1f45c63SVaclav Hapla   ierr = DMInitializePackage();CHKERRQ(ierr);
4027f1f45c63SVaclav Hapla   ierr = PetscLogEventBegin(DMPLEX_CreateFromFile,0,0,0,0);CHKERRQ(ierr);
4028ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
4029ca522641SMatthew G. Knepley   ierr = PetscStrlen(filename, &len);CHKERRQ(ierr);
4030ca522641SMatthew G. Knepley   if (!len) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Filename must be a valid path");
4031ca522641SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)],  extGmsh,      4, &isGmsh);CHKERRQ(ierr);
4032de78e4feSLisandro Dalcin   ierr = PetscStrncmp(&filename[PetscMax(0,len-5)],  extGmsh2,     5, &isGmsh2);CHKERRQ(ierr);
4033de78e4feSLisandro Dalcin   ierr = PetscStrncmp(&filename[PetscMax(0,len-5)],  extGmsh4,     5, &isGmsh4);CHKERRQ(ierr);
4034ca522641SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-5)],  extCGNS,      5, &isCGNS);CHKERRQ(ierr);
4035ca522641SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)],  extExodus,    4, &isExodus);CHKERRQ(ierr);
403690c68965SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)],  extGenesis,   4, &isGenesis);CHKERRQ(ierr);
40372f0bd6dcSMichael Lange   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)],  extFluent,    4, &isFluent);CHKERRQ(ierr);
4038cc2f8f65SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-3)],  extHDF5,      3, &isHDF5);CHKERRQ(ierr);
4039707dd687SMichael Lange   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)],  extMed,       4, &isMed);CHKERRQ(ierr);
4040f2801cd6SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)],  extPLY,       4, &isPLY);CHKERRQ(ierr);
4041c1cad2e7SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-10)], extEGADSLite, 10, &isEGADSLite);CHKERRQ(ierr);
4042c1cad2e7SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-6)],  extEGADS,     6, &isEGADS);CHKERRQ(ierr);
4043c1cad2e7SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)],  extIGES,      4, &isIGES);CHKERRQ(ierr);
4044c1cad2e7SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)],  extSTEP,      4, &isSTEP);CHKERRQ(ierr);
40458ca92349SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)],  extCV,        4, &isCV);CHKERRQ(ierr);
4046de78e4feSLisandro Dalcin   if (isGmsh || isGmsh2 || isGmsh4) {
40477d282ae0SMichael Lange     ierr = DMPlexCreateGmshFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
4048ca522641SMatthew G. Knepley   } else if (isCGNS) {
4049ca522641SMatthew G. Knepley     ierr = DMPlexCreateCGNSFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
405090c68965SMatthew G. Knepley   } else if (isExodus || isGenesis) {
4051ca522641SMatthew G. Knepley     ierr = DMPlexCreateExodusFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
40522f0bd6dcSMichael Lange   } else if (isFluent) {
40532f0bd6dcSMichael Lange     ierr = DMPlexCreateFluentFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
4054cc2f8f65SMatthew G. Knepley   } else if (isHDF5) {
40559c48423bSVaclav Hapla     PetscBool      load_hdf5_xdmf = PETSC_FALSE;
4056cc2f8f65SMatthew G. Knepley     PetscViewer viewer;
4057cc2f8f65SMatthew G. Knepley 
405843b242b4SVaclav 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 */
405943b242b4SVaclav Hapla     ierr = PetscStrncmp(&filename[PetscMax(0,len-8)], ".xdmf",  5, &load_hdf5_xdmf);CHKERRQ(ierr);
40609c48423bSVaclav Hapla     ierr = PetscOptionsGetBool(NULL, NULL, "-dm_plex_create_from_hdf5_xdmf", &load_hdf5_xdmf, NULL);CHKERRQ(ierr);
4061cc2f8f65SMatthew G. Knepley     ierr = PetscViewerCreate(comm, &viewer);CHKERRQ(ierr);
4062cc2f8f65SMatthew G. Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERHDF5);CHKERRQ(ierr);
4063bca97951SVaclav Hapla     ierr = PetscViewerSetOptionsPrefix(viewer, "dm_plex_create_");CHKERRQ(ierr);
4064bca97951SVaclav Hapla     ierr = PetscViewerSetFromOptions(viewer);CHKERRQ(ierr);
4065cc2f8f65SMatthew G. Knepley     ierr = PetscViewerFileSetMode(viewer, FILE_MODE_READ);CHKERRQ(ierr);
4066cc2f8f65SMatthew G. Knepley     ierr = PetscViewerFileSetName(viewer, filename);CHKERRQ(ierr);
4067cc2f8f65SMatthew G. Knepley     ierr = DMCreate(comm, dm);CHKERRQ(ierr);
4068cc2f8f65SMatthew G. Knepley     ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
40699c48423bSVaclav Hapla     if (load_hdf5_xdmf) {ierr = PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_XDMF);CHKERRQ(ierr);}
4070cc2f8f65SMatthew G. Knepley     ierr = DMLoad(*dm, viewer);CHKERRQ(ierr);
40719c48423bSVaclav Hapla     if (load_hdf5_xdmf) {ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);}
4072cc2f8f65SMatthew G. Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
40735fd9971aSMatthew G. Knepley 
40745fd9971aSMatthew G. Knepley     if (interpolate) {
40755fd9971aSMatthew G. Knepley       DM idm;
40765fd9971aSMatthew G. Knepley 
40775fd9971aSMatthew G. Knepley       ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr);
40785fd9971aSMatthew G. Knepley       ierr = DMDestroy(dm);CHKERRQ(ierr);
40795fd9971aSMatthew G. Knepley       *dm  = idm;
40805fd9971aSMatthew G. Knepley     }
4081707dd687SMichael Lange   } else if (isMed) {
4082707dd687SMichael Lange     ierr = DMPlexCreateMedFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
4083f2801cd6SMatthew G. Knepley   } else if (isPLY) {
4084f2801cd6SMatthew G. Knepley     ierr = DMPlexCreatePLYFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
4085c1cad2e7SMatthew G. Knepley   } else if (isEGADSLite || isEGADS || isIGES || isSTEP) {
4086c1cad2e7SMatthew G. Knepley     if (isEGADSLite) {ierr = DMPlexCreateEGADSLiteFromFile(comm, filename, dm);CHKERRQ(ierr);}
4087c1cad2e7SMatthew G. Knepley     else             {ierr = DMPlexCreateEGADSFromFile(comm, filename, dm);CHKERRQ(ierr);}
40887bee2925SMatthew Knepley     if (!interpolate) {
40897bee2925SMatthew Knepley       DM udm;
40907bee2925SMatthew Knepley 
40917bee2925SMatthew Knepley       ierr = DMPlexUninterpolate(*dm, &udm);CHKERRQ(ierr);
40927bee2925SMatthew Knepley       ierr = DMDestroy(dm);CHKERRQ(ierr);
40937bee2925SMatthew Knepley       *dm  = udm;
40947bee2925SMatthew Knepley     }
40958ca92349SMatthew G. Knepley   } else if (isCV) {
40968ca92349SMatthew G. Knepley     ierr = DMPlexCreateCellVertexFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
4097ca522641SMatthew G. Knepley   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename);
4098f1f45c63SVaclav Hapla   ierr = PetscLogEventEnd(DMPLEX_CreateFromFile,0,0,0,0);CHKERRQ(ierr);
4099ca522641SMatthew G. Knepley   PetscFunctionReturn(0);
4100ca522641SMatthew G. Knepley }
4101