xref: /petsc/src/dm/impls/plex/plexcreate.c (revision 0d862eaecdd4cac10b149e8dc135920419d5fe23)
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;
2493cd7e8a5eSksagiyam   PetscBool      flg, flg2, fflg, bdfflg, nameflg;
24949318fe57SMatthew G. Knepley   MPI_Comm       comm;
2495cd7e8a5eSksagiyam   char           filename[PETSC_MAX_PATH_LEN], bdFilename[PETSC_MAX_PATH_LEN], plexname[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);
2503cd7e8a5eSksagiyam   ierr = PetscOptionsString("-dm_plex_name", "Name of the mesh in the file", "DMPlexCreateFromFile", plexname, plexname, sizeof(plexname), &nameflg);CHKERRQ(ierr);
25049318fe57SMatthew G. Knepley   ierr = PetscOptionsEnum("-dm_plex_cell", "Cell shape", "", DMPolytopeTypes, (PetscEnum) cell, (PetscEnum *) &cell, NULL);CHKERRQ(ierr);
25059318fe57SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_reference_cell_domain", "Use a reference cell domain", "", refDomain, &refDomain, NULL);CHKERRQ(ierr);
25069318fe57SMatthew G. Knepley   ierr = PetscOptionsEnum("-dm_plex_shape", "Shape for built-in mesh", "", DMPlexShapes, (PetscEnum) shape, (PetscEnum *) &shape, &flg);CHKERRQ(ierr);
25079318fe57SMatthew G. Knepley   ierr = PetscOptionsBoundedInt("-dm_plex_dim", "Topological dimension of the mesh", "DMGetDimension", dim, &dim, &flg, 0);CHKERRQ(ierr);
25089318fe57SMatthew G. Knepley   if ((dim < 0) || (dim > 3)) SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Dimension %D should be in [1, 3]", dim);
25099318fe57SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_simplex", "Mesh cell shape", "", simplex,  &simplex, &flg);CHKERRQ(ierr);
25109318fe57SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_interpolate", "Flag to create edges and faces automatically", "", interpolate, &interpolate, &flg);CHKERRQ(ierr);
25119318fe57SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_adj_cone", "Set adjacency direction", "DMSetBasicAdjacency", adjCone,  &adjCone, &flg);CHKERRQ(ierr);
25129318fe57SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_adj_closure", "Set adjacency size", "DMSetBasicAdjacency", adjClosure,  &adjClosure, &flg2);CHKERRQ(ierr);
25139318fe57SMatthew G. Knepley   if (flg || flg2) {ierr = DMSetBasicAdjacency(dm, adjCone, adjClosure);CHKERRQ(ierr);}
25149318fe57SMatthew G. Knepley 
251561a622f3SMatthew G. Knepley   switch (cell) {
251661a622f3SMatthew G. Knepley     case DM_POLYTOPE_POINT:
251761a622f3SMatthew G. Knepley     case DM_POLYTOPE_SEGMENT:
251861a622f3SMatthew G. Knepley     case DM_POLYTOPE_POINT_PRISM_TENSOR:
251961a622f3SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
252061a622f3SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
252161a622f3SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
252261a622f3SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
252361a622f3SMatthew G. Knepley       *useCoordSpace = PETSC_TRUE;break;
252461a622f3SMatthew G. Knepley     default: *useCoordSpace = PETSC_FALSE;break;
252561a622f3SMatthew G. Knepley   }
252661a622f3SMatthew G. Knepley 
25279318fe57SMatthew G. Knepley   if (fflg) {
25289318fe57SMatthew G. Knepley     DM dmnew;
25299318fe57SMatthew G. Knepley 
2530cd7e8a5eSksagiyam     ierr = DMPlexCreateFromFile(PetscObjectComm((PetscObject) dm), filename, plexname, interpolate, &dmnew);CHKERRQ(ierr);
25319318fe57SMatthew G. Knepley     ierr = DMPlexReplace_Static(dm, &dmnew);CHKERRQ(ierr);
25329318fe57SMatthew G. Knepley   } else if (refDomain) {
25339318fe57SMatthew G. Knepley     ierr = DMPlexCreateReferenceCell_Internal(dm, cell);CHKERRQ(ierr);
25349318fe57SMatthew G. Knepley   } else if (bdfflg) {
25359318fe57SMatthew G. Knepley     DM bdm, dmnew;
25369318fe57SMatthew G. Knepley 
2537cd7e8a5eSksagiyam     ierr = DMPlexCreateFromFile(PetscObjectComm((PetscObject) dm), bdFilename, plexname, interpolate, &bdm);CHKERRQ(ierr);
25389318fe57SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) bdm, "bd_");CHKERRQ(ierr);
25399318fe57SMatthew G. Knepley     ierr = DMSetFromOptions(bdm);CHKERRQ(ierr);
25409318fe57SMatthew G. Knepley     ierr = DMPlexGenerate(bdm, NULL, interpolate, &dmnew);CHKERRQ(ierr);
25419318fe57SMatthew G. Knepley     ierr = DMDestroy(&bdm);CHKERRQ(ierr);
25429318fe57SMatthew G. Knepley     ierr = DMPlexReplace_Static(dm, &dmnew);CHKERRQ(ierr);
25439318fe57SMatthew G. Knepley   } else {
25449318fe57SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) dm, DMPlexShapes[shape]);CHKERRQ(ierr);
25459318fe57SMatthew G. Knepley     switch (shape) {
25469318fe57SMatthew G. Knepley       case DM_SHAPE_BOX:
25479318fe57SMatthew G. Knepley       {
25489318fe57SMatthew G. Knepley         PetscInt       faces[3] = {0, 0, 0};
25499318fe57SMatthew G. Knepley         PetscReal      lower[3] = {0, 0, 0};
25509318fe57SMatthew G. Knepley         PetscReal      upper[3] = {1, 1, 1};
25519318fe57SMatthew G. Knepley         DMBoundaryType bdt[3]   = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
25529318fe57SMatthew G. Knepley         PetscInt       i, n;
25539318fe57SMatthew G. Knepley 
25549318fe57SMatthew G. Knepley         n    = dim;
25559318fe57SMatthew G. Knepley         for (i = 0; i < dim; ++i) faces[i] = (dim == 1 ? 1 : 4-dim);
25569318fe57SMatthew G. Knepley         ierr = PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg);CHKERRQ(ierr);
25579318fe57SMatthew G. Knepley         n    = 3;
25589318fe57SMatthew G. Knepley         ierr = PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg);CHKERRQ(ierr);
25599318fe57SMatthew G. Knepley         if (flg && (n != dim)) SETERRQ2(comm, PETSC_ERR_ARG_SIZ, "Lower box point had %D values, should have been %D", n, dim);
25609318fe57SMatthew G. Knepley         n    = 3;
25619318fe57SMatthew G. Knepley         ierr = PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg);CHKERRQ(ierr);
25629318fe57SMatthew G. Knepley         if (flg && (n != dim)) SETERRQ2(comm, PETSC_ERR_ARG_SIZ, "Upper box point had %D values, should have been %D", n, dim);
25639318fe57SMatthew G. Knepley         n    = 3;
25649318fe57SMatthew G. Knepley         ierr = PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *) bdt, &n, &flg);CHKERRQ(ierr);
25659318fe57SMatthew G. Knepley         if (flg && (n != dim)) SETERRQ2(comm, PETSC_ERR_ARG_SIZ, "Box boundary types had %D values, should have been %D", n, dim);
25669318fe57SMatthew G. Knepley         switch (cell) {
256761a622f3SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
2568d410b0cfSMatthew G. Knepley             ierr = DMPlexCreateWedgeBoxMesh_Internal(dm, faces, lower, upper, bdt);CHKERRQ(ierr);
2569d410b0cfSMatthew G. Knepley             if (!interpolate) {
2570d410b0cfSMatthew G. Knepley               DM udm;
2571d410b0cfSMatthew G. Knepley 
2572d410b0cfSMatthew G. Knepley               ierr = DMPlexUninterpolate(dm, &udm);CHKERRQ(ierr);
2573d410b0cfSMatthew G. Knepley               ierr = DMPlexReplace_Static(dm, &udm);CHKERRQ(ierr);
2574d410b0cfSMatthew G. Knepley             }
25759318fe57SMatthew G. Knepley             break;
25769318fe57SMatthew G. Knepley           default:
25779318fe57SMatthew G. Knepley             ierr = DMPlexCreateBoxMesh_Internal(dm, dim, simplex, faces, lower, upper, bdt, interpolate);CHKERRQ(ierr);
25789318fe57SMatthew G. Knepley             break;
25799318fe57SMatthew G. Knepley         }
25809318fe57SMatthew G. Knepley       }
25819318fe57SMatthew G. Knepley       break;
25829318fe57SMatthew G. Knepley       case DM_SHAPE_BOX_SURFACE:
25839318fe57SMatthew G. Knepley       {
25849318fe57SMatthew G. Knepley         PetscInt  faces[3] = {0, 0, 0};
25859318fe57SMatthew G. Knepley         PetscReal lower[3] = {0, 0, 0};
25869318fe57SMatthew G. Knepley         PetscReal upper[3] = {1, 1, 1};
25879318fe57SMatthew G. Knepley         PetscInt  i, n;
25889318fe57SMatthew G. Knepley 
25899318fe57SMatthew G. Knepley         n    = dim+1;
25909318fe57SMatthew G. Knepley         for (i = 0; i < dim+1; ++i) faces[i] = (dim+1 == 1 ? 1 : 4-(dim+1));
25919318fe57SMatthew G. Knepley         ierr = PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg);CHKERRQ(ierr);
25929318fe57SMatthew G. Knepley         n    = 3;
25939318fe57SMatthew G. Knepley         ierr = PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg);CHKERRQ(ierr);
25949318fe57SMatthew 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);
25959318fe57SMatthew G. Knepley         n    = 3;
25969318fe57SMatthew G. Knepley         ierr = PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg);CHKERRQ(ierr);
25979318fe57SMatthew 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);
25989318fe57SMatthew G. Knepley         ierr = DMPlexCreateBoxSurfaceMesh_Internal(dm, dim+1, faces, lower, upper, interpolate);CHKERRQ(ierr);
25999318fe57SMatthew G. Knepley       }
26009318fe57SMatthew G. Knepley       break;
26019318fe57SMatthew G. Knepley       case DM_SHAPE_SPHERE:
26029318fe57SMatthew G. Knepley       {
26039318fe57SMatthew G. Knepley         PetscReal R = 1.0;
26049318fe57SMatthew G. Knepley 
26059318fe57SMatthew G. Knepley         ierr = PetscOptionsReal("-dm_plex_sphere_radius", "Radius of the sphere", "", R,  &R, &flg);CHKERRQ(ierr);
26069318fe57SMatthew G. Knepley         ierr = DMPlexCreateSphereMesh_Internal(dm, dim, simplex, R);CHKERRQ(ierr);
26079318fe57SMatthew G. Knepley       }
26089318fe57SMatthew G. Knepley       break;
26099318fe57SMatthew G. Knepley       case DM_SHAPE_BALL:
26109318fe57SMatthew G. Knepley       {
26119318fe57SMatthew G. Knepley         PetscReal R = 1.0;
26129318fe57SMatthew G. Knepley 
26139318fe57SMatthew G. Knepley         ierr = PetscOptionsReal("-dm_plex_ball_radius", "Radius of the ball", "", R,  &R, &flg);CHKERRQ(ierr);
26149318fe57SMatthew G. Knepley         ierr = DMPlexCreateBallMesh_Internal(dm, dim, R);CHKERRQ(ierr);
26159318fe57SMatthew G. Knepley       }
26169318fe57SMatthew G. Knepley       break;
26179318fe57SMatthew G. Knepley       case DM_SHAPE_CYLINDER:
26189318fe57SMatthew G. Knepley       {
26199318fe57SMatthew G. Knepley         DMBoundaryType bdt = DM_BOUNDARY_NONE;
26209318fe57SMatthew G. Knepley         PetscInt       Nw  = 6;
26219318fe57SMatthew G. Knepley 
26229318fe57SMatthew G. Knepley         ierr = PetscOptionsEnum("-dm_plex_cylinder_bd", "Boundary type in the z direction", "", DMBoundaryTypes, (PetscEnum) bdt, (PetscEnum *) &bdt, NULL);CHKERRQ(ierr);
262361a622f3SMatthew G. Knepley         ierr = PetscOptionsInt("-dm_plex_cylinder_num_wedges", "Number of wedges around the cylinder", "", Nw, &Nw, NULL);CHKERRQ(ierr);
26249318fe57SMatthew G. Knepley         switch (cell) {
262561a622f3SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
26269318fe57SMatthew G. Knepley             ierr = DMPlexCreateWedgeCylinderMesh_Internal(dm, Nw, interpolate);CHKERRQ(ierr);
26279318fe57SMatthew G. Knepley             break;
26289318fe57SMatthew G. Knepley           default:
26299318fe57SMatthew G. Knepley             ierr = DMPlexCreateHexCylinderMesh_Internal(dm, bdt);CHKERRQ(ierr);
26309318fe57SMatthew G. Knepley             break;
26319318fe57SMatthew G. Knepley         }
26329318fe57SMatthew G. Knepley       }
26339318fe57SMatthew G. Knepley       break;
26349318fe57SMatthew G. Knepley       default: SETERRQ1(comm, PETSC_ERR_SUP, "Domain shape %s is unsupported", DMPlexShapes[shape]);
26359318fe57SMatthew G. Knepley     }
26369318fe57SMatthew G. Knepley   }
26379318fe57SMatthew G. Knepley   ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE);CHKERRQ(ierr);
26380a6ba040SMatthew G. Knepley   PetscFunctionReturn(0);
26390a6ba040SMatthew G. Knepley }
26400a6ba040SMatthew G. Knepley 
264147920aaeSMatthew G. Knepley PetscErrorCode DMSetFromOptions_NonRefinement_Plex(PetscOptionItems *PetscOptionsObject, DM dm)
26420a6ba040SMatthew G. Knepley {
26430a6ba040SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2644c0f0dcc3SMatthew G. Knepley   PetscBool      flg;
26459318fe57SMatthew G. Knepley   char           bdLabel[PETSC_MAX_PATH_LEN];
26460a6ba040SMatthew G. Knepley   PetscErrorCode ierr;
26470a6ba040SMatthew G. Knepley 
26480a6ba040SMatthew G. Knepley   PetscFunctionBegin;
26490a6ba040SMatthew G. Knepley   /* Handle viewing */
26500c77aedcSMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMPlexMatSetClosure", PETSC_FALSE, &mesh->printSetValues, NULL);CHKERRQ(ierr);
26515a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-dm_plex_print_fem", "Debug output level all fem computations", "DMPlexSNESComputeResidualFEM", 0, &mesh->printFEM, NULL,0);CHKERRQ(ierr);
26520c77aedcSMatthew G. Knepley   ierr = PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMPlexSNESComputeResidualFEM", mesh->printTol, &mesh->printTol, NULL);CHKERRQ(ierr);
26535a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-dm_plex_print_l2", "Debug output level all L2 diff computations", "DMComputeL2Diff", 0, &mesh->printL2, NULL,0);CHKERRQ(ierr);
2654c0f0dcc3SMatthew G. Knepley   ierr = DMMonitorSetFromOptions(dm, "-dm_plex_monitor_throughput", "Monitor the simulation throughput", "DMPlexMonitorThroughput", DMPlexMonitorThroughput, NULL, &flg);CHKERRQ(ierr);
2655c0f0dcc3SMatthew G. Knepley   if (flg) {ierr = PetscLogDefaultBegin();CHKERRQ(ierr);}
26569318fe57SMatthew G. Knepley   /* Labeling */
26579318fe57SMatthew G. Knepley   ierr = PetscOptionsString("-dm_plex_boundary_label", "Label to mark the mesh boundary", "", bdLabel, bdLabel, sizeof(bdLabel), &flg);CHKERRQ(ierr);
26589318fe57SMatthew G. Knepley   if (flg) {ierr = DMPlexCreateBoundaryLabel_Private(dm, bdLabel);CHKERRQ(ierr);}
2659953fc75cSMatthew G. Knepley   /* Point Location */
26600c77aedcSMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_hash_location", "Use grid hashing for point location", "DMInterpolate", PETSC_FALSE, &mesh->useHashLocation, NULL);CHKERRQ(ierr);
26610848f4b5SMatthew G. Knepley   /* Partitioning and distribution */
266298ba2d7fSLawrence 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);
26632e62ab5aSMatthew G. Knepley   /* Generation and remeshing */
26640c77aedcSMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_remesh_bd", "Allow changes to the boundary on remeshing", "DMAdapt", PETSC_FALSE, &mesh->remeshBd, NULL);CHKERRQ(ierr);
2665b29cfa1cSToby Isaac   /* Projection behavior */
26665a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-dm_plex_max_projection_height", "Maxmimum mesh point height used to project locally", "DMPlexSetMaxProjectionHeight", 0, &mesh->maxProjectionHeight, NULL,0);CHKERRQ(ierr);
266764141f95SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_regular_refinement", "Use special nested projection algorithm for regular refinement", "DMPlexSetRegularRefinement", mesh->regularRefinement, &mesh->regularRefinement, NULL);CHKERRQ(ierr);
2668f12cf164SMatthew G. Knepley   /* Checking structure */
2669f12cf164SMatthew G. Knepley   {
2670e902f1eaSVaclav Hapla     PetscBool   flg = PETSC_FALSE, flg2 = PETSC_FALSE, all = PETSC_FALSE;
2671f12cf164SMatthew G. Knepley 
2672e902f1eaSVaclav Hapla     ierr = PetscOptionsBool("-dm_plex_check_all", "Perform all checks", NULL, PETSC_FALSE, &all, &flg2);CHKERRQ(ierr);
2673f12cf164SMatthew 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);
2674e902f1eaSVaclav Hapla     if (all || (flg && flg2)) {ierr = DMPlexCheckSymmetry(dm);CHKERRQ(ierr);}
267525c50c26SVaclav 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);
2676e902f1eaSVaclav Hapla     if (all || (flg && flg2)) {ierr = DMPlexCheckSkeleton(dm, 0);CHKERRQ(ierr);}
267725c50c26SVaclav 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);
2678e902f1eaSVaclav Hapla     if (all || (flg && flg2)) {ierr = DMPlexCheckFaces(dm, 0);CHKERRQ(ierr);}
2679f12cf164SMatthew G. Knepley     ierr = PetscOptionsBool("-dm_plex_check_geometry", "Check that cells have positive volume", "DMPlexCheckGeometry", PETSC_FALSE, &flg, &flg2);CHKERRQ(ierr);
2680e902f1eaSVaclav Hapla     if (all || (flg && flg2)) {ierr = DMPlexCheckGeometry(dm);CHKERRQ(ierr);}
268175ebff7aSVaclav Hapla     ierr = PetscOptionsBool("-dm_plex_check_pointsf", "Check some necessary conditions for PointSF", "DMPlexCheckPointSF", PETSC_FALSE, &flg, &flg2);CHKERRQ(ierr);
2682e902f1eaSVaclav Hapla     if (all || (flg && flg2)) {ierr = DMPlexCheckPointSF(dm);CHKERRQ(ierr);}
268375ebff7aSVaclav 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);
2684e902f1eaSVaclav Hapla     if (all || (flg && flg2)) {ierr = DMPlexCheckInterfaceCones(dm);CHKERRQ(ierr);}
2685412e9a14SMatthew G. Knepley     ierr = PetscOptionsBool("-dm_plex_check_cell_shape", "Check cell shape", "DMPlexCheckCellShape", PETSC_FALSE, &flg, &flg2);CHKERRQ(ierr);
2686412e9a14SMatthew G. Knepley     if (flg && flg2) {ierr = DMPlexCheckCellShape(dm, PETSC_TRUE, PETSC_DETERMINE);CHKERRQ(ierr);}
2687f12cf164SMatthew G. Knepley   }
26889318fe57SMatthew G. Knepley   {
26899318fe57SMatthew G. Knepley     PetscReal scale = 1.0;
26904f3833eaSMatthew G. Knepley 
26919318fe57SMatthew G. Knepley     ierr = PetscOptionsReal("-dm_plex_scale", "Scale factor for mesh coordinates", "DMPlexScale", scale, &scale, &flg);CHKERRQ(ierr);
26929318fe57SMatthew G. Knepley     if (flg) {
26939318fe57SMatthew G. Knepley       Vec coordinates, coordinatesLocal;
26949318fe57SMatthew G. Knepley 
26959318fe57SMatthew G. Knepley       ierr = DMGetCoordinates(dm, &coordinates);CHKERRQ(ierr);
26969318fe57SMatthew G. Knepley       ierr = DMGetCoordinatesLocal(dm, &coordinatesLocal);CHKERRQ(ierr);
26979318fe57SMatthew G. Knepley       ierr = VecScale(coordinates, scale);CHKERRQ(ierr);
26989318fe57SMatthew G. Knepley       ierr = VecScale(coordinatesLocal, scale);CHKERRQ(ierr);
26999318fe57SMatthew G. Knepley     }
27009318fe57SMatthew G. Knepley   }
27014f3833eaSMatthew G. Knepley   ierr = PetscPartitionerSetFromOptions(mesh->partitioner);CHKERRQ(ierr);
270268d4fef7SMatthew G. Knepley   PetscFunctionReturn(0);
270368d4fef7SMatthew G. Knepley }
270468d4fef7SMatthew G. Knepley 
270546fa42a0SMatthew G. Knepley static PetscErrorCode DMSetFromOptions_Plex(PetscOptionItems *PetscOptionsObject,DM dm)
270668d4fef7SMatthew G. Knepley {
2707bdf63967SMatthew G. Knepley   PetscFunctionList ordlist;
2708bdf63967SMatthew G. Knepley   char              oname[256];
2709d410b0cfSMatthew G. Knepley   PetscReal         volume = -1.0;
27109318fe57SMatthew G. Knepley   PetscInt          prerefine = 0, refine = 0, r, coarsen = 0, overlap = 0, extLayers = 0, dim;
2711d410b0cfSMatthew 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;
271268d4fef7SMatthew G. Knepley   PetscErrorCode ierr;
271368d4fef7SMatthew G. Knepley 
271468d4fef7SMatthew G. Knepley   PetscFunctionBegin;
2715064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
27161a1499c8SBarry Smith   ierr = PetscOptionsHead(PetscOptionsObject,"DMPlex Options");CHKERRQ(ierr);
27179318fe57SMatthew G. Knepley   /* Handle automatic creation */
27189318fe57SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
271961a622f3SMatthew G. Knepley   if (dim < 0) {ierr = DMPlexCreateFromOptions_Internal(PetscOptionsObject, &coordSpace, dm);CHKERRQ(ierr);created = PETSC_TRUE;}
2720d89e6e46SMatthew G. Knepley   /* Handle interpolation before distribution */
2721d89e6e46SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_interpolate_pre", "Flag to interpolate mesh before distribution", "", interpolate, &interpolate, &flg);CHKERRQ(ierr);
2722d89e6e46SMatthew G. Knepley   if (flg) {
2723d89e6e46SMatthew G. Knepley     DMPlexInterpolatedFlag interpolated;
2724d89e6e46SMatthew G. Knepley 
2725d89e6e46SMatthew G. Knepley     ierr = DMPlexIsInterpolated(dm, &interpolated);CHKERRQ(ierr);
2726d89e6e46SMatthew G. Knepley     if (interpolated == DMPLEX_INTERPOLATED_FULL && !interpolate) {
2727d89e6e46SMatthew G. Knepley       DM udm;
2728d89e6e46SMatthew G. Knepley 
2729d89e6e46SMatthew G. Knepley       ierr = DMPlexUninterpolate(dm, &udm);CHKERRQ(ierr);
2730d89e6e46SMatthew G. Knepley       ierr = DMPlexReplace_Static(dm, &udm);CHKERRQ(ierr);
2731d89e6e46SMatthew G. Knepley     } else if (interpolated != DMPLEX_INTERPOLATED_FULL && interpolate) {
2732d89e6e46SMatthew G. Knepley       DM idm;
2733d89e6e46SMatthew G. Knepley 
2734d89e6e46SMatthew G. Knepley       ierr = DMPlexInterpolate(dm, &idm);CHKERRQ(ierr);
2735d89e6e46SMatthew G. Knepley       ierr = DMPlexReplace_Static(dm, &idm);CHKERRQ(ierr);
2736d89e6e46SMatthew G. Knepley     }
2737d89e6e46SMatthew G. Knepley   }
27389b44eab4SMatthew G. Knepley   /* Handle DMPlex refinement before distribution */
2739c1cad2e7SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_refine_ignore_model", "Flag to ignore the geometry model when refining", "DMCreate", ignoreModel, &ignoreModel, &flg);CHKERRQ(ierr);
2740c1cad2e7SMatthew G. Knepley   if (flg) {((DM_Plex *) dm->data)->ignoreModel = ignoreModel;}
2741250712c9SMatthew G. Knepley   ierr = DMPlexGetRefinementUniform(dm, &uniformOrig);CHKERRQ(ierr);
27429318fe57SMatthew G. Knepley   ierr = PetscOptionsBoundedInt("-dm_refine_pre", "The number of refinements before distribution", "DMCreate", prerefine, &prerefine, NULL,0);CHKERRQ(ierr);
274361a622f3SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_refine_remap_pre", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL);CHKERRQ(ierr);
2744250712c9SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_refine_uniform_pre", "Flag for uniform refinement before distribution", "DMCreate", uniform, &uniform, &flg);CHKERRQ(ierr);
2745250712c9SMatthew G. Knepley   if (flg) {ierr = DMPlexSetRefinementUniform(dm, uniform);CHKERRQ(ierr);}
2746250712c9SMatthew G. Knepley   ierr = PetscOptionsReal("-dm_refine_volume_limit_pre", "The maximum cell volume after refinement before distribution", "DMCreate", volume, &volume, &flg);CHKERRQ(ierr);
27479318fe57SMatthew G. Knepley   if (flg) {
27489318fe57SMatthew G. Knepley     ierr = DMPlexSetRefinementUniform(dm, PETSC_FALSE);CHKERRQ(ierr);
27499318fe57SMatthew G. Knepley     ierr = DMPlexSetRefinementLimit(dm, volume);CHKERRQ(ierr);
27509318fe57SMatthew G. Knepley     prerefine = PetscMax(prerefine, 1);
27519318fe57SMatthew G. Knepley   }
27529b44eab4SMatthew G. Knepley   for (r = 0; r < prerefine; ++r) {
27539b44eab4SMatthew G. Knepley     DM             rdm;
27549b44eab4SMatthew G. Knepley     PetscPointFunc coordFunc = ((DM_Plex*) dm->data)->coordFunc;
27559b44eab4SMatthew G. Knepley 
27569b44eab4SMatthew G. Knepley     ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
27579b44eab4SMatthew G. Knepley     ierr = DMRefine(dm, PetscObjectComm((PetscObject) dm), &rdm);CHKERRQ(ierr);
27589318fe57SMatthew G. Knepley     ierr = DMPlexReplace_Static(dm, &rdm);CHKERRQ(ierr);
27599b44eab4SMatthew G. Knepley     ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
276061a622f3SMatthew G. Knepley     if (coordFunc && remap) {
27619b44eab4SMatthew G. Knepley       ierr = DMPlexRemapGeometry(dm, 0.0, coordFunc);CHKERRQ(ierr);
27629b44eab4SMatthew G. Knepley       ((DM_Plex*) dm->data)->coordFunc = coordFunc;
27639b44eab4SMatthew G. Knepley     }
27649b44eab4SMatthew G. Knepley   }
2765250712c9SMatthew G. Knepley   ierr = DMPlexSetRefinementUniform(dm, uniformOrig);CHKERRQ(ierr);
27669318fe57SMatthew G. Knepley   /* Handle DMPlex extrusion before distribution */
2767d410b0cfSMatthew G. Knepley   ierr = PetscOptionsBoundedInt("-dm_extrude", "The number of layers to extrude", "", extLayers, &extLayers, NULL, 0);CHKERRQ(ierr);
27689318fe57SMatthew G. Knepley   if (extLayers) {
27699318fe57SMatthew G. Knepley     DM edm;
27709318fe57SMatthew G. Knepley 
2771d410b0cfSMatthew G. Knepley     ierr = DMExtrude(dm, extLayers, &edm);CHKERRQ(ierr);
27729318fe57SMatthew G. Knepley     ierr = DMPlexReplace_Static(dm, &edm);CHKERRQ(ierr);
277348d16a33SMatthew G. Knepley     ((DM_Plex *) dm->data)->coordFunc = NULL;
2774d410b0cfSMatthew G. Knepley     ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
2775d410b0cfSMatthew G. Knepley     extLayers = 0;
27769318fe57SMatthew G. Knepley   }
2777bdf63967SMatthew G. Knepley   /* Handle DMPlex reordering before distribution */
2778bdf63967SMatthew G. Knepley   ierr = MatGetOrderingList(&ordlist);CHKERRQ(ierr);
2779bdf63967SMatthew G. Knepley   ierr = PetscOptionsFList("-dm_plex_reorder", "Set mesh reordering type", "DMPlexGetOrdering", ordlist, MATORDERINGNATURAL, oname, sizeof(oname), &flg);CHKERRQ(ierr);
2780bdf63967SMatthew G. Knepley   if (flg) {
2781bdf63967SMatthew G. Knepley     DM pdm;
2782bdf63967SMatthew G. Knepley     IS perm;
2783bdf63967SMatthew G. Knepley 
2784bdf63967SMatthew G. Knepley     ierr = DMPlexGetOrdering(dm, oname, NULL, &perm);CHKERRQ(ierr);
2785bdf63967SMatthew G. Knepley     ierr = DMPlexPermute(dm, perm, &pdm);CHKERRQ(ierr);
2786bdf63967SMatthew G. Knepley     ierr = ISDestroy(&perm);CHKERRQ(ierr);
2787bdf63967SMatthew G. Knepley     ierr = DMPlexReplace_Static(dm, &pdm);CHKERRQ(ierr);
2788bdf63967SMatthew G. Knepley     ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
2789bdf63967SMatthew G. Knepley   }
2790d410b0cfSMatthew G. Knepley   /* TODO Old-style extrusion which can be removed */
2791d410b0cfSMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_interpolate", "Flag to create edges and faces automatically", "", interpolate, &interpolate, NULL);CHKERRQ(ierr);
27929b44eab4SMatthew G. Knepley   /* Handle DMPlex distribution */
27939b44eab4SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_distribute", "Flag to redistribute a mesh among processes", "DMCreate", distribute, &distribute, NULL);CHKERRQ(ierr);
27949b44eab4SMatthew G. Knepley   ierr = PetscOptionsBoundedInt("-dm_distribute_overlap", "The size of the overlap halo", "DMCreate", overlap, &overlap, NULL, 0);CHKERRQ(ierr);
27959b44eab4SMatthew G. Knepley   if (distribute) {
27969b44eab4SMatthew G. Knepley     DM               pdm = NULL;
27979b44eab4SMatthew G. Knepley     PetscPartitioner part;
27989b44eab4SMatthew G. Knepley 
27999b44eab4SMatthew G. Knepley     ierr = DMPlexGetPartitioner(dm, &part);CHKERRQ(ierr);
28009b44eab4SMatthew G. Knepley     ierr = PetscPartitionerSetFromOptions(part);CHKERRQ(ierr);
28019b44eab4SMatthew G. Knepley     ierr = DMPlexDistribute(dm, overlap, NULL, &pdm);CHKERRQ(ierr);
28029b44eab4SMatthew G. Knepley     if (pdm) {
28039318fe57SMatthew G. Knepley       ierr = DMPlexReplace_Static(dm, &pdm);CHKERRQ(ierr);
28049b44eab4SMatthew G. Knepley     }
28059b44eab4SMatthew G. Knepley   }
28069318fe57SMatthew G. Knepley   /* Create coordinate space */
28079318fe57SMatthew G. Knepley   if (created) {
280861a622f3SMatthew G. Knepley     DM_Plex  *mesh = (DM_Plex *) dm->data;
28099318fe57SMatthew G. Knepley     PetscInt  degree = 1;
281061a622f3SMatthew G. Knepley     PetscBool periodic, flg;
28119318fe57SMatthew G. Knepley 
281261a622f3SMatthew G. Knepley     ierr = PetscOptionsBool("-dm_coord_space", "Use an FEM space for coordinates", "", coordSpace, &coordSpace, &flg);CHKERRQ(ierr);
28139318fe57SMatthew G. Knepley     ierr = PetscOptionsInt("-dm_coord_petscspace_degree", "FEM degree for coordinate space", "", degree, &degree, NULL);CHKERRQ(ierr);
281461a622f3SMatthew G. Knepley     if (coordSpace) {ierr = DMPlexCreateCoordinateSpace(dm, degree, mesh->coordFunc);CHKERRQ(ierr);}
281561a622f3SMatthew G. Knepley     if (flg && !coordSpace) {
281661a622f3SMatthew G. Knepley       DM           cdm;
281761a622f3SMatthew G. Knepley       PetscDS      cds;
281861a622f3SMatthew G. Knepley       PetscObject  obj;
281961a622f3SMatthew G. Knepley       PetscClassId id;
282061a622f3SMatthew G. Knepley 
282161a622f3SMatthew G. Knepley       ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
282261a622f3SMatthew G. Knepley       ierr = DMGetDS(cdm, &cds);CHKERRQ(ierr);
282361a622f3SMatthew G. Knepley       ierr = PetscDSGetDiscretization(cds, 0, &obj);CHKERRQ(ierr);
282461a622f3SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
282561a622f3SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
282661a622f3SMatthew G. Knepley         PetscContainer dummy;
282761a622f3SMatthew G. Knepley 
282861a622f3SMatthew G. Knepley         ierr = PetscContainerCreate(PETSC_COMM_SELF, &dummy);CHKERRQ(ierr);
282961a622f3SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) dummy, "coordinates");CHKERRQ(ierr);
283061a622f3SMatthew G. Knepley         ierr = DMSetField(cdm, 0, NULL, (PetscObject) dummy);CHKERRQ(ierr);
283161a622f3SMatthew G. Knepley         ierr = PetscContainerDestroy(&dummy);CHKERRQ(ierr);
283261a622f3SMatthew G. Knepley         ierr = DMClearDS(cdm);CHKERRQ(ierr);
283361a622f3SMatthew G. Knepley       }
283461a622f3SMatthew G. Knepley       mesh->coordFunc = NULL;
283561a622f3SMatthew G. Knepley     }
28369318fe57SMatthew G. Knepley     ierr = DMLocalizeCoordinates(dm);CHKERRQ(ierr);
283761a622f3SMatthew G. Knepley     ierr = DMGetPeriodicity(dm, &periodic, NULL, NULL, NULL);CHKERRQ(ierr);
283861a622f3SMatthew G. Knepley     if (periodic) {ierr = DMSetPeriodicity(dm, PETSC_TRUE, NULL, NULL, NULL);CHKERRQ(ierr);}
28399318fe57SMatthew G. Knepley   }
284068d4fef7SMatthew G. Knepley   /* Handle DMPlex refinement */
284161a622f3SMatthew G. Knepley   remap = PETSC_TRUE;
28425a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-dm_refine", "The number of uniform refinements", "DMCreate", refine, &refine, NULL,0);CHKERRQ(ierr);
284361a622f3SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_refine_remap", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL);CHKERRQ(ierr);
28445a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-dm_refine_hierarchy", "The number of uniform refinements", "DMCreate", refine, &refine, &isHierarchy,0);CHKERRQ(ierr);
2845b6a0289aSMatthew G. Knepley   if (refine) {ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE);CHKERRQ(ierr);}
284668d4fef7SMatthew G. Knepley   if (refine && isHierarchy) {
2847acdc6f61SToby Isaac     DM *dms, coarseDM;
284868d4fef7SMatthew G. Knepley 
2849acdc6f61SToby Isaac     ierr = DMGetCoarseDM(dm, &coarseDM);CHKERRQ(ierr);
2850acdc6f61SToby Isaac     ierr = PetscObjectReference((PetscObject)coarseDM);CHKERRQ(ierr);
285168d4fef7SMatthew G. Knepley     ierr = PetscMalloc1(refine,&dms);CHKERRQ(ierr);
285268d4fef7SMatthew G. Knepley     ierr = DMRefineHierarchy(dm, refine, dms);CHKERRQ(ierr);
285368d4fef7SMatthew G. Knepley     /* Total hack since we do not pass in a pointer */
285468d4fef7SMatthew G. Knepley     ierr = DMPlexSwap_Static(dm, dms[refine-1]);CHKERRQ(ierr);
285568d4fef7SMatthew G. Knepley     if (refine == 1) {
2856a8fb8f29SToby Isaac       ierr = DMSetCoarseDM(dm, dms[0]);CHKERRQ(ierr);
28570aef6b92SMatthew G. Knepley       ierr = DMPlexSetRegularRefinement(dm, PETSC_TRUE);CHKERRQ(ierr);
285868d4fef7SMatthew G. Knepley     } else {
2859a8fb8f29SToby Isaac       ierr = DMSetCoarseDM(dm, dms[refine-2]);CHKERRQ(ierr);
28600aef6b92SMatthew G. Knepley       ierr = DMPlexSetRegularRefinement(dm, PETSC_TRUE);CHKERRQ(ierr);
2861a8fb8f29SToby Isaac       ierr = DMSetCoarseDM(dms[0], dms[refine-1]);CHKERRQ(ierr);
28620aef6b92SMatthew G. Knepley       ierr = DMPlexSetRegularRefinement(dms[0], PETSC_TRUE);CHKERRQ(ierr);
286368d4fef7SMatthew G. Knepley     }
2864acdc6f61SToby Isaac     ierr = DMSetCoarseDM(dms[refine-1], coarseDM);CHKERRQ(ierr);
2865acdc6f61SToby Isaac     ierr = PetscObjectDereference((PetscObject)coarseDM);CHKERRQ(ierr);
286668d4fef7SMatthew G. Knepley     /* Free DMs */
286768d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
2868547f7119SMatthew G. Knepley       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]);CHKERRQ(ierr);
286968d4fef7SMatthew G. Knepley       ierr = DMDestroy(&dms[r]);CHKERRQ(ierr);
287068d4fef7SMatthew G. Knepley     }
287168d4fef7SMatthew G. Knepley     ierr = PetscFree(dms);CHKERRQ(ierr);
287268d4fef7SMatthew G. Knepley   } else {
287368d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
28749318fe57SMatthew G. Knepley       DM             rdm;
287551a74b61SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex*) dm->data)->coordFunc;
287668d4fef7SMatthew G. Knepley 
28771a1499c8SBarry Smith       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
28789318fe57SMatthew G. Knepley       ierr = DMRefine(dm, PetscObjectComm((PetscObject) dm), &rdm);CHKERRQ(ierr);
287968d4fef7SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
28809318fe57SMatthew G. Knepley       ierr = DMPlexReplace_Static(dm, &rdm);CHKERRQ(ierr);
2881547f7119SMatthew G. Knepley       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
288261a622f3SMatthew G. Knepley       if (coordFunc && remap) {
288351a74b61SMatthew G. Knepley         ierr = DMPlexRemapGeometry(dm, 0.0, coordFunc);CHKERRQ(ierr);
288451a74b61SMatthew G. Knepley         ((DM_Plex*) dm->data)->coordFunc = coordFunc;
288551a74b61SMatthew G. Knepley       }
288668d4fef7SMatthew G. Knepley     }
288768d4fef7SMatthew G. Knepley   }
28883cf6fe12SMatthew G. Knepley   /* Handle DMPlex coarsening */
28895a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-dm_coarsen", "Coarsen the mesh", "DMCreate", coarsen, &coarsen, NULL,0);CHKERRQ(ierr);
28905a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-dm_coarsen_hierarchy", "The number of coarsenings", "DMCreate", coarsen, &coarsen, &isHierarchy,0);CHKERRQ(ierr);
2891b653a561SMatthew G. Knepley   if (coarsen && isHierarchy) {
2892b653a561SMatthew G. Knepley     DM *dms;
2893b653a561SMatthew G. Knepley 
2894b653a561SMatthew G. Knepley     ierr = PetscMalloc1(coarsen, &dms);CHKERRQ(ierr);
2895b653a561SMatthew G. Knepley     ierr = DMCoarsenHierarchy(dm, coarsen, dms);CHKERRQ(ierr);
2896b653a561SMatthew G. Knepley     /* Free DMs */
2897b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
2898547f7119SMatthew G. Knepley       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]);CHKERRQ(ierr);
2899b653a561SMatthew G. Knepley       ierr = DMDestroy(&dms[r]);CHKERRQ(ierr);
2900b653a561SMatthew G. Knepley     }
2901b653a561SMatthew G. Knepley     ierr = PetscFree(dms);CHKERRQ(ierr);
2902b653a561SMatthew G. Knepley   } else {
2903b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
29049318fe57SMatthew G. Knepley       DM             cdm;
29059318fe57SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex*) dm->data)->coordFunc;
29063cf6fe12SMatthew G. Knepley 
29073cf6fe12SMatthew G. Knepley       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
29089318fe57SMatthew G. Knepley       ierr = DMCoarsen(dm, PetscObjectComm((PetscObject) dm), &cdm);CHKERRQ(ierr);
29093cf6fe12SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
29109318fe57SMatthew G. Knepley       ierr = DMPlexReplace_Static(dm, &cdm);CHKERRQ(ierr);
2911547f7119SMatthew G. Knepley       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
29129318fe57SMatthew G. Knepley       if (coordFunc) {
29139318fe57SMatthew G. Knepley         ierr = DMPlexRemapGeometry(dm, 0.0, coordFunc);CHKERRQ(ierr);
29149318fe57SMatthew G. Knepley         ((DM_Plex*) dm->data)->coordFunc = coordFunc;
29159318fe57SMatthew G. Knepley       }
29163cf6fe12SMatthew G. Knepley     }
2917b653a561SMatthew G. Knepley   }
2918909dfd52SMatthew G. Knepley   /* Handle ghost cells */
2919909dfd52SMatthew 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);
2920909dfd52SMatthew G. Knepley   if (ghostCells) {
2921909dfd52SMatthew G. Knepley     DM   gdm;
2922909dfd52SMatthew G. Knepley     char lname[PETSC_MAX_PATH_LEN];
2923909dfd52SMatthew G. Knepley 
2924909dfd52SMatthew G. Knepley     lname[0] = '\0';
2925909dfd52SMatthew G. Knepley     ierr = PetscOptionsString("-dm_plex_fv_ghost_cells_label", "Label name for ghost cells boundary", "DMCreate", lname, lname, sizeof(lname), &flg);CHKERRQ(ierr);
2926909dfd52SMatthew G. Knepley     ierr = DMPlexConstructGhostCells(dm, flg ? lname : NULL, NULL, &gdm);CHKERRQ(ierr);
2927909dfd52SMatthew G. Knepley     ierr = DMPlexReplace_Static(dm, &gdm);CHKERRQ(ierr);
2928909dfd52SMatthew G. Knepley   }
29293cf6fe12SMatthew G. Knepley   /* Handle */
29301a1499c8SBarry Smith   ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
29310a6ba040SMatthew G. Knepley   ierr = PetscOptionsTail();CHKERRQ(ierr);
29320a6ba040SMatthew G. Knepley   PetscFunctionReturn(0);
29330a6ba040SMatthew G. Knepley }
29340a6ba040SMatthew G. Knepley 
2935552f7358SJed Brown static PetscErrorCode DMCreateGlobalVector_Plex(DM dm,Vec *vec)
2936552f7358SJed Brown {
2937552f7358SJed Brown   PetscErrorCode ierr;
2938552f7358SJed Brown 
2939552f7358SJed Brown   PetscFunctionBegin;
2940552f7358SJed Brown   ierr = DMCreateGlobalVector_Section_Private(dm,vec);CHKERRQ(ierr);
2941552f7358SJed Brown   /* ierr = VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM);CHKERRQ(ierr); */
2942552f7358SJed Brown   ierr = VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex);CHKERRQ(ierr);
2943d930f514SMatthew G. Knepley   ierr = VecSetOperation(*vec, VECOP_VIEWNATIVE, (void (*)(void)) VecView_Plex_Native);CHKERRQ(ierr);
29442c40f234SMatthew G. Knepley   ierr = VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex);CHKERRQ(ierr);
2945d930f514SMatthew G. Knepley   ierr = VecSetOperation(*vec, VECOP_LOADNATIVE, (void (*)(void)) VecLoad_Plex_Native);CHKERRQ(ierr);
2946552f7358SJed Brown   PetscFunctionReturn(0);
2947552f7358SJed Brown }
2948552f7358SJed Brown 
2949552f7358SJed Brown static PetscErrorCode DMCreateLocalVector_Plex(DM dm,Vec *vec)
2950552f7358SJed Brown {
2951552f7358SJed Brown   PetscErrorCode ierr;
2952552f7358SJed Brown 
2953552f7358SJed Brown   PetscFunctionBegin;
2954552f7358SJed Brown   ierr = DMCreateLocalVector_Section_Private(dm,vec);CHKERRQ(ierr);
2955552f7358SJed Brown   ierr = VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex_Local);CHKERRQ(ierr);
29562c40f234SMatthew G. Knepley   ierr = VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex_Local);CHKERRQ(ierr);
2957552f7358SJed Brown   PetscFunctionReturn(0);
2958552f7358SJed Brown }
2959552f7358SJed Brown 
2960793f3fe5SMatthew G. Knepley static PetscErrorCode DMGetDimPoints_Plex(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
2961793f3fe5SMatthew G. Knepley {
2962793f3fe5SMatthew G. Knepley   PetscInt       depth, d;
2963793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
2964793f3fe5SMatthew G. Knepley 
2965793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
2966793f3fe5SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2967793f3fe5SMatthew G. Knepley   if (depth == 1) {
2968793f3fe5SMatthew G. Knepley     ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
2969793f3fe5SMatthew G. Knepley     if (dim == 0)      {ierr = DMPlexGetDepthStratum(dm, dim, pStart, pEnd);CHKERRQ(ierr);}
2970793f3fe5SMatthew G. Knepley     else if (dim == d) {ierr = DMPlexGetDepthStratum(dm, 1, pStart, pEnd);CHKERRQ(ierr);}
2971793f3fe5SMatthew G. Knepley     else               {*pStart = 0; *pEnd = 0;}
2972793f3fe5SMatthew G. Knepley   } else {
2973793f3fe5SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, dim, pStart, pEnd);CHKERRQ(ierr);
2974793f3fe5SMatthew G. Knepley   }
2975793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
2976793f3fe5SMatthew G. Knepley }
2977793f3fe5SMatthew G. Knepley 
297828d58a37SPierre Jolivet static PetscErrorCode DMGetNeighbors_Plex(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
2979502a2867SDave May {
2980502a2867SDave May   PetscSF           sf;
29810a19bb7dSprj-   PetscInt          niranks, njranks, n;
29820a19bb7dSprj-   const PetscMPIInt *iranks, *jranks;
29830a19bb7dSprj-   DM_Plex           *data = (DM_Plex*) dm->data;
29842f356facSMatthew G. Knepley   PetscErrorCode    ierr;
2985502a2867SDave May 
29862f356facSMatthew G. Knepley   PetscFunctionBegin;
2987502a2867SDave May   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
29880a19bb7dSprj-   if (!data->neighbors) {
298907a779feSPierre Jolivet     ierr = PetscSFSetUp(sf);CHKERRQ(ierr);
29900a19bb7dSprj-     ierr = PetscSFGetRootRanks(sf, &njranks, &jranks, NULL, NULL, NULL);CHKERRQ(ierr);
29910a19bb7dSprj-     ierr = PetscSFGetLeafRanks(sf, &niranks, &iranks, NULL, NULL);CHKERRQ(ierr);
29920a19bb7dSprj-     ierr = PetscMalloc1(njranks + niranks + 1, &data->neighbors);CHKERRQ(ierr);
29930a19bb7dSprj-     ierr = PetscArraycpy(data->neighbors + 1, jranks, njranks);CHKERRQ(ierr);
29940a19bb7dSprj-     ierr = PetscArraycpy(data->neighbors + njranks + 1, iranks, niranks);CHKERRQ(ierr);
29950a19bb7dSprj-     n = njranks + niranks;
29960a19bb7dSprj-     ierr = PetscSortRemoveDupsMPIInt(&n, data->neighbors + 1);CHKERRQ(ierr);
29970a19bb7dSprj-     /* The following cast should never fail: can't have more neighbors than PETSC_MPI_INT_MAX */
29980a19bb7dSprj-     ierr = PetscMPIIntCast(n, data->neighbors);CHKERRQ(ierr);
29990a19bb7dSprj-   }
30000a19bb7dSprj-   if (nranks) *nranks = data->neighbors[0];
30010a19bb7dSprj-   if (ranks) {
30020a19bb7dSprj-     if (data->neighbors[0]) *ranks = data->neighbors + 1;
30030a19bb7dSprj-     else                    *ranks = NULL;
30040a19bb7dSprj-   }
3005502a2867SDave May   PetscFunctionReturn(0);
3006502a2867SDave May }
3007502a2867SDave May 
30081eb70e55SToby Isaac PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM, DM, Mat, Vec, Vec);
30091eb70e55SToby Isaac 
301046fa42a0SMatthew G. Knepley static PetscErrorCode DMInitialize_Plex(DM dm)
3011552f7358SJed Brown {
3012713918a9SToby Isaac   PetscErrorCode ierr;
3013713918a9SToby Isaac 
3014552f7358SJed Brown   PetscFunctionBegin;
3015552f7358SJed Brown   dm->ops->view                            = DMView_Plex;
30162c40f234SMatthew G. Knepley   dm->ops->load                            = DMLoad_Plex;
3017552f7358SJed Brown   dm->ops->setfromoptions                  = DMSetFromOptions_Plex;
301838221697SMatthew G. Knepley   dm->ops->clone                           = DMClone_Plex;
3019552f7358SJed Brown   dm->ops->setup                           = DMSetUp_Plex;
30201bb6d2a8SBarry Smith   dm->ops->createlocalsection              = DMCreateLocalSection_Plex;
302166ad2231SToby Isaac   dm->ops->createdefaultconstraints        = DMCreateDefaultConstraints_Plex;
3022552f7358SJed Brown   dm->ops->createglobalvector              = DMCreateGlobalVector_Plex;
3023552f7358SJed Brown   dm->ops->createlocalvector               = DMCreateLocalVector_Plex;
3024184d77edSJed Brown   dm->ops->getlocaltoglobalmapping         = NULL;
30250298fd71SBarry Smith   dm->ops->createfieldis                   = NULL;
3026552f7358SJed Brown   dm->ops->createcoordinatedm              = DMCreateCoordinateDM_Plex;
3027f19dbd58SToby Isaac   dm->ops->createcoordinatefield           = DMCreateCoordinateField_Plex;
30280a6ba040SMatthew G. Knepley   dm->ops->getcoloring                     = NULL;
3029552f7358SJed Brown   dm->ops->creatematrix                    = DMCreateMatrix_Plex;
3030bceba477SMatthew G. Knepley   dm->ops->createinterpolation             = DMCreateInterpolation_Plex;
3031bd041c0cSMatthew G. Knepley   dm->ops->createmassmatrix                = DMCreateMassMatrix_Plex;
30325a84ad33SLisandro Dalcin   dm->ops->createinjection                 = DMCreateInjection_Plex;
3033552f7358SJed Brown   dm->ops->refine                          = DMRefine_Plex;
30340a6ba040SMatthew G. Knepley   dm->ops->coarsen                         = DMCoarsen_Plex;
30350a6ba040SMatthew G. Knepley   dm->ops->refinehierarchy                 = DMRefineHierarchy_Plex;
3036b653a561SMatthew G. Knepley   dm->ops->coarsenhierarchy                = DMCoarsenHierarchy_Plex;
3037d410b0cfSMatthew G. Knepley   dm->ops->extrude                         = DMExtrude_Plex;
30380298fd71SBarry Smith   dm->ops->globaltolocalbegin              = NULL;
30390298fd71SBarry Smith   dm->ops->globaltolocalend                = NULL;
30400298fd71SBarry Smith   dm->ops->localtoglobalbegin              = NULL;
30410298fd71SBarry Smith   dm->ops->localtoglobalend                = NULL;
3042552f7358SJed Brown   dm->ops->destroy                         = DMDestroy_Plex;
3043552f7358SJed Brown   dm->ops->createsubdm                     = DMCreateSubDM_Plex;
30442adcc780SMatthew G. Knepley   dm->ops->createsuperdm                   = DMCreateSuperDM_Plex;
3045793f3fe5SMatthew G. Knepley   dm->ops->getdimpoints                    = DMGetDimPoints_Plex;
3046552f7358SJed Brown   dm->ops->locatepoints                    = DMLocatePoints_Plex;
30470709b2feSToby Isaac   dm->ops->projectfunctionlocal            = DMProjectFunctionLocal_Plex;
30480709b2feSToby Isaac   dm->ops->projectfunctionlabellocal       = DMProjectFunctionLabelLocal_Plex;
3049bfc4295aSToby Isaac   dm->ops->projectfieldlocal               = DMProjectFieldLocal_Plex;
30508c6c5593SMatthew G. Knepley   dm->ops->projectfieldlabellocal          = DMProjectFieldLabelLocal_Plex;
3051ece3a9fcSMatthew G. Knepley   dm->ops->projectbdfieldlabellocal        = DMProjectBdFieldLabelLocal_Plex;
30520709b2feSToby Isaac   dm->ops->computel2diff                   = DMComputeL2Diff_Plex;
3053b698f381SToby Isaac   dm->ops->computel2gradientdiff           = DMComputeL2GradientDiff_Plex;
30542a16baeaSToby Isaac   dm->ops->computel2fielddiff              = DMComputeL2FieldDiff_Plex;
305528d58a37SPierre Jolivet   dm->ops->getneighbors                    = DMGetNeighbors_Plex;
3056f1d73a7aSMatthew G. Knepley   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C",DMPlexInsertBoundaryValues_Plex);CHKERRQ(ierr);
305756cf3b9cSMatthew G. Knepley   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertTimeDerviativeBoundaryValues_C",DMPlexInsertTimeDerivativeBoundaryValues_Plex);CHKERRQ(ierr);
30588135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",DMSetUpGLVisViewer_Plex);CHKERRQ(ierr);
305928d58a37SPierre Jolivet   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMCreateNeumannOverlap_C",DMCreateNeumannOverlap_Plex);CHKERRQ(ierr);
3060cb54e036SVaclav Hapla   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexGetOverlap_C",DMPlexGetOverlap_Plex);CHKERRQ(ierr);
30611eb70e55SToby Isaac   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMInterpolateSolution_C",DMInterpolateSolution_Plex);CHKERRQ(ierr);
3062552f7358SJed Brown   PetscFunctionReturn(0);
3063552f7358SJed Brown }
3064552f7358SJed Brown 
306546fa42a0SMatthew G. Knepley PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm)
306663a16f15SMatthew G. Knepley {
306763a16f15SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex *) dm->data;
306863a16f15SMatthew G. Knepley   PetscErrorCode ierr;
306963a16f15SMatthew G. Knepley 
307063a16f15SMatthew G. Knepley   PetscFunctionBegin;
307163a16f15SMatthew G. Knepley   mesh->refct++;
307263a16f15SMatthew G. Knepley   (*newdm)->data = mesh;
307363a16f15SMatthew G. Knepley   ierr = PetscObjectChangeTypeName((PetscObject) *newdm, DMPLEX);CHKERRQ(ierr);
307463a16f15SMatthew G. Knepley   ierr = DMInitialize_Plex(*newdm);CHKERRQ(ierr);
307563a16f15SMatthew G. Knepley   PetscFunctionReturn(0);
307663a16f15SMatthew G. Knepley }
307763a16f15SMatthew G. Knepley 
30788818961aSMatthew G Knepley /*MC
30798818961aSMatthew G Knepley   DMPLEX = "plex" - A DM object that encapsulates an unstructured mesh, or CW Complex, which can be expressed using a Hasse Diagram.
30808818961aSMatthew G Knepley                     In the local representation, Vecs contain all unknowns in the interior and shared boundary. This is
30818818961aSMatthew G Knepley                     specified by a PetscSection object. Ownership in the global representation is determined by
30828818961aSMatthew G Knepley                     ownership of the underlying DMPlex points. This is specified by another PetscSection object.
30838818961aSMatthew G Knepley 
3084e5893cccSMatthew G. Knepley   Options Database Keys:
3085250712c9SMatthew G. Knepley + -dm_refine_pre                     - Refine mesh before distribution
3086250712c9SMatthew G. Knepley + -dm_refine_uniform_pre             - Choose uniform or generator-based refinement
3087250712c9SMatthew G. Knepley + -dm_refine_volume_limit_pre        - Cell volume limit after pre-refinement using generator
3088250712c9SMatthew G. Knepley . -dm_distribute                     - Distribute mesh across processes
3089250712c9SMatthew G. Knepley . -dm_distribute_overlap             - Number of cells to overlap for distribution
3090250712c9SMatthew G. Knepley . -dm_refine                         - Refine mesh after distribution
3091250712c9SMatthew G. Knepley . -dm_plex_hash_location             - Use grid hashing for point location
3092ddce0771SMatthew G. Knepley . -dm_plex_hash_box_faces <n,m,p>    - The number of divisions in each direction of the grid hash
3093f12cf164SMatthew G. Knepley . -dm_plex_partition_balance         - Attempt to evenly divide points on partition boundary between processes
3094f12cf164SMatthew G. Knepley . -dm_plex_remesh_bd                 - Allow changes to the boundary on remeshing
3095f12cf164SMatthew G. Knepley . -dm_plex_max_projection_height     - Maxmimum mesh point height used to project locally
3096f12cf164SMatthew G. Knepley . -dm_plex_regular_refinement        - Use special nested projection algorithm for regular refinement
3097250712c9SMatthew G. Knepley . -dm_plex_check_all                 - Perform all shecks below
3098f12cf164SMatthew G. Knepley . -dm_plex_check_symmetry            - Check that the adjacency information in the mesh is symmetric
3099f12cf164SMatthew G. Knepley . -dm_plex_check_skeleton <celltype> - Check that each cell has the correct number of vertices
3100f12cf164SMatthew 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
3101f12cf164SMatthew G. Knepley . -dm_plex_check_geometry            - Check that cells have positive volume
3102f12cf164SMatthew G. Knepley . -dm_view :mesh.tex:ascii_latex     - View the mesh in LaTeX/TikZ
3103e5893cccSMatthew G. Knepley . -dm_plex_view_scale <num>          - Scale the TikZ
3104e5893cccSMatthew G. Knepley - -dm_plex_print_fem <num>           - View FEM assembly information, such as element vectors and matrices
3105e5893cccSMatthew G. Knepley 
31068818961aSMatthew G Knepley   Level: intermediate
31078818961aSMatthew G Knepley 
31088818961aSMatthew G Knepley .seealso: DMType, DMPlexCreate(), DMCreate(), DMSetType()
31098818961aSMatthew G Knepley M*/
31108818961aSMatthew G Knepley 
31118cc058d9SJed Brown PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm)
3112552f7358SJed Brown {
3113552f7358SJed Brown   DM_Plex       *mesh;
3114412e9a14SMatthew G. Knepley   PetscInt       unit;
3115552f7358SJed Brown   PetscErrorCode ierr;
3116552f7358SJed Brown 
3117552f7358SJed Brown   PetscFunctionBegin;
3118552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3119b00a9115SJed Brown   ierr     = PetscNewLog(dm,&mesh);CHKERRQ(ierr);
3120552f7358SJed Brown   dm->data = mesh;
3121552f7358SJed Brown 
3122552f7358SJed Brown   mesh->refct             = 1;
312382f516ccSBarry Smith   ierr                    = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection);CHKERRQ(ierr);
3124552f7358SJed Brown   mesh->maxConeSize       = 0;
31250298fd71SBarry Smith   mesh->cones             = NULL;
31260298fd71SBarry Smith   mesh->coneOrientations  = NULL;
312782f516ccSBarry Smith   ierr                    = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection);CHKERRQ(ierr);
3128552f7358SJed Brown   mesh->maxSupportSize    = 0;
31290298fd71SBarry Smith   mesh->supports          = NULL;
3130552f7358SJed Brown   mesh->refinementUniform = PETSC_TRUE;
3131552f7358SJed Brown   mesh->refinementLimit   = -1.0;
31327d0f5628SVaclav Hapla   mesh->interpolated      = DMPLEX_INTERPOLATED_INVALID;
31337d0f5628SVaclav Hapla   mesh->interpolatedCollective = DMPLEX_INTERPOLATED_INVALID;
3134552f7358SJed Brown 
31350298fd71SBarry Smith   mesh->facesTmp = NULL;
3136552f7358SJed Brown 
3137d9deefdfSMatthew G. Knepley   mesh->tetgenOpts   = NULL;
3138d9deefdfSMatthew G. Knepley   mesh->triangleOpts = NULL;
313977623264SMatthew G. Knepley   ierr = PetscPartitionerCreate(PetscObjectComm((PetscObject)dm), &mesh->partitioner);CHKERRQ(ierr);
31402e62ab5aSMatthew G. Knepley   mesh->remeshBd     = PETSC_FALSE;
3141d9deefdfSMatthew G. Knepley 
31420298fd71SBarry Smith   mesh->subpointMap = NULL;
3143552f7358SJed Brown 
31448865f1eaSKarl Rupp   for (unit = 0; unit < NUM_PETSC_UNITS; ++unit) mesh->scale[unit] = 1.0;
3145552f7358SJed Brown 
31460aef6b92SMatthew G. Knepley   mesh->regularRefinement   = PETSC_FALSE;
3147df0420ecSMatthew G. Knepley   mesh->depthState          = -1;
3148ba2698f1SMatthew G. Knepley   mesh->celltypeState       = -1;
31490298fd71SBarry Smith   mesh->globalVertexNumbers = NULL;
31500298fd71SBarry Smith   mesh->globalCellNumbers   = NULL;
3151a68b90caSToby Isaac   mesh->anchorSection       = NULL;
3152a68b90caSToby Isaac   mesh->anchorIS            = NULL;
315341e6d900SToby Isaac   mesh->createanchors       = NULL;
3154fa73a4e1SToby Isaac   mesh->computeanchormatrix = NULL;
3155d961a43aSToby Isaac   mesh->parentSection       = NULL;
3156d961a43aSToby Isaac   mesh->parents             = NULL;
3157d961a43aSToby Isaac   mesh->childIDs            = NULL;
3158d961a43aSToby Isaac   mesh->childSection        = NULL;
3159d961a43aSToby Isaac   mesh->children            = NULL;
3160d6a7ad0dSToby Isaac   mesh->referenceTree       = NULL;
3161dcbd3bf7SToby Isaac   mesh->getchildsymmetry    = NULL;
3162552f7358SJed Brown   mesh->vtkCellHeight       = 0;
3163e228b242SToby Isaac   mesh->useAnchors          = PETSC_FALSE;
3164552f7358SJed Brown 
3165b29cfa1cSToby Isaac   mesh->maxProjectionHeight = 0;
3166b29cfa1cSToby Isaac 
31670a19bb7dSprj-   mesh->neighbors           = NULL;
31680a19bb7dSprj- 
3169552f7358SJed Brown   mesh->printSetValues = PETSC_FALSE;
3170552f7358SJed Brown   mesh->printFEM       = 0;
31716113b454SMatthew G. Knepley   mesh->printTol       = 1.0e-10;
3172552f7358SJed Brown 
3173552f7358SJed Brown   ierr = DMInitialize_Plex(dm);CHKERRQ(ierr);
3174552f7358SJed Brown   PetscFunctionReturn(0);
3175552f7358SJed Brown }
3176552f7358SJed Brown 
3177552f7358SJed Brown /*@
3178552f7358SJed Brown   DMPlexCreate - Creates a DMPlex object, which encapsulates an unstructured mesh, or CW complex, which can be expressed using a Hasse Diagram.
3179552f7358SJed Brown 
3180d083f849SBarry Smith   Collective
3181552f7358SJed Brown 
3182552f7358SJed Brown   Input Parameter:
3183552f7358SJed Brown . comm - The communicator for the DMPlex object
3184552f7358SJed Brown 
3185552f7358SJed Brown   Output Parameter:
3186552f7358SJed Brown . mesh  - The DMPlex object
3187552f7358SJed Brown 
3188552f7358SJed Brown   Level: beginner
3189552f7358SJed Brown 
3190552f7358SJed Brown @*/
3191552f7358SJed Brown PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh)
3192552f7358SJed Brown {
3193552f7358SJed Brown   PetscErrorCode ierr;
3194552f7358SJed Brown 
3195552f7358SJed Brown   PetscFunctionBegin;
3196552f7358SJed Brown   PetscValidPointer(mesh,2);
3197552f7358SJed Brown   ierr = DMCreate(comm, mesh);CHKERRQ(ierr);
3198552f7358SJed Brown   ierr = DMSetType(*mesh, DMPLEX);CHKERRQ(ierr);
3199552f7358SJed Brown   PetscFunctionReturn(0);
3200552f7358SJed Brown }
3201552f7358SJed Brown 
3202b09969d6SVaclav Hapla /*@C
3203b09969d6SVaclav Hapla   DMPlexBuildFromCellListParallel - Build distributed DMPLEX topology from a list of vertices for each cell (common mesh generator output)
3204b09969d6SVaclav Hapla 
3205b09969d6SVaclav Hapla   Input Parameters:
3206b09969d6SVaclav Hapla + dm - The DM
3207b09969d6SVaclav Hapla . numCells - The number of cells owned by this process
320825b6865aSVaclav Hapla . numVertices - The number of vertices owned by this process, or PETSC_DECIDE
320925b6865aSVaclav Hapla . NVertices - The global number of vertices, or PETSC_DECIDE
3210b09969d6SVaclav Hapla . numCorners - The number of vertices for each cell
32115e488331SVaclav Hapla - cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
3212b09969d6SVaclav Hapla 
3213be8c289dSNicolas Barral   Output Parameters:
3214be8c289dSNicolas Barral + vertexSF - (Optional) SF describing complete vertex ownership
3215be8c289dSNicolas Barral - verticesAdjSaved - (Optional) vertex adjacency array
3216b09969d6SVaclav Hapla 
3217b09969d6SVaclav Hapla   Notes:
3218b09969d6SVaclav Hapla   Two triangles sharing a face
3219b09969d6SVaclav Hapla $
3220b09969d6SVaclav Hapla $        2
3221b09969d6SVaclav Hapla $      / | \
3222b09969d6SVaclav Hapla $     /  |  \
3223b09969d6SVaclav Hapla $    /   |   \
3224b09969d6SVaclav Hapla $   0  0 | 1  3
3225b09969d6SVaclav Hapla $    \   |   /
3226b09969d6SVaclav Hapla $     \  |  /
3227b09969d6SVaclav Hapla $      \ | /
3228b09969d6SVaclav Hapla $        1
3229b09969d6SVaclav Hapla would have input
3230b09969d6SVaclav Hapla $  numCells = 2, numVertices = 4
3231b09969d6SVaclav Hapla $  cells = [0 1 2  1 3 2]
3232b09969d6SVaclav Hapla $
3233b09969d6SVaclav Hapla which would result in the DMPlex
3234b09969d6SVaclav Hapla $
3235b09969d6SVaclav Hapla $        4
3236b09969d6SVaclav Hapla $      / | \
3237b09969d6SVaclav Hapla $     /  |  \
3238b09969d6SVaclav Hapla $    /   |   \
3239b09969d6SVaclav Hapla $   2  0 | 1  5
3240b09969d6SVaclav Hapla $    \   |   /
3241b09969d6SVaclav Hapla $     \  |  /
3242b09969d6SVaclav Hapla $      \ | /
3243b09969d6SVaclav Hapla $        3
3244b09969d6SVaclav Hapla 
324525b6865aSVaclav Hapla   Vertices are implicitly numbered consecutively 0,...,NVertices.
324625b6865aSVaclav Hapla   Each rank owns a chunk of numVertices consecutive vertices.
324725b6865aSVaclav Hapla   If numVertices is PETSC_DECIDE, PETSc will distribute them as evenly as possible using PetscLayout.
324825b6865aSVaclav Hapla   If both NVertices and numVertices are PETSC_DECIDE, NVertices is computed by PETSc as the maximum vertex index in cells + 1.
324925b6865aSVaclav Hapla   If only NVertices is PETSC_DECIDE, it is computed as the sum of numVertices over all ranks.
325025b6865aSVaclav Hapla 
3251b09969d6SVaclav Hapla   The cell distribution is arbitrary non-overlapping, independent of the vertex distribution.
3252b09969d6SVaclav Hapla 
3253b09969d6SVaclav Hapla   Not currently supported in Fortran.
3254b09969d6SVaclav Hapla 
3255b09969d6SVaclav Hapla   Level: advanced
3256b09969d6SVaclav Hapla 
3257b09969d6SVaclav Hapla .seealso: DMPlexBuildFromCellList(), DMPlexCreateFromCellListParallelPetsc(), DMPlexBuildCoordinatesFromCellListParallel()
3258b09969d6SVaclav Hapla @*/
3259be8c289dSNicolas Barral PetscErrorCode DMPlexBuildFromCellListParallel(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscInt numCorners, const PetscInt cells[], PetscSF *vertexSF, PetscInt **verticesAdjSaved)
3260a47d0d45SMatthew G. Knepley {
32612464107aSksagiyam   PetscSF         sfPoint;
32622464107aSksagiyam   PetscLayout     layout;
32632464107aSksagiyam   PetscInt        numVerticesAdj, *verticesAdj, *cones, c, p, dim;
32649852e123SBarry Smith   PetscMPIInt     rank, size;
3265a47d0d45SMatthew G. Knepley   PetscErrorCode  ierr;
3266a47d0d45SMatthew G. Knepley 
3267a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
326825b6865aSVaclav Hapla   PetscValidLogicalCollectiveInt(dm,NVertices,4);
3269b09969d6SVaclav Hapla   ierr = PetscLogEventBegin(DMPLEX_BuildFromCellList,dm,0,0,0);CHKERRQ(ierr);
3270ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
3271ffc4695bSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size);CHKERRMPI(ierr);
32726cbf6523SVaclav Hapla   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
327325b6865aSVaclav Hapla   /* Get/check global number of vertices */
327425b6865aSVaclav Hapla   {
327525b6865aSVaclav Hapla     PetscInt NVerticesInCells, i;
327625b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
327725b6865aSVaclav Hapla 
327825b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
327925b6865aSVaclav Hapla     NVerticesInCells = PETSC_MIN_INT;
328025b6865aSVaclav Hapla     for (i=0; i<len; i++) if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
328125b6865aSVaclav Hapla     ++NVerticesInCells;
3282ffc4695bSBarry Smith     ierr = MPI_Allreduce(MPI_IN_PLACE, &NVerticesInCells, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);
328325b6865aSVaclav Hapla 
328425b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE && NVertices == PETSC_DECIDE) NVertices = NVerticesInCells;
328525b6865aSVaclav 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);
328625b6865aSVaclav Hapla   }
32879079aca8SVaclav Hapla   /* Count locally unique vertices */
32889079aca8SVaclav Hapla   {
32899079aca8SVaclav Hapla     PetscHSetI vhash;
32909079aca8SVaclav Hapla     PetscInt off = 0;
32919079aca8SVaclav Hapla 
3292e8f14785SLisandro Dalcin     ierr = PetscHSetICreate(&vhash);CHKERRQ(ierr);
3293a47d0d45SMatthew G. Knepley     for (c = 0; c < numCells; ++c) {
3294a47d0d45SMatthew G. Knepley       for (p = 0; p < numCorners; ++p) {
3295e8f14785SLisandro Dalcin         ierr = PetscHSetIAdd(vhash, cells[c*numCorners+p]);CHKERRQ(ierr);
3296a47d0d45SMatthew G. Knepley       }
3297a47d0d45SMatthew G. Knepley     }
3298e8f14785SLisandro Dalcin     ierr = PetscHSetIGetSize(vhash, &numVerticesAdj);CHKERRQ(ierr);
3299be8c289dSNicolas Barral     if (!verticesAdjSaved) { ierr = PetscMalloc1(numVerticesAdj, &verticesAdj);CHKERRQ(ierr); }
3300be8c289dSNicolas Barral     else { verticesAdj = *verticesAdjSaved; }
3301e8f14785SLisandro Dalcin     ierr = PetscHSetIGetElems(vhash, &off, verticesAdj);CHKERRQ(ierr);
3302e8f14785SLisandro Dalcin     ierr = PetscHSetIDestroy(&vhash);CHKERRQ(ierr);
3303a47d0d45SMatthew G. Knepley     if (off != numVerticesAdj) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid number of local vertices %D should be %D", off, numVerticesAdj);
3304a47d0d45SMatthew G. Knepley   }
33059079aca8SVaclav Hapla   ierr = PetscSortInt(numVerticesAdj, verticesAdj);CHKERRQ(ierr);
3306a47d0d45SMatthew G. Knepley   /* Create cones */
3307a47d0d45SMatthew G. Knepley   ierr = DMPlexSetChart(dm, 0, numCells+numVerticesAdj);CHKERRQ(ierr);
3308a47d0d45SMatthew G. Knepley   for (c = 0; c < numCells; ++c) {ierr = DMPlexSetConeSize(dm, c, numCorners);CHKERRQ(ierr);}
3309a47d0d45SMatthew G. Knepley   ierr = DMSetUp(dm);CHKERRQ(ierr);
3310961cfab0SVaclav Hapla   ierr = DMPlexGetCones(dm,&cones);CHKERRQ(ierr);
3311a47d0d45SMatthew G. Knepley   for (c = 0; c < numCells; ++c) {
3312a47d0d45SMatthew G. Knepley     for (p = 0; p < numCorners; ++p) {
3313a47d0d45SMatthew G. Knepley       const PetscInt gv = cells[c*numCorners+p];
3314a47d0d45SMatthew G. Knepley       PetscInt       lv;
3315a47d0d45SMatthew G. Knepley 
33169079aca8SVaclav Hapla       /* Positions within verticesAdj form 0-based local vertex numbering;
33179079aca8SVaclav Hapla          we need to shift it by numCells to get correct DAG points (cells go first) */
3318a47d0d45SMatthew G. Knepley       ierr = PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv);CHKERRQ(ierr);
3319a47d0d45SMatthew G. Knepley       if (lv < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %D in local connectivity", gv);
3320961cfab0SVaclav Hapla       cones[c*numCorners+p] = lv+numCells;
3321a47d0d45SMatthew G. Knepley     }
3322a47d0d45SMatthew G. Knepley   }
33232464107aSksagiyam   /* Build point sf */
33242464107aSksagiyam   ierr = PetscLayoutCreate(PetscObjectComm((PetscObject)dm), &layout);CHKERRQ(ierr);
33252464107aSksagiyam   ierr = PetscLayoutSetSize(layout, NVertices);CHKERRQ(ierr);
33262464107aSksagiyam   ierr = PetscLayoutSetLocalSize(layout, numVertices);CHKERRQ(ierr);
33272464107aSksagiyam   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
33282464107aSksagiyam   ierr = PetscSFCreateByMatchingIndices(layout, numVerticesAdj, verticesAdj, NULL, numCells, numVerticesAdj, verticesAdj, NULL, numCells, vertexSF, &sfPoint);CHKERRQ(ierr);
33292464107aSksagiyam   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3330be8c289dSNicolas Barral   if (!verticesAdjSaved) { ierr = PetscFree(verticesAdj);CHKERRQ(ierr); }
3331a47d0d45SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) sfPoint, "point SF");CHKERRQ(ierr);
33322464107aSksagiyam   if (dm->sf) {
33332464107aSksagiyam     const char *prefix;
33342464107aSksagiyam 
33352464107aSksagiyam     ierr = PetscObjectGetOptionsPrefix((PetscObject)dm->sf, &prefix);CHKERRQ(ierr);
33362464107aSksagiyam     ierr = PetscObjectSetOptionsPrefix((PetscObject)sfPoint, prefix);CHKERRQ(ierr);
33372464107aSksagiyam   }
33382464107aSksagiyam   ierr = DMSetPointSF(dm, sfPoint);CHKERRQ(ierr);
33392464107aSksagiyam   ierr = PetscSFDestroy(&sfPoint);CHKERRQ(ierr);
33402464107aSksagiyam   if (vertexSF) {ierr = PetscObjectSetName((PetscObject)(*vertexSF), "Vertex Ownership SF");CHKERRQ(ierr);}
3341a47d0d45SMatthew G. Knepley   /* Fill in the rest of the topology structure */
3342a47d0d45SMatthew G. Knepley   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
3343a47d0d45SMatthew G. Knepley   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
3344b09969d6SVaclav Hapla   ierr = PetscLogEventEnd(DMPLEX_BuildFromCellList,dm,0,0,0);CHKERRQ(ierr);
3345a47d0d45SMatthew G. Knepley   PetscFunctionReturn(0);
3346a47d0d45SMatthew G. Knepley }
3347a47d0d45SMatthew G. Knepley 
3348b09969d6SVaclav Hapla /*@C
3349b09969d6SVaclav Hapla   DMPlexBuildCoordinatesFromCellListParallel - Build DM coordinates from a list of coordinates for each owned vertex (common mesh generator output)
3350b09969d6SVaclav Hapla 
3351b09969d6SVaclav Hapla   Input Parameters:
3352b09969d6SVaclav Hapla + dm - The DM
3353b09969d6SVaclav Hapla . spaceDim - The spatial dimension used for coordinates
3354b09969d6SVaclav Hapla . sfVert - SF describing complete vertex ownership
3355b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
3356b09969d6SVaclav Hapla 
3357b09969d6SVaclav Hapla   Level: advanced
3358b09969d6SVaclav Hapla 
3359b09969d6SVaclav Hapla   Notes:
3360b09969d6SVaclav Hapla   Not currently supported in Fortran.
3361b09969d6SVaclav Hapla 
3362b09969d6SVaclav Hapla .seealso: DMPlexBuildCoordinatesFromCellList(), DMPlexCreateFromCellListParallelPetsc(), DMPlexBuildFromCellListParallel()
3363b09969d6SVaclav Hapla @*/
33641edcf0b2SVaclav Hapla PetscErrorCode DMPlexBuildCoordinatesFromCellListParallel(DM dm, PetscInt spaceDim, PetscSF sfVert, const PetscReal vertexCoords[])
3365a47d0d45SMatthew G. Knepley {
3366a47d0d45SMatthew G. Knepley   PetscSection   coordSection;
3367a47d0d45SMatthew G. Knepley   Vec            coordinates;
3368a47d0d45SMatthew G. Knepley   PetscScalar   *coords;
33691edcf0b2SVaclav Hapla   PetscInt       numVertices, numVerticesAdj, coordSize, v, vStart, vEnd;
3370a47d0d45SMatthew G. Knepley   PetscErrorCode ierr;
3371a47d0d45SMatthew G. Knepley 
3372a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
3373b09969d6SVaclav Hapla   ierr = PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0);CHKERRQ(ierr);
33741edcf0b2SVaclav Hapla   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
33751edcf0b2SVaclav 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.");
33769596c6baSMatthew G. Knepley   ierr = DMSetCoordinateDim(dm, spaceDim);CHKERRQ(ierr);
3377a47d0d45SMatthew G. Knepley   ierr = PetscSFGetGraph(sfVert, &numVertices, &numVerticesAdj, NULL, NULL);CHKERRQ(ierr);
33781edcf0b2SVaclav 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);
3379a47d0d45SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
3380a47d0d45SMatthew G. Knepley   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
3381a47d0d45SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(coordSection, 0, spaceDim);CHKERRQ(ierr);
33821edcf0b2SVaclav Hapla   ierr = PetscSectionSetChart(coordSection, vStart, vEnd);CHKERRQ(ierr);
33831edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
3384a47d0d45SMatthew G. Knepley     ierr = PetscSectionSetDof(coordSection, v, spaceDim);CHKERRQ(ierr);
3385a47d0d45SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(coordSection, v, 0, spaceDim);CHKERRQ(ierr);
3386a47d0d45SMatthew G. Knepley   }
3387a47d0d45SMatthew G. Knepley   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
3388a47d0d45SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
3389a47d0d45SMatthew G. Knepley   ierr = VecCreate(PetscObjectComm((PetscObject)dm), &coordinates);CHKERRQ(ierr);
3390a47d0d45SMatthew G. Knepley   ierr = VecSetBlockSize(coordinates, spaceDim);CHKERRQ(ierr);
3391a47d0d45SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
3392a47d0d45SMatthew G. Knepley   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
3393a47d0d45SMatthew G. Knepley   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
3394a47d0d45SMatthew G. Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
3395a47d0d45SMatthew G. Knepley   {
3396a47d0d45SMatthew G. Knepley     MPI_Datatype coordtype;
3397a47d0d45SMatthew G. Knepley 
3398a47d0d45SMatthew G. Knepley     /* Need a temp buffer for coords if we have complex/single */
3399ffc4695bSBarry Smith     ierr = MPI_Type_contiguous(spaceDim, MPIU_SCALAR, &coordtype);CHKERRMPI(ierr);
3400ffc4695bSBarry Smith     ierr = MPI_Type_commit(&coordtype);CHKERRMPI(ierr);
340121016a8bSBarry Smith #if defined(PETSC_USE_COMPLEX)
340221016a8bSBarry Smith     {
340321016a8bSBarry Smith     PetscScalar *svertexCoords;
340421016a8bSBarry Smith     PetscInt    i;
34053612f820SVaclav Hapla     ierr = PetscMalloc1(numVertices*spaceDim,&svertexCoords);CHKERRQ(ierr);
34063612f820SVaclav Hapla     for (i=0; i<numVertices*spaceDim; i++) svertexCoords[i] = vertexCoords[i];
3407ad227feaSJunchao Zhang     ierr = PetscSFBcastBegin(sfVert, coordtype, svertexCoords, coords,MPI_REPLACE);CHKERRQ(ierr);
3408ad227feaSJunchao Zhang     ierr = PetscSFBcastEnd(sfVert, coordtype, svertexCoords, coords,MPI_REPLACE);CHKERRQ(ierr);
340921016a8bSBarry Smith     ierr = PetscFree(svertexCoords);CHKERRQ(ierr);
341021016a8bSBarry Smith     }
341121016a8bSBarry Smith #else
3412ad227feaSJunchao Zhang     ierr = PetscSFBcastBegin(sfVert, coordtype, vertexCoords, coords,MPI_REPLACE);CHKERRQ(ierr);
3413ad227feaSJunchao Zhang     ierr = PetscSFBcastEnd(sfVert, coordtype, vertexCoords, coords,MPI_REPLACE);CHKERRQ(ierr);
341421016a8bSBarry Smith #endif
3415ffc4695bSBarry Smith     ierr = MPI_Type_free(&coordtype);CHKERRMPI(ierr);
3416a47d0d45SMatthew G. Knepley   }
3417a47d0d45SMatthew G. Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
3418a47d0d45SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
3419a47d0d45SMatthew G. Knepley   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
3420b09969d6SVaclav Hapla   ierr = PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0);CHKERRQ(ierr);
3421a47d0d45SMatthew G. Knepley   PetscFunctionReturn(0);
3422a47d0d45SMatthew G. Knepley }
3423a47d0d45SMatthew G. Knepley 
3424c3edce3dSSatish Balay /*@
3425b09969d6SVaclav Hapla   DMPlexCreateFromCellListParallelPetsc - Create distributed DMPLEX from a list of vertices for each cell (common mesh generator output)
3426a47d0d45SMatthew G. Knepley 
3427a47d0d45SMatthew G. Knepley   Input Parameters:
3428a47d0d45SMatthew G. Knepley + comm - The communicator
3429a47d0d45SMatthew G. Knepley . dim - The topological dimension of the mesh
3430a47d0d45SMatthew G. Knepley . numCells - The number of cells owned by this process
343125b6865aSVaclav Hapla . numVertices - The number of vertices owned by this process, or PETSC_DECIDE
343225b6865aSVaclav Hapla . NVertices - The global number of vertices, or PETSC_DECIDE
3433a47d0d45SMatthew G. Knepley . numCorners - The number of vertices for each cell
3434a47d0d45SMatthew G. Knepley . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
3435a47d0d45SMatthew G. Knepley . cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
3436a47d0d45SMatthew G. Knepley . spaceDim - The spatial dimension used for coordinates
3437a47d0d45SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
3438a47d0d45SMatthew G. Knepley 
3439d8d19677SJose E. Roman   Output Parameters:
344018d54ad4SMichael Lange + dm - The DM
3441be8c289dSNicolas Barral . vertexSF - (Optional) SF describing complete vertex ownership
3442be8c289dSNicolas Barral - verticesAdjSaved - (Optional) vertex adjacency array
3443a47d0d45SMatthew G. Knepley 
3444b09969d6SVaclav Hapla   Notes:
3445b09969d6SVaclav Hapla   This function is just a convenient sequence of DMCreate(), DMSetType(), DMSetDimension(),
3446b09969d6SVaclav Hapla   DMPlexBuildFromCellListParallel(), DMPlexInterpolate(), DMPlexBuildCoordinatesFromCellListParallel()
3447a47d0d45SMatthew G. Knepley 
344825b6865aSVaclav Hapla   See DMPlexBuildFromCellListParallel() for an example and details about the topology-related parameters.
344925b6865aSVaclav Hapla   See DMPlexBuildCoordinatesFromCellListParallel() for details about the geometry-related parameters.
345025b6865aSVaclav Hapla 
3451b09969d6SVaclav Hapla   Level: intermediate
3452a47d0d45SMatthew G. Knepley 
3453b09969d6SVaclav Hapla .seealso: DMPlexCreateFromCellListPetsc(), DMPlexBuildFromCellListParallel(), DMPlexBuildCoordinatesFromCellListParallel(), DMPlexCreateFromDAG(), DMPlexCreate()
3454a47d0d45SMatthew G. Knepley @*/
3455be8c289dSNicolas Barral 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, PetscInt **verticesAdj, DM *dm)
3456a47d0d45SMatthew G. Knepley {
3457a47d0d45SMatthew G. Knepley   PetscSF        sfVert;
3458a47d0d45SMatthew G. Knepley   PetscErrorCode ierr;
3459a47d0d45SMatthew G. Knepley 
3460a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
3461a47d0d45SMatthew G. Knepley   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
3462a47d0d45SMatthew G. Knepley   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
3463a47d0d45SMatthew G. Knepley   PetscValidLogicalCollectiveInt(*dm, dim, 2);
3464064a246eSJacob Faibussowitsch   PetscValidLogicalCollectiveInt(*dm, spaceDim, 9);
3465a47d0d45SMatthew G. Knepley   ierr = DMSetDimension(*dm, dim);CHKERRQ(ierr);
3466be8c289dSNicolas Barral   ierr = DMPlexBuildFromCellListParallel(*dm, numCells, numVertices, NVertices, numCorners, cells, &sfVert, verticesAdj);CHKERRQ(ierr);
3467a47d0d45SMatthew G. Knepley   if (interpolate) {
34685fd9971aSMatthew G. Knepley     DM idm;
3469a47d0d45SMatthew G. Knepley 
3470a47d0d45SMatthew G. Knepley     ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr);
3471a47d0d45SMatthew G. Knepley     ierr = DMDestroy(dm);CHKERRQ(ierr);
3472a47d0d45SMatthew G. Knepley     *dm  = idm;
3473a47d0d45SMatthew G. Knepley   }
34741edcf0b2SVaclav Hapla   ierr = DMPlexBuildCoordinatesFromCellListParallel(*dm, spaceDim, sfVert, vertexCoords);CHKERRQ(ierr);
347518d54ad4SMichael Lange   if (vertexSF) *vertexSF = sfVert;
3476fba955ccSBarry Smith   else {ierr = PetscSFDestroy(&sfVert);CHKERRQ(ierr);}
3477a47d0d45SMatthew G. Knepley   PetscFunctionReturn(0);
3478a47d0d45SMatthew G. Knepley }
3479a47d0d45SMatthew G. Knepley 
3480a4a685f2SJacob Faibussowitsch /*@
3481a4a685f2SJacob Faibussowitsch   DMPlexCreateFromCellListParallel - Deprecated, use DMPlexCreateFromCellListParallelPetsc()
3482a4a685f2SJacob Faibussowitsch 
3483a4a685f2SJacob Faibussowitsch   Level: deprecated
3484a4a685f2SJacob Faibussowitsch 
3485a4a685f2SJacob Faibussowitsch .seealso: DMPlexCreateFromCellListParallelPetsc()
3486a4a685f2SJacob Faibussowitsch @*/
3487a4a685f2SJacob 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)
3488a4a685f2SJacob Faibussowitsch {
3489a4a685f2SJacob Faibussowitsch   PetscErrorCode ierr;
3490a4a685f2SJacob Faibussowitsch   PetscInt       i;
3491a4a685f2SJacob Faibussowitsch   PetscInt       *pintCells;
3492a4a685f2SJacob Faibussowitsch 
3493a4a685f2SJacob Faibussowitsch   PetscFunctionBegin;
3494a4a685f2SJacob 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));
3495a4a685f2SJacob Faibussowitsch   if (sizeof(int) == sizeof(PetscInt)) {
3496a4a685f2SJacob Faibussowitsch     pintCells = (PetscInt *) cells;
3497a4a685f2SJacob Faibussowitsch   } else {
3498a4a685f2SJacob Faibussowitsch     ierr = PetscMalloc1(numCells*numCorners, &pintCells);CHKERRQ(ierr);
3499a4a685f2SJacob Faibussowitsch     for (i = 0; i < numCells*numCorners; i++) {
3500a4a685f2SJacob Faibussowitsch       pintCells[i] = (PetscInt) cells[i];
3501a4a685f2SJacob Faibussowitsch     }
3502a4a685f2SJacob Faibussowitsch   }
3503be8c289dSNicolas Barral   ierr = DMPlexCreateFromCellListParallelPetsc(comm, dim, numCells, numVertices, PETSC_DECIDE, numCorners, interpolate, pintCells, spaceDim, vertexCoords, vertexSF, NULL, dm);CHKERRQ(ierr);
3504a4a685f2SJacob Faibussowitsch   if (sizeof(int) != sizeof(PetscInt)) {
3505a4a685f2SJacob Faibussowitsch     ierr = PetscFree(pintCells);CHKERRQ(ierr);
3506a4a685f2SJacob Faibussowitsch   }
3507a4a685f2SJacob Faibussowitsch   PetscFunctionReturn(0);
3508a4a685f2SJacob Faibussowitsch }
3509a4a685f2SJacob Faibussowitsch 
3510b09969d6SVaclav Hapla /*@C
3511b09969d6SVaclav Hapla   DMPlexBuildFromCellList - Build DMPLEX topology from a list of vertices for each cell (common mesh generator output)
35129298eaa6SMatthew G Knepley 
35139298eaa6SMatthew G Knepley   Input Parameters:
3514b09969d6SVaclav Hapla + dm - The DM
3515b09969d6SVaclav Hapla . numCells - The number of cells owned by this process
351625b6865aSVaclav Hapla . numVertices - The number of vertices owned by this process, or PETSC_DECIDE
35179298eaa6SMatthew G Knepley . numCorners - The number of vertices for each cell
35185e488331SVaclav Hapla - cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
35199298eaa6SMatthew G Knepley 
3520b09969d6SVaclav Hapla   Level: advanced
35219298eaa6SMatthew G Knepley 
3522b09969d6SVaclav Hapla   Notes:
3523b09969d6SVaclav Hapla   Two triangles sharing a face
35249298eaa6SMatthew G Knepley $
35259298eaa6SMatthew G Knepley $        2
35269298eaa6SMatthew G Knepley $      / | \
35279298eaa6SMatthew G Knepley $     /  |  \
35289298eaa6SMatthew G Knepley $    /   |   \
35299298eaa6SMatthew G Knepley $   0  0 | 1  3
35309298eaa6SMatthew G Knepley $    \   |   /
35319298eaa6SMatthew G Knepley $     \  |  /
35329298eaa6SMatthew G Knepley $      \ | /
35339298eaa6SMatthew G Knepley $        1
35349298eaa6SMatthew G Knepley would have input
35359298eaa6SMatthew G Knepley $  numCells = 2, numVertices = 4
35369298eaa6SMatthew G Knepley $  cells = [0 1 2  1 3 2]
35379298eaa6SMatthew G Knepley $
35389298eaa6SMatthew G Knepley which would result in the DMPlex
35399298eaa6SMatthew G Knepley $
35409298eaa6SMatthew G Knepley $        4
35419298eaa6SMatthew G Knepley $      / | \
35429298eaa6SMatthew G Knepley $     /  |  \
35439298eaa6SMatthew G Knepley $    /   |   \
35449298eaa6SMatthew G Knepley $   2  0 | 1  5
35459298eaa6SMatthew G Knepley $    \   |   /
35469298eaa6SMatthew G Knepley $     \  |  /
35479298eaa6SMatthew G Knepley $      \ | /
35489298eaa6SMatthew G Knepley $        3
35499298eaa6SMatthew G Knepley 
355025b6865aSVaclav Hapla   If numVertices is PETSC_DECIDE, it is computed by PETSc as the maximum vertex index in cells + 1.
355125b6865aSVaclav Hapla 
3552b09969d6SVaclav Hapla   Not currently supported in Fortran.
35539298eaa6SMatthew G Knepley 
3554b09969d6SVaclav Hapla .seealso: DMPlexBuildFromCellListParallel(), DMPlexBuildCoordinatesFromCellList(), DMPlexCreateFromCellListPetsc()
3555b09969d6SVaclav Hapla @*/
35565e488331SVaclav Hapla PetscErrorCode DMPlexBuildFromCellList(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const PetscInt cells[])
3557b09969d6SVaclav Hapla {
3558961cfab0SVaclav Hapla   PetscInt      *cones, c, p, dim;
3559b09969d6SVaclav Hapla   PetscErrorCode ierr;
3560b09969d6SVaclav Hapla 
3561b09969d6SVaclav Hapla   PetscFunctionBegin;
3562b09969d6SVaclav Hapla   ierr = PetscLogEventBegin(DMPLEX_BuildFromCellList,dm,0,0,0);CHKERRQ(ierr);
3563b09969d6SVaclav Hapla   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
356425b6865aSVaclav Hapla   /* Get/check global number of vertices */
356525b6865aSVaclav Hapla   {
356625b6865aSVaclav Hapla     PetscInt NVerticesInCells, i;
356725b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
356825b6865aSVaclav Hapla 
356925b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
357025b6865aSVaclav Hapla     NVerticesInCells = PETSC_MIN_INT;
357125b6865aSVaclav Hapla     for (i=0; i<len; i++) if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
357225b6865aSVaclav Hapla     ++NVerticesInCells;
357325b6865aSVaclav Hapla 
357425b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE) numVertices = NVerticesInCells;
357525b6865aSVaclav 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);
357625b6865aSVaclav Hapla   }
3577b09969d6SVaclav Hapla   ierr = DMPlexSetChart(dm, 0, numCells+numVertices);CHKERRQ(ierr);
3578b09969d6SVaclav Hapla   for (c = 0; c < numCells; ++c) {
3579b09969d6SVaclav Hapla     ierr = DMPlexSetConeSize(dm, c, numCorners);CHKERRQ(ierr);
3580b09969d6SVaclav Hapla   }
3581b09969d6SVaclav Hapla   ierr = DMSetUp(dm);CHKERRQ(ierr);
3582961cfab0SVaclav Hapla   ierr = DMPlexGetCones(dm,&cones);CHKERRQ(ierr);
3583b09969d6SVaclav Hapla   for (c = 0; c < numCells; ++c) {
3584b09969d6SVaclav Hapla     for (p = 0; p < numCorners; ++p) {
3585961cfab0SVaclav Hapla       cones[c*numCorners+p] = cells[c*numCorners+p]+numCells;
3586b09969d6SVaclav Hapla     }
3587b09969d6SVaclav Hapla   }
3588b09969d6SVaclav Hapla   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
3589b09969d6SVaclav Hapla   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
3590b09969d6SVaclav Hapla   ierr = PetscLogEventEnd(DMPLEX_BuildFromCellList,dm,0,0,0);CHKERRQ(ierr);
3591b09969d6SVaclav Hapla   PetscFunctionReturn(0);
3592b09969d6SVaclav Hapla }
3593b09969d6SVaclav Hapla 
3594b09969d6SVaclav Hapla /*@C
3595b09969d6SVaclav Hapla   DMPlexBuildCoordinatesFromCellList - Build DM coordinates from a list of coordinates for each owned vertex (common mesh generator output)
3596b09969d6SVaclav Hapla 
3597b09969d6SVaclav Hapla   Input Parameters:
3598b09969d6SVaclav Hapla + dm - The DM
3599b09969d6SVaclav Hapla . spaceDim - The spatial dimension used for coordinates
3600b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
3601b09969d6SVaclav Hapla 
3602b09969d6SVaclav Hapla   Level: advanced
3603b09969d6SVaclav Hapla 
3604b09969d6SVaclav Hapla   Notes:
3605b09969d6SVaclav Hapla   Not currently supported in Fortran.
3606b09969d6SVaclav Hapla 
3607b09969d6SVaclav Hapla .seealso: DMPlexBuildCoordinatesFromCellListParallel(), DMPlexCreateFromCellListPetsc(), DMPlexBuildFromCellList()
3608b09969d6SVaclav Hapla @*/
36091edcf0b2SVaclav Hapla PetscErrorCode DMPlexBuildCoordinatesFromCellList(DM dm, PetscInt spaceDim, const PetscReal vertexCoords[])
3610b09969d6SVaclav Hapla {
3611b09969d6SVaclav Hapla   PetscSection   coordSection;
3612b09969d6SVaclav Hapla   Vec            coordinates;
3613b09969d6SVaclav Hapla   DM             cdm;
3614b09969d6SVaclav Hapla   PetscScalar   *coords;
36151edcf0b2SVaclav Hapla   PetscInt       v, vStart, vEnd, d;
3616b09969d6SVaclav Hapla   PetscErrorCode ierr;
3617b09969d6SVaclav Hapla 
3618b09969d6SVaclav Hapla   PetscFunctionBegin;
3619b09969d6SVaclav Hapla   ierr = PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0);CHKERRQ(ierr);
36201edcf0b2SVaclav Hapla   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
36211edcf0b2SVaclav 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.");
3622b09969d6SVaclav Hapla   ierr = DMSetCoordinateDim(dm, spaceDim);CHKERRQ(ierr);
3623b09969d6SVaclav Hapla   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
3624b09969d6SVaclav Hapla   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
3625b09969d6SVaclav Hapla   ierr = PetscSectionSetFieldComponents(coordSection, 0, spaceDim);CHKERRQ(ierr);
36261edcf0b2SVaclav Hapla   ierr = PetscSectionSetChart(coordSection, vStart, vEnd);CHKERRQ(ierr);
36271edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
3628b09969d6SVaclav Hapla     ierr = PetscSectionSetDof(coordSection, v, spaceDim);CHKERRQ(ierr);
3629b09969d6SVaclav Hapla     ierr = PetscSectionSetFieldDof(coordSection, v, 0, spaceDim);CHKERRQ(ierr);
3630b09969d6SVaclav Hapla   }
3631b09969d6SVaclav Hapla   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
3632b09969d6SVaclav Hapla 
3633b09969d6SVaclav Hapla   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
3634b09969d6SVaclav Hapla   ierr = DMCreateLocalVector(cdm, &coordinates);CHKERRQ(ierr);
3635b09969d6SVaclav Hapla   ierr = VecSetBlockSize(coordinates, spaceDim);CHKERRQ(ierr);
3636b09969d6SVaclav Hapla   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
363799946890SBarry Smith   ierr = VecGetArrayWrite(coordinates, &coords);CHKERRQ(ierr);
36381edcf0b2SVaclav Hapla   for (v = 0; v < vEnd-vStart; ++v) {
3639b09969d6SVaclav Hapla     for (d = 0; d < spaceDim; ++d) {
3640b09969d6SVaclav Hapla       coords[v*spaceDim+d] = vertexCoords[v*spaceDim+d];
3641b09969d6SVaclav Hapla     }
3642b09969d6SVaclav Hapla   }
364399946890SBarry Smith   ierr = VecRestoreArrayWrite(coordinates, &coords);CHKERRQ(ierr);
3644b09969d6SVaclav Hapla   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
3645b09969d6SVaclav Hapla   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
3646b09969d6SVaclav Hapla   ierr = PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0);CHKERRQ(ierr);
3647b09969d6SVaclav Hapla   PetscFunctionReturn(0);
3648b09969d6SVaclav Hapla }
3649b09969d6SVaclav Hapla 
3650b09969d6SVaclav Hapla /*@
36513df08285SMatthew 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
36523df08285SMatthew G. Knepley 
36533df08285SMatthew G. Knepley   Collective on comm
3654b09969d6SVaclav Hapla 
3655b09969d6SVaclav Hapla   Input Parameters:
3656b09969d6SVaclav Hapla + comm - The communicator
3657b09969d6SVaclav Hapla . dim - The topological dimension of the mesh
36583df08285SMatthew G. Knepley . numCells - The number of cells, only on process 0
36593df08285SMatthew G. Knepley . numVertices - The number of vertices owned by this process, or PETSC_DECIDE, only on process 0
36603df08285SMatthew G. Knepley . numCorners - The number of vertices for each cell, only on process 0
3661b09969d6SVaclav Hapla . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
36623df08285SMatthew G. Knepley . cells - An array of numCells*numCorners numbers, the vertices for each cell, only on process 0
3663b09969d6SVaclav Hapla . spaceDim - The spatial dimension used for coordinates
36643df08285SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex, only on process 0
3665b09969d6SVaclav Hapla 
3666b09969d6SVaclav Hapla   Output Parameter:
36673df08285SMatthew G. Knepley . dm - The DM, which only has points on process 0
3668b09969d6SVaclav Hapla 
3669b09969d6SVaclav Hapla   Notes:
3670b09969d6SVaclav Hapla   This function is just a convenient sequence of DMCreate(), DMSetType(), DMSetDimension(), DMPlexBuildFromCellList(),
3671b09969d6SVaclav Hapla   DMPlexInterpolate(), DMPlexBuildCoordinatesFromCellList()
3672b09969d6SVaclav Hapla 
367325b6865aSVaclav Hapla   See DMPlexBuildFromCellList() for an example and details about the topology-related parameters.
367425b6865aSVaclav Hapla   See DMPlexBuildCoordinatesFromCellList() for details about the geometry-related parameters.
36753df08285SMatthew G. Knepley   See DMPlexCreateFromCellListParallelPetsc() for parallel input
367625b6865aSVaclav Hapla 
3677b09969d6SVaclav Hapla   Level: intermediate
3678b09969d6SVaclav Hapla 
3679b09969d6SVaclav Hapla .seealso: DMPlexCreateFromCellListParallelPetsc(), DMPlexBuildFromCellList(), DMPlexBuildCoordinatesFromCellList(), DMPlexCreateFromDAG(), DMPlexCreate()
36809298eaa6SMatthew G Knepley @*/
3681a4a685f2SJacob 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)
36829298eaa6SMatthew G Knepley {
36833df08285SMatthew G. Knepley   PetscMPIInt    rank;
36849298eaa6SMatthew G Knepley   PetscErrorCode ierr;
36859298eaa6SMatthew G Knepley 
36869298eaa6SMatthew G Knepley   PetscFunctionBegin;
36875fd8819aSMatthew 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.");
36883df08285SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
36899298eaa6SMatthew G Knepley   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
36909298eaa6SMatthew G Knepley   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
3691c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(*dm, dim);CHKERRQ(ierr);
36923df08285SMatthew G. Knepley   if (!rank) {ierr = DMPlexBuildFromCellList(*dm, numCells, numVertices, numCorners, cells);CHKERRQ(ierr);}
36933df08285SMatthew G. Knepley   else       {ierr = DMPlexBuildFromCellList(*dm, 0, 0, 0, NULL);CHKERRQ(ierr);}
36949298eaa6SMatthew G Knepley   if (interpolate) {
36955fd9971aSMatthew G. Knepley     DM idm;
36969298eaa6SMatthew G Knepley 
36979298eaa6SMatthew G Knepley     ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr);
36989298eaa6SMatthew G Knepley     ierr = DMDestroy(dm);CHKERRQ(ierr);
36999298eaa6SMatthew G Knepley     *dm  = idm;
37009298eaa6SMatthew G Knepley   }
37013df08285SMatthew G. Knepley   if (!rank) {ierr = DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, vertexCoords);CHKERRQ(ierr);}
37023df08285SMatthew G. Knepley   else       {ierr = DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, NULL);CHKERRQ(ierr);}
37039298eaa6SMatthew G Knepley   PetscFunctionReturn(0);
37049298eaa6SMatthew G Knepley }
37059298eaa6SMatthew G Knepley 
3706939f6067SMatthew G. Knepley /*@
3707a4a685f2SJacob Faibussowitsch   DMPlexCreateFromCellList - Deprecated, use DMPlexCreateFromCellListPetsc()
3708a4a685f2SJacob Faibussowitsch 
3709a4a685f2SJacob Faibussowitsch   Level: deprecated
3710a4a685f2SJacob Faibussowitsch 
3711a4a685f2SJacob Faibussowitsch .seealso: DMPlexCreateFromCellListPetsc()
3712a4a685f2SJacob Faibussowitsch @*/
3713a4a685f2SJacob 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)
3714a4a685f2SJacob Faibussowitsch {
3715a4a685f2SJacob Faibussowitsch   PetscErrorCode ierr;
3716a4a685f2SJacob Faibussowitsch   PetscInt       i;
3717a4a685f2SJacob Faibussowitsch   PetscInt       *pintCells;
3718a4a685f2SJacob Faibussowitsch   PetscReal      *prealVC;
3719a4a685f2SJacob Faibussowitsch 
3720a4a685f2SJacob Faibussowitsch   PetscFunctionBegin;
3721a4a685f2SJacob 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));
3722a4a685f2SJacob Faibussowitsch   if (sizeof(int) == sizeof(PetscInt)) {
3723a4a685f2SJacob Faibussowitsch     pintCells = (PetscInt *) cells;
3724a4a685f2SJacob Faibussowitsch   } else {
3725a4a685f2SJacob Faibussowitsch     ierr = PetscMalloc1(numCells*numCorners, &pintCells);CHKERRQ(ierr);
3726a4a685f2SJacob Faibussowitsch     for (i = 0; i < numCells*numCorners; i++) {
3727a4a685f2SJacob Faibussowitsch       pintCells[i] = (PetscInt) cells[i];
3728a4a685f2SJacob Faibussowitsch     }
3729a4a685f2SJacob Faibussowitsch   }
3730a4a685f2SJacob 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));
3731a4a685f2SJacob Faibussowitsch   if (sizeof(double) == sizeof(PetscReal)) {
3732a4a685f2SJacob Faibussowitsch     prealVC = (PetscReal *) vertexCoords;
3733a4a685f2SJacob Faibussowitsch   } else {
3734a4a685f2SJacob Faibussowitsch     ierr = PetscMalloc1(numVertices*spaceDim, &prealVC);CHKERRQ(ierr);
3735a4a685f2SJacob Faibussowitsch     for (i = 0; i < numVertices*spaceDim; i++) {
3736a4a685f2SJacob Faibussowitsch       prealVC[i] = (PetscReal) vertexCoords[i];
3737a4a685f2SJacob Faibussowitsch     }
3738a4a685f2SJacob Faibussowitsch   }
3739a4a685f2SJacob Faibussowitsch   ierr = DMPlexCreateFromCellListPetsc(comm, dim, numCells, numVertices, numCorners, interpolate, pintCells, spaceDim, prealVC, dm);CHKERRQ(ierr);
3740a4a685f2SJacob Faibussowitsch   if (sizeof(int) != sizeof(PetscInt)) {
3741a4a685f2SJacob Faibussowitsch     ierr = PetscFree(pintCells);CHKERRQ(ierr);
3742a4a685f2SJacob Faibussowitsch   }
3743a4a685f2SJacob Faibussowitsch   if (sizeof(double) != sizeof(PetscReal)) {
3744a4a685f2SJacob Faibussowitsch     ierr = PetscFree(prealVC);CHKERRQ(ierr);
3745a4a685f2SJacob Faibussowitsch   }
3746a4a685f2SJacob Faibussowitsch   PetscFunctionReturn(0);
3747a4a685f2SJacob Faibussowitsch }
3748a4a685f2SJacob Faibussowitsch 
3749a4a685f2SJacob Faibussowitsch /*@
3750939f6067SMatthew 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
3751939f6067SMatthew G. Knepley 
3752939f6067SMatthew G. Knepley   Input Parameters:
3753c73cfb54SMatthew G. Knepley + dm - The empty DM object, usually from DMCreate() and DMSetDimension()
3754939f6067SMatthew G. Knepley . depth - The depth of the DAG
3755367003a6SStefano Zampini . numPoints - Array of size depth + 1 containing the number of points at each depth
3756939f6067SMatthew G. Knepley . coneSize - The cone size of each point
3757939f6067SMatthew G. Knepley . cones - The concatenation of the cone points for each point, the cone list must be oriented correctly for each point
3758939f6067SMatthew G. Knepley . coneOrientations - The orientation of each cone point
3759367003a6SStefano Zampini - vertexCoords - An array of numPoints[0]*spacedim numbers representing the coordinates of each vertex, with spacedim the value set via DMSetCoordinateDim()
3760939f6067SMatthew G. Knepley 
3761939f6067SMatthew G. Knepley   Output Parameter:
3762939f6067SMatthew G. Knepley . dm - The DM
3763939f6067SMatthew G. Knepley 
3764939f6067SMatthew G. Knepley   Note: Two triangles sharing a face would have input
3765939f6067SMatthew G. Knepley $  depth = 1, numPoints = [4 2], coneSize = [3 3 0 0 0 0]
3766939f6067SMatthew G. Knepley $  cones = [2 3 4  3 5 4], coneOrientations = [0 0 0  0 0 0]
3767939f6067SMatthew G. Knepley $ vertexCoords = [-1.0 0.0  0.0 -1.0  0.0 1.0  1.0 0.0]
3768939f6067SMatthew G. Knepley $
3769939f6067SMatthew G. Knepley which would result in the DMPlex
3770939f6067SMatthew G. Knepley $
3771939f6067SMatthew G. Knepley $        4
3772939f6067SMatthew G. Knepley $      / | \
3773939f6067SMatthew G. Knepley $     /  |  \
3774939f6067SMatthew G. Knepley $    /   |   \
3775939f6067SMatthew G. Knepley $   2  0 | 1  5
3776939f6067SMatthew G. Knepley $    \   |   /
3777939f6067SMatthew G. Knepley $     \  |  /
3778939f6067SMatthew G. Knepley $      \ | /
3779939f6067SMatthew G. Knepley $        3
3780939f6067SMatthew G. Knepley $
3781a4a685f2SJacob Faibussowitsch $ Notice that all points are numbered consecutively, unlike DMPlexCreateFromCellListPetsc()
3782939f6067SMatthew G. Knepley 
3783939f6067SMatthew G. Knepley   Level: advanced
3784939f6067SMatthew G. Knepley 
3785a4a685f2SJacob Faibussowitsch .seealso: DMPlexCreateFromCellListPetsc(), DMPlexCreate()
3786939f6067SMatthew G. Knepley @*/
37879298eaa6SMatthew G Knepley PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[])
37889298eaa6SMatthew G Knepley {
37899298eaa6SMatthew G Knepley   Vec            coordinates;
37909298eaa6SMatthew G Knepley   PetscSection   coordSection;
37919298eaa6SMatthew G Knepley   PetscScalar    *coords;
3792811e8653SToby Isaac   PetscInt       coordSize, firstVertex = -1, pStart = 0, pEnd = 0, p, v, dim, dimEmbed, d, off;
37939298eaa6SMatthew G Knepley   PetscErrorCode ierr;
37949298eaa6SMatthew G Knepley 
37959298eaa6SMatthew G Knepley   PetscFunctionBegin;
3796c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3797811e8653SToby Isaac   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
379889c010cfSBarry Smith   if (dimEmbed < dim) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Embedding dimension %D cannot be less than intrinsic dimension %d",dimEmbed,dim);
37999298eaa6SMatthew G Knepley   for (d = 0; d <= depth; ++d) pEnd += numPoints[d];
38009298eaa6SMatthew G Knepley   ierr = DMPlexSetChart(dm, pStart, pEnd);CHKERRQ(ierr);
38019298eaa6SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
38029298eaa6SMatthew G Knepley     ierr = DMPlexSetConeSize(dm, p, coneSize[p-pStart]);CHKERRQ(ierr);
380397e052ccSToby Isaac     if (firstVertex < 0 && !coneSize[p - pStart]) {
380497e052ccSToby Isaac       firstVertex = p - pStart;
38059298eaa6SMatthew G Knepley     }
380697e052ccSToby Isaac   }
380789c010cfSBarry Smith   if (firstVertex < 0 && numPoints[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected %D vertices but could not find any", numPoints[0]);
38089298eaa6SMatthew G Knepley   ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
38099298eaa6SMatthew G Knepley   for (p = pStart, off = 0; p < pEnd; off += coneSize[p-pStart], ++p) {
38109298eaa6SMatthew G Knepley     ierr = DMPlexSetCone(dm, p, &cones[off]);CHKERRQ(ierr);
38119298eaa6SMatthew G Knepley     ierr = DMPlexSetConeOrientation(dm, p, &coneOrientations[off]);CHKERRQ(ierr);
38129298eaa6SMatthew G Knepley   }
38139298eaa6SMatthew G Knepley   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
38149298eaa6SMatthew G Knepley   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
38159298eaa6SMatthew G Knepley   /* Build coordinates */
3816c2166f76SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
38179298eaa6SMatthew G Knepley   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
3818811e8653SToby Isaac   ierr = PetscSectionSetFieldComponents(coordSection, 0, dimEmbed);CHKERRQ(ierr);
38199298eaa6SMatthew G Knepley   ierr = PetscSectionSetChart(coordSection, firstVertex, firstVertex+numPoints[0]);CHKERRQ(ierr);
38209298eaa6SMatthew G Knepley   for (v = firstVertex; v < firstVertex+numPoints[0]; ++v) {
3821811e8653SToby Isaac     ierr = PetscSectionSetDof(coordSection, v, dimEmbed);CHKERRQ(ierr);
3822811e8653SToby Isaac     ierr = PetscSectionSetFieldDof(coordSection, v, 0, dimEmbed);CHKERRQ(ierr);
38239298eaa6SMatthew G Knepley   }
38249298eaa6SMatthew G Knepley   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
38259298eaa6SMatthew G Knepley   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
38268b9ced59SLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
38276f8cbbeeSMatthew G Knepley   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
38289298eaa6SMatthew G Knepley   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
38298b9ced59SLisandro Dalcin   ierr = VecSetBlockSize(coordinates, dimEmbed);CHKERRQ(ierr);
38302eb5907fSJed Brown   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
38319318fe57SMatthew G. Knepley   if (vertexCoords) {
38329298eaa6SMatthew G Knepley     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
38339298eaa6SMatthew G Knepley     for (v = 0; v < numPoints[0]; ++v) {
38349298eaa6SMatthew G Knepley       PetscInt off;
38359298eaa6SMatthew G Knepley 
38369298eaa6SMatthew G Knepley       ierr = PetscSectionGetOffset(coordSection, v+firstVertex, &off);CHKERRQ(ierr);
3837811e8653SToby Isaac       for (d = 0; d < dimEmbed; ++d) {
3838811e8653SToby Isaac         coords[off+d] = vertexCoords[v*dimEmbed+d];
38399298eaa6SMatthew G Knepley       }
38409298eaa6SMatthew G Knepley     }
38419318fe57SMatthew G. Knepley   }
38429298eaa6SMatthew G Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
38439298eaa6SMatthew G Knepley   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
38449298eaa6SMatthew G Knepley   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
38459298eaa6SMatthew G Knepley   PetscFunctionReturn(0);
38469298eaa6SMatthew G Knepley }
38478415267dSToby Isaac 
3848ca522641SMatthew G. Knepley /*@C
38498ca92349SMatthew G. Knepley   DMPlexCreateCellVertexFromFile - Create a DMPlex mesh from a simple cell-vertex file.
38508ca92349SMatthew G. Knepley 
38518ca92349SMatthew G. Knepley + comm        - The MPI communicator
38528ca92349SMatthew G. Knepley . filename    - Name of the .dat file
38538ca92349SMatthew G. Knepley - interpolate - Create faces and edges in the mesh
38548ca92349SMatthew G. Knepley 
38558ca92349SMatthew G. Knepley   Output Parameter:
38568ca92349SMatthew G. Knepley . dm  - The DM object representing the mesh
38578ca92349SMatthew G. Knepley 
38588ca92349SMatthew G. Knepley   Note: The format is the simplest possible:
38598ca92349SMatthew G. Knepley $ Ne
38608ca92349SMatthew G. Knepley $ v0 v1 ... vk
38618ca92349SMatthew G. Knepley $ Nv
38628ca92349SMatthew G. Knepley $ x y z marker
38638ca92349SMatthew G. Knepley 
38648ca92349SMatthew G. Knepley   Level: beginner
38658ca92349SMatthew G. Knepley 
38668ca92349SMatthew G. Knepley .seealso: DMPlexCreateFromFile(), DMPlexCreateMedFromFile(), DMPlexCreateGmsh(), DMPlexCreate()
38678ca92349SMatthew G. Knepley @*/
38688ca92349SMatthew G. Knepley PetscErrorCode DMPlexCreateCellVertexFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
38698ca92349SMatthew G. Knepley {
38708ca92349SMatthew G. Knepley   DMLabel         marker;
38718ca92349SMatthew G. Knepley   PetscViewer     viewer;
38728ca92349SMatthew G. Knepley   Vec             coordinates;
38738ca92349SMatthew G. Knepley   PetscSection    coordSection;
38748ca92349SMatthew G. Knepley   PetscScalar    *coords;
38758ca92349SMatthew G. Knepley   char            line[PETSC_MAX_PATH_LEN];
38768ca92349SMatthew G. Knepley   PetscInt        dim = 3, cdim = 3, coordSize, v, c, d;
38778ca92349SMatthew G. Knepley   PetscMPIInt     rank;
3878f8d5e320SMatthew G. Knepley   int             snum, Nv, Nc, Ncn, Nl;
38798ca92349SMatthew G. Knepley   PetscErrorCode  ierr;
38808ca92349SMatthew G. Knepley 
38818ca92349SMatthew G. Knepley   PetscFunctionBegin;
3882ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
38838ca92349SMatthew G. Knepley   ierr = PetscViewerCreate(comm, &viewer);CHKERRQ(ierr);
38848ca92349SMatthew G. Knepley   ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
38858ca92349SMatthew G. Knepley   ierr = PetscViewerFileSetMode(viewer, FILE_MODE_READ);CHKERRQ(ierr);
38868ca92349SMatthew G. Knepley   ierr = PetscViewerFileSetName(viewer, filename);CHKERRQ(ierr);
3887dd400576SPatrick Sanan   if (rank == 0) {
3888f8d5e320SMatthew G. Knepley     ierr = PetscViewerRead(viewer, line, 4, NULL, PETSC_STRING);CHKERRQ(ierr);
3889f8d5e320SMatthew G. Knepley     snum = sscanf(line, "%d %d %d %d", &Nc, &Nv, &Ncn, &Nl);
3890f8d5e320SMatthew G. Knepley     if (snum != 4) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
389125ce1634SJed Brown   } else {
3892f8d5e320SMatthew G. Knepley     Nc = Nv = Ncn = Nl = 0;
38938ca92349SMatthew G. Knepley   }
38948ca92349SMatthew G. Knepley   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
38958ca92349SMatthew G. Knepley   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
38968ca92349SMatthew G. Knepley   ierr = DMPlexSetChart(*dm, 0, Nc+Nv);CHKERRQ(ierr);
38978ca92349SMatthew G. Knepley   ierr = DMSetDimension(*dm, dim);CHKERRQ(ierr);
38988ca92349SMatthew G. Knepley   ierr = DMSetCoordinateDim(*dm, cdim);CHKERRQ(ierr);
38998ca92349SMatthew G. Knepley   /* Read topology */
3900dd400576SPatrick Sanan   if (rank == 0) {
3901f8d5e320SMatthew G. Knepley     char     format[PETSC_MAX_PATH_LEN];
3902f8d5e320SMatthew G. Knepley     PetscInt cone[8];
39038ca92349SMatthew G. Knepley     int      vbuf[8], v;
39048ca92349SMatthew G. Knepley 
3905f8d5e320SMatthew G. Knepley     for (c = 0; c < Ncn; ++c) {format[c*3+0] = '%'; format[c*3+1] = 'd'; format[c*3+2] = ' ';}
3906f8d5e320SMatthew G. Knepley     format[Ncn*3-1] = '\0';
3907f8d5e320SMatthew G. Knepley     for (c = 0; c < Nc; ++c) {ierr = DMPlexSetConeSize(*dm, c, Ncn);CHKERRQ(ierr);}
39088ca92349SMatthew G. Knepley     ierr = DMSetUp(*dm);CHKERRQ(ierr);
39098ca92349SMatthew G. Knepley     for (c = 0; c < Nc; ++c) {
3910f8d5e320SMatthew G. Knepley       ierr = PetscViewerRead(viewer, line, Ncn, NULL, PETSC_STRING);CHKERRQ(ierr);
3911f8d5e320SMatthew G. Knepley       switch (Ncn) {
3912f8d5e320SMatthew G. Knepley         case 2: snum = sscanf(line, format, &vbuf[0], &vbuf[1]);break;
3913f8d5e320SMatthew G. Knepley         case 3: snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2]);break;
3914f8d5e320SMatthew G. Knepley         case 4: snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3]);break;
3915f8d5e320SMatthew G. Knepley         case 6: snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5]);break;
3916f8d5e320SMatthew 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;
3917f8d5e320SMatthew G. Knepley         default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No cell shape with %D vertices", Ncn);
3918f8d5e320SMatthew G. Knepley       }
3919f8d5e320SMatthew G. Knepley       if (snum != Ncn) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
3920f8d5e320SMatthew G. Knepley       for (v = 0; v < Ncn; ++v) cone[v] = vbuf[v] + Nc;
39218ca92349SMatthew G. Knepley       /* Hexahedra are inverted */
3922f8d5e320SMatthew G. Knepley       if (Ncn == 8) {
39238ca92349SMatthew G. Knepley         PetscInt tmp = cone[1];
39248ca92349SMatthew G. Knepley         cone[1] = cone[3];
39258ca92349SMatthew G. Knepley         cone[3] = tmp;
39268ca92349SMatthew G. Knepley       }
39278ca92349SMatthew G. Knepley       ierr = DMPlexSetCone(*dm, c, cone);CHKERRQ(ierr);
39288ca92349SMatthew G. Knepley     }
39298ca92349SMatthew G. Knepley   }
39308ca92349SMatthew G. Knepley   ierr = DMPlexSymmetrize(*dm);CHKERRQ(ierr);
39318ca92349SMatthew G. Knepley   ierr = DMPlexStratify(*dm);CHKERRQ(ierr);
39328ca92349SMatthew G. Knepley   /* Read coordinates */
39338ca92349SMatthew G. Knepley   ierr = DMGetCoordinateSection(*dm, &coordSection);CHKERRQ(ierr);
39348ca92349SMatthew G. Knepley   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
39358ca92349SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(coordSection, 0, cdim);CHKERRQ(ierr);
39368ca92349SMatthew G. Knepley   ierr = PetscSectionSetChart(coordSection, Nc, Nc + Nv);CHKERRQ(ierr);
39378ca92349SMatthew G. Knepley   for (v = Nc; v < Nc+Nv; ++v) {
39388ca92349SMatthew G. Knepley     ierr = PetscSectionSetDof(coordSection, v, cdim);CHKERRQ(ierr);
39398ca92349SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(coordSection, v, 0, cdim);CHKERRQ(ierr);
39408ca92349SMatthew G. Knepley   }
39418ca92349SMatthew G. Knepley   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
39428ca92349SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
39438ca92349SMatthew G. Knepley   ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
39448ca92349SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
39458ca92349SMatthew G. Knepley   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
39468ca92349SMatthew G. Knepley   ierr = VecSetBlockSize(coordinates, cdim);CHKERRQ(ierr);
39478ca92349SMatthew G. Knepley   ierr = VecSetType(coordinates, VECSTANDARD);CHKERRQ(ierr);
39488ca92349SMatthew G. Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
3949dd400576SPatrick Sanan   if (rank == 0) {
3950f8d5e320SMatthew G. Knepley     char   format[PETSC_MAX_PATH_LEN];
39518ca92349SMatthew G. Knepley     double x[3];
3952f8d5e320SMatthew G. Knepley     int    l, val[3];
39538ca92349SMatthew G. Knepley 
3954f8d5e320SMatthew G. Knepley     if (Nl) {
3955f8d5e320SMatthew G. Knepley       for (l = 0; l < Nl; ++l) {format[l*3+0] = '%'; format[l*3+1] = 'd'; format[l*3+2] = ' ';}
3956f8d5e320SMatthew G. Knepley       format[Nl*3-1] = '\0';
39578ca92349SMatthew G. Knepley       ierr = DMCreateLabel(*dm, "marker");CHKERRQ(ierr);
39588ca92349SMatthew G. Knepley       ierr = DMGetLabel(*dm, "marker", &marker);CHKERRQ(ierr);
3959f8d5e320SMatthew G. Knepley     }
39608ca92349SMatthew G. Knepley     for (v = 0; v < Nv; ++v) {
3961f8d5e320SMatthew G. Knepley       ierr = PetscViewerRead(viewer, line, 3+Nl, NULL, PETSC_STRING);CHKERRQ(ierr);
3962f8d5e320SMatthew G. Knepley       snum = sscanf(line, "%lg %lg %lg", &x[0], &x[1], &x[2]);
3963f8d5e320SMatthew G. Knepley       if (snum != 3) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
3964f8d5e320SMatthew G. Knepley       switch (Nl) {
3965f8d5e320SMatthew G. Knepley         case 0: snum = 0;break;
3966f8d5e320SMatthew G. Knepley         case 1: snum = sscanf(line, format, &val[0]);break;
3967f8d5e320SMatthew G. Knepley         case 2: snum = sscanf(line, format, &val[0], &val[1]);break;
3968f8d5e320SMatthew G. Knepley         case 3: snum = sscanf(line, format, &val[0], &val[1], &val[2]);break;
3969f8d5e320SMatthew G. Knepley         default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Request support for %D labels", Nl);
3970f8d5e320SMatthew G. Knepley       }
3971f8d5e320SMatthew G. Knepley       if (snum != Nl) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
39728ca92349SMatthew G. Knepley       for (d = 0; d < cdim; ++d) coords[v*cdim+d] = x[d];
3973f8d5e320SMatthew G. Knepley       for (l = 0; l < Nl; ++l) {ierr = DMLabelSetValue(marker, v+Nc, val[l]);CHKERRQ(ierr);}
39748ca92349SMatthew G. Knepley     }
39758ca92349SMatthew G. Knepley   }
39768ca92349SMatthew G. Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
39778ca92349SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(*dm, coordinates);CHKERRQ(ierr);
39788ca92349SMatthew G. Knepley   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
39798ca92349SMatthew G. Knepley   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
39808ca92349SMatthew G. Knepley   if (interpolate) {
39818ca92349SMatthew G. Knepley     DM      idm;
39828ca92349SMatthew G. Knepley     DMLabel bdlabel;
39838ca92349SMatthew G. Knepley 
39848ca92349SMatthew G. Knepley     ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr);
39858ca92349SMatthew G. Knepley     ierr = DMDestroy(dm);CHKERRQ(ierr);
39868ca92349SMatthew G. Knepley     *dm  = idm;
39878ca92349SMatthew G. Knepley 
3988f8d5e320SMatthew G. Knepley     if (!Nl) {
3989f8d5e320SMatthew G. Knepley       ierr = DMCreateLabel(*dm, "marker");CHKERRQ(ierr);
39908ca92349SMatthew G. Knepley       ierr = DMGetLabel(*dm, "marker", &bdlabel);CHKERRQ(ierr);
39918ca92349SMatthew G. Knepley       ierr = DMPlexMarkBoundaryFaces(*dm, PETSC_DETERMINE, bdlabel);CHKERRQ(ierr);
39928ca92349SMatthew G. Knepley       ierr = DMPlexLabelComplete(*dm, bdlabel);CHKERRQ(ierr);
39938ca92349SMatthew G. Knepley     }
3994f8d5e320SMatthew G. Knepley   }
39958ca92349SMatthew G. Knepley   PetscFunctionReturn(0);
39968ca92349SMatthew G. Knepley }
39978ca92349SMatthew G. Knepley 
39988ca92349SMatthew G. Knepley /*@C
3999ca522641SMatthew G. Knepley   DMPlexCreateFromFile - This takes a filename and produces a DM
4000ca522641SMatthew G. Knepley 
4001ca522641SMatthew G. Knepley   Input Parameters:
4002ca522641SMatthew G. Knepley + comm - The communicator
4003ca522641SMatthew G. Knepley . filename - A file name
4004cd7e8a5eSksagiyam . plexname - The object name of the resulting DM, also used for intra-datafile lookup by some formats
4005ca522641SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
4006ca522641SMatthew G. Knepley 
4007ca522641SMatthew G. Knepley   Output Parameter:
4008ca522641SMatthew G. Knepley . dm - The DM
4009ca522641SMatthew G. Knepley 
401002ef0d99SVaclav Hapla   Options Database Keys:
401102ef0d99SVaclav Hapla . -dm_plex_create_from_hdf5_xdmf - use the PETSC_VIEWER_HDF5_XDMF format for reading HDF5
401202ef0d99SVaclav Hapla 
4013bca97951SVaclav Hapla   Use -dm_plex_create_ prefix to pass options to the internal PetscViewer, e.g.
4014bca97951SVaclav Hapla $ -dm_plex_create_viewer_hdf5_collective
4015bca97951SVaclav Hapla 
4016cd7e8a5eSksagiyam   Notes:
4017cd7e8a5eSksagiyam   Using PETSCVIEWERHDF5 type with PETSC_VIEWER_HDF5_PETSC format, one can save multiple DMPlex
4018cd7e8a5eSksagiyam   meshes in a single HDF5 file. This in turn requires one to name the DMPlex object with PetscObjectSetName()
4019cd7e8a5eSksagiyam   before saving it with DMView() and before loading it with DMLoad() for identification of the mesh object.
4020cd7e8a5eSksagiyam   The input parameter name is thus used to name the DMPlex object when DMPlexCreateFromFile() internally
4021cd7e8a5eSksagiyam   calls DMLoad(). Currently, name is ignored for other viewer types and/or formats.
4022cd7e8a5eSksagiyam 
4023ca522641SMatthew G. Knepley   Level: beginner
4024ca522641SMatthew G. Knepley 
4025cd7e8a5eSksagiyam .seealso: DMPlexCreateFromDAG(), DMPlexCreateFromCellListPetsc(), DMPlexCreate(), PetscObjectSetName(), DMView(), DMLoad()
4026ca522641SMatthew G. Knepley @*/
4027cd7e8a5eSksagiyam PetscErrorCode DMPlexCreateFromFile(MPI_Comm comm, const char filename[], const char plexname[], PetscBool interpolate, DM *dm)
4028ca522641SMatthew G. Knepley {
4029ca522641SMatthew G. Knepley   const char    *extGmsh      = ".msh";
4030de78e4feSLisandro Dalcin   const char    *extGmsh2     = ".msh2";
4031de78e4feSLisandro Dalcin   const char    *extGmsh4     = ".msh4";
4032ca522641SMatthew G. Knepley   const char    *extCGNS      = ".cgns";
4033ca522641SMatthew G. Knepley   const char    *extExodus    = ".exo";
403490c68965SMatthew G. Knepley   const char    *extGenesis   = ".gen";
40352f0bd6dcSMichael Lange   const char    *extFluent    = ".cas";
4036cc2f8f65SMatthew G. Knepley   const char    *extHDF5      = ".h5";
4037707dd687SMichael Lange   const char    *extMed       = ".med";
4038f2801cd6SMatthew G. Knepley   const char    *extPLY       = ".ply";
4039c1cad2e7SMatthew G. Knepley   const char    *extEGADSLite = ".egadslite";
4040c1cad2e7SMatthew G. Knepley   const char    *extEGADS     = ".egads";
4041c1cad2e7SMatthew G. Knepley   const char    *extIGES      = ".igs";
4042c1cad2e7SMatthew G. Knepley   const char    *extSTEP      = ".stp";
40438ca92349SMatthew G. Knepley   const char    *extCV        = ".dat";
4044ca522641SMatthew G. Knepley   size_t         len;
4045c1cad2e7SMatthew G. Knepley   PetscBool      isGmsh, isGmsh2, isGmsh4, isCGNS, isExodus, isGenesis, isFluent, isHDF5, isMed, isPLY, isEGADSLite, isEGADS, isIGES, isSTEP, isCV;
4046ca522641SMatthew G. Knepley   PetscMPIInt    rank;
4047ca522641SMatthew G. Knepley   PetscErrorCode ierr;
4048ca522641SMatthew G. Knepley 
4049ca522641SMatthew G. Knepley   PetscFunctionBegin;
40505d80c0bfSVaclav Hapla   PetscValidCharPointer(filename, 2);
4051*0d862eaeSPierre Jolivet   if (plexname) PetscValidCharPointer(plexname, 3);
4052cd7e8a5eSksagiyam   PetscValidPointer(dm, 5);
4053f1f45c63SVaclav Hapla   ierr = DMInitializePackage();CHKERRQ(ierr);
4054f1f45c63SVaclav Hapla   ierr = PetscLogEventBegin(DMPLEX_CreateFromFile,0,0,0,0);CHKERRQ(ierr);
4055ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
4056ca522641SMatthew G. Knepley   ierr = PetscStrlen(filename, &len);CHKERRQ(ierr);
4057ca522641SMatthew G. Knepley   if (!len) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Filename must be a valid path");
4058ca522641SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)],  extGmsh,      4, &isGmsh);CHKERRQ(ierr);
4059de78e4feSLisandro Dalcin   ierr = PetscStrncmp(&filename[PetscMax(0,len-5)],  extGmsh2,     5, &isGmsh2);CHKERRQ(ierr);
4060de78e4feSLisandro Dalcin   ierr = PetscStrncmp(&filename[PetscMax(0,len-5)],  extGmsh4,     5, &isGmsh4);CHKERRQ(ierr);
4061ca522641SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-5)],  extCGNS,      5, &isCGNS);CHKERRQ(ierr);
4062ca522641SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)],  extExodus,    4, &isExodus);CHKERRQ(ierr);
406390c68965SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)],  extGenesis,   4, &isGenesis);CHKERRQ(ierr);
40642f0bd6dcSMichael Lange   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)],  extFluent,    4, &isFluent);CHKERRQ(ierr);
4065cc2f8f65SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-3)],  extHDF5,      3, &isHDF5);CHKERRQ(ierr);
4066707dd687SMichael Lange   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)],  extMed,       4, &isMed);CHKERRQ(ierr);
4067f2801cd6SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)],  extPLY,       4, &isPLY);CHKERRQ(ierr);
4068c1cad2e7SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-10)], extEGADSLite, 10, &isEGADSLite);CHKERRQ(ierr);
4069c1cad2e7SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-6)],  extEGADS,     6, &isEGADS);CHKERRQ(ierr);
4070c1cad2e7SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)],  extIGES,      4, &isIGES);CHKERRQ(ierr);
4071c1cad2e7SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)],  extSTEP,      4, &isSTEP);CHKERRQ(ierr);
40728ca92349SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)],  extCV,        4, &isCV);CHKERRQ(ierr);
4073de78e4feSLisandro Dalcin   if (isGmsh || isGmsh2 || isGmsh4) {
40747d282ae0SMichael Lange     ierr = DMPlexCreateGmshFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
4075ca522641SMatthew G. Knepley   } else if (isCGNS) {
4076ca522641SMatthew G. Knepley     ierr = DMPlexCreateCGNSFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
407790c68965SMatthew G. Knepley   } else if (isExodus || isGenesis) {
4078ca522641SMatthew G. Knepley     ierr = DMPlexCreateExodusFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
40792f0bd6dcSMichael Lange   } else if (isFluent) {
40802f0bd6dcSMichael Lange     ierr = DMPlexCreateFluentFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
4081cc2f8f65SMatthew G. Knepley   } else if (isHDF5) {
40829c48423bSVaclav Hapla     PetscBool      load_hdf5_xdmf = PETSC_FALSE;
4083cc2f8f65SMatthew G. Knepley     PetscViewer viewer;
4084cc2f8f65SMatthew G. Knepley 
408543b242b4SVaclav 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 */
408643b242b4SVaclav Hapla     ierr = PetscStrncmp(&filename[PetscMax(0,len-8)], ".xdmf",  5, &load_hdf5_xdmf);CHKERRQ(ierr);
40879c48423bSVaclav Hapla     ierr = PetscOptionsGetBool(NULL, NULL, "-dm_plex_create_from_hdf5_xdmf", &load_hdf5_xdmf, NULL);CHKERRQ(ierr);
4088cc2f8f65SMatthew G. Knepley     ierr = PetscViewerCreate(comm, &viewer);CHKERRQ(ierr);
4089cc2f8f65SMatthew G. Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERHDF5);CHKERRQ(ierr);
4090bca97951SVaclav Hapla     ierr = PetscViewerSetOptionsPrefix(viewer, "dm_plex_create_");CHKERRQ(ierr);
4091bca97951SVaclav Hapla     ierr = PetscViewerSetFromOptions(viewer);CHKERRQ(ierr);
4092cc2f8f65SMatthew G. Knepley     ierr = PetscViewerFileSetMode(viewer, FILE_MODE_READ);CHKERRQ(ierr);
4093cc2f8f65SMatthew G. Knepley     ierr = PetscViewerFileSetName(viewer, filename);CHKERRQ(ierr);
4094cd7e8a5eSksagiyam 
4095cc2f8f65SMatthew G. Knepley     ierr = DMCreate(comm, dm);CHKERRQ(ierr);
4096cd7e8a5eSksagiyam     ierr = PetscObjectSetName((PetscObject)(*dm), plexname);CHKERRQ(ierr);
4097cc2f8f65SMatthew G. Knepley     ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
40989c48423bSVaclav Hapla     if (load_hdf5_xdmf) {ierr = PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_XDMF);CHKERRQ(ierr);}
4099cc2f8f65SMatthew G. Knepley     ierr = DMLoad(*dm, viewer);CHKERRQ(ierr);
41009c48423bSVaclav Hapla     if (load_hdf5_xdmf) {ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);}
4101cc2f8f65SMatthew G. Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
41025fd9971aSMatthew G. Knepley 
41035fd9971aSMatthew G. Knepley     if (interpolate) {
41045fd9971aSMatthew G. Knepley       DM idm;
41055fd9971aSMatthew G. Knepley 
41065fd9971aSMatthew G. Knepley       ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr);
41075fd9971aSMatthew G. Knepley       ierr = DMDestroy(dm);CHKERRQ(ierr);
41085fd9971aSMatthew G. Knepley       *dm  = idm;
41095fd9971aSMatthew G. Knepley     }
4110cd7e8a5eSksagiyam     ierr = PetscObjectSetName((PetscObject)(*dm), plexname);CHKERRQ(ierr);
4111707dd687SMichael Lange   } else if (isMed) {
4112707dd687SMichael Lange     ierr = DMPlexCreateMedFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
4113f2801cd6SMatthew G. Knepley   } else if (isPLY) {
4114f2801cd6SMatthew G. Knepley     ierr = DMPlexCreatePLYFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
4115c1cad2e7SMatthew G. Knepley   } else if (isEGADSLite || isEGADS || isIGES || isSTEP) {
4116c1cad2e7SMatthew G. Knepley     if (isEGADSLite) {ierr = DMPlexCreateEGADSLiteFromFile(comm, filename, dm);CHKERRQ(ierr);}
4117c1cad2e7SMatthew G. Knepley     else             {ierr = DMPlexCreateEGADSFromFile(comm, filename, dm);CHKERRQ(ierr);}
41187bee2925SMatthew Knepley     if (!interpolate) {
41197bee2925SMatthew Knepley       DM udm;
41207bee2925SMatthew Knepley 
41217bee2925SMatthew Knepley       ierr = DMPlexUninterpolate(*dm, &udm);CHKERRQ(ierr);
41227bee2925SMatthew Knepley       ierr = DMDestroy(dm);CHKERRQ(ierr);
41237bee2925SMatthew Knepley       *dm  = udm;
41247bee2925SMatthew Knepley     }
41258ca92349SMatthew G. Knepley   } else if (isCV) {
41268ca92349SMatthew G. Knepley     ierr = DMPlexCreateCellVertexFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
4127ca522641SMatthew G. Knepley   } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename);
4128f1f45c63SVaclav Hapla   ierr = PetscLogEventEnd(DMPLEX_CreateFromFile,0,0,0,0);CHKERRQ(ierr);
4129ca522641SMatthew G. Knepley   PetscFunctionReturn(0);
4130ca522641SMatthew G. Knepley }
4131